
##################################################
# General Triggers

fp2_should_use_iberian_graphics_trigger = {
	culture = { has_building_gfx = iberian_building_gfx }
}
 
fp2_character_any_involvement_iberian_struggle_trigger = {
	has_fp2_dlc_trigger = yes
	OR = {
		fp2_character_involved_in_struggle_trigger = yes
		fp2_character_interloper_in_struggle_trigger = yes
	}
}

fp2_character_involved_in_struggle_trigger = {
	has_fp2_dlc_trigger = yes
	any_character_struggle = {
		involvement = involved
		is_struggle_type = iberian_struggle
	}
}

fp2_character_interloper_in_struggle_trigger = {
	has_fp2_dlc_trigger = yes
	any_character_struggle = {
		involvement = interloper
		is_struggle_type = iberian_struggle
	}
}

fp2_character_uninvolved_in_struggle_trigger = {
	has_fp2_dlc_trigger = yes
	exists = struggle:iberian_struggle
	fp2_character_interloper_in_struggle_trigger = no
	fp2_character_involved_in_struggle_trigger = no
}

eligible_for_fp2_dynasty_legacies_trigger = {
	has_fp2_dlc_trigger = yes
	dynasty.dynast = {
		OR = {
			game_rule_unrestricted_dynasty_legacies_trigger = yes
			any_character_struggle = {
				involvement = involved
				is_struggle_type = iberian_struggle
			}
			culture = {
				has_cultural_pillar = heritage_iberian
			}
			capital_province ?= {
				geographical_region = world_europe_west_iberia
			}
		}
	}
}


#is_toledo_ownership_valid_trigger = {
#	title:c_toledo.holder = {
#		OR = {
#			this = root
#			liege = root
#			top_liege = root
#		}
#		faith = {
#			portrait_religious_faith_or_foundational_trigger = { FAITH = faith:mozarabic_church }
#		}
#	}
#}

##################################################
# Cultural & Regional Triggers

culture_valid_for_ritualised_best_friends_trigger = {
	culture = { has_cultural_parameter = strong_hooks_and_stress_effects_best_friends }
}

valid_ritualised_best_friendship_two_way_trigger = {
	$ACTOR_FRIEND$ = {
		# Check that they have the appropriate cultural parameter...
		culture_valid_for_ritualised_best_friends_trigger = yes
		# ... and that they've still got a best friend...
		any_relation = {
			type = best_friend
			this = $RECIPIENT_FRIEND$
			# ... who also still has the cultural parameter.
			culture_valid_for_ritualised_best_friends_trigger = yes
		}
	}
}

valid_ritualised_best_friendship_one_way_trigger = {
	$ACTOR_FRIEND$ = {
		# Check that they have the appropriate cultural parameter...
		culture_valid_for_ritualised_best_friends_trigger = yes
		# ... and that they've got a best friend...
		any_relation = {
			type = best_friend
			# ... who also still has the cultural parameter.
			culture_valid_for_ritualised_best_friends_trigger = yes
		}
	}
}

##################################################
# Illustration Triggers



##################################################
# Interaction Triggers

has_any_best_friend_synergy_bonus_modifier_trigger = {
	OR = {
		has_character_modifier = fp2_single_synergy_diplomacy_modifier
		has_character_modifier = fp2_single_synergy_martial_modifier
		has_character_modifier = fp2_single_synergy_stewardship_modifier
		has_character_modifier = fp2_single_synergy_intrigue_modifier
		has_character_modifier = fp2_single_synergy_learning_modifier
		has_character_modifier = fp2_double_synergy_diplomacy_diplomacy_modifier
		has_character_modifier = fp2_double_synergy_diplomacy_martial_modifier
		has_character_modifier = fp2_double_synergy_diplomacy_stewardship_modifier
		has_character_modifier = fp2_double_synergy_diplomacy_intrigue_modifier
		has_character_modifier = fp2_double_synergy_diplomacy_learning_modifier
		has_character_modifier = fp2_double_synergy_martial_diplomacy_modifier
		has_character_modifier = fp2_double_synergy_martial_martial_modifier
		has_character_modifier = fp2_double_synergy_martial_stewardship_modifier
		has_character_modifier = fp2_double_synergy_martial_intrigue_modifier
		has_character_modifier = fp2_double_synergy_martial_learning_modifier
		has_character_modifier = fp2_double_synergy_stewardship_diplomacy_modifier
		has_character_modifier = fp2_double_synergy_stewardship_martial_modifier
		has_character_modifier = fp2_double_synergy_stewardship_stewardship_modifier
		has_character_modifier = fp2_double_synergy_stewardship_intrigue_modifier
		has_character_modifier = fp2_double_synergy_stewardship_learning_modifier
		has_character_modifier = fp2_double_synergy_intrigue_diplomacy_modifier
		has_character_modifier = fp2_double_synergy_intrigue_martial_modifier
		has_character_modifier = fp2_double_synergy_intrigue_stewardship_modifier
		has_character_modifier = fp2_double_synergy_intrigue_intrigue_modifier
		has_character_modifier = fp2_double_synergy_intrigue_learning_modifier
		has_character_modifier = fp2_double_synergy_learning_diplomacy_modifier
		has_character_modifier = fp2_double_synergy_learning_martial_modifier
		has_character_modifier = fp2_double_synergy_learning_stewardship_modifier
		has_character_modifier = fp2_double_synergy_learning_intrigue_modifier
		has_character_modifier = fp2_double_synergy_learning_learning_modifier
	}
}

purchase_truce_interaction_recipient_can_de_jure_war_actor_trigger = {
	scope:recipient = {
		OR = {
			has_cb_on = {
				target = scope:actor
				cb = de_jure_cb
			}
			has_cb_on = {
				target = scope:actor
				cb = individual_county_de_jure_cb
			}
			has_cb_on = {
				target = scope:actor
				cb = individual_duchy_de_jure_cb
			}
		}
	}
}

fp2_struggle_contract_assistance_war_winning_trigger = {
	trigger_if = {
		limit = {
			scope:recipient = { is_attacker_in_war = prev }
		}
		attacker_war_score >= offer_assistance_interaction_already_winning_value
	}
	trigger_else = { defender_war_score >= offer_assistance_interaction_already_winning_value }
}

fp2_struggle_contract_assistance_war_losing_trigger = {
	trigger_if = {
		limit = {
			scope:recipient = { is_attacker_in_war = prev }
		}
		defender_war_score >= offer_assistance_interaction_already_losing_value
	}
	trigger_else = { attacker_war_score >= offer_assistance_interaction_already_losing_value }
}

fp2_struggle_contract_assistance_war_outnumbering_trigger = {
 	trigger_if = {
		limit = {
			scope:recipient = { is_attacker_in_war = prev }
		}
		war_attacker_total_strength_halved_value > war_defender_total_strength_value
	}
	trigger_else = { war_defender_total_strength_halved_value > war_attacker_total_strength_value }
 }

fp2_struggle_contract_assistance_war_outnumbered_trigger = {
 	trigger_if = {
		limit = {
			scope:recipient = { is_attacker_in_war = prev }
		}
		war_defender_total_strength_halved_value > war_attacker_total_strength_value
	}
	trigger_else = { war_attacker_total_strength_halved_value > war_defender_total_strength_value }
}

fp2_struggle_contract_assistance_war_outnumbered_minor_trigger = {
 	trigger_if = {
		limit = {
			scope:recipient = { is_attacker_in_war = prev }
		}
		war_defender_total_strength_value > war_attacker_total_strength_value
	}
	trigger_else = { war_attacker_total_strength_value > war_defender_total_strength_value }
}

fp2_struggle_contract_assistance_war_insufficient_trigger = {
	trigger_if = {
		limit = {
			scope:recipient = { is_attacker_in_war = prev }
		}
		scope:actor.max_military_strength < war_defender_total_strength_tenth_value
	}
	trigger_else = { scope:actor.max_military_strength < war_attacker_total_strength_tenth_value }
}

##################################################
# Religious Triggers



##################################################
# Achievement Triggers

##################################################
# Decision Triggers

##### Struggle Ending #####

### Common

# Checks a de jure kingdom of Hispania is completely controlled
fp2_struggle_ending_hold_de_jure_kingdom_trigger = {
	title:e_spain ?= {
		any_in_de_jure_hierarchy = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = {
				tier = tier_county
			}
			holder = root
			root = {
				completely_controls = prev
			}
		}
	}
}

# Checks less than half of Iberia owned
fp2_struggle_ending_percent_iberia_trigger = {
	any_county_in_region = {
		region = world_europe_west_iberia
		percent < fp2_struggle_compromise_owned_percent_decimal_value
		holder.top_liege = root
	}
}

# Checks no more than half of Iberia is owned by another
fp2_struggle_ending_other_percent_iberia_trigger = {
	struggle:iberian_struggle = {
		NOT = {
			any_involved_ruler = {
				exists = primary_title # Max figured out that is_independent_ruler causes errors if you are unlanded
				this != root
				top_liege = this
				primary_title = { is_mercenary_company = no }
				any_county_in_region = {
					region = world_europe_west_iberia
					percent > fp2_struggle_compromise_involved_percent_decimal_value
					holder.top_liege = prev
				}
			}
		}
	}
}

# Checks duchy is held by an independent ruler who owns at least half of it
fp2_struggle_ending_compromise_independent_duchy_trigger = {
	tier = tier_duchy
	# Is created
	exists = holder
	OR = {
		# Is not the heartland of an existing kingdom
		title_capital_county != de_jure_liege.title_capital_county
		# Capital duke doesn't control kingdom, edge case
		AND = {
			exists = de_jure_liege.holder
			holder != de_jure_liege.holder
		}
	}
	# Is ruled by an independent duke
	holder = {
		top_liege = this
		save_temporary_scope_as = duchy_holder
		primary_title = {
			is_mercenary_company = no
			OR = {
				tier = tier_duchy
				tier = tier_kingdom
			}
		}
	}
	OR = {
		this = holder.primary_title
		holder.primary_title.tier = tier_kingdom
	} 
	# Duke rules at least half the de jure duchy
	any_direct_de_jure_vassal_title = {
		percent >= 0.5
		holder.top_liege = scope:duchy_holder
	}
}

# Checks duchy should be split from de jure kingdom
fp2_struggle_ending_compromise_split_duchy_trigger = { 
	tier = tier_duchy
	# Has not been tampered with already
	NOT = { is_in_list = duchy_kingdom } 
	# Is not created
	NOT = { exists = holder } 
	# Is not heartland of an existing kingdom
	title_capital_county != de_jure_liege.title_capital_county 
	save_temporary_scope_as = duchy
	# Less than half is owned by de jure kingdom, if created
	trigger_if = { 
		limit = { exists = scope:duchy.de_jure_liege.holder }
		any_direct_de_jure_vassal_title = {
			percent < 0.5
			holder.top_liege = scope:duchy.de_jure_liege.holder.top_liege
		}
	}
	trigger_else = { always = no }
}

# Checks titular duchy should be made into de jure kingdom
fp2_struggle_ending_compromise_titular_trigger = {
	# Has not been tampered with already
	NOT = { is_in_list = duchy_kingdom }
	# Is created
	exists = holder
	# Is ruled by an independent duke
	holder = {
		top_liege = this
		primary_title = {
			is_mercenary_company = no
			tier = tier_duchy
		}
	}
	# Titular duke holds at least half of any de jure duchy of Hispania
	title:e_spain = {
		any_in_de_jure_hierarchy = {
			tier = tier_duchy
			any_direct_de_jure_vassal_title = {
				percent >= 0.5
				holder.top_liege = scope:special_duchy.holder
			}
		}
	}
}

# Should title be created by ststus quo decision?
fp2_struggle_ending_compromise_create_title_trigger = {
	NOR = {
		this = title:e_spain
		exists = holder
		is_in_list = assigned_title
		# Only if no other same rank rulers in title (e.g. multiple kings under empire)
		any_direct_de_jure_vassal_title = {
			exists = holder
			holder != scope:new_owner_temp
		}
	}
}

# Checks two cultures were Involved in the struggle that ended in Conciliation
fp2_struggle_conciliation_special_cultural_rules_trigger = {
	has_global_variable = fp2_struggle_conciliation_ending
	any_in_global_list = {
		variable = fp2_struggle_ending_culture_list
		this = $C1$.culture
	}
	any_in_global_list = {
		variable = fp2_struggle_ending_culture_list
		this = $C2$.culture
	}
}

# Checks two faiths were Involved in the struggle that ended in Conciliation
are_holy_wars_disabled_by_struggle_conciliation_trigger = {
	has_global_variable = fp2_struggle_conciliation_ending
	any_in_global_list = {
		variable = fp2_struggle_ending_faith_list
		this = scope:attacker.faith
	}
	any_in_global_list = {
		variable = fp2_struggle_ending_faith_list
		this = scope:defender.faith
	}
}

# Checks if characters were both involved in struggle
fp2_struggle_conciliation_recipient_actor_involved_trigger = {
	has_global_variable = fp2_struggle_conciliation_ending
	AND = {
		scope:actor.capital_province = { geographical_region = world_europe_west_iberia }
		scope:recipient.capital_province = { geographical_region = world_europe_west_iberia }
		# Offer comes from an Involved culture/faith character
		OR = {
			any_in_global_list = {
				variable = fp2_struggle_ending_faith_list
				this = scope:actor.faith
			}
			any_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				this = scope:actor.culture
			}
		}
		# Recipient is from an Involved culture/faith
		OR = {
			any_in_global_list = {
				variable = fp2_struggle_ending_faith_list
				this = scope:recipient.faith
			}
			any_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				this = scope:recipient.culture
			}
		}
	}
}

# Checks if character was involved in struggle
fp2_struggle_conciliation_scope_uninvolved_trigger = {
	has_global_variable = fp2_struggle_conciliation_ending
	OR = {
		NOR = {
			any_in_global_list = {
				variable = fp2_struggle_ending_faith_list
				this = $SCOPE$.faith
			}
			any_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				this = $SCOPE$.culture
			}
		}
		NOT = {
			$SCOPE$.capital_province = { geographical_region = world_europe_west_iberia }
		}
	}
}

fp2_struggle_compromise_transfer_duchy_trigger = {
	tier = tier_duchy
	save_temporary_scope_as = transfer_duchy 
	# Duchy is completely controlled by its capital's holder's top_liege
	exists = title_capital_county.holder.top_liege
	title_capital_county.holder.top_liege = { completely_controls = scope:transfer_duchy }
	# De jure liege does not control duchy
	trigger_if = {
		limit = { exists = kingdom.holder }
		kingdom != scope:transfer_duchy.title_capital_county.holder.top_liege.capital_county.kingdom
	}
	# Is controlled by a kingdom tier
	trigger_else = { title_capital_county.holder.top_liege.primary_title.tier >= tier_kingdom }
	# Duchy is connected de_jure to holders kingdom
	any_de_jure_county = {
		any_title_to_title_neighboring_and_across_water_county = {
			exists = kingdom.holder
			kingdom.holder = scope:transfer_duchy.title_capital_county.holder.top_liege
		}
	}
}

fp2_struggle_council_toledo_attendee_trigger = {
	OR = {
		has_trait = education_learning_3
		has_trait = education_learning_4
		has_trait = education_learning_5
	}
}

fp2_struggle_council_toledo_decision_trigger = {
	is_toledo_ownership_valid_trigger = yes
	is_at_war = no
}

fp2_struggle_secure_iberian_foothold_iberian_kingdom_trigger = {
	tier = tier_kingdom
	de_jure_liege = title:e_spain
	root = { completely_controls = prev }
	save_temporary_scope_as = iberian_kingdom_temp
}

fp2_struggle_secure_iberian_foothold_faith_trigger = {
	fp2_struggle_secure_iberian_foothold_faith_percent_value >= fp2_struggle_secure_iberian_foothold_faith_target_percent_value
}

fp2_struggle_secure_iberian_foothold_outsider_kingdom_trigger = {
	any_de_jure_county = {
		any_title_to_title_neighboring_and_across_water_county = {
			NOT = { empire = title:e_spain }
			kingdom.holder = root
		}
	}
}

fp2_struggle_iberian_reclamation_should_vassalize_trigger = {
	any_character_struggle = { is_struggle_type = iberian_struggle }
	any_held_title = { tier < scope:attacker.primary_title.tier }
	any_realm_county = {
		count = all
		duchy = { is_in_list = target_titles }
		any_county_struggle = { is_struggle_type = iberian_struggle }
	}
}

fp2_struggle_any_realm_county_in_hispania_trigger = {
	any_realm_county = { target_is_de_jure_liege_or_above = title:e_spain }
}

fp2_iberian_reclamation_vassalize_iberian_trigger = {
	tier = tier_county
	holder = {
		any_character_struggle = {
			involvement = involved
			is_struggle_type = iberian_struggle
		}
		liege = scope:defender
		this != scope:defender
		capital_county = {
			any_county_struggle = { is_struggle_type = iberian_struggle }
		}
	}
}

fp2_iberian_reclamation_involved_county_trigger = {
	any_realm_county = {
		any_county_struggle = { is_struggle_type = iberian_struggle }
	}
}

fp2_iberian_reclamation_vassalize_vassal_trigger = {
	primary_title.tier >= tier_county
	primary_title.tier < scope:attacker.primary_title.tier
	any_character_struggle = { is_struggle_type = iberian_struggle }
	capital_county = {
		duchy = { is_in_list = target_titles }
	}
	any_sub_realm_county = {
		count = all
		duchy = { is_in_list = target_titles }
		any_county_struggle = { is_struggle_type = iberian_struggle }
	}
}

fp2_struggle_enforce_truce_war_leader_trigger = {
	custom_tooltip = {
		text = fp2_enforce_truce_war_with_not_involved_tt
		any_character_struggle = {
			involvement = involved
		}
	}
	custom_tooltip = {
		text = fp2_enforce_truce_tier_difference_tt
		tier_difference = {
			target = scope:actor
			value <= 0
		}
	}
	NOR = {
		this = scope:recipient
		this = scope:actor
	}
	custom_tooltip = {
		text = fp2_enforce_truce_liege_or_independent_tt
		OR = {
			scope:recipient = { top_liege = this }
			scope:recipient.top_liege = scope:actor.top_liege
			top_liege = this
			top_liege = scope:actor.top_liege
		}
	}
}

fp2_bargain_fealty_war_outnumbered_trigger = {
	war_attacker_total_strength_halved_value > scope:recipient.max_military_strength
}

fp2_bargain_fealty_war_losing_trigger = {
	attacker_war_score >= fp2_bargain_fealty_interaction_already_losing_value
}

# We use a hard trigger so that this can be easily adjusted without overwriting the rest of the fp2_interactions file by modders.
struggle_can_access_unlocks_bargain_fealty_interaction_trigger = {
	# Just uhh, use an OR if you add something else.
	is_struggle_type = iberian_struggle
}

##################################################
# Multi-File Event Triggers
#
#fp2_lyonese_monk_0002_papal_hof_trigger = {
#	OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
#	exists = faith.religious_head
#	faith.religious_head_title = title:k_papal_state
#}
#
#fp2_lyonese_monk_0002_valid_character_configuration_trigger = {
#	OR = {
#		has_trait = scholar
#		has_trait = theologian
#		has_trait = lifestyle_mystic
#		AND = {
#			is_ai = no
#			learning >= very_high_skill_rating
#			has_lifestyle = learning_lifestyle
#		}
#	}
#}
#
#fp2_lyonese_monk_0002_valid_court_trigger = {
#	# Must have the Pope as a HoF.
#	fp2_lyonese_monk_0002_papal_hof_trigger = yes
#	# Filter out anyone not able to be at home when the monk comes a'knockin'.
#	is_physically_able = yes
#	# Must be into bookish discussions.
#	## Narratively, this is what attracts them to your court.
#	## Mechanically, this is how players can opt in for the chain so it's not completely random.
#	fp2_lyonese_monk_0002_valid_character_configuration_trigger = yes
#	# Remove non-playables.
#	NOR = {
#		government_has_flag = government_is_theocracy
#		government_has_flag = government_is_republic
#	}
#	# Plus, for various reasons, we don't want Lyon _itself_.
#	NOT = {
#		any_held_title = { this = title:c_lyon }
#	}
#}
#
#fp2_lyonese_monk_0000_valid_for_events_trigger = {
#	fp2_lyonese_monk_0000_out_of_realm_trigger = no
#	fp2_lyonese_monk_0000_out_of_freedom_trigger = no
#	fp2_lyonese_monk_0000_out_of_popes_trigger = no
#	fp2_lyonese_monk_0000_out_of_faith_trigger = no
#}
#
#fp2_lyonese_monk_0000_out_of_realm_trigger = {
#	scope:acolyte = {
#		OR = {
#			is_alive = no
#			is_imprisoned = yes
#			AND = {
#				is_landed_or_landless_administrative = no
#				NOT = {
#					host = scope:acolyte_host
#					# Make an exception if you're in prison, since that should give a different event.
#					scope:acolyte_host = { is_imprisoned = no }
#				}
#			}
#			AND = {
#				is_landed_or_landless_administrative = yes
#				NOT = {
#					any_liege_or_above = { this = scope:acolyte_host }
#				}
#			}
#		}
#	}
#}
#
#fp2_lyonese_monk_0000_out_of_freedom_trigger = { is_imprisoned = yes }
#
#fp2_lyonese_monk_0000_out_of_popes_trigger = {
#	NOT = { exists = scope:story.var:base_faith.religious_head }
#}
#
#fp2_lyonese_monk_0000_out_of_faith_trigger = {
#	OR = {
#		scope:acolyte.faith.religious_head != scope:story.var:base_faith.religious_head
#		scope:acolyte_host.faith.religious_head != scope:story.var:base_faith.religious_head
#	}
#}
#
#fp2_does_this_player_care_about_the_fate_of_iberia = {
#	this != root
#	OR = {
#		location = {
#			OR = { # Is in region
#				geographical_region = world_europe_west
#				geographical_region = world_africa_north
#				geographical_region = world_europe_south_italy
#			}
#		}
#		struggle:iberian_struggle = { is_culture_involved_in_struggle = prev.culture }
#	}
#}
#
#fp2_eligible_for_yearly_events_trigger = {
#	has_fp2_dlc_trigger = yes
#	OR = {
#		any_character_struggle = {
#			involvement = involved
#			is_struggle_type = iberian_struggle
#		}
#		culture = {
#			has_cultural_pillar = heritage_iberian
#		}
#		capital_province ?= {
#			geographical_region = world_europe_west_iberia
#		}
#	}
#}

fp2_purchase_truce_interaction_soft_requirements_trigger = {
	OR = {
		always = no
#		any_character_struggle = {
#			involvement = involved
#			has_struggle_phase_parameter = unlocks_truce_purchasing_interaction
#		}
		has_perk = defensive_negotiations_perk
		culture = { has_cultural_parameter = unlocks_purchase_truce }
	}
}
