-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreechopper.lua
More file actions
1139 lines (1006 loc) · 37.4 KB
/
treechopper.lua
File metadata and controls
1139 lines (1006 loc) · 37.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Tree Chopper v1.0
A turtle script to traverse N chunks and chop down all trees encountered
*** IMPORTANT DISCLAIMER ***
This script was written by Claude 4 Sonnet AI and may contain bugs,
unexpected behavior, or other issues. Use at your own risk and always
test thoroughly before deploying on important worlds or systems.
The AI cannot guarantee perfect functionality or compatibility with
all Minecraft/CC:Tweaked versions and configurations.
Usage: treechopper -chunks <N> [-replant] [-chest <side>] [-restore]
Features:
- Traverses N chunks in a systematic pattern
- Detects and chops down trees automatically
- Optional sapling replanting
- Returns to base for inventory management
- Session persistence
]]
-- Configuration defaults
local chunks = 1
local replant = false
local chestSide = "bottom"
local saveFile = "treechopper_save"
local doBackup = true
local minFuelLevel = 1000 -- Minimum fuel to start
-- Position and state tracking
local xPos, yPos, zPos = 0, 0, 0
local facing = 0 -- 0=North(+Z), 1=East(+X), 2=South(-Z), 3=West(-X)
local homeX, homeY, homeZ, homeFacing = 0, 0, 0, 0
local currentChunk = 1
local blocksChopped = 0
local saplingSlot = 16 -- Reserve slot 16 for saplings
-- Wood types to detect (CC:Tweaked block names)
local woodTypes = {
"minecraft:oak_log",
"minecraft:birch_log",
"minecraft:spruce_log",
"minecraft:jungle_log",
"minecraft:acacia_log",
"minecraft:dark_oak_log",
"minecraft:mangrove_log",
"minecraft:cherry_log"
}
-- Sapling types corresponding to wood types
local saplingTypes = {
"minecraft:oak_sapling",
"minecraft:birch_sapling",
"minecraft:spruce_sapling",
"minecraft:jungle_sapling",
"minecraft:acacia_sapling",
"minecraft:dark_oak_sapling",
"minecraft:mangrove_propagule",
"minecraft:cherry_sapling"
}
-- Fuel management
local function checkFuel()
local fuel = turtle.getFuelLevel()
if fuel == "unlimited" then
return true
end
return fuel >= minFuelLevel
end
local function refuelFromInventory()
local originalSlot = turtle.getSelectedSlot()
for slot = 1, 16 do
turtle.select(slot)
if turtle.refuel(0) then -- Check if item can be used as fuel
local count = turtle.getItemCount(slot)
if count > 0 then
print("Refueling with " .. count .. " items from slot " .. slot)
turtle.refuel(count)
break
end
end
end
turtle.select(originalSlot)
end
local function ensureFuel()
if turtle.getFuelLevel() == "unlimited" then
return true
end
local fuel = turtle.getFuelLevel()
print("Current fuel level: " .. fuel)
if fuel < minFuelLevel then
print("Low fuel! Attempting to refuel from inventory...")
refuelFromInventory()
fuel = turtle.getFuelLevel()
if fuel < minFuelLevel then
print("ERROR: Not enough fuel! Need at least " .. minFuelLevel)
print("Current fuel: " .. fuel)
print("Please add fuel items to inventory and restart.")
return false
end
end
print("Fuel OK: " .. fuel .. " units")
return true
end
-- Utility functions
local function isWood(blockData)
if not blockData then return false end
for _, woodType in ipairs(woodTypes) do
if blockData.name == woodType then
return true
end
end
return false
end
local function isSapling(itemName)
for _, saplingType in ipairs(saplingTypes) do
if itemName == saplingType then
return true
end
end
return false
end
local function isLeaves(blockData)
if not blockData then return false end
local leafTypes = {
"minecraft:oak_leaves",
"minecraft:birch_leaves",
"minecraft:spruce_leaves",
"minecraft:jungle_leaves",
"minecraft:acacia_leaves",
"minecraft:dark_oak_leaves",
"minecraft:mangrove_leaves",
"minecraft:cherry_leaves"
}
for _, leafType in ipairs(leafTypes) do
if blockData.name == leafType then
return true
end
end
return false
end
local function saveProgress()
if not doBackup then return end
local file = fs.open(saveFile, "w")
if not file then
print("Warning: Could not save progress to file")
return
end
file.write("xPos = " .. xPos .. "\n")
file.write("yPos = " .. yPos .. "\n")
file.write("zPos = " .. zPos .. "\n")
file.write("facing = " .. facing .. "\n")
file.write("currentChunk = " .. currentChunk .. "\n")
file.write("blocksChopped = " .. blocksChopped .. "\n")
file.close()
end
local function turnRight()
turtle.turnRight()
facing = (facing + 1) % 4
saveProgress()
end
local function turnLeft()
turtle.turnLeft()
facing = (facing - 1) % 4
saveProgress()
end
local function turnTo(targetFacing)
while facing ~= targetFacing do
if (targetFacing - facing) % 4 == 1 then
turnRight()
else
turnLeft()
end
end
end
-- Terrain-aware movement functions
local function isGround(blockData)
if not blockData then return false end
local groundBlocks = {
"minecraft:dirt", "minecraft:grass_block", "minecraft:stone",
"minecraft:cobblestone", "minecraft:sand", "minecraft:gravel",
"minecraft:podzol", "minecraft:coarse_dirt", "minecraft:mycelium",
"minecraft:snow_block", "minecraft:clay", "minecraft:terracotta"
}
for _, groundType in ipairs(groundBlocks) do
if blockData.name == groundType then
return true
end
end
return false
end
local function findGroundLevel()
-- Look down to find ground level, but don't be too aggressive
local groundY = yPos
local maxDown = 2 -- Further reduced - only go down 2 blocks max
-- First check if we're already at a good level
local success, blockData = turtle.inspectDown()
if success and isGround(blockData) then
-- We're already at ground level
return groundY
end
-- Only go down if there's air below us, and be very conservative
for i = 1, maxDown do
local success, blockData = turtle.inspectDown()
if success then
-- There's a block below, stop here
break
else
-- Air below, go down to check
if turtle.down() then
yPos = yPos - 1
groundY = yPos
saveProgress()
-- Check if we found ground now
local newSuccess, newBlockData = turtle.inspectDown()
if newSuccess and isGround(newBlockData) then
print("Found ground at level " .. groundY)
return groundY
elseif not newSuccess then
-- Still air below after going down - this is likely a cave/hole
print("Detected cave/hole, climbing back up to avoid going too deep")
turtle.up()
yPos = yPos + 1
saveProgress()
return yPos
end
else
break
end
end
end
return groundY
end
local function chopTree(treeInfo)
print("Chopping tree at " .. treeInfo.x .. "," .. treeInfo.y .. "," .. treeInfo.z)
-- Store our current position before tree chopping
local originalX, originalY, originalZ = xPos, yPos, zPos
local originalFacing = facing
-- Face the tree direction
turnTo(treeInfo.direction)
-- Verify there's still wood in front of us
local success, blockData = turtle.inspect()
if success and isWood(blockData) then
print("Confirmed wood block ahead, starting to chop...")
-- Dig the base block
turtle.dig()
blocksChopped = blocksChopped + 1
-- Move into the tree position to chop upward
turtle.forward()
if treeInfo.direction == 0 then zPos = zPos + 1
elseif treeInfo.direction == 1 then xPos = xPos + 1
elseif treeInfo.direction == 2 then zPos = zPos - 1
elseif treeInfo.direction == 3 then xPos = xPos - 1
end
saveProgress()
local treeBase = yPos
-- Check if we need to go down to find the real base
while true do
local downSuccess, downData = turtle.inspectDown()
if downSuccess and isWood(downData) then
turtle.digDown()
blocksChopped = blocksChopped + 1
turtle.down()
yPos = yPos - 1
treeBase = yPos
else
break
end
end
-- Now chop upward
local woodChopped = 1 -- Already chopped the base
local maxHeight = 30
print("Starting upward chop from base level " .. treeBase)
for height = 0, maxHeight do
local upSuccess, upData = turtle.inspectUp()
if upSuccess and isWood(upData) then
print("Found wood above at height " .. (height + 1) .. ", chopping...")
turtle.digUp()
woodChopped = woodChopped + 1
blocksChopped = blocksChopped + 1
-- Simple up movement
turtle.up()
yPos = yPos + 1
else
if upSuccess then
print("Non-wood block above: " .. (upData.name or "unknown"))
else
print("No block above, reached top of tree")
end
break
end
end
print("Finished chopping upward, total wood: " .. woodChopped)
-- Save progress after tree chopping is complete
saveProgress()
-- Return to the exact original position using simple movement
print("Returning to original position: " .. originalX .. "," .. originalY .. "," .. originalZ)
-- Move to target Y first
while yPos > originalY do
turtle.down()
yPos = yPos - 1
end
while yPos < originalY do
turtle.up()
yPos = yPos + 1
end
-- Move to target X
while xPos ~= originalX do
if xPos < originalX then
turnTo(1) -- Face East
else
turnTo(3) -- Face West
end
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
else
turtle.dig()
turtle.forward()
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
end
end
-- Move to target Z
while zPos ~= originalZ do
if zPos < originalZ then
turnTo(0) -- Face North
else
turnTo(2) -- Face South
end
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
else
turtle.dig()
turtle.forward()
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
end
end
turnTo(originalFacing)
saveProgress()
print("Successfully returned to original position and facing")
-- Plant sapling if replanting is enabled
if replant then
turtle.select(saplingSlot)
if turtle.getItemCount(saplingSlot) > 0 then
turnTo(treeInfo.direction)
turtle.place()
print("Planted sapling")
turnTo(originalFacing)
else
print("No saplings available for replanting")
end
end
print("Chopped " .. woodChopped .. " wood blocks")
return woodChopped
else
print("No wood found at expected location - tree may have been removed")
return 0
end
end
local function smartForward()
print("Smart moving forward from " .. xPos .. "," .. zPos .. " facing " .. facing)
-- First, try normal forward movement
if turtle.forward() then
-- Update position
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved to " .. xPos .. "," .. zPos)
saveProgress()
-- Check if we need to adjust height for terrain following
local success, blockData = turtle.inspectDown()
if not success then
-- Air below - we might have walked off a cliff or into a hole
print("Air detected below, checking for safe ground level...")
-- Try going down one block to see if there's ground
if turtle.down() then
yPos = yPos - 1
saveProgress()
local newSuccess, newBlockData = turtle.inspectDown()
if newSuccess and isGround(newBlockData) then
-- Found ground one block down, this is fine
print("Found ground one level down")
return true
else
-- Still air below or non-ground block - this might be a cave/hole
print("Detected deep hole or cave, returning to surface level")
turtle.up()
yPos = yPos + 1
saveProgress()
return true
end
end
end
return true
end
-- Can't move forward, check what's blocking
local success, blockData = turtle.inspect()
if success then
if isWood(blockData) then
print("Wood block ahead - chopping entire tree now!")
-- We found a tree during navigation - chop it completely
local treeInfo = {
x = xPos + (facing == 1 and 1 or (facing == 3 and -1 or 0)),
z = zPos + (facing == 0 and 1 or (facing == 2 and -1 or 0)),
y = yPos,
direction = facing
}
chopTree(treeInfo)
-- After chopping, try to move forward again
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved forward after chopping tree")
saveProgress()
return true
end
elseif isGround(blockData) then
print("Ground/hill ahead - attempting to climb intelligently")
-- Try to climb over the obstacle, but be smarter about it
local climbHeight = 0
local maxClimbHeight = 3 -- Maximum height to climb
local canClimb = true
-- Check how high we need to climb
while climbHeight < maxClimbHeight and canClimb do
local upSuccess, upData = turtle.inspectUp()
if upSuccess then
if isWood(upData) then
print("Tree detected above at height +" .. (climbHeight + 1) .. " - clearing path only")
-- Just clear the path, don't count as chopped wood since we're not doing full tree processing
turtle.digUp()
-- Don't increment blocksChopped here - this is just path clearing
elseif isGround(upData) or isLeaves(upData) then
print("Ground/leaves above at height +" .. (climbHeight + 1) .. " - can dig through")
turtle.digUp()
else
print("Unknown block above: " .. (upData.name or "unknown") .. " - digging")
turtle.digUp()
end
end
-- Try to move up
if turtle.up() then
yPos = yPos + 1
climbHeight = climbHeight + 1
saveProgress()
print("Climbed to height +" .. climbHeight)
-- Check if we can now move forward
local forwardSuccess, forwardData = turtle.inspect()
if not forwardSuccess then
-- Clear path ahead, try to move forward
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Successfully climbed over obstacle to " .. xPos .. "," .. zPos)
saveProgress()
return true
end
elseif forwardSuccess and isWood(forwardData) then
print("Found wood at this height - chopping tree!")
-- Calculate tree position
local treeX = xPos + (facing == 1 and 1 or (facing == 3 and -1 or 0))
local treeZ = zPos + (facing == 0 and 1 or (facing == 2 and -1 or 0))
local treeInfo = {x = treeX, z = treeZ, y = yPos, direction = facing}
chopTree(treeInfo)
-- Try to move forward after chopping
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved forward after chopping tree at height +" .. climbHeight)
saveProgress()
return true
end
elseif forwardSuccess and not isGround(forwardData) then
print("Non-ground obstacle at height +" .. climbHeight .. " - digging")
turtle.dig()
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved forward after clearing obstacle at height +" .. climbHeight)
saveProgress()
return true
end
end
-- Still blocked, continue climbing
else
print("Can't climb higher - obstacle above")
canClimb = false
end
end
-- If we climbed but still can't move forward, we're probably at max height
if climbHeight > 0 then
print("Reached maximum climb height (" .. climbHeight .. "), trying to move forward anyway")
-- Try to dig through whatever is in front
turtle.dig()
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved forward after digging at elevated height")
saveProgress()
return true
end
else
-- Couldn't climb at all, fall back to digging through (last resort)
print("Cannot climb over obstacle, digging through as last resort")
turtle.dig()
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved forward after digging through obstacle")
saveProgress()
return true
end
end
elseif isLeaves(blockData) then
print("Leaves ahead - trying to move through")
turtle.dig() -- Leaves do block movement, so we need to dig them
else
print("Unknown obstacle ahead (" .. (blockData.name or "unknown") .. ") - digging...")
turtle.dig()
end
-- Try moving again after clearing obstacle
if turtle.forward() then
if facing == 0 then zPos = zPos + 1
elseif facing == 1 then xPos = xPos + 1
elseif facing == 2 then zPos = zPos - 1
elseif facing == 3 then xPos = xPos - 1
end
print("Moved to " .. xPos .. "," .. zPos .. " after clearing obstacle")
saveProgress()
return true
end
else
-- No block detected but can't move - probably an entity
print("Entity blocking path, attacking...")
turtle.attack()
sleep(0.1)
return smartForward() -- Retry
end
return false
end
local function smartUp()
print("Moving up from y=" .. yPos)
while not turtle.up() do
if turtle.detectUp() then
print("Obstacle above, digging...")
turtle.digUp()
else
print("Attacking entity above...")
turtle.attackUp()
end
sleep(0.1)
end
yPos = yPos + 1
print("Moved to y=" .. yPos)
saveProgress()
end
local function smartDown()
print("Moving down from y=" .. yPos)
while not turtle.down() do
if turtle.detectDown() then
print("Obstacle below, digging...")
turtle.digDown()
else
print("Attacking entity below...")
turtle.attackDown()
end
sleep(0.1)
end
yPos = yPos - 1
print("Moved to y=" .. yPos)
saveProgress()
end
local function goTo(targetX, targetZ, targetY)
print("Going to " .. targetX .. "," .. targetZ .. "," .. (targetY or "ground level"))
-- If no target Y specified, we'll follow terrain
local followTerrain = (targetY == nil)
-- Move to target X
while xPos ~= targetX do
if xPos < targetX then
turnTo(1) -- Face East
else
turnTo(3) -- Face West
end
smartForward()
-- Check fuel periodically during long moves
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 100 then
print("Low fuel during movement, attempting to refuel...")
refuelFromInventory()
end
end
-- Move to target Z
while zPos ~= targetZ do
if zPos < targetZ then
turnTo(0) -- Face North
else
turnTo(2) -- Face South
end
smartForward()
-- Check fuel periodically during long moves
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 100 then
print("Low fuel during movement, attempting to refuel...")
refuelFromInventory()
end
end
-- Move to target Y if specified, otherwise stay at ground level
if not followTerrain and targetY then
while yPos < targetY do smartUp() end
while yPos > targetY do smartDown() end
else
-- Make sure we're at ground level
findGroundLevel()
end
print("Arrived at " .. xPos .. "," .. yPos .. "," .. zPos)
end
local function returnHome()
print("Returning to base...")
goTo(homeX, homeZ, homeY)
turnTo(homeFacing)
end
local function dropItems()
print("Dropping off items...")
returnHome()
-- Drop everything except saplings
for slot = 1, 15 do
turtle.select(slot)
local item = turtle.getItemDetail()
if item and not isSapling(item.name) then
local success = false
if chestSide == "top" then
success = turtle.dropUp()
elseif chestSide == "bottom" then
success = turtle.dropDown()
elseif chestSide == "front" then
success = turtle.drop()
elseif chestSide == "left" then
turnTo((homeFacing + 3) % 4) -- Turn left
success = turtle.drop()
turnTo(homeFacing) -- Turn back
elseif chestSide == "right" then
turnTo((homeFacing + 1) % 4) -- Turn right
success = turtle.drop()
turnTo(homeFacing) -- Turn back
end
if not success then
print("Warning: Could not drop items from slot " .. slot)
end
end
end
-- Consolidate saplings to slot 16
turtle.select(saplingSlot)
for slot = 1, 15 do
turtle.select(slot)
local item = turtle.getItemDetail()
if item and isSapling(item.name) then
turtle.transferTo(saplingSlot)
end
end
turtle.select(1)
end
local function needsDropOff()
local freeSlots = 0
for slot = 1, 15 do -- Don't count sapling slot
if turtle.getItemCount(slot) == 0 then
freeSlots = freeSlots + 1
end
end
return freeSlots < 3 -- Drop off when less than 3 free slots
end
local function quickScanForWood()
-- Quick scan in current direction at multiple Y levels
-- Check current level
local success, blockData = turtle.inspect()
if success and isWood(blockData) then
return true
end
-- Check below (tree base might be lower)
local downSuccess, downData = turtle.inspectDown()
if downSuccess and isWood(downData) then
return true
end
-- Check above (tree might extend upward)
local upSuccess, upData = turtle.inspectUp()
if upSuccess and isWood(upData) then
return true
end
return false
end
local function fullScanForTrees()
-- Full 360 scan at multiple Y levels - but don't move the turtle!
local treesFound = {}
local originalFacing = facing
print("Doing full tree scan at multiple levels...")
-- Check in all 4 directions at current level, above, and below
for dir = 0, 3 do
turnTo(dir)
-- Check current level
local success, blockData = turtle.inspect()
if success and isWood(blockData) then
local treeX, treeZ = xPos, zPos
if dir == 0 then treeZ = treeZ + 1
elseif dir == 1 then treeX = treeX + 1
elseif dir == 2 then treeZ = treeZ - 1
elseif dir == 3 then treeX = treeX - 1
end
-- Check if we already found this tree
local alreadyFound = false
for _, existingTree in ipairs(treesFound) do
if existingTree.x == treeX and existingTree.z == treeZ then
alreadyFound = true
break
end
end
if not alreadyFound then
table.insert(treesFound, {x = treeX, z = treeZ, y = yPos, direction = dir})
print("Found tree at " .. treeX .. "," .. yPos .. "," .. treeZ .. " (current level)")
end
end
end
-- Return to original facing
turnTo(originalFacing)
return treesFound
end
local function processPosition(x, z)
print("Processing position " .. x .. "," .. z)
-- Move to the position
goTo(x, z)
-- Multi-level tree detection - check current level and nearby levels
local foundWood = false
local woodLevels = {}
-- Check current level first
if quickScanForWood() then
foundWood = true
table.insert(woodLevels, yPos)
print("Wood detected at current level " .. yPos)
end
-- Check one level down
local downSuccess, downData = turtle.inspectDown()
if downSuccess and isWood(downData) then
foundWood = true
table.insert(woodLevels, yPos - 1)
print("Wood detected one level down at " .. (yPos - 1))
end
-- Check one level up
local upSuccess, upData = turtle.inspectUp()
if upSuccess and isWood(upData) then
foundWood = true
table.insert(woodLevels, yPos + 1)
print("Wood detected one level up at " .. (yPos + 1))
end
if foundWood then
print("Wood detected at multiple levels, processing trees...")
-- Process trees at each detected level
for _, woodY in ipairs(woodLevels) do
-- Move to the level where wood was detected
if woodY < yPos then
print("Moving down to tree level " .. woodY)
smartDown()
elseif woodY > yPos then
print("Moving up to tree level " .. woodY)
smartUp()
end
-- Now do full scan at this level
local trees = fullScanForTrees()
-- Chop any trees found at this level
for _, tree in ipairs(trees) do
if needsDropOff() then
dropItems()
goTo(x, z) -- Return to current position
-- Re-adjust to the correct level
while yPos ~= woodY do
if yPos < woodY then smartUp()
else smartDown() end
end
end
chopTree(tree)
end
end
-- Return to ground level after processing all trees
findGroundLevel()
else
-- No wood detected at any level, skip
print("No wood detected at any level, skipping scan")
end
-- Check if we need to drop off items
if needsDropOff() then
dropItems()
end
end
local function getChunkCoords(chunkNumber)
-- Convert chunk number to spiral coordinates
if chunkNumber == 1 then
return 0, 0
end
-- Simple linear pattern for now - can be made spiral later
local chunkX = (chunkNumber - 1) % 4
local chunkZ = math.floor((chunkNumber - 1) / 4)
return chunkX, chunkZ
end
local function traverseChunk(chunkNumber)
local chunkX, chunkZ = getChunkCoords(chunkNumber)
print("Traversing chunk " .. chunkNumber .. " at chunk coords " .. chunkX .. "," .. chunkZ)
local startX = chunkX * 16
local startZ = chunkZ * 16
-- Traverse the 16x16 chunk in a snake pattern
for z = 0, 15 do
local actualZ = startZ + z
if z % 2 == 0 then
-- Left to right
for x = 0, 15 do
local actualX = startX + x
processPosition(actualX, actualZ)
if needsDropOff() then
dropItems()
processPosition(actualX, actualZ) -- Return to position
end
end
else
-- Right to left
for x = 15, 0, -1 do
local actualX = startX + x
processPosition(actualX, actualZ)
if needsDropOff() then
dropItems()
processPosition(actualX, actualZ) -- Return to position
end
end
end
end
end
local function loadProgress()
if not fs.exists(saveFile) then
return false
end
local file = fs.open(saveFile, "r")
if not file then
return false
end
local content = file.readAll()
file.close()
-- Parse the saved values
for line in content:gmatch("[^\r\n]+") do
local var, value = line:match("(%w+) = (%d+)")
if var and value then
if var == "xPos" then xPos = tonumber(value)
elseif var == "yPos" then yPos = tonumber(value)
elseif var == "zPos" then zPos = tonumber(value)
elseif var == "facing" then facing = tonumber(value)
elseif var == "currentChunk" then currentChunk = tonumber(value)
elseif var == "blocksChopped" then blocksChopped = tonumber(value)
end
end
end
return true
end
-- Argument parsing
local args = {...}
local function parseArgs()
local i = 1