use of org.openbw.bwapi4j.WalkPosition in project Ecgberht by Jabbo16.
the class BWMapInitializer method getSortedMiniTilesByDescendingAltitude.
private List<Pair<WalkPosition, MiniTile>> getSortedMiniTilesByDescendingAltitude() {
final List<Pair<WalkPosition, MiniTile>> miniTilesByDescendingAltitude = new ArrayList<>();
for (int y = 0; y < getData().getMapData().getWalkSize().getY(); ++y) {
for (int x = 0; x < getData().getMapData().getWalkSize().getX(); ++x) {
final WalkPosition w = new WalkPosition(x, y);
final MiniTile miniTile = getData().getMiniTile(w, bwem.util.CheckMode.NO_CHECK);
if (miniTile.isAreaIdMissing()) {
miniTilesByDescendingAltitude.add(new Pair<>(w, miniTile));
}
}
}
miniTilesByDescendingAltitude.sort(MiniTile.BY_ALTITUDE_ORDER);
Collections.reverse(miniTilesByDescendingAltitude);
return miniTilesByDescendingAltitude;
}
use of org.openbw.bwapi4j.WalkPosition in project Ecgberht by Jabbo16.
the class BWMapInitializer method computeTempAreas.
private List<TempAreaInfo> computeTempAreas(final List<Pair<WalkPosition, MiniTile>> miniTilesByDescendingAltitude) {
final List<TempAreaInfo> tempAreaList = new ArrayList<>();
// tempAreaList[0] left unused, as AreaIds are > 0
tempAreaList.add(new TempAreaInfo(asserter));
for (final Pair<WalkPosition, MiniTile> current : miniTilesByDescendingAltitude) {
final WalkPosition pos = new WalkPosition(current.getLeft().getX(), current.getLeft().getY());
final MiniTile cur = current.getRight();
final Pair<AreaId, AreaId> neighboringAreas = findNeighboringAreas(pos);
if (neighboringAreas.getLeft() == null) {
// no neighboring area : creates of a new area
tempAreaList.add(new TempAreaInfo(new AreaId(tempAreaList.size()), cur, pos, asserter));
} else if (neighboringAreas.getRight() == null) {
// one neighboring area : adds cur to the existing area
tempAreaList.get(neighboringAreas.getLeft().intValue()).add(cur);
} else {
// two neighboring areas : adds cur to one of them & possible merging
AreaId smaller = neighboringAreas.getLeft();
AreaId bigger = neighboringAreas.getRight();
if (tempAreaList.get(smaller.intValue()).getSize() > tempAreaList.get(bigger.intValue()).getSize()) {
AreaId smallerTmp = smaller;
smaller = bigger;
bigger = smallerTmp;
}
// Condition for the neighboring areas to merge:
// any_of(StartingLocations().begin(), StartingLocations().end(),
// [&pos](const TilePosition & startingLoc)
// { return dist(TilePosition(pos), startingLoc + TilePosition(2, 1)) <=
// 3;})
boolean cppAlgorithmStdAnyOf = getData().getMapData().getStartingLocations().stream().anyMatch(startingLoc -> BwemExt.dist(pos.toTilePosition(), startingLoc.add(new TilePosition(2, 1))) <= 3.0);
final int curAltitude = cur.getAltitude().intValue();
final int biggerHighestAltitude = tempAreaList.get(bigger.intValue()).getHighestAltitude().intValue();
final int smallerHighestAltitude = tempAreaList.get(smaller.intValue()).getHighestAltitude().intValue();
if ((tempAreaList.get(smaller.intValue()).getSize() < 80) || (smallerHighestAltitude < 80) || ((double) curAltitude / (double) biggerHighestAltitude >= 0.90) || ((double) curAltitude / (double) smallerHighestAltitude >= 0.90) || cppAlgorithmStdAnyOf) {
// adds cur to the absorbing area:
tempAreaList.get(bigger.intValue()).add(cur);
// merges the two neighboring areas:
replaceAreaIds(tempAreaList.get(smaller.intValue()).getWalkPositionWithHighestAltitude(), bigger);
tempAreaList.get(bigger.intValue()).merge(tempAreaList.get(smaller.intValue()));
} else {
// no merge : cur starts or continues the frontier between the two neighboring
// areas
// adds cur to the chosen Area:
tempAreaList.get(chooseNeighboringArea(smaller, bigger).intValue()).add(cur);
super.rawFrontier.add(new Pair<>(neighboringAreas, pos));
}
}
}
// Remove from the frontier obsolete positions
rawFrontier.removeIf(f -> f.getLeft().getLeft().equals(f.getLeft().getRight()));
return tempAreaList;
}
use of org.openbw.bwapi4j.WalkPosition in project Ecgberht by Jabbo16.
the class BWMapInitializer method replaceAreaIds.
private void replaceAreaIds(final WalkPosition p, final AreaId newAreaId) {
final MiniTile origin = getData().getMiniTile(p, bwem.util.CheckMode.NO_CHECK);
final AreaId oldAreaId = origin.getAreaId();
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 = getData().getMiniTile(next, bwem.util.CheckMode.NO_CHECK);
if (miniTile.getAreaId().equals(oldAreaId)) {
toSearch.add(next);
miniTile.replaceAreaId(newAreaId);
}
}
}
}
// also replaces references of oldAreaId by newAreaId in getRawFrontier:
if (newAreaId.intValue() > 0) {
for (final Pair<Pair<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 org.openbw.bwapi4j.WalkPosition in project Ecgberht by Jabbo16.
the class BWMapInitializer method setAltitudesAndGetUpdatedHighestAltitude.
// ----------------------------------------------------------------------
// 3) Dijkstra's algorithm to set altitude for mini tiles.
// ----------------------------------------------------------------------
private Altitude setAltitudesAndGetUpdatedHighestAltitude(final Altitude currentHighestAltitude, final TerrainData terrainData, final List<Pair<WalkPosition, Altitude>> deltasByAscendingAltitude, final List<Pair<WalkPosition, Altitude>> activeSeaSideList, final int altitudeScale) {
Altitude updatedHighestAltitude = currentHighestAltitude;
for (final Pair<WalkPosition, Altitude> deltaAltitude : deltasByAscendingAltitude) {
final WalkPosition d = deltaAltitude.getLeft();
final Altitude altitude = deltaAltitude.getRight();
for (int i = 0; i < activeSeaSideList.size(); ++i) {
final Pair<WalkPosition, Altitude> current = activeSeaSideList.get(i);
if (altitude.intValue() - current.getRight().intValue() >= 2 * altitudeScale) {
// optimization : once a seaside miniTile verifies this condition,
// we can throw it away as it will not generate min altitudes anymore
Utils.fastErase(activeSeaSideList, i--);
} else {
final WalkPosition[] deltas = { new WalkPosition(d.getX(), d.getY()), new WalkPosition(-d.getX(), d.getY()), new WalkPosition(d.getX(), -d.getY()), new WalkPosition(-d.getX(), -d.getY()), new WalkPosition(d.getY(), d.getX()), new WalkPosition(-d.getY(), d.getX()), new WalkPosition(d.getY(), -d.getX()), new WalkPosition(-d.getY(), -d.getX()) };
for (final WalkPosition delta : deltas) {
final WalkPosition w = current.getLeft().add(delta);
if (terrainData.getMapData().isValid(w)) {
final MiniTile miniTile = terrainData.getMiniTile(w, bwem.util.CheckMode.NO_CHECK);
if (miniTile.isAltitudeMissing()) {
if (updatedHighestAltitude != null && updatedHighestAltitude.intValue() > altitude.intValue()) {
asserter.throwIllegalStateException("");
}
updatedHighestAltitude = altitude;
current.setRight(altitude);
miniTile.setAltitude(altitude);
}
}
}
}
}
}
return updatedHighestAltitude;
}
use of org.openbw.bwapi4j.WalkPosition in project Ecgberht by Jabbo16.
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
(MiniTile miniTile, WalkPosition unused) -> (miniTile.getAreaId().intValue() > 0), // visitCond
Pred.accept());
return getArea(w);
}
Aggregations