﻿#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
# 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 = {
				scope:title.tier = tier_county
			}

			###########
			# MPO - Nomad Title Handling
			###########
			if = {
				limit = {
					scope:title = {
						is_nomad_title = yes
						exists = title_domicile
					}
				}
				scope:title = {
					if = {
						limit = {
							root = {
								any_held_title = {
									is_nomad_title = yes
									exists = title_domicile
									NOT = { this = scope:title }
								}
							}
						}
						if = {
							limit = {
								root = {
									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 = {
								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 = {
										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 = {
										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 = {
										is_nomad_title = yes
										exists = title_domicile
										NOT = { this = scope:title }
									}
									save_scope_as = old_nomad_title_to_clean_up
								}
							}
							title_domicile = {
								if = {
									limit = {
										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
							NOT = {
								exists = previous_holder
							}
						}
						if = {
							limit = {
								root = {
									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
								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
								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
								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
								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
								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
								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
								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 >= 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
								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
								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
								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
								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
								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
							}
						}
					}
				}

				###########
				# 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 ?= scope:title.culture
						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 ?= scope:title.culture
							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 = {
			# Hegemonies cannot have Ceremonial Regents
			if = {
				limit = {
					exists = this
					tgp_realm_has_ceremonial_liege_trigger = yes
					scope:title.tier = tier_hegemony
				}
				tgp_destroy_ceremonial_throne_effect = yes
			}
			# 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
			}
			# 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
							is_valid_celestial_dynasty = no
						}
						situation:dynastic_cycle = {
							situation_top_sub_region = {
								change_phase = { phase = situation_dynastic_cycle_phase_instability_conquest }
							}
						}
					}
					# When inheriting China - Move towards instability/chaotic phase
					if = {
						limit = {
							root.house != scope:previous_holder.house
							months_from_game_start > 0 # 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 = {
							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
						}
					}
					# 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
				if = {
					limit = {
						primary_title ?= {
							OR = {
								this = title:e_mongol_empire
								AND = {
									exists = global_var:greatest_of_khans_title
									this = global_var:greatest_of_khans_title
								}
							}
						}
						scope:title = {
							NOR = {
								this = title:e_mongol_empire
								AND = {
									exists = global_var:greatest_of_khans_title
									this = global_var:greatest_of_khans_title
								}
							}
						}
						is_ai = yes
					}
					destroy_title = scope:title
				}
				# 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
					}
				}
				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
				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
				}
				NOT = { 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
					government_has_flag = government_is_feudal
				}
			}
			# Note: anything that should be run after creation can go in on_noble_family_title_created
			if = {
				limit = {
					is_house_head = yes
					trigger_if = {
						limit = {
							OR = {
								government_has_flag = government_is_japan_feudal
								government_has_flag = government_is_feudal
							}
						}
						highest_held_title_tier >= tier_county
					}
					trigger_else_if = {
						# To avoid noble family spam, we only create family titles for county tier if that is also the primary governor tier.
						limit = { main_administrative_tier = tier_county }
						highest_held_title_tier >= tier_county
					}
					trigger_else = { highest_held_title_tier >= tier_duchy }
					liege = {
						OR = {
							government_allows = administrative
							government_has_flag = government_is_japan_feudal
							government_has_flag = government_is_feudal
						}
					}
					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 }
			}
			# 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
				}
				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 = {
					limit = {
						culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
						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 }
					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_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
				if = {
					limit = { exists = root.var:ascended_throne_reason }
					root.var:ascended_throne_reason = { save_scope_as = real_transfer_type }
					root = { remove_variable = ascended_throne_reason }
				}
				else = {
					scope:transfer_type = { save_scope_as = real_transfer_type }
				}
				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
				house.house_head = {
					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.
		fp1_major_decisions.1011	# Harald Tanglehair becomes Harald Fairhair.
		fp1_major_decisions.1012	# If Norway has just been created for the first time, flag that.
		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
	}
}
