SHOW:
|
|
- or go back to the newest paste.
1 | - | -- chtreefarm |
1 | + | -- chfirfarm |
2 | -- refill fuel in slot FUEL_SLOT | |
3 | - | -- requires at least farmLength saplings in slot SAPLING_SLOT |
3 | + | -- requires at least 4 saplings in slot SAPLING_SLOT |
4 | - | -- wood deposit at the beginning of the farm, saplings deposit at the end |
4 | + | -- 2x2 planting area in front (and to the right) of the turtle |
5 | -- must start with 4 saplings planted in the planting area or even the tree can be already grown | |
6 | -- wood deposit anywhere behind the turtle, but there has to be some stopping block | |
7 | -- auxiliary sapling storage somewhere below the starting turtle position (directly under it) where fallen saplings should be collected | |
8 | -- if the turtle stops at the wood deposit, it means it's full and needs to be emptied | |
9 | ||
10 | - | local MIN_FUEL_FOR_STEP = 26 |
10 | + | |
11 | - | local args = {...} |
11 | + | |
12 | - | local farmLength = tonumber(args[1]) |
12 | + | |
13 | local SAPLING_TMP_SLOT = 3 | |
14 | local MIN_SAPLINGS = 5 | |
15 | local SAPLINGS_BUFFER = 64 | |
16 | - | local treesChopped = 0 |
16 | + | |
17 | - | local saplingsPlanted = 0 |
17 | + | local MAX_WOOD = 620 |
18 | local MIN_FUEL_FOR_CYCLE = 360 | |
19 | - | local farmingStarted = false |
19 | + | local MAX_WOOD_STORAGE_DISTANCE = 32 |
20 | local REPEATED_DETECTION_COUNT = 3 | |
21 | local REPEATED_DETECTION_TICK = 1 | |
22 | local NEXT_DETECTION_TICK = 1 | |
23 | - | turtle.forward() |
23 | + | local TREE_GROW_SLEEP_TIME = 40 |
24 | local posX = 0 | |
25 | - | if orient==0 then |
25 | + | |
26 | - | posX = posX+1 |
26 | + | |
27 | local distanceTraveled = 0 | |
28 | - | if orient==2 then |
28 | + | local auxiliaryDepth = 0 |
29 | - | posX = posX-1 |
29 | + | |
30 | function forward(n) | |
31 | for i=1,n do | |
32 | if turtle.forward() then | |
33 | distanceTraveled = distanceTraveled + 1 | |
34 | if orient==0 then | |
35 | posX = posX+1 | |
36 | end | |
37 | if orient==2 then | |
38 | posX = posX-1 | |
39 | end | |
40 | else | |
41 | return false | |
42 | end | |
43 | end | |
44 | return true | |
45 | end | |
46 | function back(n) | |
47 | - | function transferSaplings() |
47 | + | |
48 | - | result = true |
48 | + | if turtle.back() then |
49 | distanceTraveled = distanceTraveled + 1 | |
50 | - | if not i==SAPLING_SLOT then |
50 | + | if orient==0 then |
51 | posX = posX-1 | |
52 | - | if turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then |
52 | + | |
53 | - | turtle.transferTo(SAPLING_SLOT) |
53 | + | if orient==2 then |
54 | posX = posX+1 | |
55 | end | |
56 | else | |
57 | return false | |
58 | - | if turtle.getItemCount(SAPLING_SLOT) < 2 then |
58 | + | |
59 | - | print("Not enough saplings in slot "..SAPLING_SLOT.."\n") |
59 | + | |
60 | - | -- result = false |
60 | + | |
61 | - | while turtle.getItemCount(SAPLING_SLOT) < 2 do |
61 | + | |
62 | function left() | |
63 | turtle.turnLeft() | |
64 | orient = orient-1 | |
65 | while orient<0 do | |
66 | orient = orient+4 | |
67 | - | function stripLeaves() |
67 | + | |
68 | end | |
69 | - | if turtle.detectUp() then |
69 | + | |
70 | turtle.turnRight() | |
71 | orient = orient+1 | |
72 | - | for i=1,3 do |
72 | + | |
73 | - | right() |
73 | + | |
74 | - | if turtle.detect() then |
74 | + | |
75 | - | turtle.dig() |
75 | + | |
76 | function reorderSaplings() | |
77 | local saplingsReordered = 0 | |
78 | - | transferSaplings() |
78 | + | |
79 | if i~=SAPLING_SLOT then | |
80 | turtle.select(i) | |
81 | os.sleep(0.1) | |
82 | - | function chop() |
82 | + | if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then |
83 | while (turtle.getItemCount(i)>0) and (turtle.getItemCount(SAPLING_SLOT)<64) and (turtle.transferTo(SAPLING_SLOT,1)) do | |
84 | - | while turtle.detect() do |
84 | + | saplingsReordered = saplingsReordered + 1 |
85 | os.sleep(0.1) | |
86 | end | |
87 | - | stripLeaves() |
87 | + | |
88 | end | |
89 | end | |
90 | print("reordered "..saplingsReordered.." saplings") | |
91 | end | |
92 | function dumpExcessSaplings() | |
93 | for i = 1,16 do | |
94 | if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then | |
95 | turtle.select(i) | |
96 | if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then | |
97 | turtle.drop() | |
98 | end | |
99 | - | if height>2 then |
99 | + | |
100 | - | treesChopped = treesChopped + 1 |
100 | + | |
101 | end | |
102 | ||
103 | - | function plant() |
103 | + | function checkSaplings() |
104 | reorderSaplings() | |
105 | - | if turtle.getItemCount(SAPLING_SLOT) < 2 then |
105 | + | if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then |
106 | - | if not transferSaplings() then |
106 | + | print("insufficient saplings, waiting for decaying leaves...") |
107 | os.sleep(60) | |
108 | print("refilling saplings from auxiliary storage...") | |
109 | while turtle.down() do | |
110 | auxiliaryDepth = auxiliaryDepth + 1 | |
111 | distanceTraveled = distanceTraveled + 1 | |
112 | - | os.sleep(0.5) |
112 | + | |
113 | - | if turtle.detect() and turtle.compare() then |
113 | + | |
114 | - | saplingsPlanted = saplingsPlanted + 1 |
114 | + | |
115 | if turtle.suckDown(SAPLINGS_BUFFER) then | |
116 | print("some saplings found in auxiliary storage") | |
117 | else | |
118 | print("getting more saplings failed") | |
119 | - | function waitForTree() |
119 | + | |
120 | reorderSaplings() | |
121 | - | local waitingTime = 0 |
121 | + | if turtle.getItemCount(SAPLING_SLOT) >= SAPLINGS_BUFFER then |
122 | print("returning excess saplings...") | |
123 | dumpExcessSaplings() | |
124 | - | while treeDetectedCount < 7 do -- because sometimes turtle does not compare reliably |
124 | + | |
125 | while auxiliaryDepth>0 do | |
126 | turtle.up() | |
127 | auxiliaryDepth = auxiliaryDepth - 1 | |
128 | distanceTraveled = distanceTraveled + 1 | |
129 | end | |
130 | end | |
131 | - | waitingTime = waitingTime + 2 |
131 | + | if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then |
132 | - | if waitingTime > 30 and turtle.detectUp() then |
132 | + | print("Not enough saplings in slot "..SAPLING_SLOT..", waiting for saplings...\n") |
133 | - | height = 0 |
133 | + | while turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS do |
134 | - | while turtle.detectUp() do |
134 | + | |
135 | - | turtle.select(SAPLING_TMP_SLOT) |
135 | + | |
136 | - | turtle.digUp() |
136 | + | |
137 | - | if turtle.up() then |
137 | + | |
138 | - | height = height + 1 |
138 | + | |
139 | - | distanceTraveled = distanceTraveled + 1 |
139 | + | |
140 | - | turtle.dig() |
140 | + | |
141 | function chop() --needs 5 saplings and 80 fuel | |
142 | print("chopping tree...") | |
143 | - | for i=1,height do |
143 | + | |
144 | - | turtle.down() |
144 | + | turtle.select(WOOD_SLOT) |
145 | - | distanceTraveled = distanceTraveled + 1 |
145 | + | turtle.dig() |
146 | forward(1) | |
147 | ||
148 | while turtle.detect() or turtle.detectUp() do | |
149 | turtle.select(WOOD_SLOT) | |
150 | - | function handleTree() |
150 | + | |
151 | - | if not turtle.detect() then |
151 | + | |
152 | - | if not plant() then |
152 | + | |
153 | height = height + 1 | |
154 | distanceTraveled = distanceTraveled + 1 | |
155 | end | |
156 | - | waitForTree() |
156 | + | |
157 | - | chop() |
157 | + | |
158 | - | if not plant() then |
158 | + | turtle.dig() --just in case |
159 | forward(1) | |
160 | left() | |
161 | ||
162 | --just in case | |
163 | while turtle.detectUp() do | |
164 | - | function dumpWood() |
164 | + | |
165 | turtle.dig() | |
166 | - | if (not i==SAPLING_SLOT) and (not i==FUEL_SLOT) then |
166 | + | |
167 | if turtle.up() then | |
168 | - | if not turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then |
168 | + | |
169 | distanceTraveled = distanceTraveled + 1 | |
170 | end | |
171 | end | |
172 | ||
173 | for i=1,height do | |
174 | turtle.select(WOOD_SLOT) | |
175 | - | function step() |
175 | + | |
176 | turtle.digDown() | |
177 | - | forward(2) |
177 | + | |
178 | - | if posX <= 0 then |
178 | + | |
179 | - | right() |
179 | + | |
180 | - | right() |
180 | + | turtle.suck() |
181 | distanceTraveled = distanceTraveled + 1 | |
182 | - | cycleCount = cycleCount + 1 |
182 | + | |
183 | - | print("cycles finished: "..cycleCount) |
183 | + | turtle.select(WOOD_SLOT) |
184 | - | print("saplings planted: "..saplingsPlanted) |
184 | + | turtle.dig() |
185 | - | print("trees chopped: "..treesChopped) |
185 | + | -- replant while repositioning |
186 | - | print("distance traveled: "..distanceTraveled) |
186 | + | print("replanting saplings...") |
187 | - | os.sleep(10) |
187 | + | |
188 | - | elseif posX >= farmLength then |
188 | + | turtle.suck() |
189 | - | right() |
189 | + | |
190 | - | right() |
190 | + | |
191 | right() | |
192 | back(1) | |
193 | - | transferSaplings() |
193 | + | |
194 | - | os.sleep(10) |
194 | + | turtle.suck() |
195 | - | elseif (posX % 4) == orient then |
195 | + | |
196 | - | right() |
196 | + | |
197 | - | result = handleTree() |
197 | + | left() |
198 | - | left() |
198 | + | |
199 | - | elseif (posX % 4) == (2-orient) then |
199 | + | turtle.suck() |
200 | - | left() |
200 | + | |
201 | - | result = handleTree() |
201 | + | |
202 | - | right() |
202 | + | back(1) |
203 | turtle.select(SAPLING_TMP_SLOT) | |
204 | turtle.suck() | |
205 | turtle.select(SAPLING_SLOT) | |
206 | turtle.place() | |
207 | end | |
208 | - | if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then |
208 | + | |
209 | function checkTreeIsReallyThere() | |
210 | local result = false | |
211 | if turtle.up() then | |
212 | - | if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then |
212 | + | |
213 | turtle.select(SAPLING_SLOT) | |
214 | os.sleep(2) | |
215 | if turtle.detect() and (turtle.getItemCount(SAPLING_SLOT)>0) and (not turtle.compare()) then | |
216 | result = true | |
217 | end | |
218 | - | if farmLength==nil or farmLength<2 then |
218 | + | |
219 | - | print("Farm length too short.\n") |
219 | + | distanceTraveled = distanceTraveled + 2 |
220 | - | return |
220 | + | else -- couldn't move up, assume there are tree leaves |
221 | result = true | |
222 | - | if turtle.getItemCount(SAPLING_SLOT) < farmLength then |
222 | + | |
223 | - | print("Not enough saplings in slot "..SAPLING_SLOT.."\n") |
223 | + | |
224 | - | return |
224 | + | |
225 | ||
226 | function waitForTree() -- needs at least 1 sapling | |
227 | print("waiting for tree...") | |
228 | - | if not checkFuel() then |
228 | + | local result = false |
229 | - | print("Not enough fuel\n") |
229 | + | |
230 | - | os.sleep(10) |
230 | + | os.sleep(TREE_GROW_SLEEP_TIME) |
231 | - | while not checkFuel() do |
231 | + | while treeDetectedCount < REPEATED_DETECTION_COUNT do -- because sometimes the turtle does not compare reliably |
232 | turtle.select(SAPLING_SLOT) | |
233 | os.sleep(1) | |
234 | if turtle.compare() then | |
235 | if treeDetectedCount>0 then | |
236 | - | if not step() then |
236 | + | print("correcting invalid tree detection/comparison ("..treeDetectedCount..")") |
237 | - | print("Step unsuccessfull, exiting.\n") |
237 | + | |
238 | treeDetectedCount = 0 | |
239 | else | |
240 | treeDetectedCount = treeDetectedCount + 1 | |
241 | left() | |
242 | os.sleep(REPEATED_DETECTION_TICK) | |
243 | right() | |
244 | end | |
245 | os.sleep(NEXT_DETECTION_TICK) | |
246 | end | |
247 | os.sleep(1) | |
248 | if treeDetectedCount < REPEATED_DETECTION_COUNT then | |
249 | print("correct tree detection failed") | |
250 | else | |
251 | if checkTreeIsReallyThere() then | |
252 | print("tree detected") | |
253 | result = true | |
254 | else | |
255 | print("tree detected, but not found") | |
256 | end | |
257 | end | |
258 | return result | |
259 | end | |
260 | function dumpWood() -- dump everything that is not a sapling | |
261 | while back(1) and (posX>(0-MAX_WOOD_STORAGE_DISTANCE)) do | |
262 | end | |
263 | ||
264 | for i = 1,16 do | |
265 | if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then | |
266 | turtle.select(i) | |
267 | if not turtle.compareTo(SAPLING_SLOT) then | |
268 | turtle.dropDown() | |
269 | if turtle.getItemCount(i)>0 then | |
270 | print("cannot dump wood, storage probably full...") | |
271 | while turtle.getItemCount(i)>0 do | |
272 | turtle.dropDown() | |
273 | os.sleep(10) | |
274 | end | |
275 | end | |
276 | end | |
277 | end | |
278 | end | |
279 | ||
280 | forward(0-posX) | |
281 | end | |
282 | function checkWood() | |
283 | print("checking wood amount...") | |
284 | local woodAmount = 0 | |
285 | for i = 1,16 do | |
286 | if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then | |
287 | turtle.select(i) | |
288 | if not turtle.compareTo(SAPLING_SLOT) then | |
289 | woodAmount = woodAmount + turtle.getItemCount(i) | |
290 | end | |
291 | end | |
292 | end | |
293 | if woodAmount > MAX_WOOD then | |
294 | print("going to dump "..woodAmount.." of wood...") | |
295 | dumpWood() | |
296 | else | |
297 | print("holding "..woodAmount.." of wood") | |
298 | end | |
299 | end | |
300 | function checkFuel() | |
301 | local result = true | |
302 | if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then | |
303 | turtle.select(FUEL_SLOT) | |
304 | turtle.refuel() | |
305 | end | |
306 | if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then | |
307 | result = false | |
308 | end | |
309 | return result | |
310 | end | |
311 | function cycle() | |
312 | local result = true | |
313 | ||
314 | checkSaplings() | |
315 | checkFuel() | |
316 | ||
317 | while not waitForTree() do | |
318 | os.sleep(1) | |
319 | end | |
320 | chop() | |
321 | ||
322 | checkWood() | |
323 | ||
324 | cycleCount = cycleCount + 1 | |
325 | print("cycles finished: "..cycleCount) | |
326 | print("distance traveled: "..distanceTraveled) | |
327 | print("waiting between cycles...") | |
328 | os.sleep(2) | |
329 | ||
330 | return result | |
331 | end | |
332 | ||
333 | if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then | |
334 | print("Not enough saplings in slot "..SAPLING_SLOT..", exiting.\n") | |
335 | return false | |
336 | end | |
337 | ||
338 | while true do | |
339 | if not cycle() then | |
340 | print("Cycle unsuccessfull, exiting.\n") | |
341 | return false | |
342 | end | |
343 | end |