Search in sources :

Example 1 with BattleType

use of games.strategy.triplea.delegate.IBattle.BattleType in project triplea by triplea-game.

the class ProSimulateTurnUtils method simulateBattles.

public static void simulateBattles(final GameData data, final PlayerID player, final IDelegateBridge delegateBridge, final ProOddsCalculator calc) {
    ProLogger.info("Starting battle simulation phase");
    final BattleDelegate battleDelegate = DelegateFinder.battleDelegate(data);
    final Map<BattleType, Collection<Territory>> battleTerritories = battleDelegate.getBattles().getBattles();
    for (final Entry<BattleType, Collection<Territory>> entry : battleTerritories.entrySet()) {
        for (final Territory t : entry.getValue()) {
            final IBattle battle = battleDelegate.getBattleTracker().getPendingBattle(t, entry.getKey().isBombingRun(), entry.getKey());
            final List<Unit> attackers = (List<Unit>) battle.getAttackingUnits();
            attackers.retainAll(t.getUnits().getUnits());
            final List<Unit> defenders = (List<Unit>) battle.getDefendingUnits();
            defenders.retainAll(t.getUnits().getUnits());
            final Set<Unit> bombardingUnits = new HashSet<>(battle.getBombardingUnits());
            ProLogger.debug("---" + t);
            ProLogger.debug("attackers=" + attackers);
            ProLogger.debug("defenders=" + defenders);
            ProLogger.debug("bombardingUnits=" + bombardingUnits);
            final ProBattleResult result = calc.callBattleCalculator(t, attackers, defenders, bombardingUnits);
            final List<Unit> remainingUnits = result.getAverageAttackersRemaining();
            ProLogger.debug("remainingUnits=" + remainingUnits);
            // Make updates to data
            final List<Unit> attackersToRemove = new ArrayList<>(attackers);
            attackersToRemove.removeAll(remainingUnits);
            final List<Unit> defendersToRemove = CollectionUtils.getMatches(defenders, Matches.unitIsInfrastructure().negate());
            final List<Unit> infrastructureToChangeOwner = CollectionUtils.getMatches(defenders, Matches.unitIsInfrastructure());
            ProLogger.debug("attackersToRemove=" + attackersToRemove);
            ProLogger.debug("defendersToRemove=" + defendersToRemove);
            ProLogger.debug("infrastructureToChangeOwner=" + infrastructureToChangeOwner);
            final Change attackerskilledChange = ChangeFactory.removeUnits(t, attackersToRemove);
            delegateBridge.addChange(attackerskilledChange);
            final Change defenderskilledChange = ChangeFactory.removeUnits(t, defendersToRemove);
            delegateBridge.addChange(defenderskilledChange);
            BattleTracker.captureOrDestroyUnits(t, player, player, delegateBridge, null);
            if (!checkIfCapturedTerritoryIsAlliedCapital(t, data, player, delegateBridge)) {
                delegateBridge.addChange(ChangeFactory.changeOwner(t, player));
            }
            battleDelegate.getBattleTracker().getConquered().add(t);
            battleDelegate.getBattleTracker().removeBattle(battle);
            final Territory updatedTerritory = data.getMap().getTerritory(t.getName());
            ProLogger.debug("after changes owner=" + updatedTerritory.getOwner() + ", units=" + updatedTerritory.getUnits().getUnits());
        }
    }
}
Also used : BattleDelegate(games.strategy.triplea.delegate.BattleDelegate) Territory(games.strategy.engine.data.Territory) ProTerritory(games.strategy.triplea.ai.pro.data.ProTerritory) ArrayList(java.util.ArrayList) BattleType(games.strategy.triplea.delegate.IBattle.BattleType) ProBattleResult(games.strategy.triplea.ai.pro.data.ProBattleResult) Change(games.strategy.engine.data.Change) Unit(games.strategy.engine.data.Unit) IBattle(games.strategy.triplea.delegate.IBattle) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with BattleType

use of games.strategy.triplea.delegate.IBattle.BattleType in project triplea by triplea-game.

the class BattlePanel method display.

@Override
public void display(final PlayerID id) {
    super.display(id);
    SwingUtilities.invokeLater(() -> {
        removeAll();
        actionLabel.setText(id.getName() + " battle");
        setLayout(new BorderLayout());
        final JPanel panel = JPanelBuilder.builder().gridLayout(0, 1).add(actionLabel).build();
        for (final Entry<BattleType, Collection<Territory>> entry : battles.entrySet()) {
            for (final Territory t : entry.getValue()) {
                addBattleActions(panel, t, entry.getKey().isBombingRun(), entry.getKey());
            }
        }
        add(panel, BorderLayout.NORTH);
        SwingUtilities.invokeLater(refresh);
    });
}
Also used : JPanel(javax.swing.JPanel) Territory(games.strategy.engine.data.Territory) BorderLayout(java.awt.BorderLayout) BattleType(games.strategy.triplea.delegate.IBattle.BattleType) Collection(java.util.Collection)

Aggregations

Territory (games.strategy.engine.data.Territory)2 BattleType (games.strategy.triplea.delegate.IBattle.BattleType)2 Collection (java.util.Collection)2 Change (games.strategy.engine.data.Change)1 Unit (games.strategy.engine.data.Unit)1 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)1 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)1 BattleDelegate (games.strategy.triplea.delegate.BattleDelegate)1 IBattle (games.strategy.triplea.delegate.IBattle)1 BorderLayout (java.awt.BorderLayout)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 JPanel (javax.swing.JPanel)1