#	Example:
#
#	example_trigger = {
#		is_country_type = default
#		free_leader_slots > 0
#	}
#
#
#	In a script file:
#
#	trigger = {
#		example_trigger = yes
#	}
#

has_unelectable_trait_trigger = {
	OR = {
		has_trait = disinherited
		has_trait = gallivanter
		is_eunuch_trigger = yes
		has_trait = crusader_king #Avoid realm merging after Crusade.
		has_trait = devoted
		has_trait = order_member
		AND = {
			has_trait = bastard #If landed, he might still be electable on the grounds of being an Elector.
			is_ruler = no
		}
	}
}

has_unelectable_government_trigger = {
	OR = {	
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_mercenary
		government_has_flag = government_is_holy_order
	}
}

this_is_martial_society_trigger = {
	faith = {
		NOR = {
			has_doctrine = tenet_pacifism
			has_doctrine = tenet_dharmic_pacifism
		}
	}
	OR = {
		government_has_flag = government_is_clan
		government_has_flag = government_is_tribal
		government_has_flag = government_is_mercenary
		government_has_flag = government_is_holy_order
		faith = {
			OR = {
				has_doctrine_parameter = great_holy_wars_active
				has_doctrine_parameter = great_holy_wars_active_if_reformed
				has_doctrine_parameter = cheaper_holy_wars_active
			}
		}
		culture = {
			has_cultural_tradition = tradition_tgp_bushido
		}
	}
}

this_is_diplomatic_society_trigger = {
	OR = {
		government_has_flag = government_is_feudal
		government_has_flag = government_is_republic
		government_has_flag = government_is_theocracy
	}
	OR = {
		faith = { has_doctrine = tenet_communal_identity }
		faith = { has_doctrine = tenet_alexandrian_catechism }
		faith = { has_doctrine = tenet_legalism }
	}
}
this_is_spiritual_society_trigger = {
	faith = {
		NOR = {
			has_doctrine = tenet_false_conversion_sanction
			has_doctrine = tenet_hedonistic
			has_doctrine = tenet_carnal_exaltation
			has_doctrine = tenet_exaltation_of_pain
			has_doctrine = tenet_pursuit_of_power
		}
		OR = {
			has_doctrine = tenet_inner_journey
			has_doctrine = tenet_asceticism
			has_doctrine = tenet_literalism
			has_doctrine = tenet_esotericism
			has_doctrine = tenet_astrology
			has_doctrine = tenet_monasticism
		}
	}
}

ere_elective_politician_titles_trigger = { #Used in Byzantine Elective to determine AI behavior.
	OR = {
		has_council_position = councillor_chancellor
		has_council_position = councillor_marshal
		has_council_position = councillor_steward
		has_council_position = councillor_spymaster
		has_council_position = councillor_court_chaplain
	}
}

parent_for_elective_succession_trigger = { #This trigger is used to prevent grandchildren from being selectable when their parent is still available as a candidate.
	this != scope:holder #Not the current ruler.
	dynasty = scope:holder.dynasty
	is_child_of = scope:holder

	OR = {
		is_female = no
		scope:title = {
			NOT = { has_title_law = male_only_law }
		}
	}
	OR = {
		is_female = yes
		scope:title = {
			NOT = { has_title_law = female_only_law }
		}
	}

	OR = { #Candidate should not be a landless Courtier that is about to inherit a Theocracy or Republic
		AND = {
			is_available_quick = {
				ruler = yes
				advanced_ruler = yes
			}
			has_unelectable_government_trigger = no
		}
		NOT = {
			any_heir_title = {
				holder ?= {
					has_unelectable_government_trigger = yes
				}
			}
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = faith.religious_head
			}
		}
		faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

feudal_elective_potential_landed_candidate_trigger = {
	save_temporary_scope_as = potential_candidate
	has_same_government = scope:holder
	#Either the candidate is in the realm, or the title is independent, or the realm of the title does NOT have protected inheritance.
	OR = {
		any_liege_or_above = {
			this = scope:holder
		}
		scope:holder = { top_liege = this }
		scope:holder = {
			exists = liege
			NOT = {
				any_liege_or_above = {
					has_realm_law_flag = titles_cannot_leave_realm_on_succession
					NOR = {
						this = scope:potential_candidate
						target_is_vassal_or_below = scope:potential_candidate
					}
				}
			}
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = faith.religious_head
			}
		}
		this.faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

feudal_elective_potential_landless_claimant_candidate_trigger = {
	save_temporary_scope_as = potential_candidate
	#Either the candidate is in the realm, or is landless, or the title is independent, or the realm of the title does NOT have protected inheritance.
	OR = {
		any_liege_or_above = {
			this = scope:holder
		}
		is_ruler = no
		scope:holder = { top_liege = this }
		scope:holder = {
			exists = liege
			NOT = {
				any_liege_or_above = {
					has_realm_law_flag = titles_cannot_leave_realm_on_succession
					NOR = {
						this = scope:potential_candidate
						target_is_vassal_or_below = scope:potential_candidate
					}
				}
			}
		}
	}
	NOT = { #Landless characters should not be heirs to Theocracies/Republics.
		any_heir_title = {
			holder ?= {
				has_unelectable_government_trigger = yes
			}
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = scope:holder.faith.religious_head
			}
		}
		faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

feudal_elective_potential_landless_dynastic_candidate_trigger = {
	save_temporary_scope_as = potential_candidate
	#Either the candidate is in the realm, or is landless, or the title is independent, or the realm of the title does NOT have protected inheritance.
	OR = {
		any_liege_or_above = {
			this = scope:holder
		}
		is_ruler = no
		scope:holder = { top_liege = this }
		scope:holder = {
			exists = liege
			NOT = {
				any_liege_or_above = {
					has_realm_law_flag = titles_cannot_leave_realm_on_succession
					NOR = {
						this = scope:potential_candidate
						target_is_vassal_or_below = scope:potential_candidate
					}
				}
			}
		}
	}
	NOT = { #Landless characters should not be heirs to Theocracies/Republics.
		any_heir_title = {
			holder ?= {
				has_unelectable_government_trigger = yes
			}
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = scope:holder.faith.religious_head
			}
		}
		faith = scope:holder.faith
	}

	#Grandkid block
	trigger_if = {
		limit = {
			NOR = {
				#Check to see that you're not the product of incest
				AND = {
					exists = mother
					exists = father
					mother = { 
						relation_with_character_is_incestuous_in_my_or_lieges_faith_trigger = { CHARACTER = prev.father }
					}
				}
				#Let's make the incredibly bold assumption that if incest is completely allowed we'll just default to check the child instead of parents
				scope:holder.faith = { 
					has_doctrine_parameter = consanguinity_unrestricted_incest 
				}
			}
		}
		# We don't want grandkids to show as candidates if their parent is eligable and still alive
		NOR = {
			mother ?= {
				is_alive = yes
				parent_for_elective_succession_trigger = yes
			}
			father ?= {
				is_alive = yes
				parent_for_elective_succession_trigger = yes
			}
		}
	}
	#if incest is allowed, check if you're the child of the holder since you can be both their kid and grandkid at the same time... 
	trigger_else_if = {
		limit = {
			OR = {
				mother ?= {
					is_alive = yes
				}
				father ?= {
					is_alive = yes
				}
			}
		}
		parent_for_elective_succession_trigger = yes 
	}
	trigger_else = {
		#Your parents are dead and you're eligible for title inheritance. Congrats.
	}
	
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

byzantine_elective_deformed_candidate_trigger = {
	OR = {
		has_trait = disfigured
		has_trait = blind
		is_eunuch_trigger = yes
		has_trait = dwarf
		has_trait = clubfooted
		has_trait = inbred
	}
}
