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());
}
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;
}
Aggregations