update_operation_ai = {
	if = {
		limit = {
			NOT = { check_variable = { generic_operation_target = 0 } }
			var:generic_operation_target = {
				OR = {
					exists = no
					has_capitulated = yes
					is_ally_with = PREV
				}
			}
		}
		set_variable = { generic_operation_target = 0 }
	}

	if = {
		limit = { ai_can_run_operations = yes }
		ai_custom_operation_and_target_weight = yes
	}

	clear_array = ai_operation_targets
	get_highest_scored_country = {
		scorer = ai_generic_operation_target_scorer
		var = generic_operation_target
	}
	if = {
		limit = { NOT = { check_variable = { generic_operation_target = 0 } } }
		add_to_array = { ai_operation_targets = generic_operation_target }
	}

	set_temp_variable = { captor = 0 }
	set_temp_variable = { min_needed_operative_for_operations = 0 }
	every_operative = {
		limit = { is_operative_captured = yes }
		add_to_temp_variable = { min_needed_operative_for_operations = 1 }
		if = {
			limit = { check_variable = { captor = 0 } }
			set_temp_variable = { captor = operative_captor }
		}
	}
	set_variable = { rescue_operative_from = 0 }
	if = {
		limit = { NOT = { check_variable = { captor = 0 } } }
		set_variable = { rescue_operative_from = captor }
		add_to_temp_variable = { min_needed_operative_for_operations = 2 }
	}

	set_temp_variable = { t = min_needed_operative_for_operations }
	subtract_from_temp_variable = { t = 2 }
	if = {
		limit = { num_of_operatives < t }
		set_variable = { generic_operation_target = 0 }
	}

	if = {
		limit = {
			NOT = { check_variable = { generic_operation_target = 0 } }
			num_of_operatives > min_needed_operative_for_operations
		}
		clear_temp_array = operation_types
		clear_temp_array = operation_types_scores

		if = {
			limit = {
				NOT = {
					has_operation_token = {
						tag = var:generic_operation_target
						token = token_civilian
					}
				}
			}
			set_temp_variable = { score = 100 }
			add_to_temp_array = { operation_types = token:operation_infiltrate_civilian }
			add_to_temp_array = { operation_types_scores = score }
		}

		if = {
			limit = {
				NOT = {
					has_operation_token = {
						tag = var:generic_operation_target
						token = token_army
					}
				}
			}
			set_temp_variable = { score = 50 }
			var:generic_operation_target = {
				add_to_temp_variable = { score = num_armies }
			}
			clamp_temp_variable = { var = score max = 180 }
			add_to_temp_array = { operation_types = token:operation_infiltrate_armed_forces_army }
			add_to_temp_array = { operation_types_scores = score }
		}

		if = {
			limit = {
				NOT = {
					has_operation_token = {
						tag = var:generic_operation_target
						token = token_airforce
					}
				}
			}
			set_temp_variable = { score = 50 }
			var:generic_operation_target = {
				set_temp_variable = { t = num_deployed_planes }
			}
			divide_temp_variable = { t = 10 }
			add_to_temp_variable = { score = t }
			clamp_temp_variable = { var = score max = 180 }
			add_to_temp_array = { operation_types = token:operation_infiltrate_armed_forces_airforce }
			add_to_temp_array = { operation_types_scores = score }
		}

		if = {
			limit = {
				NOT = {
					has_operation_token = {
						tag = var:generic_operation_target
						token = token_navy
					}
				}
				var:generic_operation_target = { has_navy_size = { size > 0 } }
				has_navy_size = { size > 0 }
			}
			var:generic_operation_target = {
				set_temp_variable = { score = num_ships }
				set_temp_variable = { t = num_ships_with_type@carrier }
				multiply_temp_variable = { t = 20 }
				add_to_temp_variable = { score = t }
				set_temp_variable = { t = num_ships_with_type@capital }
				multiply_temp_variable = { t = 10 }
				add_to_temp_variable = { score = t }
			}
			clamp_temp_variable = { var = score max = 180 }
			add_to_temp_array = { operation_types = token:operation_infiltrate_armed_forces_navy }
			add_to_temp_array = { operation_types_scores = score }
		}

		var:generic_operation_target = {
			if = {
				limit = {
					PREV = {
						NOT = { tag = ENG }
						NOT = { has_government = fascism }
						NOT = {
							has_operation_token = {
								tag = var:generic_operation_target
								token = token_resistance_contacts
							}
						}
					}
					has_government = fascism
					check_variable = { occupied_countries^num > 0 }
					has_added_tension_amount > 20
				}
				set_temp_variable = { score = 1 }
				add_to_temp_variable = { score = num_controlled_states }
				subtract_from_temp_variable = { score = num_owned_states }
				clamp_temp_variable = { var = score min = 1 }
				multiply_temp_variable = { score = 15 }
				add_to_temp_variable = { score = 50 }
				clamp_temp_variable = { var = score max = 200 }
				add_to_temp_array = { operation_types = token:operation_make_resistance_contacts }
				add_to_temp_array = { operation_types_scores = score }
			}
		}

		if = {
			limit = {
				ai_can_run_operations = yes
				has_operation_token = { tag = var:generic_operation_target token = token_civilian }
			}
			var:generic_operation_target = {
				set_temp_variable = { score = mtth:operation_steal_tech_civilian }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_steal_tech_civilian }
				add_to_temp_array = { operation_types_scores = score }
				set_temp_variable = { score = mtth:operation_sabotage_fuel }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_sabotage_fuel }
				add_to_temp_array = { operation_types_scores = score }
				set_temp_variable = { score = mtth:operation_sabotage_infra }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_sabotage_infra }
				add_to_temp_array = { operation_types_scores = score }
				set_temp_variable = { score = mtth:operation_sabotage_oil }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_sabotage_oil }
				add_to_temp_array = { operation_types_scores = score }
			}
		}

		if = {
			limit = {
				ai_can_run_operations = yes
				has_operation_token = { tag = var:generic_operation_target token = token_army }
			}
			var:generic_operation_target = {
				set_temp_variable = { score = mtth:operation_target_attache }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_target_attache }
				add_to_temp_array = { operation_types_scores = score }
			}
		}

		if = {
			limit = {
				ai_can_run_operations = yes
				has_operation_token = { tag = var:generic_operation_target token = token_airforce }
			}
			var:generic_operation_target = {
				set_temp_variable = { score = mtth:operation_sabotage_radar }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_sabotage_radar }
				add_to_temp_array = { operation_types_scores = score }
			}
		}

		if = {
			limit = {
				ai_can_run_operations = yes
				has_operation_token = { tag = var:generic_operation_target token = token_navy }
			}
			var:generic_operation_target = {
				set_temp_variable = { score = mtth:operation_sabotage_port }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_sabotage_port }
				add_to_temp_array = { operation_types_scores = score }
			}
		}

		if = {
			limit = {
				ai_can_run_operations = yes
				has_operation_token = { tag = var:generic_operation_target token = token_resistance_contacts }
			}
			var:generic_operation_target = {
				set_temp_variable = { score = mtth:operation_strengthen_resistance }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_strengthen_resistance }
				add_to_temp_array = { operation_types_scores = score }
				set_temp_variable = { score = mtth:operation_strengthen_society }
				multiply_temp_variable = { score = 100 }
				add_to_temp_array = { operation_types = token:operation_strengthen_society }
				add_to_temp_array = { operation_types_scores = score }
			}
		}

		clear_array = ai_operations
		clear_temp_array = operation_types_sorted
		clear_temp_array = operation_types_score_sorted

		for_each_loop = {
			array = operation_types
			break = none
			index = entry

			for_each_loop = {
				array = operation_types_score_sorted
				value = s
				if = {
					limit = { check_variable = { operation_types_scores^entry > s } }
					add_to_temp_array = { array = operation_types_sorted index = i value = v }
					add_to_temp_array = { array = operation_types_score_sorted index = i value = operation_types_scores^entry }
					set_temp_variable = { break = 1 }
				}
			}

			if = {
				limit = { check_variable = { break = 0 } }
				add_to_temp_array = { array = operation_types_sorted value = v }
				add_to_temp_array = { array = operation_types_score_sorted value = operation_types_scores^entry }
			}
			else = {
				set_temp_variable = { break = 0 }
			}
		}

		for_each_loop = {
			array = operation_types_sorted
			if = {
				limit = { check_variable = { i < 3 } }
				add_to_array = { ai_operations = v }
			}
		}

		if = {
			limit = { check_variable = { operation_types_score_sorted^0 > 0 } }
			set_variable = { generic_operation_type_to_run = operation_types_sorted^0 }
		}
		else = {
			set_variable = { generic_operation_type_to_run = 0 }
		}
	}
	else = {
		clear_array = ai_operations
		set_variable = { generic_operation_type_to_run = 0 }
	}

	ai_score_resistance_areas = yes
}

ai_custom_operation_and_target_weight = {
	every_enemy_country = {
		limit = {
			exists = yes
			has_capitulated = no
			NOT = { is_ally_with = ROOT }
		}
		ROOT = {
			add_to_temp_variable = { operation_target@PREV = 250 }
		}
		if = {
			limit = { is_ai = no }
			ROOT = {
				add_to_temp_variable = { operation_target@PREV = 350 }
				add_to_temp_variable = { operation_steal_tech_civilian@PREV = 3 }
				add_to_temp_variable = { operation_sabotage_fuel@PREV = 4 }
				add_to_temp_variable = { operation_sabotage_infra@PREV = 4 }
				add_to_temp_variable = { operation_sabotage_oil@PREV = 4 }
				add_to_temp_variable = { operation_sabotage_radar@PREV = 3 }
				add_to_temp_variable = { operation_sabotage_port@PREV = 3 }
			}
		}
		if = {
			limit = { is_major = yes }
			ROOT = {
				add_to_temp_variable = { operation_target@PREV = 150 }
				add_to_temp_variable = { operation_steal_tech_civilian@PREV = 2 }
			}
		}
	}

	if = {
		limit = { tag = GER }
		if = {
			limit = { FRA = { has_capitulated = yes } }
			add_to_temp_variable = { operation_target@ENG = 500 }
			add_to_temp_variable = { operation_sabotage_fuel@ENG = 10 }
		}
		else = {
			add_to_temp_variable = { operation_target@FRA = 50 }
		}
		if = {
			limit = { has_war_with = SOV }
			add_to_temp_variable = { operation_target@SOV = 500 }
			add_to_temp_variable = { operation_sabotage_oil@SOV = 15 }
			add_to_temp_variable = { operation_sabotage_fuel@SOV = 10 }
		}
	}
	else_if = {
		limit = { tag = SOV }
		if = {
			limit = { has_war_with = GER }
			add_to_temp_variable = { operation_target@GER = 500 }
			add_to_temp_variable = { operation_strengthen_resistance@GER = 10 }
		}
	}
	else_if = {
		limit = { tag = ENG }
		add_to_temp_variable = { operation_target@ITA = 50 }
		if = {
			limit = {
				has_war_with = GER
			}
			add_to_temp_variable = { operation_strengthen_resistance@GER = 2 }
			add_to_temp_variable = { operation_strengthen_society@GER = 2 }
		}
	}
}

ai_score_resistance_areas = {
	for_each_loop = {
		array = intel_networks
		value = resistance_target

		clear_temp_array = intel_resistance_asset_area_score
		clear_temp_array = clear_resistance_area
		for_each_scope_loop = {
			array = intel_resistance_asset_area@var:resistance_target

			if = {
				limit = { has_resistance = yes }
				add_to_temp_array = { intel_resistance_asset_area_score = mtth:resistance_area_score }
			}
			else = {
				add_to_temp_array = { clear_resistance_area = THIS }
			}
		}

		for_each_loop = {
			array = clear_resistance_area
			remove_from_array = { intel_resistance_asset_area@var:resistance_target = v }
			var:v = { clear_variable = resistance_leader }
		}

		clear_temp_array = intel_area_sorted
		clear_temp_array = intel_area_score_sorted
		for_each_loop = {
			array = intel_resistance_asset_area@var:resistance_target
			break = none
			index = entry

			for_each_loop = {
				array = intel_area_score_sorted
				value = s
				if = {
					limit = { check_variable = { intel_resistance_asset_area_score^entry > s } }
					add_to_temp_array = { array = intel_area_sorted index = i value = v }
					add_to_temp_array = { array = intel_area_score_sorted index = i value = intel_resistance_asset_area_score^entry }
					set_temp_variable = { break = 1 }
				}
			}

			if = {
				limit = { check_variable = { break = 0 } }
				add_to_temp_array = { array = intel_area_sorted value = v }
				add_to_temp_array = { array = intel_area_score_sorted value = intel_resistance_asset_area_score^entry }
			}
			else = {
				set_temp_variable = { break = 0 }
			}
		}

		if = {
			limit = { check_variable = { intel_area_score_sorted^0 > 0 } }
			set_variable = { selected_resistance_area@var:resistance_target = intel_area_sorted^0 }
		}
		else = {
			clear_variable = selected_resistance_area@var:resistance_target
		}
	}
}
