############################################################################################################
#	Expert AI mod - misc triggers
############################################################################################################

EAI_test_trigger = {

	log = "*****************************************************"

	if = {
		limit = {
			set_temp_variable = { tmp = 9496 }
			# controls_province = 11343
			# controls_province = 9263
			# controls_province = tmp

			64 = {
				any_province_building_level = {
					province = { id = var:tmp }
					building = supply_node
					level > 0
				}
			}
		}
	
		log = "> YES"
	}
	else = {
		log = "> NO"
	}

	log = "*****************************************************"
}

###

EAI_MODE_challenging = {
	has_global_flag = EAI_challenging_mode
}

EAI_MODE_normal = {
	NOT = { has_global_flag = EAI_challenging_mode }
}

EAI_MODE_historical = {
	has_global_flag = EAI_historical_mode
}

###

EAI_TRIGGER_get_side = { # var:_id var:_at_war_with arr:side_
	clear_temp_array = side_
	if = { 
		limit = { 
			if = { limit = { NOT = { check_variable = { _at_war_with = 0 } } } has_war_with = var:_at_war_with }
		} 
		add_to_temp_array = { side_ = _id } 
	}
	var:_id = {
		all_of_scopes = { array = allies 
			if = { 
				limit = { 
					NOT = { is_in_array = { side_ = THIS.id } }
					if = { limit = { NOT = { check_variable = { _at_war_with = 0 } } } has_war_with = var:_at_war_with }
				} 
				add_to_temp_array = { side_ = THIS.id }
			} 
		}
		all_of_scopes = { array = subjects 
			if = { 
				limit = { 
					NOT = { is_in_array = { side_ = THIS.id } } 
					if = { limit = { NOT = { check_variable = { _at_war_with = 0 } } } has_war_with = var:_at_war_with }
				} 
				add_to_temp_array = { side_ = THIS.id }
			} 
		}
	}
	set_temp_variable = { _at_war = 0 }
}

EAI_TRIGGER_get_state_controllers = { # arr:_states var:_at_war var:_same_side controllers_
	clear_temp_array = controllers_
	set_temp_variable = { this_id = THIS.id }
	all_of_scopes = { array = _states
		CONTROLLER = {
			if = { 
				limit = { 
					NOT = { is_in_array = { controllers_ = THIS.id } }
					if = { limit = { check_variable = { _at_war = 1 } } has_war_with = var:this_id }
					if = { limit = { check_variable = { _same_side = 0 } } NOT = { var:this_id = { EAI_PREV_on_same_side_with_THIS = yes } } }
				} 
				add_to_temp_array = { controllers_ = THIS.id }
			} 
		}
	}
	set_temp_variable = { _at_war = 0 }
	set_temp_variable = { _same_side = 0 }
	clear_temp_array = _states
}

EAI_is_island_state = {

	is_in_array = { global.EAI_MAP_island_states = THIS.id }
}

EAI_XP_has_army_check_xp = {
	
	set_temp_variable = { total_xp = EAI_XP_stored_army_xp }
	add_to_temp_variable = { total_xp = army_experience }
	NOT = { check_variable = { total_xp < _check_xp } }
}

EAI_XP_has_navy_check_xp = {
	
	set_temp_variable = { total_xp = EAI_XP_stored_navy_xp }
	add_to_temp_variable = { total_xp = navy_experience }
	NOT = { check_variable = { total_xp < _check_xp } }
}

EAI_XP_has_air_check_xp = {
	
	set_temp_variable = { total_xp = EAI_XP_stored_air_xp }
	add_to_temp_variable = { total_xp = air_experience }
	NOT = { check_variable = { total_xp < _check_xp } }
}

EAI_get_traded_factories = {

	set_temp_variable = { traded_factories = 0 }
	add_to_temp_variable = { traded_factories = resource_imported@aluminium }
	add_to_temp_variable = { traded_factories = resource_imported@rubber }
	add_to_temp_variable = { traded_factories = resource_imported@oil }
	add_to_temp_variable = { traded_factories = resource_imported@steel }
	add_to_temp_variable = { traded_factories = resource_imported@tungsten }
	add_to_temp_variable = { traded_factories = resource_imported@chromium }

	divide_temp_variable = { traded_factories = 8 }
}

EAI_enable_mio_policies = {
	FROM = {
		has_political_power > 150
	}
}

EAI_THIS_is_invasion_target_of_PREV = {
	PREV = { check_variable = { ai_strategy_invade@PREV > 1000 } }
}

### Readiness

EAI_MILITARY_army_readiness_HIGH = {
	check_variable = { EAI_fielded_eq_ratio > 0.95 }
	EAI_reserve_manpower_more_than_25p = yes
}
EAI_MILITARY_army_readiness_HIGH_abort = {
	check_variable = { EAI_fielded_eq_ratio > 0.85 }
	EAI_reserve_manpower_more_than_20p = yes
}

EAI_MILITARY_army_readiness_MEDIUM = {
	check_variable = { EAI_fielded_eq_ratio > 0.85 }
	EAI_reserve_manpower_more_than_20p = yes
}
EAI_MILITARY_army_readiness_MEDIUM_abort = {
	check_variable = { EAI_fielded_eq_ratio > 0.75 }
	EAI_reserve_manpower_more_than_15p = yes
}

EAI_MILITARY_army_readiness_LOW = {
	check_variable = { EAI_fielded_eq_ratio > 0.75 }
	EAI_reserve_manpower_more_than_15p = yes
}
EAI_MILITARY_army_readiness_LOW_abort = {
	check_variable = { EAI_fielded_eq_ratio > 0.65 }
	EAI_reserve_manpower_more_than_10p = yes
}

### Shortage

EAI_MILITARY_army_shortage_CRITICAL = {
	OR = {
		check_variable = { EAI_fielded_eq_ratio < 0.7 }
		EAI_reserve_manpower_less_than_5p = yes
	}
}
EAI_MILITARY_army_shortage_CRITICAL_abort = {
	OR = {
		check_variable = { EAI_fielded_eq_ratio < 0.8 }
		EAI_reserve_manpower_less_than_10p = yes
	}
}

EAI_MILITARY_army_shortage_MAJOR = {
	OR = {
		check_variable = { EAI_fielded_eq_ratio < 0.8 }
		EAI_reserve_manpower_less_than_10p = yes
	}
}
EAI_MILITARY_army_shortage_MAJOR_abort = {
	OR = {
		check_variable = { EAI_fielded_eq_ratio < 0.9 }
		EAI_reserve_manpower_less_than_15p = yes
	}
}

EAI_MILITARY_army_shortage_MINOR = {
	OR = {
		check_variable = { EAI_fielded_eq_ratio < 0.9 }
		EAI_reserve_manpower_less_than_15p = yes
	}
}
EAI_MILITARY_army_shortage_MINOR_abort = {
	OR = {
		check_variable = { EAI_fielded_eq_ratio < 0.95 }
		EAI_reserve_manpower_less_than_20p = yes
	}
}

###

EAI_reserve_manpower_more_than_25p = {

	OR = {
		check_variable = { EAI_reserve_mp_ratio > 0.25 }
		has_manpower > 500000
	}
}

EAI_reserve_manpower_more_than_20p = {

	OR = {
		check_variable = { EAI_reserve_mp_ratio > 0.20 }
		has_manpower > 250000
	}
}

EAI_reserve_manpower_more_than_15p = {

	OR = {
		check_variable = { EAI_reserve_mp_ratio > 0.15 }
		has_manpower > 187500
	}
}

EAI_reserve_manpower_more_than_10p = {

	OR = {
		check_variable = { EAI_reserve_mp_ratio > 0.10 }
		has_manpower > 125000
	}
}

EAI_reserve_manpower_more_than_5p = {

	OR = {
		check_variable = { EAI_reserve_mp_ratio > 0.05 }
		has_manpower > 62500
	}
}

###

EAI_reserve_manpower_less_than_25p = {

	NOT = {
		check_variable = { EAI_reserve_mp_ratio > 0.25 }
		has_manpower > 500000
	}
}

EAI_reserve_manpower_less_than_20p = {

	NOT = {
		check_variable = { EAI_reserve_mp_ratio > 0.20 }
		has_manpower > 250000
	}
}

EAI_reserve_manpower_less_than_15p = {

	NOT = {
		check_variable = { EAI_reserve_mp_ratio > 0.15 }
		has_manpower > 187500
	}
}

EAI_reserve_manpower_less_than_10p = {

	NOT = {
		check_variable = { EAI_reserve_mp_ratio > 0.10 }
		has_manpower > 125000
	}
}

EAI_reserve_manpower_less_than_5p = {

	NOT = {
		check_variable = { EAI_reserve_mp_ratio > 0.05 }
		has_manpower > 62500
	}
}

###

EAI_limited_minors = {

	is_major = no

	OR = {
		CAPITAL_SCOPE = { is_on_continent = asia }
		CAPITAL_SCOPE = { is_on_continent = africa }
		CAPITAL_SCOPE = { is_on_continent = south_america }
		CAPITAL_SCOPE = { is_on_continent = middle_east }
	}

	NOT = {
		tag = AOI
	}
}

###

# true majors in the game
EAI_major_country = { 

	OR = {
		original_tag = ENG
		original_tag = FRA
		original_tag = USA

		original_tag = GER
		original_tag = ITA
		original_tag = JAP

		original_tag = SOV
	}
}

# civil war country tags
EAI_civil_war_country = { 

	OR = {
		tag = D01
		tag = D02
		tag = D03
		tag = D04
		tag = D05
		tag = D06
		tag = D07
		tag = D08
		tag = D09
		tag = D10
		tag = D11
		tag = D12
		tag = D13
		tag = D14
		tag = D15
	}
}

### significant minors

EAI_asian_minor = {
	OR = {
		original_tag = CHI
		original_tag = SHX
		original_tag = PRC
		original_tag = XSM
		original_tag = GXC
		original_tag = YUN
		original_tag = SIA
		original_tag = PHI
	}
}

EAI_commonwealth = {
	OR = {
		original_tag = CAN
		original_tag = AST
		original_tag = SAF
		original_tag = RAJ
		original_tag = NZL
	}
}

EAI_european_minor = {
	OR = {
		original_tag = CZE
		original_tag = POL
		original_tag = ROM
		original_tag = HUN
		original_tag = BUL
		original_tag = SPR
		original_tag = FIN
		original_tag = SWE
		original_tag = NOR
		original_tag = GRE
		original_tag = TUR
		original_tag = DEN
		original_tag = POR
		original_tag = YUG
	}
}

###

# true neutrals
EAI_neutral_country = { 
	NOT = { has_war = yes }
	OR = { has_government = neutrality has_government = democratic }
	OR = {
		tag = BEL
		tag = SWI
		tag = SWE
		tag = IRE
		tag = TIB
		tag = NEP
		tag = BHU
		tag = POR
	}
}

# triggers the AI to build more units when threatened
EAI_mobilize_army = {
	NOT = { has_war = yes EAI_major_country = yes }

	OR = {
		has_country_flag = EAI_mobilize_army

		any_neighbor_country = {

			capital_scope = { has_state_flag = EAI_original_capital_@PREV }

			any_of_scopes = { array = controlled_states
				is_in_home_area = yes
				any_neighbor_state = {
					is_controlled_by = ROOT
					is_in_home_area = yes
				}
			}

			OR = {
				has_added_tension_amount > 10
				has_war = yes
				is_justifying_wargoal_against = ROOT
				has_wargoal_against = ROOT
				any_other_country = { 
					OR = {
						is_justifying_wargoal_against = PREV 
						has_wargoal_against = PREV
					}
				}
			}
		}

		### special cases

		original_tag = CHI
	}
}

# minors build infantry according to their division cap during peace time
EAI_build_infantry = {

	NOT = { EAI_major_country = yes }
	
	NOT = { has_country_flag = EAI_infantry_division_limit }

	if = { limit = { has_country_flag = EAI_mobilize_army }
		meta_trigger = {
			text = { has_army_manpower = { size < [x][y] } }
			x = "[?EAI_division_cap_10k|.0]"
			y = "0000"
		}
	}
	else = {
		set_temp_variable = { t1 = EAI_division_cap_10k }
		divide_temp_variable = { t1 = 2 }
		set_temp_variable = { _from = 0 }
		set_temp_variable = { _to = t1 }
		set_temp_variable = { _amount = global.threat }
		EAI_MATH_TRIGGER_lerp = yes
		add_to_temp_variable = { t1 = lerp_ }
		meta_trigger = {
			text = { has_army_manpower = { size < [x][y] } }
			x = "[?t1|.0]"
			y = "0000"
		}
	}
}

EAI_defend_core = {

	OR = {
		AND = {
			is_major = no
			surrender_progress > 0.0
		}

		AND = {
			tag = GRE
			is_historical_focus_on = yes
			OR = {
				surrender_progress > 0.0
				any_neighbor_country = {
					has_war_with = ROOT
					is_major = yes
				}
			}
		}
	
		AND = { # guard cairo, singapore
			OR = { original_tag = EGY original_tag = MAL }
			is_in_faction_with = ENG
		}

		AND = { # keep units on the east front
			OR = { tag = ROM tag = HUN tag = FIN }
			is_neighbor_of = SOV
			has_war_with = SOV
		}
	}
}

### Resource extraction decisions

EAI_RESOURCE_oil_extraction_decision = {

	OR = {
		AND = {
			has_political_power > 175
			OR = {
				has_political_power > 250
				AND = { is_major = no has_idea = free_trade }
				fuel_ratio < 0.5
				check_variable = { resource_imported@oil > 20 }
			}
		}

		AND = {
			has_war = yes
			fuel_ratio < 0.25
		}

		### Strategies

		AND = {
			tag = JAP
			date > 1940.1.1
		}
	}
}

EAI_RESOURCE_rubber_extraction_decision = {

	OR = {
		AND = {
			has_political_power > 175
			OR = {
				has_country_flag = EAI_extract_rubber
				has_country_flag = EAI_allies_need_rubber
			}
		}

		### Strategies
	}
}

EAI_RESOURCE_aluminium_extraction_decision = {

	OR = {
		AND = {
			has_political_power > 175
			OR = {
				has_country_flag = EAI_extract_aluminium
				has_country_flag = EAI_allies_need_aluminium
			}
		}

		### Strategies

		AND = {
			tag = JAP
			date > 1940.1.1
		}

		AND = {
			tag = MAN
		}
	}
}

EAI_RESOURCE_steel_extraction_decision = {

	OR = {
		AND = {
			has_political_power > 175
			OR = {
				has_country_flag = EAI_extract_steel
				has_country_flag = EAI_allies_need_steel
			}
		}

		### Strategies
	}
}

EAI_RESOURCE_chromium_extraction_decision = {

	OR = {
		AND = {
			has_political_power > 175
			OR = {
				has_country_flag = EAI_extract_chromium
				has_country_flag = EAI_allies_need_chromium
			}
		}

		### Strategies
	}
}

EAI_RESOURCE_tungsten_extraction_decision = {

	OR = {
		AND = {
			has_political_power > 175
			OR = {
				has_country_flag = EAI_extract_tungsten
				has_country_flag = EAI_allies_need_tungsten
			}
		}

		### Strategies
	}
}

EAI_RESOURCE_has_fuel_shortage = {
	check_variable = { EAI_fuel_resource_shortage = 3 }
}

EAI_RESOURCE_has_aluminium_shortage = {
	check_variable = { EAI_aluminium_resource_shortage = 3 }
}

EAI_RESOURCE_has_rubber_shortage = {
	check_variable = { EAI_rubber_resource_shortage = 3 }
}

EAI_RESOURCE_has_tungsten_shortage = {
	check_variable = { EAI_tungsten_resource_shortage = 3 }
}

EAI_RESOURCE_has_steel_shortage = {
	check_variable = { EAI_steel_resource_shortage = 3 }
}

EAI_RESOURCE_has_chromium_shortage = {
	check_variable = { EAI_chromium_resource_shortage = 3 }
}

EAI_is_island_nation = {
	OR = {
		original_tag = ENG #because AI struggles with convoys, it fluctuates too much. Also, because Island country, good fighter is critical
		original_tag = JAP #because AI struggles with convoys, it fluctuates too much. Also, because Island country, good fighter is critical
	}
}