#grab_appropriate_chronicler_effect
#grab_appropriate_bard_feast_effect - this one doesn't check if they're available, so it can be used in activities
#grab_appropriate_bard_effect - this one checks if they're available
#grab_mythical_founder_effect
#disburse_hunt_legend_seeds
#change_legend_quality_effect - This adjusts the quality of a legend using an invisible progress bar that goes from 0-100. When it hits 100, the legend increases one step in quality, if it dips below 0, the legend loses one step in quality
#legend_completion_quality_flag_effect
#legend_ending_rewards_heroic_effect
#legend_ending_rewards_holy_effect
#legend_ending_rewards_legitimizing_effect
#legend_completion_type_flag_effect
#get_appropriate_ancient_people - this looks for regionally appropriate ancient people for you to lay claim to
# get_appropriate_exotic_location - Atlantis, Vinland, places of dubious historicity
# generate_godly_descent_seed_support_effect - create a godly descent seed based on loc keys
# generate_religion_descent_effect - generate a godly descent seed based on scopes
# balancing_legend_global_var_effect
# send_interface_message_legend_notification_effect
# send_interface_toast_legend_notification_effect
# create_swan_knight_legend_seed_effect

grab_appropriate_chronicler_effect = {
	if = { #If the culture would have relied on poets for this we grab them first
		limit = {
			OR = {
				culture = { 
					OR = {
						has_cultural_tradition = tradition_fp1_northern_stories
						has_cultural_parameter = poet_trait_gives_bonuses
						has_cultural_tradition = tradition_storytellers
						has_cultural_tradition = tradition_chanson_de_geste 
					}
				}
				faith = {
					trait_is_virtue = lifestyle_poet
				}
			}
			any_court_position_holder = {
				type = court_poet_court_position
				is_available_ai_adult = yes
			}
		}
		random_court_position_holder = {
			type = court_poet_court_position
			limit = {
				is_available_ai_adult = yes
			}
			save_scope_as = chronicler
		}
	}		
	else_if = { #Otherwise we go for the Chronicler first
		limit = {
			any_court_position_holder = {
				type = chronicler_court_position
				is_available_ai_adult = yes
			}
		}
		random_court_position_holder = {
			type = chronicler_court_position
			limit = {
				is_available_ai_adult = yes
			}
			save_scope_as = chronicler
		}
	}
	else_if = {
		limit = {
			any_court_position_holder = {
				type = court_poet_court_position
				is_available_ai_adult = yes
			}
		}
		random_court_position_holder = {
			type = court_poet_court_position
			limit = {
				is_available_ai_adult = yes
			}
			save_scope_as = chronicler
		}
	}
	else_if = {
		limit = {
			any_court_position_holder = {
				type = court_musician_court_position
				is_available_ai_adult = yes
			}
		}
		random_court_position_holder = {
			type = court_musician_court_position
			limit = {
				is_available_ai_adult = yes
			}
			save_scope_as = chronicler
		}
	}
	else = {
		ordered_courtier = {
			limit = {
				this != root
				is_available_ai_adult = yes
			}
			order_by = {
				value = diplomacy
				if = {
					limit = {
						OR = {
							has_trait = lifestyle_poet
							has_trait = journaller
						}
					}
					add = 10
				}
				if = {
					limit = { is_spouse_of = root  }
					add = -2
				}
			}
			save_scope_as = chronicler
		}
	}
}

grab_appropriate_bard_feast_effect = {
	if = {
		limit = {
			employs_court_position = court_musician_court_position
			scope:activity = {
				any_attending_character = {
					employer = root
					has_court_position = court_musician_court_position
					is_physically_able_ai_adult = yes
				}
			}
		}
		scope:activity = {
			random_attending_character = {
				limit = {
					employer = root
					has_court_position = court_musician_court_position
					is_physically_able_ai_adult = yes
				}
				save_scope_as = bard
			}
		}
	}
	else_if = {
		limit = {
			employs_court_position = court_poet_court_position
			scope:activity = {
				any_attending_character = {
					employer = root
					has_court_position = court_poet_court_position
					is_physically_able_ai_adult = yes
				}
			}
		}
		scope:activity = {
			random_attending_character = {
				limit = {
					employer = root
					has_court_position = court_poet_court_position
					is_physically_able_ai_adult = yes
				}
				save_scope_as = bard
			}
		}
	}
	else_if = {
		limit = {
			scope:activity = {
				any_attending_character = {
					OR = {
						is_vassal_of = root
						is_courtier_of = root
					}
					this != root
					NOT = { is_spouse_of = root }
					NOT = { is_child_of = root }
					NOT = { is_councillor_of = root }
					is_physically_able_ai_adult = yes
				}				
			}
		}
		scope:activity = {
			ordered_attending_character = {
				limit = {
					OR = {
						is_vassal_of = root
						is_courtier_of = root
					}
					this != root
					NOT = { is_spouse_of = root }
					NOT = { is_child_of = root }
					is_physically_able_ai_adult = yes
				}
				order_by = {
					value = diplomacy
					if = {
						limit = {
							has_trait = lifestyle_poet
						}
						add = 10
					}
					if = {
						limit = {
							has_trait = journaller
						}
						add = 5
					}
				}
			}
			save_scope_as = bard
		}
	}
	else = {
		create_character = {
			template = bard_template
			dynasty = none
			location = root.location
			gender_female_chance = root_faith_dominant_gender_female_chance
			save_scope_as = bard
		}
	}
}

grab_appropriate_bard_effect = {
	if = {
		limit = {
			employs_court_position = court_musician_court_position
			any_court_position_holder = {
				type = court_musician_court_position
				is_available_healthy_ai_adult = yes
			}
		}
		random_court_position_holder = {
			type = court_musician_court_position
			limit = { is_available_healthy_ai_adult = yes }
			save_scope_as = bard
		}
	}
	else_if = {
		limit = {
			employs_court_position = court_poet_court_position
			any_court_position_holder = {
				type = court_poet_court_position
				is_available_healthy_ai_adult = yes
			}
		}
		random_court_position_holder = {
			type = court_poet_court_position
			limit = { is_available_healthy_ai_adult = yes }
			save_scope_as = bard
		}
	}
	else_if = {
		limit = {
			any_courtier = {
				this != root
				NOT = { is_spouse_of = root }
				NOT = { is_child_of = root }
				NOT = { is_councillor_of = root }
				is_available_healthy_ai_adult = yes
			}
		}
		ordered_courtier = {
			limit = {
				this != root
				NOT = { is_spouse_of = root }
				NOT = { is_child_of = root }
				NOT = { is_councillor_of = root }
				is_available_healthy_ai_adult = yes
			}
			order_by = {
				value = diplomacy
				if = {
					limit = {
						has_trait = lifestyle_poet
					}
					add = 10
				}
				if = {
					limit = {
						has_trait = journaller
					}
					add = 5
				}
			}
			save_scope_as = bard
		}
	}
	else = {
		create_character = {
			template = bard_template
			dynasty = none
			location = root.location
			gender_female_chance = root_faith_dominant_gender_female_chance
			save_scope_as = bard
		}
	}
}

grab_mythical_founder_effect = {
	hidden_effect = {
		if = {
			limit = {
				NOT = { exists = var:mythical_ancestor_var }
			}
			random_list = {
#				10 = { # Charles Martell
#					trigger = {
#						AND = { 
#							OR = {
#								root.primary_title = title:k_france
#								root.primary_title = title:e_france
#							}
#							current_date > 1000.1.1
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:charles_martell }
#				}
#				10 = { # Alexander the Great
#					trigger = {
#						OR = {			
#							root.primary_title = title:k_thessalonika
#							root.primary_title = title:k_hellas
#							root.primary_title = title:k_egypt
#							AND = {
#								OR = {
#									root.primary_title = title:k_persia
#									root.primary_title = title:e_persia
#								}
#								NOR = {
#									root.faith.religion = religion:islam_religion
#									root.faith.religion = religion:zoroastrianism_religion
#								}
#							}
#							root.primary_title = title:k_anatolia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:alexander }
#				}
#				10 = { # Lech
#					trigger = {
#						root.primary_title = title:k_poland
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:lech }
#				}
#				10 = { # Czech
#					trigger = {
#						OR = {
#							root.primary_title = title:k_bohemia
#							root.primary_title = title:d_bohemia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:czech }
#				}
#				10 = { # Rus
#					trigger = {
#						OR = {
#							root.primary_title = title:k_white_rus
#							root.primary_title = title:k_ruthenia
#							root.primary_title = title:e_russia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:rus }
#				}
#				10 = { # King Arthur
#					trigger = {
#						OR = {
#							root.primary_title = title:k_wales
#							root.primary_title = title:k_cornwall
#							root.primary_title = title:k_england
#							root.primary_title = title:e_britannia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:arthur }
#				}
#				10 = { # Corineus
#					trigger = {
#						root.primary_title = title:k_cornwall
#					}
#					modifier = {
#						current_date < 1000.1.1
#						factor = 3
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:corineus }
#				}
#				10 = { # Magnus Maximus
#					trigger = {
#						root.primary_title = title:k_wales
#					}
#					modifier = {
#						current_date < 1000.1.1
#						factor = 3
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:magnus_maximus }
#				}
#				30 = { # Brutus of Troy, we want this to be the default for the Brits
#					trigger = {
#						OR = {
#							root.primary_title = title:k_england
#							root.primary_title = title:e_britannia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:brutus }
#				}
#				10 = { # Aeneas of Troy
#					trigger = {
#						 OR = {
#							#Used in the Roman de Brut
#							root.primary_title = title:k_england
#							root.primary_title = title:e_britannia
#							#Also used by the house of Tosny
#							root.primary_title = title:d_normandy
#							capital_province = { geographical_region = world_europe_west_britannia }
#							#And of course used in Rome
#							root.primary_title = title:k_italy
#							root.primary_title = title:k_romagna
#							root.primary_title = title:k_sardinia
#							root.primary_title = title:e_italy
#							root = {
#								is_roman_emperor_primary_title_excluding_byzantium_trigger = yes
#							}
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:aeneas }
#				}
#				10 = { # Scota
#					trigger = {
#						OR = { # Gaels
#							root.primary_title = title:k_scotland
#							root.primary_title = title:k_ireland
#						}
#						current_date > 1100.1.1
#					}
#					modifier = {
#						root.primary_title = title:k_scotland
#						factor = 3
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:scota }
#				}
#				10 = { # Romulus
#					trigger = {
#						OR = {
#							root.primary_title = title:k_italy
#							root.primary_title = title:k_romagna
#							root.primary_title = title:k_sardinia
#							root.primary_title = title:e_italy
#							root = { is_roman_emperor_primary_title_excluding_byzantium_trigger = yes }
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:romulus }
#				}
#				10 = { # Numa Pompilius
#					trigger = {
#						OR = { 
#							root.primary_title = title:k_italy
#							root.primary_title = title:k_romagna
#							root.primary_title = title:k_sardinia
#							root.primary_title = title:e_italy
#							root = { is_roman_emperor_primary_title_excluding_byzantium_trigger = yes }
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:numa }
#				}
#				10 = { # Belisarius
#					trigger = {
#						OR = { 
#							root.primary_title = title:k_italy
#							root.primary_title = title:k_romagna
#							root.primary_title = title:k_sardinia
#							root.primary_title = title:e_italy
#							root = { is_roman_emperor_primary_title_excluding_byzantium_trigger = yes }
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:belisarius }
#				}
#				10 = { # Ardashir
#					trigger = {
#						OR = { 
#							root.primary_title = title:k_persia
#							root.primary_title = title:e_persia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:ardashir }
#				}
#				10 = { # Pharaohs
#					trigger = {
#						root.primary_title = title:k_egypt
#						NOT = { religion = religion:islam_religion }
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:pharaohs }
#				}
#				10 = { # Visigothic Kings
#					trigger = {
#						OR = {
#							root.primary_title = title:k_castille
#							root.primary_title = title:k_leon
#							root.primary_title = title:d_galicia
#							root.primary_title = title:k_aragon
#							root.primary_title = title:k_navarra
#							root.primary_title = title:k_andalusia
#							root.primary_title = title:k_portugal
#							root.primary_title = title:e_spain
#						}
#						NOT = { religion = religion:islam_religion }
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:visigothic_kings }
#				}
#				10 = { # Abd al-Rahman
#					trigger = {
#						OR = {
#							root.primary_title = title:k_castille
#							root.primary_title = title:k_aragon
#							root.primary_title = title:k_navarra
#							root.primary_title = title:k_andalusia
#							root.primary_title = title:k_portugal
#							root.primary_title = title:e_spain
#						}
#						religion = religion:islam_religion
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:abd_al_rahman }
#				}
#				10 = { # Zenobia
#					trigger = {
#						OR = {
#							root.primary_title = title:k_syria
#							root.primary_title = title:k_mesopotamia
#						}
#						NOT = { religion = religion:islam_religion }
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:zenobia }
#				}
#				10 = { # King Solomon
#					trigger = {
#						root.primary_title.title_capital_county.title_province = {
#							geographical_region = ghw_region_ethiopia
#						}
#						root.faith.religion = religion:christianity_religion
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:solomon }
#				}
#				10 = { # Conan Meriadoc
#					trigger = {
#						root.culture = culture:breton
#						NOR = { # used to distinguish themselves from the French
#							root.primary_title = title:k_france
#							root.primary_title = title:e_france
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:conan_meriadoc }
#				}
#				10 = { # Pishdadian Dynasty
#					trigger = {
#						OR = {
#							root.primary_title = title:k_persia
#							root.primary_title = title:e_persia
#						}
#						OR = {
#							root.faith.religion = religion:islam_religion
#							root.faith.religion = religion:zoroastrianism_religion
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:pishdadian }
#				}
#				10 = { # Chościsko
#					trigger = {
#						OR = {
#							root.primary_title = title:k_poland
#							root.primary_title = title:d_lesser_poland
#							AND = {
#								root.primary_title.title_capital_county.title_province = {
#									geographical_region = ghw_region_poland
#								}
#								faith.religion = faith:slavic_pagan.religion
#							}
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:choscisko }
#				}
#				10 = { # Arsacids
#					trigger = {
#						OR = {
#							root.primary_title = title:k_armenia
#							root.primary_title = title:k_armenian_principality
#							root.primary_title = title:e_armenia
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:arsacids }
#				}
#				10 = { # King David
#					trigger = {
#						OR = {
#							root.primary_title = title:k_georgia
#							root.primary_title = title:d_georgia
#						}
#						root.faith.religion = religion:christianity_religion
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:david }
#				}
#				10 = { # Karkota Naga
#					trigger = {
#						root.primary_title = title:k_kashmir
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:naga }
#				}
#				10 = { # God Brahman
#					trigger = {
#						root.culture = culture:tamil
#						OR = { #Lands held by the Pallavas
#							root.primary_title = title:d_tondai_nadu
#							root.primary_title = title:c_kanchipuram
#							root.primary_title = title:k_tamilakam
#							root.primary_title = title:c_muluvagil
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:brahman }
#				}
#				10 = { # Odin
#					trigger = {
#						root.faith = faith:folketro
#						OR = {
#							root.primary_title = title:k_norway
#							root.primary_title = title:k_sweden
#							root.primary_title = title:d_iceland
#							root.primary_title = title:k_denmark
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:odin }
#				}
#				10 = { # Ragnar Lodbrok
#					trigger = {
#						OR = {
#							root.primary_title = title:k_norway
#							root.primary_title = title:k_sweden
#							root.primary_title = title:d_iceland
#							root.primary_title = title:k_denmark
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:ragnar }
#				}
#				10 = { # Brunhild
#					trigger = {
#						OR = {
#							root.primary_title = title:k_norway
#							root.primary_title = title:k_sweden
#							root.primary_title = title:d_iceland
#							root.primary_title = title:k_denmark
#						}
#					}
#					modifier = {
#						is_female = yes
#						factor = 5
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:brunhild }
#				}
#				10 = { # Eremon
#					trigger = {
#						OR = {
#							root.primary_title = title:k_ireland
#							root.primary_title = title:d_connacht
#							root.primary_title = title:d_ulster
#							root.primary_title = title:c_ailech
#							root.primary_title = title:c_oriel
#							root.primary_title = title:c_breifne
#							root.primary_title = title:c_athlone
#						}
#					}
#					modifier = {
#						OR = {
#							root.primary_title = title:d_connacht
#							root.primary_title = title:d_ulster
#							root.primary_title = title:c_ailech
#							root.primary_title = title:c_oriel
#							root.primary_title = title:c_breifne
#							root.primary_title = title:c_athlone
#						}
#						factor = 3
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:eremon }
#				}
#				10 = { # Eber
#					trigger = {
#						OR = {
#							root.primary_title = title:k_ireland                
#							root.primary_title = title:d_munster
#							root.primary_title = title:c_ossory             
#							root.primary_title = title:d_leinster               
#							root.primary_title = title:c_desmond
#						}
#					}
#					modifier = {
#						OR = {
#							root.primary_title = title:d_munster
#							root.primary_title = title:c_ossory             
#							root.primary_title = title:d_leinster               
#							root.primary_title = title:c_desmond
#						}
#						factor = 3
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:eber }
#				}
#				10 = { # Genghis Khan
#					trigger = {
#						root.culture = { has_cultural_pillar = heritage_mongolic }
#						current_date > 1228.1.1
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:genghis }
#				}
#				10 = { # Emperor Constantine
#					trigger = {
#						OR = {
#							root.primary_title = title:c_byzantion
#							root.primary_title = title:e_byzantium
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:constantine }
#				}
#				10 = { # Emperor Justinian
#					trigger = {
#						OR = {
#							root.primary_title = title:c_byzantion
#							root.primary_title = title:e_byzantium
#						}
#					}
#					set_variable = { name = mythical_ancestor_var value = flag:justinian }
#				}
				5 = { # Melusine
					trigger = {
						OR = {
							AND = {
								has_trait = eccentric
								capital_province = { geographical_region = world_europe_west }
							}
							#Used by the house of Anjou/Plantagenet
							root.primary_title = title:c_anjou
							root.primary_title = title:d_anjou
							#Started in Lusignan
							root.primary_title = title:d_poitou
						}
						current_date > 1060.1.1
					}
					modifier = { #The first Plantagenets go crazy over her
						OR = {
							root.primary_title = title:c_anjou
							root.primary_title = title:d_anjou
							root.primary_title = title:d_poitou
						}
						current_date > 1060.1.1
						current_date < 1200.1.1
						factor = 5
					}
					set_variable = { name = mythical_ancestor_var value = flag:melusine }
				}
				1 = { # Fallback, only triggered when necessary
					set_variable = { name = mythical_ancestor_var value = flag:fallback }
				}
			}
		}
	}
}

disburse_hunt_legend_seeds = {
	if = {
		limit = {
			is_ruler = yes
			has_dlc_feature = legends
			NOT = { has_game_rule = historical_legends_only }
		}
		if = {
			limit = {
				scope:activity.var:animal_type = flag:dragon
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = beast_slayer
				properties = {
					beast = flag:dragon
					location = root.location
				}
			}
		}
		else_if = {
			limit = {
				scope:activity.var:animal_type = flag:unicorn
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = beast_slayer
				properties = {
					beast = flag:unicorn
					location = root.location
				}
			}
		}
		else_if = {
			limit = {
				scope:activity.var:animal_type = flag:bear
			}
			random = {
				chance = 50
				create_legend_seed = {
					type = heroic
					quality = famed
					chronicle = beast_slayer
					properties = {
						beast = flag:bear
						location = root.location
					}
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					scope:activity.var:animal_type = flag:tiger
					scope:activity.var:animal_type = flag:lion
					scope:activity.var:animal_type = flag:leopard
				}
			}
			random = {
				chance = 30
				create_legend_seed = {
					type = heroic
					quality = famed
					chronicle = beast_slayer
					properties = {
						beast = flag:ferocious_beast
						location = root.location
					}
				}
			}
		}
	}
}

# Has to be run on the legend itself
change_legend_quality_effect = {
	# Firstly, do I exist and if not, set me
	if = {
		limit = {
			NOT = { exists = var:legend_quality_progress }
		}
		if = {
			limit = {
				$VALUE$ <= major_legend_quality_decrease
			}
			custom_tooltip = major_legend_quality_decrease_tt
			set_variable = {
				name = legend_quality_progress
				value = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ <= medium_legend_quality_decrease
			}
			custom_tooltip = medium_legend_quality_decrease_tt
			set_variable = {
				name = legend_quality_progress
				value = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ < 0
			}
			custom_tooltip = minor_legend_quality_decrease_tt
			set_variable = {
				name = legend_quality_progress
				value = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ >= major_legend_quality_increase
			}
			custom_tooltip = major_legend_quality_increase_tt
			set_variable = {
				name = legend_quality_progress
				value = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ >= medium_legend_quality_increase
			}
			custom_tooltip = medium_legend_quality_increase_tt
			set_variable = {
				name = legend_quality_progress
				value = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ > 0
			}
			custom_tooltip = minor_legend_quality_increase_tt
			set_variable = {
				name = legend_quality_progress
				value = $VALUE$
			}
		}
		else = {
			set_variable = {
				name = legend_quality_progress
				value = 0
			}
		}
	}
	# Otherwise, check if the progress is going to go over 100 and if so, cap at 100
	else_if = {
		limit = {
			exists = var:legend_quality_progress
			100 <= {
				value = var:legend_quality_progress
				add = $VALUE$
			}
		}
		set_variable = {
			name = legend_quality_progress
			value = 100
		}
	}
	# Otherwise, check if the progress is going to go below 0 and if so, cap at 0
	else_if = {
		limit = {
			exists = var:legend_quality_progress
			0 >= {
				value = var:legend_quality_progress
				add = $VALUE$
			}
		}
		set_variable = {
			name = legend_quality_progress
			value = 0
		}
	}
	# Otherwise, just apply the mathematics
	else = {
		if = {
			limit = {
				$VALUE$ <= major_legend_quality_decrease
			}
			custom_tooltip = major_legend_quality_decrease_tt
			change_variable = {
				name = legend_quality_progress
				add = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ <= medium_legend_quality_decrease
			}
			custom_tooltip = medium_legend_quality_decrease_tt
			change_variable = {
				name = legend_quality_progress
				add = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ < 0
			}
			custom_tooltip = minor_legend_quality_decrease_tt
			change_variable = {
				name = legend_quality_progress
				add = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ >= major_legend_quality_increase
			}
			custom_tooltip = major_legend_quality_increase_tt
			change_variable = {
				name = legend_quality_progress
				add = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ >= medium_legend_quality_increase
			}
			custom_tooltip = medium_legend_quality_increase_tt
			change_variable = {
				name = legend_quality_progress
				add = $VALUE$
			}
		}
		else_if = {
			limit = {
				$VALUE$ > 0
			}
			custom_tooltip = minor_legend_quality_increase_tt
			change_variable = {
				name = legend_quality_progress
				add = $VALUE$
			}
		}
	}
}

#Rewards for Famed Legends
legend_ending_rewards_famed_heroic_effect = {
	add_legitimacy = medium_legitimacy_gain
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_famed_legend_var
		set_variable = finished_heroic_legend_var
	}
}

legend_ending_rewards_famed_holy_effect = {
	add_legitimacy = medium_legitimacy_gain
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_famed_legend_var
		set_variable = finished_holy_legend_var
	}
}

legend_ending_rewards_famed_legitimizing_effect = {
	add_legitimacy = {
		value = medium_legitimacy_gain
		multiply = 2
	}
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_famed_legend_var
		set_variable = finished_legitimizing_legend_var
	}
}

#Rewards for Illustrious Legends
legend_ending_rewards_illustrious_heroic_effect = {
	add_legitimacy = major_legitimacy_gain
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_illustrious_legend_var
		set_variable = finished_heroic_legend_var
	}
}

legend_ending_rewards_illustrious_holy_effect = {
	add_legitimacy = major_legitimacy_gain
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_illustrious_legend_var
		set_variable = finished_holy_legend_var
	}
}

legend_ending_rewards_illustrious_legitimizing_effect = {
	add_legitimacy = {
		value = major_legitimacy_gain
		multiply = 2
	}
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_illustrious_legend_var
		set_variable = finished_legitimizing_legend_var
	}
}

#Rewards for Mythical Legends
legend_ending_rewards_mythical_heroic_effect = {
	add_legitimacy = massive_legitimacy_gain
	# Chance to become a Living Legend
	if = {
		limit = {
			dynasty ?= {
				ce1_heroic_track_perks >= 4
			}
		}
		add_trait = legend
	}
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_mythical_legend_var
		set_variable = finished_heroic_legend_var
	}
}

legend_ending_rewards_mythical_holy_effect = {
	add_legitimacy = massive_legitimacy_gain
	# Chance to become a Saint
	if = {
		limit = {
			dynasty ?= {
				ce1_heroic_track_perks >= 3
			}
		}
		add_trait = saint
		if = {
			limit = { NOT = { has_global_variable = num_of_saints_var } }
			set_global_variable = {
				name = num_of_saints_var
				value = 1
			}
		}
		else = {
			change_global_variable = {
				name = num_of_saints_var
				add = 1
			}
		}
		if = {
			limit = { is_ai = no }
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_canonized_achievement_unlocked
				VALUE = yes
			}
		}
	}
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_mythical_legend_var
		set_variable = finished_holy_legend_var
	}
}

legend_ending_rewards_mythical_legitimizing_effect = {
	add_legitimacy = {
		value = massive_legitimacy_gain
		multiply = 2
	}
	if = {
		limit = { exists = promoted_legend }
		set_variable = finished_mythical_legend_var
		set_variable = finished_legitimizing_legend_var
	}
}

#We clean out the notification variables
remove_legend_notification_variables_effect = {
	every_player = {
		limit = {
			exists = var:legend_alerted_var
			var:legend_alerted_var = promoted_legend
		}
		remove_variable = legend_alerted_var
	}
}

#Rewards for all Heroic Legends
legend_ending_rewards_heroic_effect = {
	# Legitimacy gain based on Legend Protagonist
	disburse_legend_protagonist_legitimacy_effect = yes
	# Available decision
	custom_tooltip = legend_ending_decision_tt
	hidden_effect = {
		create_character_memory = {
			type = ce1_completed_my_heroic_legend
			participants = {
				owner = root
				protagonist = scope:legend_protagonist
			}
		}
	}
}

#Rewards for all Legitimizing Legends
legend_ending_rewards_legitimizing_effect = {
	# Legitimacy gain based on Legend Protagonist
	disburse_legend_protagonist_legitimacy_effect = yes
	# We let you diverge if it's an Ancient People legend
	if = {
		limit = {
			exists = scope:legendary_culture
			has_dlc_feature = diverge_culture # Don't mislead players who don't have access to divergence
		}
		custom_tooltip = { 
			text = can_select_legendary_culture_pillars_tt
			set_variable = {
				name = legendary_culture
				value = scope:legendary_culture
			}
		}
	}
	if = { #Roman Heritage legend without a Roman Empire
		limit = {
			scope:title_claim = title:h_roman_empire
			NOT = {
				exists = global_var:flag_restored_roman_empire
			}
		}
		title:d_latium.empire = { #Italy
			add_to_list = roman_heritage_claims
		}
		title:d_tunis.kingdom = { #North Africa
			add_to_list = roman_heritage_claims
		}
		title:d_thrace.empire = { #Byz
			add_to_list = roman_heritage_claims 
		}
		title:d_alexandria.kingdom = { #Egypt
			add_to_list = roman_heritage_claims 
		}
		title:d_antioch.kingdom = { #Syria
			add_to_list = roman_heritage_claims 
		}
		title:d_palestine.kingdom = { #Palestine
			add_to_list = roman_heritage_claims 
		}
		custom_tooltip = {
			text = legend_title_does_not_exist_sub_realm_claims_given_tt
		}
		hidden_effect = {
			every_in_list = {
				list = roman_heritage_claims

				if = {
					limit = {
						exists = this.holder
						this.holder != root
					}
					root = {
						add_pressed_claim = prev
						add_to_variable_list = {
							name = legendary_claims
							target = prev
						}
					}
				}
				else_if = {
					limit = {
						this.tier > tier_county
					}
					hidden_effect = {
						legend_distribute_claims_in_de_jure = yes
					}
				}
			}
		}

	}
	else_if = {
		limit = {
			exists = scope:title_claim.holder
			scope:title_claim.holder != root
		}
		add_pressed_claim = scope:title_claim
		add_to_variable_list = {
			name = legendary_claims
			target = scope:title_claim
		}
	}
	else_if = {
		limit = {
			scope:title_claim.tier > tier_county
		}
		custom_tooltip = {
			text = legend_title_does_not_exist_sub_realm_claims_given_tt
		}
		hidden_effect = {
			scope:title_claim = {
				legend_distribute_claims_in_de_jure = yes
			}
		}
	}
	# Available decision
	custom_tooltip = legend_ending_decision_tt
	promoted_legend ?= { set_variable = finished_legend_var }
	hidden_effect = {
		create_character_memory = {
			type = ce1_completed_my_legitimizing_legend
			participants = {
				owner = root
				protagonist = scope:legend_protagonist
			}
		}
	}
}

legend_distribute_claims_in_de_jure = {
	every_direct_de_jure_vassal_title = {
		if = {
			limit = {
				exists = holder
			}
			root = {
				add_pressed_claim = prev
				add_to_variable_list = {
					name = legendary_claims
					target = prev
				}
			}
		}
		else_if = {
			limit = {
				this.tier > tier_county
			}
			every_direct_de_jure_vassal_title = {
				if = {
					limit = {
						exists = holder
					}
					root = {
						add_unpressed_claim = prev
						add_to_variable_list = {
							name = legendary_claims
							target = prev
						}
					}
				}
				else_if = {
					limit = {
						this.tier > tier_county
					}
					every_direct_de_jure_vassal_title = {
						if = {
							limit = {
								exists = holder
							}
							root = {
								add_unpressed_claim = prev
								add_to_variable_list = {
									name = legendary_claims
									target = prev
								}
							}
						}
					}
				}
			}
		}
	}	
}

#Rewards for all Holy Legends
legend_ending_rewards_holy_effect = {
	# Legitimacy gain based on Legend Protagonist
	disburse_legend_protagonist_legitimacy_effect = yes
	# Available decision
	custom_tooltip = legend_ending_decision_tt
	promoted_legend ?= { set_variable = finished_legend_var }
	hidden_effect = {
		create_character_memory = {
			type = ce1_completed_my_holy_legend
			participants = {
				owner = root
				protagonist = scope:legend_protagonist
			}
		}
	}
}

# Achievements for legends
legend_ending_achievements_effect = {
	if = {
		limit = {
			exists = promoted_legend
		}
		if = { # If you have the legend of Scota
			limit = {
				is_ai = no
				promoted_legend ?= {
					has_legend_chronicle = ancient_people
					legend_property:culture ?= culture:ancient_egyptian
					legend_property:title ?= title:k_scotland
					legend_property:original_region ?= geographical_region:ghw_region_egypt_et_al
				}
			}
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_the_pharaoh_islands_achievement_unlocked
				VALUE = yes
			}
		}
		if = {
			limit = { is_ai = no }
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_legendary_achievement_unlocked
				VALUE = yes
			}
		}
		if = { # If you finish your ancestor's legend
			limit = {
				is_ai = no
				has_variable = ce1_neverending_story_achievement_possible
				promoted_legend = { has_variable = ce1_neverending_story_achievement_possible_legend }
			}
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_neverending_story_achievement_unlocked
				VALUE = yes
			}
		}
		if = { # If you finish a mythical legend as a count
			limit = {
				is_ai = no
				highest_held_title_tier = tier_county
				promoted_legend = { legend_quality = mythical }
			}
			add_achievement_global_variable_effect = {
				VARIABLE = ce1_local_legend_achievement_unlocked
				VALUE = yes
			}
		}
	}
}

get_appropriate_ancient_people = {
	random_list = {
		999 = {
			trigger = {
				religion = { is_in_family = rf_abrahamic }
				NOT = { culture = culture:hebrew }
			}
			set_variable = {
				name = original_region
				value = geographical_region:israel_region
				years = 1
			}
			culture:hebrew = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						OR = {
							culture_overlaps_geographical_region = world_europe
							culture_overlaps_geographical_region = world_africa
							culture_overlaps_geographical_region = world_asia_minor
						}
					}
					culture = culture:egyptian
					culture = culture:scottish
				}
				NOT = { culture = culture:ancient_egyptian }
			}
			set_variable = {
				name = original_region
				value = geographical_region:ghw_region_egypt_et_al
				years = 1
			}
			culture:ancient_egyptian = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						OR = {
							culture_overlaps_geographical_region = world_steppe
							culture_overlaps_geographical_region = custom_carpathia
						}
					}
					culture = { has_cultural_pillar = heritage_turkic }
					culture = { has_cultural_pillar = heritage_mongolic }
					culture = { has_cultural_pillar = heritage_magyar }
				}
				NOT = { culture = culture:hunnic }
			}
			set_variable = {
				name = original_region
				value = geographical_region:world_steppe_west
				years = 1
			}
			culture:hunnic = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						culture_overlaps_geographical_region = world_europe
					}
					culture = { has_cultural_pillar = heritage_iberian }
					culture = { has_cultural_pillar = heritage_north_germanic }
				}
				NOT = { culture = culture:gothic }
			}
			set_variable = {
				name = original_region
				value = geographical_region:world_steppe_west
				years = 1
			}
			culture:gothic = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						OR = {
							culture_overlaps_geographical_region = world_asia_minor
							culture_overlaps_geographical_region = world_middle_east
						}
					}
				}
				NOT = { culture = culture:persian }
			}
			set_variable = {
				name = original_region
				value = geographical_region:world_middle_east_persia
				years = 1
			}
			culture:persian = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						OR = {
							culture_overlaps_geographical_region = world_europe
							culture_overlaps_geographical_region = world_asia_minor
							culture_overlaps_geographical_region = world_middle_east
							culture_overlaps_geographical_region = world_africa_north
						}
					}
				}
				NOT = { culture = culture:trojan }
			}
			set_variable = {
				name = original_region
				value = geographical_region:troy_region
				years = 1
			}
			culture:trojan = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						OR = {
							culture_overlaps_geographical_region = world_europe
							culture_overlaps_geographical_region = world_asia_minor
							culture_overlaps_geographical_region = world_middle_east
							culture_overlaps_geographical_region = world_africa_north
							culture_overlaps_geographical_region = world_india
							culture_overlaps_geographical_region = world_steppe_tarim
						}
					}
				}
				NOT = { culture = culture:macedonian }
			}
			set_variable = {
				name = original_region
				value = geographical_region:world_europe_south_east
				years = 1
			}
			culture:macedonian = { save_scope_as = ancient_culture }
		}
		999 = {
			trigger = {
				OR = {
					root.culture = {
						culture_overlaps_geographical_region = custom_roman_full_borders
					}
					religion = religion:theomach_religion
					religion = {
						AND = {
							is_in_family = rf_abrahamic
							NOT = { this = religion:judaism_religion }
						}
					}
				}
				NOT = { culture = culture:roman }
			}
			set_variable = {
				name = original_region
				value = geographical_region:custom_roman_full_borders
				years = 1
			}
			culture:roman = { save_scope_as = ancient_culture }
		}
		1 = { # Roman (fallback, placeholder)
			set_variable = {
				name = original_region
				value = geographical_region:custom_roman_full_borders
				years = 1
			}
			culture:roman = { save_scope_as = ancient_culture }
		}
	}
}

get_appropriate_exotic_location = {
	random_list = {
		999 = { # Atlantis, must care about Greek stuff
			trigger = {
				OR = {
					religion = { is_in_family = rf_abrahamic }
					religion = religion:theomach_religion
					culture = { has_cultural_pillar = heritage_byzantine }
					culture = { has_cultural_pillar = heritage_ancient_greek }
					culture = { has_cultural_pillar = heritage_latin }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:atlantis
			}
		}
		999 = { # Olympus, must care about Greek stuff
			trigger = {
				OR = {
					religion = { is_in_family = rf_abrahamic }
					religion = religion:theomach_religion
					culture = { has_cultural_pillar = heritage_byzantine }
					culture = { has_cultural_pillar = heritage_ancient_greek }
					culture = { has_cultural_pillar = heritage_latin }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:olympus
			}
		}
		999 = { # India, must be faraway
			trigger = {
				NOT = {
					any_character_to_title_neighboring_and_across_water_county = {
						title_province = { geographical_region = world_india }
					}
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:hindustan
			}
		}
		999 = { # Vinland, must be into vikings and adventures and all that
			trigger = {
				OR = {
					religion = religion:folkgerman_religion
					culture = { has_cultural_pillar = heritage_north_germanic }
					culture = { has_innovation = innovation_varangian_adventurers }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:vinland
			}
		}
		999 = { # Sunset invasion memes
			trigger = {
				OR = {
					religion = religion:folkgerman_religion
					culture = { has_cultural_pillar = heritage_north_germanic }
					culture = { has_innovation = innovation_varangian_adventurers }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:sunset_empire
			}
		}
		999 = { # Kingdom of Prester John, must be a Christian who doesn't know about Copts
			trigger = {
				religion = religion:christianity_religion
				NOT = {
					any_character_to_title_neighboring_and_across_water_county = {
						holder = { faith = faith:coptic }
					}
				}
				NOT = { # Prester John isn't exciting if Egypt is Christian
					title:k_egypt = {
						holder ?= { religion = religion:christianity_religion }
					}
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:prester_john_kingdom
			}
		}
		999 = { # Albion, Christians in Jerusalem were way way into Arthurian myth
			trigger = {
				religion = religion:christianity_religion
				capital_county = {
					title_province = { geographical_region = world_middle_east }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:albion
			}
		}
		999 = { # Himavanta, legendary forest in the himalayas
			trigger = {
				OR = {
					capital_county = {
						title_province = {
							OR = {
								geographical_region = world_india
								geographical_region = world_tibet
								geographical_region = world_burma
								geographical_region = world_asia_china
							}
						}
					}
					culture = { culture_overlaps_geographical_region = world_india }
					culture = { culture_overlaps_geographical_region = world_tibet }
					culture = { culture_overlaps_geographical_region = world_burma }
					culture = { culture_overlaps_geographical_region = world_asia_china }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:himavanta
			}
		}
		999 = { # Mahishmati, lost city
			trigger = {
				OR = {
					capital_county = {
						title_province = { geographical_region = world_india }
					}
					culture = { culture_overlaps_geographical_region = world_india }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:mahishmati
			}
		}
		999 = { # Kumari Kandam, lost continent
			trigger = {
				culture = {
					has_cultural_pillar = heritage_dravidian
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:kumari_kandam
			}
		}
		999 = { # Beyul, hidden valleys
			trigger = {
				OR = {
					capital_county.title_province = { 
						OR = {
							geographical_region = world_tibet
							geographical_region = world_asia_china
						}
					}
					culture = { culture_overlaps_geographical_region = world_tibet }
					culture = { culture_overlaps_geographical_region = world_asia_china }
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:beyul
			}
		}
		999 = { # Bald Mountain, slavic witchy mountain
			trigger = {
				OR = {
					culture_has_slavic_heritage_pillar_trigger = yes
					religion = religion:slavic_religion
				}
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:bald_mountain
			}
		}
		999 = { # Irum, lost city in the Quran
			trigger = {
				religion = religion:islam_religion
			}
			save_scope_value_as = {
				name = exotic_location
				value = flag:irum
			}
		}
		1 = { # China (fallback)
			save_scope_value_as = {
				name = exotic_location
				value = flag:china
			}
		}
	}
}

# Generates a legend seed that just points to a loc key by combining FAITH with GOD (example, FAITH = germanic + GOD = high_god will result in germanic_high_god_name and localise to "Odin")
generate_godly_descent_seed_support_effect = {
	create_legend_seed = {
		type = holy
		quality = famed
		chronicle = godly_descent
		properties = {
			god = flag:$FAITH$_$GOD$_name
			title = root.primary_title
		}
	}
}

# Runs the above effect based on an actual RELIGION that you feed into it, for example feeding Root.Religion into RELIGION will cause it to generate your religion's god loc keys
generate_religion_descent_effect = {
#	if = {
#		limit = {
#			$RELIGION$ = religion:akom_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = akom
#			GOD = $GOD$
#		}
#	}
	else_if = {
		limit = {
			$RELIGION$ = religion:baltic_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = baltic
			GOD = $GOD$
		}
	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:basque_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = basque
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:bon_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = bon
#			GOD = $GOD$
#		}
#	}
	else_if = {
		limit = {
			$RELIGION$ = religion:buddhism_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = buddhism
			GOD = $GOD$
		}
	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:tani_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = tani
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:dualism_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = dualism
#			GOD = $GOD$
#		}
#	}
	else_if = {
		limit = {
			$RELIGION$ = religion:finnic_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = finnic
			GOD = $GOD$
		}
	}
	else_if = {
		limit = {
			$RELIGION$ =  religion:folkgerman_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = folkgerman
			GOD = $GOD$
		}
	}
	else_if = {
		limit = {
			$RELIGION$ = religion:theomach_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = theomach
			GOD = $GOD$
		}
	}
	else_if = {
		limit = {
			$RELIGION$ = religion:hinduism_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = hinduism
			GOD = $GOD$
		}
	}
	else_if = {
		limit = {
			$RELIGION$ = religion:jainism_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = jainism
			GOD = $GOD$
		}
	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:kushitism_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = kushitism
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:magyar_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = magyar
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:mundhum_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = mundhumism
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:north_african_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = acham
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:qiangic_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = qiangic
#			GOD = $GOD$
#		}
#	}
	else_if = {
		limit = {
			$RELIGION$ = religion:siberian_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = siberian
			GOD = $GOD$
		}
	}
	else_if = {
		limit = {
			$RELIGION$ = religion:slavic_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = slavic
			GOD = $GOD$
		}
	}
	else_if = {
		limit = {
			$RELIGION$ = religion:taoism_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = taoism
			GOD = $GOD$
		}
	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:tengrism_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = tengrism
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:waaqism_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = waaqism
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:west_african_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = west_african
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:west_african_bori_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = west_african_bori
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:west_african_orisha_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = west_african_orisha
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:west_african_roog_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = west_african_roog
#			GOD = $GOD$
#		}
#	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:yazidi_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = yazidi
#			GOD = $GOD$
#		}
#	}
	else_if = {
		limit = {
			$RELIGION$ = religion:zoroastrianism_religion
		}
		generate_godly_descent_seed_support_effect = {
			FAITH = zoroastrianism
			GOD = $GOD$
		}
	}
#	else_if = {
#		limit = {
#			$RELIGION$ = religion:zunism_religion
#		}
#		generate_godly_descent_seed_support_effect = {
#			FAITH = zunism
#			GOD = $GOD$
#		}
#	}
#	else = { # Generic Pagan, fallback
#		generate_godly_descent_seed_support_effect = {
#			FAITH = paganism
#			GOD = $GOD$
#		}
#	}
}

# Mythical Legends guaranteed access to Legendary Buildings

give_legendary_building_mythical_holy_tooltip_effect = {
	if = {
		limit = {
			OR = {
				root = {
					locale_valid_for_legendary_buildings = yes
				}
				capital_province = { has_special_building_slot = no }
				any_sub_realm_barony = {
					title_province = { has_special_building_slot = no }
				}
			}
		}
		give_legendary_building_location_holy_effect = yes
	}
	set_variable = {
		name = legendary_reward_location
		value = scope:legendary_locale
	}
	set_variable = {
		name = legendary_figure
		value = scope:protagonist
	}
}

give_legendary_building_mythical_heroic_tooltip_effect = {
	if = {
		limit = {
			OR = {
				root = {
					locale_valid_for_legendary_buildings = yes
				}
				capital_province = { has_special_building_slot = no }
				any_sub_realm_barony = {
					title_province = { has_special_building_slot = no }
				}
			}
		}
		give_legendary_building_location_heroic_effect = yes
	}
	set_variable = {
		name = legendary_reward_location
		value = scope:legendary_locale
	}
	set_variable = {
		name = legendary_figure
		value = scope:protagonist
	}
}

give_legendary_building_mythical_legitimizing_tooltip_effect = {
	if = {
		limit = {
			OR = {
				root = {
					locale_valid_for_legendary_buildings = yes
				}
				capital_province = { has_special_building_slot = no }
				any_sub_realm_barony = {
					title_province = { has_special_building_slot = no }
				}
			}
		}
		give_legendary_building_location_legitimizing_effect = yes
	}
	set_variable = {
		name = legendary_reward_location
		value = scope:legendary_locale
	}
	set_variable = {
		name = legendary_figure
		value = scope:protagonist
	}
}

# Famed/Illustrious have a 50% chance

give_legendary_building_holy_tooltip_effect = { #50% chance
	# Chance to get legendary building
	if = {
		limit = {
			OR = {
				root = {
					locale_valid_for_legendary_buildings = yes
				}
				capital_province = { has_special_building_slot = no }
				any_sub_realm_barony = {
					title_province = { has_special_building_slot = no }
				}
			}
		}
		random = {
			chance = {
				value = 50
			}
			give_legendary_building_location_holy_effect = yes
		}
	}
	set_variable = {
		name = legendary_reward_location
		value = scope:legendary_locale
	}
	set_variable = {
		name = legendary_figure
		value = scope:protagonist
	}
}

give_legendary_building_heroic_tooltip_effect = { #50% chance
	# Chance to get legendary building
	if = {
		limit = {
			OR = {
				root = {
					locale_valid_for_legendary_buildings = yes
				}
				capital_province = { has_special_building_slot = no }
				any_sub_realm_barony = {
					title_province = { has_special_building_slot = no }
				}
			}
		}
		random = {
			chance = {
				value = 50
			}
			give_legendary_building_location_heroic_effect = yes
			set_variable = {
				name = legendary_reward_location
				value = scope:legendary_locale
			}
			set_variable = {
				name = legendary_figure
				value = scope:protagonist
			}
		}
	}
}

give_legendary_building_legitimizing_tooltip_effect = { #50% chance
	# Chance to get legendary building
	if = {
		limit = {
			OR = {
				root = {
					locale_valid_for_legendary_buildings = yes
				}
				capital_province = { has_special_building_slot = no }
				any_sub_realm_barony = {
					title_province = { has_special_building_slot = no }
				}
			}
		}
		random = {
			chance = {
				value = 50
			}
			give_legendary_building_location_legitimizing_effect = yes
		}
	}
	set_variable = {
		name = legendary_reward_location
		value = scope:legendary_locale
	}
	set_variable = {
		name = legendary_figure
		value = scope:protagonist
	}
}

give_legendary_building_location_standard_effect = {
	if = { # Prioritize the location of the legend if possible
		limit = {
			root = {
				locale_valid_for_legendary_buildings = yes
			}
		}
		scope:location = { save_scope_as = legendary_locale }
	}
	else_if = { # Otherwise, get the capital
		limit = {
			capital_province = { has_special_building_slot = no }
		}
		capital_province = { save_scope_as = legendary_locale }
	}
	else_if = { # Otherwise, prioritize a title you hold directly
		limit = {
			any_held_title = {
				title_tier = county
				title_province = { has_special_building_slot = no }
			}
		}
		random_held_title = {
			title_tier = county
			limit = {
				title_province = { has_special_building_slot = no }
			}
			title_province = { save_scope_as = legendary_locale }
		}
	}
	else_if = { #Otherwise, prioritize a barony that you hold directly
		limit = {
			any_held_title = {
				title_tier = barony
				title_province = {
					has_special_building_slot = no
				}
			}
		}
		random_held_title = {
			title_tier = barony
			limit = {
				title_province = {
					has_special_building_slot = no
				}
			}
			title_province = {
				save_scope_as = legendary_locale
			}
		}

	}
	else_if = { # Otherwise, prioritize a barony within a title you hold
		limit = {
			any_held_title = {
				title_tier = county
				any_county_province = { has_special_building_slot = no }
			}
		}
		random_held_title = {
			title_tier = county
			limit = {
				any_county_province = { has_special_building_slot = no }
			}
			random_county_province = {
				limit = {
					has_special_building_slot = no
				}
				save_scope_as = legendary_locale
			}
		}
	}
	else_if = { # Find a feudal barony...
		limit = {
			any_sub_realm_barony = {
				title_province = {
					has_special_building_slot = no
					has_holding_type = castle_holding
				}
			}
		}
		random_sub_realm_barony = {
			limit = {
				title_province = {
					has_special_building_slot = no
					has_holding_type = castle_holding
				}
			}
			title_province = { save_scope_as = legendary_locale }
		}
	}
	else_if = { # Find a province with a holding
		limit = {
			any_sub_realm_barony = {
				title_province = {
					has_special_building_slot = no
					has_holding = yes
				}
			}
		}
		random_sub_realm_barony = {
			limit = {
				title_province = {
					has_special_building_slot = no
					has_holding = yes
				}
			}
			title_province = { save_scope_as = legendary_locale }
		}
	}
	else = { # Final fallback, just any barony in your realm
		random_sub_realm_barony = {
			title_province = { save_scope_as = legendary_locale }
		}
	}
	if = {
		limit = {
			OR = {
				exists = scope:beast
				has_trait_xp = {
					trait = lifestyle_hunter
					track = hunter
					value >= 62
				}
				has_trait_xp = {
					trait = lifestyle_hunter
					track = falconer
					value >= 62
				}
				culture ?= {
					has_cultural_parameter = always_unlock_legendary_hunting_lodge
				}
			}
		}
		custom_tooltip = {
			text = can_build_legendary_hunting_lodge_tt
			add_character_flag = legendary_hunting_lodge_flag
		}
	}
	if = {
		limit = {
			dynasty ?= { ce1_heroic_track_perks >= 5 }
		}
		custom_tooltip = {
			text = can_build_legendary_watchtower_tt
			add_character_flag = legendary_watchtower_flag
		}
	}
}

give_legendary_building_location_holy_effect = {
	give_legendary_building_location_standard_effect = yes
	custom_tooltip = {
		text = can_build_legendary_shrine_tt
		add_character_flag = legendary_shrine_flag
	}
}

give_legendary_building_location_heroic_effect = {
	give_legendary_building_location_standard_effect = yes
	custom_tooltip = {
		text = can_build_legendary_statue_tt
		add_character_flag = legendary_statue_flag
	}
}

give_legendary_building_location_legitimizing_effect = {
	give_legendary_building_location_standard_effect = yes
	custom_tooltip = {
		text = can_build_legendary_palace_tt
		add_character_flag = legendary_palace_flag
	}
}

legend_seed_new_title_effect = {
	if = {
		limit = { has_dlc_feature = legends }
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = new_title
			properties = {
				title = root.primary_title
				founder = root
			}
		}
	}
}

legend_seed_great_deed_dynasty_effect = {
	if = {
		limit = { has_dlc_feature = legends }
		if = {
			limit = {
				highest_held_title_tier < tier_hegemony
			}
			create_legend_seed = {
				type = legitimizing
				quality = famed
				chronicle = great_deed_dynasty
				properties = {
					dynasty = root.dynasty
					founder = root
					title = root.primary_title.empire
				}
			}
		}
		else = {
			create_legend_seed = {
				type = legitimizing
				quality = famed
				chronicle = great_deed_dynasty
				properties = {
					dynasty = root.dynasty
					founder = root
					title = root.primary_title
				}
			}
		}
	}
}

legend_seed_great_deed_region_effect = {
	if = {
		limit = { has_dlc_feature = legends }
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = great_deed_region
			properties = {
				legend_region = $REGION$
				founder = root
			}
		}
	}
}

legend_seed_great_deed_title_effect = {
	if = {
		limit = { has_dlc_feature = legends }
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = great_deed_title
			properties = {
				title = $TITLE$
				founder = root
			}
		}
	}
}

legend_seed_great_deed_faith_effect = {
	if = {
		limit = { has_dlc_feature = legends }
		create_legend_seed = {
			type = holy
			quality = famed
			chronicle = great_deed_faith
			properties = {
				founder = root
				antagonist = $ANTAGONIST$
				old_faith = $OLD_FAITH$
				new_faith = root.faith
			}
		}
	}
}

legend_seed_struggle_ended_effect = {
	if = {
		limit = { has_dlc_feature = legends }
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = struggle_ended
			properties = {
				founder = root
				culture = root.culture
				struggle = $STRUGGLE$
				struggle_ending = $STRUGGLE_ENDING$
			}
		}
	}
}

mark_historical_legend_as_done = {
	if = { # Dynastic legacy
		limit = {
			legend_property:founder ?= legend_owner.house.house_founder
			legend_property:dynasty ?= legend_owner.dynasty
			legend_property:title ?= legend_owner.primary_title
		}
		legend_owner.dynasty = {
			set_variable = {
				name = dynasty_legend_used
				value = yes
			}
		}
	}
#	if = { # Arthur
#		limit = {
#			legend_property:ancestor ?= character:159835
#			legend_property:title ?= title:k_england
#		}
#		set_global_variable = {
#			name = king_arthur_legend
#			value = yes
#		}
#	}
#	else_if = { # Carolingian
#		limit = {
#			legend_property:ancestor ?= character:6392
#			legend_property:title ?= title:e_france
#		}
#		set_global_variable = {
#			name = carolingian_legend
#			value = yes
#		}
#	}
#	else_if = { # the_wheelwright
#		limit = {
#			legend_property:ancestor ?= character:82233
#		}
#		set_global_variable = {
#			name = the_wheelwright_legend
#			value = yes
#		}
#	}
#	else_if = { # cadell
#		limit = {
#			legend_property:ancestor ?= character:159058
#		}
#		set_global_variable = {
#			name = cadell_legend
#			value = yes
#		}
#	}
#	else_if = { # sons_of_rurik
#		limit = {
#			legend_property:ancestor ?= character:40605
#		}
#		set_global_variable = {
#			name = sons_of_rurik_legend
#			value = yes
#		}
#	}
#	else_if = { # el_cid
#		limit = {
#			legend_property:ancestor ?= character:107590
#		}
#		set_global_variable = {
#			name = el_cid_legend
#			value = yes
#		}
#	}
#	else_if = { # peasant_emperor
#		limit = {
#			legend_property:ancestor ?= character:1700
#		}
#		set_global_variable = {
#			name = peasant_emperor_legend
#			value = yes
#		}
#	}
#	else_if = { # premysl
#		limit = {
#			legend_property:ancestor ?= character:168345
#		}
#		set_global_variable = {
#			name = premysl_legend
#			value = yes
#		}
#	}
#	else_if = { # borjigin
#		limit = {
#			legend_property:ancestor ?= character:303250
#		}
#		set_global_variable = {
#			name = borjigin_legend
#			value = yes
#		}
#	}
#	else_if = { # the_red_hand
#		limit = {
#			legend_property:ancestor ?= character:7255
#		}
#		set_global_variable = {
#			name = the_red_hand_legend
#			value = yes
#		}
#	}
#	else_if = { # yazdagird_iii
#		limit = {
#			legend_property:ancestor ?= character:180637
#		}
#		set_global_variable = {
#			name = yazdagird_iii_legend
#			value = yes
#		}
#	}
#	else_if = { # bahram_gur
#		limit = {
#			legend_property:ancestor ?= character:180645
#		}
#		set_global_variable = {
#			name = bahram_gur_legend
#			value = yes
#		}
#	}
#	else_if = { # saman_khudah
#		limit = {
#			legend_property:ancestor ?= character:188637
#		}
#		set_global_variable = {
#			name = saman_khudah_legend
#			value = yes
#		}
#	}
#	else_if = { # saman_khudah
#		limit = {
#			legend_property:ancestor ?= character:163109
#		}
#		set_global_variable = {
#			name = ragnarr_legend
#			value = yes
#		}
#	}
#	else_if = { 
#		limit = {
#			legend_property:ancestor_flag ?= flag:vercingetorix
#		}
#		set_global_variable = {
#			name = vercingetorix_legend
#			value = yes
#		}
#	}
#	else_if = { 
#		limit = {
#			legend_property:ancestor_flag ?= flag:afrasiyab
#		}
#		set_global_variable = {
#			name = afrasiyab_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:hebrew
#			legend_property:title ?= title:k_georgia
#			legend_property:original_region ?= geographical_region:world_middle_east_jerusalem
#		}
#		set_global_variable = {
#			name = sons_of_david_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:ancestor_flag ?= flag:magi
#		}
#		set_global_variable = {
#			name = magi_descendant_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:gothic
#			legend_property:title ?= title:e_spain
#			legend_property:original_region ?= geographical_region:world_steppe_west
#		}
#		set_global_variable = {
#			name = gothic_kings_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:trojan
#			legend_property:title ?= title:e_britannia
#			legend_property:original_region ?= geographical_region:troy_region
#		}
#		set_global_variable = {
#			name = new_troy_london_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:trojan
#			legend_property:title ?= title:c_cornwall
#			legend_property:original_region ?= geographical_region:troy_region
#		}
#		set_global_variable = {
#			name = corineus_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:ancient_egyptian
#			legend_property:title ?= title:k_scotland
#			legend_property:original_region ?= geographical_region:ghw_region_egypt_et_al
#		}
#		set_global_variable = {
#			name = scota_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:hebrew
#			legend_property:title ?= title:e_abyssinia
#			legend_property:original_region ?= geographical_region:ghw_region_jerusalem
#		}
#		set_global_variable = {
#			name = menelik_i_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:hebrew
#			legend_property:title ?= title:e_abyssinia
#			legend_property:original_region ?= geographical_region:israel_region
#		}
#		set_global_variable = {
#			name = kings_of_semien_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:roman
#			legend_property:original_region ?= geographical_region:custom_roman_full_borders
#		}
#		set_global_variable = {
#			name = roman_heritage_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:culture ?= culture:hunnic
#			legend_property:title ?= title:e_caspian-pontic_steppe
#			legend_property:original_region ?= geographical_region:world_steppe_west
#		}
#		set_global_variable = {
#			name = hunnic_heritage_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:ancestor ?= character:7627
#		}
#		set_global_variable = {
#			name = alfred_of_wessex_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:ancestor ?= character:168130
#		}
#		set_global_variable = {
#			name = william_gellones_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:ancestor ?= character:99
#		}
#		set_global_variable = {
#			name = edward_the_martyr_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:ancestor_flag ?= flag:noah
#		}
#		set_global_variable = {
#			name = sceafa_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:ancestor_flag ?= flag:shibi_chakravarti
#		}
#		set_global_variable = {
#			name = shibi_chakravarti_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:beast ?= flag:naga
#		}
#		set_global_variable = {
#			name = naga_descent_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:beast ?= flag:dragon
#		}
#		set_global_variable = {
#			name = pyusawhti_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:god ?= flag:germanic_high_god_name
#			legend_property:title ?= title:k_norway
#		}
#		set_global_variable = {
#			name = hieros_gamos_skirnismal_legend
#			value = yes
#		}
#	}
#	else_if = {
#		limit = {
#			legend_property:god ?= flag:hinduism_high_god_name
#			legend_property:title ?= title:e_deccan
#		}
#		set_global_variable = {
#			name = descendants_of_brahman_legend
#			value = yes
#		}
#	}
}

#legend_seed_struggle_ending_effect = {
#	create_legend_seed = {
#		type = heroic
#		quality = famed
#		chronicle = struggle_conclusion
#		properties = {
#			struggle_ender = $ENDER$
#			struggle = flag:$STRUGGLE$
#		}
#	}
#}
#
balancing_legend_global_var_effect = {
	if = {
		limit = {
			NOT = { has_global_variable = legend_count_var }
		}
		set_global_variable = {
			name = legend_count_var
			value = 1
		}
	}
	else = {
		change_global_variable = {
			name = legend_count_var
			add = 1
		}
	}
}

gain_heroic_legend_seed_tooltip_effect = {
	if = {
		limit = {
			has_dlc_feature = legends
		}
		custom_tooltip = gain_heroic_legend_seed.tt
	} 
}

gain_holy_legend_seed_tooltip_effect = {
	if = {
		limit = {
			has_dlc_feature = legends
		}
		custom_tooltip = gain_holy_legend_seed.tt
	} 
}

gain_legitimizing_legend_seed_tooltip_effect = {
	if = {
		limit = {
			has_dlc_feature = legends
		}
		custom_tooltip = gain_legitimizing_legend_seed.tt
	} 
}

send_interface_message_legend_notification_effect = {
	county = { save_scope_as = spread_county }
	scope:legend = { save_scope_as = spread_legend }
	county.holder = {
		set_variable = {
			name = legend_alerted_var
			value = scope:legend
		}
		send_interface_message = {
			type = event_generic_good_text
			title = legend_spread_notification_title
			desc = legend_spread_notification_desc

			right_icon = scope:spread_county
		}
	}
}

send_interface_toast_legend_notification_effect = {
	county = { save_scope_as = spread_county }
	scope:legend = { save_scope_as = spread_legend }
	county.holder = {
		set_variable = {
			name = legend_alerted_var
			value = scope:legend
		}
		send_interface_toast = {
			title = legend_spread_notification_title
			custom_tooltip = legend_spread_notification_desc
			left_icon = scope:spread_county
		}
	}
}

create_swan_knight_legend_seed_effect = {
	if = {
		limit = {
			location = {
				geographical_region = world_europe
			}
		}
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = swan_knight
			properties = {
				friendly_animal = flag:swan
			}
		}
	}
	else_if = {
		limit = {
			location = {
				OR = {
					geographical_region = world_india
					geographical_region = world_burma
					geographical_region = ghw_region_caucasus
					geographical_region = world_asia_china
					geographical_region = world_asia_japan
					geographical_region = world_asia_korea
					geographical_region = world_asia_southeast
				}
			}
		}
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = swan_knight
			properties = {
				friendly_animal = flag:tiger
			}
		}
	}
	else_if = {
		limit = {
			location = {
				geographical_region = world_africa
			}
		}
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = swan_knight
			properties = {
				friendly_animal = flag:lion
			}
		}
	}
	else = {
		create_legend_seed = {
			type = heroic
			quality = famed
			chronicle = swan_knight
			properties = {
				friendly_animal = flag:hart
			}
		}
	}
}
