# Triggers as a result of an Install Regent Faction demand being refused
replace_ceremonial_regent_faction_war = {
	icon = dissolution_war
	group = civil_war
	ai_only_against_liege = yes
	allow_hostages = no
	
	allowed_for_character = {
		scope:attacker = { is_leading_faction_type = replace_ceremonial_regent_faction }
	}

	allowed_against_character = {
		scope:attacker.liege = scope:defender
	}

	target_de_jure_regions_above = yes
	target_top_liege_if_outside_realm = no # In case of adventurers starting faction wars

	valid_to_start = {
	}

	on_declaration = {
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:defender = { is_local_player = yes }
				}
				desc = replace_ceremonial_regent_faction_war_victory_defender_desc
			}
			desc = replace_ceremonial_regent_faction_war_victory_desc
		}
	}

	on_victory = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			#EP2 Accolade glory gain from winning against higher ranked enemy
			accolade_attacker_war_end_glory_gain_med_effect = yes
		}
		# LEGITIMACY FROM LOSING FACTION WAR
		faction_war_end_defeat_legitimacy_effect = yes
		scope:defender = {
			add_prestige = -500
			add_character_flag = {
				flag = recent_replace_regent_faction_war
				years = liberty_war_victory_cooldown
			}
		}
		scope:defender = {
			faction_demand_regent_transfer_effect = {
				NEW_REGENT = scope:attacker
				REASON = faction_demand
			}
		}
		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = victory
				}
			}
			if = {
				limit = { 
					exists = scope:attacker.joined_faction 
				}
				scope:attacker.joined_faction = {
					save_scope_as = saved_faction
					add_faction_discontent = -200
					every_faction_member = {
						if = {
							limit = {
								exists = scope:attacker.joined_faction # Can get destroyed as we loop through
							}
							leave_faction_with_cooldown_effect = {
								FACTION = scope:attacker.joined_faction
								YEARS = liberty_war_victory_cooldown
							}
						}
						else = {
							add_faction_cooldown_effect = { YEARS = liberty_war_victory_cooldown }
						}
					}
				}
			}
			# The faction should have already been destroyed due to all members leaving above, but in case it hasn't, destroy it now.
			if = {
				limit = { exists = scope:saved_faction }
				scope:saved_faction = { destroy_faction = yes }
			}
		}
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = replace_ceremonial_regent_faction_war_white_peace_defender_desc
			}
			desc = replace_ceremonial_regent_faction_war_white_peace_desc
		}
	}

	on_white_peace = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}
		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = white_peace
				}
			}
		}
		scope:defender = {
			stress_impact = {
 				arrogant = medium_stress_impact_gain
 			}
			add_character_flag = {
				flag = recent_replace_regent_faction_war
				years = faction_war_white_peace_cooldown
			}
		}
		on_white_peace_faction_revolt_war = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = replace_ceremonial_regent_faction_war_defeat_defender_desc
			}
			desc = replace_ceremonial_regent_faction_war_defeat_desc
		}
	}

	on_defeat = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			save_scope_as = imprisoner
		}
		scope:defender = {
			add_character_flag = {
				flag = recent_replace_regent_faction_war
				years = faction_war_defeat_cooldown
			}
			add_dread = medium_dread_gain
			add_achievement_flag_effect = { FLAG = achievement_know_your_place_flag }
			# LEGITIMACY FROM WINNING FACTION WAR
			faction_war_end_victory_legitimacy_effect = yes
		}
		on_lost_faction_revolt_war = yes # Imprison all faction members, including the faction leader.
		scope:attacker = { # Imprison any war participant that isn't already in the faction.
			every_character_war = {
				limit = { is_defender = scope:defender }
				every_war_attacker = {
					limit = { is_imprisoned = no }
					hidden_effect = {
						hard_imprison_character_effect = {
							TARGET = this
							IMPRISONER = scope:defender
						}
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_invalidate_war_title

	check_attacker_inheritance_validity = no

	on_primary_attacker_death = inherit_faction
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = no
	defender_allies_inherit = yes

	war_name = "JAPANESE_REGENT_FACTION_WAR"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_attacker_score_from_battles = 100
	max_defender_score_from_battles = 50
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
}

# Triggers as a result of a Restore Ceremonial Liege demand being refused
restore_ceremonial_liege_faction_war = {
	icon = dissolution_war
	group = civil_war
	ai_only_against_liege = yes
	allow_hostages = no
	allowed_for_character = {
		scope:attacker = { is_leading_faction_type = restore_ceremonial_liege_faction }
	}

	allowed_against_character = {
		scope:attacker.liege = scope:defender
	}

	target_de_jure_regions_above = yes
	target_top_liege_if_outside_realm = no # In case of adventurers starting faction wars

	valid_to_start = {
	}

	on_declaration = {
	}

	on_victory_desc = {
		desc = restore_ceremonial_liege_faction_war_victory_desc
	}

	on_victory = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			#EP2 Accolade glory gain from winning against higher ranked enemy
			accolade_attacker_war_end_glory_gain_med_effect = yes
		}
		# LEGITIMACY FROM LOSING FACTION WAR
		faction_war_end_defeat_legitimacy_effect = yes
		scope:defender = {
			add_prestige = -500
			add_character_flag = {
				flag = recent_restore_ceremonial_liege_faction_war
				years = liberty_war_victory_cooldown
			}
			show_as_tooltip = {
				restore_ceremonial_liege_faction_reward_effect = yes
			}
		}
		show_as_tooltip = {
			scope:attacker = {
				imprison = {
					target = scope:defender
					type = house_arrest
				}
			}
		}
		hidden_effect = {
			rightfully_imprison_character_effect = {
				TARGET = scope:defender
				IMPRISONER = scope:attacker
			}
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = victory
				}
				joined_faction = {
					save_scope_as = saved_faction
					add_faction_discontent = -200
					every_faction_member = {
						if = {
							limit = {
								exists = scope:attacker.joined_faction # Can get destroyed as we loop through
							}
							leave_faction_with_cooldown_effect = {
								FACTION = scope:attacker.joined_faction
								YEARS = liberty_war_victory_cooldown
							}
						}
						else = {
							add_faction_cooldown_effect = { YEARS = liberty_war_victory_cooldown }
						}
					}
				}
				# The faction should have already been destroyed due to all members leaving above, but in case it hasn't, destroy it now.
				if = {
					limit = { exists = scope:saved_faction }
					scope:saved_faction = { destroy_faction = yes }
				}
			}
		}

		scope:attacker = {
			trigger_event = tgp_faction_events.0206
		}
	}

	on_white_peace_desc = {
		desc = restore_ceremonial_liege_faction_war_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}
		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = white_peace
				}
			}
		}
		scope:defender = {
			stress_impact = {
 				arrogant = medium_stress_impact_gain
 			}
			add_character_flag = {
				flag = recent_restore_ceremonial_liege_faction_war
				years = faction_war_white_peace_cooldown
			}
		}
		on_white_peace_faction_revolt_war = yes
	}

	on_defeat_desc = {
		desc = restore_ceremonial_liege_faction_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			save_scope_as = imprisoner
		}
		scope:defender = {
			add_character_flag = {
				flag = recent_restore_ceremonial_liege_faction_war
				years = faction_war_defeat_cooldown
			}
			add_dread = medium_dread_gain
			add_achievement_flag_effect = { FLAG = achievement_know_your_place_flag }
			# LEGITIMACY FROM WINNING FACTION WAR
			faction_war_end_victory_legitimacy_effect = yes
		}
		on_lost_faction_revolt_war = yes # Imprison all faction members, including the faction leader.
		scope:attacker = { # Imprison any war participant that isn't already in the faction.
			every_character_war = {
				limit = { is_defender = scope:defender }
				every_war_attacker = {
					limit = { is_imprisoned = no }
					hidden_effect = {
						hard_imprison_character_effect = {
							TARGET = this
							IMPRISONER = scope:defender
						}
					}
				}
			}
		}
		scope:claimant = {
			depose_effect = { DEPOSER = scope:defender }
		}
		on_lost_faction_revolt_war = yes
	}

	on_invalidated_desc = msg_invalidate_war_title

	check_attacker_inheritance_validity = no

	on_primary_attacker_death = inherit_faction
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = no
	defender_allies_inherit = yes

	war_name = "JAPANESE_RESTORE_EMPEROR_FACTION_WAR"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_attacker_score_from_battles = 100
	max_defender_score_from_battles = 50
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
}

# Triggers as a result of a Ceremonial Claimant Faction demand being refused
ceremonial_claimant_faction_war = {
	icon = claim_cb
	group = civil_war
	ai_only_against_liege = no
	target_titles = claim
	allow_hostages = no
	target_top_liege_if_outside_realm = no
	allowed_for_character = {
		scope:attacker = {
			is_leading_faction_type = ceremonial_claimant_faction
		}
	}

	allowed_against_character = {
		exists = scope:attacker.joined_faction
		scope:attacker.joined_faction = {
			special_title.holder = scope:defender
		}
	}

	valid_to_start = {}

	should_invalidate = {
		OR = {
			NOT = { exists = scope:attacker.joined_faction }
			NOT = {
				scope:attacker.joined_faction = {
					exists = special_character
					special_character = { is_alive = yes }
					has_special_title = yes
				}
			}
			scope:attacker.joined_faction.special_character = scope:attacker.joined_faction.special_title.holder
		}
	}

	on_invalidated_desc = msg_invalidate_war_title

	on_declaration = {
		#on_declared_war = yes
	}

	on_victory_desc = {
		desc = ceremonial_claimant_faction_war_victory_desc

	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 Accolade glory gain from winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }

		scope:defender = {
			add_character_flag = {
				flag = recent_ceremonial_claimant_faction_war
				years = liberty_war_victory_cooldown
			}
		}
		show_as_tooltip = {
			scope:attacker = {
				imprison = {
					target = scope:defender
					type = house_arrest
				}
			}
		}
		hidden_effect = {
			rightfully_imprison_character_effect = {
				TARGET = scope:defender
				IMPRISONER = scope:attacker
			}
		}
		scope:defender = {
			faction_demand_regent_transfer_effect = {
				NEW_REGENT = scope:attacker
				REASON = faction_demand
			}
		}

		random_in_list = {
			list = target_titles
			save_scope_as = target_title
		}

		on_ceremonial_claimant_faction_war_win_common = {
			TITLE = scope:target_title
			ATTACKER = scope:attacker
			DEFENDER = scope:defender
			CLAIMANT = scope:claimant
		}

		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = victory
				}
			}
		}

		# LEGITIMACY FROM LOSING FACTION WAR
		faction_war_end_defeat_legitimacy_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace_desc = {
		desc = ceremonial_claimant_faction_war_white_peace_desc

	}

	on_white_peace = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}

		scope:defender = {
			add_character_flag = {
				flag = recent_ceremonial_claimant_faction_war
				years = faction_war_white_peace_cooldown
			}
			stress_impact = {
 				arrogant = medium_stress_impact_gain
 			}
		}
		on_white_peace_faction_revolt_war = yes

		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = white_peace
				}
				if = {
					limit = { exists = joined_faction }
					joined_faction = {
						destroy_faction = yes # Destroy the faction if it wasn't already destroyed automatically.
					}
				}
			}
		}
	}

	on_defeat_desc = {
		desc = ceremonial_claimant_faction_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		on_lost_faction_revolt_war = yes

		scope:defender = {
			add_character_flag = {
				flag = recent_ceremonial_claimant_faction_war
				years = faction_war_white_peace_cooldown
			}
			add_dread = medium_dread_gain
			add_achievement_flag_effect = { FLAG = achievement_know_your_place_flag }

			# LEGITIMACY FROM WINNING FACTION WAR
			faction_war_end_victory_legitimacy_effect = yes
		}

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	check_attacker_inheritance_validity = no

	on_primary_attacker_death = inherit_faction
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = no
	defender_allies_inherit = yes

	war_name = "CEREMONIAL_CLAIMANT_WAR_NAME"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_attacker_score_from_battles = 50
	max_defender_score_from_battles = 100
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
}

# Triggers as a result of the Liberty Faction demand
imperial_policy_faction_war = {
	icon = dissolution_war
	group = civil_war
	ai_only_against_liege = yes
	allow_hostages = no
	allowed_for_character = {
		scope:attacker = {
			is_leading_faction_type = imperial_policy_faction
		}
	}

	allowed_against_character = {
		scope:attacker = {
			liege = scope:defender
		}
	}

	target_de_jure_regions_above = yes
	target_top_liege_if_outside_realm = no # In case of adventurers starting faction wars

	valid_to_start = {
	}

	on_declaration = {
	}

	on_victory_desc = {
		desc = imperial_policy_war_victory_desc
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 Accolade glory gain from winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }

		# LEGITIMACY FROM LOSING FACTION WAR
		faction_war_end_defeat_legitimacy_effect = yes

		scope:defender = {
			
			add_prestige = -500

			add_character_flag = {
				flag = recent_imperial_policy_faction_war
				years = liberty_war_victory_cooldown
			}
		}

		scope:attacker = {
			custom_tooltip = faction_demand.0222.tt
			trigger_event = {
				id = faction_demand.0227
				days = 1
			}
		}

		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = victory
				}
				joined_faction = {
					save_scope_as = saved_faction
					add_faction_discontent = -200
					every_faction_member = {
						if = {
							limit = {
								exists = scope:attacker.joined_faction # Can get destroyed as we loop through
							}
							leave_faction_with_cooldown_effect = {
								FACTION = scope:attacker.joined_faction
								YEARS = liberty_war_victory_cooldown
							}
						}
						else = {
							add_faction_cooldown_effect = { YEARS = liberty_war_victory_cooldown }
						}
					}
				}
				# The faction should have already been destroyed due to all members leaving above, but in case it hasn't, destroy it now.
				if = {
					limit = { exists = scope:saved_faction }
					scope:saved_faction = {
						destroy_faction = yes
					}
				}
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace_desc = {
		desc = imperial_policy_war_white_peace_end_desc
	}

	on_white_peace = {
		scope:attacker = {
			show_pow_release_message_effect = yes
			stress_impact = {
 				ambitious = medium_stress_impact_gain
 				arrogant = medium_stress_impact_gain
 			}
		}
		hidden_effect = {
			scope:attacker = {
				add_truce_both_ways = {
					character = scope:defender
					days = 1825
					war = root.war
					result = white_peace
				}
			}
		}
		scope:defender = {
			stress_impact = {
 				arrogant = medium_stress_impact_gain
 			}
			add_character_flag = {
				flag = recent_imperial_policy_faction_war
				years = faction_war_white_peace_cooldown
			}
		}
		on_white_peace_faction_revolt_war = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		desc = imperial_policy_war_defeat_end_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:defender = {
			add_character_flag = {
				flag = recent_imperial_policy_faction_war
				years = faction_war_defeat_cooldown
			}
			add_dread = medium_dread_gain
			add_achievement_flag_effect = { FLAG = achievement_know_your_place_flag }

			# LEGITIMACY FROM WINNING FACTION WAR
			faction_war_end_victory_legitimacy_effect = yes
		}
		on_lost_faction_revolt_war = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	on_invalidated_desc = msg_invalidate_war_title

	check_attacker_inheritance_validity = no

	on_primary_attacker_death = inherit_faction
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = no
	defender_allies_inherit = yes

	war_name = "IMPERIAL_POLICY_WAR_NAME"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_attacker_score_from_battles = 100
	max_defender_score_from_battles = 50
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
}
