Advertisement
MTM123

Untitled

Sep 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 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. turtle.turnLeft()
  129. dx = dx + 1
  130. elseif dx == -3 then
  131. turtle.turnRight()
  132. elseif dx == 3 then
  133. turtle.turnLeft()
  134. else
  135. turtle.turnRight()
  136. dx = dx - 1
  137. end
  138. end
  139.  
  140. dir = d
  141.  
  142. end
  143.  
  144. local function navigateTo(ax, ay, az)
  145. while y ~= ay do
  146. if y > ay then
  147. moveDown()
  148. else
  149. moveUp()
  150. end
  151. end
  152.  
  153. while x ~= ax do
  154. if x > ax then
  155. turn(1)
  156. move()
  157. else
  158. turn(3)
  159. move()
  160. end
  161. end
  162.  
  163. while z ~= az do
  164. if z > az then
  165. turn(2)
  166. move()
  167. else
  168. turn(0)
  169. move()
  170. end
  171. end
  172. end
  173.  
  174. local function emptyInv()
  175. local lx, ly, lz, ld = x, y, z, dir
  176.  
  177. local hw = math.floor(w/2)
  178.  
  179. print("x: %d, y:%d, z:%d", x, y, z)
  180.  
  181. navigateTo(hw, 0, -1)
  182.  
  183. for n=2,16 do
  184. turtle.select(n)
  185. turtle.dropDown()
  186. end
  187.  
  188. turtle.select(1)
  189.  
  190. turn(0)
  191. move()
  192.  
  193. navigateTo(lx, ly, lz)
  194.  
  195. turn(ld)
  196. end
  197.  
  198. local function invHasSpace()
  199. return turtle.getItemCount(16) == 0
  200. end
  201.  
  202.  
  203.  
  204. if w == nil or h == nil or d == nil then
  205. print("Usage: quarry <width> <height> <depth>")
  206. return
  207. end
  208.  
  209. w = tonumber(w)
  210. h = tonumber(h)
  211. d = tonumber(d)
  212.  
  213. assert(w ~= nil, "Width must be a number")
  214. assert(h ~= nil, "Height must be a number")
  215. assert(d ~= nil, "Depth must be a number")
  216.  
  217. if w <= 0 or h <= 0 or d <= 0 then
  218. print("All dimension values must be positive!")
  219. return
  220. end
  221.  
  222. w = math.floor(w)
  223. h = math.floor(h)
  224. d = math.floor(d)
  225.  
  226. print(string.format("Starting to mine in %dx%dx%d area", w, h, d))
  227.  
  228. local hw = math.floor(w/2)
  229.  
  230. turn(1)
  231.  
  232. while hw > 0 do
  233. tryDig()
  234. turtle.forward()
  235. hw = hw - 1
  236. end
  237.  
  238. turn(0)
  239.  
  240. local n = true
  241. local wdir = 1
  242. local hdir = 1
  243.  
  244. local wi,hi,di = 0, 1, 0
  245.  
  246. local wk = w
  247. local hk = h
  248.  
  249. while di ~= d do
  250.  
  251. while wi ~= wk do
  252.  
  253. while hi ~= hk do
  254.  
  255. if not invHasSpace() then
  256. emptyInv()
  257. end
  258.  
  259. tryDig()
  260. move()
  261.  
  262. hi = hi + hdir
  263.  
  264. end
  265.  
  266. if math.fmod(wi + wdir, w) ~= 0 then
  267. if n then
  268. turn(3)
  269. else
  270. turn(1)
  271. end
  272. tryDig()
  273. move()
  274. end
  275.  
  276. if hk == h then
  277. hk = 1
  278. turn(2)
  279. else
  280. hk = h
  281. turn(0)
  282. end
  283.  
  284. hdir = -hdir
  285.  
  286. wi = wi + wdir
  287. end
  288.  
  289. if wk == w then
  290. wk = 0
  291. else
  292. wk = w
  293. end
  294. wdir = -wdir
  295.  
  296. di = di + 1
  297. n = not n
  298.  
  299. if di ~= d then
  300. tryDigDown()
  301. moveDown()
  302. end
  303. end
  304.  
  305. print("Task finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement