###TRIGGER LIST###

#GUEST/COURTIER LEAVING/ARRIVING TRIGGERS:
#any_child_not_in_traveling_family_trigger
#any_consort_not_in_traveling_family_trigger
#courtier_allowed_to_leave_trigger
#guest_allowed_to_leave_trigger
#guest_allowed_to_arrive_trigger

#GUEST/COURTIER USEFULNESS:
#courtier_or_guest_claim_trigger
#useful_courtier_or_guest_claim_trigger
#has_useful_potential_spouse_claim_trigger
#guest_knight_candidate_trigger
#guest_commander_candidate_trigger
#guest_vassal_candidate_trigger
#guest_physician_candidate_trigger
#guest_male_female_balance_trigger
#child_available_for_guest_marriage_trigger
#guest_marriage_candidate_trigger

#POOL CHARACTER TRIGGERS:
#pool_character_is_pruneable_trigger


####GUEST/COURTIER LEAVING/ARRIVING TRIGGERS###
same_location_and_court_status_as = { # Same location and both are in court / are guests / are pool characters
	exists = location
	exists = $CHARACTER$.location
	location = $CHARACTER$.location
	OR = {
		is_in_the_same_court_as = $CHARACTER$
		trigger_if = {
			limit = { $CHARACTER$ = { is_pool_guest = yes } }
			is_pool_guest = yes
		}
		trigger_if = {
			limit = { $CHARACTER$ = { is_pool_character = yes } }
			is_pool_character = yes
		}
	}

}

any_child_not_in_traveling_family_trigger = {
	save_temporary_scope_as = traveler
	exists = location
	any_child = {
		is_adult = no
		same_location_and_court_status_as = { CHARACTER = scope:traveler }
		save_temporary_scope_as = checking_child
		scope:traveler = {
			NOT = {
				any_traveling_family_member = { this = scope:checking_child }
			}
		}
	}
}

any_consort_not_in_traveling_family_trigger = {
	save_temporary_scope_as = traveler
	exists = location
	any_spouse = {
		exists = location
		same_location_and_court_status_as = { CHARACTER = scope:traveler }
		save_temporary_scope_as = checking_consort
		scope:traveler = {
			NOT = {
				any_traveling_family_member = { this = scope:checking_consort }
			}
		}
	}
}


courtier_allowed_to_leave_trigger = {
	save_temporary_scope_as = leaving_courtier
	NOR = {
		# Not blocked by script
		has_character_flag = blocked_from_leaving
		is_councillor_of = scope:liege
		is_consort_of = scope:liege
		scope:liege = {
			player_heir_position = {
				target = scope:leaving_courtier
				value <= 2 #Not 1st, 2nd or 3rd player heir (0,1,2)
			}
		}
		has_any_court_position = yes
		has_relation_lover = scope:liege
		is_knight_of = scope:liege
		is_tax_collector_of = scope:liege
		any_relation = {
			type = ward
			OR = {
				this = scope:liege
				AND = {
					is_courtier_of = scope:liege
					OR = {
						NOT = { any_close_family_member = { this = scope:leaving_courtier } }
						any_close_family_member = { this = scope:liege }
					}
				}
			}
		}
		any_relation = {
			type = guardian
			OR = {
				this = scope:liege
				AND = {
					is_courtier_of = scope:liege
					OR = {
						NOT = { any_close_family_member = { this = scope:leaving_courtier } }
						any_close_family_member = { this = scope:liege }
					}
				}
			}
		}
		any_relation = {
			type = mentor
			OR = {
				this = scope:liege
				AND = {
					is_courtier_of = scope:liege
					OR = {
						NOT = { any_close_family_member = { this = scope:leaving_courtier } }
						any_close_family_member = { this = scope:liege }
					}
				}
			}
		}
		any_relation = {
			type = student
			OR = {
				this = scope:liege
				AND = {
					is_courtier_of = scope:liege
					OR = {
						NOT = { any_close_family_member = { this = scope:leaving_courtier } }
						any_close_family_member = { this = scope:liege }
					}
				}
			}
		}
		#Keep daughters/sons for marriages
		AND = {
			OR = {
				is_child_of = scope:liege
				is_grandchild_of = scope:liege
				is_great_grandchild_of = scope:liege
			}
			NOT = { has_trait = bastard }
			trigger_if = { #Daughters
				limit = { is_female = yes }
				scope:liege = {
					OR = {
						has_realm_law = male_only_law
						has_realm_law = male_preference_law
					}
				}
			}
			trigger_else = { #Sons
				scope:liege = {
					OR = {
						has_realm_law = female_only_law
						has_realm_law = female_preference_law
					}
				}
			}
		}
		#No children of liege can leave court while children
		AND = {
			OR = {
				is_child_of = scope:liege
				is_grandchild_of = scope:liege
				is_great_grandchild_of = scope:liege
			}
			is_adult = no
		}
		#Claimant won't leave while claim is pressed...
		scope:liege = { pressing_claim_of_character_trigger = { CHARACTER = scope:leaving_courtier } }
		#Would be leaving without a child or consort
		any_child_not_in_traveling_family_trigger = yes
		any_consort_not_in_traveling_family_trigger = yes
		#...Or while they have a promise for a claim getting pressed
		has_character_flag = courtier_staying_for_claim

		#...Or if they're involved in an ongoing event
		has_variable = stewardship_duty_1062_employer

		#...Or if someone is trying to elope with them
		any_targeting_scheme = {
			scheme_type = elope
		}
		
		#Non-dominant spouses don't wander off
		AND = {
			is_married = yes
			any_spouse = {
				OR = {
					liege ?= scope:leaving_courtier.liege

					AND = {
						exists = host
						exists = scope:leaving_courtier.host
						liege = scope:leaving_courtier.host
					}
					is_in_the_same_court_as = scope:leaving_courtier
				}
			}
			trigger_if = {
				limit = { is_female = yes }
				scope:liege = {
					OR = {
						has_realm_law = male_only_law
						has_realm_law = male_preference_law
					}
				}
			}
			trigger_else = {
				scope:liege = {
					OR = {
						has_realm_law = female_only_law
						has_realm_law = female_preference_law
					}
				}
			}
		}

		# Diarchs don't leave their court.
		is_diarch = yes
		is_designated_diarch = yes
		
		#Shieldmaidens stay till dismissed.
		has_trait = shieldmaiden
		
		# Children taught a lesson won't leave
		has_character_modifier = mellowed_spirit

		# Courtiers with sponsored inspirations won't leave on their own.
		inspiration ?= { exists = inspiration_sponsor }
		
		scope:liege = { government_has_flag = government_is_landless_adventurer }
		is_obedient_to = scope:liege
		
		AND = {
			scope:liege.primary_title = title:h_china
			OR = {
				is_child_of = scope:liege
				is_grandchild_of = scope:liege
				is_great_grandchild_of = scope:liege
			}
		}
	}
}


guest_allowed_to_leave_trigger = {
	NOR = {
		# Not blocked by script
		has_character_flag = blocked_from_leaving
		#Agent in a local scheme
		scope:host = {
			any_courtier = {
				any_targeting_scheme = {
					any_scheme_agent_character = {
						this = scope:guest
					}
				}
			}
		}
		scope:host = {
			any_targeting_scheme = {
				any_scheme_agent_character = {
					this = scope:guest
				}
			}
		}
		any_child = { # No child can be a known child of the host
			is_adult = no
			any_parent = { this = scope:host }
		}
		# Diarchs don't leave their court.
		is_diarch = yes
		is_designated_diarch = yes
		#Would be leaving without spouse or underage child
		any_child_not_in_traveling_family_trigger = yes
		any_consort_not_in_traveling_family_trigger = yes
		#Is waiting for inspiration funding
		exists = inspiration
	}
}

guest_allowed_to_arrive_trigger = {
	trigger_if = {
		limit = { exists = var:last_visited_ruler }
		NOT = { var:last_visited_ruler = $HOST$ }
	}
	trigger_if = { # Make sure that they're not trying to go back to where they are right now
		limit = { exists = host }
		NOT = { host = $HOST$ }
	}
	NOT = { has_trait_with_flag = epidemic_disease }
	NOT = { has_relation_rival = $HOST$ }
	#Compatible faiths
	faith = {
		save_temporary_scope_as = potential_guest_faith
		faith_hostility_level = {
			target = $HOST$.faith
			value < faith_evil_level
		}
	}
	$HOST$.faith = {
		faith_hostility_level = {
			target = scope:potential_guest_faith
			value < faith_evil_level
		}
	}
	
	#Isn't too good for host
	NAND = {
		OR = {
			any_claim = { tier = tier_empire }
			any_close_family_member = { highest_held_title_tier = tier_empire }
		}
		$HOST$ = { highest_held_title_tier <= tier_county }
	}
} #Note: this trigger used to contain opinion triggers but then some awful rulers could never get guests because everyone hates them


#This is a localized trigger for checking that a character do not have a spouse that's employed or has some other circumstance blocking them from being recruited to a court
can_recruit_character_to_court_trigger = {
	$RECRUITER$ = { save_temporary_scope_as = recruiter }
	$RECRUITEE$ = { save_temporary_scope_as = recruitee }
	scope:recruitee = {
		bp2_valid_for_standard_interactions_trigger = yes
		trigger_if = {
			limit = { is_adult = no }
			custom_description = {
				text = is_not_wandering_child
				subject = scope:recruitee
				OR = {
					is_close_or_extended_family_of = scope:recruiter
					scope:recruiter = {
						any_spouse = {
							any_child = { this = scope:recruitee }
						}
					}
					trigger_if = {
						limit = {
							scope:recruiter.culture = { has_cultural_parameter = wider_adoption }
							scope:recruitee.culture = { has_cultural_parameter = wider_adoption }
						}
						NOT = {
							any_parent = {
								is_alive = yes
								can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = liege }
							}
						}
					}
				}
			}
		}
		NOR = {
			is_imprisoned = yes
			is_theocratic_lessee = yes
			is_diarch = yes
			trigger_if = {
				limit = {
					exists = host
					host = { is_ai = no }
					NOT = { is_player_heir_of = scope:recruiter }
				}
				is_player_heir_of = host
			}
			trigger_if = {
				limit = {
					exists = host
					host = { is_ai = yes }
					NOT = { is_player_heir_of = scope:recruiter }
				}
				is_primary_heir_of = host
			}
			trigger_if = {
				limit = { exists = host }
				is_consort_of = host
			}
			trigger_if = {
				limit = { exists = host }
				host = {
					pressing_claim_of_character_trigger = { CHARACTER = scope:recruitee }
				}
			}
			trigger_if = {
				limit = { exists = host }
				#Are they employed?
				is_councillor_of = scope:recruitee.host
				is_knight_of = scope:recruitee.host
				any_relation = {
					type = ward
					OR = {
						this = scope:recruitee.host
						is_close_family_of = scope:recruitee.host
					}
				}
				any_relation = {
					type = guardian
					OR = {
						this = scope:recruitee.host
						is_close_family_of = scope:recruitee.host
					}
				}
				any_relation = {
					type = mentor
					OR = {
						this = scope:recruitee.host
						is_close_family_of = scope:recruitee.host
					}
				}
				any_relation = {
					type = student
					OR = {
						this = scope:recruitee.host
						is_close_family_of = scope:recruitee.host
					}
				}
				any_court_position_employer = { this = scope:recruitee.host }
			}
			trigger_if = {
				limit = {
					scope:recruiter = { has_government = landless_adventurer_government }
				}
				custom_tooltip = {
					text = can_recruit_character_to_court_trigger.tt.laamps_invitation_restrictions
					NOR = {
						is_player_heir_of = scope:recruiter
						has_relation_soulmate = scope:recruiter
						has_relation_best_friend = scope:recruiter
						is_consort_of = scope:recruiter
						AND = {
							is_pool_character = yes
							OR = {
								has_relation_friend = scope:recruiter
								has_relation_lover = scope:recruiter
								is_close_family_of = scope:recruiter
							}
						}
					}
				}
			}
			
			#Do they have the "wrong" marriage type and the spouse is employed?
			custom_description = {
				text = is_married_matrilineally_and_spouse_is_dominant_partner
				subject = scope:recruitee
				any_spouse = {
					is_female = yes
					matrilinear_marriage = yes
					host ?= scope:recruitee.host
					save_temporary_scope_as = spouse
					OR = { #Is employed in some way
						is_councillor_of = scope:recruitee.host
						is_knight_of = scope:recruitee.host
						any_relation = {
							type = ward
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						any_relation = {
							type = guardian
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						any_relation = {
							type = mentor
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						any_relation = {
							type = student
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						scope:spouse = {
							any_court_position_employer = { this = scope:recruitee.host }
						}
					}
				}
			}
			custom_description = {
				text = is_married_patrilineally_and_spouse_is_dominant_partner
				subject = scope:recruitee
				any_spouse = {
					is_male = yes
					patrilinear_marriage = yes
					host ?= scope:recruitee.host
					save_temporary_scope_as = spouse
					OR = { #Is employed in some way
						is_councillor_of = scope:recruitee.host
						is_knight_of = scope:recruitee.host
						any_relation = {
							type = ward
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						any_relation = {
							type = guardian
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						any_relation = {
							type = mentor
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						any_relation = {
							type = student
							OR = {
								this = scope:recruitee.host
								is_close_family_of = scope:recruitee.host
							}
						}
						scope:spouse = {
							any_court_position_employer = { this = scope:recruitee.host }
						}
					}
				}
			}
			custom_description = {
				text = "is_escaped_prisoner"
				subject = scope:recruitee
				OR = {
					has_opinion_modifier = {
						modifier = attempted_imprisonment_opinion
						target = scope:recruiter
					}
					has_opinion_modifier = {
						modifier = treasonous_imprison_refusal
						target = scope:recruiter
					}
					AND = {
						exists = var:escaped_imprisonment_from
						var:escaped_imprisonment_from = scope:recruiter
					}
				}
			}
		}
	}
}


###GUEST/COURTIER USEFULNESS TRIGGERS###
courtier_or_guest_claim_trigger = {
	exists = holder
	NOT = {
		holder = {
			OR = {
				this = $RULER$
				target_is_liege_or_above = $RULER$
			}
		}
	}
}

useful_courtier_or_guest_claim_trigger = {
	courtier_or_guest_claim_trigger = { RULER = $RULER$ }
	tier < $RULER$.highest_held_title_tier
	$RULER$ = { is_landed = yes }
	OR = { #For distance check
		exists = title_province
		exists = holder.capital_province
	}
	#The higher tier the ruler has, the longer distance is acceptable
	#We prefer to measure against the title's province, but if there is none, check its holder
	trigger_if = {
		limit = { $RULER$ = { highest_held_title_tier = tier_empire } }
		trigger_if = {
			limit = { exists = title_province }
			title_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_almost_massive } }
		}
		trigger_else = {
			holder.capital_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_almost_massive } }
		}
	}
	trigger_else_if = {
		limit = { $RULER$ = { highest_held_title_tier = tier_kingdom } }
		trigger_if = {
			limit = { exists = title_province }
			title_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_huge } }
		}
		trigger_else = {
			holder.capital_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_huge } }
		}
	}
	trigger_else_if = {
		limit = { $RULER$ = { highest_held_title_tier = tier_duchy } }
		trigger_if = {
			limit = { exists = title_province }
			title_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_large } }
		}
		trigger_else = {
			holder.capital_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_large } }
		}
	}
	trigger_else = {
		trigger_if = {
			limit = { exists = title_province }
			title_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_medium } }
		}
		trigger_else = {
			holder.capital_province = { squared_distance = { target = $RULER$.capital_province value <= squared_distance_medium } }
		}
	}
}

neighboring_useful_courtier_or_guest_claim_trigger = {
	courtier_or_guest_claim_trigger = { RULER = $RULER$ }
	tier < $RULER$.highest_held_title_tier
	exists = $RULER$.capital_province
	holder = {
		any_sub_realm_county = {
			is_neighbor_to_realm = $RULER$		
		}
	}
}

has_useful_potential_spouse_claim_trigger = {
	any_claim = {
		pressed = yes
		exists = holder
		NOT = {
			holder = {
				OR = {
					this = $RULER$
					target_is_liege_or_above = $RULER$
				}
			}
		}
	}
}

guest_knight_candidate_trigger = {
	can_be_knight_trigger = { ARMY_OWNER = $HOST$ }
	age < 60
	age >= 25
	prowess > medium_skill_rating
}

guest_commander_candidate_trigger = {
	can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $HOST$ }
	age < 60
	age >= 25
	martial > medium_skill_rating
}

guest_vassal_candidate_good_traits_trigger = {
	OR = { #Gives +opinion of liege
		has_trait = content
		has_trait = trusting
		has_trait = humble
	}
}

guest_vassal_candidate_bad_traits_trigger = {
	OR = { #Gives -opinion of liege
		has_trait = ambitious
		has_trait = arrogant
		has_trait = impatient
	}
}

guest_vassal_candidate_trigger = {
	age < 60
	faith = scope:host.faith
	culture = scope:host.culture
	faith = {
		has_dominant_ruling_gender = prev
	}
	guest_vassal_candidate_bad_traits_trigger = no
	save_temporary_scope_as = vassal_candidate
	is_eunuch_trigger = no
	NOR = { #So they don't leave your realm through inheritance
		any_heir_title = {
			scope:guest = { is_primary_heir_of = scope:vassal_candidate }
		}
		any_child = { is_playable_character = yes }
		has_trait = devoted
		has_trait = order_member
	}
}

guest_physician_candidate_trigger = {
	age < 70
	learning >= decent_skill_rating
}

guest_male_female_balance_trigger = { #are you interesting enough to get "upbalanced" to make up for lack of available positions due to your gender?
	is_adult = yes
	age <= 65
	is_lowborn = no
}
	
child_available_for_guest_marriage_trigger = {
	is_married = no
	is_betrothed = no
	age >= 10
	OR = {
		is_vassal_of = scope:host
		is_courtier_of = scope:host
	}
}	

guest_marriage_candidate_trigger = {
	save_temporary_scope_as = marriage_candidate
	OR = {
		is_male = yes
		age <= 40
	}
	OR = {
		AND = {
			is_married = no
			can_marry_character_trigger = { CHARACTER = scope:host }
		}
		scope:host = {
			any_child = {
				child_available_for_guest_marriage_trigger = yes
				can_marry_character_trigger = { CHARACTER = scope:marriage_candidate }
			}
		}
	}
}

###POOL TRIGGERS###
is_visitable_relation_trigger = {
	capital_province ?= { local_pool_is_full_trigger = no }
	NOR = {
		this = $CHARACTER$.host
		has_relation_rival = $CHARACTER$
	}
	$CHARACTER$ = {
		NOT = {
			var:last_visited_ruler ?= prev
		}
	}
}


###########################
# POOL CHARACTER TRIGGERS #
###########################

pool_character_is_pruneable_trigger = {
	save_temporary_scope_as = pool_prune_check
	is_adult = yes
	NOR = {
		has_trait = heresiarch
		has_trait = populist_leader
		has_trait = adventurer_follower
		has_trait = adventurer
		has_trait = historical_character
		has_character_flag = ai_will_not_convert
		is_married = yes
		any_claim = { }
		any_child = { is_adult = no }
		any_relation = {
			type = lover
			is_ruler = yes
		}
		any_relation = {
			type = friend
			is_ruler = yes
		}
		any_close_or_extended_family_member = { is_ruler = yes }
		#Belongs to a dynasty with more than 1 member
		any_relation = {
			type = rival
			is_ai = no
		}
		has_character_flag = easteregg
	}
	trigger_if = {
		limit = {
			is_lowborn = no
		}
		NOR = {
			diplomacy >= high_skill_rating
			martial >= high_skill_rating
			stewardship >= high_skill_rating
			intrigue >= high_skill_rating
			learning >= high_skill_rating
			prowess >= high_skill_rating
			dynasty ?= { any_dynasty_member = { this != scope:pool_prune_check } }
			any_relation = {
				type = rival
				is_ruler = yes
			}
			culture = {
				has_cultural_tradition = tradition_diasporic
			}
		}
	}
}


local_pool_is_full_trigger = {
	number_of_characters_in_pool >= full_pool_size
}

guest_poet_candidate_trigger = {
	age < 80
	age >= 16
	diplomacy > medium_skill_rating
}

is_courtier_or_knight_of_root = {
	OR = {
		is_courtier_of = root
		is_knight_of = root
	}
}

guest_herder_candidate_trigger = {
	government_has_flag = government_is_true_herder
	has_trait = lifestyle_seasoned_pastor
	trigger_if = {
		limit = { root.faith = { has_doctrine = doctrine_gender_male_dominated } }
		is_male = yes
	}
	trigger_if = {
		limit = { root.faith = { has_doctrine = doctrine_gender_female_dominated } }
		is_female = yes
	}
	age < 45
	age >= 25
}
