use of games.strategy.engine.data.Territory in project triplea by triplea-game.
the class AbstractEndTurnDelegate method findEstimatedIncome.
/**
* Find estimated income for given player. This only takes into account income from territories,
* units, NOs, and triggers. It ignores blockades, war bonds, relationship upkeep, and bonus income.
*/
public static IntegerMap<Resource> findEstimatedIncome(final PlayerID player, final GameData data) {
final IntegerMap<Resource> resources = new IntegerMap<>();
// Only add territory resources if endTurn not endTurnNoPU
for (GameStep step : data.getSequence()) {
if (player.equals(step.getPlayerId()) && step.getDelegate().getName().equals("endTurn")) {
final List<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
final int pusFromTerritories = getProduction(territories, data) * Properties.getPuMultiplier(data);
resources.add(new Resource(Constants.PUS, data), pusFromTerritories);
resources.add(EndTurnDelegate.getResourceProduction(territories, data));
}
}
// Add unit generated resources, NOs, and triggers
resources.add(EndTurnDelegate.findUnitCreatedResources(player, data));
resources.add(EndTurnDelegate.findNationalObjectiveAndTriggerResources(player, data));
return resources;
}
use of games.strategy.engine.data.Territory in project triplea by triplea-game.
the class EndTurnDelegate method createUnits.
private String createUnits(final IDelegateBridge bridge) {
final StringBuilder endTurnReport = new StringBuilder();
final GameData data = getData();
final PlayerID player = data.getSequence().getStep().getPlayerId();
final Predicate<Unit> myCreatorsMatch = Matches.unitIsOwnedBy(player).and(Matches.unitCreatesUnits());
final CompositeChange change = new CompositeChange();
for (final Territory t : data.getMap().getTerritories()) {
final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits().getUnits(), myCreatorsMatch);
if (myCreators != null && !myCreators.isEmpty()) {
final Collection<Unit> toAdd = new ArrayList<>();
final Collection<Unit> toAddSea = new ArrayList<>();
final Collection<Unit> toAddLand = new ArrayList<>();
for (final Unit u : myCreators) {
final UnitAttachment ua = UnitAttachment.get(u.getType());
final IntegerMap<UnitType> createsUnitsMap = ua.getCreatesUnitsList();
final Collection<UnitType> willBeCreated = createsUnitsMap.keySet();
for (final UnitType ut : willBeCreated) {
if (UnitAttachment.get(ut).getIsSea() && Matches.territoryIsLand().test(t)) {
toAddSea.addAll(ut.create(createsUnitsMap.getInt(ut), player));
} else if (!UnitAttachment.get(ut).getIsSea() && !UnitAttachment.get(ut).getIsAir() && Matches.territoryIsWater().test(t)) {
toAddLand.addAll(ut.create(createsUnitsMap.getInt(ut), player));
} else {
toAdd.addAll(ut.create(createsUnitsMap.getInt(ut), player));
}
}
}
if (!toAdd.isEmpty()) {
final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAdd) + " in " + t.getName();
bridge.getHistoryWriter().startEvent(transcriptText, toAdd);
endTurnReport.append(transcriptText).append("<br />");
final Change place = ChangeFactory.addUnits(t, toAdd);
change.add(place);
}
if (!toAddSea.isEmpty()) {
final Predicate<Territory> myTerrs = Matches.territoryIsWater();
final Collection<Territory> waterNeighbors = data.getMap().getNeighbors(t, myTerrs);
if (waterNeighbors != null && !waterNeighbors.isEmpty()) {
final Territory tw = getRandomTerritory(waterNeighbors, bridge);
final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAddSea) + " in " + tw.getName();
bridge.getHistoryWriter().startEvent(transcriptText, toAddSea);
endTurnReport.append(transcriptText).append("<br />");
final Change place = ChangeFactory.addUnits(tw, toAddSea);
change.add(place);
}
}
if (!toAddLand.isEmpty()) {
final Predicate<Territory> myTerrs = Matches.isTerritoryOwnedBy(player).and(Matches.territoryIsLand());
final Collection<Territory> landNeighbors = data.getMap().getNeighbors(t, myTerrs);
if (landNeighbors != null && !landNeighbors.isEmpty()) {
final Territory tl = getRandomTerritory(landNeighbors, bridge);
final String transcriptText = player.getName() + " creates " + MyFormatter.unitsToTextNoOwner(toAddLand) + " in " + tl.getName();
bridge.getHistoryWriter().startEvent(transcriptText, toAddLand);
endTurnReport.append(transcriptText).append("<br />");
final Change place = ChangeFactory.addUnits(tl, toAddLand);
change.add(place);
}
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
return endTurnReport.toString();
}
use of games.strategy.engine.data.Territory in project triplea by triplea-game.
the class EndTurnDelegate method findUnitCreatedResources.
/**
* Find all of the resources that will be created by units on the map.
*/
public static IntegerMap<Resource> findUnitCreatedResources(final PlayerID player, final GameData data) {
final IntegerMap<Resource> resourceTotalsMap = new IntegerMap<>();
final Predicate<Unit> myCreatorsMatch = Matches.unitIsOwnedBy(player).and(Matches.unitCreatesResources());
for (final Territory t : data.getMap().getTerritories()) {
final Collection<Unit> myCreators = CollectionUtils.getMatches(t.getUnits().getUnits(), myCreatorsMatch);
for (final Unit unit : myCreators) {
final IntegerMap<Resource> generatedResourcesMap = UnitAttachment.get(unit.getType()).getCreatesResourcesList();
resourceTotalsMap.add(generatedResourcesMap);
}
}
final Resource pus = new Resource(Constants.PUS, data);
if (resourceTotalsMap.containsKey(pus)) {
resourceTotalsMap.put(pus, resourceTotalsMap.getInt(pus) * Properties.getPuMultiplier(data));
}
return resourceTotalsMap;
}
use of games.strategy.engine.data.Territory 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.engine.data.Territory in project triplea by triplea-game.
the class EndTurnDelegate method addOtherResources.
@Override
protected String addOtherResources(final IDelegateBridge bridge) {
final StringBuilder endTurnReport = new StringBuilder();
final GameData data = bridge.getData();
final CompositeChange change = new CompositeChange();
final Collection<Territory> territories = data.getMap().getTerritoriesOwnedBy(player);
final IntegerMap<Resource> production = getResourceProduction(territories, data);
for (final Entry<Resource, Integer> resource : production.entrySet()) {
final Resource r = resource.getKey();
int toAdd = resource.getValue();
int total = player.getResources().getQuantity(r) + toAdd;
if (total < 0) {
toAdd -= total;
total = 0;
}
final String resourceText = player.getName() + " collects " + toAdd + " " + MyFormatter.pluralize(r.getName(), toAdd) + "; ends with " + total + " " + MyFormatter.pluralize(r.getName(), total) + " total";
bridge.getHistoryWriter().startEvent(resourceText);
endTurnReport.append(resourceText).append("<br />");
change.add(ChangeFactory.changeResourcesChange(player, r, toAdd));
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
return endTurnReport.toString();
}
Aggregations