# AI triggers
# By Rylock and Alpinia

### To have tags send volunteers whenever a war starts, add a new rule below
### ROOT is the tag sending the volunteers, FROM is the tag that receives them
### If the conditions are fulfilled, ROOT will automatically send volunteers to FROM whenever the latter is at war
### When adding new rules, please follow the order of triggers - first global (when the rule should apply), then FROM triggers (who should receive the volunteers), then ROOT triggers (who should send them)

#### OTHER AI TRIGGERS ####



has_reached_maximum_divisions = {
	if = {
		limit = { has_variable = num_actual_divisions }
		check_variable = { num_actual_divisions > max_divisions }
	}
	else = {
		check_variable = { num_battalions > max_divisions }
	}
}

has_enough_reserve_manpower_per_battalion = {
	num_divisions > 1
	check_variable = { manpower_k > num_battalions }
}

has_unsupported_economic_law = {
	if = {
		limit = {
			has_idea = low_economic_mobilisation
			hidden_trigger = {
				NOT = { 
					original_tag = SWE
					has_completed_focus = SWE_totalforsvar
				}
			}
		}
		has_idea = low_economic_mobilisation
		has_war_support < 0.15
	}
	else_if = {
		limit = {
			has_idea = partial_economic_mobilisation
			hidden_trigger = {
				NOT = { 
					original_tag = SWE
					has_completed_focus = SWE_totalforsvar
				}
			}
		}
		has_idea = partial_economic_mobilisation
		has_war_support < 0.25
	}
	else_if = {
		limit = {
			has_idea = war_economy
			hidden_trigger = {
				NOT = { 
					original_tag = SWE
					has_completed_focus = SWE_totalforsvar
				}
			}
		}
		has_idea = war_economy
		has_war_support < 0.5
	}
	else_if = {
		limit = {
			has_idea = tot_economic_mobilisation
			hidden_trigger = {
				NOT = { 
					original_tag = SWE
					has_completed_focus = SWE_totalforsvar
				}
			}
		}
		has_idea = tot_economic_mobilisation
		has_war_support < 0.8
	}
	else_if = {
		limit = {
			has_idea = totaler_krieg_economy
		}
		has_idea = totaler_krieg_economy
		has_war_support < 0.8
	}
	else = {
		always = no
	}
}

has_unsupported_economic_law_ai = {
	if = {
		limit = { has_idea = low_economic_mobilisation }
		has_idea = low_economic_mobilisation
		OR = {
			has_war_support < 0.25
			has_elected_government = yes
		}
	}
	else_if = {
		limit = { has_idea = partial_economic_mobilisation }
		has_idea = partial_economic_mobilisation
		OR = {
			has_war_support < 0.50
			has_elected_government = yes
		}
	}
	else_if = {
		limit = { has_idea = war_economy }
		has_idea = war_economy
	}
	else_if = {
		limit = { has_idea = tot_economic_mobilisation }
		has_idea = tot_economic_mobilisation
	}
	else = {
		always = no
	}
}

has_unsupported_manpower_law = {
	if = {
		limit = {
			has_idea = limited_conscription
		}
			has_idea = limited_conscription
			has_war_support < 0.1
			hidden_trigger = { NOT = { tag = TUR } }
	}
	else_if = {
		limit = {
			has_idea = extensive_conscription
		}
			has_idea = extensive_conscription
			has_war_support < 0.2
			NOT = { has_government = communism }
			NOT = { has_government = fascism }
	}
	else_if = {
		limit = {
			has_idea = service_by_requirement
		}
			has_idea = service_by_requirement
			has_war_support < 0.6
			NOT = { has_government = communism }
			NOT = { has_government = fascism }
	}
	else_if = {
		limit = {
			has_idea = all_adults_serve
		}
		has_idea = all_adults_serve
	}
	else_if = {
		limit = {
			has_idea = scraping_the_barrel
		}
		has_idea = scraping_the_barrel
	}
	else = {
		always = no
	}
}

has_unsupported_manpower_law_ai = {
	if = {
		limit = { has_idea = limited_conscription }
		has_idea = limited_conscription
		OR = {
			has_war_support < 0.25
			has_government = democratic #social_liberal ✖ MODDED BY KEN
		   #has_government = market_liberal ✖ MODDED BY KEN
		   #has_government = social_democrat ✖ MODDED BY KEN
		}
		NOT = { has_idea = SWI_national_conscription } # Keep SWI afloat
	}
	else_if = {
		limit = { has_idea = extensive_conscription }
		has_idea = extensive_conscription
		OR = {
			has_war_support < 0.50
			has_elected_government = yes
		}
		NOT = { has_idea = SWI_national_conscription } # Keep SWI afloat
	}
	else_if = {
		limit = { has_idea = service_by_requirement }
		has_idea = service_by_requirement
	}
	else_if = {
		limit = { has_idea = all_adults_serve }
		has_idea = all_adults_serve
	}
	else_if = {
		limit = { has_idea = scraping_the_barrel }
		has_idea = scraping_the_barrel
	}
	else = {
		always = no
	}
}

has_excessive_army_size = {
	#first calculate how much manpower all the battalions need if they were at full strength
	set_temp_variable = { total_manpower_need = 0 }

	add_to_temp_variable = { total_manpower_need = num_battalions_with_type@infantry }
	add_to_temp_variable = { total_manpower_need = num_battalions_with_type@cavalry }
	add_to_temp_variable = { total_manpower_need = num_battalions_with_type@bicycle_battalion }
	add_to_temp_variable = { total_manpower_need = num_battalions_with_type@marine }
	add_to_temp_variable = { total_manpower_need = num_battalions_with_type@mountaineers }
	add_to_temp_variable = { total_manpower_need = num_battalions_with_type@paratrooper }

	set_temp_variable = { num_batt = num_battalions_with_type@amphibious_armor }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@amphibious_mechanized }
	multiply_temp_variable = { num_batt = 1.2 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@anti_tank }
	multiply_temp_variable = { num_batt = 0.3 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@anti_tank_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@mot_anti_tank_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@anti_air }
	multiply_temp_variable = { num_batt = 0.3 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@mot_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@armored_car }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@artillery }
	multiply_temp_variable = { num_batt = 0.3 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@rocket_artillery }
	multiply_temp_variable = { num_batt = 0.3 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@rocket_artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@engineer }
	multiply_temp_variable = { num_batt = 0.3 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@field_hospital }
	multiply_temp_variable = { num_batt = 0.3 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@heavy_armor }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@motorized }
	multiply_temp_variable = { num_batt = 1.2 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@mechanized }
	multiply_temp_variable = { num_batt = 1.2 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@light_armor }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@logistics_company }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@maintenance_company }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@medium_armor }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@military_police }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@modern_armor }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@recon }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@mot_recon }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@armored_car_recon }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@light_tank_recon }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@signal_company }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@light_sp_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@medium_sp_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@heavy_sp_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@super_heavy_sp_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@modern_sp_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@light_sp_artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@medium_sp_artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@heavy_sp_artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@super_heavy_sp_artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@modern_sp_artillery_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@light_sp_anti_air_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@super_heavy_armor }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@light_tank_destroyer_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@medium_tank_destroyer_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@heavy_tank_destroyer_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@super_heavy_tank_destroyer_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }

	set_temp_variable = { num_batt = num_battalions_with_type@modern_tank_destroyer_brigade }
	multiply_temp_variable = { num_batt = 0.5 }
	add_to_temp_variable = { total_manpower_need = num_batt }


	#now add to that any manpower we currently have in the deployment queue
	set_temp_variable = { num_in_queue = amount_manpower_in_deployment_queue }
	divide_temp_variable = { num_in_queue = 1000 }
	add_to_temp_variable = { total_manpower_need = num_in_queue }

	#now divide that by our maximum available manpower
	divide_temp_variable = { total_manpower_need = max_available_manpower_k }

	#now we can check to see if we are over the limit
	check_variable = { total_manpower_need > 1 }
}

needs_army_reduction_for_manpower = {
	#first check the number of units a country's factories can support
	set_temp_variable = { num_supported_units = 0 }
	set_temp_variable = { num_civs = num_of_civilian_factories }
	multiply_temp_variable = { num_civs = 0.5 }
	add_to_temp_variable = { num_supported_units = num_civs }
	set_temp_variable = { num_mils = num_of_military_factories }
	multiply_temp_variable = { num_mils = 0.5 }
	add_to_temp_variable = { num_supported_units = num_mils }

	#now check to make sure we're not under that number
	num_divisions > num_supported_units

	#and check to make sure we're low on manpower
	has_manpower < 5000
}

is_actual_major = {
	custom_trigger_tooltip = {
		tooltip = is_actual_major
		is_major = yes

		# rank the majors according to their num_of_factories
		clear_temp_array = global.actual_majors
		get_sorted_scored_countries_temp = {
			scorer = actual_majors_score
			array = global.actual_majors
		}

		# limit that ranking to the top 7
		resize_temp_array = { global.actual_majors = 7 }

		# now figure out what 70% of the average of those 7 is, with a minimum of 35
		set_temp_variable = { global.factory_average = 0 }
		all_of_scopes = {
			array = global.actual_majors
			add_to_temp_variable = { global.factory_average = THIS.num_of_factories }
		}
		divide_temp_variable = { global.factory_average = 7 }
		multiply_temp_variable = { global.factory_average = 0.7 }
		clamp_temp_variable = { var = global.factory_average min = 35 }

		OR = {
			# is actually in the top 7
			is_in_array = { global.actual_majors = THIS }
			# has more than 70% of the average of the top 7
			check_variable = { num_of_factories > global.factory_average }
			# is in the list of "always acts like a major" countries
			tag = GER
			tag = FRA
			tag = ENG
			tag = SOV
			tag = JAP
			tag = USA
			tag = ITA
		}
	}
}

is_actual_major_without_exceptions = {
	custom_trigger_tooltip = {
		tooltip = is_actual_major_without_exceptions
		is_major = yes

		# rank the majors according to their num_of_factories
		clear_temp_array = global.actual_majors
		get_sorted_scored_countries_temp = {
			scorer = actual_majors_score
			array = global.actual_majors
		}

		# limit that ranking to the top 7
		resize_temp_array = { global.actual_majors = 7 }

		# now figure out what 70% of the average of those 7 is, with a minimum of 35
		set_temp_variable = { global.factory_average = 0 }
		all_of_scopes = {
			array = global.actual_majors
			add_to_temp_variable = { global.factory_average = THIS.num_of_factories }
		}
		divide_temp_variable = { global.factory_average = 7 }
		multiply_temp_variable = { global.factory_average = 0.7 }
		clamp_temp_variable = { var = global.factory_average min = 35 }

		OR = {
			# is actually in the top 7
			is_in_array = { global.actual_majors = THIS }
			# has more than 70% of the average of the top 7
			check_variable = { num_of_factories > global.factory_average }
			# is in the list of "always acts like a major" countries
		}
	}
}

# checks for these four government types for the country in scope
has_elected_government = {
	has_country_flag = KR_is_elected
}


