Skip to content

Commit

Permalink
Remove relations effects from loaded CK3 character history (#1970) #m…
Browse files Browse the repository at this point in the history
…inor
  • Loading branch information
IhateTrains authored Jun 2, 2024
1 parent a2e0703 commit f03ba2d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
14 changes: 5 additions & 9 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,10 @@ private void BulkRemove(ICollection<string> keys) {

private void RemoveCharacterReferencesFromHistory(ICollection<string> idsToRemove) {
var idsCapturingGroup = "(" + string.Join('|', idsToRemove) + ")";

const string commandsCapturingGroup = "(" +
"set_relation_rival|set_relation_potential_rival|set_relation_nemesis|" +
"set_relation_lover|set_relation_soulmate|" +
"set_relation_friend|set_relation_potential_friend|set_relation_best_friend|" +
"set_relation_ward|set_relation_mentor)";

var regex = new Regex(commandsCapturingGroup + @"\s*=\s*\{[^\}]*character:" + idsCapturingGroup + @"\s[^\}]*\}(?:\s*#.*)?");

// 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"];
Expand All @@ -156,7 +152,7 @@ private void RemoveCharacterReferencesFromHistory(ICollection<string> idsToRemov
continue;
}

effectsLiteralField.RegexReplaceAllEntries(regex, string.Empty);
effectsLiteralField.RegexReplaceAllEntries(simpleCommandsRegex, string.Empty);

// Remove all empty effect blocks (effect = { }).
effectsHistoryField.RemoveAllEntries(entryValue => {
Expand Down
10 changes: 10 additions & 0 deletions ImperatorToCK3/CK3/Characters/CharactersLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using commonItems;
using commonItems.Mods;
using Open.Collections.Synchronized;
using System.Linq;

namespace ImperatorToCK3.CK3.Characters;

Expand All @@ -26,6 +27,11 @@ public void LoadCK3Characters(ModFilesystem ck3ModFS, Date bookmarkDate) {
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("history/characters", ck3ModFS, "txt", recursive: true, parallel: true);

string[] irrelevantEffects = ["set_relation_rival", "set_relation_potential_rival", "set_relation_nemesis",
"set_relation_lover", "set_relation_soulmate",
"set_relation_friend", "set_relation_potential_friend", "set_relation_best_friend",
"set_relation_ward", "set_relation_mentor",];

foreach (var character in loadedCharacters) {
// Remove post-bookmark history except for births and deaths.
foreach (var field in character.History.Fields) {
Expand All @@ -48,6 +54,10 @@ public void LoadCK3Characters(ModFilesystem ck3ModFS, Date bookmarkDate) {
deathField.RemoveAllEntries();
deathField.AddEntryToHistory(deathDate, "death", value: true);
}

// Remove effects that set relations. They don't matter a lot in our alternate timeline.
character.History.Fields["effects"].RemoveAllEntries(
entry => irrelevantEffects.Any(effect => entry.ToString()?.Contains(effect) ?? false));

character.UpdateChildrenCacheOfParents();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
33922 # Muhammad

# Characters referenced in on-actions:
7627
90107 # Ludwig the German
74025
33358
Expand Down

0 comments on commit f03ba2d

Please sign in to comment.