####MARRIAGE TRIGGER LIST###

#GENERAL MARRIAGE TRIGGERS
#can_marry_common_trigger
#can_marry_trigger
#could_marry_character_trigger
#can_marry_character_trigger
#marriage_between_characters_unlocks_alliance_of_min_tier_trigger
#can_become_concubine

#MARRIAGE INTERACTION TRIGGERS

#############################
# GENERAL MARRIAGE TRIGGERS #
#############################

# Is allowed to marry (current marriage/concubine status ignored)
can_marry_common_trigger = {
	is_alive = yes
	NOT = { has_trait_with_flag = can_not_marry }
	NOT = { has_variable = can_not_marry }

	trigger_if = {
		limit = {
			exists = primary_title
		}
		NOR = {
			# Block Mercenaries from marrying or showing up in marriage interactions
			primary_title = {
				is_mercenary_company = yes
			}
			is_leading_faction_type = peasant_faction
			is_leading_faction_type = populist_faction
			government_has_flag = government_is_true_herder
		}
	}

	# Clergy cannot marry if their faith disallows it
	trigger_if = {
		limit = {
			is_clergy = yes
		}
		faith = {
			has_doctrine_parameter = clergy_can_marry
		}
	}
	trigger_if = {
		limit = {
			exists = liege.involved_activity.var:tournament_marriage_prize
			this = liege.involved_activity.var:tournament_marriage_prize
		}
		exists = scope:contest_winner
	}
}

#Can marry right now
can_marry_trigger = {
	can_marry_common_trigger = yes
	allowed_more_spouses = yes
	#To account for a person marrying their concubines please use can_marry_character_trigger
}

allowed_to_marry_same_sex_trigger = {
	has_game_rule = accepted_same_sex_marriage
	faith = {
		NOR = {
			has_doctrine_parameter = homosexuality_shunned
			has_doctrine_parameter = homosexuality_illegal
		}
	}
}

can_have_children_with = {
	sex_opposite_of = $CHARACTER$ # Only opposite sex since for now
}

allowed_to_marry_character_gender_trigger = {
	OR = {
		AND = {
			allowed_to_marry_same_sex_trigger = yes
			sex_same_as = $CHARACTER$
		}
		sex_opposite_of = $CHARACTER$
	}
}

#two characters could potentially marry (does NOT check that they're unmarried)
could_marry_character_trigger = {
	save_temporary_scope_as = can_marry_check
	can_marry_common_trigger = yes
	$CHARACTER$ = { can_marry_common_trigger = yes }
	#Opposite genders if you don't have accepted same-sex marriage game rule enabled and your faith supports it
	trigger_if = {
		limit = {
			$CHARACTER$ = { allowed_to_marry_same_sex_trigger = no }
		}
		sex_opposite_of = $CHARACTER$
	}
	#Have you recently divorced this character?
	NOT = {
		has_opinion_modifier = {
			modifier = divorced_me_opinion
			target = $CHARACTER$
		}
	}
	#Faith hostility & consanguinity
	trigger_if = {
		limit = { NOT = { is_courtier_of = $CHARACTER$ } } #If you're someone's courtier, your liege can marry you anyway
		faith = {
			faith_allows_marriage_consanguinity_trigger = {
				CHARACTER_1 = scope:can_marry_check
				CHARACTER_2 = $CHARACTER$
			}
			#faith_hostility_level = {
			#	target = $CHARACTER$.faith
			#	value < faith_hostility_prevents_marriage_level
			#}
		}
	}
	trigger_if = {
		limit = { $CHARACTER$ = { NOT = { is_courtier_of = scope:can_marry_check } } } #If you're someone's courtier, your liege can marry you anyway
		$CHARACTER$.faith = {
			faith_allows_marriage_consanguinity_trigger = {
				CHARACTER_1 = scope:can_marry_check
				CHARACTER_2 = $CHARACTER$
			}
			#faith_hostility_level = {
			#	target = scope:can_marry_check.faith
			#	value < faith_hostility_prevents_marriage_level
			#}
		}
	}
	# Cannot marry self
	NOT = {
		scope:can_marry_check = { is_spouse_of = $CHARACTER$ }
	}
}

# Same trigger as above with the exception of the recent divorce trigger
can_take_as_concubine_character_trigger = {
	save_temporary_scope_as = can_marry_check
	can_marry_common_trigger = yes
	$CHARACTER$ = { can_marry_common_trigger = yes }
	#Opposite genders if you don't have accepted same-sex marriage game rule enabled and your faith supports it
	trigger_if = {
		limit = {
			$CHARACTER$ = { allowed_to_marry_same_sex_trigger = no }
		}
		sex_opposite_of = $CHARACTER$
	}
	#Faith hostility & consanguinity
	trigger_if = {
		limit = { NOT = { is_courtier_of = $CHARACTER$ } } #If you're someone's courtier, your liege can marry you anyway
		faith = {
			faith_allows_marriage_consanguinity_trigger = {
				CHARACTER_1 = scope:can_marry_check
				CHARACTER_2 = $CHARACTER$
			}
			#faith_hostility_level = {
			#	target = $CHARACTER$.faith
			#	value < faith_hostility_prevents_marriage_level
			#}
		}
	}
	trigger_if = {
		limit = { $CHARACTER$ = { NOT = { is_courtier_of = scope:can_marry_check } } } #If you're someone's courtier, your liege can marry you anyway
		$CHARACTER$.faith = {
			faith_allows_marriage_consanguinity_trigger = {
				CHARACTER_1 = scope:can_marry_check
				CHARACTER_2 = $CHARACTER$
			}
			#faith_hostility_level = {
			#	target = scope:can_marry_check.faith
			#	value < faith_hostility_prevents_marriage_level
			#}
		}
	}
	NOT = {
		scope:can_marry_check = { is_spouse_of = $CHARACTER$ }
	}
}

#Two characters are able to marry right now
can_marry_character_trigger = {
	save_temporary_scope_as = can_marry_char_check
	OR = {
		AND = {
			is_betrothed = yes
			betrothed = $CHARACTER$
		}
		AND = {
			can_marry_trigger = yes
			$CHARACTER$ = { can_marry_trigger = yes }
		}
	}
	trigger_if = {
		limit = {
			$CHARACTER$ = { is_concubine = yes }
		}
		$CHARACTER$ = { is_concubine_of = scope:can_marry_char_check }
	}
	trigger_if = {
		limit = {
			is_concubine = yes
		}
		is_concubine_of = $CHARACTER$
		$CHARACTER$ = { could_marry_character_trigger = { CHARACTER = scope:can_marry_char_check } } #Gender, recent divorce, allowed to marry, no illegal incest etc.} If they're your concubine you're rules are the ones looked at
	}
	trigger_else = {
		could_marry_character_trigger = { CHARACTER = $CHARACTER$ } #Gender, recent divorce, allowed to marry, no illegal incest etc.
	}
}

#A marriage between MY_CHARACTER and THEIR_CHARACTER would give alliance/would unlock renegotiate alliance for current scope where current scope's ally would be at least TIER
marriage_between_characters_unlocks_alliance_of_min_tier_trigger = {
	save_temporary_scope_as = alliance_unlocker
	$MY_CHARACTER$ = {
		OR = {
			this = scope:alliance_unlocker
			is_close_family_of = scope:alliance_unlocker
		}
	}
	$THEIR_CHARACTER$ = {
		OR = {
			AND = {
				is_ruler = yes
				highest_held_title_tier >= $TIER$
			}
			any_close_family_member = {
				is_ruler = yes
				highest_held_title_tier >= $TIER$
			}
		}
	}
}

can_become_concubine_of_character_trigger = {
	is_available_quick = {
		adult = yes
		ruler = no
		advanced_ruler = no
		hostage = no
	}
	trigger_if = {
		limit = {
			NOT = { is_imprisoned_by = $CHARACTER$ }
		}
		is_married = no
		is_betrothed = no
		is_concubine = no
		number_of_concubines = 0
	}
	trigger_if = {
		limit = {
			is_imprisoned_by = $CHARACTER$
			is_married = yes
		}
		NOT = { is_spouse_of = $CHARACTER$ }
	}
	NOR = {
		is_concubine_of = $CHARACTER$
		has_character_flag = has_been_offered_as_concubine
	}
	could_marry_character_trigger = { CHARACTER = $CHARACTER$ } #Gender, recent divorce, allowed to marry, no illegal incest etc.
}

# Same as above, switching out could_marry_character_trigger for can_take_as_concubine_character_trigger, in order to better inform the player that they cannot take their ex-spouse as a concubine
can_become_concubine_of_character_valid_trigger = {
	is_available_quick = {
		adult = yes
		ruler = no
		advanced_ruler = no
	}
	trigger_if = {
		limit = {
			NOT = { is_imprisoned_by = $CHARACTER$ }
		}
		is_married = no
		is_betrothed = no
		is_concubine = no
		is_diarch = no
		number_of_concubines = 0
	}
	trigger_if = {
		limit = {
			is_imprisoned_by = $CHARACTER$
			is_married = yes
		}
		NOT = { is_spouse_of = $CHARACTER$ }
	}
	NOR = {
		is_concubine_of = $CHARACTER$
		has_character_flag = has_been_offered_as_concubine
	}
	can_take_as_concubine_character_trigger = { CHARACTER = $CHARACTER$ } #Gender, allowed to marry, no illegal incest etc.
	has_any_disease_trigger = no
	has_easily_mocked_physical_attribute_trigger = no
}

can_be_offered_as_concubine_to_character_trigger = {
	is_available_quick = {
		adult = yes
		ruler = no
		advanced_ruler = no
	}
	trigger_if = {
		limit = {
			NOT = { is_imprisoned_by = $GIVER$ }
		}
		is_married = no
		is_betrothed = no
		is_diarch = no
		OR = {
			is_concubine = no
			is_concubine_of = $GIVER$
		}
		number_of_concubines = 0
	}
	trigger_if = {
		limit = {
			is_imprisoned_by = $GIVER$
			is_married = yes
		}
		NOR = {
			is_spouse_of = $CHARACTER$
			is_spouse_of = $GIVER$
		}
	}
	NOR = {
		is_concubine_of = $CHARACTER$
		has_character_flag = has_been_offered_as_concubine
	}
	NOT = {
		is_theocratic_lessee = yes
	}
	could_marry_character_trigger = { CHARACTER = $CHARACTER$ } #Gender, recent divorce, allowed to marry, no illegal incest etc.
}

ready_to_marry_betrothed_trigger = {
	trigger_if = {
		limit = { exists = betrothed }
		is_adult = yes
		betrothed = { is_adult = yes }
	}
	trigger_else = {
		always = no
	}
}

#################################
# MARRIAGE INTERACTION TRIGGERS #
#################################

marriage_interaction_can_be_picked_trigger = {
	can_marry_trigger = yes
	is_available_quick = {
		hostage = no
		imprisoned = no
	}
}

marriage_interaction_valid_target_trigger = {
	# Clergy cannot marry if their faith doesn't allow it
	trigger_if = {
		limit = {
			scope:secondary_actor = {
				is_clergy = yes
				NOT = {
					faith = {
						has_doctrine_parameter = clergy_can_marry
					}
				}
			}
		}
		custom_description = {
			text = character_is_clergy_and_cannot_marry
			subject = scope:secondary_actor
			object = scope:secondary_recipient
			scope:secondary_actor = {
				is_clergy = no
			}
		}
	}
	trigger_else_if = {
		limit = {
			scope:secondary_recipient = {
				is_clergy = yes
				NOT = {
					faith = {
						has_doctrine_parameter = clergy_can_marry
					}
				}
			}
		}
		custom_description = {
			text = character_is_clergy_and_cannot_marry
			subject = scope:secondary_recipient
			object = scope:secondary_actor
			scope:secondary_recipient = {
				is_clergy = no
			}
		}
	}
	#Marriage
	trigger_else_if = {
		limit = {
			scope:secondary_actor = { is_adult = yes }
			scope:secondary_recipient = { is_adult = yes }
		}
		scope:secondary_actor = {
			custom_description = {
				text = "can_marry_check_secondary_actor"
				subject = scope:secondary_actor
				object = scope:secondary_recipient
				can_marry_character_trigger = { CHARACTER = scope:secondary_recipient } #Checks marriage status, betrothed, gender, consanguinity, faith hostility etc.
			}
		}
	}
	#Betrothal
	trigger_else = {
		scope:secondary_actor = {
			custom_description = {
				text = "can_betroth_check_secondary_actor"
				subject = scope:secondary_actor
				object = scope:secondary_recipient
				can_marry_character_trigger = { CHARACTER = scope:secondary_recipient } #Checks marriage status, betrothed, gender, consanguinity, faith hostility etc.
			}
		}
	}

	# If the actor is ai they will not pick a character already considered for concubinage
	trigger_if = {
		limit = {
			scope:actor = { is_ai = yes }
		}
		NOR = {
			scope:secondary_actor = { has_character_flag = has_been_offered_as_concubine }
			scope:secondary_recipient = { has_character_flag = has_been_offered_as_concubine }
		}
	}
}

marriage_interaction_auto_accept_trigger = {
	trigger_if = {
		limit = {
			exists = scope:secondary_actor.betrothed
			scope:secondary_actor = {
				has_been_promised_grand_wedding = yes
			}
		}
		always = no
	}
	trigger_else_if = {
		limit = {
			exists = scope:secondary_actor.betrothed
		}
		custom_description = {
			text = is_betrothed_auto_accept
			subject = scope:secondary_actor
			object = scope:secondary_recipient
			scope:secondary_actor.betrothed = scope:secondary_recipient
		}
	}
	trigger_else_if = {
		limit = {
			scope:hook = yes
		}
		#Strong hook = 100% accept (unless marrying 1st or 2nd player heir or the ruler and asking for matrilineal/partrilineal resulting in no dynasty children for them)
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:actor = { has_strong_hook = scope:recipient }
			scope:recipient = {
				NAND = {
					#Secondary recipient can't be recipient or 1st/2nd heir...
					OR = {
						this = scope:secondary_recipient
						player_heir_position  = {
							target = scope:secondary_recipient
							value <= 2
						}
					}
					#...if the marriage (matrilineal/patrilineal) does not match their gender
					OR = {
						scope:secondary_recipient = {
							is_female = yes
							NOT = { always = scope:matrilineal }
						}
						scope:secondary_recipient = {
							is_male = yes
							always = scope:matrilineal
						}
					}
				}
			}
		}
	}
	trigger_else = {
		always = no
	}
}

make_concubine_is_shown_trigger = {
	scope:actor = {
		allowed_concubines = yes
		is_adult = yes
	}
	scope:recipient = {
		OR = {
			is_courtier_of = scope:actor
			is_imprisoned_by = scope:actor
		}
		can_become_concubine_of_character_valid_trigger = { CHARACTER = scope:actor }
	}
}

make_concubine_is_valid_trigger = {
	custom_description = {
		text = take_concubine_allowed_more
		object = scope:recipient
		scope:actor = {
			allowed_more_concubines = yes
		}
	}
	scope:recipient = {
		custom_tooltip = {
			text = take_concubine_ex_spouse
			NOT = {
				has_opinion_modifier = {
					modifier = divorced_me_opinion
					target = scope:actor
				}
			}
		}
		OR = {
			AND = {
				is_imprisoned_by = scope:actor
				is_married = yes
			}
			is_married = no
		}
		is_hostage = no
	}
}

marriage_interaction_non_loyal_diarchs_refuse_secondary_marriage_trigger = {
	trigger_if = {
		limit = {
			scope:$TARGET$ ?= {
				is_diarch = yes
				# We're only concerned about landed rulers, those who might be married out of their own court.
				is_ruler = no
			}
		}
		custom_tooltip = {
			text = arrange_marriage_interaction.tt.non_loyal_regents_refuse_to_be_married_off.$TARGET$
			scope:$TARGET$ = { diarch_loyalty >= diarch_loyalty_visibly_loyal_threshold }
		}
	}
}

wants_very_young_wife = {
	is_available_quick = {
		female = no
		adult = no
	}
}

wants_young_wife = {
	is_available_quick = {
		female = no
		adult = yes
	}
	age < 30
	NOT = {
		any_child = {
			count >= 2
			is_alive = yes
			NOT = { has_trait = bastard }
		}
	}
}

will_not_be_very_young_wife = {
	is_female = yes
	is_very_young_character = no
}

will_not_be_young_wife = {
	is_female = yes
	is_young_character = no
}

will_not_be_fertile_wife = {
	is_female = yes
	is_aging_character = yes
}

is_ruler_or_close_family = {
	OR = {
		is_ruler = yes
		any_close_family_member = {
			even_if_dead = yes
			is_ruler = yes
		}
	}
}

can_have_concubines_trigger = {
	$CHAR$ ?= {
		custom_tooltip = {
			text = can_have_concubines_name_tt
			allowed_more_concubines = yes
		}
	}
}

# if $CHARACTER$ has the right to break betrothal or receive interaction for breaking betrothal?
break_betrothal_valid_target_trigger = {
	is_betrothed = yes
	# yourself, your courtier, or your child vassal and you were the matchmaker
	trigger_if = {
		limit = {
			this != $CHARACTER$
		}
		is_ai = yes
		OR = {
			AND = {
				is_child_of = $CHARACTER$
				target_is_liege_or_above = $CHARACTER$
				NOT = {
					matchmaker = {
						is_parent_of = prev
						this != $CHARACTER$
					}
				}
			}
			AND = {
				is_playable_character = no
				liege = $CHARACTER$
			}
		}
	}
}

cost_prestige_for_break_betrothal_trigger = {
	OR = {
		scope:rejected_betrothed = {
			OR = {
				is_close_family_of = scope:actor
				AND = {
					exists = dynasty
					exists = scope:actor.dynasty
					dynasty = scope:actor.dynasty
				}
			}
		}
		scope:rejecting_betrothed = {
			OR = {
				is_close_family_of = scope:actor
				AND = {
					exists = dynasty
					exists = scope:actor.dynasty
					dynasty = scope:actor.dynasty
				}
			}
		}
		scope:actor = {
			yields_alliance = {
				candidate = scope:rejecting_betrothed
				target = scope:rejected_betrothal_owner
				target_candidate = scope:rejected_betrothed
			}
		}
	}
}

has_proper_reason_to_break_betrothal_trigger = {
	OR = {
		scope:rejecting_betrothed = {
			OR = {
				is_eunuch_trigger = yes
				has_trait_with_flag = can_not_marry
				AND = {
					allowed_to_marry_same_sex_trigger = no
					sex_same_as = scope:rejected_betrothed
				}
			}
		}
		scope:rejected_betrothed = {
			OR = {
				is_eunuch_trigger = yes
				has_trait_with_flag = can_not_marry
				AND = {
					allowed_to_marry_same_sex_trigger = no
					sex_same_as = scope:rejecting_betrothed
				}
			}
		}
	}
}
