﻿namespace = pam_theocratic_ruler_events

########################### - By JosH
## pam_theocratic_ruler_events.0001 - A Bequest for the Church
## pam_theocratic_ruler_events.0002 - A Traveler from Across the Border
## pam_theocratic_ruler_events.0003 - Counsel for a Hardened Border
## pam_theocratic_ruler_events.0004 - Brethern as Neighbors
## pam_theocratic_ruler_events.0005 - Swords for the Faith
## pam_theocratic_ruler_events.0006 - Tithes or Mercy
## pam_theocratic_ruler_events.0007 - Blissful Harmony
## pam_theocratic_ruler_events.0008 - Stone upon Stone
## pam_theocratic_ruler_events.0009 - Try to Be the Shepherd
## pam_theocratic_ruler_events.0010 - The Sword or the Crozier
## pam_theocratic_ruler_events.0011 - Sword of the Fallen Knight
## pam_theocratic_ruler_events.0012 - A Sacred Grove (travel)
## pam_theocratic_ruler_events.0013 - An Angelic Anthem
## pam_theocratic_ruler_events.0014 - A Convert's Plea
## pam_theocratic_ruler_events.0015 - Prayer Hearing
############################


###########################
## pam_theocratic_ruler_events.0001 — A Bequest for the Church
############################

# Same faith landed theocrat in your archdiocese clerical region (may not be your feudal vassal).
# Scope: county holder. root: the theocratic ruler.
scripted_trigger pam_theocratic_ruler_0001_clerical_region_priest_trigger = {
	this != root
	is_available = yes
	faith = root.faith
	government_has_flag = government_is_theocracy
	is_landed = yes
}

# Suffragans use plain theocracy, not government_is_ecclesiastical
# Scope: candidate character. root: the theocratic ruler
scripted_trigger pam_theocratic_ruler_0001_subordinate_priest_trigger = {
	this != root
    is_available = yes
	faith = root.faith
	government_has_flag = government_is_theocracy
	is_landed = yes
}

pam_theocratic_ruler_events.0001 = {
	type = character_event
	title = pam_theocratic_ruler_events.0001.t
	desc = pam_theocratic_ruler_events.0001.desc
	theme = christian
	override_background = { reference = council_chamber }

	left_portrait = {
		character = root
		animation = priest_corrupt
	}
	right_portrait = {
		character = scope:subordinate_priest
		animation = reading
	}

	cooldown = { years = 15 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes # In pam_scripted_triggers
		is_landed = yes
		is_available_adult = yes
		OR = {
			any_vassal_or_below = {
				pam_theocratic_ruler_0001_subordinate_priest_trigger = yes
			}
			AND = {
				exists = capital_county
				capital_county = {
					exists = clerical_region_title
					clerical_region_title = {
						any_county_in_clerical_region = {
							holder = {
								pam_theocratic_ruler_0001_clerical_region_priest_trigger = yes
							}
						}
					}
				}
			}
			cp:councillor_court_chaplain ?= {
				is_imprisoned = no
				faith = root.faith
			}
		}
	}

	immediate = {
		every_vassal_or_below = {
			limit = {
				pam_theocratic_ruler_0001_subordinate_priest_trigger = yes
			}
			add_to_list = pam_theocratic_ruler_0001_priest_candidates
		}
		if = {
			limit = {
				exists = capital_county
				capital_county = {
					exists = clerical_region_title
				}
			}
			capital_county.clerical_region_title = {
				every_county_in_clerical_region = {
					limit = {
						holder = {
							pam_theocratic_ruler_0001_clerical_region_priest_trigger = yes
						}
					}
					holder = {
						add_to_list = pam_theocratic_ruler_0001_priest_candidates
					}
				}
			}
		}
		if = {
			limit = {
				list_size = {
					name = pam_theocratic_ruler_0001_priest_candidates
					value > 0
				}
			}
			random_in_list = {
				list = pam_theocratic_ruler_0001_priest_candidates
				save_scope_as = subordinate_priest
			}
		}
		if = {
			limit = {
				NOT = { exists = scope:subordinate_priest }
				exists = cp:councillor_court_chaplain
				cp:councillor_court_chaplain = {
					this != root
					is_alive = yes
					is_imprisoned = no
					faith = root.faith
				}
			}
			cp:councillor_court_chaplain = {
				save_scope_as = subordinate_priest
			}
		}
	}

	# Frame the priest: I find evidence he mishandled sacred funds…
	option = {
		name = pam_theocratic_ruler_events.0001.a
		skill = intrigue
		trigger = {
			intrigue >= high_skill_rating
		}
		show_as_unavailable = {
			always = yes
		}
		add_short_term_gold = medium_gold_value
		add_piety = medium_piety_loss
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:subordinate_priest
			IMPRISONER = root
		}
		if = {
			limit = {
				scope:subordinate_priest = {
					is_councillor_of = root
				}
			}
			fire_councillor = scope:subordinate_priest
		}
		if = {
			limit = {
				scope:subordinate_priest = {
					is_courtier_of = root
				}
			}
			remove_courtier_or_guest = scope:subordinate_priest
		}
		scope:subordinate_priest = {
			add_opinion = {
				modifier = blamed_me_unjustly
				target = root
			}
			add_stress = major_stress_gain
		}
		stress_and_fulfillment_impact = {
			honest = medium_stress_impact_gain
			just = minor_stress_impact_gain
			deceitful = medium_stress_impact_loss
			callous = minor_stress_impact_loss
		}
		ai_chance = {
			base = 5
			ai_value_modifier = {
				ai_honor = -0.5
				ai_greed = 0.35
				ai_rationality = 0.2
			}
		}
	}

	# "Lets reason together as clergy."
	option = {
		name = pam_theocratic_ruler_events.0001.b
		duel = {
			skills = { diplomacy intrigue }
			target = scope:subordinate_priest
			50 = {
				desc = pam_theocratic_ruler_events.0001.b.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				add_short_term_gold = medium_gold_value
				add_diplomacy_lifestyle_xp = minor_lifestyle_xp
				add_intrigue_lifestyle_xp = minor_lifestyle_xp
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_theocratic_ruler_events.0001.b.toast.success
					left_icon = scope:subordinate_priest
					right_icon = root
					scope:subordinate_priest = {
						if = {
							limit = {
								can_add_hook = {
									target = root
									type = favor_hook
								}
							}
							add_hook = {
								type = favor_hook
								target = root
							}
						}
					}
				}
			}
			50 = {
				desc = pam_theocratic_ruler_events.0001.b.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = pam_theocratic_ruler_events.0001.b.toast.failure
					left_icon = scope:subordinate_priest
					right_icon = root
					add_stress = minor_stress_gain
					scope:subordinate_priest = {
						add_opinion = {
							modifier = upset_opinion
							target = root
							opinion = -25
						}
					}
				}
			}
		}
		stress_and_fulfillment_impact = {
			gregarious = minor_stress_impact_loss
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 35
			ai_value_modifier = {
				ai_rationality = 0.35
				ai_sociability = 0.25
				ai_honor = 0.15
			}
		}
	}

	# Into the treasury, with thanksgiving and care.
	option = {
		name = pam_theocratic_ruler_events.0001.c
		add_treasury_or_gold = minor_treasury_or_gold_value
		add_piety = medium_piety_gain
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			humble = minor_stress_impact_loss
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 55
			ai_value_modifier = {
				ai_zeal = 0.45
				ai_compassion = 0.2
				ai_greed = -0.35
			}
		}
	}
}


###########################
## pam_theocratic_ruler_events.0002 — A Traveler from Across the Border
############################

scripted_trigger pam_theocratic_ruler_0002_foreign_neighbor_trigger = {
	top_liege != root.top_liege
	is_available = yes
	NOR = {
		is_allied_to = root
		has_truce = root
	}
	faith = {
		has_doctrine_parameter = unreformed
		this != root.faith
	}
	culture != root.culture
}

pam_theocratic_ruler_events.0002 = {
	type = character_event
	title = pam_theocratic_ruler_events.0002.t
	desc = pam_theocratic_ruler_events.0002.desc
	theme = christian
	override_background = { reference = activity_tour } 

	left_portrait = {
		character = root
		animation = personality_compassionate
	}
	right_portrait = {
		character = scope:foreign_ruler
		animation = personality_honorable
	}

	cooldown = { years = 10 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		is_available_adult = yes
		trigger_if = {
			limit = {
				top_liege != this
			}
			any_neighboring_realm_same_rank_owner = {
				culture != root.culture
				pam_theocratic_ruler_0002_foreign_neighbor_trigger = yes
			}
		}
		trigger_else = {
			any_neighboring_top_liege_realm_owner = {
				culture != root.culture
				pam_theocratic_ruler_0002_foreign_neighbor_trigger = yes
			}
		}
	}

	immediate = {
		if = {
			limit = {
				top_liege != this
			}
			random_neighboring_realm_same_rank_owner = {
				limit = {
					pam_theocratic_ruler_0002_foreign_neighbor_trigger = yes
				}
				save_scope_as = foreign_ruler
			}
		}
		else = {
			random_neighboring_top_liege_realm_owner = {
				limit = {
					pam_theocratic_ruler_0002_foreign_neighbor_trigger = yes
				}
				save_scope_as = foreign_ruler
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			diplomacy >= medium_skill_rating
		}
		modifier = {
			add = 0.25
			has_trait = zealous
		}
	}

	# A modest gift and offer of peace.
	option = {
		name = pam_theocratic_ruler_events.0002.a
		trigger = {
			short_term_gold >= minor_gold_value
		}
		add_truce_both_ways = {
			character = scope:foreign_ruler
			years = 5
			name = TRUCE_PAM_NEIGHBOR_GIFT
		}
		pay_short_term_gold = {
			target = scope:foreign_ruler
			gold = minor_gold_value
		}
		scope:foreign_ruler = {
			add_opinion = {
				modifier = grateful_opinion
				target = root
				opinion = 15
			}
		}
		ai_chance = {
			base = 35
			ai_value_modifier = {
				ai_greed = -0.25
				ai_zeal = 0.5
				ai_rationality = 0.35
			}
		}
	}

	# I seek honest understanding between our peoples.
	option = {
		name = pam_theocratic_ruler_events.0002.b
		skill = diplomacy
		duel = {
			skill = diplomacy
			target = scope:foreign_ruler
			45 = {
				desc = pam_theocratic_ruler_events.0002.b.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_theocratic_ruler_events.0002.b.toast.success
					left_icon = scope:foreign_ruler
					right_icon = root
					root.culture = {
						change_cultural_acceptance = {
							target = scope:foreign_ruler.culture
							value = medium_cultural_acceptance_gain
							desc = pam_theocratic_ruler_events.0002.b.acceptance
						}
					}
				}
			}
			45 = {
				desc = pam_theocratic_ruler_events.0002.b.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = pam_theocratic_ruler_events.0002.b.toast.failure
					left_icon = scope:foreign_ruler
					right_icon = root
					scope:foreign_ruler = {
						add_opinion = {
							modifier = insult_opinion
							target = root
							opinion = low_negative_opinion
						}
					}
				}
			}
		}
		stress_and_fulfillment_impact = {
			shy = minor_stress_impact_gain
			gregarious = minor_stress_impact_loss
		}
		ai_chance = {
			base = 45
			ai_value_modifier = {
				ai_sociability = 0.45
				ai_rationality = 0.25
				ai_boldness = 0.15
			}
		}
	}

	# Heathens have no place in our lands.
	option = {
		name = pam_theocratic_ruler_events.0002.c
		add_piety = medium_piety_gain
		scope:foreign_ruler = {
			progress_towards_rival_effect = {
				CHARACTER = root
				REASON = rival_insulted_me
				OPINION = 0
			}
		}
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			forgiving = minor_stress_impact_gain
			compassionate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_zeal = 0.65
				ai_compassion = -0.35
				ai_honor = -0.25
			}
		}
	}
}


###########################
## pam_theocratic_ruler_events.0003 - Counsel for a Hardened Border
############################


scripted_trigger pam_theocratic_ruler_0003_alliance_target_trigger = {
	this != root
	highest_held_title_tier >= tier_county
	government_has_flag = government_is_theocracy
	faith = root.faith
	NOR = {
		is_allied_to = root
		has_truce = root
		is_at_war_with = root
	}
	in_diplomatic_range = root
}

pam_theocratic_ruler_events.0003 = {
	type = character_event
	title = pam_theocratic_ruler_events.0003.t
	desc = pam_theocratic_ruler_events.0003.desc
	theme = christian
	override_background = { reference = council_chamber }

	left_portrait = {
		character = root
		animation = personality_rational
	}
	right_portrait = {
		character = scope:advisor
		animation = personality_honorable
	}
	lower_center_portrait = {
		character = scope:alliance_target
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		is_available_adult = yes
		exists = scope:advisor
		any_ruler = {
			pam_theocratic_ruler_0003_alliance_target_trigger = yes
		}
		OR = {
			any_memory = {
				OR = {
					has_memory_type = battle_won_memory
					has_memory_type = battle_lost_memory
				}
			}
			any_sub_realm_county = {
				title_province = {
					OR = {
						has_province_modifier = recently_looted_modifier
						has_province_modifier = recently_bartered_modifier
					}
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				exists = cp:councillor_marshal
			}
			cp:councillor_marshal = { save_scope_as = advisor }
		}
		else_if = {
			limit = {
				exists = cp:councillor_chancellor
			}
			cp:councillor_chancellor = { save_scope_as = advisor }
		}
		else_if = {
			limit = {
				exists = cp:councillor_steward
			}
			cp:councillor_steward = { save_scope_as = advisor }
		}

		random_ruler = {
			limit = {
				pam_theocratic_ruler_0003_alliance_target_trigger = yes
			}
			weight = {
				base = 1
				modifier = {
					add = 8
					is_independent_ruler = no
				}
				modifier = {
					add = 4
					highest_held_title_tier = tier_county
				}
				modifier = {
					add = 3
					highest_held_title_tier = tier_duchy
				}
				modifier = {
					add = -20
					primary_title = {
						is_head_of_faith = yes
					}
				}
				modifier = {
					add = -8
					highest_held_title_tier >= tier_kingdom
				}
			}
			save_scope_as = alliance_target
		}

		if = {
			limit = {
				any_sub_realm_county = {
					title_province = {
						OR = {
							has_province_modifier = recently_looted_modifier
							has_province_modifier = recently_bartered_modifier
						}
					}
				}
			}
			random_sub_realm_county = {
				limit = {
					title_province = {
						OR = {
							has_province_modifier = recently_looted_modifier
							has_province_modifier = recently_bartered_modifier
						}
					}
				}
				save_scope_as = raided_county
			}
		}
	}

	# Send envoys. We must stand together.
	option = {
		name = pam_theocratic_ruler_events.0003.a
		custom_tooltip = pam_theocratic_ruler_events.0003.a.tt
		create_alliance = {
			target = scope:alliance_target
			allied_through_owner = root
			allied_through_target = scope:alliance_target
		}
		add_prestige = minor_prestige_loss
		ai_chance = {
			base = 45
			ai_value_modifier = {
				ai_rationality = 0.6
				ai_honor = 0.3
			}
		}
	}

	# Raise a retinue from faithful peasants for border defense.
	option = {
		name = pam_theocratic_ruler_events.0003.b
		spawn_army = {
			levies = 150
			location = root.capital_province
			name = pam_theocratic_ruler_events.0003.b.army
			inheritable = no
		}
		add_piety = minor_piety_loss
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_boldness = 0.35
				ai_zeal = 0.2
			}
		}
	}

	# Send aid and restore one recently raided county.
	option = {
		name = pam_theocratic_ruler_events.0003.c
		trigger = {
			exists = scope:raided_county
		}
		scope:raided_county.title_province = {
			remove_province_modifier = recently_looted_modifier
			remove_province_modifier = recently_bartered_modifier
		}
		ai_chance = {
			base = 15
			ai_value_modifier = {
				ai_compassion = 0.2
				ai_rationality = 0.2
			}
		}
	}

	# I place this in God's hands.
	option = {
		name = pam_theocratic_ruler_events.0003.d
		add_piety = minor_piety_gain
		stress_and_fulfillment_impact = {
			zealous = miniscule_stress_impact_loss
		}
		ai_chance = {
			base = 10
			ai_value_modifier = {
				ai_zeal = 0.4
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0004 — Brethern as Neighbors
############################

pam_theocratic_ruler_events.0004 = {
	type = character_event
	title = pam_theocratic_ruler_events.0004.t
	desc = pam_theocratic_ruler_events.0004.desc
	theme = christian

	left_portrait = {
		character = root
		animation = chaplain
	}
	right_portrait = {
		character = scope:monastic_order_head
		animation = prayer_crossing
	}
	lower_center_portrait = {
		character = scope:monastic_monk
	}

	cooldown = { years = 8 }

	trigger = {
	pam_government_is_religious_playable_trigger = yes
		is_available_adult = yes
		any_sub_realm_county = {
			any_county_province = {
				barony = {
					is_leased_out = yes
					exists = lessee
					lessee = {
						government_has_flag = government_is_monastic_holy_order
						is_alive = yes
					}
				}
			}
		}
	}

	immediate = {
		random_sub_realm_county = {
			limit = {
				any_county_province = {
					barony = {
						is_leased_out = yes
						exists = lessee
						lessee = {
							government_has_flag = government_is_monastic_holy_order
							is_alive = yes
						}
					}
				}
			}
			save_scope_as = pam_monastic_lease_county
			random_county_province = {
				limit = {
					barony = {
						is_leased_out = yes
						lessee ?= {
							government_has_flag = government_is_monastic_holy_order
						}
					}
				}
				barony = {
					lessee = { save_scope_as = monastic_order_head }
				}
			}
		}

		if = {
			limit = {
				scope:pam_monastic_lease_county = {
					any_neighboring_county = {
						holder = {
							top_liege = root.top_liege
						}
					}
				}
			}
			scope:pam_monastic_lease_county = {
				random_neighboring_county = {
					limit = {
						holder = {
							top_liege = root.top_liege
						}
					}
					save_scope_as = aided_neighbor_county
				}
			}
		}
		else_if = {
			limit = {
				capital_county = {
					any_neighboring_county = {
						holder = {
							top_liege = root.top_liege
						}
					}
				}
			}
			capital_county = {
				random_neighboring_county = {
					limit = {
						holder = {
							top_liege = root.top_liege
						}
					}
					save_scope_as = aided_neighbor_county
				}
			}
		}

		if = {
			limit = {
				scope:monastic_order_head = {
					any_courtier_or_guest = {
						is_alive = yes
						is_imprisoned = no
						faith = scope:monastic_order_head.faith
						OR = {
							has_trait = devoted
							has_trait = celibate
						}
					}
				}
			}
			scope:monastic_order_head = {
				random_courtier_or_guest = {
					limit = {
						is_alive = yes
						is_imprisoned = no
						faith = scope:monastic_order_head.faith
						OR = {
							has_trait = devoted
							has_trait = celibate
						}
					}
					save_scope_as = monastic_monk
				}
			}
		}
		else_if = {
			limit = {
				scope:monastic_order_head = {
					any_courtier_or_guest = {
						is_alive = yes
						is_imprisoned = no
					}
				}
			}
			scope:monastic_order_head = {
				random_courtier_or_guest = {
					limit = {
						is_alive = yes
						is_imprisoned = no
					}
					save_scope_as = monastic_monk
				}
			}
		}
		else = {
			if = {
				limit = {
					scope:monastic_order_head.faith = {
						faith_has_monasticism_trigger = yes
					}
				}
				create_character = {
					location = root.capital_province
					template = monk_character_template
					faith = scope:monastic_order_head.faith
					rite = scope:monastic_order_head.rite
					culture = scope:monastic_order_head.culture
					gender_female_chance = root_faith_clergy_gender_female_chance
					trait = devoted
					save_scope_as = monastic_monk
				}
				set_variable = pam_theocratic_ruler_events_0004_created_character
			}
			else = {
				create_character = {
					location = root.capital_province
					template = priest_character_template
					faith = scope:monastic_order_head.faith
					rite = scope:monastic_order_head.rite
					culture = scope:monastic_order_head.culture
					gender_female_chance = root_faith_clergy_gender_female_chance
					save_scope_as = monastic_monk
				}
				set_variable = pam_theocratic_ruler_events_0004_created_character
			}
		}
	}

	# Please, let your brethren bless my lands.
	option = {
		name = pam_theocratic_ruler_events.0004.a
		trigger = {
			exists = scope:aided_neighbor_county
			short_term_gold >= miniscule_gold_value
		}
		scope:aided_neighbor_county = {
			add_county_modifier = {
				modifier = pam_monastic_order_improvement_modifier
				years = 10
			}
		}
		pay_short_term_gold = {
			target = scope:monastic_order_head
			gold = minor_gold_value
		}
		ai_chance = {
			base = 40
			modifier = {
				short_term_gold >= medium_gold_value
				factor = 0
			}
			ai_value_modifier = {
				ai_rationality = 0.45
				ai_greed = 0.25
			}
		}
	}

	# Your disciple would strengthen my court.
	option = {
		name = pam_theocratic_ruler_events.0004.b
		if = {
			limit = {
				NOT = {
					scope:monastic_monk = {
						is_courtier_of = root
					}
				}
			}
			add_courtier = scope:monastic_monk
			scope:monastic_monk = {
				court_event_character_flag_effect = yes
			}
		}
		court_position_grant_effect = {
			EMPLOYER = root
			POS = court_tutor
			CANDIDATE = scope:monastic_monk
		}
		ai_chance = {
			base = 35
			ai_value_modifier = {
				ai_rationality = 0.35
				ai_boldness = 0.15
			}
		}
	}

	# Let us share some prayers.
	option = {
		name = pam_theocratic_ruler_events.0004.c
		add_learning_lifestyle_xp = 100
		add_piety = miniscule_piety_gain
		stress_and_fulfillment_impact = {
			zealous = miniscule_stress_impact_loss
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_rationality = 0.5
			}
		}
	}

	after = {
		if = {
			limit = {
				has_variable = pam_theocratic_ruler_events_0004_created_character
				is_ai = yes
			}
			remove_variable = pam_theocratic_ruler_events_0004_created_character
			scope:monastic_monk = { silent_disappearance_effect = yes }
		}
	}
}

###########################
## pam_theocratic_ruler_events.0005 — Swords for the Faith
############################

pam_theocratic_ruler_events.0005 = {
	type = character_event
	title = pam_theocratic_ruler_events.0005.t
	desc = pam_theocratic_ruler_events.0005.desc
	theme = christian

	left_portrait = {
		character = root
		animation = personality_bold
	}
	right_portrait = {
		character = scope:military_order_head
		animation = zealous_superior
	}
	lower_center_portrait = {
		character = scope:order_knight
	}

	cooldown = { years = 8 }

	trigger = {
	pam_government_is_religious_playable_trigger = yes
		is_available_adult = yes
		any_sub_realm_county = {
			any_county_province = {
				barony = {
					is_leased_out = yes
					exists = lessee
					lessee = {
						government_has_flag = government_is_military_holy_order
						faith = root.faith
						is_alive = yes
					}
				}
			}
		}
	}

	immediate = {
		random_sub_realm_county = {
			limit = {
				any_county_province = {
					barony = {
						is_leased_out = yes
						exists = lessee
						lessee = {
							government_has_flag = government_is_military_holy_order
							faith = root.faith
						}
					}
				}
			}
			save_scope_as = pam_military_lease_county
			random_county_province = {
				limit = {
					barony = {
						is_leased_out = yes
						exists = lessee
						lessee ?= {
							government_has_flag = government_is_military_holy_order
							faith = root.faith
						}
					}
				}
				barony = {
					lessee = { save_scope_as = military_order_head }
				}
			}
		}

		if = {
			limit = {
				scope:pam_military_lease_county = {
					any_neighboring_county = {
						holder = {
							top_liege = root.top_liege
						}
					}
				}
			}
			scope:pam_military_lease_county = {
				random_neighboring_county = {
					limit = {
						holder = {
							top_liege = root.top_liege
						}
					}
					save_scope_as = aided_military_county
				}
			}
		}
		else_if = {
			limit = {
				root.capital_county = {
					any_neighboring_county = {
						holder = {
							top_liege = root.top_liege
						}
					}
				}
			}
			root.capital_county = {
				random_neighboring_county = {
					limit = {
						holder = {
							top_liege = root.top_liege
						}
					}
					save_scope_as = aided_military_county
				}
			}
		}

		if = {
			limit = {
				scope:military_order_head = {
					any_courtier_or_guest = {
						is_alive = yes
						is_imprisoned = no
						faith = root.faith
						prowess >= 8
					}
				}
			}
			scope:military_order_head = {
				random_courtier_or_guest = {
					limit = {
						is_alive = yes
						is_imprisoned = no
						faith = root.faith
						prowess >= 8
					}
					save_scope_as = order_knight
				}
			}
		}
		else = {
			create_character = {
				location = root.capital_province
				template = culture_defender_knight_template
				faith = scope:military_order_head.faith
				rite = scope:military_order_head.rite
				culture = scope:military_order_head.culture
				save_scope_as = order_knight
			}
			set_variable = pam_theocratic_ruler_events_0005_created_character
		}
	}

	# Drill my fighters in your order's discipline.
	option = {
		name = pam_theocratic_ruler_events.0005.a
		trigger = {
			exists = scope:aided_military_county
		}
		scope:aided_military_county = {
			add_county_modifier = {
				modifier = pam_military_order_improvement_modifier
				years = 5
			}
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_rationality = 0.35
				ai_boldness = 0.25
			}
		}
	}

	# Let your knight stand as my bodyguard.
	option = {
		name = pam_theocratic_ruler_events.0005.b
		if = {
			limit = {
				NOT = {
					scope:order_knight = {
						is_courtier_of = root
					}
				}
			}
			add_courtier = scope:order_knight
			scope:order_knight = {
				court_event_character_flag_effect = yes
			}
		}
		court_position_grant_effect = {
			EMPLOYER = root
			POS = bodyguard
			CANDIDATE = scope:order_knight
		}
		ai_chance = {
			base = 35
			ai_value_modifier = {
				ai_honor = 0.35
				ai_boldness = 0.3
			}
		}
	}

	# Let us speak of righteous war.
	option = {
		name = pam_theocratic_ruler_events.0005.c
		add_martial_lifestyle_xp = minor_lifestyle_xp
		add_prestige = minor_prestige_gain
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_rationality = 0.35
				ai_honor = 0.2
			}
		}
	}
	after = {
		if = {
			limit = {
				has_variable = pam_theocratic_ruler_events_0005_created_character
				is_ai = yes
			}
			remove_variable = pam_theocratic_ruler_events_0005_created_character
			scope:order_knight = { silent_disappearance_effect = yes }
		}
	}
}

###########################
## pam_theocratic_ruler_events.0006 — Tithes or Mercy
############################

pam_theocratic_ruler_events.0006 = {
	type = character_event
	title = pam_theocratic_ruler_events.0006.t
	desc = pam_theocratic_ruler_events.0006.desc
	theme = christian

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = compassionate
					has_trait = forgiving
					has_trait = temperate
					has_trait = humble
				}
			}
			animation = priest_good
		}
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = cynical
					has_trait = callous
					has_trait = arbitrary
					has_trait = greedy
				}
			}
			animation = priest_corrupt
		}
		animation = prayer_crossing
	}
	right_portrait = {
		character = scope:local_peasant_leader
		animation = crying
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_available_adult = yes
		any_sub_realm_county = {
			holder.top_liege = root.top_liege
		}
	}

	immediate = {
		random_sub_realm_county = {
			limit = {
				holder.top_liege = root.top_liege
			}
			save_scope_as = troubled_county
		}
		create_character = {
			location = scope:troubled_county.title_province
			template = generic_peasant_character
			faith = scope:troubled_county.faith
			rite = scope:troubled_county.rite
			culture = scope:troubled_county.culture
			save_scope_as = local_peasant_leader
		}
		set_variable = pam_theocratic_ruler_events_0006_created_character
	}

	# My steward failed me. The blame is his. (Moved this option up).
	option = {
		name = pam_theocratic_ruler_events.0006.c
		skill = intrigue
		trigger = {
			intrigue >= high_skill_rating
			exists = cp:councillor_steward
			can_add_hook = {
				target = cp:councillor_steward
				type = obligation_hook
			}
		}
		if = {
			limit = {
				can_add_hook = {
					target = cp:councillor_steward
					type = strong_obligation_hook
				}
			}
			add_hook = {
				target = cp:councillor_steward
				type = strong_obligation_hook
			}
		}
		else = {
			add_hook = {
				target = cp:councillor_steward
				type = obligation_hook
			}
		}
		cp:councillor_steward = {
			add_opinion = {
				modifier = blamed_me_unjustly
				target = root
			}
		}
		stress_and_fulfillment_impact = {
			deceitful = minor_stress_impact_loss
			honest = medium_stress_impact_gain
			just = minor_stress_impact_gain
		}
		ai_chance = {
			base = 15
			ai_value_modifier = {
				ai_rationality = 0.35
				ai_vengefulness = 0.25
			}
		}
	}

	# No exceptions and seize the ringleader.
	option = {
		name = pam_theocratic_ruler_events.0006.a
		add_treasury_or_gold = minor_treasury_or_gold_value
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:local_peasant_leader
			IMPRISONER = root
		}
		scope:troubled_county = {
			add_county_modifier = {
				modifier = pam_tithes_or_mercy_harsh_tithes_modifier
				years = 5
			}
		}
		stress_and_fulfillment_impact = {
			wrathful = minor_stress_impact_loss
			callous = minor_stress_impact_loss
			compassionate = medium_stress_impact_gain
		}
		ai_chance = {
			base = 45
			ai_value_modifier = {
				ai_greed = 0.4
				ai_zeal = 0.2
			}
		}
	}

	# Let this pass. Mercy can also serve God.
	option = {
		name = pam_theocratic_ruler_events.0006.b
		add_piety = minor_piety_gain
		scope:troubled_county = {
			add_county_modifier = {
				modifier = pam_tithes_or_mercy_show_mercy_modifier
				years = 5
			}
		}
		stress_and_fulfillment_impact = {
			compassionate = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			wrathful = minor_stress_impact_gain
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_compassion = 0.45
				ai_zeal = 0.15
			}
		}
	}
	after = {
		if = {
			limit = {
				has_variable = pam_theocratic_ruler_events_0006_created_character
				is_ai = yes
			}
			remove_variable = pam_theocratic_ruler_events_0006_created_character
			scope:local_peasant_leader = { silent_disappearance_effect = yes }
		}
	}
}

###########################
## pam_theocratic_ruler_events.0007 — Blissful Harmony
############################

pam_theocratic_ruler_events.0007 = { 
	type = character_event
	title = pam_theocratic_ruler_events.0007.t
	desc = pam_theocratic_ruler_events.0007.desc
	theme = christian
	override_background = garden
	override_effect_2d = { reference = legend_glow }
	left_portrait = {
		character = root
		animation = acknowledging_crozier
		camera = camera_event_very_right_close
	}
	lower_right_portrait = {
		character = liege
	}

	immediate = {
		capital_county = {
			save_scope_as = blissful_harmony_capital
		}
	}

	cooldown = { years = 20 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		primary_title = {
			title_held_years >= 10
		}
		is_available_healthy_adult = yes
		is_at_war = no
		is_in_debt = no
		NOT = {
			any_sub_realm_county = {
				county_control <= 90
			}
		}
		exists = liege
		liege = {
			opinion = {
				target = root
				value >= low_positive_opinion
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			stress >= 50
		}
		modifier = {
			add = 0.25
			spiritual_fulfillment <= -25
		}
	}

	# Ambitious: This peace is only a pause, there is still more to do.
	option = {
		name = pam_theocratic_ruler_events.0007.a
		trigger = {
			has_trait = ambitious
		}
		add_prestige = minor_prestige_gain
		add_character_modifier = {
			modifier = pam_blissful_harmony_restless_ambition_modifier
			years = 5
		}
		stress_and_fulfillment_impact = {
			ambitious = medium_stress_impact_loss
			content = medium_stress_impact_gain
			lazy = major_stress_impact_gain
			calm = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			modifier = {
				has_trait = ambitious
				factor = 2
			}
			modifier = {
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = calm
				}
				factor = 0
			}
		}
	}

	# Spread the gospel of the good life.
	option = {
		name = pam_theocratic_ruler_events.0007.b
		add_piety = medium_piety_gain
		capital_county = {
			add_county_modifier = {
				modifier = pam_blissful_harmony_gospel_modifier
				years = 5
			}
		}
		stress_and_fulfillment_impact = {
			shy = minor_stress_impact_gain
			cynical = medium_stress_impact_gain
			callous = medium_stress_impact_gain
		}
		ai_chance = {
			base = 35
			modifier = {
				OR = {
					has_trait = zealous
					has_trait = gregarious
				}
				factor = 2
			}
			modifier = {
				OR = {
					has_trait = shy
					has_trait = cynical
					has_trait = callous
				}
				factor = 0.5
			}
		}
	}

	# Let me just listen and be still.
	option = {
		name = pam_theocratic_ruler_events.0007.c
		stress_and_fulfillment_impact = {
			base = medium_stress_impact_loss
			ambitious = minor_stress_impact_gain
			wrathful = minor_stress_impact_gain
		}
		ai_chance = {
			base = 55
			modifier = {
				OR = {
					has_trait = content
					has_trait = calm
					has_trait = temperate
				}
				factor = 2
			}
			modifier = {
				OR = {
					has_trait = ambitious
					has_trait = wrathful
				}
				factor = 0.5
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0008 — The Use of Stone
############################

pam_theocratic_ruler_events.0008 = {
	type = character_event
	title = pam_theocratic_ruler_events.0008.t
	desc = pam_theocratic_ruler_events.0008.desc
	theme = christian
	override_background = pam_cathedral_construction
	left_portrait = {
		character = root
		animation = admiration
		camera = camera_body_left
	}

	cooldown = { years = 15 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		is_available_adult = yes
		domicile ?= {
			NOT = { has_domicile_building = episcopal_palace_03 }
			has_ongoing_domicile_construction = no
			NOR = {
				has_domicile_construction = episcopal_palace_01
				has_domicile_construction = episcopal_palace_02
				has_domicile_construction = episcopal_palace_03
			}
			OR = {
				has_domicile_building = episcopal_palace_01
				has_domicile_building = episcopal_palace_02
				free_external_domicile_building_slots >= 1
			}
		}
		OR = {
			primary_title = { title_held_years <= 15 }
			any_held_title = {
				title_held_years <= 15
				tier >= tier_duchy
			}
		}
		any_sub_realm_county = {
			holder = root
			title_held_years <= 15
			any_county_province = {
				has_holding_type = castle_holding
				has_building_or_higher = curtain_walls_01
			}
		}
	}

	immediate = {
		random_sub_realm_county = {
			limit = {
				holder = root
				title_held_years <= 15
				any_county_province = {
					has_holding_type = castle_holding
					has_building_or_higher = curtain_walls_01
				}
			}
			save_scope_as = inherited_stone_county
			random_county_province = {
				limit = {
					has_holding_type = castle_holding
					has_building_or_higher = curtain_walls_01
				}
				weight = {
					base = 1
					modifier = {
						add = 5
						has_building_or_higher = curtain_walls_05
					}
					modifier = {
						add = 3
						has_building_or_higher = curtain_walls_03
					}
				}
				save_scope_as = stone_province
			}
		}
	}

	# Architect: direct future stone toward the estate.
	option = {
		name = pam_theocratic_ruler_events.0008.a
		trigger = {
			has_trait = architect
		}
		add_character_modifier = {
			modifier = pam_stone_for_god_estate_build_discount_modifier
			years = 5
		}
		ai_chance = {
			base = 15
			modifier = {
				has_trait = architect
				factor = 2
			}
		}
	}

	# Repurpose walls for the Episcopal Palace.
	option = {
		name = pam_theocratic_ruler_events.0008.b
		scope:stone_province = {
			destroy_or_downgrade_building_effect = { BUILDING = curtain_walls }
		}
		domicile ?= {
			switch = {
				trigger = has_domicile_building
				episcopal_palace_02 = { construct_domicile_building = episcopal_palace_03 }
				episcopal_palace_01 = { construct_domicile_building = episcopal_palace_02 }
				fallback = {
					construct_domicile_building = episcopal_palace_01
				}
			}
		}
		scope:inherited_stone_county = {
			change_county_control = medium_county_control_loss
		}
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			diligent = minor_stress_impact_loss
			content = minor_stress_impact_gain
		}
		ai_chance = { # Setting to zero since we do not want the ai to remove their lieges work.
			base = 0
		}
	}

	# Ora et labora: Every building is an act of worship.
	option = {
		name = pam_theocratic_ruler_events.0008.c
		flavor = pam_theocratic_ruler_events.0008.c.flavor
		reason = has_tenet
		trigger = {
			OR = {
				has_personal_tenet = tenet_ora_et_labora
				rite = { rite_has_tenet = tenet_ora_et_labora }
			}
		}
		add_piety = medium_piety_gain
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			diligent = minor_stress_impact_loss
			lazy = minor_stress_impact_gain
			cynical = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			modifier = {
				OR = {
					has_trait = zealous
					has_trait = diligent
				}
				factor = 2
			}
			modifier = {
				OR = {
					has_trait = lazy
					has_trait = cynical
				}
				factor = 0.25
			}
		}
	}

	# Leave the walls - Opt out
	option = {
		name = pam_theocratic_ruler_events.0008.d
		trigger = {
			NOR = {
				has_personal_tenet = tenet_ora_et_labora
				rite = { rite_has_tenet = tenet_ora_et_labora }
			}
		}
		ai_chance = {
			base = 20
		}
	}
}

###########################
## pam_theocratic_ruler_events.0009 - Trouble with the flock
############################

pam_theocratic_ruler_events.0009 = { 
	type = character_event
	title = pam_theocratic_ruler_events.0009.t
	desc = pam_theocratic_ruler_events.0009.desc
	theme = christian
	left_portrait = {
		character = root
		animation = acknowledging_crozier
		camera = camera_event_center_pointing_right
	}

	right_portrait = {
		character = player_heir
		animation = anger
		camera = camera_event_right_pointing_right
	}

	cooldown = { years = 15 }
	
	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		is_available_adult = yes
		exists = player_heir
		player_heir = {
			NOT = { has_trait = wounded }
			has_personality_emotional_trigger = yes
		}
	}
	
	immediate = {
		player_heir = {
			save_scope_as = heir
		}
	}

	
	# Counsel the heir in patience and doctrine.
	option = {
		name = pam_theocratic_ruler_events.0009.a
		skill = learning
		duel = {
			skill = learning
			value = medium_skill_rating
			60 = {
				desc = pam_theocratic_ruler_events.0009.a.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_theocratic_ruler_events.0009.a.toast.success
					left_icon = scope:heir
					right_icon = root
					primary_title = {
						change_appointment_investment = {
							target = scope:heir
							value = 20
						}
					}
					scope:heir = {
						add_opinion = {
							modifier = mentored_me_opinion
							target = root
						}
					}
					add_learning_lifestyle_xp = minor_lifestyle_xp
				}
			}
			40 = {
				desc = pam_theocratic_ruler_events.0009.a.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				send_interface_toast = {
					type = event_toast_effect_bad
					title = pam_theocratic_ruler_events.0009.a.toast.failure
					left_icon = scope:heir
					right_icon = root
					add_stress = minor_stress_gain
					scope:heir = {
						add_stress = minor_stress_gain
					}
				}
			}
		}
		ai_chance = {
			base = 50
			compare_modifier = {
				value = learning
				multiplier = 1
			}
		}
	}
	option = {
		name = pam_theocratic_ruler_events.0009.b
		flavor = pam_theocratic_ruler_events.0009.b.flavor
		primary_title = {
			change_appointment_investment = {
				target = scope:heir
				value = -50
			}
		}
		scope:heir = {
			add_opinion = {
				modifier = upset_opinion
				target = root
				opinion = low_negative_opinion
			}
		}
		ai_chance = {
			base = 50
			modifier = {
				has_trait = callous
				factor = 2
			}
			modifier = {
				has_trait = compassionate
				factor = 0.5
			}
		}
	}
	# Say your prayers. Discipline your flesh.
	option = {
		name = pam_theocratic_ruler_events.0009.c
		add_piety = minor_piety_gain
		if = {
			limit = { has_trait = flagellant }
			add_stress = minor_stress_loss
		}
		scope:heir = {
			if = {
				limit = { has_trait = flagellant }
				increase_wounds_effect = { REASON = whipping }
				add_stress = minor_stress_loss
			}
			else = {
				increase_wounds_effect = { REASON = whipping }
				if = {
					limit = { can_be_flagellant = yes }
					random = {
						chance = 35
						add_trait = flagellant
					}
				}
			}
		}
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			sadistic = minor_stress_impact_gain
			compassionate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 35
			modifier = {
				has_trait = flagellant
				factor = 2
			}
			modifier = {
				has_trait = zealous
				factor = 1.5
			}
			modifier = {
				has_trait = compassionate
				factor = 0.25
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0010 — Sword or the Crozier
############################

pam_theocratic_ruler_events.0010 = {
	type = character_event
	title = pam_theocratic_ruler_events.0010.t
	desc = pam_theocratic_ruler_events.0010.desc
	theme = christian
	override_background = throne_room
	left_portrait = {
		character = root
		animation = divine_gesture_2
		camera = camera_event_center_moved_left
	}
	lower_center_portrait = {
		character = cp:councillor_marshal
		animation = celebrate_sword
	}
	lower_right_portrait = {
		character = cp:councillor_court_chaplain
		animation = acknowledging
	}

	cooldown = { years = 15 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		is_available_adult = yes
		has_trait = pilgrim
		any_memory = {
			memory_type = completed_pilgrimage
		}
		any_memory = {
			OR = {
				has_memory_type = battle_won_memory
				has_memory_type = battle_lost_memory
			}
		}
		cp:councillor_marshal ?= {
			is_available_adult = yes
		}
		cp:councillor_court_chaplain ?= {
			is_available_adult = yes
		}
	}

	immediate = {
		if = {
			limit = {
				any_memory = {
					memory_type = completed_pilgrimage
					var:first_pilgrimage = flag:completed_pilgrimage
				}
			}
			random_memory = {
				memory_type = completed_pilgrimage
				limit = {
					var:first_pilgrimage = flag:completed_pilgrimage
				}
				save_scope_as = pilgrimage_memory
			}
		}
		else = {
			random_memory = {
				memory_type = completed_pilgrimage
				save_scope_as = pilgrimage_memory
			}
		}
		random_memory = {
			limit = {
				OR = {
					has_memory_type = battle_won_memory
					has_memory_type = battle_lost_memory
				}
			}
			save_scope_as = battle_memory
		}
	}

	# The sword
	option = {
		name = pam_theocratic_ruler_events.0010.a
		skill = martial
		add_martial_skill = 1
		add_martial_lifestyle_xp = medium_lifestyle_xp
		cp:councillor_marshal= {
			add_opinion = {
				modifier = pleased_opinion
				target = root
				opinion = low_positive_opinion
			}
		}
		ai_chance = {
			base = 35
			compare_modifier = {
				value = martial
				multiplier = 1
			}
			ai_value_modifier = {
				ai_boldness = 0.35
				ai_energy = 0.2
			}
		}
	}

	# The crozier
	option = {
		name = pam_theocratic_ruler_events.0010.b
		skill = learning
		add_learning_skill = 1
		add_learning_lifestyle_xp = medium_lifestyle_xp
		cp:councillor_court_chaplain = {
			add_opinion = {
				modifier = pleased_opinion
				target = root
				opinion = low_positive_opinion
			}
		}
		ai_chance = {
			base = 35
			compare_modifier = {
				value = learning
				multiplier = 1
			}
			ai_value_modifier = {
				ai_rationality = 0.35
				ai_zeal = 0.2
			}
		}
	}

	# Both sword and crozier
	option = {
		name = pam_theocratic_ruler_events.0010.c
		add_piety = medium_piety_gain
		cp:councillor_marshal = {
			add_opinion = {
				modifier = pleased_opinion
				target = root
				opinion = low_positive_opinion
			}
		}
		cp:councillor_court_chaplain = {
			add_opinion = {
				modifier = pleased_opinion
				target = root
				opinion = low_positive_opinion
			}
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_zeal = 0.5
				ai_honor = 0.25
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0011 — Rest for the Fallen
############################

pam_theocratic_ruler_events.0011 = {
	type = character_event
	title = pam_theocratic_ruler_events.0011.t
	desc = pam_theocratic_ruler_events.0011.desc
	theme = christian
	override_background = { reference = temple }

	left_portrait = {
		character = root
		animation = prayer
	}
	right_portrait = {
		character = scope:landed_subject
		animation = grief
	}
	artifact = {
		target = scope:crusader_sword
		position = lower_center_portrait
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_available_adult = yes
		OR = {
			has_religion = religion:catholic_religion
			has_religion = religion:protestant_religion
			has_religion = religion:eastern_orthodox_religion
			has_religion = religion:oriental_orthodox_religion
		}
		NOT = {
    		any_character_artifact = {
        		artifact_type = sword
    		}
		}
		any_vassal_or_below = {
			this != root
			is_available = yes
			is_landed = yes
			faith = root.faith
		}
	}

	immediate = {
		random_vassal_or_below = {
			limit = {
				this != root
				is_available = yes
				is_landed = yes
				faith = root.faith
			}
			save_scope_as = landed_subject
		}
		hidden_effect = {
			create_artifact = {
				name = pam_fallen_knight_crusader_sword_name
				description = pam_fallen_knight_crusader_sword_desc
				type = sword
				visuals = sword
				modifier = artifact_prowess_2_modifier
				save_scope_as = crusader_sword
			}
			scope:crusader_sword = {
				set_artifact_rarity_common = yes
				set_max_durability = 40
			}
		}
	}

	# Grant church burial with the crusader sword.
	option = {
		name = pam_theocratic_ruler_events.0011.a
		flavor = pam_theocratic_ruler_events.0011.a.flavor
		scope:landed_subject = {
			pay_treasury_or_gold = {
				target = root
				value = minor_treasury_or_gold_value
			}
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:landed_subject
					type = strong_favor_hook
				}
			}
			add_hook = {
				target = scope:landed_subject
				type = strong_favor_hook
			}
		}
		add_piety = minor_piety_loss
		destroy_artifact = scope:crusader_sword
		scope:landed_subject = {
			add_opinion = {
				modifier = grateful_opinion
				target = root
				opinion = 15
			}
		}
		stress_and_fulfillment_impact = {
			compassionate = minor_stress_impact_loss
			zealous = minor_stress_impact_loss
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 45
			ai_value_modifier = {
				ai_compassion = 0.35
				ai_zeal = 0.25
				ai_honor = 0.2
			}
		}
	}

	# Keep the sword while still granting burial.
	option = {
		name = pam_theocratic_ruler_events.0011.b
		flavor = pam_theocratic_ruler_events.0011.b.flavor
		scope:crusader_sword = { set_owner = root }
		scope:landed_subject = {
			add_opinion = {
				modifier = upset_opinion
				target = root
				opinion = -20
			}
		}
		stress_and_fulfillment_impact = {
			deceitful = minor_stress_impact_loss
			greedy = minor_stress_impact_loss
			honest = minor_stress_impact_gain
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_greed = 0.4
				ai_rationality = 0.2
			}
		}
	}

	# Bury the knight beneath the open sky.
	option = {
		name = pam_theocratic_ruler_events.0011.c
		flavor = pam_theocratic_ruler_events.0011.c.flavor
		add_piety = medium_piety_gain
		destroy_artifact = scope:crusader_sword
		scope:landed_subject = {
			add_opinion = {
				modifier = upset_opinion
				target = root
				opinion = -10
			}
		}
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			stubborn = minor_stress_impact_loss
			compassionate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_zeal = 0.45
				ai_boldness = 0.2
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0012 — A Once Sacred Grove (travel)
############################

pam_theocratic_ruler_events.0012 = {
	type = character_event
	title = pam_theocratic_ruler_events.0012.t
	desc = pam_theocratic_ruler_events.0012.desc
	theme = travel
	override_background = { reference = wilderness_forest }

	left_portrait = {
		character = root
		animation = holding_staff
		camera = camera_event_center_pointing_forward
	}
	right_portrait = {
		character = scope:grove_subject
		animation = preaching_no_book
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_available_travelling_adult = yes
		is_playable_character = yes
		is_location_valid_for_travel_event_on_land = yes
		is_liege_or_above_of = location.county.holder
		location = {
			terrain = forest
			province_has_no_holding_trigger = yes
		}
	}

	immediate = {
		location = { save_scope_as = grove_province }
		location.county = { save_scope_as = grove_county }
		create_character = {
			location = scope:grove_province
			template = generic_peasant_character
			faith = scope:grove_county.faith
			rite = scope:grove_county.rite
			culture = scope:grove_county.culture
			save_scope_as = grove_subject
		}
		set_variable = pam_theocratic_ruler_events_0012_created_character
	}

	# Embrace nature: adopt Sanctity of Nature and fund a forest temple.
	option = {
		name = pam_theocratic_ruler_events.0012.a
		reason = has_tenet
		trigger = {
			free_personal_tenet_slots > 0
			NOT = { has_personal_tenet = tenet:tenet_sanctity_of_nature }
			any_character_tenet = {
				this = tenet:tenet_sanctity_of_nature
				knowledge_filter = known
			}
			short_term_treasury_or_gold >= pam_sacred_grove_temple_cost_value
		}
		show_as_unavailable = {
			always = yes
		}
		set_personal_tenet = tenet:tenet_sanctity_of_nature
		remove_treasury_or_gold = pam_sacred_grove_temple_cost_value
		scope:grove_province = {
			begin_create_holding = {
				type = church_holding
				refund_cost = { gold = pam_sacred_grove_temple_cost_value }
			}
		}
		add_piety = minor_piety_gain
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			humble = minor_stress_impact_loss
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_zeal = 0.45
				ai_compassion = 0.25
				ai_greed = -0.35
			}
		}
	}

	# Trick the subject into revealing the groves, then harvest the timber.
	option = {
		name = pam_theocratic_ruler_events.0012.b
		skill = intrigue
		duel = {
			skill = intrigue
			target = scope:grove_subject
			50 = {
				desc = pam_theocratic_ruler_events.0012.b.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_theocratic_ruler_events.0012.b.toast.success
					left_icon = root
					right_icon = scope:grove_subject
					add_short_term_gold = medium_gold_value
					scope:grove_county = {
						change_county_control = minor_county_control_loss
					}
				}
			}
			50 = {
				desc = pam_theocratic_ruler_events.0012.b.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				send_interface_toast = {
					type = event_toast_effect_bad
					title = pam_theocratic_ruler_events.0012.b.toast.failure
					left_icon = root
					right_icon = scope:grove_subject
					scope:grove_county = {
						change_county_control = miniscule_county_control_loss
					}
				}
			}
		}
		stress_and_fulfillment_impact = {
			deceitful = minor_stress_impact_loss
			greedy = minor_stress_impact_loss
			honest = minor_stress_impact_gain
			zealous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 30
			compare_modifier = {
				value = intrigue
				multiplier = 1
			}
			ai_value_modifier = {
				ai_greed = 0.4
				ai_rationality = 0.2
			}
		}
	}

	# Condemn the wilderness as diabolical.
	option = {
		name = pam_theocratic_ruler_events.0012.c
		add_piety = medium_piety_gain
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			stubborn = minor_stress_impact_loss
			cynical = minor_stress_impact_gain
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_zeal = 0.45
				ai_boldness = 0.2
			}
		}
	}

	after = {
		if = {
			limit = {
				has_variable = pam_theocratic_ruler_events_0012_created_character
				is_ai = yes
			}
			remove_variable = pam_theocratic_ruler_events_0012_created_character
			scope:grove_subject = { silent_disappearance_effect = yes }
		}
	}
}

###########################
## pam_theocratic_ruler_events.0013 — An Angelic Anthem
############################

pam_theocratic_ruler_events.0013 = {
	type = character_event
	title = pam_theocratic_ruler_events.0013.t
	desc = pam_theocratic_ruler_events.0013.desc
	theme = christian
	override_background = { reference = temple }

	left_portrait = {
		character = root
		animation = prayer_standing_eyes
		camera = camera_event_center_pointing_forward
	}
	right_portrait = {
		character = scope:angelic_singer
		animation = divine_ecstasy
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_available_adult = yes
	}

	immediate = {
		create_character = {
			location = root.capital_province
			template = beautiful_peasant_character
			gender_female_chance = 100
			faith = root.faith
			rite = root.rite
			culture = root.culture
			dynasty = none
			save_scope_as = angelic_singer
		}
		set_variable = pam_theocratic_ruler_events_0013_created_character
	}

	# Convince the singer to take vows and join the choir permanently.
	option = {
		name = pam_theocratic_ruler_events.0013.a
		skill = learning
		duel = {
			skills = { learning diplomacy }
			target = scope:angelic_singer
			50 = {
				desc = pam_theocratic_ruler_events.0013.a.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_theocratic_ruler_events.0013.a.toast.success
					left_icon = scope:angelic_singer
					right_icon = root
					add_courtier = scope:angelic_singer
					scope:angelic_singer = {
						add_trait = devoted
					}
					add_piety = medium_piety_gain
				}
				remove_variable = pam_theocratic_ruler_events_0013_created_character
			}
			50 = {
				desc = pam_theocratic_ruler_events.0013.a.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = pam_theocratic_ruler_events.0013.a.toast.failure
					left_icon = scope:angelic_singer
					right_icon = root
				}
			}
		}
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			diligent = minor_stress_impact_loss
			cynical = minor_stress_impact_gain
		}
		ai_chance = {
			base = 45
			compare_modifier = {
				value = learning
				multiplier = 0.5
			}
			compare_modifier = {
				value = diplomacy
				multiplier = 0.5
			}
			ai_value_modifier = {
				ai_zeal = 0.4
				ai_rationality = 0.2
			}
		}
	}

	# Keep her at court and begin a seduction.
	option = {
		name = pam_theocratic_ruler_events.0013.b
		trigger = {
			has_trait = lustful
			sex_opposite_of = scope:angelic_singer
			NOR = {
				has_trait = celibate
				has_sexuality = asexual
			}
			can_start_scheme = {
				type = seduce
				target_character = scope:angelic_singer
			}
		}
		show_as_unavailable = {
			always = yes
		}
		add_visiting_courtier = scope:angelic_singer
		hidden_effect = {
			root = { save_scope_as = host }
			scope:angelic_singer = {
				save_scope_as = guest
				trigger_event = courtier_guest_management.1001
			}
		}
		start_scheme = {
			type = seduce
			target_character = scope:angelic_singer
		}
		remove_variable = pam_theocratic_ruler_events_0013_created_character
		stress_and_fulfillment_impact = {
			lustful = minor_stress_impact_loss
			chaste = medium_stress_impact_gain
			zealous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_boldness = 0.35
				ai_rationality = -0.2
			}
			modifier = {
				factor = 0
				NOT = { has_trait = lustful }
			}
		}
	}

	# Singing should be left to the cantors.
	option = {
		name = pam_theocratic_ruler_events.0013.c
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			stubborn = minor_stress_impact_loss
			lustful = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_zeal = 0.35
				ai_honor = 0.2
			}
		}
	}

	after = {
		if = {
			limit = {
				has_variable = pam_theocratic_ruler_events_0013_created_character
				is_ai = yes
			}
			remove_variable = pam_theocratic_ruler_events_0013_created_character
			scope:angelic_singer = {
				if = {
					limit = {
						NOR = {
							is_courtier_of = root
							is_foreign_court_guest_of = root
						}
					}
					silent_disappearance_effect = yes
				}
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0014 — A Convert's Plea
############################

scripted_trigger pam_theocratic_ruler_0014_neighbour_county_trigger = {
	culture != root.culture
	faith != root.faith
}

scripted_effect pam_theocratic_ruler_events_0014_grant_mass_conversion_discount_effect = {
	set_variable = pam_theocratic_ruler_events_0014_mass_conversion_discount
}

pam_theocratic_ruler_events.0014 = {
	type = character_event
	title = pam_theocratic_ruler_events.0014.t
	desc = pam_theocratic_ruler_events.0014.desc
	theme = christian
	override_background = { reference = temple }

	left_portrait = {
		character = root
		animation = prayer_crossing
	}
	right_portrait = {
		character = scope:convert_peasant
		animation = personality_compassionate
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_available_adult = yes
		free_personal_tenet_slots > 0
		OR = {
			any_sub_realm_county = {
				any_neighboring_county = {
					pam_theocratic_ruler_0014_neighbour_county_trigger = yes
				}
			}
			any_held_title = {
				title_tier = county
				is_landless_type_title = no
				any_neighboring_county = {
					pam_theocratic_ruler_0014_neighbour_county_trigger = yes
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				any_sub_realm_county = {
					any_neighboring_county = {
						pam_theocratic_ruler_0014_neighbour_county_trigger = yes
					}
				}
			}
			random_sub_realm_county = {
				limit = {
					any_neighboring_county = {
						pam_theocratic_ruler_0014_neighbour_county_trigger = yes
					}
				}
				random_neighboring_county = {
					limit = {
						pam_theocratic_ruler_0014_neighbour_county_trigger = yes
					}
					save_scope_as = neighbour_county
				}
			}
		}
		else = {
			random_held_title = {
				limit = {
					tier = county
					any_neighboring_county = {
						pam_theocratic_ruler_0014_neighbour_county_trigger = yes
					}
				}
				random_neighboring_county = {
					limit = {
						pam_theocratic_ruler_0014_neighbour_county_trigger = yes
					}
					save_scope_as = neighbour_county
				}
			}
		}
		scope:neighbour_county.faith = {
			save_scope_as = neighbour_faith
			main_rite = {
				random_rite_tenet = {
					status = core
					limit = {
						NOT = { root = { has_personal_tenet = this } }
						NOT = {
							root = {
								rite = {
									has_tenet_status = {
										tenet = this
										status = core
									}
								}
							}
						}
					}
					save_scope_as = offered_tenet_a
				}
				if = {
					limit = {
						exists = scope:offered_tenet_a
					}
					random_rite_tenet = {
						status = core
						limit = {
							this != scope:offered_tenet_a
							NOT = { root = { has_personal_tenet = this } }
							NOT = {
								root = {
									rite = {
										has_tenet_status = {
											tenet = this
											status = core
										}
									}
								}
							}
						}
						save_scope_as = offered_tenet_b
					}
				}
			}
		}
		create_character = {
			location = root.capital_province
			template = generic_peasant_character
			faith = root.faith
			rite = root.rite
			culture = scope:neighbour_county.culture
			dynasty = none
			save_scope_as = convert_peasant
		}
		set_variable = pam_theocratic_ruler_events_0014_created_character
	}

	# Remove this filth from my sight.
	option = {
		name = pam_theocratic_ruler_events.0014.a
		trigger = {
			has_trait = arrogant
		}
		show_as_unavailable = {
			always = yes
		}
		add_dread = minor_dread_gain
		root.culture = {
			change_cultural_acceptance = {
				target = scope:neighbour_county.culture
				value = minor_cultural_acceptance_loss
				desc = pam_theocratic_ruler_events.0014.a.acceptance
			}
		}
		scope:convert_peasant = {
			silent_disappearance_effect = yes
		}
		every_vassal_or_below = {
			limit = {
				has_vassal_stance = zealot
			}
			custom = every_zealot_vassal
			add_opinion = {
				modifier = zealot_approves_of_religious_display_opinion
				target = root
			}
		}
		remove_variable = pam_theocratic_ruler_events_0014_created_character
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			callous = minor_stress_impact_loss
			compassionate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_zeal = 0.5
				ai_boldness = 0.2
			}
			modifier = {
				factor = 0
				NOT = { has_trait = arrogant }
			}
		}
	}

	# Adopt the first neighbouring core tenet as a personal tenet.
	option = {
		name = pam_theocratic_ruler_events.0014.b
		trigger = {
			exists = scope:offered_tenet_a
			free_personal_tenet_slots > 0
		}
		show_as_unavailable = {
			always = yes
		}
		add_known_tenet = scope:offered_tenet_a
		set_personal_tenet = scope:offered_tenet_a
		pam_theocratic_ruler_events_0014_grant_mass_conversion_discount_effect = yes
		custom_tooltip = pam_theocratic_ruler_events.0014.b.tt
		remove_variable = pam_theocratic_ruler_events_0014_created_character
		stress_and_fulfillment_impact = {
			compassionate = minor_stress_impact_loss
			humble = minor_stress_impact_loss
			stubborn = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_zeal = -0.15
				ai_rationality = 0.35
				ai_compassion = 0.25
			}
		}
	}

	# Adopt the second neighbouring core tenet as a personal tenet.
	option = {
		name = pam_theocratic_ruler_events.0014.c
		trigger = {
			exists = scope:offered_tenet_b
			free_personal_tenet_slots > 0
		}
		show_as_unavailable = {
			always = yes
		}
		add_known_tenet = scope:offered_tenet_b
		set_personal_tenet = scope:offered_tenet_b
		pam_theocratic_ruler_events_0014_grant_mass_conversion_discount_effect = yes
		custom_tooltip = pam_theocratic_ruler_events.0014.c.tt
		remove_variable = pam_theocratic_ruler_events_0014_created_character
		stress_and_fulfillment_impact = {
			compassionate = minor_stress_impact_loss
			humble = minor_stress_impact_loss
			stubborn = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			ai_value_modifier = {
				ai_zeal = -0.15
				ai_rationality = 0.35
				ai_compassion = 0.25
			}
		}
	}

	# Does the sheep guide the shepherd?
	option = {
		name = pam_theocratic_ruler_events.0014.d
		add_piety = medium_piety_gain
		every_vassal_or_below = {
			limit = {
				has_vassal_stance = zealot
			}
			custom = every_zealot_vassal
			add_opinion = {
				modifier = zealot_approves_of_religious_display_opinion
				target = root
			}
		}
		stress_and_fulfillment_impact = {
			zealous = minor_stress_impact_loss
			stubborn = minor_stress_impact_loss
			cynical = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_zeal = 0.4
				ai_honor = 0.25
			}
		}
	}

	after = {
		if = {
			limit = {
				has_variable = pam_theocratic_ruler_events_0014_created_character
				is_ai = yes
			}
			remove_variable = pam_theocratic_ruler_events_0014_created_character
			scope:convert_peasant = {
				if = {
					limit = {
						is_alive = yes
					}
					silent_disappearance_effect = yes
				}
			}
		}
	}
}

###########################
## pam_theocratic_ruler_events.0015 — Prayer Hearing
############################

scripted_effect pam_theocratic_ruler_events_0015_highest_clerical_skill_point_effect = {
	if = {
		limit = {
			stewardship >= learning
			stewardship >= intrigue
		}
		add_stewardship_skill = 1
	}
	else_if = {
		limit = {
			learning >= intrigue
		}
		add_learning_skill = 1
	}
	else = {
		add_intrigue_skill = 1
	}
}

pam_theocratic_ruler_events.0015 = {
	type = character_event
	title = pam_theocratic_ruler_events.0015.t
	desc = pam_theocratic_ruler_events.0015.desc
	theme = christian
	window = big_event_window
	override_background = { reference = temple }

	left_portrait = {
		character = root
		animation = divine_gesture_3
		camera = camera_event_center_close_to_right
	}
	center_portrait = {
		character = scope:succession_heir_first
		animation = prayer_standing_eyes
		camera = camera_event_center_pointing_forward_fov35
	}
	right_portrait = {
		character = scope:succession_heir_second
		animation = prayer_standing
		camera = camera_event_center_moved_left
	}

	cooldown = { years = 8 }

	trigger = {
		pam_government_is_religious_playable_trigger = yes
		is_landed = yes
		is_available_adult = yes
		primary_title = {
			any_title_heir = {
				save_temporary_scope_as = heir_temp
				prev = {
					place_in_line_of_succession = {
						target = scope:heir_temp
						value = 2
					}
				}
			}
		}
	}

	immediate = {
		primary_title = {
			save_scope_as = appointment_title
			every_title_heir = {
				limit = {
					save_temporary_scope_as = heir_temp
					scope:appointment_title = {
						place_in_line_of_succession = {
							target = scope:heir_temp
							value = 1
						}
					}
				}
				save_scope_as = succession_heir_first
			}
			every_title_heir = {
				limit = {
					save_temporary_scope_as = heir_temp
					scope:appointment_title = {
						place_in_line_of_succession = {
							target = scope:heir_temp
							value = 2
						}
					}
				}
				save_scope_as = succession_heir_second
			}
		}
	}

	# Tutor the first successor in scripture.
	option = {
		name = pam_theocratic_ruler_events.0015.a
		scope:succession_heir_first = {
			pam_theocratic_ruler_events_0015_highest_clerical_skill_point_effect = yes
		}
		primary_title = {
			change_appointment_investment = {
				target = scope:succession_heir_first
				value = clerical_appointment_score_medium_value
			}
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
			patient = minor_stress_impact_loss
			lazy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_rationality = 0.25
				ai_compassion = 0.2
			}
		}
	}

	# Guide the second successor through the holy texts.
	option = {
		name = pam_theocratic_ruler_events.0015.b
		scope:succession_heir_second = {
			pam_theocratic_ruler_events_0015_highest_clerical_skill_point_effect = yes
		}
		primary_title = {
			change_appointment_investment = {
				target = scope:succession_heir_second
				value = clerical_appointment_score_medium_value
			}
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
			patient = minor_stress_impact_loss
			lazy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_rationality = 0.25
				ai_compassion = 0.2
			}
		}
	}

	# Demonstrate superior mastery of doctrine.
	option = {
		name = pam_theocratic_ruler_events.0015.c
		skill = learning
		duel = {
			skill = learning
			value = medium_skill_rating
			60 = {
				desc = pam_theocratic_ruler_events.0015.c.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_theocratic_ruler_events.0015.c.toast.success
					left_icon = root
					add_piety = medium_piety_gain
					add_learning_lifestyle_xp = minor_lifestyle_xp
				}
			}
			40 = {
				desc = pam_theocratic_ruler_events.0015.c.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				send_interface_toast = {
					type = event_toast_effect_bad
					title = pam_theocratic_ruler_events.0015.c.toast.failure
					left_icon = root
					add_stress = minor_stress_gain
				}
			}
		}
		stress_and_fulfillment_impact = {
			arrogant = minor_stress_impact_loss
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			compare_modifier = {
				value = learning
				multiplier = 1
			}
			ai_value_modifier = {
				ai_boldness = 0.2
			}
		}
	}
}
