Search in sources :

Example 6 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseBuildingLot method execute.

@Override
public State execute() {
    try {
        Unit savedTurret = null;
        List<Unit> aux = new ArrayList<Unit>();
        for (Unit b : ((GameState) this.handler).buildingLot) {
            if (!b.isUnderAttack()) {
                if (b.getType() == UnitType.Terran_Bunker) {
                    ((GameState) this.handler).chosenBuildingLot = b;
                    return State.SUCCESS;
                }
                if (b.getType() == UnitType.Terran_Missile_Turret) {
                    savedTurret = b;
                }
                ((GameState) this.handler).chosenBuildingLot = b;
            } else {
                if ((double) b.getHitPoints() / (double) b.getType().maxHitPoints() <= 0.1) {
                    b.cancelConstruction();
                    aux.add(b);
                }
            }
        }
        ((GameState) this.handler).buildingLot.removeAll(aux);
        if (savedTurret != null) {
            ((GameState) this.handler).chosenBuildingLot = savedTurret;
            return State.SUCCESS;
        }
        if (((GameState) this.handler).chosenBuildingLot != null) {
            return State.SUCCESS;
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : ArrayList(java.util.ArrayList) GameState(ecgberht.GameState) Unit(bwapi.Unit)

Example 7 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class FinishBuilding method execute.

@Override
public State execute() {
    try {
        Unit chosen = ((GameState) this.handler).chosenWorker;
        if (chosen.rightClick(((GameState) this.handler).chosenBuildingLot)) {
            if (((GameState) this.handler).workerIdle.contains(chosen)) {
                ((GameState) this.handler).workerIdle.remove(chosen);
            } else {
                if (((GameState) this.handler).workerMining.containsKey(chosen)) {
                    Unit mineral = ((GameState) this.handler).workerMining.get(chosen);
                    ((GameState) this.handler).workerMining.remove(chosen);
                    if (((GameState) this.handler).mineralsAssigned.containsKey(mineral)) {
                        ((GameState) this.handler).mining--;
                        ((GameState) this.handler).mineralsAssigned.put(mineral, ((GameState) this.handler).mineralsAssigned.get(mineral) - 1);
                    }
                }
            }
            ((GameState) this.handler).workerTask.add(new Pair<Unit, Unit>(chosen, ((GameState) this.handler).chosenBuildingLot));
            ((GameState) this.handler).chosenWorker = null;
            ((GameState) this.handler).buildingLot.remove(((GameState) this.handler).chosenBuildingLot);
            ((GameState) this.handler).chosenBuildingLot = null;
            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 8 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class CheckHarasserAttacked method execute.

@Override
public State execute() {
    try {
        Unit attacker = null;
        int workers = 0;
        Set<Unit> attackers = new HashSet<>();
        // Thanks to @N00byEdge to the cleaner code
        for (Unit u : ((GameState) this.handler).getGame().enemy().getUnits()) {
            if (!u.getType().isBuilding() && u.getType().canAttack()) {
                Unit target = (u.getTarget() == null ? u.getOrderTarget() : u.getTarget());
                if (target != null && target.equals(((GameState) this.handler).chosenHarasser)) {
                    if (u.getType().isWorker()) {
                        workers++;
                        attacker = u;
                    }
                    attackers.add(u);
                    continue;
                }
            }
        }
        if (workers > 1) {
            ((GameState) this.handler).EI.defendHarass = true;
        }
        if (attackers.isEmpty()) {
            if (!((GameState) this.handler).getGame().isVisible(((GameState) this.handler).enemyBase.getTilePosition()) && ((GameState) this.handler).chosenUnitToHarass == null) {
                ((GameState) this.handler).chosenHarasser.move(((GameState) this.handler).enemyBase.getPosition());
            }
            return State.SUCCESS;
        } else {
            boolean winHarass = ((GameState) this.handler).simulateHarass(((GameState) this.handler).chosenHarasser, attackers, 70);
            if (winHarass) {
                if (workers == 1 && !attacker.equals(((GameState) this.handler).chosenUnitToHarass)) {
                    ((GameState) this.handler).chosenHarasser.attack(attacker);
                    ((GameState) this.handler).chosenUnitToHarass = attacker;
                    return State.SUCCESS;
                }
            } else {
                if (((GameState) this.handler).chosenHarasser.getHitPoints() <= 15) {
                    ((GameState) this.handler).workerIdle.add(((GameState) this.handler).chosenHarasser);
                    ((GameState) this.handler).chosenHarasser.stop();
                    ((GameState) this.handler).chosenHarasser = null;
                    ((GameState) this.handler).chosenUnitToHarass = null;
                } else {
                    Position kite = getGs().kiteAway(((GameState) this.handler).chosenHarasser, attackers);
                    ((GameState) this.handler).chosenHarasser.move(kite);
                    ((GameState) this.handler).chosenUnitToHarass = null;
                }
                return State.FAILURE;
            }
        }
        return State.SUCCESS;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : Position(bwapi.Position) GameState(ecgberht.GameState) Unit(bwapi.Unit) HashSet(java.util.HashSet)

Example 9 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class CheckResourcesBuilding method execute.

@Override
public State execute() {
    try {
        Pair<Integer, Integer> cash = ((GameState) this.handler).getCash();
        Unit chosen = ((GameState) this.handler).chosenWorker;
        TilePosition start = chosen.getTilePosition();
        TilePosition end = ((GameState) this.handler).chosenPosition;
        Position realEnd = ((GameState) this.handler).getCenterFromBuilding(end.toPosition(), ((GameState) this.handler).chosenToBuild);
        if (cash.first + ((GameState) this.handler).getMineralsWhenReaching(chosen, start, realEnd.toTilePosition()) >= (((GameState) this.handler).chosenToBuild.mineralPrice() + ((GameState) this.handler).deltaCash.first) && cash.second >= (((GameState) this.handler).chosenToBuild.gasPrice()) + ((GameState) this.handler).deltaCash.second) {
            return State.SUCCESS;
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : TilePosition(bwapi.TilePosition) Position(bwapi.Position) TilePosition(bwapi.TilePosition) GameState(ecgberht.GameState) Unit(bwapi.Unit)

Example 10 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseScout method execute.

@Override
public State execute() {
    try {
        for (Unit u : ((GameState) this.handler).workerIdle) {
            ((GameState) this.handler).chosenScout = u;
            ((GameState) this.handler).workerIdle.remove(u);
            break;
        }
        if (((GameState) this.handler).chosenScout == null) {
            for (Unit u : ((GameState) this.handler).workerMining.keySet()) {
                if (!u.isCarryingMinerals()) {
                    ((GameState) this.handler).chosenScout = u;
                    ((GameState) this.handler).workerMining.remove(u);
                }
                break;
            }
        }
        if (((GameState) this.handler).chosenScout != null) {
            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)

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