﻿historicinvasions_mongol_breakup_succession_score = {
	# Prioritize the actual heir
	if = {
		limit = {
			exists = scope:old_emperor.primary_heir
			this = scope:old_emperor.primary_heir
		}
		add = 250
	}
	# A bonus for being of the same house
	if = {
		limit = {
			exists = house
			house = scope:old_emperor.house
		}
		add = 50
	}
	# Prioritize siblings
	if = {
		limit = {
			is_sibling_of = scope:old_emperor
		}
		add = 100
	}
	# Children of children of the old khan gain some priority
	else_if = {
		limit = {
			any_parent = {
				any_parent = {
					this = scope:old_emperor
				}
			}
		}
		add = 50
	}
	else_if = {
		limit = {
			is_close_family_of = scope:old_emperor
		}
		add = 30
	}
	else_if = {
		limit = {
			is_close_or_extended_family_of = scope:old_emperor
		}
		add = 15
	}
	else_if = {
		limit = {
			has_trait = descendant_of_temujin_borjigin
		}
		add = 15
	}

	# has an available heir of the corresponding gender
	if = {
		limit = {
			any_heir = {
				trigger_if = {
					limit = { scope:old_emperor.faith = { has_doctrine_parameter = male_dominated_law } }
					is_male = yes
				}
				trigger_else_if = {
					limit = { scope:old_emperor.faith = { has_doctrine_parameter = female_dominated_law } }
					is_female = yes
				}
				trigger_else = {
					always = no
				}
				count > 2
				is_adult = yes
			}
		}
		add = 150
	}
	else_if = {
		limit = {
			any_heir = {
				trigger_if = {
					limit = { scope:old_emperor.faith = { has_doctrine_parameter = male_dominated_law } }
					is_male = yes
				}
				trigger_else_if = {
					limit = { scope:old_emperor.faith = { has_doctrine_parameter = female_dominated_law } }
					is_female = yes
				}
				trigger_else = {
					always = no
				}
				count > 1
				is_adult = yes
			}
		}
		add = 100
	}
	else_if = {
		limit = {
			any_heir = {
				trigger_if = {
					limit = { scope:old_emperor.faith = { has_doctrine_parameter = male_dominated_law } }
					is_male = yes
				}
				trigger_else_if = {
					limit = { scope:old_emperor.faith = { has_doctrine_parameter = female_dominated_law } }
					is_female = yes
				}
				trigger_else = {
					always = no
				}
				count > 0
				is_adult = yes
			}
		}
		add = 50
	}
	else = {
		multiply = 0.8
	}

	# Skill bonuses
	add = sum_of_all_skills_value
	
	# Gender preference, can't check for realm law so using faith
	if = {
		limit = {
			is_female = yes
			scope:old_emperor.faith = { has_doctrine_parameter = male_dominated_law }
		}
		multiply = 0.01
	}
	else_if = {
		limit = {
			scope:old_emperor.faith = { has_doctrine_parameter = female_dominated_law }
		}
		multiply = 0.01
	}
	
	# A penalty for not being the same culture, mongol, or an accepted culture
	if = {
		limit = {
			NOR = {
				culture = scope:old_emperor.culture
				culture = culture:mongol
				culture = {
					cultural_acceptance = {
						target = scope:old_emperor.culture
						value >= 75
					}
				}
			}
		}
		multiply = 0.1
	}
	
	# Not adult
	if = {
		limit = {
			NAND = { # the primary heir is excluded from this
				exists = scope:old_emperor.primary_heir
				this = scope:old_emperor.primary_heir
			}
			is_adult = no
		}
		multiply = 0.01
	}

	# ill health
	if = {
		limit = {
			OR = {
				has_contagious_deadly_disease_trigger = yes
				has_epidemic_disease_trigger = yes
				health <= poor_health
				has_trait = infirm
				has_trait = incapable
			}
		}
		multiply = 0.0001
	}

	# too old
	if = {
		limit = { age > 60 }
		multiply = 0.0001
	}
	else_if = {
		limit = { age > 50 }
		multiply = 0.01
	}

	# don't pick emperors
	if = {
		limit = { highest_held_title_tier = tier_empire }
		multiply = 0
	}
}

historicinvasions_mongol_successor_faith_chance = {
	value = 0
	save_temporary_scope_as = county_faith_check
	add = {
		scope:faith_changer = { # Mongol successor state holder
			every_vassal_or_below = {
				faith = scope:county_faith_check.faith
			}
			add = 5
		}
	}
}