﻿elope = {
	# Basic Setup
	skill = intrigue
	desc = elope_desc_general
	success_desc = "ELOPE_SUCCESS_DESC"
	icon = icon_scheme_elope
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = personal
	target_type = character
	is_secret = yes
	maximum_breaches = 5
	is_basic = yes
	cooldown = { years = 10 }
	
	# Parameters
	speed_per_skill_point = -2
	speed_per_target_skill_point = 0
	base_progress_goal = 185
	maximum_secrecy = 95
	base_maximum_success = 95
	minimum_success = 5
	freeze_scheme_when_traveling = yes
	freeze_scheme_when_traveling_target = yes

	allow = {
		is_adult = yes
		is_imprisoned = no
		is_travelling = no
		is_in_army = no
		has_contagious_deadly_disease_trigger = no

		scope:target = {
			is_travelling = no
			is_in_army = no
			has_contagious_deadly_disease_trigger = no
			NOT = { has_variable = gone_adventuring }
		}
		NOT = { has_variable = gone_adventuring }
	}

	valid = {
		is_incapable = no
		scope:target = {
			is_adult = yes
			is_alive = yes
			is_imprisoned = no
			is_courtier = yes
			NOT = { is_courtier_of = scope:owner }
			can_elope_character_trigger = { CHARACTER = scope:owner }
			OR = {
				has_relation_lover = scope:owner
				scope:owner = {
					has_government = landless_adventurer_government
					has_perk = accomplished_forger_perk
				}
			}
			in_diplomatic_range = scope:owner
		}
		custom_description = {
			object = scope:target
			text = you_have_been_caught_trying_to_elope_with_target
			scope:target.liege = {
				NOT = {
					has_opinion_modifier = {
						target = scope:owner
						modifier = attempted_elopement_opinion
					}
				}
			}
		}
	}

	odds_prediction = {
		add = base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_intrigue_value
		min = 0
	}

	base_success_chance = {
		base = 50 #So there is some chance even if their intrigue is stronger than yours
		scheme_type_skill_success_chance_modifier = { SKILL = INTRIGUE }
		
		#Language
		modifier = {
			add = -10
			desc = YOU_DONT_SPEAK_THE_LANGUAGE
			scope:owner = {
				NOT = { knows_language_of_culture = scope:target.culture }
			}
		}

		# Countermeasures.
		apply_opportunistic_scheme_success_chance_adjustments_modifier = yes
		
		# Intrigue modifiers
		modifier = {
			add = {
				value = scope:owner.intrigue
				multiply = 2.5
			}
			desc = ELOPE_SUCCESS_OWNER_INTRIGUE
		}
		modifier = {
			add = {
				value = scope:target.intrigue
				multiply = 2.5
			}
			desc = ELOPE_SUCCESS_TARGET_INTRIGUE
		}
		modifier = {
			add = {
				value = scope:target.liege.intrigue
				multiply = -2.5
			}
			desc = ELOPE_SUCCESS_TARGETS_LIEGE_INTRIGUE
		}
		modifier = {
			exists = scope:target.liege.cp:councillor_spymaster
			NOR = {
				scope:target.liege.cp:councillor_spymaster = scope:target
				scope:target.liege.cp:councillor_spymaster = scope:owner
				scope:owner = {
					any_scheme = {
						scheme_target_character = scope:target
						scheme_is_character_agent = scope:target.liege.cp:councillor_spymaster
					}
				}
			}
			add = {
				value = scope:target.liege.cp:councillor_spymaster.intrigue
				multiply = -2.5
			}
			desc = ELOPE_SUCCESS_TARGETS_LIEGES_SPYMASTER_INTRIGUE
		}

		# Malus if important target
		modifier = {
			scope:target = {
				OR = {
					is_heir_of = scope:target.liege
					is_consort_of = scope:target.liege
					is_councillor_of = scope:target.liege
					is_knight_of = scope:target.liege
					is_close_family_of = scope:target.liege
					has_relation_lover = scope:target.liege
					has_relation_rival = scope:target.liege
					has_relation_friend = scope:target.liege
					has_relation_guardian = scope:target.liege
					has_relation_ward = scope:target.liege
				}
			}
			desc = ELOPE_SUCCESS_IMPORTANT
			add = -25
		}

		# Bonus if unimportant target
		modifier = {
			scope:target = {
				has_no_real_status_trigger = yes
				NOR = {
					is_knight_of = scope:target.liege
					is_of_major_interest_trigger = { CHARACTER = scope:target.liege }
					is_of_minor_interest_trigger = { CHARACTER = scope:target.liege }
				}
			}
			desc = ELOPE_SUCCESS_UNIMPORTANT
			add = 15
		}

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

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

		# Bonus Forger Perk
		modifier = {
			add = 50
			desc = SCHEME_FORGER_PERK
			scope:owner = {
				has_government = landless_adventurer_government
				has_perk = accomplished_forger_perk
			}
		}

		# NO Thicker Than Water Perk bonus as the "opponent" in this scheme is not target but target's liege
		
		# Penalty if the relationship is known
		modifier = {
			add = -20
			desc = ELOPE_SUCCESS_TARGETS_LIEGE_KNOWS_ABOUT_RELATIONSHIP
			scope:owner = {
				NAND = {
					has_government = landless_adventurer_government
					has_perk = accomplished_forger_perk
				}
				OR = {
					# There is no secret
					NOT = {
						any_secret = {	
							type = secret_lover
							secret_target = scope:target
						}
					}
					# Liege knows the secret
					any_secret = {
						type = secret_lover
						secret_target = scope:target
						is_known_by = scope:target.liege
					}
				}
			}
		}

		# Penalty if target is imprisoned
		modifier = {
			add = -30
			scope:target = { is_imprisoned = yes }
			desc = ELOPE_SUCCESS_IMPRISONED
		}

		# Penalty if target doesn't like you anymore
		opinion_modifier = {
			trigger = {
				scope:target = {
					opinion = {
						target = scope:owner
						value < 0
					}
				}
			}
			who = scope:target
			opinion_target = scope:owner
			multiplier = 3
		}

		#Family Feud
		house_feud_sway_scheme_success_modifier = yes

		# Personal Scheme Shared
		personal_scheme_base_success_chance_modifiers = yes
		
		# Distance Penalty
		ttd_minor_distance_pentalty_modifier = yes
	}
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
	}

	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:opportunistic
		}
	}
	on_phase_completed = {
		# Grab our scopes.
		save_scope_as = scheme
		scheme_target_character = { save_scope_as = target }
		scheme_owner = { save_scope_as = owner }
		scope:owner = {
			#Do I want to proceed to a roll?
			if = {
				limit = {
					is_ai = no
				}
				trigger_event = elope.0001
			}
			#Jump straight to roll
			else = {
				trigger_event = elope.0002
			}
		}
	}
	on_monthly = {
		hostile_scheme_monthly_discovery_chance_effect = yes
		if = {
			limit = {
				NOT = { exists = scope:discovery_event_happening }
			}			
			scheme_owner = {
				trigger_event = {
					on_action = elope_ongoing
					days = { 1 15 }
				}
			}
		}
	}
	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 = elope_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_died
					}
				}
			}
		}
		else_if = {
			limit = {
				scope:owner = {
					NAND = {
						has_government = landless_adventurer_government
						has_perk = accomplished_forger_perk
					}
				 	NOT = { has_relation_lover = scope:target }
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = elope_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_no_longer_lover
					}
				}
			}
		}

		if = {
			limit = {
				scope:target = { is_imprisoned = yes }
			}
			scope:target.imprisoner = {
				save_scope_as = other_imprisoner
			}
			scope:owner = {
				send_interface_toast = {
					title = elope_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 = {
				scope:target = {
					is_courtier = no
					exists = var:last_visited_ruler
				}
			}
			scope:target.var:last_visited_ruler = { save_scope_as = target_liege }
			scope:owner = {
				send_interface_toast = {
					title = elope_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						subject = scope:target_liege
						object = scope:target
						text = scheme_target_no_longer_courtier
					}
				}
			}
		}
		if = {
			limit = {
				scope:target = {
					is_courtier = no
					NOT = { exists = var:last_visited_ruler }
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = elope_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						subject = scope:target
						text = scheme_target_no_longer_courtier_no_var
					}
				}
			}
		}
		if = {
			limit = {
				scope:target = { is_courtier_of = scope:owner }
			}
			scope:owner = {
				send_interface_toast = {
					title = elope_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						subject = scope:target
						text = scheme_target_is_your_courtier
					}
				}
			}
		}
		if = {
			limit = {
				scope:target = { 
					NOT = { in_diplomatic_range = scope:owner } 
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = elope_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_not_in_diplomatic_range
					}
				}
			}
		}

		if = {
			limit = {
				scope:owner = {
					has_variable = waiting_to_elope
					NOT = {
						any_owned_story = {
							type = lmf_waiting_to_elope_story_cycle
							var:elope_target ?= scope:target
						}
					}
				}
			}
			scope:owner = {
				remove_variable = waiting_to_elope
				if = {
					limit = { has_relation_soulmate = scope:target }
					remove_relation_soulmate = scope:target
				}
			}
			scope:target = {
				if = {
					limit = {
						is_alive = yes
						has_variable = waiting_to_elope
					}
					remove_variable = waiting_to_elope
				}
			}
		}
	}
}

