﻿#Effects relating to religions

# check_for_equal_doctrine_effect
# set_bhakti_effect
# ritual_cannibalism_personal_tenet_reward_effect - grants private rewards for eating VICTIM, scaled by rank and close relations

ritual_cannibalism_personal_tenet_reward_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_ritual_cannibalism_devour_reward
		}
		add_stress = {
			value = miniscule_stress_loss
			if = {
				limit = { $VICTIM$ = { highest_held_title_tier >= tier_duchy } }
				add = miniscule_stress_loss
			}
			if = {
				limit = { $VICTIM$ = { highest_held_title_tier >= tier_kingdom } }
				add = miniscule_stress_loss
			}
			if = {
				limit = { $VICTIM$ = { highest_held_title_tier >= tier_empire } }
				add = miniscule_stress_loss
			}
			if = {
				limit = {
					OR = {
						is_close_family_of = $VICTIM$
						is_consort_of = $VICTIM$
						has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = $VICTIM$ }
						has_any_best_good_relationship_with_character_trigger = { CHARACTER = $VICTIM$ }
					}
				}
				add = miniscule_stress_loss
			}
		}
		change_spiritual_fulfillment = {
			value = miniscule_spiritual_fulfillment_value
			if = {
				limit = { $VICTIM$ = { highest_held_title_tier >= tier_duchy } }
				add = miniscule_spiritual_fulfillment_value
			}
			if = {
				limit = { $VICTIM$ = { highest_held_title_tier >= tier_kingdom } }
				add = miniscule_spiritual_fulfillment_value
			}
			if = {
				limit = { $VICTIM$ = { highest_held_title_tier >= tier_empire } }
				add = miniscule_spiritual_fulfillment_value
			}
			if = {
				limit = {
					OR = {
						is_close_family_of = $VICTIM$
						is_consort_of = $VICTIM$
						has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = $VICTIM$ }
						has_any_best_good_relationship_with_character_trigger = { CHARACTER = $VICTIM$ }
					}
				}
				add = miniscule_spiritual_fulfillment_value
			}
		}
	}
}

# Used to set Equal Law when the Equal Faith Doctrine is gained
check_for_equal_doctrine_effect = {
	if = {
		limit = {
			is_ruler = yes
			rite = {
				rite_has_doctrine = doctrine_gender_equal
			}
			scope:old_faith.main_rite = {
				NOT = { rite_has_doctrine = doctrine_gender_equal }
			}
			NOT = {
				has_realm_law = equal_law
			}
		}
		add_realm_law_skip_effects = equal_law
	}
}
# $TITLE$ is the barony, and this expects scope:holy_order
lease_barony_to_military_holy_order_effect = {
	$TITLE$ = {
		if = { #To make sure the effect works properly we have to have the title when we lease it out
			limit = { holder != prev }
			prev = { get_title = prev }
		}
		lease_out_to_holy_order = scope:holy_order
	}
	if = {
		limit = {
			has_personal_tenet_flag = leasing_to_military_order_give_spiritual_fulfillment_personal
			faith = scope:holy_order.faith
		}
		send_interface_toast = {
			type = event_generic_good
			title = leasing_to_military_order_give_spiritual_fulfillment_personal_toast
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
		}
	}
}

# Used to set a personal god.
set_bhakti_effect = {
	# Remove any previous personal god relation.
	## HINDUISM
	if = {
		limit = {
			has_character_modifier = bhakti_hinduism_ganga
		}
		remove_character_modifier = bhakti_hinduism_ganga
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_hinduism_saraswati
		}
		remove_character_modifier = bhakti_hinduism_saraswati
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_hinduism_kali
		}
		remove_character_modifier = bhakti_hinduism_kali
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_hinduism_kubera
		}
		remove_character_modifier = bhakti_hinduism_kubera
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_vaishnavism_lakishmi
		}
		remove_character_modifier = bhakti_vaishnavism_lakishmi
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_vaishnavism_jagganath
		}
		remove_character_modifier = bhakti_vaishnavism_jagganath
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_vaishnavism_hayagriva
		}
		remove_character_modifier = bhakti_vaishnavism_hayagriva
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_shaivism_parvati
		}
		remove_character_modifier = bhakti_shaivism_parvati
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_shaivism_virabhadra
		}
		remove_character_modifier = bhakti_shaivism_virabhadra
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_shaivism_munishwarar
		}
		remove_character_modifier = bhakti_shaivism_munishwarar
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_shaivism_dakshinamoorthy
		}
		remove_character_modifier = bhakti_shaivism_dakshinamoorthy
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_smartism_ganesha
		}
		remove_character_modifier = bhakti_smartism_ganesha
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_smartism_vishnu
		}
		remove_character_modifier = bhakti_smartism_vishnu
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_smartism_shiva
		}
		remove_character_modifier = bhakti_smartism_shiva
		add_piety = replace_bhakti_piety_loss
	}
	# GERMANIC
	else_if = {
		limit = {
			has_character_modifier = bhakti_germanic_generic_odin
		}
		remove_character_modifier = bhakti_germanic_generic_odin
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_germanic_not_danish_ullr
		}
		remove_character_modifier = bhakti_germanic_not_danish_ullr
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_germanic_danish_tyr
		}
		remove_character_modifier = bhakti_germanic_danish_tyr
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_germanic_generic_thor
		}
		remove_character_modifier = bhakti_germanic_generic_thor
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_germanic_generic_freyr
		}
		remove_character_modifier = bhakti_germanic_generic_freyr
		add_piety = replace_bhakti_piety_loss
	}
	# BUDDHISM
	else_if = {
		limit = {
			has_character_modifier = bhakti_buddhism_amitabha
		}
		remove_character_modifier = bhakti_buddhism_amitabha
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_buddhism_avalokiteshvara
		}
		remove_character_modifier = bhakti_buddhism_avalokiteshvara
		add_piety = replace_bhakti_piety_loss
	}
	else_if = {
		limit = {
			has_character_modifier = bhakti_buddhism_bhaisajyaguru
		}
		remove_character_modifier = bhakti_buddhism_bhaisajyaguru
		add_piety = replace_bhakti_piety_loss
	}
	else = {
		# First personal devotion is spiritually fulfilling; swapping gods later is not
		if = {
			limit = { is_playable_character = yes }	# Only playable characters have spiritual fulfillment
			change_spiritual_fulfillment = minor_spiritual_fulfillment_value
		}
		add_piety = medium_piety_loss
	}

	add_character_modifier = $BHAKTI$
}

force_create_head_of_faith_title_effect = { # Has no cost, should be done manually on a per-case basis
	custom_description = {
		text = create_temporal_head_of_faith
		subject = root
		object = scope:faith
		# No scope:rite: creation events only check mainline rite from rite-scoped flows
		# Temporal HoF
		if = {
			limit = { faith = { has_doctrine_parameter = temporal_head_of_faith } }
			if = {
				limit = {
					faith = { NOT = { exists = religious_head_title } }
				}
				trigger_event = faith_creation.0001 # Creates new dynamic title
			}
			else = { trigger_event = faith_creation.0011 } # Re-uses existing title
		}

		# Spiritual HoF
		else_if = {
			limit = { faith = { has_doctrine_parameter = spiritual_head_of_faith } }
			if = {
				limit = {
					faith = { NOT = { exists = religious_head_title } }
				}
				trigger_event = faith_creation.0002 # Creates new dynamic title
			}
			else = { trigger_event = faith_creation.0012 } # Re-uses existing title
		}
	}

	# Send an informative message to all players about the new HoF
	hidden_effect = {
		save_scope_as = reformer
		faith = {
			save_scope_as = faith
			change_fervor = {
				value = major_fervor_gain
				desc = fervor_gain_recreated_hof
			}
		}
		every_player = {
			if = {
				limit = { faith = scope:faith }
				trigger_event = {
					id = faith_creation.1010
					days = 1
				}
			}
			else = {
				trigger_event = {
					id = faith_creation.1011
					days = 1
				}
			}
		}
	}
}

# Faith scope. A faith created by a rite diverging too far starts at 0 fervor - kindle it to the baseline.
diverged_faith_starting_fervor_effect = {
	change_fervor = {
		value = diverged_faith_starting_fervor_value
		desc = fervor_gain_diverged_faith
	}
}

# Dynamic HoF title creation
set_up_dynamic_temporal_hof_title_effect = {
	faith = { save_scope_as = my_faith }
	create_dynamic_title = {
		tier = duchy
		name = REL_HEAD_TITLE_NAME
		adj = REL_HEAD_TITLE_ADJ
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:new_title = {
		set_destroy_if_invalid_heir = yes
		set_no_automatic_claims = yes
		set_definitive_form = yes
		set_always_follows_primary_heir = yes
		change_title_holder = {
			holder = $NEW_HOLDER$
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	faith = { set_religious_head_title = scope:new_title }
	if = {
		limit = {
			NOT = { has_realm_law = same_faith_succession_law }
		}
		add_realm_law_skip_effects = same_faith_succession_law
	}
	scope:new_title = {
		generate_coa = religious_title
		add_title_law = temporal_head_of_faith_succession_law
	}
}

set_up_dynamic_spiritual_hof_title_effect = {
	# Sort scopes & starting processes.
	faith = { save_scope_as = my_faith }
	$CREATOR$ = { save_scope_as = hof_creator }
	create_dynamic_title = {
		tier = duchy
		name = REL_HEAD_TITLE_NAME
		adj = REL_HEAD_TITLE_ADJ
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	# Clergy take the title, while lay creators prefer clerical puppets over the realm priest
	if = {
		limit = {
			scope:hof_creator = { is_clergy = yes }
		}
		scope:hof_creator = { save_scope_as = new_religious_head }
	}
	else_if = {
		limit = {
			scope:hof_creator.puppet:theological_agent_puppet ?= {
				rite = scope:hof_creator.rite
			}
		}
		scope:hof_creator.puppet:theological_agent_puppet = {
			save_scope_as = new_religious_head
		}
	}
	else_if = {
		limit = {
			scope:hof_creator.puppet:external_theological_agent_puppet ?= {
				rite = scope:hof_creator.rite
			}
		}
		scope:hof_creator.puppet:external_theological_agent_puppet = {
			save_scope_as = new_religious_head
		}
	}
	else_if = {
		limit = {
			scope:hof_creator.cp:councillor_court_chaplain ?= {
				rite = scope:hof_creator.rite
			}
		}
		scope:hof_creator.cp:councillor_court_chaplain = {
			save_scope_as = new_religious_head
		}
	}
	else_if = {
		limit = {
			any_theocratic_vassal = { faith = $CREATOR$.faith }
		}
		ordered_theocratic_vassal = {
			limit = { faith = $CREATOR$.faith }
			order_by = head_of_faith_selection_weight
			save_scope_as = new_religious_head
		}
	}
	else = {
		create_character = {
			location = $CREATOR$.capital_province
			age = { 30 50 }
			gender_female_chance = root_faith_clergy_gender_female_chance
			trait = education_learning_4
			faith = $CREATOR$.faith
			rite = $CREATOR$.rite
			culture = $CREATOR$.culture
			learning = { 14 22 }
			save_scope_as = new_religious_head
		}
	}
	# Try to land the new title.
	scope:my_faith = {
		random_holy_site_barony = {
			# Prefer a capital that's under $CREATOR$.
			limit = { county.holder = $CREATOR$ }
			# Even if it's indirectly under $CREATOR$.
			alternative_limit = {
				county.holder = { target_is_liege_or_above = $CREATOR$ }
			}
			# Then go for any that's held by someone of the faith.
			alternative_limit = { county.holder.faith = $CREATOR$.faith }
			# Under *extreme* circumstances, pick any holy site with at least some faithful.
			alternative_limit = { county.faith = $CREATOR$.faith }
			# Ok, just pick any of them. We're out of options and we need _somewhere_.
			alternative_limit = { always = yes }
			save_scope_as = holy_site
		}
	}
	if = {
		limit = { exists = scope:holy_site }
		scope:new_title = { set_capital_county = scope:holy_site.county }
	}
	# Configure final effects.
	hidden_effect = {
		scope:new_title = {
			set_landless_title = yes
			set_destroy_if_invalid_heir = yes
			set_no_automatic_claims = yes
			set_definitive_form = yes
			set_always_follows_primary_heir = yes
			change_title_holder = {
				holder = scope:new_religious_head
				change = scope:change
			}
		}
	}
	resolve_title_and_vassal_change = scope:change
	faith = { set_religious_head_title = scope:new_title }
	scope:new_title = { generate_coa = religious_title }
	# The establishing rite becomes the mainline rite of the faith.
	if = {
		limit = {
			scope:hof_creator = {
				exists = rite
				NOT = { rite = scope:my_faith.main_rite }
			}
		}
		scope:hof_creator.rite = { set_main_rite = yes }
	}
	# We enclose the law safeguard in an if statement to prevent errors when this is only _about_ to be fired.
	hidden_effect = {
		if = {
			limit = { exists = scope:new_religious_head }
			scope:new_religious_head = {
				if = {
					limit = {
						NOT = { has_realm_law = same_faith_theocratic_succession_law }
					}
					add_realm_law_skip_effects = same_faith_theocratic_succession_law
				}
			}
		}
	}
}

# Excommunication management.
add_excommunication_tooltip_only_effect = {
	show_as_tooltip = {
		custom_tooltip = {
			text = get_excommunicated_tt
			add_trait = excommunicated
		}
		add_character_modifier = { # Minor negative modifier that stops us from immediately getting our excommunication recinded (so you have a guaranteed 'penance period').
			modifier = recent_excommunication
			days = 365
		}
	}
}
add_excommunication_actual_effect = {
	custom_tooltip = {
		text = get_excommunicated_tt
		add_trait = excommunicated
	}
	add_character_modifier = { # Minor negative modifier that stops us from immediately getting our excommunication recinded (so you have a guaranteed 'penance period').
		modifier = recent_excommunication
		days = 365
	}
}

add_excommunication_actual_effect_5_years_version = {
	custom_tooltip = {
		text = get_excommunicated_tt
		add_trait = excommunicated
	}
	add_character_modifier = { # Minor negative modifier that stops us from immediately getting our excommunication recinded (so you have a guaranteed 'penance period').
		modifier = recent_excommunication
		years = 5
	}
}

create_holy_order_neutral_effect = {
	# Create a general Holy Order.
	create_holy_order = {
		leader = $LEADER$
		founder = $FOUNDER$
		random_holy_order_type = $ORDER_TYPE$
		capital = $CAPITAL$
		save_scope_as = $NEW_HO_SCOPE$
	}
}

# Get random faith
get_random_faith_effect = {
	if = {
		limit = { $FORBIDROOTFAITH$ = yes }
		random_religion_global = {
			limit = { any_faith = { this != root.faith } }
			random_faith = {
				limit = { this != root.faith }
				save_scope_as = random_faith
			}
		}
	}
	else = {
		random_religion_global = {
			random_faith = { save_scope_as = random_faith }
		}
	}
}

set_up_dynamic_spiritual_hof_title_existing_hof_effect = {

	$RELIGIOUS_HEAD$ = { save_scope_as = new_religious_head }

	# Sort scopes & starting processes.
	faith = { save_scope_as = my_faith }
	create_dynamic_title = {
		tier = duchy
		name = REL_HEAD_TITLE_NAME
		adj = REL_HEAD_TITLE_ADJ
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}

	# Nab a suitable character.
	# The decision already chose one.

	# Try to land the new title.
	scope:my_faith = {
		random_holy_site_barony = {
			limit = { county.holder = $CREATOR$ }
			alternative_limit = {
				county.holder = { target_is_liege_or_above = $CREATOR$ }
			}
			alternative_limit = {
				county.holder = {
					faith = $CREATOR$.faith
					is_ai = yes
				}
			}
			save_scope_as = holy_site
		}
	}
	if = {
		limit = { exists = scope:holy_site }
		scope:new_title = { set_capital_county = scope:holy_site.county }
	}
	# Configure final effects.
	hidden_effect = {
		scope:new_title = {
			set_landless_title = yes
			set_destroy_if_invalid_heir = yes
			set_no_automatic_claims = yes
			set_definitive_form = yes
			set_always_follows_primary_heir = yes
			change_title_holder = {
				holder = scope:new_religious_head
				change = scope:change
			}
		}
	}
	resolve_title_and_vassal_change = scope:change
	faith = { set_religious_head_title = scope:new_title }
	scope:new_title = { generate_coa = religious_title }
	# We enclose the law safeguard in an if statement to prevent errors when this is only _about_ to be fired.
	hidden_effect = {
		if = {
			limit = { exists = scope:new_religious_head }
			scope:new_religious_head = {
				if = {
					limit = {
						NOT = { has_realm_law = same_faith_theocratic_succession_law }
					}
					add_realm_law_skip_effects = same_faith_theocratic_succession_law
				}
			}
		}
	}
}

### SECRET FAITH ###

# Openly convert
secret_faith_openly_convert_character = {
	if = {
		limit = { exists = secret_faith }
		set_character_faith_with_conversion = secret_faith # Gets vassals too, if they like you enough
		remove_character_secret_faith = yes
	}
}

# Openly convert all secret faith counties in your realm
secret_faith_openly_convert_all_in_realm = {
	if = {
		limit = { any_sub_realm_county = { has_variable = false_convert } }
		save_scope_value_as = { # Keeping track how many counties convert
			name = num_false_converts_counties
			value = 0
		}

		every_sub_realm_county = {
			limit = { has_variable = false_convert }
			set_county_faith = var:false_convert # Convert them
			remove_county_modifier = false_convert_modifier # Remove modifier too
			remove_variable = false_convert # No longer mark them as converts
			add_county_modifier = { # Everyone likes practicing their faith in the open!
				modifier = relieved_cryptos_modifier
				years = 25
			}
			save_scope_value_as = { # Updating how many counties convert
				name = num_false_converts_counties
				value = {
					value = scope:num_false_converts_counties
					add = 1
				}
			}
		}
		if = { # Include counties in the message if there are any
			limit = { scope:num_false_converts_counties > 0 }
			root = { # There is an edgecase where root has a link to realm, but is not a character themselves!
				send_interface_message = {
					type = event_religious_good_text
					title = event_false_converts.t
					desc = event_false_converts_plus_counties.desc
				}
			}
		}
	}
}

# Openly convert all secret faith counties of your faith in your realm
secret_faith_openly_convert_all_in_realm_specific = {
	if = {
		limit = {
			any_sub_realm_county = {
				has_variable = false_convert
				var:false_convert = root.faith
				faith != root.faith # Discount them if they've already been switched by the conversion action.
			}
		}

		save_scope_value_as = { # Keeping track how many counties convert
			name = num_false_converts_counties
			value = 0
		}

		every_sub_realm_county = {
			limit = {
				has_variable = false_convert
				var:false_convert = root.faith
				faith != root.faith # Discount them if they've already been switched by the conversion action.
			}
			set_county_faith = var:false_convert # Change faith
			remove_county_modifier = false_convert_modifier
			remove_variable = false_convert # Cleanup
			add_county_modifier = { # Everyone likes practicing their faith in the open!
				modifier = relieved_cryptos_modifier
				years = 25
			}
			save_scope_value_as = { # Updating how many counties convert
				name = num_false_converts_counties
				value = {
					value = scope:num_false_converts_counties
					add = 1
				}
			}
		}

		if = { # Include counties in the message if there are any
			limit = { scope:num_false_converts_counties > 0 }
			root = {
				send_interface_message = {
					type = event_religious_good_text
					title = event_false_converts.t
					desc = event_false_converts_plus_counties.desc
				}
			}
		}
	}
}

add_heresiarch_trait_effect = { # Flag is need to prevent trait being removed when gaining it close to conversion
	add_character_flag = {
		flag = new_heresiarch
		days = 3
	}
	if = {
		limit = {
			NOT = { has_trait = heresiarch }
		}
		add_trait = heresiarch
	}
}

self_mutilation_minor_piety_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = self_mutilation_active
		}
		custom_tooltip = {
			text = self_mutilation_minor_piety_gain_tt
			add_piety = minor_piety_gain
		}
	}
}

self_mutilation_medium_piety_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = self_mutilation_active
		}
		custom_tooltip = {
			text = self_mutilation_medium_piety_gain_tt
			add_piety = medium_piety_gain
		}
	}
}

self_mutilation_major_piety_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = self_mutilation_active
		}
		custom_tooltip = {
			text = self_mutilation_major_piety_gain_tt
			add_piety = major_piety_gain
		}
	}
}

remove_sinful_trait_effect = {
	save_scope_as = trait_changer
    random_character_trait = {
        limit = {
            scope:trait_changer.rite = {
                trait_is_sin_rite = prev
            }
        }
        save_scope_as = sinful_trait
    }
    remove_trait = scope:sinful_trait
}

add_virtuous_trait_effect = {
	save_scope_as = trait_changer
	random_trait = {
        limit = {
        	save_temporary_scope_as = temp_trait
			scope:trait_changer.rite = {
                trait_is_virtue_rite = prev
            }
			NOR = {
				scope:trait_changer = {
					has_trait = scope:temp_trait
				}
				any_opposite_trait = {
					scope:trait_changer = {
						has_trait = scope:temp_trait
					}
				}
			}
			trigger_if = {
				limit = {
					this = trait:lustful
				}
				scope:trait_changer = {
					character_is_teen_or_older_trigger = yes
					NOR = {
						has_sexuality = none
						has_sexuality = asexual
					}
				}
			}
        }
        save_scope_as = virtuous_trait
    }
    add_trait_force_tooltip = scope:virtuous_trait
}

replace_sinful_trait_effect = {
	remove_sinful_trait_effect = yes
	add_virtuous_trait_effect = yes
}

# Called from Study Faith success outcomes.
discover_syncretism_tenet_effect = {
	if = {
		limit = { is_playable_character = yes }
		if = {
			limit = {
				scope:target.religion = religion:christianity_religion
				religion != religion:christianity_religion
				NOT = {
					knows_tenet = tenet_christian_syncretism
				}
			}
			add_known_tenet = tenet_christian_syncretism
		}
		else_if = {
			limit = {
				scope:target.religion = religion:islam_religion
				religion != religion:islam_religion
				NOT = {
					knows_tenet = tenet_islamic_syncretism
				}
			}
			add_known_tenet = tenet_islamic_syncretism
		}
		else_if = {
			limit = {
				scope:target.religion = religion:judaism_religion
				religion != religion:judaism_religion
				NOT = {
					knows_tenet = tenet_jewish_syncretism
				}
			}
			add_known_tenet = tenet_jewish_syncretism
		}
		else_if = {
			limit = {
				scope:target.faith = { has_doctrine = eastern_hostility_doctrine }
				faith = {
					OR = {
						has_doctrine = abrahamic_hostility_doctrine
						has_doctrine = pagan_hostility_doctrine
						has_doctrine = sinitic_hostility_doctrine
					}
				}
				NOT = { knows_tenet = tenet_eastern_syncretism }
			}
			add_known_tenet = tenet_eastern_syncretism
		}
		else_if = {
			limit = {
				scope:target.faith = { has_doctrine = sinitic_hostility_doctrine }
				faith = {
					OR = {
						has_doctrine = abrahamic_hostility_doctrine
						has_doctrine = pagan_hostility_doctrine
						has_doctrine = eastern_hostility_doctrine
					}
				}
				NOT = { knows_tenet = tenet_sinitic_syncretism }
			}
			add_known_tenet = tenet_sinitic_syncretism
		}
		else_if = {
			limit = {
				scope:target.faith = {
					OR = {
						has_doctrine = unreformed_faith_doctrine
						has_doctrine = west_african_unreformed_faith_doctrine
					}
				}
				faith = {
					NOT = {
						has_doctrine = pagan_hostility_doctrine
					}
				}
				NOT = {
					knows_tenet = tenet_unreformed_syncretism
				}
			}
			add_known_tenet = tenet_unreformed_syncretism
		}
	}
}

# This effect sets per-faith Permitted / Prohibited tenet statuses on game start for faiths whose main rite intends a spiritual or temporal head of faith.
# Faith-core tenets that are also in the mainline rite are left alone.
# Faith-only tenets that the mainline rite drops are explicitly Permitted so they are approved rather than merely Known.

setup_rite_tenet_permissions_effect = {
	# Tenet statuses are set in history/faiths/<faith>.txt, one entry per date
	# The history loader ignores DLC-locked and main-rite core tenets
}

############################
# Faith Rite Growth System #
############################

# Faith scope. Picks the largest rite to spawn within.
rite_growth_resolve_source_rite_effect = {
	ordered_faith_rite = {
		limit = {
			rite_counties >= 1
		}
		order_by = rite_counties
		save_scope_as = source_rite
	}
}

# This needs faith scope and scope:source_rite, then we save the pick as scope:founder
rite_growth_pick_founder_effect = {
	random_faith_ruler = {
		limit = { # Archbishops that actually know shit
			is_eligible_rite_growth_founder_trigger = yes
			rite_growth_founder_is_well_versed_trigger = yes
			OR = {
				rite_growth_holds_sizable_clerical_region_trigger = yes # Is an Archbishop with a sizable see
				exists = clerical_elector_title # Is a cardinal
			}
		}
		alternative_limit = { # Archbishops making shit up
			is_eligible_rite_growth_founder_trigger = yes
			OR = {
				rite_growth_holds_sizable_clerical_region_trigger = yes # Is an Archbishop with a sizable see
				exists = clerical_elector_title # Is a cardinal
			}
		}
		alternative_limit = { # Rulers that know shit
			is_eligible_rite_growth_founder_trigger = yes
			rite_growth_founder_is_well_versed_trigger = yes
		}
		alternative_limit = { # Rulers making shit up
			is_eligible_rite_growth_founder_trigger = yes
		}

		weight = {
			base = 1
			# Tier weight - higher tier reaches more land.
			modifier = {
				add = 5
				highest_held_title_tier >= tier_kingdom
			}
			# In faiths with archbishops, prefer the archbishops themselves.
			modifier = {
				add = 50
				root = { has_doctrine_parameter = has_clerical_regions }
				rite_growth_holds_sizable_clerical_region_trigger = yes
			}
			# An archbishop with a king inside an existing clerical region is the "perfect" candidate.
			modifier = {
				add = 100
				root = { has_doctrine_parameter = has_clerical_regions }
				save_temporary_scope_as = ruler_check
				any_held_title = {
					has_clerical_region = yes
					clerical_region_size >= rite_growth_min_clerical_region_size
					any_ruler_in_clerical_region = {
						faith = root
						is_landed = yes
						highest_held_title_tier = tier_kingdom
						NOR = {
							this = scope:ruler_check
							has_trait = heresiarch
							has_trait = herald
							recently_converted_faith_trigger = yes
							has_character_flag = recently_converted_by_rite_growth
							this = faith.religious_head
							rite.head_of_rite ?= this
						}
						NOT = { any_held_title = { has_clerical_region = yes } }
					}
				}
			}
			# No clerical regions: prefer founders amid a dense source-rite cluster
			modifier = {
				add = 25
				root = { NOT = { has_doctrine_parameter = has_clerical_regions } }
				any_sub_realm_county = {
					rite = scope:source_rite
					count >= 10
				}
			}
			modifier = {
				add = 15
				root = { NOT = { has_doctrine_parameter = has_clerical_regions } }
				any_held_title = {
					tier = tier_county
					rite = scope:source_rite
					count >= 3
				}
			}
			# Would come up with interesting new rites
			modifier = {
				add = 15
				OR = {
					has_trait = eccentric
					has_trait = possessed
					has_trait = lunatic
				}
			}
			# Theological fitness traits.
			modifier = {
				add = 10
				OR = {
					has_trait = theologian
					has_trait = lifestyle_scholar
					has_trait = lifestyle_mystic
					has_trait = devoted
				}
			}
			modifier = {
				factor = 2
				has_focus = learning_theology_focus
			}
			modifier = {
				factor = 2
				has_trait = zealous
			}
			# Strong negative for those very loyal to the religious head
			modifier = {
				factor = 0
				# Headless faiths have nobody to be loyal to
				exists = faith.religious_head
				opinion = {
					target = faith.religious_head
					value >= 75
				}
			}
			# Within the clerical region of your head of faith
			modifier = {
				factor = 0
				exists = faith.religious_head.capital_county.clerical_region_title
				capital_county.clerical_region_title ?= faith.religious_head.capital_county.clerical_region_title
			}
		}

		save_scope_as = founder
	}
}

rite_growth_create_rite_effect = {
	create_dynamic_rite = {
		save_scope_as = new_rite
		convert = yes
		target_divergence = $DIVERGENCE$
		include_unknown = $INCLUDE_UNKNOWN$
		override_tenets_first = $OVERRIDE_TENETS_FIRST$
	}
}

# Requires scope:new_rite and pass founder, as the new rite has no counties for a Head of Rite fallback
# We leave include_unknown open because the founder's knowledge cannot cover the core tenets
rite_growth_spawn_doctrine_divergence_effect = {
	if = {
		limit = { exists = scope:new_rite }
		scope:new_rite = {
			change_rite_divergence = {
				value = rite_growth_spawn_doctrine_divergence
				override_tenets_first = no
				include_unknown = yes
				character = rite_founder
			}
		}
	}
}

rite_growth_convert_court_to_new_rite_effect = {
	every_courtier_or_guest = {
		limit = {
			is_physically_able_ai = yes
			rite = scope:source_rite
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_rite = scope:new_rite
	}
	every_courtier_away = {
		limit = {
			is_physically_able_ai = yes
			rite = scope:source_rite
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_rite = scope:new_rite
	}
	every_pool_character = {
		province = capital_province
		limit = {
			is_physically_able_ai = yes
			rite = scope:source_rite
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_rite = scope:new_rite
	}
}

rite_growth_resolve_differences_effect = {
	clear_saved_scope = differing_tenet
	clear_saved_scope = differing_doctrine

	# Lead with prohibited so the named tenet matches the widget highlight
	scope:new_rite = {
		random_rite_tenet = {
			status = prohibited
			limit = { rite_growth_tenet_is_unique_to_new_rite_trigger = yes }
			save_scope_as = differing_tenet
		}
		if = {
			limit = { NOT = { exists = scope:differing_tenet } }
			random_rite_tenet = {
				status = known
				limit = { rite_growth_tenet_is_unique_to_new_rite_trigger = yes }
				save_scope_as = differing_tenet
			}
		}
		if = {
			limit = { NOT = { exists = scope:differing_tenet } }
			random_rite_tenet = {
				limit = { rite_growth_tenet_is_unique_to_new_rite_trigger = yes }
				save_scope_as = differing_tenet
			}
		}
	}

	if = {
		limit = { NOT = { exists = scope:differing_tenet } }
		scope:founder = {
			random_character_doctrine = {
				limit = { rite_growth_doctrine_is_unique_to_new_rite_trigger = yes }
				save_scope_as = differing_doctrine
			}
		}
	}
}

# Character scope (an AI ruler being swept into the new rite by rite_growth.0011).
rite_growth_convert_ruler_to_new_rite_effect = {
	if = {
		limit = { rite != scope:new_rite }
		set_character_rite_with_conversion = scope:new_rite
	}
	every_held_title = {
		limit = {
			tier = tier_county
			rite = scope:source_rite
		}
		set_county_rite = scope:new_rite
	}
	# The conversion above can cost the ruler their life
	if = {
		limit = { is_alive = yes }
		add_character_flag = {
			flag = recently_converted_by_rite_growth
			years = 10
		}
		rite_growth_convert_court_to_new_rite_effect = yes
	}
	if = {
		limit = { any_held_title = { has_clerical_region = yes } }
		every_held_title = {
			limit = { has_clerical_region = yes }
			save_temporary_scope_as = title_check
			every_county_in_clerical_region = {
				limit = {
					rite = scope:source_rite
					OR = {
						this = scope:title_check.title_capital_county
						any_neighboring_county = {
							this = scope:title_check.title_capital_county
						}
					}
				}
				set_county_rite = scope:new_rite
				holder = {
					if = {
						limit = { is_eligible_rite_growth_target_trigger = yes }
						rite_growth_convert_ruler_to_new_rite_effect = yes
					}
				}
			}
		}
	}
}

# $HOLY_SITE$ is the holy site to elevate, and this is faith scope
elevate_holy_site_effect = {
	if = {
		limit = {
			can_elevate_holy_site = $HOLY_SITE$
		}
		elevate_holy_site = $HOLY_SITE$
	}
}

# Demotion takes $HOLY_SITE$ as the holy site, and this runs on the faith
# An eminent site becomes regular, and a regular site is removed from the faith
demote_holy_site_effect = {
	if = {
		limit = {
			can_demote_holy_site = $HOLY_SITE$
		}
		demote_holy_site = $HOLY_SITE$
	}
}

# After the cost is already waived, a decision effect runs this on the faith
consume_free_holy_site_action_effect = {
	if = {
		limit = { faith_has_free_holy_site_action_trigger = yes }
		change_variable = {
			name = free_holy_site_actions
			subtract = 1
		}
	}
}

# Faith scope. BARONY = the holy site's barony title.
# Records the de jure kingdom so the faith cannot place a second created or adopted holy site in it.
record_dynamic_holy_site_kingdom_effect = {
	if = {
		limit = {
			exists = $BARONY$.kingdom
			# Idempotent: the same kingdom can be recorded from more than one path.
			faith_holy_site_kingdom_unused_trigger = { BARONY = $BARONY$ }
		}
		add_to_variable_list = {
			name = dynamic_holy_site_kingdoms
			target = $BARONY$.kingdom
		}
	}
}

# Faith scope. BARONY = the barony of the holy site that was just removed.
# Frees the kingdom again once the faith has no holy site left anywhere in it.
release_dynamic_holy_site_kingdom_effect = {
	if = {
		limit = {
			has_variable_list = dynamic_holy_site_kingdoms
			exists = $BARONY$.kingdom
		}
		$BARONY$.kingdom = {
			save_temporary_scope_as = released_holy_site_kingdom
		}
		if = {
			limit = {
				is_target_in_variable_list = {
					name = dynamic_holy_site_kingdoms
					target = scope:released_holy_site_kingdom
				}
				NOT = {
					any_holy_site = {
						holy_site_barony.kingdom ?= scope:released_holy_site_kingdom
					}
				}
			}
			remove_list_variable = {
				name = dynamic_holy_site_kingdoms
				target = scope:released_holy_site_kingdom
			}
		}
	}
}

# Province scope, $FAITH$ = faith, used at start and on new holy-site creation
add_holy_site_fallback_special_building_slot_effect = {
	if = {
		limit = {
			has_special_building_slot = no
		}
		if = {
			limit = { $FAITH$ = { religion = religion:islam_religion } }
			add_special_building_slot = holy_site_mosque_01
		}
		else_if = {
			limit = { $FAITH$ = { religion = religion:christianity_religion } }
			add_special_building_slot = holy_site_cathedral_01
		}
		else_if = {
			limit = { $FAITH$ = { religion = religion:zoroastrianism_religion } }
			add_special_building_slot = holy_site_fire_temple_01
		}
		else_if = {
			limit = { $FAITH$ = { religion = religion:buddhism_religion } }
			add_special_building_slot = holy_site_buddhist_grand_temple_01
		}
		else_if = {
			limit = { $FAITH$ = { religion = religion:shintoism_religion } }
			add_special_building_slot = holy_site_japanese_temple_01
		}
		else_if = {
			limit = {
				$FAITH$ = {
					OR = {
						religion = religion:taoism_religion
						religion = religion:confucianism_religion
					}
				}
			}
			add_special_building_slot = holy_site_chinese_temple_01
		}
		else_if = {
			limit = {
				$FAITH$ = {
					religion = {
						OR = {
							this = religion:hinduism_religion
							this = religion:jainism_religion
							this = religion:tani_religion
							this = religion:bon_religion
						}
					}
				}
			}
			add_special_building_slot = holy_site_indian_grand_temple_01
		}
		else_if = {
			limit = {
				$FAITH$ = {
					religion = {
						OR = {
							this = religion:satsana_phi_religion
							this = religion:dayawism_religion
							this = religion:hantuism_religion
							this = religion:kaharingan_religion
							this = religion:tolotang_religion
							this = religion:aluk_religion
							this = religion:dab_qhuas_religion
							this = religion:biliku_religion
						}
					}
				}
			}
			add_special_building_slot = holy_site_se_asia_pagan_temple_01
		}
		else_if = {
			limit = { $FAITH$ = { religion = { is_in_family = rf_pagan } } }
			add_special_building_slot = holy_site_pagan_grand_temple_01
		}
		else = {
			add_special_building_slot = holy_site_other_grand_temple_01
		}
	}
}

# on_faith_holy_site_* provides holy_site, title, and actor
# $MESSAGE$ and $MESSAGE_BY_ACTOR$ are the loc keys
# Toast only reaches the local player
# Loc keys must be lowercase
notify_faith_of_holy_site_change_effect = {
	save_temporary_scope_as = notified_faith
	every_player = {
		limit = { faith = scope:notified_faith }
		if = {
			limit = { exists = scope:actor }
			send_interface_toast = {
				type = $MESSAGE_BY_ACTOR$
				left_icon = scope:actor
				right_icon = scope:title
				localization_values = {
					notified_faith = scope:notified_faith
					changed_holy_site = scope:holy_site
					holy_site_actor = scope:actor
				}
			}
		}
		else = {
			send_interface_toast = {
				type = $MESSAGE$
				left_icon = scope:notified_faith
				right_icon = scope:title
				localization_values = {
					notified_faith = scope:notified_faith
					changed_holy_site = scope:holy_site
				}
			}
		}
	}
}

# Called from on_rite_change, which fires for any faith or rite change. Character scope
create_conversion_memory_effect = {
	if = {
		limit = {
			exists = scope:old_rite
			scope:old_rite != rite # A rite that diverged into its own faith keeps its object; nobody converted
			NOT = { has_character_flag = conversion_memory_recently_created }
		}
		save_temporary_scope_as = conversion_memory_owner
		add_character_flag = { # The conversion window fires on_rite_change twice for a same-faith switch
			flag = conversion_memory_recently_created
			days = 2
		}
		if = {
			limit = {
				exists = scope:old_faith
				scope:old_faith != faith
			}
			create_character_memory = { type = converted_faith_memory }
			scope:new_memory = {
				set_variable = {
					name = old_faith
					value = scope:old_faith
				}
				set_variable = {
					name = new_faith
					value = scope:conversion_memory_owner.faith
				}
				set_variable = {
					name = new_rite
					value = scope:conversion_memory_owner.rite
				}
			}
		}
		else = {
			create_character_memory = { type = converted_rite_memory }
			scope:new_memory = {
				set_variable = {
					name = old_rite
					value = scope:old_rite
				}
				set_variable = {
					name = new_rite
					value = scope:conversion_memory_owner.rite
				}
			}
		}
		if = {
			limit = { is_adult = no }
			scope:new_memory = { set_variable = childhood_memory }
		}
	}
}
