﻿fabricate_hook = {
	# Basic Setup
	skill = intrigue
	desc = fabricate_hook_desc_general
	success_desc = "FABRICATE_HOOK_SUCCESS_DESC"
	discovery_desc = "FABRICATE_HOOK_DISCOVERY_DESC"
	icon = icon_scheme_fabricate_hook
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = hostile
	target_type = character
	is_secret = yes
	maximum_breaches = 5
	cooldown = { years = 2 }

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

	# Core Triggers
	allow = {
		OR = {
			has_perk = truth_is_relative_perk # Scheme unlocked by Perk
			AND = {
				scope:owner = { 
					has_trait = vengeful 
					has_relation_rival = scope:target 
				}
			}
			AND = {
				any_character_struggle = {
					involvement = involved
					has_struggle_phase_parameter = unlocks_fabricate_hooks_for_all
					is_secondary_character_involvement_either_trigger = {
						CHAR = scope:target
					}
				}
			}
			AND = {
				government_has_flag = government_is_clan
				is_in_same_clan_as_trigger = { CHARACTER = scope:target }
				house = {
					has_house_unity_parameter = unity_unlock_fabricate_hooks	
				}
			}
		}
		
		is_adult = yes
		OR = {
		is_playable_character = yes
		is_ai = no
		}
		is_imprisoned = no
	}
	valid = {
		is_incapable = no
		trigger_if = {
			limit = { exists = scope:target.court_owner }
			custom_description = {
				text = was_discovered_fabricating_hooks_already
				NOT = {
					scope:target.court_owner = {
						has_opinion_modifier = {
							target = scope:owner
							modifier = fabricated_hook_against_court_opinion
						}
					}
				}
			}
		}
		trigger_else = {
			custom_description = {
				text = can_not_fabricate_hooks_on_wanderers
				always = no
			}
		}
		scope:target = {
			is_adult = yes
			is_imprisoned = no
			NOT = { has_trait = incapable }
			#exists = location
			in_diplomatic_range = scope:owner
		}

		#Temporary fix to communicate to players that the scheme is unavailabe. 
		#A character should always have a location but some characters are lost in the pool ATM
		#This custom description should be removed once the pool is fixed, and the line from the previous block should be uncommented. 
		custom_description = {
			text = in_diplomatic_range
			scope:target = {
				exists = location
			}
		}

		custom_description = {
			text = already_has_hook_on_ruler_target
			NOT = { scope:owner = { has_hook = scope:target } }
			object = scope:target
		}
		custom_description = {
			text = already_has_strong_hook_on_target
			NOT = { scope:owner = { has_strong_hook = scope:target } }
			object = scope:target
		}
		custom_description = {
			text = already_knows_secret_about_ruler_target
			NOT = {
				scope:target = {
					is_ruler = yes
					any_secret = {
						is_known_by = scope:owner
						is_blackmailable_secret_trigger = { BLACKMAILER = scope:owner PARTICIPANT = scope:target }
					}
				}	
			}
		}
		custom_description = {
			text = already_knows_criminal_secret_about_target
			NOT = {
				scope:target = {
					any_secret = {
						is_criminal_for = scope:target
						is_known_by = scope:owner
						is_blackmailable_secret_trigger = { BLACKMAILER = scope:owner PARTICIPANT = 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_taboo_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 = hostile_scheme_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 = odds_fabricate_hook_scheme_misc_value
		min = 0
	}

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

		hostile_scheme_base_chance_modifier = yes

		# Countermeasures.
		apply_indirect_scheme_success_chance_adjustments_modifier = yes

		modifier = {
			add = 20
			desc = FABRICATE_HOOK_DECEITFUL
			scope:owner = {
				has_trait = deceitful
			}
		}

		modifier = {
			add = -20
			desc = FABRICATE_HOOK_HONEST
			scope:owner = {
				has_trait = honest
			}
		}

		modifier = {
			add = 10
			desc = FABRICATE_HOOK_TARGET_TRUSTING
			scope:target = {
				has_trait = trusting
			}
		}

		modifier = {
			add = 10
			desc = FABRICATE_HOOK_TARGET_INTELLECT_BAD_1
			scope:target = {
				has_trait = intellect_bad_1
			}
		}

		modifier = {
			add = 20
			desc = FABRICATE_HOOK_TARGET_INTELLECT_BAD_2
			scope:target = {
				has_trait = intellect_bad_2
			}
		}

		modifier = {
			add = 30
			desc = FABRICATE_HOOK_TARGET_INTELLECT_BAD_3
			scope:target = {
				has_trait = intellect_bad_3
			}
		}

		modifier = {
			add = -10
			desc = FABRICATE_HOOK_TARGET_INTELLECT_GOOD_1
			scope:target = {
				has_trait = intellect_good_1
			}
		}

		modifier = {
			add = -20
			desc = FABRICATE_HOOK_TARGET_INTELLECT_GOOD_2
			scope:target = {
				has_trait = intellect_good_2
			}
		}

		modifier = {
			add = -30
			desc = FABRICATE_HOOK_TARGET_INTELLECT_GOOD_3
			scope:target = {
				has_trait = intellect_good_3
			}
		}

		modifier = {
			add = -20
			desc = FABRICATE_HOOK_TARGET_SCHEMER
			scope:target = {
				has_trait = schemer
			}
		}

		modifier = {
			add = -20
			desc = FABRICATE_HOOK_TARGET_PARANOID
			scope:target = {
				has_trait = paranoid
			}
		}

		modifier = {
			add = -10
			desc = FABRICATE_HOOK_TARGET_FICKLE
			scope:target = {
				has_trait = fickle
			}
		}

		modifier = {
			add = 10
			desc = FABRICATE_HOOK_TARGET_ARROGANT
			scope:target = {
				has_trait = arrogant
			}
		}

		modifier = {
			add = 20
			desc = FABRICATE_HOOK_TARGET_GREEDY
			scope:target = {
				has_trait = greedy
			}
		}

		modifier = {
			add = 10
			desc = FABRICATE_HOOK_TARGET_CRAVEN
			scope:target = {
				has_trait = craven
			}
		}

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

		compare_modifier = {
			value = scope:owner.cp:councillor_spymaster.intrigue
			multiplier = 2
			desc = FABRICATE_HOOK_SPYMASTER_INTRIGUE
			trigger = {
				exists = scope:owner.cp:councillor_spymaster
				NOT = { scope:owner.cp:councillor_spymaster = scope:target }
			}
		}
		
		modifier = {
			add = -50
			desc = FABRICATE_HOOK_TARGETING_SPYMASTER
			scope:target = {
				has_council_position = councillor_spymaster
			}
		}
		
		# Spiritual Head of Faith
		modifier = {
			add = -50
			desc = "SCHEME_VS_SPIRITUAL_HOF"
			scope:target = {
				faith = scope:owner.faith
				faith = {
					has_doctrine = doctrine_spiritual_head
				}
				faith = {
					exists = religious_head
					religious_head = {
			 			this = scope:target
			 		}
				}
			}
		}
	}	
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
	}

	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:indirect
		}
		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_outcast
			add_agent_slot = agent_diplomat
			add_agent_slot = agent_thief
			add_agent_slot = agent_infiltrator
			add_agent_slot = agent_lookout
		}
	}
	on_phase_completed = {
		suppress_scheme_follow_up_event_till_input_given_effect = yes
		fabricate_hook_scheme_prep_effect = yes
		cap_schemes_and_fire_reminders_effect = yes
	}
	on_hud_click  = { fabricate_hook_scheme_prep_effect = yes }
	on_monthly = {
		hostile_scheme_monthly_discovery_chance_effect = yes
		if = {
			limit = {
				NOT = { exists = scope:discovery_event_happening }
			}
			scheme_owner = {
				trigger_event = {
					on_action = fabricate_hook_ongoing
					days = { 1 30 }
				}
			}
		}
	}
	on_semiyearly = {
		if = {
			limit = {
				scheme_owner = { is_ai = yes }
			}
			fabricate_hook_scheme_prep_effect = yes
		}
	}
	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 = fabricate_hook_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_died
					}
				}
			}
		}
		else_if = {
			limit = {
				scope:target = {
					is_imprisoned = yes
				}
			}
			scope:target.imprisoner = { save_scope_as = other_imprisoner }
			scope:owner = {
				send_interface_toast = {
					title = fabricate_hook_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						subject = scope:other_imprisoner
						object = scope:target
						text = scheme_target_imprisoned_by_other
					}
				}
			}
		}
		else_if = {
			limit = {
				scope:target = {
					has_trait = incapable
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = fabricate_hook_scheme_invalidated_title
					left_icon = scope:target
					custom_tooltip = scheme_target_is_incapable
				}
			}
		}
		else_if = {
			limit = {
				scope:owner = {
					has_hook = scope:target
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = fabricate_hook_scheme_invalidated_title
					left_icon = scope:target
					custom_tooltip = fabricate_hook_has_gained_hook
				}
			}
		}
		else_if = {
			limit = {
				scope:target = { 
					NOT = { in_diplomatic_range = scope:owner } 
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = fabricate_hook_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_not_in_diplomatic_range
					}
				}
			}
		}
	}
}
