﻿turkic_migration_war = { # mongol realm invasion
	group = conquest
	icon = turkic_migration_war_icon
	ai = no

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

	allowed_for_character = { always = no }
	
	allowed_against_character = {
		NOT = { scope:defender.top_liege = scope:attacker }
		scope:attacker = {
			NOR = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = duchy
	target_de_jure_regions_above = yes
	use_de_jure_wargoal_only = yes
	ignore_effect = change_title_holder
	
	ai_score_mult = {
		value = 1

		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
	}
	
	valid_to_start = { # only relevant for players (manual wars) - AI scripted wars use the target determined via script
		# Struggle constraint
		struggle_blocks_invasion_conquest_cb_trigger = yes
	}

	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_turkic_migration_war_invalidated_message
	
	on_invalidated = {}

	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			add = {
				value = 150
				desc = CB_BASE_COST
			}
			multiply = common_cb_prestige_cost_multiplier
		}
	}
	
	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			turkic_migration_war_declaration_effect = 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 = {
		### release prisoners
		scope:attacker = { show_pow_release_message_effect = yes }

		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

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

		historicinvasions_invasion_war_victory_effect = yes
		turkic_migration_war_victory_effect = yes

		# GAME OVER protection for players
		scope:defender = {
			if = {
				limit = {
					is_ai = no
					is_landed = no
				}
				historicinvasions_prevent_player_game_over_effect = yes
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = turkic_migration_war_ally_prestige
			opinion = { modifier = contributed_in_war }
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = turkic_migration_war_ally_prestige
			opinion = { modifier = contributed_in_war }
		}
		
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = { value = turkic_migration_war_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 }
	}

	white_peace_possible = yes # If set to "no", white peaces can't happen; only victory, defeat, or invalidation
	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 = {
		### release prisoners
		scope:attacker = { show_pow_release_message_effect = yes }

		### EP2 accolade glory gain for doing pretty good against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = turkic_migration_war_ally_prestige
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = turkic_migration_war_ally_prestige
			opinion = {
				modifier = contributed_in_war
			}
		}

		add_truce_white_peace_effect = yes

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

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

		# destroy landless nomad title
		scope:attacker = {
			if = {
				limit = { has_variable = turkish_nomad_title }
				destroy_title = this.var:turkish_nomad_title
			}
		}
	}
	
	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 = {
		### release prisoners
		scope:attacker = { show_pow_release_message_effect = yes }

		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

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

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = turkic_migration_war_ally_prestige
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = turkic_migration_war_ally_prestige
			opinion = {
				modifier = contributed_in_war
			}
		}
	
		# Truce
		add_truce_attacker_defeat_effect = yes
			
		# piety change
		every_in_list = {
			list = target_titles
			
			scope:defender = {
				add_prestige = turkic_migration_war_prestige_gain
			}

			scope:attacker = {
				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 = {
				add_opinion = {
					modifier = liege_lost_historicinvasions_war
					target = prev
				}
			}
		}
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes

		# destroy landless nomad title
		scope:attacker = {
			if = {
				limit = { has_variable = turkish_nomad_title }
				destroy_title = this.var:turkish_nomad_title
			}
		}

		# make defender immune to nomad attacks for a while
		scope:defender = {
			add_character_modifier = {
				modifier = defended_from_turkish_nomad_modifier
				years = 10
			}
		}
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "turkic_migration_war_name"
	war_name_base = "turkic_migration_war_name_base"
	cb_name = "turkic_migration_war_cb_name"
	
	interface_priority = 101
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.75
	max_attacker_score_from_battles = 150
	max_attacker_score_from_occupation = 150
	full_occupation_by_defender_gives_victory = no
	attacker_capital_gives_war_score = no

	attacker_ticking_warscore_delay = { months = 12 } # How much ticking War Score should be delayed before increasing for the Attacker
	attacker_ticking_warscore = 0.1 # How much ticking warscore increases every day for the attacker
	attacker_wargoal_percentage = 0.25 # How much the Attacker need to occupy of a wargoal to gain ticking War Score. 0.0 means "at least one occupation"
	check_all_defenders_for_ticking_war_score = yes # All defenders land within the targeted title should count

	landless_attacker_needs_armies = yes
	
	max_ai_diplo_distance_to_title = 500
}