﻿deviant_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
				deviant_send_courtesans = {
					sendcourtesans_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
			}
		}
	}
}

sendcourtesans_scheme_prep_effect = {
	save_scope_as = scheme
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = courtesango_deviant_events.5000 }
	}
}

ambitiouswomaninn_scheme_prep_effect = {
	save_scope_as = scheme
	if = {
		limit = {
			NOT = { exists = scope:suppress_next_event }
		}
		scheme_owner = { trigger_event = deviant_contract_events.1020 }
	}
}

sendcourtesans_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
		}
	}
	save_scope_value_as = {
		name = discovery_chance
		value = monthly_scheme_discovery_chance_value
	}
	random = {
		chance = scope:discovery_chance
		# Either increment breaches.
		if = {
            limit = { 
                scope:scheme = {
                    scheme_breaches >= scheme_breaches_about_to_hit_limit_value 
                }
            }
			scheme_owner = {
           		trigger_event = courtesango_deviant_events.5009
			}
        }
		# 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" }
		}
	}
}	

ambitiouswoman_scheme_monthly_issue_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
		}
	}
	save_scope_value_as = {
		name = discovery_chance
		value = monthly_scheme_discovery_chance_value
	}
	random = {
		chance = scope:discovery_chance
		# Either increment breaches.
		if = {
            limit = { 
                scope:scheme = {
                    scheme_breaches >= scheme_breaches_about_to_hit_limit_value 
                }
            }
			scheme_owner = {
           		trigger_event = deviant_contract_events.1300
			}
        }
	}
}

populate_location_with_courtesan_contracts_effect = {
	if = {
		limit = {
			# The AI have their own system for getting contracts.
			trigger_if = {
				limit = { is_landless_adventurer = yes }
				is_ai = no
			}
			lock_contracts_from_spawning_in_sahara_trigger = yes
		}
		# Flag our pre-existing contracts if necessary.
		if = {
			limit = { exists = scope:toggle_distance_ignore }
			every_character_task_contract = { add_to_list = pre_existing_contracts_list }
		}
		populate_task_contracts_for_area = {
			location = $AREA_CHAR$.capital_province
			amount = $AMOUNT$
			group = { 
				laamp_contracts_deviant_courtesan_group 
			}
		}
		# Then flag our new contracts (again, if necessary).
		if = {
			limit = { exists = scope:toggle_distance_ignore }
			every_character_task_contract = {
				limit = {
					NOT = { is_in_list = pre_existing_contracts_list }
				}
				set_variable = {
					name = retain_despite_distance
					value = yes
				}
			}
		}
	}
}