Search in sources :

Example 66 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class AbstractAi method selectKamikazeSuicideAttacks.

@Override
public HashMap<Territory, HashMap<Unit, IntegerMap<Resource>>> selectKamikazeSuicideAttacks(final HashMap<Territory, Collection<Unit>> possibleUnitsToAttack) {
    final PlayerID id = getPlayerId();
    // we are going to just assign random attacks to each unit randomly, til we run out of tokens to attack with.
    final PlayerAttachment pa = PlayerAttachment.get(id);
    if (pa == null) {
        return null;
    }
    final IntegerMap<Resource> resourcesAndAttackValues = pa.getSuicideAttackResources();
    if (resourcesAndAttackValues.size() <= 0) {
        return null;
    }
    final IntegerMap<Resource> playerResourceCollection = id.getResources().getResourcesCopy();
    final IntegerMap<Resource> attackTokens = new IntegerMap<>();
    for (final Resource possible : resourcesAndAttackValues.keySet()) {
        final int amount = playerResourceCollection.getInt(possible);
        if (amount > 0) {
            attackTokens.put(possible, amount);
        }
    }
    if (attackTokens.size() <= 0) {
        return null;
    }
    final HashMap<Territory, HashMap<Unit, IntegerMap<Resource>>> kamikazeSuicideAttacks = new HashMap<>();
    for (final Entry<Territory, Collection<Unit>> entry : possibleUnitsToAttack.entrySet()) {
        if (attackTokens.size() <= 0) {
            continue;
        }
        final Territory t = entry.getKey();
        final List<Unit> targets = new ArrayList<>(entry.getValue());
        Collections.shuffle(targets);
        for (final Unit u : targets) {
            if (attackTokens.size() <= 0) {
                continue;
            }
            final IntegerMap<Resource> resourceMap = new IntegerMap<>();
            final Resource resource = attackTokens.keySet().iterator().next();
            final int num = Math.min(attackTokens.getInt(resource), (UnitAttachment.get(u.getType()).getHitPoints() * (Math.random() < .3 ? 1 : (Math.random() < .5 ? 2 : 3))));
            resourceMap.put(resource, num);
            final HashMap<Unit, IntegerMap<Resource>> attMap = kamikazeSuicideAttacks.getOrDefault(t, new HashMap<>());
            attMap.put(u, resourceMap);
            kamikazeSuicideAttacks.put(t, attMap);
            attackTokens.add(resource, -num);
            if (attackTokens.getInt(resource) <= 0) {
                attackTokens.removeKey(resource);
            }
        }
    }
    return kamikazeSuicideAttacks;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) Unit(games.strategy.engine.data.Unit) PlayerAttachment(games.strategy.triplea.attachments.PlayerAttachment) Collection(java.util.Collection)

Example 67 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class AbstractStat method getResourcePUs.

protected static Resource getResourcePUs(final GameData data) {
    final Resource pus;
    try {
        data.acquireReadLock();
        pus = data.getResourceList().getResource(Constants.PUS);
    } finally {
        data.releaseReadLock();
    }
    return pus;
}
Also used : Resource(games.strategy.engine.data.Resource)

Example 68 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class TabbedProductionPanel method getDefaultRuleLists.

private List<Tuple<String, List<Rule>>> getDefaultRuleLists() {
    final List<Tuple<String, List<Rule>>> ruleLists = new ArrayList<>();
    final ArrayList<Rule> allRules = new ArrayList<>();
    final ArrayList<Rule> landRules = new ArrayList<>();
    final ArrayList<Rule> airRules = new ArrayList<>();
    final ArrayList<Rule> seaRules = new ArrayList<>();
    final ArrayList<Rule> constructRules = new ArrayList<>();
    final ArrayList<Rule> upgradeConsumesRules = new ArrayList<>();
    final ArrayList<Rule> resourceRules = new ArrayList<>();
    for (final Rule rule : rules) {
        allRules.add(rule);
        final NamedAttachable resourceOrUnit = rule.getProductionRule().getResults().keySet().iterator().next();
        if (resourceOrUnit instanceof UnitType) {
            final UnitType type = (UnitType) resourceOrUnit;
            final UnitAttachment attach = UnitAttachment.get(type);
            if (attach.getConsumesUnits() != null && attach.getConsumesUnits().totalValues() >= 1) {
                upgradeConsumesRules.add(rule);
            }
            // anywhere (placed without needing a factory).
            if (attach.getIsConstruction()) {
                constructRules.add(rule);
            } else if (attach.getIsSea()) {
                seaRules.add(rule);
            } else if (attach.getIsAir()) {
                airRules.add(rule);
            } else {
                landRules.add(rule);
            }
        } else if (resourceOrUnit instanceof Resource) {
            resourceRules.add(rule);
        }
    }
    ruleLists.add(Tuple.of("All", allRules));
    ruleLists.add(Tuple.of("Land", landRules));
    ruleLists.add(Tuple.of("Air", airRules));
    ruleLists.add(Tuple.of("Sea", seaRules));
    ruleLists.add(Tuple.of("Construction", constructRules));
    ruleLists.add(Tuple.of("Upgrades/Consumes", upgradeConsumesRules));
    ruleLists.add(Tuple.of("Resources", resourceRules));
    return ruleLists;
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) NamedAttachable(games.strategy.engine.data.NamedAttachable) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) Resource(games.strategy.engine.data.Resource) ProductionRule(games.strategy.engine.data.ProductionRule) Tuple(games.strategy.util.Tuple)

Example 69 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class ResourceBar method gameDataChanged.

@Override
public void gameDataChanged(final Change change) {
    gameData.acquireReadLock();
    try {
        final PlayerID player = gameData.getSequence().getStep().getPlayerId();
        if (player != null) {
            final IntegerMap<Resource> resourceIncomes = AbstractEndTurnDelegate.findEstimatedIncome(player, gameData);
            SwingUtilities.invokeLater(() -> {
                this.removeAll();
                int count = 0;
                for (final ResourceStat resourceStat : resourceStats) {
                    final Resource resource = resourceStat.resource;
                    if (!resource.isDisplayedFor(player)) {
                        continue;
                    }
                    final double quantity = resourceStat.getValue(player, gameData);
                    final StringBuilder text = new StringBuilder(resourceStat.getFormatter().format(quantity) + " (");
                    if (resourceIncomes.getInt(resource) >= 0) {
                        text.append("+");
                    }
                    text.append(resourceIncomes.getInt(resource)).append(")");
                    final JLabel label = uiContext.getResourceImageFactory().getLabel(resource, text.toString());
                    label.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
                    add(label, new GridBagConstraints(count++, 0, 1, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
                }
            });
        }
    } finally {
        gameData.releaseReadLock();
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) Resource(games.strategy.engine.data.Resource) JLabel(javax.swing.JLabel)

Example 70 with Resource

use of games.strategy.engine.data.Resource in project triplea by triplea-game.

the class TuvUtils method getCostsForTuvForAllPlayersMergedAndAveraged.

/**
 * Return a map where key are unit types and values are the AVERAGED for all RULES (not for all players).
 * Any production rule that produces multiple units
 * (like artillery in NWO, costs 7 but makes 2 artillery, meaning effective price is 3.5 each)
 * will have their costs rounded up on a per unit basis.
 * Therefore, this map should NOT be used for Purchasing information!
 */
private static IntegerMap<UnitType> getCostsForTuvForAllPlayersMergedAndAveraged(final GameData data) {
    data.acquireReadLock();
    final Resource pus;
    try {
        pus = data.getResourceList().getResource(Constants.PUS);
    } finally {
        data.releaseReadLock();
    }
    final IntegerMap<UnitType> costs = new IntegerMap<>();
    final HashMap<UnitType, List<Integer>> differentCosts = new HashMap<>();
    for (final ProductionRule rule : data.getProductionRuleList().getProductionRules()) {
        // only works for the first result, so we are assuming each purchase frontier only gives one type of unit
        final NamedAttachable resourceOrUnit = rule.getResults().keySet().iterator().next();
        if (!(resourceOrUnit instanceof UnitType)) {
            continue;
        }
        final UnitType ut = (UnitType) resourceOrUnit;
        final int numberProduced = rule.getResults().getInt(ut);
        final int costPerGroup = rule.getCosts().getInt(pus);
        // we round up the cost
        final int roundedCostPerSingle = (int) Math.ceil((double) costPerGroup / (double) numberProduced);
        if (differentCosts.containsKey(ut)) {
            differentCosts.get(ut).add(roundedCostPerSingle);
        } else {
            final List<Integer> listTemp = new ArrayList<>();
            listTemp.add(roundedCostPerSingle);
            differentCosts.put(ut, listTemp);
        }
    }
    for (final UnitType ut : differentCosts.keySet()) {
        int totalCosts = 0;
        final List<Integer> costsForType = differentCosts.get(ut);
        for (final int cost : costsForType) {
            totalCosts += cost;
        }
        final int averagedCost = (int) Math.round(((double) totalCosts / (double) costsForType.size()));
        costs.put(ut, averagedCost);
    }
    return costs;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) ProductionRule(games.strategy.engine.data.ProductionRule) UnitType(games.strategy.engine.data.UnitType) ArrayList(java.util.ArrayList) List(java.util.List)

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