Skip to content

Commit

Permalink
Handle missing cultures when setting de jure empires (#1501) #patch
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Aug 23, 2023
1 parent 4f530b0 commit 866d639
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 13 additions & 0 deletions ImperatorToCK3/CK3/Provinces/ProvinceHistory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using commonItems;
using commonItems.Collections;
using ImperatorToCK3.CK3.Cultures;
using ImperatorToCK3.CommonUtils;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -31,6 +32,18 @@ public void SetFaithId(string faithId, Date? date) {
public void SetCultureId(string cultureId, Date? date) {
History.AddFieldValue(date, "culture", "culture", cultureId);
}

public Culture? GetCulture(Date date, CultureCollection cultures) {
var cultureId = GetCultureId(date);
if (cultureId is null) {
return null;
}
if (cultures.TryGetValue(cultureId, out var culture)) {
return culture;
}
Logger.Warn($"Culture with ID {cultureId} not found!");
return null;
}

public string? GetHoldingType(Date date) {
var historyValue = History.GetFieldValue("holding", date);
Expand Down
5 changes: 2 additions & 3 deletions ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,8 @@ public void SetDeJureKingdomsAndEmpires(Date ck3BookmarkDate, ProvinceCollection
var kingdomProvinceIds = counties.SelectMany(c => c.CountyProvinces).ToImmutableHashSet();
var kingdomProvinces = ck3Provinces.Where(p => kingdomProvinceIds.Contains(p.Id));
var dominantHeritage = kingdomProvinces
.Select(c => new { County = c, CultureId = c.GetCultureId(ck3BookmarkDate)})
.Where(x => x.CultureId is not null)
.Select(x => new { x.County, ck3Cultures[x.CultureId!].Heritage })
.Select(p => new { Province = p, p.GetCulture(ck3BookmarkDate, ck3Cultures)?.Heritage})
.Where(x => x.Heritage is not null)
.GroupBy(x => x.Heritage)
.MaxBy(g => g.Count())?.Key;
if (dominantHeritage is null) {
Expand Down

0 comments on commit 866d639

Please sign in to comment.