Ejemplo n.º 1
0
        public static void LoadRecordings(ConfigNode node)
        {
            FlightRecorder.flightRecordings.Clear();
            ConfigNode flightRecorderNode = node.GetNode("FlightRecorder");

            if (flightRecorderNode == null)
            {
                return;
            }

            foreach (ConfigNode flightRecordingNode in flightRecorderNode.GetNodes())
            {
                FlightRecorder.flightRecordings.Add(flightRecordingNode.name, FlightRecording.CreateFromConfigNode(flightRecordingNode));
            }

            FlightRecorder.CollectGarbage(); // Might not work as expected in KSP 1.2, so we added this also to the timer-function.
        }
Ejemplo n.º 2
0
        // Is called every second and keeps track of used parts during a flight-recording:
        public static void Timer()
        {
            try
            {
                // Maybe remove old, invalid running recordings:
                FlightRecorder.CollectGarbage();

                // Check if we are on an vessel which is recording a flight:
                Vessel vessel = FlightGlobals.ActiveVessel;
                if (!vessel)
                {
                    return;
                }
                FlightRecording recording = GetFlightRecording(vessel);
                if (recording == null)
                {
                    return;
                }

                if (vessel.id.ToString() != FlightRecorder.timerVesselId)
                {
                    // The vessel has changed, reset all variables from the last timer-tick:
                    FlightRecorder.timerVesselId = vessel.id.ToString();
                    FlightRecorder.timerPartResources.Clear();
                }

                // Check all parts, if something has changed which makes the part unusable for payload-deployments:
                foreach (Part part in vessel.parts)
                {
                    if (recording.usedPartIds.Contains(part.flightID.ToString()))
                    {
                        continue;                                                           // Already blocked
                    }
                    bool   blockThis = false;
                    string partId    = part.flightID.ToString();

                    // Check for running engines:
                    foreach (ModuleEngines engineModule in part.FindModulesImplementing <ModuleEngines>())
                    {
                        if (engineModule.GetCurrentThrust() > 0)
                        {
                            blockThis = true;
                        }
                    }
                    foreach (ModuleEnginesFX engineModule in part.FindModulesImplementing <ModuleEnginesFX>())
                    {
                        if (engineModule.GetCurrentThrust() > 0)
                        {
                            blockThis = true;
                        }
                    }

                    // Check for resource-consumption:
                    foreach (PartResource resource in part.Resources)
                    {
                        PartResourceDefinition resourceDefinition = null;
                        string resourceId = resource.resourceName.ToString();
                        if (!KSTS.resourceDictionary.TryGetValue(resourceId, out resourceDefinition))
                        {
                            continue;
                        }
                        if (resourceDefinition.density <= 0)
                        {
                            continue;                                  // We only care about resources with mass, skipping electricity and such.
                        }
                        if (!FlightRecorder.timerPartResources.ContainsKey(partId))
                        {
                            FlightRecorder.timerPartResources.Add(partId, new Dictionary <string, double>());
                        }
                        double lastAmount;
                        if (!FlightRecorder.timerPartResources[partId].TryGetValue(resourceId, out lastAmount))
                        {
                            FlightRecorder.timerPartResources[partId].Add(resourceId, resource.amount);
                        }
                        else
                        {
                            if (lastAmount != resource.amount)
                            {
                                blockThis = true;                                // The amount has changed relative to the last timer-tick.
                            }
                        }
                    }

                    if (blockThis)
                    {
                        Debug.Log("[KSTS] marking part " + part.name.ToString() + " (" + part.flightID.ToString() + ") as used");
                        recording.usedPartIds.Add(part.flightID.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("[KSTS] FlightRecoorder.Timer(): " + e.ToString());
            }
        }