
###TRIGGER LIST

#################################
# RELATIONSHIP SETTING TRIGGERS #
#################################

### FRIEND ###
can_set_relation_potential_friend_trigger = {
	NOT = { has_relation_potential_friend = $CHARACTER$ }
	can_set_relation_friend_trigger = { CHARACTER = $CHARACTER$ }
}

can_set_relation_friend_trigger = {
	NOR = {
		has_relation_friend = $CHARACTER$
		has_relation_best_friend = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_nemesis = $CHARACTER$
	}
}

can_set_relation_best_friend_trigger = {
	NOR = {
		has_relation_best_friend = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_nemesis = $CHARACTER$
	}
	num_of_relation_best_friend = 0
	$CHARACTER$ = { num_of_relation_best_friend = 0 }
}

### LOVER ###
can_set_relation_potential_lover_trigger = {
	NOT = { has_relation_potential_lover = $CHARACTER$ }
	can_set_relation_lover_trigger = { CHARACTER = $CHARACTER$ }
}

can_set_relation_lover_trigger = {
	is_adult = yes
	is_attracted_to_gender_of = $CHARACTER$
	OR = {
		mgr_can_seduce = yes
		is_consort_of = $CHARACTER$
		$CHARACTER$ = { mgr_can_seduce = yes }
	}
	$CHARACTER$ = {
		is_adult = yes
		is_attracted_to_gender_of = prev
	}
	NOT = { has_relation_lover = $CHARACTER$ }
}

can_set_relation_soulmate_trigger = {
	is_adult = yes
	is_attracted_to_gender_of = $CHARACTER$
	$CHARACTER$ = {
		is_adult = yes
		is_attracted_to_gender_of = prev
	}
	NOR = {
		has_relation_rival = $CHARACTER$
		has_relation_nemesis = $CHARACTER$
	}
	num_of_relation_soulmate = 0
	$CHARACTER$ = { num_of_relation_soulmate = 0 }
}

### RIVAL ###
can_set_relation_potential_rival_trigger = {
	can_set_relation_rival_if_adult_trigger = { CHARACTER = $CHARACTER$ }
	NOT = { has_relation_potential_rival = $CHARACTER$ }
}

can_set_relation_rival_if_adult_trigger = {
	NOR = {
		has_relation_friend = $CHARACTER$
		has_relation_best_friend = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_nemesis = $CHARACTER$
	}
}

can_set_relation_rival_trigger = {
	is_adult = yes
	$CHARACTER$ = { is_adult = yes }
	can_set_relation_rival_if_adult_trigger = { CHARACTER = $CHARACTER$ }
}

can_set_relation_nemesis_trigger = {
	is_adult = yes
	$CHARACTER$ = { is_adult = yes }
	NOR = {
		has_relation_friend = $CHARACTER$
		has_relation_best_friend = $CHARACTER$
	}
	num_of_relation_nemesis = 0
	$CHARACTER$ = { num_of_relation_nemesis = 0 }
}

####################
# RELATION UPGRADE #
####################

# These are not hard blockers for upgrading relations, but are used in relation_upgrade events

### FRIEND
upgrade_ready_potential_friend_to_friend_trigger = {
	can_set_relation_friend_trigger = { CHARACTER = $CHARACTER$ }
}


### BEST FRIEND
upgrade_ready_friend_to_best_friend_trigger = {
	can_set_relation_best_friend_trigger = { CHARACTER = $CHARACTER$ }
}


### LOVER
upgrade_ready_potential_lover_to_lover_support_trigger = {
	OR = {
		is_ai = no #Players chose if they are ready
		trigger_if = {
			limit = { has_trait = chaste }
			opinion = {
				target = $CHARACTER$
				value >= high_positive_opinion
			}
		}
		trigger_else_if = {
			limit = {
				OR = {
					has_trait = lustful
					has_trait = seducer
					$CHARACTER$.attraction >= medium_positive_attraction
				}
			}
			opinion = {
				target = $CHARACTER$
				value >= 0
			}
		}
		trigger_else = {
			opinion = {
				target = $CHARACTER$
				value >= low_positive_opinion
			}
		}
	}
}

upgrade_ready_potential_lover_to_lover_trigger = {
	can_set_relation_lover_trigger = { CHARACTER = $CHARACTER$ }
	upgrade_ready_potential_lover_to_lover_support_trigger = { CHARACTER = $CHARACTER$ }
	save_temporary_scope_as = potential_lover_root
	$CHARACTER$ = { upgrade_ready_potential_lover_to_lover_support_trigger = { CHARACTER = scope:potential_lover_root } }
}

### SOULMATE
upgrade_ready_lover_to_soulmate_trigger = {
	can_set_relation_soulmate_trigger = { CHARACTER = $CHARACTER$ }
}


### RIVAL
upgrade_ready_potential_rival_to_rival_trigger = {
	can_set_relation_rival_trigger = { CHARACTER = $CHARACTER$ }
}

### NEMESIS
upgrade_ready_rival_to_nemesis_trigger = {
	can_set_relation_nemesis_trigger = { CHARACTER = $CHARACTER$ }
}





########################
# AI RELATIONSHIP CAPS #
########################

# Soft caps, mostly for schemes

is_below_ai_friend_soft_cap_trigger = {
	trigger_if = {
		limit = { is_ai = yes }

		trigger_if = {
			limit = {
				OR = {
					highest_held_title_tier >= tier_kingdom
					has_trait = gregarious
				}
			}
			num_of_relation_friend < 3
		}
		trigger_else = {
			num_of_relation_friend < 1
		}
	}
	trigger_else = {
		always = yes
	}
}

is_below_ai_lover_soft_cap_trigger = {
	trigger_if = {
		limit = { is_ai = yes }

		trigger_if = {
			limit = {
				has_trait = lustful
				has_trait = seducer
				has_focus = intrigue_temptation_focus
			}
			num_of_relation_lover < 3
		}
		trigger_else = {
			num_of_relation_lover < 1
		}
	}
	trigger_else = {
		always = yes
	}
}


###############################
# Character priority triggers #
###############################

has_important_relationship_with_root_trigger = {
	OR = {
		has_relation_lover = root
		has_relation_rival = root
		has_relation_friend = root
	}
}

has_friendly_relationship_with_root_trigger = {
	OR = {
		has_relation_lover = root
		has_relation_friend = root
	}
}

has_any_good_relationship_with_root_trigger = {
	OR = {
		is_spouse_of = root
		betrothed = root
		has_relation_lover = root
		has_relation_potential_lover = root
		has_relation_crush = root
		has_relation_friend = root
		has_relation_potential_friend = root
		has_relation_guardian = root
		has_relation_ward = root
	}
}

has_any_bad_relationship_with_root_trigger = {
	OR = {
		has_relation_potential_rival = root
		has_relation_rival = root
		has_relation_victim = root
		has_relation_bully = root
	}
}

lover_is_secret_trigger = {
	any_secret = {
		secret_type = secret_lover
		secret_target = $CHARACTER$
	}
}



###############################################
# Triggers that take an argument and NOT root
##############################################

# List of all localizable relations (e.g, My vassal Alex, My courtier Lisa, My liege Frank, My cousin Teddy, etc.)
has_any_relation_trigger = {
	NOT = { this = $CHARACTER$ }
	OR = {
		# They are our liege
		AND = {
			exists = $CHARACTER$.liege
			this = $CHARACTER$.liege
		}
		# We are their liege
		is_vassal_of = $CHARACTER$
		is_courtier_of = $CHARACTER$
		is_councillor_of = $CHARACTER$

		# They are our heir/spouse/familyetc.
		is_heir_of = $CHARACTER$
		is_consort_of = $CHARACTER$
		is_close_or_extended_family_of = $CHARACTER$

		#They are the head of our faith
		AND = {
			exists = $CHARACTER$.faith.religious_head
			this = $CHARACTER$.faith.religious_head
		}

		# Other relations
		has_relation_lover = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_friend = $CHARACTER$
		has_relation_guardian = $CHARACTER$
		has_relation_ward = $CHARACTER$
	}
}

is_of_major_interest_trigger = {
	NOT = { this = $CHARACTER$ }
	OR = {
		is_heir_of = $CHARACTER$
		AND = {
			exists = $CHARACTER$.liege
			this = $CHARACTER$.liege
		}
		is_powerful_vassal_of = $CHARACTER$
		is_consort_of = $CHARACTER$
		is_councillor_of = $CHARACTER$
		AND = {
			exists = $CHARACTER$.player_heir
			this = $CHARACTER$.player_heir
		}

		#They are the head of our faith
		AND = {
			exists = $CHARACTER$.faith.religious_head
			this = $CHARACTER$.faith.religious_head
		}

		trigger_if = {
			limit = {
				exists = liege
				exists = $CHARACTER$.liege
			}
			is_powerful_vassal = yes
			liege = $CHARACTER$.liege
		}
		trigger_if = {
			limit = {
				is_independent_ruler = yes
			}
			any_neighboring_top_liege_realm_owner = {
				this = $CHARACTER$
			}
		}
		#House Head
		trigger_if = {
			limit = {
				exists = $CHARACTER$.house.house_head
			}
			this = $CHARACTER$.house.house_head
		}
		is_child_of = $CHARACTER$
	}
}

is_of_minor_interest_trigger = {
	NOT = { this = $CHARACTER$ }
	OR = {
		is_vassal_of = $CHARACTER$
		betrothed = $CHARACTER$
		has_relation_lover = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_friend = $CHARACTER$
		has_relation_guardian = $CHARACTER$
		has_relation_ward = $CHARACTER$
		AND = {
			$CHARACTER$ = { target_is_liege_or_above = prev }
			NOT = { this = $CHARACTER$.liege }
		}
		#"Highly skilled courtiers"
		AND = {
			is_courtier_of = $CHARACTER$
			OR = {
				martial > high_skill_rating
				diplomacy > high_skill_rating
				intrigue > high_skill_rating
				learning > high_skill_rating
				stewardship > high_skill_rating
				prowess > high_skill_rating
			}
		}
		is_close_or_extended_family_of = $CHARACTER$
	}
}

is_of_major_or_minor_interest_trigger = {
	OR = {
		is_of_major_interest_trigger = { CHARACTER = $CHARACTER$ }
		is_of_minor_interest_trigger = { CHARACTER = $CHARACTER$ }
	}
}
has_important_relationship_trigger = {
	OR = {
		has_relation_lover = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_friend = $CHARACTER$
	}
}

has_friendly_relationship_trigger = {
	OR = {
		has_relation_lover = $CHARACTER$
		has_relation_friend = $CHARACTER$
	}
}



###################################
# Root argument versions
###################################

is_of_major_interest_to_root_trigger = {
	is_of_major_interest_trigger = {
		CHARACTER = root
	}
}

is_of_minor_interest_to_root_trigger = {
	is_of_minor_interest_trigger = {
		CHARACTER = root
	}
}


###############
# Character Lists
###############

#Support trigger
major_interest_player_heir_trigger = {
	exists = player_heir
	NOT = { player_heir = { is_in_list = $LISTNAME$ } }
}

#Support trigger
major_interest_liege_trigger = {
	exists = liege
	NOR = {
		this = this.liege
		liege = { is_in_list = $LISTNAME$ }
	}
}

#Support trigger
major_interest_realm_priest_trigger = {
	faith = { has_doctrine = doctrine_theocracy_temporal }
	exists = cp:councillor_court_chaplain
	NOT = {
		cp:councillor_court_chaplain = { is_in_list = $LISTNAME$ }
	}
}

#Support trigger
major_interest_primary_spouse_trigger = {
	exists = primary_spouse
	NOT = { primary_spouse = { is_in_list = $LISTNAME$ } }
}


#Support trigger
major_interest_potential_heirs_trigger = {
	player_heir_position = {
		target = $CHARACTER$
		position <= 5
	}
	NOT = { is_in_list = $LISTNAME$ }
}

#Makes a list of every character of major interest
	#Parameters:
	# CHARACTER - the character who the list will be for
	# LISTNAME - the name of the list
get_characters_of_major_interest_trigger = {
	save_temporary_scope_as = major_root
	# 1 - Player heir
	trigger_if = {
		limit = {
			major_interest_player_heir_trigger = { LISTNAME = $LISTNAME$ }
		}
		player_heir = { add_to_temporary_list = $LISTNAME$ }
	}
	# 2 - Liege
	trigger_if = {
		limit = {
			major_interest_liege_trigger = { LISTNAME = $LISTNAME$ }
		}
		liege = { add_to_temporary_list = $LISTNAME$ }
	}
	# 3 - Realm Priest
	trigger_if = {
		limit = {
			major_interest_realm_priest_trigger = { LISTNAME = $LISTNAME$ }
		}
		cp:councillor_court_chaplain = { add_to_temporary_list = $LISTNAME$ }
	}
	# 4 - Primary Spouse
	trigger_if = {
		limit = {
			major_interest_primary_spouse_trigger = { LISTNAME = $LISTNAME$ }
		}
		primary_spouse = { add_to_temporary_list = $LISTNAME$ }
	}
	# 5 - Spymaster & # 11 - Other councillors
	any_normal_councillor = {
		NOT = { is_in_list = $LISTNAME$ }
		count = all
		add_to_temporary_list = $LISTNAME$
	}
	# 6 - Powerful vassals
	any_powerful_vassal = {
		NOT = { is_in_list = $LISTNAME$ }
		count = all
		add_to_temporary_list = $LISTNAME$
	}
	# 7 - Religious head (Excluded because they're highly context-specific)
	# 8 - Potential heirs (Player only)
	trigger_if = {
		limit = { is_ai = no }
		trigger_if = {
			limit = { exists = dynasty }
			dynasty = {
				any_dynasty_member = {
					major_interest_potential_heirs_trigger = { LISTNAME = $LISTNAME$ CHARACTER = scope:major_root }
					count = all
					add_to_temporary_list = $LISTNAME$
				}
			}
		}
	# 9 - Allies (Player only, Enemies are only relevant for content about wars)
		any_ally = {
			NOT = { is_in_list = $LISTNAME$ }
			count = all
			add_to_temporary_list = $LISTNAME$
		}
	}
	# 10 - Neighboring rulers (not included in this list, hard to gauge relevance)
}

#Support trigger
minor_interest_concubines_secondary_spouses_trigger = {
	trigger_if = {
		limit = { exists = $CHARACTER$.primary_spouse }
		NOT = { this = $CHARACTER$.primary_spouse  }
	}
	NOT = { is_in_list = $LISTNAME$ }
}

#Support trigger
minor_interest_high_tier_vassal_trigger = {
	is_powerful_vassal = no #Already included in major
#	highest_held_title_tier >= {
#		value = liege.highest_held_title_tier
#		subtract = 2
#		min = 2 #Must be at least a count
#	}
	NOT = { is_in_list = $LISTNAME$ }
}

minor_interest_close_relatives_trigger = {
	NOR = {
		#Blocking the most common overlap with high prio
		AND = {
			is_cousin_of = $CHARACTER$
			NOT = { is_close_family_of = $CHARACTER$ } #Because... incest. Sigh.
		}
		major_interest_potential_heirs_trigger = { CHARACTER = $CHARACTER$ LISTNAME = $LISTNAME$ }
		trigger_if = {
			limit = { exists = $CHARACTER$.player_heir }
			this = $CHARACTER$.player_heir
		}
		is_in_list = $LISTNAME$
	}
}

#Support trigger
minor_interest_betrothed_trigger = {
	exists = betrothed
	betrothed = { NOT = { is_in_list = $LISTNAME$ }  }
}

#Support trigger
minor_interest_skilled_courtiers_trigger = {
	OR = {
		diplomacy > high_skill_rating
		martial > high_skill_rating
		stewardship > high_skill_rating
		intrigue > high_skill_rating
		learning > high_skill_rating
		prowess > high_skill_rating
	}
	average_skill_rating >= {
		add = diplomacy
		add = martial
		add = stewardship
		add = intrigue
		add = learning
		add = prowess
		divide = 6
	}
	NOT = { is_in_list = $LISTNAME$ }
}

#Support trigger
minor_interest_liege_or_above_trigger = {
	NOR = {
		this = $CHARACTER$.liege
		is_in_list = $LISTNAME$
	}
}

#Makes a list of every character of minor interest
	#Parameters:
	# LISTNAME - the name of the list
get_characters_of_minor_interest_trigger = {
	save_temporary_scope_as = minor_root
	# 1 - Concubines & secondary spouses
	any_consort = {
		minor_interest_concubines_secondary_spouses_trigger = { CHARACTER = scope:minor_root LISTNAME = $LISTNAME$ }
		count = all
		add_to_temporary_list = $LISTNAME$
	}	
	# 2 - Direct vassals of high tier
	any_vassal = {
		minor_interest_high_tier_vassal_trigger = { LISTNAME = $LISTNAME$ }
		count = all
		add_to_temporary_list = $LISTNAME$
	}
	# 3 - Betrothed
	trigger_if = {
		limit = { minor_interest_betrothed_trigger = { LISTNAME = $LISTNAME$ } }
		betrothed = { add_to_temporary_list = $LISTNAME$ }
	}
	# 4 - Lovers
	any_relation = {
		type = lover
		NOT = { is_in_list = $LISTNAME$ }
		count = all
		add_to_temporary_list = $LISTNAME$
	}
	# 5 - Rivals (excluded as they're typically only used in specific contexts)
	# 6 - Close relatives
	any_close_or_extended_family_member = {
		minor_interest_close_relatives_trigger = { CHARACTER = scope:minor_root LISTNAME = $LISTNAME$ }
		count = all
		add_to_temporary_list = $LISTNAME$
	}
	trigger_if = { #Below is just for players
		limit = { is_ai = no }
	# 7 - Guardian
		any_relation = {
			type = guardian
			NOT = { is_in_list = $LISTNAME$ }
			count = all
			add_to_temporary_list = $LISTNAME$
		}
	# 8 - Ward
		any_relation = {
			type = ward
			NOT = { is_in_list = $LISTNAME$ }
			count = all
			add_to_temporary_list = $LISTNAME$
		}
	# 9 - Friends
		any_relation = {
			type = friend
			NOT = { is_in_list = $LISTNAME$ }
			count = all
			add_to_temporary_list = $LISTNAME$
		}
	# 10 - Knights
		any_knight = {
			NOT = { is_in_list = $LISTNAME$ }
			count = all
			add_to_temporary_list = $LISTNAME$
		}
	# 11 - Highly skilled courtiers (players only)
		trigger_if = {
			limit = { is_ai = no }
			any_courtier = {
				minor_interest_skilled_courtiers_trigger = { LISTNAME = $LISTNAME$ }
				count = all
				add_to_temporary_list = $LISTNAME$
			}
		}
		any_liege_or_above = {
			minor_interest_liege_or_above_trigger = { CHARACTER = scope:minor_root LISTNAME = $LISTNAME$ }
			count = all
			add_to_temporary_list = $LISTNAME$
		}
	}
}



#Makes a list of all characters of interest
	#Parameters:
	# CHARACTER - the character the list is for
	# LISTNAME - the name of the list
get_characters_of_interest_trigger = {
	get_characters_of_major_interest_trigger = {
		LISTNAME = $LISTNAME$
	}
	get_characters_of_minor_interest_trigger = {
		LISTNAME = $LISTNAME$
	}
}



################
# Potential Enemies
################

#Makes a list of characters who are classified as potential enemies
	#Parameters
	# CHARACTER - The character whose enemies are being found
	# LISTNAME - The name of the list
get_potential_enemies_effect = {
	$CHARACTER$ = {
		trigger_if = {
			limit = {
				any_vassal = {
					always = yes
				}
			}
			any_vassal = {
				count = all
				add_to_temporary_list = temp_$LISTNAME$
			}
		}
		trigger_if = {
			limit = {
				any_relation = {
					type = rival
					always = yes
				}
			}
			any_relation = {
				type = rival
				count = all
				add_to_temporary_list = temp_$LISTNAME$
			}
		}
		trigger_if = {
			limit = {
				is_independent_ruler = no
			}
			liege = {
				add_to_temporary_list = temp_$LISTNAME$
			}
		}
		trigger_if = {
			limit = {
				is_independent_ruler = yes
				any_neighboring_top_liege_realm_owner = {
					always = yes
				}
			}
			any_neighboring_top_liege_realm_owner = {
				count = all
				add_to_temporary_list = temp_$LISTNAME$
			}
		}
		#Potential Heirs that do not like you
		#A Religious Head that dislikes you and you are powerful enough

		any_in_list = {
			count = all
			trigger_if = {
				limit = {
					NOT = { is_in_list = $LISTNAME$ }
					OR = {
						#Vassal enemies
						AND = {
							is_vassal_of = $CHARACTER$
							is_powerful = yes
							opinion = {
								target = $CHARACTER$
								value <= 0
							}
						}
						#Rivals
						has_relation_rival = $CHARACTER$
						#Lieges
						AND = {
							$CHARACTER$ = { is_independent_ruler = no }
							this = $CHARACTER$.liege
							opinion = {
								target = $CHARACTER$
								value <= 0
							}
						}
						#Neighboring independent rulers
						AND = {
							any_neighboring_top_liege_realm_owner = {
								this = $CHARACTER$
							}
							NOT = { is_allied_to = $CHARACTER$ }
							OR = {
								opinion = {
									target = $CHARACTER$
									value <= 20
								}
								AND = {
									opinion = {
										target = $CHARACTER$
										value <= 50
									}
									has_any_cb_on = $CHARACTER$
								}
							}
						}
					}
				}
				add_to_temporary_list = $LISTNAME$
			}
		}
	}
}

has_RelationToMe_relation = {
	save_temporary_scope_as = relation_check
	NOT = { this = $CHARACTER$ } # Don't print our relation to ourselves!
	OR = {
		is_close_or_extended_family_of = $CHARACTER$
		any_spouse = {
			even_if_dead = yes
			this = $CHARACTER$
		}
		any_spouse = {
			even_if_dead = yes
			this = $CHARACTER$
			is_concubine = yes
		}
		$CHARACTER$ = {
			exists = player_heir
			player_heir = scope:relation_check
		}
		is_heir_of = $CHARACTER$
		has_relation_rival = $CHARACTER$
		has_relation_lover = $CHARACTER$
		has_relation_friend = $CHARACTER$
		has_relation_bully = $CHARACTER$
		has_relation_victim = $CHARACTER$
		is_councillor_of = $CHARACTER$
		any_spouse = { any_child = { this = $CHARACTER$ } }
		any_parent = { any_spouse = { this = $CHARACTER$ } }
		any_parent = { any_spouse = { any_child = { this = $CHARACTER$ } } }
		AND = {
			exists = liege
			liege = $CHARACTER$
		}
		any_courtier = { this = $CHARACTER$ }
		any_vassal = { this = $CHARACTER$ }
		has_relation_crush = $CHARACTER$
		has_relation_ward = $CHARACTER$
		has_relation_guardian = $CHARACTER$
		has_relation_mentor = $CHARACTER$
		has_relation_student = $CHARACTER$
		has_relation_court_physician = $CHARACTER$
		$CHARACTER$ = { is_sibling_in_law_of_root_trigger = yes }
		AND = {
			exists = dynasty
			$CHARACTER$ = { exists = dynasty }
			dynasty = $CHARACTER$.dynasty
		}
		$CHARACTER$ = {
			any_owned_story = {
				story_type = story_peasant_affair
				var:peasant_character = {
					this = root
				}
			}
		}
		$CHARACTER$ = {
			any_scheme = {
				scheme_is_character_agent = root
			}
		}
		is_imprisoned_by = $CHARACTER$
		$CHARACTER$ = { is_imprisoned_by = scope:relation_check }
		AND = {
			exists = host
			host = $CHARACTER$
		}
		$CHARACTER$ = {
			exists = host
			host = scope:relation_check
		}
		is_knight_of = $CHARACTER$
		AND = {
			is_female = yes
			any_spouse = {
				is_child_of = root
			}
		}
		AND = {
			is_male = yes
			any_spouse = {
				is_child_of = root
			}
		}
	}
}