@Override public CoordinateStream stream() { return stream(cX, cY, startRadius, maxRadius); }
private static CoordinateStream getEarthAreaStream(int x, int y) { return HexGridArea.stream(x, y, 0, 1); } }
public static CoordinateStream streamBorder(short centerX, short centerY, int radius) { return stream(centerX, centerY, radius, radius); }
public boolean isHexAreaOfType(int x, int y, int minRadius, int maxRadius, ELandscapeType... landscapeTypes) { return HexGridArea.stream(x, y, minRadius, maxRadius) .filter((currX, currY) -> !isLandscapeOf(currX, currY, landscapeTypes)) .isEmpty(); }
@Override public boolean isCoastReachable(ShortPoint2D position) { return !HexGridArea.stream(position.x, position.y, 0, 3) .filterBounds(width, height) .filter((x, y) -> !landscapeGrid.getLandscapeTypeAt(x, y).isWater) .isEmpty(); }
@Override protected void unloadFerry() { if (passengers.isEmpty()) { return; } ShortPoint2D position = super.getPosition(); AbstractMovableGrid grid = super.getGrid(); HexGridArea.stream(position.x, position.y, 2, Constants.MAX_FERRY_UNLOADING_RADIUS) .filterBounds(grid.getWidth(), grid.getHeight()) .filter((x, y) -> !grid.isWater(x, y)) .iterate((x, y) -> { ILogicMovable passenger = passengers.get(passengers.size() - 1); if (grid.isValidPosition(passenger, x, y) && grid.isFreePosition(x,y)) { passenger.leaveFerryAt(new ShortPoint2D(x, y)); passengers.remove(passengers.size() - 1); } return !passengers.isEmpty(); }); }
private BitSet calculatePositionsInDistanceTrivial(int width, int height, ICoordinatePredicate provider, int maxDistance) { BitSet inDistance = new BitSet(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (provider.test(x, y)) { HexGridArea.stream(x, y, 0, maxDistance) .filterBounds(width, height) .forEach((currX, currY) -> inDistance.set(currY * width + currX)); } } } return inDistance; } }
private Optional<IGuiMovable> getSelectableMovable(short selectedX, short selectedY) { return HexGridArea.stream(selectedX, selectedY, 0, 2).iterateForResult((x, y) -> { IGuiMovable movable = grid.getMovable(x, y); if (movable != null && canSelectPlayer(movable.getPlayer())) { return Optional.of(movable); } else { return Optional.empty(); } }); }
final boolean isNavigable(int x, int y) { Optional<ShortPoint2D> blockingOptional = HexGridArea.stream(x, y, 0, 2) .filterBounds(width, height) .filter((x1, y1) -> !landscapeGrid.getLandscapeTypeAt(x1, y1).isWater || objectsGrid.getMapObjectAt(x1, y1, EMapObjectType.DOCK) != null) .getFirst(); return !blockingOptional.isPresent(); }
private void pushShips() { HexGridArea.stream(position.x, position.y, 1, SHIP_PUSH_DISTANCE) .filterBounds(grid.getWidth(), grid.getHeight()) .forEach((x, y) -> { ILogicMovable blockingMovable = grid.getMovableAt(x, y); if (blockingMovable != null && blockingMovable.isShip()) { blockingMovable.push(this); } }); }
@Override public final ShortPoint2D calcDecentralizeVector(short x, short y) { MutablePoint2D vector = new MutablePoint2D(); HexGridArea.stream(x, y, 1, Constants.MOVABLE_FLOCK_TO_DECENTRALIZE_MAX_RADIUS).forEach((currX, currY) -> { int radius = ShortPoint2D.getOnGridDist(currX - x, currY - y); int factor; if (!MainGrid.this.isInBounds(currX, currY)) { factor = radius == 1 ? 6 : 2; } else if (!movableGrid.hasNoMovableAt(currX, currY)) { factor = Constants.MOVABLE_FLOCK_TO_DECENTRALIZE_MAX_RADIUS - radius + 1; } else { return; } vector.x += (x - currX) * factor; vector.y += (y - currY) * factor; }); return vector.toShortPoint2D(); }
private EDirection getCloseForeignTile() { EDirection[] bestNeighbourDir = new EDirection[1]; double[] bestNeighbourDistance = new double[] { Double.MAX_VALUE }; // distance from start point ShortPoint2D position = movable.getPosition(); HexGridArea.stream(position.x, position.y, 1, 6) .filter((x, y) -> super.isValidPosition(x, y) && canWorkOnPos(x, y)) .forEach((x, y) -> { double distance = ShortPoint2D.getOnGridDist(x - centerPos.x, y - centerPos.y); if (distance < bestNeighbourDistance[0]) { bestNeighbourDistance[0] = distance; bestNeighbourDir[0] = EDirection.getApproxDirection(position.x, position.y, x, y); } }); return bestNeighbourDir[0]; }
private IAttackable getEnemyInSearchArea(IPlayer searchingPlayer, HexGridArea area, boolean isBowman, boolean includeTowers) { return area.stream().filterBounds(width, height).iterateForResult((x, y) -> { IAttackable currAttackable = movableGrid.getMovableAt(x, y); if (includeTowers && !isBowman && currAttackable == null) { currAttackable = (IAttackable) objectsGrid.getMapObjectAt(x, y, EMapObjectType.ATTACKABLE_TOWER); } if (currAttackable != null && MovableGrid.isEnemy(searchingPlayer, currAttackable)) { return Optional.of(currAttackable); } else { return Optional.empty(); } }).orElse(null); }
public ShortPoint2D pushMaterialForced(int x, int y, EMaterialType materialType) { return HexGridArea.stream(x, y, 0, 200) .filterBounds(grid.getWidth(), grid.getHeight()) .filter((currX, currY) -> canForcePushMaterial(currX, currY, materialType)) .iterateForResult((currX, currY) -> { pushMaterial(currX, currY, materialType); return Optional.of(new ShortPoint2D(currX, currY)); }).orElse(null); }
private void assertPositionsIterate(ShortPoint2D center, int startRadius, int maxRadius, int expectedCount) { int width = center.x + maxRadius + 1; int height = center.y + maxRadius + 1; BitSet positions = new BitSet(width * height); MutableInt counter = new MutableInt(0); HexGridArea.stream(center.x, center.y, startRadius, maxRadius) .forEach((x, y) -> { int onGridDist = center.getOnGridDistTo(new ShortPoint2D(x, y)); if (!(startRadius <= onGridDist && onGridDist <= maxRadius)) { fail("onGridDist: " + onGridDist + " not in the expected range of [" + startRadius + "|" + maxRadius + "] pos: (" + x + "|" + y + ")"); } positions.set(x + y * width); DebugImagesHelper.writeDebugImageBoolean("count-" + counter.value, width, height, (imageX, imageY) -> positions.get(imageX + imageY * width)); counter.value++; }); assertEquals(expectedCount, counter.value); } }
@Test public void testIterateStopsAfterFalse() { int expectedVisits = 5; MutableInt counter = new MutableInt(0); boolean wasNotStopped = HexGridArea.stream(10, 10, 3, 10).iterate((x, y) -> { counter.value++; return counter.value != expectedVisits; }); assertEquals(expectedVisits, counter.value); assertFalse(wasNotStopped); }
@Override public FerryEntrance ferryAtPosition(ShortPoint2D position, byte playerId) { Optional<ILogicMovable> ferryOptional = HexGridArea.stream(position.x, position.y, 0, Constants.MAX_FERRY_ENTRANCE_SEARCH_DISTANCE) .filterBounds(width, height) .filter((x, y) -> landscapeGrid.getLandscapeTypeAt(x, y).isWater()) .iterateForResult((x, y) -> { ILogicMovable movable = movableGrid.getMovableAt(x, y); return Optional.ofNullable(movable).filter(m -> m.getMovableType() == EMovableType.FERRY); }); if (!ferryOptional.isPresent()) { return null; } ILogicMovable ferry = ferryOptional.get(); ShortPoint2D ferryPosition = ferry.getPosition(); Optional<ShortPoint2D> entranceOptional = HexGridArea.stream(ferryPosition.x, ferryPosition.y, 0, Constants.MAX_FERRY_ENTRANCE_SEARCH_DISTANCE) .filterBounds(width, height) .filter((x, y) -> !isBlocked(x, y)) .getFirst(); if (!entranceOptional.isPresent()) { return null; } return new FerryEntrance(ferry, entranceOptional.get()); }
@Test public void testIterateSinglePoint() { MutableInt counter = new MutableInt(0); HexGridArea.stream(10, 10, 0, 0).forEach((x, y) -> { counter.value++; assertEquals(new ShortPoint2D(10, 10), new ShortPoint2D(x, y)); }); assertEquals(1, counter.value); }
@Test public void testIterateForResultStopsAfterResult() { int expectedVisits = 5; Object expectedResultObject = new Object(); MutableInt counter = new MutableInt(0); Optional<Object> actualResultObject = HexGridArea.stream(10, 10, 3, 10).iterateForResult((x, y) -> { counter.value++; if (counter.value == expectedVisits) { return Optional.of(expectedResultObject); } else { return Optional.empty(); } }); assertEquals(expectedVisits, counter.value); assertTrue(actualResultObject.isPresent()); assertSame(expectedResultObject, actualResultObject.get()); }
@Override public DockPosition findValidDockPosition(ShortPoint2D requestedPosition, ShortPoint2D buildingPosition, int maximumDistance) { if (!isWaterSafe(requestedPosition.x, requestedPosition.y)) { return null; // requested position is not in water } short buildingPartition = partitionsGrid.getPartitionIdAt(buildingPosition.x, buildingPosition.y); Optional<ShortPoint2D> coastPosition = HexGridArea .stream(requestedPosition.x, requestedPosition.y, 0, 10) .filterBounds(width, height) .filter((x, y) -> ShortPoint2D.getOnGridDist(buildingPosition.x, buildingPosition.y, x, y) <= maximumDistance) .filter((x, y) -> !landscapeGrid.getLandscapeTypeAt(x, y).isWater()) .filter((x, y) -> partitionsGrid.getPartitionIdAt(x, y) == buildingPartition) // ensure the dock is the same partition (accessible by worker of building) .filter((x, y) -> { // check that the dock goes from land to water EDirection direction = EDirection.getApproxDirection(x, y, requestedPosition.x, requestedPosition.y); ShortPoint2D firstDockWaterPosition = direction.getNextHexPoint(x, y); ShortPoint2D secondDockWaterPosition = direction.getNextHexPoint(firstDockWaterPosition.x, firstDockWaterPosition.y); return isWaterSafe(firstDockWaterPosition.x, firstDockWaterPosition.y) && isWaterSafe(secondDockWaterPosition.x, secondDockWaterPosition.y); }) .getFirst(); if (!coastPosition.isPresent()) { return null; } EDirection direction = EDirection.getApproxDirection(coastPosition.get(), requestedPosition); return new DockPosition(coastPosition.get(), direction); }