# Referenced from code, in code value will be clamped between NRoyalCourt::COURT_GRANDEUR_MIN and NRoyalCourt::COURT_GRANDEUR_MAX
# root = character calculating for
court_grandeur_minimum_expected_value = {
	value = sub_realm_size
	divide = 4
	if = { # Elective Empires have slightly lower expectations (i.e. the HRE)
		limit = {
			any_held_title = {
				tier = tier_empire
				has_title_law_flag = elective_succession_law
			}
		}
		divide = 2
	}
	if = { # Add a baseline depending on rank
		limit = {
			highest_held_title_tier = tier_empire
		}
		add = 20
	}
	else = {
		add = 10
	}
	if = {
		limit = {
			government_has_flag = government_is_tribal
		}
		multiply = 0
		add = court_grandeur_current
	}
}

# Referenced from code for the multiplier of above/below expected court grandeur level modifiers
court_grandeur_level_difference_modifier_multiplier = {
	value = 1
	if = { # Small realms receives larger bonuses for being above their expected level
		limit = {
			court_grandeur_current_level >= court_grandeur_minimum_expected_level
		}
		value = 100 # Realm size baseline
		divide = {
			value = sub_realm_size
			min = 1 		
		} 
		min = 1
		max = 4
	}
	else = { # Big realms receives larger penalties for being below their expected level
		value = sub_realm_size
		multiply = 0.025
		min = 1
		max = 4
	}
}

delusions_of_grandeur_level_difference = {
	value = court_grandeur_minimum_expected_level
	add = 6
}

ep1_court_grandeur_marriage_acceptance_value = {
	scope:actor = {
		add = court_grandeur_current_level
		subtract = court_grandeur_minimum_expected_level
		multiply = 5
		if = {
			limit = {
				scope:recipient.faith = {
					faith_hostility_level = {
						target = scope:actor.faith
						value = faith_astray_level
					}
				}
			}
			divide = 2
		}
		else_if = {
			limit = {
				scope:recipient.faith = {
					faith_hostility_level = {
						target = scope:actor.faith
						value = faith_hostile_level
					}
				}
			}
			divide = 3
		}
		else_if = {
			limit = {
				scope:recipient.faith = {
					faith_hostility_level = {
						target = scope:actor.faith
						value = faith_evil_level
					}
				}
			}
			divide = 4
		}
	}
}

ep1_court_grandeur_marriage_acceptance_value_penalty = {
	scope:actor = {
		add = court_grandeur_current_level
		subtract = court_grandeur_minimum_expected_level
		multiply = 10
	}
}

grandeur_loss_on_succession_value = {
	value = -10
	if = { # 100 Dread makes you lose 5 more grandeur
		limit = {
			dread > 0
		}
		subtract = {
			value = dread
			divide = 20
		}
	}
	if = { # 100 Tyranny makes you lose 10 more grandeur
		limit = {
			tyranny > 0
		}
		subtract = {
			value = tyranny
			divide = 10
		}
	}
	if = {
		limit = {
			exists = primary_heir
			primary_heir = {
				has_trait = ambitious
			}
		}
		multiply = 0.5
	}
	if = {
		limit = {
			exists = primary_title
			primary_title = {
				has_order_of_succession = election
			}
		}
		multiply = 0.5
	}
}

# The unmodified, average cost of sponsoring an Inspiration.
basic_fund_inspiration_cost = {
	value = 100
}

inspiration_cost_skill_multiplier = 0.5
quality_bonus_per_skill_level_value = 2 # A character with an average skill value of 20 will produce artifacts with +40 quality

# Calculates the total number of characters in the world who have an active Inspiration.
num_world_inspired_characters_value = {
	value = 0
	every_inspired_character = {
		limit = { has_completed_inspiration = no }
		add = 1
	}
}

# Calculates the total number of characters in the world who currently have a Royal Court.
num_world_royal_courts_value = {
	value = 0
	every_character_with_royal_court = {
		add = 1
	}
}

# How many new inspirations can be created by inspiration_system.0005 each year if we are below the limit.
max_new_inspirations_per_year_value =  {
	value = 3 # 16% of the existing world region have a chance to get an inspired character
	add = {
		value = 15 #if every royal court is wealthy enough, we generate 1x nb of world region inspired characters
		multiply = {
			value = 0
			add = {
				every_character_with_royal_court = {
					limit = { 
						OR = {
							is_ai = no
							AND = {
								is_ai = yes 
								short_term_gold >= ai_inspiration_desired_gold_value
							}
						}
					}
					add = 1 
				}
				divide = {
					every_character_with_royal_court = {
						add = 1
					}
					min = 1 # security to avoid dividing by 0 if there is none royal court
				}
			}
		}
		ceiling = yes
	}
}

# The 'ideal' limit for how many total inspirations will exist in the world at a time, per royal court.
# Scripted effects can bring us over this value, but inspiration_system.0005 will not generate new inspirations until we fall back under it.
ideal_total_inspirations_per_royal_court_value = 1.875

ideal_total_world_inspirations = {
	value = num_world_royal_courts_value
	multiply = ideal_total_inspirations_per_royal_court_value
}

# How many months an inspired character will wait around in a court for before moving on to a new one.
inspired_character_recent_arrival_duration_value = {
	value = 15
	# Adds -3 or +3 months, if they are (im)patient.
	if = {
		limit = { has_trait = impatient }
		subtract = 3
	}
	else_if = {
		limit = { has_trait = patient }
		add = 3
	}
	# Adds between +3 and -3 months before moving on, based on ai_energy.
	add = {
		value = ai_energy
		multiply = -0.03
		round = yes
	}
}

# How many years a courtier must serve in a specific court to get the court type trait
years_to_gain_court_type_trait = 5

# The trigger to check time served ("days_since_joined_court") needs days, so we convert the years here
days_to_gain_court_type_trait = {
	add = years_to_gain_court_type_trait
	multiply = 365
}

cgv_level_threshold_court_type_trait_1 = 5
cgv_level_threshold_court_type_trait_2 = 8

# Court Type Values
court_intrigue_spymaster_task_bonus = 20
court_administrative_steward_task_bonus = 30

# Hold Court Grace Values
hold_court_grace_period_years = 1825

hold_court_grace_period_comparison = {
	value = 1
	add = {
		value = current_year
	    subtract = var:last_hold_court_date
	    subtract = hold_court_grace_period_years # 5
	    multiply = 0.1
	    min = 0
	    max = 1
	}
	# example 
	# 1066 - 1054 = 12 - 5 = 7 * 0.1 = 0.7 + 1 = 1.7
	# subtract 5 as this is the hard cooldown, we only want to increase weight for years beyond that
	# modifier caps at 2 (10 years since last hold court)
}

# To be used for court positions that are very mobile and requires a healthy and strong body
court_position_aptitude_high_penalty_value = {
	if = {
		limit = {
			is_playable_character = yes
		}
		add = {
			value = -20
			desc = court_position_landed_penalty
		}
	}
	if = {
		limit = {
			has_trait = maimed
		}
		add = {
			value = -25
			desc = court_position_maimed_trait
		}
	}
	if = {
		limit = {
			has_trait = wounded_3
		}
		add = {
			value = -25
			desc = court_position_wounded_3_trait
		}
	}
	else_if = {
		limit = {
			has_trait = wounded_2
		}
		add = {
			value = -15
			desc = court_position_wounded_2_trait
		}
	}
	#Wounded_1 is ok since it's not a serious injury... yet
	if = {
		limit = {
			has_trait = blind
		}
		add = {
			value = -50
			desc = court_position_blind_trait
		}
	}
	if = {
		limit = {
			has_trait = infirm
		}
		add = {
			value = -75
			desc = court_position_infirm_trait
		}
	}
}

# To be used for court positions that are NOT very mobile and doesn't really require a strong/healthy body
court_position_aptitude_low_penalty_value = {
	if = {
		limit = {
			is_playable_character = yes
		}
		add = {
			value = -20
			desc = court_position_landed_penalty
		}
	}
	if = {
		limit = {
			has_trait = maimed
		}
		add = {
			value = -15
			desc = court_position_maimed_trait
		}
	}
	if = {
		limit = {
			has_trait = wounded_3
		}
		add = {
			value = -15
			desc = court_position_wounded_3_trait
		}
	}
	else_if = {
		limit = {
			has_trait = wounded_2
		}
		add = {
			value = -10
			desc = court_position_wounded_2_trait
		}
	}
	#Wounded_1 is ok since it's not a serious injury... yet
	if = {
		limit = {
			has_trait = infirm
		}
		add = {
			value = -75
			desc = court_position_infirm_trait
		}
	}
}

court_position_candidate_score_base_value = {
	scope:liege = {
		# Not very likely to hire rivals
		if = {
			limit = {
				has_relation_rival = scope:employee
			}
			add = -1000
		}
		if = {
			limit = {
				NOT = { exists = scope:firing_court_position }
				scope:employee = { has_any_court_position = yes }
			}
			add = -150
		}
	}
}

# Aptitude goes from 1 to 5, so the score here goes from 4 to 20 (except significantly discouraging hiring someone who isn't the best)
court_position_candidate_aptitude_value = {
	scope:employee = {
		# Does the liege have a better candidate that they should be hiring instead?
		if = {
			limit = {
				scope:highest_available_aptitude > scope:employee_aptitude
			}
			add = -125
		}
		# More likely to pick a courtier with a high aptitude, less likely to pick one with low
		add = {
			value = scope:employee_aptitude
			multiply = 4
		}
	}
}

court_position_debt_considerations_value = {
	scope:liege = {
		if = {
			limit = {
				debt_level >= 5
			}
			add = -5000
		}
		else_if = {
			limit = {
				debt_level >= 4
			}
			add = -1000
		}
		else_if = {
			limit = {
				debt_level >= 3
			}
			add = -500
		}
		else_if = {
			limit = {
				debt_level >= 2
			}
			add = -200
		}
		else_if = {
			limit = {
				debt_level >= 1
			}
			add = -50
		}
		if = { # Needs money for WAR!
			limit = {
				ai_has_conqueror_personality = yes
			}
			add = -100
		}
	}
}

court_position_seneschal_debt_considerations_value = { # Regardless of debt, having a seneschal is good if your control is abysmal
	scope:liege = {
		if = {
			limit = {
				is_at_war = no
				NOT = {
					any_held_title = {
						tier = tier_county
						county_control < 100
					}
				}
			}
			if = {
				limit = {
					debt_level >= 5
				}
				add = -5000
			}
			else_if = {
				limit = {
					debt_level >= 4
				}
				add = -1000
			}
			else_if = {
				limit = {
					debt_level >= 3
				}
				add = -500
			}
			else_if = {
				limit = {
					debt_level >= 2
				}
				add = -200
			}
			else_if = {
				limit = {
					debt_level >= 1
				}
				add = -50
			}
		}
	}
}

# This is balanced against a typical base score of 50
# And a typical skill score of 12 (aptitude of 3)
# We want to sit at roughly 10% of income spent
# So 10% income spent needs to give -62 score
# So the cost of them all we multiply by 600, for 10% = -60
# While the cost of the position itself with 100. 5 positions of 2% each will then each get -2
court_position_liege_score_base_value = {
	value = 0
	scope:liege = {
		subtract = {
			# Less likely to hire for the really expensive positions
			# But this is a pretty minor impact
			value = 100
			multiply = scope:percent_of_monthly_gold_income
		}
		subtract = {
			# Value decreases as cost of all positions increase
			value = 600
			if = {
				limit = {
					ai_should_focus_on_building_in_their_capital = yes
				}
				add = 400
			}
			multiply = scope:percent_of_monthly_gold_income_all_positions
		}
	}
}

order_mass_eviction_decision_cost_value = 350

exoticise_a_grand_hall_decision_cost_value = {
	# A nicely hefty base cost.
	value = massive_gold_value

	# Modified down a bit for architect.
	if = {
		limit = { has_trait = architect }
		multiply = 0.75
	}
}

# Check the guts of the value inside a sub-value so that we can script a fallback.
ep1_decision_0111_proportional_cgv_actual_gain_value = {
	value = 0

	# Add the target's CGV.
	add = court_grandeur_current
	# Subtract root's CGV to get the difference.
	subtract = root.court_grandeur_current
	# Reduce it down a bit.
	multiply = 0.5
	# Round it out.
	round = yes
}

ep1_decision_0111_proportional_cgv_gain_value = {
	value = 0

	# Import the real tally from elsewhere.
	add = ep1_decision_0111_proportional_cgv_actual_gain_value
	# Fallback: we can't set a minimum, since we can have values either side of zero. If we hit zero, default to 1 instead.
	if = {
		limit = {
			ep1_decision_0111_proportional_cgv_actual_gain_value = 0
		}
		add = 1
	}
}

ep1_decision_0111_proportional_prestige_cost_value = {
	# Carry over our CGV differential.
	value = ep1_decision_0111_proportional_cgv_gain_value

	# Invert it: if you lose CGV, we want you to gain prestige, and if you gain CGV, you should lose prestige.
	multiply = -1
	# Whilst we're here (and numbers are low), round things.
	round = yes
	# Make the cost a noteworthy fraction.
	multiply = 50
}

ep1_flavour_0001_scion_minimum_age_value = 18

ep1_flavour_0001_scion_parent_minimum_age_value = {
	# Base of 20 for something kinda sensible.
	value = 20
	# Plus the scion's minimum age.
	add = ep1_flavour_0001_scion_minimum_age_value
}

# Scripted value used so we can reference it in a custom tooltip.
ep1_flavour_0021_courts_lose_opinion_value = 20

# Aaaaand a further linked value so that the loc plays nicely; otherwise we get a double negative of "loses -20".
ep1_flavour_0021_courts_lose_opinion_actual_value = {
	value = ep1_flavour_0021_courts_lose_opinion_value
	multiply = -1
}

# Scripted value used so we can reference it in a custom tooltip.
ep1_flavour_0021_courts_gain_opinion_value = 20

# As the name says... value of gained prestige per level of food amenities at feasts!
ep1_feast_prestige_per_food_amenities_value = {
	value = medium_prestige_gain
	if = {
		limit = {
			amenity_level = { type = court_food_quality value >= max_amenity_level }
		}
		add = {
			value = medium_prestige_gain
			multiply = 0.4
		}
	}
	else_if = {
		limit = {
			amenity_level = { type = court_food_quality value = very_high_amenity_level }
		}
		add = {
			value = medium_prestige_gain
			multiply = 0.25
		}
	}
	else_if = {
		limit = {
			amenity_level = { type = court_food_quality value = medium_amenity_level }
		}
		add = {
			value = medium_prestige_gain
			multiply = -0.25
		}
	}
	else_if = {
		limit = {
			amenity_level = { type = court_food_quality value <= low_amenity_level }
		}
		add = {
			value = medium_prestige_gain
			multiply = -0.4
		}
	}
}

challenge_for_artifact_prestige_cost_value = {
	value = 150
	scope:target = {
		if = {
			limit =  { rarity = masterwork }
			multiply = 2
		}
		else_if = {
			limit = { rarity = famed }
			multiply = 3
		}
		else_if = {
			limit = { rarity = illustrious }
			multiply = 4
		}
	}
}

challenge_for_artifact_prestige_limit_value = {
	value = challenge_for_artifact_prestige_cost_value
	divide = 3
	min = 150
}

ep1_fund_inspiration_2100_rival_difference = {
	add = scope:other_inspiration_owner.inspiration.inspiration_gold_invested
	subtract = scope:inspiration_owner.inspiration.inspiration_gold_invested
}

50_percent_of_owned_gold = {
	value = current_gold_value
	multiply = 0.5
	min = 1
}

80_percent_of_owned_gold = {
	value = current_gold_value
	multiply = 0.8
	min = 1
}
