Search in sources :

Example 6 with UnitSupportAttachment

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;
}
Also used : LinkedIntegerMap(games.strategy.util.LinkedIntegerMap) IntegerMap(games.strategy.util.IntegerMap) HashMap(java.util.HashMap) UnitSupportAttachment(games.strategy.triplea.attachments.UnitSupportAttachment) Unit(games.strategy.engine.data.Unit) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) LinkedIntegerMap(games.strategy.util.LinkedIntegerMap) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(games.strategy.util.Tuple) HashSet(java.util.HashSet)

Aggregations

UnitSupportAttachment (games.strategy.triplea.attachments.UnitSupportAttachment)6 Unit (games.strategy.engine.data.Unit)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 UnitType (games.strategy.engine.data.UnitType)2 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)2 IntegerMap (games.strategy.util.IntegerMap)2 LinkedIntegerMap (games.strategy.util.LinkedIntegerMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Territory (games.strategy.engine.data.Territory)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 Tuple (games.strategy.util.Tuple)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1