Search in sources :

Example 11 with Resource

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;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) Resource(games.strategy.engine.data.Resource) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 12 with Resource

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

Example 13 with Resource

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;
}
Also used : TriggerAttachment(games.strategy.triplea.attachments.TriggerAttachment) AbstractTriggerAttachment(games.strategy.triplea.attachments.AbstractTriggerAttachment) ArrayList(java.util.ArrayList) Resource(games.strategy.engine.data.Resource) ObjectiveDummyDelegateBridge(games.strategy.triplea.ui.ObjectiveDummyDelegateBridge) RulesAttachment(games.strategy.triplea.attachments.RulesAttachment) ICondition(games.strategy.triplea.attachments.ICondition) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) HashSet(java.util.HashSet)

Example 14 with Resource

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

Example 15 with Resource

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

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