Beispiel #1
0
        public void UpdateAvailableResource(string resourceName, double changeAmount)
        {
            if (changeAmount == 0)
            {
                return;
            }

            AvailableResources.TryGetValue(resourceName, out double availableAmount);
            AvailableResources[resourceName] = availableAmount + changeAmount;
        }
Beispiel #2
0
        public void UpdateUnloadedVesselData()
        {
            // clear reference dictionaries
            foreach (var vesselDataResourceChange in ResourceChanges)
            {
                vesselDataResourceChange.Value.Change = 0;
            }

            // clear value dictionaries
            AvailableResources.Clear();
            MaxAmountResources.Clear();
            AvailableStorage.Clear();

            Position            = Vessel.GetWorldPos3D();
            Orbit               = Vessel.GetOrbit();
            OrbitalVelocityAtUt = Orbit.getOrbitalVelocityAtUT(Planetarium.GetUniversalTime()).xzy;

            // calculate heading vector once per frame per vessel
            HeadingVector = EngineBackgroundProcessing.GetHeadingVectorForAutoPilot(vesselData: this, UT: Planetarium.GetUniversalTime());

            if (HeadingVector != Vector3d.zero && DeltaTime != 0)
            {
                // apply Perturb once per frame per vessel
                AccelerationVector = Vector3d.zero;
                foreach (KeyValuePair <uint, PersistentEngineData> engineData in Engines)
                {
                    PersistentEngineData persistentEngineData = engineData.Value;

                    persistentEngineData.DeltaVVector = persistentEngineData.DeltaV * HeadingVector.normalized * persistentEngineData.PersistentEngine.cosine;
                    AccelerationVector += persistentEngineData.DeltaVVector / DeltaTime;
                }

                Orbit.Perturb(AccelerationVector * TimeWarp.fixedDeltaTime, Planetarium.GetUniversalTime());
            }

            // calculate vessel mass and total resource amounts
            TotalVesselMassInTon = 0;
            foreach (ProtoPartSnapshot protoPartSnapshot in Vessel.protoVessel.protoPartSnapshots)
            {
                TotalVesselMassInTon += protoPartSnapshot.mass;
                foreach (ProtoPartResourceSnapshot protoPartResourceSnapshot in protoPartSnapshot.resources)
                {
                    if (protoPartResourceSnapshot.definition == null)
                    {
                        continue;
                    }

                    TotalVesselMassInTon += protoPartResourceSnapshot.amount * protoPartResourceSnapshot.definition.density;

                    MaxAmountResources.TryGetValue(protoPartResourceSnapshot.resourceName, out double maxAmount);
                    MaxAmountResources[protoPartResourceSnapshot.resourceName] = maxAmount + protoPartResourceSnapshot.maxAmount;

                    UpdateAvailableResource(protoPartResourceSnapshot.resourceName, Math.Min(protoPartResourceSnapshot.maxAmount, protoPartResourceSnapshot.amount));
                }
            }
            TotalVesselMassInKg = TotalVesselMassInTon * 1000;

            // calculate storage room for resources
            foreach (KeyValuePair <string, double> availableResource in AvailableResources)
            {
                AvailableResources.TryGetValue(availableResource.Key, out double availableAmount);
                MaxAmountResources.TryGetValue(availableResource.Key, out double maxAmount);
                AvailableStorage[availableResource.Key] = maxAmount - availableAmount;
            }
        }