-
Notifications
You must be signed in to change notification settings - Fork 3
/
act.cc.lua
293 lines (275 loc) · 6.78 KB
/
act.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
-- TODO
-- history
-- save
-- actor (rednet)
-- blueprints
-- relative coords/facing
-- return home
-- infinite loop
-- waypoints
-- mini language for making scripts smaller
-- and doing ad-hoc commands faster
--
-- look down in the handlers for all the commands
-- you can put a number afterwards to repeat a single command
-- you can put parenthesis around a list and a number after that to repeat several commands
-- detect and compare will break out of parethesis
--
-- examples:
-- Chop down tree to a max height of 48
--
-- Dff(?uDuu)48d48
--
-- Here is how the commands are interpreted
--
-- Df - turtle.dig()
-- f - turtle.forward()
-- ( - for i = 1, 48 do
-- ?u - if not turtle.detectUp() then break end
-- Du - turtle.digUp()
-- u - turtle.up()
-- ) 48 - end
-- d 48 - for i = 1, 48 do turtle.down() end
--
-- You can use the language in other scripts like so
--
-- os.loadAPI("act")
-- act.act("f5rrf5ll")
-- act internal functions
local forward = 0
local up = 1
local down = 2
local tMove = {[forward] = turtle.forward,
[up] = turtle.up,
[down] = turtle.down}
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}
local function tryDir(dir)
while not tMove[dir]() do
if tDetect[dir]() then
tDig[dir]()
else
tAttack[dir]()
end
end
return true
end
-- act turtle functions
function try()
return tryDir(forward)
end
function tryUp()
return tryDir(up)
end
function tryDown()
return tryDir(down)
end
local currentSlot = 1
function select(slot)
currentSlot = slot
turtle.select(slot)
return true
end
local function findSimilar()
for s = 1, 16 do
if s ~= currentSlot then
turtle.select(s)
if turtle.compareTo(currentSlot) then
turtle.select(currentSlot)
return s
end
end
end
turtle.select(currentSlot)
return nil
end
local function placeDir(dir)
if turtle.getItemCount(currentSlot) == 1 then
local resupplySlot = findSimilar()
if resupplySlot then
if tPlace[dir]() then
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 place()
return placeDir(forward)
end
function placeUp()
return placeDir(up)
end
function placeDown()
return placeDir(down)
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"] = select,
["t"] = turtle.transfer,
["R"] = turtle.refuel,
-- dig
["Df"] = turtle.dig,
["Du"] = turtle.digUp,
["Dd"] = turtle.digDown,
-- attach
["Af"] = turtle.attack,
["Au"] = turtle.attackUp,
["Ad"] = turtle.attackDown,
-- place
["Pf"] = place,
["Pu"] = placeUp,
["Pd"] = placeDown,
-- suck
["Sf"] = turtle.suck,
["Su"] = turtle.suckUp,
["Sd"] = turtle.suckDown,
-- drop (E for eject)
["Ef"] = turtle.drop,
["Eu"] = turtle.dropUp,
["Ed"] = turtle.dropDown,
-- try, dig routing with anti-gravel/sand and anti-mob logic
["Tf"] = try,
["Tu"] = tryUp,
["Td"] = tryDown,
-- detect
["?f"] = turtle.detect,
["?u"] = turtle.detectUp,
["?d"] = turtle.detectDown,
-- compare
["=f"] = turtle.compare,
["=u"] = turtle.compareUp,
["=d"] = turtle.compareDown,
["=="] = turtle.compareTo,
["z"] = sleep
}
function getNumber(s, pos, max, default)
if tonumber(s:sub(pos + 1, pos + 1)) == nil then
return default, pos
else
local n = 0
while pos <= max and tonumber(s:sub(pos + 1, pos + 1)) ~= nil do
pos = pos + 1
n = n * 10 + tonumber(s:sub(pos, pos))
end
return n, pos
end
end
function act(plan)
local pos = 1
local max = plan:len()
while pos <= max do
local c = plan:sub(pos, pos)
if c == "(" then
-- read until matching )
local p = 1
local sub_plan = ""
while p > 0 do
pos = pos + 1
if plan:sub(pos, pos) == ")" then
p = p - 1
elseif plan:sub(pos, pos) == "(" then
p = p + 1
end
if p > 0 then
sub_plan = sub_plan .. plan:sub(pos, pos)
end
end
-- get optional count
local n = nil
n, pos = getNumber(plan, pos, max, 1)
-- call recursively
for i = 1, n, 1 do
if not act(sub_plan, n) then
print("sub plan failure")
return false
end
end
else
if c == "D"
or c == "A"
or c == "P"
or c == "S"
or c == "E"
or c == "T"
or c == "?"
or c == "=" then
pos = pos + 1
c = c .. plan:sub(pos, pos)
end
-- call handler
local fn = tHandlers[c]
if fn then
if c == "f" or c == "b" or c == "u" or c == "d" or c == "l" or c == "r" or c == "Tf" or c == "Td" or c == "Tu" then
-- move handlers, number defines iterations
-- get optional count
local n = nil
n, pos = getNumber(plan, pos, max, 1)
for i = 1, n, 1 do
if not fn() then
if turtle.getFuelLevel() == 0 then
print("Out of fuel")
return false -- stop entire plan
else
print("Blocked: " .. plan:sub(1, pos) .. " / " .. plan:sub(pos + 1))
return false -- stop entire plan
end
end
end
elseif c:sub(1,1) == "?" or c:sub(1,1) == "=" then
-- detect and compare, failure will only skip out of the current block
local result = nil
if c == "==" then
local n = nil
n, pos = getNumber(plan, pos, max, 1)
result = fn(n)
else
result = fn()
end
if not result then
return true -- stop current plan
end
else
-- all other handlers, number gets passed to function
local n = nil
n, pos = getNumber(plan, pos, max)
if not fn(n) then
print("Can't perform action: " .. c)
-- return false
end
end
else
print("Unknown command: " .. c)
return false
end
end
pos = pos + 1
end
return true
end