Skip to content

Commit ad375fe

Browse files
authored
Purge more characters from CK3 (#2165)
1 parent cddfd31 commit ad375fe

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

ImperatorToCK3/CK3/Characters/CharacterCollection.cs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using ImperatorToCK3.Mappers.Trait;
1717
using ImperatorToCK3.Mappers.UnitType;
1818
using Open.Collections;
19+
using System;
1920
using System.Collections.Generic;
2021
using System.Linq;
2122
using System.Text.RegularExpressions;
@@ -42,24 +43,33 @@ Configuration config
4243

4344
var unlocalizedImperatorNames = new ConcurrentHashSet<string>();
4445

45-
Parallel.ForEach(impWorld.Characters, irCharacter => {
46-
ImportImperatorCharacter(
47-
irCharacter,
48-
religionMapper,
49-
cultureMapper,
50-
traitMapper,
51-
nicknameMapper,
52-
impWorld.LocDB,
53-
ck3LocDB,
54-
impWorld.MapData,
55-
provinceMapper,
56-
deathReasonMapper,
57-
dnaFactory,
58-
conversionDate,
59-
config,
60-
unlocalizedImperatorNames
61-
);
46+
var parallelOptions = new ParallelOptions {
47+
MaxDegreeOfParallelism = Environment.ProcessorCount - 1,
48+
};
49+
Parallel.ForEach(impWorld.Characters, parallelOptions, irCharacter => {
50+
try {
51+
ImportImperatorCharacter(
52+
irCharacter,
53+
religionMapper,
54+
cultureMapper,
55+
traitMapper,
56+
nicknameMapper,
57+
impWorld.LocDB,
58+
ck3LocDB,
59+
impWorld.MapData,
60+
provinceMapper,
61+
deathReasonMapper,
62+
dnaFactory,
63+
conversionDate,
64+
config,
65+
unlocalizedImperatorNames
66+
);
67+
} catch (Exception e) {
68+
Logger.Error($"Exception while importing Imperator character {irCharacter.Id}: {e}");
69+
Logger.Debug("Exception stack trace: " + e.StackTrace);
70+
}
6271
});
72+
6373
if (unlocalizedImperatorNames.Any()) {
6474
Logger.Warn("Found unlocalized Imperator names: " + string.Join(", ", unlocalizedImperatorNames));
6575
}
@@ -439,13 +449,20 @@ private static IEnumerable<string> LoadCharacterIDsToPreserve() {
439449

440450
public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection dynasties, HouseCollection houses, Date ck3BookmarkDate) {
441451
Logger.Info("Purging unneeded characters...");
442-
443-
// Characters that hold or held titles should always be kept.
444-
var landedCharacterIds = titles.GetAllHolderIds();
452+
453+
// Characters from CK3 that hold titles at the bookmark date should be kept.
454+
var currentTitleHolderIds = titles.GetHolderIds(ck3BookmarkDate);
445455
var landedCharacters = this
446-
.Where(character => landedCharacterIds.Contains(character.Id))
456+
.Where(character => currentTitleHolderIds.Contains(character.Id))
447457
.ToArray();
448458
var charactersToCheck = this.Except(landedCharacters);
459+
460+
// Characters from I:R that held or hold titles should be kept.
461+
var allTitleHolderIds = titles.GetAllHolderIds();
462+
var imperatorTitleHolders = this
463+
.Where(character => character.FromImperator && allTitleHolderIds.Contains(character.Id))
464+
.ToArray();
465+
charactersToCheck = charactersToCheck.Except(imperatorTitleHolders);
449466

450467
// Don't purge animation_test or easter egg characters.
451468
charactersToCheck = charactersToCheck
@@ -457,12 +474,11 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
457474

458475
// Make some exceptions for characters referenced in game's script files.
459476
var characterIdsToKeep = LoadCharacterIDsToPreserve();
460-
461477
charactersToCheck = charactersToCheck
462478
.Where(character => !characterIdsToKeep.Contains(character.Id))
463479
.ToArray();
464480

465-
// Members of landed dynasties will be preserved, unless dead and childless.
481+
// I:R members of landed dynasties will be preserved, unless dead and childless.
466482
var dynastyIdsOfLandedCharacters = landedCharacters
467483
.Select(character => character.GetDynastyId(ck3BookmarkDate))
468484
.Distinct()
@@ -493,8 +509,8 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
493509

494510
// See who can be removed.
495511
foreach (var character in charactersToCheck) {
496-
// Does the character belong to a dynasty that holds or held titles?
497-
if (dynastyIdsOfLandedCharacters.Contains(character.GetDynastyId(ck3BookmarkDate))) {
512+
// Is the character from Imperator and do they belong to a dynasty that holds or held titles?
513+
if (character.FromImperator && dynastyIdsOfLandedCharacters.Contains(character.GetDynastyId(ck3BookmarkDate))) {
498514
// Is the character dead and childless? Purge.
499515
if (!parentIdsCache.Contains(character.Id)) {
500516
charactersToRemove.Add(character);
@@ -517,6 +533,9 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
517533
houses.PurgeUnneededHouses(this, ck3BookmarkDate);
518534
dynasties.PurgeUnneededDynasties(this, houses, ck3BookmarkDate);
519535
dynasties.FlattenDynastiesWithNoFounders(this, houses, ck3BookmarkDate);
536+
537+
// Clean up title history.
538+
titles.RemoveInvalidHoldersFromHistory(this);
520539
}
521540

522541
public void RemoveEmployerIdFromLandedCharacters(Title.LandedTitles titles, Date conversionDate) {

ImperatorToCK3/CK3/Titles/LandedTitles.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,26 @@ public override void Remove(string name) {
250250
return baronies.FirstOrDefault(b => provinceId == b?.ProvinceId, defaultValue: null);
251251
}
252252

253-
public HashSet<string> GetHolderIds(Date date) {
254-
return new HashSet<string>(this.Select(t => t.GetHolderId(date)));
253+
public ImmutableHashSet<string> GetHolderIds(Date date) {
254+
return this.Select(t => t.GetHolderId(date)).ToImmutableHashSet();
255255
}
256-
public HashSet<string> GetAllHolderIds() {
257-
return this.SelectMany(t => t.GetAllHolderIds()).ToHashSet();
256+
public ImmutableHashSet<string> GetAllHolderIds() {
257+
return this.SelectMany(t => t.GetAllHolderIds()).ToImmutableHashSet();
258+
}
259+
260+
public void RemoveInvalidHoldersFromHistory(CharacterCollection characters) {
261+
Logger.Debug("Removing invalid holders from history...");
262+
263+
var validIds = characters.Select(c => c.Id).ToImmutableHashSet();
264+
foreach (var title in this) {
265+
if (!title.History.Fields.TryGetValue("holder", out var holderField)) {
266+
continue;
267+
}
268+
269+
holderField.RemoveAllEntries(
270+
value => value.ToString() is string valStr && valStr != "0" && !validIds.Contains(valStr)
271+
);
272+
}
258273
}
259274

260275
public void ImportImperatorCountries(

ImperatorToCK3/Imperator/World.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private void LaunchImperatorToExportCountryFlags(Configuration config) {
174174
Arguments = "-continuelastsave -debug_mode",
175175
CreateNoWindow = true,
176176
RedirectStandardOutput = true,
177-
WindowStyle = ProcessWindowStyle.Hidden
177+
WindowStyle = ProcessWindowStyle.Hidden,
178178
};
179179
var imperatorProcess = Process.Start(processStartInfo);
180180
if (imperatorProcess is null) {

0 commit comments

Comments
 (0)