58 changes: 29 additions & 29 deletions src/noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ float contour(float v)
///////////////////////// [ New perlin stuff ] ////////////////////////////


Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz)
Noise::Noise(NoiseParams *np_, int seed, int sx, int sy, int sz)
{
this->np = np;
memcpy(&np, np_, sizeof(np));
this->seed = seed;
this->sx = sx;
this->sy = sy;
Expand All @@ -329,10 +329,10 @@ Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz)
this->gradient_buf = NULL;
this->result = NULL;

if (np->flags & NOISE_FLAG_DEFAULTS) {
if (np.flags & NOISE_FLAG_DEFAULTS) {
// By default, only 2d noise is eased.
if (sz <= 1)
np->flags |= NOISE_FLAG_EASED;
np.flags |= NOISE_FLAG_EASED;
}

allocBuffers();
Expand Down Expand Up @@ -380,15 +380,15 @@ void Noise::setSize(int sx, int sy, int sz)

void Noise::setSpreadFactor(v3f spread)
{
this->np->spread = spread;
this->np.spread = spread;

resizeNoiseBuf(sz > 1);
}


void Noise::setOctaves(int octaves)
{
this->np->octaves = octaves;
this->np.octaves = octaves;

resizeNoiseBuf(sz > 1);
}
Expand All @@ -400,15 +400,15 @@ void Noise::resizeNoiseBuf(bool is3d)
float ofactor;

//maximum possible spread value factor
ofactor = pow(np->lacunarity, np->octaves - 1);
ofactor = pow(np.lacunarity, np.octaves - 1);

//noise lattice point count
//(int)(sz * spread * ofactor) is # of lattice points crossed due to length
// + 2 for the two initial endpoints
// + 1 for potentially crossing a boundary due to offset
nlx = (int)ceil(sx * ofactor / np->spread.X) + 3;
nly = (int)ceil(sy * ofactor / np->spread.Y) + 3;
nlz = is3d ? (int)ceil(sz * ofactor / np->spread.Z) + 3 : 1;
nlx = (int)ceil(sx * ofactor / np.spread.X) + 3;
nly = (int)ceil(sy * ofactor / np.spread.Y) + 3;
nlz = is3d ? (int)ceil(sz * ofactor / np.spread.Z) + 3 : 1;

delete[] noise_buf;
try {
Expand Down Expand Up @@ -440,7 +440,7 @@ void Noise::gradientMap2D(
int index, i, j, x0, y0, noisex, noisey;
int nlx, nly;

Interp2dFxn interpolate = (np->flags & NOISE_FLAG_EASED) ?
Interp2dFxn interpolate = (np.flags & NOISE_FLAG_EASED) ?
biLinearInterpolation : biLinearInterpolationNoEase;

x0 = floor(x);
Expand Down Expand Up @@ -504,7 +504,7 @@ void Noise::gradientMap3D(
int index, i, j, k, x0, y0, z0, noisex, noisey, noisez;
int nlx, nly, nlz;

Interp3dFxn interpolate = (np->flags & NOISE_FLAG_EASED) ?
Interp3dFxn interpolate = (np.flags & NOISE_FLAG_EASED) ?
triLinearInterpolation : triLinearInterpolationNoEase;

x0 = floor(x);
Expand Down Expand Up @@ -588,8 +588,8 @@ float *Noise::perlinMap2D(float x, float y, float *persistence_map)
float f = 1.0, g = 1.0;
size_t bufsize = sx * sy;

x /= np->spread.X;
y /= np->spread.Y;
x /= np.spread.X;
y /= np.spread.Y;

memset(result, 0, sizeof(float) * bufsize);

Expand All @@ -600,15 +600,15 @@ float *Noise::perlinMap2D(float x, float y, float *persistence_map)
persist_buf[i] = 1.0;
}

for (size_t oct = 0; oct < np->octaves; oct++) {
for (size_t oct = 0; oct < np.octaves; oct++) {
gradientMap2D(x * f, y * f,
f / np->spread.X, f / np->spread.Y,
seed + np->seed + oct);
f / np.spread.X, f / np.spread.Y,
seed + np.seed + oct);

updateResults(g, persist_buf, persistence_map, bufsize);

f *= np->lacunarity;
g *= np->persist;
f *= np.lacunarity;
g *= np.persist;
}

return result;
Expand All @@ -620,9 +620,9 @@ float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
float f = 1.0, g = 1.0;
size_t bufsize = sx * sy * sz;

x /= np->spread.X;
y /= np->spread.Y;
z /= np->spread.Z;
x /= np.spread.X;
y /= np.spread.Y;
z /= np.spread.Z;

memset(result, 0, sizeof(float) * bufsize);

Expand All @@ -633,15 +633,15 @@ float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
persist_buf[i] = 1.0;
}

for (size_t oct = 0; oct < np->octaves; oct++) {
for (size_t oct = 0; oct < np.octaves; oct++) {
gradientMap3D(x * f, y * f, z * f,
f / np->spread.X, f / np->spread.Y, f / np->spread.Z,
seed + np->seed + oct);
f / np.spread.X, f / np.spread.Y, f / np.spread.Z,
seed + np.seed + oct);

updateResults(g, persist_buf, persistence_map, bufsize);

f *= np->lacunarity;
g *= np->persist;
f *= np.lacunarity;
g *= np.persist;
}

return result;
Expand All @@ -653,7 +653,7 @@ void Noise::updateResults(float g, float *gmap,
{
// This looks very ugly, but it is 50-70% faster than having
// conditional statements inside the loop
if (np->flags & NOISE_FLAG_ABSVALUE) {
if (np.flags & NOISE_FLAG_ABSVALUE) {
if (persistence_map) {
for (size_t i = 0; i != bufsize; i++) {
result[i] += gmap[i] * fabs(gradient_buf[i]);
Expand Down Expand Up @@ -685,6 +685,6 @@ void Noise::transformNoiseMap()
// slowdown even with -O2. To prevent this, store the value in a local.
size_t bufsize = sx * sy * sz;
for (size_t i = 0; i != bufsize; i++)
result[i] = result[i] * np->scale + np->offset;
result[i] = result[i] * np.scale + np.offset;
}

2 changes: 1 addition & 1 deletion src/noise.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ struct NoiseParams {

class Noise {
public:
NoiseParams *np;
NoiseParams np;
int seed;
int sx;
int sy;
Expand Down
9 changes: 8 additions & 1 deletion src/script/lua_api/l_mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,14 @@ int ModApiMapgen::l_register_ore(lua_State *L)
getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);

lua_getfield(L, index, "noise_params");
ore->np = get_noiseparams(L, -1);
if (read_noiseparams(L, -1, &ore->np)) {
ore->flags |= OREFLAG_USE_NOISE;
} else if (ore->NEEDS_NOISE) {
errorstream << "register_ore: specified ore type requires valid "
"noise parameters" << std::endl;
delete ore;
return 0;
}
lua_pop(L, 1);

u32 id = oremgr->add(ore);
Expand Down
8 changes: 4 additions & 4 deletions src/script/lua_api/l_noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
for (int y = 0; y != n->sy; y++) {
lua_newtable(L);
for (int x = 0; x != n->sx; x++) {
float noiseval = n->np->offset + n->np->scale * n->result[i++];
float noiseval = n->np.offset + n->np.scale * n->result[i++];
lua_pushnumber(L, noiseval);
lua_rawseti(L, -2, x + 1);
}
Expand All @@ -185,7 +185,7 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)

lua_newtable(L);
for (int i = 0; i != maplen; i++) {
float noiseval = n->np->offset + n->np->scale * n->result[i];
float noiseval = n->np.offset + n->np.scale * n->result[i];
lua_pushnumber(L, noiseval);
lua_rawseti(L, -2, i + 1);
}
Expand All @@ -210,7 +210,7 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
for (int y = 0; y != n->sy; y++) {
lua_newtable(L);
for (int x = 0; x != n->sx; x++) {
lua_pushnumber(L, n->np->offset + n->np->scale * n->result[i++]);
lua_pushnumber(L, n->np.offset + n->np.scale * n->result[i++]);
lua_rawseti(L, -2, x + 1);
}
lua_rawseti(L, -2, y + 1);
Expand All @@ -236,7 +236,7 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)

lua_newtable(L);
for (int i = 0; i != maplen; i++) {
float noiseval = n->np->offset + n->np->scale * n->result[i];
float noiseval = n->np.offset + n->np.scale * n->result[i];
lua_pushnumber(L, noiseval);
lua_rawseti(L, -2, i + 1);
}
Expand Down