﻿
namespace = imperial

#Imperial game start up
imperial.9999 = {
	hidden = yes
	type = character_event
	
	immediate = {	
		### EMPIRE SETUP ###
		every_held_title = {
			limit = { tier >= tier_kingdom }
			set_variable = { name = imperial_title value = 1 }
		}
		####Create some patricians####
		#find eligible randoms to be patricians
		every_courtier = {
			limit = {
				has_dynasty = yes
				is_physically_able_adult = yes
				is_ruler = no
				NOT = {
					house = {
						any_house_member = {
							is_alive = yes
							is_ruler = yes
						}
					}	
				}	
				NAND = {
					is_female = yes
					is_married = yes
					patrilinear_marriage = yes
				}
				NAND = {
					is_female = no
					is_married = yes
					matrilinear_marriage = yes
				}
				culture = root.culture
				religion = root.religion
				NAND = {
					root.culture_group = culture_group:byzantine_group
					byzantine_elective_deformed_candidate_trigger = yes
				}
			}
			add_to_list = eligible_patrician
		}
		every_vassal = {
			limit = { highest_held_title_tier > tier_barony }
			every_courtier = {
				limit = {
					has_dynasty = yes
					is_physically_able_adult = yes
					is_ruler = no
					NOT = {
						house = {
							any_house_member = {
								is_alive = yes
								is_ruler = yes
							}
						}	
					}	
					NAND = {
						is_female = yes
						is_married = yes
						patrilinear_marriage = yes
					}
					NAND = {
						is_female = no
						is_married = yes
						matrilinear_marriage = yes
					}
					culture = root.culture
					religion = root.religion
					NAND = {
						root.culture_group = culture_group:byzantine_group
						byzantine_elective_deformed_candidate_trigger = yes
					}
				}
				add_to_list = eligible_patrician
			}
		}
		
		set_variable = { name = new_patricians value = 3 days = 2 }
		while = {
			limit = {
				var:new_patricians > 0
			}
			clear_saved_scope = new_patrician
			#Try and find existing noble first
			random_in_list = {
				list = eligible_patrician
				limit = {
					is_ruler = no
					NOT = {
						house = {
							any_house_member = {
								is_alive = yes
								is_ruler = yes
							}
						}	
					}	
				}
				weight = {
					base = 1						
					modifier = { #has existing parents/children (therefore is likely historical)
						OR = {
							any_parent = { always = yes }
							any_child = { always = yes }
						}
						add = 100
					}
					modifier = { #prioritise eldest in the family
						add = {
							value = age
						}
					}
				}
				save_scope_as = new_patrician
			}
			if = { #If none found, create a random
				limit = { NOT = { scope:new_patrician = { always = yes } } }
				create_character = {
					location = root.capital_province
					template = random_patrician_character
					save_scope_as = new_patrician
				}
			}
			scope:new_patrician = {
				new_patrician_setup_effect = { LIEGE = root PREDECESSOR = scope:new_patrician }
				random_list = {
					30 = {
						add_character_modifier = { modifier = patrician_estate_modifier_1 }	
						add_gold = 5
					}
					40 = {
						add_character_modifier = { modifier = patrician_estate_modifier_2 }
						add_gold = 10
					}
					20 = {
						add_character_modifier = { modifier = patrician_estate_modifier_3 }
						add_gold = 25
					}
					7 = {
						add_character_modifier = { modifier = patrician_estate_modifier_4 }
						add_gold = 40
					}
					3 = {
						add_character_modifier = { modifier = patrician_estate_modifier_5 }
						add_gold = 75
					}
				}
			}		
			change_variable = { name = new_patricians subtract = 1 }
		}
		
		
		##Add military career modifier to existing characters
		add_to_list = potential_military_career_char
		every_courtier = {
			limit = {
				can_be_warrior_trigger = {
					ARMY_OWNER = root					
				}
				age >= 21
			}
			add_to_list = potential_military_career_char
		}
		every_vassal_or_below = {
			if = {
				limit = {
					has_government = military_command
					age >= 16
				}
				add_character_modifier = imperial_military_rank_gained
			}
			else_if = {
				limit = {
					can_be_warrior_trigger = {
						ARMY_OWNER = root					
					}
					age >= 21
				}
				add_to_list = potential_military_career_char
			}	
			every_courtier = {
				limit = {
					can_be_warrior_trigger = {
						ARMY_OWNER = root					
					}
					age >= 21
				}
				add_to_list = potential_military_career_char
			}
		}
		every_in_list = {
			list = potential_military_career_char
			limit = {			
				OR = {
					culture_group = root.culture_group
					religion = root.religion
					root = { accepts_non_faith_commanders_trigger = yes }
				}
				OR = {
					ai_energy > very_high_negative_ai_value
					has_education_martial_trigger = yes
					has_martial_lifestyle_trait_trigger = yes
					martial >= medium_skill_rating
					prowess >= medium_skill_rating
					ai_greed >= very_high_positive_ai_value
					ai_boldness >= very_high_positive_ai_value
					ai_honor >= very_high_positive_ai_value
				}
				NOR = {
					faith = {
						has_doctrine_parameter = pacifist_opinion_active
					}
					has_trait = craven		
				}	
			}
			add_character_modifier = imperial_military_rank_gained
		}
		
	}
}
#Imperial succession maintenance
imperial.0001 = {
	hidden = yes
	type = character_event
	
	trigger = {
		any_held_title = { 
			has_title_law = imperial_elective_succession_law 
			current_heir = {
				NOT = { this = root.capital_county.current_heir }
			}
		}	
	}

	immediate = {
		capital_county.current_heir = { 
			save_scope_as = dynastic_heir
		}
		capital_county = {
			save_scope_as = imperial_capital_county
			de_jure_liege = {
				if = {
					limit = { holder = root }
					save_scope_as = imperial_capital_duchy
				}
				de_jure_liege = {
					if = {
						limit = { holder = root }
						save_scope_as = imperial_capital_kingdom
					}
				}
			}
		}
		primary_title.current_heir = {
			save_scope_as = primary_imperial_heir
		}
	
		create_title_and_vassal_change = change
		scope:change = {
			set_title_and_vassal_change_type = inheritance
		}
		
		#Dynastic heir gains patrician status?		
		if = {
			limit = { 
				OR = {
					is_ai = no
					has_estates_trigger = yes
					scope:dynastic_heir = { has_estates_trigger = yes }
				}
				scope:dynastic_heir = { 
					OR = {
						is_ruler = no
						highest_held_title_tier < tier_county
					}
				} 
			}
			scope:dynastic_heir = { 
				add_character_flag = has_become_patrician
				new_patrician_setup_effect = { LIEGE = root PREDECESSOR = root }
				if = {
					limit = { has_estates_trigger = no }
					add_character_modifier = patrician_estate_modifier_1
				}
			}
		}	
			
		every_held_title = { 
			limit = {
				has_title_law = imperial_elective_succession_law 
				current_heir = {
					NOT = { this = scope:dynastic_heir }
				}
			}	
			save_scope_as = imperial_title_to_check
			current_heir = { save_scope_as = imperial_title_heir }
			
			root = {
				every_held_title = {
					limit = {
						NOT = { has_title_law = imperial_elective_succession_law }
						OR = {
							is_de_jure_liege_or_above_target = scope:imperial_title_to_check
							scope:imperial_title_to_check = root.primary_title
						}
						NOT = { exists = var:protect_title }
						NAND = { 
							holder = { highest_held_title_tier > tier_barony }
							this = root.capital_barony 
						}
					}
					change_title_holder = {
						holder = scope:imperial_title_heir
						change = scope:change
					}
				}
			}		
		}
		
		resolve_title_and_vassal_change = scope:change
		
		scope:dynastic_heir = { 
			trigger_event = { id = imperial.0002 days = 1 }
		}
	}
}
imperial.0002 = {
	hidden = yes
	type = character_event
	
	immediate = { #transfer capital county/duchy/kingdom
		create_title_and_vassal_change = change
		scope:change = {
			set_title_and_vassal_change_type = inheritance
		}
		
		every_held_title = {
			limit = {
				OR = {
					this = scope:imperial_capital_county
					this = scope:imperial_capital_duchy
					this = scope:imperial_capital_kingdom
				}
				NOT = { exists = var:protect_title }
				NAND = { 
					holder = { highest_held_title_tier > tier_barony }
					this = root.capital_barony 
				}
			}
			change_title_holder = {
				holder = scope:primary_imperial_heir
				change = scope:change
			}
		}
		every_vassal = { #transfer vassals not in dejure realm to new emperor
			limit = {
				NOT = {
					primary_title = {
						any_this_title_or_de_jure_above = {
							holder = root
						}
					}
				}
			}
			change_liege = {
				liege = scope:primary_imperial_heir
				change = scope:change
			}
		}
		
		resolve_title_and_vassal_change = scope:change
		
		remove_character_flag = has_become_patrician
	}
}	
#Military succession maintenance
imperial.0003 = {
	hidden = yes
	type = character_event
	
	trigger = {
		has_government = military_command
		is_ruler = yes
		liege = { NOT = { this = root } }
	}

	immediate = {
		if = { #check heir is valid
			limit = {
				OR = {
					NOT = { exists = primary_title.current_heir }
					primary_title = {
						current_heir = {
							NOT = { eligible_for_military_appointment_trigger = { APPOINTEE = root.liege } }
							NOT = { this = root.liege }
						}
					}
				}	
			}
			###TEMP FIX WHILST NOMINATE SUCCESSOR DOES NOT WORK### (PLAYER STILL USES DECISION)
			if = {
				limit = { 
					is_ai = yes 
					prestige >= 100
				}
				house = {
					random_house_member = {
						limit = {
							NOT = { this = root }
							is_alive = yes
							eligible_for_military_appointment_trigger = { APPOINTEE = root.liege }
							OR = {
								is_ruler = no
								target_is_liege_or_above = root
							}
							exists = top_liege
							top_liege = root.top_liege
						}
						weight = {
							base = 0
							
							
							modifier = {
								add = 1000
								is_child_of = root
							}
							modifier = {
								add = 900
								is_grandchild_of = root
							}
							modifier = {
								add = 800
								is_sibling_of = root
							}
							modifier = {
								add = 700
								is_close_family_of = root
							}
							modifier = {
								add = -250
								OR = {
									has_trait = bastard
									has_trait = eunuch
								}
							}
						}
						save_scope_as = military_heir
						root = {
							add_prestige = { subtract = 100 }
							
							pay_long_term_gold = { target = scope:military_heir gold = root.gold }
							inherit_estates_effect = { ESTATE_HEIR = scope:military_heir }
							
							create_title_and_vassal_change = change
							scope:change = {
								set_title_and_vassal_change_type = inheritance
							}
							every_vassal = {
								change_liege = {
									liege = scope:military_heir
									change = scope:change
								}
							}
							every_held_title = {
								limit = { 
									NAND = { 
										holder = { highest_held_title_tier > tier_barony }
										this = root.capital_barony 
									}
								}
								# if = {
									# limit = { NOT = { root.liege = { has_government = military_command } } }
									# remove_title_law = military_appointment_succession_law
								# }	
								change_title_holder = {
									holder = scope:military_heir
									change = scope:change
								}
							}
							resolve_title_and_vassal_change = scope:change
						}	
					}
				}
			}	
			###
			
			if = {
				limit = { NOT = { exists = scope:military_heir } }
			
				save_scope_as = old_commander
				#if not, return all titles to liege
				primary_title = { 
					current_heir = { 
						save_scope_as = root_heir 
					}
					save_scope_as = primary_military_title 
				}
				liege = {
					save_scope_as = the_liege
					send_interface_toast = {
						title = MILITARY_COMMAND_INHERITED_TITLE
						custom_tooltip = MILITARY_COMMAND_INHERITED_DESC
						left_icon = scope:primary_military_title
						right_icon = scope:old_commander
					}
				}
				
				#Pass on estates to root heir
				if = {
					limit = { 
						has_estates_trigger = yes 
						exists = scope:root_heir
					}
					pay_long_term_gold = { target = scope:root_heir gold = root.gold }
					inherit_estates_effect = { ESTATE_HEIR = scope:root_heir }
				}
				#Dynastic heir gains patrician status?			
				if = {
					limit = { 
						exists = scope:root_heir
						scope:root_heir = { has_estates_trigger = yes }
						OR = {
							is_ai = no
							top_liege = { below_patrician_limit_trigger = yes }
						}
						scope:root_heir = { 
							OR = {
								is_ruler = no
								highest_held_title_tier < tier_county
							}
						} 
					}		
					add_character_flag = { flag = has_become_patrician days = 7 }				
					scope:root_heir = { 	
						add_character_flag = { flag = inheriting_patrician_status days = 7 }
						add_character_flag = { flag = has_become_patrician days = 7 }
						new_patrician_setup_effect = { LIEGE = root.top_liege PREDECESSOR = root }
						top_liege = { 
							send_interface_toast = {
								title = NEW_PATRICIAN_TITLE
								custom_tooltip = NEW_PATRICIAN_DESC
								left_icon = scope:new_patrician
							}
						}
					}				
				}
				if = { #Player assumes control of root heir
					limit = { 
						is_ai = no 
						exists = scope:root_heir
						NAND = { #will actually inherit a title normally
							player_heir = scope:root_heir
							any_held_title = {
								current_heir = scope:root_heir
								NOT = { is_landless_patrician_title_trigger = yes }
								exists = var:protect_title
							}
						}
						scope:root_heir = { 
							OR = {
								has_character_flag = has_become_patrician
								highest_held_title_tier >= tier_county
							}	
						}
					}
					set_player_character = scope:root_heir
					scope:root_heir = {
						send_interface_toast = {
							title = PATRICIAN_SUCCESSION_INFORM_TITLE
							custom_tooltip = PATRICIAN_SUCCESSION_INFORM_DESC
							left_icon = root
							right_icon = scope:root_heir
						}
					}	
				}
				if = { #send courtiers to root heir
					limit = { 
						exists = scope:root_heir
						scope:root_heir = { is_ruler = yes }
					}
					every_courtier = {
						limit = { is_ruler = no }
						scope:root_heir = { recruit_courtier = prev }
					}
				}		
				if = {
					limit = { NOT = { has_character_flag = has_become_patrician } }
					every_courtier = { #other courtiers may go to liege
						limit = { 
							is_ruler = no 
							OR = {
								house = scope:the_liege.house
								is_close_or_extended_family_of = scope:the_liege
								is_spouse_of = scope:the_liege
								opinion = { target = scope:the_liege value >= 40 }
								military_appointment_martial_conditions_trigger = yes
							}
						}
						scope:the_liege = { recruit_courtier = prev }
					}
				}	
				
				create_title_and_vassal_change = change
				scope:change = {
					set_title_and_vassal_change_type = returned
				}
				every_held_title = {
					limit = { 
						NOT = { is_landless_patrician_title_trigger = yes }
						NOT = { exists = var:protect_title }
						NAND = { 
							holder = { highest_held_title_tier > tier_barony }
							this = root.capital_barony 
						}
					}
					# if = {
						# limit = { NOT = { root.liege = { has_government = military_command } } }
						# remove_title_law = military_appointment_succession_law
					# }	
					change_title_holder = {
						holder = scope:the_liege
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				
				scope:the_liege = {
					military_faction_title_grant_check_effect = { GRANTEE = scope:the_liege }
				}
			}			
		}	
		else_if = {		
			limit = { exists = primary_title.current_heir }
			#check heirs to sub-titles to make sure they aren't partitioned to ineligible characters
			primary_title = { 
				current_heir = { save_scope_as = root_heir }
			}
			create_title_and_vassal_change = change
			scope:change = {
				set_title_and_vassal_change_type = inheritance
			}
			every_held_title = {
				limit = { 
					this.current_heir = {
						NOT = { eligible_for_military_appointment_trigger = { APPOINTEE = root.liege } }
						NOT = { this = root.liege }
					}
					NAND = { 
						holder = { highest_held_title_tier > tier_barony }
						this = root.capital_barony 
					}
				}	
				change_title_holder = {
					holder = scope:root_heir
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
}
#Vassal government checks
imperial.0004 = {
	hidden = yes
	type = character_event
	
	trigger = {
		has_government = imperial_government
	}

	immediate = {
		every_held_title = { #ensure variables are set
			limit = { tier >= tier_kingdom }
			set_variable = { name = imperial_title value = 1 }
		}
		
		
		every_vassal = { #Foreign vassals default to autonomy
			limit = {
				primary_title.tier >= tier_county
				has_government = feudal_government
				NOT = { vassal_contract_has_flag = imperial_autonomy_full }
				NOT = { culture_group = root.culture_group }	
			}
			vassal_contract_set_obligation_level = { type = imperial_autonomy level = 1 }
		}	
		
		every_vassal_or_below = { #Should be a military command
			limit = {
				primary_title.tier >= tier_county
				can_get_military_command_trigger = yes
				NOT = { has_government = military_command }
				NOT = { has_government = theocracy_government }
				NOT = { has_government = republic_government }
				NOT = { has_government = holy_order_government }
			}
			change_government = military_command
		}	
		every_vassal_or_below = { #Military command shouldnt be a command
			limit = {
				primary_title.tier >= tier_county
				can_get_military_command_trigger = no
				has_government = military_command
			}
			change_government = feudal_government
			vassal_contract_set_obligation_level = { type = imperial_autonomy level = 1 }
		}
	}
}
#Estates succession maintenance
imperial.9001 = {
	hidden = yes
	type = character_event
	
	trigger = {
		has_estates_trigger = yes
	}

	immediate = {
		#check if patrician title should be destroyed
		every_held_title = { 
			limit = { 
				is_landless_patrician_title_trigger = yes
				OR = {
					NOT = { #no relative is heir
						current_heir = {
							OR = {
								house = root.house
								is_close_or_extended_family_of = root
							}
						}
					}
					holder = { is_independent_ruler = yes } #lost liege
					holder = { #no longer in imperial government
						NOT = {
							any_liege_or_above = {
								has_government = imperial_government
							}
						}
					}	
				}	
			}
			holder = { destroy_title = prev }
		}
		#Can only retain estates within appropriate governments
		if = {
			limit = {
				OR = {
					has_government = imperial_government
					has_government = patrician_government
					has_government = military_command
					any_liege_or_above = {
						OR = {
							has_government = imperial_government
							has_government = patrician_government
							has_government = military_command
						}
					}
				}
			}
			if = {
				limit = { 
					has_government = patrician_government
					primary_title = {
						is_landless_patrician_title_trigger = yes
						current_heir = { 
							OR = {
								is_close_family_of = root
								house = root.house
							}
						}
					}
				}
				primary_title = { current_heir = { save_scope_as = estates_heir } }
			}
			else_if = {
				limit = { 
					player_heir = { 
						OR = {
							is_close_family_of = root
							house = root.house
						}
					} 
				}
				player_heir = { save_scope_as = estates_heir }
			}
			else = {
				random_close_or_extended_family_member = {
					limit = {
						is_alive = yes
						top_liege = root.top_liege
						NOR = {
							has_trait = disinherited
							has_trait = devoted
							has_trait = order_member
						}
					}
					weight = {
						base = 0
						
						
						modifier = {
							add = 1000
							is_child_of = root
						}
						modifier = {
							add = 900
							is_grandchild_of = root
						}
						modifier = {
							add = 800
							is_sibling_of = root
						}
						modifier = {
							add = 700
							is_close_family_of = root
						}
						
						modifier = {
							add = 150
							house = root.house
						}
						modifier = {
							add = -250
							OR = {
								has_trait = bastard
								has_trait = eunuch
							}
						}
						modifier = {
							add = -150
							trigger_if = {
								limit = {
									liege = {
										OR = {
											has_realm_law = male_only_law
											has_realm_law = male_preference_law
										}
									}	
								}
								is_female = yes
							}
							trigger_else_if = {
								limit = {
									liege = {
										OR = {
											has_realm_law = female_only_law
											has_realm_law = female_preference_law
										}
									}	
								}
								is_female = no
							}
							trigger_else = {
								always = no
							}
						}
					}
					save_scope_as = estates_heir
				}
			}
			inherit_estates_effect = { ESTATE_HEIR = scope:estates_heir }
		}		
	}
}
##################################################
# Co-emperor named
##################################################
imperial.0005 = {
	type = character_event
	title = imperial.0005.t
	desc = imperial.0005.desc
	theme = realm
	left_portrait = {
		character = scope:new_co_emperor
		animation = personality_honorable
	}

	immediate = {
		save_scope_as = the_emperor
		capital_barony = { save_scope_as = capital_holding }
		play_music_cue = "mx_cue_epic_sacral_moment"
	}
	option = {
		name = imperial.0005.a
		# Send narrative fluff to other players in realm (if any).
		every_player = {
			limit = {
				NOT = { this = root }
				is_vassal_or_below_of = root
			}
			trigger_event = imperial.0006
		}
	}
}

# Players informed
imperial.0006 = {
	type = character_event
	title = imperial.0006.t
	desc = imperial.0006.desc
	theme = realm
	left_portrait = {
		character = scope:new_co_emperor
		animation = personality_honorable
	}
	right_portrait = {
		character = scope:the_emperor
		animation = personality_honorable
	}

	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"
	}

	option = {
		name = imperial.0006.a		
	}
}

##################################################
# Military faction events
##################################################
# Faction Demand - COUP

imperial.0020 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_MILITARY"
	}
	desc = "FACTION_DEMAND_MILITARY_DESC"
	
	trigger = {
		exists = scope:faction
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		add_dread = medium_dread_loss

		scope:faction = {
			every_faction_member = {
				trigger_event = imperial.0021
			}

			scope:faction_targeted_title = {
				add_to_list = target_titles
			}
		}

		on_claimant_faction_war_win_common = {
			TARGET_TITLES = target_titles
			ATTACKER = scope:faction.faction_leader
			DEFENDER = root
			CLAIMANT = scope:faction_claimant
		#	ATTACKER_PRESTIGE = 0
		#	DEFENDER_PRESTIGE = 0
		}

		ai_chance = {
			base = 10
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				factor = 0
				any_ally = {
					NOT = {
						is_vassal_or_below_of = root
					}
					max_military_strength > root.max_military_strength
				}
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"
		
		root = {
			save_scope_as = faction_target
		}

		show_as_tooltip = {
			scope:faction = {
				faction_start_war = {
					title = scope:target_title
				}
			}
		}
		
		scope:faction_leader = {
			trigger_event = imperial.0022
		}

		ai_chance = {
			base = 100
			compare_modifier = {
				trigger = {
					ai_boldness > 0
				}
				value = ai_boldness
				multiplier = 3
			}
			compare_modifier = {
				trigger = {
					ai_boldness < 0
				}
				value = ai_boldness
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
		}
	}
}

# Claimant Faction Demand Accepted

imperial.0021 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_MILITARY_ACCEPTED"
	}
	desc = "FACTION_DEMAND_MILITARY_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
	}
}

# Claimant Faction Demand Refused

imperial.0022 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_MILITARY_REFUSED"
	}
	desc = "FACTION_DEMAND_MILITARY_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			every_faction_member = {
				limit = {
					NOT = { this = root } # i.e. not faction leader, even when the war disbands the faction
				}
				trigger_event = imperial.0023
			}

			faction_start_war = {
				title = scope:faction_targeted_title
			}
		}
	}
}

# Claimant Faction Demand Refused Member Notice

imperial.0023 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_MILITARY_REFUSED"
	}
	desc = "FACTION_DEMAND_MILITARY_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"	
		custom_tooltip = imperial.0023.opt_tt
	}
}

# Claimant Faction Demand Send Notice

imperial.0024 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_MILITARY_SEND_DEMAND_NOTIFICATION"
	}
	desc = "FACTION_DEMAND_MILITARY_SEND_DEMAND_NOTIFICATION_DESC"

	option = {
		name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	
	}
}


# Faction Demand - Elective succession

imperial.0025 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_CLAIMANT"
	}
	desc = "FACTION_DEMAND_MILITARY_ELECTIVE_DESC"
	
	trigger = {
		exists = scope:faction
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		add_dread = medium_dread_loss

		scope:faction = {
			add_faction_discontent = -500
			every_faction_member = {
				trigger_event = imperial.0026
			}			
		}
		scope:faction_targeted_title = {
			add_title_law = imperial_elective_succession_law
		}

		ai_chance = {
			base = 30
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				factor = 0
				any_ally = {
					NOT = {
						is_vassal_or_below_of = root
					}
					max_military_strength > root.max_military_strength
				}
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"
		
		root = {
			save_scope_as = faction_target
		}
		add_character_flag = { flag = refused_military_faction_elective_monarchy years = 5 }
		scope:faction = {
			add_faction_discontent = 50		
		}
		
		scope:faction_leader = {
			trigger_event = imperial.0027
		}

		ai_chance = {
			base = 100
			compare_modifier = {
				trigger = {
					ai_boldness > 0
				}
				value = ai_boldness
				multiplier = 3
			}
			compare_modifier = {
				trigger = {
					ai_boldness < 0
				}
				value = ai_boldness
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
		}
	}
}

# Claimant Faction Demand Accepted

imperial.0026 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_MILITARY_ACCEPTED"
	}
	desc = "FACTION_DEMAND_MILITARY_ELECTIVE_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
		show_as_tooltip = {
			scope:faction_targeted_title = {
				add_title_law = imperial_elective_succession_law
			}
		}
	}
}

# Claimant Faction Demand Refused

imperial.0027 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_MILITARY_REFUSED"
	}
	desc = "FACTION_DEMAND_MILITARY_ELECTIVE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			every_faction_member = {
				limit = {
					NOT = { this = root } # i.e. not faction leader, even when the war disbands the faction
				}
				trigger_event = imperial.0028
			}
			show_as_tooltip = {
				add_faction_discontent = 50
			}
		}
	}
}

# Claimant Faction Demand Refused Member Notice

imperial.0028 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_MILITARY_REFUSED"
	}
	desc = "FACTION_DEMAND_MILITARY_ELECTIVE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"	
		scope:faction = {
			show_as_tooltip = {
				add_faction_discontent = 50
			}
		}
	}
}

# Claimant Faction Demand Send Notice

imperial.0029 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "FACTION_DEMAND_MILITARY_ELECTIVE_SEND_DEMAND_NOTIFICATION"
	}
	desc = "FACTION_DEMAND_MILITARY_ELECTIVE_SEND_DEMAND_NOTIFICATION_DESC"

	option = {
		name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	
	}
}

##Ai emperor hand title to military candidate
imperial.0030 = {
	hidden = yes
	type = character_event
	
	immediate = {
		scope:faction_update = {
			special_character = {
				save_scope_as = recipient
			}
			every_faction_member = {
				send_interface_toast = {
					title = military_faction_CANDIDATE_GIVEN_TITLE
					left_icon = scope:recipient 
					
					custom_tooltip = military_faction_CANDIDATE_GIVEN_TITLE_DESC
				}
			}
			root = {
				remove_variable = military_candidate_ignored_count		
				add_character_flag = { flag = recently_granted_candidate_a_title days = 100 }
			}
		}
		create_title_and_vassal_change = change
		scope:change = {
			set_title_and_vassal_change_type = granted
		}
		if = { #this is a promotion to duke
			limit = {
				scope:recipient = {
					is_ruler = yes
					military_faction_duke_promotion_candidate_trigger = { FACTION_TARGET = root }
				}
			}
			scope:recipient = {
				primary_title.duchy = {
					scope:faction_update = { add_faction_discontent = -25 }
					change_title_holder = {
						holder = scope:recipient
						change = scope:change
					}
					if = {
						limit = { NOT = { exists = holder } }
						root = {
							add_prestige = 300
							remove_short_term_gold = 250
						}
					}
					#give all other vassals/titles in duchy
					every_in_de_jure_hierarchy = {
						limit = { 
							tier < tier_duchy 
						}
						if = {
							limit = { 
								holder = root 
								NAND = { 
									holder = { highest_held_title_tier > tier_barony }
									this = root.capital_barony 
								}
							}
							change_title_holder = {
								holder = scope:recipient
								change = scope:change
							}
						}
						else_if = {
							limit = { 
								holder = { 
									highest_held_title_tier < tier_duchy 
									liege = root 
								}
							}
							holder = {
								change_liege = {
									liege = scope:recipient
									change = scope:change
								}
							}	
						}
					}
				}
				add_opinion = {
					target = root
					modifier = received_title_duchy
				}
				change_liege = {
					liege = root
					change = scope:change
				}
			}
		}
		else = {
			random_held_title = {
				limit = {
					tier = tier_county
					title_province = { always = yes }
					NOT = { this = root.capital_county }
				}
				weight = {
					base = 1
					
					modifier = { #I have too many duchies and would like to give one away
						add = 30
						root = {
							any_held_title = {
								tier = tier_duchy
								count > 2
							}
						}
						duchy = {
							holder = root
							NOT = { this = root.capital_county.duchy } #not capital duchy
						}
					}
					
					modifier = { #Try to keep richer provinces
						add = {
							title_province = {
								add = monthly_income
							}					
							multiply = -10
						}						
					}
					modifier = { #Try and match cultures
						add = 5
						culture = scope:recipient.culture
					}
					modifier = { #Try and match cultures
						add = 5
						culture_group = scope:recipient.culture_group
					}
				}
				scope:faction_update = { add_faction_discontent = -15 }
				holder = {
					reverse_add_opinion = {
						target = scope:recipient
						modifier = received_title_county
					}
				}	
				change_title_holder = {
					holder = scope:recipient
					change = scope:change
				}
				duchy = { #and duchy???
					if = {
						limit = {
							holder = root
							NOT = { this = root.capital_county.duchy } #not capital duchy
							OR = { #Motives
								root = {
									any_held_title = { #has too many duchies
										tier = tier_duchy
										count > 2
									}	
								}	
								NOT = { #only has one county in the duchy anyway
									any_in_de_jure_hierarchy = {
										continue = { tier > tier_barony }
										tier = tier_county
										holder = root
										count > 1
									}
								}
							}					
						}	
						scope:faction_update = { add_faction_discontent = -10 }
						holder = {
							reverse_add_opinion = {
								target = scope:recipient
								modifier = received_title_duchy
							}
						}
						change_title_holder = {
							holder = scope:recipient
							change = scope:change
						}
						#give all other vassals/titles in duchy
						every_in_de_jure_hierarchy = {
							limit = { tier < tier_duchy }
							if = {
								limit = { 
									holder = root 
									NAND = { 
										holder = { highest_held_title_tier > tier_barony }
										this = root.capital_barony 
									}
								}
								change_title_holder = {
									holder = scope:recipient
									change = scope:change
								}
							}
							else_if = {
								limit = { 
									holder = { 
										highest_held_title_tier < tier_duchy 
										liege = root 
									}
								}
								holder = {
									change_liege = {
										liege = scope:recipient
										change = scope:change
									}
								}	
							}
						}
					}
				}
				scope:recipient = {
					change_liege = {
						liege = root
						change = scope:change
					}
				}
			}
		}	
		stress_impact = {
			greedy = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
		}
		resolve_title_and_vassal_change = scope:change
		scope:recipient = { 
			trigger_event = char_interaction.0110
		}
		scope:faction_update = {
			remove_special_character = yes
			remove_special_character = special_character
		}	
	}
}

##Military command title transfer request
# Inform Liege auto-accepted
imperial.0031 = {
	type = letter_event
	opening = {
		desc = char_interaction.0020.opening
	}
	desc = imperial.0031.desc
		
	sender = scope:actor
	
	immediate = {
		show_as_tooltip = {
			custom_description = {
				text = request_new_command_TOOLTIP
			}	
			scope:actor = {
				if = {
					limit = {
						joined_faction = { faction_is_type = military_faction }
					}
					add_joined_faction_discontent = -10
				}
				if = {
					limit = { scope:hook = yes }
					use_hook = scope:recipient
				}
			}
		}
	}

	option = {
		name = imperial.0031.a
	}
}

# Title Transfer Accepted
imperial.0032 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = imperial.0032.desc
		
	sender = scope:recipient

	immediate = {
		show_as_tooltip = {
			custom_description = {
				text = request_new_command_TOOLTIP
			}	
		}			
	}

	option = {
		name = imperial.0032.a
	}
}
# Title Transfer Refused
imperial.0033 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = imperial.0033.desc
		
	sender = scope:recipient

	option = {
		name = imperial.0033.a
		add_prestige = medium_prestige_value
	}
}



######


#New character emerges from bureaucracy
imperial.0040 = { 
	type = character_event
	title = imperial.0040.t
	desc = imperial.0040.desc
	theme = stewardship
	
	left_portrait = {
		character = scope:new_courtier_1
		animation = personality_greedy
	}
	right_portrait = {
		character = scope:new_courtier_2
		animation = personality_greedy
	}
	
	trigger = { 
		has_government = imperial_government
	}

	immediate = {
		hidden_effect = {
			#Courtier 1- pool character
			
			#Set up a list of provinces, one per duchy in the realm, find all relevant pool characters there
			every_realm_province = {
				if = {
					limit = {
						save_temporary_scope_as = potential_province
						NOT = {
							any_in_list = {
								list = pool_provinces
								county.duchy = scope:potential_province.county.duchy
							}
						}
					}
					add_to_temporary_list = pool_provinces
				}
			}

			every_in_list = {
				list = pool_provinces
				save_temporary_scope_as = pool_province

				every_pool_character = {
					province = scope:pool_province
					limit = {
						can_be_councillor_basics_trigger = yes
						NOT = { has_character_flag = ineligible_for_imperial_promotion }
						OR = {
							AND = {
								is_female = yes
								root.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
								patrilinear_betrothal = no
								patrilinear_marriage = no
							}
							AND = {
								is_male = yes
								root.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
								matrilinear_marriage = no
								matrilinear_betrothal = no
							}
						}
						OR = {
							stewardship >= decent_skill_rating
							diplomacy >= decent_skill_rating
							intrigue >= decent_skill_rating
						}				
						#Skill may be limited based on emperor's stewardship
						trigger_if = { 
							limit = { root = { stewardship < mediocre_skill_rating } }
							NOR = {
								stewardship > high_skill_rating
								diplomacy > high_skill_rating
								intrigue > high_skill_rating
							}
						}
						trigger_else_if = { 
							limit = { root = { stewardship < decent_skill_rating } }
							NOR = {
								stewardship > very_high_skill_rating
								diplomacy > very_high_skill_rating
								intrigue > very_high_skill_rating
							}
						}
						trigger_else_if = { 
							limit = { root = { stewardship < high_skill_rating } }
							NOR = {
								stewardship > extremely_high_skill_rating
								diplomacy > extremely_high_skill_rating
								intrigue > extremely_high_skill_rating
							}
						}
						trigger_else = {
							always = yes
						}
						
						NAND = { #Byzantine eunuch restrictions
							is_male = yes
							root = { has_culture_group = culture_group:byzantine_group }					
							OR = {
								is_married = yes
								is_betrothed = yes
								is_claimant = yes
								any_child = { always = yes }
								any_parent = { always = yes } #not randomly generated
							}
						}
					}
					add_to_temporary_list = potential_courtiers_1
				}
			}

			#Randomize a good courtier from the realm pool characters
			random_in_list = {
				list = potential_courtiers_1
				limit = { is_alive = yes }
				weight = {
					base = 1
					modifier = {
						OR = {
							diplomacy > root.cp:councillor_chancellor.diplomacy
							stewardship > root.cp:councillor_steward.stewardship
							intrigue > root.cp:councillor_spymaster.intrigue
						}
						add = {
							value = 40					
						}
					}
					modifier = {
						faith = root.faith
						add = {
							value = 20	
						}
					}
					modifier = {
						culture = root.culture
						add = {
							value = 20		
						}
					}
					modifier = {				
						OR = {
							stewardship >= high_skill_rating
							diplomacy >= high_skill_rating
							intrigue >= high_skill_rating
						}
						add = {
							value = 20
						}
					}
					modifier = {				
						OR = {
							stewardship >= very_high_skill_rating
							diplomacy >= very_high_skill_rating
							intrigue >= very_high_skill_rating
						}
						add = {
							value = 20
						}
					}
					modifier = {				
						OR = {
							stewardship >= extremely_high_skill_rating
							diplomacy >= extremely_high_skill_rating
							intrigue >= extremely_high_skill_rating
						}
						add = {
							value = 20
						}
					}
				}
				save_scope_as = new_courtier_1
			}

			#If no one was found, make one
			if = {
				limit = { NOT = { exists = scope:new_courtier_1 } }
				random_realm_province = { #for culture
					save_scope_as = random_courtier_province
				}
				random_list = {
					1 = {
						modifier = {
							add = 100
							cp:councillor_chancellor = {
								diplomacy < very_high_skill_rating
							}
						}
						create_character = {
							location = root.capital_province
							template = pool_imperial_courtier_diplomacy
							save_scope_as = new_courtier_1
						}
					}
					1 = {
						modifier = {
							add = 100
							cp:councillor_steward = {
								stewardship < very_high_skill_rating
							}
						}
						create_character = {
							location = root.capital_province
							template = pool_imperial_courtier_stewardship
							save_scope_as = new_courtier_1
						}
					}
					1 = {
						modifier = {
							add = 100
							cp:councillor_spymaster = {
								intrigue < very_high_skill_rating
							}
						}
						create_character = {
							location = root.capital_province
							template = pool_imperial_courtier_intrigue
							save_scope_as = new_courtier_1
						}
					}
				}		
			}
			scope:new_courtier_1 = {
				if = {
					limit = {
						is_male = yes
						root = { has_culture_group = culture_group:byzantine_group }
					}
					add_trait = eunuch
				}
			}
				
			#Courtier 2- noble character
			every_vassal_or_below = {
				limit = { highest_held_title_tier > tier_barony }
				every_courtier = {
					limit = {
						is_ruler = no
						can_be_councillor_basics_trigger = yes
						NOT = { has_character_flag = ineligible_for_imperial_promotion }
						has_dynasty = yes
						OR = {
							dynasty = { dynasty_prestige > 100 }
							dynasty = liege.dynasty
						}
						OR = {
							AND = {
								is_female = yes
								root.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
								patrilinear_betrothal = no
								patrilinear_marriage = no
							}
							AND = {
								is_male = yes
								root.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
								matrilinear_marriage = no
								matrilinear_betrothal = no
							}
						}
						OR = {
							stewardship >= decent_skill_rating
							diplomacy >= decent_skill_rating
							intrigue >= decent_skill_rating
						}	
						opinion = { target = root value > -10 }
						reverse_opinion = { target = root value > -49 }
						ai_greed > high_negative_ai_value
						ai_energy > high_negative_ai_value
					}
					add_to_temporary_list = potential_courtiers_2
				}
			}
			#Randomize a good courtier from the realm pool characters
			random_in_list = {
				list = potential_courtiers_2
				limit = { 
					is_alive = yes 
					#Skill may be limited based on emperor's stewardship
					trigger_if = { 
						limit = { root = { stewardship < mediocre_skill_rating } }
						NOR = {
							stewardship > high_skill_rating
							diplomacy > high_skill_rating
							intrigue > high_skill_rating
						}
					}
					trigger_else_if = { 
						limit = { root = { stewardship < decent_skill_rating } }
						NOR = {
							stewardship > very_high_skill_rating
							diplomacy > very_high_skill_rating
							intrigue > very_high_skill_rating
						}
					}
					trigger_else_if = { 
						limit = { root = { stewardship < high_skill_rating } }
						NOR = {
							stewardship > extremely_high_skill_rating
							diplomacy > extremely_high_skill_rating
							intrigue > extremely_high_skill_rating
						}
					}
					trigger_else = {
						always = yes
					}
				}
				weight = {
					base = 1
					modifier = { #Byzantines value eunuchs
						root = { has_culture_group = culture_group:byzantine_group }
						has_trait = eunuch
						add = {
							value = 25
						}
					}
					
					modifier = {
						OR = {
							diplomacy > root.cp:councillor_chancellor.diplomacy
							stewardship > root.cp:councillor_steward.stewardship
							intrigue > root.cp:councillor_spymaster.intrigue
						}
						add = {
							value = 40					
						}
					}
					modifier = {
						faith = root.faith
						add = {
							value = 20	
						}
					}
					modifier = {
						culture = root.culture
						add = {
							value = 20		
						}
					}
					modifier = {				
						OR = {
							stewardship >= high_skill_rating
							diplomacy >= high_skill_rating
							intrigue >= high_skill_rating
						}
						add = {
							value = 20
						}
					}
					modifier = {				
						OR = {
							stewardship >= very_high_skill_rating
							diplomacy >= very_high_skill_rating
							intrigue >= very_high_skill_rating
						}
						add = {
							value = 20
						}
					}
					modifier = {				
						OR = {
							stewardship >= extremely_high_skill_rating
							diplomacy >= extremely_high_skill_rating
							intrigue >= extremely_high_skill_rating
						}
						add = {
							value = 20
						}
					}
				}
				save_scope_as = new_courtier_2
			}

			#If no one was found, make one
			if = {
				limit = { NOT = { exists = scope:new_courtier_2 } }
				random_realm_province = { #for culture
					save_scope_as = random_courtier_province
				}
				random_list = {
					1 = {
						modifier = {
							add = 100
							cp:councillor_chancellor = {
								diplomacy < very_high_skill_rating
							}
						}
						create_character = {
							location = root.capital_province
							template = pool_imperial_courtier_diplomacy
							save_scope_as = new_courtier_2
						}
					}
					1 = {
						modifier = {
							add = 100
							cp:councillor_steward = {
								stewardship < very_high_skill_rating
							}
						}
						create_character = {
							location = root.capital_province
							template = pool_imperial_courtier_stewardship
							save_scope_as = new_courtier_2
						}
					}
					1 = {
						modifier = {
							add = 100
							cp:councillor_spymaster = {
								intrigue < very_high_skill_rating
							}
						}
						create_character = {
							location = root.capital_province
							template = pool_imperial_courtier_intrigue
							save_scope_as = new_courtier_2
						}
					}
				}	
				scope:new_courtier_2 = {
					if = {
						limit = {
							is_male = yes
							root = { has_culture_group = culture_group:byzantine_group }
						}
						add_trait = eunuch
					}
				}			
			}
		}	
	}

	option = { # courtier 1
		name = imperial.0040.a		
		ai_chance = {
			base = 0
			modifier = {
				add = 100
				OR = {
					cp:councillor_chancellor = {
						diplomacy < scope:new_courtier_1.diplomacy
					}
					cp:councillor_steward = {
						stewardship < scope:new_courtier_1.stewardship
					}
					cp:councillor_spymaster = {
						intrigue < scope:new_courtier_1.intrigue
					}
				}
			}
		}
		remove_short_term_gold = 50
		add_courtier = scope:new_courtier_1
		scope:new_courtier_1 = { 
			add_prestige = 100
			save_scope_as = new_imperial_courtier 
		}
	}
	option = { # courtier 2
		name = imperial.0040.b	
		ai_chance = {
			base = 0
			modifier = {
				add = 100
				OR = {
					cp:councillor_chancellor = {
						diplomacy < scope:new_courtier_2.diplomacy
					}
					cp:councillor_steward = {
						stewardship < scope:new_courtier_2.stewardship
					}
					cp:councillor_spymaster = {
						intrigue < scope:new_courtier_2.intrigue
					}
				}
			}
		}
		hidden_effect = {
			scope:new_courtier_2 = { 
				liege = {
					if = {
						limit = {
							NOT = { this = root }
						}
						send_interface_toast = {
							title = imperial_0040_courtier_left_TITLE
							custom_tooltip = imperial_0040_courtier_left_DESC
							left_icon = scope:new_courtier_2
							right_icon = root
							show_as_tooltip = { 
								scope:new_courtier_2 = {
									add_prestige = 100
								}
							}
						}
					}
				}
			}	
		}
		remove_short_term_gold = 50
		add_courtier = scope:new_courtier_2
		scope:new_courtier_2 = { 
			add_prestige = 100
			save_scope_as = new_imperial_courtier 
		}
	}
	option = { # Neither
		name = imperial.0040.c	
		ai_chance = {
			base = 1
		}
		add_prestige = 100
		hidden_effect = { #make ineligible for 10 years
			scope:new_courtier_1 = {
				add_character_flag = { flag = ineligible_for_imperial_promotion years = 10 }
			}
			scope:new_courtier_2 = {
				add_character_flag = { flag = ineligible_for_imperial_promotion years = 10 }
			}
		}
	}
	after = {
		#AI adds them to council
		if = {
			limit = {
				is_ai = yes
			}
			if = {
				limit = { 
					cp:councillor_chancellor = {
						diplomacy < scope:new_imperial_courtier.diplomacy
					}
				}
				fire_councillor = cp:councillor_chancellor
				assign_councillor_type = { type = councillor_chancellor target = scope:new_imperial_courtier fire_on_actions = yes }
			}
			else_if = {
				limit = { 
					cp:councillor_steward = {
						stewardship < scope:new_imperial_courtier.stewardship
					}
				}
				fire_councillor = cp:councillor_steward
				assign_councillor_type = { type = councillor_steward target = scope:new_imperial_courtier fire_on_actions = yes }
			}
			else_if = {
				limit = { 
					cp:councillor_spymaster = {
						intrigue < scope:new_imperial_courtier.intrigue
					}
				}
				fire_councillor = cp:councillor_spymaster
				assign_councillor_type = { type = councillor_spymaster target = scope:new_imperial_courtier fire_on_actions = yes }
			}
		}
	}
}
#New character emerges from bureaucracy
imperial.0041 = {
	hidden = yes
	
	trigger = {
		has_government = imperial_government
		NOT = {
			any_courtier = {
				has_character_flag = prominent_bureaucrat
			}
		}
	}

	immediate = {
		random_realm_province = { #for culture
			save_scope_as = random_courtier_province
		}
		random_list = {
			1 = {
				create_character = {
					location = root.capital_province
					template = pool_imperial_courtier_diplomacy
					save_scope_as = new_courtier
				}
			}
			1 = {
				create_character = {
					location = root.capital_province
					template = pool_imperial_courtier_stewardship
					save_scope_as = new_courtier
				}
			}
			1 = {
				create_character = {
					location = root.capital_province
					template = pool_imperial_courtier_intrigue
					save_scope_as = new_courtier
				}
			}
		}		
		scope:new_courtier = {
			add_character_flag = prominent_bureaucrat
			add_prestige = 100
			if = {
				limit = {
					is_male = yes
					root = { has_culture_group = culture_group:byzantine_group }
				}
				add_trait = eunuch
			}
		}	
		send_interface_toast = {
			title = prominent_bureaucrat_TITLE
			custom_tooltip = prominent_bureaucrat_DESC
			add_courtier = scope:new_courtier
			left_icon = scope:new_courtier		
		}
	}
}	

##################################################
# Imperial Administration adopted
##################################################
imperial.0050 = {
	type = character_event
	title = imperial.0050.t
	desc = imperial.0050.desc
	theme = realm
	right_portrait = {
		character = root
		animation = personality_bold
	}

	immediate = {
		root = {
			capital_barony = { save_scope_as = capital_holding }
			save_scope_as = new_emperor
		}	
		play_music_cue = "mx_cue_epic_sacral_moment"
	}
	option = {
		name = imperial.0050.a
		custom_tooltip = TOOLTIPimperial.0050
		# Send narrative fluff to other players in realm (if any).
		every_player = {
			limit = {
				NOT = { this = root }
				is_vassal_or_below_of = root
			}
			trigger_event = imperial.0051
		}
	}
}

# Players informed
imperial.0051 = {
	type = character_event
	title = imperial.0051.t
	desc = imperial.0051.desc
	theme = realm
	right_portrait = {
		character = root
		animation = personality_bold
	}

	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"
	}

	option = {
		name = imperial.0051.a	
	}
}

##################################################
# Patrician events
##################################################
#Rich landless character creates new patrician house
imperial.0070 = {
	hidden = yes
	
	trigger = {
		OR = {
			AND = {
				has_government = imperial_government
				below_patrician_limit_trigger = yes
			}	
			top_liege = { 
				has_government = imperial_government 
				below_patrician_limit_trigger = yes
			}
		}	
	}

	immediate = {
		if = {
			limit = { #give the AI a hand
				is_ai = yes
				has_estates_trigger = no
				OR = {
					has_government = imperial_government
					has_government = military_command
				}	
				gold >= 150
			}
			add_character_modifier = { modifier = patrician_estate_modifier_1 }
			remove_short_term_gold = 200
		}	
		every_courtier = {
			limit = {
				is_ruler = no
				gold >= 200
			}
			trigger_event = imperial.0071
		}	
	}
}
imperial.0071 = {
	hidden = yes
	
	trigger = {
		gold >= 200
		
		is_imprisoned = no
		has_dynasty = yes
		is_physically_able_adult = yes
		is_ruler = no
		
		OR = {
			faith = top_liege.faith
			culture_group = top_liege.culture_group
		}
		NAND = {
			top_liege.culture_group = culture_group:byzantine_group
			byzantine_elective_deformed_candidate_trigger = yes
		}
		OR = {
			NOT = { #no fellow house is a ruler
				house = {
					any_house_member = {
						is_alive = yes
						is_ruler = yes
						NOT = { has_government = theocracy_government }
						NOT = { has_government = republic_government }
						NOT = { has_government = holy_order_government }
					}
				}	
			}	
			#can form cadet branch
			AND = {
				is_house_head = no
				house.house_head = {
					government_allows = create_cadet_branches
					custom_description = {
						text = create_cadet_branch_decision_succession_line
						NOT = {
							any_held_title = {
								place_in_line_of_succession = {
									target = root
									value <=3
								}
							}
						}
					}
				}
				custom_description = {
					text = create_cadet_branch_decision_ancestor_in_house
					NOT = {
						any_ancestor = {
							house = root.house
						}
					}
				}
				NOT = {
					has_trait = devoted
				}
			}
		}	
		trigger_if = {
			limit = {
				is_married = yes
				is_male = yes
			}
			patrilinear_marriage = yes
		}
		trigger_else_if = {
			limit = {
				is_married = yes
				is_female = yes
			}
			matrilinear_marriage = yes
		}
		trigger_else_if = { #Males of female-dominated faith must already be patrilineally married before taking this decision.
			limit = {
				is_married = no
				is_female = no
				faith = {
					has_doctrine = doctrine_gender_female_dominated
				}
			}
			patrilinear_marriage = yes
		}
		trigger_else_if = { #Females of male-dominated faith must already be matrilineally married before taking this decision.
			limit = {
				is_married = no
				is_female = yes
				faith = {
					has_doctrine = doctrine_gender_male_dominated
				}
			}
			matrilinear_marriage = yes
		}
		trigger_else = {
			always = yes
		}
	}

	immediate = {
		#create cadet branch?
		if = {
			limit = {
				is_house_head = no
				house = {
					any_house_member = {
						is_alive = yes
						is_ruler = yes
						NOT = { has_government = theocracy_government }
						NOT = { has_government = republic_government }
						NOT = { has_government = holy_order_government }
					}
				}	
			}
			execute_decision = create_cadet_branch_decision
		}
		
		new_patrician_setup_effect = { LIEGE = root.top_liege PREDECESSOR = root }
		
		add_character_modifier = { modifier = patrician_estate_modifier_1 }	
		remove_short_term_gold = 200
		
		top_liege = { 
			send_interface_toast = {
				title = NEW_PATRICIAN_TITLE
				custom_tooltip = NEW_PATRICIAN_DESC
				left_icon = scope:new_patrician
			}
		}
	}
}	

# Estate Revoked
imperial.0072 = {
	type = letter_event
	opening = {
		desc = char_interaction.0020.opening
	}
	desc = imperial.0072.desc
		
	sender = scope:actor

	option = {
		name = imperial.0072.a
	}
}

#Last estate revoked- GAME OVER
imperial.0073 = {
	type = character_event
	title = imperial.0073.t
	desc = imperial.0073.desc
	theme = realm
	left_portrait = {
		character = root
		animation = shame
	}

	immediate = {
		top_liege = { save_scope_as = the_top_liege }
		play_music_cue = "mx_cue_negative"
	}
	option = {
		name = imperial.0073.a
		custom_tooltip = imperial.0073_GAMEOVER
		hidden_effect = {
			every_held_title = { #destroy all landless patrician titles
				limit = { 
					is_landless_patrician_title_trigger = yes
				}
				holder = { destroy_title = prev }
			}
			
			#hand over other titles back to liege
			create_title_and_vassal_change = change
			scope:change = {
				set_title_and_vassal_change_type = revoked
			}
			every_held_title = {
				limit = { 
					NOT = { is_landless_patrician_title_trigger = yes }
					NAND = { 
						holder = { highest_held_title_tier > tier_barony }
						this = root.capital_barony 
					}
				}
				change_title_holder = {
					holder = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
}

# Estate Revocation Accepted
imperial.0074 = {
	type = letter_event
	opening = {
		desc = char_interaction.0001.opening
	}
	desc = imperial.0074.desc
		
	sender = scope:recipient

	immediate = {
		show_as_tooltip = { add_gold = estate_sell_value }
	}

	option = {
		name = imperial.0074.a
	}
}


###Vanilla born in purple override
# Born in the Purple allocation
#	by Ewan Cowhig Croft
birth.8011 = {
	type = character_event
	hidden = yes
	
	trigger = {
		OR = {
			any_parent = {
				OR = {
					has_title = title:e_byzantium	#Must be the current emperor.
					has_title = title:e_roman_empire	#Or the other emperor.
				}
				capital_county = title:c_byzantion	#Must rule from the Bucoleon Palace.
			}
			any_parent = { #Imperial mod- co-emperors count as well
				OR = {
					AND = {
						has_relation_co_emperor = title:e_byzantium.holder 
						title:e_byzantium.holder = { capital_county = title:c_byzantion	 } #Must rule from the Bucoleon Palace.
					}
					AND = {
						has_relation_co_emperor = title:e_roman_empire.holder
						title:e_roman_empire.holder = { capital_county = title:c_byzantion	 } #Must rule from the Bucoleon Palace.
					}					
				}				
			}
		}	
		mother = {
			OR = {
				AND = {	#Landless mothers must be actually at court to use the chamber.
					is_ruler = no
					location = title:c_byzantion.title_province
				}
				AND = {	#Landless mothers can follow father to court
					is_ruler = no
					root.father = { location = title:c_byzantion.title_province }
				}
				AND = {	#Landed rulers can be assumed to make the journey, unless they have some extremely pressing reason not to.
					is_ruler = yes
					NOT = { is_at_war_with = root.father }
				}
			}
			is_imprisoned = no	#Cannot use the purple chamber from prison.
			is_commanding_army = no	#Cannot use the purple chamber whilst commanding an army.
		}
	}

	immediate = { add_trait = born_in_the_purple }
}