-- Imports & Exports ------------------------------------------------------------------------------- local core = _G.core -- Constants --------------------------------------------------------------------------------------- local C = dofile(core.get_game_info().path.."/common/constants.lua") local SINGLEPLAYER_PRIVS = { debug = true, fast = true, fly = true, give = true, interact = true, noclip = true, privs = true, server = true, settime = true, teleport = true, } -- Private ----------------------------------------------------------------------------------------- local private = {} --[[ Find a player with a given name. Args: - player_name:string - the name of the desired player Returns: - an ObjectRef for the requested player if they're logged on, or nil ]]-- function private.find_player(name) for _, player in ipairs(core.get_connected_players()) do if player:get_player_name() == name then return player end end return nil end --[[ Move the indicated player into a safe spawn location. We're looking for somewhere which is an air block on top of something walkable. We start with the player's current position, and examine the current x/z column to find the first air block, and see if that will do. If not, we pick a new location a short distance away, and repeat. The process continues to expand outwards until a safe location is found, or we've looked half a km away without finding anything. If this function gets called before the world has finished generating, it will automatically delay for a bit and try again. After too many delays, it will simply abort the attempt. Args: player:ObjectRef - the player to relocate delays:int - how many times we've had to defer finding a location (0 by default) ]]-- function private.set_starting_pos(player_name, delays) delays = delays or 0 if delays > 10 then core.log("warning", "Too many delays in finding a safe spawning location: giving up") return end local player = private.find_player(player_name) local player_pos = player:get_pos() local node = core.get_node(player_pos) if node.name == "unknown" or node.name == "ignore" then core.log("action", "no map data, delaying player positioning") core.after(0.1, function() private.set_starting_pos(player_name, delays + 1) end) return end local pos = { x = player_pos.x, y = player_pos.y, z = player_pos.z } local distances = { 0, 8, 16, 32, 64, 128, 256, 512 } for _, distance in ipairs(distances) do for _, dx in ipairs({ 0, 1, -1 }) do pos.x = player_pos.x + dx * distance for _, dz in ipairs({ 0, 1, -1 }) do pos.z = player_pos.z + dz * distance local floor_node = nil for y = C.ALTITUDE.BEDROCK, C.ALTITUDE.SKY do pos.y = y node = core.get_node(pos) if floor_node and node.name == "air" then local floor_def = core.registered_nodes[floor_node.name] if floor_def.walkable then player:set_pos(pos) return end break end floor_node = node end end end end end --[[ Set up privledges and settings for a new player. Args: - player:ObjectRef - the current player ]]-- function private.setup_player(player) core.set_player_privs(player:get_player_name(), SINGLEPLAYER_PRIVS) local physics = player:get_physics_override() physics.speed_fast = 10 player:set_physics_override(physics) end -- Registrations ----------------------------------------------------------------------------------- core.register_on_joinplayer(function(player, last_login) if last_login then return end private.setup_player(player) private.set_starting_pos(player:get_player_name()) end)