Find spawn position and effectively spawn player (yay!)

This commit is contained in:
Gaël C 2024-02-11 09:26:05 +01:00
parent 6017510df0
commit c8b96e2836

View File

@ -43,7 +43,8 @@ local function estimate_spawn_level(pos, use_distort)
return true, h
end
function minetest.get_spawn_level(x, z)
local function get_spawn_level(x, z)
print(x, z)
local pos = {x=x, z=z}
local suitable, y = estimate_spawn_level(pos, false)
if not suitable then
@ -73,5 +74,47 @@ function minetest.get_spawn_level(x, z)
return
end
return high_bound
return high_bound + 1
end
minetest.get_spawn_level = get_spawn_level
local rmax = 2000
local function find_spawn_point(seed)
local level = get_spawn_level(0, 0)
if level then
return {x=0, y=level, z=0}
end
local pr = PcgRandom(seed or os.time())
local incr = 16
local r0 = 0
while r0 < rmax do
local r1 = r0 + incr
local r = pr:next(r0*r0+1, r1*r1) ^ 0.5
local a = pr:next() / 2147483648 * math.pi
local x = math.floor(math.cos(a) * r + 0.5)
local z = math.floor(math.sin(a) * r + 0.5)
level = get_spawn_level(x, z)
if level then
return {x=x, y=level, z=z}
end
r0 = r1
end
end
local function spawn_player(player)
if minetest.settings:get("static_spawnpoint") then
return
end
local spawn_point = find_spawn_point()
if spawn_point then
player:set_pos(spawn_point)
end
end
minetest.register_on_newplayer(spawn_player)
minetest.register_on_respawnplayer(spawn_player)