Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "Usage: tunnel <length>" )
- return
- end
- -- Mine in a quarry pattern until we hit something we can't dig
- local length = tonumber( tArgs[1] )
- if length < 1 then
- print( "Tunnel length must be positive" )
- return
- end
- --
- -- Vars
- --
- local depth = 0
- local collected = 0
- local digDelay = 0.5
- local moveDelay = 0.5
- --
- -- Timer
- --
- local iniClock = os.clock()
- local function _clock()
- return os.clock() - iniClock
- end
- local function timeStr(sec) -- Formate un nombre de secondes en dur?e lisible
- local d = math.floor(sec/86400) --24*60*60 sec
- local h = math.floor(sec/3600)%24 -- 60x60 sec
- local m = math.floor(sec/60)%60 -- 60sec
- local s = math.floor(sec%60) --Reste
- local str = ""
- if d>0 then str = d>1 and d.." days " or "1 day " end
- if h>0 then str = str..h.."h" end
- if m>0 then str = str..(m>9 and m or "0"..m) end
- if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
- return str
- end
- --
- -- Display
- --
- local function output(str,line)
- local x,y = term.getCursorPos()
- term.setCursorPos(1,line)
- term.clearLine()
- term.write(str)
- term.setCursorPos(x,y)
- end
- local function _s(v)
- local r,t = "null", type(v)
- if t ~= "number" and t ~= "string" then
- return t
- else
- return tostring(v)
- end
- end
- local label = os.getComputerLabel()
- local program = fs.getName(os.getRunningProgram())
- local function display(extra)
- term.clear()
- output(string.format(
- "%s - %s - %s",
- label,
- program,
- _clock()),
- 1)
- output(string.format(
- "Fuel: %s/%s (%s)",
- turtle.getFuelLevel(),
- turtle.getFuelLimit(),
- math.floor((turtle.getFuelLevel()*100/turtle.getFuelLimit())+0.5)),
- 2)
- output(string.format(
- "Steps: %s/%s",
- depth,
- length),
- 3)
- output(string.format(
- "Collected: %s",
- collected),
- 4)
- output(_s(extra), 5)
- end
- --
- -- Tunnel
- --
- local function collect()
- collected = collected + 1
- --if math.fmod(collected, 25) == 0 then
- -- print( "Mined "..collected.." items." )
- --end
- end
- local function tryDig()
- while turtle.detect() do
- if turtle.dig() then
- collect()
- sleep(digDelay)
- else
- return false
- end
- end
- return true
- end
- local function tryDigUp()
- while turtle.detectUp() do
- if turtle.digUp() then
- collect()
- sleep(digDelay)
- else
- return false
- end
- end
- return true
- end
- local function tryDigDown()
- while turtle.detectDown() do
- if turtle.digDown() then
- collect()
- sleep(digDelay)
- else
- return false
- end
- end
- return true
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- local function tryRefuel()
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- turtle.select(1)
- return true
- end
- end
- end
- turtle.select(1)
- return false
- end
- if not tryRefuel() then
- local tick,e,p = os.startTimer(1)
- while true do
- display("Add more fuel to continue.")
- e, p = os.pullEvent()
- if e == "turtle_inventory" then
- tryRefuel()
- elseif e == "timer" then
- tick = os.startTimer(1)
- end
- end
- os.cancelTimer(tick)
- display( "Resuming Tunnel." )
- end
- end
- local function tryUp()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- if not tryDigUp() then
- return false
- end
- elseif turtle.attackUp() then
- collect()
- else
- sleep( moveDelay )
- end
- end
- return true
- end
- local function tryDown()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- if not tryDigDown() then
- return false
- end
- elseif turtle.attackDown() then
- collect()
- else
- sleep( moveDelay )
- end
- end
- return true
- end
- local function tryForward()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- if not tryDig() then
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep( moveDelay )
- end
- end
- return true
- end
- display( "Tunnelling..." )
- for n=1,length do
- turtle.placeDown()
- tryDigUp()
- turtle.turnLeft()
- tryDig()
- tryUp()
- tryDig()
- turtle.turnRight()
- turtle.turnRight()
- tryDig()
- tryDown()
- tryDig()
- turtle.turnLeft()
- if n<length then
- tryDig()
- if not tryForward() then
- display( "Aborting Tunnel." )
- break
- end
- else
- display( "Tunnel complete." )
- end
- end
- --[[
- print( "Returning to start..." )
- -- Return to where we started
- turtle.turnLeft()
- turtle.turnLeft()
- while depth > 0 do
- if turtle.forward() then
- depth = depth - 1
- else
- turtle.dig()
- end
- end
- turtle.turnRight()
- turtle.turnRight()
- ]]
- display( "Tunnel complete." )
Add Comment
Please, Sign In to add comment