﻿### Submit to the Great Khan ###
submit_to_the_great_khan_decision = {
	ai_check_interval_by_tier = {
		barony = 0
		county = 12
		duchy = 12
		kingdom = 12
		empire = 12
		hegemony = 12
	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/mpo_decision_great_khan.dds"
	}
	desc = submit_to_the_great_khan_decision_opening_desc
	selection_tooltip = submit_to_the_great_khan_decision_tooltip
	
	sort_order = 110
	decision_group_type = major

	is_shown = {
		is_landless_adventurer = no
		is_independent_ruler = yes
		OR = {
			exists = global_var:mongol_empire_has_spawned
			AND = {
				exists = global_var:greatest_of_khans_title
				exists = global_var:greatest_of_khans_title.holder
			}
		}
		NOT = {
			mpo_has_gok_mongol_empire_trigger = yes
		}
		OR = {
			any_neighboring_top_liege_realm_owner = {
				mpo_has_gok_mongol_empire_trigger = yes
			}
			AND = {
				is_ai = no # this heavier check is for players, AI will only evaluate decision when immediately bordering the Mongols
				any_neighboring_top_liege_realm_owner = {
					any_neighboring_top_liege_realm_owner = {
						mpo_has_gok_mongol_empire_trigger = yes
					}
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		NOT = { highest_held_title_tier = tier_empire }
	}

	effect = {
		if = {
			limit = {
				exists = confederation
			}
			confederation = {
				#Your confederation will survive, it has more members
				if = {
					limit = {
						any_confederation_member = {
							count >= 2
							NOT = {
								this = root
							}
						}
					}
					remove_confederation_member = root
				}
				#Your confederation is toast
				else = {
					disband_confederation = yes
				}
			}
		}
		hidden_effect = {
			if = {
				limit = {
					exists = title:e_mongol_empire.holder
				}
				title:e_mongol_empire.holder = {
					save_scope_as = mongol_emperor
				}
			}
			else = {
				random_neighboring_top_liege_realm_owner = {
					limit = { mpo_has_gok_mongol_empire_trigger = yes }
					save_scope_as = mongol_emperor
				}
				if = {
					limit = {
						NOT = {
							exists = scope:mongol_emperor
						}
					}
					random_neighboring_top_liege_realm_owner = {
						limit = {
							mpo_has_gok_mongol_empire_trigger = yes
						}
						random_neighboring_top_liege_realm_owner = {
							limit = {
								mpo_has_gok_mongol_empire_trigger = yes
							}
							save_scope_as = mongol_emperor
						}
					}
				}
			}
		}
		every_character_war = {
			limit = {
				primary_defender = root
			}
			end_war = white_peace
		}
		hidden_effect = {
			debug_log = "A ruler submitted to the khan"
			debug_log_scopes = yes
		}
		save_scope_as = recipient
		scope:mongol_emperor = {
			send_interface_message = {
				type = event_gok_submission_with_text
				title = mpo_greatest_of_khans.0020_title
				left_icon = root
	
				root = {
					create_title_and_vassal_change = {
						type = swear_fealty
						save_scope_as = title_change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = scope:mongol_emperor
						change = scope:title_change
					}
					resolve_title_and_vassal_change = scope:title_change
					if = {
						limit = {
							government_has_flag = government_is_nomadic
						}
						pay_herd = {
							target = scope:mongol_emperor
							value = domicile.major_herd_value
						}
					}
					else_if = {
						limit = {
							NOT = {
								government_has_flag = government_is_herder
							}
						}
						pay_short_term_gold = {
							target = scope:mongol_emperor
							gold = major_gold_value
						}
					}
					if = {
						limit = {
							#khan is still nomadic or pluralist and thus doesn't care about religion
							scope:mongol_emperor = {
								faith = {
									has_doctrine = doctrine_pluralism_pluralistic
								}
							}
							OR = {
								government_has_flag = government_is_feudal
								government_has_flag = government_is_clan
							}
						}
						hidden_effect = {
							if = {
								limit = { is_independent_ruler = no } # To avoid errors during tooltip generation (prior to the contract being generated)
								vassal_contract_set_obligation_level = { type = religious_rights level = 1 }
							}
						}
						custom_tooltip = religious_protection_tt
					}
				}
			}
		}
	}

	ai_potential = {
		OR = {
			exists = global_var:mongol_empire_has_spawned
			AND = {
				exists = global_var:greatest_of_khans_title
				exists = global_var:greatest_of_khans_title.holder
			}
		}
		is_independent_ruler = yes
		highest_held_title_tier < tier_empire
		trigger_if = {
			limit = {
				government_has_flag = government_is_nomadic
			}
			domicile = {
				herd >= massive_herd_value
			}
		}
		trigger_if = {
			limit = {
				NOR = {
					government_has_flag = government_is_herder
					government_has_flag = government_is_nomadic
				}
			}
			gold >= medium_gold_value
		}
	}

	ai_will_do = { # is_shown trigger means AI will only evaluate these when immediate neighbors with the Mongols
		base = 0
		# Very likely if you're an independent count bordering the Empire
		modifier = {
			add = 25
			highest_held_title_tier = tier_county
		}
		# Fairly likely if you're an independent duke bordering the Empire
		modifier = {
			add = 10
			highest_held_title_tier = tier_duchy
			OR = {
				culture = { has_cultural_pillar = heritage_mongolic }
				culture = { has_cultural_pillar = heritage_turkic }
			}
		}
		# A little bit likely if you're an independent king in the Altaic group bordering the Empire
		modifier = {
			add = 10
			highest_held_title_tier = tier_duchy
			OR = {
				culture = { has_cultural_pillar = heritage_mongolic }
				culture = { has_cultural_pillar = heritage_turkic }
			}
		}
		# More likely if you're Craven
		modifier = {
			factor = 1.25
			has_trait = craven
		}
		# Unlikely if you're Brave
		modifier = {
			factor = 0.2
			has_trait = brave
		}
		# Unlikely if you're Arrogant
		modifier = {
			factor = 0.1
			has_trait = arrogant
		}
		# Very likely if you're already allies
		modifier = {
			factor = 1.5
			any_ally = {
				mpo_has_gok_mongol_empire_trigger = yes
			}
		}
		# The AI should never do this as a king or above
		modifier = {
			factor = 0
			highest_held_title_tier >= tier_kingdom
		}
	}
}
