use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.
the class EndTurnDelegate method getResourceProduction.
/**
* Since territory resource may contain any resource except PUs (PUs use "getProduction" instead),
* we will now figure out the total production of non-PUs resources.
*/
public static IntegerMap<Resource> getResourceProduction(final Collection<Territory> territories, final GameData data) {
final IntegerMap<Resource> resources = new IntegerMap<>();
for (final Territory current : territories) {
final TerritoryAttachment attachment = TerritoryAttachment.get(current);
if (attachment == null) {
throw new IllegalStateException("No attachment for owned territory:" + current.getName());
}
final ResourceCollection toAdd = attachment.getResources();
if (toAdd == null) {
continue;
}
// Match will check if territory is originally owned convoy center, or if contested
if (Matches.territoryCanCollectIncomeFrom(current.getOwner(), data).test(current)) {
resources.add(toAdd.getResourcesCopy());
}
}
return resources;
}
use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.
the class EndRoundDelegate method checkVictoryCities.
private void checkVictoryCities(final IDelegateBridge bridge, final String victoryMessage, final String victoryType) {
final GameData data = bridge.getData();
final Collection<Territory> territories = data.getMap().getTerritories();
for (final String allianceName : data.getAllianceTracker().getAlliances()) {
final int vcAmount = getVcAmount(data, allianceName, victoryType);
final Set<PlayerID> teamMembers = data.getAllianceTracker().getPlayersInAlliance(allianceName);
int teamVCs = 0;
for (final Territory t : territories) {
if (Matches.isTerritoryOwnedBy(teamMembers).test(t)) {
final TerritoryAttachment ta = TerritoryAttachment.get(t);
if (ta != null) {
teamVCs += ta.getVictoryCity();
}
}
}
if (teamVCs >= vcAmount) {
bridge.getHistoryWriter().startEvent(allianceName + victoryMessage + vcAmount + " Victory Cities!");
final Collection<PlayerID> winners = data.getAllianceTracker().getPlayersInAlliance(allianceName);
// Added this to end the game on victory conditions
signalGameOver(allianceName + victoryMessage + vcAmount + " Victory Cities!", winners, bridge);
}
}
}
use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.
the class InitializationDelegate method initOriginalOwner.
private static void initOriginalOwner(final IDelegateBridge bridge) {
final GameData data = bridge.getData();
final CompositeChange changes = new CompositeChange();
for (final Territory current : data.getMap()) {
if (!current.getOwner().isNull()) {
final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
if (territoryAttachment == null) {
throw new IllegalStateException("No territory attachment for " + current);
}
if (territoryAttachment.getOriginalOwner() == null && current.getOwner() != null) {
changes.add(OriginalOwnerTracker.addOriginalOwnerChange(current, current.getOwner()));
}
final Collection<Unit> factoryAndInfrastructure = current.getUnits().getMatches(Matches.unitIsInfrastructure());
changes.add(OriginalOwnerTracker.addOriginalOwnerChange(factoryAndInfrastructure, current.getOwner()));
} else if (!current.isWater()) {
final TerritoryAttachment territoryAttachment = TerritoryAttachment.get(current);
if (territoryAttachment == null) {
throw new IllegalStateException("No territory attachment for " + current);
}
}
}
bridge.getHistoryWriter().startEvent("Adding original owners");
bridge.addChange(changes);
}
use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.
the class GameDataExporter method printAttachmentOptionsBasedOnOriginalXml.
private static String printAttachmentOptionsBasedOnOriginalXml(final ArrayList<Tuple<String, String>> attachmentPlusValues, final IAttachment attachment) {
if (attachmentPlusValues.isEmpty()) {
return "";
}
final StringBuilder sb = new StringBuilder();
boolean alreadyHasOccupiedTerrOf = false;
for (final Tuple<String, String> current : attachmentPlusValues) {
sb.append(" <option name=\"").append(current.getFirst()).append("\" value=\"").append(current.getSecond()).append("\"/>\n");
if (current.getFirst().equals("occupiedTerrOf")) {
alreadyHasOccupiedTerrOf = true;
}
}
// add occupiedTerrOf until we fix engine to only use originalOwner
if (!alreadyHasOccupiedTerrOf && attachment instanceof TerritoryAttachment) {
final TerritoryAttachment ta = (TerritoryAttachment) attachment;
if (ta.getOriginalOwner() != null) {
sb.append(" <option name=\"occupiedTerrOf\" value=\"").append(ta.getOriginalOwner().getName()).append("\"/>\n");
}
}
return sb.toString();
}
use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.
the class ProTerritoryValueUtils method findEnemyCapitalsAndFactoriesValue.
private static Map<Territory, Double> findEnemyCapitalsAndFactoriesValue(final PlayerID player, final int maxLandMassSize, final List<Territory> territoriesThatCantBeHeld, final List<Territory> territoriesToAttack) {
// Get all enemy factories and capitals (check if most territories have factories and if so remove them)
final GameData data = ProData.getData();
final List<Territory> allTerritories = data.getMap().getTerritories();
final Set<Territory> enemyCapitalsAndFactories = new HashSet<>(CollectionUtils.getMatches(allTerritories, ProMatches.territoryHasInfraFactoryAndIsOwnedByPlayersOrCantBeHeld(player, ProUtils.getPotentialEnemyPlayers(player), territoriesThatCantBeHeld)));
final int numPotentialEnemyTerritories = CollectionUtils.countMatches(allTerritories, Matches.isTerritoryOwnedBy(ProUtils.getPotentialEnemyPlayers(player)));
if (enemyCapitalsAndFactories.size() * 2 >= numPotentialEnemyTerritories) {
enemyCapitalsAndFactories.clear();
}
enemyCapitalsAndFactories.addAll(ProUtils.getLiveEnemyCapitals(data, player));
enemyCapitalsAndFactories.removeAll(territoriesToAttack);
// Find value for each enemy capital and factory
final Map<Territory, Double> enemyCapitalsAndFactoriesMap = new HashMap<>();
for (final Territory t : enemyCapitalsAndFactories) {
// Get factory production if factory
int factoryProduction = 0;
if (ProMatches.territoryHasInfraFactoryAndIsLand().test(t)) {
factoryProduction = TerritoryAttachment.getProduction(t);
}
// Get player production if capital
double playerProduction = 0;
final TerritoryAttachment ta = TerritoryAttachment.get(t);
if (ta != null && ta.isCapital()) {
playerProduction = ProUtils.getPlayerProduction(t.getOwner(), data);
}
// Calculate value
final int isNeutral = t.getOwner().isNull() ? 1 : 0;
final int landMassSize = 1 + data.getMap().getNeighbors(t, 6, ProMatches.territoryCanPotentiallyMoveLandUnits(player, data)).size();
final double value = Math.sqrt(factoryProduction + Math.sqrt(playerProduction)) * 32 / (1 + 3 * isNeutral) * landMassSize / maxLandMassSize;
enemyCapitalsAndFactoriesMap.put(t, value);
}
return enemyCapitalsAndFactoriesMap;
}
Aggregations