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;
}
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;
}
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);
}
}
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);
}
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());
}
Aggregations