Search in sources :

Example 71 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class BattleDelegate method fireKamikazeSuicideAttacks.

/**
 * This rolls the dice and validates them to see if units died or not.
 * It will use LowLuck or normal dice.
 * If any units die, we remove them from the game, and if units take damage but live, we also do that here.
 */
private void fireKamikazeSuicideAttacks(final Unit unitUnderFire, final IntegerMap<Resource> numberOfAttacks, final IntegerMap<Resource> resourcesAndAttackValues, final PlayerID firingEnemy, final Territory location) {
    // TODO: find a way to autosave after each dice roll.
    final GameData data = getData();
    final int diceSides = data.getDiceSides();
    final CompositeChange change = new CompositeChange();
    int hits = 0;
    int[] rolls = null;
    if (Properties.getLowLuck(data)) {
        int power = 0;
        for (final Entry<Resource, Integer> entry : numberOfAttacks.entrySet()) {
            final Resource r = entry.getKey();
            final int num = entry.getValue();
            change.add(ChangeFactory.changeResourcesChange(firingEnemy, r, -num));
            power += num * resourcesAndAttackValues.getInt(r);
        }
        if (power > 0) {
            hits = power / diceSides;
            final int remainder = power % diceSides;
            if (remainder > 0) {
                rolls = bridge.getRandom(diceSides, 1, firingEnemy, DiceType.COMBAT, "Rolling for remainder in Kamikaze Suicide Attack on unit: " + unitUnderFire.getType().getName());
                if (remainder > rolls[0]) {
                    hits++;
                }
            }
        }
    } else {
        // avoid multiple calls of getRandom, so just do it once at the beginning
        final int numTokens = numberOfAttacks.totalValues();
        rolls = bridge.getRandom(diceSides, numTokens, firingEnemy, DiceType.COMBAT, "Rolling for Kamikaze Suicide Attack on unit: " + unitUnderFire.getType().getName());
        final int[] powerOfTokens = new int[numTokens];
        int j = 0;
        for (final Entry<Resource, Integer> entry : numberOfAttacks.entrySet()) {
            final Resource r = entry.getKey();
            int num = entry.getValue();
            change.add(ChangeFactory.changeResourcesChange(firingEnemy, r, -num));
            final int power = resourcesAndAttackValues.getInt(r);
            while (num > 0) {
                powerOfTokens[j] = power;
                j++;
                num--;
            }
        }
        for (int i = 0; i < rolls.length; i++) {
            if (powerOfTokens[i] > rolls[i]) {
                hits++;
            }
        }
    }
    final String title = "Kamikaze Suicide Attack attacks " + MyFormatter.unitsToText(Collections.singleton(unitUnderFire));
    final String dice = " scoring " + hits + " hits.  Rolls: " + MyFormatter.asDice(rolls);
    bridge.getHistoryWriter().startEvent(title + dice, unitUnderFire);
    if (hits > 0) {
        final UnitAttachment ua = UnitAttachment.get(unitUnderFire.getType());
        final int currentHits = unitUnderFire.getHits();
        if (ua.getHitPoints() <= currentHits + hits) {
            // TODO: kill dependents
            change.add(ChangeFactory.removeUnits(location, Collections.singleton(unitUnderFire)));
        } else {
            final IntegerMap<Unit> hitMap = new IntegerMap<>();
            hitMap.put(unitUnderFire, hits);
            change.add(createDamageChange(hitMap, bridge));
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
    // kamikaze suicide attacks, even if unsuccessful, deny the ability to bombard from this sea zone
    battleTracker.addNoBombardAllowedFromHere(location);
    // TODO: display this as actual dice for both players
    final Collection<PlayerID> playersInvolved = new ArrayList<>();
    playersInvolved.add(player);
    playersInvolved.add(firingEnemy);
    this.getDisplay().reportMessageToPlayers(playersInvolved, null, title + dice, title);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 72 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class TriggerAttachment method triggerResourceChange.

private static IntegerMap<Resource> triggerResourceChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen, final StringBuilder endOfTurnReport) {
    final GameData data = bridge.getData();
    Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, resourceMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    final IntegerMap<Resource> resources = new IntegerMap<>();
    for (final TriggerAttachment t : trigs) {
        if (testChance && !t.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            t.use(bridge);
        }
        final int eachMultiple = getEachMultiple(t);
        for (final PlayerID player : t.getPlayers()) {
            for (int i = 0; i < eachMultiple; ++i) {
                int toAdd = t.getResourceCount();
                if (t.getResource().equals(Constants.PUS)) {
                    toAdd *= Properties.getPuMultiplier(data);
                }
                resources.add(data.getResourceList().getResource(t.getResource()), toAdd);
                int total = player.getResources().getQuantity(t.getResource()) + toAdd;
                if (total < 0) {
                    toAdd -= total;
                    total = 0;
                }
                bridge.addChange(ChangeFactory.changeResourcesChange(player, data.getResourceList().getResource(t.getResource()), toAdd));
                final String puMessage = MyFormatter.attachmentNameToText(t.getName()) + ": " + player.getName() + " met a national objective for an additional " + t.getResourceCount() + " " + t.getResource() + "; end with " + total + " " + t.getResource();
                bridge.getHistoryWriter().startEvent(puMessage);
                endOfTurnReport.append(puMessage).append(" <br />");
            }
        }
    }
    return resources;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) Resource(games.strategy.engine.data.Resource)

Example 73 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class TriggerAttachment method setPlacement.

/**
 * Fudging this, it really represents adding placements.
 */
private void setPlacement(final String place) throws GameParseException {
    if (place == null) {
        m_placement = null;
        return;
    }
    final String[] s = place.split(":");
    if (s.length < 1) {
        throw new GameParseException("Empty placement list" + thisErrorMsg());
    }
    int count;
    int i = 0;
    try {
        count = getInt(s[0]);
        i++;
    } catch (final Exception e) {
        count = 1;
    }
    if (s.length < 1 || (s.length == 1 && count != -1)) {
        throw new GameParseException("Empty placement list" + thisErrorMsg());
    }
    final Territory territory = getData().getMap().getTerritory(s[i]);
    if (territory == null) {
        throw new GameParseException("Territory does not exist " + s[i] + thisErrorMsg());
    }
    i++;
    final IntegerMap<UnitType> map = new IntegerMap<>();
    for (; i < s.length; i++) {
        final UnitType type = getData().getUnitTypeList().getUnitType(s[i]);
        if (type == null) {
            throw new GameParseException("UnitType does not exist " + s[i] + thisErrorMsg());
        }
        map.add(type, count);
    }
    if (m_placement == null) {
        m_placement = new HashMap<>();
    }
    if (m_placement.containsKey(territory)) {
        map.add(m_placement.get(territory));
    }
    m_placement.put(territory, map);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException) GameParseException(games.strategy.engine.data.GameParseException)

Example 74 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class UnitAttachment method getReceivesAbilityWhenWithMap.

private static IntegerMap<Tuple<String, String>> getReceivesAbilityWhenWithMap(final Collection<Unit> units, final String filterForAbility, final GameData data) {
    final IntegerMap<Tuple<String, String>> map = new IntegerMap<>();
    final Collection<UnitType> canReceive = getUnitTypesFromUnitList(CollectionUtils.getMatches(units, Matches.unitCanReceiveAbilityWhenWith()));
    for (final UnitType ut : canReceive) {
        final Collection<String> receives = UnitAttachment.get(ut).getReceivesAbilityWhenWith();
        for (final String receive : receives) {
            final String[] s = receive.split(":");
            if (filterForAbility != null && !filterForAbility.equals(s[0])) {
                continue;
            }
            map.put(Tuple.of(s[0], s[1]), CollectionUtils.countMatches(units, Matches.unitIsOfType(data.getUnitTypeList().getUnitType(s[1]))));
        }
    }
    return map;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) UnitType(games.strategy.engine.data.UnitType) Tuple(games.strategy.util.Tuple)

Example 75 with IntegerMap

use of games.strategy.util.IntegerMap in project triplea by triplea-game.

the class UnitAttachment method setWhenCapturedChangesInto.

@VisibleForTesting
void setWhenCapturedChangesInto(final String value) throws GameParseException {
    final String[] s = value.split(":");
    if (s.length < 5 || s.length % 2 == 0) {
        throw new GameParseException("whenCapturedChangesInto must have 5 or more values, " + "playerFrom:playerTo:keepAttributes:unitType:howMany " + "(you may have additional unitType:howMany:unitType:howMany, etc" + thisErrorMsg());
    }
    final PlayerID pfrom = getData().getPlayerList().getPlayerId(s[0]);
    if (pfrom == null && !s[0].equals("any")) {
        throw new GameParseException("whenCapturedChangesInto: No player named: " + s[0] + thisErrorMsg());
    }
    final PlayerID pto = getData().getPlayerList().getPlayerId(s[1]);
    if (pto == null && !s[1].equals("any")) {
        throw new GameParseException("whenCapturedChangesInto: No player named: " + s[1] + thisErrorMsg());
    }
    getBool(s[2]);
    final IntegerMap<UnitType> unitsToMake = new IntegerMap<>();
    for (int i = 3; i < s.length; i += 2) {
        final UnitType ut = getData().getUnitTypeList().getUnitType(s[i]);
        if (ut == null) {
            throw new GameParseException("whenCapturedChangesInto: No unit named: " + s[i] + thisErrorMsg());
        }
        unitsToMake.put(ut, getInt(s[i + 1]));
    }
    m_whenCapturedChangesInto.put(s[0] + ":" + s[1], Tuple.of(s[2], unitsToMake));
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

IntegerMap (games.strategy.util.IntegerMap)132 UnitType (games.strategy.engine.data.UnitType)87 Test (org.junit.jupiter.api.Test)73 Route (games.strategy.engine.data.Route)66 Unit (games.strategy.engine.data.Unit)53 Territory (games.strategy.engine.data.Territory)39 ArrayList (java.util.ArrayList)35 PlayerID (games.strategy.engine.data.PlayerID)26 TripleAUnit (games.strategy.triplea.TripleAUnit)24 HashMap (java.util.HashMap)23 HashSet (java.util.HashSet)19 Resource (games.strategy.engine.data.Resource)16 GameData (games.strategy.engine.data.GameData)15 ProductionRule (games.strategy.engine.data.ProductionRule)14 Collection (java.util.Collection)12 List (java.util.List)12 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)10 Set (java.util.Set)10 RepairRule (games.strategy.engine.data.RepairRule)9 NamedAttachable (games.strategy.engine.data.NamedAttachable)7