Search in sources :

Example 71 with CompositeChange

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

the class AbstractTriggerAttachment method triggerSetUsedForThisRound.

public static CompositeChange triggerSetUsedForThisRound(final PlayerID player) {
    final CompositeChange change = new CompositeChange();
    for (final TriggerAttachment ta : TriggerAttachment.getTriggers(player, null)) {
        if (ta.getUsedThisRound()) {
            final int currentUses = ta.getUses();
            if (currentUses > 0) {
                change.add(ChangeFactory.attachmentPropertyChange(ta, Integer.toString(currentUses - 1), "uses"));
                change.add(ChangeFactory.attachmentPropertyChange(ta, false, "usedThisRound"));
            }
        }
    }
    return change;
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange)

Example 72 with CompositeChange

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

the class AbstractEndTurnDelegate method changeUnitOwnership.

private static void changeUnitOwnership(final IDelegateBridge bridge) {
    final PlayerID player = bridge.getPlayerId();
    final PlayerAttachment pa = PlayerAttachment.get(player);
    final Collection<PlayerID> possibleNewOwners = pa.getGiveUnitControl();
    final Collection<Territory> territories = bridge.getData().getMap().getTerritories();
    final CompositeChange change = new CompositeChange();
    final Collection<Tuple<Territory, Collection<Unit>>> changeList = new ArrayList<>();
    for (final Territory currTerritory : territories) {
        final TerritoryAttachment ta = TerritoryAttachment.get(currTerritory);
        // if ownership should change in this territory
        if (ta != null && ta.getChangeUnitOwners() != null && !ta.getChangeUnitOwners().isEmpty()) {
            final Collection<PlayerID> terrNewOwners = ta.getChangeUnitOwners();
            for (final PlayerID terrNewOwner : terrNewOwners) {
                if (possibleNewOwners.contains(terrNewOwner)) {
                    // PlayerOwnerChange
                    final Collection<Unit> units = currTerritory.getUnits().getMatches(Matches.unitOwnedBy(player).and(Matches.unitCanBeGivenByTerritoryTo(terrNewOwner)));
                    if (!units.isEmpty()) {
                        change.add(ChangeFactory.changeOwner(units, terrNewOwner, currTerritory));
                        changeList.add(Tuple.of(currTerritory, units));
                    }
                }
            }
        }
    }
    if (!change.isEmpty() && !changeList.isEmpty()) {
        if (changeList.size() == 1) {
            final Tuple<Territory, Collection<Unit>> tuple = changeList.iterator().next();
            bridge.getHistoryWriter().startEvent("Some Units in " + tuple.getFirst().getName() + " change ownership: " + MyFormatter.unitsToTextNoOwner(tuple.getSecond()), tuple.getSecond());
        } else {
            bridge.getHistoryWriter().startEvent("Units Change Ownership");
            for (final Tuple<Territory, Collection<Unit>> tuple : changeList) {
                bridge.getHistoryWriter().addChildToEvent("Some Units in " + tuple.getFirst().getName() + " change ownership: " + MyFormatter.unitsToTextNoOwner(tuple.getSecond()), tuple.getSecond());
            }
        }
        bridge.addChange(change);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) Collection(java.util.Collection) CompositeChange(games.strategy.engine.data.CompositeChange) Tuple(games.strategy.util.Tuple)

Example 73 with CompositeChange

use of games.strategy.engine.data.CompositeChange 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 74 with CompositeChange

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

the class TriggerAttachmentTest method testTriggerProductionFrontierEditChange.

@Test
public void testTriggerProductionFrontierEditChange() {
    TriggerAttachment.triggerProductionFrontierEditChange(satisfiedTriggers, bridge, "beforeOrAfter", "stepName", // useUses
    false, // testUses
    false, // testChance
    false, // testWhen
    false);
    final ArgumentCaptor<CompositeChange> argument = ArgumentCaptor.forClass(CompositeChange.class);
    verify(bridge).addChange(argument.capture());
    final CompositeChange change = argument.getValue();
    assertFalse(change.isEmpty());
    final ArgumentCaptor<String> ruleAddArgument = ArgumentCaptor.forClass(String.class);
    verify(historyWriter, times(3)).startEvent(ruleAddArgument.capture());
    final List<String> allValues = ruleAddArgument.getAllValues();
    assertEquals(3, allValues.size());
    assertTrue(allValues.stream().anyMatch(s -> s.contains("rule1Name") && s.contains("added")));
    assertTrue(allValues.stream().anyMatch(s -> s.contains("rule2Name") && s.contains("removed")));
    assertTrue(allValues.stream().anyMatch(s -> s.contains("rule3Name") && s.contains("added")));
    assertTrue(allValues.stream().allMatch(s -> s.contains("frontierName")));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Mock(org.mockito.Mock) IDelegateHistoryWriter(games.strategy.engine.history.IDelegateHistoryWriter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) ProductionFrontierList(games.strategy.engine.data.ProductionFrontierList) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) CompositeChange(games.strategy.engine.data.CompositeChange) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) MockitoExtension(com.example.mockito.MockitoExtension) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) GameData(games.strategy.engine.data.GameData) List(java.util.List) ProductionRuleList(games.strategy.engine.data.ProductionRuleList) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProductionRule(games.strategy.engine.data.ProductionRule) Mockito.mock(org.mockito.Mockito.mock) CompositeChange(games.strategy.engine.data.CompositeChange) Test(org.junit.jupiter.api.Test)

Aggregations

CompositeChange (games.strategy.engine.data.CompositeChange)74 Unit (games.strategy.engine.data.Unit)48 TripleAUnit (games.strategy.triplea.TripleAUnit)40 GameData (games.strategy.engine.data.GameData)31 Territory (games.strategy.engine.data.Territory)25 ArrayList (java.util.ArrayList)22 PlayerID (games.strategy.engine.data.PlayerID)21 Change (games.strategy.engine.data.Change)17 UnitType (games.strategy.engine.data.UnitType)14 Resource (games.strategy.engine.data.Resource)9 HashSet (java.util.HashSet)9 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 IntegerMap (games.strategy.util.IntegerMap)8 Tuple (games.strategy.util.Tuple)7 Collection (java.util.Collection)7 RelationshipType (games.strategy.engine.data.RelationshipType)6 Set (java.util.Set)6 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)4 ProductionRule (games.strategy.engine.data.ProductionRule)4 HashMap (java.util.HashMap)4