Skip to content

Commit

Permalink
Configurable CK3 bookmark date (#305) #minor
Browse files Browse the repository at this point in the history
* Configurable CK3 bookmark date

* Loc fixes

* Remove unused variable in test
  • Loading branch information
IhateTrains authored Sep 7, 2021
1 parent b486d31 commit 45f20f2
Show file tree
Hide file tree
Showing 35 changed files with 150 additions and 109 deletions.
4 changes: 3 additions & 1 deletion ImperatorToCK3.UnitTests/CK3/Characters/CK3CharacterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public Character Build() {
localizationMapper,
provinceMapper,
deathReasonMapper,
convertBirthAndDeathDates
convertBirthAndDeathDates,
new Date(867, 1, 1),
new Date(867, 1, 1)
);
return character;
}
Expand Down
4 changes: 3 additions & 1 deletion ImperatorToCK3.UnitTests/CK3/Dynasties/DynastyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public Character Build() {
localizationMapper,
provinceMapper,
deathReasonMapper,
convertBirthAndDeathDates
convertBirthAndDeathDates,
new Date(867, 1, 1),
new Date(867, 1, 1)
);
return character;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ImperatorToCK3.UnitTests.CK3.Provinces {
[Collection("Sequential")]
[CollectionDefinition("Sequential", DisableParallelization = true)]
public class ProvinceDetailsTests {
private readonly Date ck3BookmarkDate = new(867, 1, 1);
[Fact] public void FieldsDefaultToCorrectValues() {
var details = new ProvinceDetails();
Assert.Equal(string.Empty, details.Culture);
Expand All @@ -19,7 +20,7 @@ public void DetailsCanBeLoadedFromStream() {
var reader = new BufferedReader(
"= { religion = orthodox\n random_param = random_stuff\n culture = roman\n}"
);
var details = new ProvinceDetails(reader);
var details = new ProvinceDetails(reader, ck3BookmarkDate);

Assert.Equal("roman", details.Culture);
Assert.Equal("orthodox", details.Religion);
Expand All @@ -35,7 +36,7 @@ public void DetailsAreLoadedFromDatedBlocks() {
"850.1.1 = { religion=orthodox holding=castle_holding }" +
"}"
);
var details = new ProvinceDetails(reader);
var details = new ProvinceDetails(reader, ck3BookmarkDate);

Assert.Equal("castle_holding", details.Holding);
Assert.Equal("orthodox", details.Religion);
Expand All @@ -51,7 +52,7 @@ public void DetailsCanBeCopyConstructed() {
"\t850.1.1 = { religion=orthodox holding=castle_holding }" +
"}"
);
var details1 = new ProvinceDetails(reader);
var details1 = new ProvinceDetails(reader, ck3BookmarkDate);
var details2 = new ProvinceDetails(details1);

Assert.Equal("castle_holding", details2.Holding);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImperatorToCK3.CK3.Provinces;
using commonItems;
using Xunit;

namespace ImperatorToCK3.UnitTests.CK3.Provinces {
Expand All @@ -14,11 +15,10 @@ public void MappingsDefaultToEmpty() {
[Fact]
public void MappingsCanBeLoadedFromFile() {
var provinceMappings = new ProvinceMappings(testFilePath);
var province1 = new Province(id: 1, new commonItems.BufferedReader(string.Empty));
Assert.Collection(provinceMappings.Mappings,
mapping1 => { Assert.Equal((ulong)3, mapping1.Key); Assert.Equal((ulong)1, mapping1.Value); },
mapping1 => { Assert.Equal((ulong)4, mapping1.Key); Assert.Equal((ulong)1, mapping1.Value); },
mapping1 => { Assert.Equal((ulong)5, mapping1.Key); Assert.Equal((ulong)4, mapping1.Value); }
mapping2 => { Assert.Equal((ulong)4, mapping2.Key); Assert.Equal((ulong)1, mapping2.Value); },
mapping3 => { Assert.Equal((ulong)5, mapping3.Key); Assert.Equal((ulong)4, mapping3.Value); }
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/CK3/Provinces/ProvinceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void ProvinceCanBeLoadedFromStream() {
var reader = new BufferedReader(
"{ culture=roman random_key=random_value religion=orthodox holding=castle_holding }"
);
var province = new Province(42, reader);
var province = new Province(42, reader, new Date(867,1,1));
Assert.Equal((ulong)42, province.ID);
Assert.Equal("orthodox", province.Religion);
Assert.Equal("roman", province.Culture);
Expand Down
3 changes: 2 additions & 1 deletion ImperatorToCK3.UnitTests/CK3/Provinces/ProvincesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using commonItems;

namespace ImperatorToCK3.UnitTests.CK3.Provinces {
[Collection("Sequential")]
Expand All @@ -12,7 +13,7 @@ [Fact] public void ProvincesDefaltToEmpty() {

[Fact]
public void ProvincesAreProperlyLoadedFromFile() {
var provinces = new ImperatorToCK3.CK3.Provinces.Provinces("TestFiles/CK3ProvincesHistoryFile.txt");
var provinces = new ImperatorToCK3.CK3.Provinces.Provinces("TestFiles/CK3ProvincesHistoryFile.txt", new Date(867,1,1));

Assert.Equal(4, provinces.StoredProvinces.Count);
Assert.Equal("slovien", provinces.StoredProvinces[3080].Culture);
Expand Down
5 changes: 3 additions & 2 deletions ImperatorToCK3.UnitTests/CK3/Titles/TitleHistoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ImperatorToCK3.CK3.Titles;
using Xunit;
using commonItems;

namespace ImperatorToCK3.UnitTests.CK3.Titles {
public class TitleHistoryTests {
Expand Down Expand Up @@ -33,7 +34,7 @@ public void DevelopmentLevelDefaultsToNull() {

[Fact]
public void HistoryCanBeLoadedFromStream() {
var titlesHistory = new TitlesHistory("TestFiles/title_history");
var titlesHistory = new TitlesHistory("TestFiles/title_history", new Date(867,1,1));
var history = titlesHistory.PopTitleHistory("k_rome");

Assert.Equal("67", history.Holder);
Expand All @@ -42,7 +43,7 @@ public void HistoryCanBeLoadedFromStream() {

[Fact]
public void HistoryIsLoadedFromDatedBlocks() {
var titlesHistory = new TitlesHistory("TestFiles/title_history");
var titlesHistory = new TitlesHistory("TestFiles/title_history", new Date(867, 1, 1));
var history = titlesHistory.PopTitleHistory("k_greece");

Assert.Equal("420", history.Holder);
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/CK3/Titles/TitleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void CapitalBaronyDefaultsToZero() {

[Fact]
public void HistoryCanBeAdded() {
var titlesHistory = new TitlesHistory("TestFiles/title_history");
var titlesHistory = new TitlesHistory("TestFiles/title_history", new Date(867,1,1));
var history = titlesHistory.PopTitleHistory("k_greece");
var title = new Title();
title.AddHistory(new LandedTitles(), history);
Expand Down
21 changes: 4 additions & 17 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ public class Character {

public Imperator.Characters.Character? ImperatorCharacter { get; set; }

public void InitializeFromImperator(
Imperator.Characters.Character impCharacter,
ReligionMapper religionMapper,
CultureMapper cultureMapper,
TraitMapper traitMapper,
NicknameMapper nicknameMapper,
LocalizationMapper localizationMapper,
ProvinceMapper provinceMapper, // used to determine ck3 province for religion mapper
DeathReasonMapper deathReasonMapper,
bool ConvertBirthAndDeathDates = true
) {
var dateOnConversion = new Date(867, 1, 1);
InitializeFromImperator(impCharacter, religionMapper, cultureMapper, traitMapper, nicknameMapper, localizationMapper, provinceMapper, deathReasonMapper, ConvertBirthAndDeathDates, dateOnConversion);
}
public void InitializeFromImperator(
Imperator.Characters.Character impCharacter,
ReligionMapper religionMapper,
Expand All @@ -52,7 +38,8 @@ public void InitializeFromImperator(
ProvinceMapper provinceMapper, // used to determine ck3 province for religion mapper
DeathReasonMapper deathReasonMapper,
bool convertBirthAndDeathDates,
Date dateOnConversion
Date dateOnConversion,
Date ck3BookmarkDate
) {
ImperatorCharacter = impCharacter;
ImperatorCharacter.CK3Character = this;
Expand Down Expand Up @@ -160,8 +147,8 @@ Date dateOnConversion
DeathReason = deathReasonMapper.GetCK3ReasonForImperatorReason(impDeathReason);
}
if (!convertBirthAndDeathDates) { // if option to convert character age is chosen
BirthDate.AddYears((int)new Date(867, 1, 1).DiffInYears(dateOnConversion));
DeathDate?.AddYears((int)new Date(867, 1, 1).DiffInYears(dateOnConversion));
BirthDate.AddYears((int)ck3BookmarkDate.DiffInYears(dateOnConversion));
DeathDate?.AddYears((int)ck3BookmarkDate.DiffInYears(dateOnConversion));
}
}

Expand Down
4 changes: 2 additions & 2 deletions ImperatorToCK3/CK3/Provinces/Province.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace ImperatorToCK3.CK3.Provinces {
public class Province {
public Province() { }
public Province(ulong id, BufferedReader reader) {
public Province(ulong id, BufferedReader reader, Date ck3BookmarkDate) {
// Load from a country file, if one exists. Otherwise rely on defaults.
ID = id;
details = new ProvinceDetails(reader);
details = new ProvinceDetails(reader, ck3BookmarkDate);
}
public Province(ulong id, Province otherProvince) {
ID = id;
Expand Down
11 changes: 5 additions & 6 deletions ImperatorToCK3/CK3/Provinces/ProvinceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ public ProvinceDetails(ProvinceDetails otherDetails) {
Holding = otherDetails.Holding;
Buildings = new List<string>(otherDetails.Buildings);
}
public ProvinceDetails(BufferedReader reader) {
public ProvinceDetails(BufferedReader reader, Date ck3BookmarkDate) {
var history = historyFactory.GetHistory(reader);
var date = new Date(867, 1, 1);

var cultureOpt = history.GetSimpleFieldValue("culture", date);
var cultureOpt = history.GetSimpleFieldValue("culture", ck3BookmarkDate);
if (cultureOpt is not null) {
Culture = cultureOpt;
}
var religionOpt = history.GetSimpleFieldValue("religion", date);
var religionOpt = history.GetSimpleFieldValue("religion", ck3BookmarkDate);
if (religionOpt is not null) {
Religion = religionOpt;
}
var holdingOpt = history.GetSimpleFieldValue("holding", date);
var holdingOpt = history.GetSimpleFieldValue("holding", ck3BookmarkDate);
if (holdingOpt is null) {
Logger.Warn("Province's holding can't be null!");
} else {
Holding = holdingOpt;
}
var buildingsOpt = history.GetContainerFieldValue("buildings", date);
var buildingsOpt = history.GetContainerFieldValue("buildings", ck3BookmarkDate);
if (buildingsOpt is null) {
Logger.Warn("Province's buildings list can't be null!");
} else {
Expand Down
8 changes: 4 additions & 4 deletions ImperatorToCK3/CK3/Provinces/Provinces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
namespace ImperatorToCK3.CK3.Provinces {
public class Provinces : Parser {
public Provinces() { }
public Provinces(string filePath) {
RegisterKeys();
public Provinces(string filePath, Date ck3BookmarkDate) {
RegisterKeys(ck3BookmarkDate);
ParseFile(filePath);
ClearRegisteredRules();
}
public Dictionary<ulong, Province> StoredProvinces { get; } = new();

private void RegisterKeys() {
private void RegisterKeys(Date ck3BookmarkDate) {
RegisterRegex(CommonRegexes.Integer, (reader, provinceIdString) => {
var provID = ulong.Parse(provinceIdString);
var newProvince = new Province(provID, reader);
var newProvince = new Province(provID, reader, ck3BookmarkDate);
StoredProvinces.Add(provID, newProvince);
});
RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem);
Expand Down
11 changes: 5 additions & 6 deletions ImperatorToCK3/CK3/Titles/TitleHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
namespace ImperatorToCK3.CK3.Titles {
public class TitleHistory {
public TitleHistory() { }
public TitleHistory(History history) {
public TitleHistory(History history, Date ck3BookmarkDate) {
this.history = history;
var date = new Date(867, 1, 1);
var holderFromHistory = history.GetSimpleFieldValue("holder", date);
var holderFromHistory = history.GetSimpleFieldValue("holder", ck3BookmarkDate);
if (holderFromHistory is null) {
Logger.Warn("TitleHistory: holder should not be null!");
} else {
Holder = holderFromHistory;
}
Liege = history.GetSimpleFieldValue("liege", date);
Government = history.GetSimpleFieldValue("government", date);
Liege = history.GetSimpleFieldValue("liege", ck3BookmarkDate);
Government = history.GetSimpleFieldValue("government", ck3BookmarkDate);

var developmentLevelOpt = history.GetSimpleFieldValue("development_level", date);
var developmentLevelOpt = history.GetSimpleFieldValue("development_level", ck3BookmarkDate);
if (developmentLevelOpt is not null) {
DevelopmentLevel = int.Parse(developmentLevelOpt);
}
Expand Down
8 changes: 4 additions & 4 deletions ImperatorToCK3/CK3/Titles/TitlesHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace ImperatorToCK3.CK3.Titles {
public class TitlesHistory : Parser {
public TitlesHistory() { }
public TitlesHistory(string folderPath) {
public TitlesHistory(string folderPath, Date ck3BookmarkDate) {
var filenames = SystemUtils.GetAllFilesInFolderRecursive(folderPath);
Logger.Info("Parsing title history.");
RegisterKeys();
RegisterKeys(ck3BookmarkDate);
foreach (var filename in filenames) {
ParseFile(System.IO.Path.Combine(folderPath, filename));
}
Expand All @@ -23,7 +23,7 @@ public TitlesHistory(string folderPath) {
return null;
}

private void RegisterKeys() {
private void RegisterKeys(Date ck3BookmarkDate) {
RegisterRegex(@"(e|k|d|c|b)_[A-Za-z0-9_\-\']+", (reader, titleName) => {
var historyItem = new StringOfItem(reader).String;
if (historyItem.IndexOf('{') != -1) {
Expand All @@ -32,7 +32,7 @@ private void RegisterKeys() {
existingHistory.Update(historyFactory, tempReader);
} else {
var history = historyFactory.GetHistory(tempReader);
historyDict.Add(titleName, new TitleHistory(history));
historyDict.Add(titleName, new TitleHistory(history, ck3BookmarkDate));
}
}
});
Expand Down
25 changes: 16 additions & 9 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public World(Imperator.World impWorld, Configuration theConfiguration) {

// Load vanilla titles history
var titlesHistoryPath = Path.Combine(theConfiguration.Ck3Path, "game/history/titles");
titlesHistory = new TitlesHistory(titlesHistoryPath);
titlesHistory = new TitlesHistory(titlesHistoryPath, theConfiguration.Ck3BookmarkDate);

// Loading vanilla CK3 landed titles
var landedTitlesPath = Path.Combine(theConfiguration.Ck3Path, "game/common/landed_titles/00_landed_titles.txt");
Expand All @@ -61,12 +61,17 @@ public World(Imperator.World impWorld, Configuration theConfiguration) {

// Now we can deal with provinces since we know to whom to assign them. We first import vanilla province data.
// Some of it will be overwritten, but not all.
ImportVanillaProvinces(theConfiguration.Ck3Path);
ImportVanillaProvinces(theConfiguration.Ck3Path, theConfiguration.Ck3BookmarkDate);

// Next we import Imperator provinces and translate them ontop a significant part of all imported provinces.
ImportImperatorProvinces(impWorld);

ImportImperatorCharacters(impWorld, theConfiguration.ConvertBirthAndDeathDates, impWorld.EndDate);
ImportImperatorCharacters(
impWorld,
theConfiguration.ConvertBirthAndDeathDates,
impWorld.EndDate,
theConfiguration.Ck3BookmarkDate
);
LinkSpouses();
LinkMothersAndFathers();

Expand All @@ -77,19 +82,20 @@ public World(Imperator.World impWorld, Configuration theConfiguration) {

PurgeLandlessVanillaCharacters();
}
private void ImportImperatorCharacters(Imperator.World impWorld, bool convertBirthAndDeathDates, Date endDate) {
private void ImportImperatorCharacters(Imperator.World impWorld, bool convertBirthAndDeathDates, Date endDate, Date ck3BookmarkDate) {
Logger.Info("Importing Imperator Characters.");

foreach (var character in impWorld.Characters.StoredCharacters.Values) {
ImportImperatorCharacter(character, convertBirthAndDeathDates, endDate);
ImportImperatorCharacter(character, convertBirthAndDeathDates, endDate, ck3BookmarkDate);
}
Logger.Info($"{Characters.Count} total characters recognized.");
}

private void ImportImperatorCharacter(
Imperator.Characters.Character character,
bool convertBirthAndDeathDates,
Date endDate
Date endDate,
Date ck3BookmarkDate
) {
// Create a new CK3 character
var newCharacter = new Character();
Expand All @@ -102,7 +108,8 @@ Date endDate
provinceMapper,
deathReasonMapper,
convertBirthAndDeathDates,
endDate
endDate,
ck3BookmarkDate
);
character.CK3Character = newCharacter;
Characters.Add(newCharacter.ID, newCharacter);
Expand Down Expand Up @@ -148,7 +155,7 @@ Dictionary<ulong, Country> imperatorCountries
}
}

private void ImportVanillaProvinces(string ck3Path) {
private void ImportVanillaProvinces(string ck3Path, Date ck3BookmarkDate) {
Logger.Info("Importing Vanilla Provinces.");
// ---- Loading history/provinces
var path = Path.Combine(ck3Path, "game/history/provinces");
Expand All @@ -158,7 +165,7 @@ private void ImportVanillaProvinces(string ck3Path) {
continue;
var provincesPath = Path.Combine(ck3Path, "game/history/provinces", fileName);
try {
var newProvinces = new Provinces.Provinces(provincesPath);
var newProvinces = new Provinces.Provinces(provincesPath, ck3BookmarkDate);
foreach (var (newProvinceID, newProvince) in newProvinces.StoredProvinces) {
if (Provinces.ContainsKey(newProvinceID)) {
Logger.Warn($"Vanilla province duplication - {newProvinceID} already loaded! Overwriting.");
Expand Down
Loading

0 comments on commit 45f20f2

Please sign in to comment.