Search in sources :

Example 61 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseMarineRange method execute.

@Override
public State execute() {
    try {
        if (((GameState) this.handler).UBs.isEmpty()) {
            return State.FAILURE;
        }
        for (Unit u : ((GameState) this.handler).UBs) {
            if (((GameState) this.handler).getPlayer().hasResearched(TechType.Stim_Packs) && ((GameState) this.handler).getPlayer().getUpgradeLevel(UpgradeType.U_238_Shells) != 1 && u.canUpgrade(UpgradeType.U_238_Shells) && !u.isResearching() && !u.isUpgrading()) {
                ((GameState) this.handler).chosenUnitUpgrader = u;
                ((GameState) this.handler).chosenUpgrade = UpgradeType.U_238_Shells;
                return State.SUCCESS;
            }
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : GameState(ecgberht.GameState) Unit(bwapi.Unit)

Example 62 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseWeaponInfUp method execute.

@Override
public State execute() {
    try {
        if (((GameState) this.handler).UBs.isEmpty()) {
            return State.FAILURE;
        }
        for (Unit u : ((GameState) this.handler).UBs) {
            if (u.canUpgrade(UpgradeType.Terran_Infantry_Weapons) && !u.isResearching() && !u.isUpgrading()) {
                ((GameState) this.handler).chosenUnitUpgrader = u;
                ((GameState) this.handler).chosenUpgrade = UpgradeType.Terran_Infantry_Weapons;
                return State.SUCCESS;
            }
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : GameState(ecgberht.GameState) Unit(bwapi.Unit)

Example 63 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class InformationManagerAcceptanceTest method multipleResourceDepotsAtBaseOneDestroyed_baseLocationStillEnemies.

/**
 * Verify that enemy base locations are not removed if multiple resource depots are observed at the same location.
 */
@Test
public void multipleResourceDepotsAtBaseOneDestroyed_baseLocationStillEnemies() {
    final Race enemyRace = createMockRace(mock(UnitType.class), mock(UnitType.class));
    when(this.enemy.getRace()).thenReturn(enemyRace);
    final BaseLocation selfStartingLocation = mock(BaseLocation.class);
    final BaseLocation enemyStartingLocation = mock(BaseLocation.class);
    final BaseLocation otherStartingLocation = mock(BaseLocation.class);
    setStartingLocationsOnMock(this.bwtaWrapper, selfStartingLocation, enemyStartingLocation, otherStartingLocation);
    setSelfStartingLocationOnMock(this.bwtaWrapper, this.self, selfStartingLocation);
    this.target.onStart();
    assertThat(this.target.getEnemyBases()).as("no known enemy bases").isEmpty();
    // observe multiple resource depots in enemy base location
    final TilePosition firstResourceDepotPosition = mock(TilePosition.class);
    setNearestBaseLocationOnMock(this.bwtaWrapper, firstResourceDepotPosition, enemyStartingLocation);
    final Unit firstResourceDepot = UnitBuilder.mockUnit(this.enemy, mockResourceDepotUnitType(), firstResourceDepotPosition);
    this.target.onUnitDiscover(firstResourceDepot);
    final TilePosition secondResourceDepotPosition = mock(TilePosition.class);
    setNearestBaseLocationOnMock(this.bwtaWrapper, secondResourceDepotPosition, enemyStartingLocation);
    final Unit secondResourceDepot = UnitBuilder.mockUnit(this.enemy, mockResourceDepotUnitType(), firstResourceDepotPosition);
    this.target.onUnitDiscover(secondResourceDepot);
    // first resource depot destroyed
    this.target.onUnitDestroy(firstResourceDepot);
    // verify location is still enemy
    assertThat(this.target.getEnemyBases()).as("selfStartingLocation still enemy base").containsExactly(enemyStartingLocation);
}
Also used : UnitType(bwapi.UnitType) UnitTypeUtils.mockResourceDepotUnitType(BWJSAL.utils.UnitTypeUtils.mockResourceDepotUnitType) RaceUtils.createMockRace(BWJSAL.utils.RaceUtils.createMockRace) Race(bwapi.Race) TilePosition(bwapi.TilePosition) Unit(bwapi.Unit) BaseLocation(bwta.BaseLocation) Test(org.junit.Test)

Example 64 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitTrackerTest method assertThatDataExtractedFromUnit.

/**
 * Verifies that targetToInvoke returns Unit.methodToMock.
 *
 * This is used to verify that unit getters are called when the unit exists.
 */
private <T> void assertThatDataExtractedFromUnit(Class<T> responseClass, Function<Unit, T> methodToMock, Function<Unit, T> targetToInvoke) {
    final Unit unit = createMockUnit(true);
    final T mockResponse = Mockito.mock(responseClass);
    when(methodToMock.apply(unit)).thenReturn(mockResponse);
    assertThat(targetToInvoke.apply(unit)).isEqualTo(mockResponse);
}
Also used : Unit(bwapi.Unit)

Example 65 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitTrackerTest method assertThatTrackedInformationReturned.

/**
 * Verify that UnitTracker returns the last information saved available when the unit existed.
 *
 * @param invocationTargetReturnType    The class that is being tracked
 * @param unitGetterMethodThatIsTracked What method is called to get the tracked data from the unit
 * @param targetMethodToInvoke          What method is called from the target to get the tracked data
 * @param <T>                           The class that is being tracked
 */
public <T> void assertThatTrackedInformationReturned(Class<T> invocationTargetReturnType, Function<Unit, T> unitGetterMethodThatIsTracked, Function<Unit, T> targetMethodToInvoke) {
    final Unit unit = createMockUnit(true);
    // First value returned by getter. This should tracked, and will be updated in frame 2
    final T getterValueAtDiscovery = Mockito.mock(invocationTargetReturnType);
    when(unitGetterMethodThatIsTracked.apply(unit)).thenReturn(getterValueAtDiscovery);
    this.target.onUnitDiscover(unit, 0);
    this.target.onFrame(0);
    // Last value of getter that will be tracked
    final T lastGetterValueWhileUnitExisted = Mockito.mock(invocationTargetReturnType);
    when(unitGetterMethodThatIsTracked.apply(unit)).thenReturn(lastGetterValueWhileUnitExisted);
    this.target.onFrame(1);
    // Unit no longer exists
    when(unit.exists()).thenReturn(false);
    // Update getter to a new value. This value should NOT be returned.
    final Position getterValueButUnitDoesntExist = Mockito.mock(Position.class);
    when(unit.getPosition()).thenReturn(getterValueButUnitDoesntExist);
    this.target.onFrame(2);
    // Verify that the value tracked in frame 1 is returned.
    assertThat(targetMethodToInvoke.apply(unit)).isEqualTo(lastGetterValueWhileUnitExisted).isNotEqualTo(getterValueAtDiscovery).isNotEqualTo(getterValueButUnitDoesntExist);
}
Also used : Position(bwapi.Position) Unit(bwapi.Unit)

Aggregations

Unit (bwapi.Unit)73 GameState (ecgberht.GameState)36 Test (org.junit.Test)21 Position (bwapi.Position)14 TilePosition (bwapi.TilePosition)10 BaseLocation (bwta.BaseLocation)7 Pair (bwapi.Pair)6 UnitType (bwapi.UnitType)6 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Player (bwapi.Player)3 EnemyBuilding (ecgberht.EnemyBuilding)3 RaceUtils.createMockRace (BWJSAL.utils.RaceUtils.createMockRace)2 UnitTypeUtils.mockResourceDepotUnitType (BWJSAL.utils.UnitTypeUtils.mockResourceDepotUnitType)2 Race (bwapi.Race)2 Squad (ecgberht.Squad)2 Game (bwapi.Game)1 UnitCommand (bwapi.UnitCommand)1 Region (bwta.Region)1 Random (java.util.Random)1