
namespace = tgp_east_asia_mandala_events

tgp_east_asia_mandala_events.0010 = {
	hidden = yes

	trigger = {
		house = { has_house_head_parameter = aspect_of_creation }
		NOT = {
			has_variable = mandala_house_power_accumulated_creator
		}
	}

	immediate = {
		#We set this here to not bug out the modifier
		set_variable = {
			name = mandala_house_power_accumulated_creator
			value = 1
		}
		#Notify the player
		send_interface_message = {
			type = event_tribute_bad_with_text
			title = mandala_creator_streak_broken
			desc = mandala_creator_streak_broken_desc
			left_icon = root
			if = {
				limit = {
					house = { has_house_head_parameter = creator_30_year_modifier }
				}
				show_as_tooltip = {
					add_character_modifier = { 
						modifier = mandala_creator_modifier
						years = 30
					}
				}
				#Re-check again in 30 years (the trigger at the top should stop recursive shenanigans)
				trigger_event = {
					id = tgp_east_asia_mandala_events.0010
					days = 10951 # 30 years and 1 day
				}
			}
			else_if = {
				limit = {
					house = { house_has_creator_20_years_trigger = yes }
				}
				show_as_tooltip = {
					add_character_modifier = { 
						modifier = mandala_creator_modifier
						years = 20
					}
				}
				#Re-check again in 20 years
				trigger_event = {
					id = tgp_east_asia_mandala_events.0010
					days = 7301 # 20 years and 1 day
				}
			}
			else = {
				show_as_tooltip = {
					add_character_modifier = { 
						modifier = mandala_creator_modifier
						years = 10
					}
				}
				#Re-check again in 10 years
				trigger_event = {
					id = tgp_east_asia_mandala_events.0010
					days = 3651 # 10 years and 1 day
				}
			}
		}
		#Then remove it again because you didn't earn it by constructing
		remove_variable = mandala_house_power_accumulated_creator
	}
}

scripted_trigger natural_causes_death_reason_trigger = {
	OR = {
		death_reason = death_peaceful
		death_reason = death_natural_causes
		death_reason = death_old_age
		death_reason = death_laughter
		death_reason = death_heart_attack
		death_reason = death_giant
		death_reason = death_spindly
		death_reason = death_wheezing
		death_reason = death_bleeder
		death_reason = death_depressed
		death_reason = death_lunatic
		death_reason = death_possessed
		death_reason = death_incapable
		death_reason = death_physique_bad_1
		death_reason = death_physique_bad_2
		death_reason = death_physique_bad_3
		death_reason = death_weak
		death_reason = death_malnourishment
		death_reason = death_obesity
		death_reason = blind
		death_reason = death_inbred
		death_reason = death_apoplexy
		death_reason = death_euthanasia
		death_reason = death_euthanasia_killer
		death_reason = death_choked
		death_reason = death_roof_tile
		death_reason = death_stress
		death_reason = death_childbirth
		death_reason = death_thirst
		death_reason = death_feast_accident
		death_reason = death_hypothermia
		death_reason = death_heatstroke
		death_reason = death_struck_by_lightning
	}
}

scripted_trigger religious_death_reasons_trigger = {
	OR = {
		death_reason = death_martyrdom
		death_reason = death_martyrdom_killer
		death_reason = death_sacrificed_to_gods
		death_reason = death_crucified
		death_reason = death_crucified_by_mob
		death_reason = death_by_buddhists
		death_reason = death_ritual_suicide
	}
}

#Notify the heir that their Piety game may level up
tgp_east_asia_mandala_events.0100 = {
	hidden = yes

	trigger = {
		government_has_flag = government_is_mandala
		exists = house
		has_mandala_aspect_trigger = yes
	}

	immediate = {
		#Acceptable deaths for each Aspect type
		if = {
			limit = {
				#They were due for a level-up
				OR = {
					exists = scope:apply_godlike
					exists = scope:apply_budding_deity
					exists = scope:apply_budding_divinity
				}
				#And died an appropriate death
				OR = {
					AND = {
						house = { has_house_aspiration_parameter = aspect_of_creation }
						scope:deceased_mandala_ruler = {
							OR = {
								natural_causes_death_reason_trigger = yes
								religious_death_reasons_trigger = yes
							}
						}
					}
					AND = {
						house = { has_house_aspiration_parameter = aspect_of_serenity }
						scope:deceased_mandala_ruler = { 
							OR = {
								natural_causes_death_reason_trigger = yes
								religious_death_reasons_trigger = yes
							}
						}
					}
					AND = {
						house = { has_house_aspiration_parameter = aspect_of_destruction }
						scope:deceased_mandala_ruler = { 
							OR = {
								natural_causes_death_reason_trigger = yes
								religious_death_reasons_trigger = yes
								death_reason = death_contest_melee_accident
								death_reason = death_contest_joust_accident
								death_reason = death_contest_wrestling_accident
								death_reason = death_contest_duel_accident
								death_reason = death_contest_horse_sport_accident
								death_reason = death_tournament_pas_darmes_defend
								death_reason = death_tournament_pas_darmes_challenge
								death_reason = death_tournament_piledriver
								death_reason = death_tournament_melee_throat
								death_reason = death_tournament_roundhouse
								death_reason = death_duel
								death_reason = death_wounds
								death_reason = death_fight
								death_reason = death_fight_killer
								death_reason = death_battle
								death_reason = death_sun_trial
								death_reason = death_martyrdom
							}
						}
					}
					AND = {
						house = { has_house_aspiration_parameter = aspect_of_trickery }
						scope:deceased_mandala_ruler = { 
							OR = {
								natural_causes_death_reason_trigger = yes
								religious_death_reasons_trigger = yes
								death_reason = death_murder
								death_reason = death_plotting
								death_reason = death_poison
								death_reason = death_snake
								death_reason = death_mysterious
								death_reason = death_snakes
							}
						}
					}
				}
			}
			send_interface_message = {
				type = msg_mandala_good_with_text
				title = budding_divinity_msg
				desc = budding_divinity_msg_desc
				left_icon = root
				#If we're not already capped
				if = {
					limit = { exists = scope:apply_godlike }
					add_character_modifier = budding_godhood_modifier
				}
				else_if = {
					limit = { exists = scope:apply_budding_deity }
					add_character_modifier = budding_deity_modifier
				}
				else = { add_character_modifier = budding_divinity_modifier }
				#Piety level/piety inheritance
				if = {
					limit = { exists = scope:deceased_mandala_ruler_piety }
					add_piety = scope:deceased_mandala_ruler_piety
				}
				else = {
					add_piety_level = {
						value = {
							add = scope:deceased_mandala_ruler_piety_level
							subtract = piety_level
							min = 1
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				#They were due for a level-up
				OR = {
					exists = scope:apply_godlike
					exists = scope:apply_budding_deity
					exists = scope:apply_budding_divinity
				}
				#But died incorrectly
			}
			send_interface_message = {
				type = msg_mandala_neutral_with_text
				title = not_budding_divinity_msg
				desc = not_budding_divinity_msg_desc
				left_icon = root
				#Piety level/piety inheritance
				if = {
					limit = { exists = scope:deceased_mandala_ruler_piety }
					add_piety = scope:deceased_mandala_ruler_piety
				}
				else = {
					add_piety_level = {
						value = {
							add = scope:deceased_mandala_ruler_piety_level
							subtract = piety_level
							min = 1
						}
					}
				}
			}
		}
		else = {
			send_interface_message = {
				type = msg_mandala_good_with_text
				title = piety_inherited_msg
				desc = piety_inherited_msg_desc
				left_icon = root
				#Piety level/piety inheritance
				if = {
					limit = { exists = scope:deceased_mandala_ruler_piety }
					add_piety = scope:deceased_mandala_ruler_piety
				}
				else = {
					add_piety_level = {
						value = {
							add = scope:deceased_mandala_ruler_piety_level
							subtract = piety_level
							min = 1
						}
					}
				}
			}
		}
		if = {
			limit = { is_adult = yes }
			trigger_event = {
				id = tgp_east_asia_mandala_events.0101
				delayed = yes
			}
		}
		#Get marked for your 16th birthday
		else = {
			set_variable = {
				name = mandala_succession_waiting_on_adulthood
				value = scope:deceased_mandala_ruler
			}
			send_interface_message = {
				type = msg_mandala_neutral_text
				title = mandala_succession_awaiting_adulthood_msg
				desc = mandala_succession_awaiting_adulthood_msg_desc
				left_icon = root
			}
		}
		#SEA LEgacy
		if = {
			limit = {
				dynasty ?= { has_dynasty_perk = tgp_sea_legacy_4 }
			}
			add_character_modifier = {
				modifier = tgp_sea_legacy_heir_modifier
				years = 10
			}
		}
	}
}

#The lead up to the initiation event. Are you Traveling?
tgp_east_asia_mandala_events.0101 = {
	hidden = yes

	trigger = {
		government_has_flag = government_is_mandala
		exists = house
		has_mandala_aspect_trigger = yes
		is_adult = yes
	}

	#You dun' messed up, kid. Lose some devotion.
	on_trigger_fail = {
		if = {
			limit = { government_has_flag = government_is_mandala }
			trigger_event = {
				id = tgp_east_asia_mandala_events.8497
				delayed = yes
			}
		}
	}

	immediate = {
		#Travel gets a delayed Succession
		if = {
			limit = { exists = current_travel_plan }
			set_variable = {
				name = mandala_succession_waiting_on_travel_finish
				value = scope:deceased_mandala_ruler
			}
			send_interface_message = {
				type = msg_mandala_neutral_text
				title = pending_mandala_succession_msg
				desc = pending_mandala_succession_msg_desc
				left_icon = root
			}
		}
		#Otherwise we kick it off
		else = {
			trigger_event = {
				id = tgp_east_asia_mandala_events.8499
				delayed = yes
			}
		}
	}
}

#You're a kid Mandala who inherited a kid Mandala who inherited an adult but didn't manage to turn 16
tgp_east_asia_mandala_events.0102 = {
	hidden = yes

	trigger = {
		government_has_flag = government_is_mandala
		exists = house
		has_mandala_aspect_trigger = yes
	}

	immediate = {
		send_interface_message = {
			type = msg_mandala_neutral_with_text
			title = kid_piety_inherited_msg
			desc = kid_piety_inherited_msg_desc
			left_icon = root
			add_piety = scope:deceased_kid_mandala_ruler_piety
			if = {
				limit = { scope:deceased_kid_mandala_ruler_piety_level > 0 }
				add_piety_level = {
					value = {
						add = scope:deceased_kid_mandala_ruler_piety_level
						subtract = piety_level
						min = 1
					}
				}
			}
		}
		set_variable = {
			name = mandala_succession_waiting_on_adulthood
			value = scope:deceased_mandala_ruler
		}
		send_interface_message = {
			type = msg_mandala_neutral_text
			title = mandala_succession_awaiting_adulthood_msg
			desc = mandala_succession_awaiting_adulthood_msg_desc
			left_icon = root
		}
	}
}

#Keep track of the Aspect modifiers
tgp_east_asia_mandala_events.0200 = {
	hidden = yes

	trigger = {
		government_has_flag = government_is_mandala
		has_variable = mandala_house_power_accumulated_increments_of_peace
	}

	immediate = {
		#No more boon for you
		if = {
			limit = { 
				OR = {
					is_house_head = no
					NOT = { 
						house = { has_house_aspiration_parameter = aspect_of_serenity }
					}
				}
			}
			mandala_remove_increment_of_peace_effect = yes
		}
		#Otherwise proceed as per usual
		else = {
			if = {
				limit = {
					any_character_war = {
						primary_attacker = prev #The character in question
					}
				}
				mandala_reset_increment_of_peace_effect = yes
			}
			else = {
				mandala_upgrade_increment_of_peace_effect = yes
			}
			#Then set up the next one
			if = {
				limit = {
					house = { has_house_head_parameter = peaceful_rule_yearly_modifier }
				}
				trigger_event = {
					id = tgp_east_asia_mandala_events.0200
					years = 1
				}
			}
			else_if = {
				limit = {
					house = { house_has_peaceful_rule_3_year_trigger = yes }
				}
				trigger_event = {
					id = tgp_east_asia_mandala_events.0200
					years = 3
				}
			}
			else = {
				trigger_event = {
					id = tgp_east_asia_mandala_events.0200
					years = 5
				}
			}
		}
		
	}
}

#Track years of peace for Serenity
tgp_east_asia_mandala_events.0205 = {
	hidden = yes

	trigger = {
		government_has_flag = government_is_mandala
		house ?= { has_house_aspiration_parameter = aspect_of_serenity }
		is_house_head = yes
		has_variable = serenity_mandala_years_of_non_aggression_war
	}

	immediate = {
		if = {
			limit = {
				any_character_war = {
					primary_attacker = prev #The character in question
				}
			}
			remove_variable = serenity_mandala_years_of_non_aggression_war
			set_variable = {
				name = serenity_mandala_years_of_non_aggression_war
				value = 0
			}
		}
		else = {
			increment_variable_effect = {
				VAR = serenity_mandala_years_of_non_aggression_war
				VAL = 1
			}
		}
		trigger_event = {
			id = tgp_east_asia_mandala_events.0205
			years = 1
		}
	}
}


scripted_effect mandala_events_1000_random_onlooker_effect = {
	random_courtier = {
		limit = { 
			is_available_adult = yes
			this != root
		}
		save_scope_as = onlooker
	}
	if = {
		limit = {
			NOT = { exists = scope:onlooker }
		}
		random_pool_character = {
			province = root.capital_province
			limit = { is_available_adult = yes }
			save_scope_as = onlooker
		}
	}
	random_courtier = {
		limit = { 
			is_available_adult = yes
			this != root
			this != scope:onlooker
		}
		save_scope_as = onlooker_2
	}
	if = {
		limit = {
			NOT = { exists = scope:onlooker_2 }
		}
		random_pool_character = {
			province = root.capital_province
			limit = { is_available_adult = yes }
			save_scope_as = onlooker_2
		}
	}
}

##ASPECT LEVEL UP
tgp_east_asia_mandala_events.1000 = {
	type = character_event
	window = big_event_window
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					house = {
						OR = {
							has_house_aspiration_parameter = aspect_of_creation_05
							has_house_aspiration_parameter = aspect_of_serenity_05
							has_house_aspiration_parameter = aspect_of_destruction_05
							has_house_aspiration_parameter = aspect_of_trickery_05
						}
					}
				}
				desc = tgp_east_asia_mandala_events.1000.t.final
			}
			desc = tgp_east_asia_mandala_events.1000.t
		}
	}
	desc = {
		first_valid = {
			#Level 02
			triggered_desc = {
				trigger = {
					house = {
						OR = {
							has_house_aspiration_parameter = aspect_of_creation_02
							has_house_aspiration_parameter = aspect_of_serenity_02
							has_house_aspiration_parameter = aspect_of_destruction_02
							has_house_aspiration_parameter = aspect_of_trickery_02
						}
					}
				}
				desc = tgp_east_asia_mandala_events.1000.desc.02
			}
			#Creation
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_creation_03 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.creation_03
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_creation_04 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.creation_04
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_creation_05 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.creation_05
				}
			#Serenity
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_serenity_03 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.serenity_03
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_serenity_04 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.serenity_04
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_serenity_05 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.serenity_05
				}
			#Destruction
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_destruction_03 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.destruction_03
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_destruction_04 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.destruction_04
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_destruction_05 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.destruction_05
				}
			#Trickery
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_trickery_03 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.trickery_03
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_trickery_04 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.trickery_04
				}
				triggered_desc = {
					trigger = {
						house = { has_house_aspiration_parameter = aspect_of_trickery_05 }
					}
					desc = tgp_east_asia_mandala_events.1000.desc.trickery_05
				}
		}
	}
	theme = mandala
	override_background = { 
		trigger = {
			house = { has_house_aspiration_parameter = aspect_of_trickery }
		}
		reference = courtyard
	}
	override_background = { 
		trigger = {
			house = { has_house_aspiration_parameter = aspect_of_serenity }
		}
		reference = ep2_wedding_ceremony
	}
	override_background = { 
		trigger = {
			house = { has_house_aspiration_parameter = aspect_of_destruction }
		}
		reference = ep2_tournament
	}
	override_background = { reference = feast }
	left_portrait = {
		character = scope:onlooker
		camera = camera_event_scheme_center_look_semi_right
		triggered_animation = {
			trigger = { 
				root = {
					house = { has_house_aspiration_parameter = aspect_of_destruction }
				}
			}
			animation = random_weapon_celebrate
		}
		animation = toast
	}	
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { 
				house = { has_house_aspiration_parameter = aspect_of_creation }
			}
			animation = flirtation
		}
		triggered_animation = {
			trigger = { 
				house = { has_house_aspiration_parameter = aspect_of_serenity }
			}
			animation = personality_zealous
		}
		triggered_animation = {
			trigger = { 
				house = { has_house_aspiration_parameter = aspect_of_destruction }
			}
			animation = war_over_win
		}
		camera = camera_event_scheme_center
		animation = scheme	
	}
	right_portrait = {
		character = scope:onlooker_2
		camera = camera_event_scheme_vs_extra_right_look_left
		triggered_animation = {
			trigger = { 
				root = {
					house = { has_house_aspiration_parameter = aspect_of_destruction }
				}
			}
			animation = random_weapon_coup_degrace
		}
		animation = ecstasy
	}
	override_effect_2d = {
		trigger = {
			house = {
				OR = {
					has_house_aspiration_parameter = aspect_of_creation_05
					has_house_aspiration_parameter = aspect_of_serenity_05
					has_house_aspiration_parameter = aspect_of_destruction_05
					has_house_aspiration_parameter = aspect_of_trickery_05
				}
			}
		}
		reference = legend_glow	
	}
	immediate = {
		if = {
			limit = {
				house = { has_house_aspiration_parameter = aspect_of_destruction }
			}
			random_courtier = {
				limit = { 
					is_available_adult = yes
					can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = root }
					this != root
				}
				save_scope_as = onlooker
			}
			if = {
				limit = {
					NOT = { exists = scope:onlooker }
				}
				random_pool_character = {
					province = root.capital_province
					limit = {
						is_available_adult = yes
						can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = root }
					}
					save_scope_as = onlooker
				}
			}
			scope:onlooker = { add_character_flag = need_military_outfit }
			random_courtier = {
				limit = { 
					is_available_adult = yes
					can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = root }
					this != root
					this != scope:onlooker
				}
				save_scope_as = onlooker_2
			}
			if = {
				limit = {
					NOT = { exists = scope:onlooker_2 }
				}
				random_pool_character = {
					province = root.capital_province
					limit = {
						is_available_adult = yes
						can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = root }
					}
					save_scope_as = onlooker_2
				}
			}
			scope:onlooker_2 = { add_character_flag = need_military_outfit }
		}
		else = {
			mandala_events_1000_random_onlooker_effect = yes
		}
		#Spice up the tooltip. What just happened?
		show_as_tooltip = {
			house = {
				#Creation
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_creation_02 }
					set_house_aspiration = {
						type = aspect_of_creation
						level = 2
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_creation_03 }
					set_house_aspiration = {
						type = aspect_of_creation
						level = 3
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_creation_04 }
					set_house_aspiration = {
						type = aspect_of_creation
						level = 4
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_creation_05 }
					set_house_aspiration = {
						type = aspect_of_creation
						level = 5
					}
				}
				#Serenity
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_serenity_02 }
					set_house_aspiration = {
						type = aspect_of_serenity
						level = 2
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_serenity_03 }
					set_house_aspiration = {
						type = aspect_of_serenity
						level = 3
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_serenity_04 }
					set_house_aspiration = {
						type = aspect_of_serenity
						level = 4
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_serenity_05 }
					set_house_aspiration = {
						type = aspect_of_serenity
						level = 5
					}
				}
				#Destruction
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_destruction_02 }
					set_house_aspiration = {
						type = aspect_of_destruction
						level = 2
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_destruction_03 }
					set_house_aspiration = {
						type = aspect_of_destruction
						level = 3
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_destruction_04 }
					set_house_aspiration = {
						type = aspect_of_destruction
						level = 4
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_destruction_05 }
					set_house_aspiration = {
						type = aspect_of_destruction
						level = 5
					}
				}
				#Trickery
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_trickery_02 }
					set_house_aspiration = {
						type = aspect_of_trickery
						level = 2
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_trickery_03 }
					set_house_aspiration = {
						type = aspect_of_trickery
						level = 3
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_trickery_04 }
					set_house_aspiration = {
						type = aspect_of_trickery
						level = 4
					}
				}
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_trickery_05 }
					set_house_aspiration = {
						type = aspect_of_trickery
						level = 5
					}
				}
			}		
		}
	}
	#Cool!
	option = {
		name = tgp_east_asia_mandala_events.1000.a
		#Some boons for your hard work
		if = {
			limit = {
				house = {
					OR = {
						has_house_aspiration_parameter = aspect_of_creation_05
						has_house_aspiration_parameter = aspect_of_serenity_05
						has_house_aspiration_parameter = aspect_of_destruction_05
						has_house_aspiration_parameter = aspect_of_trickery_05
					}
				}
			}
			add_piety_experience = major_piety_gain
			dynasty = { 
				add_dynasty_prestige = major_piety_gain
			}
		}
		else_if = {
			limit = {
				house = {
					OR = {
						has_house_aspiration_parameter = aspect_of_creation_04
						has_house_aspiration_parameter = aspect_of_serenity_04
						has_house_aspiration_parameter = aspect_of_destruction_04
						has_house_aspiration_parameter = aspect_of_trickery_04
					}
				}
			}
			add_piety_experience = { 
				value = {
					add = major_piety_gain
					multiply = 0.6
				}
			}
			dynasty = { 
				add_dynasty_prestige = {
					value = {
						add = major_piety_gain
						multiply = 0.6
					}
				}
			}
		}
		else_if = {
			limit = {
				house = {
					OR = {
						has_house_aspiration_parameter = aspect_of_creation_03
						has_house_aspiration_parameter = aspect_of_serenity_03
						has_house_aspiration_parameter = aspect_of_destruction_03
						has_house_aspiration_parameter = aspect_of_trickery_03
					}
				}
			}
			add_piety_experience = { 
				value = {
					add = minor_piety_gain
					multiply = 2
				}
			}
			dynasty = { 
				add_dynasty_prestige = {
					value = {
						add = minor_dynasty_prestige_value
						multiply = 2
					}
				}
			}
		}
		else = {
			add_piety_experience = { 
				value = {
					add = miniscule_piety_gain
					multiply = 2
				}
			}
			dynasty = { 
				add_dynasty_prestige = {
					value = {
						add = miniscule_dynasty_prestige_value
						multiply = 2
					}
				}
			}
		}
		#Creation boon
		if = {
			limit = {
				house = { has_house_aspiration_parameter = aspect_of_creation }
			}
			every_child = {
				custom = mandala_descendant_from_aspect_of_creation.tt
				limit = {
					NOT = { has_character_modifier = mandala_descendant_from_aspect_of_creation_modifier }
				}
				add_character_modifier = { modifier = mandala_descendant_from_aspect_of_creation_modifier }
			}
		}
		#Serenity boon
		if = {
			limit = {
				house = { has_house_aspiration_parameter = aspect_of_serenity }
			}
			custom_tooltip = {
				text = mandala_aspect_of_serenity_devotion.tt
				add_piety_experience = num_years_of_peace_devotion_payout
			}
		}
		#Destruction boon
		if = {
			limit = {
				house = { has_house_aspiration_parameter = aspect_of_destruction }
			}
			every_knight = {
				custom = mandala_forged_from_aspect_of_destruction.tt
				limit = {
					NOT = { has_character_modifier = mandala_forged_from_aspect_of_destruction_modifier }
				}
				add_character_modifier = { modifier = mandala_forged_from_aspect_of_destruction_modifier }
			}
		}
		#Trickery boon
		if = {
			limit = {
				house = { has_house_aspiration_parameter = aspect_of_trickery }
			}
			if = {
				limit = {
					NOT = { has_character_modifier = mandala_ascended_in_aspect_of_trickery_modifier }
				}
				add_character_modifier = { modifier = mandala_ascended_in_aspect_of_trickery_modifier }
			}
			else = {
				custom_tooltip = {
					text = mandala_aspect_of_trickery_devotion.tt
					add_piety_experience = num_successful_schemes_devotion_payout
				}
			}
		}
		
		ai_chance = {
			base = 100
		}
	}
	after = {
		scope:onlooker = { remove_character_flag = need_military_outfit }
		scope:onlooker_2 = { remove_character_flag = need_military_outfit }
	}
}


##SKILL CHALLENGES
#Maintenance
tgp_east_asia_mandala_events.8495 = {
	hidden = yes

	immediate = {
		if = {
			limit = { var:mandala_skill_challenges = 3 }
			trigger_event = {
				id = tgp_east_asia_mandala_events.8550
				delayed = yes
			}
		}
		else = {
			random_list = {
				3 = {
					trigger = {
						NOT = { has_variable = had_creation_skill_challenge }
					}
					trigger_event = {
						id = tgp_east_asia_mandala_events.8500
						delayed = yes
					}
				}
				3 = {
					trigger = {
						NOT = { has_variable = had_serenity_skill_challenge }
					}
					trigger_event = {
						id = tgp_east_asia_mandala_events.8510
						delayed = yes
					}
				}
				3 = {
					trigger = {
						NOT = { has_variable = had_destruction_skill_challenge }
					}
					trigger_event = {
						id = tgp_east_asia_mandala_events.8520
						delayed = yes
					}
				}
				3 = {
					trigger = {
						NOT = { has_variable = had_trickery_skill_challenge }
					}
					trigger_event = {
						id = tgp_east_asia_mandala_events.8530
						delayed = yes
					}
				}
			}
		}
	}
}

#Finished travel, do you have a succession queued?
tgp_east_asia_mandala_events.8496 = {
	hidden = yes
	trigger = {
		location = capital_province
		has_variable = mandala_succession_waiting_on_travel_finish
		is_landed = yes
		government_has_flag = government_is_mandala
	}
	on_trigger_fail = {
		trigger_event = {
			id = tgp_east_asia_mandala_events.8497
			delayed = yes
		}
	}
	immediate = {
		if = {
			limit = { has_variable = mandala_succession_waiting_on_travel_finish }
			var:mandala_succession_waiting_on_travel_finish = { save_scope_as = deceased_mandala_ruler }
		}
		trigger_event = {
			id = tgp_east_asia_mandala_events.8499
			delayed = yes
		}
	}
}

#INVALID: Something's gone awry. Lose some Devotion.
tgp_east_asia_mandala_events.8497 = {
	hidden = yes
	trigger = {
		has_variable = mandala_succession_waiting_on_travel_finish
	}
	immediate = {
		send_interface_message = {
			type = msg_mandala_bad_with_text
			title = tgp_east_asia_mandala_events.8497.t.lose_by_default
			desc = tgp_east_asia_mandala_events.8497.tt.lose_by_default
			left_icon = root
			right_icon = scope:deceased_mandala_ruler
			add_piety_level = -3
		}
		#And _remember_ it
		create_character_memory = {
			type = invalid_mandala_trials_memory
			participants = { previous_mandala_ruler = scope:deceased_mandala_ruler }
		}
		remove_variable = mandala_succession_waiting_on_travel_finish
		remove_variable = mandala_succession_waiting_on_adulthood
	}
}

#Initiating event
tgp_east_asia_mandala_events.8499 = {
	type = character_event
	window = big_event_window
	title = tgp_east_asia_mandala_events.8499.t
	desc = {
		first_valid = {
			#We inherited while being a child, but were imprisoned before our 16th birthday
			triggered_desc = {
				trigger = { 
					is_imprisoned = yes
					has_variable = mandala_succession_waiting_on_adulthood
				}
				desc = tgp_east_asia_mandala_events.8499.intro.recent_adult.imprisoned
			}
			#We inherited while being imprisoned
			triggered_desc = {
				trigger = { is_imprisoned = yes }
				desc = tgp_east_asia_mandala_events.8499.intro.imprisoned
			}
		}
		first_valid = {
			#Both imprisonment intros work with this text body
			triggered_desc = {
				trigger = { is_imprisoned = yes }
				desc = tgp_east_asia_mandala_events.8499.desc.imprisoned
			}
			#We inherited while being a child and waited for our 16th birthday
			triggered_desc = {
				trigger = { 
					is_imprisoned = no
					has_variable = mandala_succession_waiting_on_adulthood
				}
				desc = tgp_east_asia_mandala_events.8499.desc.recent_adult
			}
			#We inherited, or came of age, while out traveling
			triggered_desc = {
				trigger = { has_variable = mandala_succession_waiting_on_travel_finish }
				desc = tgp_east_asia_mandala_events.8499.desc.was_traveling
			}
			#Default
			desc = tgp_east_asia_mandala_events.8499.desc
		}
	}
	theme = mandala
	override_background = {
		trigger = { is_imprisoned = yes }
		reference = dungeon
	}
	override_background = { reference = study }
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { is_imprisoned = yes }
			animation = prisondungeon
		}
		override_imprisonment_visuals = yes
		animation = personality_bold
	}
	immediate = {
	}
	#Let's go
	option = { 
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { is_imprisoned = yes }
						desc = tgp_east_asia_mandala_events.8499.a.imprisoned
					}
					desc = tgp_east_asia_mandala_events.8499.a
				}
			}
		}
		flavor = {
			first_valid = {
				triggered_desc = {
					trigger = { is_imprisoned = yes }
					desc = tgp_east_asia_mandala_events.8499.a.flavor.imprisoned
				}
			}
		}
		#If we're imprisoned, insta-lose
		if = {
			limit = { is_imprisoned = yes }
			custom_tooltip = tgp_east_asia_mandala_events.8499.a.tt.imprisoned
			add_piety_level = -3
			if = {
				limit = {
					NOT = { has_character_modifier = mandala_divine_retribution_modifier }
				}
				add_character_modifier = mandala_divine_retribution_modifier
			}
			#And _remember_ it
			create_character_memory = {
				type = imprisoned_mandala_trials_memory
				participants = { previous_mandala_ruler = scope:deceased_mandala_ruler }
			}
		}
		else = {
			custom_tooltip = tgp_east_asia_mandala_events.8499.a.tt.trials
			set_variable = busy_in_mandala_succession
			if = {
				limit = {
					house = { has_house_aspiration_parameter = aspect_of_creation }
				}
				trigger_event = {
					id = tgp_east_asia_mandala_events.8500
					delayed = yes
				}
			}
			if = {
				limit = {
					house = { has_house_aspiration_parameter = aspect_of_serenity }
				}
				trigger_event = {
					id = tgp_east_asia_mandala_events.8510
					delayed = yes
				}
			}
			if = {
				limit = {
					house = { has_house_aspiration_parameter = aspect_of_destruction }
				}
				trigger_event = {
					id = tgp_east_asia_mandala_events.8520
					delayed = yes
				}
			}
			if = {
				limit = {
					house = { has_house_aspiration_parameter = aspect_of_trickery }
				}
				trigger_event = {
					id = tgp_east_asia_mandala_events.8530
					delayed = yes
				}
			}
		}
	}
	after = {
		#Clean up peripheral variables
		remove_variable = mandala_succession_waiting_on_travel_finish
		remove_variable = mandala_succession_waiting_on_adulthood
	}
}


scripted_effect mandala_events_8500_instawin_trial_effect = {
	if = {
		limit = { $SKILL$ = 01 }
		custom_tooltip = tgp_east_asia_mandala_events.8500.success.tt
	}
	if = {
		limit = { $SKILL$ = 02 }
		custom_tooltip = tgp_east_asia_mandala_events.8510.success.tt
	}
	if = {
		limit = { $SKILL$ = 03 }
		custom_tooltip = tgp_east_asia_mandala_events.8520.success.tt
	}
	if = {
		limit = { $SKILL$ = 04 }
		custom_tooltip = tgp_east_asia_mandala_events.8530.success.tt
	}
	increment_variable_effect = {
		VAR = mandala_skill_challenge_success
		VAL = 1
	}
}

scripted_effect mandala_events_8500_succeeded_trial_effect = {
	send_interface_toast = {
		type = event_toast_effect_good
		title = tgp_east_asia_mandala_events.8500.b.t.success
		left_icon = root
		right_icon = scope:deceased_mandala_ruler
		increment_variable_effect = {
			VAR = mandala_skill_challenge_success
			VAL = 1
		}
		if = {
			limit = { $SKILL$ = 01 }
			custom_tooltip = tgp_east_asia_mandala_events.8500.success.tt
			add_stewardship_skill = 1
		}
		else_if = {
			limit = { $SKILL$ = 02 }
			custom_tooltip = tgp_east_asia_mandala_events.8510.success.tt
			add_diplomacy_skill = 1
		}
		else_if = {
			limit = { $SKILL$ = 03 }
			custom_tooltip = tgp_east_asia_mandala_events.8520.success.tt
			add_martial_skill = 1
		}
		else_if = {
			limit = { $SKILL$ = 04 }
			custom_tooltip = tgp_east_asia_mandala_events.8530.success.tt
			add_intrigue_skill = 1
		}
	}
}

scripted_effect mandala_events_8500_failed_trial_effect = {
	send_interface_toast = {
		type = event_toast_effect_bad
		title = tgp_east_asia_mandala_events.8500.b.t.failure
		left_icon = root
		right_icon = scope:deceased_mandala_ruler
		add_piety_level = -1
	}
}

scripted_effect mandala_events_8500_next_trial_effect = {
	increment_variable_effect = {
		VAR = mandala_skill_challenges
		VAL = 1
	}
	trigger_event = tgp_east_asia_mandala_events.8495
}

#Creation / Stewardship
tgp_east_asia_mandala_events.8500 = {
	type = character_event
	window = big_event_window
	title = tgp_east_asia_mandala_events.8500.t
	desc = tgp_east_asia_mandala_events.8500.desc
	theme = mandala
	override_background = { reference = throne_room }
	left_portrait = {
		trigger = { exists = scope:onlooker }
		character = scope:onlooker
		animation = fear
	}
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { 
				house = { has_house_head_parameter = may_override_stewardship_succession_trial }
			}
			animation = personality_zealous
		}
		animation = storyteller
	}
	right_portrait = {
		trigger = { exists = scope:onlooker_2 }
		character = scope:onlooker_2
		triggered_animation = {
			trigger = {
				root.house = { has_house_head_parameter = may_override_stewardship_succession_trial }
			}
			animation = admiration
		}
		animation = interested_left
	}
	override_effect_2d = {
		trigger = {
			house = { has_house_head_parameter = may_override_stewardship_succession_trial }
		}
		reference = legend_glow
	}
	immediate = {
		mandala_events_1000_random_onlooker_effect = yes
		random_dummy_gender_effect = yes
	}
	#Dynasty Override?
	option = {
		name = tgp_east_asia_mandala_events.8500.dynasty.override
		trigger = {
			dynasty ?= { has_dynasty_perk = tgp_sea_legacy_5 }
			NOT = { has_character_flag = spent_sea_legacy_succession_bypass }
		}
		reason = mandala_legacy_unlock
		mandala_events_8500_instawin_trial_effect = { SKILL = 01 }
		custom_description_no_bullet = { text = sea_succession_legacy_spent }
		add_character_flag = spent_sea_legacy_succession_bypass
		ai_chance = {
			base = 100
		}
	}
	#Override?
	option = {
		name = tgp_east_asia_mandala_events.8500.a
		trigger = {
			custom_tooltip = {
				text = tgp_east_asia_mandala_events.8500.a.final_aspect
				house = { has_house_head_parameter = may_override_stewardship_succession_trial }
			}
		}
		show_as_unavailable = { 
			house = { 
				has_house_aspiration_parameter = aspect_of_creation
				NOT = { has_house_head_parameter = may_override_stewardship_succession_trial }
			}
		}
		reason = creation_aspect
		mandala_events_8500_instawin_trial_effect = { SKILL = 01 }
		ai_chance = {
			base = 100
		}
	}
	#DUEL!
	option = { 
		name = tgp_east_asia_mandala_events.8500.b
		flavor = tgp_east_asia_mandala_events.8500.b.flavor
		duel = {
			skill = stewardship
			target = scope:deceased_mandala_ruler
			#Success!
			90 = {
				trigger = { highest_held_title_tier < tier_empire }
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 01 }
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 01 }
			}
			#Failure
			5 = {
				trigger = { highest_held_title_tier = tier_county }
				max = 5
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			10 = {
				trigger = { highest_held_title_tier = tier_duchy }
				max = 10
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			40 = {
				trigger = { highest_held_title_tier = tier_kingdom }
				max = 40
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = {
					is_ai = yes
					add = -45
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
		}
		ai_chance = {
			base = 100
			modifier = {
				house = { has_house_head_parameter = may_override_stewardship_succession_trial }
				factor = 0
			}
		}
	}
	after = {
		set_variable = had_creation_skill_challenge
		mandala_events_8500_next_trial_effect = yes
	}
}

#Serenity / Diplomacy
tgp_east_asia_mandala_events.8510 = {
	type = character_event
	window = big_event_window
	title = tgp_east_asia_mandala_events.8510.t
	desc = tgp_east_asia_mandala_events.8510.desc
	theme = mandala
	override_background = { reference = corridor_day }
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { 
				house = { has_house_head_parameter = may_override_diplomacy_succession_trial }
			}
			animation = personality_zealous
		}
		camera = camera_event_scheme_center_look_semi_right
		animation = stress	
	}	
	center_portrait = {
		character = scope:arguer
		camera = camera_event_scheme_vs_extra_right_look_right
		animation = dismissal
		hide_info = yes
	}
	right_portrait = {
		character = scope:arguer_2
		camera = camera_event_scheme_vs_extra_right_look_left
		animation = disapproval
		hide_info = yes
	}
	override_effect_2d = {
		trigger = {
			house = { has_house_head_parameter = may_override_diplomacy_succession_trial }
		}
		reference = legend_glow
	}
	immediate = {
		hidden_effect = {
			random_list = {
				1 = { 
					dummy_female = { save_scope_as = dummy_gender }
					save_scope_value_as = {
						name = female_gender
						value = flag:yes
					}
				}
				1 = { 
					dummy_male = { save_scope_as = dummy_gender }
					save_scope_value_as = {
						name = male_gender
						value = flag:yes
					}
				}
			}
		}
		random_pool_character = {
			province = root.capital_province
			limit = {
				is_available_healthy_adult = yes
				trigger_if = {
					limit = { exists = scope:female_gender }
					is_female = yes
				}
			}
			save_scope_as = arguer
		}
		random_pool_character = {
			province = root.capital_province
			limit = {
				is_available_healthy_adult = yes
				trigger_if = {
					limit = { exists = scope:female_gender }
					is_female = yes
				}
				NOT = { this = scope:arguer }
			}
			save_scope_as = arguer_2
		}
		if = {
			limit = {
				NOT = { exists = scope:arguer }
			}
			create_character = {
				location = root.capital_province
				template = pool_repopulate_local_flavor
				save_scope_as = arguer
				after_creation = { save_scope_as = created_character }
			}
		}
		if = {
			limit = {
				NOT = { exists = scope:arguer_2 }
			}
			create_character = {
				location = root.capital_province
				template = pool_repopulate_local_flavor
				save_scope_as = arguer_2
				after_creation = { save_scope_as = created_character }
			}
		}
	}
	#Dynasty Override?
	option = {
		name = tgp_east_asia_mandala_events.8510.dynasty.override
		trigger = {
			dynasty ?= { has_dynasty_perk = tgp_sea_legacy_5 }
			NOT = { has_character_flag = spent_sea_legacy_succession_bypass }
		}
		reason = mandala_legacy_unlock
		mandala_events_8500_instawin_trial_effect = { SKILL = 02 }
		custom_description_no_bullet = { text = sea_succession_legacy_spent }
		add_character_flag = spent_sea_legacy_succession_bypass
		ai_chance = {
			base = 100
		}
	}
	#Override?
	option = {
		name = tgp_east_asia_mandala_events.8510.a
		trigger = {
			custom_tooltip = {
				text = tgp_east_asia_mandala_events.8510.a.final_aspect
				house = { has_house_head_parameter = may_override_diplomacy_succession_trial }
			}
		}
		show_as_unavailable = { 
			house = { 
				has_house_aspiration_parameter = aspect_of_serenity
				NOT = { has_house_head_parameter = may_override_diplomacy_succession_trial }
			}
		}
		reason = serenity_aspect
		mandala_events_8500_instawin_trial_effect = { SKILL = 02 }
		ai_chance = {
			base = 100
		}
	}
	#DUEL!
	option = { 
		name = tgp_east_asia_mandala_events.8510.b
		flavor = tgp_east_asia_mandala_events.8510.b.flavor
		duel = {
			skill = diplomacy
			target = scope:deceased_mandala_ruler
			#Success!
			90 = {
				trigger = { highest_held_title_tier < tier_empire }
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 02 }
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 02 }
			}
			#Failure
			5 = {
				trigger = { highest_held_title_tier = tier_county }
				max = 5
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			10 = {
				trigger = { highest_held_title_tier = tier_duchy }
				max = 10
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			40 = {
				trigger = { highest_held_title_tier = tier_kingdom }
				max = 40
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = {
					is_ai = yes
					add = -45
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
		}
		ai_chance = {
			base = 100
			modifier = {
				house = { has_house_head_parameter = may_override_diplomacy_succession_trial }
				factor = 0
			}
		}
	}
	after = {
		set_variable = had_serenity_skill_challenge
		mandala_events_8500_next_trial_effect = yes
		scope:arguer = {
			if = {
				limit = { has_variable = created_character }
				silent_disappearance_effect = yes
			}
		}
		scope:arguer_2 = {
			if = {
				limit = { has_variable = created_character }
				silent_disappearance_effect = yes
			}
		}
	}
}

#Destruction / Martial
tgp_east_asia_mandala_events.8520 = {
	type = character_event
	window = big_event_window
	title = tgp_east_asia_mandala_events.8520.t
	desc = tgp_east_asia_mandala_events.8520.desc
	theme = mandala
	left_portrait = {
		trigger = { exists = scope:onlooker }
		character = scope:onlooker
		triggered_animation = {
			trigger = {
				root.house = { has_house_head_parameter = may_override_martial_succession_trial }
			}
			animation = wrestling_victory
		}
		animation = interested
	}
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { 
				house = { has_house_head_parameter = may_override_martial_succession_trial }
			}
			camera = camera_body_right
			animation = relaxed_spear
		}
		camera = camera_event_scheme_center_forward
		animation = aggressive_spear
	}
	right_portrait = {
		trigger = { exists = scope:onlooker_2 }
		character = scope:onlooker_2
		triggered_animation = {
			trigger = {
				root.house = { has_house_head_parameter = may_override_martial_succession_trial }
			}
			animation = admiration
		}
		camera = camera_event_scheme_far_right
		animation = interested_left
	}
	override_effect_2d = {
		trigger = {
			house = { has_house_head_parameter = may_override_martial_succession_trial }
		}
		reference = legend_glow
	}
	immediate = {
		mandala_events_1000_random_onlooker_effect = yes
	}
	#Dynasty Override?
	option = {
		name = tgp_east_asia_mandala_events.8520.dynasty.override
		trigger = {
			dynasty ?= { has_dynasty_perk = tgp_sea_legacy_5 }
			NOT = { has_character_flag = spent_sea_legacy_succession_bypass }
		}
		reason = mandala_legacy_unlock
		mandala_events_8500_instawin_trial_effect = { SKILL = 03 }
		custom_description_no_bullet = { text = sea_succession_legacy_spent }
		add_character_flag = spent_sea_legacy_succession_bypass
		ai_chance = {
			base = 100
		}
	}
	#Override?
	option = {
		name = tgp_east_asia_mandala_events.8520.a
		trigger = {
			custom_tooltip = {
				text = tgp_east_asia_mandala_events.8520.a.final_aspect
				house = { has_house_head_parameter = may_override_martial_succession_trial }
			}
		}
		show_as_unavailable = { 
			house = { 
				has_house_aspiration_parameter = aspect_of_destruction
				NOT = { has_house_head_parameter = may_override_martial_succession_trial }
			}
		}
		reason = destruction_aspect
		mandala_events_8500_instawin_trial_effect = { SKILL = 03 }
		ai_chance = {
			base = 100
		}
	}
	#DUEL!
	option = { 
		name = tgp_east_asia_mandala_events.8520.b
		flavor = tgp_east_asia_mandala_events.8520.b.flavor
		duel = {
			skill = martial
			target = scope:deceased_mandala_ruler
			#Success!
			90 = {
				trigger = { highest_held_title_tier < tier_empire }
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 03 }
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 03 }
			}
			#Failure
			5 = {
				trigger = { highest_held_title_tier = tier_county }
				max = 5
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			10 = {
				trigger = { highest_held_title_tier = tier_duchy }
				max = 10
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			40 = {
				trigger = { highest_held_title_tier = tier_kingdom }
				max = 40
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = {
					is_ai = yes
					add = -45
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
		}
		ai_chance = {
			base = 100
			modifier = {
				house = { has_house_head_parameter = may_override_martial_succession_trial }
				factor = 0
			}
		}
	}
	after = {
		set_variable = had_destruction_skill_challenge
		mandala_events_8500_next_trial_effect = yes
	}
}

#Trickery / Intrigue
tgp_east_asia_mandala_events.8530 = {
	type = character_event
	window = big_event_window
	title = tgp_east_asia_mandala_events.8530.t
	desc = tgp_east_asia_mandala_events.8530.desc
	theme = mandala
	left_portrait = {
		trigger = { exists = scope:onlooker }
		character = scope:onlooker
		triggered_animation = {
			trigger = {
				root.house = { has_house_head_parameter = may_override_intrigue_succession_trial }
			}
			animation = admiration
		}
		animation = interested
	}
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { 
				house = { has_house_head_parameter = may_override_intrigue_succession_trial }
			}
			animation = scheme
		}
		animation = storyteller
	}
	right_portrait = {
		trigger = { exists = scope:onlooker_2 }
		character = scope:onlooker_2
		triggered_animation = {
			trigger = {
				root.house = { has_house_head_parameter = may_override_intrigue_succession_trial }
			}
			animation = happiness
		}
		camera = camera_event_scheme_far_right
		animation = interested_left
	}
	override_effect_2d = {
		trigger = {
			house = { has_house_head_parameter = may_override_intrigue_succession_trial }
		}
		reference = legend_glow
	}
	immediate = {
		mandala_events_1000_random_onlooker_effect = yes
	}
	#Dynasty Override?
	option = {
		name = tgp_east_asia_mandala_events.8530.dynasty.override
		trigger = {
			dynasty ?= { has_dynasty_perk = tgp_sea_legacy_5 }
			NOT = { has_character_flag = spent_sea_legacy_succession_bypass }
		}
		reason = mandala_legacy_unlock
		mandala_events_8500_instawin_trial_effect = { SKILL = 04 }
		custom_description_no_bullet = { text = sea_succession_legacy_spent }
		add_character_flag = spent_sea_legacy_succession_bypass
		ai_chance = {
			base = 100
		}
	}
	#Override?
	option = {
		name = tgp_east_asia_mandala_events.8530.a
		trigger = {
			custom_tooltip = {
				text = tgp_east_asia_mandala_events.8530.a.final_aspect
				house = { has_house_head_parameter = may_override_intrigue_succession_trial }
			}
		}
		show_as_unavailable = { 
			house = { 
				has_house_aspiration_parameter = aspect_of_trickery
				NOT = { has_house_head_parameter = may_override_intrigue_succession_trial }
			}
		}
		reason = trickery_aspect
		mandala_events_8500_instawin_trial_effect = { SKILL = 04 }
		ai_chance = {
			base = 100
		}
	}
	#DUEL!
	option = { 
		name = tgp_east_asia_mandala_events.8530.b
		flavor = tgp_east_asia_mandala_events.8530.b.flavor
		duel = {
			skill = intrigue
			target = scope:deceased_mandala_ruler
			#Success!
			90 = {
				trigger = { highest_held_title_tier < tier_empire }
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 04 }
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.success
				mandala_events_8500_succeeded_trial_effect = { SKILL = 04 }
			}
			#Failure
			5 = {
				trigger = { highest_held_title_tier = tier_county }
				max = 5
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			10 = {
				trigger = { highest_held_title_tier = tier_duchy }
				max = 10
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			40 = {
				trigger = { highest_held_title_tier = tier_kingdom }
				max = 40
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
			50 = {
				trigger = { highest_held_title_tier >= tier_empire }
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				modifier = {
					is_ai = yes
					add = -45
				}
				desc = tgp_east_asia_mandala_events.8500.b.tt.failure
				mandala_events_8500_failed_trial_effect = yes
			}
		}
		ai_chance = {
			base = 100
			modifier = {
				house = { has_house_head_parameter = may_override_intrigue_succession_trial }
				factor = 0
			}
		}
	}
	after = {
		set_variable = had_trickery_skill_challenge
		mandala_events_8500_next_trial_effect = yes
	}
}


#Conclusion
tgp_east_asia_mandala_events.8550 = {
	type = character_event
	window = big_event_window
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = var:mandala_skill_challenge_success }
				desc = tgp_east_asia_mandala_events.8550.t
			}
			desc = tgp_east_asia_mandala_events.8550.t.failed
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { var:mandala_skill_challenge_success ?= 3 }
				desc = tgp_east_asia_mandala_events.8550.desc.critical_success
			}
			triggered_desc = {
				trigger = { var:mandala_skill_challenge_success ?= 2 }
				desc = tgp_east_asia_mandala_events.8550.desc.success
			}
			triggered_desc = {
				trigger = { var:mandala_skill_challenge_success ?= 1 }
				desc = tgp_east_asia_mandala_events.8550.desc
			}
			desc = tgp_east_asia_mandala_events.8550.desc.failed
		}
	}
	theme = mandala
	left_portrait = {
		trigger = { 
			exists = scope:onlooker
			root = { has_variable = mandala_skill_challenge_success }
		}
		character = scope:onlooker
		triggered_animation = {
			trigger = { root.var:mandala_skill_challenge_success ?= 3 }
			animation = happiness
		}
		triggered_animation = {
			trigger = { root.var:mandala_skill_challenge_success ?= 2 }
			animation = wrestling_victory
		}
		triggered_animation = {
			trigger = { root.var:mandala_skill_challenge_success ?= 1 }
			animation = dismissal
		}
		animation = dismissal
	}
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { 
				NOT = { has_variable = mandala_skill_challenge_success }
			}
			camera = shop_camera_baby_closeup_angle_45
			animation = prostration
		}
		triggered_animation = {
			trigger = { var:mandala_skill_challenge_success ?= 3 }
			animation = wrestling_victory
		}
		triggered_animation = {
			trigger = { var:mandala_skill_challenge_success ?= 2 }
			animation = happiness
		}
		animation = stress
	}
	right_portrait = {
		trigger = { 
			exists = scope:onlooker_2
			root = { has_variable = mandala_skill_challenge_success }
		}
		character = scope:onlooker_2
		triggered_animation = {
			trigger = { root.var:mandala_skill_challenge_success ?= 3 }
			animation = ecstasy
		}
		triggered_animation = {
			trigger = { root.var:mandala_skill_challenge_success ?= 2 }
			animation = happiness
		}
		triggered_animation = {
			trigger = { root.var:mandala_skill_challenge_success ?= 1 }
			animation = disgust
		}
		camera = camera_event_scheme_vs_center_positioned_left
		animation = disgust
	}
	override_effect_2d = {
		trigger = { var:mandala_skill_challenge_success ?= 3 }
		reference = legend_glow
	}
	override_effect_2d = {
		trigger = {
			NOT = { has_variable = mandala_skill_challenge_success }
		}
		reference = rain
	}
	immediate = {
		mandala_events_1000_random_onlooker_effect = yes
		show_as_tooltip = {
			if = {
				limit = { 
					NOT = { has_variable = mandala_skill_challenge_success }
				}
				add_piety_level = -3		
			}
			else_if = {
				limit = { var:mandala_skill_challenge_success ?= 1 }
				add_piety_level = -2
				if = {
					limit = {
						any_tributary = {
							is_physically_able_ai_adult = yes
							NOR = {
								has_relation_friend = root
								has_relation_lover = root
							}
						}
					}
					#Max 3
					every_tributary = {
						limit = {
							is_physically_able_ai_adult = yes
							NOR = {
								has_relation_friend = root
								has_relation_lover = root
							}
						}
						add_to_list = potential_departing_tributaries
					}
					ordered_in_list = {
						list = potential_departing_tributaries
						order_by = {
							value = "opinion(root)"
							multiply = -1
						}
						check_range_bounds = no
						save_scope_as = first_leaving_tributary
					}
					if = {
						limit = {
							list_size = {
								name = potential_departing_tributaries
								value > 1
							}
						}
						ordered_in_list = {
							list = potential_departing_tributaries
							order_by = {
								value = "opinion(root)"
								multiply = -1
							}
							limit = { 
								this != scope:first_leaving_tributary
							}
							save_scope_as = second_leaving_tributary
						}
					}
					if = {
						limit = {
							list_size = {
								name = potential_departing_tributaries
								value > 2
							}
						}
						ordered_in_list = {
							list = potential_departing_tributaries
							order_by = {
								value = "opinion(root)"
								multiply = -1
							}
							limit = { 
								NOR = { 
									this = scope:first_leaving_tributary
									this = scope:second_leaving_tributary 
								}
							}
							save_scope_as = third_leaving_tributary
						}
					}
				}
			}
			else_if = {
				limit = { var:mandala_skill_challenge_success ?= 2 }
				add_piety_level = -1
			}
		}
		set_variable = {
			name = not_subject_to_succession_trials
			value = flag:completed_trials
		}
		if = {
			limit = { var:not_subject_to_succession_trials ?= flag:completed_trials }
			#To prevent 'unused except in loc' errors :catto:
		}
	}
	#Roger, roger
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { 
							NOT = { has_variable = mandala_skill_challenge_success }
						}
						desc = tgp_east_asia_mandala_events.8550.a.failed
					}
					desc = tgp_east_asia_mandala_events.8550.a
				}
			}
		}	
		if = {
			limit = { var:mandala_skill_challenge_success ?= 3 }
			dynasty ?= { add_dynasty_prestige = minor_dynasty_prestige_value }
		}
		if = {
			limit = { var:mandala_skill_challenge_success ?= 2 }
			dynasty = { 
				add_dynasty_prestige = {
					value = minor_dynasty_prestige_value
					divide = 2
				}
			}
		}
		ai_chance = {
			base = 100
		}
	}
	after = {
		#Levels of Success
		if = {
			limit = { var:mandala_skill_challenge_success ?= 3 }
			create_character_memory = {
				type = flawless_mandala_trials_memory
				participants = { previous_mandala_ruler = scope:deceased_mandala_ruler }
			}
			if = {
				limit = {
					NOT = { has_character_flag = devaraja_flag }
				}
				add_character_flag = devaraja_flag
			}
		}
		else_if = {
			limit = { has_variable = mandala_skill_challenge_success }
			if = {
				limit = { var:mandala_skill_challenge_success ?= 2 }
				if = {
					limit = { exists = scope:first_leaving_tributary }
					custom_tooltip = {
						text = tgp_east_asia_mandala_events.lose_3_tributaries.tt
						scope:first_leaving_tributary = { end_tributary = yes }
						scope:second_leaving_tributary ?= { end_tributary = yes }
						scope:third_leaving_tributary ?= { end_tributary = yes }
					}
				}
				else = {
					if = {
						limit = {
							has_legitimacy = yes
							legitimacy > 0
						}
						add_legitimacy = -100
					}
				}
				#And remember it
				create_character_memory = {
					type = successful_mandala_trials_memory
					participants = { previous_mandala_ruler = scope:deceased_mandala_ruler }
				}
				if = {
					limit = {
						NOT = { has_character_flag = devaraja_flag }
					}
					add_character_flag = devaraja_flag
				}
			}
			else_if = {
				limit = { var:mandala_skill_challenge_success ?= 1 }
				if = {
					limit = { number_of_tributaries > 0 }
					custom_tooltip = {
						text = tgp_east_asia_mandala_events.lose_all_tributaries.tt
						every_tributary = { end_tributary = yes }
					}
				}
				else = {
					if = {
						limit = {
							has_legitimacy = yes
							legitimacy > 0
						}
						add_legitimacy = -200
					}
				}
				#And remember it
				create_character_memory = {
					type = adequate_mandala_trials_memory
					participants = { previous_mandala_ruler = scope:deceased_mandala_ruler }
				}
				if = {
					limit = {
						NOT = { has_character_flag = devaraja_flag }
					}
					add_character_flag = devaraja_flag
				}
			}
		}
		#Failure
		else = {
			stress_impact = { base = medium_stress_impact_gain }
			if = {
				limit = {
					has_legitimacy = yes
					legitimacy > 0
				}
				add_legitimacy = -500
			}
			custom_tooltip = tgp_east_asia_mandala_events.8550.a.failed.tt
			hidden_effect = {
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
					add_claim_on_loss = no
				}
				every_vassal = {
					change_liege_or_become_independent = {
						CHANGE = scope:change
						VASSAL = this
					}
				}
				every_vassal = {
					limit = { is_ai = no }
					send_interface_toast = {
						type = event_faction_neutral_text
						title = mandala_liege_primary_title_succession_dissolution
						desc = mandala_liege_primary_title_succession_dissolution_desc
						left_icon = root
					}
				}
				resolve_title_and_vassal_change = scope:change
				every_held_title = {
					limit = { tier >= tier_kingdom }
					add_to_temporary_list = titles_to_destroy
				}
			}
			#MAKE IT CLEAR in the text :evilblob:
			every_in_list = {
				list = titles_to_destroy
				holder = { destroy_title = prev }
			}
			#And remember it
			create_character_memory = {
				type = failed_mandala_trials_memory
				participants = { previous_mandala_ruler = scope:deceased_mandala_ruler }
			}
		}
		remove_variable = had_creation_skill_challenge
		remove_variable = had_serenity_skill_challenge
		remove_variable = had_destruction_skill_challenge
		remove_variable = had_trickery_skill_challenge
		remove_variable = mandala_skill_challenges
		remove_variable = mandala_skill_challenge_success
		remove_variable = busy_in_mandala_succession
	}
}

#Find our close relationship character
scripted_trigger mandala_events_9000_appropriate_relationship_character_trigger = {
	is_available_ai_adult = yes
	location = root.location
	OR = {
		faith = faith:vaishnavism
		faith = faith:shaivism
	}
}

#Find our bystander
scripted_effect mandala_events_9000_find_local_bystander_effect = {
	random_pool_character = {
		province = root.location
		limit = {
			is_lowborn = yes
			age > 10
			is_available = yes
			OR = {
				faith = faith:vaishnavism
				faith = faith:shaivism
			}
			has_mandala_faith_trigger = yes
			NOT = {
				is_of_major_or_minor_interest_trigger = { CHARACTER = root }
			}
		}
		save_scope_as = bystander
	}
	if = {
		limit = {
			NOT = { exists = scope:bystander }
		}
		create_character = {
			location = root.location
			template = generic_peasant_character
			random_culture = {
				culture:khmer = { trigger = { always = yes } }
				culture:cham = { trigger = { always = yes } }
				culture:mon = { trigger = { always = yes } }
				culture:tai = { trigger = { always = yes } }
				culture:malay = { trigger = { always = yes } }
				culture:burmese = { trigger = { always = yes } }
				culture:dayak = { trigger = { always = yes } }
				culture:bai = { trigger = { always = yes } }
			}
			faith = faith:shaivism
			after_creation = {
				random_list = {
					10 = {
						set_character_faith = faith:vaishnavism
					}
					10 = {
						set_character_faith = faith:shaivism
					}
				}
				remove_character_flag = peasant_outfit
				add_character_flag = created
			}
			save_scope_as = bystander
		}
	}
}

#Guess you're a divine now, Harry!
tgp_east_asia_mandala_events.9000 = {
	type = character_event
	title = tgp_east_asia_mandala_events.9000.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:divine_circumstance = flag:scar }
				desc = tgp_east_asia_mandala_events.9000.desc.scar
			}
			desc = tgp_east_asia_mandala_events.9000.desc.lightning_strike
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:divine_circumstance = flag:lightning_strike 
					has_mandala_faith_trigger = no 
				}
				desc = tgp_east_asia_mandala_events.9000.outro.diff_faith.lightning_strike
			}
			triggered_desc = {
				trigger = { scope:divine_circumstance = flag:lightning_strike }
				desc = tgp_east_asia_mandala_events.9000.outro.lightning_strike
			}
		}
	}
	theme = faith
	override_background = {
		trigger = { scope:divine_circumstance = flag:lightning_strike }
		reference = market
	}
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { scope:divine_circumstance = flag:lightning_strike }
			animation = delirium
		}
		animation = disbelief
	}
	right_portrait = {
		character = scope:bystander
		triggered_animation = {
			trigger = { scope:divine_circumstance = flag:lightning_strike }
			animation = thinking
		}
		animation = shock
	}
	trigger = {
		government_has_flag = government_is_wanua
		is_independent_ruler = yes
		is_available_adult = yes
		location = { is_sea_province = no }
		NOT = { has_character_modifier = divine_happenstance_modifier }
		has_mandala_culture_trigger = yes
		#We have a special decision for Mandala subjects...
		trigger_if = {
			limit = { is_tributary = yes }
			NOT = { 
				overlord = { government_has_flag = government_is_mandala }
			}
		}
	}
	weight_multiplier = {
		base = 1
		modifier = {
			add = 1
			is_ai = no
		}
		modifier = {
			add = 1
			has_realm_law = tribal_authority_2
		}
		modifier = {
			add = 1
			has_trait = scarred
		}
	}
	immediate = {
		#What was the random happenstance?
		random_list = {
			10 = {
				save_scope_value_as = {
					name = divine_circumstance
					value = flag:lightning_strike
				}
				#Find our bystander
				mandala_events_9000_find_local_bystander_effect = yes
			}
			20 = {
				trigger = { 
					has_trait = scarred
					OR = {
						any_relation = {
							type = friend
							mandala_events_9000_appropriate_relationship_character_trigger = yes
						}
						any_relation = {
							type = lover
							mandala_events_9000_appropriate_relationship_character_trigger = yes
						}
					}
				}
				save_scope_value_as = {
					name = divine_circumstance
					value = flag:scar
				}
				#Find our bystander
				random_list = {
					10 = {
						random_relation = {
							type = friend
							limit = { mandala_events_9000_appropriate_relationship_character_trigger = yes }
							save_scope_as = bystander
						}
					}
					10 = {
						random_relation = {
							type = lover
							limit = { mandala_events_9000_appropriate_relationship_character_trigger = yes }
							save_scope_as = bystander
						}
					}
				}
			}
		}
		#To make the tooltip look neat
		custom_tooltip = cheaper_mandala_government_conversion_tt
		add_character_modifier = {
			modifier = divine_happenstance_modifier
			years = cheaper_mandala_government_conversion_years
		}
	}
	#Yes please!
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { has_mandala_faith_trigger = no }
						desc = tgp_east_asia_mandala_events.9000.a.diff_faith
					}
					desc = tgp_east_asia_mandala_events.9000.a
				}
			}
		}
		if = {
			limit = { has_mandala_faith_trigger = no }
			set_character_faith = scope:bystander.faith
		}
		custom_description_no_bullet = { text = divine_happenstance_tt }
		#The boons
		show_as_tooltip = { divine_happenstance_adopt_mandala_effect = yes }
		set_variable = {
			name = vying_for_mandala_divinity
			years = cheaper_mandala_government_conversion_years
		}
		ai_chance = {
			base = 100
		}
	}
	#Eh... naah, maybe later
	option = { 
		name = tgp_east_asia_mandala_events.9000.b
		#Some lesser boons
		add_prestige = {
			value = major_prestige_value
			multiply = 1.5
		}
		scope:bystander = {
			if = {
				limit = { this = root.cp:councillor_court_chaplain }
				add_opinion = {
					target = root
					modifier = disappointed_opinion
					opinion = -5
				}
			}
		}
		
		ai_chance = {
			base = 100
		}
	}
	after = {
		scope:bystander = {
			silent_disappearance_ai_if_created_effect = yes
		}
	}
}
