protected FreeMapArea getBuildingArea() { return new FreeMapArea(this.pos, type.getBlockedTiles()); }
private FreeMapArea getGroundArea() { return new FreeMapArea(super.pos, getBuildingType().getProtectedTiles()); }
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; } }
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)); } } } }
@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))); } } } }
@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 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); } } }
FreeMapArea mapArea = new FreeMapArea(area); for (ShortPoint2D curr : c1) { if (!c2.contains(curr)) {