############################################################################################################
# 	Expert AI mod - priority construction AI
############################################################################################################

EAI_PC_FORT_strategies = {

	clear_temp_array = _cancel_strategies

	###

	if = { 		limit = { tag = SOV } EAI_PC_FORT_strategy_SOV = yes }
	else_if = { limit = { tag = ENG } EAI_PC_FORT_strategy_ENG = yes }
	else_if = { limit = { tag = USA } EAI_PC_FORT_strategy_USA = yes }
	else_if = { limit = { tag = JAP } EAI_PC_FORT_strategy_JAP = yes }
	else_if = { limit = { tag = GER } EAI_PC_FORT_strategy_GER = yes }
	else_if = { limit = { tag = ITA } EAI_PC_FORT_strategy_ITA = yes }

	###

	EAI_PC_FORT_strategy_GENERIC = yes

	###

	EAI_PC_FORT_cancel_strategies = yes
}

EAI_PC_FORT_cancel_strategies = { # _cancel_strategies

	if = { limit = { check_variable = { _cancel_strategies^num > 0 } }

		for_each_loop = { array = _cancel_strategies value = strategy_type_id
		
			add_to_temp_array = { _cancel_projects_TYPE_IDS = strategy_type_id }
		}

		set_temp_variable = { EAI_PC_cancel_projects_source = 6 }
		EAI_PC_cancel_projects = yes
	}
}

####################################################################################
### GENERIC
####################################################################################

EAI_PC_FORT_strategy_GENERIC = {

	EAI_PC_FORT_strategy_GENERIC_cities = yes

	EAI_PC_FORT_strategy_GENERIC_frontlines = yes

	EAI_PC_FORT_strategy_GENERIC_rivers = yes
}

@EAI_PC_FORT_strategy_GENERIC_cities_TYPE_ID = 500
@EAI_PC_FORT_strategy_GENERIC_cities_capital_PRIO = 2000
@EAI_PC_FORT_strategy_GENERIC_cities_capital_perimeter_PRIO = 1000
@EAI_PC_FORT_strategy_GENERIC_cities_other_PRIO = 100
EAI_PC_FORT_strategy_GENERIC_cities = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_GENERIC_cities_TYPE_ID }

	EAI_PC_FORT_strategy_GENERIC_cities_STRATEGIES = yes

	if = {
		limit = {
			OR = {
				AND = {
					has_country_flag = { flag = EAI_MILITARY_trend_stagnant_war value = 1 days > 180 }
				}
				AND = {
					has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 14 }
					check_variable = { EAI_MILITARY_trend_value > 0.02 }
				}
			}

			check_variable = { EAI_PC_FORT_strategy_GENERIC_cities_STRATEGIES = 1 }
		}

		###################################################
		### Capital state cities
		###################################################

		### Target provinces
			capital_scope = { add_to_temp_array = { _target_states = THIS.id } }
			set_temp_variable = { _get_province_city = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 75 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GENERIC_cities_capital_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 5 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Perimeter around capital state cities
		###################################################

		### Target provinces
			capital_scope = { add_to_temp_array = { _target_states = THIS.id } }
			EAI_PC_FORT_get_city_perimeter_provinces = yes

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 75 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GENERIC_cities_capital_perimeter_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Other victory points
		###################################################

		### Target provinces
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_city = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 75 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###
		
		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GENERIC_cities_other_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}
EAI_PC_FORT_strategy_GENERIC_cities_STRATEGIES = {

	set_temp_variable = { EAI_PC_FORT_strategy_GENERIC_cities_STRATEGIES = 0 }

	if = {
		limit = {

			#####################################
			### 	Strategies
			#####################################

			### JAP

			if = { limit = { tag = JAP }
			
				if = {
					limit = {
						has_war_with = CHI
					}
				
					any_enemy_country = { NOT = { tag = CHI } is_major = yes }
				}
			}

			### SOV

			if = { limit = { tag = SOV }
			
				always = no
			}
		}
	
		set_temp_variable = { EAI_PC_FORT_strategy_GENERIC_cities_STRATEGIES = 1 }
	}
}

@EAI_PC_FORT_strategy_GENERIC_frontlines_TYPE_ID = 501
@EAI_PC_FORT_strategy_GENERIC_frontlines_PRIO = 1
EAI_PC_FORT_strategy_GENERIC_frontlines = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_GENERIC_frontlines_TYPE_ID }

	EAI_PC_FORT_strategy_GENERIC_frontlines_STRATEGIES = yes

	if = {
		limit = {
			has_war = yes

			check_variable = { EAI_PC_FORT_strategy_GENERIC_frontlines_STRATEGIES = 1 }
		}

		###################################################
		### Fortify front lines
		###################################################

		### Target provinces
			EAI_PC_FORT_get_frontline_provinces = yes
			if = { limit = { has_country_flag = EAI_MILITARY_trend_winning_war }
				set_temp_variable = { _limit_provinces_state_avg_control_days = 300 }
			}
			else_if = { limit = { has_country_flag = EAI_MILITARY_trend_stagnant_war }
				if = { limit = { has_country_flag = { flag = EAI_MILITARY_trend_stagnant_war days < 365 } }
					set_temp_variable = { _limit_provinces_state_avg_control_days = 300 }
				}
				else = {
					set_temp_variable = { _limit_provinces_state_avg_control_days = 150 }
				}
			}
			else_if = { limit = { has_country_flag = EAI_MILITARY_trend_losing_war }
				set_temp_variable = { _limit_provinces_state_avg_control_days = 100 }
			}
			else = {
				set_temp_variable = { _limit_provinces_state_avg_control_days = 365 }
			}
			EAI_PC_FORT_limit_provinces_state_avg_control_days = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GENERIC_frontlines_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}
EAI_PC_FORT_strategy_GENERIC_frontlines_STRATEGIES = {

	set_temp_variable = { EAI_PC_FORT_strategy_GENERIC_frontlines_STRATEGIES = 0 }

	if = {
		limit = {

			#####################################
			### 	Strategies
			#####################################

			### JAP

			if = { limit = { tag = JAP }
			
				if = {
					limit = {
						has_war_with = CHI
					}
				
					any_enemy_country = { NOT = { tag = CHI } is_major = yes }
				}
			}
		}
	
		set_temp_variable = { EAI_PC_FORT_strategy_GENERIC_frontlines_STRATEGIES = 1 }
	}
}

@EAI_PC_FORT_strategy_GENERIC_rivers_TYPE_ID = 502
@EAI_PC_FORT_strategy_GENERIC_rivers_PRIO = 100
@EAI_PC_FORT_strategy_GENERIC_bottlenecks_TYPE_ID = 503
@EAI_PC_FORT_strategy_GENERIC_bottlenecks_PRIO = 2000
EAI_PC_FORT_strategy_GENERIC_rivers = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_GENERIC_rivers_TYPE_ID }

	EAI_PC_FORT_strategy_GENERIC_rivers_STRATEGIES = yes

	if = {
		limit = {
			has_war = yes

			OR = {
				AND = {
					has_country_flag = { flag = EAI_MILITARY_trend_stagnant_war value = 1 days > 180 }
				}
				AND = {
					has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 14 }
					check_variable = { EAI_MILITARY_trend_value > 0.02 }
				}
			}

			check_variable = { EAI_PC_FORT_strategy_GENERIC_rivers_STRATEGIES = 1 }
		}

		clear_temp_array = _states
		for_each_loop = { array = controlled_states add_to_temp_array = { _states = v } }
		EAI_PC_FORT_generate_river_fortlines = yes

		###################################################
		### Rivers
		###################################################

			### Target provinces
				clear_temp_array = _target_provinces_
				for_each_loop = { array = river_provinces_ add_to_temp_array = { _target_provinces_ = v } }

				###

				set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
				EAI_PC_FORT_limit_provinces = yes

				EAI_PC_FORT_add_to_valid_fortification_projects = yes
			###

			set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GENERIC_rivers_PRIO }
			set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
			set_temp_variable = { _project_queue_num = 1 }
			set_temp_variable = { _project_queue_max = 2 }
			set_temp_variable = { _project_target_level = 4 }
			set_temp_variable = { _project_type_id = @EAI_PC_FORT_strategy_GENERIC_rivers_TYPE_ID }
			EAI_PC_FORT_create = yes

		###################################################
		### Bottlenecks
		###################################################

			### Target provinces
				clear_temp_array = _target_provinces_
				for_each_loop = { array = bottleneck_provinces_ add_to_temp_array = { _target_provinces_ = v } }

				###

				EAI_PC_FORT_add_to_valid_fortification_projects = yes
			###

			set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GENERIC_bottlenecks_PRIO }
			set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
			set_temp_variable = { _project_queue_num = 1 }
			set_temp_variable = { _project_queue_max = 2 }
			set_temp_variable = { _project_target_level = 4 }
			set_temp_variable = { _project_type_id = @EAI_PC_FORT_strategy_GENERIC_bottlenecks_TYPE_ID }
			EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { 
		add_to_temp_array = { _cancel_strategies = @EAI_PC_FORT_strategy_GENERIC_rivers_TYPE_ID }
		add_to_temp_array = { _cancel_strategies = @EAI_PC_FORT_strategy_GENERIC_bottlenecks_TYPE_ID }
	}
}
EAI_PC_FORT_strategy_GENERIC_rivers_STRATEGIES = {

	set_temp_variable = { EAI_PC_FORT_strategy_GENERIC_rivers_STRATEGIES = 0 }

	if = {
		limit = {

			#####################################
			### 	Strategies
			#####################################

			### JAP

			if = { limit = { tag = JAP }
			
				if = {
					limit = {
						has_war_with = CHI
					}
				
					any_enemy_country = { NOT = { tag = CHI } is_major = yes }
				}
			}
		}
	
		set_temp_variable = { EAI_PC_FORT_strategy_GENERIC_rivers_STRATEGIES = 1 }
	}
}

####################################################################################
### SOV
####################################################################################

EAI_PC_FORT_strategy_SOV = {

	EAI_PC_FORT_strategy_SOV_main_line = yes

	EAI_PC_FORT_strategy_SOV_main_cities_last_stand = yes

	EAI_PC_FORT_strategy_SOV_defense_in_depth = yes
}

@EAI_PC_FORT_strategy_SOV_main_line_TYPE_ID = 400
@EAI_PC_FORT_strategy_SOV_main_line_PRIO = 600
EAI_PC_FORT_strategy_SOV_main_line = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_SOV_main_line_TYPE_ID }

	if = {
		limit = {
			date > 1941.1.1
			OR = {
				has_war_with = GER
				AND = {
					any_enemy_country = { is_major = yes }
					surrender_progress > 0.05
				}
			}
		}

		###################################################
		### Main line
		###################################################

		### Target provinces
			add_to_temp_array = { _target_provinces_ = 721 }
			add_to_temp_array = { _target_provinces_ = 737 }
			add_to_temp_array = { _target_provinces_ = 3573 }
			add_to_temp_array = { _target_provinces_ = 588 }
			add_to_temp_array = { _target_provinces_ = 11405 }
			add_to_temp_array = { _target_provinces_ = 453 }
			add_to_temp_array = { _target_provinces_ = 9463 }
			add_to_temp_array = { _target_provinces_ = 6507 }
			add_to_temp_array = { _target_provinces_ = 6495 }
			add_to_temp_array = { _target_provinces_ = 9491 }
			add_to_temp_array = { _target_provinces_ = 11530 }
			add_to_temp_array = { _target_provinces_ = 9543 }
			add_to_temp_array = { _target_provinces_ = 11557 }
			add_to_temp_array = { _target_provinces_ = 489 }
			add_to_temp_array = { _target_provinces_ = 3568 }
			add_to_temp_array = { _target_provinces_ = 546 }
			add_to_temp_array = { _target_provinces_ = 9288 }
			add_to_temp_array = { _target_provinces_ = 6344 }
			add_to_temp_array = { _target_provinces_ = 6233 }
			add_to_temp_array = { _target_provinces_ = 11271 }
			add_to_temp_array = { _target_provinces_ = 6354 }
			add_to_temp_array = { _target_provinces_ = 11220 }
			add_to_temp_array = { _target_provinces_ = 11241 }
			add_to_temp_array = { _target_provinces_ = 9323 }
			add_to_temp_array = { _target_provinces_ = 6249 }
			add_to_temp_array = { _target_provinces_ = 6326 }
			add_to_temp_array = { _target_provinces_ = 3219 }
			add_to_temp_array = { _target_provinces_ = 6459 }
			add_to_temp_array = { _target_provinces_ = 310 }
			add_to_temp_array = { _target_provinces_ = 7482 }
			add_to_temp_array = { _target_provinces_ = 10323 }
			add_to_temp_array = { _target_provinces_ = 9340 }

			###

			# set_temp_variable = { _limit_provinces_near_enemies_radius = 100 }
			# EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_SOV_main_line_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_SOV_main_cities_last_stand_TYPE_ID = 401
@EAI_PC_FORT_strategy_SOV_main_cities_last_stand_PRIO = 5000
@EAI_PC_FORT_strategy_SOV_main_cities_last_stand_perimeter_PRIO = 2500
EAI_PC_FORT_strategy_SOV_main_cities_last_stand = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_SOV_main_cities_last_stand_TYPE_ID }

	if = {
		limit = {
			OR = {
				has_war_with = GER
				AND = {
					any_enemy_country = { is_major = yes }
					surrender_progress > 0.05
					has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 60 }
				}
			}
		}

		###################################################
		### Big cities
		###################################################

		### Leningrad
			add_to_temp_array = { _target_provinces_ = 149 }
			add_to_temp_array = { _target_provinces_ = 3151 }
		### Moscow
			add_to_temp_array = { _target_provinces_ = 11330 }
			add_to_temp_array = { _target_provinces_ = 3290 }
			add_to_temp_array = { _target_provinces_ = 301 }
			add_to_temp_array = { _target_provinces_ = 6380 }
			add_to_temp_array = { _target_provinces_ = 3259 }
			add_to_temp_array = { _target_provinces_ = 344 }
			add_to_temp_array = { _target_provinces_ = 9256 }
			add_to_temp_array = { _target_provinces_ = 6245 }
		### Stalingrad
			add_to_temp_array = { _target_provinces_ = 6503 }
			add_to_temp_array = { _target_provinces_ = 9474 }
			add_to_temp_array = { _target_provinces_ = 6527 }
			add_to_temp_array = { _target_provinces_ = 9504 }
			add_to_temp_array = { _target_provinces_ = 3582 }
			add_to_temp_array = { _target_provinces_ = 9436 }
			add_to_temp_array = { _target_provinces_ = 3529 }
		### Crimea
			add_to_temp_array = { _target_provinces_ = 3722 }
			add_to_temp_array = { _target_provinces_ = 11649 }
			add_to_temp_array = { _target_provinces_ = 9718 }
			add_to_temp_array = { _target_provinces_ = 6724 }
			add_to_temp_array = { _target_provinces_ = 705 }
			add_to_temp_array = { _target_provinces_ = 3686 }
			add_to_temp_array = { _target_provinces_ = 6704 }
			add_to_temp_array = { _target_provinces_ = 6741 }
			
			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 50 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_SOV_main_cities_last_stand_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Perimeter around capital state cities
		###################################################

		### Target provinces
			clear_temp_array = _target_states
			capital_scope = { add_to_temp_array = { _target_states = THIS.id } }
			217 = { add_to_temp_array = { _target_states = THIS.id } }
			195 = { add_to_temp_array = { _target_states = THIS.id } }
			EAI_PC_FORT_get_city_perimeter_provinces = yes

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 50 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_SOV_main_cities_last_stand_perimeter_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_SOV_defense_in_depth_TYPE_ID = 402
@EAI_PC_FORT_strategy_SOV_defense_in_depth_PRIO = 500
EAI_PC_FORT_strategy_SOV_defense_in_depth = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_SOV_defense_in_depth_TYPE_ID }

	if = {
		limit = {
			OR = {
				has_war_with = GER
				AND = {
					any_enemy_country = { is_major = yes }
					surrender_progress > 0.05
					has_country_flag = EAI_MILITARY_trend_losing_war
				}
			}
		}

		clear_temp_array = _states
		for_each_loop = { array = controlled_states if = { limit = { is_in_array = { global.EAI_MAP_barbarossa_theatre = v } } add_to_temp_array = { _states = v } } }
		EAI_PC_FORT_generate_river_fortlines = yes

		###################################################
		### Defense in depth fort lines
		###################################################

		### Target provinces
			clear_temp_array = _target_provinces_
			for_each_loop = { array = river_provinces_ add_to_temp_array = { _target_provinces_ = v } }
			for_each_loop = { array = bottleneck_provinces_ add_to_temp_array = { _target_provinces_ = v } }

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 100 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_SOV_defense_in_depth_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

####################################################################################
### GER
####################################################################################

EAI_PC_FORT_strategy_GER = {

	EAI_PC_FORT_strategy_GER_fortlines = yes
}

@EAI_PC_FORT_strategy_GER_fortlines_TYPE_ID = 401
@EAI_PC_FORT_strategy_GER_fortlines_PRIO = 500
EAI_PC_FORT_strategy_GER_fortlines = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_GER_fortlines_TYPE_ID }

	if = {
		limit = {
			has_country_flag = { flag = EAI_MILITARY_trend_losing_war value = 1 days > 30 }
		}

		###################################################
		### Defensive fort lines
		###################################################

		### Target provinces
			add_to_temp_array = { _target_provinces_ = 6282 }
			add_to_temp_array = { _target_provinces_ = 3207 }
			add_to_temp_array = { _target_provinces_ = 9496 }
			add_to_temp_array = { _target_provinces_ = 3572 }
			add_to_temp_array = { _target_provinces_ = 6595 }
			add_to_temp_array = { _target_provinces_ = 3367 }
			add_to_temp_array = { _target_provinces_ = 11444 }
			add_to_temp_array = { _target_provinces_ = 6521 }
			add_to_temp_array = { _target_provinces_ = 3499 }
			add_to_temp_array = { _target_provinces_ = 6441 }
			add_to_temp_array = { _target_provinces_ = 514 }
			add_to_temp_array = { _target_provinces_ = 3512 }
			add_to_temp_array = { _target_provinces_ = 6469 }
			add_to_temp_array = { _target_provinces_ = 6570 }
			add_to_temp_array = { _target_provinces_ = 529 }
			add_to_temp_array = { _target_provinces_ = 6444 }
			add_to_temp_array = { _target_provinces_ = 3326 }
			add_to_temp_array = { _target_provinces_ = 3271 }
			add_to_temp_array = { _target_provinces_ = 3395 }
			add_to_temp_array = { _target_provinces_ = 11493 }
			add_to_temp_array = { _target_provinces_ = 6513 }
			add_to_temp_array = { _target_provinces_ = 3561 }
			add_to_temp_array = { _target_provinces_ = 362 }
			add_to_temp_array = { _target_provinces_ = 389 }
			add_to_temp_array = { _target_provinces_ = 3324 }
			add_to_temp_array = { _target_provinces_ = 279 }
			add_to_temp_array = { _target_provinces_ = 243 }
			add_to_temp_array = { _target_provinces_ = 6401 }
			add_to_temp_array = { _target_provinces_ = 548 }
			add_to_temp_array = { _target_provinces_ = 11492 }
			add_to_temp_array = { _target_provinces_ = 3544 }
			add_to_temp_array = { _target_provinces_ = 6511 }
			add_to_temp_array = { _target_provinces_ = 3458 }
			add_to_temp_array = { _target_provinces_ = 562 }
			add_to_temp_array = { _target_provinces_ = 11219 }
			add_to_temp_array = { _target_provinces_ = 375 }
			add_to_temp_array = { _target_provinces_ = 9456 }
			add_to_temp_array = { _target_provinces_ = 11499 }
			add_to_temp_array = { _target_provinces_ = 6540 }
			add_to_temp_array = { _target_provinces_ = 3299 }
			add_to_temp_array = { _target_provinces_ = 3541 }
			add_to_temp_array = { _target_provinces_ = 532 }
			add_to_temp_array = { _target_provinces_ = 6282 }
			add_to_temp_array = { _target_provinces_ = 3207 }
			add_to_temp_array = { _target_provinces_ = 9496 }
			add_to_temp_array = { _target_provinces_ = 3572 }
			add_to_temp_array = { _target_provinces_ = 6595 }
			add_to_temp_array = { _target_provinces_ = 3367 }
			add_to_temp_array = { _target_provinces_ = 11444 }
			add_to_temp_array = { _target_provinces_ = 6521 }
			add_to_temp_array = { _target_provinces_ = 3499 }
			add_to_temp_array = { _target_provinces_ = 6441 }
			add_to_temp_array = { _target_provinces_ = 514 }
			add_to_temp_array = { _target_provinces_ = 3512 }
			add_to_temp_array = { _target_provinces_ = 6469 }
			add_to_temp_array = { _target_provinces_ = 6570 }
			add_to_temp_array = { _target_provinces_ = 529 }
			add_to_temp_array = { _target_provinces_ = 6444 }
			add_to_temp_array = { _target_provinces_ = 3326 }
			add_to_temp_array = { _target_provinces_ = 3271 }
			add_to_temp_array = { _target_provinces_ = 3395 }
			add_to_temp_array = { _target_provinces_ = 11493 }
			add_to_temp_array = { _target_provinces_ = 6513 }
			add_to_temp_array = { _target_provinces_ = 3561 }
			add_to_temp_array = { _target_provinces_ = 362 }
			add_to_temp_array = { _target_provinces_ = 389 }
			add_to_temp_array = { _target_provinces_ = 3324 }
			add_to_temp_array = { _target_provinces_ = 279 }
			add_to_temp_array = { _target_provinces_ = 243 }
			add_to_temp_array = { _target_provinces_ = 6401 }
			add_to_temp_array = { _target_provinces_ = 548 }
			add_to_temp_array = { _target_provinces_ = 11492 }
			add_to_temp_array = { _target_provinces_ = 3544 }
			add_to_temp_array = { _target_provinces_ = 6511 }
			add_to_temp_array = { _target_provinces_ = 3458 }
			add_to_temp_array = { _target_provinces_ = 562 }
			add_to_temp_array = { _target_provinces_ = 11219 }
			add_to_temp_array = { _target_provinces_ = 375 }
			add_to_temp_array = { _target_provinces_ = 9456 }
			add_to_temp_array = { _target_provinces_ = 11499 }
			add_to_temp_array = { _target_provinces_ = 6540 }
			add_to_temp_array = { _target_provinces_ = 3299 }
			add_to_temp_array = { _target_provinces_ = 3541 }
			add_to_temp_array = { _target_provinces_ = 532 }

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 50 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_GER_fortlines_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

####################################################################################
### ITA
####################################################################################

EAI_PC_FORT_strategy_ITA = {

	EAI_PC_FORT_strategy_ITA_fortlines = yes

	EAI_PC_FORT_strategy_ITA_coastal = yes
}

@EAI_PC_FORT_strategy_ITA_fortlines_TYPE_ID = 400
@EAI_PC_FORT_strategy_ITA_fortlines_PRIO = 1000
EAI_PC_FORT_strategy_ITA_fortlines = {
	
	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_ITA_fortlines_TYPE_ID }

	if = {
		limit = {
			any_owned_state = {
				region = 23
				OR = {
					CONTROLLER = { has_war_with = ROOT }
					ROOT = { 
						controls_state = PREV
						NOT = { has_full_control_of_state = PREV } 
					}
				}
			}
		}

		###################################################
		### Defensive fort lines
		###################################################

		### Target provinces
			add_to_temp_array = { _target_provinces_ = 9990 }
			add_to_temp_array = { _target_provinces_ = 11846 }
			add_to_temp_array = { _target_provinces_ = 11882 }
			add_to_temp_array = { _target_provinces_ = 3910 }
			add_to_temp_array = { _target_provinces_ = 9838 }
			add_to_temp_array = { _target_provinces_ = 955 }
			add_to_temp_array = { _target_provinces_ = 11803 }
			add_to_temp_array = { _target_provinces_ = 883 }
			add_to_temp_array = { _target_provinces_ = 9582 }
			add_to_temp_array = { _target_provinces_ = 3776 }
			add_to_temp_array = { _target_provinces_ = 773 }
			add_to_temp_array = { _target_provinces_ = 3780 }
			add_to_temp_array = { _target_provinces_ = 776 }
			add_to_temp_array = { _target_provinces_ = 6946 }
			add_to_temp_array = { _target_provinces_ = 9907 }
			add_to_temp_array = { _target_provinces_ = 9879 }
			add_to_temp_array = { _target_provinces_ = 11861 }
			add_to_temp_array = { _target_provinces_ = 967 }
			add_to_temp_array = { _target_provinces_ = 11891 }
			add_to_temp_array = { _target_provinces_ = 11721 }
			add_to_temp_array = { _target_provinces_ = 9738 }
			add_to_temp_array = { _target_provinces_ = 6780 }
			add_to_temp_array = { _target_provinces_ = 611 }

			###

			set_temp_variable = { _limit_provinces_near_enemies_radius = 50 }
			EAI_PC_FORT_limit_near_enemies_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ITA_fortlines_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_ITA_coastal_TYPE_ID = 401
@EAI_PC_FORT_strategy_ITA_coastal_naval_base_PRIO = 500
@EAI_PC_FORT_strategy_ITA_coastal_coast_PRIO = 50
EAI_PC_FORT_strategy_ITA_coastal = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_ITA_coastal_TYPE_ID }

	if = {
		limit = {
			has_war = yes
			any_enemy_country = { is_major = yes }
			OR = {
				alliance_naval_strength_ratio < 1.0
				check_variable = { EAI_num_surface_ships < 25 }
			}
		}

		###################################################
		### Naval bases
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ITA_coastal_naval_base_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Coast
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_coast = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ITA_coastal_coast_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 2 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

####################################################################################
### JAP
####################################################################################

EAI_PC_FORT_strategy_JAP = {

	EAI_PC_FORT_strategy_JAP_islands = yes

	EAI_PC_FORT_strategy_JAP_coastal = yes

	EAI_PC_FORT_strategy_JAP_frontlines = yes
}

@EAI_PC_FORT_strategy_JAP_islands_TYPE_ID = 400
@EAI_PC_FORT_strategy_JAP_islands_asia_PRIO = 100
@EAI_PC_FORT_strategy_JAP_islands_island_PRIO = 0
EAI_PC_FORT_strategy_JAP_islands = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_JAP_islands_TYPE_ID }
	
	if = {
		limit = {
			threat > 0.9
			OR = {
				USA = { EAI_THIS_is_threat_to_PREV = yes }
				AND = {
					any_enemy_country = { is_major = yes }
					alliance_naval_strength_ratio < 2
				}
			}
			OR = {
				NOT = { CHI = { EAI_THIS_is_threat_to_PREV = yes } }
				CHI = { surrender_progress > 0.7 }
				AND = {
					any_enemy_country = { is_major = yes }
					alliance_naval_strength_ratio < 2
				}
			}
		}

		###################################################
		### Asia
		###################################################

		### Target provinces
			for_each_scope_loop = { array = controlled_states

				if = { 
					limit = { 
						impassable = no

						is_on_continent = asia
						EAI_is_island_state = yes
					} 

					add_to_temp_array = { _target_states = THIS.id } 
				}
			}

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_JAP_islands_asia_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Other
		###################################################

		### Target provinces
			for_each_scope_loop = { array = controlled_states

				if = { 
					limit = { 
						impassable = no

						NOT = { is_on_continent = asia }
						EAI_is_island_state = yes
					} 

					add_to_temp_array = { _target_states = THIS.id } 
				}
			}

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_JAP_islands_island_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_JAP_coastal_TYPE_ID = 401
@EAI_PC_FORT_strategy_JAP_coastal_naval_base_PRIO = 1800
@EAI_PC_FORT_strategy_JAP_coastal_coast_PRIO = 250
EAI_PC_FORT_strategy_JAP_coastal = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_JAP_coastal_TYPE_ID }
	
	if = {
		limit = {
			has_war = yes
			OR = {
				has_country_flag = EAI_JAP_homeland_invaded
				alliance_naval_strength_ratio < 1.0
			}
		}

		###################################################
		### Naval bases
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_JAP_coastal_naval_base_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Coast
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_coast = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_JAP_coastal_coast_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 2 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_JAP_frontlines_TYPE_ID = 402
@EAI_PC_FORT_strategy_JAP_frontlines_PRIO = 2000
EAI_PC_FORT_strategy_JAP_frontlines = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_JAP_frontlines_TYPE_ID }

	if = {
		limit = {
			any_owned_state = {
				is_in_home_area = yes
				OR = {
					CONTROLLER = { has_war_with = ROOT }
					ROOT = { 
						controls_state = PREV
						NOT = { has_full_control_of_state = PREV } 
					}
				}
			}
		}

		###################################################
		### Frontlines
		###################################################

		### Target provinces
			EAI_PC_FORT_get_frontline_provinces = yes

			###

			set_temp_variable = { _get_province_home_area = 1 }
			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_JAP_frontlines_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

####################################################################################
### USA
####################################################################################

EAI_PC_FORT_strategy_USA = {

	EAI_PC_FORT_strategy_USA_fortlines = yes

	EAI_PC_FORT_strategy_USA_home_coast = yes

	EAI_PC_FORT_strategy_USA_islands = yes
}

@EAI_PC_FORT_strategy_USA_fortlines_TYPE_ID = 400
@EAI_PC_FORT_strategy_USA_fortlines_city_PRIO = 2000
@EAI_PC_FORT_strategy_USA_fortlines_fortline_PRIO = 1000
EAI_PC_FORT_strategy_USA_fortlines = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_USA_fortlines_TYPE_ID }

	if = {
		limit = {
			threat > 0.9
			any_enemy_country = { is_major = yes }
			OR = {
				alliance_naval_strength_ratio < 1.0
				AND = {
					is_in_faction_with = ENG
					ENG = { has_capitulated = yes }
				}
			}

			any_owned_state = {
				is_in_home_area = yes
				OR = {
					CONTROLLER = { has_war_with = ROOT }
					ROOT = { 
						controls_state = PREV
						NOT = { has_full_control_of_state = PREV } 
					}
				}
			}

			has_country_flag = EAI_MILITARY_trend_losing_war
		}

		###################################################
		### Cities
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_city = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			set_temp_variable = { _limit_provinces_near_enemies_radius = 100 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_USA_fortlines_city_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Fort lines
		###################################################

		### Target provinces
			add_to_temp_array = { _target_provinces_ = 6984 } add_to_temp_array = { _target_provinces_ = 822 } add_to_temp_array = { _target_provinces_ = 3847 } add_to_temp_array = { _target_provinces_ = 9804 } add_to_temp_array = { _target_provinces_ = 6885 }
			add_to_temp_array = { _target_provinces_ = 865 } add_to_temp_array = { _target_provinces_ = 3823 } add_to_temp_array = { _target_provinces_ = 10343 }
			add_to_temp_array = { _target_provinces_ = 6882 } add_to_temp_array = { _target_provinces_ = 3894 } add_to_temp_array = { _target_provinces_ = 9774 } add_to_temp_array = { _target_provinces_ = 9673 } add_to_temp_array = { _target_provinces_ = 9693 }
			add_to_temp_array = { _target_provinces_ = 11927 } add_to_temp_array = { _target_provinces_ = 7083 } add_to_temp_array = { _target_provinces_ = 10081 } add_to_temp_array = { _target_provinces_ = 12445 } add_to_temp_array = { _target_provinces_ = 10468 }
			add_to_temp_array = { _target_provinces_ = 1547 } add_to_temp_array = { _target_provinces_ = 3994 } add_to_temp_array = { _target_provinces_ = 1485 }
			add_to_temp_array = { _target_provinces_ = 10407 } add_to_temp_array = { _target_provinces_ = 12325 } add_to_temp_array = { _target_provinces_ = 12498 } add_to_temp_array = { _target_provinces_ = 7583 }
			add_to_temp_array = { _target_provinces_ = 4507 } add_to_temp_array = { _target_provinces_ = 1643 } add_to_temp_array = { _target_provinces_ = 7599 } add_to_temp_array = { _target_provinces_ = 1559 } add_to_temp_array = { _target_provinces_ = 10355 } add_to_temp_array = { _target_provinces_ = 1646 } add_to_temp_array = { _target_provinces_ = 4625 } add_to_temp_array = { _target_provinces_ = 4540 } add_to_temp_array = { _target_provinces_ = 4449 } add_to_temp_array = { _target_provinces_ = 4569 } add_to_temp_array = { _target_provinces_ = 1523 } add_to_temp_array = { _target_provinces_ = 7607 } add_to_temp_array = { _target_provinces_ = 10391 } add_to_temp_array = { _target_provinces_ = 10333 } add_to_temp_array = { _target_provinces_ = 9892 } add_to_temp_array = { _target_provinces_ = 10666 } add_to_temp_array = { _target_provinces_ = 12646 } add_to_temp_array = { _target_provinces_ = 10595 }
			add_to_temp_array = { _target_provinces_ = 12410 } add_to_temp_array = { _target_provinces_ = 12341 } add_to_temp_array = { _target_provinces_ = 5022 }
			add_to_temp_array = { _target_provinces_ = 7599 } add_to_temp_array = { _target_provinces_ = 7555 } add_to_temp_array = { _target_provinces_ = 4522 } add_to_temp_array = { _target_provinces_ = 1503 } add_to_temp_array = { _target_provinces_ = 7485 } add_to_temp_array = { _target_provinces_ = 7945 } add_to_temp_array = { _target_provinces_ = 8025 } add_to_temp_array = { _target_provinces_ = 11743 } add_to_temp_array = { _target_provinces_ = 4919 }
			add_to_temp_array = { _target_provinces_ = 4444 } add_to_temp_array = { _target_provinces_ = 10477 } add_to_temp_array = { _target_provinces_ = 4597 } add_to_temp_array = { _target_provinces_ = 4567 } add_to_temp_array = { _target_provinces_ = 2043 } add_to_temp_array = { _target_provinces_ = 1806 } add_to_temp_array = { _target_provinces_ = 4811 } add_to_temp_array = { _target_provinces_ = 7892 } add_to_temp_array = { _target_provinces_ = 12024 } add_to_temp_array = { _target_provinces_ = 4814 }
			add_to_temp_array = { _target_provinces_ = 4442 } add_to_temp_array = { _target_provinces_ = 9808 } add_to_temp_array = { _target_provinces_ = 3829 } add_to_temp_array = { _target_provinces_ = 7528 } add_to_temp_array = { _target_provinces_ = 971 } add_to_temp_array = { _target_provinces_ = 11890 } add_to_temp_array = { _target_provinces_ = 4601 } add_to_temp_array = { _target_provinces_ = 4527 } add_to_temp_array = { _target_provinces_ = 1881 } add_to_temp_array = { _target_provinces_ = 11621 } add_to_temp_array = { _target_provinces_ = 11635 } add_to_temp_array = { _target_provinces_ = 954 }
			add_to_temp_array = { _target_provinces_ = 5090 } add_to_temp_array = { _target_provinces_ = 9967 } add_to_temp_array = { _target_provinces_ = 7643 } add_to_temp_array = { _target_provinces_ = 8014 } add_to_temp_array = { _target_provinces_ = 7558 } add_to_temp_array = { _target_provinces_ = 7466 } add_to_temp_array = { _target_provinces_ = 7646 } add_to_temp_array = { _target_provinces_ = 3941 } add_to_temp_array = { _target_provinces_ = 6958 } add_to_temp_array = { _target_provinces_ = 6823 } add_to_temp_array = { _target_provinces_ = 11769 }
			add_to_temp_array = { _target_provinces_ = 850 } add_to_temp_array = { _target_provinces_ = 12765 } add_to_temp_array = { _target_provinces_ = 7747 } add_to_temp_array = { _target_provinces_ = 4796 } add_to_temp_array = { _target_provinces_ = 10914 }
			add_to_temp_array = { _target_provinces_ = 12627 } add_to_temp_array = { _target_provinces_ = 10642 } add_to_temp_array = { _target_provinces_ = 4975 } add_to_temp_array = { _target_provinces_ = 3883 } add_to_temp_array = { _target_provinces_ = 12791 }
			add_to_temp_array = { _target_provinces_ = 12553 } add_to_temp_array = { _target_provinces_ = 10687 } add_to_temp_array = { _target_provinces_ = 7865 } add_to_temp_array = { _target_provinces_ = 12183 } add_to_temp_array = { _target_provinces_ = 3513 } add_to_temp_array = { _target_provinces_ = 4328 }
			add_to_temp_array = { _target_provinces_ = 12633 } add_to_temp_array = { _target_provinces_ = 10535 } add_to_temp_array = { _target_provinces_ = 1756 } add_to_temp_array = { _target_provinces_ = 7764 } add_to_temp_array = { _target_provinces_ = 7818 } add_to_temp_array = { _target_provinces_ = 7821 } add_to_temp_array = { _target_provinces_ = 12765 } add_to_temp_array = { _target_provinces_ = 11814 } add_to_temp_array = { _target_provinces_ = 8115 }

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			set_temp_variable = { _limit_provinces_near_enemies_radius = 100 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_USA_fortlines_fortline_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_USA_coastal_TYPE_ID = 401
@EAI_PC_FORT_strategy_USA_coastal_naval_base_PRIO = 750
EAI_PC_FORT_strategy_USA_home_coast = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_USA_coastal_TYPE_ID }

	if = {
		limit = {
			threat > 0.9
			any_enemy_country = { is_major = yes }
			OR = {
				alliance_naval_strength_ratio < 1.0
				AND = {
					is_in_faction_with = ENG
					ENG = { has_capitulated = yes }
				}
			}
		}

		###################################################
		### Naval bases
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_USA_coastal_naval_base_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_USA_islands_TYPE_ID = 402
@EAI_PC_FORT_strategy_USA_islands_asia_PRIO = 300
@EAI_PC_FORT_strategy_USA_islands_island_PRIO = 250
EAI_PC_FORT_strategy_USA_islands = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_USA_islands_TYPE_ID }

	if = {
		limit = {
			threat > 0.9
			OR = {
				JAP = { EAI_THIS_is_threat_to_PREV = yes }
				AND = {
					any_enemy_country = { is_major = yes }
					alliance_naval_strength_ratio < 2
				}
			}
		}

		###################################################
		### Asia
		###################################################

		### Target provinces
			for_each_scope_loop = { array = controlled_states

				if = { 
					limit = { 
						impassable = no

						is_on_continent = asia
						EAI_is_island_state = yes
					} 

					add_to_temp_array = { _target_states = THIS.id } 
				}
			}

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_USA_islands_asia_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Other
		###################################################

		### Target provinces
			for_each_scope_loop = { array = controlled_states

				if = { 
					limit = { 
						impassable = no

						NOT = { is_on_continent = asia }
						EAI_is_island_state = yes
					} 

					add_to_temp_array = { _target_states = THIS.id } 
				}
			}

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_USA_islands_island_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

####################################################################################
### ENG
####################################################################################

EAI_PC_FORT_strategy_ENG = {

	EAI_PC_FORT_strategy_ENG_coastal = yes

	EAI_PC_FORT_strategy_ENG_cities = yes

	EAI_PC_FORT_strategy_ENG_islands = yes
}

@EAI_PC_FORT_strategy_ENG_coastal_TYPE_ID = 400
@EAI_PC_FORT_strategy_ENG_coastal_naval_base_PRIO = 500
@EAI_PC_FORT_strategy_ENG_coastal_coast_PRIO = 250
EAI_PC_FORT_strategy_ENG_coastal = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_ENG_coastal_TYPE_ID }

	if = {
		limit = {
			threat > 0.9
			any_enemy_country = { is_major = yes }
		}

		###################################################
		### Naval bases
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ENG_coastal_naval_base_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Coast
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_coast = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ENG_coastal_coast_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 2 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_ENG_cities_TYPE_ID = 401
@EAI_PC_FORT_strategy_ENG_cities_PRIO = 1700
EAI_PC_FORT_strategy_ENG_cities = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_ENG_cities_TYPE_ID }

	if = {
		limit = {
			threat > 0.9

			any_enemy_country = { is_major = yes }

			OR = {
				alliance_naval_strength_ratio < 0.5

				any_owned_state = {
					is_in_home_area = yes
					OR = {
						CONTROLLER = { has_war_with = ROOT }
						ROOT = { 
							controls_state = PREV
							NOT = { has_full_control_of_state = PREV } 
						}
					}
				}
			}

			has_country_flag = EAI_MILITARY_trend_losing_war
		}

		###################################################
		### Cities
		###################################################

		### Target provinces
			set_temp_variable = { _get_state_home_area = 1 }
			set_temp_variable = { _get_state_coastal = 1 }
			EAI_PC_FORT_get_target_states = yes

			set_temp_variable = { _get_province_city = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			set_temp_variable = { _limit_provinces_near_enemies_radius = 100 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ENG_cities_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_BUNKER }
		set_temp_variable = { _project_queue_num = 3 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 4 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

@EAI_PC_FORT_strategy_ENG_islands_TYPE_ID = 402
@EAI_PC_FORT_strategy_ENG_islands_europe_PRIO = 1700
@EAI_PC_FORT_strategy_ENG_islands_island_PRIO = 0
EAI_PC_FORT_strategy_ENG_islands = {

	set_temp_variable = { strategy_type_id = @EAI_PC_FORT_strategy_ENG_islands_TYPE_ID }

	if = {
		limit = {
			threat > 0.9
			AND = {
				any_enemy_country = { is_major = yes }
				alliance_naval_strength_ratio < 2
			}
		}

		###################################################
		### Europe
		###################################################

		### Target provinces
			for_each_scope_loop = { array = controlled_states

				if = { 
					limit = { 
						impassable = no

						is_on_continent = europe
						EAI_is_island_state = yes
					} 

					add_to_temp_array = { _target_states = THIS.id } 
				}
			}

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ENG_islands_europe_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 2 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################
		### Other
		###################################################

		### Target provinces
			for_each_scope_loop = { array = controlled_states

				if = { 
					limit = { 
						impassable = no

						NOT = { is_on_continent = europe }
						EAI_is_island_state = yes
					} 

					add_to_temp_array = { _target_states = THIS.id } 
				}
			}

			set_temp_variable = { _get_province_naval_base = 1 }
			EAI_PC_FORT_get_target_provinces = yes

			###

			set_temp_variable = { _limit_provinces_queue_one_per_target = 1 }
			EAI_PC_FORT_limit_provinces = yes

			EAI_PC_FORT_add_to_valid_fortification_projects = yes
		###

		set_temp_variable = { _project_priority = @EAI_PC_FORT_strategy_ENG_islands_island_PRIO }
		set_temp_variable = { _project_building_type = global.EAI_PC_CBUNKER }
		set_temp_variable = { _project_queue_num = 1 }
		set_temp_variable = { _project_queue_max = 2 }
		set_temp_variable = { _project_target_level = 3 }
		set_temp_variable = { _project_type_id = strategy_type_id }
		EAI_PC_FORT_create = yes

		###################################################

		EAI_PC_FORT_cancel_invalid_fortification_projects = yes
	}
	else = { add_to_temp_array = { _cancel_strategies = strategy_type_id } }
}

####################################################################################

EAI_PC_FORT_create = { # _target_provinces_, [project vars]

	if = { limit = { has_country_flag = EAI_PC_FORT_logging has_country_flag = EAI_priority_construction_detailed_logging }
	
		for_each_loop = { array = _target_provinces_
		
			log = "building fort in province=[?v]" 
		}
	}

	EAI_PC_save_project_inputs = yes

	if = { limit = { check_variable = { _target_provinces_^num > 0 } }
	
		if = { limit = { check_variable = { _project_queue_num > 0 } } resize_temp_array = { sets = _project_queue_num } }
		else = { resize_temp_array = { sets = 1 } }
		for_each_loop = { array = sets index = sets_i value = sets_v break = sets_b

			for_each_loop = { array = _target_provinces_ index = province_index value = _project_province_id break = b3

				EAI_PC_get_project_inputs_provincial = yes
				set_temp_variable = { _project_queue_num = 0 }
				subtract_from_temp_variable = { _project_priority = sets_i }
				var:global.EAI_MAP_province_state_id^_project_province_id = { EAI_PC_start_project = yes }
			}
		}
	}

	clear_temp_array = _target_provinces_
	
	EAI_PC_clear_project_inputs = yes
}

###

EAI_PC_FORT_get_target_states = { # _get_state_coastal, _get_state_home_area, _get_state_island

	clear_temp_array = _target_states
	for_each_scope_loop = { array = controlled_states
	
		if = { 
			limit = { 
				impassable = no
				
				if = { limit = { check_variable = { _get_state_home_area = 1 } } is_in_home_area = yes }
				if = { limit = { check_variable = { _get_state_coastal = 1 } } is_coastal = yes }
				if = { limit = { check_variable = { _get_state_island = 1 } } EAI_is_island_state = yes }
			} 

			add_to_temp_array = { _target_states = THIS.id } 
		}
	}

	### Clear input

	set_temp_variable = { _get_state_home_area = 0 }
	set_temp_variable = { _get_state_coastal = 0 }
	set_temp_variable = { _get_state_island = 0 }
}

EAI_PC_FORT_get_city_perimeter_provinces = { # _target_states

	clear_temp_array = perimeter_states
	clear_temp_array = perimeter_cities

	for_each_scope_loop = { array = _target_states
	
		if = { limit = { NOT = { is_in_array = { perimeter_states = THIS.id } } } add_to_temp_array = { perimeter_states = THIS.id } }
		every_neighbor_state = {
			if = { 
				limit = { 
					impassable = no 

					NOT = { is_in_array = { perimeter_states = THIS.id } } 
				} 
				
				add_to_temp_array = { perimeter_states = THIS.id } 
			}
		}
		for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS value = _project_province_id
		
			if = { 
				limit = { 
					set_temp_variable = { _get_province_type_id = _project_province_id }
					EAI_PC_province_is_passable = yes
					EAI_PC_province_is_city = yes

					PREV = { controls_province = _project_province_id }
				} 
				
				add_to_temp_array = { perimeter_cities = _project_province_id } 
			}
		}
	}

	# States searched for perimeter provinces
	for_each_scope_loop = { array = perimeter_states
			
		# Provinces that are not cities in state
		for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS index = i_097 value = _province_id_a
		
			if = { 
				limit = { 
					set_temp_variable = { _get_province_type_id = _province_id_a }
					EAI_PC_province_is_passable = yes

					PREV = { controls_province = _province_id_a }
					NOT = { is_in_array = { perimeter_cities = _province_id_a } } 
				}
			
				# Target cities in range or neighbor
				for_each_loop = { array = perimeter_cities index = i_099 value = _province_id_b

					EAI_MATH_get_distance_between_provinces_a_b = yes # _province_id_a, _province_id_b, distance_

					set_temp_variable = { _get_province_id_neighbors = _province_id_b }
					EAI_MAP_get_province_neighbors = yes

					if = {
						limit = {
							OR = {
								check_variable = { distance_ < 15 }
								is_in_array = { province_neighbors_ = _province_id_a }
							}
						}
					
						if = { limit = { NOT = { is_in_array = { _target_provinces_ = _province_id_a } } }
									
							add_to_temp_array = { _target_provinces_ = _province_id_a }
						}
					}
				}
			}
		}
	}

	### Clear input

	clear_temp_array = _target_states
}

EAI_PC_FORT_get_target_provinces = { # _target_states, _get_province_home_area, _get_province_city, _get_province_coast, _get_province_naval_base

	for_each_scope_loop = { array = _target_states

		for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS value = _project_province_id

			if = {
				limit = {
					set_temp_variable = { _get_province_type_id = _project_province_id }
					EAI_PC_province_is_passable = yes
					if = { limit = { check_variable = { _get_province_home_area = 1 } } var:global.EAI_MAP_province_state_id^_project_province_id = { is_in_home_area = yes } }
					if = { limit = { check_variable = { _get_province_city = 1 } } EAI_PC_province_is_city = yes }
					if = { limit = { check_variable = { _get_province_coast = 1 } } EAI_PC_province_is_coastal = yes }
					if = { limit = { check_variable = { _get_province_naval_base = 1 } } EAI_PC_province_is_naval_base = yes }
				}
			
				if = { limit = { NOT = { is_in_array = { _target_provinces_ = _project_province_id } } }
					
					add_to_temp_array = { _target_provinces_ = _project_province_id }
				}
			}
		}
	}

	### Clear input

	clear_temp_array = _target_states
	set_temp_variable = { _get_province_home_area = 0 }
	set_temp_variable = { _get_province_city = 0 }
	set_temp_variable = { _get_province_coast = 0 }
	set_temp_variable = { _get_province_naval_base = 0 }
}

EAI_PC_FORT_get_frontline_provinces = {

	clear_temp_array = border_states
	for_each_loop = { array = controlled_states

		var:v = {
			if = {
				limit = {
					impassable = no

					OR = {
						CONTROLLER = { NOT = { has_full_control_of_state = PREV } }
						any_neighbor_state = {
							impassable = no
	
							OR = {
								CONTROLLER = { has_war_with = ROOT }
								ROOT = { 
									controls_state = PREV
									NOT = { has_full_control_of_state = PREV } 
								}
							}
						}
					}
				}
			
				if = { limit = { NOT = { is_in_array = { border_states = THIS.id } } } add_to_temp_array = { border_states = THIS.id } }

				every_neighbor_state = {
					if = { 
						limit = { 
							impassable = no

							OR = {
								CONTROLLER = { has_war_with = ROOT }
								ROOT = { 
									controls_state = PREV
									NOT = { has_full_control_of_state = PREV } 
								}
							}

							NOT = { is_in_array = { border_states = THIS.id } } 
						} 
						
						if = { limit = { NOT = { is_in_array = { border_states = THIS.id } } } add_to_temp_array = { border_states = THIS.id } }
					}
				}
			}
		}
	}

	# Border states
	for_each_scope_loop = { array = border_states

		# Provinces of state
		for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS index = i_6954 value = _project_province_id break = b_6954

			if = { 
				limit = { 
					set_temp_variable = { _get_province_type_id = _project_province_id }
					EAI_PC_province_is_passable = yes
					PREV = { controls_province = _project_province_id }
				}

				# Neighbors of province
				set_temp_variable = { _get_province_id_neighbors = _project_province_id }
				EAI_MAP_get_province_neighbors = yes
				for_each_loop = { array = province_neighbors_ index = i_230 value = neighbor_prov
						
					if = { 
						limit = { 
							set_temp_variable = { _get_province_type_id = neighbor_prov }
							EAI_PC_province_is_passable = yes
							any_of_scopes = { array = ROOT.enemies controls_province = neighbor_prov }
						}
						
						if = { limit = { NOT = { is_in_array = { _target_provinces_ = _project_province_id } } }

							add_to_temp_array = { _target_provinces_ = _project_province_id }
						}
					}
				}
			}
		}
	}
}

EAI_PC_FORT_limit_near_enemies_provinces = { # _target_provinces_, _limit_provinces_near_enemies_radius

	if = { limit = { check_variable = { _target_provinces_^num > 0 } }
	
		### Get states of target provinces
	
		clear_temp_array = target_states
		for_each_loop = { array = _target_provinces_ value = _project_province_id

			var:global.EAI_MAP_province_state_id^_project_province_id = {
	
				if = { limit = { NOT = { is_in_array = { target_states = THIS.id } } } add_to_temp_array = { target_states = THIS.id } }
			}
		}
	
		### Get provinces that dont meet the criteria
	
		clear_temp_array = remove_provinces
		for_each_scope_loop = { array = target_states

			if = {
				limit = {
					NOT = {
						CONTROLLER = { NOT = { has_full_control_of_state = PREV } }

						any_neighbor_state = {
							impassable = no

							OR = {
								CONTROLLER = { has_war_with = ROOT }
								ROOT = { 
									controls_state = PREV
									NOT = { has_full_control_of_state = PREV } 
								}
							}
						}

						any_state = {
							NOT = { state = PREV }

							impassable = no

							check_variable = { distance_to@PREV < _limit_provinces_near_enemies_radius }
							OR = {
								CONTROLLER = { has_war_with = ROOT }
								ROOT = { 
									controls_state = PREV
									NOT = { has_full_control_of_state = PREV } 
								}
							}
						}
					}
				}

				for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS value = _project_province_id
				
					if = { 
						limit = { 
							is_in_array = { _target_provinces_ = _project_province_id } 
							NOT = { is_in_array = { remove_provinces = _project_province_id } } 
						}

						add_to_temp_array = { remove_provinces = _project_province_id }
					}
				}
			}
		}

		### Remove provinces

		for_each_loop = { array = remove_provinces

			remove_from_temp_array = { array = _target_provinces_ value = v }
		}
	}

	### Clear input

	set_temp_variable = { _limit_provinces_near_enemies_radius = 0 }
}

EAI_PC_FORT_limit_provinces = { # _target_provinces_, _limit_provinces_queue_one_per_target, _limit_provinces_naval_base, _limit_provinces_control_days

	if = { limit = { check_variable = { _target_provinces_^num > 0 } }
	
		### Get provinces that dont meet the criteria
	
		clear_temp_array = remove_provinces
		for_each_loop = { array = _target_provinces_ value = _project_province_id

			if = {
				limit = {
					set_temp_variable = { _get_province_type_id = _project_province_id }
					if = { limit = { check_variable = { _limit_provinces_queue_one_per_target = 1 } } EAI_PC_has_same_type_project_here = yes }
					if = { limit = { check_variable = { _limit_provinces_naval_base = 1 } } NOT = { EAI_PC_province_is_naval_base = yes } }
					if = { limit = { check_variable = { _limit_provinces_control_days > 0 } } NOT = { check_variable = { global.EAI_MAP_province_control_days^_get_province_type_id > _limit_provinces_control_days } } }
				}
			
				if = { 
					limit = { 
						is_in_array = { _target_provinces_ = _project_province_id } 
						NOT = { is_in_array = { remove_provinces = _project_province_id } } 
					}
				
					add_to_temp_array = { remove_provinces = _project_province_id }
				}
			}
		}

		### Remove provinces>

		for_each_loop = { array = remove_provinces
		
			remove_from_temp_array = { _target_provinces_ = v }
		}
	}

	### Clear input

	set_temp_variable = { _limit_provinces_queue_one_per_target = 0 }
	set_temp_variable = { _limit_provinces_naval_base = 0 }
	set_temp_variable = { _limit_provinces_control_days = 0 }
}

EAI_PC_FORT_limit_provinces_state_avg_control_days = { # _target_provinces_, _limit_provinces_state_avg_control_days

	if = { limit = { check_variable = { _target_provinces_^num > 0 } }
	
		clear_temp_array = target_states
		for_each_loop = { array = _target_provinces_ value = target_province
		
			if = { limit = { NOT = { is_in_array = { target_states = global.EAI_MAP_province_state_id^target_province } } }
			
				add_to_temp_array = { target_states = global.EAI_MAP_province_state_id^target_province }
			}
		}
	
		clear_temp_array = remove_provinces
		for_each_scope_loop = { array = target_states
		
			set_temp_variable = { control_days_sum = 0 }
			set_temp_variable = { control_days_div = 0 }
			for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS value = sum_province
			
				if = { limit = { is_in_array = { _target_provinces_ = sum_province } }
				
					add_to_temp_variable = { control_days_sum = global.EAI_MAP_province_control_days^sum_province }
					add_to_temp_variable = { control_days_div = 1 }
				}
			}
			if = { limit = { check_variable = { control_days_div > 0 } }
			
				set_temp_variable = { control_days_avg = control_days_sum }
				divide_temp_variable = { control_days_avg = control_days_div }
			}
	
			if = { limit = { check_variable = { control_days_avg < _limit_provinces_state_avg_control_days } }
			
				for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS value = remove_province
			
					if = { limit = { is_in_array = { _target_provinces_ = remove_province } }
					
						add_to_temp_array = { remove_provinces = remove_province }
					}
				}
			}
		}
	
		### Remove provinces
	
		for_each_loop = { array = remove_provinces
		
			remove_from_temp_array = { _target_provinces_ = v }
		}
	}

	### Clear input

	set_temp_variable = { _limit_provinces_state_avg_control_days = 0 }
}

EAI_PC_FORT_generate_river_fortlines = { # arr:_states >>> arr:river_provinces_ arr:bottleneck_provinces_

	### Get all states that are near enemy front lines

		clear_temp_array = border_states
		for_each_scope_loop = { array = _states
			if = {
				limit = {
					impassable = no
					OR = {
						any_neighbor_state = { CONTROLLER = { has_war_with = ROOT } }
						NOT = { ROOT = { has_full_control_of_state = PREV } }
					}
				}
				if = { limit = { NOT = { is_in_array = { border_states = THIS.id } } } add_to_temp_array = { border_states = THIS.id } }
				every_neighbor_state = {
					if = { 
						limit = { 
							impassable = no
							OR = {
								CONTROLLER = { has_war_with = ROOT }
								AND = { is_controlled_by = ROOT NOT = { ROOT = { has_full_control_of_state = PREV } } }
							}
						} 
						if = { limit = { NOT = { is_in_array = { border_states = THIS.id } } } add_to_temp_array = { border_states = THIS.id } }
					}
				}
			}
		}

	### Get all provinces that are part of front lines 

		clear_temp_array = front_provinces
		for_each_scope_loop = { array = border_states
			for_each_loop = { array = global.EAI_MAP_state_province_ids@THIS index = front_prov_i value = _project_province_id break = front_prov_b
				if = { 
					limit = { 
						set_temp_variable = { _get_province_type_id = _project_province_id }
						EAI_PC_province_is_passable = yes
						ROOT = { controls_province = _project_province_id }
					}
					set_temp_variable = { _get_province_id_neighbors = _project_province_id }
					EAI_MAP_get_province_neighbors = yes
					set_temp_variable = { found_enemy_neighbor = 0 }
					for_each_loop = { array = province_neighbors_ index = neighbor_prov_i value = neighbor_prov_id break = found_enemy_neighbor
						if = { 
							limit = { 
								set_temp_variable = { _get_province_type_id = neighbor_prov_id }
								EAI_PC_province_is_passable = yes
								any_of_scopes = { array = ROOT.enemies controls_province = neighbor_prov_id }
							}
							set_temp_variable = { found_enemy_neighbor = 1 }
							if = { limit = { NOT = { is_in_array = { front_provinces = _project_province_id } } } add_to_temp_array = { front_provinces = _project_province_id } }
						}
					}
				}
			}
		}

	### Floodfill from front provinces

		set_temp_variable = { its = 0 }
		set_temp_variable = { max_its = 1000 }
		set_temp_variable = { depth = 10 }
		clear_temp_array = unexplored
		clear_temp_array = explored
		for_each_loop = { array = front_provinces add_to_temp_array = { unexplored = v } }
		while_loop_effect = { break = loop_b
			limit = { 
				check_variable = { its < max_its }
				check_variable = { unexplored^num > 0 }
			}
			set_temp_variable = { cur_prov = unexplored^0 }
			remove_from_temp_array = { array = unexplored index = 0 }
			add_to_temp_array = { explored = cur_prov }
			set_temp_variable = { num_steps = steps^came_from^cur_prov }
			add_to_temp_variable = { num_steps = 1 }
			set_temp_variable = { steps^cur_prov = num_steps }
			if = { limit = { check_variable = { steps^cur_prov < depth } }
				set_temp_variable = { _get_province_id_neighbors = cur_prov }
				EAI_MAP_get_province_neighbors = yes
				for_each_loop = { array = province_neighbors_ index = i value = v break = neighbor_b
					if = { limit = { NOT = { any_of_scopes = { array = enemies controls_province = v } } }
						if = { limit = { NOT = { is_in_array = { unexplored = v } is_in_array = { explored = v } } } 
							add_to_temp_array = { unexplored = v } 
							set_temp_variable = { came_from^v = cur_prov }
						} 
						else_if = { limit = { check_variable = { steps^cur_prov < steps^came_from^v } }
							if = { limit = { is_in_array = { explored = v } } remove_from_temp_array = { array = explored value = v } }
							if = { limit = { NOT = { is_in_array = { unexplored = v } } } add_to_temp_array = { unexplored = v }  }
							set_temp_variable = { came_from^v = cur_prov }
						}
					}
				}
			}
			add_to_temp_variable = { its = 1 }
		}
		clear_temp_array = front_area
		for_each_loop = { array = explored if = { limit = { NOT = { is_in_array = { front_area = v } } } add_to_temp_array = { front_area = v } } }

	###

		clear_temp_array = river_provinces_
		clear_temp_array = bottleneck_provinces_
		for_each_loop = { array = front_area index = front_area_i value = front_area_v

			if = { limit = { controls_province = front_area_v }
			
				### Id rivers
	
					set_temp_variable = { _get_river_connections_id = front_area_v }
					EAI_MAP_get_river_connections = yes
					if = { 
						limit = {
							check_variable = { river_connections_^num > 0 }
							any_of = { array = river_connections_ check_variable = { v = came_from^front_area_v } }
						}
						add_to_temp_array = { river_provinces_ = front_area_v }
					}
				
				### Id bottlenecks
	
					set_temp_variable = { _get_province_id_neighbors = front_area_v }
					EAI_MAP_get_province_neighbors = yes
					if = { 
						limit = { 
							set_temp_variable = { out = 0 }
							set_temp_variable = { tot = 0 }
							all_of = { array = province_neighbors_ 
								if = { limit = { check_variable = { came_from^v = front_area_v } } add_to_temp_variable = { out = 1 } }
								if = { limit = { is_in_array = { front_area = v } } add_to_temp_variable = { tot = 1 } }
							}
							check_variable = { tot = province_neighbors_^num }
							check_variable = { tot > 1 }
							set_temp_variable = { diff = tot }
							subtract_from_temp_variable = { diff = out }
							check_variable = { diff = 1 }
						}
						add_to_temp_array = { bottleneck_provinces_ = front_area_v }
					}
	
				###
			}
		}

	###

	clear_temp_array = _states
}

###

EAI_PC_FORT_add_to_valid_fortification_projects = {

	for_each_loop = { array = _target_provinces_ add_to_temp_array = { _valid_provinces = v } }
}

EAI_PC_FORT_cancel_invalid_fortification_projects = { # _valid_provinces, strategy_type_id

	for_each_loop = { array = EAI_PC_queue value = _project_id
	
		if = { 
			limit = { 
				check_variable = { EAI_PC_type_id^_project_id = strategy_type_id }
				NOT = { is_in_array = { _valid_provinces = EAI_PC_target_province^_project_id } }
			}
			
			add_to_temp_array = { _cancel_projects_PROVINCES = EAI_PC_target_province^_project_id }
		}
	}
	if = { limit = { check_variable = { _cancel_projects_PROVINCES^num > 0 } } 
	
		add_to_temp_array = { _cancel_projects_TYPE_IDS = strategy_type_id }
		set_temp_variable = { EAI_PC_cancel_projects_source = 5 }
		EAI_PC_cancel_projects = yes
	}

	### Clear input

	clear_temp_array = _valid_provinces
}