SHOW:
|
|
- or go back to the newest paste.
| 1 | - | print("Program 1, Hello") |
| 1 | + | -- Turtle API http://computercraft.info/wiki/Turtle_(API) |
| 2 | -- Turtle Functions | |
| 3 | local depth = 0 | |
| 4 | local unloaded = 0 | |
| 5 | local collected = 0 | |
| 6 | ||
| 7 | local xPos,zPos = 0,0 | |
| 8 | local xDir,zDir = 0,1 | |
| 9 | ||
| 10 | local goTo -- Filled in further down | |
| 11 | local refuel -- Filled in further down | |
| 12 | ||
| 13 | local function unload( _bKeepOneFuelStack ) | |
| 14 | print( "Unloading items..." ) | |
| 15 | for n=1,16 do | |
| 16 | local nCount = turtle.getItemCount(n) | |
| 17 | if nCount > 0 then | |
| 18 | turtle.select(n) | |
| 19 | local bDrop = true | |
| 20 | if _bKeepOneFuelStack and turtle.refuel(0) then | |
| 21 | bDrop = false | |
| 22 | _bKeepOneFuelStack = false | |
| 23 | end | |
| 24 | if bDrop then | |
| 25 | turtle.drop() | |
| 26 | unloaded = unloaded + nCount | |
| 27 | end | |
| 28 | end | |
| 29 | end | |
| 30 | collected = 0 | |
| 31 | turtle.select(1) | |
| 32 | end | |
| 33 | local function returnSupplies() | |
| 34 | local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir | |
| 35 | print( "Returning to surface..." ) | |
| 36 | goTo( 0,0,0,0,-1 ) | |
| 37 | ||
| 38 | local fuelNeeded = 2*(x+y+z) + 1 | |
| 39 | if not refuel( fuelNeeded ) then | |
| 40 | unload( true ) | |
| 41 | print( "Waiting for fuel" ) | |
| 42 | while not refuel( fuelNeeded ) do | |
| 43 | sleep(1) | |
| 44 | end | |
| 45 | else | |
| 46 | unload( true ) | |
| 47 | end | |
| 48 | ||
| 49 | print( "Resuming mining..." ) | |
| 50 | goTo( x,y,z,xd,zd ) | |
| 51 | end | |
| 52 | ||
| 53 | local function collect() | |
| 54 | local bFull = true | |
| 55 | local nTotalItems = 0 | |
| 56 | for n=1,16 do | |
| 57 | local nCount = turtle.getItemCount(n) | |
| 58 | if nCount == 0 then | |
| 59 | bFull = false | |
| 60 | end | |
| 61 | nTotalItems = nTotalItems + nCount | |
| 62 | end | |
| 63 | ||
| 64 | if nTotalItems > collected then | |
| 65 | collected = nTotalItems | |
| 66 | if math.fmod(collected + unloaded, 50) == 0 then | |
| 67 | print( "Mined "..(collected + unloaded).." items." ) | |
| 68 | end | |
| 69 | end | |
| 70 | ||
| 71 | if bFull then | |
| 72 | print( "No empty slots left." ) | |
| 73 | return false | |
| 74 | end | |
| 75 | return true | |
| 76 | end | |
| 77 | function refuel( ammount ) | |
| 78 | local fuelLevel = turtle.getFuelLevel() | |
| 79 | if fuelLevel == "unlimited" then | |
| 80 | return true | |
| 81 | end | |
| 82 | ||
| 83 | local needed = ammount or (xPos + zPos + depth + 2) | |
| 84 | if turtle.getFuelLevel() < needed then | |
| 85 | local fueled = false | |
| 86 | for n=1,16 do | |
| 87 | if turtle.getItemCount(n) > 0 then | |
| 88 | turtle.select(n) | |
| 89 | if turtle.refuel(1) then | |
| 90 | while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do | |
| 91 | turtle.refuel(1) | |
| 92 | end | |
| 93 | if turtle.getFuelLevel() >= needed then | |
| 94 | turtle.select(1) | |
| 95 | return true | |
| 96 | end | |
| 97 | end | |
| 98 | end | |
| 99 | end | |
| 100 | turtle.select(1) | |
| 101 | return false | |
| 102 | end | |
| 103 | ||
| 104 | return true | |
| 105 | end | |
| 106 | local function tryForwards() | |
| 107 | if not refuel() then | |
| 108 | print( "Not enough Fuel" ) | |
| 109 | returnSupplies() | |
| 110 | end | |
| 111 | ||
| 112 | while not turtle.forward() do | |
| 113 | if turtle.detect() then | |
| 114 | if turtle.dig() then | |
| 115 | if not collect() then | |
| 116 | returnSupplies() | |
| 117 | end | |
| 118 | else | |
| 119 | return false | |
| 120 | end | |
| 121 | elseif turtle.attack() then | |
| 122 | if not collect() then | |
| 123 | returnSupplies() | |
| 124 | end | |
| 125 | else | |
| 126 | sleep( 0.5 ) | |
| 127 | end | |
| 128 | end | |
| 129 | ||
| 130 | xPos = xPos + xDir | |
| 131 | zPos = zPos + zDir | |
| 132 | return true | |
| 133 | end | |
| 134 | ||
| 135 | local function tryDown() | |
| 136 | if not refuel() then | |
| 137 | print( "Not enough Fuel" ) | |
| 138 | returnSupplies() | |
| 139 | end | |
| 140 | ||
| 141 | while not turtle.down() do | |
| 142 | if turtle.detectDown() then | |
| 143 | if turtle.digDown() then | |
| 144 | if not collect() then | |
| 145 | returnSupplies() | |
| 146 | end | |
| 147 | else | |
| 148 | return false | |
| 149 | end | |
| 150 | elseif turtle.attackDown() then | |
| 151 | if not collect() then | |
| 152 | returnSupplies() | |
| 153 | end | |
| 154 | else | |
| 155 | sleep( 0.5 ) | |
| 156 | end | |
| 157 | end | |
| 158 | ||
| 159 | depth = depth + 1 | |
| 160 | if math.fmod( depth, 10 ) == 0 then | |
| 161 | print( "Descended "..depth.." metres." ) | |
| 162 | end | |
| 163 | ||
| 164 | return true | |
| 165 | end | |
| 166 | ||
| 167 | local function turnLeft() | |
| 168 | turtle.turnLeft() | |
| 169 | xDir, zDir = -zDir, xDir | |
| 170 | end | |
| 171 | ||
| 172 | local function turnRight() | |
| 173 | turtle.turnRight() | |
| 174 | xDir, zDir = zDir, -xDir | |
| 175 | end | |
| 176 | ||
| 177 | function goTo( x, y, z, xd, zd ) | |
| 178 | while depth < y do | |
| 179 | if turtle.down() then | |
| 180 | depth = depth + 1 | |
| 181 | elseif turtle.digDown() or turtle.attackDown() then | |
| 182 | collect() | |
| 183 | else | |
| 184 | sleep( 0.5 ) | |
| 185 | end | |
| 186 | end | |
| 187 | ||
| 188 | if xPos > x then | |
| 189 | while xDir ~= -1 do | |
| 190 | turnLeft() | |
| 191 | end | |
| 192 | while xPos > x do | |
| 193 | if turtle.forward() then | |
| 194 | xPos = xPos - 1 | |
| 195 | elseif turtle.dig() or turtle.attack() then | |
| 196 | collect() | |
| 197 | else | |
| 198 | sleep( 0.5 ) | |
| 199 | end | |
| 200 | end | |
| 201 | elseif xPos < x then | |
| 202 | while xDir ~= 1 do | |
| 203 | turnLeft() | |
| 204 | end | |
| 205 | while xPos < x do | |
| 206 | if turtle.forward() then | |
| 207 | xPos = xPos + 1 | |
| 208 | elseif turtle.dig() or turtle.attack() then | |
| 209 | collect() | |
| 210 | else | |
| 211 | sleep( 0.5 ) | |
| 212 | end | |
| 213 | end | |
| 214 | end | |
| 215 | ||
| 216 | if zPos > z then | |
| 217 | while zDir ~= -1 do | |
| 218 | turnLeft() | |
| 219 | end | |
| 220 | while zPos > z do | |
| 221 | if turtle.forward() then | |
| 222 | zPos = zPos - 1 | |
| 223 | elseif turtle.dig() or turtle.attack() then | |
| 224 | collect() | |
| 225 | else | |
| 226 | sleep( 0.5 ) | |
| 227 | end | |
| 228 | end | |
| 229 | elseif zPos < z then | |
| 230 | while zDir ~= 1 do | |
| 231 | turnLeft() | |
| 232 | end | |
| 233 | while zPos < z do | |
| 234 | if turtle.forward() then | |
| 235 | zPos = zPos + 1 | |
| 236 | elseif turtle.dig() or turtle.attack() then | |
| 237 | collect() | |
| 238 | else | |
| 239 | sleep( 0.5 ) | |
| 240 | end | |
| 241 | end | |
| 242 | end | |
| 243 | ||
| 244 | while depth > y do | |
| 245 | if turtle.up() then | |
| 246 | depth = depth - 1 | |
| 247 | elseif turtle.digUp() or turtle.attackUp() then | |
| 248 | collect() | |
| 249 | else | |
| 250 | sleep( 0.5 ) | |
| 251 | end | |
| 252 | end | |
| 253 | ||
| 254 | while zDir ~= zd or xDir ~= xd do | |
| 255 | turnLeft() | |
| 256 | end | |
| 257 | end | |
| 258 | -- Functions | |
| 259 | function dedif(tf) | |
| 260 | if turtle.detect() then | |
| 261 | turtle.dig() | |
| 262 | end | |
| 263 | if tf then | |
| 264 | tryForwards() | |
| 265 | end | |
| 266 | end | |
| 267 | ||
| 268 | function turnLeftRight(left) | |
| 269 | if left then | |
| 270 | turnLeft() | |
| 271 | else | |
| 272 | turnRight() | |
| 273 | end | |
| 274 | end | |
| 275 | ||
| 276 | function lineCut(left) | |
| 277 | turnLeftRight(left) | |
| 278 | dedif(true) | |
| 279 | turnLeftRight(left) | |
| 280 | for i=1, 8 do -- dig 8 times in a line | |
| 281 | dedif(true) | |
| 282 | end | |
| 283 | end | |
| 284 | ||
| 285 | ||
| 286 | -- Main Program | |
| 287 | height = 50 -- right around the base of the leaves level | |
| 288 | for full=1, height do -- run though the entire tree | |
| 289 | if not refuel() then | |
| 290 | print( "Out of Fuel" ) | |
| 291 | return | |
| 292 | end | |
| 293 | turnLeft() | |
| 294 | for i=1, 4 do -- go left 4 times | |
| 295 | dedif(true) | |
| 296 | end | |
| 297 | ||
| 298 | for i=1, 9 do -- cycle through base of tree (tree is 9x9) | |
| 299 | left=false | |
| 300 | -- 1 = false (right) | |
| 301 | -- 2 = true (left) | |
| 302 | if i==1 or i==3 or i==5 or i==7 or i==9 then | |
| 303 | left = false | |
| 304 | else | |
| 305 | left = true | |
| 306 | end | |
| 307 | lineCut(left) | |
| 308 | end | |
| 309 | turnRight() | |
| 310 | for i=1, 9 do -- go 9 forward | |
| 311 | dedif(true) | |
| 312 | end | |
| 313 | turnRight() | |
| 314 | for i=1, 4 do -- go forward 4 | |
| 315 | dedif(true) | |
| 316 | end | |
| 317 | turnRight() | |
| 318 | if turtle.detectUp() then | |
| 319 | turtle.digUp() | |
| 320 | end | |
| 321 | turtle.up() | |
| 322 | end -- end full loop |