use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class LocalLauncher method launchInNewThread.
@Override
protected void launchInNewThread(final Component parent) {
Exception exceptionLoadingGame = null;
ServerGame game = null;
try {
gameData.doPreGameStartDataModifications(playerListing);
final Messengers messengers = new Messengers(new HeadlessServerMessenger());
final Set<IGamePlayer> gamePlayers = gameData.getGameLoader().createPlayers(playerListing.getLocalPlayerTypes());
game = new ServerGame(gameData, gamePlayers, new HashMap<>(), messengers);
game.setRandomSource(randomSource);
// for debugging, we can use a scripted random source
if (ScriptedRandomSource.useScriptedRandom()) {
game.setRandomSource(new ScriptedRandomSource());
}
gameData.getGameLoader().startGame(game, gamePlayers, headless);
} catch (final MapNotFoundException e) {
exceptionLoadingGame = e;
} catch (final Exception ex) {
ClientLogger.logQuietly("Failed to start game", ex);
exceptionLoadingGame = ex;
} finally {
gameLoadingWindow.doneWait();
}
try {
if (exceptionLoadingGame == null) {
game.startGame();
}
} finally {
// todo(kg), this does not occur on the swing thread, and this notifies setupPanel observers
// having an oddball issue with the zip stream being closed while parsing to load default game. might be caused
// by closing of stream while unloading map resources.
Interruptibles.sleep(100);
gameSelectorModel.loadDefaultGameNewThread();
SwingUtilities.invokeLater(() -> JOptionPane.getFrameForComponent(parent).setVisible(true));
}
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class AirThatCantLandUtilTest method testCanLandNeighborCarrier.
@Test
public void testCanLandNeighborCarrier() {
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 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(1, americans)));
// Get total number of defending units before the battle
final int preCountSz52 = sz52.getUnits().size();
final int preCountAirSz44 = sz44.getUnits().getMatches(Matches.unitIsAir()).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 postCountInt = preCountSz52 + preCountAirSz44;
// Compare the expected count with the actual number of units in landing zone
assertEquals(expectedCountSz52, postCountInt);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class AirThatCantLandUtilTest method testCanLandNeighborLandV2.
@Test
public void testCanLandNeighborLandV2() {
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 sz9 = gameData.getMap().getTerritory("9 Sea Zone");
final Territory eastCanada = gameData.getMap().getTerritory("Eastern Canada");
final Territory sz11 = gameData.getMap().getTerritory("11 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(sz11, subType.create(1, japanese)));
gameData.performChange(ChangeFactory.addUnits(sz9, carrierType.create(1, americans)));
gameData.performChange(ChangeFactory.addUnits(sz9, fighterType.create(1, americans)));
// Get total number of defending units before the battle
final int preCountCanada = eastCanada.getUnits().size();
final int preCountAirSz9 = sz9.getUnits().getMatches(Matches.unitIsAir()).size();
// now move to attack
final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
bridge.setStepName("CombatMove");
moveDelegate.setDelegateBridgeAndPlayer(bridge);
moveDelegate.start();
moveDelegate.move(sz11.getUnits().getUnits(), gameData.getMap().getRoute(sz11, sz9));
moveDelegate.end();
// fight the battle
final BattleDelegate battle = (BattleDelegate) gameData.getDelegateList().getDelegate("battle");
battle.setDelegateBridgeAndPlayer(bridge);
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0 }));
bridge.setRemote(getDummyPlayer());
battle.start();
battle.end();
// Get the total number of units that should be left after the planes retreat
final int expectedCountCanada = eastCanada.getUnits().size();
final int postCountInt = preCountCanada + preCountAirSz9;
// Compare the expected count with the actual number of units in landing zone
assertEquals(expectedCountCanada, postCountInt);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class AirThatCantLandUtilTest method testCanLandNeighborLandWithRetreatedBattleV2.
@Test
public void testCanLandNeighborLandWithRetreatedBattleV2() {
final PlayerID japanese = GameDataTestUtil.japanese(gameData);
final PlayerID americans = GameDataTestUtil.americans(gameData);
final ITestDelegateBridge bridge = getDelegateBridge(japanese);
// Get necessary sea zones and unit types for this test
final Territory sz9 = gameData.getMap().getTerritory("9 Sea Zone");
final Territory eastCanada = gameData.getMap().getTerritory("Eastern Canada");
final Territory sz11 = gameData.getMap().getTerritory("11 Sea Zone");
final UnitType subType = GameDataTestUtil.submarine(gameData);
final UnitType carrierType = GameDataTestUtil.carrier(gameData);
final UnitType fighterType = GameDataTestUtil.fighter(gameData);
final UnitType transportType = GameDataTestUtil.transport(gameData);
final UnitType infantryType = GameDataTestUtil.infantry(gameData);
// Add units for the test
gameData.performChange(ChangeFactory.addUnits(sz11, subType.create(1, japanese)));
gameData.performChange(ChangeFactory.addUnits(sz11, transportType.create(1, japanese)));
gameData.performChange(ChangeFactory.addUnits(sz11, infantryType.create(1, japanese)));
gameData.performChange(ChangeFactory.addUnits(sz9, carrierType.create(1, americans)));
gameData.performChange(ChangeFactory.addUnits(sz9, fighterType.create(2, americans)));
// we need to initialize the original owner
final InitializationDelegate initDel = (InitializationDelegate) gameData.getDelegateList().getDelegate("initDelegate");
initDel.setDelegateBridgeAndPlayer(bridge);
initDel.start();
initDel.end();
// Get total number of defending units before the battle
final int preCountCanada = eastCanada.getUnits().size();
final int preCountAirSz9 = sz9.getUnits().getMatches(Matches.unitIsAir()).size();
// now move to attack
final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
bridge.setStepName("CombatMove");
moveDelegate.setDelegateBridgeAndPlayer(bridge);
moveDelegate.start();
moveDelegate.move(sz11.getUnits().getUnits(), gameData.getMap().getRoute(sz11, sz9));
moveDelegate.move(sz9.getUnits().getUnits(infantryType, 1), gameData.getMap().getRoute(sz9, eastCanada));
moveDelegate.end();
// fight the battle
final BattleDelegate battle = (BattleDelegate) gameData.getDelegateList().getDelegate("battle");
battle.setDelegateBridgeAndPlayer(bridge);
battle.start();
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 0, 0 }));
bridge.setRemote(getDummyPlayer());
fight(battle, sz9, false);
battle.end();
// Get the total number of units that should be left after the planes retreat
final int expectedCountCanada = eastCanada.getUnits().size();
final int postCountInt = preCountCanada + preCountAirSz9;
// Compare the expected count with the actual number of units in landing zone
assertEquals(expectedCountCanada, postCountInt);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class BattleCalculatorTest method testAaCasualtiesLowLuckMixedWithRollingMiss.
@Test
public void testAaCasualtiesLowLuckMixedWithRollingMiss() {
final GameData data = bridge.getData();
makeGameLowLuck(data);
setSelectAaCasualties(data, false);
// 7 bombers and 7 fighters
// 2 extra units, roll once
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());
// one roll, a miss
final ScriptedRandomSource randomSource = new ScriptedRandomSource(new int[] { 2, 0, 0, 0, ScriptedRandomSource.ERROR });
bridge.setRandomSource(randomSource);
final DiceRoll roll = DiceRoll.rollAa(CollectionUtils.getMatches(planes, Matches.unitIsOfTypes(UnitAttachment.get(defendingAa.iterator().next().getType()).getTargetsAa(data))), defendingAa, bridge, territory("Germany", data), true);
// make sure we rolled once
assertEquals(1, randomSource.getTotalRolled());
final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, null, null, null, territory("Germany", data), null, false, null).getKilled();
assertEquals(2, casualties.size());
assertEquals(4, randomSource.getTotalRolled());
// should be 1 fighter and 1 bomber
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
Aggregations