﻿namespace = imperial_examination

#######################
### SYSTEM EVENTS
#######################
# imperial_examination.0001 -
#
#
#
#







#######################
### ACTIVITY EVENTS
#######################
# imperial_examination.1000 - Opening event for Entrants
# imperial_examination.1001 - Opening event for Host
# imperial_examination.1002 - Opening event for guests with family
# imperial_examination.1003 - Opening event for guests supporting exam
# imperial_examination.1010 - Entrant: So many entrants
# imperial_examination.1020 - Entrant: Meet a fellow entrant
# imperial_examination.1040 - Entrant: Taking in the Sights
# imperial_examination.1050 - Entrant: First Impressions
# imperial_examination.1060 - Emperor: A delicious gift
# imperial_examination.1070 - Emperor: A Most Favorable Candidate


# Opening event
# For Entrants
imperial_examination.1000 = {
	type = activity_event
	title = imperial_examination.1000.t
	desc = imperial_examination.1000.desc
	theme = imperial_examination
	override_background = { reference = tgp_chinese_city }
	left_portrait = {
		character = root
		animation = happiness
		camera = camera_event_crowd
	}
	right_portrait = {
		character = scope:gambler
		animation = betting
		camera = camera_event_right_pointing_left_fov50
		hide_info = yes
	}
	center_portrait = {
		character = scope:courtesan
		animation = fanning_coyly
		camera = camera_event_right_pointing_right_scheme
		hide_info = yes
	}

	immediate = {
		if = {
			limit = {
				is_ai = no
			}
			create_character = {
				template = tgp_concubine_template
				location = root.location
				save_scope_as = courtesan
			}
			create_character = {
				template = servant_character
				gender = male
				location = root.location
				save_scope_as = gambler
				after_creation = {
					remove_character_flag = peasant_outfit
				}
			}
		}
	}

	option = { # I will be successful, no matter what!
		name = imperial_examination.1000.a
		custom_tooltip = imperial_examination.1000.a.tt
		trigger = {
			is_in_guest_subset = { name = entrants }
		}
		add_character_flag = exam_cheat_intent
		stress_impact = {
			just = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
			}
			modifier = {
				has_trait = just
				add = -20
			}
		}
	}

	option = { # I wish to prove myself the honorable way.
		name = imperial_examination.1000.b
		trigger = {
			is_in_guest_subset = { name = entrants }
		}
		add_piety = minor_piety_gain
		add_character_flag = exam_perform_intent

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
		}
	}

	after = {
		scope:courtesan ?= { silent_disappearance_effect = yes }
		scope:gambler ?= { silent_disappearance_effect = yes }
	}
}

#For the Host
imperial_examination.1001 = {
	type = activity_event
	title = imperial_examination.1001.t
	desc = imperial_examination.1001.desc
	theme = imperial_examination
	override_background = { reference = tgp_chinese_city }
	left_portrait = {
		character = root
		animation = personality_honorable
	}

	option = {
		name = imperial_examination.1001.a
		change_influence = minor_influence_gain
		ai_chance = {
			base = 100
		}
	}
}

#For Guests with the intent to further candidates
imperial_examination.1002 = {
	type = activity_event
	title = imperial_examination.1002.t
	desc = imperial_examination.1002.desc
	theme = imperial_examination
	override_background = { reference = tgp_chinese_city }
	left_portrait = {
		character = root
		animation = personality_bold
		camera = camera_event_right_forward
	}

	right_portrait = {
		character = scope:candidate
		animation = holding_scrolls
	}

	trigger = {
		# Family member/spouse/heir/disciple is available
		scope:activity = {
			any_attending_character = {
				is_in_guest_subset = { name = entrants }
				this != root
				OR = {
					is_close_family_of = root
					has_relation_disciple = root
					this = root.primary_spouse
				}
			}
		}
	}

	immediate = {
		# Find a family member/spouse/heir/disciple
		scope:activity = {
			random_attending_character = {
				limit = {
					is_in_guest_subset = { name = entrants }
					this != root
					OR = {
						is_close_family_of = root
						has_relation_disciple = root
						this = root.primary_spouse
					}
				}
				weight = {
					base = 1
					modifier = {
						add = 50
						is_primary_heir_of = root
					}
				}
				save_scope_as = candidate
			}
		}
	}

	option = { # Succeed at any cost!
		name = imperial_examination.1002.a
		custom_tooltip = imperial_examination.1002.a.tt
		scope:candidate = {
			add_character_flag = exam_cheat_intent
			add_character_modifier = {
				modifier = tgp_wiles_is_the_way_modifier
				years = 5
			}
		}
		stress_impact = {
			just = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
				ai_boldness = 1
			}
			modifier = {
				has_trait = just
				factor = 0
			}
		}
	}

	option = { # Be honorable, and success will follow.
		name = imperial_examination.1002.b
		add_piety = minor_piety_gain
		scope:candidate = {
			add_character_flag = exam_perform_intent
			add_character_modifier = {
				modifier = tgp_honor_is_the_way_modifier
				years = 5
			}
		}
		stress_impact = {
			deceitful = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
				ai_compassion = 1
			}
			modifier = {
				has_trait = deceitful
				factor = 0
			}
		}
	}
}

#For Guests with the intent to support the exam
imperial_examination.1003 = {
	type = activity_event
	title = imperial_examination.1003.t
	desc = {
		desc = imperial_examination.1003.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					NOR = {
						has_character_flag = passed_provincial_exam
						has_character_flag = passed_metropolitan_exam
					}
				}
				desc = imperial_examination.1003.not_passed_exam
			}
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_type = activity_local_examination
					}
					has_character_flag = passed_provincial_exam
				}
				desc = imperial_examination.1003.passed_provincial_exam
			}
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
					}
					NOT = { has_character_flag = passed_palace_exam }
				}
				desc = imperial_examination.1003.passed_metropolitan_not_palace
			}
			triggered_desc = {
				trigger = {
					has_character_flag = passed_palace_exam
				}
				desc = imperial_examination.1003.passed_all_exams
			}
		}
	}
	theme = imperial_examination
	override_background = { reference = tgp_chinese_city }
	left_portrait = {
		character = root
		animation = holding_scrolls
	}

	option = { # Nice to be here!
		name = imperial_examination.1003.a
		change_influence = {
			value = minor_influence_gain
			if = {
				limit = { has_character_flag = passed_palace_exam }
				multiply = 2
			}
		}
		ai_chance = {
			base = 100
		}
	}
}

scripted_effect imperial_examination_fetch_examiner_effect = {
	if = {
		limit = {
			scope:activity = {
				any_guest_subset = {
					name = imperial_examiners
					count >= 1
					is_ai = yes
				}
			}
		}
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = { is_ai = yes }
				save_scope_as = examiner
			}
		}
	}
	else = {
		create_character = {
			template = tgp_examiner_learning_template
			location = root.location
			save_scope_as = examiner
		}
		scope:examiner = {
			add_to_activity = scope:activity
		}
		scope:activity = {
			add_to_guest_subset = {
				name = imperial_examiners
				target = scope:examiner
			}
		}
	}
}

scripted_effect imperial_examination_fetch_other_entrant_effect = {
	scope:activity = {
		random_attending_character = {
			limit = {
				is_in_guest_subset = { name = entrants }
				this != root
			}
			save_scope_as = other_entrant
		}
	}
}

scripted_trigger 1020_new_entrant_trigger = {
	NOR = {
		this = root
		is_of_minor_interest_trigger = { CHARACTER = root }
		is_of_major_interest_trigger = { CHARACTER = root }
	}
	is_ai = yes
}

#Entrant: Meet a fellow entrant
imperial_examination.1020 = {
	type = activity_event
	title = imperial_examination.1020.t
	desc = imperial_examination.1020.desc
	theme = imperial_examination
	override_background = { reference = tavern }
	cooldown = { months = 6 }
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { has_trait = shy }
			animation = shame
		}
		animation = interested
	}
	right_portrait = {
		character = scope:new_entrant
		triggered_animation = {
			trigger = { has_trait = shy }
			animation = shame
		}
		animation = worry
	}
	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		scope:activity = {
			any_guest_subset = {
				name = entrants
				1020_new_entrant_trigger = yes
			}
		}
	}
	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = { 1020_new_entrant_trigger = yes }
				save_scope_as = new_entrant
			}
		}
	}
	#BFFs?
	option = {
		name = imperial_examination.1020.a
		duel = {
			skill = diplomacy
			value = decent_skill_rating
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				opinion_modifier = {
					opinion_target = scope:new_entrant
					multiplier = 2
					min = 0
				}
				desc = imperial_examination.1020.a.success.tt
				send_interface_toast = {
					type = event_toast_effect_good
					title = imperial_examination.1020.a.success.tt
					left_icon = root
					right_icon = scope:new_entrant
					progress_towards_friend_effect = {
						REASON = friend_bonded_over_exam_jitters
						CHARACTER = scope:new_entrant
						OPINION = default_friend_opinion
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				min = 5
				desc = imperial_examination.1020.a.failure.tt
				send_interface_toast = {
					type = event_toast_effect_bad
					title = imperial_examination.1020.a.failure.tt
					left_icon = root
					right_icon = scope:new_entrant
				}
			}
		}
		ai_chance = {
			base = 100
		}
	}
	#Safe option, play it cool
	option = {
		name = imperial_examination.1020.b
		stress_impact = {
			base = miniscule_stress_impact_loss
		}
		ai_chance = {
			base = 100
		}
	}
}

# Taking in the Sights
imperial_examination.1040 = {
	type = activity_event
	title = imperial_examination.1040.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					has_character_flag = taken_in_sights
				}
				desc = imperial_examination.1040.desc.again
			}
			desc = imperial_examination.1040.desc
		}
		desc = imperial_examination.1040.desc.outro
	}

	theme = imperial_examination
	override_background = { reference = market }

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				has_character_flag = taken_in_sights
			}
			animation = disapproval
		}
		animation = ecstasy
	}

	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
	}

	immediate = {
		# Create merchant for scroll artifact
		create_character = {
			template = merchant_template
			location = root.location
			save_scope_as = foreign_merchant
		}
	}

	option = { # I should search the local market for a scroll!
		name = imperial_examination.1040.a
		custom_tooltip = imperial_examination.1040.a.tt
		trigger = {
			has_character_flag = exam_perform_intent
			trigger_if = {
				limit = {
					involved_activity = { has_activity_type = activity_imperial_examination }
				}
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_learning
					}
				}
			}
		}
 		hidden_effect = {
 			create_artifact = {
				name = artifact_scroll_name
				description = artifact_scroll_desc
				modifier = artifact_learning_1_modifier
				type = miscellaneous
				visuals = artifact_scroll
				save_scope_as = artifact_scroll
			}
 		}
		remove_short_term_gold = minor_gold_value
	    send_interface_toast = {
			title = imperial_examination.1040.a.tt_equipped
		    left_icon = root
		    right_icon = scope:artifact_scroll
		    scope:artifact_scroll = {
		    	set_variable = {
					name = suppress_artifact_notifications
					value = yes
					days = 1
				}
		        set_owner = {
		        	target = root
					history = {
						location = root.location
						actor = scope:foreign_merchant
						recipient = root
						type = purchased
					}
				}
			}
		}

		stress_impact = {
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 0
		}
	}

	option = { # I should see what I can learn from the locals.
		name = imperial_examination.1040.d
		trigger = {
			has_character_flag = exam_perform_intent
			trigger_if = {
				limit = {
					involved_activity = { has_activity_type = activity_imperial_examination }
				}
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_stewardship
					}
				}
			}
			OR = {
				AND = {
					is_landed = yes
					government_allows = administrative
				}
				has_trait = governor
			}
		}
		duel = {
			skill = stewardship
			value = decent_skill_rating
			50 = { # You gain valuable insights!
				desc = imperial_examination.1040.d.tt.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 0.5
				}
				send_interface_toast = {
					left_icon = root
					title = imperial_examination.1040.d.tt.success
					if = {
						limit = {
							has_trait = governor
						}
						increase_governance_effect = { VALUE = 2 }
					}
					else_if = {
						limit = {
							government_has_flag = government_has_influence
						}
						change_influence = medium_influence_gain
					}
					else = {
						add_prestige = medium_prestige_gain
					}
				}
			}
			50 = { # You fail
				desc = imperial_examination.1040.d.tt.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -0.5
				}
				send_interface_toast = {
					left_icon = root
					title = imperial_examination.1040.d.tt.failure
					add_prestige = minor_prestige_loss
				}
				stress_impact = {
					base = minor_stress_impact_gain
				}
			}
		}
		stress_impact = {
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1
			}
			modifier = {
				add = 50
				stewardship >= mediocre_skill_rating
			}
			modifier = {
				add = -20
				has_trait = shy
			}
		}
	}

	option = { #I should hone my skills at the sparring grounds!
		name = imperial_examination.1040.e
		trigger = {
			has_character_flag = exam_perform_intent
			trigger_if = {
				limit = {
					involved_activity = { has_activity_type = activity_imperial_examination }
				}
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
		}
		duel = {
			skill = prowess
			value = decent_skill_rating
			50 = { # You outmatch the local warriors
				desc = imperial_examination.1040.e.tt.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 0.5
				}
				send_interface_toast = {
					left_icon = root
					title = imperial_examination.1040.e.tt.success
					add_character_modifier = {
						modifier = tgp_outmatched_local_warriors_modifier
						years = 5
					}
				}
			}
			50 = { # You fail
				desc = imperial_examination.1040.e.tt.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -0.5
				}
				send_interface_toast = {
					left_icon = root
					title = imperial_examination.1040.e.tt.failure
					add_prestige = minor_prestige_loss
					stress_impact = {
						base = minor_stress_impact_gain
					}
				}
			}
		}

		stress_impact = {
			craven = medium_stress_impact_gain
			lazy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
			}
			modifier = {
				add = -20
				OR = {
					has_trait = craven
					has_trait = lazy
				}
			}
			modifier = {
				add = 50
				prowess >= mediocre_skill_rating
			}
		}
	}

	option = { # I should search the local market for a "gift"
		name = imperial_examination.1040.b
		trigger = {
			has_character_flag = exam_cheat_intent
		}

		remove_short_term_gold = minor_gold_value
		change_influence = minor_influence_gain

		scope:host = {
			add_opinion = {
				modifier = pleased_opinion
				opinion = 25
				target = root
			}
		}
		scope:activity = {
			if = {
				limit = {
					root = { is_ai = no }
				}
				add_activity_log_entry = {
					key = examination_gifted_host
					score = 25
					tags = { positive }
					character = root
					target = scope:host
				}
			}
		}

		stress_impact = {
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = -1
			}
		}
	}

	option = { # I have no time for distractions.
		name = imperial_examination.1040.c

		stress_impact = {
			impatient = minor_stress_impact_loss
			lazy = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = 1
			}
		}
	}
	after = {
		add_character_flag = taken_in_sights
		scope:foreign_merchant = { silent_disappearance_effect = yes }
	}
}

# Entrant: First Impressions
imperial_examination.1050 = {
	type = activity_event
	title = imperial_examination.1050.t
	desc = imperial_examination.1050.desc

	theme = imperial_examination
	override_background = { reference = study }

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = admiration
	}

	center_portrait = {
		character = scope:other_entrant
		trigger = { exists = scope:other_entrant }
		animation = debating
	}

	right_portrait = {
		character = scope:examiner
		animation = interested
	}

	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		scope:activity = {
			any_guest_subset = {
				name = imperial_examiners
				count >= 1
				is_ai = yes
			}
		}
	}

	immediate = {
		imperial_examination_fetch_examiner_effect = yes
		scope:activity = {
			#Grab an examiner
			random_guest_subset = {
				name = entrants
				limit = {
					is_in_guest_subset = { name = scoreboard }
					is_ai = yes
				}
				save_scope_as = other_entrant
			}
			if = {
				limit = {
					NOT = { exists = scope:entrant_to_meet }
				}
				random_guest_subset = {
					name = entrants
					limit = {
						is_ai = yes
					}
					save_scope_as = other_entrant
				}
			}
		}
	}

	option = { #Impress with scholarly knowledge
		name = imperial_examination.1050.a
		trigger = { has_character_flag = exam_perform_intent }

		duel = {
			skill = learning
			target = scope:other_entrant
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = imperial_examination.1050.a.success.tt
				send_interface_toast = {
					title = imperial_examination.1050.a.success.tt
					left_icon = scope:examiner
					scope:activity = {
						if = {
							limit = {
								root = { is_ai = no }
							}
							add_activity_log_entry = {
								key = examination_impressed_examiner
								score = 25
								tags = { positive }
								character = root
								target = scope:examiner
							}
						}
					}
					custom_tooltip = imperial_examination.advantage.tt
					imperial_examination_adjust_advantage_variable_effect = yes
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.1050.a.failure
				send_interface_toast = {
					title = imperial_examination.1050.a.failure.tt
					left_icon = scope:examiner
					add_prestige = medium_prestige_loss
				}
				stress_impact = {
					base = medium_stress_impact_gain
				}
			}
		}

		stress_impact = {
			lazy = minor_stress_impact_gain
			impatient = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
			}
			modifier = {
				add = 50
				OR = {
					learning >= mediocre_skill_rating
					has_trait = ambitious
				}
			}
			modifier = {
				add = -20
				OR = {
					has_trait = lazy
					has_trait = impatient
				}
			}
		}
	}

	option = { # Quick! Take credit for another entrant's work!
		name = imperial_examination.1050.b
		trigger = { has_character_flag = exam_cheat_intent }

		duel = {
			skill = intrigue
			target = scope:other_entrant
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = imperial_examination.1050.b.success.tt
				send_interface_toast = {
					title = imperial_examination.1050.b.success.tt
					left_icon = scope:examiner
					scope:activity = {
						if = {
							limit = {
								root = { is_ai = no }
							}
							add_activity_log_entry = {
								key = examination_impressed_examiner
								score = 25
								tags = { positive }
								character = root
								target = scope:examiner
							}
						}
					}

					custom_tooltip = imperial_examination.advantage.tt
					imperial_examination_adjust_advantage_variable_effect = yes
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.1050.b.failure.tt
				send_interface_toast = {
					title = imperial_examination.1050.b.failure.tt
					left_icon = scope:examiner
					add_prestige = medium_prestige_loss
				}
				stress_impact = {
					base = medium_stress_impact_gain
				}
			}
		}
		stress_impact = {
			honest = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			impatient = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_honor = -1
			}
			modifier = {
				add = 50
				OR = {
					intrigue >= mediocre_skill_rating
					has_trait = ambitious
				}
			}
			modifier = {
				add = -20
				OR = {
					has_trait = lazy
					has_trait = impatient
					has_trait = ambitious
				}
			}
		}
	}

	option = { #I will prove myself at the exam
		name = imperial_examination.1050.c

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}
}

# Emperor: A delicious gift
imperial_examination.1060 = {
	type = activity_event
	title = imperial_examination.1060.t
	desc = imperial_examination.1060.desc
	theme = imperial_examination
	override_background = { reference = tavern }
	cooldown = { months = 6 }
	left_portrait = {
		character = root
		animation = interested
	}
	right_portrait = {
		character = scope:entrant_to_meet
		animation = obsequious_bow
	}
	trigger = {
		#Are we in the correct subset
		this = scope:host
		has_activity_intent = exam_recruit_courtiers_intent
		scope:activity = {
			any_attending_character = {
				is_in_guest_subset = { name = entrants }
				is_ai = yes
				NOT = { is_in_list = met_entrants }
			}
		}
	}
	immediate = {
		scope:activity = {
			random_attending_character = {
				limit = {
					is_in_guest_subset = { name = scoreboard }
					is_ai = yes
					NOT = { is_in_list = met_entrants }
				}
				save_scope_as = entrant_to_meet
				save_scope_as = scoreboard_entrant
			}
			if = {
				limit = {
					NOT = { exists = scope:entrant_to_meet }
				}
				random_attending_character = {
					limit = {
						is_in_guest_subset = { name = entrants }
						is_ai = yes
						NOT = { is_in_list = met_entrants }
					}
					save_scope_as = entrant_to_meet
				}
			}
		}
		scope:entrant_to_meet = { add_to_list = met_entrants }
	}
	#Great fortunes will come to those who show such generosity.
	option = {
		name = imperial_examination.1060.b
		custom_tooltip = imperial_examination.1060.b.tt
		scope:entrant_to_meet = {
			imperial_examination_adjust_advantage_variable_effect = yes
		}
		add_hook = {
			type = obligation_hook
			target = scope:entrant_to_meet
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
			}
		}
	}
	#Let's enjoy them together!
	option = {
		name = imperial_examination.1060.c
		scope:entrant_to_meet = {
			add_opinion = {
				modifier = pleased_opinion
				opinion = 30
				target = root
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1
			}
		}
	}
	#I will enjoy these!
	option = {
		name = imperial_examination.1060.d
		stress_impact = { base = minor_stress_impact_loss }
		ai_chance = {
			base = 100
		}
	}
}

scripted_effect favored_education_focus_effect = {
	scope:activity = {
		every_guest_subset = {
			name = entrants
			limit = { has_trait = $FAVORED_EDUCATION$ }
			imperial_examination_adjust_advantage_variable_effect = yes
		}
	}
}

# Emperor: A Most Favorable Candidate
imperial_examination.1070 = {
	type = activity_event
	title = imperial_examination.1070.t
	desc = imperial_examination.1070.desc

	theme = imperial_examination
	override_background = { reference = study }
	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = pondering
	}

	trigger = {
		this = scope:host
	}

	option = { # Those who share my education focus have superior talent.
		name = imperial_examination.1070.a
		custom_tooltip = every_shared_education_entrant.tt
		trigger = {
			has_activity_intent = exam_recruit_courtiers_intent
		}
		switch = {
			trigger = has_trait
			education_martial = {
				favored_education_focus_effect = { FAVORED_EDUCATION = education_martial }
			}
			education_intrigue = {
				favored_education_focus_effect = { FAVORED_EDUCATION = education_intrigue }
			}
			education_learning = {
				favored_education_focus_effect = { FAVORED_EDUCATION = education_learning }
			}
			education_stewardship = {
				favored_education_focus_effect = { FAVORED_EDUCATION = education_stewardship }
			}
			education_diplomacy = {
				favored_education_focus_effect = { FAVORED_EDUCATION = education_diplomacy }
			}
		}
		add_prestige = medium_prestige_gain

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_rationality = 0.5
			}
		}
	}

	option = { # All who abide by the Confucian creed are the most worthy.
		name = imperial_examination.1070.b
		custom_tooltip = imperial_examination.1070.b.tt
		trigger = {
			has_activity_intent = exam_recruit_courtiers_intent
		}
		scope:activity = {
			every_guest_subset = {
				name = entrants
				limit = {
					OR = {
						has_trait = compassionate
						has_trait = just
						has_trait = honest
						has_trait = scholar
					}
				}
				imperial_examination_adjust_advantage_variable_effect = yes
			}
		}
		add_piety = medium_piety_gain

		stress_impact = {
			compassionate = minor_stress_impact_loss
			just = minor_stress_impact_loss
			honest = minor_stress_impact_loss
			scholar = minor_stress_impact_loss
			callous = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_compassion = 1
			}
			modifier = {
				add = 20
				OR = {
					has_trait = compassionate
					has_trait = just
					has_trait = honest
					has_trait = scholar
				}
			}
			modifier = {
				add = -40
				has_trait = callous
			}
		}
	}

	option = { # Those who are descendants of the son of heaven, of course.
		name = imperial_examination.1070.d
		custom_tooltip = imperial_examination.1070.d.tt
		trigger = {
			has_activity_intent = exam_give_favor_intent
		}
		scope:activity = {
			every_guest_subset = {
				name = entrants
				limit = {
					dynasty ?= root.dynasty
				}
				imperial_examination_adjust_advantage_variable_effect = yes
			}
		}
		dynasty = {
			add_dynasty_prestige = minor_dynasty_prestige_gain
		}

		stress_impact = {
			honest = medium_stress_impact_gain
			just = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
				ai_vengefulness = 1
			}
			modifier = {
				add = -40
				OR = {
					has_trait = honest
					has_trait = just
				}
			}
		}
	}

	option = { # I will defer to the examiners' judgement.
		name = imperial_examination.1070.c
		stress_impact = {
			base = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}
}

#######################
### EMPEROR PREP EVENTS
#######################
# imperial_examination.2000 - Emperor sneaks out to meet entrants
# imperial_examination.2010 - Emperor seeks out dynasty members
# imperial_examination.2030 - Emperor discovers promising lowborn
# imperial_examination.2040 - One of the examiners is taking bribes...?

#Emperor sneaks out to meet entrants
imperial_examination.2000 = {
	type = activity_event
	title = imperial_examination.2000.t
	desc = {
		desc = imperial_examination.2000.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:entrant_to_meet = { is_in_guest_subset = { name = scoreboard } }
				}
				desc = imperial_examination.2000.desc.scoreboardee
			}
			desc = imperial_examination.2000.desc.fallback
		}
	}
	theme = imperial_examination
	override_background = { reference = tavern }
	cooldown = { months = 6 }
	left_portrait = {
		character = root
		animation = scheme
	}
	right_portrait = {
		character = scope:entrant_to_meet
		animation = happiness
	}
	trigger = {
		#Are we in the correct subset
		this = scope:host
		scope:activity = {
			has_activity_type = activity_imperial_examination
			any_guest_subset = {
				name = entrants
				count >= 1
				is_ai = yes
				NOR = {
					# This is specifically a character who you don't already have close to you.
					is_close_or_extended_family_of = root
					is_in_list = met_entrants
				}
			}
			any_guest_subset = {
				name = imperial_examiners
				count >= 1
			}
		}
		has_activity_intent = exam_recruit_courtiers_intent
	}
	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					is_ai = yes
				}
				save_scope_as = examiner
			}
			random_guest_subset = {
				name = entrants
				limit = {
					is_in_guest_subset = { name = scoreboard }
					is_ai = yes
					NOR = {
						is_close_or_extended_family_of = root
						is_in_list = met_entrants
					}
				}
				save_scope_as = entrant_to_meet
			}
			if = {
				limit = {
					NOT = { exists = scope:entrant_to_meet }
				}
				random_guest_subset = {
					name = entrants
					limit = {
						is_ai = yes
						NOR = {
							is_close_or_extended_family_of = root
							is_in_list = met_entrants
						}
					}
					save_scope_as = entrant_to_meet
				}
			}
		}
		scope:entrant_to_meet = { add_to_list = met_entrants }
	}

	option = { # I shall let the examiners know their name.
		name = imperial_examination.2000.a
		custom_tooltip = imperial_examination.2000.a.tt
		scope:entrant_to_meet = {
			imperial_examination_adjust_advantage_variable_effect = yes
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_sociability = 1
			}
		}
	}

	option = { # The entrant might go far, with some help.
		name = imperial_examination.2000.b
		trigger = {
			scope:examiner = {
				is_pool_character = yes
				merit_level > scope:entrant_to_meet.merit_level
			}
		}
		set_elder_relation_effect = {
			ELDER = scope:examiner
			DISCIPLE = scope:entrant_to_meet
			MERIT = minor_merit_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
		}
	}

	option = { # Alas, the entrant does not meet my expectations.
		name = imperial_examination.2000.c
		add_prestige = minor_prestige_gain
		stress_impact = {
			callous = medium_stress_impact_loss
			ambitious = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 1
			}
			modifier = {
				add = 40
				OR = {
					has_trait = ambitious
					has_trait = callous
					scope:activity = {
						has_activity_option = {
							category = imperial_examination_passing_grade
							option = imperial_examination_passing_grade_extreme
						}
					}
				}
			}
		}
	}
}

#Emperor seeks out dynasty members
imperial_examination.2010 = {
	type = activity_event
	title = imperial_examination.2010.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					AND = {
						exists = scope:entrant_to_meet
						exists = scope:entrant_to_meet_2
					}
				}
				desc = imperial_examination.2010.desc.two
			}
			desc = imperial_examination.2010.desc.one
		}
		desc = imperial_examination.2010.desc.outro
	}
	theme = imperial_examination
	override_background = { reference = corridor_day }
	cooldown = { months = 6 }
	left_portrait = {
		character = scope:examiner
		triggered_animation = {
			trigger = {
				NOT = { exists = scope:entrant_to_meet_2 }
			}
			animation = happy_teacher
		}
		animation = stressed_teacher
	}
	right_portrait = {
		character = root
		animation = scheme
	}
	trigger = {
		#Are we in the correct subset
		this = scope:host
		has_activity_intent = exam_give_favor_intent
		scope:activity = {
			any_guest_subset = {
				name = entrants
				is_ai = yes
				dynasty ?= root.dynasty
				NOT = { is_in_list = met_entrants }
			}
			NOT = {
				any_guest_subset = {
					name = imperial_examiners
					has_trait = honest
					count = all
				}
			}
		}
	}
	immediate = {
		#Grab a reasonably good dynasty member.
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					is_ai = yes
					dynasty ?= root.dynasty
					OR = {
						probably_intelligent_trigger = yes
						learning >= mediocre_skill_rating
					}
					NOT = { is_in_list = met_entrants }
				}
				save_scope_as = entrant_to_meet
				save_scope_as = dynasty_member
				add_to_list = met_entrants
			}
			#Grab a ... "less good" dynasty member.
			random_guest_subset = {
				name = entrants
				limit = {
					is_ai = yes
					dynasty ?= root.dynasty
					OR = {
						probably_unintelligent_trigger = yes
						learning < mediocre_skill_rating
					}
					NOT = { is_in_list = met_entrants }
				}
				save_scope_as = entrant_to_meet_2
				save_scope_as = dynasty_member
				add_to_list = met_entrants
			}
		}
		scope:activity = {
			#Grab an examiner who might be open to... suggestions
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					is_ai = yes
				}
				weight = {
					base = 1
					ai_value_modifier = {
						ai_honor = -1
						ai_greed = 1
					}
					modifier = {
						has_trait = greedy
						add = 2
					}
					modifier = {
						has_trait = schemer
						add = 2
					}
					modifier = {
						has_trait = honest
						add = -5
					}
				}
				save_scope_as = examiner
			}
		}
	}
	#Give the promising dynasty member an advantage.
	option = {
		name = imperial_examination.2010.a
		custom_tooltip = imperial_examination.2010.a.tt
		trigger = { exists = scope:entrant_to_meet }
		scope:entrant_to_meet = {
			imperial_examination_adjust_advantage_variable_effect = yes
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:entrant_to_meet
					type = obligation_hook
				}
			}
			add_hook = {
				type = obligation_hook
				target = scope:entrant_to_meet
			}
		}
		pay_short_term_gold = {
			target = scope:examiner
			gold = tiny_gold_value
		}
		stress_impact = {
			greedy = medium_stress_impact_gain
			ambitious = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_greed = -1
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
		}
	}
	#Give the disappointing dynasty member an advantage.
	option = {
		name = imperial_examination.2010.b
		custom_tooltip = imperial_examination.2010.b.tt
		trigger = { exists = scope:entrant_to_meet_2 }
		scope:entrant_to_meet_2 = {
			imperial_examination_adjust_advantage_variable_effect = yes
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:entrant_to_meet_2
					type = obligation_hook
				}
			}
			add_hook = {
				type = obligation_hook
				target = scope:entrant_to_meet_2
			}
		}
		pay_short_term_gold = {
			target = scope:examiner
			gold = tiny_gold_value
		}
		stress_impact = {
			greedy = medium_stress_impact_gain
			ambitious = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_compassion = 1
				ai_greed = -1
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
		}
	}
	#I think not.
	option = {
		name = imperial_examination.2010.c

		stress_impact = {
			shy = medium_stress_impact_loss
			honest = medium_stress_impact_loss
			diligent = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
				ai_greed = 1
			}
		}
	}
}

scripted_effect find_interesting_pool_character = {
	# This effect adds a lowborn character to the examination, based on the examination type
	# First we need to know what skills we are looking for
	scope:activity = {
		if = {
			limit = {
				has_activity_option = {
					category = special_type
					option = imperial_examination_focus_learning
				}
			}
			sort_interesting_pool_character = { EXAM_TYPE = learning }
		}
		else_if = {
			limit = {
				has_activity_option = {
					category = special_type
					option = imperial_examination_focus_stewardship
				}
			}
			sort_interesting_pool_character = { EXAM_TYPE = stewardship }
		}
		else = {
			sort_interesting_pool_character = { EXAM_TYPE = martial }
		}
	}
}

scripted_effect sort_interesting_pool_character = {
	if = {
		# Are there any available pool characters who meet our criteria?
		limit = {
			any_pool_character = {
				is_lowborn = yes
				province = scope:activity.activity_location
				is_available_healthy_ai_adult = yes
				trigger_if = {
					limit = {
						scope:host = {
							faith = { has_doctrine = doctrine_gender_female_dominated }
						}
					}
					is_female = yes
				}
				trigger_else_if = {
					limit = {
						scope:host = {
							faith = { has_doctrine = doctrine_gender_male_dominated }
						}
					}
					is_female = no
				}
				trigger_else = { always = yes }
				NOR = {
					is_participant_in_activity = scope:activity
					has_trait = devoted
					has_trait = eunuch
				}
			}
		}
		ordered_pool_character = {
			limit = {
				is_lowborn = yes
				is_available_healthy_ai_adult = yes
				trigger_if = {
					limit = {
						scope:host = {
							faith = { has_doctrine = doctrine_gender_female_dominated }
						}
					}
					is_female = yes
				}
				trigger_else_if = {
					limit = {
						scope:host = {
							faith = { has_doctrine = doctrine_gender_male_dominated }
						}
					}
					is_female = no
				}
				trigger_else = { always = yes }
				NOR = {
					is_participant_in_activity = scope:activity
					has_trait = devoted
					has_trait = eunuch
				}
			}
			province = scope:activity.activity_location
			order_by = $EXAM_TYPE$
			save_scope_as = promising_lowborn
		}
	}
	else = {
		# If no pool character is available, we create one
		create_character = {
			template = tgp_$EXAM_TYPE$_exam_entrant_template
			location = scope:activity.activity_location
			save_scope_as = promising_lowborn
		}
	}
	# Finally, add the character to the activity, set their score, and change nr of entrants.
	scope:promising_lowborn = {
		add_to_activity = scope:activity
		scope:activity = {
			add_to_guest_subset = {
				name = entrants
				target = scope:promising_lowborn
			}
			#Record the initial amount of entrants
			change_variable = {
				name = num_initial_entrants
				add = 1
			}
		}
		set_variable = {
			name = imperial_examination_score
			value = imperial_examination_score_value
		}
	}
}

#Emperor discovers promising lowborn
imperial_examination.2030 = {
	type = activity_event
	title = imperial_examination.2030.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_option = {
							category = imperial_examination_breadth
							option = imperial_examination_breadth_open
						}
					}
				}
				desc = imperial_examination.2030.desc.open
			}
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_option = {
							category = imperial_examination_breadth
							option = imperial_examination_breadth_restricted
						}
					}
				}
				desc = imperial_examination.2030.desc.restricted
			}
			desc = imperial_examination.2030.desc.exclusive
		}
		desc = imperial_examination.2030.desc.outro
	}

	theme = imperial_examination
	override_background = { reference = study }
	cooldown = { months = 6 }

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				is_ai = no
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = japanese_war_fan
		}
		animation = fanning
	}

	right_portrait = {
		character = scope:examiner
		animation = debating
	}

	lower_right_portrait = scope:promising_lowborn

	trigger = {
		is_ai = no
		#Are we in the correct subset
		this = scope:host
		scope:activity = {
			has_activity_type = activity_imperial_examination
			any_guest_subset = {
				name = imperial_examiners
				count >= 1
				is_ai = yes
			}
			# This event adds a character to the activity, so we need to check to see if the activity guest list is full
			OR = {
				AND = {
					has_activity_option = {
						category = imperial_examination_breadth
						option = imperial_examination_breadth_exclusive
					}
					any_attending_character = {
						count < exclusive_exam_guests_full_calc
					}
				}
				AND = {
					has_activity_option = {
						category = imperial_examination_breadth
						option = imperial_examination_breadth_restricted
					}
					any_attending_character = {
						count < restricted_exam_guests_full_calc
					}
				}
				AND = {
					has_activity_option = {
						category = imperial_examination_breadth
						option = imperial_examination_breadth_open
					}
					any_attending_character = {
						count < open_exam_guests_full_calc
					}
				}
			}
		}
	}

	immediate = {
		find_interesting_pool_character = yes
		scope:activity = {
			if = {
				limit = {
					any_guest_subset = {
						name = entrants
						is_ai = yes
						dynasty ?= root.dynasty
					}
				}
				random_guest_subset = {
					name = entrants
					limit = {
						is_ai = yes
						dynasty ?= root.dynasty
					}
					save_scope_as = dynasty_member
				}
			}
		}
		# Grab an examiner to front the lowborn
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = { is_ai = yes }
				save_scope_as = examiner
			}
		}
	}

	option = { # I will not allow a lowborn to outshine my own kin!
		name = imperial_examination.2030.a
		custom_tooltip = imperial_examination.2030.a.tt
		trigger = {
			has_activity_intent = exam_give_favor_intent
			scope:activity = {
				any_guest_subset = {
					name = entrants
					is_ai = yes
					dynasty ?= root.dynasty
				}
			}
		}
		scope:dynasty_member = {
			imperial_examination_adjust_advantage_variable_effect = yes
			add_stress = medium_stress_gain
		}
		dynasty = {
			add_dynasty_prestige = minor_dynasty_prestige_gain
		}
		stress_impact = {
			just = medium_stress_impact_gain
			callous = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
			}
			modifier = {
				add = -40
				has_trait = just
			}
			modifier = {
				add = 40
				has_trait = callous
			}
		}
	}

	option = { # I will keep an eye on this one
		name = imperial_examination.2030.b
		trigger = { has_activity_intent = exam_recruit_courtiers_intent }
		scope:promising_lowborn = {
			add_character_modifier = {
				modifier = tgp_emperors_notice_modifier
				years = 12
			}
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
			}
		}
	}

	option = { # Truly, everyone is equal beneath the heavens
		name = imperial_examination.2030.c
		add_legitimacy = minor_legitimacy_gain

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
		}
	}
}

#One of the examiners is taking bribes...?
imperial_examination.2040 = {
	type = activity_event
	title = imperial_examination.2040.t
	desc = imperial_examination.2040.desc
	theme = imperial_examination
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:rumored_examiner
		animation = worry
	}
	cooldown = { months = 6 }
	trigger = {
		#Are we in the correct subset
		this = scope:host
		scope:activity = {
			any_guest_subset = {
				name = imperial_examiners
				count >= 1
				is_ai = yes
			}
			any_guest_subset = {
				name = entrants
				has_variable = has_bribed_examiner
			}
		}
	}
	weight_multiplier = {
		base = 1
		modifier = {
			scope:activity = {
				any_guest_subset = {
					name = entrants
					has_variable = has_bribed_examiner
					count = all
				}
			}
			add = 1
		}
	}
	immediate = {
		#Grab an existing character, based on Exam Focus
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					this != root
					is_ai = yes
				}
				weight = {
					base = 1
					ai_value_modifier = {
						ai_honor = -1
						ai_greed = 1
					}
					modifier = {
						has_trait = greedy
						add = 2
					}
					modifier = {
						has_trait = schemer
						add = 2
					}
					modifier = {
						has_trait = honest
						add = -5
					}
				}
				save_scope_as = rumored_examiner
			}
			if = {
				limit = {
					any_guest_subset = {
						name = entrants
						is_ai = yes
						dynasty ?= root.dynasty
					}
				}
				random_guest_subset = {
					name = entrants
					limit = {
						this != root
						is_ai = yes
						dynasty ?= root.dynasty
					}
					save_scope_as = dynasty_member
				}
			}

		}
	}
	option = { # The examiner will pay recompense to the empire.
		name = imperial_examination.2040.a
		scope:rumored_examiner = {
			remove_short_term_gold = medium_gold_value
		}
		add_treasury = scope:rumored_examiner.medium_gold_value

		stress_impact = {
			compassionate = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1
			}
			modifier = {
				add = 40
				has_trait = compassionate
			}
		}
	}

	option = { # I know someone who could use some _assistance_...
		name = imperial_examination.2040.b
		custom_tooltip = imperial_examination.2040.b.tt
		trigger = {
			has_activity_intent = exam_give_favor_intent
			scope:activity = {
				any_guest_subset = {
					name = entrants
					is_ai = yes
					dynasty ?= root.dynasty
				}
			}
		}
		scope:dynasty_member = {
			imperial_examination_adjust_advantage_variable_effect = yes
		}

		stress_impact = {
			just = medium_stress_impact_gain
			honest = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
			}
			modifier = {
				add = -40
				OR = {
					has_trait = just
					has_trait = honest
				}
			}
		}
	}

	option = { # This disgrace cannot go unpunished!
		name = imperial_examination.2040.d
		imprison = {
			target = scope:rumored_examiner
			type = dungeon
		}
		add_dread = minor_dread_gain
		scope:rumored_examiner = {
			if = {
				limit = { involved_activity ?= scope:activity }
				remove_from_activity = scope:activity
			}
			examination_clear_timeout_effect = yes
		}
		stress_impact = {
			arbitrary = medium_stress_impact_loss
			sadistic = medium_stress_impact_loss
			callous = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
			}
			modifier = {
				add = 20
				OR = {
					has_trait = arbitrary
					has_trait = sadistic
					has_trait = callous
				}
			}
		}
	}

	#The examination will proceed as usual
	option = {
		name = imperial_examination.2040.c
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}

	after = {
		if = {
			limit = {
				scope:rumored_examiner = {
					is_imprisoned = yes
				}
			}
			imperial_examination_create_appropriate_examiner_effect = yes
			if = {
				limit = { exists = scope:activity }
				scope:new_examiner = { add_to_activity = scope:activity }
				scope:activity = {
					add_to_guest_subset = {
						name = imperial_examiners
						target = scope:new_examiner
					}
				}
			}
		}
	}
}

#######################
### EMPEROR EXAM EVENTS
#######################
# imperial_examination.3000 - Emperor start exam event
# imperial_examination.3005 - Guest (Candidate Supporter) start exam event
# imperial_examination.3006 - Guest (Exam Supporter) start exam event
# imperial_examination.3007 - Entrant start exam event
# imperial_examination.3010 - Did an entrant just attempt to cheat?
# imperial_examination.3020 - Dynasty member fails horribly
# imperial_examination.3030 - Examiner falls asleep?!

#Emperor start exam event
imperial_examination.3000 = {
	type = activity_event
	title =  imperial_examination.3000.t
	desc = imperial_examination.3000.desc
	theme = imperial_examination
	override_background = { reference = courtyard }
	override_effect_2d = { reference = legend_glow }
	left_portrait = {
		character = root
		animation = personality_content
	}
	#Let's go!
	option = {
		name = imperial_examination.3000.a
		add_prestige = {
			value = miniscule_prestige_gain
			add = imperial_examination_num_examinees_value
			multiply = 0.2
		}
	}
}

#Guest start exam event - candidate supporter
imperial_examination.3005 = {
	type = activity_event
	title = imperial_examination.3005.t
	desc = {
		desc = imperial_examination.3005.desc
		first_valid = {
			triggered_desc = {
				# Doesn't like to wait
				trigger = {
					OR = {
						has_trait = impatient
						has_trait = stubborn
					}
				}
				desc = imperial_examination.3005.desc.outro.impatient
			}
			desc = imperial_examination.3005.desc.outro
		}
	}
	theme = imperial_examination
	override_background = { reference = tavern }

	left_portrait = {
		character = root
		animation = pondering
	}

	right_portrait = {
		trigger = { exists = scope:concerned_parent }
		character = scope:concerned_parent
		animation = worry
	}

	trigger = {
		# Family member/spouse/heir/disciple is available
		scope:activity = {
			any_attending_character = {
				is_in_guest_subset = { name = entrants }
				OR = {
					is_close_family_of = root
					has_relation_disciple = root
					this = root.primary_spouse
				}
			}
		}
	}

	immediate = {
		set_variable = {
			name = num_final_candidates
			value = 0
		}
		scope:activity = {
			# How many family members/disciples do we have present at the activity?
			every_attending_character = {
                limit = {
					OR = {
						is_close_family_of = root
						has_relation_disciple = root
						this = root.primary_spouse
					}
                }
                root = {
	                change_variable = {
	                	name = num_final_candidates
	                	add = 1
	                }
                }
			}
		}
		scope:activity = {
			if = {
				limit = {
					any_attending_character = {
						is_in_guest_subset = { name = guests }
						this != root
						this != host
						OR = {
							has_activity_intent = local_exam_support_entrants_intent
							has_activity_intent = imperial_exam_support_entrants_intent
						}
					}
				}
				random_attending_character = {
					limit = {
						is_in_guest_subset = { name = guests }
						this != root
						this != host
						OR = {
							has_activity_intent = local_exam_support_entrants_intent
							has_activity_intent = imperial_exam_support_entrants_intent
						}
					}
					save_scope_as = concerned_parent
				}
			}
		}
	}

	option = { # I should mingle
		name = imperial_examination.3005.b
		trigger = {
			scope:activity = {
				any_attending_character = {
					is_in_guest_subset = { name = guests }
					this != root
					this != host
					OR = {
						has_activity_intent = local_exam_support_entrants_intent
						has_activity_intent = imperial_exam_support_entrants_intent
					}
				}
			}
		}
		scope:activity = {
			every_attending_character = {
				limit = {
					is_in_guest_subset = { name = guests }
					this != root
					this != host
					OR = {
						has_activity_intent = local_exam_support_entrants_intent
						has_activity_intent = imperial_exam_support_entrants_intent
					}
				}
				custom = custom.every_entrant_supporter
				add_opinion = {
					target = scope:host
					modifier = impressed_opinion
					opinion = 5
				}
			}
		}
	}

	option = { # Fingers crossed...
		name = imperial_examination.3005.a
		add_prestige = {
			value = miniscule_prestige_gain
			add = var:num_final_candidates
			multiply = 0.8
		}
		ai_chance = {
			base = 100
		}
	}

	after = {
		remove_variable = num_final_candidates
	}
}

#Guest start exam event - exam supporter
imperial_examination.3006 = {
	type = activity_event
	title = imperial_examination.3006.t
	desc = {
		desc = imperial_examination.3006.desc
		first_valid = {
			triggered_desc = {
				# Doesn't like to wait
				trigger = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_martial
						}
					}
				}
				desc = imperial_examination.3005.desc.martial
			}
			desc = imperial_examination.3005.desc.scholar
		}
	}

	theme = imperial_examination

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = marshal
		}
		animation = holding_scrolls
	}

	right_portrait = {
		character = scope:examiner
		triggered_animation = {
			trigger = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = japanese_war_fan
		}
		animation = happy_teacher
	}

	override_background = {
		trigger = {
			scope:activity = {
				has_activity_type = activity_imperial_examination
				has_activity_option = {
					category = special_type
					option = imperial_examination_focus_martial
				}
			}
		}
		reference = courtyard
	}

	immediate = {
		imperial_examination_fetch_examiner_effect = yes
	}

	option = { # Let's go!
		name = imperial_examination.3006.a
		change_merit = miniscule_merit_gain
		ai_chance = {
			base = 100
		}
	}
}

#Entrant start exam event - exam supporter
imperial_examination.3007 = {
	type = activity_event
	title = imperial_examination.3007.t
	desc = imperial_examination.3007.desc

	theme = imperial_examination

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = marshal
		}
		animation = holding_scrolls
	}

	right_portrait = {
		character = scope:examiner
		triggered_animation = {
			trigger = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = japanese_war_fan
		}
		animation = happy_teacher
	}

	override_background = {
		trigger = {
			scope:activity = {
				has_activity_type = activity_imperial_examination
				has_activity_option = {
					category = special_type
					option = imperial_examination_focus_martial
				}
			}
		}
		reference = courtyard
	}

	immediate = {
		imperial_examination_fetch_examiner_effect = yes
	}

	option = { # Let's go!
		name = imperial_examination.3007.a
		custom_description_no_bullet = {
			text = imperial_examination.3007.a.success_tooltip
		}
		ai_chance = {
			base = 100
		}
	}
}

#Did an entrant just attempt to cheat?
imperial_examination.3010 = {
	type = activity_event
	title = imperial_examination.3010.t
	desc = {
		desc = imperial_examination.3010.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_martial
						}
					}
				}
				desc = imperial_examination.3010.martial
			}
			desc = imperial_examination.3010.scholarship
		}
		desc = imperial_examination.3010.outro
	}

	theme = imperial_examination

	override_background = { reference = study }

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = disappointed
	}

	center_portrait = {
		character = scope:examiner
		animation = interested
	}

	right_portrait = {
		character = scope:examiner_2
		animation = debating
	}

	lower_right_portrait = {
		character = scope:entrant
	}

	trigger = {
		this = scope:host
		has_activity_intent = exam_recruit_courtiers_intent
		scope:activity = {
			any_guest_subset = {
				name = imperial_examiners
				count >= 2
				is_ai = yes
			}
			any_guest_subset = {
				name = entrants
				is_ai = yes
				intrigue > high_skill_rating
				NOR = {
					is_in_list = met_entrants
					# We don't want anyone who has an actual shot at passing the exam - we will assume they have no reason to cheat
					is_in_guest_subset = { name = scoreboard }
					# We don't want a close or distant family member - this is an event for recruiting a courtier
					is_close_or_extended_family_of = root
				}
			}
		}
	}

	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					is_ai = yes
					intrigue > high_skill_rating
					NOR = {
						is_in_list = met_entrants
						# We don't want anyone who has an actual shot at passing the exam - we will assume they have no reason to cheat
						is_in_guest_subset = { name = scoreboard }
						# We don't want a close or distant family member - this is an event specifically for recruiting a courtier
						is_close_or_extended_family_of = root
					}
				}
				save_scope_as = entrant
			}
			random_guest_subset = {
				name = imperial_examiners
				limit = { is_ai = yes }
				save_scope_as = examiner
			}
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					is_ai = yes
					NOT = { this = scope:examiner }
				}
				save_scope_as = examiner_2
			}
			remove_from_guest_subset = {
				name = entrants
				target = scope:entrant
			}
			add_to_guest_subset = {
				name = failees
				target = scope:entrant
			}
		}
		scope:entrant = { add_to_list = met_entrants }
	}

	option = { # The entrant can redeem themselves in my service.
		name = imperial_examination.3010.a
		add_courtier = scope:entrant
		add_hook = {
			type = obligation_hook
			target = scope:entrant
		}

		stress_impact = {
			compassionate = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_compassion = 1
			}
			modifier = {
				add = 40
				has_trait = compassionate
			}
		}
	}

	option = { # The examiner is the one who needs to be punished!
		name = imperial_examination.3010.b
		imprison = {
			target = scope:examiner
			type = dungeon
		}
		scope:examiner = {
			if = {
				limit = { involved_activity ?= scope:activity }
				remove_from_activity = involved_activity
			}
			examination_clear_timeout_effect = yes
		}
		add_dread = minor_dread_gain

		stress_impact = {
			arbitrary = medium_stress_impact_loss
			sadistic = medium_stress_impact_loss
			callous = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
			}
			modifier = {
				add = 20
				OR = {
					has_trait = arbitrary
					has_trait = sadistic
					has_trait = callous
				}
			}
		}
	}

	option = { # I will let the examiners work this out.
		name = imperial_examination.3010.c
		reverse_add_opinion = {
			target = scope:examiner
			modifier = pleased_opinion
			opinion = 5
		}
		reverse_add_opinion = {
			target = scope:examiner_2
			modifier = pleased_opinion
			opinion = 5
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}
	after = {
		if = {
			limit = {
				scope:examiner = { is_imprisoned = yes }
			}
			imperial_examination_create_appropriate_examiner_effect = yes
			add_to_activity = scope:new_examiner
			scope:activity = {
				add_to_guest_subset = {
					name = imperial_examiners
					target = scope:new_examiner
				}
			}
		}
	}
}

#Dynasty member fails horribly
imperial_examination.3020 = {
	type = activity_event
	title = imperial_examination.3020.t
	desc = {
		desc = imperial_examination.3020.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_martial
						}
					}
				}
				desc = imperial_examination.3020.desc_martial
			}
			desc = imperial_examination.3020.desc_scholarship
		}
		desc = imperial_examination.3020.desc.outro
	}

	theme = imperial_examination

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = disapproval
	}

	right_portrait = {
		character = scope:entrant
		animation = delirium
	}

	trigger = {
		this = scope:host
		has_activity_intent = exam_give_favor_intent
		scope:activity = {
			any_guest_subset = {
				name = entrants
				is_ai = yes
				dynasty ?= root.dynasty
				has_variable = imperial_examination_score
				var:imperial_examination_score < imperial_examination_decent_score_value
			}
		}
	}

	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					is_ai = yes
					dynasty ?= root.dynasty
					has_variable = imperial_examination_score
					var:imperial_examination_score < imperial_examination_decent_score_value
				}
				save_scope_as = entrant
			}
		}
	}

	option = { # They will get a stern talking to
		name = imperial_examination.3020.a
		scope:entrant = {
			add_character_modifier = {
				modifier = tgp_strict_supervision_modifier
				years = 10
			}
		}

		stress_impact = {
			just = medium_stress_impact_loss
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
			modifier = {
				add = 40
				has_trait = just
			}
		}
	}

	option = { # Send them home immediately!
		name = imperial_examination.3020.b
		scope:activity = {
			remove_from_guest_subset = {
				name = entrants
				target = scope:entrant
			}
			add_to_guest_subset = {
				name = failees
				target = scope:entrant
			}
		}
		reverse_add_opinion = {
			target = scope:entrant
			modifier = insult_opinion
			opinion = -25
		}

		stress_impact = {
			callous = medium_stress_impact_loss
			compassionate = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 1
			}
			modifier = {
				add = 40
				has_trait = callous
			}
			modifier = {
				add = -40
				has_trait = compassionate
			}
		}
	}

	option = { # Ah, this is a heatstroke, nothing more.
		name = imperial_examination.3020.c
		reverse_add_opinion = {
			target = scope:entrant
			modifier = pleased_opinion
			opinion = 10
		}

		stress_impact = {
			trusting = medium_stress_impact_loss
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 40
				has_trait = trusting
			}
		}
	}
}

#Examiner falls asleep during examination?!
imperial_examination.3030 = {
	type = activity_event
	title = imperial_examination.3030.t
	desc = imperial_examination.3030.desc

	theme = imperial_examination

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = disbelief
	}

	right_portrait = {
		character = scope:examiner
		animation = boredom
	}

	trigger = {
		this = scope:host
		scope:activity = {
			any_guest_subset = {
				name = imperial_examiners
				count >= 1
				is_ai = yes
			}
		}
	}

	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					is_ai = yes
				}
				weight = {
					base = 1
					modifier = {
						has_trait = lazy
						add = 2
					}
					modifier = {
						has_trait = disloyal
						add = 2
					}
					modifier = {
						has_trait = diligent
						add = -5
					}
					modifier = {
						has_trait = ambitious
						add = -5
					}
				}
				save_scope_as = examiner
			}
		}
	}

	option = { # This calls every result into question!
		name = imperial_examination.3030.a
		trigger = { has_activity_intent = exam_recruit_courtiers_intent }
		flavor = imperial_examination.3030.a.flavor
		scope:activity = {
			every_guest_subset = {
				name = entrants
				custom = custom.every_entrant
				change_merit = medium_merit_loss
			}
		}
		add_legitimacy = minor_legitimacy_gain

		stress_impact = {
			zealous = medium_stress_impact_loss
			paranoid = medium_stress_impact_loss
			trusting = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 1
			}
			modifier = {
				add = 40
				OR = {
					has_trait = zealous
					has_trait = paranoid
				}
			}
			modifier = {
				add = -40
				has_trait = trusting
			}
		}
	}

	option = { # I will remember this
		name = imperial_examination.3030.b
		trigger = { has_activity_intent = exam_give_favor_intent }
		add_hook = {
			type = obligation_hook
			target = scope:examiner
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
			}
		}
	}

	option = { # No one noticed but us, surely.
		name = imperial_examination.3030.c
		add_prestige = minor_prestige_loss
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}
}

#######################
### ENTRANT PREP EVENTS
#######################
# imperial_examination.4000 - Hire a stand-in to take the exam for you
# imperial_examination.4010 - Bribe examiner
# imperial_examination.4020 - Stay up studying
# imperial_examination.4030 - Study with a friend
# imperial_examination.4050 - Go out partying
# imperial_examination.4060 - A potential rival appears
# imperial_examination.4070 - Unsolicited Family Support
# imperial_examination.4080 - Two examiners disagree on the classics
# imperial_examination.4090 - Bond with likeminded entrant
# imperial_examination.4100 - Try to sneak a tiny answer book inside examination hall

#Hire a stand-in to take the exam for you
imperial_examination.4000 = {
	type = activity_event
	title = imperial_examination.4000.t
	desc = imperial_examination.4000.desc
	theme = imperial_examination
	override_background = { reference = corridor_night }
	left_portrait = {
		character = root
		animation = stress
	}
	right_portrait = {
		character = scope:exam_taker
		triggered_animation = {
			trigger = {
				root = { has_variable = been_caught_cheating_longterm }
			}
			animation = dismissal
		}
		animation = admiration
	}
	cooldown = { months = 6 }
	trigger = {
		is_ai = no
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		NOT = { has_variable = has_exam_taker }
		has_character_flag = exam_cheat_intent
	}
	immediate = {
		#For ease of access
		save_scope_as = current_entrant
		#Grab an existing character, based on Exam Focus
		random_pool_character = {
			province = root.location
			limit = {
				is_available_imperial_examiner_trigger = { ATTENDING_CHAR = scope:current_entrant }
				#It's not one of our dedicated examiners
				save_temporary_scope_as = char_check
				NOT = {
					scope:activity = {
						is_target_in_variable_list = {
							name = imperial_examiners_list
							target = scope:char_check
						}
					}
				}
			}
			save_scope_as = exam_taker
		}
		#Otherwise create someone appropriate
		if = {
			limit = {
				NOT = { exists = scope:exam_taker }
			}
			if = {
				limit = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_martial_focus_trigger = yes
					}
				}
				create_character = {
					template = master_of_horse_court_position_template
					location = root.location
					culture = root.location.culture
					faith = root.location.faith
					save_scope_as = exam_taker
				}
			}
			else_if = {
				limit = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_stewardship_focus_trigger = yes
					}
				}
				create_character = {
					template = travel_leader_court_position_template
					location = root.location
					culture = root.location.culture
					faith = root.location.faith
					save_scope_as = exam_taker
				}
			}
			else = {
				create_character = {
					template = court_tutor_court_position_template
					location = root.location
					culture = root.location.culture
					faith = root.location.faith
					learning = { medium_skill_rating high_skill_rating }
					save_scope_as = exam_taker
				}
			}
		}
	}
	#Sign me up!
	option = {
		name = imperial_examination.4000.a
		trigger = {
			NOT = { has_variable = been_caught_cheating_longterm }
		}
		custom_tooltip = imperial_examination.cheat.tt
		set_variable = {
			name = has_exam_taker
			value = scope:exam_taker
		}
		pay_short_term_gold = {
			target = scope:exam_taker
			gold = medium_gold_value
		}
		ai_chance = {
			base = 50
			modifier = {
				OR = {
					NOT = { short_term_gold >= medium_gold_value }
					has_trait = greedy
					has_variable = been_caught_cheating_longterm
				}
				factor = 0
			}
			ai_value_modifier = {
				ai_boldness = 1
			}
		}
	}
	#Let's not
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_variable = been_caught_cheating_longterm }
						desc = imperial_examination.4000.b.cheater
					}
					desc = imperial_examination.4000.b
				}
			}
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
		}
	}
}

#Bribe examiner
imperial_examination.4010 = {
	type = activity_event
	title = imperial_examination.4010.t
	desc = imperial_examination.4010.desc
	theme = imperial_examination
	override_background = { reference = corridor_day }
	left_portrait = {
		character = root
		animation = worry
	}
	right_portrait = {
		character = scope:examiner_to_bribe
		triggered_animation = {
			trigger = {
				root = { has_variable = been_caught_cheating_longterm }
			}
			animation = disapproval
		}
		animation = personality_greedy
	}
	cooldown = { months = 6 }
	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		NOT = { has_variable = has_bribed_examiner }
		scope:activity = {
			any_guest_subset = {
				name = imperial_examiners
				count >= 1
				is_ai = yes
				trigger_if = {
					limit = {
						root = { has_variable = discovering_examiner }
					}
					NOT = { this = root.var:discovering_examiner }
				}
				trigger_else = { always = yes }
			}
		}
		has_character_flag = exam_cheat_intent
	}
	immediate = {
		#For ease of access
		save_scope_as = current_entrant
		#Grab an existing character, based on Exam Focus
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					trigger_if = {
						limit = {
							root = { has_variable = discovering_examiner }
						}
						NOT = { this = root.var:discovering_examiner }
					}
					trigger_else = { always = yes }
				}
				weight = {
					base = 1
					modifier = {
						has_trait = greedy
						add = 2
					}
					modifier = {
						has_trait = schemer
						add = 2
					}
					modifier = {
						has_trait = honest
						add = -5
					}
				}
				save_scope_as = examiner_to_bribe
			}
		}
	}
	#Ohh bribing
	option = {
		name = imperial_examination.4010.a
		trigger = {
			NOT = { has_variable = been_caught_cheating_longterm }
		}
		custom_tooltip = imperial_examination.cheat.tt
		set_variable = {
			name = has_bribed_examiner
			value = scope:examiner_to_bribe
		}
		pay_short_term_gold = {
			target = scope:examiner_to_bribe
			gold = medium_gold_value
		}
		ai_chance = {
			base = 50
			modifier = {
				OR = {
					NOT = { short_term_gold >= medium_gold_value }
					has_trait = greedy
					has_variable = been_caught_cheating_longterm
				}
				factor = 0
			}
			ai_value_modifier = {
				ai_boldness = 1
			}
		}
	}
	#Let's not
	option = {
		name = imperial_examination.4010.b

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
		}
	}
}

#Stay up studying
imperial_examination.4020 = {
	type = activity_event
	title = imperial_examination.4020.t
	desc = imperial_examination.4020.desc
	theme = imperial_examination
	override_background = { reference = study }
	center_portrait = {
		character = root
		animation = worry
	}
	cooldown = { months = 6 }
	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			scope:activity = {
				NOT = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		NOT = { has_variable = has_stayed_up_studying }
		has_character_flag = exam_perform_intent
	}
	immediate = {
	}
	#YES let's studyyy
	option = {
		name = imperial_examination.4020.a
		custom_tooltip = imperial_examination.advantage.tt
		imperial_examination_adjust_advantage_variable_effect = yes
		set_variable = has_stayed_up_studying
		stress_impact = {
			base = medium_stress_impact_gain
			lazy = minor_stress_impact_gain
			diligent = minor_stress_impact_loss
		}
		ai_chance = {
			base = 50
			modifier = {
				stress_level >= 1
				factor = 0
			}
		}
	}
	#Let's not
	option = {
		name = imperial_examination.4020.b

		ai_chance = {
			base = 100
		}
	}
}


scripted_trigger imperial_examination_4030_other_entrant_trigger = {
	NOT = { this = root }
	is_ai = yes
}

scripted_trigger imperial_examination_4030_can_be_lovers_trigger = {
	root = {
		NOR = {
			has_trait = chaste
			has_trait = celibate
		}
		has_sexuality = asexual
		OR = {
			can_set_relation_lover_trigger = { CHARACTER = $CHARACTER$ }
			can_set_relation_potential_lover_trigger = { CHARACTER = $CHARACTER$ }
		}
	}
}

#Study with a friend
imperial_examination.4030 = {
	type = activity_event
	title = imperial_examination.4030.t
	desc = {
		desc = imperial_examination.4030.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					imperial_examination_4030_can_be_lovers_trigger = { CHARACTER = scope:friendly_entrant }
				}
				desc = imperial_examination.4030.outro.lover
			}
			desc = imperial_examination.4030.outro
		}
	}
	theme = imperial_examination
	override_background = { reference = bp2_university }
	center_portrait = {
		character = root
		animation = worry
	}
	right_portrait = {
		character = scope:friendly_entrant
		animation = admiration
	}
	cooldown = { months = 6 }
	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			scope:activity = {
				NOT = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		NOT = { has_variable = has_studied_with_friend }
		scope:activity = {
			any_guest_subset = {
				name = entrants
				imperial_examination_4030_other_entrant_trigger = yes
				OR = {
					has_relation_potential_friend = root
					has_relation_friend = root
					has_relation_best_friend = root
				}
			}
		}
		has_character_flag = exam_perform_intent
	}
	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					OR = {
						has_relation_potential_friend = root
						has_relation_friend = root
						has_relation_best_friend = root
					}
					save_temporary_scope_as = char_check
					imperial_examination_4030_can_be_lovers_trigger = { CHARACTER = scope:char_check }
				}
				alternative_limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					has_relation_best_friend = root
				}
				alternative_limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					has_relation_friend = root
				}
				alternative_limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					has_relation_potential_friend = root
				}
				save_scope_as = friendly_entrant
			}
		}
	}
	#Study, study
	option = {
		name = imperial_examination.4030.a
		custom_tooltip = imperial_examination.advantage.tt
		imperial_examination_adjust_advantage_variable_effect = yes
		set_variable = has_studied_with_friend
		progress_towards_friend_effect = {
			CHARACTER = scope:friendly_entrant
			REASON = friend_bonded_over_studies
			OPINION = default_lover_opinion
		}
		stress_impact = {
			base = medium_stress_impact_gain
			lazy = minor_stress_impact_gain
			diligent = minor_stress_impact_loss
		}
		ai_chance = {
			base = 50
			modifier = {
				stress_level >= 1
				factor = 0
			}
		}
	}
	#"Study"
	option = {
		name = imperial_examination.4030.b
		trigger = {
			imperial_examination_4030_can_be_lovers_trigger = { CHARACTER = scope:friendly_entrant }
		}
		had_sex_with_effect = { CHARACTER = scope:friendly_entrant PREGNANCY_CHANCE = pregnancy_chance }
		progress_towards_lover_effect = {
			CHARACTER = scope:friendly_entrant
			REASON = lover_love_studies
			OPINION = default_lover_opinion
		}
		stress_impact = {
			base = medium_stress_impact_loss
			lustful = minor_stress_impact_loss
			rakish = medium_stress_impact_loss
		}
		ai_chance = {
			base = 50
			modifier = {
				might_cheat_on_every_partner_trigger = no
				factor = 0
			}
		}
	}
	#Let's not
	option = {
		name = imperial_examination.4030.c
		stress_impact = {
			base = minor_stress_impact_loss
			lazy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}
}


scripted_effect imperial_examination_4040_cheat_washing_duel_effect = {
	duel = {
		skill = $SKILL$
		value = decent_skill_rating
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			opinion_modifier = {
				opinion_target = scope:examiner
				multiplier = 0.5
			}
			desc = imperial_examination.6000.misdirection.success.tt
			send_interface_toast = {
				type = event_toast_effect_good
				title = imperial_examination.6000.misdirection.success.tt
				left_icon = root
				right_icon = scope:examiner
				#What a relief.
				imperial_examination_cheat_washing_effect = yes
			}
		}
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = -3.5
				min = -49
			}
			min = 5
			desc = imperial_examination.6000.misdirection.failure.tt
			send_interface_toast = {
				type = event_toast_effect_bad
				title = imperial_examination.6000.misdirection.failure.tt
				left_icon = root
				right_icon = scope:examiner
				#Nope.
				stress_impact = { base = medium_stress_impact_gain }
			}
		}
	}
}

#Late Night Baijiu
imperial_examination.4050 = {
	type = activity_event
	title = imperial_examination.4050.t
	desc = imperial_examination.4050.desc
	theme = imperial_examination
	override_background = { reference = bp2_university }
	center_portrait = {
		character = root
		animation = interested
	}
	right_portrait = {
		character = scope:friendly_entrant
		animation = admiration
	}
	cooldown = { months = 6 }
	trigger = {
		exists = involved_activity
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		NOT = { has_variable = has_partied_with_friend }
		scope:activity = {
			#Are we in the planning phase
			any_guest_subset = {
				name = entrants
				imperial_examination_4030_other_entrant_trigger = yes
				OR = {
					has_relation_potential_friend = root
					has_relation_friend = root
					has_relation_best_friend = root
				}
			}
		}
	}
	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					OR = {
						has_relation_potential_friend = root
						has_relation_friend = root
						has_relation_best_friend = root
					}
					save_temporary_scope_as = char_check
					imperial_examination_4030_can_be_lovers_trigger = { CHARACTER = scope:char_check }
				}
				alternative_limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					has_relation_best_friend = root
				}
				alternative_limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					has_relation_friend = root
				}
				alternative_limit = {
					imperial_examination_4030_other_entrant_trigger = yes
					has_relation_potential_friend = root
				}
				save_scope_as = friendly_entrant
			}
		}
	}
	#Let's tear this place up!
	option = {
		name = imperial_examination.4050.a
		add_prestige = medium_prestige_value
		set_variable = has_partied_with_friend
		progress_towards_friend_effect = {
			CHARACTER = scope:friendly_entrant
			REASON = friend_bonded_over_baijiu
			OPINION = default_lover_opinion
		}
		scope:activity = {
			every_attending_character = {
				limit = {
					NOT = { this = root }
					has_trait = gregarious
				}
				custom = custom.every_activity_guest_with_gregarious_trait
				add_opinion = {
					target = root
					modifier = friendliness_opinion
					opinion = 20
				}
			}
		}
		stress_impact = {
			diligent = medium_stress_impact_gain
			shy = medium_stress_impact_gain
			gregarious = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				has_trait = diligent
				add = -50
			}
			modifier = {
				has_trait = shy
				add = -50
			}
			modifier = {
				has_trait = gregarious
				add = 50
			}
		}
	}
	#Share an intimate moment.
	option = {
		name = imperial_examination.4050.b
		trigger = {
			imperial_examination_4030_can_be_lovers_trigger = { CHARACTER = scope:friendly_entrant }
		}
		had_sex_with_effect = { CHARACTER = scope:friendly_entrant PREGNANCY_CHANCE = pregnancy_chance }
		progress_towards_lover_effect = {
			CHARACTER = scope:friendly_entrant
			REASON = lover_shared_baijiu
			OPINION = default_lover_opinion
		}
		set_variable = has_partied_with_friend
		stress_impact = {
			base = medium_stress_impact_loss
			lustful = minor_stress_impact_loss
			rakish = medium_stress_impact_loss
		}
		ai_chance = {
			base = 50
			modifier = {
				might_cheat_on_every_partner_trigger = no
				factor = 0
			}
		}
	}
	#I have elsewhere to be.
	option = {
		name = imperial_examination.4050.c
		add_character_modifier = {
			modifier = tgp_resisted_alcohol_modifier
			years = 2
		}
		stress_impact = {
			base = minor_stress_impact_loss
			lazy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				has_trait = gregarious
				add = -100
			}
		}
	}
}

scripted_effect examination_type_duel_effect = {
	duel = {
		skill = $EXAM_TYPE$
		target = scope:promising_lowborn
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			desc = imperial_examination.4060.a.success
			send_interface_toast = {
				title = imperial_examination.4060.a.success
				left_icon = scope:promising_lowborn

				custom_tooltip = imperial_examination.advantage.tt
				imperial_examination_adjust_advantage_variable_effect = yes
			}
		}
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = -3.5
				min = -49
			}
			desc = imperial_examination.4060.a.failure
			send_interface_toast = {
				title = imperial_examination.4060.a.failure
				left_icon = scope:promising_lowborn
			}
			stress_impact = {
				base = major_stress_impact_gain
			}
		}
	}
}

#A potential rival appears
imperial_examination.4060 = {
	type = activity_event
	title = imperial_examination.4060.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:activity = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_martial
						}
					}
				}
				desc = imperial_examination.4060.desc_martial
			}
			desc = imperial_examination.4060.desc_learning
		}
	}

	theme = imperial_examination
	cooldown = { months = 6 }
	override_background = {
		trigger = {
			scope:activity = {
				has_activity_option = {
					category = special_type
					option = imperial_examination_focus_martial
				}
			}
		}
		reference = courtyard
	}

	override_background = {
		trigger = {
			NOT = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
		}
		reference = tavern
	}

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = war_over_win
		}
		animation = reading
	}

	right_portrait = {
		character = scope:promising_lowborn
		triggered_animation = {
			trigger = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			animation = hero_flex
		}
		animation = debating
	}

	trigger = {
		# We don't want to create a character for every AI at the activity.
		is_ai = no
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		# This event adds a character to the activity, so we need to check to see if the activity guest list is full
		scope:activity = {
			has_activity_type = activity_imperial_examination
			OR = {
				AND = {
					has_activity_option = {
						category = imperial_examination_breadth
						option = imperial_examination_breadth_exclusive
					}
					any_attending_character = {
						count < exclusive_exam_guests_full_calc
					}
				}
				AND = {
					has_activity_option = {
						category = imperial_examination_breadth
						option = imperial_examination_breadth_restricted
					}
					any_attending_character = {
						count < restricted_exam_guests_full_calc
					}
				}
				AND = {
					has_activity_option = {
						category = imperial_examination_breadth
						option = imperial_examination_breadth_open
					}
					any_attending_character = {
						count < open_exam_guests_full_calc
					}
				}
			}
		}
	}

	immediate = {
		find_interesting_pool_character = yes
	}

	option = { # I will show them!
		name = imperial_examination.4060.a
		trigger = { has_character_flag = exam_perform_intent }
		# Seperate duels depending on Imperial Examination type.
		if = {
			limit = { # Learning exam
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_learning
					}
				}
			}
			examination_type_duel_effect = { EXAM_TYPE = learning }
		}
		else_if = {
			limit = { # Stewardship exam
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_stewardship
					}
				}
			}
			examination_type_duel_effect = { EXAM_TYPE = stewardship }
		}
		else = { # Martial examination
			examination_type_duel_effect = { EXAM_TYPE = martial }
		}
		scope:promising_lowborn = {
			set_relation_grudge = {
				target = root
				reason = rival_stifled_my_influence
			}
		}
	}

	option = { # Son of a ****!
		name = imperial_examination.4060.d
		custom_tooltip = imperial_examination.4060.d.tt
		trigger = { has_character_flag = exam_cheat_intent }
		scope:promising_lowborn = {
			if = {
				limit = { involved_activity ?= scope:activity }
				remove_from_activity = involved_activity
			}
			examination_clear_timeout_effect = yes
		}
		change_merit = minor_merit_loss
		add_prestige = medium_prestige_gain
		stress_impact = {
			honest = medium_stress_impact_gain
			just = medium_stress_impact_gain
			deceitful = minor_stress_impact_loss
		}
	}

	option = { # You know what, let's help each other.
		name = imperial_examination.4060.b
		custom_tooltip = imperial_examination.advantage.tt
		imperial_examination_adjust_advantage_variable_effect = yes
		add_prestige = major_prestige_loss
		if = {
			limit = {
				is_lowborn = no
				scope:promising_lowborn = { is_lowborn = yes }
			}
			scope:activity = {
				every_attending_character = {
					limit = {
						NOT = { this = root }
						is_landless_administrative = yes
					}
					custom = every_admin_entrant
					add_opinion = {
						modifier = consorted_with_lowborn_opinion
						target = root
						opinion = -30
					}
				}
			}
		}
		stress_impact = {
			ambitious = minor_stress_impact_gain
		}
	}

	option = { # Actually... no.
		name = imperial_examination.4060.c
		stress_impact = {
			vengeful = minor_stress_impact_gain
		}
	}
}

# Unsolicited Family Support
imperial_examination.4070 = {
	type = activity_event
	title = imperial_examination.4070.t
	desc = imperial_examination.4070.desc

	theme = imperial_examination

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = disbelief
	}

	center_portrait = {
		character = scope:parent
		animation = admiration
	}

	right_portrait = {
		character = scope:examiner
		animation = interested_left
		trigger = { exists = scope:examiner }
	}

	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		has_character_flag = exam_cheat_intent
		#Do we have a parent with _influence_
		any_close_family_member = {
			trigger_if = {
				limit = {
					is_participant_in_activity = root.involved_activity
				}
				NOT = {
					is_in_guest_subset = { name = entrants }
				}
			}
			is_parent_of = root
			is_available_ai = yes
			location = scope:activity.activity_location
			OR = {
				intrigue >= high_skill_rating
				influence >= major_influence_value
				any_hooked_character = {
					is_in_guest_subset = { name = imperial_examiners }
				}
			}
		}
	}

	immediate = {
		random_close_family_member = {
			limit = {
				NOT = { is_participant_in_activity = scope:activity }
				is_parent_of = root
				is_available_ai = yes
				location = scope:activity.activity_location
				OR = {
					intrigue >= high_skill_rating
					influence >= major_influence_value
					any_hooked_character = {
						is_in_guest_subset = { name = imperial_examiners }
					}
				}
			}
			save_scope_as = parent
		}
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				weight = {
					base = 1
					modifier = {
						scope:parent = {
							has_hook = prev
						}
						add = 4
					}
				}
				save_scope_as = examiner
			}
		}
	}

	option = { # As long as they can be subtle about it
		name = imperial_examination.4070.a
		scope:parent = {
			duel = {
				skill = intrigue
				target = scope:examiner
				50 = {
					compare_modifier = {
						value = scope:duel_value
						multiplier = 3.5
						min = -49
					}
					desc = imperial_examination.4070.a.success
					root = {
						send_interface_toast = {
							title = imperial_examination.4070.a.success
							left_icon = scope:examiner
							custom_tooltip = imperial_examination.cheat.tt
							set_variable = {
								name = has_blackmailed_examiner
								value = scope:examiner
							}
						}
					}
				}
				50 = {
					compare_modifier = {
						value = scope:duel_value
						multiplier = -3.5
						min = -49
					}
					desc = imperial_examination.4070.a.failure
					root = {
						send_interface_toast = {
							title = imperial_examination.4070.a.failure
							left_icon = scope:examiner
						}
						stress_impact = {
							base = major_stress_impact_gain
						}
					}
				}
			}
		}
		stress_impact = {
			honest = minor_stress_impact_gain
			just = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_honor = -1
			}
			modifier = {
				add = 50
				scope:parent = {
					learning > mediocre_skill_rating
				}
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = honest
					has_trait = just
				}
			}
		}
	}

	option = { # I can suffer indignity to succeed.
		name = imperial_examination.4070.b
		custom_tooltip = imperial_examination.cheat.tt
		set_variable = {
			name = has_blackmailed_examiner
			value = scope:examiner
		}
		add_prestige = medium_prestige_loss
		if = {
			limit = {
				is_lowborn = no
			}
			scope:activity = {
				every_attending_character = {
					limit = {
						NOT = { this = root }
						is_landless_administrative = yes
					}
					custom = every_admin_entrant
					add_opinion = {
						modifier = consorted_with_lowborn_opinion
						target = root
						opinion = -30
					}
				}
			}
		}
		stress_impact = {
			arrogant = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
			modifier = {
				add = -20
				has_trait = arrogant
			}
		}
	}

	option = { # _Please_ go away!
		name = imperial_examination.4070.c
		reverse_add_opinion = {
			target = scope:parent
			modifier = insulted_opinion
			opinion = -15
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 1
			}
			modifier = {
				add = 50
				has_trait = lazy
			}
		}
	}
}

# Two examiners argue the classics
imperial_examination.4080 = {
	type = activity_event
	title = imperial_examination.4080.t
	desc = imperial_examination.4080.desc

	theme = imperial_examination
	override_background = { reference = corridor_day }

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = worry
	}

	center_portrait = {
		character = scope:examiner_1
		animation = anger
	}

	right_portrait = {
		character = scope:examiner_2
		animation = dismissal
	}

	trigger = {
		is_in_guest_subset = { name = entrants }
		has_character_flag = exam_perform_intent
		scope:activity = {
			any_guest_subset = {
				name = imperial_examiners
				count >= 2
				is_ai = yes
			}
		}
	}

	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = imperial_examiners
				limit = { is_ai = yes }
				save_scope_as = examiner_1
			}
			random_guest_subset = {
				name = imperial_examiners
				limit = {
					is_ai = yes
					NOT = { this = scope:examiner_1 }
				}
				save_scope_as = examiner_2
			}
		}
	}

	option = { # Side with examiner 1
		name = imperial_examination.4080.a
		bp2_lifestyle_xp_gain_per_type_effect = { VALUE = minor }
		scope:activity = {
			if = {
				limit = {
					root = { is_ai = no }
				}
				add_activity_log_entry = {
					key = sided_with_examiner_log
					score = 25
					tags = { activity positive }
					character = root
					target = scope:examiner_1
				}
			}
		}
		if = {
			limit = {
				can_set_relation_friend_trigger = { CHARACTER = scope:examiner_1 }
			}
			progress_towards_friend_effect = {
				REASON = friend_supported_me_in_dispute
				CHARACTER = scope:examiner_1
				OPINION = default_friend_opinion
			}
		}
		if = {
			limit = {
				can_set_relation_rival_trigger = { CHARACTER = scope:examiner_2 }
			}
			progress_towards_rival_effect = {
				REASON = rival_not_supported_me_in_dispute
				CHARACTER = scope:examiner_2
				OPINION = default_rival_opinion
			}
		}
		stress_impact = {
			honest = minor_stress_impact_loss
			craven = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				add = 20
				has_trait = honest
			}
			modifier = {
				factor = 0
				has_trait = craven
			}
		}
	}

	option = { # Side with examiner 2
		name = imperial_examination.4080.b
		bp2_lifestyle_xp_gain_per_type_effect = { VALUE = minor }
		scope:activity = {
			if = {
				limit = {
					root = { is_ai = no }
				}
				add_activity_log_entry = {
					key = sided_with_examiner_log
					score = 25
					tags = { activity positive }
					character = root
					target = scope:examiner_2
				}
			}
		}
		if = {
			limit = {
				can_set_relation_friend_trigger = { CHARACTER = scope:examiner_2 }
			}
			progress_towards_friend_effect = {
				REASON = friend_supported_me_in_dispute
				CHARACTER = scope:examiner_2
				OPINION = default_friend_opinion
			}
		}
		if = {
			limit = {
				can_set_relation_rival_trigger = { CHARACTER = scope:examiner_1 }
			}
			progress_towards_rival_effect = {
				REASON = rival_not_supported_me_in_dispute
				CHARACTER = scope:examiner_1
				OPINION = default_rival_opinion
			}
		}
		stress_impact = {
			honest = minor_stress_impact_loss
			craven = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				add = 20
				has_trait = honest
			}
			modifier = {
				add = -40
				has_trait = craven
			}
		}
	}

	option = { # I enjoy both in equal measure.
		name = imperial_examination.4080.c
		reverse_add_opinion = {
			target = scope:examiner_1
			modifier = ignored_opinion
			opinion = -15
		}
		reverse_add_opinion = {
			target = scope:examiner_2
			modifier = ignored_opinion
			opinion = -15
		}
		ai_chance = {
			base = 100
			modifier = {
				has_trait = lazy
				add = 50
			}
			modifier = {
				has_trait = fickle
				add = 25
			}
			ai_value_modifier = {
				ai_sociability = -1
			}
		}
	}
}

# Bond with likeminded entrant
imperial_examination.4090 = {
	type = activity_event
	title = imperial_examination.4090.t
	desc = imperial_examination.4090.desc

	theme = imperial_examination
	override_background = { reference = tavern }

	cooldown = { months = 6 }

	center_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				has_trait = shy
			}
			animation = worry
		}
		animation = personality_bold
		camera = camera_event_very_left
	}

	right_portrait = {
		character = scope:other_entrant
		animation = interested_left
	}

	trigger = {
		NOT = { is_in_list = met_entrants }
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		has_character_flag = exam_perform_intent
		scope:activity = {
			any_guest_subset = {
				name = entrants
				count >= 1
				is_ai = yes
				NOT = {
					this = root
					has_trait = shy
				}
				number_of_traits_in_common = {
					target = root
					value >= 1
				}
			}
		}
	}

	immediate = {
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					is_ai = yes
					NOT = { this = root }
					number_of_traits_in_common = {
						target = root
						value >= 1
					}
				}
				save_scope_as = other_entrant
				add_to_list = met_entrants
			}
		}
	}

	option = { # Let's hang!
		name = imperial_examination.4090.a
		change_influence = medium_influence_gain
		scope:activity = {
			every_attending_character = {
				limit = {
					NOT = { this = root }
					number_of_traits_in_common = {
						target = root
						value >= 1
					}
				}
				custom = custom.every_activity_guest_with_trait_in_common
				add_opinion = {
					target = root
					modifier = friendliness_opinion
					opinion = 20
				}
			}
		}

		stress_impact = {
			gregarious = miniscule_stress_impact_loss
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1
				ai_energy = 0.5
			}
			modifier = {
				add = 10
				has_trait = gregarious
			}
			modifier = {
				add = -10
				has_trait = shy
			}
		}
	}

	option = { # I have important texts to read.
		name = imperial_examination.4090.b
		change_merit = medium_merit_gain

		stress_impact = {
			gregarious = miniscule_stress_impact_gain
			shy = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = -1
			}
			modifier = {
				add = -10
				has_trait = gregarious
			}
			modifier = {
				add = 10
				has_trait = shy
			}
		}
	}
}

# Try to sneak a tiny answer book inside examination hall
imperial_examination.4100 = {
	type = activity_event
	title = imperial_examination.4100.t
	desc = imperial_examination.4100.desc

	theme = imperial_examination
	override_background = { reference = tgp_examination_room }

	cooldown = { months = 6 }

	left_portrait = {
		character = root
		animation = paranoia
	}

	right_portrait = {
		character = scope:other_entrant
		animation = fanning
	}

	trigger = {
		#Are we in the correct subset
		is_in_guest_subset = { name = entrants }
		trigger_if = {
			limit = {
				scope:activity = { has_activity_type = activity_imperial_examination }
			}
			has_activity_intent = imperial_exam_taker_intent
		}
		trigger_else = { has_activity_intent = local_exam_taker_intent }
		scope:activity = {
			any_guest_subset = {
				name = entrants
				count >= 1
				NOT = {
					this = root
					has_friendly_relationship_with_character_trigger = { CHARACTER = root }
					is_close_or_extended_family_of = root
				}
				is_ai = yes
			}
		}
		has_character_flag = exam_cheat_intent
	}

	immediate = {
		imperial_examination_fetch_examiner_effect = yes
		scope:activity = {
			random_guest_subset = {
				name = entrants
				limit = {
					NOT = {
						this = root
						has_friendly_relationship_with_character_trigger = { CHARACTER = root }
						is_close_or_extended_family_of = root
					}
					is_ai = yes
				}
				weight = {
					base = 1
					modifier = {
						has_trait = greedy
						add = 2
					}
					modifier = {
						has_trait = schemer
						add = 2
					}
					modifier = {
						has_trait = honest
						add = -5
					}
				}
				save_scope_as = other_entrant
			}
		}
	}

	option = { # Let's cheat together!
		name = imperial_examination.4100.a
		duel = {
			skill = diplomacy
			target = scope:other_entrant
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = imperial_examination.4100.a.success
				send_interface_toast = {
					title = imperial_examination.4100.a.success
					left_icon = scope:other_entrant
					custom_tooltip = imperial_examination.cheat.tt
					set_variable = {
						name = has_planted_answers
						value = scope:other_entrant
					}
					if = {
						limit = {
							can_set_relation_friend_trigger = { CHARACTER = scope:other_entrant }
						}
						scope:other_entrant = {
							progress_towards_friend_effect = {
								CHARACTER = root
								REASON = friend_bonded_over_cheating
								OPINION = 10
							}
						}
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.4100.a.failure
				send_interface_toast = {
					title = imperial_examination.4100.a.failure
					left_icon = scope:other_entrant
					custom_tooltip = imperial_examination.caught_cheating.tt
					set_variable = been_caught_cheating
				}
			}
		}

		stress_impact = {
			just = medium_stress_impact_gain
			honest = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_honor = -1
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = just
					has_trait = honest
				}
			}
		}
	}

	option = { # Nothing to see here
		name = imperial_examination.4100.b
		duel = {
			skill = intrigue
			target = scope:other_entrant
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = imperial_examination.4100.b.success
				send_interface_toast = {
					title = imperial_examination.4100.b.success
					left_icon = scope:other_entrant
					custom_tooltip = imperial_examination.cheat.tt
					set_variable = {
						name = has_planted_answers
						value = scope:examiner
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.4100.b.failure
				send_interface_toast = {
					title = imperial_examination.4100.b.failure
					left_icon = scope:other_entrant
					custom_tooltip = imperial_examination.caught_cheating.tt
					set_variable = been_caught_cheating
				}
			}
		}

		stress_impact = {
			just = medium_stress_impact_gain
			honest = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_honor = -1
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = just
					has_trait = honest
				}
			}
		}
	}

	option = { # Abandon endevour
		name = imperial_examination.4100.c

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
				ai_honor = 1
			}
			modifier = {
				add = 20
				OR = {
					has_trait = shy
					has_trait = craven
				}
			}
		}
	}
}

#######################
### ENTRANT PALACE EXAM PREP EVENTS
#######################
# imperial_examination.8000 - Celebration having made top ten
# imperial_examination.8010 - Decide if happy or unhappy with result

imperial_examination.8000 = {
	type = activity_event
	title = imperial_examination.8000.t
	desc = imperial_examination.8000.desc
	theme = imperial_examination
	override_background = { reference = courtyard }
	override_effect_2d = { reference = legend_glow }
	center_portrait = {
		character = root
		animation = happiness
	}
	cooldown = { months = 6 }

	option = {
		name = imperial_examination.8000.a
		change_merit = major_merit_gain
		ai_chance = {
			base = 100
		}
	}
}

# You did not make top ten - decide if happy or unhappy with result
imperial_examination.8010 = {
	type = activity_event
	title = imperial_examination.8010.t
	desc = {
		desc = imperial_examination.8010.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					NOR = {
						has_trait = content
						has_trait = lazy
					}
				}
				desc = imperial_examination.8010.desc_worried
			}
			desc = imperial_examination.8010.desc_fallback
		}
	}

	theme = imperial_examination
	override_background = { reference = tavern }
	center_portrait = {
		character = root
        triggered_animation = {
            trigger = {
				has_trait = content
				has_trait = lazy
            }
            animation = boredom
        }
		animation = worry
	}
	cooldown = { months = 6 }

	option = { # I should focus on studying.
		name = imperial_examination.8010.a
		gain_appropriate_lifestyle_major_xp_effect = yes
		stress_impact = {
			diligent = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = 1
				ai_boldness = 1
			}
			modifier = {
				has_lifestyle = learning_lifestyle
				factor = 2
			}
			modifier = {
				has_trait = diligent
				factor = 2
			}
			modifier = {
				OR = {
					has_trait = ambitious
					has_trait = stubborn
				}
				add = 20
			}
		}
	}
	option = { # I have to swallow this shame
		name = imperial_examination.8010.b
		stress_impact = {
			base = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				OR = {
					has_trait = lazy
					has_trait = content
				}
				add = 20
			}
		}
	}
}

#######################
### EMPEROR PALACE EXAM PREP EVENTS
#######################
# imperial_examination.9000 - Waiting for Palace Exam start (Emperor)
# imperial_examination.9001 - Waiting for Palace Exam start (Entrant)

# Waiting for Palace Exam start (Emperor)
imperial_examination.9000 = {
	type = activity_event
	title = imperial_examination.9000.t
	desc = {
		desc = imperial_examination.9000.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:first_char = {
						highest_skill = diplomacy
					}
				}
				desc = imperial_examination.9000.desc.diplomacy
			}
			triggered_desc = {
				trigger = {
					scope:first_char = {
						highest_skill = stewardship
					}
				}
				desc = tgp_movement_events.0050.desc.stewardship
			}
			triggered_desc = {
				trigger = {
					scope:first_char = {
						highest_skill = intrigue
					}
				}
				desc = tgp_movement_events.0050.desc.intrigue
			}
			triggered_desc = {
				trigger = {
					scope:first_char = {
						highest_skill = learning
					}
				}
				desc = tgp_movement_events.0050.desc.learning
			}
			triggered_desc = {
				trigger = {
					scope:first_char = {
						highest_skill = martial
					}
				}
				desc = imperial_examination.9000.desc.martial
			}
		}
		desc = imperial_examination.9000.outro
	}
	theme = imperial_examination
	override_background = { reference = throne_room }
	left_portrait = {
		character = root
		animation = personality_content
	}
    center_portrait = {
        character = scope:examiner
        animation = admiration
        camera = camera_event_center_pointing_left
    }
	right_portrait = {
		character = scope:first_char
		animation = gongshou
	}
	cooldown = { months = 6 }
	trigger = {
		this = scope:host
		NOT = { has_variable = has_greeted_palace_entrants }
	}
	immediate = {
		scope:activity = {
			ordered_guest_subset = {
				name = palace_entrants
				order_by = imperial_examination_score_value
				save_scope_as = first_char
			}
		}
		imperial_examination_fetch_examiner_effect = yes
	}
	option = { # I expected more...
		trigger = {
			scope:activity = {
				OR = {
					AND = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_martial
						}
						scope:first_char = {
							NOT = { highest_skill = martial }
						}
						any_guest_subset = {
							name = palace_entrants
							highest_skill = martial
						}
					}
					AND = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_learning
						}
						scope:first_char = {
							NOT = { highest_skill = learning }
						}
						any_guest_subset = {
							name = palace_entrants
							highest_skill = learning
						}
					}
					AND = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_stewardship
						}
						scope:first_char = {
							NOT = { highest_skill = stewardship }
						}
						any_guest_subset = {
							name = palace_entrants
							highest_skill = stewardship
						}
					}
				}
			}
		}
		name = imperial_examination.9000.a
		if = {
			limit = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			custom_tooltip = imperial_examination.9000.a.tt_martial
			scope:activity = {
				every_guest_subset = {
					name = palace_entrants
					limit = {
						highest_skill = martial
					}
					custom = custom.every_martial_palace_entrant
					custom_tooltip = imperial_examination.advantage.tt
					imperial_examination_adjust_advantage_variable_effect = yes
				}
			}
		}
		else_if = {
			limit = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_learning
					}
				}
			}
			custom_tooltip = imperial_examination.9000.a.tt_learning
			scope:activity = {
				every_guest_subset = {
					name = palace_entrants
					limit = {
						highest_skill = learning
					}
					custom = custom.every_learning_palace_entrant
					custom_tooltip = imperial_examination.advantage.tt
					imperial_examination_adjust_advantage_variable_effect = yes
				}
			}
		}
		else = {
			custom_tooltip = imperial_examination.9000.a.tt_stewardship
			scope:activity = {
				every_guest_subset = {
					name = palace_entrants
					limit = {
						highest_skill = stewardship
					}
					custom = custom.every_stewardship_palace_entrant
					custom_tooltip = imperial_examination.advantage.tt
					imperial_examination_adjust_advantage_variable_effect = yes
				}
			}
		}
		add_prestige = medium_prestige_gain
		scope:first_char = {
			if = {
				limit = {
					is_ai = no
				}
				add_character_flag = denied_merit
				trigger_event = imperial_examination.9001
			}
		}
		stress_impact = {
			just = medium_stress_impact_gain
			generous = medium_stress_impact_gain
			stubborn = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
				ai_boldness = 1
			}
			modifier = {
				has_trait = stubborn
				add = 20
			}
			modifier = {
				OR = {
					has_trait = just
					has_trait = generous
				}
				factor = 0
			}
		}
	}
	option = { # Congratulations!
		name = imperial_examination.9000.b
		add_legitimacy = medium_legitimacy_gain
		if = {
			limit = {
				scope:first_char = { is_ai = yes }
			}
			scope:activity = {
				add_activity_log_entry = {
					key = examination_first_place_entrant_noticed
					score = 25
					tags = { positive }
					character = scope:host
					target = scope:first_char
					scope:first_char = {
						change_merit = 2000
					}
				}
			}
		}
		else = {
			scope:first_char = {
				add_character_flag = gained_merit
				trigger_event = imperial_examination.9001
			}
		}
		stress_impact = {
			base = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
		}
	}

	after = {
		set_variable = has_greeted_palace_entrants
	}
}

# Follow up event - Waiting for Palace Exam start (Entrant)
imperial_examination.9001 = {
	type = activity_event
	title = imperial_examination.9001.t
	desc = {
		desc = imperial_examination.9001.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:activity = { activity_is_martial_focus_trigger = yes }
				}
				desc = imperial_examination.9001.desc_weapon
			}
			desc = imperial_examination.9001.desc_brush
		}
		desc = imperial_examination.9001.desc_bridge
		first_valid = {
			triggered_desc = {
				trigger = { has_character_flag = denied_merit }
				desc = imperial_examination.9001.desc_denied
			}
			desc = imperial_examination.9001.desc_merit
		}
	}

	theme = imperial_examination
	override_background = { reference = tgp_asia_throne_room }

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { has_character_flag = denied_merit }
			animation = shock
		}
		animation = gongshou
		camera = camera_event_left_pointing_right
	}

	center_portrait = {
		trigger = { exists = scope:favored_entrant }
		character = scope:favored_entrant
		animation = admiration
		camera = camera_event_left_to_the_left
	}

	right_portrait = {
		character = scope:host
		animation = emperor
		camera = camera_event_right_to_the_right
	}

	cooldown = { months = 6 }

	immediate = {
		scope:host = {
			save_scope_as = host
		}
		if = {
			limit = {
				has_character_flag = denied_merit
			}
			scope:activity = {
				ordered_guest_subset = {
					name = palace_entrants
					order_by = imperial_examination_score_value
					limit = {
						OR = {
							AND = {
								scope:activity = {
									has_activity_option = {
										category = special_type
										option = imperial_examination_focus_martial
									}
								}
								highest_skill = martial
							}
							AND = {
								scope:activity = {
									has_activity_option = {
										category = special_type
										option = imperial_examination_focus_learning
									}
								}
								highest_skill = learning
							}
							AND = {
								scope:activity = {
									has_activity_option = {
										category = special_type
										option = imperial_examination_focus_stewardship
									}
								}
								highest_skill = stewardship
							}
						}
					}
					save_scope_as = favored_entrant
				}
			}
		}
	}

	option = { # I live to serve
		name = imperial_examination.9001.a
		trigger = { has_character_flag = gained_merit }
		change_merit = 2000
	}

	option = { # I will never forget this!
		name = imperial_examination.9001.b
		trigger = {
			has_character_flag = denied_merit
			OR = {
				can_set_relation_rival_trigger = { CHARACTER = scope:favored_entrant }
				can_set_relation_nemesis_trigger = { CHARACTER = scope:favored_entrant }
			}
		}
		# We should become rivals if we're not already.
		if = {
			limit = {
				NOT = { has_relation_rival = scope:favored_entrant }
			}
			set_relation_rival = {
				target = scope:favored_entrant
				reason = rival_favored_entrant
			}
		}
		# But if we _are_, then it's nemesis time.
		else_if = {
			limit = { has_relation_rival = scope:favored_entrant }
			set_relation_nemesis = {
				target = scope:favored_entrant
				copy_reason = rival
				reason = rival_favored_entrant
			}
		}
		# Then add influence.
		change_influence = major_influence_gain
		stress_impact = {
			forgiving = medium_stress_impact_gain
			calm = medium_stress_impact_gain
		}
	}

	option = { # I will do better.
		name = imperial_examination.9001.c
		trigger = {
			has_character_flag = denied_merit
			OR = {
				AND = {
					scope:activity = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_martial
						}
					}
					NOT = { has_lifestyle = intrigue_lifestyle }
				}
				AND = {
					scope:activity = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_learning
						}
					}
					NOT = { has_lifestyle = learning_lifestyle }
				}
				AND = {
					scope:activity = {
						has_activity_option = {
							category = special_type
							option = imperial_examination_focus_stewardship
						}
					}
					NOT = { has_lifestyle = stewardship_lifestyle }
				}
			}
		}
		if = {
			limit = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			set_focus = martial_strategy_focus
			add_martial_lifestyle_perk_points = 2
		}
		else_if = {
			limit = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_learning
					}
				}
			}
			set_focus = learning_scholarship_focus
			add_learning_lifestyle_perk_points = 2
		}
		else = {
			set_focus = stewardship_duty_focus
			add_stewardship_lifestyle_perk_points = 2
		}
		add_character_modifier = {
			modifier = tgp_changed_ways_modifier
			years = 10
		}
	}

	option = { # Remain calm...
		name = imperial_examination.9001.d
		trigger = { has_character_flag = denied_merit }
		add_piety = medium_piety_gain
        if = {
            limit = {
                NOT = { has_trait = confucian_education }
            }
            add_trait = confucian_education
        }
        add_trait_xp = {
            trait = confucian_education
            value = lifestyle_confucian_education_xp_gain_medium_value
        }
        stress_impact = {
        	base = medium_stress_impact_loss
        	arrogant = major_stress_impact_gain
        }
	}
}

#######################
### ENTRANT METROPOLITAN EXAM EVENTS
#######################
# imperial_examination.5000 - The Essay
# imperial_examination.5050 - An Imperial Classic
# imperial_examination.5100 - Examination Poetry
# imperial_examination.5150 - Archery Contest
# imperial_examination.5160 - Polearm Contest
# imperial_examination.5170 - Bow Riding
# imperial_examination.5200 - Palace Archery Contest
# imperial_examination.5300 - Palace Essay (Learning)
# imperial_examination.5400 - Palace Essay (Stewardship)
# imperial_examination.5500 - Results Published

# Actual examination EVENTS need to correspond to the number in num_intermediate_exams_value

scripted_effect imperial_examination_set_up_exam_advantage_scope = {
	if = {
		limit = { has_variable = has_exam_advantage }
		save_scope_value_as = {
			name = exam_advantage_mult
			value = {
				add = var:has_exam_advantage
				add = 2
			}
		}
	}
}

#Send in the score value
scripted_effect imperial_examination_add_score_effect = {
	#Keep track of how much we gained, locally
	if = {
		limit = { has_variable = temporary_exam_score }
		change_variable = {
			name = temporary_exam_score
			add = $VALUE$
		}
	}
	else = {
		set_variable = {
			name = temporary_exam_score
			value = $VALUE$
		}
	}
	#Update the score for the scoreboard
	if = {
		limit = { has_variable = imperial_examination_score }
		change_variable = {
			name = imperial_examination_score
			add = $VALUE$
		}
	}
	else = {
		set_variable = {
			name = imperial_examination_score
			value = $VALUE$
		}
	}
}

scripted_effect critical_success_gains_effect = {
	add_prestige = miniscule_prestige_gain
	add_stress = minor_stress_impact_loss
}

scripted_effect imperial_examination_exam_duel_critical_success_effect = {
	#Notify the player
	send_interface_toast = {
		type = event_toast_text_and_effect_good
		title = imperial_examination.duel.critical_success.t
		desc = imperial_examination.duel.$EXAM$.critical_success.tt
		left_icon = root
		show_as_tooltip = { critical_success_gains_effect = yes }
	}
	#So that we can both show the desc and effect to the player
	hidden_effect = {
		involved_activity = {
			add_activity_log_entry = {
				key = imperial_examination_ace_$EXAM$_log
				tags = { good }
				score = 50
				character = root
				#Effects
				root = {
					critical_success_gains_effect = yes
					if = {
						limit = {
							has_trait = confucian_education
							NOT = {
								has_trait_xp = {
									trait = confucian_education
									value = trait_max_value
								}
							}
						}
						random = {
							chance = 5
							add_trait_xp = {
								trait = confucian_education
								value = lifestyle_confucian_education_xp_gain_minor_value
							}
						}
					}
				}
			}
		}
	}
}

#Great success
scripted_effect imperial_examination_exam_duel_great_success_effect = {
	send_interface_toast = {
		type = event_toast_text_and_effect_good
		title = imperial_examination.duel.great_success.t
		desc = imperial_examination.duel.$EXAM$.great_success.tt
		left_icon = root
		add_stress = miniscule_stress_impact_loss
	}
}

#Success
scripted_effect imperial_examination_exam_duel_success_effect = {
	send_interface_toast = {
		type = event_toast_text_good
		title = imperial_examination.duel.success.t
		desc = imperial_examination.duel.$EXAM$.success.tt
		left_icon = root
	}
}

#Failure
scripted_effect imperial_examination_exam_duel_failure_effect = {
	send_interface_toast = {
		type = event_toast_text_bad
		title = imperial_examination.duel.failure.t
		desc = imperial_examination.duel.$EXAM$.failure.tt
		left_icon = root
	}
}

#Proper duel effect
scripted_effect imperial_examination_exam_duel_effect = {
	#Notify the player that they have collected some advantages
	if = {
		limit = { has_variable = has_exam_advantage }
		if = {
			limit = { var:has_exam_advantage >= 2 }
			custom_tooltip = imperial_examination.has_increased_advantage.tt
		}
		else = { custom_tooltip = imperial_examination.has_advantage.tt }
	}
	duel = {
		skills = { learning martial stewardship }
		value = decent_skill_rating
		10 = {
			trigger = {
				NOT = { has_variable = been_caught_cheating_longterm }
			}
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			modifier = {
				has_variable = has_exam_advantage
				factor = scope:exam_advantage_mult
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_learning_focus_trigger = yes
				}
				add = root.learning
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_martial_focus_trigger = yes
				}
				add = root.martial
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_stewardship_focus_trigger = yes
				}
				add = root.stewardship
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_local_examination
				}
				add = root.learning
				add = root.stewardship
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 100 #Max
					value >= trait_max_value
				}
				add = 20
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 80 #Fourth level
					value >= trait_fourth_of_five_level_value
				}
				add = 15
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 60 #Third level
					value >= trait_third_of_five_level_value
				}
				add = 12
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 40 #Second level
					value >= trait_second_of_five_level_value
				}
				add = 9
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 20 #First level
					value >= trait_first_of_five_level_value
				}
				add = 6
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#Zero level
					value < trait_first_of_five_level_value
				}
				add = 3
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
				}
				NOT = { has_character_flag = passed_provincial_exam }
				add = -12
			}

			min = 5
			desc = imperial_examination.duel.$EXAM$.critical_success.tt
			custom_tooltip = imperial_examination.score.critical_success.tt
			#Which activity log entry?
			if = {
				limit = { scope:current_examination = flag:essay }
				imperial_examination_exam_duel_critical_success_effect = { EXAM = essay }
			}
			if = {
				limit = { scope:current_examination = flag:classics }
				imperial_examination_exam_duel_critical_success_effect = { EXAM = classics }
			}
			if = {
				limit = { scope:current_examination = flag:poetry }
				imperial_examination_exam_duel_critical_success_effect = { EXAM = poetry }
			}
			if = {
				limit = { scope:current_examination = flag:archery }
				imperial_examination_exam_duel_critical_success_effect = { EXAM = archery }
			}
			if = {
				limit = { scope:current_examination = flag:polearm }
				imperial_examination_exam_duel_critical_success_effect = { EXAM = polearm }
			}
			if = {
				limit = { scope:current_examination = flag:riding }
				imperial_examination_exam_duel_critical_success_effect = { EXAM = riding }
			}
			#Award score
			imperial_examination_add_score_effect = { VALUE = imperial_examination_critical_success_value }
		}
		20 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			modifier = {
				has_variable = has_exam_advantage
				factor = scope:exam_advantage_mult
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_learning_focus_trigger = yes
				}
				add = root.learning
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_martial_focus_trigger = yes
				}
				add = root.martial
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_stewardship_focus_trigger = yes
				}
				add = root.stewardship
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_local_examination
				}
				add = root.learning
				add = root.stewardship
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 100 #Max
					value >= trait_max_value
				}
				add = 20
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 80 #Fourth level
					value >= trait_fourth_of_five_level_value
				}
				add = 15
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 60 #Third level
					value >= trait_third_of_five_level_value
				}
				add = 12
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 40 #Second level
					value >= trait_second_of_five_level_value
				}
				add = 9
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#value >= 20 #First level
					value >= trait_first_of_five_level_value
				}
				add = 6
			}
			modifier = {
				has_trait = confucian_education
				has_trait_xp = {
					trait = confucian_education
					#Zero level
					value < trait_first_of_five_level_value
				}
				add = 3
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
				}
				NOT = { has_character_flag = passed_provincial_exam }
				add = -6
			}
			min = 5
			desc = imperial_examination.duel.$EXAM$.great_success.tt
			custom_tooltip = imperial_examination.score.great_success.tt
			#Which exam type?
			imperial_examination_exam_duel_great_success_effect = { EXAM = $EXAM$ }
			#Award score
			imperial_examination_add_score_effect = { VALUE = imperial_examination_great_success_value }
		}
		30 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			modifier = {
				has_variable = has_exam_advantage
				factor = scope:exam_advantage_mult
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_learning_focus_trigger = yes
				}
				add = root.learning
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_martial_focus_trigger = yes
				}
				add = root.martial
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
					activity_is_stewardship_focus_trigger = yes
				}
				add = root.stewardship
			}
			modifier = {
				scope:activity = {
					has_activity_type = activity_local_examination
				}
				add = root.learning
				add = root.stewardship
			}
			min = 5
			desc = imperial_examination.duel.$EXAM$.success.tt
			custom_tooltip = imperial_examination.score.success.tt
			#Which exam type?
			imperial_examination_exam_duel_success_effect = { EXAM = $EXAM$ }
			#Award score
			imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }

		}
		40 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = -3.5
				min = -49
			}
			desc = imperial_examination.duel.$EXAM$.failure.tt
			custom_tooltip = imperial_examination.score.failure.tt
			#Which exam type?
			imperial_examination_exam_duel_failure_effect = { EXAM = $EXAM$ }
			#No score
			imperial_examination_add_score_effect = { VALUE = 0 }
		}
	}
}

scripted_effect imperial_examination_caught_cheating_effect = {
	#We remove this after this round of exams are done so that you get to do the next round of exams, with stink-eye loc
	set_variable = been_caught_cheating
	#We keep this so that you get a massive penalty on your final merit gain at the end of the activity (unless you get rid of it somehow...)
	set_variable = been_caught_cheating_longterm
	change_merit = medium_merit_loss
	add_prestige = major_prestige_loss
	stress_impact = { base = massive_stress_impact_gain }
}

#Cheat-duel effect
scripted_effect imperial_examination_exam_cheat_effect = {
	#Notify the player that they are spending this cheat
	custom_tooltip = imperial_examination.cheat_spent.tt
	#What's the outcome?
	random_list = {
		90 = {
			desc = imperial_examination.cheat.success.desc
			custom_tooltip = imperial_examination.cheat.success.tt
			imperial_examination_add_score_effect = { VALUE = imperial_examination_cheat_value }
			#Notify the player
			send_interface_toast = {
				type = event_toast_text_good
				title = imperial_examination.duel.success.t
				desc = imperial_examination.cheat.success.desc
				left_icon = root
			}
		}
		10 = {
			desc = imperial_examination.cheat.failure.desc
			custom_tooltip = imperial_examination.cheat.failure.tt
			imperial_examination_caught_cheating_effect = yes
			trigger_event = imperial_examination.6000
		}
	}
}

# The [Exam] Essay
imperial_examination.5000 = {
	type = activity_event
	title = imperial_examination.5000.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { has_variable = been_caught_cheating_longterm }
				desc = imperial_examination.5000.desc.cheater
			}
			desc = imperial_examination.5000.desc
		}
		desc = imperial_examination.5000.desc_bridge
		random_valid = {
			desc = governance_weaponry
			desc = governance_reinstatement
			desc = governance_documents
			desc = governance_filial
		}
	}
	theme = imperial_examination
	left_portrait = {
		character = scope:other_entrant
		animation = reading
	}
	center_portrait = {
		character = root
		animation = writing
	}
	right_portrait = {
		character = scope:examiner
		#Got 'em in our pocket...
		triggered_animation = {
			trigger = {
				root = { has_variable = has_bribed_examiner }
				scope:examiner = root.var:has_bribed_examiner
			}
			animation = happy_teacher
		}
		animation = stressed_teacher
	}
	trigger = {
		trigger_if = {
			limit = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
				}
			}
			NOT = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			is_in_guest_subset = { name = entrants }
			scope:activity = {
				has_current_phase = imperial_examination_phase_examination
			}
		}
		trigger_else = {
			is_in_guest_subset = { name = entrants }
			scope:activity = {
				has_current_phase = local_examination_phase_examination
			}
		}
		NOR = {
			has_variable = had_essay_examination
			has_variable = been_caught_cheating
		}
	}
	immediate = {
		set_variable = had_essay_examination
		save_scope_value_as = {
			name = current_examination
			value = flag:essay
		}
		imperial_examination_set_up_exam_advantage_scope = yes
		scope:activity = {
			imperial_examination_fetch_other_entrant_effect = yes
			if = {
				limit = {
					has_activity_type = activity_imperial_examination
					any_attending_character = { has_title = title:e_minister_of_rites }
				}
				random_attending_character = {
					limit = { has_title = title:e_minister_of_rites }
					save_scope_as = examiner
				}
			}
			else = { imperial_examination_fetch_examiner_effect = yes }
		}
	}
	#Cheat: Someone else takes the exam for you
	option = {
		name = imperial_examination.5000.b
		custom_tooltip = has_exam_taker_tt
		trigger = { has_variable = has_exam_taker }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_exam_taker
		ai_chance = {
			base = 200
		}
	}
	#Cheat: You have bribed the examiner
	option = {
		name = imperial_examination.5000.c
		custom_tooltip = has_bribed_examiner_tt
		trigger = { has_variable = has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Cheat: Your parent has blackmailed the examiner
	option = {
		name = imperial_examination.5000.d
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = has_blackmailed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_blackmailed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Cheat: You have planted exam answers
	option = {
		name = imperial_examination.5000.e
		custom_tooltip = has_planted_answers_tt
		trigger = { has_variable = has_planted_answers }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_planted_answers
		ai_chance = {
			base = 200
		}
	}
	#Cheat: Your parent has bribed the examiner
	option = {
		name = imperial_examination.5000.f
		trigger = { has_variable = parent_has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = parent_has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Just do it
	option = {
		name = imperial_examination.5000.a
		imperial_examination_exam_duel_effect = { EXAM = essay }
		add_internal_flag = special
		ai_chance = {
			base = 100
		}
	}
	#Next!
	after = {
		clear_saved_scope = current_examination
		clear_saved_scope = exam_advantage_mult
	}
}

#A [Exam] Classic
imperial_examination.5050 = {
	type = activity_event
	title = imperial_examination.5050.t
	desc = {
		random_valid = {
			desc = imperial_examination.5050.history
			desc = imperial_examination.5050.changes
			desc = imperial_examination.5050.poetry
			desc = imperial_examination.5050.annals
			desc = imperial_examination.5050.rites
		}
	}
	theme = imperial_examination
	left_portrait = {
		character = root
		animation = pondering
	}
	right_portrait = {
		character = scope:examiner
		#Got 'em in our pocket...
		triggered_animation = {
			trigger = {
				root = { has_variable = has_bribed_examiner }
				scope:examiner = root.var:has_bribed_examiner
			}
			animation = happy_teacher
		}
		animation = stressed_teacher
	}
	trigger = {
		trigger_if = {
			limit = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
				}
			}
			NOT = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			is_in_guest_subset = { name = entrants }
			scope:activity = {
				has_current_phase = imperial_examination_phase_examination
			}
		}
		trigger_else = {
			is_in_guest_subset = { name = entrants }
			scope:activity = {
				has_current_phase = local_examination_phase_examination
			}
		}
		NOR = {
			has_variable = had_classics_examination
			has_variable = been_caught_cheating
		}
	}
	immediate = {
		set_variable = had_classics_examination
		save_scope_value_as = {
			name = current_examination
			value = flag:classics
		}
		imperial_examination_set_up_exam_advantage_scope = yes
		scope:activity = {
			imperial_examination_fetch_other_entrant_effect = yes
			if = {
				limit = {
					has_activity_type = activity_imperial_examination
					any_attending_character = { has_title = title:e_minister_of_rites }
				}
				random_attending_character = {
					limit = { has_title = title:e_minister_of_rites }
					save_scope_as = examiner
				}
			}
			else = { imperial_examination_fetch_examiner_effect = yes }
		}
	}
	#Cheat: Someone else takes the exam for you
	option = {
		name = imperial_examination.5050.b
		custom_tooltip = has_exam_taker_tt
		trigger = { has_variable = has_exam_taker }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_exam_taker
		ai_chance = {
			base = 200
		}
	}
	#Cheat: You have bribed the examiner
	option = {
		name = imperial_examination.5050.c
		custom_tooltip = has_bribed_examiner_tt
		trigger = { has_variable = has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Cheat: Your parent blackmailed the examiner
	option = {
		name = imperial_examination.5050.d
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = has_blackmailed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_blackmailed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Cheat: You have planted exam answers
	option = {
		name = imperial_examination.5050.e
		custom_tooltip = has_planted_answers_tt
		trigger = { has_variable = has_planted_answers }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_planted_answers
		ai_chance = {
			base = 200
		}
	}
	#Cheat: Your parent bribed the examiner
	option = {
		name = imperial_examination.5050.f
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = parent_has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = parent_has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Just do it
	option = {
		name = imperial_examination.5050.a
		imperial_examination_exam_duel_effect = { EXAM = classics }
		ai_chance = {
			base = 100
		}
	}
	#Next!
	after = {
		clear_saved_scope = current_examination
		clear_saved_scope = exam_advantage_mult
	}
}

#[Exam] Poetry
imperial_examination.5100 = {
	type = activity_event
	title = imperial_examination.5100.t
	desc = {
		desc = imperial_examination.5100.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = calm
						has_trait = patient
						has_trait = humble
					}
				}
				desc = imperial_examination.5100.desc.river
			}
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = diligent
						has_trait = stubborn
						has_trait = honest
					}
				}
				desc = imperial_examination.5100.desc.straight
			}
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = lustful
						has_trait = gluttonous
						has_trait = impatient
					}
				}
				desc = imperial_examination.5100.desc.explosive
			}
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = eccentric
						has_trait = zealous
					}
				}
				desc = imperial_examination.5100.desc.lofty
			}
			desc = imperial_examination.5100.desc.fallback
		}
	}
	theme = imperial_examination
	left_portrait = {
		character = scope:other_entrant
		animation = interested
	}
	center_portrait = {
		character = root
		animation = storyteller
	}
	right_portrait = {
		character = scope:examiner
		#Got 'em in our pocket...
		triggered_animation = {
			trigger = {
				root = { has_variable = has_bribed_examiner }
				scope:examiner = root.var:has_bribed_examiner
			}
			animation = happy_teacher
		}
		animation = stressed_teacher
	}
	trigger = {
		trigger_if = {
			limit = {
				scope:activity = {
					has_activity_type = activity_imperial_examination
				}
			}
			NOT = {
				scope:activity = {
					has_activity_option = {
						category = special_type
						option = imperial_examination_focus_martial
					}
				}
			}
			is_in_guest_subset = { name = entrants }
			scope:activity = {
				has_current_phase = imperial_examination_phase_examination
			}
		}
		trigger_else = {
			is_in_guest_subset = { name = entrants }
			scope:activity = {
				has_current_phase = local_examination_phase_examination
			}
		}
		NOR = {
			has_variable = had_poetry_examination
			has_variable = been_caught_cheating
		}
	}
	immediate = {
		set_variable = had_poetry_examination
		save_scope_value_as = {
			name = current_examination
			value = flag:poetry
		}
		imperial_examination_set_up_exam_advantage_scope = yes
		scope:activity = {
			imperial_examination_fetch_other_entrant_effect = yes
			if = {
				limit = {
					has_activity_type = activity_imperial_examination
					any_attending_character = { has_title = title:e_minister_of_rites }
				}
				random_attending_character = {
					limit = { has_title = title:e_minister_of_rites }
					save_scope_as = examiner
				}
			}
			else = { imperial_examination_fetch_examiner_effect = yes }
		}
	}
	#Cheat: Someone else takes the exam for you
	option = {
		name = imperial_examination.5100.b
		custom_tooltip = has_exam_taker_tt
		trigger = { has_variable = has_exam_taker }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_exam_taker
		ai_chance = {
			base = 200
		}
	}
	#Cheat: You have bribed the examiner
	option = {
		name = imperial_examination.5100.c
		custom_tooltip = has_bribed_examiner_tt
		trigger = { has_variable = has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Cheat: Your parent has blackmailed the examiner
	option = {
		name = imperial_examination.5100.d
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = has_blackmailed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_blackmailed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Cheat: You have planted exam answers
	option = {
		name = imperial_examination.5100.e
		custom_tooltip = has_planted_answers_tt
		trigger = { has_variable = has_planted_answers }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_planted_answers
		ai_chance = {
			base = 200
		}
	}
	#Auto-success: You are a poet
	option = {
		name = imperial_examination.5100.f
		custom_tooltip = imperial_examination.score.great_success.tt
		trigger = {
			has_trait = lifestyle_poet
		}
		add_internal_flag = special
		#Award score
		imperial_examination_add_score_effect = { VALUE = imperial_examination_great_success_value }
		ai_chance = {
			base = 300
		}
	}
	#Cheat: Your parent has bribed the examiner
	option = {
		name = imperial_examination.5100.g
		trigger = { has_variable = parent_has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}
	#Just do it
	option = {
		name = imperial_examination.5100.a
		imperial_examination_exam_duel_effect = { EXAM = poetry }
		ai_chance = {
			base = 100other_entrant_impressive_success
		}
	}
	#Next!
	after = {
		clear_saved_scope = current_examination
		clear_saved_scope = exam_advantage_mult
	}
}

#The Archery Contest
imperial_examination.5150 = {
	type = activity_event
	title = imperial_examination.5150.t
	desc = {
		desc = imperial_examination.5150.desc
		random_valid = {
			desc = other_entrant_impressive_success
			desc = other_entrant_spinning_safe
			desc = other_entrant_embarrased_cough
			desc = other_entrant_piercing_success
			desc = other_entrant_drooping_safe
			desc = other_entrant_vanishing_arrow
		}
	}

	theme = imperial_examination

	override_background = { reference = courtyard }

	left_portrait = {
		character = root
		animation = bow_idle
		camera = camera_event_left_crowning_observation
	}

	center_portrait = {
		character = scope:other_entrant
		scripted_animation = bow_drawn
		camera = camera_event_left_forward
	}

	right_portrait = {
		character = scope:examiner
		animation = personality_bold
		camera = camera_event_right
	}

	trigger = {
		scope:activity = {
			has_activity_type = activity_imperial_examination
			has_activity_option = {
				category = special_type
				option = imperial_examination_focus_martial
			}
			has_current_phase = imperial_examination_phase_examination
		}
		NOR = {
			has_variable = had_archery_examination
			has_variable = been_caught_cheating
		}
	}

	immediate = {
		# Set up event variables
		set_variable = had_archery_examination
		save_scope_value_as = {
			name = current_examination
			value = flag:archery
		}
		scope:activity = {
			# Find an examiner
			if = {
				limit = {
					any_attending_character = { has_title = title:e_minister_of_rites }
				}
				random_attending_character = {
					limit = { has_title = title:e_minister_of_rites }
					save_scope_as = examiner
				}
			}
			else = { imperial_examination_fetch_examiner_effect = yes }
		}
		imperial_examination_fetch_other_entrant_effect = yes
		#Let's count our advantages
		imperial_examination_set_up_exam_advantage_scope = yes
	}

	#Cheat: Someone else takes the exam for you
	option = {
		name = imperial_examination.5150.b
		custom_tooltip = has_exam_taker_tt
		trigger = { has_variable = has_exam_taker }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_exam_taker
		ai_chance = {
			base = 200
		}
	}

	#Cheat: You have bribed the examiner
	option = {
		name = imperial_examination.5150.c
		custom_tooltip = has_bribed_examiner_tt
		trigger = { has_variable = has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Cheat: Your parent has blackmailed the examiner
	option = {
		name = imperial_examination.5150.d
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = has_blackmailed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_blackmailed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Cheat: Your parent has bribed the examiner
	option = {
		name = imperial_examination.5150.e
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = parent_has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = parent_has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Just do it
	option = {
		name = imperial_examination.5150.a
		imperial_examination_exam_duel_effect = { EXAM = archery }
		ai_chance = {
			base = 100
		}
	}
	#Next!
	after = {
		clear_saved_scope = current_examination
		clear_saved_scope = exam_advantage_mult
	}
}

# The Polearm Demonstration
imperial_examination.5160 = {
	type = activity_event
	title = imperial_examination.5160.t
	desc = {
		desc = imperial_examination.5160.desc
		random_valid = {
			desc = other_entrant_cutting_success
			desc = other_entrant_practiced_safe
			desc = other_entrant_stuck_fail
			desc = other_entrant_practiced_success
			desc = other_entrant_stumbling_fail
		}
	}

	theme = imperial_examination

	override_background = { reference = courtyard }

	left_portrait = {
		character = root
		animation = relaxed_spear
		camera = camera_event_left_crowning_observation
	}

	center_portrait = {
		character = scope:other_entrant
		animation = aggressive_spear
		camera = camera_event_left_forward
	}

	right_portrait = {
		character = scope:examiner
		animation = personality_bold
		camera = camera_event_right
	}

	trigger = {
		scope:activity = {
			has_activity_type = activity_imperial_examination
			has_activity_option = {
				category = special_type
				option = imperial_examination_focus_martial
			}
			has_current_phase = imperial_examination_phase_examination
		}
		NOR = {
			has_variable = had_polearm_examination
			has_variable = been_caught_cheating
		}
	}

	immediate = {
		# Set up event variables
		set_variable = had_polearm_examination
		save_scope_value_as = {
			name = current_examination
			value = flag:polearm
		}
		scope:activity = {
			if = {
				limit = {
					any_attending_character = { has_title = title:e_minister_of_rites }
				}
				random_attending_character = {
					limit = { has_title = title:e_minister_of_rites }
					save_scope_as = examiner
				}
			}
			else = { imperial_examination_fetch_examiner_effect = yes }
		}
		# Find other entrant
		imperial_examination_fetch_other_entrant_effect = yes
		#Let's count our advantages
		imperial_examination_set_up_exam_advantage_scope = yes
	}

	#Cheat: Someone else takes the exam for you
	option = {
		name = imperial_examination.5160.b
		custom_tooltip = has_exam_taker_tt
		trigger = { has_variable = has_exam_taker }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_exam_taker
	}

	#Cheat: You have bribed the examiner
	option = {
		name = imperial_examination.5160.c
		custom_tooltip = has_bribed_examiner_tt
		trigger = { has_variable = has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Cheat: Your parent has blackmailed the examiner
	option = {
		name = imperial_examination.5160.d
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = has_blackmailed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_blackmailed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Cheat: Your parent has bribed the examiner
	option = {
		name = imperial_examination.5160.e
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = parent_has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = parent_has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Just do it
	option = {
		name = imperial_examination.5160.a
		imperial_examination_exam_duel_effect = { EXAM = polearm }
		ai_chance = {
			base = 100
		}
	}
	#Next!
	after = {
		clear_saved_scope = current_examination
		clear_saved_scope = exam_advantage_mult
	}
}

# Horseback Archery Assessment
imperial_examination.5170 = {
	type = activity_event
	title = imperial_examination.5170.t
	desc = {
		desc = imperial_examination.5170.desc
		random_valid = {
			desc = other_entrant_thundering_success
			desc = other_entrant_practiced_ride
			desc = other_entrant_spooked_horse
			desc = other_entrant_swift_success
			desc = other_entrant_shortstop_fail
		}
	}

	theme = imperial_examination

	override_background = { reference = plains }

	left_portrait = {
		character = root
		animation = horse_archer_idle
		camera = camera_event_horse_left_forward
	}

	center_portrait = {
		character = scope:other_entrant
		animation = horse_archer_aggressive
		camera = camera_event_horse_left_forward
	}

	trigger = {
		scope:activity = {
			has_activity_type = activity_imperial_examination
			has_activity_option = {
				category = special_type
				option = imperial_examination_focus_martial
			}
			has_current_phase = imperial_examination_phase_examination
		}
		NOR = {
			has_variable = had_riding_examination
			has_variable = been_caught_cheating
		}
	}

	immediate = {
		# Set up event variables
		set_variable = had_riding_examination
		save_scope_value_as = {
			name = current_examination
			value = flag:riding
		}
		# Find other entrant
		imperial_examination_fetch_other_entrant_effect = yes
		#Let's count our advantages
		imperial_examination_set_up_exam_advantage_scope = yes
	}

	#Cheat: Someone else takes the exam for you
	option = {
		name = imperial_examination.5170.b
		custom_tooltip = has_exam_taker_tt
		trigger = { has_variable = has_exam_taker }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_exam_taker
	}

	#Cheat: You have bribed the examiner
	option = {
		name = imperial_examination.5170.c
		custom_tooltip = has_bribed_examiner_tt
		trigger = { has_variable = has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Cheat: Your parent has blackmailed the examiner
	option = {
		name = imperial_examination.5170.d
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = has_blackmailed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = has_blackmailed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Cheat: Your parent has bribed the examiner
	option = {
		name = imperial_examination.5170.e
		custom_tooltip = has_blackmailed_examiner_tt
		trigger = { has_variable = parent_has_bribed_examiner }
		add_internal_flag = special
		imperial_examination_exam_cheat_effect = yes
		remove_variable = parent_has_bribed_examiner
		ai_chance = {
			base = 200
		}
	}

	#Auto-success: You have the Hastilunder trait
	option = {
		name = imperial_examination.5170.e
		if = {
			limit = {
				has_trait = tourney_participant
			}
			custom_description_no_bullet = {
				text = horse_exam_tourney_success
			}
		}
		else = {
			custom_description_no_bullet = {
				text = horse_exam_hunter_success
			}
		}
		custom_tooltip = imperial_examination.score.great_success.tt
		trigger = {
			OR = {
				has_trait_xp = {
					trait = tourney_participant
					track = horse
					value >= 20
				}
				has_trait_xp = {
					trait = tourney_participant
					track = bow
					value >= 20
				}
				has_trait_xp = {
					trait = lifestyle_hunter
					track = hunter
					value >= trait_second_level
				}
			}
		}
		add_internal_flag = special
		#Award score
		imperial_examination_add_score_effect = { VALUE = imperial_examination_great_success_value }
		ai_chance = {
			base = 300
		}
	}

	#Just do it
	option = {
		name = imperial_examination.5170.a
		imperial_examination_exam_duel_effect = { EXAM = riding }
		ai_chance = {
			base = 100
		}
	}
	#Next!
	after = {
		clear_saved_scope = current_examination
		clear_saved_scope = exam_advantage_mult
	}
}

#Caught cheating!
imperial_examination.6000 = {
	type = activity_event
	title = imperial_examination.6000.t
	desc = imperial_examination.6000.desc
	theme = imperial_examination
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { has_trait = callous }
			animation = stress
		}
		animation = fear
	}
	right_portrait = {
		character = scope:examiner
		animation = disapproval
	}
	immediate = {
		if = {
			limit = {
				NOT = { has_variable = discovering_examiner }
			}
			imperial_examination_fetch_examiner_effect = yes
			#So we don't get the same examiner here and the Cheat Wash-event
			set_variable = {
				name = discovering_examiner
				value = scope:examiner
			}
		}
		else = {
			var:discovering_examiner = { save_scope_as = examiner }
		}
		#Find a friend to sacrifice
		scope:activity = {
			if = {
				limit = {
					any_guest_subset = {
						name = entrants
						is_ai = yes
						OR = {
							has_relation_potential_friend = root
							has_relation_friend = root
							has_relation_best_friend = root
							has_relation_lover = root
						}
					}
				}
				random_guest_subset = {
					name = entrants
					limit = {
						is_ai = yes
						OR = {
							has_relation_potential_friend = root
							has_relation_friend = root
							has_relation_best_friend = root
							has_relation_lover = root
						}
					}
					save_scope_as = friend_to_sacrifice
				}
			}
		}
	}
	#Throw for misdirection
	option = {
		name = imperial_examination.6000.a
		#Good luck
		duel = {
			skill = intrigue
			value = decent_skill_rating
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				opinion_modifier = {
					opinion_target = scope:examiner
					multiplier = 0.5
				}
				desc = imperial_examination.6000.misdirection.success.tt
				send_interface_toast = {
					type = event_toast_effect_good
					title = imperial_examination.6000.misdirection.success.tt
					left_icon = root
					right_icon = scope:examiner
					#What a relief.
					imperial_examination_cheat_washing_effect = yes
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				min = 5
				desc = imperial_examination.6000.misdirection.failure.tt
				send_interface_toast = {
					type = event_toast_effect_bad
					title = imperial_examination.6000.misdirection.failure.tt
					left_icon = root
					right_icon = scope:examiner
					#Nope.
					stress_impact = {
						base = medium_stress_impact_gain
					}
					remove_variable ?= temporary_exam_score
				}
			}
		}
		ai_chance = {
			base = 100
		}
	}
	#Pay with influence
	option = {
		name = imperial_examination.6000.b
		trigger = { influence >= major_influence_value }
		#Remove influence
		change_influence = major_influence_loss
		#Clean the slate
		imperial_examination_cheat_washing_effect = yes
		ai_chance = {
			base = 100
			modifier = {
				influence < major_influence_value
				factor = 0
			}
		}
	}
	#Throw a friend under the bus
	option = {
		name = imperial_examination.6000.c
		trigger = { exists = scope:friend_to_sacrifice }
		#Bye bye
		set_relation_rival = scope:friend_to_sacrifice
		#Clean the slate
		imperial_examination_cheat_washing_effect = yes
		ai_chance = {
			base = 100
		}
	}
	#IT WAS MEE
	option = {
		name = imperial_examination.6000.d
		set_variable = {
			name = temporary_exam_score
			value = 0
		}
		ai_chance = {
			base = 100
		}
	}
}


#######################
### ENTRANT PALACE EXAM EVENTS
#######################

# The [Martial] Palace Exam
imperial_examination.5200 = {
	type = activity_event
	title = imperial_examination.5200.t
	desc = imperial_examination.5200.desc
	theme = imperial_examination
	override_background = { reference = courtyard }
	left_portrait = {
		character = root
		animation = bow_idle
	}
	right_portrait = {
		character = scope:host
		animation = japanese_war_fan
	}
	cooldown = { months = 6 }

	trigger = {
		is_in_guest_subset = { name = palace_entrants }
		scope:activity = {
			activity_is_martial_focus_trigger = yes
			has_current_phase = imperial_examination_phase_palace_examination
		}
	}

	option = { # Give it all
		name = imperial_examination.5200.a
		if = {
			limit = { has_variable = has_exam_advantage }
			if = {
				limit = { var:has_exam_advantage >= 2 }
				custom_tooltip = imperial_examination.has_increased_advantage.tt
			}
			else = { custom_tooltip = imperial_examination.has_advantage.tt }
		}
		duel = {
			skill = martial
			value = decent_skill_rating
			10 = {
				trigger = {
					NOT = { has_variable = been_caught_cheating_longterm }
				}
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_martial_focus_trigger = yes
					}
					add = root.martial
				}
				min = 5
				desc = imperial_examination.duel.archery.critical_success.tt
				custom_tooltip = imperial_examination.score.critical_success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_critical_success_value }
				add_character_flag = palace_critical_success
			}
			20 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_martial_focus_trigger = yes
					}
					add = root.martial
				}
				min = 5
				desc = imperial_examination.duel.archery.great_success.tt
				custom_tooltip = imperial_examination.score.great_success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_great_success_value }
				add_character_flag = palace_great_success
			}
			30 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_martial_focus_trigger = yes
					}
					add = root.martial
				}
				min = 5
				desc = imperial_examination.duel.archery.success.tt
				custom_tooltip = imperial_examination.score.success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }
				add_character_flag = palace_success
			}
			40 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.duel.archery.failure.tt
				custom_tooltip = imperial_examination.score.failure.tt
				add_character_flag = failed_martial
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 20
				martial > high_skill_rating
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
		}
	}

	option = { # Better start slow
		name = imperial_examination.5200.b
		custom_tooltip = imperial_examination.score.success.tt
		#Award score
		imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }
		# Use this flag for loc only
		add_character_flag = safe_start
		add_prestige = minor_prestige_loss
		stress_impact = {
			diligent = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
			modifier = {
				add = 40
				stress_level < 0
				NOR = {
					has_trait = diligent
					has_trait = ambitious
				}
			}
			modifier = {
				OR = {
					prestige < minor_prestige_value
					has_trait = diligent
					has_trait = ambitious
				}
				factor = 0
			}
		}
	}

	after = {
		if = {
			limit = { is_ai = no }
			trigger_event = imperial_examination.5500
		}
	}
}

# The [Learning] Palace Exam
imperial_examination.5300 = {
	type = activity_event
	title = imperial_examination.5300.t
	desc = imperial_examination.5300.desc
	theme = imperial_examination
	override_background = { reference = courtyard }
	left_portrait = {
		character = root
		animation = chancellor
	}
	right_portrait = {
		character = scope:host
		animation = pondering
	}
	cooldown = { months = 6 }

	trigger = {
		is_in_guest_subset = { name = palace_entrants }
		scope:activity = {
			activity_is_learning_focus_trigger = yes
			has_current_phase = imperial_examination_phase_palace_examination
		}
	}

	option = {
		name = imperial_examination.5300.a
		if = {
			limit = { has_variable = has_exam_advantage }
			if = {
				limit = { var:has_exam_advantage >= 2 }
				custom_tooltip = imperial_examination.has_increased_advantage.tt
			}
			else = { custom_tooltip = imperial_examination.has_advantage.tt }
		}
		duel = {
			skill = learning
			value = decent_skill_rating
			10 = {
				trigger = {
					NOT = { has_variable = been_caught_cheating_longterm }
				}
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_learning_focus_trigger = yes
					}
					add = root.learning
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 100 #Max
						value >= trait_max_value
					}
					add = 20
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 80 #Fourth level
						value >= trait_fourth_of_five_level_value
					}
					add = 15
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 60 #Third level
						value >= trait_third_of_five_level_value
					}
					add = 12
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 40 #Second level
						value >= trait_second_of_five_level_value
					}
					add = 9
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 20 #First level
						value >= trait_first_of_five_level_value
					}
					add = 6
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#Zero level
						value < trait_first_of_five_level_value
					}
					add = 3
				}

				min = 5
				desc = imperial_examination.duel.essay.critical_success.tt
				custom_tooltip = imperial_examination.score.critical_success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_critical_success_value }
				add_character_flag = palace_critical_success
			}
			20 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_learning_focus_trigger = yes
					}
					add = root.learning
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 100 #Max
						value >= trait_max_value
					}
					add = 20
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 80 #Fourth level
						value >= trait_fourth_of_five_level_value
					}
					add = 15
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 60 #Third level
						value >= trait_third_of_five_level_value
					}
					add = 12
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 40 #Second level
						value >= trait_second_of_five_level_value
					}
					add = 9
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 20 #First level
						value >= trait_first_of_five_level_value
					}
					add = 6
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#Zero level
						value < trait_first_of_five_level_value
					}
					add = 3
				}
				min = 5
				desc = imperial_examination.duel.essay.great_success.tt
				custom_tooltip = imperial_examination.score.great_success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_great_success_value }
				add_character_flag = palace_great_success
			}
			30 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_learning_focus_trigger = yes
					}
					add = root.learning
				}
				min = 5
				desc = imperial_examination.duel.essay.success.tt
				custom_tooltip = imperial_examination.score.success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }
				add_character_flag = palace_success
			}
			40 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.duel.essay.failure.tt
				custom_tooltip = imperial_examination.score.failure.tt
				add_character_flag = failed_classics
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 20
				learning > high_skill_rating
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				OR = {
					has_trait = content
					has_trait = lazy
				}
			}
		}
	}

	option = {
		name = imperial_examination.5300.b
		custom_tooltip = imperial_examination.score.success.tt
		#Award score
		imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }
		# Use this flag for loc only
		add_character_flag = safe_start
		add_prestige = minor_prestige_loss
		stress_impact = {
			diligent = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
			modifier = {
				add = 40
				stress_level < 0
			}
			modifier = {
				OR = {
					prestige < minor_prestige_value
					has_trait = diligent
					has_trait = ambitious
				}
				factor = 0
			}
		}
	}

	after = {
		if = {
			limit = { is_ai = no }
			trigger_event = imperial_examination.5500
		}
	}
}

# The [Stewardship] Palace Exam
imperial_examination.5400 = {
	type = activity_event
	title = imperial_examination.5400.t
	desc = imperial_examination.5400.desc
	theme = imperial_examination
	override_background = { reference = courtyard }
	left_portrait = {
		character = root
		animation = chancellor
	}
	right_portrait = {
		character = scope:host
		animation = pondering
	}
	cooldown = { months = 6 }

	trigger = {
		is_in_guest_subset = { name = palace_entrants }
		scope:activity = {
			activity_is_stewardship_focus_trigger = yes
			has_current_phase = imperial_examination_phase_palace_examination
		}
	}

	option = {
		name = imperial_examination.5400.a
		if = {
			limit = { has_variable = has_exam_advantage }
			if = {
				limit = { var:has_exam_advantage >= 2 }
				custom_tooltip = imperial_examination.has_increased_advantage.tt
			}
			else = { custom_tooltip = imperial_examination.has_advantage.tt }
		}
		duel = {
			skill = stewardship
			value = decent_skill_rating
			10 = {
				trigger = {
					NOT = { has_variable = been_caught_cheating_longterm }
				}
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_stewardship_focus_trigger = yes
					}
					add = root.stewardship
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 100 #Max
						value >= trait_max_value
					}
					add = 20
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 80 #Fourth level
						value >= trait_fourth_of_five_level_value
					}
					add = 15
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 60 #Third level
						value >= trait_third_of_five_level_value
					}
					add = 12
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 40 #Second level
						value >= trait_second_of_five_level_value
					}
					add = 9
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 20 #First level
						value >= trait_first_of_five_level_value
					}
					add = 6
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#Zero level
						value < trait_first_of_five_level_value
					}
					add = 3
				}

				min = 5
				desc = imperial_examination.duel.essay.critical_success.tt
				custom_tooltip = imperial_examination.score.critical_success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_critical_success_value }
				add_character_flag = palace_critical_success
			}
			20 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_stewardship_focus_trigger = yes
					}
					add = root.stewardship
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 100 #Max
						value >= trait_max_value
					}
					add = 20
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 80 #Fourth level
						value >= trait_fourth_of_five_level_value
					}
					add = 15
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 60 #Third level
						value >= trait_third_of_five_level_value
					}
					add = 12
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 40 #Second level
						value >= trait_second_of_five_level_value
					}
					add = 9
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#value >= 20 #First level
						value >= trait_first_of_five_level_value
					}
					add = 6
				}
				modifier = {
					has_trait = confucian_education
					has_trait_xp = {
						trait = confucian_education
						#Zero level
						value < trait_first_of_five_level_value
					}
					add = 3
				}
				min = 5
				desc = imperial_examination.duel.essay.great_success.tt
				custom_tooltip = imperial_examination.score.great_success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_great_success_value }
				add_character_flag = palace_great_success
			}
			30 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				modifier = {
					has_variable = has_exam_advantage
					factor = scope:exam_advantage_mult
				}
				modifier = {
					scope:activity = {
						has_activity_type = activity_imperial_examination
						activity_is_stewardship_focus_trigger = yes
					}
					add = root.stewardship
				}
				min = 5
				desc = imperial_examination.duel.essay.success.tt
				custom_tooltip = imperial_examination.score.success.tt
				#Award score
				imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }
				add_character_flag = palace_success

			}
			40 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = imperial_examination.duel.essay.failure.tt
				custom_tooltip = imperial_examination.score.failure.tt
				add_character_flag = failed_classics
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 20
				stewardship > high_skill_rating
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				OR = {
					has_trait = content
					has_trait = lazy
				}
			}
		}
	}

	option = {
		name = imperial_examination.5400.b
		custom_tooltip = imperial_examination.score.success.tt
		#Award score
		imperial_examination_add_score_effect = { VALUE = imperial_examination_success_value }
		# Use this flag for loc only
		add_character_flag = safe_start
		add_prestige = minor_prestige_loss
		stress_impact = {
			diligent = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
			modifier = {
				add = 40
				stress_level < 0
			}
			modifier = {
				OR = {
					prestige < minor_prestige_value
					has_trait = diligent
					has_trait = ambitious
				}
				factor = 0
			}
		}
	}

	after = {
		if = {
			limit = { is_ai = no }
			trigger_event = imperial_examination.5500
		}
		else = {
			remove_character_flag ?= palace_success
			remove_character_flag ?= palace_great_success
			remove_character_flag ?= palace_critical_success
			remove_character_flag ?= failed_martial
			remove_character_flag ?= failed_classics
			remove_character_flag ?= safe_start
		}
	}
}

# Palace Exam Outcome Event
imperial_examination.5500 = {
	type = activity_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_character_flag = palace_success
						has_character_flag = safe_start
					}
				}
				desc = imperial_examination.5500.t.success
			}
			triggered_desc = {
				trigger = {
					has_character_flag = palace_great_success
				}
				desc = imperial_examination.5500.t.great_success
			}
			triggered_desc = {
				trigger = {
					has_character_flag = palace_critical_success
				}
				desc = imperial_examination.5500.t.critical_success
			}
			desc = imperial_examination.5500.t.failure
		}
	}
	desc = {
		first_valid = {
			# Did you succeed in Martial?
			triggered_desc = {
				trigger = {
					scope:activity = {
						activity_is_martial_focus_trigger = yes
					}
					NOR = {
						has_character_flag = safe_start
						has_character_flag = failed_martial
					}
				}
				desc = imperial_examination.5500.desc.success_martial
			}
			# Did you succeed in classics/governance?
			triggered_desc = {
				trigger = {
					scope:activity = {
						OR = {
							activity_is_learning_focus_trigger = yes
							activity_is_stewardship_focus_trigger = yes
						}

					}
					NOR = {
						has_character_flag = safe_start
						has_character_flag = failed_classics
					}
				}
				desc = imperial_examination.5500.desc.success_learning
			}
			# Did you play it safe in Martial?
			triggered_desc = {
				trigger = {
					scope:activity = {
						activity_is_martial_focus_trigger = yes
					}
					has_character_flag = safe_start
				}
				desc = imperial_examination.5500.desc.safe_martial
			}
			# Did you play it safe in classics/governance?
			triggered_desc = {
				trigger = {
					scope:activity = {
						OR = {
							activity_is_learning_focus_trigger = yes
							activity_is_stewardship_focus_trigger = yes
						}
					}
					has_character_flag = safe_start
				}
				desc = imperial_examination.5500.desc.safe_learning
			}
			# Did you fail in Martial?
			triggered_desc = {
				trigger = {
					scope:activity = { activity_is_martial_focus_trigger = yes }
					has_character_flag = failed_martial
				}
				desc = imperial_examination.5500.desc.failed_martial
			}
			# Did you fail in Classics?
			triggered_desc = {
				trigger = {
					scope:activity = {
						OR = {
							activity_is_stewardship_focus_trigger = yes
							activity_is_learning_focus_trigger = yes
						}
					}
					has_character_flag = failed_classics
				}
				desc = imperial_examination.5500.desc.failed_classics
			}
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_character_flag = failed_martial
						has_character_flag = failed_classics
					}
				}
				desc = imperial_examination.5500.desc.hegemon_negative
			}
			desc = imperial_examination.5500.desc.hegemon_positive
		}
	}
	theme = imperial_examination
	override_background = { reference = courtyard }
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					has_character_flag = failed_martial
					has_character_flag = failed_classics
				}
			}
			animation = shame
		}
		animation = happiness
	}
	right_portrait = {
		character = scope:host
		triggered_animation = {
			trigger = {
				root = {
					OR = {
						has_character_flag = failed_martial
						has_character_flag = failed_classics
					}
				}
			}
			animation = dismissal
		}
		animation = personality_content
	}

	immediate = {
		scope:host = {
			assign_quirk_effect = yes
		}
	}

	option = {
		name = imperial_examination.5500.a
	}

	after = {
		remove_character_flag ?= palace_success
		remove_character_flag ?= palace_great_success
		remove_character_flag ?= palace_critical_success
		remove_character_flag ?= failed_martial
		remove_character_flag ?= failed_classics
		remove_character_flag ?= safe_start
	}
}

#Conclusion event
imperial_examination.7000 = {
	type = activity_event
	title = imperial_examination.7000.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { this = scope:host }
				desc = imperial_examination.7000.desc.emperor
			}
			triggered_desc = {
				trigger = {
					has_variable = been_caught_cheating_longterm
					# We can't check for intents at this stage. Flags are set in activity on_end
					has_character_flag = exam_taker
				}
				desc = imperial_examination.7000.desc.cheater
			}
			triggered_desc = {
				trigger = {
					is_in_guest_subset = { name = entrants }
					# We can't check for intents at this stage. Flags are set in activity on_end
					has_character_flag = exam_taker
				}
				desc = imperial_examination.7000.desc.entrant
			}
			triggered_desc = {
				trigger = {
					# We can't check for intents at this stage. Flags are set in activity on_end
					has_character_flag = exam_taker
					is_in_guest_subset = { name = palace_entrants }
				}
				desc = imperial_examination.7000.desc.palace_entrant
			}
			desc = imperial_examination.7000.desc.fallback
		}
	}
	theme = imperial_examination
	override_background = {
		trigger = {
			OR = {
				this = scope:host
				is_in_guest_subset = { name = palace_entrants }
			}
		}
		reference = courtyard
	}
	override_effect_2d = {
		trigger = {
			OR = {
				this = scope:host
				is_in_guest_subset = { name = palace_entrants }
			}
		}
		reference = legend_glow
	}
	left_portrait = {
		character = scope:second_place_entrant
		animation = war_over_win
		camera = camera_event_left_forward
	}
	center_portrait = {
		character = scope:first_place_entrant
		animation = pondering
		camera = camera_event_center_pointing_forward
	}
	right_portrait = {
		character = scope:third_place_entrant
		animation = war_over_tie
		camera = camera_event_right_forward
	}
	immediate = {
		scope:activity = {
			#Ease of access
			var:first_place_entrant ?= { save_scope_as = first_place_entrant }
			var:second_place_entrant ?= { save_scope_as = second_place_entrant }
			var:third_place_entrant ?= { save_scope_as = third_place_entrant }
		}
		if = {
			limit = {
				OR = {
					is_in_guest_subset = { name = entrants }
					is_in_guest_subset = { name = palace_entrants }
				}
			}
			# Desribe your score
			if = {
				limit = { has_perfect_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_perfect_score
			}
			if = {
				limit = { has_good_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_great_score
			}
			if = {
				limit = { has_decent_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_decent_score
			}
			if = {
				limit = { has_questionable_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_questionable_score
			}
		}
	}
	option = {
		name = {
			trigger = { has_variable = been_caught_cheating_longterm }
			text = imperial_examination.7000.a.cheater
		}
		name = {
			trigger = {
				NOT = { has_variable = been_caught_cheating_longterm }
			}
			text = imperial_examination.7000.a
		}
		#Hand out rewards
		#Emperor
		show_as_tooltip = {
			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 = {
					this = {
						OR = {
							is_in_guest_subset = { name = entrants }
							is_in_guest_subset = { name = palace_entrants }
							is_in_guest_subset = { name = failees }
						}
					}
				}
				imperial_examination_disburse_activity_entrant_rewards = yes
			}
		}
		#Clean up variables set on participants
		imperial_examination_clean_up_variables_effect = yes
	}
}

imperial_examination.7001 = {
	type = activity_event
	title = imperial_examination.7001.t
	desc = {
		desc = imperial_examination.7001.desc
		first_valid = {
			triggered_desc = {
				trigger = { this = scope:host }
				desc = imperial_examination.7001.desc.host
			}
			triggered_desc = {
				trigger = {
					# We can't check for intents at this stage. Flags are set in activity on_end
					has_character_flag = exam_taker
					has_variable = been_caught_cheating_longterm
				}
				desc = imperial_examination.7001.desc.cheater
			}
			triggered_desc = {
				trigger = { this = scope:first_place_entrant }
				desc = imperial_examination.7001.desc.entrant.first
			}
			triggered_desc = {
				trigger = {
					is_in_guest_subset = { name = entrants }
					# We can't check for intents at this stage. Flags are set in activity on_end
					has_character_flag = exam_taker
				}
				desc = imperial_examination.7001.desc.entrant
			}
			desc = imperial_examination.7001.desc.fallback
		}
	}
	theme = imperial_examination
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { this = scope:first_place_entrant }
			animation = war_over_win
		}
		animation = war_over_tie
	}
	right_portrait = {
		trigger = { this != scope:first_place_entrant }
		character = scope:first_place_entrant
		animation = war_over_win
	}

	immediate = {
		scope:activity = {
			#Ease of access
			var:first_place_entrant ?= { save_scope_as = first_place_entrant }
		}
		if = {
			limit = {
				OR = {
					is_in_guest_subset = { name = entrants }
					is_in_guest_subset = { name = palace_entrants }
				}
			}
			# Desribe your score
			if = {
				limit = { has_perfect_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_perfect_score
			}
			if = {
				limit = { has_good_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_great_score
			}
			if = {
				limit = { has_decent_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_decent_score
			}
			if = {
				limit = { has_questionable_score_trigger = { SCORE_VAR = imperial_examination_score } }
				set_variable = has_questionable_score
			}
		}
	}

	option = {
		name = imperial_examination.7001.a
		#Hand out rewards
		#Emperor
		show_as_tooltip = {
			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 = {
					this = {
						OR = {
							is_in_guest_subset = { name = entrants }
							is_in_guest_subset = { name = palace_entrants }
							is_in_guest_subset = { name = failees }
						}
					}
				}
				local_examination_disburse_activity_entrant_rewards = yes
			}
		}
		#Clean up variables set on participants
		imperial_examination_clean_up_variables_effect = yes

		ai_chance = {
			base = 100
		}
	}
}

imperial_examination.7002 = {
	type = activity_event
	title = imperial_examination.7001.t
	desc = {
		desc = imperial_examination.7001.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants = 1
				}
				desc = imperial_examination.7100.desc.intro_nr_of_relatives_singular
			}
			desc = imperial_examination.7100.desc.intro_nr_of_relatives
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 1
					var:num_exam_family_entrants_passed = 0
				}
				desc = imperial_examination.7100.desc.all_failed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 1
					var:num_exam_family_entrants_failed = 0
				}
				desc = imperial_examination.7100.desc.all_passed_provincial
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants = 2
				}
				desc = imperial_examination.7100.desc.one_passed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 2
				}
				desc = imperial_examination.7100.desc.win_lose_ratio
			}
		}
	}
	theme = imperial_examination

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { var:num_exam_family_entrants_passed = 0 }
			animation = disbelief
		}
		animation = war_over_tie
	}

	immediate = {
		local_examination_grab_nr_of_family_entrants_effect = yes
	}

	trigger = {
		any_in_list = {
			list = parent_relevant_entrants
			is_alive = yes
		}
	}

	option = {
		name = imperial_examination.7001.a
		
		if = {
			limit = {
				has_activity_intent = exam_assist_exam_intent
			}
			custom_tooltip = intent_coronation_opinion_gain
			change_merit = {
				value = minor_merit_gain
				scope:activity = {
					every_guest_subset = {
						name = entrants
						add = 3
					}
				}
			}
		}

		ai_chance = {
			base = 100
		}
	}
}

imperial_examination.7003 = {
	type = activity_event
	title = imperial_examination.7001.t
	desc = {
		desc = imperial_examination.7003.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants = 1
				}
				desc = imperial_examination.7100.desc.intro_nr_of_relatives_singular
			}
			desc = imperial_examination.7100.desc.intro_nr_of_relatives
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 1
					var:num_exam_family_entrants_passed = 0
				}
				desc = imperial_examination.7100.desc.all_failed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 1
					var:num_exam_family_entrants_failed = 0
				}
				desc = imperial_examination.7100.desc.all_passed_metropolitan
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants = 2
				}
				desc = imperial_examination.7100.desc.one_passed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 2
				}
				desc = imperial_examination.7100.desc.win_lose_ratio
			}
		}
	}
	theme = imperial_examination

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { var:num_exam_family_entrants_passed = 0 }
			animation = disbelief
		}
		animation = war_over_tie
	}

	immediate = {
		imperial_examination_grab_nr_of_family_entrants_effect = yes
	}

	option = {
		name = imperial_examination.7001.a
		
		if = {
			limit = {
				has_activity_intent = exam_assist_exam_intent
			}
			custom_tooltip = intent_coronation_opinion_gain
			change_merit = {
				value = minor_merit_gain
				scope:activity = {
					every_guest_subset = {
						name = entrants
						add = 3
					}
				}
			}
		}

		ai_chance = {
			base = 100
		}
	}
}

#######################
### CLOSE FAMILY NOTIFICATION EVENTS
#######################

# Intro event
imperial_examination.7100 = {
	type = character_event
	title = imperial_examination.7100.t
	desc = {
		desc = imperial_examination.7100.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants = 1
				}
				desc = imperial_examination.7100.desc.intro_nr_of_relatives_singular
			}
			desc = imperial_examination.7100.desc.intro_nr_of_relatives
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 1
					var:num_exam_family_entrants_passed = 0
				}
				desc = imperial_examination.7100.desc.all_failed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 1
					var:num_exam_family_entrants_failed = 0
				}
				desc = imperial_examination.7100.desc.all_passed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants = 2
				}
				desc = imperial_examination.7100.desc.one_passed
			}
			triggered_desc = {
				trigger = {
					var:num_exam_family_entrants > 2
				}
				desc = imperial_examination.7100.desc.win_lose_ratio
			}
		}
		triggered_desc = {
			trigger = {
				var:num_exam_family_passed_palace >= 1
				var:num_exam_family_entrants > 2
				NOT = {
					var:num_exam_family_passed_palace = var:num_exam_family_entrants_passed
				}
			}
			desc = imperial_examination.7100.desc.won_palace
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:palace_entrant
				}
				desc = imperial_examination.7100.desc.palace_entrant
			}
			triggered_desc = {
				trigger = {
					exists = scope:metropolitan_entrant
				}
				desc = imperial_examination.7100.desc.metropolitan_entrant
			}
		}
		triggered_desc = {
			trigger = {
				exists = scope:failed_entrant
			}
			desc = imperial_examination.7100.desc.failed_entrant
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:house_member = {
						has_character_flag = been_caught_cheating
					}
				}
				desc = imperial_examination.7100.desc.cheater
			}
			triggered_desc = {
				trigger = {
					scope:house_member = {
						has_character_flag = exam_got_elder
					}
				}
				desc = imperial_examination.7100.desc.got_elder
			}
			triggered_desc = {
				trigger = {
					scope:house_member = {
						has_character_flag = failed_imperial_examination
					}
				}
				desc = imperial_examination.7100.desc.failed_fallback
			}
		}
	}

	theme = imperial_examination

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				scope:house_member = {
					has_character_flag = passed_palace_exam
				}
			}
			animation = admiration
		}
		triggered_animation = {
			trigger = {
				scope:house_member = {
					has_character_flag = passed_metropolitan_exam
				}
			}
			animation = interested
		}
		animation = pondering
	}

	right_portrait = {
		character = scope:house_member
		triggered_animation = {
			trigger = {
				scope:house_member = {
					OR = {
						has_character_flag = passed_palace_exam
						has_character_flag = passed_metropolitan_exam
					}
				}
			}
			animation = personality_bold
		}
		animation = shame
	}

	immediate = {
		scope:activity.activity_location = { save_scope_as = location }
		# Buckle up boyo - this is going to be a long one
		# First, let's see how many close family members attended the event
		set_variable = {
			name = num_exam_family_entrants
			value = 0
		}
		every_in_list = {
			list = parent_relevant_entrants
			root = {
				change_variable = {
					name = num_exam_family_entrants
					add = 1
				}
			}
		}
		# Then measure how many characters did pass the exam
		set_variable = {
			name = num_exam_family_entrants_passed
			value = 0
		}
		every_in_list = {
			list = parent_relevant_entrants
			limit = {
				OR = {
					has_character_flag = passed_palace_exam
					has_character_flag = passed_metropolitan_exam
				}
			}
			root = {
				change_variable = {
					name = num_exam_family_entrants_passed
					add = 1
				}
			}
		}
		# More specifically, how many passed the Metropolitan exam
		set_variable = {
			name = num_exam_family_passed_metropolitan
			value = 0
		}
		every_in_list = {
			list = parent_relevant_entrants
			limit = {
				has_character_flag = passed_metropolitan_exam
				NOT = { has_character_flag = passed_palace_exam }
			}
			root = {
				change_variable = {
					name = num_exam_family_passed_metropolitan
					add = 1
				}
			}
		}
		# And how many made it to the Palace exam
		set_variable = {
			name = num_exam_family_passed_palace
			value = 0
		}
		every_in_list = {
			list = parent_relevant_entrants
			limit = {
				has_character_flag = passed_palace_exam
			}
			root = {
				change_variable = {
					name = num_exam_family_passed_palace
					add = 1
				}
			}
		}
		# Finally, how many did bitterly disappoint...
		set_variable = {
			name = num_exam_family_entrants_failed
			value = 0
		}
		every_in_list = {
			list = parent_relevant_entrants
			limit = { has_character_flag = failed_imperial_examination }
			root = {
				change_variable = {
					name = num_exam_family_entrants_failed
					add = 1
				}
			}
		}
		# Let's grab a suitable candidate to focus on in the event. We're checking individual flags instead of an OR so we make sure to fetch the most relevant char
		if = {
			limit = {
				any_in_list = {
					list = parent_relevant_entrants
					has_character_flag = passed_palace_exam
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_palace_exam
				}
				weight = {
					base = 1
					modifier = {
						is_heir_of = root
						add = 50
					}
					modifier = {
						OR = {
							has_relation_friend = root
							has_relation_best_friend = root
						}
						add = 10
					}
				}
				save_scope_as = palace_entrant
				save_scope_as = house_member
			}
		}
		else_if = {
			limit = {
				any_in_list = {
					list = parent_relevant_entrants
					has_character_flag = passed_metropolitan_exam
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_metropolitan_exam
				}
				weight = {
					base = 1
					modifier = {
						is_heir_of = root
						add = 50
					}
					modifier = {
						OR = {
							has_relation_friend = root
							has_relation_best_friend = root
						}
						add = 10
					}
				}
				save_scope_as = metropolitan_entrant
				save_scope_as = house_member
			}
		}
		else_if = {
			limit = {
				any_in_list = {
					list = parent_relevant_entrants
					has_character_flag = been_caught_cheating
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = been_caught_cheating
				}
				save_scope_as = failed_entrant
				save_scope_as = house_member
			}
		}
		else_if = {
			limit = {
				any_in_list = {
					list = parent_relevant_entrants
					has_character_flag = exam_got_elder
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = exam_got_elder
				}
				save_scope_as = failed_entrant
				save_scope_as = house_member
			}
		}
		else_if = {
			limit = {
				any_in_list = {
					list = parent_relevant_entrants
					has_character_flag = failed_imperial_examination
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = failed_imperial_examination
				}
				weight = {
					base = 1
					modifier = {
						is_heir_of = root
						add = 50
					}
					modifier = {
						OR = {
							has_relation_friend = root
							has_relation_best_friend = root
						}
						add = 10
					}
				}
				save_scope_as = failed_entrant
				save_scope_as = house_member
			}
		}
		if = {
			limit = {
				scope:house_member = {
					has_character_flag = exam_got_elder
				}
			}
			scope:house_member = {
				every_relation = {
					type = elder
					save_scope_as = elder
				}
			}
		}
	}

	trigger = {
		# Event is triggered via the examination activity on_end
	}

	option = { # I must congratulate the Palace entrants!
		name = {
			trigger = { var:num_exam_family_passed_palace = 1 }
			text = imperial_examination.7100.a.singular
		}
		name = {
			trigger = { var:num_exam_family_passed_palace > 1 }
			text = imperial_examination.7100.a.plural
		}
		trigger = { var:num_exam_family_passed_palace >= 1 }
		if = {
			limit = {
				var:num_exam_family_passed_palace > 1
			}
			custom_description_no_bullet = {
				text = influence_family_effect
			}
			every_in_list = {
				list = parent_relevant_entrants
				limit = { has_character_flag = passed_palace_exam }
				add_opinion = {
					target = root
					modifier = pleased_opinion
					opinion = 10
				}
			}
		}
		else = {
			custom_description_no_bullet = {
				text = imperial_examination.7100.a.tt
			}
		}
		trigger_event = imperial_examination.7055
	}

	option = { # I wish to address the Metropolitan entrants
		name = {
			trigger = { var:num_exam_family_passed_metropolitan = 1 }
			text = imperial_examination.7100.b.singular
		}
		name = {
			trigger = { var:num_exam_family_passed_metropolitan > 1 }
			text = imperial_examination.7100.b.plural
		}
		trigger = { var:num_exam_family_passed_metropolitan >= 1 }
		if = {
			limit = {
				var:num_exam_family_passed_metropolitan > 1
			}
			custom_description_no_bullet = {
				text = influence_family_effect
			}
			every_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_metropolitan_exam
					NOT = { has_character_flag = passed_palace_exam }
				}
				add_opinion = {
					target = root
					modifier = pleased_opinion
					opinion = 5
				}
			}
		}
		else = {
			custom_description_no_bullet = {
				text = imperial_examination.7100.b.tt
			}
		}
		trigger_event = imperial_examination.7050
	}

	option = { # The failees must be disciplined!
		name = imperial_examination.7100.c
		trigger = { var:num_exam_family_entrants_failed >= 1 }
		if = {
			limit = {
				var:num_exam_family_entrants_failed > 1
			}
			custom_description_no_bullet = {
				text = influence_family_effect
			}
			every_in_list = {
				list = parent_relevant_entrants
				limit = { has_character_flag = failed_imperial_examination }
				add_opinion = {
					target = root
					modifier = annoyed_opinion
					opinion = -5
				}
			}
		}
		else = {
			custom_description_no_bullet = {
				text = imperial_examination.7100.c.tt
			}
		}
		trigger_event = imperial_examination.7060
	}

	option = { # Opt out
		name = imperial_examination.7100.d
		stress_impact = { base = minor_stress_impact_loss }
	}

	after = {
		remove_character_flag = triggered_family_followup
	}
}

scripted_effect clear_exam_notification_var_effect = {
	every_in_list = {
		list = parent_relevant_entrants
		remove_character_flag ?= exam_got_elder
		remove_variable = num_exam_family_entrants
		remove_variable = num_exam_family_entrants_passed
		remove_variable = num_exam_family_passed_metropolitan
		remove_variable = num_exam_family_passed_palace
		remove_variable = num_exam_family_entrants_failed
	}
}

# Metropolitan Entrant
imperial_examination.7050 = {
	type = character_event
	title = imperial_examination.7050.t
	desc = imperial_examination.7050.desc

	theme = imperial_examination

	left_portrait = {
		character = root
		animation = interested
	}

	right_portrait = {
		character = scope:metropolitan_entrant
		animation = personality_bold
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					exists = scope:metropolitan_entrant
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_metropolitan_exam
					NOT = { this = scope:palace_entrant }
				}
				save_scope_as = metropolitan_entrant
			}
		}
	}

	option = { # Try again, or else!
		name = {
			trigger = { var:num_exam_family_passed_metropolitan = 1 }
			text = imperial_examination.7050.a.singular
		}
		name = {
			trigger = { var:num_exam_family_passed_metropolitan > 1 }
			text = imperial_examination.7050.a.plural
		}
		if = {
			limit = {
				var:num_exam_family_passed_metropolitan = 1
			}
			custom_tooltip = imperial_examination.7050.a.tt_singular
			scope:metropolitan_entrant = {
				add_character_modifier = {
					modifier = tgp_forced_to_retake_exam_modifier
					years = 15
				}
			}
		}
		else = {
			every_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_metropolitan_exam
					NOT = { has_character_flag = passed_palace_exam }
				}
				custom = custom.every_passing_family_member
				add_character_modifier = {
					modifier = tgp_forced_to_retake_exam_modifier
					years = 15
				}
			}
			custom_description_no_bullet = {
				text = demand_retake_exam_effect
			}
		}
		stress_impact = {
			diligent = minor_stress_impact_loss
		}
	}

	option = { # Take credit
		name = imperial_examination.7050.b
		change_influence = {
			value = minor_influence_gain
			multiply = relevant_family_members_metropolitan_value
		}
		stress_impact = {
			just = medium_stress_impact_gain
			ambitious = medium_stress_impact_loss
			arrogant = minor_stress_impact_loss
		}
	}

	option = { # Opt out
		name = {
			trigger = { var:num_exam_family_entrants = 1 }
			text = imperial_examination.7050.c.singular
		}
		name = {
			trigger = { var:num_exam_family_entrants > 1 }
			text = imperial_examination.7050.c.plural
		}
		if = {
			limit = {
				var:num_exam_family_entrants = 1
			}
			scope:metropolitan_entrant = {
				stress_impact = {
					base = medium_stress_impact_loss
				}
			}
		}
		else = {
			every_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_metropolitan_exam
					NOT = { has_character_flag = passed_palace_exam }
				}
				custom = custom.every_passing_family_member
				stress_impact = {
					base = medium_stress_impact_loss
				}
			}
			stress_impact = {
				compassionate = minor_stress_impact_loss
				trusting = minor_stress_impact_loss
			}
		}

	}

	after = {
		clear_exam_notification_var_effect = yes
	}
}

# Palace entrant
imperial_examination.7055 = {
	type = character_event
	title = imperial_examination.7055.t
	desc = imperial_examination.7055.desc

	theme = imperial_examination

	left_portrait = {
		character = root
		animation = admiration
	}

	right_portrait = {
		character = scope:palace_entrant
		animation = happiness
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					exists = scope:palace_entrant
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = passed_palace_exam
				}
				save_scope_as = palace_entrant
			}
		}
	}

	option = { # Take credit
		name = imperial_examination.7050.b
		change_influence = {
			value = medium_influence_gain
			multiply = relevant_family_members_palace_value
		}

		stress_impact = {
			just = medium_stress_impact_gain
			ambitious = medium_stress_impact_loss
			arrogant = minor_stress_impact_loss
		}
	}

	option = { # As expected!
		name = imperial_examination.7055.b
		dynasty = {
			add_dynasty_prestige = {
				value = minor_dynasty_prestige_gain
				multiply = relevant_family_members_palace_value
			}
		}
		stress_impact = {
			ambitious = medium_stress_impact_loss
		}
	}

	after = {
		clear_exam_notification_var_effect = yes
	}
}

# Failed entrant
imperial_examination.7060 = {
	type = character_event
	title = imperial_examination.7060.t
	desc = imperial_examination.7060.desc

	theme = imperial_examination

	left_portrait = {
		character = root
		animation = pondering
	}

	right_portrait = {
		character = scope:failed_entrant
		animation = shame
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					exists = scope:failed_entrant
				}
			}
			random_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = failed_imperial_examination
				}
				save_scope_as = failed_entrant
			}
		}
	}

	option = { # Try again, or else!
		name = {
			trigger = { var:num_exam_family_entrants_failed = 1 }
			text = imperial_examination.7050.a.singular
		}
		name = {
			trigger = { var:num_exam_family_entrants_failed > 1 }
			text = imperial_examination.7050.a.plural
		}
		if = {
			limit = {
				var:num_exam_family_entrants_failed = 1
			}
			custom_tooltip = imperial_examination.7050.a.tt_singular
			scope:failed_entrant = {
				add_character_modifier = {
					modifier = tgp_forced_to_retake_exam_modifier
					years = 15
				}
			}
		}
		else = {
			every_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = failed_imperial_examination
				}
				custom = custom.every_failed_family_member
				add_character_modifier = {
					modifier = tgp_forced_to_retake_exam_modifier
					years = 15
				}
			}
			custom_description_no_bullet = {
				text = demand_retake_exam_effect
			}
		}
		stress_impact = {
			diligent = minor_stress_impact_loss
		}
	}

	option = { # Failed entrant - Maybe I can do something...
		name = imperial_examination.7060.b
		trigger = {
			scope:failed_entrant = {
				NOT = { has_character_flag = been_caught_cheating }
			}
		}
		if = {
			limit = {
				var:num_exam_family_entrants_failed = 1
			}
			custom_tooltip = imperial_examination.7060.b.tt_singular
			scope:failed_entrant = {
				remove_character_flag = failed_imperial_examination
				add_character_flag = passed_metropolitan_exam
			}
		}
		else = {
			custom_tooltip = imperial_examination.7060.b.tt_plural
			every_in_list = {
				list = parent_relevant_entrants
				limit = {
					has_character_flag = failed_imperial_examination
				}
				remove_character_flag = failed_imperial_examination
				add_character_flag = passed_metropolitan_exam
			}
		}
		change_influence = medium_influence_loss
	}

	option = { # Cheating Entrant - you should have tried harder...
		name = imperial_examination.7060.c
		trigger = {
			scope:failed_entrant = {
				has_character_flag = been_caught_cheating
			}
		}
		scope:failed_entrant = {
			if = {
				limit = {
					NOT = { has_lifestyle = intrigue_lifestyle }
				}
				set_focus = intrigue_skulduggery_focus
			}
			add_intrigue_lifestyle_perk_points = 2
		}
		stress_impact = {
			schemer = medium_stress_impact_loss
			honest = medium_stress_impact_gain
		}
	}

	option = { # Failed entrant - For shame!
		name = imperial_examination.7060.d
		add_piety = minor_piety_gain
		stress_impact = {
			zealous = medium_stress_impact_loss
			diligent = minor_stress_impact_loss
		}
	}

	after = {
		clear_exam_notification_var_effect = yes
	}
}
