﻿activity_imperial_examination = {

	notify_player_can_join_activity = yes
	
	is_shown = {
		government_has_flag = government_is_celestial
		has_tgp_dlc_trigger = yes
		trigger_if = { # Allow a local exam to be hosted first.
			limit = {
				is_ai = yes
				NOT = { has_character_flag = examinations_ai_override }
			}
			years_from_game_start >= 5
		}
	}
	
	can_start_showing_failures_only = {
		highest_held_title_tier = tier_hegemony
		NOT = { is_activity_type_on_cooldown = activity_imperial_examination }
		trigger_if = {
			limit = {
				is_ai = yes
			}
			# Special logic for AI Hegemons, more specific than any other is_available trigger
			is_available_quick = {
				alive = yes
				travel = no
				imprisoned = no
				incapable = no
			}
			custom_description = {
				text = ALREADY_IN_ACTIVITY
				NOR = {
					exists = involved_activity
					has_variable = {
						name = homage_liege_scope
						name = meditation_character_flag
						name = local_shrine_rite
						name = petition_liege_character_flag
						name = holding_court_character_flag
					}
					is_being_visited_on_tour_strict = yes
				}
			}
			custom_description = {
				text = ALREADY_PLANNING_ACTIVITY
				NOT = { has_character_flag = planning_an_activity }
			}
		}
		trigger_else = {
			is_available = yes
		}
	}

	is_valid = {
		scope:host = {
			is_physically_able = yes
			is_landed = yes
			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 = {
		imperial_examination_clean_up_phase_variables_effect = yes
		every_attending_character = { trigger_event = activity_system.0501 }
	}

	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 = kaifeng_palace_01 }
			desc = activity_imperial_examination_province_kaifeng_palace_desc
		}
		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

		#Special flags
		if = {
			limit = {
				is_ai = yes
				has_character_flag = examinations_ai_override
			}
			add = 10000
		}
	}

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

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

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

	is_single_location = yes

	cooldown = { years = 5 }

	max_province_icons = 3

	max_route_deviation_mult = 3.0
	
	cost = {
		treasury = {
			if = {
				limit = { is_ai = no }
				add = {
					add = {
						value = imperial_examination_actual_cost
						desc = imperial_examination_base_cost
					}
					# Kaifeng cost bonus
					if = {
						limit = {
							root.capital_province = { has_building_or_higher = kaifeng_palace_01 }
						}
						multiply = {
							value = {
								value = 1
								subtract = kaifeng_examination_cost_value
							}
							desc = kaifeng_palace_name_desc
						}
					}
					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
			}
		}
	}

	# Used to show a rough minimum or expected cost at all to plan this
	ui_predicted_cost = {
		#Treasury for China
		treasury = {
			if = {
				limit = {
					is_ai = yes
					has_character_flag = examinations_ai_override
				}
				value = 0
			}
			else = {
				add = imperial_examination_predicted_cost
				add = imperial_examination_breadth_restricted_value
			}
		}
	}

	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 = imperial_examination_conclusion.entrant_passed
			}
			triggered_desc = {
				trigger = {
					has_character_flag = exam_taker
					is_in_guest_subset = { name = palace_entrants }
				}
				desc = imperial_examination_conclusion.entrant_passed_palace
			}
		}
	}

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

	options = {
		#Emperor gets to choose skill focus option, making characters with these skills more successful in passing exam rounds
		special_type = {	
			imperial_examination_focus_learning = {
				default = yes
				ai_will_do = {
					value = 100
					# Traits
					if = {
						limit = {
							has_trait = greedy
						}
						add = 25
						desc = greedy
					}
					if = {
						limit = {
							has_trait = lazy
						}
						add = 15
						desc = lazy
					}
					if = {
						limit = {
							OR = {
								has_trait = scholarly_court_1
								has_trait = scholarly_court_2
							}
						}
						add = {
							value = 25
							desc = trait_scholarly_court_2
						}
					}
					# Lifestyle
					if = {
						limit = {
							OR = {
								has_focus = learning_scholarship_focus
								has_focus = learning_theology_focus
							}
						}
						add = {
							value = 20
							desc = focus
						}
					}
					if = {
						limit = { has_trait = education_learning }
						add = {
							value = 50
							desc = education_learning
						}
					}
					if = {
						limit = { has_trait = theologian }
						add = {
							value = 50
							desc = theologian
						}
					}
					if = {
						limit = { has_trait = scholar }
						add = {
							value = 50
							desc = scholar
						}
					}
				}
			}
			imperial_examination_focus_martial = {
				is_valid = {
					situation:dynastic_cycle ?= {
						situation_current_phase = situation_dynastic_cycle_phase_stability_expansion
					}
				}
				cost = {
					#Treasury for China
					treasury = {
						if = {
							limit = { is_ai = no }
							add = {
								value = imperial_examination_alternate_focus_value
								desc = imperial_examination_focus_martial
							}
						}
						else = { value = 0 }
					}
				}
				ai_will_do = {
					value = 100
					# Lifestyle
					if = {
						limit = { has_focus = martial_strategy_focus }
						add = {
							value = 20
							desc = focus
						}
					}
					if = {
						limit = { has_trait = strategist }
						add = {
							value = 10
							desc = strategist
						}
					}
					if = {
						limit = { has_trait = overseer }
						add = {
							value = 10
							desc = overseer
						}
					}
					# Education
					if = {
						limit = { has_trait = education_martial }
						add = {
							value = 20
							desc = education_martial
						}
					}
					# Personality 
			        if = {
			        	limit = {
							ai_has_warlike_personality = yes
						}
			        	add = {
			            	value = 25
			            	desc = "Warlike"
			            }
			        }					
				}
			}
			imperial_examination_focus_stewardship = {
				is_valid = {
					situation:dynastic_cycle ?= {
						situation_current_phase = situation_dynastic_cycle_phase_stability_advancement
					}
				}
				cost = {
					#Treasury for China
					treasury = {
						if = {
							limit = { is_ai = no }
							add = {
								value = imperial_examination_alternate_focus_value
								desc = imperial_examination_focus_stewardship
							}
						}
						else = { value = 0 }
					}
				}
				ai_will_do = {
					value = 100
					# Lifestyle
					if = {
						limit = { has_focus = stewardship_domain_focus }
						add = {
							value = 20
							desc = focus
						}
					}
					if = {
						limit = { has_trait = architect }
						add = {
							value = 10
							desc = architect
						}
					}
					if = {
						limit = { has_trait = administrator }
						add = {
							value = 10
							desc = administrator
						}
					}
					if = {
						limit = { has_trait = education_stewardship }
						add = {
							value = 20
							desc = education_stewardship
						}
					}
				}
			}
		}

		#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 = { is_ai = no }
							add = imperial_examination_breadth_exclusive_value
						}
						else = { value = 0 }
					}
				}
				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 = { is_ai = no }
							add = imperial_examination_breadth_restricted_value
						}
						else = { value = 0 }
					}
				}
				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 = { is_ai = no }
							add = imperial_examination_breadth_open_value
						}
						else = { value = 0 }
					}
				}
				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 = 25
				}
			}
			imperial_examination_passing_grade_extreme = {
				# High Legitimacy reward at cost of fewer prospective governors
				ai_will_do = {
					value = 25
				}
			}
		}
	}
	special_option_category = special_type

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

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

			on_phase_active = {
				if = {
					limit = { this = scope:host }
					#Add all the examiners to guest subsets so they don't become Entrants
					imperial_examination_set_up_examiners_effect = { HOST = scope:host }
					imperial_examination_gather_examiners_effect = yes
					#Add all the Entrants to guest subsets so we can keep track of them
					imperial_examination_gather_entrants_effect = yes
					#Add all landed characters as guests
					imperial_examination_gather_guests_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
		imperial_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 }	
			}
		}

		#Metropolitan Examination Phase
		imperial_examination_phase_examination = {
			order = 3
			is_predefined = yes

			on_enter_phase = {
				scope:activity = { 
					progress_activity_phase_after = { days = 1 }
					every_attending_character = {
						limit = {
							is_ai = no
							is_in_guest_subset = { name = entrants }
							NOT = { has_activity_intent = imperial_exam_taker_intent }
						}
						save_temporary_scope_as = temp_character
						scope:activity = {
							remove_from_guest_subset = {
								name = entrants
								target = scope:temp_character
							}
							add_to_guest_subset = {
								name = guests
								target = scope:temp_character
							}
						}
					}
				}
			}

			on_phase_active = {
				#Intro event for the phase
				if = {
					limit = { this = scope:host }
					trigger_event = imperial_examination.3000
				}
				else_if = {
					limit = { has_activity_intent = imperial_exam_support_entrants_intent }
					trigger_event = imperial_examination.3005
				}
				else_if = {
					limit = { has_activity_intent = exam_assist_exam_intent }
					trigger_event = imperial_examination.3006
				}
				else_if = {
					limit = { has_activity_intent = imperial_exam_taker_intent }
					trigger_event = imperial_examination.3007
				}
				# 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 }
					}
					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 = {
                	#Entrants
                	limit = {
                		is_in_guest_subset = { name = entrants }
                	}
                    #Get rid of failees
                    imperial_examination_prune_failees_effect = yes
                    # If not having taken the Provincial Exam and then attempting the Metropolitan, we are a bit harsh.
                    if = {
                    	limit = {
                    		is_ai = no
                    		var:imperial_examination_score < 50
                    	}
                    	save_temporary_scope_as = temp_character
                    	scope:activity = {
							remove_from_guest_subset = { 
								name = entrants
								target = scope:temp_character
							}
							add_to_guest_subset = { 
								name = failees
								target = scope:temp_character
							}
						}
                    }
                    #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
                            }
                        }
                    }
                }
                #Gather the top 10 Entrants into the Palace Entrants
                if = {
                    limit = { this = scope:host }
                    scope:activity = {
                        ordered_guest_subset = {
                            name = entrants
                            order_by = imperial_examination_score_value
                            max = metropolitan_exam_final_entrants
                            check_range_bounds = no
                            save_temporary_scope_as = temp_char
                            scope:activity = {
                                remove_from_guest_subset = { 
                                    name = entrants
                                    target = scope:temp_char
                                }
                                add_to_guest_subset = {
                                    name = palace_entrants
                                    target = scope:temp_char
                                }
                            }
                        }
                    }
                }
                #Clean up variables
                imperial_examination_clean_up_phase_variables_effect = yes
            }
		}

		#Palace Exam
		imperial_examination_phase_palace_examination = {
			order = 4
			is_predefined = yes

			on_enter_phase = {
				# Let us trigger some enter phase events
				if = {
					limit = { this = scope:host }
					trigger_event = imperial_examination.9000
					scope:activity = {
						# Save this scope so we trigger the first place entrant event correctly (handled via host event).
						ordered_guest_subset = {
							name = palace_entrants
							order_by = imperial_examination_score_value
							save_scope_as = highest_score_entrant
						}
						set_variable = {
							name = highest_score_entrant
							value = scope:highest_score_entrant
						}
					}
				}

				if = {
					# Event for those who did not make the Palace Exam
					limit = {
						OR = {
							is_in_guest_subset = { name = entrants }
							is_in_guest_subset = { name = failees }
						}
					}
					trigger_event = imperial_examination.8010
				}
				else_if = {
					# Event for those who made the Palace Exam (excluding the first place entrant)
					limit = {
						is_ai = no
						this != scope:activity.var:highest_score_entrant
						is_in_guest_subset = { name = palace_entrants }
					}
					trigger_event = imperial_examination.8000
				}
				#Let us enter the Prep event on_action loop
				trigger_event = {
					on_action = palace_examination_prep_events
					days = 9
				}
				trigger_event = {
					on_action = palace_examination_prep_events
					days = 17
				}
				trigger_event = {
					on_action = palace_examination_prep_events
					days = 24
				}
				scope:activity = { 
					progress_activity_phase_after = { days = imperial_examination_prep_days }
					#Refresh the scoreboard
					palace_examination_scoreboard_setup_effect = yes
				}
			}

			on_phase_active = {
				#Reflect on the exam starting
				if = {
					limit = { this = scope:host }
					trigger_event = imperial_examination.3000
				}
				else_if = {
					limit = {
						is_in_guest_subset = { name = palace_entrants }
						has_activity_intent = imperial_exam_taker_intent
					}
					trigger_event = { on_action = imperial_examination_exam_entrant_events }
				}
				scope:activity = { 
					progress_activity_phase_after = { days = 14 }
				}
			}

			on_weekly_pulse = {
				
			}

			on_end = {
				if = {
					#Entrant
					limit = {
						is_in_guest_subset = { name = palace_entrants }
					}
					#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
							}
						}
					}
				}
				scope:activity = {
					set_variable = {
						name = first_place_entrant_score
						value = 0
					}
					#Record the score of the top three entrants
					ordered_guest_subset = {
						name = palace_entrants
						order_by = imperial_examination_score_value
						position = 0
						save_temporary_scope_as = temp_first_char
						scope:activity = {
							set_variable = {
								name = first_place_entrant
								value = scope:temp_first_char
							}
							#And the score of the first place entrant
							change_variable = {
								name = first_place_entrant_score
								add = scope:temp_first_char.var:imperial_examination_score
							}
							if = {
								limit = { exists = var:first_place_entrant_score }
								#Error suppressant
							}
						}
					}
					ordered_guest_subset = {
						name = palace_entrants
						order_by = imperial_examination_score_value
						position = 1
						save_temporary_scope_as = temp_second_char
						scope:activity = {
							set_variable = {
								name = second_place_entrant
								value = scope:temp_second_char
							}
						}
					}
					ordered_guest_subset = {
						name = palace_entrants
						order_by = imperial_examination_score_value
						position = 2
						save_temporary_scope_as = temp_third_char
						scope:activity = {
							set_variable = {
								name = third_place_entrant
								value = scope:temp_third_char
							}
						}
					}
					#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
					}
				}
				if = {
					limit = {
						is_in_guest_subset = { name = failees }
					}
					scope:activity = {
						change_variable = {
							name = num_final_failees
							add = 1
						}
					}
				}
				#Emperor
				if = {
					limit = { this = scope:host }
					imperial_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 = palace_entrants }
							is_in_guest_subset = { name = entrants }
							is_in_guest_subset = { name = failees }
						}
					}
					save_temporary_scope_as = entrant
					# This effect also handles close family member messaging (excluding the house head)
					if = {
						limit = { is_ai = no }
						exam_grab_entrant_position_effect = yes
					}
					# This effect also handles close family member messaging (excluding the house head)
					imperial_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
								NOT = { is_in_list = exam_house_member }
								scope:entrant.court_owner != this
							}
							add_to_list = exam_house_member
							scope:entrant = {
								add_to_list = parent_relevant_entrants
							}
						}
						if = {
							limit = {
								list_size = {
									name = exam_house_member
									value = 0
								}
							}
							house.house_head ?= {
								if = {
									limit = {
										is_ai = no
										NOT = { is_in_list = exam_house_member }
										scope:entrant.court_owner != this
									}
									add_to_list = exam_house_member
									scope:entrant = {
										add_to_list = parent_relevant_entrants
									}
								}
							}
						}
					}
				}
				every_in_list = {
					list = exam_house_member
					limit = {
						NOR = {
							involved_activity ?= scope:activity
							has_character_flag = triggered_family_followup
						}
					}
					add_character_flag = triggered_family_followup
					trigger_event = imperial_examination.7100
				}
				# Save scopes for characters involved in the Capital exam, to use in conclusion descs.
				if = {
					limit = { has_activity_intent = imperial_exam_support_entrants_intent }
					scope:activity = {
						if = {
							limit = {
								any_attending_character = {
									has_activity_intent = imperial_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 = imperial_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 = imperial_exam_taker_intent }
					add_character_flag = {
						flag = exam_taker
						years = 1
					}
				}
				# Trigger conclusion event
				if = {
					limit = { has_character_flag = family_supporter }
					trigger_event = imperial_examination.7003
				}
				else = { trigger_event = imperial_examination.7000 }
			}
		}
	}

	###################
	# 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 = {
		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
			}
		}
		OR = {
			government_has_flag = government_is_celestial
			government_has_flag = government_is_landless_adventurer
		}
		#Just... roll with it.
		in_diplomatic_range = scope:host
		is_adult = yes
		# To avoid monks flooding the activity
		NOR = {
			has_trait = devoted
			has_trait = eunuch
		}
		# To avoid mercenaries and holy orders to join
		trigger_if = {
			limit = {
				exists = primary_title
			}
			primary_title = {
				is_mercenary_company = no
				is_holy_order = no
			}
		}
		# 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
			}
		}
	}

	host_intents = {
		intents = { legitimacy_intent exam_recruit_courtiers_intent }
		default = exam_recruit_courtiers_intent
	}

	guest_intents = {
		intents = { imperial_exam_taker_intent imperial_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_shared_ai_accept_modifier = yes
		activity_guest_health_ai_accept_modifier = yes
		#Failee checks
		imperial_examination_activity_failee_modifier = yes
		#Forced to retake exam checks
		imperial_examination_activity_forced_back_modifier = yes
		#Is the Minister of Rites
		imperial_examination_activity_rites_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 = {
				# You should get as many shots at the Palace Exam as you want.
				has_character_flag = passed_palace_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 = {
		palace_entrants
		entrants
		imperial_examiners
		scoreboard
		failees
		guests
	}

	wait_time_before_start = {
		# Fixed duration, because it is an open activity
		months = imperial_examination_wait_time_before_start_months
	}
	
	max_guest_arrival_delay_time = { months = 2 }

	guest_invite_rules = {
		#These are not weighed differenty because the guest invite rules themselves handle the guest distribution.
		defaults = {
			1 = activity_invite_rule_player_family_opt_in_imperial_exam
			1 = activity_invite_rule_imperial_exam_entrants
			1 = activity_invite_rule_imperial_exam_guests
		}
	}

	travel_entourage_selection = {
		weight = {
			value = standard_travel_entourage_additions
			if = {
				limit = {
					is_landed = no
					is_faith_dominant_gender = yes
					NOT = { has_character_flag = passed_palace_exam }
					court_owner = scope:owner
					is_available = yes
					is_ai = yes
					is_adult = yes
					location = scope:owner.location
				}
				add = 50
			}
		}

		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
			apa_confucian_fervor
			apa_floating_poetry
			apa_studied_confucian_classics
		}
		chance_of_no_event = 5
	}

	on_start = {
		scope:host = {
			#Dynastic Cycle is pleased!
			if = {
				limit = { this = title:h_china.holder }
				title:h_china = {
					set_variable = {
						name = years_since_imperial_examination
						value = 0
					}
				}
			}
			if = {
				limit = { is_ai = yes}
				remove_treasury = {
					value = {
						value = imperial_examination_actual_cost
						# Kaifeng cost bonus
						if = {
							limit = {
								root.capital_province = { has_building_or_higher = kaifeng_palace_01 }
							}
							multiply = {
								value = {
									value = 1
									subtract = kaifeng_examination_cost_value
								}
								desc = kaifeng_palace_name_desc
							}
						}
						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
							}
						}
						if = {
							limit = {
								scope:activity = {
									NOT = {
										has_activity_option = {
											category = special_type
											option = imperial_examination_focus_learning
										}
									}
								}
							}
							add = imperial_examination_alternate_focus_value
						}
						if = {
							limit = {
								scope:activity = {
									has_activity_option = {
										category = imperial_examination_breadth
										option = imperial_examination_breadth_exclusive
									}
								}
							}
							add = imperial_examination_breadth_exclusive_value
						}
						else_if = {
							limit = {
								scope:activity = {
									has_activity_option = {
										category = imperial_examination_breadth
										option = imperial_examination_breadth_restricted
									}
								}
							}
							add = imperial_examination_breadth_restricted_value
						}
						else = { add = imperial_examination_breadth_open_value }
						# AI gets it for free if a Movement demanded it, to ensure they do it.
						if = {
							limit = { has_character_flag = examinations_ai_override }
							multiply = 0
						}
					}
				}
				if = {
					limit = {
						has_character_flag = examinations_ai_override
					}
					remove_character_flag = examinations_ai_override
				}
			}
		}

		# Make sure that Examinations are held roughly every 5 years in relevant areas
		if = {
			limit = {
				scope:host.location = { geographical_region = world_asia_china }
				NOT = {
					exists = global_var:examination_world_asia_china_motivation
				}
			}
			set_global_variable = {
				name = examination_world_asia_china_motivation
				value = yes
				years = 5
			}
		}
		every_player = {
			limit = {
				is_landed = no
				NOT = { has_variable = recently_invited_to_exam }
				should_notify_can_join_activity = scope:activity.activity_type
				NOT = { has_character_flag = passed_palace_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_palace_exam
					has_character_flag = set_activity_intent
				}
			}
			set_activity_intent = imperial_exam_taker_intent
			add_character_flag = set_activity_intent
		}
		if = {
			limit = {
				has_activity_intent = imperial_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
	###################

	# Travel
	background = { # Bridge
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				is_riverside_province = yes
				geographical_region = world_europe
				OR = {
					terrain = farmlands
					terrain = plains
					terrain = mountains
					terrain = hills
				}
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_bridge.dds"
		environment = "environment_travel_bridge"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}

	background = { #Rice Fields - If we have Paddy fields AND/or terraced hills
	#This goes before other terrain since it can override in many different terrains if built
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				OR = {
					terrain = terraced_hills
					has_building_or_higher = paddy_fields_01
				}
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/tgp_rice_fields.dds"
		environment = "environment_tgp_rice_fields"
		ambience = "event:/DLC/EP4/SFX/Events/Event_Backgrounds/tgp_rice_fields"
	}

	background = { #Farmlands - East Asia
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				culture = { has_graphical_east_asia_culture_group_trigger = yes }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/tgp_farm_asia.dds"
		environment = "environment_tgp_farm_asia"
		ambience = "event:/DLC/EP4/SFX/Events/Event_Backgrounds/tgp_farm_asia"
	}
	
	background = { # Farmland, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_farm.dds"
		environment = "environment_travel_farmlands"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}
	background = { # Farmland, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				terrain = farmlands
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/farms.dds"
		environment = "environment_event_farms"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	background = { #Desert, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_desert_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_desert.dds"
		environment = "environment_travel_desert"
		ambience = "event:/SFX/Events/Backgrounds/desert_day"
	}
	background = { #Desert, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_desert_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/desert.dds"
		environment = "environment_event_bp1_desert"
		ambience = "event:/SFX/Events/Backgrounds/desert_day"
	}

	background = { #forest snowy
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location ?= { hunt_snowy_forest_trigger = yes }
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_hunt_snowy_forest.dds"
		environment = "environment_hunt_snowy_forest"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}

	background = { #forest_pine
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_forest_pine_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/forest_pine.dds"
		environment = "environment_event_forest_pine"
		ambience = "event:/SFX/Events/Backgrounds/coniferous_forest_day"
	}

	background = { #forest
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_forest_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/forest.dds"
		environment = "environment_event_forest"
		ambience = "event:/SFX/Events/Backgrounds/deciduous_forest_day"
	}

	background = { #mountains, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_mountains_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_mountains.dds"
		environment = "environment_travel_mountain_settlement"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}
	background = { #mountains, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_mountains_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/mountains.dds"
		environment = "environment_event_mountains"
		ambience = "event:/SFX/Events/Backgrounds/mountains_day"
	}

	background = { #steppe, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_steppe_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_steppe.dds"
		environment = "environment_travel_steppe_settlement"
		ambience = "event:/SFX/Events/Backgrounds/steppe_day"
	}
	background = { #steppe, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_steppe_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/steppe.dds"
		environment = "environment_event_steppe"
		ambience = "event:/SFX/Events/Backgrounds/steppe_day"
	}

	background = { #drylands (formerly desert)
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_drylands_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/drylands.dds"
		environment = "environment_event_desert"
		ambience = "event:/SFX/Events/Backgrounds/desert_day"
	}

	background = { #wetlands
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_wetlands_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_wetlands.dds"
		environment = "environment_event_bp1_wetlands" 
		ambience = "event:/SFX/Events/Backgrounds/deciduous_forest_day"
	}

	background = { #jungle
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_wilderness_jungle_trigger = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_jungle.dds"
		environment = "environment_event_bp1_jungle"
		ambience = "event:/SFX/Events/Backgrounds/deciduous_forest_day"
	}

	background = { #plains, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_plains_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_hills.dds"
		environment = "environment_travel_hills_settlement"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}
	background = { #plains, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_plains_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_plains.dds"
		environment = "environment_event_bp1_plains"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	background = { #hills, settled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_hills_trigger = yes
				exists = province_owner
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/ep2_travel_hills.dds"
		environment = "environment_travel_hills_settlement"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}
	background = { #hills, unsettled
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				graphical_hills_trigger = yes
				NOT = { exists = province_owner }
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/bp1_hills.dds"
		environment = "environment_event_bp1_hills"
		ambience = "event:/SFX/Events/Backgrounds/fields_farms_day"
	}

	background = { #sea
		trigger = {
			scope:host.location != scope:activity.activity_location
			scope:host.location = {
				is_sea_province = yes
			}
		}
		texture = "gfx/interface/illustrations/event_scenes/fp1_ocean.dds"
		environment = "environment_event_fp1_ocean"
		ambience = "event:/DLC/FP1/SFX/Events/event_ocean_longship"
	}
	# End Travel

	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
							}
						}
						add_to_list = characters
					}	
				}	
			}
			scripted_animation = {
				animation = jockey_walk
			}
		}
		player_entrant = {
			camera = camera_event_center
			
			effect = {
				scope:player ?= {
					if = {
						limit = {
							this != scope:activity.activity_host
							location = scope:activity.activity_location
							has_activity_intent = imperial_exam_taker_intent
						}
						add_to_list = characters
					}
				}
				
			}
			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character = { has_variable = been_caught_cheating_longterm }
					}
					animation = { stress }
				}
				animation = thinking
			}
		}
		host = {
			camera = camera_event_center
			
			effect = {
				if = {
					limit = { scope:player = scope:activity.activity_host }
				}
				scope:host ?= { add_to_list = characters }
			}
			scripted_animation = {
				animation = pondering
			}
		}

		minister_of_rites = {
			camera = camera_event_very_left_activity

			effect = {
				if = {
					limit = {
						scope:host.location = scope:activity.activity_location
						any_attending_character = {
							location = scope:activity.activity_location
							has_title = title:e_minister_of_rites
						}
					}
					every_attending_character = {	
						limit = {
							location = scope:activity.activity_location
							has_title = title:e_minister_of_rites
						}
						add_to_list = characters
					}
				}
			}
			scripted_animation = {
				animation = chancellor
			}
		}

		entrant = {
			camera = camera_event_center

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

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character = {
							NOR = {
								has_trait = drunkard
								has_trait = lifestyle_reveler
							}
						}
					}
					animation = { worry admiration beg stress holding_scrolls }
				}
				#Fallback
				animation = chancellor
			}
		}

		guest = {
			camera = camera_event_center

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

			scripted_animation = {
				triggered_animation = {
					trigger = {
						scope:character = {
							OR = {
								has_trait = drunkard
								has_trait = lifestyle_reveler
							}
						}
					}
					animation = { happiness storyteller interested_left }
				}
				animation = flirtation_left
			}
		}

		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
			}
		}
	}
}
