﻿#####################################################################
# EFFECT LIST
#####################################################################
# !!! Remember to add all new effects with a short description here !!!

### Reward effects
# pilgrimage_completed_log_entry_effect -- Grants you Hajj or Pilgrimage rewards, is run in the on_complete of the Pilgrimage activity
# pilgrimage_additional_reward_effect -- Gives additional rewards for going on Pilgrimage with certain tenets
#
# give_pilgrim_trait_or_xp_effect -- Grants you Pilgrimage trait as appropriate; if you already have the trait it either gives xp or piety. Also logs arrival in activity log for host
# give_hajj_trait_or_xp_effect -- Grants you Hajjaj trait as appropriate; if you already have the trait it either gives xp or piety. Also logs arrival in activity log for host
# give_pilgrim_xp_effect -- If you already have the trait, determine what kind of xp or other reward to get
#
# award_piety_reward_effect -- Determines the amount of piety to reward, takes the predetermined $LENGTH$-parameter
# give_pious_type_reward_effect -- If we have the Pious/Hajj Type, determine what reward we are getting
# give_worldly_type_reward_effect -- If we have the Worldly Type, determine what reward we are getting


### Activity effects
# set_pilgrimage_distance_variable_effect -- Set variables tracking the length of the Pilgrimage (See pilgrimage_values.txt)
# save_unique_holy_site_and_update_count_variable_for_tally_effect - Track unique holy sites visited for conclusion, rewards and other statistics
# pilgrimages_track_encountered_cultures_effect -- Track how many cultures you've encountered on your pilgrimage


### Artifact effects
# christian_saint_bone_creation_effect -- Create a bone of a random saint in a box
# christian_saint_bone_tooltip_effect -- Needed to ensure tooltip matches artifact name


### Event effects
# give_pilgrimage_reward_effect -- Picks the correct Pilgrimage reward to grant
# pilgrimage_arrival_stress_loss_effect -- Stress loss when arriving at the holy site
#
# pious_type_option_effect -- Checks if you have the pious type and updates the activity_special_type_progression variable
# worldly_type_option_effect -- Checks if you have the worldly type and updates the activity_special_type_progression variable
# bonus_to_all_types_effect -- Blanket effect for an option that should boost all types
# very_pious_type_option_effect -- VERY boosting - very rare - Checks if you have the pious type and updates the activity_special_type_progression variable
# very_worldly_type_option_effect - VERY boosting - very rare - Checks if you have the worldly type and updates the activity_special_type_progression variable
#
# account_for_pilgrimage_length_type_effect -- Boost short pilgrimages a bit so they don't always get shafted
# resolve_pilgrimage_type_reward_effect -- Account for the state of our variables, apply flags that are resolved in give_pilgrimage_reward_effect - run in pilgrimage.7000
# clean_up_pilgrimage_type_reward_flags_effect -- Clean up the flags





#####################################################################
# EFFECTS
#####################################################################

### Reward effects ###

#Grants you Hajj or Pilgrimage rewards, is run in the on_complete of the Pilgrimage activity
pilgrimage_completed_log_entry_effect = {
	if = {
		limit = { has_hajj_pilgrimage_type_trigger = yes }
		involved_activity = {
			add_activity_log_entry = {
				key = hajj_completed_log
				tags = { completed }
				score = 100
				show_in_conclusion = yes
				character = root
				#Effects
				root = {
					#This effect applies traits/xp as appropriate and ALSO calls give_pilgrimage_reward_effect
					give_hajj_trait_or_xp_effect = yes
				}
			}
		}
	}
	else_if = {
		limit = { exists = involved_activity }
		involved_activity = {
			add_activity_log_entry = {
				key = pilgrimage_completed_log
				tags = { completed }
				score = 100
				show_in_conclusion = yes
				character = root
				#Effects
				root = {
					#This effect applies traits/xp as appropriate and also calls give_pilgrimage_reward_effect
					give_pilgrimage_reward_effect = yes
				}
			}
		}
	}
}

#Gives additional rewards for going on Pilgrimage with certain tenets
pilgrimage_additional_reward_effect = {
	# Armed Pilgrimages
	if = {
		limit = { 
			faith = { has_doctrine = tenet_armed_pilgrimages } 
		}
		custom_description_no_bullet = {
			text = pilgrimage_because_i_have_armed_pilgrimages
		}
		add_character_modifier = {
			modifier = pilgrimage_armed_pilgrimages_modifier
			years = 10
		}
	}
	# Mendicant Preachers
	if = {
		limit = { 
			faith = { has_doctrine = tenet_mendicant_preachers } 
		}
		custom_description_no_bullet = {
			text = pilgrimage_because_i_have_tenet_mendicant_preachers
		}
		add_character_modifier = {
			modifier = pilgrimage_mendicant_preachers_modifier
			years = 10
		}
	}
	# Pentarchy
	if = {
		limit = { 
			faith = { has_doctrine = tenet_pentarchy } 
		}
		custom_description_no_bullet = {
			text = pilgrimage_because_i_have_tenet_pentarchy
		}
		add_character_modifier = {
			modifier = pilgrimage_pentarchy_modifier
			years = 10
		}
	}
	# Struggle and Submission
	if = {
		limit = { 
			faith = { has_doctrine = tenet_struggle_submission } 
		}
		custom_description_no_bullet = {
			text = pilgrimage_because_i_have_tenet_struggle_submission
		}
		add_character_modifier = {
			modifier = pilgrimage_struggle_submission_modifier
			years = 10
		}
	}
	# Ancestor Worship
	if = {
		limit = { 
			faith = { has_doctrine = tenet_ancestor_worship } 
		}
		custom_description_no_bullet = {
			text = pilgrimage_because_i_have_tenet_ancestor_worship
		}
		add_character_modifier = {
			modifier = pilgrimage_ancestor_worship_modifier
			years = 10
		}
	}
	# Esotericism
	if = {
		limit = {
			faith = { has_doctrine = tenet_esotericism }
			NOT = {
				has_trait = lifestyle_mystic
				has_trait_xp = {
					trait = lifestyle_mystic
					value >= 100
				}
			}
		}
		custom_description_no_bullet = {
			text = pilgrimage_because_i_have_tenet_esotericism
			subject = root
		}
		random_list = {
			desc = pilgrimage_learn_from_the_bishops
			50 = {
				desc = pilgrimage_learn_from_the_bishops_success
				compare_modifier = {
					value = learning
					multiplier = 5
				}
				if = {
					limit = {
						NOT = { has_trait = lifestyle_mystic }
					}
					add_trait = lifestyle_mystic
				}
				else = {
					add_trait_xp = {
						trait = lifestyle_mystic
						value = 5
					}
				}
			}
			50 = {
				desc = pilgrimage_learn_from_the_bishops_failure
				send_interface_toast = {
					title = pilgrimage_learn_from_the_bishops_failure
					left_icon = root
				}
			}
		}
	}
}

#Grants you Pilgrimage trait as appropriate; if you already have the trait it either gives xp or piety. Also logs arrival in activity log for host
give_pilgrim_trait_or_xp_effect = {
	if = {
		limit = { has_hajj_pilgrimage_type_trigger = no }
		if = { # Host
			limit = { this = involved_activity.activity_host }
			involved_activity = {
				add_activity_log_entry = {
					key = pilgrimage_arrived_log
					score = 50
					tags = { arrived }
					character = root
					show_in_conclusion = yes
					#Effects
					root = {
						if = {
							limit = {
								NOT = { has_trait = pilgrim }
							}
							add_trait = pilgrim
						}
						else_if = {
							limit = {
								has_trait_xp = { 
									trait = pilgrim 
									value < 100
								}
							}
							add_trait_xp = { 
								trait = pilgrim 
								value = 20
							}
						}
						else_if = {
							limit = { has_lifestyle = learning_lifestyle }
							add_learning_lifestyle_xp = major_lifestyle_xp
						}
						else = { add_character_flag = should_get_trait_piety }
					}
				}
			}
		}
		else = { # Non-Host
			if = {
				limit = {
					NOT = { has_trait = pilgrim }
				}
				add_trait = pilgrim
			}
			else_if = {
				limit = {
					has_trait_xp = { 
						trait = pilgrim 
						value < 100 
					}
				}
				add_trait_xp = { 
					trait = pilgrim 
					value = 10
				}
			}
			else_if = {
				limit = { has_lifestyle = learning_lifestyle }
				add_learning_lifestyle_xp = major_lifestyle_xp
			}
			else = { add_character_flag = should_get_trait_piety }
		}
	}
	#Add trait or xp for any people coming along as well
	if = {
		limit = {
			current_travel_plan ?= {
				any_entourage_character = { count > 1 }
			}
		}
		#To get the past tense loc for the Obligatory arrival
		custom_description_no_bullet = { 
			text = pilgrimage_entourage_gains_pilgrim_trait	
			hidden_effect = {
				current_travel_plan = {
					every_entourage_character = {
						if = {
							limit = {
								NOT = { has_trait = pilgrim }
							}
							add_trait = pilgrim
						}
						else_if = {
							limit = { has_trait = pilgrim }
							add_trait_xp = {
								trait = pilgrim
								value = 10
							}
						}			
					}
				}
			}
		}
	}
}

#Grants you Hajjaj trait as appropriate; if you already have the trait it either gives xp or piety. Also logs the effect in activity log for host
give_hajj_trait_or_xp_effect = {
	if = {
		limit = { 
			NOT = { has_trait = hajjaj }
		}
		add_trait = hajjaj
	}
	else_if = {
		limit = { has_lifestyle = learning_lifestyle }
		add_learning_lifestyle_xp = major_lifestyle_xp
	}
	else = { add_character_flag = should_get_trait_piety }
	give_pilgrimage_reward_effect = yes
}

#If you already have the trait, determine what kind of xp or other reward to get
give_pilgrim_xp_effect = {
	if = {
		limit = {
			has_trait = pilgrim
			has_trait_xp = {
				trait = pilgrim
				value < 100
			}
		}
		add_trait_xp = {
			trait = pilgrim
			value = 5
		}
	}
	else_if = {
		limit = { has_lifestyle = learning_lifestyle }
		add_learning_lifestyle_xp = medium_lifestyle_xp
	}
	else_if = {
		limit = { has_trait = zealous }
		stress_impact = { base = medium_stress_loss }
	}
	else = {
		stress_impact = { base = minor_stress_loss }
	}
}

#Determines the amount of piety to reward, takes the predetermined $LENGTH$-parameter
give_piety_reward_effect = {
	add_piety = {
		value = $LENGTH$_pilgrimage_piety_gain
		# Multipliers
		## How seriously are you taking this pilgrimage?
		### Not very.
		if = {
			limit = {
				root.involved_activity = {
					has_activity_option = {
						category = pilgrimage_option_fidelity
						option = pilgrimage_fidelity_obligatory
					}
				}
			}
			multiply = 0.5
		}
		### Thuper theriouthe.
		else_if = {
			limit = {
				root.involved_activity = {
					has_activity_option = {
						category = pilgrimage_option_fidelity
						option = pilgrimage_fidelity_contemplative
					}
				}
			}
			multiply = 1.25
		}
		## Depending on how well you clung to your intent, you may get a higher reward.
		if = {
			limit = { has_character_flag = perfect_pious_reward }
			multiply = pilgrimage_perfect_pious_multiplier
		}
		else_if = {
			limit = { has_character_flag = high_pious_reward }
			multiply = pilgrimage_high_pious_multiplier
		}
		else_if = {
			limit = { has_character_flag = mid_pious_reward }
			multiply = pilgrimage_mid_pious_multiplier
		}
		else_if = {
			limit = { has_character_flag = low_pious_reward }
			multiply = pilgrimage_low_pious_multiplier
		}
		# Additives
		## Experienced pilgrims may get more piety overall.
		if = {
			limit = { has_character_flag = should_get_trait_piety }
			add = minor_piety_gain
		}
		## Certain activity options may give you more piety also.
		if = {
			limit = {
				root.involved_activity = {
					OR = {
						has_activity_option = {
							category = pilgrimage_option_pomp
							option = pilgrimage_pomp_humble
						}
						has_activity_option = {
							category = pilgrimage_option_pomp
							option = pilgrimage_pomp_ceremonial
						}
					}
				}
			}
			add = minor_piety_gain
		}
		else_if = {
			limit = {
				root.involved_activity = {
					has_activity_option = {
						category = pilgrimage_option_pomp
						option = pilgrimage_pomp_throng
					}
				}
			}
			add = medium_piety_gain
		}
		# Final mutlipliers
		if = {
			limit = {
				root.confederation ?= { has_cohesion_level_parameter = any_member_increased_pilgrimage_rewards }
			}
			multiply = 1.1
		}
	}
}

#If we have the Pious Type, determine what reward we are getting
give_pious_type_reward_effect = {
	if = {
		limit = { 
			has_trait = pilgrim #This should be awarded upon arrival from your first pilgrimage
			has_trait_xp = {
				trait = pilgrim
				value < 100
			}
		}
		if = {
			limit = { has_character_flag = perfect_pious_reward }
			add_trait_xp = {
				trait = pilgrim
				value = 40
			}
		}
		if = {
			limit = { has_character_flag = high_pious_reward }
			add_trait_xp = {
				trait = pilgrim
				value = 20
			}
		}
		if = {
			limit = { has_character_flag = mid_pious_reward }
			add_trait_xp = {
				trait = pilgrim
				value = 10
			}
		}
		if = {
			limit = { has_character_flag = low_pious_reward }
			add_trait_xp = {
				trait = pilgrim
				value = 5
			}
		}
	}
}

#If we have the Worldly Pilgrimage, determine what reward we are getting
give_worldly_type_reward_effect = {
	if = {
		limit = { has_character_flag = perfect_worldly_reward }
		if = {
			limit = { 
				root.culture.culture_head = root
				current_travel_plan = {
					any_in_list = {
						variable = visited_cultures_list
						count > 0
					}
				}
			}
			current_travel_plan = {
				if = {
					limit = {
						any_in_list = {
							variable = visited_cultures_list
							count > 5
						}
					}
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_perfect_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
				else = {
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_perfect_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
			}
		}
		else = {
			add_character_modifier = {
				modifier = pilgrimage_perfect_worldly_reward_modifier
				years = 5
			}
		}
	}
	if = {
		limit = { has_character_flag = high_worldly_reward }
		if = {
			limit = { 
				root.culture.culture_head = root
				current_travel_plan = {
					any_in_list = {
						variable = visited_cultures_list
						count > 0
					}
				}
			}
			current_travel_plan = {
				if = {
					limit = {
						any_in_list = {
							variable = visited_cultures_list
							count > 5
						}
					}
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_high_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
				else = {
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_high_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
			}
		}
		else = {
			add_character_modifier = {
				modifier = pilgrimage_high_worldly_reward_modifier
				years = 5
			}
		}
	}
	if = {
		limit = { has_character_flag = mid_worldly_reward }
		if = {
			limit = { 
				root.culture.culture_head = root
				current_travel_plan = {
					any_in_list = {
						variable = visited_cultures_list
						count > 0
					}
				}
			}
			current_travel_plan = {
				if = {
					limit = {
						any_in_list = {
							variable = visited_cultures_list
							count > 3
						}
					}
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_mid_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
				else = {
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_mid_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
			}
		}
		else = {
			add_character_modifier = {
				modifier = pilgrimage_mid_worldly_reward_modifier
				years = 5
			}
		}
	}
	if = {
		limit = { has_character_flag = low_worldly_reward }
		if = {
			limit = { 
				root.culture.culture_head = root
				current_travel_plan = {
					any_in_list = {
						variable = visited_cultures_list
						count > 0
					}
				}	
			}
			current_travel_plan = {
				if = {
					limit = {
						any_in_list = {
							variable = visited_cultures_list
							count > 5
						}
					}
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_low_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
				else = {
					every_in_list = {
						variable = visited_cultures_list
						custom = pilgrimage_cultural_acceptance_all_encountered.tt
						change_cultural_acceptance = {
							target = root.culture
							value = pilgrimage_low_worldly_acceptance_value
							desc = cultural_acceptance_gain_worldly_pilgrimage
						}
					}
				}
			}
		}
		else = {
			add_character_modifier = {
				modifier = pilgrimage_low_worldly_reward_modifier
				years = 5
			}
		}
	}
}


### Activity effects ###

#Set variables tracking the length of the Pilgrimage (See pilgrimage_values.txt)
set_pilgrimage_distance_variable_effect = {
	# Requires PILGRIM = character
	# Requires HOLY_SITE = barony_title
	$PILGRIM$.capital_barony = { # Only landed characters can go on regular pilgrimages
		save_temporary_scope_as = start_barony
	}
	# Calculate distance to the target holy site
	# The value set indicates how many Journey Events a character will get
	if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > long_pilgrimage_max_length
				}
			}
		}
		scope:activity = {
			set_variable = {
				name = pilgrimage_distance
				value = flag:very_long_pilgrimage_var
			}
		}
	}
	else_if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > medium_pilgrimage_max_length
				}
			}
		}
		scope:activity = {
			set_variable = {
				name = pilgrimage_distance
				value = flag:long_pilgrimage_var
			}
		}
	}
	else_if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > short_pilgrimage_max_length
				}
			}
		}
		scope:activity = {
			set_variable = {
				name = pilgrimage_distance
				value = flag:medium_pilgrimage_var
			}
		}
	}
	else = {
		scope:activity = {
			set_variable = {
				name = pilgrimage_distance
				value = flag:short_pilgrimage_var
			}
		}
	}
}

#Track unique holy sites visited for conclusion, rewards and other statistics - saves a variable_list and variable on your character
save_unique_holy_site_and_update_count_variable_for_tally_effect = {
	#If this is our first holy site, set up the variable
	if = {
		limit = {
			#These two really shouldn't exist without each other, but safe-checking
			NOT = { exists = var:visited_unique_holy_sites_list }
			NOT = { exists = var:visited_unique_holy_sites_count }
		}
		#Create variables
		add_to_variable_list = {
			name = visited_unique_holy_sites_list
			target = $HOLY_SITE$
		}
		set_variable = {
	 		name = visited_unique_holy_sites_count
	 		value = 1
	 	}
	 	add_character_flag = added_new_holy_site
	}
	#Otherwise add the holy site if we don't already have it
	else_if = {
		limit = {
			#These two really shouldn't exist without each other, but safe-checking
			exists = var:visited_unique_holy_sites_count
			#Does this holy site exist in the list?
			any_in_list = {
				variable = visited_unique_holy_sites_list
				NOT = { this = $HOLY_SITE$ }
			}
		}
		#Add the holy site to the list
		add_to_variable_list = {
			name = visited_unique_holy_sites_list
			target = $HOLY_SITE$
		}
		#Count up the variable
		change_variable = {
			name = visited_unique_holy_sites_count
			add = 1
		}
		add_character_flag = added_new_holy_site
	}
}

#Track how many cultures you've encountered on your pilgrimage - is saved on the current_travel_plan and destroyed with the travel plan
pilgrimages_track_encountered_cultures_effect = {
	current_travel_plan = {
		if = {
			limit = {
				NOT = { 
					exists = var:visited_cultures_list
					root.location.culture = root.culture 
				}
			}
			add_to_variable_list = {
				name = visited_cultures_list
				target = root.location.culture
			}
		}
		else_if = {
			limit = {
				NOT = {
					any_in_list = {
						variable = visited_cultures_list
						this = root.location.culture
					}
					root.location.culture = root.culture
				}
			}
			add_to_variable_list = {
				name = visited_cultures_list
				target = root.location.culture
			}
		}
	}
}


### Artifact effects ###

# Create a bone of a random saint in a box
christian_saint_bone_creation_effect = { 
	if = {
		limit = {
			has_royal_court = yes
			OR = {
				faith = faith:catholic
				faith = faith:orthodox
				faith = faith:nestorian
				faith = faith:coptic
				faith = faith:armenian_apostolic
				faith = faith:insular_celtic
				faith = { has_doctrine = tenet_christian_syncretism }
			}
		}
		hidden_effect = {
			random = {
				chance = 10 # 10% base chance
				modifier = {
					add = 10 # 20% chance for medium
					exists = scope:activity
					exists = var:pilgrimage_distance
					scope:activity.var:pilgrimage_distance = flag:medium_pilgrimage_var
				}
				modifier = {
					add = 20 # 30% chance for long
					exists = scope:activity
					exists = var:pilgrimage_distance
					scope:activity.var:pilgrimage_distance = flag:long_pilgrimage_var
				}
				modifier = {
					add = 30 # 40% chance for very long
					exists = scope:activity
					exists = var:pilgrimage_distance
					scope:activity.var:pilgrimage_distance = flag:very_long_pilgrimage_var
				}
				modifier = {
					add = 10
					has_character_flag = mid_pious_reward
				}
				modifier = {
					add = 20
					has_character_flag = high_pious_reward
				}
				modifier = {
					add = 30
					has_character_flag = perfect_pious_reward
				}
				modifier = { # half as likely for AI
					factor = 0.5
					is_ai = yes
				}
				save_scope_as = saint_bone_owner
				create_artifact = {
					name = christian_saint_bone_name
					description = christian_saint_bone_description
					type = pedestal
					visuals = bone
					template = saintly_bones_template
					modifier = artifact_monthly_piety_1_modifier
					save_scope_as = christian_saint_bone_scope
				}
				scope:christian_saint_bone_scope = {
					set_variable = { # required to track usability for template
						name = saint_bone_faith
						value = scope:saint_bone_owner.faith
					}
					set_variable = { # required to track usability for template
						name = holy_site
						value = scope:saint_bone_owner.involved_activity.activity_location.county
					}
					if = {
						limit = { exists = var:holy_site }
						#To prevent unused variable errors :catto:
					}
				}
				scope:activity ?= {
					add_activity_log_entry = {
						key = pilgrimage_completed_relic_log
						score = 75
						tags = { complete }
						character = scope:host
						artifact = scope:christian_saint_bone_scope
						show_in_conclusion = yes
						# Effects
						scope:christian_saint_bone_scope = {
							set_owner = root
						}
					}
				}
			}
		}
	}
}

# Needed to ensure tooltip matches artifact name
christian_saint_bone_tooltip_effect = { 
	if = {
		limit = { exists = scope:christian_saint_bone_scope }
		show_as_tooltip = {
			scope:christian_saint_bone_scope = { set_owner = scope:host }
		}
	}
}


### Event effects ###

#Picks the correct Pilgrimage reward to grant
give_pilgrimage_reward_effect = {
	#Short Pilgrimage reward
	if = {
		limit = { short_pilgrimage_trigger = yes }
		give_piety_reward_effect = { LENGTH = short }
		give_pious_type_reward_effect = yes
		give_worldly_type_reward_effect = yes
	}
	#Medium Pilgrimage reward
	else_if = {
		limit = { medium_pilgrimage_trigger = yes }
		give_piety_reward_effect = { LENGTH = medium }
		give_pious_type_reward_effect = yes
		give_worldly_type_reward_effect = yes
	}
	#Long Pilgrimage reward
	else_if = {
		limit = { long_pilgrimage_trigger = yes }
		give_piety_reward_effect = { LENGTH = long }
		give_pious_type_reward_effect = yes
		give_worldly_type_reward_effect = yes
	}
	#Very Long Pilgrimage reward
	else_if = {
		limit = { very_long_pilgrimage_trigger = yes }
		give_piety_reward_effect = { LENGTH = very_long }
		give_pious_type_reward_effect = yes
		give_worldly_type_reward_effect = yes
	}
	christian_saint_bone_tooltip_effect = yes
	pilgrimage_additional_reward_effect = yes
	remove_character_flag = should_get_trait_piety
	# LEGITIMACY GAIN FOR ACTIVITY PARTICIPATION - PILGRIMAGE
	pilgrimage_activity_participation_legitimacy_effect = yes
	# Sacred Mountains tenet
	if = {
		limit = {
			faith = { has_doctrine_parameter = piety_from_hills_and_mountains_pilgrimages }
			location = {
				OR = {
					terrain = hills
					terrain = mountains
					terrain = desert_mountains
				}
			}
		}
		custom_tooltip = {
			text = piety_from_hills_and_mountains_pilgrimages_tt
			add_piety = medium_piety_gain
		}
	}
}

#Stress loss when arriving at the holy site
pilgrimage_arrival_stress_loss_effect = {
	if = {
		limit = { has_trait = zealous }
		stress_impact = { base = medium_stress_loss }
	}
	else_if = {
		limit = { has_trait = cynical }
		#No stress loss for you!
	}
	else = {
		stress_impact = { base = minor_stress_loss }
	}
}


#Checks if you have the pious type and updates the activity_special_type_progression variable
pious_type_option_effect = {
	involved_activity = {
		if = {
			limit = { 
				OR = {
					activity_has_pious_pilgrimage_type_trigger = yes
					#Hajj should also be considered a pious pilgrimage
					activity_has_hajj_pilgrimage_type_trigger = yes
				}
			}
			custom_tooltip = pilgrimage_pious_type_tt_positive
			activity_special_type_progression_major= yes
		}
	}
}

#Checks if you have the worldly type and updates the activity_special_type_progression variable
worldly_type_option_effect = {
	involved_activity = {
		if = {
			limit = { activity_has_worldly_pilgrimage_type_trigger = yes }
			custom_tooltip = pilgrimage_worldly_type_tt_positive
			activity_special_type_progression_major = yes
		}
	}
}

#Blanket effect for an option that should boost all types (not Hajj)
bonus_to_all_types_effect = {
	pious_type_option_effect = yes
	worldly_type_option_effect = yes
}

#VERY boosting - very rare - Checks if you have the pious type and updates the activity_special_type_progression variable
very_pious_type_option_effect = {
	involved_activity = {
		if = {
			limit = { 
				OR = {
					activity_has_pious_pilgrimage_type_trigger = yes
					#Hajj should also be considered a pious pilgrimage
					activity_has_hajj_pilgrimage_type_trigger = yes
				}
			}
			custom_tooltip = pilgrimage_pious_type_tt_very_positive
			activity_special_type_progression_massive = yes
		}
	}
}

#VERY boosting - very rare - Checks if you have the worldly type and updates the activity_special_type_progression variable
very_worldly_type_option_effect = {
	involved_activity = {
		if = {
			limit = { activity_has_worldly_pilgrimage_type_trigger = yes }
			custom_tooltip = pilgrimage_worldly_type_tt_very_positive
			activity_special_type_progression_massive = yes
		}
	}
}

#Boost short pilgrimages a bit so they don't always get shafted
account_for_pilgrimage_length_type_effect = {
	#Short Pilgrimage
	if = {
		limit = { short_pilgrimage_trigger = yes }
		involved_activity = { activity_special_type_progression_medium = yes }
	}
	#Medium Pilgrimage
	else_if = {
		limit = { medium_pilgrimage_trigger = yes }
		involved_activity = { activity_special_type_progression_tiny = yes }
	}
}

#Account for the state of our variables, apply flags that are resolved in give_pilgrimage_reward_effect - run in pilgrimage.7000
resolve_pilgrimage_type_reward_effect = {
	#Pious Type
	involved_activity ?= {
		if = {	
			limit = {
				#Pious and Hajj
				OR = {
					activity_has_pious_pilgrimage_type_trigger = yes
					activity_has_hajj_pilgrimage_type_trigger = yes
				} 
			}
			#Perfect piousness - 75-100
			if = {
				limit = { var:activity_special_type_progression >= 75 }
				scope:host = { add_character_flag = perfect_pious_reward }
			}
			#High-end piousness - 50-75
			else_if = {
				limit = { 
					var:activity_special_type_progression < 75
					var:activity_special_type_progression >= 50
				} 
				scope:host = { add_character_flag = high_pious_reward }
			}
			#OK piousness - 25-50
			else_if = {
				limit = {
					var:activity_special_type_progression < 50
					var:activity_special_type_progression >= 25
				}
				scope:host = { add_character_flag = mid_pious_reward }
			}
			#Lousy piousness - 0-25
			else = {
				scope:host = { add_character_flag = low_pious_reward }
			}
		}
		#Worldly Type
		if = {
			limit = { activity_has_worldly_pilgrimage_type_trigger = yes }
			#Perfect worldliness - 75-100
			if = {
				limit = { var:activity_special_type_progression >= 75 }
				scope:host = { add_character_flag = perfect_worldly_reward }
			}
			#High end worldliness - 50-75
			else_if = {
				limit = { 
					var:activity_special_type_progression < 75
					var:activity_special_type_progression >= 50
				}
				scope:host = { add_character_flag = high_worldly_reward }
			}
			#OK piousness - 25-50
			else_if = {
				limit = {
					var:activity_special_type_progression < 50
					var:activity_special_type_progression >= 25
				}
				scope:host = { add_character_flag = mid_worldly_reward }
			}
			#Lousy piousness - 0-25
			else = {
				scope:host = { add_character_flag = low_worldly_reward }
			}
		}
	}
	if = {
		limit = {
			scope:host = {
				has_trait_xp = {
					trait = pilgrim
					value >= 50
				}
				NOR = { 
					has_character_flag = pilgrim_legend
					has_game_rule = historical_legends_only
				}
				has_dlc_feature = legends
			}
		}
		scope:host = {
			random = {	
				chance = { 
					value = legend_seed_chance_low #15%
					if = {
						limit = {
							has_trait_xp = {
								trait = pilgrim
								value >= 75
							}
						}
						add = legend_seed_chance_medium #55% chance
					}
					else_if = {
						limit = {
							has_trait_xp = {
								trait = pilgrim
								value >= 60
							}
						}
						add = legend_seed_chance_low #30% chance
					}
				}
				add_character_flag = pilgrim_legend
				create_legend_seed = {
					type = holy
					quality = famed
					chronicle = saintly_deed
					properties = {
						ancestor = scope:host
						religion = scope:host.religion
					}
				}
			}
		}
	}
}

#Clean up the flags
clean_up_pilgrimage_type_reward_flags_effect = {
	remove_character_flag = perfect_pious_reward
	remove_character_flag = high_pious_reward
	remove_character_flag = mid_pious_reward
	remove_character_flag = low_pious_reward
	remove_character_flag = perfect_worldly_reward
	remove_character_flag = high_worldly_reward
	remove_character_flag = mid_worldly_reward
	remove_character_flag = low_worldly_reward
}


#Dead predecessor - turn back and bury
pilgrimage_turn_back_and_bury_effect = {
	save_scope_as = root_scope
	add_prestige = major_prestige_gain
	add_character_modifier = {
		modifier = pilgrimage_turned_back_buried_modifier
		years = 5
	}
	every_vassal_or_below = {
		vassal_stance = zealot
		custom = every_zealot_vassal
		add_opinion = {
			modifier = disapproving_opinion
			target = root
			opinion = -10
		}
	}
	every_vassal_or_below = {
		vassal_stance = glory_hound
		custom = every_glory_hound_vassal
		add_opinion = {
			modifier = approving_opinion
			target = root
			opinion = 20
		}
	}
	stress_impact = {
		base = medium_stress_impact_loss
		impatient = medium_stress_impact_loss
		zealous = medium_stress_impact_gain
	}
}

#Dead predecessor - bury at destination
pilgrimage_bury_at_destination_effect = {
	save_scope_as = root_scope
	add_piety = major_piety_gain
	add_character_modifier = {
		modifier = pilgrimage_buried_at_destination_modifier
		years = 10
	}
	every_vassal_or_below = {
		vassal_stance = zealot
		custom = every_zealot_vassal
		add_opinion = {
			modifier = approving_opinion
			target = root
			opinion = 20
		}
	}
	every_vassal_or_below = {
		vassal_stance = glory_hound
		custom = every_glory_hound_vassal
		add_opinion = {
			modifier = disapproving_opinion
			target = root
			opinion = -10
		}
	}
	stress_impact = {
		base = medium_stress_impact_loss
		zealous = medium_stress_impact_loss
		cynical = minor_stress_impact_gain
	}
}

#Dead predecessor - go to destination and back, then bury
pilgrimage_bury_at_home_effect = {
	save_scope_as = root_scope
	add_prestige = medium_prestige_gain
	add_piety = medium_piety_gain
	add_character_modifier = {
		modifier = pilgrimage_buried_at_capital_modifier
		years = 15
	}
	every_vassal_or_below = {
		vassal_stance = zealot
		custom = every_zealot_vassal
		add_opinion = {
			modifier = approving_opinion
			target = root
			opinion = 15
		}
	}
	every_vassal_or_below = {
		vassal_stance = glory_hound
		custom = every_glory_hound_vassal
		add_opinion = {
			modifier = approving_opinion
			target = root
			opinion = 15
		}
	}
	stress_impact = {
		base = major_stress_impact_loss
		zealous = major_stress_impact_loss
		cynical = medium_stress_impact_gain
	}
}
