Search in sources :

Example 21 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class TransportTracker method isTransportUnloadRestrictedToAnotherTerritory.

// In some versions, a transport can never unload into
// multiple territories in a given turn.
// In WW2V1 a transport can unload to multiple territories in
// non-combat phase, provided they are both adjacent to the sea zone.
static boolean isTransportUnloadRestrictedToAnotherTerritory(final Unit transport, final Territory territory) {
    final Collection<Unit> unloaded = ((TripleAUnit) transport).getUnloaded();
    if (unloaded.isEmpty()) {
        return false;
    }
    // See if transport has unloaded anywhere yet
    final GameData data = transport.getData();
    for (final Unit u : unloaded) {
        final TripleAUnit taUnit = (TripleAUnit) u;
        if (isWW2V2(data) || isTransportUnloadRestricted(data)) {
            // cannot unload to two different territories
            if (!taUnit.getUnloadedTo().equals(territory)) {
                return true;
            }
        } else {
            // cannot unload to two different territories in combat phase
            if (!GameStepPropertiesHelper.isNonCombatMove(transport.getData(), true) && !taUnit.getUnloadedTo().equals(territory)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GameData(games.strategy.engine.data.GameData) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 22 with TripleAUnit

use of games.strategy.triplea.TripleAUnit 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 23 with TripleAUnit

use of games.strategy.triplea.TripleAUnit 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)

Example 24 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class TransportTracker method loadTransportChange.

static Change loadTransportChange(final TripleAUnit transport, final Unit unit) {
    assertTransport(transport);
    final CompositeChange change = new CompositeChange();
    // clear the loaded by
    change.add(ChangeFactory.unitPropertyChange(unit, transport, TripleAUnit.TRANSPORTED_BY));
    final Collection<Unit> newCarrying = new ArrayList<>(transport.getTransporting());
    if (newCarrying.contains(unit)) {
        throw new IllegalStateException("Already carrying, transport:" + transport + " unt:" + unit);
    }
    newCarrying.add(unit);
    change.add(ChangeFactory.unitPropertyChange(unit, Boolean.TRUE, TripleAUnit.LOADED_THIS_TURN));
    change.add(ChangeFactory.unitPropertyChange(transport, true, TripleAUnit.LOADED_THIS_TURN));
    // If the transport was in combat, flag it as being loaded AFTER combat
    if (transport.getWasInCombat()) {
        change.add(ChangeFactory.unitPropertyChange(transport, true, TripleAUnit.LOADED_AFTER_COMBAT));
    }
    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)

Example 25 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class MoveDelegate method removeMovementFromAirOnDamagedAlliedCarriers.

private static void removeMovementFromAirOnDamagedAlliedCarriers(final IDelegateBridge bridge, final PlayerID player) {
    final GameData data = bridge.getData();
    final Predicate<Unit> crippledAlliedCarriersMatch = Matches.isUnitAllied(player, data).and(Matches.unitIsOwnedBy(player).negate()).and(Matches.unitIsCarrier()).and(Matches.unitHasWhenCombatDamagedEffect(UnitAttachment.UNITSMAYNOTLEAVEALLIEDCARRIER));
    final Predicate<Unit> ownedFightersMatch = Matches.unitIsOwnedBy(player).and(Matches.unitIsAir()).and(Matches.unitCanLandOnCarrier()).and(Matches.unitHasMovementLeft());
    final CompositeChange change = new CompositeChange();
    for (final Territory t : data.getMap().getTerritories()) {
        final Collection<Unit> ownedFighters = t.getUnits().getMatches(ownedFightersMatch);
        if (ownedFighters.isEmpty()) {
            continue;
        }
        final Collection<Unit> crippledAlliedCarriers = CollectionUtils.getMatches(t.getUnits().getUnits(), crippledAlliedCarriersMatch);
        if (crippledAlliedCarriers.isEmpty()) {
            continue;
        }
        for (final Unit fighter : ownedFighters) {
            final TripleAUnit taUnit = (TripleAUnit) fighter;
            if (taUnit.getTransportedBy() != null) {
                if (crippledAlliedCarriers.contains(taUnit.getTransportedBy())) {
                    change.add(ChangeFactory.markNoMovementChange(fighter));
                }
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit)

Aggregations

TripleAUnit (games.strategy.triplea.TripleAUnit)47 Unit (games.strategy.engine.data.Unit)42 Territory (games.strategy.engine.data.Territory)25 PlayerID (games.strategy.engine.data.PlayerID)19 UnitType (games.strategy.engine.data.UnitType)16 CompositeChange (games.strategy.engine.data.CompositeChange)15 Test (org.junit.jupiter.api.Test)14 Route (games.strategy.engine.data.Route)13 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)12 ArrayList (java.util.ArrayList)11 GameData (games.strategy.engine.data.GameData)10 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 IntegerMap (games.strategy.util.IntegerMap)8 Change (games.strategy.engine.data.Change)7 RepairRule (games.strategy.engine.data.RepairRule)5 HashSet (java.util.HashSet)4 Resource (games.strategy.engine.data.Resource)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 ResourceCollection (games.strategy.engine.data.ResourceCollection)2