Advertisement
Guest User

Untitled

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