IronClaymore

simplenuke_107

May 19th, 2014
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.06 KB | None | 0 0
  1. -- 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.
  2. local progVer = "1.07"
  3. local progName = "IC_SimpleNuke"
  4. local extrastuff = "Because manual control is annoying!"
  5. 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
  6. 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
  7. local minReactorTemp = 200 -- Minimum reactor temperature (^C) to maintain
  8. local maxReactorTemp = 300 -- Maximum reactor temperature (^C) to maintain
  9.  
  10. ----------
  11.  
  12. -- Basic side determination for wrapping
  13. function getDeviceSide(deviceType)
  14.     deviceType = deviceType:lower()
  15.    
  16.     for i, side in pairs(rs.getSides()) do
  17.         if (peripheral.isPresent(side)) then
  18.             if (string.lower(peripheral.getType(side)) == deviceType) then
  19.                 return side;
  20.             end
  21.         end
  22.     end
  23.    
  24.     return nil;
  25. end
  26.  
  27. -- Peripheral wrapping function
  28. function wrapThis(thing, f)
  29.     local wrapped = nil
  30.     while wrapped == nil and f <= 100 do
  31.         wrapped = peripheral.wrap(thing.."_"..f)
  32.         f = f + 1
  33.     end
  34.  
  35.     if wrapped == nil then
  36.         side = getDeviceSide(thing)
  37.         if side ~= nil then
  38.             return peripheral.wrap(side)
  39.         else
  40.             return nil
  41.         end
  42.     else
  43.         return wrapped
  44.     end
  45. end
  46.  
  47.  
  48. print("Initializing program...",1,1);
  49.  
  50. -- Initialize the monitor
  51. -- If it can't find a monitor, will wait 5 seconds then reboot
  52. -- This is needed for server restarts when the computer starts up before the modem does
  53. local monitor = wrapThis("monitor", 0)
  54. if monitor == nil then
  55.     print("Can't find monitor.")
  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. --  error("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.     print("Can't find reactor.")
  74.     sleep(5)
  75.     finished = true
  76.     os.reboot()
  77. end
  78.  
  79. -- Easyprint, saves program space elsewhere
  80. local function print(str, x, y)
  81.     term.setCursorPos(x, y)
  82.     term.write(str)
  83. end
  84.  
  85. -- Move stuff to monitor
  86. if  monitor then
  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.     term.clear()
  94. end
  95.  
  96. -- Restore functions to computer terminal
  97. function restoreNativeTerminal()
  98.     repeat
  99.         term.restore()
  100.         local w, h = term.getSize()
  101.     until w == 51 and h == 19
  102. end
  103.  
  104. -- Draw some decorative lines
  105. function drawLines()
  106.     term.setBackgroundColor(colors.black)
  107.     paintutils.drawLine(1, 3, 39, 3, colors.lightBlue)
  108.     paintutils.drawLine(1, 9, 39, 9, colors.lightBlue)
  109.     term.setBackgroundColor(colors.black)
  110. end
  111.  
  112. -- Just to make sure they're relatively high before the control program starts
  113. if  reactor then
  114.     reactor.setAllControlRodLevels(baseControlRodLevel)
  115. else
  116.     print("Can't find reactor.")
  117.     sleep(5)
  118.     finished = true
  119.     os.reboot()
  120. end
  121.  
  122. -- Draw stuff on the monitor
  123. local function drawInfo()
  124.  
  125.     statusstring = "Reactor status: "
  126.     tempstring = "Temperature: "
  127.     tempstring2 = "deg. C"
  128.     steamstring = "Steam Output: "
  129.     steamstring2 = "mb/t"
  130.     rodstring = "Control Rods: "
  131.     rodstring2 = "%"
  132.     fuelstring = "Fuel Usage: "
  133.     fuelstring2 = "mb/t"
  134.        
  135.     monitor.setTextColor(colors.green)
  136.     print(progName,10,1)
  137.     monitor.setTextColor(colors.red)
  138.     print("v",24,1)
  139.     monitor.setTextColor(colors.green)
  140.     print(progVer,25,1)
  141.     monitor.setTextColor(colors.blue)
  142.     print(extrastuff,4,2)
  143.     monitor.setTextColor(colors.white)
  144.        
  145.     print(statusstring,1,4)
  146.     if reactor.getActive() then
  147.         monitor.setTextColor(colors.green)
  148.         print(" online",33,4)
  149.     else
  150.         monitor.setTextColor(colors.red)
  151.         print("offline",33,4)
  152.     end
  153.        
  154.     monitor.setTextColor(colors.white)
  155.     reactortemp = math.floor(reactor.getFuelTemperature())
  156.     print(tempstring,1,5)
  157.     print(reactortemp,23,5)
  158.     print(tempstring2,34,5)
  159.        
  160.     steam = math.floor(reactor.getEnergyProducedLastTick())
  161.     print(steamstring,1,6)
  162.     print(steam,23,6)
  163.     print(steamstring2,36,6)
  164.  
  165.     rods = reactor.getControlRodLevel(1)
  166.     print(rodstring,1,7)
  167.     print(rods,23,7)
  168.     print(rodstring2,39,7)
  169.    
  170.     fuelbase = reactor.getFuelConsumedLastTick()
  171.     fuel = (math.floor(fuelbase * 10000)) / 10000
  172.     print(fuelstring,1,8)
  173.     print(fuel,23,8)
  174.     print(fuelstring2,36,8)
  175. end
  176.  
  177. -- Draw stuff on the bottom part of the monitor, power bar etc
  178. function drawBottomPart()
  179.     reactivity = math.floor(reactor.getFuelReactivity())
  180.     paintutils.drawLine(2, 11, 38, 11, colors.gray)
  181.     if reactivity > 400 then
  182.         paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.red)
  183.     elseif reactivity > 320 then
  184.         paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.orange)
  185.     elseif reactivity > 240 then
  186.         paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.yellow)
  187.     elseif reactivity > 160 then
  188.         paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.green)
  189.     elseif reactivity > 0 then
  190.         paintutils.drawLine(2, 11, math.floor(36*reactivity/500)+1, 11, colors.blue)
  191.     end
  192.     term.setBackgroundColor(colors.black)
  193.     print("Fuel Reactivity:",1,10)
  194.     print(reactivity,34,10)
  195.     print("%",39,10)
  196.     term.setBackgroundColor(colors.black)
  197.        
  198.     reactortemp = math.floor(reactor.getFuelTemperature())
  199.     paintutils.drawLine(2, 13, 38, 13, colors.gray)
  200.     if reactortemp > 1000 then
  201.         paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.red)
  202.     elseif reactortemp > 800 then
  203.         paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.orange)
  204.     elseif reactortemp > 600 then
  205.         paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.yellow)
  206.     elseif reactortemp > 400 then
  207.         paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.green)
  208.     elseif reactortemp > 200 then
  209.         paintutils.drawLine(2, 13, math.floor(36*reactortemp/2000)+1, 13, colors.blue)
  210.     end
  211.     term.setBackgroundColor(colors.black)
  212.     print("Reactor heat: ",1,12)
  213.     print(reactortemp,24,12)
  214.     print("deg C",29,12)
  215.     term.setBackgroundColor(colors.black)
  216. end
  217.  
  218. -- Controls the reactor, keeping it between configured temperature values
  219. function control()
  220.     reactortemp = math.floor(reactor.getFuelTemperature())
  221.     hdiff = (maxReactorTemp - minReactorTemp)
  222.     hdiffper = hdiff / 100
  223.     if reactortemp < minReactorTemp then
  224.         reactor.setActive(true)
  225.         reactor.setAllControlRodLevels(0)
  226.     elseif reactortemp > maxReactorTemp then
  227.         reactor.setActive(true)
  228.         reactor.setAllControlRodLevels(100)
  229.     elseif reactortemp > minReactorTemp and reactortemp < maxReactorTemp then
  230.         reactor.setActive(true)
  231.         blarg = reactortemp - minReactorTemp
  232.         blarg2 = math.floor(blarg/(hdiffper))
  233.         reactor.setAllControlRodLevels(blarg2)
  234.     end
  235. end
  236.  
  237. -- Main program
  238. function main()
  239.     while not finished do
  240.         if reactor.getConnected() then
  241.             drawLines()
  242.             drawInfo()
  243.             drawBottomPart()
  244.             control()
  245.             sleep(loopTime)
  246.         end
  247.     end
  248. end
  249.  
  250. -- Event handler for exiting, editting and debugging
  251. function eventHandler()
  252.     while not finished do
  253.         event, arg1, arg2, arg3 = os.pullEvent()
  254.         if event == "char" and not inManualMode then
  255.             local ch = string.lower(arg1)
  256.             if ch == "q" then
  257.                 finished = true
  258.             elseif ch == "r" then
  259.                 finished = true
  260.                 os.reboot()
  261.             end
  262.         end
  263.     end
  264. end
  265.  
  266. while not finished do
  267.     parallel.waitForAny(eventHandler, main)
  268.     sleep(loopTime)
  269. end
  270.  
  271. -- Returns control to the terminal if the program has an error or whatever
  272. term.clear()
  273. term.setCursorPos(1,1)
  274. restoreNativeTerminal()
  275. term.clear()
  276. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment