SHOW:
|
|
- or go back to the newest paste.
| 1 | local S_WOOD = 1 | |
| 2 | local S_SAPLING = 2 | |
| 3 | local S_MEAL = 3 | |
| 4 | local S_DIRT = 15 | |
| 5 | - | local SLEEP_TIME = 120 |
| 5 | + | |
| 6 | local OS_VERSION = "CraftOS 1.6" | |
| 7 | ||
| 8 | local sleep_time = 120 | |
| 9 | local qty_wood = 0 | |
| 10 | local count_checked = 0 | |
| 11 | - | turtle.select(slot) |
| 11 | + | |
| 12 | - | while turtle.getItemCount(slot) < n do |
| 12 | + | local t = turtle |
| 13 | ||
| 14 | function suckAllSapling() | |
| 15 | t.select(S_SAPLING) | |
| 16 | t.suck() | |
| 17 | t.suckUp() | |
| 18 | t.suckDown() | |
| 19 | end | |
| 20 | - | local qty = turtle.getFuelLevel() |
| 20 | + | |
| 21 | function waitForEnoughItems(itemName, n, slot) | |
| 22 | t.select(slot) | |
| 23 | - | turtle.select(slot) |
| 23 | + | while t.getItemCount(slot) < n do |
| 24 | - | turtle.refuel() |
| 24 | + | |
| 25 | - | qty = turtle.getFuelLevel() |
| 25 | + | |
| 26 | os.pullEvent("turtle_inventory")
| |
| 27 | end | |
| 28 | end | |
| 29 | ||
| 30 | function waitForEnoughFuel(minLevel, slot) | |
| 31 | local qty = t.getFuelLevel() | |
| 32 | local refuel = function() | |
| 33 | if qty > minLevel then return qty end | |
| 34 | t.select(slot) | |
| 35 | t.refuel() | |
| 36 | qty = t.getFuelLevel() | |
| 37 | print("fuel: ",qty)
| |
| 38 | - | function isWood(direction) |
| 38 | + | |
| 39 | - | turtle.select(S_WOOD) |
| 39 | + | |
| 40 | - | if direction == "top" then |
| 40 | + | |
| 41 | - | return turtle.compareUp() |
| 41 | + | |
| 42 | print(string.format("Insert fuel-items into slot %d: %d/%d", slot, qty, minLevel))
| |
| 43 | - | return turtle.compare() |
| 43 | + | |
| 44 | os.pullEvent("turtle_inventory")
| |
| 45 | end | |
| 46 | end | |
| 47 | ||
| 48 | - | turtle.select(S_WOOD) |
| 48 | + | function sel(slot, func) |
| 49 | - | turtle.dig() |
| 49 | + | t.select(slot) |
| 50 | - | turtle.forward() |
| 50 | + | return func() |
| 51 | end | |
| 52 | - | while turtle.detectUp() do |
| 52 | + | |
| 53 | - | turtle.digUp() |
| 53 | + | function useMeals() |
| 54 | - | turtle.up() |
| 54 | + | while (not sel(S_WOOD, t.compare)) and (sel(S_MEAL, t.getItemCount) > 2) do |
| 55 | t.select(S_MEAL) | |
| 56 | t.place() | |
| 57 | end | |
| 58 | end | |
| 59 | - | turtle.select(S_SAPLING) |
| 59 | + | |
| 60 | - | while not turtle.detectDown() do |
| 60 | + | |
| 61 | t.select(S_WOOD) | |
| 62 | - | turtle.dig() |
| 62 | + | t.dig() |
| 63 | - | turtle.turnRight() |
| 63 | + | t.forward() |
| 64 | ||
| 65 | - | turtle.down() |
| 65 | + | while t.detectUp() do |
| 66 | t.digUp() | |
| 67 | t.up() | |
| 68 | end | |
| 69 | - | function placeItem(slot, direction) |
| 69 | + | |
| 70 | - | turtle.select(slot) |
| 70 | + | |
| 71 | function cutLeaves() | |
| 72 | - | turtle.placeDown() |
| 72 | + | t.select(S_SAPLING) |
| 73 | while not t.detectDown() do | |
| 74 | - | turtle.place() |
| 74 | + | |
| 75 | t.dig() | |
| 76 | suckAllSapling() | |
| 77 | t.turnRight() | |
| 78 | end | |
| 79 | - | turtle.select(S_DIRT) |
| 79 | + | t.down() |
| 80 | - | turtle.digDown() |
| 80 | + | |
| 81 | - | turtle.down() |
| 81 | + | |
| 82 | ||
| 83 | function getSapling() | |
| 84 | - | turtle.select(S_SAPLING) |
| 84 | + | t.select(S_DIRT) |
| 85 | - | turtle.suck() |
| 85 | + | t.digDown() |
| 86 | - | turtle.turnRight() |
| 86 | + | t.down() |
| 87 | ||
| 88 | for i=1,4 do | |
| 89 | - | turtle.up() |
| 89 | + | suckAllSapling() |
| 90 | - | placeItem(S_DIRT, "down") |
| 90 | + | t.turnRight() |
| 91 | - | turtle.back() |
| 91 | + | |
| 92 | - | placeItem(S_SAPLING) |
| 92 | + | |
| 93 | t.up() | |
| 94 | sel(S_DIRT, t.placeDown) | |
| 95 | t.back() | |
| 96 | sel(S_SAPLING, t.place) | |
| 97 | - | turtle.select(slot) |
| 97 | + | |
| 98 | - | local n = turtle.getItemCount() |
| 98 | + | |
| 99 | function dropItem(slot, nLeaves) | |
| 100 | t.select(slot) | |
| 101 | local n = t.getItemCount() | |
| 102 | if n > nLeaves then | |
| 103 | if slot == S_WOOD then | |
| 104 | - | return turtle.drop(n-nLeaves) |
| 104 | + | |
| 105 | print(qty_wood) | |
| 106 | end | |
| 107 | return t.drop(n-nLeaves) | |
| 108 | else | |
| 109 | return false, "No items to drop" | |
| 110 | end | |
| 111 | - | turtle.select(slot) |
| 111 | + | |
| 112 | - | local n = turtle.getItemSpace() |
| 112 | + | |
| 113 | function fillItemSlot(slot, direction) | |
| 114 | - | return turtle.suckDown(n) |
| 114 | + | t.select(slot) |
| 115 | local n = t.getItemSpace() | |
| 116 | - | return turtle.suck(n) |
| 116 | + | if n < 1 then |
| 117 | return false,"No space for items" | |
| 118 | else | |
| 119 | print("Item Space is ",n)
| |
| 120 | end | |
| 121 | - | turtle.back() |
| 121 | + | |
| 122 | - | turtle.back() |
| 122 | + | return t.suckDown(n) |
| 123 | - | turtle.back() |
| 123 | + | |
| 124 | - | turtle.turnRight() |
| 124 | + | return t.suck(n) |
| 125 | - | turtle.turnRight() |
| 125 | + | |
| 126 | end | |
| 127 | ||
| 128 | function repeatFunc(n, func) | |
| 129 | - | if turtle.getFuelLevel() < 80 then |
| 129 | + | for i=1,n do |
| 130 | - | turtle.select(S_CCOAL) |
| 130 | + | func() |
| 131 | - | assert(turtle.refuel(1), "FuelLevel is low.") |
| 131 | + | |
| 132 | end | |
| 133 | ||
| 134 | - | turtle.up() |
| 134 | + | |
| 135 | - | turtle.up() |
| 135 | + | repeatFunc(3,t.back) |
| 136 | - | assert(dropItem(S_WOOD, 2), "The chest for woods is full.") |
| 136 | + | repeatFunc(2,t.turnRight) |
| 137 | ||
| 138 | - | turtle.down() |
| 138 | + | |
| 139 | - | turtle.down() |
| 139 | + | |
| 140 | - | turtle.turnLeft() |
| 140 | + | if t.getFuelLevel() < 80 then |
| 141 | t.select(S_CCOAL) | |
| 142 | assert(t.refuel(1), "FuelLevel is low.") | |
| 143 | end | |
| 144 | - | turtle.turnLeft() |
| 144 | + | |
| 145 | - | turtle.forward() |
| 145 | + | repeatFunc(2,t.up) |
| 146 | - | turtle.forward() |
| 146 | + | local b_status, err_msg = dropItem(S_WOOD, 2) |
| 147 | - | turtle.forward() |
| 147 | + | if (not b_status) and (err_msg == "Inventory full") then |
| 148 | assert(b_status, err_msg) | |
| 149 | end | |
| 150 | ||
| 151 | repeatFunc(2,t.down) | |
| 152 | t.turnLeft() | |
| 153 | dropItem(S_WOOD, 2) | |
| 154 | dropItem(S_SAPLING, 32) | |
| 155 | ||
| 156 | repeatFunc(2,t.turnLeft) | |
| 157 | fillItemSlot(S_MEAL) | |
| 158 | ||
| 159 | t.turnRight() | |
| 160 | - | local isWood = isWood() |
| 160 | + | repeatFunc(3,t.forward) |
| 161 | - | if isWood or count_checked > 1 then |
| 161 | + | |
| 162 | ||
| 163 | -- main | |
| 164 | assert(os.version() == OS_VERSION, "required "..OS_VERSION) | |
| 165 | ||
| 166 | waitForEnoughItems("BirchWood", 1, S_WOOD)
| |
| 167 | waitForEnoughItems("BirchSapling", 2, S_SAPLING)
| |
| 168 | - | if isWood then makeCoal() end |
| 168 | + | |
| 169 | waitForEnoughFuel(80, S_CCOAL) | |
| 170 | ||
| 171 | - | print("It isn't wood.")
|
| 171 | + | |
| 172 | if sel(S_MEAL, t.getItemCount) > 2 then | |
| 173 | - | turtle.select(S_SAPLING) |
| 173 | + | sleep_time = 20 |
| 174 | - | turtle.suckUp() |
| 174 | + | |
| 175 | - | turtle.suckDown() |
| 175 | + | sleep_time = 120 |
| 176 | - | turtle.suck() |
| 176 | + | |
| 177 | - | os.sleep(SLEEP_TIME) |
| 177 | + | local limit = math.floor(300/sleep_time) |
| 178 | if sel(S_WOOD, t.compare) or sel(S_MEAL, t.getItemCount) or count_checked>=limit then | |
| 179 | print("Start cutting.")
| |
| 180 | useMeals() | |
| 181 | count_checked = 0 | |
| 182 | cutWoods() | |
| 183 | cutLeaves() | |
| 184 | getSapling() | |
| 185 | ||
| 186 | if sel(S_WOOD, t.getItemCount)>32 then makeCoal() end | |
| 187 | else | |
| 188 | count_checked = count_checked+1 | |
| 189 | print(string.format("It isn't wood: %d/%d",count_checked, limit))
| |
| 190 | end | |
| 191 | suckAllSapling() | |
| 192 | os.sleep(sleep_time) | |
| 193 | end |