Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 'Hole' program, version 2
- -- 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
- local xDirSav, zDirSav = 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
- local xSay, ySav, zSav = 0,0,0
- -- **************************************************
- -- Movement / Turning functions
- local function moveForward()
- -- Account for falling blocks, such as sand and gravel
- while turtle.detect() do
- turtle.dig()
- sleep(0.8)
- 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
- sleep(1)
- end
- -- Update position
- xPos = xPos + xDir
- zPos = zPos + zDir
- end
- local function moveUp()
- -- Account for falling blocks
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.8)
- end
- --Update position
- yPos = yPos + 1
- turtle.up()
- end
- local function moveDown()
- -- No need to account for falling blocks
- turtle.digDown()
- -- Update position
- yPos = yPos - 1
- turtle.down()
- 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
- -- USE NOTE: Turtle will be facing in negative x direction upon completion.
- -- This is to do with returning home, so it's facing the chest
- 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
- turnToDir(-1, 0)
- end
- -- End of movement functions
- -- **************************************************
- -- ++++++++++++++++++++++++++++++++++++++++++++++++++
- -- Logic functions
- -- Saves the current dig position and direction
- local function saveDig()
- xSav = xPos
- ySav = yPos
- zSav = zPos
- xDirSav = xDir
- zDirSav = zDir
- end
- -- Moves the turlte back to the last saved position and direction
- local function resumeDig()
- moveToPos(xSav, ySav, zSav)
- turnToDir(xDirSav, zDirSav)
- end
- -- Dumps the load into a chest or thin air
- local function dumpLoad()
- for n = 1, 9 do
- turtle.select(n)
- turtle.drop()
- end
- 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, 9 do
- if turtle.getItemCount(n) == 0 then
- retVal = false
- end
- end
- return retVal
- 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
- -- Similarly, this one control how many 'strips' we are going to mine on this level
- for nn = 1, size do
- -- And this one controls how long each of those 'strips' is, and also if the
- -- turtle is full, it controls dumping the contents in the home chest
- for nnn = 2, size do
- moveForward()
- if isFull() then
- saveDig()
- moveToPos(0, 0, 0)
- dumpLoad()
- resumeDig()
- end
- end
- -- This determines the direction the turtle will turn at the end of the
- -- strip, since it alternates every other strip
- if nn ~= size then
- if nn % 2 == 0 then
- turnLeft()
- moveForward()
- turnLeft()
- else
- turnRight()
- moveForward()
- turnRight()
- end
- end
- end
- -- Move to the home corner when the current layer is finished
- moveToPos(0, yPos, 0)
- -- If this was no the last layer, then start digging the next one, otherwise,
- -- head home and empty contents into the chest
- if m ~= depth then
- turnAround()
- moveDown()
- elseif m == depth then
- moveToPos(0, 0, 0)
- dumpLoad()
- end
- end
- end
- -- End of logic functions
- -- ++++++++++++++++++++++++++++++++++++++++++++++++++
- -- Main function controls flow of program
- local function main(argc, argv)
- if argc ~= 2 then
- print("Usage: hole <diameter> <depth>")
- return false
- end
- size = argv[1]
- depth = argv[2]
- print("Beginning dig...")
- digOut()
- print("Finishing dig...")
- end
- main(#{...}, {...})
Advertisement
Add Comment
Please, Sign In to add comment