Skip to content

Commit 9b6f758

Browse files
authored
Handle an I:R unit having no leader (#2819) #patch
Sentry event ID: accd5806778747d1b87d0faa442d3af6
1 parent 98d1a09 commit 9b6f758

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

ImperatorToCK3/CK3/Characters/Character.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,8 @@ CK3LocDB ck3LocDB
778778
}
779779
internal void ImportUnitsAsSpecialTroops(
780780
IEnumerable<Unit> countryUnits,
781-
Imperator.Characters.CharacterCollection imperatorCharacters,
781+
Imperator.Characters.CharacterCollection irCharacters,
782+
CountryCollection irCountries,
782783
Date date,
783784
UnitTypeMapper unitTypeMapper,
784785
ProvinceMapper provinceMapper,
@@ -790,8 +791,17 @@ CK3LocDB ck3LocDB
790791
foreach (var unit in countryUnits) {
791792
var menPerUnitType = unitTypeMapper.GetMenPerCK3UnitType(unit.MenPerUnitType);
792793

793-
var imperatorLeader = imperatorCharacters[unit.LeaderId];
794-
var ck3Leader = imperatorLeader.CK3Character;
794+
if (unit.LeaderId is null || !irCharacters.TryGetValue(unit.LeaderId.Value, out var irLeader)) {
795+
// Use country ruler.
796+
irLeader = irCountries[unit.CountryId].Monarch;
797+
}
798+
799+
if (irLeader is null) {
800+
Logger.Warn($"Unit {unit.Id} has no leader and country {unit.CountryId} has no ruler! Skipping special troop spawn.");
801+
continue;
802+
}
803+
804+
var ck3Leader = irLeader.CK3Character;
795805

796806
sb.AppendLine("\t\tspawn_army={");
797807

ImperatorToCK3/CK3/Characters/CharacterCollection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using ImperatorToCK3.CommonUtils.Map;
1010
using ImperatorToCK3.Imperator.Armies;
1111
using ImperatorToCK3.Imperator.Characters;
12+
using ImperatorToCK3.Imperator.Countries;
1213
using ImperatorToCK3.Mappers.Culture;
1314
using ImperatorToCK3.Mappers.DeathReason;
1415
using ImperatorToCK3.Mappers.Nickname;
@@ -632,6 +633,7 @@ internal void ImportLegions(
632633
Title.LandedTitles titles,
633634
UnitCollection imperatorUnits,
634635
Imperator.Characters.CharacterCollection imperatorCharacters,
636+
CountryCollection irCountries,
635637
Date date,
636638
UnitTypeMapper unitTypeMapper,
637639
IdObjectCollection<string, MenAtArmsType> menAtArmsTypes,
@@ -660,7 +662,7 @@ Configuration config
660662
if (config.LegionConversion == LegionConversion.MenAtArms) {
661663
ruler.ImportUnitsAsMenAtArms(countryLegions, date, unitTypeMapper, menAtArmsTypes, ck3LocDB);
662664
} else if (config.LegionConversion == LegionConversion.SpecialTroops) {
663-
ruler.ImportUnitsAsSpecialTroops(countryLegions, imperatorCharacters, date, unitTypeMapper, provinceMapper, ck3LocDB);
665+
ruler.ImportUnitsAsSpecialTroops(countryLegions, imperatorCharacters, irCountries, date, unitTypeMapper, provinceMapper, ck3LocDB);
664666
}
665667
}
666668

ImperatorToCK3/CK3/World.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac
361361

362362
// Gold needs to be distributed after characters' successors are generated.
363363
Characters.DistributeCountriesGold(LandedTitles, config);
364-
Characters.ImportLegions(LandedTitles, impWorld.Units, impWorld.Characters, CorrectedDate, unitTypeMapper, MenAtArmsTypes, provinceMapper, LocDB, config);
364+
Characters.ImportLegions(LandedTitles, impWorld.Units, impWorld.Characters, impWorld.Countries, CorrectedDate, unitTypeMapper, MenAtArmsTypes, provinceMapper, LocDB, config);
365365

366366
// After the purging of unneeded characters, we should clean up the title history.
367367
LandedTitles.CleanUpHistory(Characters, config.CK3BookmarkDate);

ImperatorToCK3/Imperator/Armies/Unit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ internal sealed class Unit : IIdentifiable<ulong> {
1313
public bool IsArmy { get; private set; } = true;
1414
public bool IsLegion { get; private set; } = false;
1515
public ulong CountryId { get; set; }
16-
public ulong LeaderId { get; set; } // character id
16+
public ulong? LeaderId { get; set; } // character id
1717
public ulong Location { get; set; } // province id
18-
private List<ulong> CohortIds { get; } = new();
18+
private List<ulong> CohortIds { get; } = [];
1919

2020
public LocBlock? LocalizedName { get; private set; }
2121
public FrozenDictionary<string, int> MenPerUnitType { get; }

0 commit comments

Comments
 (0)