-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathact.lua
More file actions
1515 lines (1435 loc) · 39.6 KB
/
act.lua
File metadata and controls
1515 lines (1435 loc) · 39.6 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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- serialize functions ---------------------------------------------------------
function serialize(o, indent)
local s = ""
indent = indent or ""
if type(o) == "number" then
s = s .. indent .. tostring(o)
elseif type(o) == "boolean" then
s = s .. indent .. (o and "true" or "false")
elseif type(o) == "string" then
if o:find("\n") then
s = s .. indent .. "[[\n" .. o:gsub("\"", "\\\"") .. "]]"
else
s = s .. indent .. string.format("%q", o)
end
elseif type(o) == "table" then
s = s .. "{\n"
for k,v in pairs(o) do
if type(v) == "table" then
s = s .. indent .. " [" .. serialize(k) .. "] = " .. serialize(v, indent .. " ") .. ",\n"
else
s = s .. indent .. " [" .. serialize(k) .. "] = " .. serialize(v) .. ",\n"
end
end
s = s .. indent .. "}"
else
s = s .. indent .. "nil"
--error("cannot serialize a " .. type(o))
end
return s
end
function unserialize( s )
local func, e = loadstring( "return "..s, "serialize" )
if not func then
return s
else
setfenv( func, {inf=math.huge} )
return func()
end
end
function saveFile(name, table, visible)
local filename = visible and name or ".act."..name
local f = io.open(filename, "w")
f:write(serialize(table))
f:close()
end
function loadFile(name, visible)
local filename = visible and name or ".act."..name
if fs.exists(filename) then
local f = fs.open(filename, "r")
local s = f.readAll()
f.close()
return unserialize(s)
end
end
function deleteFile(fileName)
if fs.exists(".act."..fileName) then
fs.delete(".act."..fileName)
end
end
-- movement tracking -----------------------------------------------------------
-- check if API already loaded
if turtle and not turtle.act then
turtle.act = true
-- replace os.pullEvent to not eat events
os.pullEvent = function (...)
local events = {}
local event
while true do
event = {os.pullEventRaw()}
if event[1] == "terminate" then
error("Terminated", 0)
end
local matched = true
for i = 1, arg.n do
if arg[i] and arg[i] ~= event[i] then
matched = false
break
end
end
if matched then
for i, e in ipairs(events) do
os.queueEvent(unpack(e))
end
return unpack(event)
elseif event[1] == "modem_message" then
table.insert(events, event)
end
end
end
-- replace sleep function
sleep = function (time)
local timer = os.startTimer(time)
event = os.pullEvent("timer", timer)
end
-- track relative direction and coordinates
turtle.x = 0
turtle.y = 0
turtle.z = 0
turtle.facing = 0
turtle.fuel = turtle.getFuelLevel()
turtle.selected = 1
-- how much to change x and z when moving in a direction
local coord_change = {[0] = { 0, 1}, -- south / forward
[1] = {-1, 0}, -- west / right
[2] = { 0, -1}, -- north / behind
[3] = { 1, 0}} -- east / left
turtle.setLocation = function (x, y, z, facing)
turtle.x = x or 0
turtle.y = y or 0
turtle.z = z or 0
turtle.facing = facing or turtle.facing
turtle.saveLocation()
end
turtle.saveLocation = function (move)
saveFile("location", {x=turtle.x, y=turtle.y, z=turtle.z, facing=turtle.facing, fuel=turtle.getFuelLevel(), selected=turtle.selected, move=move})
turtle.move = nil
end
turtle.loadLocation = function ()
local loc = loadFile("location")
if loc then
turtle.x = loc.x
turtle.y = loc.y
turtle.z = loc.z
turtle.facing = loc.facing
turtle.fuel = loc.fuel
turtle.selected = loc.selected
turtle.move = loc.move
end
end
turtle.loadLocation()
turtle.updateLocation = function ()
if turtle.move then
local currFuel = turtle.getFuelLevel()
if turtle.fuel == currFuel and turtle.move ~= "l" and turtle.move ~= "r" then
return false -- no update occured
elseif turtle.fuel == currFuel + 1 or turtle.move ~= "l" or turtle.move ~= "r" then
turtle.update(turtle.move)
return true -- location updated
else
-- server rollback?
error("possible server rollback, aborted")
return nil
end
else
return false -- no update occured
end
end
-- waypoints
turtle.waypoint = {}
turtle.loadWaypoints = function ()
local waypoints = loadFile("waypoint")
if waypoints then
turtle.waypoint = waypoints
end
end
turtle.loadWaypoints()
turtle.setWaypoint = function (name, useFacing)
if useFacing == nil then useFacing = true end
if useFacing then
turtle.waypoint[name] = {x=turtle.x, y=turtle.y, z=turtle.z, facing=turtle.facing}
else
turtle.waypoint[name] = {x=turtle.x, y=turtle.y, z=turtle.z}
end
saveFile("waypoint", turtle.waypoint)
end
turtle.go = function (x, y, z, facing, priority)
local waypoint = {x=x, y=y, z=z, facing=facing}
if type(x) == "string" then
if turtle.waypoint[x] then
waypoint = turtle.waypoint[x]
priority = y
else
return false
end
end
turtle.updateLocation()
-- go to coordinates
if type(priority) ~= "string" or
not priority:find("x") or
not priority:find("y") or
not priority:find("z") or
#priority ~= 3 then
priority = "yxz"
end
for c in priority:gmatch(".") do
if c == "y" then
-- move up or down
local dy = waypoint.y - turtle.y
if dy > 0 then
for i = 1, dy do
goUp()
end
elseif dy < 0 then
for i = 1, -dy do
goDown()
end
end
elseif c == "x" then
-- move east or west
local dx = waypoint.x - turtle.x
if dx > 0 then -- east
turtle.face(3)
for i = 1, dx do
goForward()
end
elseif dx < 0 then -- west
turtle.face(1)
for i = 1, -dx do
goForward()
end
end
elseif c == "z" then
-- move north or south
local dz = waypoint.z - turtle.z
if dz > 0 then -- south
turtle.face(0)
for i = 1, dz do
goForward()
end
elseif dz < 0 then -- north
turtle.face(2)
for i = 1, -dz do
goForward()
end
end
end
end
-- face proper direction
if waypoint.facing then
turtle.face(waypoint.facing)
end
return true
end
turtle.gps = function (getFacing)
local x, y, z = gps.locate()
if x ~= nil then
turtle.x = x
turtle.y = y
turtle.z = z
if getFacing then
turtle.forward()
local dx, dy, dz = gps.locate()
if dx ~= nil then
if x == dx then
if z < dz then
turtle.facing = 0 --south
else
turtle.facing = 2 --north
end
else
if x < dx then
turtle.facing = 3 --east
else
turtle.facing = 1 --west
end
end
end
turtle.back()
end
return true
else
return false
end
end
turtle.update = function (move)
if move == "f" then
turtle.x = turtle.x + coord_change[turtle.facing][1]
turtle.z = turtle.z + coord_change[turtle.facing][2]
turtle.fuel = turtle.fuel - 1
elseif move == "b" then
turtle.x = turtle.x - coord_change[turtle.facing][1]
turtle.z = turtle.z - coord_change[turtle.facing][2]
turtle.fuel = turtle.fuel - 1
elseif move == "u" then
turtle.y = turtle.y + 1
turtle.fuel = turtle.fuel - 1
elseif move == "d" then
turtle.y = turtle.y - 1
turtle.fuel = turtle.fuel - 1
elseif move == "l" then
turtle.facing = (turtle.facing - 1) % 4
elseif move == "r" then
turtle.facing = (turtle.facing + 1) % 4
end
turtle.saveLocation()
end
turtle.face = function (facing)
local new_facing = (facing - turtle.facing) % 4
if new_facing == 1 then
turtle.turnRight()
elseif new_facing == 2 then
turtle.turnRight()
turtle.turnRight()
elseif new_facing == 3 then
turtle.turnLeft()
end
end
-- replace movement functions
turtle._turnLeft = turtle.turnLeft
turtle.turnLeft = function ()
turtle.saveLocation("l")
if turtle._turnLeft() then
turtle.update("l")
return true
else
turtle.saveLocation()
return false
end
end
turtle._turnRight = turtle.turnRight
turtle.turnRight = function ()
turtle.saveLocation("r")
if turtle._turnRight() then
turtle.update("r")
return true
else
turtle.saveLocation()
return false
end
end
turtle._forward = turtle.forward
turtle.forward = function ()
turtle.saveLocation("f")
if turtle._forward() then
turtle.update("f")
return true
else
turtle.saveLocation()
return false
end
end
turtle._back = turtle.back
turtle.back = function ()
turtle.saveLocation("b")
if turtle._back() then
turtle.update("b")
return true
else
turtle.saveLocation()
return false
end
end
turtle._up = turtle.up
turtle.up = function ()
turtle.saveLocation("u")
if turtle._up() then
turtle.update("u")
return true
else
turtle.saveLocation()
return false
end
end
turtle._down = turtle.down
turtle.down = function ()
turtle.saveLocation("d")
if turtle._down() then
turtle.update("d")
return true
else
turtle.saveLocation()
return false
end
end
turtle._refuel = turtle.refuel
turtle.refuel = function (...)
turtle._refuel(...)
turtle.fuel = turtle.getFuelLevel()
turtle.saveLocation()
end
turtle._select = turtle.select
turtle.select = function (slot)
if turtle._select(slot) then
turtle.selected = slot
return slot
end
end
turtle._getItemSpace = turtle.getItemSpace
turtle.getItemSpace = function (slot)
slot = slot or turtle.selected
return turtle._getItemSpace(slot)
end
turtle._getItemCount = turtle.getItemCount
turtle.getItemCount = function (slot)
slot = slot or turtle.selected
return turtle._getItemCount(slot)
end
end
-- convenience functions for building a language -------------------------------
local function nopprocess(ast)
return ast
end
local function L(rule, process) -- literial
local r = {rule}
r.type = "l"
r.process = process or nopprocess
return r
end
local function Z(rule, process) -- zero or more
local r = {rule}
r.type = "*"
r.process = process or nopprocess
return r
end
local function O(rule, process) -- one or more
local r = {rule}
r.type = "+"
r.process = process or nopprocess
return r
end
local function C(...) -- ordered choice
if type(arg[#arg]) == "function" then
arg.process = table.remove(arg)
else
arg.process = nopprocess
end
arg.type = "/"
return arg
end
local function S(...) -- sequence
if type(arg[#arg]) == "function" then
arg.process = table.remove(arg)
else
arg.process = nopprocess
end
arg.type = " "
return arg
end
local function M(rule, process) -- optional (maybe)
local r = {rule}
r.type = "?"
r.process = process or nopprocess
return r
end
local function N(rule, process) -- not predicate
local r = {rule}
r.type = "!"
r.process = process or nopprocess
return r
end
local function A(rule, process) -- and predicate
local r = {rule}
r.type = "&"
r.process = process or nopprocess
return r
end
local function P(process) -- process
if type(process) == "table" then
if #process == 0 then
return function(ast)
local rast = {}
for k, v in pairs(process) do
if type(v) == "number" then
rast[k] = ast[v]
else
rast[k] = v
end
end
return rast
end
elseif #process == 1 then
return function(ast)
return ast[process[1]]
end
elseif type(process) == "table" then
return function(ast)
local rast = {}
for i, v in ipairs(process) do
if type(v) == "number" then
rast[i] = ast[v]
else
rast[i] = v
end
end
return rast
end
end
elseif type(process) == "function" then
return function(ast)
ast = process(ast)
local rast = ""
for i, v in ipairs(ast) do
rast = rast .. tostring(v)
end
return rast
end
else
return function(ast)
local rast = ""
for i, v in ipairs(ast) do
rast = rast .. tostring(v)
end
return rast
end
end
end
local function I(rule, rules) -- help for circular reference
for k, v in pairs(rules) do
rule[k] = v
end
end
-- recursive decent parser -----------------------------------------------------
local function match(rules, src, pos)
local ast, rast, newpos, start, stop
if type(rules) == "table" then
if rules.type == "l" then -- literal
start, stop = src:find("^"..rules[1], pos)
if stop then
return stop + 1, rules.process(src:sub(start, stop)), stop + 1
else
return nil, nil, pos
end
elseif rules.type == " " then -- sequence
rast = {}
newpos = pos
for i, rule in ipairs(rules) do
newpos, ast = match(rule, src, newpos)
if newpos == nil then
return nil, nil, pos
end
rast[i] = ast
end
return newpos, rules.process(rast), newpos
elseif rules.type == "/" then -- ordered choice
for i, rule in ipairs(rules) do
newpos, ast = match(rule, src, pos)
if newpos then
return newpos, rules.process(ast), newpos
end
end
return nil, nil, pos
elseif rules.type == "*" then -- zero or more
rast = {}
newpos, ast = match(rules[1], src, pos)
while newpos do
pos = newpos
table.insert(rast, ast)
newpos, ast = match(rules[1], src, pos)
end
return pos, rules.process(rast), pos
elseif rules.type == "+" then -- one or more
rast = {}
newpos, ast = match(rules[1], src, pos)
if newpos == nil then
return nil, nil, pos
end
while newpos do
pos = newpos
table.insert(rast, ast)
newpos, ast = match(rules[1], src, pos)
end
return pos, rules.process(rast), pos
elseif rules.type == "?" then -- optional
newpos, ast = match(rules[1], src, pos)
if newpos then
return newpos, rules.process(ast), newpos
else
return pos, nil, pos
end
elseif rules.type == "&" then -- and predicate
if match(rules[1], src, pos) then
return pos, nil, pos
else
return nil, nil, pos
end
elseif rules.type == "!" then -- not predicate
if match(rules[1], src, pos) then
return nil, nil, pos
else
return pos, nil, pos
end
end
else -- literal, no processing
start, stop = src:find("^"..rules, pos)
if stop then
return stop + 1, src:sub(start, stop), stop + 1
else
return nil, nil, pos
end
end
end
-- language for striping spaces and comments, the pre-processor ----------------
local pre = {}
pre.comment = S("%-%-[^\r\n]*[\r\n]*")
pre.space = C("[ \t\r\n]+", pre.comment)
pre.string = S('"',O(C('\\"', '[^"]'), P()),'"', P())
pre.line = S(Z(pre.space), C(pre.string, '[^ \r\n"]+'), Z(pre.space), P{2})
pre.source = O(pre.line, P())
-- act language ----------------------------------------------------------------
local lang = {}
lang.float = L("%-?%d+%.%d+", function (ast) return tonumber(ast) end)
lang.int = L("%-?%d+", function (ast) return tonumber(ast) end)
lang.string = S('"',O(C('\\"', '[^"]'), P()),'"', function (ast)
return ast[2]:gsub("\\n", "\n"):gsub('\\"', '"'):gsub("\\\\", "\\")
end)
lang.token = L("[%u%l%d_]+")
lang.numvar = S("#", "[%u%l_]", P{vartype="num", name=2})
lang.additive = {}
lang.multiplicative = {}
lang.exponential = {}
lang.primary = C(lang.float, lang.int, "[%u%l_]", S("%(", lang.additive, "%)", P{2}))
lang.exponent_op = L("%^")
lang.mult_op = C("//", "[%%%*/]")
lang.additive_op = L("[%+%-]")
I(lang.exponential, C(S(lang.primary, N(lang.exponent_op), P{1}),
S(lang.primary, lang.exponent_op, lang.exponential, P{left=1, right=3, op=2})))
I(lang.multiplicative, C(S(lang.exponential, N(lang.mult_op), P{1}),
S(lang.exponential, lang.mult_op, lang.multiplicative, P{left=1, right=3, op=2})))
I(lang.additive, C(S(lang.multiplicative, N(lang.additive_op), P{1}),
S(lang.multiplicative, lang.additive_op, lang.additive, P{left=1, right=3, op=2})))
lang.calc = S("#", lang.additive, "#", P{calc=2})
lang.boolvar = S("%$", "[%u%l]", P{vartype="bool", name=2})
lang.extvar = S("%%", lang.token, "%%", P{vartype="ext", name=2})
lang.worker = S(lang.token, ":", P{1})
lang.number = C("%*", lang.float, lang.int, lang.calc, lang.numvar)
lang.variable = S("=", C(lang.numvar, lang.boolvar, lang.extvar), P{2})
lang.predicate = L("[%?~]")
lang.comparison = S(C("<", ">", "<=", ">=", "==", "~="), lang.number, P{operator=1, value=2})
lang.locationaction = C(
S("G<", lang.int, ",", lang.int, ",", lang.int, M(S(",", lang.int, P{2})), M(S(",", lang.token, P{2})), ">", P{waypointtype="G", x=2, y=4, z=6, facing=7, priority=8}),
S("[Gw]", "<", lang.token, M(S(",", lang.token, P{2})), ">", P({waypointtype=1, waypoint=3, priority=4}))
)
lang.param2action = S("t", lang.int, ",", lang.int, P{action=1, param1=2, param2=4})
lang.paramaction = S(C("[cegostz]", "[E][fud]", "Ct", "I[cs]"), C(lang.number, lang.string, lang.boolvar), P{action=1, param=2})
lang.simpleaction = C("[cefblrudq]", "[ABCDEGHMPS][fud]", "Gb", "I[cfs]")
lang.extension = S("%%", lang.token, Z(S(",", C(lang.token, lang.string, lang.number), P{2})), "%%", P{extension=2, params=3})
lang.action = {}
lang.joiner = S("/", O(lang.action))
lang.plan = S(M(lang.worker), O(lang.action), M(lang.joiner),
function (ast)
local rast = {actions=ast[2], plantype="par"}
if ast[1] then rast.worker = ast[1] end
if ast[3] then rast.join = ast[3][2] end
return rast
end
)
lang.parplan = S("%(", lang.plan, "%)", P{2})
lang.seqplan = S("{", lang.plan, "}",
function (ast)
local rast = ast[2]
rast.plantype = "seq"
return rast
end
)
I(lang.action, S(M(lang.predicate), C(
lang.extension,
lang.param2action,
lang.paramaction,
lang.locationaction,
lang.simpleaction,
lang.seqplan,
lang.parplan
), M(lang.number), M(S(",", lang.number), P{2}), M(lang.comparison), M(lang.variable),
function (ast)
local rast
if ast[2].action then
rast = ast[2]
else
rast = {action=ast[2]}
end
if ast[1] then rast.predicate = ast[1] end
if ast[4] then
rast.start = ast[3]
rast.count = ast[4]
elseif ast[3] then
rast.start = 1
rast.count = ast[3]
end
if ast[5] then rast.comparison = ast[5] end
if ast[6] then rast.variable = ast[6] end
return rast
end
))
lang.start = O(lang.plan, P{1})
-- macro mode ------------------------------------------------------------------
lang.repeater = S(M(lang.int), M(lang.joiner), "%)", M(lang.number), P{repeatlines=1, join=2, count=4})
-- compiler --------------------------------------------------------------------
local planContainer = {seq = {"{", "}"},
par = {"(", ")"}}
local varType = {num="#", bool="$"}
local function compileCalc(ast)
if type(ast) == "table" then
return "("..compileCalc(ast.left)..ast.op..compileCalc(ast.right)..")"
else
return tostring(ast)
end
end
local function varString(v)
if type(v) == "table" then
if v.calc then
return "#"..compileCalc(v.calc).."#"
elseif v.vartype == "ext" then
return "%"..v.name.."%"
else
tprint(v)
return varType[v.vartype]..v.name
end
else
if type(v) == "number" then
return tostring(v)
elseif v:find("^[%u%l%d_]+$") then
return v
else
return string.format("%q", v)
end
end
end
function compile(ast)
if ast.action then
local src = ""
if type(ast.action) == "string" then
src = ast.action
else
src = compile(ast.action)
end
if ast.predicate then
src = ast.predicate .. src
end
if ast.count then
if ast.start ~= 1 then
src = src .. varString(ast.start) .. "," .. varString(ast.count)
else
src = src .. varString(ast.count)
end
end
if ast.param then
src = src .. varString(ast.param)
end
if ast.param1 then
src = src .. varString(ast.param1) .. "," .. varString(ast.param2)
end
if ast.comparison then
src = src .. ast.comparison.operator .. varString(ast.comparison.value)
end
if ast.variable then
src = src .. "=" .. varString(ast.variable)
end
return src
elseif ast.actions then
local src = planContainer[ast.plantype][1]
if ast.worker then
src = src .. ast.worker .. ":"
end
for i, v in ipairs(ast.actions) do
src = src .. compile(v)
end
if ast.join then
src = src .. "/"
for i, v in ipairs(ast.join) do
src = src .. compile(v)
end
end
return src .. planContainer[ast.plantype][2]
elseif ast.waypointtype then
if ast.waypoint then
return ast.waypointtype .. "<" .. ast.waypoint .. ">"
else
local src = ast.waypointtype .. "<" .. tostring(ast.x) .. "," .. tostring(ast.y) .. "," .. tostring(ast.z)
if ast.facing then
src = src .. "," .. ast.facing
end
src = src .. ">"
return src
end
elseif ast.extension then
local src = "%" .. ast.extension
for i, v in ipairs(ast.params) do
src = src .. "," .. varString(v)
end
src = src .. "%"
return src
end
end
-- interpreter, the helper functions -------------------------------------------
local turtle = turtle or {}
local forward = 0
local up = 1
local down = 2
local back = 3
local tMove = {[forward] = turtle.forward,
[up] = turtle.up,
[down] = turtle.down,
[back] = turtle.back}
local tDetect = {[forward] = turtle.detect,
[up] = turtle.detectUp,
[down] = turtle.detectDown}
local tAttack = {[forward] = turtle.attack,
[up] = turtle.attackUp,
[down] = turtle.attackDown}
local tDig = {[forward] = turtle.dig,
[up] = turtle.digUp,
[down] = turtle.digDown}
local tPlace = {[forward] = turtle.place,
[up] = turtle.placeUp,
[down] = turtle.placeDown}
-- Go, move or wait to be cleared
local function goDir(dir)
while not tMove[dir]() do
sleep(1)
end
return true, 1
end
function goForward()
return goDir(forward)
end
function goUp()
return goDir(up)
end
function goDown()
return goDir(down)
end
function goBack()
return goDir(back)
end
-- Move, move or dig or attack until moved
local function moveDir(dir)
while not tMove[dir]() do
if tDetect[dir]() then
tDig[dir]()
else
tAttack[dir]()
end
end
return true, 1
end
function move()
return moveDir(forward)
end
function moveUp()
return moveDir(up)
end
function moveDown()
return moveDir(down)
end
local function findSimilar()
for s = 1, 16 do
if s ~= turtle.selected then
if turtle.compareTo(s) then
return s
end
end
end
return nil
end
-- Build, place block with automatic resupply if needed
local function buildDir(dir)
if turtle.getItemCount(turtle.selected) == 1 then
local resupplySlot = findSimilar()
if resupplySlot then
if tPlace[dir]() then
local currentSlot = turtle.selected
turtle.select(resupplySlot)
turtle.transferTo(currentSlot, turtle.getItemCount(resupplySlot))
turtle.select(currentSlot)
return true
else
return false
end
else
return tPlace[dir]()
end
else
return tPlace[dir]()
end
end
function build()
return buildDir(forward)
end
function buildUp()
return buildDir(up)
end
function buildDown()
return buildDir(down)
end
-- Sleep
local function zzz(n)
sleep(n)
return true
end
-- command handlers
local tHandlers = {
-- move
["f"] = turtle.forward,
["b"] = turtle.back,
["u"] = turtle.up,
["d"] = turtle.down,
["l"] = turtle.turnLeft,
["r"] = turtle.turnRight,
-- others
["s"] = turtle.select,
["t"] = turtle.transferTo,
["e"] = turtle.refuel,
["o"] = function (msg)
io.write(tostring(msg))
end,
["c"] = turtle.craft,
["q"] = error,
-- dig
["Df"] = turtle.dig,
["Du"] = turtle.digUp,
["Dd"] = turtle.digDown,
-- attack
["Af"] = turtle.attack,
["Au"] = turtle.attackUp,
["Ad"] = turtle.attackDown,
-- place
["Pf"] = turtle.place,
["Pu"] = turtle.placeUp,
["Pd"] = turtle.placeDown,
-- build
["Bf"] = build,
["Bu"] = buildUp,
["Bd"] = buildDown,
-- suck
["Sf"] = turtle.suck,
["Su"] = turtle.suckUp,
["Sd"] = turtle.suckDown,
-- drop (E for eject)
["Ef"] = turtle.drop,
["Eu"] = turtle.dropUp,
["Ed"] = turtle.dropDown,
-- move, dig routine with anti-gravel/sand and anti-mob logic
["Mf"] = move,
["Mu"] = moveUp,
["Md"] = moveDown,
-- go, move or wait to be cleared
["Gf"] = goForward,
["Gu"] = goUp,
["Gd"] = goDown,
["Gb"] = goBack,
-- hit, detect
["Hf"] = turtle.detect,
["Hu"] = turtle.detectUp,
["Hd"] = turtle.detectDown,
-- compare
["Cf"] = turtle.compare,