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 {}
|
|
|
|
local teleporters_rids = minetest.deserialize(storage:get_string("teleporters_rids")) or {}
|
|
|
|
local jammers = minetest.deserialize(storage:get_string("jammers")) or {}
|
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))
|
|
|
|
storage:set_string("teleporters_rids", minetest.serialize(teleporters_rids))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function register(pos)
|
2015-12-19 19:18:04 +01:00
|
|
|
if not vector.get_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x) then
|
2015-08-28 12:24:33 +02:00
|
|
|
table.insert(teleporters, pos)
|
2015-09-02 12:46:07 +02:00
|
|
|
vector.set_data_to_pos(teleporters_rids, pos.z,pos.y,pos.x, #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)
|
2015-09-02 12:46:07 +02:00
|
|
|
local RID = vector.get_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x)
|
|
|
|
if RID then
|
2015-08-31 11:25:29 +02:00
|
|
|
table.remove(teleporters, RID)
|
2015-09-02 12:46:07 +02:00
|
|
|
vector.remove_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x)
|
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
|
2018-12-31 17:26:41 +01:00
|
|
|
|
|
|
|
minetest.log("action", "[moremesecons_teleporter] loaded.")
|