﻿# 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)
front_commander_pick_weight = {
    value = root.num_units

    if = {
        limit = {
			scope:is_advancing_side = no
			has_commander_order = defend
        }
        
        multiply = 4
    }
}

# the troop distribution weight of a Front for a country/commander
# root = country
# front = the front to calculate troop weight for
# character = the country commander (if any; could be garrison)
front_country_troop_weight = {
    value = "scope:front.num_enemy_units(root)"

    if = {
        limit = {
            exists = scope:character
            scope:character = {
                is_advancing_on_front = scope:front
            }
        }

        min = 5
        multiply = 4
    }
}

# root = province
# front = the front this battle will happen on
# 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)
front_battle_province_weight = {
    value = 10
	
	add = {
		value = state.infrastructure
		divide = 2
		multiply = combat_width
	}
	
	# Increase weights for provinces in capital states
	if = {
		limit = {
			state = { is_capital = yes }
		}
		multiply = 10
	}	
	
	# Increase weight for provinces in states that contain wargoals, or are adjacent to states that contain wargoals
	if = {
		limit = {
			exists = scope:own_commander
			state = { is_target_of_wargoal = scope:own_commander.owner }
		}
		multiply = 10
	}
	else_if = {
		limit = {
			exists = scope:own_commander
			state = { any_neighbouring_state = { is_target_of_wargoal = scope:own_commander.owner } }
		}
		multiply = 6
	}
	else_if = {
		limit = {
			exists = scope:own_commander
			state = { 
				any_neighbouring_state = { 					
					any_neighbouring_state = { 
						is_target_of_wargoal = scope:own_commander.owner 
					}
				} 
				
			}
		}
		multiply = 3
	}	
}

# 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 (if any; not during Naval Invasions)
# 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 a naval landing battle
# province = the province where the battle is happening
land_battle_size = {
    # Start with a baseline number of battalions equal to the amount of allied troops on the front
    if = {
        limit = {
            exists = scope:front
        }

        value = "scope:front.num_total_battalions(root)"
    }
	else_if = {
        limit = {
            exists = scope:own_commander
			scope:is_advancing_side = yes
        }

        value = "scope:own_commander.num_mobilized_battalions"	
	}
	else_if = { # For naval invasions
        limit = {
            exists = scope:province.land_hq
			scope:is_advancing_side = no
        }

        value = "scope:province.land_hq.num_garrison_units"	
	}		
	else_if = { # Fallback
        limit = {
            exists = scope:own_commander
        }

        value = "scope:own_commander.num_battalions"	
	}	
	
	# Cap to infrastructure-based combat width
	if = {
        limit = {
            exists = scope:province
        }

		max = {
			value = 5
			add = {
				value = scope:province.state.infrastructure
				divide = 2
			}
			multiply = scope:province.combat_width
		}
		desc = "BATTLE_SIZE_COMBAT_WIDTH"	
	}

    # REMOVED: If we are defending and the enemy baseline force has higher combat stats where it matters, increase your force
	if = {
        limit = {
            exists = scope:own_commander
            exists = scope:front
            scope:is_advancing_side = no
            "scope:front.average_offense(scope:enemy_country)" > "scope:front.average_defense(root)"
        }
        multiply = {
            fixed_range = {
                min = 1
                max = {
                    add = "scope:front.average_offense(scope:enemy_country)"
                    divide = "scope:front.average_defense(root)"
					multiply = 0.3
					add = 1
					max = 1
                }
            }
        }
		desc = "BATTLE_SIZE_ENEMY_SKILL_ADVANTAGE"
    }

    #  If we are advancing and the enemy baseline force has lower combat stats where it matters, increase your force (less than when defending)
	if = {
        limit = {
            exists = scope:own_commander
            exists = scope:front
            scope:is_advancing_side = yes
            "scope:front.average_offense(root)" > "scope:front.average_defense(scope:enemy_country)"
        }
        multiply = {
                    add = "scope:front.average_offense(root)"
                    divide = "scope:front.average_defense(scope:enemy_country)"
					add = 0
					max = 2
        }
		desc = "BATTLE_SIZE_ENEMY_SKILL_ADVANTAGE"
    }

    # Add additional force if we outnumber the enemy
    if = {
        limit = {
            exists = scope:front
            "scope:front.num_total_battalions(root)" > "scope:front.num_total_battalions(scope:enemy_country)"
        }
        multiply = {
            add = "scope:front.num_total_battalions(root)"
            divide = "scope:front.num_total_battalions(scope:enemy_country)"
            add = -1
            multiply = 0.25
            add = 1
            max = 2
        }
    }

    # Cap to *actually available* troops; either the total number, or if own_commander is advancing, exclude defending commanders' troops
    if = {
        limit = {
            exists = scope:own_commander
            exists = scope:front
            scope:is_advancing_side = yes
        }
        max = {
            add = "scope:front.num_total_battalions(root)"
            subtract = "scope:front.num_defending_battalions(root)"
        }
		desc = "BATTLE_SIZE_MAX_AMOUNT_REACHED"
    }
	else_if = {
        limit = {
            exists = scope:front
        }

        max = "scope:front.num_total_battalions(root)"
	}
	else_if = {
        limit = {
           exists = scope:own_commander
		    scope:is_advancing_side = yes
        }

        max = "scope:own_commander.num_mobilized_battalions"
	}
	else_if = { # For naval invasions
        limit = {
            exists = scope:province.land_hq
			scope:is_advancing_side = no
        }

        max = "scope:province.land_hq.num_garrison_units"	
	}		
	else_if = { # Fallback
        limit = {
           exists = scope:own_commander
        }

        max = "scope:own_commander.num_battalions"
	}		
	
    # Reduce final troops by up to one-third for attacker (random)
    if = {
        limit = {
            exists = scope:own_commander
			scope:is_advancing_side = yes
        }
        multiply = {
            fixed_range = {
                min = 0.9
                max = 0.9
            }
        }
		desc = "BATTLE_SIZE_LUCK"
    }		
	
    # Reduce final troops by up to half for defender (random)
    if = {
        limit = {
            exists = scope:own_commander
			scope:is_advancing_side = no
        }
		multiply = {
            fixed_range = {
                min = 1
                max = 1
            }
        }
		desc = "BATTLE_SIZE_LUCK"
    }
	
	# If own_commander is advancing, ensure at least 1 Battalion is doing so
    if = {
        limit = {
            exists = scope:own_commander
            scope:is_advancing_side = yes
        }
        min = 1
		desc = "BATTLE_SIZE_MIN_AMOUNT_REACHED"
    }
}


# root = country
# own_commander = the commander leading this side in battle (if any; could be garrison)
# province = land province this battle takes place in
land_battle_unit_selection_weight = {
     value = {
		integer_range = {
			min = 1
			max = 100
		}
	}
}

# the warships size for each side in a naval battle
# root = country
# enemy_country = the other country
# 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)
naval_battle_size = {
     if = {
        limit = {
            exists = scope:own_commander
        }
        value = scope:own_commander.num_units
    }
}

# root = country
# own_commander = the commander leading this side in battle
# province = naval province this battle takes place in
naval_battle_unit_selection_weight = {
     value = 1
}

# the weight for how likely a unit is to take casualties over others
# root = combat unit
battle_unit_casualty_weight = {
    value = 1
}
