-
Notifications
You must be signed in to change notification settings - Fork 3
/
magicTurtle.cc.lua
879 lines (798 loc) · 17.5 KB
/
magicTurtle.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
local c=false
local x, y, z --starting pos
local face=2 --make sure that the turtle's pointing north
local autoupdate=false --make this true if you want to autoupdate everytime
local version=1.31
local updateID=11 --ID of terminal sending updates/commands
local armingID=11 --ID of terminal arming the mine
local disarmingID=11 --ID of disarming terminal
function version()
return version
end
function setMineID(arming, disarming)
armingID=arming
disarmingID=disarming
end
function setUpdateID(a)
updateID=a
end
function savePos()
if not fs.isDir("/pos") then fs.makeDir("/pos") end
local f1=io.open("/pos/posx", "w")
local f2=io.open("/pos/posy", "w")
local f3=io.open("/pos/posz", "w")
local f4=io.open("/pos/face", "w")
f1:write(x)
f2:write(y)
f3:write(z)
f4:write(face)
f1:close()
f2:close()
f3:close()
f4:close()
return true
end
function getPosF()
local f1=io.open("pos/posx", "r")
local f2=io.open("pos/posy", "r")
local f3=io.open("pos/posz", "r")
local f4=io.open("pos/face", "r")
x=f1:read()
y=f2:read()
z=f3:read()
face=f4:read()
f1:close()
f2:close()
f3:close()
f4:close()
end
function getPos()
return x,y,z
end
function setNorth()
face=2
end
function setPos(a, b, c, ...)
x=a
y=b
z=c
if ...~=nil then face=... end
savePos()
return true
end
function faceAdd(a)
if face+a>3 then face=face+a-4 else face=face+a end
end
function faceSub(a)
if face-a<0 then face=4-face-a else face=face-a end
end
function faceSouth()
while face~=0 do turnLeft() end
end
function faceEast()
while face~=1 do turnLeft() end
end
function faceNorth()
while face~=2 do turnLeft() end
end
function faceWest()
while face~=3 do turnLeft() end
end
function faceSet(a)
while face~=a do turnLeft() sleep(0.2) end
end
function turnLeft()
turtle.turnLeft()
faceAdd(1)
return true
end
function turnRight()
turtle.turnRight()
faceSub(1)
end
function rotate(angle)
if angle>0 then
for i=1, angle, 1 do
turnLeft()
end
else
angle=math.abs(angle)
for i=1, angle, 1 do
turnRight()
end
end
end
function getPos()
return x, y, z
end
function getFacing()
return face
end
function checkFace()
print("Checking Heading and Position.")
faceNorth()
print("I should be facing North Now!")
return true
end
function checkPos()
print("Make sure I have room to move around me")
print("I am now at ",x,y,z)
faceNorth()
forward(1)
print("I should have moved 1 space North")
print("I am now at ",x,y,z)
return true
end
function up()
if y~=127 then
local state=turtle.up()
if state then y=y+1 end
return state
else return false
end
end
function down()
if y~=1 then
local state=turtle.down()
if state then y=y-1 end
return state
else return false
end
end
function forward(a)
local state=true
if face==0 then for i=1, a, 1 do state=turtle.forward() if state then z=z+1 end end end
if face==1 then for i=1, a, 1 do state=turtle.forward() if state then x=x+1 end end end
if face==2 then for i=1, a, 1 do state=turtle.forward() if state then z=z-1 end end end
if face==3 then for i=1, a, 1 do state=turtle.forward() if state then x=x-1 end end end
return state
end
function back(a)
local state=true
if face==2 then for i=1, a, 1 do state=turtle.forward() if state then z=z-1 end end end
if face==3 then for i=1, a, 1 do state=turtle.forward() if state then x=x-1 end end end
if face==0 then for i=1, a, 1 do state=turtle.forward() if state then z=z+1 end end end
if face==1 then for i=1, a, 1 do state=turtle.forward() if state then x=x+1 end end end
return state
end
function goToPos(a, b, c, mode)
faceEast()
a=a-x
b=b-y
c=c-z
if a>=0 then
for i=1, a, 1 do
while mode and turtle.detect() do turtle.dig() sleep(0.2) end
forward(1)
end
else
a=math.abs(a)
rotate(2)
for i=1, a, 1 do
while mode and turtle.detect() do turtle.dig() sleep(0.2) end
forward(1)
end
end
if b>=0 then
for i=1, b, 1 do
while mode and turtle.detectUp() do turtle.digUp() sleep(0.2) end
up()
end
else
b=math.abs(b)
for i=1, b, 1 do
while mode and turtle.detectDown() do turtle.digDown() sleep(0.2) end
down()
end
end
faceSouth()
if c>=0 then
for i=1, c, 1 do
while mode and turtle.detect() do turtle.dig() sleep(0.2) end
forward(1)
end
else
c=math.abs(c)
rotate(2)
for i=1, c, 1 do
while mode and turtle.detect() do turtle.dig() sleep(0.2) end
forward(1)
end
end
return true
end
function tunnel(w, h, l)
local xs, ys, zs=getPos()
local faces=face
for k=1, l, 1 do
for j=1, h, 1 do
turnLeft()
for i=1, w, 1 do
turtle.dig()
forward(1)
end
for i=1, w, 1 do back(1) end
turnRight()
if j<h then turtle.digDown() down() end
end
for j=1, h, 1 do if j<h then up() end end
turtle.dig() forward(1)
end
goToPos(xs, ys, zs, false)
faceSet(faces)
return true
end
function staircase(w, h, l)
local xs, ys, zs=getPos()
local faces=face
for k=1, l, 1 do
for j=1, h, 1 do
turnLeft()
for i=1, w, 1 do
turtle.dig()
forward(1)
end
for i=1, w, 1 do back(1) end
turnRight()
if j<h then turtle.digDown() down() end
end
for j=1, h, 1 do up() end
turtle.dig() forward(1)
end
goToPos(xs, ys, zs, false)
faceSet(faces)
return true
end
function wread()
local _, y=term.getCursorPos()
local tmp=read()
local x, _=term.getCursorPos()
term.setCursorPos(x,y)
return tmp
end
function build(blueprint)
local l=#blueprint
local h=#blueprint[1]
local w=#blueprint[1][1]
local xs, ys, zs=getPos()
local faces=face
for k=1, l, 1 do
for j=1, h, 1 do
turnLeft()
for i=1, w, 1 do
turtle.dig()
forward(1)
end
for i=1, w, 1 do
local block=blueprint[k][j][i]
print(block)
if block~=0 then turtle.select(block) turtle.place() end
back(1)
end
turnRight()
if j<h then turtle.digDown() down() end
end
for j=1, h, 1 do if j<h then up() end end
turtle.dig() forward(1)
end
goToPos(xs, ys, zs, false)
faceSet(faces)
return true
end
function sketch()
print("Blueprint maker")
print("Use turtle's slot IDs for data")
print("Enter dimensions")
write(" Width: ") local w=wread()
write(" Height: ") local h=wread()
write(" Length: ") local l=wread()
local t={}
for i=1, l, 1 do
print("Vertical Layer ",i)
local tt={}
for j=1, h, 1 do
print("Horizontal Row ", j)
local ttt={}
for k=1, w, 1 do
local tmp=tonumber(wread())
table.insert(ttt, tmp)
write(" ")
end
table.insert(tt, ttt)
end
table.insert(t, tt)
end
return t
end
function getUpdate(side)
local a,b,c=os.pullEvent()
while a~="rednet_message" or b~=updateID do a,b,c=os.pullEvent() end
local tmp=loadstring(c)
tmp()
return true
end
function getUpdateP(side)
if peripheral.getType(side)=="disk" and fs.exists("/"..disk.getMountPath(side).."/magicTurtleUpdate/update") then
shell.run("/"..disk.getMountPath(side).."/magicTurtleUpdate/update")
return true
end
return false
end
function event()
local a,b,c=os.pullEvent()
if a=="rednet_message" and b==updateID and c=="update" then getUpdate(b) return true end
if a=="peripheral" then getUpdateP(b) return true end
return false
end
function updateSeek()
rednet.open("right")
local result = false
while not result do result = event() end
return true
end
function lua()
while true do
write(">")
local com=wread() print("")
if com=="exit" then return true end
if com=="menu" then menu() end
if c then break end
local exec=loadstring(com)
exec()
end
end
function autoUpdate()
autoupdate=true
while true do
parallel.waitForAny(lua, updateSeek)
end
return true
end
function isSpace()
for i=1, 9, 1 do
if turtle.getItemCount(i)==0 then return true end
end
return false
end
function diamondMine(l,...)
local xs,ys,zs=getPos()
local a,b,c=getPos()
while b~=12 do
turtle.dgDown()
down()
turtle.dig()
turtle.digDown()
forward(1)
a,b,c=getPos()
end
while isSpace() do
if ...~=nil then digSlot(...) else turtle.dig() end
forward(1) turtle.digDown()
turnLeft() for i=1, l, 1 do
if ...~=nil then digSlot(...) else turtle.dig() end
forward(1) end
back(l)
rotate(2)
for i=1, l, 1 do
if ...~=nil then digSlot(...) else turtle.dig() end
forward(1)
end
back(l)
end
goToPos(xs,ys,zs, true)
return true
end
function mine(mode, l,...)
if mode=="diamond" then diamondMine(l,...) end
if mode=="regular" then tunnel(l, l, l) end
if mode=="quarry" then quarry(l, ...) end
return true
end
function sendCommand(id, side, exec)
rednet.open(side)
rednet.send(id, "update")
local state=rednet.send(id, exec)
return state
end
function fcoord()
if face==0 then z=z+1 end
if face==1 then x=x+1 end
if face==2 then z=z-1 end
if face==3 then x=x-1 end
end
function lookAround()
if not turtle.forward() and rs.getInput("front") then return false end
fcoord()
if not rs.getInput("bottom") then
back(1)
turnRight()
forward(1)
if not rs.getInput("bottom") then
back(2)
rotate(2)
if not rs.getInput("bottom") then
back(1)
turnRight()
return false
else
return true
end
else
return true
end
else
return true
end
end
function road()
while true do
if not lookAround() then
up()
if not lookAround() then
if not turtle.forward() and not rs.getInput("bottom") then turnLeft() write(1)
if not turtle.forward() and not rs.getInput("bottom") then rotate(2) write(2)
if not turtle.forward() and not rs.getInput("bottom") then break
end
end
end
write(13)
fcoord()
down()
down()
if not lookAround() then up() break end
end
end
end
end
function obs()
while true do
if turtle.detect() then
blow=true
write(1)
back(1)
turtle.select(5)
turtle.place()
for i=1, 5, 1 do
write(2)
if not turtle.detectDown() then turtle.select(1) turtle.placeDown() end
back(1)
end
forward(5)
for i=1, 5, 1 do
back(1)
turtle.select(6)
turtle.place()
end
rs.setOutput("front", true) back(1) rs.setOutput("front", false)
back(3)
sleep(2.5)
forward(9)
blow=false
end
sleep(0.2)
end
end
function blowup()
local tmp
local blow=false
while true do
print(blow)
if not blow then tmp=loadstring(read()) else tmp=loadstring("") end
parallel.waitForAll(tmp, obs)
end
return true
end
function wander()
while true do
if (math.random(1,10))>5 then turnLeft()
else turnRight() end
for i=1, math.random(10, 30), 1 do if not turtle.detect() then forward(1) end end
sleep(1)
end
end
function wanderer(exec)
local wand=true
while wand do
local tmp=loadstring(exec)
parallel.waitForAny(wander, tmp)
end
end
function jack(sap)
local xs,ys,zs=getPos()
turtle.select(sap)
turtle.place()
up()
while not turtle.detect() do sleep(0.2) end
back(2) turnRight() back(2) up() up() up() up()
tunnel(5,5,5)
goToPos(xs,ys,zs, true)
end
function treefarm(sap)
while true do jack(sap) end
end
function watchUp()
while true do
if turtle.detectUp() then rs.setOutput("bottom", true) end
sleep(0.5)
end
end
function disarm()
while true do
local a,b,c=os.pullEventRaw()
if a=="rednet_message" and b==disarmingID and c=="disarm" then arm=false end
sleep(0.5)
end
end
function landmine(slot, net)
local arm
while not turtle.detectDown() do down() end
turtle.digDown() down() turtle.digDown()
turtle.select(slot)
turtle.placeDown()
while true do
if net then
local a,b,c=os.pullEventRaw()
while a~="rednet_message" or b~=armingID or c~="arming" do a,b,c=os.pullEventRaw() end
end
arm=true
while arm do
if net then
parallel.waitForAny(watchUp, disarm)
else
watchUp()
end
end
end
turtle.digdown()
up()
end
function patch(slot, l, w)
for i=1, l, 1 do
turnLeft()
for j=1, w, 1 do
forward(1)
if (turtle.getItemCount(slot))==0 then return false end
turtle.select(slot)
while not turtle.detectDown() do turtle.placeDown() end
end
back(w) turnRight() forward(1)
end
back(l)
end
function guardArea(exec, w, h)
local tmp=loadstring(exec)
while true do
for i=1, h, 1 do
turnLeft()
for j=1, w, 1 do
forward(1)
if not turtle.detectDown() then tmp() end
if turtle.detect() then tmp() end
end
back(w) turnRight() forward(1)
end
back(h)
end
end
function remoteArm(id)
return rednet.send(id, "arming")
end
function remoteDisarm(id)
return rednet.send(id, "disarming")
end
function remoteSleep(id, t)
remoteDisarm(id)
sleep(t)
remoteArm(id)
end
function cDigDown()
local it={}
for i=1, 9, 1 do
table.insert(it, turtle.getItemCount(i))
end
turtle.digDown()
for i=1, 9, 1 do
if it[i]<turtle.getItemCount(i) then return i end
end
return false
end
function cDig()
local it={}
for i=1, 9, 1 do
table.insert(it, turtle.getItemCount(i))
end
turtle.dig()
for i=1, 9, 1 do
if it[i]<(turtle.getItemCount(i)) then return i end
end
return false
end
function camoMine(slot)
local slot2=cDigDown()
down() turtle.digDown()
turtle.select(slot) turtle.placeDown() up()
turtle.select(slot2) turtle.placeDown()
return true
end
function mineGrid(slot)
for i=1, 5, 1 do
turnRight()
for j=1, 5, 1 do
camoMine(slot)
forward(3)
end
back(15)
turnLeft()
forward(3)
end
back(9) turnRight() forward(6) turnLeft()
end
function playerDetect()
if not turtle.forward() and not turtle.dig() then turtle.back() return true else return false end
end
function follow()
while not playerDetect() do
turnRight() sleep(0.1)
end
forward(1)
end
function follower()
while true do
follow()
sleep(0.1)
end
end
function detectLeft()
turnLeft()
local a=turtle.detect()
turnRight()
return a
end
function detectRight()
turnRight()
local a=turtle.detect()
turnLeft()
return a
end
function detectBack()
rotate(2)
local a=turtle.detect()
rotate(2)
return a
end
function detect(side)
if side=="top" then return turtle.detectUp() end
if side=="bottom" then return turtle.detectDown() end
if side=="left" then return detectLeft() end
if side=="right" then return detectRight() end
if side=="back" then return detectBack() end
if side=="front" then return turtle.detect() end
return false
end
function detectDir(f)
local tmp=getFacing()
faceSet(f)
local tmp2=turtle.detect()
faceSet(tmp)
return tmp2
end
function path(face)
while true do
while detectRight() and not turtle.detect() do
forward(1)
end
if detectRight() and turtle.detect() then turnLeft() end
if not detectRight() then if not forward(1) then turnRight() end elseif not turtle.detectUp() then up() else down() end
if not detectDir(face) then break end
end
end
function digSlot(slot)
local s=cDig()
if s and s~=slot then turtle.select(s) turtle.drop() turtle.select(slot) else return false end
return true
end
function quarry(w, l)
local run=true
while run and isSpace() do
for i=1, l, 1 do
while turtle.detect() do turtle.dig() end
turnRight()
for j=1, w, 1 do
local s1=turtle.digDown() forward(1)
end
back(w)
turnLeft()
local s2=down()
end
if not s1 and not s2 then run=false end
end
return true
end
function stf(a,b)
local f=io.open(b,"w")
for i=1, #a,1 do
local ai=a[i]
local tmp2=""
for j=1, #ai, 1 do
local tmp=""
for k=1, #ai[j], 1 do tmp=tmp..ai[j][k] end
tmp=tmp.." "
tmp2=tmp2..tmp tmp=""
end
f:write(tmp2) f:write("\n")
end
f:close()
return true
end
function ctl(a)
local t={}
local tt={}
local ttt={}
for k=1, #a, 1 do
tmp2=a[k]
for i=1, #tmp2, 1 do
local tmp=tmp2[i]
for j=1, string.len(tmp), 1 do table.insert(ttt, string.sub(tmp, j,j)) end
table.insert(tt,ttt) ttt={}
end
table.insert(t,tt) tt={}
end
return t
end
function stt(a)
local t={}
local tmp=""
for i=1, string.len(a), 1 do
if string.sub(a,i,i)==" " then
table.insert(t,tmp) tmp=""
else
tmp=tmp..string.sub(a,i,i)
end
end
if tmp~="" then table.insert(t,tmp) end
return t
end
function tff(name)
local tmp=io.open(name,"r")
local n={}
for line in tmp:lines() do
table.insert(n,stt(line))
end
return ctl(n)
end
function architect(w, h, l)
local blueprint={}
local xs, ys, zs=getPos()
local faces=face
local row={}
local layer={}
for k=1, l, 1 do
for j=1, h, 1 do
turnLeft()
for i=1, w, 1 do
local tmp=cDig()
if not tmp then tmp=0 end
table.insert(row, tmp)
forward(1)
end
for i=1, w, 1 do back(1) end
turnRight()
table.insert(layer, row) row={}
if j<h then
local tmp=cDigDown()
if not tmp then tmp=0 end
table.insert(row, tmp)
down()
end
end
for j=1, h, 1 do if j<h then up() end end
local tmp=cDig()
if not tmp then tmp=0 end
table.insert(row, tmp)
forward(1)
table.insert(blueprint, layer) layer={}
end
goToPos(xs, ys, zs, false)
faceSet(faces)
return blueprint
end