Skip to content

Commit

Permalink
Prevent exception when generating filler holder for county (#1550) #p…
Browse files Browse the repository at this point in the history
…atch
  • Loading branch information
IhateTrains authored Oct 6, 2023
1 parent 15e1359 commit 7bb73ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
10 changes: 8 additions & 2 deletions ImperatorToCK3/CK3/Religions/ReligionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ Date date
imperatorReligions,
holySiteEffectMapper
);
HolySites.Add(newHolySiteInSameBarony);
if (HolySites.ContainsKey(newHolySiteInSameBarony.Id)) {
Logger.Warn($"Created duplicate holy site: {newHolySiteInSameBarony.Id}!");
}
HolySites.AddOrReplace(newHolySiteInSameBarony);

faith.ReplaceHolySiteId(holySiteId, newHolySiteInSameBarony.Id);
}
Expand All @@ -238,7 +241,10 @@ Date date
imperatorReligions,
holySiteEffectMapper
);
HolySites.Add(replacementSite);
if (HolySites.ContainsKey(replacementSite.Id)) {
Logger.Warn($"Created duplicate holy site: {replacementSite.Id}!");
}
HolySites.AddOrReplace(replacementSite);

faith.ReplaceHolySiteId(holySiteId, replacementSite.Id);
}
Expand Down
45 changes: 36 additions & 9 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,9 @@ private void GenerateFillerHoldersForUnownedLands(CultureCollection cultures, Co
var allCountyProvinces = county.CountyProvinces
.Select(p => Provinces[p]);
candidateProvinces.UnionWith(allCountyProvinces);
var province = candidateProvinces
.First(p => p.GetFaithId(date) is not null);

var pseudoRandomSeed = (int)candidateProvinces.First().Id;

// Determine culture of the holder.
var culture = candidateProvinces
.Select(p => p.GetCulture(date, cultures))
.FirstOrDefault(c => c is not null);
Expand All @@ -711,33 +711,60 @@ private void GenerateFillerHoldersForUnownedLands(CultureCollection cultures, Co
.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();
}
}

// Determine faith of the holder.
var faithId = candidateProvinces
.Select(p => p.GetFaithId(date))
.FirstOrDefault(f => f is not null);
if (faithId is null) {
Logger.Debug($"Trying to use de jure duchy for faith of holder for {county.Id}...");
var deJureDuchy = county.DeJureLiege;
if (deJureDuchy is not null) {
faithId = Provinces
.Where(p => deJureDuchy.DuchyContainsProvince(p.Id))
.Select(p => p.GetFaithId(date))
.FirstOrDefault(f => f is not null);
}
if (faithId is null && deJureDuchy?.DeJureLiege is not null) {
Logger.Debug($"Trying to use de jure kingdom for faith of holder for {county.Id}...");
var deJureKingdom = deJureDuchy.DeJureLiege;
faithId = Provinces
.Where(p => deJureKingdom.KingdomContainsProvince(p.Id))
.Select(p => p.GetFaithId(date))
.FirstOrDefault(f => f is not null);
}
if (faithId is null) {
Logger.Warn($"Found no fitting faith for generated holder of {county.Id}, " +
$"using first faith from database!");
faithId = Religions.Faiths.First().Id;
}
}

bool female = false;
string name;
var maleNames = culture.MaleNames.ToImmutableList();
if (maleNames.Count > 0) {
name = maleNames.ElementAt((int)province.Id % maleNames.Count);
name = maleNames.ElementAt(pseudoRandomSeed % maleNames.Count);
} else { // Generate a female if no male name is available.
female = true;
var femaleNames = culture.FemaleNames.ToImmutableList();
name = femaleNames.ElementAt((int)province.Id % femaleNames.Count);
name = femaleNames.ElementAt(pseudoRandomSeed % femaleNames.Count);
}
int age = 18 + (int)(province.Id % 60);
int age = 18 + (pseudoRandomSeed % 60);
var holder = new Character($"IRToCK3_{county.Id}_holder", name, date, Characters) {
Female = female,
BirthDate = date.ChangeByYears(-age)
};
holder.SetFaithId(province.GetFaithId(date)!, null);
holder.SetFaithId(faithId, null);
holder.SetCultureId(culture.Id, null);
holder.History.AddFieldValue(date, "government", "change_government", "tribal_government");
Characters.Add(holder);
Characters.AddOrReplace(holder);

county.SetHolder(holder, date);
if (config.FillerDukes) {
Expand Down

0 comments on commit 7bb73ee

Please sign in to comment.