NEOW_set_bhakti_effect = { #reminder: patron gods will not remove on conversion until a specific base game event is replaced/overwritten. This folder is being created, rather than overwriting a base game folder to reduce overall complexity/troubleshooting
	# Remove any previous personal god relation.

	## Britannic
	# Leviathan
	if = {
		limit = { portrait_religious_faith_or_foundational_trigger = { FAITH = faith:leviathan } }

		if = {
			limit = {
				has_character_modifier = bhakti_leviathan_nationalisators
			}
			remove_character_modifier = bhakti_leviathan_nationalisators
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_leviathan_technosaurs
			}
			remove_character_modifier = bhakti_leviathan_technosaurs
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_leviathan_mechasaurs
			}
			remove_character_modifier = bhakti_leviathan_mechasaurs
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_leviathan_petrosaurs
			}
			remove_character_modifier = bhakti_leviathan_petrosaurs
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_leviathan_metallurgicals
			}
			remove_character_modifier = bhakti_leviathan_metallurgicals
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_leviathan_nautiluses
			}
			remove_character_modifier = bhakti_leviathan_nautiluses
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_leviathan_aerosaurs
			}
			remove_character_modifier = bhakti_leviathan_aerosaurs
			add_piety = massive_piety_loss
		}
	}

	## Folketero
	else_if = {
		limit = {
			OR = {
				has_character_modifier = bhakti_folketero_jesus
				has_character_modifier = bhakti_folketero_per
				has_character_modifier = bhakti_folketero_lucia
				has_character_modifier = bhakti_folketero_olaf
			}
		}

		if = {
			limit = {
				has_character_modifier = bhakti_folketero_jesus
			}
			remove_character_modifier = bhakti_folketero_jesus
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_folketero_per
			}
			remove_character_modifier = bhakti_folketero_per
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_folketero_lucia
			}
			remove_character_modifier = bhakti_folketero_lucia
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_folketero_olaf
			}
			remove_character_modifier = bhakti_folketero_olaf
			add_piety = massive_piety_loss
		}
	}

	## Orthodox
	else_if = {
		limit = {
			OR = {
				has_character_modifier = bhakti_orthodox_paisos
				has_character_modifier = bhakti_orthodox_gabriel
				has_character_modifier = bhakti_orthodox_adriana
				has_character_modifier = bhakti_orthodox_olga
				has_character_modifier = bhakti_orthodox_palamas
				has_character_modifier = bhakti_orthodox_surgeon
				has_character_modifier = bhakti_orthodox_lazar
				has_character_modifier = bhakti_orthodox_theotokos
			}
		}

		if = {
			limit = {
				has_character_modifier = bhakti_orthodox_paisos
			}
			remove_character_modifier = bhakti_orthodox_paisos
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_gabriel
			}
			remove_character_modifier = bhakti_orthodox_gabriel
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_adriana
			}
			remove_character_modifier = bhakti_orthodox_adriana
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_olga
			}
			remove_character_modifier = bhakti_orthodox_olga
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_palamas
			}
			remove_character_modifier = bhakti_orthodox_palamas
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_surgeon
			}
			remove_character_modifier = bhakti_orthodox_surgeon
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_lazar
			}
			remove_character_modifier = bhakti_orthodox_lazar
			add_piety = massive_piety_loss
		}
		else_if = {
			limit = {
				has_character_modifier = bhakti_orthodox_theotokos
			}
			remove_character_modifier = bhakti_orthodox_theotokos
			add_piety = massive_piety_loss
		}
	}

	else = {
		# If no current bhakti, cost is reduced.
		add_piety = medium_piety_loss
	}

	add_character_modifier = $BHAKTI$
}

# i dont know where else to put this
restore_patriarch_scripted_effect = {
	root.faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_ep_restored
		}
	}

	hidden_effect = {
		create_character = {
			template = religious_leader_character
			random_traits = no
			save_scope_as = new_patriarch
		}

		scope:new_patriarch = {
			add_trait = education_learning_3
			add_trait = zealous
			add_trait = intellect_good_1

			random_list = {
				1 = { add_trait = humble }
				1 = { add_trait = diligent }
				1 = { add_trait = just }
				1 = { add_trait = compassionate }
				1 = { add_trait = stubborn }
			}
		}

		province:496 = {
			remove_building = hagia_sophia_02
			add_special_building = hagia_sophia_01
		}

		title:k_orthodox = {
			set_capital_county = title:c_byzantion
		}

		create_title_and_vassal_change = {
			type = returned
			save_scope_as = patriarch_change
			add_claim_on_loss = no
		}

		title:k_orthodox = {
			change_title_holder = {
				holder = scope:new_patriarch
				change = scope:patriarch_change
			}
		}

		# The title carrying the Constantinople clerical region must have
		# exactly the same holder as the new Ecumenical Patriarch.
		title:d_et_constantinople = {
			change_title_holder = {
				holder = scope:new_patriarch
				change = scope:patriarch_change
			}
		}

		scope:new_patriarch = {
			change_liege = {
				liege = root
				change = scope:patriarch_change
			}
		}

		resolve_title_and_vassal_change = scope:patriarch_change

		# The Athonite Empire supersedes Constantinople as the Orthodox HoF,
		# if it has already been proclaimed.
		if = {
			limit = { title:e_athos = { is_title_created = no } }
			faith:orthodox = { set_religious_head_title = title:k_orthodox }
		}
	}
}

restore_r_patriarch_scripted_effect = {
	root.faith = {
		change_fervor = {
			value = 100
			desc = fervor_gain_mp_restored
		}
	}

	hidden_effect = {
		create_character = {
			template = religious_leader_character
			random_traits = no
			save_scope_as = new_patriarch
		}

		scope:new_patriarch = {
			add_trait = education_learning_3
			add_trait = zealous
			add_trait = intellect_good_1
			random_list = {
				1 = { add_trait = humble }
				1 = { add_trait = diligent }
				1 = { add_trait = just }
				1 = { add_trait = compassionate }
				1 = { add_trait = stubborn }
			}
		}

		# The patriarchate and the Moscow clerical region are inseparable.
		# The region itself is assigned to d_et_moscow in title history.
		title:k_russian_orthodox = {
			set_capital_county = title:c_moskva
		}

		create_title_and_vassal_change = {
			type = returned
			save_scope_as = patriarch_change
			add_claim_on_loss = no
		}
		title:k_russian_orthodox = {
			change_title_holder = {
				holder = scope:new_patriarch
				change = scope:patriarch_change
			}
		}
		title:d_et_moscow = {
			change_title_holder = {
				holder = scope:new_patriarch
				change = scope:patriarch_change
			}
		}

		scope:new_patriarch = {
			change_liege = {
				liege = root
				change = scope:patriarch_change
			}
		}
		resolve_title_and_vassal_change = scope:patriarch_change

		# Explicitly restore the Moscow Patriarch as religious head.
		faith:russian_orthodox = {
			set_religious_head_title = title:k_russian_orthodox
		}
	}
}
form_marblist_decision_effect = {
	add_prestige = massive_prestige_gain
	add_piety = massive_piety_gain
	save_scope_as = scoped_ruler

	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	title:e_marblist= {
		change_title_holder = {
			holder = scope:scoped_ruler
			change = scope:change
		}
	}
	scope:scoped_ruler = {
			add_trait = augustus 
			add_trait = zealous
	}
	resolve_title_and_vassal_change = scope:change
	hidden_effect = { 
		province:496 = {
		remove_building = hagia_sophia_02
		add_special_building = hagia_sophia_01
		}
		faith:marblist = {
		remove_doctrine = special_doctrine_not_allowed_to_hof
		add_doctrine = special_doctrine_byzantine_dreaming
		}
		set_primary_title_to = title:e_marblist
		title:k_thrace = { set_de_jure_liege_title = title:e_marblist }
		title:k_macedonia = { set_de_jure_liege_title = title:e_marblist }
		title:k_hellas= { set_de_jure_liege_title = title:e_marblist }
		title:k_bulgaria = { set_de_jure_liege_title = title:e_marblist }
		title:k_epirus = { set_de_jure_liege_title = title:e_marblist }
		title:k_cyprus = { set_de_jure_liege_title = title:e_marblist }
		title:k_krete = { set_de_jure_liege_title = title:e_marblist }
		if = {
			limit = {
				exists = title:e_greece
			}
			destroy_title = title:e_greece
		}
		if = {
			limit = {
				exists = title:e_turkey 
			}
			destroy_title = title:e_turkey 
		}
		if = {
			limit = {
					title:k_k_nikaea = { is_titular = no }
					OR = {
						has_title = title:k_nikaea
						completely_controls = title:k_nikaea 
					}
				}
				title:k_k_nikaea = { set_de_jure_liege_title = title:e_NEOW_byzantium }
		}
		if = {
			limit = {
					title:k_anatolia = { is_titular = no }
					OR = {
						has_title = title:k_anatolia
						completely_controls = title:k_anatolia 
					}
				}
				title:k_anatolia = { set_de_jure_liege_title = title:e_NEOW_byzantium }
		}
		if = {
			limit = {
					title:k_pontus = { is_titular = no }
					OR = {
						has_title = title:k_pontus
						completely_controls = title:k_pontus
					}
				}
				title:k_pontus = { set_de_jure_liege_title = title:e_NEOW_byzantium }
		}
		if = {
			limit = {
					title:k_cilicia = { is_titular = no }
					OR = {
						has_title = title:k_cilicia 
						completely_controls = title:k_cilicia 
					}
				}
				title:k_cilicia = { set_de_jure_liege_title = title:e_NEOW_byzantium }
		}
		if = {
			limit = {
					title:k_armenitolia = { is_titular = no }
					OR = {
						has_title = title:k_armenitolia
						completely_controls = title:k_armenitolia
					}
				}
				title:k_armenitolia = { set_de_jure_liege_title = title:e_NEOW_byzantium }
		}
		if = {
			limit = {
					title:k_serbia = { is_titular = no }
					OR = {
						has_title = title:k_serbia
						completely_controls = title:k_serbia
					}
				}
				title:k_serbia = { set_de_jure_liege_title = title:e_marblist }
		}
		else_if = {
			limit = {
					title:k_montenegro = { is_titular = no }
					OR = {
						has_title = title:k_montenegro
						completely_controls = title:k_montenegro
					}
				}
				title:k_montenegro = { set_de_jure_liege_title = title:e_marblist }
		}
	}
}
create_head_of_faith_title_effect = {
	# Pay costs
	if = {
		limit = {
			faith = {
				has_doctrine_parameter = temporal_head_of_faith
				OR = {
					has_doctrine = muhammad_succession_sunni_doctrine
					has_doctrine = muhammad_succession_shia_doctrine
				}
			}	
		}
		if = { # Very expensive for non-sayyid to become a Caliph.
			limit = { NOT = { has_trait = sayyid } }
			add_piety = massive_piety_loss
		}
		# Free otherwise
	}
	else_if = {
		limit = { faith = { has_doctrine_parameter = temporal_head_of_faith } }
		add_piety = major_piety_loss
	}
	else_if = {
		limit = {
			faith = {
				NOT = {
					#HO HoF already paid on the HO interface
					has_doctrine_parameter = holy_order_head_of_faith
				}
			}
		}
		remove_short_term_gold = medium_gold_max_value
	}

	# (Re)create the HoF title
	custom_description_no_bullet = {
		text = create_temporal_head_of_faith
		subject = root
		object = scope:faith
		
		# 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/Holy Order HoF
		else_if = {
			limit = {
				faith = {
					OR = {
						has_doctrine_parameter = spiritual_head_of_faith
						has_doctrine_parameter = holy_order_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
#			if = {
#				limit = {
#					faith = {
#						has_doctrine_parameter = holy_order_head_of_faith
#					}
#				}
#				if = {
#					limit = {
#						#only create a HoF if none exists
#						faith = {
#							any_faith_holy_order = {
#								count = 0
#								exists = this
#							}
#						}
#					}
#					trigger_event = N3OW_holy_order.0003
#				}
#				else = {
#					faith = {
#						random_faith_holy_order = {
#							save_scope_as = picked_holy_order
#						}
#					}
#					faith.set_religious_head_title = scope:picked_holy_order.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
				}
			}
		}
	}
}
# Follow the great-schism resolution pattern: preserve the victorious Church,
# remove the rival head of faith, and make its rites subordinate to the reunion.
# Rulers of the former rival communion can accept or reject that settlement.
mend_orthodox_schism_scripted_effect = {
	hidden_effect = {
		save_scope_as = schism_mender

		faith = {
			save_scope_as = reunited_orthodox_faith
			# As in the base-game schism, the prevailing faith becomes the
			# accepted mainline rather than maintaining two rival mainlines.
			if = {
				limit = { has_doctrine = special_doctrine_true_orthodox }
				remove_doctrine = special_doctrine_true_orthodox
			}
			if = {
				limit = { NOT = { has_doctrine = special_doctrine_mainline_orthodox } }
				add_doctrine = special_doctrine_mainline_orthodox
			}
			change_fervor = {
				value = 25
				desc = fervor_gain_orthodox_schism_mended
			}
		}

		# The losing branch remains recognisable, but no longer has a
		# rival patriarch or an independent family of rites.
		if = {
			limit = { faith = faith:orthodox }
			faith:russian_orthodox = { save_scope_as = schismatic_orthodox_faith }
		}
		else = {
			faith:orthodox = { save_scope_as = schismatic_orthodox_faith }
		}

		scope:schismatic_orthodox_faith = {
			remove_religious_head_title = yes
			if = {
				limit = { has_doctrine = special_doctrine_mainline_orthodox }
				remove_doctrine = special_doctrine_mainline_orthodox
			}
			if = {
				limit = { NOT = { has_doctrine = special_doctrine_true_orthodox } }
				add_doctrine = special_doctrine_true_orthodox
			}
			change_fervor = {
				value = -25
				desc = fervor_loss_orthodox_schism_mended
			}
			# Base-game-style incorporation of the losing Church's rites.
			every_faith_rite = {
				limit = { faith != scope:reunited_orthodox_faith }
				set_can_convert_to_rite = no
				set_parent_faith = {
					target = scope:reunited_orthodox_faith
					main = no
					include_derived = no
				}
			}
		}

		# Comparable to the base-game great-schism offer: the defeated
		# Church's secular rulers can enter the reunited communion.
		every_ruler = {
			limit = {
				faith = scope:schismatic_orthodox_faith
				NOT = { government_has_flag = government_is_theocracy }
				this != this.faith.religious_head
			}
			trigger_event = NEOW_religious_decision.0210
		}

		set_nickname_effect = { NICKNAME = nick_the_ecumenist }
		scope:schism_mender = { trigger_event = NEOW_religious_decision.0211 }
	}
}

# The ecclesiastical ruler of Constantinople assumes the patriarchal throne
# personally, rather than enthroning a newly created bishop.
NEOW_eccelarch_restore_ecumenical_patriarchate_effect = {
	root.faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_ep_restored
		}
	}

	hidden_effect = {
		save_scope_as = new_patriarch

		province:496 = {
			if = {
				limit = { has_building = hagia_sophia_02 }
				remove_building = hagia_sophia_02
				add_special_building = hagia_sophia_01
			}
		}

		title:k_orthodox = { set_capital_county = title:c_byzantion }

		create_title_and_vassal_change = {
			type = returned
			save_scope_as = patriarch_change
			add_claim_on_loss = no
		}

		title:k_orthodox = {
			change_title_holder = {
				holder = scope:new_patriarch
				change = scope:patriarch_change
			}
		}

		# This landless see owns the Constantinopolitan clerical-region office.
		title:d_et_constantinople = {
			change_title_holder = {
				holder = scope:new_patriarch
				change = scope:patriarch_change
			}
		}

		resolve_title_and_vassal_change = scope:patriarch_change

		if = {
			limit = { title:e_athos = { is_title_created = no } }
			faith:orthodox = { set_religious_head_title = title:k_orthodox }
		}
	}
}

# The Empire of Athos replaces the Constantinopolitan HoF title for Orthodoxy.
# Consecration of the Citadel is automatic when the new empire is proclaimed.
NEOW_form_empire_of_athos_effect = {
	save_scope_as = athonite_founder

	create_title_and_vassal_change = {
		type = created
		save_scope_as = athonite_title_change
		add_claim_on_loss = no
	}

	title:e_athos = {
		change_title_holder = {
			holder = scope:athonite_founder
			change = scope:athonite_title_change
		}
	}

	resolve_title_and_vassal_change = scope:athonite_title_change

	# Proclaiming the empire replaces the existing Mount Athos building with
	# the fourth stage. All tier-three bonuses are preserved at tier four.
	province:3776 = {
		if = {
			limit = { has_building = mount_athos_01 }
			remove_building = mount_athos_01
		}
		else_if = {
			limit = { has_building = mount_athos_02 }
			remove_building = mount_athos_02
		}
		else_if = {
			limit = { has_building = mount_athos_03 }
			remove_building = mount_athos_03
		}
		if = {
			limit = { NOT = { has_building = mount_athos_04 } }
			add_special_building = mount_athos_04
		}
	}

	faith:orthodox = { set_religious_head_title = title:e_athos }
	set_primary_title_to = title:e_athos
}
