Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. -- Automatic control program for Big Reactors by ErogenousBeef – http://big-reactors.com/
  2. -- Author: Sam Sargeant <sam@sargeant.net.nz>
  3.  
  4. -- Originally based on http://pastebin.com/V0eueruY by Emily Backes <lucca@accela.net>
  5. -- Adapted for use with OpenComputers
  6.  
  7. -- Copyright (c) 2014, Sam Sargeant <sam@sargeant.net.nz>
  8. -- All rights reserved.
  9.  
  10. -- Redistribution and use in source and binary forms, with or without
  11. -- modification, are permitted provided that the following conditions are met:
  12. -- * Redistributions of source code must retain the above copyright
  13. -- notice, this list of conditions and the following disclaimer.
  14. -- * Redistributions in binary form must reproduce the above copyright
  15. -- notice, this list of conditions and the following disclaimer in the
  16. -- documentation and/or other materials provided with the distribution.
  17. -- * Neither the name of the <organization> nor the
  18. -- names of its contributors may be used to endorse or promote products
  19. -- derived from this software without specific prior written permission.
  20.  
  21. -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22. -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. -- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. -- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  25. -- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. -- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. -- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. -- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  
  32. local component = require("component")
  33. local serialization = require("serialization")
  34.  
  35. local modem = component.modem
  36. local portNumber = 2805
  37.  
  38. local term = require("term")
  39.  
  40. -- Max energy in a reactor's internal cell
  41. local emax=10000000
  42.  
  43. -- wrap everything in an exception handler
  44. local ok,msg=pcall(function ()
  45. local r
  46. local m
  47. local redirected=false
  48. local p
  49.  
  50. function setupDevs()
  51. r=assert(component.br_reactor)
  52. if (not r.getConnected()) then
  53. return nil, "Computer port not connected to a valid reactor"
  54. end
  55. --if (r.getNumberOfControlRods() <1) then
  56. -- return nil, "Reactor seems invalid"
  57. --end
  58. r.getEnergyPercent = function ()
  59. return math.floor(1000 * r.getEnergyStored() / emax)/10
  60. end
  61. if r.nativeEPLT then
  62. r.getEnergyProducedLastTick = r.nativeEPLT
  63. end
  64. r.nativeEPLT = r.getEnergyProducedLastTick
  65. r.getEnergyProducedLastTick = function ()
  66. return math.floor(r.nativeEPLT()*1000)/1000
  67. end
  68.  
  69. term.setCursorBlink(false)
  70. end
  71.  
  72. function ft ()
  73. -- local d=os.day()
  74. -- local t=os.time()
  75. -- local h=math.floor(t)
  76. -- local m=math.floor((t-h)*60)
  77. -- return string.format("Day %d, %02d:%02d",d,h,m)
  78. return "timestamp..."
  79. end
  80.  
  81. function log (msg)
  82. local stamp=ft()
  83. print (stamp..": "..msg)
  84. end
  85.  
  86. function tableWidth(t)
  87. local width=0
  88. for _,v in pairs(t) do
  89. if #v>width then width=#v end
  90. end
  91. return width + 4
  92. end
  93.  
  94. function ljust(s,w)
  95. local pad=w-#s
  96. return s .. string.rep(" ",pad)
  97. end
  98.  
  99. function rjust(s,w)
  100. local pad=w-#s
  101. return string.rep(" ",pad) .. s
  102. end
  103.  
  104. function display()
  105. local floatUnits = {"%","C","mB","RF/t"}
  106. term.clear()
  107. term.setCursor(1,1)
  108. msg = {name="Reactor A - TEST"}
  109. msg["metric"] = {}
  110. print("Reactor Status")
  111. print("")
  112. local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","FuelTemperature","CasingTemperature","FuelAmount","WasteAmount","FuelAmountMax", "FuelReactivity", "FuelConsumedLastTick", "EnergyProducedLastTick"}
  113. local units={"","","","RF","%","C","C","mB","mB","mB","%","mB", "RF/t"}
  114. local values={}
  115. for _,v in pairs(funcs) do
  116. thisVal = tostring(r["get"..v]())
  117. values[#values+1] = thisVal
  118. msg["metric"][v] = thisVal
  119. end
  120.  
  121. local funcW=tableWidth(funcs)
  122. local valW=tableWidth(values)
  123. for i,v in pairs(funcs) do
  124. print(string.format("%-12s: %s %s", v, values[i], units[i]))
  125. end
  126.  
  127. modem.broadcast(2805, serialization.serialize(msg))
  128. end
  129.  
  130. log("Starting")
  131. setupDevs()
  132. while true do
  133. local e=r.getEnergyStored()
  134. local p=math.floor(100*e/emax)
  135. local a=p<100
  136. local elt=r.getEnergyProducedLastTick()
  137. display()
  138. r.setAllControlRodLevels(p)
  139. r.setActive(a)
  140. os.sleep(1)
  141. -- local event,p1,p2,p3,p4,p5 = os.pullEvent()
  142. -- if event == "key" then
  143. -- break
  144. -- elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  145. -- setupDevs()
  146. -- elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  147. -- error("received "..event)
  148. -- end
  149. end
  150.  
  151. end)
  152.  
  153. error(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement