9FastCutMan

Quarry2 CC Restitched

Aug 12th, 2025 (edited)
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. --[[
  2. ComputerCraft Mining Turtle Quarry Program
  3. Final Working Version - All Functions in Correct Order
  4. ]]
  5.  
  6. -- CONFIG --
  7. local TRASH = {
  8. ["minecraft:cobblestone"] = true,
  9. ["minecraft:dirt"] = true,
  10. ["minecraft:gravel"] = true,
  11. ["minecraft:stone"] = true,
  12. ["minecraft:sand"] = true,
  13. ["minecraft:netherrack"] = true
  14. }
  15.  
  16. local INVENTORY_THRESHOLD = 15
  17. local MIN_FUEL_LEVEL = 500
  18. local PREFERRED_FUEL_ITEMS = {
  19. ["minecraft:coal"] = true,
  20. ["minecraft:coal_block"] = true,
  21. ["minecraft:lava_bucket"] = true
  22. }
  23.  
  24. -- State Management
  25. local STATE_FILE = "quarry_state.dat"
  26. local width, length, depth
  27. local posX, posY, posZ = 0, 0, 0
  28. local dir = 0 -- 0=north,1=east,2=south,3=west
  29. local startX, startY, surfaceZ = 0, 0, 0
  30. local currentLayer = 1
  31. local goingForward = true
  32. local currentRow = 1
  33. local currentCol = 1
  34.  
  35. -- Yielding Helper
  36. local function yield()
  37. os.queueEvent("yield")
  38. os.pullEvent("yield")
  39. end
  40.  
  41. -- Movement Functions (MUST COME FIRST since others depend on them)
  42. local function right()
  43. turtle.turnRight()
  44. dir = (dir + 1) % 4
  45. return true
  46. end
  47.  
  48. local function left()
  49. turtle.turnLeft()
  50. dir = (dir - 1) % 4
  51. if dir < 0 then dir = dir + 4 end
  52. return true
  53. end
  54.  
  55. local function turnTo(targetDir)
  56. local diff = (targetDir - dir) % 4
  57. if diff == 3 then
  58. left()
  59. else
  60. for i = 1, diff do
  61. right()
  62. end
  63. end
  64. end
  65.  
  66. local function tryDig(side)
  67. local attempts = 0
  68. while true do
  69. attempts = attempts + 1
  70. if attempts % 5 == 0 then yield() end
  71.  
  72. local success, reason
  73. if side == "forward" then
  74. if not turtle.detect() then return true end
  75. success, reason = turtle.dig()
  76. elseif side == "up" then
  77. if not turtle.detectUp() then return true end
  78. success, reason = turtle.digUp()
  79. elseif side == "down" then
  80. if not turtle.detectDown() then return true end
  81. success, reason = turtle.digDown()
  82. end
  83.  
  84. if not success then return false, reason end
  85. sleep(0.2)
  86. end
  87. end
  88.  
  89. local function forward()
  90. local attempts = 0
  91. while not turtle.forward() do
  92. attempts = attempts + 1
  93. if attempts % 3 == 0 then yield() end
  94.  
  95. local success, reason = tryDig("forward")
  96. if not success then return false, reason end
  97. sleep(0.2)
  98. end
  99.  
  100. if dir == 0 then posY = posY + 1
  101. elseif dir == 1 then posX = posX + 1
  102. elseif dir == 2 then posY = posY - 1
  103. elseif dir == 3 then posX = posX - 1 end
  104. return true
  105. end
  106.  
  107. local function up()
  108. local attempts = 0
  109. while not turtle.up() do
  110. attempts = attempts + 1
  111. if attempts % 3 == 0 then yield() end
  112.  
  113. local success, reason = tryDig("up")
  114. if not success then return false, reason end
  115. sleep(0.2)
  116. end
  117.  
  118. posZ = posZ - 1
  119. return true
  120. end
  121.  
  122. local function down()
  123. local attempts = 0
  124. while not turtle.down() do
  125. attempts = attempts + 1
  126. if attempts % 3 == 0 then yield() end
  127.  
  128. local success, reason = tryDig("down")
  129. if not success then return false, reason end
  130. sleep(0.2)
  131. end
  132.  
  133. posZ = posZ + 1
  134. return true
  135. end
  136.  
  137. local function moveForwardN(n)
  138. for i = 1, n do
  139. if not forward() then return false end
  140. end
  141. return true
  142. end
  143.  
  144. -- Inventory Management
  145. local function compactInventory()
  146. for i = 1, 15 do
  147. local detail_i = turtle.getItemDetail(i)
  148. if detail_i then
  149. for j = i + 1, 16 do
  150. local detail_j = turtle.getItemDetail(j)
  151. if detail_j and detail_j.name == detail_i.name then
  152. turtle.select(j)
  153. turtle.transferTo(i)
  154. end
  155. end
  156. end
  157. end
  158. turtle.select(1)
  159. end
  160.  
  161. local function shouldDumpTrash()
  162. local usedSlots = 0
  163. for i = 1, 16 do
  164. if turtle.getItemDetail(i) then usedSlots = usedSlots + 1 end
  165. if usedSlots >= INVENTORY_THRESHOLD then return true end
  166. end
  167. return false
  168. end
  169.  
  170. local function dumpTrashNow()
  171. print("Disposing trash on ground...")
  172. local originalDir = dir
  173. turnTo(0) -- Face north to throw trash
  174.  
  175. local trashFound = false
  176. for slot = 1, 16 do
  177. local detail = turtle.getItemDetail(slot)
  178. if detail and TRASH[detail.name] then
  179. turtle.select(slot)
  180. turtle.drop()
  181. trashFound = true
  182. end
  183. end
  184.  
  185. turnTo(originalDir) -- Return to original facing
  186. turtle.select(1)
  187. return trashFound
  188. end
  189.  
  190. local function depositItems()
  191. compactInventory()
  192. print("Depositing valuables in chest")
  193. for slot = 1, 16 do
  194. local detail = turtle.getItemDetail(slot)
  195. if detail and not TRASH[detail.name] then
  196. turtle.select(slot)
  197. turtle.drop()
  198. end
  199. end
  200. turtle.select(1)
  201. end
  202.  
  203. -- Navigation
  204. local function returnToStartHorizontal()
  205. if posX > startX then
  206. turnTo(3)
  207. if not moveForwardN(posX - startX) then return false end
  208. elseif posX < startX then
  209. turnTo(1)
  210. if not moveForwardN(startX - posX) then return false end
  211. end
  212.  
  213. if posY > startY then
  214. turnTo(2)
  215. if not moveForwardN(posY - startY) then return false end
  216. elseif posY < startY then
  217. turnTo(0)
  218. if not moveForwardN(startY - posY) then return false end
  219. end
  220.  
  221. turnTo(2)
  222. return true
  223. end
  224.  
  225. local function returnToChestSurface()
  226. if not returnToStartHorizontal() then return false end
  227. while posZ > surfaceZ do
  228. if not up() then return false end
  229. end
  230. while posZ < surfaceZ do
  231. if not down() then return false end
  232. end
  233. return true
  234. end
  235.  
  236. -- Fuel System
  237. local FUEL_CHECK_ENABLED = nil
  238.  
  239. local function checkAndRefuel()
  240. if not FUEL_CHECK_ENABLED then return true end
  241. if turtle.getFuelLevel() > MIN_FUEL_LEVEL then return true end
  242.  
  243. print("Low fuel! Attempting to refuel...")
  244. for slot = 1, 16 do
  245. local item = turtle.getItemDetail(slot)
  246. if item and PREFERRED_FUEL_ITEMS[item.name] then
  247. turtle.select(slot)
  248. if turtle.refuel(1) then
  249. print("Refueled with", item.name, "| New:", turtle.getFuelLevel())
  250. turtle.select(1)
  251. return true
  252. end
  253. end
  254. end
  255. print("No fuel found!")
  256. return false
  257. end
  258.  
  259. local function initializeFuelSystem()
  260. local initialFuel = turtle.getFuelLevel()
  261. if initialFuel == "unlimited" then
  262. FUEL_CHECK_ENABLED = false
  263. print("Fuel checks disabled (unlimited fuel)")
  264. else
  265. FUEL_CHECK_ENABLED = true
  266. print("Fuel management enabled. Current:", initialFuel)
  267. if initialFuel < MIN_FUEL_LEVEL then checkAndRefuel() end
  268. end
  269. end
  270.  
  271. -- State Saving/Loading
  272. local function saveState()
  273. local data = {
  274. width = width, length = length, depth = depth,
  275. posX = posX, posY = posY, posZ = posZ,
  276. dir = dir, startX = startX, startY = startY, startZ = surfaceZ,
  277. currentLayer = currentLayer, goingForward = goingForward,
  278. currentRow = currentRow, currentCol = currentCol
  279. }
  280. local file = fs.open(STATE_FILE, "w")
  281. file.write(textutils.serialize(data))
  282. file.close()
  283. end
  284.  
  285. local function loadState()
  286. if fs.exists(STATE_FILE) then
  287. local file = fs.open(STATE_FILE, "r")
  288. local data = textutils.unserialize(file.readAll())
  289. file.close()
  290. if data then
  291. width, length, depth = data.width, data.length, data.depth
  292. posX, posY, posZ = data.posX, data.posY, data.posZ
  293. dir = data.dir
  294. startX, startY, surfaceZ = data.startX, data.startY, data.startZ
  295. currentLayer = data.currentLayer or 1
  296. goingForward = data.goingForward or true
  297. currentRow = data.currentRow or 1
  298. currentCol = data.currentCol or 1
  299. return true
  300. end
  301. end
  302. return false
  303. end
  304.  
  305. -- Mining Functions
  306. local function mineColumn()
  307. if not tryDig("up") then return false end
  308. if currentCol < width and not tryDig("forward") then return false end
  309. if not tryDig("down") then return false end
  310. return true
  311. end
  312.  
  313. -- Main Quarry Logic
  314. local function runQuarry()
  315. initializeFuelSystem()
  316. if not loadState() then
  317. width, length, depth = tonumber(arg[1]), tonumber(arg[2]), tonumber(arg[3])
  318. if not (width and length and depth) then
  319. print("Usage: quarry <width> <length> <depth>")
  320. return
  321. end
  322. startX, startY, surfaceZ = posX, posY, posZ
  323. saveState()
  324. end
  325.  
  326. print("Starting quarry:", width, "x", length, "x", depth)
  327. for layer = currentLayer, depth do
  328. print("Mining layer", layer, "of", depth)
  329. currentLayer = layer
  330.  
  331. turnTo(0) -- Face north at start of each layer
  332.  
  333. for row = currentRow, length do
  334. currentRow = row
  335. yield()
  336.  
  337. local rowDirection = goingForward and 0 or 2
  338. turnTo(rowDirection)
  339.  
  340. for col = currentCol, width do
  341. currentCol = col
  342. if col % 3 == 0 and shouldDumpTrash() then
  343. dumpTrashNow()
  344. end
  345.  
  346. if not mineColumn() then
  347. print("Stopping due to mining error")
  348. if returnToChestSurface() then depositItems() end
  349. return
  350. end
  351.  
  352. if col < width then
  353. if not forward() then
  354. print("Failed to move forward in mining pattern")
  355. if returnToChestSurface() then depositItems() end
  356. return
  357. end
  358. end
  359. end
  360. currentCol = 1
  361.  
  362. if row < length then
  363. if goingForward then
  364. right()
  365. if not forward() then
  366. print("Failed to move to next row (right turn)")
  367. if returnToChestSurface() then depositItems() end
  368. return
  369. end
  370. right()
  371. else
  372. left()
  373. if not forward() then
  374. print("Failed to move to next row (left turn)")
  375. if returnToChestSurface() then depositItems() end
  376. return
  377. end
  378. left()
  379. end
  380. goingForward = not goingForward
  381. end
  382. end
  383. currentRow = 1
  384. goingForward = true
  385.  
  386. print("Completed layer", layer)
  387. dumpTrashNow() -- Force trash dump before ascending
  388.  
  389. if returnToChestSurface() then
  390. depositItems()
  391.  
  392. if layer < depth then
  393. right()
  394. right()
  395. local nextLayerZ = surfaceZ + layer * 3
  396. while posZ < nextLayerZ do
  397. if not down() then
  398. print("Failed to descend to next layer")
  399. return
  400. end
  401. end
  402. end
  403. else
  404. print("Failed to return to chest!")
  405. return
  406. end
  407. end
  408.  
  409. print("Quarry complete!")
  410. fs.delete(STATE_FILE)
  411. end
  412.  
  413. -- Start the program
  414. runQuarry()
Advertisement
Add Comment
Please, Sign In to add comment