View difference between Paste ID: cza1nHig and ZWQeRdfw
SHOW: | | - or go back to the newest paste.
1
if(fs.exists("widget") == false) then
2
    shell.run("pastebin get 6JsWa9Bj widget")
3
    term.clear()
4
end
5
os.loadAPI("widget")
6
 
7
if not term.isColor() then
8
  print("Advanced computer required")
9
  error()
10
end
11
 
12
local radar
13
local sides = peripheral.getNames()
14
for key,side in pairs(sides) do
15
  if peripheral.getType(side) == "warpdriveRadar" then
16
    print("Radar found on " .. side)
17
    radar = peripheral.wrap(side)
18
  end
19
end
20
if radar == nil or radar.isInterfaced() == nil then
21
  showErrorAndExit("No radar detected")
22
end
23
 
24
local range
25
if(fs.exists("range.settings") == false) then
26
    file = fs.open("range.settings", "w")
27
    file.write("100")
28
    file.close()
29
end
30
if(fs.exists("range.settings")) then
31
    file = fs.open("range.settings", "r")
32
    range = tonumber(file.readAll())
33
    file.close()
34
end
35
 
36
shipName = {}
37
shipX = {}
38
shipY = {}
39
shipZ = {}
40
shipMass = {}
41
shipRange = {}
42
 
43
for i=0, 360 do
44
    shipName[i]=""
45
    shipX[i]=0
46
    shipY[i]=0
47
    shipZ[i]=0
48
    shipMass[i]=0
49
    shipRange[i]=0
50
end
51
 
52
function interface(page) 
53
    term.clear()
54
    title = widget.newLabel("Aegis Scanning System MK2")
55
    row1 = widget.newRow()
56
    row1:add(title)
57
    ship1 = widget.newButton(shipName[0+(page*18)])
58
    ship2 = widget.newButton(shipName[1+(page*18)])
59
    ship3 = widget.newButton(shipName[2+(page*18)])
60
    row2 = widget.newRow()
61
    row2:add(ship1)
62
    function ship1.onClick(button)
63
        shipData(0+page)
64
    end
65
    row2:add(ship2)
66
    function ship2.onClick(button)
67
        shipData(1+page)
68
    end
69
    row2:add(ship3)
70
    function ship3.onClick(button)
71
        shipData(2+page)
72
    end
73
    ship4 = widget.newButton(shipName[3+(page*18)])
74
    ship5 = widget.newButton(shipName[4+(page*18)])
75
    ship6 = widget.newButton(shipName[5+(page*18)])
76
    row3 = widget.newRow()
77
    row3:add(ship4)
78
    function ship4.onClick(button)
79
        shipData(3+page)
80
    end
81
    row3:add(ship5)
82
    function ship5.onClick(button)
83
        shipData(4+page)
84
    end
85
    row3:add(ship6)
86
    function ship6.onClick(button)
87
        shipData(5+page)
88
    end
89
    ship7 = widget.newButton(shipName[6+(page*18)])
90
    ship8 = widget.newButton(shipName[7+(page*18)])
91
    ship9 = widget.newButton(shipName[8+(page*18)])
92
    row4 = widget.newRow()
93
    row4:add(ship7)
94
    function ship7.onClick(button)
95
        shipData(6+page)
96
    end
97
    row4:add(ship8)
98
    function ship8.onClick(button)
99
        shipData(7+page)
100
    end
101
    row4:add(ship9)
102
    function ship9.onClick(button)
103
        shipData(8+page)
104
    end
105
    ship10 = widget.newButton(shipName[9+(page*18)])
106
    ship11 = widget.newButton(shipName[10+(page*18)])
107
    ship12 = widget.newButton(shipName[11+(page*18)])
108
    row5 = widget.newRow()
109
    row5:add(ship10)
110
    function ship10.onClick(button)
111
        shipData(9+page)
112
    end
113
    row5:add(ship11)
114
    function ship11.onClick(button)
115
        shipData(10+page)
116
    end
117
    row5:add(ship12)
118
    function ship12.onClick(button)
119
        shipData(11+page)
120
    end
121
    ship13 = widget.newButton(shipName[12+(page*18)])
122
    ship14 = widget.newButton(shipName[13+(page*18)])
123
    ship15 = widget.newButton(shipName[14+(page*18)])
124
    row6 = widget.newRow()
125
    row6:add(ship13)
126
    function ship13.onClick(button)
127
        shipData(12+page)
128
    end
129
    row6:add(ship14)
130
    function ship14.onClick(button)
131
        shipData(13+page)
132
    end
133
    row6:add(ship15)
134
    function ship15.onClick(button)
135
        shipData(14+page)
136
    end
137
    ship16 = widget.newButton(shipName[15+(page*18)])
138
    ship17 = widget.newButton(shipName[16+(page*18)])
139
    ship18 = widget.newButton(shipName[17+(page*18)])
140
    row7 = widget.newRow()
141
    row7:add(ship16)
142
    function ship16.onClick(button)
143
        shipData(15+page)
144
    end
145
    row7:add(ship17)
146
    function ship17.onClick(button)
147
        shipData(16+page)
148
    end
149
    row7:add(ship18)
150
    function ship18.onClick(button)
151
        shipData(17+page)
152
    end
153
    lastPage = widget.newButton("<--")
154
    row8 = widget.newRow()
155
    row8:add(lastPage)
156
    function lastPage.onClick(button)
157
        if(page ~= 0) then
158
			interface(page-1)
159
		end 
160
    end
161
	pageNum = widget.newButton("Page "..page+1)
162
    row8:add(pageNum)
163
    nextPage = widget.newButton("-->")
164
    row8:add(nextPage)
165
    function nextPage.onClick(button)
166
		if(page ~= 19) then
167
			interface(page+1)
168
		end
169
    end
170
    row20 = widget.newRow()
171
    sRange = widget.newButton("Range Settings")
172
    function sRange.onClick(button)
173
        rangeSettings()
174
    end
175
    startScan = widget.newButton("Scan")
176
    function startScan.onClick(button)
177
        scan()
178
    end
179
    row20:add(sRange)
180
    row20:add(startScan)
181
    main = widget.newColumn()
182
    main:add(row1)
183
    main:add(row2)
184
    main:add(row3)
185
    main:add(row4)
186
    main:add(row5)
187
    main:add(row6)
188
    main:add(row7)
189
    main:add(row8)
190
    main:add(row20)
191
    main:run()
192
end
193
 
194
function scan()
195
    radar.radius(range)
196
    local success, result = radar.getEnergyRequired()
197
    if not success then
198
        term.clear()
199
        title = widget.newLabel("Aegis Scanning System MK2")
200
        row1 = widget.newRow()
201
        row1:add(title)
202
        row2 = widget.newRow()
203
        energy = widget.newLabel("Not enough Energy!")
204
        row3 = widget.newRow()
205
        row3:add(energy)
206
        row4 = widget.newRow()
207
        row5 = widget.newRow()
208
        row6 = widget.newRow()
209
        row7 = widget.newRow()
210
        row8 = widget.newRow()
211
        okay = widget.newButton("Okay!")
212
        function okay.onClick(button)
213
            interface(0)
214
        end
215
        row20 = widget.newRow()
216
        row20:add(okay)
217
        main = widget.newColumn()
218
        main:add(row1)
219
        main:add(row2)
220
        main:add(row3)
221
        main:add(row4)
222
        main:add(row5)
223
        main:add(row6)
224
        main:add(row7)
225
        main:add(row8)
226
        main:add(row20)
227
        main:run()
228
    end
229
    local energyRequired = result
230
    local energyStored, _, energyUnits = radar.getEnergyStatus()
231
    if energyStored < energyRequired then
232
            term.clear()
233
        title = widget.newLabel("Aegis Scanning System MK1")
234
        row1 = widget.newRow()
235
        row1:add(title)
236
        row2 = widget.newRow()
237
        energy = widget.newLabel("Not enough Energy!")
238
        row3 = widget.newRow()
239
        row3:add(energy)
240
        row4 = widget.newRow()
241
        row5 = widget.newRow()
242
        row6 = widget.newRow()
243
        row7 = widget.newRow()
244
        row8 = widget.newRow()
245
        okay = widget.newButton("Okay!")
246
        function okay.onClick(button)
247
            interface(0)
248
        end
249
        row20 = widget.newRow()
250
        row20:add(okay)
251
        main = widget.newColumn()
252
        main:add(row1)
253
        main:add(row2)
254
        main:add(row3)
255
        main:add(row4)
256
        main:add(row5)
257
        main:add(row6)
258
        main:add(row7)
259
        main:add(row8)
260
        main:add(row20)
261
        main:run()
262
    end
263
    local scanDuration = radar.getScanDuration()
264
    radar.start()
265
    os.sleep(0.5)
266
     
267
    print("        Scanning... (" .. scanDuration .. " s)")
268
    os.sleep(scanDuration)
269
    
270
    local delay = 0
271
    local count
272
    repeat
273
      count = radar.getResultsCount()
274
      os.sleep(0.1)
275
      delay = delay + 1
276
    until (count ~= nil and count ~= -1) or delay > 10
277
    
278
    if count ~= nil and count > 0 then
279
        term.clear()
280
        term.setCursorPos(1, 1)
281
        filtered = 0
282
        for i=0, count-1 do
283
            local success, type, name, x, y, z, mass = radar.getResult(i)
284
            local ssuccess, dimension, sx, sy, sz, system = radar.getGlobalPosition()
285
            if success then
286
                if name ~= "" then
287
                    shipName[i-filtered] = name
288
                    shipX[i-filtered] = x
289
                    shipY[i-filtered] = y
290
                    shipZ[i-filtered] = z
291
                    shipMass[i-filtered] = mass
292
                    shipRange[i-filtered] = math.floor(math.sqrt((x-sx)^2+(z-sz)^2))
293
                else
294
                    filtered = filtered+1
295
                end
296
            else
297
                showError("Error " .. type)
298
            end
299
        end
300
        interface(0)
301
    end
302
end
303
 
304
function rangeSettings()
305
    term.clear()
306
    title = widget.newLabel("Aegis Scanning System MK2")
307
    row1 = widget.newRow()
308
    row1:add(title)
309
    row2 = widget.newRow()
310
    cRange = widget.newLabel("Current Range: "..range)
311
    row3 = widget.newRow()
312
    row3:add(cRange)
313
    slider = widget.newSlider(1, 40)
314
    sValue = slider.value
315
    row4 = widget.newRow()
316
    row4:add(slider)
317
    row5 = widget.newRow()
318
    row6 = widget.newRow()
319
    row7 = widget.newRow()
320
    accept = widget.newButton("Accept")
321
    function accept.onClick(button)
322-
        range = slider.value*50
322+
        range = slider.value*25
323
        if(fs.exists("range.settings")) then
324
            file = fs.open("range.settings", "w")
325-
            file.write(slider.value*50)
325+
            file.write(slider.value*25)
326
            file.close()
327
        end
328
        interface(0)
329
    end
330
    decline = widget.newButton("Decline")
331
    function decline.onClick(button)
332
        interface(0)
333
    end
334
    row8 = widget.newRow()
335
    row8:add(accept)
336
    row8:add(decline)
337
    row20 = widget.newRow()
338
    main = widget.newColumn()
339
    main:add(row1)
340
    main:add(row2)
341
    main:add(row3)
342
    main:add(row4)
343
    main:add(row5)
344
    main:add(row6)
345
    main:add(row7)
346
    main:add(row8)
347
    main:add(row20)
348
    main:run()
349
end
350
 
351
function shipData(num)
352
    if(shipName[num] ~= "") then
353
        term.clear()
354
        title = widget.newLabel("Aegis Scanning System MK2")
355
        row1 = widget.newRow()
356
        row1:add(title)
357
        row2 = widget.newRow()
358
        sName = widget.newLabel("Name: "..shipName[num])
359
        row3 = widget.newRow()
360
        row2:add(sName)
361
        sX = widget.newLabel("Position X: "..shipX[num])
362
        row4 = widget.newRow()
363
        row3:add(sX)
364
        sY = widget.newLabel("Position Y: "..shipY[num])
365
        row5 = widget.newRow()
366
        row4:add(sY)
367
        sZ = widget.newLabel("Position Z: "..shipZ[num])
368
        row6 = widget.newRow()
369
        row5:add(sZ)
370
        sMass = widget.newLabel("Mass: "..shipMass[num].." tons")
371
        row7 = widget.newRow()
372
        row6:add(sMass)
373
        sRange = widget.newLabel("Distance: "..shipRange[num].."m")
374
        row8 = widget.newRow()
375
        row7:add(sRange)
376
        okay = widget.newButton("Okay!")
377
        function okay.onClick(button)
378
            interface(0)
379
        end
380
        row20 = widget.newRow()
381
        row20:add(okay)
382
        main = widget.newColumn()
383
        main:add(row1)
384
        main:add(row2)
385
        main:add(row3)
386
        main:add(row4)
387
        main:add(row5)
388
        main:add(row6)
389
        main:add(row7)
390
        main:add(row8)
391
        main:add(row20)
392
        main:run()
393
    else 
394
        term.clear()
395
        title = widget.newLabel("Aegis Scanning System MK2")
396
        row1 = widget.newRow()
397
        row1:add(title)
398
        row2 = widget.newRow()
399
        noShips = widget.newLabel("No Ship found!")
400
        row3 = widget.newRow()
401
        row3:add(noShips)
402
        row4 = widget.newRow()
403
        row5 = widget.newRow()
404
        row6 = widget.newRow()
405
        row7 = widget.newRow()
406
        row8 = widget.newRow()
407
        okay = widget.newButton("Okay!")
408
        function okay.onClick(button)
409
            interface(0)
410
        end
411
        row20 = widget.newRow()
412
        row20:add(okay)
413
        main = widget.newColumn()
414
        main:add(row1)
415
        main:add(row2)
416
        main:add(row3)
417
        main:add(row4)
418
        main:add(row5)
419
        main:add(row6)
420
        main:add(row7)
421
        main:add(row8)
422
        main:add(row20)
423
        main:run()
424
    end
425
end
426
 
427
interface(0)