Skip to content

Commit

Permalink
Fix invalid liege entries in title history (#2166)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Sep 9, 2024
1 parent ad375fe commit e624550
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
dynasties.FlattenDynastiesWithNoFounders(this, houses, ck3BookmarkDate);

// Clean up title history.
titles.RemoveInvalidHoldersFromHistory(this);
titles.CleanUpHistory(this, ck3BookmarkDate);
}

public void RemoveEmployerIdFromLandedCharacters(Title.LandedTitles titles, Date conversionDate) {
Expand Down
27 changes: 26 additions & 1 deletion ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public ImmutableHashSet<string> GetAllHolderIds() {
return this.SelectMany(t => t.GetAllHolderIds()).ToImmutableHashSet();
}

public void RemoveInvalidHoldersFromHistory(CharacterCollection characters) {
public void CleanUpHistory(CharacterCollection characters, Date ck3BookmarkDate) {
Logger.Debug("Removing invalid holders from history...");

var validIds = characters.Select(c => c.Id).ToImmutableHashSet();
Expand All @@ -270,6 +270,31 @@ public void RemoveInvalidHoldersFromHistory(CharacterCollection characters) {
value => value.ToString() is string valStr && valStr != "0" && !validIds.Contains(valStr)
);
}

// Remove liege entries that are not valid (liege title is not held at the entry date).
foreach (var title in this) {
if (!title.History.Fields.TryGetValue("liege", out var liegeField)) {
continue;
}

foreach (var (date, entriesList) in liegeField.DateToEntriesDict.ToArray()) {
if (entriesList.Count == 0) {
continue;
}

var lastEntry = entriesList.Last();
var liegeTitleId = lastEntry.Value.ToString();
if (liegeTitleId is null || liegeTitleId == "0") {
continue;
}

if (!TryGetValue(liegeTitleId, out var liegeTitle)) {
liegeField.DateToEntriesDict.Remove(date);
} else if (liegeTitle.GetHolderId(date) == "0") {
liegeField.DateToEntriesDict.Remove(date);
}
}
}
}

public void ImportImperatorCountries(
Expand Down

0 comments on commit e624550

Please sign in to comment.