Search in sources :

Example 6 with RepairRule

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;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) TreeSet(java.util.TreeSet) RepairRule(games.strategy.engine.data.RepairRule) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit)

Example 7 with RepairRule

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;
}
Also used : ProductionRule(games.strategy.engine.data.ProductionRule) RepairRule(games.strategy.engine.data.RepairRule) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 8 with RepairRule

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());
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) RepairRule(games.strategy.engine.data.RepairRule) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Test(org.junit.jupiter.api.Test)

Example 9 with RepairRule

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);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) RepairRule(games.strategy.engine.data.RepairRule) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Test(org.junit.jupiter.api.Test)

Example 10 with RepairRule

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;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ProPlaceTerritory(games.strategy.triplea.ai.pro.data.ProPlaceTerritory) ProPurchaseTerritory(games.strategy.triplea.ai.pro.data.ProPurchaseTerritory) Territory(games.strategy.engine.data.Territory) HashMap(java.util.HashMap) RepairRule(games.strategy.engine.data.RepairRule) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit)

Aggregations

RepairRule (games.strategy.engine.data.RepairRule)12 Unit (games.strategy.engine.data.Unit)8 TripleAUnit (games.strategy.triplea.TripleAUnit)7 IntegerMap (games.strategy.util.IntegerMap)7 Territory (games.strategy.engine.data.Territory)5 Resource (games.strategy.engine.data.Resource)3 HashMap (java.util.HashMap)3 NamedAttachable (games.strategy.engine.data.NamedAttachable)2 ProductionRule (games.strategy.engine.data.ProductionRule)2 TreeSet (java.util.TreeSet)2 Test (org.junit.jupiter.api.Test)2 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)1 PlayerID (games.strategy.engine.data.PlayerID)1 RepairFrontier (games.strategy.engine.data.RepairFrontier)1 ResourceCollection (games.strategy.engine.data.ResourceCollection)1 Route (games.strategy.engine.data.Route)1 UnitType (games.strategy.engine.data.UnitType)1 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)1 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)1