Skip to content

Commit 0108535

Browse files
authored
Optimize the generation of successors for old characters (#3033)
2 parents 3f757b5 + 9cffb68 commit 0108535

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

.github/workflows/ck3-tiger.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ filter = {
233233
text = "missing english localization key default value"
234234
text = "subject contract flag meritocratic_province_protectorate not defined in common/subject_contracts/contracts/"
235235
text = "subject contract flag celestial_province_celestial not defined in common/subject_contracts/contracts/"
236+
text = "missing english localization key grand_marshal"
237+
text = "missing english localization key hunt_every_attendee_tt"
238+
text = "missing english localization key is_kampaku_tt"
239+
text = "missing english localization key ceremonial_liege_appointment_support_desc"
240+
text = "missing english localization key situation_parameter_era_type_chaotic"
241+
text = "missing english localization key situation_parameter_era_type_unstable"
242+
text = "missing english localization key situation_parameter_hide_in_phases_list"
243+
text = "missing english localization key situation_parameter_era_type_stable"
244+
text = "missing english localization key dynastic_cycle_situation_dynastic_cycle_phase_stability_situation_phase"
236245
}
237246
}
238247
}

ImperatorToCK3/CK3/Characters/CharacterCollection.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ internal void GenerateSuccessorsForOldCharacters(Title.LandedTitles titles, Cult
783783

784784
var titleHolderIds = titles.GetHolderIdsForAllTitlesExceptNobleFamilyTitles(ck3BookmarkDate);
785785
var oldTitleHolders = new List<Character>();
786-
var oldCharactersWithoutTitles = new List<Character>();
786+
var randomForCharactersWithoutTitles = new Random((int)randomSeed);
787787
foreach (var character in this) {
788788
if (character.BirthDate >= ck3BookmarkDate || character.DeathDate is not null || ck3BookmarkDate.DiffInYears(character.BirthDate) <= 60) {
789789
continue;
@@ -792,33 +792,28 @@ internal void GenerateSuccessorsForOldCharacters(Title.LandedTitles titles, Cult
792792
if (titleHolderIds.Contains(character.Id)) {
793793
oldTitleHolders.Add(character);
794794
} else {
795-
oldCharactersWithoutTitles.Add(character);
795+
// For characters that don't hold any titles, just set up a death date.
796+
SetUpDeathDateForCharacterWithoutTitles(character, irSaveDate, randomForCharactersWithoutTitles);
796797
}
797798
}
798799

799-
// For characters that don't hold any titles, just set up a death date.
800-
var randomForCharactersWithoutTitles = new Random((int)randomSeed);
801-
foreach (var oldCharacter in oldCharactersWithoutTitles) {
802-
SetUpDeathDateForCharacterWithoutTitles(oldCharacter, irSaveDate, randomForCharactersWithoutTitles);
800+
if (oldTitleHolders.Count == 0) {
801+
return;
803802
}
804803

805-
var heldTitlesByHolderIdLists = new Dictionary<string, List<Title>>(StringComparer.Ordinal);
804+
var titlesByHolderId = new Dictionary<string, List<Title>>(StringComparer.Ordinal);
806805
foreach (var title in titles) {
807806
var holderId = title.GetHolderId(ck3BookmarkDate);
808807
if (holderId == "0") {
809808
continue;
810809
}
811810

812-
if (!heldTitlesByHolderIdLists.TryGetValue(holderId, out var heldTitles)) {
811+
if (!titlesByHolderId.TryGetValue(holderId, out var heldTitles)) {
813812
heldTitles = [];
814-
heldTitlesByHolderIdLists[holderId] = heldTitles;
813+
titlesByHolderId[holderId] = heldTitles;
815814
}
816815
heldTitles.Add(title);
817816
}
818-
var titlesByHolderId = new Dictionary<string, Title[]>(heldTitlesByHolderIdLists.Count, StringComparer.Ordinal);
819-
foreach (var (holderId, heldTitles) in heldTitlesByHolderIdLists) {
820-
titlesByHolderId[holderId] = [.. heldTitles];
821-
}
822817

823818
var cultureIdToMaleNames = new Dictionary<string, string[]>(cultures.Count, StringComparer.Ordinal);
824819
foreach (var culture in cultures) {
@@ -829,13 +824,18 @@ internal void GenerateSuccessorsForOldCharacters(Title.LandedTitles titles, Cult
829824
Parallel.ForEach(oldTitleHolders, oldCharacter => GenerateSuccessorsForCharacter(oldCharacter, titlesByHolderId, cultureIdToMaleNames, irSaveDate, ck3BookmarkDate, randomSeed));
830825
}
831826

832-
private void SetUpDeathDateForCharacterWithoutTitles(Character oldCharacter, Date irSaveDate, Random random) {
827+
private static void SetUpDeathDateForCharacterWithoutTitles(Character oldCharacter, Date irSaveDate, Random random) {
833828
// Roll a dice to determine how much longer the character will live.
834829
var monthsToLive = random.Next(1, 30 * 12); // Can live up to 30 years more.
835830

836831
// If the character is female and pregnant, make sure she doesn't die before the pregnancy ends.
837832
if (oldCharacter is {Female: true, ImperatorCharacter: not null}) {
838-
var lastPregnancy = oldCharacter.Pregnancies.OrderBy(p => p.BirthDate).LastOrDefault();
833+
Pregnancy? lastPregnancy = null;
834+
foreach (var pregnancy in oldCharacter.Pregnancies) {
835+
if (lastPregnancy is null || pregnancy.BirthDate > lastPregnancy.BirthDate) {
836+
lastPregnancy = pregnancy;
837+
}
838+
}
839839
if (lastPregnancy is not null) {
840840
oldCharacter.DeathDate = lastPregnancy.BirthDate.ChangeByMonths(monthsToLive);
841841
return;
@@ -845,8 +845,8 @@ private void SetUpDeathDateForCharacterWithoutTitles(Character oldCharacter, Dat
845845
oldCharacter.DeathDate = irSaveDate.ChangeByMonths(monthsToLive);
846846
}
847847

848-
private void GenerateSuccessorsForCharacter(Character oldCharacter, IReadOnlyDictionary<string, Title[]> titlesByHolderId,
849-
IReadOnlyDictionary<string, string[]> cultureIdToMaleNames, Date irSaveDate, Date ck3BookmarkDate, ulong randomSeed)
848+
private void GenerateSuccessorsForCharacter(Character oldCharacter, Dictionary<string, List<Title>> titlesByHolderId,
849+
Dictionary<string, string[]> cultureIdToMaleNames, Date irSaveDate, Date ck3BookmarkDate, ulong randomSeed)
850850
{
851851
// Get all titles held by the character.
852852
var heldTitles = titlesByHolderId[oldCharacter.Id];

0 commit comments

Comments
 (0)