﻿###########################
###########################
##### CONTEST EFFECTS #####
###########################
###########################

#########################
##### SCORE EFFECTS #####
#########################

# Change score of a character for the current contest
# Used to increase/decrease chance of winning the contest based on event outcomes
activity_tournament_change_contestant_score_effect = {
	save_scope_as = score_changee
	if = {
		limit = {
			exists = scope:score_changee.involved_activity
			is_participant_in_activity = scope:score_changee.involved_activity
			scope:score_changee.involved_activity = {
				any_guest_subset_current_phase = {
					name = contestant
					this = scope:score_changee
				}
			}
			scope:score_changee = { activity_is_competing_trigger = yes }
		}
		if = {
			limit = { exists = var:progress_to_victory }
			save_scope_value_as = {
				name = old_ptv
				value = var:progress_to_victory
			}
		}
		# Set score tracker
		if = {
			limit = {
				NOT = { exists = var:progress_to_victory_score }
			}
			set_variable = {
				name = progress_to_victory_score
				value = {
					value = contest_progress_$SCORE$_value
					if = {
						limit = {
							scope:score_changee = {
								is_ai = yes
								NOR = {
									liege.court_position:champion_court_position ?= this
									involved_activity.special_guest:special_guest_champion ?= this
								}
							}
						}
						multiply = 0.5
					}
				}
			}
		}
		# Change existing score tracker
		else = {
			change_variable = {
				name = progress_to_victory_score
				add = {
					value = contest_progress_$SCORE$_value
					if = {
						limit = {
							scope:score_changee = {
								is_ai = yes
								NOR = {
									liege.court_position:champion_court_position ?= this
									involved_activity.special_guest:special_guest_champion ?= this
								}
							}
						}
						multiply = 0.5
					}
				}
			}
		}
		# Tooltip
		custom_tooltip = contestant_score_$SCORE$_tt
		# Update current PtV
		if = {
			limit = { has_activity_state = active }
			if = {
				limit = {
					scope:score_changee.involved_activity = { activity_tournament_team_contest_trigger = yes }
				}
				scope:score_changee.involved_activity = { tournament_contest_update_team_ptv_effect = yes }
			}
			else = {
				set_variable = {
					name = progress_to_victory
					value = progress_to_victory_active_contestant_value
				}
			}
		}
		else = {
			set_variable = {
				name = progress_to_victory
				value = progress_to_victory_passive_contestant_value
			}
		}
		# Toasts for qualification
		if = {
			limit = {
				has_activity_state = passive
				is_ai = no
			}
			if = { # No Longer Qualify
				limit = {
					exists = scope:old_ptv
					scope:old_ptv >= scope:score_changee.involved_activity.var:progress_to_victory_threshold
					var:progress_to_victory < scope:score_changee.involved_activity.var:progress_to_victory_threshold
				}
				send_interface_toast = {
					title = tournament_unqualified_ptv_toast
					left_icon = scope:score_changee
					custom_tooltip = tournament_unqualified_ptv_tt
				}
			}
			else_if = { # Now Qualify
				limit = {
					exists = scope:old_ptv
					scope:old_ptv < scope:score_changee.involved_activity.var:progress_to_victory_threshold
					var:progress_to_victory >= scope:score_changee.involved_activity.var:progress_to_victory_threshold
				}
				send_interface_toast = {
					title = tournament_qualified_ptv_toast
					left_icon = scope:score_changee
					custom_tooltip = tournament_qualified_ptv_tt 
				}
			}
		}
	}
	else = {
		if = {
			limit = { exists = var:progress_to_victory }
			remove_variable = progress_to_victory
		}
		if = {
			limit = { exists = var:progress_to_victory_score }
			remove_variable = progress_to_victory_score
		}
		if = {
			limit = { exists = var:contest_aptitude }
			remove_variable = contest_aptitude
		}
	}
}

# Purge all tournament related variables
tournament_clear_contest_variables_effect = {
	if = {
		limit = { exists = var:progress_to_victory }
		remove_variable = progress_to_victory
	}
	if = {
		limit = { exists = var:progress_to_victory_score }
		remove_variable = progress_to_victory_score
	}
	if = {
		limit = { exists = var:contest_aptitude }
		remove_variable = contest_aptitude
	}
	if = {
		limit = { exists = var:mysterious_stranger_origin }
		remove_variable = mysterious_stranger_origin
	}
	if = {
		limit = { has_character_flag = resigned_contest_flag }
		remove_character_flag = resigned_contest_flag
	}
	if = {
		limit = { has_character_flag = horse_race_mysterious_stranger }
		remove_character_flag = horse_race_mysterious_stranger
	}
	if = {
		limit = { has_character_flag = contest_horse_race_war_horse }
		remove_character_flag = contest_horse_race_war_horse
	}
	remove_character_flag = success_pivotal_flag
	remove_character_flag = neutral_pivotal_flag
	remove_character_flag = failure_pivotal_flag
	if = {
		limit = { exists = var:contest_qualified_match_wrestling }
		remove_variable = contest_qualified_match_wrestling
	}
	if = {
		limit = { exists = var:contest_semi_finalist_match_wrestling }
		remove_variable = contest_semi_finalist_match_wrestling
	}
	if = {
		limit = { exists = var:contest_finalist_match_wrestling }
		remove_variable = contest_finalist_match_wrestling
	}
	if = {
		limit = { exists = var:contest_qualified_match_duel }
		remove_variable = contest_qualified_match_duel
	}
	if = {
		limit = { exists = var:contest_semi_finalist_match_duel }
		remove_variable = contest_semi_finalist_match_duel
	}
	if = {
		limit = { exists = var:contest_finalist_match_duel }
		remove_variable = contest_finalist_match_duel
	}
	if = {
		limit = { exists = var:contest_qualified_match_joust }
		remove_variable = contest_qualified_match_joust
	}
	if = {
		limit = { exists = var:contest_semi_finalist_match_joust }
		remove_variable = contest_semi_finalist_match_joust
	}
	if = {
		limit = { exists = var:contest_finalist_match_joust }
		remove_variable = contest_finalist_match_joust
	}
	if = {
		limit = { exists = var:contest_qualified_match_board_game }
		remove_variable = contest_qualified_match_board_game
	}
	if = {
		limit = { exists = var:contest_semi_finalist_match_board_game }
		remove_variable = contest_semi_finalist_match_board_game
	}
	if = {
		limit = { exists = var:contest_finalist_match_board_game }
		remove_variable = contest_finalist_match_board_game
	}
	if = {
		limit = { exists = var:last_versus_match }
		remove_variable = last_versus_match
	}
	if = {
		limit = { exists = var:next_versus_match }
		remove_variable = next_versus_match
	}
}

# Clean tournament only modifiers
tournament_clear_tournament_modifiers_effect = {
	if = {
		limit = {
			any_character_artifact = {
				has_artifact_modifier = artifact_tournament_sharpened_blade_modifier
			}
		}
		every_character_artifact = {
			remove_artifact_modifier = artifact_tournament_sharpened_blade_modifier
		}
	}
}

# Purge all tournament related variables
tournament_clear_tournament_variables_effect = {
	tournament_clear_contest_variables_effect = yes
	if = {
		limit = { exists = var:contest_versus_progress }
		remove_variable = contest_versus_progress
	}
	if = {
		limit = { exists = var:tournament_marriage_prize }
		remove_variable = tournament_marriage_prize
	}
	if = {
		limit = { has_character_flag = arrived_at_tournament_flag }
		remove_character_flag = arrived_at_tournament_flag
	}
	if = {
		limit = { has_character_modifier = tournament_melee_protector_modifier }
		remove_character_modifier = tournament_melee_protector_modifier
	}
	if = {
		limit = { has_character_flag = knows_mulan_flag }
		remove_character_flag = knows_mulan_flag
	}
	if = {
		limit = { has_character_flag = cloak_duelist_flag }
		remove_character_flag = cloak_duelist_flag
	}
	if = {
		limit = { exists = var:contest_horse_race_name }
		remove_variable = contest_horse_race_name
	}
	if = {
		limit = { exists = var:contest_horse_race_gender }
		remove_variable = contest_horse_race_gender
	}
	if = {
		limit = { exists = var:horse_race_jockey }
		remove_variable = horse_race_jockey
	}
	clear_variable_list = tournament_favor_offers
}

#################
### TEAM TYPE ###
#################

# Examples: Melee, Horse Sport

# Team contests divide competitors into two groups and compare them based on contest skills to determine winners

# Sort competitors into 2 teams based on relationships and loyalties - on_start
tournament_contest_team_composition_effect = {
	scope:activity = {
		set_variable = { name = tournament_commenced }
		# Players
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				OR = {
					is_ai = no
					AND = {
						has_court_position = champion_court_position
						liege = { is_ai = no }
					}
				}
				tournament_contest_can_compete_trigger = yes
				var:progress_to_victory >= scope:activity.var:progress_to_victory_threshold
			}
			add_tournament_contest_contestant_effect = {
				CONTEST = $CONTEST$
				SKILL = $SKILL$
				PTV = progress_to_victory_active_contestant_value
			}
		}
		# Filter relevant into entrants list
		while = {
			limit = {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					count < 20
				}
				any_guest_subset = {
					name = contestant
					phase = tournament_phase_$CONTEST$
					tournament_contest_can_compete_trigger = yes
				}
			}
			# Determine eligible contestants
			ordered_guest_subset = {
				name = contestant
				phase = tournament_phase_$CONTEST$
				limit = { tournament_contest_can_compete_trigger = yes }
				order_by = var:progress_to_victory
				add_tournament_contest_contestant_effect = {
					CONTEST = $CONTEST$
					SKILL = $SKILL$
					PTV = progress_to_victory_active_contestant_value
				}
			}
		}
		# Inform liege of prestige from qualified knights
		tournament_liege_qualification_toast_effect = yes
		# Pick first team alpha character
		random_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = { this = scope:host }
			alternative_limit = {
				has_court_position = champion_court_position
				liege = scope:host
			}
			alternative_limit = { is_playable_character = yes }
			alternative_limit = { always = yes }
			save_scope_as = new_list_member
			scope:activity = {
				add_to_guest_subset = {
					name = team_alpha
					target = scope:new_list_member
					phase = tournament_phase_$CONTEST$
				}
			}
			save_scope_as = team_alpha_creator
		}
		# Pick first team beta character
		random_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				is_playable_character = yes
				has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:team_alpha_creator }
				this != scope:team_alpha_creator
			}
			alternative_limit = {
				is_playable_character = yes
				this != scope:team_alpha_creator
			}
			alternative_limit = {
				this != scope:team_alpha_creator
			}
			weight = {
				base = 1
				modifier = {
					factor = 10
					has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:team_alpha_creator }
				}
				modifier = {
					factor = 10
					AND = {
						scope:team_alpha_creator = { has_activity_intent = murder_attendee_intent }
						this = scope:team_alpha_creator.intent_target
					}
				}
				modifier = {
					factor = 0.5
					liege = scope:team_alpha_creator
				}
				modifier = {
					add = 1
					top_liege = this
				}
				modifier = {
					add = 1
					primary_title.tier >= scope:team_alpha_creator.primary_title.tier
				}
			}
			save_scope_as = new_list_member
			scope:activity = {
				add_to_guest_subset = {
					name = team_beta
					target = scope:new_list_member
					phase = tournament_phase_$CONTEST$
				}
			}
		}
		# Randomly distribute remaining competitors to teams
		ordered_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				save_temporary_scope_as = new_list_member_temp
				scope:activity = {
					NOR = {
						any_guest_subset_current_phase = {
							name = team_alpha
							this = scope:new_list_member_temp
						}
						any_guest_subset_current_phase = {
							name = team_beta
							this = scope:new_list_member_temp
						}
					}
				}
			}
			order_by = { value = var:progress_to_victory }
			max = 18
			check_range_bounds = no
			save_scope_as = new_list_member
			random_list = { # replace with more efficient script?
				10 = {
					# Favor smaller team
					activity_contest_team_size_modifier = {
						SMALLER = alpha
						LARGER = beta
					}
					# Relationships and loyalty
					activity_contest_team_preference_modifier = { TEAM = alpha }
					scope:activity = {
						add_to_guest_subset = {
							name = team_alpha
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
				}
				10 = {
					# Favor smaller team
					activity_contest_team_size_modifier = {
						SMALLER = beta
						LARGER = alpha
					}
					# Relationships and loyalty
					activity_contest_team_preference_modifier = { TEAM = beta }
					scope:activity = {
						add_to_guest_subset = {
							name = team_beta
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
				}
			}
		}
		# Captains
		random_guest_subset = {
			name = team_alpha
			weight = {
				base = 1
				tournament_team_captain_modifier = yes
			}
			save_scope_as = alpha_captain
			scope:activity = {
				add_to_guest_subset = {
					name = team_alpha_captain
					target = scope:alpha_captain
				}
			}
		}
		random_guest_subset = {
			name = team_beta
			weight = {
				base = 1
				tournament_team_captain_modifier = yes
			}
			save_scope_as = beta_captain
			scope:activity = {
				add_to_guest_subset = {
					name = team_beta_captain
					target = scope:beta_captain
				}
			}
		}
		# Update shared PtV values
		tournament_contest_update_team_ptv_effect = yes
		# Qualification ping
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			if = {
				limit = {
					save_temporary_scope_as = new_list_member_temp
					scope:activity = {
						NOR = {
							any_guest_subset_current_phase = {
								name = team_alpha
								this = scope:new_list_member_temp
							}
							any_guest_subset_current_phase = {
								name = team_beta
								this = scope:new_list_member_temp
							}
						}
					}
				}
				save_scope_as = new_list_member
				if = {
					limit = { exists = var:contest_aptitude }
					remove_variable = contest_aptitude
				}
				if = {
					limit = { exists = var:progress_to_victory }
					remove_variable = progress_to_victory
				}
				send_interface_toast = {
					title = tournament_qualify_fail_title
					left_icon = this
					custom_tooltip = tournament_qualify_fail_desc
					scope:activity = {
						add_to_guest_subset = {
							name = spectator
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
					play_sound_effect = "event:/SFX/Events/Themes/sfx_event_theme_type_activity"
				}
			}
		}
		# Add Activity Log entry
		add_activity_log_entry = {
			key = tournament_contest_start_log
			tags = { contest }
			score = 20
			character = scope:host
		}
		# Fire starting event
		every_attending_character = {
			trigger_event = contest_events.0600
		}
	}
}

# Fire contest events with correct spacing
tournament_team_pulse_effect = {
	scope:activity = {
		every_guest_subset_current_phase = {
			name = qualified
			limit = {
				exists = var:progress_to_victory
				is_ai = no
			}
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_setup_pulse
				days = 1
			}
			if = {
				limit = { is_ai = no }
				trigger_event = {
					on_action = contest_ongoing_event_$CONTEST$_random_pulse
					days = 7
				}
				trigger_event = {
					on_action = contest_ongoing_event_$CONTEST$_random_pulse
					days = 14
				}
				trigger_event = {
					on_action = contest_ongoing_event_$CONTEST$_pivotal_pulse
					days = 21
				}
			}
			else = {
				trigger_event = {
					on_action = contest_ongoing_event_$CONTEST$_ai_pulse
					days = { 7 21 }
				}
			}
		}
		every_attending_character = {
			limit = {
				OR = {
					is_in_guest_subset = { name = qualified }
					is_ai = no
				}
			}
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_outcome_pulse
				days = 25
			}
		}
	}
}

# Set team ptv variables, which pool all team member's aptitude and score
tournament_contest_update_team_ptv_effect = {
	set_variable = {
		name = team_a_ptv
		value = {
			value = 25
			every_guest_subset_current_phase = {
				name = team_alpha
				limit = { exists = var:contest_aptitude }
				add = {
					value = var:contest_aptitude
					divide = 10
				}
				if = {
					limit = { exists = var:progress_to_victory_score }
					add = {
						value = var:progress_to_victory_score
						divide = 10
					}
				}
			}
		}
	}
	set_variable = {
		name = team_b_ptv
		value = {
			value = 25
			every_guest_subset_current_phase = {
				name = team_beta
				limit = { exists = var:contest_aptitude }
				add = {
					value = var:contest_aptitude
					divide = 10
				}
				if = {
					limit = { exists = var:progress_to_victory_score }
					add = {
						value = var:progress_to_victory_score
						divide = 10
					}
				}
			}
		}
	}
	every_guest_subset_current_phase = {
		name = team_alpha
		set_variable = {
			name = progress_to_victory
			value = scope:activity.var:team_a_ptv
		}
	}
	every_guest_subset_current_phase = {
		name = team_beta
		set_variable = {
			name = progress_to_victory
			value = scope:activity.var:team_b_ptv
		}
	}
}

tournament_contest_winner_captain_effect = {
	if = { # Original captain
		limit = {
			any_guest_subset = {
				name = team_$WINNER$_captain
				phase = tournament_phase_$CONTEST$
				is_alive = yes
			}
		}
		random_guest_subset = {
			name = team_$WINNER$_captain
			phase = tournament_phase_$CONTEST$
			save_scope_as = contest_winner
		}
	}
	else = { # New captain
		random_guest_subset = {
			name = winner
			phase = tournament_phase_$CONTEST$
			limit = { is_alive = yes }
			weight = {
				base = 1
				tournament_team_captain_modifier = yes
			}
			save_scope_as = contest_winner
		}
	}
	scope:activity = {
		set_variable = {
			name = melee_winner
			value = scope:contest_winner
		}
	}
	# Save losing captain
	if = {
		limit = {
			any_guest_subset = {
				name = team_$LOSER$_captain
				phase = tournament_phase_$CONTEST$
				is_alive = yes
			}
		}
		random_guest_subset = {
			name = team_$LOSER$_captain
			phase = tournament_phase_$CONTEST$
			save_scope_as = second_place
		}
	}
	else = {
		random_guest_subset = {
			name = second_place
			phase = tournament_phase_$CONTEST$
			limit = { is_alive = yes }
			weight = {
				base = 1
				tournament_team_captain_modifier = yes
			}
			save_scope_as = second_place
		}
	}
}

# Determine winning team based on relative strength and event score - on_end
tournament_contest_team_winner_effect = {
	# Achievement.
	little_william_marshal_achievement_contest_end_effect = yes
	scope:activity = {
		tournament_contest_update_team_ptv_effect = yes
		save_scope_value_as = { name = contest_complete value = flag:$CONTEST$ }
		# Determine winning team
		random_list = {
			# ALPHA WINS
			1 = {
				# Add all team member's contest ability
				modifier = { add = var:team_a_ptv }
				# More likely is this team's PtV is higher
				modifier = {
					var:team_a_ptv > var:team_b_ptv
					factor = 2
				}
				# If other team has less than half PtV of this team, this team wins win
				modifier = {
					progress_to_victory_fetch_team_a_half_value >= scope:activity.var:team_b_ptv
					add = 1000
				}
			    every_guest_subset = {
			    	name = team_alpha
			    	phase = tournament_phase_$CONTEST$
					limit = { activity_tournament_valid_winner_trigger = yes }
					save_scope_as = new_list_member
					tournament_complete_win_contest_intent_effect = yes
					scope:activity = {
						add_to_guest_subset = {
			    			name = winner
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
					# Achievement.
					little_william_marshal_achievement_contest_end_winner_effect = yes
				}
				every_guest_subset = {
			    	name = team_beta
			    	phase = tournament_phase_$CONTEST$
					limit = { activity_tournament_valid_winner_trigger = yes }
					save_scope_as = new_list_member
					scope:activity = {
						add_to_guest_subset = {
			    			name = second_place
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
				}
				# Save winning and losing captain
				tournament_contest_winner_captain_effect = {
					WINNER = alpha
					LOSER = beta
					CONTEST = melee
				}
			}
			# BETA WINS
			1 = {
				# Add all team member's contest ability
				modifier = { add = var:team_b_ptv }
				# More likely is this team's PtV is higher
				modifier = {
					var:team_b_ptv > var:team_a_ptv
					factor = 2
				}
				# If other team has less than half PtV of this team, this team wins win
				modifier = {
					progress_to_victory_fetch_team_b_half_value >= scope:activity.var:team_a_ptv
					add = 1000
				}
				every_guest_subset = {
			    	name = team_beta
			    	phase = tournament_phase_$CONTEST$
					limit = { activity_tournament_valid_winner_trigger = yes }
					save_scope_as = new_list_member
					tournament_complete_win_contest_intent_effect = yes
					scope:activity = {
						add_to_guest_subset = {
			    			name = winner
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
					# Achievement.
					little_william_marshal_achievement_contest_end_winner_effect = yes
				}
				every_guest_subset = {
			    	name = team_alpha
			    	phase = tournament_phase_$CONTEST$
					limit = { activity_tournament_valid_winner_trigger = yes }
					save_scope_as = new_list_member
					scope:activity = {
						add_to_guest_subset = {
			    			name = second_place
							target = scope:new_list_member
							phase = tournament_phase_$CONTEST$
						}
					}
				}
				# Save winning and losing captain
				tournament_contest_winner_captain_effect = {
					WINNER = beta
					LOSER = alpha
					CONTEST = melee
				}
			}
		}
		# Rewards
		tournament_contest_team_rewards_effect = { CONTEST = $CONTEST$ }
		# Add log entry
		add_activity_log_entry = {
			key = tournament_$CONTEST$_team_winner_log
			tags = { winner contest }
			score = 80
			show_in_conclusion = yes
			character = scope:contest_winner
			target = scope:second_place

			#Effect
			show_as_tooltip = {
				scope:contest_winner ?= {
					# Prestige
					tournament_team_winner_prestige_rewards_effect = yes
					# Gold
					tournament_team_winner_gold_rewards_effect = yes
					# Trait
					tournament_give_trait_effect = { CONTEST = $CONTEST$ }
					# Modifier
					add_character_modifier = { modifier = tournament_$CONTEST$_winner_modifier years = 5 }
				}
			}
		}
		# Fire host's prize award event
		activity_host = { trigger_event = contest_events.0605 }
		# Clear redundant variables
		every_attending_character = { tournament_clear_contest_variables_effect = yes }
	}
}

##################
### TURNS TYPE ###
##################

# Examples: Quintain, Archery, Recital
# Turn contests are determined by skill and event outcomes

# Create competitor list and log entry
tournament_contest_turn_composition_effect = {
	scope:activity = {
		set_variable = { name = tournament_commenced }
		# Players
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				OR = {
					is_ai = no
					AND = {
						has_court_position = champion_court_position
						liege = { is_ai = no }
					}
				}
				tournament_contest_can_compete_trigger = yes
				var:progress_to_victory >= scope:activity.var:progress_to_victory_threshold
			}
			add_tournament_contest_contestant_effect = {
				CONTEST = $CONTEST$
				SKILL = $SKILL$
				PTV = progress_to_victory_active_contestant_value
			}
		}
		# Filter relevant into entrants list
		while = {
			limit = {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					count < 7
				}
				any_guest_subset_current_phase = {
					name = contestant
					tournament_contest_can_compete_trigger = yes
				}
			}
			ordered_guest_subset = {
				name = contestant
				phase = tournament_phase_$CONTEST$
				limit = { tournament_contest_can_compete_trigger = yes }
				order_by = var:progress_to_victory
				add_tournament_contest_contestant_effect = {
					CONTEST = $CONTEST$
					SKILL = $SKILL$
					PTV = progress_to_victory_active_contestant_value
				}
			}
		}
		hidden_effect = {
			random_list = {
				3 = {
					ordered_guest_subset = {
						name = contestant
						phase = tournament_phase_$CONTEST$
						limit = { tournament_contest_can_compete_trigger = yes }
						order_by = var:progress_to_victory
						add_tournament_contest_contestant_effect = {
							CONTEST = $CONTEST$
							SKILL = $SKILL$
							PTV = progress_to_victory_active_contestant_value
						}
					}
				}
				1 = {
					trigger = {
						NOT = { exists = var:mysterious_stranger_appeared }
						any_attending_character = {
							culture = {
								any_culture_county = {
									exists = faith
									any_direct_de_jure_vassal_title = { exists = this }
								}
							}
						}
						any_attending_character = { count <= 240 }
					}
					tournament_mysterious_stranger_effect = {
						CONTEST = $CONTEST$
						SKILL = $SKILL$
					}
				}
			}
		}
		# Add Activity Log entry
		add_activity_log_entry = {
			key = tournament_contest_start_log
			tags = { contest }
			score = 20
			character = scope:host
		}
		# Inform liege of prestige from qualified knights
		tournament_liege_qualification_toast_effect = yes
		# Qualification ping
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				save_temporary_scope_as = new_list_member
				NOT = {
					scope:activity = {
						any_guest_subset_current_phase = {
							name = qualified
							this = scope:new_list_member
						}
					}
				}
			}
			tournament_contest_qualification_failure_effect = yes
		}
		# Fire starting event
		every_attending_character = {
			trigger_event = contest_events.0700
		}
	}
}

# Fire turn events with correct spacing
tournament_turn_pulse_effect = {
	scope:activity = {
		every_guest_subset_current_phase = {
			name = qualified
			limit = { exists = var:progress_to_victory }
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_random_pulse
				days = 7
			}
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_random_pulse
				days = 14
			}
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_pivotal_pulse
				days = 21
			}
		}
		every_attending_character = {
			limit = {
				OR = {
					is_in_guest_subset = { name = qualified }
					is_ai = no
				}
			}
			# If competing yourself, it is from your perspective, otherwise find someone relevant
		    tournament_pivotal_contestant_scope_effect = yes
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_outcome_pulse
				days = 25
			}
		}
	}
}

# Determines first, second, and third place in turn based contests
tournament_contest_turn_winner_effect = {
	# Achievement.
	little_william_marshal_achievement_contest_end_effect = yes
	scope:activity = {
		every_guest_subset = {
			name = qualified
			set_variable = {
				name = progress_to_victory
				value = progress_to_victory_active_contestant_value
			}
		}
		save_scope_value_as = { name = contest_complete value = flag:$CONTEST$ }
		# Set winner if player
		if = {
			limit = {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					is_alive = yes
					exists = var:contest_aptitude
					exists = var:progress_to_victory
					is_ai = no
					save_temporary_scope_as = winner_temp
				}
				NOT = {
					any_guest_subset = {
						name = qualified
						phase = tournament_phase_$CONTEST$
						is_alive = yes
						exists = var:contest_aptitude
						exists = var:progress_to_victory
						var:progress_to_victory > scope:winner_temp.var:progress_to_victory
					}
				}
			}
			random_guest_subset = {
				name = qualified
				phase = tournament_phase_$CONTEST$
				limit = {
					is_alive = yes
					exists = var:contest_aptitude
					exists = var:progress_to_victory
					is_ai = no
					save_temporary_scope_as = winner_temp
					scope:activity  = {
						NOT = {
							any_guest_subset = {
								name = qualified
								phase = tournament_phase_$CONTEST$
								is_alive = yes
								exists = var:contest_aptitude
								exists = var:progress_to_victory
								var:progress_to_victory > scope:winner_temp.var:progress_to_victory
							}
						}
					}
				}
				save_scope_as = contest_winner
			}
		}
		# Determine winner
		else = {
			random_guest_subset = {
				name = qualified
				phase = tournament_phase_$CONTEST$
				limit = {
					is_alive = yes
					exists = var:contest_aptitude
					exists = var:progress_to_victory
					save_temporary_scope_as = winner_temp
				}
				weight = {
					base = 1
					# Weights heavily on PtV, and major differences in score
					tournament_contest_winner_determiner_modifier = { CHAR = scope:winner_temp CONTEST = $CONTEST$ }
				}
				save_scope_as = contest_winner
			}
		}
		scope:contest_winner = {
			tournament_complete_win_contest_intent_effect = yes
			# Achievement.
			little_william_marshal_achievement_contest_end_winner_effect = yes
		}
		# Determine second place
		random_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				is_alive = yes
				exists = var:contest_aptitude
				exists = var:progress_to_victory
				this != scope:contest_winner
				save_temporary_scope_as = second_temp
			}
			weight = {
				base = 1
				# Weights heavily on PtV, and major differences in score
				tournament_contest_winner_determiner_modifier = { CHAR = scope:second_temp CONTEST = $CONTEST$  }
			}
			save_scope_as = second_place
		}
		# Determine third place
		random_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				is_alive = yes
				exists = var:contest_aptitude
				exists = var:progress_to_victory
				NOR = {
					this = scope:contest_winner
					this = scope:second_place
				}
				save_temporary_scope_as = third_temp
			}
			weight = {
				base = 1
				# Weights heavily on PtV, and major differences in score
				tournament_contest_winner_determiner_modifier = { CHAR = scope:third_temp CONTEST = $CONTEST$  }
			}
			save_scope_as = third_place
		}
		# Save all non-winner entrants so we can easily check if someone lost
		every_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				exists = var:contest_aptitude
				exists = var:progress_to_victory
				this != scope:contest_winner
			}
			add_to_list = contest_losers
		}
		# Save winner
		add_to_guest_subset = {
			name = winner
			target = scope:contest_winner
			phase = tournament_phase_$CONTEST$
		}
		# Save podium placements for reference
		add_to_guest_subset = {
			name = second_place
			target = scope:second_place
			phase = tournament_phase_$CONTEST$
		}
		add_to_guest_subset = {
			name = third_place
			target = scope:third_place
			phase = tournament_phase_$CONTEST$
		}
		# Rewards
		tournament_contest_rewards_effect = { CONTEST = $CONTEST$ }
		# Fire host's prize award event
		activity_host = { trigger_event = contest_events.0705 }
		# Clear redundant variables
		every_attending_character = { tournament_clear_contest_variables_effect = yes }
	}
}

#################
### RACE TYPE ###
#################

# Turn contests are determined by skill and event outcomes

# Create competitor list and log entry
tournament_contest_race_composition_effect = {
	# Set up contest
	scope:activity = {
		set_variable = { name = tournament_commenced }
		# Set flavor variant
		if = {
			limit = {
				scope:host.culture = { has_cultural_pillar = heritage_arabic }
			}
			set_variable = {
				name = contest_horse_race_variant
				value = flag:maydan
			}
		}
		else = {
			set_variable = {
				name = contest_horse_race_variant
				value = flag:default
			}
		}
		# Players
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				is_ai = no
				tournament_contest_can_compete_trigger = yes
				exists = var:progress_to_victory
				var:progress_to_victory >= scope:activity.var:progress_to_victory_threshold
			}
			add_tournament_contest_contestant_effect = {
				CONTEST = $CONTEST$
				SKILL = $SKILL$
				PTV = progress_to_victory_active_contestant_value
			}
		}
		# Filter relevant into entrants list
		while = {
			limit = {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					count < 7
				}
				any_guest_subset_current_phase = {
					name = contestant
					tournament_contest_can_compete_trigger = yes
				}
			}
			ordered_guest_subset = {
				name = contestant
				phase = tournament_phase_$CONTEST$
				limit = { tournament_contest_can_compete_trigger = yes }
				order_by = var:progress_to_victory
				add_tournament_contest_contestant_effect = {
					CONTEST = $CONTEST$
					SKILL = $SKILL$
					PTV = progress_to_victory_active_contestant_value
				}
			}
		}
		# Chance for mysterious stranger
		hidden_effect = {
			random_list = {
				3 = {
					ordered_guest_subset = {
						name = contestant
						phase = tournament_phase_$CONTEST$
						limit = { tournament_contest_can_compete_race_trigger = yes }
						order_by = var:progress_to_victory # to favor landed
						add_tournament_contest_contestant_effect = {
							CONTEST = $CONTEST$
							SKILL = $SKILL$
							PTV = progress_to_victory_active_contestant_value
						}
					}
				}
				1 = {
					trigger = {
						NOT = { exists = var:mysterious_stranger_appeared }
						any_guest_subset = {
							name = contestant
							phase = tournament_phase_$CONTEST$
							tournament_contest_can_compete_race_trigger = yes
						}
					}
					random_guest_subset = {
						name = contestant
						phase = tournament_phase_$CONTEST$
						limit = { tournament_contest_can_compete_race_trigger = yes }
						save_scope_as = mysterious_stranger
						set_variable = {
							name = mysterious_stranger_origin
							value = scope:activity.activity_location.barony
						}
						add_character_flag = horse_race_mysterious_stranger
						add_tournament_contest_contestant_effect = {
							CONTEST = $CONTEST$
							SKILL = $SKILL$
							PTV = progress_to_victory_active_contestant_value
						}
						tournament_horse_race_fetch_horse_effect = yes
					}
					if = {
						limit = { exists = scope:mysterious_stranger }
						set_variable = {
							name = mysterious_stranger_appeared
							value = scope:mysterious_stranger
						}
					}
				}
			}
		}
		# Add Activity Log entry
		add_activity_log_entry = {
			key = tournament_contest_start_log
			tags = { contest }
			score = 20
			character = scope:host
		}
		# Inform liege of prestige from qualified knights
		tournament_liege_qualification_toast_effect = yes
		# Qualification ping
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				save_temporary_scope_as = new_list_member
				NOT = {
					scope:activity = {
						any_guest_subset_current_phase = {
							name = qualified
							this = scope:new_list_member
						}
					}
				}
			}
			tournament_contest_qualification_failure_effect = yes
		}
		# Fire starting event
		every_attending_character = {
			trigger_event = contest_events.0500
		}
	}
}

# Fire race events with correct spacing
tournament_race_pulse_effect = {
	scope:activity = {
		every_guest_subset_current_phase = {
			name = qualified
			limit = { exists = var:progress_to_victory }
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_random_pulse
				days = 7
			}
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_random_pulse
				days = 14
			}
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_pivotal_pulse
				days = 21
			}
		}
		every_attending_character = {
			limit = {
				OR = {
					is_in_guest_subset = { name = qualified }
					is_ai = no
				}
			}
			# If competing yourself, it is from your perspective, otherwise find someone relevant
		    tournament_pivotal_contestant_scope_effect = yes
			trigger_event = {
				on_action = contest_ongoing_event_$CONTEST$_outcome_pulse
				days = 25
			}
		}
	}
}

# Determines first, second, and third place in turn based contests
tournament_contest_race_winner_effect = {
	# Achievement.
	little_william_marshal_achievement_contest_end_effect = yes
	scope:activity = {
		every_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			set_variable = {
				name = progress_to_victory
				value = progress_to_victory_active_contestant_value
			}
		}
		save_scope_value_as = { name = contest_complete value = flag:$CONTEST$ }
		# Set winner if player
		if = {
			limit = {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					is_alive = yes
					exists = var:contest_aptitude
					exists = var:progress_to_victory
					is_ai = no
					save_temporary_scope_as = winner_temp
				}
				NOT = {
					any_guest_subset = {
						name = qualified
						phase = tournament_phase_$CONTEST$
						is_alive = yes
						exists = var:contest_aptitude
						exists = var:progress_to_victory
						var:progress_to_victory > scope:winner_temp.var:progress_to_victory
					}
				}
			}
			random_guest_subset = {
				name = qualified
				phase = tournament_phase_$CONTEST$
				limit = {
					is_alive = yes
					exists = var:contest_aptitude
					exists = var:progress_to_victory
					is_ai = no
					save_temporary_scope_as = winner_temp
					scope:activity  = {
						NOT = {
							any_guest_subset = {
								name = qualified
								phase = tournament_phase_$CONTEST$
								is_alive = yes
								exists = var:contest_aptitude
								exists = var:progress_to_victory
								var:progress_to_victory > scope:winner_temp.var:progress_to_victory
							}
						}
					}
				}
				save_scope_as = contest_winner
			}
		}
		else = {
			# Determine winner
			random_guest_subset = {
				name = qualified
				phase = tournament_phase_$CONTEST$
				limit = {
					is_alive = yes
					exists = var:progress_to_victory
					save_temporary_scope_as = winner_temp
				}
				weight = {
					base = 1
					# Weights heavily on PtV, and major differences in score
					tournament_contest_winner_determiner_modifier = { CHAR = scope:winner_temp CONTEST = $CONTEST$ }
				}
				save_scope_as = contest_winner
			}
		}
		scope:contest_winner = {
			tournament_horse_race_fetch_horse_effect = yes
			tournament_complete_win_contest_intent_effect = yes
			# Achievement.
			little_william_marshal_achievement_contest_end_winner_effect = yes
		}
		# Determine second place
		random_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				is_alive = yes
				exists = var:progress_to_victory
				this != scope:contest_winner
				save_temporary_scope_as = second_temp
			}
			weight = {
				base = 1
				# Weights heavily on PtV, and major differences in score
				tournament_contest_winner_determiner_modifier = { CHAR = scope:second_temp CONTEST = $CONTEST$ }
			}
			save_scope_as = second_place
			tournament_horse_race_fetch_horse_effect = yes
		}
		# Determine third place
		random_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				is_alive = yes
				exists = var:progress_to_victory
				NOR = {
					this = scope:contest_winner
					this = scope:second_place
				}
				save_temporary_scope_as = third_temp
			}
			weight = {
				base = 1
				# Weights heavily on PtV, and major differences in score
				tournament_contest_winner_determiner_modifier = { CHAR = scope:third_temp CONTEST = $CONTEST$ }
			}
			save_scope_as = third_place
			tournament_horse_race_fetch_horse_effect = yes
		}
		# Save all non-winner entrants so we can easily check if someone lost
		every_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				this != scope:contest_winner
			}
			add_to_list = contest_losers
		}
		# Save winner and add to overall tracker
		add_to_guest_subset = {
			name = winner
			target = scope:contest_winner
			phase = tournament_phase_$CONTEST$
		}
		# Save podium placements for reference
		if = {
			limit = { exists = scope:second_place }
			add_to_guest_subset = {
				name = second_place
				target = scope:second_place
				phase = tournament_phase_$CONTEST$
			}
		}
		if = {
			limit = { exists = scope:third_place }
			add_to_guest_subset = {
				name = third_place
				target = scope:third_place
				phase = tournament_phase_$CONTEST$
			}
		}
		# Rewards
		tournament_contest_rewards_effect = { CONTEST = $CONTEST$ }
		# Fire host's prize award event
		activity_host = { trigger_event = contest_events.0505 }
		# Clear redundant variables
		every_attending_character = { tournament_clear_contest_variables_effect = yes }
	}
}

###################
### VERSUS TYPE ###
###################

# Examples: Jousting, Single Combat, Board Game
# Versus contests are won through a tournament table

# Create versus contestant list and log entry  - contest_$CONTEST$_potential
tournament_contest_versus_composition_effect = {
	scope:activity = {
		set_variable = { name = tournament_commenced }
		if = {
			limit = { exists = var:contest_versus_progress }
			remove_variable = contest_versus_progress
		}
		# Players
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				OR = {
					is_ai = no
					AND = {
						has_court_position = champion_court_position
						liege = { is_ai = no }
					}
				}
				tournament_contest_can_compete_trigger = yes
				exists = var:progress_to_victory
				var:progress_to_victory >= scope:activity.var:progress_to_victory_threshold
			}
			add_tournament_contest_contestant_effect = {
				CONTEST = $CONTEST$
				SKILL = $SKILL$
				PTV = progress_to_victory_active_contestant_value
			}
		}
		# Filter relevant into entrants list
		while = {
			limit = {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					count < 7
				}
				any_guest_subset_current_phase = {
					name = contestant
					tournament_contest_can_compete_trigger = yes
				}
			}
			ordered_guest_subset = {
				name = contestant
				phase = tournament_phase_$CONTEST$
				limit = { tournament_contest_can_compete_trigger = yes }
				order_by = var:progress_to_victory
				add_tournament_contest_contestant_effect = {
					CONTEST = $CONTEST$
					SKILL = $SKILL$
					PTV = progress_to_victory_active_contestant_value
				}
			}
		}
		# Chance for a mysterious stranger
		hidden_effect = {
			random_list = {
				3 = {
					ordered_guest_subset = {
						name = contestant
						phase = tournament_phase_$CONTEST$
						limit = { tournament_contest_can_compete_trigger = yes }
						order_by = var:progress_to_victory
						add_tournament_contest_contestant_effect = {
							CONTEST = $CONTEST$
							SKILL = $SKILL$
							PTV = progress_to_victory_active_contestant_value
						}
					}
				}
				1 = {
					trigger = {
						NOT = { exists = var:mysterious_stranger_appeared }
						any_attending_character = {
							culture = {
								any_culture_county = {
									exists = faith
									any_direct_de_jure_vassal_title = { exists = this }
								}
							}
						}
						any_attending_character = { count <= 240 }
					}
					tournament_mysterious_stranger_effect = {
						CONTEST = $CONTEST$
						SKILL = $SKILL$
					}
				}
			}
		}
		# Inform liege of prestige from qualified knights
		tournament_liege_qualification_toast_effect = yes
		# Save scopes for tournament tables
		tournament_contest_versus_summary_scope_effect = yes
		# Qualification ping
		every_guest_subset = {
			name = contestant
			phase = tournament_phase_$CONTEST$
			limit = {
				save_temporary_scope_as = new_list_member
				NOT = {
					scope:activity = {
						any_guest_subset_current_phase = {
							name = qualified
							this = scope:new_list_member
						}
					}
				}
			}
			tournament_contest_qualification_failure_effect = yes
		}
		# Add Activity Log entry
		add_activity_log_entry = {
			key = tournament_contest_start_log
			tags = { contest }
			score = 20
			character = scope:host
		}
		switch = {
			trigger = has_current_phase
			tournament_phase_joust = {
				tournament_contest_versus_pulse_effect = { CONTEST = joust }
			}
			tournament_phase_duel = {
				tournament_contest_versus_pulse_effect = { CONTEST = duel }
			}
			tournament_phase_wrestling = {
				tournament_contest_versus_pulse_effect = { CONTEST = wrestling }
			}
			tournament_phase_board_game = {
				tournament_contest_versus_pulse_effect = { CONTEST = board_game }
			}
		}
		# Fire starting event
		every_attending_character = {
			trigger_event = contest_events.0800
		}
		trigger_event = {
			id = contest_events.0801
			days = 12
		}
		trigger_event = {
			id = contest_events.0801
			days = 19
		}
	}
}

# Pair opponents and fire summary event for current round
tournament_contest_versus_pulse_effect = {
	scope:activity = {
		if = {
			limit = {
				NOT = { exists = var:contest_versus_progress }
			}
			set_variable = {
				name = contest_versus_progress
				value = 0
			}
		}
		else = {
			# Progress tournament stage tracker
			change_variable = {
				name = contest_versus_progress
				add = 1
			}
		}
		# Resolve bouts for current stage (we use stage prior here to select from those who made it through)
		switch = {
			trigger = var:contest_versus_progress
			0 = {
				tournament_contest_versus_pair_effect = { CONTEST = $CONTEST$ STAGE = qualified }
			}
			1 = {
				tournament_contest_versus_pair_effect = { CONTEST = $CONTEST$ STAGE = semi_finalist }
			}
			2 = {
				tournament_contest_versus_pair_effect = { CONTEST = $CONTEST$ STAGE = finalist }
			}
		}
		# Save scopes for events
		tournament_contest_versus_summary_scope_effect = yes
		# Fire summary events
		every_attending_character = {
			limit = {
				OR = {
					is_in_guest_subset = { name = qualified }
					is_ai = no
				}
			}
			trigger_event = contest_events.0820
			trigger_event = {
				on_action = tournament_intent_setup_events
				days = 1
			}
		}
	}
}

# Pair off versus contestants into bout matches
tournament_contest_versus_pair_effect = {
 	# Remove any event score gained in last round
	every_guest_subset = {
		name = $STAGE$
		phase = tournament_phase_$CONTEST$
		add_to_list = valid_contestants
		set_variable = {
			name = progress_to_victory
			value = progress_to_victory_active_contestant_value
		}
	}
	if = {
		limit = {
			any_guest_subset = {
				name = $STAGE$ # e.g. quarter
				phase = tournament_phase_$CONTEST$
				count < contest_versus_$STAGE$_count
			}
		}
		debug_log = "Not enough versus contestants"
		while = {
			limit = {
				any_guest_subset = {
					name = $STAGE$ # e.g. quarter
					phase = tournament_phase_$CONTEST$
					count < contest_versus_$STAGE$_count
				}
			}
			if = {
				limit = {
					any_guest_subset = {
						name = contestant
						phase = tournament_phase_$CONTEST$
						save_temporary_scope_as = new_list_member
						NOT = {
							scope:activity = {
								any_guest_subset_current_phase = {
									name = $STAGE$
									this = scope:new_list_member
								}
							}
						}
						activity_is_valid_tournament_contestant = yes
					}
				}
				random_guest_subset = {
					name = contestant
					phase = tournament_phase_$CONTEST$
					limit = {
						save_temporary_scope_as = new_list_member
						NOT = {
							scope:activity = {
								any_guest_subset_current_phase = {
									name = $STAGE$
									this = scope:new_list_member
								}
							}
						}
						activity_is_valid_tournament_contestant = yes
					}
					save_scope_as = backup_contestant
				}
			}
			else = { tournament_versus_create_contestant_fallback_effect = yes }
			add_to_guest_subset = {
				name = $STAGE$
				target = scope:backup_contestant
				phase = tournament_phase_$CONTEST$
			}
		}
	}
	# Pair remaining contestants
	while = {
		limit = {
			any_guest_subset = {
				name = $STAGE$
				phase = tournament_phase_$CONTEST$
				NOT = { has_variable = contest_$STAGE$_match_$CONTEST$ }
			}
		}		
		ordered_guest_subset = {
			name = $STAGE$
			phase = tournament_phase_$CONTEST$
			limit = {
				NOT = { has_variable = contest_$STAGE$_match_$CONTEST$ }
			}
			order_by = {
				value = 50
				if = {
					limit = { scope:activity = { has_current_phase = tournament_phase_joust } }
					add = contest_horse_aptitude_value
				}
				else_if = {
					limit = { scope:activity = { has_current_phase = tournament_phase_duel } }
					add = contest_foot_aptitude_value
				}
				else_if = {
					limit = { scope:activity = { has_current_phase = tournament_phase_board_game } }
					add = contest_board_game_aptitude_value
				}
				else = { add = contest_pugilism_aptitude_value }
			}
			# Save pair of contestants
			save_scope_as = versus_matchee
			scope:activity = {
				random_guest_subset = {
					name = $STAGE$
					phase = tournament_phase_$CONTEST$
					limit = {
						NOR = {
							this = scope:versus_matchee
							has_variable = contest_$STAGE$_match_$CONTEST$
						}
					}
					weight = {
						base = 1
						# Prefer to leave rivalry matches later
						modifier = {
							factor = 0.5
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:versus_matchee }
						}
						# Prefer to leave player matches later
						modifier = {
							factor = 0.25
							is_ai = no
							scope:versus_matchee = { is_ai = no }
						}
						# Prefer to leave stranger matches later
						modifier = {
							factor = 0.25
							is_ai = yes
							scope:versus_matchee = { exists = var:mysterious_stranger_origin }
						}
					}
					save_scope_as = versus_matcher
				}
			}
			# Save stage matches for debug and fetching
			if = {
				limit = { scope:activity.var:contest_versus_progress = 2 }
				set_variable = {
					name = last_versus_match
					value = var:contest_semi_finalist_match_$CONTEST$
					years = 1
				}
				add_to_temporary_list = past_round_summary
			}
			else_if = {
				limit = { scope:activity.var:contest_versus_progress = 1 }
				set_variable = {
					name = last_versus_match
					value = var:contest_qualified_match_$CONTEST$
					years = 1
				}
				add_to_temporary_list = past_round_summary
			}
			set_variable = {
				name = contest_$STAGE$_match_$CONTEST$
				value = scope:versus_matcher
				years = 1
			}
			set_variable = {
				name = next_versus_match
				value = scope:versus_matcher
				years = 1
			}
			scope:versus_matcher = {
				if = {
					limit = { scope:activity.var:contest_versus_progress = 2 }
					set_variable = {
						name = last_versus_match
						value = var:contest_semi_finalist_match_$CONTEST$
						years = 1
					}
					add_to_temporary_list = past_round_summary
				}
				else_if = {
					limit = { scope:activity.var:contest_versus_progress = 1 }
					set_variable = {
						name = last_versus_match
						value = var:contest_qualified_match_$CONTEST$
						years = 1
					}
					add_to_temporary_list = past_round_summary
				}
				set_variable = {
					name = contest_$STAGE$_match_$CONTEST$
					value = scope:versus_matchee
					years = 1
				}
				set_variable = {
					name = next_versus_match
					value = scope:versus_matchee
					years = 1
				}
				add_to_temporary_list = past_round_summary
			}
			debug_log_scopes = yes
			### Resolve bouts
			# AI only
			if = {
				limit = {
					scope:versus_matchee = { is_ai = yes }
					scope:versus_matcher = { is_ai = yes }
				}
				scope:versus_matchee = {
					trigger_event = {
						id = contest_events.0830 # Both AI - background script
						days = 6
					}
				}
			}
			# Player involvement
			else = {
				scope:versus_matchee = {
					if = {
						limit = { is_ai = no }
						trigger_event = {
							on_action = contest_bout_$CONTEST$_random_pulse
							days = 2
						}
						trigger_event = {
							on_action = contest_bout_$CONTEST$_pivotal_pulse
							days = 4
						}
					}
				}
				scope:versus_matcher = {
					if = {
						limit = { is_ai = no }
						trigger_event = {
							on_action = contest_bout_$CONTEST$_random_pulse
							days = 2
						}
						trigger_event = {
							on_action = contest_bout_$CONTEST$_pivotal_pulse
							days = 4
						}
					}
				}
			}
		}
	}
}

# Save scopes of opponent based on bout stage
tournament_contest_versus_round_scope_effect = {
	if = {
		limit = { scope:activity.var:contest_versus_progress = 0 }
		if = {
			limit = { exists = var:contest_qualified_match_$CONTEST$ }
			var:contest_qualified_match_$CONTEST$ = { save_scope_as = versus_contestant }
		}
	}
	else_if = {
		limit = { scope:activity.var:contest_versus_progress = 1 }
		if = {
			limit = { exists = var:contest_semi_finalist_match_$CONTEST$ }
			var:contest_semi_finalist_match_$CONTEST$ = { save_scope_as = versus_contestant }
		}
	}
	else = {
		if = {
			limit = { exists = var:contest_finalist_match_$CONTEST$ }
			var:contest_finalist_match_$CONTEST$ = { save_scope_as = versus_contestant }
		}
	}
}

tournament_contest_versus_match_scope_effect = {
	scope:activity = {
		switch = {
			trigger = has_current_phase
			tournament_phase_joust = {
				$CHAR$ = { tournament_contest_versus_round_scope_effect = { CONTEST = joust } }
			}
			tournament_phase_wrestling = {
				$CHAR$ = { tournament_contest_versus_round_scope_effect = { CONTEST = wrestling } }
			}
			tournament_phase_duel = {
				$CHAR$ = { tournament_contest_versus_round_scope_effect = { CONTEST = duel } }
			}
			tournament_phase_board_game = {
				$CHAR$ = { tournament_contest_versus_round_scope_effect = { CONTEST = board_game } }
			}
		}
	}
}

# Determine versus wound/death outcomes
tournament_contest_versus_battle_wound_effect = {
	scope:contest_loser = {
		if = {
			limit = { exists = var:murder_sabotage_target }
			random = {
				chance = 50
				modifier = {
					add = {
						value = var:murder_sabotage_target
						multiply = 15
					}
				}
				increase_wounds_effect = { REASON = contest_$CONTEST$_accident }
				save_scope_as = sabotage_murder_wound
			}
			random = {
				chance = 50
				modifier = {
					add = {
						value = var:murder_sabotage_target
						multiply = 15
					}
				}
				add_character_flag = died_in_contest_flag
				death = {
					death_reason = death_contest_$CONTEST$_accident
					killer = scope:contest_winner
				}
				save_scope_as = sabotage_murder_death
			}
			if = {
				limit = { is_alive = yes }
				remove_variable = murder_sabotage_target	
			}
		}
		else = {
			random = {
				chance = $WOUND_CHANCE$
				increase_wounds_effect = { REASON = contest_$CONTEST$_accident }
			}
			random = {
				chance = $DEATH_CHANCE$
				add_character_flag = died_in_contest_flag
				death = {
					death_reason = death_contest_$CONTEST$_accident
					killer = scope:contest_winner
				}
			}
		}
	}
}

# Randomize versus bout outcome based on skill
tournament_contest_versus_battle_effect = {
	save_temporary_scope_value_as = {
		name = choose_winner
		value = $CHOOSE_WINNER$
	}
	save_temporary_scope_value_as = {
		name = injury_chance
		value = $INJURY_CHANCE$
	}
	set_variable = {
		name = progress_to_victory
		value = progress_to_victory_active_contestant_value
	}
	var:next_versus_match = {
		save_scope_as = next_versus_match
		if = {
			limit = { is_alive = yes }
			set_variable = {
				name = progress_to_victory
				value = progress_to_victory_active_contestant_value
			}
		}
	}
	# Determine winner
	if = {
		limit = { scope:choose_winner = yes }
		random_list = {
			# ROOT WINS
			10 = {
				trigger = {
					is_alive = yes
					save_temporary_scope_as = versus_matchee_temp
				}
				desc = tournament_contest_versus_battle_win_tt
				modifier = {
					exists = var:progress_to_victory_score
					add = var:progress_to_victory_score
				}
				modifier = {
					exists = var:contest_aptitude
					add = {
						value = var:contest_aptitude
						divide = 2
					}
				}
				# Prefer unmatched rivals of players to not get knocked out
				modifier = {
					is_ai = yes
					scope:next_versus_match = { is_ai = yes }
					scope:activity = {
						any_guest_subset = {
							name = contestant
							exists = var:progress_to_victory
							this != scope:next_versus_match
							is_ai = no
							has_any_bad_relationship_with_character_trigger = { CHARACTER = root }
						}
					}
					factor = 2
				}
				modifier = {
					has_variable = murder_sabotage_target
					factor = 0.5
				}
				# If aptitude is higher, more likely to win
				modifier = {
					exists = var:contest_aptitude
					exists = scope:next_versus_match.var:contest_aptitude
					var:contest_aptitude > scope:next_versus_match.var:contest_aptitude
					factor = 2
				}
				# If have at least double aptitude of other character, you win
				modifier = {
					exists = var:contest_aptitude
					exists = scope:next_versus_match.var:contest_aptitude
					contest_aptitude_fetch_half_value >= scope:next_versus_match.var:contest_aptitude
					add = 1000
				}
				show_chance = no
                versus_contest_round_outcome_tooltip_effect = {
					WINNER = root
					LOSER = scope:next_versus_match
				}
				save_scope_as = contest_winner
				scope:next_versus_match = { save_scope_as = contest_loser }
			}
			# VERSUS CONTESTANT WINS
			10 = {
				desc = tournament_contest_versus_battle_lose_tt
				trigger = {
					scope:next_versus_match = { is_alive = yes }
				}
				modifier = {
					exists = scope:next_versus_match.var:progress_to_victory_score
					add = scope:next_versus_match.var:progress_to_victory_score
				}
				modifier = {
					exists = scope:next_versus_match.var:contest_aptitude
					add = {
						value = scope:next_versus_match.var:contest_aptitude
						divide = 2
					}
				}
				# Prefer unmatched rivals of players to not get knocked out
				modifier = {
					is_ai = yes
					scope:next_versus_match = { is_ai = yes }
					scope:activity = {
						any_guest_subset = {
							name = contestant
							exists = var:progress_to_victory
							this != root
							is_ai = no
							has_any_bad_relationship_with_character_trigger = { CHARACTER = scope:next_versus_match }
						}
					}
					factor = 2
				}
				modifier = {
					scope:next_versus_match = { has_variable = murder_sabotage_target }
					factor = 0.5
				}
				# If aptitude is higher, more likely to win
				modifier = {
					exists = scope:next_versus_match.var:contest_aptitude
					exists = var:contest_aptitude
					scope:next_versus_match.var:contest_aptitude > root.var:contest_aptitude
					factor = 2
				}
				# If they have at least double your aptitude, they win
				modifier = {
					exists = scope:next_versus_match.var:progress_to_victory
					scope:next_versus_match.progress_to_victory_fetch_half_value >= root.var:progress_to_victory
					add = 1000
				}
				show_chance = no
                versus_contest_round_outcome_tooltip_effect = {
					WINNER = scope:next_versus_match
					LOSER = root
				}
				scope:next_versus_match = { save_scope_as = contest_winner }
				save_scope_as = contest_loser
			}
		}
	}
	if = {
		limit = {
			exists = scope:contest_winner.var:contest_aptitude
			exists = scope:contest_loser.var:contest_aptitude
		}
		save_scope_value_as = {
			name = score_difference
			value = {
				value = scope:contest_winner.var:contest_aptitude
				subtract = scope:contest_loser.var:contest_aptitude
			}
		}
	}
	# Save contest winner and add activity log entry
	scope:activity = {
		switch = {
			trigger = scope:activity.var:contest_versus_progress
			0 = {
				add_to_guest_subset = {
					name = semi_finalist
					target = scope:contest_winner
					phase = tournament_phase_$CONTEST$
				}
				add_activity_log_entry = {
					key = tournament_bout_quarter_log
					tags = { bout contest }
					score = 10
					character = scope:contest_winner
					target = scope:contest_loser
				}
			}
			1 = {
				add_to_guest_subset = {
					name = finalist
					target = scope:contest_winner
					phase = tournament_phase_$CONTEST$
				}
				add_activity_log_entry = {
					key = tournament_bout_semi_log
					tags = { bout contest }
					score = 10
					character = scope:contest_winner
					target = scope:contest_loser
				}
			}
			2 = {
				add_to_guest_subset = {
					name = winner
					target = scope:contest_winner
					phase = tournament_phase_$CONTEST$
				}
				scope:contest_winner = {
					set_variable = {
						name = progress_to_victory
						value = 100
					}
				}
				add_to_guest_subset = {
					name = second_place
					target = scope:contest_loser
					phase = tournament_phase_$CONTEST$
				}
			}
		}
	}
	# Chance of injuries
	if = {
		limit = { scope:injury_chance ?= yes }
		scope:activity = {
			switch = {
				trigger = has_current_phase
				tournament_phase_joust = {
					scope:contest_winner = {
						set_variable = {
							name = contest_aptitude
							value = contest_horse_aptitude_value
						}
					}
					tournament_contest_versus_battle_wound_effect = {
						WOUND_CHANCE = 30
						DEATH_CHANCE = 5
						CONTEST = joust
					}
				}
				tournament_phase_wrestling = {
					scope:contest_winner = {
						set_variable = {
							name = contest_aptitude
							value = contest_pugilism_aptitude_value
						}
					}
					tournament_contest_versus_battle_wound_effect = {
						WOUND_CHANCE = 10
						DEATH_CHANCE = 1
						CONTEST = wrestling
					}
				}
				tournament_phase_duel = {
					scope:contest_winner = {
						set_variable = {
							name = contest_aptitude
							value = contest_foot_aptitude_value
						}
					}
					tournament_contest_versus_battle_wound_effect = {
						WOUND_CHANCE = 20
						DEATH_CHANCE = 3
						CONTEST = duel
					}
				}
				tournament_phase_board_game = {
					scope:contest_winner = {
						set_variable = {
							name = contest_aptitude
							value = contest_board_game_aptitude_value
						}
					}
					tournament_contest_versus_battle_wound_effect = {
						WOUND_CHANCE = 5
						DEATH_CHANCE = 1
						CONTEST = board_game
					}
				}
			}
		}
	}
	scope:contest_winner = {
		if = {
			limit = {
				scope:activity = {
					any_guest_subset_current_phase = {
						name = winner
						this = scope:contest_winner
					}
				}
			}
			set_variable = {
				name = progress_to_victory
				value = 100
			}
		}
		else = {
			set_variable = {
				name = progress_to_victory
				value = progress_to_victory_active_contestant_value
			}
		}
	}
	scope:contest_loser = {
		if = {
			limit = { is_alive = yes }
			remove_variable = contest_aptitude
			remove_variable = progress_to_victory
		}
	}
	# Consequences for sabotage murder
	if = {
		limit = {
			OR = {
				exists = scope:sabotage_murder_wound
				exists = scope:sabotage_murder_death
			}
		}
		switch = {
			trigger = exists
			scope:sabotage_murder_wound = {
				attempted_murder_opinion_effect = { VICTIM = scope:contest_loser MURDERER = scope:contest_winner }
			}
			scope:sabotage_murder_death = {
				murder_opinion_effect = { VICTIM = scope:contest_loser MURDERER = scope:contest_winner }
			}
		}
		if = {
			limit = {
				scope:host = {
					is_alive = yes
					is_ai = yes
					this = scope:contest_loser
				}
			}
			random = {
				chance = 50
				modifier = {
					add = 25
					has_trait = vengeful
				}
				modifier = {
					add = 25
					has_trait = wrathful
				}
				modifier = {
					add = -25
					has_trait = forgiving
				}
				rightfully_imprison_character_effect = {
					TARGET = scope:contest_winner
					IMPRISONER = scope:host
				}
			}
		}
	}
	scope:contest_winner = {
		save_scope_as = pivotal_contestant
		trigger_event = { on_action = contest_bout_$CONTEST$_outcome_pulse }
	}
	scope:contest_loser = {
		save_scope_as = pivotal_contestant
		trigger_event = { on_action = contest_bout_$CONTEST$_outcome_pulse }
	}
	scope:activity = {
		# Quarter and semi finals only show pivotals to contestants, and pertinent bout pings to everyone else
		if = {
			limit = { var:contest_versus_progress < 2 }
			tournament_contest_versus_pertinent_update_effect = yes
		}
		# Everyone sees final pivotals
		else = {
			every_attending_character = {
				limit = {
					NOR = {
						this = scope:contest_winner
						this = scope:contest_loser
					}
					is_ai = no
				}
				# If competing yourself, it is from your perspective, otherwise find someone relevant
				tournament_pivotal_contestant_scope_effect = yes
				trigger_event = { on_action = contest_bout_$CONTEST$_outcome_pulse }
			}
		}	
	}
}

tournament_contest_versus_pertinent_update_effect = {
	scope:activity = {
		every_attending_character = {
			limit = {
				is_ai = no
				NOR = {
					this = scope:contest_winner
					this = scope:contest_loser
					AND = {
						exists = scope:wrathful_contestant
						this = scope:wrathful_contestant
					}
					AND = {
						exists = scope:wrathful_victim
						this = scope:wrathful_victim
					}
				}
				# First round only notify people who care
				trigger_if = {
					limit = { scope:activity.var:contest_versus_progress < 1 }
					OR = {
						# Scripted Relation
						has_any_scripted_relation = scope:contest_winner
						has_any_scripted_relation = scope:contest_loser
						# Close Family
						is_close_family_of = scope:contest_winner
						is_close_family_of = scope:contest_loser
						# Spouses
						is_consort_of = scope:contest_winner
						is_consort_of = scope:contest_loser
						# Host
						scope:host = scope:contest_winner
						scope:host = scope:contest_loser
						# Champion Liege
						court_position:champion_court_position ?= scope:contest_winner
						court_position:champion_court_position ?= scope:contest_loser
						# Knights
						any_acclaimed_knight = { this = scope:contest_winner }
						any_acclaimed_knight = { this = scope:contest_loser }
						# Intent Target
						trigger_if = {
							limit = { exists = intent_target }
							OR = {
								intent_target = scope:contest_winner
								intent_target = scope:contest_loser
							}
						}
					}
				}
			}
			trigger_event = contest_events.0810
		}
	}
}

# Set round participants after resignations
tournament_contest_versus_resignation_effect = {
	$RESIGNER$ = { save_scope_as = resign_target }
	scope:resign_target = {
		save_scope_as = contest_loser
		remove_variable = contest_aptitude
		add_character_flag = {
			flag = resigned_contest_flag
			months = 2
		}
	}
	scope:activity = {
		switch = {
			trigger = var:contest_versus_progress
			0 = {
				scope:resign_target.var:contest_qualified_match_$CONTEST$ = { save_scope_as = resign_match }
				add_to_guest_subset = {
					name = semi_finalist
					target = scope:resign_match
					phase = tournament_phase_$CONTEST$
				}
			}
			1 = {
				scope:resign_target.var:contest_semi_finalist_match_$CONTEST$ = { save_scope_as = resign_match }
				add_to_guest_subset = {
					name = finalist
					target = scope:resign_match
					phase = tournament_phase_$CONTEST$
				}
			}
			2 = {
				scope:resign_target.var:contest_finalist_match_$CONTEST$ = { save_scope_as = resign_match }
				add_to_guest_subset = {
					name = winner
					target = scope:resign_match
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = second_place
					target = scope:resign_target
					phase = tournament_phase_$CONTEST$
				}
			}
		}
	}
	scope:resign_match = {
		save_scope_as = contest_winner
		remove_variable = contest_aptitude
		set_variable = {
			name = contest_aptitude
			value = contest_$SKILL$_aptitude_value
		}
	}
	scope:activity = {
		add_activity_log_entry = {
			key = tournament_resigned_versus_log
			tags = { contestant removal }
			score = 25
			character = scope:contest_loser
			target = scope:contest_winner

			# Effect
			versus_contest_resignation_tooltip_effect = {
				RESIGNER = scope:resign_target
				WINNER = scope:resign_match
			}
		}
		every_attending_character = {
			limit = {
				is_ai = no
				NOR = {
					this = scope:contest_winner
					this = scope:contest_loser
				}
			}
			trigger_event = {
				id = contest_events.0825
				days = 1 
			}
		}
	}
}

# Set round participants after disqualifications
tournament_contest_versus_disqualification_effect = {
	$DISQUALIFIED$ = { save_scope_as = disqualify_target }
	$MATCH$ = { save_scope_as = disqualify_match }
	scope:disqualify_target = {
		save_scope_as = contest_loser
		remove_variable = contest_aptitude
		remove_variable = progress_to_victory
		add_character_flag = {
			flag = disqualified_contest_flag
			months = 2
		}
	}
	scope:disqualify_match = {
		save_scope_as = contest_winner
		remove_variable = contest_aptitude
		set_variable = {
			name = contest_aptitude
			value = contest_$SKILL$_aptitude_value
		}
		set_variable = {
			name = progress_to_victory
			value = progress_to_victory_active_contestant_value
		}
	}
	scope:activity = {
		switch = {
			trigger = var:contest_versus_progress
			0 = {
				add_to_guest_subset = {
					name = semi_finalist
					target = scope:disqualify_match
					phase = tournament_phase_$CONTEST$
				}
			}
			1 = {
				add_to_guest_subset = {
					name = finalist
					target = scope:disqualify_match
					phase = tournament_phase_$CONTEST$
				}
			}
			2 = {
				add_to_guest_subset = {
					name = winner
					target = scope:disqualify_match
					phase = tournament_phase_$CONTEST$
				}
			}
		}
		add_activity_log_entry = {
			key = tournament_disqualified_log
			score = 50
			tags = { contestant removal }
			character = scope:contest_loser
			target = scope:host

			# Effect
			scope:contest_loser = {
				if ={
					limit = { scope:activity.var:contest_versus_progress = 2 }
					custom_description = {
						text = tournament_contest_disqualify_win_tt
						subject = scope:disqualify_match # WINNER
						object = scope:disqualify_target # DISQUALIFIED
					}
				}
				else = {
					custom_description = {
						text = tournament_contest_disqualify_round_tt
						subject = scope:disqualify_match # WINNER
						object = scope:disqualify_target # DISQUALIFIED
					}
				}
				if = {
					limit = { is_ai = yes }
					add_opinion = {
						target = scope:host
						modifier = tournament_disqualified_opinion
						opinion = -15
					}
				}
			}
		}
		every_attending_character = {
			limit = {
				is_ai = no
				trigger_if = {
					limit = { exists = scope:contest_winner }
					this != scope:contest_winner
				}
				trigger_if = {
					limit = { exists = scope:contest_loser }
					this != scope:contest_loser
				}
			}
			trigger_event = contest_events.0826
		}
	}
}

# Disqualify qualified contestant from contest
tournament_contest_disqualification_effect = {
	$DISQUALIFIED$ = { save_scope_as = disqualify_target }
	scope:disqualify_target = {
		remove_variable = contest_aptitude
		remove_variable = progress_to_victory
		add_character_flag = {
			flag = disqualified_contest_flag
			months = 2
		}
	}
	custom_description = {
		text = tournament_contest_disqualify_tt
		subject = $DISQUALIFIED$
	}
}

# Swap determined winner and loser
tournament_contest_versus_swap_winner_loser_effect = {
	switch = {
		trigger = scope:activity.var:contest_versus_progress
		0 = {
			remove_from_guest_subset = {
				name = semi_finalist
				target = scope:contest_winner
				phase = tournament_phase_$CONTEST$
			}
			add_to_guest_subset = {
				name = semi_finalist
				target = scope:contest_loser
				phase = tournament_phase_$CONTEST$
			}
		}
		1 = {
			remove_from_guest_subset = {
				name = finalist
				target = scope:contest_winner
				phase = tournament_phase_$CONTEST$
			}
			add_to_guest_subset = {
				name = finalist
				target = scope:contest_loser
				phase = tournament_phase_$CONTEST$
			}
		}
		2 = {
			remove_from_guest_subset = {
				name = winner
				target = scope:contest_winner
				phase = tournament_phase_$CONTEST$
			}
			add_to_guest_subset = {
				name = winner
				target = scope:contest_loser
				phase = tournament_phase_$CONTEST$
			}
			remove_from_guest_subset = {
				name = second_place
				target = scope:contest_loser
				phase = tournament_phase_$CONTEST$
			}
			add_to_guest_subset = {
				name = second_place
				target = scope:contest_winner
				phase = tournament_phase_$CONTEST$
			}
		}
	}
	scope:contest_winner = {
		remove_variable = contest_aptitude
		remove_variable = progress_to_victory
		save_scope_as = disqualify_target
	}
	scope:contest_loser = {
		set_variable = {
			name = progress_to_victory
			value = progress_to_victory_active_contestant_value
		}
		set_variable = {
			name = contest_aptitude
			value = contest_$SKILL$_aptitude_value
		}
	}
	versus_contest_disqualification_tooltip_effect = {
		WINNER = scope:contest_loser
		DISQUALIFIED = scope:contest_winner
	}
}

# Replace qualified contestant for another - versus version
tournament_contest_versus_replace_qualifier_effect = {
	$NEW_CONTESTANT$ = { save_scope_as = new_contestant }
	$OLD_CONTESTANT$ = { save_scope_as = old_contestant }
	if = {
		limit = {
			NOT = { exists = scope:activity.var:contest_versus_progress }
		}
		remove_from_guest_subset = {
			name = qualified
			target = scope:old_contestant
			phase = tournament_phase_$CONTEST$
		}
		add_to_guest_subset = {
			name = spectator
			target = scope:old_contestant
			phase = tournament_phase_$CONTEST$
		}
		add_to_guest_subset = {
			name = qualified
			target = scope:new_contestant
			phase = tournament_phase_$CONTEST$
		}
	}
	else = {
		switch = {
			trigger = scope:activity.var:contest_versus_progress
			0 = {
				remove_from_guest_subset = {
					name = qualified
					target = scope:old_contestant
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = spectator
					target = scope:old_contestant
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = qualified
					target = scope:new_contestant
					phase = tournament_phase_$CONTEST$
				}
				scope:new_contestant = {
					set_variable = {
						name = contest_qualified_match_$CONTEST$
						value = scope:old_contestant.var:contest_qualified_match_$CONTEST$
						years = 1
					}
				}
				scope:old_contestant = { remove_variable = contest_qualified_match_$CONTEST$ }
			}
			1 = {
				remove_from_guest_subset = {
					name = semi_finalist
					target = scope:old_contestant
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = spectator
					target = scope:old_contestant
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = semi_finalist
					target = scope:new_contestant
					phase = tournament_phase_$CONTEST$
				}
				scope:new_contestant = {
					set_variable = {
						name = contest_semi_finalist_match_$CONTEST$
						value = scope:old_contestant.var:contest_qualified_match_$CONTEST$
						years = 1
					}
				}
				scope:old_contestant = { remove_variable = contest_semi_finalist_match_$CONTEST$ }
			}
			2 = {
				remove_from_guest_subset = {
					name = finalist
					target = scope:old_contestant
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = spectator
					target = scope:old_contestant
					phase = tournament_phase_$CONTEST$
				}
				add_to_guest_subset = {
					name = finalist
					target = scope:new_contestant
					phase = tournament_phase_$CONTEST$
				}
				scope:new_contestant = {
					set_variable = {
						name = contest_finalist_match_$CONTEST$
						value = scope:old_contestant.var:contest_qualified_match_$CONTEST$
						years = 1
					}
				}
				scope:old_contestant = { remove_variable = contest_finalist_match_$CONTEST$ }
			}
		}
	}
	scope:old_contestant = {
		remove_variable = contest_aptitude
		remove_variable = progress_to_victory
		save_scope_as = disqualify_target
	}
	scope:new_contestant = {
		tournament_set_current_aptitude_variable_effect = yes
		set_variable = {
			name = progress_to_victory
			value = progress_to_victory_active_contestant_value
		}
	}
	scope:activity = {
		every_attending_character = {
			limit = { is_ai = no }
			send_interface_toast = {
				title = tournament_qualifier_replaced_toast
				left_icon = scope:old_contestant
				right_icon = scope:new_contestant
				custom_description = {
					text = tournament_contest_replaced_tt
					subject = scope:new_contestant # REPLACER
					object = scope:old_contestant # REPLACED
				}
			}
		}
	}
}

# Save scopes for round summary events
tournament_contest_versus_summary_scope_effect = {
	if = {
		limit = { exists = var:contest_versus_progress }
		switch = {
			trigger = var:contest_versus_progress
			1 = { # Semi finalists
				ordered_guest_subset_current_phase = {
					name = semi_finalist
					limit = {
						NOR = {
							scope:contest_summary_1 ?= this
							scope:contest_summary_2 ?= this
							scope:contest_summary_3 ?= this
						}
					}
					max = 4
					check_range_bounds = no
					order_by = primary_title.tier
					if = {
						limit = { NOT = { exists = scope:contest_summary_1 } }
						save_scope_as = contest_summary_1
					}
					else_if = {
						limit = { NOT = { exists = scope:contest_summary_2 } }
						save_scope_as = contest_summary_2
					}
					else_if = {
						limit = { NOT = { exists = scope:contest_summary_3 } }
						save_scope_as = contest_summary_3
					}
					else = { save_scope_as = contest_summary_4 }
				}
			}
			2 = { # Finalist
				ordered_guest_subset_current_phase = {
					name = finalist
					limit = {
						NOT = { scope:contest_summary_1 ?= this }
					}
					max = 2
					check_range_bounds = no
					order_by = primary_title.tier
					if = {
						limit = { NOT = { exists = scope:contest_summary_1 } }
						save_scope_as = contest_summary_1
					}
					else = { save_scope_as = contest_summary_2 }
				}	
			}
		}
	}
}

# Save winner and second place scopes, award prizes, and fire ping events
tournament_contest_versus_winner_effect = {
	# Achievement.
	little_william_marshal_achievement_contest_end_effect = yes
	scope:activity = {
		save_scope_value_as = { name = contest_complete value = flag:$CONTEST$ }
		# Save winner and add to overall winner tracker
		random_guest_subset = {
			name = winner
			phase = tournament_phase_$CONTEST$
			save_scope_as = contest_winner
			tournament_complete_win_contest_intent_effect = yes
			# Achievement.
			little_william_marshal_achievement_contest_end_winner_effect = yes
		}
		# Save other finalist as second place
		random_guest_subset = {
			name = second_place
			phase = tournament_phase_$CONTEST$
			save_scope_as = second_place
		}
		if = {
			limit = {
				NOT = { exists = scope:second_place }
			}
			scope:contest_winner.var:next_versus_match ?= { save_scope_as = second_place }
		}
		# Save all non-winner entrants so we can easily check if someone lost
		every_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			limit = {
				NOT = { scope:contest_winner ?= this }
			}
			add_to_list = contest_losers
		}
		# Rewards
		tournament_contest_rewards_effect = { CONTEST = $CONTEST$ }
		# Fire host's prize award event
		activity_host = { trigger_event = contest_events.0805 }
		# Clear redundant variables
		remove_variable = contest_versus_progress
		every_attending_character = { tournament_clear_contest_variables_effect = yes }
	}
}

# List correct wound status for versus contests 
tournament_wounded_or_dead_contest_effect = {
	scope:activity = {
		switch = {
			trigger = has_current_phase
			tournament_phase_joust = {
				scope:contest_loser = {
					tournament_wounded_or_dead_tooltip_effect = { CONTEST = joust }
				}
			}
			tournament_phase_wrestling = {
				scope:contest_loser = {
					tournament_wounded_or_dead_tooltip_effect = { CONTEST = wrestling }
				}
			}
			tournament_phase_duel = {
				scope:contest_loser = {
					tournament_wounded_or_dead_tooltip_effect = { CONTEST = duel }
				}
			}
			tournament_phase_board_game = {
				scope:contest_loser = {
					tournament_wounded_or_dead_tooltip_effect = { CONTEST = board_game }
				}
			}
		}
	}
}

# Save versus matched contestant's scopes
contest_versus_player_vs_ai_scope_effect = {
	if = {
		limit = { this = scope:versus_matchee }
		scope:versus_matcher = { save_scope_as = versus_contestant }
	}
	else = {
		scope:versus_matchee = { save_scope_as = versus_contestant }
	}
}

# Fallback for replacing dead versus contestant with a fresh one
tournament_versus_qualifier_death_effect = {
	if = {
		limit = {
			involved_activity ?= {
				has_activity_type = activity_tournament
				OR = {
					has_current_phase = tournament_phase_duel
					has_current_phase = tournament_phase_wrestling
					has_current_phase = tournament_phase_board_game
					has_current_phase = tournament_phase_joust
				}
				any_guest_subset_current_phase = { name = qualified this = root }
			}
			NOT = { has_character_flag = died_in_contest_flag }
		}
		save_scope_as = dead_contestant
		var:next_versus_match = { save_scope_as = versus_match }
		involved_activity = {
			save_scope_as = activity
			if = {
				limit = {
					scope:versus_match = { is_in_guest_subset = { name = finalist } }
				}
				tournament_versus_dead_qualifier_effect = { SUBSET = winner }
			}
			else_if = {
				limit = {
					scope:versus_match = { is_in_guest_subset = { name = semi_finalist } }
				}
				tournament_versus_dead_qualifier_effect = { SUBSET = finalist }
			}
			else = {
				tournament_versus_dead_qualifier_effect = { SUBSET = semi_finalist }
			}
		}
		every_player = {
			limit = { is_participant_in_activity = scope:activity }
			send_interface_toast = {
				title = tournament_qualifier_dies_toast
				left_icon = scope:dead_contestant
				right_icon = scope:versus_contestant
				custom_tooltip = tournament_quailfier_dies_tt
			}
		}
	}
}

# Used to add new qualifiers to correct versus subset
tournament_versus_new_qualifier_effect = {
	switch = {
		trigger = has_current_phase
		tournament_phase_duel = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_duel
				target = scope:new_qualifier
			}
		}
		tournament_phase_wrestling = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_wrestling
				target = scope:new_qualifier
			}
		}
		tournament_phase_board_game = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_board_game
				target = scope:new_qualifier
			}
		}
		tournament_phase_joust = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_joust
				target = scope:new_qualifier
			}
		}
	}
}

tournament_versus_dead_qualifier_effect = {
	switch = {
		trigger = has_current_phase
		tournament_phase_duel = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_duel
				target = scope:versus_match
			}
		}
		tournament_phase_wrestling = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_wrestling
				target = scope:versus_match
			}
		}
		tournament_phase_board_game = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_board_game
				target = scope:versus_match
			}
		}
		tournament_phase_joust = {
			add_to_guest_subset = {
				name = $SUBSET$
				phase = tournament_phase_joust
				target = scope:versus_match
			}
		}
	}
}

###########################
### MYSTERIOUS STRANGER ###
###########################

# Create a mysterious stranger contestant
tournament_mysterious_stranger_effect = {
	random_attending_character = {
		culture = { save_scope_as = attending_culture }
	}
	scope:attending_culture = {
		random_culture_county = {
			faith = { save_scope_as = attending_faith }
			random_direct_de_jure_vassal_title = { save_scope_as = attending_barony }
		}
	}
	create_character = {
		template = tournament_$SKILL$_character
		location = scope:activity.activity_location
		faith = scope:attending_faith
		culture = scope:attending_culture
		dynasty = none
		save_scope_as = mysterious_stranger
	}
	scope:mysterious_stranger = {
		set_variable = {
			name = mysterious_stranger_origin
			value = scope:attending_barony
		}
		set_variable = {
			name = contest_aptitude
			value = contest_$SKILL$_aptitude_value
		}
		add_to_activity = scope:activity
	}
	add_activity_log_entry = {
		key = contest_mysterious_stranger_log
		tags = { contestant }
		score = 25
		character = scope:mysterious_stranger

		# Effect
		show_as_tooltip = {
			scope:mysterious_stranger = { add_to_activity = scope:activity }
		}
	}
	add_to_guest_subset = {
		name = qualified
		target = scope:mysterious_stranger
		phase = tournament_phase_$CONTEST$
	}
	set_variable = mysterious_stranger_appeared
}

##############
### WAGERS ###
##############

# Find scopes for possible wagers
tournament_generate_wager_scope_effect = {
	# Top contestant - for maths
	ordered_guest_subset_current_phase = {
		name = qualified
		limit = {
			NOT = { exists = var:mysterious_stranger_origin }
		}
		order_by = var:contest_aptitude
		save_scope_as = wager_top_contestant
		tournament_horse_race_fetch_horse_effect = yes
	}
	save_scope_value_as = {
		name = wager_diff_value
		value = {
			add = scope:wager_top_contestant.var:contest_aptitude
			divide = 2
		}
	}
	# Bottom contestant - for maths
	ordered_guest_subset_current_phase = {
		name = qualified
		order_by = {
			value = 0
			subtract = var:contest_aptitude
		}
		save_scope_as = wager_bottom_contestant
		tournament_horse_race_fetch_horse_effect = yes
	}
	# Champion contestant
	if = {
		limit = {
			scope:wagerer = { employs_court_position = champion_court_position }
			any_guest_subset_current_phase = {
				name = qualified
				has_court_position = champion_court_position
				liege = scope:wagerer
				NOR = {
					this = scope:wager_top_contestant
					this = scope:wager_bottom_contestant
				}
			}
		}
		random_guest_subset_current_phase = {
			name = qualified
			limit = {
				has_court_position = champion_court_position
				liege = scope:wagerer
				NOR = {
					this = scope:wager_top_contestant
					this = scope:wager_bottom_contestant
				}
			}
			save_scope_as = wager_champion_contestant
			if = {
				limit = {
					scope:activity = { has_current_phase = tournament_phase_horse_race }
				}
				tournament_horse_race_fetch_horse_effect = yes
			}
		}
	}
	# Flavor contestant
	if = {
		limit = {
			any_guest_subset_current_phase = {
				name = qualified
				OR = {
					has_any_good_relationship_with_character_trigger = { CHARACTER = root }
					is_child_of = root
					is_spouse_of = root
				}
				NOR = {
					this = scope:wager_top_contestant
					AND = {
						exists = scope:wager_champion_contestant
						this = scope:wager_champion_contestant
					}
				}
			}
		}
		ordered_guest_subset_current_phase = {
			name = qualified
			limit = {
				OR = {
					has_any_good_relationship_with_character_trigger = { CHARACTER = root }
					is_child_of = root
					is_spouse_of = root
				}
				NOR = {
					this = scope:wager_top_contestant
					AND = {
						exists = scope:wager_champion_contestant
						this = scope:wager_champion_contestant
					}
				}
			}
			order_by = var:contest_aptitude
			save_scope_as = wager_flavor_contestant
			if = {
				limit = {
					scope:activity = { has_current_phase = tournament_phase_horse_race }
				}
				tournament_horse_race_fetch_horse_effect = yes
			}
		}
	}
	# Underdog contestant
	if = {
		limit = {
			any_guest_subset_current_phase = {
				name = qualified
				NOR = {
					exists = var:mysterious_stranger_origin
					this = scope:wager_top_contestant
					AND = {
						exists = scope:wager_flavor_contestant
						this = scope:wager_flavor_contestant
					}
					AND = {
						exists = scope:wager_champion_contestant
						this = scope:wager_champion_contestant
					}
					trigger_if = {
						limit = { exists = scope:wager_flavor_contestant }
						this = scope:wager_flavor_contestant
					}
				}
				contest_aptitude_fetch_value <= tournament_wager_diff_value
			}
		}
		random_guest_subset_current_phase = {
			name = qualified
			limit = {
				NOR = {
					exists = var:mysterious_stranger_origin
					this = scope:wager_top_contestant
					AND = {
						exists = scope:wager_flavor_contestant
						this = scope:wager_flavor_contestant
					}
					AND = {
						exists = scope:wager_champion_contestant
						this = scope:wager_champion_contestant
					}
					trigger_if = {
						limit = { exists = scope:wager_flavor_contestant }
						this = scope:wager_flavor_contestant
					}
				}
				contest_aptitude_fetch_value <= tournament_wager_diff_value
			}
			weight = {
				base = 1
				modifier = {
					factor = 0.1
					this = root
				}
			}
			save_scope_as = wager_underdog_contestant
			if = {
				limit = {
					scope:activity = { has_current_phase = tournament_phase_horse_race }
				}
				tournament_horse_race_fetch_horse_effect = yes
			}
		}
	}
	# Second favourite
	else_if = {
		limit = {
			NOT = { exists = scope:wager_underdog_contestant }
			any_guest_subset_current_phase = {
				name = qualified
				NOR = {
					this = scope:wager_top_contestant
					exists = var:mysterious_stranger_origin
					AND = {
						exists = scope:wager_flavor_contestant
						this = scope:wager_flavor_contestant
					}
					AND = {
						exists = scope:wager_champion_contestant
						this = scope:wager_champion_contestant
					}
				}
			}
		}
		ordered_guest_subset_current_phase = {
			name = qualified
			limit = {
				NOR = {
					this = scope:wager_top_contestant
					exists = var:mysterious_stranger_origin
					AND = {
						exists = scope:wager_flavor_contestant
						this = scope:wager_flavor_contestant
					}
				}
			}
			order_by = var:contest_aptitude
			save_scope_as = wager_second_contestant
			if = {
				limit = {
					scope:activity = { has_current_phase = tournament_phase_horse_race }
				}
				tournament_horse_race_fetch_horse_effect = yes
			}
		}
	}
	# Mysterious stranger
	if = {
		limit = {
			any_guest_subset_current_phase = {
				name = qualified
				exists = var:mysterious_stranger_origin
			}
		}
		random_guest_subset_current_phase = {
			name = qualified
			limit = { exists = var:mysterious_stranger_origin }
			save_scope_as = mysterious_stranger
			if = {
				limit = {
					scope:activity = { has_current_phase = tournament_phase_horse_race }
				}
				tournament_horse_race_fetch_horse_effect = yes
			}
		}
	}
}

# Info and payout for won/lost wager
tournament_contest_wager_effect = {
	if = {
		limit = {
			exists = var:tournament_wager_target
			exists = var:tournament_wager_payout
		}
		var:tournament_wager_target = { save_scope_as = wager_target }
		if = {
			limit = { scope:wager_target = scope:contest_winner }
			if = {
				limit = {
					scope:contest_complete ?= flag:horse_race
					exists = scope:wager_target.var:contest_horse_race_name
				}
				custom_tooltip = wager_won_horse_tooltip
			}
			else = { custom_tooltip = wager_won_tooltip }
			add_gold = var:tournament_wager_payout
		}
		else = {
			if = {
				limit = {
					scope:contest_complete ?= flag:horse_race
					exists = scope:wager_target.var:contest_horse_race_name
				}
				custom_tooltip = wager_lost_horse_tooltip
			}
			else = { custom_tooltip = wager_lost_tooltip }
		}
		remove_variable = tournament_wager_stake
		remove_variable = tournament_wager_target
		remove_variable = tournament_wager_payout
	}
}

# Tooltips for current status of placed wager
tournament_contest_wager_info_effect = {
	if = {
		limit = {
			exists = var:tournament_wager_target
			exists = var:tournament_wager_payout
		}
		var:tournament_wager_target = { save_scope_as = wager_target }
		if = {
			limit = {
				scope:activity = { activity_tournament_versus_contest_trigger = yes }
			}
			switch = {
				trigger = var:tournament_wager_target
				scope:contest_loser = { custom_tooltip = wager_lost_tooltip }
				scope:contest_winner = { custom_tooltip = wager_valid_tooltip }
			}
		}
		else = {
			if = {
				limit = {
					var:tournament_wager_target = { exists = var:contest_aptitude }
				}
				if = {
					limit = {
						scope:activity = { has_current_phase = tournament_phase_horse_race }
					}
					custom_tooltip = wager_valid_horse_tooltip
				}
				else = { custom_tooltip = wager_valid_tooltip }
			}
			else = {
				if = {
					limit = {
						scope:activity = { has_current_phase = tournament_phase_horse_race }
					}
					custom_tooltip = wager_lost_horse_tooltip
				}
				else = { custom_tooltip = wager_lost_tooltip }
			}
		}
	}
}

# Place wager on a contestant
tournament_place_wager_effect = {
	$CONTENDER$ = { save_scope_as = wager_target }
	set_variable = {
		name = tournament_wager_stake
		value = tiny_gold_value
	}
	set_variable = {
		name = tournament_wager_payout
		value = tournament_wager_payout_value
	}
	set_variable = {
		name = tournament_wager_target
		value = scope:wager_target
	}
	scope:activity = {
		add_activity_log_entry = {
			key = tournament_placed_wager_log
			tags = { wager }
			score = 25
			character = root
			target = scope:wager_target

			# Effects
			root = { remove_short_term_gold = tiny_gold_value }
		}
	}
	if = {
		limit = {
			scope:activity = { has_current_phase = tournament_phase_horse_race }
		}
		custom_tooltip = tournament_events.1000.horse_winnings_tt
	}
	else = { custom_tooltip = tournament_events.1000.winnings_tt }
}

# Stress impact for betting on someone other than yourself
tournament_bet_on_other_stress_effect = {
	if = {
		limit = { exists = var:contest_aptitude }
		stress_impact = {
			arrogant = minor_stress_impact_gain
		}
	}
	stress_impact = {
		greedy = minor_stress_impact_gain
		profligate = minor_stress_impact_loss
	}
}

# Info on potential effects for breaking islamic gambling laws
tournament_contest_maysir_tooltip_effect = {
	if = {
		limit = {
			religion = religion:islam_religion
			scope:activity = {
				NOR = {
					has_current_phase = tournament_phase_archery
					has_current_phase = tournament_phase_horse_race
				}
			}
		}
		custom_description = { text = ep2_tournament_maysir_tt }
	}
}

# Effects for muslims breaking islamic gambling laws
tournament_contest_maysir_effect = {
	if = {
		limit = {
			religion = religion:islam_religion
			scope:activity = {
				NOR = {
					has_current_phase = tournament_phase_archery
					has_current_phase = tournament_phase_horse_race
				}
			}
		}
		custom_description = { text = ep2_tournament_maysir_tt }
		add_piety = medium_piety_loss
		stress_impact = {
			zealous = medium_stress_impact_gain
		}
	}
}

##############
### PRIZES ###
##############

# Create a prize artifact relevant to the current contest type
tournament_contest_generate_prize_effect = {
	save_scope_value_as = {
		name = contest_type
		value = flag:$CONTEST$
	}
	hidden_effect = { random_dummy_gender_effect = yes }
	scope:activity = {
		### Combined wealth and quality determines tier
		# 50-129 Masterwork 
		# 130-179 Famed 
		# 180+ Illustrious
		save_scope_value_as = {
			name = wealth
			value = {
				value = 0
				# Activity prize options
				if = { # Common only
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_bad } }
					add = { 1 24 }
				}
				else_if = { # Common or Masterwork - often Masterwork
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_normal } }
					add = { 15 64 }
				}
				else_if = { # Masterwork or Famed - often Masterwork
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_good } }
					add = { 35 89 }
				}
				else_if = { # Masterwork, Famed, or Illustrious - often Famed
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_very_good } }
					add = { 60 105 }
				}
				else_if = { # Famed or Illustrious - 50/50 split
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_splendid } }
					add = { 69 111 }
				}
			}
		}
		save_scope_value_as = {
			name = quality
			value = {
				value = 0
				# Activity prize options
				if = { # Common only
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_bad } }
					add = { 1 24 }
				}
				else_if = { # Common or Masterwork - often Masterwork
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_normal } }
					add = { 15 64 }
				}
				else_if = { # Masterwork or Famed - often Masterwork
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_good } }
					add = { 35 89 }
				}
				else_if = { # Masterwork, Famed, or Illustrious - often Famed
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_very_good } }
					add = { 60 105 }
				}
				else_if = { # Famed or Illustrious - 50/50 split
					limit = { has_activity_option = { category = tournament_option_prizes option = tournament_prizes_splendid } }
					add = { 69 111 }
				}
			}
		}
	}
	random_list = {
	 	15 = { # Armor
	 		trigger = {
	 			OR = {
	 				scope:contest_type = flag:duel
	 				scope:contest_type = flag:joust
	 				scope:contest_type = flag:melee
	 			}
	 		}
	 		create_artifact_armor_effect = {
				OWNER = scope:host
				CREATOR = scope:dummy_gender
				SET_ARMOR_TYPE = flag:no
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_armor_name }
			}
	 	}
	 	15 = { # Weapon
	 		trigger = {
	 			OR = {
	 				scope:contest_type = flag:duel
	 				scope:contest_type = flag:joust
	 				scope:contest_type = flag:melee
	 			}
	 		}
	 		create_artifact_weapon_effect = {
				OWNER = scope:host
				CREATOR = scope:dummy_gender
				SET_WEAPON_TYPE = flag:no
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_weapon_name }
			}
	 	}
	 	15 = { # Bow
	 		trigger = { scope:contest_type = flag:archery }
		 	create_artifact_bow_effect = {
				OWNER = scope:host
				CREATOR = scope:dummy_gender
				SET_BOW_TYPE = flag:no
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_bow_name }
			}
	 	}
	 	5 = { # Ring
		 	create_artifact_ring_effect = {
				OWNER = scope:host
				SMITH = scope:dummy_gender
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_ring_name }
			}
	 	}
	 	5 = { # Necklace
	 		trigger = {
	 			scope:host.culture = { has_cultural_era_or_later = culture_era_early_medieval }
	 		}
		 	if = {
		 		limit = {
		 			scope:$CONTEST$_winner = { has_royal_court = yes }
		 		}
		 		create_artifact_necklace_pedestal_effect = {
					OWNER = scope:host
					SMITH = scope:dummy_gender
				}
			}
			else = {
				create_artifact_necklace_effect = {
					OWNER = scope:host
					SMITH = scope:dummy_gender
				}
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_necklace_name }
			}
	 	}
	 	5 = { # Brooch
	 		if = {
		 		limit = {
		 			scope:$CONTEST$_winner = { has_royal_court = yes }
		 		}
		 		create_artifact_brooch_pedestal_effect = {
					OWNER = scope:host
					SMITH = scope:dummy_gender
				}
			}
			else = {
				create_artifact_brooch_effect = {
					OWNER = scope:host
					SMITH = scope:dummy_gender
				}
			}
	 		if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_brooch_name }
			}
	 	}
	 	5 = { # Goblet
		 	trigger = {
		 		scope:$CONTEST$_winner = { has_royal_court = yes }
		 		scope:host.culture = { has_cultural_era_or_later = culture_era_early_medieval }
		 	}
			create_artifact_goblet_effect = {
				OWNER = scope:host
				SMITH = scope:dummy_gender
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_goblet_name }
			}
	 	}
	 	5 = { # Bowl
		 	trigger = {
		 		scope:$CONTEST$_winner = { has_royal_court = yes }
		 		scope:host.culture = { has_cultural_era_or_later = culture_era_early_medieval }
		 	}
			create_artifact_bowl_effect = {
				OWNER = scope:host
				SMITH = scope:dummy_gender
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_bowl_name }
			}
	 	}
	 	5 = { # Urn
		 	trigger = {
		 		scope:$CONTEST$_winner = { has_royal_court = yes }
		 		scope:host.culture = { has_cultural_era_or_later = culture_era_early_medieval }
		 	}
			create_artifact_urn_effect = {
				OWNER = scope:host
				POTTER = scope:dummy_gender
			}
			if = {
				limit = { exists = scope:newly_created_artifact }
				scope:newly_created_artifact = { set_artifact_name = tournament_prize_urn_name }
			}
	 	}
	 	5 = { # Statuette
		 	trigger = {
		 		scope:contest_type = flag:joust
		 		scope:$CONTEST$_winner = { has_royal_court = yes }	
		 		scope:host.culture = { has_cultural_era_or_later = culture_era_high_medieval }
		 	}
			hidden_effect_new_object = {
				# Get artifact quality, wealth, materials, and decorations
				get_artifact_quality_effect = yes
				get_artifact_wealth_effect = yes
				create_artifact = {
					name = tournament_prize_statuette_name
					creator = scope:host
					description = placeholder # Will be regenerated in the post-creation effects
					type = pedestal
					visuals = statuette
					modifier = artifact_placeholder_modifier
					wealth = scope:wealth
					quality = scope:quality
					history = {
						type = prize_created
						recipient = scope:host
					}
					save_scope_as = newly_created_artifact
				}
				scope:newly_created_artifact = {
					set_artifact_description = artifact_statuette_description
					# Add artifact modifiers
					add_scaled_artifact_modifier_grandeur_small_effect = yes
					add_scaled_artifact_modifier_minor_prestige_effect = yes
					if = {
						limit = {
							OR = {
								rarity = famed
								rarity = illustrious
							}
						}
						add_scaled_artifact_modifier_majesty_effect = yes
					}
				}
			}
	 	}
	 	5 = { # Wine Tun
		 	trigger = {
		 		scope:$CONTEST$_winner = {
		 			has_royal_court = yes
		 			drinks_alcohol_trigger = yes
		 		}
		 		scope:host = {
		 			drinks_alcohol_trigger = yes
		 			culture = { has_cultural_era_or_later = culture_era_high_medieval }
		 		}
		 	}
			hidden_effect_new_object = {
				# Get artifact quality, wealth, materials, and decorations
				get_artifact_quality_effect = yes
				get_artifact_wealth_effect = yes
				create_artifact = {
					name = tournament_prize_tun_barrel_name
					creator = scope:host
					description = placeholder # Will be regenerated in the post-creation effects
					type = tun_barrel
					visuals = tun_barrel
					modifier = artifact_placeholder_modifier
					wealth = scope:wealth
					quality = scope:quality
					history = {
						type = prize_created
						recipient = scope:host
					}
					save_scope_as = newly_created_artifact
				}
				scope:newly_created_artifact = {
					set_artifact_description = artifact_tun_barrel_description
					# Add artifact modifiers
					add_scaled_artifact_modifier_grandeur_small_effect = yes
					add_scaled_artifact_modifier_minor_prestige_effect = yes
					if = {
						limit = {
							OR = {
								rarity = famed
								rarity = illustrious
							}
						}
						add_scaled_artifact_modifier_rulership_effect = yes
					}
				}
			}
	 	}
	 	5 = { # Scepter
	 		trigger = {
	 			scope:host.culture = { has_cultural_era_or_later = culture_era_high_medieval }
	 		}
	 		if = {
	 			limit = {
	 				scope:$CONTEST$_winner = { has_royal_court = yes }
	 			}
	 			hidden_effect_new_object = {
	 				# Get artifact quality, wealth, materials, and decorations
					get_artifact_quality_effect = yes
					get_artifact_wealth_effect = yes
					create_artifact = {
						name = tournament_prize_scepter_name
						creator = scope:host
						description = placeholder # Will be regenerated in the post-creation effects
						type = scepter_pedestal
						visuals = scepter_pedestal
						modifier = artifact_placeholder_modifier
						wealth = scope:wealth
						quality = scope:quality
						history = {
							type = prize_created
							recipient = scope:host
						}
						save_scope_as = newly_created_artifact
					}
					scope:newly_created_artifact = {
						set_artifact_description = artifact_scepter_description
						# Add artifact modifiers
						add_scaled_artifact_modifier_minor_prestige_effect = yes
						add_scaled_artifact_modifier_grandeur_small_effect = yes
						if = {
							limit = {
								OR = {
									rarity = famed
									rarity = illustrious
								}
							}
							add_scaled_artifact_modifier_rulership_effect = yes
						}
					}
				}
	 		}
	 		else = {
	 			hidden_effect_new_object = {
	 				# Get artifact quality, wealth, materials, and decorations
					get_artifact_quality_effect = yes
					get_artifact_wealth_effect = yes
					create_artifact = {
						name = tournament_prize_scepter_name
						creator = scope:host
						description = placeholder # Will be regenerated in the post-creation effects
						type = miscellaneous
						visuals = regalia
						template = regalia_template
						modifier = artifact_placeholder_modifier
						wealth = scope:wealth
						quality = scope:quality
						history = {
							type = prize_created
							recipient = scope:host
						}
						save_scope_as = newly_created_artifact
					}
					scope:newly_created_artifact = {
						set_artifact_description = artifact_scepter_description
						# Add artifact modifiers
						add_scaled_artifact_modifier_minor_prestige_effect = yes
						add_scaled_artifact_modifier_grandeur_small_effect = yes
						if = {
							limit = {
								OR = {
									rarity = famed
									rarity = illustrious
								}
							}
							add_scaled_artifact_modifier_rulership_effect = yes
						}
					}
				}
	 		}
	 	}
	}
	if = {
		limit = { exists = scope:newly_created_artifact }
		scope:newly_created_artifact = {
			if = {
				limit = {
					rarity = famed
				}
				random = {
					chance = 50
					add_scaled_artifact_modifier_combat_effect = yes
				}
			}
			else_if = {
				limit = {
					rarity = illustrious
				}
				add_scaled_artifact_modifier_combat_effect = yes
			}
		}
		scope:$CONTEST$_winner = { 
			save_scope_as = contest_winner 
			send_interface_toast = {
				type = toast_tournament_contest_reward
				title = tournament_contest_reward_toast_title
				left_icon = scope:newly_created_artifact
			}
		}
		scope:newly_created_artifact = {
			save_scope_as = $CONTEST$_prize
			add_to_list = contest_prizes
			if = {
				limit = { scope:host = scope:contest_winner }
				set_artifact_description = tournament_prize_host_desc_$CONTEST$
			}
			else = { set_artifact_description = tournament_prize_desc_$CONTEST$ }
			set_variable = suppress_artifact_notifications
			scope:activity = {
				add_activity_log_entry = {
					key = tournament_$CONTEST$_prize_log
					character = scope:host
					target = scope:$CONTEST$_winner
					artifact = scope:$CONTEST$_prize
					show_in_conclusion = yes
					
					# EFFECT
					scope:$CONTEST$_prize = {
						set_owner = {
							target = scope:$CONTEST$_winner
							history = {
								type = prize_awarded
								actor = scope:host
								recipient = scope:$CONTEST$_winner
							}
						}
					}
				}
			}
			remove_variable = suppress_artifact_notifications
		}
	}
}

# Create an armor prize
tournament_contest_armor_prize_effect = {
	create_artifact = {
		name = tournament_prize_armor_name
		creator = scope:host
		description = placeholder # Will be regenerated in the post-creation effects
		type = armor_$TYPE$
		visuals = armor
		modifier = artifact_prowess_1_modifier
		wealth = scope:wealth
		quality = scope:quality
		save_scope_as = newly_created_artifact
		history = {
			type = prize_created
			recipient = scope:host
		}
	}
}

# Create a weapon prize
tournament_contest_weapon_prize_effect = {
	create_artifact = {
		name = tournament_prize_weapon_name
		creator = scope:host
		description = placeholder # Will be regenerated in the post-creation effects
		visuals = $VISUALS$
		type = $TYPE$
		modifier = artifact_prowess_1_modifier
		wealth = scope:wealth
		quality = scope:quality
		save_scope_as = newly_created_artifact
		history = {
			type = prize_created
			recipient = scope:host
		}
	}
}

# Create a bow prize
tournament_contest_bow_prize_effect = {
	create_artifact = {
		name = tournament_prize_bow_name
		creator = scope:host
		description = placeholder # Will be regenerated in the post-creation effects
		visuals = $TYPE$
		type = $TYPE$
		modifier = artifact_prowess_1_modifier
		wealth = scope:wealth
		quality = scope:quality
		save_scope_as = newly_created_artifact
		history = {
			type = prize_created
			recipient = scope:host
		}
	}
}

tournament_contest_rewards_give_liege_prestige_effect = {
	if = {
		limit = { tournament_is_knightly_entourage_character_trigger = yes }
		# Now, we distribute the sum.
		hidden_effect = {
			# Save for loc.
			save_scope_as = winner
			liege = {
				# Save for loc.
				save_scope_as = winner_liege
				# Distribute prestige.
				add_prestige = scope:host.$AMOUNT$
			}
		}
		# And remember to tell everyone about it.
		custom_tooltip = $TOOLTIP$
	}
}

tournament_contest_rewards_split_prize_money_with_liege_effect = {
	if = {
		limit = { tournament_is_knightly_entourage_character_trigger = yes }
		# Grab scopes for loc.
		save_scope_as = winner
		liege = { save_scope_as = winner_liege }
		# If they love you, they give you all the cash.
		if = {
			limit = {
				opinion = {
					target = scope:winner_liege
					value >= very_high_positive_opinion
				}
			}
			custom_tooltip = tournament_landless_knight_liege_rewards.tt.tournament_gold.knight_loves_liege
			pay_short_term_gold = {
				target = scope:winner_liege
				gold = $AMOUNT$
			}
		}
		# If they like you, they give you most of it.
		if = {
			limit = {
				opinion = {
					target = scope:winner_liege
					value >= medium_positive_opinion
				}
			}
			custom_tooltip = tournament_landless_knight_liege_rewards.tt.tournament_gold.knight_likes_liege
			pay_short_term_gold = {
				target = scope:winner_liege
				gold = {
					value = $AMOUNT$
					multiply = 0.75
				}
			}
		}
		# If they're neutral to dislike, they split it 50:50.
		if = {
			limit = {
				opinion = {
					target = scope:winner_liege
					value >= high_negative_opinion
				}
			}
			custom_tooltip = tournament_landless_knight_liege_rewards.tt.tournament_gold.knight_accepts_liege
			pay_short_term_gold = {
				target = scope:winner_liege
				gold = {
					value = $AMOUNT$
					multiply = 0.5
				}
			}
		}
		# If they _really_ hate you, they refuse to give anything.
		else = { custom_tooltip = tournament_landless_knight_liege_rewards.tt.tournament_gold.knight_hates_liege }
	}
}

tournament_winner_prestige_rewards_effect = {
	if = {
		limit = { scope:activity.num_phases > 4 }
		add_prestige = scope:host.massive_prestige_value
		if = {
			limit = { exists = dynasty }
			dynasty = { add_dynasty_prestige = scope:host.minor_dynasty_prestige_gain }
		}
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_first_place_reward_prestige_major_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_first_place_reward_prestige_major
		}
	}
	else_if = {
		limit = { scope:activity.num_phases > 3 }
		add_prestige = scope:host.major_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_first_place_reward_prestige_medium_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_first_place_reward_prestige_medium
		}
	}
	else = {
		add_prestige = scope:host.medium_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_first_place_reward_prestige_minor_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_first_place_reward_prestige_minor
		}
	}
}

tournament_winner_gold_rewards_effect = {
	if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_good
				}
			}
		}
		add_gold = scope:host.medium_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = medium_gold_value }
	}
	else_if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_normal
				}
			}
		}
		add_gold = scope:host.minor_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = minor_gold_value }
	}
	else = {
		add_gold = scope:host.tiny_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = tiny_gold_value }
	}
}

tournament_second_prestige_rewards_effect = {
	if = {
		limit = { scope:activity.num_phases > 4 }
		add_prestige = scope:host.major_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_second_place_reward_prestige_major_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_second_place_reward_prestige_major
		}
	}
	else_if = {
		limit = { scope:activity.num_phases > 3 }
		add_prestige = scope:host.medium_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_second_place_reward_prestige_medium_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_second_place_reward_prestige_medium
		}
	}
	else = {
		add_prestige = scope:host.minor_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_second_place_reward_prestige_minor_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_second_place_reward_prestige_minor
		}
	}
}

tournament_second_gold_rewards_effect = {
	if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_good
				}
			}
		}
		add_gold = scope:host.minor_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = minor_gold_value }
	}
	else_if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_normal
				}
			}
		}
		add_gold = scope:host.tiny_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = tiny_gold_value }
	}
}

tournament_third_prestige_rewards_effect = {
	if = {
		limit = { scope:activity.num_phases > 4 }
		add_prestige = scope:host.medium_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_third_place_reward_prestige_major_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_third_place_reward_prestige_major
		}
	}
	else_if = {
		limit = { scope:activity.num_phases > 3 }
		add_prestige = scope:host.minor_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_third_place_reward_prestige_medium_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_third_place_reward_prestige_medium
		}
	}
	else = { add_prestige = scope:host.miniscule_prestige_value }
}

tournament_third_gold_rewards_effect = {
	if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_good
				}
			}
		}
		add_gold = scope:host.tiny_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = tiny_gold_value }
	}
}

tournament_contest_rewards_effect = {
	scope:activity = {
		random_guest_subset = {
			name = winner
			phase = tournament_phase_$CONTEST$
			save_scope_as = contest_winner
			add_to_list = awarded_contestants
			scope:activity = {
				add_activity_log_entry = {
					key = tournament_$CONTEST$_winner_log
					tags = { winner contest }
					score = 80
					show_in_conclusion = yes
					character = scope:contest_winner

					# EFFECT
					scope:contest_winner ?= {
						# Prestige
						tournament_winner_prestige_rewards_effect = yes
						# Gold
						tournament_winner_gold_rewards_effect = yes
						# Trait
						tournament_give_trait_effect = { CONTEST = $CONTEST$ }
						# Modifier
						add_character_modifier = { modifier = tournament_$CONTEST$_winner_modifier years = 5 }
					}
				}
			}
			#Memory
			create_character_memory = {
				type = tournament_won_contest_memory
				
				participants = {
					contestant = this
					host = scope:host
				}
			}
			ordered_memory = {
				memory_type = tournament_won_contest_memory
				order_by = memory_creation_date
				set_variable = {
					name = contest_type
					value = flag:tournament_phase_$CONTEST$
				}
			}
			add_to_list = awarded_contestants
			custom_tooltip = contest_winner_ceremony_tt
		}
		# Second Place
		random_guest_subset = {
			name = second_place
			phase = tournament_phase_$CONTEST$
			# Prestige
			tournament_second_prestige_rewards_effect = yes
			# Gold
			tournament_second_gold_rewards_effect = yes
			# Trait
			tournament_give_trait_effect = { CONTEST = $CONTEST$ }
			add_character_modifier = { modifier = tournament_$CONTEST$_participant_modifier years = 5 }
			add_to_list = awarded_contestants
		}
		# Third place
		random_guest_subset = {
			name = third_place
			phase = tournament_phase_$CONTEST$
			# Prestige
			tournament_third_prestige_rewards_effect = yes
			# Gold
			tournament_third_gold_rewards_effect = yes
			# Trait
			tournament_give_trait_effect = { CONTEST = $CONTEST$ }
			add_character_modifier = { modifier = tournament_$CONTEST$_participant_modifier years = 5 }
			add_to_list = awarded_contestants
		}
		# Other competitors
		every_guest_subset = {
			name = qualified
			phase = tournament_phase_$CONTEST$
			custom = contest_individual_losers_tt
			limit = {
				NOT = { is_in_list = awarded_contestants }
			}
			tournament_give_trait_effect = { CONTEST = $CONTEST$ }
			add_character_modifier = { modifier = tournament_$CONTEST$_participant_modifier years = 5 }
		}
	}
}

tournament_team_winner_prestige_rewards_effect = {
	if = {
		limit = { scope:activity.num_phases > 4 }
		add_prestige = scope:host.major_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_team_winner_reward_prestige_major_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_team_winner_reward_prestige_major
		}
	}
	else_if = {
		limit = { scope:activity.num_phases > 3 }
		add_prestige = scope:host.medium_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_team_winner_reward_prestige_medium_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_team_winner_reward_prestige_medium
		}
	}
	else = {
		add_prestige = scope:host.minor_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_team_winner_reward_prestige_minor_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_team_winner_reward_prestige_minor
		}
	}
}

tournament_team_winner_gold_rewards_effect = {
	if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_good
				}
			}
		}
		add_gold = scope:host.minor_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = minor_gold_value }
	}
	else_if = {
		limit = {
			scope:activity = {
				has_activity_option = {
					category = tournament_option_prizes
					option = tournament_prizes_normal
				}
			}
		}
		add_gold = scope:host.tiny_gold_value
		tournament_contest_rewards_split_prize_money_with_liege_effect = { AMOUNT = tiny_gold_value }
	}
}

tournament_team_loser_prestige_rewards_effect = {
	if = {
		limit = {  scope:activity.num_phases > 4 }
		add_prestige = scope:host.medium_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_team_loser_reward_prestige_major_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_team_loser_reward_prestige_major
		}
	}
	else_if = {
		limit = { scope:activity.num_phases > 3 }
		add_prestige = scope:host.minor_prestige_value
		# If appropriate, give the winner's liege a reward too.
		tournament_contest_rewards_give_liege_prestige_effect = {
			AMOUNT = tournament_team_loser_reward_prestige_medium_value
			TOOLTIP = tournament_landless_knight_liege_rewards.tt.tournament_team_loser_reward_prestige_medium
		}
	}
	else = { add_prestige = scope:host.miniscule_prestige_value }
}

tournament_contest_team_rewards_effect = {
	scope:activity = {
		every_guest_subset = {
			name = winner
			phase = tournament_phase_$CONTEST$
			custom = contest_team_winners_tt
			# Prestige
			tournament_team_winner_prestige_rewards_effect = yes
			# Gold
			tournament_team_winner_gold_rewards_effect = yes
			# Trait
			tournament_give_trait_effect = { CONTEST = $CONTEST$ }
			# Modifier
			add_character_modifier = { modifier = tournament_$CONTEST$_winner_modifier years = 5 }
			# Memory
			create_character_memory = {
				type = tournament_won_contest_memory
				
				participants = {
					contestant = this
					host = scope:host
				}
			}
			ordered_memory = {
				memory_type = tournament_won_contest_memory
				order_by = memory_creation_date
				set_variable = {
					name = contest_type
					value = flag:tournament_phase_$CONTEST$
				}
			}
			add_to_list = contest_winners
		}
		random_guest_subset = {
			name = winner 
			phase = tournament_phase_$CONTEST$
			limit = {
				scope:activity.var:melee_winner ?= this
				is_alive = yes
			}
			alternative_limit = { is_alive = yes } 
			weight = {
				base = 1
				tournament_team_captain_modifier = yes
			}
			save_scope_as = contest_winner
			custom_tooltip = contest_winner_ceremony_tt
		}
		# Losers
		every_guest_subset = {
			name = second_place
			phase = tournament_phase_$CONTEST$
			custom = contest_team_losers_tt
			# Prestige
			tournament_team_loser_prestige_rewards_effect = yes
			# Trait
			tournament_give_trait_effect = { CONTEST = $CONTEST$ }
			# Modifier
			add_character_modifier = { modifier = tournament_$CONTEST$_participant_modifier years = 5 }
			add_to_list = contest_losers
		}	
	}
}

###################
### PAS D'ARMES ###
###################

# Create token trophy taken from non-martial, e.g. scarf

# Token
create_artifact_pas_darmes_token_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = {
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		select_local_animal_effect = { TYPE = any } # Save scope to limit possible animals: any/big/small/dangerous/harmless/prowling
		save_scope_as = owner
	}
	$WINNER$ = { save_scope_as = winner }
	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		if = { #If we have set the location we use it for the materials
			limit = { exists = scope:location }
			scope:location = { add_to_list = artifact_material_sources }
		}
		scope:winner = {
			# Create the artifact
			create_artifact = {
				name = artifact_pas_darmes_token_name
				creator = scope:owner
				description = placeholder
				visuals = small_box
				type = pas_darmes_token
				modifier = artifact_monthly_prestige_add_1_modifier
				wealth = scope:wealth
				quality = scope:quality
				max_durability = 5
				save_scope_as = newly_created_artifact
				history = {
					#type = pas_darmes_yielded
					type = given
					location = scope:location
					actor = scope:owner
					recipient = scope:winner
				}
			}
		}
		scope:newly_created_artifact = {
			set_variable = {
				name = token_owner
				value = scope:owner
			}
			set_artifact_feature_group = decoration_pattern
			set_artifact_feature_group = generic_material_cloth
			set_artifact_description = artifact_pas_darmes_token_description
			switch = {
				trigger = rarity
				illustrious = { add_artifact_modifier = artifact_monthly_prestige_add_4_modifier }
				famed = { add_artifact_modifier = artifact_monthly_prestige_add_3_modifier }
				masterwork = { add_artifact_modifier = artifact_monthly_prestige_add_2_modifier }
				common = { add_artifact_modifier = artifact_monthly_prestige_add_1_modifier }
			}
			add_scaled_artifact_modifier_attractiveness_effect = yes
			set_variable = {
				name = quality
				value = scope:quality
			}
			set_variable = {
				name = wealth
				value = scope:wealth
			}
		}
		scope:owner = { remove_variable = animal_type }
	}
}

# Create spurs trophy taken from victory
create_artifact_pas_darmes_spurs_effect = {
	# Get the character the artifact is being made for.
	$WINNER$ = {
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		save_scope_as = winner
	}
	$LOSER$ = { save_scope_as = owner }
	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		if = { #If we have set the location we use it for the materials
			limit = { exists = scope:location }
			scope:location = { add_to_list = artifact_material_sources }
		}
		scope:winner = {
			# Create the artifact
			create_artifact = {
				name = artifact_pas_darmes_spurs_name
				creator = scope:owner
				description = placeholder
				visuals = small_box
				type = pas_darmes_spurs
				modifier = artifact_monthly_prestige_add_1_modifier
				wealth = scope:wealth
				quality = scope:quality
				max_durability = 30
				save_scope_as = newly_created_artifact
				history = {
					#type = pas_darmes_won
					type = given
					location = scope:location
					actor = scope:owner
					recipient = scope:winner
				}
			}
		}
		scope:newly_created_artifact = {
			set_variable = {
				name = token_owner
				value = scope:owner
			}
			get_artifact_feature_references_effect = yes
			set_artifact_description = artifact_pas_darmes_spurs_description
			switch = {
				trigger = rarity
				illustrious = { add_artifact_modifier = artifact_monthly_prestige_add_4_modifier }
				famed = { add_artifact_modifier = artifact_monthly_prestige_add_3_modifier }
				masterwork = { add_artifact_modifier = artifact_monthly_prestige_add_2_modifier }
				common = { add_artifact_modifier = artifact_monthly_prestige_add_1_modifier }
			}
			add_scaled_artifact_modifier_attractiveness_effect = yes
			add_scaled_artifact_modifier_combat_effect = yes
			set_variable = {
				name = quality
				value = scope:quality
			}
			set_variable = {
				name = wealth
				value = scope:wealth
			}
		}
	}
}

# Create favor given to favorite contestant, e.g. handkerchief
create_artifact_tournament_favor_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = {
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		save_scope_as = owner
	}
	$RECIPIENT$ = { save_scope_as = recipient }
	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		scope:activity.activity_location = { add_to_list = artifact_material_sources }
		scope:recipient = {
			# Create the artifact
			create_artifact = {
				name = artifact_tournament_favor_name
				creator = scope:owner
				description = placeholder
				visuals = small_box
				type = tournament_favor
				modifier = artifact_monthly_prestige_add_1_modifier
				wealth = scope:wealth
				quality = scope:quality
				max_durability = 10
				save_scope_as = newly_created_artifact
				history = {
					type = given
					location = scope:activity.activity_location
					recipient = scope:recipient
				}
			}
		}
		scope:newly_created_artifact = {
			flag_as_trash_artifact = yes
			set_variable = {
				name = favor_giver
				value = scope:owner
			}
			get_artifact_feature_references_effect = yes
			set_artifact_description = artifact_tournament_favor_description
			switch = {
				trigger = rarity
				illustrious = { add_artifact_modifier = artifact_monthly_prestige_add_4_modifier }
				famed = { add_artifact_modifier = artifact_monthly_prestige_add_3_modifier }
				masterwork = { add_artifact_modifier = artifact_monthly_prestige_add_2_modifier }
				common = { add_artifact_modifier = artifact_monthly_prestige_add_1_modifier }
			}
			add_scaled_artifact_modifier_attractiveness_effect = yes
		}
	}
}

##################
### HORSE RACE ###
##################

# Fetch a randomized horse name
tournament_horse_race_fetch_horse_effect = {
	save_temporary_scope_as = horse_owner
	if = {
		limit = {
			OR = {
				NOT = { exists = var:contest_horse_race_name }
				NOT = { exists = var:contest_horse_race_gender }
			}
		}
		hidden_effect = {
			while = {
				limit = {
					OR = {
						NOT = { exists = var:contest_horse_race_name }
						# Stop multiple horses with same name
						scope:activity = {
							any_guest_subset = {
								name = contestant
								exists = var:contest_horse_race_name
								this != scope:horse_owner
								var:contest_horse_race_name = scope:horse_owner.var:contest_horse_race_name
							}
						}
					}
				}
				random_list = {
					## FAITH
					# BUDDHISM
					300 = { # Kanthaka - Siddhartha's
						trigger = { religion = religion:buddhism_religion }
						set_variable = { name = contest_horse_race_name value = flag:kanthaka }
					}
					# GODS/SAINTS
					150 = {
						trigger = {
							NOT = {
								religion = { is_in_family = rf_abrahamic }
							}
						}
						modifier = {
							factor = 2
							faith = { has_doctrine = unreformed_faith_doctrine }
						}
						random_list = {
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_fate_god } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_knowledge_god } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_night_god } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_trickster_god } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_war_god } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_water_god } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:faith_wealth_god } }
						}
					}
					# HINDUISM
					300 = {
						trigger = { religion = religion:hinduism_religion }
						random_list = {
							5 = { # Dadhi-krā - Dawn
								set_variable = { name = contest_horse_race_name value = flag:dadhi_kra }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Devadatta - Vishnu's
								set_variable = { name = contest_horse_race_name value = flag:devadatta }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Dyaus Pita - Sky Father
								set_variable = { name = contest_horse_race_name value = flag:dyauspitar }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Keshi - Kalki's
								set_variable = { name = contest_horse_race_name value = flag:keshi }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Uhchaihshravas - Horse King
								set_variable = { name = contest_horse_race_name value = flag:uchchaihshravas }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# ISLAM
					300 = {
						trigger = { religion = religion:islam_religion }
						random_list = {
							5 = { # Abbayan - Muhammad's mares
								set_variable = { name = contest_horse_race_name value = flag:abbayan }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # al-Murtajiz - Muhammad's stallions
								set_variable = { name = contest_horse_race_name value = flag:al_murtajiz }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # al-Sakb - Muhammad's stallions
								set_variable = { name = contest_horse_race_name value = flag:al_sakb }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # al-Ya'sub - Muhammad's stallions
								set_variable = { name = contest_horse_race_name value = flag:al_yasub }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:buraq } } # Buraq - Islam
							5 = { # Habdan - Muhammad's mares
								set_variable = { name = contest_horse_race_name value = flag:hadban }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Haizum - Gabriel's
								set_variable = { name = contest_horse_race_name value = flag:haizum }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Hamdani - Muhammad's mares
								set_variable = { name = contest_horse_race_name value = flag:hamdani }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Kuhaylan - Muhammad's mares
								set_variable = { name = contest_horse_race_name value = flag:kuhaylan }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Lihaf - Muhammad's stallions
								set_variable = { name = contest_horse_race_name value = flag:lihaf }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Lizaz - Muhammad's stallions
								set_variable = { name = contest_horse_race_name value = flag:lizaz }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Seqlawi - Muhammad's mares
								set_variable = { name = contest_horse_race_name value = flag:seqlawi }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Zuljanah - Husayn ibn Ali's
								set_variable = { name = contest_horse_race_name value = flag:zuljanah }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# NORSE
					300 = {
						trigger = { religion = religion:germanic_religion }
						random_list = {
							5 = { # Alsviðr - Sól's
								set_variable = { name = contest_horse_race_name value = flag:alsvidr }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Árvakr - Sól's
								set_variable = { name = contest_horse_race_name value = flag:arvakr } 
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Falhófnir - Yggdrasil
								set_variable = { name = contest_horse_race_name value = flag:falhofnir }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Glad - Yggdrasil
								set_variable = { name = contest_horse_race_name value = flag:glad }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Glær - Yggdrasil
								set_variable = { name = contest_horse_race_name value = flag:glaer }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Gulltoppr - Heimdallr's
								set_variable = { name = contest_horse_race_name value = flag:gulltoppr }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Hófvarpnir - Gná's
								set_variable = { name = contest_horse_race_name value = flag:hofvarpnir }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Hrímfaxi - Nótt's
								set_variable = { name = contest_horse_race_name value = flag:hrimfaxi }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # # Skinfaxi - Dagur's
								set_variable = { name = contest_horse_race_name value = flag:skinfaxi }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Sleipnir - Odin's
								set_variable = { name = contest_horse_race_name value = flag:sleipnir }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Svaðilfari - Sleipnir father
								set_variable = { name = contest_horse_race_name value = flag:svadilfari }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					## CULTURE
					# ARABIC
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_arabic }
							}
						}
						random_list = {
							5 = { # al-Dinari - Arabian horse father
								set_variable = { name = contest_horse_race_name value = flag:al_dinari }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # al-Hujays - Arabian horse father
								set_variable = { name = contest_horse_race_name value = flag:al_hujays }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # al-Ward - Arabian horse father
								set_variable = { name = contest_horse_race_name value = flag:al_ward }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # A'waj - Arabian horse father
								set_variable = { name = contest_horse_race_name value = flag:awaj }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}					
							5 = { # Dhi al-'Uqqal - Arabian horse father
								set_variable = { name = contest_horse_race_name value = flag:dhi_al_uqqal }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Sabal - Arabian horse mother
								set_variable = { name = contest_horse_race_name value = flag:sabal }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Zad al-Rakib - Arabian horse father
								set_variable = { name = contest_horse_race_name value = flag:zad_al_rakib }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# BRYTHONIC
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_brythonic }
							}
						}
						random_list = {
							5 = { # Gringolet - Sir Gawain's
								set_variable = { name = contest_horse_race_name value = flag:gringolet }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Hengroen - King Arthur's
								set_variable = { name = contest_horse_race_name value = flag:hengroen }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Llamrei  - King Arthur's
								set_variable = { name = contest_horse_race_name value = flag:llamrei }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Morvarc’h - Breton
								set_variable = { name = contest_horse_race_name value = flag:morvarch }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# CHINESE
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_chinese }
							}
						}
						random_list = {
							5 = { # Red Hare/Chi Tu - Lü Bu's
								set_variable = { name = contest_horse_race_name value = flag:red_hare }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:typhoon } } # Typhoon
						}
					}
					# FRANKISH
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_frankish }
							}
						}
						random_list = {
							5 = { # Baucent - Guillaume d'Orange's
								set_variable = { name = contest_horse_race_name value = flag:baucent }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Bayard - Chansons de gestes
								set_variable = { name = contest_horse_race_name value = flag:bayard }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Sorel - Gerin's
								set_variable = { name = contest_horse_race_name value = flag:sorel }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Tencendur - Charlemagne's
								set_variable = { name = contest_horse_race_name value = flag:tencendur }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Veillantif - Roland's
								set_variable = { name = contest_horse_race_name value = flag:veillantif }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
						}
					}
					# GRECO-ROMAN
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = {
									OR = {
										has_cultural_pillar = heritage_byzantine
										has_cultural_pillar = heritage_latin
									}
								}
							}
						}
						random_list = {
							5 = { # Arion - Iliad
								set_variable = { name = contest_horse_race_name value = flag:arion }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Asturcus - Caesar's
								set_variable = { name = contest_horse_race_name value = flag:asturcus }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Balius - Achilles'
								set_variable = { name = contest_horse_race_name value = flag:balius }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Boreas - North Wind
								set_variable = { name = contest_horse_race_name value = flag:boreas }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Bucephalus - Megas Alexandros'
								set_variable = { name = contest_horse_race_name value = flag:bucephalus }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Caesar - Emperor
								set_variable = { name = contest_horse_race_name value = flag:caesar }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Chiron - Centaur
								set_variable = { name = contest_horse_race_name value = flag:chiron }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Epona - goddess
								set_variable = { name = contest_horse_race_name value = flag:epona }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Equuleus - Hippe
								set_variable = { name = contest_horse_race_name value = flag:equuleus }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Hippocampus - Poseidon's
								set_variable = { name = contest_horse_race_name value = flag:hippocampus }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:incitatus } } # Incitatus - Caligula's
							5 = { set_variable = { name = contest_horse_race_name value = flag:notus } } # Notus - South Wind
							5 = { # Pedasos - Achilles'
								set_variable = { name = contest_horse_race_name value = flag:pedasos }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Pegasus - Bellerophon's
								set_variable = { name = contest_horse_race_name value = flag:pegasus }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Rhaebus - Mezentius'
								set_variable = { name = contest_horse_race_name value = flag:rhaebus }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:seianian } } # Seianian - Gnaeus Seius'
							5 = { set_variable = { name = contest_horse_race_name value = flag:sterope } } # Sterope - Helios'
							5 = { # Xanthos - Achilles'
								set_variable = { name = contest_horse_race_name value = flag:xanthos }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:zephryus } } # Zephyrus - West Wind
						}
					}
					# GOIDELIC
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_goidelic }
							}
						}
						random_list = {
							5 = { # Dub Sainglend - Cúchulainn's
								set_variable = { name = contest_horse_race_name value = flag:dub_sainglend }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Embarr - Lugh Lamh-fada's
								set_variable = { name = contest_horse_race_name value = flag:enbarr }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Kelpie - mythological
								set_variable = { name = contest_horse_race_name value = flag:kelpie }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Liath Macha - Cúchulainn's
								set_variable = { name = contest_horse_race_name value = flag:liath_macha }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# IBERIAN
					300 = {
						trigger = {
							culture = { has_cultural_pillar = language_iberian }
						}
						random_list = {
							5 = { # Altivo - El Dorado
								set_variable = { name = contest_horse_race_name value = flag:altivo }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Babieca - El Cid's
								set_variable = { name = contest_horse_race_name value = flag:babieca }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Rocinante - Don Quixote's
								set_variable = { name = contest_horse_race_name value = flag:rocinante }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:tornado }
							}
						}
					}
					# INDO-ARYAN
					300 = {
						trigger = {
							culture = { has_cultural_pillar = heritage_indo_aryan }
						}
						random_list = {
							5 = { # Pratap's
								set_variable = { name = contest_horse_race_name value = flag:atak }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Pratap's
								set_variable = { name = contest_horse_race_name value = flag:chetak }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Ranjit's
								set_variable = { name = contest_horse_race_name value = flag:laili }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Pratap's
								set_variable = { name = contest_horse_race_name value = flag:natak }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}			
						}
					}
					# MONGOLIC
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_mongolic }
							}
						}
						random_list = {
							5 = { set_variable = { name = contest_horse_race_name value = flag:aranjagaan } } # Aranjagaan - Jangar's
							5 = { set_variable = { name = contest_horse_race_name value = flag:oyomaa } } # Oyomaa - Jangar's
							5 = { set_variable = { name = contest_horse_race_name value = flag:tolin_khul } } # Tolin Khul - folk song
						}
					}
					# PERSIAN
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_iranian }
							}
						}
						random_list = {
							5 = { # Rahbar (Leader) - Akbar's
								set_variable = { name = contest_horse_race_name value = flag:rahbar }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Rakhsh - Rostam's
								set_variable = { name = contest_horse_race_name value = flag:rakhsh }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# SCANDINAVIAN
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_north_germanic }
							}
						}
						random_list = {							
							5 = {
								# Grani - Sigurd's
								set_variable = { name = contest_horse_race_name value = flag:grani }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					# SLAVIC
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_east_slavic }
							}
						}
						random_list = {
							5 = { # Sivko-Burko - Ivan the Fool's
								set_variable = { name = contest_horse_race_name value = flag:sivko_burko }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:sivko }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:burko }
							}
						}
					}
					# TURKIC
					300 = {
						trigger = {
							culture = {
								any_parent_culture_or_above = { has_cultural_pillar = heritage_turkic }
							}
						}
						random_list = {
							5 = { # G'irot - Koroghlu's
								set_variable = { name = contest_horse_race_name value = flag:girot }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Tulpar - mythological
								set_variable = { name = contest_horse_race_name value = flag:tulpar }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
						}
					}
					## COLOR/NOUN
					300 = {
						modifier = { # Mongol's prefer color/marking names
							factor = 2
							culture = { has_cultural_pillar = heritage_mongolic }
						}
						random_list = {
							# COLOR
							5 = { set_variable = { name = contest_horse_race_name value = flag:bay } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:black } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:blue } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:brindle } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:buckskin } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:chestnut } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:cloud } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:cream } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:crimson } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:dapple } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:dawn } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:dun } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:dusk } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:flaxen } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:flame } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:frost } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:golden } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:gray } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:leopard } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:milk } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:night } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:pearl } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:red } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:roan } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:rose } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:ruby } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:scarlet } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:shade } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:shadow } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:shining } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:silk } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:silver } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:smoke } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:snow } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:sorrel } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:sun } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:tawny } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:yellow } }
						}
						random = {
							chance = 50
							random_list = {
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:blaze } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:coat } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:dash } }	
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:face } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:feet } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:foot } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hair } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hide } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hoof } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hooves } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:mane } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:mottle } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:socks } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:speckles } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:spots } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:star } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:steed } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:stockings } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:streak } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:stripe } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:tail } }
							}
						}
					}
					# ADJECTIVE/NOUN
					300 = {
						# ADJECTIVE
						random_list = {
							5 = { set_variable = { name = contest_horse_race_name value = flag:brave } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:brisk } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:fleet } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:long } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:speed } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:storm } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:swift } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:thunder } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:wave } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:wind } }
						}
						random = {
							chance = 50
							random_list = {
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:bolt } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:bound } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:dart } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:dash } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:feet } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:foot } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hocks } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hoof } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:hooves } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:rush } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:sprint } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:steed } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:step } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:stride } }
								5 = { set_variable = { name = contest_horse_race_name_2 value = flag:streak } }
							}
						}
					}
					## NOUN/LONG ADJECTIVE
					300 = {
						random_list = {
							25 = {
								trigger = {
									faith = { has_doctrine = tenet_monasticism }
								}
								set_variable = { name = contest_horse_race_name value = flag:abbess }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = {
								trigger = {
									faith = { has_doctrine = tenet_monasticism }
								}
								set_variable = { name = contest_horse_race_name value = flag:abbot }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:adamant } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:agile } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:charger } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:chaser } }
							25 = {
								modifier = {
									add = 75
									government_has_flag = government_is_tribal
								}
								set_variable = { name = contest_horse_race_name value = flag:chieftain }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							25 = {
								modifier = {
									add = 75
									government_has_flag = government_is_tribal
								}
								set_variable = { name = contest_horse_race_name value = flag:chieftess }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:cyclone } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:deluge } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:downpour } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:endurance } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:faithful } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:flash } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:forthright } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:fury } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:gale } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:hare } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:haste } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:intrepid } }
							25 = {
								trigger = {
									NOT = { government_has_flag = government_is_tribal }
								}
								set_variable = { name = contest_horse_race_name value = flag:lady }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:leader } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:lightning } }
							25 = {
								trigger = {
									NOT = { government_has_flag = government_is_tribal }
								}
								set_variable = { name = contest_horse_race_name value = flag:lord }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}							
							25 = { set_variable = { name = contest_horse_race_name value = flag:lucky } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:majesty } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:mule } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:plucky } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:prancer } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:pride } }
							25 = {
								trigger = {
									NOT = { government_has_flag = government_is_tribal }
								}
								set_variable = { name = contest_horse_race_name value = flag:prince }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							25 = {
								trigger = {
									NOT = { government_has_flag = government_is_tribal }
								}
								set_variable = { name = contest_horse_race_name value = flag:princess }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:rapid } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:resolute } }
							25 = {
								trigger = {
									NOT = { government_has_flag = government_is_tribal }
								}
								set_variable = { name = contest_horse_race_name value = flag:seneschal }
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:snowflake } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:spirit } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:steadfast } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:sunlight } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:tenacity } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:tempest } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:thunderclap } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:unwavering } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:whirlwind } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:winner } }
							# GENDER
							25 = {
								trigger = {
									NOT = { government_has_flag = government_is_tribal }
								}
								random_list = {
									5 = {
										trigger = {
											OR = {
												religion = religion:germanic_religion
												culture = { has_cultural_pillar = heritage_north_germanic }
											}
											faith = { has_doctrine = tenet_warmonger }
										}
										set_variable = { name = contest_horse_race_name value = flag:berserker }
									}
									5 = {
										trigger = {
											faith = { has_doctrine_parameter = great_holy_wars_active }
										}
										set_variable = { name = contest_horse_race_name value = flag:crusader }
									}
									5 = {
										set_variable = { name = contest_horse_race_name value = flag:commander }
									}
									5 = {
										set_variable = { name = contest_horse_race_name value = flag:marshal }
									}
									5 = {
										set_variable = { name = contest_horse_race_name value = flag:varangian }
									}
								}
								random_list = {
									5 = {
										trigger = {
											culture = {
												OR = {
													has_cultural_pillar = martial_custom_female_only
													has_cultural_pillar = martial_custom_equal
												}
											}
										}
										set_variable = { name = contest_horse_race_gender value = flag:female }
									}
									5 = {
										trigger = {
											culture = {
												OR = {
													has_cultural_pillar = martial_custom_male_only
													has_cultural_pillar = martial_custom_equal
												}
											}
										}
										set_variable = { name = contest_horse_race_gender value = flag:male }
									}
								}
							}
						}
					}
					## FETCH
					300 = {
						random_list = {
							100 = {
								trigger = { is_playable_character = yes }
								random_list = {
									25 = { set_variable = { name = contest_horse_race_name value = flag:capital_name } }
									25 = { set_variable = { name = contest_horse_race_name value = flag:capital_empire } }
									25 = { set_variable = { name = contest_horse_race_name value = flag:capital_pride } }
									25 = { set_variable = { name = contest_horse_race_name value = flag:capital_county } }
								}
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:culture_knight } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:culture_lightning } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:culture_thunder } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:faith_adherent } }
							25 = { set_variable = { name = contest_horse_race_name value = flag:faith_afterlife } }
							25 = {
								trigger = {
									faith = {
										OR = {
											has_doctrine = doctrine_clerical_gender_female_only
											has_doctrine = doctrine_clerical_gender_either
										}
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:faith_bishop_female }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = {
								trigger = {
									faith = {
										OR = {
											has_doctrine = doctrine_clerical_gender_male_only
											has_doctrine = doctrine_clerical_gender_either
										}
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:faith_bishop_male }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							25 = {
								trigger = {
									faith = { has_doctrine_parameter = great_holy_wars_active }
								}
								set_variable = { name = contest_horse_race_name value = flag:faith_crusade }
							}
							25 = {
								set_variable = { name = contest_horse_race_name value = flag:faith_devotee_female }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = {
								set_variable = { name = contest_horse_race_name value = flag:faith_devotee_male }
								
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:faith_divine_realm } }
							10 = {
								trigger = {
									faith = {
										NOT = { has_doctrine = doctrine_no_head }
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:faith_pope }
								if = {
									limit = {
										faith = { has_doctrine = doctrine_clerical_gender_male_only }
									}
									set_variable = { name = contest_horse_race_gender value = flag:male }
								}
								else_if = {
									limit = {
										faith = { has_doctrine = doctrine_clerical_gender_female_only }
									}
									set_variable = { name = contest_horse_race_gender value = flag:female }
								}
							}
							25 = {
								set_variable = { name = contest_horse_race_name value = flag:faith_priest_female }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							25 = {
								set_variable = { name = contest_horse_race_name value = flag:faith_priest_male }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							25 = { set_variable = { name = contest_horse_race_name value = flag:faith_symbol } }
							25 = {
								trigger = {
									this != root.top_liege
								}
								set_variable = { name = contest_horse_race_name value = flag:liege_title }
							}
							25 = {
								trigger = {
									this != root.top_liege
								}
								set_variable = { name = contest_horse_race_name value = flag:liege_title_name }
							}
							25 = {
								trigger = { is_playable_character = yes }
								set_variable = { name = contest_horse_race_name value = flag:owner_title }
							}
							25 = {
								trigger = { is_playable_character = yes }
								set_variable = { name = contest_horse_race_name value = flag:title_name }
							}
							## TRAITS							
							25 = {
								trigger = {
									NOT = {
										faith = { has_doctrine_parameter = forbidden_from_pilgrimage }
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:pilgrim }
							}
						}
					}
					## EASTER EGGS
					50 = {
						random_list = {
							5 = { # Arkle
								trigger = { title:c_sutherland.holder.top_liege = root.top_liege }								
								set_variable = { name = contest_horse_race_name value = flag:arkle }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								trigger = {
									culture = { has_cultural_pillar = language_french }	
								}
								modifier = {
									factor = 2
									has_trait = drunkard
								}
								modifier = {
									factor = 2
									has_trait = arrogant
								}
								modifier = {
									factor = 2
									has_trait = depressed
								}
								set_variable = { name = contest_horse_race_name value = flag:beaujacques }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							1 = { set_variable = { name = contest_horse_race_name value = flag:big_dog } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:big_honse } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:binky } }
							5 = { # Black Beauty
								set_variable = { name = contest_horse_race_name value = flag:black_beauty }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:boxer }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:brego }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Secretariat
								set_variable = { name = contest_horse_race_name value = flag:chancery }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:clip_clop } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:cloppy } }
							5 = { # Wellington's
								trigger = {
									culture = { has_cultural_pillar = heritage_west_germanic }
								}
								set_variable = { name = contest_horse_race_name value = flag:copenhagen }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:donkey }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Desert Orchid
								set_variable = { name = contest_horse_race_name value = flag:desert_orchid }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:dustkicker }
							}
							5 = { # Enable
								set_variable = { name = contest_horse_race_name value = flag:facilitate }
								set_variable = { name = contest_horse_race_gender value = flag:female }
							}
							5 = { # Frankel
								trigger = {
									culture = { has_cultural_pillar = language_high_german }
								}
								set_variable = { name = contest_horse_race_name value = flag:frenkel }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Man o' War
								set_variable = { name = contest_horse_race_name value = flag:galley }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:glitterhoof } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:gymer } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:hoarse } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:honse } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:horsey } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:jerry } }
							5 = {
								trigger = { religion = religion:christianity_religion }
								set_variable = { name = contest_horse_race_name value = flag:joseph }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								trigger = {
									culture = {
										OR = {
											has_cultural_pillar = heritage_chinese
											has_cultural_pillar = heritage_mongolic
										}
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:khan }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								trigger = {
									culture = { has_cultural_pillar = heritage_north_germanic }
								}
								set_variable = { name = contest_horse_race_name value = flag:kjekk }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							1 = { set_variable = { name = contest_horse_race_name value = flag:lord_pepper_iii } }
							5 = { # Napoleon's
								trigger = {
									culture = {
										OR = {
											has_cultural_pillar = heritage_frankish
											has_cultural_pillar = heritage_latin
										}
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:marengo }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								set_variable = { name = contest_horse_race_name value = flag:marshal_neigh }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = {
								trigger = {
									culture = {
										OR = {
											has_cultural_pillar = heritage_byzantine
											has_cultural_pillar = heritage_latin
										}
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:maximus }
							}
							5 = { # Seabiscuit
								set_variable = { name = contest_horse_race_name value = flag:oceancracker }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:pebbles } }
							5 = {
								trigger = {
									culture = {
										any_parent_culture_or_above = { has_cultural_pillar = language_anglic }
										any_parent_culture_or_above = { has_cultural_pillar = language_brythonic }
									}
								}								
								set_variable = { name = contest_horse_race_name value = flag:pringle }
							}
							5 = { # Galileo
								trigger = {
									culture = {
										OR = {
											has_cultural_pillar = heritage_arabic
											has_cultural_pillar = heritage_byzantine
											has_cultural_pillar = heritage_latin
										}
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:ptolemy }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { # Red Rum
								trigger = {
									NOT = {
										faith = { trait_is_sin = drunkard }
									}
								}
								set_variable = { name = contest_horse_race_name value = flag:red_wine }
								set_variable = { name = contest_horse_race_gender value = flag:male }
							}
							5 = { set_variable = { name = contest_horse_race_name value = flag:roach } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:sir_whinny } }
							1 = { set_variable = { name = contest_horse_race_name value = flag:sir_horsington } }
							5 = { set_variable = { name = contest_horse_race_name value = flag:torrent } }
						}
					}
				}
				# GENDER
				if = {
					limit = {
						NOT = { exists = var:contest_horse_race_gender }
					}
					random_list = {
						5 = { set_variable = { name = contest_horse_race_gender value = flag:female } }
						5 = { set_variable = { name = contest_horse_race_gender value = flag:male } }
					}
				}
			}
		}
	}
}

############################
### RECURRING CHARACTERS ###
############################

try_to_create_recurring_character_effect = {
	hidden_effect = {
		if = {
			limit = {
				# We want exactly one player.
				calc_true_if = {
					amount = 1
					is_ai = no
					$CHARACTER$ = { is_ai = no }
				}
				# With only a few recurring characters per tourney.
				## Currently, set this to 1 to try and make them more generally memorable.
				### If we start messing with event weight multipliers based on this, then we might want to increase it, so split things up a bit more.
				num_of_relation_activity_recurrer < 1
				NOT = { has_relation_activity_recurrer = $CHARACTER$ }
			}
			set_relation_activity_recurrer = $CHARACTER$
			# Debug stuff.
			## Disabled; decomment it if you want to see who gets grabbed.
			#if = {
				#limit = { is_ai = no }
				#add_to_variable_list = {
					#name = recurring_characters
					#target = $CHARACTER$
				#}
			#}
			#else = {
				#save_temporary_scope_as = ai_temp
				#$CHARACTER$ = {
					#add_to_variable_list = {
						#name = recurring_characters
						#target = scope:ai_temp
					#}
				#}
			#}
		}
	}
}

clean_up_recurring_characters_effect = {
	hidden_effect = {
		save_temporary_scope_as = player_temp
		every_relation = {
			type = activity_recurrer
			remove_relation_activity_recurrer = scope:player_temp
		}
	}
}

# Award XP in relevant tourney trait tracks
disburse_xp_and_liege_prestige_effect = {
	# XP
	if = {
		limit = { has_trait = tourney_participant }
		# Bow
		if = {
			limit = { exists = scope:archery_winner }
			add_trait_xp = {
				trait = tourney_participant
				track = bow
				value = {
					value = 0
					if = {
						limit = { scope:archery_winner ?= this }
						add = tournament_hastiludes_xp_gain_massive_value
					}
					else = { add = tournament_hastiludes_xp_gain_medium_value }
				}
			}
		}
		# Horse
		if = {
			limit = {
				OR = {
					exists = scope:melee_winner
					exists = scope:joust_winner
					exists = scope:horse_race_winner
				}
			}
			add_trait_xp = {
				trait = tourney_participant
				track = horse
				value = {
					value = 0
					if = { # Melee
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_melee this = root }
							}
						}
						if = {
							limit = {
								scope:activity = {
									any_guest_subset = { name = winner phase = tournament_phase_melee this = root }
								}
							}
							add = tournament_hastiludes_xp_gain_medium_value
						}
						else = { add = tournament_hastiludes_xp_gain_minor_value }
					}
					if = { # Joust
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_joust this = root 
								}
							}	
						}
						if = {
							limit = { scope:joust_winner ?= this }
							add = tournament_hastiludes_xp_gain_massive_value
						}
						else = { add = tournament_hastiludes_xp_gain_medium_value }
					}
					if = { # Horse Race
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_horse_race this = root }
							}	
						}
						if = {
							limit = { scope:horse_race_winner ?= this }
							add = tournament_hastiludes_xp_gain_massive_value
						}
						else = { add = tournament_hastiludes_xp_gain_medium_value }
					}
				}
			}
		}
		# Foot
		if = {
			limit = {
				OR = {
					exists = scope:melee_winner
					exists = scope:duel_winner
					exists = scope:wrestling_winner
				}
			}
			add_trait_xp = {
				trait = tourney_participant
				track = foot
				value = {
					value = 0
					if = { # Melee
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_melee this = root }
							}
						}
						if = {
							limit = {
								scope:activity = {
									any_guest_subset = { name = winner phase = tournament_phase_melee this = root }
								}
							}
							add = tournament_hastiludes_xp_gain_medium_value
						}
						else = { add = tournament_hastiludes_xp_gain_minor_value }
					}
					if = { # Duel
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_duel this = root }
							}
						}
						if = {
							limit = { scope:duel_winner ?= this }
							add = tournament_hastiludes_xp_gain_massive_value
						}
						else = { add = tournament_hastiludes_xp_gain_medium_value }
					}
					if = { # Wrestling
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_wrestling this = root }
							}
						}
						if = {
							limit = { scope:wrestling_winner ?= this }
							add = tournament_hastiludes_xp_gain_massive_value
						}
						else = { add = tournament_hastiludes_xp_gain_medium_value }
					}
				}
			}
		}
		# Wit
		if = {
			limit = {
				OR = {
					exists = scope:board_game_winner
					exists = scope:recital_winner
				}
			}
			add_trait_xp = {
				trait = tourney_participant
				track = wit
				value = {
					value = 0
					if = { # Board Game
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_board_game this = root }
							}
						}
						if = {
							limit = { scope:board_game_winner ?= this }
							add = tournament_hastiludes_xp_gain_massive_value
						}
						else = { add = tournament_hastiludes_xp_gain_medium_value }
					}
					if = { # Recital
						limit = {
							scope:activity = {
								any_guest_subset = { name = qualified phase = tournament_phase_recital this = root }
							}
						}
						if = {
							limit = { scope:recital_winner ?= this }
							add = tournament_hastiludes_xp_gain_massive_value
						}
						else = { add = tournament_hastiludes_xp_gain_medium_value }
					}
				}
			}
		}
		# Poets
		if = {
			limit = {
				exists = scope:recital_winner
			}
			if = {
				limit = {
					has_trait = lifestyle_poet
				}
				add_trait_xp = {
					trait = lifestyle_poet
					value = {
						value = 0
						if = {
							limit = {
								scope:activity = {
									any_guest_subset = { name = qualified phase = tournament_phase_recital this = root }
								}
							}
							if = {
								limit = { scope:recital_winner ?= this }
								add = tournament_hastiludes_xp_gain_massive_value
							}
							else = { add = tournament_hastiludes_xp_gain_medium_value }
						}
					}
				}
			}
		}
	}
	# Liege Prestige
	if = {
		limit = {
			OR = {
				AND = {
					exists = scope:archery_winner
					OR = {
						root.court_position:champion_court_position ?= scope:archery_winner
						scope:archery_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:recital_winner
					OR = {
						root.court_position:champion_court_position ?= scope:recital_winner
						scope:recital_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:joust_winner
					OR = {
						root.court_position:champion_court_position ?= scope:joust_winner
						scope:joust_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:wrestling_winner
					OR = {
						root.court_position:champion_court_position ?= scope:wrestling_winner
						scope:wrestling_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:duel_winner
					OR = {
						root.court_position:champion_court_position ?= scope:duel_winner
						scope:duel_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:board_game_winner
					OR = {
						root.court_position:champion_court_position ?= scope:board_game_winner
						scope:board_game_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:horse_race_winner
					OR = {
						root.court_position:champion_court_position ?= scope:horse_race_winner
						scope:horse_race_winner = { is_knight_of = root }
					}
				}
				AND = {
					exists = scope:melee_winner
					OR = {
						root.court_position:champion_court_position ?= scope:melee_winner
						scope:melee_winner = { is_knight_of = root }
					}
				}
			}
		}
		add_prestige = {
			value = 0
			if = {
				limit = { exists = scope:archery_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:archery_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:archery_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}
			else_if = {
				limit = { exists = scope:recital_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:recital_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:recital_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}	
			else_if = {
				limit = { exists = scope:joust_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:joust_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:joust_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}
			else_if = {
				limit = { exists = scope:wrestling_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:wrestling_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:wrestling_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}
			else_if = {
				limit = { exists = scope:duel_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:duel_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:duel_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}
			else_if = {
				limit = { exists = scope:board_game_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:board_game_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:board_game_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}
			else_if = {
				limit = { exists = scope:horse_race_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:horse_race_winner }
					add = medium_prestige_value
				}
				else_if = {
					limit = { scope:horse_race_winner = { is_knight_of = root } }
					add = minor_prestige_value
				}
			}
			else_if = {
				limit = { exists = scope:melee_winner }
				if = {
					limit = { root.court_position:champion_court_position ?= scope:melee_winner }
					add = minor_prestige_value
				}
				else_if = {
					limit = { scope:melee_winner = { is_knight_of = root } }
					add = miniscule_prestige_value
				}
			}
		}
	}
}

###########################
### PROGRESS TO VICTORY ###
###########################

# Update PtV and aptitude, used on passive pulse to keep values up to date before contest begins
set_passive_contest_aptitude_and_progress_effect = {
	if = {
		limit = {
			OR = {
				AND = {
					activity_is_valid_tournament_contestant = yes
					activity_tournament_race_contest_trigger = no
				}
				AND = {
					is_playable_character = yes
					activity_tournament_race_contest_trigger = yes
				}
			}
		}
		if = { # Foot
			limit = {	
				scope:activity = {
					OR = {
						has_current_phase = tournament_phase_melee
						has_current_phase = tournament_phase_duel
					}
				}
			}
			set_variable = { name = contest_aptitude value = contest_foot_aptitude_value }
		}
		else_if = { # Horse
			limit = {
				scope:activity = { has_current_phase = tournament_phase_joust }
			}
			set_variable = { name = contest_aptitude value = contest_horse_aptitude_value }
		}
		else_if = { # Pugilism
			limit = {
				scope:activity = { has_current_phase = tournament_phase_wrestling }
			}
			set_variable = { name = contest_aptitude value = contest_pugilism_aptitude_value }
		}
		else_if = { # Archery
			limit = {
				scope:activity = { has_current_phase = tournament_phase_archery }
			}
			set_variable = { name = contest_aptitude value = contest_archery_aptitude_value }
		}
		else_if = { # Recital
			limit = {
				scope:activity = { has_current_phase = tournament_phase_recital }
			}
			set_variable = { name = contest_aptitude value = contest_recital_aptitude_value }
		}
		else_if = { # Board Game
			limit = {
				scope:activity = { has_current_phase = tournament_phase_board_game }
			}
			set_variable = { name = contest_aptitude value = contest_board_game_aptitude_value }
		}
		else_if = { # Horse Race
			limit = {
				scope:activity = { has_current_phase = tournament_phase_horse_race }
			}
			set_variable = { name = contest_aptitude value = contest_horse_race_aptitude_value }
		}
		
		#Have you taken the Prowess Training decision?
		if = {
			limit = {
				has_character_modifier = tournament_training_decision_modifier
				activity_tournament_prowess_contest_trigger = yes			
			}
			set_variable = {
				name = progress_to_victory
				value = {
					value = progress_to_victory_passive_contestant_value
					add = 20
				}
			}
		}
		set_variable = {
			name = progress_to_victory
			value = progress_to_victory_passive_contestant_value
		}
	}
	else = { remove_variable = progress_to_victory }
}

##############
### SHARED ###
##############

# Save champion, if they exist
tournament_contest_champion_scope_effect = {
	if = {
		limit = { employs_court_position = champion_court_position }
		random_court_position_holder = {
			limit = { has_court_position = champion_court_position }
			save_scope_as = contest_champion
		}
	}
}

# Randomly wound attendees effect
contest_wound_attendees_effect = {
	custom_tooltip = {
		text = contest_wound_attendees_effect
		while = {
			count = 12
			random_guest_subset = {
				name = contestant
				limit = {
					NOT = { is_in_list = wounded_attendees }
					OR = {
						this = root
						is_ai = yes
					}
				}
				add_to_list = wounded_attendees
				increase_wounds_no_death_effect = { REASON = fight }
			}
		}
	}
}

# Randomly wound attendees unrelated to root effect
contest_wound_non_root_attendees_effect = {
	custom_tooltip = {
		text = contest_wound_non_root_attendees_effect
		while = {
			count = 9
			random_guest_subset = {
				name = contestant
				limit = {
					NOR = {
						is_in_list = wounded_attendees
						is_ai = no
						this = root
						is_courtier_of = root
						is_knight_of = root
					}
				}
				add_to_list = wounded_attendees
				increase_wounds_no_death_effect = { REASON = fight }
			}
		}
	}
}

# Randomly wound combatants unrelated to root effect
tournament_wound_non_root_combatants_effect = {
	custom_tooltip = {
		text = tournament_wound_non_root_combatants_effect
		while = {
			count = 4
			random_guest_subset = {
				name = contestant
				limit = {
					is_healthy = yes
					highest_held_title_tier < tier_county
					NOR = {
						is_in_list = wounded_attendees
						is_ai = no
						this = root
						is_courtier_of = root
						is_knight_of = root
						is_close_or_extended_family_of = root
						reverse_opinion = {
							target = root
							value >= 25
						}
					}
				}
				add_to_list = wounded_combatants
				increase_wounds_no_death_effect = { REASON = fight }
			}
		}
	}
}

# Tournament reward tooltips
tournament_display_rewards_effect = {
	show_as_tooltip = {
		switch = {
			trigger = scope:contest_complete
			flag:joust = {
				tournament_contest_rewards_effect = { CONTEST = joust }
			}
			flag:wrestling = {
				tournament_contest_rewards_effect = { CONTEST = wrestling }
			}
			flag:duel = {
				tournament_contest_rewards_effect = { CONTEST = duel }
			}
			flag:board_game = {
				tournament_contest_rewards_effect = { CONTEST = board_game }
			}
			flag:melee = {
				tournament_contest_team_rewards_effect = { CONTEST = melee }
			}
			flag:horse_race = {
				tournament_contest_rewards_effect = { CONTEST = horse_race }
			}
			flag:archery = {
				tournament_contest_rewards_effect = { CONTEST = archery }
			}
			flag:recital = {
				tournament_contest_rewards_effect = { CONTEST = recital }
			}
		}
	}
}

# Notify of arrival of intent target at tournament
activity_intent_target_arrival_notification_effect = {
	if = {
		limit = {
			NOT = {
				capital_province ?= scope:activity.activity_location
			}
			NAND = {
				is_playable_character = no
				exists = liege.capital_province
				liege.capital_province = scope:activity.activity_location
			}
			save_temporary_scope_as = intent_target_temp
			scope:activity = {
				any_attending_character = {
					intent_target ?= scope:intent_target_temp
				}
			}
		}
		save_scope_as = intent_targetee
		scope:activity = {
			every_attending_character = {
				limit = {
					intent_target ?= scope:intent_targetee
				}
				save_scope_as = intent_targeter
				send_interface_message = {
					title = activity_intent_target_arrival_title
					left_icon = scope:intent_targetee
					custom_tooltip = activity_intent_target_arrival_desc
				}
			}
		}
	}
}

# Add contestant as qualified and notify
add_tournament_contest_contestant_effect = {
	save_scope_as = new_list_member
	# Add to subset
	scope:activity = {
		add_to_guest_subset = {
			name = qualified
			target = scope:new_list_member
			phase = tournament_phase_$CONTEST$
		}
	}
	# Update stats
	set_variable = {
		name = contest_aptitude
		value = contest_$SKILL$_aptitude_value
	}
	set_variable = {
		name = progress_to_victory
		value = $PTV$
	}
	# Toast
	if = {
		limit = {
			scope:activity = { has_current_phase = tournament_phase_horse_race }
		}
		send_interface_toast = {
			title = tournament_qualify_title
			left_icon = scope:new_list_member
			custom_tooltip = tournament_qualify_race_desc
			play_sound_effect = "event:/SFX/Events/Themes/sfx_event_theme_type_martial"
		}
	}
	else = {
		send_interface_toast = {
			title = tournament_qualify_title
			left_icon = scope:new_list_member
			custom_tooltip = tournament_qualify_desc
			add_prestige = minor_prestige_gain
			play_sound_effect = "event:/SFX/Events/Themes/sfx_event_theme_type_martial"
		}
		if = {
			limit = {
				OR = {
					this = scope:activity.special_guest:special_guest_champion
					has_court_position = champion_court_position
					is_knight = yes
				}
			}
			liege = {
				add_to_list = qualification_liege_list
				add_to_variable_list = {
					name = qualified_knights
					target = scope:new_list_member
				}
			}
		}
	}
}

# Notify of failure to qualify for a contest
tournament_contest_qualification_failure_effect = {
	if = {
		limit = { exists = var:contest_aptitude }
		remove_variable = contest_aptitude
	}
	if = {
		limit = { exists = var:progress_to_victory }
		remove_variable = progress_to_victory
	}
	# RACE
	if = {
		limit = {
			scope:activity = { has_current_phase = tournament_phase_horse_race }
		}
		send_interface_toast = {
			title = tournament_qualify_fail_title
			custom_tooltip = tournament_qualify_fail_race_desc
		}
	}
	# VERSUS/TURN
	else = {
		send_interface_toast = {
			title = tournament_qualify_fail_title
			left_icon = root
			custom_tooltip = tournament_qualify_fail_desc
		}
		if = {
			limit = {
				OR = {
					this = scope:activity.special_guest:special_guest_champion
					has_court_position = champion_court_position
				}
				liege = {
					is_ai = no
					is_participant_in_activity = scope:activity
				}
			}
			save_scope_as = contest_champion
			liege = {
				send_interface_toast = {
					title = tournament_champion_qualify_fail_title
					left_icon = scope:contest_champion
					custom_tooltip = tournament_champion_qualify_fail_desc
				}
			}
		}
	}
}

# Save a random contestant on the opposite team
contest_team_save_enemy_member_effect = {
	scope:activity = {
		if = {
			limit = {
				any_guest_subset_current_phase = {
					name = team_alpha
					this = root
				}
			}
			random_guest_subset_current_phase = {
				name = team_beta
				limit = {
					is_alive = yes
					is_ai = yes
				}
				save_scope_as = $SCOPE$
			}
		}
		else = {
			random_guest_subset_current_phase = {
				name = team_beta
				limit = {
					is_alive = yes
					is_ai = yes
				}
				save_scope_as = $SCOPE$
			}
		}
	}
}

# Hand out Tourney Participant trait
tournament_give_trait_effect = {
	if = {
		limit = {
			save_temporary_scope_as = new_list_member
			scope:activity ?= {
				any_guest_subset = {
					name = qualified
					phase = tournament_phase_$CONTEST$
					this = scope:new_list_member
				}
			}
			NOT = { has_trait = tourney_participant }
		}
		hidden_effect = {
			random = {
				chance = 50
				modifier = {
					is_ai = no
					add = 50
				}
				modifier = {
					scope:contest_winner ?= this
					add = 100
				}
				modifier = {
					scope:activity ?= {
						any_guest_subset = {
							name = winner
							phase = tournament_phase_$CONTEST$
							this = root
						}
					}
					add = 50
				}
				modifier = {
					scope:activity ?= {
						any_guest_subset = {
							name = second_place
							phase = tournament_phase_$CONTEST$
							this = root
						}
					}
					add = 30
				}
				modifier = {
					scope:activity ?= {
						any_guest_subset = {
							name = third_place
							phase = tournament_phase_$CONTEST$
							this = root
						}
					}
					add = 15
				}
				add_trait = tourney_participant
			}
		}
		if = {
			limit = { has_trait = tourney_participant }
			show_as_tooltip = { add_trait = tourney_participant }
		}
	}	
}

# Replace qualified contestant for another
tournament_contest_replace_qualifier_effect = {
	$NEW_CONTESTANT$ = { save_scope_as = new_contestant }
	$OLD_CONTESTANT$ = { save_scope_as = old_contestant }
	remove_from_guest_subset = {
		name = qualified
		target = scope:old_contestant
		phase = tournament_phase_$CONTEST$
	}
	add_to_guest_subset = {
		name = spectator
		target = scope:old_contestant
		phase = tournament_phase_$CONTEST$
	}
	add_to_guest_subset = {
		name = qualified
		target = scope:new_contestant
		phase = tournament_phase_$CONTEST$
	}
	scope:old_contestant = {
		remove_variable = contest_aptitude
		remove_variable = progress_to_victory
		save_scope_as = disqualify_target
	}
	scope:new_contestant = {
		tournament_set_current_aptitude_variable_effect = yes
		set_variable = {
			name = progress_to_victory
			value = progress_to_victory_active_contestant_value
		}
	}
	scope:activity = {
		every_attending_character = {
			limit = { is_ai = no }
			send_interface_toast = {
				title = tournament_qualifier_replaced_toast
				left_icon = scope:old_contestant
				right_icon = scope:new_contestant
				custom_description = {
					text = tournament_contest_replaced_tt
					subject = scope:new_contestant # REPLACER
					object = scope:old_contestant # REPLACED
				}
			}
		}
	}
}

# Withdraw character from a contest they have qualified from
tournament_contest_withdraw_effect = {
	$CHARACTER$ = { save_scope_as = withdrawer }
	scope:withdrawer = { remove_variable = contest_aptitude }
	custom_description = {
		text = tournament_contest_withdraw_tt
		subject = scope:withdrawer
	}
}

# Knock qualified contestant out of a contest
tournament_contest_knocked_out_effect = {
	$CHARACTER$ = { save_scope_as = withdrawer }
	scope:withdrawer = {
		remove_variable = contest_aptitude
		involved_activity = {
			remove_from_guest_subset = {
				name = qualified
				target = scope:withdrawer
			}
		}
	}
	custom_description = {
		text = tournament_contest_knocked_tt
		subject = scope:withdrawer
	}
}

# List correct wound status tooltip
tournament_wounded_or_dead_tooltip_effect = {
	show_as_tooltip = {
		if = {
			limit = { is_alive = no }
			death = {
				death_reason = death_contest_$CONTEST$_accident
				killer = scope:contest_winner
			}
		}
		else_if = {
			limit = { has_character_flag = agency_wound }
			switch = {
				trigger = has_trait
				wounded_1 = { add_trait_force_tooltip = wounded_1 }
				wounded_2 = { add_trait_force_tooltip = wounded_2 }
				wounded_3 = { add_trait_force_tooltip = wounded_3 }
			}
		}
	}
}

# Potentially make wandering knight appear in tournament
chance_for_knight_appearance_effect = {
	if = {
		limit = {
			NOT = { exists = scope:activity.var:tournament_commenced }
			scope:activity = {
				any_attending_character = {
					count < 240 #Max is 250, we are leaving some open space.
				}
			}
		}
		random = {
			chance = 50
			every_realm_county = {
				if = {
					limit = {
						save_temporary_scope_as = potential_county
						NOT = {
							any_in_list = {
								list = pool_counties
								duchy = scope:potential_county.duchy
							}
						}
					}
					add_to_temporary_list = pool_counties
				}
			}
			every_in_list = {
				list = pool_counties
				save_temporary_scope_as = pool_county
				every_pool_character = {
					province = scope:pool_county.title_province
					limit = {
						prowess >= 12 #make sure this matches the tooltip of the decision and all the 12s below
						guest_knight_candidate_trigger = { HOST = scope:host }
						is_available = yes
						NOT = { exists = involved_activity }
					}
					add_to_temporary_list = potential_knights
				}
			}
			#Randomize a good knight from the realm pool characters
			random_in_list = {
				list = potential_knights
				limit = { is_alive = yes }
				weight = {
					base = 1
					modifier = {
						add = {
							value = prowess
							subtract = 12
							multiply = 5
						}
					}
				}
				save_scope_as = knight
			}
			#If no one was found, make one
			if = {
				limit = {
					NOT = { exists = scope:knight }
				}
				create_character = {
					location = root.capital_province
					template = pool_repopulate_prowess
					prowess = { 12 15 }
					save_scope_as = knight
				}
			}
			#Cultural Traditions
			if = {
				limit = {
					scope:host.culture = { has_cultural_parameter = better_knights_from_decision }
				}
				hidden_effect = {
					scope:knight = {
						blademaster_lifestyle_rank_up_effect = yes
						knight_increase_prowess_chance_effect = yes
						knight_increase_prowess_chance_effect = yes
					}
				}
			}
			#Add knight to court
			scope:host = {
				send_interface_message = {
					type = msg_tournament_knight_arrives
					title = tournament_knight_appeared_title
					left_icon = scope:knight
					scope:knight = {
						hidden_effect = {
							add_to_activity = scope:activity
							set_location = scope:activity.activity_location
							set_passive_contest_aptitude_and_progress_effect = yes
						}
					}
					custom_tooltip = tournament_knight_appeared_tt
				}
			}
		}
	}
}

# Set aptitude variable based on the current contest type
tournament_set_current_aptitude_variable_effect = {
	set_variable = {
		name = 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 }
		}
	}
}

# Replace qualified contestant with another
tournament_replace_qualifier_scope_effect = {
	# For events replacing a contestant
	scope:activity = {
		if = {
			limit = {
				NOT = { exists = var:contest_aptitude }
			}
			random_guest_subset_current_phase = {
				name = qualified
				limit = {
					is_ai = yes
					NOT = {
						liege ?= { is_ai = no }
						liege.court_position:champion_court_position ?= this
					}
					NOT = { root.court_position:champion_court_position ?= this }
					NOT = {
						any_relation = {
							type = lover
							type = friend
							type = rival
							is_participant_in_activity = scope:activity
							is_ai = no
						}
					}
				}
				weight = {
					base = 1
					modifier = {
						add = 50
						var:contest_aptitude < root.contest_current_contest_aptitude_value
					}
					modifier = { # Try to exclude mysterious strangers
						add = -100
						exists = var:mysterious_stranger_origin
					}
					modifier = { # Try to exclude own knights
						add = -25
						OR = {
							is_knight_of = root
							liege ?= { is_ai = no }
						}
					}
				}
				save_scope_as = replaced_contestant
			}
			if = {
				limit = {
					any_guest_subset_current_phase = {
						name = qualified
						has_relation_rival = root
					}
				}
				random_guest_subset_current_phase = {
					name = qualified
					limit = { has_relation_rival = root }
					save_scope_as = rival_contestant
				}
			}
		}
	}
	if = {
		limit = { exists = var:contest_aptitude }
		scope:activity = {
			ordered_guest_subset_current_phase = {
				name = contestant
				limit = {
					save_temporary_scope_as = new_list_member
					NOR = {
						scope:activity = {
							any_guest_subset_current_phase = {
								name = qualified
								this = scope:new_list_member
							}
						}
					}
					activity_is_valid_tournament_contestant = yes
				}
				order_by = contest_current_contest_aptitude_value
				save_scope_as = new_contestant
			}
		}
	}
	else = { save_scope_as = new_contestant }
}

# Save relevant scopes for start of contest events
tournament_contest_start_portrait_effect = {
	if = {
		limit = {
			intent_target ?= { is_alive = yes }
		}
		intent_target = { save_scope_as = intent_scope }
	}
	tournament_contest_champion_scope_effect = yes
	if = {
		limit = { exists = scope:marriage_prize }
		scope:marriage_prize = { save_scope_as = portrait_scope }
	}
	else_if = {
		limit = {
			exists = scope:mysterious_stranger
			NAND = {
				exists = scope:versus_contestant
				scope:versus_contestant = scope:mysterious_stranger
			}
		}
		scope:mysterious_stranger = { save_scope_as = portrait_scope }	
	}
	else_if = {
		limit = { exists = scope:contest_champion }
		scope:contest_champion = { save_scope_as = portrait_scope }
	}
	scope:activity = {
		ordered_guest_subset_current_phase = {
			name = qualified
			limit = {
				NAND = { exists = scope:contest_summary_1 this = scope:contest_summary_1 }
				NAND = { exists = scope:contest_summary_2 this = scope:contest_summary_2 }
				NAND = { exists = scope:contest_summary_3 this = scope:contest_summary_3 }
				NAND = { exists = scope:contest_summary_4 this = scope:contest_summary_4 }
				NAND = { exists = scope:contest_summary_5 this = scope:contest_summary_5 }
				NAND = { exists = scope:contest_summary_6 this = scope:contest_summary_6 }
				NAND = { exists = scope:contest_summary_7 this = scope:contest_summary_7 }
			}
			max = 8
			check_range_bounds = no
			order_by = primary_title.tier
			if = {
				limit = { NOT = { exists = scope:contest_summary_1 } }
				save_scope_as = contest_summary_1
			}
			else_if = {
				limit = { NOT = { exists = scope:contest_summary_2 } }
				save_scope_as = contest_summary_2
			}
			else_if = {
				limit = { NOT = { exists = scope:contest_summary_3 } }
				save_scope_as = contest_summary_3
			}
			else_if = {
				limit = { NOT = { exists = scope:contest_summary_4 } }
				save_scope_as = contest_summary_4
			}
			else_if = {
				limit = { NOT = { exists = scope:contest_summary_5 } }
				save_scope_as = contest_summary_5
			}
			else_if = {
				limit = { NOT = { exists = scope:contest_summary_6 } }
				save_scope_as = contest_summary_6
			}
			else_if = {
				limit = { NOT = { exists = scope:contest_summary_7 } }
				save_scope_as = contest_summary_7
			}
			else = { save_scope_as = contest_summary_8 }
		}
	}
	tournament_contest_versus_match_scope_effect = { CHAR = root }
}

# Save relevant scopes for end of contest events
tournament_contest_end_portrait_effect = {
	tournament_contest_champion_scope_effect = yes
	if = {
		limit = {
			OR = {
				scope:contest_winner ?= this
				involved_activity = {
					any_guest_subset_current_phase = { name = winner this = root }
				}
			}
			exists = scope:contest_loser
		}
		scope:contest_loser = { save_scope_as = contest_portrait }
	}
	else_if = {
		limit = {
			OR = {
				scope:contest_winner ?= this
				involved_activity = {
					any_guest_subset_current_phase = { name = winner this = root }
				}
			}
			exists = scope:second_place
		}
		scope:second_place = { save_scope_as = contest_portrait }
	}
	else_if = {
		limit = {
			OR = {
				scope:contest_loser ?= this
				NOT = {
					involved_activity = {
						any_guest_subset_current_phase = { name = winner this = root }
					}
				}
			}
			exists = scope:contest_winner
		}
		scope:contest_winner = { save_scope_as = contest_portrait }
	}
}

# Accidentally kill a character, inform other attendees, and track total number of accidents
tournament_accidental_death_effect = {
	scope:activity = {
		if = {
			limit = { exists = scope:accident_killer }
			add_activity_log_entry = {
				key = tournament_accident_killer_log
				tags = { contestant death bad }
				score = 25
				character = scope:accident_victim
				target = scope:accident_killer
			}
			scope:accident_victim = {
				death = {
					death_reason = death_contest_melee_accident
					killer = scope:accident_killer
				}
			}
		}	
		else = {
			add_activity_log_entry = {
				key = tournament_accident_log
				tags = { contestant death bad }
				score = 25
				character = scope:accident_victim
			}
			scope:accident_victim = {
				death = { death_reason = death_contest_melee_accident }
			}
		}
		every_attending_character = {
			if = {
				limit = {
					OR = {
						has_any_scripted_relation = scope:accident_victim
						is_close_family_of = scope:accident_victim
						is_consort_of = scope:accident_victim
						AND = {
							this = scope:accident_victim.liege
							tier_difference = {
								target = scope:accident_victim
								value <= 1
							}
						}
						liege = scope:accident_victim
					}
				}
				trigger_event = tournament_events.0701
			}
			else = {
				send_interface_message = {
					type = event_death_bad
					title = tournament_accident_death_msg_title
					left_icon = scope:accident_victim
					show_as_tooltip = {
						if = {
							limit = { exists = scope:accident_killer }
							scope:accident_victim = {
								death = {
									death_reason = death_contest_melee_accident
									killer = scope:accident_killer
								}
							}
						}
						else = {
							scope:accident_victim = {
								death = { death_reason = death_contest_melee_accident }
							}
						}
					}
				}
			}
		}
		# For making sure there are not obscene numbers of accidents
		if = {
			limit = { exists = var:accident_death_count }
			change_variable = {
				name = accident_death_count
				add = 1
			}
		}
		else = {
			set_variable = {
				name = accident_death_count
				value = 1
			}
		}
	}
}

tournament_versus_create_contestant_fallback_effect = {
	scope:activity = {
		random_attending_character = {
			culture = { save_scope_as = attending_culture }
		}
		scope:attending_culture = {
			random_culture_county = {
				faith = { save_scope_as = attending_faith }
				random_direct_de_jure_vassal_title = { save_scope_as = attending_barony }
			}
		}
		if = {
			limit = { has_current_phase = tournament_phase_joust }
			create_character = {
				template = tournament_horse_character
				location = scope:activity.activity_location
				faith = scope:attending_faith
				culture = scope:attending_culture
				dynasty = none
				save_scope_as = backup_contestant
			}
			scope:backup_contestant = {
				add_to_activity = scope:activity
			}
		}
		else_if = {
			limit = { has_current_phase = tournament_phase_board_game }
			create_character = {
				template = tournament_board_game_character
				location = scope:activity.activity_location
				faith = scope:attending_faith
				culture = scope:attending_culture
				dynasty = none
				save_scope_as = backup_contestant
			}
			scope:backup_contestant = {
				add_to_activity = scope:activity
			}
		}
		else_if = {
			limit = { has_current_phase = tournament_phase_wrestling }
			create_character = {
				template = tournament_pugilism_character
				location = scope:activity.activity_location
				faith = scope:attending_faith
				culture = scope:attending_culture
				dynasty = none
				save_scope_as = backup_contestant
			}
			scope:backup_contestant = {
				add_to_activity = scope:activity
			}
		}
		else = {
			create_character = {
				template = tournament_foot_character
				location = scope:activity.activity_location
				faith = scope:attending_faith
				culture = scope:attending_culture
				dynasty = none
				save_scope_as = backup_contestant
			}
			scope:backup_contestant = {
				add_to_activity = scope:activity
			}
		}
	}
}

tournament_complete_win_contest_intent_effect = {
	if = {
		limit = {
			has_activity_intent = win_contest_intent
			NOT = { has_completed_activity_intent = win_contest_intent }
		}
		send_interface_toast = {
			title = activity_intent_complete_toast
			left_icon = this
			complete_activity_intent = yes
			if = {
				limit = {
					NOT = {
						involved_activity = { has_current_phase = tournament_phase_melee }
					}
				}
				stress_impact = { base = medium_stress_impact_loss }
			}
			else = {
				stress_impact = { base = minor_stress_impact_loss }
			}
		}
	}
}

tournament_add_contestant_or_spectator_effect = {
	save_scope_as = new_arrival
	if = {
		limit = {
			NOR = {
				is_in_guest_subset = { name = contestant }
				is_in_guest_subset = { name = spectator }
			}
		}
		if = {
			limit = { activity_is_valid_tournament_contestant = yes }
			involved_activity = {
				add_to_guest_subset = { name = contestant target = scope:new_arrival }
			}
		}
		else = {
			involved_activity = {
				add_to_guest_subset = { name = spectator target = scope:new_arrival }
			}
		}
	}
}

tournament_add_as_spectator_effect = {
	save_temporary_scope_as = new_arrival
	if = {
		limit = {
			NOR = {
				is_in_guest_subset = { name = qualified }
				is_in_guest_subset = { name = spectator }
			}
		}
		involved_activity = {
			add_to_guest_subset = { name = spectator target = scope:new_arrival }
		}
	}
}

tournament_choose_jockey_effect = {
	save_scope_as = chosen_jockey
	if = {
		limit = {
			NOT = { is_participant_in_activity = scope:activity }
		}
		hidden_effect = { add_to_activity = scope:activity }
		scope:activity = {
			add_to_guest_subset = { name = spectator target = scope:chosen_jockey }
		}
	}
	every_in_list = {
		list = potential_jockeys
		limit = {
			NOR = { 
				this = scope:chosen_jockey 
				this = root
				has_character_flag = generated_jockey
			}
		}
		add_opinion = {
			target = root
			modifier = disappointed_opinion
			opinion = -10
		}
	}
	root = {
		set_variable = {
			name = horse_race_jockey
			value = scope:chosen_jockey
		}
	}
}

tournament_liege_qualification_toast_effect = {
	every_in_list = {
		list = qualification_liege_list
		save_scope_as = qualification_liege
		# Save number below 2 for toast
		set_variable = {
			name = qualified_knights_count
			value = {
				value = 0
				scope:qualification_liege = {
					every_in_list = {
						variable = qualified_knights
						limit = {
							NOR = {
								has_court_position = champion_court_position
								scope:qualification_liege.court_position:champion_court_position ?= this
							}
						}
						add = 1
					}
				}
			}
			days = 1
		}
		# If champion qualified
		if = {
			limit = {
				any_in_list = {
					variable = qualified_knights
					OR = {
						has_court_position = champion_court_position
						scope:qualification_liege.court_position:champion_court_position ?= this
					}
				}
			}
			random_in_list = {
				variable = qualified_knights
				limit = {
					OR = {
						has_court_position = champion_court_position
						scope:qualification_liege.court_position:champion_court_position ?= this
					}
				}
				save_scope_as = contest_champion
			}
		}
		if = {
			limit = {
				any_in_list = {
					variable = qualified_knights
					NOT = { scope:contest_champion ?= this  }
				}
			}
			random_in_list = {
				variable = qualified_knights
				limit = {
					NOT = { scope:contest_champion ?= this  }
					is_of_major_interest_trigger = { CHARACTER = scope:qualification_liege }
				}
				alternative_limit = {
					NOT = { scope:contest_champion ?= this  }
					is_of_minor_interest_trigger = { CHARACTER = scope:qualification_liege }
				}
				alternative_limit = {
					NOT = { scope:contest_champion ?= this  }
				}
				save_scope_as = knight_1
			}
		}
		if = {
			limit = {
				NOT = { exists = scope:contest_champion }
				any_in_list = {
					variable = qualified_knights
					NOT = { scope:knight_1 ?= this }
				}
			}
			random_in_list = {
				variable = qualified_knights
				limit = {
					NOT = { scope:knight_1 ?= this }
					is_of_major_interest_trigger = { CHARACTER = scope:qualification_liege }
				}
				alternative_limit = {
					NOT = { scope:knight_1 ?= this }
					is_of_minor_interest_trigger = { CHARACTER = scope:qualification_liege }
				}
				alternative_limit = {
					NOT = { scope:knight_1 ?= this }
				}
				save_scope_as = knight_2
			}
		}
		if = { # Champion + knights
			limit = {
				exists = scope:contest_champion
				exists = scope:knight_1
			}
			send_interface_toast = {
				type = toast_tournament_qualification_champion
				title = tournament_champion_qualify_multi_title
				left_icon = scope:contest_champion
				right_icon = scope:knight_1
				custom_tooltip = tournament_champion_qualify_multi_desc
				add_prestige = {
					value = { # Knights
						value = miniscule_prestige_gain
						multiply = var:qualified_knights_count
					}
					add = minor_prestige_gain # Champion
				}
			}
		}
		else_if = { # Champion
			limit = { exists = scope:contest_champion }
			send_interface_toast = {
				type = toast_tournament_qualification_champion
				title = tournament_champion_qualify_title
				left_icon = scope:contest_champion
				right_icon = scope:knight_1
				custom_tooltip = tournament_champion_qualify_desc
				add_prestige = minor_prestige_gain # Champion
			}
		}
		else_if = { # 2 or more knights
			limit = { exists = scope:knight_2 }
			send_interface_toast = {
				type = toast_tournament_qualification_knight
				title = tournament_knight_qualify_multi_title
				left_icon = scope:knight_1
				right_icon = scope:knight_2
				custom_tooltip = tournament_knight_qualify_multi_desc
				add_prestige = {
					value = { # Knights
						value = miniscule_prestige_gain
						multiply = var:qualified_knights_count
					}
				}
			}
		}
		else_if = { # 1 knight
			limit = { exists = scope:knight_1 }
			send_interface_toast = {
				type = toast_tournament_qualification_knight
				title = tournament_knight_qualify_title
				left_icon = scope:knight_1
				custom_tooltip = tournament_knight_qualify_desc
				add_prestige = {
					value = { # Knight
						value = miniscule_prestige_gain
						multiply = var:qualified_knights_count
					}
				}
			}
		}
		clear_variable_list = qualified_knights
	}
}

# Save winner scopes for summary event
tournament_save_contest_winner_scopes_effect = {
	scope:activity ?= {
		if = {
			limit = {
				any_guest_subset = {
					name = winner
					phase = tournament_phase_melee
					is_alive = yes
				}
			}
			random_guest_subset = {
				name = winner 
				phase = tournament_phase_melee
				limit = {
					scope:activity.var:melee_winner ?= this
					is_alive = yes
				}
				alternative_limit = { is_alive = yes } 
				weight = {
					base = 1
					tournament_team_captain_modifier = yes
				}
				save_scope_as = melee_winner 
			}
			set_variable = {
				name = melee_winner
				value = scope:melee_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_archery }
			}
			random_guest_subset = { 
				name = winner 
				phase = tournament_phase_archery 
				save_scope_as = archery_winner
			}
			set_variable = {
				name = archery_winner
				value = scope:archery_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_horse_race }
			}
			random_guest_subset = { 
				name = winner 
				phase = tournament_phase_horse_race 
				save_scope_as = horse_race_winner 
			}
			set_variable = {
				name = horse_race_winner
				value = scope:horse_race_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_recital }
			}
			random_guest_subset = { 
				name = winner phase = tournament_phase_recital 
				save_scope_as = recital_winner 
			}
			set_variable = {
				name = recital_winner
				value = scope:recital_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_joust }
			}
			random_guest_subset = { 
				name = winner
				phase = tournament_phase_joust 
				save_scope_as = joust_winner 
			}
			set_variable = {
				name = joust_winner
				value = scope:joust_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_wrestling }
			}
			random_guest_subset = { 
				name = winner 
				phase = tournament_phase_wrestling 
				save_scope_as = wrestling_winner 
			}
			set_variable = {
				name = wrestling_winner
				value = scope:wrestling_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_duel }
			}
			random_guest_subset = { 
				name = winner phase = tournament_phase_duel 
				save_scope_as = duel_winner 
			}
			set_variable = {
				name = duel_winner
				value = scope:duel_winner
			}
		}
		if = {
			limit = {
				any_guest_subset = { name = winner phase = tournament_phase_board_game }
			}
			random_guest_subset = {
				name = winner 
				phase = tournament_phase_board_game 
				save_scope_as = board_game_winner 
			}
			set_variable = {
				name = board_game_winner
				value = scope:board_game_winner
			}
		}
	}
}

# Award prize artifacts to all contest winners
tournament_disburse_contest_prize_effect = {
	if = {
		limit = { exists = scope:duel_winner }
		tournament_contest_generate_prize_effect = { CONTEST = duel }
	}
	if = {
		limit = { exists = scope:joust_winner }
		tournament_contest_generate_prize_effect = { CONTEST = joust }
	}
	if = {
		limit = { exists = scope:wrestling_winner }
		tournament_contest_generate_prize_effect = { CONTEST = wrestling }
	}
	if = {
		limit = { exists = scope:board_game_winner }
		tournament_contest_generate_prize_effect = { CONTEST = board_game }
	}
	if = {
		limit = { exists = scope:archery_winner }
		tournament_contest_generate_prize_effect = { CONTEST = archery }
	}
	if = {
		limit = { exists = scope:recital_winner }
		tournament_contest_generate_prize_effect = { CONTEST = recital }
	}
	if = {
		limit = { exists = scope:horse_race_winner }
		tournament_contest_generate_prize_effect = { CONTEST = horse_race }
	}
	if = {
		limit = { exists = scope:melee_winner }
		tournament_contest_generate_prize_effect = { CONTEST = melee }
	}
}

# Show tooltips for prize awards
tournament_disburse_contest_prize_tooltip_effect = {
	if = {
		limit = { exists = scope:duel_prize }
		show_as_tooltip = {
			scope:duel_prize = {
				set_owner = { target = scope:duel_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:joust_prize }
		show_as_tooltip = {
			scope:joust_prize = {
				set_owner = { target = scope:joust_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:wrestling_prize }
		show_as_tooltip = {
			scope:wrestling_prize = {
				set_owner = { target = scope:wrestling_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:board_game_prize }
		show_as_tooltip = {
			scope:board_game_prize = {
				set_owner = { target = scope:board_game_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:archery_prize }
		show_as_tooltip = {
			scope:archery_prize = {
				set_owner = { target = scope:archery_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:recital_prize }
		show_as_tooltip = {
			scope:recital_prize = {
				set_owner = { target = scope:recital_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:horse_race_prize }
		show_as_tooltip = {
			scope:horse_race_prize = {
				set_owner = { target = scope:horse_race_winner }
			}
		}
	}
	if = {
		limit = { exists = scope:melee_prize }
		show_as_tooltip = {
			scope:melee_prize = {
				set_owner = { target = scope:melee_winner }
			}
		}
	}
}

# Save pivotal contestant perspective scope, root or someone important to you
tournament_pivotal_contestant_scope_effect = {
	if = {
		limit = {
			trigger_if = {
				limit = {
					scope:activity = {
						activity_tournament_versus_contest_trigger = yes
						var:contest_versus_progress ?= 2
					}
				}
				is_in_guest_subset = { name = finalist }
			}
			trigger_else_if = {
				limit = {
					scope:activity = {
						activity_tournament_versus_contest_trigger = yes
						var:contest_versus_progress ?= 1
					}
				}
				is_in_guest_subset = { name = semi_finalist }
			}
			trigger_else = {
				is_in_guest_subset = { name = qualified }
			}
		}
		save_scope_as = pivotal_contestant
	}
	# Otherwise, find someone you care about to frame the event
	else = {
		# Filter out anyone who is no longer a valid contestant
		scope:activity = {
			every_guest_subset_current_phase = {
				name = qualified
				limit = {
					trigger_if = {
						limit = { scope:activity.var:contest_versus_progress ?= 2 }
						is_in_guest_subset = { name = finalist }
					}
					trigger_else_if = {
						limit = { scope:activity.var:contest_versus_progress ?= 1 }
						is_in_guest_subset = { name = semi_finalist }
					}
					trigger_else = { always = yes }
					this != root
				}
				add_to_list = potential_pivotal_contestants
			}
		}
		# Randomly select the most relevant character possible
		random_in_list = {
			list = potential_pivotal_contestants
			# Intent Targets
			limit = { root.intent_target ?= this }
			# Wager Targets
			alternative_limit = { root.var:tournament_wager_target ?= this }
			# Relations
			alternative_limit = { has_relation_to = root }
			# Champions
			alternative_limit = { root.court_position:champion_court_position ?= this }
			# Children
			alternative_limit = { is_child_of = root }
			# Spouses
			alternative_limit = { is_consort_of = root }
			# Close Family
			alternative_limit = { is_close_family_of = root }
			# Liege
			alternative_limit = { root.liege ?= this }
			# Powerful Vassals
			alternative_limit = { is_powerful_vassal_of = root }
			# Vassals
			alternative_limit = { is_vassal_of = root }
			# Knights
			alternative_limit = { is_knight_of = root }
			# Extended Family
			alternative_limit = { is_extended_family_of = root }
			# Courtiers
			alternative_limit = { is_courtier_of = root }
			# Other
			alternative_limit = { always = yes }
			weight = {
				base = 1
				opinion_modifier = {
					who = root
					opinion_target = this
				}
				modifier = { add = var:progress_to_victory }
			}
			save_scope_as = pivotal_contestant
		}
	}
	if = {
		limit = {
			exists = scope:contest_winner
			exists = scope:contest_loser
		}
		if = {
			limit = { scope:pivotal_contestant = scope:contest_winner }
			scope:contest_loser = { save_scope_as = other_contestant }
		}
		else = {
			scope:contest_winner = { save_scope_as = other_contestant }
		}
	}
}

# Save values for determining overall progress in a contest
tournament_pivotal_ptv_value_effect = {
	if = {
		limit = { exists = scope:pivotal_contestant.var:progress_to_victory }
		scope:activity = {
			ordered_guest_subset_current_phase = {
				name = qualified
				limit = { exists = var:progress_to_victory }
				order_by = var:progress_to_victory
				save_scope_as = best_contestant
			}
			ordered_guest_subset_current_phase = {
				name = qualified
				limit = { exists = var:progress_to_victory }
				order_by = {
					value = 0 
					subtract = var:progress_to_victory
				}
				save_scope_as = worst_contestant
			}
		}
		save_scope_value_as = {
			name = best_worst_diff
			value = {
				value = scope:best_contestant.var:progress_to_victory
				subtract = scope:worst_contestant.var:progress_to_victory
			}
		}
		save_scope_value_as = {
			name = ptv_half_way
			value = {
				value = scope:worst_contestant.var:progress_to_victory
				add = {
					value = scope:best_worst_diff
					divide = 2
				}
			}
		}
		save_scope_value_as = {
			name = pivotal_best_diff
			value = {
				value = scope:pivotal_contestant.var:progress_to_victory
				subtract = scope:best_contestant.var:progress_to_victory
			}
		}
		save_scope_value_as = {
			name = pivotal_worst_diff
			value = {
				value = scope:pivotal_contestant.var:progress_to_victory
				subtract = scope:worst_contestant.var:progress_to_victory
			}
		}
	}
}

# Save values for determining overall progress in a contest
tournament_pivotal_ptv_versus_value_effect = {
	save_scope_value_as = {
		name = pivotal_diff
		value = {
			value = scope:pivotal_contestant.character_chance_of_victory
			subtract = scope:versus_contestant.character_chance_of_victory
		}
	}
}

tournament_pivotal_flavor_variable_effect = {
	set_variable = {
		name = contest_$CONTEST$_flavor
		value = $ID$
		months = 1
	}
}

tournament_pivotal_duel_effect = {
	save_scope_value_as = {
		name = no_change_chance
		value = $NO_CHANGE_CHANCE$
	}
	save_scope_as = contestant
	remove_character_flag = success_pivotal_flag
	remove_character_flag = neutral_pivotal_flag
	remove_character_flag = failure_pivotal_flag
	$DUELIST$ = { save_scope_as = duelist } # used if a proxy's skills are tested, e.g. a jockey
	scope:duelist = {
		duel = {
			skills = { $SKILL_1$ $SKILL_2$ }
			value = decent_skill_rating
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = 5
				}
				tournament_horse_contest_trait_pivotal_modifier = yes
				tournament_foot_contest_trait_pivotal_modifier = yes
				tournament_wit_contest_trait_pivotal_modifier = yes
				tournament_bow_contest_trait_pivotal_modifier = yes
				scope:contestant = {
					activity_tournament_change_contestant_score_effect = { SCORE = increase_$INCREASE$ }
					add_character_flag = success_pivotal_flag
				}
			}
			10 = {
				trigger = { scope:no_change_chance = yes }
				scope:contestant = { add_character_flag = neutral_pivotal_flag }
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = 5
				}
				scope:contestant = {
					activity_tournament_change_contestant_score_effect = { SCORE = decrease_$DECREASE$ }
					add_character_flag = failure_pivotal_flag
				}
			}
		}
	}
}

tournament_pivotal_duel_single_skill_effect = {
	save_scope_value_as = {
		name = no_change_chance
		value = $NO_CHANGE_CHANCE$
	}
	save_scope_as = contestant
	$DUELIST$ = { save_scope_as = duelist } # used if a proxy's skills are tested, e.g. a jockey
	scope:duelist = {
		duel = {
			skill = $SKILL_1$
			value = decent_skill_rating
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = 5
				}
				tournament_horse_contest_trait_pivotal_modifier = yes
				tournament_foot_contest_trait_pivotal_modifier = yes
				tournament_wit_contest_trait_pivotal_modifier = yes
				tournament_bow_contest_trait_pivotal_modifier = yes
				scope:contestant = {
					activity_tournament_change_contestant_score_effect = { SCORE = increase_$INCREASE$ }
					add_character_flag = success_pivotal_flag
				}
			}
			50 = {
				trigger = { scope:no_change_chance = yes }
				scope:contestant = { add_character_flag = neutral_pivotal_flag }
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = 5
				}
				scope:contestant = {
					activity_tournament_change_contestant_score_effect = { SCORE = decrease_$DECREASE$ }
					add_character_flag = failure_pivotal_flag
				}
			}
		}
	}
}

versus_tyrannical_disqualification_effect = {
	scope:contest_winner = { save_scope_as = tyrannical_disqualification_target }
	scope:activity = {
		add_activity_log_entry = {
			key = contest_tyrannical_disqualification_log
			score = 50
			tags = { contestant removal }
			character = scope:contest_loser
			target = scope:contest_winner

			# Effects
			scope:contest_loser = {
				add_tyranny = minor_tyranny_gain
				add_prestige = minor_prestige_loss
				add_character_modifier = {
					modifier = tournament_tyrannical_disqualification_modifier
					years = 5
				}
				reverse_add_opinion = {
					target = scope:contest_winner
					modifier = tournament_tyrannical_disqualification_opinion
					opinion = -15
				}
			}
		}
		switch = {
			trigger = has_current_phase
			# Joust
			tournament_phase_joust = {
				tournament_contest_versus_swap_winner_loser_effect = { CONTEST = joust SKILL = horse }
			}
			# Wrestling
			tournament_phase_wrestling = {
				tournament_contest_versus_swap_winner_loser_effect = { CONTEST = wrestling SKILL = pugilism }
			}
			# Duel
			tournament_phase_duel = {
				tournament_contest_versus_swap_winner_loser_effect = { CONTEST = duel SKILL = foot }
			}
			# Board Game
			tournament_phase_board_game = {
				tournament_contest_versus_swap_winner_loser_effect = { CONTEST = board_game SKILL = board_game }
			}
		}
	}
	stress_impact = {
        just = medium_stress_impact_gain
        humble = medium_stress_impact_gain
    }
}

#ADD RANDOM HASTILUDES SKILL LEVELS
# with some mighty stacks of modifiers to make them culturally appropriate
add_random_tourney_participant_level_effect = {
	if = {
		limit = {
			has_trait = tourney_participant
		}
		random_list = {
			40 = { }
			# FOOT XP
			15 = {
				add_trait_xp = {
					trait = tourney_participant
					track = foot
					value = 20
				}
				modifier = {
					add = 40
					culture = {
						OR = {
							has_cultural_tradition = tradition_fp1_coastal_warriors
							has_cultural_tradition = tradition_stand_and_fight
							has_cultural_tradition = tradition_druzhina
							has_cultural_tradition = tradition_futuwaa
							has_cultural_tradition = tradition_garuda_warriors
							has_cultural_tradition = tradition_himalayan_settlers
							has_cultural_tradition = tradition_khadga_puja
							has_cultural_tradition = tradition_mountaineer_ruralism
							has_cultural_tradition = tradition_hird
						}
					}
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_metal_craftsmanship
							has_cultural_tradition = tradition_roman_legacy
							has_cultural_tradition = tradition_mountain_homes
							has_cultural_tradition = tradition_polders
							has_cultural_tradition = tradition_republican_legacy
							has_cultural_tradition = tradition_formation_fighting
							has_cultural_tradition = tradition_fp1_trials_by_combat
							has_cultural_tradition = tradition_fp1_performative_honour
							has_cultural_tradition = tradition_caucasian_wolves
							has_cultural_tradition = tradition_ep3_audacious_cadets
							has_cultural_tradition = tradition_mobile_guards
							has_cultural_tradition = tradition_mountain_herding
							has_cultural_tradition = tradition_horn_mountain_skirmishing
							has_cultural_tradition = tradition_mubarizuns
							has_cultural_tradition = tradition_burman_royal_army
							has_cultural_tradition = tradition_upland_skirmishing
						}
					}
				}
				modifier = {
					add = -20
					culture = {
						OR = {
							has_cultural_tradition = tradition_frugal_armorsmiths
							has_cultural_tradition = tradition_strength_in_numbers
						}
					}
				}
				modifier = {
					add = -25
					prowess <= 8
				}
			}
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = foot
					value = 50
				}
				modifier = {
					add = 30
					culture = {
						OR = {
							has_cultural_tradition = tradition_fp1_coastal_warriors
							has_cultural_tradition = tradition_stand_and_fight
							has_cultural_tradition = tradition_druzhina
							has_cultural_tradition = tradition_futuwaa
							has_cultural_tradition = tradition_garuda_warriors
							has_cultural_tradition = tradition_himalayan_settlers
							has_cultural_tradition = tradition_khadga_puja
							has_cultural_tradition = tradition_mountaineer_ruralism
							has_cultural_tradition = tradition_hird
						}
					}
				}
				modifier = {
					add = 15
					culture = {
						OR = {
							has_cultural_tradition = tradition_metal_craftsmanship
							has_cultural_tradition = tradition_roman_legacy
							has_cultural_tradition = tradition_mountain_homes
							has_cultural_tradition = tradition_polders
							has_cultural_tradition = tradition_republican_legacy
							has_cultural_tradition = tradition_formation_fighting
							has_cultural_tradition = tradition_fp1_trials_by_combat
							has_cultural_tradition = tradition_fp1_performative_honour
							has_cultural_tradition = tradition_caucasian_wolves
							has_cultural_tradition = tradition_ep3_audacious_cadets
							has_cultural_tradition = tradition_mobile_guards
							has_cultural_tradition = tradition_mountain_herding
							has_cultural_tradition = tradition_horn_mountain_skirmishing
							has_cultural_tradition = tradition_mubarizuns
							has_cultural_tradition = tradition_burman_royal_army
							has_cultural_tradition = tradition_upland_skirmishing
						}
					}
				}
				modifier = {
					add = -20
					culture = {
						OR = {
							has_cultural_tradition = tradition_frugal_armorsmiths
							has_cultural_tradition = tradition_strength_in_numbers
						}
					}
				}
				modifier = {
					add = -25
					prowess <= 8
				}
			}
			5 = {
				add_trait_xp = {
					trait = tourney_participant
					track = foot
					value = 80
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_fp1_coastal_warriors
							has_cultural_tradition = tradition_stand_and_fight
							has_cultural_tradition = tradition_druzhina
							has_cultural_tradition = tradition_futuwaa
							has_cultural_tradition = tradition_garuda_warriors
							has_cultural_tradition = tradition_himalayan_settlers
							has_cultural_tradition = tradition_khadga_puja
							has_cultural_tradition = tradition_mountaineer_ruralism
							has_cultural_tradition = tradition_hird
						}
					}
				}
				modifier = {
					add = 10
					culture = {
						OR = {
							has_cultural_tradition = tradition_metal_craftsmanship
							has_cultural_tradition = tradition_roman_legacy
							has_cultural_tradition = tradition_mountain_homes
							has_cultural_tradition = tradition_polders
							has_cultural_tradition = tradition_republican_legacy
							has_cultural_tradition = tradition_formation_fighting
							has_cultural_tradition = tradition_fp1_trials_by_combat
							has_cultural_tradition = tradition_fp1_performative_honour
							has_cultural_tradition = tradition_caucasian_wolves
							has_cultural_tradition = tradition_ep3_audacious_cadets
							has_cultural_tradition = tradition_mobile_guards
							has_cultural_tradition = tradition_mountain_herding
							has_cultural_tradition = tradition_horn_mountain_skirmishing
							has_cultural_tradition = tradition_mubarizuns
							has_cultural_tradition = tradition_burman_royal_army
							has_cultural_tradition = tradition_upland_skirmishing
						}
					}
				}
				modifier = {
					add = -15
					culture = {
						OR = {
							has_cultural_tradition = tradition_frugal_armorsmiths
							has_cultural_tradition = tradition_strength_in_numbers
						}
					}
				}
				modifier = {
					add = -25
					prowess <= 8
				}
			}
			# HORSE XP
			15 = {
				add_trait_xp = {
					trait = tourney_participant
					track = horse
					value = 20
				}
				modifier = {
					add = 40
					culture = {
						OR = {
							has_cultural_tradition = tradition_roman_legacy
							has_cultural_tradition = tradition_horse_breeder
							has_cultural_tradition = tradition_chivalry
							has_cultural_tradition = tradition_desert_ribat
							has_cultural_tradition = tradition_desert_nomads
							has_cultural_tradition = tradition_horse_lords
							has_cultural_tradition = tradition_hussar
							has_cultural_tradition = tradition_saharan_nomads
							has_cultural_tradition = tradition_ep3_indomitable_azatani
							has_cultural_tradition = tradition_ep3_imperial_tagmata
						}
					}
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_dryland_dwellers
							has_cultural_tradition = tradition_pastoralists
							has_cultural_tradition = tradition_hit_and_run
							has_cultural_tradition = tradition_hunters
							has_cultural_tradition = tradition_caucasian_wolves
							has_cultural_tradition = tradition_ep3_audacious_cadets
							has_cultural_tradition = tradition_chanson_de_geste
							has_cultural_tradition = tradition_burman_royal_army
							has_cultural_tradition = tradition_sacred_hunts
							has_cultural_tradition = tradition_lords_of_the_elephant
						}
					}
				}
				modifier = {
					add = -20
					prowess <= 4
				}
			}
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = horse
					value = 50
				}
				modifier = {
					add = 30
					culture = {
						OR = {
							has_cultural_tradition = tradition_roman_legacy
							has_cultural_tradition = tradition_horse_breeder
							has_cultural_tradition = tradition_chivalry
							has_cultural_tradition = tradition_desert_ribat
							has_cultural_tradition = tradition_desert_nomads
							has_cultural_tradition = tradition_horse_lords
							has_cultural_tradition = tradition_hussar
							has_cultural_tradition = tradition_lords_of_the_elephant
							has_cultural_tradition = tradition_saharan_nomads
							has_cultural_tradition = tradition_ep3_indomitable_azatani
							has_cultural_tradition = tradition_ep3_imperial_tagmata
						}
					}
				}
				modifier = {
					add = 15
					culture = {
						OR = {
							has_cultural_tradition = tradition_dryland_dwellers
							has_cultural_tradition = tradition_pastoralists
							has_cultural_tradition = tradition_hit_and_run
							has_cultural_tradition = tradition_hunters
							has_cultural_tradition = tradition_caucasian_wolves
							has_cultural_tradition = tradition_ep3_audacious_cadets
							has_cultural_tradition = tradition_chanson_de_geste
							has_cultural_tradition = tradition_burman_royal_army
							has_cultural_tradition = tradition_sacred_hunts
						}
					}
				}
				modifier = {
					add = -20
					prowess <= 4
				}
			}
			5 = {
				add_trait_xp = {
					trait = tourney_participant
					track = horse
					value = 80
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_roman_legacy
							has_cultural_tradition = tradition_horse_breeder
							has_cultural_tradition = tradition_chivalry
							has_cultural_tradition = tradition_desert_ribat
							has_cultural_tradition = tradition_desert_nomads
							has_cultural_tradition = tradition_horse_lords
							has_cultural_tradition = tradition_hussar
							has_cultural_tradition = tradition_lords_of_the_elephant
							has_cultural_tradition = tradition_saharan_nomads
							has_cultural_tradition = tradition_ep3_indomitable_azatani
							has_cultural_tradition = tradition_ep3_imperial_tagmata
						}
					}
				}
				modifier = {
					add = 10
					culture = {
						OR = {
							has_cultural_tradition = tradition_dryland_dwellers
							has_cultural_tradition = tradition_pastoralists
							has_cultural_tradition = tradition_hit_and_run
							has_cultural_tradition = tradition_hunters
							has_cultural_tradition = tradition_caucasian_wolves
							has_cultural_tradition = tradition_ep3_audacious_cadets
							has_cultural_tradition = tradition_chanson_de_geste
							has_cultural_tradition = tradition_burman_royal_army
							has_cultural_tradition = tradition_sacred_hunts
						}
					}
				}
				modifier = {
					add = -20
					prowess <= 4
				}
			}
			# WIT XP
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = wit
					value = 20
				}
				modifier = {
					add = 45
					culture = {
						OR = {
							has_cultural_tradition = tradition_astute_diplomats
							has_cultural_tradition = tradition_welcoming
							has_cultural_parameter = poet_trait_gives_bonuses
							has_cultural_tradition = tradition_storytellers
							has_cultural_tradition = tradition_fp1_northern_stories
							has_cultural_tradition = tradition_fp2_strategy_gamers
							has_cultural_tradition = tradition_chanson_de_geste
						}
					}
				}
				modifier = {
					add = -25
					diplomacy <= 10
					learning <= 10
				}
				modifier = {
					add = 20
					OR = {
						diplomacy >= 15
						learning >= 15
					}
				}
				modifier = {
					add = 25
					culture = {
						OR = {
							has_cultural_tradition = tradition_esteemed_hospitality
							has_cultural_tradition = tradition_forbearing
							has_cultural_tradition = tradition_life_is_just_a_joke
							has_cultural_tradition = tradition_chivalry
							has_cultural_tradition = tradition_philosopher_culture
							has_cultural_tradition = tradition_music_theory
							has_cultural_tradition = tradition_things
						}
					}
				}
			}
			5 = {
				add_trait_xp = {
					trait = tourney_participant
					track = wit
					value = 50
				}
				modifier = {
					add = 35
					culture = {
						OR = {
							has_cultural_tradition = tradition_astute_diplomats
							has_cultural_tradition = tradition_welcoming
							has_cultural_parameter = poet_trait_gives_bonuses
							has_cultural_tradition = tradition_storytellers
							has_cultural_tradition = tradition_fp1_northern_stories
							has_cultural_tradition = tradition_fp2_strategy_gamers
							has_cultural_tradition = tradition_chanson_de_geste
						}
					}
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_esteemed_hospitality
							has_cultural_tradition = tradition_forbearing
							has_cultural_tradition = tradition_life_is_just_a_joke
							has_cultural_tradition = tradition_chivalry
							has_cultural_tradition = tradition_philosopher_culture
							has_cultural_tradition = tradition_music_theory
							has_cultural_tradition = tradition_things
						}
					}
				}
				modifier = {
					add = -25
					diplomacy <= 10
					learning <= 10
				}
				modifier = {
					add = 20
					OR = {
						diplomacy >= 15
						learning >= 15
					}
				}
			}
			5 = {
				add_trait_xp = {
					trait = tourney_participant
					track = wit
					value = 80
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_astute_diplomats
							has_cultural_tradition = tradition_welcoming
							has_cultural_parameter = poet_trait_gives_bonuses
							has_cultural_tradition = tradition_storytellers
							has_cultural_tradition = tradition_fp1_northern_stories
							has_cultural_tradition = tradition_fp2_strategy_gamers
							has_cultural_tradition = tradition_chanson_de_geste
						}
					}
				}
				modifier = {
					add = 10
					culture = {
						OR = {
							has_cultural_tradition = tradition_esteemed_hospitality
							has_cultural_tradition = tradition_forbearing
							has_cultural_tradition = tradition_life_is_just_a_joke
							has_cultural_tradition = tradition_chivalry
							has_cultural_tradition = tradition_philosopher_culture
							has_cultural_tradition = tradition_music_theory
							has_cultural_tradition = tradition_things
						}
					}
				}
				modifier = {
					add = -25
					diplomacy <= 10
					learning <= 10
				}
				modifier = {
					add = 20
					OR = {
						diplomacy >= 15
						learning >= 15
					}
				}
			}
			# BOW XP
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = bow
					value = 20
				}
				modifier = {
					add = 45
					culture = {
						OR = {
							has_cultural_tradition = tradition_adaptive_skirmishing
							has_cultural_tradition = tradition_hunters
							has_cultural_tradition = tradition_bush_hunting
							has_cultural_tradition = tradition_forest_wardens
							has_cultural_tradition = tradition_horse_lords
							has_cultural_tradition = tradition_land_of_the_bow
							has_cultural_tradition = tradition_longbow_competitions
						}
					}
				}
				modifier = {
					add = 25
					culture = {
						OR = {
							has_cultural_tradition = tradition_forest_folk
							has_cultural_tradition = tradition_jungle_dwellers
							has_cultural_tradition = tradition_polders
							has_cultural_tradition = tradition_fp1_coastal_warriors
							has_cultural_tradition = tradition_formation_fighting
							has_cultural_tradition = tradition_hit_and_run
							has_cultural_tradition = tradition_sacred_hunts
							has_cultural_tradition = tradition_mobile_guards
							has_cultural_tradition = tradition_mountain_herding
							has_cultural_tradition = tradition_horn_mountain_skirmishing
							has_cultural_tradition = tradition_upland_skirmishing
						}
					}
				}
				modifier = {
					factor = 0
					OR = {
						has_trait = blind
						has_trait = maimed
					}
				}
				modifier = {
					add = 15
					has_trait = lifestyle_hunter
				}
			}
			5 = {
				add_trait_xp = {
					trait = tourney_participant
					track = bow
					value = 50
				}
				modifier = {
					add = 35
					culture = {
						OR = {
							has_cultural_tradition = tradition_adaptive_skirmishing
							has_cultural_tradition = tradition_hunters
							has_cultural_tradition = tradition_bush_hunting
							has_cultural_tradition = tradition_forest_wardens
							has_cultural_tradition = tradition_horse_lords
							has_cultural_tradition = tradition_land_of_the_bow
						}
					}
				}
				modifier = {
					add = 15
					culture = {
						OR = {
							has_cultural_tradition = tradition_forest_folk
							has_cultural_tradition = tradition_jungle_dwellers
							has_cultural_tradition = tradition_polders
							has_cultural_tradition = tradition_fp1_coastal_warriors
							has_cultural_tradition = tradition_formation_fighting
							has_cultural_tradition = tradition_hit_and_run
							has_cultural_tradition = tradition_sacred_hunts
							has_cultural_tradition = tradition_mobile_guards
							has_cultural_tradition = tradition_mountain_herding
							has_cultural_tradition = tradition_horn_mountain_skirmishing
							has_cultural_tradition = tradition_upland_skirmishing
							has_cultural_tradition = tradition_longbow_competitions
						}
					}
				}
				modifier = {
					add = 15
					has_trait = lifestyle_hunter
				}
				modifier = {
					factor = 0
					OR = {
						has_trait = blind
						has_trait = maimed
					}
				}
			}
			5 = {
				add_trait_xp = {
					trait = tourney_participant
					track = bow
					value = 80
				}
				modifier = {
					add = 20
					culture = {
						OR = {
							has_cultural_tradition = tradition_adaptive_skirmishing
							has_cultural_tradition = tradition_hunters
							has_cultural_tradition = tradition_bush_hunting
							has_cultural_tradition = tradition_forest_wardens
							has_cultural_tradition = tradition_horse_lords
							has_cultural_tradition = tradition_land_of_the_bow
						}
					}
				}
				modifier = {
					add = 10
					culture = {
						OR = {
							has_cultural_tradition = tradition_forest_folk
							has_cultural_tradition = tradition_jungle_dwellers
							has_cultural_tradition = tradition_polders
							has_cultural_tradition = tradition_fp1_coastal_warriors
							has_cultural_tradition = tradition_formation_fighting
							has_cultural_tradition = tradition_hit_and_run
							has_cultural_tradition = tradition_sacred_hunts
							has_cultural_tradition = tradition_mobile_guards
							has_cultural_tradition = tradition_mountain_herding
							has_cultural_tradition = tradition_horn_mountain_skirmishing
							has_cultural_tradition = tradition_upland_skirmishing
							has_cultural_tradition = tradition_longbow_competitions
						}
					}
				}
				modifier = {
					add = 15
					has_trait = lifestyle_hunter
				}
				modifier = {
					factor = 0
					OR = {
						has_trait = blind
						has_trait = maimed
					}
				}
			}
		}
		random_list = {
			60 = { }
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = foot
					value = 20
				}
			}
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = bow
					value = 20
				}
			}
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = horse
					value = 20
				}
			}
			10 = {
				add_trait_xp = {
					trait = tourney_participant
					track = wit
					value = 20
				}
			}
		}
	}
}

versus_contest_round_outcome_tooltip_effect = {
	if = {
        limit = { scope:activity.var:contest_versus_progress = 2 }
        custom_description = {
			text = tournament_contest_round_win_tt
			subject = $WINNER$
			object = $LOSER$
		}
    }
    else = {
       	custom_description = {
			text = tournament_contest_round_tt
			subject = $WINNER$
			object = $LOSER$
		}
    }
}

versus_contest_disqualification_tooltip_effect = {
	if = {
		limit = { scope:activity.var:contest_versus_progress = 2 }
		custom_description = {
			text = tournament_contest_disqualify_win_tt
			subject = $WINNER$
			object = $DISQUALIFIED$
		}
	}
	else = {
		custom_description = {
			text = tournament_contest_disqualify_round_tt
			subject = $WINNER$
			object = $DISQUALIFIED$
		}
	}
}

versus_contest_resignation_tooltip_effect = {
	if = {
		limit = { scope:activity.var:contest_versus_progress = 2 }
		custom_description = {
			text = tournament_contest_resign_win_tt
			subject = $WINNER$
			object = $RESIGNER$
		}
	}
	else = {
		custom_description = {
			text = tournament_contest_resign_round_tt
			subject = $WINNER$
			object = $RESIGNER$
		}
	}
}

versus_force_concession_effect = {
	reverse_add_opinion = {
		target = scope:versus_contestant
		modifier = tournament_bribed_replacement_opinion
		opinion = -10
	}
	scope:versus_contestant = {
		remove_variable = progress_to_victory
		remove_variable = contest_aptitude
	}
	scope:activity = {
		switch = {
			trigger = var:contest_versus_progress
			0 = {
				add_to_guest_subset = { name = semi_finalist target = root }
			}
			1 = {
				add_to_guest_subset = { name = finalist target = root }
			}
			2 = {
				add_to_guest_subset = { name = winner target = root }
			}
		}
	}
	versus_contest_resignation_tooltip_effect = {
		RESIGNER = scope:versus_contestant
		WINNER = root
	}
	stress_impact = {
		just = miniscule_stress_impact_gain
		humble = miniscule_stress_impact_gain
		content = miniscule_stress_impact_gain
		diligent = miniscule_stress_impact_gain
	}
}

little_william_marshal_achievement_contest_end_effect = {
	if = {
		limit = {
			exists = global_var:started_little_william_marshal_achievement
			scope:activity = {
				any_attending_character = {
					is_ai = no
				}
			}
		}
		# Increment overall contest tally.
		change_global_variable = {
			name = started_little_william_marshal_achievement
			add = 1
		}
	}
}

little_william_marshal_achievement_contest_end_winner_effect = {
	if = {
		limit = {
			exists = global_var:started_little_william_marshal_achievement
			is_ai = no
		}
		change_global_variable = {
			name = little_william_marshal_achievement_tally
			add = 1
		}
	}
}
