Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 'Hole' program, version 1.1
- -- Created by Pyro_
- -- Credit:
- -- theoriginalbit
- -- Also:
- -- BigSHinyToys
- -- BombMan
- local version = 1.1
- -- Stores data about current mode
- local speedy = false
- local noFuel = false
- local fuelChest = false
- -- Stores turtle status data
- local isRefuelling = false
- local fuelLevel = 0
- -- Stores dimensions for the hole
- local sizeX, sizeZ, depth = 0,0,0
- -- Stores current turtle position, relative to start point
- local xPos, yPos, zPos = 0,0,0
- -- Stores current direction, positive x is directly forwards from start point
- -- positive z is directly right
- local xDir, zDir = 1,0
- -- Stores last saved position (Save 1)
- local xSav1, ySav1, zSav1 = 0,0,0
- local xDirSav1, zDirSav1 = 0,0
- local save1Mutex = 0
- -- Save 2
- local xSav2, ySav2, zSav2 = 0,0,0
- local xDirSav2, zDirSav2 = 0,0
- local save2Mutex = 0
- -- This is required in movement functions
- -- Calculates distance to home, with small buffer
- local function distToHome()
- local distance
- -- Y is negative because the y axis increases as it goes up
- distance = xPos + (-yPos) + zPos + 5
- return distance
- end
- -- **************************************************
- -- Movement / Turning functions
- local function moveForwardRF()
- local retVal = true
- -- Account for falling blocks, such as sand and gravel
- if not speedy then
- while turtle.detect() do
- turtle.dig()
- sleep(0.8)
- end
- else
- turtle.dig()
- end
- -- If there is any mob infront of the turtle, or we are out of fuel
- -- DEV NOTE: Will be modified later to refuel, or message over rednet wifi
- while not turtle.forward() do
- turtle.attack()
- end
- -- Update position
- xPos = xPos + xDir
- zPos = zPos + zDir
- if not noFuel then
- -- Update fuel info
- fuelLevel = fuelLevel - 1
- -- Fuel checking
- if (distToHome() >= fuelLevel) and not isRefuelling then
- retVal = false
- end
- end
- return retVal
- end
- local function moveUpRF()
- local retVal = true
- -- Account for falling blocks
- if not speedy then
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.8)
- end
- else
- turtle.digUp()
- end
- --Update position
- yPos = yPos + 1
- while not turtle.up() do
- turtle.attackUp()
- end
- if not noFuel then
- -- Update fuel info
- fuelLevel = fuelLevel - 1
- -- Fuel checking
- if (distToHome() >= fuelLevel) and not isRefuelling then
- retVal = false
- end
- end
- return retVal
- end
- local function moveDownRF()
- local retVal = true
- -- No need to account for falling blocks
- turtle.digDown()
- -- Update position
- yPos = yPos - 1
- while not turtle.down() do
- turtle.attackDown()
- end
- if not noFuel then
- -- Update fuel info
- fuelLevel = fuelLevel - 1
- -- Fuel checking (skip if already refuelling)
- if (distToHome() >= fuelLevel) and not isRefuelling then
- retVal = false
- end
- end
- return retVal
- end
- -- Move functions, with fuel checking (will be ignored if noFuel mode is selected)
- local function moveForward()
- if not moveForwardRF() then
- refuel()
- end
- end
- local function moveUp()
- if not moveUpRF() then
- refuel()
- end
- end
- local function moveDown()
- if not moveDownRF() then
- refuel()
- end
- end
- local function turnLeft()
- -- Update direction vector
- zDir, xDir = -xDir, zDir
- turtle.turnLeft()
- end
- local function turnRight()
- -- Update direction vector
- zDir, xDir = xDir, -zDir
- turtle.turnRight()
- end
- local function turnAround()
- -- Update direction
- zDir, xDir = -zDir, -xDir
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Turns the turtle to the prompted direction
- local function turnToDir(x, z)
- if x == 1 and xDir == 0 then
- if zDir == 1 then
- turnLeft()
- else
- turnRight()
- end
- elseif x == 1 and xDir == -1 then
- turnAround()
- elseif x == -1 and xDir == 0 then
- if zDir == 1 then
- turnRight()
- else
- turnLeft()
- end
- elseif x == -1 and xDir == 1 then
- turnAround()
- elseif z == 1 and zDir == 0 then
- if xDir == 1 then
- turnRight()
- else
- turnLeft()
- end
- elseif z == 1 and zDir == -1 then
- turnAround()
- elseif z == -1 and zDir == 0 then
- if xDir == 1 then
- turnLeft()
- else
- turnRight()
- end
- elseif z == -1 and zDir == 1 then
- turnAround()
- else
- return false
- end
- return true
- end
- -- Takes in a coordinate and moves the turtle there
- -- IMPLEMENTATION NOTE: Turtle will be facing in whatever direction it finishes in.
- -- Keep this in mind, as it might affect what happens next.
- local function moveToPos(x, y, z)
- -- Move the turtle to the required X value
- if x > xPos then
- turnToDir(1, 0)
- while x > xPos do
- moveForward()
- end
- elseif x < xPos then
- turnToDir(-1, 0)
- while x < xPos do
- moveForward()
- end
- end
- -- Move the turtle to the required Z value
- if z > zPos then
- turnToDir(0, 1)
- while z > zPos do
- moveForward()
- end
- elseif z < zPos then
- turnToDir(0, -1)
- while z < zPos do
- moveForward()
- end
- end
- -- Move the turtle to the required Y value
- if y > yPos then
- while y > yPos do
- moveUp()
- end
- else
- while y < yPos do
- moveDown()
- end
- end
- end
- -- End of movement functions
- -- **************************************************
- -- ++++++++++++++++++++++++++++++++++++++++++++++++++
- -- Logic functions
- -- Saves the current dig position and direction
- local function saveDig()
- local retVal = 0
- if save1Mutex == 0 then
- xSav1 = xPos
- ySav1 = yPos
- zSav1 = zPos
- xDirSav1 = xDir
- zDirSav1 = zDir
- save1Mutex = 1
- retVal = 1
- elseif save2Mutex == 0 then
- xSav2 = xPos
- ySav2 = yPos
- zSav2 = zPos
- xDirSav2 = xDir
- zDirSav2 = zDir
- save2Mutex = 1
- retVal = 2
- end
- return retVal
- end
- -- Dumps the load into a chest or thin air
- local function dumpLoad()
- for n = 1, 16 do
- turtle.select(n)
- turtle.drop()
- end
- end
- -- Moves the turtle back to the last saved position and direction
- local function resumeDig(save)
- if save == 1 then
- moveToPos(xSav1, ySav1, zSav1)
- turnToDir(xDirSav1, zDirSav1)
- save1Mutex = 0
- elseif save == 2 then
- moveToPos(xSav2, ySav2, zSav2)
- turnToDir(xDirSav2, xDirSav2)
- save2Mutex = 0
- end
- end
- -- Move the turtle to the home and fuel chests, dump load and refuel, then
- -- resume digging
- function refuel()
- print("refuelling")
- local save
- isRefuelling = 1
- save = saveDig()
- moveToPos(0, 0, 0)
- turnToDir(-1, 0)
- dumpLoad()
- turnToDir(0, -1)
- turtle.select(1)
- turtle.suck()
- turtle.refuel()
- fuelLevel = turtle.getFuelLevel()
- resumeDig(save)
- isRefuelling = 0
- end
- -- Simply checks if the turtle is full
- -- DEV NOTE: this is kind of brute-forcish, I wonder if there's a better way?
- local function isFull()
- local retVal = true
- for n = 1, 16 do
- if turtle.getItemCount(n) == 0 then
- retVal = false
- end
- end
- return retVal
- end
- -- Digs the current strip in the current layer (parallel to X axis)
- local function digStrip()
- for i = 2, sizeX do
- moveForward()
- if isFull() then
- local save
- save = saveDig()
- moveToPos(0,0,0)
- turnToDir(-1, 0)
- dumpLoad()
- resumeDig(save)
- end
- end
- end
- -- Turns the turtle in the correct direction at the end of digging a strip
- local function stripTurn(strips)
- -- This is a bit bizzarre, since when we have an odd number of strips,
- -- then the turns are the same for every level, but when we have an
- -- even number of strips, they flip every other level
- if strips ~= sizeZ - 1 and sizeZ % 2 == 1 then
- if strips % 2 == 1 then
- turnLeft()
- moveForward()
- turnLeft()
- else
- turnRight()
- moveForward()
- turnRight()
- end
- -- DEV NOTE: is there a better way of doing this?
- elseif strips ~= sizeZ -1 and sizeZ % 2 == 0 then
- if yPos % 2 == 0 and strips % 2 == 0 then
- turnRight()
- moveForward()
- turnRight()
- elseif yPos % 2 == 1 and strips % 2 == 1 then
- turnRight()
- moveForward()
- turnRight()
- else
- turnLeft()
- moveForward()
- turnLeft()
- end
- end
- end
- -- Digs the current layer
- local function digLayer()
- local strips = 0
- print("Beginning layer: "..(-yPos))
- for i = 1, sizeZ do
- digStrip()
- stripTurn(strips)
- strips = strips + 1
- end
- end
- -- This function controls the digging of the hole
- local function digOut()
- -- This for loop controls how many layers we are going to dig
- for n = 1, depth do
- digLayer()
- -- If this was no the last layer, then start digging the next one, otherwise,
- -- head home and empty contents into the chest
- if n ~= depth then
- turnAround()
- moveDown()
- elseif n == depth then
- moveToPos(0, 0, 0)
- turnToDir(-1, 0)
- dumpLoad()
- turnToDir(1, 0)
- end
- end
- end
- -- Converts the dimension arguments to numbers
- local function argToNumber(argv)
- argv[1] = tonumber(argv[1])
- argv[2] = tonumber(argv[2])
- argv[3] = tonumber(argv[3])
- end
- -- Validate the dimensions passed to the program (so the first three arguments)
- local function validateDimensions(argv)
- local retVal = true
- -- Convert the flags to usable numbers
- argToNumber(argv)
- -- Check that they exist, and are numbers
- if argv[1] == nil or not type(argv[1], "number") then
- retVal = false
- elseif argv[2] == nil or not type(argv[2], "number") then
- retVal = false
- elseif argv[3] == nil or not type(argv[3], "number") then
- retVal = false
- -- Check that the numbers are valid
- elseif argv[1] < 0 then
- retVal = false
- elseif argv[2] < 0 then
- retVal = false
- elseif argv[3] < 0 then
- retVal = false
- end
- return retVal
- end
- -- Will validate the flags (if any) that were passed to the program
- -- They are also set here, as it turns out to be easier
- local function validateAndParseFlags(argc, argv)
- local retVal = true
- if not type(argv[4], "string") or not argv[4] then
- retVal = false
- elseif not type(argv[5], "string") or not argv[4] then
- retVal = false
- else
- for i = 4, argc do
- if argv[i] == "-s" then
- speedy = true
- print("Speed mode")
- elseif argv[i] == "-fc" then
- fuelChest = true
- print("Fuel chest mode")
- else
- retVal = false
- end
- end
- end
- return retVal
- end
- -- Prints usage information to the terminal
- local function printUsage()
- term.clear()
- term.setCursorPos(1, 1)
- print("Usage:")
- print("hole [<size X> <size Z> <depth> -s -fc] | help")
- print("")
- print("-s : speed mode")
- print("-fc : fuel chest mode")
- print("")
- print("Using 'hole help' will display help pages")
- end
- -- Just prints author info
- local function printInfo()
- print("Created by: Pyro_")
- print("Credit to: theoriginalbit, BigSHinyToys and BombMan")
- end
- -- Just writes help information to the terminal, one of these
- -- days I will have it write a help file for the help program,
- -- then call the help program or something
- local function printHelp()
- term.clear()
- term.setCursorPos(1, 2)
- print("Information:")
- print("An ongoing development project by Pyro_")
- print("Credit to:")
- print("theoriginalbit, BigSHinyToys and BombMan")
- print("Come check us out on the CC forums")
- print("")
- print("Version: ".. version)
- print("OS Version: " .. os.version())
- -- Keep page number and any key prompt in the same spot
- -- this way they don't interfere with spacing
- term.setCursorPos(x - string.len("Page 1"), 12)
- print("Page 1")
- term.setCursorPos(1, 13)
- print("Press any key to continue")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1, 2)
- -- Why not?
- printUsage()
- term.setCursorPos(x - string.len("Page 2"), 12)
- print("Page 2")
- term.setCursorPos(1, 13)
- print("Press any key to continue")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1, 2)
- print("All flags with '-' preceding them are optional")
- print("All flags surrounded by '<>' are required")
- print("")
- print("Using -s will skip checking for blocks that may fall in front of the turtle,")
- print("so ensure this won't happen before you use this flag")
- term.setCursorPos(x - string.len("Page 3"), 12)
- print("Page 3")
- term.setCursorPos(1, 13)
- print("Press any key to continue")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1, 2)
- print("Using -fc will tell the turtle that")
- print("there's a chest of fuel located to it's")
- print("immediate left when it initially starts")
- print("it will use the fuel inside the chest")
- print("to refuel when it gets low")
- print("")
- print("Note: The turtle will pull one whole")
- print("stack from a slot in the chest, so")
- print("budget fuel accordingly; so that it")
- print("doesn't end up eating fuel it will")
- print("never use...")
- term.setCursorPos(x - string.len("Page 4"), 12)
- print("Page 4")
- term.setCursorPos(1, 13)
- print("Press any key to return")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Checks if the user has requested the help page
- local function isFlagHelp(argv)
- local retVal = false
- if argv[1] == "help" then
- retVal = true
- end
- return retVal
- end
- -- End of logic functions
- -- ++++++++++++++++++++++++++++++++++++++++++++++++++
- -- Main function controls flow of program
- local function main(argc, argv)
- if isFlagHelp(argv) then
- printHelp()
- elseif not validateDimensions(argv) then
- printUsage()
- printInfo()
- elseif not validateAndParseFlags(argc, argv) then
- -- Test if the 'speedy' or 'fuel chest' flags were set
- printUsage()
- printInfo()
- else
- -- If we aren't in fuel chest mode, then ignore fuel checking
- if fuelChest == false then
- noFuel = true
- end
- -- Backwards compatability; think C ternary operation
- -- fuelLevel will be ignored if noFuel flag is set
- fuelLevel = turtle.getFuelLevel and turtle.getFuelLevel() or "unlimited"
- -- Will skip fuel checking if turtleNeedsFuel is 0 in config
- if fuelLevel == "unlimited" then
- noFuel = true
- end
- sizeX = argv[1]
- sizeZ = argv[2]
- depth = argv[3]
- local dims = string.format("Dimensions: X: %i Z: %i Depth: %i", sizeX, sizeZ, depth)
- print("Beginning dig...")
- print(dims)
- digOut()
- print("Finished dig...")
- end
- end
- main(#{...}, {...})
Advertisement
Add Comment
Please, Sign In to add comment