From fae8726a769f9ad3bb81bf3e364265662d333dd0 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Sat, 9 Jul 2022 22:32:33 +0200 Subject: Make `VoxelArea:position()` return a vector with metatable (#12521) --- builtin/game/voxelarea.lua | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'builtin/game/voxelarea.lua') diff --git a/builtin/game/voxelarea.lua b/builtin/game/voxelarea.lua index 64436bf1a..62f07d928 100644 --- a/builtin/game/voxelarea.lua +++ b/builtin/game/voxelarea.lua @@ -1,6 +1,9 @@ +local math_floor = math.floor +local vector_new = vector.new + VoxelArea = { - MinEdge = vector.new(1, 1, 1), - MaxEdge = vector.new(0, 0, 0), + MinEdge = vector_new(1, 1, 1), + MaxEdge = vector_new(0, 0, 0), ystride = 0, zstride = 0, } @@ -19,7 +22,7 @@ end function VoxelArea:getExtent() local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge - return vector.new( + return vector_new( MaxEdge.x - MinEdge.x + 1, MaxEdge.y - MinEdge.y + 1, MaxEdge.z - MinEdge.z + 1 @@ -36,7 +39,7 @@ function VoxelArea:index(x, y, z) local i = (z - MinEdge.z) * self.zstride + (y - MinEdge.y) * self.ystride + (x - MinEdge.x) + 1 - return math.floor(i) + return math_floor(i) end function VoxelArea:indexp(p) @@ -44,24 +47,23 @@ function VoxelArea:indexp(p) local i = (p.z - MinEdge.z) * self.zstride + (p.y - MinEdge.y) * self.ystride + (p.x - MinEdge.x) + 1 - return math.floor(i) + return math_floor(i) end function VoxelArea:position(i) - local p = {} local MinEdge = self.MinEdge i = i - 1 - p.z = math.floor(i / self.zstride) + MinEdge.z + local z = math_floor(i / self.zstride) + MinEdge.z i = i % self.zstride - p.y = math.floor(i / self.ystride) + MinEdge.y + local y = math_floor(i / self.ystride) + MinEdge.y i = i % self.ystride - p.x = math.floor(i) + MinEdge.x + local x = math_floor(i) + MinEdge.x - return p + return vector_new(x, y, z) end function VoxelArea:contains(x, y, z) -- cgit v1.2.3