2015-08-31 11:25:29 +02:00
|
|
|
local teleporters = {}
|
2015-09-02 12:46:07 +02:00
|
|
|
local teleporters_rids = {}
|
|
|
|
|
2015-08-28 12:24:33 +02:00
|
|
|
|
|
|
|
local register = function(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)
|
2015-08-28 12:24:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local teleport_nearest = function(pos)
|
2015-09-04 17:58:37 +02:00
|
|
|
local MAX_TELEPORTATION_DISTANCE = 50
|
|
|
|
local MAX_PLAYER_DISTANCE = 25
|
2015-12-19 19:18:04 +01:00
|
|
|
|
2015-08-28 12:24:33 +02:00
|
|
|
-- Search the nearest player
|
|
|
|
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())
|
|
|
|
if distance < min_distance then
|
|
|
|
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
|
|
|
|
-- If there is no nearest player (maybe too far...)
|
|
|
|
return
|
|
|
|
end
|
2015-12-19 19:18:04 +01:00
|
|
|
|
2015-08-28 12:24:33 +02:00
|
|
|
-- Search other teleporter and teleport
|
|
|
|
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 = {}
|
2015-08-28 12:24:33 +02:00
|
|
|
for i = 1, #teleporters do
|
|
|
|
if minetest.get_node(teleporters[i]).name == "moremesecons_teleporter:teleporter" then
|
|
|
|
if teleporters[i].y == pos.y and teleporters[i].x == pos.x and teleporters[i].z ~= pos.z then
|
2015-08-29 18:09:55 +02:00
|
|
|
newpos = {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
|
2015-08-29 18:09:55 +02:00
|
|
|
newpos = {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
|
2015-08-29 18:09:55 +02:00
|
|
|
newpos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z}
|
2015-08-28 12:24:33 +02:00
|
|
|
end
|
|
|
|
end
|
2015-08-29 18:09:55 +02:00
|
|
|
end
|
|
|
|
if newpos.x then
|
2015-09-04 17:58:37 +02:00
|
|
|
-- If there is another teleporter BUT too far, delete newpos.
|
|
|
|
if vector.distance(newpos, pos) > MAX_TELEPORTATION_DISTANCE then
|
|
|
|
newpos = {}
|
|
|
|
end
|
2015-09-03 19:15:25 +02:00
|
|
|
end
|
|
|
|
if not newpos.x then
|
|
|
|
newpos = {x=pos.x, y=pos.y+1, z=pos.z} -- If newpos doesn't exist, teleport on the actual teleporter.
|
|
|
|
end
|
|
|
|
nearest:moveto(newpos)
|
2015-12-19 19:18:04 +01:00
|
|
|
minetest.log("action", "Player "..nearest:get_player_name().." was teleport with 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)
|
2015-08-31 11:25:29 +02:00
|
|
|
end
|
|
|
|
end,
|
2015-08-28 12:24:33 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2016-03-22 11:41:19 +01:00
|
|
|
minetest.register_lbm({
|
|
|
|
name = "moremesecons_teleporter:add_teleporter",
|
2015-08-28 12:24:33 +02:00
|
|
|
nodenames = {"moremesecons_teleporter:teleporter"},
|
2016-03-22 11:41:19 +01:00
|
|
|
action = register
|
2015-08-28 12:24:33 +02:00
|
|
|
})
|