﻿#On actions about titles

# All on-actions in this file provide scope:transfer_type unless otherwise noted
# scope:transfer_type has the following permutations:
# flag:conquest
# flag:conquest_holy_war
# flag:conquest_claim
# flag:conquest_populist
# flag:inheritance
# flag:abdication
# flag:destroyed
# flag:created
# flag:usurped
# flag:granted
# flag:revoked
# flag:election
# flag:independency
# flag:returned
# flag:leased_out
# flag:lease_revoked
# flag:faction_demand
# flag:swear_fealty
# flag:stepped_down

# A title is destroyed
# root = the holder before destruction
# scope:landed_title = the title that is being destroyed
# Does *not* provide scope:transfer_type
on_title_destroyed = {
	effect = {
		# If the title of an independent admin ruler is destroyed, let's make sure we handle any stray admin vassals
		if = {
			limit = {
				government_allows = administrative
				top_liege = this
				administrative_counts_can_be_independent = no
				NOT = {
					any_held_title = {
						title_tier >= kingdom

						this != scope:landed_title
						is_noble_family_title = no
					}
				}
			}
			admin_change_government_effect = yes
		}
		# Adjust the size of the laamps tally.
		else_if = {
			limit = {
				# Pre-filter out anything higher or lower.
				scope:landed_title = { tier = tier_duchy }
				has_global_variable_list = laamps_tally
			}
			# We don't need to check the list twice, since if they're not in it then removing them from it won't do anything.
			remove_list_global_variable = {
				name = laamps_tally
				target = scope:landed_title
			}
		}
		#Notify folks the latin empire has fallen
		else_if = {
			limit = { scope:landed_title = title:e_latin_empire }
			frankokratia_latin_emp_end_msg_effect = yes
		}
		#Create byz collapse variable that defines anarchy period
		else_if = {
			limit = {
				scope:landed_title = {
					this = title:e_byzantium
				}
			}
			if = {
				limit = {
					exists = global_var:byz_collapse_counter
				}
				remove_global_variable = byz_collapse_counter
			}
			set_global_variable = {
				name = byz_collapse_counter
				value = {
					value = current_year
					add = 100
				}
			}
			# Silk Road prosperity gets hit if Byzantium dies
			tgp_silk_road_byzantium_destroyed_effect = yes
		}
		else_if = {
			limit = {
				scope:landed_title = title:h_china
			}
			situation:dynastic_cycle ?= {
				if = {
					limit = {
						situation_top_has_catalyst = catalyst_hegemon_lost_mandate_of_heaven
					}
					trigger_situation_catalyst = {
						catalyst = catalyst_hegemon_lost_mandate_of_heaven
						character = root
					}
				}
			}
			every_vassal = {
				# Because a change in liege isn't triggered when the title being destroyed
				if = {
					limit = {
						government_has_flag = government_is_celestial
						highest_held_title_tier >= tier_kingdom
						primary_title = {
							has_custom_title_name = no
						}
						tgp_capital_not_in_chinese_naming_region = no
					}
					if = {
						limit = {
							is_ai = no
						}
						trigger_event = {
							id = tgp_dynastic_cycle.9000
							days = 1 # so the player gets to choose first
						}
					}
					else = {
						trigger_event = {
							id = tgp_dynastic_cycle.9000
							days = 2
						}
					}
				}
			}
			title:h_china = {
				reset_title_name = yes
			}
		}
		else_if = { # Danelaw-England partition calc.
			limit = {
				# Partition must be active.
				exists = global_var:partition_active_danelaw
				# And the destroyed title must be one of the two partitioned entities.
				OR = {
					scope:landed_title = title:k_england
					scope:landed_title = title:k_danelaw
				}
			}
			# If k_england has been destroyed, set k_danelaw as the de jure inheritor.
			if = {
				limit = { scope:landed_title = title:k_england }
				title:k_danelaw = { save_scope_as = partition_winner }
				# Try to grab the old holder for down the line.
				if = {
					limit = {
						exists = this
						is_alive = yes
					}
					save_scope_as = loser
				}
			}
			# If k_danelaw has been destroyed, set k_england as the de jure inheritor.
			if = {
				limit = { scope:landed_title = title:k_danelaw }
				title:k_england = { save_scope_as = partition_winner }
				if = {
					limit = {
						exists = this
						is_alive = yes
					}
					save_scope_as = loser
				}
			}
			# Either way, trigger the maintenance event.
			trigger_event = british_isles.1041
		}
		# Safety against invalid house bloc members
		else_if = {
			limit = {
				scope:landed_title = { is_noble_family_title = yes }
				exists = house.house_confederation
			}
			house = {
				tgp_leave_house_bloc_effect = {
					OPINION = flag:no
					TRUCE = flag:no
				}
			}
		}
	}
}

# A title is transferred to a new character
# root = the new holder
# scope:title = the title that changes hands
# scope:previous_holder = previous holder. Might be dead
on_title_gain = {
	effect = {
		######################
		# COUNTY-TIER TITLES #
		######################
		
		if = {
			limit = {
				OR = {
					government_has_flag = government_is_prepublic
					government_has_flag = government_is_urepublic
				}
				scope:title = {
					is_nomad_title = no
					title_tier = county
					scope:title.title_province = {
						NOR = {
							has_holding_type = monotown_holding
							has_holding_type = city_holding
						}
					}
				}
			}
			scope:title = {
				limit = {
					is_nomad_title = no
					title_tier = county
					scope:title.title_province = {
						NOR = {
							has_holding_type = monotown_holding
							has_holding_type = city_holding
						}
					}
				}
				scope:title.title_province = {
					set_holding_type = city_holding
				}
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_prepublic
				NOT = {
					exists = domicile
				}
			}
			save_scope_as = scoped_farmer
			create_nomad_title = {
				name = domicile_farmstead
				holder = scope:scoped_farmer
				government = peasantrepublic_government
				save_scope_as = new_title
			}
			scope:new_title ?= {
				add_title_law = noble_family_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_prepublic
				any_held_title = {
					title_tier > barony
					is_nomad_title = no
					NOT = {
						has_title_law = saxon_elective_succession_law
					}
				}
			}
			every_held_title = {
				limit = {
					title_tier > barony
					is_nomad_title = no
				}
				add_title_law = saxon_elective_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_urepublic
				NOT = {
					exists = domicile
				}
			}
			save_scope_as = scoped_merchant
			create_nomad_title = {
				name = domicile_palace
				holder = scope:scoped_merchant
				government = urbanrepublic_government
				save_scope_as = new_title
			}
			scope:new_title ?= {
				add_title_law = noble_family_succession_law
			}
		}
		
		if = {
			limit = {
				government_has_flag = government_is_urepublic
				any_held_title = {
					title_tier > barony
					is_nomad_title = no
					NOT = {
						has_title_law = urban_elective_succession_law
					}
				}
			}
			every_held_title = {
				limit = {
					title_tier > barony
					is_nomad_title = no
				}
				add_title_law = urban_elective_succession_law
			}
		}
		if = {
			limit = {
				scope:title.tier = tier_county
			}

			###########
			# MPO - Nomad Title Handling
			###########
			if = {
				limit = {
					NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
					scope:title = {
						is_nomad_title = yes
						exists = title_domicile
					}
				}
				scope:title = {
					if = {
						limit = {
							root = {
								NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
								any_held_title = {
									is_nomad_title = yes
									exists = title_domicile
									NOT = { this = scope:title }
								}
							}
						}
						if = {
							limit = {
								root = {
									NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
									any_held_title = {
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
										exists = var:player_domicile_title
									}
								}
							}
							root = {
								random_held_title = {
									limit = {
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
										exists = var:player_domicile_title
									}
									title_domicile = {
										if = {
											limit = {
												exists = root.dynasty
												scope:previous_holder.dynasty ?= root.dynasty
											}
											change_herd = {
												value = scope:title.title_domicile.herd
											}
										}
									}
									root = {
										destroy_title = scope:title
									}
								}
							}
						}
						else_if = {
							limit = {
								NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
								exists = var:player_domicile_title
							}
							root = {
								random_held_title = {
									limit = {
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
									}
									save_scope_as = old_nomad_title_to_clean_up
								}
							}
							title_domicile = {
								if = {
									limit = {
										NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
										exists = root.dynasty
										scope:previous_holder.dynasty ?= root.dynasty
									}
									change_herd = {
										value = scope:old_nomad_title_to_clean_up.title_domicile.herd
									}
								}
							}
							root = {
								destroy_title = scope:old_nomad_title_to_clean_up
							}
						}
						else_if = {
							limit = {
								root = {
									any_held_title = {
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
										title_domicile.herd > scope:title.title_domicile.herd
									}
								}
							}
							root = {
								random_held_title = {
									limit = {
										NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
										title_domicile.herd > scope:title.title_domicile.herd
									}
									title_domicile = {
										if = {
											limit = {
												exists = root.dynasty
												scope:previous_holder.dynasty ?= root.dynasty
											}
											change_herd = {
												value = scope:title.title_domicile.herd
											}
										}
									}
								}
								destroy_title = scope:title
							}
						}
						else = {
							root = {
								random_held_title = {
									limit = {
										root = {
											NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
										}
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
									}
									save_scope_as = old_nomad_title_to_clean_up
								}
							}
							title_domicile = {
								if = {
									limit = {
										NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
										exists = root.dynasty
										scope:previous_holder.dynasty ?= root.dynasty
									}
									change_herd = {
										value = scope:old_nomad_title_to_clean_up.title_domicile.herd
									}
								}
							}
							root = {
								destroy_title = scope:old_nomad_title_to_clean_up
							}
						}
					}
					if = {
						limit = {
							exists = global_var:game_has_started
							NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
							NOT = {
								exists = previous_holder
							}
						}
						if = {
							limit = {
								root = {
								NOR = { liege = { government_has_flag = government_allows_nomad_domicile_titles } government_has_flag = government_allows_nomad_domicile_titles }
									OR = {
										house.house_head ?= {
											this != root
											any_held_title = {
												is_nomad_title = yes
											}
										}
										dynasty.dynast ?= {
											this != root
											any_held_title = {
												is_nomad_title = yes
											}
										}
										any_close_family_member = {
											this != root
											any_held_title = {
												is_nomad_title = yes
											}
										}
									}
								}
							}
							generate_coa = yes # To lessen confusion thinking titles are the same
						}
						if = {
							limit = {
								current_year >= 1300
							}
							title_domicile = {
								add_domicile_building = yurt_main_02
								add_domicile_building = yurt_main_03
								add_domicile_building = yurt_main_04
								add_random_yurt_external_building_effect = yes
								add_random_yurt_external_building_effect = yes
								add_random_yurt_external_building_effect = yes
								add_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
							}
						}
						else_if = {
							limit = {
								current_year >= 1200
							}
							title_domicile = {
								add_domicile_building = yurt_main_02
								add_domicile_building = yurt_main_03
								add_domicile_building = yurt_main_04
								add_random_yurt_external_building_effect = yes
								add_random_yurt_external_building_effect = yes
								add_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
							}
						}
						else_if = {
							limit = {
								current_year >= 1100
							}
							title_domicile = {
								add_domicile_building = yurt_main_02
								if = {
									limit = {
										has_domicile_building = yurt_main_02
										owner ?= {
											OR = {
												has_realm_law = nomadic_authority_2
												has_realm_law = nomadic_authority_3
												has_realm_law = nomadic_authority_4
												has_realm_law = nomadic_authority_5
											}
										}
									}
									add_domicile_building = yurt_main_03
								}
								add_random_yurt_external_building_effect = yes
								add_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
							}
						}
						else_if = {
							limit = {
								current_year >= 900
							}
							title_domicile = {
								add_domicile_building = yurt_main_02
								add_random_yurt_external_building_effect = yes
								upgrade_random_yurt_external_building_effect = yes
							}
						}
					}
				}

				###########
				# MPO - Edge case Nomadic Philosophy handover
				###########
				if = {
					limit = {
						government_has_flag = government_is_nomadic
						NOT = {
							has_trait = nomadic_philosophy
						}
						exists = situation:the_great_steppe
						any_character_situation = {
							this = situation:the_great_steppe
						}
					}
					add_trait = nomadic_philosophy
					every_heir = {
						limit = {
							top_liege ?= root
							NOT = {
								has_trait = nomadic_philosophy
							}
						}
						add_trait = nomadic_philosophy
					}
				}
			}
			###########
			# Nomads switch faith/culture of conquered nomadic counties
			###########
			else_if = {
				limit = {
					scope:title.title_province ?= {
						OR = {
							has_holding_type = nomad_holding
							has_holding_type = herder_holding
						}
					}
					government_has_flag = government_is_nomadic
					exists = domicile
				}
				if = {
					limit = {
						has_character_flag = domicile_culture_and_faith_check
						OR = {
							domicile.domicile_culture != scope:title.culture
							domicile.domicile_faith != scope:title.faith
						}
					}
					domicile = {
						set_domicile_culture = scope:title.culture
						set_domicile_faith = scope:title.faith
					}
					remove_character_flag = domicile_culture_and_faith_check
				}
				else = {
					if = {
						limit = { domicile.domicile_culture != scope:title.culture }
						scope:title ?= {
							set_variable = {
								name = migration_previous_culture
								value = culture
							}
							set_county_culture = root.domicile.domicile_culture
						}
					}
					if = {
						limit = { domicile.domicile_faith != scope:title.faith }
						scope:title = { set_county_faith = root.domicile.domicile_faith }
					}
				}
			}
			else = {
				###########
				# Apply Law of the Land title succession law if appropriate
				###########
				scope:title = {
					add_law_of_the_land_title_succession_law_if_appropriate_effect = yes
				}
			}

			###########
			# MPO - Siberian permafrost modifier switcharoo
			###########
			if = {
				limit = {
					scope:title.title_province = {
						geographical_region = geographical_region:mpo_region_permafrost
					}
				}
				if = {
					limit = {
						scope:title = {
							has_county_modifier = mpo_siberian_permafrost_modifier_bad
						}
						culture ?= { has_cultural_parameter = permafrost_modifier_mechanic }
					}
					scope:title = {
						remove_county_modifier = mpo_siberian_permafrost_modifier_bad
						add_county_modifier = mpo_siberian_permafrost_modifier
					}
				}
				else_if = {
					limit = {
						scope:title = {
							has_county_modifier = mpo_siberian_permafrost_modifier
						}
						NOR = {
							culture ?= { has_cultural_parameter = permafrost_modifier_mechanic }
						}
					}
					scope:title = {
						remove_county_modifier = mpo_siberian_permafrost_modifier
						add_county_modifier = mpo_siberian_permafrost_modifier_bad
					}
				}
			}
			else_if = {
				limit = {
					OR = {
						scope:title = title:c_antiocheia
						scope:title = title:c_jerusalem
						scope:title = title:c_alexandria
						scope:title = title:c_roma
					}
				}
				if = {
					limit = {
						OR = {
							AND = {
								is_roman_emperor_trigger = yes
								faith = faith:orthodox
								faith = { has_doctrine = special_doctrine_ecumenical_christian }
							}
							any_liege_or_above = {
								is_roman_emperor_trigger = yes
								faith = faith:orthodox
								faith = { has_doctrine = special_doctrine_ecumenical_christian }
							}
						}
					}
					trigger_event = roman_restoration.0199
				}
				###########
				# Transfer the Samaritan HoF to the realm that holds Jerusalem
				###########
				if = {
					limit = {
						scope:title = title:c_jerusalem
						title:d_samaritan.holder ?= {
							# Samaritan HoF is not independent
							# Samaritan liege is not Samaritan
							top_liege.faith != faith:samaritan
							top_liege != root
						}
						# root is at least a king, or their liege is, or their liege's liege is
						OR = {
							highest_held_title_tier >= tier_kingdom
							any_liege_or_above = {
								highest_held_title_tier >= tier_kingdom
							}
						}
					}
					create_title_and_vassal_change = {
						type = created
						save_scope_as = title_change
						add_claim_on_loss = no
					}
					if = {
						limit = {
							highest_held_title_tier >= tier_kingdom
						}
						title:d_samaritan.holder = {
							change_liege = {
								liege = root
								change = scope:title_change
							}
						}
					}
					else = {
						title:d_samaritan.holder = {
							change_liege = {
								liege = root.top_liege
								change = scope:title_change
							}
						}
					}
					resolve_title_and_vassal_change = scope:title_change
				}
			}

			###########
			# FP1 - New holder for a county with a stele.
			###########
			if = {
				limit = {
					# Using a scripted effect here so we can look at the same trigger in the grant title interaction
					fp1_remove_stele_new_holder_trigger = {
						TITLE = scope:title
						PREVIOUS_HOLDER = scope:previous_holder
						NEW_HOLDER = root
					}
					has_fp1_dlc_trigger = yes
				}
				scope:title = {
					remove_variable = stele_scope_type
					remove_variable = stele_container
					remove_variable = stele_carver
					remove_variable = stele_commissioner
					remove_variable = stele_slot_occupied
					remove_county_modifier = fp1_stele_ancestors_strong
					remove_county_modifier = fp1_stele_ancestors_weak
					remove_county_modifier = fp1_stele_conquest_strong
					remove_county_modifier = fp1_stele_conquest_weak
					remove_county_modifier = fp1_stele_promotion_strong
					remove_county_modifier = fp1_stele_promotion_weak
				}
			}
			###########
			# Non-FP1 - New holder of the county with a runestone
			###########
			else_if = {
				limit = {
					scope:title = {
						exists = var:ancestor_to_bury
					}
					dynasty != scope:previous_holder.dynasty #We only have to change something if the new holder is of a different dynasty
					scope:title = {
						NOT = { var:ancestor_to_bury.dynasty = root.dynasty }
					}
				}
				scope:title = {
					remove_county_modifier = county_raised_runestone_modifier
					remove_variable = ancestor_to_bury
				}
			}

			###########
			# EP3 ADVENTURERS BECOMING LANDED
			###########
			if = {
				limit = {
					scope:title = { is_landless_type_title = no }
					any_held_title = {
						title_tier = duchy
						has_variable = adventurer_creation_reason
					}
				}
				save_scope_as = adventurer
				if = {
					limit = {
						NOR = {
							has_trait = adventurer
							government_has_flag = government_is_nomadic
						}
					}
					add_trait = adventurer
				}
				every_courtier = {
					set_variable = {
						name = former_camp_leader
						value = scope:adventurer
					}
					add_trait = adventurer_follower
				}
				if = {
					limit = {
						house = {
							has_house_modifier = legacy_adventurer_house_modifier
						}
					}
					add_legitimacy = massive_legitimacy_gain
					set_variable = {
						name = legacy_adventurers_to_levies
						value = 0
					}
					every_courtier = {
						root = {
							change_variable = { name = legacy_adventurers_to_levies add = 25 }
						}
					}
					every_held_title = {
						limit = { has_variable = adventurer_creation_reason }
						save_scope_as = prev_title
					}
					spawn_army = {
						levies = var:legacy_adventurers_to_levies
						location = capital_province
						name = remnants_adventurer_army
					}
					remove_variable = legacy_adventurers_to_levies
				}
				destroy_laamp_effect = { ADVENTURER = scope:adventurer }
				# EP3 - Ensure NF holders are Admin
				if = {
					limit = {
						scope:title = { is_noble_family_title = yes }
						NOT = { government_has_flag = government_is_administrative }
					}
					change_government = administrative_government
				}
			}
		}
		#####################
		# DUCHY-TIER TITLES #
		#####################
		else_if = {
			limit = {
				scope:title.tier = tier_duchy
			}

			# Special Legitimacy gain for being elected (This is present in two places in this on_action)
			if = {
				limit = {
					scope:title = {
						has_order_of_succession = election
					}
					has_legitimacy = yes
				}
				save_scope_value_as = {
					name = title_tier_minus_2
					value = {
						value = scope:title.tier
						subtract = 2
					}
				}
				send_interface_toast = {
					type = msg_legitimacy_gain
					title = legitimacy_gain_toast
					left_icon = root
					right_icon = scope:title
					add_legitimacy = {
						value = {
							value = major_legitimacy_gain
							multiply = scope:title_tier_minus_2
						}
					}
				}
			}

			# Transfer title-bound artifacts with the title it's associated with (This is present in two places in this on_action)
			if = {
				limit = {
					scope:previous_holder ?= {
						any_character_artifact = {
							has_variable = artifact_succession_title #Is this an artifact that should follow a title?
							var:artifact_succession_title = { is_title_created = yes } #Does the title the artifact should follow exist?
							var:artifact_succession_title = scope:title #Scope title is the artifact title
						}
					}
				}
				scope:previous_holder = {
					every_character_artifact = {
						limit = {
							has_variable = artifact_succession_title #Is this an artifact that should follow a title?
							var:artifact_succession_title = { is_title_created = yes } #Does the title the artifact should follow exist?
							var:artifact_succession_title = scope:title #Scope title is the artifact title
						}
						if = {
							limit = {
								OR = {
									scope:transfer_type = flag:conquest
									scope:transfer_type = flag:conquest_holy_war
									scope:transfer_type = flag:conquest_claim
									scope:transfer_type = flag:conquest_populist
									scope:transfer_type = flag:abdication
									scope:transfer_type = flag:usurped
									scope:transfer_type = flag:revoked
									scope:transfer_type = flag:faction_demand
								}
							}
							set_owner = {
								target = root
								history = {
									type = conquest
									actor = scope:previous_holder
									recipient = root
									location = scope:previous_holder.location
								}
							}
						}
						else = {
							set_owner = {
								target = root
								history = {
									type = inherited
									recipient = root
								}
							}
						}
					}
				}
			}

			if = {
				limit = {
					government_has_flag = government_is_landless_adventurer
				}
				# EP3 ADVENTURERS TELEPORT TO THEIR CAMP
				trigger_event = {
					id = title_event.9911
					days = 1
				}
			}

			# EP3 ADVENTURER TITLE BEING INHERITED BY A LANDED RULER
			if = {
				limit = {
					scope:title = {
						has_variable = adventurer_creation_reason
					}
					any_held_title = {
						NOT = { has_variable = adventurer_creation_reason }
					}
				}
				every_held_title = {
					limit = { has_variable = adventurer_creation_reason }
					save_scope_as = landless_adventurer_title
					root = { destroy_title = scope:landless_adventurer_title }
				}
			}
			# Have we been given a fair shake as a laamp, but we're sucking?
			else_if = {
				limit = {
					has_government = landless_adventurer_government
					is_ai = yes
					scope:previous_holder = { is_ai = yes }
					primary_title = {
						any_past_holder = { count >= 4 }
					}
					NOR = {
						# If the heir is sufficiently impressive, then we'll keep 'em around.
						prestige_level >= 2
						# Legitimists are always worthwhile.
						has_realm_law = camp_purpose_legitimists
						# Reserve successful mercenary outfits.
						AND = {
							max_military_strength >= 1500
							# Allow absolute decimations to destroy mercs from time to time.
							current_military_strength <= 150
						}
						# Or those currently fighting alongside/against players.
						AND = {
							is_at_war = yes
							OR = {
								any_war_ally = { is_ai = no }
								any_war_enemy = { is_ai = no }
							}
						}
						# Depending on camp purpose, we want various skills.
						AND = {
							has_realm_law = camp_purpose_wanderers
							OR = {
								highest_skill_value >= very_high_skill_rating
								prowess >= very_high_skill_rating
							}
						}
						AND = {
							has_realm_law = camp_purpose_mercenaries
							OR = {
								martial >= high_skill_rating
								prowess >= high_skill_rating
							}
						}
						AND = {
							has_realm_law = camp_purpose_scholars
							OR = {
								learning >= high_skill_rating
								diplomacy >= high_skill_rating
							}
						}
						AND = {
							has_realm_law = camp_purpose_explorers
							OR = {
								diplomacy >= high_skill_rating
								stewardship >= high_skill_rating
							}
						}
						AND = {
							has_realm_law = camp_purpose_brigands
							OR = {
								intrigue >= high_skill_rating
								prowess >= high_skill_rating
							}
						}
						# Finally, keep anyone who's important to the player.
						any_player = {
							OR = {
								root.dynasty ?= dynasty
								is_consort_of = root
								has_important_relationship_with_character_trigger = { CHARACTER = root }
								any_pinned_character = { this = root }
								any_pinned_character = { this = scope:previous_holder }
							}
						}
					}
				}
				destroy_laamp_effect = { ADVENTURER = root }
			}
			# Holy Order Trait
			else_if = {
				limit = {
					scope:title = { is_holy_order = yes }
					NOT = { has_trait = order_member }
					government_has_flag = government_is_holy_order
				}
				add_trait = order_member
			}

			# Apply Created Title opinion to Courtly Vassals (This is present in two places in this on_action)
			if = {
				limit = {
					scope:transfer_type = flag:created
					is_alive = yes
				}
				every_vassal = {
					vassal_stance = courtly
					if = {
						limit = {
							scope:title.tier = tier_duchy
						}
						add_opinion = {
							target = root
							modifier = courtly_title_creation_opinion
							opinion = 10
						}
					}
					else = {
						add_opinion = {
							target = root
							modifier = courtly_title_creation_opinion
							opinion = 30
						}
					}
				}
			}
			# Lil Johnny Lackland changes his nickname.
			if = {
				limit = {
					has_nickname = nick_lackland_young
				}
				give_nickname = nick_lackland_ironic
			}
		}
		#####################
		# KING+ TIER TITLES #
		#####################
		else = {
			# Newly formed Hegemon of China should spruce up their council
			if = {
				limit = {
					scope:title = title:h_china
					exists = root.dynasty
					exists = scope:previous_holder.house
					scope:previous_holder.house != root.house
				}
				save_scope_as = councillor_liege
				fill_the_ministry_effect = yes
			}
			# Ceremonial Liege invalidation
			if = {
				limit = {
					exists = this
					any_held_title = { exists = var:administrative_ui_special_title }
				}
				every_held_title = {
					limit = { exists = var:administrative_ui_special_title }
					var:administrative_ui_special_title.holder = { tgp_destroy_ceremonial_throne_effect = yes }
				}
				if = {
					limit = { has_trait = former_emperor }
					remove_trait = former_emperor
				}
			}
			# Ceremonial Liege
			if = {
				limit = {
					OR = {
						exists = scope:title.var:ceremonial_title
						top_liege.primary_title.var:administrative_ui_special_title ?= scope:title
					}
				}
				if = {
					limit = {
						exists = house
						exists = scope:previous_holder.house
						scope:previous_holder.house != house
					}
					scope:title = { set_coa = root.house }
				}
				remove_character_flag = ceremonial_regent_flag
			}
			if = {
				limit = { exists = scope:title.var:ceremonial_title }
				add_character_flag = ceremonial_liege_flag
				if = {
					limit = { has_trait = devoted }
					remove_trait = devoted
				}
				destroy_laamp_effect = { ADVENTURER = this }
			}
			# Reset raid remit
			if = {
				limit = {
					this = top_liege
					any_noble_family = { }
				}
				every_noble_family = {
					holder = { remove_variable = raid_estate_permission }
				}
			}
			# Special Legitimacy gain for being elected (This is present in two places in this on_action)
			if = {
				limit = {
					scope:title = {
						has_order_of_succession = election
					}
					has_legitimacy = yes
				}
				save_scope_value_as = {
					name = title_tier_minus_2
					value = {
						value = scope:title.tier
						subtract = 2
					}
				}
				send_interface_toast = {
					type = msg_legitimacy_gain
					title = legitimacy_gain_toast
					left_icon = root
					right_icon = scope:title
					add_legitimacy = {
						value = {
							value = major_legitimacy_gain
							multiply = scope:title_tier_minus_2
						}
					}
				}
			}
			# Transfer title-bound artifacts with the title it's associated with (This is present in two places in this on_action)
			if = {
				limit = {
					scope:previous_holder ?= {
						any_character_artifact = {
							has_variable = artifact_succession_title #Is this an artifact that should follow a title?
							var:artifact_succession_title = { is_title_created = yes } #Does the title the artifact should follow exist?
							var:artifact_succession_title = scope:title #Scope title is the artifact title
						}
					}
				}
				scope:previous_holder = {
					every_character_artifact = {
						limit = {
							has_variable = artifact_succession_title #Is this an artifact that should follow a title?
							var:artifact_succession_title = { is_title_created = yes } #Does the title the artifact should follow exist?
							var:artifact_succession_title = scope:title #Scope title is the artifact title
						}
						if = {
							limit = {
								OR = {
									scope:transfer_type = flag:conquest
									scope:transfer_type = flag:conquest_holy_war
									scope:transfer_type = flag:conquest_claim
									scope:transfer_type = flag:conquest_populist
									scope:transfer_type = flag:abdication
									scope:transfer_type = flag:usurped
									scope:transfer_type = flag:revoked
									scope:transfer_type = flag:faction_demand
								}
							}
							set_owner = {
								target = root
								history = {
									type = conquest
									actor = scope:previous_holder
									recipient = root
									location = scope:previous_holder.location
								}
							}
						}
						else = {
							set_owner = {
								target = root
								history = {
									type = inherited
									recipient = root
								}
							}
						}
					}
				}
			}
			# Grandeur in elective realms
			if = {
				limit = {
					exists = var:previous_holder_grandeur_value
					has_royal_court = yes
					has_dlc_feature = royal_court
				}
				hidden_effect = {
					change_current_court_grandeur = -100 # Reset to 0
					change_current_court_grandeur = var:previous_holder_grandeur_value
					remove_variable = previous_holder_grandeur_value
				}
			}
			# Apply Created Title opinion to Courtly Vassals (This is present in two places in this on_action)
			if = {
				limit = {
					scope:transfer_type = flag:created
					is_alive = yes
				}
				every_vassal = {
					vassal_stance = courtly
					if = {
						limit = {
							scope:title.tier = tier_duchy
						}
						add_opinion = {
							target = root
							modifier = courtly_title_creation_opinion
							opinion = 10
						}
					}
					else = {
						add_opinion = {
							target = root
							modifier = courtly_title_creation_opinion
							opinion = 30
						}
					}
				}
			}
			# Empire and Hegemony stuff
			if = {
				limit = {
					scope:title = {
						tier >= tier_empire
					}
				}
				# Byzantine-only: The Patriarch refuses to crown you as new emperor because you have a criminal trait (e.g. kinslayer)
				if = {
					limit = {
						valid_for_byz_emperor_content_trigger = yes
						has_ep3_dlc_trigger = yes
						exists = root.faith.religious_head
						this != faith.religious_head
						faith.religious_head = {
							opinion = {
								target = root
								value <= 0
							}
							location = root.location
						}
						has_any_criminal_trait_in_faith_trigger = {
							CHARACTER = root
							FAITH = root.faith
						}
						NOT = { has_character_flag = has_had_ep3_2020 }
					}
					trigger_event = {
						id = ep3_emperor_yearly.2020
						days = 1
					}
				}
				### EP3 Admin Emperor Flavor ###
				if = {
					limit = {
						# Not for meritocratic
						NOT = { government_has_flag = government_has_merit }
						# Admin only
						government_allows = administrative
						has_ep3_dlc_trigger = yes
						# Independent rulers only
						this = top_liege
						# New title is higher or equal to your primary tier
						highest_held_title_tier >= scope:title.tier
						# You didn't just create the title
						scope:title = {
							any_past_holder = { }
							# Your predecessor wasn't from your same house - not much to celebrate if you got the title from dad!
							previous_holder ?= {
								house != root.house
							}
						}
						# Handled separately
						tgp_realm_has_ceremonial_liege_trigger = no
					}
					trigger_event = {
						id = ep3_emperor_yearly.2050
						days = 5
					}
				}
				#Byzantium is reborn!
				if = {
					limit = {
						scope:title = {
							this = title:e_byzantium
						}
						exists = global_var:byz_collapse_counter
					}
					save_scope_as = byz_emperor
					title:e_byzantium = {
						every_in_de_jure_hierarchy = {
							limit = {
								tier <= tier_kingdom
								tier >= tier_county
								is_title_created = yes
								exists = holder
							}
							if = {
								limit = {
									holder ?= {
										is_alive = yes
										NOR = {
											is_in_list = byz_return_notify
											any_held_title = {
												this = title:e_byzantium
											}
										}
									}
								}
								holder = {
									add_to_list = byz_return_notify
								}
							}
						}
					}
					title:e_latin_empire = {
						every_in_de_jure_hierarchy = {
							limit = {
								tier <= tier_kingdom
								tier >= tier_county
								is_title_created = yes
								exists = holder
							}
							if = {
								limit = {
									holder = {
										is_alive = yes
										NOR = {
											is_in_list = byz_return_notify
											any_held_title = {
												this = title:e_byzantium
											}
										}
									}
								}
								holder = {
									add_to_list = byz_return_notify
								}
							}
						}
					}
					every_in_list = {
						list = byz_return_notify
						trigger_event = ep3_frankokratia_events.0130
					}
					remove_global_variable = byz_collapse_counter
				}
				# TGP CHINA
				else_if = {
					limit = {
						scope:title = title:h_china
					}
					# When conquering China - Change to conquest phase
					if = {
						limit = {
							root.dynasty != scope:previous_holder.dynasty
						}
						if = {
							limit = {
								is_valid_celestial_dynasty = no
							}
							situation:dynastic_cycle = {
								situation_top_sub_region = {
									change_phase = { phase = situation_dynastic_cycle_phase_instability_conquest }
								}
							}
						}
						else_if = {
							limit = {
								situation:dynastic_cycle ?= {
									situation_current_phase = situation_dynastic_cycle_phase_instability_conquest
								}
							}
							situation:dynastic_cycle = {
								situation_top_sub_region = {
									change_phase = { phase = situation_dynastic_cycle_phase_instability }
								}
							}
						}
					}
					# When inheriting China - Move towards instability/chaotic phase
					if = {
						limit = {
							root.house != scope:previous_holder.house
							current_date_is_start_date_trigger = no # We check this to avoid potentially triggering this immediately on game start
						}
						situation:dynastic_cycle = {
							if = {
								limit = {
									situation_top_has_catalyst = catalyst_new_dynasty_inherits
								}
								trigger_situation_catalyst = catalyst_new_dynasty_inherits
							}
						}
					}
					# Clear the Strengthen Capital great project cooldown
					situation:dynastic_cycle ?= {
						if = {
							limit = { has_variable = gp_strengthen_capital_cooldown }
							remove_variable = gp_strengthen_capital_cooldown
						}
					}
					#setup Grand Secretariat
					if = {
						limit = {
							has_dlc_feature = all_under_heaven
							NOT = { has_diarchy_type = grand_secretariat }
						}
						if = {#  Clear out any old diarchy
							limit = { has_active_diarchy = yes }
							end_diarchy = yes
						}
						if = {
							limit = {
								has_realm_law = grand_chancellor_law
								exists = title:h_china.holder.cp:councillor_chancellor
							}
							try_start_diarchy = grand_secretariat
							set_diarch = title:h_china.holder.cp:councillor_chancellor
						}
						else_if = {
							limit = {
								has_realm_law = grand_marshal_law
								exists = title:h_china.holder.cp:minister_grand_marshal
							}
							try_start_diarchy = grand_secretariat
							set_diarch = title:h_china.holder.cp:minister_grand_marshal
						}
						else_if = {
							limit = {
								exists = title:h_china.holder.cp:councillor_spymaster
							}
							try_start_diarchy = grand_secretariat
							set_diarch = title:h_china.holder.cp:councillor_spymaster
						}
					}
					root.house.house_head.domicile ?= {
						set_family_accomplishment_effect = { ACCOMPLISHMENT = family_accomplishment_dynasty }
					}
				}
				else_if = {
					limit = {
						OR = {
							scope:title = title:e_minister_chancellor
							scope:title = title:e_minister_censor
							scope:title = title:e_minister_grand_marshal
							scope:title = title:e_minister_of_personnel
							scope:title = title:e_minister_of_revenue
							scope:title = title:e_minister_of_rites
							scope:title = title:e_minister_of_war
							scope:title = title:e_minister_of_justice
							scope:title = title:e_minister_of_works
						}
					}
					# set up the council holder as dejure liege of the ministry title, as this effect is not called with the same scope as council onactions
					scope:title.de_jure_liege.holder = {
						save_scope_as = councillor_liege
					}
					# If we get a minister title, make sure we set things up correctly
					switch = {
						trigger = scope:title
						title:e_minister_chancellor = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_chancellor MINISTER_POSITION = councillor_chancellor }
						}
						title:e_minister_censor = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_censor MINISTER_POSITION = councillor_spymaster }
						}
						title:e_minister_grand_marshal = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_grand_marshal MINISTER_POSITION = minister_grand_marshal }
						}
						title:e_minister_of_personnel = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_of_personnel MINISTER_POSITION = minister_personnel }
						}
						title:e_minister_of_revenue = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_of_revenue MINISTER_POSITION = councillor_steward }
						}
						title:e_minister_of_rites = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_of_rites MINISTER_POSITION = councillor_court_chaplain }
						}
						title:e_minister_of_war = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_of_war MINISTER_POSITION = councillor_marshal }
						}
						title:e_minister_of_justice = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_of_justice MINISTER_POSITION = minister_justice }
						}
						title:e_minister_of_works = {
							got_minister_position_effect = { MINISTER_TITLE = e_minister_of_works MINISTER_POSITION = minister_works }
						}
					}
				}
				# TGP JAPAN
				else_if = {
					limit = { scope:title = title:e_japan }
					# TGP RETIRED EMPEROR
					if = {
						limit = {
							exists = title:e_japan.var:administrative_ui_special_title
							has_title = title:e_japan.var:administrative_ui_special_title
						}
						# Flavourization setup
						title:e_japan = { remove_variable = shogun_flag }
						remove_character_flag = shogun_flag
						if = { # Flavourization setup
							limit = { has_global_variable = tenno_restored }
							title:e_japan = { set_variable = ceremonial_liege_flag }
							add_character_flag = ceremonial_liege_flag
							trigger_event = {
								id = tgp_japan_general.9100
								days = 1
							}
						}
						else = { # Event to abdicate and become Joko
							trigger_event = {
								id = tgp_japan_general.9120
								days = 1
							}
						}
					}
					# TGP SHOGUN
					else_if = {
						limit = {
							has_government = japan_feudal_government
							has_global_variable = shogunate_established
						}
						title:e_japan = { remove_variable = ceremonial_liege_flag }
						remove_character_flag = ceremonial_liege_flag
						title:e_japan = { set_variable = shogun_flag }
						add_character_flag = shogun_flag
					}
					# TGP KAMPAKU
					else = {
						title:e_japan = { remove_variable = ceremonial_liege_flag }
						remove_character_flag = ceremonial_liege_flag
						title:e_japan = { remove_variable = shogun_flag }
						remove_character_flag = shogun_flag
					}
					# Japanese regent laws
					if = {
						limit = {
							tgp_realm_has_ceremonial_liege_trigger = yes
							government_is_japanese_trigger = yes
							NOR = {
								has_global_variable = shogunate_established
								has_global_variable = tenno_restored
							}
						}
						if = {
							limit = {
								government_has_flag = government_is_japan_administrative
								NOT = { has_realm_law = japanese_regency_succession_law }
							}
							add_realm_law_skip_effects = japanese_regency_succession_law
						}
						else_if = {
							limit = {
								government_has_flag = government_is_japan_feudal
								has_realm_law = japanese_regency_succession_law
							}
							remove_realm_law = japanese_regency_succession_law
						}
					}
				}
				# ROME
				else_if = {
					limit = {
						OR = {
							scope:title = title:h_roman_empire
							scope:title = title:h_eastern_roman_empire 
						}
						NOT = { has_trait = augustus }
					}
					trigger_event = roman_restoration.0005
				}
				#Mongol empire and GoK should lose other empires
				# You just inherited an Emperor title!!
				if = {
					limit = {
						primary_title = scope:title
						exists = scope:previous_holder
						NOT = { #You don't already have an empire tier title
							any_held_title = {
								title_tier = empire
								this != scope:title
							}
						}
						# That isn't a part of the ministry
						NOR = {
							scope:title = title:e_minister_chancellor
							scope:title = title:e_minister_censor
							scope:title = title:e_minister_grand_marshal
							scope:title = title:e_minister_of_personnel
							scope:title = title:e_minister_of_revenue
							scope:title = title:e_minister_of_rites
							scope:title = title:e_minister_of_war
							scope:title = title:e_minister_of_justice
							scope:title = title:e_minister_of_works
						}
					}
					trigger_event = {
						id = realm_maintenance.2001
						days = 1
					}
				}
				# DEPRECATED
				#if = {
				#	limit = {
				#		is_independent_ruler = yes
				#		tgp_realm_has_ceremonial_liege_trigger = no
				#		government_has_flag = government_is_meritocratic
				#	}
				#	trigger_event = {
				#		id = tgp_korea_decision.9203
				#		days = 1
				#	}
				#}
				# TGP CEREMONIAL FLAVOR
				scope:previous_holder ?= {
					if = {
						limit = { is_alive = yes }
						remove_character_flag = ceremonial_liege_flag
						remove_character_flag = ceremonial_regent_flag
					}
				}
				if = {
					limit = { is_alive = yes }
					remove_character_flag = ceremonial_liege_flag
					remove_character_flag = ceremonial_regent_flag
				}
				top_liege.primary_title.var:administrative_ui_special_title.holder ?= {
					remove_character_flag = ceremonial_liege_flag
					remove_character_flag = ceremonial_regent_flag
				}
				if = {
					limit = { tgp_is_ceremonial_regent_trigger = yes } # Not 'actual' ruler
					add_character_flag = ceremonial_regent_flag
					top_liege.primary_title.var:administrative_ui_special_title.holder ?= { add_character_flag = ceremonial_liege_flag }
				}
			}
			# West Francia becomes France when no longer controlled by a Karling
			else_if = {
				limit = {
					scope:title = {
						this = title:k_france
					}
					NOR = {
						dynasty = { this = dynasty:25061 } # Not held by a Karling
						any_liege_or_above = {
							dynasty = { this = dynasty:25061 } # And does not have a liege that is a Karling
						}
						has_global_variable = west_francia_renamed
					}
				}
				trigger_event = {
					id = title_event.0001
					days = 1
				}
			}
			# East Francia becomes Germany when no longer controlled by a Karling
			else_if = {
				limit = {
					scope:title = {
						this = title:k_east_francia
					}
					NOR = {
						dynasty = { this = dynasty:25061 }  # Not held by a Karling
						any_liege_or_above = {
							dynasty = { this = dynasty:25061 } # And does not have a liege that is a Karling
						}
						has_global_variable = east_francia_renamed
					}
				}
				trigger_event = {
					id = title_event.0002
					days = 1
				}
			}
			# Hidden management event to determine if we need to axe Asturias &, if we do, what notification events to send out afterwards.
			else_if = {
				limit = {
					scope:title = title:k_castille
					title:k_asturias = {
						# Leon must be de jure part of Asturias, or else there's no reason to ever stop calling it Asturias.
						any_in_de_jure_hierarchy = { this = title:d_leon }
					}
				}
				trigger_event = {
					id = title_event.0011
					days = 1
				}
			}
			# FP2 El Cid Achievement
			else_if = {
				limit = {
					scope:title = title:k_valencia
					has_character_flag = fp2_el_cid_blood_relation_legacy
				}
				set_global_variable = { # DO. NOT. USE. add_achievement_global_variable_effect. IT BREAKS THE ACHIEVEMENT.
					name = fp2_el_cid_achievement_unlocked
					value = yes
				}
			}
			# TGP CEREMONIAL RULER
			# IMPERIAL INHERITANCE
			else_if = {
				limit = {
					exists = scope:title.var:ceremonial_title
				}
				add_character_flag = ceremonial_liege_flag
				if = {
					limit = { has_trait = devoted }
					remove_trait = devoted
				}
				destroy_laamp_effect = { ADVENTURER = this }
				trigger_event = {
					id = tgp_japan_general.9100
					days = 1
				}
			}
			# Norway stuff
			else_if = {
				limit = {
					# The title they've acquired is Norway.
					scope:title = title:k_norway
				}
				# Tanglehair becomes Fairhair.
				if = {
					limit = {
						# This character is Harald Fairhair.
						exists = character:144000
						this = character:144000
						# Norway has been created by them.
						scope:transfer_type = flag:created
						# Norway has not been created previously.
						NOT = { exists = global_var:norway_created }
					}
					trigger_event = {
						id = fp1_major_decisions.1011
						days = 1
					}
				}
				# Check Norway's creation status.
				if = {
					limit = {
						scope:transfer_type = flag:created
						NOT = { exists = global_var:norway_created }
					}
					trigger_event = {
						id = fp1_major_decisions.1012
						days = 1
					}
				}
			}
			# English Partition Result - Gained Both
			else_if = {
				limit = {
					# Partition must be active.
					exists = global_var:partition_active_danelaw
					# The gained title must be one of the two partitioned entities.
					OR = {
						scope:title = title:k_england
						scope:title = title:k_danelaw
					}
					# And you must now hold both titles.
					this = title:k_england.holder
					this = title:k_danelaw.holder
				}
				trigger_event = {
					id = british_isles.1032
					days = 1
				}
			}
			# Lil Johnny Lackland changes his nickname.
			if = {
				limit = {
					has_nickname = nick_lackland_young
				}
				give_nickname = nick_lackland_ironic
			}
		}

		#############
		# STRUGGLES #
		#############
		if = {
			limit = {
				exists = struggle:persian_struggle
			}
			#Struggle Catalysts
			if = {
				limit = {
					scope:title.empire ?= title:e_persia
					scope:title = {
						tier >= tier_county
						is_landless_type_title = no
					}
					NOR = {
						scope:transfer_type = flag:created
						scope:transfer_type = flag:inheritance
					}
				}
				if = {
					limit = {
						fp3_character_uninvolved_in_struggle_trigger = yes
						scope:previous_holder ?= {
							fp3_character_involved_in_struggle_trigger = yes
							any_character_struggle = {
								phase_has_catalyst = catalyst_interloper_uninvolved_gain_struggle_titles
							}
						}
					}
					scope:previous_holder ?= {
						every_character_struggle = {
							limit = { phase_has_catalyst = catalyst_interloper_uninvolved_gain_struggle_titles }
							activate_struggle_catalyst = {
								catalyst = catalyst_interloper_uninvolved_gain_struggle_titles
								character = root
							}
							log_debug_variable_for_persian_struggle_effect = { VAR = concession_catalyst_interloper_uninvolved_gain_struggle_titles }
						}
					}
				}
				if = {
					limit = {
						fp3_character_involved_in_struggle_trigger = yes
						any_character_struggle = {
							phase_has_catalyst = catalyst_gain_struggle_titles_from_interlopers_uninvolved
							# Stop catalyst from affecting those who are only just now uninvolved due to losing this title
							OR = {
								NOT = { is_culture_involved_in_struggle = scope:previous_holder.culture }
								NOT = { is_faith_involved_in_struggle = scope:previous_holder.faith }
							}
						}
						scope:previous_holder ?= {
							OR = {
								fp3_character_uninvolved_in_struggle_trigger = yes
								fp3_character_interloper_in_struggle_trigger = yes
							}
						}
					}
					every_character_struggle = {
						limit = { phase_has_catalyst = catalyst_gain_struggle_titles_from_interlopers_uninvolved }
						activate_struggle_catalyst = {
							catalyst = catalyst_gain_struggle_titles_from_interlopers_uninvolved
							character = root
						}
						log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_gain_struggle_titles_from_interlopers_uninvolved }
					}
				}
			}
			# FP3 Struggle special cases
			if = {
				limit = {
					scope:title = {
						OR = {
							this = title:d_sunni
							this = title:e_arabia
						}
					}
				}
				## Convert detractors to supporters if they inherit either of the titles they're detracting against.
				if = {
					limit = {
						NOT = { has_trait = fp3_struggle_supporter }
					}
					if = {
						limit = { has_trait = fp3_struggle_detractor }
						remove_trait = fp3_struggle_detractor
					}
					add_trait = fp3_struggle_supporter
				}
				# FP3 Title Transfer Notification
				struggle:persian_struggle = { # Wet script
					every_involved_ruler = {
						send_interface_toast = {
							type = event_toast_effect_bad
							title = persian_struggle_central_title_transfer.t
							left_icon = scope:previous_holder
							right_icon = scope:title.holder

							custom_tooltip = persian_struggle_central_title_transfer.desc
						}
					}
					every_interloper_ruler = {
						send_interface_toast = {
							type = event_toast_effect_bad
							title = persian_struggle_central_title_transfer.t
							left_icon = scope:previous_holder
							right_icon = scope:title.holder

							custom_tooltip = persian_struggle_central_title_transfer.desc
						}
					}
				}
			}
		}

		###############
		# GOVERNMENTS #
		###############

		# TGP bloc joining
		if = {
			limit = {
				government_is_japanese_trigger = yes
				tgp_house_bloc_interaction_valid_trigger = yes
				is_house_head = no
				NOT = { exists = confederation }
				exists = house.house_head.confederation
				liege = house.house_head.liege
				is_at_war = no
			}
			save_scope_as = bloc_joiner
			house.house_head.confederation = { add_confederation_member = scope:bloc_joiner }
		}
		# Challenged as house head in Japan
		if = {
			limit = {
				government_is_japanese_trigger = yes
				is_house_head = yes
			}
			trigger_event = {
				weeks = { 2 4 }
				id = tgp_japan_yearly_events.1210
			}
		}
		# TGP Catalysts
		if = {
			limit = {
				scope:previous_holder ?= {
					has_title = title:h_china
				}
				OR = {
					scope:transfer_type = flag:granted
					scope:transfer_type = flag:appointment
				}
				NOR = {
					any_held_title = {
						is_landless_type_title = no
						this != scope:title
					}
					has_appointment_level_for_title = scope:title
				}
			}
			situation:dynastic_cycle ?= {
				if = {
					limit = { situation_top_has_catalyst = catalyst_hegemon_appointing_low_merit_governor }
					trigger_situation_catalyst = {
						catalyst = catalyst_hegemon_appointing_low_merit_governor
						character = scope:previous_holder
					}
				}
			}
		}
		# If the minister's merit rank is too low, it will affect the dynastic cycle
		if = {
			limit = {
				scope:title = {
					OR = {
						this = title:e_minister_chancellor
						this = title:e_minister_censor
						this = title:e_minister_grand_marshal
						this = title:e_minister_of_personnel
						this = title:e_minister_of_revenue
						this = title:e_minister_of_rites
						this = title:e_minister_of_war
						this = title:e_minister_of_justice
						this = title:e_minister_of_works
					}
				}
				NOT = { has_appointment_level_for_title = scope:title }
			}
			situation:dynastic_cycle ?= {
				if = {
					limit = { situation_top_has_catalyst = catalyst_hegemon_appointing_low_merit_councillor }
					trigger_situation_catalyst = {
						catalyst = catalyst_hegemon_appointing_low_merit_councillor
						character = root
					}
				}
			}
		}
		# Admin Stuff
		if = {
			limit = {
				OR = {
					government_allows = administrative
					government_has_flag = government_is_japan_feudal
				}
				NOT = { has_variable = laamp_invasion_flag }
			}
			government_type = { save_scope_as = nf_gov_type }
			# Note: anything that should be run after creation can go in on_noble_family_title_created
			give_new_noble_family_title_effect = yes
			# EP3 - Ensure Emperor keeps old realm capital in admin realms & apply unique domicile building bonus if appropriate
			if = {
				limit = {
					top_liege = this
					highest_held_title_tier >= tier_empire
					NOT = {
						any_held_title = {
							OR = {
								tier = tier_empire
								tier = tier_hegemony
							}
							this != scope:title
						}
					}
				}
				if = { # Ensure we keep the old realm capital
					limit = {
						scope:title = {
							tier >= tier_empire
							title_capital_county != root.capital_county
							# Constantinople must have been inherited
							title_capital_county.holder = root
						}
					}
					set_realm_capital = scope:title.title_capital_county
				}
				if = { # Get legitimacy from Reception Hall House Bonus
					limit = {
						house = { has_variable = ep3_legacy_legitimacy_counter }
						primary_title = scope:title
					}
					trigger_event = {
						id = ep3_admin_events.0010
						days = 1
					}
				}
			}
			# EP3 - Fire a became governor event
			else_if = {
				limit = {
					top_liege != this
					trigger_if = {
						limit = { government_allows = merit }
						is_governor_or_admin_count = yes
					}
					trigger_else = {
						is_governor = yes
					}
					any_held_title = {
						count < 1
						this != scope:title
						tier >= scope:title.tier
						is_landless_type_title = no
						is_noble_family_title = no
					}
				}
				trigger_event = {
					id = ep3_admin_events.0002
					delayed = yes
				}
			}
			# Admin vassals get the Governor trait when they first become a governor
			if = {
				limit = {
					is_governor = yes
					NOT = { has_trait = governor }
					scope:title = {
						tier >= root.main_administrative_tier
					}
				}
				add_trait = governor
			}
			#Admin imperial succession notification event (it's a big deal)
			if = {
				limit = {
					top_liege = this
					highest_held_title_tier >= tier_kingdom
					scope:title = root.primary_title
					has_ep3_dlc_trigger = yes
					#Checks so this event doesn't fire after character creation, although it's not really a problem if it does
					trigger_if = {
						limit = {
							game_start_date = 867.1.1
						}
						current_date >= 867.1.4
					}
					trigger_if = {
						limit = {
							game_start_date = 1066.9.15
						}
						current_date >= 1066.9.18
					}
					trigger_if = {
						limit = {
							game_start_date = 1178.10.1
						}
						current_date >= 1178.10.4
					}
				}
				save_scope_as = emperor
				#Should trigger special event during fourth crusade!
				if = {
					limit = {
						exists = global_var:byz_claimant_champion
						global_var:byz_claimant_champion = {
							any_owned_story = {
								type = frankokratia_story
								exists = var:byz_claimant
								var:byz_claimant = {
									this = root
								}
							}
						}
					}
					every_vassal_or_below = {
						limit = {
							is_playable_character = yes
						}
						trigger_event = ep3_frankokratia_events.0110
					}
				}
				else = {
					if = {
						limit = {
							exists = root.house
						}
						#normal triggering
						every_vassal_or_below = {
							limit = {
								is_playable_character = yes
								exists = scope:previous_holder
								has_government = administrative_government
								OR = {
									highest_held_title_tier >= main_administrative_tier # All governors
									liege = root
								}
							}
							trigger_event = {
								id = ep3_governor_yearly.3060
								days = 1
							}
						}
					}
				}
			}
			#Admin event about rewarding person who helped you get title
			if = {
				limit = {
					has_succession_appointment_investors = scope:title
					has_ep3_dlc_trigger = yes
					scope:title = {
						any_succession_appointment_investors = {
							count >= 1
							candidate = root
							value >= 20
							NOT = {
								house ?= root.house
							}
							is_alive = yes
							is_imprisoned = no
							NOR = {
								has_relation_rival = root
								is_at_war_with = root
							}
						}
					}
				}
				save_scope_as = new_landed_admin
				trigger_event = {
					id = ep3_interactions_events.0600
					days = { 5 10 }
				}
			}
			#Notification for house members of title holder
			if = {
				limit = {
					scope:title.tier > tier_barony
					primary_title.tier <= scope:title.tier
					NOT = { has_character_flag = admin_title_gain_notification_sent }
					exists = house
					any_player = {
						house ?= root.house
						top_liege = root.top_liege
						government_allows = administrative
						this != root
					}
				}
				add_character_flag = {
					flag = admin_title_gain_notification_sent
					days = 10
				}
				save_scope_as = title_gainer
				every_player = {
					limit = {
						house ?= root.house
						top_liege = root.top_liege
						government_allows = administrative
						this != scope:title_gainer
						scope:title != title:k_chrysanthemum_throne
					}
					send_interface_message = {
						type = msg_admin_title_gained
						title = msg_admin_title_gained_house.tt
						desc = msg_admin_title_gained.desc
						left_icon = scope:title_gainer
						right_icon = scope:title
					}
				}
			}
			#Notification for admin investors in new title holder
			if = {
				limit = {
					NOT = { has_character_flag = admin_title_gain_notification_sent }
					scope:title = {
						tier > tier_barony
						any_succession_appointment_investors = {
							is_ai = no
							candidate = root
							trigger_if = {
								limit = { exists = root.house }
								NOT = { house ?= root.house }
							}
						}
					}
				}
				add_character_flag = {
					flag = admin_title_gain_notification_sent
					days = 10
				}
				save_scope_as = title_gainer
				scope:title = {
					every_succession_appointment_investors = {
						candidate = root
						value > 0
						limit = {
							is_ai = no
							trigger_if = {
								limit = { exists = root.house }
								NOT = { house ?= root.house }
							}
						}
						send_interface_message = {
							type = msg_admin_title_gained
							title = msg_admin_title_gained_investor.tt
							desc = msg_admin_title_gained.desc
							left_icon = scope:title_gainer
							right_icon = scope:title
						}
					}
					every_succession_appointment_investors = {
						candidate = root
						value < 0
						limit = {
							is_ai = no
							trigger_if = {
								limit = { exists = root.house }
								NOT = { house ?= root.house }
							}
						}
						send_interface_message = {
							type = msg_admin_title_gained_foe
							title = msg_admin_title_gained_foe.tt
							desc = msg_admin_title_gained.desc
							left_icon = scope:title_gainer
							right_icon = scope:title
						}
					}
				}
			}
			
			# Merit based candidate score bonus is removed when successfully getting a new governorship
			if = {
				limit = {
					OR = {
						has_variable = merit_civilian_career_score_bonus
						has_variable = merit_military_career_score_bonus
					}
					is_landed = yes
					scope:title.tier >= primary_title.tier
				}
				if = {
					limit = { has_variable = merit_civilian_career_score_bonus }
					remove_variable = merit_civilian_career_score_bonus
				}
				else_if = {
					limit = { has_variable = merit_military_career_score_bonus }
					remove_variable = merit_military_career_score_bonus
				}
			}
		}

		########
		# MISC #
		########

		# TGP, bow before liege mandated
		if = {
			limit = {
				culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
			}
			if = { # if you are now the top liege, all your direct vassals with the same parameter who have not yet sworn fealty to you must swear fealty
				limit = {
					this = top_liege
					primary_title.tier <= scope:title.tier
				}
				every_vassal = {
					if = {
						limit = {
							culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
							OR = {
								government_has_flag = government_is_feudal
								government_has_flag = government_is_clan
								government_has_flag = government_is_japan_feudal
							}
							NOT = { var:pledge_loyalty_to_liege_grace ?= liege }
							NOT = { owns_story_of_type = story_cycle_pledge_loyalty_to_liege_overdue }
						}
						create_story = story_cycle_pledge_loyalty_to_liege_overdue
						random_owned_story = {
							type = story_cycle_pledge_loyalty_to_liege_overdue
							set_variable = new_liege_variant
						}
					}
				}
			}
			else_if = {  # if you now have a new liege, and they also follow a culture with the Pay Homage requirement, you must swear continued fealty
				limit = {
					liege.culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
					OR = {
						government_has_flag = government_is_feudal
						government_has_flag = government_is_clan
						government_has_flag = government_is_japan_feudal
					}
					NOT = { var:pledge_loyalty_to_liege_grace ?= liege }
					NOT = { owns_story_of_type = story_cycle_pledge_loyalty_to_liege_overdue }
				}
				create_story = story_cycle_pledge_loyalty_to_liege_overdue
				random_owned_story = {
					type = story_cycle_pledge_loyalty_to_liege_overdue
					set_variable = new_title_variant
				}
			}
		}

		# Did I conquer a place with holy order leases not of my faith?
		if = {
			limit = {
				scope:title = {
					any_in_de_jure_hierarchy = {
						tier = tier_barony
						is_under_holy_order_lease = yes
						county.holder = {
							any_liege_or_above = { this = root }
						}
						lessee = {
							NAND = { # Only for leases of people who are not of your faith or the holder's faith
								faith = root.faith
								faith = prev.county.holder.faith
							}
						}
					}
				}
			}
			scope:title = {
				every_in_de_jure_hierarchy = {
					limit = {
						tier = tier_barony
						is_under_holy_order_lease = yes
						county.holder = {
							any_liege_or_above = { this = root }
						}
						lessee = {
							NAND = { # Only for leases of people who are not of your faith or the holder's faith
								faith = root.faith
								faith = prev.county.holder.faith
							}
						}
					}
					revoke_lease = yes
				}
			}
		}

		# Everything that requires a previous_holder
		if = {
			limit = {
				exists = scope:previous_holder
			}
			# If a player is on their way to pay homage, invalidate.
			if = {
				limit = {
					any_player = { var:homage_liege_scope ?= scope:previous_holder }
				}
				every_player = {
					limit = {
						var:homage_liege_scope ?= scope:previous_holder
						NOT = { has_character_flag = currently_invalidating_petition_liege_travel }
					}
					current_travel_plan = { 
						if = {
							limit = { can_cancel = yes }
							cancel_travel_plan = yes
						}
					}
					trigger_event = pay_homage.0601
					add_character_flag = {
						flag = currently_invalidating_petition_liege_travel
						days = 1
					}
				}
			}
			# If a player is on their way to bow before liege, invalidate.
			else_if = {
				limit = {
					any_player = { var:pledge_loyalty_to_liege_scope ?= scope:previous_holder }
				}
				every_player = {
					limit = {
						var:pledge_loyalty_to_liege_scope ?= scope:previous_holder
						NOT = { has_character_flag = currently_invalidating_petition_liege_travel }
					}
					current_travel_plan = { 
						if = {
							limit = { can_cancel = yes }
							cancel_travel_plan = yes
						}
					}
					trigger_event = pledge_loyalty_to_liege.0601
					add_character_flag = {
						flag = currently_invalidating_petition_liege_travel
						days = 1
					}
				}
			}

			# Feud
			if = {
				limit = {
					scope:previous_holder = {
						house_has_feud_relation_with_trigger = { TARGET = root }
						house = {
							any_house_relation = {
								any_relation_house = {
									this = root.house
									NOT = { this = scope:previous_holder.house }
								}
								var:house_feud_reason ?= flag:competing_claim
								var:house_feud_attacker.house ?= scope:previous_holder.house
								var:house_feud_victim.house ?= root.house
								var:house_feud_title ?= scope:title
								NOT = { exists = var:feud_avenged }
							}
						}
					}
				}
				scope:previous_holder.house_head = { save_scope_as = house_feud_rival }
				trigger_event = bp1_house_feud.7000
			}
		}

		# EP3 LAAMP SUPPORTER INVALIDATION
		if = {
			limit = {
				exists = var:adventurer_invasion_supporter
				NOT = { government_has_flag = government_is_landless_adventurer }
			}
			ep3_laamp_supporter_invalidated_effect = yes
		}

		# Natural Disaster Recovery project invalidation
		great_projects_ensure_unique_disaster_recovery_projects_effect = yes

		###########
		# Nomads/Tribals leaving confederacies
		###########
		if = {
			limit = {
				is_confederation_member = yes
				scope:title.tier >= tier_kingdom
				government_is_japanese_trigger = no
			}
			confederation = {
				remove_confederation_member = root
			}
		}

		# Peasants dress like nobles if landed
		if = {
			limit = {
				has_character_flag = peasant_outfit
				NOT = { government_has_flag = government_is_true_herder }
			}
			remove_character_flag = peasant_outfit
		}

		# Petition allowed again
		if = {
			limit = { has_variable = petition_title_cooldown }
			remove_variable = petition_title_cooldown
		}

		# Cancel Domicile travels
		current_travel_plan ?= {
			if = {
				limit = { is_travel_with_domicile = yes }
				abort_travel_plan = yes
			}
		}

		# BP3 - Inspector perks cleanup effect
		landless_inspector_cleanup_modifier = yes

		# Update flavorful titles (Margrave, Tenno, etc.)
		additional_flavor_check_effect = yes

		# Update CoAs
		scope:title = { update_dynamic_coa = yes }

		### MEMORIES - SHOULD STAY AT BOTTOM ###
		# Ascended to the throne
		if = {
			limit = {
				OR = {
					is_ruler = no
					highest_held_title_tier <= scope:title.tier
					government_has_flag = government_is_landless_adventurer
					government_allows = administrative
				}
			}
			if = {
				limit = { exists = scope:title.var:adventurer_flavor_char }
				create_character_memory = {
					type = became_landless_adventurer_memory
					participants = {
						flavor_character = scope:title.var:adventurer_flavor_char
					}
				}
				#Only children should get childhood memories
				if = {
					limit = { is_adult = no }
					scope:new_memory = { set_variable = childhood_memory }
				}
				scope:new_memory = {
					save_scope_as = became_landless_adventurer_memory
					set_variable = {
						name = landless_title
						value = scope:title
					}
					set_variable = {
						name = reason
						value = scope:transfer_type
					}
					set_variable = {
						name = adventurer_creation_reason
						value = scope:title.var:adventurer_creation_reason
					}
					if = {
						limit = { exists = scope:lost_primary_title }
						set_variable = {
							name = old_primary_title
							value = scope:lost_primary_title
						}
					}
					if = { # Error suppression
						limit = { exists = var:landless_title }
					}
				}
			}
			else = {
				# Overwrite if there is a more specific Reason
				root = {
					add_to_variable_list = {
						name = new_titles
						target = scope:title
					}
					trigger_event = {
						id = title_event.9900
						delayed = yes
					}
				}
			}
		}

		# If we weren't already in a diarchy but should be, put us in one.
		if = {
			limit = {
				scope:title.tier >= tier_county
			}
			trigger_event = {
				id = diarchy.0011
				days = 1
			}
		}
		#Confederation titles being re-created... get confederation law bb
		if = {
			limit = {
				scope:transfer_type = flag:created
				scope:title = {
					has_variable = confederation_culture
					NOT = {
						has_title_law = confederation_elective_succession_law
					}
				}
			}
			scope:bloc = { add_confederation_member = scope:bloc_joiner }
		}
		#Coronation modifier is lost
		if = {
			limit = {
				scope:title = {
					tier = tier_county
					OR = {
						has_county_modifier = major_coronation_approval_unsettled_modifier
						has_county_modifier = medium_coronation_approval_unsettled_modifier
						has_county_modifier = minor_coronation_approval_unsettled_modifier
						has_county_modifier = major_coronation_approval_modifier
						has_county_modifier = medium_coronation_approval_modifier
						has_county_modifier = minor_coronation_approval_modifier
					}
				}
				NOR = {
					scope:transfer_type = flag:inheritance
					scope:transfer_type = flag:leased_out
					scope:transfer_type = flag:abdication
					scope:transfer_type = flag:granted
					scope:transfer_type = flag:election
					scope:transfer_type = flag:abdication
					scope:transfer_type = flag:stepped_down
					scope:transfer_type = flag:swear_fealty
					scope:transfer_type = flag:created
				}
			}
			#Add negative modifiers if stolen
			scope:title = {
				switch = {
					trigger = has_county_modifier
					major_coronation_approval_modifier = {
						scope:title = {
							remove_county_modifier = major_coronation_approval_modifier
							add_county_modifier = {
								modifier = major_coronation_approval_malus_modifier
								years = 5
							}
						}
					}
					major_coronation_approval_unsettled_modifier = {
						scope:title = {
							remove_county_modifier = major_coronation_approval_unsettled_modifier
							add_county_modifier = {
								modifier = major_coronation_approval_malus_unsettled_modifier
								years = 5
							}
						}
					}
					medium_coronation_approval_modifier = {
						scope:title = {
							remove_county_modifier = medium_coronation_approval_modifier
							add_county_modifier = {
								modifier = medium_coronation_approval_malus_modifier
								years = 5
							}
						}
					}
					medium_coronation_approval_unsettled_modifier = {
						scope:title = {
							remove_county_modifier = medium_coronation_approval_unsettled_modifier
							add_county_modifier = {
								modifier = medium_coronation_approval_malus_unsettled_modifier
								years = 5
							}
						}
					}
					minor_coronation_approval_modifier = {
						scope:title = {
							remove_county_modifier = minor_coronation_approval_modifier
							add_county_modifier = {
								modifier = minor_coronation_approval_malus_modifier
								years = 5
							}
						}
					}
					minor_coronation_approval_unsettled_modifier = {
						scope:title = {
							remove_county_modifier = minor_coronation_approval_unsettled_modifier
							add_county_modifier = {
								modifier = minor_coronation_approval_malus_unsettled_modifier
								years = 5
							}
						}
					}
				}
			}
		}

		# House head goes to independent ruler for Japanese AI
		if = {
			limit = {
				is_house_head = no
				is_ai = yes
				government_is_japanese_trigger = yes
				scope:previous_holder = {
					is_ai = yes
					trigger_if = {
						limit = { exists = scope:player_heir }
						player_heir = { is_ai = yes }
					}
				}
				scope:new_holder = {
					is_ai = yes
					trigger_if = {
						limit = { exists = scope:player_heir }
						player_heir = { is_ai = yes }
					}
				}
				house.house_head = {
					is_ai = yes
					trigger_if = {
						limit = { exists = scope:player_heir }
						player_heir = { is_ai = yes }
					}
					is_vassal_or_below_of = root
				}
				is_independent_ruler = yes
			}
			house = { set_house_head = root }
		}

		if = { # If courtiers get lost between governorship assignments, move them home
			limit = {
				is_ai = no
				government_allows = merit
			}
			if = {
				limit = {
					is_landed = yes
				}
				every_courtier = {
					limit = {
						location != root.capital_province
						is_imprisoned = no
						is_travelling = no
						is_commanding_army = no
						NOR = {
							exists = involved_activity
							exists = knight_army
						}
					}
					set_location_to_default = yes
				}
			}
			else_if = {
				limit = {
					exists = domicile.domicile_location
				}
				every_courtier = {
					limit = {
						location != root.domicile.domicile_location
						is_imprisoned = no
						is_travelling = no
						is_commanding_army = no
						NOR = {
							exists = involved_activity
							exists = knight_army
						}
					}
					set_location_to_default = yes
				}
			}
		}
		# If you take another realm's dynasty-named title, reset its name
		if = {
			limit = {
				is_independent_ruler = yes
				government_has_flag = government_is_celestial
				scope:title ?= {
					tier >= tier_kingdom
					has_custom_title_name = yes
					this != root.primary_title
				}
			}
			scope:title = {
				reset_title_name = yes
			}
		}
	}
	events = {
		roman_restoration.0005		#New Roman Emperor gets Augustus trait. (old holder, if still alive, loses it)
		roman_restoration.0199		#Restoring the Pentarchy.
		delay = { days = 1 }
		realm_maintenance.2001		#Big notification about inheriting emperor tier title
		british_isles.1032			# Danelaw-England partition calc.
		title_event.0001			# Rename West Francia to France
		title_event.0002			# Rename East Francia to Germany
		title_event.0011			# Asturias becomes Leon upon emergence of Castille
		# If we weren't already in a diarchy but should be, put us in one.
		diarchy.0011
		ep3_emperor_yearly.2020		# EP3: The Patriarch refuses to crown you as new emperor because you have a criminal trait
	}
}

# A title is inherited by a character
# root = the new holder
# scope:title = the title that changes hands
# scope:previous_holder = previous holder. Should be dead
on_title_gain_inheritance = {
	events = {
		delay = { days = 1 }
		# Try to end inherited entrenched regencies automatically — we need to launder this by a day to make sure the AI realises they've inherited a diarchy.
		diarchy.0113
	}
	effect = {
		if = {
			limit = {
				OR = {
					government_has_flag = government_is_prepublic
					government_has_flag = government_is_urepublic
				}
				scope:title = {
					is_nomad_title = no
					title_tier = county
					scope:title.title_province = {
						NOR = {
							has_holding_type = monotown_holding
							has_holding_type = city_holding
						}
					}
				}
			}
			scope:title = {
				limit = {
					is_nomad_title = no
					title_tier = county
					scope:title.title_province = {
						NOR = {
							has_holding_type = monotown_holding
							has_holding_type = city_holding
						}
					}
				}
				scope:title.title_province = {
					set_holding_type = city_holding
				}
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_prepublic
				NOT = {
					exists = domicile
				}
			}
			save_scope_as = scoped_farmer
			create_nomad_title = {
				name = domicile_farmstead
				holder = scope:scoped_farmer
				government = peasantrepublic_government
				save_scope_as = new_title
			}
			scope:new_title ?= {
				add_title_law = noble_family_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_prepublic
				any_held_title = {
					title_tier > barony
					is_nomad_title = no
					NOT = {
						has_title_law = saxon_elective_succession_law
					}
				}
			}
			every_held_title = {
				limit = {
					title_tier > barony
					is_nomad_title = no
				}
				add_title_law = saxon_elective_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_urepublic
				NOT = {
					exists = domicile
				}
			}
			save_scope_as = scoped_merchant
			create_nomad_title = {
				name = domicile_palace
				holder = scope:scoped_merchant
				government = urbanrepublic_government
				save_scope_as = new_title
			}
			scope:new_title ?= {
				add_title_law = noble_family_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_urepublic
				any_held_title = {
					title_tier > barony
					is_nomad_title = no
					NOT = {
						has_title_law = urban_elective_succession_law
					}
				}
			}
			every_held_title = {
				limit = {
					title_tier > barony
					is_nomad_title = no
				}
				add_title_law = urban_elective_succession_law
			}
		}
		if = {
			limit = {
				government = landless_minority_government
				exists = liege
			}
			create_title_and_vassal_change = {
				type = independency
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:root = {
				becomes_independent = { change = scope:change }
			}
			resolve_title_and_vassal_change = scope:change
		}
		if = {
			limit = {
				scope:title = {
					OR = {
						this = title:e_mongol_empire
						AND = {
							exists = global_var:greatest_of_khans_title
							this = global_var:greatest_of_khans_title
						}
					}
				}
				has_character_flag = gain_mongol_succession_dread
			}
			add_dread = 50
		}
		else_if = {
			limit = { 
				OR = {
					scope:title = title:h_roman_empire 
					scope:title = title:h_eastern_roman_empire
				}
			}
			set_variable = uses_custom_caesar_flavourization
		}
	}
}

# A title is usurped by a character
# root = the new holder
# scope:title = the title that changes hands
# scope:previous_holder = previous holder. Shouldn't be dead
on_title_gain_usurpation = {
	events = {
	}
	effect = {
		if = {
			limit = {
				OR = {
					government_has_flag = government_is_prepublic
					government_has_flag = government_is_urepublic
				}
				scope:title = {
					is_nomad_title = no
					title_tier = county
					scope:title.title_province = {
						NOR = {
							has_holding_type = monotown_holding
							has_holding_type = city_holding
						}
					}
				}
			}
			scope:title = {
				limit = {
					is_nomad_title = no
					title_tier = county
					scope:title.title_province = {
						NOR = {
							has_holding_type = monotown_holding
							has_holding_type = city_holding
						}
					}
				}
				scope:title.title_province = {
					set_holding_type = city_holding
				}
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_prepublic
				NOT = {
					exists = domicile
				}
			}
			save_scope_as = scoped_farmer
			create_nomad_title = {
				name = domicile_farmstead
				holder = scope:scoped_farmer
				government = peasantrepublic_government
				save_scope_as = new_title
			}
			scope:new_title ?= {
				add_title_law = noble_family_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_prepublic
				any_held_title = {
					title_tier > barony
					is_nomad_title = no
					NOT = {
						has_title_law = saxon_elective_succession_law
					}
				}
			}
			every_held_title = {
				limit = {
					title_tier > barony
					is_nomad_title = no
				}
				add_title_law = saxon_elective_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_urepublic
				NOT = {
					exists = domicile
				}
			}
			save_scope_as = scoped_merchant
			create_nomad_title = {
				name = domicile_palace
				holder = scope:scoped_merchant
				government = urbanrepublic_government
				save_scope_as = new_title
			}
			scope:new_title ?= {
				add_title_law = noble_family_succession_law
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_urepublic
				any_held_title = {
					title_tier > barony
					is_nomad_title = no
					NOT = {
						has_title_law = urban_elective_succession_law
					}
				}
			}
			every_held_title = {
				limit = {
					title_tier > barony
					is_nomad_title = no
				}
				add_title_law = urban_elective_succession_law
			}
		}
		if = {
			limit = {
				government = landless_minority_government
				exists = liege
			}
			create_title_and_vassal_change = {
				type = independency
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:root = {
				becomes_independent = { change = scope:change }
			}
			resolve_title_and_vassal_change = scope:change
		}
		# Struggle Catalysts
		if = {
			limit = { # FP2
				# We are only interested in the title within the Struggle Region
				scope:title.empire ?= title:e_spain
				# Only for usurpation
				scope:transfer_type = flag:usurped

				root = {
					any_character_struggle = {
						involvement = involved
						activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
							CATALYST = catalyst_usurp_title_fp2
							CHAR = root
						}
					}
				}
			}
			root = {
				every_character_struggle = {
					involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
							CATALYST = catalyst_usurp_title_fp2
							CHAR = root
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_usurp_title_fp2
						character = root
					}
				}
			}
		}
		else_if = {
			limit = { # FP3
				scope:title.empire ?= title:e_persia
				scope:transfer_type = flag:usurped

				root = {
					any_character_struggle = {
						involvement = involved
						activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
							CATALYST = catalyst_usurp_title_fp3
							CHAR = root
						}
					}
				}
			}
			root = {
				every_character_struggle = {
					involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
							CATALYST = catalyst_usurp_title_fp3
							CHAR = root
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_usurp_title_fp3
						character = root
					}
					log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_usurp_title_fp3 }
				}
			}
		}
	}
}

# A title is lost by a character
# root = the old holder
# scope:title = the title that changes hands
# scope:new_holder = new holder.
on_title_lost = {
	events = {
		british_isles.1033	# Danelaw-England partition calc.
	}

	effect = {
		if = {
			limit = {
				government = landless_minority_government
				exists = liege
			}
			create_title_and_vassal_change = {
				type = independency
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:root = {
				becomes_independent = { change = scope:change }
			}
			resolve_title_and_vassal_change = scope:change
		}
		#Make sure nobody ever has only the Kingdom of Fashion
		if = {
			limit = {
				scope:title = { tier = tier_kingdom }
				has_title = title:k_fashion
				NOT = {
					any_held_title = { # Only trigger if you don't have any kingdom titles other than Fashion and the kingdom you just lost. Double negative is confusing but it's the only way I could think to script it
						title_tier = kingdom
						NOT = { this = title:k_fashion }
						this != scope:title
					}
				}
			}
			destroy_title = title:k_fashion
		}
		#Earmark landless Scandinavians for adventuring.
		if = {
			limit = {
				#We only want to grab tribal cultures.
				culture = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				#Must have lost their last piece of land.
				is_playable_character = no
				#If they've ventured far outside Europe, they're unlikely to come back, so we don't #want them.
				scope:title = {
					tier = tier_county
					title_province = {
						OR = {
							geographical_region = world_europe
							geographical_region = world_asia_minor
						}
					}
				}
			}
			#Norse, Norwegians, & Danes go into the western adventurer list.
			if = {
				limit = {
					OR = {
						has_culture = culture:norse
						has_culture = culture:norwegian
						has_culture = culture:danish
					}
				}
				add_to_global_variable_list = {
					name = western_scandinavian_adventurer_list
					target = this
				}
			}
			#Norse & Swedes go into the eastern adventurer list.
			if = {
				limit = {
					OR = {
						has_culture = culture:norse
						has_culture = culture:swedish
					}
				}
				add_to_global_variable_list = {
					name = eastern_scandinavian_adventurer_list
					target = this
				}
			}
		}

		#To remove concubines if a character becomes unlanded
		if = {
			limit = {
				is_playable_character = no
				any_concubine = { }
			}
			every_concubine = { root = { remove_concubine = prev } }
		}

		# Transfer co-monarchies.
		if = {
			limit = {
				OR = {
					scope:transfer_type = flag:inheritance
					scope:transfer_type = flag:abdication
					scope:transfer_type = flag:usurped
					scope:transfer_type = flag:election
					scope:transfer_type = flag:faction_demand
					scope:transfer_type = flag:stepped_down
				}
			}
			save_scope_as = old_holder
			trigger_event = {
				id = diarchy.0025
				days = 1
			}
		}

		#Apply loss of stress to the Rivals
		if = {
			limit = {
				scope:title.tier >= tier_county
				scope:title = { is_landless_type_title = no }
				OR = {
					scope:transfer_type = flag:conquest
					scope:transfer_type = flag:conquest_holy_war
					scope:transfer_type = flag:conquest_claim
					scope:transfer_type = flag:conquest_populist
					scope:transfer_type = flag:abdication
					scope:transfer_type = flag:usurped
					scope:transfer_type = flag:revoked
					scope:transfer_type = flag:faction_demand
				}
			}
			root = {
				save_scope_as = actor
			}
			every_relation ={
				type = rival

				send_interface_message = {
					type = event_generic_neutral
					title = msg_rival_dethroned
					left_icon = scope:actor
					right_icon = scope:title

					custom_tooltip = msg_rival_dethroned_desc

					stress_impact = {
						base = medium_stress_loss
						vengeful = medium_stress_loss
					}
				}
			}
		}

		# Struggle catalyst
		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					phase_has_catalyst = catalyst_revoke_title
				}
				OR = {
					scope:transfer_type = flag:revoked
					scope:transfer_type = flag:lease_revoked
				}
			}
			every_character_struggle = {
				activate_struggle_catalyst = {
					catalyst = catalyst_revoke_title
					character = scope:new_holder
				}
			}
		}

		# Memories
		# Lost a significant title
		if = {
			limit = {
				scope:title.tier >= highest_held_title_tier
				exists = scope:new_holder
			}
			if = {
				limit = { exists = root.var:lost_title_reason }
				root.var:lost_title_reason = { save_scope_as = real_transfer_type }
				root = { remove_variable = lost_title_reason }
			}
			else = {
				scope:transfer_type = { save_scope_as = real_transfer_type }
			}
			root = {
				add_to_variable_list = {
					name = lost_titles
					target = scope:title
				}
				trigger_event = {
					id = title_event.9901
					delayed = yes
				}
			}
		}

		# LEGITIMACY LOSS FROM USURPATION
		if = {
			limit = {
				scope:transfer_type ?= flag:usurped
				has_legitimacy = yes
				NOT = { exists = scope:suppress_legitimacy_loss }
			}
			# Minus 2, since we want Duchy to be 1, and it's normally 3
			save_scope_value_as = {
				name = title_tier_minus_2
				value = {
					value = scope:title.tier
					subtract = 2
				}
			}
			send_interface_toast = {
				type = msg_legitimacy_loss
				title = legitimacy_loss_toast
				left_icon = root
				right_icon = scope:title
				custom_tooltip = legitimacy_loss_usurpation_tt
				add_legitimacy = {
					value = {
						value = miniscule_legitimacy_loss
						multiply = scope:title_tier_minus_2
					}
				}
			}
		}

		# EP3 LAAMP SUPPORTER INVALIDATION
		ep3_laamp_supporter_invalidated_effect = yes

		# EP3 - Handle admin estates when they end up outside the realm
		if = {
			limit = {
				scope:new_holder = {
					any_sub_realm_barony = {
						title_province = {
							any_province_domicile = {
								is_domicile_type = estate
								owner ?= {
									top_liege != scope:new_holder.top_liege
								}
							}
						}
					}
				}
			}
			scope:new_holder = {
				every_sub_realm_barony = {
					limit = {
						title_province = {
							any_province_domicile = {
								is_domicile_type = estate
								owner ?= {
									top_liege != scope:new_holder.top_liege
								}
							}
						}
					}
					title_province = {
						every_province_domicile = {
							limit = {
								is_domicile_type = estate
								owner ?= {
									top_liege != scope:new_holder.top_liege
								}
							}

							# First, let's do some damage to the estate - We randomize the level of damage taken
							random_list = {
								40 = { # Damage 1
									trigger = {
										num_domicile_buildings <= 6
									}
									set_variable = {
										name = domicile_damage_count
										value = 1
									}
								}
								35 = { # Damage 2
									trigger = {
										num_domicile_buildings > 2
									}
									set_variable = {
										name = domicile_damage_count
										value = 2
									}
								}
								30 = { # Damage 3
									trigger = {
										num_domicile_buildings > 4
									}
									set_variable = {
										name = domicile_damage_count
										value = 3
									}
								}
								25 = { # Damage 4
									trigger = {
										num_domicile_buildings > 6
									}
									set_variable = {
										name = domicile_damage_count
										value = 4
									}
								}
								20 = { # Damage 5
									trigger = {
										num_domicile_buildings > 8
									}
									set_variable = {
										name = domicile_damage_count
										value = 5
									}
								}
								15 = { # Damage 6
									trigger = {
										num_domicile_buildings > 10
									}
									set_variable = {
										name = domicile_damage_count
										value = 6
									}
								}
							}
							while = {
								count = var:domicile_damage_count
								destroy_random_estate_building_variable_effect = yes
								destroy_random_estate_building_effect = yes
							}

							# Clear the variable
							if = {
								limit = { has_variable = domicile_damage_count }
								remove_variable = domicile_damage_count
							}

							# Then we'll move the estate to keep it within the realm's borders
							if = { # If you have a capital (i.e. you are a governor) let's move the estate there
								limit = {
									owner = {
										is_landed = yes
										exists = capital_province
									}
								}
								move_domicile = owner.capital_province
							}
							else_if = { # If not, let's move it to a landed house member in the realm, if you have one
								limit = {
									owner = {
										top_liege ?= { save_temporary_scope_as = owner_top_liege }
										house ?= {
											any_house_member = {
												is_governor = yes
												top_liege = scope:owner_top_liege
											}
										}
									}
								}
								owner = {
									house = {
										random_house_member = {
											limit = {
												is_governor = yes
												exists = capital_province
												top_liege = scope:owner_top_liege
											}
											capital_province = { save_temporary_scope_as = house_member_location }
										}
									}
								}
								move_domicile = scope:house_member_location
							}
							else_if = { # Otherwise move it to the realm capital
								limit = {
									exists = owner.top_liege.capital_province
								}
								move_domicile = owner.top_liege.capital_province
							}

							# Save the new estate location for msg
							domicile_location.barony = {
								save_scope_as = new_estate_location
							}

							# Finally, let's make sure the owner moves to the new estate location
							owner = {
								if = {
									limit = {
										is_alive = yes
										is_imprisoned = no
										is_travelling = no
										NOT = {
											exists = involved_activity
										}
										is_commanding_army = no
									}
									set_location_to_default = yes
								}

								# And notify the owner that all of this just happened
								if = {
									limit = { is_ai = no }
									send_interface_message = {
										type = msg_domicile_damaged
										title = msg_estate_damaged.title
										desc = msg_estate_damaged.desc
										right_icon = scope:new_estate_location
									}
								}
							}
						}
					}
				}
			}
		}

		# Additional Flavor Title
		additional_flavor_check_effect = yes

		if = {
			limit = { 
				OR = {
					scope:title = title:h_roman_empire 
					scope:title = title:h_eastern_roman_empire
				}
			}
			remove_variable = uses_custom_caesar_flavourization
		}
		#Notification for house members of loser
		if = {
			limit = {
				scope:title.tier > tier_barony
				trigger_if = { # Prevent msg spam when rulers give away or lose lower tier titles
					limit = {
						is_landed = yes
					}
					primary_title.tier <= scope:title.tier
				}
				is_alive = yes
				NOT = { has_character_flag = admin_title_loss_notification_sent }
				government_allows = administrative
				house ?= {
					any_house_member = {
						is_ai = no
						trigger_if = {
							limit = { exists = root.top_liege }
							top_liege = root.top_liege
						}
						government_allows = administrative
						this != root
					}
				}
			}
			add_character_flag = {
				flag = admin_title_loss_notification_sent
				days = 10
			}
			save_scope_as = title_loser
			house = {
				every_house_member = {
					limit = {
						is_ai = no
						trigger_if = {
							limit = { exists = root.top_liege }
							top_liege = root.top_liege
						}
						government_allows = administrative
						this != scope:title_loser
					}
					send_interface_message = {
						type = msg_admin_title_lost
						title = msg_admin_title_lost_house.tt
						desc = msg_admin_title_lost.desc
						left_icon = scope:title_loser
						right_icon = scope:title
					}
				}
			}
		}
		#Notification for admin investors in loser
		if = {
			limit = {
				is_alive = yes
				NOT = { has_character_flag = admin_title_loss_notification_sent }
				scope:title = {
					tier > tier_barony
					any_succession_appointment_investors = {
						candidate = root
						is_ai = no
						trigger_if = {
							limit = { exists = root.house }
							NOT = { house ?= root.house }
						}
					}
				}
			}
			add_character_flag = {
				flag = admin_title_loss_notification_sent
				days = 10
			}
			save_scope_as = title_loser
			scope:title = {
				every_succession_appointment_investors = {
					candidate = root
					value > 0
					limit = {
						is_ai = no
						trigger_if = {
							limit = { exists = root.house }
							NOT = { house ?= root.house }
						}
					}
					send_interface_message = {
						type = msg_admin_title_lost
						title = msg_admin_title_lost_investor.tt
						desc = msg_admin_title_lost.desc
						left_icon = scope:title_loser
						right_icon = scope:title
					}
				}
				every_succession_appointment_investors = {
					candidate = root
					value < 0
					limit = {
						is_ai = no
						trigger_if = {
							limit = { exists = root.house }
							NOT = { house ?= root.house }
						}
					}
					send_interface_message = {
						type = msg_admin_title_lost_foe
						title = msg_admin_title_lost_foe.tt
						desc = msg_admin_title_lost.desc
						left_icon = scope:title_loser
						right_icon = scope:title
					}
				}
			}
		}

		# Lil Johnny Lackland changes his nickname.
		if = {
			limit = {
				has_nickname = nick_lackland_ironic
				scope:title.tier >= tier_duchy
			}
			give_nickname = nick_lackland_old
		}

		# BP3 - Inspector perks cleanup effect
		landless_inspector_cleanup_modifier = yes

		#End Greatest of Khans story if title was lost
		if = {
			limit = {
				any_owned_story = {
					story_type = story_greatest_of_khans
				}
				global_var:greatest_of_khans_title ?= {
					this = scope:title
				}
				OR = {
					NOT = {
						exists = scope:new_holder
					}
					scope:new_holder = {
						NOR = {
							AND = {
								exists = dynasty
								dynasty ?= root.dynasty
							}
							is_grandchild_of = root
							is_child_of = root
						}
					}
				}
			}
			if = {
				limit = {
					is_alive = yes
				}
				trigger_event = {
					id = mpo_greatest_of_khans.0040
					days = 1
				}
			}
			else_if = {
				limit = {
					exists = root.player_heir
					exists = root.dynasty
					player_heir = {
						dynasty ?= story_owner.dynasty
					}
				}
				player_heir ?= {
					trigger_event = {
						id = mpo_greatest_of_khans.0040
						days = 1
					}
				}
			}
			else = {
				every_owned_story = {
					limit = { story_type = story_greatest_of_khans }
					end_story = yes
				}
				remove_character_modifier = the_great_khan_modifier
			}
		}
		#Coronation loyalty modifier
		if = {
			limit = {
				scope:title = {
					has_variable = coronation_loyal_to_title
					var:coronation_loyal_to_title = {
						NOT = {
							this = scope:new_holder.primary_title
						}
					}
				}
			}
			scope:title = {
				remove_county_modifier = coronation_loyal_to_the_crown_modifier
				remove_variable = coronation_loyal_to_title
				add_county_modifier = {
					modifier = coronation_still_loyal_modifier
					years = 5
				}
			}
		}

		if = { # If courtiers get lost between governorship assignments, move them home
			limit = {
				is_ai = no
				government_allows = merit
			}
			if = {
				limit = {
					is_landed = yes
				}
				every_courtier = {
					limit = {
						location != root.capital_province
						is_imprisoned = no
						is_travelling = no
						is_commanding_army = no
						NOR = {
							exists = involved_activity
							exists = knight_army
						}
					}
					set_location_to_default = yes
				}
			}
			else_if = {
				limit = {
					is_landed = no
					exists = domicile.domicile_location
				}
				every_courtier = {
					limit = {
						location != root.domicile.domicile_location
						is_imprisoned = no
						is_travelling = no
						is_commanding_army = no
						NOR = {
							exists = involved_activity
							exists = knight_army
						}
					}
					set_location_to_default = yes
				}
			}
		}

		# Remove manually added Ministers if they're not ministers anymore
		if = {
			limit = {
				NOR = {
					primary_title = title:e_minister_chancellor
					primary_title = title:e_minister_censor
					primary_title = title:e_minister_grand_marshal
					primary_title = title:e_minister_of_personnel
					primary_title = title:e_minister_of_revenue
					primary_title = title:e_minister_of_rites
					primary_title = title:e_minister_of_war
					primary_title = title:e_minister_of_justice
					primary_title = title:e_minister_of_works
				}
			}
			situation:dynastic_cycle = {
				remove_manual_participant = prev
			}
		}

		# Move or destroy estates without a liege
		if = {
			limit = {
				# Has an NF title
				any_held_title = {
					NOT = { this = scope:title }
					is_noble_family_title = yes
				}
				# Only remaining title is NF title
				any_held_title = {
					count = 0
					NOT = { this = scope:title }
					is_noble_family_title = no
				}
			}
			if = {
				limit = {
					OR = {
						exists = scope:new_holder.var:laamp_invasion_flag
						trigger_if = {
							limit = { government_is_japanese_trigger = yes }
							scope:new_holder.top_liege = { government_is_japanese_trigger = no }
						}
						trigger_else_if = {
							limit = {
								scope:new_holder.top_liege = { government_is_japanese_trigger = yes }
							}
							government_is_japanese_trigger = no
						}
						trigger_else = {
							NOT = { has_same_government = scope:new_holder.top_liege }
						}
					}
				}
				# Find appropriate new realm if possible
				scope:new_holder.top_liege = {
					ordered_neighboring_and_across_water_realm_owner = {
						limit = {
							highest_held_title_tier >= main_administrative_tier
							trigger_if = {
								limit = {
									root = { government_is_japanese_trigger = yes }
								}
								government_is_japanese_trigger = yes
							}
							trigger_else = { has_same_government = root }
						}
						order_by = highest_held_title_tier
						save_scope_as = new_realm_owner
					}
				}
				if = { # Change liege
					limit = { exists = scope:new_realm_owner }
					create_title_and_vassal_change = {
						type = conquest
						save_scope_as = change
					}
					change_liege = {
						liege = scope:new_realm_owner
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
				}
				else = { # Game over
					random_held_title = {
						limit = { is_noble_family_title = yes }
						save_scope_as = title_to_destroy
						scope:new_holder.top_liege = { destroy_title = scope:title_to_destroy }
					}
				}
			}
		}
		else_if = {
			limit = {
				is_house_head = no
				# Has an NF title
				exists = house
				house.house_head = {
					any_held_title = { is_noble_family_title = yes }
				}
				# Only remaining title is NF title
				any_held_title = {
					count = 0
					NOT = { this = scope:title }
				}
			}
			house.house_head = {
				save_temporary_scope_as = house_head_court
			}
			every_courtier = {
				if = {
					limit = {
						NOT = { is_courtier_of = scope:house_head_court }
					}
					save_temporary_scope_as = courtier
					scope:house_head_court = { add_courtier = scope:courtier }
				}
			}
			if = {
				limit = {
					NOT = { is_courtier_of = scope:house_head_court }
				}
				scope:house_head_court = { add_courtier = root }
			}
		}
	}
}

# A claim is gained by a character
# root = the claimant
# scope:title = the title that is claimed
# scope:transfer_type = flag:inheritance or none
on_explicit_claim_gain = {
	effect = {
		if = {
			limit = {
				title:k_england.holder = root
			}
			title:k_england = {
				update_dynamic_coa = yes
			}
		}
		# Struggle Catalyst
		if = { # FP2
			limit = {
				NOT = { scope:transfer_type = flag:inheritance }
				
				root = {
					any_character_struggle = {
						is_struggle_type = iberian_struggle
						involvement = involved
					}
				}
				title:e_spain = { any_in_de_jure_hierarchy = { this = scope:title } }
			}
			root = {
				every_character_struggle = {
					involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
							CATALYST = catalyst_gain_claim_on_title_fp2
							CHAR = root
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_gain_claim_on_title_fp2
						character = root
					}
				}
			}
		}
		else_if = { # FP3
			limit = {
				NOT = { scope:transfer_type = flag:inheritance }
				
				root = {
					any_character_struggle = {
						is_struggle_type = persian_struggle
						involvement = involved
					}
				}
				title:e_persia = { any_in_de_jure_hierarchy = { this = scope:title } }
			}
			root = {
				every_character_struggle = {
					involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
							CATALYST = catalyst_gain_claim_on_title_fp3
							CHAR = root
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_gain_claim_on_title_fp3
						character = root
					}
					log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_gain_claim_on_title_fp3 }
				}
			}
		}
	}
}

# A claim is lost by a character
# root = the claimant
# scope:title = the title that was claimed
on_explicit_claim_lost = {
	effect = {
		if = {
			limit = {
				title:k_england.holder = root
			}
			title:k_england = {
				update_dynamic_coa = yes
			}
		}
	}
}

# A title change makes the character rank up in the landed tiers (eg Duke -> King)
# root = character ranking up
# scope:title = old primary title
on_rank_up = { # Will not fire during history execution or for dying characters
	events = {
	}
	effect = {
		if = { # ACH call to coronation reminder
			limit = {
				coronation_trigger = yes
				NOT = {
					has_game_rule = coronation_laws_off
				}
				is_ai = no
				primary_title.tier >= tier_kingdom
				has_realm_law = uncrowned
				NOR = {
					has_variable = crowned_king_var
					has_variable = crowned_emperor_var
				}
				NOR = {
					has_character_flag = had_coronation_reminder
					any_sponsored_inspiration = { has_inspiration_type = smith_inspiration }
				}
				is_available_adult = yes	
			}
			trigger_event = {
				id = ach_yearly_events.1005
				months = 4
			}
		}
		#ACH new emperors losing their crowned status
		else_if = {
			limit = {
				coronation_trigger = yes
				NOR = {
					has_game_rule = coronation_laws_no_imperial_uncrowned
					has_game_rule = coronation_laws_off
					has_character_flag = claimed_the_mandate_of_heaven
				}
				highest_held_title_tier >= tier_empire
				exists = scope:title
				scope:title = {
					tier >= tier_kingdom
					#Just double checking
					tier < root.highest_held_title_tier
				}
				OR = {
					has_realm_law = crowned_king
					has_realm_law = crowned_emperor
					has_variable = crowned_king_var
					has_variable = crowned_emperor_var
				}
			}
			if = {
				limit = {
					has_variable = crowned_emperor_var
				}
				remove_variable = crowned_emperor_var
			}
			if = {
				limit = {
					has_variable = crowned_king_var
				}
				remove_variable = crowned_king_var
			}
			add_realm_law_skip_effects = uncrowned
			trigger_event = ach_yearly_events.1010
		}
		if = {
			limit = { is_landed = yes }
			add_achievement_flag_effect = { FLAG = achievement_moving_up_in_the_world_flag }
		}
		if = {
			limit = {
				is_ai = no
				is_landed = yes
				highest_held_title_tier = tier_empire
			}
			add_achievement_global_variable_effect = {
				VARIABLE = rags_to_riches_to_rags_to_riches_first_empire
				VALUE = yes
			}
		}
		if = {
			limit = {
				is_ai = no
				is_landed = yes
				highest_held_title_tier = tier_empire
				exists = global_var:rags_to_riches_to_rags_to_riches_adventurer
			}
			add_achievement_global_variable_effect = {
				VARIABLE = finished_rags_to_riches_to_rags_to_riches_achievement
				VALUE = yes
			}
		}
		update_embassies_effect = yes
		every_ally = {
			update_embassies_effect = yes
		}
		if = {
			limit = { top_liege = this }
			remove_variable = use_co_ruler_title
		}
		# Make a note of ranking up for stele purposes.
		if = {
			limit = {
				# DLC check.
				has_fp1_dlc_trigger = yes
				# Is the culture eligible?
				fp1_can_raise_stele_trigger = yes
				# Aaaaand, for balance reasons, were they landed already?
				exists = scope:title
			}
			set_variable = {
				name = recent_rank_increase
				value = root.primary_title
				years = 5
			}
		}
		# Remove any rank-inappropriate modifiers (e.g., baron buffs).
		remove_character_modifier = mandate_baronial_troops_trained_modifier
		remove_character_modifier = mandate_baronial_troops_half_trained_modifier

		# Additional Flavor Title
		additional_flavor_check_effect = yes

		#Admin kings becoming emperors need their MAA transferred to new primary title
		if = {
			limit = {
				is_independent_ruler = yes
				government_allows = administrative
				scope:title ?= {
					any_title_maa_regiment = {
						count >= 1
					}
				}
			}
			scope:title = {
				transfer_title_maa_ownership = root.primary_title
			}
		}

		if = {
			limit = {
				is_independent_ruler = yes
				government_has_flag = government_is_celestial
				scope:title ?= {
					tier >= tier_kingdom
					has_custom_title_name = yes
				}
				primary_title = {
					has_custom_title_name = no
				}
			}
			scope:title = {
				move_title_name_to = root.primary_title
				reset_title_name = yes
			}
		}
		if = {
			limit = {
				is_independent_ruler = yes
				government_has_flag = government_is_celestial
				highest_held_title_tier >= tier_kingdom
				OR = {
					primary_title ?= {
						has_custom_title_name = no
					}
					primary_title.previous_holder.dynasty != root.dynasty
				}
				trigger_if = {
					limit = {
						tgp_capital_not_in_chinese_naming_region = yes
					}
					primary_title ?= title:h_china
				}
			}
			if = {
				limit = {
					is_ai = no
				}
				trigger_event = {
					id = tgp_dynastic_cycle.9000
					days = 1 # so the player gets to choose first
				}
			}
			else = {
				trigger_event = {
					id = tgp_dynastic_cycle.9000
					days = 2
				}
			}
		}
		else_if = { # Make sure non-Celestials get to rename/recolor china
			limit = {
				is_independent_ruler = yes
				has_tgp_dlc_trigger = yes
				NOT = { government_has_flag = government_is_celestial }
				exists = var:claimed_mandate_var
				primary_title ?= title:h_china
				OR = {
					primary_title ?= {
						has_custom_title_name = no
					}
					primary_title.previous_holder.dynasty != root.dynasty
				}
			}
			if = {
				limit = {
					is_ai = no
				}
				trigger_event = {
					id = tgp_dynastic_cycle.9000
					days = 1 # so the player gets to choose first
				}
			}
			else = {
				trigger_event = {
					id = tgp_dynastic_cycle.9000
					days = 2
				}
			}
		}

		# ACH - Remove Coronated laws when titles are passed onto a new character
		coronation_reset_realm_law_effect = yes

		# TGP Japan handling for blocs
		if = {
			limit = {
				this = top_liege
				tgp_uses_house_blocs_trigger = yes
				exists = house.house_confederation
			}
			if = { # Ensure top liege is house head, if not stealing from a player
				limit = {
					is_house_head = no
					house.house_head = { is_ai = yes }
					top_liege = house.house_head.top_liege
				}
				house = { set_house_head = prev }
			}
			if = { # Ensure independent rulers are in charge of their blocs
				limit = {
					is_house_head = yes
					NOT = { house.house_confederation.leading_house = this.house }
				}
				house.house_confederation = {
					tgp_set_house_bloc_leading_house_effect = { LEADER = root.house }
				}
			}
		}

		# Ensure Ministers (or any landless rulers) do not have vassals
		if = {
			limit = {
				is_landed = no
				this != top_liege
			}
			save_scope_as = liege
			create_title_and_vassal_change = {
				type = appointment
				save_scope_as = change
			}
			every_vassal = {
				limit = {
					is_landed = no
					any_held_title = { is_noble_family_title = yes }
				}
				save_scope_as = vassal
				change_liege = {
					liege = scope:liege.top_liege
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
}

# A title change makes the character rank down in the landed tiers (eg King -> Duke)
# root = character ranking down
# scope:title = old primary title
on_rank_down = { # Will not fire during history execution or for dying characters
	events = {
	}
	effect = {
		if = {
			limit = {
				NOR = {
					has_trait = humble
					has_trait = content
					has_trait = generous
				}
			}
			add_character_flag = {
				flag = make_suicide_available
				years = 5
			}
			set_variable = {
				name = rank_demoted
				value = yes
			}
		}
		update_embassies_effect = yes
		every_ally = {
			update_embassies_effect = yes
		}
		# Inform hostage havers
		if = {
			limit = {
				any_home_court_hostage = { }
			}
			save_scope_as = home_court
			every_home_court_hostage = {
				save_scope_as = hostage
				warden = {
					save_scope_as = warden
					trigger_event = { id = bp2_hostage_system.0410 days = 5 }
				}
			}
		}
		
		# Additional Flavor Title
		additional_flavor_check_effect = yes
		
		# ACH - Remove variables if they are going below certain ranks
		coronation_reset_realm_law_variable_effect = yes
		
		# ACH - if you are below king tier, remove all oaths
		if = {
			limit = {
				highest_held_title_tier < tier_kingdom
			}
			ach_remove_oaths_on_king_minus_effect = yes
		}
	}
}

# A character gains a vassal
# root = character gaining vassal
# scope:vassal = vassal being gained
# scope:old_liege = the previous liege of the vassal. Might be the null character (vassal used to be a non-ruler or independent), so make sure to use "exists" checks where relevant
on_vassal_gained = {
	events = {
		realm_maintenance.1000 # Notify vassals of new liege
	}
	effect = {
		#Nomadic Confederation members must be independent
		if = {
			limit = {
				scope:vassal = {
					is_confederation_member = yes
					scope:vassal.confederation = { has_leading_house = no }
				}
			}
			scope:vassal.confederation = {
				remove_confederation_member = scope:vassal
			}
		}
		# EP3 LANDLESS CANNOT HAVE VASSALS
		if = {
			limit = { has_any_landed_title_trigger = no }
			save_scope_as = new_liege
			if = {
				limit = { top_liege != this }
				create_title_and_vassal_change = {
					type = swear_fealty
					save_scope_as = change
					add_claim_on_loss = no
				}
				change_liege = {
					liege = scope:new_liege.liege
					change = scope:change
				}
				resolve_title_and_vassal_change = scope:change
			}
			else = {
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
					add_claim_on_loss = no
				}
				scope:vassal = {
					becomes_independent = { change = scope:change }
				}
				resolve_title_and_vassal_change = scope:change
			}
		}

		# Additional Flavor Title
		scope:vassal = {
			additional_flavor_check_effect = yes
		}

		# Destroy ceremonial titles of vassalized realms
		if = {
			limit = {
				# The necessary checks are baked into the effect below
				scope:vassal = {
					any_held_title = {
						OR = {
							exists = var:ceremonial_title
							exists = var:administrative_ui_special_title
						}
					}
				}
			}
			# TGP end the ceremonial liege
			tgp_destroy_ceremonial_throne_effect = yes
		}
	}
}

# A vassal changes their liege
# root = vassal changing their status
# scope:old_liege = the previous liege of the vassal
# It is also fired for rulers gaining independence, those can be identified by checking current liege
on_vassal_change = {
	effect = {
		### If a admin duke-tier character becomes the top-liege's direct vassal, ensure that they have a noble family title
		if = {
			limit = {
				government_allows = administrative
				is_house_head = yes
				trigger_if = {
					# This trigger is intentionally different from the similar effecy in on_title_gain up above. We want all direct vassals of the top liege to have a family title if they can
					limit = { government_has_flag = government_has_county_tier_noble_families }
					highest_held_title_tier >= tier_county
				}
				trigger_else = { highest_held_title_tier >= tier_duchy }
				liege = {
					top_liege = this
					government_allows = administrative
				}
				NOR = {
					any_held_title = { is_noble_family_title = yes }
					house = {
						any_house_member = {
							any_held_title = { is_noble_family_title = yes }
						}
					}
				}
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
			domicile ?= { set_up_domicile_estate_effect = yes }
		}
		### If an admin ruler becomes independent...
		if = {
			limit = {
				liege = root # I.e. root is independent
				government_has_flag = government_is_administrative
			}
			# And is not of a high enough rank, change government
			if = {
				limit = {
					administrative_tier_allows_independence = no
				}
				admin_change_government_effect = yes
			}
			# If they can be independent, make sure they have a noble family title
			else_if = {
				limit = {
					NOT = {
						any_held_title = {
							is_noble_family_title = yes
						}
					}
				}
				create_noble_family_effect = { GOVERNMENT_GIVER = this }
				domicile ?= { set_up_domicile_estate_effect = yes }
			}
		}
		### Independent Celestial rulers who become direct vassals of a top liege adopt Military obligations if they have title troops - Everyone else will default to Standard.
		if = {
			limit = {
				government_has_flag = government_is_celestial
				scope:old_liege = this # I.e. they were independent
				liege != this
				liege = top_liege
				primary_title = {
					any_title_maa_regiment = { count >= 1 }
				}
			}
			vassal_contract_set_obligation_level = { type = celestial_provinces level = 3 }
		}
		
		### If a minister becomes independent we destroy the ministry title
		if = {
			limit = {
				# independent title
				OR = {
					liege ?= { tgp_has_access_to_ministry_trigger = no }
					this = top_liege
				}

				any_held_title = {
					# has a ministry title that is not being swapped
					OR =  {
						this = title:e_minister_chancellor
						this = title:e_minister_censor
						this = title:e_minister_grand_marshal
						this = title:e_minister_of_personnel
						this = title:e_minister_of_revenue
						this = title:e_minister_of_rites
						this = title:e_minister_of_war
						this = title:e_minister_of_justice
						this = title:e_minister_of_works
					}
				}
			}
			every_held_title = {
				switch = {
					trigger = this
					title:e_minister_chancellor = {
						root = { destroy_title = title:e_minister_chancellor }
					}
					title:e_minister_censor = {
						root = { destroy_title = title:e_minister_censor }
					}
					title:e_minister_grand_marshal = {
						root = { destroy_title = title:e_minister_grand_marshal }
					}
					title:e_minister_of_personnel = {
						root = { destroy_title = title:e_minister_of_personnel }
					}
					title:e_minister_of_revenue = {
						root = { destroy_title = title:e_minister_of_revenue }
					}
					title:e_minister_of_rites = {
						root = { destroy_title = title:e_minister_of_rites }
					}
					title:e_minister_of_war = {
						root = { destroy_title = title:e_minister_of_war }
					}
					title:e_minister_of_justice = {
						root = { destroy_title = title:e_minister_of_justice }
					}
					title:e_minister_of_works = {
						root = { destroy_title = title:e_minister_of_works }
					}
				}
			}
		}
		# Make sure independent rulers and their vassals leave movements if they are part of the Dynastic Cycle
		if = {
			limit = {
				exists = top_participant_group:dynastic_cycle
				NOT = {
					has_title = title:h_china
				}
				OR = {
					liege = root
					liege.top_participant_group:dynastic_cycle ?= {
						participant_group_type = other_rulers
					}
				}
			}
			remove_variable = movement_member
			every_vassal_or_below = {
				remove_variable = movement_member
			}
			recalculate_participant_group = situation:dynastic_cycle
		}

		# Additional Flavor Title
		additional_flavor_check_effect = yes
		
		if = {
			limit = {
				# We have become independent!
				NOT = {
					scope:old_liege = root
				}
				liege = root
				government_has_flag = government_is_celestial
				highest_held_title_tier >= tier_kingdom
				primary_title = {
					has_custom_title_name = no # sanity check
				}
				OR = {
					tgp_capital_not_in_chinese_naming_region = no
					primary_title = title:h_china # failsafe, in case this somehow ends up here
				}
			}
			if = {
				limit = {
					is_ai = no
				}
				trigger_event = {
					id = tgp_dynastic_cycle.9000
					days = 1 # so the player gets to choose first
				}
			}
			else = {
				trigger_event = {
					id = tgp_dynastic_cycle.9000
					days = 2
				}
			}
		}
		if = {
			limit = {
				scope:old_liege = root
				NOT = {
					liege = root
				}
				primary_title ?= {
					has_custom_title_name = yes
				}
			}
			primary_title = {
				reset_title_name = yes
			}
		}
		
		# Destroy ceremonial titles of vassalized realms
		if = {
			limit = {
				# The necessary checks are baked into the effect below
				highest_held_title_tier >= tier_empire
				any_held_title = {
					OR = {
						exists = var:ceremonial_title
						exists = var:administrative_ui_special_title
					}
				}
			}
			# TGP end the ceremonial liege
			tgp_destroy_ceremonial_throne_effect = yes
			if = {
				limit = { has_trait = former_emperor }
				remove_trait = former_emperor
			}
		}
		
		# Bloc
		if = {
			# Let code trigger this if this is due to succession
			limit = {
				scope:old_liege ?= {
					is_alive = yes
				}
			}
			tgp_liege_change_bloc_management_effect = { VASSAL = root }
		}
	}
}

# A baron is found or created for a title, E.G., due to the player using the "Give to Low Noble" action
# No transfer type here; it's always a grant
# root = the baron
# scope:liege = the person who wanted them created
# scope:title = the barony
on_baron_found_or_created_for_title = {
	effect = {
		add_opinion = {
			target = scope:liege
			modifier = received_title_barony
		}
	}
}

# Called when realm capital changes. It can be manual move, loss in conquest, move after inheritance,
# move when granted a better title, becoming landed for the first time
# root: New realm capital barony
# scope:old_capital - previous capital barony. Can be empty, can be owned by someone else
on_realm_capital_change = {
	effect = {
		holder ?= {
			# Do we need to refresh their vizier's modifiers?
			if = {
				limit = { has_diarchy_parameter = diarchy_is_vizierate }
				diarch = {
					# What we're doing here is regenerating loc on the diarchs that might refer to a specific place which they no longer have access to.
					## This is gonna get real gross, folks, so my apologies.
					if = {
						limit = { has_character_modifier = vizier_extravagance_t1_treasure_modifier }
						remove_character_modifier = vizier_extravagance_t1_treasure_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t1_treasure_modifier
							desc = vizier_extravagance_t1_treasure_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t1_activities_modifier }
						remove_character_modifier = vizier_extravagance_t1_activities_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t1_activities_modifier
							desc = vizier_extravagance_t1_activities_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t1_property_modifier }
						remove_character_modifier = vizier_extravagance_t1_property_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t1_property_modifier
							desc = vizier_extravagance_t1_property_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t1_charity_modifier }
						remove_character_modifier = vizier_extravagance_t1_charity_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t1_charity_modifier
							desc = vizier_extravagance_t1_charity_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t2_treasure_modifier }
						remove_character_modifier = vizier_extravagance_t2_treasure_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t2_treasure_modifier
							desc = vizier_extravagance_t2_treasure_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t2_activities_modifier }
						remove_character_modifier = vizier_extravagance_t2_activities_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t2_activities_modifier
							desc = vizier_extravagance_t2_activities_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t2_property_modifier }
						remove_character_modifier = vizier_extravagance_t2_property_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t2_property_modifier
							desc = vizier_extravagance_t2_property_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t2_charity_modifier }
						remove_character_modifier = vizier_extravagance_t2_charity_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t2_charity_modifier
							desc = vizier_extravagance_t2_charity_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t3_treasure_modifier }
						remove_character_modifier = vizier_extravagance_t3_treasure_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t3_treasure_modifier
							desc = vizier_extravagance_t3_treasure_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t3_activities_modifier }
						remove_character_modifier = vizier_extravagance_t3_activities_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t3_activities_modifier
							desc = vizier_extravagance_t3_activities_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t3_property_modifier }
						remove_character_modifier = vizier_extravagance_t3_property_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t3_property_modifier
							desc = vizier_extravagance_t3_property_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t3_charity_modifier }
						remove_character_modifier = vizier_extravagance_t3_charity_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t3_charity_modifier
							desc = vizier_extravagance_t3_charity_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t4_treasure_modifier }
						remove_character_modifier = vizier_extravagance_t4_treasure_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t4_treasure_modifier
							desc = vizier_extravagance_t4_treasure_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t4_activities_modifier }
						remove_character_modifier = vizier_extravagance_t4_activities_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t4_activities_modifier
							desc = vizier_extravagance_t4_activities_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t4_property_modifier }
						remove_character_modifier = vizier_extravagance_t4_property_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t4_property_modifier
							desc = vizier_extravagance_t4_property_modifier_custom_desc.on_action
						}
					}
					if = {
						limit = { has_character_modifier = vizier_extravagance_t4_charity_modifier }
						remove_character_modifier = vizier_extravagance_t4_charity_modifier
						add_character_modifier = {
							modifier = vizier_extravagance_t4_charity_modifier
							desc = vizier_extravagance_t4_charity_modifier_custom_desc.on_action
						}
					}
				}
			}
		}
	}
}

# Primary Title has changed
# root = character
# scope:previous_title = previous primary title held by character
# Not called if new primary title is the same as the previous primary title
on_primary_title_change = {
}
