-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtle.lua
403 lines (381 loc) · 7.43 KB
/
Turtle.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
key = "TurtleControlSystem"
rednet.open("right")
Main = nil
isturtle = true
if(turtle) then
run = true
else
run = false
isturtle = false
end
conectiontimer = 0
os.startTimer(0.1)
timedtask = 0
task = nil
taskX = nil
taskZ = nil
posX = 0
posY = 0
posZ = 0
dirrection = "Forward"
if(isturtle) then
write("Conntecting.")
end
function posInfo()
print(posX)
print(posY)
print(posZ)
print(dirrection)
end
function movetostart()
max = math.max(math.abs(posX), math.abs(posY), math.abs(posZ))
while(max ~= 0) do
max = math.max(math.abs(posX), math.abs(posY), math.abs(posZ))
if(max == math.abs(posZ)) then
if(posZ < 0) then
TurnTo("Forward")
while(posZ<0) do
if(turtle.detect()) then
turtle.dig()
end
forward()
end
elseif(posZ > 0) then
TurnTo("Back")
while(posZ>0) do
if(turtle.detect()) then
turtle.dig()
end
forward()
end
end
elseif(max == math.abs(posY)) then
if(posY<0) then
while(posY<0) do
if(turtle.detectUp()) then
turtle.digUp()
end
up()
end
elseif(posY>0) then
while(posY>0) do
if(turtle.detectDown()) then
turtle.digDown()
end
down()
end
end
elseif(max == math.abs(posX)) then
if(posX > 0) then
TurnTo("Left")
while(posX>0) do
if(turtle.detect()) then
turtle.dig()
end
forward()
end
elseif(posX < 0) then
TurnTo("Right")
while(posX<0) do
if(turtle.detect()) then
turtle.dig()
end
forward()
end
end
end
end
TurnTo("Forward")
end
function TurnTo(dir)
timeout = 0
while(dirrection ~= dir and timeout < 50) do
Right()
timeout = timeout + 1
end
end
function forward()
if(turtle.detect()) then
return
end
if(turtle.forward()) then
if(dirrection == "Forward") then
posZ = posZ + 1
elseif(dirrection == "Left") then
posX = posX - 1
elseif(dirrection == "Right") then
posX = posX + 1
elseif(dirrection == "Back") then
posZ = posZ - 1
end
end
end
function up()
if(turtle.up()) then
posY = posY + 1
end
end
function down()
if(turtle.down()) then
posY = posY - 1
end
end
function back()
if(turtle.back()) then
if(dirrection == "Forward") then
posZ = posZ - 1
elseif(dirrection == "Left") then
posX = posX + 1
elseif(dirrection == "Right") then
posX = posX - 1
elseif(dirrection == "Back") then
posZ = posZ + 1
end
end
end
function Left()
if(turtle.turnLeft()) then
if(dirrection == "Forward") then
dirrection = "Left"
elseif(dirrection == "Left") then
dirrection = "Back"
elseif(dirrection == "Right") then
dirrection = "Forward"
elseif(dirrection == "Back") then
dirrection = "Right"
end
end
end
function Right()
if(turtle.turnRight()) then
if(dirrection == "Forward") then
dirrection = "Right"
elseif(dirrection == "Left") then
dirrection = "Forward"
elseif(dirrection == "Right") then
dirrection = "Back"
elseif(dirrection == "Back") then
dirrection = "Left"
end
end
end
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function isBreakableFront()
i = 1
while(i<10) do
if(turtle.getItemCount(i) == 0) then
return true
end
end
while(i<10) do
turtle.select(i)
if(turtle.compare() and turtle.getItemSpace(i) ~= 0) then
return true
end
end
return false
end
function isBreakableTop()
i = 1
while(i<10) do
if(turtle.getItemCount(i) == 0) then
return true
end
end
while(i<10) do
turtle.select(i)
if(turtle.compareUp() and turtle.getItemSpace(i) ~= 0) then
return true
end
end
return false
end
if(isturtle) then
rednet.broadcast(key.."connectTurtle")
end
while(run) do
event,param1,param2 = os.pullEvent()
--if(event ~= "timer") then
-- print(event)
-- print(param1)
-- print(param2)
--end
if(event == "char") then
if(param1 == "s") then
run = false
end
if(param1 == "h") then
os.shutdown()
end
if(param1 == "p") then
rednet.send(Main,"pong")
print("pong")
end
if(param1 == "i") then
posInfo()
end
end
if(event == "key") then
if(param1 == 200) then
forward()
end
if(param1 == 203) then
Left()
end
if(param1 == 205) then
Right()
end
if(param1 == 208) then
back()
end
if(param1 == 57) then
turtle.dig()
end
if(param1 == 201) then
up()
end
if(param1 == 209) then
down()
end
if(param1 == 33) then -- f
m = 0
while(m<20) do
forward()
m = m + 1
end
end
if(param1 == 19) then -- r
movetostart()
end
end
if(event == "rednet_message") then
if(Main == nil and param2 == "connectMain") then
Main = param1
conectiontimer = nil
print("")
print("Connected")
end
if(Main ~= nil and Main == param1) then
if(param2 == "ping") then
rednet.send(Main,"pong")
end
array = split(param2, ",")
p1, p2, p3 = array[1], array[2], array[3]
if(p1 == "task") then
if(task ~= nil) then
StartX, StartY, StartZ = gps.locate(2, false)
if(StartX ~= nil) then
task = "mine"
taskX = tonumber(p2)
taskZ = tonumber(p3)
dirrection = "Forward"
else
print("ERROR: Could not locate!")
end
end
end
end
end
if(event == "timer") then
timedtask = timedtask + 1
if((timedtask % 10) == 1) then
i = 1
count = 0
slots = 0
while(i < 10) do
count = count + turtle.getItemCount(i)
if(turtle.getItemCount(i) ~= 0) then
slots = slots + 1
end
i = tonumber(i + 1)
end
rednet.send(Main,"Display,inventory,"..slots.."/"..count)
end
if(task ~= nil) then
if(StartX ~= nil) then
difX = nowX - StartX
difY = nowY - StartY
difZ = nowZ - StartZ
if(task == "mine") then
if(difX ~= taskX) then
if(difZ == taskZ and dirrection == "Forward") then
dirrection = "Backward"
elseif(difZ == taskZ and dirrection == "Backward") then
dirrection = "Forward"
else
if(isBreakableFront() and isBreakableTop()) then
turtle.dig()
turtle.digUp()
turtle.forward()
else
ClearX = difX
ClearZ = difZ
task = "clear"
end
end
else
task = "endclear"
end
elseif(task == "clears" or task == "endclear" or task == "clearY" or task == "endclearY") then
if(difX == 0 and (task == "clears" or task == "endclear")) then
turtle.turnRight()
elseif(difY == 0 and task == "clearY" or task == "endclearY") then
--TODO
--InventarLeeren
if(task == "endclearY") then
task = nil
elseif(task == "clearY") then
task = "mine"
end
else
turtle.forward()
end
if(turtle.detect()) then
turtle.dig()
end
else
end
else
print("ERROR: Could not locate!")
end
end
if(timedtask > 99) then
timedtask = 0
end
if(conectiontimer ~= nil) then
if((conectiontimer % 10) == 0) then
write(".")
end
conectiontimer = conectiontimer + 1
if(conectiontimer > 100) then
print("")
print("Timed Out")
run = false
end
if(run) then
rednet.broadcast(key.."connectConsole")
end
end
os.startTimer(0.1)
end
end
if(isturtle == false) then
print("Run this on a turtle")
end