April_The_Sergal

Mining Turtle Script

Jan 29th, 2026 (edited)
110
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.12 KB | None | 0 0
  1. -- Mining Turtle - Diagnostic Version
  2. -- Shows exactly what the turtle sees at check position
  3.  
  4. --=============================================================================
  5. -- CONFIGURATION
  6. --=============================================================================
  7.  
  8. local travelDirection = "U"
  9. local miningDirection = "F"
  10. local triggerDirection = "U"  -- Direction to look for trigger
  11.  
  12. local checkOffset = {
  13.     {"U", 1},
  14. }
  15.  
  16. local strictMode = true
  17. local fuelInventoryDirection = "D"
  18. local minFuelLevel = 100
  19. local checkInterval = 5
  20. local initialWait = 5
  21.  
  22. local context = {
  23.     modName = "justdirethings",
  24.     prefix = "raw_",
  25.     suffix = "_",
  26.     itemType = "ore",
  27.     itemNames = {
  28.         "ferricore",
  29.         "blazegold",
  30.         "celestigem",
  31.         "eclipsealloy",
  32.         "coal_t1",
  33.         "coal_t2",
  34.         "coal_t3",
  35.         "coal_t4",
  36.     },
  37. }
  38.  
  39. local additionalWhitelist = {}
  40.  
  41. local fuelItems = {
  42.     ["minecraft:coal"] = true,
  43.     ["minecraft:charcoal"] = true,
  44.     ["minecraft:coal_block"] = true,
  45.     ["minecraft:oak_planks"] = true,
  46.     ["minecraft:spruce_planks"] = true,
  47.     ["minecraft:birch_planks"] = true,
  48.     ["minecraft:jungle_planks"] = true,
  49.     ["minecraft:acacia_planks"] = true,
  50.     ["minecraft:dark_oak_planks"] = true,
  51.     ["minecraft:blaze_rod"] = true,
  52.     ["minecraft:lava_bucket"] = true,
  53. }
  54.  
  55. --=============================================================================
  56. -- WHITELIST
  57. --=============================================================================
  58.  
  59. local whitelistedBlocks = {}
  60.  
  61. local function buildWhitelist()
  62.     whitelistedBlocks = {}
  63.     for _, itemName in ipairs(context.itemNames) do
  64.         local fullName = context.modName .. ":" .. context.prefix .. itemName .. context.suffix .. context.itemType
  65.         whitelistedBlocks[fullName] = true
  66.     end
  67.     for _, blockName in ipairs(additionalWhitelist) do
  68.         whitelistedBlocks[blockName] = true
  69.     end
  70. end
  71.  
  72. buildWhitelist()
  73.  
  74. --=============================================================================
  75. -- DIRECTION HELPERS
  76. --=============================================================================
  77.  
  78. local function getOppositeDirection(dir)
  79.     local opposites = {
  80.         U = "D", D = "U", F = "B", B = "F", L = "R", R = "L",
  81.     }
  82.     return opposites[dir] or "B"
  83. end
  84.  
  85. local function directionToSide(dir)
  86.     local sides = {
  87.         U = "top", D = "bottom", F = "front", B = "back", L = "left", R = "right",
  88.     }
  89.     return sides[dir] or "front"
  90. end
  91.  
  92. local function moveInDirection(dir)
  93.     if dir == "U" then return turtle.up()
  94.     elseif dir == "D" then return turtle.down()
  95.     elseif dir == "F" then return turtle.forward()
  96.     elseif dir == "B" then return turtle.back()
  97.     elseif dir == "L" then
  98.         turtle.turnLeft()
  99.         local success = turtle.forward()
  100.         turtle.turnRight()
  101.         return success
  102.     elseif dir == "R" then
  103.         turtle.turnRight()
  104.         local success = turtle.forward()
  105.         turtle.turnLeft()
  106.         return success
  107.     end
  108.     return false
  109. end
  110.  
  111. local function digInDirection(dir)
  112.     if dir == "U" then return turtle.digUp()
  113.     elseif dir == "D" then return turtle.digDown()
  114.     elseif dir == "F" then return turtle.dig()
  115.     elseif dir == "B" then
  116.         turtle.turnLeft() turtle.turnLeft()
  117.         local success = turtle.dig()
  118.         turtle.turnLeft() turtle.turnLeft()
  119.         return success
  120.     elseif dir == "L" then
  121.         turtle.turnLeft()
  122.         local success = turtle.dig()
  123.         turtle.turnRight()
  124.         return success
  125.     elseif dir == "R" then
  126.         turtle.turnRight()
  127.         local success = turtle.dig()
  128.         turtle.turnLeft()
  129.         return success
  130.     end
  131.     return false
  132. end
  133.  
  134. local function inspectInDirection(dir)
  135.     if dir == "U" then return turtle.inspectUp()
  136.     elseif dir == "D" then return turtle.inspectDown()
  137.     elseif dir == "F" then return turtle.inspect()
  138.     elseif dir == "B" then
  139.         turtle.turnLeft() turtle.turnLeft()
  140.         local success, data = turtle.inspect()
  141.         turtle.turnLeft() turtle.turnLeft()
  142.         return success, data
  143.     elseif dir == "L" then
  144.         turtle.turnLeft()
  145.         local success, data = turtle.inspect()
  146.         turtle.turnRight()
  147.         return success, data
  148.     elseif dir == "R" then
  149.         turtle.turnRight()
  150.         local success, data = turtle.inspect()
  151.         turtle.turnLeft()
  152.         return success, data
  153.     end
  154.     return false, nil
  155. end
  156.  
  157. local function suckInDirection(dir)
  158.     if dir == "U" then return turtle.suckUp()
  159.     elseif dir == "D" then return turtle.suckDown()
  160.     elseif dir == "F" then return turtle.suck()
  161.     elseif dir == "B" then
  162.         turtle.turnLeft() turtle.turnLeft()
  163.         local success = turtle.suck()
  164.         turtle.turnLeft() turtle.turnLeft()
  165.         return success
  166.     elseif dir == "L" then
  167.         turtle.turnLeft()
  168.         local success = turtle.suck()
  169.         turtle.turnRight()
  170.         return success
  171.     elseif dir == "R" then
  172.         turtle.turnRight()
  173.         local success = turtle.suck()
  174.         turtle.turnLeft()
  175.         return success
  176.     end
  177.     return false
  178. end
  179.  
  180. --=============================================================================
  181. -- OFFSET MOVEMENT
  182. --=============================================================================
  183.  
  184. local function hasOffset()
  185.     return #checkOffset > 0
  186. end
  187.  
  188. local function moveToCheckPosition()
  189.     for _, move in ipairs(checkOffset) do
  190.         local dir = move[1]
  191.         local dist = move[2]
  192.         for i = 1, dist do
  193.             if not moveInDirection(dir) then
  194.                 digInDirection(dir)
  195.                 if not moveInDirection(dir) then
  196.                     return false
  197.                 end
  198.             end
  199.         end
  200.     end
  201.     return true
  202. end
  203.  
  204. local function returnFromCheckPosition()
  205.     for i = #checkOffset, 1, -1 do
  206.         local move = checkOffset[i]
  207.         local dir = getOppositeDirection(move[1])
  208.         local dist = move[2]
  209.         for j = 1, dist do
  210.             if not moveInDirection(dir) then
  211.                 digInDirection(dir)
  212.                 moveInDirection(dir)
  213.             end
  214.         end
  215.     end
  216.     return true
  217. end
  218.  
  219. --=============================================================================
  220. -- FUEL MANAGEMENT
  221. --=============================================================================
  222.  
  223. local function isValidFuel(itemName)
  224.     return fuelItems[itemName] == true
  225. end
  226.  
  227. local function findFuelSlot()
  228.     for slot = 1, 16 do
  229.         local item = turtle.getItemDetail(slot)
  230.         if item and isValidFuel(item.name) then
  231.             return slot
  232.         end
  233.     end
  234.     return nil
  235. end
  236.  
  237. local function findEmptySlot()
  238.     for slot = 1, 16 do
  239.         if turtle.getItemCount(slot) == 0 then
  240.             return slot
  241.         end
  242.     end
  243.     return nil
  244. end
  245.  
  246. local function refuel()
  247.     if turtle.getFuelLevel() == "unlimited" then return true end
  248.     if turtle.getFuelLevel() >= minFuelLevel then return true end
  249.    
  250.     -- Try from inventory first
  251.     local fuelSlot = findFuelSlot()
  252.     while fuelSlot and turtle.getFuelLevel() < minFuelLevel do
  253.         turtle.select(fuelSlot)
  254.         turtle.refuel(1)
  255.         fuelSlot = findFuelSlot()
  256.     end
  257.    
  258.     if turtle.getFuelLevel() >= minFuelLevel then
  259.         turtle.select(1)
  260.         return true
  261.     end
  262.    
  263.     -- Try peripheral
  264.     local side = directionToSide(fuelInventoryDirection)
  265.     if peripheral.isPresent(side) then
  266.         local inv = peripheral.wrap(side)
  267.         if inv and inv.list and inv.pushItems then
  268.             local emptySlot = findEmptySlot()
  269.             if emptySlot then
  270.                 turtle.select(emptySlot)
  271.                 local items = inv.list()
  272.                 for slot, item in pairs(items) do
  273.                     if item and isValidFuel(item.name) then
  274.                         local myName = os.getComputerLabel() or ("turtle_" .. os.getComputerID())
  275.                         local transferred = 0
  276.                         pcall(function()
  277.                             transferred = inv.pushItems(myName, slot, 64, emptySlot)
  278.                         end)
  279.                         if transferred and transferred > 0 then
  280.                             turtle.refuel()
  281.                             break
  282.                         end
  283.                     end
  284.                 end
  285.             end
  286.         end
  287.     end
  288.    
  289.     turtle.select(1)
  290.     return turtle.getFuelLevel() > 0
  291. end
  292.  
  293. --=============================================================================
  294. -- BLOCK DETECTION
  295. --=============================================================================
  296.  
  297. local function isWhitelisted(blockData)
  298.     if not blockData or not blockData.name then return false end
  299.     return whitelistedBlocks[blockData.name] == true
  300. end
  301.  
  302. local function getBlockName(dir)
  303.     local success, data = inspectInDirection(dir)
  304.     if success then
  305.         return data.name
  306.     else
  307.         return "(air/empty)"
  308.     end
  309. end
  310.  
  311. --=============================================================================
  312. -- MINING LOGIC
  313. --=============================================================================
  314.  
  315. local function mineOres()
  316.     local distanceTraveled = 0
  317.     local stopReason = "complete"
  318.    
  319.     print("Starting mining...")
  320.    
  321.     while true do
  322.         if not refuel() then
  323.             stopReason = "fuel"
  324.             break
  325.         end
  326.        
  327.         local mineSuccess, mineBlockData = inspectInDirection(miningDirection)
  328.         if mineSuccess then
  329.             if isWhitelisted(mineBlockData) then
  330.                 print("Mining: " .. mineBlockData.name)
  331.                 digInDirection(miningDirection)
  332.             end
  333.         end
  334.        
  335.         local travelSuccess, travelBlockData = inspectInDirection(travelDirection)
  336.        
  337.         if travelSuccess then
  338.             if strictMode then
  339.                 if isWhitelisted(travelBlockData) then
  340.                     digInDirection(travelDirection)
  341.                 else
  342.                     print("Strict stop: " .. travelBlockData.name)
  343.                     stopReason = "strict"
  344.                     break
  345.                 end
  346.             else
  347.                 digInDirection(travelDirection)
  348.             end
  349.         end
  350.        
  351.         if moveInDirection(travelDirection) then
  352.             distanceTraveled = distanceTraveled + 1
  353.         else
  354.             stopReason = "blocked"
  355.             break
  356.         end
  357.        
  358.         sleep(0.1)
  359.     end
  360.    
  361.     print("Returning " .. distanceTraveled .. " blocks")
  362.    
  363.     local returnDirection = getOppositeDirection(travelDirection)
  364.    
  365.     for i = 1, distanceTraveled do
  366.         local mineSuccess, mineBlockData = inspectInDirection(miningDirection)
  367.         if mineSuccess and isWhitelisted(mineBlockData) then
  368.             digInDirection(miningDirection)
  369.         end
  370.        
  371.         if not moveInDirection(returnDirection) then
  372.             digInDirection(returnDirection)
  373.             moveInDirection(returnDirection)
  374.         end
  375.         sleep(0.05)
  376.     end
  377.    
  378.     if hasOffset() then
  379.         returnFromCheckPosition()
  380.     end
  381.    
  382.     print("Cycle complete.")
  383. end
  384.  
  385. --=============================================================================
  386. -- MAIN LOOP
  387. --=============================================================================
  388.  
  389. local function main()
  390.     print("=================================")
  391.     print("  Mining Turtle - Diagnostic")
  392.     print("=================================")
  393.     print("Fuel: " .. turtle.getFuelLevel())
  394.     print("---------------------------------")
  395.    
  396.     sleep(initialWait)
  397.    
  398.     while true do
  399.         refuel()
  400.        
  401.         -- Move to check position
  402.         if hasOffset() then
  403.             print("")
  404.             print("Moving to check position...")
  405.             if not moveToCheckPosition() then
  406.                 print("ERROR: Could not move!")
  407.                 sleep(checkInterval)
  408.                 goto continue
  409.             end
  410.         end
  411.        
  412.         -- SCAN AND REPORT ALL DIRECTIONS
  413.         print("")
  414.         print("=== BLOCKS AT CHECK POSITION ===")
  415.         print("UP:    " .. getBlockName("U"))
  416.         print("DOWN:  " .. getBlockName("D"))
  417.         print("FRONT: " .. getBlockName("F"))
  418.         print("BACK:  " .. getBlockName("B"))
  419.         print("LEFT:  " .. getBlockName("L"))
  420.         print("RIGHT: " .. getBlockName("R"))
  421.         print("=================================")
  422.         print("")
  423.        
  424.         -- Check trigger direction
  425.         local triggerSuccess, triggerData = inspectInDirection(triggerDirection)
  426.        
  427.         if triggerSuccess then
  428.             print("TRIGGER FOUND: " .. triggerData.name)
  429.             sleep(0.5)
  430.             mineOres()
  431.             sleep(2)
  432.         else
  433.             print("No trigger (looking " .. triggerDirection .. ")")
  434.             if hasOffset() then
  435.                 print("Returning to home...")
  436.                 returnFromCheckPosition()
  437.             end
  438.             print("Waiting " .. checkInterval .. " seconds...")
  439.             sleep(checkInterval)
  440.         end
  441.        
  442.         ::continue::
  443.     end
  444. end
  445.  
  446. main()
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment