-
Notifications
You must be signed in to change notification settings - Fork 3
/
advancedminingturtle.cc.lua
1734 lines (1501 loc) · 61.3 KB
/
advancedminingturtle.cc.lua
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
-- ********************************************************************************** --
-- ** ** --
-- ** Minecraft Mining Turtle Ore Quarry v0.7 by AustinKK ** --
-- ** --------------------------------------------------- ** --
-- ** ** --
-- ** For instructions on how to use: ** --
-- ** ** --
-- ** http://www.youtube.com/watch?v=PIugLVzUz3g ** --
-- ** ** --
-- ** Change Log: ** --
-- ** 27th Dec 2012: [v0.2] Initial Draft Release ** --
-- ** 29th Dec 2012: [v0.3] Minor Performance Improvements ** --
-- ** 30th Dec 2012: [v0.4] Further Performance Improvements ** --
-- ** 9th Jan 2013: [v0.5] Debug Version (dropping off chest) ** --
-- ** 10th Jan 2013: [v0.51] Further Debug (dropping off chest) ** --
-- ** 10th Jan 2013: [v0.52] Fix for dropping off chest bug ** --
-- ** 11th Jan 2013: [v0.53] Fix for dropping off chest bug (release) ** --
-- ** 12th Jan 2013: [v0.6] Added support for resume ** --
-- ** 31st Mar 2013: [v0.7] Fixes for ComputerCraft v1.52 ** --
-- ** ** --
-- ********************************************************************************** --
-- Enumeration to store the the different types of message that can be written
messageLevel = { DEBUG=0, INFO=1, WARNING=2, ERROR=3, FATAL=4 }
-- Enumeration to store names for the 6 directions
direction = { FORWARD=0, RIGHT=1, BACK=2, LEFT=3, UP=4, DOWN=5 }
-- Enumeration of mining states
miningState = { START=0, LAYER=1, EMPTYCHESTDOWN=2, EMPTYINVENTORY=3 }
local messageOutputLevel = messageLevel.INFO
local messageOutputFileName
local fuelLevelToRefuelAt = 5
local refuelItemsToUseWhenRefuelling = 63
local emergencyFuelToRetain = 0
local maximumGravelStackSupported = 25 -- The number of stacked gravel or sand blocks supported
local noiseBlocksCount
local bottomLayer = 5 -- The y co-ords of the layer immediately above bedrock
local returningToStart = false
local lookForChests = false -- Determines if chests should be located as part of the quarrying
local miningOffset -- The offset to the mining layer. This is set depending on whether chests are being looked for or not
local lastEmptySlot -- The last inventory slot that was empty when the program started (is either 15 if not looking for chests or 14 if we are)
local turtleId
local isWirelessTurtle
local currentlySelectedSlot = 0 -- The slot that the last noise block was found in
local lastMoveNeededDig = true -- Determines whether the last move needed a dig first
local haveBeenAtZeroZeroOnLayer -- Determines whether the turtle has been at (0, 0) in this mining layer
local orientationAtZeroZero -- The turtle's orientation when it was at (0, 0)
local levelToReturnTo -- The level that the turtle should return to in order to head back to the start to unload
-- Variables used to support a resume
local startupParamsFile = "OreQuarryParams.txt"
local oreQuarryLocation = "OreQuarryLocation.txt"
local returnToStartFile = "OreQuarryReturn.txt"
local startupBackup = "startup_bak"
local supportResume = true -- Determines whether the turtle is being run in the mode that supports resume
local resuming = false -- Determines whether the turtle is currently in the process of resuming
local resumeX
local resumeY
local resumeZ
local resumeOrient
local resumeMiningState
-- Variables to store the current location and orientation of the turtle. x is right, left, y is up, down and
-- z is forward, back with relation to the starting orientation. Y is the actual turtle level, x and z are
-- in relation to the starting point (i.e. the starting point is (0, 0))
local currX
local currY
local currZ
local currOrient
local currMiningState = miningState.START
-- Command line parameters
local startHeight -- Represents the height (y co-ord) that the turtle started at
local quarryWidth -- Represents the length of the mines that the turtle will dig
-- ********************************************************************************** --
-- Writes an output message
-- ********************************************************************************** --
function writeMessage(message, msgLevel)
if (msgLevel >= messageOutputLevel) then
print(message)
-- If this turtle has a modem, then write the message to red net
if (isWirelessTurtle == true) then
if (turtleId == nil) then
rednet.broadcast(message)
else
-- Broadcast the message (prefixed with the turtle's id)
rednet.broadcast("[".. turtleId.."] "..message)
end
end
if (messageOutputFileName ~= nil) then
-- Open file, write message and close file (flush doesn't seem to work!)
local outputFile = io.open(messageOutputFileName, "a")
outputFile:write(message)
outputFile:write("\n")
outputFile:close()
end
end
end
-- ********************************************************************************** --
-- Ensures that the turtle has fuel
-- ********************************************************************************** --
function ensureFuel()
-- Determine whether a refuel is required
local fuelLevel = turtle.getFuelLevel()
if (fuelLevel ~= "unlimited") then
if (fuelLevel < fuelLevelToRefuelAt) then
-- Need to refuel
turtle.select(16)
currentlySelectedSlot = 16
local fuelItems = turtle.getItemCount(16)
-- Do we need to impact the emergency fuel to continue? (always
-- keep one fuel item in slot 16)
if (fuelItems == 0) then
writeMessage("Completely out of fuel!", messageLevel.FATAL)
elseif (fuelItems == 1) then
writeMessage("Out of Fuel!", messageLevel.ERROR)
turtle.refuel()
elseif (fuelItems <= (emergencyFuelToRetain + 1)) then
writeMessage("Consuming emergency fuel supply. "..(fuelItems - 2).." emergency fuel items remain", messageLevel.WARNING)
turtle.refuel(1)
else
-- Refuel the lesser of the refuelItemsToUseWhenRefuelling and the number of items more than
-- the emergency fuel level
if (fuelItems - (emergencyFuelToRetain + 1) < refuelItemsToUseWhenRefuelling) then
turtle.refuel(fuelItems - (emergencyFuelToRetain + 1))
else
turtle.refuel(refuelItemsToUseWhenRefuelling)
end
end
end
end
end
-- ********************************************************************************** --
-- Checks that the turtle has inventory space by checking for spare slots and returning
-- to the starting point to empty out if it doesn't.
--
-- Takes the position required to move to in order to empty the turtle's inventory
-- should it be full as arguments
-- ********************************************************************************** --
function ensureInventorySpace()
-- If already returning to start, then don't need to do anything
if (returningToStart == false) then
-- If the last inventory slot is full, then need to return to the start and empty
if (turtle.getItemCount(lastEmptySlot) > 0) then
-- Return to the starting point and empty the inventory, then go back to mining
returnToStartAndUnload(true)
end
end
end
-- ********************************************************************************** --
-- Function to move to the starting point, call a function that is passed in
-- and return to the same location (if required)
-- ********************************************************************************** --
function returnToStartAndUnload(returnBackToMiningPoint)
writeMessage("returnToStartAndUnload called", messageLevel.DEBUG)
returningToStart = true
local storedX, storedY, storedZ, storedOrient
local prevMiningState = currMiningState
if (resuming == true) then
-- Get the stored parameters from the necessary file
local resumeFile = fs.open(returnToStartFile, "r")
if (resumeFile ~= nil) then
-- Restore the parameters from the file
local beenAtZero = resumeFile.readLine()
if (beenAtZero == "y") then
haveBeenAtZeroZeroOnLayer = true
else
haveBeenAtZeroZeroOnLayer = false
end
local miningPointFlag = resumeFile.readLine()
if (miningPointFlag == "y") then
returnBackToMiningPoint = true
else
returnBackToMiningPoint = false
end
currX = readNumber(resumeFile)
currY = readNumber(resumeFile)
currZ = readNumber(resumeFile)
currOrient = readNumber(resumeFile)
levelToReturnTo = readNumber(resumeFile)
prevMiningState = readNumber(resumeFile)
orientationAtZeroZero = readNumber(resumeFile)
resumeFile.close()
else
writeMessage("Failed to read return to start file", messageLevel.ERROR)
end
elseif (supportResume == true) then
local outputFile = io.open(returnToStartFile, "w")
if (haveBeenAtZeroZeroOnLayer == true) then
outputFile:write("y\n")
else
outputFile:write("n\n")
end
if (returnBackToMiningPoint == true) then
outputFile:write("y\n")
else
outputFile:write("n\n")
end
outputFile:write(currX)
outputFile:write("\n")
outputFile:write(currY)
outputFile:write("\n")
outputFile:write(currZ)
outputFile:write("\n")
outputFile:write(currOrient)
outputFile:write("\n")
outputFile:write(levelToReturnTo)
outputFile:write("\n")
outputFile:write(prevMiningState)
outputFile:write("\n")
outputFile:write(orientationAtZeroZero)
outputFile:write("\n")
outputFile:close()
end
storedX = currX
storedY = currY
storedZ = currZ
storedOrient = currOrient
-- Store the current location and orientation so that it can be returned to
currMiningState = miningState.EMPTYINVENTORY
writeMessage("last item count = "..turtle.getItemCount(lastEmptySlot), messageLevel.DEBUG)
if ((turtle.getItemCount(lastEmptySlot) > 0) or (returnBackToMiningPoint == false)) then
writeMessage("Heading back to surface", messageLevel.DEBUG)
-- Move down to the correct layer to return via
if (currY > levelToReturnTo) then
while (currY > levelToReturnTo) do
turtleDown()
end
elseif (currY < levelToReturnTo) then
while (currY < levelToReturnTo) do
turtleUp()
end
end
if ((haveBeenAtZeroZeroOnLayer == false) or (orientationAtZeroZero == direction.FORWARD)) then
-- Move back to the correct X position first
if (currX > 0) then
turtleSetOrientation(direction.LEFT)
while (currX > 0) do
turtleForward()
end
elseif (currX < 0) then
-- This should never happen
writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
end
-- Then move back to the correct Z position
if (currZ > 0) then
turtleSetOrientation(direction.BACK)
while (currZ > 0) do
turtleForward()
end
elseif (currZ < 0) then
-- This should never happen
writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
end
else
-- Move back to the correct Z position first
if (currZ > 0) then
turtleSetOrientation(direction.BACK)
while (currZ > 0) do
turtleForward()
end
elseif (currZ < 0) then
-- This should never happen
writeMessage("Current z is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
end
-- Then move back to the correct X position
if (currX > 0) then
turtleSetOrientation(direction.LEFT)
while (currX > 0) do
turtleForward()
end
elseif (currX < 0) then
-- This should never happen
writeMessage("Current x is less than 0 in returnToStartAndUnload", messageLevel.ERROR)
end
end
-- Return to the starting layer
if (currY < startHeight) then
while (currY < startHeight) do
turtleUp()
end
elseif (currY > startHeight) then
-- This should never happen
writeMessage("Current height is greater than start height in returnToStartAndUnload", messageLevel.ERROR)
end
-- Empty the inventory
local slotLoop = 1
-- Face the chest
turtleSetOrientation(direction.BACK)
-- Loop over each of the slots (except the 16th one which stores fuel)
while (slotLoop < 16) do
-- If this is one of the slots that contains a noise block, empty all blocks except
-- one
turtle.select(slotLoop) -- Don't bother updating selected slot variable as it will set later in this function
if ((slotLoop <= noiseBlocksCount) or ((slotLoop == 15) and (lastEmptySlot == 14))) then
writeMessage("Dropping (n-1) from slot "..slotLoop.." ["..turtle.getItemCount(slotLoop).."]", messageLevel.DEBUG)
if (turtle.getItemCount(slotLoop) > 0) then
turtle.drop(turtle.getItemCount(slotLoop) - 1)
end
else
-- Not a noise block, drop all of the items in this slot
writeMessage("Dropping (all) from slot "..slotLoop.." ["..turtle.getItemCount(slotLoop).."]", messageLevel.DEBUG)
if (turtle.getItemCount(slotLoop) > 0) then
turtle.drop()
end
end
slotLoop = slotLoop + 1
end
-- While we are here, refill the fuel items if there is capacity
if (turtle.getItemCount(16) < 64) then
turtleSetOrientation(direction.LEFT)
turtle.select(16) -- Don't bother updating selected slot variable as it will set later in this function
local currFuelItems = turtle.getItemCount(16)
turtle.suck()
while ((currFuelItems ~= turtle.getItemCount(16)) and (turtle.getItemCount(16) < 64)) do
currFuelItems = turtle.getItemCount(16)
turtle.suck()
end
slotLoop = noiseBlocksCount + 1
-- Have now picked up all the items that we can. If we have also picked up some
-- additional fuel in some of the other slots, then drop it again
while (slotLoop <= lastEmptySlot) do
-- Drop any items found in this slot
if (turtle.getItemCount(slotLoop) > 0) then
turtle.select(slotLoop) -- Don't bother updating selected slot variable as it will set later in this function
turtle.drop()
end
slotLoop = slotLoop + 1
end
end
-- Select the 1st slot because sometimes when leaving the 15th or 16th slots selected it can result
-- in that slot being immediately filled (resulting in the turtle returning to base again too soon)
turtle.select(1)
currentlySelectedSlot = 1
end
-- If required, move back to the point that we were mining at before returning to the start
if (returnBackToMiningPoint == true) then
-- If resuming, refresh the starting point to be the top of the return shaft
if (resuming == true) then
currX = 0
currY = startHeight
currZ = 0
currOrient = resumeOrient
end
-- Return back to the required layer
while (currY > levelToReturnTo) do
turtleDown()
end
if ((haveBeenAtZeroZeroOnLayer == false) or (orientationAtZeroZero == direction.FORWARD)) then
-- Move back to the correct Z position first
writeMessage("Stored Z: "..storedZ..", currZ: "..currZ, messageLevel.DEBUG)
if (storedZ > currZ) then
writeMessage("Orienting forward", messageLevel.DEBUG)
writeMessage("Moving in z direction", messageLevel.DEBUG)
turtleSetOrientation(direction.FORWARD)
while (storedZ > currZ) do
turtleForward()
end
elseif (storedZ < currZ) then
-- This should never happen
writeMessage("Stored z is less than current z in returnToStartAndUnload", messageLevel.ERROR)
end
-- Then move back to the correct X position
if (storedX > currX) then
writeMessage("Stored X: "..storedX..", currX: "..currX, messageLevel.DEBUG)
writeMessage("Orienting right", messageLevel.DEBUG)
writeMessage("Moving in x direction", messageLevel.DEBUG)
turtleSetOrientation(direction.RIGHT)
while (storedX > currX) do
turtleForward()
end
elseif (storedX < currX) then
-- This should never happen
writeMessage("Stored x is less than current x in returnToStartAndUnload", messageLevel.ERROR)
end
else
-- Move back to the correct X position first
if (storedX > currX) then
writeMessage("Stored X: "..storedX..", currX: "..currX, messageLevel.DEBUG)
writeMessage("Orienting right", messageLevel.DEBUG)
writeMessage("Moving in x direction", messageLevel.DEBUG)
turtleSetOrientation(direction.RIGHT)
while (storedX > currX) do
turtleForward()
end
elseif (storedX < currX) then
-- This should never happen
writeMessage("Stored x is less than current x in returnToStartAndUnload", messageLevel.ERROR)
end
-- Then move back to the correct Z position
writeMessage("Stored Z: "..storedZ..", currZ: "..currZ, messageLevel.DEBUG)
if (storedZ > currZ) then
writeMessage("Orienting forward", messageLevel.DEBUG)
writeMessage("Moving in z direction", messageLevel.DEBUG)
turtleSetOrientation(direction.FORWARD)
while (storedZ > currZ) do
turtleForward()
end
elseif (storedZ < currZ) then
-- This should never happen
writeMessage("Stored z is less than current z in returnToStartAndUnload", messageLevel.ERROR)
end
end
-- Move back to the correct layer
if (storedY < currY) then
while (storedY < currY) do
turtleDown()
end
elseif (storedY > currY) then
while (storedY > currY) do
turtleUp()
end
end
-- Finally, set the correct orientation
turtleSetOrientation(storedOrient)
writeMessage("Have returned to the mining point", messageLevel.DEBUG)
end
-- Store the current location and orientation so that it can be returned to
currMiningState = prevMiningState
returningToStart = false
end
-- ********************************************************************************** --
-- Empties a chest's contents
-- ********************************************************************************** --
function emptyChest(suckFn)
local prevInventoryCount = {}
local inventoryLoop
local chestEmptied = false
-- Record the number of items in each of the inventory slots
for inventoryLoop = 1, 16 do
prevInventoryCount[inventoryLoop] = turtle.getItemCount(inventoryLoop)
end
while (chestEmptied == false) do
-- Pick up the next item
suckFn()
-- Determine the number of items in each of the inventory slots now
local newInventoryCount = {}
for inventoryLoop = 1, 16 do
newInventoryCount[inventoryLoop] = turtle.getItemCount(inventoryLoop)
end
-- Now, determine whether there have been any items taken from the chest
local foundDifferentItemCount = false
inventoryLoop = 1
while ((foundDifferentItemCount == false) and (inventoryLoop <= 16)) do
if (prevInventoryCount[inventoryLoop] ~= newInventoryCount[inventoryLoop]) then
foundDifferentItemCount = true
else
inventoryLoop = inventoryLoop + 1
end
end
-- If no items have been found with a different item count, then the chest has been emptied
chestEmptied = not foundDifferentItemCount
if (chestEmptied == false) then
prevInventoryCount = newInventoryCount
-- Check that there is sufficient inventory space as may have picked up a block
ensureInventorySpace()
end
end
writeMessage("Finished emptying chest", messageLevel.DEBUG)
end
-- ********************************************************************************** --
-- Write the current location to a file
-- ********************************************************************************** --
function saveLocation()
-- Write the x, y, z and orientation to the file
if ((supportResume == true) and (resuming == false)) then
local outputFile = io.open(oreQuarryLocation, "w")
outputFile:write(currMiningState)
outputFile:write("\n")
outputFile:write(currX)
outputFile:write("\n")
outputFile:write(currY)
outputFile:write("\n")
outputFile:write(currZ)
outputFile:write("\n")
outputFile:write(currOrient)
outputFile:write("\n")
outputFile:close()
end
end
-- ********************************************************************************** --
-- If the turtle is resuming and the current co-ordinates, orientation and
-- mining state have been matched, then no longer resuming
-- ********************************************************************************** --
function updateResumingFlag()
if (resuming == true) then
if ((resumeMiningState == currMiningState) and (resumeX == currX) and (resumeY == currY) and (resumeZ == currZ) and (resumeOrient == currOrient)) then
resuming = false
end
end
end
-- ********************************************************************************** --
-- Generic function to move the Turtle (pushing through any gravel or other
-- things such as mobs that might get in the way).
--
-- The only thing that should stop the turtle moving is bedrock. Where this is
-- found, the function will return after 15 seconds returning false
-- ********************************************************************************** --
function moveTurtle(moveFn, detectFn, digFn, attackFn, compareFn, suckFn, maxDigCount, newX, newY, newZ)
local moveSuccess = false
-- If we are resuming, then don't do anything in this function other than updating the
-- co-ordinates as if the turtle had moved
if (resuming == true) then
-- Set the move success to true (but don't move) - unless this is below bedrock level
-- in which case return false
if (currY <= 0) then
moveSuccess = false
else
moveSuccess = true
end
-- Update the co-ordinates to reflect the movement
currX = newX
currY = newY
currZ = newZ
else
local prevX, prevY, prevZ
prevX = currX
prevY = currY
prevZ = currZ
ensureFuel()
-- Flag to determine whether digging has been tried yet. If it has
-- then pause briefly before digging again to allow sand or gravel to
-- drop
local digCount = 0
if (lastMoveNeededDig == false) then
-- Didn't need to dig last time the turtle moved, so try moving first
currX = newX
currY = newY
currZ = newZ
saveLocation()
moveSuccess = moveFn()
-- If move failed, update the co-ords back to the previous co-ords
if (moveSuccess == false) then
currX = prevX
currY = prevY
currZ = prevZ
saveLocation()
end
-- Don't need to set the last move needed dig. It is already false, if
-- move success is now true, then it won't be changed
else
-- If we are looking for chests, then check that this isn't a chest before trying to dig it
if (lookForChests == true) then
if (isNoiseBlock(compareFn) == false) then
if (detectFn() == true) then
-- Determine if it is a chest before digging it
if (isChestBlock(compareFn) == true) then
-- Have found a chest, empty it before continuing
emptyChest (suckFn)
end
end
end
end
-- Try to dig (without doing a detect as it is quicker)
local digSuccess = digFn()
if (digSuccess == true) then
digCount = 1
end
currX = newX
currY = newY
currZ = newZ
saveLocation()
moveSuccess = moveFn()
if (moveSuccess == true) then
lastMoveNeededDig = digSuccess
else
currX = prevX
currY = prevY
currZ = prevZ
saveLocation()
end
end
-- Loop until we've successfully moved
if (moveSuccess == false) then
while ((moveSuccess == false) and (digCount < maxDigCount)) do
-- If there is a block in front, dig it
if (detectFn() == true) then
-- If we've already tried digging, then pause before digging again to let
-- any sand or gravel drop, otherwise check for a chest before digging
if(digCount == 0) then
-- Am about to dig a block - check that it is not a chest if necessary
-- If we are looking for chests, then check that this isn't a chest before moving
if (lookForChests == true) then
if (isNoiseBlock(compareFn) == false) then
if (detectFn() == true) then
-- Determine if it is a chest before digging it
if (isChestBlock(compareFn) == true) then
-- Have found a chest, empty it before continuing
emptyChest (suckFn)
end
end
end
end
else
sleep(0.1)
end
digFn()
digCount = digCount + 1
else
-- Am being stopped from moving by a mob, attack it
attackFn()
end
currX = newX
currY = newY
currZ = newZ
saveLocation()
-- Try the move again
moveSuccess = moveFn()
if (moveSuccess == false) then
currX = prevX
currY = prevY
currZ = prevZ
saveLocation()
end
end
if (digCount == 0) then
lastMoveNeededDig = false
else
lastMoveNeededDig = true
end
end
end
-- If we are resuming and the current co-ordinates and orientation are the resume point
-- then are no longer resuming
if (moveSuccess == true) then
updateResumingFlag()
end
-- Return the move success
return moveSuccess
end
-- ********************************************************************************** --
-- Move the turtle forward one block (updating the turtle's position)
-- ********************************************************************************** --
function turtleForward()
-- Determine the new co-ordinate that the turtle will be moving to
local newX, newZ
-- Update the current co-ordinates
if (currOrient == direction.FORWARD) then
newZ = currZ + 1
newX = currX
elseif (currOrient == direction.LEFT) then
newX = currX - 1
newZ = currZ
elseif (currOrient == direction.BACK) then
newZ = currZ - 1
newX = currX
elseif (currOrient == direction.RIGHT) then
newX = currX + 1
newZ = currZ
else
writeMessage ("Invalid currOrient in turtleForward function", messageLevel.ERROR)
end
local returnVal = moveTurtle(turtle.forward, turtle.detect, turtle.dig, turtle.attack, turtle.compare, turtle.suck, maximumGravelStackSupported, newX, currY, newZ)
if (returnVal == true) then
-- Check that there is sufficient inventory space as may have picked up a block
ensureInventorySpace()
end
return returnVal
end
-- ********************************************************************************** --
-- Move the turtle up one block (updating the turtle's position)
-- ********************************************************************************** --
function turtleUp()
local returnVal = moveTurtle(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp, turtle.compareUp, turtle.suckUp, maximumGravelStackSupported, currX, currY + 1, currZ)
if (returnVal == true) then
-- Check that there is sufficient inventory space as may have picked up a block
ensureInventorySpace()
end
return returnVal
end
-- ********************************************************************************** --
-- Move the turtle down one block (updating the turtle's position)
-- ********************************************************************************** --
function turtleDown()
local returnVal = moveTurtle(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown, turtle.compareDown, turtle.suckDown, 1, currX, currY - 1, currZ)
if (returnVal == true) then
-- Check that there is sufficient inventory space as may have picked up a block
ensureInventorySpace()
end
return returnVal
end
-- ********************************************************************************** --
-- Move the turtle back one block (updating the turtle's position)
-- ********************************************************************************** --
function turtleBack()
-- Assume that the turtle will move, and switch the co-ords back if it doesn't
-- (do this so that we can write the co-ords to a file before moving)
local newX, newZ
local prevX, prevZ
prevX = currX
prevZ = currZ
-- Update the current co-ordinates
if (currOrient == direction.FORWARD) then
newZ = currZ - 1
newX = currX
elseif (currOrient == direction.LEFT) then
newX = currX + 1
newZ = currZ
elseif (currOrient == direction.BACK) then
newZ = currZ + 1
newX = currX
elseif (currOrient == direction.RIGHT) then
newX = currX - 1
newZ = currZ
else
writeMessage ("Invalid currOrient in turtleBack function", messageLevel.ERROR)
end
-- First try to move back using the standard function
currX = newX
currZ = newZ
saveLocation()
local returnVal = turtle.back()
if (returnVal == false) then
-- Didn't move. Reset the co-ordinates to the previous value
currX = prevX
currZ = prevZ
-- Reset the location back to the previous location (because the turn takes 0.8 of a second
-- so could be stopped before getting to the forward function)
saveLocation()
turtle.turnRight()
turtle.turnRight()
-- Try to move by using the forward function (note, the orientation will be set as
-- the same way as this function started because if the function stops, that is the
-- direction that we want to consider the turtle to be pointing)
returnVal = moveTurtle(turtle.forward, turtle.detect, turtle.dig, turtle.attack, turtle.compare, turtle.suck, maximumGravelStackSupported, newX, currY, newZ)
turtle.turnRight()
turtle.turnRight()
end
if (returnVal == true) then
-- Check that there is sufficient inventory space as may have picked up a block
ensureInventorySpace()
end
return returnVal
end
-- ********************************************************************************** --
-- Turns the turtle (updating the current orientation at the same time)
-- ********************************************************************************** --
function turtleTurn(turnDir)
if (turnDir == direction.LEFT) then
if (currOrient == direction.FORWARD) then
currOrient = direction.LEFT
elseif (currOrient == direction.LEFT) then
currOrient = direction.BACK
elseif (currOrient == direction.BACK) then
currOrient = direction.RIGHT
elseif (currOrient == direction.RIGHT) then
currOrient = direction.FORWARD
else
writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
end
-- If we are resuming, just check to see whether have reached the resume point, otherwise
-- turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnLeft()
end
elseif (turnDir == direction.RIGHT) then
if (currOrient == direction.FORWARD) then
currOrient = direction.RIGHT
elseif (currOrient == direction.LEFT) then
currOrient = direction.FORWARD
elseif (currOrient == direction.BACK) then
currOrient = direction.LEFT
elseif (currOrient == direction.RIGHT) then
currOrient = direction.BACK
else
writeMessage ("Invalid currOrient in turtleTurn function", messageLevel.ERROR)
end
-- If we are resuming, just check to see whether have reached the resume point, otherwise
-- turn
if (resuming == true) then
updateResumingFlag()
writeMessage("["..currMiningState..", "..currX..", "..currY..", "..currZ..", "..currOrient.."]", messageLevel.DEBUG)
else
-- Write the new orientation and turn
saveLocation()
turtle.turnRight()
end
else
writeMessage ("Invalid turnDir in turtleTurn function", messageLevel.ERROR)
end
end
-- ********************************************************************************** --
-- Sets the turtle to a specific orientation, irrespective of its current orientation
-- ********************************************************************************** --
function turtleSetOrientation(newOrient)
if (currOrient ~= newOrient) then
if (currOrient == direction.FORWARD) then
if (newOrient == direction.RIGHT) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnRight()
end
elseif (newOrient == direction.BACK) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnRight()
turtle.turnRight()
end
elseif (newOrient == direction.LEFT) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnLeft()
end
else
writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
end
elseif (currOrient == direction.RIGHT) then
if (newOrient == direction.BACK) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnRight()
end
elseif (newOrient == direction.LEFT) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnRight()
turtle.turnRight()
end
elseif (newOrient == direction.FORWARD) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn
if (resuming == true) then
updateResumingFlag()
else
-- Write the new orientation and turn
saveLocation()
turtle.turnLeft()
end
else
writeMessage ("Invalid newOrient in turtleSetOrientation function", messageLevel.ERROR)
end
elseif (currOrient == direction.BACK) then
if (newOrient == direction.LEFT) then
currOrient = newOrient
-- If resuming, check whether the resume point has been reached, otherwise turn