﻿challenge_status = {
	# Basic Setup
	skill = learning
	desc = "challenge_status_desc_general"
	success_desc = "CHALLENGE_STATUS_SUCCESS_DESC"
	icon = icon_scheme_challenge_status
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	target_type = character
	category = political
	cooldown = { years = 10 }

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

	# 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 = t2_spsp_target_spy_value
	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 = t2_scgpsp_value

	is_secret = yes
	maximum_breaches = 5
	maximum_secrecy = 100
	minimum_secrecy = 0
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
	}

	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no
		government_allows = administrative
	}

	valid = {
		scope:target = {
			is_adult = yes
			government_allows = administrative
			top_liege.primary_title = prev.top_liege.primary_title # This hides the tooltip
		}
	}

	# Agents
	agent_leave_threshold = -25
	agent_join_chance = {
		base = 0
		ai_agent_join_chance_basic_suite_modifier = yes
		ai_agent_join_chance_hostile_general_suite_modifier = yes
		ai_agent_join_chance_political_suite_modifier = yes
	}
	agent_groups_owner_perspective = { peer_vassals courtiers vassals councillors }
	agent_groups_target_character_perspective = { courtiers vassals councillors }
	valid_agent = { is_valid_agent_standard_trigger = yes }

	odds_prediction = {
		add = base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_learning_value
		add = agent_groups_owner_perspective_value
		add = agent_groups_target_character_perspective_value
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = 0
		scheme_type_skill_success_chance_modifier = { SKILL = LEARNING }
		
		# Countermeasures
		apply_political_scheme_success_chance_adjustments_modifier = yes
		
		# Target's learning
		modifier = {
			desc = "SCHEME_TARGET_LEARNING"
			add = {
				value = scope:target.learning
				multiply = -1
			}
		}

		# Spiritual Head of Faith
		modifier = {
			add = -50
			desc = "SCHEME_VS_SPIRITUAL_HOF"
			scope:owner = {
				faith = {
					exists = religious_head
					has_doctrine = doctrine_spiritual_head
					religious_head = {
				 		this = scope:target
				 	}
				}
			}
		}

		# Family Feud
		house_feud_hostile_scheme_success_modifier = yes

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

		modifier = {
			scope:owner = {
				has_character_modifier = united_political_agents_modifier
				is_vassal_of = scope:target
			}
			desc = united_political_agents_modifier
			add = 15
		}
		
		# Influence
		modifier = {
			desc = "TARGET_INFLUENCE_LEVEL_DESC"
			add = {
				value = scope:target.influence_level
				multiply = -5
			}
		}
		
		# Target's traits
		modifier = { # Target is ambitious, and therefore more difficult to challenge
			scope:target = { has_trait = ambitious }
			desc = "SCHEME_TARGET_TRAIT_AMBITIOUS"
			add = -10
		}
		modifier = { # Target is gregarious, and is therefore good at talking to and convincing others
			scope:target = { has_trait = gregarious }
			desc = "SCHEME_BEFRIEND_THEIR_GREGARIOUSNESS"
			add = -10
		}
		
		# House
		modifier = {
			scope:owner.house = {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = OWNER_POWERFUL_FAMILY_DESC
			add = 5
		}
		modifier = {
			scope:owner.house = { is_dominant_family = yes }
			desc = OWNER_DOMINANT_FAMILY_DESC
			add = 10
		}
		modifier = {
			scope:target.house = {
				is_powerful_family = yes
				is_dominant_family = no
			}
			desc = TARGET_POWERFUL_FAMILY_DESC
			add = -10
		}
		modifier = {
			scope:target.house = { is_dominant_family = yes }
			desc = TARGET_DOMINANT_FAMILY_DESC
			add = -20
		}

		# Distance Penalty
		ttd_minor_distance_success_chance_modifier = yes
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:political
		}
		# 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_proponent
			add_agent_slot = agent_intermediary
			add_agent_slot = agent_political_socialite
			add_agent_slot = agent_bureaucrat
			add_agent_slot = agent_amanuensis
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		challenge_status_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { challenge_status_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			challenge_status_scheme_prep_effect = yes
			purge_ai_scheme_slots_effect = yes
		}
	}

	## Invalidation
	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = challenge_status_invalidated_title
				left_icon = scope:target
				if = { # Invalidation due to death
					limit = {
						scope:target = { is_alive = no }
						NOT = { block_death_event_trigger = { DEAD = scope:target } }
						scope:owner = { is_alive = yes }
					}
					custom_tooltip = scheme_target_died
				}
				else_if = { # No more diplo range
					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
				}
			}
		}
	}

	## Ongoing
	on_monthly = { 
		hostile_scheme_monthly_discovery_chance_effect = yes
		fire_generic_contract_scheme_on_going_events_effect = yes 
	}
}
