Skip to content

Commit

Permalink
Prevent exception when choosing culture for generated county holder (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Oct 1, 2023
1 parent 75534ab commit 3c7903d
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,45 @@ private void GenerateFillerHoldersForUnownedLands(CultureCollection cultures, Co
}
}

Province province;
var candidateProvinces = new OrderedSet<Province>();
if (county.CapitalBaronyProvince is not null) {
province = Provinces[county.CapitalBaronyProvince.Value];
} else {
province = county.CountyProvinces
.Select(p => Provinces[p])
.First(p => p.GetFaithId(date) is not null && p.GetCultureId(date) is not null);
// Give priority to capital province.
candidateProvinces.Add(Provinces[county.CapitalBaronyProvince.Value]);
}
var culture = cultures[province.GetCultureId(date)!];

var allCountyProvinces = county.CountyProvinces
.Select(p => Provinces[p]);
candidateProvinces.UnionWith(allCountyProvinces);
var province = candidateProvinces
.First(p => p.GetFaithId(date) is not null);

var culture = candidateProvinces
.Select(p => p.GetCulture(date, cultures))
.FirstOrDefault(c => c is not null);
if (culture is null) {
Logger.Debug($"Trying to use de jure duchy for culture of holder for {county.Id}...");
var deJureDuchy = county.DeJureLiege;
if (deJureDuchy is not null) {
culture = Provinces
.Where(p => deJureDuchy.DuchyContainsProvince(p.Id))
.Select(p => p.GetCulture(date, cultures))
.FirstOrDefault(c => c is not null);
}
if (culture is null && deJureDuchy?.DeJureLiege is not null) {
Logger.Debug($"Trying to use de jure kingdom for culture of holder for {county.Id}...");
var deJureKingdom = deJureDuchy.DeJureLiege;
culture = Provinces
.Where(p => deJureKingdom.KingdomContainsProvince(p.Id))
.Select(p => p.GetCulture(date, cultures))
.FirstOrDefault(c => c is not null);
}

if (culture is null) {
Logger.Warn($"Found no fitting culture for generated holder of {county.Id}, " +
$"using first culture from database!");
culture = cultures.First();
}
}

bool female = false;
string name;
var maleNames = culture.MaleNames.ToImmutableList();
Expand Down

0 comments on commit 3c7903d

Please sign in to comment.