﻿frankokratia_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 = {
		# cb解锁
		exists = var:launch_frankokratia_invasion_war
		# 不是拜皇
		NOR = {
			has_title = title:e_byzantium
			has_title = title:h_roman_empire
			has_title = title:h_eastern_roman_empire
		}
		# 独立
		is_independent_ruler = yes
	}
	allowed_against_character = {
		# 信仰不同
		faith != scope:attacker.faith
		OR = {
			culture = { has_cultural_pillar = heritage_byzantine }
			culture = { has_cultural_pillar = heritage_caucasian }
			has_title = title:e_byzantium
		}
		# 至少是国王
		highest_held_title_tier >= tier_kingdom
		# 控制任意拜占庭王国
		any_sub_realm_county = {
			any_this_title_or_de_jure_above = { this = title:e_byzantium }
		}
		# 独立
		is_independent_ruler = yes
		# 相邻
		scope:defender = {
			character_is_realm_neighbor = scope:attacker
		}
	}

	target_titles = neighbor_land_or_water
	target_title_tier = kingdom
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	ai_can_target_all_titles = { always = yes }
	ai_score_mult = {
		value = varangian_adventure_ai_targeting_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 = {
	}
	
	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 = {}

	cost = {
		piety = {
			value = {
				value = 2000
				desc = CB_BASE_COST
			}
			multiply = common_cb_piety_cost_multiplier
		}
		prestige = {
			value = {
				value = 2000
				desc = CB_BASE_COST
			}
			multiply = common_cb_prestige_cost_multiplier
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			remove_variable = launch_frankokratia_invasion_war
		}
	}

	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

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

		# 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
			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 = 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
		
		# 转移法理
		custom_tooltip = {
			text = frankokratia_invasion_war_set_de_jure_tt
			# scope:attacker = { frankokratia_invasion_war_set_de_jure_effect = yes }
			scope:attacker = { trigger_event = eoh_decisions.0007 }
		}
	}

	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 = "frankokratia_conquest_WAR_NAME"
	war_name_base = "frankokratia_conquest_NAME_BASE"
	cb_name = "frankokratia_invasion_war_NAME"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
}
