Search in sources :

Example 6 with CasualtyDetails

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

the class AbstractAi method selectCasualties.

@Override
public CasualtyDetails selectCasualties(final Collection<Unit> selectFrom, final Map<Unit, Collection<Unit>> dependents, final int count, final String message, final DiceRoll dice, final PlayerID hit, final Collection<Unit> friendlyUnits, final PlayerID enemyPlayer, final Collection<Unit> enemyUnits, final boolean amphibious, final Collection<Unit> amphibiousLandAttackers, final CasualtyList defaultCasualties, final GUID battleId, final Territory battlesite, final boolean allowMultipleHitsPerUnit) {
    if (defaultCasualties.size() != count) {
        throw new IllegalStateException("Select Casualties showing different numbers for number of hits to take vs total " + "size of default casualty selections");
    }
    if (defaultCasualties.getKilled().size() <= 0) {
        return new CasualtyDetails(defaultCasualties, false);
    }
    final CasualtyDetails myCasualties = new CasualtyDetails(false);
    myCasualties.addToDamaged(defaultCasualties.getDamaged());
    final List<Unit> selectFromSorted = new ArrayList<>(selectFrom);
    final int numberOfPlanesThatDoNotNeedToLandOnCarriers = 0;
    final List<Unit> interleavedTargetList = new ArrayList<>(AiUtils.interleaveCarriersAndPlanes(selectFromSorted, numberOfPlanesThatDoNotNeedToLandOnCarriers));
    for (int i = 0; i < defaultCasualties.getKilled().size(); ++i) {
        myCasualties.addToKilled(interleavedTargetList.get(i));
    }
    if (count != myCasualties.size()) {
        throw new IllegalStateException("AI chose wrong number of casualties");
    }
    return myCasualties;
}
Also used : ArrayList(java.util.ArrayList) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) Unit(games.strategy.engine.data.Unit)

Example 7 with CasualtyDetails

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

the class WW2V3Year41Test method testAttackDestroyerAndSubsAgainstSubAndDestroyer.

@Test
public void testAttackDestroyerAndSubsAgainstSubAndDestroyer() {
    final String defender = "Germans";
    final String attacker = "British";
    final Territory attacked = territory("31 Sea Zone", gameData);
    final Territory from = territory("32 Sea Zone", gameData);
    // 1 sub and 1 destroyer attack 1 sub and 1 destroyer
    // no sneak attacks
    addTo(from, submarine(gameData).create(1, british(gameData)));
    addTo(from, destroyer(gameData).create(1, british(gameData)));
    addTo(attacked, submarine(gameData).create(1, germans(gameData)));
    addTo(attacked, destroyer(gameData).create(1, germans(gameData)));
    final ITestDelegateBridge bridge = getDelegateBridge(british(gameData));
    bridge.setStepName("CombatMove");
    moveDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
    moveDelegate(gameData).start();
    move(from.getUnits().getUnits(), new Route(from, attacked));
    moveDelegate(gameData).end();
    final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(attacked, false, null);
    final List<String> steps = battle.determineStepStrings(true);
    assertEquals(Arrays.asList(attacker + SUBS_FIRE, defender + SELECT_SUB_CASUALTIES, attacker + FIRE, defender + SELECT_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, defender + FIRE, attacker + SELECT_CASUALTIES, REMOVE_CASUALTIES, attacker + ATTACKER_WITHDRAW).toString(), steps.toString());
    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);
            return new CasualtyDetails(Arrays.asList(selectFrom.iterator().next()), new ArrayList<>(), false);
        }
    });
    bridge.setRemote(dummyPlayer);
    // attacking subs sneak attack and hit
    // no chance to return fire
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 0, 0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(4, randomSource.getTotalRolled());
    assertEquals(0, attacked.getUnits().size());
}
Also used : Territory(games.strategy.engine.data.Territory) ArrayList(java.util.ArrayList) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 8 with CasualtyDetails

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

the class RevisedTest method testAttackDestroyerAndSubsAgainstSubAndDestroyer.

@Test
public void testAttackDestroyerAndSubsAgainstSubAndDestroyer() {
    final String defender = "Germans";
    final String attacker = "British";
    final Territory attacked = territory("31 Sea Zone", gameData);
    final Territory from = territory("32 Sea Zone", gameData);
    // 1 sub and 1 destroyer attack 1 sub and 1 destroyer
    // defender sneak attacks, not attacker
    addTo(from, submarine(gameData).create(1, british(gameData)));
    addTo(from, destroyer(gameData).create(1, british(gameData)));
    addTo(attacked, submarine(gameData).create(1, germans(gameData)));
    addTo(attacked, destroyer(gameData).create(1, germans(gameData)));
    final ITestDelegateBridge bridge = getDelegateBridge(british(gameData));
    bridge.setStepName("CombatMove");
    moveDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
    moveDelegate(gameData).start();
    move(from.getUnits().getUnits(), new Route(from, attacked));
    moveDelegate(gameData).end();
    final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(attacked, false, null);
    final List<String> steps = battle.determineStepStrings(true);
    assertEquals(Arrays.asList(attacker + SUBS_FIRE, defender + SELECT_SUB_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, attacker + FIRE, defender + SELECT_CASUALTIES, defender + FIRE, attacker + SELECT_CASUALTIES, REMOVE_CASUALTIES, attacker + SUBS_SUBMERGE, defender + SUBS_SUBMERGE, attacker + ATTACKER_WITHDRAW).toString(), steps.toString());
    final List<IExecutable> execs = battle.getBattleExecutables(false);
    final int attackSubs = getIndex(execs, MustFightBattle.AttackSubs.class);
    final int defendSubs = getIndex(execs, MustFightBattle.DefendSubs.class);
    assertTrue(attackSubs < defendSubs);
    when(dummyPlayer.selectCasualties(any(), any(), anyInt(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean())).thenAnswer(invocation -> {
        final Collection<Unit> selectFrom = invocation.getArgument(0);
        return new CasualtyDetails(Collections.singletonList(selectFrom.iterator().next()), new ArrayList<>(), false);
    });
    bridge.setRemote(dummyPlayer);
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 0, 0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(4, randomSource.getTotalRolled());
    assertEquals(0, attacked.getUnits().size());
}
Also used : Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 9 with CasualtyDetails

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

the class RevisedTest method setUp.

/**
 * Sets up a GameData object for testing..
 */
@BeforeEach
public void setUp() throws Exception {
    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 CasualtyList defaultCasualties = invocation.getArgument(11);
            if (defaultCasualties != null) {
                return new CasualtyDetails(defaultCasualties.getKilled(), defaultCasualties.getDamaged(), true);
            }
            return null;
        }
    });
    gameData = TestMapGameData.REVISED.getGameData();
}
Also used : CasualtyList(games.strategy.triplea.delegate.dataObjects.CasualtyList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with CasualtyDetails

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

the class StrategicBombingRaidBattle method calculateCasualties.

private CasualtyDetails calculateCasualties(final Collection<Unit> validAttackingUnitsForThisRoll, final Collection<Unit> defendingAa, final IDelegateBridge bridge, final DiceRoll dice, final String currentTypeAa) {
    getDisplay(bridge).notifyDice(dice, SELECT_PREFIX + currentTypeAa + CASUALTIES_SUFFIX);
    final boolean isEditMode = BaseEditDelegate.getEditMode(m_data);
    final boolean allowMultipleHitsPerUnit = !defendingAa.isEmpty() && defendingAa.stream().allMatch(Matches.unitAaShotDamageableInsteadOfKillingInstantly());
    if (isEditMode) {
        final String text = currentTypeAa + AA_GUNS_FIRE_SUFFIX;
        return BattleCalculator.selectCasualties(RAID, m_attacker, validAttackingUnitsForThisRoll, m_attackingUnits, m_defender, m_defendingUnits, m_isAmphibious, m_amphibiousLandAttackers, m_battleSite, m_territoryEffects, bridge, text, /* dice */
        null, /* defending */
        false, m_battleID, /* head-less */
        false, 0, allowMultipleHitsPerUnit);
    }
    final CasualtyDetails casualties = BattleCalculator.getAaCasualties(false, validAttackingUnitsForThisRoll, m_attackingUnits, defendingAa, m_defendingUnits, dice, bridge, m_defender, m_attacker, m_battleID, m_battleSite, m_territoryEffects, m_isAmphibious, m_amphibiousLandAttackers);
    final int totalExpectingHits = dice.getHits() > validAttackingUnitsForThisRoll.size() ? validAttackingUnitsForThisRoll.size() : dice.getHits();
    if (casualties.size() != totalExpectingHits) {
        throw new IllegalStateException("Wrong number of casualties, expecting:" + totalExpectingHits + " but got:" + casualties.size());
    }
    return casualties;
}
Also used : CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails)

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