﻿historicinvasions_landless_invasion_war = { # mongol realm invasion
	group = invasion # event
	icon = historicinvasions_invasion_war_icon
	ai = no

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

	# NEW_INVASION
	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 = 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
	}
	
	# NEW_INVASION
	valid_to_start = { # only relevant for players (manual wars) - AI scripted wars use the target determined via script
		always = no
	}

	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_unlanded_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 = {
		### 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

		# SELJUK RUM INVASION
		scope:attacker = {
			if = {
				limit = {
					has_character_flag = is_first_rum_ruler
					NOR = {
						is_target_in_global_variable_list = {
							name = unavailable_unique_decisions
							target = flag:flag_formed_rum_sultanate
						}
						exists = title:k_rum.holder
						highest_held_title_tier > tier_kingdom
					}
					any_county_in_region = {
						region = special_seljuk_rum_conquest_region_de_jure
						percent >= 0.5
						OR = {
							holder = scope:attacker
							holder = { target_is_liege_or_above = scope:attacker }
						}
					}
				}
				save_scope_as = first_rum_ruler
				seljuk_form_rum_effect = yes
			}
		}

		# 4th CRUSADE
		scope:attacker = {
			if = {
				limit = {
					has_character_modifier = first_vlaanderen_modifier
					NOT = { exists = global_var:fourth_crusade_effect_triggered }
					has_title = title:e_vlaanderen
					scope:attacker = title:e_vlaanderen.holder
					scope:defender = title:e_byzantium.holder
				}
				shatter_byzantium_effect = yes
			}
		}

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

		### invader for some reason doesn't hold any counties
		# scope:attacker = {
		# 	if = {
		# 		limit = {
		# 			is_landless_ruler = yes
		# 		}
		# 		create_title_and_vassal_change = {
		# 			type = revoked
		# 			save_scope_as = title_change
		# 			add_claim_on_loss = yes
		# 		}
		# 		random_realm_county = {
		# 			limit = {
		# 				holder = {
		# 					is_ai = yes
		# 					NOT = { this = scope:attacker }
		# 				}
		# 			}
		# 			change_title_holder = {
		# 				holder = scope:attacker
		# 				change = scope:title_change
		# 			}
		# 		}
		# 		resolve_title_and_vassal_change = scope:title_change
		# 	}
		# }

		# 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 = no
	on_white_peace_desc = {
		desc = WHITE_PEACE_DISABLED
	}
	
	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 }

		### 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
		scope:attacker = {
			every_owned_story = {
				limit = { has_variable = lost_invasions_counter }
				change_variable = {
					name = lost_invasions_counter
					add = 1
				}
			}
		}

		# 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
			}
		}
	}
	
	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_delay = { months = 12 } # How much ticking War Score should be delayed before increasing for the Attacker
	defender_ticking_warscore_delay = { months = 36 } # How much ticking War Score should be delayed before increasing for the Defender
	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
}