Search in sources :

Example 21 with PlayerID

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

the class MoveDelegate method removeAirThatCantLand.

private void removeAirThatCantLand() {
    final GameData data = getData();
    final boolean lhtrCarrierProd = AirThatCantLandUtil.isLhtrCarrierProduction(data) || AirThatCantLandUtil.isLandExistingFightersOnNewCarriers(data);
    boolean hasProducedCarriers = false;
    for (final PlayerID p : GameStepPropertiesHelper.getCombinedTurns(data, player)) {
        if (p.getUnits().anyMatch(Matches.unitIsCarrier())) {
            hasProducedCarriers = true;
            break;
        }
    }
    final AirThatCantLandUtil util = new AirThatCantLandUtil(bridge);
    util.removeAirThatCantLand(player, lhtrCarrierProd && hasProducedCarriers);
    // if edit mode has been on, we need to clean up after all players
    for (final PlayerID player : data.getPlayerList()) {
        // Check if player still has units to place
        if (!player.equals(this.player)) {
            util.removeAirThatCantLand(player, ((player.getUnits().anyMatch(Matches.unitIsCarrier()) || hasProducedCarriers) && lhtrCarrierProd));
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData)

Example 22 with PlayerID

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

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

the class MoveDelegate method getLargestRepairRateForThisUnit.

/**
 * This has to be the exact same as Matches.UnitCanBeRepairedByFacilitiesInItsTerritory()
 */
private static int getLargestRepairRateForThisUnit(final Unit unitToBeRepaired, final Territory territoryUnitIsIn, final GameData data) {
    if (!Properties.getTwoHitPointUnitsRequireRepairFacilities(data)) {
        return 1;
    }
    final PlayerID owner = unitToBeRepaired.getOwner();
    final Predicate<Unit> repairUnit = Matches.alliedUnit(owner, data).and(Matches.unitCanRepairOthers()).and(Matches.unitCanRepairThisUnit(unitToBeRepaired, territoryUnitIsIn));
    final Set<Unit> repairUnitsForThisUnit = new HashSet<>(territoryUnitIsIn.getUnits().getMatches(repairUnit));
    if (Matches.unitIsSea().test(unitToBeRepaired)) {
        final List<Territory> neighbors = new ArrayList<>(data.getMap().getNeighbors(territoryUnitIsIn, Matches.territoryIsLand()));
        for (final Territory current : neighbors) {
            final Predicate<Unit> repairUnitLand = Matches.alliedUnit(owner, data).and(Matches.unitCanRepairOthers()).and(Matches.unitCanRepairThisUnit(unitToBeRepaired, current)).and(Matches.unitIsLand());
            repairUnitsForThisUnit.addAll(current.getUnits().getMatches(repairUnitLand));
        }
    } else if (Matches.unitIsLand().test(unitToBeRepaired)) {
        final List<Territory> neighbors = new ArrayList<>(data.getMap().getNeighbors(territoryUnitIsIn, Matches.territoryIsWater()));
        for (final Territory current : neighbors) {
            final Predicate<Unit> repairUnitSea = Matches.alliedUnit(owner, data).and(Matches.unitCanRepairOthers()).and(Matches.unitCanRepairThisUnit(unitToBeRepaired, current)).and(Matches.unitIsSea());
            repairUnitsForThisUnit.addAll(current.getUnits().getMatches(repairUnitSea));
        }
    }
    int largest = 0;
    for (final Unit u : repairUnitsForThisUnit) {
        final int repair = UnitAttachment.get(u.getType()).getRepairsUnits().getInt(unitToBeRepaired.getType());
        if (largest < repair) {
            largest = repair;
        }
    }
    return largest;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) HashSet(java.util.HashSet) Predicate(java.util.function.Predicate)

Example 24 with PlayerID

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

the class MoveValidator method validateMove.

public static MoveValidationResult validateMove(final Collection<Unit> units, final Route route, final PlayerID player, final Collection<Unit> transportsToLoad, final Map<Unit, Collection<Unit>> newDependents, final boolean isNonCombat, final List<UndoableMove> undoableMoves, final GameData data) {
    final MoveValidationResult result = new MoveValidationResult();
    if (route.hasNoSteps()) {
        return result;
    }
    if (validateFirst(data, units, route, player, result).getError() != null) {
        return result;
    }
    if (isNonCombat) {
        if (validateNonCombat(data, units, route, player, result).getError() != null) {
            return result;
        }
    } else {
        if (validateCombat(data, units, route, player, result).getError() != null) {
            return result;
        }
    }
    if (validateNonEnemyUnitsOnPath(data, units, route, player, result).getError() != null) {
        return result;
    }
    if (validateBasic(data, units, route, player, transportsToLoad, newDependents, result).getError() != null) {
        return result;
    }
    if (AirMovementValidator.validateAirCanLand(data, units, route, player, result).getError() != null) {
        return result;
    }
    if (validateTransport(isNonCombat, data, undoableMoves, units, route, player, transportsToLoad, result).getError() != null) {
        return result;
    }
    if (validateParatroops(isNonCombat, data, units, route, player, result).getError() != null) {
        return result;
    }
    if (validateCanal(data, units, route, player, result).getError() != null) {
        return result;
    }
    if (validateFuel(data, units, route, player, result).getError() != null) {
        return result;
    }
    // Don't let the user move out of a battle zone, the exception is air units and unloading units into a battle zone
    if (AbstractMoveDelegate.getBattleTracker(data).hasPendingBattle(route.getStart(), false) && units.stream().anyMatch(Matches.unitIsNotAir())) {
        // if the units did not move into the territory, then they can move out this will happen if there is a submerged
        // sub in the area, and a different unit moved into the sea zone setting up a battle but the original unit can
        // still remain
        boolean unitsStartedInTerritory = true;
        for (final Unit unit : units) {
            if (AbstractMoveDelegate.getRouteUsedToMoveInto(undoableMoves, unit, route.getEnd()) != null) {
                unitsStartedInTerritory = false;
                break;
            }
        }
        if (!unitsStartedInTerritory) {
            final boolean unload = route.isUnload();
            final PlayerID endOwner = route.getEnd().getOwner();
            final boolean attack = !data.getRelationshipTracker().isAllied(endOwner, player) || AbstractMoveDelegate.getBattleTracker(data).wasConquered(route.getEnd());
            // unless they are unloading into another battle
            if (!(unload && attack)) {
                return result.setErrorReturnResult("Cannot move units out of battle zone");
            }
        }
    }
    return result;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult)

Example 25 with PlayerID

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

the class MustFightBattle method fire.

private void fire(final String stepName, final Collection<Unit> firingUnits, final Collection<Unit> attackableUnits, final List<Unit> allEnemyUnitsAliveOrWaitingToDie, final boolean defender, final ReturnFire returnFire, final String text) {
    final PlayerID firing = defender ? m_defender : m_attacker;
    final PlayerID defending = !defender ? m_defender : m_attacker;
    if (firingUnits.isEmpty()) {
        return;
    }
    // Fire each type of suicide on hit unit separately and then remaining units
    final List<Collection<Unit>> firingGroups = createFiringUnitGroups(firingUnits);
    for (final Collection<Unit> units : firingGroups) {
        m_stack.push(new Fire(attackableUnits, returnFire, firing, defending, units, stepName, text, this, defender, m_dependentUnits, m_headless, m_battleSite, m_territoryEffects, allEnemyUnitsAliveOrWaitingToDie));
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Collection(java.util.Collection) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Aggregations

PlayerID (games.strategy.engine.data.PlayerID)323 Territory (games.strategy.engine.data.Territory)163 Unit (games.strategy.engine.data.Unit)133 Test (org.junit.jupiter.api.Test)122 TripleAUnit (games.strategy.triplea.TripleAUnit)104 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)94 GameData (games.strategy.engine.data.GameData)90 ArrayList (java.util.ArrayList)79 UnitType (games.strategy.engine.data.UnitType)74 Route (games.strategy.engine.data.Route)67 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)46 HashSet (java.util.HashSet)44 Change (games.strategy.engine.data.Change)29 CompositeChange (games.strategy.engine.data.CompositeChange)29 IntegerMap (games.strategy.util.IntegerMap)29 List (java.util.List)29 HashMap (java.util.HashMap)28 Collection (java.util.Collection)27 Resource (games.strategy.engine.data.Resource)25 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)23