svdragster

Untitled

Aug 10th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.15 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 vertical column at current position
  179. local function digVerticalColumn(depth)
  180.   print("Digging vertical column to depth " .. depth)
  181.  
  182.   -- Dig current position
  183.   manageInventory()
  184.   turtle.digUp()
  185.   turtle.digDown()
  186.  
  187.   -- Dig down to specified depth
  188.   for level = 1, depth do
  189.     if not moveDownAndDig() then
  190.       print("Failed to dig down at level " .. level)
  191.       -- Try to return to surface
  192.       for i = 1, level - 1 do
  193.         moveUpAndDig()
  194.       end
  195.       return false
  196.     end
  197.    
  198.     -- Dig up and down at this level
  199.     manageInventory()
  200.     turtle.digUp()
  201.     turtle.digDown()
  202.   end
  203.  
  204.   -- Return to surface
  205.   for i = 1, depth do
  206.     if not moveUpAndDig() then
  207.       print("ERROR: Failed to return to surface from level " .. i)
  208.       return false
  209.     end
  210.   end
  211.  
  212.   return true
  213. end
  214.  
  215. -- Function to move to next position in grid (snake pattern)
  216. local function moveToNextPosition(currentX, currentY, width, length, direction)
  217.   if direction == "right" then
  218.     if currentY < length then
  219.       -- Move forward (right)
  220.       if not moveForwardAndDig() then
  221.         return false, currentX, currentY, direction
  222.       end
  223.       return true, currentX, currentY + 1, direction
  224.     else
  225.       -- At end of row, move down and change direction
  226.       if currentX < width then
  227.         turtle.turnRight()
  228.         if not moveForwardAndDig() then
  229.           return false, currentX, currentY, direction
  230.         end
  231.         turtle.turnRight()
  232.         return true, currentX + 1, currentY, "left"
  233.       else
  234.         -- Finished all positions
  235.         return false, currentX, currentY, direction
  236.       end
  237.     end
  238.   else -- direction == "left"
  239.     if currentY > 1 then
  240.       -- Move forward (left)
  241.       if not moveForwardAndDig() then
  242.         return false, currentX, currentY, direction
  243.       end
  244.       return true, currentX, currentY - 1, direction
  245.     else
  246.       -- At end of row, move down and change direction
  247.       if currentX < width then
  248.         turtle.turnLeft()
  249.         if not moveForwardAndDig() then
  250.           return false, currentX, currentY, direction
  251.         end
  252.         turtle.turnLeft()
  253.         return true, currentX + 1, currentY, "right"
  254.       else
  255.         -- Finished all positions
  256.         return false, currentX, currentY, direction
  257.       end
  258.     end
  259.   end
  260. end
  261.  
  262. -- =================================================================
  263. -- Main Program Logic
  264. -- =================================================================
  265.  
  266. local args = { ... }
  267. if #args ~= 3 then
  268.   print("Usage: down <width> <length> <depth>")
  269.   print("Example: down 10 20 5")
  270.   return
  271. end
  272.  
  273. local width = tonumber(args[1])
  274. local length = tonumber(args[2])
  275. local depth = tonumber(args[3])
  276.  
  277. if not width or not length or not depth or width <= 0 or length <= 0 or depth <= 0 then
  278.   print("Error: Width, length, and depth must be positive numbers.")
  279.   return
  280. end
  281.  
  282. print("Starting 3D mining operation: " .. width .. "x" .. length .. "x" .. depth)
  283. print("Whitelist is active.")
  284. os.sleep(2)
  285.  
  286. -- Main loop for vertical mining
  287. local totalPositions = width * length
  288. local currentPosition = 1
  289. local currentX = 1
  290. local currentY = 1
  291. local direction = "right"
  292.  
  293. print("Starting vertical mining: " .. totalPositions .. " positions total")
  294.  
  295. -- Mine the first position
  296. if not digVerticalColumn(depth) then
  297.   print("Mining failed at first position!")
  298.   return
  299. end
  300.  
  301. -- Mine remaining positions
  302. while currentPosition < totalPositions do
  303.   local success, newX, newY, newDirection = moveToNextPosition(currentX, currentY, width, length, direction)
  304.  
  305.   if not success then
  306.     if newX == currentX and newY == currentY then
  307.       print("Movement failed at position (" .. currentX .. ", " .. currentY .. ")")
  308.       return
  309.     else
  310.       print("Finished mining all positions!")
  311.       break
  312.     end
  313.   end
  314.  
  315.   currentX = newX
  316.   currentY = newY
  317.   direction = newDirection
  318.   currentPosition = currentPosition + 1
  319.  
  320.   print("Mining position " .. currentPosition .. "/" .. totalPositions .. " at (" .. currentX .. ", " .. currentY .. ")")
  321.  
  322.   if not digVerticalColumn(depth) then
  323.     print("Mining failed at position (" .. currentX .. ", " .. currentY .. ")")
  324.     return
  325.   end
  326. end
  327.  
  328. -- Final inventory drop
  329. manageInventory()
  330.  
  331. print("3D mining operation complete!")
  332.  
Add Comment
Please, Sign In to add comment