############################################################################################################
#	Expert AI mod
############################################################################################################

@NUM_OPERATION_STRATEGIES = 4
@NUM_HISTORICAL_OPERATION_STRATEGIES = 5
@OPERATION_COMMIT_ADDED_DAYS_BASE = 20
@OPERATION_COMMIT_ADDED_DAYS_FACTOR = 1.1
@OPERATION_FAILED_TO_START_TIMEOUT_DAYS = 120
@OPERATION_OPERATIVES_CAPTURED_TIMEOUT_DAYS = 60
@RANDOM_OPERATION_FREQUENCY_INDUSTRY_DIVISOR = 50 # Weight = 100 * (RANDOM_OPERATION_FREQUENCY_MULT - (num_of_civilian_factories_available_for_projects / RANDOM_OPERATION_FREQUENCY_INDUSTRY_DIVISOR)^4 * RANDOM_OPERATION_FREQUENCY_MULT)
@RANDOM_OPERATION_FREQUENCY_MULT = 1
@RANDOM_OPERATION_FREQUENCY_WORLD_THREAT_MULT = 0.1 # 0 threat = 10% more likely to not start operation

EAI_ESP_operation_types = { #TOKEN

	resize_array = { global.EAI_ESP_operations = @NUM_OPERATION_STRATEGIES }
	resize_array = { global.EAI_ESP_historical_operations = @NUM_HISTORICAL_OPERATION_STRATEGIES }

	###

	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_rescue_operative                     = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_infiltrate_civilian                  = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_fake_intel                           = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_capture_cipher                       = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_infiltrate_armed_forces_army         = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_infiltrate_armed_forces_navy         = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_infiltrate_armed_forces_airforce     = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_coup_government                      = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_boost_resistance                     = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_make_resistance_contacts             = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_collaboration_government             = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_coordinated_strike                   = 2 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_steal_tech_civilian                  = 3 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_targeted_sabotage_industry           = 3 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_targeted_sabotage_infrastructure     = 3 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_targeted_sabotage_resources          = 3 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_steal_tech_army                      = 3 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_steal_tech_navy                      = 3 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_steal_tech_airforce                  = 3 }

	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_instigate_workers_revolution_britain       = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_instigate_workers_revolution_italy         = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_instigate_workers_revolution_spain         = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_instigate_workers_revolution_germany       = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_coup_in_north_africa                       = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_coup_in_syria                              = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_coup_in_indochina                          = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_coup_in_central_africa                     = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_coup_in_west_africa                        = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:FRA_coup_in_madagascar                         = 1 }

	set_variable = { global.EAI_MIN_OPERATIVES^token:heavy_water_raid                               = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:bruneval_raid                                  = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:capture_tito                                   = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:rescue_mussolini                               = 1 }

	set_variable = { global.EAI_MIN_OPERATIVES^token:raid_trotskys_villa                            = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:murder_trotsky                                 = 1 }
	set_variable = { global.EAI_MIN_OPERATIVES^token:nuclear_espionage                              = 1 }

	set_variable = { global.EAI_MIN_OPERATIVES^token:operation_warsaw_uprising                      = 2 }

	###

	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_rescue_operative                     = 30 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_infiltrate_civilian                  = 35 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_fake_intel                           = 40 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_capture_cipher                       = 40 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_infiltrate_armed_forces_army         = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_infiltrate_armed_forces_navy         = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_infiltrate_armed_forces_airforce     = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_coup_government                      = 70 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_boost_resistance                     = 35 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_make_resistance_contacts             = 40 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_collaboration_government             = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_coordinated_strike                   = 70 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_steal_tech_civilian                  = 35 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_targeted_sabotage_industry           = 35 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_targeted_sabotage_infrastructure     = 35 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_targeted_sabotage_resources          = 35 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_steal_tech_army                      = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_steal_tech_navy                      = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_steal_tech_airforce                  = 50 }

	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_instigate_workers_revolution_britain       = 25 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_instigate_workers_revolution_italy         = 25 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_instigate_workers_revolution_spain         = 25 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_instigate_workers_revolution_germany       = 25 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_coup_in_north_africa                       = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_coup_in_syria                              = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_coup_in_indochina                          = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_coup_in_central_africa                     = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_coup_in_west_africa                        = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:FRA_coup_in_madagascar                         = 50 }

	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:heavy_water_raid                               = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:bruneval_raid                                  = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:capture_tito                                   = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:rescue_mussolini                               = 35 }

	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:raid_trotskys_villa                            = 50 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:murder_trotsky                                 = 25 }
	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:nuclear_espionage                              = 75 }

	set_variable = { global.EAI_OPERATION_NETWORK_REQUIREMENT^token:operation_warsaw_uprising                      = 50 }
}

EAI_ESP_update_strategy = {

	EAI_ESP_available_operatives = yes

	if = { 
		limit = {
			OR = {
				check_variable = { EAI_operation_target = 0 }
				has_country_flag = EAI_operation_completed
			}
		}

		clr_country_flag = EAI_operation_completed
		set_country_flag = EAI_create_new_operation
	}

	# Is running operation - possible to start a new operation
	else_if = {
		limit = {
			is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }

			OR = {
				check_variable = { EAI_ESP_available_operatives > 2 } # Only if it has 3 extra agents, otherwise it will keep doing 2 agent operations

				any_operative_leader = { # Operatives need rescuing
					is_operative_captured = yes 

					var:operative_captor = {

						# Timeout or already running the same operation here
						NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
						NOT = { ROOT = { is_running_operation = { target = PREV operation = operation_rescue_operative } } }

						# Can perform operations against this country
						num_of_controlled_states > 0
					}
				} 
			}
		}

		set_country_flag = EAI_create_new_operation

		if = { limit = { has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: starting another operation while running - [?EAI_operation_type.GetTokenKey] = [?EAI_operation_target.GetName]" }
	}

	# No longer has enough available operatives to start the operation
	else_if = {
		limit = { 
			OR = {
				has_country_flag = EAI_espionage_operation_underway
				is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			}

			NOT = { is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type } }

			check_variable = { EAI_ESP_available_operatives < global.EAI_MIN_OPERATIVES^EAI_operation_type }
		}

		set_country_flag = EAI_create_new_operation
		if = { limit = { var:EAI_operation_target = { check_variable = { EAI_espionage_operation_risk_@ROOT > 1 } } }
			
			var:EAI_operation_target = { set_country_flag = { flag = EAI_operation_timeout_@ROOT value = 1 days = @OPERATION_OPERATIVES_CAPTURED_TIMEOUT_DAYS } }
		}

		if = { limit = { has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: not enough available operatives - [?EAI_operation_type.GetTokenKey] = [?EAI_operation_target.GetName]" }
	}

	# Was not able to start preparing operation during the given time
	else_if = { 
		limit = { 
			NOT = { has_country_flag = EAI_espionage_operation_underway }
			
			NOT = {
				is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
				is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			}
		}

		set_country_flag = EAI_create_new_operation
		var:EAI_operation_target = { set_country_flag = { flag = EAI_operation_timeout_@ROOT value = 1 days = @OPERATION_FAILED_TO_START_TIMEOUT_DAYS } }

		if = { limit = { has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: was not able to start operation - [?EAI_operation_type.GetTokenKey] = [?EAI_operation_target.GetName]" }
	}

	###

	if = { limit = { has_country_flag = EAI_create_new_operation }

		clr_country_flag = EAI_create_new_operation
		clr_country_flag = EAI_espionage_operation_underway
		EAI_ESP_clear_special_operation_flags = yes

		if = { limit = { check_variable = { EAI_ESP_available_operatives > 0 } }

			EAI_ESP_create_operation = yes
		}

		if = { limit = { check_variable = { started_operation = 1 } }

			EAI_ESP_commit_days = yes

			if = { limit = { has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: started operation - [?EAI_operation_type.GetTokenKey] = [?EAI_operation_target.GetTag], committed [?commit_days] days" }
		}
		else = { EAI_ESP_clear_operation = yes }
	}
}

EAI_ESP_available_operatives = {

	clear_variable = EAI_ESP_available_operatives

	if = { limit = { num_of_operatives > 0 }

		every_operative = {
			limit = {
				NOT = { is_operative_captured = yes }

				NOT = { # Not in an operation
					AND = {
						check_variable = { operation_country = 0 }
						NOT = { check_variable = { operation_type = 0 } }
					}
				}

				NOT = { # Not in hiding or wounded
					AND = {
						check_variable = { operation_country = 0 }
						check_variable = { operation_state = 0 }
						check_variable = { operation_type = 0 }
					}
				}
			}

			add_to_variable = { ROOT.EAI_ESP_available_operatives = 1 }
		}
	}
}

EAI_ESP_clear_operation = {

	clear_variable = EAI_operation_type
	set_variable = { EAI_operation_target = 0 }
}

EAI_ESP_clear_special_operation_flags = {

	clr_country_flag = EAI_operation_warsaw_uprising_OPERATION_ACTIVE
}

EAI_ESP_create_operation = {

	for_each_loop = { array = global.EAI_ESP_operations index = operation_prio break = started_operation

		### Operation strategy priorities

		if = { 		limit = { check_variable = { operation_prio = 0 } } EAI_ESP_rescue_operative_strategy = yes }

		else_if = { limit = { check_variable = { operation_prio = 1 } } EAI_ESP_historical_operations = yes }

		else_if = { limit = { check_variable = { operation_prio = 2 } } EAI_ESP_collaboration_strategy = yes }

		else_if = { limit = { check_variable = { operation_prio = 3 } } EAI_ESP_random_operation_selector = yes }
	}
}

EAI_ESP_commit_days = {

	if = { limit = { check_variable = { override_committed_days = 0 } }

		if = { 		limit = { check_variable = { EAI_ESP_available_operatives = 1 } } set_temp_variable = { intel_gain = 0.4 } }
		else_if = { limit = { check_variable = { EAI_ESP_available_operatives > 1 } } set_temp_variable = { intel_gain = 0.6 } }

		###

		set_temp_variable = { commit_days = global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type }
		divide_temp_variable = { commit_days = intel_gain }
		add_to_temp_variable = { commit_days = @OPERATION_COMMIT_ADDED_DAYS_BASE }
		multiply_temp_variable = { commit_days = @OPERATION_COMMIT_ADDED_DAYS_FACTOR }
		round_temp_variable = commit_days

		###

		meta_effect = {
			text = { set_country_flag = { flag = EAI_espionage_operation_underway value = 1 days = [x] } }
			x = "[?commit_days]"
		}
	}

	set_temp_variable = { override_committed_days = 0 }
}

### Historical operations

EAI_ESP_historical_operations = {

	for_each_loop = { array = global.EAI_ESP_historical_operations index = historical_operation_prio break = found_historical_operation

		### Historical operation strategy priorities

		if = { 		limit = { check_variable = { historical_operation_prio = 0 } } EAI_ESP_historical_operations_FRA = yes }

		else_if = { limit = { check_variable = { historical_operation_prio = 1 } } EAI_ESP_historical_operations_POL = yes }

		else_if = { limit = { check_variable = { historical_operation_prio = 2 } } EAI_ESP_historical_operations_GER = yes }

		else_if = { limit = { check_variable = { historical_operation_prio = 3 } } EAI_ESP_historical_operations_SOV = yes }

		else_if = { limit = { check_variable = { historical_operation_prio = 4 } } EAI_ESP_historical_operations_ALLIES = yes }

		###

		if = { limit = { check_variable = { started_operation = 1 } } set_temp_variable = { found_historical_operation = 1 } }
	}
}

EAI_ESP_historical_operations_FRA = {

	if = {
		limit = {
			has_focus_tree = free_french_focus
		}

		# FRA_coup_in_syria
		if = { 
			limit = {
				# Operation requirements
				has_country_flag = FRA_coup_in_syria_flag 
				var:677.CONTROLLER = {
					OR = { tag = VIC has_war_with = ROOT }
					controls_state = 677
					controls_state = 680
					controls_state = 554
					controls_state = 553

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = FRA_coup_in_syria } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:FRA_coup_in_syria }
			set_variable = { EAI_operation_target = 677.CONTROLLER }
		}

		# FRA_coup_in_central_africa
		else_if = { 
			limit = { 
				# Operation requirements
				has_country_flag = FRA_coup_in_central_africa_flag 
				var:772.CONTROLLER = {
					OR = { tag = VIC has_war_with = ROOT }
					controls_state = 772
					controls_state = 539
					controls_state = 773
					controls_state = 660
					controls_state = 774

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = FRA_coup_in_central_africa } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:FRA_coup_in_central_africa }
			set_variable = { EAI_operation_target = 772.CONTROLLER }
		}

		# FRA_coup_in_west_africa
		else_if = { 
			limit = { 
				# Operation requirements
				has_country_flag = FRA_coup_in_north_africa_flag 
				var:557.CONTROLLER = {
					OR = { tag = VIC has_war_with = ROOT }
					controls_state = 557
					controls_state = 272
					controls_state = 556
					controls_state = 898
					controls_state = 899
					controls_state = 780
					controls_state = 779
					controls_state = 778
					controls_state = 781
					controls_state = 776

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = FRA_coup_in_west_africa } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:FRA_coup_in_west_africa }
			set_variable = { EAI_operation_target = 557.CONTROLLER }
		}

		# FRA_coup_in_madagascar
		else_if = { 
			limit = { 
				date > 1942.1.1

				# Operation requirements
				has_country_flag = FRA_coup_in_madagascar_flag
				var:543.CONTROLLER = {
					OR = { tag = VIC has_war_with = ROOT }

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = FRA_coup_in_madagascar } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:FRA_coup_in_madagascar }
			set_variable = { EAI_operation_target = 543.CONTROLLER }
		}

		# FRA_coup_in_north_africa
		else_if = { 
			limit = { 
				OR = {
					date > 1943.6.1
					NOT = {
						AND = {
							check_variable = { ENG.EAI_MILITARY_strategy_id = 1 }
							check_variable = { ENG.EAI_MILITARY_INVASION_operation_id = 0 }
						}
					}
				}

				# Operation requirements
				has_country_flag = FRA_coup_in_north_africa_flag 
				var:461.CONTROLLER = {
					OR = { tag = VIC has_war_with = ROOT }
					controls_state = 461
					controls_state = 462
					controls_state = 459
					controls_state = 513
					controls_state = 460
					controls_state = 458
					controls_state = 665

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = FRA_coup_in_north_africa } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:FRA_coup_in_north_africa }
			set_variable = { EAI_operation_target = 461.CONTROLLER }
		}
	}
}

EAI_ESP_historical_operations_GER = {

	if = {
		limit = {
			original_tag = GER
		}

		# rescue_mussolini
		if = {
			limit = {
				# Timeout or already running the same operation here
				ITA = {
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = rescue_mussolini } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}

				# Operation requirements
				has_government = fascism
				has_war_with = ITA
				ITA = {
					not = {
						has_government = fascism
					}
				}
				not = { has_global_flag = mussolini_rescued_flag }
				# network_national_coverage = {
				# 	target = ITA
				# 	value > 0
				# }
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:rescue_mussolini }
			set_variable = { EAI_operation_target = ITA.id }
		}
	}
}

EAI_ESP_historical_operations_SOV = {

	if = {
		limit = {
			original_tag = SOV
		}

		# murder_trotsky
		if = {
			limit = { 
				# Operation requirements
				has_completed_focus = SOV_the_workers_dictatorship # Start building a network now
				# has_completed_focus = SOV_behead_the_snake
				NOT = { has_global_flag = flag_trotsky_dead }
				NOT = { has_global_flag = flag_trotsky_has_returned_to_ussr_from_mexico }
				NOT = { ROOT = { has_country_flag = SOV_trotskys_assassination_attempt_flag } } # This operation can only be done once
				any_country = {
					OR = {
						AND = {
							controls_state = 277 #MEXICO CITY
							OR = {
								has_global_flag = trotsky_exile_mexico
								has_global_flag = flag_mexico_invited_trotsky_to_government
							}

							# Timeout or already running the same operation here
							NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
							NOT = { ROOT = { is_running_operation = { target = PREV operation = murder_trotsky } } }

							# Can perform operations against this country
							num_of_controlled_states > 0
						}
						AND = {
							controls_state = 110 #OSTLANDET (Oslo) -> Trotsky not accepted in Mexico (or Soviets being super fast to kill him, which should be impossible btw)
							NOT = {
								has_global_flag = trotsky_exile_mexico
								has_global_flag = flag_mexico_invited_trotsky_to_government
							}

							# Timeout or already running the same operation here
							NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
							NOT = { ROOT = { is_running_operation = { target = PREV operation = murder_trotsky } } }
							
							# Can perform operations against this country
						num_of_controlled_states > 0
						}
					}
					set_temp_variable = { operation_target = THIS.id }
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:murder_trotsky }
			set_variable = { EAI_operation_target = operation_target }
			
			set_country_flag = { flag = EAI_espionage_operation_underway value = 1 days = 180 }
			set_temp_variable = { override_committed_days = 1 } 
		}

		# nuclear_espionage
		else_if = {
			limit = { 
				# Operation requirements
				has_completed_focus = SOV_soviet_atomic_bomb_project
				any_other_country = {
					is_major = yes
					NOT = { has_country_flag = SOV_nuclear_espionage_flag } # This operation can only be done once in a country
					NOT = { has_war_with = ROOT }
					has_tech = atomic_research

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = nuclear_espionage } } }

					# Can perform operations against this country
					num_of_controlled_states > 0

					set_temp_variable = { operation_target = THIS.id }
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:nuclear_espionage }
			set_variable = { EAI_operation_target = operation_target }
		}
	}
}

EAI_ESP_historical_operations_POL = {

	if = {
		limit = {
			POL = { has_completed_focus = POL_niech_zyje_opor }
			OR = {
				tag = POL
				has_war_together_with = POL
			}
		}

		# operation_warsaw_uprising
		if = { 
			limit = { 
				date > 1942.1.1

				OR = {
					AND = { tag = POL GER = { is_ai = no } }
					date > 1944.1.1
					GER = { has_country_flag = EAI_MILITARY_trend_losing_war }
					SOV = { surrender_progress > 0.35 }
				}

				OR = {
					NOT = {
						var:10.CONTROLLER = { # Warsaw controller
							core_resistance = {
								occupied_country_tag = POL
								value > 50
							}
						}
					}

					tag = POL
				}

				# Timeout or already running the same operation here
				GER = {
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { has_country_flag = EAI_operation_warsaw_uprising_OPERATION_ACTIVE } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}

				# Operation requirements
				POL = { has_completed_focus = POL_niech_zyje_opor }
				NOT = { has_global_flag = warsaw_liberated_self_flag }
				var:10.CONTROLLER = { has_war_with = POL }
			}

			# Required resistance contacts
			if = { limit = { NOT = { has_operation_token = { tag = var:EAI_operation_target token = token_resistance_contacts } } }

				set_country_flag = EAI_operation_warsaw_uprising_OPERATION_ACTIVE

				set_temp_variable = { started_operation = 1 } 

				set_variable = { EAI_operation_target = 10.CONTROLLER }
				set_variable = { EAI_operation_type = token:operation_make_resistance_contacts }
			}

			# Required resistance strength
			else_if = {
				limit = {
					NOT = {
						var:10.CONTROLLER = { # Warsaw controller
							core_resistance = {
								occupied_country_tag = POL
								value > 50
							}
						}
					}
				}

				set_country_flag = EAI_operation_warsaw_uprising_OPERATION_ACTIVE

				set_temp_variable = { started_operation = 1 } 

				set_variable = { EAI_operation_target = 10.CONTROLLER }
				set_variable = { EAI_operation_type = token:operation_boost_resistance }
			}

			# Final op
			else_if = { limit = { tag = POL }

				set_country_flag = EAI_operation_warsaw_uprising_OPERATION_ACTIVE

				set_temp_variable = { started_operation = 1 } 

				set_variable = { EAI_operation_target = 10.CONTROLLER }
				set_variable = { EAI_operation_type = token:operation_warsaw_uprising }
			}

			if = { limit = { check_variable = { started_operation = 1 } has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: operation_warsaw_uprising" }
		}
	}
}

EAI_ESP_historical_operations_ALLIES = {

	if = {
		limit = {
			NOT = { original_tag = GER }
			has_war_with = GER
		}

		# bruneval_raid
		if = {
			limit = { 
				date > 1942.1.1

				has_capitulated = no

				# Timeout or already running the same operation here
				GER = {
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = bruneval_raid } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}

				### Operation requirements

				# FROM = {
				# 	original_tag = GER
				# }
				has_war_with = GER
				GER = {
					has_tech = radio_detection
				}
				FRA = {
					has_war_with = GER
					has_capitulated = yes
				}
				not = { has_global_flag = bruneval_raid_success }
				# network_national_coverage = {
				# 	target = GER
				# 	value > 0
				# }

				# enemy_has_equal_or_better_radar_tech_trigger
				if = {
					limit = {
						ROOT = {
							has_tech = radio_detection
							NOT = {
								has_tech = cavity_magnatron
							}
						}
					}
					GER = {
						OR = {
							has_tech = radio_detection
							has_tech = cavity_magnatron
						}
					}
				}
				else_if = {
					limit = {
						ROOT = {
							has_tech = cavity_magnatron

						}
					}
					GER = {
						has_tech = cavity_magnatron
					}
				}
				else_if = {
					limit = {
						ROOT = {
							NOT = {
								has_tech = centimetric_radar
							}
						}
					}
					GER = {
						has_tech = centimetric_radar
					}
				}
				else_if = {
					limit = {
						ROOT = {
							has_tech = centimetric_radar
							NOT = {
								has_tech = phased_array
							}
						}
					}
					GER = {
						OR = {
							has_tech = centimetric_radar
							has_tech = phased_array
						}
					}
				}
				else_if = {
					limit = {
						ROOT = {
							has_tech = phased_array
							NOT = {
								has_tech = monopulse_radar
							}
						}
					}
					GER = {
						OR = {
							has_tech = phased_array
							has_tech = monopulse_radar
						}
					}
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:bruneval_raid }
			set_variable = { EAI_operation_target = GER.id }
		}

		# heavy_water_raid
		else_if = {
			limit = {
				has_capitulated = no

				# Timeout or already running the same operation here
				GER = {
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = heavy_water_raid } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}

				# Operation requirements
				has_global_flag = GER_heavy_water_production_underway
				has_war_with = GER
				110 = { resistance > 50 }
				network_strength = {
					target = GER
					state = 110
					value > 50
				}
				# network_national_coverage = {
				# 	target = GER
				# 	value > 0
				# }
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:heavy_water_raid }
			set_variable = { EAI_operation_target = GER.id }
		}
	}
}

### Rescue operative

EAI_ESP_rescue_operative_strategy = {

	if = { 
		limit = { 
			any_operative_leader = { 
				is_operative_captured = yes 

				var:operative_captor = {

					# Timeout or already running the same operation here
					NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
					NOT = { ROOT = { is_running_operation = { target = PREV operation = operation_rescue_operative } } }

					# Can perform operations against this country
					num_of_controlled_states > 0
				}

				set_temp_variable = { rescue_op = operative_captor }
			} 
		}

		set_temp_variable = { started_operation = 1 } 

		set_variable = { EAI_operation_type = token:operation_rescue_operative }
		set_variable = { EAI_operation_target = rescue_op }
	}
}

### Collaboration operation

EAI_ESP_collaboration_strategy = {

	if = {
		limit = {
			check_variable = { EAI_ESP_available_operatives > 1 }
			
			OR = {
				has_government = fascism
				has_government = communism
			}

			has_capitulated = no
		}

		### Strategies

		clear_temp_array = collab_targets
		clear_temp_array = collab_targets_value
		if = { 		limit = { tag = GER } EAI_ESP_collaboration_strategy_GER = yes }
		else_if = { limit = { tag = JAP } EAI_ESP_collaboration_strategy_JAP = yes }
		else_if = { limit = { tag = ITA } EAI_ESP_collaboration_strategy_ITA = yes }

		### Select the first valid target on the list

		if = {
			limit = {
				any_of = { array = collab_targets index = collab_targets_index value = collab_targets_target

					var:collab_targets_target = {
						# Requirements
						exists = yes
						NOT = { has_government = ROOT }
						NOT = { is_in_faction_with = ROOT }
						has_capitulated = no
						ROOT = { check_variable = { has_collaboration@PREV < collab_targets_value^collab_targets_index } }
	
						# Timeout or already running the same operation here
						NOT = { has_country_flag = EAI_operation_timeout_@ROOT }
						NOT = { ROOT = { is_running_operation = { target = PREV operation = operation_collaboration_government } } }
	
						# Can perform operations against this country
						num_of_controlled_states > 0
	
						set_temp_variable = { op_target = THIS.id }
					}
				}
			}

			set_temp_variable = { started_operation = 1 } 

			set_variable = { EAI_operation_type = token:operation_collaboration_government }
			set_variable = { EAI_operation_target = op_target }
		}
	}
}

EAI_ESP_collaboration_strategy_GER = {

	add_to_temp_array = { collab_targets = FRA.id } add_to_temp_array = { collab_targets_value = 0.6 }
	add_to_temp_array = { collab_targets = POL.id } add_to_temp_array = { collab_targets_value = 0.6 }
	add_to_temp_array = { collab_targets = NOR.id } add_to_temp_array = { collab_targets_value = 0.25 }
	# add_to_temp_array = { collab_targets = BEL.id } add_to_temp_array = { collab_targets_value = 0.1 }
	# add_to_temp_array = { collab_targets = HOL.id } add_to_temp_array = { collab_targets_value = 0.1 }
	# add_to_temp_array = { collab_targets = YUG.id } add_to_temp_array = { collab_targets_value = 0.1 }
}

EAI_ESP_collaboration_strategy_JAP = {

	if = {
		limit = {
			date > 1939.1.1 
			NOT = { has_war_with = CHI }
		}
	
		add_to_temp_array = { collab_targets = PHI.id } add_to_temp_array = { collab_targets_value = 0.1 }
		add_to_temp_array = { collab_targets = INS.id } add_to_temp_array = { collab_targets_value = 0.1 }
		add_to_temp_array = { collab_targets = MAL.id } add_to_temp_array = { collab_targets_value = 0.1 }
		add_to_temp_array = { collab_targets = AST.id } add_to_temp_array = { collab_targets_value = 0.1 }
		add_to_temp_array = { collab_targets = RAJ.id } add_to_temp_array = { collab_targets_value = 0.1 }
	}
	# else = {

	# 	add_to_temp_array = { collab_targets = CHI.id } add_to_temp_array = { collab_targets_value = 0.5 }
	# }
}

EAI_ESP_collaboration_strategy_ITA = {

	if = { limit = { date > 1939.1.1 }

		add_to_temp_array = { collab_targets = GRE.id } add_to_temp_array = { collab_targets_value = 0.1 }
	}
}

### Random operation selector

EAI_ESP_random_operation_selector = {

	EAI_ESP_random_operation_selector_strategies = yes

	if = { limit = { check_variable = { EAI_ESP_available_operatives > 1 } check_variable = { disable_espionage_selector = 0 } }

		### Highest scoring target countries for each operation

		every_other_country = { # Something causes the scorer to return incorrect result from certain triggers, making this system useless without this fix
			limit = {
				OR = {
					is_in_array = { ROOT.potential_and_current_enemies = THIS.id }
					is_in_array = { ROOT.enemies_of_allies = THIS.id }
					is_in_array = { ROOT.neighbors = THIS.id }
					is_in_array = { global.majors = THIS.id }
				}
			}

			ROOT = {
				
				if = { limit = { has_operation_token = { tag = PREV token = token_civilian } }                                      set_country_flag = { flag = has_token_civilian_@PREV value = 1 days = 1 } }
				if = { limit = { has_operation_token = { tag = PREV token = token_army } }                                          set_country_flag = { flag = has_token_army_@PREV value = 1 days = 1 } }
				if = { limit = { has_operation_token = { tag = PREV token = token_navy } }                                          set_country_flag = { flag = has_token_navy_@PREV value = 1 days = 1 } }
				if = { limit = { has_operation_token = { tag = PREV token = token_airforce } }                                      set_country_flag = { flag = has_token_airforce_@PREV value = 1 days = 1 } }
				if = { limit = { has_operation_token = { tag = PREV token = token_resistance_contacts } }                           set_country_flag = { flag = has_token_resistance_contacts_@PREV value = 1 days = 1 } }

				if = { limit = { is_running_operation = { target = PREV operation = operation_infiltrate_civilian  } }              set_country_flag = { flag = is_running_operation_infiltrate_civilian_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_infiltrate_armed_forces_army  } }     set_country_flag = { flag = is_running_operation_infiltrate_armed_forces_army_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_infiltrate_armed_forces_navy  } }     set_country_flag = { flag = is_running_operation_infiltrate_armed_forces_navy_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_infiltrate_armed_forces_airforce  } } set_country_flag = { flag = is_running_operation_infiltrate_armed_forces_airforce_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_make_resistance_contacts  } }         set_country_flag = { flag = is_running_operation_make_resistance_contacts_@PREV value = 1 days = 1 } }

				if = { limit = { is_running_operation = { target = PREV operation = operation_steal_tech_civilian  } }              set_country_flag = { flag = is_running_operation_steal_tech_civilian_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_steal_tech_army  } }                  set_country_flag = { flag = is_running_operation_steal_tech_army_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_steal_tech_navy  } }                  set_country_flag = { flag = is_running_operation_steal_tech_navy_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_steal_tech_airforce  } }              set_country_flag = { flag = is_running_operation_steal_tech_airforce_@PREV value = 1 days = 1 } }

				if = { limit = { is_running_operation = { target = PREV operation = operation_boost_resistance  } }                 set_country_flag = { flag = is_running_operation_boost_resistance_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_targeted_sabotage_industry  } }       set_country_flag = { flag = is_running_operation_targeted_sabotage_industry_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_targeted_sabotage_infrastructure  } } set_country_flag = { flag = is_running_operation_targeted_sabotage_infrastructure_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_targeted_sabotage_resources  } }      set_country_flag = { flag = is_running_operation_targeted_sabotage_resources_@PREV value = 1 days = 1 } }

				if = { limit = { is_running_operation = { target = PREV operation = operation_capture_cipher  } }                   set_country_flag = { flag = is_running_operation_capture_cipher_@PREV value = 1 days = 1 } }
				if = { limit = { is_running_operation = { target = PREV operation = operation_collaboration_government  } }         set_country_flag = { flag = is_running_operation_collaboration_government_@PREV value = 1 days = 1 } }
			}
		}

		# Define vars to avoid error spam

		set_temp_variable = { EAI_SCORER_operation_infiltrate_civilian_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_infiltrate_armed_forces_army_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_infiltrate_armed_forces_navy_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_infiltrate_armed_forces_airforce_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_make_resistance_contacts_SCORES^0 = 0 }

		set_temp_variable = { EAI_SCORER_operation_steal_tech_civilian_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_steal_tech_army_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_steal_tech_navy_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_steal_tech_airforce_SCORES^0 = 0 }

		set_temp_variable = { EAI_SCORER_operation_boost_resistance_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_targeted_sabotage_industry_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_targeted_sabotage_infrastructure_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_targeted_sabotage_resources_SCORES^0 = 0 }

		set_temp_variable = { EAI_SCORER_operation_capture_cipher_SCORES^0 = 0 }
		set_temp_variable = { EAI_SCORER_operation_collaboration_government_SCORES^0 = 0 }

		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_infiltrate_civilian                     array = EAI_SCORER_operation_infiltrate_civilian_TARGETS                   scores = EAI_SCORER_operation_infiltrate_civilian_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_infiltrate_armed_forces_army            array = EAI_SCORER_operation_infiltrate_armed_forces_army_TARGETS          scores = EAI_SCORER_operation_infiltrate_armed_forces_army_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_infiltrate_armed_forces_navy            array = EAI_SCORER_operation_infiltrate_armed_forces_navy_TARGETS          scores = EAI_SCORER_operation_infiltrate_armed_forces_navy_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_infiltrate_armed_forces_airforce        array = EAI_SCORER_operation_infiltrate_armed_forces_airforce_TARGETS      scores = EAI_SCORER_operation_infiltrate_armed_forces_airforce_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_make_resistance_contacts                array = EAI_SCORER_operation_make_resistance_contacts_TARGETS              scores = EAI_SCORER_operation_make_resistance_contacts_SCORES }

		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_steal_tech_civilian                     array = EAI_SCORER_operation_steal_tech_civilian_TARGETS                   scores = EAI_SCORER_operation_steal_tech_civilian_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_steal_tech_army                         array = EAI_SCORER_operation_steal_tech_army_TARGETS                       scores = EAI_SCORER_operation_steal_tech_army_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_steal_tech_navy                         array = EAI_SCORER_operation_steal_tech_navy_TARGETS                       scores = EAI_SCORER_operation_steal_tech_navy_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_steal_tech_airforce                     array = EAI_SCORER_operation_steal_tech_airforce_TARGETS                   scores = EAI_SCORER_operation_steal_tech_airforce_SCORES }

		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_boost_resistance                        array = EAI_SCORER_operation_boost_resistance_TARGETS                      scores = EAI_SCORER_operation_boost_resistance_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_targeted_sabotage_industry              array = EAI_SCORER_operation_targeted_sabotage_industry_TARGETS            scores = EAI_SCORER_operation_targeted_sabotage_industry_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_targeted_sabotage_infrastructure        array = EAI_SCORER_operation_targeted_sabotage_infrastructure_TARGETS      scores = EAI_SCORER_operation_targeted_sabotage_infrastructure_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_targeted_sabotage_resources             array = EAI_SCORER_operation_targeted_sabotage_resources_TARGETS           scores = EAI_SCORER_operation_targeted_sabotage_resources_SCORES }

		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_capture_cipher                          array = EAI_SCORER_operation_capture_cipher_TARGETS                        scores = EAI_SCORER_operation_capture_cipher_SCORES }
		get_sorted_scored_countries_temp = { scorer = EAI_SCORER_operation_collaboration_government                array = EAI_SCORER_operation_collaboration_government_TARGETS              scores = EAI_SCORER_operation_collaboration_government_SCORES }

		if = { limit = { has_country_flag = EAI_espionage_score_logging }

			log = "======================================================================================="
			log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE SCORES: available operatives = [?EAI_ESP_available_operatives]"
			log = " "
			log = "=== GET TOKEN OPS"
			log = "[?EAI_SCORER_operation_infiltrate_civilian_TARGETS^0.GetTag] - [?EAI_SCORER_operation_infiltrate_civilian_SCORES^0|0] - infiltrate_civilian"
			log = "[?EAI_SCORER_operation_infiltrate_armed_forces_army_TARGETS^0.GetTag] - [?EAI_SCORER_operation_infiltrate_armed_forces_army_SCORES^0|0] - infiltrate_armed_forces_army"
			log = "[?EAI_SCORER_operation_infiltrate_armed_forces_navy_TARGETS^0.GetTag] - [?EAI_SCORER_operation_infiltrate_armed_forces_navy_SCORES^0|0] - infiltrate_armed_forces_navy"
			log = "[?EAI_SCORER_operation_infiltrate_armed_forces_airforce_TARGETS^0.GetTag] - [?EAI_SCORER_operation_infiltrate_armed_forces_airforce_SCORES^0|0] - infiltrate_armed_forces_airforce"
			log = "[?EAI_SCORER_operation_make_resistance_contacts_TARGETS^0.GetTag] - [?EAI_SCORER_operation_make_resistance_contacts_SCORES^0|0] - make_resistance_contacts"
			log = " "
			log = "=== USE INFILTRATION TOKEN OPS"
			log = "[?EAI_SCORER_operation_steal_tech_civilian_TARGETS^0.GetTag] - [?EAI_SCORER_operation_steal_tech_civilian_SCORES^0|0] - steal_tech_civilian"
			log = "[?EAI_SCORER_operation_steal_tech_army_TARGETS^0.GetTag] - [?EAI_SCORER_operation_steal_tech_army_SCORES^0|0] - steal_tech_army"
			log = "[?EAI_SCORER_operation_steal_tech_navy_TARGETS^0.GetTag] - [?EAI_SCORER_operation_steal_tech_navy_SCORES^0|0] - steal_tech_navy"
			log = "[?EAI_SCORER_operation_steal_tech_airforce_TARGETS^0.GetTag] - [?EAI_SCORER_operation_steal_tech_airforce_SCORES^0|0] - steal_tech_airforce"
			log = " "
			log = "=== USE RESISTANCE TOKEN OPS"
			log = "[?EAI_SCORER_operation_boost_resistance_TARGETS^0.GetTag] - [?EAI_SCORER_operation_boost_resistance_SCORES^0|0] - boost_resistance"
			log = "[?EAI_SCORER_operation_targeted_sabotage_industry_TARGETS^0.GetTag] - [?EAI_SCORER_operation_targeted_sabotage_industry_SCORES^0|0] - targeted_sabotage_industry"
			log = "[?EAI_SCORER_operation_targeted_sabotage_infrastructure_TARGETS^0.GetTag] - [?EAI_SCORER_operation_targeted_sabotage_infrastructure_SCORES^0|0] - targeted_sabotage_infrastructure"
			log = "[?EAI_SCORER_operation_targeted_sabotage_resources_TARGETS^0.GetTag] - [?EAI_SCORER_operation_targeted_sabotage_resources_SCORES^0|0] - targeted_sabotage_resources"
			log = " "
			log = "=== MISC OPS"
			log = "[?EAI_SCORER_operation_capture_cipher_TARGETS^0.GetTag] - [?EAI_SCORER_operation_capture_cipher_SCORES^0|0] - capture_cipher"
			log = "[?EAI_SCORER_operation_collaboration_government_TARGETS^0.GetTag] - [?EAI_SCORER_operation_collaboration_government_SCORES^0|0] - collaboration_government"
			log = "======================================================================================="
		}

		### Random weight = highest scoring target country score

		random_list = {

			1 = {

				modifier = { factor = EAI_SCORER_operation_infiltrate_civilian_SCORES^0 }

				### Final modifiers on operation type

				modifier = {
					factor = 2

					has_war = no
				}

				modifier = {
					factor = 0.5
					
					has_war = yes
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_infiltrate_civilian_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_infiltrate_civilian }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_infiltrate_armed_forces_army_SCORES^0 }

				### Final modifiers on operation type

				modifier = {
					factor = 0.75

					has_war = yes
				}

				modifier = {
					has_war = no

					factor = global.threat
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_infiltrate_armed_forces_army_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_infiltrate_armed_forces_army }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_infiltrate_armed_forces_navy_SCORES^0 }

				### Final modifiers on operation type

				modifier = {
					factor = 0.75

					has_war = yes
				}

				modifier = {
					has_war = no

					factor = global.threat
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_infiltrate_armed_forces_navy_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_infiltrate_armed_forces_navy }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_infiltrate_armed_forces_airforce_SCORES^0 }

				### Final modifiers on operation type

				modifier = {
					factor = 0.75

					has_war = yes
				}

				modifier = {
					has_war = no

					factor = global.threat
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_infiltrate_armed_forces_airforce_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_infiltrate_armed_forces_airforce }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_make_resistance_contacts_SCORES^0 }

				### Final modifiers on operation type

				set_variable = { EAI_operation_target = EAI_SCORER_operation_make_resistance_contacts_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_make_resistance_contacts }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_steal_tech_civilian_SCORES^0 }

				### Final modifiers on operation type

				modifier = { 
					factor = 0.5
					
					has_war = yes
				}

				modifier = { 
					factor = 2
					
					has_war = no
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_steal_tech_civilian_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_steal_tech_civilian }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_steal_tech_army_SCORES^0 }

				### Final modifiers on operation type

				modifier = { 
					factor = 0.5
					
					has_war = yes
				}

				modifier = { 
					factor = 2
					
					has_war = no
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_steal_tech_army_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_steal_tech_army }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_steal_tech_navy_SCORES^0 }

				### Final modifiers on operation type

				modifier = { 
					factor = 0.5
					
					has_war = yes
				}

				modifier = { 
					factor = 2
					
					has_war = no
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_steal_tech_navy_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_steal_tech_navy }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_steal_tech_airforce_SCORES^0 }

				### Final modifiers on operation type

				modifier = { 
					factor = 0.5
					
					has_war = yes
				}

				modifier = { 
					factor = 2
					
					has_war = no
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_steal_tech_airforce_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_steal_tech_airforce }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_boost_resistance_SCORES^0 }

				### Final modifiers on operation type

				set_variable = { EAI_operation_target = EAI_SCORER_operation_boost_resistance_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_boost_resistance }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_targeted_sabotage_industry_SCORES^0 }

				### Final modifiers on operation type

				set_variable = { EAI_operation_target = EAI_SCORER_operation_targeted_sabotage_industry_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_targeted_sabotage_industry }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_targeted_sabotage_infrastructure_SCORES^0 }

				### Final modifiers on operation type

				set_variable = { EAI_operation_target = EAI_SCORER_operation_targeted_sabotage_infrastructure_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_targeted_sabotage_infrastructure }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_targeted_sabotage_resources_SCORES^0 }

				### Final modifiers on operation type

				set_variable = { EAI_operation_target = EAI_SCORER_operation_targeted_sabotage_resources_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_targeted_sabotage_resources }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_capture_cipher_SCORES^0 }

				### Final modifiers on operation type

				modifier = {
					factor = 0.25

					has_capitulated = yes
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_capture_cipher_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_capture_cipher }
				set_temp_variable = { started_operation = 1 } 
			}

			1 = {

				modifier = { factor = EAI_SCORER_operation_collaboration_government_SCORES^0 }

				### Final modifiers on operation type

				modifier = {
					factor = 0

					NOT = {
						has_government = fascism
						has_government = communism
					}
				}

				set_variable = { EAI_operation_target = EAI_SCORER_operation_collaboration_government_TARGETS^0 }
				set_variable = { EAI_operation_type = token:operation_collaboration_government }
				set_temp_variable = { started_operation = 1 } 
			}

			100 = { # Do nothing

				modifier = { # Do use tokens tho
					factor = 0.1

					OR = {
						check_variable = { EAI_SCORER_operation_steal_tech_civilian_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_steal_tech_army_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_steal_tech_navy_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_steal_tech_airforce_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_boost_resistance_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_targeted_sabotage_industry_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_targeted_sabotage_infrastructure_SCORES^0 > 50 }
						check_variable = { EAI_SCORER_operation_targeted_sabotage_resources_SCORES^0 > 50 }
					}
				}

				modifier = { # Industry size affects operation frequency

					set_temp_variable = { temp = num_of_civilian_factories_available_for_projects }
					divide_temp_variable = { temp = @RANDOM_OPERATION_FREQUENCY_INDUSTRY_DIVISOR }
					multiply_temp_variable = { temp = temp }
					multiply_temp_variable = { temp = temp }
					clamp_temp_variable = { var = temp min = 0 max = 0.75 }
					multiply_temp_variable = { temp = @RANDOM_OPERATION_FREQUENCY_MULT }

					set_temp_variable = { industry_factor = @RANDOM_OPERATION_FREQUENCY_MULT }
					subtract_from_temp_variable = { industry_factor = temp }

					factor = industry_factor
				}

				modifier = { # Global threat has a minor effect on frequency

					set_temp_variable = { threat_factor = 1 }
					subtract_from_temp_variable = { threat_factor = global.threat }
					multiply_temp_variable = { threat_factor = @RANDOM_OPERATION_FREQUENCY_WORLD_THREAT_MULT }
					add_to_temp_variable = { threat_factor = 1 }

					factor = threat_factor
				}

				modifier = { # Does not have 3 operatives needed for better operations

					num_of_operatives < 3

					is_major = no

					has_war = no

					factor = 3
				}

				if = { limit = { has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: skipped starting operation" }
			}
		}
	}
}

EAI_ESP_random_operation_selector_strategies = {

	set_temp_variable = { disable_espionage_selector = 0 }

	###

	if = { limit = { tag = SOV }

		if = {
			limit = {
				OR = {
					check_variable = { EAI_PC_strategy_state = 1 }
					check_variable = { EAI_PC_strategy_state = 1.5 }
					check_variable = { EAI_PC_strategy_state = 2 }
				}
			}

			set_temp_variable = { disable_espionage_selector = 1 }
		}
	}
}

######################################################
###		Agency upgrades
######################################################

EAI_ESP_max_agency_upgrades = {

	set_variable = { EAI_ESP_max_agency_upgrades = global.EAI_months_elapsed }

	if = { limit = { is_major = yes }

		divide_variable = { EAI_ESP_max_agency_upgrades = 6 }
	}
	else = {
		divide_variable = { EAI_ESP_max_agency_upgrades = 12 }
	}
	
	if = { limit = { has_capitulated = yes }

		multiply_variable = { EAI_ESP_max_agency_upgrades = 5 }
	}

	set_temp_variable = { industry_factor = num_of_factories }
	divide_temp_variable = { industry_factor = 1000 }
	add_to_temp_variable = { industry_factor = 1 }
	multiply_variable = { EAI_ESP_max_agency_upgrades = industry_factor }
}

# Workaround to AI being slow to upgrade agency or unwilling to upgrade at all because of small industry
EAI_ESP_force_upgrade_agency = {

	EAI_ESP_agency_upgrade_strategies = yes

	if = {
		limit = { 
			has_dlc = "La Resistance"
			
			OR = {
				check_variable = { agency_upgrade_number < EAI_espionage_agency_target_level }
				AND = { check_variable = { EAI_build_espionage_agency = 1 } NOT = { has_intelligence_agency = yes } }
			}

			NOT = { has_country_flag = EAI_ESP_force_upgrade_agency_active }

			check_variable = { EAI_available_civilian_factories > 7 }
		}

		### Upgrades

		if = { 		limit = { NOT = { has_intelligence_agency = yes } } set_variable = { EAI_ESP_force_upgrade_agency_factories = 5 } }
		else_if = { limit = { check_variable = { agency_upgrade_number = 0 } } set_variable = { EAI_ESP_force_upgrade_agency_factories = 8 } }
		else_if = { limit = { check_variable = { agency_upgrade_number = 1 } } set_variable = { EAI_ESP_force_upgrade_agency_factories = 5 } }
		else_if = { limit = { check_variable = { agency_upgrade_number = 2 } } set_variable = { EAI_ESP_force_upgrade_agency_factories = 5 } }
		else_if = { limit = { check_variable = { agency_upgrade_number = 3 } } set_variable = { EAI_ESP_force_upgrade_agency_factories = 5 } }
		else_if = { limit = { check_variable = { agency_upgrade_number = 4 } } set_variable = { EAI_ESP_force_upgrade_agency_factories = 5 } }

		set_country_flag = EAI_ESP_force_upgrade_agency_active
		set_country_flag = { flag = EAI_ESP_force_upgrade_agency_duration value = 1 days = 30 }
		add_dynamic_modifier = { modifier = EAI_ESP_force_upgrade_agency_modifier days = 30 }
		add_to_variable = { EAI_misc_reserved_factories = EAI_ESP_force_upgrade_agency_factories }
	}
}

EAI_ESP_add_finished_agency_upgrade = {

	if = { limit = { has_country_flag = EAI_ESP_force_upgrade_agency_active NOT = { has_country_flag = EAI_ESP_force_upgrade_agency_duration } }

		if = { 		limit = { NOT = { has_intelligence_agency = yes } } create_intelligence_agency = yes }
		else_if = { limit = { check_variable = { agency_upgrade_number = 0 } } upgrade_intelligence_agency = upgrade_form_department }
		else_if = { limit = { check_variable = { agency_upgrade_number = 1 } } upgrade_intelligence_agency = upgrade_suicide_pills }
		else_if = { limit = { check_variable = { agency_upgrade_number = 2 } } upgrade_intelligence_agency = upgrade_interrogation_techniques }
		else_if = { limit = { check_variable = { agency_upgrade_number = 3 } } upgrade_intelligence_agency = upgrade_passive_defense }
		else_if = { limit = { check_variable = { agency_upgrade_number = 4 } } upgrade_intelligence_agency = upgrade_crypto_strength }

		clr_country_flag = EAI_ESP_force_upgrade_agency_active
		subtract_from_variable = { EAI_misc_reserved_factories = EAI_ESP_force_upgrade_agency_factories }
		set_variable = { EAI_ESP_force_upgrade_agency_factories = 0 }
	}
}

EAI_ESP_agency_upgrade_strategies = {

	set_temp_variable = { EAI_build_espionage_agency = 0 }
	set_temp_variable = { EAI_espionage_agency_target_level = 0 }

	### Majors

	if = { limit = { is_major = yes }

		if = { 
			limit = { 
				OR = {
					has_war = yes
					date > 1937.1.1
				}
			}

			set_temp_variable = { EAI_build_espionage_agency = 1 }
		}

		if = { 
			limit = { 
				OR = {
					has_war = yes
					date > 1938.1.1
				}
			}

			set_temp_variable = { EAI_espionage_agency_target_level = 5 }
		}
	}

	### Minors

	if = { limit = { is_major = no }

		if = { 
			limit = { 
				OR = {
					has_war = yes
					date > 1937.1.1
				}
			}

			set_temp_variable = { EAI_build_espionage_agency = 1 }
		}

		if = { 
			limit = { 
				OR = {
					has_war = yes
					date > 1938.1.1
				}
			}

			set_temp_variable = { EAI_espionage_agency_target_level = 5 }
		}
	}

	### GER

	if = { limit = { tag = GER }

		if = { 
			limit = { 
				always = yes
			}

			set_temp_variable = { EAI_build_espionage_agency = 1 }
			set_temp_variable = { EAI_espionage_agency_target_level = 5 }
		}
	}

	### SOV

	if = { limit = { tag = SOV }

		if = { limit = { is_historical_focus_on = yes }
		
			# Max CIC build
			if = {
				limit = {
					date < 1941.1.1
				}
			
				set_temp_variable = { EAI_build_espionage_agency = 0 }
				set_temp_variable = { EAI_espionage_agency_target_level = 0 }
			}
			else_if = {
				limit = {
					OR = {
						check_variable = { EAI_PC_strategy_state = 1 }
						check_variable = { EAI_PC_strategy_state = 1.5 }
						check_variable = { EAI_PC_strategy_state = 2 }
					}
				}
			
				set_temp_variable = { EAI_build_espionage_agency = 1 }
				set_temp_variable = { EAI_espionage_agency_target_level = 0 }
			}

			# Build agency for Trotsky
			if = {
				limit = {
					OR = {
						has_completed_focus = SOV_the_workers_dictatorship
						focus_progress = { focus = SOV_the_workers_dictatorship progress > 0 }
						has_completed_focus = SOV_behead_the_snake
						focus_progress = { focus = SOV_behead_the_snake progress > 0 }
					}
				}
			
				set_temp_variable = { EAI_build_espionage_agency = 1 }
				set_temp_variable = { EAI_espionage_agency_target_level = 0 }
			}
		}
	}

	### JAP

	if = { limit = { tag = JAP }

		# Collaborations in CHI asap
		set_temp_variable = { EAI_build_espionage_agency = 1 }
		set_temp_variable = { EAI_espionage_agency_target_level = 5 }
	}
}