﻿@creatable_successor_empire_county_threshold = 15


# Give a suitable County to Genghis
give_temujin_land_effect = {
	if = {
		limit = {
			OR = {
				is_landed = no
				highest_held_title_tier < tier_county
			}
		}
		# TODO: Good territory selection here (accounting for players)
		save_scope_as = genghis_khan

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:c_khentii = {
			change_title_holder = {
				holder = scope:genghis_khan
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	capital_county = { change_county_control = 100 }
}

# A wild Genghis Khan appears!
spawn_temujin_character_effect = {
	title:c_khentii.title_province = {
		save_scope_as = temujins_birthplace
	}
	if = {
		limit = {
			exists = character:125501
			character:125501 = {
				is_ai = no
				is_landed = yes
			}
		}
		character:125501 = {
			save_scope_as = temujin
		}
		scope:temujin = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 15
			}

			add_trait = greatest_of_khans
			form_the_mongol_empire_effect = yes
			add_prestige = 25000
			give_nickname = nick_genghis_khan
			trigger_event = conqueror.0001
		}
	}
	else_if = {
		limit = {
			exists = character:125501
			character:125501 = { is_physically_able_ai_adult = yes }
		}
		character:125501 = {
			save_scope_as = temujin
		}
		scope:temujin = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 15
			}

			add_trait = greatest_of_khans
			give_temujin_land_effect = yes
			add_gold = 5000
			add_dread = high_dread
			spawn_temujins_court_effect = yes
			form_the_mongol_empire_effect = yes
			add_prestige = 25000
			if = {
				limit = { NOT = { has_perk = peacemaker_perk } }
				add_perk = peacemaker_perk
			}
			remove_trait = education_diplomacy_1
			remove_trait = education_diplomacy_2
			remove_trait = education_diplomacy_3
			remove_trait = education_diplomacy_4
			remove_trait = education_diplomacy_5
			remove_trait = education_intrigue_1
			remove_trait = education_intrigue_2
			remove_trait = education_intrigue_3
			remove_trait = education_intrigue_4
			remove_trait = education_intrigue_5
			remove_trait = education_stewardship_1
			remove_trait = education_stewardship_2
			remove_trait = education_stewardship_3
			remove_trait = education_stewardship_4
			remove_trait = education_stewardship_5
			remove_trait = education_learning_1
			remove_trait = education_learning_2
			remove_trait = education_learning_3
			remove_trait = education_learning_4
			remove_trait = education_learning_5
			remove_trait = education_martial_1
			remove_trait = education_martial_2
			remove_trait = education_martial_3
			remove_trait = education_martial_4
			add_trait = education_martial_5
			add_trait = flexible_leader
			add_trait = athletic
			if = {
				limit = { has_dlc_feature = tours_and_tournaments }
				add_trait = tourney_participant
				add_random_tiered_trait_track_xp_effect = {
					TRAIT = lifestyle_hunter
					TRACK = hunter
					LEVEL_1 = yes
					LEVEL_3 = no
				}
				add_random_tiered_trait_track_xp_effect = {
					TRAIT = tourney_participant
					TRACK = horse
					LEVEL_1 = yes
					LEVEL_3 = yes
				}
				add_random_tiered_trait_track_xp_effect = {
					TRAIT = tourney_participant
					TRACK = bow
					LEVEL_1 = yes
					LEVEL_3 = yes
				}
			}
			dynasty = {
				add_dynasty_prestige_level = 5
				add_dynasty_prestige = 10000
				add_dynasty_perk = warfare_legacy_1
				add_dynasty_perk = warfare_legacy_2
				add_dynasty_perk = warfare_legacy_3
				add_dynasty_perk = warfare_legacy_4
				add_dynasty_perk = warfare_legacy_5
				if = {
					limit = {
						has_dlc_feature = hybridize_culture
					}
					add_dynasty_perk = ep1_culture_legacy_1
				}
			}
			give_nickname = nick_genghis_khan
		}
	}
	else_if = {
		limit = {
			exists = character:125501.dynasty
			character:125501.dynasty = {
				any_dynasty_member = {
					is_ai = no
					is_landed = yes
				}
			}
		}
		character:125501.dynasty = {
			random_dynasty_member = {
				limit = {
					is_ai = no
					is_landed = yes
				}
				save_scope_as = temujin
			}
		}
		scope:temujin = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 15
			}

			add_trait = greatest_of_khans
			form_the_mongol_empire_effect = yes
			add_prestige = 25000
			give_nickname = nick_genghis_khan
			trigger_event = conqueror.0001
		}
	}
	else_if = {
		limit = {
			exists = character:125501.dynasty
			character:125501.dynasty = {
				any_dynasty_member = {
					is_physically_able_ai_adult = yes
				}
			}
		}
		character:125501.dynasty = {
			random_dynasty_member = {
				limit = {
					is_physically_able_ai_adult = yes
					is_landed = yes
				}
				alternative_limit = {
					is_physically_able_ai_adult = yes
				}
				save_scope_as = temujin
			}
		}
		scope:temujin = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 15
			}

			add_trait = greatest_of_khans
			give_temujin_land_effect = yes
			add_gold = 5000
			add_dread = high_dread
			spawn_temujins_court_effect = yes
			form_the_mongol_empire_effect = yes
			add_prestige = 25000
			if = {
				limit = { NOT = { has_perk = peacemaker_perk } }
				add_perk = peacemaker_perk
			}
			remove_trait = education_diplomacy_1
			remove_trait = education_diplomacy_2
			remove_trait = education_diplomacy_3
			remove_trait = education_diplomacy_4
			remove_trait = education_diplomacy_5
			remove_trait = education_intrigue_1
			remove_trait = education_intrigue_2
			remove_trait = education_intrigue_3
			remove_trait = education_intrigue_4
			remove_trait = education_intrigue_5
			remove_trait = education_stewardship_1
			remove_trait = education_stewardship_2
			remove_trait = education_stewardship_3
			remove_trait = education_stewardship_4
			remove_trait = education_stewardship_5
			remove_trait = education_learning_1
			remove_trait = education_learning_2
			remove_trait = education_learning_3
			remove_trait = education_learning_4
			remove_trait = education_learning_5
			remove_trait = education_martial_1
			remove_trait = education_martial_2
			remove_trait = education_martial_3
			remove_trait = education_martial_4
			add_trait = education_martial_5
			add_trait = flexible_leader
			add_trait = athletic
			if = {
				limit = { has_dlc_feature = tours_and_tournaments }
				add_trait = tourney_participant
				add_random_tiered_trait_track_xp_effect = {
					TRAIT = lifestyle_hunter
					TRACK = hunter
					LEVEL_1 = yes
					LEVEL_3 = no
				}
				add_random_tiered_trait_track_xp_effect = {
					TRAIT = tourney_participant
					TRACK = horse
					LEVEL_1 = yes
					LEVEL_3 = yes
				}
				add_random_tiered_trait_track_xp_effect = {
					TRAIT = tourney_participant
					TRACK = bow
					LEVEL_1 = yes
					LEVEL_3 = yes
				}
			}
			dynasty = {
				add_dynasty_prestige_level = 5
				add_dynasty_prestige = 10000
				add_dynasty_perk = warfare_legacy_1
				add_dynasty_perk = warfare_legacy_2
				add_dynasty_perk = warfare_legacy_3
				add_dynasty_perk = warfare_legacy_4
				add_dynasty_perk = warfare_legacy_5
				if = {
					limit = {
						has_dlc_feature = hybridize_culture
					}
					add_dynasty_perk = ep1_culture_legacy_1
				}
			}
			give_nickname = nick_genghis_khan
		}
	}
	else = {
		if = {
			limit = { has_game_rule = inversed_gender_equality }
			create_character = {
				name = "Borte" # AKA: Genghis Khan's wife
				gender = female
				location = scope:temujins_birthplace
				template = borte_character_template
				save_scope_as = temujin
			}
			scope:temujin = {
				# Make temporarily immune to disease
				add_character_flag = {
					flag = immune_to_disease
					years = 15
				}

				add_trait = greatest_of_khans
				give_temujin_land_effect = yes
				add_gold = 5000
				add_dread = high_dread
				spawn_temujins_court_effect = yes
				form_the_mongol_empire_effect = yes
				add_prestige = 25000
				give_nickname = nick_genghis_khan
				if = {
					limit = { NOT = { has_perk = peacemaker_perk } }
					add_perk = peacemaker_perk
				}
				dynasty = {
					add_dynasty_prestige_level = 5
					add_dynasty_prestige = 10000
					add_dynasty_perk = warfare_legacy_1
					add_dynasty_perk = warfare_legacy_2
					add_dynasty_perk = warfare_legacy_3
					add_dynasty_perk = warfare_legacy_4
					add_dynasty_perk = warfare_legacy_5
					if = {
						limit = {
							has_dlc_feature = hybridize_culture
						}
						add_dynasty_perk = ep1_culture_legacy_1
					}
				}
			}
		}
		else_if = {
			limit = { has_game_rule = full_gender_equality }
			random_list = {
				#Female Ghengis Khan.
				50 = {
					create_character = {
						name = "Borte" # AKA: Genghis Khan's wife
						gender = female
						location = scope:temujins_birthplace
						template = borte_character_template
						save_scope_as = temujin
					}
					scope:temujin = {
						# Make temporarily immune to disease
						add_character_flag = {
							flag = immune_to_disease
							years = 15
						}

						add_trait = greatest_of_khans
						give_temujin_land_effect = yes
						add_gold = 5000
						add_dread = high_dread
						spawn_temujins_court_effect = yes
						form_the_mongol_empire_effect = yes
						add_prestige = 25000
						give_nickname = nick_genghis_khan
						if = {
							limit = { NOT = { has_perk = peacemaker_perk } }
							add_perk = peacemaker_perk
						}
						dynasty = {
							add_dynasty_prestige_level = 5
							add_dynasty_prestige = 10000
							add_dynasty_perk = warfare_legacy_1
							add_dynasty_perk = warfare_legacy_2
							add_dynasty_perk = warfare_legacy_3
							add_dynasty_perk = warfare_legacy_4
							add_dynasty_perk = warfare_legacy_5
							if = {
								limit = {
									has_dlc_feature = hybridize_culture
								}
								add_dynasty_perk = ep1_culture_legacy_1
							}
						}
					}
				}
				#Male Ghengis Khan.
				50 = {
					create_character = {
						name = "Temujin" # AKA: Genghis Khan
						location = scope:temujins_birthplace
						template = genghis_khan_character_template
						save_scope_as = temujin
					}
					scope:temujin = {
						# Make temporarily immune to disease
						add_character_flag = {
							flag = immune_to_disease
							years = 15
						}

						add_trait = greatest_of_khans
						give_temujin_land_effect = yes
						add_gold = 5000
						add_dread = high_dread
						spawn_temujins_court_effect = yes
						form_the_mongol_empire_effect = yes
						add_prestige = 25000
						give_nickname = nick_genghis_khan
						if = {
							limit = { NOT = { has_perk = peacemaker_perk } }
							add_perk = peacemaker_perk
						}
						dynasty = {
							add_dynasty_prestige_level = 5
							add_dynasty_prestige = 10000
							add_dynasty_perk = warfare_legacy_1
							add_dynasty_perk = warfare_legacy_2
							add_dynasty_perk = warfare_legacy_3
							add_dynasty_perk = warfare_legacy_4
							add_dynasty_perk = warfare_legacy_5
							if = {
								limit = {
									has_dlc_feature = hybridize_culture
								}
								add_dynasty_perk = ep1_culture_legacy_1
							}
						}
					}
				}
			}
		}
		else = {
			create_character = {
				name = "Temujin" # AKA: Genghis Khan
				location = scope:temujins_birthplace
				template = genghis_khan_character_template
				save_scope_as = temujin
			}
			scope:temujin = {
				# Make temporarily immune to disease
				add_character_flag = {
					flag = immune_to_disease
					years = 15
				}

				add_trait = greatest_of_khans
				give_temujin_land_effect = yes
				add_gold = 5000
				add_dread = high_dread
				spawn_temujins_court_effect = yes
				form_the_mongol_empire_effect = yes
				add_prestige = 25000
				give_nickname = nick_genghis_khan
				if = {
					limit = { NOT = { has_perk = peacemaker_perk } }
					add_perk = peacemaker_perk
				}
				dynasty = {
					add_dynasty_prestige_level = 5
					add_dynasty_prestige = 10000
					add_dynasty_perk = warfare_legacy_1
					add_dynasty_perk = warfare_legacy_2
					add_dynasty_perk = warfare_legacy_3
					add_dynasty_perk = warfare_legacy_4
					add_dynasty_perk = warfare_legacy_5
					if = {
						limit = {
							has_dlc_feature = hybridize_culture
						}
						add_dynasty_perk = ep1_culture_legacy_1
					}
				}
			}
		}
	}

	every_player = {
		trigger_event = mongol_invasion.1002 # Mongol Empire spawned notification
	}
}


# Form the actual Mongol Empire...
form_the_mongol_empire_effect = {
	save_scope_as = mongol_founder

	#Create the title
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_mongol_empire = {
		change_title_holder = {
			holder = scope:mongol_founder
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change
	
	if = {
		limit = {
			NOT = {
				government_has_flag = government_is_tribal
			}
		}
		change_government = tribal_government
	}
	#[FF]
	#[StA]
	if = {
		limit = { StA_is_loaded = yes }
		StA_form_the_mongol_empire_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_3 }
			remove_realm_law = tribal_authority_3
		}
		add_realm_law_skip_effects = tribal_authority_2
	}
	#[/StA]
	#[/FF]

	add_realm_law_skip_effects = high_partition_succession_law

	assert_if = {
		limit = { NOT = { exists = title:e_mongol_empire } }
		text = "Mongol Empire title was not created!"
	}

	hidden_effect = { set_primary_title_to = title:e_mongol_empire }

	every_held_title = {
		limit = {
			tier = tier_empire
			NOT = { this = title:e_mongol_empire }
		}
		scope:mongol_founder = {
			destroy_title = prev
		}
	}

	spawn_mongol_troops_effect = yes

	set_global_variable = {
		name = mongol_empire_has_spawned
		value = yes
	}
}

# Make sure the Empire has troops...
spawn_mongol_troops_effect = {
	spawn_army = {
		uses_supply = no
		inheritable = no
		name = mongol_event_troops
		men_at_arms = {
			type = horse_archers
			stacks = 14
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 5
		}
		men_at_arms = {
			type = trebuchet
			stacks = 5
		}
		men_at_arms = {
			type = trebuchet
			stacks = 5
		}
		location = capital_province
		origin = capital_province
	}
	spawn_army = {
		uses_supply = no
		inheritable = no
		name = mongol_event_troops
		levies = {
			value = 5000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 10
		}
		men_at_arms = {
			type = horse_archers
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 3
		}
		men_at_arms = {
			type = trebuchet
			stacks = 5
		}
		location = capital_province
		origin = capital_province
	}
	spawn_army = {
		uses_supply = no
		inheritable = no
		name = mongol_event_troops
		levies = {
			value = 5000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 10
		}
		men_at_arms = {
			type = horse_archers
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 3
		}
		men_at_arms = {
			type = trebuchet
			stacks = 5
		}
		location = capital_province
		origin = capital_province
	}
	spawn_army = {
		uses_supply = no
		inheritable = no
		name = mongol_event_troops
		levies = {
			value = 5000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 10
		}
		men_at_arms = {
			type = horse_archers
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 3
		}
		men_at_arms = {
			type = trebuchet
			stacks = 5
		}
		location = capital_province
		origin = capital_province
	}
}

spawn_mongol_troops_condensed_effect = {
	spawn_army = {
		uses_supply = no
		inheritable = no
		name = mongol_event_troops
		levies = {
			value = 15000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 74
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 70
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 10
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 14
		}
		men_at_arms = {
			type = trebuchet
			stacks = 25
		}
		location = capital_province
		origin = capital_province
	}
}

spawn_mongol_successor_state_troops_effect = {
	spawn_army = {
		uses_supply = no
		inheritable = yes
		name = mongol_event_troops
		levies = {
			value = 1000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 5
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 5
		}
		men_at_arms = {
			type = trebuchet
			stacks = 3
		}
		location = capital_province
		origin = capital_province
	}
	spawn_army = {
		uses_supply = no
		inheritable = yes
		name = mongol_event_troops
		levies = {
			value = 1000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 5
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 5
		}
		men_at_arms = {
			type = trebuchet
			stacks = 3
		}
		location = capital_province
		origin = capital_province
	}
	spawn_army = {
		uses_supply = no
		inheritable = yes
		name = mongol_event_troops
		levies = {
			value = 1000
		}
		men_at_arms = {
			type = horse_archers
			stacks = 5
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 5
		}
		men_at_arms = {
			type = trebuchet
			stacks = 3
		}
		location = capital_province
		origin = capital_province
	}
}

### Populating the life of Temujin...
spawn_temujins_court_effect = {
	add_character_flag = is_temujin
	if = {
		limit = {
			is_married = no
		}
		if = {
			limit = {
				scope:temujin = { is_female = yes }
			}
			# Create Temujin (husband)...
			create_character = {
				template = genghis_khan_character_template
				name = "Temujin" # Husband of Temujin
				employer = scope:temujin
				save_scope_as = borte
			}
		}
		else = {
			# Create Borte (wife)...
			create_character = {
				template = borte_character_template
				name = "Borte" # Wife of Temujin
				gender = female
				employer = scope:temujin
				save_scope_as = borte
			}
		}
		scope:borte = {
			marry = scope:temujin
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 10
			}
		}
		if = {
			limit = {
				scope:temujin = { is_male = yes }
			}
			scope:temujin = { save_scope_as = great_khan_family_father }
			scope:borte = { save_scope_as = great_khan_family_mother }
		}
		else = {
			scope:temujin = { save_scope_as = great_khan_family_mother }
			scope:borte = { save_scope_as = great_khan_family_father }		
		}

		# Create Jochi (son)...
		create_character = {
			template = jochi_character_template
			name = "Jochi" # (Oldest son of Temujin and Borte)
			employer = scope:temujin
			mother = scope:great_khan_family_mother
			father = scope:great_khan_family_father
			save_scope_as = jochi
		}
		scope:jochi = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 10
			}
		}

		# Create Tolui (son)...
		create_character = {
			template = tolui_character_template
			name = "Tolui" # (Son of Temujin and Borte)
			employer = scope:temujin
			mother = scope:great_khan_family_mother
			father = scope:great_khan_family_father
			save_scope_as = tolui
		}

		scope:tolui = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 10
			}
		}

		# Create Chagatai (son)...
		create_character = {
			template = chagatai_character_template
			name = "Chagatai"  # (Son of Temujin and Borte)
			employer = scope:temujin
			mother = scope:great_khan_family_mother
			father = scope:great_khan_family_father
			save_scope_as = chagatai
		}

		scope:chagatai = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 10
			}

			#Make it more likely that he gets a good martial education
			hidden_effect = {
				education_point_acquisition_effect = yes
				education_point_acquisition_effect = yes
			}
		}

		# Create Ögedei (son)...
		create_character = {
			template = ogodei_character_template
			name = "Ogodei" # Ögedei Khan (# Son of Temujin and Borte)
			employer = scope:temujin
			mother = scope:great_khan_family_mother
			father = scope:great_khan_family_father
			save_scope_as = ogodei
		}

		scope:ogodei = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 10
			}

			#Make it more likely that he gets a good martial education
			hidden_effect = {
				education_point_acquisition_effect = yes
				education_point_acquisition_effect = yes
			}
		}

		# Create Altun Begi (Daughter)...
		create_character = {
			template = altun_begi_character_template
			name = "Altun_Begi" # (Daughter of Temujin)
			employer = scope:temujin
			gender = female
			father = scope:great_khan_family_father
			save_scope_as = altun_begi
		}

		scope:altun_begi = {
			# Make temporarily immune to disease
			add_character_flag = {
				flag = immune_to_disease
				years = 10
			}
		}
	}

	# Create Subutai (Commander)...
	create_character = {
		template = subutai_character_template
		name = "Subutai" # (Temujin's Commander of armies)
		employer = scope:temujin
		save_scope_as = subutai
	}

	scope:subutai = {
		# Make temporarily immune to disease
		add_character_flag = {
			flag = immune_to_disease
			years = 10
		}
		# Set friendship with Temujin
		set_random_friendship_reason = { TARGET = scope:temujin }
	}
	
	# Spawn some generic Bagathurs
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_warrior_character
		faith = scope:temujin.faith
		culture = scope:temujin.culture
	}
	
	# Spawn some captured Han siege engineers
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_siege_engineer
		faith = faith:shangqing
		culture = culture:han
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_siege_engineer
		faith = faith:shangqing
		culture = culture:han
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_siege_engineer
		faith = faith:shangqing
		culture = culture:han
	}
	create_character = {
		gender_female_chance = temujin_soldier_female_chance
		employer = scope:temujin
		template = new_siege_engineer
		faith = faith:shangqing
		culture = culture:han
	}
}

### Start war of conquest for the Mongolian heartland
start_wars_for_mongolia_effect = {
	save_temporary_scope_as = mongol_conqueror
	# Build a list of potential targets
	every_neighboring_top_liege_realm_owner = {
		limit = {
			any_sub_realm_county = {
				title_province = { geographical_region = special_mongol_empire_start_region }
			}
		}
		if = {
			limit = { NOT = { is_in_list = top_liege_targets } }
			add_to_temporary_list = top_liege_targets
		}
	}
	every_in_list = {
		list = top_liege_targets
		save_temporary_scope_as = new_war_target
		primary_title.empire = { save_temporary_scope_as = war_target_title }
		# Declare the war
		scope:mongol_conqueror = {
			start_war = {
				cb = mongol_realm_invasion_war
				target = scope:new_war_target.top_liege
				target_title = scope:war_target_title.empire
			}
		}
	}
}

# Send subjugation demand effect
send_mongol_subjugation_demand_effect = {
	if = {
		limit = {
			NOT = { is_allied_to = scope:mongol_emperor }
		}
		trigger_event = {
			id = mongol_invasion.2001
			days = { 3 12 }
		}

		save_temporary_scope_as = subjugation_offer_recipient
		scope:mongol_emperor = {
			add_to_variable_list = {
				name = subjugation_offer_under_consideration
				target = scope:subjugation_offer_recipient
			}
		}
	}
	else = {
		if = {
			limit = {
				highest_held_title_tier >= tier_empire
			}
			trigger_event = {
				id = mongol_invasion.2111
				days = { 3 12 }
			}
		}
		else = {
			trigger_event = {
				id = mongol_invasion.2101
				days = { 3 12 }
			}
		}
	}
}


### Mongol war target evaluation and declaration
mongol_war_target_evaluation_and_declaration_effect = {
	# Set the Mongol Emperor as reference point
	save_scope_as = mongol_emperor
	add_character_flag = {
		flag = free_mongol_cb
		days = 14
	}
	# Select a new target
	random_neighboring_top_liege_realm_owner = {
		limit = {
			NOT = { is_allied_to = scope:mongol_emperor }
			NOT = { this = scope:mongol_emperor }
			NOT = { top_liege = scope:mongol_emperor }
			save_temporary_scope_as = truce_check
			NOT = {
				scope:mongol_emperor = {
					any_truce_target = {
						this = scope:truce_check
					}
				}
			}
			trigger_if = { # We don't want the Byzantine empire to get annihilated immediately
				limit = {
					OR = {
						has_primary_title = title:e_byzantium
						has_primary_title = title:e_latin_empire
					}
				}
				OR = {
					current_year >= 1279 # Roughly when the mongols should fracture historically
					scope:mongol_emperor = {
						completely_controls_region = special_mongol_empire_golden_horde_region # Controls all of Russia and the Steppe
					}
					any_realm_county = { # Byzantium is expansionist
						count >= 50
						title_province = {
							OR = {
								geographical_region = special_mongol_empire_conquest_region_prio_5
								geographical_region = special_mongol_empire_conquest_region_prio_4
								geographical_region = special_mongol_empire_conquest_region_prio_3
								geographical_region = special_mongol_empire_conquest_region_prio_2
								geographical_region = special_mongol_empire_conquest_region_prio_1
							}
						}
					}
				}
			}
		}
		weight = {
			base = 0
			modifier = {
				add = mongol_invasion_target_character_weight
				always = yes
			}
		}
		save_temporary_scope_as = next_invasion_target
		primary_title = {
			save_temporary_scope_as = next_invasion_title_target
		}
	}
	if = {
		limit = {
			exists = scope:next_invasion_target
		}
		if = {
			limit = { # Declare war on every same-tier count or duke in same empire
				scope:next_invasion_target = {
					OR = {
						highest_held_title_tier = tier_county
						highest_held_title_tier = tier_duchy
					}
				}
			}
			scope:next_invasion_title_target = {
				add_to_temporary_list = all_titles_to_declare_on
				empire = {
					every_in_de_jure_hierarchy = { # Find all other same-tier titles in same de jure empire
						continue = {
							tier > tier_county
							# Stop searching if you hit the Mongol Emperor's territory at any point
							trigger_if = {
								limit = { exists = holder }
								NOT = { holder = scope:mongol_emperor }
								NOT = { holder.top_liege = scope:mongol_emperor }
							}
						}
						limit = {
							exists = this
							OR = {
								tier = tier_county
								tier = tier_duchy
							}
							exists = holder
							holder = {
								is_independent_ruler = yes
								NOT = { this = scope:mongol_emperor }
								NOT = { top_liege = scope:mongol_emperor }
								NOT = { is_at_war_with = scope:mongol_emperor }
								NOT = { is_allied_to = scope:mongol_emperor }
								save_temporary_scope_as = truce_check
								NOT = {
									scope:mongol_emperor = {
										any_truce_target = {
											this = scope:truce_check
										}
									}
								}
							}
						}
						add_to_temporary_list = all_titles_to_declare_on
					}
				}
			}
			every_in_list = {
				list = all_titles_to_declare_on
				limit = {
					holder = {
						# An extra check to make sure we haven't snuck any Mongol territory in
						NOT = { this = scope:mongol_emperor }
						NOT = { top_liege = scope:mongol_emperor }

						# Standard conditions
						NOT = { is_at_war_with = scope:mongol_emperor }
						NOT = {
							is_in_list = has_been_sent_subjugation_offer
						}
						save_temporary_scope_as = truce_check
					}
					NOT = {
						scope:mongol_emperor = {
							any_truce_target = {
								this = scope:truce_check
							}
						}
					}
				}

				holder = {
					add_to_temporary_list = has_been_sent_subjugation_offer
					send_mongol_subjugation_demand_effect = yes
				}
			}
		}
		else_if = {
			limit = {
				scope:next_invasion_target = {
					highest_held_title_tier < tier_empire
				}
			}
			scope:next_invasion_target = {
				send_mongol_subjugation_demand_effect = yes
			}
		}
		else = { # Emperors are not offered ways out
			start_war = {
				cb = mongol_realm_invasion_war
				target = scope:next_invasion_target
				target_title = scope:next_invasion_title_target.empire
			}
		}
	}
}

mongols_devastate_county_effect = {
	change_development_level = {
		subtract = scope:county.development_level
		multiply = 0.5
		floor = yes
	}
}


### MONGOL EMPIRE BREAKUP EFFECTS ###

### Title creation effects
assign_core_mongol_titles_to_heir_effect = {
	save_temporary_scope_as = candidate

	if = { # Check whether everything is grantable
		limit = {
			NOT = { exists = global_var:handed_out_mongolia_in_mongol_succession }
			NOT = { exists = title:e_mongolia.holder }
		}
		
		# If the character doesn't hold any titles in the relevant
		if = {
			limit = {
				NOT = {
					any_held_title = {
						tier = tier_county
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
					}
				}
			}
			scope:old_mongol_emperor = {
				random_sub_realm_county = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
					}
					save_scope_as = county_to_transfer
				}
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = title_change
				add_claim_on_loss = no
			}
			scope:county_to_transfer = {
				change_title_holder = {
					holder = scope:candidate
					change = scope:title_change
				}
			}
			resolve_title_and_vassal_change = scope:title_change
		}
		
		# Create and grant title
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:e_mongolia = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		# Move de jure back from Mongol Empire to Mongolia
		title:e_mongol_empire = {
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_kingdom
				}
				set_de_jure_liege_title = title:e_mongolia
			}
		}
		# Used to check whether e_mongol_empire can be safely destroyed
		set_global_variable = {
			name = handed_out_mongolia_in_mongol_succession
			value = yes
		}
	}
	save_scope_as = mongolia_ruler
	add_to_list = candidate_granted_title
	
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_mongolia.title_capital_county
		}
		set_realm_capital = title:e_mongolia.title_capital_county
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_ilkhanate_effect = {
	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_ilkhanate_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_ilkhanate_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_ilkhanate = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = ilkhanate_ruler
	}
	set_global_variable = {
		name = ilkhanate_handed_out
		value = yes
	}

	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_ilkhanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_ilkhanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_ilkhanate.title_capital_county
		}
		set_realm_capital = title:e_ilkhanate.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_ilkhanate_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_ilkhanate_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_ilkhanate_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_golden_horde_effect = {
	debug_log = "Golden Horde about to be created"
	debug_log_scopes = no

	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_golden_horde_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_golden_horde_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_golden_horde = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = golden_horde_ruler
	}

	set_global_variable = {
		name = golden_horde_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_golden_horde.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_golden_horde.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_golden_horde.title_capital_county
		}
		set_realm_capital = title:e_golden_horde.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_golden_horde_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_golden_horde_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_golden_horde_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_chagatai_effect = {

	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_chagatai_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_chagatai_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_chagatai = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = chagatai_ruler
	}

	set_global_variable = {
		name = chagatai_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_chagatai.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_chagatai.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_chagatai.title_capital_county
		}
		set_realm_capital = title:e_chagatai.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_chagatai_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_chagatai_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_chagatai_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_black_horde_effect = {

	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_black_horde_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_black_horde_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_black_horde = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = black_horde_ruler
	}

	set_global_variable = {
		name = black_horde_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_black_horde.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_black_horde.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_black_horde.title_capital_county
		}
		set_realm_capital = title:e_black_horde.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_black_horde_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_black_horde_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_black_horde_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_white_horde_effect = {
	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_white_horde_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_white_horde_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_white_horde = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = white_horde_ruler
	}

	set_global_variable = {
		name = white_horde_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_white_horde.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_white_horde.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_white_horde.title_capital_county
		}
		set_realm_capital = title:e_white_horde.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_white_horde_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_white_horde_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_white_horde_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_aarlud_khanate_effect = {

	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_aarlud_khanate_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_aarlud_khanate_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_aarlud_khanate = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = aarlud_khanate_ruler
	}

	set_global_variable = {
		name = aarlud_khanate_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_aarlud_khanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_aarlud_khanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_aarlud_khanate.title_capital_county
		}
		set_realm_capital = title:e_aarlud_khanate.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_aarlud_khanate_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_aarlud_khanate_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_aarlud_khanate_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_togskol_khanate_effect = {

	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_togskol_khanate_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_togskol_khanate_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_togskol_khanate = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = togskol_khanate_ruler
	}

	set_global_variable = {
		name = togskol_khanate_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_togskol_khanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_togskol_khanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_togskol_khanate.title_capital_county
		}
		set_realm_capital = title:e_togskol_khanate.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_togskol_khanate_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_togskol_khanate_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_togskol_khanate_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}

grant_baruun_khanate_effect = {

	# If the character doesn't hold any titles in the relevant
	if = {
		limit = {
			NOT = {
				any_held_title = {
					tier = tier_county
					title_province = {
						geographical_region = special_mongol_empire_baruun_khanate_region
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			random_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = special_mongol_empire_baruun_khanate_region
					}
				}
				save_scope_as = county_to_transfer
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:county_to_transfer = {
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_baruun_khanate = {
		change_title_holder = {
			holder = scope:candidate
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	scope:candidate = {
		save_scope_as = baruun_khanate_ruler
	}

	set_global_variable = {
		name = baruun_khanate_handed_out
		value = yes
	}
	
	# Give the character the appropriate capital area, if possible
	if = {
		limit = {
			scope:old_mongol_emperor = {
				any_sub_realm_county = {
					duchy = title:e_baruun_khanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
			}
		}
		scope:old_mongol_emperor = {
			every_sub_realm_county = {
				limit = {
					duchy = title:e_baruun_khanate.title_capital_county.duchy
					NOR = {
						holder = scope:candidate
						holder = { # Don't steal the player's lands
							is_ai = no
						}
						holder = {
							any_liege_or_above = { is_ai = no }
						}
					}
				}
				add_to_list = titles_taken
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:candidate
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	every_character_war = {
		end_war = white_peace
	}
	if = {
		limit = {
			has_title = title:e_baruun_khanate.title_capital_county
		}
		set_realm_capital = title:e_baruun_khanate.title_capital_county
	}
	else_if = {
		limit = {
			NOT = {
				capital_county.title_province = {
					geographical_region = special_mongol_empire_baruun_khanate_region
				}
			}
			any_held_title = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_baruun_khanate_region
				}
			}
		}
		random_held_title = {
			limit = {
				tier = tier_county
				title_province = {
					geographical_region = special_mongol_empire_baruun_khanate_region
				}
			}
			scope:candidate = {
				set_realm_capital = prev
			}
		}
	}
	spawn_mongol_successor_state_troops_effect = yes
	add_gold = 1000
	add_dread = 100
	create_story = story_conqueror
}


# Effect to clean up borders a little
pick_up_remaining_counties_effect = {
	### Ilkhanate
	if = {
		limit = {
			NOT = { exists = global_var:ilkhanate_handed_out }
			any_in_list = {
				list = ilkhanate_counties
				count > 0
			}
		}
		# Try giving them to Chagatai
		if = {
			limit = {
				exists = global_var:chagatai_handed_out
			}
			every_in_list = {
				list = ilkhanate_counties
				add_to_list = chagatai_counties
			}
		}
		# Try Black Horde
		else_if = {
			limit = {
				exists = global_var:black_horde_handed_out
			}
			every_in_list = {
				list = ilkhanate_counties
				add_to_list = black_horde_counties
			}
		}
		# Try Golden Horde
		else_if = {
			limit = {
				exists = global_var:golden_horde_handed_out
			}
			every_in_list = {
				list = ilkhanate_counties
				add_to_list = golden_horde_counties
			}
		}
		# Try Baruun
		else_if = {
			limit = {
				exists = global_var:baruun_khanate_handed_out
			}
			every_in_list = {
				list = ilkhanate_counties
				add_to_list = baruun_khanate_counties
			}
		}
	}
	### End Ilkhanate

	### Golden Horde
	if = {
		limit = {
			NOT = { exists = global_var:golden_horde_handed_out }
			any_in_list = {
				list = golden_horde_counties
				count > 0
			}
		}
		# Try giving them to White Horde
		if = {
			limit = {
				exists = global_var:white_horde_handed_out
			}
			every_in_list = {
				list = golden_horde_counties
				add_to_list = white_horde_counties
			}
		}
		# Try Aarlud Khanate
		else_if = {
			limit = {
				exists = global_var:aarlud_khanate_handed_out
			}
			every_in_list = {
				list = golden_horde_counties
				add_to_list = aarlud_khanate_counties
			}
		}
		# Try Chagatai
		else_if = {
			limit = {
				exists = global_var:chagatai_handed_out
			}
			every_in_list = {
				list = golden_horde_counties
				add_to_list = chagatai_counties
			}
		}
		# Try Ilkhanate
		else_if = {
			limit = {
				exists = global_var:ilkhanate_handed_out
			}
			every_in_list = {
				list = golden_horde_counties
				add_to_list = ilkhanate_counties
			}
		}
	}
	### End Golden Horde

	### Chagatai
	if = {
		limit = {
			NOT = { exists = global_var:chagatai_handed_out }
			any_in_list = {
				list = chagatai_counties
				count > 0
			}
		}
		# Try giving them to Golden Horde
		if = {
			limit = {
				exists = global_var:golden_horde_handed_out
			}
			every_in_list = {
				list = chagatai_counties
				add_to_list = golden_horde_counties
			}
		}
		# Try Ilkhanate
		else_if = {
			limit = {
				exists = global_var:ilkhanate_handed_out
			}
			every_in_list = {
				list = chagatai_counties
				add_to_list = ilkhanate_counties
			}
		}
	}
	### End Chagatai

	### Black Horde
	if = {
		limit = {
			NOT = { exists = global_var:black_horde_handed_out }
			any_in_list = {
				list = black_horde_counties
				count > 0
			}
		}
		# Try giving them to Ilkhanate
		if = {
			limit = {
				exists = global_var:ilkhanate_handed_out
			}
			every_in_list = {
				list = black_horde_counties
				add_to_list = ilkhanate_counties
			}
		}
		# Try Chagatai
		else_if = {
			limit = {
				exists = global_var:chagatai_handed_out
			}
			every_in_list = {
				list = black_horde_counties
				add_to_list = chagatai_counties
			}
		}
		# Try Baruun
		else_if = {
			limit = {
				exists = global_var:baruun_khanate_handed_out
			}
			every_in_list = {
				list = black_horde_counties
				add_to_list = baruun_khanate_counties
			}
		}
	}
	### End Black Horde

	### White Horde
	if = {
		limit = {
			NOT = { exists = global_var:white_horde_handed_out }
			any_in_list = {
				list = white_horde_counties
				count > 0
			}
		}
		# Try giving them to Golden Horde
		if = {
			limit = {
				exists = global_var:golden_horde_handed_out
			}
			every_in_list = {
				list = white_horde_counties
				add_to_list = golden_horde_counties
			}
		}
		# Try Ilkhanate
		else_if = {
			limit = {
				exists = global_var:ilkhanate_handed_out
			}
			every_in_list = {
				list = white_horde_counties
				add_to_list = ilkhanate_counties
			}
		}
		# Try Aarlud Khanate
		else_if = {
			limit = {
				exists = global_var:aarlud_khanate_handed_out
			}
			every_in_list = {
				list = white_horde_counties
				add_to_list = aarlud_khanate_counties
			}
		}
		# Try Togskol
		else_if = {
			limit = {
				exists = global_var:togskol_khanate_handed_out
			}
			every_in_list = {
				list = white_horde_counties
				add_to_list = togskol_khanate_counties
			}
		}
		# Try Chagatai
		else_if = {
			limit = {
				exists = global_var:chagatai_handed_out
			}
			every_in_list = {
				list = white_horde_counties
				add_to_list = chagatai_counties
			}
		}
	}
	### End White Horde

	### Aarlud Khanate
	if = {
		limit = {
			NOT = { exists = global_var:aarlud_khanate_handed_out }
			any_in_list = {
				list = aarlud_khanate_counties
				count > 0
			}
		}
		# Try giving them to White Horde
		if = {
			limit = {
				exists = global_var:white_horde_handed_out
			}
			every_in_list = {
				list = aarlud_khanate_counties
				add_to_list = white_horde_counties
			}
		}
		# Try Golden Horde
		else_if = {
			limit = {
				exists = global_var:golden_horde_handed_out
			}
			every_in_list = {
				list = aarlud_khanate_counties
				add_to_list = golden_horde_counties
			}
		}
		# Try Togskol
		else_if = {
			limit = {
				exists = global_var:togskol_khanate_handed_out
			}
			every_in_list = {
				list = aarlud_khanate_counties
				add_to_list = togskol_khanate_counties
			}
		}
	}
	### End Aarlud Khanate

	### Togskol Khanate
	if = {
		limit = {
			NOT = { exists = global_var:togskol_khanate_handed_out }
			any_in_list = {
				list = togskol_khanate_counties
				count > 0
			}
		}
		# Try giving them to White Horde
		if = {
			limit = {
				exists = global_var:white_horde_handed_out
			}
			every_in_list = {
				list = togskol_khanate_counties
				add_to_list = white_horde_counties
			}
		}
		# Try Aarlud
		else_if = {
			limit = {
				exists = global_var:aarlud_khanate_handed_out
			}
			every_in_list = {
				list = togskol_khanate_counties
				add_to_list = aarlud_khanate_counties
			}
		}
		# Try Golden Horde
		else_if = {
			limit = {
				exists = global_var:golden_horde_handed_out
			}
			every_in_list = {
				list = togskol_khanate_counties
				add_to_list = golden_horde_counties
			}
		}
		# Try Baruun
		else_if = {
			limit = {
				exists = global_var:baruun_khanate_handed_out
			}
			every_in_list = {
				list = togskol_khanate_counties
				add_to_list = baruun_khanate_counties
			}
		}
	}
	### End Togskol Khanate

	### Baruun Khanate
	if = {
		limit = {
			NOT = { exists = global_var:baruun_khanate_handed_out }
			any_in_list = {
				list = baruun_khanate_counties
				count > 0
			}
		}
		# Try giving them to Ilkhanate
		if = {
			limit = {
				exists = global_var:ilkhanate_handed_out
			}
			every_in_list = {
				list = baruun_khanate_counties
				add_to_list = ilkhanate_counties
			}
		}
		# Try Togskol
		else_if = {
			limit = {
				exists = global_var:togskol_khanate_handed_out
			}
			every_in_list = {
				list = baruun_khanate_counties
				add_to_list = togskol_khanate_counties
			}
		}
		# Try Black Horde
		else_if = {
			limit = {
				exists = global_var:black_horde_handed_out
			}
			every_in_list = {
				list = baruun_khanate_counties
				add_to_list = black_horde_counties
			}
		}
	}
	### End Baruun Khanate
}

# Set up all vassals and assign them flags for the "correct" Khanate
set_correct_mongol_breakup_vassalage_effect = {
	scope:old_mongol_emperor = {
		every_vassal = {
			limit = {
				NOT = { is_in_list = selected_mongol_heirs }
				is_playable_character = yes
				highest_held_title_tier >= tier_county
			}
			debug_log = "Trying to assign correct vassalage placement for:"
			debug_log_scopes = no

			save_scope_as = vassal
			
			# Note: the variables for counting counties is set before checking whether the relevant title will be created, to prevent null-reference errors later
			set_global_variable = {
				name = ilkhanate_county_count
				value = 0
			}
			if = { # Count Ilkhanate Counties
				limit = {
					exists = global_var:ilkhanate_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = ilkhanate_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = ilkhanate_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = golden_horde_county_count
				value = 0
			}
			if = { # Count Golden Horde Counties
				limit = {
					exists = global_var:golden_horde_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = golden_horde_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = golden_horde_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = chagatai_county_count
				value = 0
			}
			if = { # Count Chagatai Counties
				limit = {
					exists = global_var:chagatai_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = chagatai_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = chagatai_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = black_horde_county_count
				value = 0
			}
			if = { # Count Black Horde counties
				limit = {
					exists = global_var:black_horde_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = black_horde_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = black_horde_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = white_horde_county_count
				value = 0
			}
			if = { # Count White Horde counties
				limit = {
					exists = global_var:white_horde_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = white_horde_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = white_horde_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = aarlud_khanate_county_count
				value = 0
			}
			if = { # Count Aarlud Khanate counties
				limit = {
					exists = global_var:aarlud_khanate_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = aarlud_khanate_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = aarlud_khanate_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = togskol_khanate_county_count
				value = 0
			}
			if = { # Count Tögsköl Khanate counties
				limit = {
					exists = global_var:togskol_khanate_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = togskol_khanate_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = togskol_khanate_county_count
							add = 1
						}
					}
				}
			}
			set_global_variable = {
				name = baruun_khanate_county_count
				value = 0
			}
			if = { # Count Baruun Khanate counties
				limit = {
					exists = global_var:baruun_khanate_handed_out
				}
				every_sub_realm_county = {
					limit = {
						is_in_list = baruun_khanate_counties
					}
					scope:vassal = {
						change_global_variable = {
							name = baruun_khanate_county_count
							add = 1
						}
					}
				}
			}
			# Find any counties not in any lists
			set_global_variable = {
				name = leftover_mongol_county_count
				value = 0
			}
			every_sub_realm_county = {
				limit = {
					trigger_if = {
						limit = { exists = global_var:ilkhanate_handed_out }
						NOT = { is_in_list = ilkhanate_counties }
					}
					trigger_if = {
						limit = { exists = global_var:golden_horde_handed_out }
						NOT = { is_in_list = golden_horde_counties }
					}
					trigger_if = {
						limit = { exists = global_var:chagatai_handed_out }
						NOT = { is_in_list = chagatai_counties }
					}
					trigger_if = {
						limit = { exists = global_var:black_horde_handed_out }
						NOT = { is_in_list = black_horde_counties }
					}
					trigger_if = {
						limit = { exists = global_var:white_horde_handed_out }
						NOT = { is_in_list = white_horde_counties }
					}
					trigger_if = {
						limit = { exists = global_var:aarlud_khanate_handed_out }
						NOT = { is_in_list = aarlud_khanate_counties }
					}
					trigger_if = {
						limit = { exists = global_var:togskol_khanate_handed_out }
						NOT = { is_in_list = togskol_khanate_counties }
					}
					trigger_if = {
						limit = { exists = global_var:baruun_khanate_handed_out }
						NOT = { is_in_list = baruun_khanate_counties }
					}
				}
				scope:vassal = {
					change_global_variable = {
						name = leftover_mongol_county_count
						add = 1
					}
				}
			}

			### Mark vassals for the correct liege empire
			if = {
				limit = {
					global_var:leftover_mongol_county_count >= global_var:ilkhanate_county_count
					global_var:leftover_mongol_county_count >= global_var:golden_horde_county_count
					global_var:leftover_mongol_county_count >= global_var:chagatai_county_count
					global_var:leftover_mongol_county_count >= global_var:black_horde_county_count
					global_var:leftover_mongol_county_count >= global_var:white_horde_county_count
					global_var:leftover_mongol_county_count >= global_var:aarlud_khanate_county_count
					global_var:leftover_mongol_county_count >= global_var:togskol_khanate_county_count
					global_var:leftover_mongol_county_count >= global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_mongolia
			}
			else_if = {
				limit = {
					global_var:ilkhanate_county_count > global_var:leftover_mongol_county_count
					global_var:ilkhanate_county_count > global_var:golden_horde_county_count
					global_var:ilkhanate_county_count > global_var:chagatai_county_count
					global_var:ilkhanate_county_count > global_var:black_horde_county_count
					global_var:ilkhanate_county_count > global_var:white_horde_county_count
					global_var:ilkhanate_county_count > global_var:aarlud_khanate_county_count
					global_var:ilkhanate_county_count > global_var:togskol_khanate_county_count
					global_var:ilkhanate_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_ilkhanate
			}
			else_if = {
				limit = {
					global_var:golden_horde_county_count > global_var:leftover_mongol_county_count
					global_var:golden_horde_county_count > global_var:ilkhanate_county_count
					global_var:golden_horde_county_count > global_var:chagatai_county_count
					global_var:golden_horde_county_count > global_var:black_horde_county_count
					global_var:golden_horde_county_count > global_var:white_horde_county_count
					global_var:golden_horde_county_count > global_var:aarlud_khanate_county_count
					global_var:golden_horde_county_count > global_var:togskol_khanate_county_count
					global_var:golden_horde_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_golden_horde
			}
			else_if = {
				limit = {
					global_var:chagatai_county_count > global_var:leftover_mongol_county_count
					global_var:chagatai_county_count > global_var:ilkhanate_county_count
					global_var:chagatai_county_count > global_var:golden_horde_county_count
					global_var:chagatai_county_count > global_var:black_horde_county_count
					global_var:chagatai_county_count > global_var:white_horde_county_count
					global_var:chagatai_county_count > global_var:aarlud_khanate_county_count
					global_var:chagatai_county_count > global_var:togskol_khanate_county_count
					global_var:chagatai_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_chagatai
			}
			else_if = {
				limit = {
					global_var:black_horde_county_count > global_var:leftover_mongol_county_count
					global_var:black_horde_county_count > global_var:ilkhanate_county_count
					global_var:black_horde_county_count > global_var:golden_horde_county_count
					global_var:black_horde_county_count > global_var:chagatai_county_count
					global_var:black_horde_county_count > global_var:white_horde_county_count
					global_var:black_horde_county_count > global_var:aarlud_khanate_county_count
					global_var:black_horde_county_count > global_var:togskol_khanate_county_count
					global_var:black_horde_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_black_horde
			}
			else_if = {
				limit = {
					global_var:white_horde_county_count > global_var:leftover_mongol_county_count
					global_var:white_horde_county_count > global_var:ilkhanate_county_count
					global_var:white_horde_county_count > global_var:golden_horde_county_count
					global_var:white_horde_county_count > global_var:chagatai_county_count
					global_var:white_horde_county_count > global_var:black_horde_county_count
					global_var:white_horde_county_count > global_var:aarlud_khanate_county_count
					global_var:white_horde_county_count > global_var:togskol_khanate_county_count
					global_var:white_horde_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_white_horde
			}
			else_if = {
				limit = {
					global_var:aarlud_khanate_county_count > global_var:leftover_mongol_county_count
					global_var:aarlud_khanate_county_count > global_var:ilkhanate_county_count
					global_var:aarlud_khanate_county_count > global_var:golden_horde_county_count
					global_var:aarlud_khanate_county_count > global_var:chagatai_county_count
					global_var:aarlud_khanate_county_count > global_var:black_horde_county_count
					global_var:aarlud_khanate_county_count > global_var:white_horde_county_count
					global_var:aarlud_khanate_county_count > global_var:togskol_khanate_county_count
					global_var:aarlud_khanate_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_aarlud_khanate
			}
			else_if = {
				limit = {
					global_var:togskol_khanate_county_count > global_var:leftover_mongol_county_count
					global_var:togskol_khanate_county_count > global_var:ilkhanate_county_count
					global_var:togskol_khanate_county_count > global_var:golden_horde_county_count
					global_var:togskol_khanate_county_count > global_var:chagatai_county_count
					global_var:togskol_khanate_county_count > global_var:black_horde_county_count
					global_var:togskol_khanate_county_count > global_var:white_horde_county_count
					global_var:togskol_khanate_county_count > global_var:aarlud_khanate_county_count
					global_var:togskol_khanate_county_count > global_var:baruun_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_togskol_khanate
			}
			else_if = {
				limit = {
					global_var:baruun_khanate_county_count > global_var:leftover_mongol_county_count
					global_var:baruun_khanate_county_count > global_var:ilkhanate_county_count
					global_var:baruun_khanate_county_count > global_var:golden_horde_county_count
					global_var:baruun_khanate_county_count > global_var:chagatai_county_count
					global_var:baruun_khanate_county_count > global_var:black_horde_county_count
					global_var:baruun_khanate_county_count > global_var:white_horde_county_count
					global_var:baruun_khanate_county_count > global_var:aarlud_khanate_county_count
					global_var:baruun_khanate_county_count > global_var:togskol_khanate_county_count
				}
				add_character_flag = should_be_vassalized_by_baruun_khanate
			}
			else = {
				add_character_flag = should_be_vassalized_by_mongolia
			}
		}
	}
}

# Forcibly shift counties around as appropriate
shift_counties_on_mongol_succession_effect = {
	every_in_list = {
		list = selected_mongol_heirs
		every_held_title = {
			limit = {
				tier = tier_county
			}
			if = {
				limit = {
					exists = global_var:ilkhanate_handed_out
					is_in_list = ilkhanate_counties
					NOT = { holder = { has_title = title:e_ilkhanate } }
				}
				add_to_list = ilkhanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = ilkhanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:golden_horde_handed_out
					is_in_list = golden_horde_counties
					NOT = { holder = { has_title = title:e_golden_horde } }
				}
				add_to_list = golden_horde_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = golden_horde_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:chagatai_handed_out
					is_in_list = chagatai_counties
					NOT = { holder = { has_title = title:e_chagatai } }
				}
				add_to_list = chagatai_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = chagatai_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:black_horde_handed_out
					is_in_list = black_horde_counties
					NOT = { holder = { has_title = title:e_black_horde } }
				}
				add_to_list = black_horde_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = black_horde_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:white_horde_handed_out
					is_in_list = white_horde_counties
					NOT = { holder = { has_title = title:e_white_horde } }
				}
				add_to_list = white_horde_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = white_horde_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:aarlud_khanate_handed_out
					is_in_list = aarlud_khanate_counties
					NOT = { holder = { has_title = title:e_aarlud_khanate } }
				}
				add_to_list = aarlud_khanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = aarlud_khanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:togskol_khanate_handed_out
					is_in_list = togskol_khanate_counties
					NOT = { holder = { has_title = title:e_togskol_khanate } }
				}
				add_to_list = togskol_khanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = togskol_khanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:baruun_khanate_handed_out
					is_in_list = baruun_khanate_counties
					NOT = { holder = { has_title = title:e_baruun_khanate } }
				}
				add_to_list = baruun_khanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = baruun_khanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
		}
	}
	scope:old_mongol_emperor = {
		every_held_title = {
			limit = {
				tier = tier_county
				#NOT = { this = scope:old_mongol_emperor.capital_county }
			}
			if = {
				limit = {
					exists = global_var:ilkhanate_handed_out
					is_in_list = ilkhanate_counties
					NOT = { holder = { has_title = title:e_ilkhanate } }
				}
				add_to_list = ilkhanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = ilkhanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:golden_horde_handed_out
					is_in_list = golden_horde_counties
					NOT = { holder = { has_title = title:e_golden_horde } }
				}
				add_to_list = golden_horde_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = golden_horde_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:chagatai_handed_out
					is_in_list = chagatai_counties
					NOT = { holder = { has_title = title:e_chagatai } }
				}
				add_to_list = chagatai_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = chagatai_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:black_horde_handed_out
					is_in_list = black_horde_counties
					NOT = { holder = { has_title = title:e_black_horde } }
				}
				add_to_list = black_horde_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = black_horde_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:white_horde_handed_out
					is_in_list = white_horde_counties
					NOT = { holder = { has_title = title:e_white_horde } }
				}
				add_to_list = white_horde_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = white_horde_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:aarlud_khanate_handed_out
					is_in_list = aarlud_khanate_counties
					NOT = { holder = { has_title = title:e_aarlud_khanate } }
				}
				add_to_list = aarlud_khanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = aarlud_khanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:togskol_khanate_handed_out
					is_in_list = togskol_khanate_counties
					NOT = { holder = { has_title = title:e_togskol_khanate } }
				}
				add_to_list = togskol_khanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = togskol_khanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
			if = {
				limit = {
					exists = global_var:baruun_khanate_handed_out
					is_in_list = baruun_khanate_counties
					NOT = { holder = { has_title = title:e_baruun_khanate } }
				}
				add_to_list = baruun_khanate_titles_for_transfer
				# Also mark duchies for transfer if relevant
				if = {
					limit = {
						exists = duchy.holder
						holder = duchy.holder
					}
					duchy = {
						add_to_list = baruun_khanate_titles_for_transfer
					}
				}
				# TODO: Kingdoms??
			}
		}
	}
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = mongol_succession
		add_claim_on_loss = no
	}
	
	# Ilkhanate
	if = {
		limit = {
			exists = scope:ilkhanate_ruler
		}
		every_in_list = {
			list = ilkhanate_titles_for_transfer
			change_title_holder = {
				holder = scope:ilkhanate_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_ilkhanate
				}
				change_liege = {
					liege = scope:ilkhanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Golden Horde
	if = {
		limit = {
			exists = scope:golden_horde_ruler
		}
		every_in_list = {
			list = golden_horde_titles_for_transfer
			change_title_holder = {
				holder = scope:golden_horde_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_golden_horde
				}
				change_liege = {
					liege = scope:golden_horde_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Chagatai
	if = {
		limit = {
			exists = scope:chagatai_ruler
		}
		every_in_list = {
			list = chagatai_titles_for_transfer
			change_title_holder = {
				holder = scope:chagatai_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_chagatai
				}
				change_liege = {
					liege = scope:chagatai_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Black Horde
	if = {
		limit = {
			exists = scope:black_horde_ruler
		}
		every_in_list = {
			list = black_horde_titles_for_transfer
			change_title_holder = {
				holder = scope:black_horde_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_black_horde
				}
				change_liege = {
					liege = scope:black_horde_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# White Horde
	if = {
		limit = {
			exists = scope:white_horde_ruler
		}
		every_in_list = {
			list = white_horde_titles_for_transfer
			change_title_holder = {
				holder = scope:white_horde_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_white_horde
				}
				change_liege = {
					liege = scope:white_horde_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Aarlud Khanate
	if = {
		limit = {
			exists = scope:aarlud_khanate_ruler
		}
		every_in_list = {
			list = aarlud_khanate_titles_for_transfer
			change_title_holder = {
				holder = scope:aarlud_khanate_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_aarlud_khanate
				}
				change_liege = {
					liege = scope:aarlud_khanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Togskol Khanate
	if = {
		limit = {
			exists = scope:togskol_khanate_ruler
		}
		every_in_list = {
			list = togskol_khanate_titles_for_transfer
			change_title_holder = {
				holder = scope:togskol_khanate_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_togskol_khanate
				}
				change_liege = {
					liege = scope:togskol_khanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Baruun Khanate
	if = {
		limit = {
			exists = scope:baruun_khanate_ruler
		}
		every_in_list = {
			list = baruun_khanate_titles_for_transfer
			change_title_holder = {
				holder = scope:baruun_khanate_ruler
				change = scope:mongol_succession
			}
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_baruun_khanate
				}
				change_liege = {
					liege = scope:baruun_khanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Mongolia
	if = {
		limit = {
			exists = scope:mongolia_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal_or_below = {
				limit = {
					has_character_flag = should_be_vassalized_by_mongolia
				}
				change_liege = {
					liege = scope:mongolia_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	
	# Extra backup safety
	# Ilkhanate
	if = {
		limit = {
			exists = scope:ilkhanate_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_ilkhanate_region
					}
				}
				change_liege = {
					liege = scope:ilkhanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Golden Horde
	if = {
		limit = {
			exists = scope:golden_horde_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_golden_horde_region
					}
				}
				change_liege = {
					liege = scope:golden_horde_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Chagatai
	if = {
		limit = {
			exists = scope:chagatai_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_chagatai_region
					}
				}
				change_liege = {
					liege = scope:chagatai_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Black Horde
	if = {
		limit = {
			exists = scope:black_horde_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_black_horde_region
					}
				}
				change_liege = {
					liege = scope:black_horde_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# White Horde
	if = {
		limit = {
			exists = scope:white_horde_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_white_horde_region
					}
				}
				change_liege = {
					liege = scope:white_horde_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Aarlud Khanate
	if = {
		limit = {
			exists = scope:aarlud_khanate_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_aarlud_khanate_region
					}
				}
				change_liege = {
					liege = scope:aarlud_khanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Togskol Khanate
	if = {
		limit = {
			exists = scope:togskol_khanate_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_togskol_khanate_region
					}
				}
				change_liege = {
					liege = scope:togskol_khanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Baruun Khanate
	if = {
		limit = {
			exists = scope:baruun_khanate_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						geographical_region = special_mongol_empire_baruun_khanate_region
					}
				}
				change_liege = {
					liege = scope:baruun_khanate_ruler
					change = scope:mongol_succession
				}
			}
		}
	}
	# Mongolia
	if = {
		limit = {
			exists = scope:mongolia_ruler
		}
		scope:old_mongol_emperor = {
			every_vassal = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
					capital_province = {
						NOR = {
							geographical_region = special_mongol_empire_ilkhanate_region
							geographical_region = special_mongol_empire_golden_horde_region
							geographical_region = special_mongol_empire_chagatai_region
							geographical_region = special_mongol_empire_black_horde_region
							geographical_region = special_mongol_empire_white_horde_region
							geographical_region = special_mongol_empire_aarlud_khanate_region
							geographical_region = special_mongol_empire_togskol_khanate_region
							geographical_region = special_mongol_empire_baruun_khanate_region
						} 
					}
				}
				change_liege = {
					liege = scope:mongolia_ruler
					change = scope:mongol_succession
				}
			}
		}
	}

	resolve_title_and_vassal_change = scope:mongol_succession
	
	# Extra-extra cleanup
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = mongol_cleanup_succession
		add_claim_on_loss = no
	}
	# Mongolia
	if = {
		limit = {
			exists = scope:mongolia_ruler
		}
		scope:mongolia_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Ilkhanate
	if = {
		limit = {
			exists = scope:ilkhanate_ruler
		}
		scope:ilkhanate_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Golden Horde
	if = {
		limit = {
			exists = scope:golden_horde_ruler
		}
		scope:golden_horde_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Chagatai
	if = {
		limit = {
			exists = scope:chagatai_ruler
		}
		scope:chagatai_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Black Horde
	if = {
		limit = {
			exists = scope:black_horde_ruler
		}
		scope:black_horde_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# White Horde
	if = {
		limit = {
			exists = scope:white_horde_ruler
		}
		scope:white_horde_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Aarlud Khanate
	if = {
		limit = {
			exists = scope:aarlud_khanate_ruler
		}
		scope:aarlud_khanate_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Togskol Khanate
	if = {
		limit = {
			exists = scope:togskol_khanate_ruler
		}
		scope:togskol_khanate_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_liege = {
						liege = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_baruun_khanate_region
						}
						exists = scope:baruun_khanate_ruler
					}
					change_title_holder = {
						holder = scope:baruun_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	# Baruun Khanate
	if = {
		limit = {
			exists = scope:baruun_khanate_ruler
		}
		scope:mongolia_ruler = {
			every_vassal_or_below = {
				limit = {
					NOR = {
						has_character_flag = should_be_vassalized_by_ilkhanate
						has_character_flag = should_be_vassalized_by_golden_horde
						has_character_flag = should_be_vassalized_by_chagatai
						has_character_flag = should_be_vassalized_by_black_horde
						has_character_flag = should_be_vassalized_by_white_horde
						has_character_flag = should_be_vassalized_by_aarlud_khanate
						has_character_flag = should_be_vassalized_by_togskol_khanate
						has_character_flag = should_be_vassalized_by_baruun_khanate
						has_character_flag = should_be_vassalized_by_mongolia
					}
				}
				if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_liege = {
						liege = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_liege = {
						liege = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_liege = {
						liege = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_liege = {
						liege = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_liege = {
						liege = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_liege = {
						liege = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_liege = {
						liege = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						capital_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_liege = {
						liege = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
			every_held_title = {
				limit = {
					tier = tier_county
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_ilkhanate_region
						}
						exists = scope:ilkhanate_ruler
					}
					change_title_holder = {
						holder = scope:ilkhanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_golden_horde_region
						}
						exists = scope:golden_horde_ruler
					}
					change_title_holder = {
						holder = scope:golden_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_chagatai_region
						}
						exists = scope:chagatai_ruler
					}
					change_title_holder = {
						holder = scope:chagatai_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_black_horde_region
						}
						exists = scope:black_horde_ruler
					}
					change_title_holder = {
						holder = scope:black_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_white_horde_region
						}
						exists = scope:white_horde_ruler
					}
					change_title_holder = {
						holder = scope:white_horde_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_aarlud_khanate_region
						}
						exists = scope:aarlud_khanate_ruler
					}
					change_title_holder = {
						holder = scope:aarlud_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_togskol_khanate_region
						}
						exists = scope:togskol_khanate_ruler
					}
					change_title_holder = {
						holder = scope:togskol_khanate_ruler
						change = scope:mongol_cleanup_succession
					}
				}
				else_if = {
					limit = {
						title_province = {
							NOR = {
								geographical_region = special_mongol_empire_ilkhanate_region
								geographical_region = special_mongol_empire_golden_horde_region
								geographical_region = special_mongol_empire_chagatai_region
								geographical_region = special_mongol_empire_black_horde_region
								geographical_region = special_mongol_empire_white_horde_region
								geographical_region = special_mongol_empire_aarlud_khanate_region
								geographical_region = special_mongol_empire_togskol_khanate_region
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						exists = scope:mongolia_ruler
					}
					change_title_holder = {
						holder = scope:mongolia_ruler
						change = scope:mongol_cleanup_succession
					}
				}
			}
		}
	}
	resolve_title_and_vassal_change = scope:mongol_cleanup_succession
	
	every_in_list = { # If tengri, switch to an organized faith in your new realm
		list = selected_mongol_heirs
		limit = {
			primary_title.tier = tier_empire
			faith = { has_doctrine_parameter = unreformed }
			any_sub_realm_county = {
				faith = {
					NOT = { has_doctrine_parameter = unreformed }
				}
			}
		}
		save_scope_as = faith_changer
		random_sub_realm_county = {
			limit = {
				faith = {
					NOT = { has_doctrine_parameter = unreformed }
				}
			}
			weight = {
				base = 1
				modifier = {
					save_temporary_scope_as = county_faith_check
					add = mongol_successor_faith_chance
				}
			}
			scope:faith_changer = {
				set_character_faith = prev.faith
			}
		}
	}
}
	
	
### END Title creation effects
	
grant_suitable_title_to_candidate_effect = {
	save_temporary_scope_as = candidate
	
	# Runs through available titles and grants an available one
	random_list = {
		# Ilkhanate
		1 = {
			trigger = {
				exists = global_var:creatable_ilkhanate
				NOT = { exists = global_var:ilkhanate_handed_out }
				NOT = { exists = title:e_ilkhanate.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_ilkhanate_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_ilkhanate_effect = yes
		}
		# Golden Horde
		1 = {
			trigger = {
				exists = global_var:creatable_golden_horde
				NOT = { exists = global_var:golden_horde_handed_out }
				NOT = { exists = title:e_golden_horde.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_golden_horde_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_golden_horde_effect = yes
		}
		# Chagatai
		1 = {
			trigger = {
				exists = global_var:creatable_chagatai
				NOT = { exists = global_var:chagatai_handed_out }
				NOT = { exists = title:e_chagatai.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_chagatai_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_chagatai_effect = yes
		}
		# Black Horde
		1 = {
			trigger = {
				exists = global_var:creatable_black_horde
				NOT = { exists = global_var:black_horde_handed_out }
				NOT = { exists = title:e_black_horde.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_black_horde_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_black_horde_effect = yes
		}
		# White Horde
		1 = {
			trigger = {
				exists = global_var:creatable_white_horde
				NOT = { exists = global_var:white_horde_handed_out }
				NOT = { exists = title:e_white_horde.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_white_horde_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_white_horde_effect = yes
		}
		# Aarlud Khanate
		1 = {
			trigger = {
				exists = global_var:creatable_aarlud_khanate
				NOT = { exists = global_var:aarlud_khanate_handed_out }
				NOT = { exists = title:e_aarlud_khanate.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_aarlud_khanate_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_aarlud_khanate_effect = yes
		}
		# Tögsköl Khanate
		1 = {
			trigger = {
				exists = global_var:creatable_togskol_khanate
				NOT = { exists = global_var:togskol_khanate_handed_out }
				NOT = { exists = title:e_togskol_khanate.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_togskol_khanate_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_togskol_khanate_effect = yes
		}
		# Baruun Khanate
		1 = {
			trigger = {
				exists = global_var:creatable_baruun_khanate
				NOT = { exists = global_var:baruun_khanate_handed_out }
				NOT = { exists = title:e_baruun_khanate.holder }
			}
			# Weight the chance by how much territory you hold there
			modifier = {
				add = {
					every_sub_realm_county = {
						limit = {
							title_province = {
								geographical_region = special_mongol_empire_baruun_khanate_region
							}
						}
						add = 50
					}
				}
				always = yes
			}
			grant_baruun_khanate_effect = yes
		}
	}

	# Add to list so we can move related titles around later
	add_to_list = candidate_granted_title
}


break_up_mongol_empire_effect = { # Intended to run in the Mongol Emperor's scope
	save_scope_as = old_mongol_emperor
	
	every_held_title = { # Clean up excess kingdoms
		limit = {
			OR = {
				tier = tier_kingdom
				tier = tier_duchy
			}
		}
		scope:old_mongol_emperor = {
			destroy_title = prev
		}
	}

	every_character_war = {
		limit = {
			primary_attacker = scope:old_mongol_emperor
		}
		end_war = white_peace
	}

	set_global_variable = {
		name = number_of_mongol_heir_candidates
		value = 0
	}
	# Find suitable heir candidates
	dynasty = {
		every_dynasty_member = { # Every potential khandidate
			limit = {
				character_gender_can_inherit_from_trigger = {
					CHARACTER = scope:old_mongol_emperor
				}
				is_alive = yes
			}
			change_global_variable = {
				name = number_of_mongol_heir_candidates
				add = 1
			}
			add_to_list = mongol_heir_candidates
		}
	}

	# Gather Ilkhanate counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_ilkhanate_region
			}
		}
		add_to_temporary_list = ilkhanate_counties
	}
	# Gather Golden Horde counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_golden_horde_region
			}
		}
		add_to_temporary_list = golden_horde_counties
	}
	# Gather Chagatai counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_chagatai_region
			}
		}
		add_to_temporary_list = chagatai_counties
	}
	# Gather Black Horde counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_black_horde_region
			}
		}
		add_to_temporary_list = black_horde_counties
	}
	# Gather White Horde counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_white_horde_region
			}
		}
		add_to_temporary_list = white_horde_counties
	}
	# Gather Aarlud Khanate counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_aarlud_khanate_region
			}
		}
		add_to_temporary_list = aarlud_khanate_counties
	}
	# Gather Tögsköl Khanate counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_togskol_khanate_region
			}
		}
		add_to_temporary_list = togskol_khanate_counties
	}
	# Gather Baruun Khanate counties
	every_sub_realm_county = {
		limit = {
			title_province = {
				geographical_region = special_mongol_empire_baruun_khanate_region
			}
		}
		add_to_temporary_list = baruun_khanate_counties
	}

	# Check which titles are creatable
	set_global_variable = {
		name = creatable_mongol_successor_states_number
		value = 0
	}
	# Ilkhanate
	if = {
		limit = {
			any_in_list = {
				list = ilkhanate_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Ilkhanate is creatable"
		set_global_variable = {
			name = creatable_ilkhanate
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# Golden Horde
	if = {
		limit = {
			any_in_list = {
				list = golden_horde_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Golden Horde is creatable"
		set_global_variable = {
			name = creatable_golden_horde
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# Chagatai
	if = {
		limit = {
			any_in_list = {
				list = chagatai_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Chagatai is creatable"
		set_global_variable = {
			name = creatable_chagatai
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# Black Horde
	if = {
		limit = {
			any_in_list = {
				list = black_horde_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Black Horde is creatable"
		set_global_variable = {
			name = creatable_black_horde
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# White Horde
	if = {
		limit = {
			any_in_list = {
				list = white_horde_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "White Horde is creatable"
		set_global_variable = {
			name = creatable_white_horde
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# Aarlud Khanate
	if = {
		limit = {
			any_in_list = {
				list = aarlud_khanate_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Aarlud is creatable"
		set_global_variable = {
			name = creatable_aarlud_khanate
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# Tögsköl Khanate
	if = {
		limit = {
			any_in_list = {
				list = togskol_khanate_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Tögsköl is creatable"
		set_global_variable = {
			name = creatable_togskol_khanate
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}
	# Baruun Khanate
	if = {
		limit = {
			any_in_list = {
				list = baruun_khanate_counties
				count >= @creatable_successor_empire_county_threshold
			}
		}
		debug_log = "Baruun is creatable"
		set_global_variable = {
			name = creatable_baruun_khanate
			value = yes
		}
		change_global_variable = {
			name = creatable_mongol_successor_states_number
			add = 1
		}
	}

	ordered_in_list = {
		list = mongol_heir_candidates
		order_by = mongol_breakup_succession_score
		max = 15

		debug_log = "Mongol succession list found:"
		debug_log_scopes = yes

		add_to_list = selected_mongol_heirs
	}

	# Find the "main" heir
	ordered_in_list = {
		list = selected_mongol_heirs
		order_by = mongol_breakup_succession_score
		max = 1

		debug_log = "Found Mongol main heir"
		debug_log_scopes = yes

		assign_core_mongol_titles_to_heir_effect = yes

		remove_from_list = selected_mongol_heirs
	}

	# Assign titles to everyone else
	ordered_in_list = {
		list = selected_mongol_heirs
		order_by = mongol_breakup_succession_score
		max = global_var:creatable_mongol_successor_states_number

		debug_log = "Found another Mongol Heir"
		debug_log_scopes = yes
		grant_suitable_title_to_candidate_effect = yes
		
		debug_log = "Tried granting title to heir"
	}

	pick_up_remaining_counties_effect = yes
	set_correct_mongol_breakup_vassalage_effect = yes
	shift_counties_on_mongol_succession_effect = yes

	# Send notification events
	scope:mongolia_ruler = {
		save_scope_as = successor_1
	}

	ordered_in_list = {
		list = selected_mongol_heirs
		order_by = mongol_breakup_succession_score
		max = 3

		if = {
			limit = {
				NOT = { exists = scope:successor_2 }
			}
			save_scope_as = successor_2
		}
		else_if = {
			limit = {
				NOT = { exists = scope:successor_3 }
			}
			save_scope_as = successor_3
		}
		if = {
			limit = {
				NOT = { exists = scope:successor_4 }
			}
			save_scope_as = successor_4
		}
	}

	every_player = {
		trigger_event = {
			id = mongol_invasion.1004
			days = 2 # Delay it a little to let effects settle
		}
	}

	# Cleanup
	remove_global_variable = creatable_mongol_successor_states_number
	remove_global_variable = number_of_mongol_heir_candidates
	remove_global_variable = creatable_ilkhanate
	remove_global_variable = creatable_golden_horde
	remove_global_variable = creatable_chagatai
	remove_global_variable = creatable_black_horde
	remove_global_variable = creatable_white_horde
	remove_global_variable = creatable_aarlud_khanate
	remove_global_variable = creatable_togskol_khanate
	remove_global_variable = creatable_baruun_khanate

	# Make sure the Mongol Empire title is destroyed as appropriate
	set_global_variable = {
		name = mongol_empire_was_broken_up
		value = yes
	}
	scope:successor_1 = {
		trigger_event = {
			id = mongol_invasion.9999
			days = 1
		}
	}
	
	# Assign appropriate De Jure, so that the successors do not switch away from their empires immediately
	if = {
		limit = {
			exists = scope:chagatai_ruler
		}
		scope:chagatai_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = chagatai_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:golden_horde_ruler
		}
		scope:golden_horde_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = golden_horde_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:ilkhanate_ruler
		}
		scope:ilkhanate_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = ilkhanate_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:black_horde_ruler
		}
		scope:black_horde_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = black_horde_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:white_horde_ruler
		}
		scope:white_horde_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = white_horde_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:aarlud_khanate_ruler
		}
		scope:aarlud_khanate_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = aarlud_khanate_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:togskol_khanate_ruler
		}
		scope:togskol_khanate_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = togskol_khanatede_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:baruun_khanate_ruler
		}
		scope:baruun_khanate_ruler = {
			add_realm_law_skip_effects = high_partition_succession_law
			save_scope_as = title_assignment
			every_sub_realm_county = {
				limit = {
					title_province = {
						geographical_region = baruun_khanate_de_jure_region
					}
					exists = kingdom
					OR = {
						NOT = { exists = kingdom.holder }
						AND = {
							exists = kingdom.holder
							OR = {
								kingdom.holder = scope:title_assignment
								kingdom.holder = {
									is_vassal_of = scope:title_assignment
								}
							}
						}
					}
					holder.top_liege = scope:title_assignment
				}
				if = {
					limit = {
						NOT = {
							kingdom = {
								is_in_list = assigned_kingdoms
							}
						}
					}
					kingdom = {
						set_de_jure_liege_title = scope:title_assignment.primary_title
						add_to_list = assigned_kingdoms
					}
				}
			}
		}
	}
}
