﻿courtier_management = {
	# Does the character want to leave the court? Checked once a year for each ruler.
	# root, scope:courtier = the courtier
	# scope:liege = their liege
	can_leave = {
		courtier_allowed_to_leave_trigger = yes
		trigger_if = {
			limit = { is_married = yes }
			NOT = {
				any_consort = {
					location ?= scope:courtier.location
					is_ruler = no
					courtier_willing_to_leave_trigger = no
				}
			}
		}
		NOT = {
			any_child = {
				is_adult = no
				is_ruler = no
				location ?= scope:courtier.location
				NAND = {
					exists = scope:liege.dynasty
					dynasty ?= scope:liege.dynasty
				}
				courtier_willing_to_leave_trigger = no
			}
		}
	}

	# Chance that the courtier decides to leave in ‰ (= 10 x the likelihood in %). While can_leave is true this is evaluated every month.
	# We strive to keep this value around 20 = 2% (min 10 max 30). This equals a ~50% chance of having left within 3 years
	# root, scope:courtier = the courtier
	# scope:liege = their liege
	# base = 1 unless overridden
	monthly_leave_chance_x10 = {
		base = 20

		###AI VALUES###
		#Max: 9
		#Min: -9
		ai_value_modifier = {
			ai_energy = {
				value = high_positive_ai_value_inverse
				multiply = 3
			}
			ai_boldness = {
				value = high_positive_ai_value_inverse
				multiply = 3
			}
			ai_greed = {
				value = high_positive_ai_value_inverse
				multiply = 3
			}
		}

		###CLAIMANTS###
		#Min: -10
		modifier = {
			any_claim = { useful_courtier_or_guest_claim_trigger = { RULER = scope:liege } }
			add = -10
		}

		###RELATION TO LIEGE###
		#Max: 10
		#Min: -12
		opinion_modifier = {
			opinion_target = scope:liege
			multiplier = -0.1
			min = -5
			max = 5
		}
		modifier = {
			has_relation_friend = scope:liege
			add = -2
		}
		modifier = {
			has_relation_lover = scope:liege
			add = -2
		}
		modifier = {
			is_close_family_of = scope:liege
			add = -2
		}
		modifier = {
			OR = {
				has_relation_rival = scope:liege
				has_relation_grudge = scope:liege
			}
			add = 5
		}
		modifier = {
			has_relation_potential_rival = scope:liege
			add = 2
		}
		modifier = {
			any_child = {
				any_parent = {
					this = scope:liege
				}
			}
			add = -1
		}
		modifier = {
			scope:liege = { has_imprisonment_reason = scope:courtier }
			add = 3
		}
		modifier = {
			NOT = { faith = scope:liege.faith }
			add = 1
		}
		modifier = {
			NOT = { religion = scope:liege.religion }
			add = 1
		}
		modifier = {
			culture = {
				NOT = { has_same_culture_heritage = scope:liege.culture }
			}
			add = 1
		}
		modifier = {
			is_adult = yes
			age <= 45
			is_faith_dominant_gender = yes
			OR = {
				is_child_of = scope:liege
				is_sibling_of = scope:liege
			}
			NOR = {
				any_heir_title = {}
				any_pretender_title = {
					place_in_line_of_succession = { target = scope:courtier value < 3 }
				}
			}
			add = 5
		}

		###RELATION TO OTHER COURTIERS###
		#Max: 5
		modifier = {
			any_relation = { type = lover is_courtier_of = scope:liege }
			add = -2
		}
		modifier = {
			any_relation = { type = friend is_courtier_of = scope:liege }
			add = -1
		}
		modifier = {
			any_close_family_member = {
				this != scope:liege #There is already a family bonus for liege
				NOT = { is_child_of = scope:courtier }
				is_courtier_of = scope:liege
				reverse_opinion = {
					target = scope:courtier
					value >= high_positive_opinion
				}
			}
			add = -2
		}

		###BASTARDS###
		modifier = {
			has_trait = bastard
			add = 3
		}
		
		###TRAITS###
		modifier = {
			has_trait = ambitious
			add = 3
		}
		modifier = {
			has_trait = content
			add = -3
		}
		modifier = {
			has_trait = loyal
			add = -3
		}
		modifier = {
			has_trait = disloyal
			add = 3
		}
		modifier = {
			has_trait = disinherited
			add = 10
		}
		modifier = {
			has_trait = lifestyle_traveler
			add = 1
		}
		modifier = {
			has_trait = lifestyle_traveler
			has_trait_xp = {
				trait = lifestyle_traveler
				track = travel
				value >= trait_second_level
			}
			add = 1
		}
		modifier = {
			has_trait = lifestyle_traveler
			has_trait_xp = {
				trait = lifestyle_traveler
				track = travel
				value >= trait_third_level
			}
			add = 1
		}
		modifier = {
			has_any_aging_trait = yes
			add = -10
		}

		###HEALTH###
		#Min: -13
		modifier = {
			has_trait = pregnant
			add = -10
		}
		modifier = {
			health <= poor_health
			add = -3
		}

		###FAMILY HANDLING###
		modifier = {
			primary_spouse ?= { is_courtier_of = scope:liege }
			add = -5 #Reduced because spouse might also do leaving calculations
		}
		modifier = {
			any_child = {
				is_courtier_of = scope:liege
				age < 5
			}
			add = -5
		}

		###CULTURAL TRADITIONS###
		modifier = {
			culture = {
				has_cultural_parameter = more_likely_to_leave_court
			}
			add = 5
		}
		modifier = {
			culture = {
				has_cultural_parameter = courtiers_less_likely_to_leave_same_culture_court
			}
			this.culture = scope:liege.culture
			add = -3
		}

		#stressed family duty
		modifier = {
			scope:liege = {
				has_character_modifier = united_family_modifier
			}
			is_close_family_of = scope:liege
			add = -5
		}


		min = {
			value = {
				value = 10
				if = {
					limit = { is_adult = no }
					multiply = 0 #Children shouldn't try to leave on their own
				}
			}
		}
		max = {
			value = {
				value = 30
				if = {
					limit = { is_adult = no }
					multiply = 0 #Children shouldn't try to leave on their own
				}
			}
		}
	}
}
