SHOW:
|
|
- or go back to the newest paste.
| 1 | local tArgs = { ... }
| |
| 2 | if #tArgs ~= 1 then | |
| 3 | print( "Usage: tunnel <length>" ) | |
| 4 | return | |
| 5 | end | |
| 6 | ||
| 7 | -- Mine in a quarry pattern until we hit something we can't dig | |
| 8 | local length = tonumber( tArgs[1] ) | |
| 9 | if length < 1 then | |
| 10 | print( "Tunnel length must be positive" ) | |
| 11 | return | |
| 12 | end | |
| 13 | ||
| 14 | local depth = length | |
| 15 | local collected = 0 | |
| 16 | ||
| 17 | local function collect() | |
| 18 | collected = collected + 1 | |
| 19 | if math.fmod(collected, 25) == 0 then | |
| 20 | print( "Mined "..collected.." items." ) | |
| 21 | end | |
| 22 | end | |
| 23 | ||
| 24 | local function tryDig() | |
| 25 | while turtle.detect() do | |
| 26 | if turtle.dig() then | |
| 27 | collect() | |
| 28 | sleep(0.5) | |
| 29 | else | |
| 30 | return false | |
| 31 | end | |
| 32 | end | |
| 33 | return true | |
| 34 | end | |
| 35 | ||
| 36 | local function tryDigUp() | |
| 37 | while turtle.detectUp() do | |
| 38 | if turtle.digUp() then | |
| 39 | collect() | |
| 40 | sleep(0.5) | |
| 41 | else | |
| 42 | return false | |
| 43 | end | |
| 44 | end | |
| 45 | return true | |
| 46 | end | |
| 47 | ||
| 48 | local function tryDigDown() | |
| 49 | while turtle.detectDown() do | |
| 50 | if turtle.digDown() then | |
| 51 | collect() | |
| 52 | sleep(0.5) | |
| 53 | else | |
| 54 | return false | |
| 55 | end | |
| 56 | end | |
| 57 | return true | |
| 58 | end | |
| 59 | ||
| 60 | local function refuel() | |
| 61 | local fuelLevel = turtle.getFuelLevel() | |
| 62 | if fuelLevel == "unlimited" or fuelLevel > 0 then | |
| 63 | return | |
| 64 | end | |
| 65 | ||
| 66 | local function tryRefuel() | |
| 67 | for n=1,16 do | |
| 68 | if turtle.getItemCount(n) > 0 then | |
| 69 | turtle.select(n) | |
| 70 | if turtle.refuel(1) then | |
| 71 | turtle.select(1) | |
| 72 | return true | |
| 73 | end | |
| 74 | end | |
| 75 | end | |
| 76 | turtle.select(1) | |
| 77 | return false | |
| 78 | end | |
| 79 | ||
| 80 | if not tryRefuel() then | |
| 81 | print( "Add more fuel to continue." ) | |
| 82 | while not tryRefuel() do | |
| 83 | sleep(1) | |
| 84 | end | |
| 85 | print( "Resuming Tunnel." ) | |
| 86 | end | |
| 87 | end | |
| 88 | ||
| 89 | local function tryUp() | |
| 90 | refuel() | |
| 91 | while not turtle.up() do | |
| 92 | if turtle.detectUp() then | |
| 93 | if not tryDigUp() then | |
| 94 | return false | |
| 95 | end | |
| 96 | elseif turtle.attackUp() then | |
| 97 | collect() | |
| 98 | else | |
| 99 | sleep( 0.5 ) | |
| 100 | end | |
| 101 | end | |
| 102 | return true | |
| 103 | end | |
| 104 | ||
| 105 | local function tryDown() | |
| 106 | refuel() | |
| 107 | while not turtle.down() do | |
| 108 | if turtle.detectDown() then | |
| 109 | if not tryDigDown() then | |
| 110 | return false | |
| 111 | end | |
| 112 | elseif turtle.attackDown() then | |
| 113 | collect() | |
| 114 | else | |
| 115 | sleep( 0.5 ) | |
| 116 | end | |
| 117 | end | |
| 118 | return true | |
| 119 | end | |
| 120 | ||
| 121 | local function tryForward() | |
| 122 | refuel() | |
| 123 | while not turtle.forward() do | |
| 124 | if turtle.detect() then | |
| 125 | if not tryDig() then | |
| 126 | return false | |
| 127 | end | |
| 128 | elseif turtle.attack() then | |
| 129 | collect() | |
| 130 | else | |
| 131 | sleep( 0.5 ) | |
| 132 | end | |
| 133 | end | |
| 134 | return true | |
| 135 | end | |
| 136 | ||
| 137 | local function tryBack() | |
| 138 | refuel() | |
| 139 | while not turtle.back() do | |
| 140 | if turtle.detect() then | |
| 141 | if not tryDig() then | |
| 142 | return false | |
| 143 | end | |
| 144 | elseif turtle.attack() then | |
| 145 | collect() | |
| 146 | else | |
| 147 | sleep( 0.5 ) | |
| 148 | end | |
| 149 | end | |
| 150 | return true | |
| 151 | end | |
| 152 | ||
| 153 | print( "Tunnelling..." ) | |
| 154 | ||
| 155 | for n=1,length do | |
| 156 | turtle.placeDown() | |
| 157 | tryDigUp() | |
| 158 | turtle.turnLeft() | |
| 159 | tryDig() | |
| 160 | - | tryUp() |
| 160 | + | |
| 161 | tryDig() | |
| 162 | tryDigUp() | |
| 163 | tryUp() | |
| 164 | tryDig() | |
| 165 | turtle.turnRight() | |
| 166 | turtle.turnRight() | |
| 167 | tryDig() | |
| 168 | tryForward() | |
| 169 | - | tryDown() |
| 169 | + | |
| 170 | tryDigDown() | |
| 171 | tryDown() | |
| 172 | tryDig() | |
| 173 | tryDigDown() | |
| 174 | tryDown() | |
| 175 | tryDig() | |
| 176 | tryBack() | |
| 177 | turtle.turnLeft() | |
| 178 | ||
| 179 | if n<length then | |
| 180 | tryDig() | |
| 181 | if not tryForward() then | |
| 182 | print( "Aborting Tunnel." ) | |
| 183 | break | |
| 184 | end | |
| 185 | else | |
| 186 | print( "Tunnel complete." ) | |
| 187 | end | |
| 188 | ||
| 189 | end | |
| 190 | ||
| 191 | ||
| 192 | print( "Returning to start..." ) | |
| 193 | ||
| 194 | -- Return to where we started | |
| 195 | turtle.turnLeft() | |
| 196 | turtle.turnLeft() | |
| 197 | tryUp() | |
| 198 | j = 0 | |
| 199 | while depth > 1 do | |
| 200 | if tryForward() then | |
| 201 | depth = depth - 1 | |
| 202 | j = j+1 | |
| 203 | if j == 5 then | |
| 204 | turtle.select(15) | |
| 205 | turtle.placeDown() | |
| 206 | j=0 | |
| 207 | end | |
| 208 | else | |
| 209 | turtle.dig() | |
| 210 | end | |
| 211 | end | |
| 212 | tryDown() | |
| 213 | turtle.turnRight() | |
| 214 | turtle.turnRight() | |
| 215 | ||
| 216 | ||
| 217 | print( "Tunnel complete." ) | |
| 218 | print( "Mined "..collected.." items total." ) |