targeted_decisions = {
	lollard_revoke_temple = {
		is_high_prio = yes
		ai_check_interval = 12
		filter = vassals
		ai_target_filter = vassals

		only_rulers = yes

		from_potential = {
			religion = lollard
		}

		potential = {
			vassal_of = FROM
			is_ruler = yes
			holy_order = no
			capital_holding = { holding_type = temple }

			trigger_if = {
				limit = { religion = lollard }

				OR = {
					trait = bad_priest_christian
					higher_real_tier_than = COUNT
				}
			}

			NOR = {
				has_character_flag = lollard_revoking_titles
				any_demesne_title = { temporary = yes }
				reverse_has_truce = FROM
				has_non_aggression_pact_with = FROM
			}

			is_inaccessible_trigger = no
		}
		allow = {
			ROOT = {
				war = no
			}
			FROM = {
				war = no
				is_not_in_regency_trigger = yes
			}
		}

		effect = {
			FROM = {
				add_character_modifier = {
					name = do_not_disturb
					days = 3
					hidden = yes
				}
			}

			set_character_flag = lollard_revoking_titles

			letter_event = {
				id = Plus.2021
				days = 3
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				FROM = {
					trigger_if = {
						limit = { trait = cynical }

						OR = {
							trait = ambitious
							trait = greedy
							trait = envious
						}
					}

					opinion = {
						who = ROOT
						value < 0
					}
				}
			}
		}
	}

	messalian_castrate = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		potential = {
			religion = messalian

			OR = {
				trait = lunatic
				trait = possessed
				trait = depressed
			}

			is_female = no
			is_adult = yes
			prisoner = no

			NOR = {
				trait = eunuch
				trait = incapable
			}
		}

		allow = {
			OR = {
				trait = lunatic
				trait = possessed
				trait = depressed
			}
		}

		effect = {
			piety = 300
			add_trait = eunuch

			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_ballless
			}

			if = {
				limit = { NOT = { religion_authority = 0.7 } }

				religion_authority = {
					name = apokopos
					years = 10
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				NOR = {
					trait = cynical
					trait = craven
				}
			}

			mult_modifier = {
				factor = 10

				OR = {
					trait = zealous
					trait = lunatic
				}
			}
		}
	}
}

decisions = {
	cathar_consolamentum = {
		ai_check_interval = 12

		potential = {
			OR = {
				religion = cathar
				religion = bogomilist
			}

			NOR = {
				trait = perfecti
				has_character_modifier = voice_of_satan
				has_character_flag = consolamentum_taken
			}
		}

		allow = {
			is_adult = yes
			prisoner = no
			is_incapable = no
			piety >= 0

			NOR = {
				trait = cynical
				trait = lustful
				trait = hedonist
				trait = master_seducer
				trait = master_seductress
			}
		}

		effect = {
			set_character_flag = consolamentum_taken
			add_trait = perfecti
			add_trait_silently_zealous_effect = yes
		}

		ai_will_do = {
			factor = 1

			trigger = {
				age > 25

				OR = {
					trait = zealous
					trait = possessed
					trait = lunatic
					has_education_learning_trigger = yes
					is_theocracy = yes
				}

				trigger_if = {
					limit = {
						has_job_title = job_spiritual
						religion = bogomilist
						NOT = { trait = zealous }
					}

					liege = {
						OR = {
							has_law = bogomilist_church_3
							has_law = bogomilist_church_4
						}
					}
				}
			}

			mult_modifier = {
				factor = 10
				is_theocracy = yes
			}

			mult_modifier = {
				factor = 5
				trait = zealous
			}

			mult_modifier = {
				factor = 0.25

				OR = {
					trait = gluttonous
					trait = envious
					trait = greedy
				}
			}
		}
	}

	cathar_adoption = {
		ai_check_interval = 12

		only_playable = yes

		potential = {
			OR = {
				religion = cathar
				religion = bogomilist
			}

			NOT = {
				any_child = {
					is_alive = yes
					NOT = { trait = bastard }
				}
			}
		}

		allow = {
			OR = {
				trait = perfecti
				trait = celibate
				practical_age >= 45
				fertility < 0.05
			}

			any_realm_province = {
				religion = ROOT
				hidden_trigger = { held_under_PREV = yes }
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			character_event = {
				id = Plus.2006
				tooltip = SEARCH_ADOPTED_CHILD
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				NOT = {
					player_heir = { always = yes }
				}
			}
		}
	}

	create_cathar_papacy = {
		is_high_prio = yes
		ai_check_interval = 24

		only_playable = yes

		potential = {
			religion = cathar

			NOR = {
				is_title_active = d_cathar
				has_global_flag = cathar_papacy_founded
				any_demesne_title = { temporary = yes }
			}
		}

		allow = {
			independent = yes
			piety >= 1000
			higher_real_tier_than = DUKE
			trait = perfecti
			num_of_holy_sites >= 1
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_global_flag = cathar_papacy

			character_event = {
				id = Plus.2012
				tooltip = CREATE_CATHAR_POPE
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	lollard_renounce_vows = {
		ai_check_interval = 12
		potential = {
			religion = lollard

			OR = {
				trait = monk
				trait = nun
				trait = celibate
			}

			is_adult = yes
			NOT = { has_character_modifier = lollard_former_monk }
		}

		allow = {
			piety >= 10
			prisoner = no
			is_incapable = no
		}

		effect = {
			piety = -50

			if = {
				limit = {
					OR = {
						trait = monk
						trait = nun
					}
				}

				add_character_modifier = {
					name = lollard_former_monk
					duration = -1
				}
			}

			if = {
				limit = { trait = monk }
				remove_trait = monk
			}

			if = {
				limit = { trait = nun }
				remove_trait = nun
			}

			if = {
				limit = { trait = celibate }
				remove_trait = celibate
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				trigger_if = {
					limit = {
						OR = {
							trait = zealous
							trait = content
							trait = humble
							trait = diligent
						}
					}

					OR = {
						trait = ambitious
						trait = cynical
					}
				}
			}
		}
	}

	lollard_translate_bible = {
		is_high_prio = yes
		ai_check_interval = 12

		only_playable = yes

		potential = {
			higher_real_tier_than = DUKE

			OR = {
				religion = lollard
				religion = waldensian
			}

			OR = {
				has_focus = focus_theology
				has_focus = focus_scholarship
			}

			NOR = {
				has_character_flag = bible_translation_begun
				has_global_flag = bible_translation_finished
			}
		}

		allow = {
			realm_learning >= 25
			realm_size >= 100
			scaled_wealth = 3.0
			NOT = { trait = cynical }
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_character_flag = bible_translation_begun
			scaled_wealth = -3.0

			custom_tooltip = {
				text = BEGIN_TRANSLATING_BIBLE

				random_list = {
					50 = {
						mult_modifier = {
							factor = 1.1
							learning >= 12
						}

						mult_modifier = {
							factor = 1.1
							learning >= 14
						}

						mult_modifier = {
							factor = 1.2
							learning >= 16
						}

						mult_modifier = {
							factor = 1.2
							learning >= 18
						}

						mult_modifier = {
							factor = 1.2
							learning >= 20
						}

						narrative_event = {
							id = Plus.2017
							days = 300
							random = 500
							tooltip = BIBLE_TRANSLATION_GOOD
						}
					}

					50 = {
						mult_modifier = {
							factor = 0.8
							learning < 8
						}

						mult_modifier = {
							factor = 0.8
							learning < 6
						}

						mult_modifier = {
							factor = 0.8
							learning < 4
						}

						narrative_event = {
							id = Plus.2018
							days = 300
							random = 500
							tooltip = BIBLE_TRANSLATION_POOR
						}
					}
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				scaled_wealth = 5.0
				NOT = { has_character_modifier = ai_austerity }
			}
		}
	}

	waldensian_choose_path = {
		is_high_prio = yes
		ai_check_interval = 12

		potential = {
			religion = waldensian

			NOR = {
				trait = sandaliati
				trait = novellani
				trait = doctores
				has_character_flag = wald_chose_path
			}
		}

		allow = {
			is_adult = yes
			is_incapable = no
			prisoner = no
		}

		effect = {
			set_character_flag = wald_chose_path
			character_event = { id = Plus.2031 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	waldensian_charity = {
		ai_check_interval = 12

		only_playable = yes

		potential = {
			religion = waldensian
			is_adult = yes
			NOT = { has_character_flag = voluntary_charity }

			any_realm_province = {
				religion = waldensian
				NOT = { has_province_modifier = wald_charity }
				held_under_PREV = yes
			}
		}

		allow = {
			scaled_wealth = 1.0
			NOT = { trait = greedy }
		}

		effect = {
			set_character_flag = voluntary_charity

			if = {
				limit = { NOT = { trait = charitable } }
				add_trait = charitable
			}

			scaled_wealth = -1.0
			piety = 100

			if = {
				limit = {
					capital_scope = {
						religion = waldensian
						NOT = { has_province_modifier = wald_charity }
					}
				}

				capital_scope = {
					show_scope_change = no

					add_province_modifier = {
						name = wald_charity
						years = 3
					}
				}
			}
			else_if = {
				limit = {
					any_demesne_province = {
						religion = waldensian
						NOT = { has_province_modifier = wald_charity }
						owned_by = PREV
					}
				}

				random_demesne_province = {
					show_scope_change = no

					limit = {
						religion = waldensian
						NOT = { has_province_modifier = wald_charity }
						owned_by = PREV
					}

					add_province_modifier = {
						name = wald_charity
						years = 3
					}
				}
			}
			else = {
				random_realm_province = {
					show_scope_change = no

					limit = {
						religion = waldensian
						NOT = { has_province_modifier = wald_charity }
						held_under_PREV = yes
					}

					add_province_modifier = {
						name = wald_charity
						years = 3
					}
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				scaled_wealth = 3.0
				piety < 50
				NOT = { has_character_modifier = ai_austerity }
			}
		}
	}

	waldensian_hospital = {
		ai_check_interval = 12

		only_playable = yes

		potential = {
			higher_real_tier_than = DUKE
			religion = waldensian
			has_focus = focus_rulership

			capital_scope = {
				NOT = { has_province_modifier = wald_hospital }
			}
		}

		allow = {
			wealth >= 500
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			wealth = -500
			piety = 100

			capital_scope = {
				show_scope_change = no

				if = {
					limit = { NOT = { religion = waldensian } }
					religion = waldensian
				}

				add_province_modifier = {
					name = wald_hospital
					duration = -1
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				wealth >= 1000
				NOT = { has_character_modifier = ai_austerity }
			}
		}
	}

	messalian_abandon_prayers = {
		ai_check_interval = 12

		potential = {
			religion = messalian
			has_character_modifier = messalian_prayer
		}

		allow = {
			has_character_modifier = messalian_prayer
		}

		effect = {
			prestige = -50
			piety = -50
			remove_character_modifier = messalian_prayer
			clr_character_flag = driving_out_lust
			clr_character_flag = driving_out_gluttony
			clr_character_flag = driving_out_greed
			clr_character_flag = driving_out_sloth
			clr_character_flag = driving_out_wrath
			clr_character_flag = driving_out_envy
			clr_character_flag = driving_out_pride
			clr_character_flag = driving_out_cruelty
			clr_character_flag = driving_out_possession
			clr_character_flag = driving_out_lunacy
			clr_character_flag = messalian_temptation

			random = {
				chance = 25
				add_trait = depressed
			}

			add_character_modifier = {
				name = messalian_prayer_timer
				years = 3
				hidden = yes
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				trait = cynical
			}
		}
	}

	messalian_expel_daemon = {
		ai_check_interval = 12

		potential = {
			religion = messalian

			OR = {
				trait = lustful
				trait = hedonist
				trait = gluttonous
				trait = greedy
				trait = slothful
				trait = wroth
				trait = envious
				trait = proud
				trait = cruel
				trait = possessed
				trait = lunatic
			}

			NOR = {
				has_character_modifier = messalian_prayer
				trait = messalian_master
			}
		}

		allow = {
			piety >= 50
			is_adult = yes
			prisoner = no
			is_ill = no
			in_command_trigger = no
			is_incapable = no
			NOT = { trait = cynical }

			custom_tooltip = {
				text = NOT_STOPPED_PRAYER_LAST_3_YEARS
				NOT = { has_character_modifier = messalian_prayer_timer }
			}
		}

		effect = {
			piety = -50

			add_character_modifier = {
				name = messalian_prayer
				duration = -1
			}

			character_event = {
				id = Plus.2041
				tooltip = BEGIN_DRIVING_OUT_DAEMONS
			}
		}

		ai_will_do = {
			factor = 1

			mult_modifier = {
				factor = 10
				trait = zealous
			}

			mult_modifier = {
				factor = 10

				OR = {
					trait = possessed
					trait = lunatic
				}
			}

			mult_modifier = {
				factor = 0.1
				trait = craven
			}

			mult_modifier = {
				factor = 0.1
				trait = content
			}
		}
	}

	messalian_choose_patron_saint = {
		ai_check_interval = 12

		potential = {
			religion = messalian

			OR = {
				is_playable = yes
				is_theocracy = yes
				has_education_learning_trigger = yes
			}

			NOR = {
				has_character_modifier = patron_nestor
				has_character_modifier = patron_lucifer
				has_character_modifier = patron_addai
				has_character_modifier = patron_adelphius
			}
		}

		allow = {
			war = no
			is_adult = yes
			prisoner = no
			NOT = { trait = incapable }
		}

		effect = {
			character_event = {
				id = Plus.2055
				tooltip = choose_messalian_saint
			}
		}

		ai_will_do = {
			factor = 1

			mult_modifier = {
				factor = 5
				trait = zealous
			}

			mult_modifier = {
				factor = 0.5
				trait = cynical
			}
		}
	}

	messalian_become_master = {
		ai_check_interval = 12

		potential = {
			religion = messalian

			NOR = {
				trait = lustful
				trait = gluttonous
				trait = greedy
				trait = slothful
				trait = wroth
				trait = envious
				trait = proud
				trait = cruel
				trait = possessed
				trait = lunatic
				trait = cult_of_lucifer
				has_character_modifier = messalian_prayer
				trait = messalian_master
			}
		}

		allow = {
			piety >= 500
			religion_authority >= 0.7
			is_adult = yes
			prisoner = no
			is_incapable = no
		}

		effect = {
			piety = -500

			religion_authority = {
				name = m_master
				years = 5
			}

			add_trait = messalian_master

			random_list = {
				20 = {
					trigger = { NOT = { trait = hedonist } }
					add_trait = hedonist
				}
				10 = {
					trigger = { NOT = { trait = cruel } }
					add_trait = cruel
				}
				10 = {
					trigger = { NOT = { trait = drunkard } }
					add_trait = drunkard
				}
				10 = {
					trigger = { NOT = { trait = lustful } }
					add_trait = lustful
				}
				10 = {
					trigger = { NOT = { trait = gluttonous } }
					add_trait = gluttonous
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	messalian_join_cult_of_lucifer = {
		ai_check_interval = 24

		potential = {
			religion = messalian
			is_adult = yes
			prisoner = no
			is_incapable = no

			NOR = {
				trait = cult_of_lucifer
				trait = messalian_master
			}
		}
		allow = {
			trait = proud
			intrigue >= 10
			piety >= 50

			OR = {
				has_character_modifier = patron_lucifer
				trait = zealous
				trait = paranoid
				trait = deceitful
				trait = mystic
				trait = theologian
			}
		}

		effect = {
			piety = 100
			prestige = 100
			tooltip = { add_trait = cult_of_lucifer }
			character_event = { id = Plus.2056 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	organise_army_of_lucifer = {
		is_high_prio = yes
		ai_check_interval = 12

		only_playable = yes

		potential = {
			higher_real_tier_than = COUNT
			religion = messalian
			NOT = { is_title_active = d_army_lucifer }
		}

		allow = {
			higher_real_tier_than = DUKE
			prestige = 300
			wealth = 300
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			prestige = -300
			wealth = -300

			activate_title = {
				title = d_army_lucifer
				status = yes
			}

			hidden_effect = {
				narrative_event = { id = Plus.2057 }

				create_character = {
					random_traits = yes
					religion = ROOT
					culture = ROOT
					dynasty = none
					female = no
					age = 34
					add_trait = skilled_tactician
					add_trait = zealous
				}

				new_character = {
					grant_title = d_army_lucifer
				}
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				prestige >= 500
				wealth >= 500
			}
		}
	}

	expel_army_of_lucifer = {
		ai = no

		potential = {
			independent = yes
			higher_real_tier_than = DUKE
			has_dlc = "Sons of Abraham"
			is_title_active = d_army_lucifer
			religion = d_army_lucifer
			NOT = { has_character_modifier = expelled_d_army_lucifer }

			any_realm_province = {
				held_under_PREV = yes

				any_province_lord = {
					OR = {
						character = d_army_lucifer
						is_vassal_or_below_of = d_army_lucifer
					}
				}
			}
		}

		allow = {
			piety >= 1000

			custom_tooltip = {
				text = expelling_holy_order_decision_tooltip
				NOT = { has_character_flag = expel_army_of_lucifer_check }
			}
		}

		effect = {
			set_character_flag = expel_army_of_lucifer_check

			d_army_lucifer = {
				save_event_target_as = target_holy_order

				holder_scope = {
					save_event_target_as = target_grandmaster
				}
			}

			character_event = {
				id = SoA.2990
				tooltip = expelling_holy_order_decision_tooltip_b
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	donate_money_to_army_of_lucifer = {
		ai_check_interval = 80

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			is_title_active = d_army_lucifer
			religion = d_army_lucifer

			d_army_lucifer = {
				holder_scope = {
					is_within_diplo_range = ROOT
				}
			}

			NOR = {
				government = order_government
				has_character_modifier = expelled_d_army_lucifer
				is_liege_of = d_army_lucifer
				has_landed_title = d_army_lucifer
			}
		}

		allow = {
			wealth >= 300

			custom_tooltip = {
				text = holy_order_donate_cooldown_tt
				NOT = { has_character_modifier = donate_to_holy_order_cooldown }
			}
		}

		effect = {
			wealth = -300
			piety = 150

			if = {
				limit = {
					NOT = { has_character_modifier = donated_to_holy_order }
				}

				add_character_modifier = {
					name = donated_to_holy_order
					duration = -1
				}
			}

			add_character_modifier = {
				name = donate_to_holy_order_cooldown
				years = 1
				hidden = yes
			}

			d_army_lucifer = {
				show_scope_change = no

				save_event_target_as = target_holy_order

				holder_scope = {
					show_scope_change = no

					save_event_target_as = target_grandmaster
					wealth = 300
				}
			}

			letter_event = { id = SoA.1141 }
		}

		ai_will_do = {
			factor = 0.12

			trigger = {
				piety < 1000
				wealth >= 1000

				NOR = {
					trait = cynical
					trait = greedy
					has_character_modifier = ai_austerity
				}
			}

			mult_modifier = {
				factor = 10
				trait = zealous
			}

			mult_modifier = {
				factor = 10
				piety < 100
			}
		}
	}

	organize_bogomilist_church = {
		is_high_prio = yes
		ai_check_interval = 12

		only_independent = yes

		potential = {
			religion = bogomilist

			NOR = {
				is_title_active = d_bogomilist
				has_global_flag = bogomilist_organized
				has_law = bogomilist_church_2
				has_law = bogomilist_church_3
				has_law = bogomilist_church_4
			}
		}

		allow = {
			independent = yes
			higher_real_tier_than = DUKE
			num_of_holy_sites >= 1
			has_law = bogomilist_church_1
			trait = perfecti
			piety >= 500
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_global_flag = bogomilist_organized
			piety = -500
			religion_authority = { name = bogomilism_organized }

			primary_title = {
				show_scope_change = no

				set_title_flag = non_manual_law_change # No prestige loss
				add_law = bogomilist_church_2
				clr_title_flag = non_manual_law_change # No prestige loss
			}

			hidden_effect = { # TODO: Add custom tooltip?
				any_demesne_province = {
					limit = {
						religion = bogomilist
						NOT = { has_province_modifier = religious_unrest }
						owned_by = PREV
					}

					random_list = {
						50 = {
							add_province_modifier = {
								name = religious_unrest
								years = 2
							}
						}

						25 = {
							trigger = {
								ROOT = { is_heretic = no }
							}

							become_heretic = yes

							add_province_modifier = {
								name = religious_unrest
								years = 2
							}
						}

						25 = {
							trigger = {
								ROOT = { is_heretic = yes }
							}

							abandon_heresy = yes

							add_province_modifier = {
								name = religious_unrest
								years = 2
							}
						}

						125 = {}
					}
				}
			}

			narrative_event = { id = Plus.2130 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	begin_formalize_bogomilist_church = {
		is_high_prio = yes
		ai_check_interval = 12

		only_independent = yes

		potential = {
			religion = bogomilist
			has_global_flag = bogomilist_organized

			NOR = {
				has_law = bogomilist_church_0
				has_law = bogomilist_church_1
				has_law = bogomilist_church_3
				has_law = bogomilist_church_4
				has_character_modifier = formalizing_bogomilist
				is_title_active = d_bogomilist
			}
		}

		allow = {
			independent = yes
			num_of_holy_sites >= 1
			has_law = bogomilist_church_2
			trait = perfecti
			higher_real_tier_than = DUKE

			custom_tooltip = {
				text = BOGO_ORGANIZED_TEN_YEARS

				had_global_flag = {
					flag = bogomilist_organized
					years >= 10
				}
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			add_character_modifier = {
				name = formalizing_bogomilist
				duration = -1
				inherit = yes
			}

			character_event = { id = Plus.2112 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	formalize_bogomilist_church = {
		is_high_prio = yes
		ai_check_interval = 12

		only_independent = yes

		potential = {
			religion = bogomilist
			has_global_flag = bogomilist_organized

			check_variable = {
				which = global_bogomil_formalization
				value > 0
			}

			NOR = {
				has_law = bogomilist_church_0
				has_law = bogomilist_church_1
				has_law = bogomilist_church_3
				has_law = bogomilist_church_4
				is_title_active = d_bogomilist
				has_global_flag = bogomilist_formalized
			}
		}

		allow = {
			independent = yes
			higher_real_tier_than = DUKE
			num_of_holy_sites >= 1
			has_law = bogomilist_church_2
			piety >= 500
			trait = perfecti
			has_character_modifier = formalizing_bogomilist
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no

			custom_tooltip = {
				text = BOGO_READY_TO_FORMALIZE

				check_variable = {
					which = global_bogomil_formalization
					value >= 4
				}
			}
		}

		effect = {
			set_global_flag = bogomilist_formalized
			piety = -500
			religion_authority = { name = bogomilism_formalized }

			primary_title = {
				show_scope_change = no

				set_title_flag = non_manual_law_change # No prestige loss
				add_law = bogomilist_church_3
				clr_title_flag = non_manual_law_change # No prestige loss
			}

			hidden_effect = {
				remove_character_modifier = formalizing_bogomilist
			}

			narrative_event = { id = Plus.2132 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	force_formalize_bogomilist_church = {
		is_high_prio = no
		ai_check_interval = 12

		only_independent = yes

		potential = {
			is_decision_potential = formalize_bogomilist_church
		}

		allow = {
			independent = yes
			num_of_holy_sites >= 1
			has_law = bogomilist_church_2
			piety >= 500
			higher_real_tier_than = DUKE
			trait = perfecti

			NOT = {
				custom_tooltip = {
					text = BOGO_READY_TO_FORMALIZE # TODO: Add negative tooltip text for better tooltip generation

					check_variable = {
						which = global_bogomil_formalization
						value >= 4
					}
				}
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			set_global_flag = bogomilist_formalized
			piety = -500
			religion_authority = { name = bogomilism_formalized }

			primary_title = {
				show_scope_change = no

				set_title_flag = non_manual_law_change # No prestige loss
				add_law = bogomilist_church_3
				clr_title_flag = non_manual_law_change # No prestige loss
			}

			hidden_effect = { # TODO: Add custom tooltip?
				any_demesne_province = {
					limit = {
						religion = bogomilist
						NOT = { has_province_modifier = religious_unrest }
						owned_by = PREV
					}

					random_list = {
						50 = {
							add_province_modifier = {
								name = religious_unrest
								years = 3
							}
						}

						50 = {
							trigger = {
								ROOT = { is_heretic = no }
							}

							become_heretic = yes

							add_province_modifier = {
								name = religious_unrest
								years = 3
							}
						}

						50 = {
							trigger = {
								ROOT = { is_heretic = yes }
							}

							abandon_heresy = yes

							add_province_modifier = {
								name = religious_unrest
								years = 3
							}
						}

						100 = {}
					}
				}
			}

			narrative_event = { id = Plus.2131 }
		}

		ai_will_do = {
			factor = 0
		}
	}

	create_bogomilist_patriarch = {
		is_high_prio = yes
		ai_check_interval = 12

		only_independent = yes

		potential = {
			religion = bogomilist
			has_global_flag = bogomilist_formalized
			NOT = { is_title_active = d_bogomilist }
		}

		allow = {
			independent = yes
			higher_real_tier_than = DUKE
			has_law = bogomilist_church_3
			wealth >= 500
			piety >= 1000
			prestige >= 1000
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			piety = -1000
			prestige = -1000
			wealth = -500

			if = {
				limit = { is_heretic = yes }

				set_parent_religion = {
					religion = bogomilist
					parent = 0
				}
			}

			primary_title = {
				show_scope_change = no

				set_title_flag = non_manual_law_change # No prestige loss
				add_law = bogomilist_church_4
				clr_title_flag = non_manual_law_change # No prestige loss
			}

			activate_title = {
				title = d_bogomilist
				status = yes
			}

			character_event = { id = Plus.2133 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	church_of_st_bogomil = {
		is_high_prio = yes
		ai_check_interval = 12

		only_playable = yes

		potential = {
			religion = bogomilist

			OR = {
				has_law = bogomilist_church_2
				has_law = bogomilist_church_3
				has_law = bogomilist_church_4
			}

			c_tyrnovo = {
				location = {
					NOR = {
						has_province_flag = church_of_st_bogomil_built
						has_province_modifier = building_st_bogomil
					}
				}
			}
		}

		allow = {
			completely_controls = c_tyrnovo
			wealth >= 1000

			c_tyrnovo = {
				location = {
					show_scope_change = no

					religion = ROOT
					num_of_empty_holdings >= 1
				}
			}

			OR = {
				has_law = bogomilist_church_3
				has_law = bogomilist_church_4
			}

			war = no
			is_not_in_regency_trigger = yes
		}

		effect = {
			wealth = -1000

			c_tyrnovo = {
				location = {
					show_scope_change = no

					province_event = { id = Plus.2135 } #begin construction

					province_event = {
						id = Plus.2136 # Finish construction
						years = 4
						random = 50
					}

					add_province_modifier = {
						name = building_st_bogomil
						duration = -1
					}
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}

	bogomilist_quit_theocracy = {
		is_high_prio = yes
		ai_check_interval = 12
		icon_religion = bogomilist

		only_rulers = yes

		potential = {
			religion = bogomilist
			is_theocracy = yes
			capital_holding = { NOT = { holding_type = temple } }
			NOT = { has_global_flag = bogomilist_formalized }
			ai = yes
		}

		allow = {
			war = no
			is_not_in_regency_trigger = yes
		}

		effect = {
			if = {
				limit = {
					trait = perfecti
					independent = no
				}

				piety = 25
				abdicate_to = liege
			}
		}

		ai_will_do = {
			factor = 1

			trigger = {
				NOR = {
					trait = cynical
					trait = bad_priest_christian
				}
			}
		}
	}

	bogomilist_join_theocracy = {
		is_high_prio = yes
		ai_check_interval = 12
		icon_religion = bogomilist

		only_rulers = yes

		potential = {
			religion = bogomilist
			is_feudal = yes
			is_government_potential = theocracy_government
			has_global_flag = bogomilist_formalized
			ai = yes
		}

		allow = {
			war = no
			is_not_in_regency_trigger = yes
		}

		effect = {
			set_government_type = theocracy_government
		}

		ai_will_do = {
			factor = 1

			trigger = {
				independent = no

				OR = {
					trait = perfecti
					trait = zealous
					trait = humble
					trait = content
					trait = detached_priest
					trait = dutiful_cleric
					trait = scholarly_theologian
					trait = mastermind_theologian
				}

				NOR = {
					trait = cynical
					trait = ambitious
				}
			}

			mult_modifier = {
				factor = 10

				OR = {
					trait = perfecti
					trait = zealous
				}
			}


			mult_modifier = {
				factor = 0.1
				higher_real_tier_than = COUNT
			}

			mult_modifier = {
				factor = 0.1
				higher_real_tier_than = DUKE
			}

			mult_modifier = {
				factor = 0.1

				real_tier = DUKE
				num_of_duke_titles >= 2
			}
		}
	}

	organise_knights_of_saint_mark = {
		is_high_prio = yes
		ai_check_interval = 12

		only_playable = yes

		potential = {
			higher_real_tier_than = COUNT
			is_miaphysite_branch = yes
			is_heretic = no
			NOT = { is_title_active = d_knights_saint_mark }
		}

		allow = {
			completely_controls = d_alexandria
			higher_real_tier_than = DUKE
			piety >= 500
			wealth >= 250
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			piety = -500
			wealth = -250

			activate_title = {
				title = d_knights_saint_mark
				status = yes
			}

			narrative_event = { id = Plus.2064 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	expel_knights_of_saint_mark = {
		ai = no

		potential = {
			independent = yes
			higher_real_tier_than = DUKE
			has_dlc = "Sons of Abraham"
			is_title_active = d_knights_saint_mark
			religion = d_knights_saint_mark
			NOT = { has_character_modifier = expelled_d_knights_saint_mark }

			any_realm_province = {
				held_under_PREV = yes

				any_province_lord = {
					OR = {
						character = d_knights_saint_mark
						is_vassal_or_below_of = d_knights_saint_mark
					}
				}
			}
		}

		allow = {
			piety >= 1000

			custom_tooltip = {
				text = expelling_holy_order_decision_tooltip
				NOT = { has_character_flag = expel_knights_of_saint_mark_check }
			}
		}

		effect = {
			set_character_flag = expel_knights_of_saint_mark_check

			d_knights_saint_mark = {
				save_event_target_as = target_holy_order

				holder_scope = {
					save_event_target_as = target_grandmaster
				}
			}

			character_event = {
				id = SoA.2990
				tooltip = expelling_holy_order_decision_tooltip_b
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	donate_money_to_knights_of_saint_mark = {
		ai_check_interval = 80

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			is_title_active = d_knights_saint_mark
			religion = d_knights_saint_mark

			d_knights_saint_mark = {
				holder_scope = {
					is_within_diplo_range = ROOT
				}
			}

			NOR = {
				government = order_government
				has_character_modifier = expelled_d_knights_saint_mark
				is_liege_of = d_knights_saint_mark
				has_landed_title = d_knights_saint_mark
			}
		}

		allow = {
			wealth >= 300

			custom_tooltip = {
				text = holy_order_donate_cooldown_tt
				NOT = { has_character_modifier = donate_to_holy_order_cooldown }
			}
		}

		effect = {
			wealth = -300
			piety = 150

			if = {
				limit = {
					NOT = { has_character_modifier = donated_to_holy_order }
				}

				add_character_modifier = {
					name = donated_to_holy_order
					duration = -1
				}
			}

			add_character_modifier = {
				name = donate_to_holy_order_cooldown
				years = 1
				hidden = yes
			}

			d_knights_saint_mark = {
				show_scope_change = no

				save_event_target_as = target_holy_order

				holder_scope = {
					show_scope_change = no

					save_event_target_as = target_grandmaster
					wealth = 300
				}
			}

			letter_event = { id = SoA.1141 }
		}

		ai_will_do = {
			factor = 0.12

			trigger = {
				piety < 1000
				wealth >= 1000

				NOR = {
					trait = cynical
					trait = greedy
					has_character_modifier = ai_austerity
				}
			}

			mult_modifier = {
				factor = 10
				trait = zealous
			}

			mult_modifier = {
				factor = 10
				piety < 100
			}
		}
	}

	expel_order_of_saint_blaise = {
		ai = no

		potential = {
			independent = yes
			higher_real_tier_than = DUKE
			has_dlc = "Sons of Abraham"
			is_title_active = d_saint_blaise
			religion = d_saint_blaise
			NOT = { has_character_modifier = expelled_d_saint_blaise }

			any_realm_province = {
				held_under_PREV = yes

				any_province_lord = {
					OR = {
						character = d_saint_blaise
						is_vassal_or_below_of = d_saint_blaise
					}
				}
			}
		}

		allow = {
			piety >= 1000

			custom_tooltip = {
				text = expelling_holy_order_decision_tooltip
				NOT = { has_character_flag = expel_order_of_saint_blaise_check }
			}
		}

		effect = {
			set_character_flag = expel_order_of_saint_blaise_check

			d_saint_blaise = {
				save_event_target_as = target_holy_order

				holder_scope = {
					save_event_target_as = target_grandmaster
				}
			}

			character_event = {
				id = SoA.2990
				tooltip = expelling_holy_order_decision_tooltip_b
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	donate_money_to_order_of_saint_blaise = {
		ai_check_interval = 80

		only_playable = yes

		potential = {
			has_dlc = "Sons of Abraham"
			is_title_active = d_saint_blaise
			religion = d_saint_blaise

			d_saint_blaise = {
				holder_scope = {
					is_within_diplo_range = ROOT
				}
			}

			NOR = {
				government = order_government
				has_character_modifier = expelled_d_saint_blaise
				is_liege_of = d_saint_blaise
				has_landed_title = d_saint_blaise
			}
		}

		allow = {
			wealth >= 300

			custom_tooltip = {
				text = holy_order_donate_cooldown_tt
				NOT = { has_character_modifier = donate_to_holy_order_cooldown }
			}
		}

		effect = {
			wealth = -300
			piety = 150

			if = {
				limit = {
					NOT = { has_character_modifier = donated_to_holy_order }
				}

				add_character_modifier = {
					name = donated_to_holy_order
					duration = -1
				}
			}

			add_character_modifier = {
				name = donate_to_holy_order_cooldown
				years = 1
				hidden = yes
			}

			d_saint_blaise = {
				show_scope_change = no

				save_event_target_as = target_holy_order

				holder_scope = {
					show_scope_change = no

					save_event_target_as = target_grandmaster
					wealth = 300
				}
			}

			letter_event = { id = SoA.1141 }
		}

		ai_will_do = {
			factor = 0.12

			trigger = {
				piety < 1000
				wealth >= 1000

				NOR = {
					trait = cynical
					trait = greedy
					has_character_modifier = ai_austerity
				}
			}

			mult_modifier = {
				factor = 10
				trait = zealous
			}

			mult_modifier = {
				factor = 10
				piety < 100
			}
		}
	}

	miaphysite_desert_father = {
		ai_check_interval = 12

		potential = {
			is_miaphysite_branch = yes
			is_adult = yes

			NOR = {
				rightful_religious_head = ROOT
				trait = desert_father
				trait = desert_mother
				trait = monk
				trait = nun
			}

			is_nomadic = no
		}

		allow = {
			war = no
			is_ill = no
			prisoner = no
			is_pregnant = no

			NOR = {
				trait = ambitious
				trait = proud
				trait = cynical
				trait = incapable
				trait = infirm
				any_liege = { war = yes }
			}
		}

		effect = {
			any_spouse = {
				show_scope_change = no

				remove_spouse = ROOT
			}

			set_character_flag = is_desert_mother_father
			add_trait = chaste
			change_learning = 1

			if = {
				limit = { is_female = no }

				add_trait = desert_father
				set_character_flag = title_desert_father
				set_special_character_title = DESERT_FATHER_TITLE
			}
			else = {
				add_trait = desert_mother
				set_character_flag = title_desert_mother
				set_special_character_title = DESERT_MOTHER_TITLE
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				OR = {
					has_education_learning_trigger = yes

					AND = {
						trait = zealous
						trait = humble
					}
				}

				is_theocracy = no
				is_councillor = no

				NOR = {
					trait = stressed
					trait = depressed
					trait = hedonist
					trait = sympathy_islam
					trait = sympathy_pagans
					trait = sympathy_zoroastrianism
					trait = sympathy_judaism
					trait = sympathy_indian

					any_liege = {
						OR = {
							holy_order = yes
							mercenary = yes
						}
					}

					any_spouse = {
						trait = pregnant
						father_of_unborn = { character = ROOT }
					}

					liege = {
						ai = no

						OR = {
							dynasty = ROOT
							is_close_relative = ROOT
						}
					}

					has_character_modifier = voice_of_satan
					has_character_modifier = planning_claimant_adventure
					has_character_flag = claimant_adventurer
				}

				trigger_if = {
					limit = { any_heir_title = { always = yes } }

					calc_true_if = {
						amount >= 2

						trait = zealous
						trait = humble
						trait = content
						trait = possessed
						has_character_modifier = voice_of_jesus
					}

					OR = {
						trait = envious
						trait = greedy
					}
				}

				trigger_if = {
					limit = { any_pretender_title = { always = yes } }

					OR = {
						trait = zealous
						trait = humble
						trait = content
						trait = possessed
					}

					NOR = {
						trait = envious
						trait = greedy
					}
				}

				trigger_if = {
					limit = { is_married = yes }

					OR = {
						trait = zealous
						trait = lunatic
						trait = possessed
						has_called_crusade = yes

						NOT = {
							any_spouse = {
								reverse_opinion = {
									who = ROOT
									value >= 0
								}
							}
						}
					}
				}
			}

			mult_modifier = {
				factor = 10
				has_character_modifier = voice_of_jesus
			}

			mult_modifier = {
				factor = 3
				trait = zealous
			}

			mult_modifier = {
				factor = 0.5
				trait = detached_priest
			}

			mult_modifier = {
				factor = 2
				trait = scholarly_theologian
			}

			mult_modifier = {
				factor = 3
				trait = mastermind_theologian
			}

			mult_modifier = {
				factor = 0.5
				trait = proud
			}

			mult_modifier = {
				factor = 0.5
				trait = slothful
			}

			mult_modifier = {
				factor = 1.5
				trait = humble
			}

			mult_modifier = {
				factor = 1.5
				trait = content
			}

			mult_modifier = {
				factor = 0.25

				OR = {
					father_even_if_dead = { higher_real_tier_than = COUNT }
					mother_even_if_dead = { higher_real_tier_than = COUNT }
				}
			}

			mult_modifier = {
				factor = 0.5
				has_education_learning_trigger = no
			}
		}
	}

	miaphysite_embrace_alexandrine = {
		ai_check_interval = 60

		only_rulers = yes

		potential = {
			learning >= 8

			OR = {
				is_miaphysite_branch = yes
				is_apostolic_branch = yes
				religion = nestorian
				religion = monothelite
			}

			NOR = {
				trait = alexandrine
				trait = antiochene
			}
		}

		allow = {
			piety >= 50
			NOT = { trait = zealous }
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			if = {
				limit = {
					num_of_count_titles >= 1
					demesne_size >= 1
				}

				economy_techpoints = 25
				military_techpoints = 25
			}

			custom_tooltip = {
				text = ALEXANDRINE_TOOLTIP
				add_trait = alexandrine
			}
		}

		ai_will_do = {
			factor = 0.3

			mult_modifier = {
				factor = 10
				trait = cynical
			}

			mult_modifier = {
				factor = 2
				learning >= 10
			}

			mult_modifier = {
				factor = 2
				learning >= 12
			}

			mult_modifier = {
				factor = 2
				learning >= 14
			}

			mult_modifier = {
				factor = 2
				learning >= 16
			}

			mult_modifier = {
				factor = 2
				learning >= 18
			}

			mult_modifier = {
				factor = 1.5
				liege = { trait = alexandrine }
			}
		}
	}

	miaphysite_embrace_antiochene = {
		ai_check_interval = 60

		only_rulers = yes

		potential = {
			learning >= 8

			OR = {
				is_miaphysite_branch = yes
				is_apostolic_branch = yes
				religion = nestorian
				religion = monothelite
			}

			NOR = {
				trait = alexandrine
				trait = antiochene
			}
		}

		allow = {
			piety >= 50
			NOT = { trait = cynical }
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			piety = 100

			if = {
				limit = {
					num_of_count_titles >= 1
					demesne_size >= 1
				}

				culture_techpoints = 25
			}

			custom_tooltip = {
				text = ANTIOCHIAN_TOOLTIP
				add_trait = antiochene
			}
		}

		ai_will_do = {
			factor = 0.3

			mult_modifier = {
				factor = 10
				trait = zealous
			}

			mult_modifier = {
				factor = 2
				learning >= 10
			}

			mult_modifier = {
				factor = 2
				learning >= 12
			}

			mult_modifier = {
				factor = 2
				learning >= 14
			}

			mult_modifier = {
				factor = 2
				learning >= 16
			}

			mult_modifier = {
				factor = 2
				learning >= 18
			}

			mult_modifier = {
				factor = 1.5
				liege = { trait = antiochene }
			}
		}
	}
}