﻿#EK NOTE: Disabling real-world stuff.
@unlock_scheme_opt_out_value = 1

namespace = scheme_critical_moments

##################################################
# #Base System
# 0001 - 0010	Standard Critical Moment
#
# #Ordinary Schemes
# 1001 - 1010	Murder
# 1011 - 1020	Seduce
# 1021 - 1030	Sway
# 1031 - 1040	Befriend
# 1041 - 1050	Court
# 1051 - 1060	Elope
# 1061 - 1070	Abduct
# 1071 - 1080	Fabricate Claim
# 1081 - 1090	Learn Language
# 1091 - 1100	Abduct Regent
# 1101 - 1110	Claim Throne
# 1111 - 1120	Steal Back Artefact
# 1121 - 1130	Convert to Witchcraft
# 1131 - 1140   Slander
# 1141 - 1150   Promote
# 1151 - 1160	Generate Claim
# 1161 - 1170   Steal Herd
# 1171 - 1180   Challenge Status
#
# #Contract Schemes (Diplo)
# 2001 - 2010	Regale court with stories
# 2011 - 2020	Improve ruler's reputation
# 2021 - 2030	Hobnob with local ruler
# 2031 - 2040	Paid Mediator
# 2041 - 2050	Confidence trickster
#
# #Contract Schemes (Martial)
# 2201 - 2210	Hunt criminals
# 2211 - 2220	Act as hired muscle
# 2221 - 2230	Help train local MaA
# 2231 - 2240	Garrison service
# 2241 - 2250	Ambush travellers
#
# #Contract Schemes (Stewardship)
# 2401 - 2410	Collect taxes for a third party
# 2411 - 2420	Perform a census
# 2421 - 2430	Help construct a building
# 2431 - 2440	Settle an administrative boundary dispute
# 2441 - 2450	Collect fake taxes
#
# #Contract Schemes (Intrigue)
# 2601 - 2610	Provide intelligence from your trips
# 2611 - 2620	Murder a character's rival
# 2621 - 2630	Abduct a character's rival
# 2631 - 2640	Attack the property of a character's rival
# 2641 - 2650	Heist from a ruler's treasury
#
# #Contract Schemes (Learning)
# 2801 - 2810	Copy texts for a learned ruler
# 2811 - 2820	Help settle a local theological argument
# 2821 - 2830	Create a work of learning for a ruler
# 2831 - 2840	Solicit Charity
# 2841 - 2850	Sell bogus relics
#
# #Contract Schemes (Prowess)
# 3001 - 3010	Hunt in the wilds
# 3011 - 3020	Guard merchant properties
# 3021 - 3030	Guard a local ruler at a minor event
# 3031 - 3040	Go rustling
# 3041 - 3050	Go poaching
#
##################################################










##################################################
# GENERAL SCRIPTED TRIGGERS & EFFECTS

# Simple effect for rolling if a scheme with no secrecy succeeded or failed. No thrills, no frills.
scripted_effect roll_scheme_success_or_failure_effect = {
	random = {
		chance = scope:scheme.scheme_success_chance
		save_scope_value_as = {
			name = scheme_successful
			value = yes
		}
	}
	scope:owner = {
		if = {
			limit = { exists = scope:scheme_successful }
			trigger_event = $SUCCESS_EVENT$
		}
		else = { trigger_event = $FAILURE_EVENT$ }
	}
}











##################################################
# BASE SYSTEM

##################################################
# Standard Critical Moment
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################

scripted_trigger scheme_critical_moments_0001_can_add_agents_trigger = {
	scope:scheme = {
		custom_tooltip = {
			text = scheme_critical_moments.0001.option.need_opportunities
			scheme_agent_charges >= 1
		}
		custom_tooltip = {
			text = scheme_critical_moments.0001.option.max_slots
			any_scheme_agent_slot = { count < character_max_scheme_agents_value }
		}
	}
}

scripted_effect scheme_critical_moments_0001_configure_portraits_effect = {
	# Let's configure some portraits!
	## But only for the player, the AI doesn't care.
	if = {
		limit = { is_ai = no }
		# If there's no target character, root goes in the centre.
		if = {
			limit = {
				NOT = { exists = scope:scheme.scheme_target_character}
			}
			save_scope_as = centre_portrait
		}
		# Else, root goes on the left & scope:target on the right.
		else = {
			save_scope_as = left_portrait
			if = {
				limit = {
					scope:target = {
						NOT = {
							this = root
						}
					}
				}
				scope:target = { save_scope_as = right_portrait }
			}
		}
	}
	# Do we need to show murder interception tooltips?
	## Save this as a scope so that we don't have to faff with multiple scopes getting read into a script value.
	if = {
		limit = {
			# No need to process the stuff below for robots.
			is_ai = no
			# Are we actually a murder?
			scope:scheme = { is_savable_murder_scheme_trigger = yes }
		}
		scope:scheme = {
			set_variable = {
				name = target_intercept_repeatable_chance
				value = scope:target.scheme_intercept_repeatable_chance_total_value
			}
		}
	}
}

scripted_effect scheme_critical_moments_0001_execute_scheme_default_flow_effect = {
	# If this is a murder scheme, warn about intercepts.
	if = {
		limit = {
			scope:scheme = { has_variable = target_intercept_repeatable_chance }
		}
		# Show the repeatable intercepter if relevant.
		if = {
			limit = { scope:scheme.var:target_intercept_repeatable_chance >= 1 }
			# Aaaaand throw out our warning.
			custom_tooltip = scheme_critical_moments.0001.a.tt.murders_may_be_intercepted.repeatable
			custom_tooltip = scheme_critical_moments.0001.a.tt.murders_may_be_intercepted.repeatable_and_unique
		}
		# Otherwise just show the unique intercepter.
		else = { custom_tooltip = scheme_critical_moments.0001.a.tt.murders_may_be_intercepted.unique }
	}
	scope:scheme = {
		change_opportunities = {
			value = $COST$
			multiply = -1
		}
	}
	trigger_event = { saved_event_id = scope:follow_up_event }
}

#	Scheme Type A: choose between finalising your scheme, picking new agents, or starting the round again from the top.
scheme_critical_moments.0001 = {
	type = character_event
	window = scheme_preparations_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:scheme = { is_scheme_category = contract }
				}
				desc = scheme_critical_moments.0001.t.task_contract
			}
			desc = scheme_critical_moments.0001.t.scheme
		}
	}
	desc = scheme_critical_moments.0001.desc
	theme = intrigue
	left_portrait = {
		character = scope:left_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	center_portrait = {
		character = scope:centre_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	right_portrait = {
		character = scope:right_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	# Background overrides.
	## Tabbed one degree further for easy folding.
		## Romantic schemes.
		override_background = {
			trigger = {
				scope:scheme = { should_use_amorous_scheme_animation_trigger = yes }
			}
			reference = bedchamber
		}
		## Diplo schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = diplomacy }
			}
			reference = council_chamber
		}
		## Martial or prowess schemes.
		override_background = {
			trigger = {
				scope:scheme = {
					scheme_skill = martial
					scheme_skill = prowess
				}
			}
			reference = armory
		}
		## Stewardship schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = stewardship }
			}
			reference = study
		}
		## Intrigue schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = intrigue }
			}
			reference = corridor_night
		}
		## Learning schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = learning }
			}
			reference = physicians_study
		}
	# Icon overrides.
	## Tabbed one degree further for easy folding.
		## Diplo schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = diplomacy }
			}
			reference = "gfx/interface/icons/event_types/type_diplomacy.dds"
		}
		## Martial or prowess schemes.
		override_icon = {
			trigger = {
				scope:scheme = {
					scheme_skill = martial
					scheme_skill = prowess
				}
			}
			reference = "gfx/interface/icons/event_types/type_martial.dds"
		}
		## Stewardship schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = stewardship }
			}
			reference = "gfx/interface/icons/event_types/type_stewardship.dds"
		}
		## Intrigue schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = intrigue }
			}
			reference = "gfx/interface/icons/event_types/type_intrigue.dds"
		}
		## Learning schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = learning }
			}
			reference = "gfx/interface/icons/event_types/type_learning.dds"
		}

	# Scheme Preparations status bar
	widget = {
		gui = "event_window_widget_scheme_preparations"
		container = "custom_widgets_container"
	}

	trigger = {
		NOT = { has_character_flag = currently_in_scheme_preparations }
	}
	
	immediate = {
		# Flag that we're in the event, so no doubles pls.
		add_character_flag = currently_in_scheme_preparations
		# Sort some aesthetic stuff for the player.
		scheme_critical_moments_0001_configure_portraits_effect = yes
	}
	
	# Attempt to complete the scheme.
	option = {
		name = scheme_critical_moments.0001.a
		show_as_unavailable = { always = yes }
		trigger = {
			custom_tooltip = {
				text = scheme_critical_moments.0001.a.tt.stymied.insufficient_opportunities
				scope:scheme = { scheme_agent_charges >= scheme_charge_to_advance_threshold_value }
			}
			#TODO_CD_EP3; disabled till we get a code fix — looks like this doesn't like evaluating before being run any more. Ironically since we turned off the error for evaluating before scheme finalisation.
			#custom_tooltip = {
			#	text = scheme_critical_moments.0001.a.tt.stymied.insufficient_success_chance
			#	scope:scheme.scheme_success_chance >= 1
			#}
			# Tell the AI not to bother unless their chance are at least 50:50.
			trigger_if = {
				limit = { is_ai = yes }
				scope:scheme = {
					OR = {
						# Either you've got a good shot.
						scheme_success_chance >= 50
						# Or you've run out of potential.
						AND = {
							scheme_number_of_filled_agent_slots >= character_max_scheme_agents_value
							scope:scheme.scheme_success_chance >= scope:scheme.max_scheme_success_chance
						}
					}
				}
			}
		}

		# Scheme gon' try to conclude.
		custom_tooltip = scheme_critical_moments.0001.a.tt.progress
		scheme_critical_moments_0001_execute_scheme_default_flow_effect = { COST = scheme_charge_to_advance_threshold_value }

		ai_chance = { scheme_prep_ai_should_execute_scheme_modifier = yes }
	}
	
	# Choose first agent type.
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { exists = scope:scheme.var:choice_a }
						desc = scheme_critical_moments.0001.b
					}
					desc = scheme_critical_moments.0001.b.fallback
				}
			}
		}
		show_as_unavailable = { always = yes }
		trigger = { scheme_critical_moments_0001_can_add_agents_trigger = yes }

		# Check for the variable so that, if we've run out of slots and not generated one, we don't error spam.
		if = {
			limit = { exists = scope:scheme.var:choice_a }
			select_critical_moment_agents_effect = { CHOICE = scope:scheme.var:choice_a }
		}
		scope:scheme = { change_opportunities = -2 }

		ai_chance = {
			modifier = {
				add = 25
				scope:scheme = {
					any_scheme_agent_slot = { count >= character_max_scheme_agents_value }
				}
			}
		}
	}
	
	# Choose second agent type.
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { exists = scope:scheme.var:choice_b }
						desc = scheme_critical_moments.0001.c
					}
					desc = scheme_critical_moments.0001.c.fallback
				}
			}
		}
		show_as_unavailable = { always = yes }
		trigger = { scheme_critical_moments_0001_can_add_agents_trigger = yes }

		# Check for the variable so that, if we've run out of slots and not generated one, we don't error spam.
		if = {
			limit = { exists = scope:scheme.var:choice_b }
			select_critical_moment_agents_effect = { CHOICE = scope:scheme.var:choice_b }
		}
		scope:scheme = { change_opportunities = -2 }

		ai_chance = {
			modifier = {
				add = 25
				scope:scheme = {
					any_scheme_agent_slot = { count >= character_max_scheme_agents_value }
				}
			}
		}
	}
	
	# Void both and take another go around.
	option = {
		name = scheme_critical_moments.0001.d
		show_as_unavailable = { always = yes }
		trigger = { scheme_critical_moments_0001_can_add_agents_trigger = yes }

		scope:scheme = {
			# Deduct the cost.
			change_opportunities = -2
			# Plus remember to clear our choices.
			remove_variable = choice_a
			remove_variable = choice_b
			# Let's go again.
			#TODO_CD_EP3; make this neutral
			## Hack fix for murder.
			if = {
				limit = { scheme_type = seduce }
				remove_character_flag = currently_in_scheme_preparations
				roll_critical_moment_options_06_agent_types_effect = {
					# Success results event.
					OUTPUT_EVENT = scheme_critical_moments.1001
					# Assassin
					AGENT_TYPE_01 = agent_assassin
					AGENT_TYPE_01_WEIGHT = critical_moment_agent_weight_high_value
					# Lookout
					AGENT_TYPE_02 = agent_lookout
					AGENT_TYPE_02_WEIGHT = critical_moment_agent_weight_high_value
					# Infiltrator
					AGENT_TYPE_03 = agent_infiltrator
					AGENT_TYPE_03_WEIGHT = critical_moment_agent_weight_medium_value
					# Footpad
					AGENT_TYPE_04 = agent_footpad
					AGENT_TYPE_04_WEIGHT = critical_moment_agent_weight_medium_value
					# Alibi
					AGENT_TYPE_05 = agent_alibi
					AGENT_TYPE_05_WEIGHT = critical_moment_agent_weight_high_value
					# Poisoner
					AGENT_TYPE_06 = agent_poisoner
					AGENT_TYPE_06_WEIGHT = critical_moment_agent_weight_high_value
				}
			}
			## Hack fix for seduction.
			if = {
				limit = { scheme_type = seduce }
				remove_character_flag = currently_in_scheme_preparations
				roll_critical_moment_options_07_agent_types_effect = {
					# Success results event.
					OUTPUT_EVENT = scheme_critical_moments.1011
					# Lookout
					AGENT_TYPE_01 = agent_lookout
					AGENT_TYPE_01_WEIGHT = critical_moment_agent_weight_high_value
					# Infiltrator
					AGENT_TYPE_02 = agent_infiltrator
					AGENT_TYPE_02_WEIGHT = critical_moment_agent_weight_high_value
					# Comrade_in_arms
					AGENT_TYPE_03 = agent_comrade_in_arms
					AGENT_TYPE_03_WEIGHT = critical_moment_agent_weight_high_value
					# Socialite
					AGENT_TYPE_04 = agent_socialite
					AGENT_TYPE_04_WEIGHT = critical_moment_agent_weight_medium_value
					# Musician
					AGENT_TYPE_05 = agent_musician
					AGENT_TYPE_05_WEIGHT = critical_moment_agent_weight_low_value
					# Tumbler
					AGENT_TYPE_06 = agent_tumbler
					AGENT_TYPE_06_WEIGHT = critical_moment_agent_weight_low_value
					# Poet
					AGENT_TYPE_07 = agent_poet
					AGENT_TYPE_07_WEIGHT = critical_moment_agent_weight_low_value
				}
			}
		}
		custom_tooltip = scheme_critical_moments.0001.d.tt

		# The AI selects its agent slots randomly, so it should never re-roll them.
		ai_chance = { base = -1000 }
	}

	# Come back later.
	option = {
		name = scheme_critical_moments.0001.h
		
		if = {
			limit = {
				scope:scheme = { scheme_agent_charges >= 1 }
			}
			custom_tooltip = scheme_critical_moments.0001.h.tt.some_opportunities
		}
		else = { custom_tooltip = scheme_critical_moments.0001.h.tt.zero_opportunities }

		ai_chance = {
			first_valid = {
				modifier = {
					add = -100
					scope:scheme.scheme_success_chance >= scope:scheme.max_scheme_success_chance
				}
				modifier = {
					add = 50
					always = yes
				}
			}
		}
	}

	after = {
		remove_character_flag = currently_in_scheme_preparations
		scope:scheme = { remove_variable = target_intercept_repeatable_chance }
	}
}

#	Scheme Type B: choose how many opportunities to spend on different ending types.
scheme_critical_moments.0002 = {
	type = character_event
	window = scheme_preparations_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:scheme = { is_scheme_category = contract }
				}
				desc = scheme_critical_moments.0001.t.task_contract
			}
			desc = scheme_critical_moments.0001.t.scheme
		}
	}
	desc = scheme_critical_moments.0002.desc
	theme = intrigue
	left_portrait = {
		character = scope:left_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	center_portrait = {
		character = scope:centre_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	right_portrait = {
		character = scope:right_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	# Background overrides.
	## Tabbed one degree further for easy folding.
		## Raid Estate Scheme.
		override_background = {
			trigger = {
				scope:scheme = { scheme_type = raid_estate }
			}
			reference = ep3_mediterranean_estate
		}
		## Romantic schemes.
		override_background = {
			trigger = {
				scope:scheme = { should_use_amorous_scheme_animation_trigger = yes }
			}
			reference = bedchamber
		}
		## Diplo schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = diplomacy }
			}
			reference = council_chamber
		}
		## Martial or prowess schemes.
		override_background = {
			trigger = {
				scope:scheme = {
					scheme_skill = martial
					scheme_skill = prowess
				}
			}
			reference = armory
		}
		## Stewardship schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = stewardship }
			}
			reference = study
		}
		## Intrigue schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = intrigue }
			}
			reference = corridor_night
		}
		## Learning schemes.
		override_background = {
			trigger = {
				scope:scheme = { scheme_skill = learning }
			}
			reference = physicians_study
		}
	# Icon overrides.
	## Tabbed one degree further for easy folding.
		## Diplo schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = diplomacy }
			}
			reference = "gfx/interface/icons/event_types/type_diplomacy.dds"
		}
		## Martial or prowess schemes.
		override_icon = {
			trigger = {
				scope:scheme = {
					scheme_skill = martial
					scheme_skill = prowess
				}
			}
			reference = "gfx/interface/icons/event_types/type_martial.dds"
		}
		## Stewardship schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = stewardship }
			}
			reference = "gfx/interface/icons/event_types/type_stewardship.dds"
		}
		## Intrigue schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = intrigue }
			}
			reference = "gfx/interface/icons/event_types/type_intrigue.dds"
		}
		## Learning schemes.
		override_icon = {
			trigger = {
				scope:scheme = { scheme_skill = learning }
			}
			reference = "gfx/interface/icons/event_types/type_learning.dds"
		}

	# Scheme Preparations status bar
	widget = {
		gui = "event_window_widget_scheme_preparations"
		container = "custom_widgets_container"
	}

	trigger = {
		NOR = {
			has_character_flag = currently_in_scheme_preparations
			has_character_flag = currently_in_scheme_reminder
		}
	}

	immediate = {
		# Flag that we're in the event, so no doubles pls.
		add_character_flag = currently_in_scheme_preparations
		# Sort some aesthetic stuff for the player.
		scheme_critical_moments_0001_configure_portraits_effect = yes
	}

	# Standard completion: no buffs.
	option = {
		name = scheme_critical_moments.0002.a
		show_as_unavailable = { always = yes }
		trigger = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.a.tt.stymied.insufficient_opportunities
				scope:scheme = { scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t1_value }
			}
			# Tell the AI not to bother unless their chance are at least 50:50.
			trigger_if = {
				limit = {
					is_ai = yes
					# Make sure the AI can get out of long schemes eventually.
					scope:scheme.scheme_duration_days < 3650
				}
				scope:scheme = {
					# Must have a good shot.
					scheme_success_chance >= 50
					# Only consider the best possible option.
					scheme_agent_charges < multi_ending_scheme_charge_to_advance_threshold_t2_value
				}
			}
		}

		# Scheme gon' try to conclude.
		custom_tooltip = scheme_critical_moments.0001.a.tt.progress
		scheme_critical_moments_0001_execute_scheme_default_flow_effect = { COST = multi_ending_scheme_charge_to_advance_threshold_t1_value }

		ai_chance = { scheme_prep_ai_should_execute_scheme_modifier = yes }
	}

	# Pricier completion: minor success chance gains.
	option = {
		name = scheme_critical_moments.0002.b
		show_as_unavailable = { always = yes }
		trigger = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.b.tt.stymied.insufficient_opportunities
				scope:scheme = { scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t2_value }
			}
			# Tell the AI not to bother unless their chance are at least 50:50.
			trigger_if = {
				limit = {
					is_ai = yes
					# Make sure the AI can get out of long schemes eventually.
					scope:scheme.scheme_duration_days < 3650
				}
				scope:scheme = {
					# Must have a good shot.
					scheme_success_chance >= 50
					# Only consider the best possible option.
					scheme_agent_charges < multi_ending_scheme_charge_to_advance_threshold_t3_value
				}
			}
		}

		# Scheme gon' try to conclude.
		custom_tooltip = scheme_critical_moments.0002.b.tt.progress
		scope:scheme = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.b.tt.clarify_buff
				add_scheme_modifier = { type = scheme_end_buff_t2_modifier }
			}
		}
		scheme_critical_moments_0001_execute_scheme_default_flow_effect = { COST = multi_ending_scheme_charge_to_advance_threshold_t2_value }

		ai_chance = { scheme_prep_ai_should_execute_scheme_modifier = yes }
	}

	# Very pricey completion: moderate success chance gains.
	option = {
		name = scheme_critical_moments.0002.c
		show_as_unavailable = { always = yes }
		trigger = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.c.tt.stymied.insufficient_opportunities
				scope:scheme = { scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t3_value }
			}
			# Tell the AI not to bother unless their chance are at least 50:50.
			trigger_if = {
				limit = {
					is_ai = yes
					# Make sure the AI can get out of long schemes eventually.
					scope:scheme.scheme_duration_days < 3650
				}
				scope:scheme = {
					# Must have a good shot.
					scheme_success_chance >= 50
					# Only consider the best possible option.
					scheme_agent_charges < multi_ending_scheme_charge_to_advance_threshold_t4_value
				}
			}
		}

		# Scheme gon' try to conclude.
		custom_tooltip = scheme_critical_moments.0002.c.tt.progress
		scope:scheme = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.c.tt.clarify_buff
				add_scheme_modifier = { type = scheme_end_buff_t3_modifier }
			}
		}
		scheme_critical_moments_0001_execute_scheme_default_flow_effect = { COST = multi_ending_scheme_charge_to_advance_threshold_t3_value }

		ai_chance = { scheme_prep_ai_should_execute_scheme_modifier = yes }
	}

	# Max pricey completion: excellent success chance gains.
	option = {
		name = scheme_critical_moments.0002.d
		show_as_unavailable = { always = yes }
		trigger = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.d.tt.stymied.insufficient_opportunities
				scope:scheme = { scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t4_value }
			}
			# Tell the AI not to bother unless their chance are at least 50:50.
			trigger_if = {
				limit = {
					is_ai = yes
					# Make sure the AI can get out of long schemes eventually.
					scope:scheme.scheme_duration_days < 3650
				}
				scope:scheme = {
					OR = {
						scheme_success_chance >= 50
						AND = {
							scheme_success_chance >= max_scheme_success_chance
							scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t4_value
						}
					}
				}
			}
		}

		# Scheme gon' try to conclude.
		custom_tooltip = scheme_critical_moments.0002.d.tt.progress
		scope:scheme = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.d.tt.clarify_buff
				add_scheme_modifier = { type = scheme_end_buff_t4_modifier }
			}
		}
		scheme_critical_moments_0001_execute_scheme_default_flow_effect = { COST = multi_ending_scheme_charge_to_advance_threshold_t4_value }

		ai_chance = { scheme_prep_ai_should_execute_scheme_modifier = yes }
	}

	# Come back later.
	option = {
		name = scheme_critical_moments.0002.e

		if = {
			limit = {
				scope:scheme = { scheme_agent_charges >= 1 }
			}
			custom_tooltip = scheme_critical_moments.0002.e.tt.some_opportunities
		}
		else = { custom_tooltip = scheme_critical_moments.0002.e.tt.zero_opportunities }

		ai_chance = {
			first_valid = {
				modifier = {
					add = -100
					scope:scheme.scheme_success_chance >= scope:scheme.max_scheme_success_chance
				}
				modifier = {
					add = 50
					always = yes
				}
			}
			# If this scheme has been going on for a long time, execute it regardless.
			first_valid = {
				modifier = {
					factor = 0
					scope:scheme = {
						is_scheme_category = contract
						scheme_duration_days >= 1825
					}
				}
				modifier = {
					factor = 0
					has_trait = impatient
					scope:scheme.scheme_duration_days >= 1825
				}
				modifier = {
					factor = 0
					has_trait = patient
					scope:scheme.scheme_duration_days >= 5475
				}
				modifier = {
					factor = 0
					scope:scheme.scheme_duration_days >= 3650
				}
			}
		}
	}

	after = {
		remove_character_flag = currently_in_scheme_preparations
		scope:scheme = { remove_variable = target_intercept_repeatable_chance }
	}
}

#	Error suppression.
scheme_critical_moments.0010 = {
	hidden = yes
	orphan = yes

	immediate = {
		trigger_event = scheme_critical_moments.0001
		trigger_event = scheme_critical_moments.0002
		trigger_event = scheme_critical_moments.1001
		trigger_event = scheme_critical_moments.1011
		save_scope_as = scheme_starter_personal_secrecy
	}
}

##################################################
# Reminders
# by Ewan Cowhig Croft
# 0011 - 0020
##################################################

#	Reminder event that you've hit the opportunities cap.
scheme_critical_moments.0011 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:scheme = { is_scheme_category = contract }
				}
				desc = scheme_critical_moments.0011.t.task_contract
			}
			desc = scheme_critical_moments.0011.t.scheme
		}
	}
	desc = {
		desc = scheme_critical_moments.0011.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:scheme.scheme_target_character }
				desc = scheme_critical_moments.0011.desc.middle.character
			}
			triggered_desc = {
				trigger = { exists = scope:scheme.scheme_target_title }
				desc = scheme_critical_moments.0011.desc.middle.title
			}
			# If we meet neither of these, we don't use a fallback and just leave the thing empty.
		}
		desc = scheme_critical_moments.0011.desc.outro
	}
	theme = intrigue
	left_portrait = {
		character = root
		scripted_animation = scheme_prep_pick_best_stance
	}
	right_portrait = {
		character = scope:right_portrait
		scripted_animation = scheme_prep_pick_best_stance
	}
	widget = {
		gui = "event_window_widget_scheme"
		container = "custom_widgets_container"
	}

	cooldown = { years = 1 }

	immediate = {
		# Portraits first.
		scope:scheme = {
			if = {
				limit = { is_scheme_target_type = character }
				scheme_target_character = {
					if = {
						limit = {
							NOT = { this = root }
						}
						save_scope_as = right_portrait
					}
				}
			}
			else_if = {
				limit = { is_scheme_target_type = title }
				scheme_target_title.holder ?= {
					if = {
						limit = {
							NOT = { this = root }
						}
						save_scope_as = right_portrait
					}
				}
			}
			else_if = {
				limit = { is_scheme_target_type = faith }
				scheme_target_faith.religious_head ?= {
					if = {
						limit = {
							NOT = { this = root }
						}
						save_scope_as = right_portrait
					}
				}
			}
			else_if = {
				limit = { is_scheme_target_type = culture }
				scheme_target_culture.culture_head ?= {
					if = {
						limit = {
							NOT = { this = root }
						}
						save_scope_as = right_portrait
					}
				}
			}
		}
		# Plus clarify exactly what's happening.
		custom_tooltip = scheme_critical_moments.0011.immediate.tt
	}

	# Max pricey completion: excellent success chance gains.
	option = {
		name = scheme_critical_moments.0011.c
		trigger = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.d.tt.stymied.insufficient_opportunities
				scope:scheme = { scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t4_value }
			}
			# Only fire this if we've actually got a follow-up event configured.
			exists = scope:follow_up_event
			# Tell the AI not to bother unless their chance are at least 50:50.
			trigger_if = {
				limit = { is_ai = yes }
				scope:scheme = {
					OR = {
						scheme_success_chance >= 50
						AND = {
							scheme_success_chance >= max_scheme_success_chance
							scheme_agent_charges >= multi_ending_scheme_charge_to_advance_threshold_t4_value
						}
					}
				}
			}
		}

		# Scheme gon' try to conclude.
		custom_tooltip = scheme_critical_moments.0002.d.tt.progress
		scope:scheme = {
			custom_tooltip = {
				text = scheme_critical_moments.0002.d.tt.clarify_buff
				add_scheme_modifier = { type = scheme_end_buff_t4_modifier }
			}
		}
		scheme_critical_moments_0001_execute_scheme_default_flow_effect = { COST = multi_ending_scheme_charge_to_advance_threshold_t4_value }

		ai_chance = { scheme_prep_ai_should_execute_scheme_modifier = yes }
	}

	# Noted, and don't remind me again.
	option = {
		name = scheme_critical_moments.0011.a
		
		custom_tooltip = scheme_critical_moments.0011.a.tt
		set_variable = {
			name = opportunities_reminder
			value = yes
		}
	}
	
	# Noted.
	option = {
		name = scheme_critical_moments.0011.b
		
		custom_tooltip = scheme_critical_moments.0011.b.tt
	}

	after = {
		remove_character_flag = currently_in_scheme_reminder
		scope:scheme = { remove_variable = target_intercept_repeatable_chance }
	}
}













##################################################
# CORE SCHEMES

##################################################
# Murder Results
# (shifted here) by Ewan Cowhig Croft
# 1001 - 1010
##################################################

#	Results event for the Murder scheme.
scheme_critical_moments.1001 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			generic_scheme_process_ending_effect = {
				RESULTS_TOOLTIP = murder_successful_roll_tt
				SUCCESS_EVENT_TYPE = on_action
				SUCCESS_EVENT_ID = murder_success_pre_filter_on_action
				FAILURE_EVENT_TYPE = on_action
				FAILURE_EVENT_ID = murder_failure_pre_filter_on_action
			}
		}
	}
}

##################################################
# Seduce Results
# (shifted here) by Ewan Cowhig Croft
# 1011 - 1020
##################################################

#	Results event for the Seduce scheme.
scheme_critical_moments.1011 = {
	hidden = yes

	immediate = {
		# Set up a block for the inappropriate cheating error?
		save_scope_value_as = {
			name = ignore_cheating_error_check
			value = yes
		}
		# Proceed to rolls.
		scope:scheme.scheme_owner = {
			#Do I want to proceed to a roll?
			if = {
				limit = {
					is_ai = no
					use_seduce_secrecy_trigger = {
						TARGET = scope:target
						OWNER = scope:owner
					}
				}
				trigger_event = seduce_outcome.0001
			}
			#Jump straight to roll
			else = {
				trigger_event = seduce_outcome.0002
			}
		}
	}
}

##################################################
# Sway Results
# (shifted here) by Ewan Cowhig Croft
# 1021 - 1030
##################################################

#	Results event for the Sway scheme.
scheme_critical_moments.1021 = {
	hidden = yes

	immediate = {
		random = {
			chance = scope:scheme.scheme_success_chance

			save_scope_value_as = {
				name = scheme_successful
				value = yes
			}
		}
		scope:owner = {
			if = {
				limit = {
					exists = scope:scheme_successful
				}
				trigger_event = {
					on_action = sway_success
				}
			}
			else = {
				trigger_event = {
					on_action = sway_failure
				}	
			}
		}
	}
}

##################################################
# Befriend Results
# (shifted here) by Ewan Cowhig Croft
# 1031 - 1040
##################################################

#	Results event for the Befriend scheme.
scheme_critical_moments.1031 = {
	hidden = yes

	immediate = {
		#Outcome system by Mathilda Bjarnehed
		scope:scheme.scheme_owner = {
			trigger_event = befriend_outcome.0001 #Hidden event rolling success/failure, discover/no discovery and sending on_actions or player choice event
		}
	}
}

##################################################
# Court Results
# (shifted here) by Ewan Cowhig Croft
# 1041 - 1050
##################################################

#	Results event for the Court scheme.
scheme_critical_moments.1041 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			# Set up a block for the inappropriate cheating error?
			save_scope_value_as = {
				name = ignore_cheating_error_check
				value = yes
			}
			# Proceed to rolls.
			scheme_owner = {
				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				#CONTINUE WITH OUTCOME ON_ACTION FOR AI TARGETS
				if = {
					limit = { scope:target = { is_ai = yes } }

					trigger_event = {
						on_action = court_outcome
					}
				}
				#FOR PLAYER CHARACTER, SEND CHOICE EVENT
				else = {
					scope:target = {
						trigger_event = {
							id = court_outcome.0003
						}
					}
				}
			}
		}
	}
}

##################################################
# Elope Results
# (shifted here) by Ewan Cowhig Croft
# 1051 - 1060
##################################################

#	Results event for the Elope scheme.
scheme_critical_moments.1051 = {
	hidden = yes

	immediate = {
		scope:scheme.scheme_owner = {
			#Do I want to proceed to a roll?
			if = {
				limit = {
					is_ai = no
				}
				trigger_event = elope.0001
			}
			#Jump straight to roll
			else = {
				trigger_event = elope.0002
			}
		}
	}
}

##################################################
# Abduct Results
# (shifted here) by Ewan Cowhig Croft
# 1061 - 1070
##################################################

#	Results event for the Abduct scheme.
scheme_critical_moments.1061 = {
	hidden = yes

	immediate = {
		scope:scheme.scheme_owner = {
			if = {
				limit = {
					has_character_flag = is_in_event_abduct_outcome_0001
				}
				trigger_event = {
					id = abduct_outcome.0001
					days = 1
				}
			}
			else = {
				trigger_event = abduct_outcome.0001
			}
		}
	}
}

##################################################
# Fabricate Claim Results
# (shifted here) by Ewan Cowhig Croft
# 1071 - 1080
##################################################

#	Results event for the Fabricate Claim scheme.
scheme_critical_moments.1071 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			# Do the Success Roll
			random = {
				chance = scheme_success_chance
				save_scope_value_as = {
					name = scheme_succeeded
					value = yes
				}
			}
			# Do the Secrecy Roll
			save_scope_value_as = {
				name = discovery_chance
				value = {
					value = 100
					subtract = scheme_secrecy
				}
			}
			random = {
				chance = scope:discovery_chance
				save_scope_value_as = {
					name = scheme_discovered
					value = yes
				}
			}

			# On Success: Check for viable Secret
			if = {
				limit = { exists = scope:scheme_succeeded }
				scheme_target_character = {
					if = {
						limit = { is_ruler = yes }
						# Check for secrets that can be revealed
						if = {
							limit = {
								any_secret = {
									NOT = { is_known_by = scope:owner }
								}
							}
							random_secret = {
								limit = {
									NOT = { is_known_by = scope:owner }
								}
								weight = {
									base = 1
									modifier = { # It's more fun to Discover a Criminal Secret
										add = 5
										is_criminal_for = scope:target
									}
								}
								save_scope_as = secret_to_reveal
							}
						}
						else_if = { # Others require Criminal Secrets
							limit = {
								any_secret = {
									NOT = { is_known_by = scope:owner }
									is_criminal_for = scope:target
								}
							}
							random = {
								chance = 20
								random_secret = {
									limit = {
										NOT = { is_known_by = scope:owner }
										is_criminal_for = scope:target	
									}
									save_scope_as = secret_to_reveal
								}
							}
						}
					}
					else = {
						if = {
							limit = {
								any_secret = {
									NOT = { is_known_by = scope:owner }
									is_criminal_for = scope:target
								}
							}
							random_secret = {
								limit = {
									NOT = { is_known_by = scope:owner }
									is_criminal_for = scope:target
								}
								save_scope_as = secret_to_reveal
							}
						}
					}
				}

				if = {
					limit = { exists = scope:secret_to_reveal }
					scheme_owner = {
						trigger_event = fabricate_hook_outcome.0001 # Secret discovery handling
					}
				}
				else = {
					if = {
						limit = { scope:target = { is_ruler = yes } }
						# Strong Hook against Ruler roll
						random = {
							chance = 20
							save_scope_value_as = {
								name = strong_hook_against_ruler
								value = yes
							}
						}
					}
					scheme_owner = {
						trigger_event = {
							on_action = fabricate_hook_method_success_outcome
						}
					}
				}
			}

			else = {
				if = {
					limit = { exists = scope:scheme_discovered }
					scheme_owner = {
						trigger_event = fabricate_hook_outcome.3001
					}
				}
				else = {
					scheme_owner = {
						send_interface_message = {
							type = fabricate_hook_bad_message
							title = fabricate_hook_failed_reset_message
							scope:scheme = {
								reset_failed_scheme_effect = yes
							}
						}
					}
				}
			}
		}
	}
}

##################################################
# Learn Language Results
# (shifted here) by Ewan Cowhig Croft
# 1081 - 1090
##################################################

#	Results event for the Learn Language scheme.
scheme_critical_moments.1081 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			random = {
				chance = scope:scheme.scheme_success_chance

				save_scope_value_as = {
					name = scheme_successful
					value = yes
				}
			}
			scope:owner = {
				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						on_action = learn_language_success
					}
				}
				else = {
					trigger_event = {
						on_action = learn_language_failure
					}	
				}
			}
		}
	}
}

##################################################
# Abduct Regent Results
# (shifted here) by Ewan Cowhig Croft
# 1091 - 1100
##################################################

#	Results event for the Abduct Regent scheme.
scheme_critical_moments.1091 = {
	hidden = yes

	immediate = {
		scope:scheme.scheme_owner = {
			if = {
				limit = {
					has_character_flag = is_in_event_diarchy_0501
				}
				trigger_event = {
					id = diarchy.0501
					days = 1
				}
			}
			else = { trigger_event = diarchy.0501 }
		}
	}
}

##################################################
# Claim Throne Results
# (shifted here) by Ewan Cowhig Croft
# 1101 - 1110
##################################################

#	Results event for the Claim Throne scheme.
scheme_critical_moments.1101 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character.primary_title = { save_scope_as = target_title }
			scheme_owner = {
				if = {
					limit = {
						has_character_flag = is_executing_claim_throne
					}
					trigger_event = {
						on_action = claim_throne_setup
						days = 1
					}
				}
				else = {
					trigger_event = {
						on_action = claim_throne_setup
					}
				}
			}
		}
	}
}

##################################################
# Steal Back Artefact Results
# (shifted here) by Ewan Cowhig Croft
# 1111 - 1120
##################################################

#	Results event for the Steal Back Artefact scheme.
scheme_critical_moments.1111 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			random = {
				chance = scope:scheme.scheme_success_chance

				save_scope_value_as = {
					name = scheme_successful
					value = yes
				}
			}
			scope:owner = {			
				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						on_action = steal_back_artifact_success
					}
				}
				else = {
					trigger_event = {
						on_action = steal_back_artifact_failure
					}	
				}
			}
		}
	}
}

##################################################
# Convert to Witchcraft Results
# (shifted here) by Ewan Cowhig Croft
# 1121 - 1130
##################################################

#	Results event for the Convert to Witchcraft scheme.
scheme_critical_moments.1121 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_owner = {
				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				#SECRECY: DO I WANT TO PROCEED?
				if = {
					limit = {
						use_convert_to_witchcraft_secrecy_trigger = { OWNER = scope:scheme.scheme_owner }
					}
					trigger_event = witch.2001
				}
				#CONTINUE WITH OUTCOME ON_ACTION FOR AI TARGETS
				else_if = {
					limit = { scope:target = { is_ai = yes } }

					if = {
						limit = { exists = scope:scheme_successful }
						trigger_event = witch.2003 #Success event
					}
					else = {
						trigger_event = {
							on_action = convert_to_witchcraft_failure_outcome
						}
					}
				}
				#FOR PLAYER CHARACTER, SEND CHOICE EVENT
				else = {
					scope:target = {
						trigger_event = witch.2002
					}
				}
			}
		}
	}
}

##################################################
# Slander Results
# Claudia Baldassi
# 1131 - 1140
##################################################

#	Results event for the Slander scheme.
scheme_critical_moments.1131 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner
				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.1132
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.1133
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.1132 = {
	type = character_event
	title = scheme_critical_moments.1132.t
	desc = {
		desc = scheme_critical_moments.1132.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
				}
				desc = scheme_critical_moments.1132.desc_discovered
			}
			desc = scheme_critical_moments.1132.desc_secret
		}
	}
	theme = diplomacy
	left_portrait = {
		character = root
		animation = scheme
	}
	lower_right_portrait = scope:target

	immediate = {
		# Trigger the response event for the target
		show_as_tooltip = {	# Effects on target (applied in the response events)
			scope:target = {
				change_influence = major_influence_loss
				custom_tooltip = scheme_slandered_candidacy_appointment_tt
				custom_tooltip = scheme_slandered_candidacy_acclamation_tt
				add_character_modifier = {
					modifier = scheme_slandered_modifier
					years = 10
					desc = scheme_slandered_candidacy_desc
				}
			}
		}
		if = {
			limit = {
				exists = scope:scheme_discovered
			}
			show_as_tooltip = { # Effects on target if discovered (applied in the response events)
				scope:target = {
					add_opinion = {
						target = scope:owner
						modifier = slandered_me_opinion
					}
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.1132.a
		scope:scheme = {
			end_scheme = yes
		}
		random_list = {
			10 = {
				send_interface_toast = {
					title = slander_nickname_given_tt
					left_icon = scope:target
					custom_tooltip = receive_slanderous_nickname_tt
				}
				scope:target = {
					add_character_flag = {
						flag = slander_nickname_flag
						years = 1
					}
				}
			}
			40 = {
				# you find nothing else
			}	
		}
	}
	after = {
		scope:target = {
			trigger_event = scheme_critical_moments.1134
		}
	}
}

#Failure
scheme_critical_moments.1133 = {
	type = character_event
	title = scheme_critical_moments.1133.t
	desc = {
		desc = scheme_critical_moments.1133.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
				}
				desc = scheme_critical_moments.1133.desc_discovered
			}
			desc = scheme_critical_moments.1133.desc_secret
		}
	}
	theme = diplomacy
	left_portrait = {
		character = root
		animation = disappointed
	}
	lower_right_portrait = scope:target

	immediate = {
		hidden_effect = {
			if = {
				limit = {
					exists = scope:scheme_discovered
				}
				scope:target = {
					send_interface_message = {
						type = event_scheme_bad
						title = scheme_critical_moments.1133.tt
						custom_tooltip = scheme_critical_moments.1133.tt.failed
					}
				}
			}
		}
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			deceitful = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.1133.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

# Target reaction
scheme_critical_moments.1134 = {
	type = character_event
	title = scheme_critical_moments.1134.t
	desc = {
		desc = scheme_critical_moments.1134.desc
		triggered_desc = {
			trigger = {
				has_character_flag = slander_nickname_flag
			}
			desc = scheme_critical_moments.1134.desc_given_nickname
		}
		triggered_desc = {
			trigger = {
				exists = scope:scheme_discovered
			}
			desc = scheme_critical_moments.1134.desc_discovered
		}
	}
	theme = diplomacy
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				exists = scope:scheme_discovered
			}
			animation = disbelief
		}
		animation = paranoia
	}
	lower_right_portrait = scope:owner

	immediate = {
		change_influence = major_influence_loss
		custom_tooltip = scheme_slandered_candidacy_appointment_tt
		custom_tooltip = scheme_slandered_candidacy_acclamation_tt
		add_character_modifier = {
			modifier = scheme_slandered_modifier
			years = 10
			desc = scheme_slandered_candidacy_desc
		}
		if = {
			limit = {
				exists = scope:scheme_discovered
				is_ai = yes
			}
			add_opinion = {
				target = scope:owner
				modifier = slandered_me_opinion
			}
		}
		if = {
			limit = {
				has_character_flag = slander_nickname_flag
			}
			set_random_negative_nickname_effect = yes
		}
	}

	option = {
		name = scheme_critical_moments.1134.a
	}
	after = {
		if = {
			limit = {
				has_character_flag = slander_nickname_flag
			}
			remove_character_flag = slander_nickname_flag
		}
	}
}

##################################################
# Steal Herd
# Ariana Tranumn
# 1161 - 1170
##################################################
 
#	Results event for the Steal Herd scheme.
scheme_critical_moments.1161 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			random = {
				chance = scope:scheme.scheme_success_chance

				save_scope_value_as = {
					name = scheme_successful
					value = yes
				}
			}
			scope:owner = {			
				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						on_action = steal_herd_success
					}
				}
				else = {
					trigger_event = {
						on_action = steal_herd_failure
					}	
				}
			}
		}
	}
}

##################################################
# Promote Results
# Claudia Baldassi
# 1141 - 1150
##################################################

#	Results event for the Promote scheme.
scheme_critical_moments.1141 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.1142
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.1143
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.1142 = {
	type = character_event
	title = scheme_critical_moments.1142.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = scope:owner
				}
				desc = scheme_critical_moments.1142.desc_self
			}
			desc = scheme_critical_moments.1142.desc_other
		}
	}
	theme = diplomacy
	left_portrait = {
		character = root
		animation = personality_content
	}
	lower_right_portrait = {
		trigger = {
			NOT = { scope:target = scope:owner }
		}
		character = scope:target
	}

	immediate = {
		scope:target = {
			add_character_modifier = {
				modifier = scheme_promoted_modifier
				years = 10
			}
			if = {
				limit = {
					is_ai = yes
					NOT = { this = scope:owner }
				}
				add_opinion = {
					target = scope:owner
					modifier = promoted_me_opinion
				}
			}
		}
		hidden_effect = {
			if = {
				limit = {
					NOT = { scope:owner = scope:target }
				}
				scope:target = {
					send_interface_message = {
						type = msg_promoted_scheme
						title = scheme_critical_moments.1142.tt
						desc = promote_toast_scheme_desc
						left_icon = scope:owner
						show_as_tooltip = {
							add_character_modifier = {
								modifier = scheme_promoted_modifier
								years = 10
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.1142.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

#Failure
scheme_critical_moments.1143 = {
	type = character_event
	title = scheme_critical_moments.1143.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:target = scope:owner
				}
				desc = scheme_critical_moments.1143.desc_self
			}
			desc = scheme_critical_moments.1143.desc_other
		}
	}
	theme = diplomacy
	left_portrait = {
		character = root
		animation = disapproval
	}
	lower_right_portrait = {
		trigger = {
			NOT = {
				scope:target = scope:owner
			}
		}
		character = scope:target
	}

	option = {
		name = scheme_critical_moments.1143.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

##################################################
# Generate Claim Results
# Claudia Baldassi
# 1151 - 1160
##################################################

#	Results event for the Generate Claim scheme.
scheme_critical_moments.1151 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_title = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner

				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.1152
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.1153
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.1152 = {
	type = character_event
	title = scheme_critical_moments.1152.t
	desc = {
		desc = scheme_critical_moments.1152.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
				}
				desc = scheme_critical_moments.1152.desc_discovered
			}
			desc = scheme_critical_moments.1152.desc_secret
		}
	}
	theme = learning
	left_portrait = {
		character = root
		animation = scheme
	}
	lower_right_portrait = scope:title_holder

	immediate = {
		# It needs to be pressed to unlock Contest Appointment. #TODO_CD_EP3 if Contest Appointment doesn't require a pressed claim anymore, tone down the claim given here too.
		add_pressed_claim = scope:target
		if = {
			limit = {
				scope:target = {
					exists = holder
					holder = { is_ai = yes }
				}
				exists = scope:scheme_discovered
			}
			scope:target.holder = {
				save_scope_as = title_holder
				add_opinion = {
					target = scope:owner
					modifier = claimed_my_title_opinion
					opinion = -30
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.1152.a
		if = {
			limit = {
				is_landless_adventurer = yes
			}
			trigger_event = laamp_extra_contract_schemes.0030
		}
		else = {
			scope:scheme = {
				end_scheme = yes
			}
		}
	}
}

#Failure
scheme_critical_moments.1153 = {
	type = character_event
	title = scheme_critical_moments.1153.t
	desc = {
		desc = scheme_critical_moments.1153.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
				}
				desc = scheme_critical_moments.1153.desc_discovered
			}
			desc = scheme_critical_moments.1153.desc_secret
		}
	}
	theme = learning
	left_portrait = {
		character = root
		animation = disapproval
	}
	lower_right_portrait = scope:title_holder

	immediate = {
		if = {
			limit = {
				scope:target = {
					exists = holder
					holder = { is_ai = yes }
				}
				exists = scope:scheme_discovered
			}
			scope:target.holder = {
				save_scope_as = title_holder
				add_opinion = {
					target = scope:owner
					modifier = claimed_my_title_opinion
					opinion = -30
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.1153.a
		if = {
			limit = {
				is_landless_adventurer = yes
			}
			scope:scheme.task_contract = {
				complete_task_contract = failure_standard
			}
		}
		scope:scheme = {
			end_scheme = yes
		}
	}
}

##################################################
# Challenge Status Results
# Claudia Baldassi
# 1171 - 1180
##################################################

#	Results event for the Challenge Status scheme.
scheme_critical_moments.1171 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = {
				save_scope_as = target
				save_scope_as = recipient
			}
			scheme_owner = {
				save_scope_as = owner

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.1172
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.1173
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.1172 = {
	type = character_event
	title = scheme_critical_moments.1172.t
	desc = scheme_critical_moments.1172.desc
	theme = learning
	left_portrait = {
		character = root
		animation = happy_teacher
	}
	right_portrait = {
		character = scope:target
		animation = stress
	}

	immediate = {
		scope:target = {
			save_scope_as = recipient # To fix loc
			set_variable = {
				name = challenge_status_influence_loss
				value = {
					value = 50
					add = scope:scheme.scheme_success_chance
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.1172.a
		show_as_tooltip = {
			scope:target = {
				change_influence = {
					value = scope:target.var:challenge_status_influence_loss
					multiply = -1
				}
			}
			custom_tooltip = status_challenged_success_tt
			scope:owner = {
				change_influence = {
					value = scope:target.var:challenge_status_influence_loss
				}
			}
		}
		scope:scheme = {
			end_scheme = yes
		}
	}

	after = {
		scope:target = {
			trigger_event = scheme_critical_moments.1174
		}
	}
}

#Failure
scheme_critical_moments.1173 = {
	type = character_event
	title = scheme_critical_moments.1173.t
	desc = scheme_critical_moments.1173.desc
	theme = learning
	left_portrait = {
		character = root
		animation = stressed_teacher
	}
	lower_right_portrait = scope:target

	option = {
		name = scheme_critical_moments.1173.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

#Target notification
scheme_critical_moments.1174 = {
	type = character_event
	title = scheme_critical_moments.1174.t
	desc = scheme_critical_moments.1174.desc
	theme = learning
	left_portrait = {
		character = root
		animation = stress
	}
	right_portrait = {
		character = scope:owner
		animation = laugh
	}

	immediate = {
		scope:target = { save_scope_as = recipient } # To fix loc
		change_influence = {
			value = scope:target.var:challenge_status_influence_loss
			multiply = -1
		}
		custom_tooltip = {
			text = status_challenged_success_tt
			house = {
				if = {

				}
				add_to_variable_list = {
					name = status_challenged_by
					target = scope:owner
					years = 10
				}
			}
		}
		scope:owner = {
			change_influence = {
				value = scope:target.var:challenge_status_influence_loss
			}
		}
	}

	option = {
		name = scheme_critical_moments.1174.a
	}

	after = {
		remove_variable = challenge_status_influence_loss
	}
}

##################################################
# Expand Power Base Results
# Claudia Baldassi
# 1181 - 1190
##################################################

#	Results event for the Expand Power Base scheme.
scheme_critical_moments.1181 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_owner = {
				save_scope_as = owner

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.1182
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.1183
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.1182 = {
	type = character_event
	title = scheme_critical_moments.1182.t
	desc = scheme_critical_moments.1182.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = schadenfreude
	}

	immediate = {
		duel = {
			skills = { stewardship intrigue }
			value = medium_skill_rating
			60 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				add_character_modifier = {
					modifier = ep3_expanded_power_base_minor_modifier
					years = 10
				}
			}
			30 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				add_character_modifier = {
					modifier = ep3_expanded_power_base_medium_modifier
					years = 10
				}
			}
			10 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				add_character_modifier = {
					modifier = ep3_expanded_power_base_major_modifier
					years = 10
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.1182.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

#Failure
scheme_critical_moments.1183 = {
	type = character_event
	title = scheme_critical_moments.1183.t
	desc = scheme_critical_moments.1183.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = disapproval
	}

	option = {
		name = scheme_critical_moments.1183.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

##################################################
# Depose Results
# Chad Uhl
# 1191 - 1200
##################################################

#	Results event for the Depose scheme.
scheme_critical_moments.1191 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = grand_ambitions.0200
			FAILURE_EVENT = grand_ambitions.0250
		}
	}
}









##################################################
# CONTRACT SCHEMES (DIPLO)

##################################################
# Regale court with stories Results
# by Ewan Cowhig Croft
# 2001 - 2010
##################################################

#	Results event.
scheme_critical_moments.2001 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.0002
			FAILURE_EVENT = laamp_base_contract_schemes.0003
		}
	}
}

##################################################
# Improve ruler's reputation Results
# by Ewan Cowhig Croft
# 2011 - 2020
##################################################

#	Results event.
scheme_critical_moments.2011 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.0012
			FAILURE_EVENT = laamp_base_contract_schemes.0013
		}
	}
}

##################################################
# Hobnob with local ruler Results
# by Ewan Cowhig Croft
# 2021 - 2030
##################################################

#	Results event.
scheme_critical_moments.2021 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.0022
			FAILURE_EVENT = laamp_base_contract_schemes.0023
		}
	}
}

##################################################
# Paid Mediator Results
# by Ewan Cowhig Croft
# 2031 - 2040
##################################################

#	Results event.
scheme_critical_moments.2031 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.0032
			FAILURE_EVENT = laamp_base_contract_schemes.0033
		}
	}
}

##################################################
# Confidence trickster Results
# by Ewan Cowhig Croft
# 2041 - 2050
##################################################

#	Results event.
scheme_critical_moments.2041 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.0042
			FAILURE_EVENT = laamp_base_contract_schemes.0043
		}
	}
}












##################################################
# CONTRACT SCHEMES (MARTIAL)

##################################################
# Hunt criminals
# by Ewan Cowhig Croft
# 2201 - 2210
##################################################

#	Results event.
scheme_critical_moments.2201 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.1002
			FAILURE_EVENT = laamp_base_contract_schemes.1003
		}
	}
}

##################################################
# Act as hired muscle
# by Ewan Cowhig Croft
# 2211 - 2220
##################################################

#	Results event.
scheme_critical_moments.2211 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.1012
			FAILURE_EVENT = laamp_base_contract_schemes.1013
		}
	}
}

##################################################
# Help train local MaA
# by Ewan Cowhig Croft
# 2221 - 2230
##################################################

#	Results event.
scheme_critical_moments.2221 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.1022
			FAILURE_EVENT = laamp_base_contract_schemes.1023
		}
	}
}

##################################################
# Garrison service
# by Ewan Cowhig Croft
# 2231 - 2240
##################################################

#	Results event.
scheme_critical_moments.2231 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.1032
			FAILURE_EVENT = laamp_base_contract_schemes.1033
		}
	}
}

##################################################
# Ambush travellers
# by Ewan Cowhig Croft
# 2241 - 2250
##################################################

#	Results event.
scheme_critical_moments.2241 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.1042
			FAILURE_EVENT = laamp_base_contract_schemes.1043
		}
	}
}












##################################################
# CONTRACT SCHEMES (STEWARDSHIP)

##################################################
# Hunt criminals
# by Ewan Cowhig Croft
# 2401 - 2410
##################################################

#	Results event.
scheme_critical_moments.2401 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.2002
			FAILURE_EVENT = laamp_base_contract_schemes.2003
		}
	}
}

##################################################
# Act as hired muscle
# by Ewan Cowhig Croft
# 2411 - 2420
##################################################

#	Results event.
scheme_critical_moments.2411 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.2012
			FAILURE_EVENT = laamp_base_contract_schemes.2013
		}
	}
}

##################################################
# Help train local MaA
# by Ewan Cowhig Croft
# 2421 - 2430
##################################################

#	Results event.
scheme_critical_moments.2421 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.2022
			FAILURE_EVENT = laamp_base_contract_schemes.2023
		}
	}
}

##################################################
# Garrison service
# by Ewan Cowhig Croft
# 2431 - 2440
##################################################

#	Results event.
scheme_critical_moments.2431 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.2032
			FAILURE_EVENT = laamp_base_contract_schemes.2033
		}
	}
}

##################################################
# Ambush travellers
# by Ewan Cowhig Croft
# 2441 - 2450
##################################################

#	Results event.
scheme_critical_moments.2441 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.2042
			FAILURE_EVENT = laamp_base_contract_schemes.2043
		}
	}
}












##################################################
# CONTRACT SCHEMES (INTRIGUE)

##################################################
# Provide intelligence from your trips
# by Ewan Cowhig Croft
# 2601 - 2610
##################################################

#	Results event.
scheme_critical_moments.2601 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.3002
			FAILURE_EVENT = laamp_base_contract_schemes.3003
		}
	}
}

##################################################
# Murder a character's rival
# by Ewan Cowhig Croft
# 2611 - 2620
##################################################

#	Results event.
scheme_critical_moments.2611 = {
	hidden = yes

	immediate = {
		save_scope_value_as = {
			name = contract_end_success
			value = event_id:laamp_base_contract_schemes.3012
		}
		save_scope_value_as = {
			name = contract_end_failure
			value = event_id:laamp_base_contract_schemes.3013
		}
		save_scope_value_as = {
			name = suppress_reroll_option
			value = yes
		}
		laamp_base_contract_schemes_set_up_outcome_immediate_effect = yes
		scope:task_contract.var:target = { save_scope_as = target }
		scope:scheme = {
			generic_scheme_process_ending_effect = {
				RESULTS_TOOLTIP = murder_successful_roll_tt
				SUCCESS_EVENT_TYPE = on_action
				SUCCESS_EVENT_ID = murder_success_pre_filter_on_action
				FAILURE_EVENT_TYPE = on_action
				FAILURE_EVENT_ID = murder_failure_pre_filter_on_action
			}
		}
	}
}

##################################################
# Abduct a character's rival
# by Ewan Cowhig Croft
# 2621 - 2630
##################################################

#	Results event.
scheme_critical_moments.2621 = {
	hidden = yes

	immediate = {
		save_scope_value_as = {
			name = contract_end_success
			value = event_id:laamp_base_contract_schemes.3022
		}
		save_scope_value_as = {
			name = contract_end_failure
			value = event_id:laamp_base_contract_schemes.3023
		}
		save_scope_value_as = {
			name = suppress_reroll_option
			value = yes
		}
		laamp_base_contract_schemes_set_up_outcome_immediate_effect = yes
		scope:task_contract.var:target = { save_scope_as = target }
		random_list = {
			100 = {
				save_scope_value_as = {
					name = abduct_method
					value = flag:seized_roadside
				}
			}
			100 = {
				save_scope_value_as = {
					name = abduct_method
					value = flag:on_the_way
				}
			}
			100 = {
				save_scope_value_as = {
					name = abduct_method
					value = flag:unwelcome_guests
				}
			}
		}
		scope:scheme = {
			generic_scheme_process_ending_effect = {
				RESULTS_TOOLTIP = abduct_successful_roll_tt
				SUCCESS_EVENT_TYPE = id
				SUCCESS_EVENT_ID = scheme_critical_moments.2622
				FAILURE_EVENT_TYPE = id
				FAILURE_EVENT_ID = scheme_critical_moments.2623
			}
		}
	}
}

scheme_critical_moments.2622 = {
	hidden = yes
	scope = scheme
	immediate = {
		scheme_owner = {
			trigger_event = { on_action = abduct_succeeded }
		}
	}
}

scheme_critical_moments.2623 = {
	hidden = yes
	scope = scheme
	immediate = {
		scheme_owner = {
			trigger_event = { on_action = abduct_failed }
		}
	}
}

##################################################
# Attack the property of a character's rival
# by Ewan Cowhig Croft
# 2631 - 2640
##################################################

#	Results event.
scheme_critical_moments.2631 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.3032
			FAILURE_EVENT = laamp_base_contract_schemes.3033
		}
	}
}

##################################################
# Heist from a ruler's treasury
# by Ewan Cowhig Croft
# 2641 - 2650
##################################################

#	Results event.
scheme_critical_moments.2641 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.3042
			FAILURE_EVENT = laamp_base_contract_schemes.3043
		}
	}
}












##################################################
# CONTRACT SCHEMES (LEARNING)


##################################################
# Copy texts for a learned ruler
# by Ewan Cowhig Croft
# 2801 - 2810
##################################################

#	Results event.
scheme_critical_moments.2801 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.4002
			FAILURE_EVENT = laamp_base_contract_schemes.4003
		}
	}
}

##################################################
# Help settle a local theological argument
# by Ewan Cowhig Croft
# 2811 - 2820
##################################################

#	Results event.
scheme_critical_moments.2811 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.4012
			FAILURE_EVENT = laamp_base_contract_schemes.4013
		}
	}
}

##################################################
# Create a work of learning for a ruler
# by Ewan Cowhig Croft
# 2821 - 2830
##################################################

#	Results event.
scheme_critical_moments.2821 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.4022
			FAILURE_EVENT = laamp_base_contract_schemes.4023
		}
	}
}

##################################################
# Solicit Charity
# by Ewan Cowhig Croft
# 2831 - 2840
##################################################

#	Results event.
scheme_critical_moments.2831 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.4032
			FAILURE_EVENT = laamp_base_contract_schemes.4033
		}
	}
}

##################################################
# Sell bogus relics
# by Ewan Cowhig Croft
# 2841 - 2850
##################################################

#	Results event.
scheme_critical_moments.2841 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.4042
			FAILURE_EVENT = laamp_base_contract_schemes.4043
		}
	}
}

##################################################
# Seize Realm
# by Joe Parkin
# 2901 - 2910
##################################################

#	Results event.
scheme_critical_moments.2901 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			# Set up a block for the inappropriate cheating error?
			save_scope_value_as = {
				name = ignore_cheating_error_check
				value = yes
			}
			# Proceed to rolls.
			scheme_owner = {
				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					custom_tooltip = seize_realm_successful_roll_tt
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				# FATE ROLL
				random_list = {
					10 = {
						trigger = {
							trigger_if = {
								limit = { exists = scope:scheme_successful }
								scope:scheme.scheme_target_character = { is_ai = yes }
							}
							trigger_else = {
								scope:scheme.scheme_owner = { is_ai = yes }
							}
						}
						save_scope_as = death
					}
					10 = {
						modifier = {
							NOT = { exists = scope:scheme_successful }
							exists = scope:scheme_discovered
							factor = 2 
						}
						save_scope_as = imprison
					}
					20 = {}
				}
				# FIRE EVENTS
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = ep3_laamps.0401
				}
				else = { trigger_event = ep3_laamps.0402 }
			}
		}
	}
}











##################################################
# CONTRACT SCHEMES (PROWESS)

##################################################
# Hunt in the wilds
# by Ewan Cowhig Croft
# 3001 - 3010
##################################################

#	Results event.
scheme_critical_moments.3001 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.5002
			FAILURE_EVENT = laamp_base_contract_schemes.5003
		}
	}
}

##################################################
# Guard merchant properties
# by Ewan Cowhig Croft
# 3011 - 3020
##################################################

#	Results event.
scheme_critical_moments.3011 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.5012
			FAILURE_EVENT = laamp_base_contract_schemes.5013
		}
	}
}

##################################################
# Guard a local ruler at a minor event
# by Ewan Cowhig Croft
# 3021 - 3030
##################################################

#	Results event.
scheme_critical_moments.3021 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.5022
			FAILURE_EVENT = laamp_base_contract_schemes.5023
		}
	}
}

##################################################
# Go rustling
# by Ewan Cowhig Croft
# 3031 - 3040
##################################################

#	Results event.
scheme_critical_moments.3031 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.5032
			FAILURE_EVENT = laamp_base_contract_schemes.5033
		}
	}
}

##################################################
# Go poaching
# by Ewan Cowhig Croft
# 3041 - 3050
##################################################

#	Results event.
scheme_critical_moments.3041 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.5042
			FAILURE_EVENT = laamp_base_contract_schemes.5043
		}
	}
}

##################################################
# Muggings
# by Ewan Cowhig Croft
# 3051 - 3060
##################################################

#	Results event.
scheme_critical_moments.3051 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.5052
			FAILURE_EVENT = laamp_base_contract_schemes.5053
		}
	}
}

##################################################
# Protect Minority Vassal Faith/Culture
# by Arkadiusz Majewski
# 3201 - 3210
##################################################

#	Results event.
scheme_critical_moments.3201 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_extra_contract_schemes.0002
			FAILURE_EVENT = laamp_extra_contract_schemes.0003
		}
	}
}

##################################################
# Prison Break
# by Arkadiusz Majewski
# 3211 - 3220
##################################################

#	Results event.
scheme_critical_moments.3211 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_extra_contract_schemes.0012
			FAILURE_EVENT = laamp_extra_contract_schemes.0013
		}
	}
}

##################################################
# Laamp helps find secrets
# 3221 - 3230
##################################################

#	Results event.
scheme_critical_moments.3221 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_extra_contract_schemes.0023
			FAILURE_EVENT = laamp_extra_contract_schemes.0024
		}
	}
}

##################################################
# Foster Legitimacy Results
# Veronica Pazos
# 4000 - 4009
##################################################

# Roll the die to see if you succeed/fail
scheme_critical_moments.4000 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.4001
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.4002
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.4001 = {
	type = character_event
	title = scheme_critical_moments.4001.t
	desc = {
		desc = scheme_critical_moments.4001.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					#EK EDIT
					#scope:target = { has_title = title:e_byzantium }
					always = no
				}
				desc = scheme_critical_moments.4001.desc.byzantium
			}
			desc = scheme_critical_moments.4001.desc.other
		}
		desc = scheme_critical_moments.4001.desc.outro
	}
	theme = diplomacy
	left_portrait = {
		character = root
		animation = toast_goblet
	}
	lower_right_portrait = scope:target

	immediate = {
		scope:target = {
			send_interface_message = {
				type = msg_legitimacy_fostered_scheme
				title = scheme_critical_moments.4001.tt
				desc = foster_legitimacy_scheme_toast_desc
				left_icon = scope:owner
				add_legitimacy = medium_legitimacy_gain
			}
		}
	}

	option = {
		name = scheme_critical_moments.4001.a
		scope:target = {
			add_opinion = {
				modifier = fostered_legitimacy_opinion
				target = root
			}
		}
		if = {
			limit = {
				scope:scheme.scheme_success_chance >= 90
				can_add_hook = {
					target = scope:target
					type = strong_favor_hook
				}
			}
			random = {
				chance = 50
				add_hook = {
					type = strong_favor_hook
					target = scope:target
				}
			}
		}
		else_if = {
			limit = {
				scope:scheme.scheme_success_chance >= 75
				can_add_hook = {
					target = scope:target
					type = favor_hook
				}
			}
			random = {
				chance = 50
				add_hook = {
					type = favor_hook
					target = scope:target
				}
			}
		}
		scope:scheme = {
			end_scheme = yes
		}
	}
}

#Failure
scheme_critical_moments.4002 = {
	type = character_event
	title = scheme_critical_moments.4002.t
	desc = scheme_critical_moments.4002.desc
	theme = diplomacy
	left_portrait = {
		character = root
		animation = disapproval
	}
	override_background = { reference = council_chamber }
	lower_right_portrait = scope:target

	option = {
		name = scheme_critical_moments.4002.a
		add_prestige = medium_prestige_loss
		scope:scheme = {
			end_scheme = yes
		}
	}
}

##################################################
# Damage Legitimacy Results
# Veronica Pazos
# 4010 - 4019
##################################################

# Roll the die to see if you succeed/fail
scheme_critical_moments.4010 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance 

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.4011
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.4012
						days = 1
					}	
				}
			}
		}
	}
}

#Success
scheme_critical_moments.4011 = {
	type = character_event
	title = scheme_critical_moments.4011.t
	desc = scheme_critical_moments.4011.desc
	theme = intrigue
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				NOT = {
					has_trait = calm
					has_trait = callous
				}
			}
			animation = manic
		}
		animation = manic
	}
	lower_right_portrait = scope:target
	override_background = { reference = council_chamber }

	immediate = {
		scope:target = {
			send_interface_message = {
				type = msg_legitimacy_damaged_scheme
				title = scheme_critical_moments.4011.tt
				desc = damage_legitimacy_scheme_toast_desc
				left_icon = scope:owner
				add_legitimacy = minor_legitimacy_loss
				add_opinion = {
					target = root
					modifier = damaged_legitimacy_opinion
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.4011.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

#Failure
scheme_critical_moments.4012 = {
	type = character_event
	title = scheme_critical_moments.4012.t
	desc = {
		desc = scheme_critical_moments.4012.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					#EK EDIT
					#scope:target = { has_title = title:e_byzantium }
					always = no
				}
				desc = scheme_critical_moments.4012.desc.byzantium
			}
			desc = scheme_critical_moments.4012.desc.other
		}
		desc = scheme_critical_moments.4012.desc.outro
	}
	theme = intrigue
	left_portrait = {
		character = root
		animation = disapproval
	}
	lower_right_portrait = scope:target

	option = {
		name = scheme_critical_moments.4012.a
		change_influence = medium_influence_loss
		scope:scheme = {
			end_scheme = yes
		}
	}
}

##################################################
# Protect the Innocent
# 6001 - 6003
##################################################

#	Results event.
scheme_critical_moments.6001 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.6002
			FAILURE_EVENT = laamp_base_contract_schemes.6003
		}
	}
}

##################################################
# Rescue a Fair Non-Combatant Gender
# 6011 - 6013
##################################################

#	Results event.
scheme_critical_moments.6011 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.6012
			FAILURE_EVENT = laamp_base_contract_schemes.6013
		}
	}
}

##################################################
# Participate in a Chivalry Play
# 6021 - 6023
##################################################

#	Results event.
scheme_critical_moments.6021 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.6022
			FAILURE_EVENT = laamp_base_contract_schemes.6023
		}
	}
}

##################################################
# Do a Pas d'Armes
# 6021 - 6023
##################################################

#	Results event.
scheme_critical_moments.6031 = {
	hidden = yes

	immediate = {
		roll_scheme_success_or_failure_effect = {
			SUCCESS_EVENT = laamp_base_contract_schemes.6032
			FAILURE_EVENT = laamp_base_contract_schemes.6033
		}
	}
}

##################################################
# Dispute Borders
# Joe Parkin
# 8001 - 8004
##################################################

#	Results event for the Dispute Borders scheme.
scheme_critical_moments.8001 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_title.holder = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = {
						id = scheme_critical_moments.8002
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.8003
						days = 1
					}	
				}
			}
		}
	}
}

scripted_effect dispute_border_success_effect = {
	if = {
		limit = {
			scope:target = {
				highest_held_title_tier >= tier_duchy
			}
		}
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:target_title = {
			change_title_holder = {
				holder = scope:owner
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	else = {
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:target = {
			change_liege = {
				liege = scope:owner
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
}

#Success
scheme_critical_moments.8002 = {
	type = character_event
	title = scheme_critical_moments.8002.t
	desc = scheme_critical_moments.8002.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = scheme
	}
	right_portrait = {
		character = scope:target
		animation = anger
	}
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		# Trigger the response event for the target
		scope:target = { trigger_event = scheme_critical_moments.8004 }
		every_player = {
			limit = {
				top_liege ?= scope:owner.top_liege
				NOR = {
					this = scope:owner
					this = scope:target
				}
			}
			if = {
				limit = {	
					house ?= scope:target.house
					scope:owner.house != scope:target.house
				}
				send_interface_message = {
					type = msg_admin_border_change_bad_with_text
					title = scheme_critical_moments.8004.t
					left_icon = scope:owner
					right_icon = scope:target
					desc = dispute_border_toast_desc
					show_as_tooltip = { dispute_border_success_effect = yes }	
				}
			}
			else_if = {
				limit = {	
					house ?= scope:owner.house
					scope:owner.house != scope:target.house
				}
				send_interface_message = {
					type = msg_admin_border_change_good_with_text
					title = scheme_critical_moments.8004.t
					left_icon = scope:owner
					right_icon = scope:target
					desc = dispute_border_toast_desc
					show_as_tooltip = { dispute_border_success_effect = yes }	
				}
			}
			else = {
				send_interface_message = {
					type = msg_admin_border_change_neutral_with_text
					title = scheme_critical_moments.8004.t
					left_icon = scope:owner
					right_icon = scope:target
					desc = dispute_border_toast_desc
					show_as_tooltip = { dispute_border_success_effect = yes }	
				}
			}
		}
		dispute_border_success_effect = yes
		show_as_tooltip = { # Effects on target (applied in the response events)
			scope:target = {
				add_scheme_cooldown = {
					target = scope:owner
					type = dispute_border
					years = 5
				}
				if = {
					limit = {
						is_ai = yes
						highest_held_title_tier >= tier_duchy
					}
					add_opinion = {
						target = scope:owner
						modifier = attempted_dispute_border_opinion
					}
				}
				else_if = {
					limit = {
						is_ai = yes
					}
					add_opinion = {
						target = scope:owner
						modifier = attempted_dispute_border_opinion
						opinion = -40
					}
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.8002.a
		hidden_effect = {
			scope:scheme = { end_scheme = yes }
		}
	}
}

#Failure
scheme_critical_moments.8003 = {
	type = character_event
	title = scheme_critical_moments.8003.t
	desc = scheme_critical_moments.8003.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = disappointed
	}
	lower_right_portrait = scope:target
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		scope:target = {
			send_interface_message = {
				type = event_scheme_bad
				title = scheme_critical_moments.8003.t
				left_icon = scope:owner
				right_icon = scope:target.primary_title
				custom_tooltip = scheme_critical_moments.8003.tt.failed
			}
		}
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			deceitful = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8003.a
		scope:scheme = { end_scheme = yes }
	}
}

# Target reaction
scheme_critical_moments.8004 = {
	type = character_event
	title = scheme_critical_moments.8004.t
	desc = scheme_critical_moments.8004.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = anger
	}
	lower_right_portrait = scope:owner
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		show_as_tooltip = { dispute_border_success_effect = yes }
		add_scheme_cooldown = {
			target = scope:owner
			type = dispute_border
			years = 5
		}
		if = {
			limit = { is_ai = yes }
			hidden_effect = {
				remove_opinion = {
					modifier = attempted_dispute_border_opinion
					target = scope:owner
				}
			}
			add_opinion = {
				target = scope:owner
				modifier = dispute_border_opinion
			}
		}
	}

	option = {
		name = scheme_critical_moments.8004.a
	}
}

##################################################
# Subsume Province
# Joe Parkin
# 8011 - 8015
##################################################

#	Results event for the Subsume Province scheme.
scheme_critical_moments.8011 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_title.holder = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = {
						id = scheme_critical_moments.8012
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.8013
						days = 1
					}	
				}
			}
		}
	}
}

scripted_effect subsume_province_success_effect = {
	create_title_and_vassal_change = {
		type = appointment
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	scope:target_title = {
		every_de_jure_county = {
			limit = {
				county_held_or_vassal_to_target_trigger = { TARGET = scope:target }
			}
			add_to_list = transferred_counties
			change_title_holder = {
				holder = scope:owner
				change = scope:title_change
			}
		}
		change_title_holder = {
			holder = scope:owner
			change = scope:title_change
		}
		resolve_title_and_vassal_change = scope:title_change
	}
}

#Success
scheme_critical_moments.8012 = {
	type = character_event
	title = scheme_critical_moments.8012.t
	desc = scheme_critical_moments.8012.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = schadenfreude
		camera = camera_event_right
	}
	lower_right_portrait = scope:target
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		# Trigger the response event for the target
		scope:target = { trigger_event = scheme_critical_moments.8014 }
		every_player = {
			limit = {
				NOT = { this = scope:owner }
				top_liege = scope:owner.top_liege
			}
			trigger_event = scheme_critical_moments.8015
		}
		subsume_province_success_effect = yes
		show_as_tooltip = { # Effects on target (applied in the response events)
			add_scheme_cooldown = {
				target = scope:owner
				type = subsume_province
				years = 5
			}
			scope:target = {
				progress_towards_rival_effect = {
					REASON = rival_subsumed_province
					CHARACTER = scope:owner
					OPINION = -50
				}
			}
		}
	}

	option = {
		name = scheme_critical_moments.8012.a
		scope:scheme = { end_scheme = yes }
	}
}

#Failure
scheme_critical_moments.8013 = {
	type = character_event
	title = scheme_critical_moments.8013.t
	desc = scheme_critical_moments.8013.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = disappointed
	}
	lower_right_portrait = scope:target
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		hidden_effect = {
			scope:target = {
				send_interface_message = {
					type = event_scheme_bad
					title = scheme_critical_moments.8013.t
					left_icon = scope:owner
					right_icon = scope:target.primary_title
					custom_tooltip = scheme_critical_moments.8013.tt.failed
				}
			}
		}
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			deceitful = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8013.a
		scope:scheme = { end_scheme = yes }
	}
}

# Target reaction
scheme_critical_moments.8014 = {
	type = character_event
	title = scheme_critical_moments.8014.t
	desc = scheme_critical_moments.8014.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = anger
	}
	lower_right_portrait = scope:owner
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		show_as_tooltip = { subsume_province_success_effect = yes }
		add_scheme_cooldown = {
			target = scope:owner
			type = subsume_province
			years = 5
		}
		if = {
			limit = { is_ai = yes }
			hidden_effect = {
				remove_opinion = {
					modifier = attempted_subsume_province_opinion
					target = scope:owner
				}
			}
			progress_towards_rival_effect = {
				REASON = rival_subsumed_province
				CHARACTER = scope:owner
				OPINION = -50
			}
		}
	}

	option = {
		name = scheme_critical_moments.8014.a
	}
}

# Notifaction event
scheme_critical_moments.8015 = {
	type = character_event
	title = scheme_critical_moments.8015.t
	desc = {
		desc = scheme_critical_moments.8015.desc
		first_valid = {
			triggered_desc = {
				trigger = { this = scope:owner.top_liege }
				desc = scheme_critical_moments.8015.liege
			}
			desc = {
				desc = scheme_critical_moments.8015.vassal
				first_valid = {
					triggered_desc = {
						trigger = {
							house = scope:target.house
							scope:target.house = scope:owner.house
						}
						desc = scheme_critical_moments.8015.same_house
					}
					triggered_desc = {
						trigger = { house = scope:target.house }
						desc = scheme_critical_moments.8015.target_house
					}
					desc = scheme_critical_moments.8015.fallback
				}
			}
		}
	}
	theme = administrative
	override_background = { reference = throne_room_scope }
	left_portrait = {
		character = scope:owner
		animation = schadenfreude
	}
	right_portrait = {
		character = scope:target
		animation = anger
	}

	trigger = {
		NOR = {
			this = scope:owner
			this = scope:target
		}
	}

	immediate = {
		scope:owner = { save_scope_as = background_throne_room_scope }
		show_as_tooltip = { subsume_province_success_effect = yes }
	}

	option = {
		name = scheme_critical_moments.8015.a
	}
}

##################################################
# Found Despotate
# Joe Parkin
# 8021 - 8024
##################################################

#	Results event for the Found Despotate scheme.
scheme_critical_moments.8021 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_title.holder = {
				if = {
					limit = { is_governor = yes }
					save_scope_as = target
				}
				else = {
					liege = { save_scope_as = target }
				}
			}
			scheme_owner = {
				save_scope_as = owner
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				primary_title = { save_scope_as = old_primary }
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = {
						id = scheme_critical_moments.8022
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.8023
						days = 1
					}	
				}
			}
		}
	}
}

scripted_effect found_despotate_success_effect = {
	# Save the duchy where your capital is located for use in Change 2
	capital_county.duchy = {
		save_scope_as = old_primary_title
		# TODO_CD_EP3: TIT-56112 remove and fix loc for 1.13.1
		save_scope_as = destroyed_title
	}

	# Change 1: Give scheme owner new kingdom title and its de jure vassals
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	scope:owner = {
		add_character_flag = {
			flag = successful_governorship_elevation
			days = 5
		}
		liege = { save_scope_as = liege }
	}
	scope:target_title = {
		change_title_holder = {
			holder = scope:owner
			change = scope:title_change
		}
	}
	scope:owner = {
		# Send non de jure counties back to the emperor
		every_held_title = {
			limit = {
				tier = tier_county
				NOT = { target_is_de_jure_liege_or_above = scope:target_title }
			}
			change_title_holder = {
				holder = scope:owner.top_liege
				change = scope:title_change
			}
		}
		top_liege = {
			every_vassal = {
				limit = {
					NOT = { this = scope:owner }
					highest_held_title_tier >= tier_county
					highest_held_title_tier <= tier_duchy
					primary_title.kingdom = scope:target_title
				}
				add_to_list = transferred_vassals
				change_liege = {
					liege = scope:owner
					change = scope:title_change
				}
				if = {
					limit = {
						is_ai = yes
						highest_held_title_tier >= tier_duchy
						NOT = { house = scope:owner.house }
					}
					add_opinion = {
						target = scope:owner
						modifier = found_despotate_vassalized_opinion
					}
				}
			}
		}
		if = {
			limit = { liege != scope:liege }
			change_liege = {
				liege = scope:liege
				change = scope:title_change
			}
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	# Change 2: trigger succession on all but the primary governorship held by the scheme owner
	create_title_and_vassal_change = {
		type = stepped_down
		save_scope_as = title_change2
		add_claim_on_loss = no
	}
	scope:owner = {
		every_held_title = {
			limit = {
				tier = tier_duchy
				is_landless_type_title = no
				is_noble_family_title = no
				save_temporary_scope_as = current_duchy
				this != scope:old_primary_title
			}
			current_heir = { save_scope_as = next_holder }
			# Trigger succession for those governorships
			change_title_holder_include_vassals = {
				holder = scope:next_holder
				change = scope:title_change2
				take_baronies = no
			}
		}
	}
	resolve_title_and_vassal_change = scope:title_change2

	# Change 3: Send non de jure vassals back to the top liege
	create_title_and_vassal_change = {
		type = stepped_down
		save_scope_as = title_change3
		add_claim_on_loss = no
	}
	scope:owner = {
		every_vassal = {
			limit = {
				primary_title ={
					tier >= tier_county
					NOT = { target_is_de_jure_liege_or_above = scope:owner.primary_title }
				}
			}
			change_liege = {
				liege = scope:owner.top_liege
				change = scope:title_change3
			}
		}
	}
	resolve_title_and_vassal_change = scope:title_change3
}

# Success
scheme_critical_moments.8022 = {
	type = character_event
	title = scheme_critical_moments.8022.t
	desc = scheme_critical_moments.8022.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = war_over_tie
	}
	lower_left_portrait = scope:owner.top_liege

	immediate = {
		every_player = {
			limit = {
				top_liege ?= scope:owner.top_liege
				NOT = { this = scope:owner }
			}
			trigger_event = scheme_critical_moments.8024
		}
		found_despotate_success_effect = yes
	}

	option = {
		name = scheme_critical_moments.8022.a
		scope:scheme = { end_scheme = yes }
	}
}

# Failure
scheme_critical_moments.8023 = {
	type = character_event
	title = scheme_critical_moments.8023.t
	desc = scheme_critical_moments.8023.desc
	theme = administrative
	left_portrait = {
		character = root
		animation = disappointed
	}
	lower_right_portrait = scope:owner.top_liege

	immediate = {
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			deceitful = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8023.a
		scope:scheme = { end_scheme = yes }
	}
}

# Realm ping
scheme_critical_moments.8024 = {
	type = character_event
	title = scheme_critical_moments.8024.t
	desc = {
		desc = scheme_critical_moments.8024.desc
		first_valid = {
			triggered_desc = {
				trigger = { this = scope:owner.top_liege }
				desc = scheme_critical_moments.8024.liege
			}
			desc = {
				first_valid = {
					triggered_desc = {
						trigger = { is_in_list = transferred_vassals }
						desc = scheme_critical_moments.8024.vassalised
					}
					desc = scheme_critical_moments.8024.other
				}
				desc = scheme_critical_moments.8024.fallback
			}
		}
	}
	theme = administrative
	left_portrait = {
		character = root
		animation = war_over_tie
	}
	lower_right_portrait = scope:owner.top_liege

	immediate = {
		scope:owner = { save_scope_as = background_throne_room_scope }
		show_as_tooltip = { subsume_province_success_effect = yes }
	}

	option = {
		name = scheme_critical_moments.8024.a
	}
}

##################################################
# Raid Estate
# Joe Parkin
# 8031 - 8033
##################################################

scripted_effect raid_maiming_assignment_effect = {
	scope:scheme_maim = {
		every_in_list = {
			list = target_victims
			random_list = {
				10 = {
					trigger = {
						NOT = { has_trait = blind }
					}
					add_character_flag = raid_maiming_blind
				}
				10 = {
					trigger = {
						NOT = { has_trait = disfigured }
					}
					add_character_flag = raid_maiming_disfigured
				}
				10 = {
					trigger = {
						is_male = yes
						NOT = { has_trait = eunuch }
					}
					add_character_flag = raid_maiming_eunuch
				}
			}
		}
	}
}

#	Results event for the Raid Estate scheme.
scheme_critical_moments.8031 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = {
				save_scope_as = target
				domicile ?= { save_scope_as = target_domicile }
			}
			scheme_owner = {
				save_scope_as = owner
				if = {
					limit = { var:raid_estate_permission ?= scope:target.house }
					save_scope_as = scheme_permission
				}
				#DISCOVERY ROLL
				random = {
					chance = scope:scheme.scheme_secrecy
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				if = {
					limit = { exists = scope:scheme_successful }
					save_scope_value_as = {
						name = gold
						value = {
							value = scope:target.tiny_gold_value
							multiply = scope:target_domicile.num_domicile_buildings
							add = scope:target.tiny_gold_value 
						}
					}
					if = { # Successful imprisonment always exposes involvement
						limit = { exists = scope:scheme_imprison }
						save_scope_value_as = {
							name = scheme_discovered
							value = yes
						}
					}
					scope:target_domicile = { destroy_random_estate_building_variable_effect = yes }
					scope:target.house = {
						every_house_member = {
							limit = {
								age >= 8
								is_playable_character = no
								trigger_if = {
									limit = { exists = primary_spouse }
									primary_spouse = { is_landed = no }
								}
								top_liege = scope:target.top_liege
								NOT = { this = scope:target }
							}
							add_to_list = target_house_members
						}
						ordered_in_list = {
							list = target_house_members
							limit = {
								trigger_if = {
									limit = { exists = scope:scheme_maim }
									NOR = {
										has_trait = blind
										has_trait = eunuch_1
										has_trait = disfigured
									}
								}
							}
							order_by = {
								value = 1
								if = {
									limit = { is_adult = yes }
									add = 10
								}
								if = {
									limit = { this = scope:target.player_heir }
									add = -10
								}
								if = {
									limit = {
										can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:target }
									}
									add = 10
								}
							}
							check_range_bounds = no
							max = 3
							add_to_list = target_victims
						}
						random_in_list = {
							list = target_victims
							limit = { is_heir_of = scope:target }
							alternative_limit = { is_child_of = scope:target }
							alternative_limit = { is_close_family_of = scope:target }
							alternative_limit = { always = yes }
							save_scope_as = flavor_victim
						}
					}
					random = {
						chance = {
							value = scope:scheme.scheme_success_chance
							divide = 2
						}
						save_scope_value_as = {
							name = scheme_critical_success
							value = yes
						}
					}
					# Bonus Rewards
					random_list = {
						10 = { # Steal an Artifact
							trigger = {
								scope:target = {
									any_character_artifact = { is_equipped = no }
								}
							}
							modifier = {
								exists = scope:scheme_discovered
								factor = 2
							}
							modifier = {
								exists = scope:scheme_critical_success
								factor = 2
							}
							scope:target = {
								random_character_artifact = {
									limit = { is_equipped = no }
									weight = {
										base = 1
										# Prefer worse artifacts
										modifier = {
											rarity = masterwork
											factor = 5
										}
										modifier = {
											rarity = common
											factor = 10
										}
										# Prefer equippable artifacts
										modifier = {
											scope:owner = { can_equip_artifact = prev }
											factor = 2
										}
										# Don't like banners
										modifier = {
											exists = var:banner_dynasty
											factor = 0.1
										}
										modifier = {
											exists = var:banner_house
											factor = 0.1
										}
									}
									save_scope_as = stolen_artifact
								}
							}
						}
						10 = { # Building
							trigger = {
								scope:target_domicile = {
									OR = {
										has_domicile_building_or_higher = living_quarters_01
										has_domicile_building_or_higher = trophy_room_01
										has_domicile_building_or_higher = office_01
										has_domicile_building_or_higher = servants_quarters_01
										has_domicile_building_or_higher = library_01
										has_domicile_building_or_higher = bath_01
										has_domicile_building_or_higher = guest_room_01
										has_domicile_building_or_higher = wine_cellar_01
										has_domicile_building_or_higher = prison_01
										has_domicile_building_or_higher = reception_hall_01
										has_domicile_building_or_higher = cabinet_of_curiosities_01
										has_domicile_building_or_higher = grand_solar_01
										has_domicile_building_or_higher = temple_small_01
										has_domicile_building_or_higher = barracks_01
										has_domicile_building_or_higher = watchtower_01
										has_domicile_building_or_higher = guardhouse_01
										has_domicile_building_or_higher = garden_01
										has_domicile_building_or_higher = stable_01
										has_domicile_building_or_higher = workshop_01
										has_domicile_building_or_higher = storage_01
										has_domicile_building_or_higher = grazing_land_01
										has_domicile_building_or_higher = grain_field_01
										has_domicile_building_or_higher = vineyard_01
										has_domicile_building_or_higher = olive_01
										has_domicile_building_or_higher = silk_01
									}
								}
							}
							random_list = {
								10 = { # Diplomacy Upgrade
									trigger = {
										ep3_raid_insight_trigger = { TYPE = living_quarters }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = living_quarters_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:living_quarters }
								}
								10 = { # Martial Upgrade
									trigger = {
										ep3_raid_insight_trigger = { TYPE = trophy_room }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = trophy_room_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:trophy_room }
								}
								10 = { # Stewardship Upgrade
									trigger = {
										ep3_raid_insight_trigger = { TYPE = office }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = office_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:office }
								}
								10 = { # Intrigue Upgrade
									trigger = {
										ep3_raid_insight_trigger = { TYPE = servants_quarters }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = servants_quarters_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:servants_quarters }
								}
								10 = { # Learning Upgrade
									trigger = {
										ep3_raid_insight_trigger = { TYPE = library }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = library_observatory_04
												has_domicile_building = library_education_04
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:library }
								}
								10 = { # Bath
									trigger = {
										ep3_raid_insight_trigger = { TYPE = bath }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = bath_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:bath }
								}
								10 = { # Guest Room
									trigger = {
										ep3_raid_insight_trigger = { TYPE = guest_room }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = guest_room_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:guest_room }
								}
								10 = { # Wine Cellar
									trigger = {
										ep3_raid_insight_trigger = { TYPE = wine_cellar }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = wine_cellar_03 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:wine_cellar }
								}
								10 = { # Courtyard
									trigger = {
										ep3_raid_insight_trigger = { TYPE = courtyard }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = courtyard_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:courtyard }
								}
								10 = { # Prison
									trigger = {
										ep3_raid_insight_trigger = { TYPE = prison }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = prison_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:prison }
								}
								10 = { # Reception Hall
									trigger = {
										ep3_raid_insight_trigger = { TYPE = reception_hall }
										scope:owner.dynasty ?= { has_dynasty_perk = ep3_administrative_legacy_3 }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = reception_hall_05 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:reception_hall }
								}
								10 = { # Cabinet of Curiosities
									trigger = {
										ep3_raid_insight_trigger = { TYPE = cabinet_of_curiosities }
										scope:owner.dynasty ?= { has_dynasty_perk = ep3_administrative_legacy_4 }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = cabinet_of_curiosities_03 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:cabinet_of_curiosities }
								}
								10 = { # Grand Solar
									trigger = {
										ep3_raid_insight_trigger = { TYPE = grand_solar }
										scope:owner.house.house_head = { has_character_flag = ep3_construct_grand_solar }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = grand_solar_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:grand_solar }
								}
								10 = { # Temple
									trigger = {
										scope:owner = {
											NOT = { has_character_modifier = raid_insight_temple }
										}
										scope:target_domicile = { has_domicile_building_or_higher = temple_small_01 }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = temple_crypt_06
												has_domicile_building = temple_large_06
												has_domicile_building = temple_monastery_06
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:temple }
								}
								10 = { # Watchtower
									trigger = {
										ep3_raid_insight_trigger = { TYPE = barracks }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = barracks_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:barracks }
								}
								10 = { # Watchtower
									trigger = {
										ep3_raid_insight_trigger = { TYPE = watchtower }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = watchtower_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:watchtower }
								}
								10 = { # Guardhouse
									trigger = {
										ep3_raid_insight_trigger = { TYPE = guardhouse }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = guardhouse_04 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:guardhouse }
								}
								10 = { # Garden
									trigger = {
										ep3_raid_insight_trigger = { TYPE = garden }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = garden_leisure_06
												has_domicile_building = garden_fruit_06
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:garden }
								}
								10 = { # Stable
									trigger = {
										ep3_raid_insight_trigger = { TYPE = stable }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = stable_grand_06
												has_domicile_building = stable_kennel_06
												has_domicile_building = stable_chariot_06
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:stable }
								}
								10 = { # Workshop
									trigger = {
										ep3_raid_insight_trigger = { TYPE = workshop }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = workshop_carpenter_06
												has_domicile_building = workshop_mason_06
												has_domicile_building = workshop_textile_06
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:workshop }
								}
								10 = { # Storage
									trigger = {
										ep3_raid_insight_trigger = { TYPE = storage }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = storage_warehouse_04
												has_domicile_building = storage_granary_04
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:storage }
								}
								10 = { # Market
									trigger = {
										ep3_raid_insight_trigger = { TYPE = market }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = market_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:market }
								}
								10 = { # Grazing Lands
									trigger = {
										ep3_raid_insight_trigger = { TYPE = grazing_land }
										scope:owner.domicile ?= {
											NOR = {
												has_domicile_building = grazing_land_06
												has_domicile_building = horse_pasture_06
												has_domicile_building = camel_pasture_06
												has_domicile_building = elephant_pasture_06
											}
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:grazing_land }
								}
								10 = { # Grain Field
									trigger = {
										ep3_raid_insight_trigger = { TYPE = grain_field }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = grain_field_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:grain_field }
								}
								10 = { # Vineyard
									trigger = {
										ep3_raid_insight_trigger = { TYPE = vineyard }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = vineyard_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:vineyard }
								}
								10 = { # Olive Plantation
									trigger = {
										ep3_raid_insight_trigger = { TYPE = olive }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = olive_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:olive }
								}
								10 = { # Silk Production
									trigger = {
										ep3_raid_insight_trigger = { TYPE = silk }
										scope:owner.house = { has_house_modifier = ep3_unlocked_silk }
										scope:owner.domicile ?= {
											NOT = { has_domicile_building = silk_06 }
										}
									}
									save_scope_value_as = { name = stolen_building value = flag:silk }
								}
							}	
						}
					}
					#Need to pre assign random maiming
					if = {
						limit = {
							exists = scope:scheme_maim
						}
						raid_maiming_assignment_effect = yes
					}
					trigger_event = {
						id = scheme_critical_moments.8032
						days = 1
					}
				}
				else = {
					scope:scheme = {
						if = {
							limit = { exists = scope:scheme_discovered }
							random_scheme_agent_character = {
								limit = { is_alive = yes }
								weight = {
									base = 1
									modifier = {
										add = {
											value = 20
											subtract = intrigue
											subtract = prowess
										}
									}
								}
								save_scope_as = captured
							}
						}
					}
					trigger_event = {
						id = scheme_critical_moments.8033
						days = 1
					}	
				}
			}
		}
	}
}

scripted_effect raid_estate_success_effect = {
	raid_estate_success_public_effect = yes
	if = {
		limit = { exists = scope:stolen_artifact }
		scope:stolen_artifact = {
			set_owner = {
				target = scope:owner
				history = {
					location = scope:target_domicile.domicile_location
					actor = scope:owner
					recipient = scope:owner
					type = stolen
				}
			}
		}
	}
	else_if = {
		limit = { exists = scope:stolen_building }
		scope:owner = {
			switch = {
				trigger = scope:stolen_building
				flag:living_quarters = { ep3_raid_insight_effect = { TYPE = living_quarters } }
				flag:trophy_room = { ep3_raid_insight_effect = { TYPE = trophy_room } }
				flag:office = { ep3_raid_insight_effect = { TYPE = office } }
				flag:servants_quarters = { ep3_raid_insight_effect = { TYPE = servants_quarters } }
				flag:library = { ep3_raid_insight_effect = { TYPE = library } }
				flag:bath = { ep3_raid_insight_effect = { TYPE = bath } }
				flag:guest_room = { ep3_raid_insight_effect = { TYPE = guest_room } }
				flag:wine_cellar = { ep3_raid_insight_effect = { TYPE = wine_cellar } }
				flag:prison = { ep3_raid_insight_effect = { TYPE = prison } }
				flag:reception_hall = { ep3_raid_insight_effect = { TYPE = reception_hall } }
				flag:cabinet_of_curiosities = { ep3_raid_insight_effect = { TYPE = cabinet_of_curiosities } }
				flag:grand_solar = { ep3_raid_insight_effect = { TYPE = grand_solar } }
				flag:temple = { ep3_raid_insight_effect = { TYPE = temple } }
				flag:barracks = { ep3_raid_insight_effect = { TYPE = barracks } }
				flag:watchtower = { ep3_raid_insight_effect = { TYPE = watchtower } }
				flag:guardhouse = { ep3_raid_insight_effect = { TYPE = guardhouse } }
				flag:garden = { ep3_raid_insight_effect = { TYPE = garden } }
				flag:stable = { ep3_raid_insight_effect = { TYPE = stable } }
				flag:workshop = { ep3_raid_insight_effect = { TYPE = workshop } }
				flag:storage = { ep3_raid_insight_effect = { TYPE = storage } }
				flag:grazing_land = { ep3_raid_insight_effect = { TYPE = grazing_land } }
				flag:grain_field = { ep3_raid_insight_effect = { TYPE = grain_field } }
				flag:vineyard = { ep3_raid_insight_effect = { TYPE = vineyard } }
				flag:olive = { ep3_raid_insight_effect = { TYPE = olive } }
				flag:silk = { ep3_raid_insight_effect = { TYPE = silk } }
			}
		}
	}
	if = {
		limit = {
			NOT = { exists = scope:scheme_discovered }
		}
		scope:owner = {
			add_secret = {
				type = secret_raid_estate
				target = scope:target
			}
			random_secret = {
				limit = {
					secret_type = secret_raid_estate
					secret_target = scope:target
					NOT = { exists = var:house }
				}
				save_scope_as = secret
				set_variable = {
					name = house
					value = scope:target.house
				}
				if = {
					limit = { exists = scope:scheme_maim }
					set_variable = {
						name = maim
						value = flag:yes
					}
				}
				every_in_list = {
					list = target_victims
					save_scope_as = secret_victim
					scope:secret = {
						add_to_variable_list = {
							name = victims
							target = scope:secret_victim
						}
					}
				}
			}
		}
	}
}

scripted_effect raid_estate_success_public_effect = {
	scope:owner = { add_short_term_gold = scope:gold }
	scope:target = { change_influence = medium_influence_loss }
	switch = {
		trigger = exists
		scope:scheme_kill = {
			every_in_list = {
				list = target_victims
				death = {
					death_reason = death_raid_estate
					killer = scope:owner
				}
				if = {
					limit = { exists = scope:scheme_discovered }
					hidden_effect = { set_killer_public = yes }
				}
			}
		}
		scope:scheme_maim = {
			every_in_list = {
				list = target_victims
				random_list = {
					10 = {
						trigger = {
							NOT = { has_trait = blind }
						}
						hidden_effect = {
							if = {
								limit = { has_trait = one_eyed }
								remove_trait = one_eyed
							}
						}
						add_trait = blind
					}
					10 = {
						trigger = {
							NOT = { has_trait = disfigured }
						}
						add_trait = disfigured
					}
					10 = {
						trigger = {
							is_male = yes
							NOT = { has_trait = eunuch }
						}
						add_trait = eunuch_1
					}
				}
			}
		}
		scope:scheme_imprison = {
			every_in_list = {
				list = target_victims
				save_scope_as = victim
				rightfully_imprison_character_less_verbose_effect = {
					TARGET = scope:victim
					IMPRISONER = scope:owner
				}
			}
		}
	}
	scope:target_domicile = { destroy_random_estate_building_effect = yes }
	scope:target.house = {
		add_house_modifier = {
			modifier = ep3_estate_raided_modifier
			years = 5
		}
	}
	if = {
		limit = { exists = scope:scheme_discovered }
		raid_estate_opinion_effect = {
			OWNER = scope:owner
			TARGET = scope:target
		}
	}
	scope:owner = {
		if = {
			limit = { var:raid_estate_permission ?= scope:target.house }
			remove_variable =  raid_estate_permission
		}
	}
}

# Success
scheme_critical_moments.8032 = {
	type = character_event
	title = scheme_critical_moments.8032.t
	desc = {
		desc = scheme_critical_moments.8032.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_kill
					any_in_list = {
						list = target_victims
						count > 0
					}
				}
				desc = scheme_critical_moments.8032.kill
			}
			triggered_desc = {
				trigger = {
					exists = scope:scheme_maim
					any_in_list = {
						list = target_victims
						count > 0
					}
				}
				desc = scheme_critical_moments.8032.maim
			}
			triggered_desc = {
				trigger = {
					exists = scope:scheme_imprison
					any_in_list = {
						list = target_victims
						count > 0
					}
				}
				desc = scheme_critical_moments.8032.imprison
			}
			desc = scheme_critical_moments.8032.fallback
		}
		first_valid = {
			triggered_desc = {
				trigger = { scope:owner = scope:owner.top_liege }
				desc = scheme_critical_moments.8032.emperor
			}
			triggered_desc = {
				trigger = { exists = scope:scheme_permission }
				desc = scheme_critical_moments.8032.permission
			}
			triggered_desc = {
				trigger = { exists = scope:scheme_discovered }
				desc = scheme_critical_moments.8032.crime
			}
			desc = scheme_critical_moments.8032.secret
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:target
		triggered_animation = {
			trigger = {
				exists = scope:scheme_kill
				any_in_list = {
					list = target_victims
					count > 0
				}
			}
			animation = grief
		}
		triggered_animation = {
			trigger = {
				exists = scope:scheme_maim
				any_in_list = {
					list = target_victims
					count > 0
				}
			}
			animation = rage
		}
		animation = disbelief
	}
	lower_left_portrait = scope:flavor_victim
	lower_right_portrait = {
		trigger = {
			scope:owner.top_liege != scope:owner
			scope:owner.top_liege != scope:target
			exists = scope:scheme_discovered
		}
		character = scope:owner.top_liege
	}
	override_background = { reference = burning_building }

	immediate = {
		play_music_cue = "mx_cue_murder"

		scope:target = { trigger_event = scheme_critical_moments.8034 } # Trigger the response event for the target
		raid_estate_success_effect = yes
		if = {
			limit = {
				is_ai = no
				exists = scope:scheme_permission
				house ?= { is_powerful_family = no }
				scope:target.house ?= { is_powerful_family = yes }
			}
			add_achievement_global_variable_effect = {
				VARIABLE = achieved_i_got_a_permit_achievement
				VALUE = yes
			}
		}
	}

	option = {
		name = {
			trigger = { exists = scope:scheme_discovered }
			text = scheme_critical_moments.8032.a
		}
		name = {
			trigger = {
				NOT = { exists = scope:scheme_discovered }
			}
			text = scheme_critical_moments.8032.a.secret
		}
		scope:scheme = { end_scheme = yes }
	}
	after = {
		#Hidden event that cleans up flags
		if = {
			limit = {
				exists = scope:scheme_maim
			}
			trigger_event = {
				id = scheme_critical_moments.8999
				days = 30
			}
		}
		
	}
}

scheme_critical_moments.8999 = {
	hidden = yes
	immediate = {
		scope:scheme_maim = {
			every_in_list = {
				list = target_victims
				remove_character_flag = raid_maiming_eunuch
				remove_character_flag = raid_maiming_blind
				remove_character_flag = raid_maiming_disfigured
			}
		}
	}
}

scripted_effect raid_estate_failure_effect = {
	if = {
		limit = { exists = scope:scheme_discovered }
		if = {
			limit = { exists = scope:captured }
			scope:captured = {
				add_character_flag = {
					flag = was_abducted_block_notification_event
					days = 2
				}
			}
			rightfully_imprison_character_less_verbose_effect = {
				TARGET = scope:captured
				IMPRISONER = scope:target
			}
		}
		scope:target.house.house_head = {
			add_opinion = {
				target = scope:owner
				modifier = attempted_raid_estate_crime
			}
		}
	}
}

# Failure
scheme_critical_moments.8033 = {
	type = character_event
	title = scheme_critical_moments.8033.t
	desc = {
		desc = scheme_critical_moments.8033.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:captured }
				desc = scheme_critical_moments.8033.captured
			}
			triggered_desc = {
				trigger = { exists = scope:scheme_discovered }
				desc = scheme_critical_moments.8033.discovered
			}
			desc = scheme_critical_moments.8033.fallback
		}
		first_valid = {
			triggered_desc = {
				trigger = { scope:owner = scope:owner.top_liege }
				desc = scheme_critical_moments.8032.emperor
			}
			triggered_desc = {
				trigger = { exists = scope:scheme_permission }
				desc = scheme_critical_moments.8032.permission
			}
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:target
		animation = anger
	}
	right_portrait = {
		character = scope:captured
		animation = prisonhouse
		override_imprisonment_visuals = yes
	}
	lower_right_portrait = {
		trigger = {
			scope:owner.top_liege != scope:owner
			scope:owner.top_liege != scope:target
			exists = scope:scheme_discovered
		}
		character = scope:owner.top_liege
	}
	override_background = { reference = ep3_mediterranean_estate }

	immediate = {
		play_music_cue = "mx_cue_murder"
		scope:target = { trigger_event = scheme_critical_moments.8034 } # Trigger the response event for the target
		raid_estate_failure_effect = yes
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			deceitful = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8033.a
		scope:scheme = { end_scheme = yes }
	}
}

# Target
scheme_critical_moments.8034 = {
	type = character_event
	title = {
		desc = {
			first_valid = {
				triggered_desc = {
					trigger = { exists = scope:scheme_discovered }
					desc = scheme_critical_moments.8034.t.known
				}
				desc = scheme_critical_moments.8034.t
			}
		}
	}
	desc = {
		desc = scheme_critical_moments.8034.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:scheme_successful }
				desc = scheme_critical_moments.8034.success
			}
			desc = scheme_critical_moments.8034.failure
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_kill
					any_in_list = {
						list = target_victims
						count > 0
					}
				}
				desc = scheme_critical_moments.8034.kill
			}
			triggered_desc = {
				trigger = {
					exists = scope:scheme_maim
					any_in_list = {
						list = target_victims
						count > 0
					}
				}
				desc = scheme_critical_moments.8034.maim
			}
			triggered_desc = {
				trigger = {
					exists = scope:scheme_imprison
					any_in_list = {
						list = target_victims
						count > 0
					}
				}
				desc = scheme_critical_moments.8034.imprison
			}
			triggered_desc = {
				trigger = { exists = scope:captured }
				desc = scheme_critical_moments.8034.captured
			}
		}
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:scheme_discovered }
				desc = scheme_critical_moments.8034.discovered
			}
			desc = scheme_critical_moments.8034.fallback
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
					scope:owner = scope:owner.top_liege
				}
				desc = scheme_critical_moments.8034.emperor
			}
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
					exists = scope:scheme_permission
				}
				desc = scheme_critical_moments.8034.permission
			}
			triggered_desc = {
				trigger = { exists = scope:scheme_discovered }
				desc = scheme_critical_moments.8034.crime
			}
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:target
		triggered_animation = {
			trigger = {
				exists = scope:scheme_successful
				exists = scope:scheme_kill
			}
			animation = grief
		}
		triggered_animation = {
			trigger = {
				exists = scope:scheme_successful
				exists = scope:scheme_maim
			}
			animation = rage
		}
		triggered_animation = {
			trigger = { exists = scope:scheme_successful }
			animation = disbelief
		}
		animation = anger
	}
	right_portrait = {
		character = scope:captured
		animation = prisonhouse
		override_imprisonment_visuals = yes
	}
	lower_left_portrait = scope:flavor_victim
	lower_center_portrait = {
		character = scope:owner
		trigger = { exists = scope:scheme_discovered }
	}
	lower_right_portrait = {
		trigger = {
			scope:owner.top_liege != scope:owner
			scope:owner.top_liege != scope:target
		}
		character = scope:owner.top_liege
	}
	override_background = {
		trigger = { exists = scope:scheme_successful }
		reference = burning_building
	}
	override_background = {
		trigger = {
			NOT = { exists = scope:scheme_successful }
		}
		reference = ep3_mediterranean_estate
	}

	immediate = {
		play_music_cue = "mx_cue_murder"
		show_as_tooltip = {
			if = {
				limit = { exists = scope:scheme_successful }
				raid_estate_success_public_effect = yes
			}
			else = { raid_estate_failure_effect = yes }
		}
	}

	option = {
		name = {
			text = scheme_critical_moments.8034.a.discovered
			trigger = { exists = scope:scheme_discovered }
		}
		name = {
			text = scheme_critical_moments.8034.a
			trigger = {
				NOT = { exists = scope:scheme_discovered }
			}
		}
	}
}

# Choice
scheme_critical_moments.8035 = {
	type = character_event
	title = scheme_critical_moments.8035.t
	desc = {
		desc = scheme_critical_moments.8035.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:owner = scope:owner.top_liege }
				desc = scheme_critical_moments.8035.emperor
			}			
			triggered_desc = {
				trigger = { exists = scope:scheme_permission }
				desc = scheme_critical_moments.8035.permission
			}
			desc = scheme_critical_moments.8035.crime
		}
	}
	theme = intrigue
	left_portrait = {
		character = scope:owner
		animation = menacing
	}
	lower_left_portrait = {
		character = scope:owner.top_liege
		trigger = {
			NOR = {
				scope:owner.top_liege = scope:owner
				scope:owner.top_liege = scope:target
			}
			exists = scope:scheme_discovered
		}
	}
	lower_right_portrait = scope:target
	override_background = { reference = study }

	trigger = {
		scope:scheme.scheme_target_character.house = {
			any_house_member = {
				age >= 8
				is_playable_character = no
				trigger_if = {
					limit = { exists = primary_spouse }
					primary_spouse = { is_landed = no }
				}
				top_liege = scope:scheme.scheme_target_character.top_liege
				NOT = { this = scope:scheme.scheme_target_character }
			}
		}
	}

	on_trigger_fail = {
		scope:scheme = {
			scheme_target_character = {
				save_scope_as = target
				domicile ?= { save_scope_as = target_domicile }
			}
			scheme_owner = { save_scope_as = owner }
		}
		trigger_event = scheme_critical_moments.8031
	}

	immediate = {
		scope:scheme = {
			scheme_target_character = {
				save_scope_as = target
				domicile ?= { save_scope_as = target_domicile }
			}
			scheme_owner = { save_scope_as = owner }
		}
	}

	option = {
		name = scheme_critical_moments.8035.a
		custom_tooltip = scheme_critical_moments.8035.a.tt
		save_scope_as = scheme_kill
		stress_impact = {
			forgiving = medium_stress_impact_gain
			compassionate = medium_stress_impact_gain
			just = medium_stress_impact_gain
		}
		ai_chance = {
			base = 10
			ai_value_modifier = {
				ai_compassion = -1
				ai_boldness = 1
				ai_vengefulness = 1
			}
			modifier = {
				NOT = { has_relation_rival = scope:owner }
				add = -1000
			}
		}
	}

	option = {
		name = scheme_critical_moments.8035.b
		custom_tooltip = scheme_critical_moments.8035.b.tt
		save_scope_as = scheme_maim
		stress_impact = {
			forgiving = medium_stress_impact_gain
			compassionate = medium_stress_impact_gain
			just = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100000
			ai_value_modifier = {
				ai_compassion = -0.5
				ai_boldness = 0.5
				ai_vengefulness = 0.5
			}
			modifier = {
				NOT = { has_relation_rival = scope:owner }
				factor = 0.5
			}
		}
	}

	option = {
		name = scheme_critical_moments.8035.c
		custom_tooltip = scheme_critical_moments.8035.c.tt
		custom_tooltip = scheme_critical_moments.8035.c.warning
		save_scope_as = scheme_imprison
		ai_chance = { base = 0 }
	}

	option = {
		name = scheme_critical_moments.8035.d
		ai_chance = {
			base = 10
			ai_value_modifier = {
				ai_compassion = 0.5
				ai_boldness = -0.5
				ai_vengefulness = -0.5
			}
		}
	}

	after = {
		custom_tooltip = scheme_critical_moments.8035.tt
		trigger_event = scheme_critical_moments.8031
	}
}

##################################################
# Teach Governor
# Joe Parkin
# 8041 - 8043
##################################################

scripted_effect teach_governor_track_effect = {
	if = {
		limit = { has_trait_xp = { trait = governor value >= 100 } }
		save_scope_value_as = { name = $NAME$_track value = 4 }
	}
	else_if = {
		limit = { has_trait_xp = { trait = governor value >= 75 } }
		save_scope_value_as = { name = $NAME$_track value = 3 }
	}
	else_if = {
		limit = { has_trait_xp = { trait = governor value >= 50 } }
		save_scope_value_as = { name = $NAME$_track value = 2 }
	}
	else_if = {
		limit = { has_trait_xp = { trait = governor value >= 25 } }
		save_scope_value_as = { name = $NAME$_track value = 1 }
	}
	else_if = { # Emperors get a number assignment even if they don't have the governor trait
		limit = {
			NOT = { has_trait = governor }
			primary_title.tier = tier_empire
		}
		save_scope_value_as = { name = $NAME$_track value = 2 }
	}
	else_if = { # Kings get a number assignment even if they don't have the governor trait
		limit = {
			NOT = { has_trait = governor }
			primary_title.tier = tier_kingdom
		}
		save_scope_value_as = { name = $NAME$_track value = 1 }
	}
	else = {
		save_scope_value_as = { name = $NAME$_track value = 0 }
	}
}

scripted_effect teach_governor_relative_effect = {
	scope:owner = {
		teach_governor_track_effect = { NAME = owner }
	}
	scope:target = {
		teach_governor_track_effect = { NAME = target }
	}
	save_scope_value_as = {
		name = diff_track
		value = {
			value = scope:owner_track
			subtract = scope:target_track
		}
	}
}

#	Results event for the Teach Governor scheme.
scheme_critical_moments.8041 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = { save_scope_as = owner }
			teach_governor_relative_effect = yes
			scheme_owner = {
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = {
						id = scheme_critical_moments.8042
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.8043
						days = 1
					}	
				}
			}
		}
	}
}

scripted_effect teach_governor_success_effect = {
	scope:owner = {
		change_influence = medium_influence_gain
		if = {
			limit = { NOT = { has_trait = governor } }
			add_trait = governor
		}
		if = {
			limit = { scope:owner_track < 4 }
			add_trait_xp = {
				trait = governor
				value = {
					value = 3
					if = { # Target has nothing to teach you
						limit = { scope:diff_track > 0 } # Higher Governor tier than target
						divide = scope:diff_track
					}
					else_if = { # Target has much to teach you
						limit = { scope:diff_track < 0 } # Lower Governor tier than target
						add = {
							value = 3
							multiply = scope:diff_track
							abs = yes
						}
					}
					multiply = 3
					ceiling = yes
				}
			}
		}
		if = {
			limit = { scope:target.house != house }
			add_hook = {
				type = favor_hook
				target = scope:target
				years = 5
			}
		}
		random_list = {
			50 = {
				progress_towards_friend_effect = {
					REASON = friend_mentored_in_governance
					CHARACTER = scope:target
					OPINION = default_friend_opinion
				}
			}
			50 = {
				reverse_add_opinion = {
					modifier = teach_governor_opinion
					target = scope:target
				}
			}
		}
	}
	scope:target = {
		if = {
			limit = { NOT = { has_trait = governor } }
			add_trait = governor
		}
		if = {
			limit = { scope:target_track < 4 }
			add_trait_xp = {
				trait = governor
				value = {
					value = 5
					if = { # Owner has much to teach you
						limit = { scope:diff_track > 0 }
						add = {
							value = 5
							multiply = scope:diff_track
						}
					}
					else_if = { # Target has nothing to teach you
						limit = { scope:diff_track < 0 } # Higher Governor tier than owner
						divide = {
							value = scope:diff_track
							abs = yes
						}
					}
					multiply = 4
					ceiling = yes
				}
			}
			add_character_modifier = {
				modifier = ep3_recently_mentored_governor
				years = 10
			}
		}
	}
}

# Success
scheme_critical_moments.8042 = {
	type = character_event
	title = scheme_critical_moments.8042.t
	desc = scheme_critical_moments.8042.desc
	theme = administrative
	left_portrait = {
		character = scope:owner
		animation = admiration
	}
	right_portrait = {
		character = scope:target
		animation = writing
	}
	override_background = { reference = study }

	immediate = {
		# Trigger the response event for the target
		scope:target = { trigger_event = scheme_critical_moments.8044 }
		teach_governor_success_effect = yes
	}

	option = {
		name = scheme_critical_moments.8042.a
		scope:scheme = { end_scheme = yes }
	}
}

# Failure
scheme_critical_moments.8043 = {
	type = character_event
	title = scheme_critical_moments.8043.t
	desc = scheme_critical_moments.8043.desc
	theme = administrative
	left_portrait = {
		character = scope:owner
		animation = disappointed
	}
	right_portrait = {
		character = scope:target
		animation = dismissal
	}
	override_background = { reference = study }

	immediate = {
		scope:target = {
			if = {
				limit = {
					is_alive = yes
					is_ai = yes
				}
				hidden_effect = {
					remove_opinion = {
						target = scope:owner
						modifier = mentoring_governor_opinion
					}
				}
				add_opinion = {
					target = scope:owner
					modifier = attempted_teach_governor_opinion
				}
			}
			else = { trigger_event = scheme_critical_moments.8044 }
		}
		stress_impact = {
			base = minor_stress_impact_gain
			diligent = minor_stress_impact_gain
			stubborn = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8043.a
		scope:scheme = { end_scheme = yes }
	}
}

# Target reaction
scheme_critical_moments.8044 = {
	type = character_event
	title = scheme_critical_moments.8044.t
	desc = {
		desc = scheme_critical_moments.8044.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:scheme_successful }
				desc = scheme_critical_moments.8044.success
			}
			desc = scheme_critical_moments.8044.failure
		}
	}
	theme = administrative
	left_portrait = {
		character = scope:target
		animation = writing
	}
	right_portrait = {
		character = scope:owner
		animation = admiration
	}
	lower_right_portrait = scope:owner.top_liege
	override_background = { reference = study }

	immediate = {
		scope:owner = { save_scope_as = background_throne_room_scope }
		if = {
			limit = { exists = scope:scheme_successful }
			show_as_tooltip = { teach_governor_success_effect = yes }
		}
	}

	option = {
		name = {
			text = scheme_critical_moments.8044.a.success
			trigger = { exists = scope:scheme_successful }
		}
		name = {
			text = scheme_critical_moments.8044.a
			trigger = {	
				NOT = { exists = scope:scheme_successful }
			}
		}
	}
}

# Accept response
scheme_critical_moments.8045 = {
	type = letter_event
	opening = scheme_critical_moments.8045.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:recipient = { is_governor = yes }
				}
				desc = scheme_critical_moments.8045.desc
			}
			desc = scheme_critical_moments.8045.fallback
		}
		desc = scheme_critical_moments.8045.rump
	}
	sender = scope:recipient

	immediate = {
		show_as_tooltip = {
			stress_impact = {
				arrogant = minor_stress_impact_gain
				lazy = medium_stress_impact_gain
			}
			start_scheme = {
				target_character = scope:recipient
				type = teach_governor
			}
		}
	}

	option = {
		name = scheme_critical_moments.8045.a
		scope:recipient = {
			if = {
				limit = { is_ai = yes }
				add_opinion = {
					target = scope:actor
					modifier = mentoring_governor_opinion
				}
			}
		}
	}
}

# Reject response
scheme_critical_moments.8046 = {
	type = letter_event
	opening = scheme_critical_moments.8046.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:recipient = { is_governor = yes }
				}
				desc = scheme_critical_moments.8046.desc
			}
			desc = scheme_critical_moments.8046.fallback
		}
		desc = scheme_critical_moments.8046.rump
	}
	sender = scope:recipient

	option = {
		name = scheme_critical_moments.8046.a
	}
}

##################################################
# Ingratiate Family
# Joe Parkin
# 8051 - 8054
##################################################

#	Results event for the Ingratiate Family scheme.
scheme_critical_moments.8051 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = {
						id = scheme_critical_moments.8052
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.8053
						days = 1
					}	
				}
			}
		}
	}
}

scripted_effect ingratiate_family_success_effect = {
	scope:owner = {
		house = {
			add_house_modifier = {
				modifier = ep3_ingratiated_family_modifier
				years = 10
			}
		}
		progress_towards_friend_effect = {
			REASON = friend_ingratiate_family
			CHARACTER = scope:target
			OPINION = 0
		}
	}
	scope:target = {
		change_influence = medium_influence_gain
		if = {
			limit = { is_ai = yes }
			add_opinion = {
				target = scope:owner
				modifier = ingratiate_family_opinion
			}
		}
	}
}

#Success
scheme_critical_moments.8052 = {
	type = character_event
	title = scheme_critical_moments.8052.t
	desc = scheme_critical_moments.8052.desc
	theme = diplomacy
	left_portrait = {
		character = scope:owner
		animation = war_over_tie
	}
	right_portrait = {
		character = scope:target
		animation = admiration
	}

	immediate = {
		# Trigger the response event for the target
		scope:target = { trigger_event = scheme_critical_moments.8054 }
		ingratiate_family_success_effect = yes
	}

	option = {
		name = scheme_critical_moments.8052.a
		scope:scheme = { end_scheme = yes }
	}
}

#Failure
scheme_critical_moments.8053 = {
	type = character_event
	title = scheme_critical_moments.8053.t
	desc = scheme_critical_moments.8053.desc
	theme = diplomacy
	left_portrait = {
		character = scope:owner
		animation = disbelief
	}
	right_portrait = {
		character = scope:target
		animation = dismissal
	}

	immediate = {
		scope:target = {
			show_as_tooltip = {
				change_influence = medium_influence_gain
				if = {
					limit = { is_ai = yes }
					add_opinion = {
						target = scope:owner
						modifier = attempted_ingratiate_family_opinion
					}
				}
			}
			
			# Trigger the response event for the target
			trigger_event = scheme_critical_moments.8054
		}
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			impatient = minor_stress_impact_gain
		}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8053.a
		scope:scheme = { end_scheme = yes }
	}
}

# Target reaction
scheme_critical_moments.8054 = {
	type = character_event
	title = scheme_critical_moments.8054.t
	desc = {
		desc = scheme_critical_moments.8054.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:scheme_successful }
				desc = scheme_critical_moments.8054.success
			}
			desc = scheme_critical_moments.8054.failure
		}
	}
	theme = diplomacy
	left_portrait = {
		character = scope:target
		triggered_animation = {
			trigger = { exists = scope:scheme_successful }
			animation = happiness
		}
		animation = dismissal
	}
	right_portrait = {
		character = scope:owner
		animation = war_over_tie
	}

	immediate = {
		if = {
			limit = { is_ai = yes }
			hidden_effect = {
				remove_opinion = {
					modifier = ingratiating_family_opinion
					target = scope:owner
				}
			}
		}
		if = {
			limit = { exists = scope:scheme_successful }
			show_as_tooltip = { ingratiate_family_success_effect = yes }
			if = {
				limit = { is_ai = yes }
				add_opinion = {
					target = scope:owner
					modifier = ingratiate_family_opinion
				}
			}
		}
		else = {
			change_influence = medium_influence_gain
			if = {
				limit = { is_ai = yes }
				add_opinion = {
					target = scope:owner
					modifier = attempted_ingratiate_family_opinion
				}
			}
		}
	}

	option = {
		name = {
			trigger = { exists = scope:scheme_successful }
			text = scheme_critical_moments.8054.a.success
		}
		name = {
			trigger = {
				NOT = { exists = scope:scheme_successful }
			}
			text = scheme_critical_moments.8054.a
		}
	}
}

##################################################
# Prepare Greek Fire Dromons
# Jason Cantalini
# 8060 - 8062
##################################################

#	Results event for the Prepare Greek Fire Dromons scheme.
scheme_critical_moments.8060 = {
	hidden = yes

	immediate = {
		scope:scheme = {
			scheme_owner = {
				save_scope_as = owner
				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance
					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}
				if = {
					limit = { exists = scope:scheme_successful }
					trigger_event = {
						id = scheme_critical_moments.8061
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.8062
						days = 1
					}	
				}
			}
		}
	}
}

#EK DISABLED
#scripted_effect prepare_fire_dromons_success_effect = {
#	scope:byzantion = {
#		add_county_modifier = {
#			modifier = defensive_fire_dromons_modifier
#			desc = defensive_fire_dromons_modifier_custom_desc
#			years = 30
#		}
#	}
#}

# Success
scheme_critical_moments.8061 = {
	type = character_event
	title = scheme_critical_moments.8061.t
	desc = scheme_critical_moments.8061.desc
	theme = byzantine
	override_background = {
		reference = docks
	}
	widget = {
		gui = "event_window_widget_vfx_heavy_smoke"
		container = "foreground_shader_vfx_container"
	}
	left_portrait = {
		character = root
		animation = eccentric
	}

	#EK DISABLED
	#immediate = {
	#	title:c_byzantion = {
	#		save_scope_as = byzantion
	#	}
	#	prepare_fire_dromons_success_effect = yes
	#}

	option = {
		name = scheme_critical_moments.8061.a
		scope:scheme = { end_scheme = yes }
	}
}

# Failure
scheme_critical_moments.8062 = {
	type = character_event
	title = scheme_critical_moments.8062.t
	desc = scheme_critical_moments.8062.desc
	theme = administrative
	override_background = {
		reference = docks
	}
	widget = {
		gui = "event_window_widget_vfx_heavy_smoke"
		container = "foreground_shader_vfx_container"
	}
	left_portrait = {
		character = root
		animation = fear
		camera = camera_event_left_forward
	}

	immediate = {
		stress_impact = {
			base = minor_stress_impact_gain
			ambitious = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
		#EK DISABLED
		#title:c_byzantion = {
		#	save_scope_as = byzantion
		#}
	}

	#Option A: Too bad
	option = {
		name = scheme_critical_moments.8062.a
		scope:scheme = { end_scheme = yes }
	}
}
