use of games.strategy.engine.data.RepairRule in project triplea by triplea-game.
the class PurchaseDelegate method getUnitRepairs.
private static IntegerMap<Unit> getUnitRepairs(final Map<Unit, IntegerMap<RepairRule>> repairRules) {
final IntegerMap<Unit> repairMap = new IntegerMap<>();
for (final Unit u : repairRules.keySet()) {
final IntegerMap<RepairRule> rules = repairRules.get(u);
final TreeSet<RepairRule> repRules = new TreeSet<>(repairRuleComparator);
repRules.addAll(rules.keySet());
for (final RepairRule repairRule : repRules) {
final int quantity = rules.getInt(repairRule) * repairRule.getResults().getInt(u.getType());
repairMap.add(u, quantity);
}
}
return repairMap;
}
use of games.strategy.engine.data.RepairRule 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.RepairRule in project triplea by triplea-game.
the class WW2V3Year41Test method testRepairMoreThanDamaged.
@Test
public void testRepairMoreThanDamaged() {
final Territory germany = territory("Germany", gameData);
final Unit factory = germany.getUnits().getMatches(Matches.unitCanBeDamaged()).get(0);
final PurchaseDelegate del = purchaseDelegate(gameData);
del.setDelegateBridgeAndPlayer(getDelegateBridge(germans(gameData)));
del.start();
// dame a factory
final IntegerMap<Unit> startHits = new IntegerMap<>();
startHits.put(factory, 1);
gameData.performChange(ChangeFactory.bombingUnitDamage(startHits));
assertEquals(1, ((TripleAUnit) factory).getUnitDamage());
final RepairRule repair = germans(gameData).getRepairFrontier().getRules().get(0);
final IntegerMap<RepairRule> repairs = new IntegerMap<>();
// we have 1 damaged marker, but trying to repair 2
repairs.put(repair, 2);
final String error = del.purchaseRepair(Collections.singletonMap(CollectionUtils.getMatches(germany.getUnits().getUnits(), Matches.unitCanBeDamaged()).iterator().next(), repairs));
// it is no longer an error, we just math max 0 it
assertValid(error);
assertEquals(0, ((TripleAUnit) factory).getUnitDamage());
}
use of games.strategy.engine.data.RepairRule in project triplea by triplea-game.
the class WW2V3Year41Test method testRepair.
@Test
public void testRepair() {
final Territory germany = territory("Germany", gameData);
final Unit factory = germany.getUnits().getMatches(Matches.unitCanBeDamaged()).get(0);
final PurchaseDelegate del = purchaseDelegate(gameData);
del.setDelegateBridgeAndPlayer(getDelegateBridge(germans(gameData)));
del.start();
// Set up player
final PlayerID germans = GameDataTestUtil.germans(gameData);
final int initPUs = germans.getResources().getQuantity("PUs");
// damage a factory
IntegerMap<Unit> startHits = new IntegerMap<>();
startHits.put(factory, 1);
gameData.performChange(ChangeFactory.bombingUnitDamage(startHits));
assertEquals(1, ((TripleAUnit) factory).getUnitDamage());
RepairRule repair = germans(gameData).getRepairFrontier().getRules().get(0);
IntegerMap<RepairRule> repairs = new IntegerMap<>();
repairs.put(repair, 1);
String error = del.purchaseRepair(Collections.singletonMap(CollectionUtils.getMatches(germany.getUnits().getUnits(), Matches.unitCanBeDamaged()).iterator().next(), repairs));
assertValid(error);
assertEquals(0, ((TripleAUnit) factory).getUnitDamage());
// Find cost
final int midPUs = germans.getResources().getQuantity("PUs");
assertEquals(initPUs, midPUs + 1);
/*
* INCREASED_FACTORY_PRODUCTION repairs
*/
// Set up INCREASED_FACTORY_PRODUCTION
final ITestDelegateBridge delegateBridge = getDelegateBridge(germans(gameData));
TechTracker.addAdvance(germans, delegateBridge, TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_INCREASED_FACTORY_PRODUCTION, gameData, germans));
// damage a factory
startHits = new IntegerMap<>();
startHits.put(factory, 2);
gameData.performChange(ChangeFactory.bombingUnitDamage(startHits));
assertEquals(2, ((TripleAUnit) factory).getUnitDamage());
repair = germans(gameData).getRepairFrontier().getRules().get(0);
repairs = new IntegerMap<>();
repairs.put(repair, 2);
error = del.purchaseRepair(Collections.singletonMap(CollectionUtils.getMatches(germany.getUnits().getUnits(), Matches.unitCanBeDamaged()).iterator().next(), repairs));
assertValid(error);
assertEquals(0, ((TripleAUnit) factory).getUnitDamage());
// Find cost
final int finalPUs = germans.getResources().getQuantity("PUs");
assertEquals(midPUs, finalPUs + 1);
}
use of games.strategy.engine.data.RepairRule in project triplea by triplea-game.
the class ProPurchaseAi method repair.
int repair(int pusRemaining, final IPurchaseDelegate purchaseDelegate, final GameData data, final PlayerID player) {
ProLogger.info("Repairing factories with PUsRemaining=" + pusRemaining);
// Current data at the start of combat move
this.data = data;
this.player = player;
final Predicate<Unit> ourFactories = Matches.unitIsOwnedBy(player).and(Matches.unitCanProduceUnits()).and(Matches.unitIsInfrastructure());
final List<Territory> rfactories = CollectionUtils.getMatches(data.getMap().getTerritories(), ProMatches.territoryHasInfraFactoryAndIsNotConqueredOwnedLand(player, data));
if (player.getRepairFrontier() != null && Properties.getDamageFromBombingDoneToUnitsInsteadOfTerritories(data)) {
ProLogger.debug("Factories can be damaged");
final Map<Unit, Territory> unitsThatCanProduceNeedingRepair = new HashMap<>();
for (final Territory fixTerr : rfactories) {
if (!Matches.territoryIsOwnedAndHasOwnedUnitMatching(player, Matches.unitCanProduceUnitsAndCanBeDamaged()).test(fixTerr)) {
continue;
}
final Unit possibleFactoryNeedingRepair = TripleAUnit.getBiggestProducer(CollectionUtils.getMatches(fixTerr.getUnits().getUnits(), ourFactories), fixTerr, player, data, false);
if (Matches.unitHasTakenSomeBombingUnitDamage().test(possibleFactoryNeedingRepair)) {
unitsThatCanProduceNeedingRepair.put(possibleFactoryNeedingRepair, fixTerr);
}
}
ProLogger.debug("Factories that need repaired: " + unitsThatCanProduceNeedingRepair);
final List<RepairRule> rrules = player.getRepairFrontier().getRules();
for (final RepairRule rrule : rrules) {
for (final Unit fixUnit : unitsThatCanProduceNeedingRepair.keySet()) {
if (fixUnit == null || !fixUnit.getType().equals(rrule.getResults().keySet().iterator().next())) {
continue;
}
if (!Matches.territoryIsOwnedAndHasOwnedUnitMatching(player, Matches.unitCanProduceUnitsAndCanBeDamaged()).test(unitsThatCanProduceNeedingRepair.get(fixUnit))) {
continue;
}
final TripleAUnit taUnit = (TripleAUnit) fixUnit;
final int diff = taUnit.getUnitDamage();
if (diff > 0) {
final IntegerMap<RepairRule> repairMap = new IntegerMap<>();
repairMap.add(rrule, diff);
final HashMap<Unit, IntegerMap<RepairRule>> repair = new HashMap<>();
repair.put(fixUnit, repairMap);
pusRemaining -= diff;
ProLogger.debug("Repairing factory=" + fixUnit + ", damage=" + diff + ", repairRule=" + rrule);
purchaseDelegate.purchaseRepair(repair);
}
}
}
}
return pusRemaining;
}
Aggregations