Guest User

Untitled

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