﻿namespace = pam_decision_events

###########################################
## 0001 - Take Confession                ##
## 0002 - Act of Devotion                ##
## 0003 - Provide Spiritual Guidance     ##
## 0004 - Establish Theocratic Dynasty   ##
## 0005-0007 - Last Rites                ##
## 0008 - Levy the Tithes                ##
## 0009 - Feudal to theocracy            ##
## 0010 - Feudal to theocracy - vassal   ##
## 0015 - Promulgate Word of Pious Deeds ##
## 0016 - Khachkar catalyst on dynast death (hidden) ##
## 0017 - Raise an Ancestral Khachkar (confirmation) ##
## 0011 - HoF Tenet Stance notification  ##
## 0018 - HoF Crusade Age core tenet     ##
## 0019 - Secure Pilgrim Routes          ##
## 0021 - Proclaim the Living Word       ##
## 0032 - Develop Clerical Court         ##
###########################################

# Take Confession
pam_decision_events.0001 = {
	type = character_event
	title = pam_decision_events.0001.t
	desc = {
		desc = pam_decision_events.0001.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0001.desc.bad
			}
			triggered_desc = {
				trigger = {
					spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0001.desc.good
			}
			desc = pam_decision_events.0001.desc.neutral
		}
		triggered_desc = {
			trigger = { num_sinful_traits > 0 }
			desc = pam_decision_events.0001.desc.sin.preamble
		}
		desc = {
			first_valid = {
				triggered_desc = {
					trigger = {
						has_trait = lustful
						rite = { trait_is_sin_rite = lustful }
					}
					desc = pam_decision_events.0001.desc.sin.lustful
				}
				triggered_desc = {
					trigger = {
						has_trait = chaste
						rite = { trait_is_sin_rite = chaste }
					}
					desc = pam_decision_events.0001.desc.sin.chaste
				}
				triggered_desc = {
					trigger = {
						has_trait = gluttonous
						rite = { trait_is_sin_rite = gluttonous }
					}
					desc = pam_decision_events.0001.desc.sin.gluttonous
				}
				triggered_desc = {
					trigger = {
						has_trait = temperate
						rite = { trait_is_sin_rite = temperate }
					}
					desc = pam_decision_events.0001.desc.sin.temperate
				}
				triggered_desc = {
					trigger = {
						has_trait = greedy
						rite = { trait_is_sin_rite = greedy }
					}
					desc = pam_decision_events.0001.desc.sin.greedy
				}
				triggered_desc = {
					trigger = {
						has_trait = generous
						rite = { trait_is_sin_rite = generous }
					}
					desc = pam_decision_events.0001.desc.sin.generous
				}
				triggered_desc = {
					trigger = {
						has_trait = lazy
						rite = { trait_is_sin_rite = lazy }
					}
					desc = pam_decision_events.0001.desc.sin.lazy
				}
				triggered_desc = {
					trigger = {
						has_trait = diligent
						rite = { trait_is_sin_rite = diligent }
					}
					desc = pam_decision_events.0001.desc.sin.diligent
				}
				triggered_desc = {
					trigger = {
						has_trait = wrathful
						rite = { trait_is_sin_rite = wrathful }
					}
					desc = pam_decision_events.0001.desc.sin.wrathful
				}
				triggered_desc = {
					trigger = {
						has_trait = calm
						rite = { trait_is_sin_rite = calm }
					}
					desc = pam_decision_events.0001.desc.sin.calm
				}
				triggered_desc = {
					trigger = {
						has_trait = patient
						rite = { trait_is_sin_rite = patient }
					}
					desc = pam_decision_events.0001.desc.sin.patient
				}
				triggered_desc = {
					trigger = {
						has_trait = impatient
						rite = { trait_is_sin_rite = impatient }
					}
					desc = pam_decision_events.0001.desc.sin.impatient
				}
				triggered_desc = {
					trigger = {
						has_trait = arrogant
						rite = { trait_is_sin_rite = arrogant }
					}
					desc = pam_decision_events.0001.desc.sin.arrogant
				}
				triggered_desc = {
					trigger = {
						has_trait = humble
						rite = { trait_is_sin_rite = humble }
					}
					desc = pam_decision_events.0001.desc.sin.humble
				}
				triggered_desc = {
					trigger = {
						has_trait = deceitful
						rite = { trait_is_sin_rite = deceitful }
					}
					desc = pam_decision_events.0001.desc.sin.deceitful
				}
				triggered_desc = {
					trigger = {
						has_trait = honest
						rite = { trait_is_sin_rite = honest }
					}
					desc = pam_decision_events.0001.desc.sin.honest
				}
				triggered_desc = {
					trigger = {
						has_trait = craven
						rite = { trait_is_sin_rite = craven }
					}
					desc = pam_decision_events.0001.desc.sin.craven
				}
				triggered_desc = {
					trigger = {
						has_trait = brave
						rite = { trait_is_sin_rite = brave }
					}
					desc = pam_decision_events.0001.desc.sin.brave
				}
				triggered_desc = {
					trigger = {
						has_trait = shy
						rite = { trait_is_sin_rite = shy }
					}
					desc = pam_decision_events.0001.desc.sin.shy
				}
				triggered_desc = {
					trigger = {
						has_trait = gregarious
						rite = { trait_is_sin_rite = gregarious }
					}
					desc = pam_decision_events.0001.desc.sin.gregarious
				}
				triggered_desc = {
					trigger = {
						has_trait = ambitious
						rite = { trait_is_sin_rite = ambitious }
					}
					desc = pam_decision_events.0001.desc.sin.ambitious
				}
				triggered_desc = {
					trigger = {
						has_trait = content
						rite = { trait_is_sin_rite = content }
					}
					desc = pam_decision_events.0001.desc.sin.content
				}
				triggered_desc = {
					trigger = {
						has_trait = arbitrary
						rite = { trait_is_sin_rite = arbitrary }
					}
					desc = pam_decision_events.0001.desc.sin.arbitrary
				}
				triggered_desc = {
					trigger = {
						has_trait = just
						rite = { trait_is_sin_rite = just }
					}
					desc = pam_decision_events.0001.desc.sin.just
				}
				triggered_desc = {
					trigger = {
						has_trait = cynical
						rite = { trait_is_sin_rite = cynical }
					}
					desc = pam_decision_events.0001.desc.sin.cynical
				}
				triggered_desc = {
					trigger = {
						has_trait = zealous
						rite = { trait_is_sin_rite = zealous }
					}
					desc = pam_decision_events.0001.desc.sin.zealous
				}
				triggered_desc = {
					trigger = {
						has_trait = paranoid
						rite = { trait_is_sin_rite = paranoid }
					}
					desc = pam_decision_events.0001.desc.sin.paranoid
				}
				triggered_desc = {
					trigger = {
						has_trait = trusting
						rite = { trait_is_sin_rite = trusting }
					}
					desc = pam_decision_events.0001.desc.sin.trusting
				}
				triggered_desc = {
					trigger = {
						has_trait = compassionate
						rite = { trait_is_sin_rite = compassionate }
					}
					desc = pam_decision_events.0001.desc.sin.compassionate
				}
				triggered_desc = {
					trigger = {
						has_trait = callous
						rite = { trait_is_sin_rite = callous }
					}
					desc = pam_decision_events.0001.desc.sin.callous
				}
				triggered_desc = {
					trigger = {
						has_trait = stubborn
						rite = { trait_is_sin_rite = stubborn }
					}
					desc = pam_decision_events.0001.desc.sin.stubborn
				}
				triggered_desc = {
					trigger = {
						has_trait = fickle
						rite = { trait_is_sin_rite = fickle }
					}
					desc = pam_decision_events.0001.desc.sin.fickle
				}
				triggered_desc = {
					trigger = {
						has_trait = eccentric
						rite = { trait_is_sin_rite = eccentric }
					}
					desc = pam_decision_events.0001.desc.sin.eccentric
				}
				triggered_desc = {
					trigger = {
						has_trait = vengeful
						rite = { trait_is_sin_rite = vengeful }
					}
					desc = pam_decision_events.0001.desc.sin.vengeful
				}
				triggered_desc = {
					trigger = {
						has_trait = forgiving
						rite = { trait_is_sin_rite = forgiving }
					}
					desc = pam_decision_events.0001.desc.sin.forgiving
				}
				triggered_desc = {
					trigger = {
						has_trait = sadistic
						rite = { trait_is_sin_rite = sadistic }
					}
					desc = pam_decision_events.0001.desc.sin.sadistic
				}
			}
		}
		desc = pam_decision_events.0001.desc.outro
	}
	theme = christian
	
	left_portrait = { 
		character = root
		animation = prayer_standing
	}

	right_portrait = { 
		character = scope:chaplain
		animation = personality_zealous
	}

	immediate = {
		cp:councillor_court_chaplain = { save_scope_as = chaplain }
		random_secret = {
			limit = {
				NOT = { is_known_by = scope:chaplain }
			}
			save_scope_as = my_secret
		}
	}
	
	option = { # Amen
		name = pam_decision_events.0001.a
		if = {
			limit = {
				spiritual_fulfillment < 0
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			stress_and_fulfillment_impact = {
				base = minor_stress_impact_loss
				zealous = medium_stress_impact_loss
				humble = medium_stress_impact_loss
			}
		}
		else = {
			stress_and_fulfillment_impact = {
				base = medium_stress_impact_loss
				zealous = medium_stress_impact_loss
				humble = medium_stress_impact_loss
			}
		}
		if = {
			limit = {
				exists = scope:my_secret
			}
			random = {
				chance = 50
				scope:my_secret = { reveal_to = scope:chaplain }
			}
		}
		custom_tooltip = pam_decision_events.0001.a.confession_random_outcome
		hidden_effect = {
			random_list = {
				40 = { # Nothing happens
					send_interface_toast = {
						title = pam_decision_events.0001.a.piety
						left_icon = root
						if = {
							limit = {
								has_personal_tenet_flag = tenet_confession_decision_bonus
							}
							add_piety = medium_piety_gain
						}
						else = {
							add_piety = minor_piety_gain
						}
					}
				}
				5 = { # Priest gets hook
					trigger = {
						scope:chaplain = {
							can_add_hook = {
								target = root
								type = weak_blackmail_hook_no_secret
							}
						}
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						factor = 0
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.hook
						left_icon = root
						right_icon = scope:chaplain
						scope:chaplain = {
							add_hook = {
								target = root
								type = weak_blackmail_hook_no_secret
							}
						}
					}
				}
				5 = { # Priest loses opinion
					trigger = {
						scope:chaplain = {
							NOT = { has_trait = cynical }
						}
						num_sinful_traits > 0
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						factor = 0
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.opinion_malus
						left_icon = root
						right_icon = scope:chaplain
						scope:chaplain = {
							add_opinion = {
								target = root
								opinion = -25
								modifier = impious_opinion
							}
						}
					}
				}
				10 = { # You learn of a courtier's secret
					trigger = {
						any_courtier_or_guest = {
							any_secret = {
								NOT = { is_known_by = root }
							}
						}
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						add = 50
					}
					random_courtier_or_guest = {
						limit = {
							any_secret = {
								NOT = { is_known_by = root }
							}
						}
						save_scope_as = my_courtier
						random_secret = {
							limit = {
								NOT = { is_known_by = root }
							}
							save_scope_as = courtier_secret
						}
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.courtier_secret
						left_icon = root
						right_icon = scope:my_courtier
						scope:courtier_secret = { reveal_to = root }
					}
				}
				5 = { # You hear someone else confessing
					modifier = {
						has_lifestyle = intrigue_lifestyle
						add = 5
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						add = 30
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.random_confession
						left_icon = root
						add_intrigue_lifestyle_xp = medium_lifestyle_xp
					}
				}
				10 = { # Chaplain asks you to go on pilgrimage
					trigger = {
						NOT = { has_character_modifier = pam_confession_penalty_modifier }
						rite = {
							NOT = { rite_has_doctrine = doctrine_pilgrimage_forbidden }
						}
					}
					modifier = {
						NOT = { has_trait = pilgrim }
						add = 5
					}
					modifier = {
						num_sinful_traits > 0
						add = 5
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						factor = 0
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.pilgrimage
						left_icon = root
						right_icon = scope:chaplain
						add_character_modifier = { # Removed when you do the thing
							modifier = pam_confession_penalty_modifier
						}
						set_variable = { # To check what they asked you to do
							name = pam_confession_penalty_var
							value = flag:pilgrimage
						}
					}
				}
				5 = { # Chaplain asks you to employ a High Almoner
					trigger = {
						can_employ_court_position_type = high_almoner_court_position
						NOT = { employs_court_position = high_almoner_court_position }
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						factor = 0
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.high_almoner
						left_icon = root
						right_icon = scope:chaplain
						add_character_modifier = { # Removed when you do the thing
							modifier = pam_confession_penalty_modifier
						}
						set_variable = { # To check what they asked you to do
							name = pam_confession_penalty_var
							value = flag:high_almoner
						}
					}
				}
				15 = { # Chaplain asks you to convert astray family member
					trigger = {
						OR = {
							any_spouse = { faith != root.faith }
							any_child = { faith != root.faith }
						}
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						add = -10
					}
					random_spouse = {
					   limit = { faith != root.faith }
					   save_scope_as = target
					}
					if = {
						limit = {
							NOT = { exists = scope:target }
						}
						random_child = {
							limit = { faith != root.faith }
							save_scope_as = target
						}
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.conversion
						left_icon = root
						right_icon = scope:target
						add_character_modifier = { # Removed when you do the thing
							modifier = pam_confession_penalty_modifier
						}
						set_variable = { # To check what they asked you to do
							name = pam_confession_penalty_var
							value = scope:target
						}
					}
				}
				5 = { # Chaplain asks you to not go to war for 5y
					modifier = {
						rite = {
							OR = {
								rite_has_tenet = tenet_pacifism
								rite_has_tenet = tenet_peace_of_god
							}
						}
						add = 10
					}
					modifier = {
						has_personal_tenet_flag = tenet_confession_decision_bonus
						factor = 0
					}
					send_interface_toast = {
						title = pam_decision_events.0001.a.no_war
						left_icon = root
						right_icon = scope:chaplain
						add_character_modifier = {
							modifier = pam_confession_penalty_modifier
							years = 5 # It'll go away on its own for this one
						}
						set_variable = { # To check what they asked you to do
							name = pam_confession_penalty_var
							value = flag:no_war
						}
					}
				}
			}
		}
		ai_chance = {
			base = 100
		}
	}
}

# Act of Devotion
pam_decision_events.0002 = {
	type = character_event
	title = pam_decision_events.0002.t
	desc = {
		desc = pam_decision_events.0002.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_realm_law = crowned_king
						has_realm_law = crowned_emperor
					}
				}
				desc = pam_decision_events.0002.desc.crowned
			}
			desc = pam_decision_events.0002.desc.uncrowned
		}
		desc = pam_decision_events.0002.desc.mid
		first_valid = {
			triggered_desc = {
				trigger = {
					spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0002.desc.bad
			}
			triggered_desc = {
				trigger = {
					spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0002.desc.good
			}
			desc = pam_decision_events.0002.desc.neutral
		}
		desc = pam_decision_events.0002.desc.outro
	}
	theme = christian

	left_portrait = { 
		character = root
		animation = personality_zealous
	}
	right_portrait = { 
		character = scope:my_chaplain
		animation = preaching_no_book
		camera = camera_event_center_close_to_right
	}
	override_effect_2d = {
		reference = legend_glow
	}
	
	immediate = {
		save_scope_as = devotional_guy
		if = {
			limit = { exists = cp:councillor_court_chaplain }
			cp:councillor_court_chaplain = { save_scope_as = my_chaplain }
		}
		else_if = {
			limit = {
				any_vassal = {
					government_has_flag = government_is_theocracy
				}
			}
			ordered_vassal = {
				order_by = primary_title.tier
				limit = { government_has_flag = government_is_theocracy }
				save_scope_as = my_chaplain
			}
		}
		else = {
			create_character = {
				template = priest_character_template
				location = root.location
				save_scope_as = my_chaplain
			}
		}
	}
	
	option = { # Push harder
		show_as_unavailable = { always = yes }
		name = pam_decision_events.0002.a
		trigger = {
			custom_tooltip = {
				text = pam_decision_act_of_devotion_fame_tooltip
				prestige_level < 3
			}
		}
		add_piety_level = -1
		add_prestige_level = 1
		add_prestige = medium_prestige_gain

		ai_chance = {
			base = 0
		}
	}
	
	option = { # Collect your prestige
		name = pam_decision_events.0002.b
		add_prestige = major_prestige_gain

		ai_chance = {
			base = 100
		}
	}
}

# Provide Spiritual Guidance
pam_decision_events.0003 = {
	type = character_event
	title = pam_decision_events.0003.t
	desc = {
		desc = pam_decision_events.0003.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target_child.spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0003.desc.bad
			}
			triggered_desc = {
				trigger = {
					scope:target_child.spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0003.desc.good
			}
			desc = pam_decision_events.0003.desc.neutral
		}
	}
	theme = christian

	left_portrait = { 
		character = root
		animation = prayer_standing
	}
	right_portrait = { 
		character = scope:target_child
		triggered_animation = {
			trigger = {
				has_trait = cynical
			}
			animation = boredom
		}
		triggered_animation = {
			trigger = {
				has_personality_submissive_trigger = yes
			}
			animation = worry
		}
		triggered_animation = {
			trigger = {
				OR = {
					learning >= high_skill_rating
					has_trait = theologian
					has_trait = zealous
					has_lifestyle = learning_lifestyle
				}
			}
			animation = interested
		}
		animation = prayer_standing
	}
	override_background = { reference = bedchamber } # Intentional, we talk about the private chapel
	
	immediate = {
		if = {
			limit = { scope:primary_heir = yes }
			primary_heir = { save_scope_as = target_child }
		}
		else = {
			random_relation = {
				type = favorite_child
				save_scope_as = target_child
			}
		}
		scope:target_child = { set_variable = pam_mentor_sp_taken_var }
	}
	
	option = { # Push them _more_
		name = pam_decision_events.0003.a
		if = {
			limit = {
				NOT = { has_relation_grudge = scope:target_child }
			}
			scope:target_child = {
				set_relation_grudge = {
					target = root
					reason = grudge_pressured_me_sp
				}
			}
		}
		scope:target_child = {
			if = {
				limit = {
					is_available_quick = {
						advanced_ruler = yes
					}
				}
				change_spiritual_fulfillment = 50
			}
		}

		ai_chance = {
			base = 0
			modifier = {
				has_trait = callous
				add = 25
			}
		}
	}
	
	option = { # Listen, child
		name = pam_decision_events.0003.b
		scope:target_child = {
			if = {
				limit = {
					is_available_quick = {
						advanced_ruler = yes
					}
				}
				change_spiritual_fulfillment = 25
			}
		}

		ai_chance = {
			base = 100
		}
	}
	
	after = {
		custom_tooltip = pam_decision_events.0003.after_tt
		hidden_effect = {
			random_list = {
				5 = {
					trigger = {
						scope:target_child = {
							any_secret = {
								NOT = { is_known_by = root }
							}
						}
					}
					modifier = {
						scope:target_child = { has_any_good_relationship_with_root_trigger = yes }
						add = 5
					}
					send_interface_toast = {
						title = pam_decision_events.0003.secret
						right_icon = scope:target_child
						scope:target_child = {
							random_secret = {
								limit = {
									NOT = { is_known_by = root }
								}
								reveal_to = root
							}
						}
					}
				}
				1 = {
					trigger = {
						scope:target_child = {
							NOT = { has_trait = lifestyle_mystic }
						}
					}
					send_interface_toast = {
						title = pam_decision_events.0003.mystic
						right_icon = scope:target_child
						scope:target_child = { add_trait = lifestyle_mystic }
					}
				}
				2 = {
					modifier = {
						scope:target_child = { has_lifestyle = learning_lifestyle }
						add = 2
					}
					send_interface_toast = {
						title = pam_decision_events.0003.learning
						right_icon = scope:target_child
						scope:target_child = { add_learning_skill = 1 }
					}
				}
				3 = {
					send_interface_toast = {
						title = pam_decision_events.0003.education
						right_icon = scope:target_child
						scope:target_child = { gain_appropriate_lifestyle_medium_xp_effect = yes }
					}
				}
				3 = {
					modifier = {
						scope:target_child = { has_personality_emotional_trigger = yes }
						add = 2
					}
					send_interface_toast = {
						title = pam_decision_events.0003.stress
						right_icon = scope:target_child
						scope:target_child = { add_stress = medium_stress_gain }
					}
				}
				5 = {
					trigger = {
						scope:target_child = { num_of_relation_lover > 0 }
					}
					modifier = {
						scope:target_child = {
							any_relation = {
								type = lover
								num_sinful_traits > 0
							}
						}
					}
					send_interface_toast = {
						title = pam_decision_events.0003.lover
						right_icon = scope:target_child
						scope:target_child = {
							random_relation = {
								type = lover
								save_scope_as = lover_scope
							}
							remove_relation_lover = scope:lover_scope
						}
					}
				}
				2 = {
					trigger = {
						num_sinful_traits > 0
					}
					send_interface_toast = {
						title = pam_decision_events.0003.trait
						right_icon = scope:target_child
						scope:target_child = { flip_sinful_trait_effect = yes }
					}
				}
			}
		}
	}
}

# Establish Theocratic Dynasty
pam_decision_events.0004 = {
	type = character_event
	title = pam_decision_events.0004.t
	desc = pam_decision_events.0004.desc
	theme = dynasty

	left_portrait = { 
		character = root
		animation = acknowledging
	}
	override_effect_2d = {
		reference = legend_glow
	}
	
	immediate = {
		create_dynasty = {
			spread_to_descendants = yes
		}
		# CE3 achievement: The Father, the Son, and the Holy Grandson — the chain starts at dynasty establishment
		if = {
			limit = {
				is_ai = no
				any_held_title = { has_clerical_region = yes }
			}
			set_variable = {
				name = ce3_theocrat_dynasty_chain
				value = 1
			}
		}
	}
	
	option = { # Let's goooo
		name = pam_decision_events.0004.a
		custom_tooltip = pam_decision_events.0004.tt
		pam_create_theocratic_dynasty_extra_effects = yes
		ai_chance = {
			base = 100
		}
	}
}

#Last Rites 0005-0007
#Laurence Kennedy

# Last Rites - My Final Hour
pam_decision_events.0005 = {
	type = character_event
	title = pam_decision_events.0005.t
	window = big_event_window
	theme = christian 
	override_background = {
		trigger = {
            is_travelling = no
        }
        reference = bedchamber
	}
	override_background = {
        trigger = {
            is_travelling = yes
        }
        reference = ep3_relaxing_tent
    }
	desc = {
		triggered_desc = {
			trigger = {
				exists = scope:death_trait
			}
			desc = pam_decision_events.0005.desc.healthtrait
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0005.desc.bad
			}
			triggered_desc = {
				trigger = {
					spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0005.desc.good
			}
			desc = pam_decision_events.0005.desc.neutral
		}
		
		triggered_desc = {
			trigger = {
				exists = scope:fam_or_friend_1
			}
			desc = pam_decision_events.0005.desc.notalone
		}
		triggered_desc = {
			trigger = {
				NOT = {
					exists = scope:fam_or_friend_1
				}
			}
			desc = pam_decision_events.0005.desc.alone
		}
		desc = pam_decision_events.0005.desc.outro
	}

	left_portrait = { 
		character = scope:fam_or_friend_1
		animation = crying
	}
	center_portrait = {
		character = scope:soon_dead
		triggered_animation = {
			trigger = {
				spiritual_fulfillment <= -50
			}
			animation = poison
		}
		triggered_animation = {
			trigger = {
				spiritual_fulfillment <= -25
			}
			animation = paranoia
		}		
		animation = prayer
	}
	right_portrait = { 
		character = scope:confessor
		animation = divine_gesture_3
	}

	#gathered loved ones
	lower_left_portrait = {
		character = scope:player_heir
	}
	lower_center_portrait = {
		character = scope:fam_or_friend_2
	}
	lower_right_portrait = {
		character = scope:fam_or_friend_3
	}

	immediate = {
		#Root
		root = { save_scope_as = soon_dead }
		#Find a priest
		if = {
			limit = {
				cp:councillor_court_chaplain ?= {
					basic_is_available_ai = yes
				}
			}
			cp:councillor_court_chaplain ?= { save_scope_as = confessor }
		}
		#Find a different clergy member in the area
		else_if = {
			limit = {
				NOT = {
					exists = scope:confessor
				}
			}
			random_pool_character = {
				province = root.location
				limit = {
					faith = root.faith
					is_clergy = yes
					basic_is_available_ai = yes
				}
				save_scope_as = confessor
			}
		}
		else = {
			#make a priest
			create_character = {
				template = virtuous_priest_character_template
				dynasty = none
				location = root.location
				culture = root.location.culture
				faith = root.faith
				rite = root.rite
				save_scope_as = confessor
			}
			add_character_flag = {
				flag = fake_confessor
				days = 10
			}
		}
		#Find Player Heir
		scope:soon_dead.player_heir ?= { save_scope_as = player_heir}
		#Find Friends & Family
		ordered_courtier = {
			limit = {
				NOT =  {
					this = scope:soon_dead.player_heir
				}
				OR = {
					has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:soon_dead }
					is_close_family_of = scope:soon_dead
					is_spouse_of = scope:soon_dead
				}	
			}
	
			#Order by how much they like you, but still prioritize spouse
			order_by = {
				value = "opinion(root)"
				if = {
					limit = {
						is_spouse_of = root
					}
					add = 100
				} 
			}
			max = 3
			#always put this when there is a max or it will be unhappy who knows why
			check_range_bounds = no
			if = {
				limit = { exists = scope:fam_or_friend_2 }
				save_scope_as = fam_or_friend_3
			}
			else_if = {
				limit = { exists = scope:fam_or_friend_1 }
				save_scope_as = fam_or_friend_2
			}
			else = { save_scope_as = fam_or_friend_1 }
		}
		#Find a secret
		random_secret = {
			limit = {
				NOT = { is_known_by = scope:confessor }
			}
			save_scope_as = my_secret
		}
		#Find a reason for your death
		random_character_trait = {
			limit = {
				has_trait_category = health
				has_trait_flag = lowers_health
			}
			save_scope_as = death_trait
		} 

	}
	
	option = { # Time for penance 
		name = pam_decision_events.0005.a
		change_spiritual_fulfillment = major_spiritual_fulfillment_value
		stress_and_fulfillment_impact = {
			base = minor_stress_impact_loss
			zealous = medium_stress_impact_loss
			humble = medium_stress_impact_loss
		}
		
		trigger_event = {
			id = pam_decision_events.0006
		}
		ai_chance = {
			base = 100
		}
	}
	option = { # No confession for me
		trigger = {
			OR = {
				has_trait = cynical
				any_secret = { type = secret_non_believer }
				spiritual_fulfillment < -20
			} 
		}
		name = pam_decision_events.0005.b
		change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
		stress_and_fulfillment_impact = {
			cynical = major_stress_impact_loss
			paranoid = major_stress_impact_loss
		}
		trigger_event = {
			id = pam_decision_events.0006
		}
		ai_chance = {
			base = 0
		}
	}
}

#Last Rites - Sacrament of Penance 
pam_decision_events.0006 = {
	type = character_event
	title = pam_decision_events.0006.t
	window = big_event_window
	theme = christian
	override_background = {
		trigger = {
            is_travelling = no
        }
        reference = bedchamber
	}
	override_background = {
        trigger = {
            is_travelling = yes
        }
        reference = ep3_relaxing_tent
    }
	desc = {
		desc = pam_decision_events.0006.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0006.desc.bad
			}
			triggered_desc = {
				trigger = {
					spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0006.desc.good
			}
			desc = pam_decision_events.0006.desc.neutral
		}
		#Sin based on a sinful trait
		triggered_desc = {
			trigger = {
				exists = scope:my_sinful_trait
			}
			desc = pam_decision_events.0006.desc.my_sinful_trait_sin
		}
		#Sin based on a sinful secret
		triggered_desc = {
			trigger = {
				exists = scope:my_secret
			}
			desc = pam_decision_events.0006.desc.secret_sin
		}
		#Sin based on a criminal trait
		triggered_desc = {
			trigger = {
				exists = scope:my_criminal_trait
			}
			desc = pam_decision_events.0006.desc.my_criminal_trait_sin
		}
		#Sin desc if you have neither of the above
		triggered_desc = {
			trigger = {
				NOR = {
					exists = scope:my_secret
					exists = scope:my_sinful_trait
				}
				spiritual_fulfillment < 50
			}
			desc = pam_decision_events.0006.desc.sin_default
		}
		#Sin desc if you have neither of the above
		triggered_desc = {
			trigger = {
				NOR = {
					exists = scope:my_secret
					exists = scope:my_sinful_trait
				}
				spiritual_fulfillment >= 50
			}
			desc = pam_decision_events.0006.desc.sin_default_good_sp
		}		
		desc = pam_decision_events.0006.desc.outro
	}

	left_portrait = { 
		character = scope:soon_dead
		animation = beg
		camera = camera_council
	}
	right_portrait = { 
		character = scope:confessor
		animation = personality_zealous
		camera = camera_body_left
	}

	immediate = {
		#Find HoR
		scope:soon_dead.faith.religious_head = { save_scope_as = religious_head }
		#Find a secret
		random_secret = {
			limit = {
				NOT = { is_known_by = scope:confessor }
			}
			save_scope_as = my_secret
		}
		#Find a sinful trait
		random_character_trait = {
			limit = {
				save_temporary_scope_as = temp_trait
				root.rite = {		
					trait_is_sin_rite = scope:temp_trait
				}
			}
			save_scope_as = my_sinful_trait
		}	
		#Find Criminal trait
		random_character_trait = {
			limit = {
				save_temporary_scope_as = temp_trait
				root = {	
					trait_is_criminal_in_rite_trigger = { 
						TRAIT = scope:temp_trait 
						RITE = rite 
						GENDER_CHARACTER = root 
					}
				}
			}
			save_scope_as = my_criminal_trait
		}
	}

	option = { # Confess 
		name = pam_decision_events.0006.a
		change_spiritual_fulfillment = medium_spiritual_fulfillment_value
		stress_and_fulfillment_impact = {
			base = minor_stress_impact_loss
			zealous = medium_stress_impact_loss
			humble = medium_stress_impact_loss
		} 
		if = {
			limit = {
				exists = scope:my_secret
			}
			random = {
				chance = 50
				scope:my_secret = { reveal_to = scope:confessor }
			}
			random = {
				chance = 10
				scope:my_secret = { expose_secret = scope:soon_dead }
			}
		}
		trigger_event = {
			id = pam_decision_events.0007
		}
		ai_chance = {
			base = 100
		}
	}
	option = { # No need to share everything!
		trigger = {
			exists = scope:my_secret
		}
		name = pam_decision_events.0006.b
		flavor = pam_decision_events.0006.b.flavor
		
		change_spiritual_fulfillment = major_spiritual_fulfillment_loss
		stress_and_fulfillment_impact = {
			base = minor_stress_impact_loss
			zealous = major_stress_impact_gain
			honest = major_stress_impact_gain
			loyal = minor_stress_impact_loss
		}
		
		trigger_event = {
			id = pam_decision_events.0007
		}
		ai_chance = {
			base = 100
		}
	}
}

#Last Rites - Flesh, Blood and Wine (Viaticum and Extreme Unction)  
pam_decision_events.0007 = {
	type = character_event
	title = pam_decision_events.0007.t
	window = big_event_window
	theme = christian
	override_background = {
		trigger = {
            is_travelling = no
        }
        reference = bedchamber
	}
	override_background = {
        trigger = {
            is_travelling = yes
        }
        reference = ep3_relaxing_tent
    }
    override_effect_2d = {
		trigger = {
			spiritual_fulfillment >= 50
		}
		reference = legend_glow
	}
	desc = {
		desc = pam_decision_events.0007.intro
		#Eucharist based on SP
		first_valid = {
			triggered_desc = {
				trigger = {
					spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0007.desc.bad
			}
			triggered_desc = {
				trigger = {
					spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0007.desc.good
			}
			desc = pam_decision_events.0007.desc.neutral
		}
		#Extreme Unction based on SP
		desc = pam_decision_events.0007.desc.oil.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0007.desc.oil.bad
			}
			triggered_desc = {
				trigger = {
					spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0007.desc.oil.good
			}
			desc = pam_decision_events.0007.desc.oil.neutral
		}
			
		desc = pam_decision_events.0007.desc.outro
	}

	left_portrait = {
		character = scope:soon_dead
		
		triggered_animation = {
			trigger = {
				spiritual_fulfillment >= 50
			}
			animation = divine_ecstasy
		}
		triggered_animation = {
			trigger = {
				spiritual_fulfillment <= -30
			}
			animation = worry
		}		
		animation = prayer
		camera = camera_event_center_close_to_right
	}
	right_portrait = { 
		character = scope:confessor
		animation = prayer_crossing
	}

	immediate = {
		#Find HoR
		scope:soon_dead.faith.religious_head = { save_scope_as = religious_head }
		#Find holding to give to Monastic order
		ordered_held_title = {
			order_by = {
				value = county.development_level
				if = {
					limit = {
						title_province = {
							is_county_capital = no
						}
					}
					add = 100
				}
				multiply = -1
			}
			title_tier = barony
			limit = {
				barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
			}
			save_scope_as = military_title_to_give
		}
		ordered_held_title = {
			order_by = {
				value = county.development_level
				if = {
					limit = {
						title_province = {
							is_county_capital = no
						}
					}
					add = 100
				}
				multiply = -1
			}
			title_tier = barony
			limit = {
				barony_is_valid_for_monastic_order_lease_trigger = { CHARACTER = root }
			}
			save_scope_as = monastic_title_to_give
		}
		#Find a monastic order to donate to
		#Ideally one that you patronize
		faith = {
			random_faith_holy_order = {
			    limit = { 
			    	holy_order_patron = root
			    	leader = { government_has_flag = government_is_monastic_holy_order } 
			    }
				save_scope_as = monastic_order
			}
		}
		#If not, find one that would realistically make sense to support
		if = {
		    limit = {
		        NOT = {
		            exists = scope:monastic_order
		        }
		    }
		    faith = {
		        random_faith_holy_order = {
		            limit = {
		                holy_order_range_trigger = { CHARACTER = root }
		                leader = { government_has_flag = government_is_monastic_holy_order }
		            }
		            save_scope_as = monastic_order
		        }
		    }
		}
		#Find a military order to donate to
		#Ideally one that you patronize...or should it only be one that you patronize?
		faith = {
			random_faith_holy_order = {
			    limit = { 
			    	holy_order_patron = root
			    	leader = { government_has_flag = government_is_military_holy_order } 
			    }
				save_scope_as = military_order
			}
		}
		#If not, find one that would realistically make sense to support
		if = {
		    limit = {
		        NOT = {
		            exists = scope:military_order
		        }
		    }
		    faith = {
		        random_faith_holy_order = {
		            limit = {
		                holy_order_range_trigger = { CHARACTER = root }
		                leader = { government_has_flag = government_is_military_holy_order }
		            }
		            save_scope_as = military_order
		        }
		    }
		}
	}

	option = { #Option A) GIVE TO A MONASTIC ORDER
		trigger = {
			exists = scope:monastic_order
		}
		name = pam_decision_events.0007.a
		change_spiritual_fulfillment = medium_spiritual_fulfillment_value
		if = {
			limit = {
				exists = scope:monastic_title_to_give 
			}
			scope:monastic_title_to_give = {
				lease_out_to_holy_order = scope:monastic_order
			}
			scope:player_heir ?= {
				add_character_modifier = {
					modifier = pam_last_rites_monastic_order_donation_modifier
					years = 10
				}
			}
			
		}
		else = {
			scope:monastic_order.leader = {
				save_scope_as = recipient
			}
			save_scope_as = actor
			donate_to_monastic_order_effect = yes
			scope:player_heir = {
				add_character_modifier = {
					modifier = pam_last_rites_monastic_order_donation_modifier
					years = 5
				}
			}
		}

		ai_chance = {
			base = 0
		}
	}

	option = { #Option B) GIVE TO A MILITARY ORDER
		trigger = {
			exists = scope:military_order
		}
		name = pam_decision_events.0007.b
		change_spiritual_fulfillment = medium_spiritual_fulfillment_value
		if = {
			limit = {
				exists = scope:military_title_to_give 
			}
			scope:military_title_to_give = {
				lease_out_to_holy_order = scope:military_order
			}
			scope:player_heir ?= {
				add_character_modifier = {
					modifier = pam_last_rites_military_order_donation_modifier
					years = 10
				}
			}	
		}
		else = {
			scope:military_order.leader = {
				save_scope_as = recipient
			}
			save_scope_as = actor
			donate_to_monastic_order_effect = yes
			scope:player_heir = {
				add_character_modifier = {
					modifier = pam_last_rites_military_order_donation_modifier
					years = 5
				}
			}
		}
		ai_chance = {
			base = 0
		}
	}

	option = { #Option C) GIVE TO CHURCH
		trigger = {
			exists = scope:religious_head
			scope:religious_head != root
		}
		name = pam_decision_events.0007.c
		change_spiritual_fulfillment = major_spiritual_fulfillment_value
		scope:player_heir ?= {
			add_character_modifier = {
				modifier = pam_last_rites_church_donation_modifier
				years = 5
			}
		}
		pay_short_term_gold = {
			gold = major_gold_value
			target = scope:religious_head 
		}
		
		ai_chance = {
			base = 100
		}
	}
	
	option = { #Option D) Opt out
		name = pam_decision_events.0007.d
		change_spiritual_fulfillment = minor_spiritual_fulfillment_value
		ai_chance = {
			base = 100
		}
	}
	after = {
		scope:confessor ?= {
			if = {
				limit = {
					has_character_flag = fake_confessor
					root = {
						is_ai = yes
					}
				}
				silent_disappearance_effect = yes
			}
		}
	}	
} 

##########################################
# Levy the Tithes
##########################################
scripted_trigger pam_decision_events_0008_vassal_trigger = {
	this != root.cp:councillor_court_chaplain
	OR = {
		government_has_flag = government_is_theocracy
		government_has_flag = government_is_ecclesiastical
	}
}

pam_decision_events.0008 = {
	type = character_event
	title = pam_decision_events.0008.t
	desc = {
		desc = pam_decision_events.0008.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:chaplain }
				desc = pam_decision_events.0008.desc.chaplain
			}
			desc = pam_decision_events.0008.desc.fallback
		}
	}
	theme = christian

	left_portrait = {
		character = root
		animation = dismissal
	}
	right_portrait = {
		character = scope:chaplain
		animation = anger
	}
	lower_left_portrait = {
		character = scope:tithe_vassal_1
	}
	lower_center_portrait = {
		character = scope:tithe_vassal_2
	}
	lower_right_portrait = {
		character = scope:tithe_vassal_3
	}

	immediate = {
		cp:councillor_court_chaplain ?= { save_scope_as = chaplain }
		ordered_vassal = {
			limit = {
				pam_decision_events_0008_vassal_trigger = yes
			}
			order_by = primary_title.tier
			save_scope_as = tithe_vassal_1
		}

		ordered_vassal = {
			limit = {
				pam_decision_events_0008_vassal_trigger = yes
				NOT = { this = scope:tithe_vassal_1 }
			}
			order_by = primary_title.tier
			save_scope_as = tithe_vassal_2
		}

		ordered_vassal = {
			limit = {
				pam_decision_events_0008_vassal_trigger = yes
				NOR = {
					this = scope:tithe_vassal_1
					this = scope:tithe_vassal_2
				}
			}
			order_by = primary_title.tier
			save_scope_as = tithe_vassal_3
		}
	}

	option = {
		name = pam_decision_events.0008.a
		pam_levy_the_tithes_decision_effects = yes
	}
}

# Feudal to theocracy
pam_decision_events.0009 = {
	type = character_event
	title = pam_decision_events.0009.t
	desc = pam_decision_events.0009.desc
	window = big_event_window
	theme = realm
	override_background = { reference = temple }
	left_portrait = {
		character = root
		animation = preaching
	}
	center_portrait = {
		character = scope:witness
		animation = prayer_standing
		camera = camera_event_right_crowner_crowning
	}
	right_portrait = {
		character = scope:witness_1
		animation = prayer
		camera = camera_event_very_right_looking_center_fov40
	}

	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"
		cp:councillor_court_chaplain = {
			save_scope_as = witness
		}
		if = {
			limit = {
				NOT = { exists = scope:witness }
			}
			random_courtier = {
				limit = {
					rite = root.rite
					is_adult = yes
				}
				save_scope_as = witness
			}
		}
		random_vassal = {
			limit = {
				rite = root.rite
				this != scope:witness
				is_adult = yes
			}
			save_scope_as = witness_1
		}
		if = {
			limit = {
				NOT = { exists = scope:witness_1 }
			}
			random_courtier = {
				limit = {
					rite = root.rite
					this != scope:witness
					is_adult = yes
				}
				save_scope_as = witness_1
			}
		}
		change_government = theocracy_government
		hidden_effect = {
			every_liege_or_above = {
				limit = {
					NOT = { top_liege = this }
				}
				send_interface_message = {
					type = event_stewardship_neutral_with_text
					title = pam_decision_events.0009.liege_msg_title
					desc = pam_decision_events.0009.liege_msg_desc
					right_icon = root
				}
			}
		}
		if = {
			limit = {
				capital_province = {
					NOT = { has_holding_type = church_holding }
				}
			}
			ordered_sub_realm_barony = {
				limit = {
					county = {
						faith = root.faith
						holder = root
					}
				}
				order_by = {
					value = county.development_level
					if = {
						limit = {
							title_province = {
								OR = {
									has_holding_type = church_holding
									has_holding = no
								}
							}
						}
						add = 50
					}
					if = {
						limit = {
							county = root.capital_county
						}
						add = 100
					}
					if = {
						limit = {
							is_holy_site_of = root.faith
						}
						add = 200
					}
				}
				save_scope_as = new_capital
			}
		}
	}

	option = {
		name = pam_decision_events.0009.a
		trigger = {
			exists = scope:new_capital
		}
		change_spiritual_fulfillment = massive_spiritual_fulfillment_value
		scope:new_capital = {
			if = {
				limit = {
					title_province = {
						has_holding = no
					}
				}
				set_holding_type = church_holding
			}
			custom_tooltip = {
				text = church_holding_as_new_capital
				set_capital_barony = yes
				root = { set_realm_capital = prev.county }
			}
		}
		ai_will_select = {
			value = 100
		}
	}
	option = {
		name = pam_decision_events.0009.b
		change_spiritual_fulfillment = major_spiritual_fulfillment_value
		ai_will_select = {
			value = 0
		}
	}
}

# Feudal to theocracy - letter to vassal
pam_decision_events.0010 = {
	type = letter_event
	opening = pam_decision_events.0010.opening
	desc = pam_decision_events.0010.desc
	sender = scope:theocratic_ruler

	option = {
		name = pam_decision_events.0010.a
		trigger = { government_type = scope:old_government }
		change_government = theocracy_government
		ai_will_select = {
			value = 100
			if = {
				limit = {
					faith != scope:theocratic_ruler.faith
				}
				multiply = 0
			}
		}
	}
	option = {
		name = pam_decision_events.0010.b
		scope:theocratic_ruler = {
			add_opinion = {
				modifier = rejected_becoming_theocracy_opinion
				target = root
				years = 10
			}
		}
		ai_will_select = {
			value = 0
			if = {
				limit = {
					OR = {
						faith != scope:theocratic_ruler.faith
						rite != scope:theocratic_ruler.rite
					}
				}
				add = 50
			}
		}
	}
}

# Promulgate Word of Pious Deeds - Boost Papal Election Score
# Laurence Kennedy
pam_decision_events.0015 = {
	type = character_event
	title = pam_decision_events.0015.t
	
	 desc = {
		 first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:my_praiser
				}
				desc = pam_decision_events.0015.desc.intro.praiser
			}
			desc = pam_decision_events.0015.desc.intro.no_praiser
		}
		desc = pam_decision_events.0015.desc.intro
		triggered_desc = {
			trigger = {
				exists = scope:my_virtuous_trait
			}
			desc = pam_decision_events.0015.desc.virtuoustrait
		}
		desc = pam_decision_events.0015.desc.mid
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:my_virtuous_tenet
				}
				desc = pam_decision_events.0015.desc.virtuoustenet
			}
			desc = pam_decision_events.0015.desc.novirtuoustenet
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:praise_worthy_priest.spiritual_fulfillment <= -50
				}
				desc = pam_decision_events.0015.desc.bad
			}
			triggered_desc = {
				trigger = {
					scope:praise_worthy_priest.spiritual_fulfillment >= 50
				}
				desc = pam_decision_events.0015.desc.good
			}
			desc = pam_decision_events.0015.desc.neutral
		}
		desc = pam_decision_events.0015.desc.outro
	}
	
	theme = christian
	override_background = {
		reference = pam_romanesque_church_exterior
	}
	left_portrait = { 
		character = scope:praise_worthy_priest
		animation = prayer_standing_eyes
		camera = camera_event_center_pointing_forward
	}
	right_portrait = {
		trigger = { exists = scope:my_praiser }
		character = scope:my_praiser
		triggered_animation = {
			trigger = {
				is_clergy = yes
			}
			animation = preaching
			camera = camera_event_center_close_to_right
		}
		animation = admiration
		camera = camera_event_center_moved_left
	}
	override_effect_2d = {
		reference = legend_glow
	}
	
	immediate = {
		save_scope_as = praise_worthy_priest
		save_scope_as = actor
		save_scope_as = recipient
		random_character_trait = {
			limit = {
				save_temporary_scope_as = temp_trait
				root.rite = {
					trait_is_virtue_rite = scope:temp_trait
				}
			}
			save_scope_as = my_virtuous_trait
		}
		random_personal_tenet = {
			limit = {
				save_temporary_scope_as = temp_tenet
				scope:praise_worthy_priest.rite = {
					has_at_least_tenet_status = {
						tenet = scope:temp_tenet
						status = permitted
					}
				}
			}
			save_scope_as = my_virtuous_tenet
		}
		faith = {
			ordered_faith_ruler = {
				limit = {
					has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:praise_worthy_priest }
					highest_held_title_tier >= tier_duchy
					rite = root.rite
					this != scope:praise_worthy_priest
					is_adult = yes
					is_available = yes
				}
				order_by = {
					value = primary_title.tier
					multiply = piety_level
					add = "opinion(root)"
					if = {
						limit = {
							is_close_family_of = scope:praise_worthy_priest
						}
						multiply = 1.5
					}
				}
				save_scope_as = my_praiser
			}
		}
		if = {
			limit = {
				NOT = { exists = scope:my_praiser }
				exists = cp:councillor_court_chaplain
				cp:councillor_court_chaplain = {
					is_available = yes
					this != root
				}
			}
			cp:councillor_court_chaplain = { save_scope_as = my_praiser }
		}
		if = {
			limit = {
				NOT = { exists = scope:my_praiser }
			}
			ordered_vassal = {
				order_by = primary_title.tier
				limit = {
					government_has_flag = government_is_ecclesiastical
					rite = root.rite
					this != scope:praise_worthy_priest
					is_adult = yes
					is_available = yes
				}
				save_scope_as = my_praiser
			}
		}
	}

	option = { #C) Papacy
		name = pam_decision_events.0015.c
		flavor = pam_decision_events.0015.c.flavor
		custom_tooltip = pam_decision_events.0015.c.tt
		add_prestige = medium_prestige_gain
		set_variable = {
			name = pam_propagate_word_of_my_piety
			value = 30
		}
		ai_chance = {
			base = 50
		}
	}

	option = { #D) Papacy, pressed hard enough to top the shortlist
		name = pam_decision_events.0015.d
		flavor = pam_decision_events.0015.d.flavor
		trigger = {
			pam_papabile_slot_open_to_trigger = { SLOT = theocratic_elective_populist }
			piety >= pam_place_first_as_populist_piety_cost_value
		}
		show_as_unavailable = {
			pam_papabile_slot_open_to_trigger = { SLOT = theocratic_elective_populist }
			piety < pam_place_first_as_populist_piety_cost_value
		}
		custom_tooltip = pam_decision_events.0015.d.tt
		add_piety = {
			value = pam_place_first_as_populist_piety_cost_value
			multiply = -1
		}
		pam_place_first_as_populist_effect = { VALUE = pam_papabile_place_first_populist_value }
		stress_and_fulfillment_impact = {
			humble = minor_stress_impact_gain
			content = minor_stress_impact_gain
			ambitious = minor_stress_impact_loss
			arrogant = minor_stress_impact_loss
		}
		ai_chance = {
			base = 0
		}
	}

	option = { #B) Sainthood
		name = pam_decision_events.0015.b
		flavor = pam_decision_events.0015.b.flavor
		custom_tooltip = pam_decision_events.0015.b.tt
		set_variable = {
			name = pam_propagate_word_of_my_sainthood
		}
		ai_chance = {
			base = 50
		}
	}
}

# HoF Tenet Stance letter
# Triggered from pam_permit_tenet_decision / pam_prohibit_tenet_decision
pam_decision_events.0011 = {
	type = letter_event
	opening = pam_decision_events.0011.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:pam_tenet_stance_permit }
				desc = pam_decision_events.0011.desc.permitted
			}
			triggered_desc = {
				trigger = { exists = scope:pam_tenet_stance_prohibit }
				desc = pam_decision_events.0011.desc.prohibited
			}
			desc = pam_decision_events.0011.desc.fallback
		}
	}
	sender = scope:pam_tenet_stance_issuer

	option = {
		trigger = {
			NAND = {
				exists = scope:pam_tenet_stance_prohibit
				has_personal_tenet = scope:pam_tenet_stance_tenet
			}
		}
		name = {
			trigger = {
				exists = scope:pam_tenet_stance_permit
				has_personal_tenet = scope:pam_tenet_stance_tenet
			}
			text = pam_tenet_stance_judgement_personal_permit
		}
		name = {
			trigger = {
				exists = scope:pam_tenet_stance_permit
				any_desired_tenet = {
					this = scope:pam_tenet_stance_tenet
				}
			}
			text = pam_tenet_stance_judgement_aligned_permit
		}
		name = {
			trigger = {
				exists = scope:pam_tenet_stance_prohibit
				any_desired_tenet = {
					this = scope:pam_tenet_stance_tenet
				}
			}
			text = pam_tenet_stance_judgement_aligned_prohibit
		}
		name = {
			trigger = { exists = scope:pam_tenet_stance_permit }
			text = pam_tenet_stance_judgement_distant_permit
		}
		name = {
			trigger = { exists = scope:pam_tenet_stance_prohibit }
			text = pam_tenet_stance_judgement_distant_prohibit
		}
		name = pam_tenet_stance_judgement_neutral
		
		show_as_tooltip = {
			if = {
				limit = {
					exists = scope:pam_tenet_stance_permit
				}
				faith = {
					set_tenet_status = {
						tenet = scope:selected_item
						status = permitted
					}
					change_fervor = {
						value = significant_fervor_loss
						desc = fervor_loss_pam_tenet_permitted
					}
				}
			}
			else = {
				faith = {
					set_tenet_status = {
						tenet = scope:selected_item
						status = prohibited
					}
					change_fervor = {
						value = notable_fervor_value
						desc = fervor_gain_pam_tenet_prohibited
					}
				}
			}
		}
	}

	option = {
		trigger = {
			exists = scope:pam_tenet_stance_prohibit
			has_personal_tenet = scope:pam_tenet_stance_tenet
		}
		name = pam_decision_events.0011.b
		
		show_as_tooltip = {
			faith = {
				set_tenet_status = {
					tenet = scope:selected_item
					status = prohibited
				}
				change_fervor = {
					value = notable_fervor_value
					desc = fervor_gain_pam_tenet_prohibited
				}
			}
		}
		
		add_stress = medium_stress_gain
	}
}

# HoF Rite Heresy letter - non Catholic
pam_decision_events.0012 = {
	type = letter_event
	opening = pam_decision_events.0012.opening
	desc = pam_decision_events.0012.desc
	sender = scope:bull_issuer

	option = {
		name = pam_decision_events.0012.a
		custom_tooltip = pam_declare_rite_heresy_effect_tt
	}
}

pam_decision_events.0013 = {
	type = letter_event
	opening = pam_decision_events.0013.opening
	desc = {
		desc = pam_decision_events.0013.desc.intro
		desc = pam_decision_events.0013.desc.outro
	}
	sender = scope:bull_issuer

	option = {
		name = pam_decision_events.0013.a
		add_piety = medium_piety_gain
		if = {
			limit = {
				is_available_quick = {
					advanced_ruler = yes
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
		}
	}

	option = {
		name = pam_decision_events.0013.b
		trigger = {
			exists = scope:bull_issuer
			exists = scope:bull_issuer.rite
		}
		set_character_rite = scope:bull_issuer.rite
		if = {
			limit = { has_trait = heresiarch }
			remove_trait = heresiarch
			add_trait = herald
		}
		add_prestige = medium_prestige_loss
		stress_impact = {
			arrogant = medium_stress_impact_gain
			zealous = medium_stress_impact_gain
			stubborn = medium_stress_impact_gain
		}
	}
}

# Khachkar decision maintenance event
pam_decision_events.0016 = {
	hidden = yes

	trigger = {
		has_pam_dlc_trigger = yes
		player_heir ?= {
			is_close_family_of = root
			is_alive = yes
			rite = {
				rite_has_parameter = tenet_khachkar_decision_unlock
			}
		}
		dynasty = player_heir.dynasty
	}

	immediate = {
		player_heir = {
			set_variable = {
				name = pam_khachkar_ancestral_ancestor
				value = root
				years = 5
			}
		}
	}
}

# Khachkar confirmed event.
pam_decision_events.0017 = {
	type = character_event
	title = pam_decision_events.0017.t
	desc = pam_decision_events.0017.desc
	theme = christian
	override_background = {
		trigger = { exists = scope:background_terrain_scope }
		reference = terrain_scope
	}
	override_background = { reference = temple }

	left_portrait = {
		character = root
		animation = personality_zealous
	}
	lower_right_portrait = {
		character = scope:khachkar_ancestor
		animation = dead
	}

	trigger = {
		has_pam_dlc_trigger = yes
		has_variable = pam_khachkar_ancestral_ancestor
	}

	immediate = {
		if = {
			limit = { NOT = { exists = scope:khachkar_ancestor } }
			var:pam_khachkar_ancestral_ancestor = { save_scope_as = khachkar_ancestor }
		}
		if = {
			limit = { NOT = { exists = scope:khachkar_location } }
			capital_county = { save_scope_as = khachkar_location }
		}
		scope:khachkar_location.title_province = { save_scope_as = background_terrain_scope }
	}

	option = {
		name = pam_decision_events.0017.a
		trigger = {
			scope:khachkar_location = {
				NOT = {
					has_county_modifier = pam_khachkar_ancestral_county
				}
			}
		}
		pam_apply_ancestral_khachkar_effects = {
			LOCATION = scope:khachkar_location
			ANCESTOR = scope:khachkar_ancestor
		}
		custom_description_no_bullet = {
			text = pam_raise_khachkar_decision_warning
		}
		ai_chance = {
			base = 100
		}
	}

	option = {
		name = pam_decision_events.0017.b
		add_gold = medium_gold_value
		remove_decision_cooldown = pam_raise_khachkar_decision
		ai_chance = {
			base = 0
		}
	}
}

# HoF Crusade Age core tenet letter event
pam_decision_events.0018 = {
	type = letter_event
	opening = pam_decision_events.0018.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:hof_scope.faith = faith:catholic
				}
				desc = pam_decision_events.0018.desc.armed_pilgrimages
			}
			desc = pam_decision_events.0018.desc.struggle_submission
		}
	}
	sender = scope:hof_scope

	option = {
		name = pam_decision_events.0018.a

		show_as_tooltip = {
			if = {
				limit = { faith = faith:catholic }
				faith = {
					main_rite = {
						add_rite_tenet = tenet_armed_pilgrimages
					}
				}
			}
			else = {
				faith = {
					main_rite = {
						add_rite_tenet = tenet_struggle_submission
					}
				}
			}
		}
	}
}

# Secure Pilgrim Routes confirmation event
pam_decision_events.0019 = {
	type = character_event
	window = big_event_window
	theme = christian
	title = pam_decision_events.0019.t
	desc = pam_decision_events.0019.desc

	override_effect_2d = {
		reference = legend_glow
	}

	widget = {
		gui = "event_window_widget_rite"
		container = "custom_widgets_container"
	}

	left_portrait = {
		character = root
		animation = preaching
	}

	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"
		save_scope_as = hof_scope
		pam_secure_pilgrim_routes_effect = yes
	}

	option = {
		name = pam_decision_events.0019.a
	}

	after = {
		faith = {
			every_faith_character = {
				limit = {
					is_ruler = yes
					this != scope:hof_scope
				}
				trigger_event = { id = pam_decision_events.0018 days = 1 }
			}
		}
	}
}


pam_decision_events.0020 = {
	type = character_event
	title = pam_decision_events.0020.t

	desc = pam_decision_events.0020.desc

	theme = christian
	override_background = {
		reference = pam_romanesque_church_exterior
	}
	left_portrait = {
		character = root
		animation = preaching
		camera = camera_event_center_pointing_forward
	}
	right_portrait = {
		character = scope:portrait
		triggered_animation = {
			trigger = {
				is_clergy = yes
			}
			animation = prayer_standing_eyes
		}
		animation = admiration
	}
	override_effect_2d = {
		reference = legend_glow
	}

	immediate = {
		random_in_list = {
			list = evangelized_subjects
			save_scope_as = evangelized_subject
		}
		random_in_list = {
			list = evangelized_clerical_region_rulers
			save_scope_as = evangelized_clerical_region_ruler
		}
		if = {
			limit = {
				exists = scope:evangelized_clerical_region_ruler
			}
			scope:evangelized_clerical_region_ruler = {
				save_scope_as = portrait
			}
		}
		else = {
			scope:evangelized_subject = {
				save_scope_as = portrait
			}
		}
	}
	option = {
		name = pam_decision_events.0020.a
		trigger = {
			list_size:evangelized_clerical_region_rulers > 0
		}
		every_in_list = {
			list = evangelized_clerical_region_rulers
			custom = every_clerical_region_ruler_evangelized_personal_tenet_tt
			random = {
				chance = pam_evangelize_tenet_adoption_chance_value
				save_temporary_scope_as = tenet_adopter
				custom_tooltip = pam_evangelize_tenet_adopt_personal_tenet_tt
				hidden_effect = {
					root = {
						send_interface_message = {
							type = event_generic_good
							title = pam_evangelize_tenet_adopted_message_title
							left_icon = scope:tenet_adopter
							scope:tenet_adopter = {
								pam_replace_personal_tenet_effect = { TENET = scope:pam_evangelize_tenet }
							}
						}
					}
				}
			}
		}
		ai_will_select = {
			value = 10
			multiply = list_size:evangelized_clerical_region_rulers
		}
	}
	option = {
		name = pam_decision_events.0020.b
		trigger = {
			list_size:evangelized_subjects > 0
		}
		every_in_list = {
			list = evangelized_subjects
			custom = every_subject_evangelized_personal_tenet_tt
			random = {
				chance = pam_evangelize_tenet_adoption_chance_value
				save_temporary_scope_as = tenet_adopter
				custom_tooltip = pam_evangelize_tenet_adopt_personal_tenet_tt
				hidden_effect = {
					root = {
						send_interface_message = {
							type = event_generic_good
							title = pam_evangelize_tenet_adopted_message_title
							left_icon = scope:tenet_adopter
							scope:tenet_adopter = {
								pam_replace_personal_tenet_effect = { TENET = scope:pam_evangelize_tenet }
							}
						}
					}
				}
			}
		}
		ai_will_select = {
			value = 10
			multiply = list_size:evangelized_subjects
		}
	}

	option = {
		name = pam_decision_events.0020.c
		change_spiritual_fulfillment = minor_spiritual_fulfillment_value
		ai_will_select = {
			value = 0
			if = {
				limit = {
					spiritual_fulfillment <= 0
				}
				add = 20
			}
		}
	}
}

# Proclaim the Living Word - capstone of pam_proclaim_living_word_decision, fires after the herald trait is granted
pam_decision_events.0021 = {
	type = character_event
	window = big_event_window
	title = pam_decision_events.0021.t
	desc = pam_decision_events.0021.desc
	theme = christian
	override_background = temple
	widgets = {
		widget = {
			gui = event_window_widget_vfx_godray
			container = background_shader_vfx_container
		}
	}

	left_portrait = {
		character = root
		animation = preaching
	}

	option = { # A shepherd's mercy for the gathered flock
		name = pam_decision_events.0021.a
		add_character_modifier = {
			modifier = pam_herald_shepherd_of_the_faithful_modifier
		}
		show_as_tooltip = {
			add_trait_force_tooltip = herald
		}
		stress_and_fulfillment_impact = {
			sadistic = minor_stress_impact_gain
			wrathful = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				OR = {
					has_trait = sadistic
					has_trait = wrathful
				}
			}
		}
	}

	option = { # Turn the sermon into a call to arms
		name = pam_decision_events.0021.b
		add_character_modifier = {
			modifier = pam_herald_fire_and_sword_modifier
		}
		show_as_tooltip = {
			add_trait_force_tooltip = herald
		}
		stress_and_fulfillment_impact = {
			compassionate = medium_stress_impact_gain
			forgiving = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				OR = {
					has_trait = compassionate
					has_trait = forgiving
				}
			}
		}
	}
}

#Consecrate Kingdom
pam_decision_events.0030 = {
	type = letter_event
	opening = pam_decision_events.0030.opening
	desc = pam_decision_events.0030.desc
	sender = scope:king_aspirant

	immediate = {
		if = {
			limit = {
				scope:king_aspirant = {
					any_held_title = {
						tier = tier_duchy
						kingdom.holder ?= {
							religion = root.religion
							NOT = {
								this = root
							}
						}
					}
				}
			}
			scope:king_aspirant = {
				every_held_title = {
					limit = {
						tier = tier_duchy
						kingdom.holder ?= {
							religion = root.religion
							NOT = {
								this = root
							}
						}
					}
					kingdom.holder = {
						add_to_list = grateful_refusal_chars
					}
				}
			}
		}
	}

	#Grant kingdom
	option = {
		name = pam_decision_events.0030.a
		add_prestige = major_prestige_loss
		scope:king_aspirant = {
			consecrate_kingdom_player_cleric_effect = yes
			consecrate_kingdom_effect = yes
		}

		every_in_list = {
			list = grateful_refusal_chars
			add_opinion = {
				target = root
				modifier = gave_away_de_jure_lands_opinion
				opinion = -50
			}
		}
		if = {
			limit = {
				scope:king_aspirant = {
					is_independent_ruler = no
					liege = {
						NOR = {
							is_in_list = grateful_refusal_chars
							this = root
						}
					}
				}
			}
			scope:king_aspirant.liege = {
				add_opinion = {
					target = root
					modifier = disrespect_opinion
					opinion = -50
				}
			}
		}
	}
	#Refuse
	option = {
		name = pam_decision_events.0030.b
		scope:king_aspirant = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = pam_decision_events.0030.b.decline
				left_icon = scope:religious_authority
				progress_towards_rival_effect = {
					REASON = rival_refused_kingdom
					CHARACTER = scope:religious_authority
					OPINION = 0
				}
				add_opinion = {
					target = scope:religious_authority
					modifier = refusal_opinion
					opinion = -75
				}
				add_piety_level = -1
			}
		}
		every_in_list = {
			list = grateful_refusal_chars
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 30
			}
		}
		if = {
			limit = {
				scope:king_aspirant = {
					is_independent_ruler = no
					liege = {
						NOR = {
							is_in_list = grateful_refusal_chars
							this = root
						}
					}
				}
			}
			scope:king_aspirant.liege = {
				add_opinion = {
					target = root
					modifier = grateful_opinion
					opinion = 30
				}
			}
		}
		ai_chance = {
			base = 0
		}
	}
}
#I have had a new Kingdom consecrated
pam_decision_events.0031 = {
	type = character_event
	title = pam_decision_events.0031.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:religious_authority
				}
				desc = pam_decision_events.0031.desc.self_crowning
			}
			triggered_desc = {
				trigger = { exists = scope:three_duchies }
				desc = pam_decision_events.0031.desc_three_duchies
			}
			desc = pam_decision_events.0031.desc
		}
		
	}
	theme = coronation
	override_background = {
		reference = temple
	}
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					faith.religion = religion:catholic_religion
					faith.religion = religion:protestant_religion
					faith.religion = religion:eastern_orthodox_religion
					faith.religion = religion:oriental_orthodox_religion
				}
			}
			animation = divine_gesture_3
		}
		triggered_animation = {
			trigger = {
				OR = {
					faith.religion = religion:catholic_religion
					faith.religion = religion:protestant_religion
					faith.religion = religion:eastern_orthodox_religion
					faith.religion = religion:oriental_orthodox_religion
				}
			}
			animation = scepter
		}
		animation = zealous_superior
		camera = camera_event_right_wedding
	}
	right_portrait = {
		character = scope:consecrator
		triggered_animation = {
			trigger = {
				OR = {
					faith.religion = religion:catholic_religion
					faith.religion = religion:protestant_religion
					faith.religion = religion:eastern_orthodox_religion
					faith.religion = religion:oriental_orthodox_religion
				}
			}
			animation = acknowledging_crozier
		}
		animation = wedding_priest
		camera = camera_event_left_to_the_left
	}
	window = big_event_window
	widget = { gui = event_window_widget_vfx_godray container = foreground_shader_vfx_container }
	
	immediate = {
		if = {
			limit = {
				NOT = {
					scope:religious_authority = root
				}
			}
			scope:religious_authority = {
				save_scope_as = consecrator
			}
		}
		if = {
			limit = {
				OR = {
					faith.religion = religion:catholic_religion
					faith.religion = religion:protestant_religion
					faith.religion = religion:eastern_orthodox_religion
					faith.religion = religion:oriental_orthodox_religion
				}
			}
			play_music_cue = "mx_cue_deus_hoc_vult"
		}
		else = {
			play_music_cue = "mx_cue_epic_sacral_moment"
		}
		# If Kingdom is formed from three Duchies, specifically shout that out
		if = {
			limit = {
				any_held_title = {
					title_tier = duchy
					count >= 3
				}
			}
			scope:old_title = { # Carried over from the decision
				save_scope_as = duchy_1
			}
			random_held_title = {
				title_tier = duchy
				limit = {
					this != scope:duchy_1
				}
				save_scope_as = duchy_2
			}
			random_held_title = {
				title_tier = duchy
				limit = {
					NOR = {
						this = scope:duchy_1
						this = scope:duchy_2
					}
				}
				save_scope_as = duchy_3
			}
			if = { # Safeguarding against loc-breaking here
				limit = {
					exists = scope:duchy_1
					exists = scope:duchy_2
					exists = scope:duchy_3
				}
				save_scope_value_as = {
					name = three_duchies
					value = yes
				}
			}
		}
		show_as_tooltip = {
			set_primary_title_to = scope:new_title
		}

	}

	option = {
		name = pam_decision_events.0031.a
		custom_tooltip = major_decisions.1101.i_have_become_royal
	}
}

# Develop Clerical Court
pam_decision_events.0032 = {
	type = character_event
	title = pam_decision_events.0032.t
	window = big_event_window
	theme = christian
	override_background = {
		reference = temple
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						religion = religion:catholic_religion
						religion = religion:protestant_religion
						religion = religion:eastern_orthodox_religion
						religion = religion:oriental_orthodox_religion
					}
				}
				desc = pam_decision_events.0032.desc.intro.christian
			}
			desc = pam_decision_events.0032.desc.intro
		}
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:pam_clerical_pupil }
				desc = pam_decision_events.0032.desc.pupil
			}
			desc = pam_decision_events.0032.desc.no_pupil
		}
		triggered_desc = {
			trigger = { exists = scope:pam_clerical_novice }
			desc = pam_decision_events.0032.desc.novice
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						religion = religion:catholic_religion
						religion = religion:protestant_religion
						religion = religion:eastern_orthodox_religion
						religion = religion:oriental_orthodox_religion
					}
				}
				desc = pam_decision_events.0032.desc.outro.christian
			}
			desc = pam_decision_events.0032.desc.outro
		}
	}

	left_portrait = {
		character = root
		animation = preaching
	}
	center_portrait = {
		character = scope:pam_clerical_novice
		animation = prayer_standing_eyes
		camera = camera_event_scheme_right
	}
	right_portrait = {
		character = scope:pam_clerical_pupil
		animation = prayer_standing
	}

	immediate = {
		save_scope_as = pam_clerical_patron
		# The featured pupil: prefer a cleric with room to grow, fall back to an ordination candidate
		if = {
			limit = {
				any_courtier = { pam_clerical_court_pupil_trigger = yes }
			}
			random_courtier = {
				limit = { pam_clerical_court_pupil_trigger = yes }
				save_scope_as = pam_clerical_pupil
			}
		}
		else_if = {
			limit = {
				any_courtier = { pam_clerical_court_ordain_candidate_trigger = yes }
			}
			random_courtier = {
				limit = { pam_clerical_court_ordain_candidate_trigger = yes }
				save_scope_as = pam_clerical_pupil
			}
		}
		if = {
			limit = {
				any_pool_character = {
					province = root.location
					pam_clerical_court_novice_trigger = { RULER = root }
				}
			}
			random_pool_character = {
				province = root.location
				limit = { pam_clerical_court_novice_trigger = { RULER = root } }
				save_scope_as = pam_clerical_novice
			}
		}
		# Opportunity roll - most of the time nothing lands and the top option stays hidden
		random_list = {
			100 = {
				# No opportunity this time
			}
			20 = {
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:piety }
			}
			12 = {
				trigger = {
					exists = scope:pam_clerical_novice
					any_courtier = {
						count < pam_clerical_court_size_cap_value
						is_clergy = yes
					}
				}
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:novice }
			}
			10 = {
				trigger = {
					faith = {
						OR = {
							faith_has_monasticism_trigger = yes
							any_faith_holy_order = { always = yes }
						}
					}
				}
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:oblates }
			}
			10 = {
				trigger = { exists = scope:pam_clerical_pupil }
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:language }
			}
			8 = {
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:relic }
			}
			8 = {
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:scriptorium }
			}
			6 = {
				trigger = {
					exists = scope:pam_clerical_pupil
					scope:pam_clerical_pupil = {
						OR = {
							NOT = { has_trait = lifestyle_mystic }
							has_trait_xp = { trait = lifestyle_mystic value < 100 }
						}
					}
				}
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:miracle }
			}
			# Rare, and never while the court already houses a prodigy
			3 = {
				trigger = {
					exists = scope:pam_clerical_pupil
					NOT = {
						any_courtier = { has_character_flag = pam_clerical_prodigy_flag }
					}
				}
				save_scope_value_as = { name = pam_clerical_opportunity value = flag:prodigy }
			}
		}
	}

	# Opportunity: a prodigy reveals themselves
	option = {
		name = pam_decision_events.0032.prodigy
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:prodigy
		}
		scope:pam_clerical_pupil = {
			add_character_flag = pam_clerical_prodigy_flag
			add_learning_skill = { 4 6 }
			if = {
				limit = { has_trait = education_learning_3 }
				hidden_effect = { remove_trait = education_learning_3 }
				add_trait = education_learning_4
			}
			else_if = {
				limit = { has_trait = education_learning_2 }
				hidden_effect = { remove_trait = education_learning_2 }
				add_trait = education_learning_3
			}
			else_if = {
				limit = { has_trait = education_learning_1 }
				hidden_effect = { remove_trait = education_learning_1 }
				add_trait = education_learning_2
			}
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Opportunity: a reputation as a patron of oblates
	option = {
		name = pam_decision_events.0032.oblates
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:oblates
		}
		add_character_modifier = {
			modifier = pam_oblate_patron_modifier
			years = 10
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Opportunity: a windfall of devotion
	option = {
		name = pam_decision_events.0032.piety
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:piety
		}
		add_piety = pam_clerical_court_piety_windfall_value
		ai_chance = { base = 100 }
	}

	# Opportunity: a gifted novice presents himself
	option = {
		name = pam_decision_events.0032.novice
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:novice
		}
		add_courtier = scope:pam_clerical_novice
		scope:pam_clerical_novice = {
			# Convert before the trait grant, so clergy enrollment lands on the right faith
			if = {
				limit = {
					NOT = { faith = scope:pam_clerical_patron.faith }
				}
				set_character_faith = scope:pam_clerical_patron.faith
			}
			if = {
				limit = { NOT = { rite = scope:pam_clerical_patron.rite } }
				set_character_rite = scope:pam_clerical_patron.rite
			}
			if = {
				limit = { learning < scope:pam_clerical_patron.pam_clerical_court_learning_cap_value }
				add_learning_skill = pam_clerical_court_novice_learning_boost_value
			}
			if = {
				limit = { NOT = { has_trait = cleric } }
				# Trait's dynamic name uses pre-conversion faith in the tooltip
				custom_tooltip = pam_clerical_court_joins_clergy_tt
				hidden_effect = {
					add_trait = cleric
				}
			}
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Opportunity: a miracle worker emerges
	option = {
		name = pam_decision_events.0032.miracle
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:miracle
		}
		scope:pam_clerical_pupil = {
			if = {
				limit = { NOT = { has_trait = lifestyle_mystic } }
				add_trait = lifestyle_mystic
			}
			else = {
				add_trait_xp = {
					trait = lifestyle_mystic
					value = pam_clerical_court_trait_xp_gain_value
				}
			}
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Opportunity: a holy text surfaces in the library
	option = {
		name = {
			trigger = {
				OR = {
					religion = religion:catholic_religion
					religion = religion:protestant_religion
					religion = religion:eastern_orthodox_religion
					religion = religion:oriental_orthodox_religion
				}
			}
			text = pam_decision_events.0032.relic.christian
		}
		name = {
			# Valid name candidates are picked at random, so the fallback must exclude the branched case
			trigger = {
				NOT = {
					OR = {
						religion = religion:catholic_religion
						religion = religion:protestant_religion
						religion = religion:eastern_orthodox_religion
						religion = religion:oriental_orthodox_religion
					}
				}
			}
			text = pam_decision_events.0032.relic
		}
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:relic
		}
		if = {
			limit = {
				OR = {
					religion = religion:catholic_religion
					religion = religion:protestant_religion
					religion = religion:eastern_orthodox_religion
					religion = religion:oriental_orthodox_religion
				}
			}
			create_christian_religious_artifact_effect = {
				OWNER = root
				CREATOR = root
			}
		}
		else = {
			# A lasting object for every faith - a sacred text of unknown, ancient authorship
			random_dummy_gender_effect = yes
			create_artifact_book_effect = {
				OWNER = root
				CREATOR = scope:dummy_gender
				SET_SUBJECT = flag:religious_text
				SET_TOPIC = flag:no
			}
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Opportunity: a cleric masters the liturgical tongue
	option = {
		name = pam_decision_events.0032.language
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:language
		}
		scope:pam_clerical_pupil = {
			if = {
				limit = {
					portrait_religious_western_christian_trigger = yes
					NOT = { knows_language = language_latin }
				}
				learn_language = language_latin
			}
			else_if = {
				limit = {
					portrait_religious_orthodox_trigger = yes
					NOT = { knows_language = language_greek }
				}
				learn_language = language_greek
			}
			else_if = {
				limit = { NOT = { has_trait = lifestyle_scholar } }
				add_trait = lifestyle_scholar
			}
			else = {
				add_trait_xp = {
					trait = lifestyle_scholar
					value = pam_clerical_court_trait_xp_gain_value
				}
			}
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Opportunity: the scriptorium flourishes
	option = {
		name = {
			trigger = {
				OR = {
					religion = religion:catholic_religion
					religion = religion:protestant_religion
					religion = religion:eastern_orthodox_religion
					religion = religion:oriental_orthodox_religion
				}
			}
			text = pam_decision_events.0032.scriptorium.christian
		}
		name = {
			trigger = {
				NOT = {
					OR = {
						religion = religion:catholic_religion
						religion = religion:protestant_religion
						religion = religion:eastern_orthodox_religion
						religion = religion:oriental_orthodox_religion
					}
				}
			}
			text = pam_decision_events.0032.scriptorium
		}
		trigger = {
			exists = scope:pam_clerical_opportunity
			scope:pam_clerical_opportunity = flag:scriptorium
		}
		add_character_modifier = {
			modifier = pam_scriptorium_modifier
			years = 10
		}
		add_piety = pam_clerical_court_piety_reward_value
		ai_chance = { base = 100 }
	}

	# Ordain lay courtiers into the clergy
	option = {
		name = {
			trigger = {
				OR = {
					religion = religion:catholic_religion
					religion = religion:protestant_religion
					religion = religion:eastern_orthodox_religion
					religion = religion:oriental_orthodox_religion
				}
			}
			text = pam_decision_events.0032.a.christian
		}
		name = {
			trigger = {
				NOT = {
					OR = {
						religion = religion:catholic_religion
						religion = religion:protestant_religion
						religion = religion:eastern_orthodox_religion
						religion = religion:oriental_orthodox_religion
					}
				}
			}
			text = pam_decision_events.0032.a
		}
		trigger = {
			any_courtier = { pam_clerical_court_ordain_candidate_trigger = yes }
		}
		custom_tooltip = pam_decision_events.0032.a.tt
		add_piety = pam_clerical_court_piety_reward_value
		hidden_effect = {
			ordered_courtier = {
				limit = { pam_clerical_court_ordain_candidate_trigger = yes }
				order_by = learning
				max = pam_clerical_court_batch_value
				check_range_bounds = no
				pam_clerical_court_ordain_effect = yes
			}
		}
		ai_chance = {
			base = 100
			# Always bank a rolled opportunity
			modifier = {
				factor = 0
				exists = scope:pam_clerical_opportunity
			}
		}
	}

	# Instruct the clergy already at court
	option = {
		name = pam_decision_events.0032.b
		trigger = {
			any_courtier = { pam_clerical_court_pupil_trigger = yes }
		}
		custom_tooltip = pam_decision_events.0032.b.tt
		add_piety = pam_clerical_court_piety_reward_value
		hidden_effect = {
			ordered_courtier = {
				limit = { pam_clerical_court_pupil_trigger = yes }
				order_by = learning
				max = pam_clerical_court_batch_value
				check_range_bounds = no
				pam_clerical_court_instruct_effect = yes
			}
		}
		ai_chance = {
			base = 50
			# Always bank a rolled opportunity
			modifier = {
				factor = 0
				exists = scope:pam_clerical_opportunity
			}
		}
	}

	# We call for nearby novices and hide this if none exist, never spawning one
	# A named supplicant is recruited first
	option = {
		name = {
			trigger = { exists = scope:pam_clerical_novice }
			text = pam_decision_events.0032.c.named
		}
		name = {
			trigger = { NOT = { exists = scope:pam_clerical_novice } }
			text = pam_decision_events.0032.c
		}
		trigger = {
			any_pool_character = {
				province = root.location
				pam_clerical_court_novice_trigger = { RULER = root }
			}
		}
		custom_tooltip = pam_decision_events.0032.c.tt
		add_piety = pam_clerical_court_piety_reward_value
		hidden_effect = {
			set_variable = {
				name = pam_clerical_recruits_left
				value = pam_clerical_court_batch_value
			}
			while = {
				count = 4 # Hard bound matching the highest batch; the limits below are what actually end the loop
				limit = {
					var:pam_clerical_recruits_left > 0
					any_pool_character = {
						province = root.location
						pam_clerical_court_novice_trigger = { RULER = root }
					}
				}
				pam_clerical_court_recruit_effect = yes
				change_variable = {
					name = pam_clerical_recruits_left
					subtract = 1
				}
			}
			remove_variable = pam_clerical_recruits_left
		}
		ai_chance = {
			base = 1
			modifier = {
				add = 99
				any_courtier = {
					count < pam_clerical_court_size_cap_value
					is_clergy = yes
				}
			}
			# Always bank a rolled opportunity
			modifier = {
				factor = 0
				exists = scope:pam_clerical_opportunity
			}
		}
	}
}
