public static bool RemovePawn(WorldPawns __instance, Pawn p)
        {
            lock (__instance)
            {
                if (!__instance.Contains(p))
                {
                    Log.Error("Tried to remove pawn " + (object)p + " from " + (object)__instance.GetType() + ", but it's not here.");
                }
                __instance.gc.CancelGCPass();
                if (__instance.pawnsMothballed.Contains(p))
                {
                    if (Find.TickManager.TicksGame % 15000 != 0)
                    {
                        try
                        {
                            p.TickMothballed(Find.TickManager.TicksGame % 15000);
                        }
                        catch (Exception ex)
                        {
                            Log.Error("Exception ticking mothballed world pawn (just before removing): " + (object)ex);
                        }
                    }
                }
                HashSet <Pawn> newPawnsAlive = new HashSet <Pawn>(__instance.pawnsAlive);
                newPawnsAlive.Remove(p);
                __instance.pawnsAlive = newPawnsAlive;

                HashSet <Pawn> newPawnsMothballed = new HashSet <Pawn>(__instance.pawnsMothballed);
                newPawnsMothballed.Remove(p);
                __instance.pawnsMothballed = newPawnsMothballed;

                HashSet <Pawn> newPawnsDead = new HashSet <Pawn>(__instance.pawnsDead);
                newPawnsDead.Remove(p);
                __instance.pawnsDead = newPawnsDead;

                HashSet <Pawn> newPawnsForcefullyKeptAsWorldPawns = new HashSet <Pawn>(__instance.pawnsForcefullyKeptAsWorldPawns);
                newPawnsDead.Remove(p);
                __instance.pawnsForcefullyKeptAsWorldPawns = newPawnsForcefullyKeptAsWorldPawns;

                p.becameWorldPawnTickAbs = -1;
            }
            return(false);
        }
Exemple #2
0
        public static bool Destroy(Pawn __instance, DestroyMode mode = DestroyMode.Vanish)
        {
            if (mode != 0 && mode != DestroyMode.KillFinalize)
            {
                Log.Error(string.Concat("Destroyed pawn ", __instance, " with unsupported mode ", mode, "."));
            }
            //ThingWithComps twc = __instance;
            //twc.Destroy(mode);
            ThingWithCompsDestroy(__instance, mode);
            Find.WorldPawns.Notify_PawnDestroyed(__instance);
            if (__instance.ownership != null)
            {
                Building_Grave assignedGrave = __instance.ownership.AssignedGrave;
                __instance.ownership.UnclaimAll();
                if (mode == DestroyMode.KillFinalize)
                {
                    assignedGrave?.CompAssignableToPawn.TryAssignPawn(__instance);
                }
            }

            __instance.ClearMind(ifLayingKeepLaying: false, clearInspiration: true);
            Lord lord = __instance.GetLord();

            if (lord != null)
            {
                PawnLostCondition cond = (mode != DestroyMode.KillFinalize) ? PawnLostCondition.Vanished : PawnLostCondition.IncappedOrKilled;
                lord.Notify_PawnLost(__instance, cond);
            }

            if (Current.ProgramState == ProgramState.Playing)
            {
                Find.GameEnder.CheckOrUpdateGameOver();
                Find.TaleManager.Notify_PawnDestroyed(__instance);
            }

            //foreach (Pawn item in PawnsFinder.AllMapsWorldAndTemporary_Alive.Where((Pawn p) => p.playerSettings != null && p.playerSettings.Master == __instance))
            //{
            //item.playerSettings.Master = null;
            //}
            //ClearMatchingMasters(__instance)
            if (Pawn_PlayerSettings_Patch.petsInit == false)
            {
                Pawn_PlayerSettings_Patch.RebuildPetsDictionary();
            }
            if (Pawn_PlayerSettings_Patch.pets.TryGetValue(__instance, out List <Pawn> pawnList))
            {
                for (int i = 0; i < pawnList.Count; i++)
                {
                    Pawn p;
                    try
                    {
                        p = pawnList[i];
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        break;
                    }
                    p.playerSettings.Master = null;
                }
            }

            /*
             * for (int i = 0; i < PawnsFinder.AllMapsWorldAndTemporary_Alive.Count; i++)
             * {
             *  Pawn p;
             *  try
             *  {
             *      p = PawnsFinder.AllMapsWorldAndTemporary_Alive[i];
             *  }
             *  catch (ArgumentOutOfRangeException)
             *  {
             *      break;
             *  }
             *  if (p.playerSettings != null && p.playerSettings.Master == __instance)
             *  {
             *      p.playerSettings.Master = null;
             *  }
             * }
             */
            if (__instance.equipment != null)
            {
                __instance.equipment.Notify_PawnDied();
            }

            if (mode != DestroyMode.KillFinalize)
            {
                if (__instance.equipment != null)
                {
                    __instance.equipment.DestroyAllEquipment();
                }

                __instance.inventory.DestroyAll();
                if (__instance.apparel != null)
                {
                    __instance.apparel.DestroyAll();
                }
            }

            WorldPawns worldPawns = Find.WorldPawns;

            if (!worldPawns.IsBeingDiscarded(__instance) && !worldPawns.Contains(__instance))
            {
                worldPawns.PassToWorld(__instance);
            }
            return(false);
        }