Search in sources :

Example 1 with BattleTracker

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;
}
Also used : GameDelegateBridge(games.strategy.triplea.delegate.GameDelegateBridge) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) MustFightBattle(games.strategy.triplea.delegate.MustFightBattle) BattleTracker(games.strategy.triplea.delegate.BattleTracker) DummyDelegateBridge(games.strategy.triplea.odds.calculator.DummyDelegateBridge)

Example 2 with BattleTracker

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);
                }
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) ArrayList(java.util.ArrayList) BattleTracker(games.strategy.triplea.delegate.BattleTracker)

Aggregations

BattleTracker (games.strategy.triplea.delegate.BattleTracker)2 CompositeChange (games.strategy.engine.data.CompositeChange)1 GameData (games.strategy.engine.data.GameData)1 PlayerID (games.strategy.engine.data.PlayerID)1 Territory (games.strategy.engine.data.Territory)1 Unit (games.strategy.engine.data.Unit)1 GameDelegateBridge (games.strategy.triplea.delegate.GameDelegateBridge)1 MustFightBattle (games.strategy.triplea.delegate.MustFightBattle)1 DummyDelegateBridge (games.strategy.triplea.odds.calculator.DummyDelegateBridge)1 ArrayList (java.util.ArrayList)1