コード例 #1
0
        public void FixedUpdate()
        {
            if (!HighLogic.LoadedSceneIsFlight || vessel == null || !vessel.loaded)
            {
                return;
            }

            if (vessel.isEVA)
            {
                CheckEVA(vessel);
                return;
            }

            if (_partCount != vessel.parts.Count)
            {
                if (_partCount > 0)
                {
                    _isStatusRefreshRequired = true;
                }
                _partCount = vessel.parts.Count;
            }

            if (_isDirty)
            {
                _isDirty = false;
                UpdateVesselInfo();
                UpdateStatus();
            }

            var now = Planetarium.GetUniversalTime();

            if (_currentCrewCount == 0)
            {
                VesselStatus.VesselName  = vessel.vesselName;
                VesselStatus.NumCrew     = vessel.GetCrewCount();
                VesselStatus.CrewCap     = vessel.GetCrewCapacity();
                VesselStatus.LastECCheck = now;
                VesselStatus.LastFeeding = now;
                VesselStatus.LastUpdate  = now;

                LifeSupportManager.Instance.TrackVessel(VesselStatus);
                LastUpdateTime = now;

                return;
            }

            try
            {
                bool isLongLoop = false;
                var  offKerbin  = !LifeSupportManager.IsOnKerbin(vessel);
                CheckVesselId();

                // Check our time
                double deltaTime = GetDeltaTime();
                bool   isCatchup = deltaTime / 2 > TimeWarp.fixedDeltaTime;

                if (deltaTime < ResourceUtilities.FLOAT_TOLERANCE * 10)
                {
                    return;
                }

                if (now >= _lastProcessingTime + _checkInterval)
                {
                    isLongLoop          = true;
                    _lastProcessingTime = now;
                }

                VesselStatus.LastUpdate = now;
                VesselStatus.VesselName = vessel.vesselName;
                VesselStatus.NumCrew    = vessel.GetCrewCount();
                VesselStatus.CrewCap    = vessel.GetCrewCapacity();

                if (isLongLoop)
                {
                    CheckForDeadKerbals();
                }

                if (_currentCrewCount > 0)
                {
                    //Guard clause
                    if (_crewPart == null)
                    {
                        UpdateVesselInfo();
                    }

                    //we will add a bit of a fudge factor for supplies
                    var tolerance = deltaTime / 2f;

                    //nom nom nom!
                    ConverterResults resultSupply = Converter.ProcessRecipe(deltaTime, SupplyRecipe, _crewPart, null, 1f);
                    ConverterResults resultEC     = Converter.ProcessRecipe(deltaTime, ECRecipe, _crewPart, null, 1f);

                    #region Long Loop - Crew
                    if (isLongLoop)
                    {
                        //Ensure status is current
                        UpdateStatus();

                        var habTime = LifeSupportManager.GetTotalHabTime(VesselStatus, vessel);

                        if (_oldHabChecksum < ResourceUtilities.FLOAT_TOLERANCE)
                        {
                            _oldHabChecksum = LifeSupportManager.GetHabChecksum(VesselStatus, vessel);
                        }

                        var newHabChecksum = LifeSupportManager.GetHabChecksum(VesselStatus, vessel);
                        if (Math.Abs(_oldHabChecksum - newHabChecksum) > ResourceUtilities.FLOAT_TOLERANCE)
                        {
                            Debug.Log("[USI-LS] Vessel situation changed, refreshing life support");
                            _isStatusRefreshRequired = true;
                            _oldHabChecksum          = newHabChecksum;
                        }

                        var crewRoster = vessel.GetVesselCrew();
                        var count      = crewRoster.Count;
                        for (int i = 0; i < count; ++i)
                        {
                            var  crewMember        = crewRoster[i];
                            bool isGrouchyHab      = false;
                            bool isGrouchySupplies = false;
                            bool isGrouchyEC       = false;
                            bool isScout           = crewMember.HasEffect("ExplorerSkill") && habTime >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime;
                            bool isPermaHab        = habTime >= LifeSupportScenario.Instance.settings.GetSettings().PermaHabTime;
                            bool isHomeWorld       = CheckIfHomeWorld() && habTime >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime&& vessel.LandedOrSplashed;

                            // Get the crew member's life support stats
                            var trackedKerbal = LifeSupportManager.Instance.FetchKerbal(crewMember);

                            // Update life support stats
                            if (_isStatusRefreshRequired)
                            {
                                trackedKerbal.TimeEnteredVessel = now;
                                _isStatusRefreshRequired        = false;
                                LifeSupportManager.Instance.TrackKerbal(trackedKerbal);
                            }

                            // Update Hab effects
                            if (!offKerbin || isScout || isHomeWorld || isPermaHab)
                            {
                                trackedKerbal.TimeEnteredVessel = now;
                                trackedKerbal.LastAtHome        = now;
                                trackedKerbal.MaxOffKerbinTime  = habTime + trackedKerbal.LastAtHome;
                            }
                            else
                            {
                                if (vessel.id.ToString() != trackedKerbal.CurrentVesselId)
                                {
                                    if (vessel.id.ToString() != trackedKerbal.PreviousVesselId)
                                    {
                                        trackedKerbal.TimeEnteredVessel = now;
                                    }

                                    trackedKerbal.PreviousVesselId = trackedKerbal.CurrentVesselId;
                                    trackedKerbal.CurrentVesselId  = vessel.id.ToString();
                                    LifeSupportManager.Instance.TrackKerbal(trackedKerbal);
                                }

                                isGrouchyHab = CheckHabSideEffects(trackedKerbal);
                            }

                            // Update Supplies effects
                            if (offKerbin && (deltaTime - resultSupply.TimeFactor > tolerance))
                            {
                                isGrouchySupplies = CheckSupplySideEffects(trackedKerbal);
                            }
                            else if (deltaTime >= ResourceUtilities.FLOAT_TOLERANCE)
                            {
                                //All is well
                                trackedKerbal.LastMeal   = LastUpdateTime;
                                VesselStatus.LastFeeding = LastUpdateTime;
                            }

                            // Update ElectricCharge effects
                            if (offKerbin && (deltaTime - resultEC.TimeFactor > tolerance))
                            {
                                isGrouchyEC = CheckECSideEffects(trackedKerbal);
                            }
                            else if (deltaTime >= ResourceUtilities.FLOAT_TOLERANCE)
                            {
                                //All is well
                                trackedKerbal.LastEC     = LastUpdateTime;
                                VesselStatus.LastECCheck = LastUpdateTime;
                            }

                            trackedKerbal.LastUpdate = now;

                            var isAnyGrouch = isGrouchyEC || isGrouchyHab || isGrouchySupplies;
                            if (isGrouchyEC && !isCatchup)
                            {
                                ApplyEffect(
                                    trackedKerbal,
                                    crewMember,
                                    LifeSupportManager.GetNoECEffect(trackedKerbal.KerbalName),
                                    "power loss");
                            }
                            else if (isGrouchySupplies && !isCatchup)
                            {
                                ApplyEffect(
                                    trackedKerbal,
                                    crewMember,
                                    LifeSupportManager.GetNoSupplyEffect(trackedKerbal.KerbalName),
                                    "lack of supplies");
                            }
                            else if (isGrouchyHab && !isCatchup)
                            {
                                ApplyEffect(
                                    trackedKerbal,
                                    crewMember,
                                    LifeSupportManager.GetNoHomeEffect(trackedKerbal.KerbalName),
                                    "homesickness");
                            }
                            else if (crewMember.experienceTrait.Config.Name != trackedKerbal.OldTrait && !isAnyGrouch)
                            {
                                RemoveGrouchiness(crewMember, trackedKerbal);
                            }

                            LifeSupportManager.Instance.TrackKerbal(trackedKerbal);
                        }
                    }
                    #endregion - Crew

                    var remainingSupplies = ResourceBroker.AmountAvailable(
                        _crewPart,
                        "Supplies",
                        deltaTime,
                        ResourceFlowMode.ALL_VESSEL);
                    var remainingBattery = ResourceBroker.AmountAvailable(
                        _crewPart,
                        "ElectricCharge",
                        deltaTime,
                        ResourceFlowMode.ALL_VESSEL);
                    var suppliesConsumption    = LifeSupportScenario.Instance.settings.GetSettings().SupplyAmount;
                    var electricityConsumption = LifeSupportScenario.Instance.settings.GetSettings().ECAmount;

                    VesselStatus.SuppliesLeft = remainingSupplies / suppliesConsumption / _currentCrewCount / VesselStatus.RecyclerMultiplier;
                    VesselStatus.ECLeft       = remainingBattery / electricityConsumption / _currentCrewCount;
                }
                else
                {
                    VesselStatus.LastECCheck = now;
                    VesselStatus.LastFeeding = now;
                    VesselStatus.LastUpdate  = now;
                }

                LifeSupportManager.Instance.TrackVessel(VesselStatus);
            }
            catch (Exception ex)
            {
                print(string.Format("ERROR {0} IN ModuleLifeSupport", ex.Message));
            }
        }
コード例 #2
0
        public void FixedUpdate()
        {
            if (!HighLogic.LoadedSceneIsFlight || vessel == null)
            {
                return;
            }
            if (!vessel.loaded)
            {
                return;
            }
            if (vessel.isEVA)
            {
                CheckEVA(vessel);
                return;
            }

            if (_partCount != vessel.parts.Count)
            {
                if (_partCount > 0)
                {
                    refreshVesselTime = true;
                }
                _partCount = vessel.parts.Count;
            }


            if (isDirty)
            {
                isDirty = false;
                UpdateVesselInfo();
                UpdateStatus();
            }

            if (_currentCrew == 0)
            {
                VesselStatus.VesselName  = vessel.vesselName;
                VesselStatus.NumCrew     = vessel.GetCrewCount();
                VesselStatus.CrewCap     = vessel.GetCrewCapacity();
                VesselStatus.LastECCheck = Planetarium.GetUniversalTime();
                VesselStatus.LastFeeding = Planetarium.GetUniversalTime();
                VesselStatus.LastUpdate  = Planetarium.GetUniversalTime();
                LifeSupportManager.Instance.TrackVessel(VesselStatus);
                LastUpdateTime = Planetarium.GetUniversalTime();
                return;
            }

            try
            {
                bool isLongLoop = false;
                var  offKerbin  = !LifeSupportManager.IsOnKerbin(vessel);
                CheckVesselId();

                //Check our time
                double deltaTime = GetDeltaTime();
                bool   isCatchup = deltaTime / 2 > TimeWarp.fixedDeltaTime;

                if (deltaTime < ResourceUtilities.FLOAT_TOLERANCE * 10)
                {
                    return;
                }

                if (Planetarium.GetUniversalTime() >= _lastProcessingTime + _checkInterval)
                {
                    isLongLoop          = true;
                    _lastProcessingTime = Planetarium.GetUniversalTime();
                }

                VesselStatus.LastUpdate = Planetarium.GetUniversalTime();
                VesselStatus.VesselName = vessel.vesselName;
                VesselStatus.NumCrew    = vessel.GetCrewCount();
                VesselStatus.CrewCap    = vessel.GetCrewCapacity();
                if (isLongLoop)
                {
                    CheckForDeadKerbals();
                }

                if (_currentCrew > 0)
                {
                    //Guard clause
                    if (_crewPart == null)
                    {
                        UpdateVesselInfo();
                    }

                    //we will add a bit of a fudge factor for supplies
                    var tolerance = deltaTime / 2f;
                    //nom nom nom!
                    ConverterResults resultSupply = ResConverter.ProcessRecipe(deltaTime, SupplyRecipe, _crewPart, null,
                                                                               1f);
                    ConverterResults resultEC = ResConverter.ProcessRecipe(deltaTime, ECRecipe, _crewPart, null, 1f);

                    #region Long Loop - Crew
                    if (isLongLoop)
                    {
                        //Ensure status is current
                        UpdateStatus();
                        var vCrew   = vessel.GetVesselCrew();
                        var count   = vCrew.Count;
                        var habTime = LifeSupportManager.GetTotalHabTime(VesselStatus, vessel);

                        if (oldHabChecksum < ResourceUtilities.FLOAT_TOLERANCE)
                        {
                            oldHabChecksum = LifeSupportManager.GetHabChecksum(VesselStatus, vessel);
                        }

                        var newHabChecksum = LifeSupportManager.GetHabChecksum(VesselStatus, vessel);
                        if (Math.Abs(oldHabChecksum - newHabChecksum) > ResourceUtilities.FLOAT_TOLERANCE)
                        {
                            Debug.Log("Vessel situation changed, refreshing life support");
                            refreshVesselTime = true;
                            oldHabChecksum    = newHabChecksum;
                        }

                        for (int i = 0; i < count; ++i)
                        {
                            var  c                 = vCrew[i];
                            bool isGrouchyHab      = false;
                            bool isGrouchySupplies = false;
                            bool isGrouchyEC       = false;
                            bool isScout           = c.HasEffect("ExplorerSkill") && habTime >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime;
                            bool isPermaHab        = habTime >= LifeSupportScenario.Instance.settings.GetSettings().PermaHabTime;
                            bool isHomeWorld       = CheckIfHomeWorld() && habTime >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime&& vessel.LandedOrSplashed;
                            //Fetch them from the queue
                            var k = LifeSupportManager.Instance.FetchKerbal(c);
                            //Update our stuff
                            if (refreshVesselTime)
                            {
                                k.TimeEnteredVessel = Planetarium.GetUniversalTime();
                                refreshVesselTime   = false;
                                LifeSupportManager.Instance.TrackKerbal(k);
                            }
                            //First - Hab effects.
                            if (!offKerbin || isScout || isHomeWorld || isPermaHab)
                            {
                                k.LastAtHome        = Planetarium.GetUniversalTime();
                                k.MaxOffKerbinTime  = habTime + k.LastAtHome;
                                k.TimeEnteredVessel = Planetarium.GetUniversalTime();
                            }
                            else
                            {
                                if (vessel.id.ToString() != k.CurrentVesselId)
                                {
                                    if (vessel.id.ToString() != k.PreviousVesselId)
                                    {
                                        k.TimeEnteredVessel = Planetarium.GetUniversalTime();
                                    }

                                    k.PreviousVesselId = k.CurrentVesselId;
                                    k.CurrentVesselId  = vessel.id.ToString();
                                    LifeSupportManager.Instance.TrackKerbal(k);
                                }
                                isGrouchyHab = CheckHabSideEffects(k);
                            }


                            //Second - Supply
                            if (offKerbin && (deltaTime - resultSupply.TimeFactor > tolerance))
                            {
                                isGrouchySupplies = CheckSupplySideEffects(k);
                            }
                            else if (deltaTime >= ResourceUtilities.FLOAT_TOLERANCE)
                            {
                                //All is well
                                k.LastMeal = LastUpdateTime;
                                VesselStatus.LastFeeding = LastUpdateTime;
                            }

                            //Third - EC
                            if (offKerbin && (deltaTime - resultEC.TimeFactor > tolerance))
                            {
                                isGrouchyEC = CheckECSideEffects(k);
                            }
                            else if (deltaTime >= ResourceUtilities.FLOAT_TOLERANCE)
                            {
                                //All is well
                                k.LastEC = LastUpdateTime;
                                VesselStatus.LastECCheck = LastUpdateTime;
                            }


                            k.LastUpdate = Planetarium.GetUniversalTime();
                            var isAnyGrouch = isGrouchyEC || isGrouchyHab || isGrouchySupplies;

                            if (isGrouchyEC && !isCatchup)
                            {
                                ApplyEffect(k, c,
                                            LifeSupportManager.GetNoECEffect(k.KerbalName),
                                            "power loss");
                            }
                            else if (isGrouchySupplies && !isCatchup)
                            {
                                ApplyEffect(k, c,
                                            LifeSupportManager.GetNoSupplyEffect(k.KerbalName),
                                            "lack of supplies");
                            }
                            else if (isGrouchyHab && !isCatchup)
                            {
                                ApplyEffect(k, c,
                                            LifeSupportManager.GetNoHomeEffect(k.KerbalName),
                                            "homesickness");
                            }
                            else if (c.experienceTrait.Title != k.OldTrait && !isAnyGrouch)
                            {
                                RemoveGrouchiness(c, k);
                            }
                            LifeSupportManager.Instance.TrackKerbal(k);
                        }
                    }
                    #endregion - Crew

                    var supAmount = _resBroker.AmountAvailable(_crewPart, "Supplies", deltaTime,
                                                               ResourceFlowMode.ALL_VESSEL);
                    var ecAmount = _resBroker.AmountAvailable(_crewPart, "ElectricCharge", deltaTime,
                                                              ResourceFlowMode.ALL_VESSEL);
                    VesselStatus.SuppliesLeft = supAmount /
                                                LifeSupportScenario.Instance.settings.GetSettings().SupplyAmount /
                                                _currentCrew /
                                                VesselStatus.RecyclerMultiplier;
                    VesselStatus.ECLeft = ecAmount / LifeSupportScenario.Instance.settings.GetSettings().ECAmount /
                                          _currentCrew;
                }
                else
                {
                    VesselStatus.LastECCheck = Planetarium.GetUniversalTime();
                    VesselStatus.LastFeeding = Planetarium.GetUniversalTime();
                    VesselStatus.LastUpdate  = Planetarium.GetUniversalTime();
                }
                LifeSupportManager.Instance.TrackVessel(VesselStatus);
            }
            catch (Exception ex)
            {
                print(String.Format("ERROR {0} IN ModuleLifeSupport", ex.Message));
            }
        }