Skip to content

Commit

Permalink
Don't use I:R sea provinces when converting character's religion (#1899
Browse files Browse the repository at this point in the history
…) #patch
  • Loading branch information
IhateTrains authored Apr 21, 2024
1 parent c2bab20 commit c630f78
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public Character Build() {
traitMapper,
nicknameMapper,
locDB,
irMapData,
provinceMapper,
deathReasonMapper,
DNAFactory,
Expand Down
1 change: 1 addition & 0 deletions ImperatorToCK3.UnitTests/CK3/Dynasties/DynastyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public Character Build() {
traitMapper,
nicknameMapper,
locDB,
irMapData,
provinceMapper,
deathReasonMapper,
new DNAFactory(IRModFS, ck3ModFS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void FieldsDefaultToCorrectValues() {
Assert.Equal(0, character.Attributes.Zeal);
Assert.Null(character.DNA);
Assert.Equal((uint)0, character.Age);
Assert.Equal((ulong)0, character.ProvinceId);
Assert.Null(character.ProvinceId);
}

[Fact]
Expand Down
28 changes: 15 additions & 13 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using commonItems.Localization;
using ImperatorToCK3.CK3.Armies;
using ImperatorToCK3.CommonUtils;
using ImperatorToCK3.CommonUtils.Map;
using ImperatorToCK3.Exceptions;
using ImperatorToCK3.Imperator.Armies;
using ImperatorToCK3.Imperator.Countries;
Expand Down Expand Up @@ -307,6 +308,7 @@ public Character(
TraitMapper traitMapper,
NicknameMapper nicknameMapper,
LocDB locDB,
MapData irMapData,
ProvinceMapper provinceMapper, // used to determine ck3 province for religion mapper
DeathReasonMapper deathReasonMapper,
DNAFactory dnaFactory,
Expand Down Expand Up @@ -353,29 +355,29 @@ Configuration config
DNA = dnaFactory.GenerateDNA(ImperatorCharacter, ImperatorCharacter.PortraitData);
}

// Determine valid (not dropped in province mappings) "source province" to be used by religion mapper. Don't give up without a fight.
var impProvForProvinceMapper = ImperatorCharacter.ProvinceId;
if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Father is not null) {
// Determine valid (not dropped in province mappings) "source I:R province" and "source CK3 province"
// to be used by religion mapper. Don't give up without a fight.
ulong? irProvinceId = ImperatorCharacter.GetSourceLandProvince(irMapData);

var impProvForProvinceMapper = irProvinceId;
if ((!impProvForProvinceMapper.HasValue || provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper.Value).Count == 0) && ImperatorCharacter.Father is not null) {
impProvForProvinceMapper = ImperatorCharacter.Father.ProvinceId;
}

if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Mother is not null) {
if ((!impProvForProvinceMapper.HasValue || provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper.Value).Count == 0) && ImperatorCharacter.Mother is not null) {
impProvForProvinceMapper = ImperatorCharacter.Mother.ProvinceId;
}

if (provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper).Count == 0 && ImperatorCharacter.Spouses.Count > 0) {
if ((!impProvForProvinceMapper.HasValue || provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper.Value).Count == 0) && ImperatorCharacter.Spouses.Count > 0) {
var firstSpouse = ImperatorCharacter.Spouses.First().Value;
impProvForProvinceMapper = firstSpouse.ProvinceId;
}

var ck3ProvinceNumbers = provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper);
// determine CK3 province for religionMapper
ulong ck3Province = ck3ProvinceNumbers.Count > 0 ? ck3ProvinceNumbers[0] : 0;
var ck3ProvinceNumbers = impProvForProvinceMapper.HasValue ? provinceMapper.GetCK3ProvinceNumbers(impProvForProvinceMapper.Value) : [];
ulong? ck3Province = ck3ProvinceNumbers.Count > 0 ? ck3ProvinceNumbers[0] : null;

var cultureMatch = cultureMapper.Match(
ImperatorCharacter.Culture,
ck3Province,
ImperatorCharacter.ProvinceId,
irProvinceId,
ImperatorCharacter.Country?.HistoricalTag
);
if (cultureMatch is null) {
Expand All @@ -389,8 +391,8 @@ Configuration config
ImperatorCharacter.Religion,
GetCultureId(dateOnConversion),
ck3Province,
ImperatorCharacter.ProvinceId,
ImperatorCharacter.HomeCountry?.HistoricalTag,
irProvinceId,
ImperatorCharacter.HomeCountry?.HistoricalTag,
config
);
if (faithMatch is not null) {
Expand Down
4 changes: 4 additions & 0 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ImperatorToCK3.CK3.Cultures;
using ImperatorToCK3.CK3.Dynasties;
using ImperatorToCK3.CK3.Titles;
using ImperatorToCK3.CommonUtils.Map;
using ImperatorToCK3.Imperator.Armies;
using ImperatorToCK3.Mappers.Culture;
using ImperatorToCK3.Mappers.DeathReason;
Expand Down Expand Up @@ -43,6 +44,7 @@ Configuration config
traitMapper,
nicknameMapper,
impWorld.LocDB,
impWorld.MapData,
provinceMapper,
deathReasonMapper,
dnaFactory,
Expand Down Expand Up @@ -74,6 +76,7 @@ private void ImportImperatorCharacter(
TraitMapper traitMapper,
NicknameMapper nicknameMapper,
LocDB locDB,
MapData irMapData,
ProvinceMapper provinceMapper,
DeathReasonMapper deathReasonMapper,
DNAFactory dnaFactory,
Expand All @@ -89,6 +92,7 @@ Configuration config
traitMapper,
nicknameMapper,
locDB,
irMapData,
provinceMapper,
deathReasonMapper,
dnaFactory,
Expand Down
3 changes: 1 addition & 2 deletions ImperatorToCK3/CK3/Dynasties/Dynasty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ private void SetCultureFromImperator(Family irFamily, IReadOnlyList<Character> i
var irCultureId = irFamily.Culture;
var irProvinceIdForMapping = irMembers
.Select(m => m.ProvinceId)
.Where(id => id != 0)
.NullableFirstOrDefault();
.FirstOrDefault(id => id.HasValue);
var countryTag = irMembers
.Select(m => m.Country?.HistoricalTag)
.FirstOrDefault(tag => tag is not null, defaultValue: null);
Expand Down
4 changes: 2 additions & 2 deletions ImperatorToCK3/CK3/Titles/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ ImperatorRegionMapper imperatorRegionMapper

// ------------------ determine capital
var governorProvince = impGovernor.ProvinceId;
if (imperatorRegionMapper.ProvinceIsInRegion(governorProvince, governorship.Region.Id)) {
foreach (var ck3Prov in provinceMapper.GetCK3ProvinceNumbers(governorProvince)) {
if (governorProvince.HasValue && imperatorRegionMapper.ProvinceIsInRegion(governorProvince.Value, governorship.Region.Id)) {
foreach (var ck3Prov in provinceMapper.GetCK3ProvinceNumbers(governorProvince.Value)) {
var foundCounty = parentCollection.GetCountyForProvince(ck3Prov);
if (foundCounty is not null) {
CapitalCounty = foundCounty;
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3/CommonUtils/Map/MapData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public IReadOnlySet<ulong> GetNeighborProvinceIds(ulong provinceId) {
private bool IsStaticWater(ulong provinceId) => ProvinceDefinitions[provinceId].IsStaticWater;
private bool IsRiver(ulong provinceId) => ProvinceDefinitions[provinceId].IsRiver;

private bool IsLand(ulong provinceId) => ProvinceDefinitions[provinceId].IsLand;
public bool IsLand(ulong provinceId) => ProvinceDefinitions[provinceId].IsLand;

public IReadOnlySet<ulong> ColorableImpassableProvinceIds => ProvinceDefinitions
.Where(p => p.IsColorableImpassable).Select(p => p.Id)
Expand Down
38 changes: 37 additions & 1 deletion ImperatorToCK3/Imperator/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ImperatorToCK3.Imperator.Countries;
using ImperatorToCK3.Imperator.Families;
using ImperatorToCK3.CommonUtils.Genes;
using ImperatorToCK3.CommonUtils.Map;
using Open.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -59,7 +60,7 @@ public bool LinkFamily(FamilyCollection families, SortedSet<ulong>? missingDefin
}

public string? Nickname { get; set; }
public ulong ProvinceId { get; private set; } = 0;
public ulong? ProvinceId { get; private set; }
public Date BirthDate { get; private set; } = new(1, 1, 1);
public Date? DeathDate { get; set; }
public bool IsDead => DeathDate is not null;
Expand Down Expand Up @@ -298,4 +299,39 @@ public bool LinkFather(CharacterCollection characters) {
Logger.Warn($"Father ID: {fatherId} has no definition!");
return false;
}

/// <summary>
/// Returns a land province that can be considered a "source" of this character.
/// For instance, when a character is at sea, this method tries to use the country's capital,
/// or even the location of the character's parents.
/// </summary>
/// <param name="irMapData">Imperator map data.</param>
/// <returns></returns>
public ulong? GetSourceLandProvince(MapData irMapData) {
if (ProvinceId.HasValue && irMapData.IsLand(ProvinceId.Value)) {
return ProvinceId;
}

var homeCountryCapital = HomeCountry?.CapitalProvinceId;
if (homeCountryCapital.HasValue && irMapData.IsLand(homeCountryCapital.Value)) {
return homeCountryCapital;
}

var countryCapital = Country?.CapitalProvinceId;
if (countryCapital.HasValue && irMapData.IsLand(countryCapital.Value)) {
return countryCapital;
}

var fatherProvince = Father?.GetSourceLandProvince(irMapData);
if (fatherProvince.HasValue) {
return fatherProvince;
}

var motherProvince = Mother?.GetSourceLandProvince(irMapData);
if (motherProvince.HasValue) {
return motherProvince;
}

return null;
}
}

0 comments on commit c630f78

Please sign in to comment.