﻿dispute_border = {
	# Basic Setup
	skill = diplomacy
	desc = "dispute_border_desc_general"
	success_desc = "DISPUTE_BORDER_SUCCESS_DESC"
	icon = icon_dispute_border
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = political
	target_type = title

	# Parameters
	speed_per_skill_point = t2_spsp_owner_value
	speed_per_target_skill_point = t2_spsp_target_value
	spymaster_speed_per_skill_point = t2_spsp_owner_spy_value
	target_spymaster_speed_per_skill_point = 0
	tier_speed = 1
	base_progress_goal = t3_base_phase_length_value
	base_maximum_success = t2_base_max_success_value
	phases_per_agent_charge = 1
	success_chance_growth_per_skill_point = t3_scgpsp_value

	is_secret = no

	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no
		NOT = { has_trait = incapable }
		is_governor = yes
		scope:target = {
			custom_tooltip = {
				text = dispute_border_county_neighbour_tt
				NOT = {
					county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
				}
				OR = {
					any_neighboring_county = {
						county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
					}
					AND = {
						de_jure_liege.holder ?= scope:owner
						any_title_to_title_neighboring_and_across_water_county = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
					}
				}
			}
			holder = {
				this != scope:owner
				is_alive = yes
				government_allows = administrative
				top_liege != this
				top_liege = scope:owner.top_liege
				in_diplomatic_range = scope:owner
				
				custom_tooltip = {
					text = dispute_border_existing_target_tt
					NOT = {
						is_scheming_against = {
							type = dispute_border
							target = scope:owner
						}
					}
				}
			}
		}
		custom_tooltip = {
			text = admin_vassal_border_limit_desc
			top_liege ?= {
				NOT = { has_realm_law_flag = admin_vassal_border_limit }
			}
		}
	}

	valid = {
		scope:owner = {
			is_imprisoned = no
			NOT = { has_trait = incapable }
			is_governor = yes
			is_in_civil_war = no
		}
		scope:target = {
			custom_tooltip = {
				text = dispute_border_county_neighbour_tt
				NOT = {
					county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
				}
				OR = {
					any_neighboring_county = {
						county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
					}
					AND = {
						de_jure_liege.holder ?= scope:owner
						any_title_to_title_neighboring_and_across_water_county = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
					}
				}
			}
			holder ?= { is_in_civil_war = no }
		}
	}

	# Agents
	agent_leave_threshold = -25
	agent_join_chance = {
		base = 0
		ai_agent_join_chance_basic_suite_modifier = yes
		# House
		modifier = {
			add = -50
			scope:recipient.house ?= scope:target.holder.house
			desc = TARGET_IS_MY_HOUSE_MEMBER
		}
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_owner_perspective = { courtiers peer_vassals vassals councillors }

	odds_prediction = {
		add = hostile_scheme_base_odds_prediction_target_is_title_value
		add = odds_skill_contribution_diplomacy_title_value
		add = agent_groups_owner_perspective_value
		add = agent_groups_target_character_perspective_value
		add = odds_dispute_border_scheme_misc_value
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = 0

		# Skill
		compare_modifier = {
			desc = "SCHEME_MY_DIPLOMACY"
			value = scope:owner.diplomacy
			multiplier = 2
		}
		compare_modifier = {
			desc = "SCHEME_TARGET_HOLDER_DIPLOMACY"
			value = scope:target.holder.diplomacy
			multiplier = -2
		}
		# Spiritual Head of Faith
		modifier = {
			add = -30
			desc = "SCHEME_VS_SPIRITUAL_HOF"
			scope:target.holder.faith ?= {
				this = scope:owner.faith
				has_doctrine = doctrine_spiritual_head
				religious_head ?= scope:target.holder
			}
		}
		# Sponsored Inspiration
		modifier = {
			add = -15
			desc = target_has_sponsored_inspiration
			scope:target.holder ?= {
				inspiration ?= { exists = inspiration_sponsor }
			}
		}
		# Family Feud
		house_feud_title_hostile_scheme_success_modifier = yes #Unop: Use this in-house modifier: The original modifier expects character target, but this scheme has title target
		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_title_within_realm_bonus_modifier = yes #Unop: Use the correct modifier (was diarch_scheming_within_realm_bonus_modifier)
		# Modifiers
		modifier = {
			scope:owner = {
				has_character_modifier = united_political_agents_modifier
				is_vassal_of = scope:target.holder #Unop: Target is a title here
			}
			desc = united_political_agents_modifier
			add = 15
		}
		# Land Context
		modifier = {
			scope:target.duchy.holder ?= scope:owner
			desc = DISPUTE_BORDERS_DE_JURE_COUNTY_DESC
			add = 25
		}
		modifier = {
			OR = {
				scope:target.duchy ?= scope:target.holder.primary_title
				scope:target.duchy.de_jure_liege ?= scope:target.holder.primary_title
			}
			desc = DISPUTE_BORDERS_DE_JURE_RATIO_DESC
			add = -25
		}
		modifier = {
			scope:target.holder ?= {
				highest_held_title_tier >= tier_duchy
				NOT = {
					any_sub_realm_county = {
						this != scope:target.holder.capital_county
						any_neighboring_county = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
					}
				}
			}
			desc = DISPUTE_BORDERS_ONLY_CAPITAL_DESC
			add = -25
		}
		# House
		modifier = {
			scope:owner.house ?= {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = OWNER_POWERFUL_FAMILY_DESC
			add = 10
		}
		modifier = {
			scope:target.holder.house ?= {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = TARGET_HOLDER_POWERFUL_FAMILY_DESC
			add = -10
		}
		modifier = {
			scope:owner.house = { is_dominant_family = yes }
			desc = OWNER_DOMINANT_FAMILY_DESC
			add = 20
		}
		modifier = {
			scope:target.holder.house ?= { is_dominant_family = yes }
			desc = TARGET_HOLDER_DOMINANT_FAMILY_DESC
			add = -20
		}
		modifier = {
			scope:target.holder.house = scope:owner.top_liege.house
			desc = TARGET_HOLDER_EMPEROR_HOUSE_DESC
			add = -10
		}
		# Influence
		modifier = {
			desc = OWNER_INFLUENCE_LEVEL_DESC
			add = {
				value = scope:owner.influence_level
				multiply = 5
			}
		}
		modifier = {
			desc = TARGET_HOLDER_INFLUENCE_LEVEL_DESC
			add = {
				value = scope:target.holder.influence_level
				multiply = -5
			}
		}
		# Favour
		opinion_modifier = {
			who = scope:owner.top_liege
			opinion_target = scope:owner
			multiplier = 0.2
		}
		opinion_modifier = {
			who = scope:owner.top_liege
			opinion_target = scope:target.holder
			multiplier = -0.2
		}
		# house_head_request_interaction
		modifier = {
			add = political_scheme_variable_list_value
			scope:owner = {
				has_variable_list = supporting_political_schemes
			}
			desc = HOUSE_HEAD_SCHEME_SUPPORT_DESC
		}
		modifier = {
			add = -10
			scope:owner = { has_character_modifier = political_schemes_distracted_modifier }
			desc = political_schemes_distracted_modifier
		}
	}

	# On Actions
	## Add agents
	on_start = {
		# Fallback agents — a balanced config if you start the scheme from script.
		if = {
			limit = {
				scheme_owner = {
					NOT = { has_variable = agents_added }
				}
			}
			add_agent_slot = agent_intermediary
			add_agent_slot = agent_proponent
			add_agent_slot = agent_sycophant
			add_agent_slot = agent_political_socialite
			add_agent_slot = agent_bureaucrat
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		dispute_borders_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { dispute_borders_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			dispute_borders_scheme_prep_effect = yes
			purge_ai_scheme_slots_effect = yes
		}
	}
	on_monthly = { fire_generic_contract_scheme_on_going_events_effect = yes }

	## Invalidation
	on_invalidated = {
		scheme_target_title.holder = { save_scope_as = target }
		scheme_owner = {
			save_scope_as = owner
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = dispute_border_invalidated_title
				left_icon = scope:target
				right_icon = scope:target_title
				if = { # Invalidation due to death
					limit = {
						scope:target = { is_alive = no }
						NOT = { block_death_event_trigger = { DEAD = scope:target } }
					}
					custom_tooltip = scheme_target_died
				}
				else_if = { # No more admin
					limit = {
						scope:target = {
							NOT = { government_allows = administrative }
						}
					}
					custom_tooltip = scheme_target_not_admin
				}
				else_if = { # No more admin
					limit = {
						scope:owner = {
							NOT = { government_allows = administrative }
						}
					}
					custom_tooltip = scheme_owner_not_admin
				}
				else_if = { # Not neighbors
					limit = {
						scope:target = {
							NOT = {
								any_sub_realm_county = {
									any_neighboring_county = {
										county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
									}
								}
							}
						}
					}
					custom_tooltip = scheme_target_not_neighboring_governor
				}
				else_if = { # Realm too small
					limit = {
						scope:target = {
							NOT = {
								any_sub_realm_county = { count >= 2 }
							}
						}
					}
					custom_tooltip = scheme_target_realm_size_too_small
				}
			}
		}
	}
}
