asteroidsteam icon

Extreme Reactors Remote Control

asteroidsteam | PRO | 07/13/22 12:15:33 AM UTC (Edited) | 0 ⭐ | 390 👁️ | Never ⏰ | []
Lua |

5 KB

|

None

|

0 👍

/

0 👎

rednet.open("back")
 
buttons = {}
 
local cur_power_gen = 0
local target_power = 5000 -- RF/Tick
local control_rod_level = 50
local reactor_enabled = false
 
local mw, mh = term.getSize()
 
function drawBoxWithCenteredText(x,y,w,h,text,color)
    local ox,oy = term.getCursorPos()
    local bgColor = term.getBackgroundColor()
 
    term.setBackgroundColor(color)
    for cy=1,h,1 do
        for cx=1,w,1 do
            term.setCursorPos(x+cx,y+cy)
            term.write(" ")
        end
    end
 
    local cx = math.floor(w / 2 + x - string.len(text) / 2 + 1)
    local cy = math.floor(h / 2 + y + 1)
 
    term.setCursorPos(cx, cy)
    term.write(text)
 
    term.setBackgroundColor(bgColor)
    term.setCursorPos(ox, oy)
end
 
function clamp(val, min, max) 
    if val > max then
        return max
    elseif val < min then
        return min
    else
        return val
    end
end
 
function drawBar(x, y, size, perc)
    local bgColor = term.getBackgroundColor()
    local ox, oy = term.getCursorPos()
 
    term.setCursorPos(x, y)
    term.setBackgroundColor(colors.green)
 
    local percGreen = clamp(math.floor(perc * size), 0, size)
    local percGray = clamp(size - percGreen, 0, size - percGreen)
    if percGray < 0 then
        percGray = 0
    end
 
    for i = percGreen,1,-1 do
        term.write(" ")
    end
    term.setBackgroundColor(colors.lightGray)
    for i = percGray,1,-1 do
        term.write(" ")
    end
 
    term.setBackgroundColor(bgColor)
    term.setCursorPos(ox, oy)
end
 
function getControlRodDiff(RFDiff)
    if RFDiff > 10000 then
        return 5
    elseif RFDiff > 5000 then
        return 3
    elseif RFDiff > 1000 then
        return 1
    elseif RFDiff > 500 then
        return 0.5
    elseif RFDiff > 100 then
        return 0.1
    else
        return 0.05
    end
end
 
function formatRF(rf)
    if rf >= 1000000 then
        return round(rf/1000000, 2) .. " MiRF"
    elseif rf >= 1000 then
        return round(rf/1000, 2) .. " KiRF"
    else 
        return math.floor(rf) .. " RF"
    end
end
 
function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end
 
function renderButtons()
    for i = 1, #buttons do
        local button = buttons[i]
        if not reactor_enabled then
            if button.drod then
                drawBoxWithCenteredText(button.x,button.y,button.w,button.h,button.text,button.color)
            end
        else
            drawBoxWithCenteredText(button.x,button.y,button.w,button.h,button.text,button.color)
        end
    end
end
 
function addButton(x,y,w,h,text,color,doRenderOnDisable, executor)
    local button = {
        x = x,
        y = y,
        w = w,
        h = h,
        text = text,
        color = color,
        drod = doRenderOnDisable,
        executor = executor
    }
    buttons[#buttons+1] = button
end
 
function inBounds(n, min, max)
    return n <= max and n >= min
end
 
function sendBTNCommand(btn)
    sendCommand({button = btn.text})
end
 
addButton(1,mh - 4,11,3,"SHUT DOWN",colours.red, true, sendBTNCommand)
addButton(14,mh - 4,11,3,"START",colours.green, true, sendBTNCommand)
 
addButton(1, 9, 3, 1, "-10", colours.red, false, sendBTNCommand)
addButton(5, 9, 4, 1, "-100", colours.red, false, sendBTNCommand)
addButton(10, 9, 5, 1, "-1000", colours.red, false, sendBTNCommand)
 
addButton(1, 11, 3, 1, "+10", colours.green, false, sendBTNCommand)
addButton(5, 11, 4, 1, "+100", colours.green, false, sendBTNCommand)
addButton(10, 11, 5, 1, "+1000", colours.green, false, sendBTNCommand)
 
function sendCommand(command)
    rednet.send(0, textutils.serialize(command), "reactor_control")
    local id, object = rednet.receive("reactor_response")
    return textutils.unserialize(object)
end
 
function getReactorStatus()
    return sendCommand("_QUERY_")
end
 
term.clear()
 
function render()
    while true do
        local reactor_status = getReactorStatus()
 
        cur_power_gen = reactor_status.cur_power_gen
        reactor_enabled = reactor_status.reactor_enabled
        target_power = reactor_status.target_power
        control_rod_level = reactor_status.control_rod_level
 
        if cur_power_gen ~= 0 and reactor_enabled then
            term.setCursorPos(1,1)
            term.write("Control Rod Level: ")
            drawBar(1,2,20,control_rod_level/100)
            term.setCursorPos(22,2)
            term.write(math.floor(control_rod_level) .. "\%  ")
 
            term.setCursorPos(1,4)
            term.write("Power Generation: ")
            drawBar(1,5,13,cur_power_gen/target_power)
            term.setCursorPos(15,5)
            term.write(formatRF(cur_power_gen) .. "/t    ")
 
            term.setCursorPos(1,7)
            term.write("Target power generation: ")
            term.setCursorPos(2,8)
            term.clearLine()
            term.setBackgroundColor(colors.blue)
            term.write(formatRF(target_power) .. "/t")
            term.setBackgroundColor(colors.black)
        end
 
        if not reactor_enabled then
            term.clear()
            term.setCursorPos(2,2)
            term.write("Reactor is offline")
        end
 
        renderButtons()
        sleep(0.1)
    end
end
 
function pullEvents()
    while true do
        event, button, x, y = os.pullEvent("mouse_click")
        for i=1, #buttons do
            local button = buttons[i]
            if inBounds(x, button.x, button.x + button.w) and inBounds(y, button.y, button.y + button.h) then
                button.executor(button)
                break
            end
        end
    end
end
 
parallel.waitForAny(pullEvents, render)

Comments