use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProMoveUtils method doMove.
public static void doMove(final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final List<Collection<Unit>> transportsToLoad, final IMoveDelegate moveDel) {
final GameData data = ProData.getData();
// Group non-amphib units of the same type moving on the same route
if (transportsToLoad == null) {
for (int i = 0; i < moveRoutes.size(); i++) {
final Route r = moveRoutes.get(i);
for (int j = i + 1; j < moveRoutes.size(); j++) {
final Route r2 = moveRoutes.get(j);
if (r.equals(r2)) {
moveUnits.get(j).addAll(moveUnits.get(i));
moveUnits.remove(i);
moveRoutes.remove(i);
i--;
break;
}
}
}
}
// Move units
for (int i = 0; i < moveRoutes.size(); i++) {
if (!ProData.isSimulation) {
ProUtils.pause();
}
if (moveRoutes.get(i) == null || moveRoutes.get(i).getEnd() == null || moveRoutes.get(i).getStart() == null) {
ProLogger.warn(data.getSequence().getRound() + "-" + data.getSequence().getStep().getName() + ": route not valid " + moveRoutes.get(i) + " units: " + moveUnits.get(i));
continue;
}
final String result;
if (transportsToLoad == null || transportsToLoad.get(i) == null) {
result = moveDel.move(moveUnits.get(i), moveRoutes.get(i));
} else {
result = moveDel.move(moveUnits.get(i), moveRoutes.get(i), transportsToLoad.get(i));
}
if (result != null) {
ProLogger.warn(data.getSequence().getRound() + "-" + data.getSequence().getStep().getName() + ": could not move " + moveUnits.get(i) + " over " + moveRoutes.get(i) + " because: " + result);
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProMoveUtils method calculateMoveRoutes.
public static void calculateMoveRoutes(final PlayerID player, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final Map<Territory, ProTerritory> attackMap, final boolean isCombatMove) {
final GameData data = ProData.getData();
// Find all amphib units
final Set<Unit> amphibUnits = attackMap.values().stream().map(ProTerritory::getAmphibAttackMap).map(Map::entrySet).flatMap(Collection::stream).flatMap(e -> Stream.concat(Stream.of(e.getKey()), e.getValue().stream())).collect(Collectors.toSet());
// Loop through all territories to attack
for (final Territory t : attackMap.keySet()) {
// Loop through each unit that is attacking the current territory
for (final Unit u : attackMap.get(t).getUnits()) {
// Skip amphib units
if (amphibUnits.contains(u)) {
continue;
}
// Skip if unit is already in move to territory
final Territory startTerritory = ProData.unitTerritoryMap.get(u);
if (startTerritory == null || startTerritory.equals(t)) {
continue;
}
// Add unit to move list
final List<Unit> unitList = new ArrayList<>();
unitList.add(u);
moveUnits.add(unitList);
// If carrier has dependent allied fighters then move them too
if (Matches.unitIsCarrier().test(u)) {
final Map<Unit, Collection<Unit>> carrierMustMoveWith = MoveValidator.carrierMustMoveWith(startTerritory.getUnits().getUnits(), startTerritory, data, player);
if (carrierMustMoveWith.containsKey(u)) {
unitList.addAll(carrierMustMoveWith.get(u));
}
}
// Determine route and add to move list
Route route = null;
if (unitList.stream().anyMatch(Matches.unitIsSea())) {
// Sea unit (including carriers with planes)
route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveSeaUnitsThrough(player, data, isCombatMove));
} else if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsLand())) {
// Land unit
route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveLandUnitsThrough(player, data, u, startTerritory, isCombatMove, new ArrayList<>()));
} else if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsAir())) {
// Air unit
route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, isCombatMove));
}
if (route == null) {
ProLogger.warn(data.getSequence().getRound() + "-" + data.getSequence().getStep().getName() + ": route is null " + startTerritory + " to " + t + ", units=" + unitList);
}
moveRoutes.add(route);
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProMoveUtils method calculateBombingRoutes.
public static void calculateBombingRoutes(final PlayerID player, final List<Collection<Unit>> moveUnits, final List<Route> moveRoutes, final Map<Territory, ProTerritory> attackMap) {
final GameData data = ProData.getData();
// Loop through all territories to attack
for (final Territory t : attackMap.keySet()) {
// Loop through each unit that is attacking the current territory
for (final Unit u : attackMap.get(t).getBombers()) {
// Skip if unit is already in move to territory
final Territory startTerritory = ProData.unitTerritoryMap.get(u);
if (startTerritory == null || startTerritory.equals(t)) {
continue;
}
// Add unit to move list
final List<Unit> unitList = new ArrayList<>();
unitList.add(u);
moveUnits.add(unitList);
// Determine route and add to move list
Route route = null;
if (!unitList.isEmpty() && unitList.stream().allMatch(Matches.unitIsAir())) {
route = data.getMap().getRoute_IgnoreEnd(startTerritory, t, ProMatches.territoryCanMoveAirUnitsAndNoAa(player, data, true));
}
moveRoutes.add(route);
}
}
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProOddsCalculator method callBattleCalculator.
public ProBattleResult callBattleCalculator(final Territory t, final List<Unit> attackingUnits, final List<Unit> defendingUnits, final Set<Unit> bombardingUnits, final boolean retreatWhenOnlyAirLeft) {
final GameData data = ProData.getData();
if (isCanceled || attackingUnits.isEmpty() || defendingUnits.isEmpty()) {
return new ProBattleResult();
}
final int minArmySize = Math.min(attackingUnits.size(), defendingUnits.size());
final int runCount = Math.max(16, 100 - minArmySize);
final PlayerID attacker = attackingUnits.get(0).getOwner();
final PlayerID defender = defendingUnits.get(0).getOwner();
if (retreatWhenOnlyAirLeft) {
calc.setRetreatWhenOnlyAirLeft(true);
}
final AggregateResults results = calc.setCalculateDataAndCalculate(attacker, defender, t, attackingUnits, defendingUnits, new ArrayList<>(bombardingUnits), TerritoryEffectHelper.getEffects(t), runCount);
if (retreatWhenOnlyAirLeft) {
calc.setRetreatWhenOnlyAirLeft(false);
}
// Find battle result statistics
final double winPercentage = results.getAttackerWinPercent() * 100;
final List<Unit> averageAttackersRemaining = results.getAverageAttackingUnitsRemaining();
final List<Unit> averageDefendersRemaining = results.getAverageDefendingUnitsRemaining();
final List<Unit> mainCombatAttackers = CollectionUtils.getMatches(attackingUnits, Matches.unitCanBeInBattle(true, !t.isWater(), 1, false, true, true));
final List<Unit> mainCombatDefenders = CollectionUtils.getMatches(defendingUnits, Matches.unitCanBeInBattle(false, !t.isWater(), 1, false, true, true));
double tuvSwing = results.getAverageTuvSwing(attacker, mainCombatAttackers, defender, mainCombatDefenders, data);
if (Matches.territoryIsNeutralButNotWater().test(t)) {
// Set TUV swing for neutrals
final double attackingUnitValue = TuvUtils.getTuv(mainCombatAttackers, ProData.unitValueMap);
final double remainingUnitValue = results.getAverageTuvOfUnitsLeftOver(ProData.unitValueMap, ProData.unitValueMap).getFirst();
tuvSwing = remainingUnitValue - attackingUnitValue;
}
final List<Unit> defendingTransportedUnits = CollectionUtils.getMatches(defendingUnits, Matches.unitIsBeingTransported());
if (t.isWater() && !defendingTransportedUnits.isEmpty()) {
// Add TUV swing for transported units
final double transportedUnitValue = TuvUtils.getTuv(defendingTransportedUnits, ProData.unitValueMap);
tuvSwing += transportedUnitValue * winPercentage / 100;
}
// Create battle result object
final List<Territory> territoryList = new ArrayList<>();
territoryList.add(t);
return (!territoryList.isEmpty() && territoryList.stream().allMatch(Matches.territoryIsLand())) ? new ProBattleResult(winPercentage, tuvSwing, averageAttackersRemaining.stream().anyMatch(Matches.unitIsLand()), averageAttackersRemaining, averageDefendersRemaining, results.getAverageBattleRoundsFought()) : new ProBattleResult(winPercentage, tuvSwing, !averageAttackersRemaining.isEmpty(), averageAttackersRemaining, averageDefendersRemaining, results.getAverageBattleRoundsFought());
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProPurchaseUtils method canUnitsBePlaced.
public static boolean canUnitsBePlaced(final List<Unit> units, final PlayerID player, final Territory t, final boolean isBid) {
final GameData data = ProData.getData();
AbstractPlaceDelegate placeDelegate = (AbstractPlaceDelegate) data.getDelegateList().getDelegate("place");
if (isBid) {
placeDelegate = (AbstractPlaceDelegate) data.getDelegateList().getDelegate("placeBid");
}
final IDelegateBridge bridge = new ProDummyDelegateBridge(ProData.getProAi(), player, data);
placeDelegate.setDelegateBridgeAndPlayer(bridge);
final String s = placeDelegate.canUnitsBePlaced(t, units, player);
return s == null;
}
Aggregations