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

### Agency

EAI_ESPIONAGE_agency_stop_upgrades = {
	enable = {
		always = yes
	}
	abort = { always = no }
	
	ai_strategy = {
		type = intelligence_agency_usable_factories
		value = -15
	}
}

EAI_ESPIONAGE_build_intelligence_agency = { # Built from script
	enable = {
		NOT = { has_intelligence_agency = yes }
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = intelligence_agency_usable_factories
		value = -500
	}
}

EAI_ESPIONAGE_upgrade_intelligence_agency = {
	enable = {
		date > 1937.1.1

		NOT = { check_variable = { agency_upgrade_number < 5 } }

		OR = {
			date > 1939.1.1
			has_war = yes
		}

		check_variable = { agency_upgrade_number < EAI_ESP_max_agency_upgrades }
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = intelligence_agency_usable_factories
		value = 30
	}
}

### Missions/operations

EAI_ESPIONAGE_counterintelligence_mission = {
	enable = {
		OR = {
			check_variable = { EAI_operation_target = 0 }

			check_variable = { EAI_ESP_available_operatives > 2 }

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

			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
				
			has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }

			NOT = { network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type } }
		}
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = counter_intelligence
		value = 50000
		mission_target = ROOT
		num_operatives = 1
	}
}

EAI_ESPIONAGE_counterintelligence_mission_2 = {
	enable = {
		check_variable = { EAI_ESP_available_operatives > 5 }
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = counter_intelligence
		value = 50000
		mission_target = ROOT
		num_operatives = 1
	}
}

### Keep the network up while running or preparing an op

EAI_ESPIONAGE_quiet_network_mission = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		check_variable = { EAI_ESP_available_operatives > 0 }
		OR = {
			is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }

			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
				
			has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }

			NOT = { network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type } }
		}
	}

	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = quiet_network
		value = 10000
		mission_target = var:EAI_operation_target
		num_operatives = 1
	}

	ai_strategy = {
		type = operative_mission
		mission = build_intel_network
		value = -10000
		mission_target = var:EAI_operation_target
		num_operatives = 1
	}
}

### Build intel network with up to 2 agents

EAI_ESPIONAGE_espionage_build_network_1 = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		check_variable = { EAI_ESP_available_operatives = 1 }
		network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type }
		NOT = {
			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
		}
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = build_intel_network
		value = 10000
		mission_target = var:EAI_operation_target
		num_operatives = 1
	}
}

EAI_ESPIONAGE_espionage_build_network_2 = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		check_variable = { EAI_ESP_available_operatives > 1 }
		network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type }
		NOT = {
			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
		}
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = build_intel_network
		value = 10000
		mission_target = var:EAI_operation_target
		num_operatives = 2
	}
}

EAI_ESPIONAGE_espionage_build_network_heavy_water_raid = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		check_variable = { EAI_operation_type = token:heavy_water_raid }
		check_variable = { EAI_ESP_available_operatives > 1 }
		network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type }
		NOT = {
			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
		}
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = build_intel_network
		value = 10000
		mission_target = var:EAI_operation_target
		num_operatives = 2
		priority = 100
		state = 110
	}
}

EAI_ESPIONAGE_espionage_build_network_warsaw_uprising = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		has_country_flag = EAI_operation_warsaw_uprising_OPERATION_ACTIVE
		check_variable = { EAI_ESP_available_operatives > 1 }
		network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type }
		NOT = {
			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
		}
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_mission
		mission = build_intel_network
		value = 10000
		mission_target = var:EAI_operation_target
		num_operatives = 2
		priority = 100
		state = 10
		state = 87
		state = 98
		state = 90
		state = 92
	}
}

### Start operation

EAI_ESPIONAGE_prepare_espionage_operation = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		OR = {
			NOT = { network_strength = { target = var:EAI_operation_target value < global.EAI_OPERATION_NETWORK_REQUIREMENT^EAI_operation_type } }
			is_preparing_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
			has_finished_collecting_for_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 } }
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_operation
		operation = var:EAI_operation_type
		value = 500000
		operation_target = var:EAI_operation_target
	}

	ai_strategy = {
		type = operation_equipment_priority
		value = 500
	}
}

EAI_ESPIONAGE_run_espionage_operation = {
	enable = {
		NOT = { check_variable = { EAI_operation_target = 0 } }
		NOT = { has_country_flag = EAI_operation_completed }
		has_finished_collecting_for_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type }
	}
	abort_when_not_enabled = yes

	ai_strategy = {
		type = operative_operation
		operation = var:EAI_operation_type
		value = 500000
		operation_target = var:EAI_operation_target
	}
}