2013-11-17 21:36:18 +00:00
|
|
|
#include "TerrainDefinition.h"
|
|
|
|
|
|
|
|
#include "TerrainHeightMap.h"
|
|
|
|
#include "NoiseGenerator.h"
|
|
|
|
#include "PackStream.h"
|
2015-08-13 21:46:50 +00:00
|
|
|
#include "FloatNode.h"
|
2013-11-17 21:36:18 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
TerrainDefinition::TerrainDefinition(DefinitionNode *parent) : DefinitionNode(parent, "terrain", "terrain") {
|
2013-11-17 21:36:18 +00:00
|
|
|
height = 1.0;
|
|
|
|
shadow_smoothing = 0.0;
|
|
|
|
|
|
|
|
height_map = new TerrainHeightMap(this);
|
2015-09-20 23:36:03 +00:00
|
|
|
has_painting = false;
|
2013-11-17 21:36:18 +00:00
|
|
|
addChild(height_map);
|
|
|
|
|
2015-08-20 21:11:07 +00:00
|
|
|
water_height = new FloatNode(this, "water_height");
|
2013-11-17 21:36:18 +00:00
|
|
|
|
|
|
|
_height_noise = new NoiseGenerator;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
TerrainDefinition::~TerrainDefinition() {
|
2013-11-17 21:36:18 +00:00
|
|
|
delete _height_noise;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void TerrainDefinition::validate() {
|
2013-11-17 21:36:18 +00:00
|
|
|
_height_noise->validate();
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (height < 1.0) {
|
2013-11-17 21:36:18 +00:00
|
|
|
height = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get minimal and maximal height */
|
|
|
|
_height_noise->getRange(&_min_height, &_max_height);
|
2015-09-10 16:16:57 +00:00
|
|
|
_min_height *= height;
|
|
|
|
_max_height *= height;
|
2013-11-17 21:36:18 +00:00
|
|
|
|
2015-09-20 23:36:03 +00:00
|
|
|
/* TODO Alter limits with heightmap min/max */
|
|
|
|
has_painting = height_map->hasPainting();
|
2013-11-17 21:36:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void TerrainDefinition::copy(DefinitionNode *_destination) const {
|
|
|
|
TerrainDefinition *destination = (TerrainDefinition *)_destination;
|
2013-11-17 21:36:18 +00:00
|
|
|
|
|
|
|
destination->height = height;
|
|
|
|
destination->shadow_smoothing = shadow_smoothing;
|
|
|
|
|
|
|
|
height_map->copy(destination->height_map);
|
|
|
|
|
|
|
|
_height_noise->copy(destination->_height_noise);
|
|
|
|
|
|
|
|
destination->validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void TerrainDefinition::save(PackStream *stream) const {
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode::save(stream);
|
2013-11-17 21:36:18 +00:00
|
|
|
|
|
|
|
stream->write(&height);
|
|
|
|
stream->write(&shadow_smoothing);
|
|
|
|
_height_noise->save(stream);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void TerrainDefinition::load(PackStream *stream) {
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode::load(stream);
|
2013-11-17 21:36:18 +00:00
|
|
|
|
|
|
|
stream->read(&height);
|
|
|
|
stream->read(&shadow_smoothing);
|
|
|
|
_height_noise->load(stream);
|
|
|
|
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
double TerrainDefinition::getGridHeight(int x, int z, bool with_painting) {
|
2013-11-17 21:36:18 +00:00
|
|
|
double h;
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (!with_painting || !has_painting || !height_map->getGridValue(x, z, &h)) {
|
2013-11-17 21:36:18 +00:00
|
|
|
h = _height_noise->get2DTotal((double)x, (double)z);
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
double TerrainDefinition::getInterpolatedHeight(double x, double z, bool scaled, bool with_painting,
|
|
|
|
bool water_offset) {
|
2013-11-17 21:36:18 +00:00
|
|
|
double h;
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (!with_painting || !has_painting || !height_map->getInterpolatedValue(x, z, &h)) {
|
2013-11-17 21:36:18 +00:00
|
|
|
h = _height_noise->get2DTotal(x, z);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (scaled) {
|
2015-09-10 16:16:57 +00:00
|
|
|
return (water_offset ? (h - water_height->getValue()) : h) * height;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2013-11-17 21:36:18 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
double TerrainDefinition::getWaterOffset() const {
|
2015-09-10 16:16:57 +00:00
|
|
|
return -water_height->getValue() * height;
|
2015-08-17 20:55:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
HeightInfo TerrainDefinition::getHeightInfo() {
|
2013-11-17 21:36:18 +00:00
|
|
|
HeightInfo result;
|
|
|
|
|
|
|
|
result.min_height = _min_height;
|
|
|
|
result.max_height = _max_height;
|
2015-08-19 16:14:15 +00:00
|
|
|
result.base_height = -getWaterOffset();
|
2013-11-17 21:36:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
unsigned long TerrainDefinition::getMemoryStats() {
|
2013-11-17 21:36:18 +00:00
|
|
|
return height_map->getMemoryStats();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void TerrainDefinition::applyPreset(TerrainPreset preset) {
|
2013-11-17 21:36:18 +00:00
|
|
|
int resolution = 8;
|
2015-11-09 21:30:46 +00:00
|
|
|
switch (preset) {
|
2013-11-17 21:36:18 +00:00
|
|
|
case TERRAIN_PRESET_STANDARD:
|
|
|
|
_height_noise->randomizeOffsets();
|
|
|
|
_height_noise->clearLevels();
|
|
|
|
_height_noise->addLevelSimple(pow(2.0, resolution + 1), -1.0, 1.0);
|
|
|
|
_height_noise->addLevelsSimple(resolution - 2, pow(2.0, resolution - 1), -0.7, 0.7, 0.5);
|
|
|
|
_height_noise->normalizeAmplitude(-1.0, 1.0, 0);
|
2013-12-31 14:50:28 +00:00
|
|
|
_height_noise->setFunctionParams(NoiseGenerator::NOISE_FUNCTION_SIMPLEX, 0.0, 0.0);
|
2013-11-17 21:36:18 +00:00
|
|
|
height = 30.0;
|
|
|
|
shadow_smoothing = 0.03;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:11:07 +00:00
|
|
|
water_height->setValue(-0.3);
|
|
|
|
|
2013-11-17 21:36:18 +00:00
|
|
|
height_map->clearPainting();
|
|
|
|
validate();
|
|
|
|
}
|