﻿seize_realm = {
	# Basic Setup
	skill = intrigue
	desc = seize_realm_desc_general
	success_desc = "SEIZE_REALM_SUCCESS_DESC"
	discovery_desc = "SEIZE_REALM_DISCOVERY_DESC"
	icon = icon_scheme_seizure
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = hostile
	target_type = character
	is_secret = yes
	maximum_breaches = 5
	cooldown = { years = 10 }

	# Travel Triggers (default no)
	freeze_scheme_when_traveling = yes
	freeze_scheme_when_traveling_target = yes

	speed_per_skill_point = t3_spsp_owner_value
	speed_per_target_skill_point = t3_spsp_target_value
	spymaster_speed_per_skill_point = t3_spsp_owner_spy_value
	target_spymaster_speed_per_skill_point = t3_spsp_target_spy_value
	tier_speed = 1
	base_progress_goal = t3_base_phase_length_value
	maximum_secrecy = 95
	base_maximum_success = t2_base_max_success_value
	phases_per_agent_charge = 1
	success_chance_growth_per_skill_point = t3_scgpsp_value
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
		add = odds_seize_realm_scheme_misc_value
	}

	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no
		government_has_flag = government_is_landless_adventurer
		NOT = { is_at_war_with = scope:target }
		scope:target = {
			is_landed = yes
			is_independent_ruler = yes
			highest_held_title_tier >= tier_county
			custom_tooltip = {
				text = seize_realm_valid_government_tt
				OR = {
					government_has_flag = government_is_feudal
					government_has_flag = government_is_clan
					government_has_flag = government_is_tribal
					government_has_flag = government_is_mandala
					government_has_flag = government_is_japan_feudal
				}
			}
		}
		in_diplomatic_range = scope:target
	}
	valid = {
		trigger_if = {
			limit = {
				is_parent_of = scope:target
				NOT = { has_trait_with_flag = can_murder_own_children }
			}
			NOT = { is_parent_of = scope:target }
		}
		NOT = { is_at_war_with = scope:target }
		custom_description = {
			object = scope:target
			text = "I_have_imprisoned_target"
			NOT = {
				scope:target = { is_imprisoned_by = scope:owner }
			}
		}
		government_has_flag = government_is_landless_adventurer
		scope:target = {
			is_landed = yes
			top_liege = this
			highest_held_title_tier >= tier_county
			custom_tooltip = {
				text = seize_realm_valid_government_tt
				OR = {
					government_has_flag = government_is_feudal
					government_has_flag = government_is_clan
					government_has_flag = government_is_tribal
					government_has_flag = government_is_mandala
					government_has_flag = government_is_japan_feudal
				}
			}
		}
		trigger_if = {
			limit = {
				scope:target.primary_title.tier <= tier_duchy
			}
			prestige_level >= 3
		}
		trigger_else_if = {
			limit = {
				scope:target.primary_title.tier = tier_kingdom
			}
			prestige_level >= 4
		}
		trigger_else_if = {
			limit = {
				scope:target.primary_title.tier = tier_empire
			}
			prestige_level >= 5
		}
		trigger_else = {
			always = no
		}
		in_diplomatic_range = scope:target
		# Special AI exceptions
		NOT = {
			scope:owner = {
				is_ai = yes
				has_opinion_modifier = {
					target = scope:target
					modifier = repentant_opinion
				}
			}
		}
	}

	# Agents
	agent_leave_threshold = -25
	agent_join_chance = {
		base = 0
		ai_agent_join_chance_basic_suite_modifier = yes
		ai_agent_join_chance_hostile_grievous_modifier = yes
		modifier = {
			add = 25
			"opinion(scope:target)" > "opinion(scope:target)"
			scope:owner = { has_claim_on = scope:target.primary_title }
			desc = SCHEME_PREFERRED_CLAIMANT
		}
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_target_character_perspective = { courtiers vassals }

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

	# Base Chances
	base_success_chance = {
		base = 0
		scheme_type_skill_success_chance_modifier = { SKILL = INTRIGUE }

		hostile_scheme_base_chance_modifier = yes

		# Skill
		compare_modifier = {
			desc = "SCHEME_MY_INTRIGUE"
			value = scope:owner.intrigue
			multiplier = 3
		}
		# Repeat
		modifier = {
			add = -50
			scope:target = {				
				has_opinion_modifier = {
					target = scope:owner
					modifier = attempted_to_seize_my_realm_crime
				}
			}
			desc = HAS_ATTEMPTED_TO_SEIZE_TARGETS_REALM_BEFORE
		}
		# Spiritual Head of Faith
		modifier = {
			add = -50
			desc = "SCHEME_VS_SPIRITUAL_HOF"
			scope:target.faith = {
				this = scope:owner.faith
				has_doctrine = doctrine_spiritual_head
				religious_head ?= scope:target
			}
		}
		# Sponsored Inspiration
		modifier = {
			add = -25
			desc = target_has_sponsored_inspiration
			scope:target.inspiration ?= { exists = inspiration_sponsor }
		}
		# Family Feud
		house_feud_hostile_scheme_success_modifier = yes
		# At War
		first_valid = {
			modifier = {
				add = -50
				desc = "SCHEME_AT_WAR"
				scope:target = { is_at_war_with = scope:owner }
			}
			modifier = {
				add = -25
				scope:target = {
					is_playable_character = no
					liege ?= { is_at_war_with = scope:owner }
				}
				desc = "SCHEME_AT_WAR_WITH_LIEGE"
			}
			modifier = {
				add = -25
				scope:target = {
					is_playable_character = yes
					liege ?= { is_at_war_with = scope:owner }
				}
				desc = "SCHEME_AT_WAR_WITH_LIEGE"
			}
		}
		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_within_realm_bonus_modifier = yes
		# Tailor
		modifier = {
			add = 10
			scope:owner = {
				has_character_modifier = tournament_tailor_mixup_scheme_modifier
				exists = var:tailor_mixup_house
				exists = scope:target.house #Unop ck3-target
				var:tailor_mixup_house = scope:target.house #Unop ck3-target
			}
			desc = "tournament_tailor_mixup_scheme_modifier_value"
		}
		# Feast
		modifier = {
			add = 10
			desc = feast_learnt_habits_modifier.tt
			scope:owner = {
				any_scheme = {
					type = murder
					scheme_target_character = scope:target
					has_scheme_modifier = feast_learnt_habits_modifier
				}
			}
		}
		# Caliph protection
		modifier = {
			add = -200
			scope:target = { has_character_modifier = fp3_struggle_caliph_super_suspicious_modifier }
			desc = has_fp3_struggle_caliph_super_suspicious_modifier
		}
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:calculating
		}
		add_scheme_starting_opportunities_intrigue_effect = yes
		# 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_muscle
			add_agent_slot = agent_muscle
			add_agent_slot = agent_diplomat
			add_agent_slot = agent_infiltrator
			add_agent_slot = agent_lookout
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		seize_realm_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { seize_realm_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			seize_realm_scheme_prep_effect = yes
		}
	}

	## Invalidation
	on_invalidated = {
		if = {
			limit = { exists = scheme_target_character.primary_title }
			scheme_target_character.primary_title = { save_scope_as = target_title }
		}
		scheme_owner = {
			if = { # Target has died
				limit = {
					scope:target = { is_alive = no }
				}
				send_interface_toast = {
					title = seize_realm_invalidated_title
					type = event_toast_effect_neutral
					left_icon = scope:target
					custom_tooltip = seize_realm_target_died_tt
				}
			}
			else_if = { # Target is no longer landed
				limit = {
					OR = {
						NOT = { exists = scope:target_title }
						scope:target = { is_landed = no }
					}
				}
				send_interface_toast = {
					title = seize_realm_invalidated_title
					type = event_toast_effect_neutral
					left_icon = scope:target
					custom_tooltip = seize_realm_target_lost_titles_tt
				}
			}
			else_if = { # Target is no longer independent
				limit = {
					scope:target = { top_liege != this }
				}
				send_interface_toast = {
					title = seize_realm_invalidated_title
					type = event_toast_effect_neutral
					left_icon = scope:target
					right_icon = scope:target.liege
					custom_tooltip = seize_realm_target_not_independent_tt
				}
			}
			else = { # Fallback
				send_interface_toast = {
					title = seize_realm_invalidated_title
					type = event_toast_effect_neutral
					left_icon = scope:target
					custom_tooltip = seize_realm_invalidate_fallback_tt
				}
			}
		}
	}

	## Ongoing
	on_monthly = {
		hostile_scheme_monthly_discovery_chance_effect = yes
		if = {
			limit = {
				NOT = { exists = scope:discovery_event_happening }
			}
			fire_generic_contract_scheme_on_going_events_effect = yes
		}
	}
}
