/*
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"
/*
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] = {
|