Skip to content

Commit

Permalink
Fix for TFE compatibility: load innovations from CK3 and remove inval…
Browse files Browse the repository at this point in the history
…id technology mappings (#1905)

#163
  • Loading branch information
IhateTrains authored Apr 26, 2024
1 parent 5ccfb33 commit 65bb022
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
20 changes: 20 additions & 0 deletions ImperatorToCK3/CK3/Cultures/CultureCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ private void LoadInvalidatingCultureIds(ICollection<string> ck3ModFlags, Buffere
}

public void LoadCultures(ModFilesystem ck3ModFS) {
Logger.Info("Loading cultures...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => LoadCulture(cultureId, reader));
parser.IgnoreAndLogUnregisteredItems();
Expand All @@ -95,6 +97,8 @@ public void LoadCultures(ModFilesystem ck3ModFS) {
}

public void LoadConverterCultures(string converterCulturesPath) {
Logger.Info("Loading converter cultures...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => LoadCulture(cultureId, reader));
parser.IgnoreAndLogUnregisteredItems();
Expand Down Expand Up @@ -149,13 +153,27 @@ private void ReplaceInvalidatedParents() {
}

public void LoadNameLists(ModFilesystem ck3ModFS) {
Logger.Info("Loading name lists...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, nameListId) => {
NameListCollection.AddOrReplace(new NameList(nameListId, reader));
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/culture/name_lists", ck3ModFS, "txt", recursive: true, logFilePaths: true);
}

public void LoadInnovationIds(ModFilesystem ck3ModFS) {
Logger.Info("Loading CK3 innovation IDs...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, innovationId) => {
InnovationIds.Add(innovationId);
ParserHelpers.IgnoreItem(reader);
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/culture/innovations", ck3ModFS, "txt", recursive: true, logFilePaths: true);
}

private string? GetCK3CultureIdForImperatorCountry(Country country, CultureMapper cultureMapper, ProvinceMapper provinceMapper) {
var irCulture = country.PrimaryCulture ?? country.Monarch?.Culture;
Expand All @@ -182,6 +200,7 @@ public void LoadNameLists(ModFilesystem ck3ModFS) {
var innovationMapper = new InnovationMapper();
innovationMapper.LoadLinksAndBonuses("configurables/inventions_to_innovations_map.txt");
innovationMapper.LogUnmappedInventions(inventionsDB, irLocDB);
innovationMapper.RemoveMappingsWithInvalidInnovations(InnovationIds);

// Group I:R countries by corresponding CK3 culture.
var countriesByCulture = countries.Select(c => new {
Expand All @@ -207,6 +226,7 @@ public void LoadNameLists(ModFilesystem ck3ModFS) {

protected readonly PillarCollection PillarCollection;
protected readonly IdObjectCollection<string, NameList> NameListCollection = new();
protected readonly HashSet<string> InnovationIds = [];

private CultureData cultureData = new();
private readonly Parser cultureDataParser = new();
Expand Down
4 changes: 1 addition & 3 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ public World(Imperator.World impWorld, Configuration config) {
Logger.Info("Loading converter cultural pillars...");
CulturalPillars.LoadConverterPillars("configurables/cultural_pillars");
Cultures = new CultureCollection(ck3ColorFactory, CulturalPillars, config.GetCK3ModFlags());
Logger.Info("Loading name lists...");
Cultures.LoadNameLists(ModFS);
Logger.Info("Loading cultures...");
Cultures.LoadInnovationIds(ModFS);
Cultures.LoadCultures(ModFS);
Logger.Info("Loading converter cultures...");
Cultures.LoadConverterCultures("configurables/converter_cultures.txt");
Logger.IncrementProgress();

Expand Down
10 changes: 5 additions & 5 deletions ImperatorToCK3/Mappers/Technology/InnovationBonus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace ImperatorToCK3.Mappers.Technology;

public sealed class InnovationBonus { // TODO: add tests
private readonly HashSet<string> imperatorInventions = [];
private string? ck3Innovation;
public string? CK3InnovationId { get; private set; }

public InnovationBonus(BufferedReader bonusReader) {
var parser = new Parser();
parser.RegisterKeyword("ir", reader => imperatorInventions.Add(reader.GetString()));
parser.RegisterKeyword("ck3", reader => ck3Innovation = reader.GetString());
parser.RegisterKeyword("ck3", reader => CK3InnovationId = reader.GetString());
parser.IgnoreAndLogUnregisteredItems();
parser.ParseStream(bonusReader);

if (ck3Innovation is null) {
if (CK3InnovationId is null) {
Logger.Warn($"Innovation bonus from {string.Join(", ", imperatorInventions)} has no CK3 innovation.");
}
}

public KeyValuePair<string, ushort>? GetProgress(IEnumerable<string> activeInventions) {
if (ck3Innovation is null) {
if (CK3InnovationId is null) {
return null;
}

Expand All @@ -33,6 +33,6 @@ public InnovationBonus(BufferedReader bonusReader) {
return null;
}

return new(ck3Innovation, (ushort)progress);
return new(CK3InnovationId, (ushort)progress);
}
}
10 changes: 5 additions & 5 deletions ImperatorToCK3/Mappers/Technology/InnovationLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ namespace ImperatorToCK3.Mappers.Technology;

public sealed class InnovationLink { // TODO: ADD TESTS
private string? imperatorInvention;
private string? ck3Innovation;
public string? CK3InnovationId { get; private set; }

public InnovationLink(BufferedReader linkReader) {
var parser = new Parser();
parser.RegisterKeyword("ir", reader => imperatorInvention = reader.GetString());
parser.RegisterKeyword("ck3", reader => ck3Innovation = reader.GetString());
parser.RegisterKeyword("ck3", reader => CK3InnovationId = reader.GetString());
parser.IgnoreAndLogUnregisteredItems();
parser.ParseStream(linkReader);

if (ck3Innovation is null) {
if (CK3InnovationId is null) {
Logger.Warn($"Innovation link from {imperatorInvention} has no CK3 innovation.");
}

if (imperatorInvention is null) {
Logger.Warn($"Innovation link to {ck3Innovation} has no Imperator invention.");
Logger.Warn($"Innovation link to {CK3InnovationId} has no Imperator invention.");
}
}

public string? Match(string irInvention) {
if (imperatorInvention is null) {
return null;
}
return imperatorInvention == irInvention ? ck3Innovation : null;
return imperatorInvention == irInvention ? CK3InnovationId : null;
}
}
11 changes: 11 additions & 0 deletions ImperatorToCK3/Mappers/Technology/InnovationMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ public void LogUnmappedInventions(InventionsDB inventionsDB, LocDB irLocDB) {
}

// TODO: ALSO LOG UNMAPPED CK3 MARTIAL AND CIVIC INNOVATIONS

public void RemoveMappingsWithInvalidInnovations(ISet<string> innovationIds) {
int removedCount = 0;

removedCount += innovationLinks
.RemoveAll(link => link.CK3InnovationId is null || !innovationIds.Contains(link.CK3InnovationId));
removedCount += innovationBonuses
.RemoveAll(bonus => bonus.CK3InnovationId is null || !innovationIds.Contains(bonus.CK3InnovationId));

Logger.Debug($"Removed {removedCount} technology mappings with invalid CK3 innovations.");
}
}

0 comments on commit 65bb022

Please sign in to comment.