﻿# Stop schemes from overflowing with opportunities forever + tell the player when they should do something.
sas_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 = sas_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 = sas_scheme_critical_moments.0011
				}
			}
			switch = {
				trigger = scheme_type
				murder = {
					murder_scheme_prep_effect = yes
				}
				abduct = {
					abduct_scheme_prep_effect = yes
				}
				challenge_status = {
					challenge_status_scheme_prep_effect = yes
				}
				claim_throne = {
					claim_throne_scheme_prep_effect = yes
				}
				depose = {
					depose_scheme_prep_effect = yes
				}
				overthrow_regent = {
					overthrow_regent_scheme_prep_effect = yes
				}
				expand_power_base = {
					expand_power_base_scheme_prep_effect = yes
				}
				fabricate_hook = {
					fabricate_hook_scheme_prep_effect = yes
				}
				generate_claim = {
					generate_claim_scheme_prep_effect = yes
				}
				promote = {
					promote_scheme_prep_effect = yes
				}
				slander = {
					slander_scheme_prep_effect = yes
				}
				steal_back_artifact = {
					steal_back_artifact_scheme_prep_effect = yes
				}
				seize_realm = {
					seize_realm_scheme_prep_effect = yes
				}
				found_despotate = {
					found_despotate_scheme_prep_effect = yes
				}
				raid_estate = {
					raid_estate_scheme_prep_effect = yes
				}
				prepare_fire_dromons_scheme = {
					prepare_fire_dromons_scheme_prep_effect = yes
				}
				steal_herd = {
					steal_herd_scheme_prep_effect = yes
				}
				rouse_rabble = {
					rouse_rabble_scheme_prep_effect = yes
				}
				provoke_vassals = {
					provoke_vassals_scheme_prep_effect = yes
				}
				smuggle_artifact = {
					smuggle_artifact_scheme_prep_effect = yes
				}
				spread_plague = {
					spread_plague_scheme_prep_effect = yes
				}
				break_out_of_prison = {
					break_out_of_prison_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 = sas_scheme_critical_moments.0011
			}
		}
	}
}

# Add an agent type to a scheme.
## When you add a new agent type, add them to the bottom of this effect. Everything else should be taken care of automatically.
sas_select_critical_moment_agents_effect = {
	if = {
		limit = { $CHOICE$ = flag:agent_assassin }
		scope:scheme = { add_agent_slot = agent_assassin }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_assassin
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_lookout }
		scope:scheme = { add_agent_slot = agent_lookout }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_lookout
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_infiltrator }
		scope:scheme = { add_agent_slot = agent_infiltrator }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_infiltrator
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_footpad }
		scope:scheme = { add_agent_slot = agent_footpad }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_footpad
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_alibi }
		scope:scheme = { add_agent_slot = agent_alibi }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_alibi
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_thug }
		scope:scheme = { add_agent_slot = agent_thug }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_thug
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_comrade_in_arms }
		scope:scheme = { add_agent_slot = agent_comrade_in_arms }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_comrade_in_arms
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_socialite }
		scope:scheme = { add_agent_slot = agent_socialite }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_socialite
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_musician }
		scope:scheme = { add_agent_slot = agent_musician }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_musician
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_tumbler }
		scope:scheme = { add_agent_slot = agent_tumbler }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_tumbler
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_poet }
		scope:scheme = { add_agent_slot = agent_poet }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_poet
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_gabbler }
		scope:scheme = { add_agent_slot = agent_gabbler }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_gabbler
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_shill }
		scope:scheme = { add_agent_slot = agent_shill }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_shill
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_diplomat }
		scope:scheme = { add_agent_slot = agent_diplomat }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_diplomat
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_justiciar }
		scope:scheme = { add_agent_slot = agent_justiciar }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_justiciar
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_scribe }
		scope:scheme = { add_agent_slot = agent_scribe }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_scribe
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_cleric }
		scope:scheme = { add_agent_slot = agent_cleric }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_cleric
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_theologian }
		scope:scheme = { add_agent_slot = agent_theologian }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_theologian
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_herald }
		scope:scheme = { add_agent_slot = agent_herald }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_herald
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_wolf_hunter }
		scope:scheme = { add_agent_slot = agent_wolf_hunter }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_wolf_hunter
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_thief }
		scope:scheme = { add_agent_slot = agent_thief }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_thief
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_tutor }
		scope:scheme = { add_agent_slot = agent_tutor }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_tutor
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_poisoner }
		scope:scheme = { add_agent_slot = agent_poisoner }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_poisoner
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_eunuch }
		scope:scheme = { add_agent_slot = agent_eunuch }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_eunuch
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_decoy }
		scope:scheme = { add_agent_slot = agent_decoy }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_decoy
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_muscle }
		scope:scheme = { add_agent_slot = agent_muscle }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_muscle
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_drillmaster }
		scope:scheme = { add_agent_slot = agent_drillmaster }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_drillmaster
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_tracker }
		scope:scheme = { add_agent_slot = agent_tracker }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_tracker
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_physic }
		scope:scheme = { add_agent_slot = agent_physic }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_physic
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_smith }
		scope:scheme = { add_agent_slot = agent_smith }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_smith
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_ambusher }
		scope:scheme = { add_agent_slot = agent_ambusher }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_ambusher
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_bodyguard }
		scope:scheme = { add_agent_slot = agent_bodyguard }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_bodyguard
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_scout }
		scope:scheme = { add_agent_slot = agent_scout }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_scout
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_outrider }
		scope:scheme = { add_agent_slot = agent_outrider }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_outrider
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_bookkeeper }
		scope:scheme = { add_agent_slot = agent_bookkeeper }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_bookkeeper
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_bailiff }
		scope:scheme = { add_agent_slot = agent_bailiff }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_bailiff
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_draughtsman }
		scope:scheme = { add_agent_slot = agent_draughtsman }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_draughtsman
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_planner }
		scope:scheme = { add_agent_slot = agent_planner }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_planner
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_supplier }
		scope:scheme = { add_agent_slot = agent_supplier }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_supplier
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_outcast }
		scope:scheme = { add_agent_slot = agent_outcast }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_outcast
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_wrangler }
		scope:scheme = { add_agent_slot = agent_wrangler }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_wrangler
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_wrangler_speed }
		scope:scheme = { add_agent_slot = agent_wrangler_speed }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_wrangler_speed
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_theologian_success }
		scope:scheme = { add_agent_slot = agent_theologian_success }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_theologian_success
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_cleric_success }
		scope:scheme = { add_agent_slot = agent_cleric_success }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_cleric_success
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_lookout_success }
		scope:scheme = { add_agent_slot = agent_lookout_success }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_lookout_success
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_lookout_speed }
		scope:scheme = { add_agent_slot = agent_lookout_speed }
		# Error suppression.
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_lookout_speed
		}
	}
	else_if = {
		limit = { $CHOICE$ = flag:agent_rabble_rouser}
		scope:scheme = { add_agent_slot = agent_rabble_rouser }
		# Error suppression = {
		save_scope_value_as = {
			name = $CHOICE$
			value = flag:agent_rabble_rouser
		}
	}
	# And remove the variables.
	scope:scheme = {
		remove_variable = choice_a
		remove_variable = choice_b
	}
}

# Ending Effects

# What percentage chance is there that a scheme will be discovered per month?
## tl;dr = generally, 10% of 100 - secrecy rating. So if you have 60% secrecy, that's a 4% chance per month.
sas_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 = { is_savable_murder_scheme_trigger = yes }
							}
							trigger_event = hostile_scheme_discovery.4002 # Fine as vanilla
						}
						else = { trigger_event = hostile_scheme_discovery.4001 } # Fine as vanilla
					}
					# 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 = sas_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 = sas_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 } # Fine as vanilla
						# Flag that we're discovering something so that on-going events don't fire.
						save_scope_value_as = {
							name = discovery_event_happening
							value = yes
						}
					}
				}
			}
		}
	}
}

sas_count_hostile_scheme_and_add_renown = {
	# Count hostile scheme towards running total
	if = {
		limit = {
			exists = var:sas_hostile_scheme_count
		}
		change_variable = {
			name = sas_hostile_scheme_count
			add = 1
		}
	}
	else = {
		set_variable = {
			name = sas_hostile_scheme_count
			value = 1
		}
	}
	if = {
		limit = {
			house ?= {
				OR = {
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_renown_massive
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_renown_major
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_renown_medium
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_renown_minor
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_renown_tiny
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_intrigue_xp_massive
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_intrigue_xp_major
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_intrigue_xp_medium
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_intrigue_xp_minor
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_intrigue_xp_tiny
				}
			}
		}
		dynasty = {
			if = {
				limit = {
					has_game_rule = sas_scheme_renown_massive
				}
				add_dynasty_prestige = 100
			}
			else_if = {
				limit = {
					has_game_rule = sas_scheme_renown_major
				}
				add_dynasty_prestige = 75
			}
			else_if = {
				limit = {
					has_game_rule = sas_scheme_renown_medium
				}
				add_dynasty_prestige = 50
			}
			else_if = {
				limit = {
					has_game_rule = sas_scheme_renown_minor
				}
				add_dynasty_prestige = 25
			}
			else_if = {
				limit = {
					has_game_rule = sas_scheme_renown_tiny
				}
				add_dynasty_prestige = 10
			}
		}
		if = {
			limit = {
				has_game_rule = sas_scheme_intrigue_xp_massive
			}
			add_intrigue_lifestyle_xp = 100
		}
		else_if = {
			limit = {
				has_game_rule = sas_scheme_intrigue_xp_major
			}
			add_intrigue_lifestyle_xp = 75
		}
		else_if = {
			limit = {
				has_game_rule = sas_scheme_intrigue_xp_medium
			}
			add_intrigue_lifestyle_xp = 50
		}
		else_if = {
			limit = {
				has_game_rule = sas_scheme_intrigue_xp_minor
			}
			add_intrigue_lifestyle_xp = 25
		}
		else_if = {
			limit = {
				has_game_rule = sas_scheme_intrigue_xp_tiny
			}
			add_intrigue_lifestyle_xp = 10
		}
	}
}