Search in sources :

Example 61 with Change

use of games.strategy.engine.data.Change in project triplea by triplea-game.

the class AbstractPlaceDelegate method canWeConsumeUnits.

protected boolean canWeConsumeUnits(final Collection<Unit> units, final Territory to, final boolean actuallyDoIt, final CompositeChange change) {
    boolean weCanConsume = true;
    final Collection<Unit> unitsAtStartOfTurnInTo = unitsAtStartOfStepInTerritory(to);
    final Collection<Unit> removedUnits = new ArrayList<>();
    final Collection<Unit> unitsWhichConsume = CollectionUtils.getMatches(units, Matches.unitConsumesUnitsOnCreation());
    for (final Unit unit : unitsWhichConsume) {
        if (Matches.unitWhichConsumesUnitsHasRequiredUnits(unitsAtStartOfTurnInTo).negate().test(unit)) {
            weCanConsume = false;
        }
        if (!weCanConsume) {
            break;
        }
        // remove units which are now consumed, then test the rest of the consuming units on the diminishing pile of units
        // which were in the
        // territory at start of turn
        final UnitAttachment ua = UnitAttachment.get(unit.getType());
        final IntegerMap<UnitType> requiredUnitsMap = ua.getConsumesUnits();
        final Collection<UnitType> requiredUnits = requiredUnitsMap.keySet();
        for (final UnitType ut : requiredUnits) {
            final int requiredNumber = requiredUnitsMap.getInt(ut);
            final Predicate<Unit> unitIsOwnedByAndOfTypeAndNotDamaged = Matches.unitIsOwnedBy(unit.getOwner()).and(Matches.unitIsOfType(ut)).and(Matches.unitHasNotTakenAnyBombingUnitDamage()).and(Matches.unitHasNotTakenAnyDamage()).and(Matches.unitIsNotDisabled());
            final Collection<Unit> unitsBeingRemoved = CollectionUtils.getNMatches(unitsAtStartOfTurnInTo, requiredNumber, unitIsOwnedByAndOfTypeAndNotDamaged);
            unitsAtStartOfTurnInTo.removeAll(unitsBeingRemoved);
            // if we should actually do it, not just test, then add to bridge
            if (actuallyDoIt && change != null) {
                final Change remove = ChangeFactory.removeUnits(to, unitsBeingRemoved);
                change.add(remove);
                removedUnits.addAll(unitsBeingRemoved);
            }
        }
    }
    if (weCanConsume && actuallyDoIt && change != null && !change.isEmpty()) {
        bridge.getHistoryWriter().startEvent("Units in " + to.getName() + " being upgraded or consumed: " + MyFormatter.unitsToTextNoOwner(removedUnits), removedUnits);
    }
    return weCanConsume;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 62 with Change

use of games.strategy.engine.data.Change in project triplea by triplea-game.

the class AbstractPlaceDelegate method doAfterEnd.

protected void doAfterEnd() {
    final PlayerID player = bridge.getPlayerId();
    // clear all units not placed
    final Collection<Unit> units = player.getUnits().getUnits();
    final GameData data = getData();
    if (!Properties.getUnplacedUnitsLive(data) && !units.isEmpty()) {
        bridge.getHistoryWriter().startEvent(MyFormatter.unitsToTextNoOwner(units) + " were produced but were not placed", units);
        final Change change = ChangeFactory.removeUnits(player, units);
        bridge.addChange(change);
    }
    // reset ourselves for next turn
    produced = new HashMap<>();
    placements.clear();
    if (GameStepPropertiesHelper.isRemoveAirThatCanNotLand(data)) {
        removeAirThatCantLand();
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 63 with Change

use of games.strategy.engine.data.Change in project triplea by triplea-game.

the class History method getDelta.

public Change getDelta(final HistoryNode start, final HistoryNode end) {
    assertCorrectThread();
    final int firstChange = getLastChange(start);
    final int lastChange = getLastChange(end);
    if (firstChange == lastChange) {
        return null;
    }
    final List<Change> deltaChanges = changes.subList(Math.min(firstChange, lastChange), Math.max(firstChange, lastChange));
    final Change compositeChange = new CompositeChange(deltaChanges);
    return (lastChange >= firstChange) ? compositeChange : compositeChange.invert();
}
Also used : Change(games.strategy.engine.data.Change) CompositeChange(games.strategy.engine.data.CompositeChange) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 64 with Change

use of games.strategy.engine.data.Change in project triplea by triplea-game.

the class History method gotoNode.

public synchronized void gotoNode(final HistoryNode node) {
    assertCorrectThread();
    getGameData().acquireWriteLock();
    try {
        if (currentNode == null) {
            currentNode = getLastNode();
        }
        final Change dataChange = getDelta(currentNode, node);
        currentNode = node;
        if (dataChange != null) {
            gameData.performChange(dataChange);
        }
    } finally {
        getGameData().releaseWriteLock();
    }
}
Also used : Change(games.strategy.engine.data.Change) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 65 with Change

use of games.strategy.engine.data.Change in project triplea by triplea-game.

the class RevisedTest method testMoveSubAwayFromSubmergedSubsInBattleZone.

@Test
public void testMoveSubAwayFromSubmergedSubsInBattleZone() {
    final Territory sz45 = gameData.getMap().getTerritory("45 Sea Zone");
    final Territory sz50 = gameData.getMap().getTerritory("50 Sea Zone");
    final PlayerID british = GameDataTestUtil.british(gameData);
    final PlayerID japanese = GameDataTestUtil.japanese(gameData);
    // put 1 british sub in sz 45, this simulates a submerged enemy sub
    final UnitType sub = GameDataTestUtil.submarine(gameData);
    final Change c = ChangeFactory.addUnits(sz45, sub.create(1, british));
    gameData.performChange(c);
    // new move delegate
    final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
    final ITestDelegateBridge bridge = getDelegateBridge(japanese);
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    // move a fighter into the sea zone, this will cause a battle
    final Route sz50To45 = new Route();
    sz50To45.setStart(sz50);
    sz50To45.add(sz45);
    String error = moveDelegate.move(sz50.getUnits().getMatches(Matches.unitIsAir()), sz50To45);
    assertNull(error);
    assertEquals(1, AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattleSites(false).size());
    // we should be able to move the sub out of the sz
    final Route sz45To50 = new Route();
    sz45To50.setStart(sz45);
    sz45To50.add(sz50);
    final List<Unit> japSub = sz45.getUnits().getMatches(Matches.unitIsSub().and(Matches.unitIsOwnedBy(japanese)));
    error = moveDelegate.move(japSub, sz45To50);
    // make sure no error
    assertNull(error);
    // make sure the battle is still there
    assertEquals(1, AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattleSites(false).size());
    // we should be able to undo the move of the sub
    error = moveDelegate.undoMove(1);
    assertNull(error);
    // undo the move of the fighter, should be no battles now
    error = moveDelegate.undoMove(0);
    assertNull(error);
    assertEquals(0, AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattleSites(false).size());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) Change(games.strategy.engine.data.Change) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Aggregations

Change (games.strategy.engine.data.Change)70 CompositeChange (games.strategy.engine.data.CompositeChange)44 Unit (games.strategy.engine.data.Unit)40 TripleAUnit (games.strategy.triplea.TripleAUnit)29 PlayerID (games.strategy.engine.data.PlayerID)25 Territory (games.strategy.engine.data.Territory)23 ArrayList (java.util.ArrayList)20 GameData (games.strategy.engine.data.GameData)19 UnitType (games.strategy.engine.data.UnitType)16 Resource (games.strategy.engine.data.Resource)12 Test (org.junit.jupiter.api.Test)11 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)9 Route (games.strategy.engine.data.Route)9 HashSet (java.util.HashSet)8 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)6 Collection (java.util.Collection)6 IntegerMap (games.strategy.util.IntegerMap)5 Tuple (games.strategy.util.Tuple)4 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)3 ITripleAPlayer (games.strategy.triplea.player.ITripleAPlayer)3