﻿lmf_will_not_wed_story_cycle = {

	on_setup = {
	}

	on_owner_death = {
		end_story = yes
	}

	on_end = {
		story_owner = {
			if = {
				limit = { has_trait = will_not_wed }
				remove_trait = will_not_wed
			}
			if = {
				limit = { has_character_flag = directed_to_marry }
				remove_character_flag = directed_to_marry
			}
		}
	}

	# regular updates
	effect_group = {
		days = { 200 300 }
		first_valid = {
			# should I still have the trait at all?
			triggered_effect = {
				trigger = {
					story_owner = {
						OR = {
							NOT = { has_trait = will_not_wed } # shouldn't happen, but just in case
							is_married = yes # shouldn't happen, but just in case
							is_betrothed = yes # shouldn't happen, but just in case
							is_ruler = yes # should lose the trait automatically, but just in case
							has_trait = eunuch
							has_trait = devoted
							has_trait = order_member
							# the below is referring to the character having run off on their own
							NOR = {
								exists = host
								has_character_modifier = lust_for_adventure
							}
							# the below refers to a character who isn't closely related to their host/liege
							# and thus should lose the trait anyhow
							AND = {
								is_adult = yes
								NOR = {
									has_character_modifier = lust_for_adventure
									AND = {
										exists = host
										is_non_courtier_marriage_candidate = { RULER = host }
									}
									AND = {
										exists = liege
										is_courtier = yes
										OR = {
											is_close_or_extended_family_of = host
											is_great_grandchild_of = host
										}
									}
								}
							}
						}
					}
				}
				effect = {
					end_story = yes
				}
			}
			# do I have a lover I should marry?
			triggered_effect = {
				trigger = {
					story_owner = {
						exists = host
						is_physically_able_ai_adult = yes
						num_of_relation_lover = 1
						trigger_if = {
							limit = { NOT = { has_character_flag = directed_to_marry } }
							might_agree_to_marriage = yes
						}
						any_relation = {
							type = lover
							is_physically_able_ai_adult = yes
							NOR = {
								is_close_or_extended_family_of = root.story_owner
								has_relation_rival = root.story_owner.host
								has_relation_lover = root.story_owner.host
								root.story_owner.host = { has_imprisonment_reason = prev }
								is_consort_of = root.story_owner.host
								betrothed ?= root.story_owner.host
								is_concubine = yes
								is_diarch = yes
								would_need_elopement_to_marry_target = { TARGET = root.story_owner }
							}
							might_agree_to_marriage = yes
							can_be_introduced_character_trigger = { CHARACTER = root.story_owner }
						}
					}
				}
				effect = {
					story_owner = {
						save_scope_as = owner
						random_relation = {
							type = lover # they only have the one lover, so no need to use same checks
							save_scope_as = target
						}
					}
					if = {
						limit = {
							scope:owner = {
								is_available = yes
								bp2_valid_for_standard_interactions_trigger = yes
							}
							scope:target = {
								is_available = yes
								bp2_valid_for_standard_interactions_trigger = yes
								NOR = {
									is_at_war_with = root.story_owner.host
									liege ?= { is_at_war_with = root.story_owner.host }
									AND = {
										exists = inspiration
										has_completed_inspiration = no
									}
								}
							}
						}
						# proceed with marriage request
						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 recheck before the next pulse
					else = {
						scope:owner = {
							trigger_event = { id = lmf_heir.1520 days = 100 }
						}
					}
				}
			}
			# do I have a secret lover who I can't marry?
			triggered_effect = {
				trigger = {
					NOT = { exists = var:willnotwed_had_lover_revealed }
					story_owner = {
						exists = host
						is_physically_able_ai_adult = yes
						num_of_relation_lover = 1
						trigger_if = {
							limit = { NOT = { has_character_flag = directed_to_marry } }
							might_agree_to_marriage = yes
						}
						# has a secret lover
						any_relation = {
							type = lover
							is_physically_able_ai_adult = yes
							NOR = {
								is_close_or_extended_family_of = root.story_owner
								has_relation_rival = root.story_owner.host
								has_relation_lover = root.story_owner.host
								root.story_owner.host = { has_imprisonment_reason = prev }
								is_consort_of = root.story_owner.host
								betrothed ?= root.story_owner.host
								is_concubine = yes
								is_diarch = yes
							}
							OR = {
								# could otherwise marry but has been refused permission previously
								AND = {
									can_be_introduced_character_trigger = { CHARACTER = root.story_owner }
									would_need_elopement_to_marry_target = { TARGET = root.story_owner }
								}
								# are either already married, betrothed, a monk/nun, holy order member, or same sex
								AND = {
									NOT = {
										can_be_introduced_character_trigger = { CHARACTER = root.story_owner }
									}
									OR = {
										has_trait = devoted
										has_trait = order_member
										has_trait = septon
										has_trait = silent_sister
										is_married = yes
										is_betrothed = yes
										AND = {
											allowed_to_marry_same_sex_trigger = no
											sex_same_as = root.story_owner
										}
									}
								}
							}
						}
					}
				}
				effect = {
					save_scope_as = story
					story_owner = {
						save_scope_as = heir
						random_relation = {
							type = lover
							save_scope_as = heir_lover
						}
					}
					# check to see if they're both available
					if = {
						limit = {
							scope:heir = {
								is_available = yes
								bp2_valid_for_standard_interactions_trigger = yes
							}
							scope:heir_lover = {
								is_available = yes
								bp2_valid_for_standard_interactions_trigger = yes
								NOR = {
									is_at_war_with = scope:heir.host
									liege ?= { is_at_war_with = scope:heir.host }
									AND = {
										exists = inspiration
										has_completed_inspiration = no
									}
								}
							}
						}
						scope:heir = {
							random = {
								chance = 75
								modifier = {
									add = 20
									NOT = {
										any_secret = {
											type = secret_lover
											secret_target = scope:heir_lover
											NOT = { is_known_by = scope:heir.host }
										}
									}
								}
								host = { trigger_event = lmf_heir.1519 }
							}
						}
					}
					# otherwise recheck before the next pulse
					else = {
						scope:heir = {
							trigger_event = { id = lmf_heir.1521 days = 100 }
						}
					}
				}
			}
			# do something else at random
			triggered_effect = {
				trigger = {
					always = yes
				}
				effect = {
					story_owner = {
						random_list = {
							50 = { # suggest some suitors for introduction
								trigger = {
									is_available_ai = yes
									is_healthy = yes
									has_recoverable_disease_trigger = no
									bp2_valid_for_standard_interactions_trigger = yes
									introduction_interaction_can_be_picked_trigger = yes
									is_concubine = no
									NOR = {
										has_character_flag = had_recent_introduction
										has_game_rule = lmf_no_introductions
									}
									exists = matchmaker
									exists = host
									matchmaker = host
									host = {
										is_adult = yes
										is_landed_or_landless_administrative = yes
										exists = capital_province
										is_available = yes
										NOR = {
											is_at_war = yes
											has_variable = is_in_marriage_introduction
											has_government = mercenary_government
											has_government = republic_government
											has_government = theocracy_government
											has_government = holy_order_government
										}
									}
								}
								modifier = {
									factor = 0.5
									is_adult = no
								}
								modifier = {
									factor = 0.5
									might_agree_to_marriage = no
								}
								modifier = {
									factor = 0.5
									OR = {
										has_sexuality = homosexual
										has_sexuality = asexual
									}
								}
								modifier = {
									factor = 2
									age >= 18
								}
								modifier = {
									factor = 2
									has_character_flag = directed_to_marry
								}
								ai_value_modifier = {
									ai_sociability = 0.5
									ai_honor = 0.5
								}
								min = 10
								find_suitable_introduction_candidates = yes
								if = {
									limit = { exists = scope:suitor_1 }
									save_scope_as = secondary_actor
									host = { trigger_event = lmf_heir.1516 }
								}
							}
							50 = { # look for a lover
								trigger = {
									is_physically_able_ai_adult = yes
									has_recoverable_disease_trigger = no
									exists = liege
									num_of_relation_lover < 1
									NOR = {
										has_trait = celibate
										has_trait = infirm
										has_trait = chaste
										has_trait = honest
										is_malformed_trigger = yes
										has_sexuality = asexual
										is_healthy = no
										is_involved_in_any_romantic_scheme = yes
										has_variable = is_in_marriage_introduction
										AND = {
											trait_is_criminal_in_faith_trigger = { TRAIT = trait:fornicator FAITH = root.story_owner.faith GENDER_CHARACTER = root.story_owner }
											ai_honor >= 0
											NOT = { has_trait = fornicator }
										}
										AND = {
											trait_is_shunned_in_faith_trigger = { TRAIT = trait:fornicator FAITH = root.story_owner.faith GENDER_CHARACTER = root.story_owner }
											ai_honor >= medium_positive_ai_value
											NOT = { has_trait = fornicator }
										}
									}
								}
								modifier = {
									add = 100
									likely_to_take_lover_trigger = yes
								}
								ai_value_modifier = {
									ai_honor = -0.5
									ai_boldness = 1
								}
								min = 10
								if = {
									limit = {
										is_player_realm_ruler = { RULER = root.story_owner.liege }
									}
									save_scope_value_as = { name = is_player_realm value = flag:yes }
								}
								trigger_event = lmf_scheme.1020
							}
							100 = { # begin courting scheme
								trigger = {
									is_physically_able_ai_adult = yes
									has_recoverable_disease_trigger = no
									exists = liege
									num_of_relation_lover < 1
									might_agree_to_marriage = yes
									unlikely_to_commit = no
									NOR = {
										has_trait = celibate
										has_trait = infirm
										is_malformed_trigger = yes
										has_sexuality = asexual
										is_healthy = no
										is_involved_in_any_romantic_scheme = yes
										has_variable = is_in_marriage_introduction
									}
								}
								modifier = {
									add = 100
									OR = {
										has_trait = chaste
										has_trait = honest
										has_character_flag = directed_to_marry
									}
								}
								ai_value_modifier = {
									ai_honor = 1
								}
								min = 10
								if = {
									limit = {
										is_player_realm_ruler = { RULER = root.story_owner.liege }
									}
									save_scope_value_as = { name = is_player_realm value = flag:yes }
								}
								trigger_event = lmf_scheme.1010
							}
							50 = { # AWOL heir in the pool finds a commoner spouse
								trigger = {
									is_physically_able_ai_adult = yes
									num_of_relation_lover < 1
									might_agree_to_marriage = yes
									unlikely_to_commit = no
									fertility >= 0.2
									introduction_interaction_can_be_picked_trigger = yes
									NOR = {
										exists = host
										has_trait = celibate
										has_sexuality = asexual
										has_sexuality = homosexual
										is_healthy = no
										is_involved_in_any_romantic_scheme = yes
										has_variable = is_in_marriage_introduction
									}
								}
								modifier = {
									age >= 20
									factor = 2
								}
								modifier = {
									age >= 25
									factor = 2
								}
								save_scope_as = relative
								if = {
									limit = { is_male = yes }
									create_character = {
										template = stray_bastard_beauty
										dynasty = none
										gender = female
										location = scope:relative.location
										save_scope_as = new_maiden
										after_creation = {
											replace_bad_maiden_traits_effect = yes
											set_random_lover_reason = { TARGET = scope:relative }
											marry = scope:relative
										}
									}
								}
								else = {
									create_character = {
										template = stray_bastard_knight
										dynasty = none
										gender = male
										location = scope:relative.location
										save_scope_as = new_maiden
										after_creation = {
											replace_bad_maiden_traits_effect = yes
											set_random_lover_reason = { TARGET = scope:relative }
											marry_matrilineal = scope:relative
										}
									}
								}
							}
							100 = { # voluntarily give up the trait
								trigger = {
									age >= 20
									NOR = {
										has_variable = is_in_marriage_introduction
										is_involved_in_any_romantic_scheme = yes
									}
								}
								modifier = {
									age > 20
									add = {
										value = age
										subtract = 20
										multiply = 50
										if = {
											limit = { is_female =yes }
											multiply = 2
										}
										if = {
											limit = {
												OR = {
													has_sexuality = homosexual
													has_sexuality = asexual
												}
											}
											multiply = 0.5
										}
									}
								}
								modifier = {
									add = 100
									host ?= { highest_held_title_tier < tier_duchy }
								}
								modifier = {
									add = 100
									has_character_modifier = lust_for_adventure
								}
								modifier = {
									factor = 0.5
									OR = {
										has_sexuality = homosexual
										has_sexuality = asexual
									}
								}
								modifier = {
									factor = 0.5
									has_character_flag = directed_to_marry
								}
								modifier = {
									factor = 0.25
									unlikely_to_commit = yes
								}
								save_scope_as = focus_child
								if = {
									limit = {
										host ?= {
											is_close_or_extended_family_of = root.story_owner
											is_ai = no
											NOT = { is_child_of = root.story_owner }
										}
									}
									host = { trigger_event = lmf_heir.1517 }
								}
								else_if = {
									limit = {
										has_character_modifier = lust_for_adventure
										NOT = { exists = liege }
										any_close_or_extended_family_member = { is_landed = yes }
									}
									random_close_or_extended_family_member = {
										limit = {
											is_landed = yes
											reverse_has_opinion_modifier = { target = root.story_owner modifier = refused_marry_for_love_opinion }
										}
										alternative_limit = {
											is_landed = yes
										}
										weight = {
											base = 1
											modifier = {
												add = 20
												is_close_family_of = root.story_owner
											}
											modifier = {
												add = 20
												has_relation_friend = root.story_owner
											}
											modifier = {
												add = -10
												has_relation_rival = root.story_owner
											}
											min = 1
										}
										trigger_event = lmf_heir.1518
									}
								}
								else = {
									add_character_flag = cannot_get_will_not_wed_trait
									root = { end_story = yes }
								}
							}
							100 = { # do nothing (they keep the trait)
								modifier = { # Kids less likely to give it up so that they can continue their dreams
									add = 50
									is_adult = no
								}
								modifier = { # Kids less likely to give it up if they have a crush already
									add = 20
									is_adult = no
									any_relation = {
										type = crush
										count >= 1
									}
								}
								modifier = { # Stubborn characters less likely to give it up
									add = 20
									has_trait = stubborn
								}
								modifier = { # Chaste and Zealous characters less likely to give it up because sex bad
									add = 20
									AND = {
										has_trait = chaste
										has_trait = zealous
									}
								}
								modifier = { # Unlikely to commit
									add = 50
									unlikely_to_commit = yes
								}
							}
						}
					}
				}
			}
		}
	}
}

