on_actions = {

	on_startup = {
		effect = {

			#log mod active
			set_global_flag = Outer_Limits_SPY_active
			log = "Outer Limits Spy Wars active, updated for 1.17.1"

		}
	}

	# scope is operative
	# from scope is the country 
	on_operative_created = {
		effect = {
			if = { 
				limit = { is_debug = yes }
				log = "on_operative_created | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}

			# "is_operative = yes" seems not to work
			# set a flag to filter operatives
			set_character_flag = OL_SPY_this_is_a_spy
		}
	}

	# scope is operative
	# from scope is the country 
	on_operative_recruited = {
		effect = {
			if = { 
				limit = { is_debug = yes }
				log = "on_operative_recruited | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}

			# "is_operative = yes" seems not to work
			# set a flag to filter operatives
			set_character_flag = OL_SPY_this_is_a_spy

			log = "OL_SPY_recruited | [FROM.GetName] recruited spy | name: [THIS.GetName] | skill: [?THIS.skill_level]"

			### Spy recruited ###
			FROM = { 
				add_to_variable = { OL_spy_war_support = 0.1 }
				add_to_variable = { global.OL_spy_war_support = 0.1 }
				OL_effect_spy_war_support_add = yes
				add_to_variable = { OL_spy_recruited = 1 }
				add_to_variable = { global.OL_spy_recruited = 1 }
			}
			log = "OL_SPY_warsupport | [FROM.GetName] | (+0.1) | [?FROM.OL_spy_war_support] War Support from spy operations"
		}
	}
	
	on_operation_completed = {
		effect = {
			# same scope setup as in operation outcome:
			# THIS: the operation
			# ROOT: the initiating country
			# FROM: the target country

			if = { 
				limit = { is_debug = yes }
				log = "on_operation_completed 1 | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}
	
			### START EVENT Spy Operation Completed ###
			if = {
				limit = {
					has_game_rule = {
						rule = RULE_OPERATION_COMPLETED_OL
						option = yes
					}
				}

				ROOT = { save_event_target_as = key_operative_event_root_OL }
				THIS = { save_event_target_as = key_operative_event_this_OL }
				PREV = { save_event_target_as = key_operative_event_prev_OL }
				FROM = { save_event_target_as = key_operative_event_from_OL }

				if = { 
					limit = { is_debug = yes }
					log = "on_operation_completed 2 | ROOT: [key_operative_event_root_OL.GetName] | THIS: [key_operative_event_this_OL.GetName] | PREV: [key_operative_event_prev_OL.GetName] | FROM: [key_operative_event_from_OL.GetName]"
				}

				ROOT = { country_event = { id = outer_limits_operative_event.1 } }
			}
			### END EVENT Spy Operation Completed ###


			log = "OL_SPY_mission | [ROOT.GetName] has completed \"[THIS.GetName]\" in [FROM.GetName]"

			# Own spy mission complete
			ROOT = {
				add_to_variable = { OL_spy_war_support = 0.1 }
				add_to_variable = { global.OL_spy_war_support = 0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [ROOT.GetName] | (+0.1) | [?ROOT.OL_spy_war_support] War Support from spy operations"

			# Enemy spy mission complete
			FROM = {
				add_to_variable = { OL_spy_war_support = -0.1 }
				add_to_variable = { global.OL_spy_war_support = -0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [FROM.GetName] | (-0.1) | [?FROM.OL_spy_war_support] War Support from spy operations"
		}
	}

	on_operative_detected_during_operation = {
		effect = {
			# SCOPE_UNIT_LEADER
			# ROOT: the initiating country
			# FROM: the target country
			# FROM.FROM : SCOPE_STATE ( will only be set if the operation has a specific selection_target )

			if = { 
				limit = { is_debug = yes }
				log = "on_operative_detected_during_operation | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName] | FROM.FROM: [FROM.FROM.GetName]"
			}

			log = "OL_SPY_detected | [FROM.GetName] detected spy ([THIS.GetName]) from [ROOT.GetName] (in [FROM.FROM.GetName])"

			### Own operative detected -0.1% War Support ###
			ROOT = { 
				add_to_variable = { OL_spy_war_support = -0.1 }
				add_to_variable = { global.OL_spy_war_support = -0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [ROOT.GetName] | (-0.1) Own spy detected | [?ROOT.OL_spy_war_support] War Support from spy operations"

			### Enemy operative detected +0.1% War Support ###
			FROM = { 
				add_to_variable = { OL_spy_war_support = 0.1 }
				add_to_variable = { global.OL_spy_war_support = 0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [FROM.GetName] | (+0.1) Enemy spy detected | [?FROM.OL_spy_war_support] War Support from spy operations"
        }
    }

	# called when an operative performing an offensive mission in a country has been spotted
	on_operative_on_mission_spotted = {
		effect = {
            # SCOPE  the operative
            # ROOT   the country the operative was performing its mission in
            # FROM   the country the operative is operating for

			if = { 
				limit = { is_debug = yes }
				log = "on_operative_on_mission_spotted | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}

			log = "OL_SPY_spotted | [ROOT.GetName] spottet spy ([THIS.GetName]) from [FROM.GetName]"

			### Enemy operative spotted +0.1% War Support ###
			ROOT = { 
				add_to_variable = { OL_spy_war_support = 0.1 }
				add_to_variable = { global.OL_spy_war_support = 0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [ROOT.GetName] | (+0.1) Enemy spy spotted | [?ROOT.OL_spy_war_support] War Support from spy operations"

			### Own operative spotted -0.1% War Support ###
			FROM = { 
				add_to_variable = { OL_spy_war_support = -0.1 }
				add_to_variable = { global.OL_spy_war_support = -0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [FROM.GetName] | (-0.1) Own spy spotted | [?FROM.OL_spy_war_support] War Support from spy operations"
        }
    }

	on_operative_captured = {
		effect = {
			# SCOPE  the operative
			# ROOT   the country the operative was performing its mission in
			# FROM   the country the operative is operating for

			if = { 
				limit = { is_debug = yes }
				log = "on_operative_captured | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}

			log = "OL_SPY_captured | [ROOT.GetName] captured spy ([THIS.GetName]) from [FROM.GetName]"

			### Enemy operative captured +0.1% War Support ###
			ROOT = { 
				add_to_variable = { OL_spy_war_support = 0.1 }
				add_to_variable = { global.OL_spy_war_support = 0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [ROOT.GetName] | (+0.1) Enemy spy captured | [?ROOT.OL_spy_war_support] War Support from spy operations"

			### Own operative captured -0.1% War Support ###
			FROM = { 
				add_to_variable = { OL_spy_war_support = -0.1 }
				add_to_variable = { global.OL_spy_war_support = -0.1 }
				OL_effect_spy_war_support_add = yes
			}
			log = "OL_SPY_warsupport | [FROM.GetName] | (-0.1) Own spy captured | [?FROM.OL_spy_war_support] War Support from spy operations"
        }
    }

	on_operative_death = {
		effect = {
			# SCOPE  the operative
			# ROOT   the killer country (optional)
			# FROM   the country the operative is operating for

			if = { 
				limit = { is_debug = yes }
				log = "on_operative_death | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}

			### START EVENT Agent killed ###
			if = {
				# other cause of death
				# outer limits event
				limit = { NOT = { country_exists = ROOT	} }
				operative_leader_event = {
					id = outer_limits_operative_event.2
				}
			}
			else = {
				# operative killed by a country, likelly while on mission
				# base game event
			}
			### END EVENT Agent killed ###

			log = "OL_SPY_killed |: [ROOT.GetName] killed spy ([THIS.GetName]) from [FROM.GetName]"

			### Enemy operative killed +0.2% War Support ###
			if = {
				limit = { country_exists = ROOT	}
				ROOT = { 
					add_to_variable = { OL_spy_war_support = 0.2 }
					add_to_variable = { global.OL_spy_war_support = 0.2 }
					OL_effect_spy_war_support_add = yes
					add_to_variable = { OL_spy_killed = 1 }
					add_to_variable = { global.OL_spy_killed = 1 }
					}
				log = "OL_SPY_warsupport | [ROOT.GetName] | (+0.2) Enemy spy killed | [?ROOT.OL_spy_war_support] War Support from spy operations"
			}

			### Own operative killed -0.2% War Support ###
			FROM = { 
				add_to_variable = { OL_spy_war_support = -0.2 }
				add_to_variable = { global.OL_spy_war_support = -0.2 }
				OL_effect_spy_war_support_add = yes
				add_to_variable = { OL_spy_died = 1 }
				add_to_variable = { global.OL_spy_died = 1 }
			}
			log = "OL_SPY_warsupport | [FROM.GetName] | (-0.2) Own spy killed | [?FROM.OL_spy_war_support] War Support from spy operations"
        }
    }

	#FROM - Country (owner)
	#ROOT - leader
	#FROMFROM - original owner for exiled leaders
	on_unit_leader_level_up = {
		effect = {
			if = { 
				limit = { is_debug = yes }
				log = "on_unit_leader_level_up | ROOT: [ROOT.GetName] | THIS: [THIS.GetName] | PREV: [PREV.GetName] | FROM: [FROM.GetName]"
			}

			if = {
				limit = {
					OR = {
						is_field_marshal = yes
						is_corps_commander = yes
						is_navy_leader = yes
					}
				}
				# do nothing
			}
			else_if = {
				limit = {
					OR = {
						is_operative = yes #seems not to work
						has_character_flag = OL_SPY_this_is_a_spy
						has_nationality = FROM #only operatives have a nationality
					}
				}

				log = "OL_SPY_levelup | Operative [THIS.GetName] from [FROM.GetName] leveled up to level [?THIS.skill_level]"

				if = {
					limit = {
						check_variable = { skill_level > 2 }
					}
					ROOT = { 
						save_event_target_as = OL_SPY_character
					}
					FROM = { 
						save_event_target_as = OL_SPY_country
					}
					operative_leader_event = outer_limits_operative_event.5
				}		
			}
		}
	}


	#####################################
	### START statistics for spys ####
	#####################################

	on_monthly_GER = {
		effect = {
			# count countries for statistics
			every_possible_country = {
				limit = {
					OR = {
						has_intelligence_agency = yes
						check_variable = { OL_spy_recruited > 0 }
						check_variable = { OL_spy_died > 0 }
						check_variable = { OL_spy_killed > 0 }
					}
				}
				add_to_variable = { global.OL_spy_countries_sum = 1 }
				every_operative = {
					add_to_variable = { global.OL_spy_alive_sum = 1 }
					add_to_variable = { global.OL_spy_alive_skill = skill_level }

					if = {
						limit = { check_variable = { skill_level = 1 } }
						add_to_variable = { global.OL_spy_level1 = 1 }
					}
					else_if = {
						limit = { check_variable = { skill_level = 2 } }
						add_to_variable = { global.OL_spy_level2 = 1 }
					}
					else_if = {
						limit = { check_variable = { skill_level = 3 } }
						add_to_variable = { global.OL_spy_level3 = 1 }
					}
					else_if = {
						limit = { check_variable = { skill_level = 4 } }
						add_to_variable = { global.OL_spy_level4 = 1 }
					}
					else_if = {
						limit = { check_variable = { skill_level = 5 } }
						add_to_variable = { global.OL_spy_level5 = 1 }
					}
					else_if = {
						limit = { check_variable = { skill_level = 6 } }
						add_to_variable = { global.OL_spy_level6 = 1 }
					}
				}
			}

			every_possible_country = {
				limit = {
					OR = {
						has_intelligence_agency = yes
						check_variable = { OL_spy_recruited > 0 }
						check_variable = { OL_spy_died > 0 }
						check_variable = { OL_spy_killed > 0 }
					}
				}
				add_to_variable = { global.OL_spy_countries_index = 1 }

				if = {
					limit = {
						check_variable = { global.OL_spy_countries_index = 1 }
					}
					log = "OL_SPY_statistics | ### Start Spy Statistics [GetMonth] [GetYear] ###"

					if = {
						limit = {
							check_variable = { global.OL_spy_alive_sum > 0 }
						}
						divide_variable = { global.OL_spy_alive_skill = global.OL_spy_alive_sum }
						log = "Recruit: [?global.OL_spy_level1] | Agent: [?global.OL_spy_level2] | Secret Agent: [?global.OL_spy_level3] | Master Spy: [?global.OL_spy_level4] | Invisible Phantom: [?global.OL_spy_level5] | Immortal Legend: [?global.OL_spy_level6]"
					}
					log = "Global | [?global.OL_spy_war_support] | [?global.OL_spy_alive_sum] alive | [?global.OL_spy_alive_skill] Skill | [?global.OL_spy_recruited] recruited | [?global.OL_spy_died] died | [?global.OL_spy_killed] killed"
				}

				every_operative = {
					PREV = { 
						add_to_variable = { OL_spy_alive_sum = 1 }
						add_to_variable = { OL_spy_alive_skill = PREV.skill_level }
					}
				}

				if = {
					limit = {
						check_variable = { OL_spy_alive_sum > 0 }
					}
					divide_variable = { OL_spy_alive_skill = OL_spy_alive_sum }
				}
				log = "[THIS.GetTag] | [?THIS.OL_spy_war_support] | [?num_of_operatives]/[?num_operative_slots] | [?OL_spy_alive_skill] Skill | [?OL_spy_recruited] recruited | [?OL_spy_died] died | [?OL_spy_killed] killed"
				clear_variable = OL_spy_alive_sum
				clear_variable = OL_spy_alive_skill
	
				if = {
					limit = {
						check_variable = { global.OL_spy_countries_index = global.OL_spy_countries_sum }
					}
					log = "### End Spy Statistics [GetMonth] [GetYear] ###"
				}
			}

			clear_variable = global.OL_spy_countries_index
			clear_variable = global.OL_spy_countries_sum
			clear_variable = global.OL_spy_alive_sum
			clear_variable = global.OL_spy_alive_skill

			clear_variable = global.OL_spy_level1
			clear_variable = global.OL_spy_level2
			clear_variable = global.OL_spy_level3
			clear_variable = global.OL_spy_level4
			clear_variable = global.OL_spy_level5
			clear_variable = global.OL_spy_level6
		}
	}

	#####################################
	### END statistics for spys ####
	#####################################

}
