Advertisement
swemoney

DireWolf20 reactor program modified for Energy Cells

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