Пример #1
0
        private static Pawn GeneratePawnInternal(PawnGenerationRequest request)
        {
            request.EnsureNonNullFaction();
            Pawn pawn = null;

            if (!request.Newborn && !request.ForceGenerateNewPawn)
            {
                if (request.ForceRedressWorldPawnIfFormerColonist)
                {
                    IEnumerable <Pawn> validCandidatesToRedress = PawnGenerator.GetValidCandidatesToRedress(request);
                    if (validCandidatesToRedress.Where(new Func <Pawn, bool>(PawnUtility.EverBeenColonistOrTameAnimal)).TryRandomElementByWeight(new Func <Pawn, float>(PawnGenerator.WorldPawnSelectionWeight), out pawn))
                    {
                        PawnGenerator.RedressPawn(pawn, request);
                        Find.WorldPawns.RemovePawn(pawn);
                    }
                }
                if (pawn == null && request.Inhabitant && request.Tile != -1)
                {
                    Settlement settlement = Find.WorldObjects.WorldObjectAt <Settlement>(request.Tile);
                    if (settlement != null && settlement.previouslyGeneratedInhabitants.Any <Pawn>())
                    {
                        IEnumerable <Pawn> validCandidatesToRedress2 = PawnGenerator.GetValidCandidatesToRedress(request);
                        if ((from x in validCandidatesToRedress2
                             where settlement.previouslyGeneratedInhabitants.Contains(x)
                             select x).TryRandomElementByWeight(new Func <Pawn, float>(PawnGenerator.WorldPawnSelectionWeight), out pawn))
                        {
                            PawnGenerator.RedressPawn(pawn, request);
                            Find.WorldPawns.RemovePawn(pawn);
                        }
                    }
                }
                if (pawn == null && Rand.Chance(PawnGenerator.ChanceToRedressAnyWorldPawn(request)))
                {
                    IEnumerable <Pawn> validCandidatesToRedress3 = PawnGenerator.GetValidCandidatesToRedress(request);
                    if (validCandidatesToRedress3.TryRandomElementByWeight(new Func <Pawn, float>(PawnGenerator.WorldPawnSelectionWeight), out pawn))
                    {
                        PawnGenerator.RedressPawn(pawn, request);
                        Find.WorldPawns.RemovePawn(pawn);
                    }
                }
            }
            if (pawn == null)
            {
                pawn = PawnGenerator.GenerateNewNakedPawn(ref request);
                if (pawn == null)
                {
                    return(null);
                }
                if (!request.Newborn)
                {
                    PawnGenerator.GenerateGearFor(pawn, request);
                }
                if (request.Inhabitant && request.Tile != -1)
                {
                    Settlement settlement2 = Find.WorldObjects.WorldObjectAt <Settlement>(request.Tile);
                    if (settlement2 != null)
                    {
                        settlement2.previouslyGeneratedInhabitants.Add(pawn);
                    }
                }
            }
            if (Find.Scenario != null)
            {
                Find.Scenario.Notify_PawnGenerated(pawn, request.Context);
            }
            return(pawn);
        }