﻿namespace = ep3_story_cycle_admin_eunuch

scripted_trigger ep3_story_cycle_admin_eunuch_valid_event_trigger = {
	scope:eunuch_temp ?= {
		is_available = yes
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_save_scopes_effect = {
	random_owned_story = {
		type = story_cycle_admin_eunuch
		save_scope_as = story
		story_owner = { save_scope_as = emperor }
		var:eunuch ?= { save_scope_as = eunuch }
		var:admin_title ?= { save_scope_as = admin_title }
		var:protege ?= { save_scope_as = protege }
		var:student ?= { save_scope_as = student }
		var:rival ?= { save_scope_as = rival }
		var:seducer ?= { save_temporary_scope_as = seducer }
		var:spouse ?= { save_temporary_scope_as = spouse }
		var:puppet ?= { save_temporary_scope_as = puppet }
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_realm_trigger = {
	is_ai = no
	highest_held_title_tier >= tier_empire
	top_liege = this
	government_allows = administrative
	culture = { has_cultural_parameter = can_appoint_chief_eunuch }
	NOR = {
		has_diarchy_type = co_emperorship
		any_owned_story = { type = story_cycle_admin_eunuch }
		exists = primary_title.var:story_cycle_admin_eunuch_cooldown
	}
	NAND = {
		has_active_diarchy = yes
		is_ai = yes
		diarch = { is_ai = no }
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_eunuch_trigger = {
	is_adult = yes
	is_ruler = no
	is_available = yes
	is_healthy = yes
	top_liege ?= root.top_liege
	is_eunuch_trigger = yes
	NOR = {
		has_trait = content
		has_trait = humble
		has_trait = honest
		has_relation_friend = root
		has_relation_lover = root
	}
	trigger_if = {
		limit = { exists = house }
		house != root.top_liege.house
	}
	sum_of_all_skills_value >= 20
}

scripted_trigger ep3_story_cycle_admin_eunuch_noble_family_trigger = {
	holder = {
		top_liege = root.top_liege
		house ?= {
			this != root.top_liege.house
			any_house_member = {
				count >= 1
				ep3_story_cycle_admin_eunuch_valid_eunuch_trigger = yes
			}
			any_house_member = {
				count >= 3
				top_liege = root.top_liege
				is_eunuch_trigger = no
			}
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_create_story_effect = {
	create_story = {
		type = story_cycle_admin_eunuch
		save_scope_as = story
	}
	scope:story = {
		set_variable = { name = eunuch value = scope:eunuch }
	}
	scope:eunuch = {
		if = {
			limit = { employer != root }
			set_employer = root
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_save_origin_effect = {
	random = {
		chance = 50
		random_neighboring_top_liege_realm_owner = { save_scope_as = origin_liege }
	}
	if = {
		limit = { NOT = { exists = scope:origin_liege } }
		save_scope_as = origin_liege
	}
	scope:origin_liege ?= {
		random_sub_realm_county = {
			limit = { duchy != root.capital_county.duchy }
			alternative_limit = { always = yes }
			random = {
				chance = 50
				random_neighboring_county = {
					limit = { duchy != root.capital_county.duchy }
					alternative_limit = { always = yes }
					save_scope_as = origin
				}
			}
			if = {
				limit = { NOT = { exists = scope:origin } }
				save_scope_as = origin
			}
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_create_eunuch_effect = {
	ep3_story_cycle_admin_eunuch_save_origin_effect = yes
	hidden_effect = {
		create_character = {
			culture = scope:origin.culture
			faith = root.faith
			employer = root
			gender = male
			age = { 
				integer_range = {
					min = 25
					max = 45
				}
			}
			diplomacy = {
				min_template_average_skill
				max_template_average_skill
			}
			martial = {
				min_template_low_skill
				max_template_low_skill
			}
			stewardship = {
				min_template_average_skill
				max_template_average_skill
			}
			intrigue = {
				min_template_average_skill
				max_template_average_skill
			}
			learning = {
				min_template_average_skill
				max_template_average_skill
			}
			random_traits_list = {
  				count = 1
  				education_diplomacy_3 = { weight = { base = 30 } }
  				education_stewardship_3 = { weight = { base = 30 } }
  				education_intrigue_3 = { weight = { base = 30 } }
  				education_diplomacy_4 = { weight = { base = 10 } }
  				education_stewardship_4 = { weight = { base = 10 } }
  				education_intrigue_4 = { weight = { base = 10 } }
  			}
			random_traits_list = {
				count = 1
				beardless_eunuch = { weight = { base = 90 } }
				eunuch_1 = { weight = { base = 10 } }
			}
			random_traits_list = {
				count = 3
				ambitious = {}
				arrogant = {}
				stubborn = {}
				deceitful = {}
				diligent = {}
				callous = {}
				greedy = {}
			}
			random_traits = yes
			dynasty = none
			save_scope_as = eunuch
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_create_family_children_effect = {
	save_scope_value_as = {
		name = count
		value = {
			value = { 2 3 }
			round = yes
		}
	}
	save_scope_value_as = {
		name = min_age
		value = $MIN_AGE$
	}
	save_scope_value_as = {
		name = max_age
		value = {
			value = $PARENT$.age
			add = -20
		}
	}
	while = {
		count = scope:count
		create_character = {
			culture = $PARENT$.culture
			faith = $PARENT$.faith
			employer = root
			age = {
				integer_range = {
					min = scope:min_age
					max = scope:max_age
				}
			}
			gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
			dynasty_house = scope:parent.house
			save_scope_as = newly_created_character
		}
		$PARENT$ = {
			if = {
				limit = { is_female = yes }
				scope:newly_created_character = { set_mother = $PARENT$ }
			}
			else = {
				scope:newly_created_character = { set_father = $PARENT$ }
			}
		}
		scope:newly_created_character = {
			if = {
				limit = { has_trait = infertile }
				remove_trait = infertile
			}
			if = {
				limit = { is_male = yes }
				random = {
					chance = 20
					random_list = {
						90 = { add_trait = beardless_eunuch }
						10 = { add_trait = eunuch_1 }
					}
				}
			}
			add_to_list = eunuch_$TYPE$
			add_to_list = new_arrivals
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_create_family_effect = {
	scope:eunuch ?= {
		hidden_effect = {
			if = {
				limit = {
					is_lowborn = yes
					any_close_family_member = { count < 3 }
					NOT = { has_character_flag = eunuch_family_generated }
				}
				father ?= { save_scope_as = eunuch_father }
				mother ?= { save_scope_as = eunuch_mother }
				every_sibling = {
					add_to_list = eunuch_siblings
					every_child = { add_to_list = eunuch_niblings }
				}
				add_character_flag = eunuch_family_generated
				# Parents
				if = {
					limit = {
						NOR = {
							exists = scope:eunuch_father
							exists = scope:eunuch_mother
						}
					}
					save_scope_value_as = {
						name = parent_min_age
						value = {
							value = scope:eunuch.age
							add = 20
						}
					}
					save_scope_value_as = {
						name = parent_max_age
						value = {
							value = scope:eunuch.age
							add = 40
							max = 80
						}
					}
					create_character = {
						culture = scope:eunuch.culture
						faith = scope:eunuch.faith
						employer = root
						gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
						age = { 
							integer_range = {
								min = scope:parent_min_age
								max = scope:parent_max_age
							}
						}
						dynasty = generate
						save_scope_as = parent
					}
					scope:parent ?= {
						add_to_list = new_arrivals
						if = {
							limit = { is_female = yes }
							save_scope_as = eunuch_mother
							scope:eunuch ?= { set_mother = scope:parent }
						}
						else = {
							save_scope_as = eunuch_father
							scope:eunuch ?= { set_father = scope:parent }
						}
						scope:eunuch ?= { set_house = scope:parent.house }
						if = {
							limit = { age > 65 }
							death = { death_reason = death_natural_causes }
						}
					}
				}
				if = {
					limit = { NOT = { exists = scope:parent } }
					if = {
						limit = { exists = scope:eunuch_mother }
						scope:eunuch_mother ?= { save_scope_as = parent }
					}
					else = {
						scope:eunuch_father ?= { save_scope_as = parent }
					}
				}
				if = {
					limit = { exists = scope:parent.house }
					# Siblings
					ep3_story_cycle_admin_eunuch_create_family_children_effect = { TYPE = sibling PARENT = scope:parent MIN_AGE = 16 }
					# Niblings
					every_in_list = {
						list = new_arrivals
						limit = {
							is_in_list = eunuch_sibling
							age > 20
							is_eunuch_trigger = no
							NOT = { has_trait = infertile }
						}
						save_scope_as = eunuch_sibling
						ep3_story_cycle_admin_eunuch_create_family_children_effect = { TYPE = nibling PARENT = scope:eunuch_sibling MIN_AGE = 1 }
					}
				}
			}
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_upgrade_effect = {
	custom_description_no_bullet = {
		text = ep3_story_cycle_admin_eunuch_upgrade_modifier_tt
		object = scope:eunuch
	}
	scope:story.story_owner = {
		ep3_story_cycle_admin_eunuch_upgrade_modifier_effect = { TYPE = liege }
		if = {
			limit = {
				has_active_diarchy = yes
				diarch = scope:eunuch
			}
			change_diarchy_swing = minor_sop_swing_diarch_gain
		}
	}
	scope:eunuch = {
		ep3_story_cycle_admin_eunuch_upgrade_modifier_effect = { TYPE = eunuch }
	}
	ep3_story_cycle_admin_eunuch_score_effect = { VALUE = 1 }
}

scripted_effect ep3_story_cycle_admin_eunuch_downgrade_effect = {
	custom_description_no_bullet = {
		text = ep3_story_cycle_admin_eunuch_downgrade_modifier_tt
		object = scope:eunuch
	}
	scope:story.story_owner = {
		ep3_story_cycle_admin_eunuch_downgrade_modifier_effect = { TYPE = liege }
		if = {
			limit = {
				has_active_diarchy = yes
				diarch = scope:eunuch
			}
			change_diarchy_swing = minor_sop_swing_liege_gain
		}
	}
	scope:eunuch = {
		ep3_story_cycle_admin_eunuch_downgrade_modifier_effect = { TYPE = eunuch }
	}
	ep3_story_cycle_admin_eunuch_score_effect = { VALUE = -1 }
	scope:story = {
		change_variable = {
			name = eunuch_refusals
			add = 1
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_upset_courtier_effect = {
	scope:eunuch = {
		hidden_effect = {
			progress_towards_rival_effect = {
				REASON = rival_admin_eunuch
				CHARACTER = $COURTIER$
				OPINION = default_rival_opinion
			}
		}
	}
	$COURTIER$ = {
		custom_tooltip = admin_eunuch_rivalry_tt
	}
	scope:story = {
		add_to_variable_list = {
			name = upset_courtiers
			target = $COURTIER$
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_add_ally_effect = {
	scope:story = {
		add_to_variable_list = {
			name = eunuch_allies
			target = $ALLY$
		}
	}
	$ALLY$ = {
		progress_towards_friend_effect = {
			REASON = friend_admin_eunuch_vassal
			CHARACTER = scope:eunuch
			OPINION = default_friend_opinion
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_score_effect = {
	scope:story ?= {
		change_variable = {
			name = eunuch_score
			add = $VALUE$
		}
		change_variable = {
			name = eunuch_total
			add = 1
		}
	}
}

##################################################
# Start
# by Joe Parkin
# 1000
##################################################

# Start maintenance
ep3_story_cycle_admin_eunuch.1000 = {
	hidden = yes
	type = character_event
	cooldown = { years = 10 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_realm_trigger = yes
	}

	weight_multiplier = {
		base = 1
		modifier = {
			is_adult = no
			add = 1
		}
		modifier = {
			has_diarchy_type = regency
			add = 1
		}
		modifier = {
			any_courtier = { ep3_story_cycle_admin_eunuch_valid_eunuch_trigger = yes }
			add = 1
		}
		modifier = {
			any_noble_family = { ep3_story_cycle_admin_eunuch_noble_family_trigger = yes }
			add = 1
		}
	}

	immediate = {
		primary_title = {
			set_variable = {
				name = story_cycle_admin_eunuch_cooldown
				years = 25
			}
		}
		every_noble_family = { # Eunuch of a Noble Family
			limit = { ep3_story_cycle_admin_eunuch_noble_family_trigger = yes }
			holder.house = {
				every_house_member = {
					limit = { ep3_story_cycle_admin_eunuch_valid_eunuch_trigger = yes }
					add_to_list = nf_eunuchs
				}
			}
		}
		every_courtier = { # Other Eunuchs
			limit = {
				NOT = { is_in_list = nf_eunuchs }
				ep3_story_cycle_admin_eunuch_valid_eunuch_trigger = yes
			}
			add_to_list = eunuchs
		}
		ordered_in_list = {
			list = nf_eunuchs
			order_by = sum_of_all_skills_value
			save_scope_as = eunuch
		}
		if = {
			limit = { NOT = { exists = scope:eunuch } }
			ordered_in_list = {
				list = eunuchs
				order_by = sum_of_all_skills_value
				save_scope_as = eunuch
			}	
		}
		if = {
			limit = { NOT = { exists = scope:eunuch } }
			ep3_story_cycle_admin_eunuch_create_eunuch_effect = yes
		}
		else = {
			random_sub_realm_county = {
				limit = {
					culture = scope:eunuch.culture
					duchy != root.capital_county.duchy
				}
				alternative_limit = { duchy != root.capital_county.duchy }
				alternative_limit = { culture = scope:eunuch.culture }
				alternative_limit = { always = yes }
				save_scope_as = origin
			}
		}
		ep3_story_cycle_admin_eunuch_create_family_effect = yes
		ep3_story_cycle_admin_eunuch_create_story_effect = yes
		trigger_event = {
			id = ep3_story_cycle_admin_eunuch.1001
			delayed = yes
		}
	}
}

# Emperor start
ep3_story_cycle_admin_eunuch.1001 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.1001.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { has_title = title:e_byzantium }
				desc = ep3_story_cycle_admin_eunuch.1001.intro_byz
			}
			desc = ep3_story_cycle_admin_eunuch.1001.intro
		}
		desc = ep3_story_cycle_admin_eunuch.1001.desc
		triggered_desc = {
			trigger = { any_in_list = { list = new_arrivals } }
			desc = ep3_story_cycle_admin_eunuch.1001.arrivals
		}
	}
	theme = administrative
	left_portrait = {
		character = root
		animation = war_over_tie
	}
	right_portrait = {
		character = scope:eunuch
		animation = writing
	}
	lower_center_portrait = scope:noble_family_head

	immediate = {
		play_music_cue = "mx_cue_succession"
		# Noble Family
		scope:eunuch.house ?= {
			if = {
				limit = {
					NOT = {
						any_house_member = {
							any_held_title = { is_noble_family_title = yes }
						}
					}
				}
				ordered_house_member = {
					limit = {
						exists = this
						this != scope:eunuch
					}
					order_by = age
					save_scope_as = family_head
				}
			}
		}
		scope:family_head ?= {
			if = {
				limit = {
					this != house.house_head
				}
				hidden_effect = {
					house = { set_house_head = scope:family_head }
				}
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = scope:family_head }
			if = {
				limit = {
					NOT = { government_has_flag = government_is_administrative }
				}
				hidden_effect = { change_government = administrative_government }
			}
		}
		every_in_list = {
			list = new_arrivals
			limit = {
				is_ruler = no
				is_alive = yes
				NOR = {
					this = scope:eunuch
					employer = scope:family_head
				}
			}
			hidden_effect = { set_employer = scope:family_head }
		}
		save_scope_as = liege
		scope:eunuch ?= {
			save_scope_as = candidate
			change_influence = major_influence_gain
			hidden_effect = {
				if = {
					limit = { employer != root }
					set_employer = root
				}
			}
		}
		# Starting bonus to lure you in
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
	}

    option = {
        name = ep3_story_cycle_admin_eunuch.1001.a
        flavor = admin_eunuch_flavor_tt
        trigger = { can_employ_court_position_type = chief_eunuch_court_position }
        scope:eunuch = {
        	set_variable = { name = target_court_position value = flag:chief_eunuch years = 1 }
        }
        court_position_old_holder_effect = { POS = chief_eunuch EMPLOYER = root }
        court_position_grant_effect = { POS = chief_eunuch CANDIDATE = scope:eunuch EMPLOYER = root }
        reverse_add_opinion = {
            target = scope:eunuch
            modifier = grateful_opinion
            opinion = 15
        }
    }


	option = {
		name = ep3_story_cycle_admin_eunuch.1001.b
		flavor = admin_eunuch_flavor_tt
	}
}

##################################################
# Demand Regent
# by Joe Parkin
# 2010
##################################################

ep3_story_cycle_admin_eunuch.2010 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2010.t
	desc = ep3_story_cycle_admin_eunuch.2010.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:eunuch
		triggered_animation = {
			trigger = { OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion } }
			animation = acknowledging # anim uses crucifix scepter
		}
		animation = war_over_tie
	}
	lower_center_portrait = {
		character = scope:diarch_successor
		trigger = {
			scope:diarch_successor != scope:eunuch
		}
	}
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:story_temp.var:eunuch_total >= 1
		has_active_diarchy = no
		NOR = {
			diarch ?= {
				this = scope:eunuch_temp
				is_designated_diarch = yes
			}
			designated_diarch ?= scope:eunuch_temp
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			is_adult = no
			add = 1
		}
		modifier = {
			age > 50
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		ordered_diarchy_succession_character = {
			limit = {
				is_diarchy_successor = yes
				this != scope:eunuch
			}
			order_by = diarchy_regent_succession_score_value
			save_scope_as = diarch_successor
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2010.a
		scope:diarch_successor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = ep3_story_cycle_admin_eunuch.toast
				left_icon = scope:eunuch
				right_icon = scope:emperor
				scope:emperor = { designate_diarch = scope:eunuch }
			}
		}
		if = {
			limit = { scope:diarch_successor != scope:eunuch }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:diarch_successor }
			reverse_add_opinion = {
				target = scope:diarch_successor
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		stress_impact = {
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2010.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:diarch_successor ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = medium_stress_impact_gain
			lazy = medium_stress_impact_gain
			fickle = medium_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

##################################################
# Demand Regency
# by Joe Parkin
# 2015
##################################################

ep3_story_cycle_admin_eunuch.2015 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2015.t
	desc = {
		desc = ep3_story_cycle_admin_eunuch.2015.desc
		first_valid = {
			triggered_desc = {
				trigger = { is_at_war = yes }
				desc = ep3_story_cycle_admin_eunuch.2015.war
			}
			triggered_desc = {
				trigger = { has_trait = lifestyle_hunter }
				desc = ep3_story_cycle_admin_eunuch.2015.hunter
			}
			desc = ep3_story_cycle_admin_eunuch.2015.fallback
		}
		desc = ep3_story_cycle_admin_eunuch.2015.outro
	}
	theme = administrative
	override_background = { reference = study }
	left_portrait = {
		character = root
		animation = boredom
	}
	right_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:story_temp.var:eunuch_total >= 1
		OR = {
			designated_diarch ?= scope:eunuch_temp
			diarchy_successor ?= scope:eunuch_temp
		}
		has_active_diarchy = no
	}

	weight_multiplier = {
		base = 1
		modifier = {
			is_at_war = yes
			add = 1
		}
		modifier = {
			has_trait = lazy
			add = 1
		}
		modifier = {
			has_trait = lifestyle_hunter
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		primary_title.current_heir = { save_scope_as = title_heir }
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2015.a
		if = {
			limit = { exists = scope:title_heir }
			scope:title_heir = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = ep3_story_cycle_admin_eunuch.toast
					left_icon = scope:eunuch
					right_icon = scope:emperor
				}
				scope:emperor = { try_start_diarchy = regency }
			}
		}
		else = { try_start_diarchy = regency }
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		if = {
			limit = { exists = scope:title_heir }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:title_heir }
			reverse_add_opinion = {
				target = scope:title_heir
				modifier = angry_opinion
				opinion = -15
			}
		}
		stress_impact = {
			base = minor_stress_impact_loss
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2015.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:title_heir ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			base = medium_stress_impact_gain
			trusting = medium_stress_impact_gain
			lazy = medium_stress_impact_gain
			fickle = medium_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

##################################################
# Demand Governorship
# by Joe Parkin
# 2020
##################################################

# Family
scripted_trigger ep3_story_cycle_admin_eunuch_valid_family_member_trigger = {
	is_adult = yes
	top_liege ?= root.top_liege
	is_ruler = no
	is_landed = no
	is_healthy = yes
	save_temporary_scope_as = family_temp
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = {
	tier < root.primary_title.tier
	tier >= tier_duchy
	tier <= tier_kingdom
	is_noble_family_title = no
	is_landless_type_title = no
	title_capital_county.duchy != root.capital_county.duchy
	save_temporary_scope_as = title_temp
	$GOVERNOR$ = { can_appoint_for_title = scope:title_temp }
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_appointment_vassal_trigger = {
	is_governor = yes
	NOR = {
		is_close_family_of = root
		is_close_family_of = $EUNUCH$
		is_close_family_of = $GOVERNOR$
		is_consort_of = root
		is_consort_of = $EUNUCH$
		is_consort_of = $GOVERNOR$
		has_relation_lover = root
		has_relation_lover = $EUNUCH$
		has_relation_lover = $GOVERNOR$
		has_relation_friend = root
		has_relation_friend = $EUNUCH$
		has_relation_friend = $GOVERNOR$
	}
	any_held_title = {
		ep3_story_cycle_admin_eunuch_valid_appointment_governorship_trigger = { GOVERNOR = $GOVERNOR$ }
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_appointment_governorship_trigger = {
	tier >= tier_duchy
	tier <= tier_kingdom
	is_noble_family_title = no
	is_landless_type_title = no
	NOT = { exists = var:petition_house_rights }
	title_capital_county.duchy != root.capital_county.duchy
	save_temporary_scope_as = title_temp
	$GOVERNOR$ = { can_appoint_for_title = scope:title_temp }
}

ep3_story_cycle_admin_eunuch.2021 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2021.t
	desc = ep3_story_cycle_admin_eunuch.2021.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	right_portrait = {
		character = scope:governor
		animation = obsequious_bow
	}
	lower_right_portrait = {
		character = scope:title.holder
		trigger = {
			scope:title.holder != root
			scope:governor ?= {
				this != scope:title.holder
			}
			scope:title_heir ?= {
				this != scope:title.holder
			}
		}
	}
	lower_center_portrait = scope:title_heir
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		is_at_war = no
		# Valid eunuch family member
		scope:eunuch_temp ?= {
			any_close_family_member = { ep3_story_cycle_admin_eunuch_valid_family_member_trigger = yes }
		}
		# Valid grantable or candidacy governorship
		OR = {
			custom_tooltip = {
				text = ep3_story_cycle_admin_eunuch.2021.t # Dummy, to suppress a "scope dependent values in localization" error
				any_held_title = {
					ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = { GOVERNOR = scope:family_temp }
				}
			}
			any_vassal = {
				ep3_story_cycle_admin_eunuch_valid_appointment_vassal_trigger = { GOVERNOR = scope:family_temp EUNUCH = scope:eunuch_temp }
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 1
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			scope:eunuch_temp ?= {
				any_close_family_member = { ep3_story_cycle_admin_eunuch_valid_family_member_trigger = yes }
			}
			any_held_title = {
				ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = { GOVERNOR = scope:family_temp }
			}
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		# Family member
		scope:eunuch = {
			random_close_family_member = {
				limit = { ep3_story_cycle_admin_eunuch_valid_family_member_trigger = yes }
				weight = {
					base = 1
					modifier = {
						is_sibling_of = scope:eunuch
						add = 1
					}
					modifier = {
						is_nibling_of = scope:eunuch
						add = 1
					}
					modifier = {
						has_character_flag = admin_eunuch_family_boon_flag
						factor = 0
					}
				}
				save_scope_as = governor
			}
		}
		# Grantable governorship
		random_held_title = {
			limit = {
				ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = { GOVERNOR = scope:governor }
			}
			save_scope_as = title
		}
		# Candidacy governorship
		if = {
			limit = { NOT = { exists = scope:title } }
			scope:governor = {
				ordered_heir_title = {
					limit = {
						ep3_story_cycle_admin_eunuch_valid_appointment_governorship_trigger = { GOVERNOR = scope:governor }
					}
					order_by = "place_in_line_of_succession(scope:governor)"
					save_scope_as = title
				}
			}
		}
		if = {
			limit = { NOT = { exists = scope:title } }
			ordered_vassal = {
				limit = {
					ep3_story_cycle_admin_eunuch_valid_appointment_vassal_trigger = { GOVERNOR = scope:governor EUNUCH = scope:eunuch }
				}
				order_by = {
					value = age
					if = {
						limit = { health < fine_health }
						add = 25
					}
					if = {
						limit = { health <= poor_health }
						add = 25
					}
				}
				random_held_title = {
					limit = {
						ep3_story_cycle_admin_eunuch_valid_appointment_governorship_trigger = { GOVERNOR = scope:governor }
					}
					save_scope_as = title
				}
			}
		}
		scope:title = {
			ordered_title_heir = {
				limit = {
					NOR = {
						this = root
						this = scope:eunuch
						this = scope:governor
					}
				}
				save_scope_as = title_heir
			}
		}
	}

	option = {
		name = {
			text = ep3_story_cycle_admin_eunuch.2021.a.grant
			trigger = { scope:title.holder = root }
		}
		name = {
			text = ep3_story_cycle_admin_eunuch.2021.a
			trigger = { scope:title.holder != root }
		}
		scope:title_heir = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = ep3_story_cycle_admin_eunuch.toast
				left_icon = scope:eunuch
				right_icon = scope:emperor
				scope:title = {
					if = {
						limit = { holder = scope:emperor }
						create_title_and_vassal_change = {
							type = granted
							save_scope_as = change
							add_claim_on_loss = no
						}
						change_title_holder = {
							holder = scope:governor
							change = scope:change
							take_baronies = no
						}
						resolve_title_and_vassal_change = scope:change
					}
					else = {
						change_appointment_investment = {
							target = scope:governor
							value = appointment_score_major_value
						}
					}
				}
			}
		}
		if = {
			limit = { exists = scope:title_heir }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:title_heir }
			reverse_add_opinion = {
				target = scope:title_heir
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		scope:governor = {
			add_character_flag = { flag = admin_eunuch_family_boon_flag years = 5 }
		}
		stress_impact = {
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2021.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:title_heir ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

# Vassal
scripted_trigger ep3_story_cycle_admin_eunuch_valid_vassal_governor_trigger = {
	holder = {
		NOR = {
			has_character_flag = eunuch_vassal_offer_cooldown
			has_relation_rival = $EUNUCH$
			$EUNUCH$ ?= this
		}
		reverse_opinion = {
			target = $EUNUCH$
			value >= -50
		}
		any_held_title = {
			count = all
			OR = {
				is_landless_type_title = yes
				is_noble_family_title = yes
			}
		}
		house ?= {
			NOT = { $EUNUCH$.house ?= this }
		}
		save_temporary_scope_as = governor_temp
	}
}

ep3_story_cycle_admin_eunuch.2022 = {
	type = character_event
	hidden = yes
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		is_at_war = no
		any_noble_family = {
			ep3_story_cycle_admin_eunuch_valid_vassal_governor_trigger = { EUNUCH = scope:eunuch_temp }
		}
		# Valid grantable or candidacy governorship
		OR = {
			any_held_title = {
				ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = { GOVERNOR = scope:governor_temp }
			}
			any_vassal = {
				ep3_story_cycle_admin_eunuch_valid_appointment_vassal_trigger = { GOVERNOR = scope:governor_temp EUNUCH = scope:eunuch_temp }
			}
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		save_scope_as = liege
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		random_noble_family = {
			limit = {
				ep3_story_cycle_admin_eunuch_valid_vassal_governor_trigger = { EUNUCH = scope:eunuch }
			}
			weight = {
				base = 1
				modifier = {
					holder = { is_ai = no }
					factor = 4
				}
				modifier = {
					exists = holder.house
					add = {
						value = holder.house.house_power_score
						divide = 2
					}
				}
			}
			holder = {
				save_scope_as = governor
				add_character_flag = {
					flag = eunuch_vassal_offer_cooldown
					years = 2
				}
				trigger_event = ep3_story_cycle_admin_eunuch.2023
			}
		}
	}
}

ep3_story_cycle_admin_eunuch.2023 = {
	type = letter_event
	opening = char_interaction.0010.opening
	desc = ep3_story_cycle_admin_eunuch.2023.desc
	sender = {
		character = scope:eunuch
		animation = scheme
	}

	trigger = {
		scope:eunuch ?= { is_alive = yes }
		scope:governor ?= { is_alive = yes }
	}

	immediate = {
		scope:eunuch = { save_scope_as = actor }
		save_scope_as = recipient
	}

	option = { # Accept
		name = ep3_story_cycle_admin_eunuch.2023.a
		pay_short_term_gold = {
			gold = minor_gold_value
			target = scope:eunuch
		}
		custom_tooltip = ep3_story_cycle_admin_eunuch.2023.a.tt
		scope:eunuch = { change_influence = medium_influence_gain }
		ep3_story_cycle_admin_eunuch_add_ally_effect = { ALLY = root }
		scope:emperor = {
			trigger_event = {
				id = ep3_story_cycle_admin_eunuch.2024
				days = 5
			}
		}
		ai_chance = {
			base = 1
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_energy = 0.5
			}
			opinion_modifier = {
				opinion_target = root.top_liege
				multiplier = -0.25
			}
		}
	}

	option = { # Refuse
		name = ep3_story_cycle_admin_eunuch.2023.b
		reverse_add_opinion = {
			modifier = annoyed_opinion
			target = scope:eunuch
			opinion = -15
		}
		add_character_flag = {
			flag = eunuch_vassal_offer_cooldown
			years = 5
		}
		ai_chance = {
			base = 1
			ai_value_modifier = {
				ai_boldness = -0.5
				ai_energy = -0.5
			}
			opinion_modifier = {
				opinion_target = root.top_liege
				multiplier = 0.25
			}
		}
	}
}

ep3_story_cycle_admin_eunuch.2024 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2024.t
	desc = ep3_story_cycle_admin_eunuch.2024.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = scope:emperor
		animation = admiration
	}
	right_portrait = {
		character = scope:eunuch
		animation = happiness
	}
	lower_left_portrait = scope:title_heir
	lower_right_portrait = scope:governor
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		# Valid grantable or candidacy governorship
		OR = {
			any_held_title = {
				ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = { GOVERNOR = scope:governor }
			}
			any_vassal = {
				ep3_story_cycle_admin_eunuch_valid_appointment_vassal_trigger = { GOVERNOR = scope:governor EUNUCH = scope:eunuch_temp }
			}
		}
		scope:governor = {
			is_alive = yes
			is_landed = no
		}
	}

	immediate = {
		# Grantable governorship
		random_held_title = {
			limit = {
				ep3_story_cycle_admin_eunuch_valid_grantable_governorship_trigger = { GOVERNOR = scope:governor }
			}
			save_scope_as = title
		}
		# Candidacy governorship
		if = {
			limit = { NOT = { exists = scope:title } }
			scope:governor = {
				ordered_heir_title = {
					limit = {
						ep3_story_cycle_admin_eunuch_valid_appointment_governorship_trigger = { GOVERNOR = scope:governor }
					}
					order_by = "place_in_line_of_succession(scope:governor)"
					save_scope_as = title
				}
			}
		}
		if = {
			limit = { NOT = { exists = scope:title } }
			ordered_vassal = {
				limit = {
					ep3_story_cycle_admin_eunuch_valid_appointment_vassal_trigger = { GOVERNOR = scope:governor EUNUCH = scope:eunuch }
				}
				order_by = {
					value = age
					if = {
						limit = { health < fine_health }
						add = 25
					}
					if = {
						limit = { health <= poor_health }
						add = 25
					}
				}
				random_held_title = {
					limit = {
						ep3_story_cycle_admin_eunuch_valid_appointment_governorship_trigger = { GOVERNOR = scope:governor }
					}
					save_scope_as = title
				}
			}
		}
		scope:title = {
			ordered_title_heir = {
				limit = {
					NOR = {
						this = root
						this = scope:eunuch
						this = scope:governor
					}
				}
				save_scope_as = title_heir
			}
		}
	}

	option = {
		name = {
			text = ep3_story_cycle_admin_eunuch.2021.a.grant
			trigger = { scope:title.holder = root }
		}
		name = {
			text = ep3_story_cycle_admin_eunuch.2021.a
			trigger = { scope:title.holder != root }
		}
		scope:governor = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = ep3_story_cycle_admin_eunuch.toast
				left_icon = scope:eunuch
				right_icon = scope:emperor
				scope:title_heir = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = ep3_story_cycle_admin_eunuch.toast
						left_icon = scope:eunuch
						right_icon = scope:emperor
						scope:title = {
							if = {
								limit = { holder = scope:emperor }
								create_title_and_vassal_change = {
									type = granted
									save_scope_as = change
									add_claim_on_loss = no
								}
								change_title_holder = {
									holder = scope:governor
									change = scope:change
									take_baronies = no
								}
								resolve_title_and_vassal_change = scope:change
							}
							else = {
								change_appointment_investment = {
									target = scope:governor
									value = appointment_score_major_value
								}
							}
						}
					}
				}
			}
		}
		reverse_add_opinion = {
			target = scope:governor
			modifier = grateful_opinion
			opinion = 15
		}
		if = {
			limit = { exists = scope:title_heir }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:title_heir }
			reverse_add_opinion = {
				target = scope:title_heir
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		scope:governor = {
			add_character_flag = { flag = admin_eunuch_family_boon_flag years = 5 }
		}
		stress_impact = {
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2021.b
		scope:governor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = ep3_story_cycle_admin_eunuch.2023.b.toast
				left_icon = scope:eunuch
				right_icon = scope:emperor
				ep3_story_cycle_admin_eunuch_downgrade_effect = yes
			}
		}
		reverse_add_opinion = {
			target = scope:governor
			modifier = disappointed_opinion
			opinion = -15
		}
		scope:title_heir ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

##################################################
# Demand Patriarchate
# by Joe Parkin
# 2030-31
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_want_patriarch_trigger = {
	faith.religious_head_title.holder = {
		is_vassal_of = root
		NOR = {
			this = root
			this = scope:eunuch_temp
			is_close_family_of = scope:eunuch_temp
		}
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_patriarch_trigger = {
	faith = root.faith
	ep3_story_cycle_admin_eunuch_valid_family_member_trigger = yes
	NOR = {
		scope:protege_temp ?= this
		scope:protege ?= this
	}
	trigger_if = {
		limit = { root.faith = { has_doctrine = doctrine_clerical_gender_male_only } }
		is_male = yes
	}
	trigger_else_if = {
		limit = { root.faith = { has_doctrine = doctrine_clerical_gender_female_only } }
		is_female = yes
	}
	trigger_else = { always = yes }
	trigger_if = {
		limit = { root.faith = { has_doctrine = doctrine_clerical_marriage_disallowed } }
		is_married = no
		is_betrothed = no
	}
}

ep3_story_cycle_admin_eunuch.2031 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2031.t
	desc = {
		desc = ep3_story_cycle_admin_eunuch.2031.intro
		first_valid = {
			triggered_desc = {
				trigger = { location = title:c_byzantion.title_province }
				desc = ep3_story_cycle_admin_eunuch.2031.intro_byz
			}
			desc = ep3_story_cycle_admin_eunuch.2031.intro_fallback
		}
		desc = ep3_story_cycle_admin_eunuch.2031.desc
	}
	theme = administrative
	override_background = { reference = temple_scope }
	left_portrait = {
		character = scope:eunuch
		animation = debating
	}
	right_portrait = {
		character = scope:new_patriarch
		animation = prayer
	}
	lower_center_portrait = scope:old_patriarch

	trigger = {
		NOT = { has_character_flag = admin_eunuch_patriarch_flag }
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:story_temp.var:eunuch_total >= 3
		ep3_story_cycle_admin_eunuch_want_patriarch_trigger = yes
		scope:eunuch_temp ?= {
			any_close_family_member = { ep3_story_cycle_admin_eunuch_valid_patriarch_trigger = yes }
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			scope:eunuch_temp ?= {
				any_close_family_member = {
					ep3_story_cycle_admin_eunuch_valid_patriarch_trigger = yes
					is_clergy = yes
				}
			}
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		save_scope_as = root_scope
		add_character_flag = {
			flag = admin_eunuch_patriarch_flag
			years = 15
		}
		faith.religious_head_title.holder = {
			save_scope_as = old_patriarch
			save_scope_as = background_temple_scope
		}
		scope:eunuch = {
			random_close_family_member = {
				limit = {
					ep3_story_cycle_admin_eunuch_valid_patriarch_trigger = yes
					is_clergy = yes
				}
				alternative_limit = { ep3_story_cycle_admin_eunuch_valid_patriarch_trigger = yes }
				save_scope_as = new_patriarch
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2031.a
		add_piety = major_piety_loss
		add_hook = {
			type = influence_hook
			target = scope:new_patriarch
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		hidden_effect = {
			faith.religious_head_title = {
				change_title_holder = {
					holder = scope:new_patriarch
					change = scope:title_change
				}
			}
		}
		every_player = {
			limit = { faith = scope:emperor.faith }
			send_interface_toast = {
				type = event_toast_effect_bad
				title = ep3_story_cycle_admin_eunuch.toast
				left_icon = scope:eunuch
				right_icon = scope:new_patriarch
				show_as_tooltip = {
					faith.religious_head_title = {
						change_title_holder = {
							holder = scope:new_patriarch
							change = scope:title_change
						}
					}
				}
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		if = {
			limit = { exists = scope:old_patriarch }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:old_patriarch }
			scope:old_patriarch = {
				add_opinion = {
					target = scope:new_patriarch
					modifier = angry_opinion
					opinion = -50
				}
				house ?= {
					every_house_member = {
						limit = {
							NOR = {
								this = scope:eunuch
								this = scope:old_patriarch
								this = scope:new_patriarch
							}
						}
						reverse_add_opinion = {
							target = scope:old_patriarch
							modifier = angry_opinion
							opinion = -25
						}
					}
				}
			}
		}
		every_vassal = {
			vassal_stance = zealot
			limit = {
				NOR = {
					this = scope:eunuch
					this = scope:old_patriarch
					this = scope:new_patriarch
					house ?= scope:old_patriarch.house
				}
			}
			custom = every_zealot_vassal
			add_opinion = {
				modifier = angry_opinion
				target = root
				opinion = -25
			}
		}
		stress_impact = {
			zealous = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:old_patriarch
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_rationality = -0.5
				ai_zeal = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2031.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:old_patriarch ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			cynical = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:old_patriarch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_rationality = 0.5
				ai_zeal = 0.5
			}
		}
	}
}

##################################################
# Demand Council Seat
# by Joe Parkin
# 2040-41
##################################################

# Eunuch
scripted_effect ep3_story_cycle_admin_eunuch_save_council_position_effect = {
	random_list = { # Pick best position, even if occupied 
		2 = {
			trigger = {
				petition_liege_councillor_blocked_trigger = {
					ROLE = chancellor
					JOB_TAKER = $JOB_TAKER$
					LIEGE = $LIEGE$
				}
			}
			petition_liege_councillor_compare_modifier = { SKILL = diplomacy ROLE = chancellor JOB_TAKER = $JOB_TAKER$ }
			modifier = {
				exists = $JOB_TAKER$.cp:councillor_chancellor
				$JOB_TAKER$.cp:councillor_chancellor = { is_close_family_of = $EUNUCH$ }
				factor = 0
			}
			petition_liege_0101_councillor_scope_first_effect = { ROLE = chancellor JOB_TAKER = $JOB_TAKER$ LIEGE = $LIEGE$ }
		}
		2 = {
			trigger = {
				petition_liege_councillor_blocked_trigger = {
					ROLE = marshal
					JOB_TAKER = $JOB_TAKER$
					LIEGE = $LIEGE$
				}
			}
			petition_liege_councillor_compare_modifier = { SKILL = martial ROLE = marshal JOB_TAKER = $JOB_TAKER$ }
			modifier = {
				exists = $JOB_TAKER$.cp:councillor_marshal
				$JOB_TAKER$.cp:councillor_marshal = { is_close_family_of = $EUNUCH$ }
				factor = 0
			}
			petition_liege_0101_councillor_scope_first_effect = { ROLE = marshal JOB_TAKER = $JOB_TAKER$ LIEGE = $LIEGE$ }
		}
		2 = {
			trigger = {
				petition_liege_councillor_blocked_trigger = {
					ROLE = steward
					JOB_TAKER = $JOB_TAKER$
					LIEGE = $LIEGE$
				}
			}
			petition_liege_councillor_compare_modifier = { SKILL = stewardship ROLE = steward JOB_TAKER = $JOB_TAKER$ }
			modifier = {
				exists = $JOB_TAKER$.cp:councillor_steward
				$JOB_TAKER$.cp:councillor_steward = { is_close_family_of = $EUNUCH$ }
				factor = 0
			}
			petition_liege_0101_councillor_scope_first_effect = { ROLE = steward JOB_TAKER = $JOB_TAKER$ LIEGE = $LIEGE$ }
		}
		20 = {
			trigger = {
				petition_liege_councillor_blocked_trigger = {
					ROLE = spymaster
					JOB_TAKER = $JOB_TAKER$
					LIEGE = $LIEGE$
				}
			}
			petition_liege_councillor_compare_modifier = { SKILL = intrigue ROLE = spymaster JOB_TAKER = $JOB_TAKER$ }
			modifier = {
				exists = $JOB_TAKER$.cp:councillor_spymaster
				$JOB_TAKER$.cp:councillor_spymaster = { is_close_family_of = $EUNUCH$ }
				factor = 0
			}
			petition_liege_0101_councillor_scope_first_effect = { ROLE = spymaster JOB_TAKER = $JOB_TAKER$ LIEGE = $LIEGE$ }
		}
		2 = {
			trigger = {
				petition_liege_council_chaplain_appointable_trigger = {
					JOB_TAKER = $JOB_TAKER$
					LIEGE = $LIEGE$
				}
				petition_liege_councillor_blocked_trigger = {
					ROLE = court_chaplain
					JOB_TAKER = $JOB_TAKER$
					LIEGE = $LIEGE$
				}
			}
			petition_liege_councillor_compare_modifier = { SKILL = learning ROLE = court_chaplain JOB_TAKER = $JOB_TAKER$ }
			modifier = {
				exists = $JOB_TAKER$.cp:councillor_court_chaplain
				$JOB_TAKER$.cp:councillor_court_chaplain = { is_close_family_of = $EUNUCH$ }
				factor = 0
			}
			petition_liege_0101_councillor_scope_first_effect = { ROLE = court_chaplain JOB_TAKER = $JOB_TAKER$ LIEGE = $LIEGE$ }
		}
	}
}

ep3_story_cycle_admin_eunuch.2040 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2040.t
	desc = ep3_story_cycle_admin_eunuch.2040.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = root
		animation = war_over_win
	}
	right_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	lower_center_portrait = scope:second_party
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:eunuch_temp ?= { is_councillor = no }
		petition_liege_council_seat_liege_trigger = yes
		NAND = {
			diarch ?= scope:eunuch_temp
			has_active_diarchy = yes
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		save_scope_as = petition_liege
		scope:eunuch = {
			save_scope_as = petition_vassal
			ep3_story_cycle_admin_eunuch_save_council_position_effect = {
				JOB_TAKER = scope:eunuch
				LIEGE = root
				EUNUCH = scope:eunuch
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2040.a
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = ep3_story_cycle_admin_eunuch.2040.a.toast
			left_icon = scope:eunuch
			right_icon = scope:second_party
			if = {
				limit = { exists = scope:second_party }
				scope:second_party = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = ep3_story_cycle_admin_eunuch.toast
						left_icon = scope:eunuch
						right_icon = scope:emperor
						scope:emperor = { fire_councillor = scope:second_party }
					}
				}
			}
			switch = {
				trigger = scope:eunuch.var:petition_liege_council_first
				flag:chancellor = {
					assign_councillor_type = {
						type = councillor_chancellor
						target = scope:petition_vassal
					}
				}
				flag:marshal = {
					assign_councillor_type = {
						type = councillor_marshal
						target = scope:petition_vassal
					}
				}
				flag:steward = {
					assign_councillor_type = {
						type = councillor_steward
						target = scope:petition_vassal
					}
				}
				flag:spymaster = {
					assign_councillor_type = {
						type = councillor_spymaster
						target = scope:petition_vassal
					}
				}
				flag:court_chaplain = {
					assign_councillor_type = {
						type = councillor_court_chaplain
						target = scope:petition_vassal
					}
				}
			}
			petition_liege_block_firing_councillor_effect = {
				COURT_OWNER = root
				JOB_TAKER = scope:eunuch
			}
		}
		if = {
			limit = { exists = scope:second_party }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:second_party }
			reverse_add_opinion = {
				target = scope:second_party
				modifier = angry_opinion
				opinion = -15
			}
		}
		stress_impact = {
			diligent = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:second_party
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2040.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:second_party ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

# Family
scripted_trigger ep3_story_cycle_admin_eunuch_valid_family_councillor_trigger = {
	ep3_story_cycle_admin_eunuch_valid_family_member_trigger = yes
	highest_skill_value >= 8
	NOT = { this = $EUNUCH$ }
	is_councillor = no
	save_temporary_scope_as = councillor_temp
}

ep3_story_cycle_admin_eunuch.2041 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2041.t
	desc = ep3_story_cycle_admin_eunuch.2041.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	right_portrait = {
		character = scope:councillor
		animation = happiness
	}
	lower_center_portrait = scope:second_party
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		petition_liege_council_seat_liege_trigger = yes
		scope:eunuch_temp.house ?= {
			any_house_member = {
				ep3_story_cycle_admin_eunuch_valid_family_councillor_trigger = { EUNUCH = scope:eunuch_temp }
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			scope:eunuch_temp ?= {
				any_close_family_member = {
					liege = root
					is_councillor = yes
				}
			}
			factor = 0.25
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		save_scope_as = petition_liege
		scope:eunuch.house = {
			random_house_member = {
				limit = {
					ep3_story_cycle_admin_eunuch_valid_family_councillor_trigger = { EUNUCH = scope:eunuch }
				}
				weight = {
					base = 1
					modifier = {
						has_character_flag = admin_eunuch_family_boon_flag
						factor = 0
					}
				}
				save_scope_as = councillor
				save_scope_as = petition_vassal
				if = {
					limit = {
						employer != root
					}
					root = { add_courtier = scope:councillor }
				}
				ep3_story_cycle_admin_eunuch_save_council_position_effect = {
					JOB_TAKER = scope:councillor
					LIEGE = root
					EUNUCH = scope:eunuch
				}
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2041.a
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = ep3_story_cycle_admin_eunuch.2040.a.toast
			left_icon = scope:councillor
			right_icon = scope:second_party
			if = {
				limit = { exists = scope:second_party }
				send_interface_toast = {
					type = event_toast_effect_bad
					title = ep3_story_cycle_admin_eunuch.toast
					left_icon = scope:eunuch
					right_icon = scope:emperor
					scope:emperor = { fire_councillor = scope:second_party }
				}
			}
			switch = {
				trigger = scope:councillor.var:petition_liege_council_first
				flag:chancellor = {
					assign_councillor_type = {
						type = councillor_chancellor
						target = scope:councillor
					}
				}
				flag:marshal = {
					assign_councillor_type = {
						type = councillor_marshal
						target = scope:councillor
					}
				}
				flag:steward = {
					assign_councillor_type = {
						type = councillor_steward
						target = scope:councillor
					}
				}
				flag:spymaster = {
					assign_councillor_type = {
						type = councillor_spymaster
						target = scope:councillor
					}
				}
				flag:court_chaplain = {
					assign_councillor_type = {
						type = councillor_court_chaplain
						target = scope:councillor
					}
				}
			}
			petition_liege_block_firing_councillor_effect = {
				COURT_OWNER = root
				JOB_TAKER = scope:councillor
			}
			scope:second_party ?= {
				add_opinion = {
					target = root
					modifier = annoyed_opinion
					opinion = -10
				}
			}
		}
		scope:councillor = {
			add_character_flag = { flag = admin_eunuch_family_boon_flag years = 5 }
		}
		if = {
			limit = { exists = scope:second_party }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:second_party }
			reverse_add_opinion = {
				target = scope:second_party
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		stress_impact = {
			diligent = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:second_party
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2041.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:second_party ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

# Vassal
scripted_trigger ep3_story_cycle_admin_eunuch_valid_vassal_councillor_trigger = {
	is_ruler = no
	NOR = {
		has_character_flag = eunuch_vassal_offer_cooldown
		has_relation_rival = $EUNUCH$
		this = $EUNUCH$
	}
	reverse_opinion = {
		target = $EUNUCH$
		value >= -50
	}
	any_held_title = { is_noble_family_title = yes }
	is_councillor = no
	highest_skill_value >= 8
	house ?= {
		NOT = { $EUNUCH$.house ?= this }
	}
	save_temporary_scope_as = councillor_temp
}

ep3_story_cycle_admin_eunuch.2042 = {
	type = character_event
	hidden = yes
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		petition_liege_council_seat_liege_trigger = yes
		any_vassal = {
			ep3_story_cycle_admin_eunuch_valid_vassal_councillor_trigger = { EUNUCH = scope:eunuch_temp }
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		save_scope_as = liege
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		random_vassal = {
			limit = {
				ep3_story_cycle_admin_eunuch_valid_vassal_councillor_trigger = { EUNUCH = scope:eunuch }
			}
			weight = {
				base = 1
				modifier = {
					is_ai = no
					factor = 4
				}
				modifier = {
					exists = house
					add = {
						value = house.house_power_score
						divide = 2
					}
				}
				modifier = { add = highest_skill_value }
			}
			save_scope_as = councillor
			add_character_flag = {
				flag = eunuch_vassal_offer_cooldown
				years = 2
			}
			trigger_event = ep3_story_cycle_admin_eunuch.2043
		}
	}
}

ep3_story_cycle_admin_eunuch.2043 = {
	type = letter_event
	opening = char_interaction.0010.opening
	desc = ep3_story_cycle_admin_eunuch.2043.desc
	sender = {
		character = scope:eunuch
		animation = scheme
	}

	trigger = {
		scope:eunuch ?= { is_alive = yes }
		scope:councillor ?= { is_alive = yes }
	}

	immediate = {
		scope:eunuch = { save_scope_as = actor }
		save_scope_as = recipient
	}

	option = { # Accept
		name = ep3_story_cycle_admin_eunuch.2043.a
		pay_short_term_gold = {
			gold = minor_gold_value
			target = scope:eunuch
		}
		custom_tooltip = ep3_story_cycle_admin_eunuch.2043.a.tt
		scope:eunuch = { change_influence = medium_influence_gain }
		ep3_story_cycle_admin_eunuch_add_ally_effect = { ALLY = root }
		scope:emperor = {
			trigger_event = {
				id = ep3_story_cycle_admin_eunuch.2044
				days = 5
			}
		}
		ai_chance = {
			base = 1
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_energy = 0.5
			}
			opinion_modifier = {
				opinion_target = root.top_liege
				multiplier = -0.25
			}
		}
	}

	option = { # Refuse
		name = ep3_story_cycle_admin_eunuch.2043.b
		reverse_add_opinion = {
			modifier = annoyed_opinion
			target = scope:eunuch
			opinion = -15
		}
		add_character_flag = {
			flag = eunuch_vassal_offer_cooldown
			years = 5
		}
		ai_chance = {
			base = 1
			ai_value_modifier = {
				ai_boldness = -0.5
				ai_energy = -0.5
			}
			opinion_modifier = {
				opinion_target = root.top_liege
				multiplier = 0.25
			}
		}
	}
}

ep3_story_cycle_admin_eunuch.2044 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2044.t
	desc = ep3_story_cycle_admin_eunuch.2044.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = scope:emperor
		animation = admiration
	}
	right_portrait = {
		character = scope:eunuch
		animation = happiness
	}
	lower_left_portrait = scope:second_party
	lower_right_portrait = scope:councillor
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		petition_liege_council_seat_liege_trigger = yes
		scope:councillor = {
			is_alive = yes
			is_councillor = no
		}
	}

	immediate = {
		save_scope_as = petition_liege
		scope:councillor = { save_scope_as = petition_vassal }
		ep3_story_cycle_admin_eunuch_save_council_position_effect = {
			JOB_TAKER = scope:councillor
			LIEGE = root
			EUNUCH = scope:eunuch
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2041.a
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = ep3_story_cycle_admin_eunuch.2040.a.toast
			left_icon = scope:councillor
			right_icon = scope:second_party
			scope:councillor = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = ep3_story_cycle_admin_eunuch.2040.a.toast
					left_icon = scope:eunuch
					right_icon = scope:emperor
					if = {
						limit = { exists = scope:second_party }
						send_interface_toast = {
							type = event_toast_effect_bad
							title = ep3_story_cycle_admin_eunuch.toast
							left_icon = scope:eunuch
							right_icon = scope:councillor
							scope:emperor = { fire_councillor = scope:second_party }
						}
					}
					scope:emperor = {
						switch = {
							trigger = scope:councillor.var:petition_liege_council_first
							flag:chancellor = {
								assign_councillor_type = {
									type = councillor_chancellor
									target = scope:councillor
								}
							}
							flag:marshal = {
								assign_councillor_type = {
									type = councillor_marshal
									target = scope:councillor
								}
							}
							flag:steward = {
								assign_councillor_type = {
									type = councillor_steward
									target = scope:councillor
								}
							}
							flag:spymaster = {
								assign_councillor_type = {
									type = councillor_spymaster
									target = scope:councillor
								}
							}
							flag:court_chaplain = {
								assign_councillor_type = {
									type = councillor_court_chaplain
									target = scope:councillor
								}
							}
						}
						petition_liege_block_firing_councillor_effect = {
							COURT_OWNER = scope:emperor
							JOB_TAKER = scope:councillor
						}
					}
				}
			}
			scope:second_party ?= {
				add_opinion = {
					target = root
					modifier = annoyed_opinion
					opinion = -10
				}
			}
		}
		reverse_add_opinion = {
			target = scope:councillor
			modifier = grateful_opinion
			opinion = 15
		}
		if = {
			limit = { exists = scope:second_party }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:second_party }
			reverse_add_opinion = {
				target = scope:second_party
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		stress_impact = {
			diligent = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:second_party
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2041.b
		scope:councillor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = ep3_story_cycle_admin_eunuch.2043.b.toast
				left_icon = scope:eunuch
				right_icon = scope:emperor
				ep3_story_cycle_admin_eunuch_downgrade_effect = yes
			}
		}
		reverse_add_opinion = {
			target = scope:councillor
			modifier = disappointed_opinion
			opinion = -15
		}
		scope:second_party ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

##################################################
# Boon
# by Joe Parkin
# 2050
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_fabricate_hook_trigger = {
	OR = {
		is_councillor = no
		has_relation_rival = root
		opinion = {
			target = root
			value < 25
		}
	}
	save_temporary_scope_as = hookee_temp
	root = {
		can_add_hook = {
			target = scope:hookee_temp
			type = fabrication_hook
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_select_boon_effect = {
	random_list = {
		25 = { # Debase
			trigger = {
				NOT = { has_character_flag = eunuch_boon_debase_cooldown }
			}
			modifier = {
				gold < minor_gold_value
				factor = 2
			}
			modifier = {
				gold < 0
				factor = 2
			}
			save_scope_value_as = {
				name = eunuch_boon
				value = flag:debase
			}
		}
		25 = { # Raise taxes
			trigger = {
				NOR = {
					has_character_modifier = admin_eunuch_raise_taxes_modifier
					has_character_flag = eunuch_boon_raise_taxes_cooldown
				}
			}
			save_scope_value_as = {
				name = eunuch_boon
				value = flag:raise_taxes
			}
		}
		25 = { # Increase succession
			trigger = {
				player_heir ?= { is_ruler = no }
				any_vassal = {
					primary_title = {
						any_title_heir = { house = root.house }
					}
				}
				NOT = { has_character_flag = eunuch_boon_candidacy_cooldown }
			}
			random_vassal = {
				limit = {
					primary_title = {
						any_title_heir = { this = root.player_heir }
						current_heir != root.player_heir
					}
				}
				alternative_limit = {
					primary_title = {
						any_title_heir = { house ?= root.house }
						NOT = { current_heir.house ?= root.house }
					}
				}
				alternative_limit = {
					primary_title = {
						any_title_heir = { this = root.player_heir }
					}
				}
				alternative_limit = {
					primary_title = {
						any_title_heir = { house ?= root.house }
					}
				}
				primary_title = { save_scope_as = boon_title }
				save_scope_as = boon_victim
				root.player_heir = { save_scope_as = boon_target }
			}
			save_scope_value_as = {
				name = eunuch_boon
				value = flag:candidacy
			}
		}
		25 = { # Fabricate hook
			trigger = {
				OR = {
					any_powerful_vassal = { ep3_story_cycle_admin_eunuch_fabricate_hook_trigger = yes }
					any_councillor = { ep3_story_cycle_admin_eunuch_fabricate_hook_trigger = yes }
				}
				NOT = { has_character_flag = eunuch_boon_fabricate_hook_cooldown }
			}
			modifier = {
				ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
				scope:eunuch_temp ?= {
					OR = {
						has_trait = schemer
						has_trait = education_intrigue
						has_council_position = councillor_spymaster
					}
				}
				factor = 2
			}
			random_powerful_vassal = {
				limit = { ep3_story_cycle_admin_eunuch_fabricate_hook_trigger = yes }
				save_scope_as = boon_victim
			}
			if = {
				limit = { NOT = { exists = scope:boon_victim } }
				random_councillor = {
					limit = { ep3_story_cycle_admin_eunuch_fabricate_hook_trigger = yes }
					save_scope_as = boon_victim
				}
			}
			save_scope_value_as = {
				name = eunuch_boon
				value = flag:fabricate_hook
			}
		}
		25 = { # Imprison rebel
			trigger = {
				any_targeting_faction = { exists = faction_leader }
				NOT = { has_character_flag = eunuch_boon_imprison_rebel_cooldown }
			}
			ordered_targeting_faction = {
				limit = { exists = faction_leader }
				order_by = faction_discontent
				save_scope_as = boon_faction
				faction_leader = { save_scope_as = boon_victim }
			}
			save_scope_value_as = {
				name = eunuch_boon
				value = flag:imprison_rebel
			}
		}
		5 = { # Gain influence - fallback
			save_scope_value_as = {
				name = eunuch_boon
				value = flag:influence
			}
		}
	}
}

scripted_effect ep3_story_cycle_admin_eunuch_award_boon_effect = {
	switch = {
		trigger = scope:eunuch_boon
		flag:debase = {
			add_character_flag = {
				flag = eunuch_boon_debase_cooldown
				years = 5
			}
			add_short_term_gold = major_gold_value
			add_prestige = major_prestige_loss
		}
		flag:raise_taxes = {
			add_character_flag = {
				flag = eunuch_boon_raise_taxes_cooldown
				years = 5
			}
			add_character_modifier = {
				modifier = admin_eunuch_raise_taxes_modifier
				years = 5
			}
		}
		flag:candidacy = {
			add_character_flag = {
				flag = eunuch_boon_candidacy_cooldown
				years = 5
			}
			scope:boon_target = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = ep3_story_cycle_admin_eunuch.toast
					left_icon = scope:eunuch
					right_icon = scope:boon_title
					scope:boon_title = {
						change_appointment_investment = {
							target = scope:boon_target
							value = appointment_score_major_value
						}
					}
				}
			}
		}
		flag:fabricate_hook = {
			add_character_flag = {
				flag = eunuch_boon_fabricate_hook_cooldown
				years = 5
			}
			scope:boon_victim = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = ep3_story_cycle_admin_eunuch.toast
					left_icon = scope:eunuch
					right_icon = scope:emperor
					scope:emperor = {
						add_hook = {
							type = fabrication_hook
							target = scope:boon_victim
						}
					}
				}
			}
		}
		flag:imprison_rebel = {
			add_character_flag = {
				flag = eunuch_boon_imprison_rebel_cooldown
				years = 5
			}
			scope:boon_victim = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = ep3_story_cycle_admin_eunuch.toast
					left_icon = scope:eunuch
					right_icon = scope:emperor
					rightfully_imprison_character_less_verbose_effect = {
						TARGET = scope:boon_victim
						IMPRISONER = scope:emperor
					}
				}
			}
		}
		flag:influence = { change_influence = major_influence_gain }
	}
	if = {
		limit = { exists = scope:boon_victim }
		ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:boon_victim }
	}
}

ep3_story_cycle_admin_eunuch.2050 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2050.t
	desc = {
		desc = ep3_story_cycle_admin_eunuch.2050.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:eunuch_boon = flag:debase }
				desc = ep3_story_cycle_admin_eunuch.2050.debase
			}
			triggered_desc = {
				trigger = { scope:eunuch_boon = flag:raise_taxes }
				desc = ep3_story_cycle_admin_eunuch.2050.raise_taxes
			}
			triggered_desc = {
				trigger = { scope:eunuch_boon = flag:candidacy }
				desc = ep3_story_cycle_admin_eunuch.2050.candidacy
			}
			triggered_desc = {
				trigger = { scope:eunuch_boon = flag:fabricate_hook }
				desc = ep3_story_cycle_admin_eunuch.2050.fabricate_hook
			}
			triggered_desc = {
				trigger = { scope:eunuch_boon = flag:imprison_rebel }
				desc = ep3_story_cycle_admin_eunuch.2050.imprison_rebel
			}
			desc = ep3_story_cycle_admin_eunuch.2050.influence
		}
		desc = ep3_story_cycle_admin_eunuch.2050.outro
	}
	theme = administrative
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:eunuch
		triggered_animation = {
			trigger = { OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion } }
			animation = acknowledging # anim uses crucifix scepter
		}
		animation = war_over_tie
	}
	lower_left_portrait = scope:boon_target
	lower_right_portrait = scope:boon_victim

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = {
				value = scope:story_temp.var:eunuch_score
				min = 0
			}
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		ep3_story_cycle_admin_eunuch_select_boon_effect = yes
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2050.a
		ep3_story_cycle_admin_eunuch_award_boon_effect = yes
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		stress_impact = {
			diligent = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2050.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

##################################################
# Secret
# by Joe Parkin
# 2051
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_secret_trigger = {
	NOR = {
		is_known_by = root
		secret_owner = { is_close_family_of = $EUNUCH$ }
		secret_owner = root
		secret_target ?= { is_close_family_of = $EUNUCH$ }
		secret_target ?= root
		any_secret_participant = { is_close_family_of = $EUNUCH$ }
		any_secret_participant = { this = root }
	}
	any_secret_participant = {
		OR = {
			is_of_major_interest_to_root_trigger = yes
			is_of_minor_interest_to_root_trigger = yes
		}
	}
}

ep3_story_cycle_admin_eunuch.2051 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2051.t
	desc = ep3_story_cycle_admin_eunuch.2051.desc
	theme = administrative
	left_portrait = {
		character = scope:eunuch
		animation = schadenfreude
	}
	right_portrait = {
		character = scope:secret_owner
		animation = worry
	}
	lower_center_portrait = scope:secret_target

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		OR = {
			scope:eunuch_temp ?= {
				any_known_secret = {
					ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch_temp }
				}
			}
			any_vassal_or_below = {
				any_secret = {
					ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch_temp }
				}
			}
			any_courtier = {
				any_secret = {
					ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch_temp }
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = {
				value = scope:story_temp.var:eunuch_score
				min = 0
			}
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			scope:eunuch_temp ?= {
				OR = {
					has_trait = schemer
					has_trait = education_intrigue
					has_council_position = councillor_spymaster
				}
			}
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch = {
			random_known_secret = {
				limit = {
					secret_target ?= { is_of_major_interest_to_root_trigger = yes }
					ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
				}
				alternative_limit = {	
					secret_target ?= { is_of_minor_interest_to_root_trigger = yes }
					ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
				}
				alternative_limit = {
					ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
				}
				save_scope_as = secret
			}
		}
		if = {
			limit = { NOT = { exists = scope:secret } }
			every_vassal_or_below = {
				limit = {
					any_secret = {
						ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
					}
				}
				add_to_list = secret_havers
			}
			every_courtier = {
				limit = {
					any_secret = {
						ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
					}
				}
				add_to_list = secret_havers
			}
			random_in_list = {
				list = secret_havers
				limit = { is_of_major_interest_to_root_trigger = yes }
				alternative_limit = { is_of_minor_interest_to_root_trigger = yes }
				alternative_limit = { always = yes }
				weight = {
					base = 1
					modifier = { add = highest_held_title_tier }
				}
				random_secret = {
					limit = {
						secret_target ?= { is_of_major_interest_to_root_trigger = yes }
						ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
					}
					alternative_limit = {	
						secret_target ?= { is_of_minor_interest_to_root_trigger = yes }
						ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
					}
					alternative_limit = {
						ep3_story_cycle_admin_eunuch_secret_trigger = { EUNUCH = scope:eunuch }
					}
					save_scope_as = secret
				}
			}
		}
		scope:secret ?= {
			secret_owner = { save_scope_as = secret_owner }
			secret_target ?= { save_scope_as = secret_target }
			hidden_effect = {
				if = {
					limit = { NOT = { is_known_by = scope:eunuch } }
					reveal_to = scope:eunuch
				}
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2051.a
		custom_tooltip = {
			text = ep3_story_cycle_admin_eunuch.2051.a.tt
			send_interface_toast = {
				type = event_toast_effect_neutral
				left_icon = scope:secret_owner
				right_icon = scope:secret_target
				title = playdate_revealed_secret_log_title
				scope:secret_owner = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = ep3_story_cycle_admin_eunuch.toast
						left_icon = scope:eunuch
						right_icon = scope:emperor
						scope:secret = { reveal_to = scope:emperor }
					}
				}
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:secret_owner }
		reverse_add_opinion = {
			target = scope:secret_owner
			modifier = angry_opinion
			opinion = -15
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			compassionate = minor_stress_impact_gain
			honest = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_honor = -0.5
				ai_boldness = 0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2051.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		reverse_add_opinion = {
			target = scope:secret_owner
			modifier = grateful_opinion
			opinion = 15
		}
		stress_impact = {
			deceitful = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			sadistic = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:secret_owner
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_honor = 0.5
				ai_boldness = -0.5
			}
		}
	}
}

##################################################
# Scheme
# by Joe Parkin
# 2052
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_scheme_trigger = {
	is_hostile = yes
	is_scheme_exposed = no
	NOR = {
		scheme_is_character_agent = $EUNUCH$
		scheme_is_character_agent = root
		scheme_owner = $EUNUCH$
		scheme_owner = root
		scheme_defender = $EUNUCH$
		scheme_defender = root
	}
}

ep3_story_cycle_admin_eunuch.2052 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2052.t
	desc = ep3_story_cycle_admin_eunuch.2052.desc
	theme = administrative
	override_background = { reference = relaxing_room }
	left_portrait = {
		character = scope:eunuch
		animation = schadenfreude
	}
	right_portrait = {
		character = scope:scheme_owner
		animation = worry
	}

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		OR = {
			any_vassal_or_below = {
				any_scheme = { ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch_temp } }
			}
			any_courtier = {
				any_scheme = { ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch_temp } }
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = {
				value = scope:story_temp.var:eunuch_score
				min = 0
			}
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			scope:eunuch_temp ?= {
				OR = {
					has_trait = schemer
					has_trait = education_intrigue
					has_council_position = councillor_spymaster
				}
			}
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		every_vassal_or_below = {
			limit = {
				any_scheme = { ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch } }
			}
			add_to_list = scheme_havers
		}
		every_courtier = {
			limit = {
				any_scheme = { ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch } }
			}
			add_to_list = scheme_havers
		}
		random_in_list = {
			list = scheme_havers
			limit = { is_of_major_interest_to_root_trigger = yes }
			alternative_limit = { is_of_minor_interest_to_root_trigger = yes }
			alternative_limit = { always = yes }
			random_scheme = {
				limit = {
					scheme_defender = { is_of_major_interest_to_root_trigger = yes }
					ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch }
				}
				alternative_limit = {	
					scheme_defender = { is_of_minor_interest_to_root_trigger = yes }
					ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch }
				}
				alternative_limit = {
					ep3_story_cycle_admin_eunuch_scheme_trigger = { EUNUCH = scope:eunuch }
				}
				save_scope_as = scheme
				scheme_owner = { save_scope_as = scheme_owner }
				scheme_defender ?= { save_scope_as = scheme_target }
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2052.a
		custom_tooltip = {
			text = ep3_story_cycle_admin_eunuch.2052.a.tt
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = ep3_story_cycle_admin_eunuch.2052.a.tt
				left_icon = scope:scheme_owner
				right_icon = scope:scheme_target
				scope:scheme_owner = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = ep3_story_cycle_admin_eunuch.toast
						left_icon = scope:eunuch
						right_icon = scope:emperor
						scope:scheme = { expose_scheme = yes }
					}
				}
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:scheme_owner }
		stress_impact = {
			trusting = minor_stress_impact_gain
			compassionate = minor_stress_impact_gain
			honest = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_honor = -0.5
				ai_boldness = 0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2052.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		reverse_add_opinion = {
			target = scope:scheme_owner
			modifier = grateful_opinion
			opinion = 15
		}
		stress_impact = {
			deceitful = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			sadistic = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:scheme_owner
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_honor = 0.5
				ai_boldness = -0.5
			}
		}
	}
}

##################################################
# Demand Court Position
# by Joe Parkin
# 2060-61
##################################################

ep3_story_cycle_admin_eunuch.2060 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2060.t
	desc = ep3_story_cycle_admin_eunuch.2060.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = war_over_win
	}
	right_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	lower_center_portrait = scope:second_party
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:eunuch_temp ?= { has_any_court_position = no }
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch = {
			court_position_generator_effect = {
				VAR = target
				EMPLOYER = root
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2060.a
		scope:eunuch = {
			court_position_generator_assignment_effect = {
				VAR = target
				EMPLOYER = root
				CANDIDATE = scope:eunuch
			}
		}
		if = {
			limit = { exists = scope:old_holder }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:old_holder }
			reverse_add_opinion = {
				target = scope:old_holder
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		stress_impact = {
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = -0.25
				ai_boldness = -0.25
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2060.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:old_holder ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_valid_family_position_trigger = {
	ep3_story_cycle_admin_eunuch_valid_family_member_trigger = yes
	has_any_court_position = no
	save_temporary_scope_as = position_temp
}

ep3_story_cycle_admin_eunuch.2061 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2061.t
	desc = ep3_story_cycle_admin_eunuch.2061.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	right_portrait = {
		character = scope:positioner
		animation = happiness
	}
	lower_center_portrait = scope:second_party
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:eunuch_temp ?= {
			any_close_family_member = { ep3_story_cycle_admin_eunuch_valid_family_position_trigger = yes }
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch = {
			random_close_family_member = {
				limit = { ep3_story_cycle_admin_eunuch_valid_family_position_trigger = yes }
				weight = {
					base = 1
					modifier = {
						is_house_head = yes
						factor = 0
					}
				}
				save_scope_as = positioner
				hidden_effect = {
					set_employer = root
				}
				court_position_generator_effect = {
					VAR = target
					EMPLOYER = root
				}
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2061.a
		scope:positioner = {
			court_position_generator_assignment_effect = {
				VAR = target
				EMPLOYER = root
				CANDIDATE = scope:positioner
			}
		}
		if = {
			limit = { exists = scope:old_holder }
			ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:old_holder }
			reverse_add_opinion = {
				target = scope:old_holder
				modifier = angry_opinion
				opinion = -15
			}
		}
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		stress_impact = {
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = -0.25
				ai_boldness = -0.25
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2061.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:old_holder ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
		scope:positioner = {
			hidden_effect = {
				if = {
					limit = {
						scope:eunuch.house.house_head = {
							is_landed_or_landless_administrative = yes
						}
					}
					set_employer = scope:eunuch.house.house_head
				}
				else = {
					move_to_pool = yes
				}
			}
		}
	}
}

##################################################
# Demand Family Marriage
# by Joe Parkin
# 2070
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_marry_trigger = {
	is_available = yes
	is_playable_character = no
	is_adult = yes
	is_married = no
	is_ruler = no
	is_betrothed = no
	save_temporary_scope_as = marriage_temp
}

scripted_trigger ep3_story_cycle_admin_eunuch_emperor_marry_trigger = {
	age < $MARRY$.age_plus_10
	age >= $MARRY$.age_minus_10
	is_available = yes
	is_playable_character = no
	is_adult = yes
	is_married = no
	is_betrothed = no
	scriptedtests_can_marry_character = $MARRY$
}

ep3_story_cycle_admin_eunuch.2070 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.2070.t
	desc = ep3_story_cycle_admin_eunuch.2070.desc
	theme = administrative
	override_background = { reference = council_chamber }
	left_portrait = {
		character = scope:eunuch
		animation = debating
	}
	right_portrait = {
		character = scope:eunuch_marry
		animation = war_over_tie
	}
	lower_left_portrait = scope:emperor_marry
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:eunuch_temp ?= {
			any_close_family_member = {
				ep3_story_cycle_admin_eunuch_marry_trigger = yes
				root = {
					any_close_family_member = {
						ep3_story_cycle_admin_eunuch_emperor_marry_trigger = { MARRY = scope:marriage_temp }
					}
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			scope:eunuch_temp ?= {
				any_close_family_member = {
					OR = {
						primary_spouse ?= { is_close_family_of = root }
						betrothed ?= { is_close_family_of = root }
					}
				}
			}
			factor = 0.25
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch ?= {
			random_close_family_member = {
				limit = {
					ep3_story_cycle_admin_eunuch_marry_trigger = yes
					root = {
						any_close_family_member = {
							ep3_story_cycle_admin_eunuch_emperor_marry_trigger = { MARRY = scope:marriage_temp }
						}
					}
				}
				save_scope_as = eunuch_marry
				root = {
					random_close_family_member = {
						limit = {
							ep3_story_cycle_admin_eunuch_emperor_marry_trigger = { MARRY = scope:eunuch_marry }
						}
						save_scope_as = emperor_marry
					}
				}
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2070.a
		scope:eunuch_marry = { marry = scope:emperor_marry }
		ep3_story_cycle_admin_eunuch_upgrade_effect = yes
		ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:emperor_marry }
		reverse_add_opinion = {
			target = scope:emperor_marry
			modifier = angry_opinion
			opinion = -15
		}
		stress_impact = {
			paranoid = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = -0.25
				ai_boldness = -0.25
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.2070.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		scope:emperor_marry ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 15
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:eunuch_marry
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
	}
}

##################################################
# Protégé
# by Joe Parkin
# 3000
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_family_protege_trigger = {
	age >= 8
	is_ruler = no
	is_available = yes
	is_healthy = yes
	top_liege ?= root.top_liege
	age < $EUNUCH$.age
	is_eunuch_trigger = no
	OR = {
		AND = {
			is_male = yes
			root.faith = { has_doctrine = doctrine_gender_male_dominated }
		}
		AND = {
			is_female = yes
			root.faith = { has_doctrine = doctrine_gender_female_dominated }
		}
		root.faith = { has_doctrine = doctrine_gender_equal }
	}
}

ep3_story_cycle_admin_eunuch.3000 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.3000.t
	desc = ep3_story_cycle_admin_eunuch.3000.desc
	theme = administrative
	override_background = { reference = garden }
	left_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	right_portrait = {
		character = scope:protege
		animation = personality_bold
	}
	cooldown = { years = 15 }

	trigger = {
		primary_title = {
			has_title_law_flag = appointment_type_succession
		}
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		trigger_if = {
			limit = { exists = scope:story_temp.var:eunuch_total }
			scope:story_temp.var:eunuch_total >= 3
		}
		NOR = {
			exists = scope:protege_temp
			exists = scope:puppet_temp
		}
		scope:eunuch_temp = {
			any_close_family_member = {
				ep3_story_cycle_admin_eunuch_family_protege_trigger = { EUNUCH = scope:eunuch_temp }
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = scope:story_temp.var:eunuch_total
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch = {
			random_close_family_member = {
				limit = {
					ep3_story_cycle_admin_eunuch_family_protege_trigger = { EUNUCH = scope:eunuch }
				}
				weight = {
					base = 1
					modifier = { add = sum_of_all_skills_value }
					modifier = {
						is_nibling_of = scope:eunuch
						add = 20
					}
				}
				save_scope_as = protege
			}
		}
		scope:protege ?= {
			if = {
				limit = {
					employer != root
				}
				set_employer = root
			}
		}
		scope:story = {
			set_variable = {
				name = protege
				value = scope:protege
			}
		}
		scope:protege = {
			add_stewardship_skill = 1
			add_intrigue_skill = 1
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.3000.a
		primary_title = {
			change_appointment_investment = {
				target = scope:protege
				value = appointment_score_major_value
			}
		}
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_student_trigger = {
	is_adult = yes
	is_ruler = no
	is_male = yes
	is_available = yes
	is_healthy = yes
	top_liege ?= root.top_liege
	age < scope:eunuch.age
	is_eunuch_trigger = yes
	NOR = {
		has_relation_rival = scope:eunuch
		scope:puppet ?= this
		scope:protege ?= this
		scope:rival ?= this
	}
}

ep3_story_cycle_admin_eunuch.3001 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.3001.t
	desc = ep3_story_cycle_admin_eunuch.3001.desc
	theme = administrative
	override_background = { reference = study }
	left_portrait = {
		character = scope:eunuch
		animation = scheme
	}
	right_portrait = {
		character = scope:student
		animation = writing
	}
	cooldown = { years = 15 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		NOT = { exists = scope:student_temp }
		trigger_if = {
			limit = { exists = scope:story_temp.var:eunuch_total }
			scope:story_temp.var:eunuch_total >= 5
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = scope:story_temp.var:eunuch_total
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch = {
			if = {
				limit = {
					any_close_family_member = { ep3_story_cycle_admin_eunuch_student_trigger = yes }
				}
				random_close_family_member = {
					limit = { ep3_story_cycle_admin_eunuch_student_trigger = yes }
					save_scope_as = student
				}
			}
			else_if = {
				limit = {
					root = {
						any_courtier = { ep3_story_cycle_admin_eunuch_student_trigger = yes }
					}
				}
				random_courtier = {
					limit = { ep3_story_cycle_admin_eunuch_student_trigger = yes }
					save_scope_as = student
				}
			}
			else = {
				ep3_story_cycle_admin_eunuch_save_origin_effect = yes
				create_character = {
					culture = scope:origin.culture
					faith = root.faith
					location = root.location
					gender = male
					age = {
						value = {
							add = {
								integer_range = {
									min = 18
									max = 45
								}
							}
							max = {
								value = {
									value = scope:eunuch.age
									add = -5
								}
							}
						}
					}
					random_traits_list = {
		  				count = 1
		  				education_diplomacy_3 = { weight = { base = 30 } }
		  				education_stewardship_3 = { weight = { base = 30 } }
		  				education_intrigue_3 = { weight = { base = 30 } }
		  				education_diplomacy_4 = { weight = { base = 10 } }
		  				education_stewardship_4 = { weight = { base = 10 } }
		  				education_intrigue_4 = { weight = { base = 10 } }
		  			}
		  			random_traits_list = {
						count = 1
						beardless_eunuch = { weight = { base = 90 } }
						eunuch_1 = { weight = { base = 10 } }
					}
					random_traits = yes
					dynasty = none
					save_scope_as = student
				}
			}
		}
		scope:student ?= {
			if = {
				limit = { employer != root }
				set_employer = root
			}
			hidden_effect = { set_relation_mentor = scope:eunuch }
			add_stewardship_skill = 1
			add_intrigue_skill = 1
		}
		scope:story = {
			set_variable = { name = student value = scope:student }
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.3001.a
	}
}

##################################################
# Rival
# by Joe Parkin
# 3010
##################################################

ep3_story_cycle_admin_eunuch.3010 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.3010.t
	desc = ep3_story_cycle_admin_eunuch.3010.desc
	theme = administrative
	override_background = { reference = study }
	left_portrait = {
		character = scope:eunuch
		animation = dismissal
	}
	right_portrait = {
		character = scope:rival
		animation = disapproval
	}
	cooldown = { years = 15 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		NOT = { exists = scope:rival_temp }
		trigger_if = {
			limit = { exists = scope:story_temp.var:eunuch_total }
			scope:story_temp.var:eunuch_total >= 3
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = scope:story_temp.var:eunuch_total
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		ep3_story_cycle_admin_eunuch_save_origin_effect = yes
		random_courtier = {
			limit = {
				is_available = yes
				is_adult = yes
				is_eunuch_trigger = yes
				NOR = {
					scope:eunuch ?= this
					scope:protege ?= this
					scope:student ?= this
					has_relation_friend = scope:eunuch
					has_relation_lover = scope:eunuch
					is_close_family_of = root
					is_close_family_of = scope:eunuch
				}
				trigger_if = {
					limit = { exists = house }
					NOR = {
						house = root.top_liege.house
						house = scope:eunuch.house
					}
				}
			}
			save_scope_as = rival
		}
		if = {
			limit = { NOT = { exists = scope:rival } }
			create_character = {
				culture = scope:origin.culture
				faith = root.faith
				location = root.location
				gender = male
				age = {
					value = {
						add = {
							integer_range = {
								min = 25
								max = 55
							}
						}
					}
				}
				random_traits_list = {
	  				count = 1
	  				education_diplomacy_3 = { weight = { base = 30 } }
	  				education_stewardship_3 = { weight = { base = 30 } }
	  				education_intrigue_3 = { weight = { base = 30 } }
	  				education_diplomacy_4 = { weight = { base = 10 } }
	  				education_stewardship_4 = { weight = { base = 10 } }
	  				education_intrigue_4 = { weight = { base = 10 } }
	  			}
				random_traits_list = {
					count = 1
					beardless_eunuch = { weight = { base = 90 } }
					eunuch_1 = { weight = { base = 10 } }
				}
				random_traits = yes
				dynasty = none
				save_scope_as = rival
			}
		}
		scope:story = {
			set_variable = {
				name = rival
				value = scope:rival
			}
		}
		scope:rival ?= {
			if = {
				limit = { employer != root }
				root = { add_courtier = scope:rival }
			}
			set_relation_rival = {
				target = scope:eunuch
				reason = rival_admin_eunuch
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.3010.a
		show_as_tooltip = {
			scope:rival ?= {
				set_relation_rival = {
					target = scope:eunuch
					reason = rival_admin_eunuch
				}
			}
		}
	}
}

##################################################
# Affair Accusation
# by Joe Parkin
# 4000
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_spouse_cuckolder_trigger = {
	is_adult = yes
	is_attracted_to_gender_of = $SPOUSE$
	NOR = {
		is_close_family_of = $EUNUCH$
		is_close_family_of = root
		is_close_family_of = $SPOUSE$
	}
	trigger_if = {
		limit = { is_married = yes }
		might_cheat_on_every_partner_trigger = yes
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_spouse_accusation_trigger = {
	is_adult = yes
	reverse_opinion = {
		target = $EUNUCH$
		value < 25
	}
	opinion = {
		target = $EUNUCH$
		value < 25
	}
	NOR = {
		has_relation_friend = $EUNUCH$
		has_relation_lover = $EUNUCH$
		any_relation = {
			type = lover
			is_close_family_of = $EUNUCH$
		}
	}
	save_temporary_scope_as = spouse_temp
	OR = {
		any_secret = {
			type = secret_lover
			secret_target != root
		}
		root = {
			OR = {
				any_courtier = {
					ep3_story_cycle_admin_eunuch_spouse_cuckolder_trigger = { EUNUCH = $EUNUCH$ SPOUSE = scope:spouse_temp }
				}
				any_vassal_or_below = {
					ep3_story_cycle_admin_eunuch_spouse_cuckolder_trigger = { EUNUCH = $EUNUCH$ SPOUSE = scope:spouse_temp }
				}
			}
		}
	}
}

ep3_story_cycle_admin_eunuch.4000 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.4000.t
	desc = ep3_story_cycle_admin_eunuch.4000.desc
	theme = administrative
	override_background = { reference = corridor_night }
	left_portrait = {
		character = scope:eunuch
		animation = scheme
	}
	right_portrait = {
		character = root
		animation = eavesdrop
	}
	lower_left_portrait = scope:spouse
	lower_center_portrait = scope:cuckolder
	cooldown = { years = 10 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:story_temp.var:eunuch_total >= 3
		any_spouse = {
			ep3_story_cycle_admin_eunuch_spouse_accusation_trigger = { EUNUCH = scope:eunuch_temp }
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			any_spouse = {
				any_secret = {
					type = secret_lover
					secret_target != root
				}
			}
			add = 1
		}
		modifier = {
			any_spouse = { might_cheat_on_every_partner_trigger = yes }
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			any_spouse = { has_relation_rival = scope:eunuch_temp }
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			any_spouse = {
				ep3_story_cycle_admin_eunuch_spouse_accusation_trigger = { EUNUCH = scope:eunuch_temp }
				has_trait = lustful
			}
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		play_music_cue = "mx_cue_secret"
		random_spouse = {
			limit = {
				ep3_story_cycle_admin_eunuch_spouse_accusation_trigger = { EUNUCH = scope:eunuch }
			}
			weight = {
				base = 1
				modifier = {
					has_trait = lustful
					add = 1
				}
				modifier = {
					any_secret = { type = secret_lover }
					add = 1
				}
				modifier = {
					has_relation_rival = scope:eunuch
					add = 1
				}
			}
			save_scope_as = spouse
			if = {
				limit = {
					any_secret = {
						type = secret_lover
						secret_target != root
					}
				}
				random_secret = {
					type = secret_lover
					limit = {
						secret_target != root
					}
					save_scope_as = secret
					hidden_effect = { reveal_to = scope:eunuch }
				}
			}
			else = {
				root = {
					every_courtier = {
						limit = {
							ep3_story_cycle_admin_eunuch_spouse_cuckolder_trigger = { EUNUCH = scope:eunuch SPOUSE = scope:spouse }
						}
						add_to_list = potential_cuckolders
					}
					every_vassal_or_below = {
						limit = {
							ep3_story_cycle_admin_eunuch_spouse_cuckolder_trigger = { EUNUCH = scope:eunuch SPOUSE = scope:spouse }
						}
						add_to_list = potential_cuckolders
					}
				}
				random_in_list = {
					list = potential_cuckolders
					weight = {
						base = 1
						is_of_minor_interest_to_weight_up_modifier = { CHARACTER = root }
						is_of_major_interest_to_weight_up_modifier = { CHARACTER = root }
					}
					save_scope_as = cuckolder
				}
			}
		}
		ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:spouse }
		ep3_story_cycle_admin_eunuch_upset_courtier_effect = { COURTIER = scope:cuckolder }
	}

	option = { # Discover
		name = ep3_story_cycle_admin_eunuch.4000.a
		trigger = {
			NOT = {
				any_known_secret = {
					secret_owner = scope:spouse
					secret_type = secret_lover
					secret_target != root
				}
			}
		}
		duel = {
			target = scope:eunuch
			skill = intrigue
			10 = {
				show_chance = no
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = ep3_story_cycle_admin_eunuch.4000.success
				custom_tooltip = ep3_story_cycle_admin_eunuch.4000.success.tt
				hidden_effect = {
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = ep3_story_cycle_admin_eunuch.4000.success
						left_icon = scope:eunuch
						right_icon = scope:spouse
						if = {
							limit = { exists = scope:secret }
							scope:secret ?= { reveal_to = root }
						}
						else = { custom_tooltip = ep3_story_cycle_admin_eunuch.4000.innocent }
					}
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				show_chance = no
				desc = ep3_story_cycle_admin_eunuch.4000.failure
				hidden_effect = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = ep3_story_cycle_admin_eunuch.4000.failure
						left_icon = scope:eunuch
						right_icon = scope:spouse
						custom_tooltip = ep3_story_cycle_admin_eunuch.4010.failure.tt
					}
				}
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:cuckolder
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.4000.b
		trigger = {
			any_known_secret = {
				secret_owner = scope:spouse
				secret_type = secret_lover
				secret_target != root
			}
		}
		reason = ep3_story_cycle_admin_eunuch.4000.b.reason
		stress_impact = {
			vengeful = minor_stress_impact_gain
			wrathful = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:spouse
				multiplier = 0.25
			}
		}
	}

	option = { # Imprison
		name = ep3_story_cycle_admin_eunuch.4000.c
		add_tyranny = medium_tyranny_gain
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:spouse
			IMPRISONER = root
		}
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:cuckolder
			IMPRISONER = root
		}
		stress_impact = {
			forgiving = minor_stress_impact_gain
			calm = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:spouse
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_vengefulness = 0.5
				ai_boldness = 0.5
			}
		}
	}
}

##################################################
# Seduction
# by Joe Parkin
# 4010
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_cheating_spouse_trigger = {
	is_ai = yes
	might_cheat_on_every_partner_trigger = yes
	trigger_if = {
		limit = { is_male = yes }
		NOT = {
			root.faith = { has_doctrine_parameter = adultery_male_accepted }
		}
	}
	trigger_else = {
		NOT = {
			root.faith = { has_doctrine_parameter = adultery_female_accepted }
		}
	}
	opinion = {
		target = root
		value < 25
	}
}

scripted_trigger ep3_story_cycle_admin_eunuch_seducer_trigger = {
	is_attracted_to_gender_of = $SPOUSE$
	is_eunuch_trigger = no
	NOR = {
		has_trait = celibate
		is_close_family_of = $SPOUSE$
		has_relation_rival = $SPOUSE$
	}
	save_temporary_scope_as = seducer_temp
}

ep3_story_cycle_admin_eunuch.4010 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.4010.t
	desc = ep3_story_cycle_admin_eunuch.4010.desc
	theme = administrative
	override_background = { reference = bedchamber }
	cooldown = { years = 15 }
	left_portrait = {
		character = root
		animation = stress
	}
	lower_left_portrait = scope:seducer
	lower_center_portrait = scope:spouse
	lower_right_portrait = scope:eunuch

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		scope:story_temp.var:eunuch_total >= 3
		scope:eunuch_temp = {
			opinion = {
				target = root
				value < 25
			}
		}
		any_spouse = {
			ep3_story_cycle_admin_eunuch_cheating_spouse_trigger = yes
			save_temporary_scope_as = spouse_temp
			scope:eunuch_temp = {
				any_close_family_member = {
					ep3_story_cycle_admin_eunuch_seducer_trigger = { SPOUSE = scope:spouse_temp }
				}
			}
			is_attracted_to_gender_of = scope:seducer_temp
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			is_ai = yes
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			scope:eunuch_temp ?= { has_relation_rival = root }
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		play_music_cue = "mx_cue_secret"
		random_spouse = {
			limit = {
				ep3_story_cycle_admin_eunuch_cheating_spouse_trigger = yes
				save_temporary_scope_as = spouse_temp
				scope:eunuch = {
					any_close_family_member = {
						ep3_story_cycle_admin_eunuch_seducer_trigger = { SPOUSE = scope:spouse_temp }
					}
				}
				is_attracted_to_gender_of = scope:seducer_temp
			}
			save_scope_as = spouse
			scope:eunuch = {
				ordered_close_family_member = {
					limit = {
						ep3_story_cycle_admin_eunuch_seducer_trigger = { SPOUSE = scope:spouse }
						scope:spouse = { is_attracted_to_gender_of = scope:seducer_temp }
					}
					order_by = attraction
					save_scope_as = seducer
				}
			}
		}
		scope:story = {
			set_variable = { name = spouse value = scope:spouse }
			set_variable = { name = seducer value = scope:seducer }
		}
		hidden_effect = {
			random = {
				chance = 50
				scope:seducer ?= {
					set_relation_lover = scope:spouse
					had_sex_with_effect = {
						CHARACTER = scope:spouse
						PREGNANCY_CHANCE = seduce_pregnancy_chance
					}
					random_secret = {
						type = secret_lover
						limit = {
							secret_target = scope:spouse
						}
						save_scope_as = secret
					}
				}
				scope:eunuch = {
					if = {
						limit = {
							OR = {
								can_start_scheme = { type = murder target_character = root }
								is_scheming_against = { type = murder target = root }
							}
						}
						if = {
							limit = {
								can_start_scheme = { type = murder target_character = root }
							}
							start_scheme = { type = murder target_character = root }
						}
						random_scheme = {
							type = murder
							limit = {
								scheme_target_character = root
							}
							save_scope_as = scheme
						}
						scope:scheme ?= {
							if = {
								limit = { NOT = { scheme_is_character_agent = scope:spouse } }
								add_agent_slot = agent_lookout
								random_scheme_agent_slot = {
									limit = {
										is_filled = no
										save_temporary_scope_as = slot_temp
										scope:spouse = { is_valid_as_agent_in_slot = scope:slot_temp }
									}
									save_scope_as = spouse_slot
								}
								scope:spouse = {
									force_add_to_agent_slot = {
										agent_slot = scope:spouse_slot
										years = 1
									}
								}
							}
							if = {
								limit = { NOT = { scheme_is_character_agent = scope:spouse } }
								add_agent_slot = agent_assassin
								random_scheme_agent_slot = {
									limit = {
										is_filled = no
										save_temporary_scope_as = slot_temp
										scope:seducer = { is_valid_as_agent_in_slot = scope:slot_temp }
									}
									save_scope_as = seducer_slot
								}
								scope:seducer = {
									force_add_to_agent_slot = {
										agent_slot = scope:seducer_slot
										years = 1
									}
								}
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.4010.a
		duel = {
			skill = intrigue
			target = scope:eunuch
			10 = {
				show_chance = no
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = ep3_story_cycle_admin_eunuch.4000.success
				custom_tooltip = ep3_story_cycle_admin_eunuch.4000.success.tt
				hidden_effect = {
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = ep3_story_cycle_admin_eunuch.4000.success
						left_icon = scope:eunuch
						right_icon = scope:spouse
						scope:secret ?= { reveal_to = root }
						scope:scheme ?= {
							expose_scheme = yes
							if = {
								limit = { scheme_is_character_agent = scope:spouse }
								expose_scheme_agent = scope:spouse
							}
							if = {
								limit = { scheme_is_character_agent = scope:seducer }
								expose_scheme_agent = scope:seducer
							}
						}
					}
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				show_chance = no
				desc = ep3_story_cycle_admin_eunuch.4000.failure
				hidden_effect = {
					send_interface_toast = {
						type = event_toast_effect_bad
						title = ep3_story_cycle_admin_eunuch.4000.failure
						left_icon = scope:eunuch
						right_icon = scope:spouse
						custom_tooltip = ep3_story_cycle_admin_eunuch.4010.failure.tt
					}
				}
			}
		}
		stress_impact = {
			trusting = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
	}

	option = { # Imprison
		name = ep3_story_cycle_admin_eunuch.4010.c
		add_tyranny = major_tyranny_gain
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:spouse
			IMPRISONER = root
		}
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:seducer
			IMPRISONER = root
		}
		rightfully_imprison_character_less_verbose_effect = {
			TARGET = scope:eunuch
			IMPRISONER = root
		}
		stress_impact = {
			forgiving = minor_stress_impact_gain
			calm = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:spouse
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_vengefulness = 0.5
				ai_boldness = 0.5
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.4010.d
		add_prestige = medium_prestige_loss
		stress_impact = {
			vengeful = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
			wrathful = minor_stress_impact_gain
			just = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:spouse
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_vengefulness = -0.25
				ai_boldness = -0.5
				ai_honor = -0.25
			}
		}
	}
}

##################################################
# Upset Courtiers
# by Joe Parkin
# 5000
##################################################

ep3_story_cycle_admin_eunuch.5000 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.5000.t
	desc = ep3_story_cycle_admin_eunuch.5000.desc
	theme = administrative
	override_background = { reference = study }
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:whinger
		animation = debating
	}
	lower_center_portrait = scope:eunuch
	cooldown = { years = 2 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		scope:story_temp ?= {
			var:eunuch_score >= 2
			any_in_list = {
				variable = upset_courtiers
				count >= 2
			}
			any_in_list = {
				variable = upset_courtiers
				is_available = yes
				top_liege ?= root.top_liege
				NOT = { has_character_flag = admin_rival_whinger_cooldown }
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			scope:story_temp ?= {
				any_in_list = {
					variable = upset_courtiers
					count >= 4
				}
			}
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			scope:story_temp ?= {
				any_in_list = {
					variable = upset_courtiers
					count >= 6
				}
			}
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:story = {
			every_in_list = {
				variable = upset_courtiers
				limit = {
					is_available = yes
					top_liege ?= root.top_liege
					NOT = { has_character_flag = admin_rival_whinger_cooldown }
				}
				add_to_list = upset_courtiers
			}
		}
		ordered_in_list = {
			list = upset_courtiers
			limit = {
				is_available = yes
				top_liege ?= root.top_liege
				NOT = { has_character_flag = admin_rival_whinger_cooldown }
			}
			order_by = highest_held_title_tier
			save_scope_as = whinger
			add_character_flag = { flag = admin_rival_whinger_cooldown years = 5 }
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.5000.a
		reverse_add_opinion = {
			target = scope:whinger
			modifier = angry_opinion
			opinion = -15
		}
		stress_impact = {
			diligent = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = -0.25
				ai_boldness = -0.25
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.5000.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		reverse_add_opinion = {
			target = scope:whinger
			modifier = pleased_opinion
			opinion = 15
		}
		stress_impact = {
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:whinger
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
	}
}

##################################################
# Upset Family
# by Joe Parkin
# 5010
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_rival_family_trigger = {
	age >= 8
	top_liege = root.top_liege
	NOR = {
		has_trait = fickle
		has_trait = humble
		has_trait = content
		has_trait = trusting
		has_any_scripted_relation = $EUNUCH$
		this = $EUNUCH$
		has_character_flag = admin_rival_whinger_cooldown
	}
}

ep3_story_cycle_admin_eunuch.5010 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.5010.t
	desc = ep3_story_cycle_admin_eunuch.5010.desc
	theme = administrative
	left_portrait = {
		character = scope:rival
		animation = dismissal
	}
	right_portrait = {
		character = scope:eunuch
		animation = debating
	}
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		scope:story_temp.var:eunuch_total >= 3
		OR = {
			any_close_family_member = {
				ep3_story_cycle_admin_eunuch_rival_family_trigger = { EUNUCH = scope:eunuch_temp }
			}
			any_spouse = {
				ep3_story_cycle_admin_eunuch_rival_family_trigger = { EUNUCH = scope:eunuch_temp }
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			has_active_diarchy = yes
			add = 1
		}
		modifier = {
			exists = player_heir
			primary_title.current_heir != player_heir
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			exists = primary_title.current_heir
			scope:eunuch_temp ?= { is_close_family_of = root.primary_title.current_heir }
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			scope:story_temp ?= {
				any_in_list = {
					variable = upset_courtiers
					OR = {
						is_close_family_of = root
						is_spouse_of = root
					}
				}
			}
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		every_close_family_member = {
			limit = {
				ep3_story_cycle_admin_eunuch_rival_family_trigger = { EUNUCH = scope:eunuch }
			}
			add_to_list = potential_rivals
		}
		every_spouse = {
			limit = {
				ep3_story_cycle_admin_eunuch_rival_family_trigger = { EUNUCH = scope:eunuch }
			}
			add_to_list = potential_rivals
		}
		random_in_list = {
			list = potential_rivals
			weight = {
				base = 1
				modifier = {
					this = root.player_heir
					add = 1
				}
			}
			save_scope_as = rival
			set_relation_rival = { reason = rival_admin_eunuch_family target = scope:eunuch }
			add_character_flag = {
				flag = admin_rival_whinger_cooldown
				years = 6
			}
			scope:story = {
				add_to_variable_list = {
					name = upset_courtiers
					target = scope:rival
				}
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.5010.a
		reverse_add_opinion = {
			target = scope:rival
			modifier = angry_opinion
			opinion = -15
		}
		stress_impact = {
			diligent = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_energy = -0.25
				ai_boldness = -0.25
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.5010.b
		ep3_story_cycle_admin_eunuch_downgrade_effect = yes
		reverse_add_opinion = {
			target = scope:rival
			modifier = pleased_opinion
			opinion = 15
		}
		stress_impact = {
			lazy = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:rival
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_energy = 0.25
				ai_boldness = 0.25
			}
		}
	}
}

##################################################
# Puppet Heir
# by Joe Parkin
# 5020
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_puppet_heir_trigger = {
	this != root.primary_title.current_heir
	age >= 8
	top_liege = root.top_liege
	is_playable_character = no
	OR = {
		is_adult = no
		has_trait = intellect_bad
		has_trait = incapable
		has_relation_friend = $EUNUCH$
		NAND = {
			has_trait = ambitious
			has_trait = diligent
			has_trait = paranoid
		}
	}
	sum_of_all_skills_value < root.primary_title.current_heir.sum_of_all_skills_value
	save_temporary_scope_as = puppet_temp
}

ep3_story_cycle_admin_eunuch.5020 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.5020.t
	desc = ep3_story_cycle_admin_eunuch.5020.desc
	theme = administrative
	left_portrait = {
		character = scope:puppet
		animation = happiness
	}
	right_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	override_background = { reference = garden }
	cooldown = { years = 5 }

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		trigger_if = {
			limit = { exists = scope:story_temp.var:eunuch_total }
			scope:story_temp.var:eunuch_total >= 3
		}
		NOR = {
			exists = scope:protege_temp
			exists = scope:puppet_temp
		}
		any_close_family_member = {
			ep3_story_cycle_admin_eunuch_puppet_heir_trigger = { EUNUCH = scope:eunuch_temp }
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			has_active_diarchy = yes
			add = 1
		}
		modifier = {
			player_heir ?= primary_title.current_heir
			add = 1
		}
		modifier = {
			player_heir ?= { is_adult = no }
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		primary_title.current_heir = { save_scope_as = current_heir }
		random_close_family_member = {
			limit = {
				ep3_story_cycle_admin_eunuch_puppet_heir_trigger = { EUNUCH = scope:eunuch }
			}
			save_scope_as = puppet
			set_relation_friend = { reason = friend_admin_eunuch_puppet target = scope:eunuch }
		}
		scope:story = { set_variable = { name = puppet value = scope:puppet } }
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.5020.a
		scope:puppet = {
			add_character_modifier = {
				modifier = admin_eunuch_puppet_modifier
				years = 10
			}
		}
		primary_title = {
			change_appointment_investment = {
				target = scope:puppet
				value = appointment_score_major_value
			}
		}
	}
}

##################################################
# Coup
# by Joe Parkin
# 6000
##################################################

scripted_trigger ep3_story_cycle_admin_eunuch_coup_trigger = {
	opinion = {
		target = root
		value < 50
	}
	NOR = {
		has_relation_friend = root
		is_spouse_of = root
		has_trait = eunuch_1
		has_trait = beardless_eunuch
		has_trait = blind
	}
	save_temporary_scope_as = coupist_temp
}

scripted_effect ep3_story_cycle_admin_eunuch_coup_effect = {
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = yes
	}
	primary_title = {
		change_title_holder = {
			holder = scope:coupist
			change = scope:title_change
		}
	}
	hidden_effect = {
		every_held_title = {
			title_tier >= county
			limit = {
				is_noble_family_title = no
				is_landless_type_title = no
				this != scope:emperor.primary_title
			}
			change_title_holder = {
				holder = scope:coupist
				change = scope:title_change
			}
		}
	}
	resolve_title_and_vassal_change = scope:title_change
}

ep3_story_cycle_admin_eunuch.6000 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.6000.t
	desc = {
		desc = ep3_story_cycle_admin_eunuch.6000.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:coupist ?= { is_close_family_of = root } }
				desc = ep3_story_cycle_admin_eunuch.6000.family
			}
		}
		desc = ep3_story_cycle_admin_eunuch.6000.body
		first_valid = {
			triggered_desc = {
				trigger = { scope:coupist.house ?= scope:emperor.house }
				desc = ep3_story_cycle_admin_eunuch.6000.family
			}
			triggered_desc = {
				trigger = {
					has_diarchy_type = co_emperorship
					has_active_diarchy = yes
					scope:coupist ?= diarch
				}
				desc = ep3_story_cycle_admin_eunuch.6000.co_caesar
			}
			triggered_desc = {
				trigger = { scope:coupist.house ?= scope:eunuch.house }
				desc = ep3_story_cycle_admin_eunuch.6000.eunuch_family
			}
			triggered_desc = {
				trigger = {
					has_diarchy_type = regency
					has_active_diarchy = yes
					scope:coupist ?= diarch
				}
				desc = ep3_story_cycle_admin_eunuch.6000.regent
			}
			desc = ep3_story_cycle_admin_eunuch.6000.fallback
		}
	}
	theme = administrative
	left_portrait = {
		character = root
		animation = shock
	}
	right_portrait = {
		character = scope:eunuch
		animation = war_over_win
	}
	lower_center_portrait = scope:coupist
	lower_right_portrait = scope:bodyguard

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
		is_at_war = no
		trigger_if = {
			limit = { exists = scope:story_temp.var:eunuch_total }
			scope:story_temp.var:eunuch_total >= 5
		}
		scope:eunuch_temp ?= {
			opinion = {
				target = root
				value < 25
			}
			OR = {
				has_relation_rival = root
				has_trait = ambitious
				opinion = {
					target = root
					value <= -50
				}
				scope:story_temp.var:eunuch_refusals >= 3
			}
			NOR = {
				has_relation_friend = root
				is_spouse_of = root
			}
		}
		OR = {
			scope:puppet_temp ?= { ep3_story_cycle_admin_eunuch_coup_trigger = yes }
			scope:seducer_temp ?= { ep3_story_cycle_admin_eunuch_coup_trigger = yes }
			scope:protege_temp ?= { ep3_story_cycle_admin_eunuch_coup_trigger = yes }
		}
		OR = {
			any_targeting_faction = {
				faction_type = claimant_faction
				special_character = scope:coupist_temp
			}
			any_vassal = {
				is_landed = yes
				can_create_faction = {
					type = claimant_faction
					target = scope:emperor_temp
				}
				NOR = {
					is_allied_to = scope:emperor_temp
					is_close_family_of = scope:emperor_temp
					is_spouse_of = scope:emperor_temp
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
			add = {
				value = scope:story_temp.var:eunuch_score
				min = 0
			}
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			OR = {
				diarch ?= scope:puppet_temp
				diarch ?= scope:seducer_temp
				diarch ?= scope:protege_temp
			}
			has_active_diarchy = yes
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			OR = {
				diarch ?= scope:puppet_temp
				diarch ?= scope:seducer_temp
				diarch ?= scope:protege_temp
			}
			add = 1
		}
		modifier = {
			ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
			scope:eunuch_temp ?= {
				OR = {
					has_trait = schemer
					has_trait = education_intrigue
					has_council_position = councillor_spymaster
					has_court_position = chief_eunuch_court_position
				}
			}
			add = 1
		}
		modifier = {
			any_targeting_faction = { count > 1 }
			add = 1
		}
		modifier = {
			age >= 50
			add = 1
		}
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		hidden_effect = {
			random_list = {
				10 = {
					trigger = {
						scope:puppet ?= { ep3_story_cycle_admin_eunuch_coup_trigger = yes }
					}
					modifier = { add = scope:puppet.sum_of_all_skills_value }
					scope:puppet = { save_scope_as = coupist }
				}
				10 = {
					trigger = {
						scope:seducer ?= { ep3_story_cycle_admin_eunuch_coup_trigger = yes }
					}
					modifier = { add = scope:seducer.sum_of_all_skills_value }
					scope:seducer = { save_scope_as = coupist }
				}
				10 = {
					trigger = {
						scope:protege ?= { ep3_story_cycle_admin_eunuch_coup_trigger = yes }
					}
					modifier = { add = scope:protege.sum_of_all_skills_value }
					scope:protege = { save_scope_as = coupist }
				}
			}
		}
		scope:coupist = {
			if = {
				limit = {
					NOT = { has_claim_on = root.primary_title }
				}
				add_unpressed_claim = root.primary_title
			}
		}
		random_court_position_holder = {
			limit = {
				has_court_position = bodyguard_court_position
				is_available = yes
				NOR = {
					this = scope:coupist
					this = scope:eunuch
				}
			}
			save_scope_as = bodyguard
		}
		random_targeting_faction = {
			faction_type = claimant_faction
			limit = {
				special_character = scope:coupist
			}
			save_scope_as = faction
		}
		if = {
			limit = {
				NOT = { exists = scope:faction }
			}
			scope:story = {
				ordered_in_list = {
					variable = eunuch_allies
					limit = {
						can_create_faction = {
							type = claimant_faction
							target = scope:emperor
						}
					}
					order_by = current_military_strength
					save_scope_as = faction_starter
				}
			}
			if = {
				limit = { NOT = { exists = scope:faction_starter } }
				ordered_vassal = {
					limit = {
						is_landed = yes
						can_create_faction = {
							type = claimant_faction
							target = scope:emperor
						}
						NOR = {
							has_relation_rival = scope:emperor
							is_allied_to = scope:emperor
							is_close_family_of = scope:emperor
						}
					}
					save_scope_as = faction_starter
				}
			}
			scope:faction_starter ?= {
				create_faction = {
					type = claimant_faction
					target = scope:emperor
				}
				hidden_effect = {
					joined_faction ?= {
						save_scope_as = faction
						set_special_character = scope:coupist
						set_special_title = root.primary_title
					}
				}
			}
		}
		scope:eunuch ?= {
			if = {
				limit = {
					NOT = {
						scope:faction = { any_faction_member = { this = scope:eunuch } }
					}
					can_join_faction = scope:faction
				}
				join_faction = scope:faction
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.6000.a
		duel = {
			skills = { diplomacy intrigue }
			value = decent_skill_rating
			15 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 1
				}
				modifier = {
					scope:bodyguard ?= {
						opinion = {
							target = scope:emperor
							value > 50
						}
					}
					add = 5
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_1_modifier
					add = 2
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_2_modifier
					add = 4
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_3_modifier
					add = 6
				}
				min = 1
				desc = ep3_story_cycle_admin_eunuch.6000.a.success
				send_interface_toast = {
					type = event_toast_effect_good
					title = ep3_story_cycle_admin_eunuch.6000.a.success
					left_icon = scope:eunuch
					right_icon = scope:coupist
					rightfully_imprison_character_less_verbose_effect = {
						TARGET = scope:eunuch
						IMPRISONER = scope:emperor
					}
					reverse_add_opinion = {
						target = scope:coupist
						modifier = angry_opinion
						opinion = -25
					}
				}
				scope:story = {
					if = {
						limit = {
							any_in_list = {
								variable = upset_courtiers
								is_alive = yes
								top_liege = root
								NOT = { scope:coupist ?= this }
							}
						}
						every_in_list = {
							variable = upset_courtiers
							limit = {
								is_alive = yes
								top_liege = root
								NOT = { scope:coupist ?= this }
							}
							add_opinion = {
								target = root
								modifier = pleased_opinion
								opinion = 20
							}
						}
					}
					end_story = yes
				}
			}
			1 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -1
				}
				modifier = {
					scope:bodyguard ?= {
						opinion = {
							target = scope:emperor
							value < -50
						}
					}
					add = 5
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_4_modifier
					add = 2
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_5_modifier
					add = 4
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_6_modifier
					add = 6
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_7_modifier
					add = 8
				}
				modifier = {
					has_character_modifier = admin_eunuch_liege_8_modifier
					add = 10
				}
				min = 1
				desc = ep3_story_cycle_admin_eunuch.6000.a.failure
				send_interface_toast = {
					type = event_toast_effect_bad
					title = ep3_story_cycle_admin_eunuch.6000.a.failure
					left_icon = scope:eunuch
					right_icon = scope:coupist
					ep3_story_cycle_admin_eunuch_coup_effect = yes
					rightfully_imprison_character_less_verbose_effect = {
						TARGET = scope:emperor
						IMPRISONER = scope:coupist
					}
				}
			}
		}
		stress_impact = {
			calm = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
			humble = minor_stress_impact_gain
			patient = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_boldness = 1
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.6000.c
		add_legitimacy = major_legitimacy_loss
		every_targeting_faction = { add_faction_discontent = 25 }
		stress_impact = {
			wrathful = minor_stress_impact_gain
			vengeful = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
			impatient = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_rationality = 0.5
				ai_boldness = -0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.6000.d
		ep3_story_cycle_admin_eunuch_coup_effect = yes
		stress_impact = {
			wrathful = medium_stress_impact_gain
			vengeful = medium_stress_impact_gain
			paranoid = medium_stress_impact_gain
			impatient = medium_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_boldness = -1
			}
		}
	}
}

##################################################
# Accession
# by Joe Parkin
# 8000
##################################################

scripted_trigger ep3_admin_story_cycle_valid_switch_character_trigger = {
	is_alive = yes
	employer = root
	is_playable_character = no
	trigger_if = {
		limit = { is_imprisoned = yes }
		is_imprisoned_by = root
	}
}

ep3_story_cycle_admin_eunuch.8000 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.8000.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { has_title = title:e_byzantium }
				desc = ep3_story_cycle_admin_eunuch.8000.intro_byz
			}
			desc = ep3_story_cycle_admin_eunuch.8000.intro
		}
		first_valid = {
			triggered_desc = {
				trigger = { is_close_family_of = scope:previous_story_owner }
				desc = ep3_story_cycle_admin_eunuch.8000.intro_family
			}
			triggered_desc = {
				trigger = {
					OR = {
						scope:previous_story_owner.killer ?= root
						primary_title = {
							recent_history = { type = conquest days = 5 }
						}
					}
				}
				desc = ep3_story_cycle_admin_eunuch.8000.intro_enemy
			}
			desc = ep3_story_cycle_admin_eunuch.8000.intro_fallback
		}
		desc = ep3_story_cycle_admin_eunuch.8000.desc
	}
	theme = administrative
	left_portrait = {
		character = root
		animation = war_over_tie
	}
	right_portrait = {
		character = scope:eunuch
		animation = admiration
	}
	lower_center_portrait = scope:previous_story_owner

	trigger = {
		ep3_story_cycle_admin_eunuch_valid_story_trigger = yes
		ep3_story_cycle_admin_eunuch_valid_event_trigger = yes
	}

	immediate = {
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch ?= {
			if = {
				limit = {
					is_alive = yes
					is_playable_character = no
					employer != root
				}
				set_employer = root
			}
		}
		scope:student ?= {
			if = {
				limit = {
					is_alive = yes
					is_playable_character = no
					employer != root
				}
				set_employer = root
			}
		}
		scope:rival ?= {
			if = {
				limit = {
					is_alive = yes
					is_playable_character = no
					employer != root
				}
				set_employer = root
			}
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.8000.a
		trigger = {
			OR = {
				scope:rival ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
				scope:student ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
				scope:eunuch ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
			}
		}
		flavor = admin_eunuch_flavor_tt
		change_influence = medium_influence_loss
		ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = liege }
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
			move_to_pool = yes
		}
		scope:protege ?= {
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -20
			}
		}
		scope:student ?= {
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -20
			}
		}
		scope:rival ?= {
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 20
			}
		}
		scope:story = {
			if = {
				limit = {
					any_in_list = {
						variable = upset_courtiers
						is_alive = yes
						top_liege = root
						NOR = {
							scope:protege ?= this
							scope:student ?= this
							scope:rival ?= this
						}
					}
				}
				every_in_list = {
					variable = upset_courtiers
					limit = {
						is_alive = yes
						top_liege = root
						NOR = {
							scope:protege ?= this
							scope:student ?= this
							scope:rival ?= this
						}
					}
					add_opinion = {
						target = root
						modifier = pleased_opinion
						opinion = 20
					}
				}
			}
			end_story = yes
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.8000.b
		trigger = {
			scope:student ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:student = {
			if = {
				limit = { employer != root }
				set_employer = root
			} 
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = scope:eunuch
			FIRST_TYPE = eunuch
			SECOND = scope:student
			SECOND_TYPE = eunuch
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = scope:eunuch
			FIRST_TYPE = eunuch
			SECOND = root
			SECOND_TYPE = liege
		}
		scope:eunuch ?= {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
			add_opinion = {
				modifier = angry_opinion
				target = root
				opinion = -25
			}
		}
		scope:protege ?= {
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -25
			}
		}
		scope:story = {
			set_variable = { name = eunuch value = scope:student }
			remove_variable = student
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.8000.c
		trigger = {
			scope:rival ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:rival = {
			if = {
				limit = { employer != root }
				set_employer = root
			}
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = scope:eunuch
			FIRST_TYPE = eunuch
			SECOND = scope:rival
			SECOND_TYPE = eunuch
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = scope:eunuch
			FIRST_TYPE = eunuch
			SECOND = root
			SECOND_TYPE = liege
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
			add_opinion = {
				modifier = angry_opinion
				target = root
				opinion = -25
			}
		}
		scope:protege ?= {
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -25
			}
		}
		scope:story = {
			set_variable = { name = eunuch value = scope:rival }
			remove_variable = rival
		}
	}

	option = {
		name = ep3_story_cycle_admin_eunuch.8000.d
		trigger = {
			scope:eunuch ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:eunuch = {
			if = {
				limit = { employer != root }
				set_employer = root
			}
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = scope:eunuch
			FIRST_TYPE = eunuch
			SECOND = root
			SECOND_TYPE = liege
		}
		scope:rival ?= {
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -25
			}
		}
		scope:story = {
			every_in_list = {
				list = upset_courtiers
				limit = { top_liege = root.top_liege }
				add_opinion = {
					target = root
					modifier = annoyed_opinion
					opinion = -15
				}
			}
		}
	}
}

##################################################
# Eunuch Dead
# by Joe Parkin
# 8010
##################################################

ep3_story_cycle_admin_eunuch.8010 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.8010.t
	desc = ep3_story_cycle_admin_eunuch.8010.desc
	theme = administrative
	override_background = { reference = ce1_catacombs }
	left_portrait = {
		character = scope:eunuch
		animation = dead
	}
	right_portrait = {
		character = scope:protege
		animation = grief
		trigger = { is_alive = yes }
	}
	lower_right_portrait = {
		character = scope:student
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	lower_center_portrait = {
		character = scope:rival
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	trigger = {
		any_owned_story = {
			type = story_cycle_admin_eunuch
			var:eunuch ?= { is_alive = no }
		}
	}
	immediate = {
		play_music_cue = mx_cue_death
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.b
		trigger = {
			scope:student ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:student }
			remove_variable = student
		}
		scope:student = {
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:student
			SECOND_TYPE = eunuch
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.c
		trigger = {
			scope:rival ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:rival }
			remove_variable = rival
		}
		scope:rival = {
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:rival
			SECOND_TYPE = eunuch
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8010.d
		ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = liege }
		scope:story = { end_story = yes }
	}
}

##################################################
# Eunuch Jailed
# by Joe Parkin
# 8020
##################################################

ep3_story_cycle_admin_eunuch.8020 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.8020.t
	desc = ep3_story_cycle_admin_eunuch.8020.desc
	theme = administrative
	override_background = { reference = dungeon_scope }
	left_portrait = {
		character = scope:background_dungeon_scope
		animation = war_over_tie
	}
	right_portrait = {
		character = scope:eunuch
		animation = prisonhouse
	}
	lower_right_portrait = {
		character = scope:student
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	lower_center_portrait = {
		character = scope:rival
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	trigger = {
		any_owned_story = {
			type = story_cycle_admin_eunuch
			var:eunuch ?= { is_imprisoned = yes }
		}
	}
	immediate = {
		play_music_cue = "mx_cue_prison"
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch.imprisoner = { save_scope_as = background_dungeon_scope }
	}
	cooldown = { days = 1 }
	option = {
		name = ep3_story_cycle_admin_eunuch.8020.a
		trigger = { scope:background_dungeon_scope = root }
		scope:eunuch = { release_from_prison = yes }
		stress_impact = {
			vengeful = minor_stress_impact_gain
			callous = minor_stress_impact_gain
			arbitrary = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_honor = 0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8020.a
		trigger = { scope:background_dungeon_scope != root }
		scope:eunuch = { save_scope_as = prisoner }
		save_scope_as = payer
		scope:background_dungeon_scope = { save_scope_as = imprisoner }
		pay_short_term_gold = {
			gold = scope:prisoner.ransom_cost_value
			target = scope:imprisoner
		}
		scope:prisoner = { release_from_prison = yes }
		stress_impact = {
			greedy = minor_stress_impact_gain
			callous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_greed = -0.5
				ai_honor = 0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.b
		trigger = {
			scope:student ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:student }
			remove_variable = student
		}
		scope:student = {
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:student
			SECOND_TYPE = eunuch
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		stress_impact = {
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:student
				multiplier = 0.25
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.c
		trigger = {
			scope:rival ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:rival }
			remove_variable = rival
		}
		scope:rival = {
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:rival
			SECOND_TYPE = eunuch
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		stress_impact = {
			paranoid = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:rival
				multiplier = 0.25
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8020.d
		ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = liege }
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		scope:story = { end_story = yes }
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_energy = -0.5
				ai_boldness = -0.5
			}
		}
	}
}

##################################################
# Eunuch Moved
# by Joe Parkin
# 8030
##################################################

ep3_story_cycle_admin_eunuch.8030 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.8030.t
	desc = ep3_story_cycle_admin_eunuch.8030.desc
	theme = administrative
	override_background = { reference = throne_room_scope }
	left_portrait = {
		character = scope:eunuch
		animation = writing
	}
	right_portrait = {
		character = scope:background_throne_room_scope
		animation = idle
		trigger = {
			NOR = {
				this = root
				this = scope:eunuch
			}
		}
	}
	lower_right_portrait = {
		character = scope:student
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	lower_center_portrait = {
		character = scope:rival
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	trigger = {
		any_owned_story = {
			type = story_cycle_admin_eunuch
			exists = var:eunuch.employer
			var:eunuch.employer != story_owner
		}
	}
	immediate = {
		play_music_cue = "mx_cue_negative"
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch.liege = { save_scope_as = background_throne_room_scope }
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.a
		pay_short_term_gold = {
			target = scope:eunuch
			gold = minor_gold_value
		}
		add_courtier = scope:eunuch
		stress_impact = {
			greedy = minor_stress_impact_gain
			callous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			modifier = {
				gold < medium_gold_value
				factor = 0
			}
			opinion_modifier = {
				opinion_target = scope:eunuch
				multiplier = 0.5
			}
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_greed = -0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.b
		trigger = {
			scope:student ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:student }
			remove_variable = student
		}
		scope:student = { 
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:student
			SECOND_TYPE = eunuch
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:student
				multiplier = 0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.c
		trigger = {
			scope:rival ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:rival }
			remove_variable = rival
		}
		scope:rival = { 
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:rival
			SECOND_TYPE = eunuch
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:rival
				multiplier = 0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.d
		ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = liege }
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		scope:story = { end_story = yes }
		ai_chance = {
			base = 25
		}
	}
}

##################################################
# Eunuch Ruler
# by Joe Parkin
# 8040
##################################################

ep3_story_cycle_admin_eunuch.8040 = {
	type = character_event
	title = ep3_story_cycle_admin_eunuch.8040.t
	desc = {
		desc = ep3_story_cycle_admin_eunuch.8040.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:eunuch = { has_government = landless_adventurer_government } }
				desc = ep3_story_cycle_admin_eunuch.8040.laamp
			}
			triggered_desc = {
				trigger = { scope:eunuch.primary_title ?= { is_noble_family_title = yes } }
				desc = ep3_story_cycle_admin_eunuch.8040.noble_family
			}
			desc = ep3_story_cycle_admin_eunuch.8040.ruler
		}
	}
	theme = administrative
	override_background = { reference = throne_room_scope }
	left_portrait = {
		character = scope:eunuch
		animation = war_over_win
	}
	lower_right_portrait = {
		character = scope:student
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	lower_center_portrait = {
		character = scope:rival
		trigger = { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
	}
	trigger = {
		any_owned_story = {
			type = story_cycle_admin_eunuch
			var:eunuch ?= { is_playable_character = yes }
		}
	}
	immediate = {
		play_music_cue = "mx_cue_negative"
		ep3_story_cycle_admin_eunuch_save_scopes_effect = yes
		scope:eunuch = { save_scope_as = background_throne_room_scope }
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.b
		trigger = {
			scope:student ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:student }
			remove_variable = student
		}
		scope:student = { 
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:student
			SECOND_TYPE = eunuch
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:student
				multiplier = 0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8030.c
		trigger = {
			scope:rival ?= { ep3_admin_story_cycle_valid_switch_character_trigger = yes }
		}
		scope:story = { 
			set_variable = { name = eunuch value = scope:rival }
			remove_variable = rival
		}
		scope:rival = { 
			if = {
				limit = { is_imprisoned_by = root }
				release_from_prison = yes
			}
		}
		ep3_story_cycle_admin_eunuch_matching_modifier_effect = {
			FIRST = root
			FIRST_TYPE = liege
			SECOND = scope:rival
			SECOND_TYPE = eunuch
		}
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:rival
				multiplier = 0.5
			}
		}
	}
	option = {
		name = ep3_story_cycle_admin_eunuch.8040.d
		ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = liege }
		scope:eunuch = {
			ep3_story_cycle_admin_eunuch_clear_modifier_effect = { TYPE = eunuch }
		}
		scope:story = { end_story = yes }
		ai_chance = {
			base = 25
		}
	}
}

##################################################
# Maintenance
# by Joe Parkin
# 9000-9999
##################################################

ep3_story_cycle_admin_eunuch.9980 = { # Story cycle inheritance
	scope = landed_title
	hidden = yes
	trigger = { exists = scope:story }
	immediate = {
		# Should be inherited, so transfer to heir
		if = {
			limit = { exists = holder }
			holder = { save_scope_as = new_story_owner }
			scope:story = {
				make_story_owner = scope:new_story_owner
				story_owner = {
					trigger_event = {
						id = ep3_story_cycle_admin_eunuch.8000
						days = 3
					}
				}
			}
		}
		# Or end
		else = { scope:story = { end_story = yes } }
		debug_log_scopes = yes
	}
}
