Search in sources :

Example 6 with Squad

use of ecgberht.Squad in project Ecgberht by Jabbo16.

the class CheckPerimeter method execute.

@Override
public State execute() {
    try {
        gameState.enemyInBase.clear();
        gameState.defense = false;
        Set<Unit> enemyInvaders = new TreeSet<>(gameState.enemyCombatUnitMemory);
        for (UnitInfo u : gameState.unitStorage.getEnemyUnits().values().stream().filter(u -> u.unitType.isBuilding()).collect(Collectors.toSet())) {
            if (u.unitType.canAttack() || u.unitType == UnitType.Protoss_Pylon || u.unitType.canProduce() || u.unitType.isRefinery()) {
                enemyInvaders.add(u.unit);
            }
        }
        for (Unit u : enemyInvaders) {
            UnitType uType = u.getType();
            if (u instanceof Building || ((uType.canAttack() || uType.isSpellcaster() || u instanceof Loadable) && uType != UnitType.Zerg_Scourge && uType != UnitType.Terran_Valkyrie && uType != UnitType.Protoss_Corsair && !(u instanceof Overlord))) {
                Area enemyArea = gameState.bwem.getMap().getArea(u.getTilePosition());
                if (enemyArea != null) {
                    Area myMainArea = gameState.mainCC != null ? gameState.mainCC.first.getArea() : null;
                    Area myNatArea = gameState.naturalArea;
                    for (Base b : gameState.CCs.keySet()) {
                        if (!b.getArea().equals(enemyArea))
                            continue;
                        if ((myMainArea != null && !b.getArea().equals(myMainArea) && (myNatArea != null && !b.getArea().equals(myNatArea))))
                            continue;
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Map.Entry<SCV, Building> c : gameState.workerTask.entrySet()) {
                    int dist = c.getValue() instanceof CommandCenter ? 500 : 200;
                    if (Util.broodWarDistance(u.getPosition(), c.getValue().getPosition()) <= dist) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Unit c : gameState.CCs.values()) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 500) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Unit c : gameState.DBs.keySet()) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Unit c : gameState.SBs) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (ResearchingFacility c : gameState.UBs) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                if (!gameState.getStrat().name.equals("ProxyBBS") && !gameState.getStrat().name.equals("ProxyEightRax")) {
                    for (Unit c : gameState.MBs) {
                        if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                            gameState.enemyInBase.add(u);
                            break;
                        }
                    }
                }
            }
        }
        if (!gameState.enemyInBase.isEmpty()) {
            /*if ((((GameState) gameState).getArmySize() >= 50 && ((GameState) gameState).getArmySize() / ((GameState) gameState).enemyInBase.size() > 10)) {
                    return State.FAILURE;
                }*/
            gameState.defense = true;
            return State.SUCCESS;
        }
        int cFrame = gameState.frameCount;
        List<Worker> toDelete = new ArrayList<>();
        for (Worker u : gameState.workerDefenders.keySet()) {
            if (u.getLastCommandFrame() == cFrame)
                continue;
            Position closestDefense;
            if (gameState.learningManager.isNaughty()) {
                if (!gameState.DBs.isEmpty()) {
                    closestDefense = gameState.DBs.keySet().iterator().next().getPosition();
                    u.move(closestDefense);
                    toDelete.add(u);
                    continue;
                }
            }
            closestDefense = gameState.getNearestCC(u.getPosition(), false);
            if (closestDefense != null) {
                u.move(closestDefense);
                toDelete.add(u);
            }
        }
        for (Worker u : toDelete) {
            u.stop(false);
            gameState.workerDefenders.remove(u);
            gameState.workerIdle.add(u);
        }
        for (Squad u : gameState.sqManager.squads.values()) {
            if (u.status == Status.DEFENSE) {
                Position closestCC = gameState.getNearestCC(u.getSquadCenter(), false);
                if (closestCC != null) {
                    Area squad = gameState.bwem.getMap().getArea(u.getSquadCenter().toTilePosition());
                    Area regCC = gameState.bwem.getMap().getArea(closestCC.toTilePosition());
                    if (squad != null && regCC != null) {
                        if (!squad.equals(regCC)) {
                            if (!gameState.DBs.isEmpty() && gameState.CCs.size() == 1) {
                                u.giveMoveOrder(gameState.DBs.keySet().iterator().next().getPosition());
                            } else {
                                u.giveMoveOrder(Util.getClosestChokepoint(u.getSquadCenter()).getCenter().toPosition());
                            }
                            u.status = Status.IDLE;
                            u.attack = null;
                            continue;
                        }
                    }
                    u.status = Status.IDLE;
                    u.attack = null;
                    continue;
                }
                u.status = Status.IDLE;
                u.attack = null;
            }
        }
        gameState.defense = false;
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : java.util(java.util) Util(ecgberht.Util.Util) Area(bwem.Area) UnitInfo(ecgberht.UnitInfo) Squad(ecgberht.Squad) Collectors(java.util.stream.Collectors) Status(ecgberht.Squad.Status) Conditional(org.iaie.btree.task.leaf.Conditional) UnitType(org.openbw.bwapi4j.type.UnitType) org.openbw.bwapi4j.unit(org.openbw.bwapi4j.unit) GameState(ecgberht.GameState) Base(bwem.Base) State(org.iaie.btree.BehavioralTree.State) Position(org.openbw.bwapi4j.Position) Position(org.openbw.bwapi4j.Position) Base(bwem.Base) UnitInfo(ecgberht.UnitInfo) Area(bwem.Area) UnitType(org.openbw.bwapi4j.type.UnitType) Squad(ecgberht.Squad)

Example 7 with Squad

use of ecgberht.Squad 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.enemyInBase) {
            if (u.isFlying() || ((PlayerUnit) u).isCloaked())
                continue;
            if (!cannon_rush && (u instanceof Pylon || u instanceof PhotonCannon))
                cannon_rush = true;
            air_only = false;
        }
        Set<UnitInfo> friends = new TreeSet<>();
        for (Squad s : gameState.sqManager.squads.values()) friends.addAll(s.members);
        boolean bunker = false;
        if (!gameState.DBs.isEmpty()) {
            for (Bunker b : gameState.DBs.keySet()) {
                friends.add(gameState.unitStorage.getAllyUnits().get(b));
            }
            bunker = true;
        }
        int defenders = 6;
        if (gameState.enemyInBase.size() == 1 && gameState.enemyInBase.iterator().next() instanceof Worker) {
            defenders = 1;
        }
        MutablePair<Boolean, Boolean> battleWin = new MutablePair<>(true, false);
        if (defenders != 1 && IntelligenceAgency.enemyIsRushing()) {
            if (gameState.enemyInBase.size() + friends.size() < 40) {
                battleWin = gameState.sim.simulateDefenseBattle(friends, gameState.enemyInBase, 150, bunker);
            }
        // if (gameState.enemyInBase.size() >= 3 * friends.size()) battleWin.first = false;
        }
        if (cannon_rush)
            battleWin.first = false;
        int frame = gameState.frameCount;
        int notFound = 0;
        if (!air_only && ((!battleWin.first || battleWin.second) || defenders == 1)) {
            while (gameState.workerDefenders.size() + notFound < defenders && !gameState.workerIdle.isEmpty()) {
                Worker closestWorker = null;
                Position chosen = gameState.attackPosition;
                for (Worker u : gameState.workerIdle) {
                    if (u.getLastCommandFrame() == frame)
                        continue;
                    if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
                        closestWorker = u;
                    }
                }
                if (closestWorker != null) {
                    gameState.workerDefenders.put(closestWorker, null);
                    gameState.workerIdle.remove(closestWorker);
                } else
                    notFound++;
            }
            notFound = 0;
            while (gameState.workerDefenders.size() + notFound < defenders && !gameState.workerMining.isEmpty()) {
                Worker closestWorker = null;
                Position chosen = gameState.attackPosition;
                for (Entry<Worker, MineralPatch> u : gameState.workerMining.entrySet()) {
                    if (u.getKey().getLastCommandFrame() == frame)
                        continue;
                    if ((closestWorker == null || u.getKey().getDistance(chosen) < closestWorker.getDistance(chosen))) {
                        closestWorker = u.getKey();
                    }
                }
                if (closestWorker != null) {
                    if (gameState.workerMining.containsKey(closestWorker)) {
                        MineralPatch mineral = gameState.workerMining.get(closestWorker);
                        gameState.workerDefenders.put(closestWorker, null);
                        if (gameState.mineralsAssigned.containsKey(mineral)) {
                            gameState.mining--;
                            gameState.mineralsAssigned.put(mineral, gameState.mineralsAssigned.get(mineral) - 1);
                        }
                        gameState.workerMining.remove(closestWorker);
                    }
                } else
                    notFound++;
            }
            for (Entry<Worker, Position> u : gameState.workerDefenders.entrySet()) {
                if (frame == u.getKey().getLastCommandFrame())
                    continue;
                if (gameState.attackPosition != null) {
                    gameState.workerDefenders.put(u.getKey(), gameState.attackPosition);
                    if (gameState.enemyInBase.size() == 1 && gameState.enemyInBase.iterator().next() instanceof Worker) {
                        Unit scouter = gameState.enemyInBase.iterator().next();
                        Unit lastTarget = u.getKey().getOrderTarget();
                        if (lastTarget != null && lastTarget.equals(scouter))
                            continue;
                        u.getKey().attack(scouter);
                    } else {
                        Position closestDefense = null;
                        if (gameState.learningManager.isNaughty()) {
                            if (!gameState.DBs.isEmpty())
                                closestDefense = gameState.DBs.keySet().iterator().next().getPosition();
                            if (closestDefense == null)
                                closestDefense = gameState.getNearestCC(u.getKey().getPosition(), false);
                            if (closestDefense != null && u.getKey().getDistance(closestDefense) > UnitType.Terran_Marine.groundWeapon().maxRange() * 0.95) {
                                u.getKey().move(closestDefense);
                                continue;
                            }
                        }
                        Unit toAttack = gameState.getUnitToAttack(u.getKey(), gameState.enemyInBase);
                        if (toAttack != null) {
                            Unit lastTarget = u.getKey().getOrderTarget();
                            if (lastTarget != null && lastTarget.equals(toAttack))
                                continue;
                            u.getKey().attack(toAttack);
                        } else {
                            Position lastTargetPosition = u.getKey().getOrderTargetPosition();
                            if (lastTargetPosition != null && lastTargetPosition.equals(gameState.attackPosition))
                                continue;
                            u.getKey().attack(gameState.attackPosition);
                        }
                    }
                }
            }
        } else if (!gameState.getStrat().name.equals("ProxyBBS") && !gameState.getStrat().name.equals("ProxyEightRax")) {
            for (Entry<Integer, Squad> u : gameState.sqManager.squads.entrySet()) {
                if (gameState.attackPosition != null) {
                    u.getValue().giveAttackOrder(gameState.attackPosition);
                    u.getValue().status = Status.DEFENSE;
                } else {
                    u.getValue().status = Status.IDLE;
                    u.getValue().attack = null;
                }
            }
        }
        gameState.attackPosition = null;
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : Position(org.openbw.bwapi4j.Position) UnitInfo(ecgberht.UnitInfo) MutablePair(ecgberht.Util.MutablePair) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) Squad(ecgberht.Squad)

Example 8 with Squad

use of ecgberht.Squad in project Ecgberht by Jabbo16.

the class VesselAgent method chooseVesselSquad.

private Squad chooseVesselSquad() {
    Squad chosen = null;
    double scoreMax = Double.MIN_VALUE;
    for (Squad s : getGs().sqManager.squads.values()) {
        double dist = unitInfo.getDistance(s.getSquadCenter());
        double score = -Math.pow(s.members.size(), 3) / dist;
        if (chosen == null || score > scoreMax) {
            chosen = s;
            scoreMax = dist;
        }
    }
    return chosen;
}
Also used : Squad(ecgberht.Squad)

Aggregations

Squad (ecgberht.Squad)8 GameState (ecgberht.GameState)4 UnitInfo (ecgberht.UnitInfo)4 Position (bwapi.Position)2 Unit (bwapi.Unit)2 Area (bwem.Area)2 Base (bwem.Base)2 ArrayList (java.util.ArrayList)2 Position (org.openbw.bwapi4j.Position)2 Pair (bwapi.Pair)1 TilePosition (bwapi.TilePosition)1 UnitCommand (bwapi.UnitCommand)1 Region (bwta.Region)1 EnemyBuilding (ecgberht.EnemyBuilding)1 Status (ecgberht.Squad.Status)1 MutablePair (ecgberht.Util.MutablePair)1 Util (ecgberht.Util.Util)1 java.util (java.util)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1