1
1
using commonItems ;
2
2
using commonItems . Mods ;
3
3
using Open . Collections . Synchronized ;
4
+ using System . Collections . Generic ;
4
5
using System . Linq ;
5
6
6
7
namespace ImperatorToCK3 . CK3 . Characters ;
@@ -33,9 +34,21 @@ public void LoadCK3Characters(ModFilesystem ck3ModFS, Date bookmarkDate) {
33
34
"set_relation_ward" , "set_relation_mentor" ,
34
35
"add_opinion" , "make_concubine" ,
35
36
] ;
36
- string [ ] fieldsToClear = [ "friends" , "best_friends" , "lovers" , "rivals" , "nemesis" , "primary_title" , "dna" ] ;
37
+ string [ ] fieldsToClear = [
38
+ "friends" , "best_friends" , "lovers" , "rivals" , "nemesis" ,
39
+ "primary_title" , "dna" , "spawn_army" , "add_character_modifier" , "languages" ,
40
+ "claims" ,
41
+ ] ;
37
42
43
+ var femaleCharacterIds = loadedCharacters . Where ( c => c . Female ) . Select ( c => c . Id ) . ToHashSet ( ) ;
44
+ var maleCharacterIds = loadedCharacters . Select ( c => c . Id ) . Except ( femaleCharacterIds ) . ToHashSet ( ) ;
45
+
38
46
foreach ( var character in loadedCharacters ) {
47
+ // Clear some fields we don't need.
48
+ foreach ( var fieldName in fieldsToClear ) {
49
+ character . History . Fields [ fieldName ] . RemoveAllEntries ( ) ;
50
+ }
51
+
39
52
// Remove post-bookmark history except for births and deaths.
40
53
foreach ( var field in character . History . Fields ) {
41
54
if ( field . Id == "birth" || field . Id == "death" ) {
@@ -58,7 +71,9 @@ public void LoadCK3Characters(ModFilesystem ck3ModFS, Date bookmarkDate) {
58
71
deathField . RemoveAllEntries ( ) ;
59
72
deathField . AddEntryToHistory ( deathDate , "death" , value : true ) ;
60
73
}
61
-
74
+
75
+ RemoveInvalidMotherAndFatherEntries ( character , femaleCharacterIds , maleCharacterIds ) ;
76
+
62
77
// Remove dated name changes like 64.10.13 = { name = "Linus" }
63
78
var nameField = character . History . Fields [ "name" ] ;
64
79
nameField . RemoveHistoryPastDate ( birthDate ) ;
@@ -67,14 +82,34 @@ public void LoadCK3Characters(ModFilesystem ck3ModFS, Date bookmarkDate) {
67
82
character . History . Fields [ "effects" ] . RemoveAllEntries (
68
83
entry => irrelevantEffects . Any ( effect => entry . ToString ( ) ? . Contains ( effect ) ?? false ) ) ;
69
84
70
- // Clear some fields we don't need.
71
- foreach ( var fieldName in fieldsToClear ) {
72
- character . History . Fields [ fieldName ] . RemoveAllEntries ( ) ;
73
- }
74
-
75
85
character . InitSpousesCache ( ) ;
76
86
character . InitConcubinesCache ( ) ;
77
87
character . UpdateChildrenCacheOfParents ( ) ;
78
88
}
79
89
}
90
+
91
+ private static void RemoveInvalidMotherAndFatherEntries ( Character character , HashSet < string > femaleCharacterIds , HashSet < string > maleCharacterIds ) {
92
+ // Remove wrong sex mother and father references (male mothers, female fathers).
93
+ var motherField = character . History . Fields [ "mother" ] ;
94
+ motherField . RemoveAllEntries ( value => {
95
+ string ? motherId = value . ToString ( ) ? . RemQuotes ( ) ;
96
+ if ( motherId is null || ! femaleCharacterIds . Contains ( motherId ) ) {
97
+ Logger . Debug ( $ "Removing invalid mother { motherId } from character { character . Id } ") ;
98
+ return true ;
99
+ }
100
+
101
+ return false ;
102
+ } ) ;
103
+
104
+ var fatherField = character . History . Fields [ "father" ] ;
105
+ fatherField . RemoveAllEntries ( value => {
106
+ string ? fatherId = value . ToString ( ) ? . RemQuotes ( ) ;
107
+ if ( fatherId is null || ! maleCharacterIds . Contains ( fatherId ) ) {
108
+ Logger . Debug ( $ "Removing invalid father { fatherId } from character { character . Id } ") ;
109
+ return true ;
110
+ }
111
+
112
+ return false ;
113
+ } ) ;
114
+ }
80
115
}
0 commit comments