Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --variables
- --len = shaft length
- --notDig = number of blocks that are not mined in the shaft
- --torch = location of torches
- --curDist = distance from start
- --heightPos = current relative height
- --runProgram = what program has been selected for the turtle
- --isAtHome = remember if you're at home
- --height = shaft height
- --miningLog = info about where the vein the miner is following
- --curDir = current direction (in relative terms, 1 is forward, 2 is right, 3 is back, 4 is left)
- local len = 1
- local notDig = 3
- local torch = 16 - notDig
- local curDist = 0
- local heightPos = 0
- local runProgram = "main"
- local isAtHome = true
- local height = 2
- local miningLog = {}
- local curDir = 1
- --clear the screen
- function clrScr()
- term.clear()
- term.setCursorPos(1,1)
- end
- --argument parsing
- local tArgs = {...}
- if #tArgs > 3 or #tArgs == 0 then
- print( "Type 'shaft help' for instructions" )
- elseif #tArgs == 1 then
- if tonumber(tArgs[1]) ~= nil then
- runProgram = "main"
- len = tonumber( tArgs[1] )
- else
- runProgram = tArgs[1]
- end
- elseif #tArgs == 3 then
- if tonumber(tArgs[1]) ~= nil or tonumber(tArgs[2]) ~= nil or tonumber(tArgs[3]) ~= nil then
- len = tonumber( tArgs[1] )
- height = tonumber( tArgs[2] )
- notDig = tonumber ( tArgs[3] )
- torch = 16 - notDig
- else
- runProgram = "help"
- end
- elseif #tArgs == 2 then
- if tonumber(tArgs[1]) ~= nil or tonumber(tArgs[2]) ~= nil then
- len = tonumber( tArgs[1] )
- height = tonumber( tArgs[2] )
- else
- runProgram = "help"
- end
- end
- --help output
- function runHelp()
- clrScr()
- print("Usage: shaft [length] [height] [blocks]")
- print("Length[0+] and height[1-60] are the dimensions 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 for more...")
- io.read()
- term.clear()
- print("'shaft left' will move the turtle 4 blocks to the left to start a new mining shaft.")
- print("'shaft right' will do the same thing on the right side.")
- print("While repositioning it also mines a 2 high and 3 wide tunnel.")
- print("Enter to continue.")
- io.read()
- end
- --build the starting room
- function initRoom()
- refuel(30, "Please input fuel to continue and press enter.")
- turtle.select(1)
- mineUp()
- digAndMove()
- turtle.digDown()
- turtle.turnLeft()
- digAndMove()
- turtle.digDown()
- turtle.turnLeft()
- digAndMove()
- turtle.digDown()
- digAndMove()
- turtle.digDown()
- turtle.back()
- turtle.turnRight()
- turtle.back()
- turtle.down()
- chestCheck()
- end
- --checks if there's a chest(currently block) in front
- function chestCheck()
- clrScr()
- while not turtle.detect() do
- print("Place down a chest in front of the turtle and press enter to finish.")
- io.read()
- end
- turtle.turnRight()
- clrScr()
- print("Starting position complete!")
- end
- --setup for another shaft on the left
- function shaftLeft()
- refuel(30, "Please input fuel to continue and press enter.")
- turtle.turnLeft()
- clrScr()
- if turtle.detect() then
- print("WARNING! Clear out the chest before moving on! This is your only warning! Once you hit enter the turtle will move on!")
- io.read()
- end
- clrScr()
- print("Moving on to the left!")
- mineUp()
- for i = 1, 5 do
- digAndMove()
- turtle.digDown()
- turtle.turnLeft()
- digAndMove()
- turtle.digDown()
- turtle.back()
- turtle.turnLeft()
- turtle.turnLeft()
- digAndMove()
- turtle.digDown()
- turtle.back()
- turtle.turnLeft()
- end
- turtle.back()
- turtle.down()
- chestCheck()
- end
- --setup for another shaft on the right
- function shaftRight()
- refuel(30, "Please input fuel to continue and press enter.")
- turtle.turnRight()
- clrScr()
- print("Moving on to the right!")
- mineUp()
- for i = 1, 5 do
- digAndMove()
- turtle.digDown()
- turtle.turnLeft()
- digAndMove()
- turtle.digDown()
- turtle.back()
- turtle.turnLeft()
- turtle.turnLeft()
- digAndMove()
- turtle.digDown()
- turtle.back()
- turtle.turnLeft()
- end
- turtle.back()
- turtle.down()
- turtle.turnLeft()
- turtle.turnLeft()
- chestCheck()
- 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)
- print("Fuel level = ",turtle.getFuelLevel()," need: ",need)
- print("Mining distance: ",curDist)
- io.read()
- refuel(need,message)
- elseif turtle.getFuelLevel() < need and isAtHome == false then
- goHome()
- emptyInventory()
- refuel(need,message)
- goBack()
- end
- clrScr()
- print("Turtle refuled. Returning to work...")
- end
- end
- --check if blocks that should not be mined and torches are inserted
- function validateInv()
- while turtle.getItemCount(torch) < 2 do
- clrScr()
- 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
- clrScr()
- end
- end
- function mineUp()
- while not turtle.up() do
- if not turtle.digUp() then
- return false
- end
- sleep(0.5)
- end
- return true
- end
- function digAndMove()
- while not turtle.forward() do
- if not turtle.dig() then
- return false
- end
- sleep(0.5)
- end
- return true
- end
- --check the 6 sides around the miner and mine if you find ore, then move in and repeat
- function mineOre()
- direction = detectOre()
- while #miningLog > 0 do
- direction = detectOre()
- if direction == 7 then
- backtrack = miningLog[#miningLog]
- table.remove(miningLog,#miningLog)
- if backtrack == 5 then
- while not turtle.down() do end
- elseif backtrack == 6 then
- while not turtle.up() do end
- else
- while backtrack ~= curDir do
- turtle.turnLeft()
- curDir = curDir - 1
- if curDir == 0 then curDir = 4 end
- end
- while not turtle.back() do end
- end
- end
- end
- --curDir
- end
- --detect if there's ore around you and return the direction
- function detectOre()
- dig = true
- if turtle.detectUp() then
- for i = (16-notDig), 16 do
- turtle.select(i)
- if turtle.compareUp() then dig = false end
- end
- if dig then
- if mineUp() then
- table.insert(miningLog,5)
- return 5
- end
- end
- end
- dig = true
- if turtle.detectDown() then
- for i = (16-notDig), 16 do
- turtle.select(i)
- if turtle.compareDown() then dig = false end
- end
- if dig then
- if turtle.digDown() then
- turtle.down()
- table.insert(miningLog,6)
- return 6
- end
- end
- end
- for i = 1, 4 do
- dig = true
- if turtle.detect() then
- for j = (16-notDig), 16 do
- turtle.select(j)
- if turtle.compare() then dig = false end
- end
- if dig == true then
- if digAndMove() then
- table.insert(miningLog,curDir)
- return curDir
- end
- end
- end
- turtle.turnRight()
- curDir = curDir + 1
- if curDir == 5 then curDir = 1 end
- end
- return 7
- end
- --torch placemen
- 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 + 150, "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.back()
- turtle.placeUp()
- digAndMove()
- end
- end
- --return home
- function goHome()
- i=0
- while i ~= curDist do
- if turtle.back() then i = i+1 end
- end
- isAtHome = true
- end
- --go back to work you lazy sucker!
- function goBack()
- i=0
- while i ~= curDist do
- if turtle.forward() then i = i+1 end
- end
- isAtHome = false
- end
- --function to clean inventory
- function emptyInventory()
- turtle.turnLeft()
- 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.turnRight()
- 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 + 150, "Not enough fuel to return to work. Insert fuel and press enter.")
- clrScr()
- print("Turtle ready! Resuming work...")
- goBack()
- end
- end
- --main diging program
- function mainDig()
- clrScr()
- print("Orders received.")
- print("Starting to mine a shaft ",len," long with ",notDig," block exceptions and a height of ",height,".")
- print("")
- --validate the inventory before starting
- refuel(95, "Input fuel and press enter!")
- validateInv()
- --start diging the shaft 1 by 1
- for i = 1, len do
- checkInventory()
- refuel(curDist + 95, "Out of fuel, please refil and press enter.")
- if i == 1 then isAtHome = false end
- digAndMove()
- curDist = curDist + 1
- mineOre()
- for i = 2, height do
- mineUp()
- mineOre()
- heightPos = heightPos + 1
- end
- i = 1
- while i ~= height do
- if turtle.down() then
- i = i + 1
- heightPos = heightPos + 1
- end
- end
- placeTorch()
- if curDir ~= 1 then
- for i = 2, curDir do
- turtle.turnLeft()
- end
- curDir = 1
- end
- end
- --return to base
- print("Done mining. Returning home.")
- i = 0
- while i ~= len do
- if turtle.back() then i = i + 1 end
- end
- emptyInventory()
- isAtHome = true
- --clrScr()
- print("Work complete!")
- end
- --choose what to run
- function main()
- --clrScr()
- if runProgram == "init" then
- initRoom()
- elseif runProgram == "help" then
- runHelp()
- elseif runProgram == "left" then
- shaftLeft()
- elseif runProgram == "right" then
- shaftRight()
- else
- if len > 0 and notDig < 11 and notDig > -1 and height > 0 and height < 61 and #tArgs > 0 and #tArgs < 4 then
- mainDig()
- elseif #tArgs > 0 and #tArgs < 4 then
- print("Shaft needs to be at least 1 long, height needs to be between 1 and 60 and maximum block exception is 10.")
- end
- end
- end
- --and in the end, just run the main! :)
- main()
Advertisement
Add Comment
Please, Sign In to add comment