Advertisement
peptide

Big Reactor 2

Mar 2nd, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.04 KB | None | 0 0
  1. -- Low energy mark: raise the rods ( lower the value )
  2. lowEnergy=1000000
  3.  
  4. -- High energy mark: lower the rods (raise the value )
  5. highEnergy=9000000
  6.  
  7.  
  8. --
  9. -- Decide if the rod level should change
  10. --
  11. -- * If cellEnergy < lowEnergy remove 1% (speed up)
  12. -- * If cellEnergy > highEnergy add 1% (slow down)
  13. --
  14. -- Parameters:
  15. --     rodLvl - current level of the rods
  16. --     cellEnergy - stored energy of internal cell in RF
  17. --
  18. -- Return:
  19. --     New control rod level in %.
  20. --
  21. function calcRodLevel(rodLvl, cellEnergy)
  22.  
  23.    if cellEnergy < lowEnergy then
  24.       return rodLvl - 1
  25.    elseif cellEnergy > highEnergy then
  26.       return rodLvl + 1
  27.    else
  28.       return rodLvl
  29.    end
  30.  
  31. end
  32.  
  33.  
  34. --
  35. -- Write text with colors, if possible (advance monitor)
  36. --
  37. -- Parameters:
  38. --     mon   - handle of monitor
  39. --     color - text color
  40. --     text  - text to write
  41. --
  42. function writeColor(mon, color, text)
  43.    if (mon.isColor()) then
  44.       mon.setTextColor(color)
  45.    end
  46.    mon.write(text)
  47.    if (mon.isColor()) then
  48.       mon.setTextColor(colors.white)
  49.    end
  50. end
  51.  
  52.  
  53. --
  54. -- Display reactor status to a monitor.
  55. --
  56. -- Parameters:
  57. --     mon        - handle of monitor.
  58. --     state      - state of reactor (on/off)
  59. --     rodLvl     - Level of control rods in %
  60. --     cellEnergy - stored energy of internal cell (in RF)
  61. --
  62. function displayStatusToMonitor(mon, state, rodLvl, cellEnergy)
  63.    mon.clear()
  64.  
  65.    -- First get the monitor size and try to scale, if the feature is
  66.    -- available.
  67.    if mon.setTextScale ~= nil then
  68.       -- reset text scale
  69.       mon.setTextScale(1)
  70.    end
  71.  
  72.    local width, height = mon.getSize()
  73.    if width < 15 or height < 5 then
  74.       -- too small: try to scale down.
  75.       if mon.setTextScale ~= nil then
  76.          mon.setTextScale(0.5)
  77.       else
  78.          return -- too small und no text scale available
  79.       end
  80.  
  81.       width, height = mon.getSize()
  82.       if width < 15 or height < 5 then
  83.          return -- still too small
  84.       end
  85.    else
  86.       -- Huge monitors? Try to scale up, if possible (max scale=5).
  87.       local scale = math.min(width / 16, height / 5, 5)
  88.       scale = math.floor(scale * 2) / 2 -- multiple of 0.5
  89.  
  90.       if scale > 1 and mon.setTextScale ~= nil then
  91.          mon.setTextScale(scale)
  92.          width, height = mon.getSize()
  93.       end
  94.    end
  95.  
  96.  
  97.    --
  98.    -- Output the data
  99.    --
  100.  
  101.    mon.setCursorPos(1,1)
  102.    mon.write("Reactor")
  103.  
  104.    mon.setCursorPos(1,3)
  105.    mon.write("Status ")
  106.  
  107.    if state then
  108.       writeColor(mon, colors.green, "ON")
  109.    else
  110.       writeColor(mon, colors.red, "OFF")
  111.    end
  112.  
  113.    mon.setCursorPos(1,4)
  114.    mon.write("Rod Level: " .. rodLvl .. "%")
  115.  
  116.    mon.setCursorPos(1,5)
  117.    if width < 16 then
  118.       mon.write("Cell: ") -- One block monitor (15x5 with scale 0.5)
  119.    else
  120.       mon.write("Energy: ")
  121.    end
  122.  
  123.    local c
  124.    if cellEnergy < critEnergy then
  125.       c = colors.red -- Red: We use too much energy
  126.    elseif cellEnergy > lowEnergy then
  127.       c = colors.green -- Green: More energy then low water mark
  128.    else
  129.       c = colors.orange -- Orange: Less energy the low water mark, but OK
  130.    end
  131.    writeColor(mon, c, string.format("%d", math.floor(cellEnergy/1000 + 0.5)))
  132.    mon.write(" kRF")
  133.  
  134. end
  135.  
  136.  
  137. --
  138. -- Display reactor status to any connected monitor and also to console.
  139. --
  140. -- Parameters:
  141. --     state      - state of reactor (on/off)
  142. --     rodLvl     - Level of control rods in %
  143. --     cellEnergy - stored energy of internal energy cell in RF
  144. --
  145. function displayStatus(state, rodLvl, cellEnergy)
  146.    displayStatusToMonitor(term, state, rodLvl, cellEnergy) -- console
  147.    term.setCursorPos(1,7)
  148.    term.write("* Hold Crtl-T to terminate program")
  149.    term.setCursorPos(1,8)
  150.  
  151.    local pList = peripheral.getNames()
  152.    local i, name
  153.    for i, name in pairs(pList) do
  154.       if peripheral.getType(name) == "monitor" then
  155.          -- found monitor as peripheral
  156.          displayStatusToMonitor(peripheral.wrap(name),
  157.                                 state, rodLvl, cellEnergy)
  158.       end
  159.    end
  160. end
  161.  
  162.  
  163. --
  164. -- Find the first connected big reactor and return the wraped handle.
  165. --
  166. -- If no reactor was found this function terminate the program.
  167. --
  168. -- Return:
  169. --     Handle of first connected reactor found.
  170. --
  171. function getReactorHandle()
  172.    local pList = peripheral.getNames()
  173.    local i, name
  174.    for i, name in pairs(pList) do
  175.       if peripheral.getType(name) == "BigReactors-Reactor" then
  176.          return peripheral.wrap(name)
  177.       end
  178.    end
  179.  
  180.    error("No big reactor connected: Exit program")
  181.    exit()
  182. end
  183.  
  184.  
  185. reactor = getReactorHandle()
  186.  
  187. --
  188. -- Endless loop: Recalculate rod level, set rod level, display result
  189. -- and wait for 5 secounds.
  190. --
  191.  
  192. rodLvl = 100
  193. reactor.setAllControlRodLevels(rodLvl)
  194. reactor.setActive(true)
  195.  
  196. while true do
  197.    cellEnergy = reactor.getEnergyStored()
  198.  
  199.    rodLvl=calcRodLevel(rodLvl, cellEnergy)
  200.  
  201.    reactor.setAllControlRodLevels(rodLvl)
  202.  
  203.    displayStatus(reactor.getActive(), rodLvl, cellEnergy)
  204.  
  205.    os.sleep(20) -- Wait for 20s
  206. end
  207.  
  208. --
  209. -- EOF
  210. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement