@Override public CoordinateStream stream() { return stream(x, y); }
private static void setupInitial(int width, int height, ICoordinatePredicate provider, BitSet done, BitSet next) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (provider.test(x, y)) { done.set(width * y + x); // set as done MapNeighboursArea.stream(x, y).filterBounds(width, height).forEach((neighborX, neighborY) -> { // set neighbors for next run next.set(width * neighborY + neighborX); }); } } } } }
public static BitSet calculatePositionsInDistance(int width, int height, ICoordinatePredicate provider, int maxDistance) { int area = width * height; BitSet inDistance = new BitSet(area); BitSet next = new BitSet(area); setupInitial(width, height, provider, inDistance, next); // DebugImagesHelper.writeDebugImageBoolean("inDistance-" + 0, width, height, (x, y) -> inDistance.get(x + y * width)); for (int distance = 1; distance <= maxDistance; distance++) { next.andNot(inDistance); BitSet current = next; if (current.isEmpty()) { break; } BitSet neighbors = new BitSet(area); for (int index = current.nextSetBit(0); index >= 0; index = current.nextSetBit(index + 1)) { int x = index % width; int y = index / width; MapNeighboursArea.stream(x, y).filterBounds(width, height).forEach((neighborX, neighborY) -> { // set neighbors for next run neighbors.set(width * neighborY + neighborX); }); } next = neighbors; inDistance.or(current); // DebugImagesHelper.writeDebugImageBoolean("inDistance-" + distance, width, height, (x, y) -> inDistance.get(x + y * width)); } return inDistance; }
private boolean hasBlockedNeighbor(short x, short y) { return !MapNeighboursArea.stream(x, y).iterate((currX, currY) -> isInBounds(currX, currY) && !flagsGrid.isBlocked(currX, currY)); }
private boolean hasNeighborIngestibleByPioneersOf(int x, int y, Player player) { return !MapNeighboursArea.stream(x, y) .filterBounds(mainGrid.getWidth(), mainGrid.getHeight()) .filter((currX, currY) -> isIngestibleByPioneersOf(currX, currY, player)) .isEmpty(); }