Search in sources :

Example 31 with Position

use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.

the class Move method execute.

@Override
public State execute() {
    try {
        Worker chosen = gameState.chosenWorker;
        Position realEnd = Util.getUnitCenterPosition(gameState.chosenPosition.toPosition(), gameState.chosenToBuild);
        if (chosen.move(realEnd)) {
            if (gameState.workerIdle.contains(chosen))
                gameState.workerIdle.remove(chosen);
            else if (gameState.workerMining.containsKey(chosen)) {
                MineralPatch mineral = gameState.workerMining.get(chosen);
                gameState.workerMining.remove(chosen);
                if (gameState.mineralsAssigned.containsKey(mineral)) {
                    gameState.mining--;
                    gameState.mineralsAssigned.put(mineral, gameState.mineralsAssigned.get(mineral) - 1);
                }
            }
            if (gameState.chosenToBuild == UnitType.Terran_Command_Center && gameState.bwem.getMap().getArea(gameState.chosenPosition).equals(gameState.naturalArea) && gameState.naturalChoke != null) {
                gameState.defendPosition = gameState.naturalChoke.getCenter().toPosition();
            }
            gameState.workerBuild.put((SCV) chosen, new MutablePair<>(gameState.chosenToBuild, gameState.chosenPosition));
            gameState.deltaCash.first += gameState.chosenToBuild.mineralPrice();
            gameState.deltaCash.second += gameState.chosenToBuild.gasPrice();
            gameState.chosenWorker = null;
            gameState.chosenToBuild = UnitType.None;
            return State.SUCCESS;
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : MineralPatch(org.openbw.bwapi4j.unit.MineralPatch) Position(org.openbw.bwapi4j.Position) Worker(org.openbw.bwapi4j.unit.Worker)

Example 32 with Position

use of org.openbw.bwapi4j.Position 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 33 with Position

use of org.openbw.bwapi4j.Position 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 34 with Position

use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.

the class DropShipAgent method retreat.

protected void retreat() {
    Position CC = getGs().getNearestCC(myUnit.getPosition(), false);
    if (CC != null)
        target = CC;
    else
        target = getGs().getPlayer().getStartLocation().toPosition();
    ((MobileUnit) myUnit).move(target);
}
Also used : MobileUnit(org.openbw.bwapi4j.unit.MobileUnit) Position(org.openbw.bwapi4j.Position)

Example 35 with Position

use of org.openbw.bwapi4j.Position 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)

Aggregations

Position (org.openbw.bwapi4j.Position)55 TilePosition (org.openbw.bwapi4j.TilePosition)19 UnitInfo (ecgberht.UnitInfo)10 Worker (org.openbw.bwapi4j.unit.Worker)10 UnitType (org.openbw.bwapi4j.type.UnitType)9 SimInfo (ecgberht.Simulation.SimInfo)8 Util (ecgberht.Util.Util)8 WalkPosition (org.openbw.bwapi4j.WalkPosition)8 Ecgberht.getGs (ecgberht.Ecgberht.getGs)7 UtilMicro (ecgberht.Util.UtilMicro)7 org.openbw.bwapi4j.unit (org.openbw.bwapi4j.unit)7 Area (bwem.Area)6 Base (bwem.Base)6 Collectors (java.util.stream.Collectors)6 Order (org.openbw.bwapi4j.type.Order)6 java.util (java.util)5 WeaponType (org.openbw.bwapi4j.type.WeaponType)5 ArrayList (java.util.ArrayList)4 MineralPatch (org.openbw.bwapi4j.unit.MineralPatch)4 Squad (ecgberht.Squad)3