Big Reactors and Computercraft

vliegend pak

New Member
May 28, 2021
1
0
2
So i've started a project trying to write my own program which should, in the end, have complete control over my extreme reactors using cc.
I'm just getting back into programming so a friend of mine, who actually knows what he is doing, is helping me out where he can.
We're currently working on just monitoring what's happening with our reactor(s) so far we're making some good progress but since my friend had some other stuff to do the last few days i decided to continue by myself.
With a lot of effort, googleing and a little advice i managed to get my program to a point where im more or less satisfied, so i decided it would be cool to share it here and get some feedback from other people or even ways to improve the program (since im not a experienced programmer it's a lot of spaghetti code)
Or if you just want to use it for yourself feel free to copy the codes and have fun!

the current setup is using 1 advanced computer to run the program, for now only 1 or 2 big reactors (size shouldn't matter as far as i've tested) and at least 1 2x3 advanced monitor
main features:
- display most variables from the reactor itself
- colored feedback depending on the positive/negative nature of given variable
reactor monitorring.png
reactor monitorring2.png





Code:
-- authors: greize__jager & cheater_monkey

local green = (colors.lime)
local white = (colors.white)
local red = (colors.red)
local orange = (colors.orange)
local yellow = (colors.yellow)
local monitors = {peripheral.find("monitor")}
local reactors = {peripheral.find("BigReactors-Reactor")}
local warningCount = 0
local maxWarning = 16
local status = 1
local yaxis = 1
local rWorking = 0

-- warnings
local minWarn = 0
local maxWarn = 3
local warningCalc = 0
local fuelStored = 0
local wasteStored = 0
local energyStored = 0
local rEffectTot = 0
local rEffect = 0

rednet.open("back")

local display = {}


function select_reactor(num)
    if #reactors == 1 then
        selected_reactor = 1
    else if #reactors > 1 then
        if redstone.getInput("left") == true then
            selected_reactor = 2
        else if redstone.getInput("left") == false then
            selected_reactor = 1
        end
        end   
    end
    end
end

function warningCUp()
    warningCount = warningCount + 1
end

local function divideline()
    local w = display.getSize()
    term.setCursorPos(1, yaxis)
        for i = 1, w do
            write("=")
        end
    yaxis = yaxis + 1
end

function is_connected()
     if #reactors >= 1 then
        return true
    else
         return false
    end
end

function lineEnd()
    term.setTextColor(white)
    yaxis = yaxis + 1
end
  
function nLine()
    yaxis = yaxis + 1
    term.setCursorPos(1, yaxis)
end


local function init_monitors()
    for mon,_ in pairs(monitors[1]) do
        display[mon] = function(...)
            for i=1, #monitors-1 do monitors[i][mon](unpack(arg)) end
                return monitors[#monitors][mon](unpack(arg))           
            end
    end
end

function get_active(r)
    local activity = r.getActive()
    term.setCursorPos(1, yaxis)
    write("Reactor status: ")
    term.setCursorPos(20, yaxis)
        if activity == true then
            term.setTextColor(green)
            write("online")
        else if activity == false then
            term.setTextColor(red)
            write("offline")
        end
        end
    lineEnd()
end

function generalinfo()
    local time = textutils.formatTime(os.time())

    write("reactor #: " ..selected_reactor)
    term.setCursorPos(20, yaxis)
    write("time: " ..time.. "\n")
    lineEnd()
end

function rf_generated(r)
    local activity = r.getActive()

    term.setCursorPos(1, yaxis)
    write("RF/T: ")
    term.setCursorPos(20, yaxis)
        if activity == true then
            term.setTextColor(green)
            print(math.floor(r.getEnergyProducedLastTick()))
        else
            term.setTextColor(red)
            print("reactor offline")
        end   
    lineEnd()   
end         

function get_energylvl(r)
    local eLvL = r.getEnergyStored()
    local eLvLp = math.floor(100* (r.getEnergyStored() / 10000000))
 
    term.setCursorPos(1, yaxis)
    write("Stored RF: ")
    term.setCursorPos(20, yaxis)
        if (eLvL) <= 7500000 then
            term.setTextColor(green)
              energyStored = 0
        else if (eLvL) > 7500000 and (eLvL) < 9999999 then
            term.setTextColor(orange)
            warningCUp()
            energyStored = 1
            ew = "RF storage almost full"
        else if (eLvL) > 9999999 then
            term.setTextColor(red)
            warningCUp()
            energyStored = 2
            ew = "RF storage is full"
        end
        end
        end
    print(eLvL.. " (" ..eLvLp..  ")% ")
    lineEnd()
end

local function casing_heat(r)
    term.setCursorPos(1, yaxis)
    write("casing heat: ")
    term.setCursorPos(20, yaxis)
    term.setTextColor(green)
    term.write(string.format("%.f", (r.getCasingTemperature())).. "°C")
    lineEnd()
end

local function fuel_heat(r)
    term.setCursorPos(1, yaxis)
    write("fuel heat: ")
    term.setCursorPos(20, yaxis)
    term.setTextColor(green)
    term.write(string.format("%.f", (r.getFuelTemperature())).. "°C")
    lineEnd()   
end
 
local function fuel_consumption(r)
    local activityc = r.getActive()

    term.setCursorPos(1, yaxis)
    write("Fuel consumption/T: ")
    term.setCursorPos(20, yaxis)
        if activityc == true then
            term.setTextColor(green)
            print(string.format("%.3f", (r.getFuelConsumedLastTick())))
        else
            term.setTextColor(red)
            print("reactor offline")
        end   
    lineEnd()   
end

local function time_remaining(r)
    local activityr = r.getActive()

    term.setCursorPos(1, yaxis)
    write("Can operate for: ")
    term.setCursorPos(20, yaxis)
        if activityr == true then
            term.setTextColor(green)
            print(string.format("%.f", (r.getFuelAmount()/(r.getFuelConsumedLastTick()*20*60))).. " Min")
        else
            term.setTextColor(red)
            print("reactor offline")
        end   
    lineEnd()
end   
          
local function fuel_amount(r)
    local curFuel = r.getFuelAmount()
    local maxFuel = r.getFuelAmountMax()
    term.setCursorPos(1, yaxis)
    write("Fuel stored: ")
    term.setCursorPos(20, yaxis)
        if curFuel >= (maxFuel * 0.75) then
            term.setTextColor(green)
             fuelStored = 0
        else if curFuel >= (maxFuel * 0.50) then
            term.setTextColor(orange)
            warningCUp()
            fuelStored = 1
            fw = "fuel lvls are running low"
        else
               term.setTextColor(red)
            warningCUp()
            fuelStored = 2
            fw = "fuel lvls critical"
        end
        end
    write(string.format(r.getFuelAmount()))
    term.setTextColor(white)
    write("/" ..string.format(maxFuel))
    lineEnd()
end

local function waste_amount(r)
    local curWaste = r.getWasteAmount()
    local maxFuel = r.getFuelAmountMax()
    
    term.setCursorPos(1, yaxis)
    write("waste stored: ")
    term.setCursorPos(20, yaxis)
        if curWaste >= (maxFuel * 0.75) then
            term.setTextColor(red)
            wasteStored = 2
            ww = "waste lvls are critical"
        else if curWaste >= (maxFuel * 0.25) then
            term.setTextColor(orange)
            wasteStored = 1
            ww = "waste lvls are getting high"
        else
            term.setTextColor(green)
            wasteStored = 0
        end
        end
    write(string.format(curWaste))
    term.setTextColor(white)
    write("/" ..string.format(maxFuel))
    lineEnd()
end 

local function rods(r)

local controlRods = r.getControlRodName(0)
local rodCount = r.getNumberOfControlRods(r)-1

    for xi = 0, rodCount, 1 do
 
        local effective = 0
        local name = r.getControlRodName(xi)
        local level = (100-r.getControlRodLevel(xi))

        term.setCursorPos(1, (yaxis + xi))
        term.setTextColor(white)
        write(name)
        term.setCursorPos(20, (yaxis + xi))
            if level >= 80 then
                term.setTextColor(green)
                rWorking = rWorking + 1
            else if level >= 60 then
                term.setTextColor(yellow)
                   rEffectTot = rEffectTot + 1
                warningCUp()
            else if level >= 40 then
                term.setTextColor(orange)
                rEffectTot = rEffectTot + 1
                warningCUp()
            else
                term.setTextColor(red)
                rEffectTot = rEffectTot + 1
                warningCUp()
            end
            end
            end

        term.write(string.format(level))
          term.setTextColor(white)
          term.write("%")
         term.setCursorPos(25, (yaxis + xi))
          term.write("effective")
    end
    yaxis = yaxis + (rodCount + 1)
end

function check_errors(r)
    if warningCount >= 1 then
        return true   
    else
        return false
    end
end

function eStoredW(r)
        if energyStored >= 1 then
            write(ew)
            nLine()

        end
end

function fStoredW(r)
    if fuelStored >= 1 then
            write(fw)
            nLine()
    end
end

function wStoredW(r)
        if wasteStored >= 1 then
            write(ww)
            nLine()
    end
end

function rWarning(r)
        
    local controlRodsW = r.getControlRodName(0)
    local rodCountW = r.getNumberOfControlRods(r) - 1

    while rEffect < rEffectTot  do
            divideline()
            for xiw = 0, rodCountW, 1 do

                local nameW = r.getControlRodName(xiw)
                local levelW = (100-r.getControlRodLevel(xiw))

                    if levelW < 80 then
                        term.setCursorPos(1, yaxis)
                        term.write(nameW)
                        term.setCursorPos(13, yaxis)
                          term.write("is at: " ..levelW.."% outp. reduced")
                         rEffect = rEffect + 1
                         nLine()
                     end
            end
     end 
end

function found_errors(r)
        if warningCount <= maxWarn then
            term.setTextColor(orange)
        else if warningCount > maxWarn then
             term.setTextColor(red)
         end
         end
    term.setCursorPos(1, yaxis)   
    term.write("Warnings: ")
    term.setCursorPos(20, yaxis)
    term.write("Total: " ..warningCount)
    nLine()
    eStoredW()
    fStoredW()
    wStoredW()
    rWarning(reactors[selected_reactor])
end

function no_errors(r)
    term.setTextColor(green)
    write("no warnings found, reactor is functioning as expected")
end

function reset()
        sleep(0.5)
        yaxis = 1
       warningCount = 0                   
       rEffect = 0
       rEffectTot = 0
end


while true do
        is_connected()
        init_monitors()
           term.redirect(display)
           while is_connected() == true do
            select_reactor()
            local sReactor = reactors[selected_reactor]
                term.clear()
                display.setTextScale(0.5)
                term.setCursorPos(1,1)
                term.setTextColor(white)
                generalinfo()
                divideline()
                get_active(sReactor)
                rf_generated(sReactor)
                get_energylvl(sReactor)
                casing_heat(sReactor)
                fuel_heat(sReactor)
                fuel_consumption(sReactor)
                time_remaining(sReactor)
                fuel_amount(sReactor)
                waste_amount(sReactor)
                divideline()
                rods(sReactor)
                divideline()
                    if check_errors(sReactor) == true then
                        found_errors(sReactor)
                    else
                        no_errors()
                       end
                reset()
        end
            while is_connected() == false do
                term.clear()
                display.setTextScale(2)
                    term.setCursorPos(1, 1)
                    term.setTextColor(red)
                    write("CRITICAL ERROR: no reactors connected to network")
                  sleep(0.5)
              end
end