Search in sources :

Example 26 with PlayerID

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

the class MustFightBattle method defenderWins.

private void defenderWins(final IDelegateBridge bridge) {
    m_whoWon = WhoWon.DEFENDER;
    getDisplay(bridge).battleEnd(m_battleID, m_defender.getName() + " win");
    if (Properties.getAbandonedTerritoriesMayBeTakenOverImmediately(m_data)) {
        if (CollectionUtils.getMatches(m_defendingUnits, Matches.unitIsNotInfrastructure()).size() == 0) {
            final List<Unit> allyOfAttackerUnits = m_battleSite.getUnits().getMatches(Matches.unitIsNotInfrastructure());
            if (!allyOfAttackerUnits.isEmpty()) {
                final PlayerID abandonedToPlayer = AbstractBattle.findPlayerWithMostUnits(allyOfAttackerUnits);
                bridge.getHistoryWriter().addChildToEvent(abandonedToPlayer.getName() + " takes over " + m_battleSite.getName() + " as there are no defenders left", allyOfAttackerUnits);
                // should we create a new battle records to show the ally capturing the territory (in the case where they
                // didn't already own/allied it)?
                m_battleTracker.takeOver(m_battleSite, abandonedToPlayer, bridge, null, allyOfAttackerUnits);
            }
        } else {
            // should we create a new battle records to show the defender capturing the territory (in the case where they
            // didn't already own/allied it)?
            m_battleTracker.takeOver(m_battleSite, m_defender, bridge, null, m_defendingUnits);
        }
    }
    bridge.getHistoryWriter().addChildToEvent(m_defender.getName() + " win", new ArrayList<>(m_defendingUnits));
    m_battleResultDescription = BattleRecord.BattleResultDescription.LOST;
    showCasualties(bridge);
    if (!m_headless) {
        m_battleTracker.getBattleRecords().addResultToBattle(m_attacker, m_battleID, m_defender, m_attackerLostTUV, m_defenderLostTUV, m_battleResultDescription, new BattleResults(this, m_data));
    }
    checkDefendingPlanesCanLand();
    BattleTracker.captureOrDestroyUnits(m_battleSite, m_defender, m_defender, bridge, null);
    if (!m_headless) {
        bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_FAILURE, m_attacker);
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) BattleResults(games.strategy.triplea.oddsCalculator.ta.BattleResults) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 27 with PlayerID

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

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

the class MustFightBattle method queryRetreat.

private void queryRetreat(final boolean defender, final RetreatType retreatType, final IDelegateBridge bridge, Collection<Territory> availableTerritories) {
    final boolean planes = retreatType == RetreatType.PLANES;
    final boolean subs = retreatType == RetreatType.SUBS;
    final boolean canSubsSubmerge = canSubsSubmerge();
    final boolean canDefendingSubsSubmergeOrRetreat = subs && defender && Properties.getSubmarinesDefendingMaySubmergeOrRetreat(m_data);
    final boolean partialAmphib = retreatType == RetreatType.PARTIAL_AMPHIB;
    final boolean submerge = subs && canSubsSubmerge;
    if (availableTerritories.isEmpty() && !(submerge || canDefendingSubsSubmergeOrRetreat)) {
        return;
    }
    // If attacker then add all owned units at battle site as some might have been removed from battle (infra)
    Collection<Unit> units = defender ? m_defendingUnits : m_attackingUnits;
    if (!defender) {
        units = new HashSet<>(units);
        units.addAll(m_battleSite.getUnits().getMatches(Matches.unitIsOwnedBy(m_attacker)));
        units.removeAll(m_killed);
    }
    if (subs) {
        units = CollectionUtils.getMatches(units, Matches.unitIsSub());
    } else if (planes) {
        units = CollectionUtils.getMatches(units, Matches.unitIsAir());
    } else if (partialAmphib) {
        units = CollectionUtils.getMatches(units, Matches.unitWasNotAmphibious());
    }
    if (units.stream().anyMatch(Matches.unitIsSea())) {
        availableTerritories = CollectionUtils.getMatches(availableTerritories, Matches.territoryIsWater());
    }
    if (canDefendingSubsSubmergeOrRetreat) {
        availableTerritories.add(m_battleSite);
    } else if (submerge) {
        availableTerritories.clear();
        availableTerritories.add(m_battleSite);
    }
    if (planes) {
        availableTerritories.clear();
        availableTerritories.add(m_battleSite);
    }
    if (units.size() == 0) {
        return;
    }
    final PlayerID retreatingPlayer = defender ? m_defender : m_attacker;
    final String text;
    if (subs) {
        text = retreatingPlayer.getName() + " retreat subs?";
    } else if (planes) {
        text = retreatingPlayer.getName() + " retreat planes?";
    } else if (partialAmphib) {
        text = retreatingPlayer.getName() + " retreat non-amphibious units?";
    } else {
        text = retreatingPlayer.getName() + " retreat?";
    }
    final String step;
    if (defender) {
        step = m_defender.getName() + (canSubsSubmerge ? SUBS_SUBMERGE : SUBS_WITHDRAW);
    } else {
        if (subs) {
            step = m_attacker.getName() + (canSubsSubmerge ? SUBS_SUBMERGE : SUBS_WITHDRAW);
        } else {
            step = m_attacker.getName() + ATTACKER_WITHDRAW;
        }
    }
    getDisplay(bridge).gotoBattleStep(m_battleID, step);
    final Territory retreatTo = getRemote(retreatingPlayer, bridge).retreatQuery(m_battleID, (submerge || canDefendingSubsSubmergeOrRetreat), m_battleSite, availableTerritories, text);
    if (retreatTo != null && !availableTerritories.contains(retreatTo) && !subs) {
        System.err.println("Invalid retreat selection :" + retreatTo + " not in " + MyFormatter.defaultNamedToTextList(availableTerritories));
        Thread.dumpStack();
        return;
    }
    if (retreatTo != null) {
        // if attacker retreating non subs then its all over
        if (!defender && !subs && !planes && !partialAmphib) {
            // this is illegal in ww2v2 revised and beyond (the fighters should die). still checking if illegal in classic.
            m_isOver = true;
        }
        if (subs && m_battleSite.equals(retreatTo) && (submerge || canDefendingSubsSubmergeOrRetreat)) {
            if (!m_headless) {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_SUBMERGE, m_attacker);
            }
            submergeUnits(units, defender, bridge);
            final String messageShort = retreatingPlayer.getName() + " submerges subs";
            getDisplay(bridge).notifyRetreat(messageShort, messageShort, step, retreatingPlayer);
        } else if (planes) {
            if (!m_headless) {
                bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_AIR, m_attacker);
            }
            retreatPlanes(units, defender, bridge);
            final String messageShort = retreatingPlayer.getName() + " retreats planes";
            getDisplay(bridge).notifyRetreat(messageShort, messageShort, step, retreatingPlayer);
        } else if (partialAmphib) {
            if (!m_headless) {
                if (units.stream().anyMatch(Matches.unitIsSea())) {
                    bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_SEA, m_attacker);
                } else if (units.stream().anyMatch(Matches.unitIsLand())) {
                    bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_LAND, m_attacker);
                } else {
                    bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_AIR, m_attacker);
                }
            }
            // remove amphib units from those retreating
            units = CollectionUtils.getMatches(units, Matches.unitWasNotAmphibious());
            retreatUnitsAndPlanes(units, retreatTo, defender, bridge);
            final String messageShort = retreatingPlayer.getName() + " retreats non-amphibious units";
            getDisplay(bridge).notifyRetreat(messageShort, messageShort, step, retreatingPlayer);
        } else {
            if (!m_headless) {
                if (units.stream().anyMatch(Matches.unitIsSea())) {
                    bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_SEA, m_attacker);
                } else if (units.stream().anyMatch(Matches.unitIsLand())) {
                    bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_LAND, m_attacker);
                } else {
                    bridge.getSoundChannelBroadcaster().playSoundForAll(SoundPath.CLIP_BATTLE_RETREAT_AIR, m_attacker);
                }
            }
            retreatUnits(units, retreatTo, defender, bridge);
            final String messageShort = retreatingPlayer.getName() + " retreats";
            final String messageLong;
            if (subs) {
                messageLong = retreatingPlayer.getName() + " retreats subs to " + retreatTo.getName();
            } else if (planes) {
                messageLong = retreatingPlayer.getName() + " retreats planes to " + retreatTo.getName();
            } else if (partialAmphib) {
                messageLong = retreatingPlayer.getName() + " retreats non-amphibious units to " + retreatTo.getName();
            } else {
                messageLong = retreatingPlayer.getName() + " retreats all units to " + retreatTo.getName();
            }
            getDisplay(bridge).notifyRetreat(messageShort, messageLong, step, retreatingPlayer);
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 29 with PlayerID

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

the class PoliticsDelegate method chainAlliancesTogether.

static void chainAlliancesTogether(final IDelegateBridge bridge) {
    final GameData data = bridge.getData();
    if (!Properties.getAlliancesCanChainTogether(data)) {
        return;
    }
    final Collection<RelationshipType> allTypes = data.getRelationshipTypeList().getAllRelationshipTypes();
    RelationshipType alliedType = null;
    RelationshipType warType = null;
    for (final RelationshipType type : allTypes) {
        if (type.getRelationshipTypeAttachment().isDefaultWarPosition()) {
            warType = type;
        } else if (type.getRelationshipTypeAttachment().canAlliancesChainTogether()) {
            alliedType = type;
        }
    }
    if (alliedType == null) {
        return;
    }
    // first do alliances. then, do war (since we don't want to declare war on a potential ally).
    final Collection<PlayerID> players = data.getPlayerList().getPlayers();
    for (final PlayerID p1 : players) {
        final HashSet<PlayerID> p1NewAllies = new HashSet<>();
        final Collection<PlayerID> p1AlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p1, data));
        for (final PlayerID p2 : p1AlliedWith) {
            p1NewAllies.addAll(CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p2, data)));
        }
        p1NewAllies.removeAll(p1AlliedWith);
        p1NewAllies.remove(p1);
        for (final PlayerID p3 : p1NewAllies) {
            if (!data.getRelationshipTracker().getRelationshipType(p1, p3).equals(alliedType)) {
                final RelationshipType current = data.getRelationshipTracker().getRelationshipType(p1, p3);
                bridge.addChange(ChangeFactory.relationshipChange(p1, p3, current, alliedType));
                bridge.getHistoryWriter().addChildToEvent(p1.getName() + " and " + p3.getName() + " are joined together in an " + alliedType.getName() + " treaty");
                MoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(p1, p3, current, alliedType);
            }
        }
    }
    // now war
    if (warType == null) {
        return;
    }
    for (final PlayerID p1 : players) {
        final HashSet<PlayerID> p1NewWar = new HashSet<>();
        final Collection<PlayerID> p1WarWith = CollectionUtils.getMatches(players, Matches.isAtWar(p1, data));
        final Collection<PlayerID> p1AlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p1, data));
        for (final PlayerID p2 : p1AlliedWith) {
            p1NewWar.addAll(CollectionUtils.getMatches(players, Matches.isAtWar(p2, data)));
        }
        p1NewWar.removeAll(p1WarWith);
        p1NewWar.remove(p1);
        for (final PlayerID p3 : p1NewWar) {
            if (!data.getRelationshipTracker().getRelationshipType(p1, p3).equals(warType)) {
                final RelationshipType current = data.getRelationshipTracker().getRelationshipType(p1, p3);
                bridge.addChange(ChangeFactory.relationshipChange(p1, p3, current, warType));
                bridge.getHistoryWriter().addChildToEvent(p1.getName() + " and " + p3.getName() + " declare " + warType.getName() + " on each other");
                MoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(p1, p3, current, warType);
            }
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) RelationshipType(games.strategy.engine.data.RelationshipType) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 30 with PlayerID

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

the class PoliticsDelegate method actionIsAccepted.

/**
 * Get a list of players that should accept this action and then ask each
 * player if it accepts this action.
 *
 * @param paa
 *        the politicalActionAttachment that should be accepted
 */
private boolean actionIsAccepted(final PoliticalActionAttachment paa) {
    final GameData data = getData();
    final Predicate<PoliticalActionAttachment> intoAlliedChainOrIntoOrOutOfWar = Matches.politicalActionIsRelationshipChangeOf(null, Matches.relationshipTypeIsAlliedAndAlliancesCanChainTogether().negate(), Matches.relationshipTypeIsAlliedAndAlliancesCanChainTogether(), data).or(Matches.politicalActionIsRelationshipChangeOf(null, Matches.relationshipTypeIsAtWar().negate(), Matches.relationshipTypeIsAtWar(), data)).or(Matches.politicalActionIsRelationshipChangeOf(null, Matches.relationshipTypeIsAtWar(), Matches.relationshipTypeIsAtWar().negate(), data));
    if (!Properties.getAlliancesCanChainTogether(data) || !intoAlliedChainOrIntoOrOutOfWar.test(paa)) {
        for (final PlayerID player : paa.getActionAccept()) {
            if (!(getRemotePlayer(player)).acceptAction(this.player, PoliticsText.getInstance().getAcceptanceQuestion(paa.getText()), true)) {
                return false;
            }
        }
    } else {
        // if alliances chain together, then our allies must have a say in anyone becoming a new ally/enemy
        final LinkedHashSet<PlayerID> playersWhoNeedToAccept = new LinkedHashSet<>();
        playersWhoNeedToAccept.addAll(paa.getActionAccept());
        playersWhoNeedToAccept.addAll(CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedAndAlliancesCanChainTogether(player, data)));
        for (final PlayerID player : paa.getActionAccept()) {
            playersWhoNeedToAccept.addAll(CollectionUtils.getMatches(data.getPlayerList().getPlayers(), Matches.isAlliedAndAlliancesCanChainTogether(player, data)));
        }
        playersWhoNeedToAccept.removeAll(paa.getActionAccept());
        for (final PlayerID player : playersWhoNeedToAccept) {
            String actionText = PoliticsText.getInstance().getAcceptanceQuestion(paa.getText());
            if (actionText.equals("NONE")) {
                actionText = this.player.getName() + " wants to take the following action: " + MyFormatter.attachmentNameToText(paa.getName()) + " \r\n Do you approve?";
            } else {
                actionText = this.player.getName() + " wants to take the following action: " + MyFormatter.attachmentNameToText(paa.getName()) + ".  Do you approve? \r\n\r\n " + this.player.getName() + " will ask " + MyFormatter.defaultNamedToTextList(paa.getActionAccept()) + ", the following question: \r\n " + actionText;
            }
            if (!(getRemotePlayer(player)).acceptAction(this.player, actionText, true)) {
                return false;
            }
        }
        for (final PlayerID player : paa.getActionAccept()) {
            if (!(getRemotePlayer(player)).acceptAction(this.player, PoliticsText.getInstance().getAcceptanceQuestion(paa.getText()), true)) {
                return false;
            }
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PlayerID(games.strategy.engine.data.PlayerID) PoliticalActionAttachment(games.strategy.triplea.attachments.PoliticalActionAttachment) GameData(games.strategy.engine.data.GameData)

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