Search in sources :

Example 1 with StaticBuilding

use of bwem.unit.StaticBuilding 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)

Example 2 with StaticBuilding

use of bwem.unit.StaticBuilding in project BWAPI4J by OpenBW.

the class AreaInitializerImpl method computeBaseLocationScore.

@Override
public int computeBaseLocationScore(final AdvancedData mapAdvancedData, final TilePosition location) {
    final TilePosition dimCC = UnitType.Terran_Command_Center.tileSize();
    int sumScore = 0;
    for (int dy = 0; dy < dimCC.getY(); ++dy) for (int dx = 0; dx < dimCC.getX(); ++dx) {
        final Tile tile = mapAdvancedData.getTile(location.add(new TilePosition(dx, dy)), CheckMode.NO_CHECK);
        if (!tile.isBuildable()) {
            return -1;
        }
        if (((TileImpl) tile).getInternalData() == -1) {
            // Unfortunately, this is guaranteed only for the resources in this Area, which is the very reason of validateBaseLocation
            return -1;
        }
        if (!tile.getAreaId().equals(getId())) {
            return -1;
        }
        if (tile.getNeutral() != null && (tile.getNeutral() instanceof StaticBuilding)) {
            return -1;
        }
        sumScore += ((TileImpl) tile).getInternalData();
    }
    return sumScore;
}
Also used : TileImpl(bwem.tile.TileImpl) StaticBuilding(bwem.unit.StaticBuilding) TilePosition(org.openbw.bwapi4j.TilePosition) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) ChokePoint(bwem.ChokePoint)

Example 3 with StaticBuilding

use of bwem.unit.StaticBuilding in project BWAPI4J by OpenBW.

the class MapImpl method onStaticBuildingDestroyed.

@Override
public void onStaticBuildingDestroyed(Unit u) {
    for (int i = 0; i < getNeutralData().getStaticBuildings().size(); ++i) {
        StaticBuilding building = getNeutralData().getStaticBuildings().get(i);
        if (building.getUnit().equals(u)) {
            building.simulateCPPObjectDestructor();
            /* IMPORTANT! These actions are performed in the "~Neutral" dtor in BWEM 1.4.1 C++. */
            getNeutralData().getStaticBuildings().remove(i);
            return;
        }
    }
    // bwem_assert(iStaticBuilding != StaticBuildings.end());
    throw new IllegalArgumentException("unit is not a StaticBuilding");
}
Also used : StaticBuilding(bwem.unit.StaticBuilding)

Example 4 with StaticBuilding

use of bwem.unit.StaticBuilding in project BWAPI4J by OpenBW.

the class MapPrinterExample method printMap.

public void printMap(Map theMap) {
    java.util.Map<Integer, Color> mapZoneColor = new HashMap<>();
    for (int y = 0; y < theMap.getData().getMapData().getWalkSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getWalkSize().getX(); ++x) {
        WalkPosition p = new WalkPosition(x, y);
        MiniTile miniTile = theMap.getData().getMiniTile(p, CheckMode.NO_CHECK);
        Color col;
        if (miniTile.isSea()) {
            if (mapPrinter.showSeaSide && theMap.getData().isSeaWithNonSeaNeighbors(p))
                col = MapPrinter.CustomColor.SEA_SIDE.color();
            else
                col = MapPrinter.CustomColor.SEA.color();
        } else {
            if (mapPrinter.showLakes && miniTile.isLake()) {
                col = MapPrinter.CustomColor.LAKE.color();
            } else {
                if (mapPrinter.showAltitude) {
                    int c = 255 - ((miniTile.getAltitude().intValue() * 255) / theMap.getHighestAltitude().intValue());
                    col = new Color(c, c, c);
                } else {
                    col = MapPrinter.CustomColor.TERRAIN.color();
                }
                if (mapPrinter.showAreas || mapPrinter.showContinents)
                    if (miniTile.getAreaId().intValue() > 0) {
                        Area area = theMap.getArea(miniTile.getAreaId());
                        Color zoneColor = getZoneColor(area, mapZoneColor);
                        int red = zoneColor.getRed() * col.getRed() / 255;
                        int green = zoneColor.getGreen() * col.getGreen() / 255;
                        col = new Color(red, green, 0);
                    } else {
                        col = MapPrinter.CustomColor.TINY_AREA.color();
                    }
            }
        }
        mapPrinter.point(p, col);
    }
    if (mapPrinter.showData)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) {
            int data = ((TileImpl) theMap.getData().getTile(new TilePosition(x, y))).getInternalData();
            int c = (((data / 1) * 1) % 256);
            Color col = new Color(c, c, c);
            WalkPosition origin = (new TilePosition(x, y)).toWalkPosition();
            mapPrinter.rectangle(origin, origin.add(new WalkPosition(3, 3)), col, MapPrinter.fill_t.fill);
        }
    if (mapPrinter.showUnbuildable)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) if (!theMap.getData().getTile(new TilePosition(x, y)).isBuildable()) {
            WalkPosition origin = (new TilePosition(x, y)).toWalkPosition();
            mapPrinter.rectangle(origin.add(new WalkPosition(1, 1)), origin.add(new WalkPosition(2, 2)), MapPrinter.CustomColor.UNBUILDABLE.color());
        }
    if (mapPrinter.showGroundHeight)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) {
            Tile.GroundHeight groundHeight = theMap.getData().getTile(new TilePosition(x, y)).getGroundHeight();
            if (groundHeight.intValue() >= Tile.GroundHeight.HIGH_GROUND.intValue())
                for (int dy = 0; dy < 4; ++dy) for (int dx = 0; dx < 4; ++dx) {
                    WalkPosition p = (new TilePosition(x, y).toWalkPosition()).add(new WalkPosition(dx, dy));
                    if (// groundHeight is usefull only for walkable miniTiles
                    theMap.getData().getMiniTile(p, CheckMode.NO_CHECK).isWalkable())
                        if (((dx + dy) & (groundHeight == Tile.GroundHeight.HIGH_GROUND ? 1 : 3)) != 0)
                            mapPrinter.point(p, MapPrinter.CustomColor.HIGHER_GROUND.color());
                }
        }
    if (mapPrinter.showAssignedResources)
        for (Area area : theMap.getAreas()) for (Base base : area.getBases()) {
            for (Mineral m : base.getMinerals()) mapPrinter.line(base.getCenter().toWalkPosition(), m.getCenter().toWalkPosition(), MapPrinter.CustomColor.BASES.color());
            for (Geyser g : base.getGeysers()) mapPrinter.line(base.getCenter().toWalkPosition(), g.getCenter().toWalkPosition(), MapPrinter.CustomColor.BASES.color());
        }
    if (mapPrinter.showGeysers)
        for (Geyser g : theMap.getNeutralData().getGeysers()) printNeutral(theMap, g, MapPrinter.CustomColor.GEYSERS.color());
    if (mapPrinter.showMinerals)
        for (Mineral m : theMap.getNeutralData().getMinerals()) printNeutral(theMap, m, MapPrinter.CustomColor.MINERALS.color());
    if (mapPrinter.showStaticBuildings)
        for (StaticBuilding s : theMap.getNeutralData().getStaticBuildings()) printNeutral(theMap, s, MapPrinter.CustomColor.STATIC_BUILDINGS.color());
    if (mapPrinter.showStartingLocations)
        for (TilePosition t : theMap.getData().getMapData().getStartingLocations()) {
            WalkPosition origin = t.toWalkPosition();
            // same size for other races
            WalkPosition size = UnitType.Terran_Command_Center.tileSize().toWalkPosition();
            mapPrinter.rectangle(origin, origin.add(size).subtract(new WalkPosition(1, 1)), MapPrinter.CustomColor.STARTING_LOCATIONS.color(), MapPrinter.fill_t.fill);
        }
    if (mapPrinter.showBases)
        for (Area area : theMap.getAreas()) {
            for (Base base : area.getBases()) {
                WalkPosition origin = base.getLocation().toWalkPosition();
                // same size for other races
                WalkPosition size = UnitType.Terran_Command_Center.tileSize().toWalkPosition();
                MapPrinter.dashed_t dashMode = base.getBlockingMinerals().isEmpty() ? MapPrinter.dashed_t.not_dashed : MapPrinter.dashed_t.dashed;
                mapPrinter.rectangle(origin, origin.add(size).subtract(new WalkPosition(1, 1)), MapPrinter.CustomColor.BASES.color(), MapPrinter.fill_t.do_not_fill, dashMode);
            }
        }
    if (mapPrinter.showChokePoints) {
        for (MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> f : theMap.getRawFrontier()) mapPrinter.point(f.getRight(), mapPrinter.showAreas ? MapPrinter.CustomColor.CHOKE_POINTS_SHOW_AREAS.color() : MapPrinter.CustomColor.CHOKE_POINTS_SHOW_CONTINENTS.color());
        for (Area area : theMap.getAreas()) for (ChokePoint cp : area.getChokePoints()) {
            ChokePoint.Node[] nodes = { ChokePoint.Node.END1, ChokePoint.Node.END2 };
            for (ChokePoint.Node n : nodes) mapPrinter.square(cp.getNodePosition(n), 1, new Color(255, 0, 255), MapPrinter.fill_t.fill);
            mapPrinter.square(cp.getCenter(), 1, new Color(0, 0, 255), MapPrinter.fill_t.fill);
        }
    }
    try {
        mapPrinter.writeImageToFile(Paths.get("map.png"), "png");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : TileImpl(bwem.tile.TileImpl) Mineral(bwem.unit.Mineral) StaticBuilding(bwem.unit.StaticBuilding) HashMap(java.util.HashMap) MiniTile(bwem.tile.MiniTile) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) IOException(java.io.IOException) ChokePoint(bwem.ChokePoint) Base(bwem.Base) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Area(bwem.area.Area) Geyser(bwem.unit.Geyser) TilePosition(org.openbw.bwapi4j.TilePosition) WalkPosition(org.openbw.bwapi4j.WalkPosition) ChokePoint(bwem.ChokePoint)

Aggregations

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