Repository navigation
Expand file tree
/
Copy pathoptimizedMine.lua
More file actions
950 lines (831 loc) · 25.9 KB
/
Copy pathoptimizedMine.lua
File metadata and controls
950 lines (831 loc) · 25.9 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
-- My own vector implementation
local Vec = {}
--- Create a new instance of Vec
---@field x number the X axis
---@field y number the y axis
---@field z number the z axis
function Vec.new(x, y, z)
local self = {}
self.x = x
self.y = y
self.z = z
function self.toCCVec()
return vector.new(x, y, z)
end
function self.getX()
return x
end
function self.getY()
return y
end
function self.getZ()
return z
end
function self.addX()
x = x + 1
end
function self.subX()
x = x - 1
end
---@public
function self.addY()
y = y + 1
end
---@public
function self.subY()
y = y - 1
end
function self.addZ()
z = z + 1
end
function self.subZ()
z = z - 1
end
return self
end
-- Movement management related
local SMovementManagement = {}
--local SPos_mt = { __index = SMovementManagement }
local instance
--- Singleton Pos management
---@param ori_pos Vec the original pos of the turtle
---@param heading_dir number the dir of the length
---@param mining_dir number the dir of the width
---@param length_limit number the dist which the turtle has to mine the main way
---@param width_limit number the dist the turtle has to mine the mining way
---@public
SMovementManagement.new = function(ori_pos, heading_dir, mining_dir, length_limit, width_limit)
if instance then
return instance
end
local self = {}
self.ORIGINAL_POS = ori_pos
self.HEADING_DIR = heading_dir
self.MINING_DIR = mining_dir
self.LENGTH_LIMIT = length_limit
self.WIDTH_LIMIT = width_limit
self.dist_from_ori_pos = Vec.new(0, 0, 0)
self.actual_heading = heading_dir
self.level = 0
local is_mining = true
---@param move string the direction of the movement
---@private
function self.updateDist(move)
if self.actual_heading == 0 then
if move == "forth" then
self.dist_from_ori_pos.subZ()
elseif move == "back" then
self.dist_from_ori_pos.addZ()
else
error("updateDist")
end
elseif self.actual_heading == 1 then
if move == "forth" then
self.dist_from_ori_pos.subX()
elseif move == "back" then
self.dist_from_ori_pos.addX()
else
error("updateDist")
end
elseif self.actual_heading == 2 then
if move == "forth" then
self.dist_from_ori_pos.addZ()
elseif move == "back" then
self.dist_from_ori_pos.subZ()
else
error("updateDist")
end
elseif self.actual_heading == 3 then
if move == "forth" then
self.dist_from_ori_pos.addX()
elseif move == "back" then
self.dist_from_ori_pos.subX()
else
error("updateDist")
end
else
error("updateDist")
end
end
function self.left()
turtle.turnLeft()
self.actual_heading = ((self.actual_heading + 1) % 4)
end
function self.right()
turtle.turnRight()
self.actual_heading = ((self.actual_heading - 1) % 4)
end
---move the turtle forward, gravel insensitive
---@public
function self.moveForward()
local moved
repeat
moved = turtle.forward()
if not moved then
turtle.dig()
end
until moved
self.updateDist("forth")
end
---move the turtle back, gravel insensitive
---@public
function self.moveBackward()
local moved
repeat
moved = turtle.back()
if not moved then
self.right()
self.right()
turtle.dig()
self.right()
self.right()
end
until moved
--self.dist_from_ori_pos.
self.updateDist("back")
end
---move the turtle up, gravel insensitive
---@public
function self.moveUp()
local moved
repeat
moved = turtle.up()
if not moved then
turtle.digUp()
end
until moved
self.dist_from_ori_pos.addY()
end
---move the turtle down, gravel insensitive
---@public
function self.moveDown()
local moved
repeat
moved = turtle.down()
if not moved then
turtle.digDown()
end
until moved
self.dist_from_ori_pos.subY()
end
local function returnXHome()
-- north = -Z = 0
-- west = -X = 1
-- south = +Z = 2
-- east = +X = 3
repeat
local pos_x = self.dist_from_ori_pos.getX()
if pos_x ~= 0 then
self.moveBackward()
end
until pos_x == 0
end
---@private
local function returnYHome()
repeat
if self.dist_from_ori_pos.getY() < 0 then
self.moveUp()
elseif self.dist_from_ori_pos.getY() > 0 then
-- is_full_home this case really necessary ?
-- I think if I want to dig up in the future...
self.moveDown()
end
until self.dist_from_ori_pos.getY() == 0
end
--- Return to the middle level of a mine
local function returnToMiningLevel()
local pos_y = math.abs(self.dist_from_ori_pos.getY())
local has_to_move = ((pos_y % 4) ~= 0)
local has_to_move_up, has_to_move_down
if not has_to_move then
return
end
if level == 0 then
if pos_y == 0 then
return
end
pos_y = self.dist_from_ori_pos.getY()
if pos_y < 0 then
repeat
self.moveUp()
pos_y = self.dist_from_ori_pos.getY()
until pos_y == 0
return
end
if pos_y > 0 then
repeat
self.moveDown()
pos_y = self.dist_from_ori_pos.getY()
until pos_y == 0
end
end
has_to_move_up = ((level * 4) - pos_y) < 0
has_to_move_down = ((level * 4) - pos_y) > 0
if has_to_move_up then
repeat
self.moveUp()
pos_y = math.abs(self.dist_from_ori_pos.getY())
has_to_move_up = ((level * 4) - pos_y) < 0
until has_to_move_up
return
end
if has_to_move_down then
repeat
self.moveDown()
pos_y = math.abs(self.dist_from_ori_pos.getY())
has_to_move_down = ((level * 4) - pos_y) > 0
until has_to_move_down
return
end
end
---@private
local function returnZHome()
repeat
local pos_z = self.dist_from_ori_pos.getZ()
if pos_z ~= 0 then
self.moveBackward()
end
until pos_z == 0
end
--- return home, if is_full_home is true then it return to the start point otherwise
---it just return to the center
---@param is_full_home boolean whether it need to return to the start point
function self.returnHome(is_full_home)
returnToMiningLevel()
if is_mining then
-- north = -Z = 0
-- west = -X = 1
-- south = +Z = 2
-- east = +X = 3
if self.MINING_DIR == 0 or self.MINING_DIR == 2 then
returnZHome()
returnXHome()
elseif self.MINING_DIR == 1 or self.MINING_DIR == 3 then
returnXHome()
returnZHome()
else
error("returnHome")
end
if not is_full_home then
return
end
returnYHome()
return
end
if self.HEADING_DIR == 0 or self.HEADING_DIR == 2 then
returnZHome()
elseif self.HEADING_DIR == 1 or self.HEADING_DIR == 3 then
returnXHome()
else
error("returnHome")
end
if is_full_home then
returnYHome()
end
end
--- Tell whether the length limit has been reached
---@return boolean whether the limit has been reached
function self.isLengthLimitReached()
-- north = -Z = 0
-- west = -X = 1
-- south = +Z = 2
-- east = +X = 3
if self.HEADING_DIR == 0 or self.HEADING_DIR == 2 then
local pos_z = self.dist_from_ori_pos.getZ()
if math.abs(pos_z) < self.LENGTH_LIMIT then
return false
end
end
if self.HEADING_DIR == 1 or self.HEADING_DIR == 3 then
local pos_x = self.dist_from_ori_pos.getX()
if math.abs(pos_x) < self.LENGTH_LIMIT then
return false
end
end
return true
end
--function self.isWidthLimitReached()
-- -- north = -Z = 0
-- -- west = -X = 1
-- -- south = +Z = 2
-- -- east = +X = 3
-- if self.HEADING_DIR == 0 or self.MINING_DIR == 2 then
-- if self.LENGTH_LIMIT <= self.dist_from_ori_pos.getZ() then
-- self.returnHome(false)
-- end
-- end
--
-- if self.HEADING_DIR == 1 or self.MINING_DIR == 3 then
-- if self.LENGTH_LIMIT <= self.dist_from_ori_pos.getX() then
-- self.returnHome(false)
-- end
-- end
--end
--setmetatable(self, SPos_mt)
instance = self
return instance
end
-- inventory
--- Tell whether there are slot left in the inventory of the turtle
--- assume the inventory has been sorted before the call of that function
---@return boolean whether there is available slot in the inventory of the turtle
local function hasAvailableSlot()
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
return true
end
end
return false
end
--- make space in the inventory by dropping the unnecessary items
local function makeSpace()
for i = 1, 16, 1 do
local detail = turtle.getItemDetail(i, true)
if detail ~= nil then
if not (detail.tags and (detail.tags["forge:ores"] or detail.tags["forge:raw_materials"]
or detail.tags["minecraft:coals"] or detail.tags["forge:gems"]
or detail.tags["forge:ingots"] or detail.tags["forge:dusts/redstone"]
or detail.tags["forge:storage_blocks/coal"]
or detail.tags["forge:storage_blocks/charcoal"])) then
turtle.select(i)
turtle.drop()
end
end
end
turtle.select(1)
end
--- tell whether the slot is already taken or not
---@param slot number the number of the slot
local function isSlotTaken(slot)
turtle.select(slot)
local itemCount = turtle.getItemCount()
if itemCount >= 0 then
return false
else
return true
end
end
--- place a stack of ore/gems/redstone/coal et in the first slot possible
---iterate 16 * 16 times so it takes time
local function sortStacks()
for i = 1, 16 do
for j = 1, 16 do
turtle.select(j)
local is_slot_taken = isSlotTaken(i)
if not is_slot_taken then
turtle.transferTo(i)
end
end
end
end
---stack the item together if possible
---iterate 16 * 16 times so it takes time
local function stackItems()
for i = 1, 16 do
for j = 1, 16 do
turtle.select(j)
local isEqual = turtle.compareTo(i)
if isEqual then
local nbrToTransfer = turtle.getItemCount(j)
local firstSlotSpaceLeft = turtle.getItemSpace(i)
if ((firstSlotSpaceLeft - nbrToTransfer) <= 64) then
turtle.transferTo(i, nbrToTransfer)
else
turtle.transferTo(i, firstSlotSpaceLeft)
end
end
end
end
end
--- sortInventory the inventory by stacking items together if possible
local function sortInventory()
local has_available_slot = hasAvailableSlot()
if not has_available_slot then
stackItems()
sortStacks()
turtle.select(1)
end
end
--- This function has to be called after the inventory has been cleaned up
---emptyInventory the inventory into a chest except for fuel and refuel
local function emptyInventory()
for i = 1, 16 do
local data = turtle.getItemDetail(i, true)
local count
if data then
if data.tags and not (data.tags["minecraft:coals"] or
data.tags["forge:storage_blocks/coal"] or
data.tags["forge:storage_blocks/charcoal"]) then
count = turtle.getItemCount(i)
turtle.select(i)
turtle.dropUp(count)
end
end
end
turtle.select(1)
end
-- fuel
--- prompt the user to remind their to refuel the turtle before it start
local function askForFuel()
repeat
print("\nDid you put fuel in the first slot ? [Y/n]")
local input = string.lower(read())
until input == "y"
end
--- refuel the turtle
local function refuelTurtle()
local count
for i = 1, 16 do
turtle.select(i)
count = turtle.getItemCount(i)
turtle.refuel(count)
end
turtle.select(1)
end
--- first refuel of the turtle.
---Prompt
local function init_fuel()
askForFuel()
local level = turtle.getFuelLevel()
if level == "unlimited" then
error("Turtle does not need fuel")
end
refuelTurtle()
end
---refuel the turtle
-- cli
---ask input to the user to know the facing direction
---@return number dir the facing pos as defined
local function load_facing_direction()
-- north = -Z = 0
-- west = -X = 1
-- south = +Z = 2
-- east = +X = 3
local dir
repeat
print("In which direction the turtle is facing ?\n[0]: North\n[1]: West\n[2]: South\n[3]: East")
local input = string.lower(read())
print("")
dir = tonumber(input)
if not dir then
print(("Error: you must enter a valid number\n you typed '%s'"):format(input))
dir = -1
end
until (dir >= 0 and dir <= 3) and type(dir) == "number"
return dir
end
--- load the mining direction of the turtle
---@param facing_dir number the direction it will go forth
---@return number mining_dir the direction it will mine
local function load_mining_direction(facing_dir)
-- north = -Z = 0
-- west = -X = 1
-- south = +Z = 2
-- east = +X = 3
local input
local mining_dir
repeat
print("In which direction the turtle is going to mine ?\n")
if (facing_dir % 2 == 0) then
print("[1]: West\n[3]: East")
else
print("[0]: North\n[2]: South")
end
input = string.lower(read())
print("")
mining_dir = tonumber(input)
if not mining_dir then
print(("Error: you must enter a valid number\nYou typed %s"):format(input))
print("")
mining_dir = -1
end
-- check if the values are correct e.g: not opposite one from another
if ((facing_dir % 2 == 0) and (mining_dir == 0 or mining_dir == 2)) or ((facing_dir % 2 == 1) and (mining_dir == 1 or mining_dir == 3)) then
print(("Error: check the choices !\n"))
mining_dir = -1
end
until (mining_dir >= 0 and mining_dir <= 3) and type(mining_dir) == "number"
return mining_dir
end
local function askForLength()
local input
local length
repeat
print("What length the turtle should mine ?\n")
input = string.lower(read())
length = tonumber(input)
if length and length >= 0 then
return length
else
print("Error: type a valid number (i.e: >= 0)")
length = -1
end
until type(length) == "number" and length >= 0
end
local function askForWidth()
local input, width
repeat
print("What width the turtle should mine ?\n")
input = string.lower(read())
width = tonumber(input)
if width and width >= 0 then
return width
else
print("Error: type a valid number (i.e: >= 0)")
width = -1
end
until type(width) == "number" and width >= 0
end
--- ask for the user's input the X and Z of the starting point of the turtle
---@return number, number, number the original pos of the turtle (X, Y, Z)
local function askForStartPoint()
-- Ask for the X starting point of the turtle
local pos_x, input_x
while type(pos_x) ~= "number" do
print("X=")
input_x = string.lower(read())
print("")
pos_x = tonumber(input_x)
end
-- Ask for the Y starting point
local pos_y, input_y
while type(pos_y) ~= "number" do
print("Y=")
input_y = string.lower(read())
print("")
pos_y = tonumber(input_y)
end
-- Ask for the Z starting point of the turtle
local pos_z, input_z
while type(pos_z) ~= "number" do
print("Z=")
input_z = string.lower(read())
print("")
pos_z = tonumber(input_z)
end
return pos_x, pos_y, pos_z
end
-- optimizedMine
--- inspect each sides of the turtle to find ores i.e:
---
---1= front
---
---2= right
---
---3= back
---
---4:= left
---@return boolean, number whether there is ore and the side which it found it
local function detectOreEachSide()
for i = 1, 4 do
local has_block, data = turtle.inspect()
if has_block then
if data and data.tags and (data.tags["forge:ores"] or
data.tags["forge:raw_materials"] or
data.tags["minecraft:coals"] or
data.tags["forge:gems"] or
data.tags["forge:ingots"] or
data.tags["forge:dusts/redstone"])
then
---@TODO make the turtle turn until it found its original facing
for _ = i - 1, 1, -1 do
turtle.turnLeft()
end
return true, i
end
end
turtle.turnRight()
end
return false, 0
end
--- detect whether there is or on the up side
---@return boolean whether there is ore
local function detectOreUp()
local has_block, data = turtle.inspectUp()
if has_block then
if data and data.tags and (data.tags["forge:ores"] or
data.tags["forge:raw_materials"] or
data.tags["minecraft:coals"] or
data.tags["forge:gems"] or
data.tags["forge:ingots"] or
data.tags["forge:dusts/redstone"])
then
return true
else
return false
end
else
return false
end
end
--- detect whether there is or on the down side
---@return boolean whether there is ore
local function detectOreDown()
local has_block, data = turtle.inspectDown()
if has_block then
if data and data.tags and (data.tags["forge:ores"] or
data.tags["forge:raw_materials"] or
data.tags["minecraft:coals"] or
data.tags["forge:gems"] or
data.tags["forge:ingots"] or
data.tags["forge:dusts/redstone"])
then
return true
else
return false
end
else
return false
end
end
--- A recursive function that dig all the ores and go back with the exact same path
---@param ore_path table the path of the ores
local function orePathFinder(ore_path, moves)
local has_ore_side, side = detectOreEachSide()
local has_ore_up = detectOreUp()
local has_ore_down = detectOreDown()
local has_ore = has_ore_side or has_ore_up or has_ore_down
local reverse_side
-- backward pathfinding, its deleted the last entry of the table
if not has_ore then
reverse_side = table.remove(ore_path)
if reverse_side then
-- 1: front => back
-- 2: right => left
-- 3: back => front
-- 4: left => right
-- 5: down => up
-- 6: up => down
if reverse_side == 1 then
moves.moveBackward()
elseif reverse_side == 2 then
moves.moveBackward()
moves.left()
elseif reverse_side == 3 then
moves.moveBackward()
moves.right()
moves.right()
elseif reverse_side == 4 then
moves.moveBackward()
moves.right()
elseif reverse_side == 5 then
moves.moveUp()
elseif reverse_side == 6 then
moves.moveDown()
end
end
-- forward ore finding, it insert the direction one the table
-- 1: front
-- 2: right
-- 3: back
-- 4: left
-- 5: down
-- 6: up
elseif has_ore_side then
if side then
if side == 1 then
moves.moveForward()
table.insert(ore_path, side)
elseif side == 2 then
moves.right()
moves.moveForward()
table.insert(ore_path, side)
elseif side == 3 then
moves.right()
moves.right()
moves.moveForward()
table.insert(ore_path, side)
elseif side == 4 then
moves.left()
moves.moveForward()
table.insert(ore_path, side)
end
end
elseif has_ore_down then
moves.moveDown()
table.insert(ore_path, 5)
elseif has_ore_up then
moves.moveUp()
table.insert(ore_path, 6)
end
if ((not has_ore) and (#ore_path == 0)) then
return
else
orePathFinder(ore_path, moves)
end
end
--- mine straight for 64 blocks and then come backward
local function mineStraight(move_management)
local ores
for _ = 1, move_management.WIDTH_LIMIT, 1 do
ores = {}
orePathFinder(ores, move_management)
move_management.moveForward()
-- do the up step
move_management.moveUp()
ores = {}
orePathFinder(ores, move_management)
move_management.moveDown()
-- do the final step: the down one
move_management.moveDown()
ores = {}
orePathFinder(ores, move_management)
move_management.moveUp()
if not hasAvailableSlot then
makeSpace()
end
end
refuelTurtle()
makeSpace()
for _ = 1, move_management.WIDTH_LIMIT, 1 do
move_management.moveBackward()
end
sortInventory()
end
local function mine3BlocksStraight(move_management)
for _ = 1, 3 do
turtle.dig()
move_management.moveForward()
turtle.digUp()
turtle.digDown()
end
end
---
local function shift(side, move_management)
if side == "left" then
move_management.right()
mine3BlocksStraight(move_management)
elseif side == "right" then
move_management.left()
mine3BlocksStraight(move_management)
else
error("Should not happened")
end
end
---Mine below the turtle to reach the next level (aka stage)
---@param moves SMovementManagement the movement management singleton
local function mine4BlocksBelow(moves)
moves.level = moves.level + 1
for i = 1, (moves.level * 4) do
local actual_pos_y = moves.ORIGINAL_POS.getY() - math.abs(moves.dist_from_ori_pos.getY())
if actual_pos_y == -57 and ((i % 4) ~= 3) then
moves.returnHome(true)
break
end
moves.moveDown()
end
end
-- Instructions
init_fuel()
-- local posX, posZ = load_start_point()
local facing_dir = load_facing_direction()
local mining_dir = load_mining_direction(facing_dir)
local length = askForLength()
local width = askForWidth()
local ori_x, ori_y, ori_z = askForStartPoint()
local ori_pos = Vec.new(ori_x, ori_y, ori_z)
local moves = SMovementManagement.new(ori_pos, facing_dir, mining_dir, length, width)
-- north = -Z = 0
-- west = -X = 1
-- south = +Z = 2
-- east = +X = 3
-- all the mine left moves:
-- (facing_dir == 0 and mining_dir == 1)
-- (facing_dir == 1 and mining_dir == 2)
-- (facing_dir == 2 and mining_dir == 3)
-- (facing_dir == 3 and mining_dir == 0)
-- all the mine right moves
-- (facing_dir == 0 and mining_dir == 3)
-- (facing_dir == 1 and mining_dir == 0)
-- (facing_dir == 2 and mining_dir == 1)
-- (facing_dir == 3 and mining_dir == 2)
local max_y = moves.ORIGINAL_POS.getY() - math.abs(moves.dist_from_ori_pos.getY())
while max_y > -57 do
local has_available_slot = hasAvailableSlot()
local is_limit_reached = moves.isLengthLimitReached()
while has_available_slot and not is_limit_reached do
if (facing_dir == 0 and mining_dir == 1) or (facing_dir == 1 and mining_dir == 2) or
(facing_dir == 2 and mining_dir == 3) or (facing_dir == 3 and mining_dir == 0) then
-- if the turtle needs to mine to its left at the beginning
local side = "left"
moves.left()
mineStraight(moves)
shift(side, moves)
elseif (facing_dir == 0 and mining_dir == 3) or (facing_dir == 1 and mining_dir == 0) or
(facing_dir == 2 and mining_dir == 1) or (facing_dir == 3 and mining_dir == 2) then
-- if the turtle needs to mine to its right at the beginning
local side = "right"
moves.right()
mineStraight(moves)
shift(side, moves)
end
has_available_slot = hasAvailableSlot()
is_limit_reached = moves.isLengthLimitReached()
end
max_y = moves.ORIGINAL_POS.getY() - math.abs(moves.dist_from_ori_pos.getY())
moves.returnHome(true)
makeSpace()
--- need to return home moves.returnHome(true)
emptyInventory()
mine4BlocksBelow(moves)
end