Search in sources :

Example 41 with Resource

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");
}
Also used : Resource(games.strategy.engine.data.Resource)

Example 42 with Resource

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);
    }
}
Also used : Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change)

Example 43 with Resource

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;
}
Also used : Territory(games.strategy.engine.data.Territory) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Predicate(java.util.function.Predicate) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType) Collection(java.util.Collection) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 44 with Resource

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();
}
Also used : Resource(games.strategy.engine.data.Resource) CompositeChange(games.strategy.engine.data.CompositeChange) Change(games.strategy.engine.data.Change)

Example 45 with Resource

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;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Resource(games.strategy.engine.data.Resource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CompositeChange(games.strategy.engine.data.CompositeChange) TripleAUnit(games.strategy.triplea.TripleAUnit) HashSet(java.util.HashSet)

Aggregations

Resource (games.strategy.engine.data.Resource)71 PlayerID (games.strategy.engine.data.PlayerID)22 IntegerMap (games.strategy.util.IntegerMap)16 GameData (games.strategy.engine.data.GameData)15 Unit (games.strategy.engine.data.Unit)15 ArrayList (java.util.ArrayList)15 Territory (games.strategy.engine.data.Territory)14 UnitType (games.strategy.engine.data.UnitType)13 Change (games.strategy.engine.data.Change)11 CompositeChange (games.strategy.engine.data.CompositeChange)11 ProductionRule (games.strategy.engine.data.ProductionRule)10 TripleAUnit (games.strategy.triplea.TripleAUnit)10 NamedAttachable (games.strategy.engine.data.NamedAttachable)9 ResourceCollection (games.strategy.engine.data.ResourceCollection)8 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)7 GameParseException (games.strategy.engine.data.GameParseException)6 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Test (org.junit.jupiter.api.Test)5