Skip to content

Commit

Permalink
Support ck3_title_rank parameter in government mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Jan 20, 2025
1 parent 4e3ee28 commit 3e3ddee
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using commonItems;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.Mappers.Government;
using System.Collections.Generic;
using Xunit;
Expand All @@ -10,22 +11,22 @@ public class GovernmentMapperTests {
public void NonMatchGivesNull() {
var reader = new BufferedReader("link = { ck3 = ck3Government ir = irGovernment }");
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: new List<string> { "ck3Government" });
var ck3Gov = mapper.GetCK3GovernmentForImperatorGovernment("nonMatchingGovernment", null, []);
var ck3Gov = mapper.GetCK3GovernmentForImperatorGovernment("nonMatchingGovernment", rank: null, irCultureId: null, []);
Assert.Null(ck3Gov);
}
[Fact]
public void CK3GovernmentCanBeFound() {
var reader = new BufferedReader("link = { ck3 = ck3Government ir = irGovernment }");
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: new List<string> { "ck3Government" });
var ck3Gov = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", null, []);
var ck3Gov = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", rank: null, irCultureId: null, []);
Assert.Equal("ck3Government", ck3Gov);
}
[Fact]
public void MultipleImperatorGovernmentsCanBeInARule() {
var reader = new BufferedReader("link = { ck3 = ck3Government ir = irGovernment ir = irGovernment2 }");
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: new List<string> { "ck3Government" });
var ck3Gov1 = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", null, []);
var ck3Gov2 = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment2", null, []);
var ck3Gov1 = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", rank: null, irCultureId: null, []);
var ck3Gov2 = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment2", rank: null, irCultureId: null, []);
Assert.Equal("ck3Government", ck3Gov1);
Assert.Equal("ck3Government", ck3Gov2);
}
Expand All @@ -36,7 +37,7 @@ public void CorrectRuleMatches() {
"link = { ck3 = ck3Government2 ir = irGovernment2 }"
);
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: new List<string> { "ck3Government", "ck3Government2" });
var ck3Gov = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment2", null, []);
var ck3Gov = mapper.GetCK3GovernmentForImperatorGovernment("irGovernment2", rank: null, irCultureId: null, []);
Assert.Equal("ck3Government2", ck3Gov);
}

Expand All @@ -48,10 +49,32 @@ public void CultureCanBeUsedToMatch() {
"link = { ck3 = govC ir = irGovernment }"
);
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: new List<string> { "govA", "govB", "govC" });
Assert.Equal("govA", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", "roman", []));
Assert.Equal("govB", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", "greek", []));
Assert.Equal("govC", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", "thracian", []));
Assert.Equal("govC", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", null, []));
Assert.Equal("govA", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", rank: null, "roman", []));
Assert.Equal("govB", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", rank: null, "greek", []));
Assert.Equal("govC", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", rank: null, "thracian", []));
Assert.Equal("govC", mapper.GetCK3GovernmentForImperatorGovernment("irGovernment", rank: null, irCultureId: null, []));
}

[Fact]
public void CK3TitleRankCanBeUsedToMatch() {
var reader = new BufferedReader(
"""
link = { ck3 = administrative_government ir = imperium ck3_title_rank = ke } # only for kingdoms and empires
link = { ck3 = feudal_government ir = imperium }
"""
);
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: [ "administrative_government", "feudal_government" ]);

foreach (var rank in new List<TitleRank> { TitleRank.empire, TitleRank.kingdom }) {
Assert.Equal("administrative_government",
mapper.GetCK3GovernmentForImperatorGovernment(
irGovernmentId: "imperium", rank, irCultureId: null, enabledCK3Dlcs: []));
}
foreach (var rank in new List<TitleRank> { TitleRank.duchy, TitleRank.county, TitleRank.barony }) {
Assert.Equal("feudal_government",
mapper.GetCK3GovernmentForImperatorGovernment(
irGovernmentId: "imperium", rank, irCultureId: null, enabledCK3Dlcs: []));
}
}

[Fact]
Expand All @@ -70,7 +93,7 @@ public void CK3DlcCanBeUsedToMatch() {
""");
var mapper = new GovernmentMapper(reader, ck3GovernmentIds: [ "administrative_government", "feudal_government" ]);

Assert.Equal("administrative_government", mapper.GetCK3GovernmentForImperatorGovernment("imperium", null, enabledCK3Dlcs: ["roads_to_power"]));
Assert.Equal("feudal_government", mapper.GetCK3GovernmentForImperatorGovernment("imperium", null, enabledCK3Dlcs: []));
Assert.Equal("administrative_government", mapper.GetCK3GovernmentForImperatorGovernment("imperium", rank: null, irCultureId: null, enabledCK3Dlcs: ["roads_to_power"]));
Assert.Equal("feudal_government", mapper.GetCK3GovernmentForImperatorGovernment("imperium", rank: null, irCultureId: null, enabledCK3Dlcs: []));
}
}
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/Titles/RulerTerm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public RulerTerm(
}
StartDate = imperatorRulerTerm.StartDate;
if (imperatorRulerTerm.Government is not null) {
Government = governmentMapper.GetCK3GovernmentForImperatorGovernment(imperatorRulerTerm.Government, null, enabledCK3Dlcs);
Government = governmentMapper.GetCK3GovernmentForImperatorGovernment(imperatorRulerTerm.Government, rank: null, null, enabledCK3Dlcs);
}

PreImperatorRuler = imperatorRulerTerm.PreImperatorRuler;
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/Titles/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void FillHolderAndGovernmentHistory(Country imperatorCountry,

if (imperatorCountry.Government is not null) {
var lastCK3TermGov = GetGovernment(conversionDate);
var ck3CountryGov = governmentMapper.GetCK3GovernmentForImperatorGovernment(imperatorCountry.Government, imperatorCountry.PrimaryCulture, enabledCK3Dlcs);
var ck3CountryGov = governmentMapper.GetCK3GovernmentForImperatorGovernment(imperatorCountry.Government, Rank, imperatorCountry.PrimaryCulture, enabledCK3Dlcs);
if (lastCK3TermGov != ck3CountryGov && ck3CountryGov is not null) {
History.AddFieldValue(conversionDate, "government", "government", ck3CountryGov);
}
Expand Down
7 changes: 4 additions & 3 deletions ImperatorToCK3/Mappers/Government/GovernmentMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using commonItems;
using ImperatorToCK3.CK3.Titles;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -7,7 +8,7 @@
namespace ImperatorToCK3.Mappers.Government;

public sealed class GovernmentMapper {
private readonly List<GovernmentMapping> mappings = new();
private readonly List<GovernmentMapping> mappings = [];

public GovernmentMapper(ICollection<string> ck3GovernmentIds) {
Logger.Info("Parsing government mappings...");
Expand Down Expand Up @@ -48,9 +49,9 @@ private void RemoveInvalidLinks(ICollection<string> ck3GovernmentIds) {
}
}

public string? GetCK3GovernmentForImperatorGovernment(string irGovernmentId, string? irCultureId, IReadOnlyCollection<string> enabledCK3Dlcs) {
public string? GetCK3GovernmentForImperatorGovernment(string irGovernmentId, TitleRank? rank, string? irCultureId, IReadOnlyCollection<string> enabledCK3Dlcs) {
foreach (var mapping in mappings) {
var match = mapping.Match(irGovernmentId, irCultureId, enabledCK3Dlcs);
var match = mapping.Match(irGovernmentId, rank, irCultureId, enabledCK3Dlcs);
if (match is not null) {
return match;
}
Expand Down
14 changes: 13 additions & 1 deletion ImperatorToCK3/Mappers/Government/GovernmentMapping.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
using commonItems;
using ImperatorToCK3.CK3.Titles;
using Open.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ImperatorToCK3.Mappers.Government;

internal sealed class GovernmentMapping {
public string CK3GovernmentId { get; private set; } = "";
public SortedSet<string> ImperatorGovernmentIds { get; } = [];
public SortedSet<string> ImperatorCultureIds { get; } = [];
private readonly HashSet<TitleRank> titleRanks = [];
public HashSet<string> RequiredCK3Dlcs { get; } = [];

public GovernmentMapping(BufferedReader mappingReader) {
var parser = new Parser();
parser.RegisterKeyword("ck3", reader => CK3GovernmentId = reader.GetString());
parser.RegisterKeyword("ir", reader => ImperatorGovernmentIds.Add(reader.GetString()));
parser.RegisterKeyword("irCulture", reader => ImperatorCultureIds.Add(reader.GetString()));
parser.RegisterKeyword("ck3_title_rank", reader => {
var ranksToAdd = reader.GetString().ToCharArray().Select(TitleRankUtils.CharToTitleRank);
titleRanks.AddRange(ranksToAdd);
});
parser.RegisterKeyword("has_ck3_dlc", reader => RequiredCK3Dlcs.Add(reader.GetString()));
parser.IgnoreAndLogUnregisteredItems();

parser.ParseStream(mappingReader);
}

public string? Match(string irGovernmentId, string? irCultureId, IReadOnlyCollection<string> enabledCK3Dlcs) {
public string? Match(string irGovernmentId, TitleRank? rank, string? irCultureId, IReadOnlyCollection<string> enabledCK3Dlcs) {
if (!ImperatorGovernmentIds.Contains(irGovernmentId)) {
return null;
}

if (titleRanks.Count > 0 && (rank is null || !titleRanks.Contains(rank.Value))) {
return null;
}

if (ImperatorCultureIds.Count != 0) {
if (irCultureId is null) {
Expand Down

0 comments on commit 3e3ddee

Please sign in to comment.