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

EAI_MILITARY_historical_date_TRIGGER = { # var:_index var:_shift_days
	set_temp_variable = { operation_start_day = EAI_MILITARY_INVASION_operation_start_days^_index }
	subtract_from_temp_variable = { operation_start_day = _shift_days }
	check_variable = { global.EAI_days_elapsed > operation_start_day }
}

EAI_MILITARY_enemy_presence_TRIGGER = { # arr:_states_ var:_leader_id var:_target_control_pct
	set_temp_variable = { our_states = 0 }
	set_temp_variable = { their_states = 0 }
	all_of_scopes = { array = _states_
		if = { 
			limit = { 
				OR = {
					CONTROLLER = { is_in_faction_with = var:_leader_id } 
					CONTROLLER = { is_subject_of = var:_leader_id } 
				}
			}
			add_to_temp_variable = { our_states = 1 }
		}
		if = { limit = { CONTROLLER = { has_war_with = var:_leader_id } }
			add_to_temp_variable = { their_states = 1 } 
			CONTROLLER = { add_to_temp_array = { target_countries = THIS.id } }
		}
	}
	set_temp_variable = { total_states = our_states }
	add_to_temp_variable = { total_states = their_states }
	check_variable = { total_states > 0 }
	divide_temp_variable = { our_states = total_states }
	check_variable = { our_states < _target_control_pct }
}

EAI_MILITARY_strength_TRIGGER = { # arr:_states_ var:_leader_id 
	clear_temp_array = target_countries
	all_of_scopes = { array = _states_
		if = { limit = { CONTROLLER = { has_war_with = var:_leader_id } }
			CONTROLLER = { add_to_temp_array = { target_countries = THIS.id } }
		}
	}

	# Economy

	# Army

	# Air

	# Naval
	alliance_naval_strength_ratio > 1.0
	any_of_scopes = { array = target_countries
		enemies_naval_strength_ratio > 2
	}
}

EAI_MILITARY_not_losing_TRIGGER = {
	NOT = { surrender_progress > 0.1 }
	NOT = {
		AND = {
			has_country_flag = EAI_MILITARY_trend_losing_war
			check_variable = { EAI_MILITARY_trend_value > 0.05 }
		}
	}
}

EAI_MILITARY_home_threatened_TRIGGER = {
	any_home_area_neighbor_country = {
		has_war_with = ROOT
	}
}

###

EAI_MILITARY_time_FACTOR = { # var:_index var:_shift_days
	set_temp_variable = { start = EAI_MILITARY_INVASION_operation_start_days^_index }
	subtract_from_temp_variable = { start = _shift_days }
	set_temp_variable = { _val_ = global.EAI_days_elapsed }
	subtract_from_temp_variable = { _val_ = start }
	set_temp_variable = { _min = 0 }
	set_temp_variable = { _max = _shift_days }
	EAI_MATH_TRIGGER_normalize = yes
	set_temp_variable = { time_factor_ = _val_ }
}

EAI_MILITARY_strength_FACTOR = { # arr:_states

	###

		clear_temp_array = our_side
		clear_temp_array = their_side

		set_temp_variable = { _id = THIS.id }
		EAI_TRIGGER_get_side = yes
		all_of = { array = side_ if = { limit = { NOT = { is_in_array = { our_side = v } } } add_to_temp_array = { our_side = v } } }

		set_temp_variable = { _at_war = 1 }
		EAI_TRIGGER_get_state_controllers = yes
		all_of_scopes = { array = controllers_
			set_temp_variable = { _id = THIS.id }
			EAI_TRIGGER_get_side = yes		
			all_of = { array = side_ if = { limit = { NOT = { is_in_array = { their_side = v } } } add_to_temp_array = { their_side = v } } }
		}

	###

		set_temp_variable = { our_side_army = 0 }
		set_temp_variable = { our_side_navy = 0 }
		all_of_scopes = { array = our_side
			add_to_temp_variable = { our_side_army = deployed_army_manpower_k }
			add_to_temp_variable = { our_side_navy = EAI_num_surface_ships }
		}

		set_temp_variable = { their_side_army = 0 }
		set_temp_variable = { their_side_navy = 0 }
		all_of_scopes = { array = their_side
			add_to_temp_variable = { their_side_army = deployed_army_manpower_k }
			add_to_temp_variable = { their_side_navy = EAI_num_surface_ships }
		}

	###

		if = { limit = { check_variable = { their_side_army > 0 } }
			set_temp_variable = { army_ratio = our_side_army }
			divide_temp_variable = { army_ratio = their_side_army }
		}
		else = { set_temp_variable = { army_ratio = 1 } }

		if = { limit = { check_variable = { their_side_navy > 0 } }
			set_temp_variable = { navy_ratio = our_side_navy }
			divide_temp_variable = { navy_ratio = their_side_navy }
		}
		else = { set_temp_variable = { navy_ratio = 1 } }

	###

		set_temp_variable = { tot_ratio = army_ratio }
		add_to_temp_variable = { tot_ratio = navy_ratio }
		divide_temp_variable = { tot_ratio = 2 }

	###

		set_temp_variable = { strength_factor_ = 1 }
		if = {
			limit = {
				check_variable = { navy_ratio > 1.5 }
				check_variable = { army_ratio > 0.75 }
				check_variable = { tot_ratio > 1.25 }
			}
			set_temp_variable = { _val_ = tot_ratio }
			set_temp_variable = { _min = 1 }
			set_temp_variable = { _max = 5 }
			EAI_MATH_TRIGGER_normalize = yes
			set_temp_variable = { _scale = 5 }
			EAI_MATH_TRIGGER_scale = yes
			add_to_temp_variable = { strength_factor_ = _val_ }
		}

	###
}

EAI_MILITARY_country_losing_FACTOR = { # var:_country_id arr:_states_ var:_leader_id
	any_of_scopes = { array = _states_
		CONTROLLER = { has_war_with = var:_country_id }
	}
	NOT = { var:_country_id = { has_war_with = var:_leader_id } }
	var:_country_id = { set_temp_variable = { _val_ = surrender_progress } }
	set_temp_variable = { _min = 0 }
	set_temp_variable = { _max = 0.5 }
	EAI_MATH_TRIGGER_normalize = yes
	EAI_MATH_TRIGGER_square = yes
	set_temp_variable = { _scale = 3 }
	EAI_MATH_TRIGGER_scale = yes
	add_to_temp_variable = { _val_ = 1 }
	set_temp_variable = { country_losing_factor_ = _val_ }
}

EAI_MILITARY_invasion_demand_FACTOR = {
	set_temp_variable = { _val_ = _demand }
	set_temp_variable = { _min = 0 }
	set_temp_variable = { _max = 30 }
	EAI_MATH_TRIGGER_normalize = yes
	EAI_MATH_TRIGGER_square = yes
	set_temp_variable = { _scale = 3 }
	EAI_MATH_TRIGGER_scale = yes
	add_to_temp_variable = { _val_ = 1 }
	set_temp_variable = { demand_factor_ = _val_ }
}

###

EAI_MILITARY_north_mud_season = {
	OR = {
		check_variable = { global.EAI_calendar_month = 10 }
		check_variable = { global.EAI_calendar_month = 11 }
		check_variable = { global.EAI_calendar_month = 4 }
	}
}

EAI_MILITARY_north_winter_season = {
	OR = {
		check_variable = { global.EAI_calendar_month = 12 }
		check_variable = { global.EAI_calendar_month = 1 }
		check_variable = { global.EAI_calendar_month = 2 }
		check_variable = { global.EAI_calendar_month = 3 }
	}
}

EAI_MILITARY_north_summer_season = {
	OR = {
		check_variable = { global.EAI_calendar_month = 5 }
		check_variable = { global.EAI_calendar_month = 6 }
		check_variable = { global.EAI_calendar_month = 7 }
		check_variable = { global.EAI_calendar_month = 8 }
		check_variable = { global.EAI_calendar_month = 9 }
	}
}

###

EAI_MILITARY_ABILITY_last_stand = {
	OR = {
		always = no

		AND = {
			has_country_flag = { flag = EAI_MILITARY_recent_wardec_90d value = 1 days < 60 }
			any_enemy_country = {
				PREV = { has_country_flag = { flag = EAI_MILITARY_wardec_90d_from_@PREV value = 1 days < 60 } }
				strength_ratio = {
					tag = PREV 
					ratio > 1.25
				}
			}
		}
	}
}
EAI_MILITARY_ABILITY_last_stand_HOLD = {
	OR = {
		NOT = { EAI_MILITARY_army_readiness_HIGH = yes }

		AND = {
			tag = JAP
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 365 }
		}
	}
}

EAI_MILITARY_ABILITY_force_attack = {
	OR = {
		always = no

		AND = {
			tag = GER
			has_global_flag = { flag = EAI_GER_blitz_poland_365d value = 1 days < 15 }
		}

		AND = {
			tag = GER
			has_global_flag = { flag = EAI_GER_blitz_low_countries_365d value = 1 days < 15 }
		}

		AND = {
			tag = GER
			has_global_flag = { flag = EAI_GER_blitz_russia_365d value = 1 days < 15 }
		}

		AND = {
			tag = JAP
			has_country_flag = EAI_JAP_homeland_invaded
		}

		AND = {
			tag = ENG
			has_country_flag = EAI_ENG_homeland_invaded
		}
	}
}
EAI_MILITARY_ABILITY_force_attack_HOLD = {
	OR = {
		NOT = { EAI_MILITARY_army_readiness_HIGH = yes }

		AND = {
			tag = JAP
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 365 }
		}
	}
}

EAI_MILITARY_ABILITY_staff_office_plan = {
	OR = {
		always = no
	}
}
EAI_MILITARY_ABILITY_staff_office_plan_HOLD = {
	OR = {
		always = no

		AND = {
			tag = JAP
			has_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days < 365 }
		}
	}
}

###