Search in sources :

Example 66 with Unit

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

the class MustFightBattle method retreatFromNonCombat.

/**
 * Retreat landed units from allied territory when their transport retreats.
 */
private Change retreatFromNonCombat(Collection<Unit> units, final Territory retreatTo) {
    final CompositeChange change = new CompositeChange();
    units = CollectionUtils.getMatches(units, Matches.unitIsTransport());
    final Collection<Unit> retreated = getTransportDependents(units);
    if (!retreated.isEmpty()) {
        for (final Unit unit : units) {
            final Territory retreatedFrom = TransportTracker.getTerritoryTransportHasUnloadedTo(unit);
            if (retreatedFrom != null) {
                reLoadTransports(units, change);
                change.add(ChangeFactory.moveUnits(retreatedFrom, retreatTo, retreated));
            }
        }
    }
    return change;
}
Also used : Territory(games.strategy.engine.data.Territory) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 67 with Unit

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

the class MustFightBattle method retreatFromDependents.

private Change retreatFromDependents(final Collection<Unit> units, final Territory retreatTo, final Collection<IBattle> dependentBattles) {
    final CompositeChange change = new CompositeChange();
    for (final IBattle dependent : dependentBattles) {
        final Route route = new Route();
        route.setStart(m_battleSite);
        route.add(dependent.getTerritory());
        final Collection<Unit> retreatedUnits = dependent.getDependentUnits(units);
        dependent.removeAttack(route, retreatedUnits);
        reLoadTransports(units, change);
        change.add(ChangeFactory.moveUnits(dependent.getTerritory(), retreatTo, retreatedUnits));
    }
    return change;
}
Also used : CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route)

Example 68 with Unit

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

the class MustFightBattle method getEmptyOrFriendlySeaNeighbors.

private Collection<Territory> getEmptyOrFriendlySeaNeighbors(final PlayerID player, final Collection<Unit> unitsToRetreat) {
    Collection<Territory> possible = m_data.getMap().getNeighbors(m_battleSite);
    if (m_headless) {
        return possible;
    }
    // make sure we can move through the any canals
    final Predicate<Territory> canalMatch = t -> {
        final Route r = new Route();
        r.setStart(m_battleSite);
        r.add(t);
        return MoveValidator.validateCanal(r, unitsToRetreat, m_defender, m_data) == null;
    };
    final Predicate<Territory> match = Matches.territoryIsWater().and(Matches.territoryHasNoEnemyUnits(player, m_data)).and(canalMatch);
    possible = CollectionUtils.getMatches(possible, match);
    return possible;
}
Also used : ITripleADisplay(games.strategy.triplea.ui.display.ITripleADisplay) BattleRecord(games.strategy.triplea.delegate.dataObjects.BattleRecord) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) HashMap(java.util.HashMap) TechAttachment(games.strategy.triplea.attachments.TechAttachment) PredicateBuilder(games.strategy.util.PredicateBuilder) Properties(games.strategy.triplea.Properties) TuvUtils(games.strategy.triplea.util.TuvUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Route(games.strategy.engine.data.Route) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) Map(java.util.Map) UnitType(games.strategy.engine.data.UnitType) TechAbilityAttachment(games.strategy.triplea.attachments.TechAbilityAttachment) TripleAUnit(games.strategy.triplea.TripleAUnit) CompositeChange(games.strategy.engine.data.CompositeChange) LinkedHashSet(java.util.LinkedHashSet) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) Unit(games.strategy.engine.data.Unit) Iterator(java.util.Iterator) Interruptibles(games.strategy.util.Interruptibles) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) GameData(games.strategy.engine.data.GameData) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) List(java.util.List) Tuple(games.strategy.util.Tuple) PlayerID(games.strategy.engine.data.PlayerID) Change(games.strategy.engine.data.Change) MyFormatter(games.strategy.triplea.formatter.MyFormatter) Comparator(java.util.Comparator) TerritoryEffect(games.strategy.engine.data.TerritoryEffect) Collections(java.util.Collections) SoundPath(games.strategy.sound.SoundPath) Territory(games.strategy.engine.data.Territory) Route(games.strategy.engine.data.Route)

Example 69 with Unit

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

the class MustFightBattle method retreatUnitsAndPlanes.

private void retreatUnitsAndPlanes(final Collection<Unit> retreating, final Territory to, final boolean defender, final IDelegateBridge bridge) {
    // Remove air from battle
    final Collection<Unit> units = defender ? m_defendingUnits : m_attackingUnits;
    final Collection<Unit> unitsRetreated = defender ? m_defendingUnitsRetreated : m_attackingUnitsRetreated;
    units.removeAll(CollectionUtils.getMatches(units, Matches.unitIsAir()));
    // add all land units' dependents
    retreating.addAll(getDependentUnits(units));
    // our own air units don't retreat with land units
    final Predicate<Unit> notMyAir = Matches.unitIsNotAir().or(Matches.unitIsOwnedBy(m_attacker).negate());
    final Collection<Unit> nonAirRetreating = CollectionUtils.getMatches(retreating, notMyAir);
    final String transcriptText = MyFormatter.unitsToTextNoOwner(nonAirRetreating) + " retreated to " + to.getName();
    bridge.getHistoryWriter().addChildToEvent(transcriptText, new ArrayList<>(nonAirRetreating));
    final CompositeChange change = new CompositeChange();
    change.add(ChangeFactory.moveUnits(m_battleSite, to, nonAirRetreating));
    if (m_isOver) {
        final Collection<IBattle> dependentBattles = m_battleTracker.getBlocked(this);
        // If there are no dependent battles, check landings in allied territories
        if (dependentBattles.isEmpty()) {
            change.add(retreatFromNonCombat(nonAirRetreating, to));
        // Else retreat the units from combat when their transport retreats
        } else {
            change.add(retreatFromDependents(nonAirRetreating, to, dependentBattles));
        }
    }
    bridge.addChange(change);
    units.removeAll(nonAirRetreating);
    unitsRetreated.addAll(nonAirRetreating);
    if (units.isEmpty() || m_isOver) {
        endBattle(bridge);
        if (defender) {
            attackerWins(bridge);
        } else {
            defenderWins(bridge);
        }
    } else {
        getDisplay(bridge).notifyRetreat(m_battleID, retreating);
    }
}
Also used : TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 70 with Unit

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

the class MustFightBattle method sortAmphib.

/**
 * In an amphibious assault, sort on who is unloading from transports first as this will allow the marines with higher
 * scores to get killed last.
 */
private void sortAmphib(final List<Unit> units) {
    final Comparator<Unit> decreasingMovement = UnitComparator.getLowestToHighestMovementComparator();
    units.sort(Comparator.comparing(Unit::getType, Comparator.comparing(UnitType::getName)).thenComparing((u1, u2) -> {
        final UnitAttachment ua = UnitAttachment.get(u1.getType());
        final UnitAttachment ua2 = UnitAttachment.get(u2.getType());
        if (ua.getIsMarine() != 0 && ua2.getIsMarine() != 0) {
            return compareAccordingToAmphibious(u1, u2);
        }
        return 0;
    }).thenComparing(decreasingMovement));
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Aggregations

Unit (games.strategy.engine.data.Unit)447 TripleAUnit (games.strategy.triplea.TripleAUnit)301 Territory (games.strategy.engine.data.Territory)255 ArrayList (java.util.ArrayList)204 PlayerID (games.strategy.engine.data.PlayerID)135 GameData (games.strategy.engine.data.GameData)103 HashSet (java.util.HashSet)92 Test (org.junit.jupiter.api.Test)91 Route (games.strategy.engine.data.Route)89 UnitType (games.strategy.engine.data.UnitType)85 CompositeChange (games.strategy.engine.data.CompositeChange)64 HashMap (java.util.HashMap)64 IntegerMap (games.strategy.util.IntegerMap)61 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)58 Collection (java.util.Collection)58 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)56 List (java.util.List)48 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)47 Change (games.strategy.engine.data.Change)44 Set (java.util.Set)43