Advertisement
FakoTheGreat

Harold Reactor

Jun 23rd, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.69 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 = true --Put warnings on two lines
  5.  
  6. -- Settings for dealing with an overheating reactor
  7. local maxTemperature = 2200
  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 = "right"
  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..offlineWarning
  150.   elseif emptyflag==1 then
  151.     warningColor = colors.pink
  152.     currentWarning = problemNotice..noticeSeparator..emptyWarning
  153.   elseif cooldownTimer > 0 then
  154.     currentWarning = failsafeNotice..noticeSeparator
  155.     if overheated then
  156.       warningColor = colors.green
  157.       currentWarning = currentWarning..overheatWarning..' ('..cooldownTimer..')'
  158.     end
  159.   else
  160.     currentWarning = workingNotice..noticeSeparator
  161.   end
  162.  
  163.   if emptyflag==0 and offlineflag==0 and not overheated then
  164.     if reactor.getControlRodLevel(0)>99 then
  165.       warningColor = colors.lightBlue
  166.       currentWarning2 = noDrawWarning
  167.     elseif reactor.getControlRodLevel(0)>75 then
  168.       warningColor = colors.yellow
  169.       currentWarning2 = lightDrawWarning
  170.     elseif reactor.getControlRodLevel(0)>50 then
  171.       warningColor = colors.orange
  172.       currentWarning2 = mediumDrawWarning
  173.     else
  174.       warningColor = colors.red
  175.       currentWarning2 = highDrawWarning
  176.     end
  177.     if not doubleLineWarnings then
  178.       currentWarning = currentWarning..currentWarning2
  179.     end
  180.     if cooldownTimer > 0 then
  181.       warningColor = colors.purple
  182.       currentWarning = currentWarning..' ('..cooldownTimer..')'
  183.     end
  184.   end
  185.  
  186.   monitor.setCursorPos(1,17)
  187.   monitor.setTextColor(warningColor)
  188.   monitor.write(currentWarning)
  189.   if doubleLineWarnings then
  190.     monitor.setCursorPos(1,18)
  191.     monitor.write(currentWarning2)
  192.   end
  193. else
  194.   flashflag=0
  195.   monitor.setCursorPos(1,17)
  196.   monitor.clearLine()
  197.   if doubleLineWarnings then
  198.     monitor.setCursorPos(1,18)
  199.     monitor.clearLine()
  200.   end
  201. end
  202.  
  203. if emptyflag==0 and offlineflag==0 and not overheated then
  204.   if reactor.getFuelTemperature() > maxTemperature and cooldownTimer == 0 then
  205.     cooldownTimer = failsafeCooldown
  206.   elseif reactor.getFuelTemperature() > maxTemperature and handleBadSettings then
  207.     cooldownTimer = cooldownTimer + 1
  208.     if cooldownTimer == failsafeCooldown * 2 then
  209.       overheated = true
  210.     end
  211.   end
  212.   if reactor.getEnergyStored()>= 1 then
  213.     rodLevels = math.floor(reactor.getEnergyStored()/100000)
  214.   else
  215.     rodLevels = 0 --Full Draw
  216.   end
  217.   if cooldownTimer > 0 and rodLevels < (100 - failsafeMaxDraw) then
  218.     rodLevels = (100 - failsafeMaxDraw)
  219.   end
  220.   reactor.setAllControlRodLevels(rodLevels)
  221. end
  222.  
  223. if overheated then
  224.   reactor.setAllControlRodLevels(100)
  225. end
  226.  
  227. if reactor.getFuelAmount()<=100 and offlineflag==0 then
  228.     reactor.setAllControlRodLevels(100)
  229.     reactor.setActive(false)
  230.     emptyflag=1
  231. else
  232.     emptyflag=0
  233. end
  234.  
  235. if rs.getInput('top')==false and emptyflag==0 then
  236.     reactor.setActive(true)
  237.     offlineflag=0
  238. end
  239.  
  240. if rs.getInput('top')==true and emptyflag==0 then
  241.     reactor.setActive(false)
  242.     reactor.setAllControlRodLevels(100)
  243.     offlineflag=1
  244. end
  245.  
  246. if cooldownTimer > 0 and reactor.getFuelTemperature() <= maxTemperature then
  247.   cooldownTimer = cooldownTimer - 1
  248.   if overheated and (cooldownTimer == 0 or reactor.getFuelTemperature() <= safeTemp) then
  249.       overheated = false
  250.       cooldownTimer = 0
  251.   end
  252. end
  253.  
  254. sleep(1)
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement