This commit is contained in:
Andrew Miner
2026-10-26 11:07:59 -06:00
parent bfd7f78a69
commit a822ebbdfd
25 changed files with 178 additions and 181 deletions
+116
View File
@@ -0,0 +1,116 @@
-- Private -----------------------------------------------------------------------------------------
local C = {}
-- Node Definition Constants -----------------------------------------------------------------------
C.ANIMATION_TYPE = {
VERTICAL_FRAMES = "vertical_frames",
SHEET_2D = "sheet_2d",
}
C.DRAW_TYPE = {
ALL_FACES_OPTIONAL = "allfaces_optional",
FLOWING_LIQUID = "flowingliquid",
LIQUID = "liquid",
MESH = "mesh",
NODE_BOX = "nodebox",
NORMAL = "normal",
PLANT_LIKE = "plantlike",
}
C.LIQUID_TYPE = {
NONE = "none",
SOURCE = "source",
FLOWING = "flowing",
}
C.TEXTURE_ALPHA = {
OPAQUE = "opaque",
CLIP = "clip",
BLEND = "blend",
}
C.VISCOSITY = {
XXX_FAST = 0,
XX_FAST = 1,
X_FAST = 2,
FAST = 3,
SLOW = 4,
X_SLOW = 5,
XX_SLOW = 6,
XXX_SLOW = 7,
}
C.WAVING = {
NO = 0,
PLANT_LIKE = 1,
LEAF_LIKE = 2,
LIQUID_LIKE = 3,
}
-- Mapgen Constants --------------------------------------------------------------------------------
--[[ Key altitudes which define the vertical regions of the generated map. ]]--
C.ALTITUDE = {
SKY = 256,
MOUNTAIN = 128,
MOOR = 64,
HILL = 32,
PLAIN = 16,
SURFACE = 0,
SHALLOW = -16,
DEEP = -32,
ABYSS = -64,
BEDROCK = -128,
}
local A = C.ALTITUDE
--[[ The amount of overlap (as a perentage of total weight) between adjacent terrain types. ]]--
C.TERRAIN_BLEND = 0.15
--[[ The distance from the center of the map at which only the lowest terrain types are used. ]]--
C.TERRAIN_RADIUS = 3096
--[[ The noise generator that decides which terrain type is active at a given x,z location. ]]--
C.TERRAIN_TYPE_NOISE_PARAMS = {
flags = "eased",
lacunarity = 2,
octaves = 1,
offset = 0,
persistence = 0.5,
scale = 1.0,
seed = 8381909047,
spread = { x = 512, y = 512, z = 0 },
}
--[[
A table defining the terrain types used in generating the base terrain.
These must be listed in average altitude order from lowest to highest. The weights are merely
relative values, and can be whatever you like. I find it easiest to think of them as
percentages and choose value which tally to 100.
Bear in mind that noise values are adjusted downward are you approach `TERRAIN_RADIUS` meters
from the center of the map, so these weights are only going to be fully reflected in the terrain
generated at the center of the map, and will progressively favor lower altitudes as you move
toward the edges.
]]--
C.TERRAIN_TYPES = {
{ name = "ocean_floor", lowest = A.BEDROCK + 10, highest = A.BEDROCK + 26, weight = 5 },
{ name = "ocean_abyss", lowest = A.ABYSS, highest = A.DEEP, weight = 15 },
{ name = "ocean_shelf", lowest = A.DEEP, highest = A.SHALLOW, weight = 10 },
{ name = "ocean_surface", lowest = A.SURFACE - 4, highest = A.SURFACE, weight = 5 },
{ name = "land_plains", lowest = A.PLAIN - 8, highest = A.PLAIN, weight = 20 },
{ name = "land_hills", lowest = A.HILL - 32, highest = A.HILL, weight = 25 },
{ name = "land_moors", lowest = A.MOOR - 16, highest = A.MOOR, weight = 15 },
{ name = "land_mountains", lowest = A.MOUNTAIN - 80, highest = A.MOUNTAIN, weight = 5 },
}
-- Export ------------------------------------------------------------------------------------------
return C
+34
View File
@@ -0,0 +1,34 @@
-- Private -----------------------------------------------------------------------------------------
local private = {}
function private.dump(value, depth, indent, seen)
depth = depth or -1
indent = indent or ""
seen = seen or {}
if depth == 0 then return "<too deep: "..tostring(value)..">" end
if seen[value] == true then return "<already seen: "..tostring(value)..">" end
seen[value] = true
local result = ""
if type(value) == "table" then
result = result.."{\n"
local inner_indent = indent.." "
for inner_key, inner_value in pairs(value) do
local inner_text = private.dump(inner_value, depth - 1, inner_indent, seen)
result = result..inner_indent..inner_key.." = "..inner_text.."\n"
end
result = result..indent.."}"
else
result = tostring(value)
end
return result
end
-- Export -----------------------------------------------------------------------------------------
return private.dump
+275
View File
@@ -0,0 +1,275 @@
-- Import ------------------------------------------------------------------------------------------
local math = _G["math"]
local VoxelArea = _G["VoxelArea"]
-- Region Class ------------------------------------------------------------------------------------
local Region = {
--[[ Defines the names of the known metadata tables. ]]--
META = {
--[[ The highest solid block in a certain x,z location. ]]--
ALTITUDE = "altitude",
--[[ The id of the biome at each x,z location. ]]--
BIOME = "biome",
--[[ A list of {weight:int, terrain_type:int} tuples for each x,z location. ]]--
TERRAIN_BLEND = "terrain_blend",
--[[ The heaviest weighted terrain type from the `TERRAIN_BLEND` list. ]]--
TERRAIN_TYPE = "terrain_type",
}
}
--[[
Create a new Region object to manage the generation of a part of the map.
Returns:
an empty Region ready to be filled
]]--
function Region:new()
local result = {
_emerged_area = nil,
_emerged_data = {},
_fill_min = nil,
_fill_max = nil,
_meta = {},
_voxels = nil,
}
setmetatable(result, { __index = Region })
return result
end
--[[
Return the content_id of the node at the indicated coordinate.
Args:
x:int - the x-axis position
y:int - the y-axis position
z:int - the z-axis position
Returns:
an int giving the content_id of the node at that location
]]--
function Region:get_content_id(x, y, z)
return self._emerged_data[self._emerged_area:index(x, y, z)]
end
--[[
Alter the content_id of the node at the indicated coordinate.
Args:
x:int - the x-axis position
y:int - the y-axis position
z:int - the z-axis position
content_id:int - the new content_id for the given location
]]--
function Region:set_content_id(x, y, z, content_id)
self._emerged_data[self._emerged_area:index(x, y, z)] = content_id
end
--[[
Iterate over every voxel within a specified area.
For each voxel, inside the described cuboid, call the `doOperation` function which should have
the following signature:
function doOperation(x:int, y:int, z:int) -> None
The called function can then inspect and manipulate the content ids and meta data in the region
using the various methods on this class.
Args:
x_min:int - the lowest x coordinate in the desired cuboid
x_max:int - the highest x coordinate in the desired cuboid
y_min:int - the lowest y coordinate in the desired cuboid
y_max:int - the highest y coordinate in the desired cuboid
z_min:int - the lowest z coordinate in the desired cuboid
z_max:int - the highest z coordinate in the desired cuboid
doOperation:function(x:int,y:int,z:int) - the callback to be invoked for each voxel
]]--
function Region:iterate(x_min, x_max, y_min, y_max, z_min, z_max, doOperation)
x_min = math.floor(math.max(x_min, self._fill_min.x))
x_max = math.floor(math.min(x_max, self._fill_max.x))
y_min = math.floor(math.max(y_min, self._fill_min.y))
y_max = math.floor(math.min(y_max, self._fill_max.y))
z_min = math.floor(math.max(z_min, self._fill_min.z))
z_max = math.floor(math.min(z_max, self._fill_max.z))
for z = z_min, z_max do
for y = y_min, y_max do
for x = x_min, x_max do
doOperation(x, y, z)
end
end
end
end
--[[
Iterates over all voxels at or below a certain altitude.
Works identically to the `Region:iterate` function, except that the x & z ranges are the full
area being filled by this region, and the y range is capped at the given altitude.
Args:
y_max:int - the maximum y value to include
doOperation:function(x:int,y:int,z:int) - the callback to be invoked for each voxel
]]--
function Region:iterate_below(y_max, doOperation)
self:iterate(
self._fill_min.x, self._fill_max.x,
self._fill_min.y, y_max,
self._fill_min.z, self._fill_max.z,
doOperation
)
end
--[[
Iterates over all the x,z columns in the fill region.
Works identically to the `Region:iterate` function, except that the x & z ranges are the full
area being filled by this region, and the y-axis coordinate is omitted.
Args:
doOperation:function(x:int,z:int) - the callback to be invoked for each voxel
]]--
function Region:iterate_columns(doOperation)
self:iterate(
self._fill_min.x, self._fill_max.x,
0, 0,
self._fill_min.z, self._fill_max.z,
function(x, y, z) doOperation(x, z) end
)
end
--[[
Iterates over all voxels being filled in this region.
Works identically to the `Region:iterate` function, except that the x, y, and z ranges are the
full area being filled by this region.
Args:
doOperation:function(nt,int,int) - the callback to be invoked for each voxel
]]--
function Region:iterate_fill(doOperation)
self:iterate(
self._fill_min.x, self._fill_max.x,
self._fill_min.y, self._fill_max.y,
self._fill_min.z, self._fill_max.z,
doOperation
)
end
--[[
Iterates over all voxels at a specific altitude.
Works identically to the `Region:iterate` function, except that the x & z ranges are the full
area being filled by this region, and y is limited to exactly the given altitude.
Args:
y:int - the y value to use
doOperation:function(int,int,int) - the callback to be invoked for each voxel
]]--
function Region:iterate_plane(y, doOperation)
if y < self._fill_min.y then return end
if y > self._fill_max.y then return end
self:iterate(
self._fill_min.x, self._fill_max.x,
y, y,
self._fill_min.z, self._fill_max.z,
doOperation
)
end
--[[
Get the named metadata at the indicated coordinate.
The canonical use for this method is to find the previously-calculated altitude of the base
terrain at a certain location (e.g., `get_meta(Region.META.ALTITUDE, 292, 998)`). However, any
meta name can be used, and any data stored. This data is let until overwritten, so each mapgen
cycle should be careful to recompute any metadata it requires before running algorithms which
depend upon it.
Args:
name:string - the name of the metadata table to access
x:int - the x coordinate of the desired data
z:int - the z coordinate of the desired data
Returns:
the value previously stored for that location or `nil` if there isn't any
]]--
function Region:get_meta(name, x, z)
local meta_data = self._meta[name]
if not meta_data then return nil end
return meta_data[self._emerged_area:index(x, 0, z)]
end
--[[
Change the metadata at the indicated coordinate.
This will automatically fill in any necessary pieces to ensure that the operation succeeds
(e.g., creating a missing metadata table), so it is highly advised to use the provided
`Region.META` constants to avoid accidentally creating new tables through misspellings.
Args:
name:string - the name of the metadata table to access
x:int - the x coordinate of the desired data
z:int - the z coordinate of the desired data
value:any - the value to be stored
]]--
function Region:set_meta(name, x, z, value)
local meta_data = self._meta[name]
if not meta_data then
meta_data = {}
self._meta[name] = meta_data
end
meta_data[self._emerged_area:index(x, 0, z)] = value
end
--[[
Reads raw voxel data from a voxel manipulator.
Args:
voxels:VoxelManip - the voxel manipulator containing the data
fill_min:vector - the smaller corner of the cuboid to be filled
fill_max:vector - the larger corner of the cuboid to be filled
]]--
function Region:read_voxels(voxels, fill_min, fill_max)
self:save_voxels()
self._voxels = voxels
self._fill_min = fill_min
self._fill_max = fill_max
local min_emerged, max_emerged = voxels:get_emerged_area()
self._emerged_area = VoxelArea(min_emerged, max_emerged)
self._voxels:get_data(self._emerged_data)
end
--[[
Writes raw voxel data back into the source voxel manipulator.
If no voxel data has been read (or none has been read since the last save), then this function
returns immediately.
]]--
function Region:save_voxels()
if not self._voxels then return end
self._voxels:set_data(self._emerged_data)
self._voxels:calc_lighting()
self._voxels:update_liquids()
self._emerged_area = nil
self._fill_max = nil
self._fill_min = nil
self._voxels = nil
end
-- Export ------------------------------------------------------------------------------------------
return Region
+96
View File
@@ -0,0 +1,96 @@
-- Import ------------------------------------------------------------------------------------------
local table = _G.table
-- Private -----------------------------------------------------------------------------------------
local private = {}
--[[
Add a value to the end of an array-like table.
Args:
target:array - a table which is being used like an array (i.e., contiguous integer keys)
value:any - the value to be appeneded to the array
Returns:
the target table
]]--
function private.append(target, value)
target[#target] = value
return target
end
--[[
Performs a deep clone of a given table.
This function only clones regular keys, and makes no effort to preserve the metatables of the
resulting tables. It also does not attempt to handle cycles among the given tables.
Args:
source:table - the table to be cloned
Returns:
a new table which the same keys and deeply copied values
]]--
function private.clone(source)
if type(source) ~= "table" then return source end
local target = {}
for key, value in pairs(source) do
target[key] = private.clone(value)
end
return target
end
--[[
Performs a shallow clone of a given table.
This is similar to `clone`, but it does not attempt to deeply copy the values of the given
table, instead creating a new table with the same keys and values as the original.
Args:
source:table - the table to be copied
Returns:
a new table with the same keys and values as the original
]]--
function private.copy(source)
return private.merge({}, source)
end
--[[
Overwrite the contents of a target table with the values in one or more source tables.
Only those keys defined by one of the source tables will be overridden in the target table.
Each successive source table in the given sequence may override values in the earlier source
tables. This function only performs a shallow copy, so any objects in the source tables are only copied
by reference. To create a brand new table with the merged contents of the following tables,
provide nil as the first argument.
Args:
target:table - a table whose values are to be overridden
...:table - a sequence of one or more source tables whose keys are to be copied
Returns:
the target table
]]--
function private.merge(target, ...)
target = target or nil
for _, source in ipairs({...}) do
for key, value in pairs(source) do
target[key] = value
end
end
return target
end
-- Export ------------------------------------------------------------------------------------------
return private.merge({}, table, private)