
###TRIGGER LIST
#prisoner_gives_warscore_for_trigger - takes WAR and CHARACTER (primary enemy or one of their top 3 player heirs)

at_war_with_any_coreligionist_trigger = {
	any_character_war = {
		casus_belli = {
			OR = {
				primary_attacker = {
					is_at_war_with = root
					faith = root.faith
				}
				primary_defender = {
					is_at_war_with = root
					faith = root.faith
				}
			}
		}
	}
}

at_war_with_any_non_coreligionist_trigger = {
	any_character_war = {
		casus_belli = {
			OR = {
				primary_attacker = {
					is_at_war_with = root
					NOT = { faith = root.faith }
				}
				primary_defender = {
					is_at_war_with = root
					NOT = { faith = root.faith }
				}
			}
		}
	}
}

at_war_only_with_coreligionists_trigger = {
	is_at_war = yes
	NOT = {
		any_character_war = {
			casus_belli = {
				OR = {
					primary_attacker = {
						is_at_war_with = root
						NOT = { faith = root.faith }
					}
					primary_defender = {
						is_at_war_with = root
						NOT = { faith = root.faith }
					}
				}
			}
		}
	}
}

joiner_not_already_in_another_war_with_any_target_war_participants_trigger = {
	###Triggers to make sure the recipient doesn't end up in a war with someone they already have a war with###

	# $WARRIOR$ — the already-warring character, actor in call / recipient in offer
	# $JOINER$ — the ally, recipient in call / actor in offer

	#Set the primary enemy
	trigger_if = {
		limit = { scope:target = { primary_attacker = $WARRIOR$ } }
		scope:target = { primary_defender = { save_temporary_scope_as = primary_enemy } }
	}
	trigger_else = {
		scope:target = { primary_attacker = { save_temporary_scope_as = primary_enemy } }
	}

	$JOINER$ = {
		#recipient can't already be at war with the primary_enemy
		custom_description = {
			text = "is_already_war_enemy_of_primary_enemy"
			subject = $JOINER$
			object = scope:primary_enemy
			NOT = {
				any_war_enemy = { this = scope:primary_enemy }
			}
		}
		#recipient can't already be at war with another enemy in target war
		custom_description = {
			text = "is_war_enemy_with_another_target_war_enemy"
			subject = $JOINER$
			NOT = {
				any_war_enemy = {
					NOT = { this = scope:primary_enemy } #Already handled above
					OR = {
						AND = {
							scope:target = { primary_defender = scope:primary_enemy }
							is_defender_in_war = scope:target
						}
						AND = {
							scope:target = { primary_attacker = scope:primary_enemy }
							is_attacker_in_war = scope:target
						}
					}
				}
			}
		}
		#recipient can't have a war enemy who is on actor's side in this war
		custom_description = {
			text = "is_war_enemy_with_my_war_ally"
			subject = $JOINER$
			NOT = {
				any_war_enemy = {
					OR  = {
						AND = {
							scope:target = { primary_defender = $WARRIOR$ }
							is_defender_in_war = scope:target
						}
						AND = {
							scope:target = { primary_attacker = $WARRIOR$ }
							is_attacker_in_war = scope:target
						}
					}
				}
			}
		}
		#recipient can't be on the same side in another war with primary_enemy
		custom_description = {
			text = "is_war_ally_of_primary_enemy"
			subject = $JOINER$
			object = scope:primary_enemy
			NOT = {
				any_character_war = {
					OR = {
						AND = {
							is_defender = $JOINER$
							any_war_defender = { this = scope:primary_enemy }
						}
						AND = {
							is_attacker = $JOINER$
							any_war_attacker = { this = scope:primary_enemy }
						}
					}
				}
			}
		}
		#recipient can't be on the same side in another war with another enemy in the target war
		custom_description = {
			text = "is_war_ally_of_another_target_enemy"
			subject = $JOINER$
			NOT = {
				any_war_ally = {
					NOT = { this = scope:primary_enemy } #Already handled above
					OR = {
						AND = {
							scope:target = { primary_defender = scope:primary_enemy }
							is_defender_in_war = scope:target
						}
						AND = {
							scope:target = { primary_attacker = scope:primary_enemy }
							is_attacker_in_war = scope:target
						}
					}
				}
			}
		}
	}
}

can_potentially_call_ally_trigger = {
	# Can never call vassals to your own wars (their obligations are met via taxes and levies!)
	custom_description = {
		text = cannot_call_vassal_to_war
		$JOINER$ = {
			trigger_if = {
				limit = {
					# Diarchs can join against other vassals.
					NOT = { this ?= $WARRIOR$.diarch }
				}
				NOT = {
					target_is_liege_or_above = $WARRIOR$
				}
			}
		}
	}

	# Can never call your liege to your own wars (they protect you only from foreign invaders, by being the war target instead of you)
	custom_description = {
		text = cannot_call_liege_to_war
		$JOINER$ = {
			NOT = {
				target_is_vassal_or_below = $WARRIOR$
			}
		}
	}
}

war_declarer_needs_hook_on_liege = {
	scope:actor = {
		trigger_if = {
			limit = {
				is_independent_ruler = no
				liege = scope:recipient.liege
				NOT = { liege = scope:recipient }
				NAND = {
					has_government = administrative_government
					liege = { has_government = administrative_government }
				}
				liege = { has_realm_law_flag = vassal_internal_wars_banned }
				NOT = {
					vassal_contract_has_flag = vassal_contract_war_override
				}
			}
			always = yes
		}
		trigger_else_if = {
			limit = {
				is_independent_ruler = no
				NOT = { liege = scope:recipient }
				NAND = {
					has_government = administrative_government
					liege = { has_government = administrative_government }
				}
				liege = { has_realm_law_flag = vassal_all_wars_banned }
				NOT = {
					vassal_contract_has_flag = vassal_contract_war_override
				}
			}
			always = yes
		}
		trigger_else = {
			always = no
		}
	}
}

any_liege_or_above_is_participant_trigger = {
	save_temporary_scope_as = trigger_war

	$CHARACTER$ = {
		any_liege_or_above = {
			scope:trigger_war = { is_participant = prev }
		}
	}
}

can_use_conquest_cbs_trigger = {
	OR = {
		government_has_flag = government_is_tribal
		government_has_flag = government_is_clan
		faith = { has_doctrine_parameter = conquest_cb_enabled }
		culture = { has_cultural_parameter = can_use_conquest_cbs }
	}
}

can_be_warrior_trigger = {
	is_adult = yes
	is_imprisoned = no
	NOT = { has_trait = incapable }
	OR = {
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $ARMY_OWNER$ }
		AND = {
			$ARMY_OWNER$.culture = {
				has_cultural_parameter = high_prowess_ignores_knight_restrictions
			}
			prowess >= 10
		}
	}
}

can_be_knight_trigger = {
	is_hostage = no
	trigger_if = { # Admin governors cannot be knights
		limit = {
			$ARMY_OWNER$ = { has_government = administrative_government }
			is_courtier = no
		}
		primary_title ?= {
			tier < tier_duchy
			trigger_if = {
				limit = { is_noble_family_title = yes }
				always = no
			}
		}
	}
	trigger_if = {
		limit = {
			$ARMY_OWNER$.culture = {
				has_cultural_parameter = minimum_prowess_for_knights
			}
		}
		prowess >= 12
	}
	can_be_warrior_trigger = { ARMY_OWNER = $ARMY_OWNER$ }
	trigger_if = {
		limit = {
			OR = {
				has_trait = devoted
				is_clergy = yes
			}
		}
		OR = {
			faith = { has_doctrine_parameter = clergy_can_fight }
			culture = { has_cultural_parameter = culture_clergy_can_fight }
			AND = {
				$ARMY_OWNER$.culture = {
					has_cultural_parameter = high_prowess_ignores_knight_restrictions
				}
				prowess >= 10
			}
		}
	}
	OR = {
		AND = {
			$ARMY_OWNER$.culture = {
				has_cultural_parameter = high_prowess_ignores_knight_restrictions
			}
			prowess >= 10
		}
		has_trait = order_member # Order Members will fight for their faith even if their faith says no
		NOR = {
			has_trait = blind
			has_court_position = master_assassin_court_position #FP3 addition
		}
	}
	trigger_if = {
		limit = {
			exists = liege.diarch
		}
		NOT = { this = liege.diarch }
	}
	is_ai = yes
}

can_be_commander_basic_trigger = {
	is_alive = yes
	is_adult = yes
	is_hostage = no
	NOR = {
		has_trait = incapable
		AND = {
			OR = {
				has_trait = devoted
				is_clergy = yes
			}
			NOR = {
				has_trait = order_member # Order Members will fight for their faith even if their faith says no
				faith = { has_doctrine_parameter = clergy_can_fight }
				culture = { has_cultural_parameter = culture_clergy_can_fight }
			}
		}
	}

	OR = { # You can always lead your own armies
		this = $ARMY_OWNER$
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $ARMY_OWNER$ }
	}

	trigger_if = {
		limit = { $ARMY_OWNER$ = { is_ai = yes } }
		is_ai = yes
	}
}

can_be_commander_now_trigger = {
	can_be_commander_basic_trigger = { ARMY_OWNER = $ARMY_OWNER$ }
	is_imprisoned = no
	custom_description = {
		text = ALREADY_IN_ACTIVITY
		NOR = {
			exists = involved_activity
			has_variable = homage_liege_scope
			has_character_flag = meditation_character_flag
			has_character_flag = petition_liege_character_flag
			has_character_flag = holding_court_character_flag
			is_being_visited_on_tour_strict = yes
		}
	}
	# Must not be currently coming from or going somewhere
	is_travelling = no
}

get_valid_commander_list_trigger = {
	any_vassal = {
		add_to_temporary_list = temp$LIST$
	}
	any_courtier = {
		add_to_temporary_list = temp$LIST$
	}

	any_in_list = {
		list = temp$LIST$
		count = all
		can_be_commander_now_trigger = { ARMY_OWNER = scope:army_owner }

		add_to_temporary_list = $LIST$
	}
}

prisoner_gives_warscore_for_trigger = {
	save_temporary_scope_as = war_prisoner
	#Character getting war score is primary defender
	trigger_if = {
		limit = { $WAR$.primary_defender = $CHARACTER$ }
		$WAR$.primary_attacker = {
			OR = {
				this = scope:war_prisoner
				#player_heir_position = {
					#target = scope:war_prisoner
					#value <= 3
				#}
			}
		}
	}
	#Character getting war score is primary attacker
	trigger_else_if = {
		limit = { $WAR$ = { primary_attacker = $CHARACTER$ } }
		$WAR$.primary_defender = {
			OR = {
				this = scope:war_prisoner
				#player_heir_position = {
					#target = scope:war_prisoner
					#value <= 3
				#}
			}
		}
	}
	trigger_else = {
		always = no
	}
}

using_non_ghw_holy_war_cb_trigger = {
	OR = {
		using_cb = minor_religious_war
		using_cb = religious_war
		using_cb = major_religious_war
	}
}

using_holy_war_cb_trigger = {
	OR = {
		using_non_ghw_holy_war_cb_trigger = yes
		using_cb = undirected_great_holy_war
		using_cb = directed_great_holy_war
	}
}

using_de_jure_cb_trigger = {
	OR = {
		using_cb = claim_cb
		using_cb = de_jure_cb
		using_cb = individual_county_de_jure_cb
		using_cb = individual_duchy_de_jure_cb
		using_cb = imperial_reconquest_cb
		using_cb = dismantle_holy_pretender_cb
		using_cb = dismantle_byz_pretender_cb
		using_cb = norman_conquest_cb
		using_cb = norwegian_invasion_cb
	}
}

using_conquest_cb_trigger = {
	OR = {
		using_cb = county_conquest_cb
		using_cb = duchy_conquest_cb
		using_cb = ducal_conquest_cb
		using_cb = ireland_laudabiliter_conquest_cb
		using_cb = mongol_invasion_war
	}
}

using_hierarchical_cb_trigger = {
	OR = {
		using_cb = tribal_subjugation_cb
		using_cb = vassalization_cb
	}
}

# In case is_civil_war is causing issues.
using_civil_war_cb_trigger = {
	OR = {
		using_cb = independence_faction_war
		using_cb = liberty_faction_war
		using_cb = populist_war
		using_cb = claimant_faction_war
		using_cb = depose_war
		using_cb = refused_liege_demand_war
		using_cb = nation_fracturing_faction_war
		using_cb = peasant_war
	}
}

special_invasion_cb_seize_land_in_region_trigger = {
	OR = {
		AND = { # For Duchies
			tier = tier_duchy
			OR = {
				# Title is in the specified region
				capital_vassal = {
					title_province = {
						geographical_region = $TARGET_REGION$
					}
				}
				# Title is owned by the current holder of the invasion's target kingdom
				AND = {
					exists = holder
					holder = $TARGET_KINGDOM$.holder
					kingdom = $TARGET_KINGDOM$
				}
			}
		}
		AND = { # For Counties
			tier = tier_county
			OR = {
				# Title is in the specified region
				title_province = {
					geographical_region = $TARGET_REGION$
				}
				AND = {
					exists = holder
					holder = $TARGET_KINGDOM$.holder
					kingdom = $TARGET_KINGDOM$
				}
			}
			trigger_if = {
				# If our de jure liege is an existing duchy that would have been siezed in the duchy step, do not transfer the county (it has already been transferred).
				limit = {
					exists = de_jure_liege
					de_jure_liege = {
						exists = holder
						OR = {
							capital_vassal = {
								title_province = {
									geographical_region = $TARGET_REGION$
								}
							}
							AND = {
								exists = holder
								holder = $TARGET_KINGDOM$.holder
								kingdom = $TARGET_KINGDOM$
							}
						}
					}
				}
				always = no
			}
		}
	}
}

can_join_war_liege_vassal_check_trigger = {
	custom_description = {
		text = "basic_ally_can_join_because_liege_or_vassals"
		subject = $JOINER$

		# actor is defender, attackers do not include recipient's liege or vassals
		trigger_if = {
			limit = {
				$WARRIOR$ = { is_defender_in_war = scope:target }
			}
			$JOINER$ = {
				trigger_if = {
					limit = {
						# Diarchs can join against other vassals.
						NOT = { this ?= $WARRIOR$.diarch }
					}
					NOR = {
						any_liege_or_above = {
							is_attacker_in_war = scope:target
						}
						any_vassal_or_below = {
							is_attacker_in_war = scope:target
						}
					}
				}
			}
		}

		# actor is attacker, defenders do not include recipient's liege or vassals
		trigger_else_if = {
			limit = {
				$WARRIOR$ = { is_attacker_in_war = scope:target }
			}
			$JOINER$ = {
				NOR = {
					any_liege_or_above = {
						is_defender_in_war = scope:target
					}
					any_vassal_or_below = {
						is_defender_in_war = scope:target
					}
				}
			}
		}

		# it's fine, whatever
		trigger_else = {
			always = yes
		}
	}
}

pressing_claim_of_character_trigger = {
	custom_description = {
		subject = this
		object = $CHARACTER$
		text = pressing_claim_of_character_trigger_text
		save_temporary_scope_as = claim_presser
		any_character_war = {
			primary_attacker = scope:claim_presser
			exists = claimant
			claimant = $CHARACTER$
		}
	}
}

is_religious_war = {
	OR = {
		using_cb = minor_religious_war
		using_cb = religious_war
		using_cb = major_religious_war
		using_cb = excommunication_war
		using_cb = undirected_great_holy_war
		using_cb = directed_great_holy_war
	}
}

conquest_cb_holder_under_target_can_be_vassalized = {
	# If there is no holder they can't be vassalized!
	holder ?= {
		# We can only vassalize characters of a lower tier than us.
		highest_held_title_tier < scope:attacker.highest_held_title_tier
		# We can't vassalize Governors
		NOT = { government_has_flag = government_is_administrative }
		# Only (sub)vassals of the defender can be considered for transfer (no poaching vassals from 3rd parties without fighting them!)
		target_is_liege_or_above = scope:defender
		# *All* of a character's subrealm must be within the target area. If we're attacking for Jerusalem, we don't want to also be stealing parts of Egypt, Syria, etc.
		any_sub_realm_county = {
			count = all
			target_is_de_jure_liege_or_above = scope:target_title
		}
	}
}

religious_cb_holder_under_target_can_be_vassalized = {
	conquest_cb_holder_under_target_can_be_vassalized = yes

	# Religious Tolerance Check
	scope:attacker.faith = {
		OR = {
			# For all Faiths, if the attacker views this vassal's faith more favorably than the defender does, they are 'liberated' and become vassals under the attacker.
			# For example, if Catholics are crusading against Ash'aris(viewed as Evil), any Messalian vassals (viewed as Hostile) will become vassals under the Catholic attacker.
			faith_hostility_level_comparison = {
				prev.holder.faith < scope:defender.faith
			}

			# Pluralist faiths always attempt to preserve vassals if possible, even if the vassal follows an Evil faith.
			has_doctrine_parameter = pluralism_pluralistic_holy_wars_preserve_vassals
		}
	}
}

desirable_for_capture_trigger = {
	OR = {
		is_playable_character = yes
		any_parent = { always = yes }
		any_spouse = { is_playable_character = yes }
		any_close_family_member = { is_playable_character = yes }
		diplomacy >= very_high_skill_rating
		martial >= very_high_skill_rating
		stewardship >= very_high_skill_rating
		intrigue >= very_high_skill_rating
		learning >= very_high_skill_rating
		has_trait = lifestyle_physician
		has_trait = lifestyle_mystic
	}
}

can_use_viking_invasion_cbs_trigger = {
	OR = {
		# Vanilla behaviour.
		AND = {
			has_fp1_dlc_trigger = no
			can_use_viking_invasion_cbs_dlc_trigger = yes
		}
		# DLC behaviour..
		AND = {
			has_fp1_dlc_trigger = yes
			OR = {
				#Players are under no restrictions.
				AND = {
					is_ai = no
					can_use_viking_invasion_cbs_dlc_trigger = yes
				}
				#If the host has fp1, then we want Scandis to beat each other up and export the losers overseas, not form huge overseas empires themselves.
				AND = {
					can_use_viking_invasion_cbs_dlc_trigger = yes
					top_liege = {
						any_realm_county = {
							count = all
							title_province = {
								NOT = { geographical_region = world_europe_north }
							}
						}
					}
				}
			}
		}
	}
}

can_use_viking_invasion_cbs_dlc_trigger = {
	faith = { has_doctrine = tenet_warmonger }
	culture = { has_innovation = innovation_longboats }
	any_sub_realm_county = { is_coastal_county = yes }
	trigger_if = {
		limit = { is_ai = yes }
		prestige >= 75
		OR = {
			is_independent_ruler = yes
			primary_title.tier >= tier_kingdom
		}
		NOT = {
			any_liege_or_above = {
				NOT = {
					faith = { has_doctrine = tenet_warmonger }
				}
			}
		}
	}
}

coastal_or_neighboring_county_trigger = {
	OR = {
		is_coastal_county = yes
		any_neighboring_county = {
			holder = {
				target_is_same_character_or_above = $CHARACTER$
			}
		}
	}
}

# Non-vikings will for the AI just assert it's legal, as the target titles should already have fallen back to only allowing land and sea borders
neighboring_county_or_viking_conquest_trigger = {
	OR = {
		trigger_if = {
			limit = {
				$CHARACTER$ = {
					can_use_viking_invasion_cbs_trigger = yes
				}
			}
			is_coastal_county = yes
			OR = {
				$CHARACTER$ = { is_ai = no }
				is_neighbor_to_realm = $CHARACTER$
				$CHARACTER$ = {
					NOT = {
						any_sub_realm_title = {
							tier = tier_county
							NOT = {
								is_connected_to = {
									target = $CHARACTER$.capital_province.county
								}
							}
						}
					}
				}
			}
		}
		trigger_else = {
			$CHARACTER$ = { is_ai = no }
		}
		any_neighboring_county = {
			holder = {
				target_is_same_character_or_above = $CHARACTER$
			}
		}
	}
}

valid_struggle_cb_target_trigger = {
	any_neighboring_county = {
		holder = { target_is_same_character_or_above = $CHARACTER$ }
	}
}

prisoners_of_war_trigger = {
	OR = {
		scope:defender = {
			any_prisoner = {
				OR = {
					this = scope:war.primary_attacker
					scope:war.primary_attacker.primary_title = {
						place_in_line_of_succession = {
							target = prev
							value <= 3
						}
					}
				}
			}
		}
		#Prisoners of attackers
		scope:attacker = {
			any_prisoner = {
				OR = {
					this = scope:war.primary_defender
					scope:war.primary_defender.primary_title = {
						place_in_line_of_succession = {
							target = prev
							value <= 3
						}
					}
				}
			}
		}
	}
}

# Used for checking if a captured combatant is worth war-score.
character_is_potential_valuable_prisoner = {
	OR = {
		this = scope:primary_siege_attacker
		this = scope:primary_siege_defender
		is_heir_of = scope:primary_siege_attacker
		is_heir_of = scope:primary_siege_defender
	}
}

religious_war_vassals_constraints = {
	trigger_if = {
		limit = {
			scope:attacker = { is_independent_ruler = no }
		}
		scope:attacker = {
			NOT = { character_is_land_realm_neighbor = scope:defender }
		}
		scope:defender = { is_independent_ruler = yes }
	}
	trigger_else = {
		always = no
	}
}

feudal_clan_tribal_conquest_constraints = {
	OR = {
		AND = {
			tier = tier_county
			any_county_province = {
				has_holding_type = tribal_holding
			}
		}
		AND = {
			tier > tier_county
			any_in_de_jure_hierarchy = {
				tier = tier_county
				any_county_province = {
					has_holding_type = tribal_holding
				}
			}
		}
	}
	scope:attacker = {
		NOT = { government_has_flag = government_is_tribal }
		gold < 200
		any_sub_realm_county = {
			any_county_province = {
				has_holding_type = tribal_holding
			}
		}
	}
}
