test / test (push) Successful in 1m59s
- Layer rows can name a floor cover, soft pockets and a sea row. The underworld uses ash for both, and every cave cell at or below WORLD_BOTTOM + 28 is lava: a sea that is held in on every side, so it doesn't flow when woken. - Emberite ore in basalt (about 1.5% of the layer): glows, group level 4, so no current pickaxe (maxlevel 3) digs it. It waits for the top pre-hard-mode pickaxe in v0.5. - Ruined towers move to the structure framework (#24). Tests: 3 new cases in tests/underworld.lua (emberite tool gate, the lava sea, ash floors/pockets and ore share); the layer tests accept the underworld's ash. 109/109 pass, no ERROR lines. Closes #25 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
890 lines
30 KiB
Lua
890 lines
30 KiB
Lua
-- Strata 2D terrain generator.
|
|
--
|
|
-- Everything is a function of (x, y) only: the gameplay plane (z = 0) gets
|
|
-- terrain, caves, ores and trees; z = 1 gets background walls or the
|
|
-- invisible backdrop; a slab of "shadow" nodes in front of and behind
|
|
-- underground areas makes daylight fade with depth.
|
|
|
|
local PLANE_Z = strata.PLANE_Z
|
|
local WALL_Z = strata.WALL_Z
|
|
local SHADOW_FRONT = -(strata.CAMERA_DIST + 8) -- shadow slab z range in front
|
|
local SHADOW_BACK = 8 -- and behind the plane
|
|
|
|
-- Depth layers, top to bottom. Each layer owns everything from its `top`
|
|
-- down to the next layer's top. Other systems ask mg.layer_at(y).
|
|
-- stone, wall nodes the layer is made of and backed by
|
|
-- tunnel half-width of the winding tunnels (noise units)
|
|
-- cavern cavern noise above this is open; nil for no caverns
|
|
-- ores tried in order: {node, noise, min}, node replaces stone
|
|
-- sky background colour behind missing walls; nil = the real sky
|
|
-- spawns creature entity names by time of day
|
|
-- liquid, pool liquid that pools in this layer's caves and how common
|
|
-- ("pool" noise above it); nil for no pools
|
|
-- sea every cave cell at or below this row is full of the
|
|
-- layer's liquid (the underworld's lava sea); nil for none
|
|
-- floor replaces stone that has open cave above; nil for none
|
|
-- pocket, pocket_min soft-block pockets in the stone (as biomes have)
|
|
-- Depths are tuned so a straight shaft from the surface to the underworld
|
|
-- is about 3 minutes of digging with a copper pickaxe (2 with iron); the
|
|
-- selftest measures it.
|
|
local UNDERGROUND_TOP = -20
|
|
local CAVERN_TOP = -300
|
|
local UNDERWORLD_TOP = -900
|
|
local WORLD_BOTTOM = -1000 -- bedrock from here down
|
|
local BLEND = 6 -- rows over which one layer's stone dithers into the next
|
|
local CAVE_BLEND = 16 -- rows over which cave sizes ease into the next layer's
|
|
local CAVE_ROOF = 7 -- no caves this close under the surface
|
|
local COPPER_ROOF = 4 -- no ore this close under the surface
|
|
local NO_CAVERN = 2 -- a cavern threshold the noise never reaches
|
|
local CANOPY_REACH = 3 -- widest canopy, in columns either side of a trunk
|
|
local POOL_DEPTH = 6 -- deepest generated pool, in rows
|
|
local WATER = "strata_core:water"
|
|
local LAVA = "strata_core:lava"
|
|
local LAVA_SEA_TOP = WORLD_BOTTOM + 28 -- the underworld's lava sea fills caves up to here
|
|
|
|
local LAYERS = {
|
|
{
|
|
name = "surface", top = math.huge,
|
|
stone = "strata_core:stone", wall = "strata_core:wall_stone",
|
|
tunnel = 0.06, cavern = nil,
|
|
ores = {
|
|
{node = "strata_core:copper_ore", noise = "copper", min = 0.72},
|
|
},
|
|
sky = nil,
|
|
liquid = WATER, pool = 0.45,
|
|
spawns = {day = {"strata_mobs:slime"}, night = {}},
|
|
},
|
|
{
|
|
name = "underground", top = UNDERGROUND_TOP,
|
|
stone = "strata_core:stone", wall = "strata_core:wall_stone",
|
|
tunnel = 0.06, cavern = 0.8,
|
|
ores = {
|
|
{node = "strata_core:iron_ore", noise = "iron", min = 0.78},
|
|
{node = "strata_core:copper_ore", noise = "copper", min = 0.72},
|
|
},
|
|
sky = "#17130f",
|
|
liquid = WATER, pool = 0.3,
|
|
spawns = {day = {}, night = {}},
|
|
},
|
|
{
|
|
name = "cavern", top = CAVERN_TOP,
|
|
stone = "strata_core:slate", wall = "strata_core:wall_slate",
|
|
tunnel = 0.07, cavern = 0.5,
|
|
ores = {
|
|
{node = "strata_core:slate_iron_ore", noise = "iron", min = 0.74},
|
|
{node = "strata_core:slate_copper_ore", noise = "copper", min = 0.78},
|
|
},
|
|
sky = "#0c0f18",
|
|
liquid = WATER, pool = 0.3,
|
|
spawns = {day = {}, night = {}},
|
|
},
|
|
{
|
|
name = "underworld", top = UNDERWORLD_TOP,
|
|
stone = "strata_core:basalt", wall = "strata_core:wall_basalt",
|
|
tunnel = 0.08, cavern = 0.30,
|
|
ores = {
|
|
-- needs a pickaxe better than iron (group level 4, #25)
|
|
{node = "strata_core:emberite_ore", noise = "ember", min = 0.95},
|
|
},
|
|
sky = "#1e0906",
|
|
liquid = LAVA, pool = 0.1, sea = LAVA_SEA_TOP,
|
|
floor = "strata_core:ash",
|
|
pocket = "strata_core:ash", pocket_min = 0.55,
|
|
spawns = {day = {}, night = {}},
|
|
},
|
|
}
|
|
for i, layer in ipairs(LAYERS) do
|
|
layer.index = i
|
|
layer.bottom = LAYERS[i + 1] and LAYERS[i + 1].top + 1 or WORLD_BOTTOM
|
|
end
|
|
|
|
-- Biomes sit side by side along x and reach down through the surface and
|
|
-- underground layers; the cavern layer and below ignore them. A column's
|
|
-- biome comes from two 1D noises: "biome" (cold to hot) and "humidity".
|
|
-- name also the key in mg.biomes
|
|
-- surface, fill top node and the dirt-depth band under it
|
|
-- fill_wall background wall behind the fill band
|
|
-- stone, wall replace the layer's stone and wall (surface and
|
|
-- underground layers only); nil keeps the layer's
|
|
-- pocket, pocket_min soft-block pockets in the stone and how common
|
|
-- tree tree kind (see TREES) and per-column chance (0..100)
|
|
-- ores tried before the layer's ores
|
|
-- spawns surface creature table by time of day
|
|
local SNOW_MAX = -0.45 -- "biome" noise at or below: snow
|
|
local DESERT_MIN = 0.45 -- "biome" noise above: desert
|
|
local JUNGLE_MIN = 0.4 -- "humidity" noise above (and not snow/desert): jungle
|
|
local UNDERGROUND_LAYERS = 2 -- biomes replace stone and walls in layers 1..this
|
|
local SPAWN_FOREST = 64 -- columns either side of x = 0 that are always forest
|
|
local SPAWN_FADE = 192 -- then the other biomes fade in over this many columns
|
|
|
|
local TREES = {
|
|
-- trunk, leaves, trunk height range, canopy shape
|
|
oak = {trunk = "strata_core:tree", leaves = "strata_core:leaves", min = 5, max = 11, shape = "round"},
|
|
frostpine = {trunk = "strata_core:frostpine_tree", leaves = "strata_core:frostpine_needles",
|
|
min = 7, max = 12, shape = "cone"},
|
|
redheart = {trunk = "strata_core:redheart_tree", leaves = "strata_core:redheart_leaves",
|
|
min = 10, max = 16, shape = "canopy"},
|
|
-- giant cavern mushroom (#70), grown by the cavern biome table
|
|
umbrel = {trunk = "strata_core:umbrel_stalk", leaves = "strata_core:umbrel_cap",
|
|
min = 4, max = 8, shape = "cap"},
|
|
}
|
|
|
|
local BIOMES = {
|
|
{
|
|
name = "forest",
|
|
surface = "strata_core:grass", fill = "strata_core:dirt", fill_wall = "strata_core:wall_dirt",
|
|
stone = nil, wall = nil,
|
|
pocket = "strata_core:dirt", pocket_min = 0.65,
|
|
tree = {kind = "oak", chance = 14},
|
|
ores = {},
|
|
spawns = {day = {"strata_mobs:slime"}, night = {}},
|
|
},
|
|
{
|
|
name = "desert",
|
|
surface = "strata_core:sand", fill = "strata_core:sand", fill_wall = "strata_core:wall_sandstone",
|
|
stone = "strata_core:sandstone", wall = "strata_core:wall_sandstone",
|
|
pocket = "strata_core:sand", pocket_min = 0.7,
|
|
tree = nil,
|
|
ores = {},
|
|
spawns = {day = {"strata_mobs:slime"}, night = {}},
|
|
},
|
|
{
|
|
name = "snow",
|
|
surface = "strata_core:snow", fill = "strata_core:snow", fill_wall = "strata_core:wall_snow",
|
|
stone = "strata_core:ice", wall = "strata_core:wall_ice",
|
|
pocket = "strata_core:snow", pocket_min = 0.6,
|
|
tree = {kind = "frostpine", chance = 18},
|
|
ores = {},
|
|
spawns = {day = {"strata_mobs:slime"}, night = {}},
|
|
},
|
|
{
|
|
name = "jungle",
|
|
surface = "strata_core:jungle_grass", fill = "strata_core:mud", fill_wall = "strata_core:wall_mud",
|
|
stone = nil, wall = nil,
|
|
pocket = "strata_core:mud", pocket_min = 0.35,
|
|
tree = {kind = "redheart", chance = 22},
|
|
ores = {},
|
|
spawns = {day = {"strata_mobs:slime"}, night = {}},
|
|
},
|
|
}
|
|
|
|
-- Cavern biomes (#70) are patches in both x and y inside the cavern layer,
|
|
-- picked per cell by mg.cavern_biome_at(x, y); the first row is plain
|
|
-- slate cavern. Biome rows:
|
|
-- floor replaces the layer's stone in cells with open cave above
|
|
-- flora {node, chance (0..99)} in open cells on a solid floor
|
|
-- ceiling {node, chance, max}: chains up to max long hanging from
|
|
-- the cave roof
|
|
-- mushroom {kind (a TREES row), chance} on candidate floor columns
|
|
-- pool "pool" noise threshold for liquid; nil keeps the layer's
|
|
-- spawns creature table by time of day; nil keeps the layer's
|
|
local CAVERN_LAYER = 3 -- index of the layer that has cavern biomes
|
|
local GROVE_MIN = 0.35 -- "cavern_biome" noise above: glimmer grove
|
|
local GALLERY_MAX = -0.35 -- "cavern_biome" noise below: prism gallery
|
|
local GROTTO_MIN = 0.3 -- "grotto" noise above (and neither): drowned grotto
|
|
local MAX_HANG = 4 -- longest ceiling chain of any cavern biome
|
|
local MUSHROOM_BUCKET = 8 -- one candidate mushroom column per this many
|
|
local MUSHROOM_SLACK = 3 -- candidate offset in its bucket: 0..this
|
|
local MUSHROOM_HEADROOM = 2 -- open rows needed over a mushroom's stalk
|
|
|
|
local CAVERN_BIOMES = {
|
|
{name = "plain"},
|
|
{
|
|
name = "glimmer_grove",
|
|
floor = "strata_core:mycel_turf",
|
|
flora = {node = "strata_core:glimmercap", chance = 30},
|
|
mushroom = {kind = "umbrel", chance = 70},
|
|
spawns = {day = {}, night = {}},
|
|
},
|
|
{
|
|
name = "prism_gallery",
|
|
flora = {node = "strata_core:prism_cluster", chance = 14},
|
|
ceiling = {node = "strata_core:prism_cluster_hanging", chance = 18, max = 1},
|
|
spawns = {day = {}, night = {}},
|
|
},
|
|
{
|
|
name = "drowned_grotto",
|
|
floor = "strata_core:silt",
|
|
ceiling = {node = "strata_core:dripvine", chance = 40, max = MAX_HANG},
|
|
pool = -0.3,
|
|
spawns = {day = {}, night = {}},
|
|
},
|
|
}
|
|
|
|
-- World size (#23). A finite world is WORLD_SIZES[size] columns wide,
|
|
-- centred on x = 0, with an invisible, unbreakable edge column at
|
|
-- |x| = half and an ocean in the last OCEAN_WIDTH columns before it. The
|
|
-- size is fixed when the world is created ("strata_world_size") and kept
|
|
-- in mod storage; worlds made before #23 stay infinite.
|
|
local WORLD_SIZES = {small = 2000, medium = 3200, large = 4200, infinite = false}
|
|
local WORLD_SIZE_DEFAULT = "medium"
|
|
local WORLD_EDGE = "strata_core:world_edge"
|
|
local OCEAN_WIDTH = 220 -- columns of ocean biome inside each edge
|
|
local BEACH_SHARE = 0.15 -- first part of the ocean zone: land eases to a beach
|
|
local SHELF_END = 0.7 -- the sea floor stops falling here (share of the zone)
|
|
local SEA_LEVEL = 1 -- top row of sea water
|
|
local BEACH_Y = SEA_LEVEL + 3
|
|
local OCEAN_FLOOR = UNDERGROUND_TOP + 3 -- stays in the surface layer
|
|
|
|
local OCEAN = {
|
|
name = "ocean",
|
|
surface = "strata_core:sand", fill = "strata_core:sand", fill_wall = "strata_core:wall_sandstone",
|
|
stone = nil, wall = nil,
|
|
pocket = "strata_core:sand", pocket_min = 0.7,
|
|
tree = nil,
|
|
ores = {},
|
|
spawns = {day = {"strata_mobs:slime"}, night = {}},
|
|
}
|
|
|
|
local mg = {}
|
|
strata.mapgen = mg
|
|
mg.layers = LAYERS
|
|
mg.WORLD_BOTTOM = WORLD_BOTTOM
|
|
mg.trees = TREES
|
|
mg.SPAWN_FOREST = SPAWN_FOREST
|
|
mg.biome_list = BIOMES
|
|
mg.biomes = {}
|
|
for i, biome in ipairs(BIOMES) do
|
|
biome.index = i
|
|
mg.biomes[biome.name] = biome
|
|
end
|
|
OCEAN.index = #BIOMES + 1
|
|
mg.biomes.ocean = OCEAN
|
|
mg.ocean_biome = OCEAN -- not in biome_list: it only exists at the edges of a finite world
|
|
mg.SEA_LEVEL = SEA_LEVEL
|
|
mg.OCEAN_WIDTH = OCEAN_WIDTH
|
|
mg.BEACH_SHARE = BEACH_SHARE
|
|
mg.WORLD_SIZES = WORLD_SIZES
|
|
|
|
local world_half -- nil: infinite
|
|
|
|
--- Set the world size by name (see WORLD_SIZES). Only the generator's own
|
|
-- start-up and tests call this; changing it in a world that has map
|
|
-- already generated leaves the old map as it was.
|
|
function mg.set_world_size(name)
|
|
assert(WORLD_SIZES[name] ~= nil, "unknown world size " .. tostring(name))
|
|
mg.world_size = name
|
|
world_half = WORLD_SIZES[name] and WORLD_SIZES[name] / 2 or nil
|
|
mg.world_half = world_half
|
|
end
|
|
|
|
do
|
|
local storage = core.get_mod_storage()
|
|
local size = storage:get_string("world_size")
|
|
if size == "" or WORLD_SIZES[size] == nil then
|
|
-- env_meta.txt is written on the first save, so a world without it is new
|
|
local meta = io.open(core.get_worldpath() .. "/env_meta.txt")
|
|
if meta then
|
|
meta:close()
|
|
size = "infinite"
|
|
else
|
|
size = core.settings:get("strata_world_size") or WORLD_SIZE_DEFAULT
|
|
if WORLD_SIZES[size] == nil then
|
|
core.log("warning", "[strata_mapgen] unknown strata_world_size " .. size)
|
|
size = WORLD_SIZE_DEFAULT
|
|
end
|
|
end
|
|
storage:set_string("world_size", size)
|
|
end
|
|
mg.set_world_size(size)
|
|
end
|
|
|
|
--- Whether column x is inside the world (always, in an infinite one).
|
|
function mg.in_world(x)
|
|
return not world_half or math.abs(math.floor(x)) < world_half
|
|
end
|
|
|
|
--- How far column x is into the ocean zone at a world edge: 0 inland (or
|
|
-- in an infinite world) to 1 at the edge.
|
|
function mg.ocean_t(x)
|
|
if not world_half then
|
|
return 0
|
|
end
|
|
local d = math.abs(math.floor(x)) - (world_half - OCEAN_WIDTH)
|
|
return math.max(0, math.min(1, d / OCEAN_WIDTH))
|
|
end
|
|
local ocean_t = mg.ocean_t
|
|
|
|
mg.cavern_biome_list = CAVERN_BIOMES
|
|
mg.cavern_biomes = {}
|
|
for i, cb in ipairs(CAVERN_BIOMES) do
|
|
cb.index = i
|
|
mg.cavern_biomes[cb.name] = cb
|
|
end
|
|
mg.CAVERN_LAYER = CAVERN_LAYER
|
|
|
|
--- The layer that owns row y (a table from mg.layers). Rows above the
|
|
-- ground belong to the surface layer, rows under the world to the last.
|
|
function mg.layer_at(y)
|
|
y = math.floor(y)
|
|
for i = #LAYERS, 2, -1 do
|
|
if y <= LAYERS[i].top then
|
|
return LAYERS[i]
|
|
end
|
|
end
|
|
return LAYERS[1]
|
|
end
|
|
|
|
--- The layer whose stone and wall fill cell (x, y). Same as layer_at(y)
|
|
-- except in the top BLEND rows of a layer, where cells dither from the
|
|
-- layer above so the boundary isn't a ruled line.
|
|
function mg.material_layer(x, y)
|
|
local layer = mg.layer_at(y)
|
|
local into = layer.top - y
|
|
if layer.index > 1 and into < BLEND and (x * 7349 + y * 9151) % 7919 % (BLEND + 1) > into then
|
|
return LAYERS[layer.index - 1]
|
|
end
|
|
return layer
|
|
end
|
|
|
|
--- Tunnel half-width and cavern threshold at row y, eased over the top
|
|
-- CAVE_BLEND rows of each layer.
|
|
function mg.cave_params(y)
|
|
local layer = mg.layer_at(y)
|
|
local tunnel, cavern = layer.tunnel, layer.cavern or NO_CAVERN
|
|
local into = layer.top - y
|
|
if layer.index > 1 and into < CAVE_BLEND then
|
|
local above = LAYERS[layer.index - 1]
|
|
local f = into / CAVE_BLEND
|
|
tunnel = above.tunnel + (tunnel - above.tunnel) * f
|
|
cavern = (above.cavern or NO_CAVERN) + (cavern - (above.cavern or NO_CAVERN)) * f
|
|
end
|
|
return tunnel, cavern
|
|
end
|
|
|
|
core.set_mapgen_setting("mg_name", "singlenode", true)
|
|
|
|
local np = {
|
|
height = {offset = 0, scale = 1, spread = {x = 220, y = 220, z = 220},
|
|
seed = 5934, octaves = 4, persist = 0.5, lacunarity = 2.0},
|
|
dirt = {offset = 0, scale = 1, spread = {x = 64, y = 64, z = 64},
|
|
seed = 1203, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
biome = {offset = 0, scale = 1, spread = {x = 600, y = 600, z = 600},
|
|
seed = 777, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
humidity = {offset = 0, scale = 1, spread = {x = 450, y = 450, z = 450},
|
|
seed = 6161, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
tunnel = {offset = 0, scale = 1, spread = {x = 48, y = 32, z = 48},
|
|
seed = 4242, octaves = 3, persist = 0.5, lacunarity = 2.0},
|
|
cavern = {offset = 0, scale = 1, spread = {x = 90, y = 60, z = 90},
|
|
seed = 9090, octaves = 3, persist = 0.55, lacunarity = 2.0},
|
|
pocket = {offset = 0, scale = 1, spread = {x = 16, y = 16, z = 16},
|
|
seed = 31, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
copper = {offset = 0, scale = 1, spread = {x = 6, y = 6, z = 6},
|
|
seed = 2929, octaves = 2, persist = 0.6, lacunarity = 2.0},
|
|
iron = {offset = 0, scale = 1, spread = {x = 6, y = 6, z = 6},
|
|
seed = 3838, octaves = 2, persist = 0.6, lacunarity = 2.0},
|
|
ember = {offset = 0, scale = 1, spread = {x = 7, y = 5, z = 7},
|
|
seed = 6464, octaves = 2, persist = 0.6, lacunarity = 2.0},
|
|
pool = {offset = 0, scale = 1, spread = {x = 24, y = 12, z = 24},
|
|
seed = 5150, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
cavern_biome = {offset = 0, scale = 1, spread = {x = 160, y = 90, z = 160},
|
|
seed = 8128, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
grotto = {offset = 0, scale = 1, spread = {x = 110, y = 70, z = 110},
|
|
seed = 2718, octaves = 2, persist = 0.5, lacunarity = 2.0},
|
|
}
|
|
|
|
local noise = {}
|
|
local function N(name)
|
|
local n = noise[name]
|
|
if not n then
|
|
n = core.get_perlin(np[name])
|
|
noise[name] = n
|
|
end
|
|
return n
|
|
end
|
|
|
|
local function n1(name, x)
|
|
return N(name):get_2d({x = x, y = 0})
|
|
end
|
|
|
|
local function n2(name, x, y)
|
|
return N(name):get_2d({x = x, y = y})
|
|
end
|
|
|
|
local function smooth(t)
|
|
return t * t * (3 - 2 * t)
|
|
end
|
|
|
|
-- Surface height of column x (the y of the top solid block). In the ocean
|
|
-- zone of a finite world the land eases up to a beach, then the sea floor
|
|
-- falls to OCEAN_FLOOR.
|
|
function mg.surface_y(x)
|
|
local h = 4 + 14 * n1("height", x)
|
|
local t = ocean_t(x)
|
|
if t > 0 then
|
|
local fall = smooth(math.max(0, math.min(1, (t - BEACH_SHARE) / (SHELF_END - BEACH_SHARE))))
|
|
local sea = BEACH_Y + (OCEAN_FLOOR - BEACH_Y) * fall
|
|
h = h + (sea - h) * smooth(math.min(1, t / BEACH_SHARE))
|
|
end
|
|
return math.floor(h)
|
|
end
|
|
|
|
--- The biome (a table from mg.biome_list) of column x.
|
|
-- Near the world spawn both noises are scaled towards 0, so a new player
|
|
-- starts in a forest whatever the seed.
|
|
function mg.biome_at(x)
|
|
x = math.floor(x)
|
|
if ocean_t(x) > 0 then
|
|
return OCEAN
|
|
end
|
|
local f = math.min(1, math.max(0, (math.abs(x) - SPAWN_FOREST) / SPAWN_FADE))
|
|
local heat = n1("biome", x) * f
|
|
if heat > DESERT_MIN then
|
|
return BIOMES[2]
|
|
elseif heat <= SNOW_MAX then
|
|
return BIOMES[3]
|
|
elseif n1("humidity", x) * f > JUNGLE_MIN then
|
|
return BIOMES[4]
|
|
end
|
|
return BIOMES[1]
|
|
end
|
|
|
|
--- The cavern biome (a table from mg.cavern_biome_list) of cell (x, y).
|
|
-- Outside the cavern layer it is always the plain one.
|
|
function mg.cavern_biome_at(x, y)
|
|
x, y = math.floor(x), math.floor(y)
|
|
if mg.layer_at(y).index ~= CAVERN_LAYER then
|
|
return CAVERN_BIOMES[1]
|
|
end
|
|
local v = n2("cavern_biome", x, y)
|
|
if v > GROVE_MIN then
|
|
return CAVERN_BIOMES[2]
|
|
elseif v < GALLERY_MAX then
|
|
return CAVERN_BIOMES[3]
|
|
elseif n2("grotto", x, y) > GROTTO_MIN then
|
|
return CAVERN_BIOMES[4]
|
|
end
|
|
return CAVERN_BIOMES[1]
|
|
end
|
|
|
|
-- A deterministic roll 0..99 for cell (x, y); salt picks the purpose.
|
|
local function roll(x, y, salt)
|
|
return PcgRandom(x * 92821 + y * 68917 + salt * 7919):next(0, 99)
|
|
end
|
|
|
|
function mg.is_desert(x)
|
|
return mg.biome_at(x).name == "desert"
|
|
end
|
|
|
|
function mg.dirt_depth(x)
|
|
return 7 + math.floor(4 * n1("dirt", x))
|
|
end
|
|
|
|
local function is_cave(x, y, h)
|
|
if y > h - CAVE_ROOF then
|
|
return false
|
|
end
|
|
local tunnel, cavern = mg.cave_params(y)
|
|
if math.abs(n2("tunnel", x, y)) < tunnel then
|
|
return true
|
|
end
|
|
return cavern < NO_CAVERN and n2("cavern", x, y) > cavern
|
|
end
|
|
|
|
--- The liquid that cave cell (x, y) is generated full of, or nil. A pool
|
|
-- is a run of cave cells, at most POOL_DEPTH tall, that all have a high
|
|
-- "pool" noise and rest on a solid floor, so generated liquid mostly sits
|
|
-- still. Edges that spill sideways settle once the liquid simulation sees
|
|
-- them (strata_core/liquids.lua).
|
|
local function pool_liquid(x, y, h)
|
|
local layer = mg.layer_at(y)
|
|
if not layer.liquid then
|
|
return nil
|
|
end
|
|
-- a sea: everything below its top is liquid, so it's held in on every side
|
|
if layer.sea and y <= layer.sea then
|
|
return layer.liquid
|
|
end
|
|
for d = 0, POOL_DEPTH do
|
|
local py = y - d
|
|
if py <= WORLD_BOTTOM or not is_cave(x, py, h) then
|
|
return d > 0 and layer.liquid or nil
|
|
end
|
|
local cb = layer.index == CAVERN_LAYER and mg.cavern_biome_at(x, py)
|
|
local pool = cb and cb.pool or layer.pool
|
|
if n2("pool", x, py) < pool or mg.layer_at(py).liquid ~= layer.liquid then
|
|
return nil
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
mg.pool_liquid = function(x, y)
|
|
return pool_liquid(x, y, mg.surface_y(x))
|
|
end
|
|
|
|
--- Flora for the open, dry cave cell (x, y) in the cavern layer, or nil:
|
|
-- the biome's floor flora on a solid floor, else part of a chain hanging
|
|
-- from the roof (the biome of the roof cell decides).
|
|
local function cavern_flora(x, y, h)
|
|
local cb = mg.cavern_biome_at(x, y)
|
|
if cb.flora and not is_cave(x, y - 1, h) and roll(x, y, 1) < cb.flora.chance then
|
|
return cb.flora.node
|
|
end
|
|
for k = 1, MAX_HANG do
|
|
if not is_cave(x, y + k, h) then
|
|
local ceiling = mg.cavern_biome_at(x, y + k).ceiling
|
|
if ceiling and k <= ceiling.max and roll(x, y + k, 2) < ceiling.chance
|
|
and k <= 1 + roll(x, y + k, 5) % ceiling.max then
|
|
return ceiling.node
|
|
end
|
|
return nil
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- Giant mushrooms. Each MUSHROOM_BUCKET columns have one candidate column;
|
|
-- a mushroom roots in an open cell of it on a solid floor, in a biome that
|
|
-- grows them, with room for its stalk and cap. Returns the TREES row and
|
|
-- stalk height, or nil.
|
|
local function mushroom_column(x)
|
|
local b = math.floor(x / MUSHROOM_BUCKET)
|
|
return x == b * MUSHROOM_BUCKET + PcgRandom(b * 4241 + 7):next(0, MUSHROOM_SLACK)
|
|
end
|
|
|
|
local function mushroom_at(x, y, h)
|
|
if not mushroom_column(x) or not mg.in_world(x) then
|
|
return nil
|
|
end
|
|
local m = mg.cavern_biome_at(x, y).mushroom
|
|
if not m or roll(x, y, 3) >= m.chance then
|
|
return nil
|
|
end
|
|
if not is_cave(x, y, h) or is_cave(x, y - 1, h) or pool_liquid(x, y, h) then
|
|
return nil
|
|
end
|
|
local kind = TREES[m.kind]
|
|
local th = kind.min + roll(x, y, 4) % (kind.max - kind.min + 1)
|
|
for dy = 1, th + MUSHROOM_HEADROOM - 1 do
|
|
if not is_cave(x, y + dy, h) then
|
|
return nil
|
|
end
|
|
end
|
|
return kind, th
|
|
end
|
|
|
|
--- Mushrooms whose stalk or cap may reach into the box: a list of
|
|
-- {x, y (lowest stalk cell), kind, height}.
|
|
function mg.mushrooms(minx, maxx, miny, maxy)
|
|
local list = {}
|
|
local tallest = TREES.umbrel.max + MUSHROOM_HEADROOM
|
|
local cavern = LAYERS[CAVERN_LAYER]
|
|
miny = math.max(miny - tallest, cavern.bottom)
|
|
maxy = math.min(maxy + 1, cavern.top)
|
|
for x = minx - CANOPY_REACH, maxx + CANOPY_REACH do
|
|
if mushroom_column(x) then
|
|
local h = mg.surface_y(x)
|
|
for y = miny, maxy do
|
|
local kind, th = mushroom_at(x, y, h)
|
|
if kind then
|
|
list[#list + 1] = {x = x, y = y, kind = kind, height = th}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return list
|
|
end
|
|
|
|
-- Deterministic per-column tree layout. Returns the tree kind (a TREES
|
|
-- row) and trunk height, or nil.
|
|
local TREE_GAP = 2 -- no two trunks closer than this many columns apart
|
|
local function tree_roll(x)
|
|
local tree = mg.biome_at(x).tree
|
|
return tree and PcgRandom(x * 7919 + 104729):next(0, 99) < tree.chance and tree
|
|
end
|
|
|
|
local function tree_at(x)
|
|
local tree = tree_roll(x)
|
|
if not tree then
|
|
return nil
|
|
end
|
|
-- lower x wins ties
|
|
for dx = 1, TREE_GAP do
|
|
if tree_roll(x - dx) then
|
|
return nil
|
|
end
|
|
end
|
|
local kind = TREES[tree.kind]
|
|
return kind, kind.min + PcgRandom(x * 31 + 17):next(0, kind.max - kind.min)
|
|
end
|
|
mg.tree_at = tree_at
|
|
|
|
-- Leaf offsets {dx, dy} around a trunk of height th whose top is dy = 0.
|
|
local function canopy(shape, th)
|
|
local cells = {}
|
|
if shape == "round" then
|
|
for dx = -2, 2 do
|
|
for dy = -1, 3 do
|
|
if dx * dx + (dy - 1) * (dy - 1) <= 5 then
|
|
cells[#cells + 1] = {dx, dy}
|
|
end
|
|
end
|
|
end
|
|
elseif shape == "cone" then
|
|
-- a spire over tiers that widen downwards
|
|
cells[#cells + 1] = {0, 2}
|
|
cells[#cells + 1] = {0, 1}
|
|
for i = 0, math.min(th - 3, 7) do
|
|
local r = math.min(2, 1 + math.floor(i / 3))
|
|
if i % 3 == 2 then
|
|
r = r - 1
|
|
end
|
|
for dx = -r, r do
|
|
cells[#cells + 1] = {dx, -i}
|
|
end
|
|
end
|
|
elseif shape == "cap" then
|
|
-- a mushroom cap: wide brim with drooping edges, domed centre
|
|
for dx = -3, 3 do
|
|
cells[#cells + 1] = {dx, 0}
|
|
end
|
|
for dx = -2, 2 do
|
|
cells[#cells + 1] = {dx, 1}
|
|
end
|
|
cells[#cells + 1] = {-3, -1}
|
|
cells[#cells + 1] = {3, -1}
|
|
else -- "canopy": a wide flat crown
|
|
for dx = -3, 3 do
|
|
for dy = 0, 2 do
|
|
if dx * dx + 4 * (dy - 1) * (dy - 1) <= 10 then
|
|
cells[#cells + 1] = {dx, dy}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return cells
|
|
end
|
|
mg.canopy = canopy
|
|
|
|
-- Foreground node name for (x, y), ignoring trees and mushrooms.
|
|
local function foreground(x, y, h, depth, biome)
|
|
if world_half and math.abs(x) >= world_half then
|
|
return WORLD_EDGE
|
|
end
|
|
if y > h then
|
|
-- the sea, past the beach so it can't spill inland
|
|
if y <= SEA_LEVEL and ocean_t(x) >= BEACH_SHARE then
|
|
return WATER
|
|
end
|
|
return nil
|
|
end
|
|
if y <= WORLD_BOTTOM then
|
|
return "strata_core:bedrock"
|
|
end
|
|
if is_cave(x, y, h) then
|
|
local liquid = pool_liquid(x, y, h)
|
|
if liquid or mg.layer_at(y).index ~= CAVERN_LAYER then
|
|
return liquid
|
|
end
|
|
return cavern_flora(x, y, h)
|
|
end
|
|
local below = h - y
|
|
if below < depth then
|
|
return below == 0 and biome.surface or biome.fill
|
|
end
|
|
local layer = mg.material_layer(x, y)
|
|
local in_biome = layer.index <= UNDERGROUND_LAYERS
|
|
if y < h - COPPER_ROOF then
|
|
if in_biome then
|
|
for _, ore in ipairs(biome.ores) do
|
|
if n2(ore.noise, x, y) > ore.min then
|
|
return ore.node
|
|
end
|
|
end
|
|
end
|
|
for _, ore in ipairs(layer.ores) do
|
|
if n2(ore.noise, x, y) > ore.min then
|
|
return ore.node
|
|
end
|
|
end
|
|
end
|
|
if not in_biome then
|
|
local floor = layer.floor
|
|
if layer.index == CAVERN_LAYER and mg.layer_at(y).index == CAVERN_LAYER then
|
|
floor = mg.cavern_biome_at(x, y).floor
|
|
end
|
|
if floor and is_cave(x, y + 1, h) then
|
|
return floor
|
|
end
|
|
if layer.pocket and n2("pocket", x, y) > layer.pocket_min then
|
|
return layer.pocket
|
|
end
|
|
return layer.stone
|
|
end
|
|
if n2("pocket", x, y) > biome.pocket_min then
|
|
return biome.pocket
|
|
end
|
|
return biome.stone or layer.stone
|
|
end
|
|
mg.node_at = function(x, y)
|
|
return foreground(x, y, mg.surface_y(x), mg.dirt_depth(x), mg.biome_at(x)) or "air"
|
|
end
|
|
|
|
-- Wall-layer node name for (x, y).
|
|
local function background(x, y, h, depth, biome)
|
|
if y > h - 4 then
|
|
return "strata_core:backdrop"
|
|
end
|
|
if y <= WORLD_BOTTOM then
|
|
return "strata_core:bedrock"
|
|
end
|
|
if h - y < depth then
|
|
return biome.fill_wall
|
|
end
|
|
local layer = mg.material_layer(x, y)
|
|
if layer.index <= UNDERGROUND_LAYERS and biome.wall then
|
|
return biome.wall
|
|
end
|
|
return layer.wall
|
|
end
|
|
mg.wall_at = function(x, y)
|
|
return background(x, y, mg.surface_y(x), mg.dirt_depth(x), mg.biome_at(x))
|
|
end
|
|
|
|
--- The cells of a mushroom from mg.mushrooms: a list of {x, y, node}.
|
|
-- Cap cells only replace air when generated.
|
|
function mg.mushroom_cells(m)
|
|
local cells = {}
|
|
for dy = 0, m.height - 1 do
|
|
cells[#cells + 1] = {m.x, m.y + dy, m.kind.trunk}
|
|
end
|
|
local top = m.y + m.height - 1
|
|
for _, cell in ipairs(canopy(m.kind.shape, m.height)) do
|
|
cells[#cells + 1] = {m.x + cell[1], top + cell[2], m.kind.leaves}
|
|
end
|
|
return cells
|
|
end
|
|
|
|
local c = {}
|
|
local function cids()
|
|
if c.air then
|
|
return
|
|
end
|
|
for _, n in ipairs({"air", "strata_core:shadow", "strata_core:backdrop", WORLD_EDGE,
|
|
"strata_core:dirt", "strata_core:grass", "strata_core:stone",
|
|
"strata_core:sand", "strata_core:copper_ore", "strata_core:iron_ore",
|
|
"strata_core:tree", "strata_core:leaves", "strata_core:wall_dirt",
|
|
"strata_core:wall_stone", "strata_core:bedrock", WATER, LAVA}) do
|
|
c[n] = core.get_content_id(n)
|
|
end
|
|
for _, kind in pairs(TREES) do
|
|
c[kind.trunk] = core.get_content_id(kind.trunk)
|
|
c[kind.leaves] = core.get_content_id(kind.leaves)
|
|
end
|
|
for _, biome in ipairs({OCEAN, unpack(BIOMES)}) do
|
|
for _, key in ipairs({"surface", "fill", "fill_wall", "stone", "wall", "pocket"}) do
|
|
if biome[key] then
|
|
c[biome[key]] = core.get_content_id(biome[key])
|
|
end
|
|
end
|
|
for _, ore in ipairs(biome.ores) do
|
|
c[ore.node] = core.get_content_id(ore.node)
|
|
end
|
|
end
|
|
for _, cb in ipairs(CAVERN_BIOMES) do
|
|
for _, n in pairs({cb.floor, cb.flora and cb.flora.node, cb.ceiling and cb.ceiling.node}) do
|
|
c[n] = core.get_content_id(n) -- pairs: some of the three are nil
|
|
end
|
|
end
|
|
for _, layer in ipairs(LAYERS) do
|
|
for _, n in pairs({layer.floor, layer.pocket}) do
|
|
c[n] = core.get_content_id(n)
|
|
end
|
|
c[layer.stone] = core.get_content_id(layer.stone)
|
|
c[layer.wall] = core.get_content_id(layer.wall)
|
|
for _, ore in ipairs(layer.ores) do
|
|
c[ore.node] = core.get_content_id(ore.node)
|
|
end
|
|
end
|
|
end
|
|
|
|
local data = {}
|
|
local param2 = {}
|
|
|
|
core.register_on_generated(function(minp, maxp, blockseed)
|
|
if maxp.z < SHADOW_FRONT or minp.z > SHADOW_BACK then
|
|
return
|
|
end
|
|
cids()
|
|
local vm, emin, emax = core.get_mapgen_object("voxelmanip")
|
|
local area = VoxelArea(emin, emax)
|
|
vm:get_data(data)
|
|
vm:get_param2_data(param2)
|
|
|
|
local z0, z1 = math.max(minp.z, SHADOW_FRONT), math.min(maxp.z, SHADOW_BACK)
|
|
local has_plane = minp.z <= PLANE_Z and maxp.z >= PLANE_Z
|
|
local has_wall = minp.z <= WALL_Z and maxp.z >= WALL_Z
|
|
|
|
for x = minp.x, maxp.x do
|
|
local h = mg.surface_y(x)
|
|
local depth = mg.dirt_depth(x)
|
|
local biome = mg.biome_at(x)
|
|
for y = minp.y, maxp.y do
|
|
-- shadow slab
|
|
local underground = y < h - 1
|
|
for z = z0, z1 do
|
|
if z ~= PLANE_Z and z ~= WALL_Z then
|
|
data[area:index(x, y, z)] = underground and c["strata_core:shadow"] or c.air
|
|
end
|
|
end
|
|
if has_plane then
|
|
local name = foreground(x, y, h, depth, biome)
|
|
local i = area:index(x, y, PLANE_Z)
|
|
data[i] = name and c[name] or c.air
|
|
param2[i] = (name == WATER or name == LAVA) and strata.LIQUID_LEVEL_MAX or 0
|
|
end
|
|
if has_wall then
|
|
data[area:index(x, y, WALL_Z)] = c[background(x, y, h, depth, biome)]
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Trees, including ones rooted just outside this chunk whose leaves reach in.
|
|
if has_plane then
|
|
for x = minp.x - CANOPY_REACH, maxp.x + CANOPY_REACH do
|
|
local kind, th = tree_at(x)
|
|
if kind then
|
|
local h = mg.surface_y(x)
|
|
local function put(px, py, id, only_air)
|
|
if px >= minp.x and px <= maxp.x and py >= minp.y and py <= maxp.y then
|
|
local i = area:index(px, py, PLANE_Z)
|
|
if not only_air or data[i] == c.air then
|
|
data[i] = id
|
|
end
|
|
end
|
|
end
|
|
for ty = 1, th do
|
|
put(x, h + ty, c[kind.trunk])
|
|
end
|
|
local top = h + th
|
|
for _, cell in ipairs(canopy(kind.shape, th)) do
|
|
put(x + cell[1], top + cell[2], c[kind.leaves], true)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Giant mushrooms in the cavern layer: the stalk fills its open cells,
|
|
-- the cap only air.
|
|
if has_plane and maxp.y >= LAYERS[CAVERN_LAYER].bottom and minp.y <= LAYERS[CAVERN_LAYER].top then
|
|
for _, m in ipairs(mg.mushrooms(minp.x, maxp.x, minp.y, maxp.y)) do
|
|
for _, cell in ipairs(mg.mushroom_cells(m)) do
|
|
local px, py = cell[1], cell[2]
|
|
if px >= minp.x and px <= maxp.x and py >= minp.y and py <= maxp.y then
|
|
local i = area:index(px, py, PLANE_Z)
|
|
if cell[3] == m.kind.trunk or data[i] == c.air then
|
|
data[i] = c[cell[3]]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
vm:set_data(data)
|
|
vm:set_param2_data(param2)
|
|
vm:write_to_map(false)
|
|
-- The mapgen VoxelManip lighting pass lets sunlight straight through the
|
|
-- shadow slab; a regular light fix gives the intended fade with depth.
|
|
core.fix_light(minp, maxp)
|
|
end)
|