Skip to content

Commit

Permalink
Purge unneeded dynasties (#1895) #minor
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Apr 21, 2024
1 parent 0297676 commit 5976186
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
6 changes: 4 additions & 2 deletions ImperatorToCK3.UnitTests/CK3/Characters/CK3CharacterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FluentAssertions;
using ImperatorToCK3.CK3.Characters;
using ImperatorToCK3.CK3.Cultures;
using ImperatorToCK3.CK3.Dynasties;
using ImperatorToCK3.CK3.Religions;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.CommonUtils.Genes;
Expand Down Expand Up @@ -441,7 +442,7 @@ public void UnneededCharactersArePurged() {
ck3Characters.Add(unlandedFromCK3);

var titles = new Title.LandedTitles();
ck3Characters.PurgeUnneededCharacters(titles, ConversionDate);
ck3Characters.PurgeUnneededCharacters(titles, new DynastyCollection(), ConversionDate);

Assert.Empty(ck3Characters);
}
Expand Down Expand Up @@ -500,11 +501,12 @@ public void DeadLandlessCharactersArePurgedIfChildless() {
childlessRelative.Father = fatherOfLandedCharacter;

var dynasty = new ImperatorToCK3.CK3.Dynasties.Dynasty(irFamily, irCharacters, new CulturesDB(), CultureMapper, new LocDB("english"), ConversionDate);
var dynasties = new DynastyCollection { dynasty };
Assert.Equal(dynasty.Id, landedCharacter.GetDynastyId(ConversionDate));
Assert.Equal(dynasty.Id, fatherOfLandedCharacter.GetDynastyId(ConversionDate));
Assert.Equal(dynasty.Id, childlessRelative.GetDynastyId(ConversionDate));

ck3Characters.PurgeUnneededCharacters(titles, ConversionDate);
ck3Characters.PurgeUnneededCharacters(titles, dynasties, ConversionDate);

ck3Characters.Should().BeEquivalentTo(new[] {
landedCharacter,
Expand Down
7 changes: 6 additions & 1 deletion ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using commonItems.Localization;
using ImperatorToCK3.CK3.Armies;
using ImperatorToCK3.CK3.Cultures;
using ImperatorToCK3.CK3.Dynasties;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.Imperator.Armies;
using ImperatorToCK3.Mappers.Culture;
Expand Down Expand Up @@ -374,7 +375,7 @@ private static IEnumerable<string> LoadCharacterIDsToPreserve() {
return characterIDsToPreserve;
}

public void PurgeUnneededCharacters(Title.LandedTitles titles, Date ck3BookmarkDate) {
public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection dynasties, Date ck3BookmarkDate) {
Logger.Info("Purging unneeded characters...");

// Characters that hold or held titles should always be kept.
Expand Down Expand Up @@ -447,6 +448,10 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, Date ck3BookmarkD
Logger.Debug($"Purged {farewellCharacters.Count} unneeded characters in iteration {i}.");
charactersToCheck = charactersToCheck.Except(farewellCharacters).ToList();
} while(farewellCharacters.Count > 0);

// At this point we probably have many imported dynasties with no characters left.
// Let's purge them.
dynasties.PurgeUnneededDynasties(this, ck3BookmarkDate);
}

public void RemoveEmployerIdFromLandedCharacters(Title.LandedTitles titles, Date conversionDate) {
Expand Down
3 changes: 3 additions & 0 deletions ImperatorToCK3/CK3/Dynasties/Dynasty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace ImperatorToCK3.CK3.Dynasties;
[SerializationByProperties]
public partial class Dynasty : IPDXSerializable, IIdentifiable<string> {
public Dynasty(Family irFamily, CharacterCollection irCharacters, CulturesDB irCulturesDB, CultureMapper cultureMapper, LocDB locDB, Date date) {
FromImperator = true;
Id = $"dynn_irtock3_{irFamily.Id}";
Name = Id;

Expand All @@ -35,6 +36,7 @@ public Dynasty(Family irFamily, CharacterCollection irCharacters, CulturesDB irC
}

public Dynasty(CK3.Characters.Character character, string irFamilyName, CulturesDB irCulturesDB, LocDB locDB, Date date) {
FromImperator = true;
Id = $"dynn_irtock3_from_{character.Id}";
Name = Id;

Expand All @@ -54,6 +56,7 @@ public Dynasty(CK3.Characters.Character character, string irFamilyName, Cultures

[NonSerialized] public LocBlock? LocalizedName { get; private set; }
[NonSerialized] public StringOfItem? CoA { get; set; }
[NonSerialized] public bool FromImperator { get; private set; } = false;

private void SetCultureFromImperator(Family irFamily, IReadOnlyList<Character> irMembers, CultureMapper cultureMapper, Date date) {
if (irMembers.Count > 0) {
Expand Down
24 changes: 24 additions & 0 deletions ImperatorToCK3/CK3/Dynasties/DynastyCollection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using commonItems;
using commonItems.Collections;
using commonItems.Localization;
using ImperatorToCK3.CK3.Characters;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.Mappers.Culture;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -82,4 +84,26 @@ public void SetCoasForRulingDynasties(Title.LandedTitles titles, Date date) {
}
Logger.IncrementProgress();
}

public void PurgeUnneededDynasties(CharacterCollection characters, Date date) {
Logger.Info("Purging unneeded dynasties...");

HashSet<string> dynastiesToKeep = [];
foreach (var character in characters) {
var dynastyId = character.GetDynastyId(date);
if (dynastyId is not null) {
dynastiesToKeep.Add(dynastyId);
}
}

int removedCount = 0;
foreach (var dynasty in this.Where(d => d.FromImperator).ToList()) {
if (!dynastiesToKeep.Contains(dynasty.Id)) {
Remove(dynasty.Id);
++removedCount;
}
}

Logger.Info($"Purged {removedCount} unneeded dynasties.");
}
}
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public World(Imperator.World impWorld, Configuration config) {
Characters.ImportLegions(LandedTitles, impWorld.Units, impWorld.Characters, CorrectedDate, unitTypeMapper, MenAtArmsTypes, provinceMapper, config);

Characters.RemoveEmployerIdFromLandedCharacters(LandedTitles, CorrectedDate);
Characters.PurgeUnneededCharacters(LandedTitles, config.CK3BookmarkDate);
Characters.PurgeUnneededCharacters(LandedTitles, Dynasties, config.CK3BookmarkDate);

// Check if any muslim religion exists in Imperator. Otherwise, remove Islam from the entire CK3 map.
var possibleMuslimReligionNames = new List<string> { "muslim", "islam", "sunni", "shiite" };
Expand Down

0 comments on commit 5976186

Please sign in to comment.