galygious

Turtle Movement Api

Jul 1st, 2023 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. local galymoveAPI = {}
  2.  
  3. -- Variables
  4. local position = { x = 0, y = 0, z = 0 }
  5. local direction = 0 -- 0: North, 1: East, 2: South, 3: West
  6. local positionFile = "position.txt"
  7.  
  8. -- Functions
  9. local function breakBlock()
  10. while turtle.detect() do
  11. turtle.dig()
  12. sleep(0.5) -- Wait for block to break
  13. end
  14. end
  15.  
  16. local function handleGravel()
  17. while turtle.detect() and turtle.compare() do
  18. turtle.dig()
  19. sleep(0.5) -- Wait for gravel to fall
  20. end
  21. end
  22.  
  23. local function handleEntity()
  24. while turtle.detect() do
  25. turtle.attack()
  26. sleep(0.5) -- Wait for entity to move
  27. end
  28. end
  29.  
  30. local function moveForward(steps)
  31. for _ = 1, steps do
  32. while not turtle.forward() do
  33. if turtle.detect() then
  34. breakBlock()
  35. elseif turtle.detectDown() and turtle.compareDown() then
  36. handleGravel()
  37. elseif turtle.detectUp() then
  38. handleEntity()
  39. end
  40. end
  41.  
  42. if direction == 0 then
  43. position.z = position.z + 1
  44. elseif direction == 1 then
  45. position.x = position.x + 1
  46. elseif direction == 2 then
  47. position.z = position.z - 1
  48. elseif direction == 3 then
  49. position.x = position.x - 1
  50. end
  51. end
  52. end
  53.  
  54. local function moveBack(steps)
  55. for _ = 1, steps do
  56. while not turtle.back() do
  57. if turtle.detect() then
  58. breakBlock()
  59. elseif turtle.detectDown() and turtle.compareDown() then
  60. handleGravel()
  61. elseif turtle.detectUp() then
  62. handleEntity()
  63. end
  64. end
  65.  
  66. if direction == 0 then
  67. position.z = position.z - 1
  68. elseif direction == 1 then
  69. position.x = position.x - 1
  70. elseif direction == 2 then
  71. position.z = position.z + 1
  72. elseif direction == 3 then
  73. position.x = position.x + 1
  74. end
  75. end
  76. end
  77.  
  78. local function moveUp(steps)
  79. for _ = 1, steps do
  80. while not turtle.up() do
  81. if turtle.detect() then
  82. breakBlock()
  83. elseif turtle.detectDown() and turtle.compareDown() then
  84. handleGravel()
  85. elseif turtle.detectUp() then
  86. handleEntity()
  87. end
  88. end
  89. position.y = position.y + 1
  90. end
  91. end
  92.  
  93. local function moveDown(steps)
  94. for _ = 1, steps do
  95. while not turtle.down() do
  96. if turtle.detect() then
  97. breakBlock()
  98. elseif turtle.detectDown() and turtle.compareDown() then
  99. handleGravel()
  100. elseif turtle.detectUp() then
  101. handleEntity()
  102. end
  103. end
  104. position.y = position.y - 1
  105. end
  106. end
  107.  
  108. local function turnRight()
  109. turtle.turnRight()
  110. direction = (direction - 1) % 4
  111. end
  112.  
  113. local function turnLeft()
  114. turtle.turnLeft()
  115. direction = (direction + 1) % 4
  116. end
  117.  
  118. local function moveRight(steps)
  119. turtle.turnRight()
  120. moveForward(steps or 1)
  121. turtle.turnLeft()
  122. position.x = position.x + 1
  123. end
  124.  
  125. local function moveLeft(steps)
  126. turtle.turnLeft()
  127. moveForward(steps or 1)
  128. turtle.turnRight()
  129. position.x = position.x - 1
  130. end
  131.  
  132. function galymoveAPI.move(direction, steps)
  133. steps = steps or 1 -- Set default value to 1 if steps is not provided
  134. if direction == "f" then
  135. moveForward(steps)
  136. elseif direction == "b" then
  137. moveBack(steps)
  138. elseif direction == "u" then
  139. moveUp(steps)
  140. elseif direction == "d" then
  141. moveDown(steps)
  142. elseif direction == "r" then
  143. turnRight()
  144. elseif direction == "l" then
  145. turnLeft()
  146. end
  147. end
  148.  
  149. function galymoveAPI.getPosition()
  150. return position
  151. end
  152.  
  153. function galymoveAPI.getDirection()
  154. return direction
  155. end
  156.  
  157. function galymoveAPI.savePositionToFile()
  158. local file = fs.open(positionFile, "w")
  159. file.writeLine(position.x)
  160. file.writeLine(position.y)
  161. file.writeLine(position.z)
  162. file.writeLine(direction)
  163. file.close()
  164. end
  165.  
  166. function galymoveAPI.loadPositionFromFile()
  167. if fs.exists(positionFile) then
  168. local file = fs.open(positionFile, "r")
  169. position.x = tonumber(file.readLine())
  170. position.y = tonumber(file.readLine())
  171. position.z = tonumber(file.readLine())
  172. direction = tonumber(file.readLine())
  173. file.close()
  174. end
  175. end
  176.  
  177. return galymoveAPI
  178.  
Advertisement
Add Comment
Please, Sign In to add comment