@Override public CoordinateStream stream() { return stream(cX, cY, startRadius, maxRadius); }
public static CoordinateStream streamBorder(ShortPoint2D center, int radius) { return streamBorder(center.x, center.y, radius); } }
area = HexGridArea.stream(x, y, (short) 1, Constants.SOLDIER_SEARCH_RADIUS); } else { area = HexGridArea.streamBorder(x, y, Constants.SOLDIER_SEARCH_RADIUS - 1);
@Test public void testSinglePoint() { HexGridArea area = new HexGridArea(10, 10, 0, 0); HexGridAreaIterator iter = area.iterator(); assertTrue(iter.hasNext()); assertEquals(new ShortPoint2D(10, 10), iter.next()); assertFalse(iter.hasNext()); }
@Override public IAttackable getEnemyInSearchArea(final ShortPoint2D position, final IAttackable searchingAttackable, final short minSearchRadius, final short maxSearchRadius, final boolean includeTowers) { boolean isBowman = searchingAttackable.getMovableType().isBowman(); IAttackable enemy = getEnemyInSearchArea(searchingAttackable.getPlayer(), new HexGridArea(position.x, position.y, minSearchRadius, maxSearchRadius ), isBowman, includeTowers); if (includeTowers && !isBowman && enemy == null) { enemy = getEnemyInSearchArea(searchingAttackable.getPlayer(), new HexGridArea(position.x, position.y, maxSearchRadius, Constants.TOWER_ATTACKABLE_SEARCH_RADIUS), false, true); } return enemy; }
CoordinateStream area; if (informFullArea) { area = HexGridArea.stream(position.x, position.y, 1, Constants.TOWER_ATTACKABLE_SEARCH_RADIUS); } else { area = HexGridArea.streamBorder(position.x, position.y, Constants.TOWER_ATTACKABLE_SEARCH_RADIUS - 1);
private void assertPositions(ShortPoint2D center, int startRadius, int maxRadius, int expectedCount) { HexGridArea area = new HexGridArea(center.x, center.y, startRadius, maxRadius); int count = 0; for (ShortPoint2D pos : area) { count++; int onGridDist = center.getOnGridDistTo(pos); if (!(startRadius <= onGridDist && onGridDist <= maxRadius)) { fail("onGridDist: " + onGridDist + " not in the expected range of [" + startRadius + "|" + maxRadius + "] pos: " + pos); } } assertEquals(expectedCount, count); }
private static CoordinateStream getEarthAreaStream(int x, int y) { return HexGridArea.stream(x, y, 0, 1); } }
public CoordinateStream streamBorder() { return streamBorder(cX, cY, maxRadius); }
public static CoordinateStream streamBorder(short centerX, short centerY, int radius) { return stream(centerX, centerY, radius, radius); }
private void sendManyMovables(ShortPoint2D targetPosition, List<ILogicMovable> movables) { for (int radius = 0, ringsWithoutSuccessCtr = 0; ringsWithoutSuccessCtr <= Math.max(5, 15 - radius + ringsWithoutSuccessCtr) && !movables.isEmpty(); radius++) { MutableInt numberOfSendMovables = new MutableInt(0); HexGridArea .streamBorder(targetPosition.x, targetPosition.y, radius) .filterBounds(grid.getWidth(), grid.getHeight()) .getEvery(2) .forEach((x, y) -> { Optional<ILogicMovable> movableOptional = removeMovableThatCanMoveTo(movables, x, y); movableOptional.ifPresent(movable -> { movable.moveTo(new ShortPoint2D(x, y)); numberOfSendMovables.value++; }); }); if (numberOfSendMovables.value > 0) { ringsWithoutSuccessCtr = 0; } else { ringsWithoutSuccessCtr++; } } }
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(); }
private ShortPoint2D getCloseWorkablePos() { MutablePoint2D bestNeighbourPos = new MutablePoint2D(-1, -1); MutableDouble bestNeighbourDistance = new MutableDouble(Double.MAX_VALUE); // distance from start point HexGridArea.streamBorder(movable.getPosition(), 2).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.value) { bestNeighbourDistance.value = distance; bestNeighbourPos.x = x; bestNeighbourPos.y = y; } }); if (bestNeighbourDistance.value != Double.MAX_VALUE) { return bestNeighbourPos.createShortPoint2D(); } else { return null; } }
@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(); }
@Test public void testGetOnGridDist() { ShortPoint2D center = new ShortPoint2D(100, 100); for (int radius = 1; radius < 30; radius++) { int expectedRadius = radius; HexGridArea.streamBorder(center.x,center.y, radius).forEach((x,y)->{ assertEquals(expectedRadius, center.getOnGridDistTo(x,y)); }); } }
@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); } }); }