Advertisement
MTM123

Untitled

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