use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProAi method selectAttackSubs.
@Override
public boolean selectAttackSubs(final Territory unitTerritory) {
initializeData();
// Get battle data
final GameData data = getGameData();
final PlayerID player = getPlayerId();
final BattleDelegate delegate = DelegateFinder.battleDelegate(data);
final IBattle battle = delegate.getBattleTracker().getPendingBattle(unitTerritory, false, BattleType.NORMAL);
// If battle is null then don't attack
if (battle == null) {
return false;
}
final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
ProLogger.info(player.getName() + " checking sub attack in " + unitTerritory + ", attackers=" + attackers + ", defenders=" + defenders);
calc.setData(getGameData());
// Calculate battle results
final ProBattleResult result = calc.calculateBattleResults(unitTerritory, attackers, defenders, new HashSet<>());
ProLogger.debug(player.getName() + " sub attack TUVSwing=" + result.getTuvSwing());
return result.getTuvSwing() > 0;
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProBattleUtils method checkForOverwhelmingWin.
public static boolean checkForOverwhelmingWin(final Territory t, final List<Unit> attackingUnits, final List<Unit> defendingUnits) {
final GameData data = ProData.getData();
if (defendingUnits.isEmpty() && !attackingUnits.isEmpty()) {
return true;
}
// Check that defender has at least 1 power
final double power = estimatePower(t, defendingUnits, attackingUnits, false);
if (power == 0 && !attackingUnits.isEmpty()) {
return true;
}
// Determine if enough attack power to win in 1 round
final List<Unit> sortedUnitsList = new ArrayList<>(attackingUnits);
sortedUnitsList.sort(new UnitBattleComparator(false, ProData.unitValueMap, TerritoryEffectHelper.getEffects(t), data, false, false));
Collections.reverse(sortedUnitsList);
final int attackPower = DiceRoll.getTotalPower(DiceRoll.getUnitPowerAndRollsForNormalBattles(sortedUnitsList, defendingUnits, false, false, data, t, TerritoryEffectHelper.getEffects(t), false, null), data);
final List<Unit> defendersWithHitPoints = CollectionUtils.getMatches(defendingUnits, Matches.unitIsInfrastructure().negate());
final int totalDefenderHitPoints = BattleCalculator.getTotalHitpointsLeft(defendersWithHitPoints);
return ((attackPower / data.getDiceSides()) >= totalDefenderHitPoints);
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProBattleUtils method estimatePower.
private static double estimatePower(final Territory t, final List<Unit> myUnits, final List<Unit> enemyUnits, final boolean attacking) {
final GameData data = ProData.getData();
final List<Unit> unitsThatCanFight = CollectionUtils.getMatches(myUnits, Matches.unitCanBeInBattle(attacking, !t.isWater(), 1, false, true, true));
final List<Unit> sortedUnitsList = new ArrayList<>(unitsThatCanFight);
sortedUnitsList.sort(new UnitBattleComparator(!attacking, ProData.unitValueMap, TerritoryEffectHelper.getEffects(t), data, false, false));
Collections.reverse(sortedUnitsList);
final int myPower = DiceRoll.getTotalPower(DiceRoll.getUnitPowerAndRollsForNormalBattles(sortedUnitsList, enemyUnits, !attacking, false, data, t, TerritoryEffectHelper.getEffects(t), false, null), data);
return (myPower * 6.0 / data.getDiceSides());
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProBattleUtils method territoryHasLocalNavalSuperiority.
public static boolean territoryHasLocalNavalSuperiority(final Territory t, final PlayerID player, final Map<Territory, ProPurchaseTerritory> purchaseTerritories, final List<Unit> unitsToPlace) {
final GameData data = ProData.getData();
int landDistance = ProUtils.getClosestEnemyLandTerritoryDistanceOverWater(data, player, t);
if (landDistance <= 0) {
landDistance = 10;
}
final int enemyDistance = Math.max(3, (landDistance + 1));
final Set<Territory> nearbyTerritories = data.getMap().getNeighbors(t, enemyDistance);
final List<Territory> nearbyLandTerritories = CollectionUtils.getMatches(nearbyTerritories, Matches.territoryIsLand());
final Set<Territory> nearbyEnemySeaTerritories = data.getMap().getNeighbors(t, enemyDistance, Matches.territoryIsWater());
nearbyEnemySeaTerritories.add(t);
final int alliedDistance = (enemyDistance + 1) / 2;
final Set<Territory> nearbyAlliedSeaTerritories = data.getMap().getNeighbors(t, alliedDistance, Matches.territoryIsWater());
nearbyAlliedSeaTerritories.add(t);
final List<Unit> enemyUnitsInLandTerritories = new ArrayList<>();
for (final Territory nearbyLandTerritory : nearbyLandTerritories) {
enemyUnitsInLandTerritories.addAll(nearbyLandTerritory.getUnits().getMatches(ProMatches.unitIsEnemyAir(player, data)));
}
final List<Unit> enemyUnitsInSeaTerritories = new ArrayList<>();
for (final Territory nearbySeaTerritory : nearbyEnemySeaTerritories) {
final List<Unit> enemySeaUnits = nearbySeaTerritory.getUnits().getMatches(ProMatches.unitIsEnemyNotLand(player, data));
if (enemySeaUnits.isEmpty()) {
continue;
}
final Route route = data.getMap().getRoute_IgnoreEnd(t, nearbySeaTerritory, Matches.territoryIsWater());
if (route == null) {
continue;
}
if (MoveValidator.validateCanal(route, enemySeaUnits, enemySeaUnits.get(0).getOwner(), data) != null) {
continue;
}
final int routeLength = route.numberOfSteps();
if (routeLength <= enemyDistance) {
enemyUnitsInSeaTerritories.addAll(enemySeaUnits);
}
}
final List<Unit> alliedUnitsInSeaTerritories = new ArrayList<>();
final List<Unit> myUnitsInSeaTerritories = new ArrayList<>();
for (final Territory nearbySeaTerritory : nearbyAlliedSeaTerritories) {
myUnitsInSeaTerritories.addAll(nearbySeaTerritory.getUnits().getMatches(ProMatches.unitIsOwnedNotLand(player)));
myUnitsInSeaTerritories.addAll(ProPurchaseUtils.getPlaceUnits(nearbySeaTerritory, purchaseTerritories));
alliedUnitsInSeaTerritories.addAll(nearbySeaTerritory.getUnits().getMatches(ProMatches.unitIsAlliedNotOwned(player, data)));
}
ProLogger.trace(t + ", enemyDistance=" + enemyDistance + ", alliedDistance=" + alliedDistance + ", enemyAirUnits=" + enemyUnitsInLandTerritories + ", enemySeaUnits=" + enemyUnitsInSeaTerritories + ", mySeaUnits=" + myUnitsInSeaTerritories);
// Find current naval defense strength
final List<Unit> myUnits = new ArrayList<>(myUnitsInSeaTerritories);
myUnits.addAll(unitsToPlace);
myUnits.addAll(alliedUnitsInSeaTerritories);
final List<Unit> enemyAttackers = new ArrayList<>(enemyUnitsInSeaTerritories);
enemyAttackers.addAll(enemyUnitsInLandTerritories);
final double defenseStrengthDifference = estimateStrengthDifference(t, enemyAttackers, myUnits);
ProLogger.trace(t + ", current enemy naval attack strengthDifference=" + defenseStrengthDifference + ", enemySize=" + enemyAttackers.size() + ", alliedSize=" + myUnits.size());
// Find current naval attack strength
double attackStrengthDifference = estimateStrengthDifference(t, myUnits, enemyUnitsInSeaTerritories);
attackStrengthDifference += 0.5 * estimateStrengthDifference(t, alliedUnitsInSeaTerritories, enemyUnitsInSeaTerritories);
ProLogger.trace(t + ", current allied naval attack strengthDifference=" + attackStrengthDifference + ", alliedSize=" + myUnits.size() + ", enemySize=" + enemyUnitsInSeaTerritories.size());
// If I have naval attack/defense superiority then break
return (defenseStrengthDifference < 50 && attackStrengthDifference > 50);
}
use of games.strategy.engine.data.GameData in project triplea by triplea-game.
the class ProBattleUtils method estimateStrength.
public static double estimateStrength(final Territory t, final List<Unit> myUnits, final List<Unit> enemyUnits, final boolean attacking) {
final GameData data = ProData.getData();
List<Unit> unitsThatCanFight = CollectionUtils.getMatches(myUnits, Matches.unitCanBeInBattle(attacking, !t.isWater(), 1, false, true, true));
if (Properties.getTransportCasualtiesRestricted(data)) {
unitsThatCanFight = CollectionUtils.getMatches(unitsThatCanFight, Matches.unitIsTransportButNotCombatTransport().negate());
}
final int myHitPoints = BattleCalculator.getTotalHitpointsLeft(unitsThatCanFight);
final double myPower = estimatePower(t, myUnits, enemyUnits, attacking);
return (2 * myHitPoints) + myPower;
}
Aggregations