SHOW:
|
|
- or go back to the newest paste.
| 1 | local tArgs = { ... }
| |
| 2 | ||
| 3 | if #tArgs ~= 1 then | |
| 4 | print( "Usage: tunnel <length>" ) | |
| 5 | return | |
| 6 | end | |
| 7 | ||
| 8 | -- Mine in a quarry pattern until we hit something we can't dig | |
| 9 | local length = tonumber( tArgs[1] ) | |
| 10 | if length < 1 then | |
| 11 | print( "Tunnel length must be positive" ) | |
| 12 | return | |
| 13 | end | |
| 14 | ||
| 15 | -- | |
| 16 | -- Vars | |
| 17 | -- | |
| 18 | local depth = 0 | |
| 19 | local collected = 0 | |
| 20 | - | if math.fmod(collected, 25) == 0 then |
| 20 | + | local digDelay = 0.5 |
| 21 | - | print( "Mined "..collected.." items." ) |
| 21 | + | local moveDelay = 0.5 |
| 22 | -- | |
| 23 | -- Timer | |
| 24 | -- | |
| 25 | local iniClock = os.clock() | |
| 26 | local function _clock() | |
| 27 | return os.clock() - iniClock | |
| 28 | end | |
| 29 | - | sleep(0.5) |
| 29 | + | local function timeStr(sec) -- Formate un nombre de secondes en dur?e lisible |
| 30 | local d = math.floor(sec/86400) --24*60*60 sec | |
| 31 | local h = math.floor(sec/3600)%24 -- 60x60 sec | |
| 32 | local m = math.floor(sec/60)%60 -- 60sec | |
| 33 | local s = math.floor(sec%60) --Reste | |
| 34 | local str = "" | |
| 35 | if d>0 then str = d>1 and d.." days " or "1 day " end | |
| 36 | if h>0 then str = str..h.."h" end | |
| 37 | if m>0 then str = str..(m>9 and m or "0"..m) end | |
| 38 | if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end | |
| 39 | return str | |
| 40 | end | |
| 41 | - | sleep(0.5) |
| 41 | + | |
| 42 | -- | |
| 43 | -- Display | |
| 44 | -- | |
| 45 | local function output(str,line) | |
| 46 | local x,y = term.getCursorPos() | |
| 47 | term.setCursorPos(1,line) | |
| 48 | term.clearLine() | |
| 49 | term.write(str) | |
| 50 | term.setCursorPos(x,y) | |
| 51 | end | |
| 52 | ||
| 53 | - | sleep(0.5) |
| 53 | + | local function _s(v) |
| 54 | local r,t = "null", type(v) | |
| 55 | if t ~= "number" and t ~= "string" then | |
| 56 | return t | |
| 57 | else | |
| 58 | return tostring(v) | |
| 59 | end | |
| 60 | end | |
| 61 | ||
| 62 | local label = os.getComputerLabel() | |
| 63 | local program = fs.getName(os.getRunningProgram()) | |
| 64 | local function display(extra) | |
| 65 | term.clear() | |
| 66 | output(string.format( | |
| 67 | "%s - %s - %s", | |
| 68 | label, | |
| 69 | program, | |
| 70 | _clock()), | |
| 71 | 1) | |
| 72 | output(string.format( | |
| 73 | "Fuel: %s/%s (%s)", | |
| 74 | turtle.getFuelLevel(), | |
| 75 | turtle.getFuelLimit(), | |
| 76 | math.floor((turtle.getFuelLevel()*100/turtle.getFuelLimit())+0.5)), | |
| 77 | 2) | |
| 78 | output(string.format( | |
| 79 | "Steps: %s/%s", | |
| 80 | depth, | |
| 81 | length), | |
| 82 | - | print( "Add more fuel to continue." ) |
| 82 | + | 3) |
| 83 | - | while not tryRefuel() do |
| 83 | + | output(string.format( |
| 84 | - | os.pullEvent( "turtle_inventory" ) |
| 84 | + | "Collected: %s", |
| 85 | collected), | |
| 86 | - | print( "Resuming Tunnel." ) |
| 86 | + | 4) |
| 87 | output(_s(extra), 5) | |
| 88 | end | |
| 89 | ||
| 90 | -- | |
| 91 | -- Tunnel | |
| 92 | -- | |
| 93 | local function collect() | |
| 94 | collected = collected + 1 | |
| 95 | --if math.fmod(collected, 25) == 0 then | |
| 96 | -- print( "Mined "..collected.." items." ) | |
| 97 | --end | |
| 98 | end | |
| 99 | ||
| 100 | - | sleep( 0.5 ) |
| 100 | + | |
| 101 | while turtle.detect() do | |
| 102 | if turtle.dig() then | |
| 103 | collect() | |
| 104 | sleep(digDelay) | |
| 105 | else | |
| 106 | return false | |
| 107 | end | |
| 108 | end | |
| 109 | return true | |
| 110 | end | |
| 111 | ||
| 112 | local function tryDigUp() | |
| 113 | while turtle.detectUp() do | |
| 114 | if turtle.digUp() then | |
| 115 | collect() | |
| 116 | - | sleep( 0.5 ) |
| 116 | + | sleep(digDelay) |
| 117 | else | |
| 118 | return false | |
| 119 | end | |
| 120 | end | |
| 121 | return true | |
| 122 | end | |
| 123 | ||
| 124 | local function tryDigDown() | |
| 125 | while turtle.detectDown() do | |
| 126 | if turtle.digDown() then | |
| 127 | collect() | |
| 128 | sleep(digDelay) | |
| 129 | else | |
| 130 | return false | |
| 131 | end | |
| 132 | - | sleep( 0.5 ) |
| 132 | + | |
| 133 | return true | |
| 134 | end | |
| 135 | ||
| 136 | local function refuel() | |
| 137 | local fuelLevel = turtle.getFuelLevel() | |
| 138 | - | print( "Tunnelling..." ) |
| 138 | + | |
| 139 | return | |
| 140 | end | |
| 141 | ||
| 142 | local function tryRefuel() | |
| 143 | for n=1,16 do | |
| 144 | if turtle.getItemCount(n) > 0 then | |
| 145 | turtle.select(n) | |
| 146 | if turtle.refuel(1) then | |
| 147 | turtle.select(1) | |
| 148 | return true | |
| 149 | end | |
| 150 | end | |
| 151 | end | |
| 152 | turtle.select(1) | |
| 153 | return false | |
| 154 | end | |
| 155 | ||
| 156 | if not tryRefuel() then | |
| 157 | - | print( "Aborting Tunnel." ) |
| 157 | + | local tick,e,p = os.startTimer(1) |
| 158 | while true do | |
| 159 | display("Add more fuel to continue.")
| |
| 160 | e, p = os.pullEvent() | |
| 161 | - | print( "Tunnel complete." ) |
| 161 | + | if e == "turtle_inventory" then |
| 162 | tryRefuel() | |
| 163 | elseif e == "timer" then | |
| 164 | tick = os.startTimer(1) | |
| 165 | end | |
| 166 | end | |
| 167 | os.cancelTimer(tick) | |
| 168 | display( "Resuming Tunnel." ) | |
| 169 | end | |
| 170 | end | |
| 171 | ||
| 172 | local function tryUp() | |
| 173 | refuel() | |
| 174 | while not turtle.up() do | |
| 175 | if turtle.detectUp() then | |
| 176 | if not tryDigUp() then | |
| 177 | return false | |
| 178 | end | |
| 179 | elseif turtle.attackUp() then | |
| 180 | collect() | |
| 181 | else | |
| 182 | sleep( moveDelay ) | |
| 183 | - | print( "Tunnel complete." ) |
| 183 | + | |
| 184 | - | print( "Mined "..collected.." items total." ) |
| 184 | + | |
| 185 | return true | |
| 186 | end | |
| 187 | ||
| 188 | local function tryDown() | |
| 189 | refuel() | |
| 190 | while not turtle.down() do | |
| 191 | if turtle.detectDown() then | |
| 192 | if not tryDigDown() then | |
| 193 | return false | |
| 194 | end | |
| 195 | elseif turtle.attackDown() then | |
| 196 | collect() | |
| 197 | else | |
| 198 | sleep( moveDelay ) | |
| 199 | end | |
| 200 | end | |
| 201 | return true | |
| 202 | end | |
| 203 | ||
| 204 | local function tryForward() | |
| 205 | refuel() | |
| 206 | while not turtle.forward() do | |
| 207 | if turtle.detect() then | |
| 208 | if not tryDig() then | |
| 209 | return false | |
| 210 | end | |
| 211 | elseif turtle.attack() then | |
| 212 | collect() | |
| 213 | else | |
| 214 | sleep( moveDelay ) | |
| 215 | end | |
| 216 | end | |
| 217 | return true | |
| 218 | end | |
| 219 | ||
| 220 | display( "Tunnelling..." ) | |
| 221 | ||
| 222 | for n=1,length do | |
| 223 | turtle.placeDown() | |
| 224 | tryDigUp() | |
| 225 | turtle.turnLeft() | |
| 226 | tryDig() | |
| 227 | tryUp() | |
| 228 | tryDig() | |
| 229 | turtle.turnRight() | |
| 230 | turtle.turnRight() | |
| 231 | tryDig() | |
| 232 | tryDown() | |
| 233 | tryDig() | |
| 234 | turtle.turnLeft() | |
| 235 | ||
| 236 | if n<length then | |
| 237 | tryDig() | |
| 238 | if not tryForward() then | |
| 239 | display( "Aborting Tunnel." ) | |
| 240 | break | |
| 241 | end | |
| 242 | else | |
| 243 | display( "Tunnel complete." ) | |
| 244 | end | |
| 245 | ||
| 246 | end | |
| 247 | ||
| 248 | --[[ | |
| 249 | print( "Returning to start..." ) | |
| 250 | ||
| 251 | -- Return to where we started | |
| 252 | turtle.turnLeft() | |
| 253 | turtle.turnLeft() | |
| 254 | while depth > 0 do | |
| 255 | if turtle.forward() then | |
| 256 | depth = depth - 1 | |
| 257 | else | |
| 258 | turtle.dig() | |
| 259 | end | |
| 260 | end | |
| 261 | turtle.turnRight() | |
| 262 | turtle.turnRight() | |
| 263 | ]] | |
| 264 | ||
| 265 | display( "Tunnel complete." ) |