Search in sources :

Example 1 with Tile

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

the class Graph method getNearestArea.

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

Example 2 with Tile

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

the class AreaInitializerImpl method computeDistances.

@Override
public int[] computeDistances(final TilePosition start, final List<TilePosition> targets) {
    final int[] distances = new int[targets.size()];
    TileImpl.getStaticMarkable().unmarkAll();
    // a priority queue holding the tiles to visit ordered by their distance to start.
    final Queue<Pair<Integer, TilePosition>> toVisit = new PriorityQueue<>(Comparator.comparingInt(Pair::getFirst));
    toVisit.offer(new Pair<>(0, start));
    int remainingTargets = targets.size();
    while (!toVisit.isEmpty()) {
        final Pair<Integer, TilePosition> distanceAndTilePosition = toVisit.poll();
        final int currentDist = distanceAndTilePosition.getFirst();
        final TilePosition current = distanceAndTilePosition.getSecond();
        final Tile currentTile = getMap().getData().getTile(current, CheckMode.NO_CHECK);
        // bwem_assert(currentTile.InternalData() == currentDist);
        if (!(((TileImpl) currentTile).getInternalData() == currentDist)) {
            throw new IllegalStateException("currentTile.InternalData().intValue()=" + ((TileImpl) currentTile).getInternalData() + ", currentDist=" + currentDist);
        }
        // resets Tile::m_internalData for future usage
        ((TileImpl) currentTile).setInternalData(0);
        ((TileImpl) currentTile).getMarkable().setMarked();
        for (int i = 0; i < targets.size(); ++i) {
            if (current.equals(targets.get(i))) {
                distances[i] = (int) Math.round(currentDist * 32.0 / 10000.0);
                --remainingTargets;
            }
        }
        if (remainingTargets == 0) {
            break;
        }
        final TilePosition[] deltas = { new TilePosition(-1, -1), new TilePosition(0, -1), new TilePosition(+1, -1), new TilePosition(-1, 0), new TilePosition(+1, 0), new TilePosition(-1, +1), new TilePosition(0, +1), new TilePosition(+1, +1) };
        for (final TilePosition delta : deltas) {
            final boolean diagonalMove = (delta.getX() != 0) && (delta.getY() != 0);
            final int newNextDist = currentDist + (diagonalMove ? 14142 : 10000);
            final TilePosition next = current.add(delta);
            if (getMap().getData().getMapData().isValid(next)) {
                final Tile nextTile = getMap().getData().getTile(next, CheckMode.NO_CHECK);
                if (!((TileImpl) nextTile).getMarkable().isMarked()) {
                    if (((TileImpl) nextTile).getInternalData() != 0) {
                        // next already in toVisit
                        if (newNextDist < ((TileImpl) nextTile).getInternalData()) {
                            // nextNewDist < nextOldDist
                            // To update next's distance, we need to remove-insert it from toVisit:
                            // bwem_assert(iNext != range.second);
                            final boolean removed = toVisit.remove(new Pair<>(((TileImpl) nextTile).getInternalData(), next));
                            if (!removed) {
                                throw new IllegalStateException();
                            }
                            ((TileImpl) nextTile).setInternalData(newNextDist);
                            toVisit.offer(new Pair<>(newNextDist, next));
                        }
                    } else if ((nextTile.getAreaId().equals(getId())) || (nextTile.getAreaId().equals(UNINITIALIZED))) {
                        ((TileImpl) nextTile).setInternalData(newNextDist);
                        toVisit.offer(new Pair<>(newNextDist, next));
                    }
                }
            }
        }
    }
    // bwem_assert(!remainingTargets);
    if (!(remainingTargets == 0)) {
        throw new IllegalStateException();
    }
    for (final Pair<Integer, TilePosition> distanceAndTilePosition : toVisit) {
        final TileImpl tileToUpdate = ((TileImpl) getMap().getData().getTile(distanceAndTilePosition.getSecond(), CheckMode.NO_CHECK));
        tileToUpdate.setInternalData(0);
    }
    return distances;
}
Also used : TileImpl(bwem.tile.TileImpl) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) PriorityQueue(java.util.PriorityQueue) ChokePoint(bwem.ChokePoint) TilePosition(org.openbw.bwapi4j.TilePosition) Pair(org.openbw.bwapi4j.util.Pair)

Example 3 with Tile

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

the class AreaInitializerImpl method validateBaseLocation.

@Override
public boolean validateBaseLocation(final AdvancedData mapAdvancedData, final TilePosition location, final List<Mineral> blockingMinerals) {
    final TilePosition dimCC = UnitType.Terran_Command_Center.tileSize();
    blockingMinerals.clear();
    for (int dy = -3; dy < dimCC.getY() + 3; ++dy) for (int dx = -3; dx < dimCC.getX() + 3; ++dx) {
        final TilePosition deltaLocation = location.add(new TilePosition(dx, dy));
        if (mapAdvancedData.getMapData().isValid(deltaLocation)) {
            final Tile deltaTile = mapAdvancedData.getTile(deltaLocation, CheckMode.NO_CHECK);
            final Neutral deltaTileNeutral = deltaTile.getNeutral();
            if (deltaTileNeutral != null) {
                if (deltaTileNeutral instanceof Geyser) {
                    return false;
                } else if (deltaTileNeutral instanceof Mineral) {
                    final Mineral deltaTileMineral = (Mineral) deltaTileNeutral;
                    if (deltaTileMineral.getInitialAmount() <= 8) {
                        blockingMinerals.add(deltaTileMineral);
                    } else {
                        return false;
                    }
                }
            }
        }
    }
    // checks the distance to the bases already created:
    for (final Base base : getBases()) {
        if (BwemExt.roundedDist(base.getLocation(), location) < BwemExt.min_tiles_between_Bases) {
            return false;
        }
    }
    return true;
}
Also used : Mineral(bwem.unit.Mineral) Neutral(bwem.unit.Neutral) Geyser(bwem.unit.Geyser) TilePosition(org.openbw.bwapi4j.TilePosition) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) ChokePoint(bwem.ChokePoint) Base(bwem.Base)

Example 4 with Tile

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

the class AreaInitializerImpl method createBases.

@Override
public void createBases(final AdvancedData mapAdvancedData) {
    final TilePosition resourceDepotDimensions = UnitType.Terran_Command_Center.tileSize();
    final List<Resource> remainingResources = new ArrayList<>();
    for (final Mineral mineral : getMinerals()) {
        if ((mineral.getInitialAmount() >= 40) && !mineral.isBlocking()) {
            remainingResources.add(mineral);
        }
    }
    for (final Geyser geyser : getGeysers()) {
        if ((geyser.getInitialAmount() >= 300) && !geyser.isBlocking()) {
            remainingResources.add(geyser);
        }
    }
    while (!remainingResources.isEmpty()) {
        // 1) Calculate the SearchBoundingBox (needless to search too far from the remainingResources):
        TilePosition topLeftResources = new TilePosition(Integer.MAX_VALUE, Integer.MAX_VALUE);
        TilePosition bottomRightResources = new TilePosition(Integer.MIN_VALUE, Integer.MIN_VALUE);
        for (final Resource r : remainingResources) {
            final Pair<TilePosition, TilePosition> pair1 = BwemExt.makeBoundingBoxIncludePoint(topLeftResources, bottomRightResources, r.getTopLeft());
            topLeftResources = pair1.getFirst();
            bottomRightResources = pair1.getSecond();
            final Pair<TilePosition, TilePosition> pair2 = BwemExt.makeBoundingBoxIncludePoint(topLeftResources, bottomRightResources, r.getBottomRight());
            topLeftResources = pair2.getFirst();
            bottomRightResources = pair2.getSecond();
        }
        final TilePosition dimensionsBetweenResourceDepotAndResources = new TilePosition(BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES, BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES);
        TilePosition topLeftSearchBoundingBox = topLeftResources.subtract(resourceDepotDimensions).subtract(dimensionsBetweenResourceDepotAndResources);
        TilePosition bottomRightSearchBoundingBox = bottomRightResources.add(new TilePosition(1, 1)).add(dimensionsBetweenResourceDepotAndResources);
        topLeftSearchBoundingBox = BwemExt.makePointFitToBoundingBox(topLeftSearchBoundingBox, getTopLeft(), getBottomRight().subtract(resourceDepotDimensions).add(new TilePosition(1, 1)));
        bottomRightSearchBoundingBox = BwemExt.makePointFitToBoundingBox(bottomRightSearchBoundingBox, getTopLeft(), getBottomRight().subtract(resourceDepotDimensions).add(new TilePosition(1, 1)));
        // 2) Mark the Tiles with their distances from each remaining Resource (Potential Fields >= 0)
        for (final Resource r : remainingResources) for (int dy = -resourceDepotDimensions.getY() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dy < r.getSize().getY() + resourceDepotDimensions.getY() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dy) for (int dx = -resourceDepotDimensions.getX() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dx < r.getSize().getX() + resourceDepotDimensions.getX() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dx) {
            final TilePosition deltaTilePosition = r.getTopLeft().add(new TilePosition(dx, dy));
            if (mapAdvancedData.getMapData().isValid(deltaTilePosition)) {
                final Tile tile = mapAdvancedData.getTile(deltaTilePosition, CheckMode.NO_CHECK);
                int dist = (BwemExt.distToRectangle(BwemExt.center(deltaTilePosition), r.getTopLeft().toPosition(), r.getSize().toPosition()) + (TilePosition.SIZE_IN_PIXELS / 2)) / TilePosition.SIZE_IN_PIXELS;
                int score = Math.max(BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES + 3 - dist, 0);
                if (r instanceof Geyser) {
                    // somewhat compensates for Geyser alone vs the several minerals
                    score *= 3;
                }
                if (tile.getAreaId().equals(getId())) {
                    // note the additive effect (assume tile.InternalData() is 0 at the beginning)
                    ((TileImpl) tile).setInternalData(((TileImpl) tile).getInternalData() + score);
                }
            }
        }
        // 3) Invalidate the 7 x 7 Tiles around each remaining Resource (Starcraft rule)
        for (final Resource r : remainingResources) for (int dy = -3; dy < r.getSize().getY() + 3; ++dy) for (int dx = -3; dx < r.getSize().getX() + 3; ++dx) {
            final TilePosition deltaTilePosition = r.getTopLeft().add(new TilePosition(dx, dy));
            if (mapAdvancedData.getMapData().isValid(deltaTilePosition)) {
                final Tile tileToUpdate = mapAdvancedData.getTile(deltaTilePosition, CheckMode.NO_CHECK);
                ((TileImpl) tileToUpdate).setInternalData(-1);
            }
        }
        // 4) Search the best location inside the SearchBoundingBox:
        TilePosition bestLocation = null;
        int bestScore = 0;
        final List<Mineral> blockingMinerals = new ArrayList<>();
        for (int y = topLeftSearchBoundingBox.getY(); y <= bottomRightSearchBoundingBox.getY(); ++y) for (int x = topLeftSearchBoundingBox.getX(); x <= bottomRightSearchBoundingBox.getX(); ++x) {
            final int score = computeBaseLocationScore(mapAdvancedData, new TilePosition(x, y));
            if (score > bestScore) {
                if (validateBaseLocation(mapAdvancedData, new TilePosition(x, y), blockingMinerals)) {
                    bestScore = score;
                    bestLocation = new TilePosition(x, y);
                }
            }
        }
        // 5) Clear Tile::m_internalData (required due to our use of Potential Fields: see comments in 2))
        for (Resource r : remainingResources) {
            for (int dy = -resourceDepotDimensions.getY() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dy < r.getSize().getY() + resourceDepotDimensions.getY() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dy) for (int dx = -resourceDepotDimensions.getX() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dx < r.getSize().getX() + resourceDepotDimensions.getX() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dx) {
                final TilePosition deltaTilePosition = r.getTopLeft().add(new TilePosition(dx, dy));
                if (mapAdvancedData.getMapData().isValid(deltaTilePosition)) {
                    final Tile tileToUpdate = mapAdvancedData.getTile(deltaTilePosition, CheckMode.NO_CHECK);
                    ((TileImpl) tileToUpdate).setInternalData(0);
                }
            }
        }
        if (bestScore == 0) {
            break;
        }
        // 6) Create a new Base at bestLocation, assign to it the relevant resources and remove them from RemainingResources:
        final List<Resource> assignedResources = new ArrayList<>();
        for (final Resource r : remainingResources) {
            if (BwemExt.distToRectangle(r.getCenter(), bestLocation.toPosition(), resourceDepotDimensions.toPosition()) + 2 <= BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES * TilePosition.SIZE_IN_PIXELS) {
                assignedResources.add(r);
            }
        }
        // for (int i = 0; i < remainingResources.size(); ++i) {
        // Resource r = remainingResources.get(i);
        // if (assignedResources.contains(r)) {
        // remainingResources.remove(i--);
        // }
        // }
        remainingResources.removeIf(assignedResources::contains);
        if (assignedResources.isEmpty()) {
            break;
        }
        super.bases.add(new BaseImpl(this, bestLocation, assignedResources, blockingMinerals));
    }
}
Also used : Mineral(bwem.unit.Mineral) TileImpl(bwem.tile.TileImpl) Resource(bwem.unit.Resource) ArrayList(java.util.ArrayList) BaseImpl(bwem.BaseImpl) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) ChokePoint(bwem.ChokePoint) Geyser(bwem.unit.Geyser) TilePosition(org.openbw.bwapi4j.TilePosition)

Example 5 with Tile

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

the class Graph method computeDistances.

// //////////////////////////////////////////////////////////////////////
// Returns Distances such that Distances[i] == ground_distance(start, targets[i]) in pixels
// Any Distances[i] may be 0 (meaning targets[i] is not reachable).
// This may occur in the case where start and targets[i] leave in different continents or due to Bloqued intermediate ChokePoint(s).
// For each reached target, the shortest path can be derived using
// the backward trace set in cp->getPathBackTrace() for each intermediate ChokePoint cp from the target.
// Note: same algo than Area::computeDistances (derived from Dijkstra)
private int[] computeDistances(final ChokePoint start, final List<ChokePoint> targets) {
    final int[] distances = new int[targets.size()];
    TileImpl.getStaticMarkable().unmarkAll();
    final Queue<Pair<Integer, ChokePoint>> toVisit = new PriorityQueue<>(Comparator.comparingInt(a -> a.first));
    toVisit.offer(new Pair<>(0, start));
    int remainingTargets = targets.size();
    while (!toVisit.isEmpty()) {
        final Pair<Integer, ChokePoint> distanceAndChokePoint = toVisit.poll();
        final int currentDist = distanceAndChokePoint.first;
        final ChokePoint current = distanceAndChokePoint.second;
        final Tile currentTile = getMap().getData().getTile(current.getCenter().toTilePosition(), CheckMode.NO_CHECK);
        // bwem_assert(currentTile.InternalData() == currentDist);
        if (!(((TileImpl) currentTile).getInternalData() == currentDist)) {
            throw new IllegalStateException();
        }
        // resets Tile::m_internalData for future usage
        ((TileImpl) currentTile).setInternalData(0);
        ((TileImpl) currentTile).getMarkable().setMarked();
        for (int i = 0; i < targets.size(); ++i) {
            if (current == targets.get(i)) {
                distances[i] = currentDist;
                --remainingTargets;
            }
        }
        if (remainingTargets == 0) {
            break;
        }
        if (current.isBlocked() && (!current.equals(start))) {
            continue;
        }
        for (final Area pArea : new Area[] { current.getAreas().getFirst(), current.getAreas().getSecond() }) {
            for (final ChokePoint next : pArea.getChokePoints()) {
                if (!next.equals(current)) {
                    final int newNextDist = currentDist + distance(current, next);
                    final Tile nextTile = getMap().getData().getTile(next.getCenter().toTilePosition(), CheckMode.NO_CHECK);
                    if (!((TileImpl) nextTile).getMarkable().isMarked()) {
                        if (((TileImpl) nextTile).getInternalData() != 0) {
                            // next already in toVisit
                            if (newNextDist < ((TileImpl) nextTile).getInternalData()) {
                                // nextNewDist < nextOldDist
                                // To update next's distance, we need to remove-insert it from toVisit:
                                // bwem_assert(iNext != range.second);
                                final boolean removed = toVisit.remove(new Pair<>(((TileImpl) nextTile).getInternalData(), next));
                                if (!removed) {
                                    throw new IllegalStateException();
                                }
                                ((TileImpl) nextTile).setInternalData(newNextDist);
                                ((ChokePointImpl) next).setPathBackTrace(current);
                                toVisit.offer(new Pair<>(newNextDist, next));
                            }
                        } else {
                            ((TileImpl) nextTile).setInternalData(newNextDist);
                            ((ChokePointImpl) next).setPathBackTrace(current);
                            toVisit.offer(new Pair<>(newNextDist, next));
                        }
                    }
                }
            }
        }
    }
    // reset Tile::m_internalData for future usage
    for (Pair<Integer, ChokePoint> distanceToChokePoint : toVisit) {
        ((TileImpl) getMap().getData().getTile(distanceToChokePoint.second.getCenter().toTilePosition(), CheckMode.NO_CHECK)).setInternalData(0);
    }
    return distances;
}
Also used : 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) TileImpl(bwem.tile.TileImpl) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) PriorityQueue(java.util.PriorityQueue) Area(bwem.area.Area) Pair(org.openbw.bwapi4j.util.Pair) MutablePair(org.apache.commons.lang3.tuple.MutablePair)

Aggregations

MiniTile (bwem.tile.MiniTile)8 Tile (bwem.tile.Tile)8 TilePosition (org.openbw.bwapi4j.TilePosition)7 ChokePoint (bwem.ChokePoint)5 TileImpl (bwem.tile.TileImpl)4 Geyser (bwem.unit.Geyser)3 Mineral (bwem.unit.Mineral)3 ArrayList (java.util.ArrayList)3 Area (bwem.area.Area)2 Neutral (bwem.unit.Neutral)2 StaticBuilding (bwem.unit.StaticBuilding)2 PriorityQueue (java.util.PriorityQueue)2 Pair (org.openbw.bwapi4j.util.Pair)2 Base (bwem.Base)1 BaseImpl (bwem.BaseImpl)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