Search in sources :

Example 41 with Unit

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

the class TriggerAttachment method removeUnits.

private static void removeUnits(final TriggerAttachment t, final Territory terr, final IntegerMap<UnitType> utMap, final PlayerID player, final IDelegateBridge bridge) {
    final CompositeChange change = new CompositeChange();
    final Collection<Unit> totalRemoved = new ArrayList<>();
    for (final UnitType ut : utMap.keySet()) {
        final int removeNum = utMap.getInt(ut);
        final Collection<Unit> toRemove = CollectionUtils.getNMatches(terr.getUnits().getUnits(), removeNum, Matches.unitIsOwnedBy(player).and(Matches.unitIsOfType(ut)));
        if (!toRemove.isEmpty()) {
            totalRemoved.addAll(toRemove);
            change.add(ChangeFactory.removeUnits(terr, toRemove));
        }
    }
    if (!change.isEmpty()) {
        final String transcriptText = MyFormatter.attachmentNameToText(t.getName()) + ": has removed " + MyFormatter.unitsToTextNoOwner(totalRemoved) + " owned by " + player.getName() + " in " + terr.getName();
        bridge.getHistoryWriter().startEvent(transcriptText, totalRemoved);
        bridge.addChange(change);
    }
}
Also used : UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Unit(games.strategy.engine.data.Unit)

Example 42 with Unit

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

the class UnitAttachment method getAllOfTypeAas.

public static List<String> getAllOfTypeAas(final Collection<Unit> aaUnitsAlreadyVerified) {
    final Set<String> aaSet = new HashSet<>();
    for (final Unit u : aaUnitsAlreadyVerified) {
        aaSet.add(UnitAttachment.get(u.getType()).getTypeAa());
    }
    final List<String> aaTypes = new ArrayList<>(aaSet);
    Collections.sort(aaTypes);
    return aaTypes;
}
Also used : ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) HashSet(java.util.HashSet)

Example 43 with Unit

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

the class MoveDelegate method giveBonusMovementToUnits.

static Change giveBonusMovementToUnits(final PlayerID player, final GameData data, final Territory t) {
    final CompositeChange change = new CompositeChange();
    for (final Unit u : t.getUnits().getUnits()) {
        if (Matches.unitCanBeGivenBonusMovementByFacilitiesInItsTerritory(t, player, data).test(u)) {
            if (!Matches.isUnitAllied(player, data).test(u)) {
                continue;
            }
            int bonusMovement = Integer.MIN_VALUE;
            final Predicate<Unit> givesBonusUnit = Matches.alliedUnit(player, data).and(Matches.unitCanGiveBonusMovementToThisUnit(u));
            final Collection<Unit> givesBonusUnits = new ArrayList<>(CollectionUtils.getMatches(t.getUnits().getUnits(), givesBonusUnit));
            if (Matches.unitIsSea().test(u)) {
                final Predicate<Unit> givesBonusUnitLand = givesBonusUnit.and(Matches.unitIsLand());
                final Set<Territory> neighbors = new HashSet<>(data.getMap().getNeighbors(t, Matches.territoryIsLand()));
                for (final Territory current : neighbors) {
                    givesBonusUnits.addAll(CollectionUtils.getMatches(current.getUnits().getUnits(), givesBonusUnitLand));
                }
            } else if (Matches.unitIsLand().test(u)) {
                final Predicate<Unit> givesBonusUnitSea = givesBonusUnit.and(Matches.unitIsSea());
                final Set<Territory> neighbors = new HashSet<>(data.getMap().getNeighbors(t, Matches.territoryIsWater()));
                for (final Territory current : neighbors) {
                    givesBonusUnits.addAll(CollectionUtils.getMatches(current.getUnits().getUnits(), givesBonusUnitSea));
                }
            }
            for (final Unit bonusGiver : givesBonusUnits) {
                final int tempBonus = UnitAttachment.get(bonusGiver.getType()).getGivesMovement().getInt(u.getType());
                if (tempBonus > bonusMovement) {
                    bonusMovement = tempBonus;
                }
            }
            if (bonusMovement != Integer.MIN_VALUE && bonusMovement != 0) {
                bonusMovement = Math.max(bonusMovement, (UnitAttachment.get(u.getType()).getMovement(player) * -1));
                change.add(ChangeFactory.unitPropertyChange(u, bonusMovement, TripleAUnit.BONUS_MOVEMENT));
            }
        }
    }
    return change;
}
Also used : Territory(games.strategy.engine.data.Territory) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) HashSet(java.util.HashSet) Predicate(java.util.function.Predicate)

Example 44 with Unit

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

the class MoveDelegate method move.

@Override
public String move(final Collection<Unit> units, final Route route, final Collection<Unit> transportsThatCanBeLoaded, final Map<Unit, Collection<Unit>> newDependents) {
    final GameData data = getData();
    // the reason we use this, is if we are in edit mode, we may have a different unit owner than the current player
    final PlayerID player = getUnitsOwner(units);
    final MoveValidationResult result = MoveValidator.validateMove(units, route, player, transportsThatCanBeLoaded, newDependents, GameStepPropertiesHelper.isNonCombatMove(data, false), movesToUndo, 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();
    }
    boolean isKamikaze = false;
    final boolean getKamikazeAir = Properties.getKamikazeAirplanes(data);
    Collection<Unit> kamikazeUnits = new ArrayList<>();
    // confirm kamikaze moves, and remove them from unresolved units
    if (getKamikazeAir || units.stream().anyMatch(Matches.unitIsKamikaze())) {
        kamikazeUnits = result.getUnresolvedUnits(MoveValidator.NOT_ALL_AIR_UNITS_CAN_LAND);
        if (kamikazeUnits.size() > 0 && getRemotePlayer().confirmMoveKamikaze()) {
            for (final Unit unit : kamikazeUnits) {
                if (getKamikazeAir || Matches.unitIsKamikaze().test(unit)) {
                    result.removeUnresolvedUnit(MoveValidator.NOT_ALL_AIR_UNITS_CAN_LAND, unit);
                    isKamikaze = true;
                }
            }
        }
    }
    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);
    final String transcriptText = MyFormatter.unitsToTextNoOwner(units) + " moved from " + route.getStart().getName() + " to " + route.getEnd().getName();
    bridge.getHistoryWriter().startEvent(transcriptText, currentMove.getDescriptionObject());
    if (isKamikaze) {
        bridge.getHistoryWriter().addChildToEvent("This was a kamikaze move, for at least some of the units", kamikazeUnits);
    }
    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) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult)

Example 45 with Unit

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

the class MoveDelegate method repairMultipleHitPointUnits.

static void repairMultipleHitPointUnits(final IDelegateBridge bridge, final PlayerID player) {
    final GameData data = bridge.getData();
    final boolean repairOnlyOwn = Properties.getBattleshipsRepairAtBeginningOfRound(bridge.getData());
    final Predicate<Unit> damagedUnits = Matches.unitHasMoreThanOneHitPointTotal().and(Matches.unitHasTakenSomeDamage());
    final Predicate<Unit> damagedUnitsOwned = damagedUnits.and(Matches.unitIsOwnedBy(player));
    final Map<Territory, Set<Unit>> damagedMap = new HashMap<>();
    for (final Territory current : data.getMap().getTerritories()) {
        final Set<Unit> damaged;
        if (!Properties.getTwoHitPointUnitsRequireRepairFacilities(data)) {
            damaged = new HashSet<>(current.getUnits().getMatches(repairOnlyOwn ? damagedUnitsOwned : damagedUnits));
        } else {
            damaged = new HashSet<>(current.getUnits().getMatches(damagedUnitsOwned.and(Matches.unitCanBeRepairedByFacilitiesInItsTerritory(current, player, data))));
        }
        if (!damaged.isEmpty()) {
            damagedMap.put(current, damaged);
        }
    }
    if (damagedMap.isEmpty()) {
        return;
    }
    final Map<Unit, Territory> fullyRepaired = new HashMap<>();
    final IntegerMap<Unit> newHitsMap = new IntegerMap<>();
    for (final Entry<Territory, Set<Unit>> entry : damagedMap.entrySet()) {
        for (final Unit u : entry.getValue()) {
            final int repairAmount = getLargestRepairRateForThisUnit(u, entry.getKey(), data);
            final int currentHits = u.getHits();
            final int newHits = Math.max(0, Math.min(currentHits, (currentHits - repairAmount)));
            if (newHits != currentHits) {
                newHitsMap.put(u, newHits);
            }
            if (newHits <= 0) {
                fullyRepaired.put(u, entry.getKey());
            }
        }
    }
    bridge.getHistoryWriter().startEvent(newHitsMap.size() + " " + MyFormatter.pluralize("unit", newHitsMap.size()) + " repaired.", new HashSet<>(newHitsMap.keySet()));
    bridge.addChange(ChangeFactory.unitsHit(newHitsMap));
    // now if damaged includes any carriers that are repairing, and have damaged abilities set for not allowing air
    // units to leave while damaged, we need to remove those air units now
    final Collection<Unit> damagedCarriers = CollectionUtils.getMatches(fullyRepaired.keySet(), Matches.unitHasWhenCombatDamagedEffect(UnitAttachment.UNITSMAYNOTLEAVEALLIEDCARRIER));
    // now cycle through those now-repaired carriers, and remove allied air from being dependent
    final CompositeChange clearAlliedAir = new CompositeChange();
    for (final Unit carrier : damagedCarriers) {
        final CompositeChange change = MustFightBattle.clearTransportedByForAlliedAirOnCarrier(Collections.singleton(carrier), fullyRepaired.get(carrier), carrier.getOwner(), data);
        if (!change.isEmpty()) {
            clearAlliedAir.add(change);
        }
    }
    if (!clearAlliedAir.isEmpty()) {
        bridge.addChange(clearAlliedAir);
    }
    // Check if any repaired units change into different unit types
    for (final Territory territory : damagedMap.keySet()) {
        repairedChangeInto(damagedMap.get(territory), territory, bridge);
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange)

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