Advertisement
Flawedspirit

Reactor Server

Jun 17th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. local clientID
  2. local power
  3. local fuel
  4. local waste
  5. local fuelCap
  6. local burnRate
  7. local temp
  8. local reactivity
  9. local steam
  10. local steamOut
  11. local coolant
  12. local coolantCap
  13. local conRodLevel
  14. local numConRods
  15.  
  16. local conRods = {}
  17. local refreshRate = 3
  18. local args = {...}
  19.  
  20. -- Checks that argument 1 always exists and that
  21. -- both are integers
  22. function parseArgs()
  23.   if #args < 1 or #args > 2 then
  24.     error("Usage: <program> <client ID> [refresh rate]")
  25.   end
  26.  
  27.   if tonumber(#args[1]) == nil then
  28.     error("Client ID must be a number.")
  29.   end
  30.  
  31.   if not args[2] == nil then
  32.     if tonumber(#args[2]) == nil then
  33.       error("Refresh rate must be a number.")
  34.     end
  35.  
  36.     if not #args[2] == nil then
  37.       refreshRate = args[2]
  38.     end
  39.   end
  40.   clientID = tonumber(args[1])
  41. end
  42.  
  43. -- Automatic detection of reactor and modem
  44. function detect()
  45.   for per, side in pairs(rs.getSides()) do
  46.     if peripheral.isPresent(side) then
  47.       if peripheral.getType(side) == "BigReactors-Reactor" then
  48.         reactor = peripheral.wrap(side)
  49.       elseif peripheral.getType(side) == "modem" then
  50.         modem = side
  51.       end
  52.     end
  53.   end
  54.   return reactor, modem
  55. end
  56.  
  57. function round(num, places)
  58.   local multi = 10 ^ (places or 0)
  59.   return math.floor(num * multi + 0.5) / multi
  60. end
  61.  
  62. -- Refreshes the status of each control rod
  63. -- in the control rod table
  64. function updateConRodTable(rod, level)
  65.   conRods[rod] = level
  66. end
  67.  
  68. -- Returns reactor stats that are expected to
  69. -- remain static during operation
  70. function getReactorBaseStatus()
  71.   numConRods = reactor.getNumberOfControlRods()
  72.   fuelCap = reactor.getFuelAmountMax()
  73.   coolantCap = reactor.getCoolantAmountMax()
  74. end
  75.  
  76. -- Returns reactor stats that are constantly
  77. -- changing during operation
  78. function getReactorUpdate()
  79.   if reactor.getConnected() then
  80.     if reactor.getActive() then
  81.       power = 1
  82.     else
  83.       power = 0
  84.     end
  85.   else
  86.     error("No connection to reactor.")
  87.   end
  88.  
  89.   -- For some reason the steam level and
  90.   -- coolant level functions are reversed
  91.   fuel = reactor.getFuelAmount()
  92.   waste = reactor.getWasteAmount()
  93.   burnRate = reactor.getFuelConsumedLastTick()
  94.   temp = reactor.getFuelTemperature()
  95.   steam = reactor.getCoolantAmount()
  96.   steamOut = reactor.getEnergyProducedLastTick()
  97.   coolant = reactor.getHotFluidAmount()
  98.   reactivity = reactor.getFuelReactivity()
  99. end
  100.  
  101. function sendData()
  102.   local data = {
  103.     rPower = power,
  104.     rFuel = fuel,
  105.     rFuelCap = fuelCap,
  106.     rWaste = waste,
  107.     rBurnRate = burnRate,
  108.     rTemp = temp,
  109.     rSteam = steam,
  110.     rSteamOut = steamOut,
  111.     rCoolant = coolant,
  112.     rCoolantCap = coolantCap,
  113.     rReactivity = reactivity
  114.   }
  115.   local bundle = textutils.serialize(data)
  116.   rednet.send(clientID, bundle, "FSRC")
  117. end
  118.  
  119. local reactor, modem = detect()
  120. parseArgs()
  121. rednet.open(modem)
  122. getReactorBaseStatus()
  123.  
  124. while true do
  125.   term.clear()
  126.   term.setCursorPos(1, 1)
  127.   getReactorUpdate()
  128.   sendData()
  129.  
  130.   print("Fuel Level    : " .. fuel .. " mB [Burning " .. round(burnRate, 2) .. " mB/t]")
  131.   print("Waste Level   : " .. waste .. " mB")
  132.   print("Max Capacity  : " .. fuelCap .. " mB")
  133.   print("Temperature   : " .. round(temp, 0) .. " C")
  134.   print("Reactivity    : " .. round(reactivity, 0) .. "%")
  135.   print("Steam Level   : " .. steam .. " mB")
  136.   print("Steam Output  : " .. steamOut .. " mB/t")
  137.   print("Coolant Level : " .. coolant .. " mB")
  138.   sleep(refreshRate)
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement