Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. local direction = 0
  2.  
  3. local function GetXFromDirection()
  4. if (direction == 1) then return -1
  5. elseif (direction == 3) then return 1
  6. else return 0
  7. end
  8. end
  9.  
  10. local function GetZFromDirection()
  11. if (direction == 0) then return 1
  12. elseif (direction == 2) then return -1
  13. else return 0
  14. end
  15. end
  16.  
  17. local x = 0
  18. local y = 0
  19. local z = 0
  20.  
  21. -- Save direction and coordinates to file.
  22. local function Store()
  23. local h = fs.open("location", "w")
  24. h.writeLine(x)
  25. h.writeLine(y)
  26. h.writeLine(z)
  27. h.writeLine(direction)
  28. h.close()
  29. end
  30.  
  31. -- Reads direction and coordinates from file.
  32. local function Read()
  33. if fs.exists("location") then
  34. local h = fs.open("location", "r")
  35. x = tonumber(h.readLine())
  36. y = tonumber(h.readLine())
  37. z = tonumber(h.readLine())
  38. direction = tonumber(h.readLine())
  39. h.close()
  40.  
  41. return true
  42. end
  43.  
  44. return false
  45. end
  46.  
  47. function moveBackOnce()
  48. -- Try and move.
  49. moved = turtle.back()
  50. -- If move success increment coordinates.
  51. if(moved) then
  52. x = x - GetXFromDirection()
  53. z = z - GetZFromDirection()
  54.  
  55. Store()
  56. end
  57.  
  58. return moved
  59. end
  60.  
  61. function moveForwardOnce()
  62. -- Try and move.
  63. moved = turtle.forward()
  64. -- If move success increment coordinates.
  65. if(moved) then
  66. x = x + GetXFromDirection()
  67. z = z + GetZFromDirection()
  68.  
  69. Store()
  70. end
  71. return moved
  72. end
  73.  
  74. -- Turn Right.
  75. local function TurnLeftOnce()
  76. if(direction > 0) then
  77. direction = direction - 1
  78. else
  79. direction = 3
  80. end
  81.  
  82. turtle.turnLeft()
  83.  
  84. Store()
  85. end
  86.  
  87. -- Turn Left.
  88. local function TurnRightOnce()
  89. if(direction < 3) then
  90. direction = direction + 1
  91. else
  92. direction = 0
  93. end
  94.  
  95. turtle.turnRight()
  96.  
  97. Store()
  98. end
  99.  
  100. -- Move up.
  101. local function moveUpOnce()
  102. moved = turtle.up()
  103.  
  104. if(moved) then
  105. y = y + 1
  106.  
  107. Store()
  108. end
  109.  
  110. return moved
  111. end
  112.  
  113. -- Move down.
  114. local function moveDownOnce()
  115. moved = turtle.down()
  116.  
  117. if(moved) then
  118. y = y - 1
  119.  
  120. Store()
  121. end
  122.  
  123. return moved
  124. end
  125.  
  126. -- =======================================================================================================================================================
  127. -- Public API
  128. -- =======================================================================================================================================================
  129. -- Initalization:
  130. -- =======================================================================================================================================================
  131. -- Sets the position and heading of the turtle. Use for initialization.
  132. -- FACING:
  133. --[[
  134. 0 = South
  135. 1 = West
  136. 2 = North
  137. 3 = East
  138. --]]
  139. function SetPosition(X, Y, Z, FACING)
  140. x = X
  141. y = Y
  142. z = Z
  143. direction = FACING
  144. Store()
  145. end
  146.  
  147. -- Initializes the navigator from file backed storage.
  148. function InitializeNavigator()
  149. return Read()
  150. end
  151.  
  152. -- Prints the direction enumeration in a user readable format.
  153. function PrintDirections()
  154. print("0 = South")
  155. print("1 = West")
  156. print("2 = North")
  157. print("3 = East")
  158. end
  159.  
  160. -- Takes N, S, E, W and converts it to numbers.
  161. function GetDirectionINT(dir)
  162. dir = string.lower(dir)
  163.  
  164. if(dir == "s") or (dir == "south") then return 0 end
  165. if(dir == "n") or (dir == "north") then return 2 end
  166. if(dir == "e") or (dir == "east") then return 3 end
  167. if(dir == "w") or (dir == "west") then return 1 end
  168. end
  169.  
  170. -- Returns the current position of the turtle.
  171. function GetPosition()
  172. Read()
  173. return x, y, z, direction
  174. end
  175. -- =======================================================================================================================================================
  176. -- Rotation:
  177. -- =======================================================================================================================================================
  178.  
  179. -- Turns left.
  180. -- Goal (optinal): The target heading to turn toward.
  181. -- Returns if movement succesfull.
  182. function TurnLeft( goal )
  183. goal = goal or "T"
  184.  
  185. if(goal == "T") then
  186. TurnLeftOnce()
  187. else
  188. while (direction ~= goal) do
  189. TurnLeftOnce()
  190. end
  191. end
  192. end
  193.  
  194. -- Turns right.
  195. -- Goal (optinal): The target heading to turn toward.
  196. -- Returns if movement succesfull.
  197. function TurnRight( goal )
  198. goal = goal or "T"
  199. --print(goal .. " : " .. direction)
  200.  
  201. if(goal == "T") then
  202. TurnRightOnce()
  203. else
  204. while (direction ~= goal) do
  205. --print(goal .. " : " .. direction)
  206. TurnRightOnce()
  207. end
  208. end
  209. end
  210. -- =======================================================================================================================================================
  211. -- Moving:
  212. -- =======================================================================================================================================================
  213. local function Navigate(mainMovementFunction, resetMovementFunction, goal)
  214. goal = goal or "T"
  215. steps = 0
  216.  
  217. -- If no steps required return true.
  218. if(goal == 0) then return true end
  219.  
  220. -- If only a single step is required than do it.
  221. if(goal == "T") then return mainMovementFunction() end
  222.  
  223. -- Try and move to the target position
  224. moved = false
  225. while goal > steps do
  226. moved = mainMovementFunction()
  227.  
  228. if(moved == true) then
  229. steps = steps + 1
  230. else
  231. -- If cant move reset
  232. while steps > 0 do
  233. resetMovementFunction()
  234. steps = steps - 1
  235. end
  236. -- And break out of the movement loop
  237. return false
  238. end
  239. end
  240.  
  241. return true
  242. end
  243.  
  244. -- Moves Up and returns if succesfull.
  245. -- Goal (optinal): Number of steps taken. If movement is obstructed turtle will reset to its starting position.
  246. -- Returns if movement succesfull.
  247. function Up(goal)
  248. return Navigate(moveUpOnce, moveDownOnce, goal)
  249. end
  250.  
  251. -- Moves Down and returns if succesfull.
  252. -- Goal (optinal): Number of steps taken. If movement is obstructed turtle will reset to its starting position.
  253. -- Returns if movement succesfull.
  254. function Down(goal)
  255. return Navigate(moveDownOnce, moveUpOnce, goal)
  256. end
  257.  
  258. -- Moves back and returns if succesfull.
  259. -- Goal (optinal): Number of steps taken. If movement is obstructed turtle will reset to its starting position.
  260. -- Returns if movement succesfull.
  261. function Back(goal)
  262. return Navigate(moveBackOnce, moveForwardOnce, goal)
  263. end
  264.  
  265. -- Moves forward and returns if succesfull.
  266. -- Goal (optinal): Number of steps taken. If movement is obstructed turtle will reset to its starting position.
  267. -- Returns if movement succesfull.
  268. function Forward(goal)
  269. return Navigate(moveForwardOnce, moveBackOnce, goal)
  270. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement