use of games.strategy.triplea.attachments.UnitSupportAttachment in project triplea by triplea-game.
the class DiceRoll method getUnitPowerAndRollsForNormalBattles.
/**
* @param unitsGettingPowerFor
* should be sorted from weakest to strongest, before the method is called, for the actual battle.
*/
protected static Map<Unit, Tuple<Integer, Integer>> getUnitPowerAndRollsForNormalBattles(final List<Unit> unitsGettingPowerFor, final List<Unit> allEnemyUnitsAliveOrWaitingToDie, final boolean defending, final boolean bombing, final GameData data, final Territory location, final Collection<TerritoryEffect> territoryEffects, final boolean isAmphibiousBattle, final Collection<Unit> amphibiousLandAttackers, final Map<Unit, IntegerMap<Unit>> unitSupportPowerMap, final Map<Unit, IntegerMap<Unit>> unitSupportRollsMap) {
final Map<Unit, Tuple<Integer, Integer>> unitPowerAndRolls = new HashMap<>();
if (unitsGettingPowerFor == null || unitsGettingPowerFor.isEmpty()) {
return unitPowerAndRolls;
}
// get all supports, friendly and enemy
final Set<List<UnitSupportAttachment>> supportRulesFriendly = new HashSet<>();
final IntegerMap<UnitSupportAttachment> supportLeftFriendly = new IntegerMap<>();
final Map<UnitSupportAttachment, LinkedIntegerMap<Unit>> supportUnitsLeftFriendly = new HashMap<>();
getSupport(unitsGettingPowerFor, supportRulesFriendly, supportLeftFriendly, supportUnitsLeftFriendly, data, defending, true);
final Set<List<UnitSupportAttachment>> supportRulesEnemy = new HashSet<>();
final IntegerMap<UnitSupportAttachment> supportLeftEnemy = new IntegerMap<>();
final Map<UnitSupportAttachment, LinkedIntegerMap<Unit>> supportUnitsLeftEnemy = new HashMap<>();
getSupport(allEnemyUnitsAliveOrWaitingToDie, supportRulesEnemy, supportLeftEnemy, supportUnitsLeftEnemy, data, !defending, false);
// copy for rolls
final IntegerMap<UnitSupportAttachment> supportLeftFriendlyRolls = new IntegerMap<>(supportLeftFriendly);
final IntegerMap<UnitSupportAttachment> supportLeftEnemyRolls = new IntegerMap<>(supportLeftEnemy);
final Map<UnitSupportAttachment, LinkedIntegerMap<Unit>> supportUnitsLeftFriendlyRolls = new HashMap<>();
for (final UnitSupportAttachment usa : supportUnitsLeftFriendly.keySet()) {
supportUnitsLeftFriendlyRolls.put(usa, new LinkedIntegerMap<>(supportUnitsLeftFriendly.get(usa)));
}
final Map<UnitSupportAttachment, LinkedIntegerMap<Unit>> supportUnitsLeftEnemyRolls = new HashMap<>();
for (final UnitSupportAttachment usa : supportUnitsLeftEnemy.keySet()) {
supportUnitsLeftEnemyRolls.put(usa, new LinkedIntegerMap<>(supportUnitsLeftEnemy.get(usa)));
}
final int diceSides = data.getDiceSides();
for (final Unit current : unitsGettingPowerFor) {
// find our initial strength
int strength;
final UnitAttachment ua = UnitAttachment.get(current.getType());
if (defending) {
strength = ua.getDefense(current.getOwner());
if (isFirstTurnLimitedRoll(current.getOwner(), data)) {
strength = Math.min(1, strength);
} else {
strength += getSupport(current, supportRulesFriendly, supportLeftFriendly, supportUnitsLeftFriendly, unitSupportPowerMap, true, false);
}
strength += getSupport(current, supportRulesEnemy, supportLeftEnemy, supportUnitsLeftEnemy, unitSupportPowerMap, true, false);
} else {
strength = ua.getAttack(current.getOwner());
if (ua.getIsMarine() != 0 && isAmphibiousBattle) {
if (amphibiousLandAttackers.contains(current)) {
strength += ua.getIsMarine();
}
}
if (ua.getIsSea() && isAmphibiousBattle && Matches.territoryIsLand().test(location)) {
// change the strength to be bombard, not attack/defense, because this is a
strength = ua.getBombard();
// bombarding naval unit
}
strength += getSupport(current, supportRulesFriendly, supportLeftFriendly, supportUnitsLeftFriendly, unitSupportPowerMap, true, false);
strength += getSupport(current, supportRulesEnemy, supportLeftEnemy, supportUnitsLeftEnemy, unitSupportPowerMap, true, false);
}
strength += TerritoryEffectHelper.getTerritoryCombatBonus(current.getType(), territoryEffects, defending);
strength = Math.min(Math.max(strength, 0), diceSides);
// now determine our rolls
int rolls;
if (!bombing && strength == 0) {
rolls = 0;
} else {
if (defending) {
rolls = ua.getDefenseRolls(current.getOwner());
} else {
rolls = ua.getAttackRolls(current.getOwner());
}
rolls += getSupport(current, supportRulesFriendly, supportLeftFriendlyRolls, supportUnitsLeftFriendlyRolls, unitSupportRollsMap, false, true);
rolls += getSupport(current, supportRulesEnemy, supportLeftEnemyRolls, supportUnitsLeftEnemyRolls, unitSupportRollsMap, false, true);
rolls = Math.max(0, rolls);
if (rolls == 0) {
strength = 0;
}
}
unitPowerAndRolls.put(current, Tuple.of(strength, rolls));
}
return unitPowerAndRolls;
}
Aggregations