﻿chariot_race_trigger = {
	involved_activity.activity_host.culture = {
		OR = {
			AND = {
				has_cultural_tradition = tradition_ep3_roman_ceremonies
				NOT = { has_cultural_era_or_later = culture_era_late_medieval }
			}
			this = culture:roman
		}
	}
}

activity_is_valid_tournament_contestant_training_decision = {
	is_alive = yes
	is_adult = yes
	is_imprisoned = no
	# Exclude clergy who don't fight
	trigger_if = {
		limit = { is_clergy = yes }
		faith = { has_doctrine_parameter = clergy_can_fight }
	}
}

activity_is_competing_trigger = {
	activity_is_valid_tournament_contestant = yes
	is_in_guest_subset = { name = contestant }
	exists = var:progress_to_victory # Ensure they have a PtV as a valid contestant
}

activity_is_valid_tournament_contestant = {
	involved_activity ?= { has_activity_type = activity_tournament }
	is_alive = yes
	is_adult = yes
	is_imprisoned = no
	# Exclude clergy who don't fight
	trigger_if = {
		limit = { is_clergy = yes }
		OR = {
			faith = { has_doctrine_parameter = clergy_can_fight }
			culture = { has_cultural_parameter = culture_clergy_can_fight }
		}
	}
	# Has not opted out
	NOR = {
		has_character_flag = tournament_not_competing
		is_in_guest_subset = { name = spectator }
	}
	trigger_if = {
		limit = {
			involved_activity = {
				NOR = {
					has_current_phase = tournament_phase_horse_race
					has_current_phase = tournament_phase_board_game
					has_current_phase = tournament_phase_recital
				}
			}
		}
		# Only martial sex competes
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = involved_activity.activity_host }
		NOR = {
			has_trait = wounded_2
			has_trait = wounded_3
		}
	}
	# Mulan is clever enough not to reveal themselves
	trigger_if = {
		limit = {
			involved_activity = { has_current_phase = tournament_phase_wrestling }
		}
		NOT = { has_character_modifier = ignores_gender_army_rules }
	}
	NOT = {
		has_trait = incapable
	}
	trigger_if = {
		limit = {
			NOT = {
				involved_activity = { has_current_phase = tournament_phase_horse_race }
			}
		}
		NOR = {
			has_trait = blind
			has_trait = infirm
		}
	}
	trigger_if = { # Blind and maimed people don't do archery
	    limit = {
	        NOT = {
	            involved_activity = { has_current_phase = tournament_phase_archery }
	        }
	    }
	    NOR = {
	        has_trait = blind
	        has_trait = maimed
	    }
	} 
}

activity_tournament_contest_non_martial_gender_allowed = {
	involved_activity ?= {
		OR = {
			has_current_phase = tournament_phase_horse_race
			has_current_phase = tournament_phase_board_game
			has_current_phase = tournament_phase_recital
		}
	}
}

activity_tournament_contest_martial_gender_only = {
	involved_activity ?= {
		OR = {
			has_current_phase = tournament_phase_duel
			has_current_phase = tournament_phase_wrestling
			has_current_phase = tournament_phase_melee
			has_current_phase = tournament_phase_archery
			has_current_phase = tournament_phase_joust
		}
	}
}

activity_has_reason_to_cheer_character_trigger = {
	exists = $CHAR$
	OR = {
		this = $CHAR$
		AND = {
			opinion = {
				target = $CHAR$
				value >= 25
			}
			OR = {
				is_close_family_of = $CHAR$
				is_consort_of = $CHAR$
				has_any_good_relationship_with_character_trigger = { CHARACTER = $CHAR$ }
			}
		}
	}
}

activity_has_reason_to_anger_character_trigger = {
	exists = $CHAR$
	NOT = { this = $CHAR$ }
	opinion = {
		target = $CHAR$
		value < 25
	}
	has_any_bad_relationship_with_character_trigger = { CHARACTER = $CHAR$ }
}

tournament_contest_wager_trigger = {
	AND = {
		has_current_phase = tournament_phase_$CONTEST$
		NOT = {
			any_guest_subset = {
				name = winner
				phase = tournament_phase_$CONTEST$
			}
		}
	}
	any_guest_subset_current_phase = {
		name = qualified
		count >= 3
	}
	trigger_if = {
		limit = { exists = var:contest_versus_progress }
		var:contest_versus_progress <= 3
	}
}

activity_tournament_armor_trigger = {
	involved_activity ?= { has_activity_type = activity_tournament }
	OR = {
		AND = {
			is_in_guest_subset = { name = qualified }
			involved_activity = {
				OR = {
					has_current_phase = tournament_phase_melee
					has_current_phase = tournament_phase_duel
					has_current_phase = tournament_phase_joust
				}
			}
		}  
		AND = {
			has_character_modifier = ignores_gender_army_rules
			is_in_guest_subset = { name = qualified }
		}
	}
}

activity_tournament_shirtless_trigger = {
	involved_activity ?= {
		has_activity_type = activity_tournament
		has_current_phase = tournament_phase_wrestling
	}
	is_in_guest_subset = { name = qualified }
}

activity_tournament_valid_winner_trigger = {
	is_alive = yes
	exists = var:contest_aptitude
	exists = var:progress_to_victory
}

activity_adult_available_or_attending_same_activity_trigger = {
	is_adult = yes
	OR = {
		is_available = yes
		involved_activity ?= scope:activity

	}
}

activity_tournament_versus_contest_trigger = {
	OR = {
		has_current_phase = tournament_phase_joust
		has_current_phase = tournament_phase_wrestling
		has_current_phase = tournament_phase_duel
		has_current_phase = tournament_phase_board_game
	}
}

activity_tournament_team_contest_trigger = {
	scope:activity = { has_current_phase = tournament_phase_melee }
}

activity_tournament_turn_contest_trigger = {
	scope:activity = {
		OR = {
			has_current_phase = tournament_phase_archery
			has_current_phase = tournament_phase_recital
		}
	}
}

activity_tournament_mind_contest_trigger = {
	scope:activity = {
		OR = {
			has_current_phase = tournament_phase_recital
			has_current_phase = tournament_phase_board_game
		}
	}
}

activity_tournament_prowess_contest_trigger = {
	scope:activity = {
		OR = {
			has_current_phase = tournament_phase_melee
			has_current_phase = tournament_phase_joust
			has_current_phase = tournament_phase_duel
			has_current_phase = tournament_phase_wrestling
		}
	}
}

activity_tournament_armor_contest_trigger = {
	scope:activity = {
		OR = {
			has_current_phase = tournament_phase_melee
			has_current_phase = tournament_phase_joust
			has_current_phase = tournament_phase_duel
		}
	}
}

activity_tournament_horse_contest_trigger = {
	scope:activity = {
		OR = {
			has_current_phase = tournament_phase_horse_race
			has_current_phase = tournament_phase_joust
			has_current_phase = tournament_phase_melee
		}
	}
}

activity_tournament_race_contest_trigger = {
	scope:activity = { has_current_phase = tournament_phase_horse_race }
}

activity_tournament_same_team_trigger = {
	scope:activity = {
		OR = {
			AND = {
				any_guest_subset = {
					name = team_alpha
					this = $FIRST$
				}
				any_guest_subset = {
					name = team_alpha
					this = $SECOND$
				}
			}
			AND = {
				any_guest_subset = {
					name = team_beta
					this = $FIRST$
				}
				any_guest_subset = {
					name = team_beta
					this = $SECOND$
				}
			}
		}
	}
}

tournament_character_any_contest_winner_trigger = {
	involved_activity = {
		any_guest_subset = {
			name = winner
			this = $CHAR$
			phase = tournament_phase_$CONTEST$
		}
	}
}

contest_versus_player_vs_ai_trigger = {
	calc_true_if = {
		amount = 1
		scope:versus_matchee = { is_ai = no }
		scope:versus_matcher = { is_ai = no }
	}
	calc_true_if = {
		amount = 1
		scope:versus_matchee = { is_ai = yes }
		scope:versus_matcher = { is_ai = yes }
	}
	NOR = {
		scope:versus_matchee = { has_character_flag = resigned_contest_flag }
		scope:versus_matcher = { has_character_flag = resigned_contest_flag }
	}
}

contest_event_general_contestant_valid_trigger = {
	is_available_quick = {
		ai = yes
		alive = yes
	}
	exists = var:contest_aptitude
	exists = var:progress_to_victory
	this != root
}

has_ongoing_grand_activity_trigger = {
	trigger_if = {
		limit = { exists = involved_activity }
		involved_activity = {
			OR = {
				has_activity_type = activity_tournament
				has_activity_type = activity_tour
				has_activity_type = activity_wedding
			}
		}
	}
	trigger_else = {
		always = no
	}
}

is_grand_activity_trigger = {
	OR = {
		has_activity_type = activity_tournament
		has_activity_type = activity_tour
		has_activity_type = activity_wedding
	}
}

tournament_contest_event_trigger = {
	is_ai = no
	is_in_guest_subset = { name = qualified }
	exists = var:progress_to_victory
	NOT = { has_character_flag = busy_in_contest_event }
}

tournament_contest_versus_event_trigger = {
	is_ai = no
	exists = var:progress_to_victory
	is_in_guest_subset = { name = qualified }
	NOT = { has_character_flag = resigned_contest_flag }
	save_temporary_scope_as = versus_event_scope
	# Only fire events if the bout has not been pre-decided
	involved_activity = {
		switch = {
			trigger = has_current_phase
			tournament_phase_joust = {
				scope:versus_event_scope = {
					switch = {
						trigger = involved_activity.var:contest_versus_progress
						0 = { exists = var:contest_qualified_match_joust.var:progress_to_victory }
						1 = { exists = var:contest_semi_finalist_match_joust.var:progress_to_victory }
						2 = { exists = var:contest_finalist_match_joust.var:progress_to_victory }
					}
				}
			}
			tournament_phase_duel = {
				scope:versus_event_scope = {
					switch = {
						trigger = involved_activity.var:contest_versus_progress
						0 = { exists = var:contest_qualified_match_duel.var:progress_to_victory }
						1 = { exists = var:contest_semi_finalist_match_duel.var:progress_to_victory }
						2 = { exists = var:contest_finalist_match_duel.var:progress_to_victory }
					}
				}
			}
			tournament_phase_wrestling = {
				scope:versus_event_scope = {
					switch = {
						trigger = involved_activity.var:contest_versus_progress
						0 = { exists = var:contest_qualified_match_wrestling.var:progress_to_victory }
						1 = { exists = var:contest_semi_finalist_match_wrestling.var:progress_to_victory }
						2 = { exists = var:contest_finalist_match_wrestling.var:progress_to_victory }
					}
				}
			}
			tournament_phase_board_game = {
				scope:versus_event_scope = {
					switch = {
						trigger = involved_activity.var:contest_versus_progress
						0 = { exists = var:contest_qualified_match_board_game.var:progress_to_victory }
						1 = { exists = var:contest_semi_finalist_match_board_game.var:progress_to_victory }
						2 = { exists = var:contest_finalist_match_board_game.var:progress_to_victory }
					}
				}
			}
		}
	}
	
}

tournament_contest_can_compete_trigger = {
	exists = var:progress_to_victory
	NOT = {
		is_in_guest_subset = { name = qualified }
	}
	activity_is_valid_tournament_contestant = yes
}

tournament_contest_can_compete_race_trigger = {
	is_adult = yes
	exists = var:progress_to_victory
	OR = {
		is_lowborn = no
		AND = {
			is_playable_character = yes
			primary_title.tier >= tier_county
		}
		gold >= major_gold_value
	}
	NOT = {
		is_in_guest_subset = { name = qualified }
	}
}

contest_team_enemy_member_exists_trigger = {
	scope:activity = {
		OR = {
			is_in_guest_subset = { name = team_alpha }
			is_in_guest_subset = { name = team_beta }
		}
		trigger_if = {
			limit = {
				is_in_guest_subset = { name = team_alpha }
			}
			any_guest_subset_current_phase = {
				name = team_beta
				is_available_quick = {
					ai = yes
					alive = yes
				}
			}
		}
		trigger_else = {
			any_guest_subset_current_phase = {
				name = team_alpha
				is_available_quick = {
					ai = yes
					alive = yes
				}
			}
		}
	}
}

tournament_pertinent_contestant_trigger = {
	OR = {
		root.var:contest_murder_attempt ?= this
		has_any_good_relationship_with_character_trigger = { CHARACTER = root }
		has_any_bad_relationship_with_character_trigger = { CHARACTER = root }
		is_consort_of = root
		is_close_family_of = root
		AND = {
			has_court_position = champion_court_position
			liege = root
		}
		is_knight_of = root
		AND = {
			exists = root.house
			house ?= root.house
		}
		root.var:tournament_wager_target ?= this
	}
}

tournament_is_knightly_entourage_character_trigger = {
	is_knight = yes
	is_playable_character = no
	save_temporary_scope_as = char_temp
	involved_activity = {
		any_attending_character = { this = scope:char_temp.liege }
	}
}

would_want_to_attend_tournament = {
	trigger_if = {
		limit = { is_playable_character = yes }
		NOR = {
			government_has_flag = government_is_tribal
			government_has_flag = government_is_mercenary
			government_has_flag = government_is_holy_order
		}
	}
	trigger_else = { is_clergy = no }
	OR = {
		is_ai = no
		has_trait = tourney_participant
		has_trait = education_martial
		has_trait = education_martial_prowess
		has_trait = gregarious
		has_trait = brave
		prowess >= decent_skill_rating
		has_court_position = champion_court_position
		is_knight = yes
		number_of_commander_traits >= 1
		has_any_scripted_relation = scope:inviter
	}
}

is_available_ai_adult_locale_person = { #to prevent bishops from being anything else than temple priests
	is_available_ai_adult = yes
	has_no_particular_noble_roots_trigger = yes
	is_clergy = no
}
