Search in sources :

Example 16 with CasualtyDetails

use of games.strategy.triplea.delegate.dataObjects.CasualtyDetails in project triplea by triplea-game.

the class BattleCalculatorTest method testAaCasualtiesLowLuckMixedWithChooseAaCasualties.

@Test
public void testAaCasualtiesLowLuckMixedWithChooseAaCasualties() {
    final GameData data = bridge.getData();
    makeGameLowLuck(data);
    setSelectAaCasualties(data, true);
    // 6 bombers and 6 fighters
    final Collection<Unit> planes = bomber(data).create(6, british(data));
    planes.addAll(fighter(data).create(6, british(data)));
    final Collection<Unit> defendingAa = territory("Germany", data).getUnits().getMatches(Matches.unitIsAaForAnything());
    when(dummyPlayer.selectCasualties(any(), any(), anyInt(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean())).thenAnswer(new Answer<CasualtyDetails>() {

        @Override
        public CasualtyDetails answer(final InvocationOnMock invocation) {
            final Collection<Unit> selectFrom = invocation.getArgument(0);
            final int count = invocation.getArgument(2);
            final List<Unit> selected = CollectionUtils.getNMatches(selectFrom, count, Matches.unitIsStrategicBomber());
            return new CasualtyDetails(selected, new ArrayList<>(), false);
        }
    });
    bridge.setRemote(dummyPlayer);
    // don't allow rolling, 6 of each is deterministic
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { ScriptedRandomSource.ERROR }));
    final DiceRoll roll = DiceRoll.rollAa(CollectionUtils.getMatches(planes, Matches.unitIsOfTypes(UnitAttachment.get(defendingAa.iterator().next().getType()).getTargetsAa(data))), defendingAa, bridge, territory("Germany", data), true);
    final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, germans(data), british(data), null, territory("Germany", data), null, false, null).getKilled();
    assertEquals(2, casualties.size());
    // we selected all bombers
    assertEquals(2, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
    assertEquals(0, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
Also used : TestMapGameData(games.strategy.triplea.xml.TestMapGameData) GameData(games.strategy.engine.data.GameData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) ArrayList(java.util.ArrayList) List(java.util.List) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Test(org.junit.jupiter.api.Test)

Example 17 with CasualtyDetails

use of games.strategy.triplea.delegate.dataObjects.CasualtyDetails in project triplea by triplea-game.

the class BattleDisplay method getCasualties.

CasualtyDetails getCasualties(final Collection<Unit> selectFrom, final Map<Unit, Collection<Unit>> dependents, final int count, final String message, final DiceRoll dice, final PlayerID hit, final CasualtyList defaultCasualties, final boolean allowMultipleHitsPerUnit) {
    if (SwingUtilities.isEventDispatchThread()) {
        throw new IllegalStateException("This method should not be run in the event dispatch thread");
    }
    final AtomicReference<CasualtyDetails> casualtyDetails = new AtomicReference<>(new CasualtyDetails());
    final CountDownLatch continueLatch = new CountDownLatch(1);
    SwingUtilities.invokeLater(() -> {
        final boolean isEditMode = (dice == null);
        if (!isEditMode) {
            actionLayout.show(actionPanel, DICE_KEY);
            dicePanel.setDiceRoll(dice);
        }
        final boolean plural = isEditMode || (count > 1);
        final String countStr = isEditMode ? "" : "" + count;
        final String btnText = hit.getName() + " select " + countStr + (plural ? " casualties" : " casualty");
        actionButton.setAction(new AbstractAction(btnText) {

            private static final long serialVersionUID = -2156028313292233568L;

            private UnitChooser chooser;

            private JScrollPane chooserScrollPane;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final String messageText = message + " " + btnText + ".";
                if (chooser == null || chooserScrollPane == null) {
                    chooser = new UnitChooser(selectFrom, defaultCasualties, dependents, allowMultipleHitsPerUnit, mapPanel.getUiContext());
                    chooser.setTitle(messageText);
                    if (isEditMode) {
                        chooser.setMax(selectFrom.size());
                    } else {
                        chooser.setMax(count);
                    }
                    chooserScrollPane = new JScrollPane(chooser);
                    final Dimension screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
                    final int availHeight = screenResolution.height - 130;
                    final int availWidth = screenResolution.width - 30;
                    chooserScrollPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                    final Dimension size = chooserScrollPane.getPreferredSize();
                    chooserScrollPane.setPreferredSize(new Dimension(Math.min(availWidth, size.width + (size.height > availHeight ? chooserScrollPane.getVerticalScrollBar().getPreferredSize().width : 0)), Math.min(availHeight, size.height)));
                }
                final String[] options = { "Ok", "Cancel" };
                final String focus = ClientSetting.SPACE_BAR_CONFIRMS_CASUALTIES.booleanValue() ? options[0] : null;
                final int option = JOptionPane.showOptionDialog(BattleDisplay.this, chooserScrollPane, hit.getName() + " select casualties", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, focus);
                if (option != 0) {
                    return;
                }
                final List<Unit> killed = chooser.getSelected(false);
                final List<Unit> damaged = chooser.getSelectedDamagedMultipleHitPointUnits();
                if (!isEditMode && (killed.size() + damaged.size() != count)) {
                    JOptionPane.showMessageDialog(BattleDisplay.this, "Wrong number of casualties selected", hit.getName() + " select casualties", JOptionPane.ERROR_MESSAGE);
                } else {
                    final CasualtyDetails response = new CasualtyDetails(killed, damaged, false);
                    casualtyDetails.set(response);
                    dicePanel.clear();
                    actionButton.setEnabled(false);
                    actionButton.setAction(nullAction);
                    continueLatch.countDown();
                }
            }
        });
    });
    mapPanel.getUiContext().addShutdownLatch(continueLatch);
    Interruptibles.await(continueLatch);
    mapPanel.getUiContext().removeShutdownLatch(continueLatch);
    return casualtyDetails.get();
}
Also used : JScrollPane(javax.swing.JScrollPane) ActionEvent(java.awt.event.ActionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Dimension(java.awt.Dimension) CountDownLatch(java.util.concurrent.CountDownLatch) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) List(java.util.List) ArrayList(java.util.ArrayList) CasualtyList(games.strategy.triplea.delegate.dataObjects.CasualtyList) JList(javax.swing.JList) AbstractAction(javax.swing.AbstractAction)

Aggregations

CasualtyDetails (games.strategy.triplea.delegate.dataObjects.CasualtyDetails)17 ArrayList (java.util.ArrayList)11 Unit (games.strategy.engine.data.Unit)10 GameData (games.strategy.engine.data.GameData)6 CasualtyList (games.strategy.triplea.delegate.dataObjects.CasualtyList)6 List (java.util.List)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)4 Collection (java.util.Collection)4 Test (org.junit.jupiter.api.Test)4 TripleAUnit (games.strategy.triplea.TripleAUnit)3 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)2 PlayerID (games.strategy.engine.data.PlayerID)2 Route (games.strategy.engine.data.Route)2 Territory (games.strategy.engine.data.Territory)2 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 UnitType (games.strategy.engine.data.UnitType)1