use of bwem.area.typedef.AreaId 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));
}
}
}
use of bwem.area.typedef.AreaId in project BWAPI4J by OpenBW.
the class Graph method createAreas.
// Creates a new Area for each pair (top, miniTiles) in areasList (See Area::top() and Area::miniTiles())
public void createAreas(final List<MutablePair<WalkPosition, Integer>> areasList) {
for (int id = 1; id <= areasList.size(); ++id) {
final WalkPosition top = areasList.get(id - 1).getLeft();
final int miniTileCount = areasList.get(id - 1).getRight();
this.areas.add(new AreaInitializerImpl(getMap(), new AreaId(id), top, miniTileCount));
}
}
use of bwem.area.typedef.AreaId in project BWAPI4J by OpenBW.
the class Graph method createRawFrontierByAreaPairMap.
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// 2) Dispatch the global raw frontier between all the relevant pairs of areas:
// ----------------------------------------------------------------------
private java.util.Map<MutablePair<AreaId, AreaId>, List<WalkPosition>> createRawFrontierByAreaPairMap(final List<MutablePair<MutablePair<AreaId, AreaId>, WalkPosition>> rawFrontier) {
final java.util.Map<MutablePair<AreaId, AreaId>, List<WalkPosition>> rawFrontierByAreaPair = new HashMap<>();
for (final MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> raw : rawFrontier) {
int a = raw.getLeft().getLeft().intValue();
int b = raw.getLeft().getRight().intValue();
if (a > b) {
final int a_tmp = a;
a = b;
b = a_tmp;
}
// bwem_assert(a <= b);
if (!(a <= b)) {
throw new IllegalStateException();
}
// bwem_assert((a >= 1) && (b <= areasCount()));
if (!((a >= 1) && (b <= getAreaCount()))) {
throw new IllegalStateException();
}
final MutablePair<AreaId, AreaId> key = new MutablePair<>(new AreaId(a), new AreaId(b));
rawFrontierByAreaPair.computeIfAbsent(key, mp -> new ArrayList<>()).add(raw.getRight());
}
return rawFrontierByAreaPair;
}
use of bwem.area.typedef.AreaId in project BWAPI4J by OpenBW.
the class MapInitializerImpl method replaceAreaIds.
@Override
public void replaceAreaIds(final WalkPosition p, final AreaId newAreaId) {
final MiniTile origin = ((AdvancedDataInitializer) getData()).getMiniTile_(p, CheckMode.NO_CHECK);
final AreaId oldAreaId = origin.getAreaId();
((MiniTileImpl) origin).replaceAreaId(newAreaId);
List<WalkPosition> toSearch = new ArrayList<>();
toSearch.add(p);
while (!toSearch.isEmpty()) {
final WalkPosition current = toSearch.remove(toSearch.size() - 1);
final WalkPosition[] deltas = { new WalkPosition(0, -1), new WalkPosition(-1, 0), new WalkPosition(+1, 0), new WalkPosition(0, +1) };
for (final WalkPosition delta : deltas) {
final WalkPosition next = current.add(delta);
if (getData().getMapData().isValid(next)) {
final MiniTile miniTile = ((AdvancedDataInitializer) getData()).getMiniTile_(next, CheckMode.NO_CHECK);
if (miniTile.getAreaId().equals(oldAreaId)) {
toSearch.add(next);
((MiniTileImpl) miniTile).replaceAreaId(newAreaId);
}
}
}
}
// also replaces references of oldAreaId by newAreaId in getRawFrontier:
if (newAreaId.intValue() > 0) {
for (final MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> f : super.rawFrontier) {
if (f.getLeft().getLeft().equals(oldAreaId)) {
f.getLeft().setLeft(newAreaId);
}
if (f.getLeft().getRight().equals(oldAreaId)) {
f.getLeft().setRight(newAreaId);
}
}
}
}
use of bwem.area.typedef.AreaId in project BWAPI4J by OpenBW.
the class MiniTileImpl method setWalkable.
public void setWalkable(boolean walkable) {
this.areaId = new AreaId(walkable ? -1 : 0);
this.altitude = new Altitude(walkable ? -1 : 1);
}
Aggregations