alesandreo

lib/ale/turtle/position.lua

Aug 11th, 2021 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1.  
  2. -- https://pastebin.com/xzFNmXX1
  3.  
  4. Position = {
  5.   x = 0,
  6.   y = 0,
  7.   z = 0,
  8.   face = 0,
  9.   filename = nil,
  10.   facing_map = {
  11.     N = 0,
  12.     E = 1,
  13.     S = 2,
  14.     W = 3,
  15.   }
  16. }
  17.  
  18. Position.__index = Position
  19.  
  20. function Position:new(x, y , z, face)
  21.   if type(x) == "string" then
  22.     self.filename = x
  23.     x = nil
  24.     if not self.config then
  25.       self.config = Config:new(self.filename)
  26.       if self.config:load() then
  27.         x = self.config.data.x
  28.         y = self.config.data.y
  29.         z = self.config.data.z
  30.         face = self.config.data.face
  31.       else
  32.       end
  33.     end
  34.   end
  35.   local o = {}
  36.   setmetatable(o, Position)
  37.   o.x = x or 0
  38.   o.y = y or 0
  39.   o.z = z or 0
  40.   face = face or 0
  41.   o:setFace(face)
  42.   return o
  43. end
  44.  
  45. function Position:offsetByPosition(position)
  46.   position = position or Position:new(0,0,0,0)
  47.   self:offset(position.x, position.y, position.z, position.face)
  48. end
  49.  
  50. function Position:offset(x, y, z, face)
  51.   x = x or 0
  52.   y = y or 0
  53.   z = z or 0
  54.   face = face or 0
  55.   self.x = self.x + x
  56.   self.y = self.y + y
  57.   self.z = self.z + z
  58.   self:setFace(face)
  59.   return true
  60. end
  61.  
  62. function Position:getFaceFromString(facing)
  63.   facing = string.upper(string.sub(facing, 1, 1))
  64.   return self.facing_map[facing]
  65. end
  66.  
  67. function Position:getFaceString()
  68.   for k,v in pairs(self.facing_map) do
  69.     if v == self.face then
  70.       return k
  71.     end
  72.   end
  73. end
  74.  
  75. function Position:setFace(face)
  76.   if type(face) == "string" then
  77.     face = self:getFaceFromString(face)
  78.   end
  79.   self.face = face % 4
  80.   self:persistPosition()
  81.   return true
  82. end
  83.  
  84. function Position:getBackFace()
  85.   return ( self.face + 2 ) % 4
  86. end
  87.  
  88. function Position:turnRight(n)
  89.   n = n or 1
  90.   self:setFace(self.face + n)
  91.   return true
  92. end
  93.  
  94. function Position:turnLeft(n)
  95.   n = n or 1
  96.   self:setFace(self.face - n)
  97.   return true
  98. end
  99.  
  100. function Position:turnAround(n)
  101.   n = n or 1
  102.   n = n * 2
  103.   self:setFace(self.face - n)
  104.   return true
  105. end
  106.  
  107. function Position:persistPosition()
  108.   if self.filename then
  109.     if not self.config then
  110.       self.config = Config:new(self.filename)
  111.     end
  112.     self.config.data = {x = self.x, y = self.y, z = self.z, face = self.face}
  113.     self.config:save()
  114.   end
  115. end
  116.  
  117. function Position:increment(n, direction)
  118.   n = n or 1
  119.   if direction == 0 then
  120.     self.z = self.z - n
  121.   elseif direction == 1 then
  122.     self.x = self.x + n
  123.   elseif direction == 2 then
  124.     self.z = self.z + n
  125.   elseif direction == 3 then
  126.     self.x = self.x - n
  127.   elseif direction == 4 then
  128.     self.y = self.y + n
  129.   elseif direction == 5 then
  130.     self.y = self.y - n
  131.   end
  132.   self:persistPosition()
  133.   return true
  134. end
  135.  
  136. function Position:forward(n)
  137.   n = n or 1
  138.   return self:increment(n, self.face)
  139. end
  140.  
  141. function Position:back(n)
  142.   n = n or 1
  143.   return self:increment(n, self:getBackFace())
  144. end
  145.  
  146. function Position:up(n)
  147.   n = n or 1
  148.   return self:increment(n, 4)
  149. end
  150.  
  151. function Position:down(n)
  152.   n = n or 1
  153.   return self:increment(n, 5)
  154. end
  155.  
  156. function Position:compareAxis(axis, value)
  157.   return self[axis] == value
  158. end
  159.  
  160. function Position:compare(comparison)
  161.   return (
  162.     self:compareAxis('x', comparison.x) and
  163.     self:compareAxis('y', comparison.y) and
  164.     self:compareAxis('z', comparison.z) and
  165.     self:compareAxis('face', comparison.face)
  166.   )
  167. end
  168.  
  169. function Position:copy()
  170.   return self:new(self.x, self.y, self.x, self.face)
  171. end
  172.  
  173. function Position:toString()
  174.   return "{ x: "..self.x.." y: "..self.y.." z: "..self.z.." face: "..self:getFaceString() .. " }"
  175. end
Add Comment
Please, Sign In to add comment