Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 'Hole' program, version 2
- -- Stores data about current mode
- local speedy = false
- local noFuel = false
- local fuelChest = false
- -- Stores turtle status data
- local isRefuelling = false
- -- Stores dimensions for the hole
- local size, depth = 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
- -- **************************************************
- -- Movement / Turning functions
- local function moveForward()
- -- 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
- end
- local function moveUp()
- -- 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
- end
- local function moveDown()
- -- No need to account for falling blocks
- turtle.digDown()
- -- Update position
- yPos = yPos - 1
- while not turtle.down() do
- turtle.attackDown()
- 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
- -- Simple check to see if we can make it home to refuel
- local function outOfFuel()
- local retVal = false
- -- 64 is a buffer, seeing as I cannot check if I'm about to run out of fuel on every
- -- move as that would have me paradoxically order my functions, so I have to allow
- -- for moving up or down also...
- local distToHome = xPos + yPos + zPos + 64
- if turtle.getFuelLevel() <= distToHome then
- retVal = true
- end
- return retVal
- end
- -- 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
- local function refuel()
- local save
- isRefuelling = 1
- save = saveDig()
- moveToPos(0, 0, 0)
- turnToDir(-1, 0)
- dumpLoad()
- turnToDir(0, -1)
- turtle.select(1)
- turtle.suck()
- turtle.refuel()
- 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
- local function digStrip()
- for i = 2, size do
- moveForward()
- if isFull() then
- local save
- save = saveDig()
- moveToPos(0,0,0)
- turnToDir(-1, 0)
- dumpLoad()
- resumeDig(save)
- end
- -- Are we able to make it home?
- if not noFuel and not isRefuelling and outOfFuel() then
- refuel()
- end
- end
- end
- -- Turns the turtle in the correct direction at the end of digging a strip
- local function stripTurn(strips)
- if strips ~= size - 1 and size % 2 == 1 then
- if strips % 2 == 1 then
- turnLeft()
- moveForward()
- turnLeft()
- else
- turnRight()
- moveForward()
- turnRight()
- end
- elseif strips ~= size -1 and size % 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
- for i = 1, size 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
- -- End of logic functions
- -- ++++++++++++++++++++++++++++++++++++++++++++++++++
- -- Main function controls flow of program
- local function main(argc, argv)
- if argc < 2 or argc > 4 then
- print("Usage: hole <diameter> <depth> -s -fc")
- print("")
- print("-s : speed mode, does not check for falling blocks")
- print("")
- print("-fc : fuel chest present, should be filled with fuel and placed to the left of the turtle before starting")
- return false
- end
- -- Test if the 'speedy' or 'fuel chest' flags were set
- if argv[3] == "-s" then
- speedy = true
- print("Speed mode")
- elseif argv[3] == "-fc" then
- fuelChest = true
- print("Fuel chest mode")
- else
- -- If we're not in fuel chest mode, then ignore fuel checking
- noFuel = true
- end
- -- Since users may want fuel chest mode and speedy mode
- if argv[4] == "-fc" then
- fuelChest = true
- print("Fuel chest mode")
- else
- -- If we're not in fuel chest mode, then ignore fuel checking
- noFuel = true
- end
- -- Will skip fuel checking
- if turtle.getFuelLevel() == "unlimited" then
- noFuel = true
- end
- size = tonumber(argv[1])
- depth = tonumber(argv[2])
- print("Beginning dig...")
- digOut()
- print("Finishing dig...")
- end
- main(#{...}, {...})
Advertisement
Add Comment
Please, Sign In to add comment