Advertisement
Tindrith

Clone of DW20 Reactor Program

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