Advertisement
wv1106

Mining script computercraft

Dec 26th, 2021 (edited)
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.36 KB | None | 0 0
  1. local facing = "N"  -- variables ( don't change pls =[ )
  2. local whitelist = {"minecraft:coal_ore", "", "", "", "", "", "", "", "", "", "", ""}
  3. local position = {0, 0, 0}
  4. local nodes = {}
  5. local moveHistory = {}
  6. local NNode = false
  7. local loc
  8. local lastTorch = 10
  9. local install = false
  10. local minerPath = "miner.lua"
  11. local whitelistPath = "programs/assetsMiner/whitelist"
  12. local uninstallPath = "programs/assetsMiner/uninstaller.lua"
  13. local assetsPath = "programs/assetsMiner/"
  14. local scroll = 1
  15. local distance = 50
  16. local key
  17. local event
  18. local points = 1
  19. local chestD = 0
  20.  
  21. -- 1 refueling and misc
  22.  
  23. -- 1.1 refueling
  24. function refuel()
  25.     if turtle.getFuelLevel() < 10 then
  26.         for i=1,16 do
  27.             local itemD = turtle.getItemDetail(i)
  28.             if not (itemD==nil) then
  29.                 if itemD.name=="minecraft:coal" or itemD.name=="minecraft:coal_block" or itemD.name=="minecraft:charcoal" or itemD.name=="minecraft:lava_bucket" then
  30.                     while turtle.getFuelLevel() < 1000 and itemD.count > 0 do
  31.                         turtle.select(i)
  32.                         turtle.refuel()
  33.                     end
  34.                     turtle.select(1)
  35.                     break
  36.                 end
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. -- 1.2 torch placing
  43. function placeTorch()
  44.     lastTorch = lastTorch+1
  45.     if lastTorch>=11 then
  46.         lastTorch=0
  47.         for i=1,16 do
  48.             local itemD = turtle.getItemDetail(i)
  49.             if not (itemD==nil) then
  50.                 if itemD.name=="minecraft:torch" then
  51.                     turtle.select(i)
  52.                     South()
  53.                     turtle.place()
  54.                     North()
  55.                     turtle.select(1)
  56.                     break
  57.                 end
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. function readWhitelist()        -- 1.3 for reading the whitelist file
  64.     if fs.exists("disk/whitelist") then
  65.         local file = fs.open("disk/whitelist", "r")
  66.         whitelist = textutils.unserialise(file.readAll())
  67.         file.close()
  68.     else
  69.         local file = fs.open(whitelistPath, "r")
  70.         whitelist = textutils.unserialise(file.readAll())
  71.         file.close()
  72.     end
  73. end
  74.  
  75. function writeWhitelist()       -- 1.4 for writing the whitelist file
  76.     local file = fs.open(whitelistPath, "w")
  77.     file.write(textutils.serialise(whitelist))
  78.     file.close()
  79. end
  80.  
  81. function inventoryManag()
  82.     local filledSlots = 0
  83.     for i=1,16 do
  84.         if turtle.getItemCount(i)>0 then
  85.             filledSlots = filledSlots + 1
  86.         end
  87.     end
  88.     if filledSlots == 16 then
  89.         South()
  90.         digDown()
  91.         downward()
  92.        
  93.         for i=1,chestD do
  94.             forward()
  95.         end
  96.         local block, data = turtle.inspect()
  97.         while not block do
  98.             block, data = turtle.inspect()
  99.             inventoryFull()
  100.         end
  101.         for i=1,16 do
  102.             local itemD = turtle.getItemDetail(i)
  103.             if not (itemD==nil) then
  104.                 if not(itemD.name=="minecraft:torch" or itemD.name=="minecraft:coal" or itemD.name=="minecraft:coal_block" or itemD.name=="minecraft:charcoal" or itemD.name=="minecraft:lava_bucket") then
  105.                     while turtle.getItemCount(i) > 0 do
  106.                         turtle.select(i)
  107.                         turtle.drop()
  108.                     end
  109.                 end
  110.             end
  111.             turtle.select(1)
  112.         end
  113.         North()
  114.         for i=1,chestD do
  115.             forward()
  116.         end
  117.         upward()
  118.     end
  119. end
  120.  
  121.  
  122.  
  123.  
  124. -- 2 movement
  125.  
  126. -- 2.1 facing
  127. function North()
  128.     if facing == "E" then
  129.         turtle.turnLeft()
  130.     elseif facing == "S" then
  131.         turtle.turnLeft()
  132.         turtle.turnLeft()
  133.     elseif facing == "W" then
  134.         turtle.turnRight()
  135.     end
  136.     facing = "N"
  137. end
  138.  
  139. function East()
  140.     if facing == "S" then
  141.         turtle.turnLeft()
  142.     elseif facing == "W" then
  143.         turtle.turnLeft()
  144.         turtle.turnLeft()
  145.     elseif facing == "N" then
  146.         turtle.turnRight()
  147.     end
  148.     facing = "E"
  149. end
  150.  
  151. function South()
  152.     if facing == "W" then
  153.         turtle.turnLeft()
  154.     elseif facing == "N" then
  155.         turtle.turnLeft()
  156.         turtle.turnLeft()
  157.     elseif facing == "E" then
  158.         turtle.turnRight()
  159.     end
  160.     facing = "S"
  161. end
  162.  
  163. function West()
  164.     if facing == "N" then
  165.         turtle.turnLeft()
  166.     elseif facing == "E" then
  167.         turtle.turnLeft()
  168.         turtle.turnLeft()
  169.     elseif facing == "S" then
  170.         turtle.turnRight()
  171.     end
  172.     facing = "W"
  173. end
  174.  
  175. -- 2.2 move commands so it doesn't get confused
  176.  
  177. function forward()
  178.     while not turtle.forward() do
  179.         refuel()
  180.         turtle.attack()
  181.     end
  182. end
  183. function upward()
  184.     while not turtle.up() do
  185.         refuel()
  186.         turtle.attackUp()
  187.     end
  188. end
  189. function downward()
  190.     while not turtle.down() do
  191.         refuel()
  192.         turtle.attackDown()
  193.     end
  194. end
  195.  
  196. -- 2.3dig commands so it's gravel and sand safe
  197. function dig()
  198.     while turtle.detect() do
  199.         turtle.dig()
  200.         miningAnimation()
  201.     end
  202. end
  203.  
  204. function digUp()
  205.     while turtle.detectUp() do
  206.         turtle.digUp()
  207.         miningAnimation()
  208.     end
  209. end
  210.  
  211. function digDown()
  212.     while turtle.detectDown() do
  213.         turtle.digDown()
  214.         miningAnimation()
  215.     end
  216. end
  217.  
  218. -- 3.0 the DFS stuff (depth first search https://en.wikipedia.org/wiki/Depth-first_search#:~:text=Depth%2Dfirst%20search%20(DFS),along%20each%20branch%20before%20backtracking.)
  219.  
  220. function addNode(Npos)  -- 3.1 adds the coords of an ore if there aren't any double's
  221.     for i=1,#nodes do
  222.         if sameList(Npos, nodes[i]) then
  223.             return
  224.         end
  225.     end
  226.     table.insert(nodes, #nodes+1, Npos)
  227. end
  228.  
  229. function inspect()  -- 3.2 checks if the turtle can see a whitelisted ore/ block
  230.     local block, data = turtle.inspectUp()
  231.     if block then
  232.         for i=1,#whitelist do
  233.             if data.name==whitelist[i] then
  234.                 addNode({position[1], position[2]+1, position[3]})
  235.             end
  236.         end
  237.     end
  238.     local block, data = turtle.inspectDown()
  239.     if block then
  240.         for i=1,#whitelist do
  241.             if data.name==whitelist[i] then
  242.                 addNode({position[1], position[2]-1, position[3]})
  243.             end
  244.         end
  245.     end
  246.     North()
  247.     miningAnimation()
  248.     local block, data = turtle.inspect()
  249.     if block then
  250.         for i=1,#whitelist do
  251.             if data.name==whitelist[i] then
  252.                 addNode({position[1]+1, position[2], position[3]})
  253.             end
  254.         end
  255.     end
  256.     East()
  257.     miningAnimation()
  258.     local block, data = turtle.inspect()
  259.     if block then
  260.         for i=1,#whitelist do
  261.             if data.name==whitelist[i] then
  262.                 addNode({position[1], position[2], position[3]+1})
  263.             end
  264.         end
  265.     end
  266.     South()
  267.     miningAnimation()
  268.     local block, data = turtle.inspect()
  269.     if block then
  270.         for i=1,#whitelist do
  271.             if data.name==whitelist[i] then
  272.                 addNode({position[1]-1, position[2], position[3]})
  273.             end
  274.         end
  275.     end
  276.     West()
  277.     miningAnimation()
  278.     local block, data = turtle.inspect()
  279.     if block then
  280.         for i=1,#whitelist do
  281.             if data.name==whitelist[i] then
  282.                 addNode({position[1], position[2], position[3]-1})
  283.             end
  284.         end
  285.     end
  286.    
  287. end
  288.  
  289. function sameList(a, b) -- 3.3 checks if 2 lists have the same value
  290.     return textutils.serialise(a)==textutils.serialise(b)  
  291. end
  292.  
  293. function besidesNode()  -- 3.4 checks if the turtle is beside a whitelisted ore / block
  294.     if #nodes>0 then
  295.         if sameList(position,{nodes[#nodes][1], nodes[#nodes][2]-1, nodes[#nodes][3]}) then
  296.             return true, "U"
  297.         elseif sameList(position, {nodes[#nodes][1], nodes[#nodes][2]+1, nodes[#nodes][3]}) then
  298.             return true, "D"
  299.         elseif sameList(position, {nodes[#nodes][1]-1, nodes[#nodes][2], nodes[#nodes][3]}) then
  300.             return true, "N"
  301.         elseif sameList(position, {nodes[#nodes][1], nodes[#nodes][2], nodes[#nodes][3]-1}) then
  302.             return true, "E"
  303.         elseif sameList(position, {nodes[#nodes][1]+1, nodes[#nodes][2], nodes[#nodes][3]}) then
  304.             return true, "S"
  305.         elseif sameList(position, {nodes[#nodes][1], nodes[#nodes][2], nodes[#nodes][3]+1}) then
  306.             return true, "W"
  307.         else
  308.             return false
  309.         end
  310.     else
  311.         return false
  312.     end
  313. end
  314.  
  315. -- 4 gui
  316.  
  317. function whitelistEditor()    -- 4.1 the whitelist gui
  318.     if not fs.exists(whitelistPath) then
  319.         term.clear()
  320.         term.setCursorPos(5, 2)
  321.         term.write("Install the necessary things?")
  322.         term.setCursorPos(15, 5)
  323.         term.write(" install ")
  324.         term.setCursorPos(16, 6)
  325.         term.write(">cancel<")
  326.         term.setCursorPos(1, 10)
  327.         while not fs.exists(whitelistPath) do
  328.             event, key = os.pullEvent("key")
  329.             if key==keys.up then
  330.                 term.clear()
  331.                 term.setCursorPos(5, 2)
  332.                 term.write("Install the necessary things?")
  333.                 term.setCursorPos(15, 5)
  334.                 term.write(">install<")
  335.                 term.setCursorPos(16, 6)
  336.                 term.write(" cancel ")
  337.                 install = true
  338.             elseif key==keys.down then
  339.                 term.clear()
  340.                 term.setCursorPos(5, 2)
  341.                 term.write("Install the necessary things?")
  342.                 term.setCursorPos(15, 5)
  343.                 term.write(" install ")
  344.                 term.setCursorPos(16, 6)
  345.                 term.write(">cancel<")
  346.                 install = false
  347.             elseif key==keys.enter then
  348.                 if install==true then
  349.                     local file = fs.open(whitelistPath, "w")
  350.                     file.write(textutils.serialise(whitelist))
  351.                     file.close()
  352.                     local file = fs.open(uninstallPath, "w")
  353.                     file.write('shell.run("delete '..whitelistPath..'")')
  354.                     file.write('shell.run("delete '..minerPath..'")')
  355.                     file.write('shell.run("delete '..uninstallPath..'")')
  356.                     file.close()
  357.                 elseif install==false then
  358.                     local file = fs.open(uninstallPath, "w")
  359.                     file.write('shell.run("delete '..minerPath..'")')
  360.                     file.write('shell.run("delete '..uninstallPath..'")')
  361.                     file.close()
  362.                     shell.run(uninstallPath)
  363.                 end
  364.             end
  365.         end
  366.     end
  367.     readWhitelist()
  368.  
  369.     while true do
  370.         if key==keys.up then
  371.             scroll = scroll-1
  372.         elseif key==keys.down then
  373.             scroll = scroll+1
  374.         elseif key==keys.enter then
  375.         end
  376.  
  377.         if scroll>14 then
  378.             scroll=1
  379.         elseif scroll<1 then
  380.             scroll=14
  381.         end
  382.         term.setBackgroundColor(colors.gray)
  383.         term.clear()
  384.         term.setCursorPos(1, 13)
  385.         term.setTextColour(colors.white)
  386.         term.setBackgroundColor(colors.red)
  387.         term.write(" exit ")
  388.         term.setCursorPos(33, 13)
  389.         term.setBackgroundColor(colors.green)
  390.         term.write(" start ")
  391.        
  392.         for i=1,12 do
  393.             term.setBackgroundColor(colors.gray)
  394.             if i%2==0 then
  395.                 paintutils.drawLine(1, i, 39, i, colors.lightGray)
  396.                 term.setBackgroundColor(colors.lightGray)
  397.             end
  398.             term.setCursorPos(1, i)
  399.             term.write(whitelist[i])
  400.         end
  401.         if scroll>0 and scroll<13 then
  402.             paintutils.drawLine(1, scroll, 39, scroll, colors.lime)
  403.             term.setBackgroundColor(colors.lime)
  404.             term.setCursorPos(1, scroll)
  405.             term.write(whitelist[scroll])
  406.             if key==keys.enter then
  407.                 term.setCursorPos(1, scroll)
  408.                 term.clearLine(scroll)
  409.                 whitelist[scroll] = read()
  410.             end
  411.         end
  412.         if scroll==13 then
  413.             term.setCursorPos(1, 13)
  414.             term.setBackgroundColor(colors.lime)
  415.             term.write(" exit ")
  416.             if key==keys.enter then
  417.                 writeWhitelist()
  418.                 os.reboot()
  419.             end
  420.         end
  421.        
  422.         if scroll==14 then
  423.             term.setCursorPos(33, 13)
  424.             term.setBackgroundColor(colors.lime)
  425.             term.write(" start ")
  426.             if key==keys.enter then
  427.                 return
  428.             end
  429.         end
  430.         event, key = os.pullEvent("key")
  431.         print(key)
  432.     end
  433. end
  434.  
  435. function distanceEditor()   -- 4.2 menu to specify the distance menu
  436.     key=keys.numPad9
  437.     while true do
  438.         if key==keys.up then
  439.             distance = distance + 10
  440.         elseif key==keys.down then
  441.             distance = distance - 10
  442.         elseif key==keys.enter then
  443.             break
  444.         end
  445.         if distance<10 then
  446.             distance=10
  447.         end
  448.         term.clear()
  449.         paintutils.drawFilledBox(1, 1, 39, 19, colors.black)
  450.         term.setTextColor(colors.white)
  451.         term.setCursorPos(10, 3)
  452.         term.write("specify the distance")
  453.         term.setCursorPos(18, 4)
  454.         term.write(">"..distance.."<")
  455.         if distance==420 then
  456.             term.setCursorPos(5, 7)
  457.             term.clearLine(7)
  458.             term.setCursorPos(8, 7)
  459.             term.setTextColor(colors.red)
  460.             term.write("420... nice")
  461.         elseif distance>2000 then
  462.             term.setCursorPos(5, 7)
  463.             term.setTextColor(colors.red)
  464.             term.write("why do I even try")
  465.         elseif distance>1000 then
  466.             term.setCursorPos(5, 7)
  467.             term.setTextColor(colors.red)
  468.             term.write("dude chill!!!")
  469.         elseif distance>50 then
  470.             term.setCursorPos(5, 7)
  471.             term.setTextColor(colors.red)
  472.             term.write("! caution for unloaded chunks !")
  473.         else
  474.             term.setCursorPos(5, 7)
  475.             term.clearLine(7)
  476.         end
  477.  
  478.         event, key = os.pullEvent("key")
  479.     end
  480. end
  481.  
  482.  
  483. function miningAnimation()  -- 4.3 a (somewhat) nice mining animation
  484.     term.setBackgroundColor(colors.lime)
  485.     points = points + 1
  486.     if points>3 then
  487.         points = 1
  488.     end
  489.     term.clear()
  490.     term.setCursorPos(16, 7)
  491.     term.write("mining")
  492.     for i=1,points do
  493.         term.write(".")
  494.     end
  495. end
  496.  
  497. function inventoryFull()    -- 4.4 displays if your inventory is full and can't find a sotrage thingy
  498.     paintutils.drawFilledBox(1, 1, 39, 19, colors.red)
  499.     term.setBackgroundColor(colors.red)
  500.     term.setCursorPos(12, 7)
  501.     term.write("inventory full")
  502. end
  503.  
  504. function finished()
  505.     term.clear()
  506.     paintutils.drawFilledBox(1, 1, 39, 19, colors.orange)
  507.     term.setCursorPos(12, 7)
  508.     term.write("finished")
  509.     os.pullEvent()
  510.     os.reboot()
  511. end
  512.  
  513. -- 5 the main code
  514. whitelistEditor()
  515. writeWhitelist()
  516. distanceEditor()
  517. upward()
  518.  
  519. for length=1,distance do
  520.     digUp()     --does basic mining stuff
  521.     digDown()
  522.     dig()
  523.     forward()
  524.     chestD = chestD+1
  525.     placeTorch()
  526.     inventoryManag()
  527.  
  528.     inspect()   -- checks for whitelisted ores
  529.     if #nodes>0 then
  530.         while not (sameList(position, {0, 0, 0}) and #nodes==0) do
  531.             inspect()
  532.             NNode, loc = besidesNode()
  533.             if NNode then
  534.                 if loc=="U" then    --digs nearby nodes/ores
  535.                     digUp()
  536.                     upward()
  537.                     position[2] = position[2] + 1
  538.                     table.insert(moveHistory, #moveHistory+1, "U")
  539.                 elseif loc=="D" then
  540.                     digDown()
  541.                     downward()
  542.                     position[2] = position[2] - 1
  543.                     table.insert(moveHistory, #moveHistory+1, "D")
  544.                 elseif loc=="N" then
  545.                     North()
  546.                     dig()
  547.                     forward()
  548.                     position[1] = position[1] + 1
  549.                     table.insert(moveHistory, #moveHistory+1, "N")
  550.                 elseif loc=="E" then
  551.                     East()
  552.                     dig()
  553.                     forward()
  554.                     position[3] = position[3] + 1
  555.                     table.insert(moveHistory, #moveHistory+1, "E")
  556.                 elseif loc=="S" then
  557.                     South()
  558.                     dig()
  559.                     forward()
  560.                     position[1] = position[1] - 1
  561.                     table.insert(moveHistory, #moveHistory+1, "S")
  562.                 elseif loc=="W" then
  563.                     West()
  564.                     dig()
  565.                     forward()
  566.                     position[3] = position[3] - 1
  567.                     table.insert(moveHistory, #moveHistory+1, "W")
  568.                 end
  569.                 table.remove(nodes, #nodes)
  570.             else
  571.                 while (not NNode) and (not sameList(position, {0, 0, 0})) do -- backtracks to last node or starting position
  572.                     NNode, loc = besidesNode()
  573.                     if moveHistory[#moveHistory] == "U" then
  574.                         downward()
  575.                         position[2] = position[2] - 1
  576.                     elseif moveHistory[#moveHistory] == "D" then
  577.                         upward()
  578.                         position[2] = position[2] + 1
  579.                     elseif moveHistory[#moveHistory] == "N" then
  580.                         South()
  581.                         forward()
  582.                         position[1] = position[1] - 1
  583.                     elseif moveHistory[#moveHistory] == "E" then
  584.                         West()
  585.                         forward()                      
  586.                         position[3] = position[3] - 1
  587.                     elseif moveHistory[#moveHistory] == "S" then
  588.                         North()
  589.                         forward()
  590.                         position[1] = position[1] + 1
  591.                     elseif moveHistory[#moveHistory] == "W" then
  592.                         East()
  593.                         forward()
  594.                         position[3] = position[3] + 1
  595.                     end
  596.                     table.remove(moveHistory, #moveHistory)
  597.                     NNode, loc = besidesNode()
  598.                 end
  599.             end
  600.            
  601.         end
  602.     end
  603.     North()
  604. end
  605.  
  606. South()
  607. digDown()
  608. downward()
  609. for length=1,distance do
  610.     forward()
  611. end
  612. finished()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement