Morphik08

Turbine test1

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