Search in sources :

Example 11 with MoveValidationResult

use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.

the class MoveValidatorTest method testValidateMoveForRequiresUnitsToMove.

@Test
public void testValidateMoveForRequiresUnitsToMove() throws Exception {
    final GameData twwGameData = TestMapGameData.TWW.getGameData();
    // Move regular units
    final PlayerID germans = GameDataTestUtil.germany(twwGameData);
    final Territory berlin = territory("Berlin", twwGameData);
    final Territory easternGermany = territory("Eastern Germany", twwGameData);
    final Route r = new Route(berlin, easternGermany);
    List<Unit> toMove = berlin.getUnits().getMatches(Matches.unitCanMove());
    MoveValidationResult results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), new HashMap<>(), false, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Add germanTrain to units which fails since it requires germainRail
    addTo(berlin, GameDataTestUtil.germanTrain(twwGameData).create(1, germans));
    toMove = berlin.getUnits().getMatches(Matches.unitCanMove());
    results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), new HashMap<>(), false, null, twwGameData);
    assertFalse(results.isMoveValid());
    // Add germanRail to only destination so it fails
    final Collection<Unit> germanRail = GameDataTestUtil.germanRail(twwGameData).create(1, germans);
    addTo(easternGermany, germanRail);
    results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), new HashMap<>(), false, null, twwGameData);
    assertFalse(results.isMoveValid());
    // Add germanRail to start so move succeeds
    addTo(berlin, GameDataTestUtil.germanRail(twwGameData).create(1, germans));
    results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), new HashMap<>(), false, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Remove germanRail from destination so move fails
    GameDataTestUtil.removeFrom(easternGermany, germanRail);
    results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), new HashMap<>(), false, null, twwGameData);
    assertFalse(results.isMoveValid());
    // Add allied owned germanRail to destination so move succeeds
    final PlayerID japan = GameDataTestUtil.japan(twwGameData);
    addTo(easternGermany, GameDataTestUtil.germanRail(twwGameData).create(1, japan));
    results = MoveValidator.validateMove(toMove, r, germans, Collections.emptyList(), 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 12 with MoveValidationResult

use of games.strategy.triplea.delegate.dataObjects.MoveValidationResult in project triplea by triplea-game.

the class ProNonCombatMoveAi method moveInfraUnits.

private Map<Territory, ProTerritory> moveInfraUnits(Map<Territory, ProTerritory> factoryMoveMap, final Map<Unit, Set<Territory>> infraUnitMoveMap) {
    ProLogger.info("Determine where to move infra units");
    final Map<Territory, ProTerritory> moveMap = territoryManager.getDefendOptions().getTerritoryMap();
    // Move factory units
    if (factoryMoveMap == null) {
        ProLogger.debug("Creating factory move map");
        // Determine and store where to move factories
        factoryMoveMap = new HashMap<>();
        for (final Iterator<Unit> it = infraUnitMoveMap.keySet().iterator(); it.hasNext(); ) {
            final Unit u = it.next();
            // Only check factory units
            if (Matches.unitCanProduceUnits().test(u)) {
                Territory maxValueTerritory = null;
                double maxValue = 0;
                for (final Territory t : infraUnitMoveMap.get(u)) {
                    if (!moveMap.get(t).isCanHold()) {
                        continue;
                    }
                    // Check if territory is safe after all current moves
                    if (moveMap.get(t).getBattleResult() == null) {
                        final List<Unit> defendingUnits = moveMap.get(t).getAllDefenders();
                        moveMap.get(t).setBattleResult(calc.calculateBattleResults(t, moveMap.get(t).getMaxEnemyUnits(), defendingUnits, moveMap.get(t).getMaxEnemyBombardUnits()));
                    }
                    final ProBattleResult result = moveMap.get(t).getBattleResult();
                    if (result.getWinPercentage() >= ProData.minWinPercentage || result.getTuvSwing() > 0) {
                        moveMap.get(t).setCanHold(false);
                        continue;
                    }
                    // Find value by checking if territory is not conquered and doesn't already have a factory
                    final List<Unit> units = new ArrayList<>(moveMap.get(t).getCantMoveUnits());
                    units.addAll(moveMap.get(t).getUnits());
                    final int production = TerritoryAttachment.get(t).getProduction();
                    double value = 0.1 * moveMap.get(t).getValue();
                    if (ProMatches.territoryIsNotConqueredOwnedLand(player, data).test(t) && units.stream().noneMatch(Matches.unitCanProduceUnitsAndIsInfrastructure())) {
                        value = moveMap.get(t).getValue() * production + 0.01 * production;
                    }
                    ProLogger.trace(t.getName() + " has value=" + value + ", strategicValue=" + moveMap.get(t).getValue() + ", production=" + production);
                    if (value > maxValue) {
                        maxValue = value;
                        maxValueTerritory = t;
                    }
                }
                if (maxValueTerritory != null) {
                    ProLogger.debug(u.getType().getName() + " moved to " + maxValueTerritory.getName() + " with value=" + maxValue);
                    moveMap.get(maxValueTerritory).addUnit(u);
                    if (factoryMoveMap.containsKey(maxValueTerritory)) {
                        factoryMoveMap.get(maxValueTerritory).addUnit(u);
                    } else {
                        final ProTerritory patd = new ProTerritory(maxValueTerritory);
                        patd.addUnit(u);
                        factoryMoveMap.put(maxValueTerritory, patd);
                    }
                    it.remove();
                }
            }
        }
    } else {
        ProLogger.debug("Using stored factory move map");
        // Transfer stored factory moves to move map
        for (final Territory t : factoryMoveMap.keySet()) {
            moveMap.get(t).addUnits(factoryMoveMap.get(t).getUnits());
        }
    }
    ProLogger.debug("Move infra AA units");
    // Move AA units
    for (final Iterator<Unit> it = infraUnitMoveMap.keySet().iterator(); it.hasNext(); ) {
        final Unit u = it.next();
        final Territory currentTerritory = unitTerritoryMap.get(u);
        // Only check AA units whose territory can't be held and don't have factories
        if (Matches.unitIsAaForAnything().test(u) && !moveMap.get(currentTerritory).isCanHold() && !ProMatches.territoryHasInfraFactoryAndIsLand().test(currentTerritory)) {
            Territory maxValueTerritory = null;
            double maxValue = 0;
            for (final Territory t : infraUnitMoveMap.get(u)) {
                if (!moveMap.get(t).isCanHold()) {
                    continue;
                }
                // Consider max stack of 1 AA in classic
                final Route r = data.getMap().getRoute_IgnoreEnd(currentTerritory, t, ProMatches.territoryCanMoveLandUnitsThrough(player, data, u, currentTerritory, false, new ArrayList<>()));
                final MoveValidationResult mvr = MoveValidator.validateMove(Collections.singletonList(u), r, player, new ArrayList<>(), new HashMap<>(), true, null, data);
                if (!mvr.isMoveValid()) {
                    continue;
                }
                // Find value and try to move to territory that doesn't already have AA
                final List<Unit> units = new ArrayList<>(moveMap.get(t).getCantMoveUnits());
                units.addAll(moveMap.get(t).getUnits());
                final boolean hasAa = units.stream().anyMatch(Matches.unitIsAaForAnything());
                double value = moveMap.get(t).getValue();
                if (hasAa) {
                    value *= 0.01;
                }
                ProLogger.trace(t.getName() + " has value=" + value);
                if (value > maxValue) {
                    maxValue = value;
                    maxValueTerritory = t;
                }
            }
            if (maxValueTerritory != null) {
                ProLogger.debug(u.getType().getName() + " moved to " + maxValueTerritory.getName() + " with value=" + maxValue);
                moveMap.get(maxValueTerritory).addUnit(u);
                it.remove();
            }
        }
    }
    return factoryMoveMap;
}
Also used : ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) Route(games.strategy.engine.data.Route)

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