-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.lua
229 lines (216 loc) · 5.99 KB
/
Display.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
key = "TurtleControlSystem"
rednet.open("back")
Main = nil
Ping = 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}
os.startTimer(1)
run = true
if(peripheral.getType("right") ~= "monitor") then
print("No monitor connected to the right")
run = false
end
local monitor = peripheral.wrap("right")
x,y = monitor.getSize()
if(x ~= 77 or y ~= 38) then
print("Monitor is to small")
run = false
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 Number(iid)
id = tonumber(iid)
if(id == 0) then return " [N/A] "
elseif(id < 10) then return " ["..id.."] "
elseif(id < 100) then return " ["..id.."] "
elseif(id < 1000) then return " ["..id.."] "
elseif(id < 10000) then return " ["..id.."] "
else return tostring(id) end
end
function MidInv(String)
id = string.len(String)
if(id == 0 or String == "0" or String == "0/0") then return " empty "
elseif(id == 1) then return " "..String.." "
elseif(id == 2) then return " "..String.." "
elseif(id == 3) then return " "..String.." "
elseif(id == 4) then return " "..String.." "
elseif(id == 5) then return " "..String.." "
elseif(id == 6) then return " "..String.." "
elseif(id == 7) then return " "..String.." "
elseif(id == 8) then return " "..String.." "
elseif(id == 9) then return " "..String.." "
else return String end
end
function NumberPing(iid)
id = math.floor(tonumber(iid))
if(id == 0 and iid ~= "0") then
id = 1
end
if(id == 0) then return " N/A "
elseif(id < 10) then return " "..id.." "
elseif(id < 100) then return " "..id.." "
elseif(id < 1000) then return " "..id.." "
elseif(id < 10000) then return " "..id.." "
elseif(id > 999999) then return "Overfl";
else return tostring(id) end
end
function displaymonitor(monitor)
--Clear Display
monitor.clear()
--Init Monitor
monitor.setCursorPos(28,1)
monitor.write("Turtle Control System")
monitor.setCursorPos(1,2)
monitor.write("-----------------------------------------------------------------------------")
monitor.setCursorPos(1,3)
monitor.write("|---ID---|--Connection--|-Progress-|--Task--|-Ping-|---Status---|-Inventory-|")
monitor.setCursorPos(1,4)
monitor.write("|---------------------------------------------------------------------------|")
i = 5
while(i < 38) do
monitor.setCursorPos(1,i)
n = i - 4
monitor.write("|"..Number(ID[n]).."| | | | | | |")
i = i + 1
end
monitor.setCursorPos(1,38)
monitor.write("-----------------------------------------------------------------------------")
end
monitor.clear()
monitor.setCursorBlink(false)
monitor.setCursorPos(1,1)
monitor.write("Welcome")
monitor.setCursorPos(1,2)
monitor.write("Connecting")
conectiontimer = 0
rednet.broadcast(key.."connectDisplay")
write("Conntecting.")
while(run) do
--print("Running: "..number)
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
end
if(event == "rednet_message") then
if(Main == nil and param2 == "connectMain") then
Main = param1
Ping = 0
conectiontimer = nil
print("")
print("Connected")
displaymonitor(monitor)
end
if(Main ~= nil) then
if(param1 == Main) then
if(param2 == "pong") then
Ping = 0
else
array = split(param2, ",")
p1, p2, p3 = array[1], array[2], array[3]
-- |---ID---|--Connection--|-Progress-|--Task--|-Ping-|---Status---|-Inventory-|
if(p1 == "NewTurtle") then
ID[p2] = p3
monitor.setCursorPos(1,tonumber(p2)+4)
monitor.write("|"..Number(ID[p2]).."| Connected | 0% | clear | N/A | sleep | 0 |")
end
if(p1 == "TurtleDisconect") then
ID[p2] = 0
monitor.setCursorPos(1,tonumber(p2)+4)
monitor.write("|"..Number(ID[p2]).."| | | | | | |")
end
if(p1 == "TurtleTimeout") then
monitor.setCursorPos(11,tonumber(p2)+4)
monitor.write(" Timed Out ")
monitor.setCursorPos(46,tonumber(p2)+4)
monitor.write(NumberPing(0))
end
if(p1 == "pingtime") then
monitor.setCursorPos(46,tonumber(p2)+4)
monitor.write(NumberPing(p3))
end
if(p1 == "inventory") then
monitor.setCursorPos(66,tonumber(p2)+4)
monitor.write(MidInv(p3))
end
end
end
end
end
if(event == "timer") then
os.startTimer(1)
x,y = monitor.getSize()
if(x ~= 77 or y ~= 38) then
print("Monitor is to small")
monitor.setCursorPos(1,1)
i = 1
while(i<(y+1)) do
j = 1
while(j<x) do
monitor.write("ERROR ")
j = j + 1
end
monitor.setCursorPos(1,i)
i = i + 1
end
monitor.setCursorPos(1,i-1)
monitor.write("Please restart the programm")
run = false
end
if(Main ~= nil) then
if((Ping % 2) == 0) then
rednet.send(Main,"ping")
if(Ping > 6) then
conectiontimer = 0
Main = nil
monitor.clear()
monitor.setCursorPos(30,18)
monitor.write("Lost Connection")
monitor.setCursorPos(30,19)
monitor.write("Connecting")
print("Lost Connection to Main")
write("Conntecting.")
rednet.broadcast(key.."connectDisplay")
end
end
Ping = Ping + 1
end
if(conectiontimer ~= nil) then
write(".")
conectiontimer = conectiontimer + 1
if(conectiontimer > 10) then
print("")
print("Timed Out")
monitor.clear()
run = false
end
if(run) then
rednet.broadcast(key.."connectConsole")
end
end
end
end