Advertisement
Paxon57

Untitled

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