﻿activity_local_examination = {

	notify_player_can_join_activity = yes

	is_shown = {
		government_allows = merit
		has_tgp_dlc_trigger = yes
		OR = {
			is_governor = yes
			#AGOT Disabled
			# has_title = title:e_minister_of_rites
			is_independent_ruler = yes
			#AGOT Modified
			# trigger_if = {
			# 	limit = {
			# 		is_ai = yes
			# 	}
			# 	NOT = { has_title = title:h_china }
			# }
			is_ai = yes
		}
	}

	can_start_showing_failures_only = {
		NOT = { is_activity_type_on_cooldown = activity_local_examination }
		is_available_adult = yes
		trigger_if = {
			limit = {
				is_independent_ruler = no
			}
			custom_tooltip = {
				text = activity_local_exam_not_passed_tt
				has_character_flag = passed_provincial_exam
			}
		}
	}

	is_valid = {
		scope:host = {
			is_physically_able = yes
			OR = {
				is_landed = yes
				#AGOT Disabled
				# has_title = title:e_minister_of_rites
			}
			government_allows = merit
		}
		# If no one shows up
		trigger_if = {
			limit = { is_current_phase_active = yes }
			has_attending_activity_guests = yes
		}
	}

	on_invalidated = {
		activity_type = { save_scope_as = activity_type }
		imperial_examination_clean_up_phase_variables_effect = yes
		scope:activity = {
			every_attending_character = {
				if = { # If we still have a short duration remaining on our appointment timeout, clear it
					limit = {
						appointment_timeout_days > 0
						appointment_timeout_days < 365
					}
					clear_appointment_timeout = yes
				}
			}
		}
		# Host becomes unlanded
		if = {
			limit = {
				scope:host = { is_landed = no }
			}
			every_attending_character = { trigger_event = activity_system.0320 }
		}
		if = { # No one shows up
			limit = {
				has_attending_activity_guests = no
			}
			activity_location = { save_scope_as = location }
			scope:host = { trigger_event = activity_system.0100 }
		}
	}

	on_host_death = {
		# Make the player heir the new host
		if = {
			limit = { exists = scope:host.player_heir }
			scope:host.player_heir = { save_scope_as = new_host }
		}
		# Otherwise pass to any successor
		else = {
			scope:host.primary_heir = { save_scope_as = new_host }
		}
		set_activity_host = scope:new_host
		scope:new_host = {
			trigger_event = { id = activity_system.0080 days = 1 }
		}
	}

	province_description = {
		triggered_desc = {
			trigger = { this = scope:host.capital_province }
			desc = activity_chariot_race_province_capital
		}
		# + / plus good
		triggered_desc = {
			trigger = { has_building_or_higher = examination_hall_01 }
			desc = activity_imperial_examination_province_examination_hall_desc
		}
	}

	# desc is only shown in debug AI watch window
	ai_will_do = {
		value = 100
		## Personality
		if = { # Is a bad confucian
			limit = {
				# Being diligent or ambitious should cancel out the effects of being lazy
				NOR = {
					has_trait = ambitious
					has_trait = diligent
				}
				has_trait = lazy
			}
			add = {
				value = -20
				desc = lazy
			}
		}
		if = { # Is a bad confucian
			limit = { has_trait = greedy }
			add = {
				value = -20
				desc = greedy
			}
		}
		if = { # Likes to socialize
			limit = { has_trait = gregarious }
			add = {
				value = 10
				desc = gregarious
			}
		}
		else_if = { # Does not like to socialize
			limit = { has_trait = shy }
			add = {
				value = -15
				desc = shy
			}
		}
		if = { # Really does not like socializing
			limit = { has_trait = reclusive }
			add = {
				value = -25
				desc = reclusive
			}
		}
		if = { # Is a good confucian
			limit = { has_trait = compassionate }
			add = {
				value = 10
				desc = compassionate
			}
		}
		if = { # Is a good confucian
			limit = { has_trait = just }
			add = {
				value = 10
				desc = just
			}
		}
		if = { # Is a good confucian
			limit = { has_trait = honest }
			add = {
				value = 10
				desc = honest
			}
		}
		#AGOT Disabled
		# if = { # Is a confucian scholar
		# 	limit = { has_trait = confucian_education }
		# 	add = {
		# 		value = 25
		# 		desc = confucian_education
		# 	}
		# }
		if = {
			limit = { has_character_flag = passed_metropolitan_exam }
			add = {
				value = 25
				desc = passed_metropolitan_exam
			}
		}
		if = {
			limit = { has_character_flag = passed_palace_exam }
			add = {
				value = 15
				desc = passed_palace_exam
			}
		}
		if = {
			limit = {
				OR = {
					has_trait = scholarly_court_1
					has_trait = scholarly_court_2
				}
			}
			add = {
				value = 25
				desc = trait_scholarly_court_2
			}
		}
		if = { # Low legitimacy
			limit = {
				legitimacy_level < 4
			}
			add = {
				value = 50
				desc = low_legitimacy
			}
		}

		# Movement
		#AGOT Disabled
		# if = {
		# 	limit = {
		# 		top_participant_group:dynastic_cycle ?= {
		# 			participant_group_type = conservative_movement
		# 		}
		# 	}
		# 	add = {
		# 		value = -20
		# 		desc = conservative_movement
		# 	}
		# }
		# if = {
		# 	limit = {
		# 		top_participant_group:dynastic_cycle ?= {
		# 			participant_group_type = advancement_movement
		# 		}
		# 	}
		# 	add = {
		# 		value = 20
		# 		desc = advancement_movement
		# 	}
		# }

		# Culture
		culture = {
			if = {
				limit = {
					has_cultural_tradition = tradition_legalistic
				}
				add = {
					value = 10
					desc = tradition_legalistic_name
				}
			}
			if = {
				limit = {
					has_cultural_tradition = tradition_astute_diplomats
				}
				add = {
					value = 10
					desc = tradition_astute_diplomats_name
				}
			}
			if = {
				limit = {
					has_cultural_tradition = tradition_language_scholars
				}
				add = {
					value = 10
					desc = tradition_language_scholars_name
				}
			}
			if = {
				limit = {
					has_cultural_parameter = poet_trait_gives_bonuses
				}
				add = {
					value = 5
					desc = tradition_poetry_name
				}
			}
			if = {
				limit = {
					has_cultural_tradition = tradition_philosopher_culture
				}
				add = {
					value = 25
					desc = tradition_philosopher_culture_name
				}
			}
		}

		#Religion
		#AGOT Disabled
		# if = {
		# 	limit = {
		# 		has_religion = religion:confucianism_religion
		# 	}
		# 	add = {
		# 		value = 25
		# 		desc = confucianism_religion
		# 	}
		# }

		# Plague
		if = {
			limit = {
				any_held_title = {
					tier = tier_county
			   		any_county_province = {
			   			any_province_epidemic = {
			   				count >= 1
			   				outbreak_intensity < apocalyptic
			   			}
					}
				}
				would_follow_social_distancing_value >= -1
			}
			add = {
				value = -60
				desc = "Plague in Domain"
			}
		}
		else_if = {
			limit = {
				any_realm_county = {
			 		any_county_province = {
			 			any_province_epidemic = {
			 				count >= 1
			 				outbreak_intensity < apocalyptic
			 			}
			 		}
			 	}
			 	would_follow_social_distancing_value >= -1
			}
			add = {
				value = -20
				desc = "Plague in Top Realm"
			}
		}
		if = {
			limit = {
				any_held_title = {
					tier = tier_county
			 		any_county_province = {
			 			any_province_epidemic = {
							count >= 1
							outbreak_intensity = apocalyptic
			 			}
					}
				}
			}
			add = {
				value = -200
				desc = "Apocalyptic Plague in Domain"
			}
		}
		else_if = {
			limit = {
				any_held_title = {
					tier = tier_county
			 		any_county_province = {
			 			any_province_epidemic = {
							count >= 1
							outbreak_intensity = apocalyptic
			 			}
			 		}
				}
			}
			add = {
				value = -40
				desc = "Apocalyptic Plague in Top Realm"
			}
		}

		#Special flags
		#AGOT Disabled
		# if = {
		# 	limit = {
		# 		is_ai = yes
		# 		has_character_flag = examinations_ai_override
		# 	}
		# 	add = 10000
		# }
		# if = {
		# 	limit = {
		# 		is_ai = yes
		# 		has_title = title:e_minister_of_rites
		# 	}
		# 	add = -20000
		# }
	}

	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 60
		kingdom = 60
		empire = 60
		hegemony = 0
	}

	ai_will_select_province = {
		# Base
		add = {
			value = 5
			desc = "base"
		}
		# Buildings
		if = {
			limit = {
				has_building_or_higher = examination_hall_01
			}
			add = {
				value = 1
				desc = activity_imperial_examination_province_examination_hall_desc
			}
		}
	}

	###################
	# PARAMETERS
	###################

	is_single_location = yes

	cooldown = { years = 3 }

	max_province_icons = 3

	max_route_deviation_mult = 3.0

	cost = {
		#Treasury for China
		treasury = {
			if = {
				limit = { tgp_examination_should_spend_treasury_trigger = yes }
				add = {
					add = {
						value = imperial_examination_actual_cost
						desc = imperial_examination_base_cost
					}
				}
			}
			if = {
				limit = {
					scope:province ?= { has_building_or_higher = examination_hall_01 }
				}
				add = {
					value = 0
					add = {
						value = imperial_examination_base_cost
						add = {
							value = imperial_examination_base_cost
							multiply = activity_cost_scale_by_era
							subtract = imperial_examination_base_cost
						}
					}
					multiply = imperial_examination_cost_discount_max_value
					desc = imperial_examination_cost_discount_examination_hall
				}
			}
			else = { value = 0 }
			# Some people get one free.
			if = {
				limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
				multiply = 0
			}
			#AGOT Disabled
			# if = {
			# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
			# 	multiply = 0
			# }
		}
		#Gold for the rest
		gold = {
			if = {
				limit = { tgp_examination_should_spend_treasury_trigger = no }
				add = {
					add = {
						value = imperial_examination_actual_cost
						desc = imperial_examination_actual_cost
					}
					if = {
						limit = {
							scope:province ?= { has_building_or_higher = examination_hall_01 }
						}
						add = {
							value = 0
							add = {
								value = imperial_examination_base_cost
								add = {
									value = imperial_examination_base_cost
									multiply = activity_cost_scale_by_era
									subtract = imperial_examination_base_cost
								}
							}
							multiply = imperial_examination_cost_discount_max_value
							desc = imperial_examination_cost_discount_examination_hall
						}
					}
				}
			}
			else = { value = 0 }
			# Some people get one free.
			if = {
				limit = { factor_zero_if_entitled_to_freebie_activity_trigger = yes }
				multiply = 0
			}
			#AGOT Disabled
			# if = { # Free of charge when supported by the minister of rites
			# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
			# 	multiply = {
			# 		value = 0
			# 		desc = minister_of_rites_discount_desc
			# 	}
			# }
		}
	}

	# Used to show a rough minimum or expected cost at all to plan this
	ui_predicted_cost = {
		#Treasury for China
		treasury = {
			if = {
				limit = { tgp_examination_should_spend_treasury_trigger = yes }
				#AGOT Modified
				# if = {
				# 	limit = {
				# 		OR = {
				# 			AND = {
				# 				is_ai = yes
				# 				has_character_flag = examinations_ai_override
				# 			}
				# 			has_character_flag = celestial_examination_support_cost_reduction
				# 		}
				# 	}
				# 	value = 0
				# }
				# else = { add = imperial_examination_predicted_cost }
				add = imperial_examination_predicted_cost
			}
			else = { value = 0 }
		}
		# All costs are balanced on County/Early Era starting point
		gold = {
			if = {
				limit = { tgp_examination_should_spend_treasury_trigger = no }
				#AGOT Modified
				# if = {
				# 	limit = {
				# 		OR = {
				# 			AND = {
				# 				is_ai = yes
				# 				has_character_flag = examinations_ai_override
				# 			}
				# 			has_character_flag = celestial_examination_support_cost_reduction
				# 		}
				# 	}
				# 	value = 0
				# }
				# else = { add = imperial_examination_predicted_cost }
				add = imperial_examination_predicted_cost
			}
			else = { value = 0 }
		}
	}

	conclusion_description = {
		desc = imperial_examination_conclusion.intro
		desc = {
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_extreme_grade_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.passing_grade.extreme
			}
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_high_grade_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.passing_grade.high
			}
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_mediocre_grade_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.passing_grade.mediocre
			}
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_low_grade_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.passing_grade.low
			}
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_imperial_exams_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.breadth.imperial
			}
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_grand_exams_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.breadth.grand
			}
			triggered_desc = {
				trigger = {
					this = scope:host
					scope:activity = { activity_has_palace_exams_trigger = yes }
				}
				desc = imperial_examination_conclusion.intro.breadth.palace
			}
			triggered_desc = {
				trigger = {
					this = scope:host
				}
				desc = imperial_examination_conclusion.general
			}
			triggered_desc = {
				trigger = {
					has_character_flag = exam_taker
					OR = {
						has_variable = been_caught_cheating_longterm
						is_in_guest_subset = { name = failees }
					}
				}
				desc = imperial_examination_conclusion.entrant_failee
			}
			triggered_desc = {
				trigger = {
					has_character_flag = exam_taker
					is_in_guest_subset = { name = entrants }
				}
				desc = local_examination_conclusion.entrant_passed
			}
		}
	}

	###################
	# OPTIONS
	###################

	options = {
		#Choose the amount of entrants you want
		#Choose to add additional entrants - each size adding a multiplier to final legitimacy and rewards, but also increasing cost.
		imperial_examination_breadth = {
			#50 guests
			imperial_examination_breadth_exclusive = {

				cost = {
					#Treasury for China
					treasury = {
						if = {
							limit = { tgp_examination_should_spend_treasury_trigger = yes }
							add = imperial_examination_breadth_exclusive_value
						}
						else = { value = 0 }
						# Free of charge when supported by the minister of rites
						#AGOT Disabled
						# if = {
						# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
						# 	multiply = {
						# 		value = 0
						# 		desc = minister_of_rites_discount_desc
						# 	}
						# }
					}
					gold = {
						if = {
							limit = { tgp_examination_should_spend_treasury_trigger = no }
							add = imperial_examination_breadth_exclusive_value
						}
						else = { value = 0 }
						# AI gets it for free if a Movement demanded it, to ensure they do it.
						#AGOT Disabled
						# if = {
						# 	limit = {
						# 		is_ai = yes
						# 		has_character_flag = examinations_ai_override
						# 	}
						# 	multiply = 0
						# }
						# Free of charge when supported by the minister of rites
						#AGOT Disabled
						# if = {
						# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
						# 	multiply = {
						# 		value = 0
						# 		desc = minister_of_rites_discount_desc
						# 	}
						# }
					}
				}
				ai_will_do = {
					value = 100
					# Personality
					if = { # This is the cheap option
						limit = { has_trait = greedy }
						add = {
							value = 20
							desc = greedy
						}
					}
					# This is the elitist option
					add = {
						value = ai_honor
						multiply = -0.25
						desc = "ai_honor"
					}
					add = {
						value = ai_vengefulness
						multiply = 0.15
						desc = "ai_vengefulness"
					}
					add = {
						value = ai_boldness
						multiply = 0.15
						desc = "ai_boldness"
					}
					if = { # Does not like to socialize
						limit = { has_trait = shy }
						add = {
							value = -15
							desc = shy
						}
					}
					if = { # Really does not like socializing
						limit = { has_trait = reclusive }
						add = {
							value = -25
							desc = reclusive
						}
					}
				}
			}
			#100 guests
			imperial_examination_breadth_restricted = {
				default = yes

				cost = {
					#Treasury for China
					treasury = {
						if = {
							limit = { tgp_examination_should_spend_treasury_trigger = yes }
							add = imperial_examination_breadth_restricted_value
						}
						else = { value = 0 }
						# AI gets it for free if a Movement demanded it, to ensure they do it.
						#AGOT Disabled
						# if = {
						# 	limit = {
						# 		is_ai = yes
						# 		has_character_flag = examinations_ai_override
						# 	}
						# 	multiply = 0
						# }
						# Free of charge when supported by the minister of rites
						#AGOT Disabled
						# if = {
						# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
						# 	multiply = {
						# 		value = 0
						# 		desc = minister_of_rites_discount_desc
						# 	}
						# }
					}
					gold = {
						if = {
							limit = { tgp_examination_should_spend_treasury_trigger = no }
							add = imperial_examination_breadth_restricted_value
						}
						else = { value = 0 }
						# AI gets it for free if a Movement demanded it, to ensure they do it.
						#AGOT Disabled
						# if = {
						# 	limit = {
						# 		is_ai = yes
						# 		has_character_flag = examinations_ai_override
						# 	}
						# 	multiply = 0
						# }
						# Free of charge when supported by the minister of rites
						#AGOT Disabled
						# if = {
						# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
						# 	multiply = {
						# 		value = 0
						# 		desc = minister_of_rites_discount_desc
						# 	}
						# }
					}
				}
				ai_will_do = {
					value = 100
					if = { # Lazy is likely to go for default option
						limit = { has_trait = lazy }
						add = {
							value = 20
							desc = lazy
						}
					}
			        add = { # In many ways, this is the most safe option
			        	value = ai_rationality
			        	multiply = 0.25
			        	desc = "ai_rationality"
			        }
				}
			}
			#200 guests
			imperial_examination_breadth_open = {

				cost = {
					#Treasury for China
					treasury = {
						if = {
							limit = { tgp_examination_should_spend_treasury_trigger = yes }
							add = imperial_examination_breadth_open_value
						}
						else = { value = 0 }
						# AI gets it for free if a Movement demanded it, to ensure they do it.
						#AGOT Disabled
						# if = {
						# 	limit = {
						# 		is_ai = yes
						# 		has_character_flag = examinations_ai_override
						# 	}
						# 	multiply = 0
						# }
						# Free of charge when supported by the minister of rites
						#AGOT Disabled
						# if = {
						# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
						# 	multiply = {
						# 		value = 0
						# 		desc = minister_of_rites_discount_desc
						# 	}
						# }
					}
					gold = {
						if = {
							limit = { tgp_examination_should_spend_treasury_trigger = no }
							add = imperial_examination_breadth_open_value
						}
						else = { value = 0 }
						# AI gets it for free if a Movement demanded it, to ensure they do it.
						#AGOT Disabled
						# if = {
						# 	limit = {
						# 		is_ai = yes
						# 		has_character_flag = examinations_ai_override
						# 	}
						# 	multiply = 0
						# }
						# Free of charge when supported by the minister of rites
						#AGOT Disabled
						# if = {
						# 	limit = { has_character_flag = celestial_examination_support_cost_reduction }
						# 	multiply = {
						# 		value = 0
						# 		desc = minister_of_rites_discount_desc
						# 	}
						# }
					}
				}
				ai_will_do = {
					value = 150
			        add = { # Wants to socialize and be seen by as many people as possible
			        	value = ai_sociability
			        	multiply = 0.25
			        	desc = "ai_sociability"
			        }
					if = { # Is a good confucian
						limit = { has_trait = compassionate }
						add = {
							value = 10
							desc = compassionate
						}
					}
					if = { # Is a good confucian
						limit = { has_trait = just }
						add = {
							value = 10
							desc = just
						}
					}
					if = { # Is a good confucian
						limit = { has_trait = honest }
						add = {
							value = 10
							desc = honest
						}
					}
				}
			}
		}
		imperial_examination_passing_grade = {
			imperial_examination_passing_grade_low = {
				#Don't really need a cost, you pay in decreased legitimacy reward
				ai_will_do = {
					value = 100
			        add = { # The more governors the better!
			        	value = ai_sociability
			        	multiply = 0.25
			        	desc = "ai_sociability"
			        }
					if = { # Lazy does not wish to bother with difficult tests
						limit = { has_trait = lazy }
						add = {
							value = 20
							desc = lazy
						}
					}
					if = { # Who has the patience to come up with difficult tests?!
						limit = { has_trait = impatient }
						add = {
							value = 20
							desc = impatient
						}
					}
					if = {
						limit = {
							OR = {
								has_trait = education_diplomacy_1
								has_trait = education_martial_1
								has_trait = education_stewardship_1
								has_trait = education_intrigue_1
								has_trait = education_learning_1
							}
						}
						add = {
							value = 50
							desc = "low education"
						}
					}
				}
			}
			imperial_examination_passing_grade_mediocre = {
				#Don't really need a cost, you pay in decreased legitimacy reward
				ai_will_do = {
					value = 100
					if = { # Let's be chill about this
						limit = { has_trait = content }
						add = {
							value = 20
							desc = content
						}
					}
					if = { # I really don't expect _that_ much
						limit = { has_trait = humble }
						add = {
							value = 10
							desc = humble
						}
					}
					if = { # I don't want to cause too much fuss
						limit = { has_trait = craven }
						add = {
							value = 10
							desc = craven
						}
					}
					if = { # I can't really make up my mind
						limit = { has_trait = fickle }
						add = {
							value = 10
							desc = fickle
						}
					}
					if = {
						limit = {
							OR = {
								has_trait = education_diplomacy_2
								has_trait = education_martial_2
								has_trait = education_stewardship_2
								has_trait = education_intrigue_2
								has_trait = education_learning_2
							}
						}
						add = {
							value = 50
							desc = "mediocre education"
						}
					}
				}
			}
			imperial_examination_passing_grade_high = {
				default = yes
				#Don't really need a cost, you pay in decreased legitimacy reward
				ai_will_do = {
					value = 100
			        add = { # This is about fair and equal opportunity
			        	value = ai_honor
			        	multiply = 0.25
			        	desc = "ai_honor"
			        }
			        add = {
			        	value = ai_compassion
			        	multiply = 0.25
			        	desc = "ai_compassion"
			        }
			        add = { # Safe is good
			        	value = ai_rationality
			        	multiply = 0.25
			        	desc = "ai_rationality"
			        }
					if = { # Is a good confucian
						limit = { has_trait = compassionate }
						add = {
							value = 10
							desc = compassionate
						}
					}
					if = { # Is a good confucian
						limit = { has_trait = just }
						add = {
							value = 10
							desc = just
						}
					}
					if = { # Is a good confucian
						limit = { has_trait = honest }
						add = {
							value = 10
							desc = honest
						}
					}
					if = {
						limit = {
							OR = {
								has_trait = education_diplomacy_3
								has_trait = education_martial_3
								has_trait = education_stewardship_3
								has_trait = education_intrigue_3
								has_trait = education_learning_3
							}
						}
						add = {
							value = 50
							desc = "high education"
						}
					}
				}
			}
			imperial_examination_passing_grade_extreme = {
				# High Legitimacy reward at cost of fewer prospective governors
				ai_will_do = {
					value = 100
					if = {
						limit = {
							ai_energy > 0
						}
						add = {
							add = ai_energy
							desc = "ai_energy"
						}
					}
					add = { # Only the best for me
						value = ai_boldness
						multiply = 0.15
						desc = "ai_boldness"
					}
					if = { # This is about me
						limit = { has_trait = ambitious }
						add = {
							value = 20
							desc = ambitious
						}
					}
					if = { # My way or the highway
						limit = { has_trait = stubborn }
						add = {
							value = 20
							desc = stubborn
						}
					}
					if = { # Surely it can't be that hard
						limit = { has_trait = arrogant }
						add = {
							value = 20
							desc = arrogant
						}
					}
					if = { # I really need the legitimacy
						limit = { legitimacy_level < 4 }
						add = 20
						desc = low_legitimacy
					}
					if = {
						limit = {
							OR = {
								has_trait = education_diplomacy_4
								has_trait = education_martial_4
								has_trait = education_stewardship_4
								has_trait = education_intrigue_4
								has_trait = education_learning_4
								has_trait = education_diplomacy_5
								has_trait = education_martial_5
								has_trait = education_stewardship_5
								has_trait = education_intrigue_5
								has_trait = education_learning_5
								has_trait = shrewd
								has_trait = intellect_good
							}
						}
						add = {
							value = 50
							desc = "wants to be the very best"
						}
					}
				}
			}
		}
	}

	###################
	# PHASES
	###################

	phases = {
		#Assembly Phase
		local_examination_phase_assembly = {
			order = 1
			is_predefined = yes

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					imperial_examination_set_up_examiners_effect = { HOST = scope:host }
					#Add all the examiners to guest subsets so they don't become Entrants
					imperial_examination_gather_examiners_effect = yes
					#Add all landed characters as guests
					imperial_examination_gather_guests_effect = yes
					#Add all the Entrants to guest subsets so we can keep track of them
					local_examination_gather_entrants_effect = yes
					#Let's gooo!
					trigger_event = { on_action = imperial_examination_opening_on_action }
				}
				scope:activity = {
					progress_activity_phase_after = { days = imperial_examination_assembly_days }
					#Set up the scoreboard
					metropolitan_examination_scoreboard_setup_effect = yes
				}
			}

			on_weekly_pulse = {
				trigger_event = { on_action = imperial_examination_pulse }
			}
		}

		#Preparation Phase
		local_examination_phase_preparation = {
			order = 2
			is_predefined = yes

			on_enter_phase = {
				scope:activity = {
					progress_activity_phase_after = { days = 1 }
				}
			}

			on_phase_active = {
				scope:activity = {
					progress_activity_phase_after = { days = imperial_examination_prep_days }
				}
			}

			on_weekly_pulse = {
				trigger_event = { on_action = imperial_examination_pulse }
			}
		}

		#Provincial Examination Phase
		local_examination_phase_examination = {
			order = 3
			is_predefined = yes

			on_enter_phase = {
				scope:activity = {
					progress_activity_phase_after = { days = 1 }
				}
			}

			on_phase_active = {
				#Intro event for the phase
				if = {
					limit = { this = scope:host }
					trigger_event = imperial_examination.3000
				}
				else_if = {
					limit = {
						is_in_guest_subset = { name = guests }
						has_activity_intent = local_exam_support_entrants_intent
					}
					trigger_event = imperial_examination.3005
				}
				else_if = {
					limit = {
						is_in_guest_subset = { name = guests }
						has_activity_intent = exam_assist_exam_intent
					}
					trigger_event = imperial_examination.3006
				}
				# Progress activity
				scope:activity = {
					progress_activity_phase_after = { weeks = 6 }
				}
			}

			on_weekly_pulse = {
				if = { # host
					limit = {
						this = scope:host
					}
					trigger_event = { on_action = imperial_examination_exam_host_events }
				}
				else_if = {
					limit = {
						is_in_guest_subset = { name = entrants }
						has_activity_intent = local_exam_taker_intent
					}
					trigger_event = { on_action = imperial_examination_exam_entrant_events }
				}
			}

			on_end = {
                #Run once
                if = {
                    limit = { this = scope:host }
                    imperial_examination_determine_exam_outcome_effect = yes
                }
                if = {
                	limit = {
                		is_in_guest_subset = { name = entrants }
                	}
                    #Get rid of failees
                    imperial_examination_prune_failees_effect = yes
                    #Clear the scoreboard
                    if = {
                        limit = { is_in_guest_subset = { name = scoreboard } }
                        save_temporary_scope_as = temp_character
                        scope:activity = {
                            remove_from_guest_subset = {
                                name = scoreboard
                                target = scope:temp_character
                            }
                        }
                    }
                }
                #Update the status
                scope:activity = {
					set_variable = {
						name = first_place_entrant_score
						value = 0
					}
					#Record the score of the top three entrants
					ordered_guest_subset = {
						name = entrants
						order_by = imperial_examination_score_value
						max = 3
					    check_range_bounds = no
					    if = {
					        limit = { exists = scope:top_entrant_2 }
					        save_scope_as = top_entrant_3
					    }
					    else_if = {
					        limit = { exists = scope:top_entrant_1 }
					        save_scope_as = top_entrant_2
					    }
					    else = { save_scope_as = top_entrant_1 }
					}
					scope:activity = {
						if = {
							limit = { exists = scope:top_entrant_1 }
							set_variable = {
								name = first_place_entrant
								value = scope:top_entrant_1
							}
							#And the score of the first place entrant
							change_variable = {
								name = first_place_entrant_score
								add = scope:top_entrant_1.var:imperial_examination_score
							}
							if = {
								limit = { exists = var:first_place_entrant_score }
								#Error suppressant
							}
						}
						if = {
							limit = { exists = scope:top_entrant_2 }
							set_variable = {
								name = second_place_entrant
								value = scope:top_entrant_2
							}
						}
						if = {
							limit = { exists = scope:top_entrant_3 }
							set_variable = {
								name = third_place_entrant
								value = scope:top_entrant_3
							}
						}
					}
					#Describe the score of the first place entrant
					if = {
						limit = { has_perfect_score_trigger = { SCORE_VAR = first_place_entrant_score } }
						set_variable = has_perfect_score
					}
					if = {
						limit = { has_good_score_trigger = { SCORE_VAR = first_place_entrant_score } }
						set_variable = has_great_score
					}
					if = {
						limit = { has_decent_score_trigger = { SCORE_VAR = first_place_entrant_score } }
						set_variable = has_decent_score
					}
					if = {
						limit = { has_questionable_score_trigger = { SCORE_VAR = first_place_entrant_score } }
						set_variable = has_questionable_score
					}
					#Record the final amount of failees
					set_variable = {
						name = num_final_failees
						value = 0
					}
					every_attending_character = {
						limit = { is_in_guest_subset = { name = failees } }
						scope:activity = {
							change_variable = {
								name = num_final_failees
								add = 1
							}
						}
					}
				}
				if = {
					limit = {
						is_in_guest_subset = { name = failees }
					}
					scope:activity = {
						change_variable = {
							name = num_final_failees
							add = 1
						}
					}
				}
				#Host
				if = {
					limit = { this = scope:host }
					local_examination_disburse_activity_host_rewards = yes
				}
				#Examiner/s
				if = {
					limit = {
						this = {
							is_in_guest_subset = { name = imperial_examiners }
							OR = {
								is_vassal_of = scope:host
								is_courtier_of = scope:host
							}
						}
					}
					imperial_examination_disburse_activity_examiner_rewards = yes
				}
				#Entrants & Failees
				if = {
					limit = {
						OR = {
							is_in_guest_subset = { name = entrants }
							is_in_guest_subset = { name = failees }
						}
					}
					save_temporary_scope_as = entrant
					# This effect also handles interface messages to relevant family members
					if = {
						limit = { is_ai = no }
					}
					local_examination_disburse_activity_entrant_rewards = yes
					every_parent = {
						limit = {
							is_ai = no
							scope:entrant.court_owner != this
						}
						add_to_list = exam_house_member
						scope:entrant = {
							add_to_list = parent_relevant_entrants
						}
					}
					house.house_head ?= {
						if = {
							limit = {
								is_ai = no
								scope:entrant.court_owner != this
							}
							add_to_list = exam_house_member
							scope:entrant = {
								add_to_list = parent_relevant_entrants
							}
						}
					}
				}
				scope:activity = {
	                #Clean up variables
	                imperial_examination_clean_up_phase_variables_effect = yes
				}
				#Let's save some variables for the conclusion desc.
				if = {
					limit = { has_activity_intent = local_exam_support_entrants_intent }
					scope:activity = {
						if = {
							limit = {
								any_attending_character = {
									has_activity_intent = local_exam_taker_intent
									OR = {
										is_close_or_extended_family_of = root
										has_relation_disciple = root
										this = root.primary_spouse
									}
									court_owner = root
								}
							}
							every_attending_character = {
								limit = {
									has_activity_intent = local_exam_taker_intent
									OR = {
										is_close_or_extended_family_of = root
										has_relation_disciple = root
										this = root.primary_spouse
									}
									court_owner = root
								}
								add_to_list = parent_relevant_entrants
							}
						}
					}
					add_character_flag = {
						flag = family_supporter
						years = 1
					}
				}
				if = {
					limit = { has_activity_intent = local_exam_taker_intent }
					add_character_flag = {
						flag = exam_taker
						years = 1
					}
				}
				if = {
					limit = { has_character_flag = family_supporter }
					trigger_event = imperial_examination.7002
				}
				else = { trigger_event = imperial_examination.7001 }
			}
		}
	}

	###################
	# GUEST HANDLING
	###################

	open_invite = yes

	max_guests = {
		value = 1
		if = {
			limit = {
				scope:imperial_examination_breadth ?= flag:imperial_examination_breadth_exclusive
			}
			add = exclusive_exam_guests_amount
			max = exclusive_exam_guests_amount
		}
		else_if = {
			limit = {
				scope:imperial_examination_breadth ?= flag:imperial_examination_breadth_restricted
			}
			add = restricted_exam_guests_amount
			max = restricted_exam_guests_amount
		}
		else_if = {
			limit = {
				scope:imperial_examination_breadth ?= flag:imperial_examination_breadth_open
			}
			add = open_exam_guests_amount
			max = open_exam_guests_amount
		}
		min = 1
	}

	can_be_activity_guest = {
		this != scope:host.top_liege
		trigger_if = {
			limit = {
				is_ruler = no
				host ?= {
					is_ai = yes
					is_ruler = yes
				}
			}
			trigger_if = {
				limit = {
					scope:host.involved_activity ?= {
						any_attending_character = {
							count >= {
								if = {
									limit = {
										scope:host.involved_activity = {
											has_activity_option = {
												category = imperial_examination_breadth
												option = imperial_examination_breadth_exclusive
											}
										}
									}
									add = exclusive_exam_guests_amount
									max = exclusive_exam_guests_amount
								}
								else_if = {
									limit = {
										scope:host.involved_activity = {
											has_activity_option = {
												category = imperial_examination_breadth
												option = imperial_examination_breadth_restricted
											}
										}
									}
									add = restricted_exam_guests_amount
									max = restricted_exam_guests_amount
								}
								else_if = {
									limit = {
										scope:host.involved_activity = {
											has_activity_option = {
												category = imperial_examination_breadth
												option = imperial_examination_breadth_open
											}
										}
									}
									add = open_exam_guests_amount
									max = open_exam_guests_amount
								}
								multiply = 0.8
							}
						}
					}
				}
				always = no
			}
		}
		trigger_if = {
			limit = {
				is_pool_character = no
			}
			top_liege = scope:host.top_liege
		}
		trigger_if = {
			limit = { exists = primary_title }
			primary_title = {
				is_mercenary_company = no
				is_holy_order = no
			}
		}
		in_diplomatic_range = scope:host
		#Basic checks
		is_adult = yes
		# To avoid monks flooding the activity
		NOR = {
			has_trait = devoted
			has_trait = eunuch
		}
		# Must be the right gender or a player
		trigger_if = {
			limit = {
				scope:host = {
					OR = {
						has_realm_law = male_only_law 
						has_realm_law = male_preference_law
					}
				}
			}
			OR = {
				is_male = yes
				is_ai = no
			}
		}
		trigger_if = {
			limit = {
				scope:host = {
					OR = {
						has_realm_law = female_only_law 
						has_realm_law = female_preference_law
					}
				}
			}
			OR = {
				is_female = yes
				is_ai = no
			}
		}
		#Proximity checks
		#Same empire as host
		default_location.county.empire = {
			OR = {
				this = scope:host.capital_county.empire
				any_title_to_title_neighboring_kingdom = {
					this = scope:host.capital_county.kingdom
				}
			}
		}
	}

	host_intents = {
		intents = { exam_give_favor_intent exam_recruit_courtiers_intent }
		default = exam_recruit_courtiers_intent
	}

	guest_intents = {
		intents = { local_exam_taker_intent local_exam_support_entrants_intent exam_assist_exam_intent }

		default = exam_assist_exam_intent
	}

	guest_join_chance = {
		base = 5
		base_activity_modifier = yes

		# Scripted Modifiers
		activity_guest_health_ai_accept_modifier = yes
		#Family and Dynasty checks
		imperial_examination_activity_dynasty_modifier = yes
		#Is Hegemon
		local_examination_activity_is_hegemon_modifier = yes
		modifier = {
			any_player = {
				is_close_or_extended_family_of = prev
				has_character_flag = command_to_attend_flag
				top_liege = prev.top_liege
			}
			NOR = {
				# Has not passed this exam before
				has_character_flag = passed_provincial_exam
				# Or higher level exams
				has_character_flag = passed_metropolitan_exam
				has_trait = devoted
				has_trait = eunuch
			}
			tgp_gender_can_be_exam_entrant_trigger = yes
			is_available = yes
			is_adult = yes
			
			add = 5000
			desc = ACTIVITY_GUEST_CLOSE_FAMILY_REASON
		}
	}

	guest_subsets = {
		entrants
		imperial_examiners
		scoreboard
		failees
		guests
	}

	wait_time_before_start = {
		# Fixed duration, because it is an open activity
		months = 3
	}

	max_guest_arrival_delay_time = { months = 1 }

	guest_invite_rules = {
		defaults = {
			1 = activity_invite_rule_player_family_opt_in_local_exam
			1 = activity_invite_rule_local_exam_entrants
			2 = activity_invite_rule_landless_family_heads_entrants
			3 = activity_invite_rule_local_exam_close_family
			4 = activity_invite_rule_local_exam_guests
		}
	}

	travel_entourage_selection = {
		weight = {
			value = 0
			if = {
				limit = {
					is_landed = no
					NOR = {
						has_character_flag = passed_provincial_exam
						has_character_flag = passed_metropolitan_exam
					}
					court_owner = scope:owner
					is_available = yes
					is_ai = yes
					is_adult = yes
					tgp_gender_can_be_exam_entrant_trigger = yes
					location = scope:owner.location
				}
				add = 100
			}
		}

		max = 15
		ai_max = 7
		invite_rule_order = 1
   	}

	###################
	# ON ACTIONS
	###################

	pulse_actions = {
		entries = {
			apa_boasting_calligraphy
			apa_impressive_shot
			apa_overcome_with_stress
			apa_brought_huangdi_gift
			apa_friendly_discussions
			apa_high_tempers
			apa_impressed_examiner
			apa_entrant_disagreement
			apa_burning_midnight_oil
			apa_found_disciple
			apa_fruitful_debate
			#AGOT Disabled
			# apa_confucian_fervor
			apa_floating_poetry
			#AGOT Disabled
			# apa_studied_confucian_classics
		}
		chance_of_no_event = 5
	}

	on_start = {
		scope:host = {
			#AGOT Disabled
			# if = {
			# 	limit = {
			# 		has_character_flag = examinations_ai_override
			# 	}
			# 	remove_character_flag = examinations_ai_override
			# }
			#AGOT Disabled
			# if = {
			# 	limit = {
			# 		has_character_flag = celestial_examination_support_cost_reduction
			# 	}
			# 	remove_character_flag = celestial_examination_support_cost_reduction
			# }
		}
		every_player = {
			limit = {
				is_landed = no
				NOT = { has_variable = recently_invited_to_exam }
				should_notify_can_join_activity = scope:activity.activity_type
				NOR = {
					has_character_flag = passed_provincial_exam
					has_character_flag = passed_metropolitan_exam
				}
				can_join_activity = scope:activity
				can_arrive_in_time_to_activity_minimum = scope:activity
			}
			set_variable = {
				name = recently_invited_to_exam
				years = 1
			}
			scope:activity = { open_view_data = { view = activity_list_detail_invite_window player = prev } }
		}
	}
	
	on_enter_travel_state = {
		# Set an appointment timeout, to prevent you from getting a new appointment during the examinations
		if = {
			limit = { is_pool_character = no }
			set_appointment_timeout = {
				months = 6
				desc = appointment_timeout_desc_examinations
			}
		}
	}

	on_enter_passive_state = { # Character scope
		#Add any stray Entrants who are in travel entourage of guests
		imperial_examination_gather_stray_entrants_effect = yes
		if = {
			limit = {
				is_ai = no
				NOR = {
					has_character_flag = passed_provincial_exam
					has_character_flag = set_activity_intent
				}
			}
			set_activity_intent = local_exam_taker_intent
			add_character_flag = set_activity_intent
		}
		if = {
			limit = {
				has_activity_intent = local_exam_taker_intent
				is_pool_character = no
			}
			# Set an appointment timeout, to prevent you from getting a new appointment during the examinations
			set_appointment_timeout = {
				years = 1
				desc = appointment_timeout_desc_examinations
			}
		}
	}

	on_passive_state_pulse = {
		imperial_examination_gather_stray_entrants_effect = yes
	}

	on_enter_active_state = { # Character scope
	}

	on_active_state_pulse = { # Character scope
	}

	on_complete = { # Character scope
		scope:activity = {
			every_attending_character = {
				if = { # If we still have a short duration remaining on our appointment timeout, clear it
					limit = {
						appointment_timeout_days > 0
						appointment_timeout_days < 365
					}
					clear_appointment_timeout = yes
				}
			}
		}
	}

	###################
	# GRAPHICS
	###################
	background = {
		texture = "gfx/interface/illustrations/event_scenes/tgp_examination_room.dds"
		environment = "environment_tgp_examination_room" 
		ambience = "event:/DLC/EP4/SFX/Events/Event_Backgrounds/tgp_examination_room"
	}

	### Plugin widgets
	activity_window_widgets = {
		imperial_examination_scoreboard_widget = "window_activity_contestants_right_bar_container"
		imperial_examination_progress_widget = "activity_plugin_widgets_summary"
	}

	window_characters = {
		player_entrant_traveling = {
			camera = camera_activity_horse

			effect = {
				scope:player ?= {
					if = {
						limit = {
							NOR = {
								this = scope:activity.activity_host
								location = scope:activity.activity_location
								has_character_flag = passed_provincial_exam
								has_character_flag = passed_metropolitan_exam
							}
						}
						add_to_list = characters
					}
				}
			}
			scripted_animation = {
				animation = jockey_walk
			}
		}
		player_guest_traveling = {
			camera = camera_activity_horse

			effect = {
				scope:player ?= {
					if = {
						limit = {
							NOR = {
								this = scope:activity.activity_host
								location = scope:activity.activity_location
							}
							OR = {
								has_character_flag = passed_provincial_exam
								has_character_flag = passed_metropolitan_exam
							}
						}
						add_to_list = characters
					}
				}
			}
			scripted_animation = {
				animation = jockey_walk
			}
		}
		player_entrant = {
			camera = camera_event_left

			effect = {
				scope:player ?= {
					if = {
						limit = {
							this != scope:activity.activity_host
							location = scope:activity.activity_location
							is_in_guest_subset = { name = entrants }
						}
						add_to_list = characters
					}
				}

			}
			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character = { has_variable = been_caught_cheating_longterm }
					}
					animation = { stress }
				}
				animation = page_flipping
			}
		}
		host = {
			camera = camera_event_left

			effect = {
				if = {
					limit = { scope:player = scope:activity.activity_host }
				}
				scope:host ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = personality_content
			}
		}

		entrant = {
			camera = camera_event_left_forward

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					every_attending_character = {
						limit = {
							is_in_guest_subset = { name = entrants }
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				triggered_animation = {
					trigger = {
						always = yes
					}
					animation = { holding_scrolls reading throne_room_writer }
				}
				#Fallback
				animation = chancellor
			}
		}

		guest = {
			camera = camera_event_left_forward

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					every_attending_character = {
						limit = {
							is_in_guest_subset = { name = guests }
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				animation = pondering
			}
		}

		examiner = {
			camera = camera_event_very_right_activity

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
					}
					every_attending_character = {
						limit = {
							is_in_guest_subset = { name = imperial_examiners }
						}
						add_to_list = characters
					}
				}
			}

			scripted_animation = {
				animation = happy_teacher
			}
		}
	}
}
