namespace = KHA

#Temujin's/Genghis Khan's Character ID - 125501

##################################
# Mongol Khanates Tributary Events
##################################

# Mongol Khanate becomes tributary of Mongol Empire
character_event = {
	id = KHA.98900
	desc = "As the founder of a new part of the Mongol Empire, it is my duty to pay formal respect to the Great Khan."
	
	picture = GFX_evt_mongols
	border = GFX_event_normal_frame_war
	
	trigger = {
		OR = {
			has_character_flag = khan_of_chagatai
			has_character_flag = khan_of_golden_horde
			has_character_flag = khan_of_ilkhanate
			has_character_flag = khan_of_independence
			has_landed_title = e_chagatai
			has_landed_title = e_golden_horde
			has_landed_title = e_il-khanate
			NOT = {
				has_character_flag = khan_of_chagatai
				has_character_flag = khan_of_golden_horde
				has_character_flag = khan_of_ilkhanate
				has_character_flag = khan_of_independence
			}
		}
		independent = yes
		is_tributary = no
		NOT = {
			has_landed_title = e_mongol_empire
		}
		NOT = {
			has_character_flag = mongol_khanate_tributary
		}
		OR = {
			AND = {
				has_landed_title = e_chagatai
				NOT = {
					has_global_flag = chagatai_mongol_khanate_tributary
				}
			}
			AND = {
				has_landed_title = e_golden_horde
				NOT = {
					has_global_flag = golden_horde_mongol_khanate_tributary
				}
			}
			AND = {
				has_landed_title = e_il-khanate
				NOT = {
					has_global_flag = ilkhanate_mongol_khanate_tributary
				}
			}
		}
		OR = {
			e_mongol_empire = {
				holder_scope = {
					realm_size = 50
					shares_realm_border_with = ROOT 
				}
			}
			any_dynasty_member = {
				has_landed_title = e_mongol_empire
				realm_size = 50
			}
		}
	}
	
	mean_time_to_happen = {
		days = 2
	}
	
	immediate = {
		set_character_flag = mongol_khanate_tributary
		if = {
			limit = {
				OR = {
					has_character_flag = khan_of_chagatai
					has_landed_title = e_chagatai
				}
			}
			save_event_target_as = mongol_khanates_chagatai
			if = {
				limit = {
					NOT = {
						has_character_flag = khan_of_chagatai
					}
				}
				set_character_flag = khan_of_chagatai
			}
			any_independent_ruler = {
				limit = {
					has_landed_title = e_mongol_empire
				}
				save_event_target_as = mongol_khanates_genghis_khan
			}
		}
		if = {
			limit = {
				OR = {
					has_character_flag = khan_of_golden_horde
					has_landed_title = e_golden_horde
				}
			}
			save_event_target_as = mongol_khanates_golden_horde
			if = {
				limit = {
					NOT = {
						has_character_flag = khan_of_golden_horde
					}
				}
				set_character_flag = khan_of_golden_horde
			}
			any_independent_ruler = {
				limit = {
					has_landed_title = e_mongol_empire
				}
				save_event_target_as = mongol_khanates_genghis_khan
			}
		}
		if = {
			limit = {
				OR = {
					has_character_flag = khan_of_ilkhanate
					has_landed_title = e_il-khanate
				}
			}
			save_event_target_as = mongol_khanates_ilkhanate
			if = {
				limit = {
					NOT = {
						has_character_flag = khan_of_ilkhanate
					}
				}
				set_character_flag = khan_of_ilkhanate
			}
			any_independent_ruler = {
				limit = {
					has_landed_title = e_mongol_empire
				}
				save_event_target_as = mongol_khanates_genghis_khan
			}
		}
	}
	
	option = {
		name = EVTOPTAKHA.98900
		trigger = {
			has_character_flag = khan_of_chagatai
		}
		event_target:mongol_khanates_genghis_khan = {
			make_tributary = {
				who = event_target:mongol_khanates_chagatai
				tributary_type = permanent
			}
		}
		set_global_flag = chagatai_mongol_khanate_tributary
	}
	option = {
		name = EVTOPTBKHA.98900
		trigger = {
			has_character_flag = khan_of_golden_horde
		}
		event_target:mongol_khanates_genghis_khan = {
			make_tributary = {
				who = event_target:mongol_khanates_golden_horde
				tributary_type = permanent
			}
		}
		set_global_flag = golden_horde_mongol_khanate_tributary
	}
	option = {
		name = EVTOPTCKHA.98900
		trigger = {
			has_character_flag = khan_of_ilkhanate
		}
		event_target:mongol_khanates_genghis_khan = {
			make_tributary = {
				who = event_target:mongol_khanates_ilkhanate
				tributary_type = permanent
			}
		}
		set_global_flag = ilkhanate_mongol_khanate_tributary
	}
}

### Division of the Mongol Empire ###
character_event = {
	id = KHA.98901
	desc = "The previous Khan, upon his death, dictated the division of the empire. As successor, I am expected to follow through on this to maintain stability. Despite the succession of new Khan's to their respective regions, they must answer to me."
	
	picture = GFX_evt_nomad_camp
	border = GFX_event_normal_frame_war
	
	is_triggered_only = yes
	
	trigger = {
		OR = {
			has_global_flag = division_of_the_mongol_empire
			has_global_flag = division_of_the_mongol_empire_no_gh
		}
		has_landed_title = e_mongol_empire
		NOT = {
			has_global_flag = fully_divided_mongol_empire
		}
	}
	
	mean_time_to_happen = {
		days = 2
	}
	
	immediate = {
		set_global_flag = fully_divided_mongol_empire
		set_character_flag = fully_divided_mongol_empire_successor_great_khan
	}
	
	option = {	# Original with all Khanates
		name = EVTOPTAKHA.98901
		trigger = {
			has_global_flag = division_of_the_mongol_empire
		}
		e_mongol_empire = {
			holder_scope = {
				narrative_event = { 
					id = KHA.98902 
					days = 1
				}
			}
		}
				#Yuan Dynasty
				offmap_china = {
					offmap_ruler = {
						limit = {
							offmap_ruler = {
								any_dynasty_member = {
									has_landed_title = e_mongol_empire
									has_character_flag = divided_the_mongol_empire
								}
							}
						}
						hidden_tooltip = {
						any_title = {
							limit = {
								OR = {
									de_jure_liege_or_above = e_tibet
								}
								OR = {
									owner = { is_liege_or_above = e_mongol_empire }
									owner = { has_landed_title = e_mongol_empire }
								}
							}
							grant_title = PREV
						}
						}
						custom_tooltip = { text = yuan_division_mongol_empire_tooltip }
						set_global_flag = yuan_takes_tibet
					}
				}
				#Ilkhanate
				random_dynasty_member = {
					limit = {
						is_female = no
						is_landed = no
						is_adult = yes
						is_alive = yes
						is_offmap_ruler = no
						any_owned_bloodline = { has_bloodline_flag = genghis_khan_line }
						NOT = {
							has_character_flag = khan_of_ilkhanate
						}
						NOT = {
							has_landed_title = e_mongol_empire
						}
					}
					e_il-khanate = {
						grant_title = PREV
					}
					wealth = 3000
					hidden_tooltip = {
					any_title = {
						limit = {
							OR = {
								de_jure_liege_or_above = e_il-khanate
								region = world_middle_east
								region = world_asia_minor
								region = world_africa
							}
							OR = {
								owner = { is_liege_or_above = e_mongol_empire }
								owner = { has_landed_title = e_mongol_empire }
							}
						}
						grant_title = PREV
					}
					set_character_flag = khan_of_ilkhanate
					}
				}
				
				#Golden Horde
				random_dynasty_member = {
					limit = {
						is_female = no
						is_landed = no
						is_adult = yes
						is_alive = yes
						is_offmap_ruler = no
						any_owned_bloodline = { has_bloodline_flag = genghis_khan_line }
						NOT = {
							has_character_flag = khan_of_golden_horde
						}
						NOT = {
							has_landed_title = e_mongol_empire
						}
					}
					e_golden_horde = {
						grant_title = PREV
					}
					wealth = 3000
					add_population_scaled = 0.9
					manpower = 10000
					set_government_type = nomadic_government
					hidden_tooltip = {
					any_title = {
						limit = {
							OR = {
								de_jure_liege_or_above = e_golden_horde
								de_jure_liege_or_above = e_idel_ural
								region = world_europe_east
								region = world_europe_south_east
								region = world_europe
							}
							OR = {
								owner = { is_liege_or_above = e_mongol_empire }
								owner = { has_landed_title = e_mongol_empire }
							}
						}
						grant_title = PREV
					}
					}
					set_character_flag = khan_of_golden_horde
				}
				
				#Chagatai Khanate
				random_dynasty_member = {
					limit = {
						is_female = no
						is_landed = no
						is_adult = yes
						is_alive = yes
						is_offmap_ruler = no
						any_owned_bloodline = { has_bloodline_flag = genghis_khan_line }
						NOT = {
							has_character_flag = khan_of_chagatai
						}
						NOT = {
							has_landed_title = e_mongol_empire
						}
					}
					e_chagatai = {
						grant_title = PREV
					}
					wealth = 3000
					add_population_scaled = 0.9
					manpower = 10000
					set_government_type = nomadic_government
					hidden_tooltip = {
					any_title = {
						limit = {
							de_jure_liege_or_above = e_chagatai
							OR = {
								owner = { is_liege_or_above = e_mongol_empire }
								owner = { has_landed_title = e_mongol_empire }
							}
						}
						grant_title = PREV
					}
					}
					set_character_flag = khan_of_chagatai
				}
	}
	
	option = {	# Without Golden Horde
		name = EVTOPTBKHA.98901
		trigger = {
			has_global_flag = division_of_the_mongol_empire_no_gh
		}
		e_mongol_empire = {
			holder_scope = {
				narrative_event = { 
					id = KHA.98902 
					days = 1
				}
			}
		}
				#Yuan Dynasty
				offmap_china = {
					offmap_ruler = {
						limit = {
							offmap_ruler = {
								any_dynasty_member = {
									has_landed_title = e_mongol_empire
									has_character_flag = divided_the_mongol_empire
								}
							}
						}
						hidden_tooltip = {
						any_title = {
							limit = {
								OR = {
									de_jure_liege_or_above = e_tibet
								}
								OR = {
									owner = { is_liege_or_above = e_mongol_empire }
									owner = { has_landed_title = e_mongol_empire }
								}
							}
							grant_title = PREV
						}
						}
						custom_tooltip = { text = yuan_division_mongol_empire_tooltip }
						set_global_flag = yuan_takes_tibet
					}
				}
				#Ilkhanate
				random_dynasty_member = {
					limit = {
						is_female = no
						is_landed = no
						is_adult = yes
						is_alive = yes
						is_offmap_ruler = no
						any_owned_bloodline = { has_bloodline_flag = genghis_khan_line }
						NOT = {
							has_character_flag = khan_of_ilkhanate
						}
						NOT = {
							has_landed_title = e_mongol_empire
						}
					}
					e_il-khanate = {
						grant_title = PREV
					}
					wealth = 3000
					hidden_tooltip = {
					any_title = {
						limit = {
							OR = {
								de_jure_liege_or_above = e_il-khanate
								region = world_middle_east
								region = world_asia_minor
								region = world_africa
							}
							OR = {
								owner = { is_liege_or_above = e_mongol_empire }
								owner = { has_landed_title = e_mongol_empire }
							}
						}
						grant_title = PREV
					}
					set_character_flag = khan_of_ilkhanate
					}
				}
				
				#Chagatai Khanate
				random_dynasty_member = {
					limit = {
						is_female = no
						is_landed = no
						is_adult = yes
						is_alive = yes
						is_offmap_ruler = no
						any_owned_bloodline = { has_bloodline_flag = genghis_khan_line }
						NOT = {
							has_character_flag = khan_of_chagatai
						}
						NOT = {
							has_landed_title = e_mongol_empire
						}
					}
					e_chagatai = {
						grant_title = PREV
					}
					wealth = 3000
					add_population_scaled = 0.9
					manpower = 10000
					set_government_type = nomadic_government
					hidden_tooltip = {
					any_title = {
						limit = {
							de_jure_liege_or_above = e_chagatai
							OR = {
								owner = { is_liege_or_above = e_mongol_empire }
								owner = { has_landed_title = e_mongol_empire }
							}
						}
						grant_title = PREV
					}
					}
					set_character_flag = khan_of_chagatai
				}
	}
}


### Character Switching Event ###
# Upon division, choose which character to be
narrative_event = {
	id = KHA.98902
	title = EVTTITLEKHA.98902
	desc = EVTDESCKHA.98902
	picture = GFX_evt_mongols
	border = GFX_event_narrative_frame_war
	
	is_triggered_only = yes
	
	trigger = {
		ai = no
	}
	
	immediate = {
		any_playable_ruler = {
			limit = {
				has_landed_title = e_mongol_empire
			}
			save_event_target_as = mongol_khanates_genghis_khan
		}
		any_playable_ruler = {
			limit = {
				has_landed_title = e_chagatai
			}
			save_event_target_as = mongol_khanates_chagatai
			if = {
				limit = {
					NOT = {
						has_character_flag = khan_of_chagatai
					}
				}
				set_character_flag = khan_of_chagatai
			}
		}
		any_playable_ruler = {
			limit = {
				has_landed_title = e_golden_horde
			}
			save_event_target_as = mongol_khanates_golden_horde
			if = {
				limit = {
					NOT = {
						has_character_flag = khan_of_golden_horde
					}
				}
				set_character_flag = khan_of_golden_horde
			}
		}
		any_playable_ruler = {
			limit = {
				has_landed_title = e_il-khanate
			}
			save_event_target_as = mongol_khanates_ilkhanate
			if = {
				limit = {
					NOT = {
						has_character_flag = khan_of_ilkhanate
					}
				}
				set_character_flag = khan_of_ilkhanate
			}
		}
	}
	
	option = {	# Stay as Khagan of the Mongol Empire
		name = EVTOPTAKHA.98902
	}
	option = {	# Switch to Khan of the Golden Horde
		name = EVTOPTBKHA.98902
		trigger = {
			has_global_flag = division_of_the_mongol_empire
			NOT = {
				has_global_flag = division_of_the_mongol_empire_no_gh
			}
    		event_target:mongol_khanates_golden_horde = {
    			dynasty = ROOT
    		}
		}
		tooltip_info = yes

    	custom_tooltip = {
    		text = switch_to_golden_horde_mongol_khanates_TT
    		if = {
    			limit = {
    				event_target:mongol_khanates_golden_horde = {
    					is_alive = yes
    					ai = yes
    					any_demesne_title = {
    						tier = COUNT
    					}
    				}
    			}
    			set_player_character = event_target:mongol_khanates_golden_horde
			}
    	}
	}
	option = {	# Switch to Khan of Chagatai Khanate
		name = EVTOPTCKHA.98902
		trigger = {
			OR = {
				has_global_flag = division_of_the_mongol_empire
				has_global_flag = division_of_the_mongol_empire_no_gh
			}
    		event_target:mongol_khanates_chagatai = {
    			dynasty = ROOT
    		}
		}
		
		tooltip_info = yes
		
		custom_tooltip = {
    		text = switch_to_chagatai_mongol_khanates_TT
    		if = {
    			limit = {
    				event_target:mongol_khanates_chagatai = {
    					is_alive = yes
    					ai = yes
    					any_demesne_title = {
    						tier = COUNT
    					}
    				}
    			}
    			set_player_character = event_target:mongol_khanates_chagatai
			}
    	}
	}
	option = {	# Switch to Khan of the Ilkhanate
		name = EVTOPTDKHA.98902
		trigger = {
			OR = {
				has_global_flag = division_of_the_mongol_empire
				has_global_flag = division_of_the_mongol_empire_no_gh
			}
    		event_target:mongol_khanates_ilkhanate = {
    			dynasty = ROOT
    		}
		}
		
		tooltip_info = yes
		
		custom_tooltip = {
    		text = switch_to_ilkhanate_mongol_khanates_TT
    		if = {
    			limit = {
    				event_target:mongol_khanates_ilkhanate = {
    					is_alive = yes
    					ai = yes
    					any_demesne_title = {
    						tier = COUNT
    					}
    				}
    			}
    			set_player_character = event_target:mongol_khanates_ilkhanate
			}
    	}
	}
}
