Advertisement
hornedcommando

Tracking Turtle

Apr 16th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | Gaming | 0 0
  1. --Turtle Relative Location Library
  2. --- Turtle object which tracks its movements.
  3. -- This module extends the capabilities of a normal turtle by allowing it to
  4. -- locate itself, determine its facing, and track its movements from its initial
  5. -- location.
  6. -- @module TrackingTurtle
  7. --by fatboychummy
  8.  
  9. -- CC module includes
  10. local expect = require "cc.expect".expect
  11.  
  12. local tTurtle = {}
  13.  
  14. --- Create a new TrackingTurtle object.
  15. -- @tparam boolean? blockTurtleAccess If true, will rewrite all base turtle functions (which are contained in this object) in the global to throw an error when ran.
  16. -- @treturn table The TrackingTurtle object.
  17. function tTurtle.create(blockTurtleAccess)
  18. local obj = {}
  19.  
  20. local position = {0,0,0}
  21. local facing = 0
  22. local movementFunction
  23.  
  24. local tForward = turtle.forward
  25. function obj.forward()
  26. local ok, res = tForward()
  27. if ok then
  28. position = {tTurtle.getNextPosition(position[1], position[2], position[3], facing)}
  29. if movementFunction then
  30. movementFunction()
  31. end
  32. end
  33. return ok, res
  34. end
  35.  
  36. local tBack = turtle.back
  37. function obj.back()
  38. local ok, res = tBack()
  39. if ok then
  40. position = {tTurtle.getNextPosition(position[1], position[2], position[3], (facing + 2) % 4)}
  41. if movementFunction then
  42. movementFunction()
  43. end
  44. end
  45. return ok, res
  46. end
  47.  
  48. local tUp = turtle.up
  49. function obj.up()
  50. local ok, res = tUp()
  51. if ok then
  52. position = {tTurtle.getNextPosition(position[1], position[2], position[3], 4)}
  53. if movementFunction then
  54. movementFunction()
  55. end
  56. end
  57. return ok, res
  58. end
  59.  
  60. local tDown = turtle.down
  61. function obj.down()
  62. local ok, res = tDown()
  63. if ok then
  64. position = {tTurtle.getNextPosition(position[1], position[2], position[3], 5)}
  65. if movementFunction then
  66. movementFunction()
  67. end
  68. end
  69. return ok, res
  70. end
  71.  
  72. local tTurnLeft = turtle.turnLeft
  73. function obj.turnLeft()
  74. local ok, res = tTurnLeft()
  75. if ok then
  76. facing = (facing - 1) % 4
  77. end
  78. return ok, res
  79. end
  80.  
  81. local tTurnRight = turtle.turnRight
  82. function obj.turnRight()
  83. local ok, res = tTurnRight()
  84. if ok then
  85. facing = (facing + 1) % 4
  86. end
  87. return ok, res
  88. end
  89.  
  90. --- Locate and determine facing of this turtle.
  91. -- @treturn boolean Whether or not the turtle could determine its location and facing.
  92. function obj.locate()
  93.  
  94. end
  95.  
  96. --- Set the movement function
  97. -- @tparam function|nil f The function to set (or nil to clear it).
  98. function obj.setMovementFunction(f)
  99. expect(1, f, "function", "nil")
  100.  
  101. movementFunction = f
  102. end
  103.  
  104. --- Get the movement function
  105. -- @treturn function|nil The movement function, or nil if unset.
  106. function obj.getMovementFunction()
  107. return movementFunction
  108. end
  109.  
  110. --- Turn the turtle to face in a specific direction.
  111. -- @tparam number direction The direction to face
  112. function obj.face(direction)
  113. expect(1, direction, "number")
  114. if direction < 0 or direction > 3 or direction % 1 ~= 0 then
  115. error("Expected integer from 0-3.", 2)
  116. end
  117.  
  118. local turnDir = obj.turnLeft
  119.  
  120. if (facing + 1) % 4 == direction then
  121. turnDir = obj.turnRight
  122. end
  123.  
  124. while facing ~= direction do
  125. turnDir()
  126. end
  127. end
  128.  
  129. --- Get the direction the turtle is facing.
  130. -- @treturn number The direction the turtle is facing. 0=north(-Z), 1=east(+X), 2=south(+Z), 3=west(-X).
  131. function obj.getFacing()
  132. return facing
  133. end
  134.  
  135. --- Get the position of this turtle.
  136. -- @treturn number,number,number The position of this turtle.
  137. function obj.getPosition()
  138. return table.unpack(position, 1, 3)
  139. end
  140.  
  141. if blockTurtleAccess then
  142. for k, v in pairs(obj) do
  143. if turtle[k] then
  144. turtle[k] = function() error("Base turtle movement blocked.", 2) end
  145. end
  146. end
  147. end
  148.  
  149. return setmetatable(obj, {__index = tTurtle})
  150. end
  151.  
  152. --- Return the position the turtle would move to if moving in facing direction.
  153. -- use 4 for up, and 5 for down.
  154. -- @tparam number x The starting x position.
  155. -- @tparam number y The starting y position.
  156. -- @tparam number z The starting z position.
  157. -- @tparam number facing The facing of the turtle.
  158. function tTurtle.getNextPosition(x, y, z, facing)
  159. expect(1, x, "number")
  160. expect(2, y, "number")
  161. expect(3, z, "number")
  162. expect(4, facing, "number")
  163.  
  164. if facing == 0 then -- facing -z (north)
  165. return x, y, z - 1
  166. elseif facing == 1 then -- facing +x (east)
  167. return x + 1, y, z
  168. elseif facing == 2 then -- facing +z (south)
  169. return x, y, z + 1
  170. elseif facing == 3 then -- facing -x (west)
  171. return x - 1, y, z
  172. elseif facing == 4 then -- going up
  173. return x, y + 1, z
  174. elseif facing == 5 then -- going down
  175. return x, y - 1, z
  176. end
  177.  
  178. error(string.format("Unknown facing: %d", facing), 2)
  179. end
  180.  
  181. --- Determine which direction the turtle needs to move.
  182. -- NOTE: This function expects that the inputted blocks will be directly beside
  183. -- eachother, so there may be unexpected results if they are not side-by-side.
  184. -- @tparam x1 The starting x position.
  185. -- @tparam y1 The starting y position.
  186. -- @tparam z1 The starting z position.
  187. -- @tparam x2 The desired x position.
  188. -- @tparam y2 The desired y position.
  189. -- @tparam z2 The desired z position.
  190. -- @treturn number The facing to get to the block. Returns 4 for "up", and 5 for "down". Returns -1 if the desired block is the same as the starting block.
  191. function tTurtle.getFacingToBlock(x1, y1, z1, x2, y2, z2)
  192. expect(1, x1, "number")
  193. expect(2, y1, "number")
  194. expect(3, z1, "number")
  195. expect(4, x2, "number")
  196. expect(5, y2, "number")
  197. expect(6, z2, "number")
  198.  
  199. if z2 < z1 then -- +Z (north)
  200. return 0
  201. elseif x2 > x1 then -- +X (east)
  202. return 1
  203. elseif z2 < z1 then -- -Z (south)
  204. return 2
  205. elseif x2 < x1 then -- -X (west)
  206. return 3
  207. elseif y2 > y1 then -- +Y (up)
  208. return 4
  209. elseif y2 < y1 then -- -Y (down)
  210. return 5
  211. end
  212.  
  213. return -1
  214. end
  215.  
  216. return setmetatable(tTurtle, {__index = turtle})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement