﻿reclaim_britannia_decision_effect = {
	every_sub_realm_county = {
		limit = {
			NOR = {
				culture = { has_cultural_pillar = heritage_goidelic }
				culture = { has_cultural_pillar = heritage_brythonic }
			}
			title_province = { geographical_region = world_europe_west_britannia }
		}
		custom = british_isles.0001.custom
		add_county_modifier = {
			modifier = reclaim_britannia_culture_modifier
			days = 3650
		}
	}
	capital_county = {
		set_county_culture = root.culture
	}
	if = {
		limit = {
			culture = { has_cultural_pillar = heritage_goidelic }
		}
		give_nickname = nick_the_tuatha_de_danann
	}
	else = {
		give_nickname = nick_pendragon
	}
}

restore_the_danelaw_effect = {
	hidden_effect = { #Should shift all dejure of all Empires owned at the time.
		title:k_england = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:k_danelaw
			}
		}
		title:k_danelaw = {
			set_de_jure_liege_title = title:k_england.de_jure_liege
		}
	}
	give_nickname = nick_the_dane
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	title:k_danelaw = {
		change_title_holder = {
			holder = root
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	title:k_danelaw = {
		add_title_law = scandinavian_elective_succession_law
	}

	if = {
		limit = {
			NOT = { highest_held_title_tier = tier_empire }
			NOT = { has_primary_title = title:k_danelaw }
		}
		set_primary_title_to = title:k_danelaw
	}

	#Destroy england
	destroy_title = title:k_england
}

create_holy_order_effect = {
	hidden_effect = {
		if = { # set up currencies for the HO leader, has to be saved as scope:leader
			limit = {
				exists = scope:leader
			}
			scope:leader = {
				add_gold = 100 #So that they have some money to lend out
				add_piety_level = 2
				add_gold = holy_order_starting_gold
				every_courtier = {
					add_trait = order_member
				}
			}
			
			# send messages
			send_interface_toast = {
				type = holy_order_founded_message
				desc = i_created_holy_order_message
				left_icon = scope:leader
				right_icon = scope:new_holy_order.title
			}
		}
		# send messages to relevant characters that HO got created
		save_scope_as = holy_order_creator
		every_player = {
			limit = {
				faith = scope:holy_order_creator.faith
				NOT = { this = scope:holy_order_creator }
			}
			send_interface_message = {
				type = holy_order_founded_message
				desc = someone_created_holy_order_message
				left_icon = scope:holy_order_creator
				right_icon = scope:new_holy_order.title
			}
		}
		every_neighboring_top_liege_realm_owner = {
			limit = {
				NOT = { faith = scope:holy_order_creator.faith }
			}
			send_interface_message = {
				type = enemy_holy_order_founded_message
				desc = other_faith_neighbor_created_holy_order_message
				left_icon = scope:holy_order_creator
				right_icon = scope:new_holy_order.title
			}
		}
		# get the achievements for creating a HO
		add_achievement_flag_effect = { FLAG = achievement_non_nobis_domine_flag }
		if = {
			limit = { exists = global_var:started_give_a_dog_a_bone_achievement }
			add_achievement_global_variable_effect = {
				VARIABLE = holy_order_give_a_dog_a_bone_achievement
				VALUE = yes
			}
		}
	}
	# gain opinion with your HoF, if you have one and it's not you
	if = {
		limit = {
			exists = faith.religious_head
			NOT = { faith.religious_head = root }
		}
		faith.religious_head = {
			add_opinion = {
				target = root
				modifier = founded_holy_order_opinion
			}
		}
	}
	# get a nice modifier
	if = {
		limit = {
			NOT = { has_character_modifier = founded_holy_order_modifier }
		}
		add_character_modifier = {
			modifier = founded_holy_order_modifier
			years = 5
		}
	}
	# and boost the fervor
	faith = {
		change_fervor = {
			value = 10
			desc = fervor_gain_holy_order_founded
		}
	}
}

embrace_english_culture_effect = {
	# Scopes saves for localization
	root = {
		save_scope_as = embracer
	}

	# Prestige Bonus
	add_prestige = medium_prestige_gain

	#Convert your, and your whole family's, culture
	set_culture = culture:english
	if = {
		limit = { any_spouse = { is_playable_character = no } }
		every_spouse = {
			limit = {
				is_playable_character = no
				OR = {
					culture = culture:norman
					culture = culture:french
				}
			}
			set_culture = culture:english
		}
	}
	if = {
		limit = {
			any_close_family_member = {
				is_playable_character = no
				NOT = { is_spouse_of = root }
			}
		}
		every_close_family_member = {
			limit = {
				is_playable_character = no
				NOT = { is_spouse_of = root }
			}
			custom = all_unlanded_family_members
			set_culture = culture:english
		}
	}
	if = {
		limit = {
			any_child = {
				NOT = { is_spouse_of = root }
			}
		}
		every_child = {
			limit = {
				NOT = { is_spouse_of = root }
			}
			custom = all_children_custom
			set_culture = culture:english
		}
	}

	#Convert appropriate vassals, and their family
	if = {
		limit = {
			any_vassal_or_below = {
				is_ai = yes
				OR = {
					culture = culture:norman
					culture = culture:french
				}
				primary_title = {
					OR = {
						de_jure_liege = title:k_england
						de_jure_liege.de_jure_liege = title:k_england
						de_jure_liege.de_jure_liege.de_jure_liege = title:k_england
					}
				}
			}
		}
		every_vassal_or_below = {
			custom = embrace_english_culture_vassals_custom
			limit = {
				is_ai = yes
				OR = {
					culture = culture:norman
					culture = culture:french
				}
				primary_title = {
					OR = {
						de_jure_liege = title:k_england
						de_jure_liege.de_jure_liege = title:k_england
						de_jure_liege.de_jure_liege.de_jure_liege = title:k_england
					}
				}
			}
			set_culture = culture:english
			hidden_effect = {
				if = {
					limit = { any_spouse = { is_playable_character = no } }
					every_spouse = {
						limit = {
							is_playable_character = no
							OR = {
								culture = culture:norman
								culture = culture:french
							}
						}
						set_culture = culture:english
					}
				}
				if = {
					limit = {
						any_close_family_member = {
							is_playable_character = no
							NOT = { is_spouse_of = prev }
						}
					}
					every_close_family_member = {
						limit = {
							is_playable_character = no
							NOT = { is_spouse_of = prev }
						}
						custom = all_unlanded_family_members
						set_culture = culture:english
					}
				}
				if = {
					limit = {
						any_child = {
							NOT = { is_spouse_of = prev }
						}
					}
					every_child = {
						limit = {
							NOT = { is_spouse_of = prev }
						}
						custom = all_children_custom
						set_culture = culture:english
					}
				}
				if = {
					limit = {
						any_vassal_or_below = {
							is_ai = yes
							OR = {
								culture = culture:norman
								culture = culture:french
							}
							primary_title = {
								OR = {
									de_jure_liege = title:k_england
									de_jure_liege.de_jure_liege = title:k_england
									de_jure_liege.de_jure_liege.de_jure_liege = title:k_england
								}
							}
						}
					}
					every_vassal_or_below = {
						custom = embrace_english_culture_vassals_custom
						limit = {
							is_ai = yes
							OR = {
								culture = culture:norman
								culture = culture:french
							}
							primary_title = {
								OR = {
									de_jure_liege = title:k_england
									de_jure_liege.de_jure_liege = title:k_england
									de_jure_liege.de_jure_liege.de_jure_liege = title:k_england
								}
							}
						}
						set_culture = culture:english
						hidden_effect = {
							if = {
								limit = { any_spouse = { is_playable_character = no } }
								every_spouse = {
									limit = {
										is_playable_character = no
										OR = {
											culture = culture:norman
											culture = culture:french
										}
									}
									set_culture = culture:english
								}
							}
							if = {
								limit = {
									any_close_family_member = {
										is_playable_character = no
										NOT = { is_spouse_of = prev }
									}
								}
								every_close_family_member = {
									limit = {
										is_playable_character = no
										NOT = { is_spouse_of = prev }
									}
									custom = all_unlanded_family_members
									set_culture = culture:english
								}
							}
							if = {
								limit = {
									any_child = {
										NOT = { is_spouse_of = prev }
									}
								}
								every_child = {
									limit = {
										NOT = { is_spouse_of = prev }
									}
									custom = all_children_custom
									set_culture = culture:english
								}
							}
						}
					}
				}
			}
		}
	}

	#Flip Counties
	every_county_in_region = {
		region = custom_england
		custom = embrace_english_culture_counties_custom
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
		}
		random = {
			chance = 40
			set_county_culture = culture:english
		}
	}

	culture:english = {
		custom_tooltip = decision_embrace_english_culture_get_innovations
		hidden_effect = {
			reset_culture_creation_date = yes
			get_all_innovations_from = culture:norman
			get_all_innovations_from = culture:anglo_saxon
		}
	}

	remove_global_variable = harrying_of_the_north
	every_player = {
		limit = {
			capital_province = {
				geographical_region = world_europe_west_britannia
			}
		}
		trigger_event = ep3_story_cycle_harrying.0004
	}
	global_var:harrying_of_the_north = { end_story = yes }
}

form_portugal_decision_effects = {
	#Create the title and set proper de jure
	hidden_effect = {
		title:d_porto = {
			save_scope_as = title_d_porto
			set_de_jure_liege_title = title:k_portugal
		}
		title:d_coimbra = {
			save_scope_as = title_d_coimbra
			set_de_jure_liege_title = title:k_portugal
		}
		title:d_beja = {
			save_scope_as = title_d_beja
			set_de_jure_liege_title = title:k_portugal
		}
		title:d_algarve = {
			save_scope_as = title_d_algarve
			set_de_jure_liege_title = title:k_portugal
		}
		title:k_portugal = {
			save_scope_as = title_k_portugal
		}
	}
	custom_tooltip = form_portugal_decision_effects_de_jure_tt

	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:k_portugal = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
		save_scope_as = title_k_portugal
	}
	resolve_title_and_vassal_change = scope:title_change

	if = {
		limit = {
			NOR = {
				primary_title = { this = title:k_portugal }
				highest_held_title_tier = tier_empire
			}
		}
		set_primary_title_to = title:k_portugal
	}

	hidden_effect = {
		if = {
			limit = { has_global_variable = fp2_struggle_compromise_ending }
			title:k_portugal = { fp2_struggle_compromise_create_new_empire_effect = yes }
		}
	}

	#Covert self & court to portuguese culture
	if = {
		limit = { NOT = { has_culture = culture:portuguese } }

		every_courtier = {
			limit = { has_same_culture_as = scope:portugal_former }
			add_to_list = convert_list
		}
		#transfer over innovations from previous culture. 
		culture:portuguese = {
			hidden_effect = { reset_culture_creation_date = yes }
			get_all_innovations_from = scope:portugal_former.culture
		}
		set_culture = culture:portuguese

		every_in_list = {
			list = convert_list
			custom = portguese_convert_list #This says "every subject in the realm" even though vassals and their courts only covert later, through event
			set_culture = culture:portuguese
		}
	}

	# Convert Iberian counties to Portugese culture
	every_sub_realm_county = {
		limit = {
			tier = tier_county
			culture = { has_cultural_pillar = heritage_iberian }
			OR = {
				de_jure_liege = title:d_porto
				de_jure_liege = title:d_coimbra
				de_jure_liege = title:d_beja
				de_jure_liege = title:d_algarve
			}
		}
		custom = portugese_convert_iberian_counties
		set_county_culture = culture:portuguese
	}
	every_sub_realm_county = {
		limit = {
			tier = tier_county
			NOT = { culture = { has_cultural_pillar = heritage_iberian } }
			OR = {
				de_jure_liege = title:d_porto
				de_jure_liege = title:d_coimbra
				de_jure_liege = title:d_beja
				de_jure_liege = title:d_algarve
			}
		}
		custom = portugese_convert_non_iberian_counties
		random = {
			chance = 50
			set_county_culture = culture:portuguese
		}
	}

	#Innovations
	culture:portuguese = {
		add_random_innovation = culture_group_military
		add_random_innovation = culture_group_civic
	}

	#Prestige
	add_prestige = massive_prestige_gain	
}


declare_canarias_decision_effects = {
	#Create the title and set proper de jure
	title:d_canarias = {
		save_scope_as = d_canarias
		set_de_jure_liege_title = title:k_canarias
	}
	title:k_canarias = {
		save_scope_as = title_k_canarias
	}

	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:k_canarias = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
		save_scope_as = title_k_canarias
	}
	resolve_title_and_vassal_change = scope:title_change

	if = {
		limit = {
			NOR = {
				primary_title = { this = title:k_canarias }
				highest_held_title_tier = tier_empire
			}
		}
		set_primary_title_to = title:k_canarias
	}

	#Prestige
	add_prestige = massive_prestige_gain	
}
	


unite_the_spanish_thrones_decision_effects = {
	#Prestige
	add_prestige = massive_prestige_gain

	#Laws
	if = {
		limit = { has_realm_law = crown_authority_0 }
		add_realm_law_skip_effects = crown_authority_2
	}
	else_if = {
		limit = {
			OR = {
				has_realm_law = crown_authority_1
				has_realm_law = crown_authority_2
			}
		}
		add_realm_law_skip_effects = crown_authority_3
	}
	
	# Coat of Arms
	if = {
		limit = { scope:spain_uniter.primary_title = title:k_castille }
		primary_title = { set_coa = k_castille_leon }
	}
	else_if = {
		limit = { scope:spain_uniter.primary_title = title:k_leon }
		primary_title = { set_coa = k_leon_castille }
	}

	#Handle other Kingdom titles
	title:k_castille = { add_to_list = spanish_kingdoms }
	title:k_leon = { add_to_list = spanish_kingdoms }
	title:k_spanish_galicia = { add_to_list = spanish_kingdoms }
	title:k_portugal = { add_to_list = spanish_kingdoms }
	title:k_aragon = { add_to_list = spanish_kingdoms }
	title:k_navarra = { add_to_list = spanish_kingdoms }
	title:k_andalusia = { add_to_list = spanish_kingdoms }
	title:k_badajoz = { add_to_list = spanish_kingdoms }
	title:k_valencia = { add_to_list = spanish_kingdoms }
	title:k_toledo = { add_to_list = spanish_kingdoms }

	#[FF]
	#[StA]
	if = {
		limit = { StA_is_loaded = yes }
		StA_unite_the_spanish_thrones_decision_effects = yes
	}
	else = {
		hidden_effect = {
			every_in_list = {
				list = spanish_kingdoms
				limit = {
					NOT = { this = scope:spain_uniter.primary_title }
					scope:spain_uniter = { completely_controls = prev }
				}

				#De jure shift all held spanish titles into your primary title
				every_in_de_jure_hierarchy = {
					limit = { tier = tier_duchy }
					set_de_jure_liege_title = scope:spain_uniter.primary_title
				}

				#Destroys them all!
				scope:spain_uniter = { destroy_title = prev }
			}
		}
		custom_tooltip = unite_the_spanish_thrones_decision_titles_tt
	}
	#[/StA]
	#[/FF]
}

unite_africa_decision_effects = {
	#Prestige
	add_prestige = massive_prestige_gain

	#Religion bonuses
	if = {
		limit = {
			faith = {
				OR = {
					religion_tag = islam_religion
					religion_tag = west_african_roog_religion
					religion_tag = west_african_religion
					religion_tag = west_african_bori_religion
					this = faith:coptic

				}
			}
		}
		add_piety = massive_piety_gain
		faith = {
			change_fervor = {
				value = medium_fervor_gain
				desc = fervor_gain_united_africa
			}
		}

		if = {
			limit = {
				any_vassal_or_below = {
					NOT = { faith = scope:africa_uniter.faith }
					is_ai = yes
					exists = capital_province
					capital_province = { geographical_region = world_africa }
				}
			}
			every_vassal_or_below = {
				custom = every_non_root_faith_vassal_africa
				limit = {
					NOT = { faith = scope:africa_uniter.faith }
					is_ai = yes
					exists = capital_province
					capital_province = { geographical_region = world_africa }
				}
				random = {
					chance = 33
					set_character_faith = scope:africa_uniter.faith
				}
			}
		}

		if = {
			limit = {
				any_realm_province = {
					NOT = { faith = scope:africa_uniter.faith }
					geographical_region = world_africa
				}
			}
			every_realm_province = {
				limit = {
					NOT = { faith = scope:africa_uniter.faith }
					geographical_region = world_africa
				}
				county = { add_to_list = every_realm_county }
			}

			every_in_list = {
				list = every_realm_county
				custom = every_non_root_faith_county_africa
				random = {
					chance = 33
					custom_tooltip = every_non_africa_uniter_faith_convert
					hidden_effect = { set_county_faith = scope:africa_uniter.faith }
				}
			}
		}
	}
}

avenge_the_battle_of_tours_decision_effects = {
		
	give_nickname = nick_the_avenger
	add_character_modifier = {
		modifier = avenger_of_the_martyrs
	}

	add_prestige = massive_prestige_gain

	#Religious bonuses
	add_piety = massive_piety_gain
	faith = {
		change_fervor = {
			value = medium_fervor_gain
			desc = fervor_gain_avenged_battle_of_tours
		}
	}

	add_achievement_flag_effect = { FLAG = achievement_al_andalus_flag }

	if = {
		limit = {
			any_vassal_or_below = {
				NOT = { faith = scope:avenger.faith }
				is_ai = yes
				exists = capital_province
				capital_province = { geographical_region = world_europe_west }
			}
		}
		every_vassal_or_below = {
			custom = every_non_root_faith_vassal_west_europe
			limit = {
				NOT = { faith = scope:avenger.faith }
				is_ai = yes
				exists = capital_province
				capital_province = { geographical_region = world_europe_west }
			}
			random = {
				chance = 33
				set_character_faith = scope:avenger.faith
			}
		}
	}

	if = {
		limit = {
			any_realm_province = {
				NOT = { faith = scope:avenger.faith }
				geographical_region = world_europe_west
			}
		}
		every_realm_province = {
			limit = {
				NOT = { faith = scope:avenger.faith }
				geographical_region = world_europe_west
			}
			county = {
				add_to_list = every_realm_county
			}	
		}

		every_in_list = {
			list = every_realm_county
			custom = every_non_root_faith_county_west_europe		
			random = {
				chance = 33
				custom_tooltip = every_non_avenger_faith_convert
				hidden_effect = { set_county_faith = scope:avenger.faith }
			}
		}
	}

	#Christian penalty
	if = {
		limit = { exists = scope:frankish_christian_faith }
		scope:frankish_christian_faith = {
			change_fervor = {
				value = medium_fervor_loss
				desc = fervor_loss_avenged_battle_of_tours
			}
		}
	}

	# Rattach south of France to the e_hispania
	title:e_france = {
		every_in_de_jure_hierarchy = {
			limit = {
				tier = tier_kingdom
				any_in_de_jure_hierarchy = {
					tier = tier_county
					title_province = { geographical_region = custom_south_francia }
				}
			}
			set_de_jure_liege_title = title:e_spain
		}
	}
}

unite_the_western_slavs_decision_effects = {
	#Prestige
	add_prestige = massive_prestige_gain

	#Innovations
	culture = {
		add_random_innovation = culture_group_military
		add_random_innovation = culture_group_civic
	}

	#Laws
	if = {
		limit = { has_realm_law = crown_authority_0 }
		add_realm_law_skip_effects = crown_authority_2
	}
	else_if = {
		limit = {
			OR = {
				has_realm_law = crown_authority_1
				has_realm_law = crown_authority_2
			}
		}
		add_realm_law_skip_effects = crown_authority_3
	}

	#Handle de jure drift & destructions of other kingdom titles
	title:k_poland = { add_to_list = west_slavic_kingdoms }
	title:k_pomerania = { add_to_list = west_slavic_kingdoms }
	title:k_bohemia = { add_to_list = west_slavic_kingdoms }
	title:k_sorbia = { add_to_list = west_slavic_kingdoms }
	title:k_moravia = { add_to_list = west_slavic_kingdoms }

	if = {
		limit = { highest_held_title_tier = tier_kingdom }
		custom_tooltip = unite_the_western_slavs_decision_titles_tt
	}
	custom_tooltip = unite_the_western_slavs_decision_titles_empire_tt
	# If Saxony has been created, shift everything back into Polabia
	if = {
		limit = {
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_formed_saxony
			}
		}
		title:k_saxony = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:k_sorbia
			}
		}
	}
	hidden_effect = {
		scope:western_slav_uniter = {
			#If king-tier, give them the West-Slavia title
			if = {
				limit = { highest_held_title_tier = tier_kingdom }
				create_title_and_vassal_change = {
					type = created
					save_scope_as = title_change
					add_claim_on_loss = no
				}
				title:e_west_slavia = {
					change_title_holder = {
						holder = scope:western_slav_uniter
						change = scope:title_change
					}
				}
				resolve_title_and_vassal_change = scope:title_change
			}
			# Drift kingdoms to empire
			every_in_list = {
				list = west_slavic_kingdoms
				set_de_jure_liege_title = scope:western_slav_uniter.primary_title
			}
		}
	}	
}

unite_the_southern_slavs_decision_effects = {
	#Prestige
	add_prestige = massive_prestige_gain

	#Innovations
	culture = {
		add_random_innovation = culture_group_military
		add_random_innovation = culture_group_civic
	}

	#Handle de jure drift & destructions of other kingdom titles
	title:k_croatia = { add_to_list = south_slavic_kingdoms }
	title:k_serbia = { add_to_list = south_slavic_kingdoms }
	title:k_bulgaria = { add_to_list = south_slavic_kingdoms }
	title:k_dacia = { add_to_list = south_slavic_kingdoms }
	title:k_moldavia = { add_to_list = south_slavic_kingdoms }

	custom_tooltip = unite_the_southern_slavs_decision_titles_tt
	hidden_effect = {
		every_in_list = {
			list = south_slavic_kingdoms
			limit = {
				NOT = { this = scope:southern_slav_uniter.primary_title }
				scope:southern_slav_uniter = { completely_controls = prev }
			}

			#De jure shift all held west slavic titles into your primary title
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = scope:southern_slav_uniter.primary_title
			}

			#Destroys them all!
			scope:southern_slav_uniter = { destroy_title = prev }
		}
	}
}

unite_the_slavs_decision_effects = {
	#Prestige
	add_prestige = massive_prestige_gain

	#Innovations
	custom_tooltip = unite_the_slavs_decision_effects_innovations_tt
	hidden_effect = {
		#Add all Slavic cultures in the realm in a list
		every_vassal_or_below = {
			limit = {
				OR = {
					culture = { has_cultural_pillar = heritage_west_slavic }
					culture = { has_cultural_pillar = heritage_east_slavic }
					culture = { has_cultural_pillar = heritage_south_slavic }
				}
				save_temporary_scope_as = slavic_vassal
				NOT = {
					any_in_list = {
						list = slavic_cultures
						this = scope:slavic_vassal.culture
					}
				}
			}
			culture = {
				add_to_list = slavic_cultures
			}
		}
		#Add innovations from 3 random cultures
		every_in_list = {
			list = slavic_cultures
			save_scope_as = culture_getting_innovations

			set_while_counter_variable_effect = yes
			while = {
				limit = { var:while_counter < 3 }
				random_in_list = {
					limit = { scope:culture_getting_innovations = { can_get_innovation_from = prev } }
					list = slavic_cultures
					scope:culture_getting_innovations = { get_random_innovation_from = prev }
				}
				increase_while_counter_variable_effect = yes
			}
			remove_while_counter_variable_effect = yes
		}
	}

	#Create the title and make it primary
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_slavia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	set_primary_title_to = title:e_slavia
	title:e_slavia = { save_scope_as = slavia } #For localisation

	#Destroy the other three empires if I have them
	if = {
		limit = { has_title = title:e_russia }
		destroy_title = title:e_russia
	}
	if = {
		limit = { has_title = title:e_wendish_empire }
		destroy_title = title:e_wendish_empire
	}
	if = {
		limit = { has_title = title:e_carpathia }
		destroy_title = title:e_carpathia
	}

	###Handle de jure drift and empire destruction###

	#First: add all the kingdoms
	title:k_poland = { add_to_list = slavic_kingdoms }
	title:k_pomerania = { add_to_list = slavic_kingdoms }
	title:k_bohemia = { add_to_list = slavic_kingdoms }
	title:k_moravia = { add_to_list = slavic_kingdoms }
	title:k_croatia = { add_to_list = slavic_kingdoms }
	title:k_serbia = { add_to_list = slavic_kingdoms }
	title:k_bulgaria = { add_to_list = slavic_kingdoms }
	title:k_dacia = { add_to_list = slavic_kingdoms }
	title:k_moldavia = { add_to_list = slavic_kingdoms }
	title:k_white_rus = { add_to_list = slavic_kingdoms }
	title:k_galicia-volhynia = { add_to_list = slavic_kingdoms }
	title:k_ruthenia = { add_to_list = slavic_kingdoms }
	title:k_novgorod = { add_to_list = slavic_kingdoms }
	title:k_opolye = { add_to_list = slavic_kingdoms }
	title:k_hungary = { add_to_list = slavic_kingdoms }

	# Inform players of the de jure drift
	title:e_slavia = { custom_tooltip = unite_the_slavs_decision_titles_tt } #Scope change for tooltip formatting.
	
	if = {
		limit = { has_dlc_feature = legends }
		create_legend_seed = {
			type = legitimizing
			quality = illustrious
			chronicle = new_title
			properties = {
				title = title:e_slavia
				founder = root
			}
		}
	}
	# Actually do the de jure drift
	hidden_effect = {
		every_in_list = {
			list = slavic_kingdoms

			set_de_jure_liege_title = scope:slav_uniter.primary_title
		}
	}
}

unite_india_decision_effect = {
	#Create the title and make it primary
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_india = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change
	set_primary_title_to = title:e_india

	title:e_rajastan = { add_to_list = indian_empire }
	title:e_deccan = { add_to_list = indian_empire }
	title:e_bengal = { add_to_list = indian_empire }

	every_in_list = {
		list = indian_empire
		every_in_de_jure_hierarchy = {
			limit = {
				tier = tier_kingdom
			}
			set_de_jure_liege_title = title:e_india
		}
	}

	hidden_effect = {
		if = {
			limit = { has_title = title:e_rajastan }
			destroy_title = title:e_rajastan
		}
		if = {
			limit = { has_title = title:e_deccan }
			destroy_title = title:e_deccan
		}
		if = {
			limit = { has_title = title:e_bengal }
			destroy_title = title:e_bengal
		}
	}
}

become_saoshyant_decision_effect = {
	set_nickname_effect = { NICKNAME = nick_the_saoshyant }
	add_trait = saoshyant
	custom_tooltip = become_saoshyant_decision_decendant_trait_tooltip
	dynasty = {
		add_dynasty_modifier = {
			modifier = resurging_saoshyant
			years = 25
		}
	}
	if = {
		limit = {
			any_held_title = {
				tier = tier_county
				NOT = { faith = root.faith }
			}
		}
		every_held_title = {
			custom = all_held_counties
			limit = {
				tier = tier_county
				NOT = { faith = root.faith }
			}
			random = {
				chance = 35
				set_county_faith = root.faith
			}
		}
	}
	faith = {
		change_fervor = {
			value = 15
			desc = fervor_gain_saoshyant
		}
	}
}

build_grand_church_decision_effect = {
	#faith = { activate_holy_site = segrada_familia }
	if = {
		limit = {
			prestige_level < 5
		}
		add_prestige_level = 1
	}
}

accept_cannibalism_decision_effect = {
	custom_tooltip = faith_accept_cannibalism
	root.faith = {
		set_variable = {
			name = accepted_cannibalism
			value = yes
		}
	}
	root = {
		if = {
			limit = {
				any_spouse = {
					is_cannibal_trigger = no
					is_ai = yes
				}
			}
			every_spouse = {
				limit = {
					is_cannibal_trigger = no
					is_ai = yes
				}
				add_trait = cannibal
			}
		}
		if = {
			limit = {
				any_close_family_member = {
					is_cannibal_trigger = no
					is_playable_character = no
					is_ai = yes
				}
			}
			every_close_family_member = {
				limit = {
					is_cannibal_trigger = no
					is_playable_character = no
					is_ai = yes
				}
				custom = all_my_family_members
				add_trait = cannibal
			}
		}
		if = {
			limit = {
				any_vassal = {
					is_cannibal_trigger = no
					is_ai = yes
					dread_modified_ai_boldness = { #all vassals that are intimidated
						dreaded_character = root
						value <= -50
					}
				}
			}
			every_vassal = {
				limit = {
					is_cannibal_trigger = no
					is_ai = yes
					dread_modified_ai_boldness = {
						dreaded_character = root
						value <= -50
					}
				}
				custom = all_my_intimidated_vassals
				add_trait = cannibal
			}
		}
	}
	give_nickname = nick_the_devourer
}

found_university_decision_event_effect = {
	if = {
		limit = {
			OR = {
				has_bad_nickname = yes
				has_any_nickname = no
			}
		}
		give_nickname = nick_the_scholar
	}

	hidden_effect = {
		#Event that gives you highly skilled courtier
		trigger_event = {
			id = major_decisions.2011
			days = { 50 365 }
		}
	}
}

found_university_decision_effect = {
	found_university_decision_event_effect = yes

	if = {
		limit = {
			NOT = { has_variable = university }
		}
		show_as_tooltip = {
			every_held_title = {
				custom = decision_found_university_custom
				limit = { tier = tier_barony }
				title_capital_county.title_province = {
					add_special_building_slot = generic_university
				}
			}
		}
	}
}

lunatic_building_decision_effect = {
	capital_province = {
		set_variable = { #To unlock the building
			name = variable_greenhouse
			value = yes
		}
		add_special_building_slot = special_greenhouse_01
		add_special_building = special_greenhouse_01
		#add_province_modifier = extra_building_slot
		#add_building = special_greenhouse_01
	}
	
	give_nickname = nick_the_glass
}

found_witch_coven_decision_effects = {
	save_scope_as = founder
	custom_tooltip = found_witch_coven_decision_effects.children.tt
	custom_tooltip = found_witch_coven_decision_effects.convert.tt
	custom_tooltip = found_witch_coven_decision_effects.grand_rite.tt
	house = {
		add_house_modifier = {
			modifier = witch_coven
		}
	}
	# Check for the Volva achievement.
	if = {
		limit = {
			# DLC check.
			has_fp1_dlc_trigger = yes
			fp1_achievement_religious_norse_trigger = yes
		}
		add_achievement_flag_effect = { FLAG = volva_achievement_flag }
	}
}

convert_whole_realm_to_feudalism_effect = {
	#TOOLTIP INFO
	#PLAYER EFFECTS
	##Liege has clan? Convert to clan!
	if = {
		limit = {
			is_independent_ruler = no
			OR = {
				liege = { government_has_flag = government_is_clan }
				AND = {
					liege = { government_has_flag = government_is_tribal }
					religion = religion:islam_religion
				}
			}
		}
		change_government = clan_government
	}
	##Liege has feudal? Convert to feudal!
	else_if = {
		limit = {
			is_independent_ruler = no
			OR = {
				liege = { government_has_flag = government_is_feudal }
				AND = {
					liege = { government_has_flag = government_is_tribal }
					NOT = { religion = religion:islam_religion }
				}
			}
		}
		change_government = feudal_government
	}
	##You're an independent Muslim? Convert to clan!
	else_if = {
		limit = {
			is_independent_ruler = yes
			faith = { religion_tag = islam_religion }
		}
		change_government = clan_government
	}
	##You're an independent anything else? Convert to feudal!
	else = { change_government = feudal_government }
	if = {
		limit = {
			government_has_flag = government_is_tribal
		}
		##General
		custom_tooltip = all_holding_will_change
		##Liege has clan? Convert to clan!
		if = {
			limit = {
				is_independent_ruler = no
				liege = { government_has_flag = government_is_clan }
			}
			custom_tooltip = clan_liege_vassals_will_convert
		}
		#Liege has feudal? Convert to feudal!
		if = {
			limit = {
				is_independent_ruler = no
				liege = { government_has_flag = government_is_feudal }
			}
			custom_tooltip = feudal_liege_vassals_will_convert
		}
		##If I am Islamic, we will become clan.
		if = {
			limit = {
				is_independent_ruler = yes
				faith = { religion_tag = islam_religion }
			}
			custom_tooltip = islamic_vassals_will_convert
		}
		##If I am not Islamic, we will become feudal.
		else_if = {
			limit = { is_independent_ruler = yes }
			custom_tooltip = other_vassals_will_convert
		}
	}

	hidden_effect = {
		#Nuke tribal authorities.
		#[FF]
		#[StA]
		if = {
			limit = { StA_is_loaded = yes }
			StA_convert_whole_realm_to_feudalism_effect = yes
		}
		else = {
			if = {
				limit = { has_realm_law = tribal_authority_0 }
				remove_realm_law = tribal_authority_0
			}
			if = {
				limit = { has_realm_law = tribal_authority_1 }
				remove_realm_law = tribal_authority_1
			}
			if = {
				limit = { has_realm_law = tribal_authority_2 }
				remove_realm_law = tribal_authority_2
			}
			if = {
				limit = { has_realm_law = tribal_authority_3 }
				remove_realm_law = tribal_authority_3
			}
			#Start off crown authorities.
			if = {
				limit = {
					NOT = { has_realm_law = crown_authority_0 }
				}
				add_realm_law_skip_effects = crown_authority_0
			}
		}
		#[/StA]
		#[/FF]

		#Also convert every vassal.
		#Liege has taken clan, so become clan.
		if = {
			limit = {
				government_has_flag = government_is_clan
			}
			every_vassal_or_below = {
				limit = { government_has_flag = government_is_tribal }
				change_government = clan_government
				#[FF]
				#[StA]
				if = {
					limit = { StA_is_loaded = yes }
					StA_convert_whole_realm_to_feudalism_effect = yes
				}
				else = {
					#Nuke tribal authorities.
					if = {
						limit = { has_realm_law = tribal_authority_0 }
						remove_realm_law = tribal_authority_0
					}
					if = {
						limit = { has_realm_law = tribal_authority_1 }
						remove_realm_law = tribal_authority_1
					}
					if = {
						limit = { has_realm_law = tribal_authority_2 }
						remove_realm_law = tribal_authority_2
					}
					if = {
						limit = { has_realm_law = tribal_authority_3 }
						remove_realm_law = tribal_authority_3
					}
					if = {
						limit = {
							NOT = { has_realm_law = crown_authority_0 }
						}
						add_realm_law_skip_effects = crown_authority_0
					}
				}
				#[/StA]
				#[/FF]
			}
		}
		#Liege is not clan, become feudal.
		else = {
			every_vassal_or_below = {
				limit = { government_has_flag = government_is_tribal }
				change_government = feudal_government
				#[FF]
				#[StA]
				if = {
					limit = { StA_is_loaded = yes }
					StA_convert_whole_realm_to_feudalism_effect = yes
				}
				else = {
					#Nuke tribal authorities.
					if = {
						limit = { has_realm_law = tribal_authority_0 }
						remove_realm_law = tribal_authority_0
					}
					if = {
						limit = { has_realm_law = tribal_authority_1 }
						remove_realm_law = tribal_authority_1
					}
					if = {
						limit = { has_realm_law = tribal_authority_2 }
						remove_realm_law = tribal_authority_2
					}
					if = {
						limit = { has_realm_law = tribal_authority_3 }
						remove_realm_law = tribal_authority_3
					}
					if = {
						limit = {
							NOT = { has_realm_law = crown_authority_0 }
						}
						add_realm_law_skip_effects = crown_authority_0
					}
				}
				#[/StA]
				#[/FF]
			}
		}
		
		## Spawn holdings in tribal counties
		
		every_sub_realm_county = { # Spawn temples in your holy sites
			limit = {
				any_county_province = {
					has_holding_type = tribal_holding
				}
				NOT = {
					any_county_province = {
						has_holding_type = church_holding
					}
				}
				any_county_province = {
					has_holding = no
					barony = {
						is_holy_site_of = root.faith
					}
				}
			}
			random_county_province = {
				limit = {
					has_holding = no
					barony_cannot_construct_holding = no
					barony = {
						is_holy_site_of = root.faith
					}
				}
				set_holding_type = church_holding
			}
		}
		
		every_sub_realm_county = { # Spawn a City
			limit = {
				any_county_province = {
					has_holding_type = tribal_holding
				}
				any_county_province = {
					has_holding = no
				}
				NOT = {
					any_county_province = {
						has_holding_type = city_holding
					}
				}
			}
			random_county_province = {
				limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
					}
					is_coastal = yes
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
					}
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
					}
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					barony_cannot_construct_holding = no
				}
				random = {
					chance = 60
					modifier = {
						add = county.development_level
					}
					modifier = {
						add = 50
						this.county = root.capital_county
					}
					modifier = {
						add = 20
						OR = {
							terrain = floodplains
							terrain = oasis
							terrain = farmlands
						}
					}
					set_holding_type = city_holding
				}
			}
		}
		
		every_sub_realm_county = { # Spawn a Temple
			limit = {
				any_county_province = {
					has_holding_type = tribal_holding
				}
				any_county_province = {
					has_holding = no
				}
				NOT = {
					any_county_province = {
						has_holding_type = church_holding
					}
				}
			}
			random_county_province = {
				limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
					}
					is_coastal = yes
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
					}
					has_special_building_slot = no
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					OR = {
						terrain = floodplains
						terrain = oasis
						terrain = farmlands
					}
					barony_cannot_construct_holding = no
				}
				alternative_limit = {
					has_holding = no
					barony_cannot_construct_holding = no
				}
				random = {
					chance = 50
					modifier = {
						add = county.development_level
					}
					modifier = {
						add = 50
						this.county = root.capital_county
					}
					modifier = {
						add = 20
						OR = {
							terrain = floodplains	
							terrain = oasis
							terrain = farmlands
						}
					}
					set_holding_type = church_holding
				}
			}
		}

		##Sort out new buildings for liege and all vassals
		every_realm_province = {
			limit = {
				has_holding_type = tribal_holding
				NOT = { is_in_list = feudal_conversion }
			}
			add_to_list = feudal_conversion

		}
		every_vassal_or_below = {
			every_realm_province = {
				limit = {
					has_holding_type = tribal_holding
					NOT = { is_in_list = feudal_conversion }
				}
				add_to_list = feudal_conversion

			}
		}
		every_in_list = {
			list = feudal_conversion
			set_variable = {
				name = tribal_to_feudal_building_level
				value = combined_building_level
				days = 1
			}
			set_holding_type = castle_holding
			while = {
				limit = {
					var:tribal_to_feudal_building_level > 1
				}
				generate_building = yes
				change_variable = {
					name = tribal_to_feudal_building_level
					subtract = 1
				}
			}
			remove_variable = tribal_to_feudal_building_level
			remove_from_list = feudal_conversion
		}
	}
}

##################################################
# Add Special Succession Effects

special_succession_witengamot_effect = {
	if = {
		limit = { highest_held_title_tier = tier_empire }
		every_held_title = {
			limit = {
				tier = tier_empire
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = saxon_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_witengamot_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_witengamot
		}
	}
	else = {
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = saxon_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_witengamot_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_witengamot
		}
	}
}

special_succession_thing_effect = {
	if = {
		limit = { highest_held_title_tier = tier_empire }
		every_held_title = {
			limit = {
				tier = tier_empire
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = scandinavian_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_thing_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_thing
		}
	}
	else = {
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = scandinavian_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_thing_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_thing
		}
	}
}

special_succession_tanistry_effect = {
	if = {
		limit = { highest_held_title_tier = tier_empire }
		every_held_title = {
			limit = {
				tier = tier_empire
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = gaelic_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_tanistry_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_tanistry
		}
	}
	else = {
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = gaelic_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_tanistry_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_tanistry
		}
	}
}

special_succession_jirga_effect = {
	if = {
		limit = { highest_held_title_tier = tier_empire }
		every_held_title = {
			limit = {
				tier = tier_empire
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = tribal_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_jirga_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_thing
		}
	}
	else = {
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOR = {
					has_title_law_flag = advanced_succession_law
					has_title_law_flag = elective_succession_law
				}
			}
			add_title_law = tribal_elective_succession_law
		}
		every_vassal = {
			limit = { special_succession_jirga_trigger = yes }
			add_opinion = {
				modifier = implemented_traditional_succession_law_opinion
				years = 20
				target = root
			}
			custom = major_decisions.3100.tt_opinion_jirga
		}
	}
}

##################################################
# Launch Hungarian Migration Effects

launch_hungarian_migration_scripted_effect = {
	$INVADER$ = {
		#If Hungary already exists, just tell us it's getting a name change.
		if = {
			limit = {
				title:k_hungary = { is_title_created = yes }
			}
			custom_tooltip = launch_hungarian_migration_scripted_effect.tt.pannonia_renamed
			title:k_hungary = { set_title_name = k_hungary }
		}
		#Otherwise, if Hungary doesn't exist, create & acquire it _whilst_ giving it a name change.
		else_if = {
			limit = {
				title:k_hungary = { is_title_created = no }
			}
			custom_tooltip = launch_hungarian_migration_scripted_effect.tt.pannonia_renamed_and_usurped
			hidden_effect = {
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				title:k_hungary = {
					change_title_holder = {
						holder = $INVADER$
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				title:k_hungary = { set_title_name = k_hungary }
			}
		}

		#If $INVADER$ has both k_hungary & any other kingdom-tier title, destroy the excess titles.
		custom_tooltip = launch_hungarian_migration_scripted_effect.tt.destroy_other_kingdoms
		hidden_effect = {
			if = {
				limit = {
					has_title = title:k_hungary
					any_held_title = {
						tier >= tier_kingdom
						count >= 2
					}
				}
				every_held_title = {
					limit = {
						tier >= tier_kingdom
						NOT = { this = title:k_hungary }
					}
					save_temporary_scope_as = excess_kingdom
					$INVADER$ = { destroy_title = scope:excess_kingdom }
				}
			}
		}

		#Replace all Avar territories within Pannonia with Mogyer, excepting Keve & Krasso.
		custom_tooltip = launch_hungarian_migration_scripted_effect.tt.mogyers_move_to_pannonia
		hidden_effect = {
			every_sub_realm_county = {
				limit = {
					title_capital_county.title_province = { geographical_region = custom_hungary }
					culture = culture:avar
					NOR = {
						this = title:c_keve
						this = title:c_krasso
						holder = {
							AND = {
								is_ai = no
								has_culture = prev.culture
							}
						}
					}
				}
				set_county_culture = culture:mogyer
				#Switch 'em to $INVADER$'s faith if unreformed & $INVADER$ is any Magyar pagan.
				if = {
					limit = {
						$INVADER$.faith.religion = religion:magyar_religion
						faith = { has_doctrine_parameter = unreformed }
					}
					set_county_faith = $INVADER$.faith
				}
			}
		}

		#Replace all Mogyer territories outside of the Pannonian Basin
		custom_tooltip = launch_hungarian_migration_scripted_effect.tt.mogyers_outside_pannonia
		hidden_effect = {
			every_sub_realm_county = {
				limit = {
					NOT = {
						title_capital_county.title_province = { geographical_region = custom_hungary }
					}
					culture = culture:mogyer
				}
				set_county_culture = culture:pecheneg
			}
			#Switch 'em to Tengri if Magyar pagan.
			every_sub_realm_county = {
				limit = {
					NOT = {
						title_capital_county.title_province = { geographical_region = custom_hungary }
					}
					culture = culture:pecheneg
					religion = religion:magyar_religion
				}
				set_county_faith = faith:tengri_pagan
			}
		}

		#All territory outside Pannonia is given to new holders and/or made independent.
		custom_tooltip = launch_hungarian_migration_scripted_effect.tt.territory_outside_pannonia.non_mogyer_vassals
		custom_tooltip = launch_hungarian_migration_scripted_effect.tt.territory_outside_pannonia.mogyer_vassals
		custom_tooltip = launch_hungarian_migration_scripted_effect.tt.territory_outside_pannonia.former_territories
		hidden_effect = {
			#All direct vassal non-Mogyers are given their independence, unless they hold land within Pannonia.
			every_vassal = {
				limit = {
					NOT = { culture = culture:mogyer }
					highest_held_title_tier >= tier_county
					any_held_title = {
						count = all
						NOT = {
							title_capital_county.title_province = { geographical_region = custom_hungary }
						}
					}
				}
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
					add_claim_on_loss = no
				}
				becomes_independent = { change = scope:change }
				resolve_title_and_vassal_change = scope:change
			}
			#... And catch the stragglers. Has to be a separate effect block, or we'll ruin the de facto structure.
			every_vassal_or_below = {
				limit = {
					NOT = { culture = culture:mogyer }
					highest_held_title_tier >= tier_county
					any_held_title = {
						count = all
						NOT = {
							title_capital_county.title_province = { geographical_region = custom_hungary }
						}
					}
				}
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
					add_claim_on_loss = no
				}
				becomes_independent = { change = scope:change }
				resolve_title_and_vassal_change = scope:change
			}
			#Reserve a duchy, preferring the capital, for $INVADER$.
			if = {
				limit = { title:k_hungary.title_capital_county.duchy.holder = $INVADER$ }
				title:k_hungary.title_capital_county.duchy = { save_scope_as = reserved_title }
			}
			#... Of course, if we can't have the capital for some reason, grab any random duchy in Pannonia.
			else = {
				random_held_title = {
					#Filter out Syrmia, because Syrmia sucks.
					limit = {
						title_capital_county.title_province = { geographical_region = custom_hungary }
						tier = tier_duchy
						NOT = { this = title:d_syrmia }
					}
					#But if we have no other choice, Syrmia is better than no duchy.
					alternative_limit = {
						title_capital_county.title_province = { geographical_region = custom_hungary }
						tier = tier_duchy
					}
					#If we have no possible duchy, make sure to reserve at least *one* county within Pannonia.
					alternative_limit = {
						title_capital_county.title_province = { geographical_region = custom_hungary }
					}
					save_scope_as = reserved_title
				}
			}
			#Find all Mogyer vassals outside of Pannonia.
			every_vassal_or_below = {
				limit = {
					culture = culture:mogyer
					highest_held_title_tier >= tier_county
				}
				add_to_list = mogyer_vassals_list
			}
			#Find all Pannonian titles held by $INVADER$ that aren't reserved.
			every_held_title = {
				limit = {
					title_capital_county.title_province = { geographical_region = custom_hungary }
					#Technically we could use any_this_title_or_de_jure_above for the following two checks, but it was being problematic and this does the same thing (just more cumbersomely).
					OR = {
						tier = tier_county
						tier = tier_duchy
					}
					NOT = { this = scope:reserved_title }
				}
				add_to_list = spare_pannonian_titles_list
			}
			#Attempt to give out Pannonian holdings to the vassals who are about to lose land, prioritising players by tier, then NPCs by tier.
			##Now, process duchy title assignment.
			every_in_list = {
				list = mogyer_vassals_list
				#Players first.
				limit = {
					is_ai = no
					launch_hungarian_migration_ducal_processing_trigger = yes
				}
				#NPCs after that.
				alternative_limit = {
					is_ai = yes
					launch_hungarian_migration_ducal_processing_trigger = yes
				}
				#Grab the appropriate vassal.
				save_temporary_scope_as = receiving_vassal
				#If $INVADER$ has a suitable spare ducal title, hand it over.
				if = {
					limit = {
						any_in_list = {
							list = spare_pannonian_titles_list
							tier = tier_duchy
							holder = $INVADER$
						}
					}
					#Save the suitable title.
					random_in_list = {
						list = spare_pannonian_titles_list
						limit = {
							tier = tier_duchy
							holder = $INVADER$
						}
						every_in_de_jure_hierarchy = {
							limit = {
								holder = $INVADER$
								tier >= tier_county
							}
							add_to_list = multiple_donated_titles
						}
					}
					#Hand it to the receiving vassal.
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					every_in_list = {
						list = multiple_donated_titles
						save_temporary_scope_as = donated_title
						scope:donated_title = {
							change_title_holder = {
								holder = scope:receiving_vassal
								change = scope:change
							}
						}
						remove_from_list = multiple_donated_titles
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			##Finally, process county title assignment.
			###First, attempt to pair de facto sub-vassals to their lieges.
			every_in_list = {
				list = mogyer_vassals_list
				#Players first.
				limit = {
					is_ai = no
					launch_hungarian_migration_count_processing_trigger = yes
				}
				#NPCs after that.
				alternative_limit = {
					is_ai = yes
					launch_hungarian_migration_count_processing_trigger = yes
				}
				#Grab the appropriate vassal.
				save_temporary_scope_as = receiving_vassal
				#If $INVADER$ has a suitable spare county title, hand it over.
				if = {
					#Try to pair vassals with their lieges first.
					limit = {
						#Direct vassals shouldn't bother to try pairing.
						NOT = { liege = $INVADER$ }
						any_in_list = {
							list = spare_pannonian_titles_list
							launch_hungarian_migration_sub_vassal_liege_pairing_trigger = yes
						}
					}
					#Save the suitable title.
					random_in_list = {
						list = spare_pannonian_titles_list
						limit = {
							#Direct vassals shouldn't bother to try pairing.
							NOT = { scope:receiving_vassal.liege = $INVADER$ }
							#Run the stock trigger.
							launch_hungarian_migration_sub_vassal_liege_pairing_trigger = yes
							#Don't give away the new liege's primary title's capital county.
							NOT = {	this = holder.primary_title.title_capital_county }
						}
						save_temporary_scope_as = donated_title
					}
					#Hand it to the receiving vassal.
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:donated_title = {
						change_title_holder = {
							holder = scope:receiving_vassal
							change = scope:change
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			###Then, filter through any remaining direct vassals.
			every_in_list = {
				list = mogyer_vassals_list
				#Players first.
				limit = {
					is_ai = no
					launch_hungarian_migration_count_processing_trigger = yes
				}
				#NPCs after that.
				alternative_limit = {
					is_ai = yes
					launch_hungarian_migration_count_processing_trigger = yes
				}
				#Grab the appropriate vassal.
				save_temporary_scope_as = receiving_vassal
				#If $INVADER$ has a suitable spare county title, hand it over.
				if = {
					#Otherwise, grab any spare valid title that $INVADER$ has lying around.
					limit = {
						any_in_list = {
							list = spare_pannonian_titles_list
							launch_hungarian_migration_direct_vassal_requirements_trigger = { INVADER = $INVADER$ }
						}
					}
					#Save the suitable title.
					random_in_list = {
						list = spare_pannonian_titles_list
						limit = {
							launch_hungarian_migration_direct_vassal_requirements_trigger = { INVADER = $INVADER$ }
						}
						save_temporary_scope_as = donated_title
					}
					#Hand it to the receiving vassal.
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:donated_title = {
						change_title_holder = {
							holder = scope:receiving_vassal
							change = scope:change
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			##Clean-up: any landless characters should move to $INVADER$'s court.
			every_in_list = {
				list = mogyer_vassals_list
				limit = { is_ruler = no}
				save_temporary_scope_as = landless_mogyer_vassal
				$INVADER$ = { add_courtier = scope:landless_mogyer_vassal }
			}
			#Sum all remaining territory outside of Pannonia & give that territory to created local characters.
			##Start by trying to group duchies.
			every_sub_realm_title = {
				#First we try to make duchy power blocks.
				if = {
					#Grab duchies outside of Pannonia.
					limit = {
						NOT = {
							title_capital_county.title_province = { geographical_region = custom_hungary }
						}
						tier = tier_duchy
					}
					save_temporary_scope_as = seceding_duchy
					#Now grab the duchy & any sub-realm counties beneath it outside of Pannonia, and filter them to a list for processing.
					scope:seceding_duchy = {
						every_in_de_jure_hierarchy = {
							limit = {
								tier >= tier_county
								OR = {
									holder = $INVADER$
									holder = {
										any_liege_or_above = { this = $INVADER$ }
									}
								}
							}
							add_to_temporary_list = seceding_duchy_list
						}
					}
					#Create a local peasant warlord for the duchy & counties.
					create_character = {
						location = scope:seceding_duchy.title_capital_county.title_province
						template = post_migration_local_warlord_template
						save_scope_as = local_warlord
					}
					#Assign the seceding titles to the new peasant warlord.
					create_title_and_vassal_change = {
						type = usurped
						save_scope_as = change
						add_claim_on_loss = no
					}
					every_in_list = {
						list = seceding_duchy_list
						limit = {
							NAND = {
								tier = tier_county
								title_province = { geographical_region = custom_hungary }
							}
						}
						change_title_holder = {
							holder = scope:local_warlord
							change = scope:change
							take_baronies = yes
						}
						remove_from_list = seceding_duchy_list
					}
					resolve_title_and_vassal_change = scope:change
					#And give the warlord their independence.
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:local_warlord = {
						becomes_independent = { change = scope:change }
					}
					resolve_title_and_vassal_change = scope:change
					#If any vassals within Pannonia have gotten mixed up, shunt them back to $INVADER$, and try to find a suitable duke.
					##Start off by assembling a suitable list of affected vassals (should only ever be the one, but you never know).
					scope:local_warlord = {
						every_vassal = {
							limit = {
								#We're only interested in counts...
								highest_held_title_tier = tier_county
								#... Within Panonnia.
								any_held_title = { launch_hungarian_migration_county_is_within_custom_hungary_trigger = yes }
							}
							add_to_list = vassals_to_shunt
						}
					}
					##Sort through the list, see who should be the new liege of the misplaced vassals.
					every_in_list = {
						list = vassals_to_shunt
						#First off, check if there's a valid duke to hand 'em over to.
						if = {
							limit = {
								any_held_title = {
									launch_hungarian_migration_misplaced_count_suitable_liege_trigger = { INVADER = $INVADER$ }
								}
							}
							#If there is, we make a note of them for liege determination.
							random_held_title = {
								limit = {
									launch_hungarian_migration_misplaced_count_suitable_liege_trigger = { INVADER = $INVADER$ }
								}
								duchy.holder = { save_scope_as = appropriate_liege }
							}
						}
						#Otherwise, we just grab $INVADER$ as a solid fallback.
						else = {
							$INVADER$ = { save_scope_as = appropriate_liege }
						}
						#Reassign the errant vassal back to their rightful Pannonian realm.
						create_title_and_vassal_change = {
							type = conquest
							save_scope_as = change
							add_claim_on_loss = no
						}
						change_liege = {
							liege = scope:appropriate_liege
							change = scope:change
						}
						resolve_title_and_vassal_change = scope:change
					}
				}
			}
			##... Then we catch the straggler counties.
			every_sub_realm_title = {
				#First we try to make duchy power blocks.
				if = {
					#Grab duchies outside of Pannonia.
					limit = {
						NOT = {
							title_capital_county.title_province = { geographical_region = custom_hungary }
						}
						tier = tier_county
					}
					save_temporary_scope_as = seceding_county
					#Create a local peasant warlord for the county.
					create_character = {
						location = scope:seceding_county.title_province
						template = post_migration_minor_warlord_template
						save_scope_as = minor_warlord
					}
					#Assign the seceding title to the new peasant warlord.
					create_title_and_vassal_change = {
						type = usurped
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:seceding_county = {
						change_title_holder = {
							holder = scope:minor_warlord
							change = scope:change
							take_baronies = yes
						}
					}
					resolve_title_and_vassal_change = scope:change
					#And give the warlord their independence.
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:minor_warlord = {
						becomes_independent = { change = scope:change }
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
		}
	}
}

tribal_transfer_liege_title_effect = {
	create_title_and_vassal_change = {
		type = usurped
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:title_being_fought_over = {
		change_title_holder = {
			holder = scope:challenger
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	if = {
		limit = {
			root = scope:challenger
		}
		scope:liege_to_challenge = {
			add_opinion = {
				target = scope:challenger
				modifier = usurped_title
			}
		}
	}
	else = {
		hidden_effect = {
			scope:liege_to_challenge = {
				add_opinion = {
					target = scope:challenger
					modifier = usurped_title
				}
			}
		}
	}
}

tribal_challenger_lost_effect = {
	if = {
		limit = {
			root = scope:liege_to_challenge
		}
		scope:liege_to_challenge = {
			add_prestige = medium_prestige_gain
		}
	}
	else = {
		hidden_effect = {
			scope:liege_to_challenge = {
				add_prestige = medium_prestige_gain
			}
		}
	}
	add_prestige_level = -1
	if = {
		limit = {
			root = scope:challenger
		}
		scope:liege_to_challenge = {
			add_opinion = {
				target = scope:challenger
				modifier = tried_to_usurped_title
			}
		}
	}
	else = {
		hidden_effect = {
			scope:liege_to_challenge = {
				add_opinion = {
					target = scope:challenger
					modifier = tried_to_usurped_title
				}
			}
		}
	}
}

##################################################
# Negotiate the Danelaw Effects

negotiate_the_danelaw_accept_effect = {
	# Mark the partition as being active.
	custom_tooltip = negotiate_the_danelaw_accept_effect.partition_active.tt
	set_global_variable = {
		name = partition_active_danelaw
		value = title:k_england
	}
	# Kingdoms for all!
	## Scope:ntd_brit receives k_england.
	scope:ntd_brit = {
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
		}
		title:k_england = {
			change_title_holder = {
				holder = scope:ntd_brit
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		title:k_england = { add_title_law = saxon_elective_succession_law }
	}
	## Whilst scope:ntd_nord receives k_danelaw.
	scope:ntd_nord = {
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
		}
		title:k_danelaw = {
			change_title_holder = {
				holder = scope:ntd_nord
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		title:k_danelaw = {
			add_title_law = scandinavian_elective_succession_law
			# And make sure it follows appropriate de jures.
			hidden_effect = { set_de_jure_liege_title = title:k_england.de_jure_liege }
		}
	}
	# Give k_danelaw various de jures.
	## Firstly, any duchy that it has at least one county in which isn't scope:ntd_brit's capital duchy.
	custom_tooltip = negotiate_the_danelaw_accept_effect.de_jure_holdings_recognised.tt
	hidden_effect = {
		scope:ntd_nord = {
			every_sub_realm_county = {
				limit = {
					kingdom = title:k_england
					NOT = { duchy = scope:ntd_brit.capital_county.duchy }
				}
				duchy = { set_de_jure_liege_title = title:k_danelaw }
			}
		}
	}
	## Then, any independent duchy held by a Scandi-side ruler (again exempting scope:ntd_brit's capital, just in case).
	custom_tooltip = negotiate_the_danelaw_accept_effect.independent_holdings_recognised.tt
	hidden_effect = {
		every_county_in_region = {
			region = world_europe_west_britannia
			limit = {
				kingdom = title:k_england
				exists = duchy.holder
				duchy.holder = { negotiate_the_danelaw_norse_side_trigger = yes }
				NOT = { duchy = scope:ntd_brit.capital_county.duchy }
			}
			duchy = { set_de_jure_liege_title = title:k_danelaw }
		}
	}
	# Set up a ceasefire & some opinion gain between the two rulers.
	scope:ntd_brit = {
		add_truce_both_ways = {
			character = scope:ntd_nord
			years = 25
			name = TRUCE_SIGNED_PARTITION
		}
		add_opinion = {
			target = scope:ntd_nord
			modifier = respect_opinion
			opinion = 50
		}
		reverse_add_opinion = {
			target = scope:ntd_nord
			modifier = respect_opinion
			opinion = 50
		}
	}
	# Independent rulers on both sides are extremely unimpressed with this divvying.
	## First, grab everyone applicable.
	title:k_england = {
		every_in_de_jure_hierarchy = {
			limit = {
				exists = holder
				holder = {
					is_independent_ruler = yes
					NOR = {
						this = scope:ntd_actor
						this = scope:ntd_recipient
					}
				}
			}
			holder = { add_to_list = riled_danelaw_independents_list }
		}
	}
	## Then, apply opinion & hidden rivalry effects.
	every_in_list = {
		list = riled_danelaw_independents_list
		custom = negotiate_the_danelaw_accept_effect.riled_independents_list
		add_opinion = {
			target = scope:ntd_brit
			modifier = weak_opinion
			opinion = -40
		}
		add_opinion = {
			target = scope:ntd_nord
			modifier = weak_opinion
			opinion = -40
		}
		# We hide the rivalry stuff, both to make it seem less unappealing, and to encourage valid-feeling drama without deliberately signalling it.
		hidden_effect = {
			if = {
				limit = {
					can_set_relation_potential_rival_trigger = { CHARACTER = scope:ntd_brit }
				}
				set_relation_potential_rival = scope:ntd_brit
			}
			if = {
				limit = {
					can_set_relation_potential_rival_trigger = { CHARACTER = scope:ntd_nord }
				}
				set_relation_potential_rival = scope:ntd_nord
			}
		}
	}
}

negotiate_the_danelaw_reject_effect = {
	scope:ntd_actor = {
		# If possible, scope:ntd_actor & scope:ntd_recipient immediately become nemeses.
		if = {
			limit = {
				can_set_relation_nemesis_trigger = { CHARACTER = scope:ntd_recipient }
			}
			set_relation_nemesis = {
				reason = nemesis_danelaw_rejection
				copy_reason = rival
				target = scope:ntd_recipient
			}
		}
		else = {
			add_opinion = {
				target = scope:ntd_recipient
				modifier = humiliated_opinion
				opinion = -75
			}
		}
		# Scope:ntd_actor loses a huge amount of prestige.
		add_prestige = massive_prestige_loss
		# Scope:ntd_actor gets a derrogatory nickname.
		give_nickname = nick_the_weak
	}
}

negotiate_the_danelaw_each_way_effect = {
	random_list = {
		# The offer is accepted.
		100 = {
			show_chance = no
			desc = negotiate_the_danelaw_each_way_effect.accepted
			negotiate_the_danelaw_accept_effect = yes
		}
		# The offier is rejected.
		100 = {
			show_chance = no
			desc = negotiate_the_danelaw_each-way_effect.rejected
			negotiate_the_danelaw_reject_effect = yes
		}
	}
}

resolve_danelaw_partition_effect = {
	# Drift all k_danelaw cores into England.
	custom_tooltip = resolve_danelaw_partition_effect.adjust_de_jures.tt
	hidden_effect = {
		title:k_danelaw = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:k_england
			}
		}
	}
	# If k_danelaw is victorious, take care of various bits'n'pieces.
	if = {
		limit = { scope:partition_winner = title:k_danelaw }
		# If appropriate, create k_england.
		if = {
			limit = {
				NOT = { exists = title:k_england.holder }
			}
			create_title_and_vassal_change = {
				type = created
				save_scope_as = title_change
			}
			title:k_england = {
				change_title_holder = {
					holder = scope:victor
					change = scope:title_change
				}
			}
			resolve_title_and_vassal_change = scope:title_change
		}
		# Else usurp k_england.
		else_if = {
			limit = {
				NOT = { title:k_england.holder = scope:victor }
			}
			create_title_and_vassal_change = {
				type = usurped
				save_scope_as = title_change
				add_claim_on_loss = yes
			}
			title:k_england = {
				change_title_holder = {
					holder = scope:victor
					change = scope:title_change
				}
			}
			resolve_title_and_vassal_change = scope:title_change
		}
		# Sundry cosmetic changes.
		title:k_england = {
			if = { # Only do Scandinavian things for Scandinavians
				limit = {
					holder.culture = { has_cultural_pillar = heritage_north_germanic }
				}
				# Set k_england's CoA to k_danelaw's.
				custom_tooltip = resolve_danelaw_partition_effect.coa_changed.tt
				title:k_england = { set_coa = title:k_danelaw }
				# Give k_england the appropriate succession law.
				add_title_law = scandinavian_elective_succession_law
			}
			# Use k_danelaw's title history, as they won the cold war.
			copy_title_history = title:k_danelaw
			# And make York the de jure capital.
			set_capital_county = title:c_north_riding
		}
		# Remove k_danelaw.
		destroy_title = title:k_danelaw
		# All claims on k_danelaw become claims on k_england.
		title:k_danelaw = {
			every_claimant = {
				custom = resolve_danelaw_partition_effect.all_claimants
				remove_claim = title:k_danelaw
				add_unpressed_claim = title:k_england
			}
		}
		# Finally, mark that this has happened for HKotNS purposes.
		set_global_variable = {
			name = danelaw_became_england
			value = yes
		}
	}
	# If k_england is victorious, we also have a few little things to do.
	if = {
		limit = { scope:partition_winner = title:k_england }
		# If it still exists, destroy k_danelaw.
		if = {
			limit = { exists = title:k_danelaw.holder }
			destroy_title = title:k_danelaw
		}
		# All claims on k_danelaw are removed.
		title:k_danelaw = {
			every_claimant = {
				custom = resolve_danelaw_partition_effect.all_claimants
				remove_claim = title:k_danelaw
			}
		}
	}
}

formalise_the_daneland_effect = {
	# To start with, remove the partition variable (so that we don't calculate partition resolution whilst taking the alternate route).
	remove_global_variable = partition_active_danelaw
	# Clarify the status of the partition for the player.
	custom_tooltip = formalise_the_daneland_effect.partition_status.tt
	# Sort the title swap.
	## First, flip everything in k_danelaw to k_daneland.
	custom_tooltip = formalise_the_daneland_effect.adjust_de_jures.tt
	hidden_effect = {
		title:k_danelaw = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:k_daneland
			}
		}
		# Add k_daneland to whatever empire k_danelaw is part of.
		title:k_daneland = { set_de_jure_liege_title = title:k_danelaw.de_jure_liege }
	}
	## Then, swap the two titles out.
	### Give k_daneland to scope:founder.
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
	}
	title:k_daneland = {
		change_title_holder = {
			holder = scope:founder
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change
	### Do some assorted aesthetic prep-work on k_daneland.
	hidden_effect = {
		title:k_daneland = {
			# Give them k_danelaw's CoA.
			set_coa = title:k_danelaw
			# & title history.
			copy_title_history = title:k_danelaw
		}
	}
	### Destroy k_danelaw.
	destroy_title = title:k_danelaw
	# All claims on k_danelaw become claims on k_daneland
	title:k_danelaw = {
		every_claimant = {
			custom = formalise_the_daneland_effect.all_claimants
			remove_claim = title:k_danelaw
			add_unpressed_claim = title:k_daneland
		}
	}
	# Step a rivalry with scope:england.
	scope:founder = {
		progress_towards_rival_effect = {
			CHARACTER = scope:england
			OPINION = -75
			REASON = rival_daneland
		}
	}
}

##################################################
# Found Cadet House Effects
found_cadet_house_decision_effect = {
	hidden_effect = {
		house.house_head = {
			save_scope_as = former_house_head
		}
		house = {
			save_scope_as = former_house
		}
		add_achievement_flag_effect = { FLAG = achievement_a_house_of_my_own_flag }
	}

	create_cadet_branch = yes
	add_prestige = $PRESTIGE$
	hidden_effect = {
		save_scope_as = new_head
		house = {
			save_scope_as = new_house
		}
		every_player = {
			limit = {
				exists = house
				house = scope:new_house
				NOT = { this = $CHARACTER$ }
			}
			send_interface_toast = {
				type = msg_created_new_house
				title = created_cadet_branch_toast_new_house_desc
				left_icon = scope:new_head
				desc = created_cadet_branch_toast_new_house_other_tt
			}
		}
		send_interface_toast = {
			type = msg_created_new_house
			title = created_cadet_branch_toast_new_house_desc
			left_icon = scope:new_head
			right_icon = scope:former_house_head
			desc = created_cadet_branch_toast_new_house_other_tt
		}
		if = {
			limit = {
				any_child = {
					has_trait = bastard
					is_lowborn = no
					dynasty = scope:new_head.dynasty
					NOT =  { house = scope:new_house }
				}
			}
			every_child = {
				limit = {
					has_trait = bastard
					is_lowborn = no
					dynasty = scope:new_head.dynasty
					NOT =  { house = scope:new_house }
				}
				set_house = scope:new_house
			}
		}
		if = {
			limit = {
				any_artifact = {
					trigger_if = {
						limit = { exists = var:banner_dynasty }
						var:banner_dynasty = root.dynasty
					}
					trigger_else = { always = no }
                    artifact_owner = {
						NOT = { this = root }
						in_diplomatic_range = root
					}
					NOT = {
						any_artifact_house_claimant = {
							this = scope:new_house
						}
					}
				}
			}
			random_artifact = {
				limit = {
					exists = var:banner_dynasty
					var:banner_dynasty = root.dynasty
					artifact_owner = { in_diplomatic_range = root }
					NOT = {
						any_artifact_house_claimant = {
							this = scope:new_house
						}
					}
				}
				save_scope_as = new_branch_dynasty_banner
			}
			scope:new_house = {
				add_house_artifact_claim = scope:new_branch_dynasty_banner
			}
		}

		# Struggle Catalysts
		if = {
			limit = {
				root =  {
					exists = house
					exists = house.house_head
					any_character_struggle	 = {
						involvement = involved
						phase_has_catalyst = catalyst_cadet_branch_created
					}
				}
			}
			every_character_struggle = {
				involvement = involved
				activate_struggle_catalyst = {
					catalyst = catalyst_cadet_branch_created
					character = root
				}
				log_debug_variable_for_persian_struggle_effect = { VAR = unrest_catalyst_cadet_branch_created }
			}
		}
	}
}

learn_language_linguist_effect = {
	hidden_effect = { add_courtier = $LINGUIST$ }
	$LINGUIST$ = { save_scope_as = selected_linguist }
	custom_tooltip = ep1_decision.0201.tt.aptitude
	send_interface_toast = {
		title = learn_court_language_toast_title
		left_icon = root
		right_icon = $LINGUIST$
		court_position_grant_effect = {
			EMPLOYER = root
			POS = court_tutor
			CANDIDATE = $LINGUIST$
		}
		if = {
			limit = {
				NOT = { knows_language_of_culture = $LINGUIST$.culture }
			}
	    	start_scheme = {
				type = learn_language
				target_character = $LINGUIST$
			}
	    }
	}
}

linguist_bonus_culture_effect = {
	if = {
		limit = { exists = scope:$CULTURE$ }
		if = {
			limit = {
				any_culture_global = {
					exists = culture_head
					has_same_culture_heritage = scope:$CULTURE$
					NOR = {
						has_same_culture_language = scope:$CULTURE$
						has_same_culture_language = root.culture
						AND = {
							exists = scope:target_court_language.culture
							has_same_culture_language = scope:target_court_language.culture
						}
						AND = {
							exists = scope:ling_cul_1_bonus
							THIS = scope:ling_cul_1_bonus
						}
						AND = {
							exists = scope:ling_cul_2_bonus
							THIS = scope:ling_cul_2_bonus
						}
						AND = {
							exists = scope:ling_cul_3_bonus
							THIS = scope:ling_cul_3_bonus
						}
					}
				}
			}
			random_culture_global = { # Find a culture that speaks this language natively
				limit = {
					exists = culture_head
					has_same_culture_heritage = scope:$CULTURE$
					NOR = {
						has_same_culture_language = scope:$CULTURE$
						has_same_culture_language = root.culture
						AND = {
							exists = scope:target_court_language.culture
							has_same_culture_language = scope:target_court_language.culture
						}
						AND = {
							exists = scope:ling_cul_1_bonus
							THIS = scope:ling_cul_1_bonus
						}
						AND = {
							exists = scope:ling_cul_2_bonus
							THIS = scope:ling_cul_2_bonus
						}
						AND = {
							exists = scope:ling_cul_3_bonus
							THIS = scope:ling_cul_3_bonus
						}
					}
				}
				save_scope_as = $CULTURE$_bonus
			}
		}
		else = {
			random_kingdom = {
				limit = {
					exists = holder
					NOR = {
						holder = root
						AND = {
							exists = scope:target_court_language
							holder = scope:target_court_language
						}
					}
					holder = {
						in_diplomatic_range = root
						culture = {
							NOR = {
								has_same_culture_language = scope:$CULTURE$
								has_same_culture_language = root.culture
								AND = {
									exists = scope:target_court_language
									has_same_culture_language = scope:target_court_language.culture
								}
								AND = {
									exists = scope:ling_cul_1_bonus
									this = scope:ling_cul_1_bonus
								}
								AND = {
									exists = scope:ling_cul_2_bonus
									this = scope:ling_cul_2_bonus
								}
								AND = {
									exists = scope:ling_cul_3_bonus
									this = scope:ling_cul_3_bonus
								}
							}
						}
					}
				}
				holder.culture = { save_scope_as = $CULTURE$_bonus }
			}
		}
	}
}

linguist_random_realm_effect = {
	# Save random culture realm to fetch faith
	if = {
		limit = {
			any_independent_ruler = { culture = scope:$CULTURE$ }
		}
		ordered_independent_ruler = {
			limit = { culture = scope:$CULTURE$ }
			order_by = primary_title.tier
			save_scope_as = $CULTURE$_ruler
		}
	}
	# Save fallback to fetch faith
	else = {
		scope:target_court_language = { save_scope_as = $CULTURE$_ruler }
	}
}

isolate_family_decision_effect = {
	show_as_tooltip = {
		every_close_family_member = {
			custom = every_close_family_member_at_court
			limit = {
				is_courtier_of = root
			}
			add_character_modifier = {
				modifier = isolating_modifier
				years = 5
			}
		}
		add_character_modifier = {
			modifier = isolating_modifier
			years = 5
		}
	}
	custom_tooltip = travelling_will_break_isolation_tt
	hidden_effect = {
		every_close_family_member = {
			custom = every_close_family_member_at_court
			limit = {
				is_courtier_of = root
			}
			root = {
				add_to_variable_list = {
					name = isolating_relatives
					target = prev
				}
			}
			add_character_flag = {
				flag = blocked_from_leaving
				years = 5
			}
			add_character_modifier = {
				modifier = isolating_modifier
				years = 5
			}
		}
		every_consort = {
			limit = {
				is_courtier_of = root
			}
			root = {
				add_to_variable_list = {
					name = isolating_relatives
					target = prev
				}
			}
			add_character_flag = {
				flag = blocked_from_leaving
				years = 5
			}
			add_character_modifier = {
				modifier = isolating_modifier
				years = 5
			}
		}
		add_character_modifier = {
			modifier = isolating_modifier
			years = 5
		}
		set_variable = {
			name = isolating_cooldown_var
			years = 1
		}
		add_character_flag = { # Prevent the AI from constantly coming in and out of isolation
			flag = ai_unisolate_cooldown
			years = 1
		}
	}
	stress_impact = {
		brave = minor_stress_impact_gain
		gregarious = minor_stress_impact_gain
	}
}

unisolate_family_decision_effect = {
	show_as_tooltip = {
		every_close_family_member = {
			custom = every_close_family_member_at_court
			limit = {
				is_courtier_of = root
			}
			remove_character_modifier = isolating_modifier
		}
	}
	hidden_effect = {
		every_in_list = {
			variable = isolating_relatives
			limit = { is_alive = yes }
			remove_character_modifier = isolating_modifier
			remove_character_flag = blocked_from_leaving
		}
		clear_variable_list = isolating_relatives
	}
	remove_character_modifier = isolating_modifier
	stress_impact = {
		paranoid = minor_stress_impact_gain
		shy = minor_stress_impact_gain
	}
}
