use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class MoveDelegateTest method testReloadTransportAfterRetreatAmphibious.
@Test
public void testReloadTransportAfterRetreatAmphibious() {
bridge = super.getDelegateBridge(british);
bridge.setStepName("britishCombatMove");
Route route = new Route();
route.setStart(northSea);
route.add(balticSeaZone);
IntegerMap<UnitType> map = new IntegerMap<>();
map.put(transport, 1);
map.put(infantry, 2);
// Move from the NorthSea to the BalticSea and validate the move
String results = delegate.move(GameDataTestUtil.getUnits(map, route.getStart()), route);
assertValid(results);
// Unload transports into Finland and validate
route = new Route();
route.setStart(balticSeaZone);
route.add(finlandNorway);
map = new IntegerMap<>();
map.put(infantry, 2);
results = delegate.move(GameDataTestUtil.getUnits(map, route.getStart()), route);
assertValid(results);
// Get the attacking sea units that will retreat
final List<Unit> retreatingSeaUnits = new ArrayList<>();
retreatingSeaUnits.addAll(balticSeaZone.getUnits().getMatches(Matches.enemyUnit(germans, gameData)));
// Get the attacking land units that will retreat and their number
final List<Unit> retreatingLandUnits = new ArrayList<>();
retreatingLandUnits.addAll(finlandNorway.getUnits().getMatches(Matches.enemyUnit(germans, gameData)));
final int retreatingLandSizeInt = retreatingLandUnits.size();
// Get the defending land units that and their number
final List<Unit> defendingLandUnits = new ArrayList<>();
defendingLandUnits.addAll(finlandNorway.getUnits().getMatches(Matches.enemyUnit(british, gameData)));
final int defendingLandSizeInt = defendingLandUnits.size();
// Set up the battles and the dependent battles
final IBattle inFinlandNorway = DelegateFinder.battleDelegate(gameData).getBattleTracker().getPendingBattle(finlandNorway, false, null);
final IBattle inBalticSeaZone = DelegateFinder.battleDelegate(gameData).getBattleTracker().getPendingBattle(balticSeaZone, false, null);
assertNotNull(balticSeaZone);
assertNotNull(finlandNorway);
assertEquals(DelegateFinder.battleDelegate(gameData).getBattleTracker().getDependentOn(inFinlandNorway).iterator().next(), inBalticSeaZone);
// Add some defending units in case there aren't any
final List<Unit> defendList = transport.create(1, germans);
final List<Unit> defendSub = submarine.create(1, germans);
defendList.addAll(defendSub);
// fire the defending transport then the submarine (both miss)
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1, 2 }));
// Execute the battle and verify no hits
final DiceRoll roll = DiceRoll.rollDice(defendList, true, germans, bridge, new MockBattle(balticSeaZone), "", TerritoryEffectHelper.getEffects(balticSeaZone), null);
assertEquals(0, roll.getHits());
// Get total number of units in Finland before the retreat
final int preCountInt = finlandNorway.getUnits().size();
// Retreat from the Baltic
((MustFightBattle) inBalticSeaZone).externalRetreat(retreatingSeaUnits, northSea, false, bridge);
// Get the total number of units that should be left
final int postCountInt = preCountInt - retreatingLandSizeInt;
// Compare the number of units in Finland to begin with the number after retreating
assertEquals(defendingLandSizeInt, postCountInt);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class AirThatCantLandUtilTest method testCanLandMultiNeighborCarriers.
@Test
public void testCanLandMultiNeighborCarriers() {
final PlayerID japanese = GameDataTestUtil.japanese(gameData);
final PlayerID americans = GameDataTestUtil.americans(gameData);
final ITestDelegateBridge bridge = getDelegateBridge(japanese);
// we need to initialize the original owner
final InitializationDelegate initDel = (InitializationDelegate) gameData.getDelegateList().getDelegate("initDelegate");
initDel.setDelegateBridgeAndPlayer(bridge);
initDel.start();
initDel.end();
// Get necessary sea zones and unit types for this test
final Territory sz43 = gameData.getMap().getTerritory("43 Sea Zone");
final Territory sz44 = gameData.getMap().getTerritory("44 Sea Zone");
final Territory sz45 = gameData.getMap().getTerritory("45 Sea Zone");
final Territory sz52 = gameData.getMap().getTerritory("52 Sea Zone");
final UnitType subType = GameDataTestUtil.submarine(gameData);
final UnitType carrierType = GameDataTestUtil.carrier(gameData);
final UnitType fighterType = GameDataTestUtil.fighter(gameData);
// Add units for the test
gameData.performChange(ChangeFactory.addUnits(sz45, subType.create(1, japanese)));
gameData.performChange(ChangeFactory.addUnits(sz44, carrierType.create(1, americans)));
gameData.performChange(ChangeFactory.addUnits(sz44, fighterType.create(3, americans)));
gameData.performChange(ChangeFactory.addUnits(sz43, carrierType.create(1, americans)));
// Get total number of defending units before the battle
final int preCountSz52 = sz52.getUnits().size();
final int preCountSz43 = sz43.getUnits().size();
// now move to attack
final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
bridge.setStepName("CombatMove");
moveDelegate.setDelegateBridgeAndPlayer(bridge);
moveDelegate.start();
moveDelegate.move(sz45.getUnits().getUnits(), gameData.getMap().getRoute(sz45, sz44));
moveDelegate.end();
// fight the battle
final BattleDelegate battle = (BattleDelegate) gameData.getDelegateList().getDelegate("battle");
battle.setDelegateBridgeAndPlayer(bridge);
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 0, 0 }));
bridge.setRemote(getDummyPlayer());
battle.start();
battle.end();
// Get the total number of units that should be left after the planes retreat
final int expectedCountSz52 = sz52.getUnits().size();
final int expectedCountSz43 = sz43.getUnits().size();
final int postCountSz52 = preCountSz52 + 1;
final int postCountSz43 = preCountSz43 + 2;
// Compare the expected count with the actual number of units in landing zone
assertEquals(expectedCountSz52, postCountSz52);
assertEquals(expectedCountSz43, postCountSz43);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class BattleCalculatorTest method testAaCasualtiesLowLuck.
@Test
public void testAaCasualtiesLowLuck() {
final GameData data = bridge.getData();
makeGameLowLuck(data);
setSelectAaCasualties(data, false);
final DiceRoll roll = new DiceRoll(new int[] { 0 }, 1, 1, false);
final Collection<Unit> planes = bomber(data).create(5, british(data));
final Collection<Unit> defendingAa = territory("Germany", data).getUnits().getMatches(Matches.unitIsAaForAnything());
final ScriptedRandomSource randomSource = new ScriptedRandomSource(new int[] { 0, ScriptedRandomSource.ERROR });
bridge.setRandomSource(randomSource);
final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, null, null, null, territory("Germany", data), null, false, null).getKilled();
assertEquals(1, casualties.size());
assertEquals(1, randomSource.getTotalRolled());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class BattleCalculatorTest method testAaCasualtiesLowLuckMixedWithChooseAaCasualtiesRoll.
@Test
public void testAaCasualtiesLowLuckMixedWithChooseAaCasualtiesRoll() {
final GameData data = bridge.getData();
makeGameLowLuck(data);
setSelectAaCasualties(data, true);
// 7 bombers and 7 fighters
final Collection<Unit> planes = bomber(data).create(7, british(data));
planes.addAll(fighter(data).create(7, 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);
// only 1 roll, a hit
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 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(3, casualties.size());
// we selected all bombers
assertEquals(3, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
assertEquals(0, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class BattleCalculatorTest method testAaCasualtiesLowLuckDifferentMovementLeft.
@Test
public void testAaCasualtiesLowLuckDifferentMovementLeft() {
final GameData data = bridge.getData();
makeGameLowLuck(data);
setSelectAaCasualties(data, false);
final DiceRoll roll = new DiceRoll(new int[] { 0 }, 1, 1, false);
final List<Unit> planes = bomber(data).create(5, british(data));
final Collection<Unit> defendingAa = territory("Germany", data).getUnits().getMatches(Matches.unitIsAaForAnything());
final ScriptedRandomSource randomSource = new ScriptedRandomSource(new int[] { 0, ScriptedRandomSource.ERROR });
bridge.setRandomSource(randomSource);
TripleAUnit.get(planes.get(0)).setAlreadyMoved(1);
final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, null, null, null, territory("Germany", data), null, false, null).getKilled();
assertEquals(1, casualties.size());
}
Aggregations