﻿
appointment_score_script_value = {
	value = "appointment_candidate_score(root.primary_title)"
	min = 1
	desc = APPOINTMENT_SCORE_SCRIPT_VALUE_DESC
}

# For use when promoting a candidate via interaction.
appointment_score_interaction_multiplier_modifiers_value = {
	value = 1
	# Co-emperors are more efficient to promote.
	if = {
		limit = {
			exists = scope:target
			exists = scope:secondary_recipient
			appointment_interactions_recipient_is_suitable_co_emperor_trigger = yes
		}
		if = {
			limit = {
				scope:actor.top_liege = { has_diarchy_active_parameter = diarch_more_efficient_administrative_emperor_promotion_candidate_massive }
			}
			add = diarch_more_efficient_administrative_emperor_promotion_candidate_massive_value
		}
		else_if = {
			limit = {
				scope:actor.top_liege = { has_diarchy_active_parameter = diarch_more_efficient_administrative_emperor_promotion_candidate_major }
			}
			add = diarch_more_efficient_administrative_emperor_promotion_candidate_major_value
		}
		else_if = {
			limit = {
				scope:actor.top_liege = { has_diarchy_active_parameter = diarch_more_efficient_administrative_emperor_promotion_candidate_medium }
			}
			add = diarch_more_efficient_administrative_emperor_promotion_candidate_medium_value
		}
		else_if = {
			limit = {
				scope:actor.top_liege = { has_diarchy_active_parameter = diarch_more_efficient_administrative_emperor_promotion_candidate_mild }
			}
			add = diarch_more_efficient_administrative_emperor_promotion_candidate_mild_value
		}
	}
}

appointment_cost_multiplier_value = {
	value = 1
	if = {
		limit = {
			top_liege = { has_realm_law = male_preference_law }
			is_female = yes
		}
		add = 1
	}
	else_if = {
		limit = {
			top_liege = { has_realm_law = male_only_law }
			is_female = yes
		}
		multiply = 3
	}
	else_if = {
		limit = {
			top_liege = { has_realm_law = female_preference_law }
			is_female = no
		}
		multiply = 1
	}
	else_if = {
		limit = {
			top_liege = { has_realm_law = female_only_law }
			is_female = no
		}
		multiply = 3
	}
}

appointment_minor_influence_cost = {
	value = 50
}

appointment_medium_influence_cost = {
	value = 100
}

appointment_major_influence_cost = {
	value = 250
}

appointment_score_minor_value = {
	value = appointment_minor_influence_cost
	multiply = 0.2
}

appointment_score_medium_value = {
	value = appointment_medium_influence_cost
	multiply = 0.2
}

appointment_score_major_value = {
	value = appointment_major_influence_cost
	multiply = 0.2
}

appointment_score_minor_loss_value = {
	value = appointment_score_minor_value
	multiply = -1
}

appointment_score_medium_loss_value = {
	value = appointment_score_medium_value
	multiply = -1
}

appointment_score_major_loss_value = {
	value = appointment_score_major_value
	multiply = -1
}

appointment_score_max_tt_value = {
	value = 0
	if = {
		limit = {
			exists = scope:target
			exists = scope:target.current_heir
			exists = scope:secondary_recipient
			NOT = { scope:secondary_recipient = scope:target.current_heir }
		}
		add = {
			value = "scope:target.current_heir.appointment_candidate_score(scope:target)"
			multiply = 1.1
		}
		subtract = "scope:secondary_recipient.appointment_candidate_score(scope:target)"
		min = 4 # We do this to prevent cases when the default score of the candidate is already much higher than that the heir
	}
	multiply = scope:actor.appointment_score_interaction_multiplier_modifiers_value
}

appointment_score_medium_tt_value = {
	value = appointment_score_medium_value
	multiply = scope:actor.appointment_score_interaction_multiplier_modifiers_value
}

appointment_score_major_tt_value = {
	value = appointment_score_major_value
	multiply = scope:actor.appointment_score_interaction_multiplier_modifiers_value
}

appointment_score_medium_loss_tt_value = {
	value = appointment_score_medium_loss_value
	multiply = scope:actor.appointment_score_interaction_multiplier_modifiers_value
}

appointment_score_major_loss_tt_value = {
	value = appointment_score_major_loss_value
	multiply = scope:actor.appointment_score_interaction_multiplier_modifiers_value
}

appointment_non_preferred_gender_penalty_value = 100
appointment_opposite_gender_penalty_value = 9999

# Maximum investment to support a candidate, and root is the investor
appointment_investment_cap = {
	value = {
		add = {
			value = 50 # Base
			desc = BASE_VALUE
		}
		##### AGE
		if = { # ADULT - scaling bonus
			limit = { is_adult = yes }
			add = {
				value = {
					add = age
					subtract = 16
					divide = 2
					ceiling = yes
				}
				desc = appointment_investment_cap_age
			}
		}
		else = { # CHILD - scaling malus
			add = {
				value = {
					add = age
					subtract = 16
					divide = 2
					floor = yes
				}
				desc = appointment_investment_cap_age
			}
		}
		if = {
			limit = {
				government_has_mechanic = administrative
			}
			##### FAMILY RATING
			if = {
				limit = { house.house_head ?= this }
				add = {
					value = house.house_power_score
					desc = appointment_investment_cap_family_rating
				}
			}
			##### MERIT
			if = {
				limit = { merit_level >= 1 }
				add = {
					value = {
						add = merit_level
						multiply = 5
					}
					desc = "[merit_level|E]"
				}
			}
			##### CELESTIAL MINISTER
			if = {
				limit = {
					is_councillor = yes
					liege ?= {
						tgp_has_access_to_ministry_trigger = yes
					}
				}
				add = {
					value = 10
					desc = is_a_minister_desc
				}
			}
		}
		# admin case
		if = {
			limit = {
				government_has_flag = government_has_influence
			}
			##### INFLUENCE LEVEL
			if = { # NEGATIVE
				limit = { influence_level < 1 }
				add = {
					value = -20
					desc = appointment_investment_cap_influence_level
				}
			}
			else_if = { # POSITIVE
				limit = { influence_level > 1 }
				add = {
					value = {
						add = influence_level
						multiply = 10
					}
					desc = appointment_investment_cap_influence_level
				}
			}
		}
		# otherwise piety, we don't have target passed from code so use else for now
		else = {
			if = { # NEGATIVE
				limit = { piety_level < 1 }
				add = {
					value = -20
					desc = piety_level_holder_desc
				}
			}
			else_if = { # POSITIVE
				limit = { piety_level > 1 }
				add = {
					value = {
						add = piety_level
						multiply = 10
					}
					desc = piety_level_holder_desc
				}
			}
		}
		##### ESTATE BUILDINGS
		house.house_head.domicile ?= {
			add = {
				value = 0
				if = {
					limit = { has_domicile_parameter = estate_increase_house_member_investment_cap_1 }
					add = 10
				}
				else_if = {
					limit = { has_domicile_parameter = estate_increase_house_member_investment_cap_2 }
					add = 20
				}
				else_if = {
					limit = { has_domicile_parameter = estate_increase_house_member_investment_cap_3 }
					add = 30
				}
				else_if = {
					limit = { has_domicile_parameter = estate_increase_house_member_investment_cap_4 }
					add = 40
				}
				else_if = {
					limit = { has_domicile_parameter = estate_increase_house_member_investment_cap_5 }
					add = 50
				}
				if = {
					limit = { has_domicile_parameter = japanese_manor_increase_house_member_investment_cap_1 }
					add = 5
				}
				else_if = {
					limit = { has_domicile_parameter = japanese_manor_increase_house_member_investment_cap_2 }
					add = 10
				}
				else_if = {
					limit = { has_domicile_parameter = japanese_manor_increase_house_member_investment_cap_3 }
					add = 15
				}
				else_if = {
					limit = { has_domicile_parameter = japanese_manor_increase_house_member_investment_cap_4 }
					add = 20
				}
				else_if = {
					limit = { has_domicile_parameter = japanese_manor_increase_house_member_investment_cap_5 }
					add = 25
				}
				else_if = {
					limit = { has_domicile_parameter = japanese_manor_increase_house_member_investment_cap_6 }
					add = 30
				}
				desc = appointment_investment_cap_estate_buildings
			}
		}
		##### FAMILY ATTRIBUTES
		house ?= {
			add = {
				value = 0
				if = {
					limit = { has_house_aspiration_parameter = increased_investment_cap_bonus_3 }
					add = increased_investment_cap_bonus_3_value
				}
				else_if = {
					limit = { has_house_aspiration_parameter = increased_investment_cap_bonus_2 }
					add = increased_investment_cap_bonus_2_value
				}
				else_if = {
					limit = { has_house_aspiration_parameter = increased_investment_cap_bonus_1 }
					add = increased_investment_cap_bonus_1_value
				}
				desc = political_meddlers_attribute_name_desc
			}
		}
		##### EXPANDED POWER BASE
		add = appointment_expand_power_base_bonus
		min = 0
	}
}

appointment_expand_power_base_bonus = {
	### EXPANDED POWER BASE
	save_temporary_value_as = temp_total
	if = {
		limit = { has_character_modifier = ep3_expanded_power_base_minor_modifier }
		add = {
			value = scope:temp_total
			multiply = 0.10
			round = yes
			min = 25
			desc = ep3_expanded_power_base_minor_modifier
		}
	}
	if = {
		limit = { has_character_modifier = ep3_expanded_power_base_medium_modifier }
		add = {
			value = scope:temp_total
			multiply = 0.20
			round = yes
			min = 50
			desc = ep3_expanded_power_base_medium_modifier
		}
	}
	if = {
		limit = { has_character_modifier = ep3_expanded_power_base_major_modifier }
		add = {
			value = scope:temp_total
			multiply = 0.30
			round = yes
			min = 75
			desc = ep3_expanded_power_base_major_modifier
		}
	}
}

slander_appointment_score_value = -25
slander_acclamation_score_value = -50

promote_appointment_score_value = 30
promote_acclamation_score_value = 50


appointment_score_crime_penalty = 30

# for all governor, regent, and admin emperor
appointment_score_base = {
	if = {
		limit = {
			top_liege ?= {
				has_realm_law = candidate_score_prestige_law
			}
		}
		# Base score gained from prestige rank
		add = {
			value = 10
			multiply = prestige_level
			desc = PRESTIGE_LEVEL_CONCEPT
		} 
		if = {
			limit = {
				exists = dynasty
			}
			# Base score gained from prestige rank
			add = {
				value = 10
				multiply = dynasty.dynasty_prestige_level
				desc = DYNASTY_PRESTIGE_CONCEPT
			}
		}
	}

	# Dynasty Legacy
	if = {
		limit = {
			dynasty ?= {
				has_dynasty_perk = tgp_chinese_legacy_4
			}
		}
		add = {
			value = legacy_candidate_score_bonus
			desc = "[GetDynastyPerk('tgp_chinese_legacy_4').GetName]"
		}
	}

	# Aspirations
	if = {
		limit = {
			exists = house
			has_trait = lifestyle_poet
		}
		if = {
			limit = {
				house = { has_house_aspiration_parameter = poet_trait_provides_candidate_score_1 }
			}
			add = {
				value = "has_trait_xp(lifestyle_poet)"
				multiply = poet_trait_provides_candidate_score_1_value
				desc = celestial_governor_poet_trait_desc
			}
		}
		else_if = {
			limit = {
				house = { has_house_aspiration_parameter = poet_trait_provides_candidate_score_2 }
			}
			add = {
				value = "has_trait_xp(lifestyle_poet)"
				multiply = poet_trait_provides_candidate_score_2_value
				desc = celestial_governor_poet_trait_desc
			}
		}
		else_if = {
			limit = {
				house = { has_house_aspiration_parameter = poet_trait_provides_candidate_score_3 }
			}
			add = {
				value = "has_trait_xp(lifestyle_poet)"
				multiply = poet_trait_provides_candidate_score_3_value
				desc = celestial_governor_poet_trait_desc
			}
		}
		else_if = {
			limit = {
				house = { has_house_aspiration_parameter = poet_trait_provides_candidate_score_4 }
			}
			add = {
				value = "has_trait_xp(lifestyle_poet)"
				desc = celestial_governor_poet_trait_desc
			}
		}
	}

	# Influence
	if = {
		limit = {
			influence_level != 1
		}
		add = {
			value = 10
			multiply = {
				value = influence_level
				subtract = 1 # Baseline is at 1, so we want the bonus to start at 2
				if = {
					limit = {
						scope:title.holder ?= { has_realm_law = acclamation_succession_law }
					}
					multiply = 2
				}
			}
			desc = "[influence_level|E]"
		}
	}

	# Traits
	if = {
		limit = {
			NOT = {
				scope:title.holder ?= {
					has_realm_law = acclamation_succession_law
				}
			}
			OR = {
				has_trait = inbred
				has_trait = blind
				is_incapable = yes
				has_trait_with_flag = age_related_ailment
			}
		}
		subtract = {
			value = 50
			desc = negative_trait_succession
		}
	}
	if = {
		limit = { has_trait = disinherited }
		subtract = {
			value = 200
			desc = trait_disinherited
		}
	}
	if = {
		limit = { has_trait = gallivanter }
		subtract = {
			value = 200
			desc = trait_gallivanter
		}
	}
	if = {
		limit = { has_trait = devoted }
		subtract = {
			value = 250
			desc = trait_devoted
		}
	}
	else_if = {
		limit = { is_clergy = yes }
		subtract = {
			value = 250
			desc = IS_CLERGY_TRIGGER
		}
	}
	# RITE BASED
	if = {
		limit = {
			exists = top_liege.primary_title.state_rite
		}
		top_liege.primary_title.state_rite = {
			save_temporary_scope_as = target_rite
		}
	}
	else = {
		top_liege.rite = {
			save_temporary_scope_as = target_rite
		}
	}
	# CRIMINAL
	if = {
		limit = {
			has_trait = deviant
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:deviant RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "deviant_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = incestuous
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:incestuous RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "incestuous_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = sodomite
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:sodomite RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "sodomite_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = cannibal
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:cannibal RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "cannibal_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = kinslayer_1
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:kinslayer_1 RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "kinslayer_1_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = kinslayer_2
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:kinslayer_2 RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "kinslayer_2_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = kinslayer_3
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:kinslayer_3 RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "kinslayer_3_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = witch
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:witch RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "witch_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = adulterer
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:adulterer RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "adulterer_and_criminal_desc"
		}
	}
	if = {
		limit = {
			has_trait = fornicator
			trait_is_criminal_in_rite_trigger = { TRAIT = trait:fornicator RITE = scope:target_rite GENDER_CHARACTER = root }
		}
		subtract = {
			value = appointment_score_crime_penalty
			desc = "fornicator_and_criminal_desc"
		}
	}
	if = {
		limit = { exists = scope:title }
		# CLAIMS
		if = {
			limit = { has_strong_claim_on = scope:title }
			add = {
				value = 25
				if = {
					limit = { scope:title.holder = top_liege }
					multiply = 2
				}
				desc = title_claim_desc
			}
		}
		else_if = {
			limit = { has_weak_claim_on = scope:title }
			add = {
				value = 15
				if = {
					limit = { scope:title.holder = top_liege }
					multiply = 2
				}
				desc = title_weak_claim_desc
			}
		}
	}

	# LOWBORN? EW no
	if = {
		limit = {
			is_lowborn = yes
		}
		subtract = {
			value = 25
			if = {
				limit = { scope:title.holder ?= top_liege }
				multiply = 4
			}
			desc = lowborn_desc_succession
		}
	}

	# personal relationship
	if = {
		limit = {
			scope:title.holder ?= {
				NOR = {
					government_has_flag = government_is_celestial  # should not happen
					has_realm_law = acclamation_succession_law  # has different calc
				}
			}
		}
		# RELATIONS
		if = { # Is child of holder
			limit = {
				scope:title.holder ?= { is_parent_of = root }
			}
			add = {
				value = 30
				desc = "parent_holder_desc"
			}
		}
		else_if = { # Is family of holder
			limit = {
				scope:title.holder ?= { is_close_family_of = root }
			}
			add = {
				value = 20
				desc = "close_family_holder_desc"
			}
		}
		else_if = { # Is family of holder's spouse
			limit = {
				scope:title.holder ?= {
					any_spouse = { is_close_family_of = root }
					NOT = { is_close_family_of = root }
				}
			}
			add = {
				value = 10
				desc = "close_family_married_to_holder_desc"
			}
		}
		if = { # Is a friend
			limit = {
				scope:title.holder ?= { has_relation_friend = root }
			}
			add = {
				value = 15
				desc = governor_friend_desc
			}
		}
		else_if = {
			limit = {
				scope:title.holder ?= { has_relation_best_friend = root }
			}
			add = {
				value = 30
				desc = governor_best_friend_desc
			}
		}

		# COURT POSITIONS
		if = { # Has a court position with current holder
			limit = {
				scope:title.holder ?= {
					any_court_position_holder ?= { this = root }
				}
			}
			add = {
				value = 5
				desc = court_position_succession_governor_desc
			}
		}
	}
	if = { # Is a elder
		limit = {
			scope:title.holder ?= { has_relation_elder = root }
		}
		add = {
			value = 15
			desc = governor_elder_desc
		}
	}
	if = { # Is a disciple
		limit = {
			scope:title.holder ?= { has_relation_disciple = root }
		}
		add = {
			value = 10
			desc = governor_disciple_desc
		}
	}

	if = {
		limit = { scope:title.holder.diarch ?= root }
		if = {
			limit = { scope:title.holder = { has_realm_law = acclamation_succession_law } }
			if = {
				limit = {
					scope:title.holder = { character_has_co_emperorship_trigger = yes }
				}
				# Once for their raw status.
				add = {
					value = 50
					desc = imperial_co_emperor_desc
				}
			}
			add = {
				value = scope:title.holder.diarchy_swing
				desc = imperial_diarch_desc
			}
		}
		else = {
			add = {
				value = scope:title.holder.diarchy_swing
				desc = governor_diarch_desc
			}
		}
	}

	# Schemes
	if = {
		limit = {
			scope:title.holder ?= {
				has_realm_law = acclamation_succession_law
			}
		}
		if = {
			limit = { has_character_modifier = scheme_slandered_modifier }
			add = {
				value = slander_acclamation_score_value
				desc = scheme_slandered_modifier
			}
		}
		if = {
			limit = { has_character_modifier = scheme_promoted_modifier }
			add = {
				value = promote_acclamation_score_value	# Keep this balanced if more values are added!
				desc = scheme_promoted_modifier
			}
		}
	}
	else = {
		if = {
			limit = { has_character_modifier = scheme_slandered_modifier }
			add = {
				value = slander_appointment_score_value
				desc = scheme_slandered_modifier
			}
		}
		if = {
			limit = { has_character_modifier = scheme_promoted_modifier }
			add = {
				value = promote_appointment_score_value	# Keep this balanced if more values are added!
				desc = scheme_promoted_modifier
			}
		}
	}

	# Decisions
	if = {
		limit = {
			has_character_modifier = ep3_integration_promoter_modifier
			has_variable = integration_promoted_in
			exists = scope:title
			var:integration_promoted_in.duchy ?= scope:title
		}
		add = {
			value = 30
			desc = ep3_integration_promoter_modifier
		}
	}

	# PETITION
	if = {
		limit = { scope:title.var:petition_house_rights ?= house }
		add = {
			value = 250
			desc = petition_liege_house_province_rights_value_desc
		}
	}

	# ACH PURGE PRETENDERS OATH
	if = {
		limit = {
			house ?= {
				has_house_modifier = oath_of_purge_pretenders_house
			}
		}
		add = {
			value = 50
			desc = oath_of_purge_pretenders_house
		}
	}

	# ADJUST BASED ON GENDER LAW - MUST GO AT THE END
	if = {
		limit = {
			scope:title.holder.top_liege ?= { has_realm_law = male_preference_law }
			is_female = yes
		}
		# use subtract instead, otherwise giving bonus when negative score!
		subtract = {
			value = appointment_non_preferred_gender_penalty_value
			desc = appointment_male_preference_desc
		}
	}
	else_if = {
		limit = {
			scope:title.holder.top_liege ?= { has_realm_law = male_only_law }
			is_female = yes
		}
		# use subtract instead, otherwise giving bonus when negative score!
		subtract = {
			value = appointment_opposite_gender_penalty_value
			desc = appointment_male_only_desc
		}
	}
	else_if = {
		limit = {
			scope:title.holder.top_liege ?= { has_realm_law = female_preference_law }
			is_female = no
		}
		subtract = {
			value = appointment_non_preferred_gender_penalty_value
			desc = appointment_female_preference_desc
		}
	}
	else_if = {
		limit = {
			scope:title.holder.top_liege ?= { has_realm_law = female_only_law }
			is_female = no
		}
		subtract = {
			value = appointment_opposite_gender_penalty_value
			desc = appointment_female_only_desc
		}
	}

	# We remove all score from the emperor, to make sure you don't inherit governorships needlessly
	if = {
		limit = {
			is_independent_ruler = yes
		}
		subtract = {
			value = 1000
			desc = appointment_is_top_liege
		}
	}

	# Ceremonial Liege
	if = {
		limit = {
			tgp_is_ceremonial_liege_trigger = yes
			NOT = { scope:title ?= top_liege.primary_title }
		}
		subtract = {
			value = 1000
			desc = ceremonial_liege_desc
		}
	}
}

appointment_score_military = {
	# Martial skill - We use this as additional base for the overall score
	add = {
		value = martial
		multiply = 2
		desc = "[martial_skill|E]"
	}

	# Learning skill - We use this as additional base for the overall score if you have the correct cultural parameter 
	if = {
		limit = {
			culture = {
				has_cultural_parameter = military_governor_appointment_score
			}
		}
		add = {
			value = learning
			multiply = 0.5
			round = yes
			desc = "[learning_skill|E]"
		}
	}

	# Martial education provides additional score
	if = {
		limit = {
			has_trait = education_martial
		}
		if = {
			limit = {
				has_trait = education_martial_5
			}
			add = {
				value = 100
				desc = has_trait_education_martial_5
			}
		}
		else_if = {
			limit = {
				has_trait = education_martial_4
			}
			add = {
				value = 80
				desc = has_trait_education_martial_4
			}
		}
		else_if = {
			limit = {
				has_trait = education_martial_3
			}
			add = {
				value = 60
				desc = has_trait_education_martial_3
			}
		}
		else_if = {
			limit = {
				has_trait = education_martial_2
			}
			add = {
				value = 40
				desc = has_trait_education_martial_2
			}
		}
		else_if = {
			limit = {
				has_trait = education_martial_1
			}
			add = {
				value = 20
				desc = has_trait_education_martial_1
			}
		}
	}

	# Commander traits
	add = {
		value = number_of_commander_traits
		multiply = 20
		desc = number_of_commander_traits_desc
	}

	# Prowess
	add = {
		value = prowess
		desc = prowess_concept
	}
	
	# Influenced career
	if = {
		limit = {
			exists = var:merit_military_career_score_bonus
		}
		add = {
			value = var:merit_military_career_score_bonus
			desc = merit_career_score_bonus_desc
		}
	}

	if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_military_score_bonus_1
			}
		}
		add = {
			value = celestial_military_score_bonus_1_value
			desc = "[GetDomicileBuilding('east_asian_estate_commander_study_01').GetName]"
		}
	}
	else_if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_military_score_bonus_2
			}
		}
		add = {
			value = celestial_military_score_bonus_2_value
			desc = "[GetDomicileBuilding('east_asian_estate_commander_study_02').GetName]"
		}
	}
	else_if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_military_score_bonus_3
			}
		}
		add = {
			value = celestial_military_score_bonus_3_value
			desc = "[GetDomicileBuilding('east_asian_estate_commander_study_03').GetName]"
		}
	}
	else_if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_military_score_bonus_4
			}
		}
		add = {
			value = celestial_military_score_bonus_4_value
			desc = "[GetDomicileBuilding('east_asian_estate_commander_study_04').GetName]"
		}
	}

	# Warfare Legacy
	add = {
		value = 10
		multiply = dynasty.warfare_legacy_track_perks
		desc = has_warfare_legacy_track_desc
	}

	# PERKS
	if = {
		limit = {
			has_perk = loyalty_and_respect_perk
		}
		add = {
			value = candidate_pool_loyalty_and_respect_perk_score_value
			desc = loyalty_and_respect_perk_name_desc
		}
	}

	# COUNCIL POSITIONS
	if = { # Is the marshal of current holder
		limit = { scope:title.holder.cp:councillor_marshal ?= root }
		add = {
			value = 20
			desc = marshal_governor_desc
		}
	}

	# Must be eligible as a commander
	if = {
		limit = {
			NOT = {
				can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = top_liege }
			}
		}
		add = {
			value = -1000
			desc = cannot_be_commander
		}
	}
}

appointment_score_civic = {
	if = {
		limit = {
			OR = {
				has_trait = lunatic
				has_trait = possessed
				has_trait = incapable
				has_trait = intellect_bad_3
			}
		}
		add = {
			value = -100
			desc = "unsound_mind_desc"
		}
	}
	if = {
		limit = {
			top_liege = { government_allows = merit }
		}
		if = {
			limit = {
				has_trait = confucian_education
			}
			add = {
				value = "has_trait_xp(confucian_education)"
				divide = 4
				round = yes
				min = 5
				desc = court_position_confucian_education_trait
			}
		}
	}
	# Influenced career score
	if = {
		limit = {
			exists = var:merit_civilian_career_score_bonus
		}
		add = {
			value = var:merit_civilian_career_score_bonus
			desc = merit_career_score_bonus_desc
		}
	}

	if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_civilian_score_bonus_1
			}
		}
		add = {
			value = celestial_civilian_score_bonus_1_value
			desc = "[GetDomicileBuilding('east_asian_estate_office_01').GetName]"
		}
	}
	else_if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_civilian_score_bonus_2
			}
		}
		add = {
			value = celestial_civilian_score_bonus_2_value
			desc = "[GetDomicileBuilding('east_asian_estate_office_02').GetName]"
		}
	}
	else_if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_civilian_score_bonus_3
			}
		}
		add = {
			value = celestial_civilian_score_bonus_3_value
			desc = "[GetDomicileBuilding('east_asian_estate_office_03').GetName]"
		}
	}
	else_if = {
		limit = {
			domicile ?= {
				has_domicile_parameter = celestial_civilian_score_bonus_4
			}
		}
		add = {
			value = celestial_civilian_score_bonus_4_value
			desc = "[GetDomicileBuilding('east_asian_estate_office_04').GetName]"
		}
	}
	# PERKS
	if = {
		limit = {
			has_perk = ducal_conquest_perk
		}
		add = {
			value = candidate_pool_ducal_conquest_perk_score_value
			desc = friends_in_the_right_places
		}
	}
}

# for non-merit, civic and regent
appointment_score_non_military = {
	# POPULAR OPINION
	if = {
		limit = { is_landed = yes }
		add = {
			value = {
				value = capital_county.county_holder_opinion
				multiply = 0.25
				round = yes
				if = {
					limit = { scope:title.holder ?= top_liege }
					multiply = 2
				}
			}
			desc = county_holder_opinion_desc
		}
	}
	# COUNCIL POSITIONS
	if = {
		limit = {
			scope:title.holder ?= {
				any_councillor ?= { this = root }
			}
		}
		add = {
			value = 20
			desc = councillor_position_governor_desc
		}
	}
	# COURT POSITIONS
	if = {
		limit = {
			scope:title.holder ?= {
				any_court_position_holder ?= {
					this = root
				}
			}
		}
		add = {
			value = 10
			desc = court_position_succession_governor_desc
		}
	}
}

appointment_score_dynastic_cycle = {
	# Conquest Dynasty has a favored culture
	if = {
		limit = {
			top_liege = { has_title = title:h_china }
			situation:dynastic_cycle ?= {
				has_variable = dynastic_cycle_favored_culture
				root.culture = var:dynastic_cycle_favored_culture
			}
		}
		add = {
			value = 100
			desc = dynastic_cycle_conquerer_promoted_culture
		}
	}

	# MOVEMENTS
	if = {
		limit = {
			situation:dynastic_cycle ?= {
				situation_participant_group:expansion_movement = {
					participant_group_has_character = root
					is_movement_in_power = { MOVEMENT = expansion_movement }
				}
			}
		}
		add = {
			value = 20
			desc = expansion_movement_is_favored_appointment_desc
		}
	}
	if = {
		limit = {
			situation:dynastic_cycle ?= {
				situation_participant_group:advancement_movement = {
					participant_group_has_character = root
					is_movement_in_power = { MOVEMENT = advancement_movement }
				}
			}
		}
		add = {
			value = 20
			desc = advancement_movement_is_favored_appointment_desc
		}
	}
	if = {
		limit = {
			situation:dynastic_cycle ?= {
				situation_participant_group:pro_hegemon_movement = {
					participant_group_has_character = root
					#they don't have to be in power
				}
			}
		}
		add = {
			value = 20
			desc = pro_hegemon_movement_appointment_desc
		}
	}
	if = {
		limit = {
			situation:dynastic_cycle ?= {
				situation_participant_group:conservative_movement = {
					participant_group_has_character = root
					NOT = { is_movement_in_power = { MOVEMENT = conservative_movement } }
					#they have to be in power to remove the malus
				}
			}
		}
		add = {
			value = -20
			desc = conservative_movement_appointment_value
		}
	}
}

appointment_score_non_merit = {
	# PERKS
	if = {
		limit = {
			has_perk = ducal_conquest_perk
		}
		add = {
			value = 30
			desc = friends_in_the_right_places
		}
	}
	#SAME CULTURE AS REGION
	if = {
		limit = {
			exists = scope:title
			culture ?= scope:title.holder.capital_county.culture
		}
		add = {
			value = 10
			desc = of_local_culture_succession_desc
		}
	}
	# EXISTING GOVERNORSHIPS
	if = {
		limit = { is_governor = yes }
		add = {
			value = -1000
			desc = already_a_governor_tt
		}
	}
}

appointment_score_governor = {
	if = {
		limit = { is_at_war_with = liege }
		add = {
			value = -9999
			desc = game_concept_revolt
		}
	}
	if = {
		limit = { has_trait = governor }
		add = {
			value = 5
			if = {
				limit = {
					has_trait_xp = { trait = governor value >= 25 }
				}
				add = 5
			}
			if = {
				limit = {
					has_trait_xp = { trait = governor value >= 50 }
				}
				add = 5
			}
			if = {
				limit = {
					has_trait_xp = { trait = governor value >= 75 }
				}
				add = 5
			}
			desc = governor_trait_desc_short
		}
	}
	# RITE BASED
	if = {
		limit = {
			exists = top_liege.primary_title.state_rite
		}
		top_liege.primary_title.state_rite = {
			save_temporary_scope_as = target_rite
		}
	}
	else = {
		top_liege.rite = {
			save_temporary_scope_as = target_rite
		}
	}
	# RITE HOSTILITY
	if = {
		limit = {
			scope:target_rite ?= {
				rite_hostility_level = {
					target = root.rite
					value = faith_astray_level
				}
			}
		} #Change to faith acceptance
		subtract = {
			value = 10
			desc = astray_faith_succession_desc
		}
	}
	if = {
		limit = {
			scope:target_rite ?= {
				rite_hostility_level = {
					target = root.rite
					value = faith_hostile_level
				}
			}
		} #Change to faith acceptance
		subtract = {
			value = 25
			desc = hostile_faith_succession_desc
		}
	}
	if = {
		limit = {
			scope:target_rite ?= {
				rite_hostility_level = {
					target = root.rite
					value >= faith_evil_level
				}
			}
		} #Change to faith acceptance
		subtract = {
			value = 50
			desc = evil_faith_sucession_desc
		}
	}
}

appointment_score_final_factors = {
	add = appointment_expand_power_base_bonus
	if = {
		limit = {
			tgp_realm_has_ceremonial_liege_trigger = yes
		}
		# Petition
		if = {
			limit = {
				exists = var:petition_liege_regent_support
				top_liege.primary_title.var:administrative_ui_special_title.holder ?= var:petition_liege_regent_support
				scope:title ?= top_liege.primary_title # Only top liege title
			}
			multiply = {
				value = 1.1
				desc = petition_liege_regent_support_desc
			}
		}
		# PETITION
		else_if = {
			limit = {
				exists = house.var:petition_liege_appointment_support
				top_liege.primary_title.var:administrative_ui_special_title.holder ?= house.var:petition_liege_appointment_support
				exists = scope:title
				scope:title ?= top_liege.primary_title # Only governorships
			}
			multiply = {
				value = 1.15
				desc = ceremonial_liege_appointment_support_desc
			}
		}
	}
}

appointment_score_regent = {
	# SAME CULTURE AS REGION
	#if = {
	#	limit = {
	#		tgp_is_in_ceremonial_house_trigger = no
	#		exists = scope:title
	#		#NOR = {
	#		#	culture = scope:title.holder.capital_county.culture
	#		#	top_liege.primary_title.var:administrative_ui_special_title.holder.culture = culture
	#		#}
	#	}
	#	add = {
	#		value = -50
	#		desc = not_of_local_culture_succession_desc
	#	}
	#}
	# Emperor's opinion
	if = {
		limit = {
			exists = top_liege.primary_title.var:administrative_ui_special_title.holder
			this != top_liege.primary_title.var:administrative_ui_special_title.holder
		}
		add = {
			value = "top_liege.primary_title.var:administrative_ui_special_title.holder.opinion(root)"
			divide = 2
			desc = ceremonial_liege_opinion_desc
		}
	}
	# BONUSES FOR IMPERIAL FAMILY
	if = {
		limit = {
			tgp_realm_has_ceremonial_liege_trigger = yes
			tgp_is_in_ceremonial_house_trigger = yes
		}
		if = { # Emperor
			limit = { this = top_liege.primary_title.var:administrative_ui_special_title.holder }
			add = {
				value = 100
				desc = ceremonial_liege_desc
			}
		}
		else_if = {
			limit = { this = top_liege.primary_title.var:administrative_ui_special_title.holder.player_heir }
			add = {
				value = 50
				desc = ceremonial_house_heir_desc
			}
		}
		else = {
			add = {
				value = -150
				desc = ceremonial_house_non_heir_desc
			}
		}
	}
	# BONUSES FOR NON-IMPERIAL FAMILY
	else_if = {
		limit = { tgp_realm_has_ceremonial_liege_trigger = yes }
		# PAST REGENT OF YOUR DYNASTY
		add = {
			value = japanese_kampaku_house_past_kampaku_value
			desc = japanese_kampaku_house_past_kampaku_desc
		}
		# Number of House children with Imperial dynasty parents (e.g. Fujiwara born to a Yamato mother)
		add = {
			value = japanese_kampaku_house_imperial_parent_value_actual
			desc = japanese_kampaku_house_imperial_parent_desc
		}
		# Number of Imperial dynasty members with House parents (e.g. Yamato born to a Fujiwara mother)
		add = {
			value = japanese_kampaku_house_imperial_child_score_value
			desc = japanese_kampaku_house_imperial_child_desc
		}
		# Number of Imperial spouse House members (e.g. Emperor married to a Fujiwara)
		add = {
			value = japanese_kampaku_imperial_spouse_score_value
			desc = japanese_kampaku_imperial_spouse_desc
		}
		# Number of Imperial dynasty members married to House members (e.g. Yamato married to a Fujiwara)
		add = {
			value = japanese_kampaku_house_imperial_spouse_score_value
			desc = japanese_kampaku_house_imperial_spouse_desc
		}
	}
	# Age
	else_if = {
		limit = { age < 50 }
		add = {
			value = 50
			subtract = age
			multiply = -2
			desc = japanese_kampaku_age_desc
		}
	}
	else = {
		add = {
			value = age
			subtract = 50
			multiply = 2
			desc = japanese_kampaku_age_desc
		}
	}
	# Number of Governorships held by House
	add = {
		value = japanese_kampaku_house_governorship_score_value
		desc = japanese_kampaku_house_governorship_desc
	}
	# Number of Devoted House members (monks/nuns)
	add = {
		value = japanese_kampaku_house_devoted_score_value
		desc = japanese_kampaku_house_devoted_desc
	}
	# Domicile is in the capital
	if = {
		limit = {
			# A landless top liege has no capital county to match against
			exists = root.top_liege.capital_county
			house ?= {
				any_house_member = {
					any_held_title = { is_noble_family_title = yes }
					domicile ?= {
						domicile_is_estate_trigger = yes
						domicile_location.county ?= root.top_liege.capital_county
					}
				}
			}
		}
		add = 25
	}
	# Block non-admin
	if = {
		limit = {
			NOT = { government_has_mechanic = administrative }
		}
		add = -2500
	}
}

# avoid being governor in where you grow up
appointment_score_celestial = {
	# Domicile
	if = {
		limit = {
			exists = scope:title
			root.dynasty.dynast ?= {
				has_domicile = yes
			}
			scope:title = {
				is_de_jure_liege_or_above_target = root.dynasty.dynast.domicile.domicile_location.county
			}
		}
		subtract = {
			value = 20
			desc = estate_location_in_area
		}
	}
	# Same dynasty as holder penalty
	if = {
		limit = {
			exists = dynasty
			OR = {
				scope:title.holder.dynasty ?= root.dynasty
				liege.dynasty ?= root.dynasty
			}
		}
		subtract = {
			value = 50
			desc = too_closely_related
		}
	}
}
