Search in sources :

Example 6 with MoveValidationResult

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());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) HashMap(java.util.HashMap) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 7 with MoveValidationResult

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());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) HashMap(java.util.HashMap) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 8 with MoveValidationResult

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;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 9 with MoveValidationResult

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;
}
Also used : TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult)

Example 10 with MoveValidationResult

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());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Aggregations

MoveValidationResult (games.strategy.triplea.delegate.dataObjects.MoveValidationResult)12 Unit (games.strategy.engine.data.Unit)11 TripleAUnit (games.strategy.triplea.TripleAUnit)11 PlayerID (games.strategy.engine.data.PlayerID)9 Territory (games.strategy.engine.data.Territory)9 Route (games.strategy.engine.data.Route)7 Test (org.junit.jupiter.api.Test)6 GameData (games.strategy.engine.data.GameData)5 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Change (games.strategy.engine.data.Change)1 CompositeChange (games.strategy.engine.data.CompositeChange)1 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)1 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1