Search in sources :

Example 16 with ProductionRule

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

the class PlayerUnitsPanel method getUnitTypes.

/**
 * Return all the unit types available for the given player. A unit type is
 * available if the unit can be purchased or if a player has one on the map.
 */
private Collection<UnitType> getUnitTypes(final PlayerID player) {
    Collection<UnitType> unitTypes = new LinkedHashSet<>();
    final ProductionFrontier frontier = player.getProductionFrontier();
    if (frontier != null) {
        for (final ProductionRule rule : frontier) {
            for (final NamedAttachable type : rule.getResults().keySet()) {
                if (type instanceof UnitType) {
                    unitTypes.add((UnitType) type);
                }
            }
        }
    }
    for (final Territory t : data.getMap()) {
        for (final Unit u : t.getUnits()) {
            if (u.getOwner().equals(player)) {
                unitTypes.add(u.getType());
            }
        }
    }
    // Filter out anything like factories, or units that have no combat ability AND cannot be taken casualty
    unitTypes = CollectionUtils.getMatches(unitTypes, Matches.unitTypeCanBeInBattle(!defender, isLand, player, 1, false, false, false));
    return unitTypes;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Territory(games.strategy.engine.data.Territory) ProductionRule(games.strategy.engine.data.ProductionRule) NamedAttachable(games.strategy.engine.data.NamedAttachable) UnitType(games.strategy.engine.data.UnitType) Unit(games.strategy.engine.data.Unit) ProductionFrontier(games.strategy.engine.data.ProductionFrontier)

Example 17 with ProductionRule

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

the class BidPurchaseDelegate method canWePurchaseOrRepair.

@Override
protected boolean canWePurchaseOrRepair() {
    final ResourceCollection bidCollection = new ResourceCollection(getData());
    // TODO: allow bids to have more than just PUs
    bidCollection.addResource(getData().getResourceList().getResource(Constants.PUS), bid);
    if (player.getProductionFrontier() != null && player.getProductionFrontier().getRules() != null) {
        for (final ProductionRule rule : player.getProductionFrontier().getRules()) {
            if (bidCollection.has(rule.getCosts())) {
                return true;
            }
        }
    }
    if (player.getRepairFrontier() != null && player.getRepairFrontier().getRules() != null) {
        for (final RepairRule rule : player.getRepairFrontier().getRules()) {
            if (bidCollection.has(rule.getCosts())) {
                return true;
            }
        }
    }
    return false;
}
Also used : ProductionRule(games.strategy.engine.data.ProductionRule) RepairRule(games.strategy.engine.data.RepairRule) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 18 with ProductionRule

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

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

the class VictoryTest method testNotEnoughMultipleResourcesToPurchase.

@Test
public void testNotEnoughMultipleResourcesToPurchase() {
    testBridge.setStepName("italianPurchase");
    purchaseDelegate.setDelegateBridgeAndPlayer(testBridge);
    purchaseDelegate.start();
    final IntegerMap<ProductionRule> purchaseList = new IntegerMap<>();
    final ProductionRule armourtest = gameData.getProductionRuleList().getProductionRule("buyArmourtest2");
    assertNotNull(armourtest);
    italianResources.subtract(armourtest.getCosts());
    purchaseList.add(armourtest, 1);
    final String error = purchaseDelegate.purchase(purchaseList);
    assertEquals(PurchaseDelegate.NOT_ENOUGH_RESOURCES, error);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ProductionRule(games.strategy.engine.data.ProductionRule) Test(org.junit.jupiter.api.Test)

Example 20 with ProductionRule

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

the class VictoryTest method testNoPuResourcesToPurchase.

@Test
public void testNoPuResourcesToPurchase() {
    testBridge.setStepName("italianPurchase");
    purchaseDelegate.setDelegateBridgeAndPlayer(testBridge);
    purchaseDelegate.start();
    final IntegerMap<ProductionRule> purchaseList = new IntegerMap<>();
    final ProductionRule buyArmour = gameData.getProductionRuleList().getProductionRule("buyArmourtest3");
    assertNotNull(buyArmour);
    italianResources.subtract(buyArmour.getCosts());
    purchaseList.add(buyArmour, 1);
    final String error = purchaseDelegate.purchase(purchaseList);
    assertEquals(null, error);
    assertEquals(italianResources, italians.getResources().getResourcesCopy());
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ProductionRule(games.strategy.engine.data.ProductionRule) Test(org.junit.jupiter.api.Test)

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