Advertisement
MTM123

Untitled

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