﻿invasion_war = {
	icon = invasion
	group = invasion

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = {
		NOT = {
			trigger_if = {
				scope:attacker = {
						is_ai = no
				}
			}
		}
	}
	allow_hostages = no

	allowed_for_character = {
		OR = {
			government_has_flag = government_is_tribal
			government_has_flag = government_is_nomadic
			faith = {
				has_doctrine_parameter = invasion_cb_enabled
			}
			has_character_flag = bp2_inspired_culture_for_conquest
			has_variable = mpo_desperate_stance_invasion_cb_enable_var
			ai_should_get_extreme_conqueror_bonuses = yes
			AND = {
				house ?= { has_house_head_parameter = has_mandala_invasion_cb }
				is_house_head = yes
			}
		}
		trigger_if = {
			limit = { ai_should_get_extreme_conqueror_bonuses = no }
			NOT = {
				limit = {
					scope:attacker = { is_ai = yes }
				}
				has_character_flag = used_lifetime_invasion
			}
		}
		NOT = { government_has_flag = government_is_landless_adventurer }
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				has_character_flag =  bp2_inspired_culture_for_conquest
				ai_should_get_extreme_conqueror_bonuses = no
			}
			prestige_level >= 0
		}
		trigger_else_if = {
			limit = {
				government_has_flag = government_is_clan
				house ?= {
					has_house_unity_stage = antagonistic
				}
				culture = {
					has_cultural_parameter = cb_lower_prestige_level_requirement
				}
				ai_should_get_extreme_conqueror_bonuses = no
			}
			prestige_level >= 2
		}
		trigger_else_if = {
			limit = {
				OR = {
					AND = {
						government_has_flag = government_is_clan
						house ?= {
							has_house_unity_stage = antagonistic
						}
					}
					culture = {
						has_cultural_parameter = cb_lower_prestige_level_requirement
					}
				}
				ai_should_get_extreme_conqueror_bonuses = no
			}
			prestige_level >= 3
		}
		trigger_else_if = {
			limit = { ai_should_get_extreme_conqueror_bonuses = yes }
		}
		trigger_else = {
			prestige_level >= 4
		}

		custom_tooltip = {
			text = struggle_involvement_cb_check_failure
			struggle_blocks_invasion_conquest_cb_trigger = yes
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}
	
	allowed_against_character = {
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = all
	target_title_tier = kingdom
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	ai_can_target_all_titles = {
		can_use_viking_invasion_cbs_trigger = yes
	}
	ai_score_mult = {
		value = viking_conquest_ai_score_value
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value
		multiply = muslim_invasions_prefer_nonmuslims_ai_score_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	valid_to_start = {
		scope:target = {
			any_in_de_jure_hierarchy = {
				tier = tier_county
				neighboring_county_or_viking_conquest_trigger = { CHARACTER = root }
			}
		}
		scope:target = {
			trigger_if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 4
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						target_is_same_character_or_above = scope:defender
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_invasion_war_invalidated_message
	
	on_invalidated = {
		scope:attacker = {
			if = {
				limit = {
					is_alive = yes
				}
				remove_character_flag = used_lifetime_invasion # If the war is invalidated, you should be permitted to try again
			}
		}
	}

	cost = {
		piety = {
			if = {
				limit = {
					scope:attacker = {
						NOT = {
							government_has_flag = government_is_mandala
							house ?= { has_house_head_parameter = has_mandala_invasion_cb }
							is_house_head = yes
						}
					}
				}
				value = common_cb_impious_piety_cost
			}
			else = {
				value = {
					value = 2000
					desc = CB_BASE_COST
				}
				multiply = common_cb_piety_cost_multiplier
			}
		}
		prestige = {
			if = {
				limit = {
					scope:attacker = {
						government_has_flag = government_is_mandala
						house ?= { has_house_head_parameter = has_mandala_invasion_cb }
						is_house_head = yes
					}
				}
				value = 0
			}
			else = {
				value = {
					value = 2000
					desc = CB_BASE_COST
				}
				multiply = common_cb_prestige_cost_multiplier
			}
		}
	}
	
	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			add_character_flag = used_lifetime_invasion
		}
	}	

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = invasion_war_victory_desc_attacker
			}
			desc = invasion_war_victory_desc
		}
		triggered_desc = {
			trigger = { scope:attacker = { is_local_player = yes } }
			desc = invasion_war_warning_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		# Influence
		add_influence_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

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

		show_as_tooltip = {
			every_in_list = {
				list = target_titles
				custom_tooltip = invasion_title_transfer_tt
				custom_tooltip = invasion_title_transfer_2_tt
				every_in_de_jure_hierarchy = {
					limit = { holder.top_liege = scope:defender }
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
			}
			scope:defender = {
				every_sub_realm_county = {
					limit = {
						county_controller = scope:attacker
					}
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
			}
		}
		
		hidden_effect = {
			setup_invasion_cb = {
				titles = target_titles
				attacker = scope:attacker
				defender = scope:defender
				change = scope:change
				take_occupied = yes
			}
			resolve_title_and_vassal_change = scope:change
		}

		# Attacker gets Prestige Experience, Defender loses Prestige, all other participants gain Prestige based on their war contribution.
		modify_all_participants_fame_values = {
			WINNER = scope:attacker
			LOSER = scope:defender
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_invasion_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = 10
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		# Influence
		add_influence_attacker_defeat_effect = yes
		
		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 5
			}
			add_prestige = {
				value = massive_prestige_value
				multiply = -1.0
			}
		}
		
		setup_invasion_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}

		# Attacker loses Prestige, all other war participants gain Prestige (Defender gains full prestige, all allies on both sides gain based on war contribution).
		modify_all_participants_fame_values = {
			WINNER = scope:defender
			LOSER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = scale_10_war_defender_win
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
	
		add_truce_attacker_defeat_effect = yes
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = 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
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "INVASION_WAR_NAME"
	war_name_base = "INVASION_WAR_NAME_BASE"
	cb_name = "INVASION_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}

clan_invasion_war = {
	icon = vassalization
	group = invasion

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = {
		NOT = {
			trigger_if = {
				scope:attacker = {
						is_ai = no
				}
			}
		}
	}

	allow_hostages = no
	allowed_for_character = {
		government_has_flag = government_is_clan
		NAND = {
			house ?= {
				has_house_unity_parameter = unity_disable_invasions
			}
		}
		trigger_if = {
			limit = {
				house ?= {
					has_house_unity_parameter = unity_multiple_invasions
				}
			}
			NOT = { has_character_flag = unity_invasion_cooldown }
		}
		trigger_else_if = {
			limit = {
				house ?= {
					has_house_unity_parameter = unity_unlimited_invasions
				}
			}
			always = yes
		}
		trigger_else = {
			NOR = {
				if = {
					limit = {
						scope:attacker = { is_ai = yes }
					}
					has_character_flag = used_lifetime_invasion
					has_character_flag = pending_lifetime_invasion
				}
			}
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				government_has_flag = government_is_clan
				house ?= {
					has_house_unity_stage = antagonistic
				}
				culture = {
					has_cultural_parameter = cb_lower_prestige_level_requirement
				}
			}
			prestige_level >= 2
		}
		trigger_else_if = {
			limit = {
				OR = {
					AND = {
						government_has_flag = government_is_clan
						house ?= {
							has_house_unity_stage = antagonistic
						}
					}
					culture = {
						has_cultural_parameter = cb_lower_prestige_level_requirement
					}
				}
			}
			prestige_level >= 3
		}
		trigger_else = {
			prestige_level >= 4
		}

		custom_tooltip = {
			text = struggle_involvement_cb_check_failure
			struggle_blocks_invasion_conquest_cb_trigger = yes
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}
	
	allowed_against_character = {
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = all
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	ai_only_against_neighbors = yes

	ai_score_mult = {
		value = 1
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value
		multiply = muslim_invasions_prefer_nonmuslims_ai_score_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
	
	valid_to_start = {
		scope:target = {
			tier = tier_kingdom
		}
		scope:target = {
			trigger_if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 5
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_invasion_war_invalidated_message
	
	on_invalidated = {
		if = {
			limit = {
				scope:attacker = {
					is_ai = yes
				}
			}
			scope:attacker = {
				remove_character_flag = pending_lifetime_invasion # If the war is invalidated, you should be permitted to try again
				remove_character_flag = unity_invasion_cooldown # Reset cooldown as well
			}
		}
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			add = {
				value = 200
				desc = CB_BASE_COST
			}
			add = {
				value = 20
				multiply = scope:attacker.realm_size
				desc = CB_REALM_SIZE_COST
				max = 5000
			}

			multiply = common_cb_prestige_cost_multiplier
		}
	}
	
	on_declaration = {
		on_declared_war = yes
		if = {
			limit = {
				scope:attacker = {
					is_ai = yes
				}
			}
			scope:attacker = {
				add_character_flag = pending_lifetime_invasion
				add_character_flag = {
					flag = unity_invasion_cooldown
					years = unity_antagonistic_invasion_cooldown_value
				}
			}
		}
	}	

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = clan_invasion_war_victory_desc_attacker
			}
			desc = clan_invasion_war_victory_desc
		}
		triggered_desc = {
			trigger = { 
				scope:attacker = { 
					is_local_player = yes
					#Let's not display this warning if you have unlimited invasions
					NOT = {
						house ?= { has_house_unity_parameter = unity_unlimited_invasions }
					}
				}
			}
			desc = invasion_war_warning_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

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

		hidden_effect = {
			setup_invasion_cb = {
				titles = target_titles
				attacker = scope:attacker
				defender = scope:defender
				change = scope:change
				take_occupied = yes
			}
		}

		show_as_tooltip = {
			every_in_list = {
				list = target_titles
				if = {
					limit = {
						scope:defender.primary_title.tier >= scope:attacker.primary_title.tier
					}
					custom_tooltip = invasion_title_transfer_tt
					custom_tooltip = invasion_title_transfer_2_tt
				}
				every_in_de_jure_hierarchy = {
				 	limit = {
				 		holder.top_liege = scope:defender
				 	}
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
			}
			scope:defender = {
				every_sub_realm_county = {
					limit = {
						county_controller = scope:attacker
					}
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
				# Vassal transfer tooltip for display
				every_vassal = {
					limit = {
						primary_title = {
							any_this_title_or_de_jure_above = {
								is_in_list = target_titles			
							}
						}
					}
					custom = invasion_vassal_transfer_tt
					change_liege = {
						liege = scope:attacker
						change = scope:change
					}
				}
			}
		}	
		
		#If target is lower tier, vassalise them.
		if = {
			limit = {
				scope:defender.primary_title.tier < scope:attacker.primary_title.tier
			}
			scope:defender = {
				change_liege = {
					liege = scope:attacker
					change = scope:change
				}
			}
		}

		#If target has any personally-held titles of equal tier, excepting empires take them.
		#If target is equal tier,
		if = {
			limit = {
				#Conquering, or indirectly butchering, an entire empire in one war is a bit much.
				NOT = { scope:defender.primary_title.tier = tier_empire }
				scope:defender.primary_title.tier = scope:attacker.primary_title.tier
			}
			every_in_list = {
				list = target_titles
				limit = {
				 	holder.top_liege = scope:defender
			 	}
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
				}
			}
		}

		hidden_effect = { resolve_title_and_vassal_change = scope:change }

		# Attacker gets Prestige Experience, Defender loses Prestige, all other participants gain Prestige based on their war contribution.
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = {
				value = major_prestige_value
			}
			if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				add_character_flag = used_lifetime_invasion
				remove_character_flag = pending_lifetime_invasion
			}
		}

		# Prestige loss for the defender
		scope:defender = {
			add_prestige = {
				value = major_prestige_value
				multiply = -1.0
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
			if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				add_character_flag = used_lifetime_invasion
				remove_character_flag = pending_lifetime_invasion
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
		}

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 5
			}
			add_prestige = {
				value = massive_prestige_value
				multiply = -1.0
			}
		}
		
		setup_invasion_cb = {
			titles = target_titles
			attacker = scope:attacker
			defender = scope:defender
			claimant = scope:claimant
			victory = no
		}

		# Attacker loses Prestige, all other war participants gain Prestige (Defender gains full prestige, all allies on both sides gain based on war contribution).
		modify_all_participants_fame_values = {
			WINNER = scope:defender
			LOSER = scope:attacker
			FAME_BASE = scope:cb_prestige_factor # Set by 'setup_claim_cb'
			IS_RELIGIOUS_WAR = no
			WINNER_FAME_SCALE = scale_10_war_defender_win
			LOSER_FAME_SCALE = -10
			WINNER_ALLY_FAME_SCALE = 10
			LOSER_ALLY_FAME_SCALE = 10
		}
	
		add_truce_attacker_defeat_effect = yes
		
		scope:attacker = {
			save_temporary_scope_as = loser
			if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				add_character_flag = used_lifetime_invasion
				remove_character_flag = pending_lifetime_invasion
			}
		}
		on_lost_aggression_war_discontent_loss = 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
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "INVASION_WAR_NAME"
	war_name_base = "INVASION_WAR_NAME_BASE"
	cb_name = "CLAN_INVASION_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 500
}

mongol_invasion_war = {
	icon = invasion
	group = invasion

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = {
		NOT = {
			trigger_if = {
				scope:attacker = {
						is_ai = no
				}
			}
		}
	}

	allow_hostages = no
	allowed_for_character = {
		this = title:e_mongol_empire.holder
		prestige_level >= 1
		has_mpo_dlc_trigger = no
	}

	allowed_for_character_display_regardless = {
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	allowed_against_character = {
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	
	valid_to_start = {
		scope:target = {
			tier = tier_kingdom
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_mongol_invasion_war_invalidated_message
	
	on_invalidated = {
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			add = {
				value = 200
				desc = CB_BASE_COST
			}

			multiply = common_cb_prestige_cost_multiplier

			# Genghis Khan gets it for free
			if = {
				limit = { scope:attacker = { has_character_flag = is_temujin } }
				multiply = 0
			}
		}
	}
	
	on_declaration = {
		on_declared_war = yes
	}	

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = mongol_invasion_war_victory_desc_attacker
			}
			desc = mongol_invasion_war_victory_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

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

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equaly or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = CONQUEST_CB_TITLE
			every_in_de_jure_hierarchy = {
				continue = {
					OR = {
						NOT = {
							exists = holder
						}
						NOT = {
							holder = {
								target_is_liege_or_above = scope:defender
							}
						}
						AND = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith <= scope:defender.faith
								}
							}
							holder = {
								OR = {
									this = scope:defender
									target_is_liege_or_above = scope:defender
								}
							}
						}
					}
					tier > tier_county
				}
				limit = {
					#tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
				
				if = {
					limit = {
						OR = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith >= scope:defender.faith
								}
							}
							holder.primary_title.tier >= scope:attacker.primary_title.tier
						}
					}
					add_to_temporary_list = titles_taken
				}
				else = {
					holder = {
						add_to_temporary_list = vassals_taken
					}
				}
			}
		}

		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}

		hidden_effect = { resolve_title_and_vassal_change = scope:change }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = {
				value = mongol_invasion_cb_prestige_experience_gain
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
	
		# Truce
		add_truce_attacker_defeat_effect = yes
			
		# piety change
		every_in_list = {
			list = target_titles
			
			scope:defender = {
				add_prestige = mongol_invasion_cb_prestige_gain
			}

			scope:attacker = {
				if = {
					limit = {
						scope:defender = { is_ai = no }
					}
					if = {
						limit = {
							monthly_character_income > 0
						}
						pay_short_term_income = {
							years = 3
							target = scope:defender
						}
					}
					else = {
						pay_short_term_gold = {
							target = scope:defender
							gold = medium_gold_value
						}
					}
				}
			}
		}
		
		scope:attacker = {
			every_vassal = {
				custom = custom.every_vassal
				add_opinion = {
					modifier = liege_lost_mongol_invasion_war
					target = prev
				}
			}
		}
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = 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
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "MONGOL_INVASION_WAR_NAME"
	cb_name = "MONGOL_INVASION_KINGDOM_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 500

	ai_score_mult = {
		value = 1
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

mongol_realm_invasion_war = {
	icon = invasion
	group = invasion

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	allow_hostages = no
	allowed_for_character = {
		this = title:e_mongol_empire.holder
		is_ai = yes # To keep it clean and simple for players
		prestige_level >= 1
	}

	allowed_for_character_display_regardless = {
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	allowed_against_character = {
		scope:defender.top_liege != scope:attacker
		scope:attacker = {
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	use_de_jure_wargoal_only = yes
	ignore_effect = change_title_holder
	
	valid_to_start = {
		scope:target = {
			tier = tier_empire
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_mongol_invasion_war_invalidated_message
	
	on_invalidated = {
	}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			add = {
				value = 200
				desc = CB_BASE_COST
			}

			multiply = common_cb_prestige_cost_multiplier

			# Genghis Khan gets it for free
			if = {
				limit = { scope:attacker = { has_character_flag = is_temujin } }
				multiply = {
					value = 0
					desc = CB_GENGHIS_KHAN_COST
				}
			}

			# If fired by the Mongol Invasion story, don't charge a cost
			if = {
				limit = { scope:attacker = { has_character_flag = free_mongol_cb } }
				multiply = {
					value = 0
					desc = CB_MONGOL_INVASIONS_COST
				}
			}
		}
	}
	
	on_declaration = {
		on_declared_war = yes
	}	

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = invasion_war_victory_desc_attacker
			}
			desc = invasion_war_victory_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_very_high_effect = yes }

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

		# Target titles + dominant empires only
		scope:defender = {
			# Check whether any additional empires should be considered target titles
				# First check for the primary defender
			every_realm_de_jure_empire = {
				limit = {
					any_in_de_jure_hierarchy = { # Then check how many de jure counties are in the Mongol Emperor's realm
						count >= 8 # This number determines how many counties activates the threshold for taking these titles
						continue = {
							tier > tier_county
						}
						exists = this
						tier = tier_county
						exists = holder
						holder.top_liege = scope:attacker
					}
				}
				add_to_temporary_list = target_titles
			}
			# Mark any empire-level titles for destruction after transfer
			every_realm_de_jure_empire = {
				limit = {
					holder = scope:defender
					is_in_list = target_titles
				}
				add_to_list = titles_to_destroy
			}
		}
		
		show_as_tooltip = {
			every_in_list = {
				list = target_titles
				if = {
					limit = {
						scope:defender.primary_title.tier >= scope:attacker.primary_title.tier
					}
					custom_tooltip = invasion_title_transfer_tt
					custom_tooltip = invasion_title_transfer_2_tt
				}
				every_in_de_jure_hierarchy = {
				 	limit = {
				 		holder.top_liege = scope:defender
				 	}
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
			}
			scope:defender = {
				every_sub_realm_county = {
					limit = {
						county_controller = scope:attacker
					}
					change_title_holder = {
						holder = scope:attacker
						change = scope:change
					}
				}
				# Vassal transfer tooltip for display
				every_vassal = {
					limit = {
						primary_title = {
							any_this_title_or_de_jure_above = {
								is_in_list = target_titles			
							}
						}
					}
					custom = invasion_vassal_transfer_tt
					change_liege = {
						liege = scope:attacker
						change = scope:change
					}
				}
			}
		}

		# Most of the title transfers are handled here
		hidden_effect = {
			setup_invasion_cb = {
				titles = target_titles
				attacker = scope:attacker
				defender = scope:defender
				change = scope:change
				take_occupied = yes
			}
		}
		
		scope:defender = {
			# Loot tooltip for display
			every_held_county = {
				custom = mongol_invasion_loot_county_tt
				random = {
					chance = scope:attacker.martial
					custom_tooltip = mongol_invasion_loot_gold_tt
					custom_tooltip = mongol_invasion_loot_development_tt
					custom_tooltip = mongol_invasion_loot_dread_tt
				}
			}
			hidden_effect = {
				# Sub realm looting
				every_sub_realm_county = {
					custom = mongol_invasion_loot_chance_tt
					limit = {
						duchy.kingdom.empire = { is_in_list = target_titles }
					}
					mongol_invasion_county_looting_chance_effect = yes
				}
				# Vassal sub realm looting
				every_vassal_or_below = {
					every_sub_realm_county = {
						limit = {
							holder = { is_ai = yes }
							duchy.kingdom.empire = { is_in_list = target_titles }
						}
						mongol_invasion_county_looting_chance_effect = yes
					}
				}
			}
		}

		resolve_title_and_vassal_change = scope:change
		
		# Destroy any titles that should be destroyed
		every_in_list = {
			list = titles_to_destroy
			custom = invasion_titles_destroyed_tt
			save_scope_as = title_to_destroy
			scope:attacker = { destroy_title = scope:title_to_destroy }
		}
		# If the defender was vassalised, take any Kingdom title they hold
		scope:defender = {
			if = {
				limit = {
					target_is_liege_or_above = scope:attacker
					any_held_title = {
						title_tier = kingdom
					}
				}
				create_title_and_vassal_change = {
					type = conquest
					save_scope_as = change_two
					add_claim_on_loss = yes
				}
				every_held_title = {
					title_tier = kingdom
					change_title_holder = {
						holder = scope:attacker
						change = scope:change_two
					}
				}
				resolve_title_and_vassal_change = scope:change_two
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = {
				value = mongol_invasion_cb_prestige_experience_gain
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_high_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
	
		# Truce
		add_truce_attacker_defeat_effect = yes
			
		# piety change
		every_in_list = {
			list = target_titles
			
			scope:defender = {
				add_prestige = mongol_invasion_cb_prestige_gain
			}

			scope:attacker = {
				if = {
					limit = {
						scope:defender = { is_ai = no }
					}
					if = {
						limit = {
							monthly_character_income > 0
						}
						pay_short_term_income = {
							years = 3
							target = scope:defender
						}
					}
					else = {
						pay_short_term_gold = {
							target = scope:defender
							gold = medium_gold_value
						}
					}
				}
			}
		}
		
		scope:attacker = {
			custom_tooltip = {
				text = mongol_invasion_vassal_disapproves_defeat

			
				every_vassal = {
					custom = custom.every_vassal
					add_opinion = {
						modifier = liege_lost_mongol_invasion_war
						target = prev
					}
				}
			}
		}
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = 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
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "MONGOL_INVASION_WAR_NAME"
	cb_name = "MONGOL_INVASION_EMPIRE_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 500

	ai_score_mult = {
		value = 1
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

pax_romana_invasion_war = {
	icon = invasion
	group = invasion

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = {
		NOT = {
			trigger_if = {
				scope:attacker = {
						is_ai = no
				}
			}
		}
	}

	allow_hostages = no
	allowed_for_character = {
		is_roman_emperor_excluding_byzantium_trigger = yes
		government_has_flag = government_is_administrative
		any_owned_story = { type = ep3_story_cycle_restoring_rome }
		prestige_level >= 1
	}

	allowed_for_character_display_regardless = {
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	allowed_against_character = {
		scope:attacker = { top_liege != scope:defender.top_liege }
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	
	valid_to_start = {
		scope:target = {
			tier = tier_empire
		}
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_pax_romana_invasion_war_invalidated_message
	
	on_invalidated = {}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			add = {
				value = 200
				desc = CB_BASE_COST
			}

			multiply = common_cb_prestige_cost_multiplier
		}
	}
	
	on_declaration = {
		on_declared_war = yes
	}	

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = pax_romana_invasion_war_victory_desc_attacker
			}
			desc = pax_romana_invasion_war_victory_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

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

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equaly or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = CONQUEST_CB_TITLE
			every_in_de_jure_hierarchy = {
				continue = {
					OR = {
						NOT = {
							exists = holder
						}
						NOT = {
							holder = {
								target_is_liege_or_above = scope:defender
							}
						}
						AND = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith <= scope:defender.faith
								}
							}
							holder = {
								OR = {
									this = scope:defender
									target_is_liege_or_above = scope:defender
								}
							}
						}
					}
					tier > tier_county
				}
				limit = {
					holder ?= {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}
				
				if = {
					limit = {
						OR = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith >= scope:defender.faith
								}
							}
							holder.primary_title.tier >= scope:attacker.primary_title.tier
						}
					}
					add_to_temporary_list = titles_taken
				}
				else = {
					holder = {
						add_to_temporary_list = vassals_taken
					}
				}
			}
		}

		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}

		hidden_effect = { resolve_title_and_vassal_change = scope:change }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = {
				value = mongol_invasion_cb_prestige_experience_gain
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# 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 = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_effect = yes
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
	
		# Truce
		add_truce_attacker_defeat_effect = yes
			
		# piety change
		every_in_list = {
			list = target_titles
			
			scope:defender = {
				add_prestige = mongol_invasion_cb_prestige_gain
			}

			scope:attacker = {
				if = {
					limit = {
						scope:defender = { is_ai = no }
					}
					if = {
						limit = {
							monthly_character_income > 0
						}
						pay_short_term_income = {
							years = 3
							target = scope:defender
						}
					}
					else = {
						pay_short_term_gold = {
							target = scope:defender
							gold = medium_gold_value
						}
					}
				}
			}
		}
		
		scope:attacker = {
			every_vassal = {
				custom = custom.every_vassal
				add_opinion = {
					modifier = liege_lost_mongol_invasion_war
					target = prev
				}
			}
		}
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = 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
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "PAX_ROMANA_INVASION_WAR_NAME"
	cb_name = "PAX_ROMANA_INVASION_EMPIRE_CB_NAME"
	
	interface_priority = 100
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 500
	
	ai_score_mult = {
		value = 1
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}
