-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCharacterCollection.cs
More file actions
1076 lines (925 loc) · 37.3 KB
/
CharacterCollection.cs
File metadata and controls
1076 lines (925 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using commonItems;
using commonItems.Collections;
using commonItems.Localization;
using ImperatorToCK3.CK3.Armies;
using ImperatorToCK3.CK3.Modifiers;
using ImperatorToCK3.CK3.Cultures;
using ImperatorToCK3.CK3.Dynasties;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.CommonUtils;
using ImperatorToCK3.CommonUtils.Map;
using ImperatorToCK3.Imperator.Armies;
using ImperatorToCK3.Imperator.Provinces;
using ImperatorToCK3.Imperator.Religions;
using ImperatorToCK3.Imperator.Characters;
using ImperatorToCK3.Imperator.Countries;
using ImperatorToCK3.Mappers.Artifact;
using ImperatorToCK3.Mappers.Culture;
using ImperatorToCK3.Mappers.DeathReason;
using ImperatorToCK3.Mappers.Modifier;
using ImperatorToCK3.Mappers.Nickname;
using ImperatorToCK3.Mappers.Province;
using ImperatorToCK3.Mappers.Religion;
using ImperatorToCK3.Mappers.Trait;
using ImperatorToCK3.Mappers.UnitType;
using Open.Collections;
using System;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ImperatorToCK3.CK3.Characters;
internal sealed partial class CharacterCollection : ConcurrentIdObjectCollection<string, Character> {
internal void ImportImperatorCharacters(
Imperator.World impWorld,
ReligionMapper religionMapper,
CultureMapper cultureMapper,
CultureCollection ck3Cultures,
TraitMapper traitMapper,
NicknameMapper nicknameMapper,
ProvinceMapper provinceMapper,
DeathReasonMapper deathReasonMapper,
DNAFactory dnaFactory,
CK3LocDB ck3LocDB,
Date conversionDate,
Configuration config
) {
Logger.Info("Importing Imperator Characters...");
var unlocalizedImperatorNames = new ConcurrentHashSet<string>();
var nameOverrides = GetImperatorCharacterNameOverrides();
var parallelOptions = new ParallelOptions {
MaxDegreeOfParallelism = Environment.ProcessorCount - 1,
};
Parallel.ForEach(impWorld.Characters, parallelOptions, irCharacter => {
ImportImperatorCharacter(
irCharacter,
religionMapper,
cultureMapper,
traitMapper,
nicknameMapper,
impWorld.LocDB,
ck3LocDB,
impWorld.MapData,
provinceMapper,
deathReasonMapper,
conversionDate,
config,
nameOverrides,
unlocalizedImperatorNames
);
});
if (unlocalizedImperatorNames.Any()) {
Logger.Warn("Found unlocalized Imperator names: " + string.Join(", ", unlocalizedImperatorNames));
}
Logger.Info($"Imported {impWorld.Characters.Count} characters.");
LinkMothersAndFathers();
LinkSpouses(conversionDate);
LinkPrisoners(conversionDate);
ImportFriendships(impWorld.Characters, conversionDate);
ImportRivalries(impWorld.Characters, conversionDate);
Logger.IncrementProgress();
ImportPregnancies(impWorld.Characters, conversionDate);
if (config.FallenEagleEnabled) {
SetCharacterCastes(ck3Cultures, config.CK3BookmarkDate);
}
}
private static FrozenDictionary<string, string> GetImperatorCharacterNameOverrides() {
const string configurablePath = "configurables/character_name_overrides.txt";
if (!File.Exists(configurablePath)) {
Logger.Warn($"{configurablePath} not found, will not override any Imperator character names.");
return FrozenDictionary<string, string>.Empty;
}
var reader = new BufferedReader(File.ReadAllText(configurablePath));
return reader.GetAssignments().ToFrozenDictionary();
}
private void ImportImperatorCharacter(
Imperator.Characters.Character irCharacter,
ReligionMapper religionMapper,
CultureMapper cultureMapper,
TraitMapper traitMapper,
NicknameMapper nicknameMapper,
LocDB irLocDB,
CK3LocDB ck3LocDB,
MapData irMapData,
ProvinceMapper provinceMapper,
DeathReasonMapper deathReasonMapper,
Date endDate,
Configuration config,
FrozenDictionary<string, string> nameOverrides,
ConcurrentHashSet<string> unlocalizedImperatorNames) {
// Create a new CK3 character.
var newCharacter = new Character(
irCharacter,
this,
religionMapper,
cultureMapper,
traitMapper,
nicknameMapper,
irLocDB,
ck3LocDB,
irMapData,
provinceMapper,
deathReasonMapper,
endDate,
config,
nameOverrides,
unlocalizedImperatorNames
);
irCharacter.CK3Character = newCharacter;
AddOrReplace(newCharacter);
}
public override void Remove(string key) {
BulkRemove([key]);
}
private void BulkRemove(ICollection<string> keys) {
foreach (var key in keys) {
var characterToRemove = this[key];
characterToRemove.RemoveAllSpouses();
characterToRemove.RemoveAllConcubines();
characterToRemove.RemoveAllChildren();
var irCharacter = characterToRemove.ImperatorCharacter;
if (irCharacter is not null) {
irCharacter.CK3Character = null;
}
base.Remove(key);
}
RemoveCharacterReferencesFromHistory(keys);
}
private void RemoveCharacterReferencesFromHistory(ICollection<string> idsToRemove) {
var idsCapturingGroup = "(" + string.Join('|', idsToRemove) + ")";
// Effects like "break_alliance = character:ID" entries should be removed.
const string commandsGroup = "(break_alliance|make_concubine)";
var simpleCommandsRegex = new Regex(commandsGroup + @"\s*=\s*character:" + idsCapturingGroup + @"\s*\b");
foreach (var character in this) {
var effectsHistoryField = character.History.Fields["effects"];
if (effectsHistoryField is not LiteralHistoryField effectsLiteralField) {
Logger.Warn($"Effects history field for character {character.Id} is not a literal field!");
continue;
}
if (effectsLiteralField.EntriesCount == 0) {
continue;
}
effectsLiteralField.RegexReplaceAllEntries(simpleCommandsRegex, string.Empty);
// Remove all empty effect blocks (effect = { }).
effectsHistoryField.RemoveAllEntries(entryValue => {
if (entryValue is not string valueString) {
return false;
}
string trimmedBlock = valueString.Trim();
if (!trimmedBlock.StartsWith('{') || !trimmedBlock.EndsWith('}')) {
return false;
}
return trimmedBlock[1..^1].Trim().Length == 0;
});
}
}
private void LinkMothersAndFathers() {
var motherCounter = 0;
var fatherCounter = 0;
foreach (var ck3Character in this) {
// make links between Imperator characters
if (ck3Character.ImperatorCharacter is null) {
// imperatorRegnal characters do not have ImperatorCharacter
continue;
}
var irMotherCharacter = ck3Character.ImperatorCharacter.Mother;
if (irMotherCharacter is not null) {
var ck3MotherCharacter = irMotherCharacter.CK3Character;
if (ck3MotherCharacter is not null) {
ck3Character.Mother = ck3MotherCharacter;
++motherCounter;
} else {
Logger.Warn($"Imperator mother {irMotherCharacter.Id} has no CK3 character!");
}
}
// make links between Imperator characters
var irFatherCharacter = ck3Character.ImperatorCharacter.Father;
if (irFatherCharacter is not null) {
var ck3FatherCharacter = irFatherCharacter.CK3Character;
if (ck3FatherCharacter is not null) {
ck3Character.Father = ck3FatherCharacter;
++fatherCounter;
} else {
Logger.Warn($"Imperator father {irFatherCharacter.Id} has no CK3 character!");
}
}
}
Logger.Info($"{motherCounter} mothers and {fatherCounter} fathers linked in CK3.");
}
private void LinkSpouses(Date conversionDate) {
var spouseCounter = 0;
foreach (var ck3Character in this) {
if (ck3Character.Female) {
continue; // we set spouses for males to avoid doubling marriages
}
// make links between Imperator characters
if (ck3Character.ImperatorCharacter is null) {
// imperatorRegnal characters do not have ImperatorCharacter
continue;
}
foreach (var impSpouseCharacter in ck3Character.ImperatorCharacter.Spouses.Values) {
var ck3SpouseCharacter = impSpouseCharacter.CK3Character;
if (ck3SpouseCharacter is null) {
Logger.Warn($"Imperator spouse {impSpouseCharacter.Id} has no CK3 character!");
continue;
}
// Imperator saves don't seem to store marriage date
Date estimatedMarriageDate = GetEstimatedMarriageDate(ck3Character.ImperatorCharacter, impSpouseCharacter);
ck3Character.AddSpouse(estimatedMarriageDate, ck3SpouseCharacter);
++spouseCounter;
}
}
Logger.Info($"{spouseCounter} spouses linked in CK3.");
Date GetEstimatedMarriageDate(Imperator.Characters.Character imperatorCharacter, Imperator.Characters.Character imperatorSpouse) {
// Imperator saves don't seem to store marriage date.
var marriageDeathDate = GetMarriageDeathDate(imperatorCharacter, imperatorSpouse);
var birthDateOfCommonChild = GetBirthDateOfFirstCommonChild(imperatorCharacter, imperatorSpouse);
if (birthDateOfCommonChild is not null) {
// We assume the child was conceived after marriage.
var estimatedConceptionDate = birthDateOfCommonChild.Value.ChangeByDays(-280);
if (marriageDeathDate is not null && marriageDeathDate < estimatedConceptionDate) {
estimatedConceptionDate = marriageDeathDate.Value.ChangeByDays(-1);
}
return estimatedConceptionDate;
}
if (marriageDeathDate is not null) {
return marriageDeathDate.Value.ChangeByDays(-1); // Death is not a good moment to marry.
}
return conversionDate;
}
Date? GetBirthDateOfFirstCommonChild(Imperator.Characters.Character father, Imperator.Characters.Character mother) {
var childrenOfFather = father.Children.Values.ToFrozenSet();
var childrenOfMother = mother.Children.Values.ToFrozenSet();
var commonChildren = childrenOfFather.Intersect(childrenOfMother).OrderBy(child => child.BirthDate).ToArray();
Date? firstChildBirthDate = commonChildren.Length > 0 ? commonChildren.FirstOrDefault()?.BirthDate : null;
if (firstChildBirthDate is not null) {
return firstChildBirthDate;
}
var unborns = mother.Unborns.Where(u => u.FatherId == father.Id).OrderBy(u => u.BirthDate).ToArray();
return unborns.FirstOrDefault()?.BirthDate;
}
Date? GetMarriageDeathDate(Imperator.Characters.Character husband, Imperator.Characters.Character wife) {
if (husband.DeathDate is not null && wife.DeathDate is not null) {
return husband.DeathDate < wife.DeathDate ? husband.DeathDate : wife.DeathDate;
}
return husband.DeathDate ?? wife.DeathDate;
}
}
private void LinkPrisoners(Date date) {
var prisonerCount = this.Count(character => character.LinkJailor(date));
Logger.Info($"{prisonerCount} prisoners linked with jailors in CK3.");
}
private static void ImportFriendships(Imperator.Characters.CharacterCollection irCharacters, Date conversionDate) {
Logger.Info("Importing friendships...");
foreach (var irCharacter in irCharacters) {
var ck3Character = irCharacter.CK3Character;
if (ck3Character is null) {
Logger.Warn($"Imperator character {irCharacter.Id} has no CK3 character!");
continue;
}
foreach (var irFriendId in irCharacter.FriendIds) {
// Make sure to only add this relation once.
if (irCharacter.Id.CompareTo(irFriendId) > 0) {
continue;
}
var irFriend = irCharacters[irFriendId];
var ck3Friend = irFriend.CK3Character;
if (ck3Friend is not null) {
var effectStr = $"{{ set_relation_friend={{ reason=friend_generic_history target=character:{ck3Friend.Id} }} }}";
ck3Character.History.AddFieldValue(conversionDate, "effects", "effect", effectStr);
} else {
Logger.Warn($"Imperator friend {irFriendId} has no CK3 character!");
}
}
}
}
private static void ImportRivalries(Imperator.Characters.CharacterCollection irCharacters, Date conversionDate) {
Logger.Info("Importing rivalries...");
foreach (var irCharacter in irCharacters) {
var ck3Character = irCharacter.CK3Character;
if (ck3Character is null) {
Logger.Warn($"Imperator character {irCharacter.Id} has no CK3 character!");
continue;
}
foreach (var irRivalId in irCharacter.RivalIds) {
// Make sure to only add this relation once.
if (irCharacter.Id.CompareTo(irRivalId) > 0) {
continue;
}
var irRival = irCharacters[irRivalId];
var ck3Rival = irRival.CK3Character;
if (ck3Rival is not null) {
var effectStr = $"{{ set_relation_rival={{ reason=rival_historical target=character:{ck3Rival.Id} }} }}";
ck3Character.History.AddFieldValue(conversionDate, "effects", "effect", effectStr);
} else {
Logger.Warn($"Imperator rival {irRivalId} has no CK3 character!");
}
}
}
}
private void ImportPregnancies(Imperator.Characters.CharacterCollection imperatorCharacters, Date conversionDate) {
Logger.Info("Importing pregnancies...");
foreach (var female in this.Where(c => c.Female)) {
var imperatorFemale = female.ImperatorCharacter;
if (imperatorFemale is null) {
continue;
}
foreach (var unborn in imperatorFemale.Unborns) {
var conceptionDate = unborn.EstimatedConceptionDate;
// in CK3 the make_pregnant effect used in character history is executed on game start, so
// it only makes sense to convert pregnancies that lasted around 3 months or less
// (longest recorded pregnancy was around 12 months)
var pregnancyLength = conversionDate.DiffInYears(conceptionDate);
if (pregnancyLength > 0.25) {
continue;
}
if (!imperatorCharacters.TryGetValue(unborn.FatherId, out var imperatorFather)) {
continue;
}
var ck3Father = imperatorFather.CK3Character;
if (ck3Father is null) {
continue;
}
female.Pregnancies.Add(new(ck3Father.Id, female.Id, unborn.BirthDate, unborn.IsBastard));
}
}
Logger.IncrementProgress();
}
private void SetCharacterCastes(CultureCollection cultures, Date ck3BookmarkDate) {
var casteSystemCultureIds = cultures
.Where(c => c.TraditionIds.Contains("tradition_caste_system"))
.Select(c => c.Id)
.ToFrozenSet();
var learningEducationTraits = new[]{"education_learning_1", "education_learning_2", "education_learning_3", "education_learning_4"};
foreach (var character in this.OrderBy(c => c.BirthDate)) {
if (character.ImperatorCharacter is null) {
continue;
}
var cultureId = character.GetCultureId(ck3BookmarkDate);
if (cultureId is null || !casteSystemCultureIds.Contains(cultureId)) {
continue;
}
// The caste is hereditary.
var father = character.Father;
if (father is not null) {
var foundTrait = GetCasteTraitFromParent(father);
if (foundTrait is not null) {
character.AddBaseTrait(foundTrait);
continue;
}
}
var mother = character.Mother;
if (mother is not null) {
var foundTrait = GetCasteTraitFromParent(mother);
if (foundTrait is not null) {
character.AddBaseTrait(foundTrait);
continue;
}
}
// Try to set caste based on character's traits.
var traitIds = character.BaseTraits.ToFrozenSet();
character.AddBaseTrait(traitIds.Intersect(learningEducationTraits).Any() ? "brahmin" : "kshatriya");
}
return;
static string? GetCasteTraitFromParent(Character parentCharacter) {
var casteTraits = new[]{"brahmin", "kshatriya", "vaishya", "shudra"};
var parentTraitIds = parentCharacter.BaseTraits.ToFrozenSet();
return casteTraits.Intersect(parentTraitIds).FirstOrDefault();
}
}
public void LoadCharacterIDsToPreserve(Date ck3BookmarkDate) {
Logger.Debug("Loading IDs of CK3 characters to preserve...");
const string configurablePath = "configurables/ck3_characters_to_preserve.txt";
var parser = new Parser();
parser.RegisterKeyword("keep_as_is", reader => {
var ids = reader.GetStrings();
foreach (var id in ids) {
if (!TryGetValue(id, out var character)) {
continue;
}
character.IsNonRemovable = true;
}
});
parser.RegisterKeyword("after_bookmark_date", reader => {
var ids = reader.GetStrings();
foreach (var id in ids) {
if (!TryGetValue(id, out var character)) {
continue;
}
character.IsNonRemovable = true;
character.BirthDate = ck3BookmarkDate.ChangeByDays(1);
character.DeathDate = ck3BookmarkDate.ChangeByDays(2);
// Remove all dated history entries other than birth and death.
foreach (var field in character.History.Fields) {
if (field.Id == "birth" || field.Id == "death") {
continue;
}
field.DateToEntriesDict.Clear();
}
}
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseFile(configurablePath);
}
public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection dynasties, HouseCollection houses, Date ck3BookmarkDate) {
Logger.Info("Purging unneeded characters...");
// Characters from I:R should be kept (the unimportant ones have already been purged during I:R processing).
var charactersToCheck = this.Where(c => !c.FromImperator);
// Characters from CK3 that hold titles at the bookmark date should be kept.
var currentTitleHolderIds = titles.GetHolderIdsForAllTitlesExceptNobleFamilyTitles(ck3BookmarkDate);
var landedCharacters = this
.Where(character => currentTitleHolderIds.Contains(character.Id))
.ToArray();
charactersToCheck = charactersToCheck.Except(landedCharacters);
// Don't purge animation_test characters.
charactersToCheck = charactersToCheck
.Where(c => !c.Id.StartsWith("animation_test_"));
// Make some exceptions for characters referenced in game's script files.
charactersToCheck = charactersToCheck
.Where(character => !character.IsNonRemovable)
.ToArray();
// Members of landed dynasties will be preserved, unless dead and childless.
var dynastyIdsOfLandedCharacters = landedCharacters
.Select(character => character.GetDynastyId(ck3BookmarkDate))
.Distinct()
.Where(id => id is not null)
.ToFrozenSet();
var i = 0;
var charactersToRemove = new List<Character>();
var parentIdsCache = new HashSet<string>();
do {
Logger.Debug($"Beginning iteration {i} of characters purge...");
charactersToRemove.Clear();
parentIdsCache.Clear();
++i;
// Build cache of all parent IDs.
foreach (var character in this) {
var motherId = character.MotherId;
if (motherId is not null) {
parentIdsCache.Add(motherId);
}
var fatherId = character.FatherId;
if (fatherId is not null) {
parentIdsCache.Add(fatherId);
}
}
// See who can be removed.
foreach (var character in charactersToCheck) {
// Does the character belong to a dynasty that holds or held titles?
if (dynastyIdsOfLandedCharacters.Contains(character.GetDynastyId(ck3BookmarkDate))) {
// Is the character dead and childless? Purge.
if (!parentIdsCache.Contains(character.Id)) {
charactersToRemove.Add(character);
}
continue;
}
charactersToRemove.Add(character);
}
BulkRemove(charactersToRemove.ConvertAll(c => c.Id));
Logger.Debug($"\tPurged {charactersToRemove.Count} unneeded characters in iteration {i}.");
charactersToCheck = charactersToCheck.Except(charactersToRemove).ToArray();
} while (charactersToRemove.Count > 0);
// At this point we probably have many dynasties with no characters left.
// Let's purge them.
houses.PurgeUnneededHouses(this, ck3BookmarkDate);
dynasties.PurgeUnneededDynasties(this, houses, ck3BookmarkDate);
dynasties.FlattenDynastiesWithNoFounders(this, houses, ck3BookmarkDate);
}
public void RemoveEmployerIdFromLandedCharacters(Title.LandedTitles titles, Date conversionDate) {
Logger.Info("Removing employer id from landed characters...");
var landedCharacterIds = titles.GetHolderIdsForAllTitlesExceptNobleFamilyTitles(conversionDate);
foreach (var character in this.Where(character => landedCharacterIds.Contains(character.Id))) {
character.History.Fields["employer"].RemoveAllEntries();
}
Logger.IncrementProgress();
}
/// <summary>
/// Distributes Imperator countries' gold among rulers and governors
/// </summary>
/// <param name="titles">Landed titles collection</param>
/// <param name="config">Current configuration</param>
public void DistributeCountriesGold(Title.LandedTitles titles, Configuration config) {
static void AddGoldToCharacter(Character character, float gold) {
if (character.Gold is null) {
character.Gold = gold;
} else {
character.Gold += gold;
}
}
Logger.Info("Distributing countries' gold...");
var bookmarkDate = config.CK3BookmarkDate;
var ck3CountriesFromImperator = titles.GetCountriesImportedFromImperator();
foreach (var ck3Country in ck3CountriesFromImperator) {
var rulerId = ck3Country.GetHolderId(bookmarkDate);
if (rulerId == "0") {
Logger.Debug($"Can't distribute gold in {ck3Country} because it has no holder.");
continue;
}
var imperatorGold = ck3Country.ImperatorCountry!.Currencies.Gold * config.ImperatorCurrencyRate;
var vassalCharacterIds = ck3Country.GetDeFactoVassals(bookmarkDate).Values
.Where(vassalTitle => !vassalTitle.Landless)
.Select(vassalTitle => vassalTitle.GetHolderId(bookmarkDate))
.ToFrozenSet();
var vassalCharacters = new HashSet<Character>();
foreach (var vassalCharacterId in vassalCharacterIds) {
if (TryGetValue(vassalCharacterId, out var vassalCharacter)) {
vassalCharacters.Add(vassalCharacter);
} else {
Logger.Warn($"Character {vassalCharacterId} not found!");
}
}
// Ruler should also get a share, he has double weight, so we add 2 to the count.
var mouthsToFeedCount = vassalCharacters.Count + 2;
var goldPerVassal = imperatorGold / mouthsToFeedCount;
foreach (var vassalCharacter in vassalCharacters) {
AddGoldToCharacter(vassalCharacter, goldPerVassal);
imperatorGold -= goldPerVassal;
}
var ruler = this[rulerId];
AddGoldToCharacter(ruler, imperatorGold);
}
Logger.IncrementProgress();
}
internal void ImportLegions(
Title.LandedTitles titles,
UnitCollection imperatorUnits,
Imperator.Characters.CharacterCollection imperatorCharacters,
CountryCollection irCountries,
Date date,
UnitTypeMapper unitTypeMapper,
IdObjectCollection<string, MenAtArmsType> menAtArmsTypes,
ProvinceMapper provinceMapper,
CK3LocDB ck3LocDB,
Configuration config
) {
Logger.Info("Importing Imperator armies...");
var ck3CountriesFromImperator = titles.GetCountriesImportedFromImperator();
foreach (var ck3Country in ck3CountriesFromImperator) {
var rulerId = ck3Country.GetHolderId(date);
if (rulerId == "0") {
Logger.Debug($"Can't add armies to {ck3Country} because it has no holder.");
continue;
}
var imperatorCountry = ck3Country.ImperatorCountry!;
var countryLegions = imperatorUnits.Where(u => u.CountryId == imperatorCountry.Id && u.IsArmy && u.IsLegion).ToArray();
if (countryLegions.Length == 0) {
continue;
}
var ruler = this[rulerId];
if (config.LegionConversion == LegionConversion.MenAtArms) {
ruler.ImportUnitsAsMenAtArms(countryLegions, date, unitTypeMapper, menAtArmsTypes, ck3LocDB);
} else if (config.LegionConversion == LegionConversion.SpecialTroops) {
ruler.ImportUnitsAsSpecialTroops(countryLegions, imperatorCharacters, irCountries, date, unitTypeMapper, provinceMapper, ck3LocDB);
}
}
Logger.IncrementProgress();
}
public void ImportArtifacts(
ProvinceCollection irProvinces,
ProvinceMapper provinceMapper,
Title.LandedTitles titles,
TreasureManager treasureManager,
ModifierMapper modifierMapper,
ModifierCollection ck3Modifiers,
LocDB irLocDB,
CK3LocDB ck3LocDB,
Date date
) {
Logger.Info("Importing Imperator artifacts...");
var ck3CharacterIdToTreasureIdsListDict = new Dictionary<string, IList<ulong>>();
foreach (var irProvince in irProvinces) {
var ck3Provinces = provinceMapper.GetCK3ProvinceNumbers(irProvince.Id);
if (ck3Provinces.Count == 0) {
continue;
}
var primaryCK3ProvinceId = ck3Provinces.First();
string? ownerId = null;
var barony = titles.GetBaronyForProvince(primaryCK3ProvinceId);
if (barony is null) {
Logger.Warn($"Can't find barony for province {primaryCK3ProvinceId}!");
continue;
}
var baronyHolderId = barony.GetHolderId(date);
if (baronyHolderId != "0") {
ownerId = baronyHolderId;
} else {
var county = titles.GetCountyForProvince(primaryCK3ProvinceId);
if (county is null) {
Logger.Warn($"Can't find county for province {primaryCK3ProvinceId}!");
continue;
}
var countyHolderId = county.GetHolderId(date);
if (countyHolderId != "0") {
ownerId = countyHolderId;
}
}
if (ownerId is null) {
Logger.Warn($"Can't find owner for province {primaryCK3ProvinceId}!");
continue;
}
if (ck3CharacterIdToTreasureIdsListDict.TryGetValue(ownerId, out var artifactList)) {
artifactList.AddRange(irProvince.TreasureIds);
} else {
ck3CharacterIdToTreasureIdsListDict[ownerId] = irProvince.TreasureIds.ToList();
}
}
// TODO: also import artifacts not assigned to holy sites (search for treasures={ 225 226 } in save)
// TODO: check if needed: Create visuals for artifacts.
/*var treasureIconNames = treasureManager
.Select(t => t.IconName)
.Distinct()
.ToList();
foreach (var iconName in treasureIconNames) {
throw new NotImplementedException();
// example:
// icon: artefact_icons_unique_artifact_cheese
// asset entity: ep1_western_pouch_basic_01_a_entity
// mesh: ep1_western_pouch_basic_01_a_mesh
}*/
var visualsMapper = new ArtifactMapper("configurables/artifact_map.txt");
var charactersFromImperator = this.Where(c => c.FromImperator).ToList();
foreach (var character in charactersFromImperator) {
if (!ck3CharacterIdToTreasureIdsListDict.TryGetValue(character.Id, out var irArtifactIds)) {
continue;
}
// TODO: try to use create_artifact_sculpture_babr_e_bayan_effect as base
foreach (var irArtifactId in irArtifactIds) {
var irArtifact = treasureManager[irArtifactId];
ImportArtifact(character, irArtifact, modifierMapper, ck3Modifiers, visualsMapper, irLocDB, ck3LocDB, date);
}
/*
* # Create the artifact
create_artifact = {
name = artifact_sculpture_armor_babr_name
description = artifact_sculpture_armor_babr
type = sculpture
template = babr_template
visuals = sculpture_babr_e_bayan
wealth = scope:wealth
quality = scope:quality
history = {
type = created_before_history
}
modifier = babr_e_bayan_modifier
save_scope_as = newly_created_artifact
decaying = yes
}
scope:newly_created_artifact = {
set_variable = { name = historical_unique_artifact value = yes }
set_variable = babr_e_bayan
save_scope_as = epic
}
*/
}
Logger.IncrementProgress();
}
private void ImportArtifact(Character character, Treasure irArtifact, ModifierMapper modifierMapper, ModifierCollection ck3Modifiers, ArtifactMapper artifactMapper, LocDB irLocDB, CK3LocDB ck3LocDB, Date date) {
var visualAndType = artifactMapper.GetVisualAndType(irArtifact.Key, irArtifact.IconName);
if (visualAndType is null) {
Logger.Warn($"Can't find a match for I:R artifact key {irArtifact.Key} and icon {irArtifact.IconName}!");
return;
}
var (ck3Visual, ck3Type) = visualAndType.Value;
var ck3ArtifactName = $"IRToCK3_artifact_{irArtifact.Key}_{irArtifact.Id}";
var irNameLoc = irLocDB.GetLocBlockForKey(irArtifact.Key);
if (irNameLoc is null) {
Logger.Warn($"Can't find name loc for artifact {irArtifact.Key}!");
} else {
var artifactNameLocBlock = ck3LocDB.GetOrCreateLocBlock(ck3ArtifactName);
artifactNameLocBlock.CopyFrom(irNameLoc);
}
var ck3DescKey = $"{ck3ArtifactName}_desc";
var irDescLoc = irLocDB.GetLocBlockForKey(irArtifact.Key + "_desc");
if (irDescLoc is null) {
Logger.Warn($"Can't find description loc for artifact {irArtifact.Key}!");
} else {
var descLocBlock = ck3LocDB.GetOrCreateLocBlock(ck3DescKey);
descLocBlock.CopyFrom(irDescLoc);
}
var artifactScope = $"newly_created_artifact_{irArtifact.Id}";
var ck3ModifierEffects = new Dictionary<string, double>();
foreach (var (irEffect, irEffectValue) in irArtifact.StateModifiers) {
var match = modifierMapper.Match(irEffect, irEffectValue);
if (match is null) {
Logger.Warn($"Can't find CK3 modifier for Imperator modifier {irEffect}!");
continue;
}
ck3ModifierEffects[match.Value.Key] = match.Value.Value;
}
foreach (var (irEffect, irEffectValue) in irArtifact.CharacterModifiers) {
var match = modifierMapper.Match(irEffect, irEffectValue);
if (match is null) {
Logger.Warn($"Can't find CK3 modifier for Imperator modifier {irEffect}!");
continue;
}
ck3ModifierEffects[match.Value.Key] = match.Value.Value;
}
var ck3ModifierId = $"{ck3ArtifactName}_modifier";
ck3Modifiers.Add(new Modifier(ck3ModifierId, ck3ModifierEffects));
string createArtifactEffect = $$"""
set_artifact_rarity_illustrious = yes
create_artifact = {
name = {{ ck3ArtifactName }}
description = {{ ck3DescKey }}
type = {{ ck3Type }}
# template = babr_template # TODO: check if needed
visuals = {{ ck3Visual }}
wealth = scope:wealth
quality = scope:quality
history = {
type = created_before_history
}
modifier = {{ ck3ModifierId }}
save_scope_as = {{ artifactScope }}
decaying = yes
}
scope:{{ artifactScope }} = {
set_variable = {
name = historical_unique_artifact
value = yes
}
}
""";
character.History.AddFieldValue(date, "effects", "effect", createArtifactEffect);
}
public void GenerateSuccessorsForOldCharacters(Title.LandedTitles titles, CultureCollection cultures, Date irSaveDate, Date ck3BookmarkDate, ulong randomSeed) {
Logger.Info("Generating successors for old characters...");
var oldCharacters = this
.Where(c => c.BirthDate < ck3BookmarkDate && c.DeathDate is null && ck3BookmarkDate.DiffInYears(c.BirthDate) > 60).ToArray();
var titleHolderIds = titles.GetHolderIdsForAllTitlesExceptNobleFamilyTitles(ck3BookmarkDate);
var oldTitleHolders = oldCharacters
.Where(c => titleHolderIds.Contains(c.Id))
.ToArray();
// For characters that don't hold any titles, just set up a death date.
var randomForCharactersWithoutTitles = new Random((int)randomSeed);
foreach (var oldCharacter in oldCharacters.Except(oldTitleHolders)) {
// Roll a dice to determine how much longer the character will live.
var monthsToLive = randomForCharactersWithoutTitles.Next(1, 30 * 12); // Can live up to 30 years more.
// If the character is female and pregnant, make sure she doesn't die before the pregnancy ends.
if (oldCharacter is {Female: true, ImperatorCharacter: not null}) {
var lastPregnancy = oldCharacter.Pregnancies.OrderBy(p => p.BirthDate).LastOrDefault();
if (lastPregnancy is not null) {
oldCharacter.DeathDate = lastPregnancy.BirthDate.ChangeByMonths(monthsToLive);
continue;
}
}
oldCharacter.DeathDate = irSaveDate.ChangeByMonths(monthsToLive);
}
ConcurrentDictionary<string, Title[]> titlesByHolderId = new(titles
.Select(t => new {Title = t, HolderId = t.GetHolderId(ck3BookmarkDate)})
.Where(t => t.HolderId != "0")
.GroupBy(t => t.HolderId)
.ToDictionary(g => g.Key, g => g.Select(t => t.Title).ToArray()));
ConcurrentDictionary<string, string[]> cultureIdToMaleNames = new(cultures
.ToDictionary(c => c.Id, c => c.MaleNames.ToArray()));
// For title holders, generate successors and add them to title history.
Parallel.ForEach(oldTitleHolders, oldCharacter => {
// Get all titles held by the character.
var heldTitles = titlesByHolderId[oldCharacter.Id];
string? dynastyId = oldCharacter.GetDynastyId(ck3BookmarkDate);
string? dynastyHouseId = oldCharacter.GetDynastyHouseId(ck3BookmarkDate);
string? faithId = oldCharacter.GetFaithId(ck3BookmarkDate);
string? cultureId = oldCharacter.GetCultureId(ck3BookmarkDate);
string[] maleNames;
if (cultureId is not null) {
maleNames = cultureIdToMaleNames[cultureId];
} else {
Logger.Debug($"Failed to find male names for successors of {oldCharacter.Id}.");
if (oldCharacter.Female) {
maleNames = [oldCharacter.Father?.GetName(ck3BookmarkDate) ?? "Alexander"];
} else {
maleNames = [oldCharacter.GetName(ck3BookmarkDate) ?? "Alexander"];
}
}
var randomSeedForCharacter = randomSeed ^ (oldCharacter.ImperatorCharacter?.Id ?? 0);
var random = new Random((int)randomSeedForCharacter);
int successorCount = 0;
Character currentCharacter = oldCharacter;
Date currentCharacterBirthDate = currentCharacter.BirthDate;
while (ck3BookmarkDate.DiffInYears(currentCharacterBirthDate) >= 90) {
// If the character has living male children, the oldest one will be the successor.
var successorAndBirthDate = currentCharacter.Children
.Where(c => c is {Female: false, DeathDate: null})
.Select(c => new { Character = c, c.BirthDate })
.OrderBy(x => x.BirthDate)
.FirstOrDefault();
Character successor;
Date currentCharacterDeathDate;
Date successorBirthDate;
if (successorAndBirthDate is not null) {
successor = successorAndBirthDate.Character;
successorBirthDate = successorAndBirthDate.BirthDate;
// Roll a dice to determine how much longer the character will live.
// But make sure the successor is at least 16 years old when the old character dies.
var successorAgeAtBookmarkDate = ck3BookmarkDate.DiffInYears(successorBirthDate);
var yearsUntilSuccessorBecomesAnAdult = Math.Max(16 - successorAgeAtBookmarkDate, 0);
var yearsToLive = random.Next((int)Math.Ceiling(yearsUntilSuccessorBecomesAnAdult), 25);
int currentCharacterAge = random.Next(30 + yearsToLive, 80);
currentCharacterDeathDate = currentCharacterBirthDate.ChangeByYears(currentCharacterAge);
// Needs to be after the save date.
if (currentCharacterDeathDate <= irSaveDate) {
currentCharacterDeathDate = irSaveDate.ChangeByDays(1);
}
} else {
// We don't want all the generated successors on the map to have the same birth date.
var yearsUntilHeir = random.Next(1, 5);
// Make the old character live until the heir is at least 16 years old.
var successorAge = random.Next(yearsUntilHeir + 16, 30);
int currentCharacterAge = random.Next(30 + successorAge, 80);
currentCharacterDeathDate = currentCharacterBirthDate.ChangeByYears(currentCharacterAge);
if (currentCharacterDeathDate <= irSaveDate) {
currentCharacterDeathDate = irSaveDate.ChangeByDays(1);
}
// Generate a new successor.
string id = $"irtock3_{oldCharacter.Id}_successor_{successorCount}";
string firstName = maleNames[random.Next(0, maleNames.Length)];
successorBirthDate = currentCharacterDeathDate.ChangeByYears(-successorAge);
successor = new Character(id, firstName, successorBirthDate, this) {FromImperator = true};
Add(successor);
if (currentCharacter.Female) {
successor.Mother = currentCharacter;
} else {
successor.Father = currentCharacter;
}
if (cultureId is not null) {
successor.SetCultureId(cultureId, null);
}
if (faithId is not null) {
successor.SetFaithId(faithId, null);
}
if (dynastyId is not null) {
successor.SetDynastyId(dynastyId, null);
}
if (dynastyHouseId is not null) {
successor.SetDynastyHouseId(dynastyHouseId, null);
}
}
currentCharacter.DeathDate = currentCharacterDeathDate;
// On the old character death date, the successor should inherit all titles.
foreach (var heldTitle in heldTitles) {
heldTitle.SetHolder(successor, currentCharacterDeathDate);
}