use of games.strategy.engine.data.Resource 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.Resource 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();
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class EndTurnDelegate method findNationalObjectiveAndTriggerResources.
/**
* Find the resources generated by national objectives and triggers that are currently met.
*/
public static IntegerMap<Resource> findNationalObjectiveAndTriggerResources(final PlayerID player, final GameData data) {
final IDelegateBridge bridge = new ObjectiveDummyDelegateBridge(data);
// Find and test all the conditions for triggers and national objectives
final Set<TriggerAttachment> triggers = new HashSet<>();
final List<RulesAttachment> objectives = new ArrayList<>();
final HashMap<ICondition, Boolean> testedConditions = testNationalObjectivesAndTriggers(player, data, bridge, triggers, objectives);
// Find triggers value
final IntegerMap<Resource> resources;
final boolean useTriggers = Properties.getTriggers(data);
if (useTriggers && !triggers.isEmpty()) {
final Set<TriggerAttachment> toFireTestedAndSatisfied = new HashSet<>(CollectionUtils.getMatches(triggers, AbstractTriggerAttachment.isSatisfiedMatch(testedConditions)));
resources = TriggerAttachment.findResourceIncome(toFireTestedAndSatisfied, bridge);
} else {
resources = new IntegerMap<>();
}
// Find national objectives value
int pus = 0;
for (final RulesAttachment rule : objectives) {
final int uses = rule.getUses();
if (uses == 0 || !rule.isSatisfied(testedConditions)) {
continue;
}
pus += (rule.getObjectiveValue() * rule.getEachMultiple() * Properties.getPuMultiplier(data));
}
resources.add(data.getResourceList().getResource(Constants.PUS), pus);
return resources;
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class EditDelegate method changePUs.
@Override
public String changePUs(final PlayerID player, final int newTotal) {
final String result = checkEditMode();
if (result != null) {
return result;
}
final Resource pus = getData().getResourceList().getResource(Constants.PUS);
final int oldTotal = player.getResources().getQuantity(pus);
if (oldTotal == newTotal) {
return "New PUs total is unchanged";
}
if (newTotal < 0) {
return "New PUs total is invalid";
}
logEvent("Changing PUs for " + player.getName() + " from " + oldTotal + " to " + newTotal, null);
bridge.addChange(ChangeFactory.changeResourcesChange(player, pus, (newTotal - oldTotal)));
return null;
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class GameDataExporter method repairRules.
private void repairRules(final GameData data) {
for (final RepairRule rr : data.getRepairRuleList().getRepairRules()) {
xmlfile.append(" <repairRule name=\"").append(rr.getName()).append("\">\n");
for (final Resource cost : rr.getCosts().keySet()) {
xmlfile.append(" <cost resource=\"").append(cost.getName()).append("\" quantity=\"").append(rr.getCosts().getInt(cost)).append("\"/>\n");
}
for (final NamedAttachable result : rr.getResults().keySet()) {
xmlfile.append(" <result resourceOrUnit=\"").append(result.getName()).append("\" quantity=\"").append(rr.getResults().getInt(result)).append("\"/>\n");
}
xmlfile.append(" </repairRule>\n");
}
}
Aggregations