Skip to content

Commit

Permalink
Purge unneeded dynasty houses from CK3 (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Jun 2, 2024
1 parent 95b77af commit a2e0703
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 43 deletions.
4 changes: 2 additions & 2 deletions ImperatorToCK3.UnitTests/CK3/Characters/CK3CharacterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public void UnneededCharactersArePurged() {
ck3Characters.Add(unlandedFromCK3);

var titles = new Title.LandedTitles();
ck3Characters.PurgeUnneededCharacters(titles, new DynastyCollection(), ConversionDate);
ck3Characters.PurgeUnneededCharacters(titles, new DynastyCollection(), new HouseCollection(), ConversionDate);

Assert.Empty(ck3Characters);
}
Expand Down Expand Up @@ -508,7 +508,7 @@ public void DeadLandlessCharactersArePurgedIfChildless() {
Assert.Equal(dynasty.Id, fatherOfLandedCharacter.GetDynastyId(ConversionDate));
Assert.Equal(dynasty.Id, childlessRelative.GetDynastyId(ConversionDate));

ck3Characters.PurgeUnneededCharacters(titles, dynasties, ConversionDate);
ck3Characters.PurgeUnneededCharacters(titles, dynasties, new HouseCollection(), ConversionDate);

ck3Characters.Should().BeEquivalentTo(new[] {
landedCharacter,
Expand Down
6 changes: 3 additions & 3 deletions ImperatorToCK3.UnitTests/Outputter/DynastiesOutputterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DynastiesOutputterTests {

[Fact]
public void DynastiesAreOutputted() {
const string outputModName = "outputMod";
const string outputModPath = "output/outputMod";
var locDB = new LocDB("english");
const string imperatorRoot = "TestFiles/Imperator/root";
ModFilesystem irModFS = new(imperatorRoot, Array.Empty<Mod>());
Expand All @@ -50,13 +50,13 @@ public void DynastiesAreOutputted() {
};
dynasties.Add(dynasty2);

var outputPath = Path.Combine("output", outputModName, "common/dynasties/ir_dynasties.txt");
var outputPath = Path.Combine(outputModPath, "common/dynasties/irtock3_all_dynasties.txt");
if (File.Exists(outputPath)) {
// clean up from previous runs.
File.Delete(outputPath);
}
SystemUtils.TryCreateFolder(CommonFunctions.GetPath(outputPath));
DynastiesOutputter.OutputDynasties(outputModName, dynasties);
DynastiesOutputter.OutputDynasties(outputModPath, dynasties);

using var file = File.OpenRead(outputPath);
var reader = new StreamReader(file);
Expand Down
4 changes: 4 additions & 0 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ public void SetDynastyId(string dynastyId, Date? date) {
return History.GetFieldValue("dynasty", date)?.ToString();
}

public string? GetDynastyHouseId(Date date) {
return History.GetFieldValue("dynasty_house", date)?.ToString();
}

private string? jailorId;
public void SetEmployer(Character employer, Date? date) {
SetEmployerId(employer.Id, date);
Expand Down
5 changes: 3 additions & 2 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private static IEnumerable<string> LoadCharacterIDsToPreserve() {
return characterIDsToPreserve;
}

public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection dynasties, Date ck3BookmarkDate) {
public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection dynasties, HouseCollection houses, Date ck3BookmarkDate) {
Logger.Info("Purging unneeded characters...");

// Characters that hold or held titles should always be kept.
Expand Down Expand Up @@ -509,8 +509,9 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
charactersToCheck = charactersToCheck.Except(charactersToRemove).ToList();
} while(charactersToRemove.Count > 0);

// At this point we probably have many imported dynasties with no characters left.
// At this point we probably have many dynasties with no characters left.
// Let's purge them.
houses.PurgeUnneededHouses(this, ck3BookmarkDate);
dynasties.PurgeUnneededDynasties(this, ck3BookmarkDate);
}

Expand Down
25 changes: 23 additions & 2 deletions ImperatorToCK3/CK3/Dynasties/Dynasty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using ImperatorToCK3.Imperator.Cultures;
using ImperatorToCK3.Imperator.Families;
using ImperatorToCK3.Mappers.Culture;
using Open.Collections;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -50,9 +49,31 @@ public Dynasty(CK3.Characters.Character character, string irFamilyName, Cultures
SetLocFromImperatorFamilyName(Family.GetMaleForm(irFamilyName, irCulturesDB), locDB);
}

public Dynasty(string dynastyId, BufferedReader dynastyReader) {
Id = dynastyId;
FromImperator = false;

var parser = new Parser();
parser.RegisterKeyword("prefix", reader => Prefix = reader.GetString());
parser.RegisterKeyword("name", reader => Name = reader.GetString());
parser.RegisterKeyword("culture", reader => CultureId = reader.GetString());
parser.RegisterKeyword("motto", reader => Motto = reader.GetString());
parser.RegisterKeyword("forced_coa_religiongroup", reader => ForcedCoaReligionGroup = reader.GetString());
parser.IgnoreAndLogUnregisteredItems();
parser.ParseStream(dynastyReader);

if (string.IsNullOrEmpty(Name)) {
Logger.Warn($"Dynasty {Id} has no name! Setting fallback unlocalized name.");
Name = Id;
}
}

[NonSerialized] public string Id { get; }
[SerializedName("name")] public string Name { get; }
[SerializedName("prefix")] public string? Prefix { get; private set; }
[SerializedName("name")] public string Name { get; private set; }
[SerializedName("culture")] public string? CultureId { get; set; }
[SerializedName("motto")] public string? Motto { get; set; }
[SerializedName("forced_coa_religiongroup")] public string? ForcedCoaReligionGroup { get; set; }

[NonSerialized] public LocBlock? LocalizedName { get; private set; }
[NonSerialized] public StringOfItem? CoA { get; set; }
Expand Down
15 changes: 14 additions & 1 deletion ImperatorToCK3/CK3/Dynasties/DynastyCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using commonItems;
using commonItems.Collections;
using commonItems.Localization;
using commonItems.Mods;
using ImperatorToCK3.CK3.Characters;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.Mappers.Culture;
Expand Down Expand Up @@ -34,6 +35,18 @@ public void ImportImperatorFamilies(Imperator.World irWorld, CultureMapper cultu
Logger.IncrementProgress();
}

public void LoadCK3Dynasties(ModFilesystem ck3ModFS) {
Logger.Info("Loading dynasties from CK3...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, dynastyId) => {
var dynasty = new Dynasty(dynastyId, reader);
AddOrReplace(dynasty);
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/dynasties", ck3ModFS, "txt", recursive: true, parallel: true);
}

private void CreateDynastiesForCharactersFromMinorFamilies(Imperator.World irWorld, LocDB locDB, Date date) {
Logger.Info("Creating dynasties for characters from minor families...");

Expand Down Expand Up @@ -97,7 +110,7 @@ public void PurgeUnneededDynasties(CharacterCollection characters, Date date) {
}

int removedCount = 0;
foreach (var dynasty in this.Where(d => d.FromImperator).ToList()) {
foreach (var dynasty in this.ToList()) {
if (!dynastiesToKeep.Contains(dynasty.Id)) {
Remove(dynasty.Id);
++removedCount;
Expand Down
30 changes: 30 additions & 0 deletions ImperatorToCK3/CK3/Dynasties/House.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using commonItems;
using commonItems.Collections;
using commonItems.Serialization;
using commonItems.SourceGenerators;

namespace ImperatorToCK3.CK3.Dynasties;

[SerializationByProperties]
public partial class House : IPDXSerializable, IIdentifiable<string> {
[NonSerialized] public string Id { get; }

[SerializedName("prefix")] public string? Prefix { get; private set; }
[SerializedName("name")] public string? Name { get; private set; }
[SerializedName("dynasty")] public string? DynastyId { get; private set; }
[SerializedName("motto")] public string? Motto { get; private set; }
[SerializedName("forced_coa_religiongroup")] public string? ForcedCoaReligionGroup { get; private set; }

public House(string id, BufferedReader houseReader) {
Id = id;

var parser = new Parser();
parser.RegisterKeyword("prefix", reader => Prefix = reader.GetString());
parser.RegisterKeyword("name", reader => Name = reader.GetString());
parser.RegisterKeyword("dynasty", reader => DynastyId = reader.GetString());
parser.RegisterKeyword("motto", reader => Motto = reader.GetString());
parser.RegisterKeyword("forced_coa_religiongroup", reader => ForcedCoaReligionGroup = reader.GetString());
parser.IgnoreAndLogUnregisteredItems();
parser.ParseStream(houseReader);
}
}
43 changes: 43 additions & 0 deletions ImperatorToCK3/CK3/Dynasties/HouseCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using commonItems;
using commonItems.Collections;
using commonItems.Mods;
using ImperatorToCK3.CK3.Characters;
using System.Linq;

namespace ImperatorToCK3.CK3.Dynasties;

public class HouseCollection : IdObjectCollection<string, House> {
public void LoadCK3Houses(ModFilesystem ck3ModFS) {
Logger.Info("Loading dynasty houses from CK3...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, houseId) => {
var house = new House(houseId, reader);
AddOrReplace(house);
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/dynasty_houses", ck3ModFS, "txt", recursive: true, parallel: true);
}

public void PurgeUnneededHouses(CharacterCollection ck3Characters, Date date) {
Logger.Info("Purging unneeded dynasty houses...");

var houseIdsToKeep = ck3Characters
.Select(c => c.GetDynastyHouseId(date))
.Where(id => id is not null)
.Distinct()
.Cast<string>()
.ToHashSet();

int removedCount = 0;
foreach (var house in this.ToList()) {
if (houseIdsToKeep.Contains(house.Id)) {
continue;
}

Remove(house.Id);
++removedCount;
}
Logger.Info($"Purged {removedCount} unneeded dynasty houses.");
}
}
7 changes: 5 additions & 2 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class World {
private ScriptValueCollection ScriptValues { get; } = new();
public NamedColorCollection NamedColors { get; } = new();
public CharacterCollection Characters { get; } = new();
public DynastyCollection Dynasties { get; } = new();
public DynastyCollection Dynasties { get; } = [];
public HouseCollection DynastyHouses { get; } = [];
public ProvinceCollection Provinces { get; } = new();
public Title.LandedTitles LandedTitles { get; } = new();
public PillarCollection CulturalPillars { get; }
Expand Down Expand Up @@ -203,7 +204,9 @@ public World(Imperator.World impWorld, Configuration config) {
);
ClearFeaturedCharactersDescriptions(config.CK3BookmarkDate);

Dynasties.LoadCK3Dynasties(ModFS);
Dynasties.ImportImperatorFamilies(impWorld, cultureMapper, impWorld.LocDB, CorrectedDate);
DynastyHouses.LoadCK3Houses(ModFS);

// Load existing CK3 government IDs.
Logger.Info("Loading CK3 government IDs...");
Expand Down Expand Up @@ -283,7 +286,7 @@ public World(Imperator.World impWorld, Configuration config) {
Characters.ImportLegions(LandedTitles, impWorld.Units, impWorld.Characters, CorrectedDate, unitTypeMapper, MenAtArmsTypes, provinceMapper, config);

Characters.RemoveEmployerIdFromLandedCharacters(LandedTitles, CorrectedDate);
Characters.PurgeUnneededCharacters(LandedTitles, Dynasties, config.CK3BookmarkDate);
Characters.PurgeUnneededCharacters(LandedTitles, Dynasties, DynastyHouses, config.CK3BookmarkDate);

// Check if any muslim religion exists in Imperator. Otherwise, remove Islam from the entire CK3 map.
var possibleMuslimReligionNames = new List<string> { "muslim", "islam", "sunni", "shiite" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ l_english:
galatian: "Galatian"
galatian_collective_noun: "Galatians"
galatian_prefix: "Galato" # found 'Galato–Lycaonian'
hunnic: "Hunnic"
hunnic_collective_noun: "Huns"
hunnic_prefix: "Hunno" # found 'Hunno-Germanic'
#hunnic_prefix: "Hunno" # found 'Hunno-Germanic'
urslavic: "Slavic"
urslavic_collective_noun: "Slavs"
urslavic_prefix: "Slavo" # found 'Slavo-Germanic'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
Alcimus: "Alcimus"
Guendolen: "Guendolen"
Dynwen: "Dynwen"
Kawng_Tai: "Kawng Tai"
#Kawng_Tai: "Kawng Tai"

# Oscan
Aulis: "Aulis"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
Abis:0 "Abis"
Abish:0 "Abish"
Abishqa:0 "Abishqa"
Abiyasa:0 "Abiyasa"
#Abiyasa:0 "Abiyasa"
Abli:0 "Abli"
Abo:0 "Abo"
Abog:0 "Abog"
Expand Down Expand Up @@ -818,8 +818,8 @@
Batei:0 "Batei"
Bati:0 "Bati"
Batik:0 "Batik"
Batis:0 "Batis"
Bato:0 "Bato"
#Batis:0 "Batis"
#Bato:0 "Bato"
Batommavong:0 "Batommavong"
Batongbacal:0 "Batongbacal"
Batongmalaque:0 "Batongmalaque"
Expand Down Expand Up @@ -1501,7 +1501,7 @@
Cheongja:0 "Cheongja"
Cheonmyeong:0 "Cheonmyeong"
Cheorin:0 "Cheorin"
Chert:0 "Chert"
#Chert:0 "Chert"
Chervang:0 "Chervang"
Cherzong:0 "Cherzong"
Chesa:0 "Chesa"
Expand Down Expand Up @@ -3201,7 +3201,7 @@
Isini:0 "Isini"
Isip:0 "Isip"
Ismah:0 "Ismah"
Ismail:0 "İsmail"
#Ismail:0 "İsmail"
Isog:0 "Isog"
Isonashi:0 "Isonashi"
Isot:0 "Isot"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@
galatian: "galatienne"
galatian_collective_noun: "Galates"
galatian_prefix: "galato"
hunnic: "hunnique"
hunnic_collective_noun: "Huns"
hunnic_prefix: "hunno"
#hunnic: "hunnique"
#hunnic_collective_noun: "Huns"
#hunnic_prefix: "hunno"
urslavic: "slave"
urslavic_collective_noun: "Slaves"
urslavic_prefix: "slavo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@
galatian: "galatisch"
galatian_collective_noun: "Galater"
galatian_prefix: "galato"
hunnic: "hunnisch"
hunnic_collective_noun: "Hunnen"
hunnic_prefix: "hunno"
#hunnic: "hunnisch"
#hunnic_collective_noun: "Hunnen"
#hunnic_prefix: "hunno"
urslavic: "slawisch"
urslavic_collective_noun: "Slawen"
urslavic_prefix: "slawo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@
galatian: "갈라디아"
galatian_collective_noun: "갈라디아"
galatian_prefix: "갈라디아"
hunnic: ""
hunnic_collective_noun: ""
hunnic_prefix: ""
#hunnic: "훈"
#hunnic_collective_noun: "훈"
#hunnic_prefix: "훈"
urslavic: "슬라브어"
urslavic_collective_noun: "슬라브어"
urslavic_prefix: "슬라브어"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@
galatian: "Галаты"
galatian_collective_noun: "Галатск"
galatian_prefix: "Галатo"
hunnic: "Гунны"
hunnic_collective_noun: "Гуннск"
hunnic_prefix: "Гуннo"
#hunnic: "Гунны"
#hunnic_collective_noun: "Гуннск"
#hunnic_prefix: "Гуннo"
urslavic: "Эсты"
urslavic_collective_noun: "Эстийск"
urslavic_prefix: "Эсто"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@

galatian: "加拉太"
galatian_collective_noun: "加拉太人"
hunnic: "匈奴"
hunnic_collective_noun: "匈奴人"
#hunnic: "匈奴"
#hunnic_collective_noun: "匈奴人"
urslavic: "斯拉夫语"
urslavic_collective_noun: "斯拉夫语人"
oscan: "奥斯坎"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@

galatian: "gálata"
galatian_collective_noun: "Gálatas"
hunnic: "huna"
hunnic_collective_noun: "Hunos"
#hunnic: "huna"
#hunnic_collective_noun: "Hunos"
urslavic: "eslava"
urslavic_collective_noun: "Eslavos"
oscan: "osca"
Expand Down
Loading

0 comments on commit a2e0703

Please sign in to comment.