This commit is contained in:
Andrew Miner
2026-10-29 23:13:32 -06:00
parent 267349a830
commit 079df8575e
7 changed files with 127 additions and 59 deletions
+21
View File
@@ -0,0 +1,21 @@
-- Private -----------------------------------------------------------------------------------------
local private = {}
function private.is_key(name, value, table_name, table_value)
if table_value[value] ~= nil then return end
error(name.." should be a key in "..table_name..", but "..value.." isn't present")
end
function private.is_type(name, value, type_name)
if type(value) == type_name then return end
error(name.." should be a "..type_name..", but was a "..type(value))
end
-- Export ------------------------------------------------------------------------------------------
return {
is_key = private.is_key,
is_type = private.is_type
}
+1 -3
View File
@@ -59,6 +59,4 @@ end
-- Export ----------------------------------------------------------------------------------------- -- Export -----------------------------------------------------------------------------------------
return { return private.dump
dump = private.dump
}
+86 -43
View File
@@ -1,7 +1,10 @@
-- Import ------------------------------------------------------------------------------------------ -- Import ------------------------------------------------------------------------------------------
local math = _G["math"] local core = _G.core
local VoxelArea = _G["VoxelArea"] local math = _G.math
local VoxelArea = _G.VoxelArea
local check = dofile(core.get_game_info().path.."/common/check.lua")
-- Region Class ------------------------------------------------------------------------------------ -- Region Class ------------------------------------------------------------------------------------
@@ -31,10 +34,10 @@ local Region = {
]]-- ]]--
function Region:new() function Region:new()
local result = { local result = {
fill_min = nil,
fill_max = nil,
_emerged_area = nil, _emerged_area = nil,
_emerged_data = {}, _emerged_data = {},
_fill_min = nil,
_fill_max = nil,
_meta = {}, _meta = {},
_voxels = nil, _voxels = nil,
} }
@@ -54,6 +57,10 @@ end
an int giving the content_id of the node at that location an int giving the content_id of the node at that location
]]-- ]]--
function Region:get_content_id(x, y, z) function Region:get_content_id(x, y, z)
check.is_type("x", x, "number")
check.is_type("y", y, "number")
check.is_type("z", z, "number")
return self._emerged_data[self._emerged_area:index(x, y, z)] return self._emerged_data[self._emerged_area:index(x, y, z)]
end end
@@ -67,16 +74,21 @@ end
content_id:int - the new content_id for the given location content_id:int - the new content_id for the given location
]]-- ]]--
function Region:set_content_id(x, y, z, content_id) function Region:set_content_id(x, y, z, content_id)
check.is_type("x", x, "number")
check.is_type("y", y, "number")
check.is_type("z", z, "number")
check.is_type("content_id", content_id, "number")
self._emerged_data[self._emerged_area:index(x, y, z)] = content_id self._emerged_data[self._emerged_area:index(x, y, z)] = content_id
end end
--[[ --[[
Iterate over every voxel within a specified area. Iterate over every voxel within a specified area.
For each voxel, inside the described cuboid, call the `doOperation` function which should have For each voxel, inside the described cuboid, call the `do_operation` function which should have
the following signature: the following signature:
function doOperation(x:int, y:int, z:int) -> None function do_operation(x:int, y:int, z:int) -> None
The called function can then inspect and manipulate the content ids and meta data in the region The called function can then inspect and manipulate the content ids and meta data in the region
using the various methods on this class. using the various methods on this class.
@@ -88,20 +100,28 @@ end
y_max:int - the highest 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_min:int - the lowest z coordinate in the desired cuboid
z_max:int - the highest 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 do_operation: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) function Region:iterate(x_min, x_max, y_min, y_max, z_min, z_max, do_operation)
x_min = math.floor(math.max(x_min, self._fill_min.x)) check.is_type("x_min", x_min, "number")
x_max = math.floor(math.min(x_max, self._fill_max.x)) check.is_type("x_max", x_max, "number")
y_min = math.floor(math.max(y_min, self._fill_min.y)) check.is_type("y_min", y_min, "number")
y_max = math.floor(math.min(y_max, self._fill_max.y)) check.is_type("y_max", y_max, "number")
z_min = math.floor(math.max(z_min, self._fill_min.z)) check.is_type("z_min", z_min, "number")
z_max = math.floor(math.min(z_max, self._fill_max.z)) check.is_type("z_max", z_max, "number")
check.is_type("do_operation", do_operation, "function")
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 z = z_min, z_max do
for y = y_min, y_max do for y = y_min, y_max do
for x = x_min, x_max do for x = x_min, x_max do
doOperation(x, y, z) do_operation(x, y, z)
end end
end end
end end
@@ -115,14 +135,17 @@ end
Args: Args:
y_max:int - the maximum y value to include 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 do_operation:function(x:int,y:int,z:int) - the callback to be invoked for each voxel
]]-- ]]--
function Region:iterate_below(y_max, doOperation) function Region:iterate_below(y_max, do_operation)
check.is_type("y_max", y_max, "number")
check.is_type("do_operation", do_operation, "function")
self:iterate( self:iterate(
self._fill_min.x, self._fill_max.x, self.fill_min.x, self.fill_max.x,
self._fill_min.y, y_max, self.fill_min.y, y_max,
self._fill_min.z, self._fill_max.z, self.fill_min.z, self.fill_max.z,
doOperation do_operation
) )
end end
@@ -133,14 +156,16 @@ end
area being filled by this region, and the y-axis coordinate is omitted. area being filled by this region, and the y-axis coordinate is omitted.
Args: Args:
doOperation:function(x:int,z:int) - the callback to be invoked for each voxel do_operation:function(x:int,z:int) - the callback to be invoked for each voxel
]]-- ]]--
function Region:iterate_columns(doOperation) function Region:iterate_columns(do_operation)
check.is_type("do_operation", do_operation, "function")
self:iterate( self:iterate(
self._fill_min.x, self._fill_max.x, self.fill_min.x, self.fill_max.x,
0, 0, 0, 0,
self._fill_min.z, self._fill_max.z, self.fill_min.z, self.fill_max.z,
function(x, y, z) doOperation(x, z) end function(x, y, z) do_operation(x, z) end
) )
end end
@@ -151,14 +176,16 @@ end
full area being filled by this region. full area being filled by this region.
Args: Args:
doOperation:function(nt,int,int) - the callback to be invoked for each voxel do_operation:function(nt,int,int) - the callback to be invoked for each voxel
]]-- ]]--
function Region:iterate_fill(doOperation) function Region:iterate_fill(do_operation)
check.is_type("do_operation", do_operation, "function")
self:iterate( self:iterate(
self._fill_min.x, self._fill_max.x, self.fill_min.x, self.fill_max.x,
self._fill_min.y, self._fill_max.y, self.fill_min.y, self.fill_max.y,
self._fill_min.z, self._fill_max.z, self.fill_min.z, self.fill_max.z,
doOperation do_operation
) )
end end
@@ -170,17 +197,20 @@ end
Args: Args:
y:int - the y value to use y:int - the y value to use
doOperation:function(int,int,int) - the callback to be invoked for each voxel do_operation:function(int,int,int) - the callback to be invoked for each voxel
]]-- ]]--
function Region:iterate_plane(y, doOperation) function Region:iterate_plane(y, do_operation)
if y < self._fill_min.y then return end check.is_type("y", y, "number")
if y > self._fill_max.y then return end check.is_type("do_operation", do_operation, "function")
if y < self.fill_min.y then return end
if y > self.fill_max.y then return end
self:iterate( self:iterate(
self._fill_min.x, self._fill_max.x, self.fill_min.x, self.fill_max.x,
y, y, y, y,
self._fill_min.z, self._fill_max.z, self.fill_min.z, self.fill_max.z,
doOperation do_operation
) )
end end
@@ -202,6 +232,10 @@ end
the value previously stored for that location or `nil` if there isn't any the value previously stored for that location or `nil` if there isn't any
]]-- ]]--
function Region:get_meta(name, x, z) function Region:get_meta(name, x, z)
check.is_type("name", name, "string")
check.is_type("x", x, "number")
check.is_type("z", z, "number")
local meta_data = self._meta[name] local meta_data = self._meta[name]
if not meta_data then return nil end if not meta_data then return nil end
return meta_data[self._emerged_area:index(x, 0, z)] return meta_data[self._emerged_area:index(x, 0, z)]
@@ -221,10 +255,15 @@ end
value:any - the value to be stored value:any - the value to be stored
]]-- ]]--
function Region:set_meta(name, x, z, value) function Region:set_meta(name, x, z, value)
check.is_type("name", name, "string")
check.is_type("x", x, "number")
check.is_type("z", z, "number")
local meta_data = self._meta[name] local meta_data = self._meta[name]
if not meta_data then if not meta_data then
meta_data = {} meta_data = {}
self._meta[name] = meta_data self._meta[name] = meta_data
core.debug("adding meta["..name.."]")
end end
meta_data[self._emerged_area:index(x, 0, z)] = value meta_data[self._emerged_area:index(x, 0, z)] = value
@@ -239,11 +278,15 @@ end
fill_max:vector - the larger 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) function Region:read_voxels(voxels, fill_min, fill_max)
check.is_type("voxels", voxels, "userdata")
check.is_type("fill_min", fill_min, "table")
check.is_type("fill_max", fill_max, "table")
self:save_voxels() self:save_voxels()
self._voxels = voxels self._voxels = voxels
self._fill_min = fill_min self.fill_min = fill_min
self._fill_max = fill_max self.fill_max = fill_max
local min_emerged, max_emerged = voxels:get_emerged_area() local min_emerged, max_emerged = voxels:get_emerged_area()
self._emerged_area = VoxelArea(min_emerged, max_emerged) self._emerged_area = VoxelArea(min_emerged, max_emerged)
@@ -263,9 +306,9 @@ function Region:save_voxels()
self._voxels:calc_lighting() self._voxels:calc_lighting()
self._voxels:update_liquids() self._voxels:update_liquids()
self.fill_max = nil
self.fill_min = nil
self._emerged_area = nil self._emerged_area = nil
self._fill_max = nil
self._fill_min = nil
self._voxels = nil self._voxels = nil
end end
+1 -1
View File
@@ -18,7 +18,7 @@ local private = {}
the target table the target table
]]-- ]]--
function private.append(target, value) function private.append(target, value)
target[#target] = value target[#target + 1] = value
return target return target
end end
+3 -3
View File
@@ -33,7 +33,7 @@ end
region:Region - the region currently being generated region:Region - the region currently being generated
]]-- ]]--
function private.assign_biomes(region) function private.assign_biomes(region)
region.iterate_columns(function(x, z) region:iterate_columns(function(x, z)
local terrain_type = region:get_meta(Region.META.TERRAIN_TYPE, x, z) local terrain_type = region:get_meta(Region.META.TERRAIN_TYPE, x, z)
local terrain = C.TERRAIN_TYPES[terrain_type] local terrain = C.TERRAIN_TYPES[terrain_type]
region:set_meta(Region.META.BIOME, x, z, core.get_biome_id(terrain.name)) region:set_meta(Region.META.BIOME, x, z, core.get_biome_id(terrain.name))
@@ -47,12 +47,12 @@ end
region:Region - the region currently being generated region:Region - the region currently being generated
]]-- ]]--
function private.fill_surface(region) function private.fill_surface(region)
region.iterate_columns(function(x, z) region:iterate_columns(function(x, z)
local biome_id = region:get_meta(Region.META.BIOME, x, z) local biome_id = region:get_meta(Region.META.BIOME, x, z)
local biome_name = core.get_biome_name(biome_id) local biome_name = core.get_biome_name(biome_id)
local biome = core.registered_biomes[biome_name] local biome = core.registered_biomes[biome_name]
local y = region:get_meta(Region.META.ALTITUDE) local y = region:get_meta(Region.META.ALTITUDE, x, z)
local top_count = biome.depth_top or 0 local top_count = biome.depth_top or 0
local filler_count = biome.depth_filler or 0 local filler_count = biome.depth_filler or 0
+7 -9
View File
@@ -9,6 +9,8 @@ local Region = dofile(path.."/common/region.lua")
local table = dofile(path.."/common/table.lua") local table = dofile(path.."/common/table.lua")
local biomes = dofile(core.get_modpath("jvmi_biomes").."/mapenv.lua") local biomes = dofile(core.get_modpath("jvmi_biomes").."/mapenv.lua")
local check = dofile(core.get_game_info().path.."/common/check.lua")
local dump = dofile(core.get_game_info().path.."/common/dump.lua")
-- Constants --------------------------------------------------------------------------------------- -- Constants ---------------------------------------------------------------------------------------
@@ -97,12 +99,12 @@ function private.assign_terrain_meta(region)
local band_end = raw_index + C.TERRAIN_BLEND / 2 local band_end = raw_index + C.TERRAIN_BLEND / 2
local max_weight = 0 local max_weight = 0
local blend = {}
for terrain_type, terrain in ipairs(TERRAIN_TYPES) do for terrain_type, terrain in ipairs(TERRAIN_TYPES) do
local overlap_start = math.max(band_start, terrain.range_start) local overlap_start = math.max(band_start, terrain.range_start)
local overlap_end = math.min(band_end, terrain.range_end) local overlap_end = math.min(band_end, terrain.range_end)
local overlap = overlap_end - overlap_start local overlap = overlap_end - overlap_start
local blend = {}
if overlap > 0 then if overlap > 0 then
local weight = overlap / C.TERRAIN_BLEND local weight = overlap / C.TERRAIN_BLEND
table.append(blend, { weight = weight, terrain_type = terrain_type }) table.append(blend, { weight = weight, terrain_type = terrain_type })
@@ -112,9 +114,9 @@ function private.assign_terrain_meta(region)
region:set_meta(Region.META.TERRAIN_TYPE, x, z, terrain_type) region:set_meta(Region.META.TERRAIN_TYPE, x, z, terrain_type)
end end
end end
region:set_meta(Region.META.TERRAIN_BLEND, x, z, blend)
end end
region:set_meta(Region.META.TERRAIN_BLEND, x, z, blend)
end) end)
end end
@@ -154,12 +156,8 @@ end
function private.fill_terrain(region) function private.fill_terrain(region)
region:iterate_fill(function(x, y, z) region:iterate_fill(function(x, y, z)
local altitude = region:get_meta(Region.META.ALTITUDE, x, z) local altitude = region:get_meta(Region.META.ALTITUDE, x, z)
if type(altitude) ~= "number" then check.is_type("altitude", altitude, "number")
error(
"expected altitude at ("..x..","..z..") to be a number, "..
"but was a "..type(altitude)
)
end
if y > altitude then return end if y > altitude then return end
region:set_content_id(x, y, z, STONE_ID) region:set_content_id(x, y, z, STONE_ID)
end) end)
+8
View File
@@ -26,6 +26,14 @@ core.register_node("jvmi_nodes:dirt", {
tiles = {"jvmi_nodes_dirt.png"}, tiles = {"jvmi_nodes_dirt.png"},
}) })
core.register_node("jvmi_nodes:gravel", {
description = S("Gravel"),
drop = "jvmi_nodes:gravel",
is_ground_content = true,
sounds = { footstep = "jvmi_nodes_gravel_footstep" },
tiles = {"jvmi_nodes_gravel.png"},
})
core.register_node("jvmi_nodes:sand", { core.register_node("jvmi_nodes:sand", {
description = S("Sand"), description = S("Sand"),
drop = "jvmi_nodes:sand", drop = "jvmi_nodes:sand",