Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
1,813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. directions = {"north", "east", "south", "west"}
  2.  
  3. local position = {}
  4. local direction = ""
  5. function init(x, y, z, dir)
  6. position = to_pos(x, y, z)
  7. direction = dir
  8. end
  9.  
  10. function forward()
  11. local val = turtle.forward()
  12. if val then
  13. forward_increment()
  14. print_pos()
  15. end
  16. return val
  17. end
  18.  
  19. function back()
  20. local val = turtle.back()
  21. if val then
  22. backward_increment()
  23. print_pos()
  24. end
  25. return val
  26. end
  27.  
  28. function up()
  29. local val = turtle.up()
  30. if val then
  31. position = {
  32. x = position.x,
  33. y = position.y + 1,
  34. z = position.z
  35. }
  36. print_pos()
  37. end
  38. return val
  39. end
  40.  
  41. function down()
  42. local val = turtle.down()
  43. if val then
  44. position = {
  45. x = position.x,
  46. y = position.y - 1,
  47. z = position.z
  48. }
  49. print_pos()
  50. end
  51. return val
  52. end
  53.  
  54. function turnLeft()
  55. local val = turtle.turnLeft()
  56. if val then
  57. if direction == "north" then
  58. direction = "west"
  59. elseif direction == "east" then
  60. direction = "north"
  61. elseif direction == "south" then
  62. direction = "east"
  63. elseif direction == "west" then
  64. direction = "south"
  65. end
  66. end
  67. return val
  68. end
  69.  
  70. function turnRight()
  71. local val = turtle.turnRight()
  72. if val then
  73. if direction == "north" then
  74. direction = "east"
  75. elseif direction == "east" then
  76. direction = "south"
  77. elseif direction == "south" then
  78. direction = "west"
  79. elseif direction == "west" then
  80. direction = "north"
  81. end
  82. end
  83. return val
  84. end
  85. function forward_increment()
  86. if direction == directions[1] then
  87. position = {
  88. x = position.x,
  89. y = position.y,
  90. z = position.z - 1
  91. }
  92. elseif direction == directions[2] then
  93. position = {
  94. x = position.x + 1,
  95. y = position.y,
  96. z = position.z
  97. }
  98. elseif direction == directions[3] then
  99. position = {
  100. x = position.x,
  101. y = position.y,
  102. z = position.z + 1
  103. }
  104. elseif direction == directions[4] then
  105. position = {
  106. x = position.x - 1,
  107. y = position.y,
  108. z = position.z
  109. }
  110. end
  111. end
  112.  
  113. function backward_increment()
  114. if direction == directions[1] then
  115. position = {
  116. x = position.x,
  117. y = position.y,
  118. z = position.z + 1
  119. }
  120. elseif direction == directions[2] then
  121. position = {
  122. x = position.x - 1,
  123. y = position.y,
  124. z = position.z
  125. }
  126. elseif direction == directions[3] then
  127. position = {
  128. x = position.x,
  129. y = position.y,
  130. z = position.z - 1
  131. }
  132. elseif direction == directions[4] then
  133. position = {
  134. x = position.x + 1,
  135. y = position.y,
  136. z = position.z
  137. }
  138. end
  139. end
  140.  
  141. function to_pos(x1, y1, z1)
  142. return {x=x1, y=y1, z=z1}
  143. end
  144.  
  145. function print_pos()
  146. print("Position: (" .. position.x .. ", " .. position.y .. ", " .. position.z .. ")")
  147. end
  148.  
  149. function turn_to(x, z)
  150. if x == 1 then
  151. while direction ~= "east" do
  152. turnRight()
  153. end
  154. elseif x == -1 then
  155. while direction ~= "west" do
  156. turnRight()
  157. end
  158. elseif z == 1 then
  159. while direction ~= "south" do
  160. turnRight()
  161. end
  162. elseif z == -1 then
  163. while direction ~= "north" do
  164. turnRight()
  165. end
  166. end
  167. end
  168.  
  169. function goto(xPos, yPos, zPos)
  170. -- Get direction vector
  171. local xDir, yDir, zDir
  172. if xPos > position.x then
  173. xDir = 1
  174. else
  175. xDir = -1
  176. end
  177. if yPos > position.y then
  178. yDir = 1
  179. else
  180. yDir = -1
  181. end
  182. if zPos > position.z then
  183. zDir = 1
  184. else
  185. zDir = -1
  186. end
  187.  
  188. -- match x
  189. turn_to(xDir, 0)
  190. while (position.x ~= xPos) do
  191. if turtle.detect() then
  192. turtle.dig()
  193. end
  194. forward()
  195. end
  196.  
  197. -- match z
  198. turn_to(0, zDir)
  199. while (position.z ~= zPos) do
  200. if turtle.detect() then
  201. turtle.dig()
  202. end
  203. forward()
  204. end
  205.  
  206. -- match y
  207. while (position.y ~= yPos) do
  208. if yDir == 1 then
  209. if turtle.detectUp() then
  210. turtle.digUp()
  211. end
  212. up()
  213. else
  214. if turtle.detectDown() then
  215. turtle.digDown()
  216. end
  217. down()
  218. end
  219. end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement