View difference between Paste ID: PJZ9sWhB and dQJFnnAF
SHOW: | | - or go back to the newest paste.
1
-- Cloned
2
-- This is an API for digging and
3
-- keeping track of motion
4
-- <[email protected]>
5
6
-- x is right, y is up, z is forward
7
-- r is clockwise rotation in degrees
8
9
-------------------------------------
10
-- |¯\  [¯¯]  /¯¯]    /\  |¯\ [¯¯] --
11
-- |  |  ][  | [¯|   |  | | /  ][  --
12
-- |_/  [__]  \__|   |||| ||  [__] --
13
-------------------------------------
14
15
os.loadAPI("flex.lua")
16
17
local options = {
18
  "outputblocks",
19
  "blacklist",
20
  "buildingblocks",
21
  "dumplist",
22
  "fluids"  }
23
24
25
-- Fluids, aka blocks treated as "Empty"
26
options[options[5]] = {
27
  ":air", "water", "lava", "acid", "poison",
28
  "sewage", "sludge", "blood"  }
29
30
31
-- Turtle will only drop loot into blocks
32
-- that contain any of these keywords.
33
options[options[1]] = {
34
  "chest", "storage", "box", "turtle",
35
  "hopper", "dropper", "backpack" }
36
37
38
-- Turtle will avoid breaking (or getting
39
-- stuck by) these blocks.
40
-- Disabled by default;
41
-- enable with dig.doBlacklist()
42
options[options[2]] = { "chest", "spawn",
43
    "hopper", "dropper", "portal", "turtle",
44
    "hive", "openblocks:grave" }
45
doblacklist = false
46
function doBlacklist(x)
47
 if x == nil then
48
  x = true
49
 end --if
50
 doblacklist = (x==true)
51
end --function
52
53
54
-- Run dig.setBlockSlot(n) to choose
55
-- a slot to keep building blocks stocked
56
-- The list of valid keywords:
57
options[options[3]] = { "cobblestone",
58
  "minecraft:stone", "dirt", "netherrack",
59
  "basalt", "soul_s", "magma_block",
60
  "terracotta", "rock", "sandstone",
61
  "andesite", "diorite", "granite",
62
  "marble", "bricks", "smooth_stone",
63
  "glass" }
64
function isBuildingBlock(n)
65
 return flex.isItem(options[options[3]],n)
66
end --function
67
68
69
70
-- Turtle will dispose of these blocks
71
-- at the command dig.doDump()/
72
-- dig.doDumpUp()/dig.doDumpDown().
73
-- You may add/remove entries by using
74
-- dig.addToDumpList(<string>) or
75
-- dig.removeFromDumpList(<string>).
76
local dump = options[4]
77
options[dump] = {}
78
79
function resetDumpList(n)
80
 if n == 0 then
81
  options[dump] = {}
82
 else
83
  options[dump] = { 
84
     "cobblestone", "dirt", "gravel",
85
     "andesite", "diorite", "granite",
86
     "netherrack", "soul_s", "magma_block",
87
     "rotten_flesh", "rock", "marble",
88-
     "gabbro", "scoria" }
88+
89
     "gabbro", "scoria", "diorite", "granite", "slate" }
90
 end --if/else
91
end --function
92
93
function isDumpItem(x)
94
 return flex.isItem(options["dump"],x)
95
end --function
96
97
function addToDumpList(newkey)
98
 options[dump][#options[dump]+1] =
99
     tostring(newkey)
100
end --function
101
102
function removeFromDumpList(key)
103
 local x,z
104
 z = {}
105
 for x=1,#options[dump] do
106
  if string.find(options[dump][x],key)==nil then
107
   z[#z+1] = options[dump][x]
108
  end --if
109
 end --for
110
 options[dump] = z
111
end --function
112
113
resetDumpList()
114
115
116
function doDump(z)
117
 z = z or "fwd"
118
 local slot = turtle.getSelectedSlot()
119
 local blockCount = blockStacks
120
 local x
121
 for x=1,16 do
122
  turtle.select(x)
123
  if flex.isItem(options[dump]) then
124
   
125
   if flex.isItem(options["buildingblocks"]) then
126
    blockCount = blockCount - 1
127
   end --if
128
   
129
   if blockCount <= 0 then
130
    if z == "fwd" then
131
     turtle.drop()
132
    elseif z == "down" then
133
     turtle.dropDown()
134
    elseif z == "up" then
135
     turtle.dropUp()
136
    end --if/else (direction)
137
   end --if (no more blocks needed)
138
   
139
  end --if dump item
140
 end --for
141
 checkBlocks()
142
 flex.condense(blockSlot)
143
 turtle.select(slot)
144
end --function
145
146
function doDumpDown() doDump("down") end
147
function doDumpUp() doDump("up") end
148
149
150
151
152
-- Attack entities that block the way
153
attack = false
154
function doAttack(x)
155
 if x == nil then
156
  x = true
157
 end --if
158
 attack = ( x == true )
159
end --function
160
161
162
function isChest(dir)
163
 return flex.isBlock(
164
   options["outputblocks"],dir)
165
end --function
166
function isChestUp()
167
 return isChest("up")
168
end
169
function isChestDown()
170
 return isChest("down")
171
end
172
173
174
local knownBedrock = {}
175
function getKnownBedrock()
176
 return knownBedrock
177
end --function
178
179
180
181
182
------------------------------------
183
--  /¯\ |¯\[¯¯][¯¯] /¯\ |\ ||/¯¯\ --
184
-- | O || / ||  ][ | O || \ |\_¯\ --
185
--  \_/ ||  || [__] \_/ || \|\__/ --
186
------------------------------------
187
--      |¯¯][¯¯]||  |¯¯]          --
188
--      | ]  ][ ||_ | ]           --
189
--      ||  [__]|__]|__]          --
190
------------------------------------
191
192
local options_file = "dig_options.cfg"
193
194
function optionsEdit()
195
 shell.run("edit "..options_file)
196
 optionsImport()
197
end --function
198
199
200
function optionsImport()
201
 if not fs.exists(options_file) then
202
  return false
203
 end --if
204
 local file,x,y,z
205
 
206
 for y=1,#options do
207
  z = options[y]
208
  file = fs.open(options_file,"r")
209
  
210
  x = file.readLine()
211
  while x ~= nil do
212
   
213
   if x == "["..z.."]" then
214
    options[z] = {}
215
    x = file.readLine()
216
    while x ~= "[/"..z.."]" and x ~= nil do
217
     options[z][#options[z]+1] = x
218
     x = file.readLine()
219
    end --while
220
    break
221
   end --if
222
   
223
   x = file.readLine()
224
  end --while
225
  
226
  file.close()
227
 end --for
228
 
229
 return true
230
end --function
231
232
233
function optionsExport()
234
 if fs.exists(options_file) then
235
  fs.delete(options_file)
236
 end --if
237
 
238
 local file,x,y,z
239
 while file == nil do
240
  file = fs.open(options_file,"w")
241
 end --while
242
 file.writeLine("# Dig API Options File #\n")
243
 
244
 for y=1,#options do
245
  z = options[y]
246
  file.writeLine("["..z.."]")
247
  for x=1,#options[z] do
248
   file.writeLine(options[z][x])
249
  end --for
250
  file.writeLine("[/"..z.."]\n")
251
 end --for
252
 
253
 file.close()
254
 return true
255
end --function
256
257
258
if not optionsImport() then
259
 optionsExport()
260
end --if
261
262
263
264
265
----------------------------------
266
--    /¯¯\  /\  ||  || |¯¯]     --
267
--    \_¯\ |  |  \\//  | ]      --
268
--    \__/ ||||   \/   |__]     --
269
----------------------------------
270
-- |¯\  /\   /¯] ||// || || |¯\ --
271
-- | < |  | | [  | <  ||_|| | / --
272
-- |_/ ||||  \_] ||\\  \__| ||  --
273
----------------------------------
274
275
local save = {"dig_save", ".cfg"}
276
local savefile = save[1] .. save[2]
277
local start = {"startup", ".lua"}
278
local startfile = start[1] .. start[2]
279
280
function saveExists()
281
 return ( fs.exists(startfile) and
282
    fs.exists(savefile) )
283
end --function
284
285
function saveClear()
286
 if fs.exists(startfile) then
287
  fs.delete(startfile)
288
 end --if
289
end --function
290
function clearSave()
291
 saveClear()
292
end --function
293
294
295
local args = {...}
296
if #args > 0 then
297
 local a,file
298
 
299
 if args[1] == "save" then
300
  if not saveExists() then
301
   flex.send("Nothing to save",colors.yellow)
302
   return
303
  end --if
304
  if #args == 1 then
305
   a = 1
306
   while fs.exists(start[1].."_"..
307
        tostring(a)..start[2]) do
308
    a = a + 1
309
   end --while
310
  else -- #args > 1
311
   a = args[2]
312
  end --if
313
  a = tostring(a)
314
  shell.run("mv "..startfile.." "..
315
      start[1].."_"..a..start[2])
316
  shell.run("mv "..savefile.." " ..
317
      save[1].."_"..a..save[2])
318
  flex.send("Enter 'dig load "..a..
319
      "' to restore",colors.lightBlue)
320
  
321
 elseif args[1] == "load" then
322
  if #args == 1 then
323
   flex.send("Please specify load file",
324
     colors.red)
325
   return
326
  end --if
327
  saveClear()
328
  a = args[2]
329
  shell.run("mv "..start[1].."_"..a..
330
     start[2].." "..startfile)
331
  shell.run("mv "..save[1].."_"..a..
332
     save[2].." "..savefile)
333
  flex.send("Save files restored",
334
     colors.lightBlue)
335
  
336
 elseif args[1] == "clear" then
337
  saveClear()
338
  flex.send("Save files cleared",
339
      colors.lightBlue)
340
  
341
 elseif args[1] == "edit" then
342
  optionsEdit()
343
  
344
 else -- not save/load/clear/edit
345
  flex.send("Invalid parameter: "..
346
      args[1],colors.red)
347
  
348
 end --if/else save/load
349
 return
350
end --function
351
352
353
-----------------------------------------
354
-- ||   /¯\  /¯] /\ [¯¯][¯¯] /¯\ |\ || --
355
-- ||_ | O || [ |  | ||  ][ | O || \ | --
356
-- |__] \_/  \_]|||| || [__] \_/ || \| --
357
-----------------------------------------
358
--||  || /\ |¯\[¯¯] /\ |¯\||  |¯¯]/¯¯\ --
359
-- \\// |  || / ][ |  || <||_ | ] \_¯\ --
360
--  \/  ||||| \[__]|||||_/|__]|__]\__/ --
361
-----------------------------------------
362
363
xdist = 0
364
xlast = -1
365
xmin = 0
366
xmax = 0
367
368
ydist = 0
369
ylast = -1
370
ymin = 0
371
ymax = 0
372
373
zdist = 0
374
zlast = -1
375
zmin = 0
376
zmax = 0
377
378
rdist = 0
379
rlast = -1
380
381
lastmove = "r-"
382
dugtotal = 0
383
384
385
function getx() return xdist end
386
function gety() return ydist end
387
function getz() return zdist end
388
function getr() return rdist end
389
390
function setx(x) xdist = x end
391
function sety(y) ydist = y end
392
function setz(z) zdist = z end
393
function setr(r) rdist = r end
394
395
function getxmin() return xmin end
396
function getxmax() return xmax end
397
function getymin() return ymin end
398
function getymax() return ymax end
399
function getzmin() return zmin end
400
function getzmax() return zmax end
401
402
function setxmin(x) xmin = x end
403
function setxmax(x) xmax = x end
404
function setymin(y) ymin = y end
405
function setymax(y) ymax = y end
406
function setzmin(z) zmin = z end
407
function setzmax(z) zmax = z end
408
409
function getxlast() return xlast end
410
function getylast() return ylast end
411
function getzlast() return zlast end
412
function getrlast() return rlast end
413
414
function setxlast(x) xlast = x end
415
function setylast(y) ylast = y end
416
function setzlast(z) zlast = z end
417
function setrlast(r) rlast = r end
418
419
function getlast() return lastmove end
420
function setlast(lm) lastmove = lm end
421
422
function getdug() return dugtotal end
423
function setdug(d) dugtotal = d end
424
425
426
427
428
-------------------------------------------
429
--||   /¯\  /\ |¯\   ///¯¯\ /\ ||  |||¯¯]--
430
--||_ | O ||  ||  | // \_¯\|  | \\// | ] --
431
--|__] \_/ |||||_/ //  \__/||||  \/  |__]--
432
-------------------------------------------
433
434
function location()
435
 return
436
  { xdist, ydist, zdist, rdist,
437
    xmin, xmax, ymin, ymax, zmin, zmax,
438
    xlast, ylast, zlast, rlast,
439
    lastmove, dugtotal }
440
end
441
442
443
function saveCoords(loc,save)
444
 loc = loc or location()
445
 save = save or savefile
446
 local file,x
447
 
448
 --if fs.exists(save.."_old") then
449
 -- fs.delete(save.."_old")
450
 --end
451
 --if fs.exists(save) then
452
  --fs.move(save, save.."_old")
453
 --end
454
 
455
 while file == nil do
456
  file = fs.open(save,"w")
457
 end --while
458
 
459
 for x=1,#loc do
460
  file.writeLine(tostring(loc[x]))
461
  if x == 4 then file.flush() end
462
 end --for
463
 
464
 file.close()
465
end --function
466
467
468
469
function loadCoords(save)
470
 save = save or savefile
471
 local file
472
 if fs.exists(save) then
473
  while file == nil do
474
   file = fs.open(save,"r")
475
  end --while
476
  xdist = tonumber(file.readLine() or xdist)
477
  ydist = tonumber(file.readLine() or ydist)
478
  zdist = tonumber(file.readLine() or zdist)
479
  rdist = tonumber(file.readLine() or rdist)
480
  xmin = tonumber(file.readLine() or xmin)
481
  xmax = tonumber(file.readLine() or xmax)
482
  ymin = tonumber(file.readLine() or ymin)
483
  ymax = tonumber(file.readLine() or ymax)
484
  zmin = tonumber(file.readLine() or zmin)
485
  zmax = tonumber(file.readLine() or zmax)
486
  xlast = tonumber(file.readLine() or xlast)
487
  ylast = tonumber(file.readLine() or ylast)
488
  zlast = tonumber(file.readLine() or zlast)
489
  rlast = tonumber(file.readLine() or rlast)
490
  lastmove = file.readLine() or lastmove
491
  dugtotal = tonumber(file.readLine() or dugtotal)
492
  file.close()
493
  return true
494
  
495
 else
496
  --if fs.exists(save.."_old") then
497
  -- loadCoords(save.."_old")
498
  -- return true
499
  --end
500
  
501
  return false
502
 end --if/else
503
 
504
end -- function
505
506
507
508
function makeStartup(command, args)
509
 command = tostring(command)
510
 args = args or {}
511
 local x
512
 for x=1,#args do
513
  command = command.." "..args[x]
514
 end --for
515
 
516
 local file = fs.open(startfile,"w")
517
 file.writeLine("print(\"> "..command.."\")")
518
 file.writeLine("for x=5,1,-1 do")
519
 file.writeLine(" term.write(tostring(x)..\" \")")
520
 file.writeLine(" sleep(1)")
521
 file.writeLine("end --for")
522
 file.writeLine("print(\" \")")
523
 file.writeLine("shell.run(\""..command.."\")")
524
 file.close()
525
end --function
526
527
528
529
530
-------------------------------------
531
--[¯¯]|¯\ /\  /¯]||//[¯¯]|\ || /¯¯]--
532
-- || | /|  || [ | <  ][ | \ || [¯|--
533
-- || | \|||| \_]||\\[__]|| \| \__|--
534
-------------------------------------
535
536
function update(n)
537
 
538
 if n=="fwd" then
539
  
540
  if rdist%360 == 0 then -- 12:00
541
   zdist = zdist + 1
542
   zlast = 1
543
   lastmove = "z+"
544
   
545
  elseif rdist%360 == 90 then -- 3:00
546
   xdist = xdist + 1
547
   xlast = 1
548
   lastmove = "x+"
549
   
550
  elseif rdist%360 == 180 then -- 6:00
551
   zdist = zdist - 1
552
   zlast = -1
553
   lastmove = "z-"
554
   
555
  elseif rdist%360 == 270 then -- 9:00
556
   xdist = xdist - 1
557
   xlast = -1
558
   lastmove = "x-"
559
   
560
  end --if/else
561
  
562
  
563
 elseif n=="back" then
564
  
565
  if rdist%360 == 0 then -- 12:00
566
   zdist = zdist - 1
567
   zlast = -1
568
   lastmove = "z-"
569
   
570
  elseif rdist%360 == 90 then -- 3:00
571
   xdist = xdist - 1
572
   xlast = -1
573
   lastmove = "x-"
574
   
575
  elseif rdist%360 == 180 then -- 6:00
576
   zdist = zdist + 1
577
   zlast = 1
578
   lastmove = "z+"
579
   
580
  elseif rdist%360 == 270 then -- 9:00
581
   xdist = xdist + 1
582
   xlast = 1
583
   lastmove = "x+"
584
   
585
  end --if/else
586
  
587
  
588
 elseif n=="up" then
589
  ydist = ydist + 1
590
  ylast = 1
591
  lastmove = "y+"
592
  
593
 elseif n=="down" then
594
  ydist = ydist - 1
595
  ylast = -1
596
  lastmove = "y-"
597
  
598
 elseif n=="right" then
599
  rdist = rdist + 90
600
  rlast = 1
601
  lastmove = "r+"
602
  while rdist > 999 do
603
   rdist = rdist - 360
604
  end --while
605
  
606
 elseif n=="left" then
607
  rdist = rdist - 90
608
  rlast = -1
609
  lastmove = "r-"
610
  while rdist < -999 do
611
   rdist = rdist + 360
612
  end --while
613
  
614
 end --if/else
615
 
616
 
617
 if xdist < xmin then
618
  xmin = xdist
619
 elseif xdist > xmax then
620
  xmax = xdist
621
 end
622
 
623
 if ydist < ymin then
624
  ymin = ydist
625
 elseif ydist > ymax then
626
  ymax = ydist
627
 end
628
 
629
 if zdist < zmin then
630
  zmin = zdist
631
 elseif zdist > zmax then
632
  zmax = zdist
633
 end
634
 
635
 saveCoords()
636
 
637
end
638
639
640
641
642
------------------------------------------
643
-- |\/| /¯\ ||  |||¯¯]|\/||¯¯]|\ ||[¯¯] --
644
-- |  || O | \\// | ] |  || ] | \ | ||  --
645
-- |||| \_/   \/  |__]|||||__]|| \| ||  --
646
------------------------------------------
647
648
649
stuck = false
650
function isStuck()
651
 return stuck
652
end
653
stuckDir = "none"
654
function getStuckDir()
655
 return stuckDir
656
end
657
658
659
function left(n)
660
 n = (n or 1)
661
 if n < 0 then
662
  return right(-n)
663
 end --if
664
 local x
665
 for x=1,n do
666
  turtle.turnLeft()
667
  update("left")
668
 end --if
669
 return true
670
end --function
671
672
673
function right(n)
674
 n = (n or 1)
675
 if n < 0 then
676
  return left(-n)
677
 end --if
678
 local x
679
 for x=1,n do
680
  turtle.turnRight()
681
  update("right")
682
 end --if
683
 return true
684
end --function
685
686
687
688
local unbreak = "Unbreakable block detected"
689
local protect = "Cannot break protected block"
690
691
692
693
function up(n)
694
 n = n or 1
695
 if n < 0 then
696
  return down(-n)
697
 end --if
698
 
699
 local x,a,b,t
700
 
701
 for x=1, n do
702
  refuel()
703
  
704
  if not turtle.up() then
705
   
706
   if doblacklist then
707
    -- Blocks not to mine
708
    if flex.isBlockUp(options["blacklist"]) then
709
     if not back() then return false end
710
     if not up() then return false end
711
     return true
712
    end --if
713
   end --if
714
   
715
   t = os.time()
716
   stuck = false
717
   while not turtle.up() do
718
    a,b = turtle.digUp()
719
    while a do
720
     dugtotal = dugtotal + 1 
721
     a,b = turtle.digUp()
722
    end --while
723
    
724
    if b == protect then
725
     if doblacklist then
726
      options["blacklist"][
727
         #options["blacklist"]+1]
728
         = flex.getBlockUp()
729
      return up(n-x+1)
730
     else
731
      b = unbreak
732
     end --if
733
    end --if
734
    
735
    if b == unbreak then
736
     flex.send("#1"..b.."#0: {#8"..
737
       tostring(getx()).."#0,#8"..
738
       tostring(gety()+1).."#0,#8"..
739
       tostring(getz()).."#0}")
740
     knownBedrock[#knownBedrock+1] = {
741
       getx(), gety()+1, getz() }
742
     stuck = true
743
     stuckDir = "up"
744
     return false
745
    end --if
746
    if a then
747
     dugtotal = dugtotal + 1
748
    end --if
749
    if attack then turtle.attackUp() end
750
    
751
    if (os.time()-t)/24*20*60 > 20 then
752
     -- Stuck for at least 20 seconds
753
     -- Usually caused by height limits
754
     flex.send("Unable to move up",colors.orange)
755
     stuck = true
756
     stuckDir = "up"
757
     return false
758
    end --if
759
    
760
   end --while
761
   
762
  end --if can't move
763
  
764
  update("up")
765
 end --for
766
 
767
 return true
768
end --function
769
770
771
772
function down(n)
773
 n = n or 1
774
 if n < 0 then
775
  return up(-n)
776
 end --if
777
 
778
 local x,a,b,t
779
 
780
 for x=1, n do
781
  refuel()
782
  
783
  if not turtle.down() then
784
   
785
   if doblacklist then
786
    -- Blocks not to mine
787
    if flex.isBlockDown(options["blacklist"]) then
788
     if not fwd() then return false end
789
     if not down() then return false end
790
     return true
791
    end --if
792
   end --if
793
   
794
   t = os.time()
795
   stuck = false
796
   while not turtle.down() do
797
    a,b = turtle.digDown()
798
    while a do
799
     dugtotal = dugtotal + 1
800
     a,b = turtle.digDown()
801
    end --while
802
    
803
    if b == protect then
804
     if doblacklist then
805
      options["blacklist"][
806
        #options["blacklist"]+1 ]
807
        = flex.getBlockDown()
808
      return down(n-x+1)
809
     else
810
      b = unbreak
811
     end --if
812
    end --if
813
    
814
    if b == unbreak then
815
     flex.send("#1"..b.."#0: {#8"..
816
       tostring(getx()).."#0,#8"..
817
       tostring(gety()-1).."#0,#8"..
818
       tostring(getz()).."#0}")
819
     knownBedrock[#knownBedrock+1] = {
820
       getx(), gety()-1, getz() }
821
     stuck = true
822
     stuckDir = "down"
823
     return false
824
    end --if
825
    if a then
826
     dugtotal = dugtotal + 1
827
    end --if
828
    if attack then turtle.attackDown() end
829
    
830
    if (os.time()-t)/24*20*60 > 20 then
831
     -- Stuck for at least 20 seconds
832
     -- Usually caused by height limits
833
     flex.send("Unable to move down",colors.orange)
834
     stuck = true
835
     stuckDir = "down"
836
     return false
837
    end --if
838
    
839
   end --while
840
   
841
  end --if can't move
842
  
843
  update("down")
844
 end --for
845
 
846
 return true
847
end --function
848
849
850
851
function fwd(n)
852
 n = n or 1
853
 if n < 0 then
854
  return back(-n)
855
 end --if
856
 
857
 local x,z,a,b,t
858
 for x=1, n do
859
  refuel()
860
  
861
  if not turtle.forward() then
862
   
863
   if doblacklist then
864
    -- Blocks not to mine
865
    if flex.isBlock(options["blacklist"]) then
866
     if flex.isBlockUp(options["blacklist"]) then
867
      if flex.isBlockDown(options["blacklist"]) then
868
       
869
       -- Blocks above, below and in front
870
       if not back() then return false end
871
       if not up(2) then return false end
872
       if not fwd(2) then return false end
873
       if not down(2) then return false end
874
       return true
875
       
876
      else -- Blocks in front and above
877
       if not down() then return false end
878
       x = 0
879
       while flex.isBlock(options["blacklist"]) do
880
        x = x + 1
881
        if not down() then return false end
882
       end --while
883
       if not fwd(2) then return false end
884
       if not up(x) then return false end
885
       return
886
      end --if/else
887
      
888
     else
889
      -- Block in front
890
      if not up() then return false end
891
      if not fwd(2) then return false end
892
      if not down() then return false end
893
      return true
894
     end --if/else
895
    end --if
896
   end --if doblacklist
897
   
898
   t = os.time()
899
   stuck = false
900
   while not turtle.forward() do
901
    a,b = turtle.dig()
902
    while a do
903
     dugtotal = dugtotal + 1
904
     a,b = turtle.dig()
905
    end --while
906
    
907
    if b == protect then
908
     if doblacklist then
909
      options["blacklist"][
910
          #options["blacklist"]+1]
911
          = flex.getBlock()
912
      return fwd(n-x+1)
913
     else
914
      b = unbreak
915
     end --if
916
    end --if
917
    
918
    if b == unbreak then
919
     z = { getx(), gety(),
920
           getz(), getr()%360 }
921
     
922
     if z[4] == 0 then
923
      z[3] = z[3] + 1
924
     elseif z[4] == 90 then
925
      z[1] = z[1] + 1
926
     elseif z[4] == 180 then
927
      z[3] = z[3] - 1
928
     elseif z[4] == 270 then
929
      z[1] = z[1] - 1
930
     end --if/else
931
     
932
     flex.send("#1"..b.."#0: {#8"..
933
       tostring(z[1]).."#0,#8"..
934
       tostring(z[2]).."#0,#8"..
935
       tostring(z[3]).."#0}")
936
     knownBedrock[#knownBedrock+1] =
937
       { z[1], z[2], z[3] }
938
     stuck = true
939
     stuckDir = "fwd"
940
     return false
941
    end --if
942
    
943
    if a then
944
     dugtotal = dugtotal + 1
945
    end --if
946
    if attack then turtle.attack() end
947
    
948
    --if (os.time()-t)/24*20*60 > 120 then
949
     -- Stuck for at least 20 seconds
950
     --flex.send("Unable to move forward",colors.orange)
951
     --stuck = true
952
     --stuckDir = "fwd"
953
     --return false
954
     --t = os.time()
955
    --end --if
956
    
957
   end --while not turtle.forward()
958
   
959
  end --if can't move
960
  
961
  update("fwd")
962
 end --if
963
 
964
 return true
965
end --function
966
967
968
969
function back(n)
970
 n = n or 1
971
 if n < 0 then
972
  return fwd(-n)
973
 end --if
974
 
975
 local x,turn
976
 turn = false
977
 for x=1,n do
978
  if turn then
979
   if not fwd() then return false end
980
  elseif turtle.back() then
981
   update("back")
982
  else
983
   turn = true
984
   gotor(rdist+180)
985
   if not fwd() then return false end
986
  end --if/else
987
 end --for
988
 
989
 if turn then gotor(rdist-180) end
990
 return true
991
end --function
992
993
994
995
function dig(x)
996
 local a,b
997
 x = x or "fwd"
998
 
999
 if x=="fwd" then
1000
  while turtle.dig() and not stuck do
1001
   dugtotal = dugtotal + 1
1002
  end --while
1003
  
1004
 elseif x=="up" then
1005
  while turtle.digUp() and not stuck do
1006
   dugtotal = dugtotal + 1
1007
  end --while
1008
  
1009
 elseif x=="down" then
1010
  while turtle.digDown() and not stuck do
1011
   dugtotal = dugtotal + 1
1012
  end --while
1013
  
1014
 end --if/else
1015
end --function
1016
1017
function digUp() dig("up") end
1018
function digDown() dig("down") end
1019
1020
1021
1022
-- Agressive placement functions
1023
1024
function place()
1025
 local x = flex.getBlock("fwd")
1026
 local t = os.time()
1027
 while not turtle.place()
1028
       and flex.isBlock(options["fluids"]) do
1029
  if attack then
1030
   turtle.attackUp()
1031
   turtle.attack()
1032
   turtle.attackDown()
1033
  end --if
1034
  
1035
  if (os.time()-t)/24*20*60 > 20 then
1036
   local z = { getx(), gety(),
1037
         getz(), getr()%360 }
1038
   if z[4] == 0 then
1039
    z[3] = z[3] + 1
1040
   elseif z[4] == 90 then
1041
    z[1] = z[1] + 1
1042
   elseif z[4] == 180 then
1043
    z[3] = z[3] - 1
1044
   elseif z[4] == 270 then
1045
    z[1] = z[1] - 1
1046
   end --if/else
1047
   
1048
   flex.send("#1End of world detected#0: {#8"..
1049
     tostring(z[1]).."#0,#8"..
1050
     tostring(z[2]).."#0,#8"..
1051
     tostring(z[3]).."#0}")
1052
   knownBedrock[#knownBedrock+1] =
1053
     { z[1], z[2], z[3] }
1054
   
1055
   stuck = true
1056
   stuckDir = "fwd"
1057
   return false
1058
  end --if
1059
  
1060
  sleep(0.1)
1061
  x = flex.getBlock()
1062
 end --while
1063
 if turtle.getSelectedSlot() == blockSlot then
1064
  checkBlocks()
1065
 end --if
1066
 return true
1067
end --function
1068
1069
1070
function placeUp()
1071
 local x = flex.getBlockUp()
1072
 local t = os.time()
1073
 while not turtle.placeUp()
1074
       and flex.isBlockUp(options["fluids"]) do
1075
  if attack then
1076
   turtle.attack()
1077
   turtle.attackUp()
1078
  end --if
1079
  
1080
  if (os.time()-t)/24*20*60 > 20 then
1081
   flex.send("#1Edge of world detected#0: {#8"..
1082
     tostring(getx()).."#0,#8"..
1083
     tostring(gety()+1).."#0,#8"..
1084
     tostring(getz()).."#0}")
1085
   knownBedrock[#knownBedrock+1] = {
1086
     getx(), gety()+1, getz() }
1087
   
1088
   stuck = true
1089
   stuckDir = "up"
1090
   return false
1091
  end --if
1092
  
1093
  sleep(0.1)
1094
  x = flex.getBlockUp()
1095
 end --while
1096
 if turtle.getSelectedSlot == blockSlot then
1097
  checkBlocks()
1098
 end --if
1099
 return true
1100
end --function
1101
1102
1103
function placeDown()
1104
 local x = flex.getBlockDown()
1105
 local t = os.time()
1106
 while not turtle.placeDown()
1107
       and flex.isBlockDown(options["fluids"]) do
1108
  if attack then
1109
   turtle.attack()
1110
   turtle.attackDown()
1111
  end --if
1112
  
1113
  if (os.time()-t)/24*20*60 > 20 then
1114
   flex.send("#1Edge of world detected#0: {#8"..
1115
     tostring(getx()).."#0,#8"..
1116
     tostring(gety()-1).."#0,#8"..
1117
     tostring(getz()).."#0}")
1118
   knownBedrock[#knownBedrock+1] = {
1119
     getx(), gety()-1, getz() }
1120
   up()
1121
   placeDown()
1122
   ymin = gety()
1123
   
1124
   stuck = true
1125
   stuckDir = "down"
1126
   return false
1127
  end --if
1128
  
1129
  sleep(0.1)
1130
  x = flex.getBlockDown()
1131
 end --while
1132
 if turtle.getSelectedSlot() == blockSlot then
1133
  checkBlocks()
1134
 end --if
1135
 return true
1136
end --function
1137
1138
1139
1140
1141
----------------------------
1142
--  /¯¯]  /¯\  [¯¯]  /¯\  --
1143
-- | [¯| | O |  ||  | O | --
1144
--  \__|  \_/   ||   \_/  --
1145
----------------------------
1146
1147
1148
function gotor(r)
1149
 if r == nil then
1150
  error("Number expected, got nil", 2)
1151
  return
1152
 end --if
1153
 local x = (r-rdist)%360
1154
 -- X is the target direction relative to current rotation
1155
 
1156
 if x == 90 then
1157
  right()
1158
 elseif x == 180 then
1159
  left(rlast*2)
1160
  -- Rotate opposite to last rotation
1161
 elseif x == 270 then
1162
  left()
1163
 elseif x ~= 0 then
1164
  error("Invalid rotation parameter", 2)
1165
  return false
1166
 end --if/else
1167
 
1168
 return true
1169
end --function
1170
1171
1172
1173
function gotoy(y)
1174
 if y == nil then
1175
  error("Number expected, got nil", 2)
1176
  return
1177
 end --if
1178
 
1179
 while ydist < y do
1180
  if not up() then return false end
1181
 end
1182
 while ydist > y do
1183
  if not down() then return false end
1184
 end
1185
 return true
1186
end
1187
1188
1189
function gotox(x)
1190
 if x == nil then
1191
  error("Number expected, got nil", 2)
1192
  return
1193
 end --if
1194
 
1195
 if xdist < x then
1196
  if rdist == 270 then
1197
   return back(x-xdist)
1198
   
1199
  else
1200
   gotor(90)
1201
   return fwd(x-xdist)
1202
  end
1203
 end
1204
 
1205
 if xdist > x then
1206
  if rdist == 90 then
1207
   return back(xdist-x)
1208
   
1209
  else
1210
   gotor(270)
1211
   return fwd(xdist-x)
1212
  end
1213
 end
1214
 
1215
 return true
1216
end
1217
1218
1219
function gotoz(z)
1220
 if z == nil then
1221
  error("Number expected, got nil", 2)
1222
  return
1223
 end --if
1224
 
1225
 if zdist < z then
1226
  if rdist == 180 then
1227
   return back(z-zdist)
1228
   
1229
  else
1230
   gotor(0)
1231
   return fwd(z-zdist)
1232
  end
1233
 end
1234
 
1235
 if zdist > z then
1236
  if rdist == 0 then
1237
   return back(zdist-z)
1238
   
1239
  else
1240
   gotor(180)
1241
   return fwd(zdist-z)
1242
  end
1243
 end
1244
 
1245
 return true
1246
end
1247
1248
1249
function goto(x,y,z,r,lm)
1250
 if type(x) == "table" then
1251
  if #x < 4 then
1252
   error("Invalid Goto Table",2)
1253
  end
1254
  y = x[2]
1255
  z = x[3]
1256
  r = x[4]
1257
  lm = x[15]
1258
  x = x[1]
1259
 end
1260
 gotox(x or 0)
1261
 gotoz(z or 0)
1262
 gotor(r or 0)
1263
 gotoy(y or 0)
1264
 if lm~=nil then setlast(lm) end
1265
end
1266
1267
1268
1269
1270
---------------------------------------
1271
-- |¯¯]|| |||¯¯]||    |¯\|¯\ /¯\     --
1272
-- | ] ||_||| ] ||_   | /| /| O | == --
1273
-- ||   \__||__]|__]  || | \ \_/     --
1274
---------------------------------------
1275
--   /¯]|¯¯]/¯¯\/¯¯\[¯¯]|\ || /¯¯]   --
1276
--  | [ | ] \_¯\\_¯\ ][ | \ || [¯|   --
1277
--   \_]|__]\__/\__/[__]|| \| \__|   --
1278
---------------------------------------
1279
1280
fuelSlot = {1,16}
1281
1282
function getFuelSlot()
1283
 return fuelSlot[1],fuelSlot[2]
1284
end --function
1285
1286
function setFuelSlot(a,b)
1287
 if a == nil then
1288
  return false
1289
 end --if
1290
 b = b or a
1291
 if a < b then
1292
  fuelSlot = {a,b}
1293
 else
1294
  fuelSlot = {b,a}
1295
 end --if/else
1296
 return true
1297
end --function
1298
1299
1300
1301
function refuel(b)
1302
 b = b or 1
1303
 b = math.min(b, turtle.getFuelLimit())
1304
 local a,x,z,slot
1305
 slot = turtle.getSelectedSlot()
1306
 a = true
1307
 
1308
 while turtle.getFuelLevel() < b do
1309
  for x=fuelSlot[1],fuelSlot[2] do
1310
   turtle.select(x)
1311
   if turtle.refuel(1) then break end
1312
   if x == fuelSlot[2] and a then
1313
    if fuelSlot[1] == fuelSlot[2] then
1314
	    flex.send("Waiting for fuel in "..
1315
	      "slot "..tostring(fuelSlot[1])..
1316
	      "...", colors.pink)
1317
   	else
1318
     flex.send("Waiting for fuel in "..
1319
       "slots "..tostring(fuelSlot[1])..
1320
       "-"..tostring(fuelSlot[2])..
1321
       "...", colors.pink)
1322
   	end --if/else
1323
    a = false
1324
   end --if
1325
  end --for
1326
 end --while
1327
 
1328
 if not a then
1329
  flex.send("Thanks!",colors.lime)
1330
 end --if
1331
 turtle.select(slot)
1332
 
1333
end --function
1334
1335
1336
1337
1338
local fuelvalue = {}
1339
local fuelfile = "dig_fuel.cfg"
1340
local file, line
1341
if fs.exists(fuelfile) then
1342
 file = fs.open(fuelfile, "r")
1343
 line = file.readLine()
1344
 while line ~= nil do
1345
  fuelvalue[line] = tonumber(file.readLine())
1346
  line = file.readLine()
1347
 end --while
1348
 file.close()
1349
else
1350
 file = fs.open(fuelfile, "w")
1351
 file.close()
1352
end --if/else
1353
1354
1355
function checkFuelValue(x)
1356
 if type(x) ~= "number" then
1357
  local x = turtle.getSelectedSlot()
1358
 end --if
1359
 if turtle.getItemCount(x) == 0 then
1360
  return 0
1361
 end --if
1362
 
1363
 local a,b,c,d
1364
 a = turtle.getItemDetail(x)["name"]
1365
 b = fuelvalue[a]
1366
 
1367
 if b == nil then
1368
  c = turtle.getFuelLevel()
1369
  turtle.select(x)
1370
  turtle.refuel(1)
1371
  d = turtle.getFuelLevel() - c
1372
  fuelvalue[a] = d
1373
  
1374
  file = fs.open(fuelfile, "a")
1375
  file.writeLine(a)
1376
  file.writeLine(tostring(d))
1377
  file.close()
1378
  
1379
  if turtle.getItemCount(x) == 0 then
1380
   return 0
1381
  else
1382
   return d
1383
  end --if
1384
 end --if
1385
 
1386
 return b
1387
end --function
1388
1389
1390
1391
1392
blockSlot = 0
1393
blockStacks = 1
1394
function getBlockSlot() return blockSlot end
1395
function setBlockSlot(n) blockSlot = n end
1396
function getBlockStacks() return blockStacks end
1397
function setBlockStacks(n) blockStacks = n end
1398
1399
1400
function checkBlocks()
1401
 if blockSlot == 0 then return end
1402
 local x,docondense
1403
 
1404
 if not flex.isItem(options["buildingblocks"],blockSlot) then
1405
  if turtle.getItemCount(blockSlot) > 0 then
1406
   for x=blockSlot+1,16 do
1407
    if turtle.getItemCount(x) == 0 then
1408
     turtle.select(blockSlot)
1409
     turtle.transferTo(x)
1410
     break
1411
    end --if
1412
   end --for
1413
  end --if
1414
 end --if
1415
 
1416
 if turtle.getItemCount(blockSlot) == 0 then
1417
  for x=1,16 do
1418
   if flex.isItem(options["buildingblocks"],x) then
1419
    turtle.select(x)
1420
    turtle.transferTo(blockSlot)
1421
    turtle.select(blockSlot)
1422
   	break
1423
   end --if
1424
  end --for
1425
 flex.condense(blockSlot)
1426
 end --if
1427
 
1428
 turtle.select(blockSlot)
1429
end --function
1430
1431
1432
1433
function blockLava(dir)
1434
 if not flex.isBlock("lava",dir) then
1435
  return
1436
 end --if
1437
 local slot = turtle.getSelectedSlot()
1438
 checkBlocks()
1439
 turtle.select(blockSlot)
1440
 if dir == "up" then
1441
  turtle.placeUp()
1442
 elseif dir == "down" then
1443
  turtle.placeDown()
1444
 else
1445
  turtle.place()
1446
 end --if/else
1447
 turtle.select(slot)
1448
end --function
1449
1450
function blockLavaUp() blockLava("up") end
1451
function blockLavaDown() blockLava("down") end
1452
1453
1454
1455
1456
-- Organize Inventory/Fuel
1457
--   C
1458
--  CTQQQ
1459
--   QQQQ
1460
-- (Chest/Turtle/Quarry)
1461
1462
function dropNotFuel()
1463
 
1464
 local slot = turtle.getSelectedSlot()
1465
 local a,b,c,f,x,y,z
1466
 local crafty,usedbucketalready,blocksPresent
1467
 turtle.select(1)
1468
 if turtle.getItemCount(1) > 1 then
1469
  flex.condense()
1470
 end --if
1471
 
1472
 -- Check there's inventory to place loot
1473
 a = true
1474
 while true do
1475
  if isChest() then
1476
   break
1477
  end
1478
  if a then
1479
   flex.send("Output inventory not found!",
1480
     colors.red)
1481
   a = false
1482
  end --if
1483
  sleep(1)
1484
 end --while
1485
 if not a then
1486
  flex.send("Thanks!",colors.lightBlue)
1487
 end --if
1488
 
1489
 
1490
 -- Drop off what's not fuel
1491
 blocksPresent = blockStacks
1492
 for x=1,16 do
1493
  turtle.select(x)
1494
  if not turtle.refuel(0) then
1495
   if flex.isItem(options["buildingblocks"]) then
1496
    if blocksPresent <= 0 then
1497
     turtle.drop()
1498
    else
1499
     blocksPresent = blocksPresent - 1
1500
    end --if/else
1501
   else
1502
    turtle.drop()
1503
   end --if/else
1504
  end --if
1505
 end --for
1506
 checkBlocks()
1507
 turtle.select(1)
1508
 -- Retrieve surplus fuel from fuel chest
1509
 while turtle.suckUp() do sleep(0) end
1510
 if turtle.getItemCount(fuelSlot[1]) > 0 then
1511
  flex.condense()
1512
 end --if
1513
 
1514
 
1515
 -- Craft coal into blocks
1516
 if peripheral.find("workbench") and isChestUp() then
1517
  
1518
  z = 0
1519
  for x=1,16 do
1520
   if turtle.getItemCount(x) > 0 and
1521
      turtle.getItemDetail(x)["name"]
1522
      ~= "minecraft:coal" then
1523
    turtle.select(x)
1524
    turtle.dropUp()
1525
   end --if
1526
   z = z + turtle.getItemCount(x)
1527
  end --for
1528
  
1529
  if z >= 9 then
1530
   
1531
   y = 13
1532
   for x=1,12 do
1533
    turtle.select(x)
1534
    while turtle.getItemCount(x) > 0 do
1535
     if y <= 16 then
1536
      turtle.transferTo(y)
1537
     else
1538
      z = z - turtle.getItemCount(x)
1539
      turtle.dropUp()
1540
     end --if/else
1541
     if turtle.getItemCount(x) > 0 then
1542
      y = y + 1
1543
     end --if
1544
    end --while
1545
   end --for
1546
   
1547
   y = math.floor(z/9)
1548
   turtle.select(13)
1549
   turtle.dropUp(z%9)
1550
   a = 13
1551
   b = { 1, 2, 3, 5, 6, 7, 9, 10, 11 }
1552
   for x=1,#b do
1553
    if turtle.getItemCount(a) < y then
1554
     turtle.select(a+1)
1555
     turtle.transferTo(a,
1556
       y - turtle.getItemCount(a))
1557
    end --if
1558
    turtle.select(a)
1559
    turtle.transferTo(b[x], y)
1560
    if turtle.getItemCount(a) == 0 then
1561
     a = a + 1
1562
    end --if
1563
   end --for
1564
   
1565
   if peripheral.getType("left") == "workbench" then
1566
    crafty = peripheral.wrap("left")
1567
   else
1568
    crafty = peripheral.wrap("right")
1569
   end --if/else
1570
   turtle.select(1)
1571
   crafty.craft()
1572
   
1573
  end --if
1574
  while turtle.suckUp() do sleep(0) end
1575
  checkBlocks()
1576
  
1577
 end --if
1578
 
1579
 
1580
 --Tally up fuel sources
1581
 z = {} -- slot #
1582
 f = {} -- amount of fuel
1583
 usedbucketalready = false
1584
 
1585
 for x=1,16 do
1586
  turtle.select(x)
1587
  
1588
  if turtle.refuel(0) then
1589
   y = turtle.getItemCount()
1590
   
1591
   if flex.isItem("bucket") and y==1 then
1592
    if not usedbucketalready then
1593
     -- Only use buckets one at a time
1594
     turtle.refuel()
1595
     usedbucketalready = true
1596
     turtle.drop()
1597
    end
1598
    
1599
   else -- Not a bucket
1600
    a = checkFuelValue(x)
1601
    if a > 0 then
1602
     z[#z+1] = x
1603
     f[#z] = a*turtle.getItemCount(x)
1604
    end --if
1605
    
1606
   end --if/else (is bucket)
1607
   
1608
  end --if (is fuel)
1609
 end --for (slots)
1610
 
1611
 
1612
 -- Choose best fuel available
1613
 a = 0
1614
 for x=1, #f do
1615
  a = math.max(a,f[x])
1616
 end --for
1617
 for x=1, #f do
1618
  if f[x] == a then
1619
   if z[x] > 1 then
1620
    for b=2,16 do
1621
     if turtle.getItemCount(b) == 0 then
1622
      turtle.select(1)
1623
      turtle.transferTo(b)
1624
      break
1625
     end --if
1626
    end --for
1627
    turtle.select(z[x])
1628
    turtle.transferTo(1)
1629
   end --if
1630
   break
1631
  end --if
1632
 end --for
1633
 
1634
 
1635
 -- Deposit surplus fuel
1636
 for x=fuelSlot[1]+1,16 do
1637
  if turtle.getItemCount(x) > 0 then
1638
   
1639
   turtle.select(x)
1640
   if turtle.refuel(0) then
1641
    if isChestUp() then
1642
     -- Place in fuel chest
1643
     turtle.dropUp()
1644
    end --if
1645
    if turtle.getItemCount() > 0 then
1646
     -- Fuel chest is full or absent
1647
     turtle.drop()
1648
    end --if
1649
   end --if
1650
   
1651
  end --if (more than zero items)
1652
 end --for (all slots after fuel slot)
1653
 
1654
 turtle.select(fuelSlot[1])
1655
 if not turtle.refuel(0) then
1656
  for x=fuelSlot[1]+1,16 do
1657
   turtle.transferTo(x)
1658
   if turtle.getItemCount() == 0 then
1659
    break
1660
   end --if
1661
  end --for
1662
 end --if
1663
 
1664
 flex.condense(fuelSlot[1]+1)
1665
 
1666
 if turtle.getItemCount(16) > 0 then
1667
  flex.send("Inventory full!",colors.red)
1668
  turtle.select(16)
1669
  while not turtle.drop() do sleep(5) end
1670
  flex.send("Inventory has room!",colors.lightBlue)
1671
  turtle.select(slot)
1672
  return dropNotFuel()
1673
 end --if
1674
 
1675
 turtle.select(slot)
1676
 
1677
end --function dropNotFuel()
1678
1679
1680