use of games.strategy.triplea.delegate.BattleTracker 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;
}
use of games.strategy.triplea.delegate.BattleTracker in project triplea by triplea-game.
the class TriggerAttachment method triggerChangeOwnership.
public static void triggerChangeOwnership(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
final GameData data = bridge.getData();
Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, changeOwnershipMatch());
if (testWhen) {
trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
}
if (testUses) {
trigs = CollectionUtils.getMatches(trigs, availableUses);
}
final BattleTracker bt = DelegateFinder.battleDelegate(data).getBattleTracker();
for (final TriggerAttachment t : trigs) {
if (testChance && !t.testChance(bridge)) {
continue;
}
if (useUses) {
t.use(bridge);
}
for (final String value : t.getChangeOwnership()) {
final String[] s = value.split(":");
final Collection<Territory> territories = new ArrayList<>();
if (s[0].equalsIgnoreCase("all")) {
territories.addAll(data.getMap().getTerritories());
} else {
final Territory territorySet = data.getMap().getTerritory(s[0]);
territories.add(territorySet);
}
// if null, then is must be "any", so then any player
final PlayerID oldOwner = data.getPlayerList().getPlayerId(s[1]);
final PlayerID newOwner = data.getPlayerList().getPlayerId(s[2]);
final boolean captured = getBool(s[3]);
for (final Territory terr : territories) {
final PlayerID currentOwner = terr.getOwner();
if (TerritoryAttachment.get(terr) == null) {
// any territory that has no territory attachment should definitely not be changed
continue;
}
if (oldOwner != null && !oldOwner.equals(currentOwner)) {
continue;
}
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": " + newOwner.getName() + (captured ? " captures territory " : " takes ownership of territory ") + terr.getName());
if (!captured) {
bridge.addChange(ChangeFactory.changeOwner(terr, newOwner));
} else {
bt.takeOver(terr, newOwner, bridge, null, null);
}
}
}
}
}
Aggregations