public static void launch(this Vessel vessel)
        {
            vessel.AutoPilot.ReferenceFrame  = vessel.SurfaceReferenceFrame;
            vessel.AutoPilot.TargetDirection = new Tuple <double, double, double>(1, 0, 0);
            vessel.AutoPilot.Engage();
            vessel.Accelerate(1.3F);
            vessel.Control.ActivateNextStage();
            while (vessel.Orbit.ApoapsisAltitude < 80000)
            {
                double turnoverPercentage = Math.Min(vessel.Flight().SurfaceAltitude / 40000.0, 1.0F);
                vessel.AutoPilot.TargetDirection =
                    new Tuple <double, double, double>(1.0 - turnoverPercentage, 0, turnoverPercentage);

                if (vessel.Parts.Engines.Any(e => !(e.Part.RCS == null) && !e.HasFuel))
                {
                    vessel.Control.ActivateNextStage();
                }
            }

            vessel.Control.Throttle = 0;

            while (true)
            {
                vessel.AutoPilot.TargetDirection = new Tuple <double, double, double>(0, 0, 1);
                var oribitalVelocity = Math.Sqrt(vessel.Orbit.Body.GravitationalParameter / vessel.Orbit.Apoapsis);
                var neededSpeed      = oribitalVelocity - vessel.Flight(vessel.OrbitalReferenceFrame).HorizontalSpeed;
                var accelerationTime = (neededSpeed / 2) / (vessel.AvailableThrust / vessel.Mass);
                var timeToBurn       = vessel.Orbit.TimeToApoapsis - accelerationTime;
                Console.WriteLine("Time to burn: {0}", timeToBurn);
                if (timeToBurn <= 0)
                {
                    while (vessel.Orbit.PeriapsisAltitude < 70000)
                    {
                        vessel.Control.Throttle = 1.0F;
                        System.Threading.Thread.Sleep(10);
                    }
                    break;
                }
            }
            vessel.Accelerate(0);
        }
 public void Deactivate()
 {
     _activated = false;
     _vessel.Accelerate(0);
 }
        public static void Land(this Vessel vessel)
        {
            var srfFlight = vessel.Flight(vessel.Orbit.Body.ReferenceFrame);

            while (srfFlight.VerticalSpeed > -2)
            {
                System.Threading.Thread.Sleep(100);
            }

            vessel.Control.SAS    = true;
            vessel.Control.Gear   = true;
            vessel.Control.Brakes = vessel.Orbit.Body.HasAtmosphere;

            while (!(vessel.Control.SASMode == SASMode.Retrograde && vessel.Control.RCS))
            {
                vessel.Control.SASMode = SASMode.Retrograde;
                vessel.Control.RCS     = true;
            }

            bool shouldThrust = false;

            double comOffset        = Math.Abs(vessel.BoundingBox(vessel.ReferenceFrame).Item1.Item2);
            double distanceToGround = vessel.Flight().SurfaceAltitude - comOffset;
            double srfGrav          = vessel.Orbit.Body.SurfaceGravity;

            while (distanceToGround > 0.5)
            {
                double initialSpeedSquared = Math.Pow(srfFlight.Speed, 2);
                float  acc = (float)(((3 + initialSpeedSquared) / (2 * distanceToGround)) + srfGrav);

                float thrustRequired = vessel.Mass * acc;
                float throttle       = thrustRequired / (vessel.AvailableThrust);

                if (throttle > 0.98)
                {
                    shouldThrust = true;
                }
                if (throttle < 0.1)
                {
                    shouldThrust = false;
                }

                if (shouldThrust)
                {
                    vessel.Accelerate(acc);
                }
                vessel.Control.SASMode =
                    srfFlight.Speed < 20 ?
                    SASMode.StabilityAssist : SASMode.Retrograde;

                comOffset        = Math.Abs(vessel.BoundingBox(vessel.ReferenceFrame).Item1.Item2);
                distanceToGround = vessel.Flight().SurfaceAltitude - comOffset;
            }

            while (!(vessel.Situation == VesselSituation.Landed ||
                     vessel.Situation == VesselSituation.Splashed))
            {
                vessel.Accelerate(1);
            }
            vessel.Accelerate(0);
        }