﻿steal_back_artifact = {
	# Basic Setup
	skill = intrigue
	desc = steal_back_artifact_general_desc
	success_desc = "STEAL_BACK_ARTIFACT_SUCCESS_DESC"
	icon = icon_scheme_steal_back_artifact
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = hostile
	target_type = character
	is_secret = yes
	maximum_breaches = 5
	cooldown = { years = 10 }
	
	# Parameters
	speed_per_skill_point = t3_spsp_owner_value
	speed_per_target_skill_point = t3_spsp_target_value
	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
	
	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no

		scope:target = {
			is_adult = yes
			is_imprisoned = no
		}

		#AI blockers
		trigger_if = {
			limit = { is_ai = yes }

			scope:target = { #AI won't steal if they like you
				opinion = {
					target = scope:owner
					value <= 50
				}

				trigger_if = { #AI won't steal from friends... Unless...
					limit = {
						OR = {
							has_relation_friend = scope:owner 
							has_relation_best_friend = scope:owner 
						}
					}
					scope:owner = {
						OR = {
							has_trait = greedy
							has_trait = deceitful
						}
					}
				}
				trigger_if = { #So players don't get spammed by AIs trying to steal 
					limit = {
						is_ai = no
					}
					NOT = {
						any_targeting_scheme = {
							scheme_type = steal_back_artifact
						}
					}
				}
			}
		}
	}
	valid = {
		is_incapable = no
		NOT = { is_at_war_with = scope:target }
		scope:target = {
			OR = {
				exists = location
				in_diplomatic_range = scope:owner
			}
		}
		trigger_if = {
			limit = {
				scope:owner = {
					NOR = {
						employs_court_position = master_thief_camp_officer
						any_character_active_contract = {
							task_contract_type = laamp_steal_artifact_contract
							var:task_contract_target ?= scope:target
						}
					}
				}
			}
			scope:target = {
				any_character_artifact = {
					scope:owner = { 
						has_artifact_claim = prev
					}
				}
			}
		}
	}

	# 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
	}
	valid_agent = { is_valid_agent_standard_trigger = yes }
	agent_groups_owner_perspective = { courtiers guests scripted_relations }
	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_owner_perspective_value
		add = agent_groups_target_character_perspective_value
		add = hostile_scheme_base_odds_prediction_target_is_char_value
		add = odds_steal_back_artifact_scheme_misc_value
		min = 0
	}

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

		#SCHEME OWNER#
		compare_modifier = {
			desc = SCHEME_IS_A_SCHEMER
			target = scope:owner
			value = intrigue
			multiplier = 2
		}

		# Countermeasures.
		apply_calculated_scheme_success_chance_adjustments_modifier = yes

		# Contract
		modifier = {
			scope:owner = {
				any_character_task_contract = {
					task_contract_type = laamp_steal_artifact_contract
					var:task_contract_target ?= scope:target
				}
			}
			desc = CONTRACTED_THIEF_DESC
			add = 25
		}
		# house_head_request_interaction
		modifier = {
			add = hostile_scheme_variable_list_value
			scope:owner = {
				has_variable_list = supporting_hostile_schemes
			}
			desc = HOUSE_HEAD_SCHEME_SUPPORT_DESC
		}
		modifier = {
			add = -10
			scope:owner = { has_character_modifier = hostile_schemes_distracted_modifier }
			desc = hostile_schemes_distracted_modifier
		}

		# Distance Penalty
		ttd_medium_distance_success_chance_modifier = yes
	}
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
	}

	# 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_thief
			add_agent_slot = agent_thief
			add_agent_slot = agent_thug
			add_agent_slot = agent_infiltrator
			add_agent_slot = agent_lookout
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		steal_back_artifact_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { steal_back_artifact_scheme_prep_effect = yes }
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			steal_back_artifact_scheme_prep_effect = yes
			purge_ai_scheme_slots_effect = yes
		}
	}
	on_monthly = {
		save_scope_as = scheme
		scheme_owner = { save_scope_as = owner }
		scheme_target_character = { save_scope_as = target }

		hostile_scheme_monthly_discovery_chance_effect = yes

		if = {
			limit = {
				NOT = { exists = scope:discovery_event_happening }
			}			
			scheme_owner = {
				trigger_event = {
					on_action = steal_back_artifact_ongoing
					days = { 1 15 }
				}
			}
		}
	}
	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}
		if = {
			limit = {
				scope:target = { is_alive = no }
			}
			scope:owner = {
				send_interface_toast = {
					title = steal_back_artifact_invalidated_title
					custom_tooltip = steal_back_artifact_invalidated_dead
					left_icon = scope:target
				}
			}
		}
		else_if = {	
			limit = {
				scope:owner = {
					is_at_war_with = scope:target
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = steal_back_artifact_invalidated_title
					custom_tooltip = steal_back_artifact_invalidated_war
					left_icon = scope:target
				}
			}
		}

		if = {
			limit = {
				scope:target = { 
					NOT = { in_diplomatic_range = scope:owner } 
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = steal_back_artifact_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_not_in_diplomatic_range
					}
				}
			}
		}
	}
}
