FakoTheGreat

Streamlined Reactor Display Program

May 30th, 2014
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.71 KB | None | 0 0
  1. -- Cosmetic settings for the monitor
  2. local textScale = 1 --Size of text
  3. local fuelDecimals = 3 --Decimals shown on fuel usage
  4. local doubleLineWarnings = false --Put warnings on two lines
  5.  
  6. -- Settings for dealing with an overheating reactor
  7. local maxTemperature = 3000
  8. local tempWarningRatio = 0.75 --Ratio to change color for temp
  9. local failsafeMaxDraw = 20 --Max Percentage Draw
  10. local failsafeCooldown = 150 --Timer in Seconds
  11. local safeTemp = 1 --Temperature to resume normal operation at
  12.  
  13. -- Increases cooldown timer if reactor stays above max temp, and
  14. -- goes to 0 draw if timer reaches double failsafeCooldown
  15. local handleBadSettings = true
  16.  
  17. -- Use sides or modem names to connect the devices
  18. local monitorName = "bottom"
  19. local reactorName = "back"
  20.  
  21. -- How long to wait before trying to connect if parts aren't ready
  22. -- Recommended values are 1 if next to computer, 5 if using modem.
  23. local monitorWait = 1
  24. local reactorWait = 1
  25.  
  26. -- The first half of the warning messages
  27. local problemNotice = "OFFLINE"
  28. local workingNotice = "ONLINE"
  29. local failsafeNotice = "OVERHEATED"
  30.  
  31. -- The string of characters (if any) that separates the two halves
  32. -- of the warning messages
  33. local noticeSeparator = " - "
  34.  
  35. -- The second half of the warning messages
  36. local offlineWarning = "Manual Mode"
  37. local emptyWarning = "Out of Fuel"
  38. local noDrawWarning = "Standby Mode"
  39. local lightDrawWarning = "Low Power Mode"
  40. local mediumDrawWarning = "Moderate Power Mode"
  41. local highDrawWarning = "High Power Mode"
  42. local overheatWarning = "Safety Shutdown Cycle"
  43.  
  44. -- Used Internally - changing will do nothing
  45. local rodLevels = 0
  46. local currentWarning = ""
  47. local cooldownTimer = 0
  48. local warningColor = colors.white
  49. local emptyflag = 0
  50. local offlineflag = 0
  51. local flashflag = 0
  52. local overheated = false
  53. local monitor = nil
  54. local reactor = nil
  55.  
  56. while reactor == nil do
  57.   reactor=peripheral.wrap(reactorName)
  58.   if reactor == nil then
  59.     print("Waiting for reactor peripheral")
  60.     os.sleep(reactorWait)
  61.   end
  62. end
  63.  
  64. print("Found reactor. Looking for monitor.")
  65.  
  66. while monitor == nil do
  67.   monitor = peripheral.wrap(monitorName)
  68.   if monitor == nil then
  69.     print("Waiting for monitor to connect")
  70.     os.sleep(monitorWait)
  71.   end
  72. end
  73.  
  74. print("Monitor found.")
  75.  
  76. while not reactor.getConnected() do
  77.   print("Waiting for reactor to connect.")
  78.   os.sleep(reactorWait)
  79. end
  80.  
  81. print("Program Initialized - View Monitor.")
  82.  
  83. monitor.setTextScale(textScale)
  84. monitor.setBackgroundColor(colors.black)
  85.  
  86. function round(num, idp)
  87.   local mult = 10^(idp or 0)
  88.   return math.floor(num * mult + 0.5) / mult
  89. end
  90.  
  91. while true do
  92. monitor.clear()
  93. monitor.setCursorPos(1,1)
  94. monitor.setTextColor(colors.white)
  95. monitor.write('Fuel Level:')
  96. monitor.setCursorPos(1,2)
  97. monitor.setTextColor(colors.yellow)
  98. monitor.write(math.floor(((reactor.getFuelAmount()/reactor.getFuelAmountMax())*100)+0.5)..'% Fuel')
  99. monitor.setCursorPos(1,3)
  100. monitor.setTextColor(colors.lightBlue)
  101. monitor.write(math.floor(((reactor.getWasteAmount()/reactor.getFuelAmountMax())*100)+0.5)..'% Waste')
  102. monitor.setCursorPos(1,5)
  103. monitor.setTextColor(colors.white)
  104. monitor.write('Monitoring:')
  105. monitor.setTextColor(colors.green)
  106. monitor.setCursorPos(1,6)
  107. monitor.write('Rods:  '..(100-(reactor.getControlRodLevel(0)))..'% Power')
  108. monitor.setCursorPos(1,7)
  109. monitor.write('Fuel:  '..round(reactor.getFuelConsumedLastTick(),fuelDecimals)..' mB/t')
  110. monitor.setCursorPos(1,9)
  111. monitor.setTextColor(colors.white)
  112. monitor.write('Temperature:')
  113. monitor.setCursorPos(1,10)
  114. if reactor.getFuelTemperature()>=(maxTemperature*tempWarningRatio) then
  115.     monitor.setTextColor(colors.purple)
  116. elseif reactor.getFuelTemperature()>=maxTemperature then
  117.     monitor.setTextColor(colors.red)
  118. else
  119.     monitor.setTextColor(colors.green)
  120. end
  121. monitor.write(math.floor(reactor.getFuelTemperature())..'C')
  122. monitor.setCursorPos(1,12)
  123. monitor.setTextColor(colors.white)
  124. monitor.write('Flux:')
  125. monitor.setCursorPos(1,13)
  126. monitor.setTextColor(colors.green)
  127. monitor.write(reactor.getEnergyStored()..' RF Stored      ')
  128.  
  129. if reactor.getEnergyProducedLastTick()>=500 and reactor.getEnergyProducedLastTick()<2000 then
  130.     monitor.setTextColor(colors.orange)
  131. end
  132.  
  133. if reactor.getEnergyProducedLastTick()>=2000 then
  134.     monitor.setTextColor(colors.red)
  135. end
  136.  
  137. monitor.setCursorPos(1,14)
  138. monitor.write((math.floor(reactor.getEnergyProducedLastTick()+0.5))..' RF/t')
  139. monitor.setCursorPos(1,16)
  140. monitor.setTextColor(colors.white)
  141. monitor.write('Warnings:')
  142.  
  143. if flashflag==0 then
  144.   flashflag=1
  145.   currentWarning = ""
  146.   currentWarning2 = ""
  147.   if offlineflag==1 then
  148.     warningColor = colors.lightGray
  149.     currentWarning = problemNotice..noticeSeparator
  150.     currentWarning2 = offlineWarning
  151.   elseif emptyflag==1 then
  152.     warningColor = colors.pink
  153.     currentWarning = problemNotice..noticeSeparator
  154.     currentWarning2 = emptyWarning
  155.   elseif cooldownTimer > 0 then
  156.     currentWarning = failsafeNotice..noticeSeparator
  157.     if overheated then
  158.       warningColor = colors.green
  159.       currentWarning2 = overheatWarning..' ('..cooldownTimer..')'
  160.     end
  161.   else
  162.     currentWarning = workingNotice..noticeSeparator
  163.   end
  164.  
  165.   if emptyflag==0 and offlineflag==0 and not overheated then
  166.     if reactor.getControlRodLevel(0)>99 then
  167.       warningColor = colors.lightBlue
  168.       currentWarning2 = noDrawWarning
  169.     elseif reactor.getControlRodLevel(0)>75 then
  170.       warningColor = colors.yellow
  171.       currentWarning2 = lightDrawWarning
  172.     elseif reactor.getControlRodLevel(0)>50 then
  173.       warningColor = colors.orange
  174.       currentWarning2 = mediumDrawWarning
  175.     else
  176.       warningColor = colors.red
  177.       currentWarning2 = highDrawWarning
  178.     end
  179.     if not doubleLineWarnings then
  180.       currentWarning = currentWarning..currentWarning2
  181.     end
  182.     if cooldownTimer > 0 then
  183.       warningColor = colors.purple
  184.       currentWarning = currentWarning..' ('..cooldownTimer..')'
  185.     end
  186.   end
  187.  
  188.   monitor.setCursorPos(1,17)
  189.   monitor.setTextColor(warningColor)
  190.   monitor.write(currentWarning)
  191.   if doubleLineWarnings then
  192.     monitor.setCursorPos(1,18)
  193.     monitor.write(currentWarning2)
  194.   end
  195. else
  196.   flashflag=0
  197.   monitor.setCursorPos(1,17)
  198.   monitor.clearLine()
  199.   if doubleLineWarnings then
  200.     monitor.setCursorPos(1,18)
  201.     monitor.clearLine()
  202.   end
  203. end
  204.  
  205. if emptyflag==0 and offlineflag==0 and not overheated then
  206.   if reactor.getFuelTemperature() > maxTemperature and cooldownTimer == 0 then
  207.     cooldownTimer = failsafeCooldown
  208.   elseif reactor.getFuelTemperature() > maxTemperature and handleBadSettings then
  209.     cooldownTimer = cooldownTimer + 1
  210.     if cooldownTimer == failsafeCooldown * 2 then
  211.       overheated = true
  212.     end
  213.   end
  214.   if reactor.getEnergyStored()>= 1 then
  215.     rodLevels = math.floor(reactor.getEnergyStored()/100000)
  216.   else
  217.     rodLevels = 0 --Full Draw
  218.   end
  219.   if cooldownTimer > 0 and rodLevels < (100 - failsafeMaxDraw) then
  220.     rodLevels = (100 - failsafeMaxDraw)
  221.   end
  222.   reactor.setAllControlRodLevels(rodLevels)
  223. end
  224.  
  225. if overheated then
  226.   reactor.setAllControlRodLevels(100)
  227. end
  228.  
  229. if reactor.getFuelAmount()<=100 and offlineflag==0 then
  230.     reactor.setAllControlRodLevels(100)
  231.     reactor.setActive(false)
  232.     emptyflag=1
  233. else
  234.     emptyflag=0
  235. end
  236.  
  237. if rs.getInput('top')==false and emptyflag==0 then
  238.     reactor.setActive(true)
  239.     offlineflag=0
  240. end
  241.  
  242. if rs.getInput('top')==true and emptyflag==0 then
  243.     reactor.setActive(false)
  244.     reactor.setAllControlRodLevels(100)
  245.     offlineflag=1
  246. end
  247.  
  248. if cooldownTimer > 0 and reactor.getFuelTemperature() <= maxTemperature then
  249.   cooldownTimer = cooldownTimer - 1
  250.   if overheated and (cooldownTimer == 0 or reactor.getFuelTemperature() <= safeTemp) then
  251.       overheated = false
  252.       cooldownTimer = 0
  253.   end
  254. end
  255.  
  256. sleep(1)
  257. end
Add Comment
Please, Sign In to add comment