Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- step = 0
- distance_travelled = 0
- abort = false
- facing_direction = 0
- fast = true
- tunnel_width = 1
- tunnel_height = 1
- DIRECTIONS = {
- FORWARDS = 0,
- LEFT = -1,
- BACKWARDS = -2,
- RIGHT = 1
- }
- function text()
- shell.run("clear")
- print("fuel level: ".. turtle.getFuelLevel())
- io.write "turtle will stop when this number reaches zero: "
- io.write (tunnel_length)
- print ""
- print ""
- print "or when the last slot in it's inventory is in use."
- end
- function Return()
- repeat
- turtle.back()
- distance_travelled = distance_travelled-1
- until distance_travelled == 0
- end
- function refuel()
- if turtle.getFuelLevel() < 3 then
- for i = 1,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(1) then
- break
- end
- end
- end
- turtle.select(1)
- end
- end
- function abortifinventoryfull()
- --if inventory full, skip to step 4 and set remaining tunnel length to 1
- if turtle.getItemCount(16) ~= 0 then
- tunnel_length = 1 --set remaining tunnel length to 1
- step = 4 --skip to step 4
- abort = true
- end
- end
- function dig_until_no_blocks_detected_forwards()
- -- For gravel or sand, keep diggin in front of the turtle
- -- until there are no more blocks or abort everything if inventory full
- while turtle.detect() == true and not abort do
- turtle.dig() --dig infront of turtle
- sleep(0.4) --wait for blocks to fall
- abortifinventoryfull() --abort tunnel if inventory is full
- end
- end
- function dig_until_no_blocks_detected_upwards()
- -- Dig upwards until there are no more blocks ontop of the turtle or inventory full
- if turtle.detectUp() == true and not abort then
- while turtle.detectUp() == true do --dig ontop of turtle until there are no more blocks
- turtle.digUp() --dig ontop of the turtle
- sleep(0.4) --wait for blocks to fall
- abortifinventoryfull() --abort tunnel if inventory is full
- end
- end
- end
- function dig_until_no_blocks_detected_downwards()
- -- Dig upwards until there are no more blocks below the turtle or inventory full
- if turtle.detectDown() == true and not abort then
- while turtle.detectDown() == true do --dig below turtle until there are no more blocks
- turtle.digDown() --dig below turtle
- sleep(0.4) --wait for blocks to fall
- abortifinventoryfull() --abort tunnel if inventory is full
- end
- end
- end
- function digtopandbottom()
- --dig all the blocks ontop and below the turtle
- dig_until_no_blocks_detected_upwards()
- dig_until_no_blocks_detected_downwards()
- end
- function turn_around()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- function turn_to_direction(dir)
- -- 0 = forwards
- -- -1 = left
- -- +1 = right
- -- -2 = backwards
- --TODO: implement smarter logic
- -- takes the most efficient turning direction to turn to a direction
- if dir == DIRECTIONS.FORWARDS then
- if facing_direction == DIRECTIONS.LEFT then
- turtle.turnRight()
- elseif facing_direction == DIRECTIONS.RIGHT then
- turtle.turnLeft()
- elseif facing_direction == DIRECTIONS.BACKWARDS then
- turn_around()
- end
- facing_direction = DIRECTIONS.FORWARDS
- elseif dir == DIRECTIONS.LEFT then
- if facing_direction == DIRECTIONS.FORWARDS then
- turtle.turnLeft()
- elseif facing_direction == DIRECTIONS.RIGHT then
- turn_around()
- elseif facing_direction == DIRECTIONS.BACKWARDS then
- turtle.turnRight()
- end
- facing_direction = DIRECTIONS.LEFT
- elseif dir == DIRECTIONS.RIGHT then
- if facing_direction == DIRECTIONS.FORWARDS then
- turtle.turnRight()
- elseif facing_direction == DIRECTIONS.LEFT then
- turn_around()
- elseif facing_direction == DIRECTIONS.BACKWARDS then
- turtle.turnLeft()
- end
- facing_direction = DIRECTIONS.RIGHT
- elseif dir == DIRECTIONS.BACKWARDS then
- if facing_direction == DIRECTIONS.FORWARDS then
- turn_around()
- elseif facing_direction == DIRECTIONS.LEFT then
- turtle.turnLeft()
- elseif facing_direction == DIRECTIONS.RIGHT then
- turtle.turnRight()
- end
- facing_direction = DIRECTIONS.BACKWARDS
- end
- end
- function dig_along_height_upwards()
- for h = 1, tunnel_height, 1 do
- dig_until_no_blocks_detected_upwards()
- turtle.up()
- sleep(0.4)
- end
- end
- function dig_along_height_downwards()
- for h = 1, tunnel_height, 1 do
- dig_until_no_blocks_detected_downwards()
- turtle.down()
- sleep(0.4)
- end
- end
- function dig_along_height()
- refuel()
- dig_along_height_upwards()
- dig_along_height_downwards()
- dig_until_no_blocks_detected_downwards()
- turtle.down()
- sleep(0.4)
- dig_along_height_downwards()
- dig_along_height_upwards()
- dig_until_no_blocks_detected_upwards()
- turtle.up()
- sleep(0.4)
- end
- function dig_along_width()
- refuel()
- for w = 1, tunnel_width, 1 do
- dig_until_no_blocks_detected_forwards()
- turtle.forward()
- sleep(0.4)
- dig_along_height()
- end
- end
- function return_to_center_fast()
- --go back to center by running backwards the entire width
- for w = 1, tunnel_width+1, 1 do
- turtle.back()
- sleep(0.4)
- end
- end
- function return_to_center(turn_to_dir)
- --returns the turtle to center fast or by digging half of the plane back
- if fast then
- return_to_center_fast() --runs backwards the entire width
- else
- turn_to_direction(turn_to_dir)
- dig_along_width() --digs the entire height top and bottom along the way back
- dig_until_no_blocks_detected_forwards()
- turtle.forward()
- sleep(0.4)
- dig_along_height()
- end
- end
- function dig_plane()
- dig_along_height()
- -- go left for entire width and dig up and down height
- turn_to_direction(DIRECTIONS.LEFT)
- dig_along_width()
- --return to center
- return_to_center(DIRECTIONS.RIGHT)
- -- go right for entire width and dig up and down height
- turn_to_direction(DIRECTIONS.RIGHT)
- dig_along_width()
- --return to center
- return_to_center(DIRECTIONS.LEFT)
- turn_to_direction(DIRECTIONS.FORWARDS)
- end
- function read_number()
- temp = io.read()
- return tonumber(temp)
- end
- function start()
- shell.run("clear")
- io.write "how long do you want the tunnel: "
- tunnel_length = read_number()
- io.write "how wide (radius) do you want the tunnel: "
- tunnel_width = read_number()
- io.write "how high (radius) do you want the tunnel: "
- tunnel_height = read_number()
- io.write "should the fast version be used? (y/n)"
- answer = io.read()
- if string.lower(answer) == "y" then
- fast = true
- elseif string.lower(answer) == "n" then
- fast = false
- else
- io.write "not a valid answer, defaulting to \"y\""
- fast = true
- end
- dig_until_no_blocks_detected_forwards()
- turtle.forward()
- shell.run("clear")
- end
- -- NO MORE FUNCTIONS :D
- function main()
- start()
- while true do
- text()
- dig_plane()
- dig_until_no_blocks_detected_forwards()
- turtle.forward()
- tunnel_length = tunnel_length - 1
- distance_travelled = distance_travelled + 1
- if tunnel_length == 0 or abort then
- break
- end
- end
- turn_to_direction(DIRECTIONS.FORWARDS)
- Return()
- text()
- sleep(2)
- shell.run("clear")
- print "DONE!"
- print "or out of inventory..."
- end
- main()
Add Comment
Please, Sign In to add comment