﻿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 = {
		E_kCAFG_convert_culture_in_all_county_provinces = {
			FACTOR = 0.75
			VARIANCE = 0.2
			CULTURE = root.culture
			MAX = 0.8
		}
	}
	if = {
		limit = {
			culture = { has_cultural_pillar = heritage_goidelic }
		}
		give_nickname = nick_the_tuatha_de_danann
	}
	else = {
		give_nickname = nick_pendragon
	}
}

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
		}
		E_kCAFG_convert_culture_in_all_county_provinces = {
			FACTOR = 0.2
			VARIANCE = 0.5
			CULTURE = culture:english
			MAX = 0.9
		}
	}

	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
		E_kCAFG_convert_culture_in_all_county_provinces = {
			FACTOR = 0.7
			VARIANCE = 0.1
			CULTURE = culture:portuguese
			MAX = 0.9
		}
	}
	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
		E_kCAFG_convert_culture_in_all_county_provinces = {
			FACTOR = 0.25
			VARIANCE = 0.5
			CULTURE = culture:portuguese
			MAX = 0.9
		}
	}

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

	#Prestige
	add_prestige = massive_prestige_gain	
}

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 = {
					faith != scope:africa_uniter.faith
					is_ai = yes
					capital_province ?= { geographical_region = world_africa }
				}
			}
			every_vassal_or_below = {
				custom = every_non_root_faith_vassal_africa
				limit = {
					faith != scope:africa_uniter.faith
					is_ai = yes
					capital_province ?= { geographical_region = world_africa }
				}
				random = {
					chance = 33
					set_character_faith = scope:africa_uniter.faith
				}
			}
		}

		if = {
			limit = {
				any_realm_province = {
					faith != scope:africa_uniter.faith
					geographical_region = world_africa
				}
			}
			every_realm_province = {
				limit = {
					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
				E_kCAFG_convert_faith_in_all_county_provinces = {
					FACTOR = 0.3
					VARIANCE = 0.5
					FAITH = scope:africa_uniter.faith
					MAX = 0.9
				}
			}
		}
	}
}

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 = {
				faith != scope:avenger.faith
				is_ai = yes
				capital_province ?= { geographical_region = world_europe_west }
			}
		}
		every_vassal_or_below = {
			custom = every_non_root_faith_vassal_west_europe
			limit = {
				faith != scope:avenger.faith
				is_ai = yes
				capital_province ?= { geographical_region = world_europe_west }
			}
			random = {
				chance = 33
				set_character_faith = scope:avenger.faith
			}
		}
	}

	if = {
		limit = {
			any_realm_province = {
				faith != scope:avenger.faith
				geographical_region = world_europe_west
			}
		}
		every_realm_province = {
			limit = {
				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
			E_kCAFG_convert_faith_in_all_county_provinces = {
				FACTOR = 0.3
				VARIANCE = 0.5
				FAITH = scope:avenger.faith
				MAX = 0.9
			}
		}
	}

	#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
		}
	}
}

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 = {
				title_tier = county
				faith != root.faith
			}
		}
		every_held_title = {
			custom = all_held_counties
			title_tier = county
			limit = {
				faith != root.faith
			}
			E_kCAFG_convert_faith_in_all_county_provinces = {
				FACTOR = 0.35
				VARIANCE = 0.5
				FAITH = root.faith
				MAX = 0.9
			}
		}
	}
	faith = {
		change_fervor = {
			value = 15
			desc = fervor_gain_saoshyant
		}
	}
}

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 = {
						title_tier >= kingdom
						count >= 2
					}
				}
				every_held_title = {
					title_tier >= kingdom
					limit = {
						NOT = { this = title:k_hungary }
					}
					save_temporary_scope_as = excess_kingdom
					$INVADER$ = { destroy_title = scope:excess_kingdom }
				}
			}
		}

		# Kei TO DO: could be weighted conversion using origin and target provinces scales (population)

		#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
							}
						}
					}
				}

				E_kCAFG_convert_culture_in_all_county_provinces = {
					FACTOR = 0.7
					VARIANCE = 0.4
					CULTURE = culture:mogyer
					MAX = 1
				}
				
				#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 }
					}
					E_kCAFG_convert_faith_in_all_county_provinces = {
						FACTOR = 0.7
						VARIANCE = 0.4
						FAITH = $INVADER$.faith
						MAX = 1
					}
				}
			}
		}

		#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
				}
				E_kCAFG_convert_culture_in_all_county_provinces = {
					FACTOR = 0.7
					VARIANCE = 0.4
					CULTURE = culture:pecheneg
					MAX = 1
				}
			}
			#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
				}
				E_kCAFG_convert_faith_in_all_county_provinces = {
					FACTOR = 0.7
					VARIANCE = 0.4
					FAITH = faith:tengri_pagan
					MAX = 1
				}
			}
		}

		#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 = {
					title_tier = duchy
					#Filter out Syrmia, because Syrmia sucks.
					limit = {
						title_capital_county.title_province = { geographical_region = custom_hungary }
						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
					}
					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.
							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
				}
			}
		}
	}
}