Advertisement
Guest User

turbineControl.lua

a guest
Jan 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 33.82 KB | None | 0 0
  1. -- Reactor- und Turbine control by Thor_s_Crafter --
  2. -- Version 2.6 --
  3. -- Turbine control --
  4.  
  5. --Loads the touchpoint API
  6. shell.run("cp /reactor-turbine-program/config/touchpoint.lua /touchpoint")
  7. os.loadAPI("touchpoint")
  8. shell.run("rm touchpoint")
  9.  
  10. --Loads the input API
  11. shell.run("cp /reactor-turbine-program/config/input.lua /input")
  12. os.loadAPI("input")
  13. shell.run("rm input")
  14.  
  15. --Some variables
  16. --Touchpoint init
  17. local page = touchpoint.new(touchpointLocation)
  18. --Buttons
  19. local rOn
  20. local rOff
  21. local tOn
  22. local tOff
  23. local aTOn
  24. local aTOff
  25. local aTN = { "  -  ", label = "aTurbinesOn" }
  26. local cOn
  27. local cOff
  28. --Last/Current turbine (for switching)
  29. local lastStat = 0
  30. local currStat = 0
  31. --Last/Current TurbineSpeed (for checking)
  32. local lastSpeed = {}
  33. local currSpeed = {}
  34. local speedFailCounter = {}
  35.  
  36. --Button renaming
  37. if lang == "de" then
  38.     rOn = { " Ein ", label = "reactorOn" }
  39.     rOff = { " Aus ", label = "reactorOn" }
  40.     tOn = { " Ein ", label = "turbineOn" }
  41.     tOff = { " Aus ", label = "turbineOn" }
  42.     aTOn = { " Ein ", label = "aTurbinesOn" }
  43.     aTOff = { " Aus ", label = "aTurbinesOn" }
  44.     cOn = { " Ein ", label = "coilsOn" }
  45.     cOff = { " Aus ", label = "coilsOn" }
  46. elseif lang == "en" then
  47.     rOn = { " On  ", label = "reactorOn" }
  48.     rOff = { " Off ", label = "reactorOn" }
  49.     tOn = { " On  ", label = "turbineOn" }
  50.     tOff = { " Off ", label = "turbineOn" }
  51.     aTOn = { " On ", label = "aTurbinesOn" }
  52.     aTOff = { " Off ", label = "aTurbinesOn" }
  53.     cOn = { " On  ", label = "coilsOn" }
  54.     cOff = { " Off ", label = "coilsOn" }
  55. end
  56.  
  57.  
  58. --Init auto mode
  59. function startAutoMode()
  60.     --Everything setup correctly?
  61.     checkPeripherals()
  62.  
  63.     --Loads/Calculates the reactor's rod level
  64.     findOptimalFuelRodLevel()
  65.  
  66.     --Clear display
  67.     term.clear()
  68.     term.setCursorPos(1, 1)
  69.  
  70.     --Display prints
  71.     print("Getting all Turbines to " .. turbineTargetSpeed .. " RPM...")
  72.     mon.setBackgroundColor(backgroundColor)
  73.     mon.setTextColor(textColor)
  74.     mon.clear()
  75.     mon.setCursorPos(1, 1)
  76.  
  77.     if lang == "de" then
  78.         mon.write("Bringe Turbinen auf " .. (input.formatNumber(turbineTargetSpeed)) .. " RPM. Bitte warten...")
  79.         --In Englisch
  80.     elseif lang == "en" then
  81.         mon.write("Getting Turbines to " .. (input.formatNumberComma(turbineTargetSpeed)) .. " RPM. Please wait...")
  82.     end
  83.  
  84.     --Gets turbine to target speed
  85.     --Init SpeedTables
  86.     initSpeedTable()
  87.     while not allAtTargetSpeed() do
  88.         getToTargetSpeed()
  89.         sleep(1)
  90.         term.setCursorPos(1, 2)
  91.         for i = 0, amountTurbines, 1 do
  92.             local tSpeed = t[i].getRotorSpeed()
  93.  
  94.             print("Speed: " .. tSpeed .. "     ")
  95.  
  96.             --formatting and printing status
  97.             mon.setTextColor(textColor)
  98.             mon.setCursorPos(1, (i + 3))
  99.             if i >= 16 then mon.setCursorPos(28, (i - 16 + 3)) end
  100.             if lang == "de" then
  101.                 if (i + 1) < 10 then
  102.                     mon.write("Turbine 0" .. (i + 1) .. ": " .. (input.formatNumber(math.floor(tSpeed))) .. " RPM")
  103.                 else
  104.                     mon.write("Turbine " .. (i + 1) .. ": " .. (input.formatNumber(math.floor(tSpeed))) .. " RPM")
  105.                 end
  106.             elseif lang == "en" then
  107.                 if (i + 1) < 10 then
  108.                     mon.write("Turbine 0" .. (i + 1) .. ": " .. (input.formatNumberComma(math.floor(tSpeed))) .. " RPM")
  109.                 else
  110.                     mon.write("Turbine " .. (i + 1) .. ": " .. (input.formatNumberComma(math.floor(tSpeed))) .. " RPM")
  111.                 end
  112.             end
  113.             if tSpeed > turbineTargetSpeed then
  114.                 mon.setTextColor(colors.green)
  115.                 mon.write(" OK  ")
  116.             else
  117.                 mon.setTextColor(colors.red)
  118.                 mon.write(" ...  ")
  119.             end
  120.         end
  121.     end
  122.  
  123.     --Enable reactor and turbines
  124.     r.setActive(true)
  125.     allTurbinesOn()
  126.  
  127.     --Reset terminal
  128.     term.clear()
  129.     term.setCursorPos(1, 1)
  130.  
  131.     --Reset Monitor
  132.     mon.setBackgroundColor(backgroundColor)
  133.     mon.clear()
  134.     mon.setTextColor(textColor)
  135.     mon.setCursorPos(1, 1)
  136.  
  137.     --Creates all buttons
  138.     createAllButtons()
  139.  
  140.     --Displays first turbine (default)
  141.     printStatsAuto(0)
  142.  
  143.     --run
  144.     clickEvent()
  145. end
  146.  
  147. --Init manual mode
  148. function startManualMode()
  149.     --Everything setup correctly?
  150.     checkPeripherals()
  151.     --Creates all buttons
  152.     createAllButtons()
  153.     --Creates additional manual buttons
  154.     createManualButtons()
  155.  
  156.     --Sets all turbine flow rates to maximum (if set different in auto mode)
  157.     for i = 0, #t do
  158.         t[i].setFluidFlowRateMax(targetSteam)
  159.     end
  160.  
  161.     --Displays the first turbine (default)
  162.     printStatsMan(0)
  163.  
  164.     --run
  165.     clickEvent()
  166. end
  167.  
  168. --Checks if all required peripherals are attached
  169. function checkPeripherals()
  170.     mon.setBackgroundColor(colors.black)
  171.     mon.clear()
  172.     mon.setCursorPos(1, 1)
  173.     mon.setTextColor(colors.red)
  174.     term.clear()
  175.     term.setCursorPos(1, 1)
  176.     term.setTextColor(colors.red)
  177.     --No turbine found
  178.     if t[0] == nil then
  179.         if lang == "de" then
  180.             mon.write("Turbinen nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  181.             error("Turbinen nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  182.         elseif lang == "en" then
  183.             mon.write("Turbines not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  184.             error("Turbines not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  185.         end
  186.     end
  187.     --No reactor found
  188.     --No energy storage found
  189.     if v == "" then
  190.         if lang == "de" then
  191.             mon.write("Energiespeicher nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  192.             error("Energiespeicher nicht gefunden! Bitte pruefen und den Computer neu starten (Strg+R gedrueckt halten)")
  193.         elseif lang == "en" then
  194.             mon.write("Energy Storage not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  195.             error("Energy Storage not found! Please check and reboot the computer (Press and hold Ctrl+R)")
  196.         end
  197.     end
  198. end
  199.  
  200. function getEnergy()
  201.     return v.getEnergyStored()
  202. end
  203.  
  204. function getEnergyMax()
  205.     return v.getMaxEnergyStored()
  206. end
  207.  
  208. function getEnergyPer()
  209.     local en = getEnergy()
  210.     local enMax = getEnergyMax()
  211.     local enPer = math.floor(en / enMax * 100)
  212.     return enPer
  213. end
  214.  
  215. --Returns the current energy fill status of a turbine
  216. function getTurbineEnergy(turbine)
  217.     return t[turbine].getEnergyStored()
  218. end
  219.  
  220. --Toggles the reactor status and the button
  221. function toggleReactor()
  222.     r.setActive(not r.getActive())
  223.     page:toggleButton("reactorOn")
  224.     if r.getActive() then
  225.         page:rename("reactorOn", rOn, true)
  226.     else
  227.         page:rename("reactorOn", rOff, true)
  228.     end
  229. end
  230.  
  231. --Toggles one turbine status and button
  232. function toggleTurbine(i)
  233.     t[i].setActive(not t[i].getActive())
  234.     page:toggleButton("turbineOn")
  235.     if t[i].getActive() then
  236.         page:rename("turbineOn", tOn, true)
  237.     else
  238.         page:rename("turbineOn", tOff, true)
  239.     end
  240. end
  241.  
  242. --Toggles one turbine coils and button
  243. function toggleCoils(i)
  244.     t[i].setInductorEngaged(not t[i].getInductorEngaged())
  245.     page:toggleButton("coilsOn")
  246.     if t[i].getInductorEngaged() then
  247.         page:rename("coilsOn", cOn, true)
  248.     else
  249.         page:rename("coilsOn", cOff, true)
  250.     end
  251. end
  252.  
  253. --Enable all turbines (Coils engaged, FluidRate 2000mb/t)
  254. function allTurbinesOn()
  255.     for i = 0, amountTurbines, 1 do
  256.         t[i].setActive(true)
  257.         t[i].setInductorEngaged(true)
  258.         t[i].setFluidFlowRateMax(targetSteam)
  259.     end
  260. end
  261.  
  262. --Disable all turbiens (Coils disengaged, FluidRate 0mb/t)
  263. function allTurbinesOff()
  264.     for i = 0, amountTurbines, 1 do
  265.         t[i].setInductorEngaged(false)
  266.         t[i].setFluidFlowRateMax(0)
  267.     end
  268. end
  269.  
  270. --Enable one turbine
  271. function turbineOn(i)
  272.     t[i].setInductorEngaged(true)
  273.     t[i].setFluidFlowRateMax(targetSteam)
  274. end
  275.  
  276. --Disable one turbine
  277. function turbineOff(i)
  278.     t[i].setInductorEngaged(false)
  279.     t[i].setFluidFlowRateMax(0)
  280. end
  281.  
  282. --Toggles all turbines (and buttons)
  283. function toggleAllTurbines()
  284.     page:rename("aTurbinesOn", aTOff, true)
  285.     local onOff
  286.     if t[0].getActive() then onOff = "off" else onOff = "on" end
  287.     for i = 0, amountTurbines do
  288.         if onOff == "off" then
  289.             t[i].setActive(false)
  290.             if page.buttonList["aTurbinesOn"].active then
  291.                 page:toggleButton("aTurbinesOn")
  292.                 page:rename("aTurbinesOn", aTOff, true)
  293.             end
  294.         else
  295.             t[i].setActive(true)
  296.             if not page.buttonList["aTurbinesOn"].active then
  297.                 page:toggleButton("aTurbinesOn")
  298.                 page:rename("aTurbinesOn", aTOn, true)
  299.             end --if
  300.         end --else
  301.     end --for
  302. end
  303.  
  304. --function
  305.  
  306. --Toggles all turbine coils (and buttons)
  307. function toggleAllCoils()
  308.     local coilsOnOff
  309.     if t[0].getInductorEngaged() then coilsOnOff = "off" else coilsOnOff = "on" end
  310.     for i = 0, amountTurbines do
  311.         if coilsOnOff == "off" then
  312.             t[i].setInductorEngaged(false)
  313.             if page.buttonList["Coils"].active then
  314.                 page:toggleButton("Coils")
  315.             end
  316.         else
  317.             t[i].setInductorEngaged(true)
  318.             if not page.buttonList["Coils"].active then
  319.                 page:toggleButton("Coils")
  320.             end
  321.         end
  322.     end
  323. end
  324.  
  325. --Calculates/Reads the optiomal reactor rod level
  326. function findOptimalFuelRodLevel()
  327.  
  328.     --Load config?
  329.     if not (math.floor(rodLevel) == 0) then
  330.         r.setAllControlRodLevels(rodLevel)
  331.  
  332.     else
  333.         --Get reactor below 99c
  334.         getTo99c()
  335.  
  336.         --Enable reactor + turbines
  337.         r.setActive(true)
  338.         allTurbinesOn()
  339.  
  340.         --Calculation variables
  341.         local controlRodLevel = 99
  342.         local diff = 0
  343.         local targetSteamOutput = targetSteam * (amountTurbines + 1)
  344.         local targetLevel = 99
  345.  
  346.         --Display
  347.         mon.setBackgroundColor(backgroundColor)
  348.         mon.setTextColor(textColor)
  349.         mon.clear()
  350.  
  351.         print("TargetSteam: " .. targetSteamOutput)
  352.  
  353.         if lang == "de" then
  354.             mon.setCursorPos(1, 1)
  355.             mon.write("Finde optimales FuelRod Level...")
  356.             mon.setCursorPos(1, 3)
  357.             mon.write("Berechne Level...")
  358.             mon.setCursorPos(1, 5)
  359.             mon.write("Gesuchter Steam-Output: " .. (input.formatNumber(math.floor(targetSteamOutput))) .. "mb/t")
  360.         elseif lang == "en" then
  361.             mon.setCursorPos(1, 1)
  362.             mon.write("Finding optimal FuelRod Level...")
  363.             mon.setCursorPos(1, 3)
  364.             mon.write("Calculating Level...")
  365.             mon.setCursorPos(1, 5)
  366.             mon.write("Target Steam-Output: " .. (input.formatNumberComma(math.floor(targetSteamOutput))) .. "mb/t")
  367.         end
  368.  
  369.         --Calculate Level based on 2 values
  370.         local failCounter = 0
  371.         while true do
  372.             r.setAllControlRodLevels(controlRodLevel)
  373.             sleep(2)
  374.             local steamOutput1 = r.getHotFluidProducedLastTick()
  375.             print("SO1: " .. steamOutput1)
  376.             r.setAllControlRodLevels(controlRodLevel - 1)
  377.             sleep(5)
  378.             local steamOutput2 = r.getHotFluidProducedLastTick()
  379.             print("SO2: " .. steamOutput2)
  380.             diff = steamOutput2 - steamOutput1
  381.             print("Diff: " .. diff)
  382.  
  383.             targetLevel = 100 - math.floor(targetSteamOutput / diff)
  384.             print("Target: " .. targetLevel)
  385.  
  386.             --Check target level
  387.             if targetLevel < 0 or targetLevel == "-inf" then
  388.  
  389.                 --Calculation failed 3 times?
  390.                 if failCounter > 2 then
  391.                     mon.setBackgroundColor(colors.black)
  392.                     mon.clear()
  393.                     mon.setTextColor(colors.red)
  394.                     mon.setCursorPos(1, 1)
  395.  
  396.                     if lang == "de" then
  397.                         mon.write("RodLevel-Berechnung fehlgeschlagen!")
  398.                         mon.setCursorPos(1, 2)
  399.                         mon.write("Berechnung waere < 0!")
  400.                         mon.setCursorPos(1, 3)
  401.                         mon.write("Bitte Steam/Wasser-Input pruefen!")
  402.                     elseif lang == "en" then
  403.                         mon.write("RodLevel calculation failed!")
  404.                         mon.setCursorPos(1, 2)
  405.                         mon.write("Calculation would be < 0!")
  406.                         mon.setCursorPos(1, 3)
  407.                         mon.write("Please check Steam/Water input!")
  408.                     end
  409.  
  410.                     --Disable reactor and turbines
  411.                     r.setActive(false)
  412.                     allTurbinesOff()
  413.                     for i = 1, amountTurbines do
  414.                         t[i].setActive(false)
  415.                     end
  416.  
  417.  
  418.                     term.clear()
  419.                     term.setCursorPos(1, 1)
  420.                     print("Target RodLevel: " .. targetLevel)
  421.                     error("Failed to calculate RodLevel!")
  422.  
  423.                 else
  424.                     failCounter = failCounter + 1
  425.                     sleep(2)
  426.                 end
  427.  
  428.                 print("FailCounter: " .. failCounter)
  429.  
  430.             else
  431.                 break
  432.             end
  433.         end
  434.  
  435.         --RodLevel calculation successful
  436.         print("RodLevel calculation successful!")
  437.         r.setAllControlRodLevels(targetLevel)
  438.         controlRodLevel = targetLevel
  439.  
  440.         --Find precise level
  441.         while true do
  442.             sleep(5)
  443.             local steamOutput = r.getHotFluidProducedLastTick()
  444.  
  445.             mon.setCursorPos(1, 3)
  446.             mon.write("FuelRod Level: " .. controlRodLevel .. "  ")
  447.  
  448.             if lang == "de" then
  449.                 mon.setCursorPos(1, 6)
  450.                 mon.write("Aktueller Steam-Output: " .. (input.formatNumber(steamOutput)) .. "mb/t    ")
  451.             elseif lang == "en" then
  452.                 mon.setCursorPos(1, 6)
  453.                 mon.write("Current Steam-Output: " .. (input.formatNumberComma(steamOutput)) .. "mb/t    ")
  454.             end
  455.  
  456.             --Level too big
  457.             if steamOutput < targetSteamOutput then
  458.                 controlRodLevel = controlRodLevel - 1
  459.                 r.setAllControlRodLevels(controlRodLevel)
  460.  
  461.             else
  462.                 r.setAllControlRodLevels(controlRodLevel)
  463.                 rodLevel = controlRodLevel
  464.                 saveOptionFile()
  465.                 print("Target RodLevel: " .. controlRodLevel)
  466.                 sleep(2)
  467.                 break
  468.             end --else
  469.         end --while
  470.     end --else
  471. end
  472.  
  473. --function
  474.  
  475. --Gets the reactor below 99c
  476. function getTo99c()
  477.     mon.setBackgroundColor(backgroundColor)
  478.     mon.setTextColor(textColor)
  479.     mon.clear()
  480.     mon.setCursorPos(1, 1)
  481.  
  482.     if lang == "de" then
  483.         mon.write("Bringe Reaktor unter 99 Grad...")
  484.     elseif lang == "en" then
  485.         mon.write("Getting Reactor below 99c ...")
  486.     end
  487.  
  488.     --Disables reactor and turbines
  489.     r.setActive(false)
  490.     allTurbinesOn()
  491.  
  492.     --Temperature variables
  493.     local fTemp = r.getFuelTemperature()
  494.     local cTemp = r.getCasingTemperature()
  495.     local isNotBelow = true
  496.  
  497.     --Wait until both values are below 99
  498.     while isNotBelow do
  499.         term.setCursorPos(1, 2)
  500.         print("CoreTemp: " .. fTemp .. "      ")
  501.         print("CasingTemp: " .. cTemp .. "      ")
  502.  
  503.         fTemp = r.getFuelTemperature()
  504.         cTemp = r.getCasingTemperature()
  505.  
  506.         if fTemp < 99 then
  507.             if cTemp < 99 then
  508.                 isNotBelow = false
  509.             end
  510.         end
  511.  
  512.         sleep(1)
  513.     end --while
  514. end
  515.  
  516. --function
  517.  
  518. --Checks the current energy level and controlls turbines/reactor
  519. --based on user settings (reactorOn, reactorOff)
  520. function checkEnergyLevel()
  521.     printStatsAuto(currStat)
  522.     --Level > user setting (default: 90%)
  523.     if getEnergyPer() >= reactorOffAt then
  524.         print("Energy >= reactorOffAt")
  525.         if turbineOnOff == "on" then
  526.             allTurbinesOn()
  527.         elseif turbineOnOff == "off" then
  528.             allTurbinesOff()
  529.         end
  530.         r.setActive(false)
  531.         --Level < user setting (default: 50%)
  532.     elseif getEnergyPer() <= reactorOnAt then
  533.         r.setActive(true)
  534.         for i = 0, amountTurbines do
  535.             t[i].setFluidFlowRateMax(targetSteam)
  536.             if t[i].getRotorSpeed() < turbineTargetSpeed * 0.98 then
  537.                 t[i].setInductorEngaged(false)
  538.             end
  539.             if t[i].getRotorSpeed() > turbineTargetSpeed * 1.02 then
  540.                 t[i].setInductorEngaged(true)
  541.             end
  542.         end
  543.  
  544.     else
  545.         if r.getActive() then
  546.             for i = 0, amountTurbines do
  547.                 if t[i].getRotorSpeed() < turbineTargetSpeed * 0.98 then
  548.                     t[i].setInductorEngaged(false)
  549.                 end
  550.                 if t[i].getRotorSpeed() > turbineTargetSpeed * 1.02 then
  551.                     t[i].setInductorEngaged(true)
  552.                 end
  553.             end --for
  554.         end --if
  555.     end --else
  556. end
  557.  
  558. --Sets the tables for checking the current turbineSpeeds
  559. function initSpeedTable()
  560.     for i = 0, amountTurbines do
  561.         lastSpeed[i] = 0
  562.         currSpeed[i] = 0
  563.         speedFailCounter[i] = 0
  564.     end
  565. end
  566.  
  567. --Gets turbines to targetSpeed
  568. function getToTargetSpeed()
  569.     for i = 0, amountTurbines, 1 do
  570.  
  571.         --Get the current speed of the turbine
  572.         local tspeed = t[i].getRotorSpeed()
  573.  
  574.         --Control turbines
  575.         if tspeed <= turbineTargetSpeed then
  576.             r.setActive(true)
  577.             t[i].setActive(true)
  578.             t[i].setInductorEngaged(false)
  579.             t[i].setFluidFlowRateMax(targetSteam)
  580.         end
  581.         if t[i].getRotorSpeed() > turbineTargetSpeed then
  582.             turbineOff(i)
  583.         end
  584.  
  585.  
  586.         --Not working yet - Needs reworking
  587.         --        --Write speed to the currSpeed table
  588.         --        currSpeed[i] = tspeed
  589.         --
  590.         --        --Check turbine speed progression
  591.         --        if currSpeed[i] < lastSpeed[i]-50 then
  592.         --
  593.         --            print(speedFailCounter)
  594.         --
  595.         --            --Return error message
  596.         --            if speedFailCounter[i] >= 3 then
  597.         --                mon.setBackgroundColor(colors.black)
  598.         --                mon.clear()
  599.         --                mon.setTextColor(colors.red)
  600.         --                mon.setCursorPos(1, 1)
  601.         --                if lang == "de" then
  602.         --                    mon.write("Turbinen koennen nicht auf Speed gebracht werden!")
  603.         --                    mon.setCursorPos(1,2)
  604.         --                    mon.write("Bitte den Steam-Input pruefen!")
  605.         --                    error("Turbinen koennen nicht auf Speed gebracht werden!")
  606.         --                elseif lang == "en" then
  607.         --                    mon.write("Turbines can't get to speed!")
  608.         --                    mon.setCursorPos(1,2)
  609.         --                    mon.write("Please check your Steam-Input!")
  610.         --                    error("Turbines can't get to speed!")
  611.         --                end
  612.         --
  613.         --            --increase speedFailCounter
  614.         --            else
  615.         --                speedFailCounter[i] = speedFailCounter[i] + 1
  616.         --            end
  617.         --        end
  618.         --
  619.         --        --Write speed to the lastSpeed table
  620.         --        lastSpeed[i] = tspeed
  621.     end
  622. end
  623.  
  624. --Returns true if all turbines are at targetSpeed
  625. function allAtTargetSpeed()
  626.     for i = 0, amountTurbines do
  627.         if t[i].getRotorSpeed() < turbineTargetSpeed then
  628.             return false
  629.         end
  630.     end
  631.     return true
  632. end
  633.  
  634. --Runs another program
  635. function run(program)
  636.     shell.run(program)
  637.     shell.completeProgram("/reactor-turbine-program/program/turbineControl.lua")
  638.     error("terminated.")
  639. end
  640.  
  641. --Creates all required buttons
  642. function createAllButtons()
  643.     local x1 = 40
  644.     local x2 = 47
  645.     local x3 = 54
  646.     local x4 = 61
  647.     local y = 4
  648.  
  649.     --Turbine buttons
  650.     for i = 0, amountTurbines, 1 do
  651.         if overallMode == "auto" then
  652.             if i <= 7 then
  653.                 page:add("#" .. (i + 1), function() printStatsAuto(i) end, x1, y, x1 + 5, y)
  654.             elseif (i > 7 and i <= 15) then
  655.                 page:add("#" .. (i + 1), function() printStatsAuto(i) end, x2, y, x2 + 5, y)
  656.             elseif (i > 15 and i <= 23) then
  657.                 page:add("#" .. (i + 1), function() printStatsAuto(i) end, x3, y, x3 + 5, y)
  658.             elseif i > 23 then
  659.                 page:add("#" .. (i + 1), function() printStatsAuto(i) end, x4, y, x4 + 5, y)
  660.             end
  661.             if (i == 7 or i == 15 or i == 23) then y = 4
  662.             else y = y + 2
  663.             end
  664.  
  665.         elseif overallMode == "manual" then
  666.             if i <= 7 then
  667.                 page:add("#" .. (i + 1), function() printStatsMan(i) end, x1, y, x1 + 5, y)
  668.             elseif (i > 7 and i <= 15) then
  669.                 page:add("#" .. (i + 1), function() printStatsMan(i) end, x2, y, x2 + 5, y)
  670.             elseif (i > 15 and i <= 23) then
  671.                 page:add("#" .. (i + 1), function() printStatsMan(i) end, x3, y, x3 + 5, y)
  672.             elseif i > 23 then
  673.                 page:add("#" .. (i + 1), function() printStatsMan(i) end, x4, y, x4 + 5, y)
  674.             end
  675.             if (i == 7 or i == 15 or i == 23) then y = 4
  676.             else y = y + 2
  677.             end
  678.         end --mode
  679.     end --for
  680.  
  681.     --Other buttons
  682.     if lang == "de" then
  683.         page:add("Hauptmenue", function() run("/reactor-turbine-program/start/menu.lua") end, 2, 23, 17, 23)
  684.         --In Englisch
  685.     elseif lang == "en" then
  686.         page:add("Main Menu", function() run("/reactor-turbine-program/start/menu.lua") end, 2, 23, 17, 23)
  687.     end
  688.     page:draw()
  689. end
  690.  
  691. --Creates (additional) manual buttons
  692. function createManualButtons()
  693.     page:add("reactorOn", toggleReactor, 11, 11, 15, 11)
  694.     page:add("Coils", toggleAllCoils, 25, 17, 31, 17)
  695.     page:add("aTurbinesOn", toggleAllTurbines, 18, 17, 23, 17)
  696.     page:rename("aTurbinesOn", aTN, true)
  697.  
  698.     --Switch reactor button?
  699.     if r.getActive() then
  700.         page:rename("reactorOn", rOn, true)
  701.         page:toggleButton("reactorOn")
  702.     else
  703.         page:rename("reactorOn", rOff, true)
  704.     end
  705.  
  706.     --Turbine buttons on/off
  707.     page:add("turbineOn", function() toggleTurbine(currStat) end, 20, 13, 24, 13)
  708.     if t[currStat].getActive() then
  709.         page:rename("turbineOn", tOn, true)
  710.         page:toggleButton("turbineOn")
  711.     else
  712.         page:rename("turbineOn", tOff, true)
  713.     end
  714.  
  715.     -- Turbinen buttons (Coils)
  716.     page:add("coilsOn", function() toggleCoils(currStat) end, 9, 15, 13, 15)
  717.     if t[currStat].getInductorEngaged() then
  718.         page:rename("coilsOn", cOn, true)
  719.     else
  720.         page:rename("coilsOn", cOff, true)
  721.     end
  722.     page:draw()
  723. end
  724.  
  725. --Checks for events (timer/clicks)
  726. function clickEvent()
  727.  
  728.     while true do
  729.  
  730.         --refresh screen
  731.         if overallMode == "auto" then
  732.             checkEnergyLevel()
  733.         elseif overallMode == "manual" then
  734.             printStatsMan(currStat)
  735.         end
  736.  
  737.         --timer
  738.         local timer1 = os.startTimer(1)
  739.  
  740.         while true do
  741.             --gets the event
  742.             local event, p1 = page:handleEvents(os.pullEvent())
  743.             print(event .. ", " .. p1)
  744.  
  745.             --execute a buttons function if clicked
  746.             if event == "button_click" then
  747.                 page:flash(p1)
  748.                 page.buttonList[p1].func()
  749.                 break
  750.             elseif event == "timer" and p1 == timer1 then
  751.                 break
  752.             end
  753.         end
  754.     end
  755. end
  756.  
  757. --displays all info on the screen (auto mode)
  758. function printStatsAuto(turbine)
  759.     --refresh current turbine
  760.     currStat = turbine
  761.  
  762.     --toggles turbine buttons if pressed (old button off, new button on)
  763.     if not page.buttonList["#" .. currStat + 1].active then
  764.         page:toggleButton("#" .. currStat + 1)
  765.     end
  766.     if currStat ~= lastStat then
  767.         if page.buttonList["#" .. lastStat + 1].active then
  768.             page:toggleButton("#" .. lastStat + 1)
  769.         end
  770.     end
  771.  
  772.     --gets overall energy production
  773.     local rfGen = 0
  774.     for i = 0, amountTurbines, 1 do
  775.         rfGen = rfGen + t[i].getEnergyProducedLastTick()
  776.     end
  777.  
  778.     --prints the energy level (in %)
  779.     mon.setBackgroundColor(tonumber(backgroundColor))
  780.     mon.setTextColor(tonumber(textColor))
  781.  
  782.     mon.setCursorPos(2, 2)
  783.     if lang == "de" then
  784.         mon.write("Energie: " .. getEnergyPer() .. "%  ")
  785.     elseif lang == "en" then
  786.         mon.write("Energy: " .. getEnergyPer() .. "%  ")
  787.     end
  788.  
  789.     --prints the energy bar
  790.     local part1 = getEnergyPer() / 5
  791.     mon.setCursorPos(2, 3)
  792.     mon.setBackgroundColor(colors.lightGray)
  793.     mon.write("                    ")
  794.     mon.setBackgroundColor(colors.green)
  795.     mon.setCursorPos(2, 3)
  796.     for i = 1, part1 do
  797.         mon.write(" ")
  798.     end
  799.     mon.setTextColor(textColor)
  800.  
  801.     --prints the overall energy production
  802.     mon.setBackgroundColor(tonumber(backgroundColor))
  803.  
  804.     mon.setCursorPos(2, 5)
  805.     if lang == "de" then
  806.         mon.write("RF-Produktion: " .. (input.formatNumber(math.floor(rfGen))) .. " RF/t      ")
  807.     elseif lang == "en" then
  808.         mon.write("RF-Production: " .. (input.formatNumberComma(math.floor(rfGen))) .. " RF/t      ")
  809.     end
  810.  
  811.     --Reactor status (on/off)
  812.     mon.setCursorPos(2, 7)
  813.     if lang == "de" then
  814.         mon.write("Reaktor: ")
  815.         if r.getActive() then
  816.             mon.setTextColor(colors.green)
  817.             mon.write("an ")
  818.         end
  819.         if not r.getActive() then
  820.             mon.setTextColor(colors.red)
  821.             mon.write("aus")
  822.         end
  823.     elseif lang == "en" then
  824.         mon.write("Reactor: ")
  825.         if r.getActive() then
  826.             mon.setTextColor(colors.green)
  827.             mon.write("on ")
  828.         end
  829.         if not r.getActive() then
  830.             mon.setTextColor(colors.red)
  831.             mon.write("off")
  832.         end
  833.     end
  834.  
  835.     --Prints all other informations (fuel consumption,steam,turbine amount,mode)
  836.     mon.setTextColor(tonumber(textColor))
  837.     mon.setCursorPos(2, 9)
  838.     local fuelCons = tostring(r.getFuelConsumedLastTick())
  839.     local fuelCons2 = string.sub(fuelCons, 0, 4)
  840.     local eff = math.floor(rfGen / r.getFuelConsumedLastTick())
  841.     if not r.getActive() then eff = 0 end
  842.  
  843.     if lang == "de" then
  844.         mon.write("Reaktor-Verbrauch: " .. fuelCons2 .. "mb/t     ")
  845.         mon.setCursorPos(2, 10)
  846.         mon.write("Steam: " .. (input.formatNumber(math.floor(r.getHotFluidProducedLastTick()))) .. "mb/t    ")
  847.         mon.setCursorPos(2, 11)
  848.         mon.write("Effizienz: " .. (input.formatNumber(eff)) .. " RF/mb       ")
  849.         mon.setCursorPos(40, 2)
  850.         mon.write("Turbinen: " .. (amountTurbines + 1) .. "  ")
  851.         mon.setCursorPos(2, 13)
  852.         mon.write("-- Turbine " .. (turbine + 1) .. " --")
  853.     elseif lang == "en" then
  854.         mon.write("Fuel Consumption: " .. fuelCons2 .. "mb/t     ")
  855.         mon.setCursorPos(2, 10)
  856.         mon.write("Steam: " .. (input.formatNumberComma(math.floor(r.getHotFluidProducedLastTick()))) .. "mb/t    ")
  857.         mon.setCursorPos(2, 11)
  858.         mon.write("Efficiency: " .. (input.formatNumberComma(eff)) .. " RF/mb       ")
  859.         mon.setCursorPos(40, 2)
  860.         mon.write("Turbines: " .. (amountTurbines + 1) .. "  ")
  861.         mon.setCursorPos(2, 13)
  862.         mon.write("-- Turbine " .. (turbine + 1) .. " --")
  863.     end
  864.  
  865.     --Currently selected turbine details
  866.  
  867.     --coils
  868.     mon.setCursorPos(2, 14)
  869.     mon.write("Coils: ")
  870.  
  871.     if t[turbine].getInductorEngaged() then
  872.         mon.setTextColor(colors.green)
  873.         if lang == "de" then
  874.             mon.write("eingehaengt   ")
  875.         elseif lang == "en" then
  876.             mon.write("engaged     ")
  877.         end
  878.     end
  879.     if t[turbine].getInductorEngaged() == false then
  880.         mon.setTextColor(colors.red)
  881.         if lang == "de" then
  882.             mon.write("ausgehaengt   ")
  883.         elseif lang == "en" then
  884.             mon.write("disengaged")
  885.         end
  886.     end
  887.     mon.setTextColor(tonumber(textColor))
  888.  
  889.     --rotor speed/RF-production
  890.     mon.setCursorPos(2, 15)
  891.     if lang == "de" then
  892.         mon.write("Rotor Geschwindigkeit: ")
  893.         mon.write((input.formatNumber(math.floor(t[turbine].getRotorSpeed()))) .. " RPM   ")
  894.         mon.setCursorPos(2, 15)
  895.         mon.write("RF-Produktion: " .. (input.formatNumber(math.floor(t[turbine].getEnergyProducedLastTick()))) .. " RF/t           ")
  896.     elseif lang == "en" then
  897.         mon.write("Rotor Speed: ")
  898.         mon.write((input.formatNumberComma(math.floor(t[turbine].getRotorSpeed()))) .. " RPM    ")
  899.         mon.setCursorPos(2, 15)
  900.         mon.write("RF-Production: " .. (input.formatNumberComma(math.floor(t[turbine].getEnergyProducedLastTick()))) .. " RF/t           ")
  901.     end
  902.  
  903.     --Internal buffer of the turbine
  904.     mon.setCursorPos(2, 16)
  905.     if lang == "de" then
  906.         mon.write("Interne Energie: ")
  907.         mon.write(input.formatNumber(math.floor(getTurbineEnergy(turbine))) .. " RF          ")
  908.     elseif lang == "en" then
  909.         mon.write("Internal Energy: ")
  910.         mon.write(input.formatNumberComma(math.floor(getTurbineEnergy(turbine))) .. " RF          ")
  911.     end
  912.  
  913.     --prints the current program version
  914.     mon.setCursorPos(2, 25)
  915.     mon.write("Version " .. version)
  916.  
  917.     --refreshes the last turbine id
  918.     lastStat = turbine
  919. end
  920.  
  921. --printStats (manual)
  922. function printStatsMan(turbine)
  923.     --refresh current turbine
  924.     currStat = turbine
  925.  
  926.     --toggles turbine buttons if pressed (old button off, new button on)
  927.     if not page.buttonList["#" .. currStat + 1].active then
  928.         page:toggleButton("#" .. currStat + 1)
  929.     end
  930.     if currStat ~= lastStat then
  931.         if page.buttonList["#" .. lastStat + 1].active then
  932.             page:toggleButton("#" .. lastStat + 1)
  933.         end
  934.     end
  935.  
  936.     --On/Off buttons
  937.     if t[currStat].getActive() and not page.buttonList["turbineOn"].active then
  938.         page:rename("turbineOn", tOn, true)
  939.         page:toggleButton("turbineOn")
  940.     end
  941.     if not t[currStat].getActive() and page.buttonList["turbineOn"].active then
  942.         page:rename("turbineOn", tOff, true)
  943.         page:toggleButton("turbineOn")
  944.     end
  945.     if t[currStat].getInductorEngaged() and not page.buttonList["coilsOn"].active then
  946.         page:rename("coilsOn", cOn, true)
  947.         page:toggleButton("coilsOn")
  948.     end
  949.     if not t[currStat].getInductorEngaged() and page.buttonList["coilsOn"].active then
  950.         page:rename("coilsOn", cOff, true)
  951.         page:toggleButton("coilsOn")
  952.     end
  953.  
  954.     --prints the energy level (in %)
  955.     mon.setBackgroundColor(tonumber(backgroundColor))
  956.     mon.setTextColor(tonumber(textColor))
  957.  
  958.     mon.setCursorPos(2, 2)
  959.     if lang == "de" then
  960.         mon.write("Energie: " .. getEnergyPer() .. "%  ")
  961.     elseif lang == "en" then
  962.         mon.write("Energy: " .. getEnergyPer() .. "%  ")
  963.     end
  964.  
  965.     --prints the energy bar
  966.     local part1 = getEnergyPer() / 5
  967.     mon.setCursorPos(2, 3)
  968.     mon.setBackgroundColor(colors.lightGray)
  969.     mon.write("                    ")
  970.     mon.setBackgroundColor(colors.green)
  971.     mon.setCursorPos(2, 3)
  972.     for i = 1, part1 do
  973.         mon.write(" ")
  974.     end
  975.     mon.setTextColor(textColor)
  976.  
  977.     --prints the overall energy production
  978.     local rfGen = 0
  979.     for i = 0, amountTurbines, 1 do
  980.         rfGen = rfGen + t[i].getEnergyProducedLastTick()
  981.     end
  982.  
  983.     mon.setBackgroundColor(tonumber(backgroundColor))
  984.  
  985.     --Other status informations
  986.     if lang == "de" then
  987.         mon.setCursorPos(2, 5)
  988.         mon.write("RF-Produktion: " .. (input.formatNumber(math.floor(rfGen))) .. " RF/t        ")
  989.         mon.setCursorPos(2, 7)
  990.         local fuelCons = tostring(r.getFuelConsumedLastTick())
  991.         local fuelCons2 = string.sub(fuelCons, 0, 4)
  992.         mon.write("Reaktor-Verbrauch: " .. fuelCons2 .. "mb/t     ")
  993.         mon.setCursorPos(2, 9)
  994.         mon.write("Rotor Geschwindigkeit: ")
  995.         mon.write((input.formatNumber(math.floor(t[turbine].getRotorSpeed()))) .. " RPM   ")
  996.         mon.setCursorPos(2, 11)
  997.         mon.write("Reaktor: ")
  998.         mon.setCursorPos(2, 13)
  999.         mon.write("Aktuelle Turbine: ")
  1000.         mon.setCursorPos(2, 17)
  1001.         mon.write("Alle Turbinen: ")
  1002.     elseif lang == "en" then
  1003.         mon.setCursorPos(2, 5)
  1004.         mon.write("RF-Production: " .. (input.formatNumberComma(math.floor(rfGen))) .. " RF/t      ")
  1005.         mon.setCursorPos(2, 7)
  1006.         local fuelCons = tostring(r.getFuelConsumedLastTick())
  1007.         local fuelCons2 = string.sub(fuelCons, 0, 4)
  1008.         mon.write("Fuel Consumption: " .. fuelCons2 .. "mb/t     ")
  1009.         mon.setCursorPos(2, 9)
  1010.         mon.write("Rotor Speed: ")
  1011.         mon.write((input.formatNumberComma(math.floor(t[turbine].getRotorSpeed()))) .. " RPM     ")
  1012.         mon.setCursorPos(2, 11)
  1013.         mon.write("Reactor: ")
  1014.         mon.setCursorPos(2, 13)
  1015.         mon.write("Current Turbine: ")
  1016.         mon.setCursorPos(2, 17)
  1017.         mon.write("All Turbines: ")
  1018.     end
  1019.     mon.setCursorPos(2, 15)
  1020.     mon.write("Coils: ")
  1021.  
  1022.     mon.setCursorPos(40, 2)
  1023.     if lang == "de" then
  1024.         mon.write("Turbinen: " .. (amountTurbines + 1) .. "  ")
  1025.     elseif lang == "en" then
  1026.         mon.write("Turbines: " .. (amountTurbines + 1) .. "  ")
  1027.     end
  1028.  
  1029.  
  1030.     --prints the current program version
  1031.     mon.setCursorPos(2, 25)
  1032.     mon.write("Version " .. version)
  1033.  
  1034.     --refreshes the last turbine id
  1035.     lastStat = turbine
  1036. end
  1037.  
  1038. --program start
  1039. if overallMode == "auto" then
  1040.     startAutoMode()
  1041. elseif overallMode == "manual" then
  1042.     startManualMode()
  1043. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement