# root = country
# scope:war = the war we're evaluating war exhaustion for
# No randomness is allowed in this script value

war_exhaustion_from_enemy_contested_wargoals = {
	add = define:NWar|WAR_EXHAUSTION_CONTESTED_ENEMY_WARGOALS
	multiply = "enemy_contested_wargoals(scope:war)"
}

military_size = {
	add = army_size_including_conscripts
	add = navy_size
}

war_exhaustion_from_casualties = {
	add = "scope:war.num_country_casualties(root)"
	if = {
		limit = { military_size > 0 }
		divide = military_size
	}
	divide = 2000
	multiply = define:NWar|WAR_EXHAUSTION_KIA_FACTOR
	multiply = {
		add = define:NWar|WAR_EXHAUSTION_KIA_BASE_MULTIPLIER
		add = {
			add = "size_weighted_lost_battles_fraction(scope:war)"
			multiply = define:NWar|WAR_EXHAUSTION_KIA_BATTLES_LOST_MULTIPLIER
		}
	}
}

war_exhaustion_from_lobby_clout = {
	add = "lobby_war_opposition(scope:war)"
	multiply = define:NWar|WAR_EXHAUSTION_ANTI_WAR_LOBBY_CLOUT_FACTOR
}

war_support_from_lobby_clout = {
	add = "lobby_war_support(scope:war)"
	multiply = define:NWar|WAR_EXHAUSTION_PRO_WAR_LOBBY_CLOUT_FACTOR
}

war_exhaustion_from_country_turmoil = {
	add = country_turmoil
	multiply = define:NWar|WAR_EXHAUSTION_TURMOIL_FACTOR
}

war_exhaustion = {
	# save all values that make up the war exhaustion calculation since we frequently refer to them numerous times below
	value = enemy_occupation
	save_temporary_value_as = temp_enemy_occupation
	value = war_exhaustion_from_casualties
	save_temporary_value_as = temp_war_exhaustion_from_casualties
	value = war_exhaustion_from_enemy_contested_wargoals
	save_temporary_value_as = temp_war_exhaustion_from_enemy_contested_wargoals
	value = "scope:war.war_exhaustion_from_acceptance_of_dead(root)"
	save_temporary_value_as = temp_war_exhaustion_from_acceptance_of_dead
	value = war_exhaustion_from_country_turmoil
	save_temporary_value_as = temp_war_exhaustion_from_country_turmoil
	value = war_exhaustion_from_lobby_clout
	save_temporary_value_as = temp_war_exhaustion_from_lobby_clout
	value = war_support_from_lobby_clout
	save_temporary_value_as = temp_war_support_from_lobby_clout
	# end save temp values

	# reset the calculation to the base
	value = {
		add = define:NWar|WAR_EXHAUSTION_BASE
		desc = "WAR_EXHAUSTION_BASE"
	}

	add = {
		if = {
			limit = {
				scope:temp_enemy_occupation <= 0.0
			}
			add = {
				add = 0.0
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_NONE"
			}
		}
		else_if = {
			limit = {
				scope:temp_enemy_occupation < 0.05
			}
			add = {
				add = 0.2
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_LOW"
			}
		}
		else_if = {
			limit = {
				scope:temp_enemy_occupation < 0.15
			}
			add = {
				add = 1
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_MODERATE"
			}
		}
		else_if = {
			limit = {
				scope:temp_enemy_occupation < 0.2
			}
			add = {
				add = 2
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_SUBSTANTIAL"
			}
		}
		else_if = {
			limit = {
				scope:temp_enemy_occupation < 0.5
			}
			add = {
				add = 5
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_HIGH"
			}
		}
		else_if = {
			limit = {
				scope:temp_enemy_occupation < 0.75
			}
			add = {
				add = 10
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_EXTREME"
			}
		}
		else_if = {
			limit = {
				scope:temp_enemy_occupation <= 1.0
			}
			add = {
				add = 50
				desc = "WAR_EXHAUSTION_FROM_ENEMY_OCCUPATION_TOTAL"
			}
		}
	}

	if = {
		limit = { scope:temp_war_exhaustion_from_enemy_contested_wargoals >= 0.01 }
		add = {
			add = scope:temp_war_exhaustion_from_enemy_contested_wargoals
			desc = "WAR_EXHAUSTION_FROM_ENEMY_CONTESTED_WARGOALS"
		}
	}

	if = {
		limit = { scope:temp_war_exhaustion_from_casualties >= 0.01 }
		add = {
			add = scope:temp_war_exhaustion_from_casualties
			desc = "WAR_EXHAUSTION_FROM_CASUALTIES"
			multiply = {
				add = 1
				add = modifier:country_war_exhaustion_casualties_mult
				min = 0
				desc = "WAR_EXHAUSTION_FROM_country_war_exhaustion_casualties_mult"
			}
		}
	}

	if = {
		limit = { scope:temp_war_exhaustion_from_acceptance_of_dead >= 0.01 }
		add = {
			add = scope:temp_war_exhaustion_from_acceptance_of_dead
			desc = "WAR_EXHAUSTION_FROM_ACCEPTANCE_OF_DEAD"
		}
	}

	if = {
		limit = { scope:temp_war_exhaustion_from_country_turmoil >= 0.01 }
		add = {
			add = scope:temp_war_exhaustion_from_country_turmoil
			desc = "WAR_EXHAUSTION_FROM_TURMOIL"
		}
	}

	if = {
		limit = { scope:temp_war_exhaustion_from_lobby_clout >= 0.01 }
		add = {
			add = scope:temp_war_exhaustion_from_lobby_clout
			desc = "WAR_OPPOSITION_FROM_LOBBIES"
		}
	}

	if = {
		limit = { scope:temp_war_support_from_lobby_clout <= -0.01 }
		add = {
			add = scope:temp_war_support_from_lobby_clout
			desc = "WAR_SUPPORT_FROM_LOBBIES"
		}
	}

	add = {
		add = "additional_war_exhaustion(scope:war.diplomatic_play)"
		desc = "WAR_EXHAUSTION_FROM_EVENTS"
	}

	max = define:NWar|WAR_EXHAUSTION_MAX
}
