Ejemplo n.º 1
0
 public static void RedressPawn(Pawn pawn, PawnGenerationRequest request)
 {
     try
     {
         pawn.ChangeKind(request.KindDef);
         PawnGenerator.GenerateGearFor(pawn, request);
         if (pawn.Faction != request.Faction)
         {
             pawn.SetFaction(request.Faction, null);
         }
         if (pawn.guest != null)
         {
             pawn.guest.SetGuestStatus(null, false);
         }
     }
     finally
     {
     }
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private static Pawn TryGenerateNewPawnInternal(ref PawnGenerationRequest request, out string error, bool ignoreScenarioRequirements, bool ignoreValidator)
        {
            error = null;
            Pawn pawn = (Pawn)ThingMaker.MakeThing(request.KindDef.race, null);

            PawnGenerator.pawnsBeingGenerated.Add(new PawnGenerator.PawnGenerationStatus(pawn, null));
            Pawn result;

            try
            {
                pawn.kindDef = request.KindDef;
                pawn.SetFactionDirect(request.Faction);
                PawnComponentsUtility.CreateInitialComponents(pawn);
                if (request.FixedGender.HasValue)
                {
                    pawn.gender = request.FixedGender.Value;
                }
                else if (pawn.RaceProps.hasGenders)
                {
                    if (Rand.Value < 0.5f)
                    {
                        pawn.gender = Gender.Male;
                    }
                    else
                    {
                        pawn.gender = Gender.Female;
                    }
                }
                else
                {
                    pawn.gender = Gender.None;
                }
                PawnGenerator.GenerateRandomAge(pawn, request);
                pawn.needs.SetInitialLevels();
                if (!request.Newborn && request.CanGeneratePawnRelations)
                {
                    PawnGenerator.GeneratePawnRelations(pawn, ref request);
                }
                if (pawn.RaceProps.Humanlike)
                {
                    FactionDef def;
                    Faction    faction;
                    if (request.Faction != null)
                    {
                        def = request.Faction.def;
                    }
                    else if (Find.FactionManager.TryGetRandomNonColonyHumanlikeFaction(out faction, false, true, TechLevel.Undefined))
                    {
                        def = faction.def;
                    }
                    else
                    {
                        def = Faction.OfAncients.def;
                    }
                    pawn.story.melanin   = ((!request.FixedMelanin.HasValue) ? PawnSkinColors.RandomMelanin(request.Faction) : request.FixedMelanin.Value);
                    pawn.story.crownType = ((Rand.Value >= 0.5f) ? CrownType.Narrow : CrownType.Average);
                    pawn.story.hairColor = PawnHairColors.RandomHairColor(pawn.story.SkinColor, pawn.ageTracker.AgeBiologicalYears);
                    PawnBioAndNameGenerator.GiveAppropriateBioAndNameTo(pawn, request.FixedLastName, def);
                    pawn.story.hairDef = PawnHairChooser.RandomHairDefFor(pawn, def);
                    PawnGenerator.GenerateTraits(pawn, request);
                    PawnGenerator.GenerateBodyType(pawn);
                    PawnGenerator.GenerateSkills(pawn);
                }
                if (pawn.RaceProps.Animal && request.Faction != null && request.Faction.IsPlayer)
                {
                    pawn.training.SetWantedRecursive(TrainableDefOf.Tameness, true);
                    pawn.training.Train(TrainableDefOf.Tameness, null, true);
                }
                PawnGenerator.GenerateInitialHediffs(pawn, request);
                if (pawn.workSettings != null && request.Faction != null && request.Faction.IsPlayer)
                {
                    pawn.workSettings.EnableAndInitialize();
                }
                if (request.Faction != null && pawn.RaceProps.Animal)
                {
                    pawn.GenerateNecessaryName();
                }
                if (Find.Scenario != null)
                {
                    Find.Scenario.Notify_NewPawnGenerating(pawn, request.Context);
                }
                if (!request.AllowDead && (pawn.Dead || pawn.Destroyed))
                {
                    PawnGenerator.DiscardGeneratedPawn(pawn);
                    error  = "Generated dead pawn.";
                    result = null;
                }
                else if (!request.AllowDowned && pawn.Downed)
                {
                    PawnGenerator.DiscardGeneratedPawn(pawn);
                    error  = "Generated downed pawn.";
                    result = null;
                }
                else if (request.MustBeCapableOfViolence && ((pawn.story != null && pawn.story.WorkTagIsDisabled(WorkTags.Violent)) || (pawn.RaceProps.ToolUser && !pawn.health.capacities.CapableOf(PawnCapacityDefOf.Manipulation))))
                {
                    PawnGenerator.DiscardGeneratedPawn(pawn);
                    error  = "Generated pawn incapable of violence.";
                    result = null;
                }
                else if (!ignoreScenarioRequirements && request.Context == PawnGenerationContext.PlayerStarter && Find.Scenario != null && !Find.Scenario.AllowPlayerStartingPawn(pawn, false, request))
                {
                    PawnGenerator.DiscardGeneratedPawn(pawn);
                    error  = "Generated pawn doesn't meet scenario requirements.";
                    result = null;
                }
                else if (!ignoreValidator && request.ValidatorPreGear != null && !request.ValidatorPreGear(pawn))
                {
                    PawnGenerator.DiscardGeneratedPawn(pawn);
                    error  = "Generated pawn didn't pass validator check (pre-gear).";
                    result = null;
                }
                else
                {
                    if (!request.Newborn)
                    {
                        PawnGenerator.GenerateGearFor(pawn, request);
                    }
                    if (!ignoreValidator && request.ValidatorPostGear != null && !request.ValidatorPostGear(pawn))
                    {
                        PawnGenerator.DiscardGeneratedPawn(pawn);
                        error  = "Generated pawn didn't pass validator check (post-gear).";
                        result = null;
                    }
                    else
                    {
                        for (int i = 0; i < PawnGenerator.pawnsBeingGenerated.Count - 1; i++)
                        {
                            if (PawnGenerator.pawnsBeingGenerated[i].PawnsGeneratedInTheMeantime == null)
                            {
                                PawnGenerator.pawnsBeingGenerated[i] = new PawnGenerator.PawnGenerationStatus(PawnGenerator.pawnsBeingGenerated[i].Pawn, new List <Pawn>());
                            }
                            PawnGenerator.pawnsBeingGenerated[i].PawnsGeneratedInTheMeantime.Add(pawn);
                        }
                        result = pawn;
                    }
                }
            }
            finally
            {
                PawnGenerator.pawnsBeingGenerated.RemoveLast <PawnGenerator.PawnGenerationStatus>();
            }
            return(result);
        }