Advertisement
Paxon57

Untitled

Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. args = {...}
  2.  
  3. -- Init variables
  4. local size = args[1] or 5
  5.  
  6. local maxSteps = size * size
  7. local step = 1
  8. local dir = true -- True for away, false for towards starting position
  9.  
  10. local bedrockDetected = false
  11.  
  12. local pos = {
  13. x = 1,
  14. y = 1,
  15. z = 0,
  16. f = 0
  17. }
  18. local lastData = {
  19. x = 0,
  20. y = 0,
  21. z = 0,
  22. f = 0,
  23. step = 0,
  24. dir = false
  25. }
  26.  
  27. -- States:
  28. -- 0 - Mining
  29. -- 1 - PutToChest
  30. local state = 0
  31.  
  32. --Functions
  33. local function tryToRefuel( fuelNeeded )
  34. for i = 1, 16 do
  35. turtle.select(i)
  36. while turtle.refuel(0) and turtle.getFuelLevel() < fuelNeeded * 1.5 do
  37. turtle.refuel(1)
  38. end
  39. end
  40. turtle.select(1)
  41. end
  42.  
  43. local function checkFuel()
  44. local fuelNeeded = math.abs(pos.z) * 2 + size * 4
  45. if turtle.getFuelLevel() < fuelNeeded then
  46. tryToRefuel(fuelNeeded)
  47. end
  48. end
  49.  
  50. local function moveForward()
  51. checkFuel()
  52. if not bedrockDetected then
  53. local b, data = turtle.inspectDown()
  54. if b then
  55. if data.name == "Bedrock" then bedrockDetected = true end
  56. end
  57. end
  58.  
  59. while turtle.detect() do turtle.dig() end
  60. turtle.forward()
  61.  
  62. if pos.f == 0 then pos.x = pos.x + 1 end
  63. if pos.f == 2 then pos.x = pos.x - 1 end
  64. if pos.f == 1 then pos.y = pos.y + 1 end
  65. if pos.f == 3 then pos.y = pos.y - 1 end
  66.  
  67. step = step + 1
  68. end
  69.  
  70. local function moveUp()
  71. while turtle.detectUp() do turtle.digUp() end
  72. turtle.up()
  73. pos.z = pos.z + 1
  74. end
  75.  
  76. local function moveDown()
  77. while turtle.detectDown() do turtle.digDown() end
  78. turtle.down()
  79. pos.z = pos.z - 1
  80. end
  81.  
  82. local function turnLeft()
  83. turtle.turnLeft()
  84. pos.f = (pos.f - 1) % 4
  85. end
  86.  
  87. local function turnRight()
  88. turtle.turnRight()
  89. pos.f = (pos.f + 1) % 4
  90. end
  91.  
  92. local function checkInventory()
  93. local a = 0
  94. for i = 1, 16 do
  95. turtle.select(i)
  96. if turtle.getItemCount() > 0 then a = a + 1 end
  97. end
  98. if a == 16 then
  99. state = 1
  100. lastData.x = pos.x
  101. lastData.y = pos.y
  102. lastData.z = pos.z
  103. lastData.f = pos.f
  104. lastData.step = step
  105. lastData.dir = dir
  106. end
  107. end
  108.  
  109. local function mine()
  110. if step == maxSteps then
  111. checkInventory()
  112.  
  113. if bedrockDetected then
  114. state = 1
  115. end
  116.  
  117. turnRight()
  118. turnRight()
  119. moveDown()
  120. step = 1
  121. dir = not dir
  122. end
  123.  
  124. if dir then
  125. if pos.x == size and pos.f == 0 then
  126. turnRight()
  127. moveForward()
  128. turnRight()
  129. elseif pos.x == 1 and pos.f == 2 then
  130. turnLeft()
  131. moveForward()
  132. turnLeft()
  133. else
  134. moveForward()
  135. end
  136. else
  137. if pos.x == size and pos.f == 0 then
  138. turnLeft()
  139. moveForward()
  140. turnLeft()
  141. elseif pos.x == 1 and pos.f == 2 then
  142. turnRight()
  143. moveForward()
  144. turnRight()
  145. else
  146. moveForward()
  147. end
  148. end
  149. end
  150.  
  151. local function goTo(x, y, z, f)
  152. if pos.z < z then
  153. while pos.z ~= z do moveUp() end
  154. elseif pos.z > z then
  155. while pos.z ~= z do moveDown() end
  156. end
  157.  
  158. if pos.y < y then
  159. while pos.f ~= 1 do turnRight() end
  160. elseif pos.y > y then
  161. while pos.f ~= 3 do turnRight() end
  162. end
  163. while pos.y ~= y do moveForward() end
  164.  
  165. if pos.x < x then
  166. while pos.f ~= 0 do turnRight() end
  167. elseif pos.x > x then
  168. while pos.f ~= 2 do turnRight() end
  169. end
  170. while pos.x ~= x do moveForward() end
  171.  
  172. while pos.f ~= f do turnRight() end
  173. end
  174.  
  175. local function dropAll()
  176. for i = 1, 16 do
  177. turtle.select(i)
  178. turtle.drop()
  179. end
  180. turtle.select(1)
  181. end
  182.  
  183. -- Main Code
  184. tryToRefuel(10)
  185.  
  186. print("Quarry size: "..size)
  187.  
  188. -- Main Loop
  189. while true do
  190. if state == 0 then
  191. mine()
  192. elseif state == 1 then
  193. goTo(1, 1, 0, 2)
  194. if not bedrockDetected then
  195. tryToRefuel(math.abs(lastData.z) * 2 + size * 4)
  196. end
  197. dropAll()
  198. if bedrockDetected then break end
  199. goTo(lastData.x, lastData.y, lastData.z, lastData.f)
  200. step = lastData.step
  201. dir = lastData.dir
  202. state = 0
  203. end
  204. end
  205.  
  206. print("Quarry completed!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement