﻿# EK NOTE: removed byzantium value

vassalization_cb = {
	icon = vassalization
	group = vassalization
	ai_only_against_neighbors = yes
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150
	
	allowed_for_character = {
		OR = {
			has_perk = forced_vassalage_perk
			any_character_struggle = {
				involvement = involved
				has_struggle_phase_parameter = unlocks_forced_vassalization_casus_belli
			}
			AND = {
				has_trait = dragonborn
				prestige_level >= 3
				dynasty ?= { has_dynasty_perk = dragonblood_legacy_3 }
			}
			AND = {
				OR = {
					has_game_rule = default_conquerors_bonuses
					has_game_rule = extreme_conquerors_bonuses
					has_game_rule = random_conquerors_bonuses
					has_game_rule = reduced_random_conquerors_bonuses
				}
				has_variable = conqueror
			}
			ai_should_get_conqueror_bonuses = yes
		}
		NOT = {
			government_has_flag = government_is_landless_adventurer
		}
	}

	# Root is the title
	# scope:attacker is the attacker
	# scope:defender is the defender

	allowed_against_character_display_regardless = {
		scope:defender = {
			custom_description = {
				text = "vassalization_cb_target_too_many_counties"
				subject = scope:defender
				NOT = {
					any_sub_realm_county = {
						count > db_vassalization_size_limit
					}
				}
			}
		}
		scope:attacker = {
			is_landed = yes
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}
	
	allowed_against_character = {
		scope:defender = {
			top_liege = this
			trigger_if = {
				limit = { scope:attacker = { is_ai = no } } # Already pre-filtered by the AI due to ai_only_against_neighbors = yes. any_neighboring_top_liege_realm_owner is expensive
				scope:attacker = {
					any_neighboring_top_liege_realm_owner = {
						this = scope:defender
					}
				}
			}

			trigger_if = { # if unlocked without the perk, only target involved or interloper
				limit = {
					NOR = {
						scope:attacker = { has_perk = forced_vassalage_perk }
						scope:attacker = { ai_should_get_conqueror_bonuses = yes }
					}
				}
				any_character_struggle = {
					involvement = involved
				}
			}

			NOR = { # Can't vassalize holy orders or Heads of Faith
				any_held_title = {
					is_holy_order = yes
				}
				AND = {
					exists = faith.religious_head
					faith.religious_head = root
				}
			}
		}
		
		scope:attacker.primary_title.tier > scope:defender.primary_title.tier # Must be lower tier
		
	}
	
	cost = {
		piety = {
			value = 0
			add = common_cb_impious_piety_cost
		}
		prestige = {
			value = 0
			add = {
				desc = CB_SIZE_OF_TARGET_REALM_COST
				scope:defender = {
					every_sub_realm_county = {
						add = {
							value = 125
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender.primary_title = {
						tier = tier_kingdom
					}
				}
				multiply = {
					value = 2
					desc = CB_VASSALIZING_KING
				}
			}
			if = {
				limit = {
					scope:attacker = {
						faith = { has_doctrine_parameter = dragonborn_vassalization_cb_enabled }
						has_trait = dragonborn
					}
				}
				multiply = {
					value = 0.5
					desc = DB_CB_VASSALIZING
				}
			}
			multiply = common_cb_prestige_cost_multiplier
		}
	}
	
	should_invalidate = {
		OR = {
			scope:defender = {
				top_liege != this
			}
			scope:defender.primary_title.tier >= scope:attacker.primary_title.tier
		}
	}
	
	on_declaration = {
		on_declared_war = yes
	}	
	
	on_invalidated_desc = msg_vassalization_war_invalidated_message
	
	on_invalidated = {}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = vassalization_cb_victory_desc_attacker
			}
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = vassalization_cb_victory_desc_defender
			}
			desc = vassalization_cb_victory_desc
		}
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:defender = {
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
			resolve_title_and_vassal_change = scope:change
		}
		
		
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = {
				value = medium_prestige_value
			}
		}

		# Prestige loss for the defender
		scope:defender = {
			add_prestige = {
				value = medium_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

		# 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 = vassalization_cb_white_peace_desc_defender
			}
			desc = vassalization_cb_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:defender = { accolade_defender_war_end_glory_gain_low_effect = yes }

		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = minor_prestige_value
				multiply = -1.0
			}
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		# 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 = minor_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		
		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# Truce
		add_truce_white_peace_effect = yes

		# 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 = vassalization_cb_white_peace_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = vassalization_cb_defeat_desc_attacker
			}
			desc = vassalization_cb_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_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 for Defender
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = medium_prestige_value
			}
		}
		
		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 3
			}
			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:defender
			FAME_BASE = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}

		# Truce
		add_truce_attacker_defeat_effect = yes
		
		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
	}
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "VASSALIZATION_WAR_NAME"
	war_name_base = "VASSALIZATION_WAR_NAME_BASE"
	cb_name = "VASSALIZATION_CB_NAME"
	interface_priority = 59
	
	ticking_war_score_targets_entire_realm = yes
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	attacker_ticking_warscore = 0
	attacker_wargoal_percentage = 0.8
	
	max_ai_diplo_distance_to_title = 500

	ai_score_mult = {
		value = 1
		# As a feudal gov never attack tribal for vassalization
		add = {
			if = {
				limit = {
					scope:attacker = {
						NOT = { government_has_flag = government_is_tribal }
					}
					scope:defender = {
						government_has_flag = government_is_tribal
					}
				}
				add = -1000
			}
		}
		#Mandala really shouldn't be interested in this
		add = {
			if = {
				limit = {
					scope:attacker = { 
						government_has_flag = government_is_mandala
						vassal_limit_available < 1
					}
				}
				add = -1000
			}
		}
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# EK DISABLED
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		#multiply = byzantium_conquests_ai_score_value

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

# diarch_vassalisation_cb is kept in event wars due to being event-only & for performance reasons.
