Advertisement
FREAKexe

reactor

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