Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- BranchMiner is meant to be a persisting
- mining which will dig out large areas under ground.
- It will automatically consume coal if it needs it before
- placing it into a chest.
- This is meant to be an entry-level program. When a sorting system
- and lots of fuel are available, other programs may be more appropriate.
- ]]
- --Let's get the needed API I will be using
- if fs.exists("api/move") then
- os.loadAPI("api/move")
- else
- --Insert download code here
- end
- local version = 1.0
- local chestSlot = 1
- local torchSlot = 2
- local cobbleSlot = 3
- local branchDepth = 30
- local decompress = 0
- local away = false
- local int_status = {"init", "", ""}
- local status = "Starting"
- --Known Locations I will be accessing
- local home = {0,0,0,"north"}
- local chestLoc = {0,0,-2,"north"}
- --Functions
- function buildMain(depth)
- --[[
- Builds a 3x3x3 area and ensures all sides are sealed in appropriately.
- Persists.
- ]]
- --Ensures proper location is always turtled to at first.
- while not turtle.turtleTo(0,0,turtle.getZ(), "south") do end
- for i=1,depth do
- int_status = {"buildMain", i}
- saveData()
- status = "Tunneling"..i.."/"..depth
- spaceCheck()
- --Digs out the 3x3 tunnel
- turtle.fUp(2)
- turtle.fForward()
- placeCobble("up")
- --Will start digging down the left side
- turtle.turnLeft()
- turtle.fForward()
- placeCobble("up")
- placeCobble()
- turtle.fDown()
- placeCobble()
- turtle.fDown()
- placeCobble()
- placeCobble("down")
- --At bottom, turning around
- turtle.turnAround()
- turtle.fFoward()
- placeCobble("down")
- turtle.fUp(2)
- --Begin working on right side
- turtle.fForward()
- placeCobble("up")
- placeCobble()
- turtle.fDown()
- placeCobble()
- turtle.fDown()
- placeCobble()
- placeCobble("down")
- --Getting back into place
- turtle.turnAround()
- turtle.fForward()
- turtle.turnToDir("north")
- end
- return true
- end
- function buildBranch(depth, dir)
- --Creates a 3-high branch from the main mine
- turtle.turtleTo(0,1,turtle.getZ(),turtle.turnToDir(dir))
- turtle.fForward()
- torchDist = 5
- for i=0, depth do
- int_status = {branch, i, turtle.getDir()}
- saveData()
- spaceCheck()
- turtle.fForward()
- spaceCheck()
- turtle.digUp()
- spaceCheck()
- turtle.digDown()
- --For keeping branches lit
- torchDist = torchDist+1
- if torchDist == 7 then
- if placeTorch("down") then torchDist = 0 end
- end
- end
- --Note that something may break using an untested fturtleTo.
- turtle.fturtleTo(0,0,turtle.getZ(),"north", "y", "x", "z")
- return true
- end
- function mainLoop(...)
- --The full loop of the code which goes on until you run out of resources.
- local nxtAct = "buildMain"
- local torchDist = 0
- local arg = {...}
- --This allows me to modularize the entire process.
- if arg[1] == "buildMain" or arg[1] == "buildBranch" then
- nxtAct = arg[1]
- end
- while true do
- if nxtAct == "buildMain" then
- buildMain(3)
- nxtAct = "buildBranch"
- --This handles so the main channel has plenty of lighting. Branches will not be lit to save on resources.
- torchDist = torchDist + 4
- if torchDist > 4 then
- turtle.turnLeft()
- placeTorch()
- torchDist = 0
- end
- elseif nxtAct == "buildBranch" then
- buildBranch(branchDepth, "east")
- buildBranch(branchDepth, "west")
- nxtAct = "buildMain"
- end
- end
- end
- --Utility Functions
- function spaceCheck()
- if turtle.getItemCount(16) > 0 then
- decompressInv()
- end
- end
- function resume()
- --This is made to resume previous action being performed
- if not away then return false end
- turtle.turtleTo(home, "y", "x", "z")
- turtle.turnAround()
- while turtle.forward() do end
- if int_status[1] == "branch" then
- turtle.turnToDir(int_status[3])
- turtle.forward(int_status[2])
- mainLoop("branch", int_status[2], int_status[3])
- elseif int_status[1] == "buildMain" then
- mainLoop("buildMain")
- elseif int_status[1] == "require" then
- --I would look into this to ensure resuming a require can proper handle it.
- requireItem(int_status[4], int_status[5], int_status[6])
- elseif int_status[1] == "emptyChest" then
- emptyChest()
- elseif int_status[1] == "init" then
- setup()
- mainLoop()
- end
- away = false
- --Link to mainLoop
- end
- function requireItem(invSlot, message, ...)
- --Goes to the base and waits for user intervention
- --asking for a specific needed item
- local args = {...}
- int_main[4] = int_main[1]
- int_main[5] = int_main[2]
- int_main[6] = int_main[3]
- int_main[1] = "require"
- int_main[2] = invSlot
- int_main[3] = message
- away = true
- saveData()
- while not turtle.turtleTo(home, "y", "x", "z") do end
- while not turtle.getItemCount(invSlot) > 1 do
- term.clear()
- term.setCursorPos(1,1)
- print(message)
- sleep(3)
- end
- --This restores what was initially there.
- if #args > 0 then
- int_status[1] = args[1]
- int_status[2] = args[2]
- saveData()
- else
- int_main[1] = int_main[4]
- int_main[2] = int_main[5]
- int_main[3] = int_main[6]
- saveData()
- end
- turtle.turtleTo(home, "y", "x", "z")
- turtle.turnAround()
- while turtle.forward() do end
- --if int_main[1] == "buildBranch" then
- end
- function emptyChest(...)
- local args = {...}
- if #int_main > 1 then
- int_main[2] = int_main[1]
- int_main[3] = int_main[2]
- end
- int_main[1] = "emptyChest"
- away = true
- saveData()
- --Here is actual chestCode
- turtle.turtleTo(chestLoc, "y", "x", "z")
- --This ensures that the turtle has a chest to place into.
- if not turtle.detectUp() then
- turtle.select(chestSlot)
- if turtle.getItemCount(chestSlot) < 2 then
- requireItem(chestSlot, "Please put more chests in the chest slot.")
- end
- turtle.placeUp()
- end
- --Here is the actual removal loop
- for i=4, 16 do
- --Don't need no freaking cobble.
- turtle.select(i)
- if turtle.comare(cobbleSlot) then turtle.dropDown(turtle.getItemCount(i)) end
- --Keep himself fueled up
- if turtle.getFuelLevel() < 5000 and turtle.refuel(1) then
- if turtle.getItemCount(i) > 0 then turtle.refuel(turtle.getItemCount(i)) end
- end
- if turtle.getItemCount(i) > 0 then
- --This ensures the chest still has room to take items
- if not turtle.dropUp() then
- --This will start a new chest row
- chestLoc = {0,0,getZ-2,"north"}
- emptyChest()
- end
- end
- end
- --This restores what was initially there.
- if #args > 0 then
- int_status[1] = args[1]
- int_status[2] = args[2]
- saveData()
- else
- int_main[1] = int_main[4]
- int_main[2] = int_main[5]
- saveData()
- end
- resume()
- end
- function decompressInv()
- for i=4, 16 do
- if turtle.compare(cobbleSlot) then
- turtle.dropDown(turtle.getItemCount(i))
- end
- end
- --So it does not infini-loop
- if decompress == 3 then
- emptyChest()
- decompress = 0
- end
- end
- function placeTorch(...)
- local args = {...}
- if args[1] == nil then args[1] = "forward" end
- if turtle.getItemCount(torchSlot) < 2 then
- print("Out of torches.")
- requireItem(torchSlot, "Please provide more torches in the specified slot.")
- end
- if args[1] == "forward" then
- turtle.select(torchSlot)
- if not turtle.place() then
- return false
- end
- elseif args[1] == "up" then
- turtle.select(torchSlot)
- if not turtle.placeUp() then
- return false
- end
- elseif args[1] == "down" then
- turtle.select(torchSlot)
- if not turtle.placeDown() then
- return false
- end
- else
- print("Something went really bad with placeTorch... o.O args[1]: "..args[1])
- end
- turtle.select(1)
- return true
- end
- function placeCobble(...)
- local args = {...}
- if args[1] == nil then args[1] = "forward" end
- if turtle.getItemCount(cobbleSlot) < 2 then
- print("Out of cobble. Bridging may be incomplete, continueing anyway.")
- return false
- end
- if args[1] == "forward" then
- turtle.select(cobbleSlot)
- if not turtle.place() then
- return false
- end
- elseif args[1] == "up" then
- turtle.select(cobbleSlot)
- if not turtle.placeUp() then
- return false
- end
- elseif args[1] == "down" then
- turtle.select(cobbleSlot)
- if not turtle.placeDown() then
- return false
- end
- else
- print("Something went really bad with placeTorch... o.O args[1]: "..args[1])
- end
- turtle.select(1)
- return true
- end
- function saveData()
- local f = fs.open("branchPersist", "w")
- f.write(textutils.serialize(int_status))
- f.close()
- if fs.exists("startup") and not fs.exists("branchInStart") then
- fs.turtle("startup", "branchInStart")
- f = fs.open("startup", "w")
- f.write("shell.run(\"shell.getRunningProgram()\")")
- f.close()
- end
- end
- local function loadData()
- --Keeps past coords from derping the program
- if not fs.exists("branchPersist") then
- turtle.setCoords(0,0,0,"north")
- return true
- else
- --This ensures that if the user picks up the turtle, it automatically flushes its settings
- --and calls itself done
- if turtle.getItemCount(1) == 0 then
- if fs.exists("branchInStart") then
- if fs.exists("startup") then
- fs.delete("startup")
- end
- fs.turtle("branchInStart", "startup")
- end
- return false
- end
- print("Restarting...")
- f = fs.open("branchPersist", "r")
- int_status = textutils.unserialize(f.readAll())
- resume()
- end
- end
- local function setup()
- local allReady = false
- while not allReady do
- term.clear()
- term.setCursorPos(1,1)
- print("BRANCH MINER v"..version)
- print("Please enter a stack of cobble in slot: "..cobbleSlot)
- print("please enter a stack of torches in slot: "..torchSlot)
- print("Please place some chests in slot: "..chestSlot)
- print("Note: If I need more materials while mining, I'll come and wait right here facing you so you can refill.")
- print("<Place all required items and hit any key to continue.>")
- read()
- if turtle.getItemCount(1) > 0 and turtle.getItemCount(1) > 0 and turtle.getItemCount(1) > 0 then
- print("I'm assuming you entered the correct items into the slot. Beginning...")
- while turtle.getFuelLevel() < 3000 do
- term.clear()
- term.setCursorPos(1,1)
- print("I only have "..turtle.getFuelLevel().."/3000. Put some in slot 4 and hit any key.")
- read()
- turtle.select(4)
- turtle.refuel(turtle.getItemCount(4))
- end
- allReady = true
- turtle.select(1)
- end
- end
- 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.")
- print("Hit any key to being.")
- read()
- end
- local function init()
- --Handles all the wonderful persist-y stuff
- if not loadData() then return end
- if not fs.exists("branchPersist") then
- saveData()
- end
- resume()
- end
Advertisement
Add Comment
Please, Sign In to add comment