summaryrefslogtreecommitdiff
path: root/pointset.lua
diff options
context:
space:
mode:
authorFaceDeer <derksenmobile@gmail.com>2016-12-30 23:38:18 -0700
committerFaceDeer <derksenmobile@gmail.com>2016-12-30 23:38:18 -0700
commit2695c6103383996fedfd12098cf3f28c5fc43fe2 (patch)
tree7db55aac50e27846ff3da78f9a59687a942c1721 /pointset.lua
parent25ca1c02615a8d458e16ff10b12fd6395767663c (diff)
Initial commit
Initial commit. Contains digger head, sand digger, builder, controller, pusher, structure node, light, inventory, and digtron core crafting item.
Diffstat (limited to 'pointset.lua')
-rw-r--r--pointset.lua73
1 files changed, 73 insertions, 0 deletions
diff --git a/pointset.lua b/pointset.lua
new file mode 100644
index 0000000..5bfd22a
--- /dev/null
+++ b/pointset.lua
@@ -0,0 +1,73 @@
+-- A simple special-purpose class, this is used for building up sets of three-dimensional points
+-- I only added features to it as I needed them so may not be highly useful outside of this mod's context.
+
+Pointset = {}
+Pointset.__index = Pointset
+
+function Pointset.create()
+ local set = {}
+ setmetatable(set,Pointset)
+ set.points = {}
+ return set
+end
+
+function Pointset:set(x, y, z, value)
+ -- sets a value in the 3D array "points".
+ if self.points[x] == nil then
+ self.points[x] = {}
+ end
+ if self.points[x][y] == nil then
+ self.points[x][y] = {}
+ end
+ self.points[x][y][z] = value
+end
+
+function Pointset:set_if_not_in(excluded, x, y, z, value)
+ -- If a value is not already set for this point in the 3D array "excluded", set it in "points"
+ if excluded:get(x, y, z) ~= nil then
+ return
+ end
+ self:set(x, y, z, value)
+end
+
+function Pointset:get(x, y, z)
+ -- return a value from the 3D array "points"
+ if self.points[x] == nil or self.points[x][y] == nil then
+ return nil
+ end
+ return self.points[x][y][z]
+end
+
+function Pointset:pop()
+ -- returns a point that's in the 3D array, and then removes it.
+ local pos = {}
+ local ytable
+ local ztable
+ local val
+
+ local count = 0
+ for _ in pairs(self.points) do count = count + 1 end
+ if count == 0 then
+ return nil
+ end
+
+ pos.x, ytable = next(self.points)
+ pos.y, ztable = next(ytable)
+ pos.z, val = next(ztable)
+
+ self.points[pos.x][pos.y][pos.z] = nil
+
+ count = 0
+ for _ in pairs(self.points[pos.x][pos.y]) do count = count + 1 end
+ if count == 0 then
+ self.points[pos.x][pos.y] = nil
+ end
+
+ count = 0
+ for _ in pairs(self.points[pos.x]) do count = count + 1 end
+ if count == 0 then
+ self.points[pos.x] = nil
+ end
+
+ return pos, val
+end \ No newline at end of file