From 6027c8d25920fea23a306dba02162174721250e0 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Tue, 9 Jul 2013 16:26:52 +0200 Subject: [PATCH] Add VoxelArea:position, VoxelArea:iter and VoxelArea:iterp --- builtin/voxelarea.lua | 41 +++++++++++++++++++++++++++++++++++++++++ doc/lua_api.txt | 7 +++++++ 2 files changed, 48 insertions(+) diff --git a/builtin/voxelarea.lua b/builtin/voxelarea.lua index dd9af7910..93bbf73a8 100644 --- a/builtin/voxelarea.lua +++ b/builtin/voxelarea.lua @@ -44,6 +44,22 @@ function VoxelArea:indexp(p) return math.floor(i) end +function VoxelArea:position(i) + local p = {} + + i = i - 1 + + p.z = math.floor(i / self.zstride) + self.MinEdge.z + i = i % self.zstride + + p.y = math.floor(i / self.ystride) + self.MinEdge.y + i = i % self.ystride + + p.x = math.floor(i) + self.MinEdge.x + + return p +end + function VoxelArea:contains(x, y, z) return (x >= self.MinEdge.x) and (x <= self.MaxEdge.x) and (y >= self.MinEdge.y) and (y <= self.MaxEdge.y) and @@ -60,3 +76,28 @@ function VoxelArea:containsi(i) return (i >= 1) and (i <= self:getVolume()) end +function VoxelArea:iter(minx, miny, minz, maxx, maxy, maxz) + local i = self:index(minx, miny, minz) - 1 + local last = self:index(maxx, maxy, maxz) + local ystride = self.ystride + local zstride = self.zstride + local yoff = (last+1) % ystride + local zoff = (last+1) % zstride + local ystridediff = (i - last) % ystride + local zstridediff = (i - last) % zstride + return function() + i = i + 1 + if i % zstride == zoff then + i = i + zstridediff + elseif i % ystride == yoff then + i = i + ystridediff + end + if i <= last then + return i + end + end +end + +function VoxelArea:iterp(minp, maxp) + return self:iter(minp.x, minp.y, minp.z, maxp.x, maxp.y, maxp.z) +end diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 12892e5b5..74b3d3ba5 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1676,6 +1676,13 @@ methods: - index(x, y, z): returns the index of an absolute position in a flat array starting at 1 ^ useful for things like VoxelManip, raw Schematic specifiers, PerlinNoiseMap:get2d/3dMap, and so on - indexp(p): same as above, except takes a vector +- position(i): returns the absolute position vector corresponding to index i +- contains(x, y, z): check if (x,y,z) is inside area formed by MinEdge and MaxEdge +- containsp(p): same as above, except takes a vector +- containsi(i): same as above, except takes an index +- iter(minx, miny, minz, maxx, maxy, maxz): returns an iterator that returns indices + ^ from (minx,miny,minz) to (maxx,maxy,maxz) in the order of [z [y [x]]] +- iterp(minp, maxp): same as above, except takes a vector Mapgen objects --------------- -- 2.25.1