use of games.strategy.triplea.delegate.MustFightBattle in project triplea by triplea-game.
the class DummyPlayer method retreatQuery.
/**
* The battle calc doesn't actually care if you have available territories to retreat to or not.
* It will always let you retreat to the 'current' territory (the battle territory), even if that is illegal.
* This is because the battle calc does not know where the attackers are actually coming from.
*/
@Override
public Territory retreatQuery(final GUID battleId, final boolean submerge, final Territory battleSite, final Collection<Territory> possibleTerritories, final String message) {
// null = do not retreat
if (possibleTerritories.isEmpty()) {
return null;
}
if (submerge) {
// submerge if all air vs subs
final Predicate<Unit> seaSub = Matches.unitIsSea().and(Matches.unitIsSub());
final Predicate<Unit> planeNotDestroyer = Matches.unitIsAir().and(Matches.unitIsDestroyer().negate());
final List<Unit> ourUnits = getOurUnits();
final List<Unit> enemyUnits = getEnemyUnits();
if (ourUnits == null || enemyUnits == null) {
return null;
}
if (!enemyUnits.isEmpty() && enemyUnits.stream().allMatch(planeNotDestroyer) && !ourUnits.isEmpty() && ourUnits.stream().allMatch(seaSub)) {
return possibleTerritories.iterator().next();
}
return null;
}
final MustFightBattle battle = getBattle();
if (battle == null) {
return null;
}
if (retreatAfterRound > -1 && battle.getBattleRound() >= retreatAfterRound) {
return possibleTerritories.iterator().next();
}
if (!retreatWhenOnlyAirLeft && retreatAfterXUnitsLeft <= -1) {
return null;
}
final Collection<Unit> unitsLeft = isAttacker ? battle.getAttackingUnits() : battle.getDefendingUnits();
final Collection<Unit> airLeft = CollectionUtils.getMatches(unitsLeft, Matches.unitIsAir());
if (retreatWhenOnlyAirLeft) {
// lets say we have a bunch of 3 attack air unit, and a 4 attack non-air unit,
// and we want to retreat when we have all air units left + that 4 attack non-air (cus it gets taken
// casualty
// last)
// then we add the number of air, to the retreat after X left number (which we would set to '1')
int retreatNum = airLeft.size();
if (retreatAfterXUnitsLeft > 0) {
retreatNum += retreatAfterXUnitsLeft;
}
if (retreatNum >= unitsLeft.size()) {
return possibleTerritories.iterator().next();
}
}
if (retreatAfterXUnitsLeft > -1 && retreatAfterXUnitsLeft >= unitsLeft.size()) {
return possibleTerritories.iterator().next();
}
return null;
}
use of games.strategy.triplea.delegate.MustFightBattle in project triplea by triplea-game.
the class OddsCalculator method calculate.
private AggregateResults calculate(final int count) {
isRunning = true;
final long start = System.currentTimeMillis();
final AggregateResults aggregateResults = new AggregateResults(count);
final BattleTracker battleTracker = new BattleTracker();
// CasualtySortingCaching can cause issues if there is more than 1 one battle being calced at the same time (like if
// the AI and a human
// are both using the calc)
// TODO: first, see how much it actually speeds stuff up by, and if it does make a difference then convert it to a
// per-thread, per-calc
// caching
final List<Unit> attackerOrderOfLosses = OrderOfLossesInputPanel.getUnitListByOrderOfLoss(this.attackerOrderOfLosses, attackingUnits, gameData);
final List<Unit> defenderOrderOfLosses = OrderOfLossesInputPanel.getUnitListByOrderOfLoss(this.defenderOrderOfLosses, defendingUnits, gameData);
for (int i = 0; i < count && !cancelled; i++) {
final CompositeChange allChanges = new CompositeChange();
final DummyDelegateBridge bridge1 = new DummyDelegateBridge(attacker, gameData, allChanges, attackerOrderOfLosses, defenderOrderOfLosses, keepOneAttackingLandUnit, retreatAfterRound, retreatAfterXUnitsLeft, retreatWhenOnlyAirLeft);
final GameDelegateBridge bridge = new GameDelegateBridge(bridge1);
final MustFightBattle battle = new MustFightBattle(location, attacker, gameData, battleTracker);
battle.setHeadless(true);
battle.setUnits(defendingUnits, attackingUnits, bombardingUnits, (amphibious ? attackingUnits : new ArrayList<>()), defender, territoryEffects);
bridge1.setBattle(battle);
battle.fight(bridge);
aggregateResults.addResult(new BattleResults(battle, gameData));
// restore the game to its original state
gameData.performChange(allChanges.invert());
battleTracker.clear();
battleTracker.clearBattleRecords();
}
aggregateResults.setTime(System.currentTimeMillis() - start);
isRunning = false;
cancelled = false;
return aggregateResults;
}
Aggregations