﻿# used in marriage introductions
is_within_introduction_range = {
	trigger_if = {
		limit = { is_in_the_same_court_as = $PROPOSER$ }
		always = yes
	}
	trigger_else_if = {
		limit = { has_game_rule = lmf_expanded_introductions }
		in_diplomatic_range = $PROPOSER$
	}
	trigger_else = {
		location = {
			squared_distance = { target = $PROPOSER$.capital_province value < 500000 }
		}
	}
}

resides_in_court_of_actor = {
	OR = {
		AND = {
			exists = host
			host = scope:actor
		}
		this = scope:actor
	}
}

# these are reproducing the regular marriage triggers to include the Will Not Wed trait (which ordinarily prevents marriage)
can_be_introduced_common_trigger = {
	is_alive = yes
	
	trigger_if = {
		limit = { has_trait_with_flag = can_not_marry }
		has_trait = will_not_wed
		any_owned_story = { type = lmf_will_not_wed_story_cycle }
		any_character_trait = {
			has_trait_flag = can_not_marry
			count < 2
		}
	}

	trigger_if = {
		limit = { exists = primary_title }
		NOR = {
			primary_title = { is_mercenary_company = yes }
			is_leading_faction_type = peasant_faction
			is_leading_faction_type = populist_faction
			government_has_flag = government_is_herder
		}
	}
	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
		}
		always = no
	}
}

can_be_introduced_trigger = {
	can_be_introduced_common_trigger = yes
	allowed_more_spouses = yes
}

introduction_interaction_can_be_picked_trigger = {
	can_be_introduced_trigger = yes
	is_imprisoned = no
	is_hostage = no
}

could_be_introduced_character_trigger = {
	save_temporary_scope_as = can_be_introduced_check
	can_be_introduced_common_trigger = yes
	$CHARACTER$ = { can_be_introduced_common_trigger = yes }
	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_be_introduced_check
				CHARACTER_2 = $CHARACTER$
			}
		}
	}
	trigger_if = {
		limit = { $CHARACTER$ = { NOT = { is_courtier_of = scope:can_be_introduced_check } } } #If you're someone's courtier, your liege can marry you anyway
		$CHARACTER$.faith = {
			faith_allows_marriage_consanguinity_trigger = {
				CHARACTER_1 = scope:can_be_introduced_check
				CHARACTER_2 = $CHARACTER$
			}
		}
	}
	trigger_if = {
		limit = {
			any_close_or_extended_family_member = {
				any_spouse = { this = $CHARACTER$ }
			}
		}
		faith = { has_doctrine = doctrine_consanguinity_unrestricted }
		$CHARACTER$ = {
			faith = { has_doctrine = doctrine_consanguinity_unrestricted }
			NOT = {
				any_spouse = {
					is_close_or_extended_family_of = scope:can_be_introduced_check
					NOT = {
						faith = { has_doctrine = doctrine_consanguinity_unrestricted }
					}
				}
			}
		}
	}
	# Cannot marry self
	NOT = {
		scope:can_be_introduced_check = { is_spouse_of = $CHARACTER$ }
	}
}

can_be_introduced_character_trigger = {
	save_temporary_scope_as = can_introduce_char_check

	can_be_introduced_trigger = yes
	$CHARACTER$ = { can_be_introduced_trigger = yes }

	is_concubine = no
	$CHARACTER$ = { is_concubine = no }

	could_be_introduced_character_trigger = { CHARACTER = $CHARACTER$ }
}

introduction_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_be_introduced_character_trigger = { CHARACTER = scope:secondary_recipient }
			}
		}
	}
	#Betrothal
	trigger_else = {
		scope:secondary_actor = {
			custom_description = {
				text = "can_betroth_check_secondary_actor"
				subject = scope:secondary_actor
				object = scope:secondary_recipient
				can_be_introduced_character_trigger = { CHARACTER = scope:secondary_recipient }
			}
		}
	}

	# 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 }
		}
	}
}

# evaluating whether SUITOR is seen as an attractive marriage candidate by CHARACTER, THRESHOLD is normally 0 (so looking for a positive number)
is_pleased_by_suitor_trigger = {
	$SUITOR$ = { save_temporary_scope_as = candidate }

	$CHARACTER$ = {
		save_temporary_opinion_value_as = {
			name = match_opinion
			target = scope:candidate
		}
		save_temporary_scope_as = match
	}

	$SUITOR$.introduction_candidate_interest_value > $THRESHOLD$
}

# used for heir introductions
heir_match_is_matrilineal = {
	# male heirs always patrilineal match
	trigger_if = {
		limit = {
			$HEIR$ = { is_male = yes }
		}
		always = no
	}
	# always matrilineal against lowborn match
	trigger_else_if = {
		limit = {
			$MATCH$ = { is_lowborn = yes }
		}
		always = yes
	}
	# content or humble female heir otherwise always patrilineal
	trigger_else_if = {
		limit = {
			$HEIR$ = {
				OR = {
					has_trait = content
					has_trait = humble
				}
			}
		}
		always = no
	}
	# assuming none of the above, female heir gaining titles is matrilineal
	trigger_else_if = {
		limit = {
			$HEIR$ = {
				any_heir_title = {}
				# provided game rules allow it
				trigger_if = {
					limit = { has_game_rule = matrilineal_marriages_never }
					OR = {
						host = { has_realm_law = female_only_law }
						host = { has_realm_law = female_preference_law }
						faith = { has_doctrine_parameter = female_dominated_law }
					}
				}
				trigger_if = {
					limit = { has_game_rule = matrilineal_marriages_female_and_equal }
					OR = {
						host = { has_realm_law = female_only_law }
						host = { has_realm_law = female_preference_law }
						host = { has_realm_law = equal_law }
						faith = { has_doctrine_parameter = female_dominated_law }
					}
				}
			}
		}
		always = yes
	}
	# female heirs not gaining titles are still matrilineal against certain matches
	trigger_else_if = {
		limit = {
			$MATCH$ = {
				OR = {
					# they have no liege
					is_pool_character = yes
					# they are a courtier with no important family and no titles to come
					AND = {
						is_courtier = yes
						NOR = {
							any_heir_title = {}
							any_close_or_extended_family_member = { is_ruler = yes }
						}
					}
					# they are a baron or mayor
					AND = {
						is_ruler = yes
						highest_held_title_tier = tier_barony
					}
				}
			}
			# provided game rules allow it
			trigger_if = {
				limit = { has_game_rule = matrilineal_marriages_never }
				OR = {
					$HEIR$.host = { has_realm_law = female_only_law }
					$HEIR$.host = { has_realm_law = female_preference_law }
					$HEIR$.faith = { has_doctrine_parameter = female_dominated_law }
				}
			}
			trigger_if = {
				limit = { has_game_rule = matrilineal_marriages_female_and_equal }
				OR = {
					$HEIR$.host = { has_realm_law = female_only_law }
					$HEIR$.host = { has_realm_law = female_preference_law }
					$HEIR$.host = { has_realm_law = equal_law }
					$HEIR$.faith = { has_doctrine_parameter = female_dominated_law }
				}
			}
		}
		always = yes
	}
	# otherwise, always patrilineal
	trigger_else = {
		always = no
	}
}

