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;
}
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;
}
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));
}
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;
}
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;
}
Aggregations