use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class GameDataExporter method resourceList.
private void resourceList(final GameData data) {
xmlfile.append("\n");
xmlfile.append(" <resourceList>\n");
for (final Resource resource : data.getResourceList().getResources()) {
xmlfile.append(" <resource name=\"").append(resource.getName()).append("\"/>\n");
}
xmlfile.append(" </resourceList>\n");
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class PoliticsDelegate method chargeForAction.
/**
* Subtract money from the players wallet
*
* @param paa
* the politicalactionattachment this the money is charged for.
*/
private void chargeForAction(final PoliticalActionAttachment paa) {
final Resource pus = getData().getResourceList().getResource(Constants.PUS);
final int cost = paa.getCostPu();
if (cost > 0) {
// don't notify user of spending money anymore
// notifyMoney(paa, true);
final String transcriptText = bridge.getPlayerId().getName() + " spend " + cost + " PU on Political Action: " + MyFormatter.attachmentNameToText(paa.getName());
bridge.getHistoryWriter().startEvent(transcriptText);
final Change charge = ChangeFactory.changeResourcesChange(bridge.getPlayerId(), pus, -cost);
bridge.addChange(charge);
} else {
final String transcriptText = bridge.getPlayerId().getName() + " takes Political Action: " + MyFormatter.attachmentNameToText(paa.getName());
// we must start an event anyway
bridge.getHistoryWriter().startEvent(transcriptText);
}
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class PurchaseDelegate method purchase.
@Override
public String purchase(final IntegerMap<ProductionRule> productionRules) {
final IntegerMap<Resource> costs = getCosts(productionRules);
final IntegerMap<NamedAttachable> results = getResults(productionRules);
if (!(canAfford(costs, player))) {
return NOT_ENOUGH_RESOURCES;
}
// check to see if player has too many of any building with a building limit
for (final NamedAttachable next : results.keySet()) {
if (!(next instanceof Resource)) {
final UnitType type = (UnitType) next;
final int quantity = results.getInt(type);
final UnitAttachment ua = UnitAttachment.get(type);
final int maxBuilt = ua.getMaxBuiltPerPlayer();
if (maxBuilt == 0) {
return "May not build any of this unit right now: " + type.getName();
} else if (maxBuilt > 0) {
// count how many units are yet to be placed or are in the field
int currentlyBuilt = player.getUnits().countMatches(Matches.unitIsOfType(type));
final Predicate<Unit> unitTypeOwnedBy = Matches.unitIsOfType(type).and(Matches.unitIsOwnedBy(player));
final Collection<Territory> allTerrs = getData().getMap().getTerritories();
for (final Territory t : allTerrs) {
currentlyBuilt += t.getUnits().countMatches(unitTypeOwnedBy);
}
final int allowedBuild = maxBuilt - currentlyBuilt;
if (allowedBuild - quantity < 0) {
return "May only build " + allowedBuild + " of " + type.getName() + " this turn, may only build " + maxBuilt + " total";
}
}
}
}
// remove first, since add logs PUs remaining
final Collection<Unit> totalUnits = new ArrayList<>();
final Collection<UnitType> totalUnitTypes = new ArrayList<>();
final Collection<Resource> totalResources = new ArrayList<>();
final CompositeChange changes = new CompositeChange();
// and find all added units
for (final NamedAttachable next : results.keySet()) {
if (next instanceof Resource) {
final Resource resource = (Resource) next;
final int quantity = results.getInt(resource);
final Change change = ChangeFactory.changeResourcesChange(player, resource, quantity);
changes.add(change);
for (int i = 0; i < quantity; i++) {
totalResources.add(resource);
}
} else {
final UnitType type = (UnitType) next;
final int quantity = results.getInt(type);
final Collection<Unit> units = type.create(quantity, player);
totalUnits.addAll(units);
for (int i = 0; i < quantity; i++) {
totalUnitTypes.add(type);
}
}
}
final Collection<NamedAttachable> totalAll = new ArrayList<>();
totalAll.addAll(totalUnitTypes);
totalAll.addAll(totalResources);
// add changes for added units
if (!totalUnits.isEmpty()) {
final Change change = ChangeFactory.addUnits(player, totalUnits);
changes.add(change);
}
// add changes for spent resources
final String remaining = removeFromPlayer(costs, changes);
// add history event
final String transcriptText;
if (!totalUnits.isEmpty()) {
transcriptText = player.getName() + " buy " + MyFormatter.defaultNamedToTextList(totalAll, ", ", true) + "; " + remaining;
} else {
transcriptText = player.getName() + " buy nothing; " + remaining;
}
bridge.getHistoryWriter().startEvent(transcriptText, totalUnits);
// commit changes
bridge.addChange(changes);
return null;
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class PurchaseDelegate method removeFromPlayer.
protected String removeFromPlayer(final IntegerMap<Resource> costs, final CompositeChange changes) {
final StringBuilder returnString = new StringBuilder("Remaining resources: ");
final IntegerMap<Resource> left = player.getResources().getResourcesCopy();
left.subtract(costs);
for (final Entry<Resource, Integer> entry : left.entrySet()) {
returnString.append(entry.getValue()).append(" ").append(entry.getKey().getName()).append("; ");
}
for (final Resource resource : costs.keySet()) {
final float quantity = costs.getInt(resource);
final int cost = (int) quantity;
final Change change = ChangeFactory.changeResourcesChange(player, resource, -cost);
changes.add(change);
}
return returnString.toString();
}
use of games.strategy.engine.data.Resource in project triplea by triplea-game.
the class PurchaseDelegate method purchaseRepair.
@Override
public String purchaseRepair(final Map<Unit, IntegerMap<RepairRule>> repairRules) {
final IntegerMap<Resource> costs = getRepairCosts(repairRules, player);
if (!(canAfford(costs, player))) {
return NOT_ENOUGH_RESOURCES;
}
if (!Properties.getDamageFromBombingDoneToUnitsInsteadOfTerritories(getData())) {
return null;
}
// Get the map of the factories that were repaired and how much for each
final IntegerMap<Unit> repairMap = getUnitRepairs(repairRules);
if (repairMap.isEmpty()) {
return null;
}
// remove first, since add logs PUs remaining
final CompositeChange changes = new CompositeChange();
final Set<Unit> repairUnits = new HashSet<>(repairMap.keySet());
final IntegerMap<Unit> damageMap = new IntegerMap<>();
for (final Unit u : repairUnits) {
final int repairCount = repairMap.getInt(u);
// Display appropriate damaged/repaired factory and factory damage totals
if (repairCount > 0) {
final TripleAUnit taUnit = (TripleAUnit) u;
final int newDamageTotal = Math.max(0, taUnit.getUnitDamage() - repairCount);
if (newDamageTotal != taUnit.getUnitDamage()) {
damageMap.put(u, newDamageTotal);
}
}
}
if (!damageMap.isEmpty()) {
changes.add(ChangeFactory.bombingUnitDamage(damageMap));
}
// add changes for spent resources
final String remaining = removeFromPlayer(costs, changes);
// add history event
final String transcriptText;
if (!damageMap.isEmpty()) {
transcriptText = player.getName() + " repair damage of " + MyFormatter.integerUnitMapToString(repairMap, ", ", "x ", true) + "; " + remaining;
} else {
transcriptText = player.getName() + " repair nothing; " + remaining;
}
bridge.getHistoryWriter().startEvent(transcriptText, new HashSet<>(damageMap.keySet()));
// commit changes
if (!changes.isEmpty()) {
bridge.addChange(changes);
}
return null;
}
Aggregations