svdragster

Untitled

Aug 10th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.54 KB | None | 0 0
  1. --[[
  2.   3D Rectangular Mining Program for ComputerCraft Turtles (with Whitelist)
  3.  
  4.   Description:
  5.   This program instructs a mining turtle to dig a rectangular area down to a specified depth.
  6.   It includes a whitelist to keep valuable items and automatically retains fuel.
  7.   All other items are deposited into a chest.
  8.  
  9.   How to Use:
  10.   1. Configure the 'itemWhitelist' table below with the item IDs you want to keep.
  11.      You can find item IDs in-game (often by pressing F3+H).
  12.  
  13.   2. Place this file on a mining turtle (e.g., 'down.lua').
  14.  
  15.   3. Place a chest directly behind the turtle's starting position. This is where
  16.     it will deposit all non-whitelisted items.
  17.  
  18.  4. Make sure the turtle has some fuel. It will automatically find and use it
  19.     from its inventory, and it will never drop fuel items.
  20.  
  21.  5. Run the program with three arguments: width, length, and depth.
  22.     Example: down 10 20 5
  23. ]]
  24.  
  25. -- =================================================================
  26. -- Configuration
  27. -- =================================================================
  28.  
  29. -- Add the string IDs of items you want the turtle to KEEP.
  30. -- The turtle will automatically keep any fuel source, so you don't need to add coal.
  31. -- Example Format: ["mod_name:item_name"] = true,
  32. local itemWhitelist = {
  33.   ["minecraft:diamond"] = true,
  34.   ["minecraft:emerald"] = true,
  35.   ["minecraft:raw_gold"] = true,
  36.   ["minecraft:raw_iron"] = true,
  37.   ["minecraft:lapis_lazuli"] = true,
  38.   ["minecraft:redstone"] = true,
  39.  
  40.   -- Example for a modded ore from AllTheMods
  41.   ["allthemodium:raw_allthemodium"] = true,
  42.   ["allthemodium:raw_vibranium"] = true,
  43.   ["allthemodium:raw_unobtainium"] = true,
  44.   ["alltheores:raw_tin"] = true,
  45.   ["alltheores:raw_lead"] = true,
  46. }
  47.  
  48. -- =================================================================
  49. -- Helper Functions
  50. -- =================================================================
  51.  
  52. -- Function to check fuel and refuel if necessary
  53. local function ensureFuel()
  54.   if turtle.getFuelLevel() < 2 then
  55.     print("Low on fuel. Attempting to refuel...")
  56.     for i = 1, 16 do
  57.       turtle.select(i)
  58.       if turtle.refuel(1) then
  59.         print("Refueled successfully.")
  60.         turtle.select(1) -- Select the first slot again for digging
  61.         return true
  62.       end
  63.     end
  64.     print("ERROR: Out of fuel! Please add fuel to my inventory.")
  65.     return false
  66.   end
  67.   return true
  68. end
  69.  
  70. -- Function to check if inventory is full and empty non-whitelisted items.
  71. local function manageInventory()
  72.   local isFull = true
  73.   for i = 1, 15 do -- Check first 15 slots
  74.     if turtle.getItemCount(i) == 0 then
  75.       isFull = false
  76.       break
  77.     end
  78.   end
  79.  
  80.   if isFull then
  81.     print("Inventory full. Dropping non-whitelisted items.")
  82.     -- Turn around to face the chest
  83.     turtle.turnLeft()
  84.     turtle.turnLeft()
  85.  
  86.     -- Iterate through inventory and drop unwanted items
  87.     for i = 1, 16 do
  88.       turtle.select(i)
  89.       local itemDetail = turtle.getItemDetail(i)
  90.  
  91.       if itemDetail then
  92.         local itemName = itemDetail.name
  93.         -- turtle.refuel(0) checks if the item is fuel without consuming it
  94.         local isFuel = turtle.refuel(0)
  95.         local isWhitelisted = itemWhitelist[itemName]
  96.  
  97.         -- If the item is NOT fuel and is NOT on the whitelist, drop it.
  98.         if not isFuel and not isWhitelisted then
  99.           turtle.drop()
  100.         end
  101.       end
  102.     end
  103.  
  104.     turtle.select(1) -- Reselect the first slot
  105.     -- Turn back to the original direction
  106.     turtle.turnLeft()
  107.     turtle.turnLeft()
  108.     print("Inventory managed. Resuming mining.")
  109.   end
  110. end
  111.  
  112. -- A robust function for moving forward.
  113. local function moveForwardAndDig()
  114.   if not ensureFuel() then
  115.     return false
  116.   end
  117.  
  118.   while turtle.detect() do
  119.     if not turtle.dig() then
  120.       print("Waiting for obstruction to clear...")
  121.       os.sleep(1)
  122.     end
  123.   end
  124.  
  125.   while not turtle.forward() do
  126.     print("Movement blocked. Re-digging...")
  127.     turtle.dig()
  128.     os.sleep(0.5)
  129.   end
  130.  
  131.   return true
  132. end
  133.  
  134. -- A robust function for moving down.
  135. local function moveDownAndDig()
  136.   if not ensureFuel() then
  137.     return false
  138.   end
  139.  
  140.   while turtle.detectDown() do
  141.     if not turtle.digDown() then
  142.       print("Waiting for obstruction below to clear...")
  143.       os.sleep(1)
  144.     end
  145.   end
  146.  
  147.   while not turtle.down() do
  148.     print("Downward movement blocked. Re-digging...")
  149.     turtle.digDown()
  150.     os.sleep(0.5)
  151.   end
  152.  
  153.   return true
  154. end
  155.  
  156. -- A robust function for moving up.
  157. local function moveUpAndDig()
  158.   if not ensureFuel() then
  159.     return false
  160.   end
  161.  
  162.   while turtle.detectUp() do
  163.     if not turtle.digUp() then
  164.       print("Waiting for obstruction above to clear...")
  165.       os.sleep(1)
  166.     end
  167.   end
  168.  
  169.   while not turtle.up() do
  170.     print("Upward movement blocked. Re-digging...")
  171.     turtle.digUp()
  172.     os.sleep(0.5)
  173.   end
  174.  
  175.   return true
  176. end
  177.  
  178. -- Function to dig a single row of a given length at current level
  179. local function digRow(length)
  180.   for i = 1, length - 1 do
  181.     manageInventory()
  182.  
  183.     turtle.digUp()
  184.     turtle.digDown()
  185.  
  186.     if not moveForwardAndDig() then
  187.       return false
  188.     end
  189.   end
  190.   manageInventory()
  191.   turtle.digUp()
  192.   turtle.digDown()
  193.   return true
  194. end
  195.  
  196. -- Function to turn the turtle right
  197. local function turnRight()
  198.   ensureFuel()
  199.   turtle.turnRight()
  200. end
  201.  
  202. -- Function to turn the turtle left
  203. local function turnLeft()
  204.   ensureFuel()
  205.   turtle.turnLeft()
  206. end
  207.  
  208. -- Function to return to surface from any depth
  209. local function returnToSurface(currentDepth)
  210.   print("Returning to surface from depth " .. currentDepth)
  211.   for i = 1, currentDepth do
  212.     if not moveUpAndDig() then
  213.       print("ERROR: Failed to return to surface!")
  214.       return false
  215.     end
  216.   end
  217.   print("Reached surface.")
  218.   return true
  219. end
  220.  
  221. -- Function to dig a complete level (width x length rectangle)
  222. local function digLevel(width, length)
  223.   -- Main loop for digging rows at this level
  224.   for i = 1, width do
  225.     if not digRow(length) then
  226.       print("Halting program due to movement failure.")
  227.       return false
  228.     end
  229.  
  230.     if i < width then
  231.       if i % 2 ~= 0 then
  232.         turnRight()
  233.         moveForwardAndDig()
  234.         turnRight()
  235.       else
  236.         turnLeft()
  237.         moveForwardAndDig()
  238.         turnLeft()
  239.       end
  240.     end
  241.   end
  242.   return true
  243. end
  244.  
  245. -- Function to return to start position of current level
  246. local function returnToLevelStart(width, length)
  247.   -- If we're at an odd row, we need to go back to start
  248.  if width % 2 ~= 0 then
  249.    -- We're facing the same direction as we started
  250.     -- Turtle is at (width-1, length-1) facing original direction
  251.     turnLeft()
  252.     turnLeft()
  253.     for i = 1, length - 1 do
  254.       moveForwardAndDig()
  255.     end
  256.     turnLeft()
  257.     for i = 1, width - 1 do
  258.       moveForwardAndDig()
  259.     end
  260.     turnLeft()
  261.   else
  262.     -- We're facing opposite to start direction
  263.    -- Turtle is at (width-1, 0) facing opposite to start direction
  264.    -- Just need to move back to (0, 0) and turn around
  265.    for i = 1, width - 1 do
  266.      moveForwardAndDig()
  267.    end
  268.    -- Now at (0, 0) facing opposite direction, turn around
  269.    turnLeft()
  270.    turnLeft()
  271.  end
  272. end
  273.  
  274. -- =================================================================
  275. -- Main Program Logic
  276. -- =================================================================
  277.  
  278. local args = { ... }
  279. if #args ~= 3 then
  280.  print("Usage: down <width> <length> <depth>")
  281.  print("Example: down 10 20 5")
  282.  return
  283. end
  284.  
  285. local width = tonumber(args[1])
  286. local length = tonumber(args[2])
  287. local depth = tonumber(args[3])
  288.  
  289. if not width or not length or not depth or width <= 0 or length <= 0 or depth <= 0 then
  290.  print("Error: Width, length, and depth must be positive numbers.")
  291.  return
  292. end
  293.  
  294. print("Starting 3D mining operation: " .. width .. "x" .. length .. "x" .. depth)
  295. print("Whitelist is active.")
  296. os.sleep(2)
  297.  
  298. -- Main loop for digging levels
  299. for level = 1, depth do
  300.  print("Mining level " .. level .. "/" .. depth .. " (depth " .. (level - 1) .. ")")
  301.  
  302.  if not digLevel(width, length) then
  303.    print("Mining failed at level " .. level)
  304.    returnToSurface(level - 1)
  305.    return
  306.  end
  307.  
  308.  -- Don't move down after the last level
  309.   if level < depth then
  310.     returnToLevelStart(width, length)
  311.     if not moveDownAndDig() then
  312.       print("Failed to move to next level!")
  313.       returnToSurface(level - 1)
  314.       return
  315.     end
  316.   end
  317. end
  318.  
  319. -- Return to surface
  320. returnToLevelStart(width, length)
  321. returnToSurface(depth)
  322.  
  323. -- Final inventory drop
  324. manageInventory()
  325.  
  326. print("3D mining operation complete!")
  327.  
Add Comment
Please, Sign In to add comment