Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tunnels = 0 --Number of "Branches" in mine--
- local length = 0 --Length of Mineshaft "branches"--
- local dropOff = false --Return to Start Point for Inventory Clearing?--
- local placeTorch = false --Place Torches While Mining?--
- local torch = 1 --Slot Torches are placed in
- local refuelAt = 5 --default fuel level the turtle will refuel at
- local refuelMany = 1 -- how many fuel the turtle will consume while refueling
- local emergFuelKeep = 3 -- the number of fuel considered "emergency levels"
- local returningToStart = false --tags the turtle as returning as to bypass checks
- local stageCurrent = 0 --current stage of progress
- local stage -- total stages for mine (1 for the "trunk", and then +1 for every pair of "branches")
- os.setComputerLabel("ProMine 5000")
- --Label change is part of a plan to have the program install into the startup,
- --but not bothered to put that in yet due to the size of the program so far,
- --and the frustrations I have had while debugging.
- function header1() --some simple headers
- term.clear()
- term.setCursorPos(1,1)
- print("Gathering Info 1 - 4")
- term.setCursorPos(25,1)
- print("'Quit' to abort.")
- end
- function header2() --some simple headers
- term.clear()
- term.setCursorPos(1,1)
- print("Gathering Info 2 - 4")
- term.setCursorPos(25,1)
- print("'Quit' to abort.")
- end
- function header3() --some simple headers
- term.clear()
- term.setCursorPos(1,1)
- print("Gathering Info 3 - 4")
- term.setCursorPos(25,1)
- print("'Quit' to abort.")
- end
- function header4() --some simple headers
- term.clear()
- term.setCursorPos(1,1)
- print("Gathering Info 4 - 4")
- term.setCursorPos(25,1)
- print("'Quit' to abort.")
- end
- function header5() --some simple headers
- term.clear()
- term.setCursorPos(1,1)
- print("running program")
- term.setCursorPos(25,1)
- print("stage "..stageCurrent.." - "..stage)
- end
- function quitting() --the quit program function
- term.setCursorPos(1,5)
- term.clearLine()
- print("Mine Program Aborted... Shutting Down...")
- sleep(2)
- os.shutdown()
- end
- function openScreen() --strictly visual, no code relevance.
- print(" ")
- print(" _____ __ __ _ ")
- print(" | __ \\ | \\/ (_) ")
- print(" | |__) | __ ___ | \\ / |_ _ __ ___ ")
- print(" | ___/ '__/ _ \\| |\\/| | | '_ \\ / _ \\ ")
- print(" | | | | | (_) | | | | | | | | __/ ")
- print(" |_| |_| \\___/|_| |_|_|_| |_|\\___| ")
- print(" _____ ___ ___ ___ ")
- print(" | ____|/ _ \\ / _ \\ / _ \\ ")
- print(" | |__ | | | | | | | | | | ")
- print(" |___ \\| | | | | | | | | | ")
- print(" ___) | |_| | |_| | |_| | ")
- print(" |____/ \\___/ \\___/ \\___/ ")
- sleep(2)
- for i = 0, 13 do
- print(" ")
- sleep(0.3)
- end
- end
- local currX = 0 --current X position, in regards to starting point. (forwards/backwards)
- local currY = 0 --current Y position, in regards to starting point. (up/down)
- local currZ = 0 --current Z position, in regards to starting point. (left/right)
- local currO = 0 --current orientation, in regards to starting point. (north-0, east-1, south-2, west-3)
- --GETTING TUNNEL DISTANCE INFO
- --Enters a loop that prevents the stage from
- --being skipped without a proper input, then
- --stores the data value for later use.
- openScreen()
- repeat --loop of whole menu section, allows re-entry of data if you are unhappy with the settings
- while true do --question 1 loop
- local num
- header1()
- term.setCursorPos(1,6)
- print("How Long Should I dig The Mineshaft Branches?")
- write("Length: ")
- input = read()
- num = tonumber(input)
- if input:lower() == "quit" then --user quits program
- quitting()
- elseif not num then --input was not a number
- term.setCursorPos(1,8)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: invalid input, not a number.")
- print("---------------------------------------")
- sleep(3)
- elseif num > 200 then --input was too long
- term.setCursorPos(1,8)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: too long! Must be 200 or less.")
- print("---------------------------------------")
- sleep(3)
- elseif num < 201 then --correct input, saves value as "length"
- length = num
- break --breaks question 1 loop
- end
- end
- header1()
- term.setCursorPos(1,6)
- print("---------------------------------------")
- print("Accepted:")
- print("tunnels will be "..length.." blocks long.")
- print("---------------------------------------")
- sleep(2)
- while true do --queestion 2 loop
- local num
- header2()
- term.setCursorPos(1,6)
- print("How Many Mineshaft Branches Should I Dig?")
- write("Branches: ")
- input = read()
- num = tonumber(input)
- if input:lower() == "quit" then --user wuits program
- quitting()
- elseif not num then --input is not a number
- term.setCursorPos(1,8)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: invalid input, not a number.")
- print("---------------------------------------")
- sleep(3)
- elseif num <= 20 then --correct input, value saved as tunnels
- tunnels = num
- break
- else -- input too long
- term.setCursorPos(1,8)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: Too Many Tunnels! (20 or less)")
- print("---------------------------------------")
- sleep(2)
- end
- end
- header2()
- term.setCursorPos(1,6)
- print("---------------------------------------")
- print("Accepted:")
- print("I will dig "..tunnels.." mineshafts.")
- print("---------------------------------------")
- sleep(2)
- repeat --question 3 loop
- loop1 = true
- header3()
- term.setCursorPos(1,6)
- print("Place Torches?")
- write("true/false: ")
- input = read()
- if input:lower() == "quit" then --user quits program
- quitting()
- elseif input:lower() == "no" or input:lower() == "false" then --input is false or no, sets placeTorch false and breaks loop
- placeTorch = false
- loop1 = false
- elseif input:lower() == "yes" or input:lower() == "true" then --input is true or yes, sets placeTorch
- repeat --question 3.5 loop
- local num
- header3()
- term.setCursorPos(1,6)
- print("What slot are the torches in?")
- write("Slot number: ")
- input = read()
- num = tonumber(input)
- if input:lower() == "quit" then --user quits program
- quitting()
- elseif not num then --input not number
- term.setCursorPos(1,7)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: Invalid input, not a number!")
- print("---------------------------------------")
- sleep(3)
- elseif num >= 1 and num < 16 then --sets torch slot, torch placement to true and break loop
- torch = num
- placeTorch = true
- loop1 = false
- elseif num == 16 then --slot 16 is fuel dedicated, it can not be used
- term.setCursorPos(1,7)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: Slot 16 reserved for fuel!")
- print("---------------------------------------")
- sleep(3)
- elseif num > 16 then --the turtle has no more than 16 slots
- term.setCursorPos(1,7)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: I do not have that many slots!")
- print("---------------------------------------")
- sleep(3)
- else --failsafe, don't really need it
- term.setCursorPos(1,7)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: Unknown Input Error!")
- print("---------------------------------------")
- sleep(3)
- end
- until torch < 16 and torch > 0
- else --input was not valid to the required conditions
- term.setCursorPos(1,5)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: Input not recognized... Please enter true, false, yes, no, or quit")
- print("---------------------------------------")
- sleep(3)
- end
- until loop1 == false
- header3()
- term.setCursorPos(1,6)
- print("---------------------------------------")
- print("Accepted:")
- print("Lighting mode set to "..tostring(placeTorch))
- print("---------------------------------------")
- sleep(2)
- repeat --question loop 4 (does nothing yet)
- loop2 = true
- header4()
- term.setCursorPos(1,6)
- print("return Here To Empty My Inventory? (once per branch)")
- write("true/false: ")
- input3 = read()
- if input3:lower() == "yes" or input3:lower() == "true" then
- dropOff = true
- loop2 = false
- elseif input3:lower() == "false" or input3:lower() == "no" then
- dropOff = false
- loop2 = false
- elseif input3:lower() == "quit" then
- quitting()
- else
- term.setCursorPos(1,5)
- term.clearLine()
- print("---------------------------------------")
- print("ERROR: Input not recognized... Please enter true, false, yes, no or quit")
- print("---------------------------------------")
- sleep(3)
- end
- until loop2 == false
- header4()
- term.setCursorPos(1,6)
- print("---------------------------------------")
- print("Accepted:")
- print("loot drop-off set to "..tostring(dropOff))
- print("---------------------------------------")
- sleep(2)
- --[[ once all the information has been gathered, it is listed back to the user
- so they can make sure the input was what they wanted. This is the last point
- that you are able to quit the program without CTR - T]]
- term.clear()
- term.setCursorPos(1,1)
- print("Confirming Task")
- term.setCursorPos(1,2)
- print("'Quit' to abort.")
- term.setCursorPos(1,4)
- print("---------------------------------------")
- print("You would like me to mine:")
- if tunnels == 1 then --strictly grammar, picks between singular or plural print statements
- print(" "..tunnels.." Branch, that is "..length.." Long")
- else
- print(" "..tunnels.." Branches, each being "..length.." Long")
- end
- print("---------------------------------------")
- print(" Place Torches: "..tostring(placeTorch).." (slot "..torch..")")
- print(" Return Loot: "..tostring(dropOff))
- print("---------------------------------------")
- print("")
- write("correct?: ")
- input = read()
- if input:lower() == "yes" or input:lower() == "true" then --starts the program
- term.clear()
- term.setCursorPos(1,10)
- print("---------------------------------------")
- print(" Preparing Program...")
- print(" | |")
- print("---------------------------------------")
- term.setCursorPos(6,11)
- for i = 0, 28 do --strictly visual, loading bar loop.
- write("#")
- sleep(.2)
- end
- fin = true
- sleep(1)
- elseif input:lower() == "false" or input:lower() == "no" then --restarts the questions loop
- term.setCursorPos(1,10)
- term.clearLine()
- print("---------------------------------------")
- print(" Starting Over...")
- print("---------------------------------------")
- sleep(2)
- elseif input:lower() == "quit" then --user quits the program
- quitting()
- else --bad input
- term.clear()
- term.setCursorPos(1,1)
- print("Confirming Task")
- term.setCursorPos(1,2)
- print("'Quit' to abort.")
- term.setCursorPos(1,8)
- print("---------------------------------------")
- print("ERROR: Input not recognized... Please enter true, false, yes, no or quit")
- print("---------------------------------------")
- sleep(2)
- end
- until fin == true
- function ensureSpace() --checks to make sure there is an empty slot at all times
- if (returningToStart == false) then
- if (turtle.getItemCount(15) > 0) then
- lootRun(true)
- end
- end
- end
- function ensureFuel() --checks the fuel levels, and acts accordingly
- local fuelLevel = turtle.getFuelLevel()
- if (fuelLevel ~= "unlimited") then
- if (fuelLevel < refuelAt) then
- turtle.select(16)
- local fuelItems = turtle.getItemCount(16)
- if (fuelItems == 0) then
- header5()
- print("Completely out of fuel!")
- elseif (fuelItems == 1) then
- header5()
- print("Out of Fuel!")
- elseif (fuelItems <= (emergFuelKeep + 1)) then
- header5()
- print("Consuming emergency fuel supply. "..(fuelItems - 2).." emergency fuel items remain")
- turtle.refuel(1)
- else
- if (fuelItems - (emergFuelKeep + 1) < refuelMany) then
- turtle.refuel(fuelItems - (emergFuelKeep + 1))
- else
- turtle.refuel(refuelMany)
- end
- end
- end
- end
- end
- function emptyInventory() --invintory emptying script
- local slotLoop = 1
- while (slotLoop < 16) do
- turtle.select(slotLoop)
- if slotLoop == torch then
- sleep(0)
- elseif slotLoop ~= torch then
- turtle.drop()
- end
- slotLoop = slotLoop + 1
- end
- turtle.select(1)
- end
- function lootRun() --returning to dump loot, and getting back to position script
- returningToStart = true
- local savedX = currX
- local savedY = currY
- local savedZ = currZ
- local savedO = currO
- while cuurX ~= 0 and currY ~= 0 and currZ ~= 0 do
- if currZ ~= 0 then
- faceTrunk()
- repeat
- forward()
- until currZ == 0
- end
- if currY == 1 then
- down()
- elseif currY == 0 then
- end
- faceHome()
- repeat
- forward()
- until currX == 0
- emptyInventory()
- Uturn()
- returningToStart = false
- repeat
- forward()
- until currX == savedX
- repeat
- up()
- until currY == savedY
- repeat
- left()
- until currO == savedO
- repeat
- forward()
- until currZ == savedZ
- end
- end
- function left() --left turn, with location tracking and wait to move code
- if currO >= 1 and currO < 4 then
- turtle.turnLeft()
- currO = currO - 1
- elseif currO == 0 then
- turtle.turnLeft()
- currO = 3
- else
- navError()
- end
- end
- function right() --right turn, with location tracking and wait to move code
- if currO >= 0 and currO < 3 then
- turtle.turnRight()
- currO = currO + 1
- elseif currO == 3 then
- turtle.turnRight()
- currO = 0
- else
- navError()
- end
- end
- function up() --up, with location tracking and wait to move code
- while not turtle.up(true) do
- sleep(1)
- end
- --turtle.up()
- currY = currY + 1
- end
- function down() --down, with location tracking and wait to move code
- while not turtle.down(true) do
- sleep(1)
- end
- --turtle.down()
- currY = currY - 1
- end
- function forward() --forward, , with location tracking and wait to move code
- while not turtle.forward(true) do
- sleep(1)
- end
- if currO == 0 then
- currX = currX + 1
- --turtle.forward()
- elseif currO == 1 then
- currZ = currZ + 1
- --turtle.forward()
- elseif currO == 2 then
- currX = currX - 1
- --turtle.forward()
- elseif currO == 3 then
- currZ = currZ - 1
- --turtle.forward()
- else
- navError()
- end
- end
- function back() --back, with location tracking and wait to move code
- while not turtle.back(true) do
- sleep(1)
- end
- if currO == 0 then
- currX = currX - 1
- --turtle.back()
- elseif currO == 1 then
- currZ = currZ - 1
- --turtle.back()
- elseif currO == 2 then
- currX = currX + 1
- --turtle.back()
- elseif currO == 3 then
- currZ = currZ + 1
- --turtle.back()
- else
- navError()
- end
- end
- function Uturn() --turns the turtle to face the oppisite direction
- right()
- right()
- end
- function navError() --error tossed if somthing goes wrong with the navagation scripts
- term.clear()
- term.setCursorPos(1,1)
- print("ERROR: Turtle Locational Awareness System Falure!")
- print(":currO: Exceeded opperational paramiters.")
- print("Expected 0-4, Recieved "..currO)
- print("")
- print("Task Que Cleared,Program Aborted!")
- sleep(10)
- os.pullEvent("terminate")
- end
- function faceTrunk() --face the main shaft, regardless of current orentation or location
- if currO == 0 then
- if currZ > 0 then
- right()
- elseif currZ < 0 then
- left()
- end
- elseif currO == 1 then
- if currZ > 0 then
- Uturn()
- end
- elseif currO == 2 then
- if currZ > 0 then
- left()
- elseif currZ < 0 then
- right()
- end
- elseif currO == 3 then
- if currZ < 0 then
- Uturn()
- end
- else
- navError()
- end
- end
- function faceHome() --face the starting point, regardless of current orentation or location
- if currO == 0 then
- Uturn()
- elseif currO == 1 then
- right()
- elseif currO == 2 then
- elseif currO == 3 then
- left()
- else
- navError()
- end
- end
- --ACTUAL MINING PROGRAM
- --this is what does the tunneling itself.
- local waiting = false --prevents the turtle from being stuck in a loop when moving back to place torches
- function mineTrunk() --the code that calculates and mines the main trunk
- trunk = tunnels * 4
- header5()
- term.setCursorPos(1,3)
- print("Digging Primary Shaft...")
- while trunk > 0 do --loop that keeps the turtle going until the proper length branch has been made.
- ensureFuel()
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- if turtle.detect(true) then --anti-gravel measures.
- while turtle.detect(true) do
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- end
- end
- ensureSpace()
- forward()
- turtle.digUp()
- sleep(0.5)
- turtle.detectUp()
- if turtle.detectUp(true) then --anti-gravel measures.
- while turtle.detectUp(true) do
- turtle.digUp()
- sleep(0.5)
- turtle.detectUp()
- end
- end
- ensureSpace()
- left()
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- if turtle.detect(true) then --anti-gravel measures.
- while turtle.detect(true) do
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- end
- end
- ensureSpace()
- up()
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- if turtle.detect(true) then --anti-gravel measures.
- while turtle.detect(true) do
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- end
- end
- ensureSpace()
- turtle.digUp()
- sleep(0.5)
- turtle.detectUp()
- if turtle.detectUp(true) then --anti-gravel measures.
- while turtle.detectUp(true) do
- turtle.digUp()
- sleep(0.5)
- turtle.detectUp()
- end
- end
- ensureSpace()
- waiting = false
- if currX % 6 == 0 and placeTorch and not waiting then --torch placement check and script
- right()
- back()
- turtle.select(torch)
- turtle.placeUp()
- turtle.select(1)
- waiting = true
- forward()
- left()
- end
- Uturn()
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- if turtle.detect(true) then --anti-gravel measures.
- while turtle.detect(true) do
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- end
- end
- ensureSpace()
- down()
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- if turtle.detect(true) then --anti-gravel measures. (This should be and will be a function later)
- while turtle.detect(true) do
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- end
- end
- ensureSpace()
- left()
- trunk = trunk -1
- end
- end
- function mineShaft() --the code that mines the branches, and relocates for the next pass
- header5()
- term.setCursorPos(1,3)
- print("Digging Mineshaft...")
- repeat --the loop that keeps the turtle going until it reaches the appropriate distance
- ensureFuel()
- waiting = false
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- if turtle.detect(true) then --anti-gravel measures.
- while turtle.detect(true) do
- turtle.dig()
- sleep(0.5)
- turtle.detect()
- end
- end
- ensureSpace()
- forward()
- turtle.digUp()
- ensureSpace()
- if currZ % 8 == 0 and placeTorch and not waiting then --torch placement test
- back()
- turtle.select(torch)
- turtle.placeUp()
- turtle.select(1)
- waiting = true
- forward()
- end
- until length < currZ or currZ < 0 and length * -1 > currZ
- end
- tunnels = tunnels + 1 --attempt to fix an issue with the turtles branch counts being short by 1
- stageCurrent = 1 --current stage
- stage = tunnels + 1 --total stages calculation
- mineTrunk()
- stageCurrent = stageCurrent + 1
- header5()
- left()
- --this is the core loop itself, this is what calls all the functions at the right time to build the mine as a whole
- repeat
- mineShaft()
- faceTrunk()
- repeat --loop to get the turtle back to the "branch"
- forward()
- until currZ == 0
- mineShaft()
- faceTrunk()
- stageCurrent = stageCurrent + 1
- header5()
- repeat --loop to get the turtle back to the "branch"
- forward()
- until currZ == 0
- left()
- if currX > 3 then --failsafe to make sure the turtle doesn't backtrack past its starting position.
- for i = 0, 3 do --positioning for the next 2 branches.
- forward()
- end
- right()
- else
- stageCurrent = stage
- end
- until stageCurrent == tunnels
- faceHome()
- term.clear()
- term.setCursorPos(1,1)
- print("Running program")
- term.setCursorPos(25,1)
- print("Done")
- if currX > 0 then --returning the turtle to the start after a successful run of the program
- repeat
- forward()
- until currX == 0
- end
- emptyInventory()
- Uturn()
- print("")
- print("---------------------------------------")
- print(" Task Completed")
- print("---------------------------------------")
Advertisement
Add Comment
Please, Sign In to add comment