159 lines
5.1 KiB
Lua
159 lines
5.1 KiB
Lua
-- Imports & Exports -------------------------------------------------------------------------------
|
|
|
|
local core = _G.core
|
|
local jvmi = _G.jvmi
|
|
|
|
jvmi.register_mod("jvmi_player")
|
|
local private = {}
|
|
|
|
|
|
-- Constants ---------------------------------------------------------------------------------------
|
|
|
|
local C = jvmi.C
|
|
|
|
--[[ The multiplier on the player's speed when in "fast" mode. ]]--
|
|
local FAST_SPEED_MULTIPLIER = 10
|
|
|
|
--[[ The maximum number of times to put off setting the player's start position. ]]--
|
|
local MAX_PLAYER_SPAWN_DELAYS = 10
|
|
|
|
--[[ The default privs to be granted to a new player in a singleplayer game. ]]--
|
|
local SINGLEPLAYER_PRIVS = {
|
|
debug = true, fast = true, fly = true, give = true, interact = true, noclip = true,
|
|
privs = true, server = true, settime = true, teleport = true,
|
|
}
|
|
|
|
|
|
-- Private -----------------------------------------------------------------------------------------
|
|
|
|
--[[ Configure the player's hand with default settings for this game. ]]--
|
|
function private.configure_hand()
|
|
local hand_def = core.registered_items[""]
|
|
core.debug("hand: "..jvmi.dump(hand_def))
|
|
end
|
|
|
|
--[[
|
|
Set up privs for a new player.
|
|
|
|
Args:
|
|
player:ObjectRef - the player to be affected
|
|
]]--
|
|
function private.configure_privs(player)
|
|
core.set_player_privs(player:get_player_name(), SINGLEPLAYER_PRIVS)
|
|
end
|
|
|
|
--[[
|
|
Update the player physics for this particular game.
|
|
|
|
Args:
|
|
player:ObjectRef - the player to be affected
|
|
]]--
|
|
function private.configure_physics(player)
|
|
local physics = player:get_physics_override()
|
|
physics.speed_fast = FAST_SPEED_MULTIPLIER
|
|
player:set_physics_override(physics)
|
|
end
|
|
|
|
--[[
|
|
Find a player with a given name.
|
|
|
|
Args:
|
|
player_name:string - the name of the desired player
|
|
|
|
Returns:
|
|
an ObjectRef for the requested player, or `nil` if no such player is logged on
|
|
]]--
|
|
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
|
|
|
|
--[[
|
|
Configure the player's object when they join the server.
|
|
|
|
Args:
|
|
player:ObjectRef - the player who just joined
|
|
last_login:timestamp - when the player last logged in (or `nil` if they're brand new)
|
|
]]--
|
|
function private.on_player_joined(player, last_login)
|
|
if last_login then return end
|
|
|
|
private.configure_privs(player)
|
|
private.configure_physics(player)
|
|
private.set_starting_pos(player:get_player_name())
|
|
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 > MAX_PLAYER_SPAWN_DELAYS 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
|
|
|
|
|
|
-- Script ------------------------------------------------------------------------------------------
|
|
|
|
private.configure_hand()
|
|
core.register_on_joinplayer(private.on_player_joined)
|