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;
}
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();
}
}
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();
}
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();
}
}
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());
}
Aggregations