##############################################################################################################################
#	Expert AI mod - Construction strategies
##############################################################################################################################

# Vars:

# EAI_BUILDING_num_total_slots
# EAI_BUILDING_num_free_slots

# EAI_BUILDING_total_factories
# EAI_BUILDING_num_CIC
# EAI_BUILDING_num_available_CIC
# EAI_BUILDING_num_reserved_CIC
# EAI_BUILDING_num_consumer_CIC
# EAI_BUILDING_num_MIC
# EAI_BUILDING_num_NIC
# EAI_BUILDING_num_REF
# EAI_BUILDING_num_SILO

# EAI_BUILDING_ratio_CIC
# EAI_BUILDING_ratio_available_CIC
# EAI_BUILDING_ratio_MIC
# EAI_BUILDING_ratio_NIC

# EAI_BUILDING_target_weight_CIC
# EAI_BUILDING_target_weight_MIC
# EAI_BUILDING_target_weight_NIC

# EAI_BUILDING_target_num_CIC
# EAI_BUILDING_target_num_MIC
# EAI_BUILDING_target_num_NIC
# EAI_BUILDING_target_num_REF
# EAI_BUILDING_target_num_SILO

# Functions:

# EAI_BUILDING_FACTORY_force_ratio_CIC = yes (input = ratio_value)
# EAI_BUILDING_FACTORY_force_ratio_MIC = yes (input = ratio_value)
# EAI_BUILDING_FACTORY_force_ratio_NIC = yes (input = ratio_value)

##############################################################################################################################
### 	Factories
##############################################################################################################################

@EAI_EARLY_GAME_BUILD_PERIOD = 1939.1.1

# CIC
@EAI_MIN_CONSTRUCTION_SPEED_AVOID_CIC = -0.3
@EAI_MIN_AVAILABLE_NUM_CIC = 5
@EAI_HIGH_AVAILABLE_NUM_CIC = 100
@EAI_MAX_AVAILABLE_NUM_CIC = 200
@EAI_MIN_RATIO_AVAILABLE_CIC = 0.15
@EAI_MIN_RATIO_CIC = 0.33
@EAI_MAX_CONSUMER_GOODS_FACTOR_AVOID_CIC = 0.5
@EAI_MAX_CONSUMER_GOODS_FACTORIES_AVOID_CIC = 75

# MIC
@EAI_MIN_CONSTRUCTION_SPEED_AVOID_MIC = 0.4
@EAI_MIN_NUM_MIC = 1
@EAI_MAX_NUM_MIC = 400

# NIC
@EAI_MIN_CONSTRUCTION_SPEED_AVOID_NIC = 0.2
@EAI_HIGH_NUM_NIC = 75
@EAI_MAX_NUM_NIC = 100
@EAI_MINOR_COUNTRY_NIC_FACTOR = 0.25

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

EAI_BUILDING_FACTORY_general_building_weight_modifiers = { # Set weights which determine CIC/MIC/NIC target building ratios across all shared slots the AI has

	###################################################
	###		Default weight ratios
	###################################################

	if = {
		limit = {
			has_war = no
		}

		set_temp_variable = { EAI_BUILDING_target_weight_CIC = 100 }
		set_temp_variable = { EAI_BUILDING_target_weight_MIC = 100 }
		set_temp_variable = { EAI_BUILDING_target_weight_NIC = 25 }
	}
	else_if = {
		limit = {
			has_war = yes
		}

		set_temp_variable = { EAI_BUILDING_target_weight_CIC = 50 }
		set_temp_variable = { EAI_BUILDING_target_weight_MIC = 100 }
		set_temp_variable = { EAI_BUILDING_target_weight_NIC = 25 }
	}

	###################################################
	###		Modifiers
	###################################################
	
	# Bad CIC construction speed
	if = { 
		limit = { 
			set_temp_variable = { construction_speed = modifier@production_speed_industrial_complex_factor }
			add_to_temp_variable = { construction_speed = modifier@production_speed_buildings_factor }
			check_variable = { construction_speed < @EAI_MIN_CONSTRUCTION_SPEED_AVOID_CIC }
		}

		multiply_temp_variable = { EAI_BUILDING_target_weight_CIC = 0.25 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Bad CIC construction speed" }
	}

	# Bad MIC construction speed
	if = { 
		limit = { 
			has_war = no
			set_temp_variable = { construction_speed = modifier@production_speed_arms_factory_factor }
			add_to_temp_variable = { construction_speed = modifier@production_speed_buildings_factor }
			check_variable = { construction_speed < @EAI_MIN_CONSTRUCTION_SPEED_AVOID_MIC }
		}

		multiply_temp_variable = { EAI_BUILDING_target_weight_MIC = 0.25 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Bad MIC construction speed" }
	}

	# Bad NIC construction speed
	if = { 
		limit = { 
			set_temp_variable = { construction_speed = modifier@production_speed_dockyard_factor }
			add_to_temp_variable = { construction_speed = modifier@production_speed_buildings_factor }
			check_variable = { construction_speed < @EAI_MIN_CONSTRUCTION_SPEED_AVOID_NIC }
		}

		multiply_temp_variable = { EAI_BUILDING_target_weight_NIC = 0.25 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Bad NIC construction speed" }
	}

	# Needs more available CIC
	if = { 
		limit = { 
			OR = {
				check_variable = { EAI_BUILDING_num_available_CIC < @EAI_MIN_AVAILABLE_NUM_CIC }
				check_variable = { EAI_BUILDING_ratio_CIC < @EAI_MIN_RATIO_CIC }
			} 
			NOT = { check_variable = { EAI_BUILDING_num_available_CIC > @EAI_HIGH_AVAILABLE_NUM_CIC } }
		}

		add_to_temp_variable = { EAI_BUILDING_target_weight_CIC = 1000 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Needs more available CIC [?EAI_BUILDING_num_available_CIC] [?EAI_BUILDING_ratio_available_CIC]" }
	}

	# Peace time CIC buildup
	if = { 
		limit = { 
			has_war = no
			date < 1937.6.1
			threat < 0.5
		}

		set_temp_variable = { EAI_BUILDING_target_weight_CIC = 1000 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Peace time CIC buildup" }
	}

	# Lots of CIC locked away in consumer goods - build NIC
	if = { 
		limit = { 
			has_war = no
			check_variable = { modifier@consumer_goods_factor > @EAI_MAX_CONSUMER_GOODS_FACTOR_AVOID_CIC }
			check_variable = { EAI_BUILDING_num_consumer_CIC > @EAI_MAX_CONSUMER_GOODS_FACTORIES_AVOID_CIC }
		}

		set_temp_variable = { EAI_BUILDING_target_weight_CIC = 0.01 }
		set_temp_variable = { EAI_BUILDING_target_weight_MIC = 0.01 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Lots of CIC locked away in consumer goods - build NIC" }
	}

	# A lot of ACIC
	if = { 
		limit = { 
			check_variable = { EAI_BUILDING_num_available_CIC > @EAI_HIGH_AVAILABLE_NUM_CIC }
		}

		multiply_temp_variable = { EAI_BUILDING_target_weight_CIC = 0.5 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "A lot of ACIC -  less CIC" }
	}

	# Too much ACIC
	if = { 
		limit = { 
			check_variable = { EAI_BUILDING_num_available_CIC > @EAI_MAX_AVAILABLE_NUM_CIC }
		}

		set_temp_variable = { EAI_BUILDING_target_weight_CIC = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Too much ACIC -  less CIC" }
	}

	# Too much MIC
	if = { 
		limit = { 
			check_variable = { EAI_BUILDING_num_MIC > @EAI_MAX_NUM_MIC }
		}

		set_temp_variable = { EAI_BUILDING_target_weight_MIC = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Too much MIC" }
	}

	# A lot of NIC
	if = { 
		limit = { 
			check_variable = { EAI_BUILDING_num_NIC > @EAI_HIGH_NUM_NIC }
		}

		set_temp_variable = { EAI_BUILDING_target_weight_NIC = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "A lot of NIC" }
	}

	# Too much NIC
	if = { 
		limit = { 
			check_variable = { EAI_BUILDING_num_NIC > @EAI_MAX_NUM_NIC }
		}

		set_temp_variable = { EAI_BUILDING_target_weight_NIC = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Too much NIC" }
	}

	# Landlocked - no NIC
	if = {
		limit = {
			NOT = {
				any_controlled_state = {
					is_coastal = yes
					free_building_slots = { 
						building = industrial_complex 
						size > 0
					}
				}
			}
		}

		set_temp_variable = { EAI_BUILDING_target_weight_NIC = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Landlocked - no NIC" }
	}

	# Minor country builds less NIC
	if = {
		limit = {
			is_major = no
		}

		multiply_temp_variable = { EAI_BUILDING_target_weight_NIC = 0.25 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Minor country builds less NIC" }
	}
}

EAI_BUILDING_FACTORY_special_building_weight_modifiers = { # Special case weight modifiers

	# FRA and ENG military buildup
	if = {
		limit = {
			date > 1937.1.1
			OR = {
				tag = ENG
				tag = FRA
			}
			check_variable = { EAI_BUILDING_num_CIC > 45 }
			check_variable = { EAI_BUILDING_num_MIC < 100 }
			has_war = no
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "FRA and ENG military buildup" }

		set_temp_variable = { ratio_value = 0.75 }
		EAI_BUILDING_FACTORY_force_ratio_MIC = yes
	}

	# ITA military buildup
	if = {
		limit = {
			date > 1937.1.1
			check_variable = { EAI_BUILDING_num_CIC > 35 }
			check_variable = { EAI_BUILDING_num_MIC < 100 }
			has_war = no
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "ITA military buildup" }

		set_temp_variable = { ratio_value = 0.75 }
		EAI_BUILDING_FACTORY_force_ratio_MIC = yes
	}

	# Major naval powers build more NIC
	if = {
		limit = {
			OR = {
				tag = ENG
				tag = USA
				tag = JAP
				tag = FRA
				tag = ITA
			}
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "Major naval powers build more NIC" }

		multiply_temp_variable = { EAI_BUILDING_target_weight_NIC = 2 }
	}

	# CHI and JAP prepare for early war
	if = {
		limit = {
			OR = {
				tag = CHI
				tag = JAP
			}
			date < 1939.1.1
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "CHI and JAP prepare for early war" }

		set_temp_variable = { ratio_value = 0.25 }
		EAI_BUILDING_FACTORY_force_ratio_CIC = yes
	}

	# GER and SOV have less need for NIC at start
	if = {
		limit = {
			OR = {
				tag = GER
				tag = SOV
			}
			date < 1941.1.1
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "GER and SOV have less need for NIC at start" }

		set_temp_variable = { ratio_value = 0.01 }
		EAI_BUILDING_FACTORY_force_ratio_NIC = yes
	}

	# SOV focuses on CIC
	if = {
		limit = {
			tag = SOV
			date < 1939.1.1
			has_war = no
			check_variable = { EAI_BUILDING_num_CIC < 150 }
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "SOV focuses on CIC" }

		set_temp_variable = { ratio_value = 0.99 }
		EAI_BUILDING_FACTORY_force_ratio_CIC = yes
	}
}

EAI_BUILDING_FACTORY_special_building_target_modifiers = { # Modify target num of buildings directly

	set_temp_variable = { _lock_target_num_CIC = 0 }
	set_temp_variable = { _lock_target_num_MIC = 0 }
	set_temp_variable = { _lock_target_num_NIC = 0 }

	#################################
	### General strategy
	#################################

	### Generic

		### Peace time industrial strategy - focus CIC

			if = { limit = { has_war = no }

				### Target CIC
			
					if = { 
						limit = { 
							AND = {
								date < 1937.6.1
								threat < 0.5
							}
							OR = {
								has_idea = civilian_economy
								has_idea = low_economic_mobilisation
							}
						}
				
						set_temp_variable = { _target_ratio_CIC = 1.0 }
						set_temp_variable = { _target_ratio_MIC = 0.0 }
						set_temp_variable = { _target_ratio_NIC = 0.0 }
						EAI_BUILDING_FACTORY_force_target_ratio = yes
				
						if = { limit = { has_country_flag = EAI_building_logging } log = "Peace time industrial strategy - focus CIC" }
					}

				### Target CIC/MIC

					else_if = {
						limit = {
							check_variable = { EAI_BUILDING_ratio_CIC < 0.33 }
						}
						
						set_temp_variable = { _target_ratio_CIC = 0.5 }
						set_temp_variable = { _target_ratio_MIC = 0.5 }
						set_temp_variable = { _target_ratio_NIC = 0.0 }
						EAI_BUILDING_FACTORY_force_target_ratio = yes
				
						if = { limit = { has_country_flag = EAI_building_logging } log = "Peace time industrial strategy - focus CIC/MIC" }
					}

				### Target MIC

					else = {
						
						set_temp_variable = { _target_ratio_CIC = 0.0 }
						set_temp_variable = { _target_ratio_MIC = 1.0 }
						set_temp_variable = { _target_ratio_NIC = 0.0 }
						EAI_BUILDING_FACTORY_force_target_ratio = yes
				
						if = { limit = { has_country_flag = EAI_building_logging } log = "Peace time industrial strategy - focus CIC/MIC" }
					}

				###
			}

		### War time industrial strategy - focus MIC

			else = {

				### Build MIC unless there are other needs

					set_temp_variable = { _target_ratio_CIC = 0.0 }
					set_temp_variable = { _target_ratio_MIC = 1.0 }
					set_temp_variable = { _target_ratio_NIC = 0.0 }
					EAI_BUILDING_FACTORY_force_target_ratio = yes

					if = { limit = { has_country_flag = EAI_building_logging } log = "War time industrial strategy - focus MIC" }

				###
			}

		###

	###

	#################################
	### Country strategy
	#################################

	### GER

		if = { limit = { tag = GER }

			### Peace time

				if = { limit = { has_war = no }

					### Target CIC
				
						if = {
							limit = {
								date < 1938.1.1
							}
						
							set_temp_variable = { _target_ratio_CIC = 1.0 }
							set_temp_variable = { _target_ratio_MIC = 0.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "GER - CIC" }
						}

					### Target MIC

						else = {
				
							set_temp_variable = { _target_ratio_CIC = 0.0 }
							set_temp_variable = { _target_ratio_MIC = 1.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "GER - MIC" }
						}

					###
				}

			### War time

				else = {

					### Endgame - expand NIC
		
						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_MIC > 300 }
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = EAI_BUILDING_num_NIC }
							add_to_temp_variable = { EAI_BUILDING_target_num_NIC = 2 }
							clamp_temp_variable = { var = EAI_BUILDING_target_num_NIC min = 0 max = 60 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "Endgame - expand NIC" }
						}
		
					###
				}

			###
		}

	### ITA

		if = { limit = { tag = ITA }

			### Peace time

				if = { limit = { has_war = no }

					### Target CIC
				
						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_CIC < 40 }
							}
						
							set_temp_variable = { _target_ratio_CIC = 1.0 }
							set_temp_variable = { _target_ratio_MIC = 0.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ITA - CIC" }
						}

					### Target MIC

						else = {
				
							set_temp_variable = { _target_ratio_CIC = 0.0 }
							set_temp_variable = { _target_ratio_MIC = 1.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ITA - MIC" }
						}

					### Target NIC

						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_NIC < 25 }
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = EAI_BUILDING_num_NIC }
							add_to_temp_variable = { EAI_BUILDING_target_num_NIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ITA - NIC" }
						}

					###
				}

			### War time			

				else = {

					### Target NIC

						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_NIC < 25 }
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = EAI_BUILDING_num_NIC }
							add_to_temp_variable = { EAI_BUILDING_target_num_NIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ITA - NIC" }
						}

					### Endgame - expand NIC
		
						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_MIC > 150 }
							}
		
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = EAI_BUILDING_num_NIC }
							add_to_temp_variable = { EAI_BUILDING_target_num_NIC = 2 }
							clamp_temp_variable = { var = EAI_BUILDING_target_num_NIC min = 0 max = 60 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "Endgame - expand NIC" }
						}
		
					###
				}

			###
		}

	### JAP

		if = { limit = { tag = JAP }

			### Peace time		

				if = { limit = { has_war = no }
				
					### Target CIC
					
						if = { limit = { check_variable = { EAI_BUILDING_num_CIC < 45 } }
						
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 45 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 0 }
		
							set_temp_variable = { _lock_target_num_CIC = 1 }
		
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build CIC 50" }
						}
		
					### Target MIC
		
						else_if = { limit = { check_variable = { EAI_BUILDING_num_MIC < 50 } }
		
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 50 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
		
							set_temp_variable = { _lock_target_num_MIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build MIC 50" }
						}
		
					### Target NIC
		
						else_if = { limit = { check_variable = { EAI_BUILDING_num_NIC < 25 } }
		
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 25 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
		
							set_temp_variable = { _lock_target_num_NIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build NIC 25" }
						}
		
					### Target MIC
		
						else_if = { limit = { check_variable = { EAI_BUILDING_num_MIC < 100 } }
		
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 100 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
		
							set_temp_variable = { _lock_target_num_MIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build MIC 100" }
						}
		
					###
				}

			### War time			

				else = {
				
					### Target CIC
					
						if = { limit = { check_variable = { EAI_BUILDING_num_CIC < 45 } }
						
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 45 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 0 }
		
							set_temp_variable = { _lock_target_num_CIC = 1 }
		
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build CIC 50" }
						}
		
					### Target MIC
		
						else_if = { limit = { check_variable = { EAI_BUILDING_num_MIC < 50 } }
		
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 50 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
		
							set_temp_variable = { _lock_target_num_MIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build MIC 50" }
						}
		
					### Target NIC
		
						else_if = { limit = { check_variable = { EAI_BUILDING_num_NIC < 25 } }
		
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 25 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
		
							set_temp_variable = { _lock_target_num_NIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build NIC 25" }
						}
		
					### Target MIC
		
						else_if = { limit = { check_variable = { EAI_BUILDING_num_MIC < 100 } }
		
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 100 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
		
							set_temp_variable = { _lock_target_num_MIC = 1 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "JAP Build MIC 100" }
						}
		
					### Endgame - expand NIC
		
						else = {
		
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = EAI_BUILDING_num_NIC }
							add_to_temp_variable = { EAI_BUILDING_target_num_NIC = 2 }
							clamp_temp_variable = { var = EAI_BUILDING_target_num_NIC min = 0 max = 60 }
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "Endgame - expand NIC" }
						}
		
					###
				}

			###
		}

	### SOV

		if = { limit = { tag = SOV }

			### Peace time		

				if = { limit = { has_war = no }
				
					### Target MIC
		
						if = { 
							limit = { 
								check_variable = { EAI_PC_strategy_state = 3 } 
		
								check_variable = { EAI_BUILDING_num_CIC > 100 }
								check_variable = { EAI_BUILDING_num_MIC < 500 }
							}
		
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 500 }
		
							if = { limit = { has_country_flag = EAI_building_logging } log = "Target MIC" }
						}
		
					###
				}

			### War time			

				else = {
				
					### Target MIC
		
						if = { 
							limit = { 
								check_variable = { EAI_PC_strategy_state = 3 } 
		
								check_variable = { EAI_BUILDING_num_CIC > 100 }
								check_variable = { EAI_BUILDING_num_MIC < 500 }
							}
		
							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = 500 }
		
							if = { limit = { has_country_flag = EAI_building_logging } log = "Target MIC" }
						}
		
					###
				}

			###
		}

	### USA 

		if = { limit = { tag = USA }

			### Peace time			

				if = { limit = { has_war = no }
				
					### Target NIC
		
						if = {
							limit = {
								set_temp_variable = { construction_speed = modifier@production_speed_industrial_complex_factor }
								add_to_temp_variable = { construction_speed = modifier@production_speed_buildings_factor }
								OR = {
									check_variable = { construction_speed < -0.1 }
									AND = {
										EAI_MODE_challenging = yes
										date < 1938.6.1
									}
								}
								
							}
		
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 70 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = -1 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = -1 }

							if = { limit = { has_country_flag = EAI_building_logging } log = "Target NIC" }
						}
		
					### Target 1.5*(JAP NIC)

						if = {
							limit = {
								JAP = { EAI_THIS_is_threat_to_PREV = yes }
								set_temp_variable = { target_nic = JAP.num_of_naval_factories }
								all_of_scopes = { array = JAP.allies add_to_temp_variable = { target_nic = num_of_naval_factories } }
								multiply_temp_variable = { target_nic = 1.5 }
								round_temp_variable = target_nic
								check_variable = { EAI_BUILDING_num_NIC < target_nic }
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = target_nic }

							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_country_flag = EAI_BUILDING_construction_queue_NIC
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "Target 1.5*(JAP NIC)" }
						}

					###
				}

			### War time				

				else = {
				
					### Target NIC
		
						if = {
							limit = {
								set_temp_variable = { construction_speed = modifier@production_speed_industrial_complex_factor }
								add_to_temp_variable = { construction_speed = modifier@production_speed_buildings_factor }
								check_variable = { construction_speed < -0.1 }
							}
		
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 70 }
							set_temp_variable = { EAI_BUILDING_target_num_CIC = -1 }
							set_temp_variable = { EAI_BUILDING_target_num_MIC = -1 }
						}
		
					### Target 1.5*(JAP NIC)

						if = {
							limit = {
								JAP = { EAI_THIS_is_threat_to_PREV = yes }
								set_temp_variable = { target_nic = JAP.num_of_naval_factories }
								all_of_scopes = { array = JAP.allies add_to_temp_variable = { target_nic = num_of_naval_factories } }
								multiply_temp_variable = { target_nic = 1.5 }
								round_temp_variable = target_nic
								check_variable = { EAI_BUILDING_num_NIC < target_nic }
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = target_nic }

							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_country_flag = EAI_BUILDING_construction_queue_NIC
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "Target 1.5*(JAP NIC)" }
						}

					###
				}

			###
		}

	### ENG

		if = { limit = { tag = ENG }

			### Peace time

				if = { limit = { has_war = no }
				
					### Target CIC

						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_CIC < 45 }
							}
						
							set_temp_variable = { _target_ratio_CIC = 1.0 }
							set_temp_variable = { _target_ratio_MIC = 0.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ENG - CIC" }
						}

					### Target NIC - expand slowly

						else_if = {
							limit = {
								check_variable = { EAI_BUILDING_num_NIC < 62 }
							}

							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = EAI_BUILDING_num_NIC }
							add_to_temp_variable = { EAI_BUILDING_target_num_NIC = 2 }
							clamp_temp_variable = { var = EAI_BUILDING_target_num_NIC min = 0 max = 60 }

							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_country_flag = EAI_BUILDING_construction_queue_NIC
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ENG - Target NIC" }
						}

					### Target MIC

						else = {
				
							set_temp_variable = { _target_ratio_CIC = 0.0 }
							set_temp_variable = { _target_ratio_MIC = 1.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ENG - MIC" }
						}

					###
				}

			### War time

				else = {

					### Target NIC - rush NIC

						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_NIC < 62 } #Aigle2: From 30 to 50 to improve naval production
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = 62 }

							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_country_flag = EAI_BUILDING_construction_queue_NIC
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ENG - Target NIC" }
						}

					### Target 2*(AXIS NIC)

						else_if = {
							limit = {
								has_war_with = GER
								set_temp_variable = { target_nic = GER.num_of_naval_factories }
								if = { limit = { ITA = { EAI_THIS_is_threat_to_PREV = yes } }
									add_to_temp_variable = { target_nic = ITA.num_of_naval_factories }
								}
								multiply_temp_variable = { target_nic = 1 }
								round_temp_variable = target_nic
								check_variable = { EAI_BUILDING_num_NIC < target_nic }
							}
						
							set_temp_variable = { _lock_target_num_NIC = 1 }
							set_temp_variable = { EAI_BUILDING_target_num_NIC = target_nic }

							set_temp_variable = { EAI_BUILDING_target_num_CIC = 0 }
							set_country_flag = EAI_BUILDING_construction_queue_NIC
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "ENG - Target 2*(AXIS NIC)" }
						}

					###
				}
				
			###
		}

	### FRA

		if = { limit = { tag = FRA }

			### Peace time

				if = { limit = { has_war = no }

					### Target CIC
					
						if = {
							limit = {
								check_variable = { EAI_BUILDING_num_CIC < 45 }
							}
						
							set_temp_variable = { _target_ratio_CIC = 1.0 }
							set_temp_variable = { _target_ratio_MIC = 0.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "FRA - CIC" }
						}

					### Target MIC

						else = {
				
							set_temp_variable = { _target_ratio_CIC = 0.0 }
							set_temp_variable = { _target_ratio_MIC = 1.0 }
							set_temp_variable = { _target_ratio_NIC = 0.0 }
							EAI_BUILDING_FACTORY_force_target_ratio = yes
					
							if = { limit = { has_country_flag = EAI_building_logging } log = "FRA - MIC" }
						}

					###
				}

			### War time

				else = {

				}

			###
		}

	###

	#################################
	### Special case
	#################################

	### Needs more CIC

		if = { 
			limit = { 
				check_variable = { EAI_BUILDING_num_available_CIC < @EAI_MIN_AVAILABLE_NUM_CIC }
				check_variable = { EAI_BUILDING_target_num_CIC < EAI_BUILDING_num_CIC }
			}

			set_temp_variable = { _target_ratio_CIC = 1.0 }
			set_temp_variable = { _target_ratio_MIC = 0.0 }
			set_temp_variable = { _target_ratio_NIC = 0.0 }
			EAI_BUILDING_FACTORY_force_target_ratio = yes

			if = { limit = { has_country_flag = EAI_building_logging } log = "Needs more CIC" }
		}

	### Massive resource deficits

		if = {
			limit = {
				OR = {
					check_variable = { EAI_aluminium_resource_massive_shortage = 3 }
					check_variable = { EAI_rubber_resource_massive_shortage = 3 }
					check_variable = { EAI_steel_resource_massive_shortage = 3 }
				}
			}
		
			set_temp_variable = { _target_ratio_CIC = 1.0 }
			set_temp_variable = { _target_ratio_MIC = 0.0 }
			set_temp_variable = { _target_ratio_NIC = 0.0 }
			EAI_BUILDING_FACTORY_force_target_ratio = yes
		}

	###
}

##############################################################################################################################
### 	Synthetic Refineries
##############################################################################################################################

@EAI_FUEL_PRODUCED_TO_MAX_CONSUMED_RATIO_TO_BUILD_REF = 0.1
@EAI_FUEL_RESERVE_DAYS_TO_BUILD_REF = 180

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

EAI_BUILDING_REFINERY_general_building_target_modifiers = {

	set_temp_variable = { EAI_BUILDING_target_num_REF = EAI_BUILDING_num_REF }

	# Needs more refineries if producing too little fuel compared to max consumption AND fuel reserve days at max consumption is low
	if = { 
		limit = { 
			check_variable = { EAI_BUILDING_num_CIC > 50 }

			has_war = yes

			set_temp_variable = { EAI_refineries_needed_for_fuel = EAI_max_fuel_consumption_estimate }

			set_temp_variable = { EAI_fuel_produced = resource_produced@oil }
			add_to_temp_variable = { EAI_fuel_produced = resource_imported@oil }
			subtract_from_temp_variable = { EAI_fuel_produced = resource_exported@oil }
			multiply_temp_variable = { EAI_fuel_produced = 48 }

			if = { limit = { check_variable = { EAI_max_fuel_consumption_estimate > 0 } }	
				divide_temp_variable = { EAI_fuel_produced = EAI_max_fuel_consumption_estimate }
			}
			else = { always = no }
			check_variable = { EAI_fuel_produced < @EAI_FUEL_PRODUCED_TO_MAX_CONSUMED_RATIO_TO_BUILD_REF }

			set_temp_variable = { EAI_fuel_reserve = fuel_k }
			set_temp_variable = { EAI_max_fuel_consumption_estimate_k = EAI_max_fuel_consumption_estimate }
			if = { limit = { check_variable = { EAI_max_fuel_consumption_estimate_k > 0 } }	
				divide_temp_variable = { EAI_fuel_reserve = EAI_max_fuel_consumption_estimate_k }
			}
			else = { always = no }
			check_variable = { EAI_fuel_reserve < @EAI_FUEL_RESERVE_DAYS_TO_BUILD_REF }
		}

		add_to_temp_variable = { EAI_BUILDING_target_num_REF = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Needs more refineries if producing too little fuel compared to max consumption AND fuel reserve days at max consumption is low" }
	}

	# There has been a rubber shortage for a while, build REF
	if = { 
		limit = { 
			check_variable = { EAI_rubber_resource_shortage = 3 }
		}

		add_to_temp_variable = { EAI_BUILDING_target_num_REF = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "There has been a rubber shortage for a while, build REF" }
	}
}

EAI_BUILDING_REFINERY_special_building_target_modifiers = {

	# GER needs to build REF pre-emptively
	if = {
		limit = {
			tag = GER
			is_major = yes
			check_variable = { EAI_BUILDING_num_REF < 25 }
			date > 1937.6.1
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "GER needs to build refineries pre-emptively" }

		set_temp_variable = { EAI_BUILDING_target_num_REF = EAI_BUILDING_num_REF }
		add_to_temp_variable = { EAI_BUILDING_target_num_REF = 3 }
	}

	# USA needs to build a rubber industry to supply the allies later
	if = {
		limit = {
			tag = USA
			check_variable = { EAI_BUILDING_num_REF < 30 }
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "USA needs to build a rubber industry to supply the allies later" }

		set_temp_variable = { EAI_BUILDING_target_num_REF = EAI_BUILDING_num_REF }
		add_to_temp_variable = { EAI_BUILDING_target_num_REF = 3 }
	}

	# JAP avoid refineries vs china
	if = {
		limit = {
			tag = JAP
			has_war_with = CHI
			date < 1940.1.1
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "JAP avoid refineries vs china" }

		set_temp_variable = { EAI_BUILDING_target_num_REF = 0 }
	}
}

##############################################################################################################################
### 	Fuel Silos
##############################################################################################################################

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

EAI_BUILDING_SILO_general_building_target_modifiers = {

	set_temp_variable = { EAI_BUILDING_target_num_SILO = 0 }

	###

	# Build silos as needed (this logic looks weird)
	if = {
		limit = {
			check_variable = { EAI_BUILDING_num_CIC > 50 }
			date > @EAI_EARLY_GAME_BUILD_PERIOD
			has_war = yes

			check_variable = { EAI_max_fuel_consumption_estimate > 5000 }
			set_temp_variable = { EAI_num_silos_needed = num_of_military_factories }
			subtract_from_temp_variable = { EAI_num_silos_needed = resource_produced@oil }
			divide_temp_variable = { EAI_num_silos_needed = 6 }

			check_variable = { EAI_BUILDING_num_SILO < EAI_num_silos_needed }
		}

		set_temp_variable = { EAI_BUILDING_target_num_SILO = EAI_BUILDING_num_SILO }
		add_to_temp_variable = { EAI_BUILDING_target_num_SILO = 1 }

		if = { limit = { has_country_flag = EAI_building_logging } log = "Build silos as needed (this logic looks weird)" }
	}

	# JAP should focus on other industry against CHI
	if = {
		limit = {
			tag = JAP
			has_war_with = CHI
			date < 1940.1.1
		}

		if = { limit = { has_country_flag = EAI_building_logging } log = "JAP avoid silos vs china" }

		set_temp_variable = { EAI_BUILDING_target_num_SILO = 0 }
	}
}

EAI_BUILDING_SILO_special_building_target_modifiers = {

	
}

##############################################################################################################################
### 	Misc
##############################################################################################################################

EAI_BUILDING_misc_strategies = {

	EAI_BUILDING_fortifications_FIN = yes
}

EAI_BUILDING_fortifications_FIN = {

	if = {
		limit = {
			tag = FIN

			NOT = { has_country_flag = EAI_BUILDING_fortifications_FIN }

			SOV = { is_ai = no }
			
			num_of_civilian_factories > 15
		}

		set_country_flag = EAI_BUILDING_fortifications_FIN

		111 = {
			add_building_construction = { type = bunker level = 3 instant_build = no 
				province = { 
					id = 11105
				}
			}

			add_building_construction = { type = coastal_bunker level = 3 instant_build = no 
				province = { 
					id = 11105
				}
			}
		}
	
		928 = {
			add_building_construction = { type = bunker level = 7 instant_build = no 
				province = { 
					id = 6186
					id = 11056
					id = 3061
				}
			}
		}
	
		930 = {
			add_building_construction = { type = bunker level = 5 instant_build = no 
				province = { 
					id = 3131
					id = 37
					id = 3013
				}
			}
		}

		150 = {
			add_building_construction = { type = bunker level = 3 instant_build = no 
				province = { 
					id = 9106
					id = 3125
					id = 9104
					id = 11141
					id = 136
				}
			}
		}
	
		929 = {
			add_building_construction = { type = bunker level = 3 instant_build = no 
				province = { 
					id = 11121
					id = 6212
				}
			}
		}
	}
}