Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require ("component")
- local computer = require ("computer")
- local SGL = require ("SGL")
- local term = require ("term")
- local redstone
- local reactor
- ------------------------------------------------
- if not component.isAvailable ("reactor") then
- if not component.isAvailable ("reactor_chamber") then
- print ("No available reactor found")
- return
- else
- reactor = component.reactor_chamber
- end
- else
- reactor = component.reactor
- end
- if not component.isAvailable ("redstone") then
- print ("No available redstone controller found")
- return
- end
- redstone = component.redstone
- SGL.Init ()
- local last_w, last_h = SGL.Gpu.getResolution ()
- ------------------------------------------------
- local running = true
- local safe_mode, protected, igonre_overheat = false, true, false
- local w, h = 75, 7
- local background = 0x323232
- local textcolor = 0xFFFFFF
- local heatbar_background = 0x242424
- local heatbar_foreground = 0x0095FF
- local output_textcolor = 0xEEFF00
- local tobipizda_color = 0xEEFF00
- local reactor_maxheat = reactor.getMaxHeat ()
- local redstone_side = 1
- local power = false
- local heat_ok = true
- ------------------------------------------------
- local function setResolution (w_, h_)
- if not safe_mode then
- SGL.Gpu.setResolution (w_, h_)
- end
- end
- ------------------------------------------------
- local function Beep ()
- for i = 1, 3 do computer.beep (1000, 0.01) end
- end
- ------------------------------------------------
- local function CheckHeat (heat)
- return 100 / reactor_maxheat * heat <= 75 or ignore_overheat
- end
- ------------------------------------------------
- local function SetPower (to)
- if to then
- if heat_ok then
- power = true
- redstone.setOutput (redstone_side, 15)
- else
- power = false
- computer.beep (500, 0.01)
- end
- else
- power = false
- redstone.setOutput (redstone_side, 0)
- end
- end
- ------------------------------------------------
- local function ShowHelpMenu ()
- local text = {
- "Press space to toggle reactor power",
- "Press tab to exit program",
- "If the heating of the reactor is above 75%,",
- "the power will turn off automatically",
- "Created by Smok1e",
- "[Press any key to continue]"
- }
- local width = 0
- local height = #text
- for i = 1, height do
- if #text[i] > width then width = #text[i] end
- end
- local local_w, local_h = w, h
- if w < width + 2 then local_w = width + 2 end
- if h < height + 2 then local_h = height + 2 end
- if local_w ~= w or local_h ~= h then
- setResolution (local_w, local_h)
- end
- SGL.Clear (background)
- for i = 1, height do
- SGL.Draw.Text (local_w / 2, local_h / 2 - height / 2 + i, text[i], false, textcolor, true)
- end
- SGL.Display ()
- while computer.pullSignal () ~= "key_down" do end
- if local_w ~= w or local_h ~= h then
- setResolution (w, h)
- end
- end
- ------------------------------------------------
- local function OnTab (event)
- running = false
- return true
- end
- ------------------------------------------------
- local function OnSpace (event)
- SetPower (not power)
- return true
- end
- ------------------------------------------------
- local function OnH (event)
- ShowHelpMenu ()
- return true
- end
- ------------------------------------------------
- local function OnReactorRemoved (event)
- SetPower (false)
- SGL.Clear (background)
- local text1 = "It's looks like reactor has been removed or crashed"
- local text2 = "[Press any key to continue]"
- SGL.Draw.Text (w / 2, h / 2, text1, false, textcolor, true)
- SGL.Draw.Text (w / 2, h / 2 + 1, text2, false, textcolor, true)
- SGL.Display ()
- while computer.pullSignal () ~= "key_down" do end
- error ("Reactor has been removed")
- return true
- end
- ------------------------------------------------
- local function OnRedstoneRemoved (event)
- local _, address = table.unpack (event)
- if address == redstone.address then
- error ("Redstone controller was removed. How do you think will program work without him, idiot?")
- return true
- else
- return false
- end
- end
- ------------------------------------------------
- local function OnComponentRemoved (event)
- local _, name, name = table.unpack (event)
- if name == "reactor" then return OnReactorRemoved (event) end
- if name == "redstone" then return OnRedstoneRemoved (event) end
- end
- ------------------------------------------------
- local function OnKeyDown (event)
- local _, _, key, alt = table.unpack (event)
- if key == 9 and alt == 15 then return OnTab (event) end
- if key == 32 and alt == 57 then return OnSpace (event) end
- if key == 104 and alt == 35 then return OnH (event) end
- return false
- end
- ------------------------------------------------
- local function ProcessEvents (event)
- local event_type = table.unpack (event)
- if event_type == "key_down" then return OnKeyDown (event) end
- if event_type == "component_removed" then return OnComponentRemoved (event) end
- return false
- end
- ------------------------------------------------
- local function DrawHeatBar (heat)
- local pct = math.floor (100 / reactor_maxheat * heat)
- local text1 = "Heat: "
- local text2 = pct .. "%"
- local color = textcolor
- if not heat_ok then color = 0xff0000 end
- local x, y = 13, 2
- local width, height = w - 13, 1
- local fill = width / 100 * pct
- SGL.Draw.Text (2, 2, text1, false, textcolor)
- SGL.Draw.Text (2 + #text1, 2, text2, false, color)
- SGL.Draw.Rect (x, y, width, height, heatbar_background)
- SGL.Draw.Rect (x, y, fill, height, heatbar_foreground)
- SGL.Draw.Text (x + width / 2, 2, heat .. "/" .. reactor_maxheat, false, textcolor, true)
- end
- ------------------------------------------------
- local function DrawStatus ()
- local text1 = "Status: "
- local text2 = "OFF"
- local color = 0xff0000
- if power then text2 = "ON" color = 0x00ff00 end
- if not heat_ok then text2 = "TOBI PIZDA" color = tobipizda_color end
- SGL.Draw.Text (2, 4, text1, false, textcolor)
- SGL.Draw.Text (2 + #text1, 4, text2, false, color)
- end
- ------------------------------------------------
- local function DrawEnergyOutput (output)
- local text1 = "Output: "
- local text2 = output .. " EU/t"
- local x, y = 2, 6
- SGL.Draw.Text (x, y, text1, false, textcolor)
- SGL.Draw.Text (x + #text1, y, text2, false, output_textcolor)
- end
- ------------------------------------------------
- function DrawAll (heat, output)
- DrawHeatBar (heat)
- DrawStatus ()
- DrawEnergyOutput (output)
- SGL.Draw.Text (w / 2, h, "Press 'h' to show help menu", false, textcolor, true)
- SGL.Display ()
- end
- ------------------------------------------------
- local function main ()
- while running do
- SGL.Clear (background)
- local heat = reactor.getHeat ()
- local output = reactor.getReactorEUOutput ()
- heat_ok = CheckHeat (heat)
- DrawAll (heat, output)
- if power then
- if not heat_ok then
- Beep ()
- SetPower (false)
- end
- end
- local event = {computer.pullSignal (0.1)}
- ProcessEvents (event)
- end
- end
- do
- local args = {...}
- for i = 1, #args do
- local arg = args[i]
- if arg == "-safe" or arg == "-s" then
- safe_mode = true
- elseif arg == "-unprotected" or arg == "-u" then
- protected = false
- elseif arg == "-i" or arg == "-ignore" then
- ignore_overheat = true
- end
- end
- setResolution (w, h)
- SetPower (false)
- local result, reason = true
- if protected then
- result, reason = pcall (main)
- else
- main ()
- end
- setResolution (last_w, last_h)
- SGL.Gpu.setBackground (0x000000)
- SGL.Gpu.setForeground (0xffffff)
- term.clear ()
- if not result then
- print ("Runtime error: " .. reason)
- if not component.proxy (reactor.address) then print ("Reactor is not available") ractor = nil end
- if not component.proxy (redstone.address) then print ("Redstone controller is not available") redstone = nil end
- if redstone then
- SetPower (false)
- print ("Reactor was powered off")
- end
- else
- SetPower (false)
- end
- end
Add Comment
Please, Sign In to add comment