﻿namespace = stress_threshold_special

###############
# Special Mental Breaks
# 	stress_threshold_special.1001 - Mourning the loss of someone I cared about.
#	stress_threshold_special.2001 - Rakish character has become Celibate
#

#############################
# Torrential Grief 			#
# by Sean Hughes			#
# 1001						#
#############################

scripted_trigger stress_threshold_special_1001_confidant_trigger = {
	ai_compassion >= medium_positive_ai_value
	opinion = {
		target = root
		value >= medium_positive_opinion
	}
}

scripted_trigger stress_threshold_special_1001_depression_trigger = {
	OR = {
		var:mental_break_deceased_character_relation = flag:lover
		var:mental_break_deceased_character_relation = flag:spouse
		AND = {
			var:mental_break_deceased_character_relation = flag:child
			NOT = {	
				always = no				
				any_spouse = {
					is_alive = yes
					OR = {
						this = scope:deceased_character.mother
						this = scope:deceased_character.father
					}
				}
			}
		}
		stress >= major_stress_gain_threshold # Stress Level 3
	}
}

stress_threshold_special.1001 = {
	type = character_event
	title = stress_threshold_special.1001.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {var:mental_break_deceased_character_relation = flag:lover}
				desc = stress_threshold_special.1001.desc_intro_lover
			}
			triggered_desc = {
				trigger = {var:mental_break_deceased_character_relation = flag:friend}
				desc = stress_threshold_special.1001.desc_intro_friend
			}
			desc = stress_threshold_special.1001.desc_intro_standard
		}
		first_valid = {
			triggered_desc = {
				trigger = {var:mental_break_deceased_character_relation = flag:spouse}
				desc = stress_threshold_special.1001.desc_spouse
			}
			triggered_desc = {
				trigger = {
					var:mental_break_deceased_character_relation = flag:child
					scope:deceased_character.age < childhood_education_start_age
				}
				desc = stress_threshold_special.1001.desc_child_toddler
			}
			triggered_desc = {
				trigger = {
					var:mental_break_deceased_character_relation = flag:child
					scope:deceased_character.age >= childhood_education_start_age
				}
				desc = stress_threshold_special.1001.desc_child_older
			}
			triggered_desc = {
				trigger = {var:mental_break_deceased_character_relation = flag:parent}
				desc = stress_threshold_special.1001.desc_parent
			}
			triggered_desc = {
				trigger = {var:mental_break_deceased_character_relation = flag:lover}
				desc = stress_threshold_special.1001.desc_lover
			}
			triggered_desc = {
				trigger = {var:mental_break_deceased_character_relation = flag:friend}
				desc = stress_threshold_special.1001.desc_friend
			}
		}
	}
	
	theme = mental_break
	left_portrait = {
		character = root
		animation = grief
	}
	right_portrait = scope:deceased_character
	lower_center_portrait = scope:confidant
	
	trigger = {
		exists = var:mental_break_deceased_character
	}

	weight_multiplier = {
		base = 1
	}

	widget = {
		gui = "event_window_widget_stress"
		container = "custom_widgets_container"
	}

	immediate = {
		root = { save_scope_as = stress_character } # For stress widget
		var:mental_break_deceased_character = {	save_scope_as = deceased_character } # For easier localization references

		if = {
			limit = { # Pre-check if you lost a child and are (still) married to their other parent.
				any_spouse = {
					OR = {
						this = scope:deceased_character.mother
						this = scope:deceased_character.father
					}
				}
			}
			random_spouse = {
				limit = {
					OR = {
						this = scope:deceased_character.mother
						this = scope:deceased_character.father
					}
				}
				save_scope_as = confidant
				root = {
					add_character_flag = stress_threshold_option_confider
				}
			}
		}
		if = {
			limit = { # Severe Depression - High Stress
				has_two_stress_threshold_options = no
				stress >= medium_stress_gain_threshold # Stress Level 2
				stress_threshold_special_1001_depression_trigger = yes
			}
			add_character_flag = stress_threshold_option_depression
		}
		if = { # Substance Abuse (Alcohol or Cannabis, depending on Faith/Region.)
			limit = {
				has_two_stress_threshold_options = no
				OR = {
					can_be_drunkard = yes
					can_be_hashishiyah = yes
					has_trait = drunkard
					has_trait = hashishiyah
				}
			}
			hidden_effect = {
				random_list = { # Which option the player gets is somewhat dependent on where they are and what their faith is.
					50 = {
						trigger = {
							OR = {
								can_be_drunkard = yes
								has_trait = drunkard
							}
						}
						add_character_flag = stress_threshold_option_drunkard
					}
					50 = {
						trigger = {
							OR = {
								can_be_hashishiyah = yes
								has_trait = hashishiyah
							}
						}
						add_character_flag = stress_threshold_option_hashishiyah
					}
				}
			}
		}
		if = {
			limit = {
				has_two_stress_threshold_options = no
				scope:deceased_character.killer = root
				OR = {
					has_trait = flagellant
					can_be_flagellant = yes
				}
			}
			add_character_flag = stress_threshold_option_flagellant
		}
		if = {
			limit = {
				has_two_stress_threshold_options = no
				OR = {
					has_trait = inappetetic
					can_be_inappetetic = yes
				}
			}
			add_character_flag = stress_threshold_option_inappetetic
		}
		if = {
			limit = {
				has_two_stress_threshold_options = no
				OR = {
					has_trait = journaller
					can_be_journaller = yes
				}
			}
			add_character_flag = stress_threshold_option_journaller
		}
		if = {
			limit = {
				has_two_stress_threshold_options = no
				has_trait = confider
				can_be_confider = yes
				# Check if we have a valid confidant
				OR = {
					any_spouse = {
						stress_threshold_special_1001_confidant_trigger = yes
					}
					any_relation = {
						type = friend
						stress_threshold_special_1001_confidant_trigger = yes
					}
					any_relation = {
						type = lover
						stress_threshold_special_1001_confidant_trigger = yes
					}
				}
			}

			# Find a valid confidant and save their scope.
			random_spouse = {
				limit = { stress_threshold_special_1001_confidant_trigger = yes }
				save_scope_as = confidant
			}
			if = {
				limit = { NOT = { exists = scope:confidant} }
				random_relation = {
					type = friend
					limit = { stress_threshold_special_1001_confidant_trigger = yes }
					save_scope_as = confidant
				}
				if = {
					limit = { NOT = { exists = scope:confidant} }
					random_relation = {
						type = lover
						limit = { stress_threshold_special_1001_confidant_trigger = yes }
						save_scope_as = confidant
					}
				}
			}

			if = {
				# Add confider flag.
				limit = {
					exists = scope:confidant
				}				
				add_character_flag = stress_threshold_option_confider
			}

		}
		if = { # Severe Depression - Low Stress
			limit = {
				has_two_stress_threshold_options = no
				stress_threshold_special_1001_depression_trigger = yes
			}
			add_character_flag = stress_threshold_option_depression
		}
		stress_threshold_event_post_immediate = yes
	}

	# Option A: Become Depressed
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_depression
		}
		name = stress_threshold_special.1001.depressed
		trait = depressed_1
		trait = depressed_genetic

		add_trait = depressed_1
		stress_impact = {
			base = major_stress_impact_loss
		}
	}

	# Option B-1: Become Drunkard
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_drunkard
		}
		name = stress_threshold_special.1001.drunkard
		trait = drunkard

		if = {
			limit = {
				has_trait = drunkard
			}
			add_character_modifier = {
				modifier = stress_drinking_binge
				years = 3
			}
		}	
		else = {
			add_trait = drunkard
		}
		
		stress_impact = {
			base = medium_stress_impact_loss
		}
	}

	# Option B-2: Become Hashishiyah
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_hashishiyah
		}
		name = stress_threshold_special.1001.hashishiyah
		trait = hashishiyah

		if = {
			limit = {
				has_trait = hashishiyah
			}
			add_character_modifier = {
				modifier = stress_hashish_stupor
				years = 3
			}
		}	
		else = {
			add_trait = hashishiyah
		}
		
		stress_impact = {
			base = medium_stress_impact_loss
		}
	}

	# Option C: Become Flagellant
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_flagellant
		}
		name = stress_threshold_special.1001.flagellant
		trait = flagellant

		if = {
			limit = {
				NOT = { has_trait = flagellant}
			}
			add_trait = flagellant
		}
		increase_wounds_effect = {REASON = whipping}

		stress_impact = {
			base = medium_stress_impact_loss
		}
	}

	# Option D-1: Become Inappetetic
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_inappetetic
		}
		name = {
			text = stress_threshold_special.1001.inappetetic.effect
			trigger = { has_trait = inappetetic }
		}
		name = {
			text = stress_threshold_special.1001.inappetetic.trait
			trigger = { NOT = { has_trait = inappetetic } }
		}
		trait = inappetetic

		if = {
			limit = {
				has_trait = inappetetic
			}
			inappetetic_advance_starvation_effect = yes
		}
		else = {
			add_trait = inappetetic
		}

		stress_impact = {
			base = medium_stress_impact_loss
		}
	}

	# Option G: Become Journaller
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_journaller
		}
		name = stress_threshold_special.1001.journaller

		if = {
			limit = {
				NOT = { has_trait = journaller }
			}
			add_trait = journaller
		}
		add_character_modifier = {
			modifier = stress_managed_grief
			years = 5
		}

		stress_impact = {
			base = medium_stress_impact_loss
		}
	}

	# Option F: Become Confider
	option = {
		trigger = {
			has_character_flag = stress_threshold_option_confider
			#exists = scope:confidant	
		}
		name = {
			text = stress_threshold_special.1001.confider.shared
			trigger = {
				scope:confidant = {
					any_child = { this = scope:deceased_character }
				}
			}
		}
		name = {
			text = stress_threshold_special.1001.confider.assist
			trigger = {
				scope:confidant = {
					NOT = { any_child = { this = scope:deceased_character } }
				}
			}
		}
		trait = confider

		if = {
			limit = {
				NOT = { has_trait = confider }
			}
			add_trait = confider
		}
		add_character_modifier = {
			modifier = stress_managed_grief
			years = 5
		}
		add_opinion = {
			modifier = trust_opinion
			target = scope:confidant
			opinion = 20
		}
		stress_impact = {
			base = medium_stress_impact_loss
		}
		hidden_effect = {
			reverse_add_opinion = {
				modifier = trust_opinion
				target = scope:confidant
				opinion = 20
			}
		}
	}
	# Option G-1: Frozen Grief
	option = {
		name = stress_threshold_special.1001.frozen_grief
		trigger = { NOT = { has_trait = lunatic_1 } }

		add_character_modifier = {
			modifier = stress_frozen_grief
		}

		stress_impact = {
			base = medium_stress_impact_loss
		}
	}

	# Option G-2: Lunatic Corpse
	option = {
		name = stress_threshold_special.1001.lunatic
		trigger = { has_trait = lunatic_1 }
		trait = lunatic_1
		flavor = stress_threshold_special.1001.lunatic.flavor

		add_character_modifier = {
			modifier = stress_stuffed_corpse
		}

		stress_impact = {
			base = massive_stress_impact_loss
		}
	}

	after = {
		stress_threshold_event_aftereffects = yes # Perform stress threshold event clean-up and preparation for future stress threshold events.
	}
}


#############################
# Suppressed Desires		#
# by Sean Hughes			#
# 2001						#
#############################

# Checks if this character is attracted enough to root character to want to have sex with them
scripted_trigger stress_threshold_special_2001_valid_attraction_target_trigger = {

	# Basic validity check
	age >= 16
	is_attracted_to_gender_of = root
	root = { is_attracted_to_gender_of = prev }
	opinion = {
		target = root
		value >= low_positive_opinion
	}
	is_eunuch_trigger = no
	# Disqualifying conditions for target character
	NOR = {
		has_trait = chaste
		has_trait = celibate
		has_sexuality = asexual
	}

	# Target character must be interested in having sex with root character
	OR = {
		has_relation_soulmate = root
		calc_true_if = {
			amount >= 3

			has_trait = lustful
			has_trait = lustful # lustful counts as 2 'points'
			has_relation_lover = root
			has_relation_lover = root # So does an existing lover relation
			opinion = {
				target = root
				value >= medium_positive_opinion
			}
			opinion = {
				target = root
				value >= high_positive_opinion
			}
			trait_compatibility = {
				target = root
				value >= low_positive_trait_compatibility
			}
			trait_compatibility = {
				target = root
				value >= medium_positive_trait_compatibility
			}
			trait_compatibility = {
				target = root
				value >= high_positive_trait_compatibility
			}
			root = { attraction >= 10 }
			root = { attraction >= 25 }
		}
	}
}

# Cognative dissonance between rakish tendecies and a celibate lifestyle.
stress_threshold_special.2001 = {
	type = character_event
	title = stress_threshold_special.2001.t
	desc = {
		desc = stress_threshold_special.2001.beginning
		first_valid = {
			triggered_desc = {
				trigger = { local_var:mental_break_2001_character_relation = flag:consort}
				desc = stress_threshold_special.2001.consort
			}
			triggered_desc = {
				trigger = { local_var:mental_break_2001_character_relation = flag:noble}
				desc = stress_threshold_special.2001.noble
			}
			triggered_desc = {
				trigger = { local_var:mental_break_2001_character_relation = flag:servant}
				desc = stress_threshold_special.2001.servant
			}
		}
		desc = stress_threshold_special.2001.ending
	}
	
	theme = mental_break
	left_portrait = {
		character = root
		animation = stress
	}
	right_portrait = {
		character = scope:target
		animation = flirtation
	}
	override_background = {
		reference = bedchamber
	}

	trigger = {
		has_trait = rakish
		has_trait = celibate

		might_cheat_on_every_partner_trigger = yes
	}

	weight_multiplier = {
		base = 1
	}

	widget = {
		gui = "event_window_widget_stress"
		container = "custom_widgets_container"
	}

	immediate = {

		# First check if any spouse meets the target character requirements, and if so save them as the target character
		random_spouse = {
			limit = {
				stress_threshold_special_2001_valid_attraction_target_trigger = yes
			}
			save_scope_as = target
			set_local_variable = {
				name = mental_break_2001_character_relation
				value = flag:consort
			}

		}
		# If we failed to find a valid spouse, look for a valid pre-existing lover relationship
		if = {
			limit = { NOT = { exists = scope:target } }

			random_relation = {
				type = lover
				limit = { stress_threshold_special_2001_valid_attraction_target_trigger = yes }

				save_scope_as = target
				set_local_variable = {
					name = mental_break_2001_character_relation
					value = flag:consort # Lovers are considered consorts for the purposes of this event (as they are a pre-existing relationship).
				}
			}
		}
		# If that also failed, look for a valid target character among liege's spouses, vassals, and vassal's spouses
		if = {
			limit = { NOT = { exists = scope:target } }
			liege = {
				every_spouse = {
					limit = {
						stress_threshold_special_2001_valid_attraction_target_trigger = yes
						might_cheat_on_every_partner_trigger = yes
					}
					add_to_list = potential_noble_targets
				}
			}
			every_vassal = {
				if = {
					limit = {
						stress_threshold_special_2001_valid_attraction_target_trigger = yes
						might_cheat_on_every_partner_trigger = yes
					}
					add_to_list = potential_noble_targets
				}
				every_spouse = {
					limit = {
						stress_threshold_special_2001_valid_attraction_target_trigger = yes
						might_cheat_on_every_partner_trigger = yes
					}
					add_to_list = potential_noble_targets
				}
			}
			if = {
				# If none were found, move on to your courtiers
				limit = {
					NOT = {
						any_in_list = {
							list = potential_noble_targets
							exists = this
						}
					}
				}
				every_courtier_or_guest = {
					limit = {
						stress_threshold_special_2001_valid_attraction_target_trigger = yes
						might_cheat_on_every_partner_trigger = yes
					}
					add_to_list = potential_noble_targets
				}
			}
			random_in_list = {
				list = potential_noble_targets
				save_scope_as = target
				set_local_variable = {
					name = mental_break_2001_character_relation
					value = flag:noble
				}
			}
		}
		# Finally, if we still don't have a valid character, generate a servant character and add them to root's court
		if = {
			limit = { NOT = { exists = scope:target } }
			create_character = {
				employer = root
				age = { 16 25 }
				gender_female_chance = {
					value = 50
					if = {
						limit = { root = { is_attracted_to_men = yes } }
						add = -50
					}
					if = {
						limit = { root = { is_attracted_to_women = yes } }
						add = 50
					}
				}
				trait = education_intrigue_2
				trait = beauty_good_1
				trait = lustful
				trait = honest
				trait = ambitious
				faith = this.faith
				culture = this.culture
				diplomacy = { 10 12 }
				intrigue = { 14 16 }
				dynasty = none
				save_scope_as = target
			}
			set_local_variable = {
				name = mental_break_2001_character_relation
				value = flag:servant
			}
		}
	}

	# Option A: Remove Celibate, lose Stress
	option = {
		name = stress_threshold_special.2001.a
		remove_trait = celibate
		if = {
			limit = { has_perk = restraint_perk }
			hidden_effect = { remove_perk = restraint_perk }
			custom_tooltip = stress_threshold_special.2001.a.tt 
		}
		had_sex_with_effect = {
			CHARACTER = scope:target
			PREGNANCY_CHANCE = 100
		}
		stress_impact = {
			base = massive_stress_impact_loss
		}
		hidden_effect = {
			if = {
				limit = {
					NOR = {
						has_relation_potential_lover = scope:target
						has_relation_lover = scope:target
						has_relation_soulmate = scope:target
					}
				}
				set_relation_potential_lover = scope:target
			}
		}
	}

	# Option B: Remove Rakish, gain Stress
	option = {
		name = stress_threshold_special.2001.b
		remove_trait = rakish
		if = {
			limit = { faith = { trait_is_sin = lustful }}
			add_piety = medium_piety_gain
		}
		stress_impact = {
			base = major_stress_impact_gain
		}
	}

	after = {
		stress_threshold_event_aftereffects = yes # Perform stress threshold event clean-up and preparation for future stress threshold events.
	}
}
