-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.lua
305 lines (288 loc) · 6.59 KB
/
Main.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
key = "TurtleControlSystem"
PING_MAX = 10
rednet.open("back")
Display = nil
DisplayPing = 0
Console = nil
ConsolePing = 0
ID = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
Ping = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
NextPing = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
PingTime = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
nPos = 0
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
--Console Init
sLine = ""
nHistoryPos = nil
nPos = 0
_tHistory = {}
w, h = term.getSize()
sx, sy = term.getCursorPos()
function print( ... )
term.setCursorBlink( false )
term.setCursorPos(1, sy)
write(string.rep(" ",w))
term.setCursorPos(1, sy)
local nLinesPrinted = 0
for n,v in ipairs( { ... } ) do
nLinesPrinted = nLinesPrinted + write( tostring( v ) )
end
nLinesPrinted = nLinesPrinted + write( "\n" )
sx, sy = term.getCursorPos()
ConsoleText()
return nLinesPrinted
end
function ConsoleText()
term.setCursorBlink( false )
term.setCursorPos( sx, sy )
term.write( string.rep(" ", w - sx + 1) )
term.setCursorPos( sx, sy )
term.write( sLine )
term.setCursorPos( sx + nPos, sy )
term.setCursorBlink( true )
end
function Command(input)
if(input == "exit") then
run = false
elseif(input == "reboot") then
os.reboot()
elseif(input == "shutdown") then
os.shutdown()
elseif(input == "info") then
print("DisplayPing:"..DisplayPing)
print("ConsolePing:"..ConsolePing)
print("sx:"..sx)
print("sy:"..sy)
elseif(input == "clear") then
term.clear()
term.setCursorPos(1,1)
else
print("Unknown command")
end
end
term.setCursorBlink( true )
--Console End
os.startTimer(1)
run = true
while(run) do
--print("Running")
event,param1,param2 = os.pullEvent()
--if(event ~= "timer") then
-- print(event)
-- print(param1)
-- print(param2)
--end
if(event == "rednet_message") then
if(param2 == key.."connectDisplay" and (Display ~= nil or DisplayPing > 6)) then
Display = param1
print("Display Connected")
rednet.send(param1,"connectMain")
end
if(param2 == key.."connectConsole" and (Console ~= nil or ConsolePing > 6)) then
Console = param1
print("Console Connected")
rednet.send(param1,"connectMain")
end
if(param2 == "ping") then
if(param1 == Display) then
DisplayPing = 0
rednet.send(Display,"pong")
end
if(param1 == Console) then
ConsolePing = 0
rednet.send(Console,"pong")
end
i = 0
done = 0
while(i<33 and done == 0) do
if(param1 == ID[i]) then
rednet.send(param1,"pong")
end
i = i + 1
end
end
if(Display ~= nil and Console ~= nil) then
if(param2 == key.."connectTurtle") then
i=0
done = 0
while(i<33) do
if(ID[i] ~= 0 and ID[i] == param1) then
print("Connecting known Turtle")
rednet.send(param1, "ping")
rednet.send(param1,"connectMain")
done = 1
end
i=i+1
end
i = 0
while(i<33 and done == 0) do
if(ID[i] == 0) then
print("Connecting new Turtle")
ID[i] = param1
rednet.send(Display,"NewTurtle,"..i..","..param1)
rednet.send(param1, "ping")
rednet.send(param1,"connectMain")
done = 1
end
i=i+1
end
end
if(param2 == "TurtleDisconnect") then
i = 0
done = 0
while(i<33 and done == 0) do
if(param1 == ID[i]) then
ID[i] = 0
rednet.send(Display,"TurtleDisconect,"..param1)
done = 1
end
i=i+1
end
end
if(param2 == "pong") then
i = 0
done = 0
while(i<33 and done == 0) do
if(param1 == ID[i]) then
Ping[i] = 0
dtime = os.clock() - PingTime[i]
if(dtime == 0) then
dtime = 1
end
rednet.send(Display,"pingtime,"..i..","..dtime)
done = 1
end
i=i+1
end
end
array = split(param2, ",")
p1, p2, p3 = array[1], array[2], array[3]
if(p1 == "Display") then
i = 0
done = 0
while(i<33 and done == 0) do
if(param1 == ID[i]) then
rednet.send(Display,p2..","..i..","..p3)
done = 1
end
i=i+1
end
end
end
end
-- Console
if(event == "char") then
sLine = string.sub( sLine, 1, nPos ) .. param1 .. string.sub( sLine, nPos + 1 )
nPos = nPos + 1
ConsoleText()
end
if event == "key" then
if param1 == 28 then
-- Enter
print(sLine)
nPos = 0
Command(sLine)
table.insert(_tHistory, sLine)
sLine = ""
ConsoleText()
elseif param1 == 203 then
-- Left
if nPos > 0 then
nPos = nPos - 1
ConsoleText()
end
elseif param1 == 205 then
-- Right
if nPos < string.len(sLine) then
nPos = nPos + 1
ConsoleText()
end
elseif param1 == 200 or param1 == 208 then
-- Up or down
if _tHistory then
if param1 == 200 then
-- Up
if nHistoryPos == nil then
if #_tHistory > 0 then
nHistoryPos = #_tHistory
end
elseif nHistoryPos > 1 then
nHistoryPos = nHistoryPos - 1
end
else
-- Down
if nHistoryPos == #_tHistory then
nHistoryPos = nil
elseif nHistoryPos ~= nil then
nHistoryPos = nHistoryPos + 1
end
end
if nHistoryPos then
if(sLine ~= "") then
table.insert(_tHistory, sLine)
end
sLine = _tHistory[nHistoryPos]
nPos = string.len( sLine )
else
sLine = ""
nPos = 0
end
ConsoleText()
end
elseif param1 == 14 then
-- Backspace
if nPos > 0 then
sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
nPos = nPos - 1
ConsoleText()
end
end
end
-- Console end
if(event == "timer") then
os.startTimer(1)
DisplayPing = DisplayPing + 1
if(DisplayPing > 6 and Display ~= nil) then
Display = nil
print("Lost Display connection")
end
ConsolePing = ConsolePing + 1
if(ConsolePing > 6 and Console ~= nil) then
Console = nil
print("Lost Console connection")
end
i = 1
while(i<33) do
if(ID[i] ~= 0) then
Ping[i] = Ping[i] + 1
NextPing[i] = NextPing[i] + 1
if(Ping[i] > PING_MAX and Ping[i] < (PING_MAX + 2)) then
rednet.send(Display,"TurtleTimeout,"..i)
end
if(NextPing[i] > 5) then
rednet.send(ID[i],"ping")
PingTime[i] = os.clock()
NextPing[i] = 0
end
end
i=i+1
end
end
end