﻿# the weight of being picked when a new battle occurs on a Front
# root = commander
# is_advancing_side = whether this side is the one advancing or not (defending = no)
REPLACE:front_commander_pick_weight = {
	# base
	value = root.command_limit_num_units
	# defend
	if = {
		limit = { scope:is_advancing_side = no }
		# weight from commander order
		if = {
			limit = {
				OR = {
					has_commander_order = defend_dig_in # defense_mult 0.15
					has_commander_order = defend # defense_mult 0.10
					has_commander_order = defend_desperate_charge # defense_mult 0.00
					has_commander_order = defend_last_stand # defense_mult 0.20
					has_commander_order = defend_guerilla_warfare # defense_mult 0.10
				}
			}
			add = 20
		}
		# weight from defensive skill trait 1
		if = {
			limit = { has_trait = expert_defensive_strategist } # defense_mult 0.30
			add = 30
		}
		else_if = {
			limit = { has_trait = experienced_defensive_strategist } # defense_mult 0.20
			add = 20
		}
		else_if = {
			limit = { has_trait = basic_defensive_strategist } # defense_mult 0.10
			add = 10
		}
		# weight from defensive skill trait 2
		if = {
			limit = { has_trait = defense_in_depth_specialist } # defense_add 20
			add = 30
		}
		else_if = {
			limit = { has_trait = trench_rat } # defense_add 10
			add = 20
		}
		else_if = {
			limit = { has_trait = stalwart_defender } # defense_mult 0.10
			add = 10
		}
	}
	# advance
	else = {
		if = {
			limit = { has_trait = expert_offensive_planner }
			add = 15
		}
		else_if = {
			limit = { has_trait = experienced_offensive_planner }
			add = 10
		}
		else_if = {
			limit = { has_trait = basic_offensive_planner }
			add = 5
		}
	}
	# Commanders shouldn't be picked for multiple battles
	if = {
		limit = {
			is_in_battle = yes
		}
		multiply = 0
	}
}


# root = state
# front = the front we're evaluating basecamp states for
# own_commander = the commander leading this side in battle
# is_strategic_objective = indicates whether the root state is the Front's Strategic Objective
# is_adjacent_to_strategic_objective = indicates whether the root state is adjacent to the Front's Strategic Objective
# distance_to_strategic_objective = *squared* distance to this front active Strategic Objective, if any (use `exists` to check). Always > 0.0 when it exists.
# distance_to_closest_war_goal = *squared* distance to this front closest War Goal, if any (use `exists` to check). Always > 0.0 when it exists.
# if the console variable `debug_front_battle_weight` is enabled, data will be logged to std::cout.
# No randomness allowed

REPLACE:front_battle_state_weight = {
	# modded: wargoal
	if = {
		limit = {
			OR = {
				is_target_of_wargoal = scope:own_commander.owner
				AND = {
					is_capital = yes
					owner = { is_subject = no }
				}
			}
		}
		value = 100000
	}
	else = {
		value = 5000
		# vanilla
		if = {
			limit = {
				scope:is_strategic_objective = yes
			}
			add = 1000
		}
		else_if = {
			limit = {
				scope:is_adjacent_to_strategic_objective = yes
			}
			add = 100
		}
		else_if = {
			limit = {
				exists = scope:distance_to_strategic_objective
			}
			divide = scope:distance_to_strategic_objective
		}
		else_if = {
			limit = {
				exists = scope:distance_to_closest_war_goal
			}
			divide = scope:distance_to_closest_war_goal
		}
		if = { # Strategic objective for the Carlist Royal Expedition
			limit = {
				exists = scope:own_commander
				scope:own_commander.owner = {
					c:SPC ?= this
					is_ai = yes
					is_involved_in_journal_entry = je_the_first_carlist_war
					has_modifier = the_last_stand

				}
			}
			add = 100000
		}
	}
}

# root = province
# front = the front this battle will happen on
# own_commander = the commander leading this side in battle
# enemy_commander = the commander defending in this battle (if any)
# is_strategic_objective = indicates whether the root province lies in the Front Strategic Objective
# is_adjacent_to_strategic_objective = indicates whether the root province is adjacent to the Front's Strategic Objective
# distance_to_strategic_objective = *squared* distance to this front active Strategic Objective, if any (use `exists` to check). Always > 0.0 when it exists.
# distance_to_closest_war_goal = *squared* distance to this front closest War Goal, if any (use `exists` to check). Always > 0.0 when it exists.
# if the console variable `debug_front_battle_weight` is enabled, data will be logged to std::cout.
# No randomness allowed

REPLACE:front_battle_province_weight = {
	# modded: wargoal
	if = {
		limit = {
			state = { is_target_of_wargoal = scope:own_commander.owner }
		}
		value = 100000
	}
	else = {
		value = 50000
		# vanilla
		if = {
			limit = {
				exists = scope:distance_to_strategic_objective
			}
			divide = scope:distance_to_strategic_objective
		}
		else_if = {
			limit = {
				exists = scope:distance_to_closest_war_goal
			}
			divide = scope:distance_to_closest_war_goal
		}
	}
}

# the battalion size for each side in a land battle
# root = a country on the side being evaluated (this general's country, or this theater side owner if none)
# enemy_country = a country on the opposing side (the other general's country, or that theater side owner if none)
# front = the front to calculate troop weight for
# own_commander = the commander leading this side in battle (if any; could be garrison)
# enemy_commander = the commander leading this side in battle (if any; could be garrison)
# is_advancing_side = whether this side is the one advancing or not (defending = no)
# is_naval_invasion = if the battle is related to a naval invasion
# province = the province where the battle is happening

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"
		}
    }
}

