Advertisement
Guest User

ReactorCodeAttemptV2

a guest
Nov 21st, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Basic control for BigReactors-Reactor
  2. -- BSD3 License
  3. -- Emily Backes <lucca@accela.net>
  4.  
  5. -- Uses the first monitor it finds, if any
  6. -- May need 3x3 or larger for that
  7. -- No log output or printer usage yet
  8. -- Will work on adv comps but mouse event handling
  9. -- would need to be added below
  10. -- Suitable for use in /startup
  11.  
  12. -- Max energy in a reactor's internal cell
  13. local emax=10000000
  14.  
  15. -- wrap everything in an exception handler
  16. local ok,msg=pcall(function ()
  17. local r
  18. local m
  19. local redirected=false
  20. local p
  21.  
  22. function findDev (dType)
  23. local d
  24. for _,d in pairs(peripheral.getNames()) do
  25. if (peripheral.getType(d) == dType) then
  26. return peripheral.wrap(d)
  27. end
  28. end
  29. return nil, dType..": not found"
  30. end
  31.  
  32. function setupDevs()
  33. r=assert(findDev("BigReactors-Reactor"))
  34. if (not r.getConnected()) then
  35. return nil, "Computer port not connected to a valid reactor"
  36. end
  37. --if (r.getNumberOfControlRods() <1) then
  38. -- return nil, "Reactor seems invalid"
  39. --end
  40. r.getEnergyPercent = function ()
  41. return math.floor(1000 * r.getEnergyStored() / emax)/10
  42. end
  43. if r.nativeEPLT then
  44. r.getEnergyProducedLastTick = r.nativeEPLT
  45. end
  46. r.nativeEPLT = r.getEnergyProducedLastTick
  47. r.getEnergyProducedLastTick = function ()
  48. return math.floor(r.nativeEPLT()*1000)/1000
  49. end
  50.  
  51. if redirected then
  52. term.restore()
  53. redirected = false
  54. end
  55. m=findDev("monitor")
  56. if m then
  57. m.setTextScale(0.5)
  58. term.clear()
  59. term.setCursorPos(1,1)
  60. print("Redirecting to attached monitor")
  61. term.redirect(m)
  62. redirected = true
  63. end
  64.  
  65. term.setCursorBlink(false)
  66. p=findDev("printer")
  67. end
  68.  
  69. function ft ()
  70. local d=os.day()
  71. local t=os.time()
  72. local h=math.floor(t)
  73. local m=math.floor((t-h)*60)
  74. return string.format("Day %d, %02d:%02d",d,h,m)
  75. end
  76.  
  77. function log (msg)
  78. local stamp=ft()
  79. print (stamp..": "..msg)
  80. end
  81.  
  82. function tableWidth(t)
  83. local width=0
  84. for _,v in pairs(t) do
  85. if #v>width then width=#v end
  86. end
  87. return width
  88. end
  89.  
  90. function ljust(s,w)
  91. local pad=w-#s
  92. return s .. string.rep(" ",pad)
  93. end
  94.  
  95. function rjust(s,w)
  96. local pad=w-#s
  97. return string.rep(" ",pad) .. s
  98. end
  99.  
  100. function display()
  101. term.clear()
  102. term.setCursorPos(1,1)
  103. print("Reactor Status")
  104. print(ft())
  105. print("")
  106. local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","CasingTemperature","FuelTemperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"}
  107. local units={"","","","RF","%","C","C","mB","mB","mB","RF/t"}
  108. local values={}
  109. for _,v in pairs(funcs) do
  110. values[#values+1] = tostring(r["get"..v]())
  111. end
  112. local funcW=tableWidth(funcs)
  113. local valW=tableWidth(values)
  114. for i,v in pairs(funcs) do
  115. print(rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i])
  116. end
  117. end
  118.  
  119. log("Starting")
  120. setupDevs()
  121. while true do
  122. local e=r.getEnergyStored()
  123. local p=math.floor(100*e/emax)
  124. local a=p<100
  125. local elt=r.getEnergyProducedLastTick()
  126. display()
  127. r.setAllControlRodLevels(p)
  128. r.setActive(a)
  129. if e > 9000000 then
  130. r.setActive(false)
  131. end
  132.  
  133. if e < 1000 then
  134. r.setActive(true)
  135. end
  136. os.startTimer(0.8333334)
  137. local event,p1,p2,p3,p4,p5 = os.pullEvent()
  138. if event == "key" then
  139. break
  140. elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  141. setupDevs()
  142. elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  143. error("received "..event)
  144. end
  145. end
  146.  
  147. end)
  148. term.restore()
  149. error(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement