######################
# Legends
######################

#### SPREAD CHANCE ###
# Chance for an legend of this quality to spread to an adjacent province per month
# % from 0-100 inclusive
# root = potential province
# scope:legend = the legend

### CONTROL VALUES ###
# These should be enough to change the balance of legends

# legend seed drop chance

legend_seed_chance_very_low = 5
legend_seed_chance_low = 15
legend_seed_chance_medium = 40
legend_seed_chance_high = 75

# Spread

legend_spread_chance_base = 0.5

legend_spread_increase_add_miniscule = 0.06
legend_spread_increase_add_very_low = 0.12
legend_spread_increase_add_low = 0.25
legend_spread_increase_add_medium = 0.5
legend_spread_increase_add_high = 1
legend_spread_increase_add_very_high = 1.5

legend_spread_decrease_add_very_low = -0.12
legend_spread_decrease_add_low = -0.25
legend_spread_decrease_add_medium = -0.5
legend_spread_decrease_add_high = -1

legend_spread_increase_mult_low = 0.25
legend_spread_increase_mult_medium = 0.75
legend_spread_increase_mult_high = 1.5

legend_spread_decrease_mult_low = -0.15
legend_spread_decrease_mult_medium = -0.25
legend_spread_decrease_mult_high = -0.5
legend_spread_decrease_mult_unlikely = -0.99



spread_chance_legends_default_value = {
	value = legend_spread_chance_base

	scope:legend = {
		if = {
			limit = {
				legend_quality = illustrious
			}
			add = legend_spread_increase_add_low
		}
		else_if = {
			limit = {
				legend_quality = mythical
			}
			add = legend_spread_increase_add_medium
		}
		
		#prestige levels
		legend_owner = {
			if = {
				limit = {
					prestige_level > 4
				}
				add = legend_spread_increase_add_low
			}
			else_if = {
				limit = {
					prestige_level > 3
				}
				add = legend_spread_increase_add_very_low
			}
		}
	}
	
	#Buildings in which people gossip
	if = {
		limit = {
			has_building_or_higher = common_tradeport_07
		}
		add = legend_spread_increase_add_low
		add = legend_spread_increase_add_low
		add = legend_spread_increase_add_very_low
		add = legend_spread_increase_add_miniscule
	}
	else_if = {
		limit = {
			has_building_or_higher = common_tradeport_05
		}
		add = legend_spread_increase_add_low
		add = legend_spread_increase_add_very_low
		add = legend_spread_increase_add_miniscule
	}
	else_if = {
		limit = {
			has_building_or_higher = common_tradeport_03
		}
		add = legend_spread_increase_add_very_low
		add = legend_spread_increase_add_miniscule
	}
	else_if = {
		limit = {
			has_building_or_higher = common_tradeport_01
		}
		add = legend_spread_increase_add_miniscule
	}

	if = {
		limit = {
			has_building_or_higher = city_03
		}
		add = legend_spread_increase_add_low
		add = legend_spread_increase_add_miniscule
	}
	else_if = {
		limit = {
			has_building_or_higher = city_02
		}
		add = legend_spread_increase_add_miniscule
	}
	
	# This can be else_if because market villages and cities are exclusive and cannot be in the same province
	else_if = {
		limit = {
			has_building_or_higher = market_villages_02
		}
		add = legend_spread_increase_add_low
		add = legend_spread_increase_add_miniscule
	}
	else_if = {
		limit = {
			has_building_or_higher = market_villages_01
		}
		add = legend_spread_increase_add_miniscule
	}

	# Legend type bonuses

	if = {
		limit = {
			has_building_or_higher = warrior_lodges_05
			scope:legend.legend_type = legend_type:heroic 
		}
		add = legend_spread_increase_add_low
		add = legend_spread_increase_add_low
	}
	else_if = {
		limit = {
			has_building_or_higher = warrior_lodges_03
			scope:legend.legend_type = legend_type:heroic 
		}
		add = legend_spread_increase_add_low
	}

	if = {
		limit = {
			faith = scope:legend.legend_owner.faith
			scope:legend.legend_type = legend_type:holy
		}
		add = legend_spread_increase_add_very_low
	}
	else_if = {
		limit = {
			scope:legend.legend_type = legend_type:holy
			faith = {
				faith_hostility_level = {
					target = scope:legend.legend_owner.faith
					value >= faith_astray_level
				}
			}
		}
		add = legend_spread_decrease_add_medium
	}
	else_if = {
		limit = {
			scope:legend.legend_type = legend_type:holy
			faith = {
				faith_hostility_level = {
					target = scope:legend.legend_owner.faith
					value > faith_astray_level
				}
			}
		}
		add = legend_spread_decrease_add_high
	}

	if = {
		limit = {
			scope:legend.legend_type = legend_type:legitimizing
			county.holder = {
				OR = {
					dynasty = scope:legend.legend_owner.dynasty
					top_liege.dynasty = scope:legend.legend_owner.dynasty
				}
			}
		}
		add = legend_spread_increase_add_very_low
	}
	else_if = {
		limit = {
			scope:legend.legend_type = legend_type:legitimizing
		}
		add = legend_spread_decrease_add_low
	}

	
	#Terrain
	if = {
		limit = {
			OR = {
				terrain = hills
				terrain = drylands
				terrain = forest
				terrain = floodplains
				terrain = wetlands
			}
		}
		add = legend_spread_decrease_add_low
	}
	else_if = {
		limit = {
			OR = {
				terrain = jungle
				terrain = taiga
			}
		}
		add = legend_spread_decrease_add_medium
	}
	else_if = {
		limit = {
			OR = {
				terrain = desert
				terrain = desert_mountains
				terrain = mountains
			}
		}
		add = legend_spread_decrease_add_high
	}
	#Island?
	if = {
		limit = {
			geographical_region = divergence_island_regions
		}
		add = legend_spread_decrease_add_medium
	}

	#Less patchy
	if = {
		limit = {
			county = {
				any_county_province = {
					any_province_legend = {
						this = scope:legend
					}
				}
			}
		}
		add = legend_spread_increase_add_very_low
	}


	#Is this in your realm? 
	county.holder = {
		if = {
			limit = { this = scope:legend.legend_owner }
			add = legend_spread_increase_add_very_high
		}
		else_if = {
			limit = { promoted_legend ?= scope:legend }
			add = legend_spread_increase_add_high
		}
		else_if = {
			limit = { top_liege = scope:legend.legend_owner }
			add = legend_spread_increase_add_medium
		}
		else_if = {
			limit = { top_liege.promoted_legend ?= scope:legend }
			add = legend_spread_increase_add_low
		}
		else = {
			add = legend_spread_decrease_add_low
		}
		
		#County holder opinion of legend owner
		if = {
			limit = {
				has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:legend.legend_owner }
			}
			add = legend_spread_increase_add_medium
		}
		else_if = {
			limit = {
				has_relation_rival = scope:legend.legend_owner
			}
			add = legend_spread_decrease_add_high
		}
		else = {
			add = legend_spread_decrease_add_low
		}
	}

	#Languages 

	if = {
		limit = {
			culture = { has_same_culture_language = scope:legend.legend_owner.culture }
		}
		add = legend_spread_increase_add_low
	}
	else_if = {
		limit = {
			county.holder = { knows_language_of_culture = scope:legend.legend_owner.culture }
		}
		add = legend_spread_increase_add_very_low
	}
	else_if = {
		limit = {
			scope:legend.legend_owner = {
				has_royal_court = yes
				has_dlc_feature = royal_court
			}
			county.holder = { knows_court_language_of = scope:legend.legend_owner }
		}
		add = legend_spread_increase_add_very_low
	}
	else_if = {
		limit = {
			culture = { has_same_culture_heritage = scope:legend.legend_owner.culture }
		}
		add = legend_spread_increase_add_miniscule
	}
	else_if = {
		limit = {
			scope:legend.legend_owner = {
				has_royal_court = yes
				has_dlc_feature = royal_court
			}
			OR = {
				county.holder.top_liege = {
					has_royal_court = yes
					has_same_court_language = scope:legend.legend_owner
				}
				county.holder = {
					has_royal_court = yes 
					has_same_court_language = scope:legend.legend_owner
				}
			}
		}
		add = legend_spread_decrease_add_very_low
	}
	else_if = {
		limit = {
			culture = {
				cultural_acceptance = { target = scope:legend.legend_owner.culture value > 40 }
			}
		}
		add = legend_spread_decrease_add_low
	}
	else_if = {
		limit = {
			culture = {
				cultural_acceptance = { target = scope:legend.legend_owner.culture value > 20 }
			}
		}
		add = legend_spread_decrease_add_medium
	}
	
	else = {
		add = legend_spread_decrease_add_high
	}

	#Coastal Statistics Squash
	if = {
		limit = {
			OR = {
				is_coastal = yes
				is_riverside_province = yes
			}
			NOR = {
				county = {
					any_county_province = {
						any_province_legend = {
							this = scope:legend
						}
					}
				}
				culture = { has_same_culture_language = scope:legend.legend_owner.culture }
			}
		}
		add = legend_spread_decrease_add_high
	}
}

spread_chance_legends_mult_value = {
	#NO ADD AFTER THIS POINT
	#NO ADD ONLY MULT
	value = 1
	min = 0.001

	#Coastal Statistics Squash
	if = {
		limit = {
			OR = {
				is_coastal = yes
				is_riverside_province = yes
			}
			NOR = {
				county = {
					any_county_province = {
						any_province_legend = {
							this = scope:legend
						}
					}
				}
			}
		}
		add = legend_spread_decrease_mult_high
	}

	county = {
		#Is this in your realm?
		holder = {
			if = {
				limit = {
					this = scope:legend.legend_owner
				}
				add = legend_spread_increase_mult_medium
			} 
			else_if = {
				limit = {
					OR = {
						top_liege = scope:legend.legend_owner
						promoted_legend ?= scope:legend
						top_liege.promoted_legend ?= scope:legend
					}
				}
				add = legend_spread_increase_mult_low
			}
			else = {
				add = legend_spread_decrease_mult_medium
			}
			
			#County holder and top liege opinion of legend owner
			if = {
				limit = { #slight mult increase for owner and promoter domain
					OR = {
						this = scope:legend.legend_owner
						promoted_legend ?= scope:legend
					}
				}
				add = legend_spread_increase_mult_medium
			}
			else_if = {
				limit = {
					OR = {
						has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:legend.legend_owner }
						top_liege = { has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:legend.legend_owner } }
					}
				}
				add = legend_spread_increase_mult_low
			}
			else_if = {
				limit = {
					OR = {
						has_relation_rival = scope:legend.legend_owner
						top_liege = { has_relation_rival = scope:legend.legend_owner }
					}
				}
				add = legend_spread_decrease_mult_high
			}
			else = {
				add = legend_spread_decrease_mult_low
			}
		}

		#Cultural Traditions
		culture = {
			if = {
				limit = {
					OR = {
						has_cultural_tradition = tradition_chanson_de_geste
						has_cultural_tradition = tradition_fp1_northern_stories
						has_cultural_tradition = tradition_storytellers
						has_cultural_tradition = tradition_poetry
					}
				}
				add = legend_spread_increase_mult_medium
			}
			else_if = {
				limit = {
					OR = {
						has_cultural_tradition = tradition_esteemed_hospitality
						has_cultural_tradition = tradition_horse_lords
						has_cultural_tradition = tradition_caravaneers
						has_cultural_tradition = tradition_seafaring
						has_cultural_tradition = tradition_city_keepers
						has_cultural_tradition = tradition_maritime_mercantilism
					}
				}
				add = legend_spread_increase_mult_low
			}
			else_if = {
				limit = {
					OR = {
						has_cultural_tradition = tradition_practiced_pirates
						has_cultural_tradition = tradition_parochialism
					}
				}
				add = legend_spread_increase_mult_low
			}
		}

		#Development
		if = {
			limit = {
				development_level >= good_development_level
			}
			add = legend_spread_decrease_mult_medium
		}
		else_if = {
			limit = {
				development_level >= medium_development_level 
			}
			add = legend_spread_decrease_mult_low
		}
	}

	# Game rules
	if = {
		limit = {
			has_game_rule = legend_spread_chance_high
		}
		add = legend_spread_increase_mult_low
	}
	else_if = {
		limit = {
			has_game_rule = legend_spread_chance_very_high
		}
		add = legend_spread_increase_mult_medium
	}
	else_if = {
		limit = {
			has_game_rule = legend_spread_chance_low
		}
		add = legend_spread_decrease_mult_low
	}
	else_if = {
		limit = {
			has_game_rule = legend_spread_chance_very_low
		}
		add = legend_spread_decrease_mult_medium
	}
}

### GOLD COSTS ###

legend_base_cost_value = {
	value = 200
}

legend_base_cost = {
	value = legend_base_cost_value
	#Scale by tier
	multiply = {
		value = root.primary_title.tier
		subtract = 1
		multiply = 0.5
		min = 1
	}
	if = {
		limit = {
			root = {
				government_has_flag = government_is_tribal
			}
		}
		multiply = 0.8
	}
	#Scale by era
	add = {
		value = legend_base_cost_value
		multiply =  legend_cost_scale_by_era
		subtract = legend_base_cost_value
		desc = legend_cost_scale_by_era_desc
	}
	#Legends Dynasty Track discounts
	add = legend_cost_dynasty_discounts

	# Game rules
	if = {
		limit = {
			has_game_rule = legend_cost_high
		}
		multiply = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_high
		}
		multiply = 4
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_low
		}
		divide = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_low
		}
		divide = 4
	}
}

legend_cost_scale_by_era = {
	value = 0
	if = {
		limit = {
			exists = culture
		}
		culture ?= {
			if = {
				limit = {
					has_cultural_era_or_later = culture_era_late_medieval
				}
				add = 1.6
			}
			else_if = {
				limit = {
					has_cultural_era_or_later = culture_era_high_medieval
				}
				add = 1.4
			}
			else_if = {
				limit = {
					has_cultural_era_or_later = culture_era_early_medieval
				}
				add = 1.2
			}
			else = {
				add = 1
			}
		}
	}
}

legend_cost_dynasty_discounts = {
	if = {
		limit = {
			dynasty ?= {
				ce1_heroic_track_perks >= 4
			}
		}
		multiply = 0.75
	}
	else_if = {
		limit = {
			dynasty ?= {
				ce1_heroic_track_perks >= 3
			}
		}
		multiply = 0.85
	}
}

legend_upgrade_cost = {
	value = legend_base_cost #includes scaling by era, tier and game rules
	multiply = 0.8
	if = {
		limit = {
			promoted_legend ?= { has_variable = legend_quality_progress }
		}
		subtract = {
			#Legend costs until now
			value = legend_base_cost
			multiply = 0.6
			#We give you 50% discount max based on your progress
			multiply = {
				value = promoted_legend.var:legend_quality_progress
				divide = 200
			}
		}
	}
}

legend_era_cost = {
	value = monthly_character_income
	multiply = legend_cost_scale_by_era
	subtract = monthly_character_income
	desc = legend_cost_scale_by_era_desc
}

legend_game_rule_multiplier = {
	value = 1
	
	# Game rules
	if = {
		limit = {
			has_game_rule = legend_cost_high
		}
		value = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_high
		}
		value = 4
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_low
		}
		value = 0.5
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_low
		}
		value = 0.25
	}
}

famed_legend_owner_cost = {
	value = monthly_character_income
	multiply = 0.1
	min = 3
	add = 1
	multiply = legend_cost_scale_by_era
	multiply = legend_game_rule_multiplier
	max = 300
}

illustrious_legend_owner_cost = {
	value = monthly_character_income
	multiply = 0.15
	min = 4.5
	add = 2
	multiply = legend_cost_scale_by_era
	multiply = legend_game_rule_multiplier
	max = 450
}

mythical_legend_owner_cost = {
	value = monthly_character_income
	multiply = 0.25
	min = 8
	add = 4
	multiply = legend_cost_scale_by_era
	multiply = legend_game_rule_multiplier  
	max = 800
}

# Promoter costs

famed_legend_promoter_cost = {
	value = monthly_character_income
	multiply = 0.05
	add = 0.5
	multiply = legend_cost_scale_by_era
	multiply = legend_game_rule_multiplier
}

illustrious_legend_promoter_cost = {
	value = monthly_character_income
	multiply = 0.1
	add = 1
	multiply = legend_cost_scale_by_era
	multiply = legend_game_rule_multiplier
}

mythical_legend_promoter_cost = {
	value = monthly_character_income
	multiply = 0.2
	add = 1.5
	multiply = legend_cost_scale_by_era
	multiply = legend_game_rule_multiplier
}

# We want higher AI income than cost when starting promoting, so we do not toggle start/stop promoting too much, or spend all their income on it

ai_famed_legend_promoter_start_required_income = {
	value = famed_legend_promoter_cost
	multiply = 1.4
}

ai_illustrious_legend_promoter_start_required_income = {
	value = illustrious_legend_promoter_cost
	multiply = 1.4
}

ai_mythical_legend_promoter_start_required_income = {
	value = mythical_legend_promoter_cost
	multiply = 1.4
}

### Prestige costs ###

legend_prestige_cost = {
	value = 200
	#Scale by tier
	multiply = {
		value = root.primary_title.tier
		subtract = 1
		multiply = 0.5
		min = 1
	}
	if = {
		limit = {
			root = {
				government_has_flag = government_is_tribal
			}
		}
		multiply = 0.8
	}
	#Scale by era
	add = {
		value = legend_base_cost_value
		multiply =  legend_cost_scale_by_era
		subtract = legend_base_cost_value
		desc = legend_cost_scale_by_era_desc
	}

	# Game rules
	if = {
		limit = {
			has_game_rule = legend_cost_high
		}
		multiply = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_high
		}
		multiply = 4
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_low
		}
		divide = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_low
		}
		divide = 4
	}
}

### Piety costs ###

legend_piety_cost = {
	value = 200
	#Scale by tier
	multiply = {
		value = root.primary_title.tier
		subtract = 1
		multiply = 0.5
		min = 1
	}
	if = {
		limit = {
			root = {
				government_has_flag = government_is_tribal
			}
		}
		multiply = 0.8
	}
	#Scale by era
	add = {
		value = legend_base_cost_value
		multiply =  legend_cost_scale_by_era
		subtract = legend_base_cost_value
		desc = legend_cost_scale_by_era_desc
	}

	# Game rules
	if = {
		limit = {
			has_game_rule = legend_cost_high
		}
		multiply = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_high
		}
		multiply = 4
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_low
		}
		divide = 2
	}
	else_if = {
		limit = {
			has_game_rule = legend_cost_very_low
		}
		divide = 4
	}
}

### COURT POSITION TASKS ###

monthly_court_position_task_cost_value = {
	value = 0.05
}

monthly_court_position_task_cost = {
	value = monthly_court_position_task_cost_value
	scope:liege = {
		#Scale by tier
		multiply = {
			value = primary_title.tier
			subtract = 1
			min = 1
		}
		# Performance-heavy, so we let the AI cheat very lightly here
		if = {
			limit = {
				is_ai = no
			}
			#Scale by era
			if = {
				limit = {
					scope:liege.culture ?= { has_cultural_era_or_later = culture_era_late_medieval }
				}
				multiply =  {
					value =  1.75
					desc = culture_era_late_medieval_salary_mod
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
			else_if = {
				limit = {
					scope:liege.culture ?= { has_cultural_era_or_later = culture_era_high_medieval }
				}
				multiply = {
					value = 1.5
					desc = culture_era_high_medieval_salary_mod
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
			else_if = {
				limit = {
					scope:liege.culture ?= { has_cultural_era_or_later = culture_era_early_medieval }
				}
				multiply = {
					value = 1.25
					desc = culture_era_early_medieval_salary_mod
					format = "BASE_VALUE_FORMAT_DECIMALS_PLUS_NEGATIVE"
				}
			}
		}
	}
	if = {
		limit = {
			OR = {
				has_game_rule = hard_difficulty
				has_game_rule = very_hard_difficulty
			}
			scope:liege = {
				is_ai = yes
			}
		}
		multiply = 0
	}
}

monthly_court_position_task_cost_double = {
	value = monthly_court_position_task_cost
	multiply = 2
}

monthly_court_position_task_cost_triple = {
	value = monthly_court_position_task_cost
	multiply = 3
}

monthly_court_position_task_cost_quadruple = {
	value = monthly_court_position_task_cost
	multiply = 4
}

### DYNAMIC VALUES ###
legend_piety_gain = {
	value = 0
	if = {
		limit = { root = { legend_quality = famed } }
		value = minor_piety_gain
	}
	else_if = {
		limit = { root = { legend_quality = illustrious } }
		value = medium_piety_gain
	}
	else = { #mythical
		value = major_piety_gain
	}
}

legend_prestige_gain = {
	value = 0
	if = {
		limit = { root = { legend_quality = famed } }
		value = minor_prestige_gain
	}
	else_if = {
		limit = { root = { legend_quality = illustrious } }
		value = medium_prestige_gain
	}
	else = { #mythical
		value = major_prestige_gain
	}
}

legend_dynasty_prestige_gain = {
	value = 0
	if = {
		limit = { root = { legend_quality = famed } }
		value = miniscule_dynasty_prestige_gain
	}
	else_if = {
		limit = { root = { legend_quality = illustrious } }
		value = minor_dynasty_prestige_gain
	}
	else = { #mythical
		value = major_dynasty_prestige_gain
	}
}

major_legend_quality_increase = 50
medium_legend_quality_increase = 20
minor_legend_quality_increase = 10
minor_legend_quality_decrease = {
	value = 0
	subtract = minor_legend_quality_increase
}
medium_legend_quality_decrease = {
	value = 0
	subtract = medium_legend_quality_increase
}
major_legend_quality_decrease = {
	value = 0
	subtract = major_legend_quality_increase
}

base_legend_create_ai = {
	value = 10

	add = {
		value = ai_greed
		multiply = -1
	}

	if = {
		limit = {
			highest_held_title_tier <= tier_duchy
		}
		add = -10
	}
	
	if = {
		limit = { 
			monthly_character_income < mythical_legend_owner_cost
		}
		multiply = 0.2
	}
	if = {
		limit = {
			monthly_character_income <= monthly_character_expenses
		}
		multiply = 0
	}
	if = {
		limit = {
			has_trait = arrogant
		}
		multiply = 2
	}
	if = {
		limit = {
			has_trait = ambitious
		}
		multiply = 2
	}

	#you need to be this rich to create legends
	if = {
		limit = {
			OR = {
				is_at_war = yes
				monthly_character_income < illustrious_legend_owner_cost
			}
		}
		value = 0
	}
	if = {
		limit = {
			has_variable = conqueror
			gold < 2500
		}
		multiply = 0
	}
	if = {
		limit = {
			ai_should_focus_on_building_in_their_capital = yes
		}
		multiply = 0
	}
	if = {
		limit = {
			this != top_liege
		}
		multiply = 0
	}
	if = {
		limit = {
			has_global_variable_list = active_legends_tally
			global_variable_list_size = {
				name = active_legends_tally
				value >= 10
			}
		}
		multiply = 0
		add = -5000
	}
}

heroic_legend_create_ai = {
	value = base_legend_create_ai
	add = ai_honor
	if = {
		limit = {
			highest_held_title_tier <= tier_duchy
		}
		add = -150
	}
	if = {
		limit = {
			ai_has_warlike_personality = yes
		}
		multiply = 2
	}
	if = {
		limit = {
			ai_has_cautious_personality = yes
		}
		multiply = 0.1
	}
	if = {
		limit = {
			ai_has_economical_boom_personality = yes
		}
		multiply = 0.75
	}
	if = {
		limit = {
			has_variable = conqueror
			gold < 2500
		}
		multiply = 0
	}
	if = {
		limit = {
			ai_should_focus_on_building_in_their_capital = yes
		}
		multiply = 0
	}
}

holy_legend_create_ai = {
	value = base_legend_create_ai
	add = ai_zeal
	if = {
		limit = {
			highest_held_title_tier <= tier_duchy
		}
		add = -150
	}
	if = {
		limit = {
			ai_has_pious_builder_personality = yes
		}
		multiply = 1.3
	}
	if = {
		limit = {
			ai_has_warlike_personality = yes
		}
		multiply = 1.1
	}
	if = {
		limit = {
			ai_has_cautious_personality = yes
		}
		multiply = 0.9
	}
	if = {
		limit = {
			ai_has_economical_boom_personality = yes
		}
		multiply = 0.75
	}
	if = {
		limit = {
			has_variable = conqueror
			gold < 2500
		}
		multiply = 0
	}
	if = {
		limit = {
			ai_should_focus_on_building_in_their_capital = yes
		}
		multiply = 0
	}
}

legitimizing_legend_create_ai = {
	value = base_legend_create_ai
	add = ai_rationality
	if = {
		limit = {
			highest_held_title_tier <= tier_duchy
		}
		add = -150
	}
	if = {
		limit = {
			ai_has_warlike_personality = yes
		}
		multiply = 1.1
	}
	if = {
		limit = {
			ai_has_cautious_personality = yes
		}
		multiply = 0.1
	}
	if = {
		limit = {
			ai_has_economical_boom_personality = yes
		}
		multiply = 0.9
	}
	if = {
		limit = {
			has_variable = conqueror
			gold < 2500
		}
		multiply = 0
	}
	if = {
		limit = {
			ai_should_focus_on_building_in_their_capital = yes
		}
		multiply = 0
	}
}

illustrious_legend_create_ai = {
	add = 25
	if = {
		limit = {
			monthly_character_income < illustrious_legend_promoter_cost
		}
		value = 0
	}
}

mythical_legend_create_ai = {
	add = 50
	if = {
		limit = {
			monthly_character_income < mythical_legend_promoter_cost
		}
		value = 0
	}
}


base_legend_promote_ai = {
	value = 0
	if = {
		limit = {
			has_any_best_good_relationship_with_character_trigger = { CHARACTER = scope:legend.legend_owner }
		}
		add = 20
	}
	else_if = {
		limit = {
			has_any_moderate_good_relationship_with_character_trigger = { CHARACTER = scope:legend.legend_owner }
		}
		add = 8
	}
	else_if = {
		limit = {
			likes_character_trigger = { CHARACTER = scope:legend.legend_owner }
		}
		add = 2
	}
	if = {
		limit = {
			culture = {
				OR = {
					has_cultural_tradition = tradition_chanson_de_geste
					has_cultural_tradition = tradition_fp1_northern_stories
					has_cultural_tradition = tradition_storytellers
					has_cultural_parameter = poet_trait_gives_bonuses
				}
			}
		}
		add = 2
	}
	if = { # The protag is my relative
		limit = {
			is_close_or_extended_family_of = scope:legend.legend_owner
		}
		add = 2
	}

	if = {
		limit = {
			has_trait = arrogant # I want to create my own legend >:(
		}
		add = -4
	}
	if = {
		limit = {
			has_trait = cynical
		}
		add = -4
	}

	if = {
		limit = {
			top_liege != this
		}
		add = -4
	}
	
	add = {
		value = "opinion(scope:legend.legend_owner)"
		multiply = 0.05
	}	

	if = {
		limit = {
			any_character_to_title_neighboring_county = {
				any_county_province = {
					any_province_legend = {
						this = scope:legend
					}
				}
			}
		}
		add = 2
	}
	else = {
		add = -8
	}

	if = { #no free meals
		limit = {
			primary_title.tier = tier_empire
		}
		add = -16
	}
	else_if = {
		limit = {
			primary_title.tier = tier_kingdom
		}
		add = -8
	}
	else_if = { #you need to be this tall to promote legends
		limit = {
			primary_title.tier < tier_duchy
		}
		value = 0
	}

	if = {
		limit = {
			scope:legend = {
				legend_quality = illustrious
			}
		}
		multiply = 1.2
	}
	else_if = {
		limit = {
			scope:legend = {
				legend_quality = mythical
			}
		}
		multiply = 1.4
	}

	if = {
		limit = {
			OR = {
				# War is no time for idle spending
				is_at_war = yes

				# Opt out if we do not have enough income to promote this legend
				# (only for legends we're not currently promoting - stopping the currently promoting legend is handled by code)
				AND = {
					NOT = { promoted_legend ?= scope:legend }
					OR = {
						AND = {
							scope:legend = {
								legend_quality = famed
							}
							monthly_character_income < ai_famed_legend_promoter_start_required_income
						}
						AND = {
							scope:legend = {
								legend_quality = illustrious
							}
							monthly_character_income < ai_illustrious_legend_promoter_start_required_income
						}
						AND = {
							scope:legend = {
								legend_quality = mythical
							}
							monthly_character_income < ai_mythical_legend_promoter_start_required_income
						}
					}
				}
			}
		}
		value = 0
	}
	#to prevent AI switching promoters too often
	if = {
		limit = {
			exists = promoted_legend
		}
		multiply = 0.4
	}
	if = {
		limit = {
			has_variable = conqueror
			gold < 2500
		}
		multiply = 0
	}
	if = {
		limit = {
			ai_should_focus_on_building_in_their_capital = yes
		}
		multiply = 0
	}
}

base_legend_complete_ai = {
	value = 0
	if = { # I cannot afford the current levels monthly cost anymore
        limit = {
            scope:can_afford_current_level = no
        }
        add = 100
    }
	if = { # My legend is done
		limit = {
			scope:legend = { legend_quality = mythical }
		}
		add = 100
	}
	if = { # This legend is going nowhere :(
		limit = {
			current_year >= {
				value = scope:legend.legend_start_date
				add = 100
			}
		}
		add = 10
	}
	if = {
		limit = {
			current_year >= {
				value = scope:legend.legend_start_date
				add = 200
			}
		}
		add = 20
	}
	if = {
		limit = {
			scope:legend = {
				any_spread_province > { count = 350 }
			}
		}
		add = 40
	}
	if = {
		limit = {
			monthly_character_income <= monthly_character_expenses 
			debt_level = 2 #Is one year in debt.
		}
		add = 10
	}
	else_if = {
		limit = {
			monthly_character_income <= monthly_character_expenses
			debt_level > 2 # Is two years in debt.
		}
		add = 25
	}
	if = {
		limit = {
			debt_level = 5	# Is four years in debt
		}
		add = 40
	}
}

base_chronicler_promote_legend_realm_cost = {
	value = 0
	if = {
		limit = {
			scope:liege ?= {
				promoted_legend ?= {
					legend_owner = scope:liege
				}
			}
		}
		add = scope:liege.famed_legend_owner_cost
	}
	else = {
		add = scope:liege.famed_legend_promoter_cost
	}
	multiply = 0.8
}
