Search in sources :

Example 1 with Territory

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

the class BattleDelegate method selectBombardingBattle.

/**
 * Select which territory to bombard.
 */
private IBattle selectBombardingBattle(final Unit u, final Territory unitTerritory, final Collection<IBattle> battles) {
    final boolean bombardRestricted = isShoreBombardPerGroundUnitRestricted(getData());
    // If only one battle to select from just return that battle
    if ((battles.size() == 1)) {
        return battles.iterator().next();
    }
    final List<Territory> territories = new ArrayList<>();
    final Map<Territory, IBattle> battleTerritories = new HashMap<>();
    for (final IBattle battle : battles) {
        // If Restricted & # of bombarding units => landing units, don't add territory to list to bombard
        if (bombardRestricted) {
            if (battle.getBombardingUnits().size() < battle.getAmphibiousLandAttackers().size()) {
                territories.add(battle.getTerritory());
            }
        } else {
            territories.add(battle.getTerritory());
        }
        battleTerritories.put(battle.getTerritory(), battle);
    }
    final ITripleAPlayer remotePlayer = getRemotePlayer();
    Territory bombardingTerritory = null;
    if (!territories.isEmpty()) {
        bombardingTerritory = remotePlayer.selectBombardingTerritory(u, unitTerritory, territories, true);
    }
    if (bombardingTerritory != null) {
        return battleTerritories.get(bombardingTerritory);
    }
    // User elected not to bombard with this unit
    return null;
}
Also used : Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer)

Example 2 with Territory

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

the class BattleDelegate method scramblingCleanup.

private void scramblingCleanup() {
    // return scrambled units to their original territories, or let them move 1 or x to a new territory.
    final GameData data = getData();
    if (!Properties.getScrambleRulesInEffect(data)) {
        return;
    }
    final boolean mustReturnToBase = Properties.getScrambledUnitsReturnToBase(data);
    for (final Territory t : data.getMap().getTerritories()) {
        int carrierCostOfCurrentTerr = 0;
        final Collection<Unit> wasScrambled = t.getUnits().getMatches(Matches.unitWasScrambled());
        for (final Unit u : wasScrambled) {
            final CompositeChange change = new CompositeChange();
            final Territory originatedFrom = TripleAUnit.get(u).getOriginatedFrom();
            Territory landingTerr = null;
            final String historyText;
            if (!mustReturnToBase || !Matches.isTerritoryAllied(u.getOwner(), data).test(originatedFrom)) {
                final Collection<Territory> possible = whereCanAirLand(Collections.singletonList(u), t, u.getOwner(), data, battleTracker, carrierCostOfCurrentTerr, 1, !mustReturnToBase);
                if (possible.size() > 1) {
                    landingTerr = getRemotePlayer(u.getOwner()).selectTerritoryForAirToLand(possible, t, "Select territory for air units to land. (Current territory is " + t.getName() + "): " + MyFormatter.unitsToText(Collections.singletonList(u)));
                } else if (possible.size() == 1) {
                    landingTerr = possible.iterator().next();
                }
                if (landingTerr == null || landingTerr.equals(t)) {
                    carrierCostOfCurrentTerr += AirMovementValidator.carrierCost(Collections.singletonList(u));
                    historyText = "Scrambled unit stays in territory " + t.getName();
                } else {
                    historyText = "Moving scrambled unit from " + t.getName() + " to " + landingTerr.getName();
                }
            } else {
                landingTerr = originatedFrom;
                historyText = "Moving scrambled unit from " + t.getName() + " back to originating territory: " + landingTerr.getName();
            }
            // if null, we leave it to die
            if (landingTerr != null) {
                change.add(ChangeFactory.moveUnits(t, landingTerr, Collections.singletonList(u)));
                change.add(Route.getFuelChanges(Collections.singleton(u), new Route(t, landingTerr), u.getOwner(), data));
            }
            change.add(ChangeFactory.unitPropertyChange(u, null, TripleAUnit.ORIGINATED_FROM));
            change.add(ChangeFactory.unitPropertyChange(u, false, TripleAUnit.WAS_SCRAMBLED));
            if (!change.isEmpty()) {
                bridge.getHistoryWriter().startEvent(historyText, u);
                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) Route(games.strategy.engine.data.Route)

Example 3 with Territory

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

the class BattleDelegate method doKamikazeSuicideAttacks.

/**
 * KamikazeSuicideAttacks are attacks that are made during an Opponent's turn, using Resources that you own that have
 * been designated.
 * The resources are designated in PlayerAttachment, and hold information like the attack power of the resource.
 * KamikazeSuicideAttacks are done in any territory that is a kamikazeZone, and the attacks are done by the original
 * owner of that
 * territory.
 * The user has the option not to do any attacks, and they make target any number of units with any number of resource
 * tokens.
 * The units are then attacked individually by each resource token (meaning that casualties do not get selected
 * because the attacks are
 * targeted).
 * The enemies of current player should decide all their attacks before the attacks are rolled.
 */
private void doKamikazeSuicideAttacks() {
    final GameData data = getData();
    if (!Properties.getUseKamikazeSuicideAttacks(data)) {
        return;
    }
    // the current player is not the one who is doing these attacks, it is the all the enemies of this player who will
    // do attacks
    final Collection<PlayerID> enemies = CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAtWar(player, data));
    if (enemies.isEmpty()) {
        return;
    }
    final Predicate<Unit> canBeAttackedDefault = Matches.unitIsOwnedBy(player).and(Matches.unitIsSea()).and(Matches.unitIsNotTransportButCouldBeCombatTransport()).and(Matches.unitIsNotSub());
    final boolean onlyWhereThereAreBattlesOrAmphibious = Properties.getKamikazeSuicideAttacksOnlyWhereBattlesAre(data);
    final Collection<Territory> pendingBattles = battleTracker.getPendingBattleSites(false);
    // create a list of all kamikaze zones, listed by enemy
    final Map<PlayerID, Collection<Territory>> kamikazeZonesByEnemy = new HashMap<>();
    for (final Territory t : data.getMap().getTerritories()) {
        final TerritoryAttachment ta = TerritoryAttachment.get(t);
        if (ta == null || !ta.getKamikazeZone()) {
            continue;
        }
        final PlayerID owner = !Properties.getKamikazeSuicideAttacksDoneByCurrentTerritoryOwner(data) ? ta.getOriginalOwner() : t.getOwner();
        if (owner == null) {
            continue;
        }
        if (enemies.contains(owner)) {
            if (t.getUnits().getUnits().stream().noneMatch(Matches.unitIsOwnedBy(player))) {
                continue;
            }
            if (onlyWhereThereAreBattlesOrAmphibious) {
                // if no battle or amphibious from here, ignore it
                if (!pendingBattles.contains(t)) {
                    if (!Matches.territoryIsWater().test(t)) {
                        continue;
                    }
                    boolean amphib = false;
                    final Collection<Territory> landNeighbors = data.getMap().getNeighbors(t, Matches.territoryIsLand());
                    for (final Territory neighbor : landNeighbors) {
                        final IBattle battle = battleTracker.getPendingBattle(neighbor, false, BattleType.NORMAL);
                        if (battle == null) {
                            final Map<Territory, Collection<Unit>> whereFrom = battleTracker.getFinishedBattlesUnitAttackFromMap().get(neighbor);
                            if (whereFrom != null && whereFrom.containsKey(t)) {
                                amphib = true;
                                break;
                            }
                            continue;
                        }
                        if (battle.isAmphibious() && ((battle instanceof MustFightBattle && ((MustFightBattle) battle).getAmphibiousAttackTerritories().contains(t)) || (battle instanceof NonFightingBattle && ((NonFightingBattle) battle).getAmphibiousAttackTerritories().contains(t)))) {
                            amphib = true;
                            break;
                        }
                    }
                    if (!amphib) {
                        continue;
                    }
                }
            }
            final Collection<Territory> currentTerrs = kamikazeZonesByEnemy.getOrDefault(owner, new ArrayList<>());
            currentTerrs.add(t);
            kamikazeZonesByEnemy.put(owner, currentTerrs);
        }
    }
    if (kamikazeZonesByEnemy.isEmpty()) {
        return;
    }
    for (final Entry<PlayerID, Collection<Territory>> entry : kamikazeZonesByEnemy.entrySet()) {
        final PlayerID currentEnemy = entry.getKey();
        final PlayerAttachment pa = PlayerAttachment.get(currentEnemy);
        if (pa == null) {
            continue;
        }
        Predicate<Unit> canBeAttacked = canBeAttackedDefault;
        final Set<UnitType> suicideAttackTargets = pa.getSuicideAttackTargets();
        if (suicideAttackTargets != null) {
            canBeAttacked = Matches.unitIsOwnedBy(player).and(Matches.unitIsOfTypes(suicideAttackTargets));
        }
        // See if the player has any attack tokens
        final IntegerMap<Resource> resourcesAndAttackValues = pa.getSuicideAttackResources();
        if (resourcesAndAttackValues.size() <= 0) {
            continue;
        }
        final IntegerMap<Resource> playerResourceCollection = currentEnemy.getResources().getResourcesCopy();
        final IntegerMap<Resource> attackTokens = new IntegerMap<>();
        for (final Resource possible : resourcesAndAttackValues.keySet()) {
            final int amount = playerResourceCollection.getInt(possible);
            if (amount > 0) {
                attackTokens.put(possible, amount);
            }
        }
        if (attackTokens.size() <= 0) {
            continue;
        }
        // now let the enemy decide if they will do attacks
        final Collection<Territory> kamikazeZones = entry.getValue();
        final HashMap<Territory, Collection<Unit>> possibleUnitsToAttack = new HashMap<>();
        for (final Territory t : kamikazeZones) {
            final List<Unit> validTargets = t.getUnits().getMatches(canBeAttacked);
            if (!validTargets.isEmpty()) {
                possibleUnitsToAttack.put(t, validTargets);
            }
        }
        final Map<Territory, HashMap<Unit, IntegerMap<Resource>>> attacks = getRemotePlayer(currentEnemy).selectKamikazeSuicideAttacks(possibleUnitsToAttack);
        if (attacks == null || attacks.isEmpty()) {
            continue;
        }
        // now validate that we have the resources and those units are valid targets
        for (final Entry<Territory, HashMap<Unit, IntegerMap<Resource>>> territoryEntry : attacks.entrySet()) {
            final Territory t = territoryEntry.getKey();
            final Collection<Unit> possibleUnits = possibleUnitsToAttack.get(t);
            if (possibleUnits == null || !possibleUnits.containsAll(territoryEntry.getValue().keySet())) {
                throw new IllegalStateException("Player has chosen illegal units during Kamikaze Suicide Attacks");
            }
            for (final IntegerMap<Resource> resourceMap : territoryEntry.getValue().values()) {
                attackTokens.subtract(resourceMap);
            }
        }
        if (!attackTokens.isPositive()) {
            throw new IllegalStateException("Player has chosen illegal resource during Kamikaze Suicide Attacks");
        }
        for (final Entry<Territory, HashMap<Unit, IntegerMap<Resource>>> territoryEntry : attacks.entrySet()) {
            final Territory location = territoryEntry.getKey();
            for (final Entry<Unit, IntegerMap<Resource>> unitEntry : territoryEntry.getValue().entrySet()) {
                final Unit unitUnderFire = unitEntry.getKey();
                final IntegerMap<Resource> numberOfAttacks = unitEntry.getValue();
                if (numberOfAttacks != null && numberOfAttacks.size() > 0 && numberOfAttacks.totalValues() > 0) {
                    fireKamikazeSuicideAttacks(unitUnderFire, numberOfAttacks, resourcesAndAttackValues, currentEnemy, location);
                }
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) HashMap(java.util.HashMap) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) UnitType(games.strategy.engine.data.UnitType) IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) Resource(games.strategy.engine.data.Resource) Collection(java.util.Collection) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 4 with Territory

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

the class BattleDelegate method resetMaxScrambleCount.

private static void resetMaxScrambleCount(final IDelegateBridge bridge) {
    // reset the tripleaUnit property for all airbases that were used
    final GameData data = bridge.getData();
    if (!Properties.getScrambleRulesInEffect(data)) {
        return;
    }
    final CompositeChange change = new CompositeChange();
    for (final Territory t : data.getMap().getTerritories()) {
        final Collection<Unit> airbases = t.getUnits().getMatches(Matches.unitIsAirBase());
        for (final Unit u : airbases) {
            final UnitAttachment ua = UnitAttachment.get(u.getType());
            final int currentMax = ((TripleAUnit) u).getMaxScrambleCount();
            final int allowedMax = ua.getMaxScrambleCount();
            if (currentMax != allowedMax) {
                change.add(ChangeFactory.unitPropertyChange(u, allowedMax, TripleAUnit.MAX_SCRAMBLE_COUNT));
            }
        }
    }
    if (!change.isEmpty()) {
        bridge.getHistoryWriter().startEvent("Preparing Airbases for Possible Scrambling");
        bridge.addChange(change);
    }
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Example 5 with Territory

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

the class BattleDelegate method whereCanAirLand.

private static Collection<Territory> whereCanAirLand(final Collection<Unit> strandedAir, final Territory currentTerr, final PlayerID alliedPlayer, final GameData data, final BattleTracker battleTracker, final int carrierCostForCurrentTerr, final int allowedMovement, final boolean useMaxScrambleDistance) {
    int maxDistance = allowedMovement;
    if ((maxDistance > 1) || useMaxScrambleDistance) {
        UnitType ut = null;
        for (final Unit u : strandedAir) {
            if (ut == null) {
                ut = u.getType();
            } else if (!ut.equals(u.getType())) {
                throw new IllegalStateException("whereCanAirLand can only accept 1 UnitType if byMovementCost or scrambled is true");
            }
        }
        if (useMaxScrambleDistance) {
            maxDistance = UnitAttachment.get(ut).getMaxScrambleDistance();
        }
    }
    if (maxDistance < 1 || strandedAir == null || strandedAir.isEmpty()) {
        return Collections.singletonList(currentTerr);
    }
    final boolean areNeutralsPassableByAir = (Properties.getNeutralFlyoverAllowed(data) && !Properties.getNeutralsImpassable(data));
    final HashSet<Territory> canNotLand = new HashSet<>();
    canNotLand.addAll(battleTracker.getPendingBattleSites(false));
    canNotLand.addAll(CollectionUtils.getMatches(data.getMap().getTerritories(), Matches.territoryHasEnemyUnits(alliedPlayer, data)));
    final Collection<Territory> possibleTerrs = new ArrayList<>(data.getMap().getNeighbors(currentTerr, maxDistance));
    if (maxDistance > 1) {
        final Iterator<Territory> possibleIter = possibleTerrs.iterator();
        while (possibleIter.hasNext()) {
            final Route route = data.getMap().getRoute(currentTerr, possibleIter.next(), Matches.airCanFlyOver(alliedPlayer, data, areNeutralsPassableByAir));
            if (route == null || route.getMovementCost(strandedAir.iterator().next()) > maxDistance) {
                possibleIter.remove();
            }
        }
    }
    possibleTerrs.add(currentTerr);
    final HashSet<Territory> availableLand = new HashSet<>(CollectionUtils.getMatches(possibleTerrs, Matches.isTerritoryAllied(alliedPlayer, data).and(Matches.territoryIsLand())));
    availableLand.removeAll(canNotLand);
    final HashSet<Territory> whereCanLand = new HashSet<>(availableLand);
    // now for carrier-air-landing validation
    if (!strandedAir.isEmpty() && strandedAir.stream().allMatch(Matches.unitCanLandOnCarrier())) {
        final HashSet<Territory> availableWater = new HashSet<>(CollectionUtils.getMatches(possibleTerrs, Matches.territoryHasUnitsThatMatch(Matches.unitIsAlliedCarrier(alliedPlayer, data)).and(Matches.territoryIsWater())));
        availableWater.removeAll(battleTracker.getPendingBattleSites(false));
        // a rather simple calculation, either we can take all the air, or we can't, nothing in the middle
        final int carrierCost = AirMovementValidator.carrierCost(strandedAir);
        final Iterator<Territory> waterIter = availableWater.iterator();
        while (waterIter.hasNext()) {
            final Territory t = waterIter.next();
            int carrierCapacity = AirMovementValidator.carrierCapacity(t.getUnits().getMatches(Matches.unitIsAlliedCarrier(alliedPlayer, data)), t);
            if (!t.equals(currentTerr)) {
                carrierCapacity -= AirMovementValidator.carrierCost(t.getUnits().getMatches(Matches.unitCanLandOnCarrier().and(Matches.alliedUnit(alliedPlayer, data))));
            } else {
                carrierCapacity -= carrierCostForCurrentTerr;
            }
            if (carrierCapacity < carrierCost) {
                waterIter.remove();
            }
        }
        whereCanLand.addAll(availableWater);
    }
    return whereCanLand;
}
Also used : Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route) HashSet(java.util.HashSet)

Aggregations

Territory (games.strategy.engine.data.Territory)420 Unit (games.strategy.engine.data.Unit)254 TripleAUnit (games.strategy.triplea.TripleAUnit)195 PlayerID (games.strategy.engine.data.PlayerID)164 ArrayList (java.util.ArrayList)160 Test (org.junit.jupiter.api.Test)140 Route (games.strategy.engine.data.Route)137 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)118 GameData (games.strategy.engine.data.GameData)94 HashSet (java.util.HashSet)87 UnitType (games.strategy.engine.data.UnitType)65 HashMap (java.util.HashMap)65 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)50 Collection (java.util.Collection)47 IntegerMap (games.strategy.util.IntegerMap)45 Set (java.util.Set)41 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)39 TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)37 List (java.util.List)36 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)34