﻿tribal_subjugation_cb = {
	icon = vassalization
	group = subjugation
	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 = {
			government_has_flag = government_is_tribal
			has_variable = koh_subjugation_cb_allowed # Kingdom of Heaven allows this
			AND = {
				OR = {
					faith.religious_head = this
					pam_is_antipope_trigger = yes
				}
				faith ?= { has_doctrine = doctrine_temporal_head }
			}
			faith ?= {
				has_doctrine = eastern_hostility_doctrine
			}
			house ?= {
				has_variable = subjugations_expanded
			}
		}
		NOR = {
			government_has_flag = government_is_landless_adventurer
			AND = { # FP3: Renewed Caliphate houses with subjugations_expanded bypass the admin restriction
				government_has_mechanic = administrative
				NOT = {
					house ?= {
						has_variable = subjugations_expanded
					}
				}
			}
		}
	}

	# Root is the title
	# scope:attacker is the attacker
	# scope:defender is the defender
	allowed_against_character_display_regardless = {
		tgp_japan_offensive_wars_ban_trigger = yes
		scope:attacker = {
			trigger_if = {
				limit = {
					scope:attacker = { has_variable = koh_subjugation_cb_allowed }
				}
				scope:attacker.faith = scope:defender.faith
			}
			trigger_if = {
				limit = {
					faith.religion = religion:islam_religion
					house ?= {
						house_head = scope:attacker
						has_variable = subjugations_expanded
					}
					scope:defender.faith.religion = religion:islam_religion
				}
			}
			trigger_else = {
				trigger_if = {
					limit = {
						government_has_flag = government_is_tribal
						scope:defender = {
							government_has_flag = government_is_tribal
						}
					}
					trigger_if = {
						limit = {
							culture ?= {
								has_cultural_parameter = cb_lower_prestige_level_requirement
							}
						}
						prestige_level >= 1
					}
					trigger_else = {
						prestige_level >= 2
					}
				}
				trigger_else_if = {
					limit = {
						faith ?= { has_doctrine = doctrine_temporal_head }
						OR = {
							AND = {
								faith.religious_head = scope:attacker
								scope:defender.faith.religious_head ?= scope:attacker.faith.religious_head
							}
							AND = {
								pam_is_antipope_trigger = yes
								rite = scope:defender.rite
							}
						}
					}
					trigger_if = {
						limit = {
							OR = {
								culture ?= {
									has_cultural_parameter = cb_lower_piety_level_requirement
								}
								# Iberian Struggle Hostility Reward
								AND = {
									house ?= {
										OR = {
											has_house_modifier = fp2_struggle_hostility_house_faith_modifier
											has_house_modifier = fp2_struggle_hostility_house_combined_modifier
										}
									}
								}
							}
						}
						piety_level >= 2
					}
					trigger_else = {
						piety_level >= 3
					}
				}
				trigger_else = {
					trigger_if = {
						limit = {
							government_has_flag = government_is_clan
							house ?= {
								has_house_unity_stage = antagonistic
							}
							culture ?= {
								has_cultural_parameter = cb_lower_prestige_level_requirement
							}
						}
						prestige_level >= 2
					}
					trigger_else_if = {
						limit = {
							OR = {
								AND = {
									government_has_flag = government_is_clan
									house ?= {
										has_house_unity_stage = antagonistic
									}
								}
								culture ?= {
									has_cultural_parameter = cb_lower_prestige_level_requirement
								}
							}
						}
						prestige_level >= 3
					}
					trigger_else = {
						prestige_level >= 4
					}
				}
				custom_description = {
					text = has_used_lifetime_subjugation
					subject = scope:attacker
					NOT = {
						scope:attacker = {
							has_character_flag = used_lifetime_subjugation
						}
					}
				}
			}
		}
	}

	allowed_against_character = {
		scope:attacker = {
			OR = {
				AND = { # Kingdom of Heaven Subjugation
					has_variable = koh_subjugation_cb_allowed
					faith = scope:defender.faith
				}
				AND = { # Tribal Subjugation
					government_has_flag = government_is_tribal
					scope:defender = {
						government_has_flag = government_is_tribal
					}
				}
				AND = { # Temporal Subjugation
					faith ?= { has_doctrine = doctrine_temporal_head }
					OR = {
						AND = {
							faith.religious_head = scope:attacker
							scope:defender.faith.religious_head ?= scope:attacker.faith.religious_head
						}
						AND = {
							pam_is_antipope_trigger = yes
							rite = scope:defender.rite
						}
					}
				}
				AND = { # Indian Subjugation
					faith ?= {
						has_doctrine = eastern_hostility_doctrine
					}
					scope:defender.faith ?= {
						has_doctrine = eastern_hostility_doctrine
					}
				}
				AND = {
					faith.religion = religion:islam_religion
					house ?= {
						house_head = scope:attacker
						has_variable = subjugations_expanded
					}
					scope:defender.faith.religion = religion:islam_religion
				}
			}
		}

		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
					}
				}
			}
			NOR = { # Can't subjugate Military Orders or Heads of Faith
				any_held_title = {
					is_holy_order = yes
				}
				faith.religious_head ?= root
			}
		}
	}

	cost = {
		prestige = tribal_subjugation_cb_prestige_cost
		piety = {
			value = 0
			# Explicit cost when attacking HoF in addition to the impious additions
			if = {
				limit = {
					scope:attacker.faith.religious_head = scope:attacker
					scope:defender.faith = scope:attacker.faith
				}
				add = {
					value = 1000
					desc = CB_BASE_COST
				}
			}
			add = common_cb_impious_piety_cost
			multiply = common_cb_piety_cost_multiplier
			# FP3: caliphal discounts.
			## We have to do this here as we otherwise can't select what the war is.
			if = {
				limit = {
					scope:attacker = {
						has_title = title:d_sunni
						# We scope directly to the struggle because e_arabia might not actually be _in_ the struggle.
						struggle:persian_struggle ?= { has_struggle_phase_parameter = fp3_caliph_gets_cheaper_subjugation_cb }
					}
				}
				multiply = {
					value = fp3_caliph_gets_cheaper_subjugation_cb_actual_value
					desc = CB_STRUGGLE_PHASE_DISCOUNT
				}
			}
		}
	}

	should_invalidate = {
		scope:defender = {
			top_liege != this
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			add_character_flag = used_lifetime_subjugation
		}
	}

	on_invalidated_desc = msg_subjugation_war_invalidated_message

	on_invalidated = {
		scope:attacker = {
			if = {
				limit = {
					is_alive = yes
				}
				remove_character_flag = used_lifetime_subjugation # If the war is invalidated, you should be permitted to try again
				# Refund their investment.
				add_prestige_no_experience = tribal_subjugation_cb_prestige_cost
			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender.primary_title.tier < scope:attacker.primary_title.tier }
				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
					}
				}
			}
			desc = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:attacker = { is_local_player = yes } }
						desc = tribal_subjugation_cb_victory_desc_equal_tier_attacker
					}
					triggered_desc = {
						trigger = { scope:defender = { is_local_player = yes } }
						desc = tribal_subjugation_cb_victory_desc_equal_tier_defender
					}
					desc = tribal_subjugation_cb_victory_desc_equal_tier
				}
			}
		}
		triggered_desc = {
			trigger = { scope:attacker = { is_local_player = yes } }
			desc = subjugation_war_warning_desc
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

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

		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change
			add_claim_on_loss = no
		}

		if = {
			limit = {
				scope:defender.primary_title.tier < scope:attacker.primary_title.tier
			}
			scope:defender = {
				change_liege = {
					liege = scope:attacker
					change = scope:change
				}
			}
		}
		else = {
			scope:defender = {
				every_held_title = {
					if = {
						limit = {
							tier = scope:defender.primary_title.tier
						}
						change_title_holder = {
							holder = scope:attacker
							change = scope:change
						}
					}
					else_if = {
						limit = {
							OR = {
								NOT = { exists = scope:secondary_title }
								tier > scope:secondary_title.tier
							}
						}
						save_temporary_scope_as = secondary_title
					}
				}
			}

			scope:defender = {
				if = {
					limit = {
						exists = scope:secondary_title
					}
					every_vassal = {
						limit = {
							primary_title.tier >= scope:secondary_title.tier
							NOT = {
								primary_title.tier = tier_barony
							}
						}
						change_liege = {
							liege = scope:attacker
							change = scope:change
						}
					}
				}
			}

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

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

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

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = tribal_subjugation_cb_white_peace_desc_defender
			}
			desc = tribal_subjugation_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:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = medium_prestige_value
				multiply = -1.0
			}
			stress_and_fulfillment_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 = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

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

		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 = tribal_subjugation_cb_white_peace_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = tribal_subjugation_cb_defeat_desc_attacker
			}
			desc = tribal_subjugation_cb_white_peace_desc
		}
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Legitimacy
		add_legitimacy_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 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 = "SUBJUGATION_WAR_NAME"
	war_name_base = "SUBJUGATION_WAR_NAME_BASE"
	cb_name = "SUBJUGATION_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
		multiply = pam_peace_of_god_coreligionist_war_ai_score_value
		multiply = zealous_war_on_head_of_faith_ai_score_value
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}
