﻿historicinvasions_invasion_war = { # mongol realm invasion
	group = invasion
	icon = historicinvasions_invasion_war_icon

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

	allowed_for_character = {
		historicinvasions_invasion_war_allowed_for_character_trigger = yes
		prestige_level >= 1
	}
	
	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 = all
	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
		historicinvasions_invasion_war_valid_to_start_trigger = yes
		# 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_historicinvasions_invasion_war_invalidated_message
	
	on_invalidated = {}

	cost = {}
	
	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			add_character_flag = used_lifetime_invasion
			add_character_flag = declared_major_religious_war_flag
			historicinvasions_spawn_army_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 = {
		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 }

		scope:defender = {
			capital_county = { save_scope_as = county_to_raze }
		}

		historicinvasions_invasion_war_victory_effect = yes

		# NEW_MIGRATION
		scope:attacker = {
			if = {
				limit = {
					#scope:target = { this = title:k_egypt }
					scope:attacker = title:e_fatimid.holder
					NOT = {
						is_target_in_global_variable_list = {
							name = historicinvasions_story_ended
							target = flag:fatimid_africa_story_ended
						}
					}
					exists = global_var:fatimid_wars_north_africa_ended
					NOT = {
						is_target_in_global_variable_list = {
							name = historicinvasions_migrations
							target = flag:fatimid_migration_complete
						}
					}
					any_county_in_region = {
						region = special_fatimid_conquest_region_4
						percent > 0.7
						OR = {
							holder = scope:attacker
							holder = { target_is_liege_or_above = scope:attacker }
						}
					}
				}
				historicinvasions_migration_effect_full = {
					MIGRATION_LEADER = scope:attacker
					MIGRATION_TARGET = title:k_egypt
					MIGRATION_TARGET_TIER = tier_kingdom
					MIGRATION_REGION = special_fatimid_conquest_region_4
					MIGRATION_DYNN = fatimid
					MIGRATION_COUNTY_AMOUNT = 9
				}
				every_vassal = {
					limit = { any_held_title = { is_head_of_faith = no } }
					trigger_event = {
						id = fatimid_invasion.1007
						days = 14
					}
				}
				if = {
					limit = { max_military_strength < 8000 }
					spawn_army = {
						name = fatimid_event_troops
						location = capital_province
						uses_supply = yes
						inheritable = no
						
						levies = {
							value = 250
						}
						men_at_arms = {
							type = camel_rider
							stacks = 5
						}
						men_at_arms = {
							type = sahel_horsemen
							stacks = 5
						}
					}
				}
				if = {
					limit = { exists = title:e_arabia.holder }
					if = {
						limit = { title:e_arabia.holder = { has_claim_on = title:k_egypt } }
						title:e_arabia.holder = { remove_claim = title:k_egypt }
					}	
				}
			}
			else = {
				#debug_log = "something went wrong with the historicinvasions_migration_war"
			}
		}

		# defender executed Mongol envoys; Mongols take revenge
		if = {
			limit = {
				scope:attacker = { has_title = title:e_mongol_empire }
				scope:defender = {
					is_ruler = yes
					is_landed = yes
					exists = dynasty
					has_character_flag = insulted_mongol_emperor_flag
				}
			}
			scope:defender = { borjigin_defender_insulted_mongol_emperor_effect = yes }
		}

		scope:defender = {
			# GAME OVER protection for players
			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 = historicinvasions_cb_ally_prestige
			opinion = { modifier = contributed_in_war }
		}

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

	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 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 }

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

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

		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = historicinvasions_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
			}
		}
	}
	
	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_very_high_effect = yes }

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

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = historicinvasions_cb_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 = historicinvasions_cb_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

		### keep track of failed invasion attempts
		if = {
			limit = { scope:defender = { realm_size >= 3 } }
			scope:attacker = {
				every_owned_story = {
					limit = { has_variable = lost_invasions_counter }
					change_variable = {
						name = lost_invasions_counter
						add = 1
					}
				}
			}
		}

		### end story due to defeat
		# player check to end invasions prematurely
		if = {
			limit = {
				has_game_rule = enabled_invasion_ends_after_player_win
				scope:war = { any_war_defender = { is_ai = no } }
				scope:attacker = { is_ai = yes }
			}
			debug_log = "ending an invasion story because the invader lost a historic invasion war to any defending human player"
			debug_log_date = yes
			scope:attacker = {
				add_character_flag = invasion_ends_after_player_win
			}
		}

		# invader A directly attacked invader B but lost - ending story of A
		if = {
			limit = {
				scope:war = { primary_defender = { has_character_modifier = historicinvasions_plot_armor } }
			}
			debug_log = "ending an invasion story because the invader lost a historic invasion war to another invader"
			debug_log_date = yes
			scope:attacker = {
				add_character_flag = invasion_ends_after_loss_to_invader
			}
		}

		# end story prematurely; special end condition for certain rebellions / uprisings
		if = {
			limit = {
				OR = {
					scope:attacker = { has_character_modifier = first_balgarsko_modifier }
					scope:attacker = title:k_ziyarid.holder
					scope:attacker = title:k_jannabid.holder
					scope:attacker = title:d_al-qays.holder
				}
			}
			debug_log = "ending an invasion story because the invader lost a historic invasion war and is a rebellion / uprising"
			debug_log_date = yes
			scope:attacker = {
				add_character_flag = invasion_ends_after_offensive_defeat
			}
		}
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "historicinvasions_invasion_war_name"
	war_name_base = "historicinvasions_invasion_war_name_BASE"
	cb_name = "historicinvasions_invasion_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 = 0.1 # How much ticking warscore increases every day for the attacker
	attacker_ticking_warscore_delay = { months = 12 } # How much ticking War Score should be delayed before increasing 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

	defender_ticking_warscore = 0.05
	defender_ticking_warscore_delay = { months = 24 }

	landless_attacker_needs_armies = yes
	
	max_ai_diplo_distance_to_title = 500
}