Search in sources :

Example 11 with ProductionFrontier

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

the class TriggerAttachment method triggerProductionFrontierEditChange.

public static void triggerProductionFrontierEditChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
    final GameData data = bridge.getData();
    Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, prodFrontierEditMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    final CompositeChange change = new CompositeChange();
    for (final TriggerAttachment triggerAttachment : trigs) {
        if (testChance && !triggerAttachment.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            triggerAttachment.use(bridge);
        }
        triggerAttachment.getProductionRule().stream().map(s -> s.split(":")).forEach(array -> {
            final ProductionFrontier front = data.getProductionFrontierList().getProductionFrontier(array[0]);
            final String rule = array[1];
            final String ruleName = rule.replaceFirst("^-", "");
            final ProductionRule productionRule = data.getProductionRuleList().getProductionRule(ruleName);
            final boolean ruleAdded = !rule.startsWith("-");
            if (ruleAdded) {
                if (!front.getRules().contains(productionRule)) {
                    change.add(ChangeFactory.addProductionRule(productionRule, front));
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(triggerAttachment.getName()) + ": " + productionRule.getName() + " added to " + front.getName());
                }
            } else {
                if (front.getRules().contains(productionRule)) {
                    change.add(ChangeFactory.removeProductionRule(productionRule, front));
                    bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(triggerAttachment.getName()) + ": " + productionRule.getName() + " removed from " + front.getName());
                }
            }
        });
    }
    if (!change.isEmpty()) {
        // TODO: we should sort the frontier list if we make changes to it...
        bridge.addChange(change);
    }
}
Also used : BattleTracker(games.strategy.triplea.delegate.BattleTracker) ITripleADisplay(games.strategy.triplea.ui.display.ITripleADisplay) Constants(games.strategy.triplea.Constants) Properties(games.strategy.triplea.Properties) IAttachment(games.strategy.engine.data.IAttachment) MutableProperty(games.strategy.engine.data.MutableProperty) DelegateFinder(games.strategy.triplea.delegate.DelegateFinder) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge) Map(java.util.Map) IntegerMap(games.strategy.util.IntegerMap) GameParseException(games.strategy.engine.data.GameParseException) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Attachable(games.strategy.engine.data.Attachable) GameData(games.strategy.engine.data.GameData) List(java.util.List) Tuple(games.strategy.util.Tuple) PlayerID(games.strategy.engine.data.PlayerID) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) Matches(games.strategy.triplea.delegate.Matches) MyFormatter(games.strategy.triplea.formatter.MyFormatter) MapSupport(games.strategy.triplea.MapSupport) TerritoryEffect(games.strategy.engine.data.TerritoryEffect) TechTracker(games.strategy.triplea.delegate.TechTracker) HashMap(java.util.HashMap) Resource(games.strategy.engine.data.Resource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) UnitType(games.strategy.engine.data.UnitType) CompositeChange(games.strategy.engine.data.CompositeChange) TechnologyFrontier(games.strategy.engine.data.TechnologyFrontier) CollectionUtils(games.strategy.util.CollectionUtils) TechAdvance(games.strategy.triplea.delegate.TechAdvance) RelationshipType(games.strategy.engine.data.RelationshipType) Unit(games.strategy.engine.data.Unit) Territory(games.strategy.engine.data.Territory) IDelegate(games.strategy.engine.delegate.IDelegate) ClientLogger(games.strategy.debug.ClientLogger) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) NotificationMessages(games.strategy.triplea.ui.NotificationMessages) Change(games.strategy.engine.data.Change) EndRoundDelegate(games.strategy.triplea.delegate.EndRoundDelegate) OriginalOwnerTracker(games.strategy.triplea.delegate.OriginalOwnerTracker) ProductionRule(games.strategy.engine.data.ProductionRule) AbstractMoveDelegate(games.strategy.triplea.delegate.AbstractMoveDelegate) Collections(java.util.Collections) SoundPath(games.strategy.sound.SoundPath) GameData(games.strategy.engine.data.GameData) ProductionRule(games.strategy.engine.data.ProductionRule) CompositeChange(games.strategy.engine.data.CompositeChange) ProductionFrontier(games.strategy.engine.data.ProductionFrontier)

Example 12 with ProductionFrontier

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

ProductionFrontier (games.strategy.engine.data.ProductionFrontier)12 ProductionRule (games.strategy.engine.data.ProductionRule)7 UnitType (games.strategy.engine.data.UnitType)5 GameData (games.strategy.engine.data.GameData)4 NamedAttachable (games.strategy.engine.data.NamedAttachable)4 PlayerID (games.strategy.engine.data.PlayerID)4 Change (games.strategy.engine.data.Change)3 CompositeChange (games.strategy.engine.data.CompositeChange)3 IntegerMap (games.strategy.util.IntegerMap)3 HashSet (java.util.HashSet)3 GameParseException (games.strategy.engine.data.GameParseException)2 Resource (games.strategy.engine.data.Resource)2 Territory (games.strategy.engine.data.Territory)2 Unit (games.strategy.engine.data.Unit)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ClientLogger (games.strategy.debug.ClientLogger)1 Attachable (games.strategy.engine.data.Attachable)1 IAttachment (games.strategy.engine.data.IAttachment)1 MutableProperty (games.strategy.engine.data.MutableProperty)1 RelationshipType (games.strategy.engine.data.RelationshipType)1