Search in sources :

Example 36 with CompositeChange

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

the class SpecialMoveDelegate method getNewAssignmentOfNumberLaunchedChange.

private static Change getNewAssignmentOfNumberLaunchedChange(int newNumberLaunched, final Collection<Unit> bases, final PlayerID player, final GameData data) {
    final CompositeChange launchedChange = new CompositeChange();
    if (newNumberLaunched <= 0) {
        return launchedChange;
    }
    final IntegerMap<UnitType> capacityMap = TechAbilityAttachment.getAirborneCapacity(player, data);
    for (final Unit u : bases) {
        if (newNumberLaunched <= 0) {
            break;
        }
        final int numberLaunchedAlready = ((TripleAUnit) u).getLaunched();
        final int capacity = capacityMap.getInt(u.getType());
        final int toAdd = Math.min(newNumberLaunched, capacity - numberLaunchedAlready);
        if (toAdd <= 0) {
            continue;
        }
        newNumberLaunched -= toAdd;
        launchedChange.add(ChangeFactory.unitPropertyChange(u, (toAdd + numberLaunchedAlready), TripleAUnit.LAUNCHED));
    }
    return launchedChange;
}
Also used : UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 37 with CompositeChange

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

the class SpecialMoveDelegate method move.

@Override
public String move(final Collection<Unit> units, final Route route, final Collection<Unit> transportsThatCanBeLoaded, final Map<Unit, Collection<Unit>> newDependents) {
    if (!allowAirborne(player, getData())) {
        return "No Airborne Movement Allowed Yet";
    }
    final GameData data = getData();
    // there reason we use this, is because if we are in edit mode, we may have a different unit owner than the current
    // player.
    final PlayerID player = getUnitsOwner(units);
    // here we have our own new validation method....
    final MoveValidationResult result = validateMove(units, route, player, data);
    final StringBuilder errorMsg = new StringBuilder(100);
    final int numProblems = result.getTotalWarningCount() - (result.hasError() ? 0 : 1);
    final String numErrorsMsg = numProblems > 0 ? ("; " + numProblems + " " + MyFormatter.pluralize("error", numProblems) + " not shown") : "";
    if (result.hasError()) {
        return errorMsg.append(result.getError()).append(numErrorsMsg).toString();
    }
    if (result.hasDisallowedUnits()) {
        return errorMsg.append(result.getDisallowedUnitWarning(0)).append(numErrorsMsg).toString();
    }
    if (result.hasUnresolvedUnits()) {
        return errorMsg.append(result.getUnresolvedUnitWarning(0)).append(numErrorsMsg).toString();
    }
    // allow user to cancel move if aa guns will fire
    final AAInMoveUtil aaInMoveUtil = new AAInMoveUtil();
    aaInMoveUtil.initialize(bridge);
    final Collection<Territory> aaFiringTerritores = aaInMoveUtil.getTerritoriesWhereAaWillFire(route, units);
    if (!aaFiringTerritores.isEmpty()) {
        if (!getRemotePlayer().confirmMoveInFaceOfAa(aaFiringTerritores)) {
            return null;
        }
    }
    // do the move
    final UndoableMove currentMove = new UndoableMove(units, route);
    // add dependencies (any move that came before this, from this start territory, is a dependency)
    for (final UndoableMove otherMove : movesToUndo) {
        if (otherMove.getStart().equals(route.getStart())) {
            currentMove.addDependency(otherMove);
        }
    }
    // make the units airborne
    final CompositeChange airborneChange = new CompositeChange();
    for (final Unit u : units) {
        airborneChange.add(ChangeFactory.unitPropertyChange(u, true, TripleAUnit.AIRBORNE));
    }
    currentMove.addChange(airborneChange);
    // make the bases start filling up their capacity
    final Collection<Unit> basesAtStart = route.getStart().getUnits().getMatches(getAirborneBaseMatch(player, data));
    final Change fillLaunchCapacity = getNewAssignmentOfNumberLaunchedChange(units.size(), basesAtStart, player, data);
    currentMove.addChange(fillLaunchCapacity);
    // start event
    final String transcriptText = MyFormatter.unitsToTextNoOwner(units) + " moved from " + route.getStart().getName() + " to " + route.getEnd().getName();
    bridge.getHistoryWriter().startEvent(transcriptText, currentMove.getDescriptionObject());
    // actually do our special changes
    bridge.addChange(airborneChange);
    bridge.addChange(fillLaunchCapacity);
    tempMovePerformer = new MovePerformer();
    tempMovePerformer.initialize(this);
    tempMovePerformer.moveUnits(units, route, player, transportsThatCanBeLoaded, newDependents, currentMove);
    tempMovePerformer = null;
    return null;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) 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) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 38 with CompositeChange

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

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

the class TransportTracker method unloadTransportChange.

static Change unloadTransportChange(final TripleAUnit unit, final Territory territory, final boolean dependentBattle) {
    final CompositeChange change = new CompositeChange();
    final TripleAUnit transport = (TripleAUnit) transportedBy(unit);
    if (transport == null) {
        return change;
    }
    assertTransport(transport);
    if (!transport.getTransporting().contains(unit)) {
        throw new IllegalStateException("Not being carried, unit:" + unit + " transport:" + transport);
    }
    final ArrayList<Unit> newUnloaded = new ArrayList<>(transport.getUnloaded());
    newUnloaded.add(unit);
    change.add(ChangeFactory.unitPropertyChange(unit, territory, TripleAUnit.UNLOADED_TO));
    if (!GameStepPropertiesHelper.isNonCombatMove(unit.getData(), true)) {
        change.add(ChangeFactory.unitPropertyChange(unit, true, TripleAUnit.UNLOADED_IN_COMBAT_PHASE));
        change.add(ChangeFactory.unitPropertyChange(unit, true, TripleAUnit.UNLOADED_AMPHIBIOUS));
        change.add(ChangeFactory.unitPropertyChange(transport, true, TripleAUnit.UNLOADED_IN_COMBAT_PHASE));
        change.add(ChangeFactory.unitPropertyChange(transport, true, TripleAUnit.UNLOADED_AMPHIBIOUS));
    }
    if (!dependentBattle) {
        // TODO: this is causing issues with Scrambling. if the units were unloaded, then scrambling creates a battle,
        // there is no longer any
        // way to have the units removed if those transports die.
        change.add(ChangeFactory.unitPropertyChange(unit, null, TripleAUnit.TRANSPORTED_BY));
    }
    change.add(ChangeFactory.unitPropertyChange(transport, newUnloaded, TripleAUnit.UNLOADED));
    return change;
}
Also used : ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 40 with CompositeChange

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

the class TransportTracker method unloadAirTransportChange.

static Change unloadAirTransportChange(final TripleAUnit unit, final Territory territory, final boolean dependentBattle) {
    final CompositeChange change = new CompositeChange();
    final TripleAUnit transport = (TripleAUnit) transportedBy(unit);
    if (transport == null) {
        return change;
    }
    assertTransport(transport);
    if (!transport.getTransporting().contains(unit)) {
        throw new IllegalStateException("Not being carried, unit:" + unit + " transport:" + transport);
    }
    final ArrayList<Unit> newUnloaded = new ArrayList<>(transport.getUnloaded());
    newUnloaded.add(unit);
    change.add(ChangeFactory.unitPropertyChange(unit, territory, TripleAUnit.UNLOADED_TO));
    if (!GameStepPropertiesHelper.isNonCombatMove(unit.getData(), true)) {
        change.add(ChangeFactory.unitPropertyChange(unit, true, TripleAUnit.UNLOADED_IN_COMBAT_PHASE));
        // change.add(ChangeFactory.unitPropertyChange(unit, true, TripleAUnit.UNLOADED_AMPHIBIOUS));
        change.add(ChangeFactory.unitPropertyChange(transport, true, TripleAUnit.UNLOADED_IN_COMBAT_PHASE));
    // change.add(ChangeFactory.unitPropertyChange(transport, true, TripleAUnit.UNLOADED_AMPHIBIOUS));
    }
    if (!dependentBattle) {
        // TODO: this is causing issues with Scrambling. if the units were unloaded, then scrambling creates a battle,
        // there is no longer any
        // way to have the units removed if those transports die.
        change.add(ChangeFactory.unitPropertyChange(unit, null, TripleAUnit.TRANSPORTED_BY));
    }
    // change.add(ChangeFactory.unitPropertyChange(transport, newUnloaded, TripleAUnit.UNLOADED));
    return change;
}
Also used : ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) TripleAUnit(games.strategy.triplea.TripleAUnit)

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