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)
local x,y,z = 0,0,0 for n=1,fgen or 1 do
x,z = x+math.random(-64,64),z+math.random(-64,64) minetest.after((n-1)*30,function()
y=256+math.random(-32,32) local x,y,z = 0,0,0
for k,isl in ipairs(cells) do x,z = x+math.random(-96,96),z+math.random(-96,96)
if isl and isl.valid then y=256+math.random(-32,32)
table.remove(cells,k) if not fgen then
local ip = isl.pos for k,isl in pairs(cells) do
local ref = minetest.get_player_by_name(name) if isl and isl.valid then
local pos = ip table.remove(cells,k)
players[name] = {pos = pos, valid = false} local ip = isl.pos
local ref = minetest.get_player_by_name(name)
local pos = ip
players[name] = {pos = pos, valid = true}
save()
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})
return
end
end
end
local ok = checkpos(x,y,z)
while not ok do
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)
ok = checkpos(x,y,z)
end
local ip = {x=x,y=y,z=z}
if not fgen then
spawn_island(name,ip)
else
raw_spawn_island(ip)
end
local pl = {
pos = ip,
valid = true
}
if not fgen then
players[name] = pl
else
table.insert(cells,pl)
end
save() save()
pos = {x=f(pos.x),y=f(pos.y),z=f(pos.z)} end)
ref:set_pos({x=pos.x,y=pos.y+256,z=pos.z})
return
end
end end
local ok = checkpos(x,y,z)
while not ok do
x,z = x+math.random(-64,64),z+math.random(-64,64)
y=256+math.random(-32,32)
ok = checkpos(x,y,z)
end
local ip = {x=x,y=y,z=z}
spawn_island(name,ip)
local pl = {
pos = ip,
valid = false
}
players[name] = pl
save()
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()