Advertisement
Lyqyd

Reactor Control

May 21st, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.89 KB | None | 0 0
  1. local modemSide, reactorSide
  2.  
  3. for _, side in ipairs(rs.getSides()) do
  4.     if peripheral.getType(side) == "modem" then
  5.         local wirelessMethod = false
  6.         for i, method in ipairs(peripheral.getMethods(side)) do
  7.             if method == "isWireless" then
  8.                 wirelessMethod = true
  9.                 if peripheral.call(side, "isWireless") then
  10.                     modemSide = side
  11.                 end
  12.                 break
  13.             end
  14.         end
  15.         if not wirelessMethod then
  16.             modemSide = side
  17.         end
  18.     elseif peripheral.getType(side) == "BigReactors-Reactor" then
  19.         reactorSide = side
  20.     end
  21.     if modemSide and reactorSide then break end
  22. end
  23.  
  24. local modem, reactor
  25.  
  26. if modemSide then
  27.     rednet.open(modemSide)
  28.     modem = peripheral.wrap(modemSide)
  29. end
  30. if reactorSide then
  31.     reactor = peripheral.wrap(reactorSide)
  32. else
  33.     error("No Reactor Found")
  34. end
  35.  
  36. local function sendUpdate(id, message)
  37.     if modemSide then
  38.         rednet.send(id, message)
  39.     end
  40. end
  41.  
  42. local function average(tab)
  43.     local average = 0
  44.     for i = 1, #tab do
  45.         average = average + tab[i]
  46.     end
  47.     return average / #tab
  48. end
  49.  
  50. local function pollInformation(reactor)
  51.     local information = {}
  52.     information.reactor = {}
  53.     information.fuel = {}
  54.     information.rods = {}
  55.     information.cooling = {}
  56.     information.power = {}
  57.     do
  58.         information.reactor.active = reactor.getActive()
  59.         information.reactor.activeCooling = reactor.isActivelyCooled()
  60.         information.reactor.temperature = reactor.getCasingTemperature()
  61.     end
  62.     do
  63.         information.fuel.fuelAmount = reactor.getFuelAmount()
  64.         information.fuel.wasteAmount = reactor.getWasteAmount()
  65.         information.fuel.max = reactor.getFuelAmountMax()
  66.         information.fuel.reactivity = reactor.getFuelReactivity()
  67.         information.fuel.consumption = reactor.getFuelConsumedLastTick()
  68.         information.fuel.temperature = reactor.getFuelTemperature()
  69.     end
  70.     do
  71.         information.rods.count = reactor.getNumberOfControlRods()
  72.         information.rods.levels = {}
  73.         for i = 1, information.rods.count do
  74.             information.rods.levels[i] = reactor.getControlRodLevel(i - 1)
  75.         end
  76.         information.rods.average = average(information.rods.levels)
  77.     end
  78.     if information.reactor.activeCooling then
  79.         information.cooling.coldType = reactor.getCoolantType()
  80.         information.cooling.coldAmount = reactor.getCoolantAmount()
  81.         information.cooling.hotType = reactor.getHotFluidType()
  82.         information.cooling.hotAmount = reactor.getHotFluidAmount()
  83.         information.cooling.hotProduced = reactor.getHotFluidProducedLastTick()
  84.     end
  85.     if not information.reactor.activeCooling then
  86.         information.power.produced = reactor.getEnergyProducedLastTick()
  87.         information.power.stored = reactor.getEnergyStored()
  88.         information.power.percentage = math.floor(information.power.stored / 100000)
  89.     end
  90.     return information
  91. end
  92.  
  93. local settings = {
  94.     rods = {
  95.         setpoint = 0,
  96.         maximum = 100,
  97.         minimum = 0,
  98.         mode = "auto",
  99.         --values are auto, operator, startup.
  100.     },
  101.     reactor = {
  102.         active = true,
  103.     },
  104.     general = {
  105.         slowStartTickDelayCount = 4,
  106.         slowStartSteamTickDelayCount = 20,
  107.     }
  108. }
  109.  
  110. local startupCount = 0
  111. local lastRodsSetting
  112.  
  113. local function printCenter(text)
  114.     text = tostring(text)
  115.     local x, y = term.getSize()
  116.     local xCur, yCur = term.getCursorPos()
  117.     term.setCursorPos((x - #text) / 2 + 1, yCur)
  118.     term.write(text)
  119. end
  120.  
  121. local function displayScreen(info)
  122.     local x, y = term.getSize()
  123.     local column = math.ceil(x / 2)
  124.     term.setBackgroundColor(term.isColor() and colors.gray or colors.black)
  125.     term.setTextColor(colors.white)
  126.     term.clear()
  127.     do
  128.         term.setCursorPos(2, 3)
  129.         term.write("Temperature:")
  130.         term.setCursorPos(2, 4)
  131.         term.write("Core: "..info.fuel.temperature)
  132.         term.setCursorPos(2, 5)
  133.         term.write("Case: "..info.reactor.temperature)
  134.     end
  135.     do
  136.         term.setCursorPos(2, 7)
  137.         term.write("Fuel:")
  138.         term.setCursorPos(2, 8)
  139.         term.write("Fuel Level: "..tostring(info.fuel.fuelAmount).." mB")
  140.         term.setCursorPos(2, 9)
  141.         term.write("Waste Level: "..tostring(info.fuel.wasteAmount).." mB")
  142.         term.setCursorPos(2, 10)
  143.         term.write("Total: "..tostring(info.fuel.fuelAmount + info.fuel.wasteAmount).." mB")
  144.         term.setCursorPos(2, 11)
  145.         term.write("Capacity: "..tostring(info.fuel.max).." mB")
  146.         term.setCursorPos(2, 12)
  147.         term.write("Consumed: "..string.match(string.format("%f", info.fuel.consumption).."0000", "(%d+%.%d%d%d%d)").." mB/t")
  148.         term.setCursorPos(2, 13)
  149.         term.write("Reactivity: "..string.match(string.format("%f", info.fuel.reactivity).."00", "(%d+%.%d%d)").."%")
  150.     end
  151.     do
  152.         term.setCursorPos(column, 3)
  153.         term.write("Control Rods:")
  154.         term.setCursorPos(column, 4)
  155.         term.write("Actual: "..tostring(info.rods.average).."%")
  156.         term.setCursorPos(column, 5)
  157.         term.write("Setpoint: "..tostring(settings.rods.setpoint).."%")
  158.         do
  159.             if term.isColor() then term.setBackgroundColor(colors.red) end
  160.             term.setCursorPos(column, 6)
  161.             term.write("--")
  162.             term.setCursorPos(column + 3, 6)
  163.             term.write("-")
  164.             if term.isColor() then term.setBackgroundColor(colors.yellow) end
  165.             term.setCursorPos(column + 5, 6)
  166.             term.write("=")
  167.             if term.isColor() then term.setBackgroundColor(colors.lime) end
  168.             term.setCursorPos(column + 7, 6)
  169.             term.write("+")
  170.             term.setCursorPos(column + 9, 6)
  171.             term.write("++")
  172.             if term.isColor() then term.setBackgroundColor(colors.blue) end
  173.             term.setCursorPos(column + 12, 6)
  174.             term.write("A")
  175.             if term.isColor() then term.setBackgroundColor(colors.gray) end
  176.         end
  177.     end
  178.     if info.reactor.activeCooling then
  179.         --active cooling set
  180.         term.setCursorPos(column, 8)
  181.         term.write("Cooling:")
  182.         term.setCursorPos(column, 9)
  183.         term.write("Steam Output: "..tostring(info.cooling.hotProduced).." mB/t")
  184.         term.setCursorPos(column, 10)
  185.         term.write("Steam Level: "..tostring(info.cooling.hotAmount).." mB")
  186.         term.setCursorPos(column, 11)
  187.         term.write("Water Level: "..tostring(info.cooling.coldAmount).." mB")
  188.     else
  189.         --passive cooling set
  190.         term.setCursorPos(column, 8)
  191.         term.write("Power:")
  192.         term.setCursorPos(column, 9)
  193.         term.write("RF Output: "..tostring(math.floor(info.power.produced)).." RF/t")
  194.         term.setCursorPos(column, 10)
  195.         term.write("RF Stored: "..tostring(info.power.stored).." RF")
  196.         term.setCursorPos(column, 11)
  197.         term.write("RF % Full: "..tostring(info.power.percentage).."%")
  198.     end
  199. end
  200.  
  201. local function handleScreen(event)
  202.     if event[1] == "mouse_click" then
  203.         local x, y = term.getSize()
  204.         local column = math.ceil(x / 2)
  205.         if event[4] == 6 then
  206.             if event[3] == column or event[3] == column + 1 then
  207.                 if event[2] == 1 then
  208.                     settings.rods.setpoint = math.max(0, settings.rods.setpoint - 10)
  209.                     settings.rods.mode = "operator"
  210.                 elseif event[2] == 2 then
  211.                     settings.rods.setpoint = 0
  212.                     settings.rods.mode = "operator"
  213.                 end
  214.             elseif event[3] == column + 3 then
  215.                 settings.rods.setpoint = math.max(0, settings.rods.setpoint - 1)
  216.                 settings.rods.mode = "operator"
  217.             elseif event[3] == column + 5 then
  218.                 settings.rods.setpoint = lastRodsSetting
  219.                 settings.rods.mode = "operator"
  220.             elseif event[3] == column + 7 then
  221.                 settings.rods.setpoint = math.min(100, settings.rods.setpoint + 1)
  222.                 settings.rods.mode = "operator"
  223.             elseif event[3] == column + 9 or event[3] == column + 10 then
  224.                 if event[2] == 1 then
  225.                     settings.rods.setpoint = math.min(100, settings.rods.setpoint + 10)
  226.                     settings.rods.mode = "operator"
  227.                 elseif event[2] == 2 then
  228.                     settings.rods.setpoint = 100
  229.                     settings.rods.mode = "operator"
  230.                 end
  231.             elseif event[3] == column + 12 then
  232.                 settings.rods.mode = "auto"
  233.             end
  234.         end
  235.     elseif event[1] == "char" then
  236.         if event[2] == "-" then
  237.             settings.rods.setpoint = math.max(0, settings.rods.setpoint - 1)
  238.             settings.rods.mode = "operator"
  239.         elseif event[2] == "=" then
  240.             settings.rods.setpoint = lastRodsSetting
  241.             settings.rods.mode = "operator"
  242.         elseif event[2] == "+" then
  243.             settings.rods.setpoint = math.min(100, settings.rods.setpoint + 1)
  244.             settings.rods.mode = "operator"
  245.         elseif string.lower(event[2]) == "a" then
  246.             settings.rods.mode = "auto"
  247.         end
  248.     end
  249. end
  250.  
  251. local function setRods(setpoint)
  252.     if setpoint ~= lastRodsSetting then
  253.         reactor.setAllControlRodLevels(math.max(settings.rods.minimum, math.min(settings.rods.maximum, setpoint)))
  254.         sendUpdate(masterControl, "rods.actual:"..tostring(setpoint))
  255.         lastRodsSetting = setpoint
  256.     end
  257. end
  258.  
  259. local tickRods = {
  260.     operator = function(info)
  261.         setRods(settings.rods.setpoint)
  262.     end,
  263.     startup = function(info)
  264.         startupCount = startupCount + 1
  265.         if info.power.percentage < info.rods.average then
  266.             if startupCount >= settings.general.slowStartTickDelayCount then
  267.                 setRods(info.rods.average - 1)
  268.                 startupCount = 0
  269.             end
  270.         else
  271.             setRods(info.power.percentage)
  272.             settings.rods.mode = "auto"
  273.             print("Reactor in AUTO mode")
  274.         end
  275.     end,
  276.     auto = function(info)
  277.         setRods(info.power.percentage)
  278.     end,
  279.     steamStartup = function(info)
  280.         --
  281.     end,
  282. }
  283.  
  284. if fs.exists("reactorsettings") then
  285.     dofile("reactorsettings")
  286. end
  287.  
  288. local reactorTick = os.startTimer(0.5)
  289. print("Reactor Control Started")
  290.  
  291. while true do
  292.     local info = pollInformation(reactor)
  293.     displayScreen(info)
  294.     local event = {os.pullEvent()}
  295.     if event[1] == "rednet_message" then
  296.         if event[2] == masterControl then
  297.             if event[3] == "stop" then
  298.                 print("Received STOP command")
  299.                 settings.reactor.active = false
  300.                 sendUpdate(masterControl, "reactor.active:false")
  301.                 reactor.setActive(false)
  302.             elseif event[3] == "start" then
  303.                 print("Received START command")
  304.                 settings.reactor.active = true
  305.                 sendUpdate(masterControl, "reactor.active:true")
  306.                 reactor.setActive(true)
  307.                 reactor.setAllControlRodLevels(99)
  308.                 settings.rods.mode = "startup"
  309.                 sendUpdate(masterControl, "rods.mode:startup")
  310.                 print("Reactor in STARTUP mode")
  311.                 startupCount = 0
  312.                 reactorTick = os.startTimer(0.5)
  313.             end
  314.         end
  315.     elseif event[1] == "timer" then
  316.         if event[2] == reactorTick then
  317.             if tickRods[settings.rods.mode] then
  318.                 tickRods[settings.rods.mode](info)
  319.             end
  320.             reactorTick = os.startTimer(0.5)
  321.         end
  322.     else
  323.         handleScreen(event)
  324.     end
  325. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement