﻿spy_network_limit = {
	# Every 5 points of Intrigue increases cap by 1
	value = 0
	add = intrigue
	if = {
		limit = {
			exists = cp:councillor_spymaster
		}
		add = cp:councillor_spymaster.intrigue
	}
	if = {
		limit = {
			house ?= { has_house_modifier = sas_expand_spy_network_house_modifier }
		}
		multiply = 2
	}
	divide = 5

	floor = yes
	min = 0
}

spy_network_over_limit_recruitment = {
	value = spy_network_members_count
	subtract = spy_network_limit

	floor = yes
	min = 0
}

spy_network_over_limit_recruitment_penalty = {
	value = spy_network_over_limit_recruitment
	multiply = 25

	floor = yes
	min = 0
}

spy_network_over_limit = {
	value = spy_network_members_count_gui
	subtract = spy_network_limit

	floor = yes
	min = 0
}

spy_network_over_limit_penalty = {
	value = spy_network_over_limit
	multiply = 25
	
	floor = yes
	min = 0
}

spy_network_members_count = {
	value = 0
	add = 1		# to count the spy to be recruited
	#scope:actor = {
		# Add +1 for every entity in the list.
		every_in_list = {
			variable = spy_network_members
			add = 1
		}
	#}
}

spy_network_members_count_gui = {
	value = 0
	every_in_list = {
		variable = spy_network_members
		add = 1
	}
}

surveil_target_success_chance = {
	value = 0
	# Add your total spy count
	add = {
		value =	spy_network_members_count_gui
	}
	# Add 0.5x your intrigue
	add = {
		value = intrigue
		multiply = 0.5
	}
	# Subtract 0.5x their intrigue
	subtract = {
		value = var:surveil_target.intrigue
		multiply = 0.5
	}
	var:surveil_target = {
		# Add your spymaster's intrigue if finding secrets
		if = {
			limit = {
				exists = root.cp:councillor_spymaster
				root.cp:councillor_spymaster = {
					is_performing_council_task = task_find_secrets
					councillor_task_target = prev
				}
			}
			add = root.cp:councillor_spymaster.intrigue
		}
		# Subtract their spymaster's intrigue if disrupting schemes
		if = {
			limit = {
				exists = cp:councillor_spymaster
				cp:councillor_spymaster = {
					is_performing_council_task = task_disrupt_schemes
				}
				# Don't subtract if they're on your side
				NOT = {
					root = {
						any_in_list = {
							variable = spy_network_members
							this = root.var:surveil_target.cp:councillor_spymaster
						}
					}
				}
			}
			subtract = cp:councillor_spymaster.intrigue
		}
		# Subtract their local spy count total
		every_in_list = {
			variable = spy_network_members
			if = {
				limit = {
					is_alive = yes
					court_owner = root.var:surveil_target
				}
				subtract = 1
			}
		}
		# Add if they're trusting
		if = {
			limit = {
				has_trait = trusting
			}
			add = 10
		}
		# Subtract if they're paranoid
		if = {
			limit = {
				has_trait = paranoid
			}
			subtract = 10
		}
	}
	# Add intrigue of every spy you have in their court
	every_in_list = {
		variable = spy_network_members
		if = {
			limit = {
				is_alive = yes
				OR = {
					court_owner = root.var:surveil_target
					court_owner = root.var:surveil_target.court_owner
				}
			}
			add = intrigue
		}
	}
	# Chance goes to zero if we have no spies in their court
	if = {
		limit = {
			NOT = {
				any_in_list = {
					variable = spy_network_members
					is_alive = yes
					OR = {
						court_owner = root.var:surveil_target
						court_owner = root.var:surveil_target.court_owner
					}
				}
			}
		}
		multiply = 0
	}
	# Embracing the spiders
	if = {
		limit = {
			exists = var:femme_fatale_is_embracing_the_spiders
			var:femme_fatale_is_embracing_the_spiders = yes
		}
		add = 5
	}
	# House of sabotage and subterfuge
	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
					has_house_modifier = sas_house_of_sabotage_and_subterfuge_modifier_none
				}
			}
		}
		add = 5
	}
	# Subtract based on spy count
	subtract = spy_network_over_limit_penalty

	floor = yes
	min = 0
	max = 100
}
