This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtleengine.lua
299 lines (241 loc) · 5.29 KB
/
turtleengine.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
local PROG_FILE_NAME = "prog.txt"
local tArgs = { ... }
function tokenise(str)
local sLine = table.concat( { str }, " " )
local tWords = {}
local bQuoted = false
for match in string.gmatch( sLine .. "\"", "(.-)\"" ) do
if bQuoted then
table.insert( tWords, match )
else
for m in string.gmatch( match, "[^ \t]+" ) do
table.insert( tWords, m )
end
end
bQuoted = not bQuoted
end
return tWords
end
scriptLines = {}
autoRefuel = false
loop = 0
function readScript(fileName)
if fileName == nil then
return false
end
file = fs.open(fileName, "r")
if file then
line = file.readLine()
while line do
table.insert(scriptLines, line)
line = file.readLine()
end
file.close()
end
end
function saveProgress(action, loop)
file = fs.open(PROG_FILE_NAME, "w")
if loop then
tbl = {action = action; loop = loop}
else
tbl = {action = action}
end
data = textutils.serialise(tbl)
file.write(data)
file.close()
print("Saved " .. action)
end
function readProgress()
file = fs.open(PROG_FILE_NAME, "r")
if file then
tbl = textutils.unserialise(file.readAll())
if tbl and tbl.action then
if tbl.loop then
return {action = tbl.action, loop = tbl.loop}
else
return {action = tbl.action}
end
end
file.close()
end
return nil
end
function deleteProgress()
fs.delete(PROG_FILE_NAME)
print("Progress deleted")
end
function findItem(itemName)
for i = 1, 16 do
det = turtle.getItemDetail(i)
if det and det.name == itemName then
return i
end
end
return -1
end
function parseAction(action)
if not action then
return false, "Action is nil"
end
print("Do \"" .. action .. "\"")
local args = tokenise(action)
if not args or #args == 0 or #args[1] == 0 then
return true
end
if args[1] == "repeat" and #args >= 3 then
if args[2] == "repeat" then
return false, "No recursion :)"
end
count = tonumber(args[3])
if count then
if loop < 1 then
loop = 1
end
for i = loop, count do
print("Do in loop \"" .. args[2] .. "\" (" .. i .. ")")
flag, err = parseAction(args[2])
if not flag then
loop = i
if err then
return false, "Error in loop: " .. (err or "nilerror")
end
end
end
loop = 0
return true
end
return false, "Not correct number"
end
if args[1] == "hello" then
print("Hello")
return true
end
if args[1] == "forward" then
if not turtle then
return false, "Not turtle"
end
return turtle.forward()
elseif args[1] == "back" then
if not turtle then
return false, "Not turtle"
end
return turtle.back()
elseif args[1] == "turnLeft" or args[1] == "left" then
if not turtle then
return false, "Not turtle"
end
return turtle.turnLeft()
elseif args[1] == "turnRight" or args[1] == "right" then
if not turtle then
return false, "Not turtle"
end
return turtle.turnRight()
elseif args[1] == "up" then
if not turtle then
return false, "Not turtle"
end
return turtle.up()
elseif args[1] == "down" then
if not turtle then
return false, "Not turtle"
end
return turtle.down()
elseif args[1] == "autorefuel" then
if not turtle then
return false, "Not turtle"
end
autoRefuel = true
return true
elseif args[1] == "noautorefuel" then
if not turtle then
return false, "Not turtle"
end
autoRefuel = false
return true
elseif args[1] == "place" and args[2] then
if not turtle then
return false, "Not turtle"
end
itemIndex = findItem(args[2])
if itemIndex >= 1 and itemIndex <= 16 then
turtle.select(itemIndex)
return turtle.place()
end
return false, "Item \"" .. args[2] .. "\" not found"
elseif args[1] == "placeu" and args[2] then
if not turtle then
return false, "Not turtle"
end
itemIndex = findItem(args[2])
if itemIndex >= 1 and itemIndex <= 16 then
turtle.select(itemIndex)
return turtle.placeUp()
end
return false, "Item \"" .. args[2] .. "\" not found"
elseif args[1] == "placed" and args[2] then
if not turtle then
return false, "Not turtle"
end
itemIndex = findItem(args[2])
if itemIndex >= 1 and itemIndex <= 16 then
turtle.select(itemIndex)
return turtle.placeDown()
end
return false, "Item \"" .. args[2] .. "\" not found"
end
return false, "Not exist action \"" .. args[1] .. "\""
end
scriptPath = nil
if tArgs and tArgs[1] then
scriptPath = tArgs[1]
end
if scriptPath == nil then
print("What script path?")
sciprtPath = read()
end
print(scriptPath)
if scriptPath == nil or #scriptPath == 0 then
print("goodbye")
return
end
readScript(scriptPath)
print(#scriptLines)
prog = readProgress()
run = true
action = 1
if prog then
action = prog.action
if prog.loop then
loop = prog.loop
else
loop = 1
end
end
print("Last progress: " .. action .. " " .. loop)
while run and action <= #scriptLines do
line = scriptLines[action]
flag, err = parseAction(line)
if not flag then
run = false
print("Finishing because \"" .. (err or "nilerror") .. "\"")
end
if loop >= 1 then
saveProgress(action, loop)
else
saveProgress(action)
end
if flag then
action = action + 1
end
end
if action > #scriptLines then
print("Script comleted!")
deleteProgress()
end
if not run then
if loop >= 1 then
print("Script finished at " .. action .. " position and loop " .. loop)
else
print("Script finished at " .. action .. " position")
end
end