@Override public final boolean contains(ShortPoint2D pos) { return contains(pos.x, pos.y); }
protected FreeMapArea getBuildingArea() { return new FreeMapArea(this.pos, type.getBlockedTiles()); }
/** * Creates a free map area by converting the relative points to absolute ones. * * @param pos * The origin for the relative points * @param relativePoints * The relative points */ public FreeMapArea(ShortPoint2D pos, RelativePoint[] relativePoints) { this(convertRelative(pos, relativePoints)); }
/** * @param positions * the positions this map area will contain. */ public FreeMapArea(List<ShortPoint2D> positions) { assert positions.size() > 0 : "positions must contain at least one value!!"; this.positions = positions; SRectangle bounds = getBounds(positions); this.xOffset = bounds.xMin; this.yOffset = bounds.yMin; this.width = bounds.getWidth() + 1; this.height = bounds.getHeight() + 1; this.areaMap = new boolean[width][height]; this.upperLeftPosition = setPositionsToMap(areaMap, positions); }
@Test public void test() { List<ShortPoint2D> positions = new LinkedList<>(); positions.add(new ShortPoint2D(1, 1)); positions.add(new ShortPoint2D(2, 2)); positions.add(new ShortPoint2D(3, 3)); positions.add(new ShortPoint2D(2, 1)); positions.add(new ShortPoint2D(1, 2)); positions.add(new ShortPoint2D(1, 3)); positions.add(new ShortPoint2D(3, 1)); FreeMapArea area = new FreeMapArea(positions); boolean[][] expected = { { false, false, false, false, false }, { false, true, true, true, false }, { false, true, true, false, false }, { false, true, false, true, false }, { false, false, false, false, false } }; for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { assertEquals(expected[y][x], area.contains(new ShortPoint2D(x, y))); } } } }
private void placeReusableMaterials() { int posIdx = 0; FreeMapArea buildingArea = new FreeMapArea(this.pos, type.getBlockedTiles()); if (isConstructionFinished()) { for (ConstructionStack curr : type.getConstructionStacks()) { byte paybackAmount = (byte) (curr.requiredForBuild() * Constants.BUILDINGS_DESTRUCTION_MATERIALS_PAYBACK_FACTOR); while (paybackAmount > 0) { byte paybackForStack = (byte) Math.min(Constants.STACK_SIZE, paybackAmount); ShortPoint2D position = buildingArea.get(posIdx); grid.pushMaterialsTo(position, curr.getMaterialType(), paybackForStack); paybackAmount -= paybackForStack; posIdx += 4; } } } else { for (IRequestStack stack : stacks) { posIdx += 4; int paybackAmount = (int) (stack.getNumberOfPopped() * Constants.BUILDINGS_DESTRUCTION_MATERIALS_PAYBACK_FACTOR); if (paybackAmount > 0) { ShortPoint2D position = buildingArea.get(posIdx); grid.pushMaterialsTo(position, stack.getMaterialType(), (byte) Math.min(paybackAmount, Constants.STACK_SIZE)); } } } }
@Override public final void removeBuildingAt(ShortPoint2D pos) { IBuilding building = (IBuilding) objectsGrid.getMapObjectAt(pos.x, pos.y, EMapObjectType.BUILDING); mapObjectsManager.removeMapObjectType(pos.x, pos.y, EMapObjectType.BUILDING); FreeMapArea area = new FreeMapArea(pos, building.getBuildingType().getProtectedTiles()); objectsGrid.setBuildingArea(area, null); area.stream().filterBounds(width, height).forEach((x, y) -> { StackMapObject stack = (StackMapObject) objectsGrid.getMapObjectAt(x, y, EMapObjectType.STACK_OBJECT); flagsGrid.setBlockedAndProtected(x, y, false, stack != null); // if there is a stack, the position must stay protected }); }
@Override protected final void killedEvent() { setSelected(false); if (occupiedArea) { freeArea(); int idx = 0; FreeMapArea buildingArea = super.getBuildingArea(); for (TowerOccupier curr : sortedOccupiers) { addInformableMapObject(curr, false);// if curr is a bowman, this removes the informable map object. curr.getSoldier().leaveOccupyableBuilding(buildingArea.get(idx)); idx++; } sortedOccupiers.clear(); } if (attackableTowerObject != null && attackableTowerObject.currDefender != null) { attackableTowerObject.currDefender.soldier.leaveOccupyableBuilding(attackableTowerObject.getPosition()); } setAttackableTowerObject(false); }
FreeMapArea mapArea = new FreeMapArea(area); for (ShortPoint2D curr : c1) { if (!c2.contains(curr)) { assertTrue(mapArea.contains(curr));
/** * Sets the positions to the map and returns the upper left position * * @param areaMap * @param positions * @return */ private final ShortPoint2D setPositionsToMap(boolean[][] areaMap, List<ShortPoint2D> positions) { if (positions.isEmpty()) { return null; } ShortPoint2D upperLeft = positions.get(0); for (ShortPoint2D curr : positions) { areaMap[getMapX(curr)][getMapY(curr)] = true; if (curr.y < upperLeft.y || curr.y == upperLeft.y && curr.x < upperLeft.x) { upperLeft = curr; } } return upperLeft; }
public void removeArea(FreeMapArea area) { Iterator<ShortPoint2D> iter = positions.iterator(); while (iter.hasNext()) { if (area.contains(iter.next())) { iter.remove(); } } }
private FreeMapArea getGroundArea() { return new FreeMapArea(super.pos, getBuildingType().getProtectedTiles()); }
public CoordinateStream getAreaWithoutGround() { return area.stream().filter((x, y) -> !groundArea.contains(x, y)); } }
private FreeMapArea getGroundArea(ShortPoint2D pos) { return new FreeMapArea(pos, EBuildingType.TOWER.getProtectedTiles()); }
@Override public final boolean setBuilding(ShortPoint2D position, Building newBuilding) { if (MainGrid.this.isInBounds(position.x, position.y)) { FreeMapArea protectedArea = new FreeMapArea(position, newBuilding.getBuildingType().getProtectedTiles()); if (canConstructAt(protectedArea)) { setProtectedState(protectedArea, true); mapObjectsManager.addBuildingTo(position, newBuilding); objectsGrid.setBuildingArea(protectedArea, newBuilding); return true; } else { return false; } } else { return false; } }
@Override protected void placeAdditionalMapObjects(IBuildingsGrid grid, ShortPoint2D pos, boolean place) { if (place) { MapObjectsManager objectsManager = grid.getMapObjectsManager(); for (ShortPoint2D currPos : new FreeMapArea(pos, super.getBuildingType().getProtectedTiles())) { objectsManager.removeMapObjectType(currPos.x, currPos.y, EMapObjectType.FOUND_COAL); objectsManager.removeMapObjectType(currPos.x, currPos.y, EMapObjectType.FOUND_GOLD); objectsManager.removeMapObjectType(currPos.x, currPos.y, EMapObjectType.FOUND_IRON); objectsManager.removeMapObjectType(currPos.x, currPos.y, EMapObjectType.FOUND_BRIMSTONE); objectsManager.removeMapObjectType(currPos.x, currPos.y, EMapObjectType.FOUND_GEMSTONE); objectsManager.removeMapObjectType(currPos.x, currPos.y, EMapObjectType.FOUND_NOTHING); } } }