Search in sources :

Example 1 with MiniTile

use of bwem.tile.MiniTile in project BWAPI4J by OpenBW.

the class MapImpl method breadthFirstSearch.

public WalkPosition breadthFirstSearch(final WalkPosition start, final Pred findCond, final Pred visitCond, final boolean connect8) {
    if (findCond.isTrue(getData().getMiniTile(start), start, this)) {
        return start;
    }
    final List<WalkPosition> visited = new ArrayList<>();
    final Queue<WalkPosition> toVisit = new ArrayDeque<>();
    toVisit.add(start);
    visited.add(start);
    final WalkPosition[] dir8 = { new WalkPosition(-1, -1), new WalkPosition(0, -1), new WalkPosition(1, -1), new WalkPosition(-1, 0), new WalkPosition(1, 0), new WalkPosition(-1, 1), new WalkPosition(0, 1), new WalkPosition(1, 1) };
    final WalkPosition[] dir4 = { new WalkPosition(0, -1), new WalkPosition(-1, 0), new WalkPosition(1, 0), new WalkPosition(0, 1) };
    final WalkPosition[] directions = connect8 ? dir8 : dir4;
    while (!toVisit.isEmpty()) {
        final WalkPosition current = toVisit.remove();
        for (final WalkPosition delta : directions) {
            final WalkPosition next = current.add(delta);
            if (getData().getMapData().isValid(next)) {
                final MiniTile miniTile = getData().getMiniTile(next, CheckMode.NO_CHECK);
                if (findCond.isTrue(miniTile, next, this)) {
                    return next;
                }
                if (visitCond.isTrue(miniTile, next, this) && !visited.contains(next)) {
                    toVisit.add(next);
                    visited.add(next);
                }
            }
        }
    }
    // bwem_assert(false);
    throw new IllegalStateException();
// return start;
}
Also used : MiniTile(bwem.tile.MiniTile)

Example 2 with MiniTile

use of bwem.tile.MiniTile in project BWAPI4J by OpenBW.

the class MapImpl method onBlockingNeutralDestroyed.

public void onBlockingNeutralDestroyed(Neutral pBlocking) {
    // bwem_assert(pBlocking && pBlocking->blocking());
    if (!(pBlocking != null && pBlocking.isBlocking())) {
        throw new IllegalArgumentException();
    }
    for (Area pArea : pBlocking.getBlockedAreas()) for (ChokePoint cp : pArea.getChokePoints()) {
        ((ChokePointImpl) cp).onBlockingNeutralDestroyed(pBlocking);
    }
    if (getData().getTile(pBlocking.getTopLeft()).getNeutral() != null) {
        // there remains some blocking Neutrals at the same location
        return;
    }
    // Unblock the miniTiles of pBlocking:
    AreaId newId = pBlocking.getBlockedAreas().iterator().next().getId();
    WalkPosition pBlockingW = pBlocking.getSize().toWalkPosition();
    for (int dy = 0; dy < pBlockingW.getY(); ++dy) for (int dx = 0; dx < pBlockingW.getX(); ++dx) {
        MiniTile miniTile = ((AdvancedDataInitializer) getData()).getMiniTile_(pBlocking.getTopLeft().toWalkPosition().add(new WalkPosition(dx, dy)));
        if (miniTile.isWalkable()) {
            ((MiniTileImpl) miniTile).replaceBlockedAreaId(newId);
        }
    }
    // Unblock the Tiles of pBlocking:
    for (int dy = 0; dy < pBlocking.getSize().getY(); ++dy) for (int dx = 0; dx < pBlocking.getSize().getX(); ++dx) {
        ((TileImpl) ((AdvancedDataInitializer) getData()).getTile_(pBlocking.getTopLeft().add(new TilePosition(dx, dy)))).resetAreaId();
        setAreaIdInTile(pBlocking.getTopLeft().add(new TilePosition(dx, dy)));
    }
    if (automaticPathUpdate()) {
        getGraph().computeChokePointDistanceMatrix();
    }
}
Also used : Area(bwem.area.Area) AreaId(bwem.area.typedef.AreaId) MiniTile(bwem.tile.MiniTile)

Example 3 with MiniTile

use of bwem.tile.MiniTile in project BWAPI4J by OpenBW.

the class PairGenericMiniTileAltitudeComparatorTest method testPairGenericMiniTileAltitudeComparator.

@Test
public void testPairGenericMiniTileAltitudeComparator() {
    final List<MutablePair<WalkPosition, MiniTile>> list = new ArrayList<>();
    final MiniTile m1 = new MiniTileImpl();
    ((MiniTileImpl) m1).setAreaId(new AreaId(2));
    ((MiniTileImpl) m1).setAltitude(new Altitude(5));
    final MiniTile m2 = new MiniTileImpl();
    ((MiniTileImpl) m2).setAreaId(new AreaId(1));
    ((MiniTileImpl) m2).setAltitude(new Altitude(2));
    final MiniTile m3 = new MiniTileImpl();
    ((MiniTileImpl) m3).setAreaId(new AreaId(3));
    ((MiniTileImpl) m3).setAltitude(new Altitude(7));
    final MiniTile m4 = new MiniTileImpl();
    ((MiniTileImpl) m4).setAreaId(new AreaId(5));
    ((MiniTileImpl) m4).setAltitude(new Altitude(1));
    final MiniTile m5 = new MiniTileImpl();
    ((MiniTileImpl) m5).setAreaId(new AreaId(4));
    ((MiniTileImpl) m5).setAltitude(new Altitude(8));
    list.add(new MutablePair<>(new WalkPosition(5, 15), m1));
    list.add(new MutablePair<>(new WalkPosition(2, 98), m2));
    list.add(new MutablePair<>(new WalkPosition(63, 123), m3));
    list.add(new MutablePair<>(new WalkPosition(103, 435), m4));
    list.add(new MutablePair<>(new WalkPosition(89, 77), m5));
    // Sort by descending order.
    Collections.sort(list, new PairGenericMiniTileAltitudeComparator<>());
    Collections.reverse(list);
    Assert.assertEquals(new MutablePair<>(new WalkPosition(89, 77), m5), list.get(0));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(63, 123), m3), list.get(1));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(5, 15), m1), list.get(2));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(2, 98), m2), list.get(3));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(103, 435), m4), list.get(4));
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) MiniTile(bwem.tile.MiniTile) AreaId(bwem.area.typedef.AreaId) WalkPosition(org.openbw.bwapi4j.WalkPosition) MiniTileImpl(bwem.tile.MiniTileImpl) Altitude(bwem.typedef.Altitude) Test(org.junit.Test)

Example 4 with MiniTile

use of bwem.tile.MiniTile in project BWAPI4J by OpenBW.

the class Graph method getNearestArea.

public Area getNearestArea(final WalkPosition walkPosition) {
    final Area area = getArea(walkPosition);
    if (area != null) {
        return area;
    }
    final WalkPosition w = getMap().breadthFirstSearch(walkPosition, // findCond
    args -> {
        final Object ttile = args[0];
        if (ttile instanceof MiniTile) {
            final MiniTile miniTile = (MiniTile) ttile;
            return (miniTile.getAreaId().intValue() > 0);
        } else {
            throw new IllegalArgumentException();
        }
    }, // visitCond
    args -> true);
    return getArea(w);
}
Also used : Area(bwem.area.Area) WalkPosition(org.openbw.bwapi4j.WalkPosition) MiniTile(bwem.tile.MiniTile)

Example 5 with MiniTile

use of bwem.tile.MiniTile in project BWAPI4J by OpenBW.

the class Graph method createChokePoints.

// ----------------------------------------------------------------------
// //////////////////////////////////////////////////////////////////////
// Creates a new Area for each pair (top, miniTiles) in AreasList (See Area::top() and Area::miniTiles())
public void createChokePoints(final List<StaticBuilding> staticBuildings, final List<Mineral> minerals, final List<MutablePair<MutablePair<AreaId, AreaId>, WalkPosition>> rawFrontier) {
    Index newIndex = new Index(0);
    final List<Neutral> blockingNeutrals = new ArrayList<>();
    for (final StaticBuilding s : staticBuildings) {
        if (s.isBlocking()) {
            blockingNeutrals.add(s);
        }
    }
    for (final Mineral m : minerals) {
        if (m.isBlocking()) {
            blockingNeutrals.add(m);
        }
    }
    // Note: pseudoChokePointsToCreate is only used for pre-allocating the GetChokePoints array size.
    // This number will highly likely be very small. There is no reason to set a minimum size.
    // int pseudoChokePointsToCreate = 0;
    // for (final Neutral blockingNeutral : blockingNeutrals) {
    // if (blockingNeutral.getNextStacked() == null) {
    // ++pseudoChokePointsToCreate;
    // }
    // }
    // 1) size the matrix
    initializeChokePointsMatrix(this.chokePointsMatrix, getAreaCount());
    // 2) Dispatch the global raw frontier between all the relevant pairs of areas:
    final java.util.Map<MutablePair<AreaId, AreaId>, List<WalkPosition>> rawFrontierByAreaPair = createRawFrontierByAreaPairMap(rawFrontier);
    // 3) For each pair of areas (A, B):
    for (final java.util.Map.Entry<MutablePair<AreaId, AreaId>, List<WalkPosition>> entry : rawFrontierByAreaPair.entrySet()) {
        MutablePair<AreaId, AreaId> rawleft = entry.getKey();
        final List<WalkPosition> rawFrontierAB = entry.getValue();
        // Because our dispatching preserved order,
        // and because Map::m_RawFrontier was populated in descending order of the altitude (see Map::computeAreas),
        // we know that rawFrontierAB is also ordered the same way, but let's check it:
        {
            final List<Altitude> altitudes = new ArrayList<>();
            for (final WalkPosition w : rawFrontierAB) {
                altitudes.add(getMap().getData().getMiniTile(w).getAltitude());
            }
            // bwem_assert(is_sorted(altitudes.rbegin(), altitudes.rend()));
            for (int i = 1; i < altitudes.size(); ++i) {
                final int prev = altitudes.get(i - 1).intValue();
                final int curr = altitudes.get(i).intValue();
                if (prev < curr) {
                    throw new IllegalStateException();
                }
            }
        }
        // 3.1) Use that information to efficiently cluster rawFrontierAB in one or several chokepoints.
        // Each cluster will be populated starting with the center of a chokepoint (max altitude)
        // and finishing with the ends (min altitude).
        final int clusterMinDist = (int) Math.sqrt(BwemExt.lake_max_miniTiles);
        final List<List<WalkPosition>> clusters = new ArrayList<>();
        for (final WalkPosition w : rawFrontierAB) {
            boolean added = false;
            for (final List<WalkPosition> cluster : clusters) {
                final int distToFront = BwemExt.queenWiseDist(cluster.get(0), w);
                final int distToBack = BwemExt.queenWiseDist(cluster.get(cluster.size() - 1), w);
                if (Math.min(distToFront, distToBack) <= clusterMinDist) {
                    if (distToFront < distToBack) {
                        cluster.add(0, w);
                    } else {
                        cluster.add(w);
                    }
                    added = true;
                    break;
                }
            }
            if (!added) {
                final List<WalkPosition> list = new ArrayList<>();
                list.add(w);
                clusters.add(list);
            }
        }
        // 3.2) Create one Chokepoint for each cluster:
        final AreaId a = rawleft.getLeft();
        final AreaId b = rawleft.getRight();
        // getChokePoints(a, b).reserve(clusters.size() + pseudoChokePointsToCreate);
        for (final List<WalkPosition> cluster : clusters) {
            getChokePoints(a, b).add(new ChokePointImpl(this, newIndex, getArea(a), getArea(b), cluster));
            newIndex = newIndex.add(1);
        }
    }
    // 4) Create one Chokepoint for each pair of blocked areas, for each blocking Neutral:
    for (final Neutral blockingNeutral : blockingNeutrals) {
        if (blockingNeutral.getNextStacked() == null) {
            // in the case where several neutrals are stacked, we only consider the top
            final List<Area> blockedAreas = blockingNeutral.getBlockedAreas();
            for (final Area blockedAreaA : blockedAreas) for (final Area blockedAreaB : blockedAreas) {
                if (blockedAreaB.equals(blockedAreaA)) {
                    // breaks symmetry
                    break;
                }
                final WalkPosition center = getMap().breadthFirstSearch(blockingNeutral.getCenter().toWalkPosition(), // findCond
                args -> {
                    Object ttile = args[0];
                    if (!(ttile instanceof MiniTile)) {
                        throw new IllegalArgumentException();
                    }
                    MiniTile miniTile = (MiniTile) ttile;
                    return miniTile.isWalkable();
                }, // visitCond
                args -> true);
                final List<WalkPosition> list = new ArrayList<>();
                list.add(center);
                getChokePoints(blockedAreaA, blockedAreaB).add(new ChokePointImpl(this, newIndex, blockedAreaA, blockedAreaB, list, blockingNeutral));
                newIndex = newIndex.add(1);
            }
        }
    }
    // 5) Set the references to the freshly created Chokepoints:
    for (int loopA = 1; loopA <= getAreaCount(); ++loopA) for (int loopB = 1; loopB < loopA; ++loopB) {
        final AreaId a = new AreaId(loopA);
        final AreaId b = new AreaId(loopB);
        if (!getChokePoints(a, b).isEmpty()) {
            ((AreaInitializer) getArea(a)).addChokePoints(getArea(b), getChokePoints(a, b));
            ((AreaInitializer) getArea(b)).addChokePoints(getArea(a), getChokePoints(a, b));
            this.chokePoints.addAll(getChokePoints(a, b));
        }
    }
}
Also used : StaticBuilding(bwem.unit.StaticBuilding) Utils(bwem.util.Utils) Altitude(bwem.typedef.Altitude) MutableInt(org.apache.commons.lang3.mutable.MutableInt) PriorityQueue(java.util.PriorityQueue) Map(bwem.map.Map) HashMap(java.util.HashMap) TilePosition(org.openbw.bwapi4j.TilePosition) Pair(org.openbw.bwapi4j.util.Pair) ArrayList(java.util.ArrayList) Area(bwem.area.Area) MutablePair(org.apache.commons.lang3.tuple.MutablePair) GroupId(bwem.area.typedef.GroupId) AreaId(bwem.area.typedef.AreaId) CPPath(bwem.typedef.CPPath) Index(bwem.typedef.Index) AdvancedData(bwem.map.AdvancedData) StaticBuilding(bwem.unit.StaticBuilding) Position(org.openbw.bwapi4j.Position) Tile(bwem.tile.Tile) Neutral(bwem.unit.Neutral) AreaInitializer(bwem.area.AreaInitializer) WalkPosition(org.openbw.bwapi4j.WalkPosition) Mineral(bwem.unit.Mineral) TileImpl(bwem.tile.TileImpl) List(java.util.List) Geyser(bwem.unit.Geyser) AreaInitializerImpl(bwem.area.AreaInitializerImpl) BwemExt(bwem.util.BwemExt) Queue(java.util.Queue) Comparator(java.util.Comparator) MiniTile(bwem.tile.MiniTile) Neutral(bwem.unit.Neutral) ArrayList(java.util.ArrayList) AreaId(bwem.area.typedef.AreaId) MiniTile(bwem.tile.MiniTile) Index(bwem.typedef.Index) MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) List(java.util.List) Mineral(bwem.unit.Mineral) Area(bwem.area.Area) WalkPosition(org.openbw.bwapi4j.WalkPosition) Map(bwem.map.Map) HashMap(java.util.HashMap)

Aggregations

MiniTile (bwem.tile.MiniTile)6 Area (bwem.area.Area)4 WalkPosition (org.openbw.bwapi4j.WalkPosition)4 AreaId (bwem.area.typedef.AreaId)3 MutablePair (org.apache.commons.lang3.tuple.MutablePair)3 Tile (bwem.tile.Tile)2 TileImpl (bwem.tile.TileImpl)2 Altitude (bwem.typedef.Altitude)2 Geyser (bwem.unit.Geyser)2 Mineral (bwem.unit.Mineral)2 StaticBuilding (bwem.unit.StaticBuilding)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TilePosition (org.openbw.bwapi4j.TilePosition)2 Base (bwem.Base)1 ChokePoint (bwem.ChokePoint)1 AreaInitializer (bwem.area.AreaInitializer)1 AreaInitializerImpl (bwem.area.AreaInitializerImpl)1 GroupId (bwem.area.typedef.GroupId)1 AdvancedData (bwem.map.AdvancedData)1