Search in sources :

Example 46 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class CollectMineral method execute.

@Override
public State execute() {
    try {
        Unit chosen = ((GameState) this.handler).chosenWorker;
        if (!((GameState) this.handler).mineralsAssigned.isEmpty()) {
            Unit closestMineral = null;
            for (Entry<Unit, Integer> m : ((GameState) this.handler).mineralsAssigned.entrySet()) {
                if ((closestMineral == null || chosen.getDistance(m.getKey()) < chosen.getDistance(closestMineral)) && m.getValue() < 2) {
                    closestMineral = m.getKey();
                }
            }
            if (closestMineral != null) {
                ((GameState) this.handler).mineralsAssigned.put(closestMineral, ((GameState) this.handler).mineralsAssigned.get(closestMineral) + 1);
                ((GameState) this.handler).workerIdle.remove(chosen);
                ((GameState) this.handler).workerMining.put(chosen, closestMineral);
                chosen.gather(closestMineral, false);
                ((GameState) this.handler).chosenWorker = null;
                ((GameState) this.handler).mining++;
                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 47 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseRepairer method execute.

@Override
public State execute() {
    try {
        Unit closestWorker = null;
        Position chosen = ((GameState) this.handler).chosenBuildingRepair.getPosition();
        int frame = ((GameState) this.handler).frameCount;
        for (Unit u : ((GameState) this.handler).workerIdle) {
            if (u.getLastCommandFrame() == frame) {
                continue;
            }
            if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
                closestWorker = u;
            }
        }
        for (Unit u : ((GameState) this.handler).workerMining.keySet()) {
            if (u.getLastCommandFrame() == frame) {
                continue;
            }
            if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen)) && !u.isCarryingMinerals()) {
                closestWorker = u;
            }
        }
        if (closestWorker != null) {
            ((GameState) this.handler).chosenRepairer = closestWorker;
            return State.SUCCESS;
        }
        return State.FAILURE;
    } 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)

Example 48 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class Repair method execute.

@Override
public State execute() {
    try {
        if (((GameState) this.handler).chosenRepairer.repair(((GameState) this.handler).chosenBuildingRepair)) {
            if (((GameState) this.handler).workerIdle.contains(((GameState) this.handler).chosenRepairer)) {
                ((GameState) this.handler).workerIdle.remove(((GameState) this.handler).chosenRepairer);
            } else {
                if (((GameState) this.handler).workerMining.containsKey(((GameState) this.handler).chosenRepairer)) {
                    Unit mineral = ((GameState) this.handler).workerMining.get(((GameState) this.handler).chosenRepairer);
                    ((GameState) this.handler).workerMining.remove(((GameState) this.handler).chosenRepairer);
                    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).repairerTask.add(new Pair<Unit, Unit>(((GameState) this.handler).chosenRepairer, ((GameState) this.handler).chosenBuildingRepair));
            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 49 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class CheckScan method execute.

@Override
public State execute() {
    try {
        if (((GameState) this.handler).CSs.isEmpty()) {
            return State.FAILURE;
        }
        if (((GameState) this.handler).getGame().elapsedTime() - ((GameState) this.handler).startCount > 1) {
            for (Unit u : ((GameState) this.handler).enemyCombatUnitMemory) {
                if ((u.isCloaked() || u.isBurrowed()) && !u.isDetected() && u.getType().canAttack()) {
                    ((GameState) this.handler).checkScan = u.getTilePosition();
                    return State.SUCCESS;
                }
            }
        }
        List<BaseLocation> valid = new ArrayList<>();
        for (BaseLocation b : ((GameState) this.handler).EnemyBLs) {
            if (((GameState) this.handler).getGame().isVisible(b.getTilePosition()) || b.isIsland()) {
                continue;
            }
            if (((GameState) this.handler).enemyBase != null) {
                if (((GameState) this.handler).enemyBase.getTilePosition().equals(b.getTilePosition())) {
                    continue;
                }
            }
            valid.add(b);
        }
        if (valid.isEmpty()) {
            return State.FAILURE;
        }
        for (Unit u : ((GameState) this.handler).CSs) {
            if (u.getEnergy() == 200) {
                Random random = new Random();
                ((GameState) this.handler).checkScan = valid.get(random.nextInt(valid.size())).getTilePosition();
                return State.SUCCESS;
            }
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) GameState(ecgberht.GameState) Unit(bwapi.Unit) BaseLocation(bwta.BaseLocation)

Example 50 with Unit

use of bwapi.Unit in project Ecgberht by Jabbo16.

the class ChooseWorker method execute.

@Override
public State execute() {
    try {
        Unit closestWorker = null;
        int frame = ((GameState) this.handler).frameCount;
        Position chosen = ((GameState) this.handler).chosenPosition.toPosition();
        if (!((GameState) this.handler).workerIdle.isEmpty()) {
            for (Unit u : ((GameState) this.handler).workerIdle) {
                if (u.getLastCommandFrame() == frame) {
                    continue;
                }
                if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
                    closestWorker = u;
                }
            }
        }
        if (!((GameState) this.handler).workerMining.isEmpty()) {
            for (Unit u : ((GameState) this.handler).workerMining.keySet()) {
                if (u.getLastCommandFrame() == frame) {
                    continue;
                }
                if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen)) && !u.isCarryingMinerals()) {
                    closestWorker = u;
                }
            }
        }
        if (closestWorker != null) {
            ((GameState) this.handler).chosenWorker = closestWorker;
            return State.SUCCESS;
        }
        return State.FAILURE;
    } 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)

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