Advertisement
Paxon57

Untitled

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