Advertisement
apencer1w

Computercraft position class

Jan 4th, 2020 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. function dirToNum(d)
  2.     assert(type(d) == "string", "Expected string, got " .. type(d))
  3.     d = d:lower()
  4.    
  5.     if d == "north" then
  6.         return 0
  7.     elseif d == "east" then
  8.         return 1
  9.     elseif d == "south" then
  10.         return 2
  11.     elseif d == "west" then
  12.         return 3
  13.     else
  14.         error('"' .. d .. '"' .. " is an unrecognized direction")
  15.     end
  16. end
  17.  
  18. function numToDir(n)
  19.     assert(type(n) == "number", "Expected number, got " .. type(n))
  20.     n = n % 4
  21.    
  22.     if n == 0 then
  23.         return "north"
  24.     elseif n == 1 then
  25.         return "east"
  26.     elseif n == 2 then
  27.         return "south"
  28.     elseif n == 3 then
  29.         return "west"
  30.     end
  31. end
  32.  
  33. function getUpVector()
  34.     return new(0, 1, 0)
  35. end
  36.  
  37. function getDownVector()
  38.     return new(0, -1, 0)
  39. end
  40.  
  41. function getNorthVector()
  42.     return new(0, 0, -1, 0)
  43. end
  44.  
  45. function getSouthVector()
  46.     return new(0, 0, 1, 2)
  47. end
  48.  
  49. function getWestVector()
  50.     return new(-1, 0, 0, 3)
  51. end
  52.  
  53. function getEastVector()
  54.     return new(1, 0, 0, 1)
  55. end
  56.  
  57. local pos = {}
  58. pos.__index = pos
  59.  
  60. function new(x, y, z, dir)
  61.     if type(dir) == "string" then
  62.         dir = dirToNum(dir)
  63.     end
  64.     local t = {
  65.         ["x"] = x or 0,
  66.         ["y"] = y or 0,
  67.         ["z"] = z or 0,
  68.         ["dir"] = dir or 0
  69.     }
  70.     t.dir = t.dir % 4
  71.    
  72.     setmetatable(t, pos)
  73.     return t
  74. end
  75.  
  76. function fromGPS(loadDir)
  77.     if not loadDir then loadDir = false end
  78.     local coords = {gps.locate()}
  79.    
  80.     assert(coords ~= nil, "Cannot load pos from GPS. There is no modem. ")
  81.    
  82.     local t = new(coords[1], coords[2], coords[3])
  83.     if loadDir then
  84.         if not turtle.forward() then
  85.             repeat until not turtle.dig()
  86.             if not turtle.forward() then error("must be able to go forward to load dir!") end
  87.         end
  88.        
  89.         local newCoords = {gps.locate()}
  90.         local newT = new(newCoords[1], newCoords[2], newCoords[3])
  91.         local changeInCoords = newT - t;
  92.        
  93.         turtle.back()
  94.        
  95.         if changeInCoords == new(0, 0, -1) then
  96.             t:set(nil, nil, nil, "north")
  97.         elseif changeInCoords == new(1, 0, 0) then
  98.             t:set(nil, nil, nil, "east")
  99.         elseif changeInCoords == new(0, 0, 1) then
  100.             t:set(nil, nil, nil, "south")
  101.         elseif changeInCoords == new(-1, 0, 0) then
  102.             t:set(nil, nil, nil, "west")
  103.         end
  104.     end
  105.     return t
  106. end
  107.  
  108. function isPos(t)
  109.     return getmetatable(t) == pos
  110. end
  111.  
  112. function pos:set(x, y, z, dir)
  113.     if isPos(x) then
  114.         self.x = x.x
  115.         self.y = x.y
  116.         self.z = x.z
  117.         self.dir = x.dir
  118.     else
  119.         self.x = x or self.x
  120.         self.y = y or self.y
  121.         self.z = z or self.z
  122.         if type(dir) == "number" then
  123.             self.dir = dir % 4
  124.         elseif type(dir) == "string" then
  125.             self.dir = dirToNum(dir)
  126.         end
  127.     end
  128. end
  129.  
  130. function pos:clone()
  131.     return new(self.x, self.y, self.z, self.dir)
  132. end
  133.  
  134. function pos:replace(newPos, keepDir)
  135.     assert(isPos(newPos), "Cannot replace pos with non-pos")
  136.     if not keepDir then keepDir = false end
  137.    
  138.     self.x = newPos.x
  139.     self.y = newPos.y
  140.     self.z = newPos.z
  141.    
  142.     if not keepDir then self.dir = newPos.dir end
  143.    
  144.     return self
  145. end
  146.  
  147. function pos:change()
  148.  
  149. end
  150.  
  151. function pos:getForwardVector()
  152.     if self.dir == 0 then
  153.         return new(0, 0, -1, 0)
  154.     elseif self.dir == 1 then
  155.         return new(1, 0, 0, 1)
  156.     elseif self.dir == 2 then
  157.         return new(0, 0, 1, 2)
  158.     elseif self.dir == 3 then
  159.         return new(-1, 0, 0, 3)
  160.     end
  161. end
  162.  
  163. function pos:getBackwardVector()
  164.     return self:getForwardVector() * -1
  165. end
  166.  
  167. function pos:getLeftVector()
  168.     if self.dir == 0 then
  169.         return new(-1, 0, 0, 3)
  170.     elseif self.dir == 1 then
  171.         return new(0, 0, -1, 0)
  172.     elseif self.dir == 2 then
  173.         return new(1, 0, 0, 1)
  174.     elseif self.dir == 3 then
  175.         return new(0, 0, 1, 2)
  176.     end
  177. end
  178.  
  179. function pos:getRightVector()
  180.     return self:getLeftVector() * -1
  181. end
  182.  
  183. function pos.__add(a,b)
  184.     assert(isPos(a) and isPos(b), "Can't add pos to non-pos")
  185.     return new(a.x + b.x, a.y + b.y, a.z + b.z, a.dir)
  186. end
  187.  
  188. function pos.__sub(a,b)
  189.     assert(isPos(a) and isPos(b), "Can't sub pos from non-pos")
  190.     return new(a.x - b.x, a.y - b.y, a.z - b.z, a.dir)
  191. end
  192.  
  193. function pos.__mul(a,b)
  194.     if isPos(a) and type(b) == "number" then
  195.         return new(a.x * b, a.y * b, a.z * b)
  196.     elseif isPos(b) and type(a) == "number" then
  197.         return new(b.x * a, b.y * a, b.z * a)
  198.     else
  199.         error("expected pos and number")
  200.     end
  201. end
  202.  
  203. function pos.__div(a,b)
  204.     assert(isPos(a) and type(b) == "number", "Cannot divide pos by non-number")
  205.    
  206.     return new(a.x / b, a.y / b, a.z / b)
  207. end
  208.  
  209. function pos.__eq(a,b)
  210.     assert(isPos(a) and isPos(b), "Can't compare pos with non-pos")
  211.     return a.x == b.x and a.y == b.y and a.z == b.z
  212. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement