Shaka01

New Miner 2024

Nov 24th, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.65 KB | None | 0 0
  1. -- Mining Turtle Program
  2.  
  3. --User configurable
  4. local heightLimit = -58 -- Minimum height to stop mining (bedrock start assumed at y=-59)
  5. local yHeight = -55 --preferred baseline height, will be overwritten if gps works
  6. local enderchestName = "kibe:entangled_chest" -- name of the enderchest like thing you're using
  7.  
  8.  
  9.  
  10. -------------------------
  11. local limit = 3
  12. local width = 3
  13. local startPos = {x = 1, y = 0, z = 1} -- Starting position
  14. local currentPos = {x = 0, y = 0, z = 0} -- Track position
  15. local miningPos = {x = 0, y = 0, z = 0} -- Track position
  16. local lowerStart = 1
  17. local returning = false
  18. local miningReturn = false
  19. local useBlacklist = false
  20. local blacklist = {}
  21. local rememberSelected = nil
  22. local statistics = {}
  23. local useEnderchest = false
  24. local startingSequenceVar = true
  25. local turtleX, turtleY, turtleZ = gps.locate()
  26.  
  27. if turtleY then
  28.     yHeight = turtleY
  29. end
  30.  
  31.  
  32. ---get API
  33. if fs.exists("API") == false then
  34.     shell.run("pastebin", "get", "EzkfU5ZM", "API")
  35. end
  36. shaka = require("API")
  37. fs.delete("statistics")
  38.  
  39.  
  40. function lookForEnderchest()
  41.     for slot = 1, 16 do
  42.         local item = turtle.getItemDetail(slot)
  43.         if item and item.name == enderchestName then
  44.             useEnderchest = true
  45.             return true
  46.         end
  47.     end
  48.     useEnderchest = false
  49. end
  50.  
  51. --- main menu
  52. function configureSettings()
  53.     lookForEnderchest()
  54.     local blacklistEnabled = true
  55.     local quarryLength = limit or 3
  56.     local quarryWidth = width or 1
  57.     local startLower = lowerStart or 1
  58.     local startYHeight = yHeight or 64 -- Default Current Y Lvl
  59.  
  60.     local selectedOption = 1
  61.     local options = {"Blacklist", "Quarry Length", "Quarry Width", "Lower Start", "Current Y lvl"}
  62.  
  63.     local function drawMenu()
  64.         term.clear()
  65.         term.setCursorPos(1, 1)
  66.         term.setTextColor(colors.black)
  67.         term.setBackgroundColor(colors.gray)
  68.         term.clearLine()
  69.         shaka.centerText("<=== Quarry Configuration Menu ===>", 1)
  70.         term.setTextColor(colors.white)
  71.         term.setCursorPos(1, 3)
  72.        
  73.         for i, option in ipairs(options) do
  74.             if i == selectedOption then
  75.                 term.setTextColor(colors.white)
  76.                 term.setBackgroundColor(colors.black)
  77.             else
  78.                 term.setTextColor(colors.lightGray)
  79.                 term.setBackgroundColor(colors.black)
  80.             end
  81.  
  82.             if option == "Blacklist" then
  83.                 -- Set highlight for the entire line
  84.                 if i == selectedOption then
  85.                     term.setTextColor(colors.white)
  86.                     term.setBackgroundColor(colors.black) -- Highlight background
  87.                 else
  88.                     term.setTextColor(colors.lightGray)
  89.                     term.setBackgroundColor(colors.black)
  90.                 end
  91.  
  92.                 -- Print the option label normally
  93.                 write(string.format("- %s: ", option))
  94.  
  95.                 -- Now print ON or OFF in the appropriate color
  96.                 if blacklistEnabled then
  97.                     term.setTextColor(colors.green) -- Green for ON
  98.                     write("ON")
  99.                 else
  100.                     term.setTextColor(colors.red) -- Red for OFF
  101.                     write("OFF")
  102.                 end
  103.  
  104.                 -- Reset colors after the line
  105.                 term.setTextColor(colors.lightGray)
  106.                 term.setBackgroundColor(colors.black)
  107.                 print("") -- Move to the next line
  108.             elseif option == "Quarry Length" then
  109.                 print(string.format("- %s: %d", option, quarryLength))
  110.             elseif option == "Quarry Width" then
  111.                 print(string.format("- %s: %d", option, quarryWidth))
  112.             elseif option == "Lower Start" then
  113.                 print(string.format("- %s: %d", option, startLower))
  114.             elseif option == "Current Y lvl" then
  115.                 print(string.format("- %s: %d", option, startYHeight))
  116.             end
  117.         end
  118.        
  119.         term.setCursorPos(1, 8)
  120.         term.setTextColor(colors.lightGray)
  121.         write("- Use Enderchest: ")
  122.         if useEnderchest then
  123.             term.setTextColor(colors.green)
  124.             enderText = "YES"
  125.         else
  126.             term.setTextColor(colors.red)
  127.             enderText = "NO"
  128.         end
  129.         write(enderText)
  130.         term.setTextColor(colors.gray)
  131.         write(" (checks inv)")
  132.         term.setCursorPos(1, 10)
  133.         shaka.changeColors(colors.black, colors.yellow)
  134.         print("- Use w, a, s, d, arrows or mousewheel.")
  135.         term.setTextColor(colors.green)
  136.         term.setBackgroundColor(colors.black)
  137.         print("- Press Enter to start mining.")
  138.     end
  139.  
  140.     -- Main loop for handling user interaction
  141.     local running = true
  142.     while running do
  143.         drawMenu()
  144.         local event, param1, param2, param3 = os.pullEvent()
  145.  
  146.         if event == "key" then
  147.             if rememberSelected ~= nil then
  148.                 selectedOption = rememberSelected
  149.                 rememberSelected = nil
  150.             end
  151.             if param1 == keys.up or param1 == keys.w then
  152.                 selectedOption = math.max(1, selectedOption - 1)
  153.             elseif param1 == keys.down or param1 == keys.s then
  154.                 selectedOption = math.min(#options, selectedOption + 1)
  155.             elseif (param1 == keys.left or param1 == keys.a) or (param1 == keys.right or param1 == keys.d) then
  156.                 if selectedOption == 1 then
  157.                     blacklistEnabled = not blacklistEnabled
  158.                 elseif selectedOption == 2 then
  159.                     if param1 == keys.left or param1 == keys.a then
  160.                         quarryLength = math.max(3, quarryLength - 1)
  161.                     else
  162.                         quarryLength = quarryLength + 1
  163.                     end
  164.                 elseif selectedOption == 3 then
  165.                     if param1 == keys.left or param1 == keys.a then
  166.                         quarryWidth = math.max(1, quarryWidth - 1)
  167.                     else
  168.                         quarryWidth = quarryWidth + 1
  169.                     end
  170.                 elseif selectedOption == 4 then
  171.                     if param1 == keys.left or param1 == keys.a then
  172.                         startLower = math.max(1, startLower - 1)
  173.                     else
  174.                         startLower = startLower + 1
  175.                     end
  176.                 elseif selectedOption == 5 then
  177.                     if param1 == keys.left or param1 == keys.a then
  178.                         startYHeight = startYHeight - 1
  179.                     else
  180.                         startYHeight = startYHeight + 1
  181.                     end
  182.                 end
  183.             elseif param1 == keys.enter then
  184.                 running = false
  185.             elseif param1 == keys.backspace then
  186.                 os.reboot()
  187.             end
  188.         elseif event == "mouse_scroll" then
  189.             if selectedOption ~= nil then
  190.                 rememberSelected = selectedOption
  191.             end
  192.             selectedOption = nil
  193.             if param1 == -1 then -- Scroll up
  194.                 if param3 == 3 then
  195.                     blacklistEnabled = not blacklistEnabled
  196.                 elseif param3 == 4 then
  197.                     quarryLength = quarryLength + 3
  198.                 elseif param3 == 5 then
  199.                     quarryWidth = quarryWidth + 3
  200.                 elseif param3 == 6 then
  201.                     startLower = startLower + 3
  202.                 elseif param3 == 7 then
  203.                     startYHeight = startYHeight + 3
  204.                 end
  205.             elseif param1 == 1 then -- Scroll down
  206.                 if param3 == 3 then
  207.                     blacklistEnabled = not blacklistEnabled
  208.                 elseif param3 == 4 then
  209.                     quarryLength = math.max(3, quarryLength - 3)
  210.                 elseif param3 == 5 then
  211.                     quarryWidth = math.max(1, quarryWidth - 3)
  212.                 elseif param3 == 6 then
  213.                     startLower = math.max(1, startLower - 3)
  214.                 elseif param3 == 7 then
  215.                     startYHeight = startYHeight - 3
  216.                 end
  217.             end
  218.         elseif event == "turtle_inventory" then
  219.             lookForEnderchest()
  220.         end
  221.     end
  222.  
  223.     -- Apply the configuration to global variables
  224.     limit = quarryLength
  225.     width = quarryWidth
  226.     lowerStart = startLower
  227.     useBlacklist = blacklistEnabled
  228.     startPos.y = startYHeight
  229. end
  230.  
  231. function isInventoryInFront()
  232.     -- Attempt to wrap the block in front as a peripheral
  233.     local peripheralName = peripheral.getType("front")
  234.     if peripheralName then
  235.         -- Check if the peripheral type includes "inventory" capabilities
  236.         local methods = peripheral.getMethods("front")
  237.         if methods then
  238.             for _, method in ipairs(methods) do
  239.                 if method == "list" then
  240.                     -- If the "list" method exists, it's likely an inventory
  241.                     return true
  242.                 end
  243.             end
  244.         end
  245.     end
  246.     return false -- No inventory detected
  247. end
  248.  
  249.  
  250. function startingSequence()
  251.     shaka.changeColors(colors.black, colors.white)
  252.     shaka.clearScreen()
  253.    
  254.     if useEnderchest == false then
  255.         term.setTextColor(colors.orange)
  256.         print("- Checking if there is an inventory..")
  257.         turtle.turnLeft()
  258.         turtle.turnLeft()
  259.         if isInventoryInFront() then
  260.             term.setTextColor(colors.green)
  261.             print("\n- Good job, inventory detected.")
  262.         else
  263.             repeat
  264.                 term.setBackgroundColor(colors.black)
  265.                 term.setCursorPos(1, 3)
  266.                 term.clearLine()
  267.                 shaka.changeColors(colors.red, colors.black)
  268.                 print("!Please place inventory in front of me!")
  269.                 sleep(1)
  270.             until isInventoryInFront()
  271.             shaka.changeColors(colors.black, colors.green)
  272.             print("Thanks!")
  273.         end
  274.         turtle.turnRight()
  275.         turtle.turnRight()
  276.     else
  277.         print("- Enderchest detected")
  278.     end
  279.     shaka.changeColors(colors.black, colors.yellow)
  280.     print("\n- Position set, safety checks done.\n")
  281.     term.setTextColor(colors.green)
  282.     textutils.slowPrint("- Starting..", 10)
  283.     startingSequenceVar = false
  284. end
  285.  
  286. -- Helper functions
  287.  
  288. function isBlacklisted(blockName)
  289.     if useBlacklist == false then
  290.         return false
  291.     end
  292.     blacklist = shaka.readFile("blacklist.txt")
  293.     if blacklist == false then
  294.         blacklist = {}
  295.     end
  296.     for _, name in ipairs(blacklist) do
  297.         if name == blockName then
  298.             return true
  299.         end
  300.     end
  301.     return false
  302. end
  303.  
  304. function addToBlacklist()
  305.     useBlacklist = true
  306.     -- Load or initialize the blacklist
  307.     local blacklist = shaka.readFile("blacklist.txt")
  308.     if not blacklist then
  309.         blacklist = {}
  310.     end
  311.  
  312.     -- Function to display the main blacklist menu
  313.     local function drawMenu()
  314.         term.clear()
  315.         term.setCursorPos(1, 1)
  316.         term.setTextColor(colors.black)
  317.         term.setBackgroundColor(colors.gray)
  318.         term.clearLine()
  319.         shaka.centerText("<=== Blacklist Configuration ===>", 1)
  320.         term.setBackgroundColor(colors.black)
  321.         term.setTextColor(colors.gray)
  322.         term.setCursorPos(1, 11)
  323.         print("[B] to view blacklist\nBackspace to return")
  324.         term.setTextColor(colors.white)
  325.         term.setCursorPos(1, 3)
  326.         term.setTextColor(colors.green)
  327.         print("Slot <15: Add items")
  328.         term.setTextColor(colors.red)
  329.         print("Slot  16: Remove items")
  330.         term.setTextColor(colors.lightGray)
  331.         term.setCursorPos(1, 6)
  332.         print("Detected changes:")
  333.     end
  334.  
  335.     -- Function to display the blacklist with scrolling
  336.     local function viewBlacklist()
  337.         local currentPage = 1
  338.         local itemsPerPage = 9
  339.  
  340.         local function drawBlacklist()
  341.             term.clear()
  342.             term.setCursorPos(1, 1)
  343.             term.setTextColor(colors.black)
  344.             term.setBackgroundColor(colors.gray)
  345.             term.clearLine()
  346.             shaka.centerText("<=== Blacklist Items ===>", 1)
  347.             term.setBackgroundColor(colors.black)
  348.             term.setCursorPos(1, 3)
  349.             term.setTextColor(colors.white)
  350.  
  351.             -- Determine the range of items to display
  352.             local startIndex = (currentPage - 1) * itemsPerPage + 1
  353.             local endIndex = math.min(#blacklist, currentPage * itemsPerPage)
  354.  
  355.             -- Display items
  356.             for i = startIndex, endIndex do
  357.                 print(string.format("%d. %s", i, shaka.prettyName(blacklist[i])))
  358.             end
  359.  
  360.             -- Display navigation info
  361.             term.setCursorPos(1, 11)
  362.             term.setTextColor(colors.gray)
  363.             print("Use arrows or W/S to scroll.\nBackspace to return")
  364.         end
  365.  
  366.         -- Scrollable menu loop
  367.         local running = true
  368.         while running do
  369.             drawBlacklist()
  370.             local event, param1 = os.pullEvent("key")
  371.             if param1 == keys.up or param1 == keys.w then
  372.                 currentPage = math.max(1, currentPage - 1)
  373.             elseif param1 == keys.down or param1 == keys.s then
  374.                 local maxPages = math.ceil(#blacklist / itemsPerPage)
  375.                 currentPage = math.min(maxPages, currentPage + 1)
  376.             elseif param1 == keys.backspace or param1 == keys.escape then
  377.                 running = false -- Return to the main menu
  378.             end
  379.         end
  380.     end
  381.  
  382.     -- Function to add an item to the blacklist and save immediately
  383.     local function addItemToBlacklist(itemName)
  384.         if not isBlacklisted(itemName) then
  385.             table.insert(blacklist, itemName)
  386.             shaka.writeFile("blacklist.txt", blacklist) -- Save updated blacklist immediately
  387.             term.setTextColor(colors.green)
  388.             write("+ ".. shaka.prettyName(itemName))
  389.             textutils.slowPrint(" added", 10)
  390.         else
  391.             term.setTextColor(colors.yellow)
  392.             write("> ".. shaka.prettyName(itemName))
  393.             textutils.slowPrint(" already known", 10)
  394.         end
  395.     end
  396.  
  397.     -- Function to remove an item from the blacklist and save immediately
  398.     local function removeItemFromBlacklist(itemName)
  399.         for i = #blacklist, 1, -1 do
  400.             if blacklist[i] == itemName then
  401.                 table.remove(blacklist, i)
  402.                 shaka.writeFile("blacklist.txt", blacklist) -- Save updated blacklist immediately
  403.                 term.setTextColor(colors.red)
  404.                 write("- ".. shaka.prettyName(itemName))
  405.                 textutils.slowPrint(" removed", 10)
  406.                 return true
  407.             end
  408.         end
  409.         return false
  410.     end
  411.  
  412.     -- Main menu loop
  413.     local running = true
  414.     while running do
  415.         drawMenu()
  416.         local event, param1 = os.pullEvent() -- Wait for an event
  417.  
  418.         if event == "turtle_inventory" then
  419.             -- Inventory change detected, process slots
  420.             term.setCursorPos(1, 8) -- Start displaying items below the menu
  421.  
  422.             -- Handle slots 1-15 for adding to the blacklist
  423.             for i = 1, 15 do
  424.                 local item = turtle.getItemDetail(i)
  425.                 if item then
  426.                     addItemToBlacklist(item.name)
  427.                     sleep(0.5)
  428.                 end
  429.             end
  430.  
  431.             -- Handle slot 16 for removing from the blacklist
  432.             local slot16Item = turtle.getItemDetail(16)
  433.             if slot16Item then
  434.                 removeItemFromBlacklist(slot16Item.name)
  435.                 sleep(0.5)
  436.             end
  437.         elseif event == "key" then
  438.             if param1 == keys.b then
  439.                 viewBlacklist() -- Open the blacklist viewer
  440.             elseif param1 == keys.backspace then
  441.                 running = false -- Exit the blacklist menu
  442.             end
  443.         end
  444.     end
  445.    
  446.     useBlacklist = false
  447. end
  448.  
  449.  
  450. -- Function to start the main menu
  451. function startMenu()
  452.     while true do
  453.         shaka.clearScreen()
  454.         term.setTextColor(colors.yellow)
  455.         shaka.centerText("=== Quarry Mining Menu ===", 1)
  456.         term.setTextColor(colors.white)
  457.         term.setCursorPos(1, 3)
  458.         print("1. Configure blacklist")
  459.         print("2. Start mining")
  460.  
  461.         -- Wait for user input
  462.         local success, key = os.pullEvent("key")
  463.  
  464.         if key == keys.one then
  465.             -- Blacklist configuration
  466.             addToBlacklist()
  467.         elseif key == keys.two then
  468.             -- Quarry configuration
  469.             configureSettings()
  470.             break
  471.         elseif key == keys.enter then
  472.             -- Start mining
  473.             -- return
  474.         end
  475.     end
  476. end
  477.  
  478.  
  479. function savePosition()
  480.     local file = fs.open("position.txt", "w")
  481.     file.writeLine(textutils.serialize(currentPos))
  482.     file.close()
  483. end
  484.  
  485. function printPosition()
  486.     -- print("x: ", currentPos.x,"y: ", currentPos.y,"z :", currentPos.z)-- "baseLine Top: ", startPos.y - lowerStart)
  487.     -- print("minX: ", miningPos.x, "minY: ", miningPos.y)
  488. end
  489.  
  490. function loadPosition()
  491.     shaka.clearScreen()
  492.     if fs.exists("position.txt") then
  493.         local file = fs.open("position.txt", "r")
  494.         local data = textutils.unserialize(file.readLine())
  495.         file.close()
  496.         if data and type(data) == "table" then
  497.             currentPos = data
  498.         else
  499.             print("Starting position set.")
  500.             currentPos = {x = startPos.x, y = startPos.y, z = startPos.z}
  501.         end
  502.     else
  503.         print("Position file not found, using startPos.")
  504.         currentPos = {x = startPos.x, y = startPos.y, z = startPos.z}
  505.     end
  506. end
  507.  
  508. function calculateFuelForReturn(currentPos, startingPos)
  509.     -- Calculate the distance between the current position and starting position
  510.     local dx = math.abs(currentPos.x - startingPos.x)
  511.     local dy = math.abs(currentPos.y - startingPos.y)
  512.     local dz = math.abs(currentPos.z - startingPos.z)
  513.  
  514.     -- The total distance is the sum of the absolute differences in all coordinates
  515.     local totalDistance = dx + dy + dz
  516.  
  517.     -- Return the calculated fuel cost
  518.     return totalDistance
  519. end
  520.  
  521. function checkFuelForReturn(currentPos, startingPos)
  522.     -- Calculate the fuel needed to return home
  523.     local fuelNeeded = calculateFuelForReturn(currentPos, startingPos)
  524.  
  525.     -- Get the current fuel level of the turtle
  526.     local currentFuel = turtle.getFuelLevel()
  527.  
  528.     -- Check if the turtle has enough fuel
  529.     if currentFuel < fuelNeeded + 50 then
  530.         -- Not enough fuel
  531.         term.setBackgroundColor(colors.red)
  532.         shaka.clearScreen()
  533.         print("Warning: Low on fuel, returning home!")
  534.         print("Fuel required: " .. fuelNeeded)
  535.         print("Current fuel: " .. currentFuel)
  536.         returnToStart()
  537.         print("Hit enter to restart")
  538.         while true do
  539.             success, key = os.pullEvent("key")
  540.             if key == keys.enter then
  541.                 os.reboot()
  542.             end
  543.         end
  544.     end
  545. end
  546.  
  547. -------movement functions
  548. function moveDown()
  549.     checkFuelForReturn(currentPos, startPos)
  550.     if turtle.down() then
  551.         currentPos.y = currentPos.y - 1
  552.         savePosition()
  553.         printPosition()
  554.         recordStatistics("moveDown")
  555.         return true
  556.     else
  557.         recordStatistics("timesScanned")
  558.         recordStatistics("timesMined")
  559.         local success, data = turtle.inspectDown()
  560.         recordStatistics(data.name)
  561.         turtle.digDown()
  562.     end
  563.     return false
  564. end
  565.  
  566. function moveUp()
  567.     checkFuelForReturn(currentPos, startPos)
  568.     if turtle.up() then
  569.         currentPos.y = currentPos.y + 1
  570.         savePosition()
  571.         printPosition()
  572.         recordStatistics("moveUp")
  573.         return true
  574.     else
  575.         recordStatistics("timesScanned")
  576.         recordStatistics("timesMined")
  577.         local success, data = turtle.inspectUp()
  578.         recordStatistics(data.name)
  579.         turtle.digUp()
  580.     end
  581.     return false
  582. end
  583.  
  584. function moveForward()
  585.     checkFuelForReturn(currentPos, startPos)
  586.     if turtle.forward() then
  587.         if returning == false then
  588.             currentPos.x = currentPos.x + 1
  589.         else
  590.             currentPos.x = currentPos.x - 1
  591.         end
  592.         savePosition()
  593.         printPosition()
  594.         recordStatistics("moveForward")
  595.         return true
  596.     else
  597.         recordStatistics("timesScanned")
  598.         recordStatistics("timesMined")
  599.         local success, data = turtle.inspect()
  600.         recordStatistics(data.name)
  601.         turtle.dig()
  602.     end
  603.     return false
  604. end
  605.  
  606. function moveZ()
  607.     if turtle.forward() then
  608.         currentPos.z = currentPos.z + 1
  609.         savePosition()
  610.         printPosition()
  611.         recordStatistics("moveSide")
  612.         return true
  613.     else
  614.         recordStatistics("timesMined")
  615.         recordStatistics("timesScanned")
  616.         local success, data = turtle.inspect()
  617.         recordStatistics(data.name)
  618.         turtle.dig()
  619.     end
  620.     return false
  621. end
  622. ----------------------
  623. function recordStatistics(name)
  624.     -- Initialize the key for the given name if it doesn't exist
  625.     if statistics[name] == nil then
  626.         statistics[name] = 0
  627.     end
  628.     -- Increment the counter for the specific item
  629.     statistics[name] = statistics[name] + 1
  630.     -- Save the updated statistics to a file
  631.     shaka.writeFile("statistics", statistics)
  632. end
  633.  
  634. function mineDirection()
  635.     for i = 1, 4 do
  636.         local success, data = turtle.inspect()
  637.         recordStatistics("timesScanned")
  638.         if not success then
  639.             recordStatistics("scannedAir")
  640.         end
  641.         if success and not isBlacklisted(data.name) then
  642.             -- Determine whether to mine based on conditions
  643.             local shouldMine = true
  644.            
  645.             if currentPos.x == startPos.x and currentPos.z == startPos.z and i == 3 then
  646.                 shouldMine = false
  647.             end
  648.            
  649.             if currentPos.z == startPos.z then
  650.                 if not returning and i == 4 then
  651.                     shouldMine = false
  652.                 elseif returning and i == 2 then
  653.                     shouldMine = false
  654.                 end
  655.             end
  656.            
  657.             if currentPos.z >= width then
  658.                 if not returning and i == 2 then
  659.                     shouldMine = false
  660.                 elseif returning and i == 4 then
  661.                     shouldMine = false
  662.                 end
  663.             end
  664.            
  665.             if (currentPos.x == limit and not returning) or (currentPos.x == startPos.x and returning) then
  666.                 if i == 1 then
  667.                     shouldMine = false
  668.                 end
  669.             end
  670.            
  671.             if shouldMine then
  672.                 recordStatistics(data.name)
  673.                 repeat
  674.                     sleep(0.1)
  675.                 until not turtle.dig()
  676.                 recordStatistics("timesMined")
  677.             end
  678.            
  679.         end
  680.         turtle.turnRight()
  681.     end
  682. end
  683.  
  684. function changeLane()
  685.     if returning == false then
  686.         returning = true
  687.         turtle.turnRight()
  688.         for i =1, 2 do
  689.             if currentPos.z == width then
  690.                 break
  691.             end
  692.             repeat until moveZ()
  693.         end
  694.         turtle.turnRight()
  695.     else
  696.         returning = false
  697.         turtle.turnLeft()
  698.         for i =1, 2 do
  699.             if currentPos.z == width then
  700.                 break
  701.             end
  702.             repeat until moveZ()
  703.         end
  704.         turtle.turnLeft()
  705.     end
  706.     repeat until moveForward()
  707.     mineDirection()
  708. end
  709.  
  710. function isInventoryFull()
  711.     for i = 1, 16 do
  712.         if turtle.getItemCount(i) == 0 then
  713.             return false
  714.         end
  715.     end
  716.     return true
  717. end
  718.  
  719. function unloadInventory()
  720.     -- Check if there's an inventory in front
  721.     if not isInventoryInFront() then
  722.         printError("No inventory detected in front")
  723.         repeat sleep(1) until isInventoryInFront()
  724.     end
  725.  
  726.     -- Main unloading logic
  727.     shaka.changeColors(colors.black, colors.white)
  728.     shaka.clearScreen()
  729.     print("Starting to unload turtle inventory...\n")
  730.     for slot = 1, 16 do
  731.         if turtle.getItemCount(slot) > 0 then
  732.             -- Select the slot with items
  733.             turtle.select(slot)
  734.  
  735.             -- Attempt to drop items into the inventory
  736.             while not turtle.drop() do
  737.                 -- If the drop fails, inform the user and retry
  738.                 shaka.clearScreen()
  739.                 printError("Failed to drop items from slot [" .. slot.."]")
  740.                 print("Inventory may be full, please clear space!")
  741.                 sleep(1)
  742.             end
  743.  
  744.             print("Emptied slot " .. slot)
  745.         end
  746.     end
  747.  
  748.     term.setTextColor(colors.green)
  749.     print("All items successfully unloaded.")
  750.     turtle.select(1) -- Reset turtle's selected slot
  751. end
  752.  
  753. function enderUnload()
  754.     -- Locate the Ender Chest in the turtle's inventory
  755.     local enderChestSlot = nil
  756.     for slot = 1, 16 do
  757.         local item = turtle.getItemDetail(slot)
  758.         if item and item.name == enderchestName then
  759.             enderChestSlot = slot
  760.             break
  761.         end
  762.     end
  763.  
  764.     -- Place the Ender Chest in front of the turtle
  765.     turtle.select(enderChestSlot)
  766.     if not turtle.placeUp() then
  767.         printError("Failed to place the Ender Chest. Make sure the front is clear.")
  768.         repeat turtle.digUp() until turtle.placeUp()
  769.     end
  770.  
  771.     -- Unload all items into the Ender Chest
  772.     for slot = 1, 16 do
  773.         if slot ~= enderChestSlot and turtle.getItemCount(slot) > 0 then
  774.             turtle.select(slot)
  775.             while not turtle.dropUp() do
  776.                 printError("Ender Chest is full, clear space.")
  777.                 sleep(1)
  778.             end
  779.         end
  780.     end
  781.  
  782.     -- Break the Ender Chest to retrieve it
  783.     turtle.select(enderChestSlot)
  784.     if not turtle.digUp() then
  785.         printError("Failed to retrieve the Ender Chest!")
  786.         return false
  787.     end
  788.  
  789.     -- Reset selection to slot 1
  790.     turtle.select(1)
  791.     print("Successfully unloaded inventory into Ender Chest.")
  792.     return true
  793.  
  794. end
  795.  
  796. function returnToStart()
  797.     print("Returning to start")
  798.     miningPos.x = currentPos.x
  799.     miningPos.y = currentPos.y
  800.     miningPos.z = currentPos.z
  801.     miningReturn = returning
  802.  
  803.     -- Turn around to mine blocks on the way back
  804. if returning == false then
  805.     turtle.turnLeft()
  806.     turtle.turnLeft()
  807.     returning = true
  808. end
  809.     -- Move back to the starting position
  810.     turtle.turnRight()
  811.     while currentPos.z > startPos.z do
  812.         repeat
  813.             turtle.dig()
  814.         until turtle.forward()
  815.         currentPos.z = currentPos.z - 1
  816.     end
  817.     turtle.turnLeft()
  818.    
  819.  
  820.     while currentPos.x > startPos.x do
  821.         moveForward()
  822.     end
  823.    
  824.     while currentPos.y < startPos.y do
  825.         moveUp()
  826.     end
  827.    
  828.     if useEnderchest == false then
  829.         unloadInventory()
  830.     else
  831.         enderUnload()
  832.     end
  833.    
  834.     -- Turn back to the original orientation
  835.     turtle.turnLeft()
  836.     turtle.turnLeft()
  837.     returning = false
  838. end
  839.  
  840. function resumeMining()
  841.     print("Resuming mining")
  842.     -- move down to saved position
  843.     while currentPos.y > miningPos.y do
  844.         repeat until moveDown()
  845.     end
  846.     -- Move forward to the saved position
  847.     while currentPos.x < miningPos.x do
  848.         repeat until moveForward()
  849.     end
  850.     --move on z axis to saved position
  851.     if currentPos.z ~= miningPos.z then
  852.         turtle.turnRight()
  853.         while currentPos.z < miningPos.z do
  854.             repeat until moveZ()
  855.         end
  856.         if miningReturn then
  857.             turtle.turnRight()
  858.             returning = true
  859.         else
  860.             turtle.turnLeft()
  861.             returning = false
  862.         end
  863.     end
  864. end
  865.  
  866. function invCheck()
  867.     if isInventoryFull() then
  868.         if useEnderchest == false then
  869.             returnToStart()
  870.             resumeMining()
  871.         else
  872.             enderUnload()
  873.         end
  874.     end
  875. end
  876.  
  877. function getLimit()
  878.     -- Move forward by 3 blocks
  879.     for _ = 1, 3 do
  880.         repeat until moveForward()
  881.         if returning == false then
  882.             if currentPos.x == limit then
  883.                 mineDirection()
  884.                 return true
  885.             end
  886.         else
  887.             if currentPos.x == startPos.x then
  888.                 mineDirection()
  889.                 return true
  890.             end
  891.         end
  892.     end
  893.     mineDirection()
  894.     return false
  895. end
  896.  
  897. function showEnd()
  898.     shaka.clearScreen()
  899.     term.setTextColor(colors.green)
  900.     statistics.timesMined = statistics.timesMined or 0
  901.     shaka.centerText("Mined "..statistics.timesMined.."/"..statistics.timesScanned.." scanned", 1)
  902.     term.setCursorPos(1, 2)
  903.     term.setTextColor(colors.lightGray)
  904.  
  905.     -- Convert the table to a list of key-value pairs
  906.     local sortedList = {}
  907.     for key, value in pairs(statistics) do
  908.         if shaka.stringFind(key, ":") then
  909.             table.insert(sortedList, {key = key, value = value})
  910.         end
  911.     end
  912.  
  913.     -- Sort the list by the value (count) in descending order
  914.     table.sort(sortedList, function(a, b)
  915.         return a.value > b.value
  916.     end)
  917.     -- Display the sorted items
  918.     for _, entry in ipairs(sortedList) do
  919.         print(shaka.prettyName(entry.key) .. " [" .. entry.value.."]")
  920.     end
  921. end
  922.  
  923. function oneRow()
  924.     --function to stop
  925.     local function killSwitch()
  926.         if getLimit() then
  927.             if currentPos.y == startPos.y - lowerStart then
  928.                 while currentPos.y > heightLimit do
  929.                     invCheck()
  930.                     if moveDown() then
  931.                         mineDirection()
  932.                     end
  933.                 end            
  934.             else
  935.                 while currentPos.y < startPos.y - lowerStart do
  936.                     invCheck()
  937.                     if moveUp() then
  938.                         mineDirection()
  939.                     end
  940.                 end        
  941.             end
  942.             shaka.changeColors(colors.black, colors.gray)
  943.             print("- Quarry boundary reached")
  944.             return true
  945.         end
  946.         return false
  947.     end
  948.    
  949.    
  950.     ------main function
  951.     while true do
  952.         -- Mine downward until bedrock or height limit
  953.         if currentPos.y >= startPos.y - lowerStart then
  954.             while currentPos.y > heightLimit do
  955.                 invCheck()
  956.                 if moveDown() then
  957.                     mineDirection()
  958.                 end
  959.             end
  960.         else
  961.             while currentPos.y < startPos.y - lowerStart do
  962.                 invCheck()
  963.                 if moveUp() then
  964.                     mineDirection()
  965.                 end
  966.             end
  967.         end
  968.    
  969.         if killSwitch() then break end
  970.        
  971.     end
  972. end
  973.  
  974. -- Main mining loop
  975.  
  976. local function mainLoopVertical()
  977.     startMenu()
  978.     loadPosition()
  979.     startingSequence()
  980.  
  981.     if lowerStart ~= 0 then
  982.         for i = 1, lowerStart do
  983.             repeat until moveDown()
  984.         end
  985.     mineDirection()
  986.     end
  987.  
  988.     while true do
  989.         oneRow()
  990.         -- print("Status: ", currentPos.z, width)
  991.         if currentPos.z >= width then
  992.             break
  993.         end
  994.        
  995.         changeLane()
  996.     end
  997.  
  998.     returnToStart()
  999.     showEnd()
  1000. end
  1001.  
  1002. mainLoopVertical()
  1003.  
Add Comment
Please, Sign In to add comment