-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Technology conversion configurable through configurables/inventions_t…
- Loading branch information
1 parent
792d5ef
commit f9889a6
Showing
13 changed files
with
263 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using commonItems; | ||
using commonItems.Collections; | ||
using commonItems.Mods; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ImperatorToCK3.Imperator.Inventions; | ||
|
||
public class InventionsDB { | ||
private readonly OrderedSet<string> inventions = []; | ||
|
||
public void LoadInventions(ModFilesystem irModFS) { | ||
var inventionsParser = new Parser(); | ||
inventionsParser.RegisterKeyword("technology", ParserHelpers.IgnoreItem); | ||
inventionsParser.RegisterKeyword("color", ParserHelpers.IgnoreItem); | ||
inventionsParser.RegisterRegex(CommonRegexes.String, (reader, inventionId) => { | ||
inventions.Add(inventionId); | ||
ParserHelpers.IgnoreItem(reader); | ||
}); | ||
inventionsParser.IgnoreAndLogUnregisteredItems(); | ||
|
||
var inventionGroupsParser = new Parser(); | ||
inventionGroupsParser.RegisterRegex(CommonRegexes.String, reader => inventionsParser.ParseStream(reader)); | ||
inventionGroupsParser.IgnoreAndLogUnregisteredItems(); | ||
|
||
Logger.Info("Loading Imperator inventions..."); | ||
inventionGroupsParser.ParseGameFolder("common/inventions", irModFS, "txt", recursive: true); | ||
} | ||
|
||
public IEnumerable<string> GetActiveInventionIds(IList<bool> booleans) { | ||
// Enumerate over the inventions and return the ones that are active (bool is true). | ||
foreach (var item in inventions.Select((inventionId, i) => new { i, inventionId })) { | ||
if (booleans[item.i]) { | ||
yield return item.inventionId; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using commonItems; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ImperatorToCK3.Mappers.Technology; | ||
|
||
public sealed class InnovationBonus { // TODO: add tests | ||
private readonly HashSet<string> imperatorInventions = []; | ||
private string? ck3Innovation; | ||
|
||
public InnovationBonus(BufferedReader bonusReader) { | ||
var parser = new Parser(); | ||
parser.RegisterKeyword("ir", reader => imperatorInventions.Add(reader.GetString())); | ||
parser.RegisterKeyword("ck3", reader => ck3Innovation = reader.GetString()); | ||
parser.IgnoreAndLogUnregisteredItems(); | ||
parser.ParseStream(bonusReader); | ||
|
||
// A bonus should have at most 3 inventions. | ||
if (imperatorInventions.Count > 3) { | ||
Logger.Warn($"Innovation bonus for {ck3Innovation} has more than 3 inventions: {string.Join(", ", imperatorInventions)}"); | ||
} | ||
} | ||
|
||
public KeyValuePair<string, ushort>? GetProgress(IEnumerable<string> activeInventions) { | ||
if (ck3Innovation is null) { | ||
return null; | ||
} | ||
|
||
// For each matching invention, add 25 to the progress. | ||
int progress = activeInventions | ||
.Where(invention => imperatorInventions.Contains(invention)) | ||
.Sum(invention => 25); | ||
return new(ck3Innovation, (ushort)progress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using commonItems; | ||
|
||
namespace ImperatorToCK3.Mappers.Technology; | ||
|
||
public sealed class InnovationLink { // TODO: ADD TESTS | ||
private string? imperatorInvention; | ||
private string? ck3Innovation; | ||
|
||
public InnovationLink(BufferedReader linkReader) { | ||
var parser = new Parser(); | ||
parser.RegisterKeyword("ir", reader => imperatorInvention = reader.GetString()); | ||
parser.RegisterKeyword("ck3", reader => ck3Innovation = reader.GetString()); | ||
parser.IgnoreAndLogUnregisteredItems(); | ||
parser.ParseStream(linkReader); | ||
} | ||
|
||
public string? Match(string irInvention) { | ||
if (imperatorInvention is null) { | ||
return null; | ||
} | ||
return imperatorInvention == irInvention ? ck3Innovation : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using commonItems; | ||
using System.Collections.Generic; | ||
|
||
namespace ImperatorToCK3.Mappers.Technology; | ||
|
||
public class InnovationMapper { | ||
private readonly List<InnovationLink> innovationLinks = []; | ||
private readonly List<InnovationBonus> innovationBonuses = []; | ||
|
||
public void LoadLinksAndBonuses(string configurablePath) { | ||
var parser = new Parser(); | ||
parser.RegisterKeyword("link", reader => innovationLinks.Add(new InnovationLink(reader))); | ||
parser.RegisterKeyword("bonus", reader => innovationBonuses.Add(new InnovationBonus(reader))); | ||
parser.IgnoreAndLogUnregisteredItems(); | ||
parser.ParseFile(configurablePath); | ||
} | ||
|
||
public IList<string> GetInnovations(IEnumerable<string> irInventions) { | ||
var ck3Innovations = new List<string>(); | ||
foreach (var irInvention in irInventions) { | ||
foreach (var link in innovationLinks) { | ||
var match = link.Match(irInvention); | ||
if (match is not null) { | ||
ck3Innovations.Add(match); | ||
} | ||
} | ||
} | ||
return ck3Innovations; | ||
} | ||
|
||
public IDictionary<string, ushort> GetInnovationProgresses(ICollection<string> irInventions) { | ||
Dictionary<string, ushort> progressesToReturn = []; | ||
foreach (var bonus in innovationBonuses) { | ||
var innovationProgress = bonus.GetProgress(irInventions); | ||
if (!innovationProgress.HasValue) { | ||
continue; | ||
} | ||
|
||
if (progressesToReturn.TryGetValue(innovationProgress.Value.Key, out ushort currentValue)) { | ||
// Only the highest progress should be kept. | ||
if (currentValue < innovationProgress.Value.Value) { | ||
progressesToReturn[innovationProgress.Value.Key] = innovationProgress.Value.Value; | ||
} | ||
} else { | ||
progressesToReturn[innovationProgress.Value.Key] = innovationProgress.Value.Value; | ||
} | ||
} | ||
return progressesToReturn; | ||
} | ||
} |
Oops, something went wrong.