Move content ids outside 'on gen'. More reliable meselamps in tunnels. Code improvements

master
paramat 2016-08-31 02:23:55 +01:00
parent 7fb407ab1e
commit e8137cdc17
3 changed files with 189 additions and 202 deletions

View File

@ -1,4 +1,4 @@
pathv7 0.1.3 by paramat pathv7 0.1.4 by paramat
For Minetest 0.4.14 and later For Minetest 0.4.14 and later
Depends default stairs Depends default stairs
Licenses: Code LGPLv2.1, textures CC BY-SA 3.0 Licenses: Code LGPLv2.1, textures CC BY-SA 3.0

389
init.lua
View File

@ -2,6 +2,7 @@
local HSAMP = 0.025 -- Height select amplitude. local HSAMP = 0.025 -- Height select amplitude.
-- Controls maximum steepness of paths. -- Controls maximum steepness of paths.
local DEBUG = false -- Print generation time
-- Mapgen v7 noises -- Mapgen v7 noises
@ -101,6 +102,40 @@ local np_column = {
dofile(minetest.get_modpath("pathv7") .. "/nodes.lua") dofile(minetest.get_modpath("pathv7") .. "/nodes.lua")
-- Constants
local c_wood = minetest.get_content_id("pathv7:junglewood")
local c_column = minetest.get_content_id("pathv7:bridgewood")
local c_stairn = minetest.get_content_id("pathv7:stairn")
local c_stairs = minetest.get_content_id("pathv7:stairs")
local c_staire = minetest.get_content_id("pathv7:staire")
local c_stairw = minetest.get_content_id("pathv7:stairw")
local c_stairne = minetest.get_content_id("pathv7:stairne")
local c_stairnw = minetest.get_content_id("pathv7:stairnw")
local c_stairse = minetest.get_content_id("pathv7:stairse")
local c_stairsw = minetest.get_content_id("pathv7:stairsw")
local c_air = minetest.CONTENT_AIR
local c_ignore = minetest.CONTENT_IGNORE
local c_stone = minetest.get_content_id("default:stone")
local c_sastone = minetest.get_content_id("default:sandstone")
local c_destone = minetest.get_content_id("default:desert_stone")
local c_ice = minetest.get_content_id("default:ice")
local c_tree = minetest.get_content_id("default:tree")
local c_leaves = minetest.get_content_id("default:leaves")
local c_apple = minetest.get_content_id("default:apple")
local c_jungletree = minetest.get_content_id("default:jungletree")
local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
local c_pinetree = minetest.get_content_id("default:pine_tree")
local c_pineneedles = minetest.get_content_id("default:pine_needles")
local c_snow = minetest.get_content_id("default:snow")
local c_acaciatree = minetest.get_content_id("default:acacia_tree")
local c_acacialeaves = minetest.get_content_id("default:acacia_leaves")
local c_aspentree = minetest.get_content_id("default:aspen_tree")
local c_aspenleaves = minetest.get_content_id("default:aspen_leaves")
local c_meselamp = minetest.get_content_id("default:meselamp")
-- Initialise noise objects to nil -- Initialise noise objects to nil
local nobj_base = nil local nobj_base = nil
@ -112,6 +147,7 @@ local nobj_pathc = nil
local nobj_pathd = nil local nobj_pathd = nil
local nobj_column = nil local nobj_column = nil
-- Localise noise buffers -- Localise noise buffers
local nbuf_base local nbuf_base
@ -138,60 +174,25 @@ minetest.register_on_generated(function(minp, maxp, seed)
local x0 = minp.x local x0 = minp.x
local y0 = minp.y local y0 = minp.y
local z0 = minp.z local z0 = minp.z
local c_air = minetest.CONTENT_AIR
local c_ignore = minetest.CONTENT_IGNORE
local c_tree = minetest.get_content_id("default:tree")
local c_sand = minetest.get_content_id("default:sand")
local c_dirt = minetest.get_content_id("default:dirt")
local c_grass = minetest.get_content_id("default:dirt_with_grass")
local c_drygrass = minetest.get_content_id("default:dirt_with_dry_grass")
local c_dirtsnow = minetest.get_content_id("default:dirt_with_snow")
local c_desand = minetest.get_content_id("default:desert_sand")
local c_stone = minetest.get_content_id("default:stone")
local c_sastone = minetest.get_content_id("default:sandstone")
local c_destone = minetest.get_content_id("default:desert_stone")
local c_ice = minetest.get_content_id("default:ice")
local c_meselamp = minetest.get_content_id("default:meselamp")
local c_gravel = minetest.get_content_id("default:gravel")
local c_tree = minetest.get_content_id("default:tree")
local c_jungletree = minetest.get_content_id("default:jungletree")
local c_pinetree = minetest.get_content_id("default:pine_tree")
local c_acaciatree = minetest.get_content_id("default:acacia_tree")
local c_aspentree = minetest.get_content_id("default:aspen_tree")
local c_wood = minetest.get_content_id("pathv7:junglewood")
local c_column = minetest.get_content_id("pathv7:bridgewood")
local c_stairn = minetest.get_content_id("pathv7:stairn")
local c_stairs = minetest.get_content_id("pathv7:stairs")
local c_staire = minetest.get_content_id("pathv7:staire")
local c_stairw = minetest.get_content_id("pathv7:stairw")
local c_stairne = minetest.get_content_id("pathv7:stairne")
local c_stairnw = minetest.get_content_id("pathv7:stairnw")
local c_stairse = minetest.get_content_id("pathv7:stairse")
local c_stairsw = minetest.get_content_id("pathv7:stairsw")
local sidelen = x1 - x0 + 1 local sidelen = x1 - x0 + 1
local emerlen = sidelen + 32 local emerlen = sidelen + 32
local overlen = sidelen + 5 local overlen = sidelen + 5
local chulens = {x = overlen, y = overlen, z = 1} local chulens = {x = overlen, y = overlen, z = 1}
local minpos = {x = x0 - 3, y = z0 - 3} local minpos = {x = x0 - 3, y = z0 - 3}
nobj_base = nobj_base or minetest.get_perlin_map(np_base, chulens) nobj_base = nobj_base or minetest.get_perlin_map(np_base, chulens)
nobj_alt = nobj_alt or minetest.get_perlin_map(np_alt, chulens) nobj_alt = nobj_alt or minetest.get_perlin_map(np_alt, chulens)
nobj_select = nobj_select or minetest.get_perlin_map(np_select, chulens) nobj_select = nobj_select or minetest.get_perlin_map(np_select, chulens)
nobj_patha = nobj_patha or minetest.get_perlin_map(np_patha, chulens) nobj_patha = nobj_patha or minetest.get_perlin_map(np_patha, chulens)
nobj_pathb = nobj_pathb or minetest.get_perlin_map(np_pathb, chulens) nobj_pathb = nobj_pathb or minetest.get_perlin_map(np_pathb, chulens)
nobj_pathc = nobj_pathc or minetest.get_perlin_map(np_pathc, chulens) nobj_pathc = nobj_pathc or minetest.get_perlin_map(np_pathc, chulens)
nobj_pathd = nobj_pathd or minetest.get_perlin_map(np_pathd, chulens) nobj_pathd = nobj_pathd or minetest.get_perlin_map(np_pathd, chulens)
nobj_column = nobj_column or minetest.get_perlin_map(np_column, chulens) nobj_column = nobj_column or minetest.get_perlin_map(np_column, chulens)
local nvals_base = nobj_base :get2dMap_flat({x = x0 - 3, y = z0 - 3}, nbuf_base) local nvals_base = nobj_base :get2dMap_flat(minpos, nbuf_base)
local nvals_alt = nobj_alt :get2dMap_flat({x = x0 - 3, y = z0 - 3}, nbuf_alt) local nvals_alt = nobj_alt :get2dMap_flat(minpos, nbuf_alt)
local nvals_select = nobj_select:get2dMap_flat({x = x0 - 3, y = z0 - 3}, nbuf_select) local nvals_select = nobj_select:get2dMap_flat(minpos, nbuf_select)
local nvals_patha = nobj_patha :get2dMap_flat(minpos, nbuf_patha) local nvals_patha = nobj_patha :get2dMap_flat(minpos, nbuf_patha)
local nvals_pathb = nobj_pathb :get2dMap_flat(minpos, nbuf_pathb) local nvals_pathb = nobj_pathb :get2dMap_flat(minpos, nbuf_pathb)
local nvals_pathc = nobj_pathc :get2dMap_flat(minpos, nbuf_pathc) local nvals_pathc = nobj_pathc :get2dMap_flat(minpos, nbuf_pathc)
@ -203,7 +204,6 @@ minetest.register_on_generated(function(minp, maxp, seed)
local data = vm:get_data() local data = vm:get_data()
local ni = 1 local ni = 1
local stable = {}
for z = z0 - 3, z1 + 2 do for z = z0 - 3, z1 + 2 do
local n_xprepatha = false local n_xprepatha = false
local n_xprepathb = false local n_xprepathb = false
@ -213,13 +213,10 @@ minetest.register_on_generated(function(minp, maxp, seed)
for x = x0 - 3, x1 + 2 do for x = x0 - 3, x1 + 2 do
local n_patha = nvals_patha[ni] local n_patha = nvals_patha[ni]
local n_zprepatha = nvals_patha[(ni - overlen)] local n_zprepatha = nvals_patha[(ni - overlen)]
local n_pathb = nvals_pathb[ni] local n_pathb = nvals_pathb[ni]
local n_zprepathb = nvals_pathb[(ni - overlen)] local n_zprepathb = nvals_pathb[(ni - overlen)]
local n_pathc = nvals_pathc[ni] local n_pathc = nvals_pathc[ni]
local n_zprepathc = nvals_pathc[(ni - overlen)] local n_zprepathc = nvals_pathc[(ni - overlen)]
local n_pathd = nvals_pathd[ni] local n_pathd = nvals_pathd[ni]
local n_zprepathd = nvals_pathd[(ni - overlen)] local n_zprepathd = nvals_pathd[(ni - overlen)]
@ -234,7 +231,6 @@ minetest.register_on_generated(function(minp, maxp, seed)
local tblend = 0.5 + HSAMP * (select - 0.5) local tblend = 0.5 + HSAMP * (select - 0.5)
tblend = math.min(math.max(tblend, 0), 1) tblend = math.min(math.max(tblend, 0), 1)
local tlevel = math.floor(base * tblend + alt * (1 - tblend)) local tlevel = math.floor(base * tblend + alt * (1 - tblend))
-- TODO allow path above
local pathy = math.min(math.max(tlevel, 7), 42) local pathy = math.min(math.max(tlevel, 7), 42)
if (n_patha >= 0 and n_xprepatha < 0) -- detect sign change of noise if (n_patha >= 0 and n_xprepatha < 0) -- detect sign change of noise
@ -256,178 +252,167 @@ minetest.register_on_generated(function(minp, maxp, seed)
or (n_pathd < 0 and n_xprepathd >= 0) or (n_pathd < 0 and n_xprepathd >= 0)
or (n_pathd >= 0 and n_zprepathd < 0) or (n_pathd >= 0 and n_zprepathd < 0)
or (n_pathd < 0 and n_zprepathd >= 0) then or (n_pathd < 0 and n_zprepathd >= 0) then
if pathy > y1 then -- scan disk 5 nodes above path
-- build columns through this chunk local tunnel = false
if abscol < 0.3 then local excatop
for xx = x - 1, x + 1, 2 do for zz = z - 2, z + 2 do
for zz = z - 1, z + 1, 2 do local vi = area:index(x - 2, pathy + 5, zz)
local vi = area:index(xx, y1, zz) for xx = x - 2, x + 2 do
for y = 1, sidelen do local nodid = data[vi]
local nodid = data[vi] if nodid == c_stone
if nodid == c_stone or nodid == c_destone
or nodid == c_destone or nodid == c_sastone
or nodid == c_sastone or nodid == c_ice then
or nodid == c_ice then tunnel = true
break
else
data[vi] = c_column
end
vi = vi - emerlen
end
end
end end
vi = vi + 1
end end
elseif pathy >= y0 then end
-- path in chunk if tunnel then
-- scan disk 5 nodes above path excatop = pathy + 5
local tunnel = false else
local excatop excatop = y1
for zz = z - 2, z + 2 do end
local vi = area:index(x - 2, pathy + 5, zz) -- place path node brush
for xx = x - 2, x + 2 do local vi = area:index(x - 2, pathy, z - 2)
if data[vi] ~= c_wood then
data[vi] = c_stairne
end
for iter = 1, 3 do
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairn
end
end
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairnw
end
for zz = z - 1, z + 1 do
local vi = area:index(x - 2, pathy, zz)
if data[vi] ~= c_wood then
data[vi] = c_staire
end
for iter = 1, 3 do
vi = vi + 1
data[vi] = c_wood
end
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairw
end
end
local vi = area:index(x - 2, pathy, z + 2)
if data[vi] ~= c_wood then
data[vi] = c_stairse
end
for iter = 1, 3 do
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairs
end
end
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairsw
end
-- bridge understructure
for zz = z - 1, z + 1 do
local vi = area:index(x - 1, pathy - 1, zz)
for xx = x - 1, x + 1 do
local nodid = data[vi]
if nodid ~= c_stone
and nodid ~= c_destone
and nodid ~= c_sastone then
data[vi] = c_column
end
vi = vi + 1
end
end
local vi = area:index(x, pathy - 2, z)
data[vi] = c_column
-- bridge columns
if abscol < 0.3 then
for xx = x - 1, x + 1, 2 do
for zz = z - 1, z + 1, 2 do
local vi = area:index(xx, pathy - 2, zz)
for y = pathy - 2, y0, -1 do
local nodid = data[vi] local nodid = data[vi]
if nodid == c_stone if nodid == c_stone
or nodid == c_destone or nodid == c_destone
or nodid == c_sastone or nodid == c_sastone then
or nodid == c_ice then break
tunnel = true else
data[vi] = c_column
end end
vi = vi + 1 vi = vi - emerlen
end end
end end
if tunnel then
excatop = pathy + 5 -- tunnel
else
excatop = y1 + 16 -- excavate to vm top
end end
end
-- place path node brush -- excavate above path
local vi = area:index(x - 2, pathy, z - 2) local det_destone = false
if data[vi] ~= c_wood then local det_sastone = false
data[vi] = c_stairne local det_ice = false
end for y = pathy + 1, excatop do
for iter = 1, 3 do for zz = z - 2, z + 2 do
vi = vi + 1 local vi = area:index(x - 2, y, zz)
if data[vi] ~= c_wood then for xx = x - 2, x + 2 do
data[vi] = c_stairn local nodid = data[vi]
end if nodid == c_destone then
end det_destone = true
vi = vi + 1 elseif nodid == c_sastone then
if data[vi] ~= c_wood then det_sastone = true
data[vi] = c_stairnw elseif nodid == c_ice then
end det_ice = true
for zz = z - 1, z + 1 do end
local vi = area:index(x - 2, pathy, zz) if tunnel and y == excatop then -- tunnel ceiling
if data[vi] ~= c_wood then if nodid ~= c_air
data[vi] = c_staire and nodid ~= c_ignore
end and nodid ~= c_meselamp then
for iter = 1, 3 do if (math.abs(zz - z) == 2
vi = vi + 1 or math.abs(xx - x) == 2)
data[vi] = c_wood and math.random() <= 0.2 then
end data[vi] = c_meselamp
vi = vi + 1 elseif det_destone then
if data[vi] ~= c_wood then data[vi] = c_destone
data[vi] = c_stairw elseif det_sastone then
end data[vi] = c_sastone
end elseif det_ice then
local vi = area:index(x - 2, pathy, z + 2) data[vi] = c_ice
if data[vi] ~= c_wood then else
data[vi] = c_stairse data[vi] = c_stone
end end
for iter = 1, 3 do
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairs
end
end
vi = vi + 1
if data[vi] ~= c_wood then
data[vi] = c_stairsw
end
-- excavate above path
local det_destone = false
local det_sastone = false
local det_ice = false
for y = pathy + 1, excatop do
for zz = z - 2, z + 2 do
local vi = area:index(x - 2, y, zz)
for xx = x - 2, x + 2 do
local nodid = data[vi]
if nodid == c_destone then
det_destone = true
elseif nodid == c_sastone then
det_sastone = true
elseif nodid == c_ice then
det_ice = true
end end
if tunnel and y == excatop then -- tunnel ceiling elseif y <= pathy + 5 then
if nodid ~= c_air if nodid ~= c_wood
and nodid ~= c_ignore and nodid ~= c_stairn
and nodid ~= c_meselamp then and nodid ~= c_stairs
if math.random() < 0.1 then and nodid ~= c_staire
data[vi] = c_meselamp and nodid ~= c_stairw
elseif det_destone then and nodid ~= c_stairne
data[vi] = c_destone and nodid ~= c_stairnw
elseif det_sastone then and nodid ~= c_stairse
data[vi] = c_sastone and nodid ~= c_stairsw then
elseif det_ice then
data[vi] = c_ice
else
data[vi] = c_stone
end
end
elseif y <= pathy + 5 then
if nodid ~= c_wood
and nodid ~= c_stairn
and nodid ~= c_stairs
and nodid ~= c_staire
and nodid ~= c_stairw
and nodid ~= c_stairne
and nodid ~= c_stairnw
and nodid ~= c_stairse
and nodid ~= c_stairsw then
data[vi] = c_air
end
else
data[vi] = c_air data[vi] = c_air
end end
vi = vi + 1 elseif nodid == c_tree
or nodid == c_leaves
or nodid == c_apple
or nodid == c_jungletree
or nodid == c_jungleleaves
or nodid == c_pinetree
or nodid == c_pineneedles
or nodid == c_snow
or nodid == c_acaciatree
or nodid == c_acacialeaves
or nodid == c_aspentree
or nodid == c_aspenleaves then
data[vi] = c_air
end end
end
end
-- bridge understructure
for zz = z - 1, z + 1 do
local vi = area:index(x - 1, pathy - 1, zz)
for xx = x - 1, x + 1 do
data[vi] = c_column
vi = vi + 1 vi = vi + 1
end end
end end
local vi = area:index(x, pathy - 2, z)
data[vi] = c_column
-- bridge columns
if abscol < 0.3 then
for xx = x - 1, x + 1, 2 do
for zz = z - 1, z + 1, 2 do
local vi = area:index(xx, pathy - 2, zz)
for y = pathy - 2, y0, -1 do
local nodid = data[vi]
if nodid == c_stone
or nodid == c_destone
or nodid == c_sastone
or nodid == c_ice then
break
else
data[vi] = c_column
end
vi = vi - emerlen
end
end
end
end
end end
end end
end end
@ -446,5 +431,7 @@ minetest.register_on_generated(function(minp, maxp, seed)
vm:write_to_map(data) vm:write_to_map(data)
local chugent = math.ceil((os.clock() - t1) * 1000) local chugent = math.ceil((os.clock() - t1) * 1000)
print ("[pathv7] Generate chunk " .. chugent .. " ms") if DEBUG then
print ("[pathv7] Generate chunk " .. chugent .. " ms")
end
end) end)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 616 B