SHOW:
|
|
- or go back to the newest paste.
| 1 | ||
| 2 | local tArgs = { ... }
| |
| 3 | - | if #tArgs ~= 1 or #tArgs ~= 2 then |
| 3 | + | if #tArgs <= 1 or #tArgs >= 2 then |
| 4 | print( "Usage: excavate <diameter> [<max depth>]" ) | |
| 5 | return | |
| 6 | end | |
| 7 | ||
| 8 | -- Mine in a quarry pattern until we hit something we can't dig | |
| 9 | local size = tonumber( tArgs[1] ) | |
| 10 | local maxDepth = tonumber( tArgs[2] ) or 99999 | |
| 11 | - | if size < 1 then |
| 11 | + | --if size < 1 then |
| 12 | - | print( "Excavate diameter must be positive" ) |
| 12 | + | --print( "Excavate diameter must be positive" ) |
| 13 | --return | |
| 14 | --end | |
| 15 | ||
| 16 | local depth = 0 | |
| 17 | local unloaded = 0 | |
| 18 | local collected = 0 | |
| 19 | ||
| 20 | local xPos,zPos = 0,0 | |
| 21 | local xDir,zDir = 0,1 | |
| 22 | ||
| 23 | local goTo -- Filled in further down | |
| 24 | local refuel -- Filled in further down | |
| 25 | ||
| 26 | local function unload( _bKeepOneFuelStack ) | |
| 27 | print( "Unloading items..." ) | |
| 28 | for n=1,16 do | |
| 29 | local nCount = turtle.getItemCount(n) | |
| 30 | if nCount > 0 then | |
| 31 | turtle.select(n) | |
| 32 | local bDrop = true | |
| 33 | if _bKeepOneFuelStack and turtle.refuel(0) then | |
| 34 | bDrop = false | |
| 35 | _bKeepOneFuelStack = false | |
| 36 | end | |
| 37 | if bDrop then | |
| 38 | turtle.drop() | |
| 39 | unloaded = unloaded + nCount | |
| 40 | end | |
| 41 | end | |
| 42 | end | |
| 43 | collected = 0 | |
| 44 | turtle.select(1) | |
| 45 | end | |
| 46 | ||
| 47 | local function returnSupplies() | |
| 48 | local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir | |
| 49 | print( "Returning to surface..." ) | |
| 50 | goTo( 0,0,0,0,-1 ) | |
| 51 | ||
| 52 | local fuelNeeded = 2*(x+y+z) + 1 | |
| 53 | if not refuel( fuelNeeded ) then | |
| 54 | unload( true ) | |
| 55 | print( "Waiting for fuel" ) | |
| 56 | while not refuel( fuelNeeded ) do | |
| 57 | os.pullEvent( "turtle_inventory" ) | |
| 58 | end | |
| 59 | else | |
| 60 | unload( true ) | |
| 61 | end | |
| 62 | ||
| 63 | print( "Resuming mining..." ) | |
| 64 | goTo( x,y,z,xd,zd ) | |
| 65 | end | |
| 66 | ||
| 67 | local function collect() | |
| 68 | local bFull = true | |
| 69 | local nTotalItems = 0 | |
| 70 | for n=1,16 do | |
| 71 | local nCount = turtle.getItemCount(n) | |
| 72 | if nCount == 0 then | |
| 73 | bFull = false | |
| 74 | end | |
| 75 | nTotalItems = nTotalItems + nCount | |
| 76 | end | |
| 77 | ||
| 78 | if nTotalItems > collected then | |
| 79 | collected = nTotalItems | |
| 80 | if math.fmod(collected + unloaded, 50) == 0 then | |
| 81 | print( "Mined "..(collected + unloaded).." items." ) | |
| 82 | end | |
| 83 | end | |
| 84 | ||
| 85 | if bFull then | |
| 86 | print( "No empty slots left." ) | |
| 87 | return false | |
| 88 | end | |
| 89 | return true | |
| 90 | end | |
| 91 | ||
| 92 | function refuel( ammount ) | |
| 93 | local fuelLevel = turtle.getFuelLevel() | |
| 94 | if fuelLevel == "unlimited" then | |
| 95 | return true | |
| 96 | end | |
| 97 | ||
| 98 | local needed = ammount or (xPos + zPos + depth + 2) | |
| 99 | if turtle.getFuelLevel() < needed then | |
| 100 | local fueled = false | |
| 101 | for n=1,16 do | |
| 102 | if turtle.getItemCount(n) > 0 then | |
| 103 | turtle.select(n) | |
| 104 | if turtle.refuel(1) then | |
| 105 | while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do | |
| 106 | turtle.refuel(1) | |
| 107 | end | |
| 108 | if turtle.getFuelLevel() >= needed then | |
| 109 | turtle.select(1) | |
| 110 | return true | |
| 111 | end | |
| 112 | end | |
| 113 | end | |
| 114 | end | |
| 115 | turtle.select(1) | |
| 116 | return false | |
| 117 | end | |
| 118 | ||
| 119 | return true | |
| 120 | end | |
| 121 | ||
| 122 | local function tryForwards() | |
| 123 | if not refuel() then | |
| 124 | print( "Not enough Fuel" ) | |
| 125 | returnSupplies() | |
| 126 | end | |
| 127 | ||
| 128 | while not turtle.forward() do | |
| 129 | if turtle.detect() then | |
| 130 | if turtle.dig() then | |
| 131 | if not collect() then | |
| 132 | returnSupplies() | |
| 133 | end | |
| 134 | else | |
| 135 | return false | |
| 136 | end | |
| 137 | elseif turtle.attack() then | |
| 138 | if not collect() then | |
| 139 | returnSupplies() | |
| 140 | end | |
| 141 | else | |
| 142 | sleep( 0.5 ) | |
| 143 | end | |
| 144 | end | |
| 145 | ||
| 146 | xPos = xPos + xDir | |
| 147 | zPos = zPos + zDir | |
| 148 | return true | |
| 149 | end | |
| 150 | ||
| 151 | local function tryDown() | |
| 152 | if not refuel() then | |
| 153 | print( "Not enough Fuel" ) | |
| 154 | returnSupplies() | |
| 155 | end | |
| 156 | ||
| 157 | while not turtle.down() do | |
| 158 | if turtle.detectDown() then | |
| 159 | if turtle.digDown() then | |
| 160 | if not collect() then | |
| 161 | returnSupplies() | |
| 162 | end | |
| 163 | else | |
| 164 | return false | |
| 165 | end | |
| 166 | elseif turtle.attackDown() then | |
| 167 | if not collect() then | |
| 168 | returnSupplies() | |
| 169 | end | |
| 170 | else | |
| 171 | sleep( 0.5 ) | |
| 172 | end | |
| 173 | end | |
| 174 | ||
| 175 | depth = depth + 1 | |
| 176 | if math.fmod( depth, 10 ) == 0 then | |
| 177 | print( "Descended "..depth.." metres." ) | |
| 178 | end | |
| 179 | ||
| 180 | return true | |
| 181 | end | |
| 182 | ||
| 183 | local function turnLeft() | |
| 184 | turtle.turnLeft() | |
| 185 | xDir, zDir = -zDir, xDir | |
| 186 | end | |
| 187 | ||
| 188 | local function turnRight() | |
| 189 | turtle.turnRight() | |
| 190 | xDir, zDir = zDir, -xDir | |
| 191 | end | |
| 192 | ||
| 193 | function goTo( x, y, z, xd, zd ) | |
| 194 | while depth > y do | |
| 195 | if turtle.up() then | |
| 196 | depth = depth - 1 | |
| 197 | elseif turtle.digUp() or turtle.attackUp() then | |
| 198 | collect() | |
| 199 | else | |
| 200 | sleep( 0.5 ) | |
| 201 | end | |
| 202 | end | |
| 203 | ||
| 204 | if xPos > x then | |
| 205 | while xDir ~= -1 do | |
| 206 | turnLeft() | |
| 207 | end | |
| 208 | while xPos > x do | |
| 209 | if turtle.forward() then | |
| 210 | xPos = xPos - 1 | |
| 211 | elseif turtle.dig() or turtle.attack() then | |
| 212 | collect() | |
| 213 | else | |
| 214 | sleep( 0.5 ) | |
| 215 | end | |
| 216 | end | |
| 217 | elseif xPos < x then | |
| 218 | while xDir ~= 1 do | |
| 219 | turnLeft() | |
| 220 | end | |
| 221 | while xPos < x do | |
| 222 | if turtle.forward() then | |
| 223 | xPos = xPos + 1 | |
| 224 | elseif turtle.dig() or turtle.attack() then | |
| 225 | collect() | |
| 226 | else | |
| 227 | sleep( 0.5 ) | |
| 228 | end | |
| 229 | end | |
| 230 | end | |
| 231 | ||
| 232 | if zPos > z then | |
| 233 | while zDir ~= -1 do | |
| 234 | turnLeft() | |
| 235 | end | |
| 236 | while zPos > z do | |
| 237 | if turtle.forward() then | |
| 238 | zPos = zPos - 1 | |
| 239 | elseif turtle.dig() or turtle.attack() then | |
| 240 | collect() | |
| 241 | else | |
| 242 | sleep( 0.5 ) | |
| 243 | end | |
| 244 | end | |
| 245 | elseif zPos < z then | |
| 246 | while zDir ~= 1 do | |
| 247 | turnLeft() | |
| 248 | end | |
| 249 | while zPos < z do | |
| 250 | if turtle.forward() then | |
| 251 | zPos = zPos + 1 | |
| 252 | elseif turtle.dig() or turtle.attack() then | |
| 253 | collect() | |
| 254 | else | |
| 255 | sleep( 0.5 ) | |
| 256 | end | |
| 257 | end | |
| 258 | end | |
| 259 | ||
| 260 | while depth < y do | |
| 261 | if turtle.down() then | |
| 262 | depth = depth + 1 | |
| 263 | elseif turtle.digDown() or turtle.attackDown() then | |
| 264 | collect() | |
| 265 | else | |
| 266 | sleep( 0.5 ) | |
| 267 | end | |
| 268 | end | |
| 269 | ||
| 270 | while zDir ~= zd or xDir ~= xd do | |
| 271 | turnLeft() | |
| 272 | end | |
| 273 | end | |
| 274 | ||
| 275 | if not refuel() then | |
| 276 | print( "Out of Fuel" ) | |
| 277 | return | |
| 278 | end | |
| 279 | ||
| 280 | print( "Excavating..." ) | |
| 281 | ||
| 282 | local reseal = false | |
| 283 | turtle.select(1) | |
| 284 | if turtle.digDown() then | |
| 285 | reseal = true | |
| 286 | end | |
| 287 | ||
| 288 | local alternate = 0 | |
| 289 | local done = false | |
| 290 | while not done do | |
| 291 | for n=1,size do | |
| 292 | for m=1,size-1 do | |
| 293 | if not tryForwards() then | |
| 294 | done = true | |
| 295 | break | |
| 296 | end | |
| 297 | end | |
| 298 | if done then | |
| 299 | break | |
| 300 | end | |
| 301 | if n<size then | |
| 302 | if math.fmod(n + alternate,2) == 0 then | |
| 303 | turnLeft() | |
| 304 | if not tryForwards() then | |
| 305 | done = true | |
| 306 | break | |
| 307 | end | |
| 308 | turnLeft() | |
| 309 | else | |
| 310 | turnRight() | |
| 311 | if not tryForwards() then | |
| 312 | done = true | |
| 313 | break | |
| 314 | end | |
| 315 | turnRight() | |
| 316 | end | |
| 317 | end | |
| 318 | end | |
| 319 | if done then | |
| 320 | break | |
| 321 | end | |
| 322 | ||
| 323 | if size > 1 then | |
| 324 | if math.fmod(size,2) == 0 then | |
| 325 | turnRight() | |
| 326 | else | |
| 327 | if alternate == 0 then | |
| 328 | turnLeft() | |
| 329 | else | |
| 330 | turnRight() | |
| 331 | end | |
| 332 | alternate = 1 - alternate | |
| 333 | end | |
| 334 | end | |
| 335 | ||
| 336 | if not tryDown() or (depth + 1) >= maxDepth then | |
| 337 | done = true | |
| 338 | break | |
| 339 | end | |
| 340 | end | |
| 341 | ||
| 342 | print( "Returning to surface..." ) | |
| 343 | ||
| 344 | -- Return to where we started | |
| 345 | goTo( 0,0,0,0,-1 ) | |
| 346 | unload( false ) | |
| 347 | goTo( 0,0,0,0,1 ) | |
| 348 | ||
| 349 | -- Seal the hole | |
| 350 | if reseal then | |
| 351 | turtle.placeDown() | |
| 352 | end | |
| 353 | ||
| 354 | print( "Mined "..(collected + unloaded).." items total." ) |