Advertisement
fatboychummy

relativeTurtlePather.lua

Sep 9th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.14 KB | None | 0 0
  1. -------------------------------------------------
  2. -- Copyright 2019 Fatboychummy
  3. -- You can edit and redistribute, but keep this
  4. -- copyright info in the code, untampered with.
  5. -------------------------------------------------
  6.  
  7. if not turtle then
  8.   error("Why are you trying to run this on a non-turtle computer?", 0)
  9.   -- seriuzly tho y
  10. end
  11.  
  12. local funcs = {}
  13. local nt = {}
  14. local saveFile = "TORTLOC.dat"
  15.  
  16. -------------------------------------------------
  17. -- function: save
  18. -- info    : save turtle positional data to file
  19. -- inputs  : nil
  20. -- returns : nil
  21. -------------------------------------------------
  22. function save(self)
  23.   local h = io.open(saveFile, 'w')
  24.   h:write("return {\n")
  25.   h:write("  facing = " .. tostring(self.facing) .. ",\n")
  26.   h:write("  x = " .. tostring(self.x) .. ",\n")
  27.   h:write("  y = " .. tostring(self.y) .. ",\n")
  28.   h:write("  z = " .. tostring(self.z) .. ",\n")
  29.   h:write("  home = {\n")
  30.   h:write("    x = " .. tostring(self.home.x) .. ",\n")
  31.   h:write("    y = " .. tostring(self.home.y) .. ",\n")
  32.   h:write("    z = " .. tostring(self.home.z) .. '\n')
  33.   h:write("  }\n")
  34.   h:write("}")
  35.   h:close()
  36. end
  37.  
  38. -------------------------------------------------
  39. -- function: doIf [private]
  40. -- info    : runs a function, if it's first returned thing is true, run a second
  41. --           function with the extra inputs
  42. -- inputs  : self, ifThis [function], thenThis[function], [extra]
  43. -- returns : whatever ifThis returns
  44. -------------------------------------------------
  45. local function doIf(self, ifThis, thenThis, ...)
  46.   local ptal = {ifThis()} -- run the func and put output to table
  47.   if ptal[1] then -- if the first item is true
  48.     thenThis(self, ...) -- run the second func
  49.   end
  50.   return table.unpack(ptal) -- return what the first function returned
  51. end
  52.  
  53. -------------------------------------------------
  54. -- function: moveBy
  55. -- info    : Changes the turtle object's position by df and du
  56. -- inputs  : self, df (distance moved forward), du (distance moved up)
  57. -- returns : nil
  58. -------------------------------------------------
  59. local function moveBy(self, df, du)
  60.   if not df then df = 0 end -- default df to 0
  61.   if not du then du = 0 end -- defualt du to 0
  62.  
  63.   if self.facing == 0 then -- facing towards positive Z
  64.     self.z = self.z + df
  65.   elseif self.facing == 1 then -- facing towards negative X
  66.     self.x = self.x - df
  67.   elseif self.facing == 2 then -- facing towards negative Z
  68.     self.z = self.z - df
  69.   elseif self.facing == 3 then -- facing towards positive X
  70.     self.x = self.x + df
  71.   end
  72.   self.y = self.y + du -- add du, which is default 0
  73.   save(self)
  74. end
  75.  
  76. -------------------------------------------------
  77. -- function: turtle movement functions
  78. -- info    : move the turtle AND set turtle data
  79. -- inputs  : nil
  80. -- returns : whatever the equivalent turtle function returns
  81. -------------------------------------------------
  82. function nt:forward()
  83.   return doIf(self, turtle.forward, moveBy, 1, 0)
  84. end
  85.  
  86. function nt:back()
  87.   return doIf(self, turtle.back, moveBy, -1, 0)
  88. end
  89.  
  90. function nt:up()
  91.   return doIf(self, turtle.up, moveBy, 0, 1)
  92. end
  93.  
  94. function nt:down()
  95.   return doIf(self, turtle.down, moveBy, 0, -1)
  96. end
  97.  
  98. function nt:turnLeft()
  99.   return doIf(self, turtle.turnLeft,
  100.   function()
  101.     self.facing = self.facing - 1
  102.     self.facing = self.facing % 4
  103.   end
  104.   )
  105. end
  106.  
  107. function nt:turnRight()
  108.   return doIf(self, turtle.turnRight,
  109.   function()
  110.     self.facing = self.facing + 1
  111.     self.facing = self.facing % 4
  112.   end
  113.   )
  114. end
  115.  
  116. -------------------------------------------------
  117. -- function: setHome
  118. -- info    : sets the turtle's "home" position
  119. -- inputs  : nil
  120. -- returns : nil
  121. -------------------------------------------------
  122. function nt:setHome()
  123.   self.home = {
  124.     x = self.x,
  125.     y = self.y,
  126.     z = self.z
  127.   }
  128. end
  129.  
  130. -------------------------------------------------
  131. -- function: getHome
  132. -- info    : gets the turtle's "home" position
  133. -- inputs  : nil
  134. -- returns : home[x], home[y], home[z]
  135. -------------------------------------------------
  136. function nt:getHome()
  137.   return self.home.x, self.home.y, self.home.z
  138. end
  139.  
  140. -------------------------------------------------
  141. -- function: getPos
  142. -- info    : gets the turtle's current relative position
  143. --           (from it's starting point)
  144. -- inputs  : nil
  145. -- returns : pos[x], pos[y], pos[z]
  146. -------------------------------------------------
  147. function nt:getPos()
  148.   return self.x, self.y, self.z
  149. end
  150.  
  151. -------------------------------------------------
  152. -- function: getFacing
  153. -- info    : gets the relative direction the turtle is facing
  154. --           (from it's starting point)
  155. -- inputs  : nil
  156. -- returns : the turtle's direction (0-3)
  157. -------------------------------------------------
  158. function nt:getFacing()
  159.   return self.facing
  160. end
  161.  
  162. -------------------------------------------------
  163. -- function: load
  164. -- info    : load turtle positional data from file
  165. -- inputs  : nil
  166. -- returns : true/false, depending if it works.
  167. -------------------------------------------------
  168. function nt:load()
  169.   local dat = loadfile(saveFile)
  170.   if type(dat) == "function" then
  171.     dat = dat()
  172.   else
  173.     return false
  174.   end
  175.  
  176.   self.facing = dat.facing
  177.   self.x = self.x
  178.   self.y = self.y
  179.   self.z = self.z
  180.   self.home = {}
  181.   self.home.x = dat.home.x
  182.   self.home.y = dat.home.y
  183.   self.home.z = dat.home.z
  184.   return true
  185. end
  186.  
  187. -------------------------------------------------
  188. -- function: equip
  189. -- info    : creates a turtle positional object
  190. -- inputs  : nil
  191. -- returns : Tortois
  192. -------------------------------------------------
  193. function funcs.equip()
  194.   local tmp = setmetatable(
  195.     {
  196.       facing = 0,
  197.       x = 0,
  198.       y = 0,
  199.       z = 0,
  200.       home = {
  201.         x = 0,
  202.         y = 0,
  203.         z = 0
  204.       }
  205.     },
  206.     {__index = nt}
  207.   )
  208.  
  209.   return tmp
  210. end
  211.  
  212. -------------------------------------------------
  213. -- function: uhhhhhhhhhhhhhhhhh
  214. -- info    : uhhhhhhhhhhhhhhhhhh
  215. -- inputs  : uhhhhhhhhhhhhhhhhhhhhhhh
  216. -- returns : uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
  217. -------------------------------------------------
  218. function funcs.unequip()
  219.   -- is this actually needed?
  220. end
  221.  
  222. return funcs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement