namespace = tgp_japan_general

#tgp_japan_general.9001 = {
#	type = character_event
#	title = tgp_japan_general.9001.t
#	desc = tgp_japan_general.9001.desc
#	theme = realm
#
#	immediate = {
#	}
#
#	option = {
#		name = tgp_japan_general.9001.a
#	}
#}
#
#tgp_japan_general.9010 = {
#	type = character_event
#	title = tgp_japan_general.9010.t
#	desc = tgp_japan_general.9010.desc
#	theme = realm
#
#	immediate = {
#	}
#
#	option = {
#		name = tgp_japan_general.9010.a
#	}
#}


# INSTALL BLOC OUTCOME
scripted_trigger tgp_japan_general_8800_trigger = {
	is_adult = yes
	is_ruler = no
	is_clergy = no
	is_faith_dominant_gender = yes
	is_incapable = no
	is_lowborn = no
	save_temporary_scope_as = potential_temp
	root = {
		any_held_title = {
			count = 0
			place_in_line_of_succession = {
				target = scope:potential_temp
				value <= 3
			}
		}
	}
	NOR = {
		scope:potential_temp = root.player_heir
		scope:potential_temp = root.primary_heir
		scope:family_1 ?= scope:potential_temp
		scope:family_2 ?= scope:potential_temp
		scope:family_3 ?= scope:potential_temp
		scope:fallback_1 ?= scope:potential_temp
		scope:fallback_2 ?= scope:potential_temp
		scope:fallback_3 ?= scope:potential_temp
		trigger_if = {
			limit = { exists = scope:potential_temp.house }
			OR = {
				scope:fallback_1.house ?= scope:potential_temp.house
				scope:fallback_2.house ?= scope:potential_temp.house
			}
		}
	}
}

scripted_effect tgp_japan_general_8800_scope_effect = {
	if = {
		limit = { exists = scope:$NAME$_2 }
		save_scope_as = $NAME$_3
	}
	else_if = {
		limit = { exists = scope:$NAME$_1 }
		save_scope_as = $NAME$_2
	}
	else = { save_scope_as = $NAME$_1 }
	tgp_japan_general_8800_portrait_effect = yes
}

scripted_effect tgp_japan_general_8800_portrait_effect = {
	if = {
		limit = { exists = scope:portrait_3 }
		# early out
	}
	else_if = {
		limit = { exists = scope:portrait_2 }
		save_scope_as = portrait_3
		add_to_list = potential_list
	}
	else_if = {
		limit = { exists = scope:portrait_1 }
		save_scope_as = portrait_2
		add_to_list = potential_list
	}
	else = {
		save_scope_as = portrait_1
		add_to_list = potential_list
	}
}

scripted_effect tgp_japan_general_8800_grant_effect = {
	save_scope_as = recipient

	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = change
		add_claim_on_loss = yes
	}

	#As this is just a county conquest, simply grab the designated county.
	every_in_list = {
		list = target_titles
		change_title_holder = {
			holder = scope:recipient
			change = scope:change
		}
	}

	if = {
		limit = {
			root.liege = { is_independent_ruler = no }
		}
		change_liege = {
			liege = root.liege
			change = scope:change
		}
	}

	resolve_title_and_vassal_change = scope:change

	root = {

		if = {
			limit = {
				can_add_hook = {
					target = scope:recipient
					type = loyalty_hook
				}
			}
			add_hook = {
				target = scope:recipient
				type = loyalty_hook
			}
		}
	}

	scope:recipient = {
		add_opinion = {
			target = root
			modifier = grateful_opinion
			opinion = 25
		}
		if = {
			limit = {
				NOT = { government_has_flag = government_is_japan_feudal }
			}
			change_government = japan_feudal_government
		}
		if = {
			limit = { is_house_head = no }
			custom_tooltip = {
				text = tgp_japan_general.8800.cadet_tt
				add_character_flag = found_cadet_branch
				trigger_event = tgp_japan_decision.9001
			}
		}
		house = {
			tgp_join_house_bloc_effect = {
				INVITER = root.house
				OPINION = flag:yes
			}
			set_variable = {
				name = bloc_expansion_won_forced_membership_var
				years = 5
			}
			save_scope_as = new_house
		}
		root.house = {
			switch = {
				trigger = has_house_aspiration_parameter
				ceremony_cheaper_feasts = { #Ceremony
					scope:new_house = {
						set_house_aspiration = {
							type = ceremony
							level = 1
						}
					}
				}
				determination_spymaster_task_find_secrets_efficiency = { #Determination
					scope:new_house = {
						scope:new_house = {
							set_house_aspiration = {
								type = determination
								level = 1
							}
						}
					}
				}
				unlocks_japanese_manor_shrine = { #Humility
					scope:new_house = {
						scope:new_house = {
							set_house_aspiration = {
								type = humility
								level = 1
							}
						}
					}
				}
				unlocks_japanese_manor_brewery = { #Prosperity
					scope:new_house = {
						scope:new_house = {
							set_house_aspiration = {
								type = prosperity
								level = 1
							}
						}
					}
				}
				service_house_improved_educator_outcome_chance = { #Service
					scope:new_house = {
						scope:new_house = {
							set_house_aspiration = {
								type = service
								level = 1
							}
						}
					}
				}
				unlocks_japanese_manor_armory = { #Strength
					scope:new_house = {
						scope:new_house = {
							set_house_aspiration = {
								type = strength
								level = 1
							}
						}
					}
				}
			}
		}
	}
	every_in_list = {
		list = potential_list
		limit = {
			NOT = { this = scope:recipient }
		}
		custom = tgp_japan_general_8800_custom
		add_opinion = {
			target = scope:recipient
			modifier = annoyed_opinion
			opinion = -15
		}
	}
}

tgp_japan_general.8800 = {
	type = character_event
	title = tgp_japan_general.8800.t
	desc = tgp_japan_general.8800.desc
	theme = war
	override_background = { reference = estate }
	left_portrait = {
		character = root
		animation = japanese_war_fan
	}
	lower_right_portrait = scope:portrait_3
	lower_center_portrait = scope:portrait_2
	lower_left_portrait = scope:portrait_1
	override_background = { reference = throne_room_scope }

	immediate = {
		save_scope_as = winner
		scope:defender = { save_scope_as = loser }
		play_sound_effect = "event:/DLC/EP4/SFX/Stingers/Japan/tgp_mx_sting_adopting_feudal_government"
		house.house_confederation = {
			tgp_bloc_change_cohesion_effect = {
				VALUE = 5
				ICON = scope:attacker
				REASON = install_bloc_member
			}
		}
		every_child = {
			limit = { tgp_japan_general_8800_trigger = yes }
			add_to_list = potential_family
		}
		every_sibling = {
			limit = { tgp_japan_general_8800_trigger = yes }
			add_to_list = potential_family
		}
		every_close_or_extended_family_member = {
			limit = {
				OR = {
					is_uncle_or_aunt_of = root
					is_nibling_of = root
				}
				tgp_japan_general_8800_trigger = yes
			}
			add_to_list = potential_family
		}
		every_close_or_extended_family_member = {
			limit = {
				is_nibling_of = root
				tgp_japan_general_8800_trigger = yes
			}
			add_to_list = potential_family
		}
		hidden_effect = {
			ordered_in_list = {
				list = potential_family
				order_by = "opinion(root)"
				tgp_japan_general_8800_scope_effect = { NAME = family }
			}
		}
		if = {
			limit = { NOT = { exists = scope:portrait_3 } }
			every_councillor = {
				limit = {
					NOT = { is_in_list = potential_family }
					tgp_japan_general_8800_trigger = yes
				}
				add_to_list = potential_fallback
			}
			every_knight = {
				limit = {
					NOT = { is_in_list = potential_family }
					tgp_japan_general_8800_trigger = yes
				}
				add_to_list = potential_fallback
			}
			every_courtier = {
				limit = {
					NOT = { is_in_list = potential_family }
					tgp_japan_general_8800_trigger = yes
				}
				add_to_list = potential_fallback
			}
			hidden_effect = {
				while = {
					count = 3
					random_in_list = {
						list = potential_fallback
						limit = {
							is_house_head = yes
							tgp_japan_general_8800_trigger = yes
						}
						alternative_limit = {
							tgp_japan_general_8800_trigger = yes
						}
						weight = {
							base = 1
							modifier = { add = "opinion(root)" }
							modifier = { is_councillor = yes add = 50 }
							modifier = { is_knight = yes add = 25 }
						}
						tgp_japan_general_8800_scope_effect = { NAME = fallback }
					}
				}
				if = {
					limit = {
						NOT = {
							exists = scope:fallback_1
						}
					}
					create_character = {
						template = new_warrior_character
						gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
						employer = root
						faith = root.faith
						culture = root.culture
						dynasty = generate
						gender_female_chance = root_soldier_female_chance
						save_scope_as = fallback_1
					}
				}
			}
		}
		if = {
			limit = { NOT = { exists = scope:portrait_3 } }
			hidden_effect = {
				while = {
					count = 3
					random_pool_guest = {
						limit = {
							is_house_head = yes
							tgp_japan_general_8800_trigger = yes
						}
						alternative_limit = {
							tgp_japan_general_8800_trigger = yes
						}
						weight = {
							base = 1
							modifier = { add = "opinion(root)" }
						}
						tgp_japan_general_8800_scope_effect = { NAME = fallback }
					}
				}
			}
		}
		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }
	}

	option = {
		name = tgp_japan_general.8800.a
		trigger = { exists = scope:family_1 }
		scope:family_1 = { tgp_japan_general_8800_grant_effect = yes }
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:family_1
				multiplier = 1
			}
		}
	}

	option = {
		name = tgp_japan_general.8800.b
		trigger = { exists = scope:family_2 }
		scope:family_2 = { tgp_japan_general_8800_grant_effect = yes }
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:family_2
				multiplier = 1
			}
		}
	}

	option = {
		name = tgp_japan_general.8800.c
		trigger = { exists = scope:family_3 }
		scope:family_3 = { tgp_japan_general_8800_grant_effect = yes }
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:family_3
				multiplier = 1
			}
		}
	}

	option = {
		name = tgp_japan_general.8800.e
		trigger = {
			exists = scope:fallback_1
			calc_true_if = {
				amount < 3
				exists = scope:family_1
				exists = scope:family_2
				exists = scope:family_3
			}
		}
		scope:fallback_1 = { tgp_japan_general_8800_grant_effect = yes }
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:fallback_1
				multiplier = 1
			}
		}
	}

	option = {
		name = tgp_japan_general.8800.f
		trigger = {
			exists = scope:fallback_2
			calc_true_if = {
				amount < 3
				exists = scope:family_1
				exists = scope:family_2
				exists = scope:family_3
				exists = scope:fallback_1
			}
		}
		scope:fallback_2 = { tgp_japan_general_8800_grant_effect = yes }
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:fallback_2
				multiplier = 1
			}
		}
	}

	option = {
		name = tgp_japan_general.8800.g
		trigger = {
			exists = scope:fallback_3
			calc_true_if = {
				amount < 3
				exists = scope:family_1
				exists = scope:family_2
				exists = scope:family_3
				exists = scope:fallback_1
				exists = scope:fallback_2
			}
		}
		scope:fallback_3 = { tgp_japan_general_8800_grant_effect = yes }
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:fallback_3
				multiplier = 1
			}
		}
	}

	option = {
		name = tgp_japan_general.8800.h

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}

		#As this is just a county conquest, simply grab the designated county.
		every_in_list = {
			list = target_titles
			change_title_holder = {
				holder = root
				change = scope:change
			}
		}

		resolve_title_and_vassal_change = scope:change

		every_in_list = {
			list = potential_list
			limit = { is_ai = yes }
			custom = tgp_japan_general_8800_custom
			add_opinion = {
				target = root
				modifier = annoyed_opinion
				opinion = -25
			}
		}
		ai_chance = {
			base = 300
			modifier = {
				domain_limit_available <= 0
				factor = 0
			}
		}
	}
}

## NEW CEREMONIAL EMPEROR NOTIFICATION

scripted_effect tgp_new_ceremonial_liege_notification_effect = {
	$TITLE$ = {
		save_scope_as = ceremonial_title
		holder = { save_scope_as = ceremonial_liege }
		previous_holder = { save_scope_as = previous_holder }
	}
	hidden_effect = {
		every_player = {
			limit = { $TITLE$.var:ceremonial_title.holder.top_liege = top_liege }
			send_interface_message = {
				type = msg_liege_changed
				title = new_ceremonial_liege_message_title
				left_icon = scope:ceremonial_liege
				right_icon = scope:previous_holder
				custom_tooltip = new_ceremonial_liege_message_effect
			}
		}
	}
}

tgp_japan_general.9100 = {
	type = character_event
	title = tgp_japan_general.9100.t
	desc = {
		first_valid = {
    		triggered_desc = {
    		    trigger = { exists = scope:murder }
    		    desc = tgp_japan_general.9100.desc_murdered
    		}
    		triggered_desc = {
    		    trigger = {
    				scope:previous_holder = { is_alive = no }
    		    }
    		    desc = tgp_japan_general.9100.desc_dead
    		}
    		triggered_desc = {
    		    trigger = {
    				scope:previous_holder = top_liege
    		    }
    		    desc = tgp_japan_general.9100.desc_joko
    		}
    		desc = tgp_japan_general.9100.desc_alive
    	}
    	desc = tgp_japan_general.9100.desc
	}
	theme = crown
	left_portrait = {
		character = root
		animation = war_over_tie
	}
	right_portrait = {
		character = top_liege
		animation = chancellor
		trigger = {
			NOT = { this = root }
		}
	}
	lower_right_portrait = scope:title.previous_holder
	override_background = { reference = throne_room }

	trigger = {
		exists = scope:previous_holder
		scope:title = { exists = var:ceremonial_title }
		NOR = {
			scope:transfer_type = flag:conquest
			scope:transfer_type = flag:conquest_claim
		}
	}

	immediate = {
		show_as_tooltip = { get_title = scope:title }
    	if = {
    		limit = {
    			OR = {
    				any_secret = {
    					type = secret_murder
    					secret_target = scope:previous_holder
    				}
    				scope:previous_holder ?= {
						is_alive = no
						killer ?= root
					}
    			}
    		}
    		save_scope_value_as = {
    			name = murder
    			value = yes
    		}
    	}
		assign_quirk_effect = yes
		scope:title = {
			previous_holder ?= { save_scope_as = previous_holder }
			save_scope_as = ceremonial_title
			holder = {
				house = { save_scope_as = ceremonial_house }
				save_scope_as = ceremonial_liege
				house = { save_scope_as = ceremonial_house }
				if = {
					limit = { government_is_japanese_trigger = yes }
					### REVOKE ALL GOVERNORSHIPS IF TENNO NOT RESTORED
					if = {
						limit = {
							is_independent_ruler = no
							NOT = { has_global_variable = tenno_restored }
						}
						every_held_title = {
							limit = {
								tier >= tier_county
								is_landless_type_title = no
								is_noble_family_title = no
								NOT = { scope:title ?= this }
							}
							add_to_list = lost_titles
						}
						create_title_and_vassal_change = {
							type = conquest
							save_scope_as = title_change
							add_claim_on_loss = no
						}
						every_in_list = {
							list = lost_titles
							change_title_holder_include_vassals = {
								holder = root.top_liege
								change = scope:title_change
							}
						}
						resolve_title_and_vassal_change = scope:title_change
					}
				}
				### CLEANUP RELEVANT TRAITS
				if = {
					limit = { has_trait = former_emperor }
					remove_trait = former_emperor
				}
			}
		}
		tgp_new_ceremonial_liege_notification_effect = { TITLE = scope:title }
	}

	option = {
		name = tgp_japan_general.9100.a
	}
}

## BECOME CLOISTRED EMPEROR DECISION FOR CEREMONIAL EMPEROR REGENT

tgp_japan_general.9120 = {
	type = character_event
	title = tgp_japan_general.9120.t
	desc = tgp_japan_general.9120.desc
	theme = crown
	left_portrait = {
		character = root
		animation = holding_hu
	}
	lower_left_portrait = {
		character = scope:emperor_player_heir
		trigger = { this != title:e_japan.previous_holder }
	}
	lower_right_portrait = {
		character = title:e_japan.previous_holder
	}

	trigger = {
		has_tgp_dlc_trigger = yes
		NOR = {
			has_global_variable = tenno_restored
			var:ascended_throne_reason ?= flag:conquest_claim
			var:ascended_throne_reason ?= flag:conquest
		}
	}

	on_trigger_fail = {
		trigger_event = tgp_japan_general.9100
	}

	immediate = {
		player_heir = { save_scope_as = emperor_player_heir }
	}

	# Become Cloistered Emperor
	option = {
		name = tgp_japan_general.9120.a
		japan_clear_flavour_flags_effect = yes
		add_character_flag = joko_flag
		change_influence = major_influence_gain
		if = {
			limit = {
				NOT = { has_trait = devoted }
			}
			add_trait = devoted
		}
		if = {
			limit = {
				NOT = { has_trait = former_emperor }
			}
			add_trait = former_emperor
		}
		create_title_and_vassal_change = {
			type = appointment
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_chrysanthemum_throne = {
			change_title_holder = {
				holder = scope:emperor_player_heir
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		scope:emperor_player_heir = { add_character_flag = ceremonial_liege_flag }
		tgp_new_ceremonial_liege_notification_effect = { TITLE = title:k_chrysanthemum_throne }
		ai_chance = {
			base = 50
		}
	}

	#Option B
	option = {
		name = tgp_japan_general.9120.b
		japan_clear_flavour_flags_effect = yes
		add_character_flag = ceremonial_liege_flag
		add_prestige = major_prestige_loss
		every_vassal = {
			custom = custom.every_vassal
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -25
			}
		}
		custom_tooltip = tgp_japan_emperor_restoration_decision_faction_effect
		ai_chance = {
			base = 0
		}
	}
}

# IMPERIAL POLICY NOTIFICATION

tgp_japan_general.9300 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = defense_mobilization_law } }
				desc = tgp_japan_general.9300.t.defense_mobilization_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = disarmament_law } }
				desc = tgp_japan_general.9300.t.disarmament_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = isolation_law } }
				desc = tgp_japan_general.9300.t.isolation_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = imperial_expansion_law } }
				desc = tgp_japan_general.9300.t.imperial_expansion_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = manor_reform_law } }
				desc = tgp_japan_general.9300.t.manor_reform_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = bandit_crackdown_law } }
				desc = tgp_japan_general.9300.t.bandit_crackdown_law
			}
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:liege ?= root
					scope:ceremonial_liege != root
				}
				desc = tgp_japan_general.9300.restored_emperor
			}
			triggered_desc = {
				trigger = {
					scope:ceremonial_liege ?= root
					scope:liege != root
				}
				desc = tgp_japan_general.9300.ceremonial_liege
			}
			triggered_desc = {
				trigger = {
					scope:ceremonial_liege ?= root
					scope:liege = root
				}
				desc = tgp_japan_general.9300.restored_emperor
			}
			triggered_desc = {
				trigger = { scope:ceremonial_liege != scope:liege }
				desc = tgp_japan_general.9300.vassal_ceremonial
			}
			desc = tgp_japan_general.9300.vassal
		}
		desc = tgp_japan_general.9300.outro
		first_valid = {
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = defense_mobilization_law } }
				desc = tgp_japan_general.9300.defense_mobilization_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = disarmament_law } }
				desc = tgp_japan_general.9300.disarmament_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = isolation_law } }
				desc = tgp_japan_general.9300.isolation_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = imperial_expansion_law } }
				desc = tgp_japan_general.9300.imperial_expansion_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = manor_reform_law } }
				desc = tgp_japan_general.9300.manor_reform_law
			}
			triggered_desc = {
				trigger = { scope:liege = { has_realm_law = bandit_crackdown_law } }
				desc = tgp_japan_general.9300.bandit_crackdown_law
			}
		}
	}
	theme = crown
	left_portrait = {
		character = scope:liege
		animation = writing
	}
	right_portrait = {
		character = scope:ceremonial_liege
		animation = holding_hu
		trigger = {
			NOT = { this = scope:liege }
		}
	}
	override_background = { reference = throne_room_scope }

	immediate = {
		play_sound_effect = "event:/DLC/EP4/SFX/Stingers/Japan/tgp_mx_sting_restoring_japanese_emperor"
		tgp_save_realm_ceremonial_liege_effect = yes
		if = {
			limit = { tgp_realm_has_ceremonial_liege_trigger = no }
			save_scope_as = ceremonial_liege
			save_scope_as = liege
		}
		scope:ceremonial_liege ?= { save_scope_as = background_throne_room_scope }
		scope:liege = {
			if = {
				limit = { NOT = { exists = scope:background_throne_room_scope } }
				save_scope_as background_throne_room_scope
			}
		}
	}

	option = {
		name = {
			trigger = {
				scope:liege = { has_realm_law = disarmament_law }
				government_has_flag = government_is_japan_feudal
			}
			text = tgp_japan_general.9300.a.disarmament_soryo
		}
		name = {
			text = tgp_japan_general.9300.a
		}
		scope:liege = {
			show_as_tooltip = {
				switch = {
					trigger = has_realm_law
					defense_mobilization_law = { add_realm_law = defense_mobilization_law }
					disarmament_law = { add_realm_law = disarmament_law }
					isolation_law = { add_realm_law = isolation_law }
					imperial_expansion_law = { add_realm_law = imperial_expansion_law }
					manor_reform_law = { add_realm_law = manor_reform_law }
					bandit_crackdown_law = { add_realm_law = bandit_crackdown_law }
				}
			}
		}
	}
}

#scripted stuff for Eradicate House cb post war events tgp_japan_general.9550

#valid courtiers to kill
scripted_trigger tgp_japan_soryo_eradicate_cb_valid_courtier = {
	NOR = {
		scope:loser_spouse ?= this
		scope:loser_heir ?= this
		is_close_family_of = scope:loser
	}
}
#valid house members to kill
scripted_trigger tgp_japan_soryo_eradicate_cb_valid_family = {
	NOT = {
		scope:loser_heir ?= this
	}
	OR = {
		is_close_family_of = scope:loser
		is_spouse_of = scope:loser
	}
}
#If loser isn't house head (i.e. has no manor), their heir becomes a landless adventurer. If not we move their manor to a friendly lord's land.
scripted_effect tgp_japan_soryo_eradicate_cb_heir_go_to_savior = {
	scope:loser_heir ?= {
		if = {
			limit = {
				NOT = {
				 	exists = scope:loser.domicile
				}
				has_dlc_feature = roads_to_power
			}
			create_landless_adventurer_title_effect = {
				REASON = flag:eradicate
				FLAVOR_CHAR = scope:winner
			}
		}
		else = {
			custom_tooltip = {
				text = tgp_japan_soryo_eradicate_cb_heir_move_manor_to_savior_tt
				scope:loser.domicile = {
					move_domicile = scope:savior.capital_province
				}
			}
		}
	}
}
#If loser successfully flees and isn't house head (i.e. has no manor), they becomes a landless adventurer. If not we move their manor to a friendly lord's land.
scripted_effect tgp_japan_soryo_eradicate_cb_loser_go_to_savior = {
	if = {
		limit = {
			NOT = {
			 	exists = scope:loser.domicile
			}
			has_dlc_feature = roads_to_power
		}
		create_landless_adventurer_title_effect = {
			REASON = flag:eradicate
			FLAVOR_CHAR = scope:winner
		}
	}
	else_if = {
		limit = {
			exists = scope:savior
		}
		custom_tooltip = {
			text = tgp_japan_soryo_eradicate_cb_heir_move_to_savior_tt
			scope:loser.domicile = {
				move_domicile = scope:savior.capital_province
			}
		}
	}
	else = {
		custom_tooltip = {
			text = tgp_japan_soryo_eradicate_cb_heir_move_to_cap_tt
			scope:loser.domicile = {
				move_domicile = scope:loser_heir.top_liege.capital_province
			}
		}
	}
}
#Loser kills loser's family to avoid dishonor
scripted_effect tgp_japan_soryo_eradicate_cb_kill_family = {
	scope:loser = {
		every_courtier = {
			custom = every_close_family_house_member_in_court
			custom_tooltip = is_killed_by_you_and_you_kill_self
			limit = {
				OR = {
					AND = {
						house = scope:loser.house
						is_close_family_of = scope:loser
					}
					is_spouse_of = scope:loser
				}
				NOT = {
					this = scope:loser_heir
				}
			}
			hidden_effect = {
				death = {
					death_reason = death_avoid_dishonor
					killer = scope:winner
				}
			}

		}
	}
}
#Winner kills loser's family
scripted_effect tgp_japan_soryo_eradicate_cb_kill_house = {
	scope:loser = {
		every_courtier = {
			custom = every_house_member_in_court
			limit = {
				OR = {
					is_spouse_of = scope:loser
					house = scope:loser.house
				}
				NOT = {
					scope:loser_heir ?= this
				}
			}
			death = {
				death_reason = death_eradicated
				killer = scope:winner
			}
		}
		house = {
			every_house_member = {
				custom = every_house_member_mad
				add_opinion = {
					modifier = eradicated_my_house_opinion
					target = scope:winner
				}
			}
		}
	}
}
#You lost Eradicate House cb war tgp_japan_general.9550
tgp_japan_general.9550 = {
	type = character_event
	title = tgp_japan_general.9550.t
	window = big_event_window
	desc = {
		first_valid = {
			#You started it, but lost
			triggered_desc = {
				trigger = {
					scope:loser = scope:attacker
				}
				desc = tgp_japan_general.9550.desc.intro.a
			}
			#You were attacked
			desc = tgp_japan_general.9550.desc.intro.b
		}
		desc = tgp_japan_general.9550.desc
		first_valid = {
			#You are alone
			triggered_desc = {
				trigger = {
					NOR = {
						exists = scope:loser_spouse
						exists = scope:loser_heir
					}
				}
				desc = tgp_japan_general.9550.desc.alone
			}
			#you are with your family
			desc = tgp_japan_general.9550.desc.fam
		}
		desc = tgp_japan_general.9550.desc.end
	}
	theme = death
	override_background = {
		reference = corridor_night
	}
	#war loser
	left_portrait = {
		character = scope:loser
		animation = beg
	}
	#spouse
	center_portrait = {
		trigger = { exists = scope:loser_spouse }
		character = scope:loser_spouse
		animation = crying
	}
	#heir
	right_portrait = {
		trigger = { exists = scope:loser_heir }
		character = scope:loser_heir
		animation = disbelief
	}
	#killed courtiers
	lower_left_portrait = {
		trigger = { exists = scope:dead_courtier_1 }
		character = scope:dead_courtier_1
	}
	lower_center_portrait = {
		trigger = { exists = scope:dead_courtier_2 }
		character = scope:dead_courtier_2
	}
	lower_right_portrait = {
		trigger = { exists = scope:dead_courtier_3 }
		character = scope:dead_courtier_3
	}

	immediate = {
		save_scope_as = loser
		if = {
			limit = {
				scope:loser = scope:attacker
			}
			scope:defender = {
				save_scope_as = winner
			}
		}
		else =  {
			scope:attacker = {
				save_scope_as = winner
			}
		}
		#Find close family if they exist
		#spouse
		scope:loser.primary_spouse ?= {
			save_scope_as = loser_spouse
		}
		#heir
		scope:loser.player_heir ?= {
			if = {
				limit = {
					is_courtier_of = scope:loser
				}
				save_scope_as = loser_heir
			}
		}
		#Find courtiers to kill
		ordered_courtier = {
			limit = {
				tgp_japan_soryo_eradicate_cb_valid_courtier = yes
			}
			#the more they like you the more it hurts
			order_by = "opinion(root)"
			max = 3
			#always put this when there is a max or it will be unhappy who knows why
			check_range_bounds = no
			if = {
				limit = { exists = scope:dead_courtier_2 }
				save_scope_as = dead_courtier_3
			}
			else_if = {
				limit = { exists = scope:dead_courtier_1 }
				save_scope_as = dead_courtier_2
			}
			else = { save_scope_as = dead_courtier_1 }
			death = {
				death_reason = death_eradicated
				killer = scope:winner
			}
		}

		#Find savior for you or your heir to flee to the lands of
		top_liege = {
			ordered_vassal = {
				limit = {
					NOT = {
						this = scope:loser
					}
					is_landed = yes
					highest_held_title_tier > tier_barony
				}
				order_by = "opinion(root)"
				save_scope_as = savior
			}
		}
	}

	#1) kill yourself (and family) to maintain honor
	option = {
		name = {
			text = {
				first_valid = {
					#alone
					triggered_desc = {
						trigger = {
							NOT = {
								exists = scope:loser_spouse
								exists = scope:loser_heir
							}
						}
						desc = tgp_japan_general.9550.a.alone
					}
					#if you have family
					desc = tgp_japan_general.9550.a.fam
				}
			}
		}
		flavor = {
			first_valid = {
				#alone
				triggered_desc = {
					trigger = {
						NOT = {
							exists = scope:loser_spouse
							exists = scope:loser_heir
						}
					}
					desc = tgp_japan_general.9550.a.alone.flavor
				}
				#fam
				desc = tgp_japan_general.9550.a.fam.flavor
			}
		}
		#Heir sets up manor in land of savior
		show_as_tooltip = {
			tgp_japan_soryo_eradicate_cb_heir_go_to_savior = yes
			tgp_japan_soryo_eradicate_cb_kill_family =  yes

			#then kill yourself
			death = {
				death_reason = death_avoid_dishonor
			}
		}

		house = {
			add_house_modifier = {
				modifier = house_eradicated_suicide_modifier
				years = house_eradicated_duration_value
			}
		}
		save_scope_as = suicide
		ai_chance = { base = 0}
	}
	#2) attempt to flee (lose honor but maybe keep life)
	option = {
		name = tgp_japan_general.9550.b
		flavor = tgp_japan_general.9550.b.flavor

		duel = {
			skill = intrigue
			value = high_skill_rating
			#If you win you escape and move your manor to a friendly lord's land or if you don't have manor you become an adventurer
			5 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = 1
				}
				modifier = {
					add = {
						value = prowess
						subtract = average_skill_rating
					}
				}
				modifier = {
					add = 1.5
					has_character_modifier = childhood_prison_escape
				}
				desc = tgp_japan_general.9550.b.success
				send_interface_toast = {
					title = tgp_japan_general.9550.b.success
					left_icon = scope:loser
					right_icon = scope:winner
					tgp_japan_soryo_eradicate_cb_loser_go_to_savior = yes
				}
				save_scope_as = flee_success
			}
			15 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = 1
				}
				modifier = {
					add = 2.5
					OR = {
						has_trait = weak
						has_trait = craven
						has_trait = physique_bad_1
					}
				}
				modifier = {
					add = 5
					OR = {
						has_trait = physique_bad_2
						has_trait = physique_bad_3
					}
				}
				desc = tgp_japan_general.9550.b.failure
				show_as_tooltip = {
					death = {
						death_reason = death_eradicated
						killer = scope:winner
					}
					tgp_japan_soryo_eradicate_cb_kill_house = yes
				}
				send_interface_toast = {
					title = tgp_japan_general.9550.b.failure
					left_icon = scope:loser
					right_icon = scope:winner
				}
			}
		}
		stress_impact = {
			brave = minor_stress_impact_gain
			craven = minor_stress_impact_loss
		}
		ai_chance = { base = 0}
		house = {
			add_house_modifier = {
				modifier = house_eradicated_modifier
				years = house_eradicated_duration_value
			}
		}
	}
	#3) put yourself to the mercy of your captor (AI only option so you can execute them)
	option = {
		name = tgp_japan_general.9550.c
		#only for AI
		trigger = {
			is_ai = yes
		}
		ai_chance = { base = 100}
		house = {
			add_house_modifier = {
				modifier = house_eradicated_modifier
				years = house_eradicated_duration_value
			}
		}
	}
	after = {
		scope:winner = {
			trigger_event = tgp_japan_general.9551
		}
	}
}
#You won eradicate house cb war
tgp_japan_general.9551 = {
	type = character_event
	window = big_event_window
	title = tgp_japan_general.9551.t
	desc = {
		first_valid = {
			#if you capture loser
			triggered_desc = {
				trigger = {
					NOR = {
						exists = scope:flee_success
						exists = scope:suicide
					}
				}
				desc = tgp_japan_general.9551.desc.intro
			}
			#intro for alt
			desc =  tgp_japan_general.9551.desc.alt.intro
		}
		first_valid = {
			#if loser suicides
			triggered_desc = {
				trigger = {
					exists = scope:suicide
				}
				desc = tgp_japan_general.9551.desc.alt.suicide
			}
			#if loser escapes
			triggered_desc = {
				trigger = {
					exists = scope:flee_success
				}
				desc = tgp_japan_general.9551.desc.alt.escape
			}
		}
		#outro for alt
		triggered_desc = {
			trigger = {
				OR = {
					exists = scope:flee_success
					exists = scope:suicide
				}
			}
			desc = tgp_japan_general.9551.desc.alt.outro
		}
	}

	theme = battle
	override_effect_2d = { reference = flies }
	#root winner
	left_portrait = {
		character = scope:winner
		triggered_animation = {
			trigger = {
				NOR = {
					exists = scope:flee_success
					exists = scope:suicide
				}
			}
			#off with his head!
			animation = chudan_no_kamae
		}
		#I didn't get to kill him!
		animation = anger
		camera = camera_event_center_coronation_kneeling
	}
	#loser
	right_portrait = {
		trigger = {
			NOR = {
				exists = scope:flee_success
				exists = scope:suicide
			}
		}
		character = scope:loser
		animation = anger
		camera = camera_event_near_angle_345_close
	}
	#freshly killed loser house members
	lower_left_portrait = {
		trigger = { exists = scope:portrait }
		character = scope:portrait
	}
	lower_center_portrait = {
		trigger = { exists = scope:dead_loser_family_2 }
		character = scope:dead_loser_family_2
	}
	lower_right_portrait = {
		trigger = { exists = scope:dead_loser_family_3 }
		character = scope:dead_loser_family_3
	}

	immediate = {
		#find loser's region
		scope:loser.capital_county.empire = {
		 save_scope_as = loser_region
		}
		if = {
			limit = { exists = scope:suicide }
			tgp_japan_soryo_eradicate_cb_heir_go_to_savior = yes
			tgp_japan_soryo_eradicate_cb_kill_family =  yes
		}
		#take land
		tgp_japan_soryo_eradicate_cb_transfer_effect = yes
		if = {
			limit = { exists = scope:suicide }
			scope:loser = {
				death = {
					death_reason = death_avoid_dishonor
					killer = scope:winner
				}
			}
		}
		scope:loser_heir ?= {
			#make loser_heir and nemesis or rival friends
			if = {
				limit = {
					can_set_relation_nemesis_trigger = {
						CHARACTER = scope:winner
					}
				}
				set_relation_nemesis = {
					reason = nemesis_killed_family
					target = scope:winner
				}
			}
			else_if = {
				limit = {
					can_set_relation_rival_trigger = {
						CHARACTER = scope:winner
					}
				}
				set_relation_rival = {
					reason = nemesis_killed_family
					target = scope:winner
				}
			}
			#make loser_heir and savior friends
			if = {
				limit = {
					can_set_relation_friend_trigger = {
						CHARACTER = scope:winner
					}
				}
				set_relation_friend = {
					reason = friend_eradication_save
					target = scope:savior
				}
			}
		}
		#grab some of the loser's house members to kill right away to help set the mood
		scope:loser = {
			ordered_courtier = {
				limit = {
					tgp_japan_soryo_eradicate_cb_valid_family = yes
				}
				order_by = "opinion(scope:loser)"
				max = 3
				check_range_bounds = no
				if = {
					limit = { exists = scope:dead_loser_family_2 }
					save_scope_as = dead_loser_family_3
				}
				else_if = {
					limit = { exists = scope:dead_loser_family_1 }
					save_scope_as = dead_loser_family_2
				}
				else = { save_scope_as = dead_loser_family_1 }
				death = {
					death_reason = death_eradicated
					killer = scope:winner
				}
			}
		}
		if = {
			limit = {
				OR = {
					exists = scope:suicide
					exists = scope:flee_success
				}
			}
			scope:loser = { save_scope_as = portrait }
		}
		else = {
			scope:dead_loser_family_1 ?= { save_scope_as = portrait }
		}
	}

	#1) Execute loser and eradicate the house!
	option = {
		name = tgp_japan_general.9551.a
		flavor = tgp_japan_general.9551.a.flavor
		show_as_tooltip = {	
			scope:loser = {
				death = {
					death_reason = death_avoid_dishonor
					killer = scope:winner
				}
			}
		}
		#kill house
		tgp_japan_soryo_eradicate_cb_kill_house = yes
		add_dread = medium_dread_gain
		house = {
			add_house_modifier = {
				modifier = rival_eradicated_modifier
				years = house_eradicated_duration_value
			}
		}
		#kill loser
		if = {
			limit = {
				NOT = { exists = scope:suicide }
			}
			scope:loser = {
				death = {
					death_reason = death_eradicated
					killer = scope:winner
				}
			}
		}
	}
}

# Event to force the historical birth of Emperor Antoku for Genpei content
tgp_japan_general.9910 = {
	type = character_event
	hidden = yes

	trigger = { has_tgp_dlc_trigger = yes }

	immediate = {
		add_trait = pregnant
		set_variable = {
			name = historical_pregnancy
			value = flag:antoku
			years = 1 # fallback cleanup
		}
		set_variable = {
			name = historical_pregnancy_start
			value = 430048 # ~1178.3.15
		}
		add_character_flag = {
			flag = birth_will_go_smoothly
			years = 1 # fallback cleanup
		}
		trigger_event = {
			id = birth.0001
			days = 82 # 1178.12.22
		}
		# Error suppression
		show_as_tooltip = { trigger_event = tgp_japan_general.9910 }
	}
}
