Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require('robot')
- local computer = require('component').computer
- local tArgs = {...}
- if #tArgs ~= 1 then
- print("Usage: tunnel <length>")
- return
- end
- local length = tonumber(tArgs[1])
- if length < 1 then
- print("Tunnel length must be positive")
- return
- end
- local depth = 0
- local collected = 0
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 25) == 0 then
- print("Mined "..collected.." items.")
- end
- end
- local function tryDig()
- while robot.detect() do
- if robot.swing() then
- collect()
- os.sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigUp()
- while robot.detectUp() do
- if robot.swingUp() then
- collect()
- os.sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigDown()
- while robot.detectDown() do
- if robot.swingDown() then
- collect()
- os.sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryUp()
- while not robot.up() do
- if robot.detectUp() then
- if not tryDigUp() then
- return false
- end
- elseif robot.swingkUp() then
- collect()
- else
- os.sleep(0.5)
- end
- end
- return true
- end
- local function tryDown()
- while not robot.down() do
- if robot.detectDown() then
- if not tryDigDown() then
- return false
- end
- elseif robot.swingDown() then
- collect()
- else
- os.sleep(0.5)
- end
- end
- return true
- end
- local function tryForward()
- while not robot.forward() do
- if robot.detect() then
- if not tryDig() then
- return false
- end
- elseif robot.attack() then
- collect()
- else
- os.sleep(0.5)
- end
- end
- return true
- end
- print("Tunnelling...")
- for n=1,length do
- robot.placeDown()
- tryDigUp()
- robot.turnLeft()
- tryDig()
- tryUp()
- tryDig()
- robot.turnRight()
- robot.turnRight()
- tryDig()
- tryDown()
- tryDig()
- robot.turnLeft()
- if n<length then
- tryDig()
- if not tryForward() then
- print("Aborting Tunnel.")
- break
- end
- else
- print("Tunnel complete.")
- end
- end
- print("Tunnel complete.")
- print("Mined "..collected.." items total.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement