############################################################################################################
#	Expert AI mod - division cap effects
############################################################################################################

EAI_check_division_limits = {

	set_temp_variable = { division_production_was_adjusted = 0 } # Limits that adjust role_ratio need this fix
	
	EAI_calculate_division_cap = yes

	# infantry

	if = { limit = { EAI_infantry_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_infantry_division_limit } } 

			set_temp_variable = { division_production_was_adjusted = 1 }
		
			set_country_flag = EAI_infantry_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: infantry, [?EAI_division_cap_10k]0000" }  
		}
	}
	else_if = { limit = { has_country_flag = EAI_infantry_division_limit }

		set_temp_variable = { division_production_was_adjusted = 1 }
		
		clr_country_flag = EAI_infantry_division_limit
	}

	# anti tank

	if = { limit = { EAI_anti_tank_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_anti_tank_division_limit } } 
		
			set_country_flag = EAI_anti_tank_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: anti tank" }  
		}
	}
	else = { clr_country_flag = EAI_anti_tank_division_limit }

	# armor

	if = { limit = { OR = { EAI_armored_division_limit = yes has_country_flag = EAI_armor_production_design_upgrade_fix } }

		if = { limit = { NOT = { has_country_flag = EAI_armored_division_limit } } 
		
			set_country_flag = EAI_armored_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: armor [?EAI_arm_bat_count] > [?EAI_armor_division_cap]" }  
		}
	}
	else = { clr_country_flag = EAI_armored_division_limit }

	# light armor

	if = { limit = { EAI_light_armor_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_light_armor_division_limit } } 
		
			set_country_flag = EAI_light_armor_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: EAI_light_armor_division_limit" }  
		}
	}
	else = { clr_country_flag = EAI_light_armor_division_limit }

	# medium armor

	if = { limit = { EAI_medium_armor_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_medium_armor_division_limit } } 
		
			set_country_flag = EAI_medium_armor_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: EAI_medium_armor_division_limit" }  
		}
	}
	else = { clr_country_flag = EAI_medium_armor_division_limit }

	# heavy armor

	if = { limit = { EAI_heavy_armor_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_heavy_armor_division_limit } } 
		
			set_country_flag = EAI_heavy_armor_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: EAI_heavy_armor_division_limit" }  
		}
	}
	else = { clr_country_flag = EAI_heavy_armor_division_limit }

	# modern armor

	if = { limit = { EAI_modern_armor_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_modern_armor_division_limit } } 
		
			set_country_flag = EAI_modern_armor_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: EAI_modern_armor_division_limit" }  
		}
	}
	else = { clr_country_flag = EAI_modern_armor_division_limit }

	# motorized

	if = { limit = { EAI_motorized_division_limit = yes } 

		if = { limit = { NOT = { has_country_flag = EAI_motorized_division_limit } } 
		
			set_country_flag = EAI_motorized_division_limit 
			if = { limit = { has_country_flag = EAI_division_limit_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | DIVISION LIMIT: motorized" }  
		}
	}
	else = { clr_country_flag = EAI_motorized_division_limit }

	if = { limit = { has_country_flag = EAI_armor_production_design_upgrade_fix } 

		EAI_armor_production_design_upgrade_fix = yes
	}
}

# division cap calculation
EAI_calculate_division_cap = {

	### infantry division cap

	set_variable = { EAI_division_cap_10k = 0 }

	set_temp_variable = { EAI_core_states_slots = 0 }
	set_temp_variable = { EAI_noncore_states_slots = 0 }

	for_each_scope_loop = { array = controlled_states 

		if = { limit = { NOT = { is_owned_by = PREV } }

			add_to_temp_variable = { EAI_noncore_states_slots = modifier@local_building_slots }
		}
		else = { add_to_temp_variable = { EAI_core_states_slots = modifier@local_building_slots } }
	}

	# building slot score from core states
	set_temp_variable = { EAI_slots_factor = modifier@global_building_slots_factor }
	clamp_temp_variable = { var = EAI_slots_factor min = 0 max = 1 }
	multiply_temp_variable = { EAI_slots_factor = 0.4 }
	add_to_temp_variable = { EAI_slots_factor = 1 }
	multiply_temp_variable = { EAI_core_states_slots = EAI_slots_factor }

	add_to_variable = { EAI_division_cap_10k = EAI_core_states_slots }

	# building slot score from noncores
	multiply_temp_variable = { EAI_noncore_states_slots = 0.2 }

	add_to_variable = { EAI_division_cap_10k = EAI_noncore_states_slots }

	### armor division cap

	set_variable = { EAI_armor_division_cap = num_of_military_factories }
	multiply_variable = { EAI_armor_division_cap = 1.4 }

	# increase with difficulty settings
	if = { limit = { check_variable = { EAI_difficulty_equipment_bonus_active = 1 } } multiply_variable = { EAI_armor_division_cap = 1.25 } }
	else_if = { limit = { check_variable = { EAI_difficulty_equipment_bonus_active = 2 } } multiply_variable = { EAI_armor_division_cap = 1.50 } }
	else_if = { limit = { check_variable = { EAI_difficulty_equipment_bonus_active = 3 } } multiply_variable = { EAI_armor_division_cap = 1.75 } }

	# reduce if it has no armor focus
	if = { limit = { check_variable = { EAI_FOCUS/LAND/armor_focus = global.EAI_AT_FOCUS } } multiply_variable = { EAI_armor_division_cap = 0.25 } }
	
	# reduce if it has low fuel
	if = { limit = { fuel_ratio < 0.5 } multiply_variable = { EAI_armor_division_cap = 0.5 } }
	
	### limits

	clamp_variable = { var = EAI_division_cap_10k min = 20 max = 600 } # 1 = 10 battalions
	clamp_variable = { var = EAI_armor_division_cap min = 15 max = 1000 } # 1 = 1 battalions

	# special adjustments
	EAI_special_infantry_division_limits = yes
}

# special modifiers applied to division cap
EAI_special_infantry_division_limits = {

	#############################################
	#	germany/europe
	#############################################

	if = { # france: limit ai
		limit = { 
			original_tag = FRA

			is_historical_focus_on = yes

			GER = { is_ai = yes }

			date < 1941.1.1

			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 90 }
	}

	else_if = { # GER: needs to be able to do Anschluss on time
		limit = { 
			original_tag = AUS

			threat < 0.75
			has_war = no
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 4 }

		# To be safe
		if = { limit = { GER = { is_ai = yes } is_historical_focus_on = yes } 

			clamp_variable = { var = EAI_division_cap_10k min = 0 max = 3 } 
		}
	}

	else_if = { # low countries: limit ai
		limit = { 
			OR = {
				original_tag = HOL
				original_tag = BEL
				original_tag = LUX
			}

			is_historical_focus_on = yes

			GER = { is_ai = yes is_major = yes has_government = fascism }

			has_war = no
			date < 1941.1.1 

			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }
		}

		# set_variable = { EAI_division_cap_10k = 0 }
	}

	else_if = { # norway: limit ai
		limit = { 
			original_tag = NOR

			is_historical_focus_on = yes

			GER = { is_ai = yes }
			NOT = { is_in_faction_with = GER }

			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }
		}

		set_variable = { EAI_division_cap_10k = 0 }
	}

	else_if = { # poland: limit ai
		limit = { 
			is_historical_focus_on = yes
			original_tag = POL
			date < 1941.1.1
		}

		if = { 
			limit = { 
				GER = { is_ai = yes } 
				NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
				NOT = { EAI_player_intervening = yes }
			} 
			clamp_variable = { var = EAI_division_cap_10k min = 0 max = 50 } 
		}
		else = { clamp_variable = { var = EAI_division_cap_10k min = 100 } }
	}

	else_if = { # greece: limit ai
		limit = { 
			original_tag = GRE

			is_historical_focus_on = yes
			
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }

			GER = { is_ai = yes } 
			ITA = { is_ai = yes } 
			NOT = { any_enemy_country = { is_ai = no } }
			date < 1942.1.1
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 10 }
	}

	#############################################
	#	japan/pacific
	#############################################

	else_if = { # China: limit AI army size of entire faction
		limit = { 
			is_historical_focus_on = yes
			is_literally_china = yes
			if = { limit = { CHI = { has_war = yes } }
				OR = {
					has_war_together_with = CHI
					tag = CHI
				}
			}
			JAP = { is_ai = yes }
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }
		}

		# Favor Japan
		if = { limit = { check_variable = { global.EAI_sino_japanese_war = 0 } } 
		
			if = { limit = { check_variable = { global.EAI_MILITARY_china_war_LIMITER_total_army_size_k > 400 } }
			
				clamp_variable = { var = EAI_division_cap_10k min = 0 max = 1 }
			}
			else = {
			
				clamp_variable = { var = EAI_division_cap_10k min = 100 max = 500 }
			}
		}

		# Favor China
		else_if = { limit = { check_variable = { global.EAI_sino_japanese_war = 1 } } 
		
			clamp_variable = { var = EAI_division_cap_10k min = 100 max = 500 }
		}

		# Stalemate
		else_if = { limit = { check_variable = { global.EAI_sino_japanese_war = 2 } } 
		
			if = { 
				limit = { 
					check_variable = { global.EAI_MILITARY_china_war_LIMITER_total_army_size_k > global.EAI_MILITARY_china_war_LIMITER_total_army_size_limit_k } 
					date < 1944.1.1
					NOT = {
						any_enemy_country = {
							is_major = yes
							NOT = { tag = JAP }
						}
					}
				}
			
				clamp_variable = { var = EAI_division_cap_10k min = 0 max = 1 }
			}
			else = {
			
				clamp_variable = { var = EAI_division_cap_10k min = 100 max = 500 }
			}
		}
	}

	else_if = { # China: more divisions against player JAP
		limit = {
			is_historical_focus_on = yes
			original_tag = CHI
			is_subject = no
			OR = {
				JAP = { is_ai = no }
				EAI_DIFFICULTY_has_difficulty_bonus = yes
				EAI_player_intervening = yes
			}
		}

		clamp_variable = { var = EAI_division_cap_10k min = 400 max = 500 }
	}

	else_if = { # pacific: limit ai 
		limit = { 
			OR = { 
				original_tag = PHI 
				original_tag = INS 
				original_tag = MAL 
			}

			JAP = { is_ai = yes is_major = yes has_government = fascism }

			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 2 }
	}

	#############################################
	#	middle east
	#############################################

	else_if = { # iran: limit ai
		limit = { 
			original_tag = PER 

			NOT = { EAI_player_intervening = yes }
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			
			SOV = { is_ai = yes }

			ENG = { is_ai = yes }

			is_historical_focus_on = yes

			has_government = neutrality
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 7 }
	}

	else_if = { # iraq: limit ai
		limit = { 
			original_tag = IRQ 

			NOT = { EAI_player_intervening = yes }
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }

			ENG = { is_ai = yes }

			is_historical_focus_on = yes

			has_government = neutrality
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 1 }
	}

	#############################################
	#	other
	#############################################

	else_if = { # SOV army size
		limit = { 
			original_tag = SOV

			num_of_military_factories < 350
		}

		clamp_variable = { var = EAI_division_cap_10k min = 300 max = 600 }
	}

	else_if = { # RAJ has limited MP
		limit = { 
			original_tag = RAJ 

			NOT = { is_subject = no }
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 60 }
	}

	else_if = { # limit USA to a more historical army size
		limit = { 
			original_tag = USA

			NOT = { EAI_player_intervening = yes }
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }

			OR = {
				EAI_MODE_historical = yes
				has_global_flag = EAI_performance_mode
			}

			# is not being invaded
			NOT = { surrender_progress > 0.01 }
			NOT = { any_home_area_neighbor_country = { has_war_with = ROOT } }

			# has a strong navy
			alliance_naval_strength_ratio > 1.0
			has_navy_size = { size > 100 }

			# another major ally that has not capitulated
			any_allied_country = {
				EAI_major_country = yes
				has_capitulated = no
			}
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 80 }
	}

	else_if = { # ETH
		limit = { 
			tag = AOI
			is_historical_focus_on = yes

			NOT = { EAI_player_intervening = yes }
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }

			ENG = { is_ai = yes }
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 15 }
	}

	else_if = { # GER
		limit = { 
			tag = GER
			is_historical_focus_on = yes
			EAI_MODE_challenging = yes
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 200 }
		clamp_variable = { var = EAI_armor_division_cap min = 30 max = 2000 }
	}

	else_if = { # HUN
		limit = { 
			tag = HUN
			EAI_MODE_challenging = yes
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 100 }
	}

	else_if = { # ROM
		limit = { 
			tag = ROM
			EAI_MODE_challenging = yes
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 100 }
	}

	else_if = { # USA
		limit = { 
			original_tag = USA
			EAI_MODE_challenging = yes
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 250 }
	}

	else_if = { # ENG
		limit = { 
			original_tag = ENG
			EAI_MODE_challenging = yes
		}

		clamp_variable = { var = EAI_division_cap_10k min = 0 max = 170 }
	}

	#############################################
	#	performance mode
	#############################################

	if = {
		limit = {
			has_global_flag = EAI_performance_mode
			has_war = no
			NOT = { 
				EAI_major_country = yes 
				EAI_asian_minor = yes
				EAI_commonwealth = yes
				EAI_european_minor = yes
			}
			NOT = { EAI_DIFFICULTY_has_difficulty_bonus = yes }
			NOT = { EAI_player_intervening = yes }
		}

		set_variable = { EAI_division_cap_10k = 0 }
	}
}