﻿pam_consolamentum_grief_immunity_trigger = {
	has_personal_tenet_flag = consolamentum_grief_immunity
}

# Character scope. Keep this cheap for relation/war-enemy iterators; full target validation is below.
pam_adorcism_curse_target_is_living_adult_trigger = {
	is_available_quick = {
		adult = yes
		alive = yes
	}
}

pam_adorcism_curse_target_is_valid_trigger = {
	$TARGET$ ?= {
		pam_adorcism_curse_target_is_living_adult_trigger = yes
		OR = {
			has_relation_rival = $ACTOR$
			has_relation_nemesis = $ACTOR$
			$ACTOR$ = {
				any_war_enemy = {
					this = $TARGET$
				}
			}
		}
	}
}

pam_adorcism_has_available_curse_target_trigger = {
	OR = {
		any_relation = {
			type = rival
			pam_adorcism_curse_target_is_living_adult_trigger = yes
		}
		any_war_enemy = {
			pam_adorcism_curse_target_is_living_adult_trigger = yes
		}
	}
}

# The liturgical language triggers below only evaluate in a rite scope; call them through
# main_rite from a faith and through rite from a character.
# Pre-scripted rites skip the Head of Rite culture fallback. Slavic, Georgian, Antiochian,
# heresies, and custom divergences keep that fallback.
rite_has_pre_scripted_liturgical_language_trigger = {
	OR = {
		religion = religion:catholic_religion
		faith = faith:orthodox
		this = rite:byzantine_rite
		this = rite:celtic_rite
		this = rite:ethiopian_rite
		this = rite:armenian_rite
		this = rite:coptic_rite
	}
}

# These are founding-see rites in rite scope, not map-wide or ordinary swap traditions
pam_rite_is_regional_trigger = {
	OR = {
		this = rite:celtic_rite
		this = rite:teutonic_rite
		this = rite:carpathian_rite
		this = rite:iberian_rite
		this = rite:orthodox_hellenic_rite
		this = rite:coptic_rite
		this = rite:ethiopian_rite
		this = rite:armenian_rite
	}
}

pam_latin_as_liturgical_language_trigger = {
	religion = religion:catholic_religion
}

pam_greek_as_liturgical_language_trigger = {
	OR = {
		faith = faith:orthodox
		this = rite:byzantine_rite
		religion = religion:theomach_religion
		AND = {
			rite_has_pre_scripted_liturgical_language_trigger = no
			head_of_rite.culture ?= {
				OR = {
					this = culture:greek
					has_same_culture_language = culture:greek
				}
			}
		}
	}
}

pam_irish_as_liturgical_language_trigger = {
	OR = {
		this = rite:celtic_rite
		AND = {
			rite_has_pre_scripted_liturgical_language_trigger = no
			head_of_rite.culture ?= {
				OR = {
					this = culture:irish
					has_same_culture_language = culture:irish
				}
			}
		}
	}
}

pam_ethiopic_as_liturgical_language_trigger = {
	OR = {
		this = rite:ethiopian_rite
		AND = {
			rite_has_pre_scripted_liturgical_language_trigger = no
			head_of_rite.culture ?= {
				OR = {
					this = culture:ethiopian
					has_same_culture_language = culture:ethiopian
				}
			}
		}
	}
}

pam_armenian_as_liturgical_language_trigger = {
	OR = {
		this = rite:armenian_rite
		AND = {
			rite_has_pre_scripted_liturgical_language_trigger = no
			head_of_rite.culture ?= {
				OR = {
					this = culture:armenian
					has_same_culture_language = culture:NEOW_armenian
				}
			}
		}
	}
}

can_hold_ecumenical_council_trigger = {
	OR = {
		religion = religion:catholic_religion
		religion = religion:protestant_religion
		religion = religion:eastern_orthodox_religion
		religion = religion:oriental_orthodox_religion
	}
}

#has_current_phase_any_debate_trigger = {
#	OR = {	
#		has_current_phase = ecumenical_council_phase_first_debate
#		has_current_phase = ecumenical_council_phase_second_debate
#		has_current_phase = ecumenical_council_phase_third_debate
#	}
#}

pam_is_clerical_region_holder_trigger = {
	any_held_title = {
		tier >= tier_duchy
		has_clerical_region = yes
		clerical_region_size > 0
	}
}

pam_ecumenical_council_has_quorum_trigger = {
	trigger_if = {
		limit = { scope:host.faith ?= { has_doctrine_parameter = has_clerical_regions } }
		trigger_if = {
			limit = { scope:host = { pam_is_clerical_region_holder_trigger = yes } }
			any_attending_character = {
				count >= ecumenical_council_min_clerical_region_guests
				faith = scope:host.faith
				pam_is_clerical_region_holder_trigger = yes
			}
		}
		trigger_else = {
			any_attending_character = {
				count >= ecumenical_council_min_clerical_region_holders
				faith = scope:host.faith
				pam_is_clerical_region_holder_trigger = yes
			}
		}
	}
}

#the_christian_church_radical_change_allowed_trigger = {
#	situation:the_christian_church ?= {
#		OR = {
#			has_situation_top_phase_parameter = era_type_instability
#			has_situation_top_phase_parameter = era_type_crisis
#		}
#	}
#}

#pam_mass_rite_conversion_conversion_phase_trigger = {
#	situation:the_christian_church ?= {
#		has_situation_top_phase_parameter = the_christian_church_mass_rite_conversion_decision
#	}
#}

#pam_mass_rite_conversion_requirements_trigger = {
#	has_pam_dlc_trigger = yes
#	highest_held_title_tier >= tier_duchy
#}

# Great Schism: progress can still change. Any scope
#pam_great_schism_progress_can_change_trigger = {
#	NOT = { has_global_variable = pam_great_schism_decision_taken }
#	exists = situation:the_christian_church
#}

#pam_can_pick_great_schism_stance_trigger = {
#	situation:the_christian_church ?= {
#		var:pam_great_schism_status ?= flag:ongoing
#		pam_great_schism_progress_can_change_trigger = yes
#		exists = var:schism_mainline_rite
#		exists = var:schism_competitor_rite
#	}
#	is_ruler = yes
#	faith = faith:christian_faith
#}

# pam_save_schism_head_of_faith_effect saves the see first, then we run in character scope
# Uses situation main-power faith so this works before 0062 saves christianity
#pam_schism_can_seat_head_of_faith_trigger = {
#	this = scope:schism_peacemaker
#	exists = scope:new_head_of_faith
#	situation:the_christian_church.var:tcc_main_power_faith ?= { NOT = { has_doctrine = doctrine_temporal_head } }
#}

pam_is_ecumenical_patriarch_trigger = {
	faith = faith:orthodox_faith
	exists = faith.religious_head
	this = faith.religious_head
	OR = {
		faith.religious_head_title ?= title:d_et_constantinople
		faith.religious_head_title ?= title:k_orthodox
	}
}

# Great Schism: schism stance champions $RITE$ ($OTHER_RITE$ is the opposing side). Character scope
#pam_schism_stance_supports_rite_trigger = {
#	OR = {
#		AND = {
#			var:pam_schism_stance ?= flag:mainline
#			faith:christian_faith.main_rite ?= rite:$RITE$
#		}
#		AND = {
#			var:pam_schism_stance ?= flag:competitor
#			faith:christian_faith.main_rite ?= rite:$OTHER_RITE$
#		}
#	}
#}

#pam_schism_is_chalcedonian_trigger = {
#	OR = {
#		faith = scope:schism_western_faith
#		faith = scope:schism_eastern_faith
#		faith = faith:christian_faith
#	}
#}

# Great Schism: has a personal tenet dear to $RITE$. Character scope
#pam_has_personal_tenet_of_rite_trigger = {
#	any_personal_tenet = {
#		rite:$RITE$ ?= { rite_has_tenet = prev }
#	}
#}

# The delegate's rite is $RITE$, not the judging host's, on a character
#pam_ecumenical_council_rite_delegate_trigger = {
#	this != root
#	rite = rite:$RITE$
#}

# Heading a rite does not make the holder follow it
#pam_is_head_of_schism_pair_rite_trigger = {
#	OR = {
#		AND = {
#			rite = rite:roman_rite
#			rite:roman_rite.head_of_rite ?= this
#		}
#		AND = {
#			rite = rite:byzantine_rite
#			rite:byzantine_rite.head_of_rite ?= this
#		}
#	}
#}

# Great Schism: $FIRST$ and $SECOND$ follow opposite schism-pair rites. False if unset. Any scope
#pam_schism_characters_in_opposite_pair_rites_trigger = {
#	situation:the_christian_church ?= {
#		exists = var:schism_mainline_rite
#		exists = var:schism_competitor_rite
#		OR = {
#			AND = {
#				$FIRST$.rite ?= var:schism_mainline_rite
#				$SECOND$.rite ?= var:schism_competitor_rite
#			}
#			AND = {
#				$FIRST$.rite ?= var:schism_competitor_rite
#				$SECOND$.rite ?= var:schism_mainline_rite
#			}
#		}
#	}
#}

# Great Schism: $HEAD$ heads one schism-pair rite; $OTHER$ follows the opposing. False if unset. Any scope
#pam_schism_head_of_rite_and_opposing_character_trigger = {
#	situation:the_christian_church ?= {
#		exists = var:schism_mainline_rite
#		exists = var:schism_competitor_rite
#		OR = {
#			AND = {
#				var:schism_mainline_rite.head_of_rite ?= $HEAD$
#				$OTHER$.rite ?= var:schism_competitor_rite
#			}
#			AND = {
#				var:schism_competitor_rite.head_of_rite ?= $HEAD$
#				$OTHER$.rite ?= var:schism_mainline_rite
#			}
#		}
#	}
#}

# $RITE$ must be absolute (scope:/root./rite:), and this works in any scope
# A bare rite link silently resolves to nothing inside the situation
#pam_schism_rite_in_pair_trigger = {
#	situation:the_christian_church ?= {
#		exists = var:schism_mainline_rite
#		exists = var:schism_competitor_rite
#		OR = {
#			$RITE$ ?= var:schism_mainline_rite
#			$RITE$ ?= var:schism_competitor_rite
#		}
#	}
#}

# Great Schism: $COUNTY$'s rite is one of the schism-pair rites. False if unset. Any scope
#pam_schism_county_rite_in_pair_trigger = {
#	situation:the_christian_church ?= {
#		exists = var:schism_mainline_rite
#		exists = var:schism_competitor_rite
#		OR = {
#			$COUNTY$.rite ?= var:schism_mainline_rite
#			$COUNTY$.rite ?= var:schism_competitor_rite
#		}
#	}
#}

# Root is the converting ruler, this is the one who stayed on $RITE$
#pam_schism_rite_dissenter_valid_trigger = {
#	this != root
#	is_alive = yes
#	is_adult = yes
#	is_imprisoned = no
#	rite ?= $RITE$
#}

is_clerical_appointment_valid_trigger = {
	$TITLE$ = { save_temporary_scope_as = title_temp }
	$CANDIDATE$ = { save_temporary_scope_as = candidate_temp }
	$TOP_LIEGE$ = { save_temporary_scope_as = top_liege_temp }
	trigger_if = {
		limit = { exists = scope:title_temp }
		custom_tooltip = { # Has relevant law
			text = has_appointment_succession_tt
			$TITLE$ = {
				OR = {
					has_title_law = clerical_appointment_law
					holder ?= { has_realm_law = clerical_appointment_law }
				}
			}
		}
		custom_tooltip = { # Titles below the holder's primary tier are inherited alongside it, so they cannot be contested on their own
			text = follows_other_title_appointment_tt
			scope:title_temp.holder ?= { primary_title.tier = scope:title_temp.tier }
		}
	}
	is_clerical_appointment_valid_base_trigger = {
		CANDIDATE = $CANDIDATE$
		TOP_LIEGE = $TOP_LIEGE$
	}
}

# Secular crowns only reach sees in their realm, while ecclesiastical and Head of Rite are unrestricted
pam_clerical_candidacy_reach_limited_trigger = {
	scope:actor = {
		NOR = {
			government_has_flag = government_is_ecclesiastical
			this = faith.main_rite.head_of_rite
			AND = {
				is_landless_adventurer = yes
				scope:secondary_recipient ?= this
			}
		}
	}
}

influence_clerical_candidacy_reach_trigger = {
	trigger_if = {
		limit = {
			pam_clerical_candidacy_reach_limited_trigger = yes
		}
		custom_tooltip = {
			text = influence_clerical_candidacy_outside_realm_tt
			scope:target = {
				holder ?= { target_is_same_character_or_above = scope:actor }
			}
		}
	}
}

influence_clerical_candidacy_any_valid_title_trigger = {
	scope:actor.faith ?= {
		any_clerical_region_title = {
			OR = {
				has_title_law = clerical_appointment_law
				holder ?= { has_realm_law = clerical_appointment_law }
			}
			NOT = { holder ?= scope:secondary_recipient }
			trigger_if = {
				limit = {
					pam_clerical_candidacy_reach_limited_trigger = yes
				}
				holder ?= { target_is_same_character_or_above = scope:actor }
			}
		}
	}
}

pam_head_of_faith_candidacy_valid_title_trigger = {
	has_clerical_region = no
	trigger_if = {
		limit = { exists = holder }
		has_order_of_succession = appointment
	}
}

influence_head_of_faith_candidacy_target_trigger = {
	custom_tooltip = {
		text = influence_head_of_faith_candidacy_not_created_tt
		exists = scope:target.holder
	}
	# has_order_of_succession resolves the holder's realm laws, so both checks stay behind the holder check
	trigger_if = {
		limit = { exists = scope:target.holder }
		custom_tooltip = {
			text = has_appointment_succession_tt
			scope:target = { has_order_of_succession = appointment }
		}
		custom_tooltip = {
			text = influence_head_of_faith_candidacy_same_faith_tt
			scope:target.holder ?= { faith = scope:actor.faith }
		}
	}
}

is_clerical_appointment_valid_base_trigger = {
	$CANDIDATE$ = { save_temporary_scope_as = candidate_temp }
	$TOP_LIEGE$ = { save_temporary_scope_as = top_liege_temp }
	scope:candidate_temp = {
		is_adult = yes
		faith = scope:top_liege_temp.faith # Must be of the same faith
		custom_tooltip = {
			text = can_be_appointed_interaction_tt
			OR = {
				can_be_granted_theocratic_titles_by = { RULER = scope:top_liege_temp } # Gender checks
				AND = {
					is_diarch_of_target = scope:top_liege_temp
					scope:top_liege_temp = { has_diarchy_active_parameter = diarchy_is_co_rulership }
				}
			}
		}
		custom_tooltip = {
			text = can_be_appointed_interaction_clergy_tt
			OR = { # Clergy or Lay Clergy
				is_clergy = yes
				rite = { rite_has_parameter = lay_clergy } # HoF cannot be temporal if you have Theocratic clergy, so no need to check for that
				this = scope:top_liege_temp.player_heir # Designated Theocratic Heir
				AND = {
					is_landless_adventurer = yes
					this = scope:top_liege_temp
				}
			}
		}
		custom_tooltip = { # Only theocracies
			text = only_theocratic_tt
			OR = {
				government_has_flag = government_is_theocracy
				is_landed = no
			}
		}
		custom_tooltip = { # Barred from holding titles
			text = can_be_appointed_interaction_can_inherit_tt
			NOR = {
				has_trait = order_member
				has_trait = gallivanter
				has_trait = disinherited
			}
		}
	}
}

# Expects interaction scopes actor/target/secondary_recipient
pam_clerical_self_candidacy_restriction_trigger = {
	trigger_if = {
		limit = {
			exists = scope:secondary_recipient
			scope:actor = {
				is_landless_adventurer = yes
				this = scope:secondary_recipient
			}
		}
		scope:actor = {
			employs_court_position = camp_priest_camp_officer
		}
	}
	trigger_if = {
		limit = {
			exists = scope:target
			scope:actor ?= scope:secondary_recipient
			scope:actor = {
				any_held_title = { has_clerical_region = yes }
			}
		}
		custom_tooltip = {
			text = pam_clerical_self_candidacy_restriction_tt
			scope:target = {
				OR = {
					this = title:d_et_constantinople
					is_head_of_faith = yes
					holder ?= { faith.religious_head ?= this }
				}
			}
		}
	}
}


pam_investiture_supports_spiritual_revocable_trigger = {
	# Both flag values are read so neither is flagged "set but never used".
	trigger_if = {
		limit = {
			has_variable = investiture_stance
			var:investiture_stance = flag:spiritual_revocable
		}
		always = yes
	}
	trigger_else_if = {
		limit = {
			has_variable = investiture_stance
			var:investiture_stance = flag:temporal_revocable
		}
		always = no
	}
	trigger_else = {
		# is_clergy too: landless chaplains have no government, so the flag would count them as secular
		OR = {
			government_is_religious_trigger = yes
			is_clergy = yes
		}
	}
}

pam_investiture_council_voter_supports_spiritual_trigger = {
	trigger_if = {
		limit = {
			exists = puppeteer
			puppeteer = { has_theological_agent_puppet_trigger = yes }
		}
		puppeteer = { pam_investiture_supports_spiritual_revocable_trigger = yes }
	}
	trigger_else = {
		pam_investiture_supports_spiritual_revocable_trigger = yes
	}
}

pam_investiture_council_voter_has_locked_stance_trigger = {
	trigger_if = {
		limit = {
			exists = puppeteer
			puppeteer = { has_theological_agent_puppet_trigger = yes }
		}
		puppeteer = { has_variable = investiture_stance }
	}
	trigger_else = {
		has_variable = investiture_stance
	}
}

#pam_can_use_investiture_conflict_cb_trigger = {
#	has_pam_dlc_trigger = yes
#	faith = { religion = religion:christianity_religion }
#	situation:the_christian_church ?= {
#		has_situation_top_phase_parameter = the_christian_church_investiture_controversy
#	}
#	# We block new conflicts while the council is pending, as a war would pull the host away and cancel it
#	custom_tooltip = {
#		text = pam_investiture_cb_council_pending_tt
#		NOT = { faith = { has_variable = investiture_council_pending } }
#	}
#}

# scope:attacker is the aggressor, and the defender is root
pam_valid_investiture_conflict_target_trigger = {
	this != scope:attacker
	faith = scope:attacker.faith
	trigger_if = {
		limit = { scope:attacker = { pam_investiture_supports_spiritual_revocable_trigger = yes } }
		NOT = { pam_investiture_supports_spiritual_revocable_trigger = yes }
	}
	trigger_else = {
		pam_investiture_supports_spiritual_revocable_trigger = yes
	}
}

#pam_main_power_faith_conditional_appointment_trigger = {
#	situation:the_christian_church.var:tcc_main_power_faith ?= {
#		has_doctrine = doctrine_clerical_succession_conditional
#	}
#}

#pam_investiture_crisis_has_contested_faith_trigger = {
#	situation:the_christian_church.var:tcc_main_power_faith ?= {
#		has_doctrine = doctrine_clerical_succession_conditional
#		exists = religious_head
#	}
#}

pam_available_investiture_hof_ai_trigger = {
	is_ai = yes
	is_alive = yes
	is_adult = yes
	is_imprisoned = no
	is_incapable = no
	is_commanding_army = no
	has_contagious_deadly_disease_trigger = no
}

pam_valid_investiture_council_host_trigger = {
	is_ai = yes
	is_available_adult = yes
	is_landed = yes
	# PAM archbishops often fail the is_clergy org match
	government_has_flag = government_is_ecclesiastical
	can_hold_ecumenical_council_trigger = yes
	NOT = { faith.religious_head = this }
	# any_held_title avoids the landless context-switch primary_title would cause
	any_held_title = {
		tier >= tier_duchy
		has_clerical_region = yes
	}
}

###Sell Claim###
# True if TARGET, a county title, lies within a clerical region the scoped character holds.
pam_sell_claim_target_in_own_clerical_region_trigger = {
	exists = $TARGET$.clerical_region_title
	any_held_title = {
		has_clerical_region = yes
		this = $TARGET$.clerical_region_title
	}
}

# Shared with has_valid_target_showing_failures_only: can_be_picked_title is not rechecked on send
pam_can_sell_claim_on_target_trigger = {
	scope:target = {
		is_landless_type_title = no
		is_head_of_faith = no
	}
	custom_tooltip = {
		text = sell_claim_target_must_be_in_clerical_region_tt
		scope:puppet_or_actor ?= {
			OR = {
				pam_sell_claim_target_in_own_clerical_region_trigger = { TARGET = scope:target }
				faith.religious_head ?= this
			}
		}
	}
	custom_tooltip = {
		text = sell_claim_recipient_must_not_hold_title_tt
		scope:target ?= {
			NOT = {
				holder ?= scope:recipient
			}
		}
	}
	custom_tooltip = {
		text = sell_claim_recipient_must_not_have_claim_tt
		scope:recipient = {
			NOT = {
				has_claim_on = scope:target
			}
		}
	}
	# Realm scan, so it sits behind the cheap ownership and claim gates
	custom_tooltip = {
		text = sell_claim_recipient_must_border_target_tt
		scope:target ?= {
			is_neighbor_to_realm = scope:recipient
		}
	}
}

###Gift Claim###
pam_gift_claim_recipient_in_own_clerical_region_trigger = {
	exists = $TARGET$.capital_county.clerical_region_title
	any_held_title = {
		has_clerical_region = yes
		this = $TARGET$.capital_county.clerical_region_title
	}
}

pam_gift_claim_valid_title_trigger = {
	$TITLE$ = {
		tier = tier_county
		faith ?= scope:puppet_or_actor.faith
		NOT = { holder ?= scope:recipient }
		holder ?= {
			NOR = {
				government_has_flag = government_is_ecclesiastical
				government_has_flag = government_is_theocracy
			}
		}
	}
	scope:puppet_or_actor ?= {
		OR = {
			pam_sell_claim_target_in_own_clerical_region_trigger = { TARGET = $TITLE$ }
			faith.religious_head ?= this
		}
	}
	scope:recipient = {
		NOT = { has_claim_on = $TITLE$ }
	}
}

pam_can_gift_claim_on_target_trigger = {
	custom_tooltip = {
		text = gift_claim_target_must_be_county_of_faith_tt
		scope:target ?= {
			tier = tier_county
			faith ?= scope:puppet_or_actor.faith
		}
	}
	custom_tooltip = {
		text = gift_claim_target_must_be_in_clerical_region_tt
		trigger_if = {
			limit = { scope:target ?= { tier = tier_county } }
			scope:puppet_or_actor ?= {
				OR = {
					pam_sell_claim_target_in_own_clerical_region_trigger = { TARGET = scope:target }
					faith.religious_head ?= this
				}
			}
		}
	}
	custom_tooltip = {
		text = gift_claim_target_must_not_be_church_held_tt
		scope:target.holder ?= {
			NOR = {
				government_has_flag = government_is_ecclesiastical
				government_has_flag = government_is_theocracy
			}
		}
	}
	custom_tooltip = {
		text = gift_claim_recipient_must_not_hold_title_tt
		scope:target ?= {
			NOT = {
				holder ?= scope:recipient
			}
		}
	}
	custom_tooltip = {
		text = gift_claim_recipient_must_not_have_claim_tt
		scope:recipient = {
			NOT = {
				has_claim_on = scope:target
			}
		}
	}
}

can_influence_clerical_appointment_trigger = {
	faith = { has_doctrine_parameter = has_clerical_regions }
	OR = {
		has_realm_law = clerical_appointment_law
		any_held_title = {
			has_title_law = clerical_appointment_law
		}
		cp:councillor_court_chaplain ?= {
			OR = {
				has_realm_law = clerical_appointment_law
				any_held_title = {
					has_title_law = clerical_appointment_law
				}
			}
		}
		# Capital see filled by appointment still matters even if the realm priest is landless
		capital_county.clerical_region_title ?= {
			OR = {
				has_title_law = clerical_appointment_law
				holder ?= { has_realm_law = clerical_appointment_law }
			}
		}
	}
}

pam_cathedral_project_requirements_trigger = {
	has_pam_dlc_trigger = yes
	highest_held_title_tier >= tier_duchy
	has_spiritual_fulfillment_type = christian_fulfillment
}

# Landless clerical titles: title_capital_county is the holder's current location, not the fixed see
is_valid_cathedral_clerical_region_capital_trigger = {
	trigger_if = {
		limit = { exists = county.clerical_region_title }
		custom_tooltip = {
			text = great_project_cathedral_clerical_region_capital_tt
			county.clerical_region_title.title_domicile.domicile_location ?= this
		}
	}
	trigger_else_if = {
		limit = { root = { pam_is_clerical_region_holder_trigger = yes } }
		custom_tooltip = {
			text = great_project_cathedral_clerical_region_holder_capital_tt
			always = no
		}
	}
	trigger_else = {
		always = yes
	}
}

is_valid_cathedral_spot_in_province_trigger = {
	custom_tooltip = {
		text = no_under_construction_or_same_level_great_project_cathedral_building_tt
		NOR = {
			has_building_with_flag = great_project_cathedral_building
			any_great_project_in_province = {
				OR = {
					great_project_type = pam_cathedral_01
					great_project_type = pam_cathedral_02
					great_project_type = pam_cathedral_03
				}
			}
		}
	}
	has_great_building = no
}

has_cathedral_great_project_building_trigger = {
	has_building_with_flag = great_project_cathedral_building
}

is_valid_cathedral_upgrade_spot_in_province_trigger = {
	custom_tooltip = {
		text = great_project_cathedral_01_required_for_upgrade_tt
		has_building = great_project_cathedral_01
		NOT = { has_building = great_project_cathedral_02 }
		NOT = { has_building = great_project_cathedral_03 }
	}
	custom_tooltip = {
		text = no_cathedral_upgrade_in_progress_tt
		NOR = {
			has_building = great_project_cathedral_under_construction
			any_great_project_in_province = {
				OR = {
					great_project_type = pam_cathedral_01
					great_project_type = pam_cathedral_02
					great_project_type = pam_cathedral_03
				}
			}
		}
	}
}

is_valid_cathedral_tier_3_upgrade_spot_in_province_trigger = {
	custom_tooltip = {
		text = great_project_cathedral_02_required_for_tier_3_upgrade_tt
		has_building = great_project_cathedral_02
		NOT = { has_building = great_project_cathedral_03 }
	}
	custom_tooltip = {
		text = no_cathedral_upgrade_in_progress_tt
		NOR = {
			has_building = great_project_cathedral_under_construction
			any_great_project_in_province = {
				OR = {
					great_project_type = pam_cathedral_01
					great_project_type = pam_cathedral_02
					great_project_type = pam_cathedral_03
				}
			}
		}
	}
}

has_accessible_cathedral_tier_2_upgrade_site_trigger = {
	trigger_if = {
		limit = {
			NOT = {
				this = faith.religious_head
			}
			is_clergy = no
		}
		any_sub_realm_county = {
			any_county_province = {
				is_valid_cathedral_upgrade_spot_in_province_trigger = yes
			}
		}
	}
	trigger_else = {
		faith = {
			any_faith_ruler = {
				highest_held_title_tier >= tier_county
				any_realm_county = {
					any_county_province = {
						is_valid_cathedral_upgrade_spot_in_province_trigger = yes
					}
				}
			}
		}
	}
}

has_accessible_cathedral_tier_3_upgrade_site_trigger = {
	trigger_if = {
		limit = {
			NOT = {
				this = faith.religious_head
			}
			is_clergy = no
		}
		any_sub_realm_county = {
			any_county_province = {
				is_valid_cathedral_tier_3_upgrade_spot_in_province_trigger = yes
			}
		}
	}
	trigger_else = {
		faith = {
			any_faith_ruler = {
				highest_held_title_tier >= tier_county
				any_realm_county = {
					any_county_province = {
						is_valid_cathedral_tier_3_upgrade_spot_in_province_trigger = yes
					}
				}
			}
		}
	}
}

is_valid_cathedral_master_mason_contributor_trigger = {
	OR = {
		AND = {
			is_ai = yes
			this = scope:founder
		}
		has_trait = architect
		employs_court_position = royal_architect_court_position
		stewardship >= $STEWARDSHIP_THRESHOLD$
		any_courtier = {
			OR = {
				has_trait = architect
				stewardship >= $STEWARDSHIP_THRESHOLD$
			}
		}
		cp:councillor_steward ?= {
			stewardship >= $STEWARDSHIP_THRESHOLD$
		}
	}
}

is_valid_cathedral_facade_contributor_trigger = {
	OR = {
		learning >= extremely_high_skill_rating
		AND = {
			highest_held_title_tier >= tier_kingdom
			custom_tooltip = {
				text = has_good_royal_architect_tt
				any_court_position_holder = {
					type = royal_architect_court_position
					aptitude = { court_position = royal_architect_court_position value >= 3 }
				}
			}
		}
	}
}

take_the_vows_valid_monastic_order_trigger = {
	leader ?= {
		government_has_flag = government_is_monastic_holy_order
    	NOT = {
   	    	is_at_war_with = $CHARACTER$
    	}
    }
}

pam_is_monastic_order_member_trigger = {
	OR = {
		government_has_flag = government_is_monastic_holy_order
		AND = {
			has_trait = order_member
			OR = {
				liege ?= { government_has_flag = government_is_monastic_holy_order }
				host ?= { government_has_flag = government_is_monastic_holy_order }
			}
		}
	}
}

pam_is_military_order_member_trigger = {
	OR = {
		government_has_flag = government_is_military_holy_order
		AND = {
			has_trait = order_member
			OR = {
				liege ?= { government_has_flag = government_is_military_holy_order }
				host ?= { government_has_flag = government_is_military_holy_order }
			}
		}
	}
}

hyperdulia_take_the_vows_trigger = {
	scope:recipient = {
		is_female = yes
		is_child_of = scope:puppet_or_actor
	}
	scope:puppet_or_actor = {
		has_personal_tenet_flag = tenet_hyperdulia_daughter_vows_bonus
	}
}


faith_has_monasticism_trigger = {
	OR = {
		has_doctrine = doctrine_monasticism_encouraged
		has_doctrine = doctrine_monasticism_accepted
		main_rite = {
			OR = {
				rite_has_doctrine = doctrine_monasticism_encouraged
				rite_has_doctrine = doctrine_monasticism_accepted
			}
		}
	}
}

pam_rite_values_martial_clergy_trigger = {
	rite = {
		OR = {
			rite_has_tenet = tenet_warmonger
			rite_has_tenet = tenet_unrelenting_faith
		}
	}
}

# We skip is_landed because ecclesiastical government is landless_playable
pam_court_wants_monk_courtiers_trigger = {
	has_pam_dlc_trigger = yes
	is_ruler = yes
	highest_held_title_tier >= tier_county # A village priest is not a bishop with a familia
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	pam_government_is_religious_playable_trigger = yes
	faith = { faith_has_monasticism_trigger = yes }
	primary_title = {
		is_mercenary_company = no
		is_holy_order = no
	}
}

# Province holds a temple leased to a monastic order
pam_province_has_monastic_lease_trigger = {
	has_holding_type = church_holding
	barony = {
		is_leased_out = yes
		lessee = { government_has_flag = government_is_monastic_holy_order }
	}
}

# The courtier is the scope so kin stay eligible for appointment standing
pam_clerical_court_ordain_candidate_trigger = {
	is_adult = yes
	is_available = yes
	is_clergy = no
	is_ruler = no
	is_landed = no
	exists = employer
	faith = employer.faith
	NOR = {
		is_spouse_of = employer
		is_consort_of = employer
		betrothed ?= employer
	}
}

# Court cleric with room left to grow under the develop-clerical-court caps. Evaluated on the courtier.
pam_clerical_court_pupil_trigger = {
	is_adult = yes
	is_available = yes
	is_clergy = yes
	is_ruler = no
	exists = employer
	faith = employer.faith
	OR = {
		learning < employer.pam_clerical_court_learning_cap_value
		NOT = { has_trait = lifestyle_scholar }
		NOT = { has_trait = lifestyle_mystic }
	}
}

pam_clerical_court_novice_trigger = {
	is_adult = yes
	is_physically_able = yes
	age < 60
	NOT = {
		culture = { has_cultural_tradition = tradition_diasporic }
	}
	OR = {
		faith = $RULER$.faith
		NOT = { has_trait = zealous }
	}
}

faith_has_sacraments_central_trigger = {
	OR = {
		has_doctrine = doctrine_sacraments_central
		main_rite = { rite_has_doctrine = doctrine_sacraments_central }
	}
}

faith_has_sacraments_trigger = {
	OR = {
		has_doctrine = doctrine_sacraments_central
		main_rite = { rite_has_doctrine = doctrine_sacraments_central }
	}
}

#SCRIPTED TRIGGER FOR GENERAL HOLY ORDER INTERACTIONS, TO FIND ORDERS WITH LEASED HOLDINGS NEARBY
#Find leased title within a believe range that you would reasonable have access to or care about
holy_order_range_trigger = {
	custom_tooltip = {
		text = monastic_holdings_too_distant_tt
    	any_leased_title = {
    	    OR = {
    	        county = {
    	            save_temporary_scope_as = monastic_county
    	            OR = {
    	            	$CHARACTER$ = {
    	            	    any_sub_realm_county = {
    	            	        this = scope:monastic_county
    	            	    }
    	            	}
    	            	clerical_region_title.holder ?= $CHARACTER$
    	            }
    	        }
    	        county = {
    	            any_title_to_title_neighboring_and_across_water_county = {
    	                OR = {
    	                	clerical_region_title.holder ?= $CHARACTER$
    	                	holder = {
    	                		OR = {
    	                			this = $CHARACTER$
    	                			any_liege_or_above = {
    	                				this = $CHARACTER$
    	                			}
    	                		}
    	                	}
    	                }
    	            }
    	        }
    	    }
    	}
	}
}

government_is_religious_trigger = {
	OR = {
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_holy_order
	}
}

#pam_better_siphon_treasury_trigger = {
#	top_participant_group:the_christian_church ?= {
#		has_participant_group_parameter = the_christian_church_better_siphon_treasury
#	}
#}

# No Annoinments during the Investiture Crisis
#pam_the_christian_church_no_coronation_trigger = {
#	top_participant_group:the_christian_church ?= {
#		has_participant_group_parameter = the_christian_church_no_coronation
#	}
#}

# Secular rulers who follow an Antipope can be anointed by them while the mainline hierarchy refuses
#pam_the_christian_church_antipope_coronations_trigger = {
#	top_participant_group:the_christian_church ?= {
#		has_participant_group_parameter = the_christian_church_antipope_coronations
#	}
#}

# Anointment is sought from this authority, and $RULER$ is the ruler seeking it
# We still let an antipope anoint their followers, otherwise Investiture Crisis blocks the mainline
#pam_the_christian_church_no_anointment_trigger = {
#	save_temporary_scope_as = pam_anointment_authority_check
#	trigger_if = {
#		limit = {
#			pam_is_antipope_trigger = yes
#			$RULER$ = {
#				pam_the_christian_church_antipope_coronations_trigger = yes
#				religious_head_or_challenger ?= scope:pam_anointment_authority_check
#			}
#		}
#		always = no
#	}
#	trigger_else = {
#		pam_the_christian_church_no_coronation_trigger = yes
#	}
#}

# Any living ruler of kingdom tier or above whose capital lies in Europe and follows a non-Christian religion
pam_non_christian_kingdom_holder_in_europe_trigger = {
	any_ruler = {
		is_alive = yes
		OR = {
					religion != religion:catholic_religion
					religion != religion:protestant_religion
					religion != religion:eastern_orthodox_religion
					religion != religion:oriental_orthodox_religion
		}
		highest_held_title_tier >= tier_kingdom
		capital_province = {
			geographical_region = world_europe
		}
		NOT = { has_variable = pam_non_christian_kingdom_holder_in_europe_trigger_var } # To avoid the same ruler counting every year
	}
}

can_have_puppets_trigger = {
	is_available_quick = {
		adult = yes
		advanced_ruler = yes
	}
	NOR = {
		government_has_flag = government_is_mercenary
		government_has_flag = government_is_herder
	}
	highest_held_title_tier >= tier_county
}

can_have_external_ruler_puppet_trigger = {
	can_have_puppets_trigger = yes
	has_pam_dlc_trigger = yes
}

can_have_theological_agent_puppet_trigger = {
	can_have_puppets_trigger = yes
	rite = {
		OR = {
			rite_has_doctrine = doctrine_theocracy_temporal
			rite_has_doctrine = doctrine_monasticism_accepted
			rite_has_doctrine = doctrine_monasticism_encouraged
		}
	}
	has_pam_dlc_trigger = yes
}

can_have_realm_priest_puppet_trigger = {
	can_have_puppets_trigger = yes
	government_allows = council
	rite = { rite_has_doctrine = doctrine_theocracy_temporal }
	is_clergy = no
	tgp_has_access_to_ministry_trigger = no
	NOT = { government_has_flag = government_is_nomadic }
}

# Ecclesiastical government is landless_playable, so do not filter these on is_landed
pam_landless_playable_cleric_trigger = {
	has_pam_dlc_trigger = yes
	government_has_flag = government_is_ecclesiastical
}

# Archbishop (clerical region title holder) or above them in the hierarchy: Head of Rite or Head of Faith
pam_is_archbishop_or_higher_trigger = {
	OR = {
		any_held_title = { has_clerical_region = yes }
		religious_head_or_challenger ?= this
	}
}

# A cardinal, Head of Rite, or archbishop of $CHARACTER$'s faith
pam_is_senior_cleric_of_character_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	faith = $CHARACTER$.faith
	OR = {
		exists = clerical_elector_title
		$CHARACTER$.rite.head_of_rite ?= this
		any_held_title = { has_clerical_region = yes }
	}
}

# A character is a theological puppet if they have a puppeteer who keeps them as a (external) theological puppet.
is_theological_agent_puppet_trigger = {
	trigger_if = {
		limit = {
			exists = scope:puppeteer
		}
		scope:puppeteer = {
			has_theological_agent_puppet_trigger = yes
		}
	}
	trigger_else = {
		custom_tooltip = {
			text = no_puppeteer_tt
			always = no
		}
	}
}

# A character has a theological puppet if they keep either an internal or external theological puppet.
has_theological_agent_puppet_trigger = {
	custom_tooltip = {
		text = no_clerical_puppet_tt
		OR = {
			exists = scope:puppet
			exists = puppet:theological_agent_puppet
			exists = puppet:external_theological_agent_puppet
		}
	}
}

puppet_action_puppet_is_secular_ruler_trigger = {
	puppet:external_ruler_puppet ?= scope:puppet
}

# The acting character is the scope, and on the puppet action path that is the puppet
# A puppet standing in for a missing Head of Faith defers to its puppeteer
can_manage_faith_holy_sites_trigger = {
	trigger_if = {
		limit = { exists = faith.religious_head }
		custom_tooltip = {
			text = manage_holy_sites_hof_or_puppet_tt
			OR = {
				faith.religious_head ?= this
				puppet:theological_agent_puppet ?= faith.religious_head
				puppet:external_theological_agent_puppet ?= faith.religious_head
			}
		}
	}
	trigger_else = {
		trigger_if = {
			limit = { exists = puppeteer }
			puppeteer = { can_manage_faith_holy_sites_without_hof_trigger = yes }
		}
		trigger_else = {
			can_manage_faith_holy_sites_without_hof_trigger = yes
		}
	}
}

# Substitute authority for a faith with no head of faith: a realm priest puppet plus piety standing.
can_manage_faith_holy_sites_without_hof_trigger = {
	OR = {
		custom_tooltip = {
			text = manage_holy_sites_realm_priest_puppet_tt
			# puppet: errors on non-playable characters, so we only check playable ones and tooltips never run it
			trigger_if = {
				limit = { is_playable_character = yes }
				exists = puppet:theological_agent_puppet
			}
			trigger_else = {
				always = no
			}
		}
		government_has_flag = government_is_theocracy
	}
	custom_tooltip = {
		text = manage_holy_sites_piety_level_tt
		piety_level >= 4
	}
}

can_create_faith_holy_site_trigger = {
	OR = {
		can_manage_faith_holy_sites_trigger = yes
		AND = {
			is_independent_ruler = yes
			highest_held_title_tier >= tier_kingdom
			piety_level >= 4
			save_temporary_scope_as = holy_site_creator
			$BARONY$.holder ?= {
				target_is_same_character_or_above = scope:holy_site_creator
			}
			$BARONY$.county.holder.faith ?= faith
			exists = faith.religious_head
			faith.religious_head = {
				opinion = {
					target = scope:holy_site_creator
					value > 0
				}
			}
		}
	}
}

holy_site_free_action_show_usage_trigger = {
	trigger_if = {
		limit = { faith_has_free_holy_site_action_trigger = yes  }
		save_temporary_scope_as = faith
		custom_tooltip = {
			text = holy_site_decision_free_action_cost
			always =  yes
		}
	}
}

# The attendee is the teacher, and root is the student receiving the activity event
ecumenical_shareable_unknown_tenet_for_root_trigger = {
	save_temporary_scope_as = ecumenical_tenet_teacher
	any_character_tenet = {
		knowledge_filter = known
		save_temporary_scope_as = ecumenical_tenet_candidate
		NOT = { root = { knows_tenet = scope:ecumenical_tenet_candidate } }
		root.faith.main_rite = {
			has_tenet_status = {
				tenet = scope:ecumenical_tenet_candidate
				status = unknown
			}
		}
		trigger_if = {
			limit = {
				exists = scope:tenet_a_temp
			}
			NOT = {
				this = scope:tenet_a_temp
			}
		}
		trigger_if = {
			limit = {
				exists = scope:tenet_target_a_shareable_tenet
			}
			NOT = {
				this = scope:tenet_target_a_shareable_tenet
			}
		}
		count >= 1
	}
}

pam_request_legitimacy_priest_trigger = {
	this != $CHARACTER$
	OR = {
		$CHARACTER$.religious_head_or_challenger = this
		government_has_flag = government_is_ecclesiastical
	}
	faith = $CHARACTER$.faith
}

pam_ecclesiastical_superior_or_subordinate_trigger = {
	NOR = {
		$ACTOR$ = $RECIPIENT$
		AND = {
			exists = $ACTOR$.religious_head_or_challenger
			exists = $RECIPIENT$.religious_head_or_challenger
			$ACTOR$.religious_head_or_challenger != $RECIPIENT$.religious_head_or_challenger
		}
	}
	OR = {
		$ACTOR$ = {
			OR = {
				superior ?= $RECIPIENT$
				superior.superior ?= $RECIPIENT$
			}
		}
		$RECIPIENT$ = {
			OR = {
				superior ?= $ACTOR$
				superior.superior ?= $ACTOR$
			}
		}
	}
}

pam_not_antipope_when_recipient_is_hof_tt = {
	trigger_if = {
		limit = {
			scope:recipient ?= $CHARACTER$.faith.religious_head
		}
		$CHARACTER$ = {
			trigger_if = {
				limit = { pam_is_antipope_trigger = yes }
				custom_tooltip = {
					text = is_antipope_blocked_tt
					always = no
				}
			}
		}
	}
}

#Does the province have a domicile that whose owner is the same rite as the character in question and also has a cathedral school level 3 (has_domicile_parameter = domicile_building_is_university)
has_domicile_building_cathedral_school = {
	$LOCATION$ = {
		any_province_domicile = {
			has_domicile_parameter = domicile_building_is_university
			owner = {
				rite = $CHARACTER$.rite
				$CHARACTER$.capital_county.clerical_region_title.holder ?= this
			}
		}
	}
}

# Kingdom of Heaven — no other character of your faith has a higher Piety Level (ties at same level allowed)
pam_kingdom_of_heaven_has_highest_piety_level_in_faith_trigger = {
	NOT = {
		root.faith = {
			any_faith_character = {
				NOT = { this = root }
				piety_level > root.piety_level
			}
		}
	}
}

# Either there is no independent emperor-tier ruler of your faith, or you have a hook on each of them
pam_kingdom_of_heaven_hooks_on_all_faith_emperors_or_none_trigger = {
	NOT = {
		any_independent_ruler = {
			NOT = { this = root }
			is_landed = yes
			faith = root.faith
			highest_held_title_tier >= tier_empire
			save_temporary_scope_as = emperor_temp
			NOT = {
				root = { has_hook = scope:emperor_temp }
			}
		}
	}
}

# At least half of the worldwide same faith counties are in your realm
pam_kingdom_of_heaven_half_faith_in_realm_trigger = {
	pam_koh_same_faith_realm_counties_value >= pam_koh_half_faith_counties_required_value
}

# For identifying religious player trigger (Theocratic ruler events)
pam_government_is_religious_playable_trigger = {
	OR = {
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_ecclesiastical
	}
}

pam_actor_is_clerical_title_authority_of_this_trigger = {
	trigger_if = {
		limit = { exists = religious_head_or_challenger }
		religious_head_or_challenger = scope:puppet_or_actor
	}
	trigger_else = {
		faith.main_rite.head_of_rite ?= scope:puppet_or_actor
	}
}

pam_is_clerical_title_authority_trigger = {
	trigger_if = {
		limit = { exists = religious_head_or_challenger }
		religious_head_or_challenger = this
	}
	trigger_else = {
		faith.main_rite.head_of_rite ?= this
	}
}

pam_recipient_is_clerical_title_authority_trigger = {
	trigger_if = {
		limit = { exists = scope:puppet_or_actor.religious_head_or_challenger }
		scope:puppet_or_actor.religious_head_or_challenger ?= scope:recipient
	}
	trigger_else = {
		scope:puppet_or_actor.faith.main_rite.head_of_rite ?= scope:recipient
	}
}

# secondary_recipient if present (replace), else recipient (grant)
pam_grant_ecclesiastical_title_appointee_changes_rite_trigger = {
	trigger_if = {
		limit = { exists = scope:secondary_recipient }
		scope:secondary_recipient.rite != scope:target.holder.rite
	}
	trigger_else = {
		scope:recipient.rite != scope:target.holder.rite
	}
}

# Non-military clergy ruler: generic theocracy, Christian ecclesiastical, or monastic holy order.
pam_is_clergy_ruler_trigger = {
	OR = {
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_ecclesiastical
		government_has_flag = government_is_monastic_holy_order
	}
}

# Dynastic approval is a separate authority and still applies
# We require the DLC because government_is_theocracy already exists in the base game
pam_grants_own_divorce_trigger = {
	has_pam_dlc_trigger = yes
	pam_is_clergy_ruler_trigger = yes
}

pam_clergy_continence_expected_trigger = {
	rite = {
		NOR = {
			rite_has_parameter = tenet_carnal_exaltation_clergy_no_continence
			trait_is_virtue_rite = lustful
		}
	}
}

# Antipopes are filed as subordinates of the top superior
pam_court_scene_resident_subordinate_trigger = {
	exists = location
	location = scope:ruler.capital_province
	is_available_healthy_ai_adult = yes
	NOT = { has_short_disease_trigger = yes }
	is_clergy = yes
	pam_is_antipope_trigger = no
}

pam_clergy_interactions_faith_trigger = {
	faith = { NOT = { has_doctrine_parameter = unreformed } }
	OR = {
		#Has a theocratic head of faith
		faith = { has_doctrine_parameter = spiritual_head_of_faith }
		rite = { rite_has_parameter = theocracy_temple_lease }
	}
}
claim_valid_faith_subordinate_trigger = {
	pam_request_legitimacy_priest_trigger = { CHARACTER = scope:puppet_or_actor }
	NOR = {
		is_at_war_with = scope:puppet_or_actor
		has_trait = excommunicated
	}
	# Also used to populate the recipient list, where no recipient exists yet
	trigger_if = {
		limit = { exists = scope:recipient }
		NOR = {
			this = scope:recipient
			is_at_war_with = scope:recipient
		}
	}
	is_available_quick = {
		incapable = no
		imprisoned = no
		hostage = no
		ruler = yes
		adult = yes
	}
	highest_held_title_tier >= tier_duchy
	OR = {
		#Is head of faith
		scope:puppet_or_actor.religious_head_or_challenger ?= scope:recipient
		clergy_claim_title_responsibility_trigger = yes
		#you gotta be able to use your puppet
		scope:puppeteer ?= scope:puppet_or_actor
	}
}
clergy_claim_title_responsibility_trigger = {
	OR = {
		#Clerical region overlaps
		AND = {
			any_held_title = {
				has_clerical_region = yes
				any_county_in_clerical_region = {
					any_this_title_or_de_jure_above = {
						holder ?= scope:recipient
					}
				}
			}
		}
		#Landed title overlaps
		AND = {
			is_landed = yes
			any_held_title = {
				OR = {
					#they have a title beneath recipient's titles
					any_this_title_or_de_jure_above = {
						holder ?= scope:recipient
					}
					#they have a title that presides over recipient's titles
					any_in_de_jure_hierarchy = {
						holder ?= scope:recipient
					}
				}
			}
		}
	}
}
clergy_claims_hof_trigger = {
	pam_request_legitimacy_priest_trigger = { CHARACTER = scope:puppet_or_actor }
	NOT = {
		is_at_war_with = scope:puppet_or_actor
	}
	is_available_quick = {
		incapable = no
		imprisoned = no
		hostage = no
		adult = yes
	}
	NOT = {
		has_trait = excommunicated
	}
}

#When AI should be bothering player clergy with requests
pam_should_bother_clergy_interaction_trigger = {
	$PRIEST$ = {
		trigger_if = {
			limit = {
				scope:puppet_or_actor = { is_ai = yes }
				is_ai = no
			}
			OR = {
				scope:puppet_or_actor.highest_held_title_tier >= $PRIEST$.highest_held_title_tier
				AND = {
					scope:puppet_or_actor = { is_vassal_of = $PRIEST$ }
					scope:puppet_or_actor.highest_held_title_tier >= $PRIEST$.highest_held_title_tier_minus_one
				}
				is_allied_to = scope:puppet_or_actor
				AND = {
					exists = house
					house ?= scope:puppet_or_actor.house
				}
				is_heir_of = scope:puppet_or_actor
				any_heir = {
					this = scope:puppet_or_actor
				}
			}
		}
	}
}

# Show the Offer Pilgrimage send option at all - hidden for rites that cannot pilgrimage
pam_offer_pilgrimage_option_shown_trigger = {
	NOT = {
		$ACTOR$.rite = {
			rite_has_parameter = forbidden_from_pilgrimage
		}
	}
}

pam_offer_pilgrimage_option_valid_trigger = {
	$ACTOR$ = {
		custom_tooltip = {
			text = can_host_pilgrimage_tt
			can_host_activity = activity_pilgrimage
		}
		gold > 0
		trigger_if = {
			limit = { is_ai = yes }
			highest_held_title_tier >= tier_kingdom
			short_term_gold > monumental_gold_value
			NOT = {
				any_realm_county = {
					has_province_with_epidemic = { intensity >= major }
				}
			}
		}
	}
}

# Clerical interactions are blocked while a promised pilgrimage is unfulfilled
pam_not_promised_pilgrimage_trigger = {
	$ACTOR$ = {
		custom_tooltip = {
			text = pam_blocked_by_promised_pilgrimage_tt
			NOT = { has_character_modifier = promised_pilgrimage_to_clergy_modifier }
		}
	}
}

pam_ai_deposable_cleric_trigger = {
	NOR = {
		has_relation_friend = $HEAD_OF_FAITH$
		has_relation_lover = $HEAD_OF_FAITH$
	}
	OR = {
		has_relation_rival = $HEAD_OF_FAITH$
		AND = {
			$HEAD_OF_FAITH$ = {
				NOR = {
					has_trait = cynical
					has_trait = arbitrary
					has_trait = excommunicated
				}
			}
			NOT = { has_imprisonment_reason = $HEAD_OF_FAITH$ }
			OR = {
				has_trait = excommunicated
				$HEAD_OF_FAITH$ = { has_imprisonment_reason = prev }
			}
		}
	}
}

# We set this just under the default heresy threshold so rites convert before heresy fires
# Phase first: outside Reform the global lookup ends this before the per-see checks
#pam_ai_reform_rite_replacement_trigger = {
#	situation:the_christian_church ?= { situation_current_phase = reform_phase }
#	scope:actor.faith = { fervor < 50 }
#	scope:target.holder ?= {
#		rite != scope:actor.rite
#		is_ai = yes
#		rite = {
#			"divergence(faith.main_rite)" >= 45
#		}
#	}
#	scope:recipient = { rite = scope:actor.rite }
#	years_from_game_start >= 25
#}

pam_beseech_claims_access_trigger = {
	has_pam_dlc_trigger = yes
	pam_clergy_interactions_faith_trigger = yes
	NOT = {
		government_has_flag = government_is_ecclesiastical
	}
}
pam_actor_rite_has_core_simony_trigger = {
	scope:actor = {
		rite = {
			rite_has_parameter = tenet_simony_gold_donations
		}
	}
}

pam_actor_has_simony_trigger = {
	scope:actor = {
		OR = {
			rite = { rite_has_parameter = tenet_simony_ecclesiastical_purchase }
			has_personal_tenet_flag = tenet_simony_ecclesiastical_purchase
		}
	}
}

# Head of Rite dies while still subject to short_reign on their primary title.
pam_head_of_rite_had_short_reign_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
		religion = religion:catholic_religion
		religion = religion:protestant_religion
		religion = religion:eastern_orthodox_religion
		religion = religion:oriental_orthodox_religion
	}
	rite.head_of_rite ?= root
	primary_title ?= {
		title_held_years < 10
	}
}

# Recipient is the scope, and we check the pointers before scanning counties
pam_rite_sway_connected_recipient_trigger = {
	OR = {
		is_courtier_of = $ACTOR$
		is_vassal_or_below_of = $ACTOR$
		top_liege ?= $ACTOR$.top_liege
		capital_county.clerical_region_title.holder ?= $ACTOR$
		any_sub_realm_county = {
			any_neighboring_county = { rite = $ACTOR$.rite }
		}
	}
}

# We keep this in sync with sway_ruler_to_my_rite_interaction is_shown and is_valid
pam_sway_to_rite_diocese_candidate_trigger = {
	this != $ARCHBISHOP$
	is_ruler = yes
	is_adult = yes
	faith = $ARCHBISHOP$.faith
	rite != $ARCHBISHOP$.rite
	NOR = {
		has_trait = incapable
		rite.head_of_rite ?= this
		religious_head_or_challenger ?= this
	}
	capital_county.clerical_region_title.holder ?= $ARCHBISHOP$
}

# These are prelates of other dioceses still on another rite, for the peer petition important actions
pam_sway_to_rite_adjacent_prelate_candidate_trigger = {
	this != $ARCHBISHOP$
	is_ruler = yes
	is_adult = yes
	faith = $ARCHBISHOP$.faith
	rite != $ARCHBISHOP$.rite
	NOR = {
		has_trait = incapable
		rite.head_of_rite ?= this
		religious_head_or_challenger ?= this
	}
	any_held_title = { has_clerical_region = yes }
}

# A Head of Faith on the mainline rite may call other Heads of Rite back to it, which Sway to Rite otherwise forbids
pam_sway_to_rite_head_of_faith_reclaims_trigger = {
	$ACTOR$ = {
		faith.religious_head ?= this
		faith.main_rite ?= rite
	}
}

# Sway to Rite reach lane: petitioning a fellow prelate.
pam_rite_sway_peer_prelate_lane_trigger = {
	$RECIPIENT$ = {
		any_held_title = { has_clerical_region = yes }
	}
	$ACTOR$ = {
		pam_is_archbishop_or_higher_trigger = yes
		piety_level >= high_piety_level
	}
}

# Sway to Rite reach lane: petitioning the liege chain.
pam_rite_sway_liege_lane_trigger = {
	$RECIPIENT$ = { is_liege_or_above_of = $ACTOR$ }
	$ACTOR$ = { piety_level >= very_high_piety_level }
}

# $ACTOR$ and $RECIPIENT$ dioceses border or are nearby, and the scan is bounded by the actor's diocese
pam_rite_sway_adjacent_diocese_trigger = {
	OR = {
		$ACTOR$.capital_county.clerical_region_title ?= {
			any_county_in_clerical_region = {
				any_neighboring_county = {
					clerical_region_title ?= $RECIPIENT$.capital_county.clerical_region_title
				}
			}
		}
		AND = {
			# No diocese seat on the far end means there is no adjacent diocese to measure
			exists = $RECIPIENT$.capital_county.clerical_region_title.title_capital_county
			$ACTOR$.capital_county.clerical_region_title.title_capital_county ?= {
				squared_distance = {
					target = $RECIPIENT$.capital_county.clerical_region_title.title_capital_county
					value <= squared_distance_small
				}
			}
		}
	}
}

# We put quick checks first and leave the bordering realm scan last, behind a capital distance check
pam_rite_sway_clergy_reach_trigger = {
	OR = {
		custom_tooltip = {
			text = sway_ruler_to_my_rite_interaction_flock_reach_tt
			OR = {
				$RECIPIENT$ = {
					superior ?= $ACTOR$
				}
				trigger_if = {
					limit = { $RECIPIENT$ = { is_ruler = yes } }
					$RECIPIENT$.capital_county.clerical_region_title.holder ?= $ACTOR$
				}
				trigger_else = {
					$RECIPIENT$.location.county.clerical_region_title.holder ?= $ACTOR$
				}
			}
		}
		custom_tooltip = {
			text = sway_ruler_to_my_rite_interaction_prelate_reach_tt
			pam_rite_sway_peer_prelate_lane_trigger = {
				ACTOR = $ACTOR$
				RECIPIENT = $RECIPIENT$
			}
		}
		custom_tooltip = {
			text = sway_ruler_to_my_rite_interaction_liege_reach_tt
			pam_rite_sway_liege_lane_trigger = {
				ACTOR = $ACTOR$
				RECIPIENT = $RECIPIENT$
			}
		}
		custom_tooltip = {
			text = sway_ruler_to_my_rite_interaction_border_reach_tt
			# A landless recipient has no capital county for the distance pre-gate to measure
			exists = $RECIPIENT$.capital_county
			$ACTOR$.capital_county ?= {
				squared_distance = {
					target = $RECIPIENT$.capital_county
					value <= squared_distance_large
				}
			}
			$ACTOR$ = {
				any_neighboring_realm_owner = { this = $RECIPIENT$ }
			}
		}
	}
}

# Sway to Rite: landed house or bloc kin, as with Demand Conversion. Lives on the vassal variant.
pam_rite_sway_house_lane_trigger = {
	$RECIPIENT$ = { is_ruler = yes }
	$ACTOR$ = { is_house_head = yes }
	OR = {
		$RECIPIENT$.house ?= $ACTOR$.house
		$RECIPIENT$.house.house_confederation ?= { # House Bloc
			$ACTOR$.house.house_confederation ?= this
			has_cohesion_level_parameter = bloc_members_join_leaders_faith
		}
	}
}

# Iterates the faith's few holy sites instead of scanning the recipient's whole sub-realm
pam_sway_recipient_holds_holy_site_trigger = {
	scope:puppet_or_actor.faith ?= {
		any_holy_site = {
			exists = holy_site_county.holder
			holy_site_county.holder = {
				OR = {
					this = scope:recipient
					target_is_liege_or_above = scope:recipient
				}
			}
		}
	}
}

# We keep this in sync with how the three Sway to Rite variants split is_shown
pam_sway_to_rite_variant_can_send_trigger = {
	trigger_if = {
		limit = { $TARGET$ = { is_courtier_of = $ARCHBISHOP$ } }
		$ARCHBISHOP$ = {
			is_character_interaction_potentially_accepted = {
				recipient = $TARGET$
				interaction = sway_courtier_to_my_rite_interaction
			}
		}
	}
	trigger_else_if = {
		limit = {
			$TARGET$ = {
				OR = {
					target_is_liege_or_above = $ARCHBISHOP$
					is_tributary_of = $ARCHBISHOP$
					pam_rite_sway_house_lane_trigger = {
						ACTOR = $ARCHBISHOP$
						RECIPIENT = this
					}
				}
			}
		}
		$ARCHBISHOP$ = {
			is_character_interaction_potentially_accepted = {
				recipient = $TARGET$
				interaction = sway_vassal_to_my_rite_interaction
			}
		}
	}
	trigger_else = {
		$ARCHBISHOP$ = {
			is_character_interaction_potentially_accepted = {
				recipient = $TARGET$
				interaction = sway_ruler_to_my_rite_interaction
			}
		}
	}
}

# As above, but also demands the given ai_accept threshold from the matching variant.
pam_sway_to_rite_variant_acceptance_trigger = {
	trigger_if = {
		limit = { $TARGET$ = { is_courtier_of = $ARCHBISHOP$ } }
		$ARCHBISHOP$ = {
			is_character_interaction_potentially_accepted = {
				recipient = $TARGET$
				interaction = sway_courtier_to_my_rite_interaction
				ai_accept = $ACCEPT$
			}
		}
	}
	trigger_else_if = {
		limit = {
			$TARGET$ = {
				OR = {
					target_is_liege_or_above = $ARCHBISHOP$
					is_tributary_of = $ARCHBISHOP$
					pam_rite_sway_house_lane_trigger = {
						ACTOR = $ARCHBISHOP$
						RECIPIENT = this
					}
				}
			}
		}
		$ARCHBISHOP$ = {
			is_character_interaction_potentially_accepted = {
				recipient = $TARGET$
				interaction = sway_vassal_to_my_rite_interaction
				ai_accept = $ACCEPT$
			}
		}
	}
	trigger_else = {
		$ARCHBISHOP$ = {
			is_character_interaction_potentially_accepted = {
				recipient = $TARGET$
				interaction = sway_ruler_to_my_rite_interaction
				ai_accept = $ACCEPT$
			}
		}
	}
}

# Runs in the ruler's scope
pam_rite_locally_present_trigger = {
	any_sub_realm_county = {
		OR = {
			rite = $RITE$
			any_neighboring_county = { rite = $RITE$ }
		}
	}
}

pam_died_from_murder_reason_trigger = {
	trigger_if = {
		limit = { is_alive = no }
		OR = {
			exists = scope:killer
			death_reason = death_murder
			death_reason = death_murder_known
			death_reason = death_plotting
			death_reason = death_poison
			death_reason = death_murder_feast
			death_reason = death_mysterious
			death_reason = death_killed_while_attempting_murder
		}
	}
	trigger_else = {
		exists = scope:killer
	}
}

pam_clergy_court_chaplain_murdered_catalyst_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	has_council_position = councillor_court_chaplain
	pam_died_from_murder_reason_trigger = yes
}

pam_clergy_archbishop_murdered_catalyst_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	any_held_title = { has_clerical_region = yes }
	pam_died_from_murder_reason_trigger = yes
}

pam_clergy_cardinal_murdered_catalyst_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	exists = clerical_elector_title
	pam_died_from_murder_reason_trigger = yes
}

pam_clergy_head_of_rite_murdered_catalyst_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	rite.head_of_rite ?= this
	pam_died_from_murder_reason_trigger = yes
}

pam_the_christian_church_head_of_faith_murdered_catalyst_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	pam_died_from_murder_reason_trigger = yes
	any_held_title = { is_head_of_faith = yes }
}

pam_the_christian_church_civil_war_in_christian_kingdom_participant_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
				religion = religion:catholic_religion
				religion = religion:protestant_religion
				religion = religion:eastern_orthodox_religion
				religion = religion:oriental_orthodox_religion
	}
	highest_held_title_tier >= tier_kingdom
	any_character_situation = {
		situation_type = the_christian_church
		situation_top_has_catalyst = catalyst_the_christian_church_civil_war_in_christian_kingdom
	}
}

pam_neighboring_unreformed_pagan_raid_you_catalyst_trigger = {
	scope:raider = {
		religion = {
			is_in_family = rf_pagan
		}
		faith = { has_doctrine_parameter = unreformed }
		any_neighboring_and_across_water_realm_owner = {
			this = scope:county.holder.top_liege
		}
	}
	scope:county.holder.top_liege = {
		has_pam_dlc_trigger = yes
		any_character_situation = {
			situation_type = the_christian_church
			situation_top_has_catalyst = catalyst_neighboring_unreformed_pagan_raid_you
		}
	}
}

pam_the_christian_church_civil_war_in_christian_kingdom_trigger = {
	scope:war = {
		using_civil_war_cb_trigger = yes
	}
	OR = {
		AND = {
			scope:attacker.top_liege = scope:defender.top_liege
			scope:defender.top_liege = {
				pam_the_christian_church_civil_war_in_christian_kingdom_participant_trigger = yes
			}
		}
		AND = {
			scope:defender = scope:attacker.top_liege
			scope:defender = {
				pam_the_christian_church_civil_war_in_christian_kingdom_participant_trigger = yes
			}
		}
		AND = {
			scope:attacker = scope:defender.top_liege
			scope:attacker = {
				pam_the_christian_church_civil_war_in_christian_kingdom_participant_trigger = yes
			}
		}
	}
}

pam_the_christian_church_ruler_died_in_battle_against_non_christian_catalyst_trigger = {
	has_pam_dlc_trigger = yes
	is_ruler = yes
	# death_reason errors on living characters; in on_death the dier is still is_alive = yes, so use has_variable = battle_death_enemy there.
	trigger_if = {
		limit = { is_alive = no }
		death_reason = death_battle
	}
	trigger_else = {
		has_variable = battle_death_enemy
	}
	OR = {
		exists = scope:killer
		has_variable = battle_death_enemy
	}
	trigger_if = {
		limit = { exists = scope:killer }
		scope:killer = {
			OR = {
						religion != religion:catholic_religion
						religion != religion:protestant_religion
						religion != religion:eastern_orthodox_religion
						religion != religion:oriental_orthodox_religion
			}
		}
	}
	trigger_else = {
		has_variable = battle_death_enemy
		var:battle_death_enemy = {
			OR = {
						religion != religion:catholic_religion
						religion != religion:protestant_religion
						religion != religion:eastern_orthodox_religion
						religion != religion:oriental_orthodox_religion
			}
		}
	}
	any_character_situation = {
		situation_type = the_christian_church
		situation_top_has_catalyst = catalyst_the_christian_church_ruler_died_in_battle_against_non_christian
	}
}

pam_ecclesiastic_transfer_land_access_trigger = {
	has_pam_dlc_trigger = yes
	pam_clergy_interactions_faith_trigger = yes
	NOT = {
		government_has_flag = government_is_ecclesiastical
	}
}
#The recipient of the "Give me land" interaction will back down without any convincing required
#The crit, the instant success
pam_instant_negotiate_land_success_trigger = {
	scope:recipient = {
		is_ai = yes
	}
	OR = {
		#They don't have the legitimacy or piety
		scope:recipient = {
			piety_level <= 0
			legitimacy_level <= 0
		}
		#You have the pope in your pocket
		trigger_if = {
			limit = {
				exists = scope:secondary_recipient
			}
			pam_negotiate_land_instant_pope_hook_trigger = yes
		}
		#You intimidate them militarily or through dread
		OR = {
			#Mostly milstrength
			AND = {
				#Milstrength 4x as high
				half_current_military_strength >= scope:recipient.twice_current_military_strength
				has_dread_level_towards = {
					target = scope:recipient
					level >= 1
				}
			}
			#Milstrength and intimidation
			AND = {
				#Milstrength twice as high
				current_military_strength >= scope:recipient.twice_current_military_strength
				has_dread_level_towards = {
					target = scope:recipient
					level >= 2
				}
			}
		}
	}
}

pam_negotiate_land_instant_pope_hook_trigger = {
	has_strong_hook = scope:secondary_recipient
	highest_held_title_tier >= scope:recipient.highest_held_title_tier
	#Won't give away primary title or its aboves
	scope:target = {
		NOR = {
			this = scope:recipient.primary_title
			any_in_de_jure_hierarchy = {
				this = scope:recipient.primary_title
			}
		}
	}
	scope:target.tier < tier_kingdom
	faith.religious_head = scope:secondary_recipient
	scope:recipient = {
		NOR = {
			has_trait = cynical
			has_trait = excommunicated
		}
		rite = faith.main_rite
	}
}

#You DEFINITELY can force the recipient to give in, if you sacrifice
pam_forced_negotiate_land_success_trigger = {
	OR = {
		pam_negotiate_land_forced_hook_trigger = yes
		pam_negotiate_land_forced_relation_trigger = yes
		pam_negotiate_land_forced_piety_level_trigger = yes
		pam_negotiate_land_forced_legitimacy_level_trigger = yes
		pam_negotiate_land_forced_gold_trigger = yes
	}
}

#There is a CHANCE you can convince the recipient to give 
pam_possible_negotiate_land_success_trigger = {
	OR = {
		pam_negotiate_land_possible_hook_trigger = yes
		pam_negotiate_land_possible_relation_trigger = yes
		pam_negotiate_land_possible_piety_level_trigger = yes
		pam_negotiate_land_possible_legitimacy_level_trigger = yes
		pam_negotiate_land_possible_gold_trigger = yes
	}
}

pam_intimidate_clerical_transfer_recipient_trigger = {
	scope:recipient = {
		is_ai = yes
		#Needs non-combative personality
		NOR = {
			has_trait = greatest_of_khans
			has_trait = conqueror
		}
	}
}
pam_negotiate_land_forced_hook_trigger = {
	has_usable_hook = scope:recipient
	has_strong_hook = scope:recipient
	#Isn't root's capital county/doesn't contain recipient's capital county
	trigger_if = {
		limit = {
			exists = scope:target
		}
		scope:target = {
			NOR = {
				this = scope:recipient.primary_title
				any_in_de_jure_hierarchy = {
					this = scope:recipient.capital_county
				}
			}
		}
		scope:target.tier < scope:recipient.highest_held_title_tier
	}
	scope:recipient = {
		is_ai = yes
	}
}
pam_negotiate_land_forced_relation_trigger = {
	OR = {
		AND = {
			has_relation_friend = scope:recipient
			has_relation_soulmate = scope:recipient
		}
		AND = {
			has_relation_disciple = scope:recipient
			has_relation_soulmate = scope:recipient
		}
		AND = {
			has_relation_friend = scope:recipient
			has_relation_disciple = scope:recipient
		}
		has_relation_best_friend = scope:recipient
	}
}
pam_negotiate_land_forced_piety_level_trigger = {
	OR = {
		AND = {
			piety_level > scope:recipient.piety_level
			scope:recipient.piety_level = 0
		}
		AND = {
			piety_level >= 5
			scope:recipient.piety_level <= 1
		}
	}
}
pam_negotiate_land_forced_legitimacy_level_trigger = {
	has_legitimacy = yes
	scope:recipient = { has_legitimacy = yes }
	OR = {
		AND = {
			legitimacy_level > scope:recipient.legitimacy_level
			scope:recipient.legitimacy_level = 0
		}
		AND = {
			legitimacy_level >= 5
			scope:recipient.legitimacy_level <= 1
		}
	}
}
pam_negotiate_land_forced_gold_trigger = {
	gold >= scope:recipient.pam_transfer_land_gold_value
	scope:recipient = {
		OR = {
			debt_level >= 4
			treasury_debt_level >= 4
			#Losing a war and needs the money
			AND = {
				is_at_war = yes
				any_character_war = {
					OR = {
						AND = {
							primary_defender = scope:recipient
							defender_war_score <= -50
							primary_attacker.current_military_strength > scope:recipient.twice_current_military_strength
						}
						AND = {
							primary_attacker = scope:recipient
							attacker_war_score <= -50
							primary_attacker.current_military_strength > scope:recipient.twice_current_military_strength
						}
					}
				}
				OR = {
					gold < major_gold_value
					treasury < major_gold_value
				}
			}
		}
	}
}
pam_negotiate_land_possible_hook_trigger = {
	#Hook
	has_usable_hook = scope:recipient
	scope:recipient = {
		is_ai = yes
	}
}
pam_negotiate_land_possible_relation_trigger = {
	OR = {
		#Strong relations you can destroy
		has_relation_friend = scope:recipient
		has_relation_soulmate = scope:recipient
		has_relation_disciple = scope:recipient
	}
}
pam_negotiate_land_possible_piety_level_trigger = {
	#Superior devotion
	piety_level > scope:recipient.piety_level
}
pam_negotiate_land_possible_legitimacy_level_trigger = {
	has_legitimacy = yes
	scope:recipient = { has_legitimacy = yes }
	#Superior legitimacy
	legitimacy_level > scope:recipient.legitimacy_level
}
pam_negotiate_land_possible_gold_trigger = {
	#Gold to pay for land
	treasury_or_gold >= scope:recipient.pam_transfer_land_gold_value
}

pam_clergy_land_interaction_likely_acceptance_no_methods_trigger = {
	scope:recipient = {
		is_ai = yes
	}
	OR = {
		AND = {
			OR = {
				religious_head_or_challenger = scope:secondary_recipient
				is_allied_to = scope:recipient
			}
			OR = {
				highest_held_title_tier > scope:recipient.highest_held_title_tier
				scope:recipient = { ai_zeal >= medium_positive_ai_value }
				scope:recipient = { ai_greed <= medium_negative_ai_value }
			}
		}
		#You intimidate them militarily or through dread
		OR = {
			#Mostly milstrength
			AND = {
				#Milstrength 2x as high
				current_military_strength >= scope:recipient.twice_current_military_strength
				has_dread_level_towards = {
					target = scope:recipient
					level >= 1
				}
			}
			#Milstrength and intimidation
			AND = {
				#Milstrength higher
				current_military_strength > scope:recipient.twice_current_military_strength
				has_dread_level_towards = {
					target = scope:recipient
					level >= 2
				}
			}
			#Milstrength
			#Milstrength 4x as high
			half_current_military_strength >= scope:recipient.twice_current_military_strength
		}
	}
}


pam_tenet_can_be_permitted_trigger = {
	save_temporary_scope_as = pam_tenet_candidate
	custom_tooltip = {
		text = pam_tenet_stance_can_be_permitted_tt
		scope:actor.rite = {
			NOR = {
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = permitted }
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = core }
			}
		}
	}
}

pam_tenet_can_be_permitted_no_actor_trigger = {
	save_temporary_scope_as = pam_tenet_candidate
	custom_tooltip = {
		text = pam_tenet_stance_can_be_permitted_tt
		root.rite = {
			NOR = {
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = permitted }
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = core }
			}
		}
	}
}

pam_tenet_can_be_prohibited_trigger = {
	save_temporary_scope_as = pam_tenet_candidate
	custom_tooltip = {
		text = pam_tenet_stance_can_be_prohibited_tt
		scope:actor.rite = {
			NOR = {
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = prohibited }
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = core }
			}
		}
	}
}

pam_tenet_can_be_prohibited_no_actor_trigger = {
	save_temporary_scope_as = pam_tenet_candidate
	custom_tooltip = {
		text = pam_tenet_stance_can_be_prohibited_tt
		root.rite = {
			NOR = {
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = prohibited }
				has_tenet_status = { tenet = scope:pam_tenet_candidate status = core }
			}
		}
	}
}

# Tenet scope. Practices a Christian church reads as corruption rather than doctrine.
pam_tenet_is_christian_vice_trigger = {
	OR = {
		this = tenet:tenet_simony
		this = tenet:tenet_polyamory
		this = tenet:tenet_hedonistic
		this = tenet:tenet_gnosticism
		this = tenet:tenet_carnal_exaltation
		this = tenet:tenet_natural_primitivism
		this = tenet:tenet_consolamentum
	}
}

# Tenet scope. Christologies settled against by Nicaea, Chalcedon and Antioch.
pam_tenet_is_christological_heresy_trigger = {
	OR = {
		this = tenet:tenet_monophysitism
		this = tenet:tenet_miaphysitism
		this = tenet:tenet_adoptionism
	}
}

# Decision-side counterpart of pam_ec_tenet_is_christian_abomination_trigger
# We leave Ritual Cannibalism off because the Eucharist already invites the charge
pam_tenet_is_christian_abomination_trigger = {
	OR = {
		this = tenet:tenet_human_sacrifice
		this = tenet:tenet_cranial_trophies
		this = tenet:tenet_gruesome_festivals
		this = tenet:tenet_sacrificial_ceremonies
	}
}

# We pool these in tenet scope, since prohibit and core checks already close the wrong paths per faith
pam_tenet_is_christian_dogma_trigger = {
	OR = {
		this = tenet:tenet_apostolic_succession
		this = tenet:tenet_divine_liturgy
		this = tenet:tenet_communion
		this = tenet:tenet_original_sin
		this = tenet:tenet_transubstantiation
		this = tenet:tenet_pentarchy
		this = tenet:tenet_celestial_hierarchy
		this = tenet:tenet_confession
		this = tenet:tenet_philanthropia
		this = tenet:tenet_hyperdulia
		this = tenet:tenet_armed_pilgrimages
		this = tenet:tenet_scholasticism
		this = tenet:tenet_holy_myron
		this = tenet:tenet_alexandrian_catechism
	}
}

# $CHARACTER$ is the Head of Faith deciding, and the tenet is the scope
# We only apply this to the AI, so players keep the full list
pam_tenet_ai_may_permit_trigger = {
	trigger_if = {
		limit = {
			$CHARACTER$.faith = { has_tenet_popularity_trigger = yes }
			pam_tenet_is_christian_vice_trigger = yes
		}
		OR = {
			situation:the_christian_church ?= { situation_current_phase = saeculum_obscurum }
			pam_tenet_is_at_least_very_popular_trigger = { CHARACTER = $CHARACTER$ }
		}
	}
	trigger_else_if = {
		limit = {
			$CHARACTER$.faith = { has_tenet_popularity_trigger = yes }
			OR = {
				pam_tenet_is_christological_heresy_trigger = yes
				pam_tenet_is_christian_abomination_trigger = yes
			}
		}
		pam_tenet_is_overwhelmingly_popular_trigger = { CHARACTER = $CHARACTER$ }
	}
	trigger_else = {
		always = yes
	}
}

pam_tenet_ai_may_prohibit_trigger = {
	trigger_if = {
		limit = {
			$CHARACTER$.faith = { has_tenet_popularity_trigger = yes }
			pam_tenet_is_christian_dogma_trigger = yes
		}
		pam_tenet_is_unpopular_trigger = { CHARACTER = $CHARACTER$ }
	}
}

# Phase first: this runs per candidate tenet, so outside Reform the global lookup ends it at once
pam_tenet_ai_should_purge_trigger = {
	situation:the_christian_church ?= { situation_current_phase = reform_phase }
	OR = {
		pam_tenet_is_christian_vice_trigger = yes
		pam_tenet_is_christian_abomination_trigger = yes
	}
	$CHARACTER$.faith = { has_tenet_popularity_trigger = yes }
}

pam_tenet_can_be_persecuted_trigger = {
	save_temporary_scope_as = pam_tenet_candidate
	custom_tooltip = {
		text = pam_tenet_can_be_persecuted_tt
		NOT = {
			scope:actor = {
				any_personal_tenet = { this = scope:pam_tenet_candidate }
			}
		}
	}
}

pam_tenet_can_be_persecuted_no_actor_trigger = {
	save_temporary_scope_as = pam_tenet_candidate
	custom_tooltip = {
		text = pam_tenet_can_be_persecuted_tt
		NOT = {
			root = {
				any_personal_tenet = { this = scope:pam_tenet_candidate }
			}
		}
	}
}

pam_tenet_is_incompatible_with_armed_pilgrimages_trigger = {
	OR = {
		this = tenet:tenet_peace_of_god
		this = tenet:tenet_pacifism
		this = tenet:tenet_dharmic_pacifism
	}
}

pam_tenet_can_be_replaced_for_armed_pilgrimages_trigger = {
	trigger_if = {
		limit = {
			scope:actor.faith.main_rite ?= {
				any_rite_tenet = {
					status = core
					pam_tenet_is_incompatible_with_armed_pilgrimages_trigger = yes
				}
			}
		}
		custom_tooltip = {
			text = pam_secure_pilgrim_routes_incompatible_tenet_tt
			pam_tenet_is_incompatible_with_armed_pilgrimages_trigger = yes
		}
	}
}

pam_faith_tenet_is_core_trigger = {
	main_rite = {
		has_tenet_status = { tenet = $TENET$ status = core }
	}
}

pam_faith_is_at_core_tenet_cap_trigger = {
	main_rite = {
		any_rite_tenet = {
			count >= pam_faith_core_tenets_cap_value
		}
	}
}

# $CHARACTER$'s faith supplies the popularity, from tenet scope
pam_tenet_is_overwhelmingly_popular_trigger = {
	save_temporary_scope_as = pam_popularity_tenet
	$CHARACTER$.faith = {
		has_tenet_popularity_trigger = yes
		pam_faith_tenet_popularity_value >= pam_tenet_popularity_ai_override_threshold
	}
}

# At or above the Popular display tier
pam_tenet_is_at_least_popular_trigger = {
	save_temporary_scope_as = pam_popularity_tenet
	$CHARACTER$.faith = {
		has_tenet_popularity_trigger = yes
		pam_faith_tenet_popularity_value >= tenet_popularity_popular_threshold
	}
}

# As above, but for the Very Popular display tier.
pam_tenet_is_at_least_very_popular_trigger = {
	save_temporary_scope_as = pam_popularity_tenet
	$CHARACTER$.faith = {
		has_tenet_popularity_trigger = yes
		pam_faith_tenet_popularity_value >= tenet_popularity_very_popular_threshold
	}
}

# Below the Neutral display tier: the faithful have turned against it, not merely indifferent
pam_tenet_is_unpopular_trigger = {
	save_temporary_scope_as = pam_popularity_tenet
	$CHARACTER$.faith = {
		has_tenet_popularity_trigger = yes
		pam_faith_tenet_popularity_value < tenet_popularity_neutral_threshold
	}
}

pam_rite_can_be_declared_heresy_trigger = {
	save_temporary_scope_as = pam_rite_candidate
	custom_tooltip = {
		text = pam_rite_heresy_not_mainline_tt
		this != faith.main_rite
	}
	custom_tooltip = {
		text = pam_rite_heresy_no_antipope_tt
		faith = {
			NOT = {
				any_faith_ruler = {
					rite = scope:pam_rite_candidate
					is_following_religious_head_challenger = yes
				}
			}
		}
	}
	trigger_if = {
		limit = {
			faith.religious_head ?= { has_trait = zealous }
		}
		custom_tooltip = {
			text = pam_rite_heresy_divergence_zealous_tt
			"divergence(faith.main_rite)" > faith_heresy_threshold_times_4_over_5
		}
	}
	trigger_else = {
		custom_tooltip = {
			text = pam_rite_heresy_divergence_tt
			"divergence(faith.main_rite)" > faith.heresy_threshold
		}
	}
}

pam_ai_meaningful_jewish_presence_trigger = {
	OR = {
		any_vassal = {
			faith ?= { religion_tag = judaism_religion }
		}
		any_sub_realm_county = {
			faith ?= { religion_tag = judaism_religion }
		}
		any_neighboring_and_across_water_realm_owner = {
			faith ?= { religion_tag = judaism_religion }
		}
	}
}

pam_ai_meaningful_christian_presence_trigger = {
	OR = {
		any_vassal = {
			OR = {
				faith ?= { religion_tag = catholic_religion }
				faith ?= { religion_tag = protestant_religion }
				faith ?= { religion_tag = eastern_orthodox_religion }
				faith ?= { religion_tag = oriental_orthodox_religion }
			}
		}
		any_sub_realm_county = {
			OR = {
				faith ?= { religion_tag = catholic_religion }
				faith ?= { religion_tag = protestant_religion }
				faith ?= { religion_tag = eastern_orthodox_religion }
				faith ?= { religion_tag = oriental_orthodox_religion }
			}
		}
		any_neighboring_and_across_water_realm_owner = {
			OR = {
				faith ?= { religion_tag = catholic_religion }
				faith ?= { religion_tag = protestant_religion }
				faith ?= { religion_tag = eastern_orthodox_religion }
				faith ?= { religion_tag = oriental_orthodox_religion }
			}
		}
	}
}

pam_ai_meaningful_islamic_presence_trigger = {
	OR = {
		any_vassal = {
			faith ?= { religion_tag = islam_religion }
		}
		any_sub_realm_county = {
			faith ?= { religion_tag = islam_religion }
		}
		any_neighboring_and_across_water_realm_owner = {
			faith ?= { religion_tag = islam_religion }
		}
	}
}

pam_ai_meaningful_eastern_presence_trigger = {
	OR = {
		any_vassal = {
			faith.religion ?= { is_in_family = rf_eastern }
		}
		any_sub_realm_county = {
			faith.religion ?= { is_in_family = rf_eastern }
		}
		any_neighboring_and_across_water_realm_owner = {
			faith.religion ?= { is_in_family = rf_eastern }
		}
	}
}

pam_ai_meaningful_sinitic_presence_trigger = {
	OR = {
		any_vassal = {
			faith.religion ?= { is_in_family = rf_sinitic }
		}
		any_sub_realm_county = {
			faith.religion ?= { is_in_family = rf_sinitic }
		}
		any_neighboring_and_across_water_realm_owner = {
			faith.religion ?= { is_in_family = rf_sinitic }
		}
	}
}

pam_ai_meaningful_unreformed_presence_trigger = {
	OR = {
		any_vassal = {
			faith ?= { has_doctrine_parameter = unreformed }
		}
		any_sub_realm_county = {
			faith ?= { has_doctrine_parameter = unreformed }
		}
		any_neighboring_and_across_water_realm_owner = {
			faith ?= { has_doctrine_parameter = unreformed }
		}
	}
}

pam_ai_oriental_christian_actor_trigger = {
	OR = {
		culture = { has_cultural_pillar = heritage_egyptian }
		culture = { has_cultural_pillar = heritage_syriac }
		culture = { has_cultural_pillar = heritage_caucasian }
		culture = { has_same_culture_language = culture:ethiopian }
		faith ?= faith:miaphysitism
		faith ?= faith:armenian_apostolic
	}
}

pam_ai_armenian_actor_trigger = {
	OR = {
		culture = culture:armenian
		culture = { has_same_culture_language = culture:armenian }
	}
}

pam_ai_pentarchy_christian_actor_trigger = {
	OR = {
		culture = culture:greek
		culture = { has_same_culture_language = culture:greek }
		rite ?= { pam_greek_as_liturgical_language_trigger = yes }
		# Alexandria and Antioch are Oriental Orthodox sees, so those churches count the five as their own.
		pam_ai_oriental_christian_actor_trigger = yes
	}
}

pam_ai_southeast_asian_actor_trigger = {
	OR = {
		culture = { has_cultural_pillar = heritage_austronesian }
		culture = { has_cultural_pillar = heritage_mon_khmer }
		culture = { has_cultural_pillar = heritage_viet }
		culture = { has_cultural_pillar = heritage_tai }
		culture = { has_cultural_pillar = heritage_hmongic }
		culture = { has_cultural_pillar = heritage_burman }
	}
}

pam_ai_mountain_capital_actor_trigger = {
	capital_province ?= {
		OR = {
			terrain = mountains
			terrain = hills
		}
	}
}

# title_preferred_capital_county is the de jure capital even if k_papal_state is uncreated
pam_catholic_hof_weak_papal_state_control_trigger = {
	has_pam_dlc_trigger = yes
	title:k_papal_state = {
		title_preferred_capital_county = {
			holder ?= {
				is_alive = yes
				faith = {
					NOR = {
						has_doctrine = special_doctrine_ecumenical_christian
						this = faith:catholic
						this = faith:christian_faith
					}
				}
			}
		}
	}
}

# Holder of the Byzantine Empire (or Eastern Roman hegemony) with a non-Christian religion
#pam_byzantine_emperor_non_christian_trigger = {
#	OR = {
#		title:e_byzantium.holder ?= {
#			is_alive = yes
#			religion != religion:christianity_religion
#		}
#		title:h_eastern_roman_empire.holder ?= {
#			is_alive = yes
#			religion != religion:christianity_religion
#		}
#	}
#}

# Used by demand_child_as_monk_interaction.
# True if scope:recipient genuinely cares about losing scope:secondary_recipient, either their own direct child, or any child betrothed to one of their close family members.
pam_demand_child_recipient_cares_trigger = {
	OR = {
		scope:secondary_recipient = { is_child_of = scope:recipient }
		scope:secondary_recipient = {
			exists = betrothed
			betrothed = { is_close_family_of = scope:recipient }
		}
	}
}

pam_has_forbidden_personal_tenet_trigger = {
	faith = { save_temporary_scope_as = temp_faith }
	any_personal_tenet = {
		scope:temp_faith.main_rite = {
			has_tenet_status = {
				tenet = prev
				status = prohibited
			}
		}
	}
}

pam_ai_can_consider_forbidden_personal_tenet_trigger = {
	ai_boldness >= -25
	OR = {
		ai_zeal >= 100
		AND = {
			ai_boldness >= 100
			ai_zeal >= 0
		}
		AND = {
			ai_rationality < 0
			ai_zeal >= 0
		}
	}
	NOR = {
		has_trait = humble
		has_trait = content
	}
	# Clergy: a prohibited personal tenet is a criminal secret, so the elected pontiff never considers one
	# This goes last because it iterates held titles, unlike the simple checks above
	pam_holds_college_elected_church_seat_trigger = no
}

pam_is_christian_clergy_trigger = {
	OR = {
		religion = religion:catholic_religion
		religion = religion:protestant_religion
		religion = religion:eastern_orthodox_religion
		religion = religion:oriental_orthodox_religion
	}
	OR = {
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_monastic_holy_order
	}
}
pam_is_eastern_christian_trigger = {
	OR = {
		religion = religion:eastern_orthodox_religion
		religion = religion:oriental_orthodox_religion
	}
	rite = {
		rite_has_doctrine = special_doctrine_is_eastern_christian_faith
	}
}
pam_is_western_christian_trigger = {
	OR = {
		religion = religion:catholic_religion
		religion = religion:protestant_religion
	}
	rite = {
		rite_has_doctrine = special_doctrine_is_western_christian_faith
	}
}

# Character scope. The College is a faith-locked doctrine (Catholicism).
pam_can_hold_cardinalate_trigger = {
	faith ?= { has_doctrine_parameter = has_clerical_electors }
	trigger_if = {
		limit = { NOT = { has_global_variable = pam_great_schism_decision_taken } }
		NOT = { has_title = title:d_et_constantinople }
	}
}

# Character scope. Before the Great Schism, AI only chooses Western candidates.
pam_ai_cardinal_candidate_is_immersive_trigger = {
	OR = {
		has_global_variable = pam_great_schism_decision_taken
		rite = {
			NOT = { rite_has_doctrine = special_doctrine_is_eastern_christian_faith }
		}
	}
}

# Character scope. The seat carries no government of its own, so a lay ruler handed one stays lay.
pam_can_receive_cardinalate_trigger = {
	OR = {
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_holy_order
		is_ruler = no
	}
	pam_is_antipope_trigger = no
}

# Held title scope. Any faith's cardinalate - faith.any_clerical_elector_title only lists its own.
pam_title_is_clerical_elector_trigger = {
	holder.clerical_elector_title ?= this
}

pam_church_background_location_is_mediterranean_or_urban_trigger = {
	OR = {
		culture ?= { has_graphical_mediterranean_culture_group_trigger = yes }
		steppe_building_gfx_use_mediterranean_trigger = yes
		has_holding_type = city_holding
	}
}

# Grand cathedral is the highest church background
pam_church_event_background_is_grand_western_trigger = {
	#Gothic movement starts in 12th century
	current_year >= 1130
	trigger_if = {
		limit = {
			scope:activity.activity_location ?= {
			OR = {
				faith.religion = religion:catholic_religion
				faith.religion = religion:protestant_religion
				faith.religion = religion:eastern_orthodox_religion
				faith.religion = religion:oriental_orthodox_religion
			}
			}
		}
		scope:activity.activity_location.rite = {
			rite_has_doctrine = special_doctrine_is_western_christian_faith
		}
	}
	trigger_else_if = {
		limit = {
			exists = scope:activity.activity_host
		}
		scope:activity.activity_host.rite = {
			rite_has_doctrine = special_doctrine_is_western_christian_faith
		}
	}
	trigger_else = {
		location.rite ?= {
			rite_has_doctrine = special_doctrine_is_western_christian_faith
		}
	}
	OR = {
		AND = {
			this = faith.religious_head
			highest_held_title_tier >= tier_kingdom
			capital_county.faith ?= faith
		}
		domicile ?= { has_domicile_building_or_higher = ecclesiastical_domicile_main_03 }
		capital_county ?= {
			any_county_province = {
				has_building_with_flag = { flag = grand_cathedral_flag }
			}
		}
		holy_order ?= {
			num_leased_titles >= 12
		}
	}
}
#pam_church_event_background_is_grand_western_pre_gothic_trigger = {
#	#Gothic movement starts in 12th century
#	current_year < 1130
#	trigger_if = {
#		limit = {
#			scope:activity.activity_location ?= {
#				faith.religion = religion:christianity_religion
#			}
#		}
#		scope:activity.activity_location.rite = {
#			rite_has_doctrine = special_doctrine_is_western_christian_faith
#		}
#	}
#	trigger_else_if = {
#		limit = {
#			exists = scope:activity.activity_host
#		}
#		scope:activity.activity_host.rite = {
#			rite_has_doctrine = special_doctrine_is_western_christian_faith
#		}
#	}
#	trigger_else = {
#		location.rite ?= {
#			rite_has_doctrine = special_doctrine_is_western_christian_faith
#		}
#	}
#	OR = {
#		AND = {
#			this = faith.religious_head
#			highest_held_title_tier >= tier_kingdom
#			capital_county.faith ?= faith
#		}
#		domicile ?= { has_domicile_building_or_higher = ecclesiastical_domicile_main_03 }
#		capital_county ?= {
#			any_county_province = {
#				has_building_with_flag = { flag = grand_cathedral_flag }
#			}
#		}
#		holy_order ?= {
#			num_leased_titles >= 12
#		}
#	}
#}

#For using the Hagia Sophia background
pam_church_event_background_is_grand_eastern_trigger = {
	trigger_if = {
		limit = {
			scope:activity.activity_location ?= {
				OR = {
					faith.religion = religion:catholic_religion
					faith.religion = religion:protestant_religion
					faith.religion = religion:eastern_orthodox_religion
					faith.religion = religion:oriental_orthodox_religion
				}
			}
		}
		scope:activity.activity_location.rite = {
			rite_has_doctrine = special_doctrine_is_eastern_christian_faith
		}
	}
	trigger_else_if = {
		limit = {
			exists = scope:activity.activity_host
		}
		scope:activity.activity_host.rite = {
			rite_has_doctrine = special_doctrine_is_eastern_christian_faith
		}
	}
	trigger_else = {
		location.rite ?= {
			rite_has_doctrine = special_doctrine_is_eastern_christian_faith
		}
	}
	OR = {
		AND = {
			this = faith.religious_head
			highest_held_title_tier >= tier_kingdom
			capital_county.faith ?= faith
		}
		domicile ?= { has_domicile_building_or_higher = ecclesiastical_domicile_main_03 }
		capital_county ?= {
			any_county_province = {
				has_building_with_flag = { flag = grand_cathedral_flag }
			}
		}
		holy_order ?= {
			num_leased_titles >= 12
		}
	}
}


# This grandeur check does not care about religion, so the call site checks religion
pam_church_event_background_is_developed_trigger = {
	OR = {
		domicile ?= { has_domicile_building_or_higher = ecclesiastical_domicile_main_02 }
		capital_county ?= {
			OR = {
				this = title:c_ile_de_france
				this = title:c_antiocheia
				any_county_province = {
					OR = {
						has_building_or_higher = temple_02
						has_building_with_flag = holy_building
						#A wooden church looks wrong here
						geographical_region = world_middle_east
						geographical_region = world_africa
						geographical_region = world_steppe_tarim
						geographical_region = world_steppe_central
					}
				}
			}
		}
		AND = {
			highest_held_title_tier >= tier_duchy
			this = rite.head_of_rite
		}
		holy_order ?= {
			OR = {
				num_leased_titles >= 5
				holy_order_patron.highest_held_title_tier >= tier_empire
			}
		}
	}
}

# Khachkar is removed when a county leaves the commissioning dynasty (like runestones).
pam_remove_khachkar_new_holder_trigger = {
	$TITLE$ = {
		exists = var:pam_khachkar_ancestor
		has_county_modifier = pam_khachkar_ancestral_county
	}
	$NEW_HOLDER$ = {
		dynasty != $PREVIOUS_HOLDER$.dynasty
	}
	$TITLE$ = {
		NOT = { var:pam_khachkar_ancestor.dynasty = $NEW_HOLDER$.dynasty }
	}
}

# Cheap pre-filter for HoF split secular ruler lists (since split_clerical_region_size might not be performant)
pam_potential_secular_clerical_split_ruler_trigger = {
	$RULER$ = {
		is_alive = yes
		is_landed = yes
		faith = scope:actor.faith
		NOR = {
			this = scope:recipient
			trigger_if = {
				limit = { exists = scope:recipient.liege }
				this = scope:recipient.liege
			}
		}
		highest_held_title_tier >= tier_duchy
		NOT = { pam_government_is_religious_playable_trigger = yes }
		capital_county = {
			exists = clerical_region_title
			clerical_region_title.holder = scope:recipient
			# We always keep the region capital, because splitting it off would leave the new region with no capital
			NOT = { this = clerical_region_title.title_preferred_capital_county }
		}
	}
	# any_sub_realm_county, not any_realm_county: for a vassal the latter is the liege's whole realm
	trigger_if = {
		limit = { scope:recipient = scope:actor }
		$RULER$ = {
			NOT = {
				any_sub_realm_county = {
					any_neighboring_county = { this = scope:recipient.capital_county }
				}
			}
		}
	}
}

# Full validation including code-side split size calc — use sparingly
pam_valid_secular_clerical_split_ruler_trigger = {
	pam_potential_secular_clerical_split_ruler_trigger = { RULER = $RULER$ }
	$RULER$ = {
		split_clerical_region_size > 0
	}
}

# This has to be the region at their capital, which is what the clerical_region pickers list
# Hides the interaction when a landed archbishop's capital sits in someone else's region
pam_acting_clerical_region_trigger = {
	$ARCHBISHOP$ = {
		save_temporary_scope_as = acting_archbishop_temp
		custom_tooltip = {
			text = pam_acting_clerical_region_trigger
			capital_province.county.clerical_region_title ?= {
				has_clerical_region = yes
				is_head_of_faith = no
				holder ?= scope:acting_archbishop_temp
			}
		}
	}
}

# Cheap pre-filter for HoF merge clerical region title list
pam_potential_merge_clerical_region_title_trigger = {
	$TITLE$ = {
		has_clerical_region = yes
		is_head_of_faith = no
		# Absorbed regions stay as empty picker shells until reload
		clerical_region_size > 0
		custom_tooltip = {
			text = pam_potential_merge_clerical_region_title_same_title_trigger
			this != $ABSORBING_TITLE$
		}
		trigger_if = {
			limit = { exists = holder }
			custom_tooltip = {
				text = request_elevation_cannot_unseat_player_tt
				holder = { is_ai = yes }
			}
			custom_tooltip = {
				text = pam_potential_merge_clerical_region_title_rite_trigger
				NOT = { rite.head_of_rite ?= holder }
			}
			custom_tooltip = {
				text = pam_potential_merge_clerical_region_title_same_hof_trigger
				holder.religious_head_or_challenger = $ABSORBING_TITLE$.holder.religious_head_or_challenger
			}
		}
		trigger_else = {
			custom_tooltip = {
				text = pam_potential_merge_clerical_region_title_no_holder_trigger
				exists = holder
			}
		}
	}
	$ABSORBING_TITLE$ ?= {
		has_clerical_region = yes
		is_head_of_faith = no
		custom_tooltip = {
			text = pam_potential_merge_clerical_region_title_neighboring_trigger
			any_county_in_clerical_region = {
				any_neighboring_county = { clerical_region_title ?= $TITLE$ }
			}
		}
	}
}

# True when the scoped clerical region title covers at most three distinct de jure duchies (TIT-76192)
pam_clerical_region_at_most_three_duchies_trigger = {
	NAND = {
		any_county_in_clerical_region = {
			save_temporary_scope_as = small_region_county_1
		}
		any_county_in_clerical_region = {
			NOT = { duchy = scope:small_region_county_1.duchy }
			save_temporary_scope_as = small_region_county_2
		}
		any_county_in_clerical_region = {
			NOR = {
				duchy = scope:small_region_county_1.duchy
				duchy = scope:small_region_county_2.duchy
			}
			save_temporary_scope_as = small_region_county_3
		}
		any_county_in_clerical_region = {
			NOR = {
				duchy = scope:small_region_county_1.duchy
				duchy = scope:small_region_county_2.duchy
				duchy = scope:small_region_county_3.duchy
			}
		}
	}
}

pam_has_adjacent_mergeable_clerical_region_trigger = {
	custom_tooltip = {
		text = pam_has_adjacent_mergeable_clerical_region_trigger
		save_temporary_scope_as = absorbing_clerical_region_temp
		any_county_in_clerical_region = {
			any_neighboring_county = {
				clerical_region_title ?= {
					has_clerical_region = yes
					is_head_of_faith = no
					this != scope:absorbing_clerical_region_temp
					holder ?= {
						religious_head_or_challenger = scope:absorbing_clerical_region_temp.holder.religious_head_or_challenger
						NOT = { this = rite.head_of_rite }
					}
				}
			}
		}
	}
}

# Same-faith land in another de jure kingdom
pam_expand_target_is_same_faith_outside_home_kingdom_trigger = {
	exists = $TITLE$.kingdom
	exists = $ARCHBISHOP$.capital_county.kingdom
	NOT = { $TITLE$.kingdom = $ARCHBISHOP$.capital_county.kingdom }
	$TITLE$ = {
		any_de_jure_county = {
			count >= all
			OR = {
				exists = clerical_region_title
				faith = $ARCHBISHOP$.faith
			}
		}
	}
}

# Picked title only chooses the see, then create_clerical_region fills from that county's de jure kingdom
# The lowest tier decides who may petition, not how large the region ends up
pam_valid_new_clerical_region_anchor_trigger = {
	trigger_if = {
		limit = { $PETITIONER$ ?= { has_government = ecclesiastical_government } }
		tier >= tier_county
	}
	trigger_else = {
		tier >= tier_duchy
	}
	is_landless_type_title = no
	is_head_of_faith = no
	exists = title_capital_county
	exists = title_capital_county.kingdom
	title_capital_county = {
		NOT = { exists = clerical_region_title }
		exists = holder
		faith = $PETITIONER$.faith # the region takes its faith from this county, and a mismatch destroys the new title
		OR = {
			holder = $PETITIONER$
			holder = {
				any_liege_or_above = {
					this = $PETITIONER$
				}
			}
		}
	}
}

# At least one eligible title exists
pam_valid_expand_ecclesiastical_title_target_trigger = {
	$ARCHBISHOP$ = { save_temporary_scope_as = archbishop_temp }
	$TITLE$ = {
		tier = tier_duchy
		is_landless_type_title = no
		is_head_of_faith = no
		custom_tooltip = {
			text = pam_valid_expand_ecclesiastical_title_de_jure_county_faith_trigger
			OR = {
				holder.faith ?= $ARCHBISHOP$.faith
				any_de_jure_county = {
					count = all
					holder.faith ?= $ARCHBISHOP$.faith
				}
			}
		}
		trigger_if = {
			limit = {
				any_de_jure_county = {
					NOT = { exists = clerical_region_title }
				}
			}
			scope:archbishop_temp = {
				custom_tooltip = {
					text = pam_valid_expand_ecclesiastical_title_adjacent_to_clerical_region_trigger
					capital_province.county.clerical_region_title ?= {
						has_clerical_region = yes
						is_head_of_faith = no
						holder ?= scope:archbishop_temp
						is_adjacent_to_clerical_region = $TITLE$ # very unperformant
					}
				}
			}
		}
		trigger_else = {
			custom_tooltip = {
				text = pam_valid_expand_ecclesiastical_title_clerical_region_exists_trigger
				always = no
			}
		}
	}
}

# At least one selectable expand target exists
pam_has_eligible_expand_ecclesiastical_title_target_trigger = {
	save_temporary_scope_as = archbishop_temp
	capital_county.clerical_region_title = { # must work like this due to clerical_region_adjacent_duchies behaviour
		has_clerical_region = yes
		is_head_of_faith = no
		holder ?= scope:archbishop_temp
		save_temporary_scope_as = clerical_region_title_temp
		any_county_in_clerical_region = {
			any_neighboring_county = {
				NOT = { clerical_region_title ?= scope:clerical_region_title_temp }
				duchy = {
					save_temporary_scope_as = duchy_temp
					pam_valid_expand_ecclesiastical_title_target_trigger = { TITLE = scope:duchy_temp ARCHBISHOP = scope:archbishop_temp }
				}
			}
		}
	}
}

pam_can_expand_clerical_region_after_war_trigger = {
	exists = scope:target
	#You hold a clerical region that neighbors or is within target
	save_temporary_scope_as = archbishop_temp
	any_held_title = {
		has_clerical_region = yes
		save_temporary_scope_as = clerical_region_title_temp
		any_county_in_clerical_region = {
			trigger_if = {
				limit = {
					scope:target = {
						tier = tier_county
					}
				}
				any_neighboring_county = {
					this = scope:target
					NOT = { exists = clerical_region_title }
				}
				#The rest of the duchy needs to be ready
				scope:target = {
					duchy = {
						#All counties must have no clerical region
						#All counties must be within recipient's realm OR held by a same-faith ruler
						any_de_jure_county = {
							count >= all
							NOT = { exists = clerical_region_title }
							OR = {
								this = scope:target
								NOT = {
									holder = {
										faith = scope:puppet_or_actor.faith
									}
								}
							}
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					scope:target = {
						tier = tier_duchy
					}
				}
				any_neighboring_county = {
					duchy = scope:target
					NOT = { exists = scope:clerical_region_title }
				}
				scope:target = {
					#All counties must have no clerical region
					#All counties must be within recipient's realm OR held by a same-faith ruler
					any_de_jure_county = {
						count >= all
						NOT = { exists = clerical_region_title }
						holder = {
							OR = {
								this = $HEATHEN$
								target_is_liege_or_above = $HEATHEN$
								faith = scope:puppet_or_actor.faith
							}
						}
					}
				}
			}
			trigger_else = {
				any_neighboring_county = {
					kingdom = scope:target
					NOT = { exists = clerical_region_title }
					save_temporary_scope_as = county_temp
				}
				scope:target = {
					any_direct_de_jure_vassal_title = {
						#All counties must have no clerical region
						#All counties must be within recipient's realm OR held by a same-faith ruler
						any_de_jure_county = {
							count >= all
							NOT = {
								exists = clerical_region_title
							}
							holder = {
								OR = {
									this = $HEATHEN$
									target_is_liege_or_above = $HEATHEN$
									faith = scope:puppet_or_actor.faith
								}
							}
						}
						any_de_jure_county = {
							this = scope:county_temp
						}
					}
				}
			}
		}
	}
}

# Evaluated in county scope for the Convert Faith cardinal order.
pam_cardinal_order_convert_faith_in_region_county_trigger = {
	faith != $CARDINAL$.faith
	exists = holder
	holder.top_liege.faith = $CARDINAL$.faith
}

pam_cardinal_order_convert_faith_neighboring_county_trigger = {
	has_clerical_region = no
	faith != $CARDINAL$.faith
	exists = holder
	holder.top_liege.faith = $CARDINAL$.faith
}

pam_any_christian_holy_site_not_under_christian_control_trigger = {
	religion:catholic_religion = {
		any_holy_site_barony = {
			OR = {
				holder.top_liege.faith.religion != religion:catholic_religion
				holder.top_liege.faith.religion != religion:protestant_religion
				holder.top_liege.faith.religion != religion:eastern_orthodox_religion
				holder.top_liege.faith.religion != religion:oriental_orthodox_religion
			}
		}
	}
}

pam_any_muslim_holy_site_not_under_muslim_control_trigger = {
	religion:islam_religion = {
		any_faith = {
			any_holy_site_barony = {
				holder.top_liege.faith.religion != religion:islam_religion
			}
		}
	}
}

pam_is_the_christian_church_main_power_trigger = {
	top_participant_group:the_christian_church ?= {
		OR = {
			participant_group_type = christian_clerical_main_power_rulers
			participant_group_type = christian_regular_main_power_rulers
		}
	}
}

pam_ai_can_proclaim_armed_pilgrimages_core_trigger = {
	faith = {
		this = faith:catholic
		has_doctrine = doctrine_spiritual_head
		main_rite = {
			NOT = {
				has_tenet_status = {
					tenet = tenet_armed_pilgrimages
					status = core
				}
			}
		}
	}
}

pam_ai_can_proclaim_struggle_submission_core_trigger = {
	faith.religion = {
		this = religion:islam_religion
		exists = var:variable_ghw_unlocked # You've entered the Age of Crusades
	}
	faith = {
		main_rite = {
			NOT = {
				has_tenet_status = {
					tenet = tenet_struggle_submission
					status = core
				}
			}
		}
	}
}

divorce_is_justified_trigger = {
	OR = {
		relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = scope:secondary_recipient	}
		AND = {
			faith = scope:recipient.faith
			sex_same_as = scope:secondary_recipient
			allowed_to_marry_same_sex_trigger = no
		}
		scope:secondary_recipient = {
			has_character_modifier = wedding_accused_impotency_modifier
		}
		scope:secondary_recipient = {
			any_relation = {
				type = lover
				NOR = {
					this = $DIVORCER$
					this = scope:recipient
				}
				relation_with_character_is_incestuous_in_my_rite_trigger = { CHARACTER = scope:secondary_recipient	}
			}
		}
		scope:secondary_recipient = {
			exposed_cheating_on_spouse_trigger = { SPOUSE = $DIVORCER$ }
		}
		scope:secondary_recipient = {
			is_female = no
			is_eunuch_trigger = yes
		}
		AND = {
			any_consort = {
				is_alive = yes
				this != scope:secondary_recipient
			}
			any_consort = {
				this = scope:secondary_recipient
			}
			AND = {
				scope:secondary_recipient.culture = { NOT = { has_cultural_tradition = tradition_polygamous } }
				scope:secondary_recipient.rite = { NOT = { rite_has_doctrine = doctrine_polygamy } }
			}
		}
		scope:secondary_recipient.faith = {
			faith_hostility_level = {
				target = scope:recipient.faith
				value >= faith_hostile_level
			}
		}
		scope:secondary_recipient = {
			has_trait = adulterer
		}
		scope:secondary_recipient = {
			fertility < 0.1
			NOT = {
				any_child = {
					any_parent = {
						this = $DIVORCER$
					}
				}
			}
		}
		$DIVORCER$ = {
			has_opinion_modifier = {
				modifier = attempted_murder_me_crime
				target = scope:secondary_recipient
			}
		}
	}
}

amicable_infertile_divorce_trigger = {
	rite = { rite_has_parameter = tenet_be_fruitful_and_multiply_low_fertility_divorce }
	$DIVORCEE$ = {
		fertility < 0.1
	}
}

# Mendicant Preachers personal tenet: guests a preaching host can win over at an activity
pam_mendicant_preachers_apa_convert_trigger = {
	this != $PREACHER$
	highest_held_title_tier < tier_duchy
	is_physically_able_ai_adult = yes
	faith != $PREACHER$.faith
	NOT = { has_trait = zealous }
	opinion = {
		target = $PREACHER$
		value >= 0
	}
}

# PAM Reincarnation personal tenet: a parent or grandparent that root could claim as a past life
# Checked in the forebear's scope, with root as the character doing the remembering
pam_recallable_forebear_trigger = {
	save_temporary_scope_as = candidate_forebear
	is_alive = no
	OR = {
		root = { is_child_of = scope:candidate_forebear }
		root = { is_grandchild_of = scope:candidate_forebear }
	}
	any_character_trait = {
		has_trait_category = personality
		NOT = { root = { has_trait = prev } }
	}
}

divorce_can_cause_house_feud_trigger = {
	scope:recipient = {
		intrigue >= decent_skill_rating
	}
	scope:divorcer = {
		house.house_head = {
			trigger_if = {
				limit = {
					is_ai = no
				}
				OR = {
					this = scope:divorcer
					this = scope:actor
				}
			}
			OR = {
				this = scope:divorcer
				this = scope:actor
				is_close_family_of = scope:divorcer
			}
		}
	}
	scope:secondary_recipient = {
		house ?= {
			any_house_relation = {
				#Not already feuding or quite fond of one another
				OR = {
					has_house_relation_level = quarrel
					has_house_relation_level = rivalry
					has_house_relation_level = neutral
				}
				any_relation_house = {
					this = scope:divorcer.house
				}
			}
		}
		house.house_head = {
			trigger_if = {
				limit = {
					is_ai = no
				}
				this = scope:secondary_recipient
			}
			OR = {
				this = scope:secondary_recipient
				is_close_family_of = scope:secondary_recipient
			}
		}
	}
	scope:divorcer.house.house_head = { valid_for_feud_events_with_target_trigger = { TARGET = scope:secondary_recipient.house.house_head } }
}
divorce_can_cause_holy_order_lease_trigger = {
	scope:recipient = {
		diplomacy >= decent_skill_rating
	}
	OR = {
		#Monastic lease
		scope:divorcer = {
			any_held_title = {
				title_tier = county
				any_county_province = {
					barony = {
						barony_is_valid_for_monastic_order_lease_trigger = { CHARACTER = scope:divorcer }
					}
				}
			}
			faith = {
				any_faith_holy_order = {
					leader ?= {
						government_has_flag = government_is_monastic_holy_order
					}
					holy_order_range_trigger = { CHARACTER = scope:recipient }
				}
			}
		}
		#Military lease
		scope:divorcer = {
			any_held_title = {
				title_tier = county
				any_county_province = {
					barony = {
						barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = scope:divorcer }
					}
				}
			}
			faith = {
				any_faith_holy_order = {
					leader ?= { 
						government_has_flag = government_is_military_holy_order
					}
					holy_order_range_trigger = { CHARACTER = scope:recipient }
				}
			}
		}
	}
	
}
divorce_can_cause_hook_trigger = {
	scope:recipient = {
		trigger_if = {
			limit = {
				exists = scope:forcing_divorce
			}
			NOT = {
				has_hook = scope:forcing_divorce
			}
		}
		trigger_else = {
			NOT = {
				has_hook = scope:divorcer
			}
		}
	}
}
divorce_can_put_spouse_in_monastery_trigger = {
	scope:secondary_recipient = {
		NOR = {
			has_trait = devoted
			has_trait = order_member
		}
		is_ai = yes
		is_ruler = no
		faith = {
			this = scope:recipient.faith
			any_faith_holy_order = {
				leader ?= {
					government_has_flag = government_is_monastic_holy_order
					trigger_if = {
						limit = {
							scope:secondary_recipient = {
								is_male = yes
							}
						}
						NOT = {
							has_realm_law = female_monastic_holy_order_succession_law
						}
					}
					trigger_else = {
						NOT = {
							has_realm_law = male_monastic_holy_order_succession_law
						}
					}
				}
				holy_order_range_trigger = { CHARACTER = scope:recipient }
			}
		}
	}
}

# Would a priest consider marriage in Saeculum Obscurum?
saeculum_obscurum_clergy_would_marry_trigger = {
	# Older priests just don't bother
	age < 50
	# Must be somewhat inclined
	NOR = {
		has_trait = chaste
		has_trait = celibate
	}
	OR = {
		# Strict personality/actions
		has_trait = lustful
		has_trait = seducer
		has_trait = rakish
		any_relation = { type = lover }
		is_deviant_trigger = yes
		rite = { trait_is_virtue_rite = lustful }
		any_personal_tenet = {
			OR = {
				this = tenet:tenet_carnal_exaltation
				this = tenet:tenet_hedonistic
				this = tenet:tenet_polyamory
				this = tenet:tenet_sacred_childbirth
				this = tenet:tenet_be_fruitful_and_multiply
			}
		}
		# General vibes
		saeculum_obscurum_clergy_marry_ai_value_trigger = yes
	}
}

saeculum_obscurum_would_marry_clergy_trigger = {
	# General vibes
	saeculum_obscurum_clergy_marry_ai_value_trigger = yes
}

saeculum_obscurum_clergy_marry_ai_value_trigger = {
	# Higher tiers need less honor and zeal, and more boldness
	trigger_if = {
		limit = { highest_held_title_tier > tier_duchy }
		ai_honor < low_negative_ai_value
		ai_zeal < low_negative_ai_value
		ai_boldness > high_positive_ai_value
	}
	trigger_if = {
		limit = { highest_held_title_tier = tier_duchy }
		ai_honor < medium_positive_ai_value
		ai_zeal < medium_positive_ai_value
		ai_boldness > low_positive_ai_value
	}
	trigger_else = {
		ai_honor < high_positive_ai_value
		ai_zeal < high_positive_ai_value
		ai_boldness > medium_negative_ai_value
	}
}

is_in_same_religious_family_trigger = {
	OR = {
		AND = {
			is_in_family = rf_abrahamic
			$FAITH$ = { is_in_family = rf_abrahamic }
		}
		AND = {
			is_in_family = rf_eastern
			$FAITH$ = { is_in_family = rf_eastern }
		}
		AND = {
			is_in_family = rf_sinitic
			$FAITH$ = { is_in_family = rf_sinitic }
		}
		AND = {
			is_in_family = rf_pagan
			$FAITH$ = { is_in_family = rf_pagan }
		}
		AND = {
			is_in_family = rf_esoteric
			$FAITH$ = { is_in_family = rf_esoteric }
		}
		AND = {
			is_in_family = rf_antediluvianist
			$FAITH$ = { is_in_family = rf_antediluvianist }
		}
		AND = {
			is_in_family = rf_iranic
			$FAITH$ = { is_in_family = rf_iranic }
		}
	}
}

can_request_priest_from_recipient_trigger = {
	OR = {
		cp:councillor_court_chaplain ?= {
			OR = {
				this = $RECIPIENT$
				superior ?= $RECIPIENT$
				superior.superior ?= $RECIPIENT$
			}
		}
		# A realm under an antipope asks them instead
		religious_head_or_challenger ?= $RECIPIENT$
		trigger_if = {
			limit = { NOT = { exists = faith.religious_head } }
			faith.main_rite.head_of_rite ?= $RECIPIENT$
		}
	}
}

# Gates the tenet popularity system, for PAM it's only for Christian faiths. Runs in a faith scope.
has_tenet_popularity_trigger = {
	OR = {
		religion = religion:catholic_religion
		religion = religion:protestant_religion
		religion = religion:eastern_orthodox_religion
		religion = religion:oriental_orthodox_religion
	}
}
has_papacy_trigger = {
	religion = religion:catholic_religion
}
valid_holy_war_incited_attacker_trigger = {
	highest_held_title_tier >= tier_county
	is_at_war = no
	is_landed = yes
	gold > 0

	is_available_quick = {
		alive = yes
		imprisoned = no
		incapable = no
	}
	current_military_strength >= $RECIPIENT$.one_tenth_current_military_strength
	NOR = {
		government_has_flag = government_is_landless_adventurer
		government_has_flag = government_is_nomadic
		government_has_flag = government_is_celestial
		government_has_flag = government_is_theocracy
		this = scope:puppet_or_actor
		this = $RECIPIENT$
		is_allied_to = $RECIPIENT$
		has_truce = $RECIPIENT$
		is_tributary_of = $RECIPIENT$.top_liege
		is_tributary_of = $RECIPIENT$
		top_liege = $RECIPIENT$.top_liege
		liege = $RECIPIENT$
		puppet:theological_agent_puppet ?= scope:puppet_or_actor
		puppet:external_theological_agent_puppet ?= scope:puppet_or_actor
	}
	NOT = {
		any_liege_or_above = {
			is_at_war_with = $RECIPIENT$
		}
	}
	any_character_to_title_neighboring_and_across_water_county = {
		OR = {
			holder = $RECIPIENT$
			holder.top_liege = $RECIPIENT$
		}
	}
	tgp_japan_offensive_wars_ban_trigger = yes
	$RECIPIENT$ = {
		save_temporary_scope_as = defender
	}
	save_temporary_scope_as = attacker
	trigger_if = {
		limit = {
			scope:attacker = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = iberian_struggle
				}
			}
			scope:defender = {
				any_character_struggle = {
					involvement = involved
					is_struggle_type = iberian_struggle
				}
			}
		}
		are_holy_wars_disabled_in_struggle_phase_trigger = yes
	}
}
pam_clergy_title_interaction_player_recipient_auto_accept_trigger = {
	scope:secondary_recipient = { save_temporary_scope_as = cleric }
	scope:recipient = { save_temporary_scope_as = owner }
	scope:recipient = {
		is_ai = no
		scope:puppet_or_actor = {
			OR = {
				puppet:theological_agent_puppet ?= scope:secondary_recipient
				ai_zeal >= 75
				has_trait = saint
				has_trait = herald
				has_trait = saoshyant_descendant
				has_trait = order_member
				has_trait = faith_warrior
				has_trait = saoshyant
				has_trait = paragon
				has_trait = savior
				has_trait = crusader_king
				reverse_opinion = {
					target = scope:secondary_recipient
					value >= 70
				}
				scope:recipient = {
					reverse_opinion = {
						target = scope:secondary_recipient
						value <= -70
					}
				}
				is_close_family_of = scope:secondary_recipient
				has_perk = sanctioned_loopholes_perk
				scope:use_hook = yes
				AND = {
					scope:offer_hook = yes
					highest_held_title_tier >= tier_kingdom
				}
				AND = {
					scope:donate_faith = yes
					medium_gold_value >= scope:secondary_recipient.medium_gold_value
				}
				AND = {
					highest_held_title_tier >= tier_duchy
					scope:offer_pilgrimage = yes
				}
				AND = {
					scope:threaten_priest = yes
					scope:puppet_or_actor = {
						OR = {
							highest_held_title_tier > scope:secondary_recipient.highest_held_title_tier
							is_liege_or_above_of = scope:secondary_recipient
							scope:secondary_recipient = {
								any_held_title = {
									is_neighbor_to_realm = scope:puppet_or_actor
								}
							}
							scope:secondary_recipient.capital_county.holder ?= {
								OR = {
									this = scope:puppet_or_actor
									this = scope:recipient
									any_liege_or_above = {
										this = scope:puppet_or_actor
									}
								}
							}
							liege ?= scope:secondary_recipient.liege
						}
					}
				}
				AND = {
					exists = scope:target.holder
					clerical_hesitation_from_target_holder_value <= -2
				}
				AND = {
					scope:recipient = {
						is_independent_ruler = no
						NOT = {
							top_liege = scope:puppet_or_actor.top_liege
						}
					}
					clerical_hesitation_from_potential_defender_value <= -2
				}
				situation:the_christian_church ?= {
					any_participant_group = { # Both characters are involved in the situation
						any_situation_group_participant = {
							this = scope:puppet_or_actor
						}
						trigger_if = {
							limit = { scope:recipient = { is_ruler = yes } }
							any_situation_group_participant = {
								this = scope:recipient
							}
						}
					}
					has_situation_top_phase_parameter = claim_seeking_acceptance_bonus
				}
				scope:secondary_recipient = { has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:recipient } }
				has_any_good_relationship_with_character_trigger = { CHARACTER = scope:secondary_recipient }
				scope:secondary_recipient = {
					has_dread_level_towards = {
						target = scope:puppet_or_actor
						level >= 2
					}
					highest_held_title_tier <= scope:puppet_or_actor.highest_held_title_tier
				}
				scope:recipient = { character_considered_wicked_trigger = { RITE = scope:secondary_recipient.rite } }
				AND = {
					rite.head_of_rite ?= scope:actor
					rite = scope:secondary_recipient.rite
				}
			}
			NOR = {
				scope:recipient = {
					reverse_opinion = {
						target = scope:secondary_recipient
						value >= 80
					}
				}
				scope:secondary_recipient = {
					has_dread_level_towards = {
						target = scope:recipient
						level >= 2
					}
					OR = {
						top_liege = scope:recipient.top_liege
						character_is_realm_neighbor = scope:recipient
					}
				}
				scope:secondary_recipient = { has_any_good_relationship_with_character_trigger = { CHARACTER = scope:recipient } }
				has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:secondary_recipient }
				AND = {
					exists = scope:target.holder
					clerical_hesitation_from_target_holder_value >= 3
				}
				AND = {
					scope:recipient = {
						is_independent_ruler = no
						NOT = {
							top_liege = scope:puppet_or_actor.top_liege
						}
					}
					clerical_hesitation_from_potential_defender_value >= 3
				}
				AND = {
					NOT = { rite = scope:secondary_recipient.rite }
					any_tenet = {
						scope:puppet_or_actor.rite = {
							has_tenet_status = {
								tenet = prev
								status = core
							}
						}
						scope:secondary_recipient.rite = {
							has_tenet_status = {
								tenet = prev
								status = prohibited
							}
						}
					}
				}
				trigger_if = {
					limit = {
						highest_held_title_tier >= tier_hegemony
					}
					sub_realm_size >= 1500
				}
				trigger_else_if = {
					limit = {
						highest_held_title_tier = tier_empire
					}
					sub_realm_size >= 300
				}
				trigger_else_if = {
					limit = {
						highest_held_title_tier = tier_kingdom
					}
					sub_realm_size >= 120
				}
				trigger_else_if = {
					limit = {
						highest_held_title_tier = tier_duchy
					}
					sub_realm_size >= 18
				}
				trigger_else = {
					sub_realm_size >= 10
				}
				has_realm_law = uncrowned
				AND = {
					exists = scope:target
					scope:secondary_recipient = {
						any_claim = {
							this = scope:target
						}
					}
				}
				AND = {
					scope:recipient.rite = scope:secondary_recipient.rite
					scope:recipient.rite = {
						rite_counties <= 10
					}
				}
				AND = {
					exists = scope:secondary_recipient.house
					scope:recipient.house ?= scope:secondary_recipient.house
					NOT = { house ?= scope:secondary_recipient.house }
				}
				scope:secondary_recipient = {
					is_allied_to = scope:recipient
				}
				#Must be neighboring or overlapping
				scope:target ?= {
					NOR = {
						any_de_jure_county = {
							is_neighbor_to_realm = scope:actor
						}
						any_de_jure_county = {
							OR = {
								holder = scope:actor
								holder.top_liege = scope:actor
							}
						}
						any_in_de_jure_hierarchy = {
							holder ?= scope:actor
						}
						any_this_title_or_de_jure_above = {
							holder ?= scope:actor
						}
					}
				}
			}
		}
	}
}
# PAM Peace of God: is prosecuting an offensive war against a ruler of their own faith.
pam_peace_of_god_coreligionist_aggressor_trigger = {
	save_temporary_scope_as = pam_aggressor
	any_character_war = {
		is_attacker = scope:pam_aggressor
		primary_defender ?= { faith = scope:pam_aggressor.faith }
	}
}

pam_valid_excommunication_target_trigger = {
	age >= 12
	save_temporary_scope_as = target_char
	$EXCOMMUNICATOR$ = { save_temporary_scope_as = excommunicator }
	trigger_if = {
		limit = { has_title = title:k_papal_state }
		custom_tooltip = {
			text = cannot_excommunicate_pope_without_patriarch_or_antipope_tt
			liege ?= $EXCOMMUNICATOR$
			$EXCOMMUNICATOR$ = {
				OR = {
					pam_holds_christian_patriarch_title_trigger = yes
					pam_is_antipope_trigger = yes
				}
			}
		}
	}
	#always valid if Hof
	#This trigger is only relevant if excommunicator is not
	trigger_if = {
		limit = {
			scope:target_char = { need_hof_for_clergy_interaction_trigger = yes }
			scope:target_char.faith = {
				has_doctrine = doctrine_spiritual_head
				exists = scope:target_char.religious_head_or_challenger
			}
		}
		custom_tooltip = {
			text = excommunicator_must_be_hof_tt
			scope:excommunicator = scope:target_char.religious_head_or_challenger
		}
	}
	trigger_else_if = {
		limit = {
			scope:target_char.religious_head_or_challenger = scope:excommunicator
		}
	}
	trigger_else = {
		# If there is no HoF, a clergy HoR can take it
		trigger_if = {
			limit = {
				NOT = { exists = scope:target_char.faith.religious_head }
				scope:target_char.rite.head_of_rite ?= $EXCOMMUNICATOR$
			}
		}
		#Wanderers and courtiers
		trigger_else_if = {
			limit = {
				is_ruler = no
			}
			#Courtiers
			trigger_if = {
				limit = {
					exists = liege
				}
				custom_tooltip = {
					text = bishop_excommunication_target_courtier_tt
					liege = {
						OR = {
							target_is_same_character_or_above = $EXCOMMUNICATOR$
							AND = {
								is_landed = yes
								any_character_to_title_neighboring_county = {
									holder ?= {
										target_is_same_character_or_above = $EXCOMMUNICATOR$
									}
								}
							}
							capital_province.county = {
								clerical_region_title ?= {
									holder = $EXCOMMUNICATOR$
								}
							}
							cp:councillor_court_chaplain ?= $EXCOMMUNICATOR$
							any_liege_or_above = {
								cp:councillor_court_chaplain ?= $EXCOMMUNICATOR$
							}
						}
					}
				}
			}
			#Wanderers
			trigger_else = {
				custom_tooltip = {
					text = bishop_excommunication_target_wanderer_tt
					location.county = {
						OR = {
							holder ?= {
								target_is_same_character_or_above = $EXCOMMUNICATOR$
							}
							any_neighboring_county = {
								holder ?= {
									target_is_same_character_or_above = $EXCOMMUNICATOR$
								}
							}
							clerical_region_title ?= {
								holder = $EXCOMMUNICATOR$
							}
							holder.cp:councillor_court_chaplain ?= $EXCOMMUNICATOR$
							holder = {
								any_liege_or_above = {
									cp:councillor_court_chaplain ?= $EXCOMMUNICATOR$
								}
							}
						}
					}
				}
			}
		}
		#Theocrats/ecclesiasticals
		trigger_else_if = {
			limit = {
				government_has_flag = government_is_theocracy
			}
			custom_tooltip = {
				text = target_char_must_be_clerical_subordinate_tt
				OR = {
					$EXCOMMUNICATOR$ = {
						any_subordinate = {
							OR = {
								this = scope:target_char
								any_subordinate = {
									OR = {
										this = scope:target_char
										any_subordinate = {
											OR = {
												this = scope:target_char
												any_subordinate = {
													this = scope:target_char
												}
											}
										}
									}
								}
							}
						}
					}
					target_is_liege_or_above = $EXCOMMUNICATOR$
				}
				
			}
		}
		#Secular rulers
		trigger_else = {
			custom_tooltip = {
				text = bishop_excommunication_target_secular_tt
				OR = {
					$EXCOMMUNICATOR$ = {
						exists = scope:target_char.cp:councillor_court_chaplain
						OR = {
							AND = {
								this = scope:target_char.cp:councillor_court_chaplain
								NOT = {
									exists = superior
								}
							}
							any_subordinate = {
								OR = {
									this = scope:target_char.cp:councillor_court_chaplain
									any_subordinate = {
										OR = {
											this = scope:target_char.cp:councillor_court_chaplain
											any_subordinate = {
												OR = {
													this = scope:target_char.cp:councillor_court_chaplain
													any_subordinate = {
														this = scope:target_char.cp:councillor_court_chaplain
													}
												}
											}
										}
									}
								}
							}
						}
					}
					target_is_liege_or_above = $EXCOMMUNICATOR$
					AND = {
						is_landed = yes
						any_character_to_title_neighboring_county = {
							holder ?= {
								target_is_same_character_or_above = $EXCOMMUNICATOR$
							}
						}
					}
					capital_county ?= {
						clerical_region_title ?= {
							holder = $EXCOMMUNICATOR$
						}
					}
					any_liege_or_above = {
						cp:councillor_court_chaplain ?= $EXCOMMUNICATOR$
					}
				}
			}
		}
	}
}
actor_incite_holy_war_shown_trigger = {
	is_ruler = yes
	OR = {
		government_has_flag = government_is_ecclesiastical
		this = religious_head_or_challenger
	}
	faith = {
		NOR = {
			has_doctrine_parameter = holy_wars_forbidden
			has_doctrine_parameter = unreformed
		}
	}
}
incite_holy_war_title_can_be_picked_trigger = {
	tier <= tier_kingdom
	tier >= tier_county
	is_landless_type_title = no
	any_in_de_jure_hierarchy = {
		tier = tier_county
		holder ?= {
			target_is_same_character_or_above = $RECIPIENT$
		}
	}
	trigger_if = {
		limit = {
			exists = $SECONDARY$
		}
		any_in_de_jure_hierarchy = {
			tier = tier_county
			holder ?= {
				target_is_same_character_or_above = $RECIPIENT$
			}
			OR = {
				holder.top_liege = $SECONDARY$.top_liege
				any_title_to_title_neighboring_and_across_water_county = {
					holder.top_liege = $SECONDARY$.top_liege
				}
			}
		}
	}
	trigger_if = {
		limit = {
			tier >= tier_kingdom
		}
		custom_tooltip = {
			text = piety_too_low_target_kingdom_tt
			scope:puppet_or_actor = { piety_level >= 4 }
		}
	}
	trigger_if = {
		limit = {
			tier >= tier_duchy
		}
		custom_tooltip = {
			text = piety_too_low_target_duchy_tt
			scope:puppet_or_actor = { piety_level >= 3 }
		}
	}
	trigger_if = {
		limit = {
			scope:puppet_or_actor = {
				is_at_war_with = $RECIPIENT$
			}
		}
		custom_tooltip = {
			text = not_at_war_for_target_title_or_above_tt
			scope:puppet_or_actor = {
				NOT = {
					any_character_war = {
						casus_belli = {
							any_target_title = {
								OR = {
									any_this_title_or_de_jure_above = {
										this = scope:target
									}
									any_in_de_jure_hierarchy = {
										this = scope:target
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

valid_claim_war_incited_attacker_trigger = {
	faith = scope:puppet_or_actor.faith
	highest_held_title_tier >= tier_county
	is_at_war = no
	is_landed = yes
	gold > 0

	is_available_quick = {
		alive = yes
		imprisoned = no
		incapable = no
	}
	current_military_strength >= $RECIPIENT$.one_tenth_current_military_strength
	NOR = {
		government_has_flag = government_is_landless_adventurer
		government_has_flag = government_is_nomadic
		government_has_flag = government_is_celestial
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_ecclesiastical
		this = scope:puppet_or_actor
		this = $RECIPIENT$
		is_allied_to = $RECIPIENT$
		has_truce = $RECIPIENT$
		is_tributary_of = $RECIPIENT$.top_liege
		is_tributary_of = $RECIPIENT$
		top_liege = $RECIPIENT$.top_liege
		liege = $RECIPIENT$
		puppet:theological_agent_puppet ?= scope:puppet_or_actor
		puppet:external_theological_agent_puppet ?= scope:puppet_or_actor
	}
	NOT = {
		any_liege_or_above = {
			is_at_war_with = $RECIPIENT$
		}
	}
	any_claim = {
		tier >= tier_county
		tier <= tier_kingdom
		is_landless_type_title = no
		holder ?= {
			target_is_same_character_or_above = $RECIPIENT$
		}
	}
	tgp_japan_offensive_wars_ban_trigger = yes
}
claim_war_incited_attacker_can_be_picked_trigger = {
	custom_description = {
		text = incite_claim_war_attacker_must_share_faith_tt
		faith = scope:puppet_or_actor.faith
	}
	custom_description = {
		text = incite_claim_war_attacker_must_be_landed_count_tt
		is_landed = yes
		highest_held_title_tier >= tier_county
	}
	custom_description = {
		text = incite_claim_war_attacker_must_not_be_at_war_tt
		is_at_war = no
	}
	custom_description = {
		text = incite_claim_war_attacker_must_not_be_in_debt_tt
		gold > 0
	}
	is_available_quick = {
		alive = yes
		imprisoned = no
		incapable = no
	}
	custom_description = {
		text = incite_claim_war_attacker_must_be_strong_enough_tt
		current_military_strength >= $RECIPIENT$.one_tenth_current_military_strength
	}
	custom_description = {
		text = incite_claim_war_attacker_must_be_secular_tt
		NOR = {
			government_has_flag = government_is_landless_adventurer
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_celestial
			government_has_flag = government_is_theocracy
			government_has_flag = government_is_ecclesiastical
		}
	}
	custom_description = {
		text = incite_claim_war_attacker_must_not_be_blocked_diplomacy_tt
		NOR = {
			this = scope:puppet_or_actor
			this = $RECIPIENT$
			is_allied_to = $RECIPIENT$
			has_truce = $RECIPIENT$
			is_tributary_of = $RECIPIENT$.top_liege
			is_tributary_of = $RECIPIENT$
			top_liege = $RECIPIENT$.top_liege
			liege = $RECIPIENT$
			puppet:theological_agent_puppet ?= scope:puppet_or_actor
			puppet:external_theological_agent_puppet ?= scope:puppet_or_actor
		}
	}
	custom_description = {
		text = incite_claim_war_attacker_liege_not_at_war_tt
		NOT = {
			any_liege_or_above = {
				is_at_war_with = $RECIPIENT$
			}
		}
	}
	custom_description = {
		text = incite_claim_war_attacker_must_have_claim_on_recipient_tt
		any_claim = {
			tier >= tier_county
			tier <= tier_kingdom
			is_landless_type_title = no
			holder ?= {
				target_is_same_character_or_above = $RECIPIENT$
			}
		}
	}
	trigger_if = {
		limit = { exists = scope:target }
		custom_description = {
			text = incite_claim_war_attacker_must_have_claim_on_target_tt
			has_claim_on = scope:target
		}
	}
	tgp_japan_offensive_wars_ban_trigger = yes
}
incite_claim_war_title_can_be_picked_trigger = {
	tier <= tier_kingdom
	tier >= tier_county
	is_landless_type_title = no
	holder ?= {
		target_is_same_character_or_above = $RECIPIENT$
	}
	trigger_if = {
		limit = {
			exists = $SECONDARY$
		}
		custom_tooltip = {
			text = sponsored_claim_war_attacker_must_have_claim_tt
			$SECONDARY$ = { has_claim_on = scope:target }
		}
	}
	trigger_else = {
		custom_tooltip = {
			text = sponsored_claim_war_someone_must_have_claim_tt
			any_claimant = {
				valid_claim_war_incited_attacker_trigger = { RECIPIENT = $RECIPIENT$ }
			}
		}
	}
	trigger_if = {
		limit = {
			tier >= tier_kingdom
		}
		custom_tooltip = {
			text = piety_too_low_target_kingdom_tt
			scope:puppet_or_actor = { piety_level >= 4 }
		}
	}
	trigger_if = {
		limit = {
			tier >= tier_duchy
		}
		custom_tooltip = {
			text = piety_too_low_target_duchy_tt
			scope:puppet_or_actor = { piety_level >= 3 }
		}
	}
	trigger_if = {
		limit = {
			scope:puppet_or_actor = {
				is_at_war_with = $RECIPIENT$
			}
		}
		custom_tooltip = {
			text = not_at_war_for_target_title_or_above_tt
			scope:puppet_or_actor = {
				NOT = {
					any_character_war = {
						casus_belli = {
							any_target_title = {
								OR = {
									any_this_title_or_de_jure_above = {
										this = scope:target
									}
									any_in_de_jure_hierarchy = {
										this = scope:target
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
incite_holy_war_ai_invalidation_trigger = {
	#One condition must be true, and none of the NOR true
	OR = {
		$SECONDARY$ = { ai_zeal >= 80 }
		$SECONDARY$ = { ai_boldness >= 100 }
		#Being forced to (despite having precariously comparable mil strength)
		scope:use_hook ?= yes
		trigger_if = {
			limit = {
				exists = $SECONDARY$
			}
			AND = {
				scope:fund_war ?= yes
				scope:puppet_or_actor.major_gold_value >= $SECONDARY$.monumental_gold_value
			}
		}
		AND = {
			scope:join_war ?= yes
			scope:puppet_or_actor.current_military_strength >= $RECIPIENT$.half_current_military_strength
		}
		#Can probably win
		trigger_if = {
			limit = {
				exists = $SECONDARY$
			}
			current_military_strength <= $SECONDARY$.seventy_percent_of_current_military_strength
		}
		scope:puppet_or_actor = { is_allied_to = $SECONDARY$ }
		AND = {
			scope:puppet_or_actor.house.house_head ?= scope:puppet_or_actor
			$SECONDARY$.house ?= scope:puppet_or_actor.house
		}
		$SECONDARY$ = {
			has_relation_best_friend = scope:puppet_or_actor
		}
		AND = {
			$SECONDARY$ = {
				target_is_liege_or_above = scope:puppet_or_actor
				has_dread_level_towards = {
					target = scope:puppet_or_actor
					level >= 2
				}
			}
		}
	}
	$SECONDARY$ = {
		NOR = {
			ai_zeal < -80
			opinion = {
				target = scope:puppet_or_actor
				value <= -90
			}
			opinion = {
				target = $RECIPIENT$
				value >= 75
			}
			has_any_good_relationship_with_character_trigger = { CHARACTER = $RECIPIENT$ }
			rite = { rite_has_parameter = holy_wars_forbidden }
			any_personal_tenet = {
				OR = {
					this = tenet:tenet_pacifism
					this = tenet:tenet_dharmic_pacifism
				}
			}
			AND = {
				scope:puppet_or_actor = {
					NOT = { rite = $SECONDARY$.rite }
				}
				any_tenet = {
					scope:puppet_or_actor.rite = {
						has_tenet_status = {
							tenet = prev
							status = core
						}
					}
					$SECONDARY$.rite = {
						has_tenet_status = {
							tenet = prev
							status = prohibited
						}
					}
				}
			}
			$RECIPIENT$ = {
				has_trait = greatest_of_khans
			}
			scope:target ?= {
				NOT = {
					tier = tier_county
				}
				any_de_jure_county = {
					count >= 4
					holder = {
						target_is_same_character_or_above = $RECIPIENT$
					}
					any_county_province = {
						is_county_capital = yes
						OR = {
							has_holding_type = nomad_holding
							has_holding_type = herder_holding
						}
					}
				}
			}
			has_dread_level_towards = {
				target = $RECIPIENT$
				level >= 2
			}
		}
	}
}

pam_valid_enforce_succession_title_trigger = {
	$TITLE$ = {
		tier >= tier_county
		is_landless_type_title = no
		exists = current_heir
		NOT = {
			holder ?= current_heir
		}
		holder ?= {
			is_ai = yes
			this != $ACTOR$
			faith = $ACTOR$.faith
			NOT = { is_at_war_with = $ACTOR$ }
		}
	}
}

pam_demand_church_succession_cs_candidate_trigger = {
	this != $ACTOR$
	age >= 50
	NOT = { any_child = { is_alive = yes } }
	is_landed = yes
	faith = $ACTOR$.faith
	is_valid_demand_church_succession_target_trigger = yes
	primary_title ?= { tier >= tier_county }
	NOR = {
		has_character_flag = demand_church_succession_refused_immunity
		exists = fallback_succession_title
		exists = fallback_succession_character
	}
}

pam_request_white_peace_enemy_trigger = {
	is_ruler = yes
	OR = {
		religion = religion:catholic_religion
		religion = religion:protestant_religion
		religion = religion:eastern_orthodox_religion
		religion = religion:oriental_orthodox_religion
	}
	OR = {
		rite ?= scope:puppet_or_actor.rite
		faith ?= scope:puppet_or_actor.faith
		religious_head_or_challenger ?= {
			scope:puppet_or_actor.religious_head_or_challenger ?= this
		}
	}
	NOR = {
		government_has_flag = government_is_theocracy
		has_trait = excommunicated
	}
	scope:puppet_or_actor = {
		is_at_war_with = prev
	}
}

pam_request_white_peace_war_with_enemy_trigger = {
	has_valid_casus_belli = yes
	is_white_peace_possible = yes
	war_days >= 730
	NOR = {
		using_cb = undirected_great_holy_war
		using_cb = directed_great_holy_war
	}
	OR = {
		AND = {
			casus_belli = {
				primary_attacker = scope:puppet_or_actor
				primary_defender = $ENEMY$
			}
		}
		AND = {
			casus_belli = {
				primary_defender = scope:puppet_or_actor
				primary_attacker = $ENEMY$
			}
		}
	}
}
pam_request_victory_war_with_enemy_trigger = {
	has_valid_casus_belli = yes
	#A war the design will not let you negotiate to a white peace is not one the clergy may rule won either
	is_white_peace_possible = yes
	NOR = {
		using_cb = undirected_great_holy_war
		using_cb = directed_great_holy_war
	}
	OR = {
		AND = {
			casus_belli = {
				primary_attacker = scope:puppet_or_actor
				primary_defender = $ENEMY$
			}
			attacker_war_score >= 90
		}
		AND = {
			casus_belli = {
				primary_defender = scope:puppet_or_actor
				primary_attacker = $ENEMY$
			}
			defender_war_score >= 90
		}
	}
}

#A war that can already be won outright, so there is nothing for the clergy to do
pam_request_victory_war_already_won_trigger = {
	has_valid_casus_belli = yes
	#Kept in step with pam_request_victory_war_with_enemy_trigger so an ineligible war cannot block an eligible one
	is_white_peace_possible = yes
	NOR = {
		using_cb = undirected_great_holy_war
		using_cb = directed_great_holy_war
	}
	OR = {
		AND = {
			casus_belli = {
				primary_attacker = scope:puppet_or_actor
				primary_defender = $ENEMY$
			}
			attacker_war_score >= 100
		}
		AND = {
			casus_belli = {
				primary_defender = scope:puppet_or_actor
				primary_attacker = $ENEMY$
			}
			defender_war_score >= 100
		}
	}
}

pam_characters_to_evangelize_personal_tenet_trigger = {
	is_adult = yes
	is_ai = yes
	faith = root.faith
	highest_held_title_tier <= root.highest_held_title_tier
	trigger_if = {
		limit = {
			exists = scope:selected_item
		}
		any_personal_tenet = {
			this != scope:selected_item
		}
		trigger_if = {
			limit = {
				faith.main_rite = {
					has_tenet_status = {
						tenet = scope:selected_item
						status = prohibited
					}
				}
			}
			pam_ai_can_consider_forbidden_personal_tenet_trigger = yes
		}
	}
}
pam_monastic_tenet_spread_target_trigger = {
	is_adult = yes
	faith = $ORDER_LEADER$.faith
	NOR = {
		any_personal_tenet = {
			this =  $ORDER_LEADER$.holy_order.order_tenet
		}
		is_in_list = personal_tenet_targets
	}
	trigger_if = {
		limit = {
			faith.main_rite = {
				has_tenet_status = {
					tenet = $ORDER_LEADER$.holy_order.order_tenet
					status = prohibited
				}
			}
		}
		pam_ai_can_consider_forbidden_personal_tenet_trigger = yes
	}
	trigger_if = {
		limit = {
			exists = scope:actor
		}
		NOT = {
			this = scope:actor
		}
	}
}
monastic_lease_can_be_suborned_trigger = {
	barony.lessee = {
		save_temporary_scope_as = lessee
	}
	trigger_if = {
		limit = {
			county.holder ?= {
				is_ai = no
			}
		}
		county.holder = {
			NAND = {
				this = $ORDER$.holy_order_patron
				this = scope:lessee.holy_order.holy_order_patron
			}
			NOT = {
				scope:lessee.puppeteer ?= this
			}
		}
		NOT = {
			county = scope:lessee.capital_county
		}
	}
	OR = {
		county.holder ?= {
			OR = {
				AND = {
					exists = $ORDER$.leader.puppeteer
					this = $ORDER$.leader.puppeteer
				}
				AND = {
					exists = $ORDER$.holy_order_patron
					this = $ORDER$.holy_order_patron
					rite = $ORDER$.leader.rite
				}
			}
		}
		county.clerical_region_title.holder.rite ?= {
			this = $ORDER$.leader.rite
			NOT = {
				this = scope:lessee.rite
			}
		}
		county.clerical_region_title.holder ?= {
			OR = {
				AND = {
					exists = $ORDER$.leader.puppeteer
					this = $ORDER$.leader.puppeteer
				}
				AND = {
					exists = $ORDER$.holy_order_patron
					this = $ORDER$.holy_order_patron
					rite = $ORDER$.leader.rite
				}
			}
		}
		county.clerical_region_title.holder ?= {
			rite = $ORDER$.leader.rite
			any_personal_tenet = {
				this = $ORDER$.order_tenet
			}
			NOT = {
				any_personal_tenet = {
					this = scope:lessee.holy_order.order_tenet
				}
			}
			OR = {
				piety_level >= 4
				learning >= extremely_high_skill_rating
				has_trait = theologian
			}
		}
		AND = {
			exists = scope:lessee.holy_order.order_tenet
			county.holder ?= {
				rite = $ORDER$.leader.rite
				any_personal_tenet = {
					this = $ORDER$.order_tenet
				}
				NOT = {
					any_personal_tenet = {
						this = scope:lessee.holy_order.order_tenet
					}
				}
			}
		}
		AND = {
			county.holder.rite ?= {
				this = $ORDER$.leader.rite
				NOT = {
					scope:lessee.rite ?= this
				}
			}
		}
	}
	county.kingdom ?= { save_temporary_scope_as = target_kingdom }
	$ORDER$ = {
		any_leased_title = {
			kingdom ?= scope:target_kingdom
		}
	}
}


pam_tenet_can_be_evangelized_trigger = {
	scope:actor = {
		has_personal_tenet = $TENET$
	}
}

pam_evangelize_personal_tenet_send_option_selected_trigger = {
	OR = {
		scope:tenet_aniconism = yes
		scope:tenet_alexandrian_catechism = yes
		scope:tenet_armed_pilgrimages = yes
		scope:tenet_carnal_exaltation = yes
		scope:tenet_communal_identity = yes
		scope:tenet_communion = yes
		scope:tenet_consolamentum = yes
		scope:tenet_divine_marriage = yes
		scope:tenet_gnosticism = yes
		scope:tenet_mendicant_preachers = yes
		scope:tenet_pacifism = yes
		scope:tenet_pentarchy = yes
		scope:tenet_unrelenting_faith = yes
		scope:tenet_vows_of_poverty = yes
		scope:tenet_pastoral_isolation = yes
		scope:tenet_adaptive = yes
		scope:tenet_esotericism = yes
		scope:tenet_legalism = yes
		scope:tenet_literalism = yes
		scope:tenet_reincarnation = yes
		scope:tenet_religious_legal_pronouncements = yes
		scope:tenet_struggle_submission = yes
		scope:tenet_false_conversion_sanction = yes
		scope:tenet_tax_nonbelievers = yes
		scope:tenet_asceticism = yes
		scope:tenet_bhakti = yes
		scope:tenet_dharmic_pacifism = yes
		scope:tenet_inner_journey = yes
		scope:tenet_ritual_hospitality = yes
		scope:tenet_adorcism = yes
		scope:tenet_ancestor_worship = yes
		scope:tenet_astrology = yes
		scope:tenet_hedonistic = yes
		scope:tenet_human_sacrifice = yes
		scope:tenet_mystical_birthright = yes
		scope:tenet_ritual_celebrations = yes
		scope:tenet_sacred_childbirth = yes
		scope:tenet_sanctity_of_nature = yes
		scope:tenet_sun_worship = yes
		scope:tenet_warmonger = yes
		scope:tenet_gruesome_festivals = yes
		scope:tenet_cthonic_redoubts = yes
		scope:tenet_household_gods = yes
		scope:tenet_sinitic_syncretism = yes
		scope:tenet_eastern_syncretism = yes
		scope:tenet_unreformed_syncretism = yes
		scope:tenet_christian_syncretism = yes
		scope:tenet_islamic_syncretism = yes
		scope:tenet_jewish_syncretism = yes
		scope:tenet_exaltation_of_pain = yes
		scope:tenet_natural_primitivism = yes
		scope:tenet_pursuit_of_power = yes
		scope:tenet_ritual_cannibalism = yes
		scope:tenet_sacred_shadows = yes
		scope:tenet_polyamory = yes
		scope:tenet_sacrificial_ceremonies = yes
		scope:tenet_megaliths = yes
		scope:tenet_fp3_fedayeen = yes
		scope:tenet_communal_possessions = yes
		scope:tenet_pure_land = yes
		scope:tenet_no_mind = yes
		scope:tenet_mountain_worship = yes
		scope:tenet_extinction_of_dharma = yes
		scope:tenet_cranial_trophies = yes
		scope:tenet_pursuit_of_knowledge = yes
		scope:tenet_benevolent_governance = yes
		scope:tenet_filial_piety = yes
		scope:tenet_harmonious_society = yes
		scope:tenet_sacred_destruction = yes
		scope:tenet_preservation = yes
		scope:tenet_takamin = yes
		scope:tenet_peace_of_god = yes
		scope:tenet_dulia = yes
		scope:tenet_transubstantiation = yes
		scope:tenet_ora_et_labora = yes
		scope:tenet_hyperdulia = yes
		scope:tenet_simony = yes
		scope:tenet_monophysitism = yes
		scope:tenet_acts_of_apostles = yes
		scope:tenet_purgatory = yes
		scope:tenet_be_fruitful_and_multiply = yes
		scope:tenet_apostolic_succession = yes
		scope:tenet_confession = yes
		scope:tenet_original_sin = yes
		scope:tenet_celestial_hierarchy = yes
		scope:tenet_miles_christi = yes
		scope:tenet_philanthropia = yes
		scope:tenet_scholasticism = yes
		scope:tenet_adoptionism = yes
		scope:tenet_love_thy_neighbour = yes
		scope:tenet_divine_liturgy = yes
		scope:tenet_miaphysitism = yes
		scope:tenet_hesychasm = yes
		scope:tenet_anachoresis = yes
		scope:tenet_mortification_of_the_flesh = yes
		scope:tenet_holy_myron = yes
		scope:tenet_khachkar = yes
	}
}

pam_evangelize_recipient_desires_selected_tenet_trigger = {
	exists = scope:pam_evangelize_tenet
	scope:recipient = {
		any_desired_tenet = {
			this = scope:pam_evangelize_tenet
		}
	}
}

pam_evangelize_selected_tenet_prohibited_trigger = {
	exists = scope:pam_evangelize_tenet
	scope:recipient.rite = {
		has_tenet_status = {
			tenet = scope:pam_evangelize_tenet
			status = prohibited
		}
	}
}

# scope:pam_evangelize_tenet_check is the tenet being weighed on a character
# Saved scope not $TENET$: a macro inside a quoted script value drops the opening parenthesis
pam_evangelize_tenet_below_very_popular_trigger = {
	"faith.tenet_popularity(scope:pam_evangelize_tenet_check)" < tenet_popularity_very_popular_threshold
}

pam_character_has_peaceful_tenet_trigger = {
	$CHARACTER$ = {
		OR = {
			rite = {
				OR = {
					rite_has_tenet = tenet_peace_of_god
					rite_has_tenet = tenet_pacifism
					rite_has_tenet = tenet_dharmic_pacifism
				}
			}
			has_personal_tenet = tenet_peace_of_god
			has_personal_tenet = tenet_pacifism
			has_personal_tenet = tenet_dharmic_pacifism
		}
	}
}

pam_evangelize_personal_tenet_send_option_valid_trigger = {
	scope:recipient = {
		NOT = { has_personal_tenet = $TENET$ }
	}
	custom_tooltip = {
		text = pam_evangelize_only_one_personal_tenet_tt
		OR = {
			scope:$TENET$ = yes
			pam_evangelize_personal_tenet_send_option_selected_trigger = no
		}
	}
}

pam_college_of_cardinals_election_active_trigger = {
	exists = title:k_papal_state
	title:k_papal_state.holder ?= {
		has_realm_law = theocratic_elective_succession_law
	}
}

pam_leads_college_of_cardinals_trigger = {
	has_title = title:k_papal_state
	pam_college_of_cardinals_election_active_trigger = yes
}

pam_pope_is_head_of_faith_or_rite_trigger = {
	OR = {
		rite.head_of_rite ?= { has_title = title:k_papal_state }
		faith.main_rite.head_of_rite ?= { has_title = title:k_papal_state }
		faith.religious_head ?= { has_title = title:k_papal_state }
	}
}

# Cardinal scope needs $POPE$ here
# Both dynasty sides need exists or two lowborn prelates match each other
pam_cardinal_is_nepotistic_appointment_trigger = {
	OR = {
		AND = {
			exists = dynasty
			exists = $POPE$.dynasty
			dynasty = $POPE$.dynasty
		}
		has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = $POPE$ }
		has_any_best_good_relationship_with_character_trigger = { CHARACTER = $POPE$ }
	}
}

# $TITLE$ is the seat that heads this character's Church, and this runs on a character
# Rome is named directly: headless Chalcedonianism has no religious_head_title to compare
pam_title_is_church_seat_trigger = {
	OR = {
		$TITLE$ = title:k_papal_state
		AND = {
			exists = faith.religious_head_title
			$TITLE$ = faith.religious_head_title
		}
	}
}

pam_holds_christian_patriarch_title_trigger = {
	OR = {
		has_title = title:d_et_constantinople
		has_title = title:k_orthodox
		has_title = title:d_et_alexandria
		has_title = title:d_et_antioch
		has_title = title:d_et_jerusalem
		has_title = title:d_et_aquileia
		has_title = title:d_et_armenia
	}
}

# While Chalcedonianism is headless the Pope heads it as head of rite
pam_holds_church_seat_trigger = {
	OR = {
		any_held_title = { is_head_of_faith = yes }
		has_title = title:k_papal_state
	}
}

pam_college_election_title_has_electors_trigger = {
	trigger_if = {
		limit = { exists = faith.religious_head_title }
		# any_elector rather than a bare exists: is_candidate_from_set errors on a title with no election law.
		faith.religious_head_title = { any_elector = {} }
	}
	trigger_else = {
		title:k_papal_state = { any_elector = {} }
	}
}

pam_college_election_title_is_theocratic_elective_trigger = {
	trigger_if = {
		limit = { exists = faith.religious_head_title }
		faith.religious_head_title.holder ?= { has_realm_law = theocratic_elective_succession_law }
	}
	trigger_else = {
		title:k_papal_state.holder ?= { has_realm_law = theocratic_elective_succession_law }
	}
}

# Character scope. Do they sit on the Church seat the College of Cardinals elects?
pam_holds_college_elected_church_seat_trigger = {
	pam_holds_church_seat_trigger = yes
	pam_college_election_title_has_electors_trigger = yes
	pam_college_election_title_is_theocratic_elective_trigger = yes
}

pam_is_college_election_candidate_trigger = {
	trigger_if = {
		limit = { pam_college_election_title_has_electors_trigger = yes }
		trigger_if = {
			limit = { exists = faith.religious_head_title }
			is_candidate_from_set = {
				title = faith.religious_head_title
				source = $SOURCE$
			}
		}
		trigger_else = {
			is_candidate_from_set = {
				title = title:k_papal_state
				source = $SOURCE$
			}
		}
	}
	trigger_else = { # Error log suppressing
		always = no
	}
}

# We check the clerical title, because the government flag and is_clergy miss many PAM prelates
pam_is_investiture_clergy_trigger = {
	OR = {
		any_held_title = { has_clerical_region = yes }
		government_has_flag = government_is_ecclesiastical
		is_clergy = yes
	}
}

# Only landed county tier and above, because temple baronies match has_clerical_region and flood the guests
pam_is_investiture_landed_prelate_trigger = {
	is_landed = yes
	highest_held_title_tier >= tier_county
	OR = {
		any_held_title = { has_clerical_region = yes }
		government_has_flag = government_is_ecclesiastical
	}
}

pam_valid_ecumenical_council_voter_trigger = {
	faith = involved_activity.activity_host.faith
	OR = {
		pam_is_investiture_clergy_trigger = yes
		rite.head_of_rite ?= this
		has_variable = investiture_stance
		# puppet:X errors on non-playable characters, and only landed county tier rulers own that puppet
		AND = {
			is_landed = yes
			highest_held_title_tier >= tier_county
			OR = {
				has_theological_agent_puppet_trigger = yes
				is_theological_agent_puppet_trigger = yes
			}
		}
	}
}

pam_ec_doctrine_status_is_criminal_trigger = {
	OR = {
		scope:doctrine = doctrine:doctrine_witchcraft_crime
		scope:doctrine = doctrine:doctrine_deviancy_crime
		scope:doctrine = doctrine:doctrine_homosexuality_crime
		scope:doctrine = doctrine:doctrine_adultery_men_crime
		scope:doctrine = doctrine:doctrine_adultery_women_crime
		scope:doctrine = doctrine:doctrine_kinslaying_any_dynasty_member_crime
		scope:doctrine = doctrine:doctrine_kinslaying_extended_family_crime
		scope:doctrine = doctrine:doctrine_kinslaying_close_kin_crime
	}
}

pam_ec_doctrine_status_is_shunned_trigger = {
	OR = {
		scope:doctrine = doctrine:doctrine_witchcraft_shunned
		scope:doctrine = doctrine:doctrine_deviancy_shunned
		scope:doctrine = doctrine:doctrine_homosexuality_shunned
		scope:doctrine = doctrine:doctrine_adultery_men_shunned
		scope:doctrine = doctrine:doctrine_adultery_women_shunned
		scope:doctrine = doctrine:doctrine_kinslaying_shunned
	}
}

pam_ec_doctrine_status_is_accepted_trigger = {
	OR = {
		scope:doctrine = doctrine:doctrine_witchcraft_accepted
		scope:doctrine = doctrine:doctrine_deviancy_accepted
		scope:doctrine = doctrine:doctrine_homosexuality_accepted
		scope:doctrine = doctrine:doctrine_adultery_men_accepted
		scope:doctrine = doctrine:doctrine_adultery_women_accepted
		scope:doctrine = doctrine:doctrine_kinslaying_accepted
	}
}

pam_ec_doctrine_status_is_virtuous_trigger = {
	OR = {
		scope:doctrine = doctrine:doctrine_witchcraft_virtuous
		scope:doctrine = doctrine:doctrine_deviancy_virtuous
	}
}

pam_ec_doctrine_is_graded_trigger = {
	OR = {
		pam_ec_doctrine_status_is_criminal_trigger = yes
		pam_ec_doctrine_status_is_shunned_trigger = yes
		pam_ec_doctrine_status_is_accepted_trigger = yes
		pam_ec_doctrine_status_is_virtuous_trigger = yes
	}
}

pam_ec_doctrine_is_change_trigger = {
	scope:host.faith ?= {
		NOT = { has_doctrine = scope:doctrine }
	}
}

# The AI Conservative named to oppose the host in this debate; stands down while a player argues the Conservative side
pam_ec_is_active_counterplayer_trigger = {
	is_ai = yes
	is_in_guest_subset = { name = conservative }
	involved_activity ?= {
		exists = var:ec_counterplayer
		var:ec_counterplayer = prev
		NOT = {
			any_attending_character = {
				is_ai = no
				is_in_guest_subset = { name = conservative }
			}
		}
	}
}

# AI proposers who may skip the nearest-step rule at an Ecumenical Council
pam_ec_doctrine_extremist_proposer_trigger = {
	OR = {
		has_trait = herald
		has_trait = heresiarch
		has_trait = lunatic_1
		has_trait = lunatic_genetic
		has_trait = possessed_1
		has_trait = possessed_genetic
	}
}

# The proposed scope:ec_step_to is the closest step from the held scope:ec_step_from that the rite can choose
pam_ec_doctrine_is_nearest_step_trigger = {
	NOT = {
		root = {
			any_character_doctrine = {
				doctrine_in_same_group = scope:ec_step_from
				this != scope:ec_step_from
				this != scope:ec_step_to
				save_temporary_scope_as = ec_step_other
				root.faith.main_rite = { rite_can_choose_doctrine = scope:ec_step_other }
				"doctrine_divergence(scope:ec_step_from)" < ecumenical_council_doctrine_change_divergence_value
			}
		}
	}
}

pam_ec_doctrine_is_binary_trigger = {
	OR = {
		scope:doctrine = doctrine:doctrine_theocracy_temporal
		scope:doctrine = doctrine:doctrine_theocracy_lay_clergy
		scope:doctrine = doctrine:doctrine_clerical_marriage_allowed
		scope:doctrine = doctrine:doctrine_clerical_marriage_disallowed
		scope:doctrine = doctrine:doctrine_monotheist
		scope:doctrine = doctrine:doctrine_polytheist
		scope:doctrine = doctrine:doctrine_sacraments_central
		scope:doctrine = doctrine:doctrine_sacraments_absent
	}
}

pam_ec_doctrine_in_christian_abomination_group_trigger = {
	scope:host.faith ?= { has_doctrine = special_doctrine_is_christian_faith }
	OR = {
		scope:doctrine = doctrine:doctrine_witchcraft_crime
		scope:doctrine = doctrine:doctrine_witchcraft_shunned
		scope:doctrine = doctrine:doctrine_witchcraft_accepted
		scope:doctrine = doctrine:doctrine_witchcraft_virtuous
		scope:doctrine = doctrine:doctrine_deviancy_crime
		scope:doctrine = doctrine:doctrine_deviancy_shunned
		scope:doctrine = doctrine:doctrine_deviancy_accepted
		scope:doctrine = doctrine:doctrine_deviancy_virtuous
		scope:doctrine = doctrine:doctrine_kinslaying_any_dynasty_member_crime
		scope:doctrine = doctrine:doctrine_kinslaying_extended_family_crime
		scope:doctrine = doctrine:doctrine_kinslaying_close_kin_crime
		scope:doctrine = doctrine:doctrine_kinslaying_shunned
		scope:doctrine = doctrine:doctrine_kinslaying_accepted
	}
}

pam_ec_doctrine_in_christian_scandal_group_trigger = {
	scope:host.faith ?= { has_doctrine = special_doctrine_is_christian_faith }
	OR = {
		scope:doctrine = doctrine:doctrine_homosexuality_crime
		scope:doctrine = doctrine:doctrine_homosexuality_shunned
		scope:doctrine = doctrine:doctrine_homosexuality_accepted
		scope:doctrine = doctrine:doctrine_adultery_men_crime
		scope:doctrine = doctrine:doctrine_adultery_men_shunned
		scope:doctrine = doctrine:doctrine_adultery_men_accepted
		scope:doctrine = doctrine:doctrine_adultery_women_crime
		scope:doctrine = doctrine:doctrine_adultery_women_shunned
		scope:doctrine = doctrine:doctrine_adultery_women_accepted
	}
}

pam_ec_doctrine_in_christian_clerical_order_group_trigger = {
	scope:host.faith ?= { has_doctrine = special_doctrine_is_christian_faith }
	OR = {
		scope:doctrine = doctrine:doctrine_clerical_gender_male_only
		scope:doctrine = doctrine:doctrine_clerical_gender_either
		scope:doctrine = doctrine:doctrine_clerical_gender_female_only
	}
}

pam_ec_tenet_is_christian_abomination_trigger = {
	scope:host.faith ?= { has_doctrine = special_doctrine_is_christian_faith }
	OR = {
		scope:tenet = tenet:tenet_ritual_cannibalism
		scope:tenet = tenet:tenet_natural_primitivism
		scope:tenet = tenet:tenet_human_sacrifice
		scope:tenet = tenet:tenet_cranial_trophies
		scope:tenet = tenet:tenet_gruesome_festivals
		scope:tenet = tenet:tenet_sacrificial_ceremonies
		scope:tenet = tenet:tenet_carnal_exaltation
	}
}

pam_custom_kingdom_can_claim_duchy_exclave_trigger = {
	tier = tier_duchy
	save_temporary_scope_as = duchy_temp
    NOR = {
        $NEW_KING$ = {
            completely_controls = prev
        }
        holder ?= $NEW_KING$
        holder ?= {
            target_is_liege_or_above = $NEW_KING$
        }
    }
    NOT = {
    	any_de_jure_county = {
    		kingdom.title_capital_county ?= this
    	}
    }
    #Is not only duchy in kingdom
    kingdom = {
    	any_direct_de_jure_vassal_title = {
    		tier = tier_duchy
    		NOR = {
    			this = scope:duchy_temp
    			$NEW_KING$ = {
        		    completely_controls = prev
        		}
        		holder ?= $NEW_KING$
        		holder ?= {
        		    target_is_liege_or_above = $NEW_KING$
        		}
    		}
    	}
    }
    #Is not controlled by de jure liege
   	trigger_if = {
   		limit = {
   			exists = kingdom.holder
   		}
   		kingdom.holder = {
   			NOR = {
   				this = scope:duchy_temp.holder
   				any_vassal_or_below = {
   					this = scope:duchy_temp.holder
   				}
   				completely_controls = scope:duchy_temp
   			}
   		}
   	}
    #Not bordering any other county of de jure kingdom (not held by new king)
    NOT = {
        any_de_jure_county = {
            save_temporary_scope_as = county_temp
            
            any_neighboring_county = {
                kingdom = scope:county_temp.kingdom
                duchy = {
                    NOR = {
                        this = scope:county_temp.duchy
                        $NEW_KING$ = {
                            completely_controls = prev
                        }
                        holder ?= {
                            target_is_same_character_or_above = $NEW_KING$
                        }
                    }
                }
            }
        }
    }
    any_de_jure_county = {
        any_neighboring_county = {
            duchy = {
            	OR = {
            		holder ?= {
                		target_is_same_character_or_above = $NEW_KING$
                	}
                	$NEW_KING$ = {
                	    completely_controls = prev
                	}
                }
            }
        }
    }
}
pam_custom_empire_can_claim_kingdom_exclave_trigger = {
	tier = tier_kingdom
	save_temporary_scope_as = kingdom_temp
    NOR = {
        $NEW_EMPEROR$ = {
            completely_controls = prev
        }
        holder ?= $NEW_EMPEROR$
        holder ?= {
            target_is_liege_or_above = $NEW_EMPEROR$
        }
    }
    NOT = {
    	any_de_jure_county = {
    		empire.title_capital_county ?= this
    	}
    }
    #Is not only kingdom in empire
    empire = {
    	any_direct_de_jure_vassal_title = {
    		tier = tier_kingdom
    		NOR = {
    			this = scope:kingdom_temp
    			$NEW_EMPEROR$ = {
        		    completely_controls = prev
        		}
        		holder ?= $NEW_EMPEROR$
        		holder ?= {
        		    target_is_liege_or_above = $NEW_EMPEROR$
        		}
    		}
    	}
    }
    #Is not controlled by de jure liege
   	trigger_if = {
   		limit = {
   			exists = empire.holder
   		}
   		empire.holder = {
   			NOR = {
   				this = scope:kingdom_temp.holder
   				any_vassal_or_below = {
   					this = scope:kingdom_temp.holder
   				}
   				completely_controls = scope:kingdom_temp
   			}
   		}
   	}
    #Not bordering any other county of de jure empire (not held by new empire)
    NOT = {
        any_de_jure_county = {
            save_temporary_scope_as = county_temp
            any_title_to_title_neighboring_and_across_water_county = {
                empire = scope:county_temp.empire
                kingdom = {
                    NOR = {
                        this = scope:county_temp.kingdom
                        $NEW_EMPEROR$ = {
                            completely_controls = prev
                        }
                        holder ?= {
                            target_is_same_character_or_above = $NEW_EMPEROR$
                        }
                    }
                }
            }
        }
    }
    any_de_jure_county = {
        any_neighboring_county = {
            kingdom = {
            	OR = {
            		holder ?= {
                		target_is_same_character_or_above = $NEW_EMPEROR$
                	}
                	$NEW_EMPEROR$ = {
                	    completely_controls = prev
                	}
                }
            }
        }
    }
}

ai_religious_request_blocker_trigger = {
	scope:actor = {
		trigger_if = {
			limit = {
				is_ai = yes
				scope:recipient = { is_ai = no }
			}
			# Each religious request has its own cooldown against a player Head of Faith
			NOT = { scope:recipient = { has_character_flag = $INTERACTION$_received_cooldown } }
		}
	}
}

target_valid_ecclesiastical_de_jure_shifting_trigger = {
	is_landless_type_title = no
	is_head_of_faith = no
	tier = scope:puppet_or_actor.highest_held_title_tier_minus_one
	custom_tooltip = {
		text = petition_de_jure_integration_not_already_de_jure_tt
		NOT = {
			de_jure_liege = scope:puppet_or_actor.primary_title
		}
	}
	custom_tooltip = {
		text = petition_de_jure_integration_target_is_connected_tt
		any_de_jure_county = {
			holder ?= {
				target_is_same_character_or_above = scope:puppet_or_actor
			}
		}
	}
	custom_tooltip = {
		text = recipient_not_targets_de_jure_liege_tt
		NOT = {
			de_jure_liege.holder ?= scope:recipient
		}
	}
	trigger_if = {
		limit = {
			exists = scope:recipient.religious_head_or_challenger
			scope:recipient.religious_head_or_challenger != scope:recipient
			exists = scope:target.de_jure_liege.holder
		}
		custom_tooltip = {
			text = hof_is_target_de_jure_liege_tt
			NOT = {
				scope:recipient.religious_head_or_challenger ?= scope:target.de_jure_liege.holder
			}
		}
	}
	#AI should only do direct borders, not over water - ew gross
	trigger_if = {
		limit = {
			scope:puppet_or_actor = {
				is_ai = no
			}
		}
		custom_tooltip = {
			text = target_must_border_your_de_jure
			any_de_jure_county = {
				any_title_to_title_neighboring_and_across_water_county = {
					any_this_title_or_de_jure_above = {
						this = scope:puppet_or_actor.primary_title
					}
				}
			}
		}
	}
	trigger_else = {
		custom_tooltip = {
			text = target_must_border_your_de_jure_land
			any_de_jure_county = {
				any_title_to_title_neighboring_county = {
					any_this_title_or_de_jure_above = {
						this = scope:puppet_or_actor.primary_title
					}
				}
			}
		}
	}
}
clergy_interaction_generic_piety_level_or_puppet_req_trigger = {
	trigger_if = {
		limit = {
			NOT = {
				government_has_flag = government_is_theocracy
			}
		}
		custom_tooltip = {
			text = piety_level_or_puppet_req_tt
			OR = {
				has_theological_agent_puppet_trigger = yes
				piety_level >= 3
			}
		}
	}
	trigger_else = {
		piety_level >= 1
	}
}

#Title scope. An uncreated title the petitioner already has a foothold in; scope:puppet_or_actor is the petitioning ruler.
target_valid_request_unformed_title_trigger = {
	is_landless_type_title = no
	is_head_of_faith = no
	tier > tier_county
	custom_tooltip = {
		text = target_has_holder_tt
		NOT = { exists = holder }
	}
	custom_tooltip = {
		text = request_unformed_title_no_hegemony_tt
		tier < tier_hegemony
	}
	custom_tooltip = {
		text = request_unformed_title_tier_range_tt
		NOR = {
			tier > scope:puppet_or_actor.highest_held_title_tier_plus_one
			tier < scope:puppet_or_actor.highest_held_title_tier
		}
	}
	custom_tooltip = {
		text = request_unformed_title_hold_de_jure_vassal_tt
		any_direct_de_jure_vassal_title = {
			holder ?= scope:puppet_or_actor
		}
	}
	custom_tooltip = {
		text = request_unformed_title_third_of_de_jure_tt
		any_de_jure_county = {
			percent >= 0.33
			holder ?= {
				target_is_same_character_or_above = scope:puppet_or_actor
			}
			faith = scope:puppet_or_actor.faith
		}
	}
}

pam_personal_pacifism_offensive_war_ban_trigger = {
	custom_tooltip = {
		text = pam_personal_pacifism_offensive_war_ban_tt
		scope:attacker = {
			NOT = { has_personal_tenet_flag = tenet_pacifism_no_offensive_war }
		}
	}
}

has_different_doctrine_with_rite_trigger = {
	rite_has_doctrine = $DOCTRINE$
	NOT = { $RITE$ = { rite_has_doctrine = $DOCTRINE$ } }
}

pam_doctrine_can_be_reaffirm = {
	OR = {
		this = doctrine:doctrine_monogamy
		this = doctrine:doctrine_polygamy
		this = doctrine:doctrine_concubines

		this = doctrine:doctrine_divorce_disallowed
		this = doctrine:doctrine_divorce_approval
		this = doctrine:doctrine_divorce_allowed

		this = doctrine:doctrine_bastardry_none
		this = doctrine:doctrine_bastardry_legitimization
		this = doctrine:doctrine_bastardry_all

		this = doctrine:doctrine_homosexuality_crime
		this = doctrine:doctrine_homosexuality_shunned
		this = doctrine:doctrine_homosexuality_accepted

		this = doctrine:doctrine_deviancy_crime
		this = doctrine:doctrine_deviancy_shunned
		this = doctrine:doctrine_deviancy_accepted
		this = doctrine:doctrine_deviancy_virtuous

		this = doctrine:doctrine_adultery_men_crime
		this = doctrine:doctrine_adultery_men_shunned
		this = doctrine:doctrine_adultery_men_accepted

		this = doctrine:doctrine_adultery_women_crime
		this = doctrine:doctrine_adultery_women_shunned
		this = doctrine:doctrine_adultery_women_accepted

		this = doctrine:doctrine_kinslaying_any_dynasty_member_crime
		this = doctrine:doctrine_kinslaying_extended_family_crime
		this = doctrine:doctrine_kinslaying_close_kin_crime
		this = doctrine:doctrine_kinslaying_shunned
		this = doctrine:doctrine_kinslaying_accepted

		this = doctrine:doctrine_witchcraft_crime
		this = doctrine:doctrine_witchcraft_shunned
		this = doctrine:doctrine_witchcraft_accepted
		this = doctrine:doctrine_witchcraft_virtuous

		this = doctrine:doctrine_gender_male_dominated
		this = doctrine:doctrine_gender_equal
		this = doctrine:doctrine_gender_female_dominated

		this = doctrine:doctrine_consanguinity_dynastic
		this = doctrine:doctrine_consanguinity_restricted
		this = doctrine:doctrine_consanguinity_cousins
		this = doctrine:doctrine_consanguinity_aunt_nephew_and_uncle_niece
		this = doctrine:doctrine_consanguinity_unrestricted

		this = doctrine:doctrine_pluralism_fundamentalist
		this = doctrine:doctrine_pluralism_righteous
		this = doctrine:doctrine_pluralism_pluralistic

		this = doctrine:doctrine_theocracy_temporal
		this = doctrine:doctrine_theocracy_lay_clergy

		this = doctrine:doctrine_no_head
		this = doctrine:doctrine_spiritual_head
		this = doctrine:doctrine_temporal_head

		this = doctrine:doctrine_clerical_function_taxation
		this = doctrine:doctrine_clerical_function_alms_and_pacification
		this = doctrine:doctrine_clerical_function_recruitment

		this = doctrine:doctrine_clerical_gender_male_only
		this = doctrine:doctrine_clerical_gender_either
		this = doctrine:doctrine_clerical_gender_female_only

		this = doctrine:doctrine_clerical_marriage_allowed
		this = doctrine:doctrine_clerical_marriage_disallowed

		this = doctrine:doctrine_clerical_succession_temporal_appointment
		this = doctrine:doctrine_clerical_succession_spiritual_appointment
		this = doctrine:doctrine_clerical_succession_temporal_fixed_appointment
		this = doctrine:doctrine_clerical_succession_spiritual_fixed_appointment
		this = doctrine:doctrine_clerical_succession_conditional

		this = doctrine:doctrine_pilgrimage_forbidden
		this = doctrine:doctrine_pilgrimage_encouraged
		this = doctrine:doctrine_pilgrimage_local_rites
		this = doctrine:doctrine_pilgrimage_mandatory
		this = doctrine:doctrine_pilgrimage_mandatory_hajj

		this = doctrine:doctrine_funeral_stoic
		this = doctrine:doctrine_funeral_bewailment
		this = doctrine:doctrine_funeral_cremation
		this = doctrine:doctrine_funeral_sky_burial
		this = doctrine:doctrine_funeral_mummification
		this = doctrine:doctrine_family_rites

		this = doctrine:doctrine_no_anointment
		this = doctrine:doctrine_anointment_permitted
		this = doctrine:doctrine_imperial_anointment

		this = doctrine:doctrine_monotheist
		this = doctrine:doctrine_polytheist

		this = doctrine:doctrine_monasticism_encouraged
		this = doctrine:doctrine_monasticism_accepted
		this = doctrine:doctrine_monasticism_absent
		this = doctrine:doctrine_monasticism_forbidden

		this = doctrine:doctrine_sacraments_central
		this = doctrine:doctrine_sacraments_absent
	}
}

pam_reaffirm_doctrine_send_option_valid_trigger = {
	custom_tooltip = {
		text = pam_reaffirm_doctrine_one_doctrine_tt
		OR = {
			scope:$DOCTRINE$ = yes
			pam_reaffirm_doctrine_send_option_selected_trigger = no
		}
	}
}

pam_vows_of_poverty_premium_activity_option_valid_trigger = {
	custom_tooltip = {
		text = pam_vows_of_poverty_premium_activity_option_blocked_tt
		NOT = { has_personal_tenet_flag = tenet_vows_of_poverty_humble_host }
	}
}

pam_reaffirm_doctrine_send_option_selected_trigger = {
	OR = {
		scope:doctrine_monogamy = yes
		scope:doctrine_polygamy = yes
		scope:doctrine_concubines = yes

		scope:doctrine_divorce_disallowed = yes
		scope:doctrine_divorce_approval = yes
		scope:doctrine_divorce_allowed = yes

		scope:doctrine_bastardry_none = yes
		scope:doctrine_bastardry_legitimization = yes
		scope:doctrine_bastardry_all = yes

		scope:doctrine_homosexuality_crime = yes
		scope:doctrine_homosexuality_shunned = yes
		scope:doctrine_homosexuality_accepted = yes

		scope:doctrine_deviancy_crime = yes
		scope:doctrine_deviancy_shunned = yes
		scope:doctrine_deviancy_accepted = yes
		scope:doctrine_deviancy_virtuous = yes

		scope:doctrine_adultery_men_crime = yes
		scope:doctrine_adultery_men_shunned = yes
		scope:doctrine_adultery_men_accepted = yes

		scope:doctrine_adultery_women_crime = yes
		scope:doctrine_adultery_women_shunned = yes
		scope:doctrine_adultery_women_accepted = yes

		scope:doctrine_kinslaying_any_dynasty_member_crime = yes
		scope:doctrine_kinslaying_extended_family_crime = yes
		scope:doctrine_kinslaying_close_kin_crime = yes
		scope:doctrine_kinslaying_shunned = yes
		scope:doctrine_kinslaying_accepted = yes

		scope:doctrine_witchcraft_crime = yes
		scope:doctrine_witchcraft_shunned = yes
		scope:doctrine_witchcraft_accepted = yes
		scope:doctrine_witchcraft_virtuous = yes

		scope:doctrine_gender_male_dominated = yes
		scope:doctrine_gender_equal = yes
		scope:doctrine_gender_female_dominated = yes

		scope:doctrine_consanguinity_dynastic = yes
		scope:doctrine_consanguinity_restricted = yes
		scope:doctrine_consanguinity_cousins = yes
		scope:doctrine_consanguinity_aunt_nephew_and_uncle_niece = yes
		scope:doctrine_consanguinity_unrestricted = yes

		scope:doctrine_pluralism_fundamentalist = yes
		scope:doctrine_pluralism_righteous = yes
		scope:doctrine_pluralism_pluralistic = yes

		scope:doctrine_theocracy_temporal = yes
		scope:doctrine_theocracy_lay_clergy = yes

		scope:doctrine_no_head = yes
		scope:doctrine_spiritual_head = yes
		scope:doctrine_temporal_head = yes

		scope:doctrine_clerical_function_taxation = yes
		scope:doctrine_clerical_function_alms_and_pacification = yes
		scope:doctrine_clerical_function_recruitment = yes

		scope:doctrine_clerical_gender_male_only = yes
		scope:doctrine_clerical_gender_either = yes
		scope:doctrine_clerical_gender_female_only = yes

		scope:doctrine_clerical_marriage_allowed = yes
		scope:doctrine_clerical_marriage_disallowed = yes

		scope:doctrine_clerical_succession_temporal_appointment = yes
		scope:doctrine_clerical_succession_spiritual_appointment = yes
		scope:doctrine_clerical_succession_temporal_fixed_appointment = yes
		scope:doctrine_clerical_succession_spiritual_fixed_appointment = yes
		scope:doctrine_clerical_succession_conditional = yes

		scope:doctrine_pilgrimage_forbidden = yes
		scope:doctrine_pilgrimage_encouraged = yes
		scope:doctrine_pilgrimage_local_rites = yes
		scope:doctrine_pilgrimage_mandatory = yes
		scope:doctrine_pilgrimage_mandatory_hajj = yes

		scope:doctrine_funeral_stoic = yes
		scope:doctrine_funeral_bewailment = yes
		scope:doctrine_funeral_cremation = yes
		scope:doctrine_funeral_sky_burial = yes
		scope:doctrine_funeral_mummification = yes
		scope:doctrine_family_rites = yes

		scope:doctrine_no_anointment = yes
		scope:doctrine_anointment_permitted = yes
		scope:doctrine_imperial_anointment = yes

		scope:doctrine_monotheist = yes
		scope:doctrine_polytheist = yes

		scope:doctrine_monasticism_encouraged = yes
		scope:doctrine_monasticism_accepted = yes
		scope:doctrine_monasticism_absent = yes
		scope:doctrine_monasticism_forbidden = yes

		scope:doctrine_sacraments_central = yes
		scope:doctrine_sacraments_absent = yes
	}
}

pam_reaffirm_doctrine_kinslayer_decline_trigger = {
	OR = {
		has_trait = kinslayer
		any_secret = {
			type = secret_murder
			secret_target = { save_temporary_scope_as = target }
			scope:recipient = {
				murdering_character_is_crime_in_rite_trigger = {
					CHARACTER = scope:target
					RITE = scope:actor.rite
				}
			}
		}
	}
}
pam_reaffirm_doctrine_kinslayer_accept_trigger = {
	OR = {
		has_trait = kinslayer
		any_secret = {
			type = secret_murder
			secret_target = { save_temporary_scope_as = target }
			scope:recipient = {
				murdering_character_is_kinslaying_in_rite_trigger = {
					CHARACTER = scope:target
					RITE = scope:recipient.rite
				}
			}
		}
	}
}

pam_reaffirm_doctrine_consanguinity_decline_trigger = {
	OR = {
		any_relation = {
			type = lover
			relation_with_character_is_incestuous_in_rite_trigger = {
				CHARACTER = scope:recipient
				RITE = scope:actor.rite
			}
		}
		any_spouse = {
			relation_with_character_is_incestuous_in_rite_trigger = {
				CHARACTER = scope:recipient
				RITE = scope:actor.rite
			}
		}
	}
}

pam_reaffirm_doctrine_consanguinity_accept_trigger = {
	OR = {
		any_relation = {
			type = lover
			relation_with_character_is_incestuous_in_rite_trigger = {
				CHARACTER = scope:recipient
				RITE = scope:recipient.rite
			}
		}
		any_spouse = {
			relation_with_character_is_incestuous_in_rite_trigger = {
				CHARACTER = scope:recipient
				RITE = scope:recipient.rite
			}
		}
	}
}

skill_support_trigger = {
	#AI precautions
	trigger_if = {
		limit = {
			is_ai = yes
		}
		ai_greed > low_negative_ai_value
		ai_rationality > low_negative_ai_value
	}

	custom_tooltip = {
		text = sum_of_all_skills_comparison_unlock_tt
		scope:actor = {
			sum_of_all_skills_value > scope:recipient.sum_of_all_skills_value
		}
	}
	custom_tooltip = {
		text = sum_of_all_skills_unlock_tt
		scope:actor = {
			sum_of_all_skills_value >= 50
		}
	}
}
piety_support_trigger = {
	#AI precautions
	trigger_if = {
		limit = {
			is_ai = yes
		}
		ai_zeal >= 0
		ai_boldness >= low_negative_ai_value
	}

	NOT = {
		has_trait = excommunicated
	}
	rite = scope:recipient.rite
	custom_tooltip = {
		text = comparable_piety_to_recipient_req_tt
		piety_level >= scope:recipient.piety_level
	}
	OR = {
		has_trait = saint
		has_trait = herald
		has_trait = saoshyant
		has_trait = paragon
		has_trait = savior
		has_trait = crusader_king
		has_trait = divine_blood
		has_trait = faith_warrior
		has_trait = theologian
		has_trait = zealous
		custom_tooltip = {
			text = you_are_head_of_faith_req_tt
			trigger_if = {
				limit = { exists = scope:recipient.faith.religious_head }
				this = scope:recipient.faith.religious_head
			}
		}
		custom_tooltip = {
			text = pilgrim_xp_req_tt
			has_trait = pilgrim
			has_trait_xp = {
				trait = pilgrim
				value >= 50
			}
		}
		custom_tooltip = {
			text = scholar_xp_req_tt
			has_trait = lifestyle_scholar
			has_trait_xp = {
				trait = lifestyle_scholar
				value >= 50
			}
		}
	}
	scope:recipient = {
		NOT = {
			has_trait = cynical
		}
	}
}
dread_support_trigger = {
	#AI precautions
	trigger_if = {
		limit = {
			is_ai = yes
		}
		ai_compassion < 0
		ai_honor < low_positive_ai_value
		ai_zeal < low_positive_ai_value
	}

	custom_tooltip = {
		text = recipient_must_be_terrified_tt
		scope:recipient = {
			has_dread_level_towards = {
				target = scope:actor
				level >= 2
			}
		}
	}
}
dread_support_secular_trigger = {
	dread_support_trigger = yes
	scope:actor = {
		dread >= medium_dread_value
	}
}
friendly_support_base_trigger = {
	#AI precautions
	trigger_if = {
		limit = {
			is_ai = yes
		}
		ai_compassion >= 0
		ai_honor >= 0
		ai_sociability >= low_negative_ai_value
		ai_greed >= low_negative_ai_value
	}

	scope:actor = {
		NOR = {
			has_trait = disloyal
			has_trait = denounced
		}
		piety_level >= 1
	}
	custom_tooltip = {
		text = recipient_opinion_80_req
		scope:recipient = {
			opinion = {
				target = scope:actor
				value >= 80
			}
		}
	}
	scope:actor = {
		OR = {
			has_relation_friend = scope:recipient
			is_close_family_of = scope:recipient
			has_relation_soulmate = scope:recipient
			is_spouse_of = scope:recipient
			has_relation_blood_brother = scope:recipient
			has_relation_ward = scope:recipient
		}
	}
}
friendly_support_trigger = {
	friendly_support_base_trigger = yes
	scope:actor = {
		prestige_level >= 1
	}
}
friendly_support_secular_trigger = {
	friendly_support_base_trigger = yes
	scope:actor = {
		prestige_level >= 2
	}
}
lunacy_support_trigger = {
	#AI precautions
	trigger_if = {
		limit = {
			is_ai = yes
		}
		ai_compassion <= medium_negative_ai_value
		ai_rationality <= low_positive_ai_value
		ai_sociability <= low_positive_ai_value
		ai_boldness <= low_positive_ai_value
		OR = {
			custom_tooltip = {
				text = witch_secret_unlock_tt
				any_secret = {
					type = secret_witch
				}
			}
			has_trait = witch
			has_trait = schemer
			has_trait = torturer
			ai_compassion <= high_negative_ai_value
		}
	}

	scope:recipient = {
		intrigue < mediocre_skill_rating
	}
	OR = {
		custom_tooltip = {
			text = witch_secret_unlock_tt
			any_secret = {
				type = secret_witch
			}
		}
		has_trait = witch
		has_trait = schemer
		has_trait = lifestyle_herbalist
		has_trait = torturer
		custom_tooltip = {
			text = high_trickster_xp_unlock_tt
			has_trait = gallowsbait
			has_trait_xp = {
				trait = gallowsbait
				track = trickster
				value >= 40
			}
		}
	}
	scope:recipient = {
		OR = {
			has_trait = eccentric
			has_trait = lunatic
			has_trait = possessed
			has_trait = drunkard
			has_trait = hashishiyah
			has_trait = withering_mind
			has_trait = ergotism
			has_trait = bubonic_plague
			is_adult = no
		}
	}
}

# 5+ counties so small dukes do not found theocracies
# When no theocratic vassal exists we skip the percent scan so this stays fast
realm_theocratic_county_share_ai_below_cap_trigger = {
	any_sub_realm_county = {
		count >= 5
	}
	OR = {
		NOT = { any_vassal = { government_has_flag = government_is_theocracy } }
		any_sub_realm_county = {
			percent < 0.10
			holder = { government_has_flag = government_is_theocracy }
		}
	}
}

realm_theocratic_county_share_player_below_cap_trigger = {
	OR = {
		NOT = { any_vassal = { government_has_flag = government_is_theocracy } }
		any_sub_realm_county = {
			percent < 0.25
			holder = { government_has_flag = government_is_theocracy }
		}
	}
}

# The granting ruler is root, and scope:title is the highest-tier title being granted
pam_nomad_grant_settled_government_trigger = {
	OR = {
		government_has_flag = government_is_nomadic
		government_has_flag = government_is_herder
	}
	NOT = { has_variable = grant_vassal_government_cooldown }
	pam_nomad_inclined_to_settle_trigger = yes
	scope:title = {
		tier = tier_county
		title_province = {
			OR = {
				has_holding_type = castle_holding
				has_holding_type = city_holding
				has_holding_type = church_holding
			}
		}
	}
}

# Follows settle_people_decision ai_potential so an AI nomad always or never grants settled holdings
pam_nomad_inclined_to_settle_trigger = {
	NOR = {
		has_trait = greatest_of_khans
		has_trait = conqueror
		any_owned_story = {
			OR = {
				story_type = story_mongol_invasion
				story_type = story_greatest_of_khans
			}
		}
	}
	OR = {
		has_trait = content
		domicile ?= { herd <= 500 }
		is_independent_ruler = no
	}
}

# Artifact scope
pam_holy_weapon_trigger = {
	is_equipped = yes
	artifact_slot_type = primary_armament
	is_holy_relic_for = artifact_owner.faith
}

###############################################
#	Harmonious Society - the Five Bonds
###############################################
# A bond only counts while it is actually kept, so each one asks for a living partner who thinks
# well of the scoped character. 50 opinion is the threshold throughout.

pam_five_bonds_ruler_subject_trigger = {
	OR = {
		AND = {
			exists = liege
			liege = {
				opinion = {
					target = prev
					value >= 50
				}
			}
		}
		any_vassal = {
			opinion = {
				target = prev
				value >= 50
			}
		}
	}
}

pam_five_bonds_parent_child_trigger = {
	OR = {
		any_parent = {
			opinion = {
				target = prev
				value >= 50
			}
		}
		any_child = {
			is_adult = yes
			opinion = {
				target = prev
				value >= 50
			}
		}
	}
}

pam_five_bonds_spouse_trigger = {
	any_spouse = {
		opinion = {
			target = prev
			value >= 50
		}
	}
}

pam_five_bonds_sibling_trigger = {
	any_sibling = {
		is_adult = yes
		opinion = {
			target = prev
			value >= 50
		}
	}
}

pam_five_bonds_friend_trigger = {
	any_relation = {
		type = friend
		count >= 1
		opinion = {
			target = prev
			value >= 50
		}
	}
}

# Same rules as release_from_prison_interaction and only valid inside that interaction
tenet_benevolent_governance_unconditional_release_trigger = {
	scope:demand_conversion = no
	scope:renounce_claims = no
	scope:banish = no
	scope:take_vows = no
	scope:recruit = no
	scope:disfigure = no
	scope:blind = no
	scope:castrate = no
	scope:demand_admin = no
	scope:gain_hook = no
	scope:make_puppet = no
	scope:change_prison = no
	scope:become_executioner = no
}

theocratic_lay_clergy_trigger = {
	trigger_if = {
		limit = {
			rite = { rite_has_doctrine = doctrine_theocracy_lay_clergy }
		}
		OR = {
			# Cardinals
			exists = clerical_elector_title
			# Archdioceses
			primary_title ?= { has_clerical_region = yes }
			# Other landless explicit clergy
			AND = {
				is_landed = no
				is_clergy = yes
			}
		}
	}
}

pam_is_clerical_comprehensive_trigger = {
	OR = {
		# Cardinals
		exists = clerical_elector_title
		# Archdioceses
		primary_title ?= { has_clerical_region = yes }
		# Landed and landless ecclesiastical rulers
		government_has_flag = government_is_ecclesiastical
		# Other landless explicit clergy
		AND = {
			is_landed = no
			is_clergy = yes
		}
	}
}

# Current scope is a faith and root is the character choosing the personal tenet
pam_personal_syncretism_faith_matches_trigger = {
	OR = {
		AND = {
			root = { has_personal_tenet = tenet_sinitic_syncretism }
			religion = { is_in_family = rf_sinitic }
		}
		AND = {
			root = { has_personal_tenet = tenet_eastern_syncretism }
			religion = { is_in_family = rf_eastern }
		}
		AND = {
			root = { has_personal_tenet = tenet_unreformed_syncretism }
			has_doctrine_parameter = unreformed
		}
		AND = {
			root = { has_personal_tenet = tenet_christian_syncretism }
			religion_tag = catholic_religion
		}
		AND = {
			root = { has_personal_tenet = tenet_christian_syncretism }
			religion_tag = protestant_religion
		}
		AND = {
			root = { has_personal_tenet = tenet_christian_syncretism }
			religion_tag = eastern_orthodox_religion
		}
		AND = {
			root = { has_personal_tenet = tenet_christian_syncretism }
			religion_tag = oriental_orthodox_religion
		}
		AND = {
			root = { has_personal_tenet = tenet_islamic_syncretism }
			religion_tag = islam_religion
		}
		AND = {
			root = { has_personal_tenet = tenet_jewish_syncretism }
			religion_tag = judaism_religion
		}
	}
}

# Exile pool eligibility probe for the personal syncretism decision tooltip.
pam_personal_syncretism_exile_rite_probe_trigger = {
	pam_personal_syncretism_exile_rite_trigger = yes
}

# Valid exile pool entry for the personal syncretism decision.
pam_personal_syncretism_exile_rite_trigger = {
	exists = var:converted_development
	var:converted_development >= 1
	NOT = { rite_has_parameter = unreformed }
	exists = var:associated_culture
	exists = var:associated_county
	save_temporary_scope_as = rite_scope
	trigger_if = {
		limit = {
			root = { exists = capital_province }
			exists = var:associated_county
		}
		"root.capital_province.squared_distance(scope:rite_scope.var:associated_county.title_province)" <= squared_distance_mega
	}
	root = {
		trigger_if = {
			limit = {
				has_variable_list = rites_converted_in_counties
			}
			NOT = {
				is_target_in_variable_list = {
					name = rites_converted_in_counties
					target = scope:rite_scope
				}
			}
		}
		any_sub_realm_county = {
			faith = root.faith
			trigger_if = {
				limit = {
					has_variable_list = rites_converted_in_the_county
				}
				NOT = {
					is_target_in_variable_list = {
						name = rites_converted_in_the_county
						target = scope:rite_scope
					}
				}
			}
		}
	}
	faith = {
		pam_personal_syncretism_faith_matches_trigger = yes
	}
}

pam_personal_syncretism_invite_exiles_available_trigger = {
	has_personal_tenet_flag = tenet_syncretism_invite_religious_exiles
}

pam_personal_syncretism_county_bonus_trigger = {
	# The yearly pulse evaluates this on a held county; root is its ruler.
	root = {
		has_personal_tenet_flag = tenet_syncretism_county_bonus
	}
	faith = {
		pam_personal_syncretism_faith_matches_trigger = yes
	}
}

###############################################
# Holy Myron

# Until pam_holy_myron.0002 destroys it, an emptied vessel stays as an artifact
# trigger_if keeps the variable fetch from evaluating on ordinary artifacts
pam_is_holy_myron_artifact_trigger = {
	trigger_if = {
		limit = { has_variable = pam_holy_myron_charges }
		var:pam_holy_myron_charges >= 1
	}
	trigger_else = {
		always = no
	}
}

# Character scope: owns an ampulla with oil left to spend.
pam_has_usable_holy_myron_trigger = {
	any_character_artifact = {
		pam_is_holy_myron_artifact_trigger = yes
	}
}

eligible_for_pam_legacy_trigger = {
	has_pam_dlc_trigger = yes
	OR = {
		game_rule_unrestricted_dynasty_legacies_trigger = yes
		dynasty = {
			OR = {
				dynast = {
					OR = {
						faith.religion = religion:catholic_religion
						faith.religion = religion:protestant_religion
						faith.religion = religion:eastern_orthodox_religion
						faith.religion = religion:oriental_orthodox_religion
					}
				}
				has_dynasty_perk = pam_legacy_1
			}
		}
	}
}

###############################################
# Religious Study scheme

pam_study_scripture_pursuit_of_knowledge_trigger = {
	OR = {
		rite ?= { rite_has_parameter = tenet_pursuit_of_knowledge_study_scripture_bonus }
		has_personal_tenet_flag = tenet_pursuit_of_knowledge_study_scripture_bonus
	}
}

pam_study_scripture_scholasticism_trigger = {
	OR = {
		rite ?= { rite_has_parameter = tenet_scholasticism_study_scripture_bonus }
		has_personal_tenet_flag = tenet_scholasticism_study_scripture_bonus
	}
}

pam_study_scripture_confession_tenet_trigger = {
	OR = {
		rite ?= { rite_has_parameter = tenet_confession_study_scripture_bonus }
		has_personal_tenet_flag = tenet_confession_study_scripture_bonus
	}
}

# An eloquent cleric root could learn homiletics from
pam_study_scripture_preacher_trigger = {
	diplomacy >= 8
	is_physically_able_ai_adult = yes
	NOT = { has_relation_rival = root }
	has_trait_malicious_trigger = no
	pam_is_investiture_clergy_trigger = yes
}

pam_study_scripture_mysticism_trigger = {
	OR = {
		has_trait = lifestyle_mystic
		rite ?= {
			OR = {
				rite_has_tenet = tenet_hesychasm
				rite_has_tenet = tenet_anachoresis
				rite_has_tenet = tenet_esotericism
				rite_has_tenet = tenet_astrology
				rite_has_tenet = tenet_gnosticism
				rite_has_tenet = tenet_inner_journey
				rite_has_tenet = tenet_mystical_birthright
			}
		}
		has_personal_tenet = tenet_hesychasm
		has_personal_tenet = tenet_anachoresis
		has_personal_tenet = tenet_esotericism
		has_personal_tenet = tenet_astrology
		has_personal_tenet = tenet_gnosticism
		has_personal_tenet = tenet_inner_journey
		has_personal_tenet = tenet_mystical_birthright
	}
}

# A vow of chastity, or a marriage the Church has just dissolved, keeps a character off the marriage market.
pam_under_clerical_marriage_restraint_trigger = {
	OR = {
		has_character_modifier = clerical_chastity_vow_modifier
		has_variable = pam_clerical_divorce_enforced
	}
}

# Only the AI is held to this: a player may still discard the vow's intent and arrange the match anyway.
pam_ai_marriage_respects_clerical_restraint_trigger = {
	trigger_if = {
		limit = {
			scope:actor ?= { is_ai = yes }
		}
		NOR = {
			scope:secondary_actor ?= { pam_under_clerical_marriage_restraint_trigger = yes }
			scope:secondary_recipient ?= { pam_under_clerical_marriage_restraint_trigger = yes }
		}
	}
}

# $SLOT$ is the papabile shortlist seat being claimed
pam_papabile_slot_open_to_trigger = {
	exists = clerical_elector_title
	NOT = { has_variable = pam_refuses_papacy }
	pam_college_election_title_has_electors_trigger = yes
	NOT = {
		pam_is_college_election_candidate_trigger = { SOURCE = $SLOT$ }
	}
}

pam_papal_ruling_rite_has_parameter_trigger = {
	trigger_if = {
		limit = { exists = faith.religious_head_title }
		faith.religious_head_title.holder.rite ?= { rite_has_parameter = $PARAMETER$ }
	}
	trigger_else = {
		title:k_papal_state.holder.rite ?= { rite_has_parameter = $PARAMETER$ }
	}
}

pam_papabile_barred_by_clerical_gender_trigger = {
	trigger_if = {
		limit = { is_female = yes }
		pam_papal_ruling_rite_has_parameter_trigger = { PARAMETER = clergy_must_be_male }
	}
	trigger_else = {
		pam_papal_ruling_rite_has_parameter_trigger = { PARAMETER = clergy_must_be_female }
	}
}

# Divergent rites only: an Ecumenical Council can give the main rite Lay Clergy, and its College must still have candidates
pam_papabile_barred_by_lay_clergy_trigger = {
	rite ?= { rite_has_doctrine = doctrine_theocracy_lay_clergy }
	rite != faith.main_rite
}

# Only the patron's own personal tenets may be imposed, and not one the order already advocates.
pam_reform_order_tenet_option_shown_trigger = {
	scope:actor = {
		has_personal_tenet = $TENET$
		any_patroned_holy_order = {
			leader = scope:recipient
			NOT = { order_tenet = tenet:$TENET$ }
		}
	}
}

monastic_movement_potential_leader_trigger = {
	is_adult = yes
	is_ruler = no
	has_trait = devoted
	is_physically_able_ai_adult = yes
	in_diplomatic_range = root
	OR = {
		opinion = {
			target = root
			value >= 100
		}
		root = {
			has_usable_hook = prev
		}
	}
}
monastic_movement_non_order_size_reqs_trigger = {
	OR = {
		custom_tooltip = {
			text = character_is_rite_head_first
			rite.head_of_rite ?= root
		}
		custom_tooltip = {
			text = house_member_potential_leader
			house ?= {
				any_house_member = {
					monastic_movement_potential_leader_trigger = yes
				}
			}
		}
		trigger_if = {
			limit = {
				government_has_flag = government_is_ecclesiastical
			}
			custom_tooltip = {
				text = has_cloister_building_tt
				domicile ?= {
					has_domicile_parameter = estate_cloister_monastic_movement
				}
			}
		}
	}
}

# Core tenet or personal pick; shared by scheme success/prediction
pam_study_rite_tenet_bonus_trigger = {
	OR = {
		rite ?= { rite_has_parameter = $PARAM$ }
		has_personal_tenet_flag = $PARAM$
	}
}

can_send_make_external_ruler_puppet_trigger = {
	custom_tooltip = {
		text = make_external_ruler_puppet_tt
		OR = {
			$ACTOR$ = {
				is_clergy = yes
				is_vassal_of = $RECIPIENT$
			}
			AND = {
				$ACTOR$ = {
					has_trait = seducer
				}
				trigger_if = {
					limit = {
						$RECIPIENT$ = {
							highest_held_title_tier >= tier_kingdom
						}
					}
					$RECIPIENT$ = {
						has_relation_soulmate = $ACTOR$
					}
				}
				trigger_else = {
					$RECIPIENT$ = {
						has_relation_lover = $ACTOR$
					}
				}
			}
			AND = {
				$ACTOR$ = {
					has_trait = torturer
				}
				$RECIPIENT$ = {
					is_imprisoned_by = $ACTOR$
					is_in_prison_type = dungeon
				}
			}
			AND = {
				$ACTOR$ = {
					has_trait = family_first
				}
				$RECIPIENT$ = {
					is_close_family_of = $ACTOR$
				}
			}
			$ACTOR$ = {
				has_trait = schemer
				trigger_if = {
					limit = {
						$RECIPIENT$ = {
							highest_held_title_tier >= tier_kingdom
						}
					}
					has_strong_hook = $RECIPIENT$
				}
				trigger_else = {
					has_hook = $RECIPIENT$
				}
			}
			$ACTOR$ = {
				has_trait = diplomat
				is_allied_to = $RECIPIENT$
			}
			AND = {
				$ACTOR$ = {
					has_trait = gallant
				}
				$RECIPIENT$ = {
					is_spouse_of = $ACTOR$
				}
			}
			AND = {
				$ACTOR$ = {
					is_diarch_of_target = $RECIPIENT$
				}
				$RECIPIENT$ = {
					has_diarchy_parameter = diarchy_type_is_entrenched_regency
				}
			}
			AND = {
				exists = $RECIPIENT$.religious_head_or_challenger
				$RECIPIENT$.religious_head_or_challenger = $ACTOR$
			}
			AND = {
				exists = $RECIPIENT$.rite.head_of_rite
				$RECIPIENT$.rite.head_of_rite = $ACTOR$
			}
		}
	}
	$ACTOR$ = {
		trigger_if = {
			limit = {
				$RECIPIENT$ = {
					highest_held_title_tier >= tier_empire
				}
			}
			custom_tooltip = {
				text = pam_empire_dread_or_strong_hook_tt
				OR = {
					has_strong_hook = $RECIPIENT$
					$RECIPIENT$ = {
						has_dread_level_towards = {
							target = $ACTOR$
							level >= 2
						}
					}
					$RECIPIENT$.religious_head_or_challenger ?= $ACTOR$
				}
			}
		}
		trigger_else_if = {
			limit = {
				$RECIPIENT$ = {
					highest_held_title_tier >= tier_kingdom
				}
			}
			custom_tooltip = {
				text = piety_level_4_dread_or_strong_hook_tt
				OR = {
					piety_level >= 4
					has_strong_hook = $RECIPIENT$
					$RECIPIENT$ = {
						has_dread_level_towards = {
							target = $ACTOR$
							level >= 2
						}
					}
					$RECIPIENT$.religious_head_or_challenger ?= $ACTOR$
				}
			}
		}
		trigger_else = {
			custom_tooltip = {
				text = piety_level_3_dread_or_hook_tt
				OR = {
					piety_level >= 3
					has_hook = $RECIPIENT$
					$RECIPIENT$ = {
						has_dread_level_towards = {
							target = $ACTOR$
							level >= 2
						}
					}
					$RECIPIENT$.religious_head_or_challenger ?= $ACTOR$
				}
			}
		}
	}
}
