﻿capital_province_being_besieged = {
	capital_province = {
		any_army_in_location = {
			army_owner = {
				is_at_war_with = root
			}
		}
	}
}

is_in_same_army_location_as = {
	is_in_army = yes
	$CHARACTER$ = { is_in_army = yes }

	trigger_if = {
		limit = { exists = knight_army }
		knight_army = { save_temporary_scope_as = my_army }
	}
	trigger_else = {
		commanding_army ?= { save_temporary_scope_as = my_army }
	}

	trigger_if = {
		limit = { exists = $CHARACTER$.knight_army }
		$CHARACTER$.knight_army = { save_temporary_scope_as = their_army }
	}
	trigger_else = {
		$CHARACTER$.commanding_army ?= { save_temporary_scope_as = their_army }
	}

	OR = {
		AND = {
			exists = scope:my_army
			exists = scope:their_army
			scope:my_army = scope:their_army
		}
		AND = {
			exists = scope:my_army.army_owner
			exists = scope:their_army.army_owner
			exists = scope:my_army.location
			exists = scope:their_army.location
			scope:my_army.army_owner = scope:their_army.army_owner
			scope:my_army.location = scope:their_army.location
		}
	}
}

is_in_same_location_or_activity_as = {
	OR = {
		# they're physically at the same location
		is_at_same_location = $CHARACTER$
        # they're traveling together
        current_travel_plan ?= {
            OR = {
                travel_plan_owner = { this = $CHARACTER$ }
                any_entourage_character = { this = $CHARACTER$ }
            }
        }
        # they're at the same activity
        involved_activity ?= {
            OR = {
                activity_host = { this = $CHARACTER$ }
                any_attending_character = {
                    this = $CHARACTER$
                    is_travelling = no
                }
            }
        }
		# they're in the same army location
		is_in_same_army_location_as = { CHARACTER = $CHARACTER$ }
	}
}

available_or_same_location = {
	NOR = {
		has_variable = {
			name = homage_liege_scope
			name = meditation_character_flag
			name = local_shrine_rite
			name = petition_liege_character_flag
			name = holding_court_character_flag
			name = gone_adventuring
			name = busy_in_mandala_succession
		}
		is_being_visited_on_tour_strict = yes
	}
	OR = {
		is_travelling = no
		current_travel_plan = {
			OR = {
				travel_plan_owner = { this = $EXCEPTION$ }
				any_entourage_character = { this = $EXCEPTION$ }
			}
		}
	}
	OR = {
		NOT = { exists = involved_activity }
		involved_activity = {
			OR = {
				activity_host = { this = $EXCEPTION$ }
				any_attending_character = {
					this = $EXCEPTION$
					is_travelling = no
				}
			}
		}
	}
	OR = {
		is_in_army = no
		is_in_same_army_location_as = { CHARACTER = $EXCEPTION$ }
	}
}

is_preferred_gender_of_ruler_realm_law = {
	trigger_if = {
		limit = {
			$RULER$ = {
				trigger_if = {
					limit = { is_ruler = yes }
					OR = {
						has_realm_law = male_only_law
						has_realm_law = male_preference_law
					}
				}
				trigger_else_if = {
					limit = { exists = liege }
					liege = {
						OR = {
							has_realm_law = male_only_law
							has_realm_law = male_preference_law
						}
					}
				}
				trigger_else = {
					faith = { has_doctrine = doctrine_gender_male_dominated }
				}
			}
		}
		is_male = yes
	}
	trigger_else_if = {
		limit = {
			$RULER$ = {
				trigger_if = {
					limit = { is_ruler = yes }
					OR = {
						has_realm_law = female_only_law
						has_realm_law = female_preference_law
					}
				}
				trigger_else_if = {
					limit = { exists = liege }
					liege = {
						OR = {
							has_realm_law = female_only_law
							has_realm_law = female_preference_law
						}
					}
				}
				trigger_else = {
					faith = { has_doctrine = doctrine_gender_female_dominated }
				}
			}
		}
		is_female = yes
	}
	trigger_else = {
		always = yes
	}
}

is_old_age_or_not_have_children = {
	trigger_if = {
		limit = { is_female = yes }
		OR = {
			age > define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
			age > define:NCharacter|FEMALE_ELDERLY_AGE
		}
	}
	trigger_else = {
		age > define:NCharacter|MALE_ELDERLY_AGE
	}
}

is_pious_or_clergy = {
	OR = {
		ai_zeal >= 100
		AND = {
			OR = {
				is_clergy = yes
				has_council_position = councillor_court_chaplain
				has_trait = septon
				has_trait = silent_sister
			}
			ai_zeal > 0
		}
	}
}

army_can_have_army_events = {
	army_size >= 1000
	is_army_in_combat = no
	is_army_in_raid = no
	exists = location
	location = {
		is_sea_province = no
		NOT = { this = root.capital_province }
	}
}

has_recoverable_disease_trigger = {
	OR = {
		has_trait = ill
		has_trait = pneumonic
		has_trait = consumption
		has_trait = typhus
		has_trait = smallpox
		has_trait = bubonic_plague
		has_trait = ergotism
		has_trait = measles
		has_trait = dysentery
	}
}

could_potentially_inherit_titles = {
	$HEIR$ = {
		any_parent = {
			is_alive = yes
			OR = {
				any_heir_title = {
					$HEIR$ = {
						character_gender_can_rule_title_trigger = {
							GENDER = $HEIR$
							TITLE = prev
						}
					}
				}
				AND = {
					is_ruler = yes
					NOR = {
						has_government = mercenary_government
						has_government = republic_government
						has_government = theocracy_government
						has_government = holy_order_government
					}
					any_held_title = {
						$HEIR$ = {
							character_gender_can_rule_title_trigger = {
								GENDER = $HEIR$
								TITLE = prev
							}
						}
					}
				}
			}
		}
	}
}

#would the character in scope leave CURRENT_LIEGE's court to move to NEW_LIEGE's court?
would_leave_liege_trigger = {
	NOR = {
		is_obedient_to = $CURRENT_LIEGE$
		is_courtier_of = $NEW_LIEGE$
		any_heir_title = { holder ?= $CURRENT_LIEGE$ }
		any_pretender_title = {
			holder ?= $CURRENT_LIEGE$
			place_in_line_of_succession = {
				target = prev
				value < 3
			}
		}
		is_councillor_of = $CURRENT_LIEGE$
		is_consort_of = $CURRENT_LIEGE$
		has_relation_friend = $CURRENT_LIEGE$
		has_relation_lover = $CURRENT_LIEGE$
		has_relation_rival = $NEW_LIEGE$
		$NEW_LIEGE$ = { has_imprisonment_reason = prev }
		has_opinion_modifier = { modifier = demanded_recruitment target = $CURRENT_LIEGE$ }
		has_opinion_modifier = { modifier = loyal_servant target = $CURRENT_LIEGE$ }
		$CURRENT_LIEGE$ = { has_hook = prev }
		AND = {
			exists = inspiration
			has_completed_inspiration = no
		}
		AND = {
			NOT = { has_relation_soulmate = $NEW_LIEGE$ }
			OR = {
				has_any_court_position = yes
				is_knight_of = $CURRENT_LIEGE$
			}
			OR = {
				opinion = { target = $CURRENT_LIEGE$ value >= 50 }
				AND = {
					save_temporary_opinion_value_as = { name = opinion_of_you target = $NEW_LIEGE$ }
					opinion = { target = $CURRENT_LIEGE$ value > scope:opinion_of_you }
				}
			}
		}
	}
}

#this is based on has_any_major_revenge_opinion_against_character_trigger but removes some of the less egregious offences
has_simplified_revenge_opinion_against_character_trigger = {
	OR = {
		has_opinion_modifier = {
			modifier = imprisoned_me
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = banished_me
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = killed_my_child
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = botched_my_treatment_crime_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = botched_treatment_of_kin_crime_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = theft_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = embezzled_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = attempted_arrest_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = attempted_imprisonment_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = abducted_me_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = attempted_abduction_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = murdered_close_family_crime
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = attempted_murder_close_family_crime
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = attempted_murder_me_crime
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = murdered_close_relation_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = murdered_someone_close_to_me_crime
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = hurt_someone_close_to_me_crime
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = executed_close_family
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = executed_close_family_crime
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = tortured_family_member
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = tortured_me
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = blinded_family_member
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = blinded_me
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = castrated_family_member
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = castrated_me
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = slept_with_spouse_discovered_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = slept_with_spouse_exposed_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = unfaithful_spouse_discovered_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = unfaithful_spouse_exposed_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = refused_to_renounce_lover_opinion
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = betrayed_our_promise
			target = $CHARACTER$
		}
		has_opinion_modifier = {
			modifier = reason_child_died
			target = $CHARACTER$
		}
	}
}

#used for determining if the character should use player-only realistic pregnancy rule
should_use_realistic_pregnancy_player_only = {
	OR = {
		is_ai = no
		primary_spouse = { is_ai = no }
		AND = {
			is_courtier = yes
			liege = { is_ai = no }
		}
	}
}

#both of the below are used for determining valid pregnancies
is_at_same_location_or_activity_as_father = {
    # first make sure there IS a father - if not, we need to fail the trigger immediately
    OR = {
        exists = scope:real_father
        exists = scope:father
    }
    # then track which kind of father it is
    trigger_if = {
        limit = { exists = scope:real_father }
        scope:real_father = { save_temporary_scope_as = father_check }
    }
    trigger_else = {
        scope:father = { save_temporary_scope_as = father_check }
    }
	
	# and neither of them are imprisoned
	is_imprisoned = no
	scope:father_check = { is_imprisoned = no }
	
    # now let's see if they're close enough to have a pregnancy
    is_in_same_location_or_activity_as = { CHARACTER = scope:father_check }
}

realistic_pregnancy_system_prevents_pregnancy = {
	# the realistic pregnancies game rule is set
	OR = {
		has_game_rule = lmf_realistic_pregnancies_active
		AND = {
			has_game_rule = lmf_realistic_pregnancies_player_only
			OR = {
				should_use_realistic_pregnancy_player_only = yes
				scope:real_father = { should_use_realistic_pregnancy_player_only = yes }
			}
		}
	}
	NOR = {
		# it's not an event-driven pregnancy (such as via had_sex_with_effect)
		has_character_flag = allow_event_pregnancy
		# the characters aren't in the same location or activity
		is_at_same_location_or_activity_as_father = yes
		
		### covering some uses of make_pregnant rather than had_sex_with_effect in vanilla events
		scope:real_father = { has_character_flag = adventurer_pregnant_lover_flag } # from fund_inspiration.6513
		has_character_flag = pregnancy_real_father_of_bastard_is_known_flag # from stewardship_duty.1055, normally set *after* the pregnancy notification
		has_character_flag = medicine_learning_fertility_treamtent_unexpected_trait # from learning_medicine.2023, one of three options covered
		exists = var:borte_first_child_var # from mpo_temujin_flavor.0030
		# nothing for ep3_wedding.0003 just yet
		# nothing for lover.8102 just yet
	}
}

should_disallow_pregnancy_trigger = {
	OR = {
		# disallow the pregnancy if a flag has been set on either the mother or father to prevent them from having children
		has_character_flag = lmf_no_children
		scope:real_father = { has_character_flag = lmf_no_children }
		has_inactive_trait = infertile
		scope:real_father = { has_inactive_trait = infertile }
		# Unfruitful Marriage Relation
		has_relation_unfruitful_marriage = scope:real_father
		# Realistic Pregnancy system
		realistic_pregnancy_system_prevents_pregnancy = yes
	}
}

is_interesting_character_trigger = {
	OR = {
		is_lowborn = no
		has_trait = intellect_good
		has_trait = physique_good
		has_trait = beauty_good
		diplomacy >= high_skill_rating
		martial >= high_skill_rating
		stewardship >= high_skill_rating
		intrigue >= high_skill_rating
		learning >= high_skill_rating
		prowess >= high_skill_rating
	}
}

#these are used for rivalry events
has_revenge_motivation_against = {
	ai_vengefulness >= 0
	has_simplified_revenge_opinion_against_character_trigger = { CHARACTER = $CHARACTER$ }
}

is_resentful_of_impious_character = {
	OR = {
		has_personality_malicious_trigger = yes
		is_hot_headed_trigger = yes
	}
	is_pious_or_clergy = yes
	$CHARACTER$.ai_zeal <= -100
	faith = $CHARACTER$.faith
}

is_resentful_of_pious_character = {
	OR = {
		has_personality_malicious_trigger = yes
		is_hot_headed_trigger = yes
	}
	$CHARACTER$ = { is_pious_or_clergy = yes }
	ai_zeal <= -100
	faith = $CHARACTER$.faith
}

is_resentful_of_hostile_faith_character = {
	OR = {
		has_personality_malicious_trigger = yes
		is_hot_headed_trigger = yes
	}
	is_pious_or_clergy = yes
	faith = {
		faith_hostility_level = {
			target = $CHARACTER$.faith
			value >= faith_hostile_level
		}
	}
}

is_resentful_of_pious_astray_faith_character = {
	OR = {
		has_personality_malicious_trigger = yes
		is_hot_headed_trigger = yes
	}
	is_pious_or_clergy = yes
	$CHARACTER$ = { is_pious_or_clergy = yes }
	faith = {
		faith_hostility_level = {
			target = $CHARACTER$.faith
			value = faith_astray_level
		}
	}
}

has_hatred_towards = {
	age <= $CHARACTER$.age_plus_10
	age >= $CHARACTER$.age_minus_10
	OR = {
		has_personality_malicious_trigger = yes
		is_hot_headed_trigger = yes
	}
	has_moderate_bad_opinion_of_character_trigger = { CHARACTER = $CHARACTER$ }
}

has_major_incompatibility_with = {
	age <= $CHARACTER$.age_plus_10
	age >= $CHARACTER$.age_minus_10
	has_personality_benevolent_trigger = no
	has_personality_submissive_trigger = no
	trait_compatibility = { target = $CHARACTER$ value <= -20 }
	NOT = {
		has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = $CHARACTER$ }
	}
}

is_jealous_of_their_lover = {
	save_temporary_scope_as = jealous_lover
	$CHARACTER$ = {
		any_relation = {
			type = lover
			OR = {
				any_secret = {
					type = secret_lover
					is_known_by = scope:jealous_lover
					secret_target = $CHARACTER$
				}
				NOT = {
					any_secret = {
						type = secret_lover
						secret_target = $CHARACTER$
					}
				}
			}
			OR = {
				has_relation_lover = scope:jealous_lover
				AND = {
					has_relation_old_flame = scope:jealous_lover
					reverse_opinion = { target = scope:jealous_lover value >= 40 }
				}
			}
		}
	}
}

has_equal_or_better_aptitude_for_court_position = {
	$CHARACTER$ = { has_court_position = $POSITION$ }
	can_be_employed_as = $POSITION$
	aptitude = {
		court_position = $POSITION$
		value >= $CHARACTER$.aptitude:$POSITION$
	}
}

is_jealous_of_their_court_position = {
	$CHARACTER$ = { has_any_court_position = yes }
	opinion = { target = $CHARACTER$ value < 0 }
	is_jealous_trigger = yes
	is_adult = yes
	is_councillor = no
	has_any_court_position = no
	exists = liege
	liege = $CHARACTER$.liege
	OR = {
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = court_physician_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = lady_in_waiting_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = travel_leader_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = court_tutor_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = master_of_horse_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = master_of_hunt_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = royal_architect_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = seneschal_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = court_poet_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = court_musician_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = champion_court_position }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = second_camp_officer }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = quartermaster_camp_officer }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = chief_engineer_camp_officer }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = master_bard_camp_officer }
		has_equal_or_better_aptitude_for_court_position = { CHARACTER = $CHARACTER$ POSITION = master_of_arms_camp_officer }
	}
}

is_jealous_of_their_council_position = {
	$CHARACTER$ = { is_councillor = yes }
	opinion = { target = $CHARACTER$ value < 0 }
	is_jealous_trigger = yes
	is_adult = yes
	is_councillor = no
	exists = liege
	liege = $CHARACTER$.liege
	NOT = { is_consort_of = liege }
	OR = {
		is_ruler = yes
		$CHARACTER$ = { is_ruler = no }
	}
	OR = {
		AND = {
			$CHARACTER$ = { has_council_position = councillor_chancellor }
			diplomacy_plus_2 >= $CHARACTER$.diplomacy
		}
		AND = {
			$CHARACTER$ = { has_council_position = councillor_marshal }
			martial_plus_2 >= $CHARACTER$.martial
		}
		AND = {
			$CHARACTER$ = { has_council_position = councillor_steward }
			stewardship_plus_2 >= $CHARACTER$.stewardship
		}
		AND = {
			$CHARACTER$ = { has_council_position = councillor_spymaster }
			intrigue_plus_2 >= $CHARACTER$.intrigue
		}
	}
}

is_jealous_of_their_heir_position = {
	is_jealous_trigger = yes
	NOT = {
		has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = $CHARACTER$ }
	}
	save_temporary_scope_as = title_check
	any_pretender_title = {
		tier >= tier_county
		OR = {
			scope:title_check = { is_ruler = no }
			tier > scope:title_check.highest_held_title_tier
		}
		place_in_line_of_succession = {
			target = scope:title_check
			value < 4
		}
		place_in_line_of_succession = {
			target = $CHARACTER$
			value < "place_in_line_of_succession(scope:title_check)"
		}
	}
}

has_issue_with_disputed_heritage_of = {
	$CHARACTER$ = {
		has_trait = disputed_heritage
		has_dynasty = yes
	}
	dynasty = $CHARACTER$.dynasty
	is_dynast = no
	OR = {
		has_trait = arrogant
		has_personality_malicious_trigger = yes
	}
	NOR = {
		has_relation_friend = $CHARACTER$
		is_grandparent_of = $CHARACTER$
		is_parent_of = $CHARACTER$
		is_child_of = $CHARACTER$
		is_grandchild_of = $CHARACTER$
		is_illegitimate = yes
		has_trait = disputed_heritage
		AND = {
			exists = $CHARACTER$.real_father
			this = $CHARACTER$.real_father
		}
	}
}

has_any_trait_disapproved_of_by = {
	OR = {
		#KINSLAYER
		AND = {
			has_trait = kinslayer_3
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:kinslayer_3 GENDER_CHARACTER = prev } }
		}
		AND = {
			has_trait = kinslayer_2
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:kinslayer_2 GENDER_CHARACTER = prev } }
		}
		AND = {
			has_trait = kinslayer_1
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:kinslayer_1 GENDER_CHARACTER = prev } }
		}
		#WITCH
		AND = {
			has_trait = witch
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:witch GENDER_CHARACTER = prev } }
		}
		#CANNIBAL
		AND = {
			has_trait = cannibal
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:cannibal GENDER_CHARACTER = prev } }
		}
		#SODOMITE
		AND = {
			has_trait = sodomite
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:sodomite GENDER_CHARACTER = prev } }
		}
		#DEVIANT
		AND = {
			has_trait = deviant
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:deviant GENDER_CHARACTER = prev } }
		}
		AND = {
			has_trait = incestuous
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:incestuous GENDER_CHARACTER = prev } }
		}
		#ADULTERER/FORNICATOR
		AND = {
			has_trait = adulterer
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:adulterer GENDER_CHARACTER = prev } }
		}
		AND = {
			has_trait = fornicator
			$CHARACTER$ = { has_negative_attitude_towards_trait_trigger = { TRAIT = trait:fornicator GENDER_CHARACTER = prev } }
		}
	}
}

#used to identify rulers in a player realm, but to be conscious of processing
is_player_realm_ruler = {
	exists = capital_province
	any_player = {
		is_liege_or_above_of = $RULER$
		capital_province.kingdom ?= $RULER$.capital_province.kingdom
		trigger_if = {
			limit = { highest_held_title_tier > tier_duchy }
			$RULER$.highest_held_title_tier >= tier_duchy
		}
	}
}

# used for courtiers leaving their liege
pool_character_can_go_to_holy_order = {
	is_physically_able_adult = yes
	exists = location
	has_any_aging_trait = no
	is_healthy = yes
	OR = {
		ai_zeal >= 0
		faith = { exists = great_holy_war }
	}
	OR = {
		has_education_martial_trigger = yes
		prowess >= 14
	}
	faith = {
		any_faith_holy_order = {
			leader = {
				is_alive = yes
				$CHARACTER$ = {
					can_be_warrior_trigger = { ARMY_OWNER = prev }
					NOT = { has_relation_rival = prev }
				}
				available_courtier_slot > 0
			}
		}
	}
	NOR = {
		has_trait = order_member
		has_trait = excommunicated
		has_trait = broke_vows
		exists = secret_faith
		primary_spouse ?= { has_relation_lover = $CHARACTER$ }
		AND = {
			is_married = yes
			ai_honor > 0
		}
		AND = {
			patrilinear_marriage = yes
			primary_spouse = {
				is_male = yes
				location ?= $CHARACTER$.location
			}
		}
		AND = {
			matrilinear_marriage = yes
			primary_spouse = {
				is_female = yes
				location ?= $CHARACTER$.location
			}
		}
		any_child = {
			is_adult = no
			location ?= $CHARACTER$.location
		}
	}
}

pool_character_can_take_holy_vows = {
	is_physically_able = yes
	age >= 14
	exists = location
	ai_zeal >= 0
	faith = { has_doctrine_parameter = take_vows_active }
	NOR = {
		has_trait = devoted
		has_trait = excommunicated
		has_trait = broke_vows
		exists = secret_faith
		AND = {
			is_married = yes
			ai_honor > 0
		}
		AND = {
			patrilinear_marriage = yes
			primary_spouse = {
				is_male = yes
				location ?= $CHARACTER$.location
			}
		}
		AND = {
			matrilinear_marriage = yes
			primary_spouse = {
				is_female = yes
				location ?= $CHARACTER$.location
			}
		}
		any_child = {
			is_adult = no
			location ?= $CHARACTER$.location
		}
	}
}

adventurer_can_use_pool_character = {
	OR = {
		AND = {
			$CHARACTER$ = {
				OR = {
					martial >= $SKILL$
					prowess >= $SKILL$
				}
				can_be_warrior_trigger = { ARMY_OWNER = prev }
			}
		}
		AND = {
			$CHARACTER$.diplomacy >= $SKILL$
			NOT = { any_courtier = { diplomacy >= $CHARACTER$.diplomacy } }
		}
		AND = {
			$CHARACTER$.stewardship >= $SKILL$
			NOT = { any_courtier = { stewardship >= $CHARACTER$.stewardship } }
		}
		AND = {
			$CHARACTER$.intrigue >= $SKILL$
			NOT = { any_courtier = { intrigue >= $CHARACTER$.intrigue } }
		}
		AND = {
			$CHARACTER$.learning >= $SKILL$
			NOT = { any_courtier = { learning >= $CHARACTER$.learning } }
		}
	}
}

pool_character_can_join_random_adventurer = {
	is_physically_able_adult = yes
	exists = location
	has_any_aging_trait = no
	is_healthy = yes
	ai_boldness >= 0
	ai_energy >= 0
	OR = {
		highest_skill_value >= 15
		prowess >= 15
	}
	any_independent_ruler = {
		NOR = {
			is_parent_of = $CHARACTER$
			$CHARACTER$.host ?= this
			$CHARACTER$ = this
		}
		is_physically_able_ai_adult = yes
		has_government = landless_adventurer_government
		exists = domicile.domicile_location
		NOT = { has_relation_rival = $CHARACTER$ }
		OR = {
			faith = $CHARACTER$.faith
			faith = {
				faith_hostility_level = {
					target = $CHARACTER$.faith
					value <= faith_astray_level
				}
			}
		}
		available_courtier_slot > 0
		adventurer_can_use_pool_character = { CHARACTER = $CHARACTER$ SKILL = 15 }
		domicile.domicile_location = {
			squared_distance = { target = $CHARACTER$.location value <= 100000 }
		}
	}
}

pool_character_can_join_dynasty_adventurer = {
	is_physically_able_adult = yes
	exists = location
	has_any_aging_trait = no
	is_healthy = yes
	ai_boldness >= 0
	ai_energy >= 0
	dynasty ?= {
		any_dynasty_member = {
			NOR = {
				is_parent_of = $CHARACTER$
				$CHARACTER$.host ?= this
				$CHARACTER$ = this
			}
			OR = {
				AND = {
					is_landless_adventurer = yes
					is_physically_able_ai_adult = yes
					exists = domicile.domicile_location
					NOT = { has_relation_rival = $CHARACTER$ }
					available_courtier_slot > 0
				}
				AND = {
					is_courtier = yes
					liege = {
						is_landless_adventurer = yes
						is_physically_able_ai_adult = yes
						exists = domicile.domicile_location
						NOT = { has_relation_rival = $CHARACTER$ }
						available_courtier_slot > 0
						adventurer_can_use_pool_character = { CHARACTER = $CHARACTER$ SKILL = 11 }
						domicile.domicile_location = {
							squared_distance = { target = $CHARACTER$.location value <= 200000 }
						}
					}
				}
			}
		}
	}
}

