Search in sources :

Example 1 with Pair

use of bwapi.Pair in project Ecgberht by Jabbo16.

the class Expand method execute.

@Override
public State execute() {
    try {
        Unit chosen = ((GameState) this.handler).chosenBuilderBL;
        if (!chosen.build(UnitType.Terran_Command_Center, ((GameState) this.handler).chosenBaseLocation)) {
            ((GameState) this.handler).movingToExpand = false;
            ((GameState) this.handler).expanding = false;
            ((GameState) this.handler).checkUnitsBL(((GameState) this.handler).chosenBaseLocation, chosen);
            ((GameState) this.handler).chosenBaseLocation = null;
            ((GameState) this.handler).workerIdle.add(((GameState) this.handler).chosenBuilderBL);
            ((GameState) this.handler).chosenBuilderBL.stop();
            ((GameState) this.handler).chosenBuilderBL = null;
            ((GameState) this.handler).deltaCash.first -= UnitType.Terran_Command_Center.mineralPrice();
            ((GameState) this.handler).deltaCash.second -= UnitType.Terran_Command_Center.gasPrice();
            return State.FAILURE;
        }
        ((GameState) this.handler).movingToExpand = false;
        ((GameState) this.handler).workerBuild.add(new Pair<Unit, Pair<UnitType, TilePosition>>(chosen, new Pair<UnitType, TilePosition>(UnitType.Terran_Command_Center, ((GameState) this.handler).chosenBaseLocation)));
        ((GameState) this.handler).expanding = false;
        ((GameState) this.handler).chosenBaseLocation = null;
        ((GameState) this.handler).chosenBuilderBL = null;
        return State.SUCCESS;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : GameState(ecgberht.GameState) Unit(bwapi.Unit) Pair(bwapi.Pair)

Example 2 with Pair

use of bwapi.Pair in project Ecgberht by Jabbo16.

the class ChoosePosition method execute.

@Override
public State execute() {
    try {
        Game juego = ((GameState) this.handler).getGame();
        Player jugador = ((GameState) this.handler).getPlayer();
        TilePosition origin = null;
        if (((GameState) this.handler).chosenToBuild.isRefinery()) {
            if (!((GameState) this.handler).refineriesAssigned.isEmpty()) {
                for (Pair<Pair<Unit, Integer>, Boolean> g : ((GameState) this.handler).refineriesAssigned) {
                    if (!g.second) {
                        ((GameState) this.handler).chosenPosition = g.first.first.getTilePosition();
                        return State.SUCCESS;
                    }
                }
            }
        } else {
            if (!((GameState) this.handler).workerBuild.isEmpty()) {
                for (Pair<Unit, Pair<UnitType, TilePosition>> w : ((GameState) this.handler).workerBuild) {
                    ((GameState) this.handler).testMap.updateMap(w.second.second, w.second.first, false);
                }
            }
            if (!((GameState) this.handler).chosenToBuild.equals(UnitType.Terran_Bunker) && !((GameState) this.handler).chosenToBuild.equals(UnitType.Terran_Missile_Turret)) {
                if (((GameState) this.handler).strat.proxy && ((GameState) this.handler).chosenToBuild == UnitType.Terran_Barracks) {
                    origin = new TilePosition(((GameState) this.handler).getGame().mapWidth() / 2, ((GameState) this.handler).getGame().mapHeight() / 2);
                } else {
                    // origin = BWTA.getRegion(jugador.getStartLocation()).getCenter().toTilePosition();
                    origin = jugador.getStartLocation();
                }
            } else {
                if (((GameState) this.handler).chosenToBuild.equals(UnitType.Terran_Missile_Turret)) {
                    if (((GameState) this.handler).DBs.isEmpty()) {
                        origin = BWTA.getNearestChokepoint(jugador.getStartLocation()).getCenter().toTilePosition();
                    } else {
                        for (Unit b : ((GameState) this.handler).DBs.keySet()) {
                            origin = b.getTilePosition();
                            break;
                        }
                    }
                } else {
                    if (((GameState) this.handler).EI.naughty && ((GameState) this.handler).enemyRace == Race.Zerg) {
                        TilePosition raxTile = ((GameState) this.handler).MBs.iterator().next().getTilePosition();
                        origin = ((GameState) this.handler).testMap.findBunkerPositionAntiPool(raxTile.toPosition(), ((GameState) this.handler).closestChoke);
                        if (origin != null) {
                            ((GameState) this.handler).testMap = ((GameState) this.handler).map.clone();
                            ((GameState) this.handler).chosenPosition = origin;
                            return State.SUCCESS;
                        } else {
                            origin = ((GameState) this.handler).getBunkerPositionAntiPool();
                            if (origin != null) {
                                ((GameState) this.handler).testMap = ((GameState) this.handler).map.clone();
                                ((GameState) this.handler).chosenPosition = origin;
                                return State.SUCCESS;
                            } else {
                                origin = raxTile;
                            }
                        }
                    } else {
                        if (((GameState) this.handler).Ts.isEmpty()) {
                            if (((GameState) this.handler).closestChoke != null) {
                                origin = ((GameState) this.handler).testMap.findBunkerPosition(((GameState) this.handler).closestChoke);
                                if (origin != null) {
                                    ((GameState) this.handler).testMap = ((GameState) this.handler).map.clone();
                                    ((GameState) this.handler).chosenPosition = origin;
                                    return State.SUCCESS;
                                } else {
                                    origin = ((GameState) this.handler).closestChoke.getCenter().toTilePosition();
                                }
                            }
                        // else {
                        // origin = BWTA.getNearestChokepoint(jugador.getStartLocation()).getCenter().toTilePosition();
                        // }
                        } else {
                            for (Unit b : ((GameState) this.handler).Ts) {
                                origin = b.getTilePosition();
                                break;
                            }
                        }
                    }
                }
            }
            TilePosition posicion = ((GameState) this.handler).testMap.findPosition(((GameState) this.handler).chosenToBuild, origin);
            ((GameState) this.handler).testMap = ((GameState) this.handler).map.clone();
            if (posicion != null) {
                ((GameState) this.handler).chosenPosition = posicion;
                return State.SUCCESS;
            }
        }
        TilePosition posicion = juego.getBuildLocation(((GameState) this.handler).chosenToBuild, BWTA.getRegion(jugador.getStartLocation()).getCenter().toTilePosition(), 500);
        if (posicion != null) {
            if (juego.canBuildHere(posicion, ((GameState) this.handler).chosenToBuild)) {
                ((GameState) this.handler).chosenPosition = posicion;
                return State.SUCCESS;
            }
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : Player(bwapi.Player) Game(bwapi.Game) TilePosition(bwapi.TilePosition) GameState(ecgberht.GameState) Unit(bwapi.Unit) Pair(bwapi.Pair)

Example 3 with Pair

use of bwapi.Pair in project Ecgberht by Jabbo16.

the class ChooseRefinery method execute.

@Override
public State execute() {
    try {
        if (((GameState) this.handler).getPlayer().supplyUsed() < ((GameState) this.handler).strat.supplyForFirstRefinery || ((GameState) this.handler).getCash().second >= 300) {
            return State.FAILURE;
        }
        if (((GameState) this.handler).refineriesAssigned.size() == 1) {
            boolean found = false;
            for (Pair<Unit, Pair<UnitType, TilePosition>> w : ((GameState) this.handler).workerBuild) {
                if (w.second.first == UnitType.Terran_Barracks) {
                    found = true;
                    break;
                }
            }
            for (Pair<Unit, Unit> w : ((GameState) this.handler).workerTask) {
                if (w.second.getType() == UnitType.Terran_Barracks) {
                    found = true;
                    break;
                }
            }
            if (((GameState) this.handler).MBs.isEmpty() && found == false) {
                return State.FAILURE;
            }
        }
        int count = 0;
        Unit geyser = null;
        for (Pair<Pair<Unit, Integer>, Boolean> r : ((GameState) this.handler).refineriesAssigned) {
            if (r.second) {
                count++;
            } else {
                geyser = r.first.first;
            }
        }
        if (count == ((GameState) this.handler).refineriesAssigned.size()) {
            return State.FAILURE;
        }
        for (Pair<Unit, Pair<UnitType, TilePosition>> w : ((GameState) this.handler).workerBuild) {
            if (w.second.first == UnitType.Terran_Refinery) {
                return State.FAILURE;
            }
        }
        for (Pair<Unit, Unit> w : ((GameState) this.handler).workerTask) {
            if (w.second.getType() == UnitType.Terran_Refinery && w.second.getPosition().equals(geyser.getPosition())) {
                return State.FAILURE;
            }
        }
        ((GameState) this.handler).chosenToBuild = UnitType.Terran_Refinery;
        return State.SUCCESS;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        System.err.println(e);
        return State.ERROR;
    }
}
Also used : GameState(ecgberht.GameState) Unit(bwapi.Unit) Pair(bwapi.Pair)

Example 4 with Pair

use of bwapi.Pair in project BWJSAL by RobinsonMann.

the class BfsBuildingPlacer method getBuildLocationNear.

private TilePosition getBuildLocationNear(ReservedMap reservedMap, UnitType type, TilePosition position, Unit builder, int buildDist) {
    // returns a valid build location near the specified tile position.
    if (type.isAddon()) {
        type = type.whatBuilds().first;
    }
    PriorityQueue<Pair<TilePosition, Integer>> searchHeap = new PriorityQueue<>((o1, o2) -> o1.second.compareTo(o2.second));
    searchHeap.add(new Pair<>(position, 0));
    Set<TilePosition> closed = new HashSet<>();
    // Do a breadth first search to find a nearby valid build location with space
    while (searchHeap.isEmpty() == false) {
        TilePosition t = searchHeap.peek().first;
        int s = searchHeap.peek().second;
        searchHeap.poll();
        if (canBuildHereWithSpace(reservedMap, type, t, builder, buildDist)) {
            // We can build here with space so return this tile position
            return t;
        }
        int tx = t.getX();
        int ty = t.getY();
        int minx = Math.max(tx - 1, 0);
        int maxx = Math.min(tx + 1, game.mapWidth() - 1);
        int miny = Math.max(ty - 1, 0);
        int maxy = Math.min(ty + 1, game.mapHeight() - 1);
        for (int x = minx; x <= maxx; x++) {
            for (int y = miny; y <= maxy; y++) {
                if (walkable[x][y]) {
                    TilePosition t2 = new TilePosition(x, y);
                    if (!closed.contains(t2)) {
                        int ds = 10;
                        if (x != tx && y != ty) {
                            // diagonal distance, approximation of 10*sqrt( 2 )
                            ds = 14;
                        }
                        closed.add(t2);
                        searchHeap.add(new Pair(t2, s + ds));
                    }
                }
            }
        }
    }
    // We didn't find a build position, try looking for one with less space
    if (buildDist > 0) {
        return getBuildLocationNear(reservedMap, type, position, builder, buildDist - 1);
    }
    return TilePosition.None;
}
Also used : TilePosition(bwapi.TilePosition) PriorityQueue(java.util.PriorityQueue) Pair(bwapi.Pair) HashSet(java.util.HashSet)

Example 5 with Pair

use of bwapi.Pair 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)8 Unit (bwapi.Unit)6 GameState (ecgberht.GameState)6 TilePosition (bwapi.TilePosition)3 Position (bwapi.Position)2 HashSet (java.util.HashSet)2 Game (bwapi.Game)1 Player (bwapi.Player)1 UnitCommand (bwapi.UnitCommand)1 Squad (ecgberht.Squad)1 Point (java.awt.Point)1 PriorityQueue (java.util.PriorityQueue)1