Guest User

reactorcontrol

a guest
Jan 26th, 2015
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.42 KB | None | 0 0
  1. --Big Reactors Control Program
  2. --Brydon Lezchuk <[email protected]>
  3.  
  4. --Some code inspiration came from similar
  5. --programs by:
  6. --Emily Backes <[email protected]>
  7. --pastebin.com/uALkzjWt
  8. --and Direwolf20
  9. --pastebin.com/4qNyaPav
  10. --pastebin.com/XBbMUYNn
  11.  
  12. --This version only works with 1 reactor and
  13. --1 turbine, though it is written to be
  14. --expanded upon.
  15.  
  16. --Must be ran from the top level directory /
  17. --This is where config.txt gets saved
  18.  
  19.  
  20. --Outputs text in red
  21. function errorprint(txt)
  22.   term.setTextColor(colors.red)
  23.   print(txt)
  24.   term.setTextColor(colors.white)
  25. end
  26.  
  27.  
  28. --Outputs the given text then returns
  29. --true or false given the user input
  30. function yesno(txt)
  31.   print(txt.." (y/n)")
  32.   local input = read()
  33.   if input=="y" then
  34.     return true
  35.   elseif input=="n" then
  36.     return false
  37.   else
  38.     errorprint("Please use 'y' or 'n'")
  39.     return yesno(txt)
  40.   end
  41. end
  42.  
  43.  
  44. --Checks for the config.txt file
  45. --Returns true if file exists
  46. --Returns true if setup ran
  47. --Returns false if setup was not ran
  48. function chkconfig()
  49.   if not fs.exists("config.txt") then
  50.     if yesno("Configuration not found, would you like to run setup?")
  51.     then
  52.       setup()
  53.       return true
  54.     else
  55.       return false
  56.     end
  57.   else
  58.     return true
  59.   end
  60. end
  61.  
  62.  
  63. --loads in config settings from config.txt
  64. function loadconfig()
  65.   local file = fs.open("config.txt","r")
  66.   local data = file.readAll()
  67.   file.close()
  68.   return textutils.unserialize(data)
  69. end
  70.  
  71.  
  72. --saves configs to config.txt
  73. function saveconfig(config)
  74.   local file = fs.open("config.txt","w")
  75.   file.write(textutils.serialize(config))
  76.   file.close()
  77. end
  78.  
  79.  
  80. --Gets config settings from user and saves
  81. --them to config.txt
  82. function setup()
  83.   local config = {}
  84.  
  85.   print("How many capacitors are connected to network?")
  86.   config.numcap = read()
  87.   print("At what power capacity should Reactor/Turbine shut off?")
  88.   config.powermax = read()
  89.   print("How low should the capacity be to turn on Reactor/Turbine?")
  90.   config.powermin = read()
  91.  
  92.   saveconfig(config)
  93. end
  94.  
  95.  
  96. --Gets devices from the network given the type
  97. --and returns them in a table
  98. function getdev(type)
  99.   local dev
  100.   local key
  101.   local d = {} --table of the specified device type
  102.   local wrapped = {}
  103.  
  104.   for key,dev in pairs(peripheral.getNames()) do
  105.     if (peripheral.getType(dev)==type) then
  106.       table.insert(d, dev)
  107.       print("Found Device: "..dev)
  108.     end
  109.   end
  110.  
  111.   for i = 1,#d do
  112.     table.insert(wrapped, peripheral.wrap(d[i]))
  113.     print("Wrapping device "..d[i])
  114.   end
  115.   return wrapped
  116. end
  117.  
  118.  
  119. function setRods(reactor, level)
  120.   reactor.setAllControlRodLevels(level)
  121. end
  122.  
  123.  
  124. --calculates the optimal fuel rod insertion rate
  125. --to generate the steam needed by turbines
  126. --given a reactor and the steam needed
  127. function getoptfuelrod(r,s)
  128.   local steam99
  129.   local estlevel
  130.   local eststeam
  131.   local steamup = 0
  132.   local steamdown = s * 2
  133.  
  134.   resetdisplay()
  135.   r.setActive(false)
  136.   while r.getFuelTemperature() > 99 do
  137.     resetdisplay()
  138.     print("Reactor Offline")
  139.     print("Waiting for reactor to cool off")
  140.     print("Reactor Temp: "..r.getFuelTemperature())
  141.     sleep(1)
  142.   end
  143.  
  144.   setRods(r,99)
  145.   r.setActive(true)
  146.   while r.getHotFluidAmount() > 10000 do
  147.     resetdisplay()
  148.     print("Reactor Online")
  149.     print("Heating reactor up")
  150.     print("Reactor Temp: "..r.getFuelTemperature())
  151.     sleep(1)
  152.   end
  153.  
  154.   for i = 1,4 do
  155.     resetdisplay()
  156.     print("Reactor Online")
  157.     print("Fuel rods set to 99% for "..i.." seconds")
  158.     steam99 = r.getHotFluidProducedLastTick()
  159.     print("Steam produced: "..steam99)
  160.     sleep(1)
  161.   end
  162.  
  163.   estlevel = 100 - math.ceil(s / steam99)
  164.   print("Estimated optimal fuel rod level is "..estlevel)
  165.   --setRods(r,estlevel)
  166.   sleep(3)
  167.  
  168.   local test=true
  169.   while test do
  170.     for i = 1,4 do
  171.       resetdisplay()
  172.       setRods(r,estlevel)
  173.       print("Reactor Online")
  174.       print("Fuel rods set to "..estlevel.."% for "..i.." seconds")
  175.       eststeam = r.getHotFluidProducedLastTick()
  176.       print("Steam produced: "..eststeam)
  177.       sleep(1)
  178.     end
  179.    
  180.     if eststeam > steamdown and eststeam > s and eststeam < steamup then
  181.       test=false
  182.     elseif eststeam > steamup and eststeam > s then
  183.       steamup = eststeam
  184.       estlevel = estlevel + 1
  185.     elseif eststeam < steamdown  then
  186.       steamdown = eststeam
  187.       estlevel = estlevel - 1
  188.     end
  189.   end
  190.  
  191.   print("Optimal Fuel rod insertion is "..estlevel.."%")
  192.   sleep(3)
  193.   return estlevel
  194. end
  195.  
  196.  
  197. --Clears the screen and moves cursor
  198. function resetdisplay()
  199.   term.clear()
  200.   term.setCursorPos(1,1)
  201.  
  202. end
  203.  
  204.  
  205. --The backbone of the program
  206. function main()
  207.   local config = {} --table of configs
  208.   local r --table of reactors
  209.   local t --table of turbines
  210.   local m --table of monitors
  211.   local c --table of capacitors
  212.  
  213.   local reactor
  214.   local turbine
  215.   local numpassreactor = 0
  216.   local steamneeded = 0
  217.   local steamperreactor = 0
  218.   local optfuelrod = {}
  219.  
  220.   local energystored
  221.   local energymax
  222.   local energypercent
  223.  
  224.   errorprint("Please note, if anything has changed on the network, delete the config.txt")
  225.   print()
  226.  
  227.   if not chkconfig() then
  228.     errorprint("No configuration set, exiting program.")
  229.     return
  230.   end
  231.  
  232.   r = getdev("BigReactors-Reactor")
  233.   t = getdev("BigReactors-Turbine")
  234.   c = getdev("tile_blockcapacitorbank_name")
  235.   m = getdev("monitor")
  236.    
  237.   sleep(3)
  238.  
  239.   if t~=nil then
  240.     steamneeded = 2000
  241.     config.optfuelrod = getoptfuelrod(r[1],steamneeded)
  242.   end
  243.  
  244.   local capacitor = c[1]
  245.  
  246.   print("test")
  247.   print(capacitor.getMaxEnergyStored())
  248.   sleep(2)
  249.   energymax = capacitor.getMaxEnergyStored()
  250.   print("System storage capacity is "..energymax)
  251.  
  252.   while true do
  253.     for i,reactor in pairs(r) do
  254.       if reactor.isActivelyCooled() then
  255.         --actively cooled reactor
  256.         resetdisplay()
  257.         energystored = capacitor.getEnergyStored()
  258.         energypercent = math.floor((energystored/energymax)*100)
  259.        
  260.         reactor.setActive(true)
  261.         t[1].setActive(true)
  262.        
  263.         if energypercent < config.powermin then
  264.           t[1].setInductorEngaged(true)
  265.         elseif energypercent > config.powermax then
  266.           t[1].setInductorEngaged(false)
  267.         end
  268.        
  269.        
  270.         --display info
  271.         if reactor.getActive() then
  272.           print("Reactor Online")
  273.         else
  274.           print("Reactor Offline")
  275.         end
  276.         print("Heat: "..reactor.getFuelTemperature())
  277.         print("Steam Produced: "..reactor.getHotFluidProducedLastTick())
  278.        
  279.        
  280.         print()
  281.         if t[1].getActive() then
  282.           print("Turbine Online")
  283.         else
  284.           print("Turbine Offline")
  285.         end
  286.         if t[1].getInductorEngaged() then
  287.           print("Inductors Engaged")
  288.         else
  289.           print("Inductors Disengaged")
  290.         end
  291.         print("Rotor Speed: "..t[1].getRotorSpeed())
  292.         print("Energy Output "..t[1].getEnergyProducedLastTick())
  293.        
  294.         print("Capacitor max storaged: "..energymax)
  295.         print("Currenty energy: "..energystored)
  296.         print("("..energypercent.."% full)")
  297.        
  298.         sleep(1)
  299.  
  300.         return --exit here for testing
  301.       else
  302.         --code here for passively cooled reactor
  303.         errorprint("Sorry, code for passively cooled reactors is not yet implemented")
  304.         return
  305.       end
  306.     end
  307.   end
  308. end
  309.  
  310.  
  311. --local args = {...}
  312. main()
Advertisement
Add Comment
Please, Sign In to add comment