Advertisement
npexception

BigReactors Control

Dec 19th, 2014
1,661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.26 KB | None | 0 0
  1. local version = 0.72
  2. --  +------------------------+  --
  3. --  |-->  INITIALIZATION  <--|  --
  4. --  +------------------------+  --
  5.  
  6. local ARGS = {...}
  7.  
  8. -- UPDATE HANDLING --
  9. if _UD and _UD.su(version, "URiX6dc3", {...}) then return end
  10.  
  11. local startswith = function(text, piece)
  12.   return string.sub(text, 1, string.len(piece)) == piece
  13. end
  14.  
  15.  
  16. local smooth = true
  17.  
  18. local minEnergy = 100000
  19. local maxEnergy = 1000000
  20.  
  21.  
  22. --[[
  23.   This function takes the text to put out as the first argument,
  24.   the color to write the text in as the second argument,
  25.   third argument is the number of characters printed per second
  26.   and the fourth argument can hide the adjustment mode info.
  27.  
  28.   The text parameter may be a single string or a list of strings
  29.     (one for each line to print)
  30. ]]--
  31. local function status(text, color, slowrate, hideAdjustmentMode)
  32.   local lines
  33.   if type(text) == "table" then
  34.     lines = text
  35.   else
  36.     lines = { text }
  37.   end
  38.  
  39.   term.clear()
  40.   term.setCursorPos(1,1)
  41.   if term.isColor() then
  42.     term.setTextColor(colors.yellow)
  43.   end
  44.   print(" Reactor Control "..tostring(version))
  45.   print("-----------------------")
  46.   print()
  47.   if term.isColor() then
  48.     term.setTextColor(colors.white)
  49.   end
  50.   term.write("--> ")
  51.  
  52.   for i=1,#lines do
  53.     if i>1 then
  54.       term.write("    ")
  55.     end
  56.     if term.isColor() then
  57.       if color == nil then
  58.         color = colors.white
  59.       end
  60.       term.setTextColor(color)
  61.     end
  62.     if slowrate and (slowrate>0) then
  63.       textutils.slowPrint(lines[i], slowrate)
  64.     else
  65.       print(lines[i])
  66.     end
  67.   end
  68.  
  69.   if term.isColor() then
  70.     term.setTextColor(colors.white)
  71.   end
  72.  
  73.   if not hideAdjustmentMode then
  74.     print()
  75.     print()
  76.     term.write("Adjustment mode: ")
  77.     if term.isColor() then
  78.       term.setTextColor(colors.yellow)
  79.     end
  80.     if smooth then
  81.       print("SMOOTH")
  82.     else
  83.       print("ON / OFF")
  84.     end
  85.    
  86.     if term.isColor() then
  87.       term.setTextColor(colors.lightGray)
  88.     end
  89.     print("Press 'a' to switch adjustment mode")
  90.     if term.isColor() then
  91.       term.setTextColor(colors.white)
  92.     end
  93.   end
  94. end
  95.  
  96.  
  97. -- READ OUT COMMAND LINE PARAMETERS --
  98. local paramMsgs = {}
  99.  
  100. for _,par in pairs(ARGS) do
  101.   if par == "static" then
  102.     smooth = false
  103.     table.insert(paramMsgs, "Disabled smooth adjustment")
  104.   elseif startswith(par, "min:") then
  105.     minEnergy = tonumber(string.sub(par, string.len("min:")+1))
  106.     table.insert(paramMsgs, "Set minimum energy: "..minEnergy)
  107.   elseif startswith(par, "max:") then
  108.     maxEnergy = tonumber(string.sub(par, string.len("max:")+1))
  109.     table.insert(paramMsgs, "Set maximum energy: "..maxEnergy)
  110.   end
  111. end
  112.  
  113. if #paramMsgs > 0 then
  114.   status(paramMsgs, colors.lightBlue, 20, true)
  115.   sleep(1)
  116. end
  117.  
  118. local reactor = peripheral.find("BigReactors-Reactor")
  119.  
  120. if not reactor then
  121.   print("No reactor found!")
  122.   return
  123. end
  124.  
  125.  
  126. local function toggleSmooth()
  127.   smooth = not smooth
  128.   if reactor.getConnected() then
  129.     reactor.setActive(true)
  130.     if not smooth then
  131.       reactor.setAllControlRodLevels(0)
  132.     end
  133.   end
  134. end
  135.  
  136.  
  137. status("Activating reactor for startup", colors.lightBlue)
  138.  
  139. reactor.setActive(true)
  140. sleep(2)
  141.  
  142.  
  143. -- MAIN PROGRAM --
  144.  
  145. while true do
  146.   if not reactor.getConnected() then
  147.     status("No valid reactor connected!", colors.red)
  148.     sleep(5)
  149.   else
  150.     local energy = reactor.getEnergyStored()
  151.     local level = (energy/10000000)*100
  152.    
  153.     if smooth then
  154.       reactor.setAllControlRodLevels(level)
  155.      
  156.       local temperature = reactor.getFuelTemperature()
  157.      
  158.       local color
  159.       if temperature >= 2000 then
  160.         color = colors.red
  161.       elseif temperature >=1000 then
  162.         color = colors.orange
  163.       elseif temperature >=500 then
  164.         color = colors.yellow
  165.       elseif temperature >=200 then
  166.         color = colors.green
  167.       else
  168.         color = colors.lightBlue
  169.       end
  170.      
  171.       status("Control Rod Level: "..tostring(math.floor(level*100)/100).."  Temp.: "..tostring(math.floor(temperature)).."°C", color)
  172.     else
  173.       if energy > maxEnergy then
  174.         reactor.setActive(false)
  175.         status("Deactivating: Excess energy detected", colors.orange)
  176.       elseif energy < minEnergy then
  177.         reactor.setActive(true)
  178.         status("Activating: Energy level below threshold", colors.green)
  179.       else
  180.         local lines = {
  181.           "Status: "..(reactor.getActive() and "ACTIVE" or "INACTIVE"),
  182.           "Energy level: "..tostring(math.floor(level)).."%"
  183.         }
  184.         status(lines, colors.lightBlue)
  185.       end
  186.     end
  187.    
  188.     -- check time or keypress
  189.     local timerID = os.startTimer(5)
  190.     local event, var
  191.     repeat
  192.       event, var = os.pullEventRaw()
  193.       local isMyTimer = event == "timer" and var == timerID
  194.       local isTerminate = event == "terminate"
  195.       local isAPressed = event == "key" and var == keys.a
  196.     until isMyTimer or isTerminate or isAPressed
  197.    
  198.     if event == "terminate" then
  199.       if reactor.getConnected() then
  200.         reactor.setActive(false)
  201.       end
  202.       status("TERMINATE: Shutting down reactor", colors.red)
  203.       sleep(1)
  204.       shell.run("clear")
  205.       return -- end program
  206.     elseif event == "key" and var == keys.a then
  207.       toggleSmooth()
  208.     end
  209.   end
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement