Initial galaxy creation loop. Space skybox in ignore nodes

This commit is contained in:
paramat 2016-07-02 06:17:13 +01:00
parent b06ff91ccc
commit 1656687dc5
2 changed files with 72 additions and 38 deletions

View File

@ -1,4 +1,4 @@
planets 0.1.2 by paramat planets 0.1.3 by paramat
For Minetest 0.4.13 and later For Minetest 0.4.13 and later
Depends default Depends default
Licenses: code WTFPL Licenses: code WTFPL

View File

@ -1,5 +1,8 @@
-- Parameters -- Parameters
local pnum = 1 -- number of planets desired
local maxatt = 128 -- maximum number of attempts to add a planet
local np_terrain = { local np_terrain = {
offset = 0, offset = 0,
scale = 1.0, scale = 1.0,
@ -27,10 +30,10 @@ dofile(minetest.get_modpath("planets") .. "/nodes.lua")
-- Mapgen parameters -- Mapgen parameters
minetest.set_mapgen_params({mgname = "singlenode", minetest.set_mapgen_params({mgname = "singlenode",
flags = "nolight", water_level = -31000}) chunksize = 5, water_level = -31000, flags = "nolight"})
-- Space array and planet def table -- Create pseudorandom galaxy
-- space is 64 ^ 3 chunks, centred on world centre -- space is 64 ^ 3 chunks, centred on world centre
-- space table is flat array of planet ids or nil (vacuum chunk) -- space table is flat array of planet ids or nil (vacuum chunk)
@ -39,41 +42,71 @@ local space = {}
local def = {} local def = {}
local spzstr = 64 * 64 -- space array z stride local spzstr = 64 * 64 -- space array z stride
local spystr = 64 -- space array y stride local spystr = 64 -- space array y stride
local plid = 0 -- planet id of last planet added, 0 = none
local addatt = 0 -- number of attempts to add a planet
while plid < pnum and addatt <= maxatt do -- avoid infinite attempts
-- create initial planet data to check for obstruction
-- cenx/y/z is planet centre -- cenx/y/z is planet centre
-- radter = terrain radius / water level -- radter = terrain radius / water level
-- radatm = atmosphere radius -- radmax = atmosphere radius or max mountain radius
-- plavar = planet variation
local cenx = 0 local cenx = 0
local ceny = 0 local ceny = 0
local cenz = 0 local cenz = 0
local radter = 256 local radter = 256
local radatm = 384 local radmax = 384
local plavar = 1
-- chunk co-ords of chunk containing planet centre -- chunk co-ords of chunk containing planet centre
-- measured from space origin (-32, -32, -32 chunks) -- measured from space origin (-32, -32, -32 chunks)
local cenxcc = math.floor((cenx + 32) / 80) + 32 local cenxcc = math.floor((cenx + 32) / 80) + 32
local cenycc = math.floor((ceny + 32) / 80) + 32 local cenycc = math.floor((ceny + 32) / 80) + 32
local cenzcc = math.floor((cenz + 32) / 80) + 32 local cenzcc = math.floor((cenz + 32) / 80) + 32
local radatmc = math.ceil(radatm / 80) -- planet radius in chunks local radmaxc = math.ceil(radmax / 80) -- planet radius in chunks
-- TODO add similar scan loop to check for existing planets -- check space is clear for planet
-- do this once centre and atmosphere radius are known and local clear = true -- is space clear
-- before calculating other properties or creating def table entry print ("[planets] Checking for obstruction")
-- planet 1 for cz = cenzcc - radmaxc, cenzcc + radmaxc do
def[1] = {i = cenx, j = ceny, k = cenz, t = radter, a = radatm, v = plavar} for cy = cenycc - radmaxc, cenycc + radmaxc do
local spi = cz * spzstr + cy * spystr + cenxcc - radmaxc + 1
for cx = cenxcc - radmaxc, cenxcc + radmaxc do
if space[spi] ~= nil then
clear = false
print ("[planets] Planet obstructed")
break
end
spi = spi + 1
end
if not clear then
break
end
end
if not clear then
break
end
end
for cz = cenzcc - radatmc, cenzcc + radatmc do if clear then -- add planet
for cy = cenycc - radatmc, cenycc + radatmc do -- TODO generate extra planet data
local spi = cz * spzstr + cy * spystr + cenxcc - radatmc + 1 plid = #def + 1 -- planet id
for cx = cenxcc - radatmc, cenxcc + radatmc do -- add planet data to def table
space[spi] = 1 def[plid] = {i = cenx, j = ceny, k = cenz, t = radter, m = radmax}
print ("[planets] Adding planet " .. plid)
for cz = cenzcc - radmaxc, cenzcc + radmaxc do
for cy = cenycc - radmaxc, cenycc + radmaxc do
local spi = cz * spzstr + cy * spystr + cenxcc - radmaxc + 1
for cx = cenxcc - radmaxc, cenxcc + radmaxc do
space[spi] = plid -- add planet id to this chunk
spi = spi + 1 spi = spi + 1
end end
end end
end end
end
addatt = addatt + 1
end
-- Globalstep function -- Globalstep function
@ -89,14 +122,15 @@ local skybox_space = {
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
for _, player in ipairs(minetest.get_connected_players()) do for _, player in ipairs(minetest.get_connected_players()) do
if math.random() < 0.02 then -- set gravity, skybox and override light if math.random() < 0.05 then -- set gravity, skybox and override light
local ppos = player:getpos() local ppos = player:getpos()
ppos.y = ppos.y + 1.5 -- node player head is in ppos.y = ppos.y + 1.5 -- node player head is in
if minetest.get_node(ppos).name == "planets:vacuum" then local nodename = minetest.get_node(ppos).name
if nodename == "planets:vacuum" or nodename == "ignore" then -- space
--player:set_physics_override(1, 1, 1) -- speed, jump, gravity --player:set_physics_override(1, 1, 1) -- speed, jump, gravity
player:set_sky({r = 0, g = 0, b = 0, a = 0}, "skybox", skybox_space) player:set_sky({r = 0, g = 0, b = 0, a = 0}, "skybox", skybox_space)
player:override_day_night_ratio(1) player:override_day_night_ratio(1)
else else -- regular sky for now
--player:set_physics_override(1, 1, 1) -- speed, jump, gravity --player:set_physics_override(1, 1, 1) -- speed, jump, gravity
player:set_sky({}, "regular", {}) player:set_sky({}, "regular", {})
player:override_day_night_ratio(nil) player:override_day_night_ratio(nil)
@ -165,7 +199,7 @@ minetest.register_on_generated(function(minp, maxp, seed)
local nvals_terrain = nobj_terrain:get3dMap_flat(pmapminp, nbuf_terrain) local nvals_terrain = nobj_terrain:get3dMap_flat(pmapminp, nbuf_terrain)
local nvals_cloud = nobj_cloud:get3dMap_flat(pmapminpclo, nbuf_cloud) local nvals_cloud = nobj_cloud:get3dMap_flat(pmapminpclo, nbuf_cloud)
local tersca = (pdef.a - pdef.t) / 2 -- terrain scale local tersca = (pdef.m - pdef.t) / 2 -- terrain scale
local ni = 1 -- noise index local ni = 1 -- noise index
for z = z0, z1 do for z = z0, z1 do
@ -195,7 +229,7 @@ minetest.register_on_generated(function(minp, maxp, seed)
if nvals_cloud[niq] > 0.5 then if nvals_cloud[niq] > 0.5 then
data[vi] = c_cloud data[vi] = c_cloud
end end
elseif nodrad <= pdef.a then -- air elseif nodrad <= pdef.m then -- air
data[vi] = c_air data[vi] = c_air
else -- vacuum else -- vacuum
data[vi] = c_vacuum data[vi] = c_vacuum
@ -227,5 +261,5 @@ minetest.register_on_generated(function(minp, maxp, seed)
vm:write_to_map(data) vm:write_to_map(data)
local chugent = math.ceil((os.clock() - t0) * 1000) local chugent = math.ceil((os.clock() - t0) * 1000)
print ("[planets] " .. chugent .. " ms") print ("[planets] Generated chunk " .. chugent .. " ms")
end) end)