﻿courting = {
	# Basic Setup
	skill = diplomacy
	desc = courting_desc_general
	success_desc = "COURT_SUCCESS_DESC"
	icon = icon_scheme_romance
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	target_type = character
	is_secret = no
	is_basic = yes
	cooldown = { months = 2 }
	
	# Travel Triggers (default no)
	freeze_scheme_when_traveling = yes
	freeze_scheme_when_traveling_target = yes

	# Parameters
	speed_per_skill_point = -2.5
	spymaster_speed_per_skill_point = 0	
	base_progress_goal = 365
	base_maximum_success = 95
	minimum_success = 5
	
	allow = {
		is_adult = yes
		trigger_if = {
			limit = { is_landless_adventurer = no }
			is_travelling = no
		}
		scope:target = {
			is_adult = yes 
			trigger_if = {
				limit = {
					scope:owner = { is_landless_adventurer = no }
				}
				is_travelling = no
			}
			this != scope:owner
		}
		
		#Have I been blocked from targeting this character?
		custom_description = { #Permanent
			text = seduce_blocked
			object = scope:target

			NOT = {
				scope:target = {
					has_opinion_modifier = {
						modifier = seduce_permanent_blocker_opinion
						target = scope:owner
					}
				}
			}
		}

		#Time-limited blocker
		custom_description = {
			text = romance_cooldown
			object = scope:target

			NOT = {
				scope:target = {
					has_opinion_modifier = {
						modifier = seduce_regular_cooldown_opinion
						target = scope:owner
					}
				}
			}
		}

		#Have I already successfully romanced this person?
		custom_description = {
			text = romance_success_blocked
			object = scope:target

			NOT = {
				scope:target = {
					has_opinion_modifier = {
						modifier = romance_scheme_courtship_opinion
						target = scope:owner
					}
				}
			}
		}

		#They're not interested
		custom_description = {
			text = no_attraction_due_to_gender
			object = scope:target
			subject = scope:owner
			scope:target = { is_attracted_to_gender_of = scope:owner  }
		}

		#AI blockers
		trigger_if = {
			limit = { is_ai = yes }

			trigger_if = {
				limit = {
					OR = {
						any_spouse = { }
						any_relation = { type = lover }
						exists = betrothed
					}
				}
				might_cheat_on_every_partner_trigger = yes
			}

			scope:target = {
				trigger_if = { #So players don't get spammed by AI courting
					limit = {
						is_ai = no
					}
					NOT = {
						any_targeting_scheme = {
							scheme_type = courting
						}
					}
				}
			}
		}
		trigger_if = {
			limit = { is_ai = yes }
			NOT = {
				scope:target = {
					 has_character_flag = courting_cooldown
				}
			}
		}
	}

	valid = {
		is_incapable = no
		#HARD-BLOCK: Incest Target is too young
		NOT = { 
			custom_description = { 
				text = SCHEME_SEDUCE_UNWILLING_INCEST_TOO_YOUNG
				object = scope:target
				guaranteed_under_20_incest_rejection_trigger = {
					TARGET = scope:target
					SEDUCER = scope:owner
				}
			}
		}

		#We're already soulmates
		scope:owner = {
			NOT = {
				custom_description = {
					text = "romance_already_soulmate"
					object = scope:target
					has_relation_soulmate = scope:target
				}
			}
		}
		#You've been kicked out of bed
		custom_description = { 
			text = seduce_blocked
			object = scope:target
			NOT = {
				scope:owner = {
					has_character_modifier = rejected_from_marriage_bed_modifier
					is_spouse_of = scope:target 
				}
			}
		}

		# They can't consent
		NOT = { scope:target = { has_trait = intellect_bad_3 } } # Implies a mental development level akin to an average child
		NOT = { scope:target = { has_trait = incapable } } # Bed-ridden at best, but in many cases comatose or in a vegetative state
		
		# One of you is celibate
		NOT = { has_trait = celibate }
		scope:target = { NOT = { has_trait = celibate } }
		
		# One of you is imprisoned
		is_imprisoned = no
		scope:target = {
			is_imprisoned = no
		}

		# You're too far away
		scope:target = {
			OR = {
				exists = location
				in_diplomatic_range = scope:owner
			}
		}
	}

	odds_prediction = {
		add = lmf_odds_skill_contribution_diplomacy_value
		add = lmf_base_seduce_odds_prediction_target_is_char_value	
		add = lmf_odds_romance_scheme_misc_value
		min = 0
	}

	base_success_chance = {
		base = 0
		lmf_court_diplomacy_success_chance_modifier = yes
		
		# Countermeasures.
		apply_opportunistic_scheme_success_chance_adjustments_modifier = yes

		# You got a wedding night memento
		modifier = {
			add = 20
			desc = "SCHEME_WNIGHT_COURT_MEMENTO"
			scope:owner = {
				exists = var:wnight_court_memento_var
				var:wnight_court_memento_var = scope:target
			}
		}

		modifier = {
			add = 10
			desc = hopeless_romantic_modifier
			scope:owner = { has_character_modifier = hopeless_romantic_modifier }
		}
		
		#Language
		modifier = {
			add = -10
			desc = YOU_DONT_SPEAK_THE_LANGUAGE
			scope:owner = {
				NOT = { knows_language_of_culture = scope:target.culture }
			}
		}
		modifier = {
			add = -10
			desc = THEY_DONT_SPEAK_THE_LANGUAGE
			scope:target = {
				NOT = { knows_language_of_culture = scope:owner.culture }
			}
		}

		#Gallant
		modifier = {
			desc = "SCHEME_GALLANT_TRAIT"
			scope:owner = { has_trait = gallant }
			add = 30
		}

		#Beauty good
		modifier = {
			desc = "scheme_beauty_good_1"
			scope:owner = { has_trait = beauty_good_1 }
			add = 10
		}
		modifier = {
			desc = "scheme_beauty_good_2"
			scope:owner = { has_trait = beauty_good_2 }
			add = 15
		}
		modifier = {
			desc = "scheme_beauty_good_3"
			scope:owner = { has_trait = beauty_good_3 }
			add = 20
		}

		#Physique good
		modifier = {
			desc = "scheme_physique_good_1"
			scope:owner = { has_trait = physique_good_1 }
			add = 5
		}
		modifier = {
			desc = "scheme_physique_good_2"
			scope:owner = { has_trait = physique_good_2 }
			add = 10
		}
		modifier = {
			desc = "scheme_physique_good_3"
			scope:owner = { has_trait = physique_good_3 }
			add = 15
		}

		#Beauty bad
		modifier = {
			desc = "scheme_beauty_bad_1"
			scope:owner = { has_trait = beauty_bad_1 }
			add = -10
		}
		modifier = {
			desc = "scheme_beauty_bad_2"
			scope:owner = { has_trait = beauty_bad_2 }
			add = -15
		}
		modifier = {
			desc = "scheme_beauty_bad_3"
			scope:owner = { has_trait = beauty_bad_3 }
			add = -20
		}
		
		# You are a Courteous Knight
		modifier = {
			add = 50
			desc = "SCHEME_COURTEOUS_KNIGHT"
			scope:owner = {
				has_character_modifier = ep3_courteous_knight_modifier
			}
		}
		modifier = {
			desc = scheme_family_widow_seduce_romance
			scope:owner ?= {
				var:family_widow_forbidden_divorce ?= scope:target
			}
			add = 20
		}

		#TARGET

		#Does their spouse have a strong hook on them?
		modifier = {
			scope:target = {
				exists = primary_spouse
				NOT = { primary_spouse = scope:owner }
				OR = {
					primary_spouse = { has_strong_hook = scope:target }
					has_trait = loyal
				}
			}
			add = -100
			desc = SCHEME_SEDUCE_SPOUSE_STRONG_HOOK_PENALTY
		}
		
		#Opinion of owner
		opinion_modifier = {
			who = scope:target
			opinion_target = scope:owner
			min = -50
			max = 50
			multiplier = 1.5
			step = 5
		}

		#Trait similitarity to owner
		compatibility_modifier = {
			who = scope:target
			compatibility_target = scope:owner
			min = -100
			max = 80
			multiplier = 3
		}

		#Rank tier difference (landed target/target whose liege doesn't care)
		modifier = { #3 or more higher rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = 20
			desc = "HIGHER_RANK_THAN_SCHEME_TARGET"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = no
				NOT = {
					is_theocratic_lessee = yes
				}
			}
			scope:owner = {
				tier_difference = {
					target = scope:target
					value >= 3
				}
			}
		}
		modifier = { #2 higher rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = 10
			desc = "HIGHER_RANK_THAN_SCHEME_TARGET"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = no
				NOT = {
					is_theocratic_lessee = yes
				}
			}
			scope:owner = {
				tier_difference = {
					target = scope:target
					value = 2
				}
			}
		}
		modifier = { #1 higher rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = 5
			desc = "HIGHER_RANK_THAN_SCHEME_TARGET"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = no
				NOT = {
					is_theocratic_lessee = yes
				}
			}
			scope:owner = {
				tier_difference = {
					target = scope:target
					value = 1
				}
			}
		}
		modifier = { #1 lower rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = -5
			desc = "LOWER_RANK_THAN_SCHEME_TARGET"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = no
			}
			scope:owner = {
				tier_difference = {
					target = scope:target
					value = -1
				}
			}
		}
		modifier = { #2 lower rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = -10
			desc = "LOWER_RANK_THAN_SCHEME_TARGET"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = no
			}
			scope:owner = {
				tier_difference = {
					target = scope:target
					value = -2
				}
			}
		}
		modifier = { #3 or less lower rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = -20
			desc = "LOWER_RANK_THAN_SCHEME_TARGET"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = no
			}
			scope:owner = {
				tier_difference = {
					target = scope:target
					value <= -3
				}
			}
		}

		#Rank tier difference (unlanded character)
		modifier = { #3 or more higher rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = 50
			desc = "HIGHER_RANK_THAN_SCHEME_TARGET_LIEGE"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = yes
				NOT = {
					is_theocratic_lessee = yes
				}
			}
			scope:owner = {
				tier_difference = {
					target = scope:target.liege
					value >= 3
				}
			}
		}
		modifier = { #2 higher rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = 25
			desc = "HIGHER_RANK_THAN_SCHEME_TARGET_LIEGE"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = yes
				NOT = {
					is_theocratic_lessee = yes
				}
			}
			scope:owner = {
				tier_difference = {
					target = scope:target.liege
					value = 2
				}
			}
		}
		modifier = { #1 higher rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = 10
			desc = "HIGHER_RANK_THAN_SCHEME_TARGET_LIEGE"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = yes
				NOT = {
					is_theocratic_lessee = yes
				}
			}
			scope:owner = {
				tier_difference = {
					target = scope:target.liege
					value = 1
				}
			}
		}
		modifier = { #1 lower rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = -10
			desc = "LOWER_RANK_THAN_SCHEME_TARGET_LIEGE"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = yes
			}
			scope:owner = {
				tier_difference = {
					target = scope:target.liege
					value = -1
				}
			}
		}
		modifier = { #2 lower rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = -25
			desc = "LOWER_RANK_THAN_SCHEME_TARGET_LIEGE"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = yes
			}
			scope:owner = {
				tier_difference = {
					target = scope:target.liege
					value = -2
				}
			}
		}
		modifier = { #3 or less lower rank
			trigger = { personal_scheme_should_not_evaluate_tier_differences_trigger = yes }
			add = -50
			desc = "LOWER_RANK_THAN_SCHEME_TARGET_LIEGE"
			scope:target = {
				personal_scheme_success_compare_target_liege_tier_trigger = yes
			}
			scope:owner = {
				tier_difference = {
					target = scope:target.liege
					value <= -3
				}
			}
		}

		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_within_realm_bonus_modifier = yes

		#Gubermint difference
		modifier = { #3 or more higher rank
			add = -50
			desc = "TRIBE_ROMANCING_OTHER_GOVERNMENTS"
			scope:target.court_owner ?= {
				NOT = { government_has_flag = government_is_tribal }
			}
			scope:owner = { government_has_flag = government_is_tribal }
		}

		#Religious Heads
		modifier = {
			add = -50
			desc = "SCHEME_VS_RELIGIOUS_HEAD"
			scope:target = {
				faith = scope:owner.faith
				faith = {
					religious_head ?= {
			 			this = scope:target
			 		}
				}
			}
		}

		#Taboo
		modifier = { #Incest is taboo (norms & consequences)
			add = -25
			desc = SCHEME_SEDUCE_TABOO_INCEST
			scope:target = {
				relation_with_character_is_incestuous_in_my_or_lieges_faith_trigger = { CHARACTER = scope:owner }
			}
			scope:owner = {
				# The Subtle Desire Perk nullifies this
				NOT = { has_perk = subtle_desire_perk }
				# being already a lover with the target nulifies it
				NOT = { has_relation_lover = scope:target }
			}
		}
		modifier = { #Character is not willing to engage in incest with scope:owner
			add = {
				if = {
					limit = { scope:target = { is_close_family_of = scope:owner } }
					value = -40
				}
				else_if = {
					limit = { scope:target = { is_extended_family_of = scope:owner } }
					value = -15
				}
				if = {
					limit = { scope:target = { is_deviant_trigger = yes } }
					add = 15
				}
			}
			desc = SCHEME_SEDUCE_UNWILLING_INCEST
			scope:target = {
				NOT = {
					is_consort_of = scope:owner
				}
				is_close_or_extended_family_of = scope:owner
				NOR = {
					accepts_incest_with_trigger = { CHARACTER = scope:owner } #100% fine with incest with owner
					scope:owner = { has_perk = subtle_desire_perk }  # The Subtle Desire Perk nullifies this
					guaranteed_under_20_incest_rejection_trigger = { # handled by hard block modifier
						TARGET = scope:target
						SEDUCER = scope:owner
					}
				}
			}
		}
		modifier = { #Sodomy
			add = -20
			desc = SCHEME_SEDUCE_TABOO_SODOMY
			scope:target = {
				relation_with_character_is_sodomy_in_my_or_lieges_faith_trigger = { CHARACTER = scope:owner }
				NOT = { freely_accepts_sodomy_with_trigger = { CHARACTER = scope:owner } }
			}
		}
		
		modifier = { # Tradition
			add = 40
			desc = SCHEME_TRAIT_POET
			scope:owner = {
				has_trait = lifestyle_poet
				culture = {
					has_cultural_parameter = poet_trait_romance_bonuses
				}
			}
		}

		# Courtship Perk
		modifier = {
			add = courtship_court_success_chance_bonus_value
			desc = SCHEME_COURTSHIP_PERK
			scope:owner = {
				has_perk = courtship_perk
			}
		}

		# Dynasty Kin Personal Scheme Success Chance on Dynasty Perk
		modifier = {
			add = kin_legacy_4_success_chance
			desc = KIN_LEGACY_DESC
			scope:owner.dynasty ?= {
				has_dynasty_perk = kin_legacy_4
			}
			scope:target.dynasty = scope:owner.dynasty
		}

		# House Personal Scheme Success Chance on Cultural Parameter
		modifier = {
			add = cultural_house_personal_scheme_success_chance
			desc = KIN_PARAMETER_DESC
			exists = scope:owner.house
			exists = scope:target.house
			scope:owner.culture = {
				has_cultural_parameter = cultural_house_personal_scheme_success_chance
			}
			scope:target.house = scope:owner.house
		}

		# Spouse Scheme Success Chance on Cultural Parameter
		modifier = {
			add = 30
			desc = SPOUSE_PARAMETER_DESC
			scope:owner = {
				is_spouse_of = scope:target
			}
			scope:owner.culture = {
				has_cultural_parameter = romance_scheme_bonus_vs_spouse
			}
		}

		#Family Feud
		house_feud_sway_scheme_success_modifier = yes

		# Modifiers
		modifier = {
			scope:owner = { has_character_modifier = poet_for_diplo_schemes_modifier }
			desc = poet_for_diplo_schemes_modifier
			add = 15
		}
		
		# Personal Scheme Shared
		personal_scheme_base_success_chance_modifiers = yes

		modifier = {
			trigger = {
				scope:target = {
					is_married = yes
					NOT = { is_consort_of = scope:owner }
					exists = primary_spouse
					NOT = { might_cheat_on_partner_trigger = { PARTNER = scope:target.primary_spouse } }
				}
			}
			# Branching logic for opinion checks, to save performance
			add = {
				value = 0
				scope:target = {
					if = { # Hard block for AI seducers, they don't need the granularity and opinion checks are expensive performance-wise. 
						limit = { scope:owner = { is_ai = yes } }
						add = -1000
					}
					else_if = { # Players get scaling penalties depending on how high their target's opinion is of their primary spouse.
						limit = { has_relation_soulmate = scope:target.primary_spouse }
						add = -500
					}
					else_if = {
						limit = {
							opinion = {
								target = scope:target.primary_spouse
								value >= high_positive_opinion
							}
						}
						if = {
							limit = {
								opinion = {
									target = scope:target.primary_spouse
									value < very_high_positive_opinion
								}
							}
							# Opinion between 80 and 100
							add = -100
						}
						else = {
							# Opinion between 60 and 80
							add = -75
						}
					}
					else = {
						if = {
							limit = {
								opinion = {
									target = scope:target.primary_spouse
									value >= medium_positive_opinion
								}
							}
							# Opinion between 40 and 60
							add = -50
						}
						else = {
							# Opinion between 0 and 40 (technically -100 and 40 but at below 0 we shouldn't be in this trigger block at all since 'might_cheat_on_partner_trigger' will have passed).
							add = -25
						}
					}
				}
			}
			desc = "SCHEME_SEDUCE_SPOUSE_OPINION_PENALTY"
		}
		
		# Same Location Bonus
		modifier = {
			add = 10
			desc = SAME_LOCATION_BONUS_DESC
			exists = global_var:travel_the_distance_is_loaded
			scope:owner = { is_in_the_same_court_as = scope:target }
		}

		# Distance Penalty
		ttd_major_distance_pentalty_modifier = yes
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:opportunistic
		}
		scheme_owner = {
			trigger_event = court_ongoing.0501
		}
	}
	on_phase_completed = {
		# Grab our scopes.
		save_scope_as = scheme
		scheme_target_character = { save_scope_as = target }
		scheme_owner = { save_scope_as = owner }
		scope:scheme = {
			# Set up a block for the inappropriate cheating error?
			save_scope_value_as = {
				name = ignore_cheating_error_check
				value = yes
			}
			# Proceed to rolls.
			scheme_owner = {
				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				#CONTINUE WITH OUTCOME ON_ACTION FOR AI TARGETS
				if = {
					limit = { scope:target = { is_ai = yes } }

					trigger_event = {
						on_action = court_outcome
					}
				}
				#FOR PLAYER CHARACTER, SEND CHOICE EVENT
				else = {
					scope:target = {
						trigger_event = {
							id = court_outcome.0003
						}
					}
				}
			}
		}
	}
	on_monthly = {
		save_scope_as = scheme
		scheme_owner = { save_scope_as = owner }
		scheme_target_character = { save_scope_as = target }

		# Sending an ongoing event
		if = {
			limit = {
				scheme_owner = { is_available = yes }
				scheme_target_character = { is_available = yes }
				player_target_available_for_personal_scheme_ongoing_events_trigger = {
					OWNER = scope:owner
					TARGET = scope:target
				}
				OR = {
					#Milestone 1 event
					AND = {
						scope:scheme = {
							scheme_progress >= scheme_progress_33
						}
						NOT = { exists = var:court_milestone_event }
					}
					#Milestone 2 event
					AND = {
						scope:scheme = {
							scheme_progress >= scheme_progress_50
						}
						OR = {
							NOT = { exists = var:court_milestone_event }
							var:court_milestone_event < 2
						}
					}
					#Milestone 3 event
					AND = {
						scope:scheme = {
							scheme_progress >= scheme_progress_66
						}
						OR = {
							NOT = { exists = var:court_milestone_event }
							var:court_milestone_event < 3
						}
					}
				}
			}
			#Set a var so we can track which milestone you've had
			if = {
				limit = { NOT = { exists = var:court_milestone_event } }
				set_variable = {
					name = court_milestone_event
					value = 1
				}
			}
			else = {
				change_variable = {
					name = court_milestone_event
					add = 1
				}
			}
			#Trigger event
			scheme_owner = {
				trigger_event = { on_action = court_ongoing }
			}
		}
	}
	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}
		if = {
			limit = {
				scope:target = { is_alive = no }
			}
			scope:owner = {
				send_interface_toast = {
					title = court_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_died
					}
				}
			}
		}
		if = {
			limit = {
				scope:target = { is_imprisoned = yes }
			}
			scope:target.imprisoner = {
				save_scope_as = other_imprisoner
			}
			scope:owner = {
				send_interface_toast = {
					title = court_scheme_invalidated_title
					left_icon = scope:target
					right_icon = scope:other_imprisoner
					custom_description_no_bullet = {
						subject = scope:other_imprisoner
						object = scope:target
						text = scheme_target_imprisoned_by_other
					}
				}
			}
		}
		
		if = {
			limit = {
				OR = {
					#We're already soulmates
					scope:owner = {
						has_relation_soulmate = scope:target
					}

					#You've been kicked out of bed
					scope:owner = {
						has_character_modifier = rejected_from_marriage_bed_modifier
						is_spouse_of = scope:target 
					}
						
					scope:owner = {  has_trait = celibate  }
					scope:target = {  has_trait = celibate }
					
					scope:owner = { is_imprisoned = yes }
					scope:target = { is_imprisoned = yes }

					scope:target = {
						NOR = {
							exists = location
							in_diplomatic_range = scope:owner
						}
					}
				}
			}
			scheme_owner = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = courting
					left_icon = scope:target
					custom_tooltip = courting_invalid
				}
			}
		}
	}
}

#DESIGN NOTES
#Taboo romance schemes: pairings which are considered incestuous/sodomy are allowed and there is no secrecy. However, some events should be blocked or texts altered as to avoid public displays of affection for taboo pairings. Anything in private, or anything that can be seen as being honorable or polite, is fine for everyone.
