use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class PlayerAttachment method setPlacementLimit.
private void setPlacementLimit(final String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length < 3) {
throw new GameParseException("placementLimit must have 3 parts: count, type, unit list" + thisErrorMsg());
}
final int max = getInt(s[0]);
if (max < 0) {
throw new GameParseException("placementLimit count must have a positive number" + thisErrorMsg());
}
if (!(s[1].equals("owned") || s[1].equals("allied") || s[1].equals("total"))) {
throw new GameParseException("placementLimit type must be: owned, allied, or total" + thisErrorMsg());
}
final HashSet<UnitType> types = new HashSet<>();
if (s[2].equalsIgnoreCase("all")) {
types.addAll(getData().getUnitTypeList().getAllUnitTypes());
} else {
for (int i = 2; i < s.length; i++) {
final UnitType ut = getData().getUnitTypeList().getUnitType(s[i]);
if (ut == null) {
throw new GameParseException("No unit called: " + s[i] + thisErrorMsg());
}
types.add(ut);
}
}
m_placementLimit.add(Triple.of(max, s[1], types));
}
use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class PlayerAttachment method setMovementLimit.
private void setMovementLimit(final String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length < 3) {
throw new GameParseException("movementLimit must have 3 parts: count, type, unit list" + thisErrorMsg());
}
final int max = getInt(s[0]);
if (max < 0) {
throw new GameParseException("movementLimit count must have a positive number" + thisErrorMsg());
}
if (!(s[1].equals("owned") || s[1].equals("allied") || s[1].equals("total"))) {
throw new GameParseException("movementLimit type must be: owned, allied, or total" + thisErrorMsg());
}
final HashSet<UnitType> types = new HashSet<>();
if (s[2].equalsIgnoreCase("all")) {
types.addAll(getData().getUnitTypeList().getAllUnitTypes());
} else {
for (int i = 2; i < s.length; i++) {
final UnitType ut = getData().getUnitTypeList().getUnitType(s[i]);
if (ut == null) {
throw new GameParseException("No unit called: " + s[i] + thisErrorMsg());
}
types.add(ut);
}
}
m_movementLimit.add(Triple.of(max, s[1], types));
}
use of games.strategy.engine.data.UnitType in project triplea by triplea-game.
the class RulesAttachment method setUnitPresence.
private void setUnitPresence(String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length <= 1) {
throw new GameParseException("unitPresence must have at least 2 fields. Format value=unit1 count=number, or " + "value=unit1:unit2:unit3 count=number" + thisErrorMsg());
}
final int n = getInt(s[0]);
if (n < 0) {
throw new GameParseException("unitPresence must be a positive integer" + thisErrorMsg());
}
for (int i = 1; i < s.length; i++) {
final String unitTypeToProduce = s[i];
// validate that this unit exists in the xml
final UnitType ut = getData().getUnitTypeList().getUnitType(unitTypeToProduce);
if (ut == null && !(unitTypeToProduce.equals("any") || unitTypeToProduce.equals("ANY"))) {
throw new GameParseException("No unit called: " + unitTypeToProduce + thisErrorMsg());
}
}
value = value.replaceFirst(s[0] + ":", "");
m_unitPresence.put(value, n);
}
use of games.strategy.engine.data.UnitType 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.engine.data.UnitType 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);
}
Aggregations