use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.
the class MoveValidatorTest method testValidateUnitsCanLoadInHostileSeaZones.
@Test
public void testValidateUnitsCanLoadInHostileSeaZones() throws Exception {
final GameData twwGameData = TestMapGameData.TWW.getGameData();
// Load german unit in sea zone with no enemy ships
final PlayerID germans = GameDataTestUtil.germany(twwGameData);
final Territory northernGermany = territory("Northern Germany", twwGameData);
final Territory sz27 = territory("27 Sea Zone", twwGameData);
final Route r = new Route(northernGermany, sz27);
northernGermany.getUnits().clear();
addTo(northernGermany, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
final List<Unit> transport = sz27.getUnits().getMatches(Matches.unitIsTransport());
MoveValidationResult results = MoveValidator.validateMove(northernGermany.getUnits(), r, germans, transport, new HashMap<>(), false, null, twwGameData);
assertTrue(results.isMoveValid());
// Add USA ship to transport sea zone
final PlayerID usa = GameDataTestUtil.usa(twwGameData);
addTo(sz27, GameDataTestUtil.americanCruiser(twwGameData).create(1, usa));
results = MoveValidator.validateMove(northernGermany.getUnits(), r, germans, transport, new HashMap<>(), false, null, twwGameData);
assertFalse(results.isMoveValid());
// Set 'Units Can Load In Hostile Sea Zones' to true
twwGameData.getProperties().set(Constants.UNITS_CAN_LOAD_IN_HOSTILE_SEA_ZONES, true);
results = MoveValidator.validateMove(northernGermany.getUnits(), r, germans, transport, new HashMap<>(), false, null, twwGameData);
assertTrue(results.isMoveValid());
}
use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.
the class MoveValidatorTest method testValidateMoveForLandTransports.
@Test
public void testValidateMoveForLandTransports() throws Exception {
final GameData twwGameData = TestMapGameData.TWW.getGameData();
// Move truck 2 territories
final PlayerID germans = GameDataTestUtil.germany(twwGameData);
final Territory berlin = territory("Berlin", twwGameData);
final Territory easternGermany = territory("Eastern Germany", twwGameData);
final Territory poland = territory("Poland", twwGameData);
final Route r = new Route(berlin, easternGermany, poland);
berlin.getUnits().clear();
GameDataTestUtil.truck(twwGameData).create(1, germans);
addTo(berlin, GameDataTestUtil.truck(twwGameData).create(1, germans));
MoveValidationResult results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
assertTrue(results.isMoveValid());
// Add an infantry for truck to transport
addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
assertTrue(results.isMoveValid());
// Add an infantry and the truck can't transport both
addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
assertFalse(results.isMoveValid());
// Add a large truck (has capacity for 2 infantry) to transport second infantry
addTo(berlin, GameDataTestUtil.largeTruck(twwGameData).create(1, germans));
results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
assertTrue(results.isMoveValid());
// Add an infantry that the large truck can also transport
addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
assertTrue(results.isMoveValid());
// Add an infantry that can't be transported
addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
assertFalse(results.isMoveValid());
}
use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.
the class SpecialMoveDelegate method move.
@Override
public String move(final Collection<Unit> units, final Route route, final Collection<Unit> transportsThatCanBeLoaded, final Map<Unit, Collection<Unit>> newDependents) {
if (!allowAirborne(player, getData())) {
return "No Airborne Movement Allowed Yet";
}
final GameData data = getData();
// there reason we use this, is because if we are in edit mode, we may have a different unit owner than the current
// player.
final PlayerID player = getUnitsOwner(units);
// here we have our own new validation method....
final MoveValidationResult result = validateMove(units, route, player, data);
final StringBuilder errorMsg = new StringBuilder(100);
final int numProblems = result.getTotalWarningCount() - (result.hasError() ? 0 : 1);
final String numErrorsMsg = numProblems > 0 ? ("; " + numProblems + " " + MyFormatter.pluralize("error", numProblems) + " not shown") : "";
if (result.hasError()) {
return errorMsg.append(result.getError()).append(numErrorsMsg).toString();
}
if (result.hasDisallowedUnits()) {
return errorMsg.append(result.getDisallowedUnitWarning(0)).append(numErrorsMsg).toString();
}
if (result.hasUnresolvedUnits()) {
return errorMsg.append(result.getUnresolvedUnitWarning(0)).append(numErrorsMsg).toString();
}
// allow user to cancel move if aa guns will fire
final AAInMoveUtil aaInMoveUtil = new AAInMoveUtil();
aaInMoveUtil.initialize(bridge);
final Collection<Territory> aaFiringTerritores = aaInMoveUtil.getTerritoriesWhereAaWillFire(route, units);
if (!aaFiringTerritores.isEmpty()) {
if (!getRemotePlayer().confirmMoveInFaceOfAa(aaFiringTerritores)) {
return null;
}
}
// do the move
final UndoableMove currentMove = new UndoableMove(units, route);
// add dependencies (any move that came before this, from this start territory, is a dependency)
for (final UndoableMove otherMove : movesToUndo) {
if (otherMove.getStart().equals(route.getStart())) {
currentMove.addDependency(otherMove);
}
}
// make the units airborne
final CompositeChange airborneChange = new CompositeChange();
for (final Unit u : units) {
airborneChange.add(ChangeFactory.unitPropertyChange(u, true, TripleAUnit.AIRBORNE));
}
currentMove.addChange(airborneChange);
// make the bases start filling up their capacity
final Collection<Unit> basesAtStart = route.getStart().getUnits().getMatches(getAirborneBaseMatch(player, data));
final Change fillLaunchCapacity = getNewAssignmentOfNumberLaunchedChange(units.size(), basesAtStart, player, data);
currentMove.addChange(fillLaunchCapacity);
// start event
final String transcriptText = MyFormatter.unitsToTextNoOwner(units) + " moved from " + route.getStart().getName() + " to " + route.getEnd().getName();
bridge.getHistoryWriter().startEvent(transcriptText, currentMove.getDescriptionObject());
// actually do our special changes
bridge.addChange(airborneChange);
bridge.addChange(fillLaunchCapacity);
tempMovePerformer = new MovePerformer();
tempMovePerformer.initialize(this);
tempMovePerformer.moveUnits(units, route, player, transportsThatCanBeLoaded, newDependents, currentMove);
tempMovePerformer = null;
return null;
}
use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.
the class SpecialMoveDelegate method validateMove.
static MoveValidationResult validateMove(final Collection<Unit> units, final Route route, final PlayerID player, final GameData data) {
final MoveValidationResult result = new MoveValidationResult();
if (route.hasNoSteps()) {
return result;
}
if (MoveValidator.validateFirst(data, units, route, player, result).getError() != null) {
return result;
}
if (MoveValidator.validateFuel(data, units, route, player, result).getError() != null) {
return result;
}
final boolean isEditMode = getEditMode(data);
if (!isEditMode) {
// make sure all units are at least friendly
for (final Unit unit : CollectionUtils.getMatches(units, Matches.unitIsOwnedBy(player).negate())) {
result.addDisallowedUnit("Can only move owned units", unit);
}
}
if (validateAirborneMovements(data, units, route, player, result).getError() != null) {
return result;
}
return result;
}
use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.
the class WW2V3Year41Test method testBomberWithTankOverWaterParatroopers.
@Test
public void testBomberWithTankOverWaterParatroopers() {
final PlayerID germans = germans(gameData);
TechAttachment.get(germans).setParatroopers("true");
final Territory sz5 = territory("5 Sea Zone", gameData);
final Territory germany = territory("Germany", gameData);
final Territory karelia = territory("Karelia S.S.R.", gameData);
addTo(germany, armour(gameData).create(1, germans));
final Route r = new Route(germany, sz5, karelia);
final Collection<Unit> toMove = germany.getUnits().getMatches(Matches.unitCanBlitz());
toMove.addAll(germany.getUnits().getMatches(Matches.unitIsStrategicBomber()));
assertEquals(2, toMove.size());
final MoveValidationResult results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), new HashMap<>(), false, null, gameData);
assertFalse(results.isMoveValid());
}
Aggregations