Search in sources :

Example 21 with IntegerMap

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

the class TripleAUnit method translateAttributesToOtherUnits.

/**
 * Currently made for translating unit damage from one unit to another unit. Will adjust damage to be within max
 * damage for the new units.
 *
 * @return change for unit's properties
 */
public static Change translateAttributesToOtherUnits(final Unit unitGivingAttributes, final Collection<Unit> unitsThatWillGetAttributes, final Territory t) {
    final CompositeChange changes = new CompositeChange();
    // must look for m_hits, m_unitDamage,
    final TripleAUnit taUnit = (TripleAUnit) unitGivingAttributes;
    final int combatDamage = taUnit.getHits();
    final IntegerMap<Unit> hits = new IntegerMap<>();
    if (combatDamage > 0) {
        for (final Unit u : unitsThatWillGetAttributes) {
            hits.put(u, combatDamage);
        }
    }
    if (hits.size() > 0) {
        changes.add(ChangeFactory.unitsHit(hits));
    }
    final int unitDamage = taUnit.getUnitDamage();
    final IntegerMap<Unit> damageMap = new IntegerMap<>();
    if (unitDamage > 0) {
        for (final Unit u : unitsThatWillGetAttributes) {
            final TripleAUnit taNew = (TripleAUnit) u;
            final int maxDamage = taNew.getHowMuchDamageCanThisUnitTakeTotal(u, t);
            final int transferDamage = Math.max(0, Math.min(unitDamage, maxDamage));
            if (transferDamage <= 0) {
                continue;
            }
            damageMap.put(u, transferDamage);
        }
    }
    changes.add(ChangeFactory.bombingUnitDamage(damageMap));
    return changes;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) CompositeChange(games.strategy.engine.data.CompositeChange) Unit(games.strategy.engine.data.Unit)

Example 22 with IntegerMap

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

the class RulesAttachment method checkUnitPresence.

/**
 * Checks for the collection of territories to see if they have units owned by the exclType alliance.
 */
private boolean checkUnitPresence(final Collection<Territory> territories, final String exclType, final int numberNeeded, final List<PlayerID> players, final GameData data) {
    boolean useSpecific = false;
    if (getUnitPresence() != null && !getUnitPresence().keySet().isEmpty()) {
        useSpecific = true;
    }
    boolean satisfied = false;
    int numberMet = 0;
    for (final Territory terr : territories) {
        final Collection<Unit> allUnits = new ArrayList<>(terr.getUnits().getUnits());
        if (exclType.equals("direct")) {
            allUnits.removeAll(CollectionUtils.getMatches(allUnits, Matches.unitIsOwnedByOfAnyOfThesePlayers(players).negate()));
        } else if (exclType.equals("allied")) {
            allUnits.retainAll(CollectionUtils.getMatches(allUnits, Matches.alliedUnitOfAnyOfThesePlayers(players, data)));
        } else if (exclType.equals("enemy")) {
            allUnits.retainAll(CollectionUtils.getMatches(allUnits, Matches.enemyUnitOfAnyOfThesePlayers(players, data)));
        } else {
            return false;
        }
        if (allUnits.size() > 0) {
            if (!useSpecific) {
                numberMet += 1;
                if (numberMet >= numberNeeded) {
                    satisfied = true;
                    if (!getCountEach()) {
                        break;
                    }
                }
            } else if (useSpecific) {
                final IntegerMap<String> unitComboMap = getUnitPresence();
                final Set<String> unitCombos = unitComboMap.keySet();
                boolean hasEnough = false;
                for (final String uc : unitCombos) {
                    final int unitsNeeded = unitComboMap.getInt(uc);
                    if (uc == null || uc.equals("ANY") || uc.equals("any")) {
                        hasEnough = allUnits.size() >= unitsNeeded;
                    } else {
                        final Set<UnitType> typesAllowed = data.getUnitTypeList().getUnitTypes(uc.split(":"));
                        hasEnough = CollectionUtils.getMatches(allUnits, Matches.unitIsOfTypes(typesAllowed)).size() >= unitsNeeded;
                    }
                    if (!hasEnough) {
                        break;
                    }
                }
                if (hasEnough) {
                    numberMet += 1;
                    if (numberMet >= numberNeeded) {
                        satisfied = true;
                        if (!getCountEach()) {
                            break;
                        }
                    }
                }
            }
        }
    }
    if (getCountEach()) {
        m_eachMultiple = numberMet;
    }
    return satisfied;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit)

Example 23 with IntegerMap

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

the class UnitAttachmentTest method testSetWhenCapturedChangesInto.

@Test
public void testSetWhenCapturedChangesInto() throws Exception {
    final SecureRandom rand = new SecureRandom();
    final String from = "any";
    final String to = "any";
    final String trueString = Boolean.toString(true);
    final String falseString = Boolean.toString(false);
    final Map<String, Tuple<String, IntegerMap<UnitType>>> mapReference = attachment.getWhenCapturedChangesInto();
    final int random1 = rand.nextInt();
    final IntegerMap<UnitType> expected1 = new IntegerMap<>(unit1, random1);
    attachment.setWhenCapturedChangesInto(concatWithColon(from, to, trueString, unit1String, String.valueOf(random1)));
    assertEquals(Tuple.of(trueString, expected1), mapReference.get(from + ":" + to));
    final int random2 = rand.nextInt();
    final int random3 = rand.nextInt();
    final IntegerMap<UnitType> expected2 = new IntegerMap<>();
    expected2.put(unit1, random2);
    expected2.put(unit2, random3);
    attachment.setWhenCapturedChangesInto(concatWithColon(from, to, falseString, unit1String, String.valueOf(random2), unit2String, String.valueOf(random3)));
    assertEquals(Tuple.of(falseString, expected2), mapReference.get(from + ":" + to));
}
Also used : IntegerMap(games.strategy.util.IntegerMap) UnitType(games.strategy.engine.data.UnitType) SecureRandom(java.security.SecureRandom) Tuple(games.strategy.util.Tuple) Test(org.junit.jupiter.api.Test)

Example 24 with IntegerMap

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

the class TripleAFrame method selectKamikazeSuicideAttacks.

public Map<Territory, IntegerMap<Unit>> selectKamikazeSuicideAttacks(final HashMap<Territory, Collection<Unit>> possibleUnitsToAttack, final Resource attackResourceToken, final int maxNumberOfAttacksAllowed) {
    if (SwingUtilities.isEventDispatchThread()) {
        throw new IllegalStateException("Should not be called from dispatch thread");
    }
    final Map<Territory, IntegerMap<Unit>> selection = new HashMap<>();
    if (possibleUnitsToAttack == null || possibleUnitsToAttack.isEmpty() || attackResourceToken == null || maxNumberOfAttacksAllowed <= 0 || messageAndDialogThreadPool == null) {
        return selection;
    }
    messageAndDialogThreadPool.waitForAll();
    final CountDownLatch continueLatch = new CountDownLatch(1);
    final Collection<IndividualUnitPanelGrouped> unitPanels = new ArrayList<>();
    SwingUtilities.invokeLater(() -> {
        final Map<String, Collection<Unit>> possibleUnitsToAttackStringForm = new HashMap<>();
        for (final Entry<Territory, Collection<Unit>> entry : possibleUnitsToAttack.entrySet()) {
            final List<Unit> units = new ArrayList<>(entry.getValue());
            units.sort(new UnitBattleComparator(false, TuvUtils.getCostsForTuv(units.get(0).getOwner(), data), TerritoryEffectHelper.getEffects(entry.getKey()), data, true, false));
            Collections.reverse(units);
            possibleUnitsToAttackStringForm.put(entry.getKey().getName(), units);
        }
        mapPanel.centerOn(data.getMap().getTerritory(possibleUnitsToAttackStringForm.keySet().iterator().next()));
        final IndividualUnitPanelGrouped unitPanel = new IndividualUnitPanelGrouped(possibleUnitsToAttackStringForm, uiContext, "Select Units to Suicide Attack using " + attackResourceToken.getName(), maxNumberOfAttacksAllowed, true, false);
        unitPanels.add(unitPanel);
        final String optionAttack = "Attack";
        final String optionNone = "None";
        final Object[] options = { optionAttack, optionNone };
        final JOptionPane optionPane = new JOptionPane(unitPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[1]);
        final JDialog dialog = new JDialog((Frame) getParent(), "Select units to Suicide Attack using " + attackResourceToken.getName());
        dialog.setContentPane(optionPane);
        dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        dialog.setLocationRelativeTo(getParent());
        dialog.setAlwaysOnTop(true);
        dialog.pack();
        dialog.setVisible(true);
        dialog.requestFocusInWindow();
        optionPane.addPropertyChangeListener(e -> {
            if (!dialog.isVisible()) {
                return;
            }
            final String option = ((String) optionPane.getValue());
            if (option.equals(optionNone)) {
                unitPanels.clear();
                selection.clear();
                dialog.setVisible(false);
                dialog.removeAll();
                dialog.dispose();
                continueLatch.countDown();
            } else if (option.equals(optionAttack)) {
                if (unitPanels.size() != 1) {
                    throw new IllegalStateException("unitPanels should only contain 1 entry");
                }
                for (final IndividualUnitPanelGrouped terrChooser : unitPanels) {
                    for (final Entry<String, IntegerMap<Unit>> entry : terrChooser.getSelected().entrySet()) {
                        selection.put(data.getMap().getTerritory(entry.getKey()), entry.getValue());
                    }
                }
                dialog.setVisible(false);
                dialog.removeAll();
                dialog.dispose();
                continueLatch.countDown();
            }
        });
    });
    mapPanel.getUiContext().addShutdownLatch(continueLatch);
    Interruptibles.await(continueLatch);
    mapPanel.getUiContext().removeShutdownLatch(continueLatch);
    return selection;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) EventThreadJOptionPane(games.strategy.util.EventThreadJOptionPane) JOptionPane(javax.swing.JOptionPane) UnitBattleComparator(games.strategy.triplea.delegate.UnitBattleComparator) Entry(java.util.Map.Entry) ResourceCollection(games.strategy.engine.data.ResourceCollection) Collection(java.util.Collection) JDialog(javax.swing.JDialog)

Example 25 with IntegerMap

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

the class ProductionRepairPanel method getProduction.

private HashMap<Unit, IntegerMap<RepairRule>> getProduction() {
    final HashMap<Unit, IntegerMap<RepairRule>> prod = new HashMap<>();
    for (final Rule rule : rules) {
        final int quantity = rule.getQuantity();
        if (quantity != 0) {
            final IntegerMap<RepairRule> repairRule = new IntegerMap<>();
            final Unit unit = rule.getUnit();
            repairRule.put(rule.getProductionRule(), quantity);
            prod.put(unit, repairRule);
        }
    }
    return prod;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) HashMap(java.util.HashMap) RepairRule(games.strategy.engine.data.RepairRule) RepairRule(games.strategy.engine.data.RepairRule) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

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