﻿determine_primary_story_effect_lover = {
	if = {
		limit = {
			$LOVER_1$ = {
				should_be_primary_lover = { TARGET = $LOVER_2$ }
			}
		}
		$LOVER_1$ = { save_scope_as = primary_lover }
		$LOVER_2$ = { save_scope_as = secondary_lover }
	}
	else = {
		$LOVER_1$ = { save_scope_as = secondary_lover }
		$LOVER_2$ = { save_scope_as = primary_lover }
	}
}

move_pool_lover_to_root_location = {
	if = {
		limit = {
			$TARGET$ = {
				is_pool_character = yes
				exists = location
				NOT = { location = root.location }
			}
		}
		$TARGET$.location = { save_scope_as = lover_location }
		root.location = { save_scope_as = my_location }
		$TARGET$ = {
			every_traveling_family_member = {
				limit = { is_in_pool_at = scope:lover_location }
				hidden_effect = {
					move_to_pool_at = scope:my_location
					set_variable = {
						name = last_visited_ruler
						value = scope:my_location.county.holder
						days = 1825
					}
				}
			}
		}
	}
}

remove_lover_modifiers_effect = {
	if = {
		limit = {
			exists = scope:story.var:lmf_lover_1000_modifier
			scope:story.var:lmf_lover_1000_modifier = scope:story.var:lover_target
		}
		remove_character_modifier = soulmate_bliss_modifier
		scope:story = { remove_variable = lmf_lover_1000_modifier }
	}
	if = {
		limit = {
			exists = scope:story.var:lmf_lover_1030_modifier
			scope:story.var:lmf_lover_1030_modifier = scope:story.var:lover_target
		}
		if = {
			limit = { has_character_modifier = lover_diplomacy_help_modifier }
			remove_character_modifier = lover_diplomacy_help_modifier
		}
		if = {
			limit = { has_character_modifier = lover_diplomacy_help_small_modifier }
			remove_character_modifier = lover_diplomacy_help_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_martial_help_modifier }
			remove_character_modifier = lover_martial_help_modifier
		}
		if = {
			limit = { has_character_modifier = lover_martial_help_small_modifier }
			remove_character_modifier = lover_martial_help_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_stewardship_help_modifier }
			remove_character_modifier = lover_stewardship_help_modifier
		}
		if = {
			limit = { has_character_modifier = lover_stewardship_help_small_modifier }
			remove_character_modifier = lover_stewardship_help_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_intrigue_help_modifier }
			remove_character_modifier = lover_intrigue_help_modifier
		}
		if = {
			limit = { has_character_modifier = lover_intrigue_help_small_modifier }
			remove_character_modifier = lover_intrigue_help_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_learning_help_modifier }
			remove_character_modifier = lover_learning_help_modifier
		}
		if = {
			limit = { has_character_modifier = lover_learning_help_small_modifier }
			remove_character_modifier = lover_learning_help_small_modifier
		}
		scope:story = { remove_variable = lmf_lover_1030_modifier }
	}
	if = {
		limit = {
			exists = scope:story.var:lmf_lover_1045_modifier
			scope:story.var:lmf_lover_1045_modifier = scope:story.var:lover_target
		}
		if = {
			limit = { has_character_modifier = lover_diplomacy_advice_modifier }
			remove_character_modifier = lover_diplomacy_advice_modifier
		}
		if = {
			limit = { has_character_modifier = lover_diplomacy_advice_small_modifier }
			remove_character_modifier = lover_diplomacy_advice_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_martial_advice_modifier }
			remove_character_modifier = lover_martial_advice_modifier
		}
		if = {
			limit = { has_character_modifier = lover_martial_advice_small_modifier }
			remove_character_modifier = lover_martial_advice_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_stewardship_advice_modifier }
			remove_character_modifier = lover_stewardship_advice_modifier
		}
		if = {
			limit = { has_character_modifier = lover_stewardship_advice_small_modifier }
			remove_character_modifier = lover_stewardship_advice_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_intrigue_advice_modifier }
			remove_character_modifier = lover_intrigue_advice_modifier
		}
		if = {
			limit = { has_character_modifier = lover_intrigue_advice_small_modifier }
			remove_character_modifier = lover_intrigue_advice_small_modifier
		}
		if = {
			limit = { has_character_modifier = lover_learning_advice_modifier }
			remove_character_modifier = lover_learning_advice_modifier
		}
		if = {
			limit = { has_character_modifier = lover_learning_advice_small_modifier }
			remove_character_modifier = lover_learning_advice_small_modifier
		}
		scope:story = { remove_variable = lmf_lover_1045_modifier }
	}
	if = {
		limit = {
			exists = scope:story.var:lmf_lover_1050_modifier
			scope:story.var:lmf_lover_1050_modifier = scope:story.var:lover_target
		}
		if = {
			limit = { has_character_modifier = physician_lover_nurses_me_modifier }
			remove_character_modifier = physician_lover_nurses_me_modifier
		}
		if = {
			limit = { has_character_modifier = lover_nurses_me_modifier }
			remove_character_modifier = lover_nurses_me_modifier
		}
		scope:story = { remove_variable = lmf_lover_1050_modifier }
	}
	if = {
		limit = {
			exists = scope:story.var:lmf_lover_1110_modifier
			scope:story.var:lmf_lover_1110_modifier = scope:story.var:lover_target
		}
		remove_character_modifier = lover_distraction_modifier
		scope:story = { remove_variable = lmf_lover_1110_modifier }
	}
	if = {
		limit = {
			exists = scope:story.var:lmf_lover_1170_modifier
			scope:story.var:lmf_lover_1170_modifier = scope:story.var:lover_target
		}
		scope:story.var:lover_target = {
			if = {
				limit = { has_character_modifier = physician_lover_nurses_me_modifier }
				remove_character_modifier = physician_lover_nurses_me_modifier
			}
			if = {
				limit = { has_character_modifier = lover_nurses_me_modifier }
				remove_character_modifier = lover_nurses_me_modifier
			}
		}
		scope:story = { remove_variable = lmf_lover_1170_modifier }
	}
}

no_hat_respectful_effect = {
	if = {
		limit = { portrait_wear_armor_trigger = yes }
		add_character_flag = no_hat
	}
}

remove_no_hat_effect = {
	if = {
		limit = { has_character_flag = no_hat }
		remove_character_flag = no_hat
	}
}

make_naked_effect = {
	add_character_flag = is_naked
	add_character_flag = no_hat
}

remove_naked_effect = {
	if = {
		limit = { has_character_flag = is_naked }
		remove_character_flag = is_naked
	}
	remove_no_hat_effect = yes
}

recheck_disease_recovery_effect = {
	if = { #Ill
		limit = { has_trait = ill }
		trigger_event = {
			id = health.1101
			days = { ill_recovery_min ill_recovery_max }
		}
	}
	else_if = { #Pneumonic
		limit = { has_trait = pneumonic }
		trigger_event = {
			id = health.1102
			days = { pneumonic_recovery_min pneumonic_recovery_max }
		}
	}
	else_if = { #Gout
		limit = { has_trait = gout_ridden }
		random = {
			chance = 25
			modifier = {
				has_trait = gluttonous
				add = -15
			}
			modifier = {
				has_trait = temperate
				add = 15
			}
			trigger_event = {
				id = health.1103
				days = { gout_ridden_recovery_min gout_ridden_recovery_max } #1 to 30 years
			}
		}
	}
	else_if = { #Typhus
		limit = { has_trait = typhus }
		trigger_event = {
			id = health.1105
			days = { 45 150 }
		}
	}
	else_if = { #Consumption
		limit = { has_trait = consumption }
		trigger_event = {
			id = health.1106
			days = { 365 2190 } #1 to 6 years
		}
	}
	else_if = { #Cancer
		limit = { has_trait = cancer }
		random = {
			chance = 1
			trigger_event = {
				id = health.1107
				days = { 365 3650 } #1 to 10 years
			}
		}
	}
	else_if = { #Great pox
		limit = {
			OR = {
				has_trait = early_great_pox
				has_trait = great_pox
			}
		}
		trigger_event = {
			id = health.1109
			days = { 730 5475 } #2 to 15 years (note that early pox progresses into great pox in 60-150 days and great pox leads to lunacy after 5 to 15 years)
		}
	}
	else_if = { #Smallpox
		limit = { has_trait = smallpox }
		trigger_event = {
			id = health.1110
			days = { smallpox_min_recovery_time smallpox_max_recovery_time }
		}
	}
	else_if = { #Bubonic Plague
		limit = { has_trait = bubonic_plague }
		trigger_event = {
			id = health.1111
			days = { bubonic_plague_min_recovery_time bubonic_plague_max_recovery_time }
		}
	}
	else_if = { #Measles
		limit = { has_trait = measles }
		trigger_event = {
			id = health.1112
			days = { measles_min_recovery_time measles_max_recovery_time }
		}
	}
	else_if = { #Dysentery
		limit = { has_trait = dysentery }
		trigger_event = {
			id = health.1113
			days = { dysentery_min_recovery_time dysentery_max_recovery_time }
		}
	}
	else_if = { #Ergotism
		limit = { has_trait = ergotism }
		trigger_event = {
			id = health.1114
			days = { ergotism_min_recovery_time ergotism_max_recovery_time }
		}
	}
}

have_sex_if_extra_pregnancy_restrictions = {
	if = {
		limit = {
			fertility > 0.10
			$PARTNER$.fertility > 0.10
			OR = {
				has_game_rule = lmf_realistic_pregnancies_active
				AND = {
					has_game_rule = lmf_realistic_pregnancies_player_only
					OR = {
						should_use_realistic_pregnancy_player_only = yes
						$PARTNER$ = { should_use_realistic_pregnancy_player_only = yes }
					}
				}
			}
		}
		had_sex_with_effect = { CHARACTER = $PARTNER$ PREGNANCY_CHANCE = pregnancy_chance }
	}
}

kick_out_courtier_effect = {
	hidden_effect = {
		send_interface_message = {
			type = event_generic_neutral
			title = msg_kicked_courtier_from_court_interaction_title

			right_icon = $KICKED$

			show_as_tooltip = {
				$KICKER$ = {
					remove_courtier_or_guest = $KICKED$
				}
			}

			#Remove them as guardian/ward for any courtier
			if = {
				limit = {
					any_courtier = { has_relation_guardian = $KICKED$ }
				}
				every_courtier = {
					limit = { has_relation_guardian = $KICKED$ }
					save_scope_as = ward
					remove_guardian_effect = {
						GUARDIAN = $KICKED$
						WARD = scope:ward
						RETURN_WARD = yes
						HIDE_OPINION = no
					}
				}
			}
			if = {
				limit = {
					any_courtier = { has_relation_ward = $KICKED$ }
				}
				random_courtier = {
					limit = { has_relation_ward = $KICKED$ }
					save_scope_as = guardian

					send_interface_message = {
						type = event_childhood_neutral
						title = remove_guardian_interaction_notification
						left_icon = $KICKED$
						right_icon = scope:guardian
						remove_guardian_effect = {
							GUARDIAN = scope:guardian
							WARD = $KICKED$
							RETURN_WARD = yes
							HIDE_OPINION = no
						}
					}
				}
			}
		}
	}

	#To be able to send a cohesive interface message AND show a good tooltip
	show_as_tooltip = {
		#Remove them as guardian for any child
		if = {
			limit = {
				any_courtier = { has_relation_guardian = $KICKED$ }
			}
			every_courtier = {
				limit = { has_relation_guardian = $KICKED$ }
				save_scope_as = ward
				remove_guardian_effect = {
					GUARDIAN = $KICKED$
					WARD = scope:ward
					RETURN_WARD = yes
					HIDE_OPINION = no
				}
			}
		}

		#Remove them as ward of any courtier
		if = {
			limit = {
				any_courtier = { has_relation_ward = $KICKED$ }
			}
			random_courtier = {
				limit = { has_relation_ward = $KICKED$ }
				save_scope_as = guardian
				remove_guardian_effect = {
					GUARDIAN = scope:guardian
					WARD = $KICKED$
					RETURN_WARD = yes
					HIDE_OPINION = no
				}
			}
		}
	}

	$KICKED$ = {
		every_traveling_family_member = {
			limit = {
				NOR = {
					is_consort_of = $KICKER$
					is_close_or_extended_family_of = $KICKER$
				}
			}
			add_to_temporary_list = kicked_family
		}
	}

	every_in_list = {
		list = kicked_family
		#Kicks, adds opinions, adds people to kicked_relatives_list
		kick_from_court_interaction_effect = yes
	}

	if = {
		limit = {
			any_in_list = {
				list = kicked_relatives_list
				is_alive = yes
			}
		}
		every_in_list = {
			list = kicked_relatives_list
			custom = all_close_family_members_of_kicked_characters
			add_opinion = {
				modifier = kicked_relative_from_court
				target = $KICKER$
			}
		}
	}

	employed_booner_invalidation_effect = { EMPLOYEE = $KICKED$ LIEGE = $KICKER$ }
}

