﻿ttd_check_residence_and_save_as_temporary_scope_effect = {
	$CHARACTER$ = {
		if = { # Get characters's permanent location if possible
			limit = { is_landed = yes }
			capital_province = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		else_if = {
			limit = { is_courtier = yes }
			liege = { capital_province = { save_temporary_scope_as = $SCOPE_NAME$ } }
		}
		else_if = {
			limit = { is_pool_guest = yes }
			host = { capital_province = { save_temporary_scope_as = $SCOPE_NAME$ } }
		}
		else_if = {
			limit = {
				is_landed = no
				is_courtier = no
				is_pool_guest = no
			}
			location = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
	}
}

ttd_cancel_far_off_realm_travel_clean_up_effect = {
	if = {
		limit = { has_character_flag = ttd_nearby_realm }
		remove_character_flag = ttd_nearby_realm
	}
	if = {
		limit = { has_variable = ttd_far_off_target }
		remove_variable = ttd_far_off_target
	}
	remove_variable = ttd_far_off_location
	if = {
		limit = { has_variable = ttd_far_off_host }
		remove_variable = ttd_far_off_host
	}
}

ttd_learn_foreign_tactic_effect = {
	random_list = {
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_forder_tirgger = { LOCATION = $LOCATION$ } }
			add_trait = forder
		}
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_forest_fighter_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ } }
			modifier = {
				$HOST_CULTURE$ = { has_cultural_parameter = forest_fighter_trait_more_common }
				add = 1
			}
			add_trait = forest_fighter
		}
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_open_terrain_expert_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ } }
			add_trait = open_terrain_expert
		}
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_rough_terrain_expert_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ } }
			modifier = {
				$HOST_CULTURE$ = { has_cultural_parameter = rough_terrain_expert_trait_more_common }
				add = 1
			}
			add_trait = rough_terrain_expert
		}
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_desert_warrior_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ } }
			modifier = {
				$HOST_CULTURE$ = { has_cultural_parameter = desert_warrior_trait_more_common }
				add = 1
			}
			add_trait = desert_warrior
		}
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_jungle_stalker_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ } }
			modifier = {
				$HOST_CULTURE$ = { has_cultural_parameter = jungle_stalker_trait_more_common }
				add = 1
			}
			add_trait = jungle_stalker
		}
		1 = {
			trigger = { ttd_can_learn_foreign_tactic_winter_soldier_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ } }
			modifier = {
				$LOCATION$ = { has_province_modifier = winter_harsh_modifier }
				add = 1
			}
			modifier = {
				$HOST_CULTURE$ = { has_cultural_parameter = winter_soldier_trait_more_common }
				add = 1
			}
			add_trait = winter_soldier
		}
	}
}

tdd_seduce_local_night_effect = {
	# Warn the character about possible negative consequences.
	custom_tooltip = stress_threshold.1011.rakish.tt

	# Negative consequences of the night.
	random_list = {
		50 = {
			trigger = {
				is_attracted_to_men = yes
			}
			set_variable = {
				name = had_recent_sex
				value = flag:male
			}
			give_homosexual_secret_or_nothing_with_target_effect = { CHARACTER = dummy_male }
		}
		50 = {
			trigger = {
				is_attracted_to_women = yes
			}
			set_variable = {
				name = had_recent_sex
				value = flag:female
			}
		}
	}
	hidden_effect = {
		#Adultery suspicion
		random = {
			chance = 5
			random_spouse = {
				limit = { is_ai = no }
				alternative_limit = { always = yes }
				trigger_event = adultery.0001
			}
		}

		# Possible STD transmission
		random_list = {
			89 = {
				#Nothing
			}
			10 = {
				contract_disease_notify_effect = { DISEASE = lovers_pox }
			}
			1 = {
				contract_disease_notify_effect = { DISEASE = great_pox }
			}
		}

		#Possibility to start peasant story cycle
		random = {
			chance = 5
			if = {
				limit = {
					NOT = { has_character_flag = had_story_cycle_peasant_affair }
					is_ruler = yes
				}
				create_character = {
					location = var:ttd_far_off_location
					template = beautiful_peasant_character
					gender_female_chance = root_attraction_based_female_chance
					faith = var:ttd_far_off_location.county.faith
					culture = var:ttd_far_off_location.county.culture
					save_scope_as = beautiful_peasant
				}
				scope:beautiful_peasant = {
					set_sexuality = bisexual
				}
				if = {
					limit = {
						is_male = yes
						is_eunuch_trigger = no
						exists = scope:beautiful_peasant
						scope:beautiful_peasant = {
							is_female = yes
							age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
						}
					}
					random = {
						chance = pregnancy_chance
						add_character_flag = {
							flag = peasant_is_pregnant
							days = 560
						}
					}
				}
				set_variable = {
					name = story_peasant_affair_target
					value = scope:beautiful_peasant
				}
				create_story = story_peasant_affair
			}
		}
	}

	add_stress = major_stress_impact_loss
}

ttd_on_send_marriage_effect = {
	if = {
		limit = { # Marriage, not betrothal
			scope:grand_wedding_promise = no
			scope:secondary_actor = {
				is_adult = yes
				NOT = { is_in_the_same_court_as = scope:secondary_recipient }
			}
			scope:secondary_recipient = { is_adult = yes }
		}
		if = {
			limit = {
				scope:actor = { is_landed = yes }
				scope:secondary_actor = {
					OR = {
						this = scope:actor
						trigger_if = { # Dominant partner in the marriage
							limit = {
								is_female = yes
								OR = {
									sex_opposite_of = scope:secondary_recipient
									exists = global_var:ao_adoption_options_is_loaded
									exists = global_var:cc_celebrate_crimes_is_loaded
								}
							}
							scope:matrilineal = yes
						}
						trigger_else_if = {
							limit = {
								is_male = yes
								OR = {
									sex_opposite_of = scope:secondary_recipient
									exists = global_var:ao_adoption_options_is_loaded
									exists = global_var:cc_celebrate_crimes_is_loaded
								}
							}
							scope:matrilineal = no
						}
						trigger_else = { # Cannot predict same-sex dominance reliably otherwise
							always = no
						}
					}
				}
				scope:secondary_recipient = {
					is_ruler = no
					is_available = yes
					NOR = { # Not already there
						is_courtier_of = scope:actor
						location ?= scope:actor.capital_province
					}
				}
			}
			scope:secondary_recipient = {
				set_variable = {
					name = ttd_start_location
					value = location
					days = 90
				}
			}
		}
		else_if = {
			limit = {
				scope:recipient = { is_landed = yes }
				scope:secondary_recipient = {
					OR = {
						this = scope:recipient
						OR = {
							this = scope:actor
							trigger_if = { # Dominant partner in the marriage
								limit = {
									is_female = yes
									sex_opposite_of = scope:secondary_actor
								}
								scope:matrilineal = yes
							}
							trigger_else_if = {
								limit = {
									is_male = yes
									sex_opposite_of = scope:secondary_actor
								}
								scope:matrilineal = no
							}
							trigger_else_if = {
								limit = {
									is_female = yes
									OR = { # Same-sex by default
										exists = global_var:ao_adoption_options_is_loaded
										exists = global_var:cc_celebrate_crimes_is_loaded
									}
								}
								scope:matrilineal = no # Flipped from this prespective
							}
							trigger_else_if = {
								limit = {
									is_male = yes
									OR = { # Same-sex by default
										exists = global_var:ao_adoption_options_is_loaded
										exists = global_var:cc_celebrate_crimes_is_loaded
									}
								}
								scope:matrilineal = yes # Flipped from this prespective
							}
							trigger_else = { # Cannot predict same-sex dominance reliably otherwise
								always = no
							}
						}
					}
				}
				scope:secondary_actor = {
					is_ruler = no
					is_available = yes
					NOR = { # Not already there
						is_courtier_of = scope:recipient
						location ?= scope:recipient.capital_province
					}
				}
			}
			scope:secondary_actor = {
				set_variable = {
					name = ttd_start_location
					value = location
					days = 90
				}
			}
		}
	}
}

ttd_on_accept_marriage_effect = {
	if = { # If in far off realm just add them to current travel plan
		limit = {
			scope:actor = {
				var:ttd_far_off_host ?= {
					OR = {
						this = scope:recipient
						this = scope:secondary_recipient.host # To handle guests
					}
				}
				current_travel_plan ?= { travel_plan_owner = scope:actor }
			}
			scope:secondary_actor = {
				is_adult = yes
				OR = {
					this = scope:actor
					is_courtier_of = scope:actor
				}
				OR = { # Is the dominant partner
					AND = {
						is_male = yes
						scope:matrilineal = no
					}
					AND = {
						is_female = yes
						scope:matrilineal = yes
					}
				}
			}
			scope:secondary_recipient = {
				is_adult = yes
				is_ruler = no
				is_available = yes
			}
			scope:grand_wedding_promise = no
		}
		scope:actor = {
			current_travel_plan = {
				add_companion = scope:secondary_recipient
			}
		}
		ttd_marriage_proposal_clean_up_effect = yes
	}
	else_if = {
		limit = {
			scope:secondary_actor = {
				has_variable = ttd_start_location
				is_ruler = no # Make sure this is still true
				is_available = yes # Make sure this is still true
			}
			scope:secondary_recipient = { # Only bother for non-baron landed rulers or any landed court if a player is involved
				OR = {
					AND = {
						is_landed = yes
						highest_held_title_tier > tier_barony
					}
					AND = {
						OR = {
							is_courtier = yes
							is_landed = yes
						}
						OR = {
							scope:actor = { is_ai = no }
							scope:recipient = { is_ai = no }
						}
						liege ?= { is_landed = yes }
					}
				}
			}
		}
		if = {
			limit = { scope:secondary_recipient = { is_landed = yes } }
			ttd_newlywed_depart_for_spouses_hosts_court_effect = { NEWLYWED = scope:secondary_actor SPOUSE = scope:secondary_recipient HOST = scope:secondary_recipient }
		}
		else = {
			ttd_newlywed_depart_for_spouses_hosts_court_effect = { NEWLYWED = scope:secondary_actor SPOUSE = scope:secondary_recipient HOST = scope:secondary_recipient.liege }
		}
	}
	else_if = {
		limit = {
			scope:secondary_recipient = {
				has_variable = ttd_start_location
				is_ruler = no # Make sure this is still true
				is_available = yes # Make sure this is still true
			}
			scope:secondary_actor = { # Only bother for non-baron landed rulers or any landed court if a player is involved
				OR = {
					AND = {
						is_landed = yes
						highest_held_title_tier > tier_barony
					}
					AND = {
						OR = {
							is_courtier = yes
							is_landed = yes
						}
						OR = {
							scope:actor = { is_ai = no }
							scope:recipient = { is_ai = no }
						}
						liege ?= { is_landed = yes }
					}
				}
			}
		}
		if = {
			limit = { scope:secondary_actor = { is_landed = yes } }
			ttd_newlywed_depart_for_spouses_hosts_court_effect = { NEWLYWED = scope:secondary_recipient SPOUSE = scope:secondary_actor HOST = scope:secondary_actor }
		}
		else = {
			ttd_newlywed_depart_for_spouses_hosts_court_effect = { NEWLYWED = scope:secondary_recipient SPOUSE = scope:secondary_actor HOST = scope:secondary_actor.liege }
		}
	}
	else = {
		ttd_marriage_proposal_clean_up_effect = yes
	}
}

ttd_marriage_proposal_clean_up_effect = {
	if = {
		limit = { scope:secondary_actor = { has_variable = ttd_start_location } }
		scope:secondary_actor = { remove_variable = ttd_start_location }
	}
	else_if = {
		limit = { scope:secondary_recipient = { has_variable = ttd_start_location } }
		scope:secondary_recipient = { remove_variable = ttd_start_location }
	}
}

ttd_newlywed_depart_for_spouses_hosts_court_effect = {
	$NEWLYWED$ = {
		save_scope_as = ttd_newlywed # Used for message in on action
		$SPOUSE$ = { save_scope_as = ttd_spouse } # Used for message in on action
		$HOST$ = { save_scope_as = ttd_host } # Used for message and variable in on action
		move_to_pool_at = var:ttd_start_location
		start_travel_plan = {
			destination = $HOST$.capital_province
			on_start_on_action = on_ttd_newlywed_depart_for_hosts_court
			on_travel_planner_cancel_on_action = on_ttd_newlywed_cancels_departure_for_hosts_court
			on_arrival_on_action = on_ttd_newlywed_arrive_at_hosts_court
			on_arrival_destinations = last
			return_trip = no # One way
		}
	}
}

ttd_newlywed_travel_to_hosts_court_clean_up_effect = {
	if = {
		limit = { has_variable  = ttd_newlywed_traveling_to_court_of }
		if = {
			limit = {
				NAND = {
					exists = var:ttd_newlywed_traveling_to_court_of.capital_province
					any_spouse = {
						OR = {
							this = prev.var:ttd_newlywed_traveling_to_court_of
							this.liege ?= prev.var:ttd_newlywed_traveling_to_court_of
						}
					}
					location ?= var:ttd_newlywed_traveling_to_court_of.capital_province
				}
			}
			set_location_to_default = yes
		}
		remove_variable = ttd_newlywed_traveling_to_court_of
	}
}