diff options
author | kwolekr <kwolekr@minetest.net> | 2013-06-28 10:28:15 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2013-06-28 11:07:45 -0400 |
commit | 3f13dc790caeda7ca8f5e429f6f94458bda5826c (patch) | |
tree | 23912b30d723790d481a69751914ac0f2c8b1f48 /builtin | |
parent | 280946ba836cde9516f9344f47561f3356bdf869 (diff) | |
download | minetest-3f13dc790caeda7ca8f5e429f6f94458bda5826c.tar.gz minetest-3f13dc790caeda7ca8f5e429f6f94458bda5826c.tar.bz2 minetest-3f13dc790caeda7ca8f5e429f6f94458bda5826c.zip |
Add voxelarea.lua helper to builtin
Diffstat (limited to 'builtin')
-rw-r--r-- | builtin/builtin.lua | 2 | ||||
-rw-r--r-- | builtin/voxelarea.lua | 46 |
2 files changed, 47 insertions, 1 deletions
diff --git a/builtin/builtin.lua b/builtin/builtin.lua index f10c6c7cf..d573cba90 100644 --- a/builtin/builtin.lua +++ b/builtin/builtin.lua @@ -25,4 +25,4 @@ dofile(minetest.get_modpath("__builtin").."/static_spawn.lua") dofile(minetest.get_modpath("__builtin").."/detached_inventory.lua") dofile(minetest.get_modpath("__builtin").."/falling.lua") dofile(minetest.get_modpath("__builtin").."/features.lua") - +dofile(minetest.get_modpath("__builtin").."/voxelarea.lua") diff --git a/builtin/voxelarea.lua b/builtin/voxelarea.lua new file mode 100644 index 000000000..9426c3acf --- /dev/null +++ b/builtin/voxelarea.lua @@ -0,0 +1,46 @@ +VoxelArea = { + MinEdge = {x=1, y=1, z=1}, + MaxEdge = {x=0, y=0, z=0}, + ystride = 0, + zstride = 0, +} + +function VoxelArea:new(o) + o = o or {} + setmetatable(o, self) + self.__index = self + + local e = o:getExtent() + o.ystride = e.x + o.zstride = e.x * e.y + + return o +end + +function VoxelArea:getExtent() + return { + x = self.MaxEdge.x - self.MinEdge.x + 1, + y = self.MaxEdge.y - self.MinEdge.y + 1, + z = self.MaxEdge.z - self.MinEdge.z + 1, + } +end + +function VoxelArea:getVolume() + local e = self:getExtent() + return e.x * e.y * e.z +end + +function VoxelArea:index(x, y, z) + local i = (z - self.MinEdge.z) * self.zstride + + (y - self.MinEdge.y) * self.ystride + + (x - self.MinEdge.x) + 1 + return math.floor(i) +end + +function VoxelArea:indexp(p) + local i = (p.z - self.MinEdge.z) * self.zstride + + (p.y - self.MinEdge.y) * self.ystride + + (p.x - self.MinEdge.x) + 1 + return math.floor(i) +end + |