Skip to content

Commit

Permalink
Fix crash on loading an 867 AD conversion in CK3 (#2217) #patch
Browse files Browse the repository at this point in the history
closes #2130
  • Loading branch information
IhateTrains committed Sep 22, 2024
1 parent 63c4c96 commit edf280f
Show file tree
Hide file tree
Showing 13 changed files with 1,228 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CharacterOutputterTests {
[Fact]
public void PregnancyIsOutputted() {
Date conversionDate = "600.8.1";
Date bookmarkDate = "600.1.1";
Date childBirthDate = "600.10.7";
Date conceptionDate = childBirthDate.ChangeByDays(-280);

Expand All @@ -20,7 +21,7 @@ public void PregnancyIsOutputted() {
pregnantFemale.Pregnancies.Add(new Pregnancy(fatherId:"2", motherId: "1", childBirthDate, isBastard:false));

var sb = new StringBuilder();
CharacterOutputter.WriteCharacter(sb, pregnantFemale, conversionDate);
CharacterOutputter.WriteCharacter(sb, pregnantFemale, conversionDate, bookmarkDate);

var outputString = sb.ToString();
outputString.Should().Contain("female = yes");
Expand Down
1 change: 0 additions & 1 deletion ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public string? DeathReason {
}
}

public bool Dead => DeathDate is not null;
public IList<Pregnancy> Pregnancies { get; } = new List<Pregnancy>();

public IDictionary<string, int> MenAtArmsStacksPerType { get; } = new Dictionary<string, int>();
Expand Down
13 changes: 10 additions & 3 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@ public void LoadCharacterIDsToPreserve(Date ck3BookmarkDate) {
character.IsNonRemovable = true;
character.BirthDate = ck3BookmarkDate.ChangeByDays(1);
character.DeathDate = ck3BookmarkDate.ChangeByDays(2);
// Remove all dated history entries other than birth and death.
foreach (var field in character.History.Fields) {
if (field.Id == "birth" || field.Id == "death") {
continue;
}
field.DateToEntriesDict.Clear();
}
}
});
parser.IgnoreAndLogUnregisteredItems();
Expand All @@ -485,13 +492,13 @@ public void PurgeUnneededCharacters(Title.LandedTitles titles, DynastyCollection
.ToArray();
charactersToCheck = charactersToCheck.Except(imperatorTitleHolders);

// Don't purge animation_test or easter egg characters.
// Don't purge animation_test characters.
charactersToCheck = charactersToCheck
.Where(c => !c.Id.StartsWith("animation_test_") && !c.Id.StartsWith("easteregg_"));
.Where(c => !c.Id.StartsWith("animation_test_"));

// Keep alive Imperator characters.
charactersToCheck = charactersToCheck
.Where(c => c is not {FromImperator: true, Dead: false});
.Where(c => c is not {FromImperator: true, ImperatorCharacter.IsDead: false});

// Make some exceptions for characters referenced in game's script files.
charactersToCheck = charactersToCheck
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/Dynasties/DynastyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void FlattenDynastiesWithNoFounders(CharacterCollection characters, House

foreach (var character in cadetHouseMembers) {
character.ClearDynastyHouse();
character.SetDynastyId(dynasty.Id, date);
character.SetDynastyId(dynasty.Id, null);
}

// Remove all the cadet houses.
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3/CK3/Religions/ReligionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Date date
Logger.Info("Generating religious heads for faiths with Spiritual Head of Faith doctrine...");

var aliveCharacterFaithIds = characters
.Where(c => !c.Dead)
.Where(c => c.DeathDate is null || c.DeathDate > date)
.Select(c => c.GetFaithId(date)).ToImmutableHashSet();

var provinceFaithIds = provinces
Expand Down
18 changes: 17 additions & 1 deletion ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ private void OverwriteCountiesHistory(CountryCollection irCountries, IEnumerable
Logger.Info("Overwriting counties' history...");
HashSet<Governorship> governorshipsSet = governorships.ToHashSet();
HashSet<Governorship> countyLevelGovernorshipsSet = countyLevelGovernorships.ToHashSet();

foreach (var county in LandedTitles.Where(t => t.Rank == TitleRank.county)) {
if (county.CapitalBaronyProvinceId is null) {
Logger.Warn($"County {county} has no capital barony province!");
Expand Down Expand Up @@ -468,6 +468,7 @@ private void OverwriteCountiesHistory(CountryCollection irCountries, IEnumerable
if (irCountry is null || irCountry.CountryType == CountryType.rebels) { // e.g. uncolonized Imperator province
county.SetHolder(null, conversionDate);
county.SetDeFactoLiege(null, conversionDate);
RevokeBaroniesFromCountyGivenToImperatorCharacter(county);
} else {
bool given = TryGiveCountyToCountyLevelRuler(county, irCountry, countyLevelCountries, irCountries);
if (!given) {
Expand All @@ -492,6 +493,7 @@ private bool TryGiveCountyToMonarch(Title county, Country irCountry) {
}

GiveCountyToMonarch(county, ck3Country);
RevokeBaroniesFromCountyGivenToImperatorCharacter(county);
return true;
}

Expand Down Expand Up @@ -543,6 +545,7 @@ private bool TryGiveCountyToGovernor(Title county,
} else {
GiveCountyToGovernor(county, ck3GovernorshipId);
}
RevokeBaroniesFromCountyGivenToImperatorCharacter(county);
return true;
}

Expand Down Expand Up @@ -607,9 +610,22 @@ private bool TryGiveCountyToCountyLevelRuler(Title county,
} else {
county.SetDeFactoLiege(null, ruleStartDate);
}
RevokeBaroniesFromCountyGivenToImperatorCharacter(county);
return true;
}

private void RevokeBaroniesFromCountyGivenToImperatorCharacter(Title county) {
foreach (var barony in county.DeJureVassals) {
// Skip the county capital barony.
if (barony.ProvinceId == county.CapitalBaronyProvinceId) {
continue;
}

// Clear the barony holders history.
barony.ClearHolderSpecificHistory();
}
}

private void HandleIcelandAndFaroeIslands(Configuration config) {
Logger.Info("Handling Iceland and Faroe Islands...");
Date bookmarkDate = config.CK3BookmarkDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,2 @@
# The vanilla CK3 Oghuz dynasty needs a CoA because we preserve the Seljuk cadet house.
7327 = k_oghuz_il
100721 = { # Abbasid
pattern = "pattern_solid.dds"
color1 = "black"
color2 = "white"
colored_emblem = {
texture = "ce_block_02.dds"
color1 = "black"
color2 = "black"
instance = { position = { 0.5 0.5 } scale = { 1.0 1.0 } }
}
colored_emblem = {
texture = "ce_block_02.dds"
color1 = "black"
color2 = "black"
instance = { position = { 0.5 0.48 } scale = { 0.70 0.70 } }
}
colored_emblem = {
texture = "ce_border_circle_fimbriated.dds"
color1 = "black"
color2 = "white"
instance = { position = { 0.5 0.48 } scale = { 0.75 0.75 } }
}
colored_emblem = {
texture = "ce_arabic_basmala_kufic.dds"
color1 = "white"
color2 = "white"
instance = { position = { 0.5 0.48 } scale = { 0.64 0.64 } }
}
}
103 changes: 103 additions & 0 deletions ImperatorToCK3/Data_Files/configurables/ck3_characters_to_preserve.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,107 @@ after_bookmark_date = {

# Characters referenced in achievements:
109607

# Characters eferenced in events\dlc\fp2\fp2_el_cid_events.txt:
107590 # El Cid

# Characters referenced in events\dlc\fp3\fp3_struggle_events.txt:
34010
34012
34013
34014
34005
33911

# Easter egg characters:
easteregg_veronica_pazos
easteregg_jakub_potapczyk
easteregg_charna_frostwhisper
easteregg_joe_parkin
easteregg_alessandro_bragalini
easteregg_daan_broekhof
easteregg_debbie_lane
easteregg_isabella_welch
easteregg_elisabeth_gangenes
easteregg_henrik_fahraeus
easteregg_alexander_oltner
easteregg_joacim_carlberg
easteregg_olof_bjork
easteregg_petter_lundh
easteregg_linnea_thimren
easteregg_matthew_clohessy
easteregg_steacy_mcilwham
easteregg_nils_wadensten
easteregg_emil_tisander
easteregg_joel_hansson
easteregg_malin_jonsson
easteregg_peter_johannesson
easteregg_linda_tiger
easteregg_katya_boestad
easteregg_mathilda_bjarnehed
easteregg_milla_lankinen
easteregg_petter_vilberg
easteregg_henrik_lohmander
easteregg_max_weltz
easteregg_lysann_schlegel
easteregg_sean_hughes
easteregg_ismael_serrano
easteregg_bianca_savazzi
easteregg_christian_daflos
easteregg_max_collin
easteregg_jonas_wickerstrom
easteregg_tegan_harris
easteregg_paul_depre
easteregg_maximilian_olbers
easteregg_hanna_lohman
easteregg_nikolaos_kaltsogiannis
easteregg_pontus_olofsson
easteregg_martin_anward
easteregg_zack_holmgren
easteregg_claudia_baldassi
easteregg_lukasz_opara
easteregg_mateusz_mierzwa
easteregg_natalia_lechnata
easteregg_natalia_poreba
easteregg_struan_mccallum
easteregg_riad_deneche
easteregg_anton_leirnes
easteregg_stella_carrick
easteregg_fenni_johansson
easteregg_james_beaumont
easteregg_bahar_shefket
easteregg_hugo_cortell
easteregg_alexander_newcombe
easteregg_elena_zenko
easteregg_fei_berg_hulthen
easteregg_stina_arvidsson_radestig
easteregg_ola_jentzsch
easteregg_nikola_stokic
easteregg_tamara_stokic
easteregg_sandra_runge_moller
easteregg_alessandro_bragalini
easteregg_jon_astrup_hallkvist
easteregg_maxence_voleau
easteregg_jason_cantalini
easteregg_fabian_franzen
easteregg_veronica_pazos
easteregg_weronika_kowalczyk
easteregg_jakob_jorstedt
easteregg_tess_lindholm
easteregg_luke_bean
easteregg_artur_kacprzak
easteregg_tomas_versekys
easteregg_ida_hedberg
easteregg_alexander_windahl
easteregg_johan_husen
easteregg_alexia_belfort
easteregg_david_drake
easteregg_ernesto_lopez
easteregg_lucas_ribeiro
easteregg_daniel_velasquez
easteregg_emelie_verdugo
easteregg_ida_hedberg
easteregg_francesca_passoni
easteregg_elisabeth_gangenes
easteregg_alva_granholm
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
# Comments are allowed, and start with a '#' character.

jamshid
100721 # Abbasid, referenced in events\dlc\fp2\fp2_yearly_events.txt
1029100 # Sassanid, referenced in events\dlc\fp3\fp3_struggle_events.txt
613 # Seljuk, referenced in events\yearly_events\yearly_events_persia.txt
Loading

0 comments on commit edf280f

Please sign in to comment.