Search in sources :

Example 41 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class RevisedTest method testUnloadMultipleTerritories.

@Test
public void testUnloadMultipleTerritories() {
    // in revised a transport may only unload to 1 territory.
    final Territory sz5 = gameData.getMap().getTerritory("5 Sea Zone");
    final Territory eastEurope = gameData.getMap().getTerritory("Eastern Europe");
    final UnitType infantryType = GameDataTestUtil.infantry(gameData);
    final PlayerID germans = GameDataTestUtil.germans(gameData);
    final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
    final ITestDelegateBridge bridge = getDelegateBridge(germans);
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    final Route eeToSz5 = new Route();
    eeToSz5.setStart(eastEurope);
    eeToSz5.add(sz5);
    // load the transport in the baltic
    final List<Unit> infantry = eastEurope.getUnits().getMatches(Matches.unitIsOfType(infantryType));
    assertEquals(2, infantry.size());
    final TripleAUnit transport = (TripleAUnit) sz5.getUnits().getMatches(Matches.unitIsTransport()).get(0);
    String error = moveDelegate.move(infantry, eeToSz5, Collections.singletonList(transport));
    assertNull(error, error);
    // unload one infantry to Norway
    final Territory norway = gameData.getMap().getTerritory("Norway");
    final Route sz5ToNorway = new Route();
    sz5ToNorway.setStart(sz5);
    sz5ToNorway.add(norway);
    error = moveDelegate.move(infantry.subList(0, 1), sz5ToNorway);
    assertNull(error, error);
    // make sure the transport was unloaded
    assertTrue(moveDelegate.getMovesMade().get(1).wasTransportUnloaded(transport));
    // try to unload the other infantry somewhere else, an error occurs
    final Route sz5ToEastEurope = new Route();
    sz5ToEastEurope.setStart(sz5);
    sz5ToEastEurope.add(eastEurope);
    error = moveDelegate.move(infantry.subList(1, 2), sz5ToEastEurope);
    assertNotNull(error, error);
    assertTrue(error.startsWith(MoveValidator.TRANSPORT_HAS_ALREADY_UNLOADED_UNITS_TO));
    // end the round
    moveDelegate.end();
    bridge.setStepName("NonCombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    moveDelegate.end();
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    // a new round, the move should work
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    error = moveDelegate.move(infantry.subList(1, 2), sz5ToEastEurope);
    assertNull(error);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 42 with TripleAUnit

use of games.strategy.triplea.TripleAUnit 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 43 with TripleAUnit

use of games.strategy.triplea.TripleAUnit 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 44 with TripleAUnit

use of games.strategy.triplea.TripleAUnit in project triplea by triplea-game.

the class BattleTrackerTest method verifyRaids.

@Test
public void verifyRaids() {
    final Territory territory = new Territory("terrName", mockGameData);
    final Route route = new Route(territory);
    final PlayerID playerId = new PlayerID("name", mockGameData);
    // need at least one attacker for there to be considered a battle.
    final Unit unit = new TripleAUnit(new UnitType("unit", mockGameData), playerId, mockGameData);
    final List<Unit> attackers = Collections.singletonList(unit);
    when(mockDelegateBridge.getData()).thenReturn(mockGameData);
    when(mockGameData.getProperties()).thenReturn(mockGameProperties);
    when(mockGameData.getRelationshipTracker()).thenReturn(mockRelationshipTracker);
    when(mockGameProperties.get(Constants.RAIDS_MAY_BE_PRECEEDED_BY_AIR_BATTLES, false)).thenReturn(true);
    doReturn(null).when(mockGetBattleFunction).apply(territory, IBattle.BattleType.AIR_RAID);
    doReturn(mockBattle).when(mockGetBattleFunction).apply(territory, IBattle.BattleType.BOMBING_RAID);
    // set up the testObj to have the bombing battle
    testObj.addBombingBattle(route, attackers, playerId, mockDelegateBridge, null, null);
    testObj.fightAirRaidsAndStrategicBombing(mockDelegateBridge, () -> Collections.singleton(territory), mockGetBattleFunction);
    verify(mockBattle).fight(mockDelegateBridge);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) TripleAUnit(games.strategy.triplea.TripleAUnit) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 45 with TripleAUnit

use of games.strategy.triplea.TripleAUnit 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

TripleAUnit (games.strategy.triplea.TripleAUnit)47 Unit (games.strategy.engine.data.Unit)42 Territory (games.strategy.engine.data.Territory)25 PlayerID (games.strategy.engine.data.PlayerID)19 UnitType (games.strategy.engine.data.UnitType)16 CompositeChange (games.strategy.engine.data.CompositeChange)15 Test (org.junit.jupiter.api.Test)14 Route (games.strategy.engine.data.Route)13 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)12 ArrayList (java.util.ArrayList)11 GameData (games.strategy.engine.data.GameData)10 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 IntegerMap (games.strategy.util.IntegerMap)8 Change (games.strategy.engine.data.Change)7 RepairRule (games.strategy.engine.data.RepairRule)5 HashSet (java.util.HashSet)4 Resource (games.strategy.engine.data.Resource)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 ResourceCollection (games.strategy.engine.data.ResourceCollection)2