Advertisement
Guest User

startup

a guest
May 28th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.13 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 = 20 -- 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 = 60 -- 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 = 400 -- 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. -- Connect to the big reactor peripheral
  69. -- If it can't find a reactor, will wait 5 seconds then reboot
  70. -- This is needed for server restarts when the computer starts up before the modem does
  71. local reactor = wrapThis("BigReactors-Reactor", 0)
  72. if reactor == nil then
  73.         sleep(5)
  74.         finished = true
  75.         os.reboot()
  76. end
  77.  
  78. -- Easyprint, saves program space elsewhere
  79. local function print(str, x, y)
  80.         term.setCursorPos(x, y)
  81.         term.write(str)
  82. end
  83.  
  84. -- Move stuff to monitor
  85. if  monitor then
  86.         sleep(1)
  87.         term.clear()
  88.         term.setCursorPos(1,1)
  89.         term.write("Display redirected to Monitor. Type r to reboot, or")
  90.         term.setCursorPos(1,2)
  91.         term.write("q to quit and return control to this terminal.")
  92.         term.redirect(monitor)
  93. end
  94.  
  95. -- Restore functions to computer terminal
  96. function restoreNativeTerminal()
  97.         repeat
  98.                 term.restore()
  99.                 local w, h = term.getSize()
  100.         until w == 51 and h == 19
  101. end
  102.  
  103. -- Draw some decorative lines
  104. function drawLines()
  105.         term.setBackgroundColor(colors.black)
  106.         paintutils.drawLine(1, 2, 29, 2, colors.lightBlue)
  107.         paintutils.drawLine(1, 7, 29, 7, colors.lightBlue)
  108.         term.setBackgroundColor(colors.black)
  109. end
  110.  
  111. -- Just to make sure they're relatively high before the control program starts
  112. if  reactor then
  113.         sleep(1)
  114.         reactor.setAllControlRodLevels(baseControlRodLevel)
  115. else
  116.         sleep(5)
  117.         finished = true
  118.         os.reboot()
  119. end
  120.  
  121. -- Draw stuff on the monitor
  122. local function drawInfo()
  123.  
  124.         statusstring = "Reactor status: "
  125.         tempstring = "Temperature: "
  126.         tempstring2 = "deg. C"
  127.         powerstring = "Power Output: "
  128.         powerstring2 = "RF/t"
  129.         rodstring = "Control Rods: "
  130.         rodstring2 = "%"
  131.                
  132.         monitor.setTextColor(colors.green)
  133.         print(progName,5,1)
  134.         monitor.setTextColor(colors.red)
  135.         print("v",19,1)
  136.         monitor.setTextColor(colors.green)
  137.         print(progVer,20,1)
  138.         monitor.setTextColor(colors.white)
  139.                
  140.         print(statusstring,1,3)
  141.         if reactor.getActive() then
  142.                 monitor.setTextColor(colors.green)
  143.                 print(" online",23,3)
  144.         else
  145.                 monitor.setTextColor(colors.red)
  146.                 print("offline",23,3)
  147.         end
  148.                
  149.         monitor.setTextColor(colors.white)
  150.         reactortemp = math.floor(reactor.getFuelTemperature())
  151.         print(tempstring,1,4)
  152.         print(reactortemp,16,4)
  153.         print(tempstring2,24,4)
  154.                
  155.         power = math.floor(reactor.getEnergyProducedLastTick())
  156.         print(powerstring,1,5)
  157.         print(power,16,5)
  158.         print(powerstring2,26,5)
  159.  
  160.         rods = reactor.getControlRodLevel(1)
  161.         print(rodstring,1,6)
  162.         print(rods,16,6)
  163.         print(rodstring2,29,6)
  164. end
  165.  
  166. -- Draw stuff on the bottom part of the monitor, power bar etc
  167. function drawBottomPart()
  168.         reactivity = math.floor(reactor.getFuelReactivity())
  169.         paintutils.drawLine(2, 9, 28, 9, colors.gray)
  170.         if reactivity > 400 then
  171.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.red)
  172.         elseif reactivity > 320 then
  173.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.orange)
  174.         elseif reactivity > 240 then
  175.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.yellow)
  176.         elseif reactivity > 160 then
  177.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.green)
  178.         elseif reactivity > 0 then
  179.                 paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.blue)
  180.         end
  181.         term.setBackgroundColor(colors.black)
  182.         print("Fuel Reactivity:",1,8)
  183.         print(reactivity,24,8)
  184.         print("%",29,8)
  185.         term.setBackgroundColor(colors.black)
  186.                
  187.         energystorage = reactor.getEnergyStored()
  188.         storagepercent = math.floor(energystorage/10000000*100)
  189.         paintutils.drawLine(2, 12, 28, 12, colors.gray)
  190.         if storagepercent > 4 then
  191.                 paintutils.drawLine(2, 12, math.floor(26*storagepercent/100)+2, 12, colors.yellow)
  192.         elseif storagepercent > 0 then
  193.                 paintutils.drawPixel(2,12,colors.yellow)
  194.         end
  195.         term.setBackgroundColor(colors.black)
  196.         print("Internal buffer: ",1,11)
  197.         print(storagepercent,24,11)
  198.         print("%",29,11)
  199.         term.setBackgroundColor(colors.black)
  200. end
  201.  
  202. -- Controls the reactor, keeping it between configured temps and power storage
  203. function control()
  204.         energystorage = reactor.getEnergyStored()
  205.         reactortemp = math.floor(reactor.getFuelTemperature())
  206.         ediff = (maxStoredEnergyPercent - minStoredEnergyPercent)*100000
  207.         ediffper = ediff / 100
  208.         if energystorage > maxStoredEnergyPercent*100000 then
  209.                 reactor.setActive(false)
  210.         reactor.setAllControlRodLevels(0)
  211.         elseif energystorage < minStoredEnergyPercent*100000 then
  212.                 reactor.setActive(true)
  213.                 reactor.setAllControlRodLevels(0)
  214.         elseif energystorage > minStoredEnergyPercent*100000 then
  215.                 reactor.setActive(true)
  216.                 blarg = energystorage - minStoredEnergyPercent*100000
  217.                 blarg2 = math.floor(blarg/(ediffper+1))
  218.                 reactor.setAllControlRodLevels(blarg2)
  219.         end
  220. end
  221.  
  222. -- Main program
  223. function main()
  224.         while not finished do
  225.                 if reactor.getConnected() then
  226.                         drawLines()
  227.                         drawInfo()
  228.                         drawBottomPart()
  229.                         control()
  230.                         sleep(loopTime)
  231.                 end
  232.         end
  233. end
  234.  
  235. -- Event handler for exiting, editting and debugging
  236. function eventHandler()
  237.         while not finished do
  238.                 event, arg1, arg2, arg3 = os.pullEvent()
  239.                 if event == "char" and not inManualMode then
  240.                         local ch = string.lower(arg1)
  241.                         if ch == "q" then
  242.                                 finished = true
  243.                         elseif ch == "r" then
  244.                                 finished = true
  245.                                 os.reboot()
  246.                         end
  247.                 end
  248.         end
  249. end
  250.  
  251. while not finished do
  252.         parallel.waitForAny(eventHandler, main)
  253.         sleep(loopTime)
  254. end
  255.  
  256. -- Returns control to the terminal if the program has an error or whatever
  257. term.clear()
  258. term.setCursorPos(1,1)
  259. restoreNativeTerminal()
  260. term.clear()
  261. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement