bulk island generation, better island distribution algorhythm

This commit is contained in:
Kimapr 2019-12-29 22:37:41 +05:00
parent 1e2e9721e6
commit 17bef30b02
1 changed files with 61 additions and 31 deletions

View File

@ -121,10 +121,13 @@ local function island(pos,r)
end) end)
end end
local f,abs = math.floor, math.abs local f,abs = math.floor, math.abs
local function spawn_island(name,pos) local function raw_spawn_island(pos)
local ref = minetest.get_player_by_name(name)
pos = {x=f(pos.x),y=f(pos.y),z=f(pos.z)} pos = {x=f(pos.x),y=f(pos.y),z=f(pos.z)}
island(pos,48,name) island(pos,48,name)
end
local function spawn_island(name,pos)
local ref = minetest.get_player_by_name(name)
raw_spawn_island(pos)
ref:set_pos({x=pos.x,y=pos.y+256,z=pos.z}) ref:set_pos({x=pos.x,y=pos.y+256,z=pos.z})
end end
local store = minetest.get_mod_storage() local store = minetest.get_mod_storage()
@ -284,37 +287,56 @@ local function get_standing_island(name)
return closesti return closesti
end end
local function gen_island_pos(name) local function gen_island_pos(name,fgen)
for n=1,fgen or 1 do
minetest.after((n-1)*30,function()
local x,y,z = 0,0,0 local x,y,z = 0,0,0
x,z = x+math.random(-64,64),z+math.random(-64,64) x,z = x+math.random(-96,96),z+math.random(-96,96)
y=256+math.random(-32,32) y=256+math.random(-32,32)
for k,isl in ipairs(cells) do if not fgen then
for k,isl in pairs(cells) do
if isl and isl.valid then if isl and isl.valid then
table.remove(cells,k) table.remove(cells,k)
local ip = isl.pos local ip = isl.pos
local ref = minetest.get_player_by_name(name) local ref = minetest.get_player_by_name(name)
local pos = ip local pos = ip
players[name] = {pos = pos, valid = false} players[name] = {pos = pos, valid = true}
save() save()
pos = {x=f(pos.x),y=f(pos.y),z=f(pos.z)} pos = {x=f(pos.x),y=f(pos.y),z=f(pos.z)}
ref:set_pos({x=pos.x,y=pos.y+256,z=pos.z}) ref:set_pos({x=pos.x,y=pos.y+256,z=pos.z})
return return
end end
end end
end
local ok = checkpos(x,y,z) local ok = checkpos(x,y,z)
while not ok do while not ok do
x,z = x+math.random(-64,64),z+math.random(-64,64) local sx,sz = math.random(-96,96),math.random(-96,96)
if ((x+sx)^2+(z+sz)^2)^0.5 < (x^2+z^2)^0.5 then
sx=-sx
sz=-sz
end
x,z = x+sx,z+sz
y=256+math.random(-32,32) y=256+math.random(-32,32)
ok = checkpos(x,y,z) ok = checkpos(x,y,z)
end end
local ip = {x=x,y=y,z=z} local ip = {x=x,y=y,z=z}
if not fgen then
spawn_island(name,ip) spawn_island(name,ip)
else
raw_spawn_island(ip)
end
local pl = { local pl = {
pos = ip, pos = ip,
valid = false valid = true
} }
if not fgen then
players[name] = pl players[name] = pl
else
table.insert(cells,pl)
end
save() save()
end)
end
end end
local updrate = 0.5 local updrate = 0.5
local to_upd = updrate local to_upd = updrate
@ -515,6 +537,14 @@ minetest.register_chatcommand("reset",{
end end
}) })
minetest.register_chatcommand("islandgen",{
description = S"Bulk island generation",
privs = {interact = true, server = true},
func = function(name,param)
gen_island_pos(name,tonumber(param) or 1)
end
})
minetest.register_on_joinplayer(function(ref) minetest.register_on_joinplayer(function(ref)
local meta = ref:get_meta() local meta = ref:get_meta()
local name = ref:get_player_name() local name = ref:get_player_name()