Search in sources :

Example 1 with CPPath

use of bwem.typedef.CPPath 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 2 with CPPath

use of bwem.typedef.CPPath in project BWAPI4J by OpenBW.

the class Graph method computeChokePointDistanceMatrix.

// ----------------------------------------------------------------------
// Computes the ground distances between any pair of ChokePoints in pContext
// This is achieved by invoking several times pContext->ComputeDistances,
// which effectively computes the distances from one starting ChokePoint, using Dijkstra's algorithm.
// If Context == Area, Dijkstra's algorithm works on the Tiles inside one Area.
// If Context == Graph, Dijkstra's algorithm works on the GetChokePoints between the AreaS.
public void computeChokePointDistanceMatrix() {
    // 1) size the matrix
    chokePointDistanceMatrix.clear();
    // m_ChokePointDistanceMatrix.resize (chokePoints.size());
    for (int i = 0; i < chokePoints.size(); ++i) {
        chokePointDistanceMatrix.add(new ArrayList<>());
    }
    // line.resize (chokePoints.size(), -1);
    for (int i = 0; i < chokePointDistanceMatrix.size(); ++i) {
        for (int n = 0; n < chokePoints.size(); ++n) {
            chokePointDistanceMatrix.get(i).add(-1);
        }
    }
    // m_PathsBetweenChokePoints.resize (chokePoints.size());
    pathsBetweenChokePoints.clear();
    for (int i = 0; i < chokePoints.size(); ++i) {
        pathsBetweenChokePoints.add(new ArrayList<>());
    }
    // line.resize (chokePoints.size());
    for (int i = 0; i < pathsBetweenChokePoints.size(); ++i) {
        for (int n = 0; n < chokePoints.size(); ++n) {
            pathsBetweenChokePoints.get(i).add(new CPPath());
        }
    }
    // 2) Compute distances inside each Area
    for (final Area area : getAreas()) {
        computeChokePointDistances(area);
    }
    // 3) Compute distances through connected areas
    computeChokePointDistances(this);
    for (final ChokePoint cp : getChokePoints()) {
        setDistance(cp, cp, 0);
        CPPath cppath = new CPPath();
        cppath.add(cp);
        setPath(cp, cp, cppath);
    }
    // 4) Update Area::m_AccessibleNeighbors for each Area
    for (final Area area : getAreas()) ((AreaInitializer) area).updateAccessibleNeighbors();
    // 5)  Update Area::m_groupId for each Area
    updateGroupIds();
}
Also used : Area(bwem.area.Area) CPPath(bwem.typedef.CPPath)

Example 3 with CPPath

use of bwem.typedef.CPPath in project BWAPI4J by OpenBW.

the class Graph method setPathForComputeChokePointDistances.

private void setPathForComputeChokePointDistances(final int[] distanceToTargets, ChokePoint pStart, List<ChokePoint> targets, boolean collectIntermediateChokePoints) {
    for (int i = 0; i < targets.size(); ++i) {
        final int newDist = distanceToTargets[i];
        final ChokePoint target = targets.get(i);
        final int existingDist = distance(pStart, target);
        if (newDist != 0 && ((existingDist == -1) || (newDist < existingDist))) {
            setDistance(pStart, target, newDist);
            // Build the path from pStart to targets[i]:
            final CPPath path = new CPPath();
            path.add(pStart);
            path.add(target);
            // if ((void *)(pContext) == (void *)(this))	// tests (Context == Graph) without warning about constant condition
            if (collectIntermediateChokePoints) {
                for (ChokePoint pPrev = ((ChokePointImpl) target).getPathBackTrace(); !pPrev.equals(pStart); pPrev = ((ChokePointImpl) pPrev).getPathBackTrace()) {
                    path.add(1, pPrev);
                }
            }
            setPath(pStart, target, path);
        }
    }
}
Also used : CPPath(bwem.typedef.CPPath)

Example 4 with CPPath

use of bwem.typedef.CPPath in project BWAPI4J by OpenBW.

the class Graph method getPath.

public CPPath getPath(final Position a, final Position b, final MutableInt pLength) {
    final Area areaA = getNearestArea(a.toWalkPosition());
    final Area areaB = getNearestArea(b.toWalkPosition());
    if (areaA.equals(areaB)) {
        if (pLength != null) {
            pLength.setValue(BwemExt.getApproxDistance(a, b));
        }
        return new CPPath();
    }
    if (!areaA.isAccessibleFrom(areaB)) {
        if (pLength != null) {
            pLength.setValue(-1);
        }
        return new CPPath();
    }
    int minDistAB = Integer.MAX_VALUE;
    ChokePoint pBestCpA = null;
    ChokePoint pBestCpB = null;
    for (final ChokePoint cpA : areaA.getChokePoints()) {
        if (!cpA.isBlocked()) {
            final int distACpA = BwemExt.getApproxDistance(a, cpA.getCenter().toPosition());
            for (final ChokePoint cpB : areaB.getChokePoints()) {
                if (!cpB.isBlocked()) {
                    final int distBToCPB = BwemExt.getApproxDistance(b, cpB.getCenter().toPosition());
                    final int distAToB = distACpA + distBToCPB + distance(cpA, cpB);
                    if (distAToB < minDistAB) {
                        minDistAB = distAToB;
                        pBestCpA = cpA;
                        pBestCpB = cpB;
                    }
                }
            }
        }
    }
    // bwem_assert(minDistAB != numeric_limits<int>::max());
    if (minDistAB == Integer.MAX_VALUE) {
        throw new IllegalStateException();
    }
    final CPPath path = getPath(pBestCpA, pBestCpB);
    if (pLength != null) {
        // bwem_assert(Path.size() >= 1);
        if (!(path.size() >= 1)) {
            throw new IllegalStateException();
        }
        pLength.setValue(minDistAB);
        if (path.size() == 1) {
            // bwem_assert(pBestCpA == pBestCpB);
            if (!pBestCpA.equals(pBestCpB)) {
                throw new IllegalStateException();
            }
            final Position cpEnd1 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END1));
            final Position cpEnd2 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END2));
            if (Utils.intersect(a.getX(), a.getY(), b.getX(), b.getY(), cpEnd1.getX(), cpEnd1.getY(), cpEnd2.getX(), cpEnd2.getY())) {
                pLength.setValue(BwemExt.getApproxDistance(a, b));
            } else {
                for (final ChokePoint.Node node : new ChokePoint.Node[] { ChokePoint.Node.END1, ChokePoint.Node.END2 }) {
                    final Position c = BwemExt.center(pBestCpA.getNodePosition(node));
                    final int distAToB = BwemExt.getApproxDistance(a, c) + BwemExt.getApproxDistance(b, c);
                    if (distAToB < pLength.intValue()) {
                        pLength.setValue(distAToB);
                    }
                }
            }
        }
    }
    return getPath(pBestCpA, pBestCpB);
}
Also used : Area(bwem.area.Area) CPPath(bwem.typedef.CPPath) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position) WalkPosition(org.openbw.bwapi4j.WalkPosition)

Example 5 with CPPath

use of bwem.typedef.CPPath in project BWAPI4J by OpenBW.

the class Graph method setPath.

private void setPath(final ChokePoint cpA, final ChokePoint cpB, final CPPath pathAB) {
    final int indexA = ((ChokePointImpl) cpA).getIndex().intValue();
    final int indexB = ((ChokePointImpl) cpB).getIndex().intValue();
    this.pathsBetweenChokePoints.get(indexA).set(indexB, pathAB);
    if (cpA != cpB) {
        final CPPath reversePath = this.pathsBetweenChokePoints.get(indexB).get(indexA);
        reversePath.clear();
        for (int i = pathAB.size() - 1; i >= 0; --i) {
            final ChokePoint cp = pathAB.get(i);
            reversePath.add(cp);
        }
    }
}
Also used : CPPath(bwem.typedef.CPPath)

Aggregations

CPPath (bwem.typedef.CPPath)5 Area (bwem.area.Area)2 WalkPosition (org.openbw.bwapi4j.WalkPosition)2 ChokePoint (bwem.ChokePoint)1 IOException (java.io.IOException)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1 Position (org.openbw.bwapi4j.Position)1 TilePosition (org.openbw.bwapi4j.TilePosition)1