﻿actor_current_military_strength = {
	value = {
		add = scope:actor.current_military_strength
	}
}

recipient_current_military_strength = {
	value = {
		add = scope:recipient.current_military_strength
	}
}

actor_ally_military_strength = {
	value = 0
	scope:actor = {
		every_ally = { add = current_military_strength }
	}
}

recipient_ally_military_strength = {
	value = 0
	scope:recipient = {
		every_ally = { add = current_military_strength }
	}
}

max_strength_ninety_percent_value = {
	value = max_military_strength
	multiply = 0.9
}

max_strength_seventy_five_percent_value = {
	value = max_military_strength
	multiply = 0.9
}

current_strength_ninety_percent_value = {
	value = current_military_strength
	multiply = 0.9
}

current_strength_seventy_five_percent_value = {
	value = current_military_strength
	multiply = 0.9
}

current_strength_with_allies_value = {
	value = current_military_strength
	every_ally = { add = current_military_strength }
}

current_allies_strength_value = {
	value = 0
	every_ally = { add = current_military_strength }
}

current_strength_with_allies_fifty_percent_value = {
	value = current_military_strength
	every_ally = { add = current_military_strength }
	multiply = 0.5
}

current_strength_with_allies_seventy_five_percent_value = {
	value = current_military_strength
	every_ally = { add = current_military_strength }
	multiply = 0.75
}

recipient_strength_divided_by_actors_value = {
	value = 1
	add = {
		value = scope:recipient.current_military_strength # 1828
		scope:recipient = {
			every_ally = { add = current_military_strength }
		}
		divide = {
			value = scope:actor.current_military_strength # 1382
			scope:actor = {
				every_ally = { add = current_military_strength }
			}
			min = 1
		}
		# 1828 / 1382 = 1.32
		subtract = 1 # 1.32 - 1 = 0.32
		multiply = 100 # 0.32 * 100 = 32
		max = 50
		min = -50
	}
	max = 20
}

hostage_secondary_recipient_place_in_succession = {
	scope:recipient.primary_title = {
		if = {
			limit = {
				place_in_line_of_succession = { target = scope:secondary_recipient value = 1 }
			}
			add = 4
		}
		else_if = {
			limit = {
				place_in_line_of_succession = { target = scope:secondary_recipient value = 2 }
			}
			add = 3
		}
		else_if = {
			limit = {
				place_in_line_of_succession = { target = scope:secondary_recipient value = 3 }
			}
			add = 2
		}
		else = { add = 1 }
	}
}

hostage_secondary_actor_place_in_succession = {
	scope:actor.primary_title = {
		if = {
			limit = {
				place_in_line_of_succession = { target = scope:secondary_actor value = 1 }
			}
			add = 4
		}
		else_if = {
			limit = {
				place_in_line_of_succession = { target = scope:secondary_actor value = 2 }
			}
			add = 3
		}
		else_if = {
			limit = {
				place_in_line_of_succession = { target = scope:secondary_actor value = 3 }
			}
			add = 2
		}
		else = { add = 1 }
	}
}

demand_hostage_acceptance_value = {
	value = 10
	if = {
		limit = {
			NOR = { # Close in power
				scope:actor.current_strength_ninety_percent_value > scope:recipient.current_military_strength
				scope:actor.current_military_strength < scope:recipient.current_strength_ninety_percent_value
			}
		}
		add = 10
	}
}

bp2_yearly_6203_levy_size = {
	value = 250
	multiply = {
		value = 0
		add = scope:home_court.primary_title.tier
		if = {
			limit = {
				scope:home_court.culture = {
					has_cultural_era_or_later = culture_era_high_medieval
				}
			}
			add = 1
		}
		else_if = {
			limit = {
				scope:home_court.culture = {
					has_cultural_era_or_later = culture_era_late_medieval
				}
			}
			add = 1
		}
		subtract = 1
		min = 1
	}
}

bp2_yearly_6203_skirmishers_size = {
	value = 1
	multiply = {
		value = 0
		add = scope:home_court.primary_title.tier
		if = {
			limit = {
				scope:home_court.culture = {
					has_cultural_era_or_later = culture_era_high_medieval
				}
			}
			add = 1
		}
		else_if = {
			limit = {
				scope:home_court.culture = {
					has_cultural_era_or_later = culture_era_late_medieval
				}
			}
			add = 1
		}
		subtract = 1
		min = 1
	}
}

# Determine how much Prestige the Warden receives
# root - hostage
# scope:warden - warden
# scope:home_court - home court
hostage_prestige_income_value = {
	# Defines
	# < 20 - 0.1 per month
	# 20-49 - 0.5 per month
	# >= 50 - 1 per month

	value = 0
	if = {
		limit = {
			# No gain within dynasty
			bp2_hostage_benefits_valid_trigger = yes
		}
		# Dynasty Level Difference
		if = {
			limit = { bp2_warden_dynasty_renown_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = {
				value = scope:home_court.dynasty.dynasty_prestige_level
				subtract = scope:warden.dynasty.dynasty_prestige_level
				min = 1
				multiply = 15
				desc = HOSTAGE_DYNASTY_PRESTIGE_LEVEL_DIFFERENCES
			}
		}
		# Tier Difference
		if = {
			limit = { bp2_warden_title_tier_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = {
				value = scope:home_court.primary_title.tier
				subtract = scope:warden.primary_title.tier
				min = 1
				multiply = 15
				desc = HOSTAGE_TIER_DIFFERENCES_REASON
			}
		}
		# Prestige Level Difference
		if = {
			limit = { bp2_warden_prestige_level_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = {
				value = scope:home_court.prestige_level
				subtract = scope:warden.prestige_level
				min = 1
				multiply = 15
				desc = HOSTAGE_PRESTIGE_DIFFERENCES_REASON
			}
		}
		# Hostage from Liege or above
		if = {
			limit = {
				scope:home_court = { is_liege_or_above_of = scope:warden }
			}
			add = {
				value = 15
				desc = HOSTAGE_LIEGE_OF_WARDEN
			}
		}
		if = {
			limit = {
				scope:warden = { has_character_flag = halved_hostage_prestige_income }
			}
			add = -25
		}
	}
}

# Determines how much Piety the Warden receives
# root - hostage
# scope:warden - warden
# scope:home_court - home court
hostage_piety_income_value = {
	# Defines
	# < 20 - 0.1 per month
	# 20-49 - 0.5 per month
	# >= 50 - 1 per month

	value = 0

	if = {
		limit = {
			# No gain within same dynasty
			bp2_hostage_benefits_valid_trigger = yes
			# Piety gain only for lower renown or rank
			OR = {
				bp2_warden_dynasty_renown_lower_trigger = {
					WARDEN = scope:warden
					HOME_COURT = scope:home_court
				}
				bp2_warden_title_tier_lower_trigger = {
					WARDEN = scope:warden
					HOME_COURT = scope:home_court
				}
			}
		}
		# Faith Hostility
		if = {
			limit = { scope:warden.faith != scope:home_court.faith }
			if = {
				limit = {
					scope:warden.faith = {
						faith_hostility_level = {
							target = scope:home_court.faith
							value = faith_hostile_level
						}
					}
				}
				add = 10
			}
			else_if = {
				limit = {
					scope:warden.faith = {
						faith_hostility_level = {
							target = scope:home_court.faith
							value = faith_evil_level
						}
					}
				}
				add = 20
			}
		}
		# Dynasty Renown Difference
		if = {
			limit = { bp2_warden_dynasty_renown_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = 10
		}
		# Title Difference
		if = {
			limit = { bp2_warden_title_tier_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = 10
		}
	}
}

# Determines how much Renown the Warden's Dynasty receives
# root - hostage
# scope:warden - warden
# scope:home_court - home court
hostage_renown_income_value = {
	# Defines
	# < 20 - nothing
	# 20-49 - 0.25 per month
	# >= 50 - 0.5 per month

	value = 0

	if = { 
		limit = { bp2_hostage_benefits_valid_trigger = yes } # No gain within same dynasty
		# Dynasty Renown Difference
		if = {
			limit = { bp2_warden_dynasty_renown_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = {
				value = scope:home_court.dynasty.dynasty_prestige_level
				subtract = scope:warden.dynasty.dynasty_prestige_level
				min = 1
				multiply = 20
				desc = HOSTAGE_DYNASTY_PRESTIGE_LEVEL_DIFFERENCES
			}
		}
		# Title Tier Difference
		if = {
			limit = { bp2_warden_title_tier_lower_trigger = { WARDEN = scope:warden HOME_COURT = home_court } }
			add = {
				value = scope:home_court.primary_title.tier
				subtract = scope:warden.primary_title.tier
				min = 1
				multiply = 10
				desc = HOSTAGE_TIER_DIFFERENCES_REASON
			}
		}
	}
}

# Sorts hostages by relevance in various character lists
# root - hostage
hostage_value = {
	value = 0
	# Age inversed, younger is better, over 25 is bad
	if = {
		limit = { age != 25 }
		add = {
			value = 25
			subtract = age
		}
		desc = HOSTAGE_AGE_REASON
	}
	# Bonus for young children
	if = {
		limit = { age <= 12 }
		add = {
			value = 10
			desc = HOSTAGE_CHILD_REASON
		}
	}
	# Bonus for own children
	if = {
		limit = { is_child_of = scope:home_court }
		add = {
			value = 20
			desc = HOSTAGE_OWN_CHILD_REASON
		}
	}
	# Bonus for own grandchildren
	else_if = {
		limit = { is_grandchild_of = scope:home_court }
		add = {
			value = 10
			desc = HOSTAGE_OWN_GRANDCHILD_REASON
		}
	}
	# Line of succession
	if = {
		limit = { is_heir_of = scope:home_court }
		add = {
			value = 10
			scope:home_court.primary_title = {
				if = {
					limit = {
						place_in_line_of_succession = { target = scope:hostage value = 1 }
					}
					add = 90
				}
				else_if = {
					limit = {
						place_in_line_of_succession = { target = scope:hostage value = 2 }
					}
					add = 40
				}
				else_if = {
					limit = {
						place_in_line_of_succession = { target = scope:hostage value = 3 }
					}
					add = 15
				}
			}
			desc = HOSTAGE_SUCCESSION_PLACE_REASON
		}
	}
	# Non-inheriting
	if = {
		limit = {
			OR = {
				has_trait_with_flag = can_not_marry
				switch = {
					trigger = is_female
					yes = {
						scope:home_court = { has_realm_law = male_only_law }
					}
					no = {
						scope:home_court = { has_realm_law = female_only_law }
					}
				}
			}
		}
		add = {
			value = -50
			desc = HOSTAGE_RECIPIENT_SEX_REASON
		}
	}
	# Sickly
	if = {
		limit = {
			OR = {
				has_trait = sickly
				has_weak_physical_health_traits_trigger = yes
				has_short_disease_trigger = yes
				has_serious_disease_trigger = yes
				has_trait = incapable
				has_trait = infirm
				health <= poor_health
			}
		}
		add = {
			value = -25
			desc = HOSTAGE_SICK_REASON
		}
	}
	# Bad genes
	if = {
		limit = {
			OR = {
				has_trait = physique_bad
				has_trait = intellect_bad
				has_trait = beauty_bad
				has_trait = inbred
			}
		}
		add = {
			value = -15
			desc = HOSTAGE_BAD_GENES_REASON
		}
	}
	# Good genes/healthy
	if = {
		limit = {
			OR = {
				has_trait = physique_good
				has_trait = beauty_good
				has_trait = intellect_good
				has_trait = fecund
			}
		}
		add = {
			value = 15
			desc = HOSTAGE_GOOD_GENES_REASON
		}
	}
	# Good genes/healthy
	if = {
		limit = { health >= good_health }
		add = {
			value = 10
			if = {
				limit = { health >= excellent_health }
				add = 10
			}
			desc = HOSTAGE_HEALTHY_REASON
		}
	}
	# Dynasty Level Difference
	if = {
		limit = { bp2_warden_dynasty_renown_lower_trigger = { WARDEN = scope:warden HOME_COURT = scope:home_court } }
		add = {
			value = scope:home_court.dynasty.dynasty_prestige_level
			subtract = scope:warden.dynasty.dynasty_prestige_level
			min = 1
			multiply = 20
			desc = HOSTAGE_RENOWN_DIFFERENCES_REASON
		}
	}
	# Tier Difference
	if = {
		limit = { bp2_warden_title_tier_lower_trigger = { WARDEN = scope:warden HOME_COURT = scope:home_court } }
		add = {
			value = scope:home_court.primary_title.tier
			subtract = scope:warden.primary_title.tier
			min = 1
			multiply = 20
			desc = HOSTAGE_TIER_DIFFERENCES_REASON
		}
	}
	# Prestige Level Difference
	if = {
		limit = { bp2_warden_prestige_level_lower_trigger = { WARDEN = scope:warden HOME_COURT = scope:home_court } }
		add = {
			value = scope:home_court.prestige_level
			subtract = scope:warden.prestige_level
			min = 1
			multiply = 20
			desc = HOSTAGE_PRESTIGE_DIFFERENCES_REASON
		}
	}
	# Independence
	if = {
		limit = {
			scope:home_court = { is_independent_ruler = yes }
		}
		add = {
			value = 10
			desc = HOSTAGE_HOME_COURT_INDEPENDENT_RULER_REASON
		}
	}
	# Neighboring
	if = {
		limit = {
			scope:home_court = { character_is_land_realm_neighbor = scope:warden }
		}
		add = {
			value = 10
			desc = HOSTAGE_HOME_COURT_NEIGHBORING_RULER_REASON
		}
	}
	# Hostage from Liege or above
	if = {
		limit = {
			scope:home_court = { is_liege_or_above_of = scope:warden }
		}
		add = {
			value = 50
			desc = HOSTAGE_HOME_COURT_LIEGE_REASON
		}
	}
}

# Positive actor interaction
interaction_actor_hostage_value = {
	value = 0
	if = {
		limit = { exists = scope:secondary_actor }
		scope:actor = { save_temporary_scope_as = home_court }
		scope:recipient = { save_temporary_scope_as = warden }
		scope:secondary_actor = { save_temporary_scope_as = hostage }
		add = scope:hostage.hostage_value
		ceiling = yes
	}
}

# Negative recipient interaction
interaction_recipient_hostage_value = {
	value = 0
	if = {
		limit = { exists = scope:secondary_recipient }
		scope:actor = { save_temporary_scope_as = warden }
		scope:recipient = { save_temporary_scope_as = home_court }
		scope:secondary_recipient = { save_temporary_scope_as = hostage }
		add = scope:hostage.hostage_value
		floor = yes
		multiply = -1 # Negative as hostage value makes home court more reluctant
	}
}

# Positive actor interaction
war_actor_hostage_value = {
	value = 0
	if = {
		limit = { exists = scope:secondary_actor }
		scope:actor = { save_temporary_scope_as = home_court }
		scope:recipient = { save_temporary_scope_as = warden }
		scope:secondary_actor = { save_temporary_scope_as = hostage }
		add = scope:hostage.hostage_value
		ceiling = yes
	}
	if = { # Cowardly characters more willing to use hostages
		limit = {
			exists = scope:warden
			scope:warden.ai_boldness < 0
		}
		add = {
			value = scope:warden.ai_boldness
			multiply = 0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_BOLDNESS
	}
	if = { # Compassionate characters are less willing to use hostages
		limit = {
			exists = scope:warden
			scope:warden.ai_compassion > 0
		}
		add = {
			value = scope:warden.ai_compassion
			multiply = -0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_COMPASSION
	}
	if = { # Greedy characters are more willing to gain but not give hostage
		limit = {
			exists = scope:warden
			scope:warden.ai_greed != 0
		}
		add = {
			value = scope:warden.ai_greed
			multiply = 0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_GREED
	}
	if = { # Honorable characters are more willing to use hostages, dishonorable only to take them
		limit = {
			exists = scope:warden
			scope:warden.ai_honor != 0
		}
		add = {
			value = scope:warden.ai_honor
			if = {
				limit = { scope:warden.ai_honor < 0 }
				multiply = -0.2
			}
			else = { multiply = 0.2 }
		}

		desc = HOSTAGE_AI_VALUE_MODIFIER_HONOR
	}
	if = { # Honorable characters are more willing to use hostages, dishonorable only when to take them
		limit = {
			exists = scope:home_court
			scope:home_court.ai_honor > 0
			exists = scope:warden
			scope:home_court = { is_vassal_of = scope:warden }
			scope:warden = { has_realm_law_flag = vassal_refusal_is_treason }
		}
		add = {
			value = scope:home_court.ai_honor
			if = {
				limit = { scope:warden.ai_honor < 0 }
				multiply = -0.2
			}
			else = { multiply = 0.2 }
		}
		desc = HOSTAGE_REALM_LAW_REASON
	}
	if = { # Rational characters are more willing to use hostages
		limit = {
			exists = scope:warden
			scope:warden.ai_rationality != 0
		}
		add = {
			value = scope:warden.ai_rationality
			multiply = 0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_RATIONALITY
	}
	if = { # Special trait bonus
		limit = {
			scope:home_court = { has_trait = education_diplomacy_5 }
		}
		add = {
			value = 50
		}
		desc = HOSTAGE_HOME_COURT_EDUCATION_REASON
	}
	divide = 10
	min = 1
	max = {
		value = 15
		if = {
			limit = {
				scope:home_court = { has_trait = education_diplomacy_5 }
			}
			add = 10
		}
	}
	round = yes
}

# Negative recipient interaction
war_recipient_hostage_value = {
	value = 0
	if = {
		limit = { exists = scope:secondary_recipient }
		scope:actor = { save_temporary_scope_as = warden }
		scope:recipient = { save_temporary_scope_as = home_court }
		scope:secondary_recipient = { save_temporary_scope_as = hostage }
		add = scope:hostage.hostage_value
		floor = yes
		multiply = -1 # Negative as hostage value makes home court more reluctant
	}
	# -7
	if = { # Cowardly characters more willing to use hostages
		limit = {
			exists = scope:home_court
			scope:home_court.ai_boldness < 0
		}
		add = {
			value = scope:home_court.ai_boldness
			multiply = -0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_BOLDNESS
	}
	if = { # Compassionate characters are less willing to use hostages
		limit = {
			exists = scope:home_court
			scope:home_court.ai_compassion > 0
		}
		add = {
			value = scope:home_court.ai_compassion
			multiply = -0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_COMPASSION
	}
	if = { # Greedy characters are more willing to gain but not give hostage
		limit = {
			exists = scope:home_court
			scope:home_court.ai_greed != 0
		}
		add = {
			value = scope:home_court.ai_greed
			multiply = -0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_GREED
	}
	if = { # Honorable characters are more willing to use hostages, dishonorable only to take them
		limit = {
			exists = scope:home_court
			scope:home_court.ai_honor != 0
		}
		add = {
			value = scope:home_court.ai_honor
			multiply = 0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_HONOR
	}
	if = { # Honorable characters are more willing to use hostages, dishonorable only when to take them
		limit = {
			exists = scope:home_court
			scope:home_court.ai_honor > 0
			exists = scope:warden
			scope:home_court = { is_vassal_of = scope:warden }
			scope:warden = { has_realm_law_flag = vassal_refusal_is_treason }
		}
		add = {
			value = scope:home_court.ai_honor
			multiply = 0.2
		}
		desc = HOSTAGE_REALM_LAW_REASON
	}
	if = { # Rational characters are more willing to use hostages
		limit = {
			exists = scope:home_court
			scope:home_court.ai_rationality != 0
		}
		add = {
			value = scope:home_court.ai_rationality
			multiply = 0.2
		}
		desc = HOSTAGE_AI_VALUE_MODIFIER_RATIONALITY
	}
	divide = 10
	max = -1
	min = -15
	round = yes
}

# Negative recipient interaction
interaction_relative_hostage_value = {
	value = 0
	add = interaction_actor_hostage_value
	subtract = interaction_recipient_hostage_value
	round = yes
}

# Negative recipient interaction
call_to_arms_hostage_value = {
	home_court = { save_temporary_scope_as = home_court }
	warden = { save_temporary_scope_as = warden }
	save_temporary_scope_as = hostage
	value = hostage_value
}

bp2_hostage_loyalty_hook_chance_value = {
	value = 20
	# Time - max +35
	if = {
		limit = {
			hostage_duration = { years >= 2 }
		}
		add = 5
	}
	if = {
		limit = {
			hostage_duration = { years >= 4 }
		}
		add = 5
	}
	if = {
		limit = {
			hostage_duration = { years >= 6 }
		}
		add = 5
	}
	if = {
		limit = {
			hostage_duration = { years >= 8 }
		}
		add = 5
	}
	if = {
		limit = {
			hostage_duration = { years >= 10 }
		}
		add = 10
	}
	if = {
		limit = {
			hostage_duration = { years >= 14 }
		}
		add = 10
	}
	# Traits - max +30
	if = {
		limit = { has_trait = loyal }
		add = 10
	}
	else_if = {
		limit = { has_trait = disloyal }
		add = -10
	}
	if = {
		limit = { has_trait = trusting }
		add = 10
	}
	else_if = {
		limit = { has_trait = paranoid }
		add = -10
	}
	if = {
		limit = { has_trait = just }
		add = 10
	}
	else_if = {
		limit = { has_trait = arbitrary }
		add = -10
	}
	# Opinion - max +33
	if = {
		limit = { exists = scope:warden_opinion }
		add = {
			value = scope:warden_opinion
			multiply = 0.33
		}
	}
}

hostage_adulthood_retained_halved_prestige_duration_value = 10

offer_hostage_military_strength_value = {
	value = current_military_strength
	multiply = 1.5
}

demand_hostage_military_strength_value = {
	value = current_military_strength
	multiply = 0.65
}

hostage_tier_check_value = {
	value = highest_held_title_tier
	subtract = 1
	min = tier_county
}
