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
+86 -43
View File
@@ -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