harvest.lua
140 lines · 2.5 KB
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/Michael-Reeves808/harvest.lua
| 1 | local TOTAL_SESSION_POTATOES = 0 |
| 2 | local SLOT_COUNT = 16 |
| 3 | |
| 4 | function checkFuel() |
| 5 | turtle.select(1) |
| 6 | |
| 7 | if(turtle.getFuelLevel() < 50) then |
| 8 | print("Attempting Refuel...") |
| 9 | for slot = 1, SLOT_COUNT, 1 do |
| 10 | turtle.select(slot) |
| 11 | if(turtle.refuel(1)) then |
| 12 | return true |
| 13 | end |
| 14 | end |
| 15 | |
| 16 | return false |
| 17 | else |
| 18 | return true |
| 19 | end |
| 20 | end |
| 21 | |
| 22 | function getItemIndex(itemName) |
| 23 | for slot = 1, SLOT_COUNT, 1 do |
| 24 | local item = turtle.getItemDetail(slot) |
| 25 | if(item ~= nil) then |
| 26 | if(item["name"] == itemName) then |
| 27 | return slot |
| 28 | end |
| 29 | end |
| 30 | end |
| 31 | end |
| 32 | |
| 33 | function dropPotatoes(potatoIndex) |
| 34 | -- Drop normal potatoes |
| 35 | numPotatoes = turtle.getItemCount(potatoIndex) - 1 |
| 36 | |
| 37 | if numPotatoes > 0 |
| 38 | then |
| 39 | turtle.select(potatoIndex) |
| 40 | turtle.dropDown(numPotatoes) |
| 41 | end |
| 42 | |
| 43 | -- Drop poisin potatoes |
| 44 | potatoIndex = getItemIndex("minecraft:poisonous_potato") |
| 45 | if potatoIndex ~= nil |
| 46 | then |
| 47 | turtle.select(potatoIndex) |
| 48 | turtle.dropDown() |
| 49 | end |
| 50 | |
| 51 | end |
| 52 | |
| 53 | function harvestRow() |
| 54 | isBlock, data = turtle.inspect() |
| 55 | |
| 56 | if(isBlock) |
| 57 | then |
| 58 | if (data['state']['age'] == 7) |
| 59 | then |
| 60 | potatoIndex = getItemIndex("minecraft:potato") |
| 61 | turtle.select(potatoIndex) |
| 62 | turtle.dig() |
| 63 | turtle.place() |
| 64 | succ() |
| 65 | |
| 66 | dropPotatoes(potatoIndex) |
| 67 | TOTAL_SESSION_POTATOES = TOTAL_SESSION_POTATOES + numPotatoes |
| 68 | end |
| 69 | else |
| 70 | succ() |
| 71 | potatoIndex = getItemIndex("minecraft:potato") |
| 72 | turtle.select(potatoIndex) |
| 73 | canPlace = turtle.place() |
| 74 | |
| 75 | if(not canPlace) |
| 76 | then |
| 77 | turtle.turnLeft() |
| 78 | end |
| 79 | end |
| 80 | |
| 81 | succ() |
| 82 | end |
| 83 | |
| 84 | function isGrown() |
| 85 | isBlock, data = turtle.inspect() |
| 86 | |
| 87 | if(isBlock) |
| 88 | then |
| 89 | if (data['state']['age'] == 7) |
| 90 | then |
| 91 | return true |
| 92 | end |
| 93 | end |
| 94 | return false |
| 95 | end |
| 96 | |
| 97 | function waitForGrowth() |
| 98 | while(not isGrown()) |
| 99 | do |
| 100 | sleep(5) |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | function checkLeft() |
| 105 | turtle.turnLeft() |
| 106 | if (turtle.detect()) |
| 107 | then |
| 108 | return true |
| 109 | end |
| 110 | turtle.forward() |
| 111 | turtle.turnRight() |
| 112 | return false |
| 113 | end |
| 114 | |
| 115 | |
| 116 | function succ() |
| 117 | for i = 1, 6, 1 |
| 118 | do |
| 119 | turtle.suck() |
| 120 | end |
| 121 | end |
| 122 | |
| 123 | |
| 124 | print('Beginning Harvest...') |
| 125 | |
| 126 | while(1) |
| 127 | do |
| 128 | harvestRow() |
| 129 | |
| 130 | if (checkLeft()) |
| 131 | then |
| 132 | print(TOTAL_SESSION_POTATOES .. ' potatoes harvested') |
| 133 | turtle.turnLeft() |
| 134 | checkFuel() |
| 135 | waitForGrowth() |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | |
| 140 |