Thunder7102

BranchMiner

Jun 17th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.58 KB | None | 0 0
  1. --[[
  2.     BranchMiner is meant to be a persisting
  3.     mining which will dig out large areas under ground.
  4.     It will automatically consume coal if it needs it before
  5.     placing it into a chest.
  6.  
  7.     This is meant to be an entry-level program. When a sorting system
  8.     and lots of fuel are available, other programs may be more appropriate.
  9. ]]
  10.  
  11. --Let's get the needed API I will be using
  12. if fs.exists("api/move") then
  13.     os.loadAPI("api/move")
  14. else
  15.     --Insert download code here
  16. end
  17.  
  18. local version = 1.0
  19. local chestSlot = 1
  20. local torchSlot = 2
  21. local cobbleSlot = 3
  22. local branchDepth = 30
  23. local decompress = 0
  24. local away = false
  25. local int_status = {"init", "", ""}
  26. local status = "Starting"
  27.  
  28. --Known Locations I will be accessing
  29. local home = {0,0,0,"north"}
  30. local chestLoc = {0,0,-2,"north"}
  31.  
  32. --Functions
  33. function buildMain(depth)
  34.     --[[
  35.         Builds a 3x3x3 area and ensures all sides are sealed in appropriately.
  36.         Persists.
  37.     ]]
  38.  
  39.     --Ensures proper location is always turtled to at first.
  40.     while not turtle.turtleTo(0,0,turtle.getZ(), "south") do end
  41.  
  42.     for i=1,depth do
  43.         int_status = {"buildMain", i}
  44.         saveData()
  45.         status = "Tunneling"..i.."/"..depth
  46.         spaceCheck()
  47.         --Digs out the 3x3 tunnel
  48.         turtle.fUp(2)
  49.         turtle.fForward()
  50.         placeCobble("up")
  51.         --Will start digging down the left side
  52.         turtle.turnLeft()
  53.         turtle.fForward()
  54.         placeCobble("up")
  55.         placeCobble()
  56.         turtle.fDown()
  57.         placeCobble()
  58.         turtle.fDown()
  59.         placeCobble()
  60.         placeCobble("down")
  61.         --At bottom, turning around
  62.         turtle.turnAround()
  63.         turtle.fFoward()
  64.         placeCobble("down")
  65.         turtle.fUp(2)
  66.         --Begin working on right side
  67.         turtle.fForward()
  68.         placeCobble("up")
  69.         placeCobble()
  70.         turtle.fDown()
  71.         placeCobble()
  72.         turtle.fDown()
  73.         placeCobble()
  74.         placeCobble("down")
  75.         --Getting back into place
  76.         turtle.turnAround()
  77.         turtle.fForward()
  78.         turtle.turnToDir("north")
  79.     end
  80.  
  81.     return true
  82. end
  83.  
  84. function buildBranch(depth, dir)
  85.     --Creates a 3-high branch from the main mine
  86.     turtle.turtleTo(0,1,turtle.getZ(),turtle.turnToDir(dir))
  87.     turtle.fForward()
  88.     torchDist = 5
  89.  
  90.     for i=0, depth do
  91.         int_status = {branch, i, turtle.getDir()}
  92.         saveData()
  93.         spaceCheck()
  94.         turtle.fForward()
  95.         spaceCheck()
  96.         turtle.digUp()
  97.         spaceCheck()
  98.         turtle.digDown()
  99.        
  100.         --For keeping branches lit
  101.         torchDist = torchDist+1
  102.         if torchDist == 7 then
  103.             if placeTorch("down") then torchDist = 0 end
  104.         end
  105.     end
  106.  
  107.     --Note that something may break using an untested fturtleTo.
  108.     turtle.fturtleTo(0,0,turtle.getZ(),"north", "y", "x", "z")
  109.     return true
  110. end
  111.  
  112. function mainLoop(...)
  113.     --The full loop of the code which goes on until you run out of resources.
  114.     local nxtAct = "buildMain"
  115.     local torchDist = 0
  116.     local arg = {...}
  117.    
  118.     --This allows me to modularize the entire process.
  119.     if arg[1] == "buildMain" or arg[1] == "buildBranch" then
  120.         nxtAct = arg[1]
  121.     end
  122.  
  123.     while true do
  124.         if nxtAct == "buildMain" then
  125.             buildMain(3)
  126.             nxtAct = "buildBranch"
  127.            
  128.             --This handles so the main channel has plenty of lighting. Branches will not be lit to save on resources.
  129.             torchDist = torchDist + 4
  130.             if torchDist > 4 then
  131.                 turtle.turnLeft()
  132.                 placeTorch()
  133.                 torchDist = 0
  134.             end
  135.         elseif nxtAct == "buildBranch" then
  136.             buildBranch(branchDepth, "east")
  137.             buildBranch(branchDepth, "west")
  138.             nxtAct = "buildMain"
  139.         end
  140.     end
  141. end
  142.  
  143. --Utility Functions
  144. function spaceCheck()
  145.     if turtle.getItemCount(16) > 0 then
  146.         decompressInv()
  147.     end
  148. end
  149.  
  150. function resume()
  151.     --This is made to resume previous action being performed
  152.     if not away then return false end
  153.  
  154.     turtle.turtleTo(home, "y", "x", "z")
  155.     turtle.turnAround()
  156.     while turtle.forward() do end
  157.  
  158.     if int_status[1] == "branch" then
  159.         turtle.turnToDir(int_status[3])
  160.         turtle.forward(int_status[2])
  161.         mainLoop("branch", int_status[2], int_status[3])
  162.     elseif int_status[1] == "buildMain" then
  163.         mainLoop("buildMain")
  164.     elseif int_status[1] == "require" then
  165.         --I would look into this to ensure resuming a require can proper handle it.
  166.         requireItem(int_status[4], int_status[5], int_status[6])
  167.     elseif int_status[1] == "emptyChest" then
  168.         emptyChest()
  169.     elseif int_status[1] == "init" then
  170.         setup()
  171.         mainLoop()
  172.     end
  173.     away = false
  174.     --Link to mainLoop
  175. end
  176.  
  177. function requireItem(invSlot, message, ...)
  178.     --Goes to the base and waits for user intervention
  179.     --asking for a specific needed item
  180.     local args = {...}
  181.     int_main[4] = int_main[1]
  182.     int_main[5] = int_main[2]
  183.     int_main[6] = int_main[3]
  184.  
  185.     int_main[1] = "require"
  186.     int_main[2] = invSlot
  187.     int_main[3] = message
  188.     away = true
  189.     saveData()
  190.     while not turtle.turtleTo(home, "y", "x", "z") do end
  191.  
  192.     while not turtle.getItemCount(invSlot) > 1 do
  193.         term.clear()
  194.         term.setCursorPos(1,1)
  195.         print(message)
  196.         sleep(3)
  197.     end
  198.  
  199.     --This restores what was initially there.
  200.     if #args > 0 then
  201.         int_status[1] = args[1]
  202.         int_status[2] = args[2]
  203.         saveData()
  204.     else
  205.         int_main[1] = int_main[4]
  206.         int_main[2] = int_main[5]
  207.         int_main[3] = int_main[6]
  208.  
  209.         saveData()
  210.     end
  211.  
  212.     turtle.turtleTo(home, "y", "x", "z")
  213.     turtle.turnAround()
  214.     while turtle.forward() do end
  215.     --if int_main[1] == "buildBranch" then
  216. end
  217.  
  218. function emptyChest(...)
  219.     local args = {...}
  220.     if #int_main > 1 then
  221.         int_main[2] = int_main[1]
  222.         int_main[3] = int_main[2]
  223.     end
  224.     int_main[1] = "emptyChest"
  225.     away = true
  226.     saveData()
  227.     --Here is actual chestCode
  228.     turtle.turtleTo(chestLoc, "y", "x", "z")
  229.  
  230.     --This ensures that the turtle has a chest to place into.
  231.     if not turtle.detectUp() then
  232.         turtle.select(chestSlot)
  233.         if turtle.getItemCount(chestSlot) < 2 then
  234.             requireItem(chestSlot, "Please put more chests in the chest slot.")
  235.         end
  236.         turtle.placeUp()
  237.     end
  238.  
  239.     --Here is the actual removal loop
  240.     for i=4, 16 do
  241.         --Don't need no freaking cobble.
  242.         turtle.select(i)
  243.         if turtle.comare(cobbleSlot) then turtle.dropDown(turtle.getItemCount(i)) end
  244.         --Keep himself fueled up
  245.         if turtle.getFuelLevel() < 5000 and turtle.refuel(1) then
  246.             if turtle.getItemCount(i) > 0 then turtle.refuel(turtle.getItemCount(i)) end
  247.         end
  248.  
  249.         if turtle.getItemCount(i) > 0 then
  250.             --This ensures the chest still has room to take items
  251.             if not turtle.dropUp() then
  252.                 --This will start a new chest row
  253.                 chestLoc = {0,0,getZ-2,"north"}
  254.                 emptyChest()
  255.             end
  256.         end
  257.     end
  258.  
  259.     --This restores what was initially there.
  260.     if #args > 0 then
  261.         int_status[1] = args[1]
  262.         int_status[2] = args[2]
  263.         saveData()
  264.     else
  265.         int_main[1] = int_main[4]
  266.         int_main[2] = int_main[5]
  267.         saveData()
  268.     end
  269.     resume()
  270. end
  271.  
  272. function decompressInv()
  273.     for i=4, 16 do
  274.         if turtle.compare(cobbleSlot) then
  275.             turtle.dropDown(turtle.getItemCount(i))
  276.         end
  277.     end
  278.     --So it does not infini-loop
  279.     if decompress == 3 then
  280.         emptyChest()
  281.         decompress = 0
  282.     end
  283. end
  284.  
  285. function placeTorch(...)
  286.     local args = {...}
  287.     if args[1] == nil then args[1] = "forward" end
  288.  
  289.     if turtle.getItemCount(torchSlot) < 2 then
  290.         print("Out of torches.")
  291.         requireItem(torchSlot, "Please provide more torches in the specified slot.")
  292.     end
  293.  
  294.     if args[1] == "forward" then
  295.         turtle.select(torchSlot)
  296.         if not turtle.place() then  
  297.             return false
  298.         end
  299.     elseif args[1] == "up" then
  300.         turtle.select(torchSlot)
  301.         if not turtle.placeUp() then    
  302.             return false
  303.         end
  304.     elseif args[1] == "down" then
  305.         turtle.select(torchSlot)
  306.         if not turtle.placeDown() then
  307.             return false
  308.         end
  309.     else
  310.         print("Something went really bad with placeTorch... o.O args[1]: "..args[1])
  311.     end
  312.  
  313.     turtle.select(1)
  314.     return true
  315. end
  316.  
  317. function placeCobble(...)
  318.     local args = {...}
  319.     if args[1] == nil then args[1] = "forward" end
  320.  
  321.     if turtle.getItemCount(cobbleSlot) < 2 then
  322.         print("Out of cobble. Bridging may be incomplete, continueing anyway.")
  323.         return false
  324.     end
  325.  
  326.     if args[1] == "forward" then
  327.         turtle.select(cobbleSlot)
  328.         if not turtle.place() then  
  329.             return false
  330.         end
  331.     elseif args[1] == "up" then
  332.         turtle.select(cobbleSlot)
  333.         if not turtle.placeUp() then    
  334.             return false
  335.         end
  336.     elseif args[1] == "down" then
  337.         turtle.select(cobbleSlot)
  338.         if not turtle.placeDown() then
  339.             return false
  340.         end
  341.     else
  342.         print("Something went really bad with placeTorch... o.O args[1]: "..args[1])
  343.     end
  344.  
  345.     turtle.select(1)
  346.     return true
  347. end
  348.  
  349. function saveData()
  350.     local f = fs.open("branchPersist", "w")
  351.     f.write(textutils.serialize(int_status))
  352.     f.close()
  353.  
  354.     if fs.exists("startup") and not fs.exists("branchInStart") then
  355.         fs.turtle("startup", "branchInStart")
  356.         f = fs.open("startup", "w")
  357.         f.write("shell.run(\"shell.getRunningProgram()\")")
  358.         f.close()
  359.     end
  360. end
  361.  
  362. local function loadData()
  363.     --Keeps past coords from derping the program
  364.     if not fs.exists("branchPersist") then
  365.          turtle.setCoords(0,0,0,"north")
  366.          return true
  367.     else
  368.         --This ensures that if the user picks up the turtle, it automatically flushes its settings
  369.         --and calls itself done
  370.         if turtle.getItemCount(1) == 0 then
  371.             if fs.exists("branchInStart") then
  372.                 if fs.exists("startup") then
  373.                     fs.delete("startup")
  374.                 end
  375.                 fs.turtle("branchInStart", "startup")
  376.             end
  377.             return false
  378.         end
  379.         print("Restarting...")
  380.         f = fs.open("branchPersist", "r")
  381.         int_status = textutils.unserialize(f.readAll())
  382.         resume()
  383.     end
  384. end
  385.  
  386. local function setup()
  387.     local allReady = false
  388.     while not allReady do
  389.         term.clear()
  390.         term.setCursorPos(1,1)
  391.         print("BRANCH MINER v"..version)
  392.         print("Please enter a stack of cobble in slot: "..cobbleSlot)
  393.         print("please enter a stack of torches in slot: "..torchSlot)
  394.         print("Please place some chests in slot: "..chestSlot)
  395.         print("Note:  If I need more materials while mining, I'll come and wait right here facing you so you can refill.")
  396.         print("<Place all required items and hit any key to continue.>")
  397.         read()
  398.         if turtle.getItemCount(1) > 0 and turtle.getItemCount(1) > 0 and turtle.getItemCount(1) > 0 then
  399.             print("I'm assuming you entered the correct items into the slot. Beginning...")
  400.             while turtle.getFuelLevel() < 3000 do
  401.                 term.clear()
  402.                 term.setCursorPos(1,1)
  403.                 print("I only have "..turtle.getFuelLevel().."/3000. Put some in slot 4 and hit any key.")
  404.                 read()
  405.                 turtle.select(4)
  406.                 turtle.refuel(turtle.getItemCount(4))
  407.             end
  408.             allReady = true
  409.             turtle.select(1)
  410.         end
  411.     end
  412.     print("Note: This program will run forever unless it runs out of fuel (which should not happen if enough is started with). When it comes and sits here for materials, that is also the perfect opportunity to quit if you so desire.")
  413.     print("Hit any key to being.")
  414.     read()
  415. end
  416.  
  417. local function init()
  418.     --Handles all the wonderful persist-y stuff
  419.     if not loadData() then return end
  420.     if not fs.exists("branchPersist") then
  421.         saveData()
  422.     end
  423.     resume()
  424. end
Advertisement
Add Comment
Please, Sign In to add comment