internal void OnBeforeSave()
        {
            if (FlightGlobals.ActiveVessel == null)
            {
                return;
            }
            var         state  = FlightGlobals.ActiveVessel.state;
            LaunchEvent launch = GetLaunchByVesselId(FlightGlobals.ActiveVessel.id.ToString());

            if (launch != null)
            {
                RecordMaxSpeed();
                RecordMaxGee();
            }
            if (launch != null && state == Vessel.State.DEAD)
            {
                if (launch.GetLastEvent() is EndFlightEvent)
                {
                    return;
                }
                VesselDestroyedEvent destroyed = new VesselDestroyedEvent();
                launch.AddEvent(destroyed);

                EndFlightEvent endFlight = new EndFlightEvent();
                endFlight.finalMass   = 0;
                endFlight.crewMembers = new List <string>();
                launch.AddEvent(endFlight);
            }
        }
        internal void UpdateDynamic()
        {
            if (!HighLogic.LoadedSceneIsFlight)
            {
                return;
            }
            if (FlightGlobals.ActiveVessel == null)
            {
                return;
            }
            LaunchEvent activeLaunch = GetLaunch(FlightGlobals.ActiveVessel);

            if (activeLaunch != null)
            {
                ProtoVessel item = FlightGlobals.ActiveVessel.protoVessel;
                double      currentVesselSpeed = item.vesselRef.obt_speed;
                if (item.situation == Vessel.Situations.FLYING || item.situation == Vessel.Situations.PRELAUNCH)
                {
                    currentVesselSpeed = item.vesselRef.srfSpeed;
                }
                if (currentVesselSpeed > activeLaunch.maxSpeed)
                {
                    activeLaunch.maxSpeed = currentVesselSpeed;
                }

                if (activeLaunch.maxGee < FlightGlobals.ActiveVessel.geeForce)
                {
                    activeLaunch.maxGee = FlightGlobals.ActiveVessel.geeForce;
                }
                foreach (ProtoCrewMember kerbal in FlightGlobals.ActiveVessel.GetVesselCrew())
                {
                    LaunchCrewEvent crewLaunch = GetKerbalLaunch(kerbal.name);
                    if (crewLaunch.maxGee < FlightGlobals.ActiveVessel.geeForce)
                    {
                        crewLaunch.maxGee = FlightGlobals.ActiveVessel.geeForce;
                    }
                }

                if (activeLaunch.GetLastEvent() is LandingEvent)
                {
                    double altitude = FlightGlobals.ActiveVessel.RevealAltitude();
                    if (altitude - FlightGlobals.ActiveVessel.terrainAltitude > 10)
                    {
                        activeLaunch.AddEvent(new FlyingEvent());
                    }
                }
                float currentMass = 0;
                foreach (var part in FlightGlobals.ActiveVessel.parts)
                {
                    currentMass += part.GetResourceMass() + part.mass;
                }
                activeLaunch.currentMass = currentMass;
            }
        }
        internal void OnEndFlight(ProtoVessel protoVessel, bool data1)
        {
            LaunchEvent launch = GetLaunchByVesselId(protoVessel.vesselID.ToString());

            if (launch == null || launch.GetLastEvent() is EndFlightEvent)
            {
                return;
            }
            EndFlightEvent endFlight = new EndFlightEvent();

            endFlight.finalMass = 0;
            foreach (var part in protoVessel.protoPartSnapshots)
            {
                endFlight.finalMass += part.mass;
            }
            endFlight.crewMembers = new List <string>();
            foreach (var kerbal in protoVessel.GetVesselCrew())
            {
                endFlight.crewMembers.Add(kerbal.name);
            }
            launch.AddEvent(endFlight);
            FlightGUI.SaveData();
        }
        public void RecordLanding(Vessel vessel)
        {
            if (vessel.isEVA)
            {
                return;
            }
            LandingEvent landing = new LandingEvent();

            landing.mainBodyName = vessel.mainBody.name;
            landing.biome        = getBiomeName(vessel.mainBody, vessel.longitude, vessel.latitude);
            LaunchEvent launch = GetLaunch(vessel);

            if (launch != null)
            {
                FlightEvent flightEvent = launch.GetLastEvent();
                if (flightEvent is EvaEvent && (landing.time - flightEvent.time) / TimeSpan.TicksPerSecond < 1)
                {
                    return;
                }
                if (flightEvent is LandingEvent)
                {
                    if (((landing.time - flightEvent.time) / TimeSpan.TicksPerSecond < 2))
                    {
                        return;
                    }
                }
                launch.AddEvent(landing);
            }


            foreach (ProtoCrewMember kerbal in vessel.GetVesselCrew())
            {
                LandingCrewEvent landingCrewEvent = new LandingCrewEvent();
                LaunchCrewEvent  crewLaunch       = GetKerbalLaunch(kerbal.name);
                crewLaunch.AddEvent(landingCrewEvent);
            }
        }
        private void DrawFlightWindow(int id)
        {
            if (GUI.Button(new Rect(flightWindowRect.width - 28, 2, 25, 16), "X"))
            {
                needToShowFlight = false;
            }


            List <LaunchEvent> launches = EventProcessor.Instance.launches.GetRange(0, EventProcessor.Instance.launches.Count);

            /*if (reverseFlights)
            *       launches.Reverse();*/
            LaunchEvent launch = launches[flightIdToShow];

            if (!(launch.IsMissionFinished() || launch.GetLastEvent() is EndFlightEvent))
            {
                if (GUI.Button(new Rect(5, flightWindowRect.height - 22, 100, 20), "Finish mission"))
                {
                    EventProcessor.Instance.OnFinishMission(launch);
                }
            }
            Rect     captionRect = new Rect(0, 2, flightWindowRect.width, 16);
            string   txt         = "Flight №" + launch.posInTable + " - " + launch.shipName;
            GUIStyle currentStyle;

            if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id.ToString() == launch.shipID)
            {
                currentStyle = activeStyle;
            }
            else if (launch.IsDestroyed())
            {
                currentStyle = destroyedStyle;
            }
            else if (launch.GetEndDate() != -1)
            {
                currentStyle = endedStyle;
            }
            else
            {
                currentStyle = proceedingStyle;
            }
            currentStyle.fontStyle = FontStyle.Bold;
            currentStyle.alignment = TextAnchor.MiddleCenter;
            GUI.Label(captionRect, txt, currentStyle);
            currentStyle.fontStyle = FontStyle.Normal;
            currentStyle.alignment = TextAnchor.MiddleLeft;



            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical(GUILayout.Width(100));
            GUILayout.Label("Vessel", boldtext);
            GUILayout.Label("Launch date", boldtext);
            GUILayout.Label("Launch mass", boldtext);
            GUILayout.Label("Cost", boldtext);
            hintContent.text    = "Close Orbit/SOI masses";
            hintContent.tooltip = "Masses";
            GUILayout.Label(hintContent, boldtext);
            GUILayout.Label("Docked", boldtext);
            GUILayout.Label("EVAs", boldtext);
            GUILayout.Label("Max speed", boldtext);
            GUILayout.Label("Max Gee", boldtext);
            hintContent.text    = "Landings";
            hintContent.tooltip = "Biomes";
            GUILayout.Label(hintContent, boldtext);
            GUILayout.Label("End date", boldtext);
            GUILayout.Label("Duration", boldtext);
            GUILayout.Label("Final mass", boldtext);
            hintContent.text    = "Science points";
            hintContent.tooltip = "Science points";
            GUILayout.Label(hintContent, boldtext);
            GUILayout.EndVertical();

            GUILayout.BeginVertical(GUILayout.Width(400));
            GUILayout.Label(launch.shipName);
            GUILayout.Label(Utils.TicksToDate(launch.time));
            GUILayout.Label(Utils.CorrectNumber(launch.launchMass.ToString(), 3));
            GUILayout.Label(Utils.CorrectNumber(launch.launchCost.ToString()));
            float  massOnOrbit     = launch.GetMassOnOrbit();
            string massOnOrbitText = "-";

            if (massOnOrbit > 0)
            {
                massOnOrbitText = massOnOrbit.ToString();
            }
            hintContent.text    = Utils.CorrectNumber(massOnOrbitText);
            hintContent.tooltip = "Masses";
            GUILayout.Label(hintContent);
            GUILayout.Label(launch.GetDockings().ToString());
            GUILayout.Label(launch.GetEvas().ToString());
            GUILayout.Label(Utils.CorrectNumber(launch.maxSpeed.ToString()) + "m/s");
            GUILayout.Label(Utils.CorrectNumber(launch.maxGee.ToString()) + " G");
            hintContent.text    = launch.GetLandingsCount().ToString();
            hintContent.tooltip = "Biomes";
            GUILayout.Label(hintContent);

            long endDate = launch.GetEndDate();

            if (endDate != -1)
            {
                GUILayout.Label(Utils.TicksToDate(endDate));
                GUILayout.Label(Utils.TicksToTotalTime(launch.GetTotalFlightTime()));
            }
            else
            {
                GUILayout.Label("-");
                GUILayout.Label(Utils.TicksToTotalTime(launch.GetTotalFlightTime()));
            }
            GUILayout.Label(Utils.CorrectNumber(launch.GetFinalMass().ToString(), 3));
            float  sciencePoints     = launch.GetSciencePoints();
            string sciencePointsText = "-";

            if (sciencePoints > 0)
            {
                sciencePointsText = Utils.CorrectNumber(sciencePoints.ToString());
            }
            hintContent.text    = sciencePointsText;
            hintContent.tooltip = "Science points";
            GUILayout.Label(sciencePointsText);
            GUILayout.EndVertical();

            GUILayout.BeginVertical();
            GUILayout.Box("SOI changes (dates, SOI names)");
            scrollViewVector1 = GUILayout.BeginScrollView(scrollViewVector1, GUILayout.Height((flightWindowRect.height - 130) / 3));
            List <FlightEvent> sois = launch.GetSOIChanges().ToArray().ToList <FlightEvent>();

            sois.AddRange(launch.GetLandings());
            sois.Sort();
            foreach (FlightEvent record in sois)
            {
                if (record is SOIChangeEvent)
                {
                    GUILayout.Label(Utils.TicksToDate(record.time) + " \t " + (record as SOIChangeEvent).soiName.ToString());
                }
                else
                {
                    GUILayout.Label(Utils.TicksToDate(record.time) + " \t Land:" + (record as LandingEvent).mainBodyName.ToString()
                                    + ":" + (record as LandingEvent).biome.ToString());
                }
            }
            GUILayout.EndScrollView();
            GUILayout.Box("Initial crew");
            scrollViewVector2 = GUILayout.BeginScrollView(scrollViewVector2, GUILayout.Height((flightWindowRect.height - 130) / 3));
            foreach (string crewMember in launch.crewMembers)
            {
                GUILayout.Label(crewMember);
            }
            GUILayout.EndScrollView();
            GUILayout.Box("Final crew");
            scrollViewVector3 = GUILayout.BeginScrollView(scrollViewVector3, GUILayout.Height((flightWindowRect.height - 130) / 3));
            foreach (string crewMember in launch.GetFinalCrew())
            {
                GUILayout.Label(crewMember);
            }
            GUILayout.EndScrollView();

            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();

            ProcessPopup();
            GUI.DragWindow(new Rect(0, 0, 10000, 20));
        }