Advertisement
MTM123

quarry

Sep 16th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. local w,h,d= ...
  2. -- n -> 0, w -> 1, s -> 2, e -> 3
  3. local dir = 0
  4. local x,y,z = 0, 0, 0
  5.  
  6. local function oppositeDir(d)
  7. if d == 0 then return 2
  8. elseif d == 1 then return 3
  9. elseif d == 2 then return 0
  10. elseif d == 3 then return 1
  11. else return d end
  12. end
  13.  
  14. local function tryDig()
  15. while turtle.detect() do
  16. if not turtle.dig() then return false end
  17. end
  18.  
  19. return true
  20. end
  21.  
  22. local function tryDigDown()
  23. while turtle.detectDown() do
  24. if not turtle.digDown() then return false end
  25. end
  26.  
  27. return true
  28. end
  29.  
  30. local function tryDigUp()
  31. while turtle.detectUp() do
  32. if not turtle.digUp() then return false end
  33. end
  34.  
  35. return true
  36. end
  37.  
  38. local function refuel()
  39. local fuelLevel = turtle.getFuelLevel()
  40. if fuelLevel == "unlimited" or fuelLevel > 0 then
  41. return
  42. end
  43.  
  44. local function tryRefuel()
  45. for n=1,16 do
  46. if turtle.getItemCount(n) > 0 then
  47. turtle.select(n)
  48. if turtle.refuel(n) then
  49. turtle.select(n)
  50. return true
  51. end
  52. end
  53. end
  54. turtle.select(1)
  55. return false
  56. end
  57.  
  58. if not tryRefuel() then
  59. print("Out of fuel. Please add more fuel!")
  60. while not tryRefuel() do
  61. os.pullEvent("turtle_inventory")
  62. end
  63. print("Resuming...")
  64. end
  65. end
  66.  
  67. local function move()
  68. refuel()
  69.  
  70. while not turtle.forward() do
  71. if turtle.detect() then
  72. if not tryDig() then return false end
  73. elseif turtle.attack() then
  74. print("Something in the way. Attacking...")
  75. else sleep(0.5) end
  76. end
  77.  
  78. if dir == 0 then z = z + 1
  79. elseif dir == 1 then x = x - 1
  80. elseif dir == 2 then z = z - 1
  81. elseif dir == 3 then x = x + 1
  82. else return false end
  83.  
  84. return true
  85. end
  86.  
  87. local function moveUp()
  88. refuel()
  89.  
  90. while not turtle.up() do
  91. if turtle.detectUp() then
  92. if not tryDigUp() then return false end
  93. elseif turtle.attackUp() then
  94. print("Something in the way. Attacking...")
  95. else sleep(0.5) end
  96. end
  97.  
  98. y = y + 1
  99.  
  100. return true
  101.  
  102. end
  103.  
  104. local function moveDown()
  105. refuel()
  106.  
  107. while not turtle.down() do
  108. if turtle.detectDown() then
  109. if not tryDigDown() then return false end
  110. elseif turtle.attackDown() then
  111. print("Something in the way. Attacking...")
  112. else sleep(0.5) end
  113. end
  114.  
  115. y = y - 1
  116.  
  117. return true
  118. end
  119.  
  120. local function turn(d)
  121.  
  122. if dir == d then return end
  123.  
  124. local dx = dir - d
  125.  
  126. while dx ~= 0 do
  127. if dx < 0 then
  128. if dx == -3 then
  129. turtle.turnRight()
  130. dx = 0
  131. else
  132. turtle.turnLeft()
  133. dx = dx + 1
  134. end
  135. elseif dx == 3 then
  136.  
  137. else
  138. if dx == 3 then
  139. turtle.turnLeft()
  140. dx = 0
  141. else
  142. turtle.turnRight()
  143. dx = dx - 1
  144. end
  145. end
  146. end
  147.  
  148. dir = d
  149.  
  150. end
  151.  
  152. local function navigateTo(ax, ay, az)
  153. while y ~= ay do
  154. if y > ay then
  155. moveDown()
  156. else
  157. moveUp()
  158. end
  159. end
  160.  
  161. while x ~= ax do
  162. if x > ax then
  163. turn(1)
  164. move()
  165. else
  166. turn(3)
  167. move()
  168. end
  169. end
  170.  
  171. while z ~= az do
  172. if z > az then
  173. turn(2)
  174. move()
  175. else
  176. turn(0)
  177. move()
  178. end
  179. end
  180. end
  181.  
  182. local function emptyInv()
  183. local lx, ly, lz, ld = x, y, z, dir
  184.  
  185. local hw = math.floor(w/2)
  186.  
  187. print("x: %d, y:%d, z:%d", x, y, z)
  188.  
  189. navigateTo(hw, 0, -1)
  190.  
  191. for n=2,16 do
  192. turtle.select(n)
  193. turtle.dropDown()
  194. end
  195.  
  196. turtle.select(1)
  197.  
  198. turn(0)
  199. move()
  200.  
  201. navigateTo(lx, ly, lz)
  202.  
  203. turn(ld)
  204. end
  205.  
  206. local function invHasSpace()
  207. return turtle.getItemCount(16) == 0
  208. end
  209.  
  210.  
  211.  
  212. if w == nil or h == nil or d == nil then
  213. print("Usage: quarry <width> <height> <depth>")
  214. return
  215. end
  216.  
  217. w = tonumber(w)
  218. h = tonumber(h)
  219. d = tonumber(d)
  220.  
  221. assert(w ~= nil, "Width must be a number")
  222. assert(h ~= nil, "Height must be a number")
  223. assert(d ~= nil, "Depth must be a number")
  224.  
  225. if w <= 0 or h <= 0 or d <= 0 then
  226. print("All dimension values must be positive!")
  227. return
  228. end
  229.  
  230. w = math.floor(w)
  231. h = math.floor(h)
  232. d = math.floor(d)
  233.  
  234. print(string.format("Starting to mine in %dx%dx%d area", w, h, d))
  235.  
  236. local hw = math.floor(w/2)
  237.  
  238. turn(1)
  239.  
  240. while hw > 0 do
  241. tryDig()
  242. turtle.forward()
  243. hw = hw - 1
  244. end
  245.  
  246. turn(0)
  247.  
  248. local n = true
  249. local wdir = 1
  250. local hdir = 1
  251.  
  252. local wi,hi,di = 0, 1, 0
  253.  
  254. local wk = w
  255. local hk = h
  256.  
  257. while di ~= d do
  258.  
  259. while wi ~= wk do
  260.  
  261. while hi ~= hk do
  262.  
  263. if not invHasSpace() then
  264. emptyInv()
  265. end
  266.  
  267. tryDig()
  268. move()
  269.  
  270. hi = hi + hdir
  271.  
  272. end
  273.  
  274. if math.fmod(wi + wdir, w) ~= 0 then
  275. if n then
  276. turn(3)
  277. else
  278. turn(1)
  279. end
  280. tryDig()
  281. move()
  282. end
  283.  
  284. if hk == h then
  285. hk = 1
  286. turn(2)
  287. else
  288. hk = h
  289. turn(0)
  290. end
  291.  
  292. hdir = -hdir
  293.  
  294. wi = wi + wdir
  295. end
  296.  
  297. if wk == w then
  298. wk = 0
  299. else
  300. wk = w
  301. end
  302. wdir = -wdir
  303.  
  304. di = di + 1
  305. n = not n
  306.  
  307. if di ~= d then
  308. tryDigDown()
  309. moveDown()
  310. end
  311. end
  312.  
  313. print("Task finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement