﻿
REPLACE:land_battle_size = {
    # Start with a baseline number of battalions equal to the amount of allied troops on the front
	value = {
		value = "scope:front.num_total_battalions(root)"
		desc = "BATTLE_SIZE_BASE_TOTAL_BATTALIONS_ON_FRONT"
	}

	# If it's a land or naval invasion defender can bring additional troops
	if = {
		limit = {
			exists = scope:front.invasion
			scope:is_advancing_side = no
		}
		add = {
			value = "scope:province.land_controller_hq.num_garrison_units"
			desc = "BATTLE_SIZE_INVASION_GARRISONED_BATTALIONS"
		}
	}

	save_temporary_value_as = num_units_on_front_and_hq

	# For regular land battles, subtract a random number of battalions depending on how many battalions are on the front compared to how long it is
	if = {
        limit = {
			exists = scope:front
        }

        subtract = {
			fixed_range = {
				min = 0
				max = {
					add = "scope:front.num_total_battalions(root)"
					subtract = {
						add = "scope:front.num_total_battalions(root)"
						divide = "scope:front.front_length"
					}
					divide = 3
				}
			}
			floor = yes
			desc = "BATTLE_SIZE_CONCENTRATION_ACROSS_LENGTH_OF_FRONT"
		}
    }

	save_temporary_value_as = pre_combat_width_cap

	# Cap to infrastructure-based combat width
	if = {
		limit = {
			scope:pre_combat_width_cap > province_terrain_infrastructure_force_limit
		}
		max = {
			value = province_terrain_infrastructure_force_limit
			ceiling = yes
			desc = "BATTLE_SIZE_TERRAIN_INFRASTRUCTRE"
		}
	}

	save_temporary_value_as = pre_numeric_advantage

    # In a regular land battle, if we have a general and outnumber the enemy we may be able to bring additional troops
	if = {
        limit = {
			scope:is_naval_invasion = no
            exists = scope:own_commander
            exists = scope:front
			"scope:front.num_total_battalions(root)" > "scope:front.num_total_battalions(scope:enemy_country)"
			"scope:front.num_total_battalions(scope:enemy_country)" > 0
		}
		value = {
			add = scope:pre_numeric_advantage
			multiply = {
				fixed_range = {
					min = 1
					max = {
						add = "scope:front.num_total_battalions(root)"
						divide = "scope:front.num_total_battalions(scope:enemy_country)"
						subtract = 1
						multiply = 1.5
						add = 1
						max = 10
					}
				}
			}
			ceiling = yes
			desc = "BATTLE_SIZE_NUMERIC_ADVANTAGE"
		}
    }

	save_temporary_value_as = pre_max_amount_cap

	# Multiply by any total combat width modifiers
	multiply = {
		value = {
			add = 1
			add = modifier:battle_total_combat_width_mult
		}
		desc = "battle_total_combat_width_mult"
	}

    # Cap to *actually available* troops
	if = {
		limit = {
			scope:is_advancing_side = yes
			scope:pre_max_amount_cap > {
				add = "scope:front.num_total_battalions(root)"
				subtract = "scope:front.num_defending_battalions(root)"
			}
		}
		max = {
			add = "scope:front.num_total_battalions(root)"
			subtract = "scope:front.num_defending_battalions(root)"
			desc = "BATTLE_SIZE_MAX_AMOUNT_REACHED"
		}
	}
	else_if = {
		limit = {
			scope:is_advancing_side = no
			scope:pre_max_amount_cap > scope:num_units_on_front_and_hq
		}
		max = {
			add = scope:num_units_on_front_and_hq
			desc = "BATTLE_SIZE_MAX_AMOUNT_REACHED"
		}
	}

	save_temporary_value_as = pre_min_amount_cap

	# If we're advancing, ensure at least 1 Battalion is doing so
    if = {
        limit = {
            scope:is_advancing_side = yes
			scope:pre_min_amount_cap < 1
        }
        min = {
			value = 1
			desc = "BATTLE_SIZE_MIN_AMOUNT_REACHED"
		}
    }
}