Devious_NEo

Reactor Monitor

Apr 10th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. local emax=10000000
  2.  
  3. local ok,msg=pcall(function ()
  4. local r
  5. local m
  6. local redirected=false
  7. local p
  8.  
  9. function findDev (dType)
  10. local d
  11. for _,d in pairs(peripheral.getNames()) do
  12. if (peripheral.getType(d) == dType) then
  13. return peripheral.wrap(d)
  14. end
  15. end
  16. return nil, dType..": not found"
  17. end
  18.  
  19. function setupDevs()
  20. r=assert(findDev("BigReactors-Reactor"))
  21. if (not r.getConnected()) then
  22. return nil, "Computer port not connected to a valid reactor"
  23. end
  24. --if (r.getNumberOfControlRods() <1) then
  25. -- return nil, "Reactor seems invalid"
  26. --end
  27. r.getEnergyPercent = function ()
  28. return math.floor(1000 * r.getEnergyStored() / emax)/10
  29. end
  30. if r.nativeEPLT then
  31. r.getEnergyProducedLastTick = r.nativeEPLT
  32. end
  33. r.nativeEPLT = r.getEnergyProducedLastTick
  34. r.getEnergyProducedLastTick = function ()
  35. return math.floor(r.nativeEPLT()*1000)/1000
  36. end
  37.  
  38. if redirected then
  39. term.restore()
  40. redirected = false
  41. end
  42. m=findDev("monitor")
  43. if m then
  44. m.setTextScale(1)
  45. term.clear()
  46. m.setCursorPos(1,1)
  47. m.setTextColor(colors.red)
  48. m.setBackgroundColor(colors.black)
  49. print("Redirecting to attached monitor")
  50. term.redirect(m)
  51. redirected = true
  52. end
  53.  
  54. term.setCursorBlink(false)
  55. p=findDev("printer")
  56. end
  57.  
  58. function log (msg)
  59. local stamp=ft()
  60. print (stamp..": "..msg)
  61. end
  62.  
  63. function tableWidth(t)
  64. local width=0
  65. for _,v in pairs(t) do
  66. if #v>width then width=#v end
  67. end
  68. return width
  69. end
  70.  
  71. function ljust(s,w)
  72. local pad=w-#s
  73. return s .. string.rep(" ",pad)
  74. end
  75.  
  76. function rjust(s,w)
  77. local pad=w-#s
  78. return string.rep(" ",pad) .. s
  79. end
  80.  
  81. function display()
  82. term.clear()
  83. term.setCursorPos(18,1)
  84. print("Reactor Status")
  85.  
  86. print("")
  87. local funcs={"Active","EnergyStored","EnergyPercent","CasingTemperature","FuelTemperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"}
  88. local units={"","RF","%","C","C","mB","mB","mB","RF/t"}
  89. local values={}
  90. for _,v in pairs(funcs) do
  91. values[#values+1] = tostring(r["get"..v]())
  92. end
  93. local funcW=tableWidth(funcs)
  94. local valW=tableWidth(values)
  95. for i,v in pairs(funcs) do
  96. print(rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i])
  97. end
  98. end
  99.  
  100. log("Starting")
  101. setupDevs()
  102. while true do
  103. local e=r.getEnergyStored()
  104. local p=r.getAllControlRodLevels()
  105. local a=p<100
  106. local elt=r.getEnergyProducedLastTick()
  107. display()
  108. r.getAllControlRodLevels(p)
  109. r.getActive(a)
  110. os.startTimer(0.8333334)
  111. local event,p1,p2,p3,p4,p5 = os.pullEvent()
  112. if event == "key" then
  113. break
  114. elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  115. setupDevs()
  116. elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  117. error("received "..event)
  118. end
  119. end
  120.  
  121. end)
  122. error(msg)
Advertisement
Add Comment
Please, Sign In to add comment