ladyDia

3x+1 turtle

Aug 16th, 2021 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. local a = {}
  2. local args = {...}
  3.  
  4. local counter = 4
  5. local numbering = 5
  6.  
  7. local posX = 0
  8. local posY = 0
  9. local posZ = 0
  10.  
  11. posX, posY, posZ = gps.locate(5)
  12.  
  13. local startX = posX
  14. local startY = posY
  15. local startZ = posZ
  16.  
  17. local direction = 0
  18. local staticDirect = 0
  19. local startDirect = 0
  20.  
  21. local inventory = 1
  22.  
  23. function a.orient()
  24. turtle.forward()
  25. posX, posY, posZ = gps.locate(5)
  26. if posX-startX > 0 then
  27. direction = 0
  28. end
  29. if posZ-startZ > 0 then
  30. direction = 1
  31. end
  32. if posX-startX < 0 then
  33. direction = 2
  34. end
  35. if posX-startX < 0 then
  36. direction = 3
  37. end
  38. staticDirect = direction
  39. startDirect = direction
  40. end
  41.  
  42. function a.threeXplus()
  43. for i=0, tonumber(args[1]) do
  44. counter = counter+1
  45. numbering = counter
  46. while numbering>4 do
  47. if numbering%2 == 0 then
  48. numbering = numbering/2
  49. a.buildDown()
  50. turtle.forward()
  51. turtle.turnLeft()
  52. staticDirect = staticDirect - 1
  53. turtle.forward()
  54. if tonumber(args[2]) == 1 then
  55. staticDirect = staticDirect + 1
  56. turtle.turnRight()
  57. end
  58. else
  59. numbering = numbering*3+1
  60. a.buildDown()
  61. turtle.forward()
  62. turtle.turnRight()
  63. staticDirect = staticDirect + 1
  64. turtle.forward()
  65. if tonumber(args[2]) == 1 then
  66. staticDirect = staticDirect - 1
  67. turtle.turnLeft()
  68. end
  69. end
  70. if staticDirect> 3 then
  71. staticDirect= 0
  72. end
  73. if staticDirect< 0 then
  74. staticDirect= 3
  75. end
  76. direction = staticDirect
  77. a.checkSupply()
  78. end
  79. end
  80. end
  81.  
  82. function a.checkSupply()
  83. turtle.select(16)
  84. if turtle.getItemCount() < 10 then
  85. posX, posY, posZ = gps.locate(5)
  86. tempX = posX
  87. tempY = posY
  88. tempZ = posZ
  89. a.moveTo(startX,tempY,startZ)
  90. a.moveTo(startX,startY,startZ)
  91. a.rotateTo(startDirect)
  92. turtle.turnLeft()
  93. print("Not enough blocks")
  94. while turtle.getItemCount() <10 do
  95. turtle.suck()
  96. sleep(1)
  97. end
  98. turtle.turnRight()
  99. a.moveTo(startX,tempY,startZ)
  100. a.moveTo(tempX,tempY,tempZ)
  101. inventory = 1
  102. end
  103. turtle.select(inventory)
  104. if turtle.getFuelLevel() < 100 then
  105. posX, posY, posZ = gps.locate(5)
  106. tempX = posX
  107. tempY = posY
  108. tempZ = posZ
  109. a.moveTo(startX,tempY,startZ)
  110. a.moveTo(startX,startY,startZ)
  111. a.rotateTo(startDirect)
  112. turtle.turnRight()
  113. print("Not enough fuel")
  114. while turtle.getFuelLevel() < 100 do
  115. turtle.suck()
  116. turtle.refuel()
  117. sleep(1)
  118. end
  119. turtle.turnLeft()
  120. a.moveTo(startX,tempY,startZ)
  121. a.moveTo(tempX,tempY,tempZ)
  122. end
  123. end
  124.  
  125. function a.buildDown()
  126. if turtle.getItemCount() >0 then
  127. inventory = inventory+1
  128. end
  129. if inventory > 16 then
  130. a.checkSupply()
  131. else
  132. turtle.select(inventory)
  133. turtle.placeDown()
  134. end
  135. end
  136.  
  137. function a.moveTo(targetX,targetY,targetZ)
  138. posX, posY, posZ = gps.locate(5)
  139. while posY > targetY do
  140. turtle.down()
  141. end
  142. while posY < targetY do
  143. turtle.up()
  144. end
  145.  
  146. if posX > targetX then
  147. a.rotateTo(2)
  148. end
  149. if posX < targetX then
  150. a.rotateTo(0)
  151. end
  152. while posX ~= targetX do
  153. turtle.forward()
  154. end
  155.  
  156. if posZ > targetZ then
  157. a.rotateTo(3)
  158. end
  159. if posZ < targetZ then
  160. a.rotateTo(1)
  161. end
  162. while posZ ~= targetZ do
  163. turtle.forward()
  164. end
  165. end
  166.  
  167. function a.rotateTo(targetD)
  168. while direction ~= targetD
  169. if direction > 3 then
  170. direction = 0
  171. end
  172. if direction < 0 then
  173. direction = 3
  174. end
  175. if direction < targetD then
  176. direction = direction + 1
  177. turtle.turnRight()
  178. end
  179. if direction > targetD then
  180. direction = direction - 1
  181. turtle.turnLeft()
  182. end
  183. end
  184. end
  185.  
  186.  
  187. --Start of program
  188. a.orient()
  189. a.threeXplus()
Add Comment
Please, Sign In to add comment