﻿### adjusted from the vanilla hungarian migration effects

# MIGRATION_TARGET = title:k_hungary
# MIGRATION_REGION = custom_hungary
# MIGRATION_RENAME = k_hungary
# MIGRATION_CULTURE = culture:mogyer
# MIGRATION_LEADER = scope:attacker
# MIGRATION_TARGET_TIER = tier_kingdom

#Launch Hungarian Migration Effects
historicinvasions_migration_effect_full = {
	historicinvasions_migration_effect_acquire_title = {
		MIGRATION_LEADER = $MIGRATION_LEADER$
		MIGRATION_TARGET = $MIGRATION_TARGET$ # acquire title
	}
	historicinvasions_migration_effect_destroy_other_titles = {
		MIGRATION_LEADER = $MIGRATION_LEADER$
		MIGRATION_TARGET = $MIGRATION_TARGET$ # is not destroyed
		MIGRATION_TARGET_TIER = $MIGRATION_TARGET_TIER$ # destroy all higher titles
	}
	historicinvasions_migration_effect_acquire_new_counties = {
		MIGRATION_LEADER = $MIGRATION_LEADER$
		MIGRATION_DYNN = $MIGRATION_DYNN$ # used for naming variables
		MIGRATION_REGION = $MIGRATION_REGION$ # acquire ai-controlled counties
		MIGRATION_COUNTY_AMOUNT = $MIGRATION_COUNTY_AMOUNT$
	}
	historicinvasions_migration_effect_grant_independence = {
		MIGRATION_LEADER = $MIGRATION_LEADER$
		MIGRATION_REGION = $MIGRATION_REGION$ # grant independence if not within here
		MIGRATION_DYNN = $MIGRATION_DYNN$ # just to set a global variable with that dynasty name
	}
}

historicinvasions_migration_effect_acquire_title = {
	$MIGRATION_LEADER$ = {
		if = { # usurp MIGRATION_TARGET if it is already created and MIGRATION_LEADER doesn't hold it
			limit = {
				$MIGRATION_TARGET$ = { is_title_created = yes }
				NOT = { $MIGRATION_TARGET$.holder = $MIGRATION_LEADER$ }
			}
			hidden_effect = {
				create_title_and_vassal_change = {
					type = usurped
					save_scope_as = change
					add_claim_on_loss = no
				}
				$MIGRATION_TARGET$ = {
					change_title_holder = {
						holder = $MIGRATION_LEADER$
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
		else_if = { # if MIGRATION_TARGET is not created, acquire it
			limit = {
				$MIGRATION_TARGET$ = { is_title_created = no }
			}
			hidden_effect = {
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				$MIGRATION_TARGET$ = {
					change_title_holder = {
						holder = $MIGRATION_LEADER$
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
	}
}

historicinvasions_migration_effect_destroy_other_titles = {
	$MIGRATION_LEADER$ = {
		#If $MIGRATION_LEADER$ has both INVASION_TARGET & any other kingdom-tier title, destroy the excess titles.
		hidden_effect = {
			if = {
				limit = {
					has_title = $MIGRATION_TARGET$
					any_held_title = {
						tier >= $MIGRATION_TARGET_TIER$
						count >= 2
					}
				}
				every_held_title = {
					limit = {
						tier >= $MIGRATION_TARGET_TIER$
						NOT = { this = $MIGRATION_TARGET$ }
					}
					save_temporary_scope_as = excess_top_title
					$MIGRATION_LEADER$ = { destroy_title = scope:excess_top_title }
				}
			}
		}
	}
}

historicinvasions_migration_effect_acquire_new_counties = {
	set_local_variable = {
		name = $MIGRATION_DYNN$_max_migration_counties_var
		value = 0
	}
	while = {
		limit = {
			local_var:$MIGRATION_DYNN$_max_migration_counties_var < $MIGRATION_COUNTY_AMOUNT$
		}
		random_county_in_region = {
			region = $MIGRATION_REGION$
			limit = {
				holder = { is_ai = yes }
			}
			county = { add_to_temporary_list = $MIGRATION_DYNN$_de_facto_migration_provinces_list }
		}
		change_local_variable = {
			name = $MIGRATION_DYNN$_max_migration_counties_var
			add = 1
		}
	}
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	every_in_list = {
		list = $MIGRATION_DYNN$_de_facto_migration_provinces_list
		change_title_holder = {
			holder = $MIGRATION_LEADER$
			change = scope:title_change
		}
		set_county_faith = $MIGRATION_LEADER$.faith
	}
	resolve_title_and_vassal_change = scope:title_change
}

historicinvasions_migration_effect_grant_independence = { # also sets completion variable for dynasty
	$MIGRATION_LEADER$ = {
		hidden_effect = {
			#All direct vassals are given their independence, unless they hold land within MIGRATION_TARGET.
			every_vassal = {
				limit = {
					highest_held_title_tier >= tier_county
					any_held_title = {
						count = all
						NOT = {
							title_capital_county.title_province = { geographical_region = $MIGRATION_REGION$ }
						}
					}
				}
				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 = {
					highest_held_title_tier >= tier_county
					any_held_title = {
						count = all
						NOT = {
							title_capital_county.title_province = { geographical_region = $MIGRATION_REGION$ }
						}
					}
				}
				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
			}

			#Sum all remaining territory outside of MIGRATION_TARGET & 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 MIGRATION_TARGET.
					limit = {
						is_head_of_faith = no
						is_landless_type_title = no
						NOT = { title_capital_county.title_province = { geographical_region = $MIGRATION_REGION$ } }
						tier = tier_duchy
					}
					save_temporary_scope_as = seceding_duchy
					#Now grab the duchy & any sub-realm counties beneath it outside of MIGRATION_TARGET, and filter them to a list for processing.
					scope:seceding_duchy = {
						every_in_de_jure_hierarchy = {
							limit = {
								tier >= tier_county
								OR = {
									holder = $MIGRATION_LEADER$
									holder = {
										any_liege_or_above = { this = $MIGRATION_LEADER$ }
									}
								}
							}
							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 = hi_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 = $MIGRATION_REGION$ }
							}
						}
						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 MIGRATION_TARGET have gotten mixed up, shunt them back to $MIGRATION_LEADER$, 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 MIGRATION_TARGET.
								any_held_title = { historicinvasions_migration_county_is_within_MIGRATION_REGION_trigger = { MIGRATION_REGION = $MIGRATION_REGION$ } }
							}
							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 = {
									historicinvasions_migration_misplaced_count_suitable_liege_trigger = {
										MIGRATION_LEADER = $MIGRATION_LEADER$
										MIGRATION_REGION = $MIGRATION_REGION$
									}
								}
							}
							#If there is, we make a note of them for liege determination.
							random_held_title = {
								limit = {
									historicinvasions_migration_misplaced_count_suitable_liege_trigger = {
										MIGRATION_LEADER = $MIGRATION_LEADER$
										MIGRATION_REGION = $MIGRATION_REGION$
									}
								}
								duchy.holder = { save_scope_as = appropriate_liege }
							}
						}
						#Otherwise, we just grab $MIGRATION_LEADER$ as a solid fallback.
						else = {
							$MIGRATION_LEADER$ = { save_scope_as = appropriate_liege }
						}
						#Reassign the errant vassal back to their rightful MIGRATION_TARGET 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 MIGRATION_TARGET.
					limit = {
						is_head_of_faith = no
						is_landless_type_title = no
						NOT = { title_capital_county.title_province = { geographical_region = $MIGRATION_REGION$ } }
						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 = hi_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
				}
			}
		}
	}
	add_to_global_variable_list = {
		name = historicinvasions_migrations
		target = flag:$MIGRATION_DYNN$_migration_complete
	}
}