View difference between Paste ID: 2882fQRy and fk1kdQDD
SHOW: | | - or go back to the newest paste.
1
--  BigReactor Control
2
--  by jaranvil aka jared314
3
--  and reghzy/carrot ;)
4
--
5
--  feel free to use and/or modify this code
6
--
7
-----------------------------------------------
8
--Reactor Control - Version History
9
--
10
--  Version 2.3.1 - 14/08/2021
11
-- 	  - Removed monitor interactions because they crash the server sometimes for a complex reason
12
--    - First update in awhile 
13
--    - lots of minor fixes and improvments 
14
--    - New loading and setup search with automated peripheral search 
15
--    - Changes to the update methods
16
--    - Except new updates soon
17
18
19
-----------------------------------------------
20
21
local version = 2.3
22
--is auto power enabled 
23
local auto_string = false 
24
--auto on value
25
local on = 0 
26
--auto off value
27
local off = 99 
28
--is auto control rods enabled 
29
local auto_rods = false 
30
--control rod auto value
31
local auto_rf = 0 
32
33
--peripherals
34
local reactor
35
local mon
36
37
--monitor size
38
local monX
39
local monY
40
41
term.clear()
42
-------------------FORMATTING-------------------------------
43
function clear()
44
  mon.setBackgroundColor(colors.black)
45
  mon.clear()
46
  mon.setCursorPos(1,1)
47
end
48
49
--display text on computer's terminal screen
50
function draw_text_term(x, y, text, text_color, bg_color)
51
  term.setTextColor(text_color)
52
  term.setBackgroundColor(bg_color)
53
  term.setCursorPos(x,y)
54
  write(text)
55
end
56
57
--display text text on monitor, "mon" peripheral
58
function draw_text(x, y, text, text_color, bg_color)
59
  mon.setBackgroundColor(bg_color)
60
  mon.setTextColor(text_color)
61
  mon.setCursorPos(x,y)
62
  mon.write(text)
63
end
64
65
--draw line on computer terminal
66
function draw_line(x, y, length, color)
67
    mon.setBackgroundColor(color)
68
    mon.setCursorPos(x,y)
69
    mon.write(string.rep(" ", length))
70
end
71
72
--draw line on computer terminal
73
function draw_line_term(x, y, length, color)
74
    term.setBackgroundColor(color)
75
    term.setCursorPos(x,y)
76
    term.write(string.rep(" ", length))
77
end
78
79
--create progress bar
80
--draws two overlapping lines
81
--background line of bg_color 
82
--main line of bar_color as a percentage of minVal/maxVal
83
function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
84
  draw_line(x, y, length, bg_color) --backgoround bar
85
  local barSize = math.floor((minVal/maxVal) * length) 
86
  draw_line(x, y, barSize, bar_color) --progress so far
87
end
88
89
--same as above but on the computer terminal
90
function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
91
  draw_line_term(x, y, length, bg_color) --backgoround bar
92
  local barSize = math.floor((minVal/maxVal) * length) 
93
  draw_line_term(x, y, barSize, bar_color)  --progress so far
94
end
95
96
--create button on monitor
97
function button(x, y, length, text, txt_color, bg_color)
98
  draw_line(x, y, length, bg_color)
99
  draw_text((x+2), y, text, txt_color, bg_color)
100
end
101
102
--header and footer bars on monitor
103
function menu_bar()
104
  draw_line(1, 1, monX, colors.blue)
105
  draw_text(2, 1, "Power    Tools    Settings", colors.white, colors.blue)
106
  draw_line(1, 19, monX, colors.blue)
107
  draw_text(2, 19, "     Reactor Control", colors.white, colors.blue)
108
end
109
110
--dropdown menu for power options
111
function power_menu()
112
  draw_line(1, 2, 9, colors.gray)
113
  draw_line(1, 3, 9, colors.gray)
114
  draw_line(1, 4, 9, colors.gray)
115
  if active then 
116
    draw_text(2, 2, "ON", colors.lightGray, colors.gray)
117
    draw_text(2, 3, "OFF", colors.white, colors.gray)
118
  else
119
    draw_text(2, 2, "ON", colors.white, colors.gray)
120
    draw_text(2, 3, "OFF", colors.lightGray, colors.gray)
121
  end
122
  draw_text(2, 4, "Auto", colors.white, colors.gray)
123
end
124
125
--dropbox menu for tools
126
function tools_menu()
127
  draw_line(10, 2, 14, colors.gray)
128
  draw_line(10, 3, 14, colors.gray)
129
  draw_line(10, 4, 14, colors.gray)
130
  draw_line(10, 5, 14, colors.gray)
131
  draw_text(11, 2, "Control Rods", colors.white, colors.gray)
132
  draw_text(11, 3, "Efficiency", colors.white, colors.gray) 
133
  draw_text(11, 4, "Fuel", colors.white, colors.gray)
134
  draw_text(11, 5, "Waste", colors.white, colors.gray)
135
end
136
137
--dropdown menu for settings
138
function settings_menu()
139
  draw_line(12, 2, 18, colors.gray)
140
  draw_line(12, 3, 18, colors.gray)
141
  draw_text(13, 2, "Check for Updates", colors.white, colors.gray)
142
  draw_text(13, 3, "Reset peripherals", colors.white, colors.gray)
143
end
144
145
--basic popup screen with title bar and exit button 
146
function popup_screen(y, title, height)
147
  clear()
148
  menu_bar()
149
150
  draw_line(4, y, 22, colors.blue)
151
  draw_line(25, y, 1, colors.red)
152
153
  for counter = y+1, height+y do
154
    draw_line(4, counter, 22, colors.white)
155
  end
156
157
  draw_text(25, y, "X", colors.white, colors.red)
158
  draw_text(5, y, title, colors.white, colors.blue)
159
end
160
161
--write settings to config file
162
function save_config()
163
  sw = fs.open("config.txt", "w")   
164
    sw.writeLine(version)
165
    sw.writeLine(auto_string)
166
    sw.writeLine(on)
167
    sw.writeLine(off)
168
    sw.writeLine(auto_rods)
169
    sw.writeLine(auto_rf)
170
  sw.close()
171
end
172
173
--read settings from file
174
function load_config()
175
  sr = fs.open("config.txt", "r")
176
    version = tonumber(sr.readLine())
177
    auto_string = sr.readLine()
178
    on = tonumber(sr.readLine())
179
    off = tonumber(sr.readLine())
180
    auto_rods = sr.readLine()
181
    auto_rf = tonumber(sr.readLine())
182
  sr.close()
183
end
184
185
------------------------END FORMATTING--------------------------
186
187
--
188
function homepage()
189
  while true do
190
    clear()
191
    menu_bar()
192
    terminal_screen()
193
194
    energy_stored = reactor.getEnergyStored()
195
    
196
    --------POWER STAT--------------
197
    draw_text(2, 3, "Power:", colors.yellow, colors.black)
198
    active = reactor.getActive()
199
    if active then
200
      draw_text(10, 3, "ONLINE", colors.lime, colors.black)
201
    else
202
      draw_text(10, 3, "OFFLINE", colors.red, colors.black)
203
    end
204
205
    -----------FUEL---------------------
206
    draw_text(2, 5, "Fuel Level:", colors.yellow, colors.black)
207
    local maxVal = reactor.getFuelAmountMax()
208
    local minVal = reactor.getFuelAmount() 
209
    local percent = math.floor((minVal/maxVal)*100)
210
    draw_text(15, 5, percent.."%", colors.white, colors.black)
211
212
    if percent < 25 then
213
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.red, colors.gray)
214
    else if percent < 50 then
215
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.orange, colors.gray)
216
    else if percent < 75 then 
217
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.yellow, colors.gray)
218
    else if percent <= 100 then
219
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.lime, colors.gray)
220
    end
221
    end
222
    end
223
    end
224
225
    -----------ROD HEAT---------------
226
    draw_text(2, 8, "Fuel Temp:", colors.yellow, colors.black)
227
    local maxVal = 2000
228
    local minVal = math.floor(reactor.getFuelTemperature())
229
230
    if minVal < 500 then
231
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.lime, colors.gray)
232
    else if minVal < 1000 then
233
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.yellow, colors.gray)
234
    else if minVal < 1500 then  
235
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.orange, colors.gray)
236
    else if minVal < 2000 then
237
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.red, colors.gray)
238
    else if minVal >= 2000 then
239
      progress_bar(2, 9, monX-2, 2000, maxVal, colors.red, colors.gray)
240
    end
241
    end
242
    end
243
    end
244
    end
245
246
    draw_text(15, 8, math.floor(minVal).."/"..maxVal, colors.white, colors.black)
247
248
    -----------CASING HEAT---------------
249
    draw_text(2, 11, "Casing Temp:", colors.yellow, colors.black)
250
    local maxVal = 2000
251
    local minVal = math.floor(reactor.getCasingTemperature())
252
    if minVal < 500 then
253
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.lime, colors.gray)
254
    else if minVal < 1000 then
255
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.yellow, colors.gray)
256
    else if minVal < 1500 then  
257
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.orange, colors.gray)
258
    else if minVal < 2000 then
259
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.red, colors.gray)
260
    else if minVal >= 2000 then
261
      progress_bar(2, 12, monX-2, 2000, maxVal, colors.red, colors.gray)
262
    end
263
    end
264
    end
265
    end
266
    end
267
    draw_text(15, 11, math.floor(minVal).."/"..maxVal, colors.white, colors.black)
268
269
    -------------OUTPUT-------------------
270
    if reactor.isActivelyCooled() then
271
272
      draw_text(2, 14, "mB/tick:", colors.yellow, colors.black)
273
      mbt = math.floor(reactor.getHotFluidProducedLastTick())
274
      draw_text(13, 14, mbt.." mB/t", colors.white, colors.black)
275
276
    else
277
278
      draw_text(2, 14, "RF/tick:", colors.yellow, colors.black)
279
      rft = math.floor(reactor.getEnergyProducedLastTick())
280
      draw_text(13, 14, rft.." RF/T", colors.white, colors.black)
281
282
    end
283
284
    ------------STORAGE------------
285
    if reactor.isActivelyCooled() then 
286
287
      draw_text(2, 15, "mB Stored:", colors.yellow, colors.black)
288
      fluid_stored = reactor.getHotFluidAmount()
289
      fluid_max = reactor.getHotFluidAmountMax()
290
      fluid_stored_percent = math.floor((fluid_stored/fluid_max)*100)
291
      draw_text(13, 15, fluid_stored_percent.."% ("..fluid_stored.." mB)", colors.white, colors.black)
292
293
    else
294
295
      draw_text(2, 15, "RF Stored:", colors.yellow, colors.black)
296
      energy_stored_percent = math.floor((energy_stored/10000000)*100)
297
      draw_text(13, 15, energy_stored_percent.."% ("..energy_stored.." RF)", colors.white, colors.black)
298
299
300
    end
301
302
    -------------AUTO CONTROL RODS-----------------------
303
    auto_rods_bool = auto_rods == "true"
304
    insertion_percent = reactor.getControlRodLevel(0)
305
306
    if reactor.isActivelyCooled() then
307
      draw_text(2, 16, "Control Rods:", colors.yellow, colors.black)
308
      draw_text(16, 16, insertion_percent.."%", colors.white, colors.black)
309
    else
310
311
      if auto_rods_bool then
312
        if active then
313
          if rft > auto_rf+50 then
314
            reactor.setAllControlRodLevels(insertion_percent+1)
315
          else if rft < auto_rf-50 then
316
            reactor.setAllControlRodLevels(insertion_percent-1)
317
          end
318
          end
319
        end
320
321
        draw_text(2, 16, "Control Rods:", colors.yellow, colors.black)
322
        draw_text(16, 16, insertion_percent.."%", colors.white, colors.black)
323
        draw_text(21, 16, "(Auto)", colors.red, colors.black)
324
      
325
      else
326
        draw_text(2, 16, "Control Rods:", colors.yellow, colors.black)
327
        draw_text(16, 16, insertion_percent.."%", colors.white, colors.black)
328
      end
329
    end
330
331
332
    -------------AUTO SHUTOFF--------------------------
333
    if reactor.isActivelyCooled() then
334
335
      --i dont know what I should do here
336
337
338
    else
339
      auto = auto_string == "true"
340
      if auto then
341
        if active then
342
          draw_text(2, 17, "Auto off:", colors.yellow, colors.black)
343
          draw_text(13, 17, off.."% RF Stored", colors.white, colors.black)
344
          if energy_stored_percent >= off then
345
            reactor.setActive(false)
346
            call_homepage()
347
          end
348
        else
349
          draw_text(2, 17, "Auto on:", colors.yellow, colors.black)
350
          draw_text(13, 17, on.."% RF Stored", colors.white, colors.black)
351
          if energy_stored_percent <= on then
352
            reactor.setActive(true)
353
            call_homepage()
354
          end
355
        end
356
      else
357
        draw_text(2, 17, "Auto power:", colors.yellow, colors.black)
358
        draw_text(14, 17, "disabled", colors.red, colors.black)
359
      end
360
    end
361
362
    sleep(0.5)
363
  end
364
end
365
366
--------------MENU SCREENS--------------
367
368
--auto power menu
369
function auto_off()
370
371
  auto = auto_string == "true"
372
  if auto then --auto power enabled
373
374
    popup_screen(3, "Auto Power", 11)
375
    draw_text(5, 5, "Enabled", colors.lime, colors.white)
376
    draw_text(15, 5, " disable ", colors.white, colors.black)
377
    
378
    draw_text(5, 7, "ON when storage =", colors.gray, colors.white)
379
    draw_text(5, 8, " - ", colors.white, colors.black)
380
    draw_text(13, 8, on.."% RF", colors.black, colors.white)
381
    draw_text(22, 8, " + ", colors.white, colors.black)
382
383
    draw_text(5, 10, "OFF when storage =", colors.gray, colors.white)
384
    draw_text(5, 11, " - ", colors.white, colors.black)
385
    draw_text(13, 11, off.."% RF", colors.black, colors.white)
386
    draw_text(22, 11, " + ", colors.white, colors.black)
387
388
    draw_text(11, 13, " Save ", colors.white, colors.black)
389
390
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
391
392
    --disable auto
393
    if yPos == 5 then
394
      if xPos >= 15 and xPos <= 21 then
395
        auto_string = "false"
396
        save_config()
397
        auto_off()
398
      else
399
        auto_off()
400
      end
401
    end
402
403
    --increase/decrease auto on %
404
    if yPos == 8 then
405
      if xPos >= 5 and xPos <= 8 then
406
        previous_on = on
407
        on = on-1
408
      end
409
      if xPos >= 22 and xPos <= 25 then
410
        previous_on = on
411
        on = on+1
412
      end
413
    end
414
415
    --increase/decrease auto off %
416
    if yPos == 11 then
417
      if xPos >= 5 and xPos <= 8 then
418
        previous_off = off
419
        off = off-1
420
      end
421
      if xPos >= 22 and xPos <= 25 then
422
        previous_off = off
423
        off = off+1
424
      end
425
    end
426
427
    if on < 0 then on = 0 end
428
    if off >99 then off = 99 end
429
430
    if on == off or on > off then
431
      on = previous_on
432
      off = previous_off
433
      popup_screen(5, "Error", 6)
434
      draw_text(5, 7, "Auto On value must be", colors.black, colors.white)
435
      draw_text(5, 8, "lower then auto off", colors.black, colors.white)
436
      draw_text(11, 10, "Okay", colors.white, colors.black)
437
      local event, side, xPos, yPos = os.pullEvent("monitor_touch")
438
439
      auto_off()
440
    end
441
442
    --Okay button
443
    if yPos == 13 and xPos >= 11 and xPos <= 17 then 
444
      save_config()
445
      call_homepage()
446
    end
447
448
    --Exit button
449
    if yPos == 3 and xPos == 25 then 
450
      call_homepage()
451
    end
452
453
    auto_off()
454
  else
455
    popup_screen(3, "Auto Power", 5)
456
    draw_text(5, 5, "Disabled", colors.red, colors.white)
457
    draw_text(15, 5, " enable ", colors.white, colors.gray)
458
    draw_text(11, 7, "Okay", colors.white, colors.black)
459
460
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
461
462
    --Okay button
463
    if yPos == 7 and xPos >= 11 and xPos <= 17 then 
464
      call_homepage()
465
    end
466
467
    if yPos == 5 then
468
      if xPos >= 15 and xPos <= 21 then
469
        auto_string = "true"
470
        save_config()
471
        auto_off()
472
      else
473
        auto_off()
474
      end
475
    else
476
      auto_off()
477
    end
478
  end
479
end
480
481
--efficiency menu
482
function efficiency()
483
  popup_screen(3, "Efficiency", 12)
484
  fuel_usage = reactor.getFuelConsumedLastTick()
485
  rft = math.floor(reactor.getEnergyProducedLastTick())
486
487
  rfmb = rft / fuel_usage
488
489
  draw_text(5, 5, "Fuel Consumption: ", colors.lime, colors.white)
490
  draw_text(5, 6, fuel_usage.." mB/t", colors.black, colors.white)
491
  draw_text(5, 8, "Energy per mB: ", colors.lime, colors.white)
492
  draw_text(5, 9, rfmb.." RF/mB", colors.black, colors.white)
493
494
  draw_text(5, 11, "RF/tick:", colors.lime, colors.white)
495
  draw_text(5, 12, rft.." RF/T", colors.black, colors.white)
496
497
  draw_text(11, 14, " Okay ", colors.white, colors.black)
498
499
  local event, side, xPos, yPos = os.pullEvent("monitor_touch")
500
501
  --Okay button
502
  if yPos == 14 and xPos >= 11 and xPos <= 17 then 
503
    call_homepage()
504
  end
505
506
  --Exit button
507
  if yPos == 3 and xPos == 25 then 
508
    call_homepage()
509
  end
510
511
  efficiency()
512
end
513
514
515
function fuel()
516
  popup_screen(3, "Fuel", 9)
517
518
  fuel_max = reactor.getFuelAmountMax()
519
  fuel_level = reactor.getFuelAmount()
520
  fuel_reactivity = math.floor(reactor.getFuelReactivity())
521
522
  draw_text(5, 5, "Fuel Level: ", colors.lime, colors.white)
523
  draw_text(5, 6, fuel_level.."/"..fuel_max, colors.black, colors.white)
524
525
  draw_text(5, 8, "Reactivity: ", colors.lime, colors.white)
526
  draw_text(5, 9, fuel_reactivity.."%", colors.black, colors.white)
527
528
  draw_text(11, 11, " Okay ", colors.white, colors.black)
529
530
  local event, side, xPos, yPos = os.pullEvent("monitor_touch")
531
532
533
  --Okay button
534
  if yPos == 11 and xPos >= 11 and xPos <= 17 then 
535
    call_homepage()
536
  end
537
538
  --Exit button
539
  if yPos == 3 and xPos == 25 then 
540
    call_homepage()
541
  end
542
543
  fuel()
544
end
545
546
function waste()
547
  popup_screen(3, "Waste", 8)
548
549
  waste_amount = reactor.getWasteAmount()
550
  draw_text(5, 5, "Waste Amount: ", colors.lime, colors.white)
551
  draw_text(5, 6, waste_amount.." mB", colors.black, colors.white)
552
  draw_text(8, 8, " Eject Waste ", colors.white, colors.red)
553
  draw_text(11, 10, " Close ", colors.white, colors.black)
554
555
  local event, side, xPos, yPos = os.pullEvent("monitor_touch")
556
557
  --eject button
558
  if yPos == 8 and xPos >= 8 and xPos <= 21 then 
559
    reactor.doEjectWaste()
560
    popup_screen(5, "Waste Eject", 5)
561
    draw_text(5, 7, "Waste Ejeceted.", colors.black, colors.white)
562
    draw_text(11, 9, " Close ", colors.white, colors.black)
563
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
564
    --Okay button
565
    if yPos == 7 and xPos >= 11 and xPos <= 17 then 
566
      call_homepage()
567
    end
568
569
    --Exit button
570
    if yPos == 3 and xPos == 25 then 
571
      call_homepage()
572
    end
573
  end
574
575
  --Okay button
576
  if yPos == 10 and xPos >= 11 and xPos <= 17 then 
577
    call_homepage()
578
  end
579
580
  --Exit button
581
  if yPos == 3 and xPos == 25 then 
582
    call_homepage()
583
  end
584
  waste()
585
end
586
587
function set_auto_rf()
588
  popup_screen(5, "Auto Adjust", 11)
589
    draw_text(5, 7, "Try to maintain:", colors.black, colors.white)
590
591
    draw_text(13, 9, " ^ ", colors.white, colors.gray)
592
    draw_text(10, 11, auto_rf.." RF/t", colors.black, colors.white)
593
    draw_text(13, 13, " v ", colors.white, colors.gray)
594
    draw_text(11, 15, " Okay ", colors.white, colors.gray)
595
596
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
597
598
    --increase button
599
    if yPos == 9 then 
600
      auto_rf = auto_rf + 100
601
      save_config()
602
      set_auto_rf()
603
    end
604
605
    --decrease button
606
    if yPos == 13 then 
607
      auto_rf = auto_rf - 100
608
      if auto_rf < 0 then auto_rf = 0 end
609
      save_config()
610
      set_auto_rf()
611
    end
612
613
    if yPos == 15 then 
614
      control_rods()
615
    end
616
617
    set_auto_rf()
618
end
619
620
function control_rods()
621
622
  if reactor.isActivelyCooled() then 
623
624
    popup_screen(3, "Control Rods", 13)
625
    insertion_percent = reactor.getControlRodLevel(0)
626
627
    draw_text(5, 5, "Inserted: "..insertion_percent.."%", colors.black, colors.white)
628
    progress_bar(5, 7, 20, insertion_percent, 100, colors.yellow, colors.gray)
629
630
    draw_text(5, 9, " << ", colors.white, colors.black)
631
    draw_text(10, 9, " < ", colors.white, colors.black)
632
    draw_text(17, 9, " > ", colors.white, colors.black)
633
    draw_text(21, 9, " >> ", colors.white, colors.black)
634
635
    draw_text(5, 11, "Auto:", colors.black, colors.white)
636
    draw_text(5, 13, "unavilable for", colors.red, colors.white)
637
    draw_text(5, 14, "active cooling", colors.red, colors.white)
638
639
    draw_text(11, 16, " Close ", colors.white, colors.gray)
640
641
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
642
643
    if yPos == 9 and xPos >= 5 and xPos <= 15 then 
644
      reactor.setAllControlRodLevels(insertion_percent-10)
645
    end
646
647
    if yPos == 9 and xPos >= 10 and xPos <= 13 then 
648
      reactor.setAllControlRodLevels(insertion_percent-1)
649
    end
650
651
    if yPos == 9 and xPos >= 17 and xPos <= 20 then 
652
      reactor.setAllControlRodLevels(insertion_percent+1)
653
    end
654
655
    if yPos == 9 and xPos >= 21 and xPos <= 25 then 
656
      reactor.setAllControlRodLevels(insertion_percent+10)
657
    end
658
659
    ------Close button-------
660
    if yPos == 16 and xPos >= 11 and xPos <= 17 then 
661
      call_homepage()
662
    end
663
664
    ------Exit button------------
665
    if yPos == 5 and xPos == 25 then 
666
      call_homepage()
667
    end
668
    control_rods()
669
670
  else
671
672
    popup_screen(3, "Control Rods", 13)
673
    insertion_percent = reactor.getControlRodLevel(0)
674
675
    draw_text(5, 5, "Inserted: "..insertion_percent.."%", colors.black, colors.white)
676
    progress_bar(5, 7, 20, insertion_percent, 100, colors.yellow, colors.gray)
677
678
    draw_text(5, 9, " << ", colors.white, colors.black)
679
    draw_text(10, 9, " < ", colors.white, colors.black)
680
    draw_text(17, 9, " > ", colors.white, colors.black)
681
    draw_text(21, 9, " >> ", colors.white, colors.black)
682
683
    draw_text(5, 11, "Auto:", colors.black, colors.white)
684
    draw_text(16, 11, " disable ", colors.white, colors.black)
685
686
    auto_rods_bool = auto_rods == "true"
687
    if auto_rods_bool then
688
      
689
      draw_text(5, 13, "RF/t: "..auto_rf, colors.black, colors.white)
690
      draw_text(18, 13, " set ", colors.white, colors.black)
691
    else
692
      draw_text(16, 11, " enable ", colors.white, colors.black)
693
      draw_text(5, 13, "disabled", colors.red, colors.white)
694
    end
695
696
    draw_text(11, 15, " Close ", colors.white, colors.gray)
697
698
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
699
700
    -----manual adjust buttons------------
701
    if yPos == 9 and xPos >= 5 and xPos <= 15 then 
702
      reactor.setAllControlRodLevels(insertion_percent-10)
703
    end
704
705
    if yPos == 9 and xPos >= 10 and xPos <= 13 then 
706
      reactor.setAllControlRodLevels(insertion_percent-1)
707
    end
708
709
    if yPos == 9 and xPos >= 17 and xPos <= 20 then 
710
      reactor.setAllControlRodLevels(insertion_percent+1)
711
    end
712
713
    if yPos == 9 and xPos >= 21 and xPos <= 25 then 
714
      reactor.setAllControlRodLevels(insertion_percent+10)
715
    end
716
717
718
    ------auto buttons-----------------
719
    if yPos == 11 and xPos >= 16 then 
720
      if auto_rods_bool then
721
        auto_rods = "false"
722
        save_config()
723
        control_rods()
724
      else
725
        auto_rods = "true"
726
        save_config()
727
        control_rods()
728
      end
729
    end
730
731
    if yPos == 13 and xPos >= 18 then 
732
      set_auto_rf()
733
    end
734
735
    ------Close button-------
736
    if yPos == 15 and xPos >= 11 and xPos <= 17 then 
737
      call_homepage()
738
    end
739
740
    ------Exit button------------
741
    if yPos == 5 and xPos == 25 then 
742
      call_homepage()
743
    end
744
    control_rods()
745
746
  end
747
end
748
749
-----------------------Settings--------------------------------
750
751
752
function rf_mode()
753
  wait = read()
754
end
755
756
function steam_mode()
757
  wait = read()
758
end
759
760
function install_update(program, pastebin)
761
    clear()
762
    draw_line(4, 5, 22, colors.blue)
763
764
    for counter = 6, 10 do
765
      draw_line(4, counter, 22, colors.white)
766
    end
767
768
    draw_text(5, 5, "Updating...", colors.white, colors.blue)
769
    draw_text(5, 7, "Open computer", colors.black, colors.white)
770
    draw_text(5, 8, "terminal.", colors.black, colors.white)
771
772
    if fs.exists("install") then fs.delete("install") end
773
    shell.run("pastebin get p4zeq7Ma install")
774
    shell.run("install")
775
end
776
777
function update()
778
  popup_screen(5, "Updates", 4)
779
  draw_text(5, 7, "Connecting to", colors.black, colors.white)
780
  draw_text(5, 8, "pastebin...", colors.black, colors.white)
781
782
  sleep(0.5)
783
  
784
  shell.run("pastebin get MkF2QQjH current_version.txt")
785
  sr = fs.open("current_version.txt", "r")
786
  current_version = tonumber(sr.readLine())
787
  sr.close()
788
  fs.delete("current_version.txt")
789
  terminal_screen()
790
791
  if current_version > version then
792
793
    popup_screen(5, "Updates", 7)
794
    draw_text(5, 7, "Update Available!", colors.black, colors.white)
795
    draw_text(11, 9, " Install ", colors.white, colors.black)
796
    draw_text(11, 11, " Ignore ", colors.white, colors.black)
797
798
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
799
800
    --Instatll button
801
    if yPos == 9 and xPos >= 11 and xPos <= 17 then 
802
      install_update()
803
    end
804
805
    --Exit button
806
    if yPos == 5 and xPos == 25 then 
807
      call_homepage()
808
    end
809
    call_homepage()
810
811
  else
812
    popup_screen(5, "Updates", 5)
813
    draw_text(5, 7, "You are up to date!", colors.black, colors.white)
814
    draw_text(11, 9, " Okay ", colors.white, colors.black)
815
816
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
817
818
    --Okay button
819
    if yPos == 9 and xPos >= 11 and xPos <= 17 then 
820
      call_homepage()
821
    end
822
823
    --Exit button
824
    if yPos == 5 and xPos == 25 then 
825
      call_homepage()
826
    end
827
    call_homepage()
828
  end
829
830
  
831
832
end
833
834
function reset_peripherals()
835
  clear()
836
  draw_line(4, 5, 22, colors.blue)
837
838
  for counter = 6, 10 do
839
    draw_line(4, counter, 22, colors.white)
840
  end
841
842
  draw_text(5, 5, "Reset Peripherals", colors.white, colors.blue)
843
  draw_text(5, 7, "Open computer", colors.black, colors.white)
844
  draw_text(5, 8, "terminal.", colors.black, colors.white)
845
  setup_wizard()
846
847
end
848
849
--stop running status screen if monitors was touched
850
function stop()
851
  while true do
852
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
853
      x = xPos
854
      y = yPos
855
      stop_function = "monitor_touch"
856
    return
857
  end 
858
end
859
860
function mon_touch()
861
  --when the monitor is touch on the homepage
862
  if y == 1 then 
863
      if x < monX/3 then
864
        power_menu()
865
        local event, side, xPos, yPos = os.pullEvent("monitor_touch")
866
        if xPos < 9 then
867
          if yPos == 2 then
868
            reactor.setActive(true)
869
            timer = 0 --reset anytime the reactor is turned on/off
870
            call_homepage()
871
          else if yPos == 3 then
872
            reactor.setActive(false)
873
            timer = 0 --reset anytime the reactor is turned on/off
874
            call_homepage()
875
          else if yPos == 4 then
876
            auto_off()
877
          else
878
            call_homepage()
879
          end
880
          end
881
          end
882
        else
883
          call_homepage()
884
        end
885
        
886
      else if x < 20 then
887
        tools_menu()
888
        local event, side, xPos, yPos = os.pullEvent("monitor_touch")
889
        if xPos < 25 and xPos > 10 then
890
          if yPos == 2 then
891
            control_rods()
892
          else if yPos == 3 then
893
            efficiency()
894
          else if yPos == 4 then
895
            fuel()
896
          else if yPos == 5 then
897
            waste()
898
          else
899
            call_homepage()
900
          end
901
          end
902
          end
903
          end
904
        else
905
          call_homepage()
906
        end
907
      else if x < monX then
908
        settings_menu()
909
        local event, side, xPos, yPos = os.pullEvent("monitor_touch")
910
        if xPos > 13 then
911
          if yPos == 2 then
912
            update()
913
          else if yPos == 3 then
914
            reset_peripherals() 
915
          else
916
            call_homepage()
917
          end
918
          end
919
        else
920
          call_homepage()
921
        end
922
      end
923
      end
924
      end
925
    else
926
      call_homepage()
927
    end
928
end
929
930
function terminal_screen()
931
  term.clear()
932
  draw_line_term(1, 1, 55, colors.blue)
933
  draw_text_term(13, 1, "BigReactor Controls", colors.white, colors.blue)
934
  draw_line_term(1, 19, 55, colors.blue)
935
  draw_text_term(13, 19, "by jaranvil aka jared314", colors.white, colors.blue)
936
937
  draw_text_term(1, 3, "Current program:", colors.white, colors.black)
938
  draw_text_term(1, 4, "Reactor Control v"..version, colors.blue, colors.black)
939
  
940
  draw_text_term(1, 6, "Installer:", colors.white, colors.black)
941
  draw_text_term(1, 7, "pastebin.com/p4zeq7Ma", colors.blue, colors.black)
942
943
  draw_text_term(1, 9, "Please give me your feedback, suggestions,", colors.white, colors.black)
944
  draw_text_term(1, 10, "and errors!", colors.white, colors.black)
945
946
  draw_text_term(1, 11, "reddit.com/r/br_controls", colors.blue, colors.black)
947
end
948
949
--run both homepage() and stop() until one returns
950
function call_homepage()
951
  clear()
952
  homepage()
953
954
  if stop_function == "terminal_screen" then 
955
    stop_function = "nothing"
956
    setup_wizard()
957
  else if stop_function == "monitor_touch" then
958
    stop_function = "nothing"
959
  end
960
  end
961
end
962
963
--test if the entered monitor and reactor can be wrapped
964
  function test_configs()
965
  term.clear()
966
967
  draw_line_term(1, 1, 55, colors.blue)
968
  draw_text_term(10, 1, "BigReactors Controls", colors.white, colors.blue)
969
  
970
  draw_line_term(1, 19, 55, colors.blue)
971
  draw_text_term(10, 19, "by jaranvil aka jared314", colors.white, colors.blue)
972
973
  draw_text_term(1, 3, "Searching for a peripherals...", colors.white, colors.black)
974
  sleep(1)
975
976
  reactor = reactorSearch()
977
  mon = monitorSearch()
978
  
979
  
980
  draw_text_term(2, 5, "Connecting to reactor...", colors.white, colors.black)
981
  sleep(0.5)
982
  if reactor == null then
983
      draw_text_term(1, 8, "Error:", colors.red, colors.black)
984
      draw_text_term(1, 9, "Could not connect to reactor", colors.red, colors.black)
985
      draw_text_term(1, 10, "Reactor must be connected with networking cable", colors.white, colors.black)
986
      draw_text_term(1, 11, "and modems or the computer is directly beside", colors.white, colors.black)
987
       draw_text_term(1, 12,"the reactors computer port.", colors.white, colors.black)
988
      draw_text_term(1, 14, "Press Enter to continue...", colors.gray, colors.black)
989
      wait = read()
990
      setup_wizard()
991
  else
992
      draw_text_term(27, 5, "success", colors.lime, colors.black)
993
      sleep(0.5)
994
  end
995
  
996
  draw_text_term(2, 6, "Connecting to monitor...", colors.white, colors.black)
997
  sleep(0.5)
998
  if mon == null then
999
      draw_text_term(1, 7, "Error:", colors.red, colors.black)
1000
      draw_text_term(1, 8, "Could not connect to a monitor. Place a 3x3 advanced monitor", colors.red, colors.black)
1001
      draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
1002
      wait = read()
1003
      setup_wizard()
1004
  else
1005
      monX, monY = mon.getSize()
1006
      draw_text_term(27, 6, "success", colors.lime, colors.black)
1007
      sleep(0.5)
1008
  end
1009
    draw_text_term(2, 7, "saving configuration...", colors.white, colors.black)  
1010
1011
    save_config()
1012
1013
    sleep(0.1)
1014
    draw_text_term(1, 9, "Setup Complete!", colors.lime, colors.black) 
1015
    sleep(1)
1016
1017
    auto = auto_string == "true"
1018
    call_homepage() 
1019
1020
end
1021
----------------SETUP-------------------------------
1022
1023
function setup_wizard()
1024
  
1025
  term.clear()
1026
1027
  
1028
     draw_text_term(1, 1, "BigReactor Controls v"..version, colors.lime, colors.black)
1029
     draw_text_term(1, 2, "Peripheral setup", colors.white, colors.black)
1030
     draw_text_term(1, 4, "Step 1:", colors.lime, colors.black)
1031
     draw_text_term(1, 5, "-Place 3x3 advanced monitors next to computer.", colors.white, colors.black)
1032
     draw_text_term(1, 7, "Step 2:", colors.lime, colors.black)
1033
     draw_text_term(1, 8, "-Place a wired modem on this computer and on the ", colors.white, colors.black)
1034
     draw_text_term(1, 9, " computer port of the reactor.", colors.white, colors.black)
1035
     draw_text_term(1, 10, "-connect modems with network cable.", colors.white, colors.black)
1036
     draw_text_term(1, 11, "-right click modems to activate.", colors.white, colors.black)
1037
     draw_text_term(1, 13, "Press Enter when ready...", colors.gray, colors.black)
1038
    
1039
     wait = read()
1040
      test_configs()
1041
1042
   
1043
end
1044
1045
-- peripheral searching thanks to /u/kla_sch
1046
-- http://pastebin.com/gTEBHv3D
1047
function reactorSearch()
1048
   local names = peripheral.getNames()
1049
   local i, name
1050
   for i, name in pairs(names) do
1051
      if peripheral.getType(name) == "BigReactors-Reactor" then
1052
         return peripheral.wrap(name)
1053
      else
1054
         --return null
1055
      end
1056
   end
1057
end
1058
1059
function monitorSearch()
1060
   local names = peripheral.getNames()
1061
   local i, name
1062
   for i, name in pairs(names) do
1063
      if peripheral.getType(name) == "monitor" then
1064
        test = name
1065
         return peripheral.wrap(name)
1066
      else
1067
         --return null
1068
      end
1069
   end
1070
end
1071
1072
function start()
1073
  --if configs exists, load values and test
1074
  if fs.exists("config.txt") then
1075
      load_config()
1076
      test_configs()
1077
  else
1078
    setup_wizard()
1079
  end
1080
end
1081
1082-
start()
1082+
while (true) do
1083
    local ok, err = pcall(start)
1084
    os.sleep(1)
1085
end