private void MoveKerbal(ICLSKerbal k)
        {
            // Debug.Log("MoveKerbal");
            // If there is only one seat in this space, then our friend is not going anywhere!
            ICLSPart  temppart = k.Part;
            ICLSSpace s        = temppart.Space;

            if (null == s)
            {
                Debug.LogWarning("space is null :(");
            }

            int maxCrew = s.MaxCrew;

            if (k.Part.Space.MaxCrew > 1)
            {
                //Debug.Log("vessel has more than 1 seat");

                List <PartSeat> listSeats = new List <PartSeat>();

                // There are other seats in the space that our chosen kerbal is in. Pick one of them at random, and then we can arrange the swap.
                foreach (ICLSPart p in k.Part.Space.Parts)
                {
                    for (int counter = 0; counter < p.Part.CrewCapacity; counter++)
                    {
                        //Debug.Log("Adding " + p.partInfo.title + " seat "+counter);
                        listSeats.Add(new PartSeat(p.Part, counter));
                    }
                }

                // Now we have a list of all the possible places we could move the kerbal to - choose one of them.

                int choosenLocation = this.random.Next(listSeats.Count);

                // TODO remove debugging
                //Debug.Log("choosen location: " + choosenLocation);

                // Do the swap
                bool kerbalsSwapped = SwapKerbals(k, listSeats[choosenLocation].part, listSeats[choosenLocation].seat);
            }
            else
            {
                //Debug.Log("Unable to move kerbal as there is only one seat in this vessel!");
            }
        }
        private void ChooseAndMoveAKerbal()
        {
            // Debug.Log("KHSAddon:ChooseAndMoveAKerbal");

            //Debug.Log("Before moving KerbalGUIManager.ActiveCrew.Count:" + KerbalGUIManager.ActiveCrew.Count);

            // If CLS is not installed then just bug out
            if (!CLSClient.CLSInstalled)
            {
                Debug.LogWarning("Not moving kerbals as the CLS mod is not installed");
                return;
            }

            ICLSVessel activeVessel = CLSClient.GetCLS().Vessel;

            if (activeVessel.Spaces.Count > 0)
            {
                List <ICLSSpace> crewedSpaces = new List <ICLSSpace>(activeVessel.Spaces.Count);

                foreach (ICLSSpace s in activeVessel.Spaces)
                {
                    if (s.Crew.Count > 0)
                    {
                        crewedSpaces.Add(s);
                    }
                }

                // now we have got a list of crewed spaces - pick one at random
                if (crewedSpaces.Count > 0)
                {
                    ICLSSpace pickedSpace = crewedSpaces[this.random.Next(crewedSpaces.Count)];

                    // Now pick one of the crew in that space at random!

                    ICLSKerbal pickedKerbel = pickedSpace.Crew[this.random.Next(pickedSpace.Crew.Count)];

                    // Move move the kerbal somewhere else
                    MoveKerbal(pickedKerbel);
                }
            }

            //Debug.Log("After moving KerbalGUIManager.ActiveCrew.Count:" + KerbalGUIManager.ActiveCrew.Count);
        }
        private bool SwapKerbals(ICLSKerbal k, Part targetPart, int targetSeatIdx)
        {
            //Debug.Log("SwapKerbals");

            if (k.Kerbal.seat.part == targetPart && k.Kerbal.seatIdx == targetSeatIdx)
            {
                // Nothing to do because the choosen target seat is where the kerbal is

                //Debug.Log("Attempted to move kerbal to the seat he is already sitting in!");

                return(false);
            }
            else
            {
                //Debug.Log("moving keral to somewhere other than his own seat");
                ProtoCrewMember sourceKerbal = k.Kerbal;
                // Find out if a kerbal is already sitting in the target seat
                // ProtoCrewMember targetKerbal = targetSeat.part.vessel.GetVesselCrew().Find(c => (c.seat.part == targetSeat.part) && (c.seatIdx == targetSeat.seat));

                Part            sourcePart    = k.Part.Part;
                ProtoCrewMember targetKerbal  = targetPart.internalModel.seats[targetSeatIdx].crew; // TODO I am unsure what will happen if a pod does not have an internal model.
                int             sourceSeatIdx = sourceKerbal.seatIdx;

                // TODO remove debugging

                /*
                 * {
                 *  Debug.Log("sourceKerbel: " + sourceKerbal.name);
                 *  Debug.Log("sourcePart:" + sourcePart.partInfo.title);
                 *  Debug.Log("sourceSeatIdx:" + sourceSeatIdx);
                 *
                 *  if (null == targetKerbal)
                 *  {
                 *      Debug.Log("targetKerbal: null");
                 *  }
                 *  else
                 *  {
                 *      Debug.Log("targetKerbal: " + targetKerbal.name);
                 *  }
                 *  Debug.Log("targetPart:" + targetPart.partInfo.title);
                 *  Debug.Log("targetSeatIdx:" + targetSeatIdx);
                 * }
                 */

                // Remove the kerbal(s) from their current seat(s)
                sourcePart.RemoveCrewmember(sourceKerbal);
                if (null != targetKerbal)
                {
                    targetPart.RemoveCrewmember(targetKerbal);
                }

                // Add the source kerbal to his new seat
                targetPart.AddCrewmemberAt(sourceKerbal, targetSeatIdx);
                if (null != sourceKerbal.seat)
                {
                    sourceKerbal.seat.SpawnCrew();
                }
                else
                {
                    Debug.LogError("sourceKerbal (" + sourceKerbal.name + ") does not have a seat!");
                }

                // Add the target kerbal to his new seat (if there is one)
                if (null != targetKerbal)
                {
                    sourcePart.AddCrewmemberAt(targetKerbal, sourceSeatIdx);
                    if (null != targetKerbal.seat)
                    {
                        targetKerbal.seat.SpawnCrew();
                    }
                    else
                    {
                        Debug.LogError("targetKerbal (" + targetKerbal.name + ") does not have a seat!");
                    }
                }

                // Fire the vessel change event.In particular CLS will pick up on this and rebuild its understanding of what is what.
                GameEvents.onVesselChange.Fire(FlightGlobals.ActiveVessel);
                //FlightGlobals.ActiveVessel.SpawnCrew(); // This does not seem to do the trick, so instead we set a flag and try to respawn on the next Update.
                this.refreshPortraitsPending = true;
                // Fire the Vessel Was Changed event in an attempt to update the GUI
                //GameEvents.onVesselWasModified.Fire(FlightGlobals.ActiveVessel);

                return(true);
            }
        }