View difference between Paste ID: dZ36kMZf and dwJbiZhH
SHOW: | | - or go back to the newest paste.
1-
mon = peripheral.find("monitor")
1+
mon = peripheral.wrap("monitor_0")
2
sg = peripheral.find("stargate")
3-
chat = peripheral.find("chat_box")
3+
4
mon.clear()
5
maxEng = 50000
6
dialling = {}
7
8
9
  
10
11
function drawChevrons() --draws cheyvrons on the screen
12
  x,y = mon.getSize()
13
  chevX1 = x/3
14
  chevX2 = x/3*2+1
15
  chevY1 = y/3-2
16
  chevY2 = y/3*2 +2
17
  mon.setBackgroundColor(colors.black)
18
  for yc = chevY1-2, chevY2-2 do
19
    for xc = chevX1-2, chevX2-2 do
20
	  mon.setCursorPos(xc, yc)
21
	  mon.write(" ")
22
	end
23
  end
24
  mon.setBackgroundColor(colors.lightGray)
25
  for i = chevX1+2, chevX2-2 do
26
    mon.setCursorPos(i,chevY1)
27
	mon.write(" ")
28
  end
29
  for i = chevX1+2, chevX2-2 do
30
    mon.setCursorPos(i,chevY2)
31
	mon.write(" ")
32
  end
33
  for i = chevY1+2, chevY2-2 do
34
    mon.setCursorPos(chevX1,i)
35
	mon.write(" ")
36
  end
37
  for i = chevY1+2, chevY2-2 do
38
    mon.setCursorPos(chevX2, i)
39
	mon.write(" ")
40
  end
41
  chev1pos = {chevX1, chevY2 }
42
  mon.setBackgroundColor(colors.gray)
43
  mon.setTextColor(colors.black)
44
  mon.setCursorPos(math.floor(chev1pos[1]), math.floor(chev1pos[2])-1)
45
  mon.write(" > ")
46
  chev2pos = {chevX1, chevY1 + ((chevY2 - chevY1) / 2) }
47
  mon.setCursorPos(math.floor(chev2pos[1]-1), math.floor(chev2pos[2]))
48
  mon.write(" > ")
49
  chev3pos = {chevX1, chevY1 }
50
  mon.setCursorPos(math.floor(chev3pos[1]), math.floor(chev3pos[2]+1))
51
  mon.write(" > ")
52
  chev4pos = {chevX1 + ((chevX2 - chevX1) / 2), chevY1 }
53
  mon.setCursorPos(math.floor(chev4pos[1]-1), math.floor(chev4pos[2]))
54
  mon.write(" V ")
55
  chev5pos = {chevX2, chevY1 }
56
  mon.setCursorPos(math.floor(chev5pos[1]-2), math.floor(chev5pos[2])+1)
57
  mon.write(" < ")
58
  chev6pos = {chevX2, chevY1 + ((chevY2 - chevY1) / 2) }
59
  mon.setCursorPos(math.floor(chev6pos[1]-1), math.floor(chev6pos[2]))
60
  mon.write(" < ")
61
  chev7pos = {chevX2, chevY2 }
62
  mon.setCursorPos(math.floor(chev7pos[1]-2), math.floor(chev7pos[2]-1))
63
  mon.write(" < ")
64
  chev8pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
65
  mon.setCursorPos(math.floor(chev8pos[1]-1), math.floor(chev8pos[2]))
66
  mon.write("   ")
67
--  chev9pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
68
--  mon.setCursorPos(math.floor(chev8pos[1]-1), chevY1 + ((chevY2 - chevY1) / 2))
69
--  mon.write(" 9 ")
70
  mon.setBackgroundColor(colors.black)
71
  mon.setCursorPos(x/2 - 4, y/2 - 1)
72
  mon.write("           ")
73
  mon.setCursorPos(x/2-1, y/2+4)
74
  mon.write("     ")
75
end
76
77
function drawChev( chevInfo )
78
  mon.setBackgroundColor(colors.gray)
79
  x,y = mon.getSize()
80
  chevX1 = x/3
81
  chevX2 = x/3*2+1
82
  chevY1 = y/3-2
83
  chevY2 = y/3*2 +2
84
  if chevInfo[1] == 1 then
85
    chev1pos = {chevX1, chevY2 }
86
    mon.setBackgroundColor(colors.gray)
87
    mon.setCursorPos(math.floor(chev1pos[1]), math.floor(chev1pos[2])-1)
88
    mon.write(" "..chevInfo[2].." ")
89
  elseif chevInfo[1] == 2 then
90
    chev2pos = {chevX1, chevY1 + ((chevY2 - chevY1) / 2) }
91
    mon.setCursorPos(math.floor(chev2pos[1]-1), math.floor(chev2pos[2]))
92
    mon.write(" "..chevInfo[2].." ")
93
  elseif chevInfo[1] == 3 then
94
    chev3pos = {chevX1, chevY1 }
95
    mon.setCursorPos(math.floor(chev3pos[1]), math.floor(chev3pos[2]+1))
96
    mon.write(" "..chevInfo[2].." ")
97
  elseif chevInfo[1] == 4 then
98
    chev4pos = {chevX1 + ((chevX2 - chevX1) / 2), chevY1 }
99
    mon.setCursorPos(math.floor(chev4pos[1]-1), math.floor(chev4pos[2]))
100
    mon.write(" "..chevInfo[2].." ")
101
  elseif chevInfo[1] == 5 then
102
    chev5pos = {chevX2, chevY1 }
103
    mon.setCursorPos(math.floor(chev5pos[1]-2), math.floor(chev5pos[2])+1)
104
    mon.write(" "..chevInfo[2].." ")
105
  elseif chevInfo[1] == 6 then
106
    chev6pos = {chevX2, chevY1 + ((chevY2 - chevY1) / 2) }
107
    mon.setCursorPos(math.floor(chev6pos[1]-1), math.floor(chev6pos[2]))
108
    mon.write(" "..chevInfo[2].." ")
109
  elseif chevInfo[1] == 7 then
110
    chev7pos = {chevX2, chevY2 }
111
    mon.setCursorPos(math.floor(chev7pos[1]-2), math.floor(chev7pos[2]-1))
112
    mon.write(" "..chevInfo[2].." ")
113
  elseif chevInfo[1] == 8 then
114
    chevsave8 = chevInfo[2]
115
  elseif chevInfo[1] == 9 then
116
    chev8pos = {chevX1 + ((chevX2 - chevX1) /2), chevY2 }
117
    mon.setCursorPos(math.floor(chev8pos[1]-1), math.floor(chev8pos[2]))
118
    mon.write(""..chevsave8.." "..chevInfo[2])
119
  mon.setBackgroundColor(colors.black)
120
end
121
122
end
123
124
function drawSgStatus(status) -- draws stargate status
125
  if status ~= "Idle" then
126
  term.setCursorPos(1,2)
127
  write(status) --needed for sting length because sting.len() won't work with stargateStatus()
128
  xc, yc = term.getCursorPos()
129
  term.clear()
130
  term.setCursorPos(1,2)
131
  write("> ")
132
  if xc%2 == 1 then
133
    xc = xc+1
134
	even = true
135
  else
136
    even = false
137
  end
138
  mon.setBackgroundColor(colors.black)
139
  if status == "Connected" then
140
    mon.setTextColor(colors.lightBlue)
141
  elseif status == "Dialling" then
142
    mon.setTextColor(colors.orange)
143
  else
144
    mon.setTextColor(colors.green)
145
  end
146
  x,y = mon.getSize()
147
  mon.setCursorPos((x/2+1) - 6, y/2+2)
148
  mon.write("          ")
149
  mon.setCursorPos((x/2+1) - (xc/2-1), y/2+2)
150
  mon.write(status)
151
  if even == true then
152
    mon.write(".")
153
  end
154
  end
155
end
156
157
158
function drawLocalAddress() -- draws the address stargate being controlled 
159
  x,y = mon.getSize()
160
  mon.setBackgroundColor(colors.black)
161
  mon.setTextColor(colors.lightGray)
162
  mon.setCursorPos(x/2-7, 1)
163
  mon.write("Stargate Address:")
164
  
165
end
166
167
function drawDial() -- draws the button to access the dialing menu
168
  x,y = mon.getSize()
169
  state, int = sg.stargateState()
170
  for yc = y-3, y-1 do
171
    for xc = x/2-15, x/2 do
172
	  if state == "Idle" then
173
	    mon.setBackgroundColor(colors.lightGray)
174
	  else
175
	    mon.setBackgroundColor(colors.gray)
176
	  end
177
	  mon.setCursorPos(xc,yc)
178
	  mon.write(" ")
179
	end
180
  end
181
  mon.setCursorPos(x/2-7, y-2)
182
  mon.setTextColor(colors.black)
183
  mon.write("List")
184
end
185
186
function drawTerm() -- draws the button to terminate the stargate connection to another gate
187
  x,y = mon.getSize()
188
  state, int = sg.stargateState()
189
  for yc = y-3, y-1 do
190
    for xc = x/2+2, x/2+15 do
191
	  if state == "Connected" or state == "Connecting" or state == "Dialling" then
192
	    mon.setBackgroundColor(colors.lightGray)
193
	  else
194
	    mon.setBackgroundColor(colors.gray)
195
	  end
196
	  mon.setCursorPos(xc,yc)
197
	  mon.write(" ")
198
	end
199
  end
200
  mon.setCursorPos(x/2+3, y-2)
201
  mon.setTextColor(colors.black)
202
  mon.write("Eteindre")
203
end 
204
205
206
function drawHome() -- draws the home screen
207
  mon.setBackgroundColor(colors.black)
208
  x,y = mon.getSize()
209
  mon.clear()
210
  mon.setCursorPos(1,y)
211
  mon.setTextColor(colors.gray)
212
  mon.setBackgroundColor(colors.black)
213
    
214
  drawChevrons()
215
  status, int = sg.stargateState()
216
  drawSgStatus(tostring(status))
217
  
218
  
219
  drawLocalAddress()
220
  
221
  drawDial()
222
  mon.setCursorBlink(false)
223
  drawTerm()
224
end
225
226
function drawBookmarksPage()
227
  mon.setBackgroundColor(colors.black)
228
  mon.clear()
229
  mon.setTextColor(colors.black)
230
  x,y = mon.getSize()
231
  for yc = 1,y-3 do
232
    if yc%2 == 1 then
233
      mon.setBackgroundColor(colors.lightBlue)
234
	else
235
	  mon.setBackgroundColor(colors.lightGray)
236
	end
237
	for xc = 1,x do
238
	  mon.setCursorPos(xc, yc)
239
	  mon.write(" ")
240
	end
241
  end
242
  for i= 1,y do
243
  	if i%2 == 1 then
244
      mon.setBackgroundColor(colors.lightBlue)
245
	else
246
	  mon.setBackgroundColor(colors.lightGray)
247
	end
248
    if fs.exists(tostring(i)) then
249
      file = fs.open(tostring(i),"r")
250
	  bookmark = textutils.unserialize(file.readAll())
251
	  file.close()
252
	  mon.setCursorPos(1,i)
253
	  for k,v in pairs(bookmark) do
254
	    if k == "name" then
255
	      mon.write(v)
256
		  mon.setCursorPos(x/2, i)
257
		  mon.write(bookmark.address)
258
	      mon.setCursorPos(x,i)
259
	    end
260
	  end
261
	end
262
  end
263
  mon.setCursorPos(x/2, y-1)
264
  mon.setBackgroundColor(colors.black)
265
  mon.setTextColor(colors.white)
266
  mon.write("BACK")
267
end
268
269
270
function inputPage(type)
271
  mon.clear()
272
  term.redirect(mon)
273
  term.setBackgroundColor(colors.lightGray)
274
  term.clear()
275
  x,y = term.getSize()
276
  term.setCursorPos(x/2-8, y/2-2)
277
  print("Set an address name")
278
  term.setCursorPos(x/2 - 4, y/2)
279
  print("         ")
280
  term.setCursorPos(x/2 - 4, y/2)
281
  nameInput = read()
282
  addressInput = "nil"
283
  term.setBackgroundColor(colors.lightGray)
284
  term.clear()
285
  term.setCursorPos(x/2-9, y/2-4)
286
  print("Enter Stargate address")
287
  if type == "secEntry" then
288
    term.setCursorPos(x/2-10, y/2-2)
289
    print("DO NOT ENTER ANY HYPHONS")
290
  end
291
  term.setBackgroundColor(colors.black)
292
  term.setCursorPos(x/2 - 5, y/2)
293
  print("           ")
294
  term.setCursorPos(x/2 - 5, y/2)
295
  addressInput = string.upper(read())
296
  newGate ={name = nameInput, address = addressInput}
297
  term.redirect(term.native())
298
  return newGate
299
end
300
301
function drawRemoteAddress(name)
302
  mon.setBackgroundColor(colors.black)
303
  mon.setTextColor(colors.blue)
304
  x,y = mon.getSize()
305
  mon.setCursorPos((x/2+1) - string.len(name)/2, y/2-2)
306
  mon.write(name)
307
end
308
309
310
311
312
if fs.exists("currentSec") then -- checks to see if there's list of gates stored for security reasons
313
  file = fs.open("currentSec", "r")
314
  currentSec = file.readAll()
315
  file.close()
316
else
317
  currentSec = "NONE"
318
end
319
mon.setTextScale(1)
320
drawHome()
321
while true do
322
  event, param1, param2, param3 = os.pullEvent()
323
  if event == "monitor_touch" then
324
    x,y = mon.getSize()
325
    if param2 > x/2-15 and param2 <= x/2 and param3 >= y-3 and param3 <= y-1 then -- Click has opened dial menu
326
	  status, int = sg.stargateState()
327
		if status == "Idle" then
328
			while true do
329
				drawBookmarksPage()
330
				event, param1, param2, param3 = os.pullEvent()
331
				if event == "monitor_touch" then
332
					if param3 >= y-2 then -- user clicked back
333
						drawHome()
334
						break
335
					   
336
						
337
					else -- user has clicked on a bookmark
338
						if fs.exists(tostring(param3)) then
339
						  file = fs.open(tostring(param3), "r")
340
						  gateData = textutils.unserialize(file.readAll()) -- GATE DATA VARIABLE!!!
341
						  file.close()
342
						  drawHome()
343
						  for k,v in pairs(gateData) do
344
							if k == "address" then
345
							  ok, result = pcall(sg.dial, v)
346
							  if ok then
347
								chat.say("Attention, ne reste pas en face de la porte quand elle s'ouvre ou tu risque de mourrir")	
348
							
349
									
350
										
351
									drawRemoteAddress(gateData.name)
352
								status, int = sg.stargateState()
353
								drawSgStatus(status)								
354
							  else
355-
							for k,v in ipairs(gateData) do
355+
356-
								if k == "name"	then
356+
357-
									name = v
357+
358-
									mon.setTextColor(colors.orange)
358+
								
359-
									drawRemoteAddress(name)														
359+
360-
									print(k,v)
360+
361-
								end  
361+
362
							
363-
							sleep(.5)
363+
364
						  
365
						break
366
						end
367
					end
368
				else
369
				  drawHome()
370
				  break
371
				end
372
			end
373
		end
374
		
375
	  
376
	
377
	elseif param2 > x/2+2 and param2 <= x/2+15 and param3 >= y-3 and param3 <= y-1 then -- user clicked TERM
378
	  ok, result = pcall(sg.disconnect)
379
	  drawChevrons()
380
	  drawRemoteAddress(" ")
381
	end
382
	
383
  elseif event == "sgDialIn" then
384
	mon.setTextColor(colors.orange)
385
	
386
	if fs.exists("currentSec") then
387
      file = fs.open("currentSec", "r")
388
	  currentSec = file.readAll()
389
	  file.close()
390
	end
391
	if fs.exists("secList") then
392
	  file = fs.open("secList", "r")
393
	  secList = textutils.unserialize(file.readAll())
394
	  for k,v in pairs(secList) do
395
	    address = v.address
396
	    if string.sub(v.address,1,7) == param2 or v.address == param2 then
397
	      
398
		  secGate = true
399
	    end
400
	  end
401
	end
402
   
403
	
404
   
405
  elseif event == "sgStargateStateChange" or "sgChevronEngaged" then
406
    drawDial()
407
   
408
    drawTerm()
409
	status, int = sg.stargateState()
410
    drawSgStatus(tostring(status))
411
	if status == "idle" then
412
	  isConnected = false
413
	else
414
	  isConnected = true
415
	end
416
	if event == "sgChevronEngaged" then
417
	  mon.setTextColor(colors.orange)
418
	  drawChev({param2, param3})
419
	  if param2 == 1 then
420
	    dialling = {}
421
	  end
422
	  table.insert(dialling, param2, param3)
423
	  --drawRemoteAddress()
424
	 
425-
	  chat.say("Attention, ne reste pas en face de la porte quand elle s'ouvre ou tu risque de mourrir")
425+
	elseif param2 == "Idle" then
426-
	elseif param2 == "Idle" then  
426+
427-
    drawChevrons()
427+
428
	elseif param2 == "Connected" then
429
	
430
	  mon.setTextColor(colors.lightBlue)
431-
      drawRemoteAddress()
431+
432
	    drawChev({k,v})
433
	  end
434
	  sg.sendMessage(sg.irisState())
435
	end
436
  end
437
  end