﻿ namespace = lmf_scheme

################################
# COURTIER SEDUCTION & ROMANCE #
################################

scripted_trigger court_within_reasonable_distance_of_root_trigger = {
	OR = {
		this = root.liege
		AND = {
			exists = capital_county
			exists = root.liege.capital_county
			# capital is in a neighboring duchy to my liege's capital duchy
			capital_county.duchy = {
				any_title_to_title_neighboring_and_across_water_duchy = { this = root.liege.capital_county.duchy }
			}
		}
	}
}

scripted_trigger is_appropriate_ai_elopement_courtier = {
	is_physically_able_ai_adult = yes
	num_of_relation_lover > 0
	is_travelling = no
	is_old_age_or_not_have_children = no
	NOR = {
		has_trait = celibate
		has_trait = infirm
		has_trait = eunuch
		has_variable = waiting_to_elope # handled via story
		is_involved_in_any_romantic_scheme = yes
		has_character_flag = made_recent_elope_check
	}
	save_temporary_scope_as = potential_elope
	any_relation = {
		type = lover
		can_elope_character_trigger = { CHARACTER = scope:potential_elope }
		would_need_elopement_to_marry_target = { TARGET = scope:potential_elope }
		NOT = { has_variable = waiting_to_elope }
	}
	# women only initiate elopement in non-male dominated faiths if a ruler or very bold (and vice versa)
	trigger_if = {
		limit = { is_female = yes }
		OR = {
			faith = { has_doctrine = doctrine_gender_female_dominated }
			faith = { has_doctrine = doctrine_gender_equal }
			ai_boldness >= high_positive_ai_value
		}
	}
	trigger_else = {
		OR = {
			faith = { has_doctrine = doctrine_gender_male_dominated }
			faith = { has_doctrine = doctrine_gender_equal }
			ai_boldness >= high_positive_ai_value
		}
	}
}

scripted_trigger relation_proposal_check = {
	any_relation = {
		type = $TYPE$
		is_appropriate_ai_proposal_target = { PROPOSER = $CHARACTER$ }
	}
}

scripted_trigger will_make_nonlover_proposal = {
	NOT = { has_character_flag = no_nonlover_proposals }
	# will never propose to anyone other than soulmate
	num_of_relation_soulmate < 1
	# will only marry non-lovers if they would cheat on them
	trigger_if = {
		limit = { num_of_relation_lover > 0 }
		might_cheat_on_every_partner_trigger = yes
		NOR = {
			# but not if their lover is also their liege
			liege ?= { has_relation_lover = $CHARACTER$ }
			# and not if they have a lover they could propose to
			relation_proposal_check = { TYPE = lover CHARACTER = $CHARACTER$ }
		}
	}
}

scripted_trigger is_appropriate_ai_proposal_courtier = {
	is_available_ai_adult = yes
	is_old_age_or_not_have_children = no
	might_agree_to_marriage = yes
	is_healthy = yes
	has_any_debilitating_illness = no
	OR = {
		has_dynasty = yes
		has_usable_hook = liege
	}
	# use introduction trigger because courtiers with "Will Not Wed" will still propose
	can_be_introduced_common_trigger = yes
	bp2_valid_for_standard_interactions_trigger = yes
	exists = location

	NOR = {
		is_concubine = yes
		has_character_flag = made_recent_proposal_check
		is_involved_in_any_romantic_scheme = yes
		liege = { has_character_flag = player_received_recent_proposal }
		has_relation_rival = liege
		has_relation_lover = liege
		has_relation_soulmate = liege
		has_opinion_modifier = { target = liege modifier = refused_marriage_permission_opinion }
		AND = {
			exists = inspiration
			has_completed_inspiration = no
		}
	}

	save_temporary_scope_as = potential_proposal_courtier

	# female proposers must be within acceptable age range
	trigger_if = {
		limit = { is_female = yes }
		age <= marriage_female_fertility_cutoff_age_value
	}

	# must be 20 years old or have a lover they can propose to
	OR = {
		age >= 20
		relation_proposal_check = { TYPE = lover CHARACTER = scope:potential_proposal_courtier }
		relation_proposal_check = { TYPE = soulmate CHARACTER = scope:potential_proposal_courtier }
	}

	# can't be pregnant unless there's a lover or soulmate they can marry
	OR = {
		is_pregnant = no
		relation_proposal_check = { TYPE = lover CHARACTER = scope:potential_proposal_courtier }
		relation_proposal_check = { TYPE = soulmate CHARACTER = scope:potential_proposal_courtier }
	}

	# must be liege's faith or astray
	OR = {
		faith = liege.faith
		liege.faith = {
			faith_hostility_level = {
				target = scope:potential_proposal_courtier.faith
				value <= faith_astray_level
			}
		}
	}

	# cannot have traits objectionable to liege's faith
	NOT = {
		has_any_shunned_or_criminal_trait_in_faith_trigger = { FAITH = scope:potential_proposal_courtier.liege.faith CHARACTER = scope:potential_proposal_courtier }
	}

	# will only propose to soulmate, if they have one
	trigger_if = {
		limit = { num_of_relation_soulmate > 0 }
		relation_proposal_check = { TYPE = soulmate CHARACTER = scope:potential_proposal_courtier }
	}
	# for lovers, must be willing to cheat or propose
	trigger_else_if = {
		limit = { num_of_relation_lover > 0 }
		# lotharios don't propose
		num_of_relation_lover < 2
		OR = {
			will_make_nonlover_proposal = { CHARACTER = scope:potential_proposal_courtier }
			relation_proposal_check = { TYPE = lover CHARACTER = scope:potential_proposal_courtier }
		}
	}
	trigger_else = {
		always = yes
	}
}

scripted_trigger is_appropriate_ai_proposal_target = {
	# is adult available for a proposal
	is_available_ai_adult = yes
	is_old_age_or_not_have_children = no
	might_agree_to_marriage = yes
	is_healthy = yes
	is_ruler = no
	is_concubine = no
	has_any_debilitating_illness = no
	is_interesting_character_trigger = yes
	exists = location
	bp2_valid_for_standard_interactions_trigger = yes

	# must be 20+ unless the proposer is a lover or someone important
	OR = {
		age >= 20
		has_relation_lover = $PROPOSER$
		$PROPOSER$ = {
			OR = {
				is_playable_character = yes
				any_close_or_extended_family_member = { is_playable_character = yes }
				any_heir_title = {}
			}
		}
	}

	# female targets must be within acceptable age range
	trigger_if = {
		limit = { is_female = yes }
		age <= marriage_female_fertility_cutoff_age_value
	}

	# age range compared to the proposer
	trigger_if = {
		limit = {
			$PROPOSER$ = {
				is_female = yes
				age < 30
			}
		}
		age <= $PROPOSER$.age_plus_25
		age >= $PROPOSER$.age_minus_10
	}
	trigger_else_if = {
		limit = { $PROPOSER$ = { is_female = yes } }
		age <= $PROPOSER$.age_plus_25
		age >= $PROPOSER$.age_minus_5
	}
	trigger_else = {
		age <= $PROPOSER$.age_plus_5
	}

	save_temporary_scope_as = proposal_candidate

	# will only accept proposals from non-lovers under conditions
	trigger_if = {
		limit = { num_of_relation_soulmate > 0 }
		has_relation_soulmate = $PROPOSER$
	}
	trigger_else_if = {
		limit = { num_of_relation_lover > 0 }
		OR = {
			has_relation_lover = $PROPOSER$
			AND = {
				might_cheat_on_every_partner_trigger = yes
				NOT = {
					any_relation = {
						type = lover
						OR = {
							is_playable_character = yes
							has_trait = will_not_wed
							any_heir_title = {}
							any_close_family_member = {
								is_close_family_of = prev
								is_playable_character = yes
							}
						}
					}
				}
			}
		}
	}
	trigger_else = {
		always = yes
	}

	# can't be pregnant unless the proposer is their lover
	OR = {
		is_pregnant = no
		has_relation_lover = $PROPOSER$
	}

	NOR = {
		this = $PROPOSER$
		is_malformed_trigger = yes
		is_involved_in_any_romantic_scheme = yes
		is_close_or_extended_family_of = $PROPOSER$
		has_relation_rival = $PROPOSER$.liege
		has_relation_rival = $PROPOSER$
		is_diarch = yes
		$PROPOSER$.liege = { has_imprisonment_reason = scope:proposal_candidate }
		# lotharios won't accept proposals
		num_of_relation_lover > 1
		# don't grab stray seed mothers unless their child is already born
		any_owned_story = {
			type = lmf_stray_seed_story_cycle
			NOT = { exists = var:stray_seed_child }
		}
		AND = {
			exists = liege
			OR = {
				# can't have their liege as their lover
				has_relation_lover = liege
				# liege has already refused permission to marry
				has_opinion_modifier = { target = liege modifier = refused_marriage_permission_opinion }
				# liege is a player who received a recent proposal
				liege = { has_character_flag = player_received_recent_proposal }
				# their liege and my liege won't talk
				liege = { is_at_war_with = $PROPOSER$.liege }
			}
		}
		would_need_elopement_to_marry_target = { TARGET = $PROPOSER$ }
		AND = {
			exists = inspiration
			has_completed_inspiration = no
		}
	}

	# will only marry clergy if pious and same faith
	trigger_if = {
		limit = { is_clergy = yes }
		faith = $PROPOSER$.faith
		$PROPOSER$ = { ai_zeal >= 0 }
	}
	trigger_if = {
		limit = { $PROPOSER$ = { is_clergy = yes } }
		faith = $PROPOSER$.faith
		ai_zeal >= 0
	}

	# must be same faith or less than evil
	OR = {
		faith = $PROPOSER$.faith
		$PROPOSER$.faith = {
			faith_hostility_level = {
				target = scope:proposal_candidate.faith
				value <= faith_hostility_prevents_marriage_level
			}
		}
	}
	# must be same culture or at least not completely unaccepted
	OR = {
		culture = $PROPOSER$.culture
		$PROPOSER$.culture = {
			cultural_acceptance = {
				target = scope:proposal_candidate.culture
				value > 0
			}
		}
	}

	# use introduction since "Will Not Wed" characters can still be proposed to
	can_be_introduced_character_trigger = { CHARACTER = $PROPOSER$ }
}

scripted_trigger has_spouse_to_reunite = {
	NOR = {
		# is the courtier an adulterer who wouldn't want his spouse to come?
		has_trait = adulterer
		likely_to_take_lover_trigger = yes
		# or too old for us to care about?
		is_old_age_or_not_have_children = yes
	}
	primary_spouse ?= {
		# spouse in court or pool
		OR = {
			is_courtier = yes
			is_pool_character = yes
		}
		exists = location
		# who isn't busy?
		is_available_ai = yes
		has_recoverable_disease_trigger = no
		is_hostage = no
		NOR = {
			# who isn't in the same court
			is_courtier_of = $REUNITOR$.liege
			is_pool_guest_of = $REUNITOR$.liege
			# doesn't hate their spouse or his liege
			has_relation_rival = $REUNITOR$.liege
			has_relation_rival = $REUNITOR$
			$REUNITOR$.liege = { has_imprisonment_reason = $REUNITOR$.primary_spouse }
			# isn't an adulterer or too old for us to care about
			has_trait = adulterer
			is_old_age_or_not_have_children = yes
			# doesn't have any children who aren't the spouse's children
			any_child = {
				is_adult = no
				NOT = { is_child_of = $REUNITOR$ }
			}
			# and if they do have children, none of them are busy or sick
			any_child = {
				is_adult = no
				is_child_of = $REUNITOR$
				trigger_if = {
					limit = { $REUNITOR$.primary_spouse = { is_courtier = yes } }
					is_courtier_of = $REUNITOR$.primary_spouse.liege
				}
				trigger_else = {
					is_at_same_location = $REUNITOR$.primary_spouse
				}
				OR = {
					is_hostage = yes
					is_available_ai = no
					has_recoverable_disease_trigger = yes
				}
			}
		}
		# for courtier spouses, would they lieve their liege? and is their liege currently in a pandemic?
		trigger_if = {
			limit = { is_courtier = yes }
			would_leave_liege_trigger = { CURRENT_LIEGE = $REUNITOR$.primary_spouse.liege NEW_LIEGE = $REUNITOR$.liege }
			NOR = {
				liege = { has_character_modifier = isolating_modifier }
				liege.capital_county ?= { has_county_modifier = isolate_capital_decision_modifier }
			}
		}
	}
}

scripted_trigger is_child_to_reunite = {
	# in court or pool
	OR = {
		is_courtier = yes
		is_pool_character = yes
	}
	# who aren't in their court?
	NOT = { is_courtier_of = $REUNITOR$.liege }
	exists = location
	# and aren't busy or ill?
	is_available_ai_child = yes
	is_hostage = no
	has_recoverable_disease_trigger = no
	save_temporary_scope_as = child_check
	# and aren't in the court with another parent or liege relative?
	trigger_if = {
		limit = { is_courtier = yes }
		NOR = {
			any_parent = { is_courtier_of = scope:child_check.liege }
			is_close_or_extended_family_of = scope:child_check.liege
			has_opinion_modifier = { modifier = demanded_recruitment target = scope:child_check.liege }
			has_opinion_modifier = { modifier = loyal_servant target = scope:child_check.liege }
			liege = { has_hook = prev }
			liege = { is_ai = no }
		}
	}
	# or in the same location as a pool parent?
	trigger_else = {
		NOT = {
			any_parent = { is_at_same_location = scope:child_check }
		}
	}
}

scripted_trigger is_appropriate_ai_reunite_courtier = {
	save_temporary_scope_as = spouse_check
	# just a quick check at the start to eliminate most courtiers
	OR = {
		primary_spouse ?= {
			NOR = {
				is_courtier_of = scope:spouse_check.liege
				is_pool_guest_of = scope:spouse_check.liege
			}
		}
		any_child = {
			is_adult = no
			NOR = {
				is_courtier_of = scope:spouse_check.liege
				is_pool_guest_of = scope:spouse_check.liege
			}
		}
	}
	exists = location
	NOR = {
		has_relation_rival = liege
		liege = { has_character_flag = player_received_recent_proposal }
		has_character_flag = made_recent_reunite_check
		has_opinion_modifier = { target = liege modifier = refused_reunion_permission_opinion }
	}
	# make sure they're someone of significance
	OR = {
		has_dynasty = yes
		has_usable_hook = liege
	}
	# who cares what happens to their wife or children
	ai_honor >= -10
	# and not currently busy or sick
	is_available_ai = yes
	has_recoverable_disease_trigger = no
	OR = {
		# first, do they have a courtier/pool wife?
		has_spouse_to_reunite = { REUNITOR = scope:spouse_check }
		# next, do they have any courtier/pool children?
		any_child = {
			is_child_to_reunite = { REUNITOR = scope:spouse_check }
		}
	}
}

scripted_trigger has_spouse_to_visit = {
	# I can still have children
	fertility >= 0.20
	primary_spouse ?= {
		# with a spouse that's a courtier or pool char
		OR = {
			is_courtier = yes
			is_pool_character = yes
		}
		# that's not in my court
		NOR = {
			is_courtier_of = $VISITOR$.liege
			is_pool_guest_of = $VISITOR$.liege
		}
		# see if this is a player-only realistic pregnancy rule
		trigger_if = {
			limit = { has_game_rule = lmf_realistic_pregnancies_player_only }
			OR = {
				should_use_realistic_pregnancy_player_only = yes
				$VISITOR$ = { should_use_realistic_pregnancy_player_only = yes }
			}
		}
		exists = location
		# that can still have children
		is_old_age_or_not_have_children = no
		fertility >= 0.20
		# and is not busy or sick
		is_available_ai = yes
		has_recoverable_disease_trigger = no
		is_hostage = no
		# we don't have children yet
		any_child = {
			is_child_of = $VISITOR$
			count < 1
		}
		# and isn't pregnant, in the local province, or in a pandemic
		NOR = {
			has_trait = pregnant
			is_at_same_location = $VISITOR$
			location.county ?= { any_county_province = { any_province_epidemic = { count >= 1 } } }
			has_variable = visited_by_target
		}
		# or at a court that's at war or closed dur to pandemic
		trigger_if = {
			limit = { is_courtier = yes }
			NOR = {
				liege = { is_at_war = yes }
				liege = { has_character_modifier = isolating_modifier }
				liege.capital_county ?= { has_county_modifier = isolate_capital_decision_modifier }
			}
		}
		# and reasonably nearby
		in_diplomatic_range = $VISITOR$
	}
}

scripted_trigger has_lover_to_visit = {
	# in court or pool
	OR = {
		is_courtier = yes
		is_pool_character = yes
	}
	exists = location
	# who isn't at the same court
	NOR = {
		is_courtier_of = $VISITOR$.liege
		is_pool_guest_of = $VISITOR$.liege
	}
	# see if this is a player-only realistic pregnancy rule
	trigger_if = {
		limit = { has_game_rule = lmf_realistic_pregnancies_player_only }
		OR = {
			should_use_realistic_pregnancy_player_only = yes
			$VISITOR$ = { should_use_realistic_pregnancy_player_only = yes }
		}
	}
	# who might still have children
	is_old_age_or_not_have_children = no
	fertility >= 0.20
	# and isn't busy or sick
	is_available_ai = yes
	has_recoverable_disease_trigger = no
	is_hostage = no
	# and not pregnant or in the same location, with no pandemic
	NOR = {
		is_at_same_location = $VISITOR$
		has_trait = pregnant
		location.county ?= { any_county_province = { any_province_epidemic = { count >= 1 } } }
		has_variable = visited_by_target
	}
	# and not in a court at war or closed due to pandemic
	trigger_if = {
		limit = { is_courtier = yes }
		NOR = {
			liege = { is_at_war = yes }
			liege = { has_character_modifier = isolating_modifier }
			liege.capital_county ?= { has_county_modifier = isolate_capital_decision_modifier }
		}
	}
	# and reasonably nearby
	in_diplomatic_range = $VISITOR$
}

scripted_trigger is_appropriate_ai_visit_courtier = {
	save_temporary_scope_as = visit_check
	# just a quick check at the start to eliminate most courtiers
	OR = {
		primary_spouse ?= {
			NOR = {
				is_courtier_of = scope:visit_check.liege
				is_pool_guest_of = scope:visit_check.liege
			}
		}
		any_relation = {
			type = lover
			NOR = {
				is_courtier_of = scope:visit_check.liege
				is_pool_guest_of = scope:visit_check.liege
			}
		}
	}
	exists = location
	# make sure they're someone of significance
	has_dynasty = yes
	is_old_age_or_not_have_children = no
	# and not currently busy or sick
	is_available_ai = yes
	is_hostage = no
	has_recoverable_disease_trigger = no
	# and not pregnant or lazy
	NOR = {
		has_character_flag = made_recent_courtier_visit
		has_trait = pregnant
		ai_energy <= -50
		has_variable = visited_by_target
	}
	OR = {
		# has spouse available to visit
		has_spouse_to_visit = { VISITOR = scope:visit_check }
		AND = {
			# I'm stressed or lustful or unmarried
			OR = {
				stress >= normal_baseline_stress
				has_trait = lustful
				is_deviant_trigger = yes
				is_married = no
			}
			# has lover available to visit
			any_relation = {
				type = lover
				has_lover_to_visit = { VISITOR = scope:visit_check }
			}
		}
	}
}

scripted_trigger proposer_is_considered_important_marriage = {
	scope:proposer = {
		OR = {
			is_close_or_extended_family_of = root
			is_consort_of = root
			is_player_heir_of = root
			has_relation_friend = root
			is_hostage_of = root
			dynasty ?= root.dynasty
			AND = {
				OR = {
					is_knight_of = root
					is_councillor_of = root
					has_any_court_position = yes
				}
				proposer_will_leave_your_court = yes
			}
		}
	}
}

scripted_trigger proposer_target_is_considered_important_marriage = {
	scope:proposer_target = {
		OR = {
			is_close_or_extended_family_of = root
			is_consort_of = root
			is_player_heir_of = root
			has_relation_friend = root
			is_hostage_of = root
			dynasty ?= root.dynasty
			AND = {
				OR = {
					is_knight_of = root
					is_councillor_of = root
					has_any_court_position = yes
				}
				proposer_target_will_leave_your_court = yes
			}
		}
	}
}

scripted_trigger courtier_wants_weapon = {
	OR = {
		is_knight_of = root
		AND = {
			can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = root }
			has_education_martial_trigger = yes
		}
	}
	OR = {
		NOT = {
			any_character_artifact = { artifact_slot_type = primary_armament }
		}
		any_character_artifact = {
			count = all
			artifact_slot_type = primary_armament
			OR = {
				var:quality < 25
				ep1_artifact_durability_lower_equal_percent_trigger = { PERCENT = 0.24 } # could be replaced by checking LOW_DURATION
			}
		}
	}
	NOR = {
		has_trait = humble
		has_trait = content
	}
}

scripted_trigger courtier_wants_gold = {
	OR = {
		ai_greed >= 20
		has_trait = ambitious
		has_council_position = councillor_court_chaplain
		is_clergy = yes
	}
}

scripted_trigger courtier_wants_position = {
	is_councillor = no
	has_any_court_position = no
	highest_skill_value >= medium_skill_rating
	OR = {
		has_trait = ambitious
		ai_boldness >= 20
		ai_honor <= -20
	}
	NOR = {
		has_trait = lazy
		has_trait = humble
		has_trait = content
	}
	can_be_employed_as_any_court_position_trigger = yes
}

# Main bounce event for courtier proposals, bringing spouses to court, visits
lmf_scheme.100 = {
	type = character_event

	hidden = yes

	trigger = {
		exists = capital_province
		NOR = {
			has_government = mercenary_government
			has_government = republic_government
			has_government = theocracy_government
			has_government = holy_order_government
		}
	}

	immediate = {
		# First set scope if this is a player's realm
		if = {
			limit = {
				is_player_realm_ruler = { RULER = root }
			}
			save_scope_value_as = {
				name = is_player_realm
				value = flag:yes
			}
		}
		
		# And determine what the pool province size is
		if = {
			limit = {
				exists = scope:is_player_realm
				exists = root.capital_province.kingdom
			}
			every_realm_province = {
				limit = { kingdom = root.capital_province.kingdom }
				add_to_list = pool_provinces
			}
		}
		else_if = {
			limit = { exists = capital_province }
			capital_province = { add_to_list = pool_provinces }
		}
		else_if = {
			limit = { exists = domicile.domicile_location }
			domicile.domicile_location = { add_to_list = pool_provinces }
		}
		
		# Now find appropriate courtier to check
		random_courtier = {
			limit = { is_appropriate_ai_proposal_courtier = yes }
			weight = {
				base = 5
				ai_value_modifier = {
					ai_boldness = 0.25
					ai_honor = 0.25
					min = -25
					max = 25
				}
				modifier = {
					add = 10
					age >= 25
					is_female = no
				}
				modifier = {
					add = 10
					age >= 30
					is_female = yes
				}
				modifier = {
					add = 25
					is_of_major_or_minor_interest_trigger = { CHARACTER = root }
				}
				modifier = {
					add = 10
					has_trait = diligent
				}
				modifier = {
					add = -25
					any_secret = { type = secret_lover }
					OR = {
						has_trait = paranoid
						has_trait = deceitful
					}
				}
				modifier = {
					factor = 0.5
					has_sexuality = homosexual
				}
				modifier = {
					factor = 0.5
					has_trait = lazy
				}
				modifier = {
					factor = 0.5
					is_female = yes
					OR = {
						faith = { has_doctrine = doctrine_gender_male_dominated }
						has_trait = maiden
						has_personality_submissive_trigger = yes
					}
				}
				modifier = {
					factor = 0.5
					is_male = yes
					faith = { has_doctrine = doctrine_gender_female_dominated }
				}
				modifier = {
					add = 100
					OR = {
						num_of_relation_lover > 0
						num_of_relation_soulmate > 0
					}
					save_temporary_scope_as = lover_check
					OR = {
						relation_proposal_check = { TYPE = lover CHARACTER = scope:lover_check }
						relation_proposal_check = { TYPE = soulmate CHARACTER = scope:lover_check }
					}
				}
				modifier = {
					add = 100
					is_pregnant = yes
				}
				modifier = {
					add = 50
					has_trait = will_not_wed
					any_owned_story = { type = lmf_will_not_wed_story_cycle }
				}
				modifier = {
					add = 25
					has_usable_hook = root
				}
				min = 1
			}
			trigger_event = lmf_scheme.1030
		}
		
		# See if there's a courtier wanting to reunite
		random_courtier = {
			limit = { is_appropriate_ai_reunite_courtier = yes }
			weight = {
				base = 5
				ai_value_modifier = {
					ai_boldness = 0.25
					ai_honor = 0.25
					min = -25
					max = 25
				}
				modifier = {
					add = 25
					is_of_major_or_minor_interest_trigger = { CHARACTER = root }
				}
				modifier = {
					add = 25
					has_usable_hook = root
				}
				min = 1
			}
			save_scope_as = requester
			trigger_event = { id = lmf_scheme.1040 days = 30 }
		}
	
		# See if there's a courtier wanting to visit their spouse/lover
		if = {
			limit = {
				# Unnecessary if we don't need to worry about distant pregnancies
				OR = {
					has_game_rule = lmf_realistic_pregnancies_active
					has_game_rule = lmf_realistic_pregnancies_player_only
				}
				# Liege can't be at war or in the middle of a pandemic
				NOR = {
					is_at_war = yes
					has_character_modifier = isolating_modifier
					capital_county ?= { has_county_modifier = isolate_capital_decision_modifier }
					capital_county ?= { any_county_province = { any_province_epidemic = { count >= 1 } } }
				}
			}
			random_courtier = {
				limit = { is_appropriate_ai_visit_courtier = yes }
				weight = {
					base = 5
					ai_value_modifier = {
						ai_energy = 0.5
						min = -25
						max = 25
					}
					modifier = {
						add = 25
						is_close_or_extended_family_of = root
					}
					modifier = {
						add = 10
						is_of_major_or_minor_interest_trigger = { CHARACTER = root }
					}
					modifier = {
						add = 10
						is_married = yes
						NOT = { is_at_same_location = primary_spouse }
						any_child = {
							NOT = { has_trait = bastard }
							count < 1
						}
					}
					modifier = {
						factor = 0.5
						is_female = yes
						NOT = { is_close_or_extended_family_of = root }
					}
					modifier = {
						factor = 0.5
						OR = {
							is_councillor_of = root
							has_any_court_position = yes
							is_knight_of = root
						}
					}
					min = 1
				}
				trigger_event = { id = lmf_scheme.1050 days = 60 }
			}
		}
	}
}

# Main bounce event for courtier schemes
lmf_scheme.101 = {
	type = character_event

	hidden = yes

	trigger = {
		NOR = {
			has_government = mercenary_government
			has_government = republic_government
			has_government = theocracy_government
			has_government = holy_order_government
		}
	}

	immediate = {
		# First set scope if this is a player's realm
		if = {
			limit = {
				is_player_realm_ruler = { RULER = root }
			}
			save_scope_value_as = {
				name = is_player_realm
				value = flag:yes
			}
		}
		
		# AI Elopement
		random_courtier = {
			limit = { is_appropriate_ai_elopement_courtier = yes }
			weight = {
				base = 1
				modifier = {
					add = 100
					num_of_relation_soulmate > 0
				}
			}
			trigger_event = lmf_scheme.1000
		}

		# AI Courting
		random_courtier = {
			limit = { is_appropriate_ai_courtship_courtier = yes }
			weight = {
				base = 3
				modifier = {
					add = 2
					age <= 25
				}
				modifier = {
					add = -1
					age >= 35
				}
				modifier = {
					add = 2
					is_of_major_or_minor_interest_trigger = { CHARACTER = root }
				}
				modifier = {
					add = 1
					has_trait = maiden
				}
				modifier = {
					add = -1
					is_lowborn = yes
				}
				modifier = {
					add = 5
					has_trait = will_not_wed
					any_owned_story = { type = lmf_will_not_wed_story_cycle }
				}
			}
			trigger_event = lmf_scheme.1010
		}

		# AI Seduction
		random_courtier = {
			limit = { is_appropriate_ai_seduction_courtier = yes }
			weight = {
				base = 3
				modifier = {
					add = 3
					likely_to_take_lover_trigger = yes
				}
				modifier = {
					add = 2
					age <= 25
				}
				modifier = {
					add = -1
					age >= 35
				}
				modifier = {
					add = 2
					is_of_major_or_minor_interest_trigger = { CHARACTER = root }
				}
				modifier = {
					add = 1
					has_trait = maiden
				}
				modifier = {
					add = -1
					is_lowborn = yes
				}
				modifier = {
					add = 2
					has_trait = will_not_wed
					any_owned_story = { type = lmf_will_not_wed_story_cycle }
				}
			}
			trigger_event = lmf_scheme.1020
		}
	
		# Courtiers using boons
		random_courtier = {
			limit = {
				has_usable_hook = root
				is_available_healthy_ai_adult = yes
				host = { is_available_healthy_adult = yes }
				OR = {
					courtier_wants_weapon = yes
					courtier_wants_gold = yes
					courtier_wants_position = yes
				}
			}
			trigger_event = lmf_scheme.1100
		}
	}
}

# AI Elopement
lmf_scheme.1000 = {
	type = character_event

	hidden = yes

	trigger = {
		is_courtier = yes
		is_ruler = no
		is_physically_able_ai_adult = yes

		num_of_relation_lover > 0
		is_imprisoned = no
		is_travelling = no
		is_old_age_or_not_have_children = no
		NOR = {
			has_trait = celibate
			has_trait = infirm
			is_involved_in_any_romantic_scheme = yes
			has_variable = waiting_to_elope # handled in story
		}
		# women only initiate elopement in non-male dominated faiths if a ruler or very bold (and vice versa)
		trigger_if = {
			limit = { is_female = yes }
			OR = {
				faith = { has_doctrine = doctrine_gender_female_dominated }
				faith = { has_doctrine = doctrine_gender_equal }
				ai_boldness >= high_positive_ai_value
			}
		}
		trigger_else = {
			OR = {
				faith = { has_doctrine = doctrine_gender_male_dominated }
				faith = { has_doctrine = doctrine_gender_equal }
				ai_boldness >= high_positive_ai_value
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = made_recent_elope_check
			years = 5
		}

		if = {
			limit = {
				any_relation = {
					type = lover
					count = all
					is_courtier = yes
					is_physically_able = yes
					is_old_age_or_not_have_children = no
					can_elope_character_trigger = { CHARACTER = root }
					would_need_elopement_to_marry_target = { TARGET = root }
					root = {
						can_start_scheme = {
							type = elope
							target_character = prev
						}
					}
					NOR = {
						any_targeting_scheme = {}
						has_any_illness = yes
						has_variable = waiting_to_elope # handled in story
					}
					add_to_temporary_list = elope_targets
				}
			}
		}

		if = {
			limit = {
				list_size = {
					name = elope_targets
					value > 0
				}
			}
			random_in_list = {
				list = elope_targets
				limit = {
					root = {
						can_start_scheme = {
							target_character = prev
							type = elope
						}
					}
					NOT = { this = root }
				}
				weight = {
					base = 1
					modifier = {
						add = 100
						has_relation_soulmate = root
					}
					opinion_modifier = {
						who = root
						opinion_target = this
						multiplier = 0.5
					}
				}
				random = {
					chance = 100
					ai_value_modifier = {
						ai_boldness = 0.5
						ai_honor = -0.5
					}
					modifier = {
						add = 25
						has_relation_soulmate = root
					}
					modifier = {
						add = {
							value = root.intrigue
							multiply = 4
						}
					}
					modifier = {
						add = {
							value = intrigue
							multiply = 4
						}
					}
					modifier = {
						factor = 0
						NOT = { has_relation_soulmate = root }
						root = {
							ai_rationality > -100 # Lunatics can do whatever
							exists = primary_spouse
							primary_spouse = {
								reverse_opinion = { target = root value >= 20 }
								opinion = { target = root value >= 20 }
							}
						}
					}
					modifier = {
						factor = 0
						NOT = { has_relation_soulmate = root }
						root = {
							ai_rationality > -100 # Lunatics can do whatever
						}
						any_close_family_member = {
							is_close_family_of = root
							OR = {
								has_relation_friend = root
								target_is_liege_or_above = root
							}
						}
					}
					min = 0
					max = 100
					root = { save_scope_as = actor }
					save_scope_as = recipient
					random = {
						chance = 0
						elopment_ai_accept_modifier = yes
						modifier = {
							add = 25
							liege = { is_ai = no }
						}
						min = 0
						max = 100
						scope:actor = {
							begin_scheme_basic_effect = {
								SCHEME_TYPE = elope
								TARGET_TYPE = target_character
								TARGET_SCOPE = scope:recipient
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				liege = {
					NOT = { has_character_flag = check_elope_second_time }
					is_ai = no
				}
			}
			liege = {
				add_character_flag = { flag = check_elope_second_time days = 10 }
				random_courtier = {
					limit = { is_appropriate_ai_elopement_courtier = yes }
					weight = {
						base = 1
						modifier = {
							add = 10
							num_of_relation_soulmate > 0
						}
					}
					trigger_event = lmf_scheme.1000
				}
			}
		}
	}
}

# AI Courting
lmf_scheme.1010 = {
	type = character_event
	
	hidden = yes
	
	immediate = {
		add_character_flag = {
			flag = made_recent_courtship_check
			years = 5
		}
		# in case we're doing this a second time, for player lieges
		every_in_list = {
			list = courting_targets
			remove_from_list = courting_targets
		}
		
		# first track the basic targets for all
		if = {
			limit = {
				any_consort = {
					count = all
					save_temporary_scope_as = potential_ai_court_target
					is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
					add_to_temporary_list = courting_targets
				}
			}
		}
		if = {
			limit = {
				any_relation = {
					type = lover
					count = all
					NOT = { is_in_list = courting_targets }
					save_temporary_scope_as = potential_ai_court_target
					is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
					add_to_temporary_list = courting_targets
				}
			}
		}
		if = {
			limit = {
				liege ?= {
					any_courtier_or_guest = {
						count = all
						NOT = { is_in_list = courting_targets }
						save_temporary_scope_as = potential_ai_court_target
						is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
						add_to_temporary_list = courting_targets
					}
				}
			}
		}
		if = {
			limit = {
				any_pool_character = {
					province = root.location
					count = all
					NOT = { is_in_list = courting_targets }
					save_temporary_scope_as = potential_ai_court_target
					is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
					add_to_temporary_list = courting_targets
				}
			}
		}
		
		# then the more advanced targets specific to player realms
		if = {
			limit = {
				exists = scope:is_player_realm
				liege ?= { top_liege != this }
				liege.liege ?= {
					court_within_reasonable_distance_of_root_trigger = yes
					any_courtier_or_guest = {
						count = all
						NOT = { is_in_list = courting_targets }
						save_temporary_scope_as = potential_ai_court_target
						is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
						add_to_temporary_list = courting_targets
					}
				}
			}
		}
		if = {
			limit = {
				exists = scope:is_player_realm
				liege ?= {
					any_vassal = {
						count = all
						court_within_reasonable_distance_of_root_trigger = yes
						trigger_if = {
							limit = {
								NOT = { is_in_list = courting_targets }
								save_temporary_scope_as = potential_ai_court_target
								is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
								is_ai = yes
							}
							add_to_temporary_list = courting_targets
						}
						trigger_else = {
							always = yes
						}
						any_courtier_or_guest = {
							count = all
							NOT = { is_in_list = courting_targets }
							save_temporary_scope_as = potential_ai_court_target
							is_appropriate_ai_courtship_target = { LOVER = root TARGET = scope:potential_ai_court_target }
							add_to_temporary_list = courting_targets
						}
					}
				}
			}
		}

		if = {
			limit = { list_size:courting_targets > 0 }
			random_in_list = {
				list = courting_targets
				# first priority: lovers
				limit = { has_relation_lover = root }
				# second priority: consorts
				alternative_limit = { is_consort_of = root }
				# third priority: we can get married now
				alternative_limit = { can_be_introduced_character_trigger = { CHARACTER = root } }
				# fourth priority: we're at the same location and we like each other
				alternative_limit = {
					opinion = { target = root value > 0 }
					reverse_opinion = { target = root value > 0 }
					is_at_same_location = root
				}
				# fifth priority: they're attractive and we like each other
				alternative_limit = {
					opinion = { target = root value > 0 }
					reverse_opinion = { target = root value > 0 }
					attraction >= 0
				}
				# otherwise? anyone
				alternative_limit = { always = yes }
				weight = {
					base = 1
					modifier = {
						add = 100
						is_consort_of = root
					}
					modifier = {
						add = 100
						OR = {
							has_relation_lover = root
							root = { has_relation_potential_lover = prev }
							root = { has_relation_crush = prev }
						}
					}
					modifier = {
						add = 50
						reverse_has_opinion_modifier = { modifier = childhood_crush target = root }
					}
					modifier = {
						add = 20
						any_spouse = { is_playable_character = yes }
					}
					modifier = {
						add = 5
						liege ?= { is_ai = no }
					}
					modifier = {
						add = {
							value = attraction
							multiply = 2
						}
					}
					opinion_modifier = {
						who = root
						opinion_target = this
						multiplier = 0.5
					}
					compatibility_modifier = {
						who = root
						compatibility_target = this
						multiplier = 5
						max = court_scheme_max_compatability_value
					}
					modifier = {
						add = -5
						is_female = yes
						root = { is_male = yes }
						age > root.age
					}
					modifier = {
						factor = 0.75
						attraction < root.attraction
					}
					modifier = {
						factor = 0.75
						exists = liege
						NOT = { liege = root.liege }
					}
					modifier = {
						factor = 0.5
						root = { is_female = no }
						is_female = yes
						attraction <= 0
						age >= 30
					}
					modifier = {
						factor = 0.5
						root = {
							relation_with_character_is_incestuous_in_my_faith_trigger = { CHARACTER = prev }
							NOT = { likely_to_commit_incest_trigger = { CHARACTER = prev } }
						}
					}
					modifier = {
						factor = 0.75
						root = { has_sexuality = bisexual }
						sex_same_as = root
					}
					modifier = {
						factor = 0.5
						NOT = { exists = liege }
					}
					modifier = {
						factor = 0.5
						opinion = { target = root value < -10 }
					}
					modifier = {
						factor = 0.1
						is_lowborn = yes
						NOT = { is_consort_of = root }
					}
					modifier = {
						factor = 0.1
						is_unattractive_trigger = yes
					}
					min = 1
				}
				random = {
					chance = 10
					modifier = {
						add = 25
						exists = liege
						liege = { is_ai = no }
					}
					modifier = {
						add = 40
						root = { is_married = no }
						can_be_introduced_character_trigger = { CHARACTER = root }
					}
					modifier = {
						add = 65
						root = {
							OR = {
								has_trait = maiden
								has_trait = will_not_wed
							}
						}
					}
					modifier = {
						add = 5
						is_consort_of = root
					}
					modifier = {
						add = 10
						is_consort_of = root
						age < 25
						root = { age < 25 }
					}
					opinion_modifier = {
						who = root
						opinion_target = this
						multiplier = 0.4
					}
					modifier = {
						add = 15
						culture = { has_cultural_parameter = ai_romance_scheme_increase }
					}
					modifier = {
						add = 25
						has_focus = martial_chivalry_focus
					}
					modifier = {
						add = -50
						root = {
							OR = {
								is_clergy = yes
								has_trait = devoted
								has_council_position = councillor_court_chaplain
								has_trait = septon
								has_trait = silent_sister
							}
							faith = {
								NOT = { has_doctrine_parameter = clergy_can_marry }
							}
						}
					}
					modifier = {
						factor = 0.5
						root = { is_unattractive_trigger = yes }
					}
					min = 0
					max = 100
					root = { save_temporary_scope_as = actor }
					save_temporary_scope_as = recipient
					scope:actor = {
						begin_scheme_basic_effect = {
							SCHEME_TYPE = courting
							TARGET_TYPE = target_character
							TARGET_SCOPE = scope:recipient
						}
						# add a modifier just to give non-rulers a better chance of success with single targets
						if = {
							limit = {
								scope:recipient = { is_married = no }
							}
							random_scheme = {
								type = courting
								add_scheme_modifier = {
									type = determined_courting_modifier
								}
							}
						}
					}
				}
			}
		}
		# courtiers with player lieges will check again
		else_if = {
			limit = {
				liege ?= {
					NOT = { has_character_flag = check_courtship_second_time }
					is_ai = no
				}
			}
			liege = {
				add_character_flag = { flag = check_courtship_second_time days = 10 }
				random_courtier = {
					limit = { is_appropriate_ai_courtship_courtier = yes }
					weight = {
						base = 3
						modifier = {
							add = 2
							age <= 25
						}
						modifier = {
							add = -1
							age >= 35
						}
						modifier = {
							add = 2
							is_of_major_or_minor_interest_trigger = { CHARACTER = liege }
						}
						modifier = {
							add = 1
							has_trait = maiden
						}
						modifier = {
							add = -1
							is_lowborn = yes
						}
						modifier = {
							add = 5
							has_trait = will_not_wed
							any_owned_story = { type = lmf_will_not_wed_story_cycle }
						}
					}
					trigger_event = lmf_scheme.1010
				}
			}
		}
	}
}

# AI Seduction
lmf_scheme.1020 = {
	type = character_event

	hidden = yes

	immediate = {
		add_character_flag = {
			flag = made_recent_seduction_check
			years = 5
		}		
		# in case we're doing this a second time, for player lieges
		every_in_list = {
			list = seduction_targets
			remove_from_list = seduction_targets
		}
		
		# first track the basic targets for all
		if = {
			limit = {
				liege ?= {
					any_courtier_or_guest = {
						count = all
						is_appropriate_ai_seduction_target = { LOVER = root TARGET = this }
						add_to_temporary_list = seduction_targets
					}
				}
			}
		}
		if = {
			limit = {
				any_pool_character = {
					province = root.location
					count = all
					is_appropriate_ai_seduction_target = { LOVER = root TARGET = this }
					add_to_temporary_list = seduction_targets
				}
			}
		}
		
		# then the more advanced targets specific to player realms
		if = {
			limit = {
				exists = scope:is_player_realm
				liege ?= { top_liege != this }
				liege.liege ?= {
					court_within_reasonable_distance_of_root_trigger = yes
					any_courtier_or_guest = {
						count = all
						is_appropriate_ai_seduction_target = { LOVER = root TARGET = this }
						NOT = { is_in_list = seduction_targets }
						add_to_temporary_list = seduction_targets
					}
				}
			}
		}
		if = {
			limit = {
				exists = scope:is_player_realm
				liege ?= {
					any_vassal = {
						count = all
						court_within_reasonable_distance_of_root_trigger = yes
						trigger_if = {
							limit = {
								root = { ai_boldness >= 20 }
								NOT = { is_in_list = seduction_targets }
								is_appropriate_ai_seduction_target = { LOVER = root TARGET = this }
								is_ai = yes
							}
							add_to_temporary_list = seduction_targets
						}
						trigger_else = {
							always = yes
						}
						any_courtier_or_guest = {
							count = all
							is_appropriate_ai_seduction_target = { LOVER = root TARGET = this }
							NOT = { is_in_list = seduction_targets }
							add_to_temporary_list = seduction_targets
						}
					}
				}
			}
		}
		
		if = {
			limit = { list_size:seduction_targets > 0 }
			# courtiers with a non-player liege get a bit less attention to who they target
			if = {
				limit = {
					OR = {
						NOT = { exists = liege }
						liege = { is_ai = yes }
					}
				}
				random_in_list = {
					list = seduction_targets
					weight = {
						base = 1
						modifier = {
							add = 100
							OR = {
								root = { has_relation_potential_lover = prev }
								root = { has_relation_crush = prev }
							}
						}
						modifier = {
							add = 50
							reverse_has_opinion_modifier = { modifier = childhood_crush target = root }
						}
						modifier = {
							add = 50
							is_married = yes
							primary_spouse = {
								OR = {
									has_relation_rival = root
									has_relation_nemesis = root
								}
							}
						}
						modifier = {
							add = -50
							is_close_family_of = root
							root = {
								OR = {
									ai_honor > 0
									ai_rationality > 0
								}
							}
						}
						modifier = {
							add = -20
							is_married = yes
							NOT = { is_consort_of = root }
							root = {
								NOR = {
									has_trait = lustful
									has_focus = intrigue_temptation_focus
								}
							}
						}
						modifier = {
							add = {
								value = attraction
								multiply = 4
							}
						}
						modifier = {
							add = -100
							opinion = { target = root value < -40 }
						}
						modifier = {
							add = -20
							opinion = { target = root value < -20 }
						}
						modifier = {
							add = 10
							opinion = { target = root value >= 0 }
						}
						modifier = {
							add = 10
							opinion = { target = root value >= 20 }
						}
						modifier = {
							add = 20
							likely_to_take_lover_trigger = yes
						}
						modifier = {
							factor = 0.75
							attraction < root.attraction
						}
						modifier = {
							factor = 0.75
							root = { has_sexuality = bisexual }
							sex_same_as = root
						}
						modifier = {
							factor = 0.75
							OR = {
								NOT = { exists = liege }
								NOT = { liege = root.liege }
							}
						}
						modifier = {
							factor = 0.5
							root = { might_agree_to_marriage = yes }
							NOT = { can_be_introduced_character_trigger = { CHARACTER = root } }
						}
						modifier = {
							factor = 0.5
							root = { is_female = no }
							is_female = yes
							attraction <= 0
							OR = {
								age >= 25
								age >= root.age
							}
						}
						modifier = {
							factor = 0.5
							is_close_or_extended_family_of = root
						}
						modifier = {
							factor = 0.5
							number_of_opposing_personality_traits = { target = root value = 1 }
						}
						modifier = {
							factor = 0.5
							NOT = { exists = liege }
						}
						modifier = {
							factor = 0.1
							NOT = { is_attracted_to_gender_of = root }
						}
						modifier = {
							factor = 0.1
							number_of_opposing_personality_traits = { target = root value > 1 }
						}
						min = 1
					}
					save_scope_as = selected_seduction_target
				}
			}
			# courtiers with a player liege evaluate their targets more aggressively
			else = {
				every_in_list = {
					list = seduction_targets
					
					# first figure out how much root is attracted to them
					save_temporary_scope_as = candidate
					root = {
						save_temporary_opinion_value_as = { name = match_opinion target = scope:candidate }
						save_temporary_scope_as = match
					}
					# and save the value
					set_variable = { name = sexual_interest_value value = sexual_attraction_value days = 1 }
					if = {
						limit = { root = { has_relation_potential_lover = prev } }
						change_variable = { name = sexual_interest_value add = 50 }
					}
					if = {
						limit = { root = { has_relation_crush = prev } }
						change_variable = { name = sexual_interest_value add = 25 }
					}
					if = {
						limit = { likely_to_take_lover_trigger = yes }
						change_variable = { name = sexual_interest_value add = 10 }
					}
					if = {
						limit = {
							is_married = yes
							NOT = { is_consort_of = root }
							root = {
								NOR = {
									has_trait = lustful
									has_focus = intrigue_temptation_focus
								}
							}
						}
						change_variable = { name = sexual_interest_value add = -10 }
					}
					if = {
						limit = {
							root = { has_sexuality = bisexual }
							sex_same_as = root
						}
						change_variable = { name = sexual_interest_value add = -10 }
					}
					if = {
						limit = {
							NOT = { exists = liege }
						}
						change_variable = { name = sexual_interest_value add = -10 }
					}
					if = {
						limit = {
							root = { might_agree_to_marriage = yes }
							NOT = { can_be_introduced_character_trigger = { CHARACTER = root } }
						}
						change_variable = { name = sexual_interest_value add = -25}
					}
				}
				ordered_in_list = {
					list = seduction_targets
					limit = {
						var:sexual_interest_value > 0
						is_at_same_location = root
					}
					alternative_limit = {
						var:sexual_interest_value > 0
					}
					order_by = var:sexual_interest_value
					save_scope_as = selected_seduction_target
				}
			}
			
			# Do we actually start the seduction?
			if = {
				limit = { exists = scope:selected_seduction_target }
				random = {
					chance = 10
					modifier = {
						add = 50
						has_trait = will_not_wed
						any_owned_story = { type = lmf_will_not_wed_story_cycle }
					}
					modifier = {
						add = 25
						root.liege = { is_ai = no }
					}
					modifier = {
						add = 25
						scope:selected_seduction_target = { is_married = no }
						is_married = no
						can_be_introduced_character_trigger = { CHARACTER = scope:selected_seduction_target }
					}
					modifier = {
						add = 25
						has_focus = intrigue_temptation_focus
					}
					modifier = {
						add = 25
						root.dynasty ?= { has_dynasty_perk = fp1_adventure_legacy_2 }
					}
					modifier = {
						add = -25
						OR = {
							is_clergy = yes
							has_trait = devoted
							has_trait = septon
							has_trait = silent_sister
						}
						faith = {
							NOT = { has_doctrine = tenet_carnal_exaltation }
						}
						NOR = {
							has_trait = lustful
							has_focus = intrigue_temptation_focus
						}
					}
					modifier = {
						add = -25
						is_close_or_extended_family_of = scope:selected_seduction_target
					}
					modifier = {
						add = 50
						scope:selected_seduction_target = {
							has_sexuality = homosexual
							sex_same_as = root
						}
						ai_boldness > 0
						has_sexuality = homosexual
					}
					modifier = {
						add = 10
						has_trait = lustful
					}
					modifier = {
						factor = 0.5
						is_unattractive_trigger = yes
					}
					min = 0
					max = 100
					save_temporary_scope_as = actor
					scope:selected_seduction_target = { save_temporary_scope_as = recipient }
					scope:actor = {
						begin_scheme_basic_effect = {
							SCHEME_TYPE = seduce
							TARGET_TYPE = target_character
							TARGET_SCOPE = scope:recipient
						}
					}
					# let's make sure recipients can only be targeted for courtier seductions once in a while
					scope:recipient = {
						add_character_flag = { flag = blocked_courtier_seductions years = 5 }
					}
				}
			}
			# courtiers with player lieges will check again
			else_if = {
				limit = {
					liege ?= {
						NOT = { has_character_flag = check_seduce_second_time }
						is_ai = no
					}
				}
				liege = {
					add_character_flag = { flag = check_seduce_second_time days = 10 }
					random_courtier = {
						limit = { is_appropriate_ai_seduction_courtier = yes }
						weight = {
							base = 3
							modifier = {
								add = 3
								likely_to_take_lover_trigger = yes
							}
							modifier = {
								add = 2
								age <= 25
							}
							modifier = {
								add = -1
								age >= 35
							}
							modifier = {
								add = 2
								is_of_major_or_minor_interest_trigger = { CHARACTER = liege }
							}
							modifier = {
								add = 1
								has_trait = maiden
							}
							modifier = {
								add = -1
								is_lowborn = yes
							}
							modifier = {
								add = 2
								has_trait = will_not_wed
								any_owned_story = { type = lmf_will_not_wed_story_cycle }
							}
						}
						trigger_event = lmf_scheme.1020
					}
				}
			}
		}
		# courtiers with player lieges will check again
		else_if = {
			limit = {
				liege ?= {
					NOT = { has_character_flag = check_seduce_second_time }
					is_ai = no
				}
			}
			liege = {
				add_character_flag = { flag = check_seduce_second_time days = 10 }
				random_courtier = {
					limit = { is_appropriate_ai_seduction_courtier = yes }
					weight = {
						base = 3
						modifier = {
							add = 3
							likely_to_take_lover_trigger = yes
						}
						modifier = {
							add = 2
							age <= 25
						}
						modifier = {
							add = -1
							age >= 35
						}
						modifier = {
							add = 2
							is_of_major_or_minor_interest_trigger = { CHARACTER = liege }
						}
						modifier = {
							add = 1
							has_trait = maiden
						}
						modifier = {
							add = -1
							is_lowborn = yes
						}
						modifier = {
							add = 2
							has_trait = will_not_wed
							any_owned_story = { type = lmf_will_not_wed_story_cycle }
						}
					}
					trigger_event = lmf_scheme.1020
				}
			}
		}
	}
}

# AI Proposals
lmf_scheme.1030 = {
	type = character_event
	
	hidden = yes
	
	immediate = {
		add_character_flag = { flag = made_recent_proposal_check years = 5 }
		# in case we're doing this a second time, for player lieges
		every_in_list = {
			list = potential_marriage_targets
			remove_from_list = potential_marriage_targets
		}
		# first add any lovers/soulmates that might get a proposal
		every_relation = {
			type = soulmate
			limit = {
				is_appropriate_ai_proposal_target = { PROPOSER = root }
			}
			add_to_temporary_list = potential_marriage_targets
		}
		every_relation = {
			type = lover
			limit = {
				is_appropriate_ai_proposal_target = { PROPOSER = root }
				NOT = { is_in_list = potential_marriage_targets }
			}
			add_to_temporary_list = potential_marriage_targets
		}

		# then look for additional targets, if we're going beyond lovers
		if = {
			limit = {
				will_make_nonlover_proposal = { CHARACTER = root }
			}
			liege = {
				# first look within liege's court (maximum 10 targets)
				every_courtier_or_guest = {
					limit = {
						list_size:potential_marriage_targets < 10
						NOT = { is_in_list = potential_marriage_targets }
						is_appropriate_ai_proposal_target = { PROPOSER = root }
					}
					add_to_temporary_list = potential_marriage_targets
				}
				# and pick two random vassals within the same de jure kingdom ( maximum 20 targets)
				while = {
					count = 2
					random_vassal = {
						limit = { capital_province.kingdom ?= root.liege.capital_province.kingdom }
						every_courtier_or_guest = {
							limit = {
								list_size:potential_marriage_targets < 30
								NOT = { is_in_list = potential_marriage_targets }
								is_appropriate_ai_proposal_target = { PROPOSER = root }
							}
							add_to_temporary_list = potential_marriage_targets
						}
					}
				}
			}
			# for player realms, grab a few more candidates from the liege's neighboring realms
			if = {
				limit = { exists = scope:is_player_realm }
				liege = {
					every_neighboring_realm_same_rank_owner = {
						limit = { list_size:potential_marriage_targets < 50 }
						if = {
							limit = {
								capital_province ?= {
									squared_distance = { target = root.liege.capital_province value < 300000 }
								}
							}
							random_courtier_or_guest = {
								limit = {
									NOT = { is_in_list = potential_marriage_targets }
									is_appropriate_ai_proposal_target = { PROPOSER = root }
								}
								add_to_temporary_list = potential_marriage_targets
							}
						}
						random_vassal = {
							limit = {
								capital_province ?= {
									squared_distance = { target = root.liege.capital_province value < 300000 }
								}
							}
							random_courtier_or_guest = {
								limit = {
									NOT = { is_in_list = potential_marriage_targets }
									is_appropriate_ai_proposal_target = { PROPOSER = root }
								}
								add_to_temporary_list = potential_marriage_targets
							}
						}
					}
				}
			}
			# otherwise, just grab one at random, for a bit of cross-pollination
			else = {
				liege = {
					random_neighboring_realm_same_rank_owner = {
						if = {
							limit = {
								capital_province ?= {
									squared_distance = { target = root.liege.capital_province value < 300000 }
								}
							}
							random_courtier_or_guest = {
								limit = {
									NOT = { is_in_list = potential_marriage_targets }
									is_appropriate_ai_proposal_target = { PROPOSER = root }
								}
								add_to_temporary_list = potential_marriage_targets
							}
						}
						random_vassal = {
							limit = {
								capital_province ?= {
									squared_distance = { target = root.liege.capital_province value < 300000 }
								}
							}
							random_courtier_or_guest = {
								limit = {
									NOT = { is_in_list = potential_marriage_targets }
									is_appropriate_ai_proposal_target = { PROPOSER = root }
								}
								add_to_temporary_list = potential_marriage_targets
							}
						}
					}
				}
			}
			# now, if we still need additional candidates, add some from the pool provinces
			if = {
				limit = { list_size:potential_marriage_targets < 50 }
				every_in_list = {
					list = pool_provinces
					save_temporary_scope_as = target_province
					every_pool_character = {
						province = scope:target_province
						limit = {
							list_size:potential_marriage_targets < 50
							NOT = { is_in_list = potential_marriage_targets }
							is_appropriate_ai_proposal_target = { PROPOSER = root }
						}
						add_to_temporary_list = potential_marriage_targets
					}
				}
			}
		}
		
		# now see if any of those would accept a proposal
		if = {
			limit = { list_size:potential_marriage_targets > 0 }
			
			root.liege = { save_scope_as = actor }
			root = { save_scope_as = secondary_actor }
			save_scope_value_as = { name = hook value = no }
			save_scope_value_as = { name = grand_wedding_promise value = no }
			save_scope_value_as = { name = influence_send_option value = no }
			save_scope_value_as = { name = herd_send_option value = no }
			
			every_in_list = {
				list = potential_marriage_targets
				limit = {
					save_temporary_scope_as = potential_marriage_target
					# they meet the minimum threshold of finding each other acceptable suitors
					is_pleased_by_suitor_trigger = { CHARACTER = scope:potential_marriage_target SUITOR = root THRESHOLD = -10 }
					is_pleased_by_suitor_trigger = { CHARACTER = root SUITOR = scope:potential_marriage_target THRESHOLD = -10 }
				}
				
				# now save the proper scopes to evaluate proposal acceptance
				save_temporary_scope_as = secondary_recipient
				if = {
					limit = { exists = matchmaker }
					matchmaker = { save_temporary_scope_as = recipient }
				}
				else = { save_temporary_scope_as = recipient }
				if = {
					limit = { should_marry_matrilineally_trigger = { PROPOSER = scope:secondary_actor TARGET = scope:secondary_recipient } }
					save_temporary_scope_value_as = { name = matrilineal value = yes }
				}
				else = {
					save_temporary_scope_value_as = { name = matrilineal value = no }
				}
				
				# check acceptance
				random = {
					chance = 0
					marriage_ai_accept_modifier = yes
					evaluate_action_increasing_house_unity = { VALUE = 100 }
					# does the recipient like the secondary recipient? This is also thier request
					opinion_modifier = {
						trigger = {
							NOT = { scope:recipient = scope:secondary_recipient }
						}
						who = scope:recipient
						opinion_target = scope:secondary_recipient
						multiplier = 0.25
					}
					# remove the malus if it's the recipient's own marriage, as it's their request also
					modifier = {
						add = 15
						scope:recipient = scope:secondary_recipient
					}
					# better chances if not related to rulers
					modifier = {
						add = 20
						NOR = {
							scope:secondary_actor = {
								is_close_or_extended_family_of = scope:actor
							}
							scope:recipient = {
								is_playable_character = yes
								is_close_or_extended_family_of = scope:secondary_recipient
							}
						}
					}
					modifier = { factor = 100 }
					min = 0
					max = 100
					
					# since there will be acceptance, record the interest value on the candidate
					save_temporary_scope_as = candidate
					scope:secondary_actor = {
						save_temporary_opinion_value_as = { name = match_opinion target = scope:candidate }
						save_temporary_scope_as = match
					}
					set_variable = {
						name = interest_value
						value = introduction_candidate_interest_value
						days = 1
					}
					
					# and then add them to the final list
					add_to_temporary_list = marriage_targets
				}
			}
			
			# is there anyone in the final list? If so, find the one the proposer is most interested in
			if = {
				limit = { list_size:marriage_targets > 0 }
				root = { save_scope_as = owner }
				ordered_in_list = {
					# first priority: lovers
					limit = { has_relation_lover = root }
					# second priority: those at their location, so long as the interest value is +
					alternative_limit = {
						is_at_same_location = root
						var:interest_value > 0
					}
					# third priority: those with high interest value
					alternative_limit = { var:interest_value >= 20 }
					# lastly: anyone
					alternative_limit = { always = yes }
					list = marriage_targets
					order_by = var:interest_value
					save_scope_as = target
				}
				
				#first add some opinion if they're not already lovers
				if = {
					limit = { NOT = { has_relation_lover = scope:target } }
					if = {
						limit = {
							scope:target = { is_attracted_to_gender_of = scope:owner }
							scope:owner = { is_attracted_to_gender_of = scope:target }
						}
						add_marry_for_love_flag_effect = { PARTNER1 = scope:target PARTNER2 = scope:owner }
						add_opinion = { target = scope:target modifier = love_opinion opinion = 20 }
						reverse_add_opinion = { target = scope:target modifier = love_opinion opinion = 20 }
					}
					save_scope_value_as = { name = is_non_lover_proposal value = flag:yes }
				}
				
				#then see if we need to ask permission first
				if = {
					limit = {
						needs_permission_to_marry_trigger = { CHARACTER1 = scope:owner CHARACTER2 = scope:target }
					}
					find_proposer_and_lieges_effect = yes
					if = {
						limit = { exists = scope:proposer_liege }
						scope:proposer_liege = { trigger_event = lmf_scheme.1031 }
					}
					else = {
						scope:proposer_target_liege = { trigger_event = lmf_scheme.1032 }
					}
				}
				#otherwise just marry them
				else = {
					find_proposer_and_lieges_effect = yes
					scope:proposer = {
						if = {
							limit = { should_marry_matrilineally_trigger = { PROPOSER = scope:proposer TARGET = scope:proposer_target } }
							marry_matrilineal = scope:proposer_target
						}
						else = {
							marry = scope:proposer_target
						}
					}
				}
			}
			# otherwise, courtiers of player lieges will check again
			else_if = {
				limit = {
					liege = {
						NOT = { has_character_flag = check_proposals_second_time }
						is_ai = no
					}
				}
				liege = {
					add_character_flag = { flag = check_proposals_second_time days = 10 }
					random_courtier = {
						limit = { is_appropriate_ai_proposal_courtier = yes }
						weight = {
							base = 5
							ai_value_modifier = {
								ai_boldness = 0.25
								ai_honor = 0.25
								min = -25
								max = 25
							}
							modifier = {
								add = 10
								age >= 25
								is_female = no
							}
							modifier = {
								add = 10
								age >= 30
								is_female = yes
							}
							modifier = {
								add = 25
								is_of_major_or_minor_interest_trigger = { CHARACTER = liege }
							}
							modifier = {
								add = 10
								has_trait = diligent
							}
							modifier = {
								add = -25
								any_secret = { type = secret_lover }
								OR = {
									has_trait = paranoid
									has_trait = deceitful
								}
							}
							modifier = {
								factor = 0.5
								has_sexuality = homosexual
							}
							modifier = {
								factor = 0.5
								has_trait = lazy
							}
							modifier = {
								factor = 0.5
								is_female = yes
								OR = {
									faith = { has_doctrine = doctrine_gender_male_dominated }
									has_trait = maiden
									has_personality_submissive_trigger = yes
								}
							}
							modifier = {
								factor = 0.5
								is_male = yes
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							modifier = {
								add = 100
								num_of_relation_lover > 0
								save_temporary_scope_as = lover_check
								OR = {
									relation_proposal_check = { TYPE = lover CHARACTER = scope:lover_check }
									relation_proposal_check = { TYPE = soulmate CHARACTER = scope:lover_check }
								}
							}
							modifier = {
								add = 100
								is_pregnant = yes
							}
							modifier = {
								add = 50
								has_trait = will_not_wed
								any_owned_story = { type = lmf_will_not_wed_story_cycle }
							}
							min = 1
						}
						trigger_event = lmf_scheme.1030
					}
				}
			}
		}
		# otherwise, courtiers of player lieges will check again
		else_if = {
			limit = {
				liege = {
					NOT = { has_character_flag = check_proposals_second_time }
					is_ai = no
				}
			}
			liege = {
				add_character_flag = { flag = check_proposals_second_time days = 10 }
				random_courtier = {
					limit = { is_appropriate_ai_proposal_courtier = yes }
					weight = {
						base = 5
						ai_value_modifier = {
							ai_boldness = 0.25
							ai_honor = 0.25
							min = -25
							max = 25
						}
						modifier = {
							add = 10
							age >= 25
							is_female = no
						}
						modifier = {
							add = 10
							age >= 30
							is_female = yes
						}
						modifier = {
							add = 25
							is_of_major_or_minor_interest_trigger = { CHARACTER = liege }
						}
						modifier = {
							add = 10
							has_trait = diligent
						}
						modifier = {
							add = -25
							any_secret = { type = secret_lover }
							OR = {
								has_trait = paranoid
								has_trait = deceitful
							}
						}
						modifier = {
							factor = 0.5
							has_sexuality = homosexual
						}
						modifier = {
							factor = 0.5
							has_trait = lazy
						}
						modifier = {
							factor = 0.5
							is_female = yes
							OR = {
								faith = { has_doctrine = doctrine_gender_male_dominated }
								has_trait = maiden
								has_personality_submissive_trigger = yes
							}
						}
						modifier = {
							factor = 0.5
							is_male = yes
							faith = { has_doctrine = doctrine_gender_female_dominated }
						}
						modifier = {
							add = 100
							num_of_relation_lover > 0
							save_temporary_scope_as = lover_check
							OR = {
								relation_proposal_check = { TYPE = lover CHARACTER = scope:lover_check }
								relation_proposal_check = { TYPE = soulmate CHARACTER = scope:lover_check }
							}
						}
						modifier = {
							add = 100
							is_pregnant = yes
						}
						modifier = {
							add = 50
							has_trait = will_not_wed
							any_owned_story = { type = lmf_will_not_wed_story_cycle }
						}
						min = 1
					}
					trigger_event = lmf_scheme.1030
				}
			}
		}
	}
}

# Letter from proposer to own liege asking for permission to marry target
lmf_scheme.1031 = {
	type = letter_event
	opening = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:proposer = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.family.opening
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { is_courtier_of = root }
				}
				desc = lmf_scheme.1031.courtier.opening
			}
			desc = lmf_scheme.1031.opening
		}
	}
	desc = {
		desc = lmf_scheme.1031.start
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:proposer_target = {
						has_any_court_relationship_with = { CHARACTER = root }
						is_courtier = yes
						OR = {
							NOT = { is_courtier_of = ROOT }
							AND = {
								scope:proposer = { NOT = { is_courtier_of = root } }
								NOR = {
									is_knight_of = root
									is_councillor_of = root
									is_hostage_of = root
									AND = {
										has_any_court_position = yes
										is_courtier_of = root
									}
								}
							}
						}
					}
				}
				desc = lmf_scheme.1031.my.relation.notcourtier
			}
			triggered_desc = {
				trigger = {
					scope:proposer_target = {
						has_any_court_relationship_with = { CHARACTER = root }
					}
				}
				desc = lmf_scheme.1031.my.relation
			}
			triggered_desc = {
				trigger = {
					exists = scope:proposer_target_liege
					scope:proposer_target = {
						NOT = { is_courtier_of = root }
						has_any_court_relationship_with = { CHARACTER = scope:proposer_target_liege }
					}
				}
				desc = lmf_scheme.1031.other.relation
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { is_courtier_of = root }
					scope:proposer_target = { is_courtier_of = root }
				}
				desc = lmf_scheme.1031.both.my.courtier
			}
			triggered_desc = {
				trigger = {
					exists = scope:proposer_liege
					scope:proposer_target = { is_courtier_of = scope:proposer_liege }
				}
				desc = lmf_scheme.1031.my.courtier
			}
			triggered_desc = {
				trigger = {
					exists = scope:proposer_target_liege
					scope:proposer_target = { is_courtier_of = scope:proposer_target_liege }
				}
				desc = lmf_scheme.1031.other.courtier
			}
			desc = lmf_scheme.1031.stranger
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:proposer_target = { is_close_or_extended_family_of = root }
					scope:proposer = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.both.family
			}
			triggered_desc = {
				trigger = {
					scope:proposer_target = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.only.target.family
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.only.proposer.family
			}
			triggered_desc = {
				trigger = {
					scope:proposer = {
						is_courtier = no
						is_ruler = no
					}
				}
				desc = lmf_scheme.1031.proposer.wanderer
			}
			triggered_desc = {
				trigger = {
					scope:proposer_target = {
						is_courtier = no
						is_ruler = no
					}
				}
				desc = lmf_scheme.1031.target.wanderer
			}
			triggered_desc = {
				trigger = { proposer_will_leave_your_court = yes }
				desc = lmf_scheme.1031.proposer.leavescourt
			}
			triggered_desc = {
				trigger = { proposer_target_will_leave_your_court = yes }
				desc = lmf_scheme.1031.target.leavescourt
			}
			triggered_desc = {
				trigger = { proposer_will_move_to_court_of_character = { NEW_LIEGE = root } }
				desc = lmf_scheme.1031.proposer.arrivescourt
			}
			triggered_desc = {
				trigger = { proposer_target_will_move_to_court_of_character = { NEW_LIEGE = root } }
				desc = lmf_scheme.1031.target.arrivescourt
			}
			desc = lmf_scheme.1031.neither.family
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:is_pregnancy_proposal
					scope:proposer_target = { is_female = yes }
				}
				desc = lmf_scheme.1031.target.mother
			}
			triggered_desc = {
				trigger = {
					exists = scope:is_pregnancy_proposal
					scope:proposer_target = { is_male = yes }
				}
				desc = lmf_scheme.1031.target.father
			}
			triggered_desc = {
				trigger = {
					proposer_will_move_to_court_of_character = { NEW_LIEGE = root }
				}
				desc = lmf_scheme.1031.proposer.worthy
			}
			triggered_desc = {
				trigger = {
					proposer_target_will_move_to_court_of_character = { NEW_LIEGE = root }
				}
				desc = lmf_scheme.1031.target.worthy
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { has_relation_soulmate = scope:proposer_target }
				}
				desc = lmf_scheme.1031.soulmates
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { has_relation_lover = scope:proposer_target }
				}
				desc = lmf_scheme.1031.lovers
			}
			desc = lmf_scheme.1031.default
		}
		desc = lmf_scheme.1031.request
		triggered_desc = {
			trigger = {
				scope:proposer = {
					has_trait = will_not_wed
					is_close_or_extended_family_of = root
				}
			}
			desc = lmf_scheme.1031.proposer.family.willnotwed
		}
		triggered_desc = {
			trigger = {
				scope:proposer = {
					NOT = { has_trait = will_not_wed }
					is_close_or_extended_family_of = root
				}
			}
			desc = lmf_scheme.1031.proposer.family
		}
		triggered_desc = {
			trigger = {
				scope:proposer = {
					NOT = { is_close_or_extended_family_of = root }
					has_dynasty = yes
					dynasty = root.dynasty
				}
			}
			desc = lmf_scheme.1031.proposer.dynasty
		}
		triggered_desc = {
			trigger = {
				scope:proposer = {
					is_courtier_of = root
					NOR = {
						is_close_or_extended_family_of = root
						dynasty ?= root.dynasty
					}
				}
			}
			desc = lmf_scheme.1031.proposer.courtier
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:proposer_target_liege
					NOT = { scope:proposer_target_liege = root }
				}
				desc = lmf_scheme.1031.mustask
			}
			desc = lmf_scheme.1031.marry
		}
	}
	sender = scope:proposer

	trigger = {
		trigger_if = {
			limit = { has_character_flag = auto_approve_unimportant_marriages }
			OR = {
				proposer_is_considered_important_marriage = yes
				proposer_target_is_considered_important_marriage = yes
			}
		}
		trigger_else = {
			always = yes
		}
	}

	immediate = {
		hidden_effect = {
			if = {
				limit = { is_ai = no }
				add_character_flag = {
					flag = player_received_recent_proposal
					months = 6
				}
			}
			if = {
				limit = {
					proposer_will_move_to_court_of_character = { NEW_LIEGE = root }
				}
				scope:proposer = {
					set_variable = {
						name = is_extoll_character
						value = flag:yes
					}
				}
			}
			scope:proposer = {
				set_variable = {
					name = potential_spouse
					value = scope:proposer_target
				}
			}
			scope:proposer_target = {
				set_variable = {
					name = potential_spouse
					value = scope:proposer
				}
			}
		}
	}

	on_trigger_fail = {
		hidden_effect = {
			marriage_permission_given_effect = yes
			if = {
				limit = { only_liege_to_ask_permission_trigger = yes }
				send_interface_message = {
					type = event_generic_neutral_with_text
					title =  lmf_scheme.1031.auto.permission.title.tt
					desc =  lmf_scheme.1031.auto.permission.tt
					right_icon = scope:proposer
				}
				courtiers_marry_effect = yes
			}
			else = {
				send_interface_message = {
					type = event_generic_neutral_with_text
					title =  lmf_scheme.1031.auto.permission.title.tt
					desc =  lmf_scheme.1031.auto.permission.maybe.tt
					right_icon = scope:proposer
				}
				scope:proposer_target_liege = {
					trigger_event = lmf_scheme.1032
				}
			}
		}
	}

	option = {
		name = lmf_scheme.1031.a #Give them leave
		if = {
			limit = {
				scope:proposer = { has_usable_hook = root }
			}
			scope:proposer = { use_hook = root }
		}
		else_if = {
			limit = {
				scope:proposer_target = { has_usable_hook = root }
			}
			scope:proposer_target = { use_hook = root }
		}
		marriage_permission_given_effect = yes
		if = {
			limit = { only_liege_to_ask_permission_trigger = yes }
			courtiers_marry_effect = yes
		}
		else = {
			custom_tooltip = lmf_scheme.1031.a.seek.permission.tt
			show_as_tooltip = {
				random_list = {
					1 = {
						desc = lmf_scheme.1031.a.permission.granted.tt
						show_chance = no
						courtiers_marry_effect = yes
					}
					1 = {
						desc = lmf_scheme.1031.a.permission.refused.tt
						show_chance = no
						marriage_refused_fornicator_effect = yes
						custom_tooltip = lmf_scheme.1031.b.tt
					}
				}
			}
			scope:proposer_target_liege = {
				trigger_event = lmf_scheme.1032
			}
		}
	}
	option = {
		trigger = {
			is_ai = no
			NOR = {
				scope:proposer = { has_usable_hook = root }
				scope:proposer_target = { has_usable_hook = root }
			}
		}
		show_as_unavailable = {
			is_ai = no
			OR = {
				scope:proposer = { has_usable_hook = root }
				scope:proposer_target = { has_usable_hook = root }
			}
		}
		name = lmf_scheme.1031.b #Refuse
		custom_tooltip = lmf_scheme.1031.b.tt
		marriage_permission_refused_effect = yes
		scope:proposer = {
			trigger_event = court_outcome.2004
		}
	}
	option = {
		name = lmf_scheme.1031.c #Ignore unimportant requests
		trigger = {
			is_ai = no
			NOR = {
				scope:proposer = { has_any_court_relationship_with = { CHARACTER = root } }
				scope:proposer_target = { has_any_court_relationship_with = { CHARACTER = root } }
			}
		}
		custom_tooltip = lmf_scheme.1031.c.tt
		add_character_flag = auto_approve_unimportant_marriages
		marriage_permission_given_effect = yes
		if = {
			limit = { only_liege_to_ask_permission_trigger = yes }
			courtiers_marry_effect = yes
		}
		else = {
			custom_tooltip = lmf_scheme.1031.a.seek.permission.tt
			show_as_tooltip = {
				random_list = {
					1 = {
						desc = lmf_scheme.1031.a.permission.granted.tt
						show_chance = no
						courtiers_marry_effect = yes
					}
					1 = {
						desc = lmf_scheme.1031.a.permission.refused.tt
						show_chance = no
						custom_tooltip = lmf_scheme.1031.b.tt
					}
				}
			}
			scope:proposer_target_liege = {
				trigger_event = lmf_scheme.1032
			}
		}
		ai_chance = {
			base = 0
		}
	}

	after = {
		scope:proposer = {
			remove_variable = is_extoll_character
			remove_variable = potential_spouse
		}
		scope:proposer_target = {
			remove_variable = potential_spouse
		}
	}
}

# Letter from proposer to target's liege asking for permission to marry target
lmf_scheme.1032 = {
	type = letter_event
	opening = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:proposer = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.family.opening
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { is_courtier_of = root }
				}
				desc = lmf_scheme.1031.courtier.opening
			}
			desc = lmf_scheme.1031.opening
		}
	}
	desc = {
		desc = lmf_scheme.1031.start
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:proposer_target = {
						has_any_court_relationship_with = { CHARACTER = root }
						is_courtier = yes
						OR = {
							NOT = { is_courtier_of = ROOT }
							AND = {
								scope:proposer = { NOT = { is_courtier_of = root } }
								NOR = {
									is_knight_of = root
									is_councillor_of = root
									is_hostage_of = root
									AND = {
										has_any_court_position = yes
										is_courtier_of = root
									}
								}
							}
						}
					}
				}
				desc = lmf_scheme.1031.my.relation.notcourtier
			}
			triggered_desc = {
				trigger = {
					scope:proposer_target = {
						has_any_court_relationship_with = { CHARACTER = root }
					}
				}
				desc = lmf_scheme.1031.my.relation
			}
			triggered_desc = {
				trigger = {
					exists = scope:proposer_target_liege
					scope:proposer_target = {
						NOT = { is_courtier_of = root }
						has_any_court_relationship_with = { CHARACTER = scope:proposer_target_liege }
					}
				}
				desc = lmf_scheme.1031.other.relation
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { is_courtier_of = root }
					scope:proposer_target = { is_courtier_of = root }
				}
				desc = lmf_scheme.1031.both.my.courtier
			}
			triggered_desc = {
				trigger = {
					exists = scope:proposer_liege
					scope:proposer_target = { is_courtier_of = scope:proposer_liege }
				}
				desc = lmf_scheme.1031.my.courtier
			}
			triggered_desc = {
				trigger = {
					exists = scope:proposer_target_liege
					scope:proposer_target = { is_courtier_of = scope:proposer_target_liege }
				}
				desc = lmf_scheme.1031.other.courtier
			}
			desc = lmf_scheme.1031.stranger
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:proposer_target = { is_close_or_extended_family_of = root }
					scope:proposer = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.both.family
			}
			triggered_desc = {
				trigger = {
					scope:proposer_target = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.only.target.family
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1031.only.proposer.family
			}
			triggered_desc = {
				trigger = {
					scope:proposer = {
						is_courtier = no
						is_ruler = no
					}
				}
				desc = lmf_scheme.1031.proposer.wanderer
			}
			triggered_desc = {
				trigger = {
					scope:proposer_target = {
						is_courtier = no
						is_ruler = no
					}
				}
				desc = lmf_scheme.1031.target.wanderer
			}
			triggered_desc = {
				trigger = { proposer_will_leave_your_court = yes }
				desc = lmf_scheme.1031.proposer.leavescourt
			}
			triggered_desc = {
				trigger = { proposer_target_will_leave_your_court = yes }
				desc = lmf_scheme.1031.target.leavescourt
			}
			triggered_desc = {
				trigger = { proposer_will_move_to_court_of_character = { NEW_LIEGE = root } }
				desc = lmf_scheme.1031.proposer.arrivescourt
			}
			triggered_desc = {
				trigger = { proposer_target_will_move_to_court_of_character = { NEW_LIEGE = root } }
				desc = lmf_scheme.1031.target.arrivescourt
			}
			desc = lmf_scheme.1031.neither.family
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:is_pregnancy_proposal
					scope:proposer_target = { is_female = yes }
				}
				desc = lmf_scheme.1031.target.mother
			}
			triggered_desc = {
				trigger = {
					exists = scope:is_pregnancy_proposal
					scope:proposer_target = { is_male = yes }
				}
				desc = lmf_scheme.1031.target.father
			}
			triggered_desc = {
				trigger = {
					proposer_will_move_to_court_of_character = { NEW_LIEGE = root }
				}
				desc = lmf_scheme.1031.proposer.worthy
			}
			triggered_desc = {
				trigger = {
					proposer_target_will_move_to_court_of_character = { NEW_LIEGE = root }
				}
				desc = lmf_scheme.1031.target.worthy
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { has_relation_soulmate = scope:proposer_target }
				}
				desc = lmf_scheme.1031.soulmates
			}
			triggered_desc = {
				trigger = {
					scope:proposer = { has_relation_lover = scope:proposer_target }
				}
				desc = lmf_scheme.1031.lovers
			}
			desc = lmf_scheme.1031.default
		}
		desc = lmf_scheme.1031.request
		triggered_desc = {
			trigger = {
				scope:proposer = { is_close_or_extended_family_of = root }
			}
			desc = lmf_scheme.1031.proposer.family
		}
		triggered_desc = {
			trigger = {
				scope:proposer = {
					NOT = { is_close_or_extended_family_of = root }
					has_dynasty = yes
					dynasty = root.dynasty
				}
			}
			desc = lmf_scheme.1031.proposer.dynasty
		}
		triggered_desc = {
			trigger = {
				scope:proposer = {
					is_courtier_of = root
					NOR = {
						is_close_or_extended_family_of = root
						dynasty ?= root.dynasty
					}
				}
			}
			desc = lmf_scheme.1031.proposer.courtier
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:proposer_liege
					NOT = { scope:proposer_target_liege = scope:proposer_liege }
				}
				desc = lmf_scheme.1031.alreadyask
			}
			desc = lmf_scheme.1031.marry
		}
	}
	sender = scope:proposer

	trigger = {
		trigger_if = {
			limit = { has_character_flag = auto_approve_unimportant_marriages }
			OR = {
				proposer_is_considered_important_marriage = yes
				proposer_target_is_considered_important_marriage = yes
			}
		}
		trigger_else = {
			always = yes
		}
	}

	immediate = {
		hidden_effect = {
			if = {
				limit = { is_ai = no }
				add_character_flag = {
					flag = player_received_recent_proposal
					months = 6
				}
			}
			if = {
				limit = {
					proposer_will_move_to_court_of_character = { NEW_LIEGE = root }
				}
				scope:proposer = {
					set_variable = {
						name = is_extoll_character
						value = scope:proposer
					}
				}
			}
			scope:proposer = {
				set_variable = {
					name = potential_spouse
					value = scope:proposer_target
				}
			}
			scope:proposer_target = {
				set_variable = {
					name = potential_spouse
					value = scope:proposer
				}
			}
		}
	}

	on_trigger_fail = {
		hidden_effect = {
			marriage_permission_given_effect = yes
			send_interface_message = {
				type = event_generic_neutral_with_text
				title =  lmf_scheme.1031.auto.permission.title.tt
				desc =  lmf_scheme.1031.auto.permission.tt
				right_icon = scope:proposer
			}
			courtiers_marry_effect = yes
		}
	}

	option = {
		name = lmf_scheme.1031.a #Give them leave
		if = {
			limit = {
				scope:proposer = { has_usable_hook = root }
			}
			scope:proposer = { use_hook = root }
		}
		else_if = {
			limit = {
				scope:proposer_target = { has_usable_hook = root }
			}
			scope:proposer_target = { use_hook = root }
		}
		marriage_permission_given_effect = yes
		courtiers_marry_effect = yes
	}
	option = {
		name = lmf_scheme.1031.b #Refuse
		trigger = {
			is_ai = no
			NOR = {
				scope:proposer = { has_usable_hook = root }
				scope:proposer_target = { has_usable_hook = root }
			}
		}
		show_as_unavailable = {
			is_ai = no
			OR = {
				scope:proposer = { has_usable_hook = root }
				scope:proposer_target = { has_usable_hook = root }
			}
		}
		custom_tooltip = lmf_scheme.1031.b.tt
		marriage_permission_refused_effect = yes
		scope:proposer = {
			trigger_event = court_outcome.2004
		}
	}
	option = {
		name = lmf_scheme.1031.c #Ignore unimportant requests
		trigger = {
			is_ai = no
			NOR = {
				scope:proposer = { has_any_court_relationship_with = { CHARACTER = root } }
				scope:proposer_target = { has_any_court_relationship_with = { CHARACTER = root } }
			}
		}
		custom_tooltip = lmf_scheme.1031.c.tt
		add_character_flag = auto_approve_unimportant_marriages
		marriage_permission_given_effect = yes
		courtiers_marry_effect = yes
		ai_chance = {
			base = 0
		}
	}

	after = {
		scope:proposer = {
			remove_variable = is_extoll_character
			remove_variable = potential_spouse
		}
		scope:proposer_target = {
			remove_variable = potential_spouse
		}
	}
}

# Courtier asks to bring distant spouse and children to court
lmf_scheme.1040 = {
	type = character_event
	hidden = yes
	
	trigger = {
		# Check triggers again, just in case they've changed in the interim
		is_alive = yes
		is_courtier = yes
		is_appropriate_ai_reunite_courtier = yes
	}
	
	on_trigger_fail = {
		if = {
			limit = {
				is_alive = yes
				is_courtier = yes
			}
			liege = {
				# Since they did change, let's check if there's another courtier applicable
				random_courtier = {
					limit = { is_appropriate_ai_reunite_courtier = yes }
					weight = {
						base = 5
						ai_value_modifier = {
							ai_boldness = 0.25
							ai_honor = 0.25
							min = -25
							max = 25
						}
						modifier = {
							add = 25
							is_of_major_or_minor_interest_trigger = { CHARACTER = root.liege }
						}
						min = 1
					}
					save_scope_as = requester
					trigger_event = lmf_scheme.1040
				}
			}
		}
	}
	
	immediate = {
		add_character_flag = { flag = made_recent_reunite_check years = 5 }
		
		# first check if this is for a spouse or children
		if = {
			limit = {
				has_spouse_to_reunite = { REUNITOR = root }
			}
			primary_spouse = { save_scope_as = requester_spouse }
			# see if there's one child who we can reference by name
			random_child = {
				limit = {
					is_adult = no
					is_child_of = root.primary_spouse
					trigger_if = {
						limit = { root.primary_spouse = { is_courtier = yes } }
						is_courtier_of = root.primary_spouse.liege
					}
					trigger_else = {
						is_at_same_location = root.primary_spouse
					}
				}
				save_scope_as = requester_child
			}
			# either way, make sure we grab a list
			every_child = {
				limit = {
					is_adult = no
					is_child_of = root.primary_spouse
					trigger_if = {
						limit = { root.primary_spouse = { is_courtier = yes } }
						is_courtier_of = root.primary_spouse.liege
					}
					trigger_else = {
						is_at_same_location = root.primary_spouse
					}
				}
				add_to_list = reunite_children
			}
		}
		else = {
			random_child = {
				limit = {
					NOT = { is_in_list = reunite_children }
					is_child_to_reunite = { REUNITOR = root }
				}
				save_scope_as = requester_child2
			}
		}
		# now grab a list of every child elsewhere, for either case
		every_child = {
			limit = {
				NOT = { is_in_list = reunite_children }
				is_child_to_reunite = { REUNITOR = root }
			}
			add_to_list = reunite_children
		}
		# send the request
		liege = { trigger_event = lmf_scheme.1041 }
	}
}

lmf_scheme.1041 = {
	type = letter_event
	opening = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:requester = { is_close_or_extended_family_of = root }
				}
				desc = lmf_scheme.1040.family.opening
			}
			desc = lmf_scheme.1040.courtier.opening
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:requester_spouse }
				desc = {
					desc = lmf_scheme.1040.start
					first_valid = {
						triggered_desc = {
							trigger = {
								scope:requester_spouse = { is_courtier = yes }
							}
							desc = lmf_scheme.1040.courtier
						}
						desc = lmf_scheme.1040.pool
					}
					first_valid = {
						triggered_desc = {
							trigger = { list_size = { name = reunite_children value > 1 } }
							desc = lmf_scheme.1040.children
						}
						triggered_desc = {
							trigger = { exists = scope:requester_child }
							desc = lmf_scheme.1040.child
						}
						desc = lmf_scheme.1040.alone
					}
				}
			}
			desc = {
				first_valid = {
					triggered_desc = {
						trigger = { list_size = { name = reunite_children value > 2 } }
						desc = lmf_scheme.1040.children.several
					}
					triggered_desc = {
						trigger = { list_size = { name = reunite_children value > 1 } }
						desc = lmf_scheme.1040.children.two
					}
					desc = {
						desc = lmf_scheme.1040.child.specific
						first_valid = {
							triggered_desc = {
								trigger = {
									scope:requester_child2 = { is_courtier = yes }
								}
								desc = lmf_scheme.1040.child.specific.courtier
							}
							desc = lmf_scheme.1040.child.specific.pool
						}
					}
				}
			}
		}
		desc = lmf_scheme.1040.mid
		first_valid = {
			triggered_desc = {
				trigger = { scope:requester = { is_close_or_extended_family_of = root } }
				desc = lmf_scheme.1040.family
			}
			triggered_desc = {
				trigger = { scope:requester.dynasty ?= root.dynasty }
				desc = lmf_scheme.1040.dynasty
			}
			desc = lmf_scheme.1040.normal
		}
		desc = lmf_scheme.1040.end
	}
	sender = scope:requester
	
	immediate = {
		hidden_effect = {
			if = {
				limit = { is_ai = no }
				add_character_flag = {
					flag = player_received_recent_proposal
					months = 6
				}
			}
		}
	}
	
	option = {
		name = lmf_scheme.1040.a #Yes, they may come
		if = {
			limit = {
				scope:requester = { has_usable_hook = root }
			}
			scope:requester = { use_hook = root }
		}
		reverse_add_opinion = { modifier = granted_reunion_permission_opinion target = scope:requester }
		if = {
			limit = { exists = scope:requester_spouse }
			if = {
				limit = { exists = scope:requester_spouse.liege }
				scope:requester_spouse.liege = {
					add_to_list = already_contacted
					hidden_effect = {
						send_interface_toast = {
							type = send_interface_message
							title = lmf_scheme.1040.title
							right_icon = scope:requester
							left_icon = scope:requester_spouse
							custom_tooltip = lmf_scheme.1040.spouse.tt
						}
					}
				}
			}
			reverse_add_opinion = { modifier = granted_reunion_permission_opinion target = scope:requester_spouse }
			add_courtier = scope:requester_spouse
		}
		every_in_list = {
			list = reunite_children
			limit = {
				trigger_if = {
					limit = { exists = liege }
					NOT = { liege = root }
				}
				trigger_else = {
					always = yes
				}
			}
			save_temporary_scope_as = leaving_child
			if = {
				limit = {
					liege ?= { NOT = { is_in_list = already_contacted } }
				}
				liege = {
					add_to_list = already_contacted
					hidden_effect = {
						send_interface_toast = {
							type = send_interface_message
							title = lmf_scheme.1040.title
							right_icon = scope:requester
							left_icon = scope:leaving_child
							custom_tooltip = lmf_scheme.1040.child.tt
						}
					}
				}
			}
			every_relation = {
				type = guardian
				limit = {
					NOR = {
						this = scope:requester
						this = scope:requester.liege
						is_courtier_of = scope:requester.liege
						AND = { 
							exists = scope:requester_spouse
							this = scope:requester_spouse
						}
					}
				}
				hidden_effect = { remove_relation_ward = scope:leaving_child }
			}
			root = { add_courtier = scope:leaving_child }
		}
	}
	option = {
		name = lmf_scheme.1040.b #My court is not your home!
		trigger = {
			is_ai = no
			NOT = { scope:requester = { has_usable_hook = root } }
		}
		show_as_unavailable = {
			is_ai = no
			scope:requester = { has_usable_hook = root }
		}
		reverse_add_opinion = { modifier = refused_reunion_permission_opinion target = scope:requester }
		if = {
			limit = { exists = scope:requester_spouse }
			reverse_add_opinion = { modifier = refused_reunion_permission_opinion target = scope:requester_spouse }
		}
	}
}

# Courtier leaves to visit spouse or lover
lmf_scheme.1050 = {
	type = character_event
	hidden = yes
	
	trigger = {
		# check the triggers again, in case they've changed in the interim
		is_alive = yes
		is_courtier = yes
		is_appropriate_ai_visit_courtier = yes
		liege = {
			NOR = {
				is_at_war = yes
				has_character_modifier = isolating_modifier
				capital_county ?= { has_county_modifier = isolate_capital_decision_modifier }
				capital_county ?= { any_county_province = { any_province_epidemic = { count >= 1 } } }
			}
		}
	}
	
	on_trigger_fail = {
		# they did change, so let's see if we can grab another courtier
		if = {
			limit = {
				is_alive = yes
				is_courtier = yes
				liege = {
					NOR = {
						is_at_war = yes
						has_character_modifier = isolating_modifier
						capital_county ?= { has_county_modifier = isolate_capital_decision_modifier }
						capital_county ?= { any_county_province = { any_province_epidemic = { count >= 1 } } }
					}
				}
			}
			liege = {
				random_courtier = {
					limit = { is_appropriate_ai_visit_courtier = yes }
					weight = {
						base = 5
						ai_value_modifier = {
							ai_energy = 0.5
							min = -25
							max = 25
						}
						modifier = {
							add = 25
							is_close_or_extended_family_of = root.liege
						}
						modifier = {
							add = 10
							is_of_major_or_minor_interest_trigger = { CHARACTER = root.liege }
						}
						modifier = {
							factor = 2
							is_married = yes
							any_child = {
								NOT = { has_trait = bastard }
								count < 1
							}
						}
						modifier = {
							factor = 0.5
							is_female = yes
							NOT = { is_close_or_extended_family_of = root.liege }
						}
						modifier = {
							factor = 0.5
							OR = {
								is_councillor_of = root.liege
								has_any_court_position = yes
								is_knight_of = root.liege
							}
						}
						min = 1
					}
					trigger_event = lmf_scheme.1050
				}
			}
		}
	}
	
	immediate = {
		save_scope_as = recipient
		add_character_flag = { flag = made_recent_courtier_visit years = 5 }
		# first let's check if we're visiting a spouse
		if = {
			limit = {
				exists = primary_spouse
				has_spouse_to_visit = { VISITOR = root }
			}
			primary_spouse = {
				save_scope_as = actor
				set_variable = { name = visited_by_target value = root }
				add_opinion = { target = root modifier = agreed_to_visit_opinion }
			}
			# notify the liege of the visitation
			liege = {
				send_interface_toast = {
					type = send_interface_message
					title = lmf_scheme.1050.title
					right_icon = scope:actor
					left_icon = scope:recipient
					custom_tooltip = lmf_scheme.1050.tt
				}
			}
		}
		# otherwise, let's find the lover we're visiting
		else = {
			random_relation = {
				type = lover
				limit = {
					has_lover_to_visit = { VISITOR = root }
				}
				save_scope_as = actor
				set_variable = { name = visited_by_target value = root }
				add_opinion = { target = root modifier = agreed_to_visit_opinion }
				# for secret lovers, inform the liege but keep the reason vague
				if = {
					limit = {
						any_secret = {
							type = secret_lover
							secret_target = root
							NOT = { is_known_by = root.liege }
						}
					}
					root.liege = {
						send_interface_toast = {
							type = send_interface_message
							title = lmf_scheme.1050.title
							left_icon = scope:recipient
							custom_tooltip = lmf_scheme.1050.unknown.tt
						}
					}
				}
				# otherwise, inform them normally
				else = {
					root.liege = {
						send_interface_toast = {
							type = send_interface_message
							title = lmf_scheme.1050.title
							right_icon = scope:actor
							left_icon = scope:recipient
							custom_tooltip = lmf_scheme.1050.lover.tt
						}
					}
				}
			}
		}
		# now send them for a visit
		create_story = {
			type = lmf_visit_invitation_story_cycle
			save_scope_as = story
		}
		scope:story = {
			set_variable = { name = visit_target value = scope:actor }
			if = {
				limit = { exists = scope:actor.host.capital_province }
				set_variable = { name = visit_province value = scope:actor.host.capital_province }
			}
			else = {
				set_variable = { name = visit_province value = scope:actor.location }
			}
			if = {
				limit = { exists = scope:recipient.host.capital_province }
				set_variable = { name = start_province value = scope:recipient.host.capital_province }
			}
			else = {
				set_variable = { name = start_province value = scope:recipient.location }
			}
			set_variable = { name = courtier_visit value = flag:yes }
		}
		start_travel_plan = {
			destination = scope:story.var:visit_province
			on_travel_planner_cancel_event = lmf_interaction.1502
			on_arrival_event = lmf_interaction.1505
			on_arrival_destinations = all_but_last
		}
		# and give them a small safety boost
		current_travel_plan = {
			add_travel_plan_modifier = travel_guide_modifier
		}
	}
}

# Courtier uses boon on their liege
lmf_scheme.1100 = {
	type = character_event
	hidden = yes
	
	immediate = {
		save_scope_as = requester
		random_list = {
			25 = {
				trigger = { courtier_wants_weapon = yes }
				save_scope_value_as = { name = wants_weapon value = yes }
				host = { trigger_event = lmf_scheme.1101 }
			}
			25 = {
				trigger = { courtier_wants_gold = yes }
				save_scope_value_as = { name = wants_gold value = yes }
				host = { trigger_event = lmf_scheme.1101 }
			}
			25 = {
				trigger = { courtier_wants_position = yes }
				save_scope_value_as = { name = wants_position value = yes }
				court_position_generator_effect = { VAR = target EMPLOYER = root.host ONLY_EMPTY = no }
				court_position_generator_effect = { VAR = backup EMPLOYER = root.host ONLY_EMPTY = yes }
				if = {
					limit = { exists = var:target_court_position }
					host = { trigger_event = lmf_scheme.1101 }
				}
			}
			25 = {
				ai_value_modifier = {
					ai_boldness = -0.5
					min = -15
				}
			}
		}
	}
}

lmf_scheme.1101 = {
	type = character_event
	title = lmf_scheme.1101.t
	desc = {
		desc = lmf_scheme.1101.start
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:wants_weapon
					scope:requester = { is_knight_of = root }
				}
				desc = lmf_scheme.1101.weapon_knight
			}
			triggered_desc = {
				trigger = { exists = scope:wants_weapon }
				desc = lmf_scheme.1101.weapon_courtier
			}
			triggered_desc = {
				trigger = {
					exists = scope:wants_gold
					scope:requester = {
						OR = {
							has_council_position = councillor_court_chaplain
							is_clergy = yes
						}
					}
				}
				desc = lmf_scheme.1101.gold_chaplain
			}
			triggered_desc = {
				trigger = { exists = scope:wants_gold }
				desc = lmf_scheme.1101.gold_courtier
			}
			triggered_desc = {
				trigger = { exists = scope:wants_position }
				desc = lmf_scheme.1101.position
			}
			desc = lmf_scheme.1101.position
		}
		desc = lmf_scheme.1101.end
	}
	theme = realm
	
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:requester
		animation = throne_room_bow_1
	}
	
	immediate = {
		if = {
			limit = { exists = scope:wants_weapon }
			if = {
				limit = {
					any_courtier = {
						has_court_position = court_artificer_court_position
						is_available_healthy_ai_adult = yes
					}
				}
				random_courtier = {
					limit = {
						has_court_position = court_artificer_court_position
						is_available_healthy_ai_adult = yes
					}
					save_scope_as = maker
				}
			}
			else = {
				dummy_male = { save_scope_as = maker }
			}
			scope:requester = {
				save_scope_as = underequipped
				set_signature_weapon_effect = yes
			}
		}
	}
	
	option = {
		name = lmf_scheme.1101.a # Have a fine weapon created for him
		trigger = { exists = scope:wants_weapon }
        stress_impact = {
			greedy = medium_stress_impact_gain
			callous = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		remove_short_term_gold = medium_gold_value
		custom_tooltip = court.8010.a.tt
		hidden_effect = {
        	scope:requester = {
	        	if = {
					limit = { var:signature_weapon = flag:axe }
					create_artifact_weapon_effect = {
						OWNER = scope:requester
						CREATOR = scope:maker
						SET_WEAPON_TYPE = flag:artifact_weapon_type_axe
					}
				}
				else_if = {
					limit = { var:signature_weapon = flag:hammer }
					create_artifact_weapon_effect = {
						OWNER = scope:requester
						CREATOR = scope:maker
						SET_WEAPON_TYPE = flag:artifact_weapon_type_hammer
					}
				}
				else_if = {
					limit = { var:signature_weapon = flag:spear }
					create_artifact_weapon_effect = {
						OWNER = scope:requester
						CREATOR = scope:maker
						SET_WEAPON_TYPE = flag:artifact_weapon_type_spear
					}
				}
				else_if = {
					limit = { var:signature_weapon = flag:mace }
					create_artifact_weapon_effect = {
						OWNER = scope:requester
						CREATOR = scope:maker
						SET_WEAPON_TYPE = flag:artifact_weapon_type_mace
					}
				}
				else = { #For swords, but also as fallback - no daggers here
					create_artifact_weapon_effect = {
						OWNER = scope:requester
						CREATOR = scope:maker
						SET_WEAPON_TYPE = flag:artifact_weapon_type_sword
					}
				}
			}
	        if = {
	        	limit = { exists = scope:newly_created_artifact } # To avoid errors
	        	send_interface_toast = {
		        	title = court.8010.a.equipped_tt
		        	left_icon = scope:requester
		        	right_icon = scope:newly_created_artifact
		        	scope:newly_created_artifact = {
		        		set_owner = {
		        			target = scope:requester
							history = {
								location = root.capital_province
								actor = root
								recipient = scope:requester
								type = given
							}
						}
		        	}
		        }
	        }
	    }
	    scope:requester = {
			use_hook = root
	    	add_opinion = {
	    		target = root
	    		modifier = grateful_opinion
	    		opinion = 10
	    	}
	    }	
	}
	option = {
		name = lmf_scheme.1101.b # If coin is all he wants, so be it
		trigger = { exists = scope:wants_gold }
        stress_impact = {
			greedy = medium_stress_impact_gain
			callous = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
			fickle = minor_stress_impact_gain
		}
		pay_short_term_gold = {
			target = scope:requester
			gold = medium_gold_value
		}
	    scope:requester = {
			use_hook = root
	    	add_opinion = {
	    		target = root
	    		modifier = grateful_opinion
	    		opinion = 10
	    	}
	    }
	}
	option = {
		name = lmf_scheme.1101.c # Very well, he will have the position he desires
		trigger = {
			exists = scope:wants_position
			exists = scope:requester.var:target_court_position
		}
		court_position_generator_assignment_effect = { VAR = target CANDIDATE = scope:requester  EMPLOYER = root }
		scope:requester = {
			use_hook = root
	    	add_opinion = {
	    		target = root
	    		modifier = grateful_opinion
	    		opinion = 20
	    	}
	    }
	}
	option = {
		name = lmf_scheme.1101.d # How about I offer him something else?
		trigger = {
			exists = scope:wants_position
			exists = scope:requester.var:backup_court_position
		}
		court_position_generator_assignment_effect = { VAR = backup CANDIDATE = scope:requester EMPLOYER = root }
		scope:requester = { use_hook = root }
	}
	option = {
		name = lmf_scheme.1101.e # Wave him away. He is in no position to demand anything.
		trigger = { is_ai = no }
        stress_impact = {
			generous = medium_stress_impact_gain
			compassionate = minor_stress_impact_gain
			honest = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
			diligent = medium_stress_impact_gain
		}
		scope:requester = {
			remove_hook = { target = root }
	    	add_opinion = {
	    		target = root
	    		modifier = disappointed_opinion
	    		opinion = -20
	    	}
		}
		add_prestige = major_prestige_loss
	}
}

