############################################################################################################
#	Expert AI mod - misc effects
############################################################################################################

@EAI_VERSION = 5.202

EAI_set_version = {

	set_variable = { global.EAI_version = @EAI_VERSION }
}

EAI_save_compatibility = {

	if = { 
		limit = { 
			check_variable = { global.EAI_days_elapsed > 2 }
			check_variable = { global.EAI_version < @EAI_VERSION }
		}

		### Save compatibility effects

			### 5.118

				if = { limit = { check_variable = { global.EAI_version < 5.118 } }
				
					EAI_MAP_save_original_capital_location = yes
				}

			### 5.115

				if = { limit = { check_variable = { global.EAI_version < 5.115 } }
				
					EAI_PC_set_global_variables = yes
					EAI_MAP_set_province_railway_connections = yes
					set_global_flag = EAI_delayed_startup_effects
				} 

			### 5.111

				if = { limit = { check_variable = { global.EAI_version < 5.111 } }
				
					EAI_MAP_set_province_river_connections = yes
					EAI_MAP_set_province_adjacencies = yes
				}

			### Variables that store a token value should probably be reset each update

				every_country = {
					EAI_PR_clear_priority_research_queue = yes
					EAI_ESP_clear_operation = yes
				}
				EAI_ESP_operation_types = yes
				clr_global_flag = EAI_MIO_add_design_team_init

			###

		###

		EAI_set_version = yes
	}
}

EAI_check_game_version = { # Show warning to players

	# if = {
	# 	limit = {
	# 		NOT = { check_variable = { SOV.modifier@consumer_goods_factor = 0.4 } }
	# 	}
		
	# 	country_event = EAI.5
	# }
}

###

EAI_MODE_set_challenging = {
	set_global_flag = EAI_challenging_mode

	set_global_flag = EAI_free_convoys
}
EAI_MODE_clear_challenging = {
	clr_global_flag = EAI_challenging_mode

	clr_global_flag = EAI_free_convoys
}

###

EAI_track_calendar_month = {

	if = { limit = { NOT = { has_global_flag = EAI_track_calendar_month_update } }

		if = { limit = { check_variable = { global.EAI_calendar_month = 0 } } 			set_variable = { global.EAI_calendar_month = 1 } 	set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #JAN

		else_if = { limit = { check_variable = { global.EAI_calendar_month = 1 } } 		set_variable = { global.EAI_calendar_month = 2 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 28 } } #FEB
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 2 } } 		set_variable = { global.EAI_calendar_month = 3 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #MAR
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 3 } } 		set_variable = { global.EAI_calendar_month = 4 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 30 } } #MAY
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 4 } } 		set_variable = { global.EAI_calendar_month = 5 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #APR
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 5 } } 		set_variable = { global.EAI_calendar_month = 6 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 30 } } #JUN
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 6 } } 		set_variable = { global.EAI_calendar_month = 7 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #JUL
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 7 } } 		set_variable = { global.EAI_calendar_month = 8 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #AUG
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 8 } } 		set_variable = { global.EAI_calendar_month = 9 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 30 } } #SEP
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 9 } } 		set_variable = { global.EAI_calendar_month = 10 }	set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #OCT
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 10 } } 	set_variable = { global.EAI_calendar_month = 11 }	set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 30 } } #NOV
		else_if = { limit = { check_variable = { global.EAI_calendar_month = 11 } } 	set_variable = { global.EAI_calendar_month = 12 }	set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #DEC
		else = { 																		set_variable = { global.EAI_calendar_month = 1 }		set_global_flag = { flag = EAI_track_calendar_month_update value = 1 days = 31 } } #JAN
	}
}

### Advisors

EAI_advisor_types = {

	set_variable = { global.EAI_silent_workhorse_TYPE = 1 }

	set_variable = { global.EAI_captain_of_industry_TYPE = 2 }

	set_variable = { global.EAI_war_industrialist_TYPE = 3 }

	set_variable = { global.EAI_head_of_intelligence_TYPE = 4 }

	set_variable = { global.EAI_popular_queen_TYPE = 5 }
	set_variable = { global.EAI_popular_figurehead_TYPE = 6 }
	set_variable = { global.EAI_marxist_fundementalist_TYPE = 7 }
	set_variable = { global.EAI_autocratic_archbishop_TYPE = 8 }
}

EAI_advisor_strategies = {

	if = { limit = { NOT = { EAI_ADVISOR_country_has_pick_order = yes } }

		clear_variable = EAI_get_advisor

		# silent_workhorse
		if = {
			limit = {
				has_available_idea_with_traits = { idea = silent_workhorse limit = 1 }
				NOT = { has_idea_with_trait = silent_workhorse }
			}

			set_variable = { EAI_get_advisor = global.EAI_silent_workhorse_TYPE }
		}

		# captain_of_industry
		else_if = {
			limit = {
				has_available_idea_with_traits = { idea = captain_of_industry limit = 1 }
				NOT = { has_idea_with_trait = captain_of_industry }
			}

			set_variable = { EAI_get_advisor = global.EAI_captain_of_industry_TYPE }
		}

		# head_of_intelligence
		else_if = {
			limit = {
				has_available_idea_with_traits = { idea = head_of_intelligence limit = 1 }
				NOT = { has_idea_with_trait = head_of_intelligence }
			}

			set_variable = { EAI_get_advisor = global.EAI_head_of_intelligence_TYPE }
		}

		# war_industrialist
		else_if = {
			limit = {
				has_available_idea_with_traits = { idea = war_industrialist limit = 1 }
				NOT = { has_idea_with_trait = war_industrialist }
			}

			set_variable = { EAI_get_advisor = global.EAI_war_industrialist_TYPE }
		}

		# popular_figurehead
		else_if = {
			limit = {
				has_available_idea_with_traits = { idea = popular_figurehead limit = 1 }
				NOT = { has_idea_with_trait = popular_figurehead }
			}

			set_variable = { EAI_get_advisor = global.EAI_popular_figurehead_TYPE }
		}
	}
}

###

EAI_get_side = { # var:_id var:_at_war arr:side_
	if = { limit = { EAI_TRIGGER_get_side = yes } }
}

EAI_get_sides = { # var:_id arr:_targets arr:our_side_ arr:their_side_
	clear_temp_array = our_side_
	clear_temp_array = their_side_
	set_temp_variable = { _id = THIS.id }
	EAI_get_side = yes
	for_each_loop = { array = side_ add_to_temp_array = { our_side_ = v } }

	for_each_scope_loop = { array = _targets
		set_temp_variable = { _id = THIS.id }
		EAI_get_side = yes
		for_each_loop = { array = side_ if = { limit = { NOT = { is_in_array = { their_side_ = v } } } add_to_temp_array = { their_side_ = v } } }
	}
	clear_temp_array = _targets
}

EAI_get_state_controllers = { # arr:_states var:_at_war var:_same_side controllers_
	if = { limit = { EAI_TRIGGER_get_state_controllers = yes } }
}

###

# Transitions between fighter variants based on resources
EAI_fighter_variant_selection = {
	if = {
		limit = {
			NOT = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_E } }
		}
		
		set_temp_variable = { rubber_excess = 0 }
		set_temp_variable = { aluminium_shortage = 0 }

		### Normal variant

		if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A } }

			# Has aluminium shortage
			if = {
				limit = {
					check_variable = { EAI_aluminium_resource_shortage > 2 }
					NOT = { EAI_STRATEGY_ignore_shortage_aircraft_design = yes }
				}

				set_temp_variable = { aluminium_shortage = 1 }
			}

			# Can afford the estimated extra rubber consumption
			if = {
				limit = {
					OR = {
						AND = {
							check_variable = { resource@rubber > 0 }
							set_temp_variable = { rubber_needed = resource_consumed@rubber }
							multiply_temp_variable = { rubber_needed = 1.5 }#from 2 to 1.5 as rubber based variants are critical to get
							check_variable = { resource@rubber > rubber_needed }
						}
						EAI_STRATEGY_CHL_use_rubber_heavy_fighter_design = yes
					}
				}

				set_temp_variable = { rubber_excess = 1 }
			}
		}

		### More rubber variant

		if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B } }

			# Has aluminium shortage
			if = {
				limit = {
					check_variable = { EAI_aluminium_resource_shortage > 2 }
					NOT = { EAI_STRATEGY_ignore_shortage_aircraft_design = yes }
				}

				set_temp_variable = { aluminium_shortage = 1 }
			}

			# Doesn't have rubber shortage
			if = {
				limit = {
					OR = {
						NOT = { check_variable = { EAI_rubber_resource_shortage > 2 } }
						EAI_STRATEGY_CHL_use_rubber_heavy_fighter_design = yes
					}
				}

				set_temp_variable = { rubber_excess = 1 }
			}
		}

		### Less aluminium variant

		if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C } }

			# Can afford the estimated extra aluminium consumption
			if = {
				limit = {
					NOT = {
						AND = {
							check_variable = { resource@aluminium > 0 }
							set_temp_variable = { aluminium_needed = resource_consumed@aluminium }
							multiply_temp_variable = { aluminium_needed = 3 }
							check_variable = { resource@aluminium > aluminium_needed }
						}
					}
					NOT = {EAI_STRATEGY_ignore_shortage_aircraft_design = yes}	
				}

				set_temp_variable = { aluminium_shortage = 1 }
			}

			# Can afford the estimated extra rubber consumption
			if = {
				limit = {
					OR = {
						AND = {
							check_variable = { resource@rubber > 0 }
							set_temp_variable = { rubber_needed = resource_consumed@rubber }
							multiply_temp_variable = { rubber_needed = 2 }
							check_variable = { resource@rubber > rubber_needed }
						}
						EAI_STRATEGY_CHL_use_rubber_heavy_fighter_design = yes
					}
				}

				set_temp_variable = { rubber_excess = 1 }
			}
		}

		### More rubber + less aluminium variant

		if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D } }

			# Doesn't have rubber shortage
			if = {
				limit = {
					OR = {
						NOT = { check_variable = { EAI_rubber_resource_shortage > 2 } }
						EAI_STRATEGY_CHL_use_rubber_heavy_fighter_design = yes
					}
				}

				set_temp_variable = { rubber_excess = 1 }
			}

			# Can afford the estimated extra aluminium consumption
			if = {
				limit = {
					NOT = {
						AND = {
							check_variable = { resource@aluminium > 0 }
							set_temp_variable = { aluminium_needed = resource_consumed@aluminium }
							multiply_temp_variable = { aluminium_needed = 3 }
							check_variable = { resource@aluminium > aluminium_needed }
						}
					}
					NOT = { EAI_STRATEGY_ignore_shortage_aircraft_design = yes }
				}

				set_temp_variable = { aluminium_shortage = 1 }
			}
		}

		###### 

		if = { limit = { NOT = { has_country_flag = EAI_fighter_variant_selection_timeout } }

			if = { limit = { check_variable = { aluminium_shortage = 1 } check_variable = { rubber_excess = 1 } }

				if = { limit = { NOT = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D } } } 
					
					set_country_flag = { flag = EAI_fighter_variant_selection_timeout value = 1 days = 90 }
					set_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D }

					# log = "[GetYear] [GetMonth] | AI | [Root.GetName] | FIGHTER VARIANT SELECTION: More rubber + less aluminium"
				}
			}

			else_if = { limit = { check_variable = { aluminium_shortage = 1 } }

				if = { limit = { NOT = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C } } } 
					
					set_country_flag = { flag = EAI_fighter_variant_selection_timeout value = 1 days = 90 }
					set_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C }

					# log = "[GetYear] [GetMonth] | AI | [Root.GetName] | FIGHTER VARIANT SELECTION: Less aluminium"
				}
			}

			else_if = { limit = { check_variable = { rubber_excess = 1 } }

				if = { limit = { NOT = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B } } } 

					set_country_flag = { flag = EAI_fighter_variant_selection_timeout value = 1 days = 90 }
					set_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B }

					# log = "[GetYear] [GetMonth] | AI | [Root.GetName] | FIGHTER VARIANT SELECTION: More rubber"
				}
			}

			else = {

				if = { limit = { NOT = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A } } } 
					
					set_country_flag = { flag = EAI_fighter_variant_selection_timeout value = 1 days = 90 }
					set_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A }

					# log = "[GetYear] [GetMonth] | AI | [Root.GetName] | FIGHTER VARIANT SELECTION: Normal type"
				}
			}
		}
	}
}

###

EAI_MIO_add_design_team_update = {

	@EAI_MIO_min_army_xp_to_upgrade = 100
	@EAI_MIO_min_air_xp_to_upgrade = 75
	@EAI_MIO_trait_diff_max_xp = 200
	@EAI_MIO_trait_diff_scaled_xp = 150
	@EAI_MIO_trait_diff_scaled_num = 8

	### Init

		if = { limit = { NOT = { has_global_flag = EAI_MIO_add_design_team_init } }
			set_global_flag = EAI_MIO_add_design_team_init

			### Eq categories

				set_variable = { global.EAI_MIO_eq_cat_INF = 1 }
				set_variable = { global.EAI_MIO_eq_cat_ART = 2 }
				set_variable = { global.EAI_MIO_eq_cat_SUP = 3 }
				set_variable = { global.EAI_MIO_eq_cat_AA = 4 }
				set_variable = { global.EAI_MIO_eq_cat_AT = 5 }
				set_variable = { global.EAI_MIO_eq_cat_MOT = 6 }
				set_variable = { global.EAI_MIO_eq_cat_MECH = 7 }
				set_variable = { global.EAI_MIO_eq_cat_AMECH = 8 }

				set_variable = { global.EAI_MIO_eq_cat_FTR = 9 }
				set_variable = { global.EAI_MIO_eq_cat_LARM = 10 }
				set_variable = { global.EAI_MIO_eq_cat_MARM = 11 }
				set_variable = { global.EAI_MIO_eq_cat_HARM = 12 }

			### Set country design teams #TOKEN

				GER = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:GER_mauser_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:GER_rheinmetall_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = token:GER_carl_walther_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:GER_rheinmetall_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:GER_rheinmetall_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:GER_opel_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:GER_opel_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:GER_opel_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:GER_messerschmitt_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:GER_daimler_benz_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:GER_man_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = token:GER_porsche_organization }
				}

				ITA = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:ITA_beretta_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:ITA_breda_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = token:ITA_brescia_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:ITA_breda_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:ITA_breda_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:ITA_lancia_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:ITA_lancia_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:ITA_lancia_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:ITA_macchi_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:ITA_fiat_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:ITA_fiat_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = 0 }
				}

				JAP = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:JAP_kokura_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:JAP_osaka_army_arsenal_artillery_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = 0 }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:JAP_osaka_army_arsenal_artillery_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:JAP_osaka_army_arsenal_artillery_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:JAP_nissan_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:JAP_nissan_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:JAP_nissan_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:JAP_mitsubishi_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:JAP_osaka_army_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:JAP_osaka_army_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = 0 }
				}

				ENG = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:ENG_rsaf_enfield_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:ENG_royal_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = 0 }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:ENG_royal_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:ENG_royal_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:ENG_vauxhall_automotive_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:ENG_vauxhall_automotive_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:ENG_vauxhall_automotive_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:ENG_supermarine_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:ENG_vauxhall_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:ENG_vickers_armstrong_eng_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = token:ENG_vauxhall_organization }
				}

				FRA = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:FRA_mas_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:FRA_schneider_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = 0 }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:FRA_schneider_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:FRA_schneider_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:FRA_somua_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:FRA_somua_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:FRA_somua_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:FRA_levasseur_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:FRA_hotchkiss_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:FRA_apx_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = token:FRA_renault_organization }
				}

				USA = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:USA_springfield_armory_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:USA_rock_island_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = 0 }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:USA_rock_island_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:USA_rock_island_arsenal_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:USA_ford_motor_company_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:USA_ford_motor_company_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:USA_ford_motor_company_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:USA_north_american_aviation_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:USA_marmon_herrington_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:USA_chrysler_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = token:USA_marmon_herrington_organization }
				}

				SOV = {
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = token:SOV_tula_arms_plant_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = token:SOV_grabin_design_bureau_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = token:SOV_mytishchy_machine_building_plant_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = token:SOV_grabin_design_bureau_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = token:SOV_grabin_design_bureau_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = token:SOV_gaz_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = token:SOV_gaz_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = token:SOV_gaz_organization }

					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = token:SOV_mig_design_bureau_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = token:SOV_okmo_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = token:SOV_okmo_organization }
					set_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = token:SOV_okmo_organization }
				}

			### Target equipment
			
				### Add teams to equipment in this order (only works well for equipment that doesn't have a designer)

				clear_array = global.EAI_MIO_army_design_team_targets
				clear_array = global.EAI_MIO_army_design_team_target_cats

				### Army
					
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:advanced_infantry_weapons } # infantry_equipment_3
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_INF }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:improved_infantry_weapons } # infantry_equipment_2
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_INF }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:infantry_weapons1 } # infantry_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_INF }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:artillery4 } # artillery_equipment_3
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_ART }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:artillery1 } # artillery_equipment_2
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_ART }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:tech_support } # support_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_SUP }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:antiair5 } # anti_air_equipment_3
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AA }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:antiair2 } # anti_air_equipment_2
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AA }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:interwar_antiair } # anti_air_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AA }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:antitank5 } # anti_tank_equipment_3
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AT }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:antitank2 } # anti_tank_equipment_2
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AT }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:interwar_antitank } # anti_tank_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AT }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:motorised_infantry } # motorized_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_MOT }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:tech_trucks } # motorized_equipment_0
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_MOT }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:mechanised_infantry3 } # mechanized_equipment_3
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_MECH }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:mechanised_infantry2 } # mechanized_equipment_2
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_MECH }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:mechanised_infantry } # mechanized_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_MECH }

					add_to_array = { global.EAI_MIO_army_design_team_targets = token:amphibious_mechanized_infantry_2 } # amphibious_mechanized_equipment_2
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AMECH }
					add_to_array = { global.EAI_MIO_army_design_team_targets = token:amphibious_mechanized_infantry } # amphibious_mechanized_equipment_1
					add_to_array = { global.EAI_MIO_army_design_team_target_cats = global.EAI_MIO_eq_cat_AMECH }

				###
			###
		}

	### Update

		if = {
			limit = {
				OR = {
					tag = GER
					tag = ITA
					tag = JAP
					tag = ENG
					tag = USA
					tag = FRA
					tag = SOV
				}
			}

			### Track MIO traits

				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_INF = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_INF = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_INF.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_ART = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_ART = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_ART.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_SUP = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_SUP = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_SUP.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AA = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_AA = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_AA.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AT = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_AT = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_AT.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MOT = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_MOT = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_MOT.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MECH = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_MECH = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_MECH.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_AMECH = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_FTR = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_FTR = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_FTR.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_LARM = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_LARM = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_LARM.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_MARM = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_MARM = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_MARM.GetTokenKey]"
					}
				}
				if = { limit = { NOT = { check_variable = { EAI_MIO_team^global.EAI_MIO_eq_cat_HARM = 0 } } }
					meta_effect = {
						text = { mio:[x] = { set_variable = { ROOT.EAI_MIO_team_cat_traits^global.EAI_MIO_eq_cat_HARM = number_of_unlocked_traits } } }
						x = "[?EAI_MIO_team^global.EAI_MIO_eq_cat_HARM.GetTokenKey]"
					}
				}

			### Add teams

				### Non designer

					if = {
						limit = {
							has_army_experience > @EAI_MIO_min_army_xp_to_upgrade
						}

						set_temp_variable = { found_tech_in_cat = 0 }
						for_each_loop = { array = global.EAI_MIO_army_design_team_targets index = i value = _tech 
							if = { 
								limit = { 
									NOT = { check_variable = { found_tech_in_cat = global.EAI_MIO_army_design_team_target_cats^i } }
									has_tech = var:_tech
								} 
								set_temp_variable = { found_tech_in_cat = global.EAI_MIO_army_design_team_target_cats^i }
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^found_tech_in_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_last_upgrade_traits^_tech }
								if = { 
									limit = { 
										NOT = { check_variable = { EAI_MIO_team^found_tech_in_cat = 0 } }
										set_temp_variable = { req_xp = @EAI_MIO_trait_diff_max_xp }
										set_temp_variable = { _val_ = trait_diff }
										set_temp_variable = { _min = 0 }
										set_temp_variable = { _max = @EAI_MIO_trait_diff_scaled_num }
										EAI_MATH_TRIGGER_normalize = yes
										set_temp_variable = { _scale = @EAI_MIO_trait_diff_scaled_xp }
										EAI_MATH_TRIGGER_scale = yes
										subtract_from_temp_variable = { req_xp = _val_ }
										NOT = { check_variable = { trait_diff < 1 } } 
										has_army_experience > var:req_xp
									}
									EAI_MIO_add_design_team_ARMY = yes 
								}
							}
						}
					}

				### Designer

					if = {
						limit = {
							OR = {
								has_air_experience > @EAI_MIO_min_air_xp_to_upgrade # Can assume the AI created this design by now
							}
							EAI_FTR_military_focus = yes #Not useful for heavy fighters
						}
						EAI_MIO_create_fighter_variant_with_team = yes
					}

				###
			###
		}
	###
}

EAI_MIO_add_design_team_ARMY = {

	set_temp_variable = { team_added = 0 }

	### Infantry

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_INF } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_INF }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:infantry_weapons1 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:improved_infantry_weapons } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_2 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:advanced_infantry_weapons } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_3 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "infantry_equipment"
			}
		}

	### Artillery

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_ART } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_ART }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:artillery1 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_2 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:artillery4 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_3 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "artillery_equipment"
			}
		}

	### Support

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_SUP } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_SUP }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:tech_support } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "support_equipment"
			}
		}

	### Anti air

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_AA } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_AA }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:interwar_antiair } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:antiair2 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_2 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:antiair5 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_3 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "anti_air_equipment"
			}
		}

	### Anti tank

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_AT } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_AT }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:interwar_antitank } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:antitank2 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_2 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:antitank5 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_3 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "anti_tank_equipment"
			}
		}

	### Motorized

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_MOT } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_MOT }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:tech_trucks } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_0 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:motorised_infantry } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "motorized_equipment"
			}
		}

	### Mechanized

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_MECH } }

			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_MECH }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:mechanised_infantry } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:mechanised_infantry2 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_2 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:mechanised_infantry3 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_3 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "mechanized_equipment"
			}
		}

	### Amphibious

		if = { limit = { check_variable = { found_tech_in_cat = global.EAI_MIO_eq_cat_AMECH } }
		
			set_temp_variable = { mio_team = EAI_MIO_team^global.EAI_MIO_eq_cat_AMECH }
			meta_effect = {
				text = {
					if = { limit = { check_variable = { _tech = token:amphibious_mechanized_infantry } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_1 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
					if = { limit = { check_variable = { _tech = token:amphibious_mechanized_infantry_2 } mio:[x] = { is_mio_available = yes } NOT = { check_variable = { mio_team = 0 } } } create_equipment_variant = { type = [y]_2 mark_older_equipment_obsolete = yes design_team = mio:[x] } set_temp_variable = { team_added = 1 } }
				}
				x = "[?mio_team.GetTokenKey]"
				y = "amphibious_mechanized_equipment"
			}
		}

	###

	if = { limit = { check_variable = { team_added = 1 } } 
		# log = "[GetYear] [GetMonth] | AI | [Root.GetName] | MIO: added to equipment from [?_tech.GetTokenKey] in cat=[?found_tech_in_cat] team = [?mio_team.GetTokenKey]"
		# log = "last traits=[?EAI_MIO_last_upgrade_traits^_tech] current=[?EAI_MIO_team_cat_traits^found_tech_in_cat]"
		army_experience = -5
		set_variable = { EAI_MIO_last_upgrade_traits^_tech = EAI_MIO_team_cat_traits^found_tech_in_cat }
	}
}

EAI_MIO_create_fighter_variant_with_team = {

	set_temp_variable = { team_added = 0 }
	set_temp_variable = { _tech_cat = global.EAI_MIO_eq_cat_FTR }
	
	### Fighter 4

		if = {
			limit = {
				set_temp_variable = { _tech = token:advanced_small_airframe }
				has_tech = var:_tech 
				has_tech = jet_engines
			}

			if = {
				limit = {
					has_tech = aa_hmg
					has_tech = range_improvements
					has_tech = survivability_studies
				}
			
				### Variant A
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A } }
	
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_4
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = armor_plate_small
											special_type_slot_4 = empty
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant B
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B } }
	
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_4
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = self_sealing_fuel_tanks_small
											special_type_slot_4 = armor_plate_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant C
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C } }
	
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_4
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = armor_plate_small
											special_type_slot_2 = armor_plate_small
											special_type_slot_3 = fuel_tanks_small
											special_type_slot_4 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant D
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D } }
	
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_4
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = self_sealing_fuel_tanks_small
											special_type_slot_3 = armor_plate_small
											special_type_slot_4 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant E

					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_E } }

						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_4_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_4
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = self_sealing_fuel_tanks_small
											special_type_slot_4 = armor_plate_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
				}
			}
		}

	### Fighter 3

		else_if = {
			limit = {
				set_temp_variable = { _tech = token:advanced_small_airframe }
				has_tech = var:_tech 
				has_tech = engines_4
			}

			if = {
				limit = {
					has_tech = aa_hmg
					has_tech = range_improvements
					has_tech = survivability_studies
				}
			
				### Variant A
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A } }
					
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_3
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = armor_plate_small
											special_type_slot_4 = armor_plate_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant B
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B } }
					
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_3
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = self_sealing_fuel_tanks_small
											special_type_slot_4 = armor_plate_small										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant C
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C } }
					
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_3
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = armor_plate_small
											special_type_slot_3 = armor_plate_small
											special_type_slot_4 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant D
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D } }
					
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_3
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = self_sealing_fuel_tanks_small
											special_type_slot_2 = armor_plate_small
											special_type_slot_3 = armor_plate_small
											special_type_slot_4 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant E
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_E } }
						
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_3_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_3
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_4_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = self_sealing_fuel_tanks_small
											special_type_slot_4 = armor_plate_small										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
			}
		}

	### Fighter 2

		else_if = {
			limit = {
				set_temp_variable = { _tech = token:improved_small_airframe }
				has_tech = var:_tech 
				has_tech = engines_3
			}

			if = {
				limit = {
					has_tech = aa_hmg
					has_tech = range_improvements
					has_tech = survivability_studies
				}
			
				### Variant A
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A } }
	
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_2
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_3_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = armor_plate_small
											special_type_slot_3 = armor_plate_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant B
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B } }
	
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_2
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_3_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = self_sealing_fuel_tanks_small
											special_type_slot_3 = armor_plate_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant C
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C } }
	
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_2
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_3_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = armor_plate_small
											special_type_slot_3 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant D
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D } }
	
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_2
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = light_mg_4x
											engine_type_slot = engine_3_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = self_sealing_fuel_tanks_small
											special_type_slot_3 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant E
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_E } }
		
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_2_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_2
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = heavy_mg_4x
											fixed_auxiliary_weapon_slot_2 = heavy_mg_4x
											engine_type_slot = engine_3_1x
											special_type_slot_1 = fuel_tanks_small
											special_type_slot_2 = fuel_tanks_small
											special_type_slot_3 = self_sealing_fuel_tanks_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
			}
		}

	### Fighter 1

		else_if = {
			limit = {
				set_temp_variable = { _tech = token:basic_small_airframe }
				has_tech = var:_tech 
				has_tech = engines_2
			}

			if = {
				limit = {
					has_tech = aa_hmg
					has_tech = range_improvements
					has_tech = survivability_studies
				}
			
				### Variant A
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_A } }
	
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_A = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_1
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = light_mg_4x
											engine_type_slot = engine_2_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = armor_plate_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant B
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_B } }
	
						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_B = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_1
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = light_mg_4x
											engine_type_slot = engine_2_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = self_sealing_fuel_tanks_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant C
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_C } }
	
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_C = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_1
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = light_mg_4x
											engine_type_slot = engine_2_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant D
	
					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_D } }
	
						if = {
							limit = {
								has_tech = aircraft_construction

								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_D = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_1
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = light_mg_4x
											engine_type_slot = engine_2_1x
											special_type_slot_1 = self_sealing_fuel_tanks_small
											special_type_slot_2 = non_strategic_materials_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
	
				### Variant E

					if = { limit = { check_variable = { EAI_fighter_variant = global.EAI_FIGHTER_VARIANT_E } }

						if = {
							limit = {
								set_temp_variable = { trait_diff = EAI_MIO_team_cat_traits^_tech_cat }
								subtract_from_temp_variable = { trait_diff = EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E }
								OR = {
									NOT = { check_variable = { trait_diff < 3 } }
									AND = {
										NOT = { check_variable = { trait_diff < 1 } }
										has_air_experience > 200
									}
								}
							}
							set_variable = { EAI_MIO_fighter_1_last_upgrade_traits^global.EAI_FIGHTER_VARIANT_E = EAI_MIO_team_cat_traits^_tech_cat }
							set_temp_variable = { team_added = 1 }
							meta_effect = {
								text = {
									create_equipment_variant = {
										type = small_plane_airframe_1
										mark_older_equipment_obsolete = yes
										modules = {
											fixed_main_weapon_slot = heavy_mg_4x
											fixed_auxiliary_weapon_slot_1 = light_mg_4x
											engine_type_slot = engine_2_1x
											special_type_slot_1 = drop_tanks
											special_type_slot_2 = self_sealing_fuel_tanks_small
										}
										design_team = mio:[x]
									}
								}
								x = "[?EAI_MIO_team^_tech_cat.GetTokenKey]"
							}
						}
					}
			}
		}

	###

	if = { limit = { check_variable = { team_added = 1 } } 
		# log = "[GetYear] [GetMonth] | AI | [Root.GetName] | MIO: added to equipment from [?_tech.GetTokenKey] in cat=[?_tech_cat] team = [?EAI_MIO_team^_tech_cat.GetTokenKey]"
		# log = "current=[?EAI_MIO_team_cat_traits^_tech_cat]"
		air_experience = -5
	}
}

###

EAI_build_armies_fix = {

	if = {
		limit = {
			NOT = { has_country_flag = EAI_infantry_division_limit }
			
			# Build amount
			set_temp_variable = { build_manpower = EAI_division_cap_10k }
			multiply_temp_variable = { build_manpower = 10 }
			clamp_temp_variable = { var = build_manpower min = 0 max = 350 }
			check_variable = { deployed_army_manpower_k < build_manpower }

			# Has enough equipment
			OR = {
				AND = {
					has_war = no
					check_variable = { EAI_fielded_eq_ratio > 0.9 }
				}
				AND = {
					has_war = yes
					check_variable = { EAI_reserve_eq_ratio > 0.25 }
				}
			}

			# Has 40% of fielded army manpower in reserve manpower
			if = { limit = { check_variable = { deployed_army_manpower_k > 0 } }
			
				set_temp_variable = { manpower_reserve = manpower_k }
				divide_temp_variable = { manpower_reserve = deployed_army_manpower_k }
				OR = {
					AND = {
						has_war = yes
						check_variable = { manpower_reserve > 0.4 }
					}
					AND = {
						has_war = no
						check_variable = { manpower_reserve > 0.2 }
					}
				}
			}
		}

		set_country_flag = EAI_build_armies_fix
	}
	else = { clr_country_flag = EAI_build_armies_fix }
}

###

EAI_refresh_gui = {

	if = { limit = { check_variable = { ROOT.EAI_refresh_gui = 0 } } set_variable = { ROOT.EAI_refresh_gui = 1 } }
	else = { set_variable = { ROOT.EAI_refresh_gui = 0 } }
}

###

EAI_num_fighters_deployed = { # num_deployed_planes_with_type@fighter does not work

	set_temp_variable = { val_min = 0 }
    set_temp_variable = { val_max = 200000 }
    set_temp_variable = { guess = val_max }
    divide_temp_variable = { guess = 2 }

    while_loop_effect = { limit = { always = yes }

        if = {
            limit = {
                meta_trigger = {
					text = { has_deployed_air_force_size = { size > [x] type = fighter } }
					x = "[?guess]"
				}
            }

            set_temp_variable = { val_min = guess }
			add_to_temp_variable = { val_min = 1 }

            set_temp_variable = { guess = val_max }
            subtract_from_temp_variable = { guess = val_min }
			
            divide_temp_variable = { guess = 2 }
            add_to_temp_variable = { guess = val_min }
        }

        else_if = {
            limit = {
                meta_trigger = {
					text = { has_deployed_air_force_size = { size < [x] type = fighter } }
					x = "[?guess]"
				}
            }

            set_temp_variable = { val_max = guess }
			add_to_temp_variable = { val_max = -1 }

			set_temp_variable = { guess = val_max }
            subtract_from_temp_variable = { guess = val_min }

            divide_temp_variable = { guess = 2 }
            add_to_temp_variable = { guess = val_min }
        }

        else = { set_temp_variable = { break = 1 } }

		meta_effect = {
			text = { set_temp_variable = { guess = [x] } }
			x = "[?guess|0]"
		}
    }

	round_temp_variable = guess
    set_variable = { num_fighters_deployed = guess }

	###

	set_temp_variable = { val_min = 0 }
    set_temp_variable = { val_max = 200000 }
    set_temp_variable = { guess = val_max }
    divide_temp_variable = { guess = 2 }

    while_loop_effect = { limit = { always = yes }

        if = {
            limit = {
                meta_trigger = {
					text = { has_deployed_air_force_size = { size > [x] type = heavy_fighter } }
					x = "[?guess]"
				}
            }

            set_temp_variable = { val_min = guess }
			add_to_temp_variable = { val_min = 1 }

            set_temp_variable = { guess = val_max }
            subtract_from_temp_variable = { guess = val_min }
			
            divide_temp_variable = { guess = 2 }
            add_to_temp_variable = { guess = val_min }
        }

        else_if = {
            limit = {
                meta_trigger = {
					text = { has_deployed_air_force_size = { size < [x] type = heavy_fighter } }
					x = "[?guess]"
				}
            }

            set_temp_variable = { val_max = guess }
			add_to_temp_variable = { val_max = -1 }

			set_temp_variable = { guess = val_max }
            subtract_from_temp_variable = { guess = val_min }

            divide_temp_variable = { guess = 2 }
            add_to_temp_variable = { guess = val_min }
        }

        else = { set_temp_variable = { break = 1 } }

		meta_effect = {
			text = { set_temp_variable = { guess = [x] } }
			x = "[?guess|0]"
		}
    }

	round_temp_variable = guess
    add_to_variable = { num_fighters_deployed = guess }
}

###

EAI_calculate_fielded_eq_ratio = {

	### Infantry

		set_temp_variable = { equipment_count = 0 }
		set_temp_variable = { tot_fielded_eq_ratio = 0 }

		### Good enough indicator of the enemy eq. strength

		set_temp_variable = { fielded_eq_ratio = num_equipment_in_armies_k@infantry_equipment }
		divide_temp_variable = { fielded_eq_ratio = num_target_equipment_in_armies_k@infantry_equipment }
		add_to_temp_variable = { tot_fielded_eq_ratio = fielded_eq_ratio }
		add_to_temp_variable = { equipment_count = 1 }

		# if = { limit = { check_variable = { num_target_equipment_in_armies_k@artillery_equipment > 1 } }

		# 	set_temp_variable = { fielded_eq_ratio = num_equipment_in_armies_k@artillery_equipment }
		# 	divide_temp_variable = { fielded_eq_ratio = num_target_equipment_in_armies_k@artillery_equipment }
		# 	add_to_temp_variable = { tot_fielded_eq_ratio = fielded_eq_ratio }
		# 	add_to_temp_variable = { equipment_count = 1 }
		# }

		###

		divide_temp_variable = { tot_fielded_eq_ratio = equipment_count }

		set_variable = { EAI_fielded_eq_ratio = tot_fielded_eq_ratio }

		if = { limit = { has_country_flag = EAI_equipment_logging} log = "[GetYear] [GetMonth] | AI | [Root.GetName] | EQUIPMENT: EAI_fielded_eq_ratio = [?EAI_fielded_eq_ratio|%0]" }

	### Artillery
		
		if = { limit = { check_variable = { deployed_army_manpower_k > 0 } }
			set_temp_variable = { tmp = num_target_equipment_in_armies_k@artillery_equipment }
			multiply_temp_variable = { tmp = 1000 }
			divide_temp_variable = { tmp = deployed_army_manpower_k }
			if = { limit = { check_variable = { tmp > 0.8 } check_variable = { num_equipment@artillery_equipment < 1000 } }
				set_country_flag = EAI_fielded_eq_ratio_artillery_wanted
			}
			else = { clr_country_flag = EAI_fielded_eq_ratio_artillery_wanted }
		}

		if = { limit = { check_variable = { num_target_equipment_in_armies_k@artillery_equipment > 0 } }
			set_variable = { EAI_fielded_eq_ratio_artillery = num_equipment_in_armies_k@artillery_equipment }
			divide_variable = { EAI_fielded_eq_ratio_artillery = num_target_equipment_in_armies_k@artillery_equipment }
		}
		else = { set_variable = { EAI_fielded_eq_ratio_artillery = 1 } }

	### Support

		if = { limit = { check_variable = { deployed_army_manpower_k > 0 } }
			set_temp_variable = { tmp = num_target_equipment_in_armies_k@support_equipment }
			multiply_temp_variable = { tmp = 1000 }
			divide_temp_variable = { tmp = deployed_army_manpower_k }
			if = { limit = { check_variable = { tmp > 1.0 } check_variable = { num_equipment@support_equipment < 1000 } }
				set_country_flag = EAI_fielded_eq_ratio_support_wanted
			}
			else = { clr_country_flag = EAI_fielded_eq_ratio_support_wanted }
		}

		if = { limit = { check_variable = { num_target_equipment_in_armies_k@support_equipment > 0 } }
			set_variable = { EAI_fielded_eq_ratio_support = num_equipment_in_armies_k@support_equipment }
			divide_variable = { EAI_fielded_eq_ratio_support = num_target_equipment_in_armies_k@support_equipment }
		}
		else = { set_variable = { EAI_fielded_eq_ratio_support = 1 } }
	
	### Mechanized

		if = { limit = { check_variable = { num_target_equipment_in_armies_k@mechanized_equipment > 0 } }
			set_variable = { EAI_fielded_eq_ratio_mechanized = num_equipment_in_armies_k@mechanized_equipment }
			divide_variable = { EAI_fielded_eq_ratio_mechanized = num_target_equipment_in_armies_k@mechanized_equipment }
		}
		else = { set_variable = { EAI_fielded_eq_ratio_mechanized = 1 } }
	
	### Modern

		if = { limit = { check_variable = { num_target_equipment_in_armies_k@modern_tank_chassis > 0 } }
			set_variable = { EAI_fielded_eq_ratio_modern = num_equipment_in_armies_k@modern_tank_chassis }
			divide_variable = { EAI_fielded_eq_ratio_modern = num_target_equipment_in_armies_k@modern_tank_chassis }
		}
		else = { set_variable = { EAI_fielded_eq_ratio_modern = 1 } }
	
	###
}

EAI_calculate_reserve_eq_ratio = {

	set_temp_variable = { reserve_eq_ratio = num_equipment@infantry_equipment }
	divide_temp_variable = { reserve_eq_ratio = 1000 }

	divide_temp_variable = { reserve_eq_ratio = num_equipment_in_armies_k@infantry_equipment }

	###

	set_variable = { EAI_reserve_eq_ratio = reserve_eq_ratio }

	if = { limit = { has_country_flag = EAI_equipment_logging} log = "[GetYear] [GetMonth] | AI | [Root.GetName] | EQUIPMENT: EAI_reserve_eq_ratio = [?EAI_reserve_eq_ratio|%0]" }
}

EAI_calculate_reserve_mp_ratio = {

	set_temp_variable = { reserve_mp_ratio = manpower_k }
	if = { limit = { check_variable = { deployed_army_manpower_k > 0 } }
		divide_temp_variable = { reserve_mp_ratio = deployed_army_manpower_k }
	}
	else = {
		set_temp_variable = { reserve_mp_ratio = 1 }
	}

	###

	set_variable = { EAI_reserve_mp_ratio = reserve_mp_ratio }

	if = { limit = { has_country_flag = EAI_equipment_logging} log = "[GetYear] [GetMonth] | AI | [Root.GetName] | EQUIPMENT: EAI_reserve_mp_ratio = [?EAI_reserve_mp_ratio|%0]" }
}

# command power cost when changing an ally's military focuses
EAI_calculate_config_cp_cost = {

	if = { 		limit = { strength_ratio = { tag = ROOT ratio > 1.0 } } 			set_variable = { ROOT.EAI_config_cp_cost = 100 } }
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.9 } } 			set_variable = { ROOT.EAI_config_cp_cost = 90 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.8 } } 			set_variable = { ROOT.EAI_config_cp_cost = 80 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.7 } } 			set_variable = { ROOT.EAI_config_cp_cost = 70 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.6 } } 			set_variable = { ROOT.EAI_config_cp_cost = 60 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.5 } } 			set_variable = { ROOT.EAI_config_cp_cost = 50 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.4 } } 			set_variable = { ROOT.EAI_config_cp_cost = 40 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.3 } } 			set_variable = { ROOT.EAI_config_cp_cost = 30 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.2 } } 			set_variable = { ROOT.EAI_config_cp_cost = 20 } } 
	else_if = { limit = { strength_ratio = { tag = ROOT ratio > 0.1 } } 			set_variable = { ROOT.EAI_config_cp_cost = 10 } } 
	else_if = { limit = { NOT = { strength_ratio = { tag = ROOT ratio > 0.1 } } } 	set_variable = { ROOT.EAI_config_cp_cost = 10 } }

	multiply_variable = { ROOT.EAI_config_cp_cost = 0.5 }

	if = { limit = { is_subject_of = ROOT }
		multiply_variable = { ROOT.EAI_config_cp_cost = 0.5 } # 50% cheaper on subjects
	}
	
	if = { limit = { ROOT = { is_subject_of = PREV } }
		multiply_variable = { ROOT.EAI_config_cp_cost = 1.5 } # 50% more expensive on overlord
	}
	
	# clamp_variable = { var = ROOT.EAI_config_cp_cost_@THIS min = 0 max = 100 } # max cost to allow weak minors to influence overlords
	
	set_variable = { ROOT.EAI_config_cp_cost_effect = ROOT.EAI_config_cp_cost }
	
	multiply_variable = { ROOT.EAI_config_cp_cost_effect = -1 }
}

EAI_num_surface_ships = {

	set_variable = { EAI_num_surface_ships = num_ships_with_type@destroyer }
	add_to_variable = { EAI_num_surface_ships = num_ships_with_type@light_cruiser }
	add_to_variable = { EAI_num_surface_ships = num_ships_with_type@heavy_cruiser }
	add_to_variable = { EAI_num_surface_ships = num_ships_with_type@battle_cruiser }
	add_to_variable = { EAI_num_surface_ships = num_ships_with_type@battleship }
	add_to_variable = { EAI_num_surface_ships = num_ships_with_type@carrier }

	set_variable = { EAI_num_capital_ships = num_ships_with_type@heavy_cruiser }
	add_to_variable = { EAI_num_capital_ships = num_ships_with_type@battle_cruiser }
	add_to_variable = { EAI_num_capital_ships = num_ships_with_type@battleship }
	add_to_variable = { EAI_num_capital_ships = num_ships_with_type@carrier }

	set_variable = { EAI_num_screen_ships = num_ships_with_type@destroyer }
	add_to_variable = { EAI_num_screen_ships = num_ships_with_type@light_cruiser }
}

EAI_enemies_air_strength_ratio = {

	set_variable = { EAI_enemies_air_strength_ratio = 0 }
	every_enemy_country = {

		add_to_variable = { PREV.EAI_enemies_air_strength_ratio = num_fighters_deployed }
	}


	if = { limit = { check_variable = { num_fighters_deployed > 0 } }

		divide_variable = { EAI_enemies_air_strength_ratio = num_fighters_deployed }
	}
}

EAI_calc_air_force_sizes = {

	### allied air size
		
	set_temp_variable = { friendly_air_force_size = num_fighters_deployed }

	for_each_scope_loop = { array = allies

		add_to_temp_variable = { friendly_air_force_size = num_fighters_deployed }
	}

	# log = "friendly = [this.gettag] [?friendly_air_force_size]"

	### enemies air size

	clear_temp_array = enemy_air_force_sizes
	every_enemy_country = {

		set_temp_variable = { air_force_size_@THIS = num_fighters_deployed }

		if = { limit = { is_in_faction = yes }

			if = { limit = { is_faction_leader = yes }

				add_to_temp_variable = { faction_air_force_size_@THIS = air_force_size_@THIS }
				add_to_temp_array = { enemy_air_force_sizes = THIS.id }
			}

			else = {

				var:faction_leader = {

					add_to_temp_variable = { faction_air_force_size_@THIS = air_force_size_@PREV }
				}
			}
		}
		else = { add_to_temp_array = { enemy_air_force_sizes = THIS.id } }
	}

	# for_each_scope_loop = { array = enemy_air_force_sizes

	# 	log = "enemy = [this.gettag] air_force_size_ [?air_force_size_@THIS] faction_air_force_size_ [?faction_air_force_size_@THIS]"
	# }
}

# stockpile display
EAI_set_stockpile_variables = {

	set_variable = { EAI_stockpile_infantry_equipment = Prev.num_equipment@infantry_equipment }
	set_variable = { EAI_str_ratio_infantry_equipment = Prev.num_equipment_in_armies@infantry_equipment }
	divide_variable = { EAI_str_ratio_infantry_equipment = Prev.num_target_equipment_in_armies@infantry_equipment }
	multiply_variable = { EAI_str_ratio_infantry_equipment = 100 }

	set_variable = { EAI_stockpile_support_equipment = Prev.num_equipment@support_equipment }
	set_variable = { EAI_str_ratio_support_equipment = Prev.num_equipment_in_armies@support_equipment }
	divide_variable = { EAI_str_ratio_support_equipment = Prev.num_target_equipment_in_armies@support_equipment }
	multiply_variable = { EAI_str_ratio_support_equipment = 100 }

	set_variable = { EAI_stockpile_motorized_equipment = Prev.num_equipment@motorized_equipment }
	set_variable = { EAI_str_ratio_motorized_equipment = Prev.num_equipment_in_armies@motorized_equipment }
	divide_variable = { EAI_str_ratio_motorized_equipment = Prev.num_target_equipment_in_armies@motorized_equipment }
	multiply_variable = { EAI_str_ratio_motorized_equipment = 100 }

	set_variable = { EAI_stockpile_mechanized_equipment = Prev.num_equipment@mechanized_equipment }
	set_variable = { EAI_str_ratio_mechanized_equipment = Prev.num_equipment_in_armies@mechanized_equipment }
	divide_variable = { EAI_str_ratio_mechanized_equipment = Prev.num_target_equipment_in_armies@mechanized_equipment }
	multiply_variable = { EAI_str_ratio_mechanized_equipment = 100 }

	set_variable = { EAI_stockpile_artillery_equipment = Prev.num_equipment@artillery_equipment }
	set_variable = { EAI_str_ratio_artillery_equipment = Prev.num_equipment_in_armies@artillery_equipment }
	divide_variable = { EAI_str_ratio_artillery_equipment = Prev.num_target_equipment_in_armies@artillery_equipment }
	multiply_variable = { EAI_str_ratio_artillery_equipment = 100 }

	set_variable = { EAI_stockpile_rocket_artillery_equipment = Prev.num_equipment@rocket_artillery_equipment }
	set_variable = { EAI_str_ratio_rocket_artillery_equipment = Prev.num_equipment_in_armies@rocket_artillery_equipment }
	divide_variable = { EAI_str_ratio_rocket_artillery_equipment = Prev.num_target_equipment_in_armies@rocket_artillery_equipment }
	multiply_variable = { EAI_str_ratio_rocket_artillery_equipment = 100 }

	set_variable = { EAI_stockpile_motorized_rocket_equipment = Prev.num_equipment@motorized_rocket_equipment }
	set_variable = { EAI_str_ratio_motorized_rocket_equipment = Prev.num_equipment_in_armies@motorized_rocket_equipment }
	divide_variable = { EAI_str_ratio_motorized_rocket_equipment = Prev.num_target_equipment_in_armies@motorized_rocket_equipment }
	multiply_variable = { EAI_str_ratio_motorized_rocket_equipment = 100 }

	set_variable = { EAI_stockpile_anti_tank_equipment = Prev.num_equipment@anti_tank_equipment }
	set_variable = { EAI_str_ratio_anti_tank_equipment = Prev.num_equipment_in_armies@anti_tank_equipment }
	divide_variable = { EAI_str_ratio_anti_tank_equipment = Prev.num_target_equipment_in_armies@anti_tank_equipment }
	multiply_variable = { EAI_str_ratio_anti_tank_equipment = 100 }

	set_variable = { EAI_stockpile_anti_air_equipment = Prev.num_equipment@anti_air_equipment }
	set_variable = { EAI_str_ratio_anti_air_equipment = Prev.num_equipment_in_armies@anti_air_equipment }
	divide_variable = { EAI_str_ratio_anti_air_equipment = Prev.num_target_equipment_in_armies@anti_air_equipment }
	multiply_variable = { EAI_str_ratio_anti_air_equipment = 100 }


	set_variable = { EAI_stockpile_light_tank_chassis = Prev.num_equipment@light_tank_chassis }
	set_variable = { EAI_str_ratio_light_tank_chassis = Prev.num_equipment_in_armies@light_tank_chassis }
	divide_variable = { EAI_str_ratio_light_tank_chassis = Prev.num_target_equipment_in_armies@light_tank_chassis }
	multiply_variable = { EAI_str_ratio_light_tank_chassis = 100 }

	set_variable = { EAI_stockpile_light_tank_artillery_chassis = Prev.num_equipment@light_tank_artillery_chassis }
	set_variable = { EAI_str_ratio_light_tank_artillery_chassis = Prev.num_equipment_in_armies@light_tank_artillery_chassis }
	divide_variable = { EAI_str_ratio_light_tank_artillery_chassis = Prev.num_target_equipment_in_armies@light_tank_artillery_chassis }
	multiply_variable = { EAI_str_ratio_light_tank_artillery_chassis = 100 }

	set_variable = { EAI_stockpile_light_tank_destroyer_chassis = Prev.num_equipment@light_tank_destroyer_chassis }
	set_variable = { EAI_str_ratio_light_tank_destroyer_chassis = Prev.num_equipment_in_armies@light_tank_destroyer_chassis }
	divide_variable = { EAI_str_ratio_light_tank_destroyer_chassis = Prev.num_target_equipment_in_armies@light_tank_destroyer_chassis }
	multiply_variable = { EAI_str_ratio_light_tank_destroyer_chassis = 100 }

	set_variable = { EAI_stockpile_light_tank_aa_chassis = Prev.num_equipment@light_tank_aa_chassis }
	set_variable = { EAI_str_ratio_light_tank_aa_chassis = Prev.num_equipment_in_armies@light_tank_aa_chassis }
	divide_variable = { EAI_str_ratio_light_tank_aa_chassis = Prev.num_target_equipment_in_armies@light_tank_aa_chassis }
	multiply_variable = { EAI_str_ratio_light_tank_aa_chassis = 100 }


	set_variable = { EAI_stockpile_medium_tank_chassis = Prev.num_equipment@medium_tank_chassis }
	set_variable = { EAI_str_ratio_medium_tank_chassis = Prev.num_equipment_in_armies@medium_tank_chassis }
	divide_variable = { EAI_str_ratio_medium_tank_chassis = Prev.num_target_equipment_in_armies@medium_tank_chassis }
	multiply_variable = { EAI_str_ratio_medium_tank_chassis = 100 }

	set_variable = { EAI_stockpile_medium_tank_artillery_chassis = Prev.num_equipment@medium_tank_artillery_chassis }
	set_variable = { EAI_str_ratio_medium_tank_artillery_chassis = Prev.num_equipment_in_armies@medium_tank_artillery_chassis }
	divide_variable = { EAI_str_ratio_medium_tank_artillery_chassis = Prev.num_target_equipment_in_armies@medium_tank_artillery_chassis }
	multiply_variable = { EAI_str_ratio_medium_tank_artillery_chassis = 100 }

	set_variable = { EAI_stockpile_medium_tank_destroyer_chassis = Prev.num_equipment@medium_tank_destroyer_chassis }
	set_variable = { EAI_str_ratio_medium_tank_destroyer_chassis = Prev.num_equipment_in_armies@medium_tank_destroyer_chassis }
	divide_variable = { EAI_str_ratio_medium_tank_destroyer_chassis = Prev.num_target_equipment_in_armies@medium_tank_destroyer_chassis }
	multiply_variable = { EAI_str_ratio_medium_tank_destroyer_chassis = 100 }

	set_variable = { EAI_stockpile_medium_tank_aa_chassis = Prev.num_equipment@medium_tank_aa_chassis }
	set_variable = { EAI_str_ratio_medium_tank_aa_chassis = Prev.num_equipment_in_armies@medium_tank_aa_chassis }
	divide_variable = { EAI_str_ratio_medium_tank_aa_chassis = Prev.num_target_equipment_in_armies@medium_tank_aa_chassis }
	multiply_variable = { EAI_str_ratio_medium_tank_aa_chassis = 100 }


	set_variable = { EAI_stockpile_heavy_tank_chassis = Prev.num_equipment@heavy_tank_chassis }
	set_variable = { EAI_str_ratio_heavy_tank_chassis = Prev.num_equipment_in_armies@heavy_tank_chassis }
	divide_variable = { EAI_str_ratio_heavy_tank_chassis = Prev.num_target_equipment_in_armies@heavy_tank_chassis }
	multiply_variable = { EAI_str_ratio_heavy_tank_chassis = 100 }

	set_variable = { EAI_stockpile_heavy_tank_artillery_chassis = Prev.num_equipment@heavy_tank_artillery_chassis }
	set_variable = { EAI_str_ratio_heavy_tank_artillery_chassis = Prev.num_equipment_in_armies@heavy_tank_artillery_chassis }
	divide_variable = { EAI_str_ratio_heavy_tank_artillery_chassis = Prev.num_target_equipment_in_armies@heavy_tank_artillery_chassis }
	multiply_variable = { EAI_str_ratio_heavy_tank_artillery_chassis = 100 }

	set_variable = { EAI_stockpile_heavy_tank_destroyer_chassis = Prev.num_equipment@heavy_tank_destroyer_chassis }
	set_variable = { EAI_str_ratio_heavy_tank_destroyer_chassis = Prev.num_equipment_in_armies@heavy_tank_destroyer_chassis }
	divide_variable = { EAI_str_ratio_heavy_tank_destroyer_chassis = Prev.num_target_equipment_in_armies@heavy_tank_destroyer_chassis }
	multiply_variable = { EAI_str_ratio_heavy_tank_destroyer_chassis = 100 }

	set_variable = { EAI_stockpile_heavy_tank_aa_chassis = Prev.num_equipment@heavy_tank_aa_chassis }
	set_variable = { EAI_str_ratio_heavy_tank_aa_chassis = Prev.num_equipment_in_armies@heavy_tank_aa_chassis }
	divide_variable = { EAI_str_ratio_heavy_tank_aa_chassis = Prev.num_target_equipment_in_armies@heavy_tank_aa_chassis }
	multiply_variable = { EAI_str_ratio_heavy_tank_aa_chassis = 100 }


	set_variable = { EAI_stockpile_modern_tank_chassis = Prev.num_equipment@modern_tank_chassis }
	set_variable = { EAI_str_ratio_modern_tank_chassis = Prev.num_equipment_in_armies@modern_tank_chassis }
	divide_variable = { EAI_str_ratio_modern_tank_chassis = Prev.num_target_equipment_in_armies@modern_tank_chassis }
	multiply_variable = { EAI_str_ratio_modern_tank_chassis = 100 }

	set_variable = { EAI_stockpile_modern_tank_artillery_chassis = Prev.num_equipment@modern_tank_artillery_chassis }
	set_variable = { EAI_str_ratio_modern_tank_artillery_chassis = Prev.num_equipment_in_armies@modern_tank_artillery_chassis }
	divide_variable = { EAI_str_ratio_modern_tank_artillery_chassis = Prev.num_target_equipment_in_armies@modern_tank_artillery_chassis }
	multiply_variable = { EAI_str_ratio_modern_tank_artillery_chassis = 100 }

	set_variable = { EAI_stockpile_modern_tank_destroyer_chassis = Prev.num_equipment@modern_tank_destroyer_chassis }
	set_variable = { EAI_str_ratio_modern_tank_destroyer_chassis = Prev.num_equipment_in_armies@modern_tank_destroyer_chassis }
	divide_variable = { EAI_str_ratio_modern_tank_destroyer_chassis = Prev.num_target_equipment_in_armies@modern_tank_destroyer_chassis }
	multiply_variable = { EAI_str_ratio_modern_tank_destroyer_chassis = 100 }

	set_variable = { EAI_stockpile_modern_tank_aa_chassis = Prev.num_equipment@modern_tank_aa_chassis }
	set_variable = { EAI_str_ratio_modern_tank_aa_chassis = Prev.num_equipment_in_armies@modern_tank_aa_chassis }
	divide_variable = { EAI_str_ratio_modern_tank_aa_chassis = Prev.num_target_equipment_in_armies@modern_tank_aa_chassis }
	multiply_variable = { EAI_str_ratio_modern_tank_aa_chassis = 100 }

	PREV = {
		set_variable = { ROOT.EAI_stockpile_convoy_equipment = 0 }
		while_loop_effect = {
			limit = {
				meta_trigger = {
					text = { stockpile_ratio = { archetype = convoy ratio > [x] } }
					x = "[?ROOT.EAI_stockpile_convoy_equipment]"
				}
			}
			add_to_variable = { ROOT.EAI_stockpile_convoy_equipment = 1 }
		}

		set_variable = { ROOT.EAI_fielded_convoy_equipment = 0 }
		while_loop_effect = { 
			limit = {
				meta_trigger = {
					text = { has_navy_size = { size > [x] type = convoy } }
					x = "[?ROOT.EAI_fielded_convoy_equipment]"
				}
			}
			add_to_variable = { ROOT.EAI_fielded_convoy_equipment = 1 }
		}
	}
	subtract_from_variable = { EAI_fielded_convoy_equipment = EAI_stockpile_convoy_equipment }


	set_variable = { EAI_stockpile_fuel = Prev.fuel_k }
	set_variable = { EAI_str_ratio_fuel = Prev.fuel_ratio }
	multiply_variable = { EAI_str_ratio_fuel = 100 }
}
EAI_clear_stockpile_variables = {

	clear_variable = EAI_stockpile_infantry_equipment
	clear_variable = EAI_str_ratio_infantry_equipment

	clear_variable = EAI_stockpile_support_equipment
	clear_variable = EAI_str_ratio_support_equipment

	clear_variable = EAI_stockpile_motorized_equipment
	clear_variable = EAI_str_ratio_motorized_equipment

	clear_variable = EAI_stockpile_mechanized_equipment
	clear_variable = EAI_str_ratio_mechanized_equipment

	clear_variable = EAI_stockpile_artillery_equipment
	clear_variable = EAI_str_ratio_artillery_equipment

	clear_variable = EAI_stockpile_rocket_artillery_equipment
	clear_variable = EAI_str_ratio_rocket_artillery_equipment

	clear_variable = EAI_stockpile_motorized_rocket_equipment
	clear_variable = EAI_str_ratio_motorized_rocket_equipment

	clear_variable = EAI_stockpile_anti_tank_equipment
	clear_variable = EAI_str_ratio_anti_tank_equipment

	clear_variable = EAI_stockpile_anti_air_equipment
	clear_variable = EAI_str_ratio_anti_air_equipment


	clear_variable = EAI_stockpile_light_tank_chassis
	clear_variable = EAI_str_ratio_light_tank_chassis

	clear_variable = EAI_stockpile_light_tank_artillery_chassis
	clear_variable = EAI_str_ratio_light_tank_artillery_chassis

	clear_variable = EAI_stockpile_light_tank_destroyer_chassis
	clear_variable = EAI_str_ratio_light_tank_destroyer_chassis

	clear_variable = EAI_stockpile_light_tank_aa_chassis
	clear_variable = EAI_str_ratio_light_tank_aa_chassis


	clear_variable = EAI_stockpile_medium_tank_chassis
	clear_variable = EAI_str_ratio_medium_tank_chassis

	clear_variable = EAI_stockpile_medium_tank_artillery_chassis
	clear_variable = EAI_str_ratio_medium_tank_artillery_chassis

	clear_variable = EAI_stockpile_medium_tank_destroyer_chassis
	clear_variable = EAI_str_ratio_medium_tank_destroyer_chassis

	clear_variable = EAI_stockpile_medium_tank_aa_chassis
	clear_variable = EAI_str_ratio_medium_tank_aa_chassis


	clear_variable = EAI_stockpile_heavy_tank_chassis
	clear_variable = EAI_str_ratio_heavy_tank_chassis

	clear_variable = EAI_stockpile_heavy_tank_artillery_chassis
	clear_variable = EAI_str_ratio_heavy_tank_artillery_chassis

	clear_variable = EAI_stockpile_heavy_tank_destroyer_chassis
	clear_variable = EAI_str_ratio_heavy_tank_destroyer_chassis

	clear_variable = EAI_stockpile_heavy_tank_aa_chassis
	clear_variable = EAI_str_ratio_heavy_tank_aa_chassis


	clear_variable = EAI_stockpile_modern_tank_chassis
	clear_variable = EAI_str_ratio_modern_tank_chassis

	clear_variable = EAI_stockpile_modern_tank_artillery_chassis
	clear_variable = EAI_str_ratio_modern_tank_artillery_chassis

	clear_variable = EAI_stockpile_modern_tank_destroyer_chassis
	clear_variable = EAI_str_ratio_modern_tank_destroyer_chassis

	clear_variable = EAI_stockpile_modern_tank_aa_chassis
	clear_variable = EAI_str_ratio_modern_tank_aa_chassis


	clear_variable = EAI_stockpile_convoy_equipment
	clear_variable = EAI_fielded_convoy_equipment


	clear_variable = EAI_stockpile_fuel
	clear_variable = EAI_str_ratio_fuel
}

### Track armor and piercing seen in divisions of this and enemy countries

EAI_armor_intel_check = {

	if = {
		limit = {
			OR = { # only check if it has a template that can upgrade its piercing
				has_country_flag = EAI_anti_tank_template_upgrade
				has_country_flag = EAI_marm_template_upgrade
				has_country_flag = EAI_harm_template_upgrade
			}
		}

		#####################################
		### 	Armor
		#####################################

		### This country

		set_temp_variable = { armor_intel_value = EAI_max_armor }

		while_loop_effect = { limit = { check_variable = { armor_intel_value < 200 } }

			if = {
				limit = {
					meta_trigger = {
						text = {
							NOT = { ROOT = { estimated_intel_max_armor = { tag = ROOT value > [x] } } }
						}
						x = "[?armor_intel_value|.0]"
					}
				}

				if = { limit = { NOT = { check_variable = { EAI_max_armor = armor_intel_value } } }

					if = { limit = { has_country_flag = EAI_armor_intel_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ARMOR INTEL: new highest armor of this country = [?armor_intel_value]" } 
				}

				set_variable = { EAI_max_armor = armor_intel_value }
				set_temp_variable = { break = 1 }
			}

			add_to_temp_variable = { armor_intel_value = 2 }
		}

		### Enemies and neighbors

		set_temp_variable = { armor_intel_value = EAI_max_armor_seen }

		while_loop_effect = { limit = { check_variable = { armor_intel_value < 200 } }

			if = {
				limit = {
					meta_trigger = {
						text = {
							NOT = {
								any_neighbor_country = { 
									EAI_is_threat = yes
									ROOT = { estimated_intel_max_armor = { tag = PREV value > [x] } } 
								}
								any_enemy_country = { ROOT = { estimated_intel_max_armor = { tag = PREV value > [x] } } }
							}
						}
						x = "[?armor_intel_value|.0]"
					}
				}

				if = { limit = { NOT = { check_variable = { EAI_max_armor_seen = armor_intel_value } } }

					if = { limit = { has_country_flag = EAI_armor_intel_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ARMOR INTEL: new highest armor seen = [?armor_intel_value]" } 
				}

				set_variable = { EAI_max_armor_seen = armor_intel_value }
				set_temp_variable = { break = 1 }
			}

			add_to_temp_variable = { armor_intel_value = 5 }
		}

		### Enemies

		set_temp_variable = { armor_intel_value = EAI_max_armor_seen_enemies }

		while_loop_effect = { limit = { check_variable = { armor_intel_value < 200 } }

			if = {
				limit = {
					meta_trigger = {
						text = {
							NOT = {
								any_enemy_country = { ROOT = { estimated_intel_max_armor = { tag = PREV value > [x] } } }
							}
						}
						x = "[?armor_intel_value|.0]"
					}
				}

				if = { limit = { NOT = { check_variable = { EAI_max_armor_seen_enemies = armor_intel_value } } }

					if = { limit = { has_country_flag = EAI_armor_intel_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ARMOR INTEL: new highest enemy armor seen = [?armor_intel_value]" } 
				}

				set_variable = { EAI_max_armor_seen_enemies = armor_intel_value }
				set_temp_variable = { break = 1 }
			}

			add_to_temp_variable = { armor_intel_value = 5 }
		}

		#####################################
		### 	Piercing
		#####################################

		### This country

		set_temp_variable = { piercing_value = EAI_max_piercing }

		while_loop_effect = { limit = { check_variable = { piercing_value < 200 } }

			if = {
				limit = {
					meta_trigger = {
						text = {
							NOT = { ROOT = { estimated_intel_max_piercing = { tag = ROOT value > [x] } } }
						}
						x = "[?piercing_value|.0]"
					}
				}

				if = { limit = { NOT = { check_variable = { EAI_max_piercing = piercing_value } } }

					if = { limit = { has_country_flag = EAI_armor_intel_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ARMOR INTEL: new highest piercing of this country = [?piercing_value]" } 
				}

				set_variable = { EAI_max_piercing = piercing_value }
				set_temp_variable = { break = 1 }
			}

			add_to_temp_variable = { piercing_value = 2 }
		}

		### Enemies

		set_temp_variable = { piercing_value = EAI_max_piercing_enemies }

		while_loop_effect = { limit = { check_variable = { piercing_value < 200 } }

			if = {
				limit = {
					meta_trigger = {
						text = {
							NOT = {
								any_enemy_country = { ROOT = { estimated_intel_max_piercing = { tag = PREV value > [x] } } }
							}
						}
						x = "[?piercing_value|.0]"
					}
				}

				if = { limit = { NOT = { check_variable = { EAI_max_piercing_seen_enemies = piercing_value } } }

					if = { limit = { has_country_flag = EAI_armor_intel_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ARMOR INTEL: new highest piercing of enemies = [?piercing_value]" } 
				}

				set_variable = { EAI_max_piercing_seen_enemies = piercing_value }
				set_temp_variable = { break = 1 }
			}

			add_to_temp_variable = { piercing_value = 5 }
		}
	}
}

EAI_armor_intel_startup_targets = {

	if = {
		limit = {
			OR = {
				tag = GER
				tag = SOV
			}
		}

		set_variable = { EAI_max_armor_seen = 65 }
		set_variable = { EAI_max_armor_seen_enemies = 65 }
	}
}

###

EAI_remove_research_slots_from_minors_setting = {

	if = {
		limit = {
			has_global_flag = EAI_limit_research
			NOT = { EAI_major_country = yes }
			OR = {
				AND = { original_research_slots > 3 NOT = { has_country_flag = EAI_slots_reduced_3 } }
				AND = { original_research_slots > 2 original_research_slots < 4 NOT = { has_country_flag = EAI_slots_reduced_2 } }
				AND = { original_research_slots > 1 original_research_slots < 3 NOT = { has_country_flag = EAI_slots_reduced_1 } }
			}
		}

		if = { limit = { original_research_slots > 3 } add_research_slot = -3 set_country_flag = EAI_slots_reduced_3 }
		if = { limit = { original_research_slots > 2 original_research_slots < 4 } add_research_slot = -2 set_country_flag = EAI_slots_reduced_2 }
		if = { limit = { original_research_slots > 1 original_research_slots < 3 } add_research_slot = -1 set_country_flag = EAI_slots_reduced_1 }

		if = { limit = { has_country_flag = EAI_difficulty_logging } log = "[GetYear] [GetMonth] | GAME | [Root.GetName] | reducing research slots" } 
	}
}

EAI_remove_relief_of_command = {

	if = {
		limit = {
			has_idea = relief_of_command_spirit
			EAI_land_doctrine_completed = yes
			has_army_experience > 100
		}
	
		remove_ideas = relief_of_command_spirit
	}
}

EAI_simulate_fleet_training_monthly = {
	if = {
		limit = {
			has_war = no
			date > 1937.1.1 #AI trains properly early game
		}
		if = {
			limit = {
				original_tag = USA	
			}
			navy_experience = 30
			#FUEL reduction not needed as USA has virtually infinite fuel
		}
	}
}