﻿subsume_province = {
	# Basic Setup
	skill = diplomacy
	desc = "subsume_province_desc_general"
	success_desc = "SUBSUME_PROVINCE_SUCCESS_DESC"
	icon = icon_subsume_province
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = political
	target_type = title
	cooldown = { years = 10 }

	# Parameters
	speed_per_skill_point = t3_spsp_owner_value
	speed_per_target_skill_point = t3_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 = 365
	base_maximum_success = t1_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 = {
			holder ?= {
				this != scope:owner
				is_alive = yes
				government_allows = administrative
				top_liege != this
				top_liege = scope:owner.top_liege
				is_governor = yes
				in_diplomatic_range = scope:owner
				custom_tooltip = {
					text = subsume_province_neighboring_duchy_tt
					any_sub_realm_county = {
						any_neighboring_county = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
					}
				}
				custom_tooltip = {
					text = subsume_province_existing_target_tt
					NOT = {
						is_scheming_against = {
							type = subsume_province
							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_governor = yes
			is_in_civil_war = no
		}
		scope:target = {
			holder ?= {
				trigger_if = {
					limit = { exists = scope:scheme.var:target_character}
					this = scope:scheme.var:target_character
				}
				is_alive = yes
				is_governor = yes
				house != scope:owner.house
				NOT = { is_at_war_with = scope:owner }
				custom_tooltip = {
					text = subsume_province_existing_target_tt
					NOT = {
						is_scheming_against = {
							type = subsume_province
							target = scope:owner
						}
					}
				}
				is_in_civil_war = no
			}
		}
		scope:target = {
			ep3_subsume_province_duchy_trigger = {
				OWNER = scope:owner
				TARGET = scope:target.holder
			}
		}
		scope:owner = {
			NOT = { is_diarch_of_target = liege }
		}
	}

	# 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
			scope:owner.house != scope:target.holder.house
			desc = TARGET_IS_MY_HOUSE_MEMBER
		}
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_owner_perspective = { peer_vassals vassals councillors courtiers scripted_relations }

	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_subsume_province_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
		# Land Context
		modifier = { # Owner's counties outside primary title
			scope:owner = {
				any_sub_realm_county = {
					NOT = { target_is_de_facto_liege_or_above = scope:owner.primary_title }
				}
			}
			desc = SUBSUME_PROVINCE_HELD_PROVINCES_DESC
			add = {
				value = 0
				scope:owner = {
					every_sub_realm_county = {
						limit = {
							NOT = { target_is_de_facto_liege_or_above = scope:owner.primary_title }
						}
						add = -1
					}
				}
			}
		}
		modifier = { # Target's capital duchy is only target
			scope:target.holder ?= {
				NOT = {
					any_sub_realm_county = {
						any_neighboring_county = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
						duchy != scope:target.holder.primary_title.title_capital_county.duchy
					}
				}
			}
			desc = SUBSUME_PROVINCE_ONLY_CAPITAL_DESC
			add = -25
		}
		modifier = { # Held counties in subsume theme
			scope:target ?= {
				any_de_jure_county = {
					county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
				}
			}
			desc = SUBSUME_PROVINCE_HELD_COUNTIES_IN_TARGET_DESC
			add = {
				value = 0
				scope:target = { #Unop ck3-target
					every_de_jure_county = {
						limit = {
							county_held_or_vassal_to_target_trigger = { TARGET = scope:owner }
						}
						add = 5
					}
				}
			}
		}
		modifier = { # Total House land
			scope:owner.house ?= {
				any_house_member = {
					top_liege ?= scope:owner.top_liege
					is_governor = yes
				}
			}
			desc = SUBSUME_GOVERNORSHIP_OWNER_HOUSE_GOVERNORSHIPS
			add = {
				add = 0
				scope:owner.house = {
					every_house_member = {
						limit = {
							top_liege ?= scope:owner.top_liege
							is_governor = yes
						}
						every_sub_realm_county = { add = -0.5 }
					}
				}
			}
		}
		# 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
			}
		}
		# Influence
		modifier = {
			desc = OWNER_GOVERNOR_EFFICIENCY_DESC
			add = {
				value = scope:owner.governor_efficiency_presented
				divide = 4
			}
		}
		modifier = {
			exists = scope:target
			desc = TARGET_HOLDER_GOVERNOR_EFFICIENCY_DESC
			add = {
				value = scope:target.holder.governor_efficiency_presented
				multiply = -1
				divide = 4
			}
		}
		# 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
		}
		modifier = {
			scope:owner = {
				has_character_modifier = united_political_agents_modifier
				is_vassal_of = scope:target.holder #Unop ck3-tiger
			}
			desc = united_political_agents_modifier
			add = 15
		}
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:indirect
		}
		# 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_proponent
			add_agent_slot = agent_political_socialite
			add_agent_slot = agent_bureaucrat
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		subsume_province_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { subsume_province_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			subsume_province_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 = subsume_province_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 = { # Diplo range: is it relevant here?
					limit = {
						scope:target = { 
							NOT = { in_diplomatic_range = scope:owner } 
						}
					}
					custom_tooltip = scheme_target_not_in_diplomatic_range
				}
				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
				}
			}
		}
	}
}
