﻿can_be_combatant_based_on_gender_trigger = {
	custom_description = {
		text = "can_be_combatant_based_on_gender"
		object = this
		subject = $ARMY_OWNER$
		# Priests only fight if faith has war priests
		trigger_if = {
			limit = {
				is_clergy = yes
			}
			OR = {
				faith = { has_doctrine_parameter = clergy_can_fight }
				culture = { has_cultural_parameter = culture_clergy_can_fight }
				AND = {
					$ARMY_OWNER$.culture = {
						has_cultural_parameter = high_prowess_ignores_knight_restrictions
					}
					prowess >= 10
				}
			}
		}
		OR = {

			#GPT
			AND = {
				is_female = yes
				is_close_or_extended_family_of = $ARMY_OWNER$
				prowess >= 12
				is_adult = yes
				is_imprisoned = no
				NOR = { 
					has_trait = incapable
					has_trait = pregnant
				}
				OR = {
					has_trait = brave
					has_trait = lifestyle_blademaster
					has_trait = education_martial_1
					has_trait = education_martial_2
					has_trait = education_martial_3
					has_trait = education_martial_4
					has_trait = education_martial_5
				}
			}
			AND = {
				is_female = yes
				prowess >= 16
				is_lowborn = no
				is_adult = yes
				is_imprisoned = no
				NOR = { 
					has_trait = incapable
					has_trait = pregnant
				}
				OR = {
					has_trait = education_martial_3
					has_trait = education_martial_4
					has_trait = education_martial_5
				}
			}

			trigger_if = { # Has the Royal Court and thus can modify pillars
				limit = {
					has_dlc_feature = diverge_culture # Can modify pillars
				}
				trigger_if = {
					limit = {
						$ARMY_OWNER$.culture = { has_cultural_parameter = martial_custom_male_only_combatant }
					}
					is_male = yes
				}
				trigger_else_if = {
					limit = {
						$ARMY_OWNER$.culture = { has_cultural_parameter = martial_custom_female_only_combatant }
					}
					is_female = yes
				}
				trigger_else = {}
			}
			trigger_else = { # Does not have the Royal Court and thus combatant is governed by faith
				OR = {
					$ARMY_OWNER$.faith = { has_doctrine_parameter = combatant_can_be_either_gender_if_no_roco }
					AND = {
						$ARMY_OWNER$.faith = { has_doctrine_parameter = combatant_must_be_male_if_no_roco }
						is_male = yes
					}
					AND = {
						$ARMY_OWNER$.faith = { has_doctrine_parameter = combatant_must_be_female_if_no_roco }
						is_female = yes
					}
				}
			}
			AND = {
				# No need to gender diversify this: if they're a right-gender shieldperson of the culture, then they've already got permission to fight from the previous AND conditions.
				has_trait = shieldmaiden
				$ARMY_OWNER$ = {
					culture = { has_cultural_parameter = has_access_to_shieldmaidens }
				}
			}
			# Event-based special exceptions
			OR = {
				has_character_modifier = ignores_gender_army_rules
				has_character_modifier = mpo_destined_leader_modifier
			}
			AND = {
				prowess >= 10
				$ARMY_OWNER$.culture = { has_cultural_parameter = high_prowess_ignores_knight_restrictions }
				NOT = { $ARMY_OWNER$.culture = { has_cultural_parameter = minimum_prowess_for_knights } }
			}
			AND = {
				prowess >= 12
				$ARMY_OWNER$.culture = {
					has_cultural_parameter = high_prowess_ignores_knight_restrictions
					has_cultural_parameter = minimum_prowess_for_knights
				}
			}
		}
	}
}

# If you update one of the available triggers, update all of them. It's a lot better for performance to not have scripted triggers within scripted triggers when they're checked as often as these ones are.
can_be_commander_basic_trigger = {
	is_available_quick = {
		alive = yes
		adult = yes
		incapable = no
		hostage = no
	}

	#gpt
	OR = {
	 #	is_lowborn = no
		this = $ARMY_OWNER$
	 	is_councillor_of = $ARMY_OWNER$
		is_vassal_of = $ARMY_OWNER$
		is_close_or_extended_family_of = $ARMY_OWNER$
		AND = {
			exists = primary_title
			OR = {
				primary_title = { is_mercenary_company = yes }
				primary_title = { is_holy_order = yes }
			}
		}
	}
  #	health >= 1
	NOR = {
		has_relation_rival = $ARMY_OWNER$
		has_relation_nemesis = $ARMY_OWNER$
	}

	NAND = {
		is_clergy = yes
		NOR = {
			has_trait = order_member # Order Members will fight for their faith even if their faith says no
			faith = { has_doctrine_parameter = clergy_can_fight }
			culture = { has_cultural_parameter = culture_clergy_can_fight }
		}
	}

	OR = { # You can always lead your own armies
		this = $ARMY_OWNER$
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $ARMY_OWNER$ }
		is_close_family_of = $ARMY_OWNER$
	}

	trigger_if = {
		limit = { $ARMY_OWNER$ = { is_ai = yes } }
		is_ai = yes
		highest_held_title_tier < tier_hegemony
	}
}





can_be_chancellor_trigger = {
	can_be_councillor_basics_trigger = yes
	$COURT_OWNER$ ?= {
		NOT = { government_has_flag = government_is_nomadic }
	}
	NAND = {
		has_council_position = councillor_court_chaplain
		faith = { has_doctrine_parameter = clerical_appointment_fixed }
	}
	NOT = {
		OR = { 
			has_council_position = councillor_spouse
			AND = {
				this = liege.diarch
				liege = { has_diarchy_active_parameter = diarchy_is_vizierate }
			}
		}
	}
	OR = {
		is_ruler = yes
		AND = {
			is_female = yes
			$COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
		}
		AND = {
			is_male = yes
			$COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
		}
		is_close_family_of = $COURT_OWNER$
	}
}

can_be_steward_trigger = {
	can_be_councillor_basics_trigger = yes
	$COURT_OWNER$ ?= {
		NOT = { government_has_flag = government_is_nomadic }
	}
	NAND = {
		has_council_position = councillor_court_chaplain
		faith = { has_doctrine_parameter = clerical_appointment_fixed }
	}
	NOT = {
		OR = { 
			has_council_position = councillor_spouse
			AND = {
				this = liege.diarch
				liege = { has_diarchy_active_parameter = diarchy_is_vizierate }
			}
		}
	}
	OR = {
		is_ruler = yes
		AND = {
			is_female = yes
			$COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
		}
		AND = {
			is_male = yes
			$COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
		}
		is_close_family_of = $COURT_OWNER$
	}
}

can_be_marshal_trigger = {
	can_be_councillor_basics_trigger = yes
	$COURT_OWNER$ ?= {
		NOT = { government_has_flag = government_is_nomadic }
	}
	is_hostage = no
	NAND = {
		has_council_position = councillor_court_chaplain
		faith = { has_doctrine_parameter = clerical_appointment_fixed }
	}
	NOT = {
		OR = { 
			has_council_position = councillor_spouse
			AND = {
				this = liege.diarch
				liege = { has_diarchy_active_parameter = diarchy_is_vizierate }
			}
		}
	}
	OR = {
		is_ruler = yes
		AND = {
			is_female = yes
			$COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
		}
		AND = {
			is_male = yes
			$COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
		}
		AND = {
			has_trait = shieldmaiden
			$COURT_OWNER$.culture = { has_cultural_parameter = has_access_to_shieldmaidens }
		}
		is_close_family_of = $COURT_OWNER$
	}
	OR = {
		is_close_family_of = $COURT_OWNER$
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $COURT_OWNER$ }
	}
}





can_be_granted_titles_by = { #This is set up with trigger-ifs to allow for nice tooltip printouts in grant title interaction
	save_temporary_scope_as = title_grantee

	OR = {
		
		#gpt
		AND = {
			is_ruler = no
			is_courtier_of = $RULER$
			is_close_family_of = $RULER$
		}

		#Gender check
		trigger_if = {
			limit = {
				is_ruler = no #Rulers can always get additional titles
				is_theocratic_lessee = no #Theocratic lessees use other gender checks
				NOT = { $RULER$ = { government_allows = administrative } }
				NOT = {
					$RULER$.faith = {
						has_dominant_ruling_gender = scope:title_grantee
					}
				}
			}
			trigger_if = {
				limit = {
					$RULER$ = { government_allows = state_faith }
				}
				$RULER$.primary_title.state_faith = {
					has_dominant_ruling_gender = scope:title_grantee
				}
			}
			trigger_else = {
				$RULER$.faith = {
					has_dominant_ruling_gender = scope:title_grantee
				}
			}
		}
		trigger_if = {
			limit = {
				is_ruler = no #Rulers can always get additional titles
				is_theocratic_lessee = no #Theocratic lessees use other gender checks
				$RULER$ = {
					government_allows = administrative
					NOT = { has_realm_law = equal_law }
				}
			}
			trigger_if = {
				limit = {
					$RULER$ = { has_realm_law = male_only_law }
				}
				scope:title_grantee = { is_female = no }
			}
			trigger_if = {
				limit = {
					$RULER$ = { has_realm_law = female_only_law }
				}
				scope:title_grantee = { is_female = yes }
			}
			#trigger_else = { always = yes }
		}
		trigger_else_if = {
			limit = {
				is_theocratic_lessee = yes
			}
			liege = $RULER$
			#If this is ever visible anywhere, we need to write a custom desc "Theocratic lessees cannot only be given titles by their ruler"
		}
		trigger_else = {
			always = yes
		}
	}
}




