Advertisement
Guest User

reacvtor2

a guest
Jul 24th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local progVer = "Core 1"
  2. local coreNr = "Kern 1"
  3. local progName = "Reactor"
  4. 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
  5. local baseControlRodLevel = 20 -- Put this as close to what you guess the optimum control rod level will be for your reactor, plus a little more
  6. local minStoredEnergyPercent = 20 -- Min energy % to store before activating, probably keep it at default (20)
  7. local maxStoredEnergyPercent = 80 -- Max energy % to store before shutdown, probably keep it at default (70)
  8. -- This program doesn't care about heat anymore, only tried to maintain the internal power buffer, following 2 lines only included for legacy
  9. local minReactorTemp = 20 -- Minimum reactor temperature (^C) to maintain
  10. local maxReactorTemp = 400 -- Maximum reactor temperature (^C) to maintain
  11.  
  12. ----------
  13.  
  14. -- Basic side determination for wrapping
  15. function getDeviceSide(deviceType)
  16.     deviceType = deviceType:lower()
  17.    
  18.     for i, side in pairs(rs.getSides()) do
  19.         if (peripheral.isPresent(side)) then
  20.             if (string.lower(peripheral.getType(side)) == deviceType) then
  21.                 return side;
  22.             end
  23.         end
  24.     end
  25.    
  26.     return nil;
  27. end
  28.  
  29. -- Peripheral wrapping function
  30. function wrapThis(thing, f)
  31.     local wrapped = nil
  32.     while wrapped == nil and f <= 100 do
  33.         wrapped = peripheral.wrap(thing.."_"..f)
  34.         f = f + 1
  35.     end
  36.  
  37.     if wrapped == nil then
  38.         side = getDeviceSide(thing)
  39.         if side ~= nil then
  40.             return peripheral.wrap(side)
  41.         else
  42.             return nil
  43.         end
  44.     else
  45.         return wrapped
  46.     end
  47. end
  48.  
  49.  
  50. print("Initializing program...",1,1);
  51.  
  52. -- Initialize the monitor
  53. -- If it can't find a monitor, will wait 5 seconds then reboot
  54. -- This is needed for server restarts when the computer starts up before the modem does
  55. local monitor = wrapThis("monitor", 0)
  56. if monitor == nil then
  57.     sleep(5)
  58.     finished = true
  59.     print("Can´t find a monitor!",1,1)
  60.     sleep(10)
  61.     os.reboot()
  62. end
  63. local monitorx, monitory = monitor.getSize()
  64. if monitorx ~= 29 or monitory ~= 12 then
  65.     print("Monitor size must be 3x2!")
  66.     sleep(10)
  67.     finished = true
  68.     os.reboot()
  69. end
  70.  
  71. -- Connect to the big reactor peripheral
  72. -- If it can't find a reactor, will wait 5 seconds then reboot
  73. -- This is needed for server restarts when the computer starts up before the modem does
  74. local reactor = wrapThis("BigReactors-Reactor", 0)
  75. if reactor == nil then
  76.     sleep(10)
  77.     finished = true
  78.     os.reboot()
  79. end
  80.  
  81. -- Easyprint, saves program space elsewhere
  82. local function print(str, x, y)
  83.     term.setCursorPos(x, y)
  84.     term.write(str)
  85. end
  86.  
  87. -- Move stuff to monitor
  88. if  monitor then
  89.     sleep(1)
  90.     term.clear()
  91.     term.setCursorPos(1,1)
  92.     term.write("Reactor now showing on Monitor!")
  93.     term.setCursorPos(1,2)
  94.     term.write("Press r to reboot or q to quit")
  95.     term.redirect(monitor)
  96. end
  97.  
  98. -- Restore functions to computer terminal
  99. function restoreNativeTerminal()
  100.     repeat
  101.         term.restore()
  102.         local w, h = term.getSize()
  103.     until w == 51 and h == 19
  104. end
  105.  
  106. -- Draw some decorative lines
  107. function drawLines()
  108.     term.setBackgroundColor(colors.black)
  109.     paintutils.drawLine(1, 2, 29, 2, colors.lightBlue)
  110.     paintutils.drawLine(1, 7, 29, 7, colors.lightBlue)
  111.     term.setBackgroundColor(colors.black)
  112. end
  113.  
  114. -- Just to make sure they're relatively high before the control program starts
  115. if  reactor then
  116.     sleep(1)
  117.     reactor.setAllControlRodLevels(baseControlRodLevel)
  118. else
  119.     sleep(5)
  120.     finished = true
  121.     os.reboot()
  122. end
  123.  
  124. -- Draw stuff on the monitor
  125. local function drawInfo()
  126.  
  127.     statusstring = "Reactor Status: "
  128.     tempstring = "Temperatur: "
  129.     tempstring2 = " Celsius "
  130.     powerstring = "Strom: "
  131.     powerstring2 = "RF/t"
  132.     rodstring = "Control Rods: "
  133.     rodstring2 = "%"
  134.        
  135.     monitor.setTextColor(colors.green)
  136.     print("Reactor",5,1)
  137.     monitor.setTextColor(colors.green)
  138.     print(coreNr,20,1)
  139.     monitor.setTextColor(colors.white)
  140.        
  141.     print(statusstring,1,3)
  142.     if reactor.getActive() then
  143.         monitor.setTextColor(colors.green)
  144.         print(" Online",23,3)
  145.     else
  146.         monitor.setTextColor(colors.red)
  147.         print("Offline",23,3)
  148.     end
  149.        
  150.     monitor.setTextColor(colors.white)
  151.     reactortemp = math.floor(reactor.getFuelTemperature())
  152.     print(tempstring,1,4)
  153.     print(reactortemp,16,4)
  154.     print(tempstring2,24,4)
  155.        
  156.     power = math.floor(reactor.getEnergyProducedLastTick())
  157.     print(powerstring,1,5)
  158.     print(power,16,5)
  159.     print(powerstring2,26,5)
  160.  
  161.     rods = reactor.getControlRodLevel(1)
  162.     print(rodstring,1,6)
  163.     print(rods,16,6)
  164.     print(rodstring2,29,6)
  165. end
  166.  
  167. -- Draw stuff on the bottom part of the monitor, power bar etc
  168. function drawBottomPart()
  169.     reactivity = math.floor(reactor.getFuelReactivity())
  170.         paintutils.drawLine(2, 9, 28, 9, colors.gray)
  171.         if reactivity > 400 then
  172.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.red)
  173.         elseif reactivity > 320 then
  174.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.orange)
  175.         elseif reactivity > 240 then
  176.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.yellow)
  177.         elseif reactivity > 160 then
  178.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.green)
  179.         elseif reactivity > 0 then
  180.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.blue)
  181.         end
  182.         term.setBackgroundColor(colors.black)
  183.         print("Fuel Reactivity:",1,8)
  184.         print(reactivity,24,8)
  185.         print("%",29,8)
  186.         term.setBackgroundColor(colors.black)
  187.        
  188.     energystorage = reactor.getEnergyStored()
  189.     storagepercent = math.floor(energystorage/10000000*100)
  190.     paintutils.drawLine(2, 12, 28, 12, colors.gray)
  191.     if storagepercent > 4 then
  192.         paintutils.drawLine(2, 12, math.floor(26*storagepercent/100)+2, 12, colors.yellow)
  193.     elseif storagepercent > 0 then
  194.         paintutils.drawPixel(2,12,colors.yellow)
  195.     end
  196.     term.setBackgroundColor(colors.black)
  197.     print("Internal buffer: ",1,11)
  198.     print(storagepercent,24,11)
  199.     print("%",29,11)
  200.     term.setBackgroundColor(colors.black)
  201. end
  202.  
  203. -- Controls the reactor, keeping it between configured temps and power storage
  204. function control()
  205.     energystorage = reactor.getEnergyStored()
  206.     reactortemp = math.floor(reactor.getFuelTemperature())
  207.     ediff = (maxStoredEnergyPercent - minStoredEnergyPercent)*100000
  208.     ediffper = ediff / 100
  209.     if energystorage > maxStoredEnergyPercent*100000 then
  210.         reactor.setActive(false)
  211.     reactor.setAllControlRodLevels(95)
  212.     elseif energystorage < minStoredEnergyPercent*100000 then
  213.         reactor.setActive(true)
  214.         reactor.setAllControlRodLevels(5)
  215.     elseif energystorage > minStoredEnergyPercent*100000 then
  216.         reactor.setActive(true)
  217.         blarg = energystorage - minStoredEnergyPercent*100000
  218.         blarg2 = math.floor(blarg/(ediffper+1))
  219.         reactor.setAllControlRodLevels(blarg2)
  220.     end
  221. end
  222.  
  223. -- Main program
  224. function main()
  225.     while not finished do
  226.         if reactor.getConnected() then
  227.             drawLines()
  228.             drawInfo()
  229.             drawBottomPart()
  230.             control()
  231.             sleep(loopTime)
  232.         end
  233.     end
  234. end
  235.  
  236. -- Event handler for exiting, editting and debugging
  237. function eventHandler()
  238.     while not finished do
  239.         event, arg1, arg2, arg3 = os.pullEvent()
  240.         if event == "char" and not inManualMode then
  241.             local ch = string.lower(arg1)
  242.             if ch == "q" then
  243.                 finished = true
  244.             elseif ch == "r" then
  245.                 finished = true
  246.                 os.reboot()
  247.             end
  248.         end
  249.     end
  250. end
  251.  
  252. while not finished do
  253.     parallel.waitForAny(eventHandler, main)
  254.     sleep(loopTime)
  255. end
  256.  
  257. -- Returns control to the terminal if the program has an error or whatever
  258. term.clear()
  259. term.setCursorPos(1,1)
  260. restoreNativeTerminal()
  261. term.clear()
  262. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement