Advertisement
Guest User

turbineControl.lua

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