/*
Minetest-c55
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
(c) 2010 Perttu Ahola <celeron55@gmail.com>
*/
#include "heightmap.h"
// For MAP_BLOCKSIZE
#include "mapblock.h"
/*
ValueGenerator
*/
ValueGenerator* ValueGenerator::deSerialize(std::string line)
{
std::istringstream ss(line);
//ss.imbue(std::locale("C"));
std::string name;
std::getline(ss, name, ' ');
if(name == "constant")
{
f32 value;
ss>>value;
return new ConstantGenerator(value);
}
else if(name == "linear")
{
f32 height;
v2f slope;
ss>>height;
ss>>slope.X;
ss>>slope.Y;
return new LinearGenerator(height, slope);
}
else if(name == "power")
{
f32 height;
v2f slope;
f32 power;
ss>>height;
ss>>slope.X;
ss>>slope.Y;
ss>>power;
return new PowerGenerator(height, slope, power);
}
else
{
throw SerializationError
("Invalid heightmap generator (deSerialize)");
}
}
/*
FixedHeightmap
*/
f32 FixedHeightmap::avgNeighbours(v2s16 p, s16 d)
{
v2s16 dirs[4] = {
v2s16(1,0),
v2s16(0,1),
v2s16(-1,0),
v2s16(0,-1)
};
f32 sum = 0.0;
f32 count = 0.0;
for(u16 i=0; i<4; i++){
v2s16 p2 = p + dirs[i] * d;
f32 n = getGroundHeightParent(p2);
if(n < GROUNDHEIGHT_VALID_MINVALUE)
continue;
sum += n;
count += 1.0;
}
assert(count > 0.001);
return sum / count;
}
f32 FixedHeightmap::avgDiagNeighbours(v2s16 p, s16 d)
{
v2s16 dirs[4] = {
v2s16(1,1),
v2s16(-1,-1),
v2s16(-1,1),
v2s16(1,-1)
};
f32 sum = 0.0;
f32 count = 0.0;
for(u16 i=0; i<4; i++){
v2s16 p2 = p + dirs[i] * d;
f32 n = getGroundHeightParent(p2);
if(n < GROUNDHEIGHT_VALID_MINVALUE)
continue;
sum += n;
count += 1.0;
}
assert(count > 0.001);
return sum / count;
}
/*
Adds a point to transform into a diamond pattern
center = Center of the diamond phase (center of a square)
a = Side length of the existing square (2, 4, 8, ...)
Adds the center points of the next squares to next_squares as
dummy "true" values.
*/
void FixedHeightmap::makeDiamond(
v2s16 center,
s16 a,
f32 randmax,
core::map<v2s16, bool> &next_squares)
{
/*dstream<<"makeDiamond(): center="
<<"("<<center.X<<","<<center.Y<<")"
<<", a="<<a<<", randmax="<<randmax
<<", next_squares.size()="<<next_squares.size()
<<std::endl;*/
f32 n = avgDiagNeighbours(center, a/2);
// Add (-1.0...1.0) * randmax
n += ((float)myrand() / (float)(MYRAND_MAX/2) - 1.0)*randmax;
bool worked = setGroundHeightParent(center, n);
if(a >= 2 && worked)
{
next_squares[center + a/2*v2s16(-1,0)] = true;
next_squares[center + a/2*v2s16(1,0)] = true;
next_squares[center + a/2*v2s16(0,-1)] = true;
next_squares[center + a/2*v2s16(0,1)] = true;
}
}
/*
Adds a point to transform into a square pattern
center = The point that is added. The center of a diamond.
a = Diameter of the existing diamond. (2, 4, 8, 16, ...)
Adds center points of the next diamonds to next_diamonds.
*/
void FixedHeightmap::makeSquare(
v2s16 center,
s16 a,
f32 randmax,
core::map<v2s16, bool> &next_diamonds)
{
/*dstream<<"makeSquare(): center="
<<"("<<center.X<<","<<center.Y<<")"
<<", a="<<a<<", randmax="<<randmax
<<", next_diamonds.size()="<<next_diamonds.size()
<<std::endl;*/
f32 n = avgNeighbours(center, a/2);
// Add (-1.0...1.0) * randmax
n += ((float)myrand() / (float)(MYRAND_MAX/2) - 1.0)*randmax;
bool worked = setGroundHeightParent(center, n);
if(a >= 4 && worked)
{
next_diamonds[center + a/4*v2s16(1,1)] = true;
next_diamonds[center + a/4*v2s16(-1,1)] = true;
next_diamonds[center + a/4*v2s16(-1,-1)] = true;
next_diamonds[center + a/4*v2s16(1,-1)] = true;
}
}
|