Advertisement
Brodur

Reactor Control 3.0

Oct 20th, 2017
2,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. CREDIT = "\n\nProgrammed by Brodie Giesbrecht"
  2.  
  3. reactor = nil           --The global var for the reactor peripheral
  4. version = "3.0"   --Version
  5. NRG_STORAGE = 10000000  --The energy stored in a standard reactor
  6. fuelLev = nil           --The current fuel level
  7. FUEL_MAX = nil           --The maximum fuel level
  8. percentFuel = nil       --The percentage fuel level
  9. nrg = nil               --The energy stored
  10.  
  11. --@function:: firstRun
  12. --@purpose::  Initialize the reactor and wrap it as well as determine wheter
  13. --            it is activley cooled etc.
  14. --@returns::
  15. --  boolean; whether we found a reactor,
  16. --  boolean; if actively cooled
  17. function firstRun()
  18.   reactor = peripheral.find("BigReactors-Reactor")
  19.   if not(reactor == nil) then --Is there a reactor?, yes
  20.     return not(reactor == nil), not(reactor.isActivelyCooled()) --True, cooling supported(T/F)
  21.   else --No reactor
  22.     return not(reactor == nil), false --False, False
  23.   end
  24. end --firstRun()
  25.  
  26. --@function:: conditions
  27. --@purpose::  Set reactor active true or false depending on
  28. --            energy and fuel levels
  29. --            --Perhaps temperature in the future--
  30. function conditions()
  31.   fuelLev = reactor.getFuelAmount()
  32.   FUEL_MAX = reactor.getFuelAmountMax()
  33.   percentFuel = ((fuelLev / FUEL_MAX) * 100)
  34.   nrg = reactor.getEnergyStored()
  35.   if percentFuel <= 10 and reactor.getActive()==true then
  36.     reactor.setActive(false)
  37.   end
  38.   if nrg >= NRG_STORAGE and reactor.getActive()==true then
  39.     reactor.setActive(false)
  40.   elseif nrg < NRG_STORAGE and percentFuel > 10 then
  41.     reactor.setActive(true)
  42.   end
  43. end --conditions()
  44.  
  45. --@function:: trim
  46. --@purpose::  trim the numbers after the decimal point of a number
  47. --@params::
  48. --  num;      The number to truncate
  49. --  figures;  how many figures we want after the decimalpoint
  50. --@returns::
  51. --  the truncated number
  52. function trim(num, figures)
  53.   return math.floor(num * 10^figures) /10^figures
  54. end --trim()
  55.  
  56. --@function:: toString
  57. --@purpose::  Get a printable version of all of the data we could
  58. --            possibly want to know about the reactor we are running
  59. --@return::
  60. --  string;   A formatted string containing all of the information
  61. function toString()
  62.   result = string.format("Reactor Control Version: %s\n",version)  
  63.   result = result ..              "Reactor Status:\n"
  64.   result = result .. string.format("       Running: %s\n", tostring(reactor.getActive()))
  65.   result = result .. string.format("          Fuel: %3.1f %s\n", trim(percentFuel, 1), "%")
  66.   result = result .. string.format("        Energy: %g / %g RF\n", trim(nrg, 0), trim(NRG_STORAGE, 0))
  67.   result = result ..             "\nAdditonal Info:\n"
  68.   result = result .. string.format("Energy per tick: %.2f RF\n", trim(reactor.getEnergyProducedLastTick(), 2))
  69.   result = result .. string.format("  Fuel per tick: %.2f mB\n", trim(reactor.getFuelConsumedLastTick(), 2))
  70.   result = result .. string.format("    Casing temp: %g C\n", trim(reactor.getCasingTemperature(), 1))
  71.   result = result..CREDIT
  72.   return result
  73. end --toString()
  74.  
  75. --@function:: output
  76. --@purpose::  print the data
  77. function output()
  78.   term.clear()
  79.   term.setCursorPos(1,1)
  80.   print(toString())
  81. end
  82.  
  83. --@function:: toggleevent
  84. --@purpose::  Runs a loop that keeps the reactor from
  85. --            (hopefully) exploding
  86. function toggleevent()
  87.   found,passive = firstRun()
  88.   if found and passive then --Make sure we have a reactor
  89.     conditions() --Determine the reactor's status
  90.     output()
  91.     os.startTimer(0.5)
  92.     while true do --Run forever
  93.       local args = { os.pullEvent() }
  94.         if args[1] == "key" then --If keypress
  95.           if args[2] == 184 then --if R-ALT is pressed, exit
  96.             print("Break break break!")
  97.             break
  98.           end
  99.         elseif args[1] == "timer" then --Wait a bit and reevaluate the reactor status
  100.           conditions()
  101.           output()
  102.           os.startTimer(0.5)
  103.         end
  104.     end
  105.   else --Error "catching"
  106.     if not(found) then print("No reactor found!") end --No reactor
  107.     if not(passive) then print("Reactor unsupported, not passively cooled!") end --Don't know enough to do active
  108.   end
  109. end --toggleevent()
  110. toggleevent()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement