﻿task_disrupt_schemes = {
	position = councillor_spymaster
	default_task = yes

	task_type = task_type_general
	task_progress = task_progress_infinite

	asset = {
		icon = "gfx/interface/icons/council_task_types/task_disrupt_schemes.dds"
		background = "gfx/interface/buttons/button_round_bg.dds"
		frame = "gfx/interface/buttons/button_round_frame.dds"
		glow = "gfx/particles/halo.dds"
	}

	effect_desc = {
		#Hostile Scheme Resistance Descs
		desc = task_disrupt_schemes_hostile_scheme_resistance_base
		triggered_desc = {
			trigger = { scope:councillor_liege = { has_perk = court_of_shadows_perk } }
			desc = task_disrupt_schemes_hostile_scheme_resistance_perk_bonus
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			desc = task_disrupt_schemes_hostile_scheme_resistance_legacy_bonus
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_family_business_bonus = yes
			}
			desc = task_disrupt_schemes_hostile_scheme_resistance_family_business_bonus
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			desc = task_disrupt_schemes_hostile_scheme_resistance_consulted_house_bonus
		}
		triggered_desc = {
			trigger = {
				scope:councillor_liege.house = { has_house_power_parameter = determination_spymaster_task_disrupt_schemes_efficiency }
			}
			desc = determination_spymaster_hostile_scheme_resistance_task_disrupt_schemes
		}
		#Discovery Chance Descs
		desc = task_disrupt_schemes_discovery_chance_base
		triggered_desc = {
			trigger = { scope:councillor_liege = { has_perk = court_of_shadows_perk } }
			desc = task_disrupt_schemes_discovery_chance_perk_bonus
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			desc = task_disrupt_schemes_discovery_chance_legacy_bonus
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_family_business_bonus = yes
			}
			desc = task_disrupt_schemes_discovery_chance_family_business_bonus
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			desc = task_disrupt_schemes_discovery_chance_consulted_house_bonus
		}
		triggered_desc = {
			trigger = {
				scope:councillor_liege.house = { has_house_power_parameter = determination_spymaster_task_disrupt_schemes_efficiency }
			}
			desc = determination_spymaster_discovery_task_disrupt_schemes
		}
		#Possible Side Effects Descs
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					intrigue > mediocre_skill_rating
				}
				desc = task_disrupt_schemes_hostile_scheme_disrupted
			}
			triggered_desc = {
				trigger = {
					intrigue < high_skill_rating
					scope:councillor_liege = {
						has_any_secrets = yes
					}
				}
				desc = task_disrupt_schemes_secret_revealed
			}
			triggered_desc = {
				trigger = {
					intrigue < high_skill_rating
				}
				desc = task_disrupt_schemes_courtier_secret_revealed
			}
		}
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_modifier
		enemy_hostile_scheme_phase_duration_add = 1
		scale = scope:councillor.spymaster_disrupt_schemes_modifier_base_scale
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_court_of_shadows_modifier #NOT USED?
		enemy_hostile_scheme_phase_duration_add = 1
		scale = scope:councillor.spymaster_disrupt_schemes_court_of_shadows_modifier_scale
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_erudition_legacy_5_modifier
		enemy_hostile_scheme_phase_duration_add = 1
		scale = scope:councillor.spymaster_disrupt_schemes_erudition_legacy_5_modifier_scale
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_erudition_legacy_5_modifier #TO EDIT?
		enemy_hostile_scheme_phase_duration_add = 1
		scale = scope:councillor.spymaster_disrupt_schemes_family_business_modifier_scale
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_modifier
		scheme_discovery_chance_mult = 1.0
		scale = { value = scope:councillor.spymaster_disrupt_schemes_scheme_discovery_modifier divide = 200 }
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_court_of_shadows_modifier
		scheme_discovery_chance_mult = 1.0
		scale = { value = scope:councillor.spymaster_disrupt_schemes_court_of_shadows_scheme_discovery_modifier_scale divide = 200 }
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_erudition_legacy_5_modifier #NOT USED?
		scheme_discovery_chance_mult = 1.0
		scale = { value = scope:councillor.spymaster_disrupt_schemes_erudition_legacy_5_discovery_modifier_scale divide = 200 }
	}

	council_owner_modifier = {
		name = task_disrupt_schemes_erudition_legacy_5_modifier #TO EDIT
		scheme_discovery_chance_mult = 1.0
		scale = { value = scope:councillor.spymaster_disrupt_schemes_family_business_discovery_modifier_scale divide = 200 }
	}

	council_owner_modifier = {
		name = determination_spymaster_task_disrupt_schemes_modifier
		scheme_discovery_chance_mult = 1.0
		scale = { value = scope:councillor.spymaster_disrupt_schemes_determination_discovery_modifier_scale divide = 200 }
	}

	monthly_on_action = task_disrupt_schemes_side_effects

	ai_will_do = {
		value = 1 # Always a good backup
		if = {
			limit = {
				has_trait = paranoid
			}
			add = 1000
		}
		if = {
			limit = {
				any_targeting_scheme = {
					hostile_scheme_trigger = yes
					is_scheme_exposed = yes
				}
			}
			add = 1000
		}
		if = { # Somewhat rational AI's want to protect themselves from people who might murder them
			limit = {
				ai_rationality > 0
				OR = {
					any_relation = {
						type = rival
						intrigue >= 12
						ai_honor <= 0
					}
					any_sibling = {
						ai_honor <= -50
						ai_greed >= 50
						save_temporary_scope_as = sibling
						root = {
							any_held_title = {
								place_in_line_of_succession = {
									target = scope:sibling
									value <= 3
								}
							}
						}
					}
				}
			}
			add = ai_rationality
		}
	}
}

task_support_schemes = {
	position = councillor_spymaster

	task_type = task_type_general
	task_progress = task_progress_infinite

	asset = {
		icon = "gfx/interface/icons/council_task_types/task_support_schemes.dds"
		background = "gfx/interface/buttons/button_round_bg.dds"
		frame = "gfx/interface/buttons/button_round_frame.dds"
		glow = "gfx/particles/halo.dds"
	}

	effect_desc = {
		desc = task_support_schemes_base_desc
		triggered_desc = {
			trigger = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			desc = task_support_schemes_legacy_bonus_desc
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_family_business_bonus = yes
			}
			desc = task_support_schemes_family_business_bonus_desc
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			desc = task_support_schemes_consulted_house_bonus_desc
		}
		triggered_desc = {
			trigger = {
				exists = scope:councillor_liege
				scope:councillor_liege = {
					has_royal_court = yes
					has_dlc_feature = royal_court
					has_court_type = court_intrigue
					court_grandeur_current_level >= 4
				}
			}
			desc = spymaster_espionage_court_type_bonus_desc
		}
		# Determination House Aspiration - Hostile Scheme Phase Length
		triggered_desc = {
			trigger = {
				scope:councillor_liege.house = { has_house_power_parameter = determination_spymaster_task_support_schemes_efficiency }
			}
			desc = determination_spymaster_task_support_schemes_bonus
		}
		desc = task_support_schemes_success_chance_base_desc
		triggered_desc = {
			trigger = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			desc = task_support_schemes_success_chance_legacy_bonus_desc
		}
		triggered_desc = {
			trigger = {
				councillor_liege_has_family_business_bonus = yes
			}
			desc = task_support_schemes_success_chance_family_business_bonus_desc
		}
		triggered_desc = {
			trigger = {
				exists = scope:councillor_liege
				scope:councillor_liege = {
					has_royal_court = yes
					has_dlc_feature = royal_court
					has_court_type = court_intrigue
					court_grandeur_current_level >= 4
				}
			}
			desc = spymaster_espionage_success_chance_court_type_bonus_desc
		}
		# Determination House Aspiration - Hostile Scheme Success Chance
		triggered_desc = {
			trigger = {
				scope:councillor_liege.house = { has_house_power_parameter = determination_spymaster_task_support_schemes_efficiency }
			}
			desc = determination_spymaster_task_support_schemes_success_chance_bonus
		}
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					intrigue > mediocre_skill_rating
				}
				desc = task_support_schemes_learn_secret
			}
			triggered_desc = {
				trigger = {
					intrigue < high_skill_rating
				}
				desc = task_support_schemes_lose_piety
			}
			triggered_desc = {
				trigger = {
					intrigue < high_skill_rating
				}
				desc = task_support_schemes_lose_prestige
			}
			triggered_desc = {
				trigger = {
					intrigue < high_skill_rating
				}
				desc = task_support_schemes_fail_scheme
			}
		}
	}

	council_owner_modifier = {
		name = task_support_schemes_modifier
		hostile_scheme_phase_duration_add = 1
		scale = spymaster_espionage_modifier_final_scale
	}

	council_owner_modifier = {
		name = task_support_schemes_modifier
		owned_hostile_scheme_success_chance_add = 1.0
		scale = spymaster_espionage_success_chance_total
	}

	monthly_on_action = task_support_schemes_side_effects

	ai_will_do = {
		value = 0

		scope:councillor_liege = {
			every_scheme = {
				limit = {
					OR = {
						scheme_type = murder
	 					scheme_type = abduct
					}
				}
				add = 300
			}
		}
	}
}


task_find_secrets = {
	position = councillor_spymaster

	task_type = task_type_court
	task_progress = task_progress_percentage
	restart_on_finish = yes

	asset = {
		icon = "gfx/interface/icons/council_task_types/task_find_secrets.dds"
		background = "gfx/interface/buttons/button_round_bg.dds"
		frame = "gfx/interface/buttons/button_round_frame.dds"
		glow = "gfx/particles/halo.dds"
	}

	effect_desc = {
		desc = task_find_secrets_effect_desc
		desc = {
			desc = council_task_possible_side_effects
			desc = task_find_secrets_opinion_loss
			triggered_desc = {
				trigger = {
					exists = scope:target_character
					scope:target_character = {
						is_vassal_of = scope:councillor_liege
					}
				}
				desc = task_find_secrets_vassal_intimidation
			}
		}
	}

	progress = {
		value = 0
		add = {
			value = spymaster_find_secrets_base
			desc = SPYMASTER_PROGRESS_BASE
		}
		add = {
			value = scope:councillor.spymaster_find_secrets_monthly_increase
			desc = SCALED_COUNCILLOR_INTRIGUE_VALUE
		}
		# Relation Bonuses/Penalties
		if = {
			limit = {
				exists = scope:councillor_liege
			}
			if = { # Friend
				limit = {
					scope:councillor_liege = {
						has_relation_friend = scope:councillor
						NOT = { has_relation_best_friend = scope:councillor }
					}
				}
				add = {
					value = scope:councillor.spymaster_find_secrets_monthly_increase_friend_bonus
					desc = COUNCILLOR_IS_YOUR_FRIEND
				}
			}
			if = { # Best Friend
				limit = {
					scope:councillor_liege = {
						has_relation_best_friend = scope:councillor
					}
				}
				add = {
					value = scope:councillor.spymaster_find_secrets_monthly_increase_best_friend_bonus
					desc = COUNCILLOR_IS_YOUR_BEST_FRIEND
				}
			}
			if = { # Rival
				limit = {
					scope:councillor_liege = {
						has_relation_rival = scope:councillor
						NOT = { has_relation_nemesis = scope:councillor }
					}
				}
				add = {
					value = scope:councillor.spymaster_find_secrets_monthly_increase_rival_bonus
					desc = COUNCILLOR_IS_YOUR_RIVAL
				}
			}
			if = { # Nemesis
				limit = {
					scope:councillor_liege = {
						has_relation_nemesis = scope:councillor
					}
				}
				add = {
					value = scope:councillor.spymaster_find_secrets_monthly_increase_nemesis_bonus
					desc = COUNCILLOR_IS_YOUR_NEMESIS
				}
			}
			if = {
				limit = {
					scope:councillor_liege = { has_perk = digging_for_dirt_perk }
				}
				add = {
					value = scope:councillor.spymaster_find_secrets_perk_monthly_increase
					desc = ESPIONAGE_PERK_BONUS_VALUE
				}
			}
		}
		if = {
			limit = {
				councillor_liege_has_erudition_legacy_5_perk = yes
			}
			add = {
				value = scope:councillor_liege.spymaster_find_secrets_erudition_monthly_increase
				desc = ERUDITION_DYNASTY_PERK_BONUS_VALUE
			}
		}
		if = {
			limit = {
				councillor_liege_has_family_business_bonus = yes
			}
			add = {
				value = scope:councillor_liege.spymaster_find_secrets_family_business_monthly_increase
				desc = FAMILY_BUSINESS_BONUS_VALUE
			}
		}
		if = {
			limit = {
				councillor_liege_has_consulted_house_bonus = yes
			}
			add = {
				value = scope:councillor_liege.spymaster_find_secrets_consulted_house_monthly_increase
				desc = CONSULTED_HOUSE_BONUS_VALUE
			}
		}
		#Events
		if = {
			limit = {
				scope:councillor_liege = { has_character_flag = court_of_no_secrets }
			}
			add = {
				value = spymaster_court_of_no_secrets_event_increase
				desc = COURT_OF_NO_SECRETS_VALUE
			}
		}
		if = {
			limit = {
				scope:councillor_liege = { has_character_modifier = court_without_secrets_modifier }
			}
			add = {
				value = spymaster_court_of_no_secrets_event_increase
				desc = court_without_secrets_modifier
			}
		}
		if = {
			limit = {
				exists = scope:target_character.cp:councillor_spymaster
				scope:target_character.cp:councillor_spymaster != scope:councillor_liege
				scope:target_character.cp:councillor_spymaster = {
					is_performing_council_task = task_disrupt_schemes
				}	
			}
			multiply = {
				value = scope:target_character.cp:councillor_spymaster.spymaster_find_secrets_disrupt_schemes_modifier_factor
				desc = TARGET_SPYMASTER_disrupt_schemes
			}
		}
		if = {
			limit = {
				exists = scope:target_character
				target_is_liege_or_above = scope:target_character
			}
			multiply = {
				value = 0.5
				desc = TARGET_IS_LIEGE_penalty
			}
		}
		if = {
			limit = {
				exists = scope:target_character
				target_is_vassal_or_below = scope:target_character
				has_character_modifier = servant_informants_modifier
			}
			multiply = {
				value = 2
				desc = SPYMASTER_FIND_SECRETS_CHARACTER_MODIFIERS
			}
		}
		# Lady in Waiting
		if = {
			limit = {
				scope:councillor_liege = { has_character_modifier = employer_booner_lady_in_waiting_2_modifier }
				OR = {
					target_is_vassal_or_below = scope:target_character
					this = scope:target_character
				}
			}
			add = {
				value = 10
				desc = employer_booner_lady_in_waiting_2_modifier
			}
		}
		else_if = {
			limit = {
				scope:councillor_liege = { has_character_modifier = employer_booner_lady_in_waiting_3_modifier }
				OR = {
					target_is_vassal_or_below = scope:target_character
					this = scope:target_character
				}
			}
			add = {
				value = 10
				desc = employer_booner_lady_in_waiting_3_modifier
			}
		}
		# Determination House Aspiration
		if = {
			limit = {
				scope:councillor_liege.house ?= { has_house_power_parameter = determination_spymaster_task_find_secrets_efficiency }
			}
			add = {
				value = spymaster_determination_house_aspiration_find_secrets_total
				desc = determination_house_aspiration_find_secrets_modifier
			}
		}
	}
	
	is_shown = {
		scope:councillor_liege = {
			trigger_if = {
				limit = {
					is_ai = yes
				}
				ai_honor <= 0
				scope:councillor.intrigue >= 15
				static_group_filter = {
					group = spy_block
					match = 0.75
				}
			}
		}
	}

	valid_target_court = {
		scope:councillor_liege = {
			trigger_if = {
				limit = {
					is_ai = yes
				}
				NOR = {
					has_relation_friend = scope:target_character
					has_relation_lover = scope:target_character
					is_allied_to = scope:target_character
					has_hook = scope:target_character
				}
				scope:target_character = {
					OR = {
						is_vassal_of = scope:councillor_liege
						has_relation_rival = scope:councillor_liege
					}
					in_diplomatic_range = scope:councillor_liege
					highest_held_title_tier >= tier_county
				}
			}
			trigger_else = {
				scope:target_character = {
					in_diplomatic_range = scope:councillor_liege
					highest_held_title_tier >= tier_county
				}
			}
		}
	}

	potential_target_court = {
		scope:target_character = {
			is_alive = yes
			is_landed = yes
			custom_description = {
				text = spymaster_will_not_find_secrets_at_own_court
				this != scope:councillor
			}
		}
	}

	on_start_task_court = {
		#spawn a task contract to help finding secrets
		if = {
			limit = {
				any_task_contract = {
					task_contract_type = laamp_help_find_secrets_contract
					NOT = {	task_contract_target = scope:councillor_liege }
				}
				any_player = {
					has_government = landless_adventurer_government
					is_within_diplo_range = { CHARACTER = scope:councillor }
					any_character_task_contract = {
						task_contract_type = laamp_help_find_secrets_contract
						count <= council_task_contracts_limit_value
					}
					can_create_task_contract = {
						type_name = laamp_help_find_secrets_contract
						employer = scope:councillor
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_help_find_secrets_contract
					task_contract_tier = scope:councillor.task_contract_tier_value
					location = scope:councillor.location
					task_contract_employer = scope:councillor
					target = scope:councillor_liege
				}
			}
		}
	}

	on_finish_task_court = {
		scope:councillor_liege = {
			if = {
				limit = {
					scope:target_character = {
						OR = {
							spymaster_task_find_secrets_court_has_minor_secrets_trigger = yes
							spymaster_task_find_secrets_court_has_major_secrets_trigger = yes
						}
					}
				}
				random = {
					chance = scope:councillor.spymaster_find_secrets_secret_discovery_chance
					save_scope_value_as = {
						name = having_find_secrets_event
						value = yes
					}
					trigger_event = {
						on_action = task_find_secrets_outcome
					}
				}
			}
			if = {
				limit = {
					has_perk = truth_is_relative_perk
					NOT = { exists = scope:having_find_secrets_event }
				}
				random = {
					chance = scope:councillor.spymaster_find_secrets_secret_discovery_chance
					save_scope_value_as = {
						name = having_find_secrets_event
						value = yes
					}
					trigger_event = spymaster_task.0381 # Fabricate a Hook (if you have Truth is Relative)
				}
			}
			if = {
				limit = {
					NOT = { exists = scope:having_find_secrets_event }
				}
				trigger_event = spymaster_task.0399
			}
			random = {
				chance = 10
				add_character_flag = {
					flag = no_ai_find_secrets
					years = 3
				}
			}
		}
	}

	monthly_on_action = task_find_secrets_side_effects
	
	ai_will_do = {
		value = 2
		if = { # Greedy AI's like spying on their subjects
			limit = {
				scope:councillor_liege.ai_greed > 0
			}
			add = {
				value = scope:councillor_liege.ai_greed
				divide = 2
			}
		}
		if = { # AI's with Golden Obligations really like this
			limit = {
				scope:councillor_liege = {
					has_perk = golden_obligations_perk
				}
			}
			add = 25
		}
		if = { # AI's with certain focuses enjoy this
			limit = {
				scope:councillor_liege = {
					OR = {
						has_focus = intrigue_skulduggery_focus
						has_focus = intrigue_intimidation_focus
						has_focus = martial_authority_focus
					}
				}
			}
			add = 50
		}
		if = { # Honorable AI's dislike spying on their subjects
			limit = {
				scope:councillor_liege.ai_honor > 0
			}
			add = {
				value = scope:councillor_liege.ai_honor
				multiply = -1
			}
		}
		if = { # Compassionate AI's dislike spying on their subjects
			limit = {
				scope:councillor_liege.ai_compassion > 0
			}
			add = {
				value = scope:councillor_liege.ai_compassion
				multiply = -0.5
			}
		}
		if = {
			# Honest or trusting AI's just don't do this at all
			limit = {
				scope:councillor_liege = {
					OR = {
						has_trait = honest
						has_trait = trusting
					}
				}
			}
			multiply = 0
		}
		if = {
			# Don't do this too often
			limit = {
				scope:councillor_liege = { has_character_flag = no_ai_find_secrets }
			}
			multiply = 0
		}
	}
}
