Search in sources :

Example 11 with NamedAttachable

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

the class EditProductionPanel method initRules.

@Override
protected void initRules(final PlayerID player, final IntegerMap<ProductionRule> initialPurchase) {
    this.data.acquireReadLock();
    try {
        id = player;
        final Set<UnitType> unitsAllowed = new HashSet<>();
        if (player.getProductionFrontier() != null) {
            for (final ProductionRule productionRule : player.getProductionFrontier()) {
                final Rule rule = new Rule(productionRule, player);
                for (final Entry<NamedAttachable, Integer> entry : productionRule.getResults().entrySet()) {
                    if (UnitType.class.isAssignableFrom(entry.getKey().getClass())) {
                        unitsAllowed.add((UnitType) entry.getKey());
                    }
                }
                final int initialQuantity = initialPurchase.getInt(productionRule);
                rule.setQuantity(initialQuantity);
                rules.add(rule);
            }
        }
        // use the units on a map.
        for (final Territory t : data.getMap()) {
            for (final Unit u : t.getUnits()) {
                if (u.getOwner().equals(player)) {
                    final UnitType ut = u.getType();
                    if (!unitsAllowed.contains(ut)) {
                        unitsAllowed.add(ut);
                        final IntegerMap<NamedAttachable> result = new IntegerMap<>();
                        result.add(ut, 1);
                        final IntegerMap<Resource> cost = new IntegerMap<>();
                        cost.add(data.getResourceList().getResource(Constants.PUS), 1);
                        final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
                        final Rule rule = new Rule(newRule, player);
                        rule.setQuantity(0);
                        rules.add(rule);
                    }
                }
            }
        }
        // now check if we have the art for anything that is left
        for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
            if (!unitsAllowed.contains(ut)) {
                try {
                    final UnitImageFactory imageFactory = uiContext.getUnitImageFactory();
                    if (imageFactory != null) {
                        final Optional<Image> unitImage = imageFactory.getImage(ut, player, false, false);
                        if (unitImage.isPresent()) {
                            unitsAllowed.add(ut);
                            final IntegerMap<NamedAttachable> result = new IntegerMap<>();
                            result.add(ut, 1);
                            final IntegerMap<Resource> cost = new IntegerMap<>();
                            cost.add(data.getResourceList().getResource(Constants.PUS), 1);
                            final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
                            final Rule rule = new Rule(newRule, player);
                            rule.setQuantity(0);
                            rules.add(rule);
                        }
                    }
                } catch (final Exception e) {
                // ignore
                }
            }
        }
    } finally {
        this.data.releaseReadLock();
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit) Image(java.awt.Image) ProductionRule(games.strategy.engine.data.ProductionRule) UnitType(games.strategy.engine.data.UnitType) ProductionRule(games.strategy.engine.data.ProductionRule) HashSet(java.util.HashSet) UnitImageFactory(games.strategy.triplea.image.UnitImageFactory)

Example 12 with NamedAttachable

use of games.strategy.engine.data.NamedAttachable 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 13 with NamedAttachable

use of games.strategy.engine.data.NamedAttachable 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 14 with NamedAttachable

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

NamedAttachable (games.strategy.engine.data.NamedAttachable)14 ProductionRule (games.strategy.engine.data.ProductionRule)11 UnitType (games.strategy.engine.data.UnitType)10 Resource (games.strategy.engine.data.Resource)9 IntegerMap (games.strategy.util.IntegerMap)6 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)5 Unit (games.strategy.engine.data.Unit)5 Territory (games.strategy.engine.data.Territory)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)3 LinkedHashMap (java.util.LinkedHashMap)3 CompositeChange (games.strategy.engine.data.CompositeChange)2 GameData (games.strategy.engine.data.GameData)2 PlayerID (games.strategy.engine.data.PlayerID)2 RepairRule (games.strategy.engine.data.RepairRule)2 ResourceCollection (games.strategy.engine.data.ResourceCollection)2 TripleAUnit (games.strategy.triplea.TripleAUnit)2 Collection (java.util.Collection)2