Refactor minetest.schematic

Fixes a `size` off-by-one error, breaking backwards compatibility.
Also changes `self` to `params` in `schematic.create` to modify the passed table.
Furthermore gets rid of the dirty do-undo for writing.
This commit is contained in:
Lars Mueller 2023-06-01 13:21:29 +02:00
parent 322da6cf0a
commit 98e452bb6a

@ -10,12 +10,13 @@ function schematic.setmetatable(self)
return setmetatable(self, metatable) return setmetatable(self, metatable)
end end
function schematic.create(self, pos_min, pos_max) function schematic.create(params, pos_min, pos_max)
self.size = vector.subtract(pos_max, pos_min) pos_min, pos_max = vector.sort(pos_min, pos_max)
local size = vector.add(vector.subtract(pos_max, pos_min), 1)
local voxelmanip = minetest.get_voxel_manip(pos_min, pos_max) local voxelmanip = minetest.get_voxel_manip(pos_min, pos_max)
local emin, emax = voxelmanip:read_from_map(pos_min, pos_max) local emin, emax = voxelmanip:read_from_map(pos_min, pos_max)
local voxelarea = VoxelArea:new{ MinEdge = emin, MaxEdge = emax } local voxelarea = VoxelArea:new{ MinEdge = emin, MaxEdge = emax }
local nodes, light_values, param2s = {}, self.light_values and {}, {} local nodes, light_values, param2s = {}, params.light_values and {}, {}
local vm_nodes, vm_light_values, vm_param2s = voxelmanip:get_data(), light_values and voxelmanip:get_light_data(), voxelmanip:get_param2_data() local vm_nodes, vm_light_values, vm_param2s = voxelmanip:get_data(), light_values and voxelmanip:get_light_data(), voxelmanip:get_param2_data()
local node_names, node_ids = {}, {} local node_names, node_ids = {}, {}
local i = 0 local i = 0
@ -32,33 +33,35 @@ function schematic.create(self, pos_min, pos_max)
end end
i = i + 1 i = i + 1
nodes[i] = id nodes[i] = id
if self.light_values then if params.light_values then
light_values[i] = vm_light_values[index] light_values[i] = vm_light_values[index]
end end
param2s[i] = vm_param2s[index] param2s[i] = vm_param2s[index]
end end
local metas = self.metas local metas
if metas or metas == nil then if params.metas or params.metas == nil then
local indexing = vector.add(self.size, 1)
metas = {} metas = {}
for _, pos in ipairs(minetest.find_nodes_with_meta(pos_min, pos_max)) do for _, pos in ipairs(minetest.find_nodes_with_meta(pos_min, pos_max)) do
local meta = minetest.get_meta(pos):to_table() local meta = minetest.get_meta(pos):to_table()
if next(meta.fields) ~= nil or next(meta.inventory) ~= nil then if next(meta.fields) ~= nil or next(meta.inventory) ~= nil then
local relative = vector.subtract(pos, pos_min) local relative = vector.subtract(pos, pos_min)
metas[((relative.z * indexing.y) + relative.y) * indexing.x + relative.x] = meta metas[((relative.z * size.y) + relative.y) * size.x + relative.x] = meta
end end
end end
end end
self.node_names = node_names return schematic.setmetatable({
self.nodes = nodes size = size,
self.light_values = light_values node_names = node_names,
self.param2s = param2s nodes = nodes,
self.metas = metas light_values = light_values,
return schematic.setmetatable(self) param2s = param2s,
metas = metas,
})
end end
function schematic:write_to_voxelmanip(voxelmanip, pos_min) function schematic:write_to_voxelmanip(voxelmanip, pos_min)
local pos_max = vector.add(pos_min, self.size) local size = self.size
local pos_max = vector.subtract(vector.add(pos_min, size), 1) -- `pos_max` is inclusive
local emin, emax = voxelmanip:read_from_map(pos_min, pos_max) local emin, emax = voxelmanip:read_from_map(pos_min, pos_max)
local voxelarea = VoxelArea:new{ MinEdge = emin, MaxEdge = emax } local voxelarea = VoxelArea:new{ MinEdge = emin, MaxEdge = emax }
local nodes, light_values, param2s, metas = self.nodes, self.light_values, self.param2s, self.metas local nodes, light_values, param2s, metas = self.nodes, self.light_values, self.param2s, self.metas
@ -86,13 +89,12 @@ function schematic:write_to_voxelmanip(voxelmanip, pos_min)
end end
voxelmanip:set_param2_data(vm_param2s) voxelmanip:set_param2_data(vm_param2s)
if metas then if metas then
local indexing = vector.add(self.size, 1)
for index, meta in pairs(metas) do for index, meta in pairs(metas) do
local floored = math.floor(index / indexing.x) local floored = math.floor(index / size.x)
local relative = { local relative = {
x = index % indexing.x, x = index % size.x,
y = floored % indexing.y, y = floored % size.y,
z = math.floor(floored / indexing.y) z = math.floor(floored / size.y)
} }
minetest.get_meta(vector.add(relative, pos_min)):from_table(meta) minetest.get_meta(vector.add(relative, pos_min)):from_table(meta)
end end
@ -100,7 +102,7 @@ function schematic:write_to_voxelmanip(voxelmanip, pos_min)
end end
function schematic:place(pos_min) function schematic:place(pos_min)
local pos_max = vector.add(pos_min, self.size) local pos_max = vector.subtract(vector.add(pos_min, self.size), 1) -- `pos_max` is inclusive
local voxelmanip = minetest.get_voxel_manip(pos_min, pos_max) local voxelmanip = minetest.get_voxel_manip(pos_min, pos_max)
self:write_to_voxelmanip(voxelmanip, pos_min) self:write_to_voxelmanip(voxelmanip, pos_min)
voxelmanip:write_to_map(not self.light_values) voxelmanip:write_to_map(not self.light_values)
@ -113,18 +115,22 @@ local function table_to_byte_string(tab)
end end
local function write_bluon(self, stream) local function write_bluon(self, stream)
local metas, light_values, param2s = self.metas, self.light_values, self.param2s local metas = modlib.table.copy(self.metas)
self.metas = modlib.table.copy(metas) for _, meta in pairs(metas) do
for _, meta in pairs(self.metas) do
for _, list in pairs(meta.inventory) do for _, list in pairs(meta.inventory) do
for index, stack in pairs(list) do for index, stack in pairs(list) do
list[index] = stack:to_string() list[index] = stack:to_string()
end end
end end
end end
self.light_values, self.param2s = table_to_byte_string(light_values), table_to_byte_string(param2s) modlib.bluon:write({
modlib.bluon:write(self, stream) size = self.size,
self.metas, self.light_values, self.param2s = metas, light_values, param2s node_names = self.node_names,
nodes = self.nodes,
light_values = table_to_byte_string(self.light_values),
param2s = table_to_byte_string(self.param2s),
metas = metas,
}, stream)
end end
function schematic:write_bluon(path) function schematic:write_bluon(path)