Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ###########################################################################
- -- General Digging Program ###################################################
- -- By Jharakn ################################################################
- -- ###########################################################################
- -- Changelog --
- -- Ver 0.1 alpha
- -- Ver 0.2 moved bedrock clearance to lvl 6
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Global Variables ##
- -- ##
- --define the location and boundary global variables
- position = {x=0, y=0, z=0}
- boundary = {xMin=0, yMin=0, zMin=0, xMax=0, yMax=0, zMax=0}
- -- define the direction the turtle is facing
- facing = "forward"
- --define the various flags for quarrying
- xInvert = nil
- yInvert = nil
- zInvert = nil
- newLayer = nil
- filterSlots = nil
- clearBedrock = nil
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Error Handling and Com Chatter Functions ##
- -- ##
- function crash(message)
- term.clear()
- term.setCursorPos(1,1)
- print(message)
- print("")
- print("Press Any Key to Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- end
- function debugPrint()
- term.clear()
- term.setCursorPos(1,1)
- print("x locations")
- print(boundary.xMin.." -- "..position.x.." -- "..boundary.xMax)
- print("y locations")
- print(boundary.yMin.." -- "..position.y.." -- "..boundary.yMax)
- print("z locations")
- print(boundary.zMin.." -- "..position.z.." -- "..boundary.zMax)
- sleep(10)
- end
- function display(message, clearscreen)
- if clearscreen then
- term.clear()
- term.setCursorPos(1,1)
- end
- print("["..os.time().."] "..message)
- end
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Turning and Basic Movement ##
- -- ##
- --turns the turtle the the called direction updating the facing variable
- function turn(direction)
- if direction == "forward" then
- if facing == "forward" then
- --facing the right direction don't need to do anything
- elseif facing == "right" then
- turtle.turnLeft()
- facing = "forward"
- elseif facing == "back" then
- turtle.turnLeft()
- turtle.turnLeft()
- facing = "forward"
- elseif facing == "left" then
- turtle.turnRight()
- facing = "forward"
- else
- crash("incorrect facing value in turn function")
- end
- elseif direction == "right" then
- if facing == "forward" then
- turtle.turnRight()
- facing = "right"
- elseif facing == "right" then
- --facing the right direction don't need to do anything
- elseif facing == "back" then
- turtle.turnLeft()
- facing = "right"
- elseif facing == "left" then
- turtle.turnLeft()
- turtle.turnLeft()
- facing = "right"
- else
- crash("incorrect facing value in turn function")
- end
- elseif direction == "back" then
- if facing == "forward" then
- turtle.turnLeft()
- turtle.turnLeft()
- facing = "back"
- elseif facing == "right" then
- turtle.turnRight()
- facing = "back"
- elseif facing == "back" then
- --facing the right direction don't need to do anything
- elseif facing == "left" then
- turtle.turnLeft()
- facing = "back"
- else
- crash("incorrect facing value in turn function")
- end
- elseif direction == "left" then
- if facing == "forward" then
- turtle.turnLeft()
- facing = "left"
- elseif facing == "right" then
- turtle.turnLeft()
- turtle.turnLeft()
- facing = "left"
- elseif facing == "back" then
- turtle.turnRight()
- facing = "left"
- elseif facing == "left" then
- --facing the right direction don't need to do anything
- else
- crash("incorrect facing value in turn function")
- end
- else
- crash("incorrect direction value in turn function")
- end
- end
- --dig and move forward one space updating the position based on the current facing direction
- function digForward()
- local counter = 0
- while true do
- if turtle.forward() then
- if facing == "forward" then position.x = position.x + 1 end
- if facing == "right" then position.y = position.y + 1 end
- if facing == "back" then position.x = position.x - 1 end
- if facing == "left" then position.y = position.y - 1 end
- do return true end
- break
- else
- turtle.dig()
- turtle.attack()
- counter = counter + 1
- end
- if counter == 100 then
- do return false end
- break
- end
- end
- end
- --dig and move up one space updating the position
- function digUp()
- local counter = 0
- while true do
- if turtle.up() then
- position.z = position.z + 1
- do return true end
- break
- else
- turtle.digUp()
- turtle.attackUp()
- counter = counter + 1
- end
- if counter == 100 then
- do return false end
- break
- end
- end
- end
- --dig and move down one space updating the position
- function digDown()
- local counter = 0
- while true do
- if turtle.down() then
- position.z = position.z - 1
- do return true end
- break
- else
- turtle.digDown()
- turtle.attackDown()
- counter = counter + 1
- end
- if counter == 100 then
- do return false end
- break
- end
- end
- end
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Inventory, Fuel and Space Checking ##
- -- ##
- --check the turtles fuel end fill it up if needing
- function checkFuel()
- local counter = 0
- --check the turtles current fuel level if its less that 100 get more
- if turtle.getFuelLevel() < 100 then
- --chat about it
- display("Getting More Fuel", false)
- --select the fuel chest
- turtle.select(16)
- --try to place the chest above the turtle, if it fails mine or attack above to clear the space
- while true do
- if turtle.placeUp() then
- break
- else
- counter = counter + 1
- turtle.digUp()
- turtle.attackUp()
- --if it can't mine or clear above the turtle then crash
- if counter == 100 then crash("error placing chest") end
- end
- end
- --suck some fuel from the chest
- turtle.suckUp()
- --count how much fuel its taken and return all but 1 of the fuel back to the chest refueling with whats left
- --if it sucked nothing the fuel chest is empty so return home to prevent getting lost
- if turtle.getItemCount(16) > 1 then
- turtle.dropUp(turtle.getItemCount(16) - 1)
- turtle.refuel()
- elseif turtle.getItemCount(16) == 0 then
- print("Fuel chest empty - returning home")
- --need a return home function
- else
- turtle.refuel()
- end
- --select slot 16 again and pick the chest back up in that space
- turtle.select(16)
- if turtle.digUp() == false then crash("error picking up chest") end
- --select the first slot after the filters to being mining
- turtle.select(1)
- end
- end
- --check the turtles inventory and empty it if its full
- function checkSpace(force)
- local counter = 0
- -- if theres stuff in slot 14 the inventory must be full
- if turtle.getItemCount(14) ~= 0 or force == true then
- --chat about it
- display("Emptying the Inventory", false)
- --select the unload chest
- turtle.select(15)
- --try to place the chest above the turtle, if it fails mine or attack above to clear the space
- while true do
- if turtle.placeUp() then
- break
- else
- counter = counter + 1
- turtle.digUp()
- turtle.attackUp()
- --if it can't mine or clear above the turtle then crash
- if counter == 100 then crash("error placing chest") end
- end
- end
- --unload the inventory except the filter slots to the chest
- for i = filterSlots + 1, 14 do
- turtle.select(i)
- turtle.dropUp()
- end
- --select slot 15 again and pick the chest back up in that space
- turtle.select(15)
- if turtle.digUp() == false then crash("error picking up chest") end
- --select the first slot after the filters to being mining
- turtle.select(1)
- end
- end
- --clears the blocks above and below the turtle, if they don't match the filter or there is none
- function digAroundTurtle()
- --if filterSlots is 0 there is not filter so always clear out
- if filterSlots == 0 then
- --check were not digging outside of our boundarys
- if position.z < boundary.zMax then
- --if we detect something mine it
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- --check were not digging outside of our boundarys
- if position.z > boundary.zMin then
- --if we detect something mine it
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- --if there is a filter slots value check the blocks with the filters to see if they should be mined
- else
- local canDigUp = false
- local canDigDown = false
- if position.z < boundary.zMax then canDigUp = true end
- if position.z > boundary.zMin then canDigDown = true end
- if turtle.detectUp() or turtle.detectDown() then
- for i = 1, filterSlots do
- turtle.select(i)
- if turtle.compareUp() then canDigUp = false end
- if turtle.compareDown() then canDigDown = false end
- end
- end
- if canDigUp then turtle.digUp() end
- if canDigDown then turtle.digDown() end
- end
- end
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Complex Movement ##
- -- ##
- --dig out a row travaling forwards or backwards depending on the xInvert variable
- function digRow()
- --if xInvert is true were digging backwards towards boundary.xMin
- if xInvert then
- while position.x ~= boundary.xMin do
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- --turn to face the right direction
- turn("back")
- if digForward() == false then crash("could not dig properly") end
- digAroundTurtle()
- --clear down to bedrock if the flag is true
- if clearBedrock then
- digBedrock()
- end
- end
- --flip the xInvert for the next row
- xInvert = false
- --if xInvert is false were digging forwards towards boundary.xMax
- else
- while position.x ~= boundary.xMax do
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- --turn to face the right direction
- turn("forward")
- if digForward() == false then crash("could not dig properly") end
- digAroundTurtle()
- --clear down to bedrock if the flag is true
- if clearBedrock then
- digBedrock()
- end
- end
- --flip the xInvert for the next row
- xInvert = true
- end
- end
- --dig out a layer traveling either right or left depending on the yInvert variable and running the digRow() function after every turn
- function digLayer()
- --if yInvert is true were digging forwards towards boundary.yMin
- if yInvert then
- while position.y ~= boundary.yMin do
- --use newLayer to determine if were starting a new layer, if true flip it to false, if false move a space to the left
- if newLayer then
- newLayer = false
- else
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- --turn to face the right direction
- turn("left")
- if digForward() == false then crash("could not dig properly") end
- digAroundTurtle()
- --Clear the bedrock if required
- if clearBedrock then
- digBedrock()
- end
- end
- digRow()
- end
- --flip the yInvert for the next row
- yInvert = false
- --if yInvert is false were digging forwards towards boundary.yMax
- else
- while position.y ~= boundary.yMax do
- --use newLayer to determine if were starting a new layer, if true flip it to false, if false move a space to the right
- if newLayer then
- newLayer = false
- else
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- --turn to face the right direction
- turn("right")
- if digForward() == false then crash("could not dig properly") end
- digAroundTurtle()
- --Clear the bedrock if required
- if clearBedrock then
- digBedrock()
- end
- end
- digRow()
- end
- --flip the yInvert for the next row
- yInvert = true
- end
- end
- function digBedrock()
- local counter = 0
- turtle.digDown()
- while turtle.down() do
- position.z = position.z - 1
- turtle.digDown()
- counter = counter + 1
- end
- while counter ~= 0 do
- if digUp() == false then crash("could not dig properly") end
- counter = counter - 1
- end
- end
- function goto(x, y, z)
- --return to z level 0
- while position.z ~= z do
- if position.z < z then
- if digUp() == false then crash("could not dig properly") end
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- end
- if position.z > z then
- if digDown() == false then crash("could not dig properly") end
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- end
- print(position.z)
- end
- --return to y level
- while position.y ~= y do
- if position.y < y then
- turn("right")
- if digForward() == false then crash("could not dig properly") end
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- end
- if position.y > y then
- turn("left")
- if digForward() == false then crash("could not dig properly") end
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- end
- end
- --return to x level
- while position.x ~= x do
- if position.x < x then
- turn("forward")
- if digForward() == false then crash("could not dig properly") end
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- end
- if position.x > x then
- turn("back")
- if digForward() == false then crash("could not dig properly") end
- --check the fuel and inventory space
- checkFuel()
- checkSpace()
- end
- end
- end
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Room and Quarrying Functions ##
- -- ##
- -- dig a quarry
- function digQuarry()
- --assign the recovery hight so if the turtle crashes it can return to where it started from
- local recoveryHeight = position.z
- --grab some fuel if needed
- checkFuel()
- --move down until you hit solid ground
- while turtle.detectDown() == false do
- if digDown() == false then crash("could not dig properly") end
- end
- --work out what the nearest point would be so you hit lvl 6 moving down 3 spaces at a time
- local quarryStart = 6
- while quarryStart < position.z do
- quarryStart = quarryStart + 3
- end
- --move to that point
- while position.z ~= quarryStart do
- if position.z < quarryStart then
- if digUp() == false then crash("could not dig properly") end
- elseif position.z > quarryStart then
- crash("Quarry Start point is lower than position.z")
- end
- end
- --part 2 dig down to z level 6
- while position.z > boundary.zMin do
- if newLayer == false then
- if digDown() == false then crash("could not dig properly") end
- if digDown() == false then crash("could not dig properly") end
- if digDown() == false then crash("could not dig properly") end
- digAroundTurtle()
- newLayer = true
- end
- --save/update the recovery file
- createRecovery(position.z, recoveryHeight)
- display("Starting to Quarry at lvl: "..position.z, false)
- digLayer()
- end
- --part 3 clear out the bedrock
- --clear the bedrock where the turtle is sat
- digBedrock()
- --run the layer again clearing the bedrock this time
- newLayer = true
- clearBedrock = true
- display("Clearing the Bedrock", false)
- digLayer()
- clearBedrock = false
- end
- --dig a room
- function digRoom()
- --grab some fuel if needed
- checkFuel()
- --dig up a space to maximise the dig area
- if digUp() == false then crash("could not dig properly") end
- while position.z < boundary.zMax do
- if newLayer == false then
- --dig up a space
- if digUp() == false then crash("could not dig properly") end
- --dig up another 2 spaces if the boundarys allow it
- if position.z ~= boundary.zMax then
- if digUp() == false then crash("could not dig properly") end
- end
- if position.z ~= boundary.zMax then
- if digUp() == false then crash("could not dig properly") end
- end
- digAroundTurtle()
- newLayer = true
- end
- digLayer()
- end
- end
- --dig a strip
- function digStrip()
- --grab some fuel if needed
- checkFuel()
- --dig up a space to maximise the dig area
- if digUp() == false then crash("could not dig properly") end
- while position.z < boundary.zMax do
- if newLayer == false then
- --dig up a space
- if digUp() == false then crash("could not dig properly") end
- --dig up another 2 spaces if the boundarys allow it
- if position.z ~= boundary.zMax then
- if digUp() == false then crash("could not dig properly") end
- end
- if position.z ~= boundary.zMax then
- if digUp() == false then crash("could not dig properly") end
- end
- end
- digAroundTurtle()
- digRow()
- newLayer = false
- end
- end
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Startup and Recovery Functions ##
- -- ##
- --start the program select what mode you want and ask questions to fill out the various variables
- function startup()
- local mode = nil
- local quarryHeight
- local quarrySize
- local roomWidth
- local roomDepth
- local roomHight
- local stripLength
- local stripHight
- term.clear()
- term.setCursorPos(1,1)
- print("----------------------------")
- print("Multi-Purpose Digger Program")
- print("----------------------------")
- print("")
- sleep(1)
- print("(q)arry, (r)oom or (s)trip mode?")
- mode = read()
- if mode == "q" or mode == "r" or mode == "s" then
- --everythings ok
- else
- print("Unknown Command - Press Any Key To Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- end
- if turtle.getItemCount(15) == 0 then
- print("Missing Offload Chest in Slot 15 - Press Any Key To Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- elseif turtle.getItemCount(16) == 0 then
- print("Missing Fuel Chest in Slot 16 - Press Any Key To Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- end
- if mode == "q" then
- term.clear()
- term.setCursorPos(1,1)
- print("----------------------------")
- print("Multi-Purpose Digger Program")
- print("----------------------------")
- print("")
- print("*** Quarry Mode ***")
- print("")
- print("")
- sleep(1)
- print("Please Enter Current Height Level")
- quarryHeight = tonumber(read())
- print("Please Enter Quarry Dimensions")
- quarrySize = tonumber(read())
- --set the x boundaries of the excavation
- boundary.xMin = 0
- boundary.xMax = quarrySize - 1
- --set the y boundaries of the excavation
- boundary.yMin = 0
- boundary.yMax = quarrySize - 1
- --set the z boundaries of the excavation
- boundary.zMin = 6
- boundary.zMax = quarryHeight
- --set position.z to the current height
- position.z = quarryHeight
- --develop the filter
- if turtle.getItemCount(1) == 0 then
- print("Not Filtering Against Any Items, Is This OK? Y/N")
- local responce = read()
- if responce == "n" or responce == "N" then
- print("Press Any Key To Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- elseif responce == "y" or responce == "Y" then
- filterSlots = 0
- else
- print("Unknown Responce - Press Any Key To Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- end
- else
- local counter = 1
- while turtle.getItemCount(counter) ~= 0 do
- counter = counter + 1
- end
- filterSlots = counter - 1
- end
- xInvert = false
- yInvert = false
- zInvert = true
- newLayer = true
- clearBedrock = false
- return "quarry"
- elseif mode == "r" then
- term.clear()
- term.setCursorPos(1,1)
- print("----------------------------")
- print("Multi-Purpose Digger Program")
- print("----------------------------")
- print("")
- print("*** Room Mode ***")
- print("")
- print("")
- sleep(1)
- print("Please Enter Room Width")
- roomWidth = tonumber(read())
- print("Please Enter Room Depth")
- roomDepth = tonumber(read())
- print("Please Enter Room Height")
- roomHight = tonumber(read())
- --set the x boundaries of the excavation
- boundary.xMin = 0
- boundary.xMax = roomDepth - 1
- --set the y boundaries of the excavation
- boundary.yMin = 0
- boundary.yMax = roomWidth - 1
- --set the z boundaries of the excavation
- boundary.zMin = 0
- boundary.zMax = roomHight - 1
- xInvert = false
- yInvert = false
- zInvert = false
- newLayer = true
- filterSlots = 0
- clearBedrock = false
- return "room"
- elseif mode == "s" then
- term.clear()
- term.setCursorPos(1,1)
- print("----------------------------")
- print("Multi-Purpose Digger Program")
- print("----------------------------")
- print("")
- print("*** Strip Mode ***")
- print("")
- print("")
- sleep(1)
- print("Please Enter Strip Length")
- stripLength = tonumber(read())
- print("Please Enter Strip Height")
- stripHight = tonumber(read())
- --set the x boundaries of the excavation
- boundary.xMin = 0
- boundary.xMax = stripLength - 1
- --Don't set the y boundaries, there unused for this mode
- boundary.yMin = 0
- boundary.yMax = 0
- --set the z boundaries of the excavation
- boundary.zMin = 0
- boundary.zMax = stripHight - 1
- xInvert = false
- yInvert = false
- zInvert = false
- newLayer = true
- filterSlots = 0
- clearBedrock = false
- return "strip"
- end
- end
- function createRecovery(currentHeight, recoveryHeight)
- --create a table called data and fill it with the current height and the recover height the function was called with
- local data = {}
- data["currentHeight"] = currentHeight
- data["recoveryHeight"] = recoveryHeight
- --serialise the data table and write it to a file called "recovery.info"
- local file = fs.open("recovery.info","w")
- file.write(textutils.serialize(data))
- file.close()
- end
- function recover()
- --if the "recovery.info" file exists then load the data into memory, delete the file and goto that point
- if fs.exists("recovery.info") then
- local file = fs.open("recovery.info","r")
- local data = file.readAll()
- file.close()
- fs.delete("recovery.info")
- local recovery = {}
- recovery = textutils.unserialize(data)
- position.z = recovery.currentHeight
- display("Recovery File Detected, Moving to Recovery Point", true)
- goto(0,0,recovery.recoveryHeight)
- print("Arrived at Recovery Point - Press Any Key To Restart")
- while true do
- local event, character = os.pullEvent()
- if event == "char" then os.reboot() end
- end
- end
- end
- -- ###########################################################################
- -- ###########################################################################
- -- ##
- -- Begin Program ##
- -- ##
- local returnHeight = 0
- recover()
- local operation = startup()
- if operation == "quarry" then
- returnHeight = position.z
- display("Starting Quarry", true)
- digQuarry()
- elseif operation == "room" then
- display("Digging Room", true)
- digRoom()
- elseif operation == "strip" then
- display("Digging Strip", true)
- digStrip()
- end
- --empty the inventory
- checkSpace(true)
- --return home
- goto(0, 0, returnHeight)
- --clear the recovery file if it exists
- if fs.exists("recovery.info") then fs.delete("recovery.info") end
- --Print message saying all done
- display("Task Complete", true)
Advertisement
Add Comment
Please, Sign In to add comment