ColdIV

advancedMining

Dec 30th, 2021 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.74 KB | None | 0 0
  1. -- pastebin run FuQ3WvPs wbPXakgy advancedMining
  2. local args = {...}
  3.  
  4. -- CONFIG -- EDIT BELOW
  5.  
  6. local fuelTable = {
  7.     "coal",
  8.     "charcoal"
  9. }
  10. local lootTable = {
  11.     "coal",
  12.     "coal_ore",
  13.     "diamond",
  14.     "diamond_ore",
  15.     "iron_ore",
  16.     "gold_ore",
  17.     "lapis_lazuli",
  18.     "lapis_lazuli_ore",
  19.     "emerald"
  20. }
  21. -- NOTE: The turtle will send notifications if it finds loot in this table
  22. -- the turtle wont look for these blocks, if you want to farm them add them to the loot table
  23. local rareLootTable = {
  24.     "coal",
  25.     "coal_ore",
  26.     "diamond",
  27.     "diamond_ore",
  28.     "gold_ore",
  29.     "lapis_lazuli",
  30.     "lapis_lazuli_ore",
  31.     "emerald",
  32.     "emerald_ore"
  33. }
  34. local blockTable = {
  35.     "cobblestone",
  36.     "dirt",
  37.     "stone",
  38.     "diorite",
  39.     "andesite",
  40.     "granite",
  41.     "redstone",
  42.     "cobbled_deepslate"
  43. }
  44. local lightTable = {
  45.     "torch"
  46. }
  47. local sideTrackMaxLength = 32
  48. local placeTorchAt = 8
  49. local notifications = true
  50. local ignoreErrors = {} -- just add the error nr to the table
  51. local DEBUG = false
  52.  
  53. -- END CONFIG / START CODE -- DO NOT EDIT BELOW
  54.  
  55. local version = "v1.0"
  56. local turtleName = os.getComputerLabel()
  57. local startupMessage = [[
  58. ---------------------------------
  59. - advancedMining ]] .. version .. [[           -
  60. - by ColdIV                     -
  61. - pastebin.com/wbPXakgy         -
  62. ---------------------------------
  63. ]]
  64. local consolePrefix = "[aM " .. version .. "] "
  65.  
  66. local T = turtle
  67. local chatBox = peripheral.wrap("right")
  68.  
  69. local itemPrefix = "minecraft:"
  70. local fuelSlots = {}
  71. local lootSlots = {}
  72. local blockSlots = {}
  73. local lightSlots = {}
  74.  
  75. local wayBack = 0 -- should hold the amount of fuel needed for the way back to the starting point
  76. local onMainTrack = true
  77. local mainTrackLength = 0 -- will increase over time
  78. local lastSideTrack = 0 -- distance since last side track
  79. local sideTrackLength = 0 -- out of sideTrackMaxLength
  80.  
  81. local errors = {}
  82. local errorCodes = {
  83.     ["0"] = "Not enough fuel.",
  84.     ["1"] = "Not enough building blocks.",
  85.     ["2"] = "Not enough light sources."
  86. }
  87.  
  88. function hasError ()
  89.     if DEBUG then print ("start hasError") end
  90.    
  91.     if #errors == 0 then
  92.         if DEBUG then print ("end hasError (no error)") end
  93.        
  94.         return false
  95.     end
  96.    
  97.     local skip = false
  98.     for nr = 1, #errors do
  99.         -- check if error should be ignored
  100.         for i = 1, #ignoreErrors do
  101.             if errors[nr] == ignoreErrors[i] then
  102.                 skip = true
  103.                 break
  104.             end
  105.         end
  106.         -- skip error if it is in ignore list, otherwise print it
  107.         if not skip then
  108.             if notifications and chatBox then
  109.                 chatBox.sendMessage(consolePrefix .. "(" .. turtleName .. ")ERROR: " .. errorCodes[tostring(errors[nr])])
  110.             end
  111.             error(errorCodes[tostring(errors[nr])])
  112.         end
  113.         skip = false
  114.     end
  115.    
  116.     if DEBUG then print ("end hasError (error)") end
  117.    
  118.     return true
  119. end
  120.  
  121. function selectBuildingBlock (selectBlock)
  122.     if DEBUG then print ("start selectBuildingBlock") end
  123.    
  124.     if selectBlock == nil then selectBlock = true end
  125.    
  126.     blockSlots = {}
  127.    
  128.     for slot = 1, 16 do
  129.         local itemDetails = T.getItemDetail(slot)
  130.         if itemDetails then
  131.             for i = 1, #blockTable do
  132.                 if itemDetails.name == itemPrefix .. blockTable[i] then
  133.                     table.insert(blockSlots, slot)
  134.                 end
  135.             end
  136.         end
  137.     end
  138.  
  139.     if #blockSlots == 0 then
  140.         table.insert(errors, 1)
  141.        
  142.         if DEBUG then print ("end selectBuildingBlock") end
  143.        
  144.         return false
  145.     end
  146.    
  147.     if selectBlock then T.select(blockSlots[1]) end
  148.    
  149.     if DEBUG then print ("end selectBuildingBlock") end
  150.    
  151.     return true
  152. end
  153.  
  154. function selectLightBlock (selectBlock)
  155.     if DEBUG then print ("start selectLightBlock") end
  156.    
  157.     if selectBlock == nil then selectBlock = true end
  158.    
  159.     lightSlots = {}
  160.    
  161.     for slot = 1, 16 do
  162.         local itemDetails = T.getItemDetail(slot)
  163.         if itemDetails then
  164.             for i = 1, #lightTable do
  165.                 if itemDetails.name == itemPrefix .. lightTable[i] then
  166.                     table.insert(lightSlots, slot)
  167.                 end
  168.             end
  169.         end
  170.     end
  171.    
  172.     if #lightSlots == 0 then
  173.         table.insert(errors, 2)
  174.        
  175.         if DEBUG then print ("end selectLightBlock") end
  176.        
  177.         return false
  178.     end
  179.    
  180.     if selectBlock then T.select(lightSlots[1]) end
  181.    
  182.     if DEBUG then print ("end selectLightBlock") end
  183.    
  184.     return true
  185. end
  186.  
  187. function checkSlots ()
  188.     if DEBUG then print ("start checkSlots") end
  189.    
  190.     -- checks which slots are being used and for what
  191.     local itemDetails
  192.     fuelSlots = {}
  193.     lootSlots = {}
  194.    
  195.     for slot = 1, 16 do
  196.         local itemDetails = T.getItemDetail(slot)
  197.         if itemDetails then
  198.             for i = 1, #fuelTable do
  199.                 if itemDetails.name == (itemPrefix .. fuelTable[i]) then
  200.                     table.insert(fuelSlots, slot)
  201.                 end
  202.             end
  203.            
  204.             for i = 1, #lootTable do
  205.                 if itemDetails.name == itemPrefix .. lootTable[i] then
  206.                     table.insert(lootSlots, slot)
  207.                 end
  208.             end    
  209.         end
  210.     end
  211.  
  212.     if #fuelSlots == 0 and T.getFuelLevel() - wayBack <= 0 then
  213.         table.insert(errors, 0)
  214.         return false
  215.     end
  216.    
  217.     selectBuildingBlock()
  218.    
  219.     selectLightBlock(false)
  220.    
  221.     if DEBUG then print ("end checkSlots") end
  222.    
  223.     return true
  224. end
  225.  
  226. function refuel ()
  227.     if DEBUG then print ("start refuel") end
  228.    
  229.     print (consolePrefix .. "Refuelling")
  230.     if #fuelSlots == 0 and T.getFuelLevel() - wayBack <= 0 then
  231.         table.insert(errors, 0)
  232.        
  233.         if DEBUG then print ("end refuel (error 1)") end
  234.        
  235.         return false
  236.     end
  237.    
  238.     if #fuelSlots > 0 then
  239.         local currentSlot = T.getSelectedSlot()
  240.         T.select(fuelSlots[1])
  241.         T.refuel()
  242.         T.select(currentSlot)
  243.     else
  244.         if DEBUG then print ("end refuel (error 2)") end
  245.     end
  246.    
  247.     if DEBUG then print ("end refuel") end
  248.    
  249.     return true
  250. end
  251.  
  252. function place (dir)
  253.     if DEBUG then print ("start place") end
  254.  
  255.     dir = dir or ""
  256.     local placeFunc = T.place
  257.     local blockFunc = selectBuildingBlock
  258.     if dir == "up" then
  259.         placeFunc = T.placeUp
  260.     elseif dir == "down" then
  261.         placeFunc = T.placeDown
  262.     elseif dir == "torch" then
  263.         placeFunc = T.placeUp
  264.         blockFunc = selectLightBlock
  265.     end
  266.    
  267.     -- if not placeFunc() then
  268.     blockFunc()
  269.     placeFunc()
  270.     -- end
  271.    
  272.     if DEBUG then print ("end place") end
  273. end
  274.  
  275. function dig (dir)
  276.     if DEBUG then print ("start dig") end
  277.    
  278.     dir = dir or ""
  279.     local has_block, data = T.inspect()
  280.     local digFunc = T.dig
  281.    
  282.     if dir == "up" then
  283.         digFunc = T.digUp
  284.         has_block, data = T.inspectUp()
  285.     elseif dir == "down" then
  286.         digFunc = T.digDown
  287.         has_block, data = T.inspectDown()
  288.     end
  289.    
  290.     if has_block then
  291.         -- check if block is in rare loot table
  292.         if notifications and chatBox then
  293.             for item = 1, #rareLootTable do
  294.                 if itemPrefix .. rareLootTable[item] == data.name then
  295.                     chatBox.sendMessage(consolePrefix .. turtleName .. " found rare item: " .. rareLootTable[item])
  296.                 end
  297.             end
  298.         end
  299.         -- dig
  300.         digFunc()
  301.     end
  302.    
  303.     if DEBUG then print ("end dig") end
  304. end
  305.  
  306. function placeLight ()
  307.     if DEBUG then print ("start placeLight") end
  308.    
  309.     local currentSlot = T.getSelectedSlot()
  310.     place("torch")
  311.     T.select(currentSlot)
  312.    
  313.     if DEBUG then print ("end placeLight") end
  314. end
  315.  
  316. function mining (placeTorch)
  317.     if DEBUG then print ("start mining") end
  318.    
  319.     print (consolePrefix .. "Moving forward")
  320.      -- move forward, if not possible -> dig
  321.     while not T.forward() do
  322.         if T.getFuelLevel() == 0 then
  323.             refuel()
  324.         end
  325.        
  326.         dig()
  327.     end
  328.    
  329.     -- build floor
  330.     place("down")
  331.    
  332.     -- build lower left wall
  333.     T.turnLeft()
  334.     place()
  335.    
  336.     -- move up
  337.     while not T.up() do
  338.         dig("up")
  339.     end
  340.    
  341.     -- build ceiling
  342.     place("up")
  343.    
  344.     -- build upper left wall
  345.     place()
  346.    
  347.     -- build upper right wall
  348.     turnAround()
  349.     place()
  350.    
  351.     -- move down
  352.     T.down()
  353.    
  354.     -- build lower right wall
  355.     place()
  356.    
  357.     -- place light
  358.     if placeTorch then
  359.         print (consolePrefix .. "Placing light")
  360.         placeLight()
  361.     end
  362.    
  363.     -- turn back to path
  364.     T.turnLeft()
  365.    
  366.     if onMainTrack then
  367.         mainTrackLength = mainTrackLength + 1
  368.         lastSideTrack = lastSideTrack + 1
  369.     else sideTrackLength = sideTrackLength + 1 end
  370.    
  371.     if DEBUG then print ("end mining") end
  372. end
  373.  
  374. function turnAround ()
  375.     if DEBUG then print ("start turnAround") end
  376.    
  377.     T.turnRight()
  378.     T.turnRight()
  379.    
  380.     if DEBUG then print ("end turnAround") end
  381. end
  382.  
  383. function move ()
  384.     if DEBUG then print ("start move") end
  385.    
  386.     if onMainTrack and lastSideTrack >= 3 then
  387.         print (consolePrefix .. "Leaving main track")
  388.         onMainTrack = false
  389.         lastSideTrack = 0
  390.         T.turnRight()
  391.     elseif not onMainTrack and sideTrackLength >= sideTrackMaxLength then
  392.         print (consolePrefix .. "Going back to main track")
  393.         turnAround()
  394.         sideTrackLength = 0
  395.         for step = 1, sideTrackMaxLength do
  396.             print (consolePrefix .. tostring(step) .. " of " .. tostring(sideTrackMaxLength))
  397.             T.forward()
  398.         end
  399.         T.turnRight()
  400.         onMainTrack = true
  401.     end
  402.    
  403.     if DEBUG then print ("end move") end
  404. end
  405.  
  406. function returnHome ()
  407.     if DEBUG then print ("start returnHome") end
  408.    
  409.     print (consolePrefix .. "Returning to starting point")
  410.    
  411.     if onMainTrack then
  412.         print (consolePrefix .. "On main track")
  413.         turnAround()
  414.        
  415.         for i = 1, mainTrackLength do
  416.             T.forward()
  417.         end
  418.     else
  419.         print (consolePrefix .. "On side track")
  420.         turnAround()
  421.        
  422.         for i = 1, sideTrackLength do
  423.             T.forward()
  424.         end
  425.        
  426.         T.turnRight()
  427.         onMainTrack = true
  428.         returnHome()
  429.         return
  430.     end
  431.    
  432.     print (consolePrefix .. "I am home! Quitting.")
  433.    
  434.     if DEBUG then print ("end returnHome") end
  435. end
  436.  
  437. function main ()
  438.     if DEBUG then print ("start main") end
  439.    
  440.     print (startupMessage)
  441.    
  442.     if chatBox then print (consolePrefix .. "Chatbox detected") end
  443.    
  444.     local continue = true
  445.    
  446.     -- startup
  447.     continue = checkSlots()
  448.     if continue then continue = refuel() end
  449.     if continue then continue = selectBuildingBlock() end
  450.     continue = not hasError()
  451.    
  452.     -- program loop
  453.     print (consolePrefix .. "Start mining")
  454.     while continue do
  455.         if T.getFuelLevel() == 0 then
  456.             refuel()
  457.         end
  458.        
  459.         move()
  460.         mining((not onMainTrack and (((sideTrackLength + 1) % placeTorchAt == 0) or (sideTrackLength + 1) == 1)))
  461.        
  462.         continue = not hasError()
  463.     end
  464.    
  465.     -- an error occured, return turtle to starting point
  466.     returnHome()
  467.    
  468.     if DEBUG then print ("end main") end
  469. end
  470.  
  471. -- END CODE
  472.  
  473. if args[1] == "update" then
  474.     if args[2] == "run" then
  475.         shell.run("pastebin", "run", "FuQ3WvPs wbPXakgy advancedMining run")
  476.     else
  477.         shell.run("pastebin", "run", "FuQ3WvPs wbPXakgy advancedMining")
  478.     end
  479. else
  480.     -- RUN MAIN PROGRAM
  481.     main()
  482. end
Add Comment
Please, Sign In to add comment