Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Low energy mark: raise the rods ( lower the value )
- lowEnergy=1000000
- -- High energy mark: lower the rods (raise the value )
- highEnergy=9000000
- --
- -- Decide if the rod level should change
- --
- -- * If cellEnergy < lowEnergy remove 1% (speed up)
- -- * If cellEnergy > highEnergy add 1% (slow down)
- --
- -- Parameters:
- -- rodLvl - current level of the rods
- -- cellEnergy - stored energy of internal cell in RF
- --
- -- Return:
- -- New control rod level in %.
- --
- function calcRodLevel(rodLvl, cellEnergy)
- if cellEnergy < lowEnergy then
- return rodLvl - 1
- elseif cellEnergy > highEnergy then
- return rodLvl + 1
- else
- return rodLvl
- end
- end
- --
- -- Write text with colors, if possible (advance monitor)
- --
- -- Parameters:
- -- mon - handle of monitor
- -- color - text color
- -- text - text to write
- --
- function writeColor(mon, color, text)
- if (mon.isColor()) then
- mon.setTextColor(color)
- end
- mon.write(text)
- if (mon.isColor()) then
- mon.setTextColor(colors.white)
- end
- end
- --
- -- Display reactor status to a monitor.
- --
- -- Parameters:
- -- mon - handle of monitor.
- -- state - state of reactor (on/off)
- -- rodLvl - Level of control rods in %
- -- cellEnergy - stored energy of internal cell (in RF)
- --
- function displayStatusToMonitor(mon, state, rodLvl, cellEnergy)
- mon.clear()
- -- First get the monitor size and try to scale, if the feature is
- -- available.
- if mon.setTextScale ~= nil then
- -- reset text scale
- mon.setTextScale(1)
- end
- local width, height = mon.getSize()
- if width < 15 or height < 5 then
- -- too small: try to scale down.
- if mon.setTextScale ~= nil then
- mon.setTextScale(0.5)
- else
- return -- too small und no text scale available
- end
- width, height = mon.getSize()
- if width < 15 or height < 5 then
- return -- still too small
- end
- else
- -- Huge monitors? Try to scale up, if possible (max scale=5).
- local scale = math.min(width / 16, height / 5, 5)
- scale = math.floor(scale * 2) / 2 -- multiple of 0.5
- if scale > 1 and mon.setTextScale ~= nil then
- mon.setTextScale(scale)
- width, height = mon.getSize()
- end
- end
- --
- -- Output the data
- --
- mon.setCursorPos(1,1)
- mon.write("Reactor")
- mon.setCursorPos(1,3)
- mon.write("Status ")
- if state then
- writeColor(mon, colors.green, "ON")
- else
- writeColor(mon, colors.red, "OFF")
- end
- mon.setCursorPos(1,4)
- mon.write("Rod Level: " .. rodLvl .. "%")
- mon.setCursorPos(1,5)
- if width < 16 then
- mon.write("Cell: ") -- One block monitor (15x5 with scale 0.5)
- else
- mon.write("Energy: ")
- end
- local c
- if cellEnergy < critEnergy then
- c = colors.red -- Red: We use too much energy
- elseif cellEnergy > lowEnergy then
- c = colors.green -- Green: More energy then low water mark
- else
- c = colors.orange -- Orange: Less energy the low water mark, but OK
- end
- writeColor(mon, c, string.format("%d", math.floor(cellEnergy/1000 + 0.5)))
- mon.write(" kRF")
- end
- --
- -- Display reactor status to any connected monitor and also to console.
- --
- -- Parameters:
- -- state - state of reactor (on/off)
- -- rodLvl - Level of control rods in %
- -- cellEnergy - stored energy of internal energy cell in RF
- --
- function displayStatus(state, rodLvl, cellEnergy)
- displayStatusToMonitor(term, state, rodLvl, cellEnergy) -- console
- term.setCursorPos(1,7)
- term.write("* Hold Crtl-T to terminate program")
- term.setCursorPos(1,8)
- local pList = peripheral.getNames()
- local i, name
- for i, name in pairs(pList) do
- if peripheral.getType(name) == "monitor" then
- -- found monitor as peripheral
- displayStatusToMonitor(peripheral.wrap(name),
- state, rodLvl, cellEnergy)
- end
- end
- end
- --
- -- Find the first connected big reactor and return the wraped handle.
- --
- -- If no reactor was found this function terminate the program.
- --
- -- Return:
- -- Handle of first connected reactor found.
- --
- function getReactorHandle()
- local pList = peripheral.getNames()
- local i, name
- for i, name in pairs(pList) do
- if peripheral.getType(name) == "BigReactors-Reactor" then
- return peripheral.wrap(name)
- end
- end
- error("No big reactor connected: Exit program")
- exit()
- end
- reactor = getReactorHandle()
- --
- -- Endless loop: Recalculate rod level, set rod level, display result
- -- and wait for 5 secounds.
- --
- rodLvl = 100
- reactor.setAllControlRodLevels(rodLvl)
- reactor.setActive(true)
- while true do
- cellEnergy = reactor.getEnergyStored()
- rodLvl=calcRodLevel(rodLvl, cellEnergy)
- reactor.setAllControlRodLevels(rodLvl)
- displayStatus(reactor.getActive(), rodLvl, cellEnergy)
- os.sleep(20) -- Wait for 20s
- end
- --
- -- EOF
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement