Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change internal type for seeds to s32
This fixes value truncation (and therefore incompatibility) on platforms
with an LP32 data model, such as VAX or MS-DOS.
  • Loading branch information
kwolekr committed Jun 4, 2016
1 parent 2060fd9 commit dfbdb5b
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/cavegen.cpp
Expand Up @@ -35,7 +35,7 @@ static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0

CavesNoiseIntersection::CavesNoiseIntersection(
INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize,
NoiseParams *np_cave1, NoiseParams *np_cave2, int seed, float cave_width)
NoiseParams *np_cave1, NoiseParams *np_cave2, s32 seed, float cave_width)
{
assert(nodedef);
assert(biomemgr);
Expand Down Expand Up @@ -130,7 +130,7 @@ void CavesNoiseIntersection::generateCaves(MMVManip *vm,
CavesRandomWalk::CavesRandomWalk(
INodeDefManager *ndef,
GenerateNotifier *gennotify,
int seed,
s32 seed,
int water_level,
content_t water_source,
content_t lava_source)
Expand Down
6 changes: 3 additions & 3 deletions src/cavegen.h
Expand Up @@ -41,7 +41,7 @@ class CavesNoiseIntersection {
public:
CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr,
v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2,
int seed, float cave_width);
s32 seed, float cave_width);
~CavesNoiseIntersection();

void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, u8 *biomemap);
Expand Down Expand Up @@ -83,7 +83,7 @@ class CavesRandomWalk {
s16 *heightmap;

// configurable parameters
int seed;
s32 seed;
int water_level;
int lava_depth;
NoiseParams *np_caveliquids;
Expand Down Expand Up @@ -122,7 +122,7 @@ class CavesRandomWalk {
// If gennotify is NULL, generation events are not logged.
CavesRandomWalk(INodeDefManager *ndef,
GenerateNotifier *gennotify = NULL,
int seed = 0,
s32 seed = 0,
int water_level = 1,
content_t water_source = CONTENT_IGNORE,
content_t lava_source = CONTENT_IGNORE);
Expand Down
2 changes: 1 addition & 1 deletion src/dungeongen.h
Expand Up @@ -39,7 +39,7 @@ int dir_to_facedir(v3s16 d);


struct DungeonParams {
int seed;
s32 seed;

content_t c_water;
content_t c_river_water;
Expand Down
20 changes: 17 additions & 3 deletions src/mapgen.cpp
Expand Up @@ -89,11 +89,25 @@ Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
{
generating = false;
id = mapgenid;
seed = (int)params->seed;
water_level = params->water_level;
flags = params->flags;
csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);

/*
We are losing half our entropy by doing this, but it is necessary to
preserve reverse compatibility. If the top half of our current 64 bit
seeds ever starts getting used, existing worlds will break due to a
different hash outcome and no way to differentiate between versions.
A solution could be to add a new bit to designate that the top half of
the seed value should be used, essentially a 1-bit version code, but
this would require increasing the total size of a seed to 9 bytes (yuck)
It's probably okay if this never gets fixed. 4.2 billion possibilities
ought to be enough for anyone.
*/
seed = (s32)params->seed;

vm = NULL;
ndef = emerge->ndef;
biomegen = NULL;
Expand All @@ -107,7 +121,7 @@ Mapgen::~Mapgen()
}


u32 Mapgen::getBlockSeed(v3s16 p, int seed)
u32 Mapgen::getBlockSeed(v3s16 p, s32 seed)
{
return (u32)seed +
p.Z * 38134234 +
Expand All @@ -116,7 +130,7 @@ u32 Mapgen::getBlockSeed(v3s16 p, int seed)
}


u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed)
{
u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
n = (n >> 13) ^ n;
Expand Down
6 changes: 3 additions & 3 deletions src/mapgen.h
Expand Up @@ -150,7 +150,7 @@ struct MapgenParams {
*/
class Mapgen {
public:
int seed;
s32 seed;
int water_level;
u32 flags;
bool generating;
Expand All @@ -171,8 +171,8 @@ class Mapgen {
Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
virtual ~Mapgen();

static u32 getBlockSeed(v3s16 p, int seed);
static u32 getBlockSeed2(v3s16 p, int seed);
static u32 getBlockSeed(v3s16 p, s32 seed);
static u32 getBlockSeed2(v3s16 p, s32 seed);
s16 findGroundLevelFull(v2s16 p2d);
s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
Expand Down
2 changes: 1 addition & 1 deletion src/mg_biome.h
Expand Up @@ -80,7 +80,7 @@ struct BiomeParams {
virtual void writeParams(Settings *settings) const = 0;
virtual ~BiomeParams() {}

int seed;
s32 seed;
};

class BiomeGen {
Expand Down
26 changes: 13 additions & 13 deletions src/noise.cpp
Expand Up @@ -156,7 +156,7 @@ s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials)

///////////////////////////////////////////////////////////////////////////////

float noise2d(int x, int y, int seed)
float noise2d(int x, int y, s32 seed)
{
unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
+ NOISE_MAGIC_SEED * seed) & 0x7fffffff;
Expand All @@ -166,7 +166,7 @@ float noise2d(int x, int y, int seed)
}


float noise3d(int x, int y, int z, int seed)
float noise3d(int x, int y, int z, s32 seed)
{
unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
+ NOISE_MAGIC_SEED * seed) & 0x7fffffff;
Expand Down Expand Up @@ -235,7 +235,7 @@ float triLinearInterpolationNoEase(
return linearInterpolation(u, v, z);
}

float noise2d_gradient(float x, float y, int seed, bool eased)
float noise2d_gradient(float x, float y, s32 seed, bool eased)
{
// Calculate the integer coordinates
int x0 = myfloor(x);
Expand All @@ -256,7 +256,7 @@ float noise2d_gradient(float x, float y, int seed, bool eased)
}


float noise3d_gradient(float x, float y, float z, int seed, bool eased)
float noise3d_gradient(float x, float y, float z, s32 seed, bool eased)
{
// Calculate the integer coordinates
int x0 = myfloor(x);
Expand Down Expand Up @@ -290,7 +290,7 @@ float noise3d_gradient(float x, float y, float z, int seed, bool eased)
}


float noise2d_perlin(float x, float y, int seed,
float noise2d_perlin(float x, float y, s32 seed,
int octaves, float persistence, bool eased)
{
float a = 0;
Expand All @@ -306,7 +306,7 @@ float noise2d_perlin(float x, float y, int seed,
}


float noise2d_perlin_abs(float x, float y, int seed,
float noise2d_perlin_abs(float x, float y, s32 seed,
int octaves, float persistence, bool eased)
{
float a = 0;
Expand All @@ -321,7 +321,7 @@ float noise2d_perlin_abs(float x, float y, int seed,
}


float noise3d_perlin(float x, float y, float z, int seed,
float noise3d_perlin(float x, float y, float z, s32 seed,
int octaves, float persistence, bool eased)
{
float a = 0;
Expand All @@ -336,7 +336,7 @@ float noise3d_perlin(float x, float y, float z, int seed,
}


float noise3d_perlin_abs(float x, float y, float z, int seed,
float noise3d_perlin_abs(float x, float y, float z, s32 seed,
int octaves, float persistence, bool eased)
{
float a = 0;
Expand All @@ -363,7 +363,7 @@ float contour(float v)
///////////////////////// [ New noise ] ////////////////////////////


float NoisePerlin2D(NoiseParams *np, float x, float y, int seed)
float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
{
float a = 0;
float f = 1.0;
Expand All @@ -389,7 +389,7 @@ float NoisePerlin2D(NoiseParams *np, float x, float y, int seed)
}


float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed)
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
{
float a = 0;
float f = 1.0;
Expand All @@ -416,7 +416,7 @@ float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed)
}


Noise::Noise(NoiseParams *np_, int seed, u32 sx, u32 sy, u32 sz)
Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
{
memcpy(&np, np_, sizeof(np));
this->seed = seed;
Expand Down Expand Up @@ -543,7 +543,7 @@ void Noise::resizeNoiseBuf(bool is3d)
void Noise::gradientMap2D(
float x, float y,
float step_x, float step_y,
int seed)
s32 seed)
{
float v00, v01, v10, v11, u, v, orig_u;
u32 index, i, j, noisex, noisey;
Expand Down Expand Up @@ -607,7 +607,7 @@ void Noise::gradientMap2D(
void Noise::gradientMap3D(
float x, float y, float z,
float step_x, float step_y, float step_z,
int seed)
s32 seed)
{
float v000, v010, v100, v110;
float v001, v011, v101, v111;
Expand Down
32 changes: 16 additions & 16 deletions src/noise.h
Expand Up @@ -148,7 +148,7 @@ struct NoiseParams {
class Noise {
public:
NoiseParams np;
int seed;
s32 seed;
u32 sx;
u32 sy;
u32 sz;
Expand All @@ -157,7 +157,7 @@ class Noise {
float *persist_buf;
float *result;

Noise(NoiseParams *np, int seed, u32 sx, u32 sy, u32 sz=1);
Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
~Noise();

void setSize(u32 sx, u32 sy, u32 sz=1);
Expand All @@ -167,11 +167,11 @@ class Noise {
void gradientMap2D(
float x, float y,
float step_x, float step_y,
int seed);
s32 seed);
void gradientMap3D(
float x, float y, float z,
float step_x, float step_y, float step_z,
int seed);
s32 seed);

float *perlinMap2D(float x, float y, float *persistence_map=NULL);
float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
Expand Down Expand Up @@ -202,11 +202,11 @@ class Noise {

};

float NoisePerlin2D(NoiseParams *np, float x, float y, int seed);
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed);
float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);

inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
float y, float yoff, int seed)
float y, float yoff, s32 seed)
{
return NoisePerlin2D(np,
x + xoff * np->spread.X,
Expand All @@ -215,7 +215,7 @@ inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
}

inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
float y, float yoff, float z, float zoff, int seed)
float y, float yoff, float z, float zoff, s32 seed)
{
return NoisePerlin3D(np,
x + xoff * np->spread.X,
Expand All @@ -225,22 +225,22 @@ inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
}

// Return value: -1 ... 1
float noise2d(int x, int y, int seed);
float noise3d(int x, int y, int z, int seed);
float noise2d(int x, int y, s32 seed);
float noise3d(int x, int y, int z, s32 seed);

float noise2d_gradient(float x, float y, int seed, bool eased=true);
float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
float noise2d_gradient(float x, float y, s32 seed, bool eased=true);
float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);

float noise2d_perlin(float x, float y, int seed,
float noise2d_perlin(float x, float y, s32 seed,
int octaves, float persistence, bool eased=true);

float noise2d_perlin_abs(float x, float y, int seed,
float noise2d_perlin_abs(float x, float y, s32 seed,
int octaves, float persistence, bool eased=true);

float noise3d_perlin(float x, float y, float z, int seed,
float noise3d_perlin(float x, float y, float z, s32 seed,
int octaves, float persistence, bool eased=false);

float noise3d_perlin_abs(float x, float y, float z, int seed,
float noise3d_perlin_abs(float x, float y, float z, s32 seed,
int octaves, float persistence, bool eased=false);

inline float easeCurve(float t)
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_env.cpp
Expand Up @@ -758,7 +758,7 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L)
return 0;
v3s16 size = read_v3s16(L, 2);

int seed = (int)(env->getServerMap().getSeed());
s32 seed = (s32)(env->getServerMap().getSeed());
LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size);
*(void **)(lua_newuserdata(L, sizeof(void *))) = n;
luaL_getmetatable(L, "PerlinNoiseMap");
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_noise.cpp
Expand Up @@ -146,7 +146,7 @@ const luaL_reg LuaPerlinNoise::methods[] = {
LuaPerlinNoiseMap
*/

LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, int seed, v3s16 size)
LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, s32 seed, v3s16 size)
{
m_is3d = size.Z > 1;
np = *params;
Expand Down
4 changes: 2 additions & 2 deletions src/script/lua_api/l_noise.h
Expand Up @@ -79,7 +79,7 @@ class LuaPerlinNoiseMap : public ModApiBase {
static int l_getMapSlice(lua_State *L);

public:
LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size);
LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size);

~LuaPerlinNoiseMap();

Expand Down Expand Up @@ -111,7 +111,7 @@ class LuaPseudoRandom : public ModApiBase {
static int l_next(lua_State *L);

public:
LuaPseudoRandom(int seed) :
LuaPseudoRandom(s32 seed) :
m_pseudo(seed) {}

// LuaPseudoRandom(seed)
Expand Down

0 comments on commit dfbdb5b

Please sign in to comment.