Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --variables
- --len = shaft length
- local len = 1
- --notDig = number of blocks that are not mined in the shaft
- local notDig = 3
- --torch = location of torches
- local torch = 16 - notDig
- --curDist = distance from start
- local curDist = 0
- --top = bool for checking if the turtle is mining the top ot bot level of the shaft
- local top = false
- --run init instead of the main program
- runInit = false
- --isAtHome = remember if you're at home
- isAtHome = true
- --clear the screen
- function clrScr()
- term.clear()
- term.setCursorPos(1,1)
- end
- --argument parsing
- local tArgs = {...}
- if #tArgs > 2 or #tArgs == 0 then
- term.clear()
- print( "Type 'shaft help' for instructions" )
- elseif #tArgs == 1 then
- if tArgs[1] == "help" then
- term.clear()
- print("Usage: shaft [length] [blocks]")
- print("Length is the length of the mining shaft.")
- print("Blocks is the number of blocks that will not be mined [range: 0-10]. By default it's 3 for stone, gravel and dirt and they need to be put in slots 16, 15 and 14")
- print("Enter for more...")
- io.read()
- term.clear()
- print("Torches are placed every 8 blocks and go 1 slot before the ores that won't be mined, by default 13.")
- print("Place chest underneath the starting position.")
- print("use 'shaft init' to make a starting room")
- print("Enter to continue.")
- io.read()
- elseif tArgs[1] == "init" then
- runInit = true
- else
- len = tonumber( tArgs[1] )
- end
- elseif #tArgs == 2 then
- len = tonumber( tArgs[1] )
- notDig = tonumber ( tArgs[2] )
- end
- --refueling
- function refuel(need, message)
- while turtle.getFuelLevel() < (need) do
- for n=1,(16-notDig) do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- refueled = turtle.refuel(1)
- while turtle.getFuelLevel() < need and refueled == true do
- refueled = turtle.refuel(1)
- if turtle.getFuelLevel() > need and refueled == true then
- break
- end
- end
- end
- end
- if turtle.getFuelLevel() < need and isAtHome == true then
- print(message)
- io.read()
- refuel(need,message)
- elseif turtle.getFuelLevel() < need and isAtHome == false then
- goHome()
- refuel(need,message)
- goBack()
- end
- clrScr()
- print("Turtle refuled. Returning to work...")
- end
- end
- --build the starting room
- function initRoom()
- refuel(30, "Please input fuel to continue and press enter.")
- turtle.select(1)
- mineUp()
- digAndMove()
- turtle.turnLeft()
- digAndMove()
- turtle.turnLeft()
- digAndMove()
- digAndMove()
- turtle.digDown()
- turtle.down()
- turtle.turnLeft()
- turtle.turnLeft()
- digAndMove()
- digAndMove()
- turtle.turnRight()
- digAndMove()
- turtle.turnLeft()
- turtle.back()
- turtle.turnLeft()
- turtle.place()
- turtle.turnRight()
- end
- --check if blocks that should not be mined and torches are inserted
- function validateInv()
- clrScr()
- while turtle.getItemCount(torch) < 2 do
- print("")
- print("Please put torches into slot ",torch," and press enter to continue.")
- io.read()
- end
- while not blocksPresent do
- blocksPresent = true
- for i = 17-notDig, 16 do
- if turtle.getItemCount(i) == 0 then blocksPresent = false end
- end
- if not blocksPresent then
- print("Please put at least 1 block into slots ",17-notDig,"-16 for the items you don't want to mine and press enter.")
- io.read()
- end
- end
- clrScr()
- end
- --torch placement
- function placeTorch()
- turtle.select(torch)
- i = curDist
- while i > 0 do
- i = i-8
- end
- if turtle.getItemCount(torch) < 2 then
- clrScr()
- print("Almost out of torches! Returning home...")
- goHome()
- emptyInventory()
- while turtle.getItemCount(torch) < 2 do
- print("")
- print("Please insert torches into slot ",torch," and press enter.")
- io.read()
- end
- refuel(curDist*2 + 95, "Not enough fuel to return to job. Insert fuel and press enter.")
- clrScr()
- print("Turtle ready! Resuming work...")
- goBack()
- end
- if i == 0 then
- turtle.placeUp()
- end
- end
- --return home
- function goHome()
- if top then
- turtle.turnLeft()
- turtle.turnLeft()
- else
- turtle.up()
- end
- for i = 1, curDist do
- turtle.forward()
- end
- turtle.down()
- isAtHome = true
- end
- --go back to work you lazy sucker!
- function goBack()
- turtle.up()
- for i = 1, curDist do
- turtle.back()
- end
- if top then
- turtle.turnLeft()
- turtle.turnLeft()
- else
- turtle.down()
- end
- isAtHome = false
- end
- --check if there's a need to empty the inventory
- function checkInventory()
- drop = 15-notDig
- tempCounter = 0
- for i = 1, drop do
- if turtle.getItemCount(i) > 0 then tempCounter = tempCounter + 1 end
- end
- if tempCounter == drop then
- clrScr()
- print("Inventory full, returning home.")
- goHome()
- emptyInventory()
- refuel(curDist*2+95, "Not enough fuel to return to work. Insert fuel and press enter.")
- clrScr()
- print("Turtle ready! Resuming work...")
- goBack()
- end
- end
- --function that moves forward and digs if there's an obstacle
- function digAndMove()
- while not turtle.forward() do
- sleep(0.5)
- turtle.dig()
- end
- end
- --function to clean inventory
- function emptyInventory()
- turtle.turnRight()
- for i = 1, 15-notDig do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- while not turtle.drop() do
- print("")
- print("Chest full. Please empty chest and press enter to continue.")
- io.read()
- end
- end
- end
- turtle.turnLeft()
- end
- --special case for mining up
- function mineUp()
- while not turtle.up() do
- turtle.digUp()
- sleep(0.5)
- end
- end
- --check for ore below miner
- function checkDown()
- ore = true
- if turtle.detectDown() then
- for i = 17-notDig, 16 do
- turtle.select(i)
- if turtle.compareDown() then
- ore = false
- end
- end
- else
- ore = false
- end
- return ore
- end
- --check for ore above miner
- function checkUp()
- ore = true
- if turtle.detectUp() then
- for i = 17-notDig, 16 do
- turtle.select(i)
- if turtle.compareUp() then
- ore = false
- end
- end
- else
- ore = false
- end
- return ore
- end
- --check for ore in front miner
- function checkFront()
- ore = true
- if turtle.detect() then
- for i = 17-notDig, 16 do
- turtle.select(i)
- if turtle.compare() then
- ore = false
- end
- end
- else
- ore = false
- end
- return ore
- end
- --check the 6 sides around the miner and mine if you find ore, then move in and repeat
- function mineOre()
- if checkDown() then
- turtle.digDown()
- turtle.down()
- mineOre()
- turtle.up()
- end
- if checkUp() then
- mineUp()
- mineOre()
- turtle.down()
- end
- if checkFront() then
- digAndMove()
- mineOre()
- turtle.back()
- end
- turtle.turnRight()
- if checkFront() then
- digAndMove()
- mineOre()
- turtle.back()
- end
- turtle.turnRight()
- if checkFront() then
- digAndMove()
- mineOre()
- turtle.back()
- end
- turtle.turnRight()
- if checkFront() then
- digAndMove()
- mineOre()
- turtle.back()
- end
- turtle.turnRight()
- end
- --main program
- function main()
- clrScr()
- print("Orders received.")
- print("Starting to mine a shaft ",len," long with ",notDig," block exceptions.")
- --validate the inventory before starting
- refuel(95, "Input fuel and press enter!")
- validateInv()
- --go up 1 as we will start mining at the top row
- isAtHome = false
- mineUp()
- mineOre()
- --start diging the top and checking it for ore and refueling if needed
- top = true
- for i = 1, len do
- checkInventory()
- refuel(curDist+95, "Out of fuel, please refil and press enter.")
- digAndMove()
- mineOre()
- curDist = curDist + 1
- end
- --move down to bottom row
- turtle.digDown()
- turtle.down()
- turtle.turnLeft()
- turtle.turnLeft()
- top = false
- mineOre()
- --now dig the bottom while, again, checking for ore and fuel but also place down torches
- for i = 1, (len-1) do
- checkInventory()
- refuel(curDist+95, "Out of fuel, please refil and press enter.")
- digAndMove()
- mineOre()
- curDist = curDist - 1
- placeTorch()
- end
- turtle.forward()
- emptyInventory()
- turtle.turnLeft()
- turtle.turnLeft()
- isAtHome = true
- clrScr()
- print("Work complete!")
- end
- --choose what to run
- clrScr()
- if runInit then
- turtle.select(1)
- while turtle.getItemCount(1) < 1 do
- print("Please insert chest into slot 1 and press enter")
- io.read()
- end
- initRoom()
- clrScr()
- print("Startup location ready!")
- elseif tArgs[1] == "help" then
- print("")
- else
- if len > 0 and notDig < 11 and #tArgs > 0 and #tArgs < 2 then
- main()
- elseif #tArgs > 0 and #tArgs < 2 then
- print("Shaft needs to be at least 1 long and blocks need to be less then 11 or empty.")
- end
- end
Add Comment
Please, Sign In to add comment