Skip to content

Commit

Permalink
Tweaks for vanilla CK3 compatibility (#2212) #patch
Browse files Browse the repository at this point in the history
closes #2168 
closes #2135

---------

Co-authored-by: codefactor-io <[email protected]>
  • Loading branch information
IhateTrains and code-factor authored Sep 22, 2024
1 parent d036aac commit 59d25c1
Show file tree
Hide file tree
Showing 13 changed files with 3,101 additions and 1,082 deletions.
7 changes: 5 additions & 2 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace ImperatorToCK3.CK3.Characters;
public sealed class Character : IIdentifiable<string> {
public string Id { get; }
public bool FromImperator { get; init; } = false;

// A flag that marks the character as non-removable.
public bool IsNonRemovable { get; set; } = false;

public bool Female {
get {
Expand Down Expand Up @@ -92,7 +95,7 @@ public string GetAgeSex(Date date) {

public Date BirthDate {
get => History.Fields["birth"].DateToEntriesDict.First().Key;
init {
set {
var field = History.Fields["birth"];
field.RemoveAllEntries();
field.AddEntryToHistory(value, "birth", true);
Expand All @@ -104,7 +107,7 @@ public Date? DeathDate {
var entriesDict = History.Fields["death"].DateToEntriesDict;
return entriesDict.Count == 0 ? null : entriesDict.First().Key;
}
private init {
set {
var field = History.Fields["death"];
field.RemoveAllEntries();
if (value is not null) {
Expand Down
31 changes: 23 additions & 8 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,35 @@ private void SetCharacterCastes(CultureCollection cultures, Date ck3BookmarkDate
}
}

private static IEnumerable<string> LoadCharacterIDsToPreserve() {
public void LoadCharacterIDsToPreserve(Date ck3BookmarkDate) {
Logger.Debug("Loading IDs of CK3 characters to preserve...");
HashSet<string> characterIDsToPreserve = [];

string configurablePath = "configurables/ck3_characters_to_preserve.txt";
var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (_, id) => {
characterIDsToPreserve.Add(id);
parser.RegisterRegex("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);
}
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseFile(configurablePath);

return characterIDsToPreserve;
}

public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection dynasties, HouseCollection houses, Date ck3BookmarkDate) {
Expand Down Expand Up @@ -478,9 +494,8 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
.Where(c => c is not {FromImperator: true, Dead: false});

// Make some exceptions for characters referenced in game's script files.
var characterIdsToKeep = LoadCharacterIDsToPreserve();
charactersToCheck = charactersToCheck
.Where(character => !characterIdsToKeep.Contains(character.Id))
.Where(character => !character.IsNonRemovable)
.ToArray();

// I:R members of landed dynasties will be preserved, unless dead and childless.
Expand Down
9 changes: 9 additions & 0 deletions ImperatorToCK3/CK3/Dynasties/DynastyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public void PurgeUnneededDynasties(CharacterCollection characters, HouseCollecti
Logger.Info("Purging unneeded dynasties...");

HashSet<string> dynastiesToKeep = [];

// Load from configurable first.
var nonRemovableIdsParser = new Parser();
nonRemovableIdsParser.RegisterRegex(CommonRegexes.String, (_, id) => {
dynastiesToKeep.Add(id);
});
nonRemovableIdsParser.IgnoreAndLogUnregisteredItems();
nonRemovableIdsParser.ParseFile("configurables/dynasties_to_preserve.txt");

foreach (var character in characters) {
var dynastyIdAtBirth = character.GetDynastyId(character.BirthDate);
if (dynastyIdAtBirth is not null) {
Expand Down
12 changes: 12 additions & 0 deletions ImperatorToCK3/CK3/Dynasties/HouseCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public void LoadCK3Houses(ModFilesystem ck3ModFS) {

public void PurgeUnneededHouses(CharacterCollection ck3Characters, Date date) {
Logger.Info("Purging unneeded dynasty houses...");

// Load IDs of houses that should always be kept.
var houseIdsToPreserve = new HashSet<string>();
var nonRemovableIdsParser = new Parser();
nonRemovableIdsParser.RegisterRegex(CommonRegexes.String, (_, id) => {
houseIdsToPreserve.Add(id);
});
nonRemovableIdsParser.IgnoreAndLogUnregisteredItems();
nonRemovableIdsParser.ParseFile("configurables/dynasty_houses_to_preserve.txt");

HashSet<string> houseIdsToKeep = ck3Characters
.Select(c => c.GetDynastyHouseId(date))
Expand All @@ -35,6 +44,9 @@ public void PurgeUnneededHouses(CharacterCollection ck3Characters, Date date) {
if (houseIdsToKeep.Contains(house.Id)) {
continue;
}
if (houseIdsToPreserve.Contains(house.Id)) {
continue;
}

Remove(house.Id);
++removedCount;
Expand Down
2 changes: 2 additions & 0 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac
CorrectedDate,
config
);
// Now that we have loaded all characters, we can mark some of them as non-removable.
Characters.LoadCharacterIDsToPreserve(config.CK3BookmarkDate);
ClearFeaturedCharactersDescriptions(config.CK3BookmarkDate);

Dynasties.LoadCK3Dynasties(ModFS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
# This file should contain a simple, whitespace-separate list
# This file should contain a simple whitespace-separated lists
# of CK3 character IDs to preserve from purging.
# Comments are allowed, and start with a '#' character.

# Characters referenced in artifacts:
33922 # Muhammad
# Characters in the keep_as_is list will be preserved with their original birth and death dates.
keep_as_is = {
# Characters referenced in artifacts:
33922 # Muhammad
}

# Characters referenced in common/character_interactions/00_debug_interactions.txt:
159835 # King Arthur

# Characters referenced in achievements:
109607
# Characters in the after_bookmark_date will be preserved, but their birth dates will be
# set to a date after the bookmark date. This is useful for characters referenced in CK3 script files,
# but who we don't actually want to exist in the game.
# Consider it an error suppressor.
after_bookmark_date = {
# Characters referenced in common\casus_belli_types\00_event_war.txt:
124 # Tostig
140 # William the Conqueror

# Characters referenced in common/character_interactions/00_debug_interactions.txt:
159835 # King Arthur

# Characters referenced in common\council_positions\00_council_positions.txt:
528

# Characters referenced in common\script_values\00_diarchy_values.txt:
214

# Characters referenced in common\script_values\00_war_values.txt:
6878

# Characters referenced in common\scripted_modifiers\00_faction_modifiers.txt:
73683

# Characters referenced in achievements:
109607
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file should contain a simple whitespace-separated list of
# of CK3 dynasty IDs to preserve from purging.
# Comments are allowed, and start with a '#' character.

jamshid
100721 # Abbasid, referenced in events\dlc\fp2\fp2_yearly_events.txt
1029100 # Sassanid, referenced in events\dlc\fp3\fp3_struggle_events.txt
613 # Seljuk, referenced in events\yearly_events\yearly_events_persia.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file should contain a simple whitespace-separated list of
# of CK3 dynasty house IDs to preserve from purging.
# Comments are allowed, and start with a '#' character.

house_seljuk # referenced in common\scripted_effects\01_dlc_fp3_scripted_effects.txt
house_borjigin # referenced in common\scripted_character_templates\00_mongol_templates.txt
house_onggirat # referenced in common\scripted_character_templates\00_mongol_templates.txt
house_olkhunut # referenced in common\scripted_character_templates\00_mongol_templates.txt
house_uriankhai # referenced in common\scripted_character_templates\00_mongol_templates.txt
house_harmaytan # referenced in common\scripted_character_templates\04_fp3_character_templates.txt
house_tusi # referenced in common\scripted_character_templates\04_fp3_character_templates.txt
house_tumert # referenced in common\scripted_effects\00_almohad_invasion_effects.txt
house_almohad # referenced in common\scripted_effects\00_almohad_invasion_effects.txt
Loading

0 comments on commit 59d25c1

Please sign in to comment.