Search in sources :

Example 26 with UnitInfo

use of ecgberht.UnitInfo 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 27 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class ChoosePosition method execute.

@Override
public State execute() {
    try {
        if (gameState.chosenToBuild == UnitType.None)
            return State.FAILURE;
        Player self = gameState.getPlayer();
        TilePosition origin;
        if (gameState.chosenToBuild.isRefinery()) {
            if (!gameState.vespeneGeysers.isEmpty()) {
                for (Entry<VespeneGeyser, Boolean> g : gameState.vespeneGeysers.entrySet()) {
                    if (!g.getValue()) {
                        gameState.chosenPosition = g.getKey().getTilePosition();
                        return State.SUCCESS;
                    }
                }
            }
        } else if (gameState.chosenToBuild == UnitType.Terran_Command_Center) {
            if (!gameState.islandBases.isEmpty() && gameState.islandCCs.size() < gameState.islandBases.size()) {
                if (gameState.islandExpand)
                    return State.FAILURE;
                for (Agent u : gameState.agents.values()) {
                    if (u instanceof DropShipAgent && u.statusToString().equals("IDLE")) {
                        gameState.islandExpand = true;
                        return State.FAILURE;
                    }
                }
            }
            TilePosition main;
            if (gameState.mainCC != null)
                main = gameState.mainCC.second.getTilePosition();
            else
                main = gameState.getPlayer().getStartLocation();
            List<Base> valid = new ArrayList<>();
            if (gameState.getStrat().name.equals("PlasmaWraithHell")) {
                for (Base b : gameState.specialBLs) {
                    if (!gameState.CCs.containsKey(b)) {
                        gameState.chosenPosition = b.getLocation();
                        return State.SUCCESS;
                    }
                }
            }
            for (Base b : gameState.BLs) {
                if (!gameState.CCs.containsKey(b) && Util.isConnected(b.getLocation(), main))
                    valid.add(b);
            }
            List<Base> remove = new ArrayList<>();
            for (Base b : valid) {
                if (gameState.getGame().getBWMap().isVisible(b.getLocation()) && !gameState.getGame().getBWMap().isBuildable(b.getLocation(), true)) {
                    remove.add(b);
                    continue;
                }
                if (b.getArea() != gameState.naturalArea) {
                    for (Unit u : gameState.enemyCombatUnitMemory) {
                        if (gameState.bwem.getMap().getArea(u.getTilePosition()) == null || !(u instanceof Attacker) || u instanceof Worker) {
                            continue;
                        }
                        if (gameState.bwem.getMap().getArea(u.getTilePosition()).equals(b.getArea())) {
                            remove.add(b);
                            break;
                        }
                    }
                    for (UnitInfo u : gameState.unitStorage.getEnemyUnits().values().stream().filter(u -> u.unitType.isBuilding()).collect(Collectors.toSet())) {
                        if (gameState.bwem.getMap().getArea(u.tileposition) == null)
                            continue;
                        if (gameState.bwem.getMap().getArea(u.tileposition).equals(b.getArea())) {
                            remove.add(b);
                            break;
                        }
                    }
                }
            }
            valid.removeAll(remove);
            if (valid.isEmpty())
                return State.FAILURE;
            gameState.chosenPosition = valid.get(0).getLocation();
            return State.SUCCESS;
        } else {
            if (!gameState.workerBuild.isEmpty()) {
                for (MutablePair<UnitType, TilePosition> w : gameState.workerBuild.values()) {
                    gameState.testMap.updateMap(w.second, w.first, false);
                }
            }
            if (!gameState.chosenToBuild.equals(UnitType.Terran_Bunker) && !gameState.chosenToBuild.equals(UnitType.Terran_Missile_Turret)) {
                if (gameState.getStrat().proxy && gameState.chosenToBuild == UnitType.Terran_Barracks) {
                    origin = gameState.mapCenter.toTilePosition();
                } else if (gameState.mainCC != null && gameState.mainCC.first != null) {
                    origin = gameState.mainCC.first.getLocation();
                } else
                    origin = self.getStartLocation();
            } else if (gameState.chosenToBuild.equals(UnitType.Terran_Missile_Turret)) {
                if (gameState.defendPosition != null)
                    origin = gameState.defendPosition.toTilePosition();
                else if (gameState.DBs.isEmpty()) {
                    origin = Util.getClosestChokepoint(self.getStartLocation().toPosition()).getCenter().toTilePosition();
                } else {
                    origin = gameState.DBs.keySet().stream().findFirst().map(UnitImpl::getTilePosition).orElse(null);
                }
            } else if (gameState.learningManager.isNaughty() && gameState.enemyRace == Race.Zerg) {
                origin = gameState.getBunkerPositionAntiPool();
                if (origin != null) {
                    gameState.testMap = gameState.map.clone();
                    gameState.chosenPosition = origin;
                    return State.SUCCESS;
                } else {
                    origin = gameState.testMap.findBunkerPositionAntiPool();
                    if (origin != null) {
                        gameState.testMap = gameState.map.clone();
                        gameState.chosenPosition = origin;
                        return State.SUCCESS;
                    } else if (gameState.mainCC != null)
                        origin = gameState.mainCC.second.getTilePosition();
                    else
                        origin = gameState.getPlayer().getStartLocation();
                }
            } else if (gameState.Ts.isEmpty()) {
                if (gameState.defendPosition != null && gameState.naturalChoke != null && gameState.defendPosition.equals(gameState.naturalChoke.getCenter().toPosition())) {
                    origin = gameState.testMap.findBunkerPosition(gameState.naturalChoke);
                    if (origin != null) {
                        gameState.testMap = gameState.map.clone();
                        gameState.chosenPosition = origin;
                        return State.SUCCESS;
                    }
                }
                if (gameState.mainChoke != null && !gameState.getStrat().name.equals("MechGreedyFE") && !gameState.getStrat().name.equals("BioGreedyFE") && !gameState.getStrat().name.equals("14CC") && !gameState.getStrat().name.equals("BioMechGreedyFE")) {
                    origin = gameState.testMap.findBunkerPosition(gameState.mainChoke);
                    if (origin != null) {
                        gameState.testMap = gameState.map.clone();
                        gameState.chosenPosition = origin;
                        return State.SUCCESS;
                    } else
                        origin = gameState.mainChoke.getCenter().toTilePosition();
                } else if (gameState.naturalChoke != null) {
                    origin = gameState.testMap.findBunkerPosition(gameState.naturalChoke);
                    if (origin != null) {
                        gameState.testMap = gameState.map.clone();
                        gameState.chosenPosition = origin;
                        return State.SUCCESS;
                    } else
                        origin = gameState.mainChoke.getCenter().toTilePosition();
                } else {
                    origin = gameState.testMap.findBunkerPosition(gameState.mainChoke);
                    if (origin != null) {
                        gameState.testMap = gameState.map.clone();
                        gameState.chosenPosition = origin;
                        return State.SUCCESS;
                    } else
                        origin = gameState.mainChoke.getCenter().toTilePosition();
                }
            } else
                origin = gameState.Ts.stream().findFirst().map(UnitImpl::getTilePosition).orElse(null);
            TilePosition position = gameState.testMap.findPositionNew(gameState.chosenToBuild, origin);
            gameState.testMap = gameState.map.clone();
            if (position != null) {
                gameState.chosenPosition = position;
                return State.SUCCESS;
            }
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : Agent(ecgberht.Agents.Agent) DropShipAgent(ecgberht.Agents.DropShipAgent) Player(org.openbw.bwapi4j.Player) Base(bwem.Base) UnitInfo(ecgberht.UnitInfo) MutablePair(ecgberht.Util.MutablePair) DropShipAgent(ecgberht.Agents.DropShipAgent) TilePosition(org.openbw.bwapi4j.TilePosition) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class VultureAgent method getUnitToAttack.

private UnitInfo getUnitToAttack(Set<UnitInfo> enemies) {
    UnitInfo chosen = null;
    double distB = Double.MAX_VALUE;
    for (UnitInfo u : enemies) {
        if (u.flying || u.unit.isCloaked())
            continue;
        double distA = unitInfo.getDistance(u);
        if (chosen == null || distA < distB) {
            chosen = u;
            distB = distA;
        }
    }
    return chosen;
}
Also used : UnitInfo(ecgberht.UnitInfo)

Example 29 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class VultureAgent method kite.

private void kite() {
    // Position kite = UtilMicro.kiteAway(unit, closeEnemies);
    Optional<UnitInfo> closestUnit = mySim.enemies.stream().min(Comparator.comparing(u -> unitInfo.getDistance(u)));
    Position kite = closestUnit.map(unit1 -> UtilMicro.kiteAwayAlt(unit.getPosition(), unit1.position)).orElse(null);
    if (kite == null || !getGs().getGame().getBWMap().isValidPosition(kite)) {
        kite = UtilMicro.kiteAway(unit, mySim.enemies);
        if (kite == null || !getGs().getGame().getBWMap().isValidPosition(kite)) {
            retreat();
            return;
        }
    }
    UtilMicro.move(unit, kite);
}
Also used : Util(ecgberht.Util.Util) UnitInfo(ecgberht.UnitInfo) UtilMicro(ecgberht.Util.UtilMicro) Bunker(org.openbw.bwapi4j.unit.Bunker) Set(java.util.Set) Objects(java.util.Objects) Unit(org.openbw.bwapi4j.unit.Unit) Ecgberht.getGs(ecgberht.Ecgberht.getGs) UnitType(org.openbw.bwapi4j.type.UnitType) SimInfo(ecgberht.Simulation.SimInfo) Order(org.openbw.bwapi4j.type.Order) Vulture(org.openbw.bwapi4j.unit.Vulture) MutablePair(ecgberht.Util.MutablePair) Base(bwem.Base) Optional(java.util.Optional) Position(org.openbw.bwapi4j.Position) Comparator(java.util.Comparator) UnitInfo(ecgberht.UnitInfo) Position(org.openbw.bwapi4j.Position)

Example 30 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class VultureAgent method combat.

private void combat() {
    UnitInfo toAttack = Util.getRangedTarget(unitInfo, mySim.enemies);
    if (toAttack != null) {
        if (attackUnit != null && attackUnit.equals(toAttack))
            return;
        UtilMicro.attack(unitInfo, toAttack);
        attackUnit = toAttack;
    }
}
Also used : UnitInfo(ecgberht.UnitInfo)

Aggregations

UnitInfo (ecgberht.UnitInfo)32 Position (org.openbw.bwapi4j.Position)12 ArrayList (java.util.ArrayList)9 Base (bwem.Base)7 Util (ecgberht.Util.Util)7 Collectors (java.util.stream.Collectors)7 Ecgberht.getGs (ecgberht.Ecgberht.getGs)6 MutablePair (ecgberht.Util.MutablePair)6 List (java.util.List)6 Set (java.util.Set)6 org.openbw.bwapi4j.unit (org.openbw.bwapi4j.unit)6 Area (bwem.Area)5 SimInfo (ecgberht.Simulation.SimInfo)5 Squad (ecgberht.Squad)5 ChokePoint (bwem.ChokePoint)4 TreeSet (java.util.TreeSet)4 org.openbw.bwapi4j.type (org.openbw.bwapi4j.type)4 Cluster (ecgberht.Clustering.Cluster)3 ConfigManager (ecgberht.ConfigManager)3 GameState (ecgberht.GameState)3