mirror of
https://gitlab.com/gaelysam/mapgen_rivers.git
synced 2024-12-28 03:40:39 +01:00
Find spawn position and effectively spawn player (yay!)
This commit is contained in:
parent
6017510df0
commit
c8b96e2836
47
spawn.lua
47
spawn.lua
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user