Skip to content

Remove relations effects from loaded CK3 character history #1970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading