From 079df8575e7b2c83d294745485e061d86f9780b0 Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Thu, 29 Oct 2026 23:13:32 -0600 Subject: [PATCH] wip --- common/check.lua | 21 ++++++ common/dump.lua | 4 +- common/region.lua | 129 ++++++++++++++++++++++++------------ common/table.lua | 2 +- mods/jvmi_biomes/mapenv.lua | 6 +- mods/jvmi_mapgen/mapenv.lua | 16 ++--- mods/jvmi_nodes/earthy.lua | 8 +++ 7 files changed, 127 insertions(+), 59 deletions(-) create mode 100644 common/check.lua diff --git a/common/check.lua b/common/check.lua new file mode 100644 index 0000000..588c56a --- /dev/null +++ b/common/check.lua @@ -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 +} diff --git a/common/dump.lua b/common/dump.lua index 3781839..4151734 100644 --- a/common/dump.lua +++ b/common/dump.lua @@ -59,6 +59,4 @@ end -- Export ----------------------------------------------------------------------------------------- -return { - dump = private.dump -} +return private.dump diff --git a/common/region.lua b/common/region.lua index 10cc52b..1a78315 100644 --- a/common/region.lua +++ b/common/region.lua @@ -1,7 +1,10 @@ -- Import ------------------------------------------------------------------------------------------ -local math = _G["math"] -local VoxelArea = _G["VoxelArea"] +local core = _G.core +local math = _G.math +local VoxelArea = _G.VoxelArea + +local check = dofile(core.get_game_info().path.."/common/check.lua") -- Region Class ------------------------------------------------------------------------------------ @@ -31,10 +34,10 @@ local Region = { ]]-- function Region:new() local result = { + fill_min = nil, + fill_max = nil, _emerged_area = nil, _emerged_data = {}, - _fill_min = nil, - _fill_max = nil, _meta = {}, _voxels = nil, } @@ -54,6 +57,10 @@ end an int giving the content_id of the node at that location ]]-- 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)] end @@ -67,16 +74,21 @@ end content_id:int - the new content_id for the given location ]]-- 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 end --[[ 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: - 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 using the various methods on this class. @@ -88,20 +100,28 @@ end 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 + 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) - 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)) +function Region:iterate(x_min, x_max, y_min, y_max, z_min, z_max, do_operation) + check.is_type("x_min", x_min, "number") + check.is_type("x_max", x_max, "number") + check.is_type("y_min", y_min, "number") + check.is_type("y_max", y_max, "number") + check.is_type("z_min", z_min, "number") + 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 y = y_min, y_max do for x = x_min, x_max do - doOperation(x, y, z) + do_operation(x, y, z) end end end @@ -115,14 +135,17 @@ end 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 + 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._fill_min.x, self._fill_max.x, - self._fill_min.y, y_max, - self._fill_min.z, self._fill_max.z, - doOperation + self.fill_min.x, self.fill_max.x, + self.fill_min.y, y_max, + self.fill_min.z, self.fill_max.z, + do_operation ) end @@ -133,14 +156,16 @@ end 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 + 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._fill_min.x, self._fill_max.x, + 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 + self.fill_min.z, self.fill_max.z, + function(x, y, z) do_operation(x, z) end ) end @@ -151,14 +176,16 @@ end full area being filled by this region. 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._fill_min.x, self._fill_max.x, - self._fill_min.y, self._fill_max.y, - self._fill_min.z, self._fill_max.z, - doOperation + self.fill_min.x, self.fill_max.x, + self.fill_min.y, self.fill_max.y, + self.fill_min.z, self.fill_max.z, + do_operation ) end @@ -170,17 +197,20 @@ end Args: 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) - if y < self._fill_min.y then return end - if y > self._fill_max.y then return end +function Region:iterate_plane(y, do_operation) + check.is_type("y", y, "number") + 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._fill_min.x, self._fill_max.x, + self.fill_min.x, self.fill_max.x, y, y, - self._fill_min.z, self._fill_max.z, - doOperation + self.fill_min.z, self.fill_max.z, + do_operation ) end @@ -202,6 +232,10 @@ end the value previously stored for that location or `nil` if there isn't any ]]-- 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] if not meta_data then return nil end return meta_data[self._emerged_area:index(x, 0, z)] @@ -221,10 +255,15 @@ end value:any - the value to be stored ]]-- 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] if not meta_data then meta_data = {} self._meta[name] = meta_data + core.debug("adding meta["..name.."]") end 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 ]]-- 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._voxels = voxels - self._fill_min = fill_min - self._fill_max = fill_max + 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) @@ -263,9 +306,9 @@ function Region:save_voxels() self._voxels:calc_lighting() self._voxels:update_liquids() + self.fill_max = nil + self.fill_min = nil self._emerged_area = nil - self._fill_max = nil - self._fill_min = nil self._voxels = nil end diff --git a/common/table.lua b/common/table.lua index cd7ce3c..5d07769 100644 --- a/common/table.lua +++ b/common/table.lua @@ -18,7 +18,7 @@ local private = {} the target table ]]-- function private.append(target, value) - target[#target] = value + target[#target + 1] = value return target end diff --git a/mods/jvmi_biomes/mapenv.lua b/mods/jvmi_biomes/mapenv.lua index 8028ecf..45b2be3 100644 --- a/mods/jvmi_biomes/mapenv.lua +++ b/mods/jvmi_biomes/mapenv.lua @@ -33,7 +33,7 @@ end region:Region - the region currently being generated ]]-- 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 = C.TERRAIN_TYPES[terrain_type] 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 ]]-- 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_name = core.get_biome_name(biome_id) 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 filler_count = biome.depth_filler or 0 diff --git a/mods/jvmi_mapgen/mapenv.lua b/mods/jvmi_mapgen/mapenv.lua index 26a397c..4dd5fd0 100644 --- a/mods/jvmi_mapgen/mapenv.lua +++ b/mods/jvmi_mapgen/mapenv.lua @@ -9,6 +9,8 @@ local Region = dofile(path.."/common/region.lua") local table = dofile(path.."/common/table.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 --------------------------------------------------------------------------------------- @@ -97,12 +99,12 @@ function private.assign_terrain_meta(region) local band_end = raw_index + C.TERRAIN_BLEND / 2 local max_weight = 0 + local blend = {} for terrain_type, terrain in ipairs(TERRAIN_TYPES) do local overlap_start = math.max(band_start, terrain.range_start) local overlap_end = math.min(band_end, terrain.range_end) local overlap = overlap_end - overlap_start - local blend = {} if overlap > 0 then local weight = overlap / C.TERRAIN_BLEND 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) end end - - region:set_meta(Region.META.TERRAIN_BLEND, x, z, blend) end + + region:set_meta(Region.META.TERRAIN_BLEND, x, z, blend) end) end @@ -154,12 +156,8 @@ end function private.fill_terrain(region) region:iterate_fill(function(x, y, z) local altitude = region:get_meta(Region.META.ALTITUDE, x, z) - if type(altitude) ~= "number" then - error( - "expected altitude at ("..x..","..z..") to be a number, ".. - "but was a "..type(altitude) - ) - end + check.is_type("altitude", altitude, "number") + if y > altitude then return end region:set_content_id(x, y, z, STONE_ID) end) diff --git a/mods/jvmi_nodes/earthy.lua b/mods/jvmi_nodes/earthy.lua index 6deb9e8..3bfef7e 100644 --- a/mods/jvmi_nodes/earthy.lua +++ b/mods/jvmi_nodes/earthy.lua @@ -26,6 +26,14 @@ core.register_node("jvmi_nodes:dirt", { 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", { description = S("Sand"), drop = "jvmi_nodes:sand",