﻿### EK NOTE: Removed references to vanilla religions
### EK NOTE: Implemented the Mystical Birthright HoF

#Effects relating to religions

# check_for_equal_doctrine_effect
# set_bhakti_effect

# Used to set Equal Law when the Equal Faith Doctrine is gained
check_for_equal_doctrine_effect = {
	if = {
		limit = {
			is_playable_character = yes
			faith = {
				has_doctrine = doctrine_gender_equal
			}
			scope:old_faith = {
				NOT = { has_doctrine = doctrine_gender_equal }
			}
			NOT = {
				has_realm_law = equal_law
			}
		}
		add_realm_law_skip_effects = equal_law
	}
}

##EK DISABLED: The Bhakti tenet has been disabled
# Used to set a Hindu's personal god.
# set_bhakti_effect = {
	# # Remove any previous personal god relation.
	# ## HINDUISM
	# if = {
		# limit = { religion = religion:hinduism_religion }
		# if = {	
			# limit = {
				# has_character_modifier = bhakti_hinduism_ganga
			# }
			# remove_character_modifier = bhakti_hinduism_ganga
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_hinduism_saraswati
			# }
			# remove_character_modifier = bhakti_hinduism_saraswati
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_hinduism_kali
			# }
			# remove_character_modifier = bhakti_hinduism_kali
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_hinduism_kubera
			# }
			# remove_character_modifier = bhakti_hinduism_kubera
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_vaishnavism_lakishmi
			# }
			# remove_character_modifier = bhakti_vaishnavism_lakishmi
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_vaishnavism_jagganath
			# }
			# remove_character_modifier = bhakti_vaishnavism_jagganath
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_vaishnavism_jagganath
			# }
			# remove_character_modifier = bhakti_vaishnavism_jagganath
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_vaishnavism_hayagriva
			# }
			# remove_character_modifier = bhakti_vaishnavism_hayagriva
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_shaivism_parvati
			# }
			# remove_character_modifier = bhakti_shaivism_parvati
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_shaivism_virabhadra
			# }
			# remove_character_modifier = bhakti_shaivism_virabhadra
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_shaivism_munishwarar
			# }
			# remove_character_modifier = bhakti_shaivism_munishwarar
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_shaivism_dakshinamoorthy
			# }
			# remove_character_modifier = bhakti_shaivism_dakshinamoorthy
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_smartism_ganesha
			# }
			# remove_character_modifier = bhakti_smartism_ganesha
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_smartism_vishnu
			# }
			# remove_character_modifier = bhakti_smartism_vishnu
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_smartism_shiva
			# }
			# remove_character_modifier = bhakti_smartism_shiva
			# add_piety = massive_piety_loss
		# }
	# }
	# # GERMANIC
	# else_if = {
		# limit = { religion = religion:germanic_religion }
		# if = {	
			# limit = {
				# has_character_modifier = bhakti_germanic_generic_odin
			# }
			# remove_character_modifier = bhakti_germanic_generic_odin
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_germanic_not_danish_ullr
			# }
			# remove_character_modifier = bhakti_germanic_not_danish_ullr
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_germanic_danish_tyr
			# }
			# remove_character_modifier = bhakti_germanic_danish_tyr
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_germanic_generic_thor
			# }
			# remove_character_modifier = bhakti_germanic_generic_thor
			# add_piety = massive_piety_loss
		# }
		# else_if = {	
			# limit = {
				# has_character_modifier = bhakti_germanic_generic_freyr
			# }
			# remove_character_modifier = bhakti_germanic_generic_freyr
			# add_piety = massive_piety_loss
		# }
	# }
	# else = {
		# # If no current bhakti, cost is reduced.
		# add_piety = medium_piety_loss
	# }

	# add_character_modifier = $BHAKTI$
# }


create_head_of_faith_title_effect = {
	#EK DISABLE: Muhammad does not exist
	# 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
	#}
	if = {
		limit = { faith = { has_doctrine_parameter = temporal_head_of_faith } }
		add_piety = major_piety_loss
	}
	else = { 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 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.
		}
		#EK ADDITION: Mystical Birthright HoF
		else_if = {
			limit = {
				faith = {
					has_doctrine = doctrine_mystical_birthright_head
				}
			}
			if = {
				limit = {
					faith = {
						NOT = { exists = religious_head_title }
					}
				}
				trigger_event = faith_creation.0004 # Creates new dynamic title
			}
			else = {
				trigger_event = faith_creation.0014 # Re-uses existing title.
			}
		}
		#EK ADDITION END
	}

	# 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
				}
			}
		}
	}
}

force_create_head_of_faith_title_Effect = { # Has no cost, should be done manually on a per-case basis
	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 } }
			trigger_event = faith_creation.0001
		}

		# Spiritual HoF
		else_if = {
			limit = { faith = { has_doctrine_parameter = spiritual_head_of_faith } }
			trigger_event = faith_creation.0002
		}
	}
}

# 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
	}
	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 }
	create_dynamic_title = {
		tier = duchy
		name = REL_HEAD_TITLE_NAME
	}
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
		add_claim_on_loss = no
	}
	# Nab a suitable character.
	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
			culture = $CREATOR$.culture
			learning = { 14 22 }
			#EK EDIT: EK specific character setup effect
			after_creation = {
				ek_character_setup_effect = yes
			}
			#EK EDIT END
			save_scope_as = new_religious_head
		}
	}
	# Try to land the new title.
	scope:my_faith = {
		random_holy_site = {
			# 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 }
	# 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 = {
		add_trait_force_tooltip = 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 = {
	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 = {
	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$
		capital = $CAPITAL$
		save_scope_as = $NEW_HO_SCOPE$
	}
	# Struggle Catalysts.
	$FOUNDER$ = {
		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					phase_has_catalyst = catalyst_creating_a_holy_order
				}
			}
			every_character_struggle = {
				involvement = involved
				limit = { phase_has_catalyst = catalyst_creating_a_holy_order }
				activate_struggle_catalyst = {
					catalyst = catalyst_creating_a_holy_order
					character = $FOUNDER$
				}
			}
		}
	}
}


# Get random faith
get_random_faith_effect = {
	if = {
		limit = { $FORBIDROOTFAITH$ = yes }
		random_religion_global = {
			limit = { any_faith = { NOT = { this = root.faith } } }
			random_faith = {
				limit = { NOT = { 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
	}
	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 = {
			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 # todo_cd_polish, this loc might not fit the scenario
					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
				NOT = { 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
				NOT = { 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 = {
			faith = { has_doctrine_parameter = 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 = {
			faith = { has_doctrine_parameter = 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 = {
			faith = { has_doctrine_parameter = self_mutilation_active }
		}
		custom_tooltip = {
			text = self_mutilation_major_piety_gain_tt
			add_piety = major_piety_gain
		}
	}
}
