golden_obligation_value = {
	if = {
		limit = { 
			ransom_cost_value < medium_gold_value 
			ransom_cost_value > minor_gold_value 
		}
		value = minor_gold_value
	}
	else_if = {
		limit = {
			ransom_cost_value < medium_gold_value 
			ransom_cost_value < minor_gold_value 
		}
		value = ransom_cost_value
	}
	else = {
		value = medium_gold_value
	}
}

golden_obligation_value_check_current_situation = {
	add = gold
	max = golden_obligation_value
}

ransom_cost_value = {
	value = ransom_cost
	# Some cultures are better at ransoming than others.
	if = {
		limit = {
			exists = imprisoner
			imprisoner.culture = { has_cultural_parameter = can_demand_higher_ransoms_from_lower_tiers }
			# Kings and emperors are already worth more, so we just boost landless and ducal-tier or below.
			OR = {
				NOT = { exists = liege }
				highest_held_title_tier <= tier_duchy
			}
		}
		multiply = 1.5
	}
	# Rich guests can pay a bit more...
	if = {
		limit = {
			NOT = { exists = liege }
			gold >= 100
		}
		add = 90
	}
	#increase it if the imprisoner has a Haggler Officer employed
	if = {
		limit = {
			exists = scope:imprisoner
			scope:imprisoner ?= {
				employs_court_position = person_haggler_camp_officer
			}
		}
		multiply = {
			value = scope:imprisoner.court_position:person_haggler_camp_officer.aptitude:person_haggler_camp_officer
			add = 1
			divide = 15
			add = 1
		}
	}
	if = {
		limit = {
			imprisoner ?= {
				employs_court_position = person_haggler_camp_officer
			}
		}
		multiply = {
			value = imprisoner.court_position:person_haggler_camp_officer.aptitude:person_haggler_camp_officer
			add = 1
			divide = 15
			add = 1
		}
	}
	#decrease it if the imprisoned has a Haggler Officer employed
	if = {
		limit = {
			OR = {
				AND = {
					exists = scope:imprisoner
					scope:recipient ?= scope:imprisoner
				}
				AND = {
					exists = imprisoner
					scope:recipient ?= imprisoner
				}
			}
			scope:actor ?= {
				employs_court_position = person_haggler_camp_officer
			}
		}
		divide = {
			value = scope:actor.court_position:person_haggler_camp_officer.aptitude:person_haggler_camp_officer
			add = 1
			divide = 10
			add = 1
		}
	}
	#decrease it if the imprisoned has a Haggler Officer employed
	if = {
		limit = {
			OR = {
				AND = {
					exists = scope:imprisoner
					scope:actor ?= scope:imprisoner
				}
				AND = {
					exists = imprisoner
					scope:actor ?= imprisoner
				}
			}
			scope:recipient ?= {
				employs_court_position = person_haggler_camp_officer
			}
		}
		divide = {
			value = scope:recipient.court_position:person_haggler_camp_officer.aptitude:person_haggler_camp_officer
			add = 1
			divide = 10
			add = 1
		}
	}
}

current_situation_gold_ransom = {
	if = {
		limit = {
			NOT = {
				exists = liege
			}
		}
		add = gold
	}
	else_if = {
		limit = {
			is_ruler = no
		}
		add = liege.gold
	}
	else = {
		add = gold
	}
	max = ransom_cost_value
}

half_ransom_cost_value = {
	value = {
		add = ransom_cost_value
		divide = 2
		min = 1
	}
}

half_increased_ransom_cost_value = {
	value = {
		add = increased_ransom_cost_value
		divide = 2
		min = 1
	}
}

increased_ransom_cost_value = {
	value = {
		add = ransom_cost_value
		multiply = 1.5
	}
}

increased_ransom_cost = {
	value = {
		add = ransom_cost
		multiply = 1.5
	}
}

#reverse all ransom_cost_value calculations leaving just the haggler value and show it in all ransom interation windows
haggler_ransom_cost_modifier = {
	value = ransom_cost_value
	# Rich guests can pay a bit more...
	if = {
		limit = {
			NOT = { exists = liege }
			gold >= 100
		}
		subtract = 90
	}
	if = {
		limit = {
			exists = imprisoner
			imprisoner.culture = { has_cultural_parameter = can_demand_higher_ransoms_from_lower_tiers }
			# Kings and emperors are already worth more, so we just boost landless and ducal-tier or below.
			OR = {
				NOT = { exists = liege }
				highest_held_title_tier <= tier_duchy
			}
		}
		divide = 1.5
	}
	subtract = ransom_cost
}

grant_title_opinion_sum_baronies = {
	if = { # if no other titles present, calc for baronies
		limit = {
			any_in_list = {
				list = titles_to_grant
				tier = tier_barony
			}
		}

		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_barony
			}
			add = 20 # barony grant opinion
		}
	}
}

grant_title_opinion_sum_counties = {
	if = { # if no higher titles present, calc for counties
		limit = {
			any_in_list = {
				list = titles_to_grant
				tier = tier_county
			}
		}

		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_county
			}
			add = 40 # county grant opinion
		}
	}
}

grant_title_opinion_sum_duchies = {
	if = {
		limit = {
			any_in_list = {
				list = titles_to_grant
				tier = tier_duchy
			}
		}
		
		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_duchy
			}
			add = 60 # county grant opinion
		}
	}
}

grant_title_opinion_sum_kingdoms = {
	if = {
		limit = {
			any_in_list = {
				list = titles_to_grant
				tier = tier_kingdom
			}
		}
		
		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_kingdom
			}
			add = 80 # kingdom grant opinion
		}
	}
}

grant_title_opinion_sum_empires = {
	if = {
		limit = {
			any_in_list = {
				list = titles_to_grant
				tier = tier_empire
			}
		}
		
		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_empire
			}
			add = 100 # empire grant opinion
		}
	}
}

# Used in the corresponding character interaction
# when granting a title to the member of a faction.
grant_title_opinion_sum_discontent_calc = {
	value = 0

	add = {
		if = {
			limit = {
				grant_title_opinion_sum_empires > 0
			}
			add = {
				value = grant_title_opinion_sum_empires
				multiply = 0.6
			}
		}
		if = {
			limit = {
				grant_title_opinion_sum_kingdoms > 0
			}
			add = {
				value = grant_title_opinion_sum_kingdoms
				divide = 2
			}
		}
		if = {
			limit = {
				grant_title_opinion_sum_duchies > 0
			}
			add = {
				value = grant_title_opinion_sum_duchies
				divide = 3
			}
		}
		if = {
			limit = {
				grant_title_opinion_sum_counties > 0
			}
			add = {
				value = grant_title_opinion_sum_counties
				divide = 4
			}
		}
		if = {
			limit = {
				grant_title_opinion_sum_baronies > 0
			}
			add = {
				value = grant_title_opinion_sum_baronies
				divide = 4
			}
		}
	}

	# Discontent should *drop* when someone is granted a title
	multiply = -1
}

courtly_vassal_lowborn_grant_penalty = {
	if = {
		limit = {
			any_in_list = {
				list = titles_to_grant
				tier >= tier_county
			}
		}

		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_county
			}
			add = -5
		}

		every_in_list = {
			list = titles_to_grant
			limit = {
				tier = tier_duchy
			}
			add = -20
		}

		every_in_list = {
			list = titles_to_grant
			limit = {
				tier > tier_duchy
			}
			add = -50
		}
	}
}

number_of_courtly_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = courtly
		}
		add = 1
	}
}

number_of_participating_courtly_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = courtly
			is_participant_in_activity = root.involved_activity
		}
		add = 1
	}
}

number_of_glory_hound_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = glory_hound
		}
		add = 1
	}
}

number_of_participating_glory_hound_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = glory_hound
			is_participant_in_activity = root.involved_activity
		}
		add = 1
	}
}

number_of_parochial_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = parochial
		}
		add = 1
	}
}

number_of_participating_parochial_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = parochial
			is_participant_in_activity = root.involved_activity
		}
		add = 1
	}
}

number_of_zealot_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = zealot
		}
		add = 1
	}
}

number_of_participating_zealot_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = zealot
			is_participant_in_activity = root.involved_activity
		}
		add = 1
	}
}

number_of_minority_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = minority
		}
		add = 1
	}
}

number_of_barons_and_minor_landholders_vassals = {
	value = 0
	every_vassal = {
		limit = {
			has_vassal_stance = barons_and_minor_landholders
		}
		add = 1
	}
}

council_position_locked_value = {
	value = 25
}

marriage_fertile_age_value = {
	value = {
		add = 16
		subtract = scope:secondary_recipient.age
		add = scope:secondary_actor.age
	}
}

marriage_fertile_age_reverse_value = {
	value = {
		add = 16
		subtract = scope:secondary_actor.age
		add = scope:secondary_recipient.age
	}
}

marriage_female_fertility_cutoff_age_value = {
	value = {
		add = define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
		add = -3
		if = {
			limit = {
				has_trait = fecund
			}
			add = 5
		}
		if = {
			limit = {
				exists = dynasty
				dynasty = {
					has_dynasty_perk = blood_legacy_5
				}
			}
			add = 5
		}
	}
}

offer_vassalage_acceptance_value = {
	value = {
		add = -35
		if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 80 }
				}
			}
			add = {
				value = 33
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 70 }
				}
			}
			add = {
				value = 30
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 60 }
				}
			}
			add = {
				value = 25
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 50 }
				}
			}
			add = {
				value = 20
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 40 }
				}
			}
			add = {
				value = 15
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 30 }
				}
			}
			add = {
				value = 10
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 20 }
				}
			}
			add = {
				value = 8
			}
		}
		else_if = {
			limit = {
				scope:actor.culture = {
					cultural_acceptance = { target = scope:recipient.culture value >= 10 }
				}
			}
			add = {
				value = 5
			}
		}
	}
}

vassal_claim_liege_title_interaction_highlight_value = {
	value = {
		add = monumental_prestige_value
		if = {
			limit = {
				scope:actor.prestige_level > scope:recipient.prestige_level
			}
			subtract = {
				value = medium_prestige_value
				multiply = scope:actor.prestige_level
			}
		}
		else_if = {
			limit = {
				scope:actor.prestige_level < scope:recipient.prestige_level
			}
			add = {
				value = medium_prestige_value
				multiply = scope:recipient.prestige_level
			}
		}
	}
}

script_current_military_strength = {
	value = {
		add = current_military_strength
	}
}

one_and_a_half_times_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 1.5
	}
}

twice_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 2
	}
}

fifty_percent_more_of_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 1.5
	}
}

eighty_percent_of_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 0.8
	}
}

a_third_of_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 0.3
	}
}

forty_percent_of_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 0.4
	}
}

fifty_percent_of_current_military_strength = {
	value = {
		add = current_military_strength
		multiply = 0.5
	}
}

eighty_percent_of_max_military_strength = {
	value = {
		add = max_military_strength
		multiply = 0.8
	}
}

fifty_percent_of_max_military_strength = {
	value = {
		add = max_military_strength
		multiply = 0.5
	}
}

twenty_percent_of_max_military_strength = {
	value = {
		add = max_military_strength
		multiply = 0.2
	}
}

designate_heir_cost = 1000
designate_heir_admin_prestige_cost = 500
designate_heir_admin_influence_cost = 150
designate_heir_laamp_prestige_cost = 150

marry_close_family_tiers_acceptance_value = {
	value = 0
	
	scope:secondary_actor = {
		save_temporary_scope_as = tier_comparison_first
		every_close_family_member = {
			even_if_dead = yes
			if = {
				limit = {
					highest_held_title_tier > scope:tier_comparison_first.highest_held_title_tier
				}
				
				save_temporary_scope_as = tier_comparison_first
			}				
		}
	}
	
	scope:secondary_recipient = {
		save_temporary_scope_as = tier_comparison_second
		every_close_family_member = {
			even_if_dead = yes
			if = {
				limit = {
					highest_held_title_tier > scope:tier_comparison_second.highest_held_title_tier
				}
				
				save_temporary_scope_as = tier_comparison_second
			}				
		}
	}	
	
	add = scope:tier_comparison_first.highest_held_title_tier
	subtract = scope:tier_comparison_second.highest_held_title_tier

	if = {
		limit = {
			scope:recipient = { is_close_family_of = scope:secondary_recipient }			
		}
		
		multiply = 30
	}
	else = {
		multiply = 20
	}
}

elope_close_family_tiers_acceptance_value = {
	value = 0
	
	scope:actor = {
		save_temporary_scope_as = tier_comparison_first
		every_close_family_member = {
			even_if_dead = yes
			if = {
				limit = {
					highest_held_title_tier > scope:tier_comparison_first.highest_held_title_tier
				}
				
				save_temporary_scope_as = tier_comparison_first
			}				
		}
	}
	
	scope:recipient = {
		save_temporary_scope_as = tier_comparison_second
		every_close_family_member = {
			even_if_dead = yes
			if = {
				limit = {
					highest_held_title_tier > scope:tier_comparison_second.highest_held_title_tier
				}
				
				save_temporary_scope_as = tier_comparison_second
			}				
		}
	}	
	
	add = scope:tier_comparison_first.highest_held_title_tier
	subtract = scope:tier_comparison_second.highest_held_title_tier
	multiply = 25
}

