Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Will mine a hole in the ground
- local tArgs = {...}
- -- If incorrect no of args is given
- if #tArgs ~= 2 then
- print("Usage: hole <side> <depth>")
- return
- end
- -- Size will be the length of the sides of our square hole
- local size = tonumber(tArgs[1])
- -- StopDepth is the number of levels we are mining in the downwards direction
- local stopDepth = tonumber(tArgs[2])
- -- Info about turtle's current position and direction. Note that when the turtle is
- -- placed, positive x is directly forwards, positive z is to the right
- -- Also note, direction vector components should be mutually exclusive, ie if one
- -- holds a value, the other *MUST* be zero
- local depth = 0
- local xPos, zPos = 0,0
- local xDir, zDir = 1,0
- -- Will be used to save the position and direction of the turtle when we need
- -- to refuel/empty the inventory
- local xSav, ySav, zSav = 0,0,0
- local xDirSav, zDirSav = 0,0
- -- Check if there are any free slots in the inventory space
- local function isFull()
- for n = 1,9 do
- if turtle.getItemCount(n) == 0 then
- return false
- end
- end
- return true
- end
- -- Will dig forwards (if required) and move forwards, will also update position
- local function moveForward()
- while turtle.detect() do
- turtle.dig()
- sleep(0.8) -- Gravity blocks
- end
- -- Update position
- xPos = xPos + xDir
- zPos = zPos + zDir
- turtle.forward()
- end
- -- Simply moves the turtle down, digging if necessary
- local function moveDown()
- if turtle.detectDown() then
- turtle.digDown()
- end
- -- Update position
- depth = depth + 1
- turtle.down()
- end
- -- Similar to moveDown()
- local function moveUp()
- if turtle.detectUp() then
- turtle.digUp()
- end
- depth = depth - 1
- turtle.up()
- end
- -- Turn right, update direction
- local function turnRight()
- zDir, xDir = xDir, -zDir
- turtle.turnRight()
- end
- -- Turn left, update direction
- local function turnLeft()
- zDir, xDir = -xDir, zDir
- turtle.turnLeft()
- end
- -- This will just dump everything in it's inventory directly infront of it.
- local function dumpLoad()
- local stackCount = 0
- for n = 1,9 do
- stackCount = turtle.getItemCount(n)
- turtle.select(n)
- for i = 0, stackCount do
- turtle.drop()
- end
- end
- end
- -- This is where the fun begins, this function (should) make the turtle return to the 'Home' corner
- -- ie this is the corner that it started in (but it won't take it to the starting depth)
- -- This will be used to reset the turtle in order to dig down another level, or before it returns
- -- to it's starting point (and depth)
- local function returnCorner()
- if xPos > 0 then
- -- The goal here is to negate the x displacement, the first step is to point it in the negative x
- -- direction
- if xDir > 0 then
- turnRight()
- turnRight()
- elseif zPos ~= 0 then
- if zPos > 0 then
- turnRight()
- elseif zPos < 0 then
- turnLeft()
- end
- end
- -- Then we just move it in the negative x direction until it hits 0 displacement
- while xPos > 0 do
- moveForward()
- end
- end
- -- Now we repeat the process for the z displacement, first, orient the turtle in the correct
- -- direction (negative z)
- if zPos > 0 then
- if zDir > 0 then
- turnLeft()
- turnLeft()
- elseif xPos ~= 0 then
- if xPos > 0 then
- turnLeft()
- elseif xPos < 0 then
- turnRight()
- end
- end
- -- Then move the turtle forward until it hits 0 displacement
- while zPos > 0 do
- moveForward()
- end
- -- Rotate the turtle so that it faces the direction it started in (towards the inside of the hole,
- -- in the positive x direction)
- turnRight()
- end
- end
- -- This is similar to returnCorener, except it will take the turtle back to the starting height also,
- -- where we will generally have a chest for it to spew its contents into
- local function returnHome()
- returnCorner()
- while depth > 0 do
- moveUp()
- end
- -- Turn around and empty inv into chest, then turn around again (positive x)
- turnRight()
- turnRight()
- dumpLoad()
- turnRight()
- turnRight()
- end
- -- This should save the turtles current position and direction, so that we can
- -- move around (eg empty inventory) and come back to where we left off
- local function saveDig()
- xSav = xPos
- zSav = zPos
- ySav = depth
- xDirSav = xDir
- zDirSav = zDir
- end
- -- This function should return the turtle to the saved coordinates, so that we
- -- can continue to dig our hole... But it's broken, I think
- local function resumeDig()
- -- First, orient the turtle in the positive x direction, so we can adjust for our x displacement
- if xDir < 0 then
- turnRight()
- turnRight()
- elseif zDir > 0 then
- turnLeft()
- elseif zDir < 0 then
- turnRight()
- end
- -- If we started from the home corner, then our displacement will be zero, so our move
- -- command becomes a tad more simple, however, if we are inside the hole somewhere for
- -- any reason, then we will need to adjust for our current position
- if xPos == 0 then
- while xPos ~= xSav do
- moveForward()
- end
- elseif xPos > 0 then
- local deltaX = xSav - xPos
- for n = 1,deltaX do
- moveForward()
- end
- end
- -- Orient the turtle in the positive z direction
- turnRight()
- -- Follow the same logic as we did in x
- if zPos == 0 then
- while zPos ~= zSav do
- moveForward()
- end
- elseif zPos > 0 then
- local deltaZ = zSav - zPos
- for n = 1,deltaZ do
- moveForward()
- end
- end
- -- We must also account for the depth that we were digging at we, again, use the same logic
- -- as x and z, just in the y direction
- if depth == 0 then
- while depth ~= ySav do
- moveDown()
- end
- elseif depth > 0 then
- local deltaY = ySav - depth
- for n = 1,deltaY do
- moveDown()
- end
- end
- -- Finally, account for whatever direction we were facing when we left off
- while xDir ~= xDirSav do
- turnRight()
- end
- while zDir ~= zDirSav do
- turnRight()
- end
- end
- -- This finction should dig one complete hole
- local function digOut()
- -- For every layer we were told to dig, we just need to repeat the same operation,
- -- of digging out that layer
- for m = 1,stopDepth do
- -- This is where we dig out the individual layers, this first nested for loop
- -- controls how many colums the turtle will dig (in this case, because we are
- -- digging a square hole, it is the same as the number of rows we are digging)
- for n = 1, size do
- -- This second nested for loop control our digging of one column (along the x axis)
- for nn = 2, size do
- moveForward()
- -- If the turtle is full, we tell it to move to the home position (where it
- -- started) which is where it will spew up on the ground, or into a chest if we
- -- put one there
- if isFull() then
- saveDig()
- returnHome()
- resumeDig()
- end
- end
- -- This controls the turn into the next column at the end of digging the last column,
- -- if the column we just dug was the last one, we want to skip this step.
- if n ~= size then
- if n % 2 > 0 then
- -- For odd rows we want to turn right (seeing as we start from row 1 and not row
- -- 0)
- turnRight()
- moveForward()
- turnRight()
- else
- -- And even rows we turn left
- turnLeft()
- moveForward()
- turnLeft()
- end
- end
- end
- -- Move the turtle to the home corner, ready for either the next layer, or to go home
- returnCorner()
- if m ~= stopDepth then
- moveDown()
- end
- end
- -- Return the turtle to it's starting point when we are done
- returnHome()
- end
- -- LUA is a weird language, but this is the equivalent of the main() method
- print("Beginning...")
- digOut()
- print("Finished...")
Advertisement
Add Comment
Please, Sign In to add comment