Skip to content

Commit

Permalink
Handle title being set as its own liege in title history (#1714) #patch
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Jan 16, 2024
1 parent cc5b8c9 commit 1964480
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/CK3/Titles/LandedTitlesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public void HistoryCanBeLoadedFromInitialValues() {
titles.LoadHistory(config, ck3ModFS);

Assert.Equal("67", title.GetHolderId(date));
Assert.Equal("e_italia", title.GetLiege(date));
Assert.Equal("e_italia", title.GetLiegeId(date));
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions ImperatorToCK3.UnitTests/CK3/Titles/TitleHistoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public void HolderDefaultsToZeroString() {
}

[Fact]
public void LiegeDefaultsToNull() {
public void LiegeIdDefaultsToNull() {
var titles = new Title.LandedTitles();
var title = titles.Add("k_title");

Assert.Null(title.GetLiege(new Date(867, 1, 1)));
Assert.Null(title.GetLiegeId(new Date(867, 1, 1)));
}

[Fact]
Expand Down
12 changes: 9 additions & 3 deletions ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,7 @@ public void RemoveInvalidLandlessTitles(Date ck3BookmarkDate) {
Logger.IncrementProgress();
}

public void SetDeJureKingdomsAndEmpires(Date ck3BookmarkDate, ProvinceCollection ck3Provinces, CultureCollection ck3Cultures) {
// Generate King/Empire de jure hierarchy from governorships
private void SetDeJureKingdoms(Date ck3BookmarkDate) {
Logger.Info("Setting de jure kingdoms...");
foreach (var duchy in this.Where(t => t.Rank == TitleRank.duchy && t.DeJureVassals.Count > 0)) {
// If capital county belongs to a kingdom, make the kingdom a de jure liege of the duchy.
Expand All @@ -592,7 +591,7 @@ public void SetDeJureKingdomsAndEmpires(Date ck3BookmarkDate, ProvinceCollection
if (kingdomRealm is null) {
continue;
}
kingdomRealmShares.TryGetValue(kingdomRealm.Id, out var currentCount);
kingdomRealmShares.TryGetValue(kingdomRealm.Id, out int currentCount);
kingdomRealmShares[kingdomRealm.Id] = currentCount + county.CountyProvinceIds.Count();
}
if (kingdomRealmShares.Count > 0) {
Expand All @@ -601,7 +600,9 @@ public void SetDeJureKingdomsAndEmpires(Date ck3BookmarkDate, ProvinceCollection
}
}
Logger.IncrementProgress();
}

private void SetDeJureEmpires(ProvinceCollection ck3Provinces, CultureCollection ck3Cultures, Date ck3BookmarkDate) {
Logger.Info("Setting de jure empires...");
var deJureKingdoms = GetDeJureKingdoms();
foreach (var kingdom in deJureKingdoms) {
Expand Down Expand Up @@ -674,6 +675,11 @@ public void SetDeJureKingdomsAndEmpires(Date ck3BookmarkDate, ProvinceCollection
Logger.IncrementProgress();
}

public void SetDeJureKingdomsAndEmpires(Date ck3BookmarkDate, ProvinceCollection ck3Provinces, CultureCollection ck3Cultures) {
SetDeJureKingdoms(ck3BookmarkDate);
SetDeJureEmpires(ck3Provinces, ck3Cultures, ck3BookmarkDate);
}

private HashSet<string> GetCountyHolderIds(Date date) {
var countyHoldersCache = new HashSet<string>();
foreach (var county in this.Where(t => t.Rank == TitleRank.county)) {
Expand Down
15 changes: 13 additions & 2 deletions ImperatorToCK3/CK3/Titles/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,19 @@ public Title? DeJureLiege { // direct de jure liege title
}
}
public Title? GetDeFactoLiege(Date date) { // direct de facto liege title
var liegeStr = GetLiege(date);
if (liegeStr is not null && parentCollection.TryGetValue(liegeStr, out var liegeTitle)) {
var liegeId = GetLiegeId(date);
if (liegeId is not null && parentCollection.TryGetValue(liegeId, out var liegeTitle)) {
if (liegeTitle.Id == Id) {
Logger.Debug($"A title cannot be its own liege! Title: {Id}");
return null;
}

if (liegeTitle.Rank <= Rank) {
Logger.Debug($"Liege title's rank is not higher than vassal's! " +
$"Title: {Id}, liege: {liegeTitle.Id}");
return null;
}

return liegeTitle;
}

Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/Titles/TitleHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void SetGovernment(string governmentId, Date date) {
History.AddFieldValue(date, "government", "government", governmentId);
}

public string? GetLiege(Date date) {
public string? GetLiegeId(Date date) {
if (History.GetFieldValue("liege", date) is string liegeStr) {
return liegeStr;
}
Expand Down

0 comments on commit 1964480

Please sign in to comment.