# Plus events

# Genghis Khan dies with a large enough empire to split up among sons
narrative_event = {
	id = 60002
	title = EVTNAME60002
	picture = GFX_evt_mongols

	desc = {
		text = EVTDESC60002A
		trigger = {
			ROOT = { has_character_flag = has_third_son }
		}
	}
	desc = {
		text = EVTDESC60002B
		trigger = {
			ROOT = { NOT = { has_character_flag = has_third_son } }
		}
	}

	is_triggered_only = yes
	major = yes

	only_independent = yes
	culture = mongol
	only_capable = yes
	min_age = 16
	prisoner = no

	trigger = {
		always = no
		has_landed_title = e_mongol_empire

		any_realm_title = {
			real_tier = COUNT

			location = {
				OR = {
					region = custom_cuman
					duchy = { title = d_aral }
				}
			}
		}

		any_realm_title = {
			real_tier = COUNT

			location = {
				OR = {
					region = custom_persia
					duchy = { title = d_dihistan }
				}
			}
		}

		any_realm_title = {
			tier = COUNT

			location = {
				region = custom_khwarizm

				duchy = {
					NOR = {
						title = d_aral
						title = d_dihistan
					}
				}
			}
		}

		any_child = {
			count = 2

			is_female = no
			has_inheritance_blocker = no
		}
	}

	immediate = {
		# Attempt to give titles to historical people
		random_child = {
			limit = { has_character_flag = is_jochi_khan }

			save_event_target_as = first_mongol_son
			ROOT = { set_character_flag = jochi_exists }
		}

		if = {
			limit = {
				has_character_flag = is_genghis_khan
				NOT = { has_character_flag = jochi_exists }
			}

			random_realm_character = {
				limit = {
					has_character_flag = son_of_jochi_khan
					has_inheritance_blocker = no

					NOR = {
						any_sibling = {
							is_older_than = PREV
							has_inheritance_blocker = no
						}
					}
				}

				save_event_target_as = first_mongol_son
				ROOT = { set_character_flag = jochi_exists }
			}
		}

		random_child = {
			limit = { has_character_flag = is_tolui_khan }

			save_event_target_as = second_mongol_son
			ROOT = { set_character_flag = tolui_exists }
		}

		random_child = {
			limit = {
				has_character_flag = is_chagatai_khan

				ROOT = {
					any_child = {
						count = 2

						is_female = no
						has_inheritance_blocker = no
						NOT = { has_character_flag = is_chagatai_khan }
					}
				}
			}

			save_event_target_as = third_mongol_son
			ROOT = { set_character_flag = has_third_son }
		}

		# Otherwise assign areas to children
		if = {
			limit = { NOT = { has_character_flag = jochi_exists } }

			# Give Golden Horde to a son/grandson who's already a king in Cuman
			random_vassal = {
				limit = {
					real_tier = KING

					capital_scope = {
						region = custom_cuman
					}

					OR = {
						is_child_of = ROOT

						father_even_if_dead = {
							is_alive = no
							is_child_of = ROOT
						}
					}

					has_inheritance_blocker = no

					NOR = {
						character = event_target:second_mongol_son
						character = event_target:third_mongol_son
					}
				}

				save_event_target_as = first_mongol_son
				ROOT = { set_character_flag = jochi_exists }
			}
		}

		if = {
			limit = { NOT = { has_character_flag = jochi_exists } }

			# Give Golden Horde to a son who's already a duke in cuman
			random_realm_lord = {
				limit = {
					real_tier = DUKE
					is_child_of = ROOT
					is_female = no
					has_inheritance_blocker = no

					capital_scope = {
						region = custom_cuman
					}

					NOR = {
						character = event_target:second_mongol_son
						character = event_target:third_mongol_son
					}
				}

				save_event_target_as = first_mongol_son
				ROOT = { set_character_flag = jochi_exists }
			}
		}

		if = {
			limit = { NOT = { has_character_flag = jochi_exists } }

			# Otherwise give Golden Horde to eldest available son
			random_child = {
				limit = {
					is_female = no
					is_alive = yes
					has_inheritance_blocker = no

					NOR = {
						character = event_target:second_mongol_son
						character = event_target:third_mongol_son

						any_sibling = {
							is_older_than = PREV
							has_inheritance_blocker = no

							NOR = {
								character = event_target:second_mongol_son
								character = event_target:third_mongol_son
							}
						}
					}
				}

				save_event_target_as = first_mongol_son
			}
		}

		if = {
			limit = { NOT = { has_character_flag = tolui_exists } }

			# Give Ilkhanate to a son/grandson who's already a king in Persia
			random_vassal = {
				limit = {
					real_tier = KING
					has_inheritance_blocker = no

					capital_scope = {
						region = custom_persia
					}

					OR = {
						is_child_of = ROOT

						father_even_if_dead = {
							is_alive = no
							is_child_of = ROOT
						}
					}

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:third_mongol_son
					}
				}

				save_event_target_as = second_mongol_son
				ROOT = { set_character_flag = tolui_exists }
			}
		}

		if = {
			limit = { NOT = { has_character_flag = tolui_exists } }

			# Give Ilkhanate to a son who's already a duke in Persia
			random_realm_lord = {
				limit = {
					is_child_of = ROOT
					real_tier = DUKE
					is_female = no
					has_inheritance_blocker = no

					capital_scope = {
						region = custom_persia
					}

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:third_mongol_son
					}
				}

				save_event_target_as = second_mongol_son
				ROOT = { set_character_flag = tolui_exists }
			}
		}

		if = {
			limit = { NOT = { has_character_flag = tolui_exists } }

			# Otherwise give Ilkhanate to eldest available son
			random_child = {
				limit = {
					is_alive = yes
					is_female = no
					has_inheritance_blocker = no

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:third_mongol_son

						any_sibling = {
							is_older_than = PREV
							has_inheritance_blocker = no

							NOR = {
								character = event_target:first_mongol_son
								character = event_target:third_mongol_son
							}
						}
					}
				}

				save_event_target_as = second_mongol_son
			}
		}

		if = {
			limit = {
				NOT = { has_character_flag = has_third_son }

				any_child = {
					is_alive = yes
					is_female = no
					has_inheritance_blocker = no

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
					}
				}
			}

			# Give Chagatai to son/grandson who's already a king in Khwarizm
			random_vassal = {
				limit = {
					real_tier = KING
					has_inheritance_blocker = no

					capital_scope = {
						region = custom_khwarizm
					}

					OR = {
						is_child_of = ROOT

						father_even_if_dead = {
							is_alive = no
							is_child_of = ROOT
						}
					}

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
					}
				}

				save_event_target_as = third_mongol_son
				ROOT = { set_character_flag = has_third_son }
			}
		}

		if = {
			limit = {
				NOT = { has_character_flag = has_third_son }

				any_child = {
					is_alive = yes
					is_female = no
					has_inheritance_blocker = no

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
					}
				}
			}

			# Give Chagatai to son who's already a duke in Khwarizm
			random_realm_lord = {
				limit = {
					is_child_of = ROOT
					real_tier = DUKE
					is_female = no
					has_inheritance_blocker = no

					capital_scope = {
						region = custom_khwarizm
					}

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
					}
				}

				save_event_target_as = third_mongol_son
				ROOT = { set_character_flag = has_third_son }
			}
		}

		if = {
			limit = {
				NOT = { has_character_flag = has_third_son }

				any_child = {
					is_alive = yes
					is_female = no
					has_inheritance_blocker = no

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
					}
				}
			}

			# Otherwise give Chagatai to eldest available son
			random_child = {
				limit = {
					is_female = no
					is_alive = yes
					has_inheritance_blocker = no

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son

						any_sibling = {
							is_older_than = PREV
							has_inheritance_blocker = no

							NOR = {
								character = event_target:first_mongol_son
								character = event_target:second_mongol_son
							}
						}
					}
				}

				save_event_target_as = third_mongol_son
				ROOT = { set_character_flag = has_third_son }
			}
		}

		clr_character_flag = jochi_exists
		clr_character_flag = tolui_exists

		# Stop any wars and disband the initial mongol forces
		any_war = {
			limit = {
				OR = {
					attacker = { character = ROOT }
					defender = { character = ROOT }
				}
			}
			end_war = invalid
		}

		disband_event_forces = first_mongol_horde

		if = {
			limit = { has_character_flag = has_third_son }
			current_heir = { set_character_flag = has_third_son }
		}

		current_heir = {
			character_event = {
				id = 60005
				days = 1
			}
		}
	}

	option = {
		name = EVTOPTA60002

		trigger = { character = ROOT }
	}

	option = {
		name = EVTOPTB60002

		trigger = {
			OR = {
				culture = mongol
				same_realm = ROOT
			}

			NOT = { character = ROOT }
		}
	}

	option = {
		name = EVTOPTC60002

		trigger = {
			NOR = {
				character = ROOT
				culture = mongol
				same_realm = ROOT
			}
		}
	}
}

# Children/vassals/courtiers are given the option to serve one of the sons
character_event = {
	id = 60003
	picture = GFX_evt_tengri_throneroom_oldgods

	desc = {
		text = EVTDESC60003A
		trigger = { FROM = { has_character_flag = has_third_son } }
	}
	desc = {
		text = EVTDESC60003B
		trigger = { FROM = { NOT = { has_character_flag = has_third_son } } }
	}

	is_triggered_only = yes

	option = {
		name = EVTOPTA60003 # Join Golden Horde

		ai_chance = {
			factor = 50

			mult_modifier = {
				factor = 0.1

				is_ruler = yes

				NOT = {
					any_neighbor_independent_ruler = {
						has_landed_title = e_golden_horde
					}
				}
			}

			mult_modifier = {
				factor = 3

				opinion_diff = {
					first = event_target:first_mongol_son
					second = event_target:second_mongol_son
					value >= 10
					as_if_liege = yes
				}

				OR = {
					FROM = { NOT = { has_character_flag = has_third_son } }

					opinion_diff = {
						first = event_target:first_mongol_son
						second = event_target:third_mongol_son
						value >= 10
						as_if_liege = yes
					}
				}
			}

			mult_modifier = {
				factor = 2

				is_ruler = yes

				event_target:first_mongol_son = {
					any_realm_province = {
						held_under_PREV = yes

						ROOT = {
							capital_scope = {
								distance = {
									where = PREVPREV
									value >= 200
								}
							}
						}
					}
				}
			}
		}

		if = {
			limit = { is_ruler = no }

			any_spouse = {
				limit = {
					prisoner = no
					is_ruler = no
				}
				move_character = event_target:first_mongol_son
			}

			any_child = {
				limit = {
					prisoner = no
					is_adult = no
					is_ruler = no
				}
				move_character = event_target:first_mongol_son
			}

			move_character = event_target:first_mongol_son
		}

		if = {
			limit = { is_ruler = yes }
			set_defacto_liege = event_target:first_mongol_son
		}

		opinion = {
			name = opinion_loyal_vassal
			who = event_target:first_mongol_son
			years = 5
		}
	}

	option = {
		name = EVTOPTB60003 # Join Ilkhanate

		ai_chance = {
			factor = 50

			mult_modifier = {
				factor = 0.1

				is_ruler = yes

				NOT = {
					any_neighbor_independent_ruler = {
						has_landed_title = e_ilkhanate
					}
				}
			}

			mult_modifier = {
				factor = 3

				opinion_diff = {
					first = event_target:second_mongol_son
					second = event_target:first_mongol_son
					value >= 10
					as_if_liege = yes
				}

				OR = {
					FROM = { NOT = { has_character_flag = has_third_son } }

					opinion_diff = {
						first = event_target:second_mongol_son
						second = event_target:third_mongol_son
						value >= 10
						as_if_liege = yes
					}
				}
			}

			mult_modifier = {
				factor = 2

				is_ruler = yes

				event_target:second_mongol_son = {
					any_realm_province = {
						held_under_PREV = yes

						ROOT = {
							capital_scope = {
								distance = {
									where = PREVPREV
									value >= 200
								}
							}
						}
					}
				}
			}
		}

		if = {
			limit = { is_ruler = no }

			any_spouse = {
				limit = {
					prisoner = no
					is_ruler = no
				}

				move_character = event_target:second_mongol_son
			}

			any_child = {
				limit = {
					prisoner = no
					is_adult = no
					is_ruler = no
				}

				move_character = event_target:second_mongol_son
			}

			move_character = event_target:second_mongol_son
		}

		if = {
			limit = { is_ruler = yes }
			set_defacto_liege = event_target:second_mongol_son
		}

		opinion = {
			name = opinion_loyal_vassal
			who = event_target:second_mongol_son
			years = 5
		}
	}

	option = {
		name = EVTOPTC60003 # Join Chagatai

		trigger = {
			FROM = { has_character_flag = has_third_son }
		}

		ai_chance = {
			factor = 50

			modifier = {
				factor = 0.1

				is_ruler = yes

				NOT = {
					any_neighbor_independent_ruler = {
						has_landed_title = e_chagatai
					}
				}
			}

			modifier = {
				factor = 3

				opinion_diff = {
					first = event_target:third_mongol_son
					second = event_target:first_mongol_son
					value >= 10
					as_if_liege = yes
				}

				opinion_diff = {
					first = event_target:third_mongol_son
					second = event_target:second_mongol_son
					value >= 10
					as_if_liege = yes
				}
			}

			modifier = {
				factor = 2

				is_ruler = yes

				event_target:third_mongol_son = {
					any_realm_province = {
						held_under_PREV = yes

						ROOT = {
							capital_scope = {
								distance = {
									where = PREVPREV
									value >= 200
								}
							}
						}
					}
				}
			}
		}

		if = {
			limit = { is_ruler = no }

			any_spouse = {
				limit = {
					prisoner = no
					is_ruler = no
				}

				move_character = event_target:third_mongol_son
			}

			any_child = {
				limit = {
					prisoner = no
					is_adult = no
					is_ruler = no
				}

				move_character = event_target:third_mongol_son
			}

			move_character = event_target:third_mongol_son
		}

		if = {
			limit = { is_ruler = yes }
			set_defacto_liege = event_target:third_mongol_son
		}

		opinion = {
			name = opinion_loyal_vassal
			who = event_target:third_mongol_son
			years = 5
		}
	}
}

# Sons are informed of the Mongol Empire division
character_event = {
	id = 60004
	desc = EVTDESC60004
	picture = GFX_evt_tengri_throneroom_oldgods

	is_triggered_only = yes

	immediate = {
		# Son will immediately form a cadet dynasty
		FROMFROM = {
			save_event_target_as = old_dynasty_head
		}

		any_dynasty_member = {
			limit = {
				OR = {
					is_child_of = ROOT
					mother_even_if_dead = { is_child_of = ROOT }
					father_even_if_dead = { is_child_of = ROOT }
				}
			}

			character_event = {
				id = Plus.142
				days = 1
			}
		}

		dynasty = father_bastard

		if = {
			limit = {
				OR = {
					has_character_flag = is_jochi_khan

					AND = {
						has_character_flag = son_of_jochi_khan

						NOT = {
							any_independent_ruler = {
								has_character_flag = son_of_jochi_khan
								NOT = { character = ROOT }
							}
						}
					}
				}
			}

			dynasty = 7379
		}

		if = {
			limit = {
				OR = {
					has_character_flag = is_chagatai_khan

					AND = {
						has_character_flag = son_of_chagatai_khan

						NOT = {
							any_independent_ruler = {
								has_character_flag = son_of_chagatai_khan
								NOT = { character = ROOT }
							}
						}
					}
				}
			}

			dynasty = 7380
		}

		if = {
			limit = {
				OR = {
					has_character_flag = is_tolui_khan

					AND = {
						has_character_flag = son_of_tolui_khan

						NOT = {
							any_independent_ruler = {
								has_character_flag = son_of_tolui_khan
								NOT = { character = ROOT }
							}
						}
					}
				}
			}

			dynasty = 101916
		}

		if = {
			limit = {
				OR = {
					has_character_flag = is_ogodei_khan

					AND = {
						has_character_flag = son_of_ogodei_khan

						NOT = {
							any_independent_ruler = {
								has_character_flag = son_of_ogodei_khan
								NOT = { character = ROOT }
							}
						}
					}
				}
			}

			dynasty = 1000124389
		}

		if = {
			limit = { ai = no }

			chronicle = {
				entry = CHRONICLE_CADET_BRANCH
				portrait = [Root.GetID]
				picture = GFX_evt_coronation
			}
		}

		# Move capital to de jure capital, if possible
		if = {
			limit = {
				primary_title = {
					capital_scope = {
						owned_by = ROOT
						is_capital = no
						county = { is_feudal = yes }
					}
				}
			}

			primary_title = {
				capital_scope = {
					ROOT = {
						capital = PREV
					}
				}
			}
		}

		# Move tribal capital to feudal one, if de jure capital not held
		else_if = {
			limit = {
				is_feudal = no

				any_demesne_title = {
					tier = COUNT
					is_feudal = yes
				}
			}

			random_demesne_title = {
				limit = {
					real_tier = COUNT
					is_feudal = yes
				}

				ROOT = {
					capital = PREV
				}
			}
		}

		# Change succession to gavelkind if still tribal
		if = {
			limit = { is_tribal = yes }

			primary_title = {
				set_title_flag = non_manual_law_change # No prestige loss
				add_law = succ_gavelkind
				clr_title_flag = non_manual_law_change # No prestige loss
			}
		}
	}

	option = {
		name = EVTOPTA60004

		trigger = { has_landed_title = e_golden_horde }

		set_global_flag = golden_horde_arrival
		custom_tooltip = { text = ESTABLISH_CADET_BRANCH }

		# Lose all claims held by the other hordes
		any_claim = {
			show_scope_change = no

			limit = {
				holder_scope = {
					OR = {
						has_landed_title = e_ilkhanate
						has_landed_title = e_chagatai

						top_liege = {
							OR = {
								has_landed_title = e_ilkhanate
								has_landed_title = e_chagatai
							}
						}
					}
				}
			}

			remove_claim = ROOT
		}

		spawn_unit = {
			owner = THIS
			province = capital_scope
			home = capital_scope
			attrition = 0.35
			maintenance_multiplier = 0
			earmark = second_mongol_horde

			troops = {
				horse_archers = { 6750 6750 }
				light_cavalry = { 4000 4000 }
				light_infantry = { 1350 1350 }
				heavy_infantry = { 525 525 }
				knights = { 118 118 }
			}
		}

		while = {
			count = 3

			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = ROOT
				female = no
				age = 30

				attributes = {
					martial = 12
				}

				add_trait = brilliant_strategist
			}
		}

		hidden_effect = {
			# Turn over any remaining vassals
			any_vassal = {
				limit = {
					capital_scope = {
						OR = {
							region = custom_persia
							region = custom_afghanistan
							region = custom_baluchistan
							region = custom_byzantium
							region = custom_arabia
							region = custom_jerusalem
							region = custom_mesopotamia
							region = custom_syria
							region = custom_armenia
							region = custom_azerbaijan
							region = custom_nikaea
							region = custom_trebizond
							region = world_india_bengal
							region = world_india_deccan
							region = world_india_rajastan

							duchy = {
								OR = {
									title = d_merv
									title = d_dihistan
								}
							}
						}
					}
				}

				set_defacto_liege = event_target:second_mongol_son

				opinion = {
					name = opinion_loyal_vassal
					who = event_target:second_mongol_son
					years = 5
				}
			}

			any_vassal = {
				any_vassal = {
					limit = {
						capital_scope = {
							OR = {
								region = custom_persia
								region = custom_afghanistan
								region = custom_baluchistan
								region = custom_byzantium
								region = custom_arabia
								region = custom_jerusalem
								region = custom_mesopotamia
								region = custom_syria
								region = custom_armenia
								region = custom_azerbaijan
								region = custom_nikaea
								region = custom_trebizond
								region = world_india_bengal
								region = world_india_deccan
								region = world_india_rajastan

								duchy = {
									OR = {
										title = d_merv
										title = d_dihistan
									}
								}
							}
						}
					}

					set_defacto_liege = event_target:second_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:second_mongol_son
						years = 5
					}
				}
			}

			if = {
				limit = {
					FROM = {
						has_character_flag = has_third_son
					}
				}

				any_vassal = {
					limit = {
						capital_scope = {
							region = custom_khwarizm

							duchy = {
								NOT = { title = d_aral }
							}
						}
					}

					set_defacto_liege = event_target:third_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:third_mongol_son
						years = 5
					}
				}

				any_vassal = {
					any_vassal = {
						limit = {
							capital_scope = {
								region = custom_khwarizm

								duchy = {
									NOT = { title = d_aral }
								}
							}
						}

						set_defacto_liege = event_target:third_mongol_son

						opinion = {
							name = opinion_loyal_vassal
							who = event_target:third_mongol_son
							years = 5
						}
					}
				}
			}
		}
	}

	option = {
		name = EVTOPTA60004

		trigger = { has_landed_title = e_ilkhanate }

		set_global_flag = il_khanate_arrival

		custom_tooltip = { text = ESTABLISH_CADET_BRANCH }

		# Lose all claims held by the other hordes
		any_claim = {
			show_scope_change = no

			limit = {
				holder_scope = {
					OR = {
						has_landed_title = e_golden_horde
						has_landed_title = e_chagatai
						top_liege = { has_landed_title = e_golden_horde }
						top_liege = { has_landed_title = e_chagatai }
					}
				}
			}

			remove_claim = ROOT
		}

		spawn_unit = {
			owner = THIS
			province = capital_scope
			home = capital_scope
			attrition = 0.35
			maintenance_multiplier = 0
			earmark = second_mongol_horde

			troops = {
				horse_archers = { 6750 6750 }
				light_cavalry = { 4000 4000 }
				light_infantry = { 1350 1350 }
				heavy_infantry = { 525 525 }
				knights = { 118 118 }
			}
		}

		while = {
			count = 3

			create_character = {
				random_traits = yes
				dynasty = random
				religion = ROOT
				culture = ROOT
				female = no
				age = 30

				attributes = {
					martial = 12
				}

				add_trait = brilliant_strategist
			}
		}

		hidden_effect = {
			# Turn over any remaining vassals
			any_vassal = {
				limit = {
					capital_scope = {
						OR = {
							region = custom_cuman
							region = custom_perm
							region = custom_volga_bulgaria
							region = custom_alania
							region = custom_crimea
							region = custom_lithuania
							region = custom_livonia
							region = custom_muscovy
							region = custom_novgorod
							region = custom_ruthenia
							region = custom_bulgaria
							region = custom_croatia
							region = custom_serbia
							region = custom_wallachia
							duchy = { title = d_aral }
						}
					}
				}

				set_defacto_liege = event_target:first_mongol_son

				opinion = {
					name = opinion_loyal_vassal
					who = event_target:first_mongol_son
					years = 5
				}
			}

			any_vassal = {
				any_vassal = {
					limit = {
						capital_scope = {
							OR = {
								region = custom_cuman
								region = custom_perm
								region = custom_volga_bulgaria
								region = custom_alania
								region = custom_crimea
								region = custom_lithuania
								region = custom_livonia
								region = custom_muscovy
								region = custom_novgorod
								region = custom_ruthenia
								region = custom_bulgaria
								region = custom_croatia
								region = custom_serbia
								region = custom_wallachia
								duchy = { title = d_aral }
							}
						}
					}

					set_defacto_liege = event_target:first_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:first_mongol_son
						years = 5
					}
				}
			}

			if = {
				limit = { FROM = { has_character_flag = has_third_son } }

				any_vassal = {
					limit = {
						capital_scope = {
							region = custom_khwarizm

							duchy = {
								NOR = {
									title = d_merv
									title = d_dihistan
								}
							}
						}
					}

					set_defacto_liege = event_target:third_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:third_mongol_son
						years = 5
					}
				}

				any_vassal = {
					any_vassal = {
						limit = {
							capital_scope = {
								region = custom_khwarizm

								duchy = {
									NOR = {
										title = d_merv
										title = d_dihistan
									}
								}
							}
						}

						set_defacto_liege = event_target:third_mongol_son

						opinion = {
							name = opinion_loyal_vassal
							who = event_target:third_mongol_son
							years = 5
						}
					}
				}
			}
		}
	}

	option = {
		name = EVTOPTA60004

		trigger = { has_landed_title = e_chagatai }

		set_global_flag = chagatai_arrival

		if = {
			limit = { NOT = { dynasty = 7380 } }

			e_chagatai = {
				show_scope_change = no

				set_name = MOGHUL
				adjective = MOGHUL_ADJ
			}
		}

		custom_tooltip = { text = ESTABLISH_CADET_BRANCH }

		# Lose all claims held by the other hordes
		any_claim = {
			show_scope_change = no

			limit = {
				holder_scope = {
					OR = {
						has_landed_title = e_golden_horde
						has_landed_title = e_ilkhanate

						top_liege = {
							OR = {
								has_landed_title = e_golden_horde
								has_landed_title = e_ilkhanate
							}
						}
					}
				}
			}

			remove_claim = ROOT
		}

		spawn_unit = {
			owner = THIS
			province = capital_scope
			home = capital_scope
			attrition = 0.35
			maintenance_multiplier = 0
			earmark = second_mongol_horde

			troops = {
				horse_archers = { 6750 6750 }
				light_cavalry = { 4000 4000 }
				light_infantry = { 1350 1350 }
				heavy_infantry = { 525 525 }
				knights = { 118 118 }
			}
		}

		create_character = {
			random_traits = yes
			dynasty = random
			religion = ROOT
			culture = ROOT
			female = no
			age = 30

			attributes = {
				martial = 12
			}

			add_trait = brilliant_strategist
		}

		hidden_effect = {
			# Turn over any remaining vassals
			any_vassal = {
				limit = {
					capital_scope = {
						OR = {
							region = custom_cuman
							region = custom_perm
							region = custom_volga_bulgaria
							region = custom_alania
							region = custom_crimea
							region = custom_lithuania
							region = custom_livonia
							region = custom_muscovy
							region = custom_novgorod
							region = custom_ruthenia
							region = custom_bulgaria
							region = custom_croatia
							region = custom_serbia
							region = custom_wallachia
							duchy = { title = d_aral }
						}
					}
				}

				set_defacto_liege = event_target:first_mongol_son

				opinion = {
					name = opinion_loyal_vassal
					who = event_target:first_mongol_son
					years = 5
				}
			}

			any_vassal = {
				any_vassal = {
					limit = {
						capital_scope = {
							OR = {
								region = custom_cuman
								region = custom_perm
								region = custom_volga_bulgaria
								region = custom_alania
								region = custom_crimea
								region = custom_lithuania
								region = custom_livonia
								region = custom_muscovy
								region = custom_novgorod
								region = custom_ruthenia
								region = custom_bulgaria
								region = custom_croatia
								region = custom_serbia
								region = custom_wallachia
								duchy = { title = d_aral }
							}
						}
					}

					set_defacto_liege = event_target:first_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:first_mongol_son
						years = 5
					}
				}
			}

			any_vassal = {
				limit = {
					capital_scope = {
						OR = {
							region = custom_persia
							region = custom_afghanistan
							region = custom_baluchistan
							region = custom_byzantium
							region = custom_arabia
							region = custom_jerusalem
							region = custom_mesopotamia
							region = custom_syria
							region = custom_armenia
							region = custom_azerbaijan
							region = custom_nikaea
							region = custom_trebizond
							region = world_india_bengal
							region = world_india_deccan
							region = world_india_rajastan
							duchy = { title = d_merv }
							duchy = { title = d_dihistan }
						}
					}
				}

				set_defacto_liege = event_target:second_mongol_son

				opinion = {
					name = opinion_loyal_vassal
					who = event_target:second_mongol_son
					years = 5
				}
			}

			any_vassal = {
				any_vassal = {
					limit = {
						capital_scope = {
							OR = {
								region = custom_persia
								region = custom_afghanistan
								region = custom_baluchistan
								region = custom_byzantium
								region = custom_arabia
								region = custom_jerusalem
								region = custom_mesopotamia
								region = custom_syria
								region = custom_armenia
								region = custom_azerbaijan
								region = custom_nikaea
								region = custom_trebizond
								region = world_india_bengal
								region = world_india_deccan
								region = world_india_rajastan
								duchy = { title = d_merv }
								duchy = { title = d_dihistan }
							}
						}
					}

					set_defacto_liege = event_target:second_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:second_mongol_son
						years = 5
					}
				}
			}
		}
	}
}

# Bounce event to assign territories
character_event = {
	id = 60005
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		# Make sure the first son gets a county in Cuman/Khwarizmia
		if = {
			limit = {
				NOT = {
					any_realm_title = {
						real_tier = COUNT

						location = {
							region = custom_cuman
						}
					}
				}
			}

			if = {
				limit = { has_landed_title = c_aral }

				c_aral = {
					grant_title = event_target:first_mongol_son
				}
			}

			if = {
				limit = {
					c_aral = {
						holder_scope = {
							is_vassal_or_below_of = ROOT

							NOR = {
								character = event_target:first_mongol_son
								character = event_target:second_mongol_son
								character = event_target:third_mongol_son
							}
						}
					}
				}

				c_aral = {
					usurp_title_plus_barony_if_unlanded = event_target:first_mongol_son
				}
			}

			if = {
				limit = {
					event_target:first_mongol_son = {
						NOT = {
							any_demesne_title = {
								real_tier = COUNT

								location = {
									duchy = {
										title = d_aral
									}
								}
							}
						}
					}
				}

				random_realm_title = {
					limit = {
						tier = COUNT
						is_feudal = yes

						location = {
							duchy = {
								title = d_aral
							}
						}
					}

					usurp_title_plus_barony_if_unlanded = event_target:first_mongol_son
				}
			}
		}

		if = {
			limit = {
				has_landed_title = c_saray

				event_target:first_mongol_son = {
					NOT = { has_landed_title = c_saray }
				}
			}

			c_saray = {
				grant_title = event_target:first_mongol_son
			}
		}

		if = {
			limit = {
				c_saray = {
					holder_scope = {
						is_vassal_or_below_of = ROOT

						NOR = {
							character = event_target:first_mongol_son
							character = event_target:second_mongol_son
							character = event_target:third_mongol_son
						}
					}
				}
			}

			c_saray = {
				usurp_title_plus_barony_if_unlanded = event_target:first_mongol_son
			}
		}

		if = {
			limit = {
				event_target:first_mongol_son = {
					NOT = {
						any_demesne_title = {
							tier = COUNT

							location = {
								region = custom_cuman
							}
						}
					}
				}
			}

			random_realm_title = {
				limit = {
					tier = COUNT
					is_feudal = yes

					location = {
						region = custom_cuman
					}
				}

				usurp_title_plus_barony_if_unlanded = event_target:first_mongol_son
			}
		}

		# Make sure the second son gets a county in persia
		if = {
			limit = {
				has_landed_title = c_esfahan

				event_target:second_mongol_son = {
					NOT = { has_landed_title = c_esfahan }
				}
			}

			c_esfahan = {
				grant_title = event_target:second_mongol_son
			}
		}

		if = {
			limit = {
				c_esfahan = {
					holder_scope = {
						is_vassal_or_below_of = ROOT

						NOR = {
							character = event_target:first_mongol_son
							character = event_target:second_mongol_son
							character = event_target:third_mongol_son
						}
					}
				}
			}

			c_esfahan = {
				usurp_title_plus_barony_if_unlanded = event_target:second_mongol_son
			}
		}

		if = {
			limit = {
				event_target:second_mongol_son = {
					NOT = {
						any_demesne_title = {
							tier = COUNT

							location = {
								region = custom_persia
							}
						}
					}
				}
			}

			random_realm_title = {
				limit = {
					tier = COUNT
					is_feudal = yes

					OR = {
						location = { region = custom_persia }
						duchy = { title = d_dihistan }
					}
				}

				usurp_title_plus_barony_if_unlanded = event_target:second_mongol_son
			}
		}

		# Make sure the third son gets a county in khwarizm
		if = {
			limit = { has_character_flag = has_third_son }

			if = {
				limit = {
					has_landed_title = c_ili

					event_target:third_mongol_son = {
						NOT = { has_landed_title = c_ili }
					}
				}

				c_ili = {
					grant_title = event_target:third_mongol_son
				}
			}

			if = {
				limit = {
					c_ili = {
						holder_scope = {
							is_vassal_or_below_of = ROOT

							NOR = {
								character = event_target:first_mongol_son
								character = event_target:second_mongol_son
								character = event_target:third_mongol_son
							}
						}
					}
				}

				c_ili = {
					usurp_title_plus_barony_if_unlanded = event_target:third_mongol_son
				}
			}

			if = {
				limit = {
					event_target:third_mongol_son = {
						NOT = {
							any_demesne_title = {
								tier = COUNT

								location = {
									region = custom_khwarizm
								}
							}
						}
					}
				}

				random_realm_title = {
					limit = {
						tier = COUNT
						is_feudal = yes

						location = {
							region = custom_khwarizm
						}
					}

					usurp_title_plus_barony_if_unlanded = event_target:third_mongol_son
				}
			}
		}

		# Sons who have lands in another's territory should give them up
		event_target:first_mongol_son = {
			any_demesne_title = {
				limit = {
					OR = {
						location = {
							OR = {
								region = custom_persia
								region = custom_afghanistan
								region = custom_baluchistan
								region = custom_byzantium
								region = custom_arabia
								region = custom_jerusalem
								region = custom_mesopotamia
								region = custom_syria
								region = custom_armenia
								region = custom_azerbaijan
								region = custom_nikaea
								region = custom_trebizond
								region = world_india_bengal
								region = world_india_deccan
								region = world_india_rajastan

								duchy = {
									OR = {
										title = d_merv
										title = d_dihistan
									}
								}
							}
						}

						capital_scope = {
							OR = {
								region = custom_persia
								region = custom_afghanistan
								region = custom_baluchistan
								region = custom_byzantium
								region = custom_arabia
								region = custom_jerusalem
								region = custom_mesopotamia
								region = custom_syria
								region = custom_armenia
								region = custom_azerbaijan
								region = custom_nikaea
								region = custom_trebizond
								region = world_india_bengal
								region = world_india_deccan
								region = world_india_rajastan

								duchy = {
									OR = {
										title = d_merv
										title = d_dihistan
									}
								}
							}
						}

						title = d_merv
						title = d_dihistan
					}

					NOT = { tier = EMPEROR }
				}

				grant_title = event_target:second_mongol_son
			}

			if = {
				limit = { ROOT = { has_character_flag = has_third_son } }

				any_demesne_title = {
					limit = {
						OR = {
							location = { region = custom_khwarizm }
							capital_scope = { region = custom_khwarizm }
						}

						NOR = {
							duchy = { title = d_aral }
							title = d_aral
							tier = EMPEROR
						}
					}

					grant_title = event_target:third_mongol_son
				}
			}
		}

		event_target:second_mongol_son = {
			any_demesne_title = {
				limit = {
					OR = {
						location = {
							OR = {
								region = custom_cuman
								region = custom_perm
								region = custom_volga_bulgaria
								region = custom_alania
								region = custom_crimea
								region = custom_lithuania
								region = custom_livonia
								region = custom_muscovy
								region = custom_novgorod
								region = custom_ruthenia
								region = custom_bulgaria
								region = custom_croatia
								region = custom_serbia
								region = custom_wallachia
								duchy = { title = d_aral }
							}
						}

						capital_scope = {
							OR = {
								region = custom_cuman
								region = custom_perm
								region = custom_volga_bulgaria
								region = custom_alania
								region = custom_crimea
								region = custom_lithuania
								region = custom_livonia
								region = custom_muscovy
								region = custom_novgorod
								region = custom_ruthenia
								region = custom_bulgaria
								region = custom_croatia
								region = custom_serbia
								region = custom_wallachia
								duchy = { title = d_aral }
							}
						}

						title = d_aral
					}

					NOT = { real_tier = EMPEROR }
				}

				grant_title = event_target:first_mongol_son
			}

			if = {
				limit = { ROOT = { has_character_flag = has_third_son } }

				any_demesne_title = {
					limit = {
						OR = {
							location = { region = custom_khwarizm }
							capital_scope = { region = custom_khwarizm }
						}

						NOR = {
							duchy = {
								OR = {
									title = d_merv
									title = d_dihistan
								}
							}

							title = d_merv
							title = d_dihistan
							real_tier = EMPEROR
						}
					}

					grant_title = event_target:third_mongol_son
				}
			}
		}

		if = {
			limit = { has_character_flag = has_third_son }

			event_target:third_mongol_son = {
				any_demesne_title = {
					limit = {
						OR = {
							location = {
								OR = {
									region = custom_cuman
									region = custom_perm
									region = custom_volga_bulgaria
									region = custom_alania
									region = custom_crimea
									region = custom_lithuania
									region = custom_livonia
									region = custom_muscovy
									region = custom_novgorod
									region = custom_ruthenia
									region = custom_bulgaria
									region = custom_croatia
									region = custom_serbia
									region = custom_wallachia
									duchy = { title = d_aral }
								}
							}

							capital_scope = {
								OR = {
									region = custom_cuman
									region = custom_perm
									region = custom_volga_bulgaria
									region = custom_alania
									region = custom_crimea
									region = custom_lithuania
									region = custom_livonia
									region = custom_muscovy
									region = custom_novgorod
									region = custom_ruthenia
									region = custom_bulgaria
									region = custom_croatia
									region = custom_serbia
									region = custom_wallachia
									duchy = { title = d_aral }
								}
							}

							title = d_aral
						}

						NOT = { real_tier = EMPEROR }
					}

					grant_title = event_target:first_mongol_son
				}

				any_demesne_title = {
					limit = {
						OR = {
							location = {
								OR = {
									region = custom_persia
									region = custom_afghanistan
									region = custom_baluchistan
									region = custom_byzantium
									region = custom_arabia
									region = custom_jerusalem
									region = custom_mesopotamia
									region = custom_syria
									region = custom_armenia
									region = custom_azerbaijan
									region = custom_nikaea
									region = custom_trebizond
									region = world_india_bengal
									region = world_india_deccan
									region = world_india_rajastan

									duchy = {
										OR = {
											title = d_merv
											title = d_dihistan
										}
									}
								}
							}

							capital_scope = {
								OR = {
									region = custom_persia
									region = custom_afghanistan
									region = custom_baluchistan
									region = custom_byzantium
									region = custom_arabia
									region = custom_jerusalem
									region = custom_mesopotamia
									region = custom_syria
									region = custom_armenia
									region = custom_azerbaijan
									region = custom_nikaea
									region = custom_trebizond
									region = world_india_bengal
									region = world_india_deccan
									region = world_india_rajastan

									duchy = {
										OR = {
											title = d_merv
											title = d_dihistan
										}
									}
								}
							}

							title = d_merv
							title = d_dihistan
						}

						NOT = { real_tier = EMPEROR }
					}

					grant_title = event_target:second_mongol_son
				}
			}
		}

		# Split up lands
		if = {
			limit = {
				NOR = {
					character = event_target:first_mongol_son
					character = event_target:second_mongol_son
					character = event_target:third_mongol_son
				}
			}

			any_demesne_title = {
				limit = {
					OR = {
						location = {
							OR = {
								region = custom_cuman
								region = custom_perm
								region = custom_volga_bulgaria
								region = custom_alania
								region = custom_crimea
								region = custom_lithuania
								region = custom_livonia
								region = custom_muscovy
								region = custom_novgorod
								region = custom_ruthenia
								region = custom_bulgaria
								region = custom_croatia
								region = custom_serbia
								region = custom_wallachia
								duchy = { title = d_aral }
							}
						}

						capital_scope = {
							OR = {
								region = custom_cuman
								region = custom_perm
								region = custom_volga_bulgaria
								region = custom_alania
								region = custom_crimea
								region = custom_lithuania
								region = custom_livonia
								region = custom_muscovy
								region = custom_novgorod
								region = custom_ruthenia
								region = custom_bulgaria
								region = custom_croatia
								region = custom_serbia
								region = custom_wallachia
								duchy = { title = d_aral }
							}
						}

						duchy = { title = d_aral }
						title = d_aral
					}

					NOT = { real_tier = EMPEROR }
				}

				grant_title = event_target:first_mongol_son
			}

			any_demesne_title = {
				limit = {
					OR = {
						location = {
							OR = {
								region = custom_persia
								region = custom_afghanistan
								region = custom_baluchistan
								region = custom_byzantium
								region = custom_arabia
								region = custom_jerusalem
								region = custom_mesopotamia
								region = custom_syria
								region = custom_armenia
								region = custom_azerbaijan
								region = custom_nikaea
								region = custom_trebizond
								region = world_india_bengal
								region = world_india_deccan
								region = world_india_rajastan

								duchy = {
									OR = {
										title = d_merv
										title = d_dihistan
									}
								}
							}
						}

						capital_scope = {
							OR = {
								region = custom_persia
								region = custom_afghanistan
								region = custom_baluchistan
								region = custom_byzantium
								region = custom_arabia
								region = custom_jerusalem
								region = custom_mesopotamia
								region = custom_syria
								region = custom_armenia
								region = custom_azerbaijan
								region = custom_nikaea
								region = custom_trebizond
								region = world_india_bengal
								region = world_india_deccan
								region = world_india_rajastan

								duchy = {
									OR = {
										title = d_merv
										title = d_dihistan
									}
								}
							}
						}

						title = d_merv
						title = d_dihistan
					}

					NOT = { real_tier = EMPEROR }
				}

				grant_title = event_target:second_mongol_son
			}

			if = {
				limit = { has_character_flag = has_third_son }

				any_demesne_title = {
					limit = {
						OR = {
							location = { region = custom_khwarizm }
							capital_scope = { region = custom_khwarizm }
						}

						NOT = { real_tier = EMPEROR }
					}

					grant_title = event_target:third_mongol_son
				}
			}
		}

		# Create new horde titles
		activate_title = {
			title = e_golden_horde
			status = yes
		}

		e_golden_horde = {
			set_title_flag = non_manual_law_change # No prestige loss
			copy_title_laws = e_mongol_empire
			add_law = succ_ultimogeniture
			grant_title = event_target:first_mongol_son
			make_primary_title = yes
			clr_title_flag = non_manual_law_change # No prestige loss
		}

		activate_title = {
			title = e_ilkhanate
			status = yes
		}

		e_ilkhanate = {
			set_title_flag = non_manual_law_change # No prestige loss
			copy_title_laws = e_mongol_empire
			add_law = succ_ultimogeniture
			grant_title = event_target:second_mongol_son
			make_primary_title = yes
			clr_title_flag = non_manual_law_change # No prestige loss
		}

		if = {
			limit = { has_character_flag = has_third_son }

			activate_title = {
				title = e_chagatai
				status = yes
			}

			e_chagatai = {
				set_title_flag = non_manual_law_change # No prestige loss
				copy_title_laws = e_mongol_empire
				add_law = succ_ultimogeniture
				grant_title = event_target:third_mongol_son
				make_primary_title = yes
				clr_title_flag = non_manual_law_change # No prestige loss
			}
		}

		hidden_effect = {
			# Seperate vassals
			any_vassal = {
				limit = {
					capital_scope = {
						OR = {
							region = custom_cuman
							region = custom_perm
							region = custom_volga_bulgaria
							region = custom_alania
							region = custom_crimea
							region = custom_lithuania
							region = custom_livonia
							region = custom_muscovy
							region = custom_novgorod
							region = custom_ruthenia
							region = custom_bulgaria
							region = custom_croatia
							region = custom_serbia
							region = custom_wallachia
							duchy = { title = d_aral }
						}
					}
				}

				set_defacto_liege = event_target:first_mongol_son

				opinion = {
					who = event_target:first_mongol_son
					name = opinion_loyal_vassal
					years = 5
				}
			}

			any_vassal = {
				limit = {
					capital_scope = {
						OR = {
							region = custom_persia
							region = custom_afghanistan
							region = custom_baluchistan
							region = custom_byzantium
							region = custom_arabia
							region = custom_jerusalem
							region = custom_mesopotamia
							region = custom_syria
							region = custom_armenia
							region = custom_azerbaijan
							region = custom_nikaea
							region = custom_trebizond
							region = world_india_bengal
							region = world_india_deccan
							region = world_india_rajastan

							duchy = {
								OR = {
									title = d_merv
									title = d_dihistan
								}
							}
						}
					}
				}

				set_defacto_liege = event_target:second_mongol_son

				opinion = {
					name = opinion_loyal_vassal
					who = event_target:second_mongol_son
					years = 5
				}
			}

			if = {
				limit = { has_character_flag = has_third_son }

				any_vassal = {
					limit = {
						capital_scope = { region = custom_khwarizm }
					}

					set_defacto_liege = event_target:third_mongol_son

					opinion = {
						name = opinion_loyal_vassal
						who = event_target:third_mongol_son
						years = 5
					}
				}
			}

			# Any remaining children will be asked to swear to a son
			FROM = {
				any_child = {
					limit = {
						is_ruler = no
						is_adult = yes

						NOR = {
							character = event_target:first_mongol_son
							character = event_target:second_mongol_son
							character = event_target:third_mongol_son
						}
					}

					character_event = {
						id = 60003
						days = 1
					}
				}
			}

			# Any mongol generals will be asked to serve a son
			any_courtier = {
				limit = {
					culture = FROM
					trait = brilliant_strategist
					is_ruler = no
					is_female = no

					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
						character = event_target:third_mongol_son
						is_child_of = FROM
					}
				}

				character_event = {
					id = 60003
					days = 1
				}
			}

			# Send event to inheriting sons
			event_target:first_mongol_son = { character_event = { id = 60004 } }
			event_target:second_mongol_son = { character_event = { id = 60004 } }
			event_target:third_mongol_son = { character_event = { id = 60004 } }

			if = {
				limit = {
					NOR = {
						character = event_target:first_mongol_son
						character = event_target:second_mongol_son
						character = event_target:third_mongol_son
					}
				}

				set_defacto_liege = event_target:first_mongol_son
			}

			clr_character_flag = has_third_son

			e_mongol_empire = {
				unsafe_destroy_landed_title = THIS

				activate_title = {
					title = THIS
					status = no
				}
			}
		}
	}
}

# Assigning flags to the mongol sons
character_event = {
	id = 60006

	hide_window = yes
	is_triggered_only = yes

	trigger = {
		always = no
		is_title_active = e_mongol_empire
		culture = mongol

		OR = {
			has_character_flag = is_jochi_khan
			has_character_flag = son_of_jochi_khan
			has_character_flag = is_tolui_khan
			has_character_flag = son_of_tolui_khan
			has_character_flag = is_chagatai_khan
			has_character_flag = son_of_chagatai_khan
			has_character_flag = is_ogodei_khan
			has_character_flag = son_of_ogodei_khan
		}
	}

	immediate = {
		if = {
			limit = {
				OR = {
					has_character_flag = is_jochi_khan
					has_character_flag = son_of_jochi_khan
				}
			}

			any_child = {
				limit = {
					is_female = no
					NOT = { trait = bastard }
				}
				set_character_flag = son_of_jochi_khan
			}
		}

		if = {
			limit = {
				OR = {
					has_character_flag = is_tolui_khan
					has_character_flag = son_of_tolui_khan
				}
			}

			any_child = {
				limit = {
					is_female = no
					NOT = { trait = bastard }
				}
				set_character_flag = son_of_tolui_khan
			}
		}

		if = {
			limit = {
				OR = {
					has_character_flag = is_chagatai_khan
					has_character_flag = son_of_chagatai_khan
				}
			}

			any_child = {
				limit = {
					is_female = no
					NOT = { trait = bastard }
				}
				set_character_flag = son_of_chagatai_khan
			}
		}

		if = {
			limit = {
				OR = {
					has_character_flag = is_ogodei_khan
					has_character_flag = son_of_ogodei_khan
				}
			}

			any_child = {
				limit = {
					is_female = no
					NOT = { trait = bastard }
				}
				set_character_flag = son_of_ogodei_khan
			}
		}
	}
}

# Prompt AI to use the manifest destiny CB
character_event = {
	id = 60209

	is_triggered_only = yes # on_yearly_pulse, mongol
	hide_window = yes

	only_independent = yes
	min_age = 16
	only_capable = yes
	prisoner = no
	ai = yes
	war = no

	trigger = {
		OR = {
			dynasty = 613 # Seljuks
			dynasty = 800 # Timurids
		}

		wealth >= 0
		mercenary = no
		is_tributary = no
		is_ill = no
		has_regent = no
		realm_size <= 150
		is_maimed_trigger = no
		is_inaccessible_trigger = no

		NOR = {
			trait = content
			trait = humble
			trait = craven
			trait = depressed
			trait = infirm
		}

		any_neighbor_independent_ruler = {
			reverse_realm_levy_diff = {
				who = ROOT
				value >= 10000
			}

			any_realm_title = {
				count = 3

				real_tier = COUNT

				kingdom = {
					capital_scope = {
						OR = {
							region = custom_persia
							region = custom_baluchistan
							region = custom_afghanistan
							region = custom_khwarizm
							region = custom_mesopotamia
							region = custom_azerbaijan
							region = custom_georgia
							region = custom_alania
							region = custom_armenia
							region = custom_trebizond
							region = custom_nikaea
							region = custom_syria
							region = custom_arabia
							region = custom_jerusalem
							region = custom_egypt
							region = custom_africa
						}
					}

					any_de_jure_vassal_title = {
						OR = {
							holder = ROOT

							location = {
								any_neighbor_province = {
									owned_by = ROOT
								}
							}
						}
					}
				}
			}

			NOR = {
				reverse_has_truce = ROOT
				is_married = ROOT
				is_allied_with = ROOT
				has_non_aggression_pact_with = ROOT
				pays_tribute_to = ROOT
				ROOT = { rightful_religious_head = PREV }
			}

			reverse_opinion = {
				who = ROOT
				value < 0
			}
		}
	}

	immediate = {
		# First priority
		random_neighbor_independent_ruler = {
			limit = {
				reverse_realm_levy_diff = {
					who = ROOT
					value >= 10000
				}

				NOR = {
					reverse_has_truce = ROOT
					is_married = ROOT
					is_allied_with = ROOT
					has_non_aggression_pact_with = ROOT
					pays_tribute_to = ROOT
					ROOT = { rightful_religious_head = PREV }
				}

				reverse_opinion = {
					who = ROOT
					value < 0
				}
			}

			preferred_limit = {
				any_realm_title = {
					count = 3

					real_tier = COUNT

					kingdom = {
						capital_scope = {
							OR = {
								region = custom_persia
								region = custom_baluchistan
								region = custom_afghanistan
								region = custom_mesopotamia
							}
						}

						any_de_jure_vassal_title = {
							OR = {
								holder = ROOT

								location = {
									any_neighbor_province= {
										owned_by = ROOT
									}
								}
							}
						}
					}
				}
			}

			preferred_limit = {
				any_realm_title = {
					count = 3

					real_tier = COUNT

					kingdom = {
						capital_scope = {
							OR = {
								region = custom_armenia
								region = custom_nikaea
								region = custom_syria
							}
						}

						any_de_jure_vassal_title = {
							OR = {
								holder = ROOT

								location = {
									any_neighbor_province = {
										owned_by = ROOT
									}
								}
							}
						}
					}
				}
			}

			preferred_limit = {
				any_realm_title = {
					count = 3

					real_tier = COUNT

					kingdom = {
						capital_scope = {
							OR = {
								region = custom_azerbaijan
								region = custom_trebizond
								region = custom_arabia
								region = custom_jerusalem
								region = custom_egypt
							}
						}

						any_de_jure_vassal_title = {
							OR = {
								holder = ROOT

								location = {
									any_neighbor_province = {
										owned_by = ROOT
									}
								}
							}
						}
					}
				}
			}

			save_event_target_as = target_ruler

			random_realm_title = {
				limit = { real_tier = COUNT }

				preferred_limit = {
					kingdom = {
						event_target:target_ruler = {
							num_of_realm_counties = {
								value = 3
								title = PREV
							}
						}

						capital_scope = {
							OR = {
								region = custom_persia
								region = custom_baluchistan
								region = custom_afghanistan
								region = custom_mesopotamia
							}
						}

						any_de_jure_vassal_title = {
							OR = {
								holder = ROOT

								location = {
									any_neighbor_province= {
										owned_by = ROOT
									}
								}
							}
						}
					}
				}

				preferred_limit = {
					kingdom = {
						event_target:target_ruler = {
							num_of_realm_counties = {
								value = 3
								title = PREV
							}
						}

						capital_scope = {
							OR = {
								region = custom_armenia
								region = custom_nikaea
								region = custom_syria
							}
						}

						any_de_jure_vassal_title = {
							OR = {
								holder = ROOT

								location = {
									any_neighbor_province= {
										owned_by = ROOT
									}
								}
							}
						}
					}
				}

				preferred_limit = {
					kingdom = {
						event_target:target_ruler = {
							num_of_realm_counties = {
								value = 3
								title = PREV
							}
						}

						capital_scope = {
							OR = {
								region = custom_azerbaijan
								region = custom_trebizond
								region = custom_arabia
								region = custom_jerusalem
								region = custom_egypt
							}
						}

						any_de_jure_vassal_title = {
							OR = {
								holder = ROOT

								location = {
									any_neighbor_province= {
										owned_by = ROOT
									}
								}
							}
						}
					}
				}

				kingdom = {
					ROOT = {
						war = {
							casus_belli = manifest_destiny_invasion
							target = event_target:target_ruler
							thirdparty_title = PREV
						}
					}
				}
			}
		}
	}
}

### MONGOL WAR EVENTS

# Use nomad invasion
character_event = {
	id = 60535

	is_triggered_only = yes
	hide_window = yes

	only_independent = yes
	culture = mongol
	min_age = 16
	only_capable = yes
	prisoner = no
	ai = yes
	war = no

	trigger = {
		OR = {
			has_landed_title = e_mongol_empire
			has_landed_title = e_golden_horde
			has_landed_title = e_ilkhanate
		}

		is_nomadic = yes
		is_tributary = no
		prestige >= 500
		population_factor >= 0.9
		is_inaccessible_trigger = no

		trigger_if = {
			limit = { num_of_count_titles_in_realm >= 50 }
			has_landed_title = e_mongol_empire
		}

		any_neighbor_independent_ruler = {
			num_of_count_titles_in_realm >= 10

			reverse_realm_levy_diff = {
				who = ROOT
				value >= 10000
			}

			relative_power = {
				who = ROOT
				power < 3.0
			}

			any_realm_province = {
				held_under_PREV = yes

				any_neighbor_province = {
					owner = { same_realm = ROOT }
				}

				trigger_if = {
					limit = {
						ROOT = {
							NOT = { has_landed_title = e_mongol_empire }
						}
					}

					trigger_if = {
						limit = {
							ROOT = { has_landed_title = e_ilkhanate }
						}

						OR = {
							region = world_europe_south_east
							region = world_asia_minor
							region = world_middle_east
							region = world_persia
						}
					}
					trigger_else = {
						ROOT = { has_landed_title = e_golden_horde }

						NOR = {
							region = world_europe_south_east
							region = world_asia_minor
							region = world_middle_east
							region = world_persia
						}
					}
				}

				NOT = { region = world_india }
			}

			NOR = {
				reverse_has_truce = ROOT
				dynasty = ROOT
				is_allied_with = ROOT
				has_non_aggression_pact_with = ROOT
				is_married = ROOT
				pays_tribute_to = ROOT
			}

			reverse_opinion = {
				who = ROOT
				value < 25
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			num_of_count_titles_in_realm < 20
		}

		mult_modifier = {
			factor = 0.5
			num_of_count_titles_in_realm >= 35
		}
	}

	immediate = {
		random_neighbor_independent_ruler = {
			limit = {
				num_of_count_titles_in_realm >= 10

				reverse_realm_levy_diff = {
					who = ROOT
					value >= 10000
				}

				relative_power = {
					who = ROOT
					power < 3.0
				}

				any_realm_province = {
					held_under_PREV = yes

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}

					trigger_if = {
						limit = {
							ROOT = {
								NOT = { has_landed_title = e_mongol_empire }
							}
						}

						trigger_if = {
							limit = {
								ROOT = { has_landed_title = e_ilkhanate }
							}

							OR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
						trigger_else = {
							ROOT = { has_landed_title = e_golden_horde }

							NOR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
					}

					NOT = { region = world_india }
				}

				NOR = {
					same_realm = ROOT
					reverse_has_truce = ROOT
					dynasty = ROOT
					is_allied_with = ROOT
					has_non_aggression_pact_with = ROOT
					is_married = ROOT
					pays_tribute_to = ROOT
				}

				reverse_opinion = {
					who = ROOT
					value < 25
				}
			}

			random_realm_province = {
				limit = {
					held_under_PREV = yes

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}

					trigger_if = {
						limit = {
							ROOT = {
								NOT = { has_landed_title = e_mongol_empire }
							}
						}

						trigger_if = {
							limit = {
								ROOT = { has_landed_title = e_ilkhanate }
							}

							OR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
						trigger_else = {
							ROOT = { has_landed_title = e_golden_horde }

							NOR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
					}

					NOT = { region = world_india }
				}

				kingdom = {
					ROOT = {
						war = {
							casus_belli = nomad_invasion
							target = PREVPREVPREV # The top liege of the owner
							thirdparty_title = PREV # The de jure kingdom
						}
					}
				}
			}
		}
	}
}

# Use nomad expansion
character_event = {
	id = 60536

	is_triggered_only = yes
	hide_window = yes

	only_independent = yes
	culture = mongol
	min_age = 16
	only_capable = yes
	prisoner = no
	ai = yes
	war = no

	trigger = {
		OR = {
			has_landed_title = e_mongol_empire
			has_landed_title = e_golden_horde
			has_landed_title = e_ilkhanate
		}

		is_nomadic = yes
		is_tributary = no
		prestige >= 200
		population_factor = 0.9
		is_inaccessible_trigger = no

		trigger_if = {
			limit = { num_of_count_titles_in_realm >= 50 }
			has_landed_title = e_mongol_empire
		}

		any_neighbor_independent_ruler = {
			reverse_realm_levy_diff = {
				who = ROOT
				value >= 10000
			}

			relative_power = {
				who = ROOT
				power < 3.0
			}

			any_realm_province = {
				held_under_PREV = yes

				any_neighbor_province = {
					owner = { same_realm = ROOT }
				}

				trigger_if = {
					limit = {
						ROOT = {
							NOT = { has_landed_title = e_mongol_empire }
						}
					}

					trigger_if = {
						limit = {
							ROOT = { has_landed_title = e_ilkhanate }
						}

						OR = {
							region = world_europe_south_east
							region = world_asia_minor
							region = world_middle_east
							region = world_persia
						}
					}
					trigger_else = {
						ROOT = { has_landed_title = e_golden_horde }

						NOR = {
							region = world_europe_south_east
							region = world_asia_minor
							region = world_middle_east
							region = world_persia
						}
					}
				}

				NOT = { region = world_india }
			}

			NOR = {
				reverse_has_truce = ROOT
				dynasty = ROOT
				is_allied_with = ROOT
				has_non_aggression_pact_with = ROOT
				is_married = ROOT
				pays_tribute_to = ROOT
			}

			reverse_opinion = {
				who = ROOT
				value < 25
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			num_of_count_titles_in_realm < 20
		}

		mult_modifier = {
			factor = 0.5
			num_of_count_titles_in_realm >= 35
		}
	}

	immediate = {
		random_neighbor_independent_ruler = {
			limit = {
				reverse_realm_levy_diff = {
					who = ROOT
					value >= 10000
				}

				relative_power = {
					who = ROOT
					power <= 3.0
				}

				any_realm_province = {
					held_under_PREV = yes

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}

					trigger_if = {
						limit = {
							ROOT = {
								NOT = { has_landed_title = e_mongol_empire }
							}
						}

						trigger_if = {
							limit = {
								ROOT = { has_landed_title = e_ilkhanate }
							}

							OR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
						trigger_else = {
							ROOT = { has_landed_title = e_golden_horde }

							NOR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
					}

					NOT = { region = world_india }
				}

				NOR = {
					reverse_has_truce = ROOT
					dynasty = ROOT
					is_allied_with = ROOT
					has_non_aggression_pact_with = ROOT
					is_married = ROOT
					pays_tribute_to = ROOT
				}

				reverse_opinion = {
					who = ROOT
					value < 25
				}
			}

			random_realm_province = {
				limit = {
					held_under_PREV = yes

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}

					trigger_if = {
						limit = {
							ROOT = {
								NOT = { has_landed_title = e_mongol_empire }
							}
						}

						trigger_if = {
							limit = {
								ROOT = { has_landed_title = e_ilkhanate }
							}

							OR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
						trigger_else = {
							ROOT = { has_landed_title = e_golden_horde }

							NOR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
					}

					NOT = { region = world_india }
				}

				duchy = {
					ROOT = {
						war = {
							casus_belli = nomad_expansion
							target = PREVPREVPREV # The top liege of the owner
							thirdparty_title = PREV # The de jure duchy
						}
					}
				}
			}
		}
	}
}

# Use tribal invasion
character_event = {
	id = 60537

	is_triggered_only = yes
	hide_window = yes

	only_independent = yes
	culture = mongol
	lacks_dlc = "Horse Lords"
	min_age = 16
	only_capable = yes
	prisoner = no
	ai = yes
	war = no

	trigger = {
		OR = {
			has_landed_title = e_mongol_empire
			has_landed_title = e_golden_horde
			has_landed_title = e_ilkhanate
		}

		is_nomadic = no
		is_tributary = no
		prestige >= 500
		is_inaccessible_trigger = no
		NOT = { religion_group = christian }

		trigger_if = {
			limit = { num_of_count_titles_in_realm >= 50 }
			has_landed_title = e_mongol_empire
		}

		any_neighbor_independent_ruler = {
			reverse_realm_levy_diff = {
				who = ROOT
				value >= 10000
			}

			relative_power = {
				who = ROOT
				power < 3.0
			}

			any_realm_province = {
				held_under_PREV = yes

				any_neighbor_province = {
					owner = { same_realm = ROOT }
				}

				trigger_if = {
					limit = {
						ROOT = {
							NOT = { has_landed_title = e_mongol_empire }
						}
					}

					trigger_if = {
						limit = {
							ROOT = { has_landed_title = e_ilkhanate }
						}

						OR = {
							region = world_europe_south_east
							region = world_asia_minor
							region = world_middle_east
							region = world_persia
						}
					}
					trigger_else = {
						ROOT = { has_landed_title = e_golden_horde }

						NOR = {
							region = world_europe_south_east
							region = world_asia_minor
							region = world_middle_east
							region = world_persia
						}
					}
				}

				NOT = { region = world_india }
			}

			NOR = {
				reverse_has_truce = ROOT
				dynasty = ROOT
				is_allied_with = ROOT
				has_non_aggression_pact_with = ROOT
				is_married = ROOT
				pays_tribute_to = ROOT
			}

			reverse_opinion = {
				who = ROOT
				value < 25
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			num_of_count_titles_in_realm < 20
		}

		mult_modifier = {
			factor = 0.5
			num_of_count_titles_in_realm >= 35
		}
	}

	immediate = {
		random_neighbor_independent_ruler = {
			limit = {
				reverse_realm_levy_diff = {
					who = ROOT
					value = 10000
				}

				relative_power = {
					who = ROOT
					power < 3.0
				}

				any_realm_province = {
					held_under_PREV = yes

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}

					trigger_if = {
						limit = {
							ROOT = {
								NOT = { has_landed_title = e_mongol_empire }
							}
						}

						trigger_if = {
							limit = {
								ROOT = { has_landed_title = e_ilkhanate }
							}

							OR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
						trigger_else = {
							ROOT = { has_landed_title = e_golden_horde }

							NOR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
					}

					NOT = { region = world_india }
				}

				NOR = {
					reverse_has_truce = ROOT
					dynasty = ROOT
					is_allied_with = ROOT
					has_non_aggression_pact_with = ROOT
					is_married = ROOT
					pays_tribute_to = ROOT
				}

				reverse_opinion = {
					who = ROOT
					value < 25
				}
			}

			random_realm_province = {
				limit = {
					held_under_PREV = yes

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}

					trigger_if = {
						limit = {
							ROOT = {
								NOT = { has_landed_title = e_mongol_empire }
							}
						}

						trigger_if = {
							limit = {
								ROOT = { has_landed_title = e_ilkhanate }
							}

							OR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
						trigger_else = {
							ROOT = { has_landed_title = e_golden_horde }

							NOR = {
								region = world_europe_south_east
								region = world_asia_minor
								region = world_middle_east
								region = world_persia
							}
						}
					}

					NOT = { region = world_india }
				}

				kingdom = {
					ROOT = {
						war = {
							casus_belli = tribal_invasion
							target = PREVPREVPREV # The top liege of the owner
							thirdparty_title = PREV # The de jure kingdom
						}
					}
				}
			}
		}
	}
}