Hiranus icon

Reactor + Turbine Manager

Hiranus | PRO | 05/03/14 09:37:06 AM UTC | 0 ⭐ | 620 👁️ | Never ⏰ | []
Lua |

8.2 KB

|

None

|

0 👍

/

0 👎

 
 
--this program can be used to manage Big Reactors
--Reactors & Turbines
 
--important note
--your reactor HAS to be set to manual waste ejection
 
 
 
local _interval = 1 -- in sec
local reactor, turbine, monitor
 
local isReactor 
local isMonitor 
local isTurbine 
local _maxEnergy = 10000000
local _maxEnergyTurbine = 1000000
local _cyaniteMode = false
local _cyaniteToCreate = 0
local _loop = true
    local turbinePowerToEnableReactor = 50 -- in % , at 0 reactor will run only if turbine has no power
 
function search(perToFind)
    local printMode = false
    if perToFind == "print" then
        printMode=true
    end
    if printMode == true then
        term.clear()
        term.setCursorPos(1,1)
    end
    local sides = { "top", "bottom", "left", "right", "front", "back" }
    for i = 1, #sides do
        if peripheral.isPresent(sides[i]) then
            if peripheral.getType(sides[i]) == "modem" then
                local modem = peripheral.wrap(sides[i])
                local methods = peripheral.getMethods(sides[i])
                local wired = false
                for j = 1, #methods do
                    if methods[j] == "getNamesRemote" then
                        wired=true
                    end
                end
                if wired == true then
                    if printMode == true then
                        print("There is " ..peripheral.getType(sides[i]).." on the "..sides[i] )
                    else
                        if perToFind==peripheral.getType(sides[i]) then
                            return sides[i]
                        end
                    ----------------------------------------------------------------
                    end
                    local net = modem.getNamesRemote()
                    for j = 1, #net do
                        if printMode == true then       
                            print("   ".. peripheral.getType(net[j]))
                        else
                            if perToFind==peripheral.getType(net[j]) then
                                return net[j]
                            end 
                        ---------------------------------
                        end
                    end
                else
                    if printMode == true then
                        print("There is wireless modem on the "..sides[i] )
                    else
                        if perToFind==peripheral.getType(sides[i]) then
                            return sides[i]
                        end
                    
                    ---------------------------------
                    end
                end
            else
                if printMode == true then
                    print("There is " ..peripheral.getType(sides[i]).." on the "..sides[i] )
                else
                    if perToFind==peripheral.getType(sides[i]) then
                            return sides[i]
                        end
                -----------------------
                end 
            end
        else
        if printMode == true then
            print("There is nothing on the "..sides[i] )
        end
        end
    end
    return nil
end
 
 
function wrapper ()
    local r1 = search("BigReactors-Reactor")
    
    if r1 ~= nil then
        reactor = peripheral.wrap(r1)
        if reactor.getConnected() == true then
            isReactor = true
        else
            isReactor = false
        end
        isReactor = true
    else
        isReactor = false
    end
    
    local t1 = search("BigReactors-Turbine")
    
    if t1 ~= nil then 
        turbine = peripheral.wrap(t1)
        if turbine.getConnected() == true then  
            isTurbine = true
        else
            isTurbine = false
        end
    else
        isTurbine = false
    end
    
    local m1 = search("monitor")
    
    if m1 ~= nil then 
        monitor = peripheral.wrap(m1)
        isMonitor = true
    else
        isMonitor = false
    end
    
end
 
function newLine(obj)
  local _,cY= obj.getCursorPos()
  obj.setCursorPos(1,cY+1)
end
 
function clear(obj)
    obj.clear()
    obj.setCursorPos(1,1)
end
 
function clearMarks(obj,number)
    local x,y = obj.getCursorPos()
    local obj_x,obj_y = obj.getSize()
    while x+number > obj_x do
        number=number-1
        if number>= 0 then
            return
        end
    end
    for i=1,number do
        obj.write(" ")
    end
    obj.setCursorPos(x,y)
end
local arr_x={}
local arr_y={}
 
function prepare (obj)
    clear(obj)
    if isReactor then
        obj.write("Reactor Status:")
        newLine(obj)        
        obj.write("Energy stored: ")
        arr_x[1],arr_y[1] = obj.getCursorPos()
        newLine(obj)
        obj.write("Energy produced: ")
        arr_x[2],arr_y[2] = obj.getCursorPos()
        newLine(obj)
        obj.write("Fuel temperature: ")
        arr_x[3],arr_y[3] = obj.getCursorPos()
        newLine(obj)
        obj.write("Waste amount: ")
        arr_x[4],arr_y[4] = obj.getCursorPos()
        newLine(obj)
        obj.write("Reactivity: ")
        arr_x[5],arr_y[5] = obj.getCursorPos()
        newLine(obj)
        obj.write("Fuel burnup: ")
        arr_x[6],arr_y[6] = obj.getCursorPos()
    end
    if isTurbine then
        newLine(obj)
        newLine(obj)
        obj.write("Turbine Status:")
        newLine(obj)
        obj.write("Energy stored: ")
        arr_x[7],arr_y[7] = obj.getCursorPos()
        newLine(obj)
        obj.write("Energy produced: ")
        arr_x[8],arr_y[8] = obj.getCursorPos()
        newLine(obj)
        obj.write("Rotor speed: ")
        arr_x[9],arr_y[9] = obj.getCursorPos()
    end
    newLine(obj)
    newLine(obj)
end
 
function show (obj)
    local x,y = obj.getCursorPos()
    if isReactor then       
        obj.setCursorPos(arr_x[1],arr_y[1])
        obj.write((math.floor(reactor.getEnergyStored()/_maxEnergy*1000)/10).." %")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[2],arr_y[2])
        obj.write((math.floor(reactor.getEnergyProducedLastTick())).. " RF")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[3],arr_y[3])
        obj.write((math.floor(reactor.getFuelTemperature())).. " C")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[4],arr_y[4])
        obj.write(reactor.getWasteAmount().. " mB")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[5],arr_y[5])
        obj.write((math.floor(reactor.getFuelReactivity())).. " %")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[6],arr_y[6])
        obj.write((math.floor(reactor.getFuelConsumedLastTick()*1000)/1000).. " mB")
        clearMarks(obj,5)
    end
    if isTurbine then
        obj.setCursorPos(arr_x[7],arr_y[7])
        obj.write((math.floor(turbine.getEnergyStored()/_maxEnergyTurbine*1000)/10).." %")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[8],arr_y[8])
        obj.write((math.floor(turbine.getEnergyProducedLastTick())).. " RF")
        clearMarks(obj,5)
        obj.setCursorPos(arr_x[9],arr_y[9])
        obj.write((math.ceil(turbine.getRotorSpeed()*10)/10).. " RPM")
        clearMarks(obj,5)
    end
    obj.setCursorPos(x,y)
end
 
function eject ()
    local wasteAmount = reactor.getWasteAmount() 
    if wasteAmount > 1000 then
        reactor.doEjectWaste()
        return true
    else
        return false
    end
end
 
function cyanite ()
    local ejected = eject()
    if ejected == true then
        _cyaniteToCreate = _cyaniteToCreate - 1
        if _cyaniteToCreate <= 0 then
            _cyaniteMode = false
        end
    end
end
 
function updateReactor()
 eject()
  local rodControl = math.floor((reactor.getEnergyStored()/_maxEnergy)*100)
  if isTurbine then
    local turbineEnergy = math.floor(turbine.getEnergyStored()/_maxEnergy*100)
    local turbineMultipler = 100 / turbinePowerToEnableReactor
    local turbineControl = turbineEnergy * turbineMultipler
    if rodControl < turbineControl then
        if turbineControl > 100 then
            rodControl = 100
        else
            rodControl = turbineControl
        end
    end
  end
  reactor.setAllControlRodLevels(rodControl)
end
 
function psleep(n)
    local tTimer = os.startTimer(n)
    for sEvent, t in os.pullEventRaw do
        if sEvent == "timer" and t == tTimer then
            break
        end
    end
end
 
function reactorManagement()
    if _cyaniteMode == true then
        cyanite()
    else
        updateReactor()
    end
end
 
function menu()
    if isMonitor == true then
        clear(term)
    end
    print("1. Cyanite production")
    print("2. Normal reactor work")
    print("3. Shutdown")
    local x,y = term.getCursorPos()
    while _loop do
        local keyPressed = read()
        if keyPressed == "1" then
            term.write("How many: ")
            _cyaniteToCreate = tonumber(read())
            _cyaniteMode = true
            reactor.setAllControlRodLevels(0)
        elseif keyPressed == "2" then
            _cyaniteToCreate = 0
            _cyaniteMode = false
        elseif keyPressed == "3" then 
            _loop = false
            break
        end
        local x2,y2 = term.getCursorPos()
        for i=y,y2 do
            term.setCursorPos(1,i)
            term.clearLine()
        end
        term.setCursorPos(x,y)
    end
end
 
function mainLoop()
  if isReactor == true then
   if reactor.getActive() == false then
        reactor.setActive(true)
   end
  end
    if isTurbine then
        if turbine.getActive() == false then
            turbine.setActive(true)
        end
    end
    while _loop do
        psleep(_interval)
        if isReactor == true then
            reactorManagement()
        end
        if isMonitor==true then
            show(monitor)
        else
            show(term)
        end
    end
    if isReactor == true then
        reactor.setActive(false)
    end
end
 
wrapper()
if isMonitor == true then
    prepare(monitor)
else
    prepare(term)
end
parallel.waitForAll(mainLoop, menu)

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎