Search in sources :

Example 26 with ProductionRule

use of games.strategy.engine.data.ProductionRule 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)

Example 27 with ProductionRule

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

the class TuvUtils method getCostsForTuv.

/**
 * Return map where keys are unit types and values are PU costs of that unit type, based on a player.
 * 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 (so NWO artillery will become 4).
 * Therefore, this map should NOT be used for Purchasing information!
 *
 * @param player
 *        The player to get costs schedule for
 * @param data
 *        The game data.
 * @return a map of unit types to PU cost
 */
public static IntegerMap<UnitType> getCostsForTuv(final PlayerID player, 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 ProductionFrontier frontier = player.getProductionFrontier();
    // any one will do then
    if (frontier == null) {
        return TuvUtils.getCostsForTuvForAllPlayersMergedAndAveraged(data);
    }
    for (final ProductionRule rule : frontier.getRules()) {
        final NamedAttachable resourceOrUnit = rule.getResults().keySet().iterator().next();
        if (!(resourceOrUnit instanceof UnitType)) {
            continue;
        }
        final UnitType type = (UnitType) resourceOrUnit;
        final int costPerGroup = rule.getCosts().getInt(pus);
        final int numberProduced = rule.getResults().getInt(type);
        // we average the cost for a single unit, rounding up
        final int roundedCostPerSingle = (int) Math.ceil((double) costPerGroup / (double) numberProduced);
        costs.put(type, roundedCostPerSingle);
    }
    // since our production frontier may not cover all the units we control, and not the enemy units,
    // we will add any unit types not in our list, based on the list for everyone
    final IntegerMap<UnitType> costsAll = TuvUtils.getCostsForTuvForAllPlayersMergedAndAveraged(data);
    for (final UnitType ut : costsAll.keySet()) {
        if (!costs.keySet().contains(ut)) {
            costs.put(ut, costsAll.getInt(ut));
        }
    }
    // Override with XML TUV or consumesUnit sum
    final IntegerMap<UnitType> result = new IntegerMap<>(costs);
    for (final UnitType unitType : costs.keySet()) {
        result.put(unitType, getTotalTuv(unitType, costs, new HashSet<>()));
    }
    return result;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ProductionRule(games.strategy.engine.data.ProductionRule) NamedAttachable(games.strategy.engine.data.NamedAttachable) UnitType(games.strategy.engine.data.UnitType) Resource(games.strategy.engine.data.Resource) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) HashSet(java.util.HashSet)

Aggregations

ProductionRule (games.strategy.engine.data.ProductionRule)27 IntegerMap (games.strategy.util.IntegerMap)14 NamedAttachable (games.strategy.engine.data.NamedAttachable)10 Resource (games.strategy.engine.data.Resource)10 UnitType (games.strategy.engine.data.UnitType)10 Unit (games.strategy.engine.data.Unit)9 GameData (games.strategy.engine.data.GameData)8 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)8 Territory (games.strategy.engine.data.Territory)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 PlayerID (games.strategy.engine.data.PlayerID)7 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Map (java.util.Map)5 RepairRule (games.strategy.engine.data.RepairRule)4 Matches (games.strategy.triplea.delegate.Matches)4 CollectionUtils (games.strategy.util.CollectionUtils)4 Collection (java.util.Collection)4 LinkedHashMap (java.util.LinkedHashMap)4