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);
}
}
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;
}
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);
}
}
}
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);
}
}
}
}
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;
}
Aggregations