View difference between Paste ID: JvGp5Ts7 and Ndgnay68
SHOW: | | - or go back to the newest paste.
1
-- Cloned
2
-- Digs a staircase around a quarry
3
-- Run "stairs help"
4
-- Or dig a staircase to bedrock
5
-- Run "stairs"
6
-- <[email protected]>
7
-- Please email me if you have any
8
-- bugs or suggestions!
9
10
-----------------------------------
11
--  /¯\  || ||  /\  |¯\ |¯\ \\// --
12
-- | O | ||_|| |  | | / | /  \/  --
13
--  \_\\  \__| |||| | \ | \  ||  --
14
-----------------------------------
15
-- /¯¯\ [¯¯]  /\  [¯¯] |¯\ /¯¯\  --
16
-- \_¯\  ||  |  |  ][  | / \_¯\  --
17
-- \__/  ||  |||| [__] | \ \__/  --
18
-----------------------------------
19
20
-- Names of tools
21
local name_torch = {
22
   "torch", "lantern", "lamp", "light" }
23
local name_bench = {
24
   "minecraft:crafting_table",
25
   "forge:workbench" }
26
local name_chest = { "chest" }
27
local name_box = {
28
   "shulker_box", "travelersbackpack" }
29
30
-- Stair blocks crafting material
31
local name_cobble = {
32
  "minecraft:cobblestone",
33
  "forge:cobblestone" }
34
35
36
-- Side that swaps with crafting bench
37
local tool_side = "none"
38
if not peripheral.find("workbench") then
39
 tool_side = "left"
40
 if peripheral.getType("left") == "modem" then
41
  tool_side = "right"
42
 end --if
43
end --if
44
45
46
-- Load APIs
47
os.loadAPI("flex.lua")
48
os.loadAPI("dig.lua")
49
dig.setFuelSlot(1)
50
dig.setBlockSlot(2)
51
dig.setBlockStacks(4)
52
53
54
function dump()
55
 local slot = turtle.getSelectedSlot()
56
 local keepers = { name_cobble, name_box,
57
    name_torch, name_bench, name_chest,
58
    "stairs" }
59
 local x,a = 0,false
60
 
61
 for x=1,16 do
62
  if flex.isItem(name_box,x) then
63
   turtle.select(x)
64
   a = turtle.placeUp()
65
   break
66
  end --if
67
 end --for
68
 
69
 if not a then
70
  keepers[#keepers+1] = "diamond"
71
  keepers[#keepers+1] = "ancient_debris"
72
 end --if
73
 
74
 local blocksPresent = dig.getBlockStacks()
75
 for x=4,16 do
76
  if not flex.isItem(keepers,x) then
77
   if dig.isDumpItem(x) then
78
    if blocksPresent <= 0 then
79
     turtle.drop()
80
    else
81
     blocksPresent = blocksPresent - 1
82
    end --if/else
83
   else
84
    turtle.select(x)
85
    if a then
86
     turtle.dropUp()
87
    else
88
     turtle.drop()
89
    end --if/else
90
   end --if/else
91
  end --if
92
 end --for
93
 
94
 turtle.select(slot)
95
 if a then turtle.digUp() end
96
 dig.checkBlocks()
97
 flex.condense()
98
end --function
99
100
101
102
-- Program parameter(s)
103
local args={...}
104
105
-- Tutorial, kind of
106
if #args > 0 and args[1] == "help" then
107
 flex.printColors("Place just to the "..
108
   "left of a turtle quarrying the same "..
109
   "dimensions.",colors.lightBlue)
110
 flex.printColors("Include a crafting "..
111
   "table and a chest in turtle's "..
112
   "inventory to auto-craft a staircase",
113
   colors.yellow)
114
 flex.printColors("Usage: stairs "..
115
   "[length] [width] [depth]",colors.pink)
116
 return
117
end --if
118
119
120
-- What Goes Where
121
flex.printColors("Slot 1: Fuel\n"..
122
  "Slot 2: Blocks\nSlot 3: Torches\n"..
123
  "Anywhere: Crafting Bench, Chest\n"..
124
  "Optional: Shulker Box / Backpack",
125
  colors.lightBlue)
126
flex.printColors("Press Enter",
127
  colors.pink)
128
while flex.getKey() ~= keys.enter do
129
 sleep(0.1)
130
end --while
131
132
133
-- Convert Inputs
134
local dx,dy,dz,n,x,y,z
135
local height = 5
136
dz = tonumber(args[1]) or 256
137
dx = tonumber(args[2]) or dz
138
dy = tonumber(args[3]) or 256
139
-- -1 to match Quarry depth
140
141
142
--------------------------------------
143
-- |¯\ [¯¯] /¯¯] /¯¯][¯¯]|\ || /¯¯] --
144
-- |  | ][ | [¯|| [¯| ][ | \ || [¯| --
145
-- |_/ [__] \__| \__|[__]|| \| \__| --
146
--------------------------------------
147
148
flex.send("Digging staircase...",
149
  colors.yellow)
150
151
-- Staircase Digging Functions
152
153
local torchNum = 9
154
155
function placeTorch()
156
 turtle.select(3)
157
 if flex.isItem(name_torch) then
158
  
159
  if not turtle.place() then
160
   if not dig.fwd() then return false end
161
   turtle.select(2)
162
   dig.place()
163
   if not dig.back() then return false end
164
   
165
   turtle.select(3)
166
   if not dig.place() then
167
    if not dig.fwd() then return false end
168
    turtle.select(2)
169
    dig.placeUp()
170
    if not dig.back() then return false end
171
    turtle.select(3)
172
    dig.place()
173
   end --if/else
174
  end --if
175
 end --if
176
 
177
 turtle.select(2)
178
end --function
179
180
181
function stepDown()
182
 local x
183
 
184
 turtle.select(2)
185
 dig.right()
186
 for x=1,height-2 do
187
  dig.blockLava()
188
  if not dig.up() then return false end
189
 end --for
190
 dig.blockLava()
191
 dig.blockLavaUp()
192
 
193
 dig.left()
194
 dig.blockLava()
195
 dig.left()
196
 if not dig.fwd() then return false end
197
 dig.blockLavaUp()
198
 dig.blockLava()
199
 dig.right()
200
 dig.blockLava()
201
 dig.left()
202
 
203
 if torchNum >= 3 then
204
  if not dig.back() then return false end
205
  placeTorch()
206
  if not dig.down() then return false end
207
  if not dig.fwd() then return false end
208
  torchNum = 0
209
 else
210
  dig.blockLava()
211
  if not dig.down() then return false end
212
  torchNum = torchNum + 1
213
 end --if/else
214
 
215
 for x=1,height-2 do
216
  dig.blockLava()
217
  if not dig.down() then return false end
218
 end --for
219
 dig.blockLava()
220
 if not dig.placeDown() then return false end
221
 
222
 dig.right(2)
223
 if not dig.fwd() then return false end
224
 dig.blockLava()
225
 if not dig.placeDown() then return false end
226
 dig.left()
227
 
228
 if turtle.getItemCount(16) > 0 then
229
  dig.left()
230
  dump()
231
  dig.right()
232
 end --if/else
233
 
234
 if not dig.fwd() then return false end
235
 
236
 return true
237
end --function
238
239
240
local function turnRight()
241
 turtle.select(2)
242
 dig.right()
243
 if not dig.up(height-2) then return false end
244
 dig.blockLavaUp()
245
 
246
 dig.left()
247
 if not dig.down() then return false end
248
 if not dig.fwd() then return false end
249
 dig.blockLavaUp()
250
 for x=1,height-3 do
251
  dig.blockLava()
252
  if not dig.down() then return false end
253
 end --for
254
 dig.blockLava()
255
 if not dig.placeDown() then return false end
256
 
257
 dig.left()
258
 if not dig.fwd() then return false end
259
 for x=1,height-3 do
260
  dig.blockLava()
261
  if not dig.up() then return false end
262
 end --for
263
 dig.blockLava()
264
 dig.blockLavaUp()
265
 
266
 dig.right()
267
 for x=1,height-3 do
268
  dig.blockLava()
269
  if not dig.down() then return false end
270
 end --for
271
 dig.blockLava()
272
 if not dig.placeDown() then return false end
273
 
274
 dig.left(2)
275
 if not dig.fwd() then return false end
276
 dig.right()
277
 if not dig.placeDown() then return false end
278
 for x=1,height-2 do
279
  dig.blockLava()
280
  if not dig.up() then return false end
281
 end --for
282
 dig.blockLava()
283
 dig.blockLavaUp()
284
 
285
 dig.right(2)
286
 if not dig.fwd() then return false end
287
 if not dig.down(height-1) then return false end
288
 if not dig.placeDown() then return false end
289
 dig.left()
290
 if not dig.fwd() then return false end
291
 dig.blockLava()
292
 if not dig.placeDown() then return false end
293
 if not dig.back() then return false end
294
 dig.right()
295
 if not dig.fwd() then return false end
296
 
297
 torchNum = torchNum + 1
298
 return true
299
end --function
300
301
302
function endcap(h,stop)
303
 stop = ( stop ~= nil )
304
 h = h or 0 -- Height to dig layer
305
 local x
306
 
307
 dig.right()
308
 if not dig.placeDown() then return false end
309
 dig.checkBlocks()
310
 for x=1,height-2-h do
311
  dig.blockLava()
312
  if not dig.up() then return false end
313
 end --for
314
 dig.blockLava()
315
 dig.blockLavaUp()
316
 
317
 dig.left(2)
318
 if not dig.fwd() then return false end
319
 dig.blockLavaUp()
320
 for x=1,height-2-h do
321
  dig.blockLava()
322
  if not dig.down() then return false end
323
 end --for
324
 dig.blockLava()
325
 if not dig.placeDown() then return false end
326
 dig.checkBlocks()
327
 if not dig.back() then return false end
328
 
329
 dig.right()
330
 
331
 if stop then
332
  dig.blockLava()
333
  for x=1,height-2-h do
334
   if not dig.up() then return false end
335
   dig.blockLava()
336
  end --for
337
  dig.blockLavaUp()
338
  dig.left()
339
  
340
  if not dig.fwd() then return false end
341
  dig.blockLavaUp()
342
  dig.right()
343
  dig.blockLava()
344
  for x=1,height-2-h do
345
   if not dig.down() then return false end
346
   dig.blockLava()
347
  end --for
348
  
349
  dig.left()
350
  if not dig.back() then return false end
351
  dig.right()
352
  
353
 end --if
354
 
355
 return true
356
end --function
357
358
359
360
local direction
361
362
function avoidBedrock()
363
 if dig.isStuck() then
364
  -- Hit Bedrock/Void
365
  if dig.getStuckDir() == "fwd" then
366
   dig.up()
367
   dig.placeDown()
368
   dig.checkBlocks()
369
   dig.setymin(dig.gety())
370
   dig.fwd()
371
  elseif dig.getStuckDir() == "down" then
372
   dig.setymin(dig.gety())
373
  end --if
374
 end --if
375
 
376
 -- Get X and Z on the inner stair block
377
 if dig.getx() >= dx+2 then
378
  dig.gotox(dx+1)
379
  
380
 elseif dig.getx() <= -1 then
381
  dig.gotox(0)
382
  
383
 end --if/else
384
 
385
 if dig.getz() >= dz+1 then
386
  dig.gotoz(dz)
387
  
388
 elseif dig.getz() <= -2 then
389
  dig.gotoz(-1)
390
  
391
 end --if/else
392
 
393
 dig.gotor(direction)
394
 dig.gotoy(dig.getymin())
395
end --function
396
397
398
399
-- Start Digging
400
401
turtle.select(2)
402
403
x = 0
404
direction = dig.getr()
405
while true do
406
 
407
 for n=0,dz-1 do
408
  if not stepDown() then break end
409
  x = x + 1
410
  if x >= dy then break end
411
 end
412
 if dig.isStuck() or x >= dy then break end
413
 if not turnRight() then break end
414
 x = x + 1
415
 
416
 direction = dig.getr()
417
 for n=0,dx-1 do
418
  if not stepDown() then break end
419
  x = x + 1
420
  if x >= dy then break end
421
 end
422
 if dig.isStuck() or x >= dy then break end
423
 if not turnRight() then break end
424
 x = x + 1
425
 
426
 direction = dig.getr()
427
end
428
429
avoidBedrock()
430
if not dig.fwd() then avoidBedrock() end
431
if not endcap(1) then avoidBedrock() end
432
if not dig.fwd() then avoidBedrock() end
433
if not endcap(1,true) then avoidBedrock() end
434
435
dig.left(2)
436
while not turtle.detect() do
437
 dig.fwd()
438
end --while
439
dig.back()
440
441
-- This bit compensates for random Bedrock (mostly)
442
if #dig.getKnownBedrock() > 0 then
443
 for x=1,4 do
444
  dig.placeDown()
445
  dig.right()
446
  dig.fwd()
447
 end --for
448
end --for
449
450
451
452
----------------------------------------------
453
--  /¯] |¯\  /\  |¯¯] [¯¯] [¯¯] |\ ||  /¯¯] --
454
-- | [  | / |  | | ]   ||   ][  | \ | | [¯| --
455
--  \_] | \ |||| ||    ||  [__] || \|  \__| --
456
----------------------------------------------
457
458
-- Return locations of bench/chest
459
local function checkTools()
460
 local bench,chest = 0,0
461
 local x
462
 for x=1,16 do
463
  turtle.select(x)
464
  if flex.isItem(name_bench) then
465
   bench = x
466
  elseif flex.isItem(name_chest) then
467
   chest = x
468
  end --if/else
469
 end --for
470
 return bench,chest
471
end --function
472
473
474
local oldTool
475
local success = true
476
477
local function equip()
478
 if tool_side == "right" then
479
  return turtle.equipRight()
480
 elseif tool_side == "left" then
481
  return turtle.equipLeft()
482
 end --if/else
483
end --function
484
485
486
-- Equip Crafting Bench
487
local function setTool()
488
 if tool_side == "none" then return end
489
 
490
 flex.condense()
491
 local x,y = checkTools()
492
 
493
 if x == 0 then
494
  flex.send("Crafting Bench not found",
495
    colors.red)
496
  success = false
497
  return false
498
 end --if
499
 
500
 turtle.select(x)
501
 y = turtle.getItemCount()
502
 if y > 1 then
503
  turtle.transferTo(math.min(
504
    x+1,16),y-1)
505
 end --if
506
 
507
 if not equip() then
508
  return false
509
 end --if
510
 
511
 if turtle.getItemCount() > 0 then
512
  oldTool = turtle.getItemDetail()["name"]
513
 end --if
514
 
515
 flex.send("Crafting Bench equipped",
516
   colors.yellow)
517
 return true
518
end --function setTool()
519
520
521
-- Unequip Crafting Bench
522
local function restoreTool()
523
 if tool_side == "none" then return end
524
 
525
 flex.condense()
526
 local slot = turtle.getSelectedSlot()
527
 local x,y
528
 for x=1,16 do
529
  turtle.select(x)
530
  y = turtle.getItemCount()
531
  
532
  if oldTool == nil then
533
   -- If no tool, put Bench in empty slot
534
   if y == 0 then
535
    return equip()
536
   end --if
537
   
538
  else
539
   if y > 0 then
540
    if turtle.getItemDetail()["name"]
541
       == oldTool then
542
     if equip() then
543
	     flex.send("Tool restored",
544
	       colors.lightBlue)
545
	     turtle.select(slot)
546
      return true
547
     end --if
548
    end --if
549
   end --if
550
  end --if
551
 end --for
552
 
553
 flex.send("Unable to restore tool",
554
   colors.red)
555
 success = false
556
 return false
557
end --function restoreTool()
558
559
560
local depth = -dig.gety()
561
local bench, chest = checkTools()
562
local stairsNeeded = depth*2
563
local craftNum
564
565
-- Count existing stair blocks
566
local numStairs = 0
567
for x=1,16 do
568
 turtle.select(x)
569
 y = turtle.getItemCount()
570
 if y > 0 then
571
  if flex.isItem("stairs") then
572
   numStairs = numStairs + y
573
  end --if
574
 end --if
575
end --for
576
577
-- Count Cobblestone
578
local numCobble = 0
579
for x=1,16 do
580
 turtle.select(x)
581
 if flex.isItem(name_cobble) then
582
  numCobble = numCobble + turtle.getItemCount()
583
 end --if
584
end --for
585
turtle.select(1)
586
587
craftNum = math.ceil((stairsNeeded-numStairs)/4)
588
589
590
-- Check against cobble needed
591
if numCobble < craftNum*6
592
   or stairsNeeded > 64*4 then
593
 
594
 x = math.floor(numCobble/6)
595
 x = math.min(x,64)
596
 y = math.ceil(stairsNeeded/4)
597
 z = math.floor(100*x/y)
598
 
599
 flex.send("#1Only enough cobblestone "
600
   .."to craft #4"..tostring(z)
601
   .."#0%#1 of stairs")
602
 success = false
603
 
604
 craftNum = x
605
end --if
606
607
if craftNum < 0 then
608
 craftNum = 0
609
end --if
610
611
612
613
-- If Crafting needs to (and can) happen
614
if craftNum > 0 and chest > 0 and
615
   ( bench > 0 or tool_side == "none" ) then
616
 
617
 local stairSlots = {1,5,6,9,10,11}
618
 local freeSlots = {2,3,4,7,8,12,13,14,15,16}
619
 
620
 -- Equip Crafing Banch and place Chest
621
 setTool()
622
 turtle.select(chest)
623
 turtle.place()
624
 
625
 -- Everything except Cobble into Chest
626
 for x=1,16 do
627
  turtle.select(x)
628
  if turtle.getItemCount() > 0 then
629
   if not flex.isItem(name_cobble)
630
      or flex.isItem("stairs") then
631
    turtle.drop()
632
   end --if
633
  end --if
634
 end --for
635
 flex.condense()
636
 
637
 -- Collect Cobble to Craft
638
 for x=1,11 do
639
  turtle.select(x)
640
  if x <= 5 then
641
   turtle.transferTo(x+11)
642
  elseif x == 6 then
643
   turtle.transferTo(4)
644
  elseif x == 7 then
645
   turtle.transferTo(8)
646
  else
647
   turtle.drop()
648
  end --if/else
649
  if turtle.getItemCount() > 0 then
650
   turtle.drop()
651
  end --if
652
 end --for
653
 
654
 -- Arrange Cobble into Recipe
655
 z = 16
656
 for x=1,#stairSlots do
657
  turtle.select(z)
658
  while turtle.getItemCount() < craftNum do
659
   if z > 12 then
660
    z = z-1
661
   else
662
    z = z-4
663
   end --if
664
   if z < 1 then break end
665
   turtle.select(z)
666
  end --while
667
  if z < 1 then break end
668
  turtle.select(z)
669
  turtle.transferTo(stairSlots[x],
670
    craftNum)
671
 end --for
672
 
673
 -- Drop excess cobble into chest
674
 for x=1,#freeSlots do
675
  turtle.select(freeSlots[x])
676
  turtle.drop()
677
 end --for
678
 
679
 -- Main Event! Craft Function! =D
680
 local cb = peripheral.wrap("left") or
681
            peripheral.wrap("right")
682
 if cb.craft(craftNum) then
683
  flex.send("Stairs crafted",colors.lightBlue)
684
 else
685
  flex.send("Crafting error",colors.red)
686
  success = false
687
 end --if
688
 
689
 -- Restore inventory in correct order
690
 for x=1,16 do
691
  turtle.select(x)
692
  turtle.drop()
693
 end --for
694
 turtle.select(1)
695
 while turtle.suck() do end
696
 restoreTool()
697
 turtle.dig()
698
 flex.condense()
699
 
700
end --if (crafting needed)
701
702
703
704
705
706
-----------------------------------------------
707
-- |¯\ || || [¯¯] ||   |¯\  [¯¯] |\ ||  /¯¯] --
708
-- | < ||_||  ][  ||_  |  |  ][  | \ | | [¯| --
709
-- |_/  \__| [__] |__] |_/  [__] || \|  \__| --
710
-----------------------------------------------
711
712
713
local function placeStairs()
714
 local x,y,z,slot
715
 slot = turtle.getSelectedSlot()
716
 y = turtle.getItemCount()
717
 z = true
718
 
719
 if y < 2 or not flex.isItem("stairs") then
720
  for x=1,16 do
721
   turtle.select(x)
722
   y = turtle.getItemCount()
723
   if y >= 2 and flex.isItem("stairs") then
724
    z = false
725
    break
726
   end --if
727
  end --for
728
  
729
  if z then
730
   turtle.select(slot)
731
   return false
732
  end --if
733
 end --if
734
 
735
 dig.placeDown()
736
 dig.right()
737
 dig.fwd()
738
 dig.left()
739
 dig.placeDown()
740
 dig.left()
741
 dig.fwd()
742
 dig.right()
743
end --function
744
745
746
flex.send("Returning to surface",
747
  colors.yellow)
748
749
function isDone()
750
 -- Reached Surface
751
 return dig.gety() >= 0
752
end
753
754
-- Follow the Spiral [and place Stairs]
755
while not isDone() do
756
757
 if dig.getr()%360 == 0 then
758
  while dig.getz() < dig.getzmax()-1 do
759
   dig.fwd()
760
   dig.up()
761
   placeStairs()
762
   if isDone() then break end
763
  end --while
764
  
765
 elseif dig.getr()%360 == 90 then
766
  while dig.getx() < dig.getxmax()-1 do
767
   dig.fwd()
768
   dig.up()
769
   placeStairs()
770
   if isDone() then break end
771
  end --while
772
  
773
 elseif dig.getr()%360 == 180 then
774
  while dig.getz() > dig.getzmin()+1 do
775
   dig.fwd()
776
   dig.up()
777
   placeStairs()
778
   if dig.gety() > -4 and dig.getz()
779
      == dig.getzmin()+1 then
780
    -- Up at the top
781
    dig.fwd()
782
    dig.up()
783
    placeStairs()
784
   end --if
785
   if isDone() then break end
786
  end --while
787
  
788
 elseif dig.getr()%360 == 270 then
789
  while dig.getx() > dig.getxmin()+1 do
790
   dig.fwd()
791
   dig.up()
792
   placeStairs()
793
   if isDone() then break end
794
  end --while
795
  
796
 end --if/else
797
 
798
 if not isDone() then dig.left() end
799
 
800
end --while
801
802
803
-- All Done!
804
turtle.select(1)
805
dig.goto(0,0,0,0)
806
807
if success then
808
 flex.send("Stairway finished!",
809
   colors.lightBlue)
810
else
811
 flex.send("Reached Origin",
812
   colors.lightBlue)
813
end --if
814
815
flex.modemOff()
816
os.unloadAPI("dig.lua")
817
os.unloadAPI("flex.lua")
818