Advertisement
fatboychummy

Tortois

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