﻿had_sex_with_effect = {
	save_scope_as = had_sex_root_character
	$CHARACTER$ = { save_scope_as = had_sex_with_effect_partner }

	# Error logging
	## Children should never run this effect.
	if = {
		limit = {
			OR = {
				scope:had_sex_root_character = { is_adult = no }
				scope:had_sex_with_effect_partner = { is_adult = no }
			}
		}
		error_log = "Trying to run had_sex_with_effect but one or more partners is a child"
	}
	## Did you forget to run the check for whether adultery was valid?
	if = {
		limit = {
			# Rarely, we may also want to block this check (e.g., seduction schemes or monogamous threesomes).
			NOT = { exists = scope:ignore_cheating_error_check }
		}
		# Check whether the root char is cheating.
		if = {
			limit = {
				scope:had_sex_root_character = {
					NOR = {
						is_consort_of = scope:had_sex_with_effect_partner
						has_relation_lover = scope:had_sex_with_effect_partner
					}
					might_cheat_on_every_partner_trigger = no
				}
			}
			# The weird structure here is so that we can use prev scope in loc to grab both chars.
			scope:had_sex_with_effect_partner = {
				scope:had_sex_root_character = { error_log = had_sex_with_effect.log.root_cheated_inappropriately }
			}
		}
		# Check whether their partner is cheating.
		## Though this does lead to potential duplication in the error log, where you'll get an entry for X cheating with Y _and_ for Y cheating with X, it is necessary: it's perfectly possible for one char to be cheating but the other not.
		if = {
			limit = {
				scope:had_sex_with_effect_partner = {
					NOR = {
						is_consort_of = scope:had_sex_root_character
						has_relation_lover = scope:had_sex_root_character
					}
					might_cheat_on_every_partner_trigger = no
				}
			}
			# The weird structure here is so that we can use prev scope in loc to grab both chars.
			scope:had_sex_root_character = {
				scope:had_sex_with_effect_partner = { error_log = had_sex_with_effect.log.target_cheated_inappropriately }
			}
		}
	}
	### Plus, remove the blocking scope we might have.
	clear_saved_scope = ignore_cheating_error_check

	if = {
		limit = { this = root }
		custom_tooltip = had_sex_with_effect.first_person.tt
	}
	else_if = {
		limit = { root = scope:had_sex_with_effect_partner }
		custom_tooltip = had_sex_with_effect.other_person.tt
	}
	else = {
		custom_tooltip = had_sex_with_effect.third_person.tt
	}

	if = {
		limit = {
			NOT = { exists = scope:no_sex_memory }
		}
		create_character_memory = {
			type = had_sex
			participants = {
				sex_partner = scope:had_sex_with_effect_partner
			}
		}
	}

	#Stress
	if = {
		limit = { # For use with events that add/remove more stress than these values, to avoid ugly tooltips
			NOT = {
				has_character_flag = no_sex_stress_loss
			}
		}
		$CHARACTER$ = {
			if = {
				limit = { this = root }
				stress_impact = {
					lustful = minor_stress_impact_loss
					rakish = minor_stress_impact_loss
				}
			}
			else = {
				hidden_effect = {
					stress_impact = {
						lustful = minor_stress_impact_loss
						rakish = minor_stress_impact_loss
					}
				}
			}
		}
		if = {
			limit = { this = root }
			stress_impact = {
				lustful = minor_stress_impact_loss
				rakish = minor_stress_impact_loss
			}
		}
		else = {
			hidden_effect = {
				stress_impact = {
					lustful = minor_stress_impact_loss
					rakish = minor_stress_impact_loss
				}
			}
		}
	}

	#Pregnancy & story cycles
	hidden_effect = {
		if = {
			limit = {
				is_female = yes
				$CHARACTER$ = { is_male = yes }
				possible_pregnancy_after_sex_with_character_trigger = { CHARACTER = scope:had_sex_with_effect_partner }
			}
			add_to_variable_list = {
				name = potential_father_variable_list
				target = $CHARACTER$
			}
			random = {
				chance = $PREGNANCY_CHANCE$
				make_pregnant = {
					father = $CHARACTER$
				}
			}
			# added to make sure the pregnancy can actually take effect, if it occurs
			event_pregnancy_effect = yes
			if = {
				limit = {
					is_ai = no
					NOT = { has_character_flag = had_story_cycle_peasant_affair }
					is_ruler = yes
					$CHARACTER$ = {
						is_lowborn = yes
						is_playable_character = no
						is_courtier = no
					}
					NOT = {
						any_scheme = {
							scheme_target_character = $CHARACTER$
						}
					}
				}
				set_variable = {
					name = story_peasant_affair_target
					value = $CHARACTER$
				}
				create_story = story_peasant_affair
			}
		}
		else_if = {
			limit = {
				is_male = yes
				$CHARACTER$ = { is_female = yes }
				possible_pregnancy_after_sex_with_character_trigger = { CHARACTER = scope:had_sex_with_effect_partner }
			}
			$CHARACTER$ = {
				add_to_variable_list = {
					name = potential_father_variable_list
					target = scope:had_sex_root_character
				}
			}
			if = {
				limit = {
					is_ai = no
					NOT = { has_character_flag = had_story_cycle_peasant_affair }
					is_ruler = yes
					$CHARACTER$ = {
						is_lowborn = yes
						is_playable_character = no
						is_courtier = no
					}
					NOT = {
						any_scheme = {
							scheme_target_character ?= $CHARACTER$
						}
					}
				}
				set_variable = {
					name = story_peasant_affair_target
					value = $CHARACTER$
				}
				$CHARACTER$ = {
					if = {
						limit = {
							is_pregnant = no
							fertility >= 0.1
							is_visibly_fertile = yes
						}
						random = {
							chance = $PREGNANCY_CHANCE$
							add_character_flag = {
								flag = peasant_is_pregnant
								days = 560
							}
						}
					}
				}
				create_story = story_peasant_affair
			}
			else = {
				$CHARACTER$ = {
					random = {
						chance = $PREGNANCY_CHANCE$
						make_pregnant = {
							father = scope:had_sex_root_character
						}
					}
					# added to make sure the pregnancy can actually take effect, if it occurs
					event_pregnancy_effect = yes
				}
			}
		}
		
		#STD transmission
		risk_of_std_from_effect = { PARTNER = $CHARACTER$ }
		hidden_effect = {
			$CHARACTER$ = { risk_of_std_from_effect = { PARTNER = scope:had_sex_root_character } }
		}
	
		#Adultery suspicion
		hidden_effect = {
			if = {
				limit = {
					OR = {
						is_married = yes
						is_concubine = yes
					}
					NOR = {
						any_consort = { this = $CHARACTER$ }
						AND = {
							house.house_head ?= this
							house ?= { has_house_aspiration_parameter = aspect_of_creation }
						}
					}
				}
				set_variable = {
					name = had_recent_sex_with
					value = $CHARACTER$
					days = 1000
				}
				random = {
					chance = 50 #This should be quite high as there is a chance that adultery.0001 "fails" i.e. spouse doesn't realize
					every_relation = {
						type = soulmate
						limit = {
							NOT = { this = $CHARACTER$ } #Soulmates should not grow suspicous of adulterous affairs after sleeping with their lover
						}
						add_to_temporary_list = partners_1
					}
					every_consort = {
						add_to_temporary_list = partners_1
					}
					random_in_list = {
						list = partners_1
						limit = { is_ai = no }
						alternative_limit = { always = yes }
						trigger_event = {
							id = adultery.0001
							days = { 14 365 }
						}
					}
				}
			}
			$CHARACTER$ = {
				if = {
					limit = {
						OR = {
							is_married = yes
							is_concubine = yes
						}
						NOR = {
							any_consort = { this = scope:had_sex_root_character }
							AND = {
								house.house_head ?= this
								house ?= { has_house_aspiration_parameter = aspect_of_creation }
							}
						}
					}
					set_variable = {
						name = had_recent_sex_with
						value = scope:had_sex_root_character
						days = 1000
					}
					random = {
						chance = 50 #This should be quite high as there is a chance that adultery.0001 "fails" i.e. spouse doesn't realize
						every_relation = {
							type = soulmate
							limit = {
								this != scope:had_sex_root_character #Soulmates should not grow suspicous of adulterous affairs after sleeping with their lover
							}
							add_to_temporary_list = partners_2
						}
						every_consort = {
							add_to_temporary_list = partners_2
						}
						random_in_list = {
							list = partners_2
							limit = { is_ai = no }
							alternative_limit = { always = yes }
							trigger_event = {
								id = adultery.0001
								days = { 14 365 }
							}
						}
					}
				}
			}
		}

		#Incestuous Relations
		give_incest_secret_or_nothing_with_target_effect = { CHARACTER = $CHARACTER$ }
		hidden_effect = {
			$CHARACTER$ = { give_incest_secret_or_nothing_with_target_effect = { CHARACTER = scope:had_sex_root_character } }
		}

		#Homosexuality
		give_homosexual_secret_or_nothing_with_target_effect = { CHARACTER = $CHARACTER$ }
		hidden_effect = {
			$CHARACTER$ = { give_homosexual_secret_or_nothing_with_target_effect = { CHARACTER = scope:had_sex_root_character } }
		}
	}

	#Violet Poet trait
	if = {
		limit = {
			scope:had_sex_root_character = {
				has_trait = violet_poet
			}
		}
		scope:had_sex_root_character = {
			add_trait_xp = {
				trait = violet_poet
				value = {
					integer_range = {
						min = 5
						max = 10
					}
				}
			}
		}
	}
	if = {
		limit = {
			scope:had_sex_with_effect_partner = {
				has_trait = violet_poet
			}
		}
		scope:had_sex_with_effect_partner = {
			add_trait_xp = {
				trait = violet_poet
				value = {
					integer_range = {
						min = 5
						max = 10
					}
				}
			}
		}
	}

	# Achievements
	fp2_had_friend_with_benefits = yes # FP2 History's Best Friend
}

