karelvysinka

Reactor Radim

Oct 19th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.70 KB | None | 0 0
  1. --
  2. -- Control passive cooled Big Reactor (http://big-reactors.com/).
  3. --
  4. -- Author: kla_sch
  5. --
  6. -- History:
  7. --     v0.1, 2014-05-05:
  8. --         first version
  9. --
  10. -- Remarks:
  11. --     Reactor API: http://big-reactors.com/cc_api.html
  12. --
  13.  
  14. --
  15. -- Constant values (configuration)
  16. --
  17.  
  18. -- Critical energy mark: give us the maximum power.
  19. critEnergy=3000000
  20.  
  21. -- Low energy mark: turn reactor on to get more energy
  22. lowEnergy=7000000
  23.  
  24. -- Heigh energy mark: we have enough, so turn the reactor off.
  25. highEnergy=9000000
  26.  
  27.  
  28. --
  29. -- Calculate the control rod level (in %) by stored energy of internal
  30. -- recator cell.
  31. --
  32. -- * If cellEnergy <= critEnergy, the calculation results in maximum
  33. --   energy generation (control rod level = 0%).
  34. -- * If cellEnergy >= highEnergy, the calculation results in 10% energy
  35. --   generation (control rod level = 90%).
  36. --
  37. -- Parameters:
  38. --     cellEnergy - stored energy of internal cell in RF
  39. --
  40. -- Return:
  41. --     New control rod level in %.
  42. --
  43. function calcRodLevel(cellEnergy)
  44.    -- Delta between critical and heigh energy mark
  45.    local deltaEnergy = highEnergy - critEnergy
  46.  
  47.    -- Calculated maximum delta energy (not the real maximum), so that
  48.    -- the high energy mark results into a control rod level of 90%
  49.    local deltaEnergy100 = deltaEnergy * 100 / 90
  50.  
  51.    -- Energy for calculation: value between 0 and 'deltaEnergy'
  52.    local calcEnergy = cellEnergy - critEnergy
  53.  
  54.    if calcEnergy < 0 then
  55.       calcEnergy = 0
  56.    elseif calcEnergy > deltaEnergy then
  57.       calcEnergy = deltaEnergy
  58.    end
  59.  
  60.    -- Calculate control rod level and round the result (math.floor + 0.5)
  61.    return math.floor(calcEnergy * 100 / deltaEnergy100 + 0.5)
  62. end
  63.  
  64.  
  65. --
  66. -- Write text with colors, if possible (advance monitor)
  67. --
  68. -- Parameters:
  69. --     mon   - handle of monitor
  70. --     color - text color
  71. --     text  - text to write
  72. --
  73. function writeColor(mon, color, text)
  74.    if (mon.isColor()) then
  75.       mon.setTextColor(color)
  76.    end
  77.    mon.write(text)
  78.    if (mon.isColor()) then
  79.       mon.setTextColor(colors.white)
  80.    end
  81. end
  82.  
  83.  
  84. --
  85. -- Display reactor status to a monitor.
  86. --
  87. -- Parameters:
  88. --     mon        - handle of monitor.
  89. --     state      - state of reactor (on/off)
  90. --     rodLvl     - Level of control rods in %
  91. --     cellEnergy - stored energy of internal cell (in RF)
  92. --
  93. function displayStatusToMonitor(mon, state, rodLvl, cellEnergy)
  94.    mon.clear()
  95.  
  96.    -- First get the monitor size and try to scale, if the feature ist
  97.    -- available.
  98.    if mon.setTextScale ~= nil then
  99.       -- reset text scale
  100.       mon.setTextScale(1)
  101.    end
  102.  
  103.    local width, height = mon.getSize()
  104.    if width < 15 or height < 5 then
  105.       -- too small: try to scale down.
  106.       if mon.setTextScale ~= nil then
  107.          mon.setTextScale(0.5)
  108.       else
  109.          return -- too small und no text scale available
  110.       end
  111.  
  112.       width, height = mon.getSize()
  113.       if width < 15 or height < 5 then
  114.          return -- still too small
  115.       end
  116.    else
  117.       -- Huge monitors? Try to scale up, if possible (max scale=5).
  118.       local scale = math.min(width / 16, height / 5, 5)
  119.       scale = math.floor(scale * 2) / 2 -- multiple of 0.5
  120.  
  121.       if scale > 1 and mon.setTextScale ~= nil then
  122.          mon.setTextScale(scale)
  123.          width, height = mon.getSize()
  124.       end
  125.    end
  126.  
  127.  
  128.    --
  129.    -- Output the data
  130.    --
  131.  
  132.    mon.setCursorPos(1,1)
  133.    mon.write("Reactor")
  134.  
  135.    mon.setCursorPos(1,3)
  136.    mon.write("Status ")
  137.  
  138.    if state then
  139.       writeColor(mon, colors.green, "ON")
  140.    else
  141.       writeColor(mon, colors.red, "OFF")
  142.    end
  143.  
  144.    mon.setCursorPos(1,4)
  145.    mon.write("Rod Level: " .. rodLvl .. "%")
  146.  
  147.    mon.setCursorPos(1,5)
  148.    if width < 16 then
  149.       mon.write("Cell: ") -- One block monitor (15x5 with scale 0.5)
  150.    else
  151.       mon.write("Energy: ")
  152.    end
  153.  
  154.    local c
  155.    if cellEnergy < critEnergy then
  156.       c = colors.red -- Red: We use too much energy
  157.    elseif cellEnergy > lowEnergy then
  158.       c = colors.green -- Green: More energy then low water mark
  159.    else
  160.       c = colors.orange -- Orange: Less energy the low water mark, but OK
  161.    end
  162.    writeColor(mon, c, string.format("%d", math.floor(cellEnergy/1000 + 0.5)))
  163.    mon.write(" kRF")
  164. end
  165.  
  166.  
  167. --
  168. -- Display reactor status to any connected monitor and also to console.
  169. --
  170. -- Parameters:
  171. --     state      - state of reactor (on/off)
  172. --     rodLvl     - Level of control rods in %
  173. --     cellEnergy - stored energy of internal energy cell in RF
  174. --
  175. function displayStatus(state, rodLvl, cellEnergy)
  176.    displayStatusToMonitor(term, state, rodLvl, cellEnergy) -- console
  177.    term.setCursorPos(1,7)
  178.    term.write("* Hold Crtl-T to terminate program")
  179.    term.setCursorPos(1,8)
  180.  
  181.    local pList = peripheral.getNames()
  182.    local i, name
  183.    for i, name in pairs(pList) do
  184.       if peripheral.getType(name) == "monitor" then
  185.          -- found monitor as peripheral
  186.          displayStatusToMonitor(peripheral.wrap(name),
  187.                                 state, rodLvl, cellEnergy)
  188.       end
  189.    end
  190. end
  191.  
  192.  
  193. --
  194. -- Find the first connected big reactor and return the wraped handle.
  195. --
  196. -- If no reactor was found this function terminate the program.
  197. --
  198. -- Return:
  199. --     Handle of first connected reactor found.
  200. --
  201. function getReactorHandle()
  202.    local pList = peripheral.getNames()
  203.    local i, name
  204.    for i, name in pairs(pList) do
  205.       if peripheral.getType(name) == "BigReactors-Reactor" then
  206.          return peripheral.wrap(name)
  207.       end
  208.    end
  209.  
  210.    error("No big reactor connected: Exit program")
  211.    exit()
  212. end
  213.  
  214.  
  215. reactor = getReactorHandle()
  216.  
  217. --
  218. -- Endless loop: Recalculate rod level, set rod level, display result
  219. -- and wait for 5 secounds.
  220. --
  221. while true do
  222.    cellEnergy = reactor.getEnergyStored()
  223.    if cellEnergy < lowEnergy then
  224.       -- Low energy: switch reactor ON and calculate control rods by
  225.       -- energy cell level.
  226.       reactor.setActive(true)
  227.       rodLvl=calcRodLevel(cellEnergy)
  228.    elseif cellEnergy > highEnergy then
  229.       -- High energy: switch reactor OFF and set control rod level to 100
  230.       reactor.setActive(false)
  231.       rodLvl=100
  232.    elseif cellEnergy > lowEnergy then
  233.       -- Enough energy: do not change state of reactor. Only recalculate
  234.       -- control rod level.
  235.       --
  236.       -- * If the reactor ist switched off, we will wait until energy
  237.       --   fall below low energy mark.
  238.       --
  239.       -- * If it is turned on, we generate more energy until the
  240.       --   energy level exeeds the high energy mark.
  241.       rodLvl=calcRodLevel(cellEnergy)
  242.    end
  243.  
  244.    reactor.setAllControlRodLevels(rodLvl)
  245.  
  246.    displayStatus(reactor.getActive(), rodLvl, cellEnergy)
  247.  
  248.    os.sleep(5) -- Wait for 5s
  249. end
  250.  
  251. --
  252. -- EOF
  253. --
Add Comment
Please, Sign In to add comment