Advertisement
zeplintwo

Testing BR reactor Code

Feb 16th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.23 KB | None | 0 0
  1. --[[
  2. Installer pastebin code http://pastebin.com/MuS68wCr
  3. Reactor pastebin code http://pastebin.com/YbHGQ2wY
  4. Button pastebin code http://pastebin.com/wGAt78pv
  5. Startup pastebin code http://pastebin.com/irim4gRD
  6.  
  7. Changes:
  8.    -2/16/2015
  9.    - pulling hot water capacity from reactor in active cooled mode
  10.    - verfied 3 peak speed for turbines default speed is 1820. Low is 900, High 2728
  11.    - user set-able reactor offset for cold start-up default % increase is 5
  12.    - build Histarics to help turbines stay in high output zones
  13.    - corrected some venting issues causing long start-up time is odd cases
  14.    - functions turbineWarmup and turbineRunning to help reduce code and help histarics implementation.
  15.  
  16.  
  17.    -2/15/2015
  18.    - adjusted hot water start to 20,000 mb
  19.    - set turnOnAt to 60 from 50
  20.    - set numCapacitors to 10 for house from 3
  21.    - function setVenting() for dumping water in reactor
  22.    - function setVentRuning() closing turbine before startup
  23. ]]--
  24.  
  25. os.loadAPI("button")
  26.  
  27. p = peripheral.find("tile_blockcapacitorbank_name")
  28. m = peripheral.find("monitor")
  29. r = peripheral.find("BigReactors-Reactor")
  30. t = peripheral.find("BigReactors-Turbine")
  31.  
  32. local steamReactor = r.isActivelyCooled()
  33.  
  34. local numCapacitors = 27 -- number of capacitors in your capacitor bank
  35. local turnOnAt = 60 -- capacitor percentage to turn coils on in turbine or turn on the reactor
  36. local turnOffAt = 90 -- capacitor percentage to shut coils off on turbine or shut reactor off
  37. local turbineHistarics = 40 -- band above targetSpeed for turbine Max RPM
  38. local reactorOffset = 5 -- used to help cold start-ups this is % of rods in during startup  \
  39. local steamMin = 2000 -- Min um steam to start the program lower only to accommodate 1 rod reactors
  40. local targetSpeed = 1840 -- valid speeds peaks 900, (1840), 2728
  41.  
  42. local energy = 0
  43. local energyStored = 0
  44. local energyMax = 0
  45. local energyStoredPercent = 0
  46. local timerCode
  47. local mode = "Automatic"
  48. local RFProduction = 0
  49. local fuelUse = 0
  50. local coreTemp = 0
  51. local reactorOnline = false
  52. local rodLevel = 0
  53.  
  54. local turbineOnline = false
  55. local turbineRotorSpeed = 0
  56. local turbineRFGen = 0
  57. local turbineFluidRate = 0
  58. local turbineInductor = false
  59. local targetSpeedMax = (targetSpeed + turbineHistarics)
  60. local powerToggle=false
  61.  
  62. local OptFuelRodLevel = 0
  63.  
  64. if (steamReactor == true) then
  65.     local menuType = "Turbine"
  66. else
  67.     local menuType = "Reactor"
  68. end
  69.  
  70. function autoMenu()
  71.     m.setTextScale(1)
  72.     button.clearTable()
  73.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  74.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  75.    
  76.     if steamReactor then
  77.        button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  78.        button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  79.     end
  80.     button.screen()
  81.     checkMode()
  82.     menuMode()
  83. end
  84.  
  85. function manualMenu()
  86.     m.setTextScale(1)
  87.     button.clearTable()
  88.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  89.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  90.     button.setTable("Online", online, "", 3, 13, 8, 8)
  91.     button.setTable("Offline", offline, "", 15, 25, 8, 8)
  92.     button.setTable("0", setRods, 0, 11,14, 10, 10)
  93.     button.setTable("10", setRods, 10, 5,8, 12, 12)
  94.     button.setTable("20", setRods, 20, 11,14, 12, 12)
  95.     button.setTable("30", setRods, 30, 17,20, 12, 12)
  96.     button.setTable("40", setRods, 40, 5,8, 14, 14)
  97.     button.setTable("50", setRods, 50, 11,14, 14, 14)
  98.     button.setTable("60", setRods, 60, 17,20, 14, 14)
  99.     button.setTable("70", setRods, 70, 5,8, 16, 16)
  100.     button.setTable("80", setRods, 80, 11,14, 16, 16)
  101.     button.setTable("90", setRods, 90, 17,20, 16, 16)
  102.     button.setTable("+", rodPlus, "", 23, 25, 12, 12)
  103.     button.setTable("-", rodMinus, "", 23, 25, 16, 16)
  104.     if steamReactor then
  105.         button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  106.         button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  107.     end
  108.     button.screen()
  109.     checkMode()
  110.     reactorOnOff()
  111.     menuMode()
  112. end
  113.  
  114. function turbineAutoMenu()
  115.     m.setTextScale(1)
  116.     button.clearTable()
  117.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  118.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  119.     button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  120.     button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  121.     button.screen()
  122.     checkMode()
  123.     menuMode()
  124. end
  125.  
  126. function turbineManualMenu()
  127.     m.setTextScale(1)
  128.     button.clearTable()
  129.     button.setTable("Automatic", autoMode, "", 3, 13, 6, 6)
  130.     button.setTable("Manual", manualMode, "", 15, 25, 6, 6)
  131.     button.setTable("Reactor", reactorMenu, "", 5, 18, 19, 19)
  132.     button.setTable("Turbine", turbineMenu, "", 22, 35, 19, 19)
  133.     button.setTable("Online", setTurbineOnline, "", 3, 13, 8, 8)
  134.     button.setTable("Offline", setTurbineOffline, "", 15, 25, 8, 8)
  135.     button.setTable("Coils On", coilsOn, "", 3, 13, 10, 10)
  136.     button.setTable("Coils Off", coilsOff, "", 15, 25, 10, 10)
  137.     button.screen()
  138.     checkMode()
  139.     turbineOnOff()
  140.     coilsOnOff()
  141.     menuMode()
  142. end
  143.  
  144. function reactorMenu()
  145.     menuType = "Reactor"
  146.     displayScreen()
  147. end
  148.  
  149. function turbineMenu()
  150.     menuType = "Turbine"
  151.     displayScreen()
  152. end
  153.  
  154. function online()
  155.     r.setActive(true)
  156. end
  157.  
  158. function offline()
  159.     r.setActive(false)
  160. end
  161.  
  162. function setTurbineOnline()
  163.     t.setActive(true)
  164. end
  165.  
  166. function setTurbineOffline()
  167.     t.setActive(false)
  168. end
  169.  
  170. function reactorOnOff()
  171.     button.setButton("Online", r.getActive())
  172.     button.setButton("Offline", not r.getActive())
  173. end
  174.  
  175. function turbineOnOff()
  176.     button.setButton("Online", t.getActive())
  177.     button.setButton("Offline", not t.getActive())
  178. end
  179.  
  180. function coilsOnOff()
  181.     button.setButton("Coils On", t.getInductorEngaged())
  182.     button.setButton("Coils Off", not t.getInductorEngaged())
  183. end
  184.  
  185. function coilsOn()
  186.     t.setInductorEngaged(true)
  187. end
  188.  
  189. function coilsOff()
  190.     t.setInductorEngaged(false)
  191. end
  192.  
  193. function menuMode()
  194.     if steamReactor then
  195.         if menuType == "Reactor" then
  196.             button.setButton("Reactor", true)
  197.             button.setButton("Turbine", false)
  198.         else
  199.             button.setButton("Reactor", false)
  200.             button.setButton("Turbine", true)
  201.         end
  202.     end
  203. end
  204.  
  205. function setRods(setLevel)
  206.     button.flash(tostring(setLevel))
  207.     r.setAllControlRodLevels(setLevel)
  208.     fuelRodLevel()
  209. end
  210.  
  211. function rodPlus()
  212.     button.flash("+")
  213.     r.setAllControlRodLevels(rodLevel+1)
  214.     fuelRodLevel()
  215. end
  216.  
  217. function rodMinus()
  218.     button.flash("-")
  219.     r.setAllControlRodLevels(rodLevel-1)
  220.     fuelRodLevel()
  221. end
  222.  
  223. function checkMode()
  224.     button.toggleButton(mode)
  225. end
  226.    
  227. function manualMode()
  228.     mode = "Manual"
  229.     manualMenu()
  230. end
  231.  
  232. function autoMode()
  233.     mode = "Automatic"
  234.     displayScreen()
  235. end
  236.  
  237. function comma_value(amount)
  238.     local formatted = amount
  239.     local swap = false
  240.     if formatted < 0 then
  241.         formatted = formatted*-1
  242.         swap = true
  243.     end
  244.     while true do
  245.         formatted, k = string.gsub(formatted, "^(%d+)(%d%d%d)", '%1,%2')
  246.         if k == 0 then
  247.             break
  248.         end
  249.     end
  250.     if swap then
  251.         formatted = "-"..formatted
  252.     end
  253.     return formatted
  254. end
  255.  
  256. function displayEn()
  257.     m.clear()
  258.     m.setCursorPos(1,1)
  259.     m.write("Energy Use: ")
  260.     if energy < 0 then
  261.         m.setTextColor(colors.red)
  262.     else
  263.         m.setTextColor(colors.green)
  264.     end
  265.     m.write(comma_value(math.floor(energy)).. "RF/t")
  266.     m.setTextColor(colors.white)
  267.     m.setCursorPos(1,2)
  268.     m.write("Energy Stored: "..energyStoredPercent.."%")
  269.     if menuType == "Reactor" then
  270.         m.setCursorPos(1,3)
  271.         m.write("Reactor is: ")
  272.         if reactorOnline then
  273.             m.setTextColor(colors.green)
  274.             m.write("Online")
  275.         else
  276.             m.setTextColor(colors.red)
  277.             m.write("Offline")
  278.         end
  279.         m.setTextColor(colors.white)
  280.         m.setCursorPos(22,1)
  281.         if steamReactor then
  282.             m.write("Steam: ")
  283.             m.setTextColor(colors.green)
  284.             m.write(comma_value(math.floor(RFProduction)).."MB/t")
  285.         else
  286.             m.write("RF Gen: ")
  287.             m.setTextColor(colors.green)
  288.             m.write(comma_value(math.floor(RFProduction)).."RF/t")
  289.         end
  290.         m.setTextColor(colors.white)
  291.         m.setCursorPos(22,2)
  292.         m.write("Core Temp: "..math.floor(coreTemp).."c")
  293.         m.setCursorPos(22,3)
  294.         m.write("Fuel Use: "..fuelUse.."MB/t")  
  295.     else
  296.         m.setCursorPos(1,3)
  297.         m.write("Turbine is: ")
  298.         if turbineOnline then
  299.             m.setTextColor(colors.green)
  300.             m.write("Online")
  301.         else
  302.             m.setTextColor(colors.red)
  303.             m.write("Offline")
  304.         end
  305.         m.setCursorPos(1,4)
  306.         m.setTextColor(colors.white)
  307.         m.write("Reactor is: ")
  308.         if reactorOnline then
  309.             m.setTextColor(colors.green)
  310.             m.write("Online")
  311.         else
  312.             m.setTextColor(colors.red)
  313.             m.write("Offline")
  314.         end
  315.         m.setTextColor(colors.white)
  316.         m.setCursorPos(22,1)
  317.         m.write("RFGen: ")
  318.         m.setTextColor(colors.green)
  319.         m.write(comma_value(math.floor(turbineRFGen)).."RF/t")
  320.         m.setTextColor(colors.white)
  321.         m.setCursorPos(22,2)
  322.         m.write("Rotor: "..comma_value(math.floor(turbineRotorSpeed)).." RPM")
  323.         m.setCursorPos(22,3)
  324.         m.write("Steam: "..comma_value(turbineFluidRate).."MB/t")  
  325.     end
  326. end
  327.  
  328. function checkEn()
  329.     local tempEnergy = 0
  330.     energyStored = p.getEnergyStored()
  331.     energyMax = p.getMaxEnergyStored()
  332.     energyStoredPercent = math.floor((energyStored/energyMax)*100)
  333.     RFProduction = r.getEnergyProducedLastTick()
  334.     fuelUse = r.getFuelConsumedLastTick()
  335.     fuelUse = math.floor(fuelUse*100)
  336.     fuelUse = fuelUse/100
  337.     coreTemp = r.getFuelTemperature()
  338.     reactorOnline = r.getActive()
  339.     tempEnergy = p.getEnergyStored()
  340.     sleep(0.1)
  341.     energy = (p.getEnergyStored()-tempEnergy)/2
  342.     energy = energy*numCapacitors
  343.     if steamReactor then
  344.         turbineOnline = t.getActive()
  345.         turbineRotorSpeed = t.getRotorSpeed()
  346.         turbineRFGen = t.getEnergyProducedLastTick()
  347.         turbineFluidRate = t.getFluidFlowRate()
  348.     end
  349. end
  350.  
  351. function fuelRodLevel()
  352.     rodLevel = r.getControlRodLevel(0)
  353.     m.setCursorPos(30,5)
  354.     m.write(tostring(rodLevel).."%")
  355.     m.setBackgroundColor(colors.white)
  356.     m.setCursorPos(28,6)
  357.     m.write("       ")
  358.     for i = 1,10 do
  359.         m.setCursorPos(28,i+6)
  360.         m.setBackgroundColor(colors.white)
  361.         m.write(" ")
  362.         m.setBackgroundColor(colors.yellow)
  363.         m.write(" ")
  364.         if rodLevel/10 >= i then
  365.             m.setBackgroundColor(colors.red)
  366.         else
  367.             m.setBackgroundColor(colors.yellow)
  368.         end
  369.         m.write("   ")
  370.         m.setBackgroundColor(colors.yellow)
  371.         m.write(" ")
  372.         m.setBackgroundColor(colors.white)
  373.         m.write(" ")
  374.     end
  375.     m.setCursorPos(28,17)
  376.     m.write("       ")
  377.     m.setBackgroundColor(colors.black)
  378. end
  379.  
  380. function turbineInductorDisplay()
  381.     turbineInductor = t.getInductorEngaged()
  382.     m.setCursorPos(30,5)
  383.     if turbineInductor then
  384.         m.write("On")
  385.     else
  386.         m.write("Off")
  387.     end
  388.     m.setBackgroundColor(colors.gray)
  389.     m.setCursorPos(28,6)
  390.     m.write("       ")
  391.     for i = 1,7 do
  392.         m.setCursorPos(28,i+6)
  393.         m.setBackgroundColor(colors.gray)
  394.         m.write(" ")
  395.         m.setBackgroundColor(colors.lightGray)
  396.         m.write(" ")
  397.         if i % 2 == 0 then
  398.             m.setBackgroundColor(colors.gray)
  399.         end
  400.         m.write(" ")
  401.         m.setBackgroundColor(colors.gray)
  402.         m.write(" ")
  403.         if i % 2 ~= 0 then
  404.             m.setBackgroundColor(colors.lightGray)
  405.         end
  406.         m.write(" ")
  407.         m.setBackgroundColor(colors.lightGray)
  408.         m.write(" ")
  409.         m.setBackgroundColor(colors.gray)
  410.         m.write(" ")
  411.     end
  412.     for i = 8,10 do
  413.         m.setCursorPos(28,i+6)
  414.         m.setBackgroundColor(colors.gray)
  415.         m.write(" ")
  416.         m.setBackgroundColor(colors.lightGray)
  417.         m.write(" ")
  418.         if turbineInductor then
  419.             m.setBackgroundColor(colors.red)
  420.         else
  421.             m.setBackgroundColor(colors.blue)
  422.         end
  423.         m.write(" ")
  424.         m.setBackgroundColor(colors.gray)
  425.         m.write(" ")
  426.         if turbineInductor then
  427.             m.setBackgroundColor(colors.red)
  428.         else
  429.             m.setBackgroundColor(colors.blue)
  430.         end
  431.         m.write(" ")
  432.         m.setBackgroundColor(colors.lightGray)
  433.         m.write(" ")
  434.         m.setBackgroundColor(colors.gray)
  435.         m.write(" ")
  436.     end
  437.     m.setCursorPos(28,17)
  438.     m.write("       ")
  439.     m.setBackgroundColor(colors.black)
  440. end
  441.  
  442. function getClick()
  443.     local event, side, x, y = os.pullEvent("monitor_touch")
  444.     button.checkxy(x,y)
  445. end
  446.  
  447. function setVenting()
  448.     t.setVentAll()
  449.     t.setActive(true)
  450. end
  451.  
  452. function setVentRuning()
  453.     t.setVentOverflow()
  454.     t.setActive(true)
  455. end
  456.  
  457. function turbineWarmup()
  458.     setTurbineOnline()
  459.     coilsOff()
  460. end
  461.  
  462. function turbineRunning()
  463.     setTurbineOnline()
  464.     coilsOn()
  465. end
  466.  
  467.    
  468. function findOptFuelRods()
  469.     m.clear()
  470.     r.setActive(false)
  471.     checkEn()
  472.     displayEn()
  473.     fuelRodLevel()
  474.     while r.getFuelTemperature() > 99 do
  475.         turbineWarmup()
  476.         for i= 1,3 do
  477.             checkEn()
  478.             displayEn()
  479.             fuelRodLevel()
  480.             m.setCursorPos(3,6)
  481.             m.write("Finding Optimal Rod Level")
  482.             m.setCursorPos(3,7)
  483.             m.write("Core Temp: "..r.getFuelTemperature())
  484.             m.setCursorPos(3,8)
  485.             m.write("Waiting for 99c")
  486.             sleep(1)
  487.         end
  488.     end
  489.     local reactorFluidCap = r.getCoolantAmountMax()
  490.     while r.getHotFluidAmount() > reactorFluidCap do
  491.         turbineWarmup()
  492.         for i = 1,3 do
  493.             checkEn()
  494.             displayEn()
  495.             fuelRodLevel()
  496.             setVentRuning()
  497.             m.setCursorPos(2,6)
  498.             m.write("Finding Optimal Rod Level, please wait...")
  499.             m.setCursorPos(2,7)
  500.             m.write("Fluid Amount: "..comma_value(r.getHotFluidAmount()).."mb")
  501.             m.setCursorPos(2,8)
  502.             m.write("Waiting for fluid levels.")
  503.             sleep(1)
  504.         end
  505.     end
  506.     setVentRuning()
  507.     r.setAllControlRodLevels(99)
  508.     r.setActive(true)
  509.     while r.getFuelTemperature() < 100 do
  510.         for i = 1,5 do
  511.             turbineWarmup()
  512.             checkEn()
  513.             displayEn()
  514.             fuelRodLevel()
  515.             setVentRuning()
  516.             m.setCursorPos(3,6)
  517.             m.write("Set all rod levels to 99")
  518.             m.setCursorPos(3,7)
  519.             m.write("Waiting 5 seconds...")
  520.             sleep(1)
  521.         end
  522.     end
  523.     for i = 1,5 do
  524.         turbineWarmup()
  525.         checkEn()
  526.         displayEn()
  527.         fuelRodLevel()
  528.         setVentRuning()
  529.         m.setCursorPos(3,6)
  530.         m.write("Set all rod levels to 99")
  531.         m.setCursorPos(3,7)
  532.         m.write("Waiting 5 seconds...")
  533.         sleep(1)
  534.     end
  535.     local tempMB = r.getEnergyProducedLastTick()
  536.     local tempRodLevels = math.floor(steamMin/tempMB)
  537.     tempRodLevels = 100-tempRodLevels+reactorOffset
  538.     if tempRodLevels < 0 then
  539.         if reactorOffset <0 then
  540.             reactorOffset = 5
  541.         end
  542.         tempRodLevels = (0 + reactorOffset)
  543.     end
  544.     r.setAllControlRodLevels(math.floor(tempRodLevels))
  545.     for i = 1,10 do
  546.         turbineWarmup()
  547.         checkEn()
  548.         displayEn()
  549.         fuelRodLevel()
  550.         setVentRuning()
  551.         m.setCursorPos(3,6)
  552.         m.write("Estimated Level: "..tempRodLevels)
  553.         m.setCursorPos(3,7)
  554.         m.write("Waiting 10 seconds...")
  555.         sleep(1)
  556.     end
  557.     tempMB = r.getEnergyProducedLastTick()
  558.     while tempMB > steamMin do
  559.         turbineWarmup()
  560.         tempRodLevels = tempRodLevels+1
  561.         r.setAllControlRodLevels(math.floor(tempRodLevels))
  562.         for i = 1,5 do
  563.             turbineWarmup()
  564.             checkEn()
  565.             displayEn()
  566.             fuelRodLevel()
  567.             setVentRuning()
  568.             m.setCursorPos(3,6)
  569.             m.write("Getting below "..steamMin.."mb/t")
  570.             m.setCursorPos(3,7)
  571.             m.write("Currently at: "..tempMB)
  572.             sleep(1)
  573.         end
  574.         tempMB = r.getEnergyProducedLastTick()
  575.     end
  576.     while tempMB < steamMin do
  577.         tempRodLevels = tempRodLevels -1
  578.         r.setAllControlRodLevels(math.floor(tempRodLevels))
  579.         for i = 1,5 do
  580.             turbineWarmup()
  581.             checkEn()
  582.             displayEn()
  583.             fuelRodLevel()
  584.             setVentRuning()
  585.             m.setCursorPos(3,6)
  586.             m.write("Getting Above "..steamMin.."mb/t")
  587.             m.setCursorPos(3,7)
  588.             m.write("Currently at: "..tempMB)
  589.             sleep(1)
  590.         end
  591.         tempMB = r.getEnergyProducedLastTick()
  592.     end
  593.     OptFuelRodLevel = tempRodLevels
  594. end
  595.    
  596. function autoReactor()
  597.     if not steamReactor then
  598.         r.setAllControlRodLevels(0)
  599.         if energyStoredPercent < turnOnAt then
  600.             if not reactorOnline then
  601.                 online()
  602.             end
  603.         end
  604.         if energyStoredPercent > turnOffAt then
  605.             if reactorOnline then
  606.                 offline()
  607.             end
  608.         end
  609.     else
  610.         r.setAllControlRodLevels(OptFuelRodLevel)
  611.         if energyStoredPercent < turnOnAt then
  612.             turbineRunning()
  613.         end
  614.         if energyStoredPercent > turnOffAt then
  615.             turbineWarmup()
  616.         end
  617.         if turbineRotorSpeed > targetSpeed then
  618.             offline()
  619.         else
  620.             online()
  621.         end
  622.     end
  623. end
  624.  
  625. function displayScreen()
  626.     checkEn()
  627.     displayEn()
  628.     if menuType == "Reactor" then
  629.         fuelRodLevel()
  630.         if mode == "Automatic" then
  631.             autoMenu()
  632.             autoReactor()
  633.         else
  634.             manualMenu()
  635.         end
  636.     else
  637.         turbineInductorDisplay()
  638.         if mode == "Automatic" then
  639.             turbineAutoMenu()
  640.             autoReactor()
  641.         else
  642.             turbineManualMenu()
  643.         end
  644.     end
  645.     timerCode = os.startTimer(1)
  646.     local event, side, x, y
  647.     repeat
  648.     event, side, x, y = os.pullEvent()
  649.     until event~= "timer" or timerCode == side
  650.     if event == "monitor_touch" then
  651.     button.checkxy(x,y)
  652.     end
  653. end
  654.  
  655. if steamReactor then
  656.    findOptFuelRods()
  657. end
  658.  
  659. while true do
  660.    displayScreen()
  661. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement