mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-12 08:18:35 +00:00
Update Upstream
ed28089 Don't crash if fields are null in ChunkDeleter (1874) f049d56 Revert "Use a Guava Cache instead of a ThreadLocal (1859)" c5a4450 Internally use a negated mask class to prevent russian doll wrapping (1877) 1397ec7 Add Snow Smooth Tools (1580) Fixes #955 Fixes #858
This commit is contained in:
@ -180,7 +180,7 @@ public class HeightMap {
|
||||
System.arraycopy(data, 0, newData, 0, data.length);
|
||||
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
newData = filter.filter(newData, width, height);
|
||||
newData = filter.filter(newData, width, height, 0.5F);
|
||||
}
|
||||
|
||||
//FAWE start - check layers
|
||||
@ -229,7 +229,7 @@ public class HeightMap {
|
||||
BlockStateHolder<BlockState> existing = session.getBlock(xr, curBlock, zr);
|
||||
|
||||
// Skip water/lava
|
||||
if (existing.getBlockType().getMaterial().isMovementBlocker()) {
|
||||
if (!existing.getBlockType().getMaterial().isLiquid()) {
|
||||
// Grow -- start from 1 below top replacing airblocks
|
||||
for (int setY = newBlock - 1, getY = curBlock; setY >= curBlock; --setY, getY--) {
|
||||
BlockStateHolder<BlockState> get = session.getBlock(xr, getY, zr);
|
||||
|
@ -79,50 +79,107 @@ public class HeightMapFilter {
|
||||
* @return the modified height map
|
||||
*/
|
||||
public int[] filter(int[] inData, int width, int height) {
|
||||
return filter(inData, width, height, 0.5F);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter with a 2D kernel.
|
||||
*
|
||||
* @param inData the data
|
||||
* @param width the width
|
||||
* @param height the height
|
||||
* @param offset the offset added to the height
|
||||
* @return the modified height map
|
||||
*/
|
||||
public int[] filter(int[] inData, int width, int height, float offset) {
|
||||
checkNotNull(inData);
|
||||
|
||||
float[] inDataFloat = new float[inData.length];
|
||||
for (int i = 0; i < inData.length; i++) {
|
||||
inDataFloat[i] = inData[i];
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
float[] matrix = kernel.getKernelData(null);
|
||||
int[] outData = new int[inData.length];
|
||||
|
||||
int kh = kernel.getHeight();
|
||||
int kw = kernel.getWidth();
|
||||
int kox = kernel.getXOrigin();
|
||||
int koy = kernel.getYOrigin();
|
||||
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
float z = 0;
|
||||
|
||||
for (int ky = 0; ky < kh; ++ky) {
|
||||
int offsetY = y + ky - koy;
|
||||
// Clamp coordinates inside data
|
||||
if (offsetY < 0 || offsetY >= height) {
|
||||
offsetY = y;
|
||||
}
|
||||
|
||||
offsetY *= width;
|
||||
|
||||
int matrixOffset = ky * kw;
|
||||
for (int kx = 0; kx < kw; ++kx) {
|
||||
float f = matrix[matrixOffset + kx];
|
||||
if (f == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int offsetX = x + kx - kox;
|
||||
// Clamp coordinates inside data
|
||||
if (offsetX < 0 || offsetX >= width) {
|
||||
offsetX = x;
|
||||
}
|
||||
|
||||
z += f * inData[offsetY + offsetX];
|
||||
}
|
||||
}
|
||||
outData[index++] = (int) (z + 0.5);
|
||||
outData[index++] = (int) calculateHeight(inDataFloat, width, height, offset, matrix, x, y);
|
||||
}
|
||||
}
|
||||
return outData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter with a 2D kernel for float values.
|
||||
*
|
||||
* @param inData the data
|
||||
* @param width the width
|
||||
* @param height the height
|
||||
* @return the modified height map
|
||||
*/
|
||||
public float[] filter(float[] inData, int width, int height, float offset) {
|
||||
checkNotNull(inData);
|
||||
|
||||
int index = 0;
|
||||
float[] matrix = kernel.getKernelData(null);
|
||||
float[] outData = new float[inData.length];
|
||||
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
outData[index++] = calculateHeight(inData, width, height, offset, matrix, x, y);
|
||||
}
|
||||
}
|
||||
return outData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the height based on the existing data and the kernel data.
|
||||
*
|
||||
* @param inData the existing height data
|
||||
* @param width the width
|
||||
* @param height the height
|
||||
* @param offset the offset to add to the height
|
||||
* @param matrix the matrix with the kernel's data
|
||||
* @param x the current x coordinate
|
||||
* @param y the current y coordinate
|
||||
* @return the calculated height for that block
|
||||
*/
|
||||
private float calculateHeight(float[] inData, int width, int height, float offset, float[] matrix, int x, int y) {
|
||||
int kh = kernel.getHeight();
|
||||
int kw = kernel.getWidth();
|
||||
int kox = kernel.getXOrigin();
|
||||
int koy = kernel.getYOrigin();
|
||||
|
||||
float z = 0;
|
||||
|
||||
for (int ky = 0; ky < kh; ++ky) {
|
||||
int offsetY = y + ky - koy;
|
||||
// Clamp coordinates inside data
|
||||
if (offsetY < 0 || offsetY >= height) {
|
||||
offsetY = y;
|
||||
}
|
||||
|
||||
offsetY *= width;
|
||||
|
||||
int matrixOffset = ky * kw;
|
||||
for (int kx = 0; kx < kw; ++kx) {
|
||||
float f = matrix[matrixOffset + kx];
|
||||
if (f == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int offsetX = x + kx - kox;
|
||||
// Clamp coordinates inside data
|
||||
if (offsetX < 0 || offsetX >= width) {
|
||||
offsetX = x;
|
||||
}
|
||||
|
||||
z += f * inData[offsetY + offsetX];
|
||||
}
|
||||
}
|
||||
return z + offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,218 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.math.convolution;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.MaxChangedBlocksException;
|
||||
import com.sk89q.worldedit.function.mask.Mask;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.registry.state.Property;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockTypes;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* Allows applications of Kernels onto the region's height map for snow layers
|
||||
*
|
||||
* <p>Currently only used for snow layer smoothing (with a GaussianKernel)</p>.
|
||||
*/
|
||||
public class SnowHeightMap {
|
||||
|
||||
private final float[] data;
|
||||
private final int width;
|
||||
private final int height;
|
||||
|
||||
private final Region region;
|
||||
private final EditSession session;
|
||||
|
||||
private final Property<Integer> layers;
|
||||
|
||||
/**
|
||||
* Constructs the SnowHeightMap.
|
||||
*
|
||||
* @param session an edit session
|
||||
* @param region the region
|
||||
* @param mask optional mask for the height map
|
||||
*/
|
||||
public SnowHeightMap(EditSession session, Region region, @Nullable Mask mask) {
|
||||
checkNotNull(session);
|
||||
checkNotNull(region);
|
||||
|
||||
this.session = session;
|
||||
this.region = region;
|
||||
|
||||
this.width = region.getWidth();
|
||||
this.height = region.getLength();
|
||||
|
||||
this.layers = BlockTypes.SNOW.getProperty("layers");
|
||||
|
||||
int minX = region.getMinimumPoint().getBlockX();
|
||||
int minY = region.getMinimumPoint().getBlockY();
|
||||
int minZ = region.getMinimumPoint().getBlockZ();
|
||||
int maxY = region.getMaximumPoint().getBlockY();
|
||||
|
||||
// Store current heightmap data
|
||||
data = new float[width * height];
|
||||
for (int z = 0; z < height; ++z) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
int highestBlockY = session.getHighestTerrainBlock(x + minX, z + minZ, minY, maxY, mask);
|
||||
BlockState upper = session.getBlock(BlockVector3.at(x + minX, highestBlockY + 1, z + minZ));
|
||||
if (upper.getBlockType() == BlockTypes.SNOW) {
|
||||
Integer amountLayers = upper.getState(layers);
|
||||
data[z * width + x] = (highestBlockY + 1 + (((float) amountLayers - 1) / 8));
|
||||
} else {
|
||||
BlockState block = session.getBlock(BlockVector3.at(x + minX, highestBlockY, z + minZ));
|
||||
if (block.getBlockType().getMaterial().isAir()) {
|
||||
data[z * width + x] = highestBlockY;
|
||||
} else {
|
||||
data[z * width + x] = highestBlockY + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the new heightmap with the filter 'iterations' amount times.
|
||||
*
|
||||
* @param filter the filter
|
||||
* @param iterations the number of iterations
|
||||
* @return new generated heightmap of the terrain
|
||||
*/
|
||||
public float[] applyFilter(HeightMapFilter filter, int iterations) {
|
||||
checkNotNull(filter);
|
||||
|
||||
float[] newData = data.clone();
|
||||
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
// add an offset from 0.0625F to the values (snowlayer half)
|
||||
newData = filter.filter(newData, width, height, 0.0625F);
|
||||
}
|
||||
return newData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a raw heightmap to a region. Use snow layers.
|
||||
*
|
||||
* @param data the data
|
||||
* @param layerBlocks amount of blocks with type SNOW_BLOCK
|
||||
* @return number of blocks affected
|
||||
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
|
||||
*/
|
||||
public int applyChanges(float[] data, int layerBlocks) throws MaxChangedBlocksException {
|
||||
checkNotNull(data);
|
||||
|
||||
BlockVector3 minY = region.getMinimumPoint();
|
||||
int originX = minY.getBlockX();
|
||||
int originY = minY.getBlockY();
|
||||
int originZ = minY.getBlockZ();
|
||||
|
||||
int maxY = region.getMaximumPoint().getBlockY();
|
||||
|
||||
BlockState fillerAir = BlockTypes.AIR.getDefaultState();
|
||||
BlockState fillerSnow = BlockTypes.SNOW_BLOCK.getDefaultState();
|
||||
|
||||
int blocksChanged = 0;
|
||||
|
||||
// Apply heightmap
|
||||
for (int z = 0; z < height; ++z) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
int index = z * width + x;
|
||||
float curHeight = this.data[index];
|
||||
|
||||
if (curHeight == originY) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Clamp newHeight within the selection area
|
||||
float newHeight = Math.min(maxY, data[index]);
|
||||
|
||||
// Offset x,z to be 'real' coordinates
|
||||
int xr = x + originX;
|
||||
int zr = z + originZ;
|
||||
|
||||
// We are keeping the topmost blocks so take that in account for the scale
|
||||
double scale = (double) (curHeight - originY) / (double) (newHeight - originY);
|
||||
|
||||
// Depending on growing or shrinking we need to start at the bottom or top
|
||||
if (newHeight >= curHeight) {
|
||||
// Set the top block of the column to be the same type (this might go wrong with rounding)
|
||||
BlockState existing = session.getBlock(BlockVector3.at(xr, curHeight, zr));
|
||||
|
||||
// Skip water/lava
|
||||
if (!existing.getBlockType().getMaterial().isLiquid()) {
|
||||
setSnowLayer(xr, zr, newHeight);
|
||||
++blocksChanged;
|
||||
|
||||
// Grow -- start from 1 below top replacing airblocks
|
||||
for (int y = (int) newHeight - 1 - originY; y >= 0; --y) {
|
||||
if (y >= newHeight - 1 - originY - layerBlocks) {
|
||||
session.setBlock(BlockVector3.at(xr, originY + y, zr), fillerSnow);
|
||||
} else {
|
||||
int copyFrom = (int) (y * scale);
|
||||
BlockState block = session.getBlock(BlockVector3.at(xr, originY + copyFrom, zr));
|
||||
session.setBlock(BlockVector3.at(xr, originY + y, zr), block);
|
||||
}
|
||||
++blocksChanged;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Shrink -- start from bottom
|
||||
for (int y = 0; y < (int) newHeight - originY; ++y) {
|
||||
if (y >= (int) newHeight - originY - layerBlocks) {
|
||||
session.setBlock(BlockVector3.at(xr, originY + y, zr), fillerSnow);
|
||||
} else {
|
||||
int copyFrom = (int) (y * scale);
|
||||
BlockState block = session.getBlock(BlockVector3.at(xr, originY + copyFrom, zr));
|
||||
session.setBlock(BlockVector3.at(xr, originY + y, zr), block);
|
||||
}
|
||||
++blocksChanged;
|
||||
}
|
||||
|
||||
setSnowLayer(xr, zr, newHeight);
|
||||
++blocksChanged;
|
||||
|
||||
// Fill rest with air
|
||||
for (int y = (int) newHeight + 1; y <= curHeight; ++y) {
|
||||
session.setBlock(BlockVector3.at(xr, y, zr), fillerAir);
|
||||
++blocksChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Drop trees to the floor -- TODO
|
||||
return blocksChanged;
|
||||
}
|
||||
|
||||
private void setSnowLayer(int x, int z, float newHeight) throws MaxChangedBlocksException {
|
||||
int numOfLayers = (int) ((newHeight % 1) * 8) + 1;
|
||||
session.setBlock(
|
||||
BlockVector3.at(x, (int) newHeight, z),
|
||||
BlockTypes.SNOW.getState(ImmutableMap.of(this.layers, numOfLayers))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user