SHOW:
|
|
- or go back to the newest paste.
| 1 | -- It mines trees! - V2-- | |
| 2 | ||
| 3 | -- doesn't go out of the outOfBoundsMarker | |
| 4 | -- reverses at the stop marker | |
| 5 | -- errors at a halt maker | |
| 6 | ||
| 7 | -- ground markers | |
| 8 | local outOfBoundsMarker = "minecraft:grass_block" | |
| 9 | local outOfBoundsMarker2 = "minecraft:dirt" | |
| 10 | local pauseMarker = "atmospheric:arid_sandstone" | |
| 11 | local stopMakrer = "minecraft:cobblestone" | |
| 12 | local haltMarker = "minecraft:glass" | |
| 13 | local inventoryMarker = "environmental:willow_planks" | |
| 14 | local inventory = "environmental:willow_chest" | |
| 15 | ||
| 16 | ||
| 17 | --side markers | |
| 18 | local woodLog = "minecraft:birch_log" | |
| 19 | local sapling ="minecraft:birch_sapling" | |
| 20 | local ignore = "minecraft:snow" | |
| 21 | local realSnow ="snowrealmagic:snow" | |
| 22 | - | --local |
| 22 | + | |
| 23 | --other | |
| 24 | local maxZ = 6 --tree goes up this high | |
| 25 | local fuelSlot = 2 | |
| 26 | ||
| 27 | local emptyTurtleSlots = { 3,4,5,6,7,8,9,10,11,12,13,14 }
| |
| 28 | local keepFullSlots = {1,2}
| |
| 29 | ||
| 30 | function combine(slot) | |
| 31 | ---Make sure slot is not already full | |
| 32 | turtle.select(slot) | |
| 33 | if turtle.getItemCount() == 64 then return end | |
| 34 | ||
| 35 | if turtle.getItemCount() > 0 then | |
| 36 | for i=1,16 do | |
| 37 | turtle.select(i) | |
| 38 | if turtle.getItemCount() >0 then | |
| 39 | if turtle.compareTo(i) then | |
| 40 | turtle.transferTo(slot) | |
| 41 | end | |
| 42 | end | |
| 43 | end | |
| 44 | else | |
| 45 | return "slot empty" | |
| 46 | end | |
| 47 | end | |
| 48 | ||
| 49 | function pickupItems() | |
| 50 | turtle.suck() | |
| 51 | turtle.suckDown() | |
| 52 | turtle.suckUp() | |
| 53 | end | |
| 54 | ||
| 55 | function t_detect(f) | |
| 56 | detectDown = detectBlockNameDown() | |
| 57 | ||
| 58 | end | |
| 59 | ||
| 60 | function t_detectFwd(f) | |
| 61 | if turtle.detect() == true then | |
| 62 | local a,b = turtle.inspect() | |
| 63 | if b ~= false then | |
| 64 | detectFwd = b.name | |
| 65 | else | |
| 66 | detectFwd = "none" | |
| 67 | end | |
| 68 | else | |
| 69 | detectFwd = false | |
| 70 | - | send |
| 70 | + | |
| 71 | -- f() | |
| 72 | end | |
| 73 | ||
| 74 | function testBounds() | |
| 75 | local isBlock = turtle.detect == true | |
| 76 | haltIfNeccesary(detectDown) | |
| 77 | keepInBounds(detectDown,f) | |
| 78 | testInfFront(isBlock) | |
| 79 | end | |
| 80 | ||
| 81 | function testInfFront(isBlock) | |
| 82 | local blockInfort = turtle.inspect() | |
| 83 | if isBlock == true then | |
| 84 | if blockInfort == "minecraft:snow" then | |
| 85 | turtle.turnRight2() | |
| 86 | elseif blockInfort == woodLog then | |
| 87 | elseif blockInfort == sapling then | |
| 88 | elseif blockInfort == ignore then | |
| 89 | elseif blockInfort == realSnow then turtle.dig() | |
| 90 | else | |
| 91 | error("obstructed - there is something strange in the way!")
| |
| 92 | end | |
| 93 | end | |
| 94 | end | |
| 95 | ||
| 96 | ||
| 97 | ||
| 98 | function isBlockDown(block,blockDetected) | |
| 99 | if type(blockDetected) ~= "string" then | |
| 100 | print(string.sub(debug.traceback(),1,200)) | |
| 101 | error("invalid detection")
| |
| 102 | end | |
| 103 | ||
| 104 | if block == blockDetected then | |
| 105 | return true | |
| 106 | else | |
| 107 | return false | |
| 108 | end | |
| 109 | end | |
| 110 | ||
| 111 | function detectBlockNameDown() | |
| 112 | local a,b = turtle.inspectDown() | |
| 113 | return b.name or "none" | |
| 114 | ||
| 115 | end | |
| 116 | ||
| 117 | function haltIfNeccesary(detectDown) | |
| 118 | local halt = isBlockDown(haltMarker,detectDown) | |
| 119 | if halt == true then | |
| 120 | error("Halted")
| |
| 121 | end | |
| 122 | end | |
| 123 | ||
| 124 | ||
| 125 | function returnFromOutOfBounds(f) | |
| 126 | turtle.back2() | |
| 127 | turtle.turnRight2() | |
| 128 | end | |
| 129 | ||
| 130 | function keepInBounds(detectDown,f) | |
| 131 | if isBlockDown(outOfBoundsMarker,detectDown) == true then | |
| 132 | returnFromOutOfBounds(f) | |
| 133 | end | |
| 134 | ||
| 135 | -- out of bounds marker 2 | |
| 136 | if isBlockDown(outOfBoundsMarker2,detectDown,f) == true then | |
| 137 | returnFromOutOfBounds(f) | |
| 138 | end | |
| 139 | - | do |
| 139 | + | |
| 140 | ||
| 141 | do -- turtle.forward wrapped | |
| 142 | local f = turtle.forward | |
| 143 | function turtle.forward2()--detectDown) | |
| 144 | f() | |
| 145 | t_detect(f) | |
| 146 | testBounds() | |
| 147 | pickupItems() | |
| 148 | return true | |
| 149 | end | |
| 150 | - | do |
| 150 | + | |
| 151 | ||
| 152 | do -- turtle.back wrapped | |
| 153 | local b = turtle.back | |
| 154 | function turtle.back2() | |
| 155 | b() | |
| 156 | t_detect(b) | |
| 157 | testBounds() | |
| 158 | pickupItems() | |
| 159 | end | |
| 160 | - | do |
| 160 | + | |
| 161 | ||
| 162 | do -- turtle.turnLeft wraped | |
| 163 | local l = turtle.turnLeft | |
| 164 | function turtle.turnLeft2() | |
| 165 | --print("l1")
| |
| 166 | l() | |
| 167 | t_detectFwd(l) | |
| 168 | pickupItems() | |
| 169 | --print("l2")
| |
| 170 | end | |
| 171 | - | do |
| 171 | + | |
| 172 | ||
| 173 | do -- turtle.turnRight wrapped | |
| 174 | local r= turtle.turnRight | |
| 175 | function turtle.turnRight2() | |
| 176 | --print("r1")
| |
| 177 | r() | |
| 178 | t_detectFwd(r) | |
| 179 | pickupItems() | |
| 180 | --print("r2")
| |
| 181 | end | |
| 182 | - | do |
| 182 | + | |
| 183 | ||
| 184 | do -- turtle.turnUp wrapped | |
| 185 | local u = turtle.up | |
| 186 | function turtle.up2() | |
| 187 | local try = u() | |
| 188 | print("going up")
| |
| 189 | while try == false do | |
| 190 | print("failed going up retying")
| |
| 191 | turtle.digUp() | |
| 192 | try = u() | |
| 193 | end | |
| 194 | pickupItems() | |
| 195 | end | |
| 196 | - | do |
| 196 | + | |
| 197 | ||
| 198 | do --turtle.down wrapped | |
| 199 | local d = turtle.down | |
| 200 | function turtle.down2() | |
| 201 | local try = d() | |
| 202 | print("going down")
| |
| 203 | while try == false do | |
| 204 | print("failed going down retying")
| |
| 205 | turtle.digDown() | |
| 206 | try = d() | |
| 207 | end | |
| 208 | pickupItems() | |
| 209 | end | |
| 210 | - | function sort() |
| 210 | + | |
| 211 | ||
| 212 | function sort() | |
| 213 | for i,v in pairs(keepFullSlots) do | |
| 214 | combine(v) | |
| 215 | end | |
| 216 | end | |
| 217 | ||
| 218 | function insertToInventory() | |
| 219 | sort() | |
| 220 | --local emptyTurtleSlots | |
| 221 | for i,v in pairs(emptyTurtleSlots) do | |
| 222 | turtle.select(v) | |
| 223 | turtle.drop() | |
| 224 | end | |
| 225 | end | |
| 226 | ||
| 227 | ||
| 228 | function FindInventory() | |
| 229 | local i=1 | |
| 230 | while detectFwd ~= inventory do | |
| 231 | turtle.turnLeft2() | |
| 232 | i = i + 1 | |
| 233 | if i == 10 then | |
| 234 | error("unable to find inventory")
| |
| 235 | end | |
| 236 | end | |
| 237 | insertToInventory() | |
| 238 | for i2=1, i-1 do | |
| 239 | turtle.turnRight2() | |
| 240 | end | |
| 241 | ||
| 242 | end | |
| 243 | ||
| 244 | function checkForChestBlock() | |
| 245 | if detectDown == inventoryMarker then | |
| 246 | FindInventory() | |
| 247 | end | |
| 248 | ||
| 249 | end | |
| 250 | ||
| 251 | ||
| 252 | - | local maxZ = 10 |
| 252 | + | |
| 253 | ||
| 254 | ||
| 255 | ||
| 256 | function isBlock(block) | |
| 257 | if isBlock ~= false then | |
| 258 | --local a,b = turtle.inspect() | |
| 259 | local blockName = detectFwd | |
| 260 | if blockName == block then | |
| 261 | return true | |
| 262 | else | |
| 263 | return false | |
| 264 | end | |
| 265 | else | |
| 266 | return false | |
| 267 | end | |
| 268 | end | |
| 269 | ||
| 270 | function searchSlotsFor(blockId) | |
| 271 | for slot = 1,16 do | |
| 272 | turtle.select(slot) | |
| 273 | --local i = turtle. | |
| 274 | local i = turtle.getItemDetail() or "nothing" | |
| 275 | if i ~= "nothing" then | |
| 276 | if i.name == blockId then | |
| 277 | return slot | |
| 278 | else | |
| 279 | end | |
| 280 | end | |
| 281 | end | |
| 282 | return "nothing found" | |
| 283 | end | |
| 284 | ||
| 285 | function findBlock(blockId) | |
| 286 | local i = turtle.getItemDetail() or "nothing" | |
| 287 | if i ~= "nothing" and i.name == blockId then | |
| 288 | return true | |
| 289 | else | |
| 290 | slot = searchSlotsFor(blockId) | |
| 291 | if slot ~= "nothing found" then | |
| 292 | return true | |
| 293 | else | |
| 294 | print(tostring(blockId) .. " not found") | |
| 295 | ||
| 296 | local i = 0 | |
| 297 | local ingnoreTime = 5 | |
| 298 | while searchSlotsFor(blockId) == "nothing found" do | |
| 299 | i = i + 1 | |
| 300 | sleep(1) | |
| 301 | write(".")
| |
| 302 | if type(ingnoreTime) == "number" then | |
| 303 | if i == ingnoreTime then | |
| 304 | print("ignoring")
| |
| 305 | return "none" | |
| 306 | end | |
| 307 | end | |
| 308 | ||
| 309 | ||
| 310 | end | |
| 311 | end | |
| 312 | ||
| 313 | end | |
| 314 | end | |
| 315 | ||
| 316 | function ifLogDigTree() | |
| 317 | local isLog = isBlock(woodLog) == true | |
| 318 | - | turtle.dig() |
| 318 | + | |
| 319 | turtle.dig() | |
| 320 | for i=1,maxZ do | |
| 321 | turtle.up2() | |
| 322 | turtle.dig() | |
| 323 | turtle.digUp() | |
| 324 | end | |
| 325 | ||
| 326 | for i=1,maxZ do | |
| 327 | turtle.down2() | |
| 328 | end | |
| 329 | ||
| 330 | if findBlock(sapling) == true then | |
| 331 | turtle.place() | |
| 332 | end | |
| 333 | end | |
| 334 | ||
| 335 | if not isLog then | |
| 336 | ||
| 337 | ||
| 338 | local isRealSnow = isBlock(realSnow) == true | |
| 339 | if isRealSnow then | |
| 340 | turtle.dig() | |
| 341 | end | |
| 342 | ||
| 343 | local isSnow = isBlock(ignore) == true | |
| 344 | if isSnow then | |
| 345 | turtle.dig() | |
| 346 | ||
| 347 | --local isSapling = isBlock(sapling) | |
| 348 | else | |
| 349 | turtle.place() | |
| 350 | end | |
| 351 | ||
| 352 | end | |
| 353 | ||
| 354 | end | |
| 355 | ||
| 356 | ifLogDigTree() | |
| 357 | ||
| 358 | ||
| 359 | end | |
| 360 | ||
| 361 | ||
| 362 | detectDown = detectBlockNameDown() --- updates in turtle.forward2() | |
| 363 | function paceBlock(pauseAt,stopAt,direction) | |
| 364 | ||
| 365 | print(detectDown) | |
| 366 | turtle.forward2()--detectDown) | |
| 367 | if direction == "right" then | |
| 368 | checkForChestBlock() | |
| 369 | end | |
| 370 | ||
| 371 | local pause = isBlockDown(pauseAt,detectDown) | |
| 372 | local stop = isBlockDown(stopAt,detectDown) | |
| 373 | ||
| 374 | if pause == true then | |
| 375 | --pauseTurn() | |
| 376 | turtle.turnRight2() | |
| 377 | digTree() | |
| 378 | turtle.turnLeft2() | |
| 379 | turtle.turnLeft2() | |
| 380 | digTree() | |
| 381 | turtle.turnRight2() | |
| 382 | --resumeTurn() | |
| 383 | elseif stop == true then | |
| 384 | return "stop" | |
| 385 | end | |
| 386 | ||
| 387 | -- | |
| 388 | end | |
| 389 | ||
| 390 | ||
| 391 | function paceBlockRepeatedly(pauseAt,stopAt,direction) | |
| 392 | while true do | |
| 393 | topUpFuel() | |
| 394 | if paceBlock(pauseAt,stopAt,direction) == "stop" then return "stop" end | |
| 395 | end | |
| 396 | end | |
| 397 | ||
| 398 | function paceRight(pauseAt,stopAt) | |
| 399 | --turtle.turnRight() | |
| 400 | paceBlockRepeatedly(pauseAt,stopAt,"right") | |
| 401 | turtle.turnRight2() | |
| 402 | turtle.turnRight2() | |
| 403 | --turtle.turnRight() | |
| 404 | ||
| 405 | end | |
| 406 | ||
| 407 | ||
| 408 | function paceLeft(pauseAt,stopAt) | |
| 409 | -- turtle.turnLeft() | |
| 410 | paceBlockRepeatedly(pauseAt,stopAt,"left") | |
| 411 | turtle.turnLeft2() | |
| 412 | turtle.turnLeft2() | |
| 413 | -- turtle.turnLeft() | |
| 414 | --paceRight() | |
| 415 | end | |
| 416 | ||
| 417 | function pace(pauseAt,stopAt,direction) | |
| 418 | if direction == "right" then | |
| 419 | paceRight(pauseAt,stopAt) | |
| 420 | elseif direction == "left" then | |
| 421 | paceLeft(pauseAt,stopAt) | |
| 422 | end | |
| 423 | end | |
| 424 | ||
| 425 | ||
| 426 | function topUpFuel() | |
| 427 | local fuelBuffer = 1000 | |
| 428 | ||
| 429 | ||
| 430 | local fuelLevel = turtle.getFuelLevel() | |
| 431 | if fuelLevel < fuelBuffer then | |
| 432 | local prevSlot = turtle.getSelectedSlot() | |
| 433 | print("attempting to refuel")
| |
| 434 | ||
| 435 | local i = 1 | |
| 436 | while turtle.getFuelLevel() < fuelBuffer do | |
| 437 | ||
| 438 | turtle.select(fuelSlot) | |
| 439 | turtle.refuel(1) | |
| 440 | i = i+1 | |
| 441 | ||
| 442 | ||
| 443 | end | |
| 444 | turtle.select(prevSlot) | |
| 445 | else | |
| 446 | ||
| 447 | end | |
| 448 | ||
| 449 | end | |
| 450 | ||
| 451 | function turnToCorretSide() | |
| 452 | turtle.turnRight2() | |
| 453 | while detectFwd ~= false do | |
| 454 | turtle.turnLeft2() | |
| 455 | print(detectFwd) | |
| 456 | end | |
| 457 | ||
| 458 | --turtle.turnLeft2() | |
| 459 | end | |
| 460 | ||
| 461 | ||
| 462 | ------ main | |
| 463 | turtle.select(1) | |
| 464 | topUpFuel() | |
| 465 | turnToCorretSide() | |
| 466 | while true do | |
| 467 | topUpFuel() | |
| 468 | paceLeft(pauseMarker,stopMakrer) | |
| 469 | topUpFuel() | |
| 470 | paceRight(pauseMarker,stopMakrer) | |
| 471 | ||
| 472 | end |