Search in sources :

Example 21 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseSiegeMode 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.Tank_Siege_Mode) && u.canResearch(TechType.Tank_Siege_Mode) && !u.isResearching() && !u.isUpgrading()) {
                ((GameState) this.handler).chosenUnitUpgrader = u;
                ((GameState) this.handler).chosenResearch = TechType.Tank_Siege_Mode;
                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 22 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class CheckPerimeter method execute.

@Override
public State execute() {
    try {
        ((GameState) this.handler).enemyInBase.clear();
        ((GameState) this.handler).defense = false;
        List<Unit> enemyInvaders = new ArrayList<>();
        enemyInvaders.addAll(((GameState) this.handler).enemyCombatUnitMemory);
        for (EnemyBuilding u : ((GameState) this.handler).enemyBuildingMemory.values()) {
            if (u.type.canAttack() || u.type == UnitType.Protoss_Pylon || u.type.canProduce() || u.type.isRefinery()) {
                enemyInvaders.add(u.unit);
            }
        }
        for (Unit u : enemyInvaders) {
            if ((u.getType().canAttack() || u.getType() == UnitType.Protoss_Pylon) && u.getType() != UnitType.Zerg_Scourge && u.getType() != UnitType.Protoss_Corsair) {
                for (Unit c : ((GameState) this.handler).CCs.values()) {
                    if (((GameState) this.handler).broodWarDistance(u.getPosition(), c.getPosition()) < 500) {
                        ((GameState) this.handler).enemyInBase.add(u);
                        continue;
                    }
                }
                for (Unit c : ((GameState) this.handler).DBs.keySet()) {
                    if (((GameState) this.handler).broodWarDistance(u.getPosition(), c.getPosition()) < 200) {
                        ((GameState) this.handler).enemyInBase.add(u);
                        continue;
                    }
                }
                for (Unit c : ((GameState) this.handler).SBs) {
                    if (((GameState) this.handler).broodWarDistance(u.getPosition(), c.getPosition()) < 200) {
                        ((GameState) this.handler).enemyInBase.add(u);
                        continue;
                    }
                }
                for (Unit c : ((GameState) this.handler).MBs) {
                    if (((GameState) this.handler).broodWarDistance(u.getPosition(), c.getPosition()) < 200) {
                        ((GameState) this.handler).enemyInBase.add(u);
                        continue;
                    }
                }
            }
        }
        boolean overlordCheck = true;
        for (Unit u : ((GameState) this.handler).enemyInBase) {
            if (u.getType().canAttack() || u.getType() == UnitType.Protoss_Shuttle || u.getType() == UnitType.Terran_Dropship) {
                overlordCheck = false;
                break;
            }
        }
        if (!((GameState) this.handler).enemyInBase.isEmpty() && !overlordCheck) {
            if ((((GameState) this.handler).getArmySize() >= 50 && ((GameState) this.handler).getArmySize() / ((GameState) this.handler).enemyInBase.size() > 10)) {
                return State.FAILURE;
            }
            ((GameState) this.handler).defense = true;
            return State.SUCCESS;
        }
        int cFrame = ((GameState) this.handler).frameCount;
        for (Unit u : ((GameState) this.handler).workerDefenders) {
            if (u.getLastCommandFrame() == cFrame) {
                continue;
            }
            Position closestCC = ((GameState) this.handler).getNearestCC(u.getPosition());
            if (closestCC != null) {
                if (!BWTA.getRegion(u.getPosition()).getCenter().equals(BWTA.getRegion(closestCC).getCenter())) {
                    u.move(closestCC);
                }
            }
        }
        for (Squad u : ((GameState) this.handler).squads.values()) {
            if (u.status == Status.DEFENSE) {
                Position closestCC = ((GameState) this.handler).getNearestCC(((GameState) this.handler).getSquadCenter(u));
                if (closestCC != null) {
                    Region squad = BWTA.getRegion(((GameState) this.handler).getSquadCenter(u));
                    Region regCC = BWTA.getRegion(closestCC);
                    if (squad != null && regCC != null) {
                        if (!squad.getCenter().equals(regCC.getCenter())) {
                            if (!((GameState) this.handler).DBs.isEmpty() && ((GameState) this.handler).CCs.size() == 1) {
                                u.giveMoveOrder(((GameState) this.handler).DBs.keySet().iterator().next().getPosition());
                            } else {
                                u.giveMoveOrder(BWTA.getNearestChokepoint(((GameState) this.handler).getSquadCenter(u)).getCenter());
                            }
                            u.status = Status.IDLE;
                            u.attack = Position.None;
                            continue;
                        }
                    }
                    u.status = Status.IDLE;
                    u.attack = Position.None;
                    continue;
                }
                u.status = Status.IDLE;
                u.attack = Position.None;
                continue;
            }
        }
        ((GameState) this.handler).defense = false;
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : Position(bwapi.Position) ArrayList(java.util.ArrayList) Region(bwta.Region) GameState(ecgberht.GameState) Unit(bwapi.Unit) Squad(ecgberht.Squad) EnemyBuilding(ecgberht.EnemyBuilding)

Example 23 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseStimUpgrade 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) && u.canResearch(TechType.Stim_Packs) && !u.isResearching() && !u.isUpgrading()) {
                ((GameState) this.handler).chosenUnitUpgrader = u;
                ((GameState) this.handler).chosenResearch = TechType.Stim_Packs;
                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 24 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class InformationManagerAcceptanceTest method resourceDepotToBaseLocationMappingCorrectlyKeptUpToDate.

/**
 * Verify that enemy base location manually set via setBaseAsEnemy is removed
 * when a command center that was later observed is destroyed.
 *
 * This test verifies that base locations can be tied to resource depots even
 * if the base location was already marked as an enemy location.
 */
@Test
public void resourceDepotToBaseLocationMappingCorrectlyKeptUpToDate() {
    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();
    // manually set base as enemy
    this.target.setBaseAsEnemy(enemyStartingLocation);
    assertThat(this.target.getEnemyBases()).as("sanctity test that setBaseAsEnemy actual sets the base as an enemy base").containsExactly(enemyStartingLocation);
    // observe enemy command center in enemy base
    final TilePosition commandCenterPosition = mock(TilePosition.class);
    setNearestBaseLocationOnMock(this.bwtaWrapper, commandCenterPosition, enemyStartingLocation);
    final Unit commandCenter = UnitBuilder.mockUnit(this.enemy, mockResourceDepotUnitType(), commandCenterPosition);
    this.target.onUnitDiscover(commandCenter);
    // command center destroyed
    this.target.onUnitDestroy(commandCenter);
    // verify location is not enemy
    assertThat(this.target.getEnemyBases()).as("because all bases mapped to base location have been destroyed, no more bases for enemy").isEmpty();
}
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 25 with Unit

use of bwapi.Unit in project BWJSAL by RobinsonMann.

the class UnitTrackerTest method getLastSeenTime_unitDoesNotExistButIsTracked_returnsLastSeenFrame.

@Test
public void getLastSeenTime_unitDoesNotExistButIsTracked_returnsLastSeenFrame() {
    final Unit unit = createMockUnit(true);
    // Unit exists, so last seen frame count should now be set to 0
    when(this.mockGame.getFrameCount()).thenReturn(0);
    this.target.onUnitDiscover(unit, 0);
    this.target.onFrame(0);
    // Unit exists, so last seen frame count should be set to 1
    when(this.mockGame.getFrameCount()).thenReturn(1);
    this.target.onFrame(1);
    when(unit.exists()).thenReturn(false);
    // Unit no longer exists, so last seen frame count should still be 1
    when(this.mockGame.getFrameCount()).thenReturn(2);
    this.target.onFrame(2);
    assertThat(this.target.getLastSeenTime(unit)).isPresent().contains(1);
}
Also used : Unit(bwapi.Unit) Test(org.junit.Test)

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