• We have updated our Community Code of Conduct. Please read through the new rules for the forum that are an integral part of Paradox Interactive’s User Agreement.

CK2 Dev Diary #18: Optimization and modding

Good afternoon. I’m Magne Skjæran, and I’m currently interning as a programmer on Crusader Kings 2. Some might also know me from administrating the Paradox Wikis. My internship is reaching its end, so it was decided that a good way to cap it off would be for me to write a dev diary about two of my favorite topics: optimization and modding.

As you all know, as the game goes on it has a tendency to get slower and slower, mainly due to the gradual increase in the number of characters around. This can be especially annoying for users on weaker computers, but anyone that runs speed 5 is likely to notice.
Beyond that, we’ve got a few points where the game will halt for several seconds. The biggest of these is autosaves, but there’s also a pause at the end of each year, and a smaller pause at the end of each month.

We’ve not only made the effort to reduce these pauses significantly but also to improve the speed of the game generally. This has been hugely successful.
With 2.6, late-game autosaves are in our tests over twice as quick as they used to be, while the year-end lag is now barely more noticeable than the month-end lag, which has also been reduced significantly.
General, daily performance is also significantly faster; the individual days take less time to process.
Overall, in our tests, the game will get 30-50% further in the same period of time. Thus, in the time 10 years would take on speed 5 in 2.5.2, you’ll now most likely get 13-15 years into the game, and with far smaller annoying pauses.

We’ve achieved this through a combination of many different optimizations:
  • Uncompressed saves are now smaller than they used to be, saving compression and write times
  • Saves are now saved in a more efficient manner
  • Almost everything that happened at the end of the year is now distributed across the year instead, so as to avoid the noticeable pause
  • We’ve made improvements to the rendering of the map to speed it up significantly
  • We’ve gone through cases where map modes, names, etc. were being marked as “outdated” on the map and in the UI, and ensured they’re actually only updated when truly in need of update. As an example, previously the occupation overlay would sometimes be marked as needing an update when no occupation status had actually changed, which of course would waste CPU cycles
  • Several computations that used to be single-threaded are now multi-threaded
  • Various improvements to a variety of performance-intensive computations
  • Fewer random characters are generated than previously, and the game more aggressively eliminates unnecessary characters
To illustrate the last point, here’s the number of living characters by year from one of my test games:
vMnckpc.png


I don’t have a similar chart from 2.5, but as a point of comparison, 400 years into an old 769 save we had lying around there were 30 000 living characters, almost twice the number my test save peaked at.

Finally, you don’t just have to take my word for this; here’s a video showing 2.5.2 and 2.6 running side by side:

Now, let’s move on to modding.

I used to mod Crusader Kings 2, being one of the founders of the excellent Historical Immersion Project, so modding is a topic that’s very dear to me.

I know from experience that often modders have to write convoluted workarounds because the information they need isn’t easily available. I’ve therefore spent time both at work and on my free time implementing useful triggers and effects, even in cases where we at PDS have had no immediate need for those triggers. Divine has also spent time implementing a variety of modding features suggested by users, and other members of the team have implemented features that have been needed for Reaper’s Due. A number of bugs that affect modders have also been fixed, and a handful of new console commands added.

All told, our modding changelog is a bit over a hundred items long, and a number of items consist of more than one new trigger or effect.

The full list of modding changes in 2.6:
- Added possibility to have major province modifiers
- Causes of death can now be customized
- Execution method can be chosen via a priority system similar to government flavors
- added "health_penalty", "fertility_penalty", "martial_penalty", "intrigue_penalty", "diplomacy_penalty", "stewardship_penalty", "learning_penalty"
- a set of modifiers whose sum for a given stat can never be positive (allows your giving bonuses that only offset maluses instead of bonus that are positive no matter what)​
- Added military_techpoints, economy_techpoints, culture_techpoints as generic modifiers (for province modifiers)
- Added "any_demesne_province" as an effect
- holdings/settlement modifiers can now be hidden
- Added "has_any_symptom" and "symptom=" triggers
- Added "num_of_symptoms" trigger
- Added "spawn_disease" effect which spawns a specific disease in a specific province
- Scope must be a province
- "spawn_disease=disease_name"​
- Added "start_outbreak" which forces an outbreak, but follows the scenarios set up in the disease.txt file
- "start_outbreak=disease_name"
- Scope is not used​
- Added "on_outbreak" OnAction which is called when a new outbreak starts
- scope is the province where the outbreak starts
- token_data is the disease name​
- Added "scenarios" to diseases: they exclude certain regions and determine possible starting provinces
- Added various defines for modifiers to contagiousness of disease when determining the spread. They are only used with The Reaper's Due DLC
- Added yearly "on_province_major_modifier" on_action
- Added "regional_percentage" trigger:
regional_percentage = {
region = andalusia
percentage = 0.5

has_province_flag = aztec_explorers​
}​
- Added hospital_level trigger that tests the hospital level in a province. Province scope
- Added disease_defence trigger that tests the disease defense in a province. Province scope
- Added "has_game_rule" trigger to test the value of a game rule:
has_game_rule = {
name = gender
value = all​
}​
- Added triggers:
any_hospital
num_of_hospitals
num_of_hospitals_diff
has_hospital
hospital_has_any_building
is_hospital_decision_allowed
is_hospital_decision_potential​
- Added Event scope:
hospital_owner​
- Added supported_checksum = yes/no trigger. Added supported_checksums = { ABCD EFGH } field in .mod files. When supported_checksum trigger is used in game it tries to match the current checksum of the game with the entries in all active mods mod-files. The trigger returns true if it finds at least one match.
- Fixed an issue for mods trying to remove localisation from the base game's localisation.
- targeted_decisions, is_targeted_decision_allowed and is_targeted_decision_potential are now considered aliases for targetted_decisions, is_targetted_decision_allowed and is_targetted_decision_potential respectively and are evaluated in the same way.
- Fixed a CTD that could happen for mods that allowed you to play characters with only titular titles.
- scaled_wealth and transfer_scaled_wealth now supports an additional max field to cap the maximum amount of wealth gained/lost.
- Added enatic trait attribute equivalent of the agnatic attribute (which allowed certain traits, eg Sayyid, to always be inherited from the father).
- Added define MERCHANT_REPUBLIC_MAX_PATRICIANS which determines the amount of merchant republic families the game will allow to exist in a merchant republic.
- Added triggers monthly_prestige and monthly_piety.
- Added effects scaled_prestige and scaled_piety.
- It is now possible to script council voting for targeted_decisions.
- The council interface should now support voter minor titles with a grant limit higher than one (1).
- Added set_pentarch_title = <title/none> effect.
- The pentarchy entry in landed titles can now be assigned to a specific religion rather than all religions with autocephaly.
- Using title_prefix in a government definition should no longer default all non-prefix defined title names to the default versions.
- Ambitions are now valid for the ai aggression modifier "aggression".
- add_character_modifier and add_holding_modifier now supports the stacking = yes parameter.
- Added has_holding_modifier = x trigger that returns true of the holding has modifier x.
- Added has_instances_of_character_modifier, has_instances_of_province_modifier, has_instances_of_holding_modifier = { modifier = x amount = y } trigger where y is the amount of current applications of modifier x that the character needs to currently hold for the trigger to return true.
- Added remove_character_modifiers, remove_province_modifiers, remove_holding_modifiers = { modifier = x amount = y } effects that removes y amount of modifier x from the current scope.
- Added on_trade_post_construction_completed, on_fort_construction_completed on_action entries.
- Added on_trade_post_construction_start, on_fort_construction_start on action entries.
- Fixed some issues with on_settlement_construction_start on_action events.
- Added GetHusbandWifeCap, GetHusbandWifeCapOpp, GetMasterMistressCap, GetMasterMistressOpp, GetMasterMistressCapOpp character localisation functions.
- any_character trigger should no longer exclude the ROOT scope in it's evaluation.
- Fixed a possible issue with hidden effects inside scripted effects.
- Added trigger 'leader_traits' (number of)
- Reactivated the on_action 'on_focus_pulse'
- Disease effects are now customizable through script per disease
- Silk Road modifier icon can now be set in the text file
- Added define AUTOMATICALLY_ACCEPTS_COALITION_CALLS to determine if defensive pacts members automatically join at the start of wars
- Converted the Family Focus events to on_action events
- Fixed a bug where the 'end_war' effect would incorrectly invalidate wars
- The 'regent' event target is now properly invalidated if there is no regent
- Added trigger 'any_player'
- Added effect 'any_player'
- Added effect 'random_player'
- Added define WAR_CONTRIBUTION_THEOCRACY_GHW_MULT
- Fixed era bookmarks ignoring their defined text keys. WARNING: This means that your mod's era names are likely now unlocalised
- Fixed buttons F9 through F12 not working as shortcuts
- Fixed a number of triggers functioning identically when set to "= no" as when set to "= yes"
- has_terrain_specialization now takes "yes" and "no" as parameters as well, in addition to "any" and specific terrains
- Added missing localisation for a handful of triggers
- Era screen characters can now be restricted based on dlc by adding 'dlc = "dlc name"' to their entry
- Fixed the time control buttons being inactive in 867.1.1 in mods starting on or before that date
- Opinion modifiers can now be scripted to be stacking (default) or not
- Added a give_birth console command (requires existing pregnancy; use pollinate/cuckoo to impregnate)
- Added on_action 'on_holding_building_start'
- Added has_assigned_minor_title trigger. Usage: "has_assigned_minor_title = title_master_of_the_horse" or "has_assigned_minor_title = { title = title_commander count = 2 }"
- Added has_children and has_living_children triggers
- The "loot" trigger will no longer crash the game if applied to a land unit. It will always return false for land units, but it won't crash
- has_minor_title now also accepts "yes" and "no", to check whether the character holds any minor titles at all
- Can now add and destroying buildings in extra holdings (trade posts, forts & hospitals) with the following effects:
add_to_extra_holding = {
type = hospital
building = leper_colony_1​
}

destroy_in_extra_holding = {
type = hospital
building = leper_colony_1​
}​
- The following effects have been added. They work the same as the equivalent for forts or trade posts:
create_hospital
destroy_hospital
any_hospital​
- Scripted effects in event options will now show the traits and characters they affect
- Added a war_participation trigger. Example: any_war = { war_participation = { who = ROOT score = 0.5 } }
- Added an is_landed_title_allowed trigger, which checks whether a character fulfills the "allow" section of a specific title. Example: is_landed_title_allowed = e_hre or is_landed_title_allowed = ROOT, where ROOT is a title
- Added a "force_host" effect that forcibly changes the host a character. Scope should be the character being moved, target should be the character's new host.
Shouldn't be used lightly
force_host = ROOT​
- Added join_faction, leave_faction and start_faction effects.
- Added faction_exists trigger.
- Added export_to_variable effect.
- Added while effect.
- Title prefix defined in landed_titles.txt should now support localisation variations dependent on the holder of the title.
- Added sound_effect effect.
- It should now be possible to make baronies independent through history editing.
- Added a "character_stats" console command that prints a variety of statistics to the console, such as the # of rulers of each tier, gender breakdown, and total wealth. This also gets written to stats.log
- Added a "dynasty_stats" console command that prints a variety of statistics to the console, such as the # of dynasties, number of single-person dynasties, and number of dynasties with only dead characters. This also gets written to stats.log
- Added a COURT_PRUNE_SIZE define, which defines when the game will try to find unneeded courtiers to kill off. Lowered to 10 from the original 20
- Added a "de_facto_liege_title" trigger, which checks that the defacto liege title of a character or title is the right-hand side. Example: ROOT = { de_facto_liege_title = e_hre }. Both sides can take a character or a title. If a character is provided, their primary title will be used. A defacto_liege_title scope already existed, but this should make some operations easier
- Added an "immortal" trigger, which checks if the character in the current scope is immortal
- Added an "is_incapable" trigger, which checks if the character in the current scope has an incapacitating trait
- Added an "is_pilgrim" trigger, which checks if the character in the current scope has a pilgrim trait
- Added a "same_regnal_name" trigger, which checks if the character in the current scope has the same regnal name as another character, meaning that they'll be considered the same name for the purpose of regnal numbering. Regnal names are currently defined as having the same first first name (E.G., "Gustav" and "Gustav Adolf" are the same regnal name), or the same cultural first first name (E.G., "Alfr_Alf" and "Alf_Alf" are the same regnal name)
- "monthly_income" and "yearly_income" can now be used in a holding scope. Before it could only take a character. Example: "b_constantinople = { yearly_income = 15 }"
- Added a "has_inheritance_blocker" trigger, which checks if the character in the current scope has a trait that blocks inheritance (cannot_inherit = yes)
- Added a "dynastic_prestige" trigger, which checks if the dynasty of the character in the current scope has a prestige of at least the given value. Example: "dynastic_prestige = 100"
- Added a "set_preferred_capital" effect, which sets the preferred capital of the title in the current scope (used for a variety of things, such as the AI to determine where to put their capital). Only works for dynamic titles, as static titles base their capital off of the landed_titles folder
- Added a "lacks_dlc" event pre-trigger. If the DLC is enabled, the event will not be evaluated
- Added a "has_dlc" event pre-trigger. If the DLC is disabled, the event will not be evaluated
- Added a "war" event pre-trigger, which takes "yes" or "no", and checks that the character is/isn't at war
- Added a "is_married" event pre-trigger, which takes "yes" or "no", and checks that the character is/isn't married
- Added a "friends" event pre-trigger, which takes "yes" or "no", and checks that the character does/doesn't have friends
- Added a "rivals" event pre-trigger, which takes "yes" or "no", and checks that the character does/doesn't have rivals
- Added a "has_global_flag" event pre-trigger, which checks that the given global flag has been set
- Added support for an after = { } - effect field in events. It works as a counterpart to immediate and executes after any option is executed.
- Replaced the COALITION_PROVINCE_THREAT_RATIO define with COALITION_SCARY_TROOP_STRENGTH_THREAT_RATIO. It is now actually used by the game
- Decisions now take the pre-triggers "only_rulers", "only_landed", and "only_independent". It is recommended that they be used when possible so as to reduce the time spent by the AI evaluating the decision. Note that the pre-trigger currently only applies to the AI, not to the player
- The "is_occupied" trigger now works in province scope, not just title scope
- The "is_occupied" trigger now works for county-level titles, not just baron-level titles
- "plot_decisions" are now only checked by characters that lead a plot or faction. Previously there was no functional difference between plot decisions and regular decisions
- Added "has_flag" as a quick trigger for province events. It works exactly like the "has_character_flag" quicktrigger, except the scope is the province for the event
- Added on_war_ended_invalid and on_crusade_invalid on_actions, that provide the same scopes as the other on_war_ended and on_crusade on_actions
- Added a CROWN_LAW_CHANGE_TIMER define. If set to 0, rulers will be restricted to MAX_CROWN_LAW_CHANGES. If set to 1, they'll have a CROWN_LAW_CHANGE_MONTHS cooldown
- Added a CROWN_LAW_CHANGE_MONTHS define
- Traits can now be hidden. Adding "hidden = yes" will make it invisible in all parts of the UI (except event options, where they can be hidden using hidden_tooltip)
- Added define "FAR_CRUSADES_WITHOUT_WEIGHT_MODIFIER" as a weight modifier to starting crusades on titles that are not adjacent to territory of our religion and don't have a crusade weight modifier. Default value is 75%

One of the biggest is likely the addition of game rules, as described in an earlier dev diary.
With game rules, you can add new settings that your users can use to configure your mod. No longer will you need day 1 events or decisions to handle that in a relatively unintuitive and inflexible way, but you can instead have the user configure it in the same way they configure vanilla rules.
Any number of game rules can be added, with any number of possible values. Want to give the option to have a Shattered World scenario for example? Put it in the game rules, and have a hidden startup event do the shattering based on the rule, rather than having the user fire a day 1 decision.
Game rules can be referenced anywhere in script, so they can be used for virtually any purpose where user configuration is relevant.

Finally, I’ve also written a post about things modders can do to make their mods faster. You can read it in the modding sub-forum. I’ve kept it separate so that modders can have a place to ask their questions, and get answers from an inside perspective, separately from this dev diary:
https://forum.paradoxplaza.com/forum/index.php?threads/official-mod-optimization-tips.962056/

That’s all for today. The Reaper’s Due and 2.6 will both be out on the 25th of August, so it won’t be long before you can experience all of these performance and modding improvements for yourself. I hope you’ll all enjoy the improvements made as much as I’ve enjoyed coding them!
 
  • 248
  • 80
  • 2
Reactions:
Optimization is always welcome.

Oh, and a very well written diary!
 
  • 42
Reactions:
Will CK2 utilize more than 2 CPU cores now? I mean that will quad/hexa/octacore CPU owners get any benefits from optimization?
CK2 has pretty much always utilized all cores, the thing is that it switches back and forth between multi-threaded and single-threaded (generally several times a second), so it averages out to 2 to 2.5 cores
The average is likely slightly higher now, since much of what we optimized was single-threaded, and some was moved from single-threaded to multi-threaded.

Most of the machines we've tested on have had 4 physical cores, and 8 logical cores.
I've also seen much the same speed increase on my laptop, which has 2/4 rather than 4/8.

Congratulations on finishing you internship and thank you for a fascinating DD! It's fun to read about the technical stuff behind the game from time to time, and you explain it very clearly and precisely. Good luck with your career!
Thanks! I'll be heading back to Norway and university on Monday.
 
  • 48
  • 7
Reactions:
That optimization effort! Finally! I really like this newly created team, with new people. They are really putting the effort to make a name themselves in Paradox. Hope on the 25th we won't mistaken.
 
  • 14
Reactions:
CK2 has pretty much always utilized all cores, the thing is that it switches back and forth between multi-threaded and single-threaded (generally several times a second), so it averages out to 2 to 2.5 cores.
The average is likely slightly higher now, since much of what we optimized was single-threaded, and some was moved from single-threaded to multi-threaded.

Most of the machines we've tested on have had 4 physical cores, and 8 logical cores.
I've also seen much the same speed increase on my laptop, which has 2/4 rather than 4/8.

Thanks a lot for the reply and an awesome DD! As a laptop user with a pretty mediocre AMD CPU I was suffering from performance drops badly. This DD has given me even more reasons to wait for "Reaper's Due" and launch a new MP campaign with my friends!
 
  • 6
Reactions:
Almost everything that happened at the end of the year is now distributed across the year instead, so as to avoid the noticeable pause

Sounds good but it makes me wonder what we were running at the end of the year and how it will affect game play.

and the game more aggressively eliminates unnecessary characters

Through what methods, and by what criteria is a character defined as unnecessary?

And that is a question born from contention and annoyance at stealth game play mechanics that kill characters.

Will this be similar to the negative fertility modifier that is allegedly imposed for courts of over 30 people? (though I realize that you personally might not have worked on that)
I hate that mechanic, right because the court is to busy it gets harder to find a quiet place to make love, bullocks you guys finally realized every second son or daughter unmarried was a sucker who you could collect at your court through invitation, and you couldn't have me keeping people on shelves.

But yea plague aside are we talking lowborn people, or is there going to be some stealth script causing the prince I just invited to trip down the stairs?
 
  • 5
  • 3
  • 1
Reactions:
Well, I expected something different - like news about content pack, but this is also great.. :D
-----
I have strong pc, so optimization is not my problem. But my friend has a really slow pc and he can't play CK2 on higher speed, then the 2nd one. :( So this update could really help him and also my patience, when we play multiplayer.
 
Last edited:
  • 20
  • 4
Reactions:
Great job Paradox ! It's very nice to see the effects of your hard work about optimization.

My old PC send you its grateful regards :D
 
  • 3
Reactions:
Gotta love that depopulation in the middle.
Take a wild guess at what event that was.

Sounds good but it makes me wonder what we were running at the end of the year and how it will affect game play.
The biggest thing was that AI independent rulers were updating their list of threatening characters. Spreading it out over the year instead should have little to no gameplay effect, but avoids the annoying pause it caused.

Through what methods, and by what criteria is a character defined as unnecessary?

And that is a question born from contention and annoyance at stealth game play mechanics that kill characters.

Will this be similar to the negative fertility modifier that is allegedly imposed for courts of over 30 people? (though I realize that you personally might not have worked on that)
I hate that mechanic, right because the court is to busy it gets harder to find a quiet place to make love, bullocks you guys finally realized every second son or daughter unmarried was a sucker who you could collect at your court through invitation, and you couldn't have me keeping people on shelves.

But yea plague aside are we talking lowborn people, or is there going to be some stealth script causing the prince I just invited to trip down the stairs?
Unnecessary characters are simply killed off.
Characters are considered unnecessary if none of the following are true:
  • They're a ruler
  • They're imprisoned
  • They've got parents
  • They've got a spouse
  • They've got children
  • They've got a council position
  • They've got a claim
  • They're under age 40
How large a court has to be for it to be checked for such characters can be modded in defines.lua. It is set to 10 in 2.6.

At what size the fertility modifier can also be modded.
What it does is simply set the max number of children someone can have 1 lower. It is only applied if the court is larger than the limit (30), and the character is unlanded and has no landed parents. Essentially this means they're limited to a single child.
 
  • 37
  • 5
Reactions:
Interesting stuff, particularly about the optimization. That video was a nice way to visualize the changes.

Big tip to anyone who wants to improve the running of their game; keep your dynasty small. You don't have to marry off every single second cousin, twice removed.
 
  • 3
  • 3
Reactions:
Uncompressed saves are now smaller than they used to be, saving compression and write times
What's been removed from the savefiles?

(It's too bad that prestige and piety are no longer saved for dead characters. There was a nice utility called Gloria Mundi that would sum up all the score for every member of every dynasty--but it no longer works.)
 
  • 4
Reactions:
Fantastic, I have a nice big rig but yep, late game really slows down. Great to see you working for Paradox too, really looking forward to the patch.
 
  • 2
Reactions: