mirror of
https://github.com/Uberi/Minetest-WorldEdit.git
synced 2025-01-01 05:40:35 +01:00
Add a hollowcuboid iteration helper (#190)
This commit is contained in:
parent
8c758671bc
commit
cda772b18d
@ -122,3 +122,72 @@ function mh.finish(manip, data)
|
|||||||
manip:update_map()
|
manip:update_map()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- returns an iterator that returns voxelarea indices for a hollow cuboid
|
||||||
|
function mh.iter_hollowcuboid(area, minx, miny, minz, maxx, maxy, maxz)
|
||||||
|
local i = area:index(minx, miny, minz) - 1
|
||||||
|
local xrange = maxx - minx + 1
|
||||||
|
local nextaction = i + 1 + xrange
|
||||||
|
local do_hole = false
|
||||||
|
|
||||||
|
local y = 0
|
||||||
|
local ydiff = maxy - miny
|
||||||
|
local ystride = area.ystride
|
||||||
|
local ymultistride = ydiff * ystride
|
||||||
|
|
||||||
|
local z = 0
|
||||||
|
local zdiff = maxz - minz
|
||||||
|
local zstride = area.zstride
|
||||||
|
local zcorner = true
|
||||||
|
|
||||||
|
return function()
|
||||||
|
-- continue i until it needs to jump ystride
|
||||||
|
i = i + 1
|
||||||
|
if i ~= nextaction then
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
|
||||||
|
-- add the x offset if y (and z) are not 0 or maxy (or maxz)
|
||||||
|
if do_hole then
|
||||||
|
do_hole = false
|
||||||
|
i = i + xrange - 2
|
||||||
|
nextaction = i + 1
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
|
||||||
|
-- continue y until maxy is exceeded
|
||||||
|
y = y+1
|
||||||
|
if y ~= ydiff + 1 then
|
||||||
|
i = i + ystride - xrange
|
||||||
|
if zcorner
|
||||||
|
or y == ydiff then
|
||||||
|
nextaction = i + xrange
|
||||||
|
else
|
||||||
|
nextaction = i + 1
|
||||||
|
do_hole = true
|
||||||
|
end
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
|
||||||
|
-- continue z until maxz is exceeded
|
||||||
|
z = z+1
|
||||||
|
if z == zdiff + 1 then
|
||||||
|
-- hollowcuboid finished, return nil
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- set i to index(minx, miny, minz + z) - 1
|
||||||
|
i = i + zstride - (ymultistride + xrange)
|
||||||
|
zcorner = z == zdiff
|
||||||
|
|
||||||
|
-- y is 0, so traverse the xs
|
||||||
|
y = 0
|
||||||
|
nextaction = i + xrange
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function mh.iterp_hollowcuboid(area, minp, maxp)
|
||||||
|
return mh.iter_hollowcuboid(area, minp.x, minp.y, minp.z,
|
||||||
|
maxp.x, maxp.y, maxp.z)
|
||||||
|
end
|
||||||
|
@ -14,31 +14,26 @@ local mh = worldedit.manip_helpers
|
|||||||
-- @return The number of nodes added.
|
-- @return The number of nodes added.
|
||||||
function worldedit.cube(pos, width, height, length, node_name, hollow)
|
function worldedit.cube(pos, width, height, length, node_name, hollow)
|
||||||
-- Set up voxel manipulator
|
-- Set up voxel manipulator
|
||||||
local basepos = vector.subtract(pos, {x=math.floor(width/2), y=0, z=math.floor(length/2)})
|
local basepos = vector.subtract(pos,
|
||||||
local manip, area = mh.init(basepos, vector.add(basepos, {x=width, y=height, z=length}))
|
{x = math.floor(width / 2), y = 0, z = math.floor(length / 2)})
|
||||||
|
local endpos = vector.add(basepos,
|
||||||
|
{x = width - 1, y = height - 1, z = length - 1})
|
||||||
|
local manip, area = mh.init(basepos, endpos)
|
||||||
local data = mh.get_empty_data(area)
|
local data = mh.get_empty_data(area)
|
||||||
|
|
||||||
-- Add cube
|
-- Add cube
|
||||||
local node_id = minetest.get_content_id(node_name)
|
local node_id = minetest.get_content_id(node_name)
|
||||||
local stride = vector.new(1, area.ystride, area.zstride)
|
|
||||||
local offset = vector.subtract(basepos, area.MinEdge)
|
|
||||||
local count = 0
|
local count = 0
|
||||||
|
local iterfunc
|
||||||
|
if hollow then
|
||||||
|
iterfunc = mh.iterp_hollowcuboid(area, basepos, endpos)
|
||||||
|
else
|
||||||
|
iterfunc = area:iterp(basepos, endpos)
|
||||||
|
end
|
||||||
|
|
||||||
for z = 0, length-1 do
|
for vi in iterfunc do
|
||||||
local index_z = (offset.z + z) * stride.z + 1 -- +1 for 1-based indexing
|
data[vi] = node_id
|
||||||
for y = 0, height-1 do
|
count = count + 1
|
||||||
local index_y = index_z + (offset.y + y) * stride.y
|
|
||||||
for x = 0, width-1 do
|
|
||||||
local is_wall = z == 0 or z == length-1
|
|
||||||
or y == 0 or y == height-1
|
|
||||||
or x == 0 or x == width-1
|
|
||||||
if not hollow or is_wall then
|
|
||||||
local i = index_y + (offset.x + x)
|
|
||||||
data[i] = node_id
|
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mh.finish(manip, data)
|
mh.finish(manip, data)
|
||||||
|
Loading…
Reference in New Issue
Block a user