예제 #1
0
        public void AddFactionPawn(FactionDef def)
        {
            var         kinds   = PrepareCarefully.Instance.Providers.Factions.GetPawnKindsForFactionDefLabel(def);
            PawnKindDef kindDef = kinds.RandomElementWithFallback(def.basicMemberKind);
            Faction     faction = PrepareCarefully.Instance.Providers.Factions.GetFaction(def);
            // Workaround to force pawn generation to skip adding weapons to the pawn.
            // Might be a slightly risky hack, but the finally block should guarantee that
            // the weapons money range always gets set back to its original value.
            // TODO: Try to remove this at a later date.  It would be nice if the pawn generation
            // request gave you an option to skip weapon and equipment generation.
            FloatRange savedWeaponsMoney = kindDef.weaponMoney;

            kindDef.weaponMoney = new FloatRange(0, 0);
            Pawn pawn = null;

            try {
                pawn = randomizer.GeneratePawn(new PawnGenerationRequestWrapper()
                {
                    Faction = faction,
                    KindDef = kindDef
                }.Request);
                if (pawn.equipment != null)
                {
                    pawn.equipment.DestroyAllEquipment(DestroyMode.Vanish);
                }
                if (pawn.inventory != null)
                {
                    pawn.inventory.DestroyAll(DestroyMode.Vanish);
                }
            }
            catch (Exception e) {
                Log.Warning("Failed to create faction pawn of kind " + kindDef.defName);
                Log.Message(e.Message);
                Log.Message(e.StackTrace);
                if (pawn != null)
                {
                    pawn.Destroy();
                }
                state.AddError("EdB.PC.Panel.PawnList.Error.FactionPawnFailed".Translate());
                return;
            }
            finally {
                kindDef.weaponMoney = savedWeaponsMoney;
            }
            // Reset the quality and damage of all apparel.
            foreach (var a in pawn.apparel.WornApparel)
            {
                a.SetQuality(QualityCategory.Normal);
                a.HitPoints = a.MaxHitPoints;
            }

            CustomPawn customPawn = new CustomPawn(pawn);

            PrepareCarefully.Instance.AddPawn(customPawn);
            state.CurrentPawn = customPawn;
            PawnAdded(customPawn);
        }
예제 #2
0
        public void AddFactionPawn(PawnKindDef kindDef, bool startingPawn)
        {
            FactionDef factionDef = kindDef.defaultFactionType;
            Faction    faction    = PrepareCarefully.Instance.Providers.Factions.GetFaction(factionDef);

            // Workaround to force pawn generation to skip adding weapons to the pawn.
            // Might be a slightly risky hack, but the finally block should guarantee that
            // the weapons money range always gets set back to its original value.
            // TODO: Try to remove this at a later date.  It would be nice if the pawn generation
            // request gave you an option to skip weapon and equipment generation.
            FloatRange savedWeaponsMoney = kindDef.weaponMoney;

            kindDef.weaponMoney = new FloatRange(0, 0);
            Pawn pawn = null;

            try {
                pawn = randomizer.GeneratePawn(new PawnGenerationRequestWrapper()
                {
                    Faction = faction,
                    KindDef = kindDef,
                    Context = PawnGenerationContext.NonPlayer,
                    WorldPawnFactionDoesntMatter = false
                }.Request);
                if (pawn.equipment != null)
                {
                    pawn.equipment.DestroyAllEquipment(DestroyMode.Vanish);
                }
                if (pawn.inventory != null)
                {
                    pawn.inventory.DestroyAll(DestroyMode.Vanish);
                }
            }
            catch (Exception e) {
                Logger.Warning("Failed to create faction pawn of kind " + kindDef.defName, e);
                if (pawn != null)
                {
                    pawn.Destroy();
                }
                state.AddError("EdB.PC.Panel.PawnList.Error.FactionPawnFailed".Translate());
                return;
            }
            finally {
                kindDef.weaponMoney = savedWeaponsMoney;
            }

            // Reset the quality and damage of all apparel.
            foreach (var a in pawn.apparel.WornApparel)
            {
                a.SetQuality(QualityCategory.Normal);
                a.HitPoints = a.MaxHitPoints;
            }

            // TODO: Revisit this if we add a UI to edit titles.
            // Clear out all titles.
            if (pawn.royalty != null)
            {
                pawn.royalty = new Pawn_RoyaltyTracker(pawn);
            }

            CustomPawn customPawn = new CustomPawn(pawn);

            customPawn.OriginalKindDef    = kindDef;
            customPawn.OriginalFactionDef = faction.def;
            pawn.SetFaction(Faction.OfPlayer);

            customPawn.Type = startingPawn ? CustomPawnType.Colonist : CustomPawnType.World;
            if (!startingPawn)
            {
                CustomFaction customFaction = PrepareCarefully.Instance.Providers.Factions.FindRandomCustomFactionByDef(factionDef);
                if (customFaction != null)
                {
                    customPawn.Faction = customFaction;
                }
            }

            PrepareCarefully.Instance.AddPawn(customPawn);
            state.CurrentPawn = customPawn;
            PawnAdded(customPawn);
        }