soulgriever

Ore Quarry Redux

Oct 8th, 2021 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.84 KB | None | 0 0
  1.  
  2. -- ********************************************************************************** --
  3. -- **                               OreQuarry REDUX                                ** --
  4. -- **   Minecraft Mining Turtle Ore Quarry v0.01 by Soulgriever                    ** --
  5. -- **                                                                              ** --
  6. -- **   Written from scratch but heavily inspired by the original OreQuarry        ** --
  7. -- **     by AustinKK                                                              ** --
  8. -- **                                                                              ** --
  9. -- **   Instructions:                                                              ** --
  10. -- **   OreQuarry <X> <Z> <current Y>                                              ** --
  11. -- **                                                                              ** --
  12. -- **   <X> and <Z> must be even numbers                                           ** --
  13. -- **                                                                              ** --
  14. -- **   <X> How far left and right to be mined, defaultX if not specified at run   ** --
  15. -- **                                                                              ** --
  16. -- **   <Z> How far forward and back to be mined, defaultX if not specified at run ** --
  17. -- **                                                                              ** --
  18. -- **   <Current Y> What layer the turtle is currently located on                  ** --
  19. -- **               Can be omitted if their is a GPS server deployed               ** --
  20. -- **                                                                              ** --
  21. -- **   Slots 1-14:                                                                ** --
  22. -- **   blocks in these slots will be added to the blacklist and the turtle        ** --
  23. -- **   not mine them if above or below. ***See blacklistArray***                  ** --
  24. -- **                                                                              ** --
  25. -- **   Slot 15:                                                                   ** --
  26. -- **     Optional: Fuel EnderChest                                                ** --
  27. -- **     If an enderchest is found in slot 15 the turtle assumes this enderchest  ** --
  28. -- **     contains fuel and will use items inside to refuel if low on fuel         ** --
  29. -- **                                                                              ** --
  30. -- **     Optional: Fuel                                                           ** --
  31. -- **     Fuel can be stored here to refuel                                        ** --
  32. -- **                                                                              ** --
  33. -- **   Slot 16:                                                                   ** --
  34. -- **     Optional: EnderChest                                                     ** --
  35. -- **     If an enderchest is found in slot 16 the turtle assumes this enderchest  ** --
  36. -- **     is for dumping ores into when the turtle's inventory fills               ** --
  37. -- **                                                                              ** --
  38. -- **   Video Instructions:                                                        ** --
  39. -- **   **Will make one eventually**                                               ** --
  40. -- **                                                                              ** --
  41. -- **  Change Log:                                                                 ** --
  42. -- **    8th Oct 2021: [v0.01] Started writing original program                    ** --
  43. -- **  TODO:                                                                       ** --
  44. -- **    Resume support                                                            ** --
  45. -- **    GPS based pathfinding logic                                               ** --
  46. -- **    Fix Bug that breaks program if turtle exits GPS range                     ** --
  47. -- **                                                                              ** --
  48. -- **  Dictionary:                                                                 ** --
  49. -- **  _ = not done yet                                                            ** --
  50. -- **  * = No further changes needed at this time                                  ** --
  51. -- **  ! = Major design change planned                                             ** --
  52. -- **                                                                              ** --
  53. -- ********************************************************************************** --
  54.  
  55.  
  56.  
  57.  
  58.  
  59. -- OreQuarry REDUX config options
  60.  
  61. -- Set this to true to have the turtle ignore GPS servers
  62. gpsDisable = false
  63.  
  64. -- Set this to true if you do not want to have the turtle broadcast updates and remain silent
  65. --   turtle still prints information locally
  66. silentMode = false
  67.  
  68. -- Clears the generated .blacklist file before start equivilant to "delete .blacklist" and loads default values
  69. --  OreQuarry REDUX by default uses a dynamic .blacklist file that is updated every time the program starts but saves previous values
  70. clearBlacklist = false
  71.  
  72. -- Dumps any items mined on the blacklist on the ground during checkInv()
  73. dumpBlacklist = true
  74.  
  75. -- Default blacklist values ***This is ignored after a .blacklist file has been generated unless "clearBlacklist = true"***
  76. local blacklistArray = {
  77.     ["minecraft:air"] = true,
  78.     ["minecraft:bedrock"] = true,
  79.     ["minecraft:cobblestone"] = true,
  80.     ["minecraft:dirt"] = true,
  81.     ["minecraft:ice"] = true,
  82.     ["minecraft:gravel"] = true,
  83.     ["minecraft:ladder"] = true,
  84.     ["minecraft:sand"] = true,
  85.     ["minecraft:snow"] = true,
  86.     ["minecraft:snow_layer"] = true,
  87.     ["minecraft:stone"] = true,
  88.     ["minecraft:grass"] = true,
  89.     ["minecraft:torch"] = true
  90. }
  91.  
  92. --Initial variable values
  93.  
  94. -- Uses these values if X and Z are not specified at program start
  95. --   Must be multiple of 2 (Makes mining faster and simplifies logic required)
  96. defaultX = 6
  97. defaultZ = 6
  98. --What Level bedrock starts at
  99. bedrock = 5
  100. -- Fuel value that the turtle views as low and attempts to refuel or returns to start of quarry
  101. --   may need adjusted depending on quarry size and server fuel requirements
  102. lowFuel = 5000
  103. -- Channel the turtle transmits its broadcast on
  104. channel = 1
  105.  
  106.  
  107. -- ********************************************************************************** --
  108. -- **            No Touchy below this line......Unless you want to                 ** --
  109. -- ********************************************************************************** --
  110.  
  111. --Passes arguements specified at program start to be initialized
  112. local arg = { ... }
  113.  
  114. --*Checks to see if a blacklist file has been generated, imports current blacklist, adds items in slots 1-14 to blacklist
  115. function checkBlacklist()
  116.     if clearBlacklist == false then
  117.         if fs.exists(".blacklist") then
  118.             --Opens the .blacklist file and loads contents to blacklistArray
  119.             local file = fs.open(".blacklist","r")
  120.             local data = file.readAll()
  121.             file.close()
  122.             blacklistArray = textutils.unserialize(data)
  123.         end
  124.         --Compares items in slots 1-14 with blacklistArray adds item to blacklistArray if not already specified
  125.         for i=1,14 do
  126.             turtle.select(i)
  127.             local item = turtle.getItemDetail()
  128.             if item then
  129.                 if blacklistArray[item.name] then
  130.                 else
  131.                 print("Added "..item.name.." to the blacklist!")
  132.                 blacklistArray[item.name] = true
  133.                 end
  134.             end
  135.         end
  136.         --Writes the updated blacklistArray to .blacklist for next run
  137.         local file = fs.open(".blacklist","w")
  138.         file.write(textutils.serialize(blacklistArray))
  139.         file.close()
  140.         turtle.select(16)
  141.     else
  142.         --if clearBlacklist = true deletes previous .blacklist and generates fresh list from table and turtle inventory
  143.         fs.delete(".blacklist")
  144.         clearBlacklist = false
  145.         checkBlacklist()
  146.     end
  147. end
  148.  
  149. --*Attempts to suck items from inventory if turtle has inventory returns to start if not.
  150. function checkChest()
  151.     if turtle.suck() == true then
  152.         goodies = goodies+1
  153.         checkChest()
  154.     else
  155.         if checkInv() > 0 then
  156.             turtle.dig()
  157.             goodies = goodies+1
  158.         else
  159.             quarryStart()
  160.         end
  161.         if turtle.suckUp() == true then
  162.             goodies = goodies+1
  163.             checkChest()
  164.         else
  165.             if checkInv() < 1 then
  166.                 quarryStart()
  167.             end
  168.         end
  169.         if turtle.suckDown() == true then
  170.             goodies = goodies+1
  171.             checkChest()
  172.         else
  173.             if checkInv() < 1 then
  174.                 quarryStart()
  175.             end
  176.         end
  177.     end
  178. end
  179.  
  180. --*Checks the turtles current fuel level and attempts to refuel if low, returns to start if low fuel and no fuel on hand, if their is an enderchest in slot 15 will pull fuel from that
  181. function checkFuel()
  182.     if turtle.getFuelLevel() < lowFuel then
  183.         if enderFuel == true then
  184.             turtle.turnRight()
  185.             turtle.turnRight()
  186.             turtle.select(15)
  187.             turtle.place()
  188.             turtle.suck()
  189.             if turtle.refuel() ~= true then
  190.                 repeat
  191.                     turtle.drop()
  192.                     turtle.suck()
  193.                     if silentMode == false then
  194.                         t = {"Turtle is low on fuel, please refuel the fuel enderchest"}
  195.                         local msg = textutils.serialize(t)
  196.                         m.transmit(channel, channel, msg)
  197.                     end
  198.                 until turtle.refuel() == true
  199.             end
  200.             turtle.dig()
  201.             turtle.select(16)
  202.             turtle.turnRight()
  203.             turtle.turnRight()
  204.         else
  205.             for i=1,16 do
  206.                 turtle.select(i)
  207.                 turtle.refuel()
  208.             end
  209.         end
  210.     end
  211.     return turtle.getFuelLevel()
  212. end
  213.  
  214. --*Checks Turtle's inventory to verify it has space to proceed
  215. function checkInv()
  216.     --Compares previous inv reading to blocks mined since last checkInv was executed
  217.     if inv <= potentialInv then
  218.         updatePos()
  219.         if enderChest == false then
  220.             inv = 0
  221.             for i=1,14 do
  222.                 turtle.select(i)
  223.                 if turtle.getItemDetail() == nil then
  224.                     inv = inv + 1
  225.                 end
  226.             end
  227.             if inv < 2 then
  228.             --Dumps items on the blacklist from inventory if enabled
  229.                 if dumpBlacklist == true then
  230.                     for i=1,14 do
  231.                         turtle.select(i)
  232.                         local data = turtle.getItemDetail()
  233.                         if data then
  234.                             if blacklistArray[data.name] then
  235.                                 turtle.drop()
  236.                             end
  237.                         end
  238.                     end
  239.                     checkInv()
  240.                 end
  241.             end
  242.             turtle.select(16)
  243.             potentialInv = 0
  244.         else
  245.             inv = 0
  246.             for i=1,14 do
  247.                 turtle.select(i)
  248.                 if turtle.getItemDetail() == nil then
  249.                     inv = inv + 1
  250.                 end
  251.             end
  252.             if inv < 2 then
  253.                 --Dumps items on the blacklist from inventory if enabled
  254.                 if dumpBlacklist == true then
  255.                     for i=1,14 do
  256.                         turtle.select(i)
  257.                         local data = turtle.getItemDetail()
  258.                         if data then
  259.                             if blacklistArray[data.name] then
  260.                                 turtle.drop()
  261.                             end
  262.                         end
  263.                     end
  264.                 end
  265.                 enderDump()
  266.                 checkInv()
  267.             end
  268.             turtle.select(16)
  269.             potentialInv = 0
  270.         end
  271.     end
  272.     return inv
  273. end
  274.  
  275. --!The sweet sauce, Main function that executes logic to dig the current level the turtle is on
  276. function digLevel()
  277.     if state ~= 1 then
  278.         state = 1
  279.         local h = fs.open(".lastState", "w")
  280.         h.write("1")
  281.         h.close()
  282.     end
  283.     curX = initX-1
  284.     curZ = initZ-1
  285.     while curX>0 or curZ>0 do
  286.         if checkInv() > 1 and checkFuel() > lowFuel then
  287.             if curZ > 0 then
  288.                 insp()
  289.                 if turtle.forward() == false then
  290.                     repeat
  291.                         turtle.attack()
  292.                     until turtle.forward() == true
  293.                 end
  294.                 curZ = curZ-1
  295.                 updatePos()
  296.             end        
  297.             if curZ == 0 then
  298.                 if curX > 0 then
  299.                     if curX % 2 == 0 then
  300.                         turtle.turnLeft()
  301.                         insp2()
  302.                         if turtle.forward() == false then
  303.                             repeat
  304.                                 turtle.attack()
  305.                             until turtle.forward() == true
  306.                         end
  307.                        
  308.                         turtle.turnLeft()
  309.                     else
  310.                         turtle.turnRight()
  311.                         insp2()
  312.                         turtle.forward()
  313.                         turtle.turnRight()
  314.                     end
  315.                     curX = curX-1
  316.                     curZ = initZ-1
  317.                     updatePos()
  318.                 else
  319.                     levelStart()
  320.                 end
  321.             end
  322.         else
  323.             quarryStart()
  324.         end
  325.     end
  326. end
  327.  
  328. --*Places the enderchest in slot 16 down empyties contents and picks it back up
  329. function enderDump()
  330.     turtle.turnRight()
  331.     turtle.turnRight()
  332.     turtle.select(16)
  333.     if turtle.place() == false then
  334.         repeat
  335.             turtle.dig()
  336.             goodies = goodies+1
  337.         until turtle.place() == true
  338.     end
  339.  
  340.     for i=1,14 do
  341.         turtle.select(i)
  342.         if turtle.getItemDetail() then
  343.             if turtle.drop() ~= true then
  344.                 print("Enderchest full returning to quarry start")
  345.                 turtle.select(16)
  346.                 turtle.dig()
  347.                 quarryStart()
  348.             end
  349.         end
  350.     end
  351.     turtle.select(16)
  352.     turtle.dig()
  353.     turtle.turnRight()
  354.     turtle.turnRight()
  355. end
  356.  
  357. --*Startup function that checks for previous in progress dig and runs first time variable values
  358. function initialize()
  359.     sleep(2)
  360.     --Checks if turtle has a wireless modem and wraps the peripheral
  361.     if peripheral.getType("left") == "modem" then
  362.         m = peripheral.wrap("left")
  363.     elseif peripheral.getType("right") == "modem" then
  364.         m = peripheral.wrap("right")
  365.     else
  366.         silentMode = true
  367.     end
  368.    
  369.     if fs.exists(".lastPosition") then
  370.         resume = true
  371.         print("Resuming previous dig")
  372.         if silentMode == false then
  373.             t = {"Resuming previous dig"}
  374.             local msg = textutils.serialize(t)
  375.             m.transmit(channel, channel, msg)  
  376.         end
  377.     else
  378.        
  379.         --Checks for GPS and set's current position based on GPS coordinates
  380.         if gps.locate() and gpsDisable == false then
  381.             x,y,z = gps.locate()
  382.             if silentMode == false then
  383.                 gpsBroadcast = true
  384.             else
  385.                 gpsBroadcast = false
  386.             end
  387.         end
  388.        
  389.         --Checks arguements provided on startup and sets initial X,Y,Z values
  390.         if y and gpsDisable == false then
  391.             if arg[1] and arg[2] then
  392.                 if arg[1] % 2 == 0 and arg[2] % 2 == 0 then
  393.                     initX = arg[1]
  394.                     initZ = arg[2]
  395.                     initY = y
  396.                     print("GPS Server detected")
  397.                     print("Currently Location is X:"..x.." Y:"..y.." Z:"..z)
  398.                     print("Mining "..initX.." X "..initZ.." area")
  399.                 else
  400.                     print("X and Z value's must be even")
  401.                     error()
  402.                 end
  403.             else
  404.                 if arg[1] then
  405.                     if arg[1] % 2 == 0 then
  406.                         initX = arg[1]
  407.                         initZ = arg[1]
  408.                         initY = y
  409.                         print("GPS Server detected")
  410.                         print("Currently Location is X:"..x.." Y:"..y.." Z:"..z)
  411.                         print("Mining "..initX.." X "..initZ.." area")
  412.                     else
  413.                         print("X and Z value's must be even")
  414.                         error()
  415.                     end
  416.                    
  417.                 else
  418.                     initX = defaultX
  419.                     initZ = defaultZ
  420.                     initY = y
  421.                     print("GPS Server detected")
  422.                     print("Currently Location is X:"..x.." Y:"..y.." Z:"..z)
  423.                     print("Mining "..initX.." X "..initZ.." area")
  424.                 end
  425.             end
  426.         else
  427.             if arg[1] and arg[2] and arg[3] then
  428.                 if arg[1] % 2 == 0 and arg[2] % 2 == 0 then
  429.                     initX = arg[1]
  430.                     initZ = arg[2]
  431.                     initY = arg[3]
  432.                     print("Currently on layer:"..initY)
  433.                     print("Mining "..initX.." X "..initZ.." area")
  434.                 else
  435.                     print("X and Z value's must be even")
  436.                     error()
  437.                 end
  438.             else
  439.                 if arg[1] and arg[2] then
  440.                     if arg[1] % 2 == 0 then
  441.                         initX = arg[1]
  442.                         initZ = arg[1]
  443.                         initY = arg[2]
  444.                         print("Currently on layer:"..initY)
  445.                         print("Mining "..initX.." X "..initZ.." area")
  446.                     else
  447.                         print("X and Z value's must be even")
  448.                         error()
  449.                     end
  450.                 else
  451.                     if arg[1] then
  452.                         initX = defaultX
  453.                         initZ = defaultZ
  454.                         initY = arg[1]
  455.                         print("Currently on layer:"..initY)
  456.                         print("Mining "..initX.." X "..initZ.." area")
  457.                     else
  458.                         print("Error!")
  459.                         print("Could not determine current height.")
  460.                         print("Please specify height or deploy a GPS server.")
  461.                         error()
  462.                     end
  463.                 end
  464.             end
  465.         end
  466.         print()
  467.         --Checks for an enderchest in slot 15 to pull fuel from
  468.         turtle.select(15)
  469.         if turtle.getItemDetail() then
  470.             if turtle.getItemDetail().name == "minecraft:ender_chest" or turtle.getItemDetail().name == "enderstorage:ender_storage" then
  471.                 enderFuel = true
  472.                 print("EnderChest detected in Fuel slot")
  473.                 print("Enabling EnderFuel")
  474.                 print()
  475.             else
  476.                 enderFuel = false
  477.             end
  478.         end
  479.        
  480.         --Checks for an enderchest in slot 16 to dump items into
  481.         turtle.select(16)
  482.         if turtle.getItemDetail() then
  483.             if turtle.getItemDetail().name == "minecraft:ender_chest" or turtle.getItemDetail().name == "enderstorage:ender_storage" then
  484.                 enderChest = true
  485.                 print("EnderChest detected in Chest slot")
  486.                 print("Enabling EnderDump")
  487.                 print()
  488.             else
  489.                 enderChest = false
  490.             end
  491.         end
  492.         --Declares starting value of rolling variable potential inventory
  493.         potentialInv = 0
  494.         inv = 0
  495.         --Keep's track of how many items have been mined (Not unload tolerant)
  496.         goodies = 0
  497.        
  498.         checkBlacklist()
  499.         print("Variables Set")
  500.         print("Starting new dig")
  501.         if silentMode == false then
  502.             t = {"Starting new dig"}
  503.             local msg = textutils.serialize(t)
  504.             m.transmit(channel, channel, msg)  
  505.         end
  506.         sleep(5)
  507.         updatePos()
  508.     end
  509. end
  510.  
  511. --*Inspects the block infront, above, and below
  512. function insp()
  513.     local I, data = turtle.inspect()
  514.     if I then
  515.         repeat
  516.             local I, data = turtle.inspect()
  517.             if data.name ~= "minecraft:water" or data.name ~= "minecraft:lava" then
  518.                 if data.name == "minecraft:chest" then
  519.                     checkChest()
  520.                 else
  521.                     turtle.dig()
  522.                     goodies = goodies+1
  523.                     potentialInv = potentialInv + 1
  524.                 end
  525.             end
  526.         until I == false or data.name == "minecraft:water" or data.name == "minecraft:lava"
  527.     end
  528.     local I, data = turtle.inspectUp()
  529.     if I then
  530.         if data.name ~= "minecraft:water" or data.name ~= "minecraft:lava" then
  531.             if data.name == "minecraft:chest" then
  532.                 checkChest()
  533.             else
  534.                 if blacklistArray[data.name] then
  535.                 else
  536.                     turtle.digUp()
  537.                     goodies = goodies+1
  538.                     potentialInv = potentialInv + 1
  539.                 end
  540.             end
  541.         end
  542.     end
  543.     local I, data = turtle.inspectDown()
  544.     if I then
  545.         if data.name ~= "minecraft:water" or data.name ~= "minecraft:lava" then
  546.             if data.name == "minecraft:chest" then
  547.                 checkChest()
  548.             else
  549.                 if blacklistArray[data.name] then
  550.                 else
  551.                     turtle.digDown()
  552.                     goodies = goodies+1
  553.                     potentialInv = potentialInv + 1
  554.                 end
  555.             end
  556.         end
  557.     end
  558. end
  559.  
  560. --*Clone of Inspect but does not execute a sanity check incase of full inventory when turning corner before position is updated
  561. function insp2()
  562.     local I, data = turtle.inspect()
  563.     if I then
  564.         repeat
  565.         local I, data = turtle.inspect()
  566.             if data.name ~= "minecraft:water" or data.name ~= "minecraft:lava" then
  567.                 if data.name == "minecraft:chest" then 
  568.                     while turtle.suck() == true do
  569.                         turtle.suck()
  570.                     end
  571.                     turtle.dig()
  572.                     goodies = goodies+1
  573.                     potentialInv = potentialInv + 1
  574.                 else
  575.                     turtle.dig()
  576.                     goodies = goodies+1
  577.                     potentialInv = potentialInv + 1
  578.                 end
  579.             end
  580.         until I == false or data.name == "minecraft:water" or data.name == "minecraft:lava"
  581.     end
  582.     local I, data = turtle.inspectUp()
  583.     if I then
  584.         if data.name ~= "minecraft:water" or data.name ~= "minecraft:lava" then
  585.             if data.name == "minecraft:chest" then
  586.                 while turtle.suckUp() == true do
  587.                     turtle.suckUp()
  588.                 end
  589.             else
  590.                 if blacklistArray[data.name] then
  591.                 else
  592.                     turtle.digUp()
  593.                     goodies = goodies+1
  594.                     potentialInv = potentialInv + 1
  595.                 end
  596.             end
  597.         end
  598.     end
  599.     local I, data = turtle.inspectDown()
  600.     if I then
  601.         if data.name ~= "minecraft:water" or data.name ~= "minecraft:lava" then
  602.             if data.name == "minecraft:chest" then
  603.                 while turtle.suckDown() == true do
  604.                     turtle.suckDown()
  605.                 end
  606.             else
  607.                 if blacklistArray[data.name] then
  608.                 else
  609.                     turtle.digDown()
  610.                     goodies = goodies+1
  611.                     potentialInv = potentialInv + 1
  612.                 end
  613.             end
  614.         end
  615.     end
  616. end
  617.  
  618. --!Saves current location and returns turtle to the beginning of current level
  619. function levelStart()
  620.     if state ~= 2 then
  621.         state = 2
  622.         local h = fs.open(".lastState", "w")
  623.         h.write("2")
  624.         h.close()
  625.     end
  626.     if curX % 2 == 0 then
  627.         turtle.turnRight()
  628.         savedZ = curZ
  629.     else
  630.         turtle.turnLeft()
  631.         savedZ = initZ-curZ-1
  632.     end
  633.     savedX = initX-curX-1
  634.     for i=1, savedX do
  635.         if turtle.forward() == false then
  636.             repeat
  637.                 turtle.attack()
  638.                 turtle.dig()
  639.                 goodies = goodies+1
  640.             until turtle.forward() == true
  641.         end
  642.         savedX = savedX-1
  643.         updatePos()
  644.     end
  645.     turtle.turnLeft()
  646.     if curZ > 0 then
  647.         for i=1, savedZ do
  648.             if turtle.forward() == false then
  649.                 repeat
  650.                     turtle.attack()
  651.                     turtle.dig()
  652.                     goodies = goodies+1
  653.                 until turtle.forward() == true
  654.             end
  655.             savedZ = savedZ-1
  656.             updatePos()
  657.         end
  658.     end
  659.     turtle.turnRight()
  660.     turtle.turnRight()
  661. end
  662.  
  663. --_Returns turtle to the beginning of the quarry to empty inventory
  664. function quarryStart()
  665.     if state ~= 3 then
  666.         state = 3
  667.         local h = fs.open(".lastState", "w")
  668.         h.write("3")
  669.         h.close()
  670.     end
  671.     print("Quarry Start")
  672.     levelStart()
  673. end
  674.  
  675. --_Returns turtle to the current digLevel before its inventory was filled
  676. function pathToCurrentLevel()
  677.     if state ~= 4 then
  678.         state = 4
  679.         local h = fs.open(".lastState", "w")
  680.         h.write("4")
  681.         h.close()
  682.     end
  683. end
  684.  
  685. --_Returns turtle to the current progress it was at before its inventory was filled
  686. function pathToCurrentPosition()
  687.     if state ~= 5 then
  688.         state = 5
  689.         local h = fs.open(".lastState", "w")
  690.         h.write("5")
  691.         h.close()
  692.     end
  693. end
  694.  
  695. --*Moves the turtle up to the next level to be mined, exits program at starting point
  696. function pathToNextLevel()
  697.     --Checks to see if turtle is above starting point
  698.     if curY + bedrock <= initY then
  699.         --Checks to see if turtle can go up to next full level
  700.         if curY + bedrock + 3 <= initY then
  701.             for i=1, 3 do
  702.                 if turtle.up() == false then
  703.                     repeat
  704.                         turtle.digUp()
  705.                         goodies = goodies+1
  706.                         turtle.attackUp()
  707.                     until turtle.up() == true
  708.                 end
  709.                 curY = curY + 1
  710.             end
  711.         --Checks to see if turtle can go up a partial level
  712.         elseif curY + bedrock + 2 <= initY then
  713.             for i=1, 2 do
  714.                 if turtle.up() == false then
  715.                     repeat
  716.                         turtle.digUp()
  717.                         goodies = goodies+1
  718.                         turtle.attackUp()
  719.                     until turtle.up() == true
  720.                 end
  721.                 curY = curY + 1
  722.             end
  723.         --If turtle is 1 block below starting point goes to starting point
  724.         elseif curY + bedrock + 1 == initY then
  725.             if turtle.up() == false then
  726.                 repeat
  727.                     turtle.digUp()
  728.                     goodies = goodies+1
  729.                     turtle.attackUp()
  730.                 until turtle.up() == true
  731.             end
  732.             curY = curY + 1
  733.             term.clear()
  734.             term.setCursorPos(1,1)
  735.             print("Mining Finished!")
  736.             print()
  737.             print("This turtle mined "..goodies.." items")
  738.             error()
  739.         else
  740.             term.clear()
  741.             term.setCursorPos(1,1)
  742.             print("Mining Finished!")
  743.             print()
  744.             print("This turtle mined "..goodies.." items")
  745.             error()
  746.         end
  747.     else
  748.         term.clear()
  749.         term.setCursorPos(1,1)
  750.         print("Mining Finished!")
  751.         print()
  752.         print("This turtle mined "..goodies.." items")
  753.         error()
  754.     end
  755. end
  756.  
  757. --*Moves the turtle down to the bottom level
  758. function pathToStart()
  759.     if state ~= 0 then
  760.         state = 0
  761.         local h = fs.open(".lastState", "w")
  762.         h.write("0")
  763.         h.close()
  764.     end
  765.     curY = initY-bedrock
  766.     for i=1, curY do
  767.         if turtle.down() == false then
  768.             repeat
  769.                 turtle.digDown()
  770.                 goodies = goodies+1
  771.                 turtle.attackDown()
  772.                 potentialInv = potentialInv + 1
  773.             until turtle.down() == true
  774.         end
  775.         curY = curY - 1
  776.     end
  777.     updatePos()
  778. end
  779.  
  780. --_Resumes mining if the turtle gets unloaded
  781. function resumePreviousDig()
  782.     local file = fs.open(".initialPosition","r")
  783.     local data = file.readAll()
  784.     file.close()
  785.     local ini = textutils.unserialize(data)
  786.     initX = ini[1]
  787.     initY = ini[2]
  788.     initZ = ini[3]
  789.    
  790.     local file = fs.open(".lastPosition","r")
  791.     local data = file.readAll()
  792.     file.close()
  793.     local pos = textutils.unserialize(data)
  794.     curX  = pos[1]
  795.     curY  = pos[2]
  796.     curZ  = pos[3]
  797.    
  798.     local file = fs.open(".lastState","r")
  799.     local data = file.readAll()
  800.     file.close()
  801.     state = data
  802. end
  803.  
  804. --!Updates the current position to a backup file incase of unload and broadcast's position if silentMode = false
  805. function updatePos()
  806.     term.clear()
  807.     term.setCursorPos(1,1)
  808.     if gpsBroadcast == true then
  809.         --Broadcast's current GPS coordinates
  810.         x,y,z = gps.locate()
  811.         t = {x,y,z,goodies}
  812.         local msg = textutils.serialize(t)
  813.         m.transmit(channel, channel, msg)
  814.         print("Current Position is")
  815.         print("X:"..x.." Y:"..y.." Z:"..z)
  816.         print("Goodies Mined:"..goodies)
  817.     elseif gpsBroadcast == false and silentMode == false then
  818.         --Broadcast's current Y
  819.         trueY = curY+bedrock
  820.         m.transmit(channel, channel, trueY)
  821.         print("Current Layer:"..trueY)
  822.         print("X:"..x.." Y:"..y.." Z:"..z)
  823.         print("Goodies Mined:"..goodies)
  824.     else
  825.         trueY = curY+bedrock
  826.         print("Current Layer:"..trueY)
  827.         print("Goodies Mined:"..goodies)
  828.     end
  829. end
  830.  
  831.  
  832. --Program Started
  833.  
  834.  
  835. term.clear()
  836. term.setCursorPos(1,1)
  837. print("           ___  ___  ___")                  
  838. print("          / _ \\| _ \\| __|")                
  839. print("         | (_) |   /| _|")                  
  840. print("          \\___/|_|_\\|___| v0.01")                
  841. print("   ___  _   _  _   ___ _____   __")
  842. print("  / _ \\| | | |/_\\ | _ \\ _ \\ \\ / /")
  843. print(" | (_) | |_| / _ \\|   /   /\\ V /")
  844. print("  \\__\\_\\\\___/_/ \\_\\_|_\\_|_\\ |_|")
  845. print()
  846. print("       REDUX! by. Soulgriever")
  847. print()
  848.  
  849. initialize()
  850. if resume == true then
  851.     resumePreviousDig()
  852. else
  853.     pathToStart()
  854.     while true do
  855.         digLevel()
  856.         pathToNextLevel()
  857.     end
  858. end
Add Comment
Please, Sign In to add comment