oxgon

Direwolf20 error nil 304 fix code

Jan 19th, 2015
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = 6
  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.    cell = peripheral.wrap("left")
  305.    energyStored = cell.getEnergyStored()
  306.    energyMax = cell.getMaxEnergyStored()
  307.    energyStoredPercent = math.floor((energyStored/energyMax)*100)
  308.    RFProduction = r.getEnergyProducedLastTick()
  309.    fuelUse = r.getFuelConsumedLastTick()
  310.    fuelUse = math.floor(fuelUse*100)
  311.    fuelUse = fuelUse/100
  312.    coreTemp = r.getFuelTemperature()
  313.    reactorOnline = r.getActive()
  314.    tempEnergy = cell.getEnergyStored()
  315.    sleep(0.1)
  316.    energy = (cell.getMaxEnergyStored()-tempEnergy)/2
  317.    energy = energy*6
  318.    if steamReactor then
  319.      turbineOnline = t.getActive()
  320.      turbineRotorSpeed = t.getRotorSpeed()
  321.      turbineRFGen = t.getEnergyProducedLastTick()
  322.      turbineFluidRate = t.getFluidFlowRate()
  323.    end
  324. end
  325.  
  326. function fuelRodLevel()
  327.    rodLevel = r.getControlRodLevel(0)
  328.    --print(rodLevel)
  329.    m.setCursorPos(30,5)
  330.    m.write(tostring(rodLevel).."%")
  331.    m.setBackgroundColor(colors.white)
  332.    m.setCursorPos(28,6)
  333.    m.write("       ")
  334.    for i = 1,10 do
  335.       m.setCursorPos(28,i+6)
  336.       m.setBackgroundColor(colors.white)
  337.       m.write(" ")
  338.       m.setBackgroundColor(colors.yellow)
  339.       m.write(" ")
  340.       if rodLevel/10 >= i then
  341.          m.setBackgroundColor(colors.red)
  342.       else
  343.          m.setBackgroundColor(colors.yellow)
  344.       end
  345.       m.write("   ")
  346.       m.setBackgroundColor(colors.yellow)
  347.       m.write(" ")
  348.       m.setBackgroundColor(colors.white)
  349.       m.write(" ")
  350.    end
  351.    m.setCursorPos(28,17)
  352.    m.write("       ")
  353.    m.setBackgroundColor(colors.black)
  354. end
  355.  
  356. function turbineInductorDisplay()
  357.    turbineInductor = t.getInductorEngaged()
  358.    m.setCursorPos(30,5)
  359.    if turbineInductor then
  360.       m.write("On")
  361.    else
  362.       m.write("Off")
  363.    end
  364.    m.setBackgroundColor(colors.gray)
  365.    m.setCursorPos(28,6)
  366.    m.write("       ")
  367.    for i = 1,7 do
  368.       m.setCursorPos(28,i+6)
  369.       m.setBackgroundColor(colors.gray)
  370.       m.write(" ")
  371.       m.setBackgroundColor(colors.lightGray)
  372.       m.write(" ")
  373.       if i % 2 == 0 then
  374.          m.setBackgroundColor(colors.gray)
  375.       end
  376.       m.write(" ")
  377.       m.setBackgroundColor(colors.gray)
  378.       m.write(" ")
  379.       if i % 2 ~= 0 then
  380.          m.setBackgroundColor(colors.lightGray)
  381.       end
  382.       m.write(" ")
  383.       m.setBackgroundColor(colors.lightGray)
  384.       m.write(" ")
  385.       m.setBackgroundColor(colors.gray)
  386.       m.write(" ")
  387.    end
  388.    for i = 8,10 do
  389.    m.setCursorPos(28,i+6)
  390.       m.setBackgroundColor(colors.gray)
  391.       m.write(" ")
  392.       m.setBackgroundColor(colors.lightGray)
  393.       m.write(" ")
  394.       if turbineInductor then
  395.          m.setBackgroundColor(colors.red)
  396.       else
  397.          m.setBackgroundColor(colors.blue)
  398.       end
  399.       m.write(" ")
  400.       m.setBackgroundColor(colors.gray)
  401.       m.write(" ")
  402.       if turbineInductor then
  403.          m.setBackgroundColor(colors.red)
  404.       else
  405.          m.setBackgroundColor(colors.blue)
  406.       end
  407.       m.write(" ")
  408.       m.setBackgroundColor(colors.lightGray)
  409.       m.write(" ")
  410.       m.setBackgroundColor(colors.gray)
  411.       m.write(" ")
  412.    end
  413.    m.setCursorPos(28,17)
  414.    m.write("       ")
  415.    m.setBackgroundColor(colors.black)
  416. end
  417. function getClick()
  418.    local event, side, x, y = os.pullEvent("monitor_touch")
  419.    button.checkxy(x,y)
  420. end
  421.  
  422. function findOptFuelRods()
  423.    m.clear()
  424.    r.setActive(false)
  425.    checkEn()
  426.    displayEn()
  427.    fuelRodLevel()
  428.    while r.getFuelTemperature() > 99 do
  429.       for i= 1,3 do
  430.         checkEn()
  431.         displayEn()
  432.         fuelRodLevel()
  433.         m.setCursorPos(3,6)
  434.         m.write("Finding Optimal Rod Level")
  435.         m.setCursorPos(3,7)
  436.         m.write("Core Temp: "..r.getFuelTemperature())
  437.         m.setCursorPos(3,8)
  438.         m.write("Waiting for 99c")
  439.         sleep(1)
  440.       end
  441.    end
  442.    while r.getHotFluidAmount() > 10000 do
  443.      for i = 1,3 do
  444.         checkEn()
  445.         displayEn()
  446.         fuelRodLevel()
  447.         m.setCursorPos(3,6)
  448.         m.write("Finding Optimal Rod Level, please wait....")
  449.         m.setCursorPos(3,7)
  450.         m.write("Fluid Amount: "..comma_value(r.getHotFluidAmount()).."mb")
  451.         m.setCursorPos(3,8)
  452.         m.write("Waiting for 10,000mb")
  453.         sleep(1)
  454.       end
  455.    end
  456.    r.setAllControlRodLevels(99)
  457.    r.setActive(true)
  458.    
  459.    while r.getFuelTemperature() < 100 do
  460.    for i = 1,5 do
  461.      checkEn()
  462.      displayEn()
  463.      fuelRodLevel()
  464.      m.setCursorPos(3,6)
  465.      m.write("Set all rod levels to 99")
  466.      m.setCursorPos(3,7)
  467.      m.write("Waiting 5 seconds...")
  468.      sleep(1)
  469.    end
  470.    end
  471.    for i = 1,5 do
  472.      checkEn()
  473.      displayEn()
  474.      fuelRodLevel()
  475.      m.setCursorPos(3,6)
  476.      m.write("Set all rod levels to 99")
  477.      m.setCursorPos(3,7)
  478.      m.write("Waiting 5 seconds...")
  479.      sleep(1)
  480.    end
  481.    local tempMB = r.getEnergyProducedLastTick()
  482.    print(tempMB.."MB/t of steam")
  483.    local tempRodLevels = math.floor(2000/tempMB)
  484.    print("2000/"..tempMB.." = "..tempRodLevels)
  485.    tempRodLevels = 100-tempRodLevels+5
  486.    print("Adding 5 to Rod Levels: "..math.floor(tempRodLevels))
  487.    r.setAllControlRodLevels(math.floor(tempRodLevels))
  488.    print("Waiting 10 seconds to confirm...")
  489.    for i = 1,10 do
  490.      checkEn()
  491.      displayEn()
  492.      fuelRodLevel()
  493.      m.setCursorPos(3,6)
  494.      m.write("Estimated Level: "..tempRodLevels)
  495.      m.setCursorPos(3,7)
  496.      m.write("Waiting 10 seconds...")
  497.      sleep(1)
  498.    end
  499.    tempMB = r.getEnergyProducedLastTick()
  500.    while tempMB > 2000 do
  501.       tempRodLevels = tempRodLevels+1
  502.       r.setAllControlRodLevels(math.floor(tempRodLevels))
  503.       print("Setting Rod Levels to: "..tempRodLevels)
  504.       for i = 1,5 do
  505.        checkEn()
  506.        displayEn()
  507.        fuelRodLevel()
  508.        m.setCursorPos(3,6)
  509.        m.write("Getting below 2000mb/t")
  510.        m.setCursorPos(3,7)
  511.        m.write("Currently at: "..tempMB)
  512.        sleep(1)
  513.       end
  514.       tempMB = r.getEnergyProducedLastTick()
  515.    end
  516.    while tempMB < 2000 do
  517.       tempRodLevels = tempRodLevels -1
  518.       r.setAllControlRodLevels(math.floor(tempRodLevels))
  519.       print("Setting Rod Levels to: "..tempRodLevels)
  520.       for i = 1,5 do
  521.        checkEn()
  522.        displayEn()
  523.        fuelRodLevel()
  524.        m.setCursorPos(3,6)
  525.        m.write("Getting Above 2000mb/t")
  526.        m.setCursorPos(3,7)
  527.        m.write("Currently at: "..tempMB)
  528.        sleep(1)
  529.       end
  530.       tempMB = r.getEnergyProducedLastTick()
  531.    end
  532.    OptFuelRodLevel = tempRodLevels
  533. end
  534.    
  535.  
  536. function autoReactor()
  537.    if not steamReactor then
  538.       r.setAllControlRodLevels(0)
  539.       if energyStoredPercent < turnOnAt then
  540.        if not reactorOnline then
  541.          online()
  542.        end
  543.       end
  544.       if energyStoredPercent > turnOffAt then
  545.        if reactorOnline then
  546.          offline()
  547.        end
  548.       end
  549.     else
  550.        r.setAllControlRodLevels(OptFuelRodLevel)
  551.        if energyStoredPercent < turnOnAt then
  552.          --online()
  553.              setTurbineOnline()
  554.              coilsOn()
  555.        end
  556.        if energyStoredPercent > turnOffAt then
  557.          --if turbineRotorSpeed > 1800 then
  558.          --   offline()
  559.          --else
  560.             --   online()
  561.          --end
  562.          setTurbineOnline()
  563.          coilsOff()
  564.        end
  565.     if turbineRotorSpeed > 1800 then
  566.        offline()
  567.     else
  568.        online()
  569.     end
  570.     end
  571. end
  572.  
  573. function displayScreen()
  574.  --  repeat
  575.       checkEn()
  576.       displayEn()
  577.       if menuType == "Reactor" then
  578.         fuelRodLevel()
  579.         if mode == "Automatic" then
  580.            autoMenu()
  581.            autoReactor()
  582.         else
  583.            manualMenu()
  584.         end
  585.        
  586.       else
  587.         turbineInductorDisplay()
  588.         if mode == "Automatic" then
  589.            turbineAutoMenu()
  590.            autoReactor()
  591.         else
  592.            turbineManualMenu()
  593.         end
  594.        
  595.       end
  596.      
  597.       timerCode = os.startTimer(1)
  598.       local event, side, x, y
  599.       repeat
  600.         event, side, x, y = os.pullEvent()
  601.         print(event)
  602.         if event == "timer" then
  603.            print(timerCode..":"..side)
  604.            if timerCode ~= side then
  605.               print("Wrong Code")
  606.             else
  607.               print("Right Code")
  608.             end
  609.         end
  610.        until event~= "timer" or timerCode == side
  611.        if event == "monitor_touch" then
  612.             print(x..":"..y)
  613.             button.checkxy(x,y)
  614.         end
  615.  --  until event ~= "timer"
  616. end
  617.  
  618. if steamReactor then
  619.    findOptFuelRods()
  620. end
  621.  
  622. while true do
  623.    displayScreen()
  624. end
Advertisement
Add Comment
Please, Sign In to add comment