2011-01-01 02:40:07 +01:00
|
|
|
package com.sk89q.worldedit;
|
2010-11-21 22:58:05 +01:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEditLibrary
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
|
|
|
import com.sk89q.worldedit.filters.HeightMapFilter;
|
|
|
|
import com.sk89q.worldedit.regions.Region;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows applications of Kernels onto the region's heightmap.
|
|
|
|
* Currently only used for smoothing (with a GaussianKernel).
|
|
|
|
*
|
|
|
|
* @author Grum
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class HeightMap {
|
|
|
|
private int[] data;
|
|
|
|
private int width;
|
|
|
|
private int height;
|
|
|
|
|
|
|
|
private Region region;
|
|
|
|
private EditSession session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the HeightMap
|
|
|
|
*
|
|
|
|
* @param session
|
|
|
|
* @param region
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HeightMap(EditSession session, Region region) {
|
|
|
|
this.session = session;
|
|
|
|
this.region = region;
|
|
|
|
|
|
|
|
this.width = region.getWidth();
|
|
|
|
this.height = region.getLength();
|
|
|
|
|
|
|
|
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 int[width * height];
|
|
|
|
for (int z = 0; z < height; z++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
data[z * width + x] = session.getHighestTerrainBlock(x + minX, z + minZ, minY, maxY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the filter 'iterations' amount times.
|
|
|
|
*
|
|
|
|
* @param filter
|
|
|
|
* @param iterations
|
|
|
|
* @return number of blocks affected
|
|
|
|
* @throws MaxChangedBlocksException
|
|
|
|
*/
|
|
|
|
|
|
|
|
public int applyFilter(HeightMapFilter filter, int iterations) throws MaxChangedBlocksException {
|
|
|
|
int[] newData = new int[data.length];
|
|
|
|
System.arraycopy(data, 0, newData, 0, data.length);
|
|
|
|
|
|
|
|
for (int i = 0; i < iterations; i++)
|
|
|
|
newData = filter.filter(newData, width, height);
|
|
|
|
|
|
|
|
return apply(newData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply a raw heightmap to the region
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @return number of blocks affected
|
|
|
|
* @throws MaxChangedBlocksException
|
|
|
|
*/
|
|
|
|
|
|
|
|
public int apply(int[] data) throws MaxChangedBlocksException {
|
|
|
|
Vector minY = region.getMinimumPoint();
|
|
|
|
int originX = minY.getBlockX();
|
|
|
|
int originY = minY.getBlockY();
|
|
|
|
int originZ = minY.getBlockZ();
|
|
|
|
|
|
|
|
int maxY = region.getMaximumPoint().getBlockY();
|
|
|
|
BaseBlock fillerAir = new BaseBlock(0);
|
|
|
|
|
|
|
|
int blocksChanged = 0;
|
|
|
|
|
|
|
|
// Apply heightmap
|
|
|
|
for (int z = 0; z < height; z++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
int index = z * width + x;
|
|
|
|
int curHeight = this.data[index];
|
|
|
|
|
|
|
|
// Clamp newHeight within the selection area
|
|
|
|
int newHeight = Math.min(maxY, data[index]);
|
|
|
|
|
|
|
|
// Offset x,z to be 'real' coordinates
|
|
|
|
int X = x + originX;
|
|
|
|
int Z = z + originZ;
|
|
|
|
|
2010-11-21 23:02:41 +01:00
|
|
|
// We are keeping the topmost blocks so take that in account for the scale
|
2010-11-21 22:58:05 +01:00
|
|
|
double scale = (double) (curHeight - originY) / (double) (newHeight - originY);
|
|
|
|
|
2010-11-21 23:02:41 +01:00
|
|
|
// Depending on growing or shrinking we need to start at the bottom or top
|
2010-11-21 22:58:05 +01:00
|
|
|
if (newHeight > curHeight) {
|
2010-11-21 23:02:41 +01:00
|
|
|
// Set the top block of the column to be the same type (this might go wrong with rounding)
|
2010-11-27 07:46:44 +01:00
|
|
|
BaseBlock existing = session.getBlock(new Vector(X, curHeight, Z));
|
2010-11-21 22:58:05 +01:00
|
|
|
|
2010-11-27 07:46:44 +01:00
|
|
|
// Skip water/lava
|
|
|
|
if (existing.getID() < 8 || existing.getID() > 11) {
|
|
|
|
session.setBlock(new Vector(X, newHeight, Z), existing);
|
2010-11-21 22:58:05 +01:00
|
|
|
blocksChanged++;
|
2010-11-27 07:46:44 +01:00
|
|
|
|
|
|
|
// Grow -- start from 1 below top replacing airblocks
|
|
|
|
for (int y = newHeight - 1 - originY; y >= 0; y--) {
|
|
|
|
int copyFrom = (int) (y * scale);
|
|
|
|
session.setBlock(new Vector(X, originY + y, Z), session.getBlock(new Vector(X, originY + copyFrom, Z)));
|
|
|
|
blocksChanged++;
|
|
|
|
}
|
2010-11-21 22:58:05 +01:00
|
|
|
}
|
|
|
|
} else if (curHeight > newHeight) {
|
|
|
|
// Shrink -- start from bottom
|
|
|
|
for (int y = 0; y < newHeight - originY; y++) {
|
|
|
|
int copyFrom = (int) (y * scale);
|
|
|
|
session.setBlock(new Vector(X, originY + y, Z), session.getBlock(new Vector(X, originY + copyFrom, Z)));
|
|
|
|
blocksChanged++;
|
|
|
|
}
|
|
|
|
|
2010-11-21 23:02:41 +01:00
|
|
|
// Set the top block of the column to be the same type
|
|
|
|
// (this could otherwise go wrong with rounding)
|
2010-11-21 22:58:05 +01:00
|
|
|
session.setBlock(new Vector(X, newHeight, Z), session.getBlock(new Vector(X, curHeight, Z)));
|
|
|
|
blocksChanged++;
|
|
|
|
|
|
|
|
// Fill rest with air
|
|
|
|
for (int y = newHeight + 1; y <= curHeight; y++) {
|
|
|
|
session.setBlock(new Vector(X, y, Z), fillerAir);
|
|
|
|
blocksChanged++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop trees to the floor -- TODO
|
|
|
|
|
|
|
|
return blocksChanged;
|
|
|
|
}
|
|
|
|
}
|