use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.
the class MapImpl method findNeighboringAreas.
public MutablePair<AreaId, AreaId> findNeighboringAreas(final WalkPosition p) {
final MutablePair<AreaId, AreaId> result = new MutablePair<>(null, null);
final WalkPosition[] deltas = { new WalkPosition(0, -1), new WalkPosition(-1, 0), new WalkPosition(+1, 0), new WalkPosition(0, +1) };
for (final WalkPosition delta : deltas) {
if (getData().getMapData().isValid(p.add(delta))) {
final AreaId areaId = getData().getMiniTile(p.add(delta), CheckMode.NO_CHECK).getAreaId();
if (areaId.intValue() > 0) {
if (result.getLeft() == null) {
result.setLeft(areaId);
} else if (!result.getLeft().equals(areaId)) {
if (result.getRight() == null || ((areaId.intValue() < result.getRight().intValue()))) {
result.setRight(areaId);
}
}
}
}
}
return result;
}
use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.
the class MapInitializerImpl method getSortedMiniTilesByDescendingAltitude.
@Override
public List<MutablePair<WalkPosition, MiniTile>> getSortedMiniTilesByDescendingAltitude() {
final List<MutablePair<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 = ((TerrainDataInitializer) getData()).getMiniTile_(w, CheckMode.NO_CHECK);
if (((MiniTileImpl) miniTile).isAreaIdMissing()) {
miniTilesByDescendingAltitude.add(new MutablePair<>(w, miniTile));
}
}
}
miniTilesByDescendingAltitude.sort(new PairGenericMiniTileAltitudeComparator<>());
Collections.reverse(miniTilesByDescendingAltitude);
return miniTilesByDescendingAltitude;
}
use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.
the class MapInitializerImpl method createAreas.
// Initializes Graph with the valid and big enough areas in tempAreaList.
@Override
public void createAreas(final List<TempAreaInfo> tempAreaList, final int areaMinMiniTiles) {
final List<MutablePair<WalkPosition, Integer>> areasList = new ArrayList<>();
int newAreaId = 1;
int newTinyAreaId = -2;
for (final TempAreaInfo tempArea : tempAreaList) {
if (tempArea.isValid()) {
if (tempArea.getSize() >= areaMinMiniTiles) {
// bwem_assert(newAreaId <= tempArea.id());
if (!(newAreaId <= tempArea.getId().intValue())) {
throw new IllegalStateException();
}
if (newAreaId != tempArea.getId().intValue()) {
replaceAreaIds(tempArea.getWalkPositionWithHighestAltitude(), new AreaId(newAreaId));
}
areasList.add(new MutablePair<>(tempArea.getWalkPositionWithHighestAltitude(), tempArea.getSize()));
++newAreaId;
} else {
replaceAreaIds(tempArea.getWalkPositionWithHighestAltitude(), new AreaId(newTinyAreaId));
--newTinyAreaId;
}
}
}
getGraph().createAreas(areasList);
}
use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.
the class MapInitializerImpl method computeTempAreas.
@Override
public List<TempAreaInfo> computeTempAreas(final List<MutablePair<WalkPosition, MiniTile>> miniTilesByDescendingAltitude) {
final List<TempAreaInfo> tempAreaList = new ArrayList<>();
// tempAreaList[0] left unused, as AreaIds are > 0
tempAreaList.add(new TempAreaInfo());
for (final MutablePair<WalkPosition, MiniTile> current : miniTilesByDescendingAltitude) {
final WalkPosition pos = new WalkPosition(current.getLeft().getX(), current.getLeft().getY());
final MiniTile cur = current.getRight();
final MutablePair<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));
} 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 MutablePair<>(neighboringAreas, pos));
}
}
}
// Remove from the frontier obsolete positions
rawFrontier.removeIf(f -> f.getLeft().getLeft().equals(f.getLeft().getRight()));
return tempAreaList;
}
use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.
the class PairGenericMiniTileAltitudeComparatorTest method testPairGenericMiniTileAltitudeComparator.
@Test
public void testPairGenericMiniTileAltitudeComparator() {
final List<MutablePair<WalkPosition, MiniTile>> list = new ArrayList<>();
final MiniTile m1 = new MiniTileImpl();
((MiniTileImpl) m1).setAreaId(new AreaId(2));
((MiniTileImpl) m1).setAltitude(new Altitude(5));
final MiniTile m2 = new MiniTileImpl();
((MiniTileImpl) m2).setAreaId(new AreaId(1));
((MiniTileImpl) m2).setAltitude(new Altitude(2));
final MiniTile m3 = new MiniTileImpl();
((MiniTileImpl) m3).setAreaId(new AreaId(3));
((MiniTileImpl) m3).setAltitude(new Altitude(7));
final MiniTile m4 = new MiniTileImpl();
((MiniTileImpl) m4).setAreaId(new AreaId(5));
((MiniTileImpl) m4).setAltitude(new Altitude(1));
final MiniTile m5 = new MiniTileImpl();
((MiniTileImpl) m5).setAreaId(new AreaId(4));
((MiniTileImpl) m5).setAltitude(new Altitude(8));
list.add(new MutablePair<>(new WalkPosition(5, 15), m1));
list.add(new MutablePair<>(new WalkPosition(2, 98), m2));
list.add(new MutablePair<>(new WalkPosition(63, 123), m3));
list.add(new MutablePair<>(new WalkPosition(103, 435), m4));
list.add(new MutablePair<>(new WalkPosition(89, 77), m5));
// Sort by descending order.
Collections.sort(list, new PairGenericMiniTileAltitudeComparator<>());
Collections.reverse(list);
Assert.assertEquals(new MutablePair<>(new WalkPosition(89, 77), m5), list.get(0));
Assert.assertEquals(new MutablePair<>(new WalkPosition(63, 123), m3), list.get(1));
Assert.assertEquals(new MutablePair<>(new WalkPosition(5, 15), m1), list.get(2));
Assert.assertEquals(new MutablePair<>(new WalkPosition(2, 98), m2), list.get(3));
Assert.assertEquals(new MutablePair<>(new WalkPosition(103, 435), m4), list.get(4));
}
Aggregations