Skip to content

Add support for 3 more keywords in landed title definitions #2003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ public void LoadStaticTitles() {

Logger.IncrementProgress();
}
public void LoadStaticTitles(BufferedReader reader) {
Logger.Info("Loading static landed titles...");

var parser = new Parser();
RegisterKeys(parser);

parser.ParseStream(reader);

LogIgnoredTokens();

Logger.IncrementProgress();
}

public void CarveTitles(LandedTitles overrides) {
Logger.Debug("Carving titles...");
Expand Down Expand Up @@ -119,7 +107,9 @@ private void RegisterKeys(Parser parser) {
}

private static void LogIgnoredTokens() {
Logger.Debug($"Ignored Title tokens: {Title.IgnoredTokens}");
if (Title.IgnoredTokens.Count > 0) {
Logger.Warn($"Ignored title tokens: {Title.IgnoredTokens}");
}
}

public Title Add(string id) {
Expand Down
6 changes: 6 additions & 0 deletions ImperatorToCK3/CK3/Titles/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,163 +166,163 @@
vassal.DeJureLiege = this;
}
}
public void InitializeFromTag(
Country country,
Dependency? dependency,
CountryCollection imperatorCountries,
LocDB locDB,
ProvinceMapper provinceMapper,
CoaMapper coaMapper,
GovernmentMapper governmentMapper,
SuccessionLawMapper successionLawMapper,
DefiniteFormMapper definiteFormMapper,
ReligionMapper religionMapper,
CultureMapper cultureMapper,
NicknameMapper nicknameMapper,
CharacterCollection characters,
Date conversionDate,
Configuration config
) {
ImperatorCountry = country;
ImperatorCountry.CK3Title = this;

LocBlock? validatedName = GetValidatedName(country, imperatorCountries, locDB);

HasDefiniteForm = definiteFormMapper.IsDefiniteForm(ImperatorCountry.Name);
RulerUsesTitleName = false;

PlayerCountry = ImperatorCountry.PlayerCountry;

ClearHolderSpecificHistory();

FillHolderAndGovernmentHistory();

// Determine color.
var color1Opt = ImperatorCountry.Color1;
if (color1Opt is not null) {
Color1 = color1Opt;
}

// determine successions laws
History.AddFieldValue(conversionDate,
"succession_laws",
"succession_laws",
successionLawMapper.GetCK3LawsForImperatorLaws(ImperatorCountry.GetLaws())
);

// Determine CoA.
if (IsCreatedFromImperator || !config.UseCK3Flags) {
CoA = coaMapper.GetCoaForFlagName(ImperatorCountry.Flag);
}

// Determine other attributes:
// Set capital to Imperator tag's capital.
if (ImperatorCountry.CapitalProvinceId is not null) {
var srcCapital = ImperatorCountry.CapitalProvinceId.Value;
foreach (var ck3ProvId in provinceMapper.GetCK3ProvinceNumbers(srcCapital)) {
var foundCounty = parentCollection.GetCountyForProvince(ck3ProvId);
if (foundCounty is null) {
continue;
}

// If the title is a de jure duchy, potential capital must be within it.
if (Rank == TitleRank.duchy && DeJureVassals.Count > 0 && foundCounty.DeJureLiege?.Id != Id) {
continue;
}

CapitalCounty = foundCounty;
break;
}
}

// determine country name localization
var nameSet = false;
if (validatedName is not null) {
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock.CopyFrom(validatedName);
nameSet = true;
}
if (!nameSet) {
var irTagLoc = locDB.GetLocBlockForKey(ImperatorCountry.Tag);
if (irTagLoc is not null) {
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock.CopyFrom(irTagLoc);
nameSet = true;
}
}
if (!nameSet) {
// use unlocalized name if not empty
var name = ImperatorCountry.Name;
if (!string.IsNullOrEmpty(name)) {
Logger.Warn($"Using unlocalized Imperator name {name} as name for {Id}!");
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock[ConverterGlobals.PrimaryLanguage] = name;
nameSet = true;
}
}
// giving up
if (!nameSet) {
Logger.Warn($"{Id} needs help with localization! {ImperatorCountry.Name}?");
}

// determine adjective localization
TrySetAdjectiveLoc(locDB, imperatorCountries);

void FillHolderAndGovernmentHistory() {
// ------------------ determine previous and current holders

foreach (var impRulerTerm in ImperatorCountry.RulerTerms) {
var rulerTerm = new RulerTerm(
impRulerTerm,
characters,
governmentMapper,
locDB,
religionMapper,
cultureMapper,
nicknameMapper,
provinceMapper,
config
);

var characterId = rulerTerm.CharacterId;
if (characterId is null) {
continue;
}
var gov = rulerTerm.Government;

var termStartDate = new Date(rulerTerm.StartDate);
var ruler = characters[characterId];
if (ruler.DeathDate is not null && ruler.DeathDate < termStartDate) {
Logger.Warn($"{ruler.Id} can not begin his rule over {Id} after his death, skipping!");
continue;
}

History.AddFieldValue(termStartDate, "holder", "holder", characterId);
if (gov is not null) {
History.AddFieldValue(termStartDate, "government", "government", gov);
}
}

if (ImperatorCountry.Government is not null) {
var lastCK3TermGov = GetGovernment(conversionDate);
var ck3CountryGov = governmentMapper.GetCK3GovernmentForImperatorGovernment(ImperatorCountry.Government, ImperatorCountry.PrimaryCulture);
if (lastCK3TermGov != ck3CountryGov && ck3CountryGov is not null) {
History.AddFieldValue(conversionDate, "government", "government", ck3CountryGov);
}
}
}

// If country is a subject, convert it to a vassal.
if (dependency is not null) {
var overLordTitle = imperatorCountries[dependency.OverlordId].CK3Title;
if (overLordTitle is null) {
Logger.Warn($"Can't find CK3 title for country {dependency.OverlordId}, overlord of {country.Id}.");
}
DeJureLiege = overLordTitle;
SetDeFactoLiege(overLordTitle, dependency.StartDate);
}
}

Check notice on line 325 in ImperatorToCK3/CK3/Titles/Title.cs

View check run for this annotation

codefactor.io / CodeFactor

ImperatorToCK3/CK3/Titles/Title.cs#L169-L325

Complex Method
internal void RemoveDeFactoLiegeReferences(string liegeName) {
if (!History.Fields.TryGetValue("liege", out var liegeField)) {
return;
Expand Down Expand Up @@ -544,95 +544,95 @@
}
}

private void TrySetNameFromGovernorship(
Governorship governorship,
ImperatorRegionMapper irRegionMapper,
Country country,
Imperator.Provinces.ProvinceCollection irProvinces,
bool regionHasMultipleGovernorships,
LocDB locDB
) {
if (Localizations.ContainsKey(Id)) {
return;
}

var nameSet = false;
var regionId = governorship.Region.Id;
irRegionMapper.Regions.TryGetValue(regionId, out var region);
LocBlock? regionLocBlock = locDB.GetLocBlockForKey(regionId);

// If any area in the region is at least 60% owned, use the area name for governorship name.
if (regionHasMultipleGovernorships && region is not null) {
Area? potentialSourceArea = null;
float biggestOwnershipPercentage = 0f;
foreach (var area in region.Areas) {
var areaProvinces = area.Provinces;
if (areaProvinces.Count == 0) {
continue;
}
var controlledProvinces = areaProvinces.Where(p => country.Equals(p.OwnerCountry));
var ownershipPercentage = (float)controlledProvinces.Count() / areaProvinces.Count;
if (ownershipPercentage < 0.6) {
continue;
}
if (ownershipPercentage > biggestOwnershipPercentage) {
potentialSourceArea = area;
biggestOwnershipPercentage = ownershipPercentage;
}
}

if (potentialSourceArea is not null && locDB.TryGetValue(potentialSourceArea.Id, out var areaLocBlock)) {
Logger.Debug($"Naming {Id} after I:R area {potentialSourceArea.Id} majorly ({biggestOwnershipPercentage:P}) controlled by {country.Tag}...");
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock.CopyFrom(areaLocBlock);
nameSet = true;

var adjLocBlock = Localizations.AddLocBlock($"{Id}_adj");
adjLocBlock.CopyFrom(nameLocBlock);
adjLocBlock.ModifyForEveryLanguage((loc, language) => language == "english" ? loc?.GetAdjective() : loc);
}
}
// Try to use the name of most developed owned territory in the region.
if (!nameSet && regionHasMultipleGovernorships && region is not null) {
var sourceProvince = irProvinces
.Where(p => region.ContainsProvince(p.Id) && country.Equals(p.OwnerCountry))
.MaxBy(p => p.CivilizationValue);
if (sourceProvince is not null && locDB.TryGetValue(sourceProvince.Name, out var provinceLocBlock)) {
Logger.Debug($"Naming {Id} after most developed I:R territory: {sourceProvince.Id}...");
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock.CopyFrom(provinceLocBlock);
nameSet = true;

var adjLocBlock = Localizations.AddLocBlock($"{Id}_adj");
adjLocBlock.CopyFrom(nameLocBlock);
adjLocBlock.ModifyForEveryLanguage((loc, language) => language == "english" ? loc?.GetAdjective() : loc);
}
}
// Try to use "<country adjective> <region name>" as governorship name if region has multiple governorships.
// Example: Mauretania -> Roman Mauretania
if (!nameSet && regionHasMultipleGovernorships && regionLocBlock is not null) {
var ck3Country = country.CK3Title;
if (ck3Country is not null && ck3Country.Localizations.TryGetValue($"{ck3Country.Id}_adj", out var countryAdjectiveLocBlock)) {
Logger.Debug($"Naming {Id} after governorship with country adjective: {country.Tag} {governorship.Region.Id}...");
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock.CopyFrom(regionLocBlock);
nameLocBlock.ModifyForEveryLanguage(countryAdjectiveLocBlock,
(orig, adj, _) => $"{adj} {orig}"
);
nameSet = true;
}
}
if (!nameSet && regionLocBlock is not null) {
Logger.Debug($"Naming {Id} after governorship: {governorship.Region.Id}...");
var nameLocBlock = Localizations.AddLocBlock(Id);
nameLocBlock.CopyFrom(regionLocBlock);
nameSet = true;
}
if (!nameSet && Id.Contains("_IRTOCK3_")) {
Logger.Warn($"{Id} needs help with localization!");
}
}

Check notice on line 635 in ImperatorToCK3/CK3/Titles/Title.cs

View check run for this annotation

codefactor.io / CodeFactor

ImperatorToCK3/CK3/Titles/Title.cs#L547-L635

Complex Method
public void LoadTitles(BufferedReader reader) {
var parser = new Parser();
RegisterKeys(parser);
Expand Down Expand Up @@ -949,9 +949,12 @@
[SerializedName("ruler_uses_title_name")] public bool RulerUsesTitleName { get; set; } = false;

[SerializedName("ai_primary_priority")] public StringOfItem? AIPrimaryPriority { get; private set; }
[SerializedName("ignore_titularity_for_title_weighting")] public bool? IgnoreTitularityForTitleWeighting { get; private set; }
[SerializedName("can_create")] public StringOfItem? CanCreate { get; private set; }
[SerializedName("can_create_on_partition")] public StringOfItem? CanCreateOnPartition { get; private set; }
[SerializedName("can_destroy")] public StringOfItem? CanDestroy { get; private set; }
[SerializedName("destroy_if_invalid_heir")] public bool? DestroyIfInvalidHeir { get; set; }
[SerializedName("destroy_on_succession")] public bool? DestroyOnSuccession { get; set; }
[SerializedName("no_automatic_claims")] public bool? NoAutomaticClaims { get; set; }
[SerializedName("always_follows_primary_heir")] public bool? AlwaysFollowsPrimaryHeir { get; set; }
[SerializedName("de_jure_drift_disabled")] public bool? DeJureDriftDisabled { get; set; }
Expand Down Expand Up @@ -1019,10 +1022,13 @@
});
parser.RegisterKeyword("capital", reader => CapitalCountyId = reader.GetString());
parser.RegisterKeyword("ai_primary_priority", reader => AIPrimaryPriority = reader.GetStringOfItem());
parser.RegisterKeyword("ignore_titularity_for_title_weighting", reader => IgnoreTitularityForTitleWeighting = reader.GetBool());
parser.RegisterKeyword("can_create", reader => CanCreate = reader.GetStringOfItem());
parser.RegisterKeyword("can_create_on_partition", reader => CanCreateOnPartition = reader.GetStringOfItem());
parser.RegisterKeyword("can_destroy", reader => CanDestroy = reader.GetStringOfItem());
parser.RegisterKeyword("province", reader => ProvinceId = reader.GetULong());
parser.RegisterKeyword("destroy_if_invalid_heir", reader => DestroyIfInvalidHeir = reader.GetBool());
parser.RegisterKeyword("destroy_on_succession", reader => DestroyOnSuccession = reader.GetBool());
parser.RegisterKeyword("no_automatic_claims", reader => NoAutomaticClaims = reader.GetBool());
parser.RegisterKeyword("always_follows_primary_heir", reader => AlwaysFollowsPrimaryHeir = reader.GetBool());
parser.RegisterKeyword("de_jure_drift_disabled", reader => DeJureDriftDisabled = reader.GetBool());
Expand Down
Loading