16
16
using ImperatorToCK3 . Mappers . Trait ;
17
17
using ImperatorToCK3 . Mappers . UnitType ;
18
18
using Open . Collections ;
19
+ using System ;
19
20
using System . Collections . Generic ;
20
21
using System . Linq ;
21
22
using System . Text . RegularExpressions ;
@@ -42,24 +43,33 @@ Configuration config
42
43
43
44
var unlocalizedImperatorNames = new ConcurrentHashSet < string > ( ) ;
44
45
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
+ }
62
71
} ) ;
72
+
63
73
if ( unlocalizedImperatorNames . Any ( ) ) {
64
74
Logger . Warn ( "Found unlocalized Imperator names: " + string . Join ( ", " , unlocalizedImperatorNames ) ) ;
65
75
}
@@ -439,13 +449,20 @@ private static IEnumerable<string> LoadCharacterIDsToPreserve() {
439
449
440
450
public void PurgeUnneededCharacters ( Title . LandedTitles titles , DynastyCollection dynasties , HouseCollection houses , Date ck3BookmarkDate ) {
441
451
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 ) ;
445
455
var landedCharacters = this
446
- . Where ( character => landedCharacterIds . Contains ( character . Id ) )
456
+ . Where ( character => currentTitleHolderIds . Contains ( character . Id ) )
447
457
. ToArray ( ) ;
448
458
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 ) ;
449
466
450
467
// Don't purge animation_test or easter egg characters.
451
468
charactersToCheck = charactersToCheck
@@ -457,12 +474,11 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
457
474
458
475
// Make some exceptions for characters referenced in game's script files.
459
476
var characterIdsToKeep = LoadCharacterIDsToPreserve ( ) ;
460
-
461
477
charactersToCheck = charactersToCheck
462
478
. Where ( character => ! characterIdsToKeep . Contains ( character . Id ) )
463
479
. ToArray ( ) ;
464
480
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.
466
482
var dynastyIdsOfLandedCharacters = landedCharacters
467
483
. Select ( character => character . GetDynastyId ( ck3BookmarkDate ) )
468
484
. Distinct ( )
@@ -493,8 +509,8 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
493
509
494
510
// See who can be removed.
495
511
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 ) ) ) {
498
514
// Is the character dead and childless? Purge.
499
515
if ( ! parentIdsCache . Contains ( character . Id ) ) {
500
516
charactersToRemove . Add ( character ) ;
@@ -517,6 +533,9 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
517
533
houses . PurgeUnneededHouses ( this , ck3BookmarkDate ) ;
518
534
dynasties . PurgeUnneededDynasties ( this , houses , ck3BookmarkDate ) ;
519
535
dynasties . FlattenDynastiesWithNoFounders ( this , houses , ck3BookmarkDate ) ;
536
+
537
+ // Clean up title history.
538
+ titles . RemoveInvalidHoldersFromHistory ( this ) ;
520
539
}
521
540
522
541
public void RemoveEmployerIdFromLandedCharacters ( Title . LandedTitles titles , Date conversionDate ) {
0 commit comments