﻿######################
# TOURNAMENT STUFF
######################

# Contest Aptitude Values

contest_aptitude_fetch_value = { 
	value = 0
	if = {
		limit = { exists = var:contest_aptitude }
		add = var:contest_aptitude
	}
	min = 1
}

contest_aptitude_fetch_half_value = { 
	value = contest_aptitude_fetch_value
	divide = 2
	min = 1
}

progress_to_victory_fetch_value = {
	value = 0
	if = {
		limit = { exists = var:contest_aptitude }
		add = var:contest_aptitude
	}
	min = 1
}

progress_to_victory_fetch_half_value = { 
	value = progress_to_victory_fetch_value
	divide = 2
	min = 1
}

progress_to_victory_fetch_team_a_half_value = { 
	value = var:team_a_ptv
	divide = 2
	min = 1
}

progress_to_victory_fetch_team_b_half_value = { 
	value = var:team_b_ptv
	divide = 2
	min = 1
}

progress_to_victory_total_ffa_current_active_phase = {
	value = 0
	involved_activity = {
		every_guest_subset_current_phase = {
			name = qualified
			limit = { exists = var:progress_to_victory }
			add = var:progress_to_victory
		}
	}
	min = 1
}

progress_to_victory_own_team_ptv_value = {
	value = 0
	if = {
		limit = {
			is_in_guest_subset = { name = team_alpha }
		}
		add = involved_activity.var:team_a_ptv
	}
	else_if = {
		limit = {
			is_in_guest_subset = { name = team_beta }
		}
		add = involved_activity.var:team_b_ptv
	}
}

progress_to_victory_other_team_ptv_value = {
	value = 0
	if = {
		limit = {
			is_in_guest_subset = { name = team_alpha }
		}
		add = involved_activity.var:team_b_ptv
	}
	else_if = {
		limit = {
			is_in_guest_subset = { name = team_beta }
		}
		add = involved_activity.var:team_a_ptv
	}
}

progress_to_victory_total_team_current_active_phase = {
	value = 0
	if = {
		limit = {
			exists = involved_activity.var:team_a_ptv
			exists = involved_activity.var:team_b_ptv
		}
		add = involved_activity.var:team_a_ptv
		add = involved_activity.var:team_b_ptv
	}
	min = 1
}

progress_to_victory_versus_match_value = {
	if = {
		limit = {
			involved_activity = { has_current_phase = tournament_phase_joust }
		}
		# Compare with correct rounds match
		if = {
			limit = { involved_activity.var:contest_versus_progress ?= 2 }
			if = {
				limit = { is_in_guest_subset = { name = winner } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_finalist_match_joust.var:progress_to_victory }
				add = var:contest_finalist_match_joust.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 1 }
			if = {
				limit = { is_in_guest_subset = { name = finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_semi_finalist_match_joust.var:progress_to_victory }
				add = var:contest_semi_finalist_match_joust.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 0 }
			if = {
				limit = { is_in_guest_subset = { name = semi_finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_qualified_match_joust.var:progress_to_victory }
				add = var:contest_qualified_match_joust.var:progress_to_victory
			}
		}
		# Use own to make it 50% for UI as a fallback
		else = { add = var:progress_to_victory }
	}
	if = {
		limit = {
			involved_activity = { has_current_phase = tournament_phase_duel }
		}
		# Compare with correct rounds match
		if = {
			limit = { involved_activity.var:contest_versus_progress ?= 2 }
			if = {
				limit = { is_in_guest_subset = { name = winner } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_finalist_match_duel.var:progress_to_victory }
				add = var:contest_finalist_match_duel.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 1 }
			if = {
				limit = { is_in_guest_subset = { name = finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_semi_finalist_match_duel.var:progress_to_victory }
				add = var:contest_semi_finalist_match_duel.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 0 }
			if = {
				limit = { is_in_guest_subset = { name = semi_finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_qualified_match_duel.var:progress_to_victory }
				add = var:contest_qualified_match_duel.var:progress_to_victory
			}
		}
		# Use own to make it 50% for UI as a fallback
		else = { add = var:progress_to_victory }
	}
	if = {
		limit = {
			involved_activity = { has_current_phase = tournament_phase_wrestling }
		}
		# Compare with correct rounds match
		if = {
			limit = { involved_activity.var:contest_versus_progress ?= 2 }
			if = {
				limit = { is_in_guest_subset = { name = winner } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_finalist_match_wrestling.var:progress_to_victory }
				add = var:contest_finalist_match_wrestling.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 1 }
			if = {
				limit = { is_in_guest_subset = { name = finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_semi_finalist_match_wrestling.var:progress_to_victory }
				add = var:contest_semi_finalist_match_wrestling.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 0 }
			if = {
				limit = { is_in_guest_subset = { name = semi_finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_qualified_match_wrestling.var:progress_to_victory }
				add = var:contest_qualified_match_wrestling.var:progress_to_victory
			}
		}
		# Use own to make it 50% for UI as a fallback
		else = { add = var:progress_to_victory }
	}
	if = {
		limit = {
			involved_activity = { has_current_phase = tournament_phase_board_game }
		}
		# Compare with correct rounds match
		if = {
			limit = { involved_activity.var:contest_versus_progress ?= 2 }
			if = {
				limit = { is_in_guest_subset = { name = winner } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_finalist_match_board_game.var:progress_to_victory }
				add = var:contest_finalist_match_board_game.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 1 }
			if = {
				limit = { is_in_guest_subset = { name = finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_semi_finalist_match_board_game.var:progress_to_victory }
				add = var:contest_semi_finalist_match_board_game.var:progress_to_victory
			}
		}
		else_if = {
			limit = { involved_activity.var:contest_versus_progress ?= 0 }
			if = {
				limit = { is_in_guest_subset = { name = semi_finalist } }
				add = -100
			}
			else_if = {
				limit = { exists = var:contest_qualified_match_board_game.var:progress_to_victory }
				add = var:contest_qualified_match_board_game.var:progress_to_victory
			}
		}
		# Use own to make it 50% for UI as a fallback
		else = { add = var:progress_to_victory }
	}
	min = 0
}

## Referenced to in the UI
# Returns the scoped character's chance of winning any contest based on the 
# their PTV against the opposing PTV. 
#
# Calculate as: 
# team: team_ptv / ( team_ptv + opposing_team_ptv )
# ffa: root.var:ptv / ( sum( scope:all_qualified.var:ptv ) )
# 1v1: root.var:ptv / ( root.var:ptv + scope:opponent.var:ptv )
character_chance_of_victory = {
	value = 0
	if = {
		limit = {
			is_in_guest_subset = { name = qualified }
			exists = var:progress_to_victory
		}
		# TEAM
		if = {
			limit = {
				involved_activity = { has_current_phase = tournament_phase_melee }
			}
			# Fetch own team's PtV
			add = progress_to_victory_own_team_ptv_value
			# Divide by both team's PtV
			divide = progress_to_victory_total_team_current_active_phase
		}
		# TURN/RACE
		else_if = {
			limit = {
				involved_activity = {
					OR = {
						has_current_phase = tournament_phase_archery
						has_current_phase = tournament_phase_recital
						has_current_phase = tournament_phase_horse_race
					}
				}
			}
			# Fetch own PtV
			add = var:progress_to_victory
			# Divide by combined contestant's PtV
			divide = progress_to_victory_total_ffa_current_active_phase
		}
		# VERSUS
		else_if = {
			limit = { exists = var:progress_to_victory }
			# Fetch own PtV
			add = var:progress_to_victory
			# Divide by own + matches PtV
			divide = {
				value = var:progress_to_victory
				add = progress_to_victory_versus_match_value
			}
		}
	}
}

## Referenced in the Debug UI
# Returns the PTV of the scoped character's opponent in any situation. 
# This is the pure PTV for the opponents, no their chance of victory.
#
# Return as:
# team: opposing_team_ptv
# ffa: sum( scope:all_qualified.var:ptv ) - root.var:ptv
# 1v1: scope:opponent.var:ptv

debug_opposing_progress_to_victory = {
	value = 0
	if = {
		limit = {
			is_in_guest_subset = { name = qualified }
		}
		# TEAM
		if = {
			limit = {
				involved_activity = { has_current_phase = tournament_phase_melee }
			}
			# Fetch other team's PtV
			add = progress_to_victory_other_team_ptv_value
		}
		# TURN/RACE
		else_if = {
			limit = {
				exists = var:progress_to_victory
				involved_activity = {
					OR = {
						has_current_phase = tournament_phase_archery
						has_current_phase = tournament_phase_recital
						has_current_phase = tournament_phase_horse_race
					}
				}
			}
			# Fetch combined contestant's PtV, except yours
			add = progress_to_victory_total_ffa_current_active_phase
			subtract = var:progress_to_victory
		}
		# VERSUS
		# Fetch matched contestant's PtV
		else = { add = progress_to_victory_versus_match_value }
	}
	min = 1
}

count_remaining_contestants = {
	value = 0
	involved_activity = {
		every_guest_subset_current_phase = {
			name = qualified
			limit = { 
				AND = { 
					exists = var:progress_to_victory 
					var:progress_to_victory > 0
				}
			}
			add = 1
		}
	}
}

# 	The end result of this number MUST be between 0 and 5, otherwise the game
# 	will crash due to being used in the UI to allocate a segmented progress bar
contest_type_aptitude_wit = {
	value = 0
	# Score
	if = {
		limit = {
			involved_activity ?= {
				OR = {
					has_current_phase = tournament_phase_board_game
					has_current_phase = tournament_phase_recital
				}
			}
			exists = var:progress_to_victory_score
			var:progress_to_victory_score >= contest_progress_increase_medium_value
		}
		add = {
			value = 1
			if = {
				limit = { var:progress_to_victory_score >= contest_progress_increase_massive_value }
				add = 1
			}
			desc = tournament_aptitude.events
		}
	}
	# Trait
	if = {
		limit = {
			has_trait_xp = {
				trait = tourney_participant
				track = wit
				value >= 25
			}
		}
		add = {
			value = 1
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = wit
						value >= 50
					}
				}
				add = 1
			}
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = wit
						value >= 75
					}
				}
				add = 1
			}
			desc = tourney_participant_wit
		}
	}
	#Primary skill without Hastiludes
	if = { # Diplomacy
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 25
				}
			}
			diplomacy >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { diplomacy >= high_skill_rating }
				add = 1
			}
			desc = diplomacy_concept
		}
	}
	if = { # Learning
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 25
				}
			}
			learning >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { learning >= high_skill_rating }
				add = 1
			}
			desc = learning_concept
		}
	}
	# Lifestyle & Genetic
	if = {
		limit = {
			involved_activity ?= { has_current_phase = tournament_phase_recital }
			has_trait = poet
		}
		add = {
			value = 1
			desc = trait_poet
		}
	}
	if = {
		limit = {
			OR = {
				has_trait = intellect_good_1
				has_trait = intellect_good_2
				has_trait = intellect_good_3
				has_trait = shrewd
			}
		}
		add = {
			value = 1
			desc = trait_shrewd
		}
	}
	# Disability & Misc.
	if = {
		limit = {
			involved_activity ?= { has_current_phase = tournament_phase_recital }
			has_trait = lisping
		}
		add = {
			value = -1
			desc = trait_lisping
		}
	}
	if = {
		limit = {
			involved_activity ?= { has_current_phase = tournament_phase_recital }
			has_trait = stuttering
		}
		add = {
			value = -1
			desc = trait_stuttering
		}
	}
	if = {
		limit = { has_trait = dull }
		add = {
			value = -1
			desc = trait_dull
		}
	}

	max = 5
}

# 	The end result of this number MUST be between 0 and 5, otherwise the game
# 	will crash due to being used in the UI to allocate a segmented progress bar
contest_type_aptitude_foot = {
	value = 0
	# Score
	if = {
		limit = {
			involved_activity ?= {
				OR = {
					has_current_phase = tournament_phase_duel
					has_current_phase = tournament_phase_wrestling
				}
			}
			exists = var:progress_to_victory_score
			var:progress_to_victory_score >= contest_progress_increase_medium_value
		}
		add = {
			value = 1
			if = {
				limit = { var:progress_to_victory_score >= contest_progress_increase_massive_value }
				add = 1
			}
			desc = tournament_aptitude.events
		}
	}
	else_if = {
		limit = {
			involved_activity ?= { has_current_phase = tournament_phase_melee }
			exists = var:progress_to_victory_score
			var:progress_to_victory_score >= contest_progress_increase_medium_value
		}
		add = {
			value = 0.5
			if = {
				limit = { var:progress_to_victory_score >= contest_progress_increase_massive_value }
				add = 0.5
			}
			desc = tournament_aptitude.events
		}
	}
	# Trait
	if = {
		limit = {
			has_trait_xp = {
				trait = tourney_participant
				track = foot
				value >= 25
			}
		}
		add = {
			value = 1
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = foot
						value >= 50
					}
				}
				add = 1
			}
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = foot
						value >= 75
					}
				}
				add = 1
			}
			desc = tourney_participant_foot
		}
	}
	#Primary skill without Hastiludes
	if = { # Prowess
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 25
				}
			}
			prowess >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { prowess >= high_skill_rating }
				add = 1
			}
			desc = prowess_concept
		}
	}
	if = { # Martial
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 25
				}
			}
			martial >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { martial >= high_skill_rating }
				add = 1
			}
			desc = martial_concept
		}
	}
	# Lifestyle & Genetic
	if = {
		limit = { has_trait = lifestyle_blademaster }
		add = {
			value = 1
			desc = trait_lifestyle_blademaster
		}
	}
	if = {
		limit = {
			OR = {
				has_trait = physique_good_1
				has_trait = physique_good_2
				has_trait = physique_good_3
				has_trait = strong
			}
		}
		add = {
			value = 1
			desc = trait_strong
		}
	}

	# Disability & Misc.
	if = {
		limit = { has_trait = weak }
		add = {
			value = -1
			desc = trait_weak
		}
	}
	if = {
		limit = { has_trait = maimed }
		add = {
			value = -1
			desc = trait_maimed
		}
	}
	if = {
		limit = { has_trait = giant }
		add = {
			value = 1
			desc = trait_giant
		}
	}
	if = {
		limit = {
			has_trait = viking
			culture ?= { has_innovation = innovation_longboats }
		}
		add = {
			value = 1
			desc = trait_viking_has_longships
		}
	}
	if = {
		limit = {
			has_trait = viking
			exists = culture
			NOT = {
				culture = { has_innovation = innovation_longboats }
			}
		}
		add = {
			value = 1
			desc = trait_viking_fallback
		}
	}
	if = {
		limit = { has_trait = faith_warrior }
		add = {
			value = 1
			desc = trait_faith_warrior
		}
	}
	if = {
		limit = { has_trait = berserker }
		add = {
			value = 1
			desc = trait_berserker
		}
	}

	max = 5
	min = 0
}

# 	The end result of this number MUST be between 0 and 5, otherwise the game
# 	will crash due to being used in the UI to allocate a segmented progress bar
contest_type_aptitude_horse = {
	value = 0
	# Score
	if = {
		limit = {
			involved_activity ?= {
				OR = {
					has_current_phase = tournament_phase_horse_race
					has_current_phase = tournament_phase_joust
				}
			}
			exists = var:progress_to_victory_score
			var:progress_to_victory_score >= contest_progress_increase_medium_value
		}
		add = {
			value = 1
			if = {
				limit = { var:progress_to_victory_score >= contest_progress_increase_massive_value }
				add = 1
			}
			desc = tournament_aptitude.events
		}
	}
	else_if = {
		limit = {
			involved_activity ?= { has_current_phase = tournament_phase_melee }
			exists = var:progress_to_victory_score
			var:progress_to_victory_score >= contest_progress_increase_medium_value
		}
		add = {
			value = 0.5
			if = {
				limit = { var:progress_to_victory_score >= contest_progress_increase_massive_value }
				add = 0.5
			}
			desc = tournament_aptitude.events
		}
	}
	# Trait
	if = {
		limit = {
			has_trait_xp = {
				trait = tourney_participant
				track = horse
				value >= 25
			}
		}
		add = {
			value = 1
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = horse
						value >= 50
					}
				}
				add = 1
			}
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = horse
						value >= 75
					}
				}
				add = 1
			}
			desc = tourney_participant_foot
		}
	}
	if = { # Prowess
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 25
				}
			}
			prowess >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { prowess >= high_skill_rating }
				add = 1
			}
			desc = prowess_concept
		}
	}
	if = { # Martial
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 25
				}
			}
			martial >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { martial >= high_skill_rating }
				add = 1
			}
			desc = martial_concept
		}
	}
	# Lifestyle & Genetic
	if = {
		limit = { has_trait = adventurer } #Bit of a stretch, I know
		add = {
			value = 1
			desc = trait_adventurer
		}
	}
	if = {
		limit = {
			OR = {
				has_trait = education_martial_prowess_1
				has_trait = education_martial_prowess_2
				has_trait = education_martial_prowess_3
				has_trait = education_martial_prowess_4
			}
		}
		add = {
			value = 1
			if = {
				limit = { has_trait = education_martial_prowess_2 }
				add = 1
			}
			else_if = {
				limit = { has_trait = education_martial_prowess_3 }
				add =  1
			}
			else_if = {
				limit = { has_trait = education_martial_prowess_4 }
				add = 1
			}
			desc = trait_education_martial_prowess
		}
	}
	# Disability & Misc.
	if = {
		limit = { has_trait = dwarf }
		add = {
			value = 1
			desc = trait_dwarf
		}
	}
	if = {
		limit = { has_trait = giant }
		add = {
			value = -1
			desc = trait_giant
		}
	}

	max = 5
	min = 0
}

# 	The end result of this number MUST be between 0 and 5, otherwise the game
# 	will crash due to being used in the UI to allocate a segmented progress bar
contest_type_aptitude_archery = {
	value = 0
	# Score
	if = {
		limit = {
			involved_activity ?= { has_current_phase = tournament_phase_archery }
			exists = var:progress_to_victory_score
			var:progress_to_victory_score >= contest_progress_increase_medium_value
		}
		add = {
			value = 1
			if = {
				limit = { var:progress_to_victory_score >= contest_progress_increase_massive_value }
				add = 1
			}
			desc = tournament_aptitude.events
		}
	}
	# Trait
	if = {
		limit = {
			has_trait_xp = {
				trait = tourney_participant
				track = bow
				value >= 25
			}
		}
		add = {
			value = 1
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = bow
						value >= 50
					}
				}
				add = 1
			}
			if = {
				limit = {
					has_trait_xp = {
						trait = tourney_participant
						track = bow
						value >= 75
					}
				}
				add = 1
			}
			desc = tourney_participant_bow
		}
	}
	# Trait
	if = {
		limit = {
			has_trait_xp = {
				trait = tourney_participant
				track = bow
				value >= 25
			}
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 25
				}
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value < 50
				}
			}
			add = {
				value = 1
				desc = tourney_participant_foot
			}
		}
		else_if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 50
				}
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value < 75
				}
			}
			add = {
				value = 2
				desc = tourney_participant_bow
			}
		}
		else = {
			add = {
				value = 3
				desc = tourney_participant_bow
			}
		}
	}
	#Primary skill without Hastiludes
	if = { # Prowess
		limit = {
			NOT = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 25
				}
			}
			prowess >= medium_skill_rating
		}
		add = {
			value = 1
			if = {
				limit = { prowess >= high_skill_rating }
				add = 1
			}
			desc = prowess_concept
		}
	}
	# Lifestyle & Genetic
	if = {
		limit = { 
			has_trait_xp = {
				trait = lifestyle_hunter
				track = hunter
				value >= 25
	        }
        }
		add = {
			value = 1
			desc = trait_lifestyle_hunter
		}
	}

	# Disability & Misc.
	if = {
		limit = { has_trait = strong }
		add = {
			value = 1
			desc = trait_strong
		}
	}
	if = {
		limit = { has_trait = weak }
		add = {
			value = -1
			desc = trait_weak
		}
	}
	if = {
		limit = { has_trait = one_eyed }
		add = {
			value = -1
			desc = trait_one_eyed
		}
	}
	if = {
		limit = { has_trait = maimed }
		add = {
			value = -5
			desc = trait_maimed
		}
	}

	max = 5
	min = 0
}

contest_health_aptitude_offset_value = { 
	# Health
	if = {
		limit = { health >= excellent_health }
		add = 5
	}
	else_if = {
		limit = {
			health >= good_health
			health < excellent_health
		}
		add = 3
	}
	else_if = {
		limit = {	
			health < fine_health
			health > poor_health
		}
		add = -5
	}
	else_if = {
		limit = {
			health <= poor_health
			health > dying_health
		}
		add = -10
	}
	else_if = {
		limit = { health <= dying_health }
		add = -20
	}
}

contest_foot_aptitude_value = {

	### Base ###
	# Prowess
	value = contest_base_aptitude_value
	add = {
		value = prowess_no_portrait # exclude artifacts
		multiply = 1.5
	}

	### Additional Modifiers ###
	# Artifacts - only own armor is used
	if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = illustrious
			}
		}
		add = 20
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = famed
			}
		}
		add = 15
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = masterwork
			}
		}
		add = 10
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = common
			}
		}
		add = 5
	}
	# Tourney Trait - Foot
    if = {
		limit = { has_trait = tourney_participant }
		add = 5
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 20
				}
			}
			add = 10
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 50
				}
			}
			add = 20
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 80
				}
			}
			add = 20
		}
	}
	# Strength
	if = {
		limit = { has_trait = giant }
		add = 4
	}
	if = {
		limit = { has_trait = strong }
		add = 4
	}
	if = {
		limit = { has_trait = viking }
		add = 2
	}
	if = {
		limit = { has_trait = faith_warrior }
		add = 2
	}
	if = {
		limit = { has_trait = berserker }
		add = 4
	}
	if = {
		limit = { has_trait = physique_good }
		if = {
			limit = { has_trait = physique_good_1 }
			add = 3
		}
		else_if = {
			limit = { has_trait = physique_good_2 }
			add = 6
		}
		else_if = {
			limit = { has_trait = physique_good_3 }
			add = 9
		}
	}
	# Lifestyle
	if = {
		limit = { has_trait = lifestyle_blademaster }
		add = 4
		if = {
			limit = {
				has_trait_xp = {
					trait = lifestyle_blademaster
					value >= 50
				}
			}
			add = 8
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = lifestyle_blademaster
					value >= 100
				}
			}
			add = 12
		}	
	}
	# Health
	add = contest_health_aptitude_offset_value
	# Favors
	every_character_artifact = {
		limit = { artifact_type = tournament_favor }
		add = 5
	}
	# Modifiers
    add = shared_tournament_aptitude_modifiers_value
	# Other
	if = {
		limit = { has_character_modifier = tournament_armorer_tournament_modifier }
		add = 10
	}

	### Boundaries ###
	min = 1
}

contest_melee_aptitude_value = {

	value = contest_foot_aptitude_value
	add = contest_horse_aptitude_value
	divide = 2

	### Boundaries ###
	min = 1
}

contest_archery_aptitude_value = {

	### Base ###
	# Prowess
	value = contest_base_aptitude_value 
	add = {
		value = prowess_no_portrait # exclude artifacts
		multiply = 1.5
	}

	### Additional Modifiers ###
	# Artifacts - only own bows are used
	if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = primary_armament
				OR = {
					artifact_type = longbow
					artifact_type = composite
					artifact_type = bow
				}
				rarity = illustrious
			}
		}
		add = 20
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = primary_armament
				OR = {
					artifact_type = longbow
					artifact_type = composite
					artifact_type = bow
				}
				rarity = famed
			}
		}
		add = 15
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = primary_armament
				OR = {
					artifact_type = longbow
					artifact_type = composite
					artifact_type = bow
				}
				rarity = masterwork
			}
		}
		add = 10
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = primary_armament
				OR = {
					artifact_type = longbow
					artifact_type = composite
					artifact_type = bow
				}
				rarity = common
			}
		}
		add = 5
	}
	# Tourney Trait - Bow
    if = {
		limit = { has_trait = tourney_participant }
		add = 5
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 20
				}
			}
			add = 10
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 50
				}
			}
			add = 15
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 80
				}
			}
			add = 20
		}
	}
	# Strength
	if = {
		limit = { has_trait = strong }
		add = 5
	}
	if = {
		limit = { has_trait = physique_good }
		if = {
			limit = { has_trait = physique_good_1 }
			add = 4
		}
		else_if = {
			limit = { has_trait = physique_good_2 }
			add = 6
		}
		else_if = {
			limit = { has_trait = physique_good_3 }
			add = 9
		}
	}
    # Lifestyle
	if = {
		limit = { has_trait = tourney_participant }
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 25
				}
			}
			add = 7
		}
		else_if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 50
				}
			}
			add = 14
		}
		else_if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 75
				}
			}
			add = 21
		}	
	}
	# Personality
	if = {
        limit = { has_trait = patient }
        add = 3
    }
	# Health
	add = contest_health_aptitude_offset_value
	# Modifiers
    add = shared_tournament_aptitude_modifiers_value
	### Boundaries ###
	min = 1
}

contest_pugilism_aptitude_value = {

	### Base ###
	# Prowess
	value = contest_base_aptitude_value 
	add = {
		value = prowess_no_portrait # exclude artifacts
		multiply = 1.5
	}

	### Additional Modifiers ###
	# Tourney Trait - Foot
    if = {
		limit = { has_trait = tourney_participant }
		add = 5
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 20
				}
			}
			add = 10
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 50
				}
			}
			add = 15
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = foot
					value >= 80
				}
			}
			add = 20
		}
	}
	# Strength
	if = {
		limit = { has_trait = giant }
		add = 10
	}
	if = {
		limit = { has_trait = strong }
		add = 5
	}
	if = {
		limit = { has_trait = physique_good }
		if = {
			limit = { has_trait = physique_good_1 }
			add = 3
		}
		else_if = {
			limit = { has_trait = physique_good_2 }
			add = 6
		}
		else_if = {
			limit = { has_trait = physique_good_3 }
			add = 9
		}
	}
	# Personality
	if = {
        limit = { has_trait = wrathful }
        add = 3
    }
    if = {
        limit = { has_trait = deceitful }
        add = 3
    }
    if = {
        limit = { has_trait = patient }
        add = 3
    }
	# Health
	add = contest_health_aptitude_offset_value
	# Modifiers
    add = shared_tournament_aptitude_modifiers_value
	### Boundaries ###
	min = 1
}

contest_horse_aptitude_value = {

	### Base ###
	# Prowess
	value = contest_base_aptitude_value 
	add = {
		value = prowess_no_portrait # exclude artifacts
		multiply = 0.5
	}

	### Additional Modifiers ###
	# Artifacts - only own armor is used
	if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = illustrious
			}
		}
		add = 20
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = famed
			}
		}
		add = 15
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = masterwork
			}
		}
		add = 10
	}
	else_if = {
		limit = {
			any_character_artifact = {
				artifact_slot_type = armor
				rarity = common
			}
		}
		add = 5
	}
	# Tourney Trait - Horse
    if = {
		limit = { has_trait = tourney_participant }
		add = 5
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = horse
					value >= 20
				}
			}
			add = 10
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = horse
					value >= 50
				}
			}
			add = 15
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = horse
					value >= 80
				}
			}
			add = 20
		}
	}
	# Education
    if = {
		limit = { has_trait = education_martial_prowess }
		if = {
			limit = { has_trait = education_martial_prowess_1 }
			add = 3
		}
		else_if = {
			limit = { has_trait = education_martial_prowess_2 }
			add = 6
		}
		else_if = {
			limit = { has_trait = education_martial_prowess_3 }
			add = 9
		}
		else = { add = 12 }
	}
	# Content
	if = {
        limit = { has_character_modifier = improved_horse_mastery_modifier }
        add = 5
    }
    if = {
        limit = { has_character_modifier = cavalry_charge_exercise_modifier }
        add = 5
    }
    if = {
        limit = { has_character_modifier = mounted_warrior_practice_modifier }
        add = 5
    }
    if = {
        limit = { has_character_modifier = tournament_farrier_mistake_modifier }
        add = farrier_mistake_value
    }

	# Personality
	if = {
        limit = { has_trait = calm }
        add = 3
    }
    if = {
        limit = { has_trait = compassionate }
        add = 3
    }
    if = {
        limit = { has_trait = patient }
        add = 3
    }
	# Health
	add = contest_health_aptitude_offset_value

	# Modifiers
    add = shared_tournament_aptitude_modifiers_value

	### Boundaries ###
	min = 1
}

contest_recital_aptitude_value = {

	### Base ###
	# Prowess
	value = contest_base_aptitude_value 
	add = {
		value = diplomacy
	}

	### Additional Modifiers ###
	# Tourney Trait - Wit
    if = {
		limit = { has_trait = tourney_participant }
		add = 5
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 20
				}
			}
			add = 10
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 50
				}
			}
			add = 15
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 80
				}
			}
			add = 20
		}
	}
	# Lifestyle
	if = {
		limit = { has_trait = poet }
		add = 20
	}
	# Disability
	if = {
		limit = { has_trait = lisping }
		add = -10
	}
	if = {
		limit = { has_trait = stuttering }
		add = -10
	}

	# Modifiers
    add = shared_tournament_aptitude_modifiers_value

	### Boundaries ###
	min = 1
}

contest_board_game_aptitude_value = {

	### Base ###
	# Prowess
	value = contest_base_aptitude_value 
	add = {
		value = learning
		multiply = 1.5
	}

	### Additional Modifiers ###
	# Tourney Trait - Wit
    if = {
		limit = { has_trait = tourney_participant }
		add = 5
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 20
				}
			}
			add = 10
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 50
				}
			}
			add = 15
		}
		if = {
			limit = {
				has_trait_xp = {
					trait = tourney_participant
					track = wit
					value >= 80
				}
			}
			add = 20
		}
	}

	# Modifiers
    add = shared_tournament_aptitude_modifiers_value

	### Boundaries ###
	min = 1
}

contest_horse_race_aptitude_value = {

	### Base ###
	# Prowess
	value = 0
	add = primary_title.tier
	multiply = contest_base_aptitude_value

	### Additional Modifiers ###
	# Education
    if = {
		limit = { has_trait = education_stewardship }
		if = {
			limit = { has_trait = education_stewardship_1 }
			add = 2
		}
		else_if = {
			limit = { has_trait = education_stewardship_2 }
			add = 5
		}
		else_if = {
			limit = { has_trait = education_stewardship_3 }
			add = 8
		}
		else = { add = 10 }
	}
	if = {
		limit = { has_character_flag = contest_horse_race_war_horse }
		if = {
			limit = { has_character_modifier = horse_agility_agility_modifier }
			add = 20
		}
		else_if ={
			limit = { has_character_modifier = horse_endurance_agility_modifier }
			add = 15
		}
		else_if ={
			limit = { has_character_modifier = horse_endurance_endurance_modifier }
			add = 10
		}
		else_if ={
			limit = {
				OR = {
					has_character_modifier = horse_agility_modifier
					has_character_modifier = horse_endurance_modifier
					has_character_modifier = horse_agility_strength_modifier
					has_character_modifier = horse_fierceness_agility_modifier
					has_character_modifier = horse_fierceness_endurance_modifier
					has_character_modifier = horse_endurance_strength_modifier
				}
			}
			add = 5
		}
	}
	# Personality
	if = {
        limit = { has_trait = diligent }
        add = 3
    }
    if = {
        limit = { has_trait = overseer }
        add = 3
    }
    if = {
        limit = { has_trait = administrator }
        add = 3
    }
    # Flavor
    if = {
    	limit = {
    		exists = var:contest_horse_race_name
    		var:contest_horse_race_name = flag:seianian
    	}
    	multiply = 0.25
    }
    # Flavor
    if = {
    	limit = {
    		exists = var:horse_race_jockey
    		var:horse_race_jockey = { is_alive = yes }
    	}
    	add = var:horse_race_jockey.contest_horse_aptitude_value
    }
	### Boundaries ###
	min = 1
}

contest_current_contest_aptitude_value = {

	value = 0
	if = {
		limit = { scope:activity = { has_current_phase = tournament_phase_joust } }
		add = contest_horse_aptitude_value
	}
	else_if = {
		limit = {
			scope:activity = {
				OR = {
					has_current_phase = tournament_phase_duel
					has_current_phase = tournament_phase_melee
				}
			}
		}
		add = contest_foot_aptitude_value
	}
	else_if = {
		limit = { scope:activity = { has_current_phase = tournament_phase_board_game } }
		add = contest_board_game_aptitude_value
	}
	else_if = {
		limit = { scope:activity = { has_current_phase = tournament_phase_recital } }
		add = contest_recital_aptitude_value
	}
	else_if = {
		limit = { scope:activity = { has_current_phase = tournament_phase_horse_race } }
		add = contest_horse_race_aptitude_value
	}
	else_if = {
		limit = { scope:activity = { has_current_phase = tournament_phase_archery } }
		add = contest_archery_aptitude_value
	}
	else = { add = contest_pugilism_aptitude_value }

	### Boundaries ###
	min = 1
}

contest_base_aptitude_value = 5

shared_tournament_aptitude_modifiers_value = {
	value = 0
	if = {
    	limit = { has_character_modifier = unwilling_tourneyer_modifier }
    	add = -10
    }
    if = {
    	limit = { has_character_modifier = generous_to_knights_modifier }
    	add = 5
    }
}

contest_progress_increase_massive_value = 30
contest_progress_increase_major_value = 25
contest_progress_increase_medium_value = 20
contest_progress_increase_minor_value = 15
contest_progress_increase_miniscule_value = 10

contest_progress_decrease_massive_value = {
	value = contest_progress_increase_massive_value
	multiply = -1
}
contest_progress_decrease_major_value = {
	value = contest_progress_increase_major_value
	multiply = -1
}
contest_progress_decrease_medium_value = {
	value = contest_progress_increase_minor_value
	multiply = -1
}
contest_progress_decrease_minor_value = {
	value = contest_progress_increase_minor_value
	multiply = -1
}
contest_progress_decrease_miniscule_value = {
	value = contest_progress_increase_miniscule_value
	multiply = -1
}

tournament_team_alpha_size = {
	scope:activity = {
		every_guest_subset_current_phase = { name = team_alpha add = 1 }
	}
}

tournament_team_beta_size = {
	scope:activity = {
		every_guest_subset_current_phase = { name = team_beta add = 1 }
	}
}

# WAGERS
# Top = A, Bottom = B, Target = C, Odds percent = X
# (C - B) / (A - B) = X

# A
tournament_wager_top_score_value = { value = scope:wager_top_contestant.var:contest_aptitude }

# B
tournament_wager_bottom_score_value = { value = scope:wager_bottom_contestant.var:contest_aptitude }

# C
tournament_wager_target_score_value = { value = scope:wager_target.var:contest_aptitude }

# A - B
tournament_wager_top_minus_bottom_score_value = {
	value = tournament_wager_top_score_value
	subtract = tournament_wager_bottom_score_value
}

# C - B
tournament_wager_target_minus_bottom_value = {
	value = tournament_wager_target_score_value
	subtract = tournament_wager_bottom_score_value
}

# (C - B) / (A - B) = X
tournament_wager_odds_percent_value = {
	value = tournament_wager_target_minus_bottom_value
	divide = tournament_wager_top_minus_bottom_score_value
	# Betting on yourself pays out less
	if = {
		limit = { scope:wagerer = scope:wager_target }
		multiply = 1.5
	}
	# Betting on mysterious stranger pays out more
	if = {
		limit = {
			exists = scope:mysterious_stranger
			scope:mysterious_stranger = scope:wager_target
		}
		multiply = 0.5
	}
	min = 0
	max = 1
}

# Convert odds percent into multiplier
tournament_wager_payout_value = {
	value = tiny_gold_value
	if = {
		limit = { tournament_wager_odds_percent_value >= 0.9 }
		multiply = 1.25 # 4/1
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.8 }
		multiply = 1.5 # 2/1
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.7 }
		multiply = 2 # 1/2
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.6 }
		multiply = 3 # 1/3
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.5 }
		multiply = 4 # 1/4
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.4 }
		multiply = 5 # 1/5
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.3 }
		multiply = 6 # 1/6
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.2 }
		multiply = 7 # 1/7
	}
	else_if = {
		limit = { tournament_wager_odds_percent_value >= 0.1 }
		multiply = 8 # 1/8
	}
	else = { multiply = 9 } # 1/9
	ceiling = yes
}

# Payout minus gold value
tournament_wager_winning_value = {
	value = tournament_wager_payout_value
	subtract = tiny_gold_value
}

tournament_wager_diff_value = {
	value = tournament_wager_top_score_value
	multiply = 0.5
}

tournament_versus_bout_delay_min = 1

tournament_versus_bout_delay_max = 6

tournament_versus_bout_summary_delay = 30

tournament_versus_duration_months = 1 # Versus must be specific duration to work - do not edit

tournament_standard_duration_months = 1

tournament_qualification_duration_weeks = 8

tournament_turn_contest_duration_weeks = 3

tournament_team_contest_duration_weeks = 4

tournament_race_contest_duration_weeks = 3

tournament_intent_delay_min = 1

tournament_intent_delay_max = 2

tournament_contest_aptitude_diff_value = {
	value = 0
	if = {
		limit = { exists = var:progress_to_victory }
		add = var:progress_to_victory
	}
	if = {
		limit = { exists = scope:contest_compare.var:progress_to_victory }
		subtract = scope:contest_compare.var:progress_to_victory
	}
}

tournament_active_phase_count_value = {
	# 1-5 Phases:
	value = 1
	# Tribes have less phases
	# Removed; felt a little punishing - NM
	#if = {
	#	limit = {
	#		NOT = { government_has_flag = government_is_tribal }
	#	}
	#	add = 1
	#}
	# Extra phases unlocked over time
	culture = {
		if = {
			limit = { has_cultural_era_or_later = culture_era_early_medieval }
			add = 1
		}
		if = {
			limit = {
				has_cultural_era_or_later = culture_era_high_medieval
			}
			add = 1
		}
		if = {
			limit = { has_cultural_era_or_later = culture_era_late_medieval }
			add = 2
		}
	}
	# Some cultures like practicing war and words; unlock another slot in High medieval age as opposed to Late
	culture = {
		if = {
			limit = {
				NOT = {
					has_cultural_era_or_later = culture_era_late_medieval
				}
				AND = {
					has_cultural_era_or_later = culture_era_high_medieval
					OR = {
						has_cultural_pillar = heritage_north_germanic
						has_cultural_pillar = heritage_frankish
						has_cultural_pillar = heritage_iberian
						has_cultural_pillar = heritage_latin
						has_cultural_pillar = heritage_mongolic
						has_cultural_pillar = heritage_north_germanic
						has_cultural_pillar = heritage_west_germanic
						has_cultural_pillar = heritage_central_germanic
					}
				}
			}
			add = 1
		}
	}
}

contest_contestants_count_value = {
	scope:activity = {
		every_attending_character = {
			limit = { activity_is_valid_tournament_contestant = yes }
			add = 1
		}
	}
}

contest_guests_count_value = {
	scope:activity = {
		every_attending_character = {
			limit = {
				NOT = { activity_is_valid_tournament_contestant = yes }
			}
			add = 1
		}
	}
}

contest_declined_invites_count_value = {
	scope:activity = {
		every_invited_character = {
			limit = {
				NOT = { is_participant_in_activity = scope:activity }
			}
			add = 1
		}
	}
}

contest_valid_contestants_count_value = {
	value = contest_contestants_count_value
	add = contest_guests_count_value
}

tournament_base_cost = {
	value = 100
}

tournament_option_bad_cost = {
	value = 20
}

tournament_option_normal_cost = {
	value = 60
}

tournament_option_good_cost = {
	value = 180
}

tournament_option_very_good_cost = {
	value = 360
}

tournament_option_splendid_cost = {
	value = 720
}

tournament_option_bad_era_scaled_cost = {
	value = 20
	multiply = activity_cost_scale_by_era
}

tournament_option_normal_era_scaled_cost = {
	value = 60
	multiply = activity_cost_scale_by_era
}

tournament_option_good_era_scaled_cost = {
	value = 180
	multiply = activity_cost_scale_by_era
}

tournament_option_very_good_era_scaled_cost = {
	value = 360
	multiply = activity_cost_scale_by_era
}

tournament_option_splendid_era_scaled_cost = {
	value = 720
	multiply = activity_cost_scale_by_era
}

tournament_contest_base_cost = 100
tournament_contest_base_tribal_cost = {
	value = tournament_contest_base_cost
	divide = 2
}
tournament_contest_horse_cost = 150
tournament_contest_horse_tribal_cost = {
	value = tournament_contest_horse_cost
	divide = 2
}
tournament_contest_cheap_cost = 50
tournament_contest_cheap_tribal_cost = {
	value = tournament_contest_cheap_cost
	divide = 2
}
tournament_contest_expensive_cost = 200
tournament_contest_expensive_tribal_cost = {
	value = tournament_contest_expensive_cost
	divide = 2
}
tournament_activity_cost_discount = {
	value = 0.9
}
tournament_activity_cost_discount_value = {
	value = tournament_activity_cost_discount
	subtract = 1
}
tournament_activity_cost_max_discount = {
	value = 0.75
}
tournament_activity_cost_max_discount_value = {
	value = tournament_activity_cost_max_discount
	subtract = 1
}
tournament_contest_cost_discount = {
	value = 0.5
}
tournament_contest_cost_discount_value = {
	value = tournament_contest_cost_discount
	subtract = 1
}
tournament_contest_cost_low_discount = {
	value = 0.9	
}
tournament_contest_cost_low_discount_value = {
	value = tournament_contest_cost_low_discount
	subtract = 1
}

contest_versus_qualified_count = 8
contest_versus_semi_finalist_count = 4
contest_versus_finalist_count = 2

progress_to_victory_passive_contestant_value = {
	value = 0
	# if to avoid errors
	if = {
		limit = { exists = var:contest_aptitude }
		value = {
			value = var:contest_aptitude
			multiply = 0.5
		}
	}
	if = {
		limit = { has_court_position = champion_court_position }
		add = 5
	}
	if = {
		limit = { exists = var:progress_to_victory_score }
		add = {
			value = var:progress_to_victory_score
			#multiply = 0.5
		}
	}
	ceiling = yes
	min = 1
	max = 25
}

## This variable is referenced in the UI. 
progress_to_victory_current = {
	value = 0
	if = {
		limit = { exists = var:progress_to_victory }
		add = var:progress_to_victory
	}
}

progress_to_victory_active_contestant_value = {
	value = 0
	if = {
		limit = {
			involved_activity ?= { activity_tournament_versus_contest_trigger = no }
		}
		if = {
			limit = { exists = var:contest_aptitude }
			value = var:contest_aptitude
		}
		if = {
			limit = { exists = var:progress_to_victory_score }
			add = var:progress_to_victory_score
		}
	}
	else = {
		if = {
			limit = { exists = var:contest_aptitude }
			add = {
				value = var:contest_aptitude
				divide = 2
			}
		}
		if = {
			limit = { exists = var:progress_to_victory_score }
			add = {
				value = var:progress_to_victory_score
				divide = 2
			}
		}
	}
	if = {
		limit = { exists = var:progress_to_victory_score }
		add = var:progress_to_victory_score
	}	
	ceiling = yes
	max = 90
	min = 1
}

base_progress_to_victory_qualified_value = 40

base_progress_to_victory_qualified_versus_value = 25

tournament_wait_time_before_start_months = 3

tournament_opening_event_weeks = {
	value = tournament_wait_time_before_start_months
	multiply = 4
	add = -1
}

# 1% increment: best - worst / 40
progress_to_victory_increment_value = {
	value = 30
	subtract = contest_base_aptitude_value # TODO_CD_EP2 use worst instead?
	divide = 40
}

# PtV to qualify: this - worst / 1% increment
progress_to_victory_contestant_qualification_value = {
	value = var:contest_aptitude
	subtract = contest_base_aptitude_value # TODO_CD_EP2 use worst instead?
	divide = progress_to_victory_increment_value
	round = yes
}

#EVENT VALUES

skilled_enforcers_value = {
	value = 0
	every_knight = {
		limit = {
			prowess >= 20
		}
		add = 20
	}
}

legendary_enforcers_value = {
	value = 0
	every_knight = {
		limit = {
			prowess > 25
		}
		add = 20
	}
}

farrier_mistake_value = -5

jason_locale_events_210_desired_prowess_value = {
	value = accolade_prowess_requirement_value
	subtract = scope:foreign_knight.prowess
}

jason_locale_events_210_desired_martial_value = {
	value = maa_accolade_minimum_martial_value
	subtract = scope:foreign_knight.martial
}

#REWARD VALUES

tournament_first_place_reward_prestige_major_value = major_prestige_gain
tournament_first_place_reward_prestige_medium_value = medium_prestige_gain
tournament_first_place_reward_prestige_minor_value = minor_prestige_gain

tournament_second_place_reward_prestige_major_value = medium_prestige_gain
tournament_second_place_reward_prestige_medium_value = minor_prestige_gain
tournament_second_place_reward_prestige_minor_value = miniscule_prestige_gain

tournament_third_place_reward_prestige_major_value = minor_prestige_gain
tournament_third_place_reward_prestige_medium_value = miniscule_prestige_gain
# Tournament not grand enough for a minor value.

tournament_team_winner_reward_prestige_major_value = medium_prestige_gain
tournament_team_winner_reward_prestige_medium_value = minor_prestige_gain
tournament_team_winner_reward_prestige_minor_value = miniscule_prestige_gain

tournament_team_loser_reward_prestige_major_value = minor_prestige_gain
tournament_team_loser_reward_prestige_medium_value = miniscule_prestige_gain
# Tournament not grand enough for a minor value.

# Hastiludes XP randomiser

tournament_hastiludes_xp_gain_miniscule_value = {
	value = { 1 3 }
}

tournament_hastiludes_xp_gain_minor_value = {
	value = { 3 7 }
}

tournament_hastiludes_xp_gain_medium_value = {
	value = { 8 12 }
}

tournament_hastiludes_xp_gain_major_value = {
	value = { 13 17 }
}

tournament_hastiludes_xp_gain_massive_value = {
	value = 20
}

tournament_hastiludes_xp_loss_minor_value = {
	value = -5
}

tournament_hastiludes_xp_loss_medium_value = {
	value = -10
}

tournament_hastiludes_xp_loss_major_value = {
	value = -15
}

tournament_base_cost_value = {
	value = 200
}
