HangMan23

ReactorControl

Aug 13th, 2020 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. local component = require ("component")
  2. local computer = require ("computer")
  3. local SGL = require ("SGL")
  4. local term = require ("term")
  5.  
  6. local redstone
  7. local reactor
  8.  
  9. ------------------------------------------------
  10.  
  11. if not component.isAvailable ("reactor") then
  12.  
  13. if not component.isAvailable ("reactor_chamber") then
  14.  
  15. print ("No available reactor found")
  16.  
  17. return
  18.  
  19. else
  20.  
  21. reactor = component.reactor_chamber
  22.  
  23. end
  24.  
  25. else
  26.  
  27. reactor = component.reactor
  28.  
  29. end
  30.  
  31. if not component.isAvailable ("redstone") then
  32.  
  33. print ("No available redstone controller found")
  34.  
  35. return
  36.  
  37. end
  38.  
  39. redstone = component.redstone
  40.  
  41. SGL.Init ()
  42.  
  43. local last_w, last_h = SGL.Gpu.getResolution ()
  44.  
  45. ------------------------------------------------
  46.  
  47. local running = true
  48.  
  49. local safe_mode, protected, igonre_overheat = false, true, false
  50.  
  51. local w, h = 75, 7
  52.  
  53. local background = 0x323232
  54. local textcolor = 0xFFFFFF
  55. local heatbar_background = 0x242424
  56. local heatbar_foreground = 0x0095FF
  57. local output_textcolor = 0xEEFF00
  58. local tobipizda_color = 0xEEFF00
  59.  
  60. local reactor_maxheat = reactor.getMaxHeat ()
  61.  
  62. local redstone_side = 1
  63.  
  64. local power = false
  65.  
  66. local heat_ok = true
  67.  
  68. ------------------------------------------------
  69.  
  70. local function setResolution (w_, h_)
  71.  
  72. if not safe_mode then
  73.  
  74. SGL.Gpu.setResolution (w_, h_)
  75.  
  76. end
  77.  
  78. end
  79.  
  80. ------------------------------------------------
  81.  
  82. local function Beep ()
  83.  
  84. for i = 1, 3 do computer.beep (1000, 0.01) end
  85.  
  86. end
  87.  
  88. ------------------------------------------------
  89.  
  90. local function CheckHeat (heat)
  91.  
  92. return 100 / reactor_maxheat * heat <= 75 or ignore_overheat
  93.  
  94. end
  95.  
  96. ------------------------------------------------
  97.  
  98. local function SetPower (to)
  99.  
  100. if to then
  101.  
  102. if heat_ok then
  103.  
  104. power = true
  105.  
  106. redstone.setOutput (redstone_side, 15)
  107.  
  108. else
  109.  
  110. power = false
  111.  
  112. computer.beep (500, 0.01)
  113.  
  114. end
  115.  
  116. else
  117.  
  118. power = false
  119.  
  120. redstone.setOutput (redstone_side, 0)
  121.  
  122. end
  123.  
  124. end
  125.  
  126. ------------------------------------------------
  127.  
  128. local function ShowHelpMenu ()
  129.  
  130. local text = {
  131.  
  132. "Press space to toggle reactor power",
  133. "Press tab to exit program",
  134. "If the heating of the reactor is above 75%,",
  135. "the power will turn off automatically",
  136. "Created by Smok1e",
  137. "[Press any key to continue]"
  138.  
  139. }
  140.  
  141. local width = 0
  142. local height = #text
  143.  
  144. for i = 1, height do
  145.  
  146. if #text[i] > width then width = #text[i] end
  147.  
  148. end
  149.  
  150. local local_w, local_h = w, h
  151.  
  152. if w < width + 2 then local_w = width + 2 end
  153. if h < height + 2 then local_h = height + 2 end
  154.  
  155. if local_w ~= w or local_h ~= h then
  156.  
  157. setResolution (local_w, local_h)
  158.  
  159. end
  160.  
  161. SGL.Clear (background)
  162.  
  163. for i = 1, height do
  164.  
  165. SGL.Draw.Text (local_w / 2, local_h / 2 - height / 2 + i, text[i], false, textcolor, true)
  166.  
  167. end
  168.  
  169. SGL.Display ()
  170.  
  171. while computer.pullSignal () ~= "key_down" do end
  172.  
  173. if local_w ~= w or local_h ~= h then
  174.  
  175. setResolution (w, h)
  176.  
  177. end
  178.  
  179. end
  180.  
  181. ------------------------------------------------
  182.  
  183. local function OnTab (event)
  184.  
  185. running = false
  186.  
  187. return true
  188.  
  189. end
  190.  
  191. ------------------------------------------------
  192.  
  193. local function OnSpace (event)
  194.  
  195. SetPower (not power)
  196.  
  197. return true
  198.  
  199. end
  200.  
  201. ------------------------------------------------
  202.  
  203. local function OnH (event)
  204.  
  205. ShowHelpMenu ()
  206.  
  207. return true
  208.  
  209. end
  210.  
  211. ------------------------------------------------
  212.  
  213. local function OnReactorRemoved (event)
  214.  
  215. SetPower (false)
  216.  
  217. SGL.Clear (background)
  218.  
  219. local text1 = "It's looks like reactor has been removed or crashed"
  220. local text2 = "[Press any key to continue]"
  221.  
  222. SGL.Draw.Text (w / 2, h / 2, text1, false, textcolor, true)
  223. SGL.Draw.Text (w / 2, h / 2 + 1, text2, false, textcolor, true)
  224.  
  225. SGL.Display ()
  226.  
  227. while computer.pullSignal () ~= "key_down" do end
  228.  
  229. error ("Reactor has been removed")
  230.  
  231. return true
  232.  
  233. end
  234.  
  235. ------------------------------------------------
  236.  
  237. local function OnRedstoneRemoved (event)
  238.  
  239. local _, address = table.unpack (event)
  240.  
  241. if address == redstone.address then
  242.  
  243. error ("Redstone controller was removed. How do you think will program work without him, idiot?")
  244.  
  245. return true
  246.  
  247. else
  248.  
  249. return false
  250.  
  251. end
  252.  
  253. end
  254.  
  255. ------------------------------------------------
  256.  
  257. local function OnComponentRemoved (event)
  258.  
  259. local _, name, name = table.unpack (event)
  260.  
  261. if name == "reactor" then return OnReactorRemoved (event) end
  262. if name == "redstone" then return OnRedstoneRemoved (event) end
  263.  
  264. end
  265.  
  266. ------------------------------------------------
  267.  
  268. local function OnKeyDown (event)
  269.  
  270. local _, _, key, alt = table.unpack (event)
  271.  
  272. if key == 9 and alt == 15 then return OnTab (event) end
  273. if key == 32 and alt == 57 then return OnSpace (event) end
  274. if key == 104 and alt == 35 then return OnH (event) end
  275.  
  276. return false
  277.  
  278. end
  279.  
  280. ------------------------------------------------
  281.  
  282. local function ProcessEvents (event)
  283.  
  284. local event_type = table.unpack (event)
  285.  
  286. if event_type == "key_down" then return OnKeyDown (event) end
  287. if event_type == "component_removed" then return OnComponentRemoved (event) end
  288.  
  289. return false
  290.  
  291. end
  292.  
  293. ------------------------------------------------
  294.  
  295. local function DrawHeatBar (heat)
  296.  
  297. local pct = math.floor (100 / reactor_maxheat * heat)
  298.  
  299. local text1 = "Heat: "
  300. local text2 = pct .. "%"
  301.  
  302. local color = textcolor
  303.  
  304. if not heat_ok then color = 0xff0000 end
  305.  
  306. local x, y = 13, 2
  307.  
  308. local width, height = w - 13, 1
  309.  
  310. local fill = width / 100 * pct
  311.  
  312. SGL.Draw.Text (2, 2, text1, false, textcolor)
  313. SGL.Draw.Text (2 + #text1, 2, text2, false, color)
  314. SGL.Draw.Rect (x, y, width, height, heatbar_background)
  315. SGL.Draw.Rect (x, y, fill, height, heatbar_foreground)
  316. SGL.Draw.Text (x + width / 2, 2, heat .. "/" .. reactor_maxheat, false, textcolor, true)
  317.  
  318. end
  319.  
  320. ------------------------------------------------
  321.  
  322. local function DrawStatus ()
  323.  
  324. local text1 = "Status: "
  325. local text2 = "OFF"
  326.  
  327. local color = 0xff0000
  328.  
  329. if power then text2 = "ON" color = 0x00ff00 end
  330. if not heat_ok then text2 = "TOBI PIZDA" color = tobipizda_color end
  331.  
  332. SGL.Draw.Text (2, 4, text1, false, textcolor)
  333. SGL.Draw.Text (2 + #text1, 4, text2, false, color)
  334.  
  335. end
  336.  
  337. ------------------------------------------------
  338.  
  339. local function DrawEnergyOutput (output)
  340.  
  341. local text1 = "Output: "
  342. local text2 = output .. " EU/t"
  343.  
  344. local x, y = 2, 6
  345.  
  346. SGL.Draw.Text (x, y, text1, false, textcolor)
  347. SGL.Draw.Text (x + #text1, y, text2, false, output_textcolor)
  348.  
  349. end
  350.  
  351. ------------------------------------------------
  352.  
  353. function DrawAll (heat, output)
  354.  
  355. DrawHeatBar (heat)
  356. DrawStatus ()
  357. DrawEnergyOutput (output)
  358.  
  359. SGL.Draw.Text (w / 2, h, "Press 'h' to show help menu", false, textcolor, true)
  360.  
  361. SGL.Display ()
  362.  
  363. end
  364.  
  365. ------------------------------------------------
  366.  
  367. local function main ()
  368.  
  369. while running do
  370.  
  371. SGL.Clear (background)
  372.  
  373. local heat = reactor.getHeat ()
  374. local output = reactor.getReactorEUOutput ()
  375.  
  376. heat_ok = CheckHeat (heat)
  377.  
  378. DrawAll (heat, output)
  379.  
  380. if power then
  381.  
  382. if not heat_ok then
  383.  
  384. Beep ()
  385.  
  386. SetPower (false)
  387.  
  388. end
  389.  
  390. end
  391.  
  392. local event = {computer.pullSignal (0.1)}
  393.  
  394. ProcessEvents (event)
  395.  
  396. end
  397.  
  398. end
  399.  
  400. do
  401.  
  402. local args = {...}
  403.  
  404. for i = 1, #args do
  405.  
  406. local arg = args[i]
  407.  
  408. if arg == "-safe" or arg == "-s" then
  409.  
  410. safe_mode = true
  411.  
  412. elseif arg == "-unprotected" or arg == "-u" then
  413.  
  414. protected = false
  415.  
  416. elseif arg == "-i" or arg == "-ignore" then
  417.  
  418. ignore_overheat = true
  419.  
  420. end
  421.  
  422. end
  423.  
  424. setResolution (w, h)
  425.  
  426. SetPower (false)
  427.  
  428. local result, reason = true
  429.  
  430. if protected then
  431.  
  432. result, reason = pcall (main)
  433.  
  434. else
  435.  
  436. main ()
  437.  
  438. end
  439.  
  440. setResolution (last_w, last_h)
  441.  
  442. SGL.Gpu.setBackground (0x000000)
  443. SGL.Gpu.setForeground (0xffffff)
  444.  
  445. term.clear ()
  446.  
  447. if not result then
  448.  
  449. print ("Runtime error: " .. reason)
  450.  
  451. if not component.proxy (reactor.address) then print ("Reactor is not available") ractor = nil end
  452. if not component.proxy (redstone.address) then print ("Redstone controller is not available") redstone = nil end
  453.  
  454. if redstone then
  455.  
  456. SetPower (false)
  457.  
  458. print ("Reactor was powered off")
  459.  
  460. end
  461.  
  462. else
  463.  
  464. SetPower (false)
  465.  
  466. end
  467.  
  468. end
Add Comment
Please, Sign In to add comment