#Events related to the Spymaster's tasks

namespace = spymaster_task


####
# 0001-0999: Task outcome events
# by Petter Vilberg
####
# 0301 - Find Minor Secret - Hidden setup!
# 0302 - Find Major Secret - Hidden setup!

# 0341-0359 - Reveal secret

# 0399 - Spymaster found nothing


####
# 1000-1999: Side effects from Disrupt Schemes
# by Petter Vilberg
####
# 1001-1002 - Spymaster Gives Up Your Secret
# 1003-1004 - Spymaster Gives Up a Courtier's Secret
# 1005 - Spymaster Giving Up Secret: Follow-up for Forgiving

# 1101 - Hostile Scheme Interrupted


####
# 2000-2999: Side effects from Support Schemes
# by Petter Vilberg
####
# 2001 - Loss of Piety
# 2002 - Loss of Prestige
# 2003 - Scheme Fails

# 2101-2103 - Discover a Minor Secret
# 2104 - 2106 - Discover a Major Secret


####
# 3000-3999: Side effects from Espionage
# by Petter Vilberg
####
### Own vassal targets:
# 3001 - Snooping on your vassal causes opinion loss
# 3002 - Your Liege has been and continues to snoop on you
# 3003 - Your Liege has been snooping, but promises to stop

### Own liege target:
# 3004 - Snooping on your liege causes opinion loss
# 3005 - Your vassal has been and continues to snoop on you
# 3006 - Your Vassal has been snooping, but promises to stop

### Foreign Ruler target
# 3007 - Snooping on a foreign ruler causes opinion loss
# 3008 - A Foreign Ruler has been and continues to snoop on you
# 3009 - A Foreign Ruler has been snooping, but promises to stop

# Foreign Ruler's Vassal target
# 3010 - Snooping on a foreign vassal causes opinion loss
# 3011 - A Foreign Ruler has been and continues to snoop on you
# 3012 - A Foreign Ruler has been snooping, but promises to stop



##################
# Task Outcome Events
# 0001-0999
##################

#######
# Espionage
# 0301-0399
#  by Petter Vilberg
#######

#Pick a minor secret to reveal
spymaster_task.0301 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:target_character = {
			spymaster_task_find_secrets_court_has_minor_secrets_trigger = yes		
		}
	}

	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		scope:target_character = {
			#Find any secrets from the court owner
			if = {
				limit = {
					any_secret = {
						save_temporary_scope_as = illegal_or_shunned_secret
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
					}
				}
				every_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes	
					}
					add_to_temporary_list = suitable_secrets
				}
			}
			#Find any secrets from courtiers or guests
			every_courtier_or_guest = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
					}	
				}
				every_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
						NOT = { is_in_list = suitable_secrets }
					}
					add_to_temporary_list = suitable_secrets
				}
			}
			#Find any secrets from direct vassals
			every_vassal = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
					}
				}
				every_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
						NOT = { is_in_list = suitable_secrets }
					}
					add_to_temporary_list = suitable_secrets
				}
			}
		}

		#Pick a Secret to discover
		random_in_list = {
			list = suitable_secrets
			weight = {
				base = 1
				modifier = {
					add = 2
					secret_owner = {
						save_temporary_scope_as = secret_holder
					}
					spymaster_find_secrets_secret_holder_is_interesting_to_me_trigger = yes
				}
				modifier = {
					add = 1
					spymaster_find_secrets_secret_target_is_interesting_to_me_trigger = yes
				}
			}
			save_scope_as = secret_to_reveal
			secret_owner = {
				save_scope_as = secret_holder
			}
		}

		#Fire events
		scope:councillor_liege = {
			if = { #It's someone I care about
				limit = {
					OR = {
						spymaster_find_secrets_secret_holder_is_interesting_to_me_trigger = yes
						scope:secret_to_reveal = {
							spymaster_find_secrets_secret_target_is_interesting_to_me_trigger = yes
						}
					}
				}
				trigger_event = {
					on_action = task_find_secrets_reveal_selection
				}
			}
			else = { #For everyone else, send a feed message
				send_interface_message = {
					type = event_spymaster_task_good
					title = spymaster_task.0311.notification
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor = { intrigue <= average_skill_level }
							}
							desc = task_intrigue_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor = { intrigue > average_skill_level }
							}
							desc = task_intrigue_good_skilled_notification_tooltip
						}
					}
					
					left_icon = scope:secret_holder
					right_icon = scope:councillor
					goto = scope:secret_holder
					
					scope:secret_to_reveal = {
						reveal_to = scope:councillor_liege
					}
				}
			}
		}
		scope:councillor = {
			if = { #Check that the Spymaster doesn't already know the secret
				limit = {
					scope:secret_to_reveal = {
						NOT = { is_known_by = prev }
					}
				}
				if = { #It's the owner of the court or someone else I care about
					limit = { spymaster_find_secrets_secret_holder_is_interesting_to_me_trigger = yes }
					trigger_event = {
						on_action = task_find_secrets_reveal_selection
					}
				}
				else = { #For everyone else, send a feed message
					send_interface_message = {
						type = event_spymaster_task_good
						title = spymaster_task.0311.notification
						
						left_icon = scope:secret_holder
						goto = scope:secret_holder
						
						scope:secret_to_reveal = {
							reveal_to = scope:councillor
						}
					}
				}
			}
		}
	}
}

#Pick a major secret to reveal
spymaster_task.0302 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:target_character = {
			spymaster_task_find_secrets_court_has_major_secrets_trigger = yes
		}
	}

	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		scope:target_character = {
			#Find any secrets from the court owner
			if = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
					}
				}
				every_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes	
					}
					add_to_temporary_list = suitable_secrets
				}
			}
			#Find any secrets from courtiers
			every_courtier_or_guest = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
					}	
				}
				every_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
						NOT = { is_in_list = suitable_secrets }
					}
					add_to_temporary_list = suitable_secrets
				}
			}
			#Find any secrets from direct vassals
			every_vassal = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
					}
				}
				every_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
						NOT = { is_in_list = suitable_secrets }
					}
					add_to_temporary_list = suitable_secrets
				}
			}
		}

		#Pick a Secret to discover
		random_in_list = {
			list = suitable_secrets
			weight = {
				base = 1
				modifier = {
					add = 2
					secret_owner = {
						save_temporary_scope_as = secret_holder
					}
					spymaster_find_secrets_secret_holder_is_interesting_to_me_trigger = yes
				}
				modifier = {
					add = 1
					spymaster_find_secrets_secret_target_is_interesting_to_me_trigger = yes
				}
			}
			save_scope_as = secret_to_reveal
			secret_owner = {
				save_scope_as = secret_holder
			}
		}

		#Fire events
		scope:councillor_liege = {
			if = { #It's someone I care about
				limit = {
					OR = {
						spymaster_find_secrets_secret_holder_is_interesting_to_me_trigger = yes
						scope:secret_to_reveal = {
							spymaster_find_secrets_secret_target_is_interesting_to_me_trigger = yes
						}
					}
				}
				trigger_event = {
					on_action = task_find_secrets_reveal_selection
				}
			}
			else = { #For everyone else, send a feed message
				send_interface_message = {
					type = event_spymaster_task_good
					title = spymaster_task.0311.notification
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor = { intrigue <= average_skill_level }
							}
							desc = task_intrigue_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor = { intrigue > average_skill_level }
							}
							desc = task_intrigue_good_skilled_notification_tooltip
						}
					}
					
					left_icon = scope:secret_holder
					right_icon = scope:councillor
					goto = scope:secret_holder
					
					scope:secret_to_reveal = {
						reveal_to = scope:councillor_liege
					}
				}
			}
		}
		scope:councillor = {
			if = { #Check that the Spymaster doesn't already know the secret
				limit = {
					scope:secret_to_reveal = {
						NOT = { is_known_by = prev }
					}
				}
				if = { #It's the owner of the court or someone else I care about
					limit = {
						OR = {
							scope:secret_holder = {
								is_ruler = yes
							}
							spymaster_find_secrets_secret_holder_is_interesting_to_me_trigger = yes
							scope:secret_to_reveal = {
								spymaster_find_secrets_secret_target_is_interesting_to_me_trigger = yes
							}
						}
					}
					trigger_event = {
						on_action = task_find_secrets_reveal_selection
					}
				}
				else = { #For everyone else, send a feed message
					scope:councillor = {
						send_interface_message = {
							type = event_spymaster_task_good
							title = spymaster_task.0311.notification
							
							left_icon = scope:secret_holder
							goto = scope:secret_holder
							
							scope:secret_to_reveal = {
								reveal_to = scope:councillor
							}
						}
					}
				}
			}
		}
	}
}



#Spymaster found Secret Deviant
spymaster_task.0341 = {
	type = character_event
	title = spymaster_task.0341.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = deviant
						any_secret = {
							secret_type = secret_deviant
						}
					}
				}
				desc = spymaster_task.0311.secret_deviant.shared
			}
			desc = spymaster_task.0311.secret_deviant
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disgust
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_deviant
		}
	}

	immediate = {
	
	}

	option = {
		name = {
			text = spymaster_task.0341.a.deviant
			trigger = {
				OR = {
					has_trait = deviant
					any_secret = {
						secret_type = secret_deviant
					}
				}
			}
		}
		name = {
			trigger = {
				has_trait = callous
				has_trait = stubborn
				has_trait = sadistic
			}
			text = spymaster_task.0341.a.detached
		}
		name = spymaster_task.0341.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found Secret Sodomite
spymaster_task.0342 = {
	type = character_event
	title = spymaster_task.0342.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			#Sodomy is negative
			triggered_desc = {
				trigger = {
					OR = {
						trait_is_shunned_in_faith_trigger = {
							TRAIT = sodomite
							GENDER_CHARACTER = scope:secret_holder
							FAITH = root.faith
						}
						trait_is_criminal_in_faith_trigger = {
							TRAIT = sodomite
							GENDER_CHARACTER = scope:secret_holder
							FAITH = root.faith
						}
					}
				}
				desc = {
					first_valid = {
						#Root is ambivalent
						triggered_desc = {
							trigger = {			
								any_secret = {
									secret_type = secret_homosexual
								}
							}
							desc = spymaster_task.0311.secret_sodomy.illegal_shared
						}
						desc = spymaster_task.0311.secret_sodomy.illegal
					}
				}
				desc = spymaster_task.0311.sodomy.accepted
			}		
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disgust
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_homosexual
		}
	}

	option = {
		name = spymaster_task.0342.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found Secret Murder Attempt
spymaster_task.0343 = {
	type = character_event
	title = spymaster_task.0343.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = { scope:secret_to_reveal = { secret_target = root } }
				desc = spymaster_task.0311.secret_murder_attempt.targeting_me
			}
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_target = {
							OR = {
								is_of_major_interest_to_root_trigger = yes
								is_of_minor_interest_to_root_trigger = yes
							}
						}
					}
				}
				desc = spymaster_task.0311.secret_murder_attempt.close_to_me
			}
			desc = spymaster_task.0311.secret_murder_attempt.other
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disgust
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	lower_right_portrait = scope:murder_target
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_murder_attempt
		}
	}

	immediate = {
		scope:secret_to_reveal = {
			secret_target = {
				save_scope_as = murder_target
			}
		}
		if = {
			limit = {
				any_scheme = {
					scheme_type = murder
					scheme_target = scope:secret_holder
				}
			}
			random_scheme = {
				limit = {
					scheme_type = murder
					scheme_target = scope:secret_holder
				}
				save_scope_as = scheme
			}
		}
	}

	option = {
		name = spymaster_task.0343.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}

	option = {
		name = spymaster_task.0343.murder_back
		trigger = {
			exists = scope:scheme
		}
		exclusive = yes
		scope:secret_to_reveal = {
			reveal_to = root
		}
		scope:scheme = {
			add_scheme_progress = 1
		}
	}
}

#Spymaster found Secret Murder
spymaster_task.0344 = {
	type = character_event
	title = spymaster_task.0344.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					has_trait = sadistic
				}
				desc = spymaster_task.0311.secret_murder.sadistic
			}
			triggered_desc = {
				trigger = {
					has_trait = callous
				}
				desc = spymaster_task.0311.secret_murder.callous
			}
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_target = {
							OR = {
								is_of_major_interest_to_root_trigger = yes
								is_of_minor_interest_to_root_trigger = yes
							}
						}
					}
				}
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {
								has_trait = vengeful
							}
							desc = spymaster_task.0311.secret_murder.close_to_me.vengeful
						}
						desc = spymaster_task.0311.secret_murder.close_to_me
					}
					triggered_desc = {
						trigger = {
							OR = {
								has_trait = fickle
								has_trait = impatient
								has_trait = compassionate
							}
						}
						desc = spymaster_task.0311.secret_murder.close_to_me_regret
					}
				}
			}
			desc = spymaster_task.0311.secret_murder.other
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disgust
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	lower_right_portrait = scope:murder_target
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_murder
		}
	}

	immediate = {
		scope:secret_to_reveal = {
			secret_target = {
				save_scope_as = murder_target
			}
		}
		if = {
			limit = {
				any_scheme = {
					scheme_type = murder
					scheme_target = scope:secret_holder
				}
			}
			random_scheme = {
				limit = {
					scheme_type = murder
					scheme_target = scope:secret_holder
				}
				save_scope_as = scheme
			}
		}
	}

	option = {
		name = {
			text = spymaster_task.0344.a.sadistic
			trigger = {
				has_trait = sadistic
			}
		}
		name = {
			text = spymaster_task.0344.a.callous
			trigger = {
				has_trait = callous
			}
		}
		name = spymaster_task.0344.a
		scope:secret_to_reveal = {
			reveal_to_without_events_effect = {
				CHARACTER = root
			}
		}
	}

	option = {
		name = {
			text = spymaster_task.0344.a.sadistic
			trigger = {
				has_trait = sadistic
			}
		}
		name = spymaster_task.0344.b
		trigger = {
			exists = scope:scheme
		}
		exclusive = yes
		scope:secret_to_reveal = {
			reveal_to_without_events_effect = {
				CHARACTER = root
			}
		}
		scope:scheme = {
			add_scheme_progress = 1
		}
	}
}

#Spymaster found Secret Cannibal
spymaster_task.0345 = {
	type = character_event
	title = spymaster_task.0345.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = cannibal
						any_secret = {	
							secret_type = secret_cannibal
						}
					}
				}
				desc = spymaster_task.0311.secret_cannibal.shared
			}
			desc = spymaster_task.0311.secret_cannibal
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disgust
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_cannibal
		}
	}

	option = {
		name = {
			text = spymaster_task.0345.a.cannibal
			trigger = {
				has_trait = cannibal
			}
		}
		name = spymaster_task.0345.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found Secret Lover
spymaster_task.0346 = {
	type = character_event
	title = spymaster_task.0346.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			#Lover Secret
			triggered_desc = {
				trigger = {
					scope:secret_holder = { is_consort_of = root }
				}
				desc = spymaster_task.0311.secret_lover.consort
			}
			triggered_desc = {
				trigger = {
					scope:lover = {
						is_consort_of = root
					}
				}
				desc = spymaster_task.0311.secret_lover.consort_is_lover
			}
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_type = secret_lover
					}
					scope:lover = {
						is_of_major_interest_to_root_trigger = yes
					}
				}
				desc = spymaster_task.0311.secret_lover.lover_close_to_me
			}
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_type = secret_lover
					}
					has_trait = lustful
				}
				desc = spymaster_task.0311.secret_lover.lustful
			}
			desc = spymaster_task.0311.secret_lover
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:secret_holder
		animation = love
	}
	right_portrait = {
		character = scope:lover
		animation = flirtation
	}
	lower_right_portrait = scope:councillor
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_lover
			exists = secret_target
		}
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					exists = scope:lover
				}
			}
			scope:secret_to_reveal = {
				secret_target = {
					save_scope_as = lover
				}
			}
		}
	}

	option = {
		name = {
			text = spymaster_task.0346.a.consort
			trigger = {
				OR = {
					scope:secret_holder = { is_consort_of = root }
					scope:lover = { is_consort_of = root }
				}
			}
		}
		name = {
			text = spymaster_task.0346.a.close
			trigger = {
				OR = {
					scope:secret_holder = {
						is_close_family_of = root
					}
					scope:lover = {
						is_close_family_of = root
					}
				}
			}
		}
		name = spymaster_task.0346.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found Secret Unbeliever
spymaster_task.0347 = {
	type = character_event
	title = spymaster_task.0347.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = { faith = scope:secret_holder.faith }
				}
				desc = spymaster_task.0311.secret_non_believer.other_faith
			}
			triggered_desc = {
				trigger = {
					OR = {
						has_character_modifier = known_non_believer
						any_secret = {	
							secret_type = secret_non_believer
						}
					}
				}
				desc = spymaster_task.0311.secret_non_believer.shared
			}
			desc = spymaster_task.0311.secret_non_believer
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	right_portrait = {
		character = scope:secret_holder
		animation = dismissal
	}
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_non_believer
		}
	}

	immediate = {
	
	}

	option = {
		name = spymaster_task.0347.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found Secret Incest
spymaster_task.0348 = {
	type = character_event
	title = spymaster_task.0348.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					has_negative_attitude_towards_trait_trigger = { TRAIT = incestuous GENDER_CHARACTER = scope:secret_holder }
				}
				desc = spymaster_task.0311.secret_incest
			}
			desc = spymaster_task.0311.secret_incest.shared #Less condemning for not illegal/shared secret
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = disgust
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shock
	}
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_incest
		}
	}

	option = {
		name = {
			trigger = {
				OR = {
					has_trait = incestuous
					any_secret = {
						secret_type = secret_incest
					}
				}
			}
			text = spymaster_task.0348.a.shared
		}
		name = {
			trigger = {
				OR = {
					has_trait = lustful
					is_deviant_trigger = yes
				}
			}
			text = spymaster_task.0348.a.lustful
		}
		name = spymaster_task.0348.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found Secret Witch
spymaster_task.0349 = {
	type = character_event
	title = spymaster_task.0349.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = witch
						any_secret = {
							secret_type = secret_witch
						}
					}
				}
				desc = spymaster_task.0311.secret_witch.shared
			}
			triggered_desc = {
				trigger = {
					OR = {
						has_trait = zealous
						has_trait = lustful
					}
				}
				desc = {
					desc = spymaster_task.0311.secret_witch.zealous
					triggered_desc = {
						trigger = {
							has_trait = zealous
						}
						desc = spymaster_task.0311.secret_witch.zealous_end
					}
				}
			}
			desc = spymaster_task.0311.secret_witch
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shock
	}
	right_portrait = {
		character = scope:secret_holder
		animation = dismissal
	}
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_witch
		}
	}

	option = {
		name = {
			trigger = {
				OR = {
					has_trait = zealous
					has_trait = lustful
				}
			}
			text = spymaster_task.0349.a.zealous
		}
		name = spymaster_task.0349.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster finds a person who is a bastard
spymaster_task.0350 = {
	type = character_event
	title = spymaster_task.0350.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			#Target is Council Owner's Bastard
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_target.real_father = root
					}
				}
				desc = spymaster_task.0311.secret_bastard.i_am_real_father
			}
			#Target is NOT Council Owner's after all
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						OR = {
							secret_type = secret_disputed_heritage
							secret_type = secret_unmarried_illegitimate_child
						}
						secret_target.father = root
					}
				}
				desc = spymaster_task.0311.secret_bastard.i_am_not_father
			}
			#Target is Bastard
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						OR = {
							secret_type = secret_disputed_heritage
							secret_type = secret_unmarried_illegitimate_child
						}
						secret_target = scope:secret_holder
					}
				}
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {
								scope:secret_to_reveal = {
									secret_target.real_father = {
										is_of_major_interest_to_root_trigger = yes
									}
								}
							}
							desc = spymaster_task.0311.secret_bastard.father_is_interesting
						}
						desc = spymaster_task.0311.secret_bastard
					}
				}
			}
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shock
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	lower_right_portrait = scope:actual_father
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_disputed_heritage
			secret_target = scope:secret_holder
		}
	}

	immediate = {
		scope:secret_to_reveal = {
			secret_target = {
				save_scope_as = bastard
			}
			secret_target.real_father = {
				save_scope_as = actual_father
			}
			secret_target.mother = {
				save_scope_as = mother
			}
		}
	}

	option = {
		name = {
			trigger = {
				OR = {
					scope:actual_father = root
					scope:secret_holder.father = root
				}
			}
			text = spymaster_task.0350.a.related
		}
		name = spymaster_task.0350.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster finds a bastard
spymaster_task.0351 = {
	type = character_event
	title = spymaster_task.0350.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			#Target is Council Owner's Bastard
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_target.real_father = root
					}
				}
				desc = spymaster_task.0311.secret_bastard.i_am_real_father
			}
			#Target is NOT Council Owner's after all
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						OR = {
							secret_type = secret_disputed_heritage
							secret_type = secret_unmarried_illegitimate_child
						}
						secret_target.father = root
					}
				}
				desc = spymaster_task.0311.secret_bastard.i_am_not_father
			}
			#Target is Bastard
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						OR = {
							secret_type = secret_disputed_heritage
							secret_type = secret_unmarried_illegitimate_child
						}
						secret_target = scope:secret_holder
					}
				}
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = {
								scope:secret_to_reveal = {
									secret_target.real_father = {
										is_of_major_interest_to_root_trigger = yes
									}
								}
							}
							desc = spymaster_task.0311.secret_bastard.father_is_interesting
						}
						desc = spymaster_task.0311.secret_bastard
					}
				}
			}
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shock
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	lower_right_portrait = scope:bastard

	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_disputed_heritage
		}
	}

	immediate = {
	
	}

	option = {
		name = {
			trigger = {
				OR = {
					scope:actual_father = root
					scope:secret_holder.father = root
				}
			}
			text = spymaster_task.0350.a.related
		}
		name = spymaster_task.0350.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster finds father of a known bastard
spymaster_task.0352 = {
	type = character_event
	title = spymaster_task.0350.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
		first_valid = {
			#Target is Council Owner's Bastard
			triggered_desc = {
				trigger = {
					scope:secret_to_reveal = {
						secret_target.real_father = root
					}
				}
				desc = spymaster_task.0311.secret_bastard.i_am_real_father
			}
			desc = spymaster_task.0311.secret_bastard
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shock
	}
	right_portrait = {
		character = scope:secret_holder
		animation = worry
	}
	lower_right_portrait = scope:actual_father
	lower_left_portrait = scope:bastard
	
	trigger = {
		scope:secret_to_reveal = {
			secret_type = secret_unmarried_illegitimate_child
		}
	}

	immediate = {
		scope:secret_to_reveal = {
			secret_target = {
				save_scope_as = bastard
			}
			secret_target.real_father = {
				save_scope_as = actual_father
			}
			secret_target.mother = {
				save_scope_as = mother
			}
		}
	}

	option = {
		name = {
			trigger = {
				OR = {
					scope:actual_father = root
					scope:secret_holder.father = root
				}
			}
			text = spymaster_task.0350.a.related
		}
		name = spymaster_task.0350.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

#Spymaster found something - FALLBACK
spymaster_task.0359 = {
	type = character_event
	title = spymaster_task.0359.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					root = scope:councillor_liege
				}
				desc = spymaster_task.0311.councillor_liege_opening
			}
			desc = spymaster_task.0311.spymaster_opening
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shock
	}
	right_portrait = {
		character = scope:secret_holder
		animation = shame
	}
	
	trigger = {
		exists = scope:secret_to_reveal
	}

	option = {
		name = spymaster_task.0359.a
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}


################################
# Fabricate a Hook on a Courtier
# by Petter Vilberg
################################
scripted_trigger spymaster_task_courtier_valid_for_hook_fabrication_trigger = {
	is_available_ai_adult = yes
	save_temporary_scope_as = target_check
	NOR = {
		this = scope:councillor
		root = {
			has_hook = scope:target_check
		}
		root = {
			any_scheme = {
				scheme_target = scope:target_character
				scheme_is_character_agent = scope:target_check
			}
		}
		any_secret = { # Should prioritise actually finding Secrets
			is_known_by = root
		}
	}
}


spymaster_task.0381 = {
	type = letter_event
	opening = spymaster_task.0381.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target_character = root
				}
				desc = spymaster_task.0381.desc_your_court
			}
			desc = spymaster_task.0381.desc_other_court
		}
		desc = spymaster_task.0381.desc
	}
	sender = scope:councillor
	
	trigger = {
		NOT = { has_game_rule = fake_news_off }
		has_perk = truth_is_relative_perk
		scope:target_character = {
			any_courtier_or_guest = {
				spymaster_task_courtier_valid_for_hook_fabrication_trigger = yes
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 2
			any_scheme = {
				scheme_target = scope:target_character
				is_hostile = yes
			}
		}
	}

	immediate = {
		scope:target_character = {
			random_courtier_or_guest = {
				limit = {
					spymaster_task_courtier_valid_for_hook_fabrication_trigger = yes
				}
				weight = {
					base = 1
					modifier = { # Prioritise actual Courtiers
						add = 3
						is_courtier_of = scope:target_character
					}
				}
				save_scope_as = character_to_hook
			}
		}
	}

	option = {
		name = spymaster_task.0381.a
		custom_tooltip = spymaster_task.0381.a.truth_is_relative_unlock
		add_hook = {
			target = scope:character_to_hook
			type = fabrication_hook
		}
		remove_short_term_gold = medium_gold_value
		stress_impact = {
			honest = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = -1
				ai_greed = -0.5
			}
		}
	}

	option = {
		name = spymaster_task.0381.b
		flavor = spymaster_task.0381.b.flavor
		scope:character_to_hook = {
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 30
			}
		}
		ai_chance = {
			base = 20
			ai_value_modifier = {
				ai_compassion = 0.5
			}
		}
	}
}




#Spymaster finds nothing
spymaster_task.0399 = {
	type = letter_event
	opening = {
		desc = spymaster_task.0399.opening
	}
	desc = {
		desc = spymaster_task.0399.desc
		first_valid = {
			triggered_desc = {
				trigger = { root = scope:target_character }
				desc = spymaster_task.0399.home_court
			}
			desc = spymaster_task.0399.other_court
		}
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:no_secrets_here }
				desc = spymaster_task.0399.nothing_to_find
			}
			desc = spymaster_task.0399.something_here
		}
	}
	sender = scope:councillor

	weight_multiplier = {
		base = 1
		compare_modifier = {
			trigger = {
				scope:councillor = {
					intrigue < medium_skill_rating
				}	
			}
			target = scope:councillor
			value = intrigue
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		#Calculate whether Spymaster thinks there are secrets to be found
		random_list = {
			5 = {
				modifier = {
					add = scope:councillor.intrigue
					scope:target_character = {
						OR = {
							spymaster_task_find_secrets_court_has_minor_secrets_trigger = yes
							spymaster_task_find_secrets_court_has_major_secrets_trigger = yes
						}
					}
				}
				save_scope_value_as = {
					name = secrets_to_be_found
					value = yes
				}
			}
			5 = {
				modifier = {
					add = scope:councillor.intrigue
					scope:target_character = {
						NOR = {
							spymaster_task_find_secrets_court_has_minor_secrets_trigger = yes
							spymaster_task_find_secrets_court_has_major_secrets_trigger = yes
						}
					}
				}
				save_scope_value_as = {
					name = no_secrets_here
					value = yes
				}
			}
		}
	}

	#Set Spymaster to default task
	option = {
		name = {
			trigger = {
				exists = scope:no_secrets_here
			}
			text = spymaster_task.0399.a.recommended
		}
		name = {
			trigger = {
				exists = scope:secrets_to_be_found
			}
			text = spymaster_task.0399.a.counter
		}
		hidden_effect = {
			scope:councillor = {
				start_default_task = yes	
			}
		}
		custom_tooltip = spymaster_task.0399.a.tt
		ai_chance = {
			base = 80
			modifier = {
				factor = 0.2
				exists = scope:secrets_to_be_found
			}
		}
	}

	#I am not convinced, keep it up
	option = {
		name = {
			trigger = {
				exists = scope:secrets_to_be_found
			}
			text = spymaster_task.0399.b.recommended
		}
		name = {
			trigger = {
				exists = scope:no_secrets_here
			}
			text = spymaster_task.0399.b.counter
		}
		custom_tooltip = spymaster_task.0399.b.tt
		ai_chance = {
			base = 100
			modifier = {
				add = 100
				has_trait = paranoid
			}
			modifier = {
				factor = 0.3
				exists = scope:no_secrets_here
			}
		}
	}
}



#####################
# Side Effects from Disrupt Schemes
# 1000-1999
#####################

###Bad Side Effects

#Spymaster gives up one of your secrets... - chain
# 1001-1002
spymaster_task.1001 = {
	hidden = yes
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		any_vassal = {
			is_powerful_vassal = yes
			NOT = { this = scope:councillor }
			save_temporary_scope_as = vassal
		}
		any_secret = {
			NOT = { is_known_by = scope:vassal }
		}
		scope:councillor = {
			intrigue < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		random_vassal = {
			limit = {
				is_powerful_vassal = yes
				NOT = { this = scope:councillor }
				save_temporary_scope_as = vassal
				root = {
					any_secret = {
						NOT = { is_known_by = scope:vassal }
					}
				}
			}
			save_scope_as = vassal
		}
		random_secret = {
			limit = {
				NOT = { is_known_by = scope:vassal }
			}
			save_scope_as = secret_to_reveal
		}
		scope:vassal = { #Fire the revelation event for the vassal
			trigger_event = spymaster_task.1002
		}

		if = {
			limit = {
				OR = {
					NOT = { has_trait = forgiving }
					has_hook_of_type = {
						target = scope:councillor
						type = favor_hook
					}
				}
			}
			send_interface_message = {
				type = event_spymaster_task_bad
				title = spymaster.1001.notification
				
				left_icon = scope:councillor

				desc = {
					desc = spymaster_task.1001.a.tt
					triggered_desc = {
						trigger = {
							scope:active_councillor = { intrigue <= average_skill_level }
						}
						desc = task_intrigue_bad_unskilled_notification_tooltip
					}
					triggered_desc = {
						trigger = {
							scope:active_councillor = { intrigue > average_skill_level }
						}
						desc = task_intrigue_bad_skilled_notification_tooltip
					}
				}
				add_opinion = {
					target = scope:councillor
					modifier = respect_opinion
					opinion = -20
				}
			}
		}
		else = {
			trigger_event = spymaster_task.1005
		}
	}

}


#Vassal gets to know about the secret
spymaster_task.1002 = {
	type = letter_event
	opening = {
		desc = spymaster_task.1002.greeting
	}
	desc = spymaster_task.1002.desc
	sender = scope:councillor
	
	option = {
		name = spymaster_task.1002.a
		flavor = spymaster_task.1002.a.tt
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

### END Spymaster giving up secret chain

#Spymaster gives up a courtier's secret... - chain
# 1003-1004
scripted_trigger spymaster_task_1003_courtier_secret_trigger = {
	NOT = { is_known_by = scope:vassal }
	is_known_by = scope:councillor_liege
}

scripted_trigger spymaster_task_1003_courtier_trigger = {
	NOT = { this = scope:councillor }
	any_secret = {
		spymaster_task_1003_courtier_secret_trigger = yes
	}
}

spymaster_task.1003 = {
	hidden = yes
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		any_vassal = {
			is_powerful_vassal = yes
			save_temporary_scope_as = vassal
		}
		any_courtier = {
			spymaster_task_1003_courtier_trigger = yes
		}
		scope:councillor = {
			intrigue < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		random_vassal = {
			limit = {
				is_powerful_vassal = yes
				save_temporary_scope_as = vassal
				root = {
					any_courtier = {
						spymaster_task_1003_courtier_trigger = yes
					}
				}
			}
			save_scope_as = vassal
		}
		random_courtier = {
			limit = {
				spymaster_task_1003_courtier_trigger = yes
			}
			random_secret = {
				limit = {
					spymaster_task_1003_courtier_secret_trigger = yes
				}
				save_scope_as = secret_to_reveal
			}
			save_scope_as = courtier
		}
		scope:vassal = { #Fire the revelation event for the vassal
			trigger_event = spymaster_task.1004
		}

		send_interface_message = {
			type = event_spymaster_task_bad
			title = spymaster_task.1003.notification

			right_icon = scope:courtier
			left_icon = scope:councillor

			desc = {
				desc = spymaster_task.1003.a.tt
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue <= average_skill_level }
					}
					desc = task_intrigue_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue > average_skill_level }
					}
					desc = task_intrigue_bad_skilled_notification_tooltip
				}
			}
			add_opinion = {
				target = scope:councillor
				modifier = respect_opinion
				opinion = -10
			}
		}
	}

}


#Vassal gets to know about the secret
spymaster_task.1004 = {
	type = letter_event
	opening = {
		desc = spymaster_task.0399.opening
	}
	desc = spymaster_task.1002.desc
	sender = scope:councillor
	
	option = {
		name = spymaster_task.1002.a
		custom_tooltip = spymaster_task.1002.a.tt
		scope:secret_to_reveal = {
			reveal_to = root
		}
	}
}

### END Spymaster giving up secret chain

#Spymaster Giving Up Secret: Follow-up for Forgiving
spymaster_task.1005 = {
	type = character_event
	title = spymaster_task.1001.t
	desc = spymaster_task.1001.desc
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shame
	}

	option = { #How could you ever!
		name = spymaster_task.1001.a
		custom_tooltip = spymaster_task.1001.a.tt
		add_opinion = {
			target = scope:councillor
			modifier = respect_opinion
			opinion = -20
		}
	}

	option = { #We all make mistake s
		name = spymaster_task.1001.b
		trigger = {
			has_trait = forgiving
		}
		trait = forgiving
		custom_tooltip = spymaster_task.1001.a.tt
		if = {
			limit = {
				can_add_hook = {
					type = favor_hook
					target = scope:councillor
				}
			}
			add_hook = {
				type = favor_hook
				target = scope:councillor
			}
		}
	}
}


###Good Side Effects

#Hostile Scheme Interrupted

scripted_trigger spymaster_task_1101_scheme_trigger = {
	hostile_scheme_trigger = yes
	scheme_owner = { is_ai = yes }
	scheme_progress >= 20
	scheme_power_resistance_difference > 0
}


spymaster_task.1101 = {
	type = letter_event
	opening = {
		desc = spymaster_task.0399.opening
	}
	desc = {
		desc = spymaster_task.1101.desc_opening
		first_valid = {
			triggered_desc = {
				trigger = { scope:scheme = { scheme_type = murder } }
				desc = spymaster_task.1101.murder
			}
			triggered_desc = {
				trigger = { scope:scheme = { scheme_type = abduct } }
				desc = spymaster_task.1101.abduct
			}
			desc = spymaster_task.1101.fallback
		}
		desc = spymaster_task.1101.ending
	}
	sender = scope:councillor
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		any_targeting_scheme = {
			spymaster_task_1101_scheme_trigger = yes
		}
		scope:councillor = {
			councillor_positive_event_opinion_trigger = yes
			intrigue >= medium_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		random_targeting_scheme = {
			limit = {
				spymaster_task_1101_scheme_trigger = yes
			}
			save_scope_as = scheme
			scheme_owner = {
				save_scope_as = owner
			}
			if = {
				limit = {
					is_scheme_exposed = no
				}
				hidden_effect = {
					expose_scheme = yes
				}
				custom_tooltip = spymaster_task.1101.exposure_tt
			}
		}
	}

	option = {
		name = spymaster_task.1101.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}


#####################
# Side Effects from Support Schemes
# 2000-2999
#####################


###Bad Side Effects


#Loss of Piety
# by Petter Vilberg

spymaster_task.2001 = {
	type = character_event
	title = spymaster_task.2001.t
	desc = spymaster_task.2001.desc
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = worry
	}
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:councillor = {
			intrigue < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		set_variable = {
			name = had_spymaster_task_side_effect
			value = yes
			days = spymaster_task_side_effect_cooldown
		}
	}

	option = {
		name = spymaster_task.2001.a
		add_piety = medium_piety_loss
	}

	option = {
		name = spymaster_task.2001.b
		add_piety = major_piety_loss
		add_dread = medium_dread_gain
		stress_impact = {
			zealous = medium_stress_impact_gain
			just = major_stress_impact_gain
			compassionate = major_stress_impact_gain
		}
		ai_chance = {
			base = 0
			modifier = {
				add = 20
				has_trait = arbitrary
			}
			modifier = {
				add = 20
				has_trait = callous
			}
			modifier = {
				add = 100
				has_trait = sadistic
			}
			modifier = {
				add = 20
				has_trait = cynical
			}
		}
	}
}


#Loss of Prestige
# by Petter Vilberg

spymaster_task.2002 = {
	hidden = yes
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:councillor = {
			intrigue < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_spymaster_task_side_effect
			value = yes
			days = spymaster_task_side_effect_cooldown
		}

		send_interface_message = {
			type = event_spymaster_task_bad
			title = spymaster_task.2002.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue <= average_skill_level }
					}
					desc = task_intrigue_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue > average_skill_level }
					}
					desc = task_intrigue_bad_skilled_notification_tooltip
				}
			}
			
			left_icon = scope:councillor

			add_prestige = medium_prestige_loss
		}
	}
}


#Scheme Fails
# by Petter Vilberg

scripted_trigger spymaster_task_2003_scheme_trigger = {
	hostile_scheme_trigger = yes
	scheme_progress >= 50
}

spymaster_task.2003 = {
	type = character_event
	title = spymaster_task.2003.t
	desc = spymaster_task.2003.desc
	theme = intrigue
	left_portrait = {
		character = scope:councillor
		animation = shock
	}
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:councillor = {
			intrigue < high_skill_rating
		}
		any_scheme = {
			spymaster_task_2003_scheme_trigger = yes
		}
	}
	
	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		random_scheme = {
			limit = {
				spymaster_task_2003_scheme_trigger = yes
			}
			save_scope_as = scheme
		}
	}

	option = {
		name = spymaster_task.2003.a
		if = {
			limit = { exists = scope:scheme }
			scope:scheme = {
				end_scheme = yes
			}
		}
	}
}



###Good Side Effects

#Discovery of Minor Secrets Chain
# 1101-1103
# by Petter Vilberg
spymaster_task.2101 = {
	hidden = yes
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:councillor = {
			councillor_positive_event_opinion_trigger = yes
			intrigue >= inverted_mediocre_skill_rating
		}
		scope:councillor_liege = {
			any_courtier = {
				any_secret = {
					spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
				}
			}
			any_vassal = {
				any_secret = {
					spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		scope:councillor_liege = {
			every_courtier = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
					}
				}
				add_to_temporary_list = potential_secret_holder
			}
			every_vassal = {
				limit = {
					OR = {
						root = { highest_held_title_tier = tier_county }
						highest_held_title_tier = tier_county
					}
					any_secret = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
					}
				}
				add_to_temporary_list = potential_secret_holder
			}
			random_in_list = {
				list = potential_secret_holder
				save_scope_as = secret_owner
				random_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_minor_secret_trigger = yes
					}
					save_scope_as = secret_to_reveal
				}
			}
		}
		scope:councillor = {
			trigger_event = spymaster_task.2102
		}
	}
}

spymaster_task.2102 = {
	type = character_event
	title = spymaster_task.2102.t
	desc = spymaster_task.2102.desc
	theme = intrigue
	left_portrait = {
		character = scope:secret_owner
		animation = worry
	}

	trigger = {
		exists = scope:secret_to_reveal
	}

	option = {
		name = spymaster_task.2102.a
		trigger = {
			scope:secret_to_reveal = {
				NOT = { is_known_by = root }
			}
		}
		scope:secret_to_reveal = {
			reveal_to = root
		}
		show_as_tooltip = {
			scope:secret_to_reveal = {
				reveal_to = scope:councillor_liege
			}
		}
		hidden_effect = {
			scope:councillor_liege = {
				trigger_event = spymaster_task.2103
			}
		}
		ai_chance = {
			base = 200
			opinion_modifier = {
				opinion_target = scope:councillor_liege
			}
		}
	}

	option = {
		name = spymaster_task.2102.b
		trigger = {
			scope:secret_to_reveal = {
				is_known_by = root
			}
		}
		show_as_tooltip = {
			scope:secret_to_reveal = {
				reveal_to = scope:councillor_liege
			}
		}
		hidden_effect = {
			scope:councillor_liege = {
				trigger_event = spymaster_task.2103
			}
		}
		ai_chance = {
			base = 100
			opinion_modifier = {
				opinion_target = scope:councillor_liege
			}
		}
	}

	option = {
		name = spymaster_task.2102.c
		scope:secret_to_reveal = {
			if = {
				limit = {
					NOT = { is_known_by = root }
				}
				reveal_to = root
			}
		}
		custom_tooltip = spymaster_task.2102.c.tt
		ai_chance = {
			base = 50
		}
	}
}


spymaster_task.2103 = {
	hidden = yes
	
	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		send_interface_message = {
			type = event_spymaster_task_good
			title = spymaster_task.2103.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue <= average_skill_level }
					}
					desc = task_intrigue_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue > average_skill_level }
					}
					desc = task_intrigue_good_skilled_notification_tooltip
				}
			}
			
			right_icon = scope:secret_owner
			left_icon = scope:councillor
	
			scope:secret_to_reveal = {
				reveal_to = root
			}
		}
	}
}

###END Discovery of Minor Secrets Chain

#Discovery of Major Secrets Chain
# 2104-2106
# by Petter Vilberg
spymaster_task.2104 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:councillor = {
			councillor_positive_event_opinion_trigger = yes
			intrigue >= mediocre_skill_rating
		}
		scope:councillor_liege = {
			any_courtier = {
				any_secret = {
					spymaster_task_find_secrets_suitable_major_secret_trigger = yes
				}
			}
			any_vassal = {
				any_secret = {
					spymaster_task_find_secrets_suitable_major_secret_trigger = yes
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		scope:councillor_liege = {
			every_courtier = {
				limit = {
					any_secret = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
					}
				}
				add_to_temporary_list = potential_secret_holder
			}
			every_vassal = {
				limit = {
					OR = {
						root = { highest_held_title_tier = tier_county }
						highest_held_title_tier = tier_county
					}
					any_secret = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
					}
				}
				add_to_temporary_list = potential_secret_holder
			}
			random_in_list = {
				list = potential_secret_holder
				save_scope_as = secret_owner
				random_secret = {
					limit = {
						spymaster_task_find_secrets_suitable_major_secret_trigger = yes
					}
					save_scope_as = secret_to_reveal
				}
			}
		}
		scope:councillor = {
			trigger_event = spymaster_task.2105
		}
	}
}

spymaster_task.2105 = {
	type = character_event
	title = spymaster_task.2102.t
	desc = spymaster_task.2102.desc
	theme = intrigue
	left_portrait = {
		character = scope:secret_owner
		animation = worry
	}

	option = {
		name = spymaster_task.2102.a
		trigger = {
			scope:secret_to_reveal = {
				NOT = { is_known_by = root }
			}
		}
		scope:secret_to_reveal = {
			reveal_to = root
		}
		show_as_tooltip = {
			scope:secret_to_reveal = {
				reveal_to = scope:councillor_liege
			}
		}
		hidden_effect = {
			scope:councillor_liege = {
				trigger_event = spymaster_task.2106
			}
		}
	}

	option = {
		name = spymaster_task.2102.b
		trigger = {
			scope:secret_to_reveal = {
				is_known_by = root
			}
		}
		show_as_tooltip = {
			scope:secret_to_reveal = {
				reveal_to = scope:councillor_liege
			}
		}
		hidden_effect = {
			scope:councillor_liege = {
				trigger_event = spymaster_task.2106
			}
		}
	}

	option = {
		name = spymaster_task.2102.c
		scope:secret_to_reveal = {
			if = {
				limit = {
					NOT = { is_known_by = root}
				}
				reveal_to = root
			}
		}
		custom_tooltip = spymaster_task.2102.c.tt
	}
}


spymaster_task.2106 = {
	hidden = yes
	
	immediate = {
		cp:councillor_spymaster = {
			save_scope_as = active_councillor
		}
		send_interface_message = {
			type = event_spymaster_task_good
			title = spymaster_task.2103.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue <= average_skill_level }
					}
					desc = task_intrigue_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor = { intrigue > average_skill_level }
					}
					desc = task_intrigue_good_skilled_notification_tooltip
				}
			}
			
			right_icon = scope:secret_owner
			left_icon = scope:councillor
	
			scope:secret_to_reveal = {
				reveal_to = root
			}
		}
	}
}

###END Discovery of Major Secrets Chain


###############
# Espionage Side Effects
# 3000-3999
###############

###Bad Side Effects

#Snooping on your vassal causes opinion loss
# by Petter Vilberg
spymaster_task.3001 = {
	type = letter_event
	opening = {
		desc = spymaster_task.3001.opening
	}
	desc = spymaster_task.3001.desc
	sender = scope:target_character
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:target_character = {
			target_is_liege_or_above = root
		}
	}

	weight_multiplier = {
		base = 1
		#Check whether the target's Spymaster is performing Disrupt Schemes
		modifier = {
			add = {
				value = scope:target_character.cp:councillor_spymaster.intrigue
				multiply = 0.05
			}
			exists = scope:target_character.cp:councillor_spymaster
			scope:target_character.cp:councillor_spymaster = {
				is_performing_council_task = task_disrupt_schemes
			}
		}
		# Reduce discovery chance by councillor's intrigue
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.05
		}
	}

	immediate = {
		set_variable = {
			name = had_spymaster_task_side_effect
			value = yes
			days = spymaster_task_side_effect_cooldown
		}
	}

	#Deny everything and continue
	option = {
		name = spymaster_task.3001.a
		custom_tooltip = spymaster_task.3001.continue_snooping
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = angry_opinion
				opinion = -30
			}
		}
		add_prestige = medium_prestige_loss
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3002
			}
		}
	}

	#Promise it will stop
	option = {
		name = spymaster_task.3001.b
		scope:councillor = {
			start_default_task = yes
		}
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = suspicion_opinion
				opinion = -15
			}
		}
		add_prestige = minor_prestige_loss
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3003
			}
		}
	}
}

#Your Liege has been and continues to snoop on you
# by Petter Vilberg
spymaster_task.3002 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3002.opening
		desc = spymaster_task.3002.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = scheme
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3002.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = angry_opinion
			opinion = -30
		}
	}
}

#Your Liege has been snooping, but promises to stop
# by Petter Vilberg
spymaster_task.3003 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3002.opening
		desc = spymaster_task.3003.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = shame
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3003.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = suspicion_opinion
			opinion = -15
		}
	}
}


#Snooping on your liege or above causes opinion loss
# by Petter Vilberg
spymaster_task.3004 = {
	type = letter_event
	opening = {
		desc = spymaster_task.3001.opening
	}
	desc = spymaster_task.3004.desc
	sender = scope:target_character
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		target_is_liege_or_above = scope:target_character
	}

	weight_multiplier = {
		base = 1
		#Check whether the target's Spymaster is performing Disrupt Schemes
		modifier = {
			add = {
				value = scope:target_character.cp:councillor_spymaster.intrigue
				multiply = 0.05
			}
			exists = scope:target_character.cp:councillor_spymaster
			scope:target_character.cp:councillor_spymaster = {
				is_performing_council_task = task_disrupt_schemes
			}
		}
		# Reduce discovery chance by councillor's intrigue
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.05
		}
	}

	immediate = {
		set_variable = {
			name = had_spymaster_task_side_effect
			value = yes
			days = spymaster_task_side_effect_cooldown
		}
	}

	#Deny everything and continue
	option = {
		name = spymaster_task.3004.a
		custom_tooltip = spymaster_task.3001.continue_snooping
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = spying_on_liege_opinion
			}
		}
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3005
			}
		}
	}

	#Promise it will stop
	option = {
		name = spymaster_task.3004.b
		scope:councillor = {
			start_default_task = yes
		}
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = suspicion_opinion
				opinion = -15
			}
		}
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3006
			}
		}
	}
}

#Your vassal has been and continues to snoop on you
spymaster_task.3005 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3005.opening
		desc = spymaster_task.3005.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = scheme
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3005.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = angry_opinion
			opinion = -30
		}
	}
}

#Your Vassal has been snooping, but promises to stop
spymaster_task.3006 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3005.opening
		desc = spymaster_task.3006.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = shame
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3006.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = suspicion_opinion
			opinion = -15
		}
	}
}

#Snooping on a foreign ruler causes opinion loss
# by Petter Vilberg
spymaster_task.3007 = {
	type = letter_event
	opening = {
		desc = spymaster_task.3001.opening
	}
	desc = spymaster_task.3007.desc
	sender = scope:target_character
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:target_character = { is_independent_ruler = yes }
		NOT = { target_is_liege_or_above = scope:target_character }
		NOT = { scope:target_character = root }
	}

	weight_multiplier = {
		base = 1
		#Check whether the target's Spymaster is performing Disrupt Schemes
		modifier = {
			add = {
				value = scope:target_character.cp:councillor_spymaster.intrigue
				multiply = 0.05
			}
			exists = scope:target_character.cp:councillor_spymaster
			scope:target_character.cp:councillor_spymaster = {
				is_performing_council_task = task_disrupt_schemes
			}
		}
		# Reduce discovery chance by councillor's intrigue
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.05
		}
	}

	immediate = {
		set_variable = {
			name = had_spymaster_task_side_effect
			value = yes
			days = spymaster_task_side_effect_cooldown
		}
	}

	#Deny everything and continue
	option = {
		name = spymaster_task.3007.a
		custom_tooltip = spymaster_task.3001.continue_snooping
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = angry_opinion
				opinion = -30
			}
		}
		add_prestige = medium_prestige_loss
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3008
			}
		}
	}

	#Promise it will stop
	option = {
		name = spymaster_task.3007.b
		scope:councillor = {
			start_default_task = yes
		}
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = suspicion_opinion
				opinion = -15
			}
		}
		add_prestige = medium_prestige_loss
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3009
			}
		}
	}
}

#A Foreign Ruler has been and continues to snoop on you
spymaster_task.3008 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3005.opening
		desc = spymaster_task.3005.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = scheme
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3005.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = angry_opinion
			opinion = -30
		}
	}
}

#A Foreign Ruler has been snooping, but promises to stop
spymaster_task.3009 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3005.opening
		desc = spymaster_task.3009.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = shame
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3006.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = suspicion_opinion
			opinion = -15
		}
	}
}




#Snooping on a foreign vassal causes opinion loss
# by Petter Vilberg
spymaster_task.3010 = {
	type = letter_event
	opening = {
		desc = spymaster_task.3001.opening
	}
	desc = spymaster_task.3007.desc
	sender = scope:target_character
	
	trigger = {
		exists = cp:councillor_spymaster
		scope:councillor = cp:councillor_spymaster
		scope:target_character = {
			is_independent_ruler = no
			NOT = { target_is_liege_or_above = root }
		}
		NOT = { target_is_liege_or_above = scope:target_character }
		NOT = { scope:target_character = root }
	}

	weight_multiplier = {
		base = 1
		#Check whether the target's Spymaster is performing Disrupt Schemes
		modifier = {
			add = {
				value = scope:target_character.cp:councillor_spymaster.intrigue
				multiply = 0.05
			}
			exists = scope:target_character.cp:councillor_spymaster
			scope:target_character.cp:councillor_spymaster = {
				is_performing_council_task = task_disrupt_schemes
			}
		}
		# Reduce discovery chance by councillor's intrigue
		compare_modifier = {
			target = scope:councillor
			value = intrigue
			multiplier = -0.05
		}
	}

	immediate = {
		set_variable = {
			name = had_spymaster_task_side_effect
			value = yes
			days = spymaster_task_side_effect_cooldown
		}
		scope:target_character.top_liege = {
			save_scope_as = target_top_liege
		}
	}

	#Deny everything and continue
	option = {
		name = spymaster_task.3007.a
		custom_tooltip = spymaster_task.3001.continue_snooping
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = angry_opinion
				opinion = -30
			}
			reverse_add_opinion = {
				target = scope:target_top_liege
				modifier = plotting_against_vassals_opinion
				opinion = -15
			}
		}
		add_prestige = medium_prestige_loss
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3011
			}
		}
	}

	#Promise it will stop
	option = {
		name = spymaster_task.3007.b
		scope:councillor = {
			start_default_task = yes
		}
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:target_character
				modifier = suspicion_opinion
				opinion = -15
			}
			reverse_add_opinion = {
				target = scope:target_top_liege
				modifier = plotting_against_vassals_opinion
				opinion = -15
			}
		}
		hidden_effect = {
			scope:target_character = {
				trigger_event = spymaster_task.3012
			}
		}
	}
}

#A Foreign Ruler has been and continues to snoop on you
spymaster_task.3011 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3005.opening
		desc = spymaster_task.3005.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = scheme
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3005.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = angry_opinion
			opinion = -30
		}
		scope:target_top_liege = {
			add_opinion = {
				target = scope:councillor_liege
				modifier = plotting_against_vassals_opinion
				opinion = -15
			}
		}
	}
}

#A Foreign Ruler has been snooping, but promises to stop
spymaster_task.3012 = {
	type = character_event
	title = spymaster_task.3002.t
	desc = {
		desc = spymaster_task.3005.opening
		desc = spymaster_task.3006.end
	}
	theme = intrigue
	left_portrait = {
		character = scope:councillor_liege
		animation = shame
	}
	right_portrait = {
		character = scope:councillor
		animation = disapproval
	}
	
	option = {
		name = spymaster_task.3006.a
		add_opinion = {
			target = scope:councillor_liege
			modifier = suspicion_opinion
			opinion = -15
		}
		scope:target_top_liege = {
			add_opinion = {
				target = scope:councillor_liege
				modifier = plotting_against_vassals_opinion
				opinion = -15
			}
		}
	}
}

