﻿# Start a scheme to replace an incumbent regent.
overthrow_regent_scheme_interaction = {
	category = interaction_category_diarch
	icon = icon_declare_me_regent
	ignores_pending_interaction_block = yes

	scheme = overthrow_regent
	desc = overthrow_regent_scheme_interaction_desc

	interface_priority = 20

	is_shown = {
		# Standard setup.
		scope:actor.liege ?= {
			has_active_diarchy = yes
			# You can't overthrow non-regencies. At least not this way.
			has_diarchy_parameter = diarchy_is_regency
		}
		scope:recipient = {
			this != scope:actor
			liege ?= {
				this = scope:actor.liege
				diarch ?= scope:recipient
				this != scope:actor
			}
		}
	}

	is_valid_showing_failures_only = {
		# Make sure you wouldn't just cop out immediately.
		## Disabling is fine, FWIW. You'll de-disable with time.
		scope:actor = { is_diarch_valid_trigger = yes }
		# Have we been vetoed from succession due to politicking?
		custom_tooltip = {
			text = overthrow_regent_scheme_interaction.tt.filtered_due_to_incumbent_politicking
			NOT = { scope:actor.var:diarch_succession_filtered_due_to_incumbents_politicking ?= scope:recipient }
		}
	}

	# Long specific cooldown. If you give up, you should give it a rest 'less things change.
	cooldown_against_recipient = { years = 10 }

	# Scheme Starter Packages
	options_heading = schemes.t.agent_packages
	send_options_exclusive = yes
	## Balanced agents.
	send_option = {
		flag = agent_focus_balance
		current_description = overthrow_regent_scheme_interaction.tt.agent_focus_balance
	}
	## Focused on Success Chance.
	send_option = {
		flag = agent_focus_success
		current_description = overthrow_regent_scheme_interaction.tt.agent_focus_success
	}
	## Focused on Speed.
	send_option = {
		flag = agent_focus_speed
		current_description = overthrow_regent_scheme_interaction.tt.agent_focus_speed
	}
	## Focused on Secrecy.
	send_option = {
		flag = agent_focus_secrecy
		current_description = overthrow_regent_scheme_interaction.tt.agent_focus_secrecy
	}

	on_accept = {
		scope:actor = {
			# Sort some stress stuff.
			overthrow_regent_scheme_interaction_actor_stress_effect = yes
			# And some feedback for starting.
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = overthrow_regent_scheme_interaction.tt.started_scheme
				left_icon = scope:actor
				right_icon = scope:recipient
				# Success.
				if = {
					limit = { scope:agent_focus_success ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = overthrow_regent
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_thug
						AGENT_2 = agent_ambusher
						AGENT_3 = agent_muscle
						# Speed.
						AGENT_4 = agent_footpad
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
				# Speed.
				else_if = {
					limit = { scope:agent_focus_speed ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = overthrow_regent
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Speed.
						AGENT_1 = agent_footpad
						AGENT_2 = agent_tracker
						AGENT_3 = agent_planner
						# Success.
						AGENT_4 = agent_thug
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
				# Secrecy.
				else_if = {
					limit = { scope:agent_focus_secrecy ?= yes }
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = overthrow_regent
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Secrecy.
						AGENT_1 = agent_lookout
						AGENT_2 = agent_lookout
						AGENT_3 = agent_decoy
						# Success.
						AGENT_4 = agent_thug
						# Speed.
						AGENT_5 = agent_footpad
					}
				}
				# Balanced.
				else = {
					begin_scheme_with_agents_effect = {
						SCHEME_TYPE = overthrow_regent
						TARGET_TYPE = target_character
						TARGET_SCOPE = scope:recipient
						# Success.
						AGENT_1 = agent_thug
						AGENT_2 = agent_ambusher
						# Speed.
						AGENT_3 = agent_footpad
						AGENT_4 = agent_planner
						# Secrecy.
						AGENT_5 = agent_lookout
					}
				}
			}
		}
	}

	auto_accept = yes

	# AI
	ai_targets = {
		ai_recipients = peer_vassals
		ai_recipients = scripted_relations
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 120
		duchy = 60
		kingdom = 60
		empire = 60
		hegemony = 60
	}

	ai_potential = {
		# Some traits lock the AI out.
		## Calc these first 'cause they're cheaper.
		NOR = {
			has_trait = lazy
			has_trait = humble
			has_trait = content
		}
		OR = {
			# Driven by traits or personality.
			has_trait = arrogant
			has_trait = ambitious
			# Family think themselves worthy.
			any_close_or_extended_family_member = { this = liege }
			# If you're rivals with the current regent, that's a good cause.
			any_relation = {
				type = rival
				is_diarch_of_target = root.liege
			}
		}
	}

	ai_will_do = {
		# Most AI think this is audacious.
		base = -50

		# Opinion Factor
		opinion_modifier = {
			opinion_target = scope:recipient
			multiplier = -1
		}
		# Weight for personality.
		## Both modifier.
		ai_value_modifier = {
			# Bold, energetic characters fancy the gig.
			ai_boldness = 0.5
			ai_energy = 0.5
		}
		## And traits!
		### These are balanced differently to the other interactions, so it's deliberate that they're not using the @values here.
		### +++ Loyal (if current regent is disloyal).
		modifier = {
			add = diarch_ai_desire_plus_3_value
			has_trait = loyal
			exists = liege
			scope:recipient = { diarch_loyalty <= diarch_loyalty_visibly_disloyal_threshold }
			diarch_loyalty_score_type_regency_value >= diarch_loyalty_visibly_loyal_threshold
		}
		### +++ Disloyal (if current regent is loyal).
		modifier = {
			add = diarch_ai_desire_plus_3_value
			has_trait = disloyal
			exists = liege
			scope:recipient = { diarch_loyalty >= diarch_loyalty_visibly_loyal_threshold }
			diarch_loyalty_score_type_regency_value >= diarch_loyalty_visibly_disloyal_threshold
		}
		### ++ Ambitious.
		modifier = {
			add = diarch_ai_desire_plus_2_value
			has_trait = ambitious
		}
		### ++ Arrogant.
		modifier = {
			add = diarch_ai_desire_plus_2_value
			has_trait = arrogant
		}
		### ++ Deceitful.
		modifier = {
			add = diarch_ai_desire_plus_2_value
			has_trait = deceitful
		}
		### + Diligent.
		modifier = {
			add = diarch_ai_desire_plus_1_value
			has_trait = diligent
		}
		### - Lazy.
		modifier = {
			add = diarch_ai_desire_minus_1_value
			has_trait = lazy
		}
		### -- Honest.
		modifier = {
			add = diarch_ai_desire_minus_2_value
			has_trait = honest
		}
		### -- Humble.
		modifier = {
			add = diarch_ai_desire_minus_2_value
			has_trait = humble
		}
		### -- Content.
		modifier = {
			add = diarch_ai_desire_minus_2_value
			has_trait = content
		}
		### --- Disloyal (if current regent is disloyal).
		modifier = {
			add = diarch_ai_desire_minus_3_value
			has_trait = disloyal
			exists = liege
			scope:recipient = { diarch_loyalty <= diarch_loyalty_visibly_disloyal_threshold }
			diarch_loyalty_score_type_regency_value <= diarch_loyalty_visibly_disloyal_threshold
		}
		### --- Loyal (if current regent is loyal).
		#### Also some loyal-adjacent traits that would otherwise cause stress.
		modifier = {
			add = diarch_ai_desire_minus_3_value
			OR = {
				has_trait = loyal
				has_trait = honest
				has_trait = compassionate
				has_trait = just
			}
			exists = liege
			scope:recipient = { diarch_loyalty >= diarch_loyalty_visibly_loyal_threshold }
			diarch_loyalty_score_type_regency_value >= diarch_loyalty_visibly_loyal_threshold
		}
		# Weight by proximity to the ruler: close family, spouses, etc.
		modifier = {
			add = diarch_ai_desire_plus_2_value
			exists = scope:recipient.liege
			OR = {
				any_close_or_extended_family_member = { this = scope:recipient.liege }
				any_spouse = { this = scope:recipient.liege }
			}
		}
		# Councillors have ambitions.
		modifier = {
			add = diarch_ai_desire_plus_2_value
			is_councillor = yes
		}

		ttd_medium_distance_ai_will_do_modifier = { CHARACTER_1 = scope:actor CHARACTER_2 = scope:recipient.liege }
		
		# Factors.
		## If relations are near-perfect, we don't want to go plotting, as it'll look weird.
		modifier = {
			factor = 0
			OR = {
				has_any_good_relationship_with_character_trigger = { CHARACTER = scope:recipient }
				opinion = {
					target = scope:recipient
					value >= very_high_positive_opinion
				}
			}
		}
	}
}
