Search in sources :

Example 1 with ChokePoint

use of bwem.ChokePoint in project BWAPI4J by OpenBW.

the class MapTest method assert_ChokePoints.

/**
 * Tests that all chokepoint centers match between the original BWEM in C++ and this Java port.<br/>
 */
private void assert_ChokePoints(final List<WalkPosition> expectedChokePointCenters, final List<ChokePoint> actualChokePoints) {
    final List<WalkPosition> actualChokepointCenters = new ArrayList<>();
    for (final ChokePoint actualChokePoint : actualChokePoints) {
        final WalkPosition actualChokePointCenter = actualChokePoint.getCenter();
        actualChokepointCenters.add(actualChokePointCenter);
    }
    Assert.assertEquals("Chokepoint container sizes do not match. expected=" + expectedChokePointCenters.size() + ", actual=" + actualChokepointCenters.size(), expectedChokePointCenters.size(), actualChokepointCenters.size());
    // If an exact position is not found, search within this WalkTile radius value.
    final int tolerance = 20;
    // Keep track of and do not use the same tolerant center more than once.
    final List<WalkPosition> tolerantCenters = new ArrayList<>();
    for (final WalkPosition expectedChokePointCenter : expectedChokePointCenters) {
        final boolean found = actualChokepointCenters.contains(expectedChokePointCenter);
        if (!found) {
            logger.warn("Did not find original chokepoint: " + expectedChokePointCenter.toString() + ". Retrying with a max tolerance of " + tolerance + ".");
            final int boundsLowerX = expectedChokePointCenter.getX() - tolerance;
            final int boundsUpperX = expectedChokePointCenter.getX() + tolerance;
            final int boundsLowerY = expectedChokePointCenter.getY() - tolerance;
            final int boundsUpperY = expectedChokePointCenter.getY() + tolerance;
            boolean foundTolerant = false;
            for (int y = boundsLowerY; y <= boundsUpperY; ++y) {
                for (int x = boundsLowerX; x <= boundsUpperX; ++x) {
                    final WalkPosition tolerantCenter = new WalkPosition(x, y);
                    if (actualChokepointCenters.contains(tolerantCenter)) {
                        Assert.assertEquals("Found a tolerant center but it has already been used: " + tolerantCenter.toString(), false, tolerantCenters.contains(tolerantCenter));
                        tolerantCenters.add(tolerantCenter);
                        foundTolerant = true;
                        logger.debug("Found tolerant center for chokepoint: " + expectedChokePointCenter.toString() + ", tolerant_center=" + tolerantCenter.toString() + ", tolerance=" + expectedChokePointCenter.subtract(tolerantCenter) + ".");
                        break;
                    }
                }
                if (foundTolerant) {
                    break;
                }
            }
            Assert.assertEquals("Did not find original chokepoint even with a tolerance value. actualChokepointCenters=" + actualChokepointCenters.toString(), true, foundTolerant);
        }
    }
}
Also used : WalkPosition(org.openbw.bwapi4j.WalkPosition) ArrayList(java.util.ArrayList) ChokePoint(bwem.ChokePoint) ChokePoint(bwem.ChokePoint)

Example 2 with ChokePoint

use of bwem.ChokePoint in project BWAPI4J by OpenBW.

the class MapPrinterExample method pathExample.

public void pathExample(Map theMap) {
    if (theMap.getData().getMapData().getStartingLocations().size() < 2)
        return;
    Color col = new Color(255, 255, 255);
    WalkPosition a = (theMap.getData().getMapData().getStartingLocations().get(randomGenerator.nextInt(theMap.getData().getMapData().getStartingLocations().size()))).toWalkPosition();
    WalkPosition b = a;
    while (b.equals(a)) {
        b = (theMap.getData().getMapData().getStartingLocations().get(randomGenerator.nextInt(theMap.getData().getMapData().getStartingLocations().size()))).toWalkPosition();
    }
    // Uncomment this to use random positions for a and b:
    // a = WalkPosition(theMap.RandomPosition());
    // b = WalkPosition(theMap.RandomPosition());
    mapPrinter.circle(a, 6, col, MapPrinter.fill_t.fill);
    mapPrinter.circle(b, 6, col, MapPrinter.fill_t.fill);
    MutableInt length = new MutableInt(0);
    CPPath path = theMap.getPath(a.toPosition(), b.toPosition(), length);
    // cannot reach b from a
    if (length.intValue() < 0)
        return;
    if (path.isEmpty()) {
        // bwem_assert(theMap.getNearestArea(a) == theMap.getNearestArea(b));
        if (!(theMap.getNearestArea(a).equals(theMap.getNearestArea(b)))) {
            throw new IllegalStateException();
        }
        // just draw a single line between them:
        mapPrinter.line(a, b, col, MapPrinter.dashed_t.dashed);
    } else {
        // bwem_assert(theMap.getNearestArea(a) != theMap.getNearestArea(b));
        if (theMap.getNearestArea(a).equals(theMap.getNearestArea(b))) {
            throw new IllegalStateException();
        }
        // draw a line between each ChokePoint in path:
        ChokePoint cpPrevious = null;
        for (ChokePoint cp : path) {
            if (cpPrevious != null) {
                mapPrinter.line(cpPrevious.getCenter(), cp.getCenter(), col, MapPrinter.dashed_t.dashed);
            }
            mapPrinter.circle(cp.getCenter(), 6, col);
            cpPrevious = cp;
        }
        mapPrinter.line(a, path.get(0).getCenter(), col, MapPrinter.dashed_t.dashed);
        mapPrinter.line(b, path.get(path.size() - 1).getCenter(), col, MapPrinter.dashed_t.dashed);
    }
    // TODO: Handle exception.
    try {
        mapPrinter.writeImageToFile(Paths.get("map.png"), "png");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : CPPath(bwem.typedef.CPPath) MutableInt(org.apache.commons.lang3.mutable.MutableInt) WalkPosition(org.openbw.bwapi4j.WalkPosition) ChokePoint(bwem.ChokePoint) IOException(java.io.IOException)

Example 3 with ChokePoint

use of bwem.ChokePoint in project BWAPI4J by OpenBW.

the class MapPrinterExample method getZoneColorCppAlgorithmAnyOf.

private boolean getZoneColorCppAlgorithmAnyOf(java.util.Map<Area, List<ChokePoint>> chokePointsByArea, java.util.Map<Integer, Color> mapZoneColor, Color color) {
    for (Area neighbor : chokePointsByArea.keySet()) {
        int neighborId = neighbor.getId().intValue();
        Color neighboringColor = mapZoneColor.get(neighborId);
        if (neighboringColor != null && (Math.abs(color.getRed() - neighboringColor.getRed()) + Math.abs(color.getGreen() - neighboringColor.getGreen()) < 150)) {
            return true;
        }
    }
    return false;
}
Also used : Area(bwem.area.Area) ChokePoint(bwem.ChokePoint)

Example 4 with ChokePoint

use of bwem.ChokePoint in project Ecgberht by Jabbo16.

the class BuildingMap method findBunkerPosition.

public TilePosition findBunkerPosition(ChokePoint choke) {
    TilePosition buildingSize = UnitType.Terran_Bunker.tileSize();
    int size = Math.max(buildingSize.getY(), buildingSize.getX());
    double chokeWidth = Util.getChokeWidth(choke);
    Position starting = choke.getCenter().toPosition();
    int x = starting.toTilePosition().getY();
    int y = starting.toTilePosition().getX();
    int i = 10;
    int j = 10;
    boolean expandBunker = choke.equals(getGs().naturalChoke);
    // Finds the first valid tileposition starting around the given tileposition
    TilePosition position = null;
    double dist = Double.MAX_VALUE;
    for (int ii = (x - i); ii <= (x + i); ii++) {
        for (int jj = (y - j); jj <= (y + j); jj++) {
            if ((ii >= 0 && ii < height) && (jj >= 0 && jj < width)) {
                if ((!map[ii][jj].equals("M") && !map[ii][jj].equals("V") && !map[ii][jj].equals("E") && !map[ii][jj].equals("B")) && Integer.parseInt(map[ii][jj]) >= size) {
                    Area area = bwem.getMap().getArea(new TilePosition(jj, ii));
                    if (area != null && area.equals(getGs().naturalArea) && !expandBunker)
                        continue;
                    if (area != null && !area.equals(getGs().naturalArea) && expandBunker)
                        continue;
                    if (checkUnitsChosenBuildingGrid(new TilePosition(jj, ii), UnitType.Terran_Bunker)) {
                        TilePosition newPosition = new TilePosition(jj, ii);
                        Position centerBunker = Util.getUnitCenterPosition(newPosition.toPosition(), UnitType.Terran_Bunker);
                        if (chokeWidth <= 64.0 && Util.broodWarDistance(choke.getCenter().toPosition(), centerBunker) <= 64)
                            continue;
                        double newDist = Util.broodWarDistance(centerBunker, starting);
                        if (position == null || newDist < dist) {
                            position = newPosition;
                            dist = newDist;
                        }
                    }
                }
            }
        }
    }
    return position;
}
Also used : Area(bwem.Area) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position) TilePosition(org.openbw.bwapi4j.TilePosition) ChokePoint(bwem.ChokePoint)

Example 5 with ChokePoint

use of bwem.ChokePoint in project Ecgberht by Jabbo16.

the class Util method getGroundDistanceClosestChoke.

public static ChokePoint getGroundDistanceClosestChoke(Position pos) {
    ChokePoint closestChoke = null;
    double dist = Double.MAX_VALUE;
    for (ChokePoint choke : getGs().bwem.getMap().getChokePoints()) {
        double cDist = getGroundDistance(pos, choke.getCenter().toPosition());
        if (cDist == 0.0)
            continue;
        if (closestChoke == null || cDist < dist) {
            closestChoke = choke;
            dist = cDist;
        }
    }
    return closestChoke;
}
Also used : ChokePoint(bwem.ChokePoint)

Aggregations

ChokePoint (bwem.ChokePoint)10 TilePosition (org.openbw.bwapi4j.TilePosition)4 WalkPosition (org.openbw.bwapi4j.WalkPosition)3 Base (bwem.Base)2 Area (bwem.area.Area)2 MiniTile (bwem.tile.MiniTile)2 Tile (bwem.tile.Tile)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Area (bwem.Area)1 Mineral (bwem.Mineral)1 TileImpl (bwem.tile.TileImpl)1 CPPath (bwem.typedef.CPPath)1 Geyser (bwem.unit.Geyser)1 Mineral (bwem.unit.Mineral)1 StaticBuilding (bwem.unit.StaticBuilding)1 MutablePair (ecgberht.Util.MutablePair)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1