Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Basic control program for an actively cooled reactor. Will automatically detect a reactor and a monitor, either through direct proximity or a wired modem. Please ensure the computer and monitors are the advanced version (it's just gold, not exactly rare in Minecraft). Designed for a 4x3 monitor.
- local progVer = "1.07"
- local progName = "IC_SimpleNuke"
- local extrastuff = "Because manual control is annoying!"
- local loopTime = 0.5 -- Time between program loops, in seconds. Increase this value if program somehow causes a lot of lag. Which should not happen on any computers made this century
- local baseControlRodLevel = 60 -- Put this as close to what you guess the optimum control rod level will be for your reactor, plus a little more
- local minReactorTemp = 200 -- Minimum reactor temperature (^C) to maintain
- local maxReactorTemp = 300 -- Maximum reactor temperature (^C) to maintain
- ----------
- -- Basic side determination for wrapping
- function getDeviceSide(deviceType)
- deviceType = deviceType:lower()
- for i, side in pairs(rs.getSides()) do
- if (peripheral.isPresent(side)) then
- if (string.lower(peripheral.getType(side)) == deviceType) then
- return side;
- end
- end
- end
- return nil;
- end
- -- Peripheral wrapping function
- function wrapThis(thing, f)
- local wrapped = nil
- while wrapped == nil and f <= 100 do
- wrapped = peripheral.wrap(thing.."_"..f)
- f = f + 1
- end
- if wrapped == nil then
- side = getDeviceSide(thing)
- if side ~= nil then
- return peripheral.wrap(side)
- else
- return nil
- end
- else
- return wrapped
- end
- end
- print("Initializing program...",1,1);
- -- Initialize the monitor
- -- If it can't find a monitor, will wait 5 seconds then reboot
- -- This is needed for server restarts when the computer starts up before the modem does
- local monitor = wrapThis("monitor", 0)
- if monitor == nil then
- print("Can't find monitor.")
- sleep(5)
- finished = true
- os.reboot()
- end
- -- local monitorx, monitory = monitor.getSize()
- -- if monitorx ~= 29 or monitory ~= 12 then
- -- error("Monitor is the wrong size! Needs to be 3x2.")
- -- sleep(5)
- -- finished = true
- -- os.reboot()
- -- end
- -- Connect to the big reactor peripheral
- -- If it can't find a reactor, will wait 5 seconds then reboot
- -- This is needed for server restarts when the computer starts up before the modem does
- local reactor = wrapThis("BigReactors-Reactor", 0)
- if reactor == nil then
- print("Can't find reactor.")
- sleep(5)
- finished = true
- os.reboot()
- end
- -- Easyprint, saves program space elsewhere
- local function print(str, x, y)
- term.setCursorPos(x, y)
- term.write(str)
- end
- -- Move stuff to monitor
- if monitor then
- term.clear()
- term.setCursorPos(1,1)
- term.write("Display redirected to Monitor. Type r to reboot, or")
- term.setCursorPos(1,2)
- term.write("q to quit and return control to this terminal.")
- term.redirect(monitor)
- term.clear()
- end
- -- Restore functions to computer terminal
- function restoreNativeTerminal()
- repeat
- term.restore()
- local w, h = term.getSize()
- until w == 51 and h == 19
- end
- -- Draw some decorative lines
- function drawLines()
- term.setBackgroundColor(colors.black)
- paintutils.drawLine(1, 3, 39, 3, colors.lightBlue)
- paintutils.drawLine(1, 9, 39, 9, colors.lightBlue)
- term.setBackgroundColor(colors.black)
- end
- -- Just to make sure they're relatively high before the control program starts
- if reactor then
- reactor.setAllControlRodLevels(baseControlRodLevel)
- else
- print("Can't find reactor.")
- sleep(5)
- finished = true
- os.reboot()
- end
- -- Draw stuff on the monitor
- local function drawInfo()
- statusstring = "Reactor status: "
- tempstring = "Temperature: "
- tempstring2 = "deg. C"
- steamstring = "Steam Output: "
- steamstring2 = "mb/t"
- rodstring = "Control Rods: "
- rodstring2 = "%"
- fuelstring = "Fuel Usage: "
- fuelstring2 = "mb/t"
- monitor.setTextColor(colors.green)
- print(progName,10,1)
- monitor.setTextColor(colors.red)
- print("v",24,1)
- monitor.setTextColor(colors.green)
- print(progVer,25,1)
- monitor.setTextColor(colors.blue)
- print(extrastuff,4,2)
- monitor.setTextColor(colors.white)
- print(statusstring,1,4)
- if reactor.getActive() then
- monitor.setTextColor(colors.green)
- print(" online",33,4)
- else
- monitor.setTextColor(colors.red)
- print("offline",33,4)
- end
- monitor.setTextColor(colors.white)
- reactortemp = math.floor(reactor.getFuelTemperature())
- print(tempstring,1,5)
- print(reactortemp,23,5)
- print(tempstring2,34,5)
- steam = math.floor(reactor.getEnergyProducedLastTick())
- print(steamstring,1,6)
- print(steam,23,6)
- print(steamstring2,36,6)
- rods = reactor.getControlRodLevel(1)
- print(rodstring,1,7)
- print(rods,23,7)
- print(rodstring2,39,7)
- fuelbase = reactor.getFuelConsumedLastTick()
- fuel = (math.floor(fuelbase * 10000)) / 10000
- print(fuelstring,1,8)
- print(fuel,23,8)
- print(fuelstring2,36,8)
- end
- -- Draw stuff on the bottom part of the monitor, power bar etc
- function drawBottomPart()
- reactivity = math.floor(reactor.getFuelReactivity())
- paintutils.drawLine(2, 11, 38, 11, colors.gray)
- if reactivity > 400 then
- paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.red)
- elseif reactivity > 320 then
- paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.orange)
- elseif reactivity > 240 then
- paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.yellow)
- elseif reactivity > 160 then
- paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.green)
- elseif reactivity > 0 then
- paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.blue)
- end
- term.setBackgroundColor(colors.black)
- print("Fuel Reactivity:",1,10)
- print(reactivity,34,10)
- print("%",39,10)
- term.setBackgroundColor(colors.black)
- reactortemp = math.floor(reactor.getFuelTemperature())
- paintutils.drawLine(2, 13, 38, 13, colors.gray)
- if reactortemp > 1000 then
- paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.red)
- elseif reactortemp > 800 then
- paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.orange)
- elseif reactortemp > 600 then
- paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.yellow)
- elseif reactortemp > 400 then
- paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.green)
- elseif reactortemp > 200 then
- paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.blue)
- end
- term.setBackgroundColor(colors.black)
- print("Reactor heat: ",1,12)
- print(reactortemp,24,12)
- print("deg C",29,12)
- term.setBackgroundColor(colors.black)
- end
- -- Controls the reactor, keeping it between configured temperature values
- function control()
- reactortemp = math.floor(reactor.getFuelTemperature())
- hdiff = (maxReactorTemp - minReactorTemp)
- hdiffper = hdiff / 100
- if reactortemp < minReactorTemp then
- reactor.setActive(true)
- reactor.setAllControlRodLevels(0)
- elseif reactortemp > maxReactorTemp then
- reactor.setActive(true)
- reactor.setAllControlRodLevels(100)
- elseif reactortemp > minReactorTemp and reactortemp < maxReactorTemp then
- reactor.setActive(true)
- blarg = reactortemp - minReactorTemp
- blarg2 = math.floor(blarg/(hdiffper))
- reactor.setAllControlRodLevels(blarg2)
- end
- end
- -- Main program
- function main()
- while not finished do
- if reactor.getConnected() then
- drawLines()
- drawInfo()
- drawBottomPart()
- control()
- sleep(loopTime)
- end
- end
- end
- -- Event handler for exiting, editting and debugging
- function eventHandler()
- while not finished do
- event, arg1, arg2, arg3 = os.pullEvent()
- if event == "char" and not inManualMode then
- local ch = string.lower(arg1)
- if ch == "q" then
- finished = true
- elseif ch == "r" then
- finished = true
- os.reboot()
- end
- end
- end
- end
- while not finished do
- parallel.waitForAny(eventHandler, main)
- sleep(loopTime)
- end
- -- Returns control to the terminal if the program has an error or whatever
- term.clear()
- term.setCursorPos(1,1)
- restoreNativeTerminal()
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment