Search in sources :

Example 1 with UnitCommand

use of bwapi.UnitCommand in project Ecgberht by Jabbo16.

the class SendDefenders method execute.

@Override
public State execute() {
    try {
        boolean air_only = true;
        boolean cannon_rush = false;
        for (Unit u : ((GameState) this.handler).enemyInBase) {
            if (u.isFlying() || u.isCloaked()) {
                continue;
            }
            if (!cannon_rush) {
                if (u.getType() == UnitType.Protoss_Pylon || u.getType() == UnitType.Protoss_Photon_Cannon) {
                    cannon_rush = true;
                }
            }
            air_only = false;
        }
        Set<Unit> friends = new HashSet<Unit>();
        for (Squad s : ((GameState) this.handler).squads.values()) {
            for (Unit u : s.members) {
                friends.add(u);
            }
        }
        boolean bunker = false;
        if (!((GameState) this.handler).DBs.isEmpty()) {
            for (Unit u : ((GameState) this.handler).DBs.keySet()) {
                friends.add(u);
            }
            bunker = true;
        }
        int defenders = 6;
        Iterator<Unit> it = ((GameState) this.handler).enemyInBase.iterator();
        if (((GameState) this.handler).enemyInBase.size() == 1 && it.next().getType().isWorker()) {
            defenders = 1;
        }
        Pair<Boolean, Boolean> battleWin = new Pair<>(true, false);
        if (defenders != 1) {
            if (((GameState) this.handler).enemyInBase.size() + friends.size() < 25) {
                battleWin = ((GameState) this.handler).simulateDefenseBattle(friends, ((GameState) this.handler).enemyInBase, 150, bunker);
            }
            if (((GameState) this.handler).enemyInBase.size() >= 2 * friends.size()) {
                battleWin.first = false;
            }
        }
        if (cannon_rush) {
            battleWin.first = false;
        }
        int frame = ((GameState) this.handler).frameCount;
        if (!air_only && ((!battleWin.first || battleWin.second) || defenders == 1)) {
            while (((GameState) this.handler).workerDefenders.size() < defenders && !((GameState) this.handler).workerIdle.isEmpty()) {
                Unit closestWorker = null;
                Position chosen = ((GameState) this.handler).attackPosition;
                for (Unit u : ((GameState) this.handler).workerIdle) {
                    if (u.getLastCommandFrame() == frame) {
                        continue;
                    }
                    if ((closestWorker == null || ((GameState) this.handler).broodWarDistance(u.getPosition(), chosen) < ((GameState) this.handler).broodWarDistance(closestWorker.getPosition(), chosen))) {
                        closestWorker = u;
                    }
                }
                if (closestWorker != null) {
                    ((GameState) this.handler).workerDefenders.add(closestWorker);
                    ((GameState) this.handler).workerIdle.remove(closestWorker);
                }
            }
            while (((GameState) this.handler).workerDefenders.size() < defenders && !((GameState) this.handler).workerMining.isEmpty()) {
                Unit closestWorker = null;
                Position chosen = ((GameState) this.handler).attackPosition;
                for (Unit u : ((GameState) this.handler).workerMining.keySet()) {
                    if (u.getLastCommandFrame() == frame) {
                        continue;
                    }
                    if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
                        closestWorker = u;
                    }
                }
                if (closestWorker != null) {
                    if (((GameState) this.handler).workerMining.containsKey(closestWorker)) {
                        Unit mineral = ((GameState) this.handler).workerMining.get(closestWorker);
                        ((GameState) this.handler).workerDefenders.add(closestWorker);
                        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).workerMining.remove(closestWorker);
                    }
                }
            }
            for (Unit u : ((GameState) this.handler).workerDefenders) {
                if (frame == u.getLastCommandFrame()) {
                    continue;
                }
                if (((GameState) this.handler).attackPosition != null) {
                    if (u.isIdle()) {
                        if (((GameState) this.handler).enemyInBase.size() == 1) {
                            u.attack(((GameState) this.handler).enemyInBase.iterator().next());
                        } else {
                            Unit toAttack = ((GameState) this.handler).getUnitToAttack(u, ((GameState) this.handler).enemyInBase);
                            if (toAttack != null) {
                                Unit lastTarget = u.getOrderTarget();
                                if (lastTarget != null && lastTarget.exists()) {
                                    if (lastTarget.equals(toAttack)) {
                                        continue;
                                    }
                                }
                                UnitCommand lastUnitCommand = u.getLastCommand();
                                if (lastUnitCommand != null) {
                                    if (lastUnitCommand.getTarget() != null && lastUnitCommand.getTarget().exists())
                                        if (lastUnitCommand.getTarget().equals(toAttack)) {
                                            continue;
                                        }
                                }
                                u.attack(toAttack);
                            } else {
                                u.attack(((GameState) this.handler).attackPosition);
                            }
                            continue;
                        }
                    }
                }
            }
        } else {
            if (((GameState) this.handler).strat.name != "ProxyBBS") {
                for (Entry<String, Squad> u : ((GameState) this.handler).squads.entrySet()) {
                    if (((GameState) this.handler).attackPosition != null) {
                        // if(u.getValue().estado == Status.IDLE || !((GameState)this.handler).attackPosition.equals(u.getValue().attack)) {
                        u.getValue().giveAttackOrder(((GameState) this.handler).attackPosition);
                        u.getValue().status = Status.DEFENSE;
                        continue;
                    // }
                    } else {
                        u.getValue().status = Status.IDLE;
                        u.getValue().attack = Position.None;
                        continue;
                    }
                }
            }
        }
        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) UnitCommand(bwapi.UnitCommand) Squad(ecgberht.Squad) HashSet(java.util.HashSet) Pair(bwapi.Pair)

Aggregations

Pair (bwapi.Pair)1 Position (bwapi.Position)1 Unit (bwapi.Unit)1 UnitCommand (bwapi.UnitCommand)1 GameState (ecgberht.GameState)1 Squad (ecgberht.Squad)1 HashSet (java.util.HashSet)1