use of bwem.tile.TileImpl 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;
}
use of bwem.tile.TileImpl 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));
}
}
use of bwem.tile.TileImpl 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;
}
use of bwem.tile.TileImpl 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;
}
use of bwem.tile.TileImpl 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();
}
}
Aggregations