public static void CheckDefeated(Settlement factionBase)
        {
            if (factionBase.Faction == Faction.OfPlayer)
            {
                return;
            }
            Map map = factionBase.Map;

            if (map == null || !IsDefeated(map, factionBase.Faction))
            {
                return;
            }
            DestroyedSettlement destroyedSettlement = (DestroyedSettlement)WorldObjectMaker.MakeWorldObject(WorldObjectDefOf.DestroyedSettlement);

            destroyedSettlement.Tile = factionBase.Tile;
            destroyedSettlement.SetFaction(factionBase.Faction);
            Find.WorldObjects.Add(destroyedSettlement);
            TimedDetectionRaids component = destroyedSettlement.GetComponent <TimedDetectionRaids>();

            component.CopyFrom(factionBase.GetComponent <TimedDetectionRaids>());
            component.SetNotifiedSilently();
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("LetterFactionBaseDefeated".Translate(factionBase.Label, component.DetectionCountdownTimeLeftString));
            if (!HasAnyOtherBase(factionBase))
            {
                factionBase.Faction.defeated = true;
                stringBuilder.AppendLine();
                stringBuilder.AppendLine();
                stringBuilder.Append("LetterFactionBaseDefeated_FactionDestroyed".Translate(factionBase.Faction.Name));
            }
            foreach (Faction allFaction in Find.FactionManager.AllFactions)
            {
                if (!allFaction.Hidden && !allFaction.IsPlayer && allFaction != factionBase.Faction && allFaction.HostileTo(factionBase.Faction))
                {
                    FactionRelationKind playerRelationKind = allFaction.PlayerRelationKind;
                    if (allFaction.TryAffectGoodwillWith(Faction.OfPlayer, 20, canSendMessage: false, canSendHostilityLetter: false))
                    {
                        stringBuilder.AppendLine();
                        stringBuilder.AppendLine();
                        stringBuilder.Append("RelationsWith".Translate(allFaction.Name) + ": " + 20.ToStringWithSign());
                        allFaction.TryAppendRelationKindChangedInfo(stringBuilder, playerRelationKind, allFaction.PlayerRelationKind);
                    }
                }
            }
            Find.LetterStack.ReceiveLetter("LetterLabelFactionBaseDefeated".Translate(), stringBuilder.ToString(), LetterDefOf.PositiveEvent, new GlobalTargetInfo(factionBase.Tile), factionBase.Faction);
            map.info.parent = destroyedSettlement;
            factionBase.Destroy();
            TaleRecorder.RecordTale(TaleDefOf.CaravanAssaultSuccessful, map.mapPawns.FreeColonists.RandomElement());
        }
        public static void Debug_FixRimWarSettlements(bool generateReport = false, bool cleanupErrors = false)
        {
            int rwsCount             = 0;
            int wosCount             = 0;
            int rws_no_wosCount      = 0;
            int wos_no_rwsCount      = 0;
            int factionMismatchCount = 0;

            List <WorldObject> woList  = Find.WorldObjects.AllWorldObjects;
            List <WorldObject> wosList = new List <WorldObject>();

            wosList.Clear();
            for (int i = 0; i < woList.Count; i++)
            {
                RimWorld.Planet.Settlement wos = woList[i] as RimWorld.Planet.Settlement;
                if (wos != null)
                {
                    wosList.Add(wos);
                    wosCount++;
                    RimWarSettlementComp rws = WorldUtility.GetRimWarSettlementAtTile(wos.Tile);
                    if (rws != null)
                    {
                        if (wos.Destroyed)
                        {
                            if (generateReport)
                            {
                                Log.Warning(wos.Label + " destroyed but has RWSC");
                            }
                            if (cleanupErrors)
                            {
                                if (generateReport)
                                {
                                    Log.Message("Cleaning RWS...");
                                }
                                //WorldUtility.GetRimWarDataForFaction(wos.Faction)?.WorldSettlements?.Remove(rws);
                            }
                        }
                        else if (wos.Faction != rws.parent.Faction)
                        {
                            factionMismatchCount++;
                            if (generateReport)
                            {
                                Log.Warning(wos.Label + " of Faction " + wos.Faction + " different from RWS Faction " + rws.parent.Faction);
                            }
                            if (cleanupErrors)
                            {
                                if (generateReport)
                                {
                                    Log.Message("Removing RWS from " + rws.parent.Faction + "...");
                                }
                                //WorldUtility.GetRimWarDataForFaction(rws.parent.Faction)?.WorldSettlements?.Remove(rws);
                                if (generateReport)
                                {
                                    Log.Message("Adding RWS to " + wos.Faction + "...");
                                }
                                //rws.Faction = wos.Faction;
                                //WorldUtility.GetRimWarDataForFaction(wos.Faction)?.WorldSettlements?.Add(rws);
                            }
                        }
                    }
                    else
                    {
                        wos_no_rwsCount++;
                        if (generateReport)
                        {
                            Log.Warning("" + wos.Label + " has no RWS");
                        }
                        if (cleanupErrors)
                        {
                            if (generateReport)
                            {
                                Log.Message("Generating RWS for " + wos.Label + "...");
                            }
                            WorldUtility.CreateRimWarSettlement(WorldUtility.GetRimWarDataForFaction(wos.Faction), wos);
                        }
                    }
                }
            }

            List <RimWarData> rwdList = WorldUtility.Get_WCPT().RimWarData;

            for (int i = 0; i < rwdList.Count; i++)
            {
                RimWarData rwd = rwdList[i];
                if (rwd.WorldSettlements != null)
                {
                    for (int j = 0; j < rwd.WorldSettlements.Count; j++)
                    {
                        RimWarSettlementComp rws = rwd.WorldSettlements[j].GetComponent <RimWarSettlementComp>();
                        rwsCount++;
                        int wosHere = 0;
                        List <RimWorld.Planet.Settlement> wosHereList = new List <RimWorld.Planet.Settlement>();
                        wosHereList.Clear();
                        for (int k = 0; k < wosList.Count; k++)
                        {
                            if (wosList[k].Tile == rws.parent.Tile)
                            {
                                wosHere++;
                                wosHereList.Add(wosList[k] as RimWorld.Planet.Settlement);
                                if (wosList[k].Faction != rws.parent.Faction)
                                {
                                    factionMismatchCount++;
                                    if (generateReport)
                                    {
                                        Log.Warning(wosList[k].Label + " of Faction " + wosList[k].Faction + " different from RWS Faction " + rws.parent.Faction);
                                    }
                                    if (cleanupErrors)
                                    {
                                        if (generateReport)
                                        {
                                            Log.Message("Removing RWS from " + rws.parent.Faction + "...");
                                        }
                                        //WorldUtility.GetRimWarDataForFaction(rws.parent.Faction)?.WorldSettlements?.Remove(rws);
                                        if (generateReport)
                                        {
                                            Log.Message("Adding RWS to " + wosList[k].Faction + "...");
                                        }
                                        //rws.Faction = wosList[k].Faction;
                                        //WorldUtility.GetRimWarDataForFaction(wosList[k].Faction)?.WorldSettlements?.Add(rws);
                                    }
                                }
                            }
                        }
                        if (wosHere == 0)
                        {
                            rws_no_wosCount++;
                            if (generateReport)
                            {
                                Log.Warning("No settlement found at " + Find.WorldGrid.LongLatOf(rws.parent.Tile));
                            }
                            if (cleanupErrors)
                            {
                                if (generateReport)
                                {
                                    Log.Warning("Removing RWS...");
                                }
                                //rwd.FactionSettlements.Remove(rws);
                            }
                        }
                        if (wosHere > 1)
                        {
                            if (generateReport)
                            {
                                Log.Warning("Stacked settlements (" + wosHere + ") found at " + Find.WorldGrid.LongLatOf(rws.parent.Tile));
                            }
                            if (cleanupErrors)
                            {
                                while (wosHereList.Count > 1)
                                {
                                    if (generateReport)
                                    {
                                        Log.Message("Destroying settlement...");
                                    }
                                    RimWorld.Planet.Settlement wosHereDes = wosHereList[0];
                                    wosHereList.Remove(wosHereDes);
                                    if (!wosHereDes.Destroyed)
                                    {
                                        wosHereDes.Destroy();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (generateReport)
                    {
                        Log.Warning("Found null RWD");
                    }
                    if (cleanupErrors)
                    {
                        if (generateReport)
                        {
                            Log.Message("Removing RWD...");
                        }
                        rwdList.Remove(rwd);
                    }
                }
            }

            if (generateReport)
            {
                bool errors = wos_no_rwsCount != 0 || rws_no_wosCount != 0 || factionMismatchCount != 0;
                Log.Message("Rim War Settlement Count: " + rwsCount);
                Log.Message("World Settlement Count: " + wosCount);
                if (!errors)
                {
                    Log.Message("No errors found.");
                }
                if (wos_no_rwsCount > 0)
                {
                    Log.Warning("Settlements without RWS component: " + wos_no_rwsCount);
                }
                if (rws_no_wosCount > 0)
                {
                    Log.Warning("Rim War components without Settlement: " + rws_no_wosCount);
                }
                if (factionMismatchCount > 0)
                {
                    Log.Warning("Faction mismatches: " + factionMismatchCount);
                }
            }
        }