﻿loaa_cap_schemes_and_fire_reminders_effect = {
	if = {
		limit = {
			scheme_agent_charges >= opportunity_cap_value
			scheme_success_chance >= max_scheme_success_chance
		}
		if = {
			limit = {
				scheme_owner = {
					is_ai = no
					NOR = {
						has_character_flag = scheme_critical_moments.0011
						has_variable = opportunities_reminder
					}
				}
			}
			scheme_owner = {
				add_character_flag = currently_in_scheme_reminder
				# supress the 'variable is used but is never set' error, the flag is set through code with the cooldown effect on the event, smh!
				if = {
					limit = {
						always = no
					}
					add_character_flag = scheme_critical_moments.0011
				}
			}
			switch = {
				trigger = scheme_type
				raid_camp = {
					raid_camp_scheme_prep_effect = yes
				}
			}
		}
		# If the player hasn't been reminded once, tell them that their scheme has hit max opportunities.
		scheme_owner = {
			if = {
				limit = {
					is_ai = no
					NOT = { has_variable = opportunities_reminder }
				}
				trigger_event = scheme_critical_moments.0011
			}
		}
	}
}

raid_camp_scheme_prep_effect = {
	save_scope_as = scheme
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:loaa_raid_camp_events.8035
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = scheme_critical_moments.0002 }
	}
}

raid_camp_opinion_effect = {
	$TARGET$ = {
		add_opinion = {
			target = $OWNER$
			modifier = raided_camp_crime
		}
		progress_towards_rival_effect = {
			REASON = rival_raid_camp
			CHARACTER = $OWNER$
			OPINION = 0
		}
	}
	$OWNER$ = {
		if = {
			limit = { is_landless_adventurer = yes }
			laamp_rewards_apply_criminal_xp_effect = {
				TRACK = bandit
				XP_MAX = gallowsbait_xp_major_gain
				XP_MIN = gallowsbait_xp_major_gain
			}
		}
	}
	every_in_list = {
		list = target_victims
		if = {
			limit = { exists = scope:scheme_maim }
			add_opinion = {
				target = $OWNER$
				modifier = raid_camp_maimed_opinion
			}
		}
	}
}

loaa_laamp_base_0011_contract_scheme_prep_effect = {
	save_scope_as = scheme
	scope:scheme.task_contract ?= { save_scope_as = task_contract }
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:scheme_critical_moments.2011
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = scheme_critical_moments.0002 }
	}
}

loaa_laamp_base_3011_contract_scheme_prep_effect = {
	save_scope_as = scheme
	scope:scheme.task_contract ?= { save_scope_as = task_contract }
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:loaa_scheme_critical_moments.1
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = scheme_critical_moments.0002 }
	}
}

loaa_laamp_base_3021_contract_scheme_prep_effect = {
	save_scope_as = scheme
	scope:scheme.task_contract ?= { save_scope_as = task_contract }
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:scheme_critical_moments.2621
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = scheme_critical_moments.0002 }
	}
}

loaa_laamp_base_5031_contract_scheme_prep_effect = {
	save_scope_as = scheme
	scope:scheme.task_contract ?= { save_scope_as = task_contract }
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:scheme_critical_moments.3031
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = scheme_critical_moments.0002 }
	}
}

loaa_sabotage_contract_scheme_prep_effect = {
	save_scope_as = scheme
	scope:scheme.task_contract ?= { save_scope_as = task_contract }
	save_scope_value_as = {
		name = follow_up_event
		value = event_id:loaa_contract_event.0064
	}
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = scheme_critical_moments.0002 }
	}
}

loaa_hostile_scheme_monthly_discovery_chance_effect = {
	# If we're in the first X months of a scheme, give a grace period.
	if = {
		limit = {
			has_variable = secrecy_grace_period
			var:secrecy_grace_period >= 1
		}
		change_variable = {
			name = secrecy_grace_period
			add = -1
		}
	}
	# Otherwise we want to start checking for discovery.
	else_if = {
		# Without a spymaster, you can't discover schemes.
		## This isn't tooltipped for the person doing the scheming: communicating it (and when it turns on, and when it turns off) would be erratic and spammy, so we just let them think they're getting nice'n'lucky.
		limit = {
			scope:scheme = {
				# Character
				trigger_if = {
					limit = { exists = scheme_target_character.host }
					scheme_target_character.host = { save_temporary_scope_as = breach_host_temp }
				}
				# Title
				trigger_else_if = {
					limit = { exists = scheme_target_title.holder }
					scheme_target_title.holder = { save_temporary_scope_as = breach_host_temp }
				}
				# Fallback
				trigger_else = { always = no }
				scope:breach_host_temp ?= {
					# Second for Adventurers
					trigger_if = {
						limit = { is_landless_adventurer = yes }
						court_position:second_camp_officer ?= { save_temporary_scope_as = breach_maker_temp }
					}
					# Spymaster for everyone else
					trigger_else = {
						cp:councillor_spymaster ?= { save_temporary_scope_as = breach_maker_temp }
					}
				}
			}
		}
		# Sort scopes.
		## Vs. titles.
		if = {
			limit = { exists = scope:target.holder }
			scope:target.holder = { save_scope_as = target_character }
		}
		## Vs. characters.
		else = {
			scope:target = { save_scope_as = target_character }
		}
		## Nab the spymaster too.
		scope:breach_maker_temp = { save_scope_as = spymaster }
		# Alright, now we roll for discovery.
		# Save chance for simple debugging
		save_scope_value_as = {
			name = discovery_chance
			value = monthly_scheme_discovery_chance_value
		}
		## ... if scope:target lives at scope:owner's court.
		if = {
			limit = {
				scope:breach_host_temp = scope:owner
				NOT = { exists = scope:scheme.var:had_self_court_discovery }
			}
			random = {
				chance = scope:discovery_chance
				# Either increment breaches.
				if = {
					limit = { scheme_breaches >= scheme_breaches_about_to_hit_limit_value }
					increment_breaches_with_message_effect = yes
				}
				# Or expose us generally.
				else = {
					add_scheme_breach = 1
					scheme_owner = { play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Scheme_Rework/ep3_mx_sting_schemerework_scheme_suffering_breaches" }
					set_variable = {
						name = had_self_court_discovery
						value = yes
						years = 3
					}
					scope:owner = {
						if = {
							limit = {
								scope:scheme = { loaa_is_savable_murder_scheme_trigger = yes }
							}
							trigger_event = hostile_scheme_discovery.4002
						}
						else = { trigger_event = hostile_scheme_discovery.4001 }
					}
					# Flag that we're discovering something so that on-going events don't fire.
					save_scope_value_as = {
						name = discovery_event_happening
						value = yes
					}
				}
			}
		}
		## ... otherwise they're at different courts.
		else_if = {
			limit = {
				NOR = {
					# Spymaster won't expose their own Scheme
					scope:owner = scope:spymaster
					# Spymaster won't expose *your* (liege's) Scheme
					scope:owner = scope:spymaster.liege
					# Spymaster won't expose a scheme if they are an agent of it
					## Secrecy is still relevant, you can still be discovered, just not on the monthly tick.
					scheme_is_character_agent = scope:spymaster
				}
			}
			# Roll to see whether we discover something.
			random = {
				chance = scope:discovery_chance
				# If the scheme isn't exposed, then we do that first.
				if = {
					limit = { is_scheme_exposed = no }
					# Increment our breaches.
					increment_breaches_with_message_effect = yes
					# Fire off an actual warning event.
					scope:target_character.host ?= { trigger_event = hostile_scheme_discovery.2001 }
					# Flag that we're discovering something so that on-going events don't fire.
					save_scope_value_as = {
						name = discovery_event_happening
						value = yes
					}
				}
				# Else, that means we've hit the limit for the scheme, so we discover and destroy it.
				else_if = {
					limit = { scheme_breaches >= scheme_breaches_about_to_hit_limit_value }
					# Don't forget to increment breaches to max.
					add_scheme_breach = 1
					scheme_owner = { play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Scheme_Rework/ep3_mx_sting_schemerework_scheme_suffering_breaches" }
					# Fire off the correct event.
					scope:target_character.host ?= { trigger_event = hostile_scheme_discovery.1002 }
					# Flag that we're discovering something so that on-going events don't fire.
					save_scope_value_as = {
						name = discovery_event_happening
						value = yes
					}
				}
				# Otherwise, we want to discover an agent.
				else = {
					# Increment our breaches.
					increment_breaches_with_message_effect = yes
					# And reveal an agent, if we can.
					if = {
						limit = {
							any_scheme_agent_character = {
								is_alive = yes
								agent_valid_to_be_discovered_by_spymaster = yes
							}
						}
						ordered_scheme_agent_character = {
							limit = {
								is_alive = yes
								agent_valid_to_be_discovered_by_spymaster = yes
							}
							# Get the least stealthy agent.
							order_by = {
								value = 100
								subtract = intrigue
							}
							save_scope_as = agent
						}
						# If we have a candidate, reveal them.
						scope:target_character.host ?= { trigger_event = hostile_scheme_discovery.1001 }
						# Flag that we're discovering something so that on-going events don't fire.
						save_scope_value_as = {
							name = discovery_event_happening
							value = yes
						}
					}
				}
			}
		}
	}
}

# Sort the ending for a scheme.
loaa_generic_scheme_process_ending_effect = {
	# Grab our scopes.
	save_scope_as = scheme
	scheme_owner = { save_scope_as = owner }
	## Targets.
	scheme_target_character ?= { save_scope_as = target }
	scheme_target_culture ?= { save_scope_as = target }
	scheme_target_faith ?= { save_scope_as = target }
	scheme_target_title ?= { save_scope_as = target }
	# Success roll.
	random = {
		chance = scheme_success_chance
		custom_tooltip = $RESULTS_TOOLTIP$
		save_scope_value_as = {
			name = scheme_successful
			value = yes
		}
		# Interception saves.
		## Unique saves roll.
		### E.g., your dog flings itself in front of an assassin for you.
		if = {
			limit = { loaa_is_savable_murder_scheme_trigger = yes }
			# We use a scripted effect so that mods can overwrite it without overwriting the whole block.
			scheme_unique_interception_roll_effect = yes
		}
		## Repeatable saves roll.
		### E.g., your bodyguard flings itself in front of an assassin for you.
		if = {
			limit = { loaa_is_savable_murder_scheme_trigger = yes }
			random = {
				chance = scope:target.scheme_intercept_repeatable_chance_total_value
				save_scope_value_as = {
					name = interception_repeatable
					value = yes
				}
			}
		}
	}
	# Secrecy roll.
	## We check this after an interception so that, if a successful scheme is intercepted, we can boost its success a little to compensate.
	if = {
		limit = { is_type_secret = yes }
		## First, determine our secrecy value.
		save_scope_value_as = {
			name = discovery_chance
			value = {
				value = 100
				subtract = scheme_secrecy
				# Boost this up a bit if the scheme was intercepted.
				if = {
					limit = {
						OR = {
							exists = scope:interception_unique
							exists = scope:interception_repeatable
						}
					}
					add = -30
				}
				# And make sure there's always _some_ risk.
				min = 5
			}
		}
		random = {
			chance = scope:discovery_chance
			custom_tooltip = scheme_remain_secret_roll_tt
			save_scope_value_as = {
				name = scheme_discovered
				value = yes
			}
			# Grab an exposer.
			if = {
				limit = { exists = scope:target.host }
				scope:target.host = { save_scope_as = murder_exposer }
			}
			else = {
				scope:target = { save_scope_as = murder_exposer }
			}
			# Set up assorted cosmetics for murders.
			scope:owner = {
				# Portraits.
				save_scope_as = owner_to_reveal
				# Plus a flavour scope we use.
				random_memory = {
					limit = {
						has_memory_category = negative
						has_memory_participant = scope:target
						NOR = { # These read poorly
							has_memory_type = became_rivals
							has_memory_type = became_nemesis
						}
					}
					save_scope_as = bad_memory
				}
			}
		}
	}
	# Process ending on_actions.
	## Scheme intercepted by a unique flag, stopping a killing at the last moment.
	if = {
		limit = { exists = scope:interception_unique }
		scheme_owner = {
			trigger_event = { on_action = pick_unique_murder_intercept_on_action }
		}
	}
	## Scheme intercepted by a repeatable flag, stopping a killing at the last moment.
	else_if = {
		limit = { exists = scope:interception_repeatable }
		scheme_owner = {
			trigger_event = { on_action = pick_repeatable_murder_intercept_on_action }
		}
	}
	## Scheme successful.
	else_if = {
		limit = { exists = scope:scheme_successful }
		# If we have a contract conclusion event to fire, fire it.
		if = {
			limit = { exists = scope:contract_end_success }
			scheme_owner = {
				trigger_event = {
					saved_event_id = scope:contract_end_success
					delayed = yes
				}
			}
		}
		task_contract ?= { complete_task_contract = success_standard }
		trigger_event = { $SUCCESS_EVENT_TYPE$ = $SUCCESS_EVENT_ID$ }
		scheme_owner = { play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Scheme_Rework/ep3_mx_sting_schemerework_positive" }
	}
	## Scheme failed.
	else = {
		# If we have a contract conclusion event to fire, fire it.
		if = {
			limit = { exists = scope:contract_end_failure }
			scheme_owner = {
				trigger_event = {
					saved_event_id = scope:contract_end_failure
					delayed = yes
				}
			}
		}
		task_contract ?= { complete_task_contract = failure_standard }
		trigger_event = { $FAILURE_EVENT_TYPE$ = $FAILURE_EVENT_ID$ }
		scheme_owner = { play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Scheme_Rework/ep3_mx_sting_schemerework_negative" }
	}
}

# Sort the ending for a scheme.
loaa_simple_generic_scheme_process_ending_effect = {
	# Grab our scopes.
	save_scope_as = scheme
	scheme_owner = { save_scope_as = owner }
	## Targets.
	scheme_target_character ?= { save_scope_as = target }
	scheme_target_culture ?= { save_scope_as = target }
	scheme_target_faith ?= { save_scope_as = target }
	scheme_target_title ?= { save_scope_as = target }
	# Success roll.
	random = {
		chance = scheme_success_chance
		save_scope_value_as = {
			name = scheme_successful
			value = yes
		}
	}
	# Secrecy roll.
	## We check this after an interception so that, if a successful scheme is intercepted, we can boost its success a little to compensate.
	if = {
		limit = { is_type_secret = yes }
		## First, determine our secrecy value.
		save_scope_value_as = {
			name = discovery_chance
			value = {
				value = 100
				subtract = scheme_secrecy
				# And make sure there's always _some_ risk.
				min = 5
			}
		}
		random = {
			chance = scope:discovery_chance
			custom_tooltip = scheme_remain_secret_roll_tt
			save_scope_value_as = {
				name = scheme_discovered
				value = yes
			}
			# Set up assorted cosmetics for murders.
			scope:owner = {
				# Portraits.
				save_scope_as = owner_to_reveal
			}
		}
	}
	## Scheme successful.
	if = {
		limit = { exists = scope:scheme_successful }
		task_contract ?= { complete_task_contract = success_standard }
		scheme_owner = { trigger_event = { $SUCCESS_EVENT_TYPE$ = $SUCCESS_EVENT_ID$ } }
		scheme_owner = { play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Scheme_Rework/ep3_mx_sting_schemerework_positive" }
	}
	## Scheme failed.
	else = {
		task_contract ?= { complete_task_contract = failure_standard }
		scheme_owner = { trigger_event = { $FAILURE_EVENT_TYPE$ = $FAILURE_EVENT_ID$ } }
		scheme_owner = { play_sound_effect = "event:/DLC/EP3/SFX/Stingers/Scheme_Rework/ep3_mx_sting_schemerework_negative" }
	}
}