Skip to content

Commit

Permalink
Remove undefined traits from CK3 character history
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Jan 5, 2025
1 parent c11364d commit 64b00e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,23 @@ internal void ConvertImperatorCharacterDNA(DNAFactory dnaFactory) {
}
}
}

public void RemoveUndefinedTraits(TraitMapper traitMapper) {
Logger.Info("Removing undefined traits from CK3 character history...");

var definedTraits = traitMapper.ValidCK3TraitIDs.ToHashSet();

foreach (var character in this) {
if (character.FromImperator) {
continue;
}

var traitsField = character.History.Fields["traits"];
int removedCount = traitsField.InitialEntries.RemoveAll(
kvp => !definedTraits.Contains(kvp.Value.ToString() ?? string.Empty));
if (removedCount > 0) {
Logger.Debug($"Removed {removedCount} undefined traits from character {character.Id}.");
}
}
}
}
3 changes: 3 additions & 0 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac
Logger.Warn($"No base mapping found for I:R culture {cultureStr}!");
}
}
},
() => { // depends on TraitMapper and CK3 characters being loaded
Characters.RemoveUndefinedTraits(traitMapper);
}
);

Expand Down
2 changes: 2 additions & 0 deletions ImperatorToCK3/Mappers/Trait/TraitMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class TraitMapper {
protected IDictionary<string, string> ImperatorToCK3TraitMap = new Dictionary<string, string>();
protected IdObjectCollection<string, CK3.Characters.Trait> CK3Traits = [];

public IEnumerable<string> ValidCK3TraitIDs => CK3Traits.Select(t => t.Id);

// TODO: add a method for logging all unmapped I:R traits

public TraitMapper() { }
Expand Down

0 comments on commit 64b00e2

Please sign in to comment.