2017-06-23 13:47:41 +02:00
|
|
|
local storage = minetest.get_mod_storage()
|
2015-09-02 12:46:07 +02:00
|
|
|
|
2017-06-23 13:47:41 +02:00
|
|
|
local teleporters = minetest.deserialize(storage:get_string("teleporters")) or {}
|
2020-04-13 19:30:15 +02:00
|
|
|
local teleporters_rids = moremesecons.load_MapDataStorage_legacy(storage,
|
|
|
|
"teleporters_rids_v2", "teleporters_rids")
|
2015-08-28 12:24:33 +02:00
|
|
|
|
2017-04-17 11:17:09 +02:00
|
|
|
local function update_mod_storage()
|
|
|
|
storage:set_string("teleporters", minetest.serialize(teleporters))
|
2020-04-13 19:30:15 +02:00
|
|
|
storage:set_string("teleporters_rids_v2", teleporters_rids:serialize())
|
2017-04-17 11:17:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function register(pos)
|
2020-04-13 19:30:15 +02:00
|
|
|
if not teleporters_rids:getAt(pos) then
|
2015-08-28 12:24:33 +02:00
|
|
|
table.insert(teleporters, pos)
|
2020-04-13 19:30:15 +02:00
|
|
|
teleporters_rids:setAt(pos, #teleporters)
|
2017-04-17 11:17:09 +02:00
|
|
|
update_mod_storage()
|
2015-08-28 12:24:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-17 11:17:09 +02:00
|
|
|
local function teleport_nearest(pos)
|
2017-04-19 11:08:58 +02:00
|
|
|
local MAX_TELEPORTATION_DISTANCE = moremesecons.setting("teleporter", "max_t2t_distance", 50, 1)
|
|
|
|
local MAX_PLAYER_DISTANCE = moremesecons.setting("teleporter", "max_p2t_distance", 25, 1)
|
2015-12-19 19:18:04 +01:00
|
|
|
|
2017-03-12 16:58:48 +01:00
|
|
|
-- Search for the nearest player
|
2015-08-28 12:24:33 +02:00
|
|
|
local nearest = nil
|
2015-09-04 17:58:37 +02:00
|
|
|
local min_distance = MAX_PLAYER_DISTANCE
|
2015-08-28 12:24:33 +02:00
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
for index, player in pairs(players) do
|
|
|
|
local distance = vector.distance(pos, player:getpos())
|
2017-03-12 16:58:48 +01:00
|
|
|
if distance <= min_distance then
|
2015-08-28 12:24:33 +02:00
|
|
|
min_distance = distance
|
|
|
|
nearest = player
|
|
|
|
end
|
|
|
|
end
|
2015-12-19 19:18:04 +01:00
|
|
|
|
2015-09-04 17:58:37 +02:00
|
|
|
if not nearest then
|
2017-03-12 16:58:48 +01:00
|
|
|
-- If there is no nearest player (maybe too far away...)
|
2015-09-04 17:58:37 +02:00
|
|
|
return
|
|
|
|
end
|
2015-12-19 19:18:04 +01:00
|
|
|
|
2017-03-12 16:58:48 +01:00
|
|
|
-- Search for the corresponding teleporter and teleport
|
2015-08-28 12:24:33 +02:00
|
|
|
if not minetest.registered_nodes["moremesecons_teleporter:teleporter"] then return end
|
2015-12-19 19:18:04 +01:00
|
|
|
|
2015-08-29 18:09:55 +02:00
|
|
|
local newpos = {}
|
2017-03-12 16:58:48 +01:00
|
|
|
local min_distance = MAX_TELEPORTATION_DISTANCE
|
2015-08-28 12:24:33 +02:00
|
|
|
for i = 1, #teleporters do
|
|
|
|
if minetest.get_node(teleporters[i]).name == "moremesecons_teleporter:teleporter" then
|
2017-03-12 16:58:48 +01:00
|
|
|
local tel_pos
|
2015-08-28 12:24:33 +02:00
|
|
|
if teleporters[i].y == pos.y and teleporters[i].x == pos.x and teleporters[i].z ~= pos.z then
|
2017-03-12 16:58:48 +01:00
|
|
|
tel_pos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z}
|
2015-08-28 12:24:33 +02:00
|
|
|
elseif teleporters[i].z == pos.z and teleporters[i].x == pos.x and teleporters[i].y ~= pos.y then
|
2017-03-12 16:58:48 +01:00
|
|
|
tel_pos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z}
|
2015-08-28 12:24:33 +02:00
|
|
|
elseif teleporters[i].z == pos.z and teleporters[i].y == pos.y and teleporters[i].x ~= pos.x then
|
2017-03-12 16:58:48 +01:00
|
|
|
tel_pos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z}
|
|
|
|
end
|
|
|
|
|
|
|
|
if tel_pos then
|
|
|
|
local distance = vector.distance(tel_pos, pos)
|
|
|
|
if distance <= min_distance then
|
|
|
|
min_distance = distance
|
|
|
|
newpos = tel_pos
|
|
|
|
end
|
2015-08-28 12:24:33 +02:00
|
|
|
end
|
2015-09-04 17:58:37 +02:00
|
|
|
end
|
2015-09-03 19:15:25 +02:00
|
|
|
end
|
|
|
|
if not newpos.x then
|
2017-03-12 16:58:48 +01:00
|
|
|
newpos = {x=pos.x, y=pos.y+1, z=pos.z} -- If newpos doesn't exist, teleport on the current teleporter
|
2015-09-03 19:15:25 +02:00
|
|
|
end
|
2017-03-12 16:58:48 +01:00
|
|
|
|
2015-09-03 19:15:25 +02:00
|
|
|
nearest:moveto(newpos)
|
2017-03-12 16:58:48 +01:00
|
|
|
minetest.log("action", "Player "..nearest:get_player_name().." was teleported using a MoreMesecons Teleporter.")
|
2015-08-28 12:24:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_craft({
|
2015-08-29 12:33:03 +02:00
|
|
|
output = "moremesecons_teleporter:teleporter 2",
|
|
|
|
recipe = {{"default:diamond","default:stick","default:mese"}}
|
2015-08-28 12:24:33 +02:00
|
|
|
})
|
|
|
|
minetest.register_node("moremesecons_teleporter:teleporter", {
|
2015-08-30 11:49:32 +02:00
|
|
|
tiles = {"moremesecons_teleporter.png"},
|
2015-08-28 12:24:33 +02:00
|
|
|
paramtype = "light",
|
|
|
|
walkable = true,
|
|
|
|
groups = {cracky=3},
|
|
|
|
description="Teleporter",
|
|
|
|
mesecons = {effector = {
|
|
|
|
state = mesecon.state.off,
|
|
|
|
action_on = teleport_nearest
|
|
|
|
}},
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
2016-03-22 11:41:19 +01:00
|
|
|
on_construct = register,
|
2015-08-31 11:25:29 +02:00
|
|
|
on_destruct = function(pos)
|
2020-04-13 19:30:15 +02:00
|
|
|
local RID = teleporters_rids:getAt(pos)
|
2015-09-02 12:46:07 +02:00
|
|
|
if RID then
|
2015-08-31 11:25:29 +02:00
|
|
|
table.remove(teleporters, RID)
|
2020-04-13 19:30:15 +02:00
|
|
|
teleporters_rids:removeAt(pos)
|
2017-04-17 11:17:09 +02:00
|
|
|
update_mod_storage()
|
2015-08-31 11:25:29 +02:00
|
|
|
end
|
|
|
|
end,
|
2015-08-28 12:24:33 +02:00
|
|
|
})
|
2017-04-19 10:36:33 +02:00
|
|
|
|
2017-06-23 13:47:41 +02:00
|
|
|
if moremesecons.setting("teleporter", "enable_lbm", false) then
|
2017-04-19 10:36:33 +02:00
|
|
|
minetest.register_lbm({
|
|
|
|
name = "moremesecons_teleporter:add_teleporter",
|
|
|
|
nodenames = {"moremesecons_teleporter:teleporter"},
|
|
|
|
run_at_every_load = true,
|
|
|
|
action = register
|
|
|
|
})
|
|
|
|
end
|