Advertisement
zeplintwo

Button for Reactor

Feb 10th, 2015
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. --Big Reactors Control Program
  2. --Brydon Lezchuk <90littlegreenmen@gmail.com>
  3.  
  4. --Some code inspiration came from similar
  5. --programs by:
  6. --Emily Backes <lucca@accela.net>
  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(config)
  83.   print("How many capacitors are connected to network?")
  84.   config.numcap = read()
  85.   print("At what power capacity should Reactor/Turbine shut off?")
  86.   config.powermax = read()
  87.   print("How low should the capacity be to turn on Reactor/Turbine?")
  88.   config.powermin = read()
  89.  
  90.   saveconfig(config)
  91. end
  92.  
  93.  
  94. --Gets devices from the network given the type
  95. --and returns them in a table
  96. function getdev(type)
  97.   local dev
  98.   local key
  99.   local d = {} --table of the specified device type
  100.   local wrapped = {}
  101.  
  102.   for key,dev in pairs(peripheral.getNames()) do
  103.     if (peripheral.getType(dev)==type) then
  104.       table.insert(d, dev)
  105.       print("Found Device: "..dev)
  106.     end
  107.   end
  108.   return wrapped
  109. end
  110.  
  111.  
  112. --calculates the optimal fuel rod insertion rate
  113. --to generate the steam needed by turbines
  114. --given a reactor and the steam needed
  115. function getoptfuelrod(r,s)
  116.   resetdisplay()
  117.   --print(r)
  118.   --r.setActive(false)
  119.   --while r.getFuelTemperature() > 99 do
  120.   --  print("Waiting for reactor to cool off")
  121.   --  print("Reactor Temp: "..r.getFuelTemperature())
  122.   --  sleep(1)
  123.   --end
  124. end
  125.  
  126.  
  127. --Clears the screen and moves cursor
  128. function resetdisplay()
  129.   term.clear()
  130.   term.setCursorPos(1,1)
  131.  
  132. end
  133.  
  134.  
  135. --The backbone of the program
  136. function main()
  137.   local config = {} --table of configs
  138.   local r --table of reactors
  139.   local t --table of turbines
  140.   local m --table of monitors
  141.   local c --table of capacitors
  142.  
  143.   local reactor
  144.   local turbine
  145.   local numpassreactor = 0
  146.   local steamneeded = 0
  147.   local steamperreactor = 0
  148.   local optfuelrod = {}
  149.  
  150.   errorprint("Please note, if anything has changed on the network, delete the config.txt")
  151.   print()
  152.  
  153.   if not chkconfig() then
  154.     errorprint("No configuration set, exiting program.")
  155.     return
  156.   end
  157.  
  158.   r = getdev("BigReactors-Reactor")
  159.   t = getdev("BigReactors-Turbine")
  160.   c = getdev("tile_blockcapacitorbank_name")
  161.   m = getdev("monitor")
  162.    
  163.   if t~=nil then
  164.     steamneeded = 2000 * (#t)
  165.     getoptfuelrod(r[1],steamneeded)
  166.   end
  167.  
  168.   while true do
  169.     for i,reactor in pairs(r) do
  170.       if reactor.isActivelyCooled() then
  171.         --actively cooled reactor
  172.        
  173.        
  174.        
  175.       else
  176.         --code here for passively cooled reactor
  177.       end
  178.     end
  179.   end
  180. end
  181.  
  182.  
  183. --local args = {...}
  184. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement