diff --git a/moremesecons_teleporter/init.lua b/moremesecons_teleporter/init.lua index 0172796..7278167 100644 --- a/moremesecons_teleporter/init.lua +++ b/moremesecons_teleporter/init.lua @@ -23,49 +23,54 @@ local teleport_nearest = function(pos) MAX_PLAYER_DISTANCE = 25 end - -- Search the nearest player + -- Search for the nearest player local nearest = nil local min_distance = MAX_PLAYER_DISTANCE 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 + if distance <= min_distance then min_distance = distance nearest = player end end if not nearest then - -- If there is no nearest player (maybe too far...) + -- If there is no nearest player (maybe too far away...) return end - -- Search other teleporter and teleport + -- Search for the corresponding teleporter and teleport if not minetest.registered_nodes["moremesecons_teleporter:teleporter"] then return end local newpos = {} + local min_distance = MAX_TELEPORTATION_DISTANCE for i = 1, #teleporters do if minetest.get_node(teleporters[i]).name == "moremesecons_teleporter:teleporter" then + local tel_pos if teleporters[i].y == pos.y and teleporters[i].x == pos.x and teleporters[i].z ~= pos.z then - newpos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z} + tel_pos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z} elseif teleporters[i].z == pos.z and teleporters[i].x == pos.x and teleporters[i].y ~= pos.y then - newpos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z} + tel_pos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z} elseif teleporters[i].z == pos.z and teleporters[i].y == pos.y and teleporters[i].x ~= pos.x then - newpos = {x=teleporters[i].x, y=teleporters[i].y+1, z=teleporters[i].z} + 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 end end end - if newpos.x then - -- If there is another teleporter BUT too far, delete newpos. - if vector.distance(newpos, pos) > MAX_TELEPORTATION_DISTANCE then - newpos = {} - end - 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. + newpos = {x=pos.x, y=pos.y+1, z=pos.z} -- If newpos doesn't exist, teleport on the current teleporter end + nearest:moveto(newpos) - minetest.log("action", "Player "..nearest:get_player_name().." was teleport with a MoreMesecons Teleporter.") + minetest.log("action", "Player "..nearest:get_player_name().." was teleported using a MoreMesecons Teleporter.") end minetest.register_craft({