Skip to content

Fix some kingdom adjectives referencing non-existent duchy adjectives #2161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,168 +517,168 @@
}
}

public void ImportImperatorHoldings(ProvinceCollection ck3Provinces, Imperator.Characters.CharacterCollection irCharacters, Date conversionDate) {
Logger.Info("Importing Imperator holdings...");
var counter = 0;

var highLevelTitlesThatHaveHolders = this
.Where(t => t.Rank >= TitleRank.duchy && t.GetHolderId(conversionDate) != "0")
.ToImmutableList();
var highLevelTitleCapitalBaronyIds = highLevelTitlesThatHaveHolders
.Select(t=>t.CapitalCounty?.CapitalBaronyId ?? t.CapitalBaronyId)
.ToImmutableHashSet();

// Dukes and above should be excluded from having their holdings converted.
// Otherwise, governors with holdings would own parts of other governorships.
var dukeAndAboveIds = highLevelTitlesThatHaveHolders
.Where(t => t.Rank >= TitleRank.duchy)
.Select(t => t.GetHolderId(conversionDate))
.ToImmutableHashSet();

// We exclude baronies that are capitals of duchies and above.
var eligibleBaronies = this
.Where(t => t.Rank == TitleRank.barony)
.Where(b => !highLevelTitleCapitalBaronyIds.Contains(b.Id))
.ToArray();

var countyCapitalBaronies = eligibleBaronies
.Where(b => b.DeJureLiege?.CapitalBaronyId == b.Id)
.OrderBy(b => b.Id)
.ToArray();

var nonCapitalBaronies = eligibleBaronies.Except(countyCapitalBaronies).OrderBy(b => b.Id).ToArray();


// In CK3, a county holder shouldn't own baronies in counties that are not their own.
// This dictionary tracks what counties are held by what characters.
Dictionary<string, HashSet<string>> countiesPerCharacter = []; // characterId -> countyIds

// Evaluate all capital baronies first (we want to distribute counties first, then baronies).
foreach (var barony in countyCapitalBaronies) {
var ck3Province = GetBaronyProvince(barony);
if (ck3Province is null) {
continue;
}

// Skip none holdings and temple holdings.
if (ck3Province.GetHoldingType(conversionDate) is "church_holding" or "none") {
continue;
}

var irProvince = ck3Province.PrimaryImperatorProvince; // TODO: when the holding owner of the primary I:R province is not able to hold the CK3 equivalent, also check the holding owners from secondary source provinces
var ck3Owner = GetEligibleCK3OwnerForImperatorProvince(irProvince);
if (ck3Owner is null) {
continue;
}

var realm = ck3Owner.ImperatorCharacter?.HomeCountry?.CK3Title;
var deFactoLiege = realm;
if (realm is not null) {
var deJureDuchy = barony.DeJureLiege?.DeJureLiege;
if (deJureDuchy is not null && deJureDuchy.GetHolderId(conversionDate) != "0" && deJureDuchy.GetTopRealm(conversionDate) == realm) {
deFactoLiege = deJureDuchy;
} else {
var deJureKingdom = deJureDuchy?.DeJureLiege;
if (deJureKingdom is not null && deJureKingdom.GetHolderId(conversionDate) != "0" && deJureKingdom.GetTopRealm(conversionDate) == realm) {
deFactoLiege = deJureKingdom;
}
}
}

// Barony is a county capital, so set the county holder to the holding owner.
var county = barony.DeJureLiege;
if (county is null) {
Logger.Warn($"County capital barony {barony.Id} has no de jure county!");
continue;
}
county.SetHolder(ck3Owner, conversionDate);
county.SetDeFactoLiege(deFactoLiege, conversionDate);

if (!countiesPerCharacter.TryGetValue(ck3Owner.Id, out var countyIds)) {
countyIds = [];
countiesPerCharacter[ck3Owner.Id] = countyIds;
}
countyIds.Add(county.Id);

++counter;
}

// In CK3, a baron that doesn't own counties can only hold a single barony.
// This dictionary IDs of such barons that already hold a barony.
HashSet<string> baronyHolderIds = [];

// After all possible county capital baronies are distributed, distribute the rest of the eligible baronies.
foreach (var barony in nonCapitalBaronies) {
var ck3Province = GetBaronyProvince(barony);
if (ck3Province is null) {
continue;
}

// Skip none holdings and temple holdings.
if (ck3Province.GetHoldingType(conversionDate) is "church_holding" or "none") {
continue;
}

var irProvince = ck3Province.PrimaryImperatorProvince; // TODO: when the holding owner of the primary I:R province is not able to hold the CK3 equivalent, also check the holding owners from secondary source provinces
var ck3Owner = GetEligibleCK3OwnerForImperatorProvince(irProvince);
if (ck3Owner is null) {
continue;
}
if (baronyHolderIds.Contains(ck3Owner.Id)) {
continue;
}

var county = barony.DeJureLiege;
if (county is null) {
Logger.Warn($"Barony {barony.Id} has no de jure county!");
continue;
}
// A non-capital barony cannot be held by a character that owns a county but not the county the barony is in.
if (countiesPerCharacter.TryGetValue(ck3Owner.Id, out var countyIds) && !countyIds.Contains(county.Id)) {
continue;
}

barony.SetHolder(ck3Owner, conversionDate);
// No need to set de facto liege for baronies, they are tied to counties.

baronyHolderIds.Add(ck3Owner.Id);

++counter;
}
Logger.Info($"Imported {counter} holdings from I:R.");
Logger.IncrementProgress();
return;

Province? GetBaronyProvince(Title barony) {
var ck3ProvinceId = barony.ProvinceId;
if (ck3ProvinceId is null) {
return null;
}
if (!ck3Provinces.TryGetValue(ck3ProvinceId.Value, out var ck3Province)) {
return null;
}
return ck3Province;
}

Character? GetEligibleCK3OwnerForImperatorProvince(Imperator.Provinces.Province? irProvince) {
var holdingOwnerId = irProvince?.HoldingOwnerId;
if (holdingOwnerId is null) {
return null;
}

var irOwner = irCharacters[holdingOwnerId.Value];
var ck3Owner = irOwner.CK3Character;
if (ck3Owner is null) {
return null;
}
if (dukeAndAboveIds.Contains(ck3Owner.Id)) {
return null;
}

return ck3Owner;
}
}

Check notice on line 681 in ImperatorToCK3/CK3/Titles/LandedTitles.cs

View check run for this annotation

codefactor.io / CodeFactor

ImperatorToCK3/CK3/Titles/LandedTitles.cs#L520-L681

Complex Method
public void RemoveInvalidLandlessTitles(Date ck3BookmarkDate) {
Logger.Info("Removing invalid landless titles...");
var removedGeneratedTitles = new HashSet<string>();
Expand Down Expand Up @@ -744,9 +744,17 @@
);

var kingdomAdjLoc = ck3LocDB.GetOrCreateLocBlock(kingdom.Id + "_adj");
string duchyAdjLocKey = duchy.Id + "_adj"; // TODO: add some handling for the case where this is not localized
string duchyAdjLocKey = duchy.Id + "_adj";
kingdomAdjLoc.ModifyForEveryLanguage(
(orig, language) => $"${duchyAdjLocKey}$");
(orig, language) => {
if (ck3LocDB.HasKeyLocForLanguage(duchyAdjLocKey, language)) {
return $"${duchyAdjLocKey}$";
}

Logger.Debug($"Using duchy name as adjective for {kingdom.Id} in {language} because duchy adjective is missing.");
return $"${duchy.Id}$";
}
);

kingdom.DeJureLiege = capitalEmpireRealm;
duchy.DeJureLiege = kingdom;
Expand Down
Loading