Skip to content

Commit dfbdb5b

Browse files
committed
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.
1 parent 2060fd9 commit dfbdb5b

13 files changed

+68
-54
lines changed

src/cavegen.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0
3535

3636
CavesNoiseIntersection::CavesNoiseIntersection(
3737
INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize,
38-
NoiseParams *np_cave1, NoiseParams *np_cave2, int seed, float cave_width)
38+
NoiseParams *np_cave1, NoiseParams *np_cave2, s32 seed, float cave_width)
3939
{
4040
assert(nodedef);
4141
assert(biomemgr);
@@ -130,7 +130,7 @@ void CavesNoiseIntersection::generateCaves(MMVManip *vm,
130130
CavesRandomWalk::CavesRandomWalk(
131131
INodeDefManager *ndef,
132132
GenerateNotifier *gennotify,
133-
int seed,
133+
s32 seed,
134134
int water_level,
135135
content_t water_source,
136136
content_t lava_source)

src/cavegen.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CavesNoiseIntersection {
4141
public:
4242
CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr,
4343
v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2,
44-
int seed, float cave_width);
44+
s32 seed, float cave_width);
4545
~CavesNoiseIntersection();
4646

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

8585
// configurable parameters
86-
int seed;
86+
s32 seed;
8787
int water_level;
8888
int lava_depth;
8989
NoiseParams *np_caveliquids;
@@ -122,7 +122,7 @@ class CavesRandomWalk {
122122
// If gennotify is NULL, generation events are not logged.
123123
CavesRandomWalk(INodeDefManager *ndef,
124124
GenerateNotifier *gennotify = NULL,
125-
int seed = 0,
125+
s32 seed = 0,
126126
int water_level = 1,
127127
content_t water_source = CONTENT_IGNORE,
128128
content_t lava_source = CONTENT_IGNORE);

src/dungeongen.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int dir_to_facedir(v3s16 d);
3939

4040

4141
struct DungeonParams {
42-
int seed;
42+
s32 seed;
4343

4444
content_t c_water;
4545
content_t c_river_water;

src/mapgen.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,25 @@ Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
8989
{
9090
generating = false;
9191
id = mapgenid;
92-
seed = (int)params->seed;
9392
water_level = params->water_level;
9493
flags = params->flags;
9594
csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
9695

96+
/*
97+
We are losing half our entropy by doing this, but it is necessary to
98+
preserve reverse compatibility. If the top half of our current 64 bit
99+
seeds ever starts getting used, existing worlds will break due to a
100+
different hash outcome and no way to differentiate between versions.
101+
102+
A solution could be to add a new bit to designate that the top half of
103+
the seed value should be used, essentially a 1-bit version code, but
104+
this would require increasing the total size of a seed to 9 bytes (yuck)
105+
106+
It's probably okay if this never gets fixed. 4.2 billion possibilities
107+
ought to be enough for anyone.
108+
*/
109+
seed = (s32)params->seed;
110+
97111
vm = NULL;
98112
ndef = emerge->ndef;
99113
biomegen = NULL;
@@ -107,7 +121,7 @@ Mapgen::~Mapgen()
107121
}
108122

109123

110-
u32 Mapgen::getBlockSeed(v3s16 p, int seed)
124+
u32 Mapgen::getBlockSeed(v3s16 p, s32 seed)
111125
{
112126
return (u32)seed +
113127
p.Z * 38134234 +
@@ -116,7 +130,7 @@ u32 Mapgen::getBlockSeed(v3s16 p, int seed)
116130
}
117131

118132

119-
u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
133+
u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed)
120134
{
121135
u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
122136
n = (n >> 13) ^ n;

src/mapgen.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ struct MapgenParams {
150150
*/
151151
class Mapgen {
152152
public:
153-
int seed;
153+
s32 seed;
154154
int water_level;
155155
u32 flags;
156156
bool generating;
@@ -171,8 +171,8 @@ class Mapgen {
171171
Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
172172
virtual ~Mapgen();
173173

174-
static u32 getBlockSeed(v3s16 p, int seed);
175-
static u32 getBlockSeed2(v3s16 p, int seed);
174+
static u32 getBlockSeed(v3s16 p, s32 seed);
175+
static u32 getBlockSeed2(v3s16 p, s32 seed);
176176
s16 findGroundLevelFull(v2s16 p2d);
177177
s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax);
178178
s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);

src/mg_biome.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct BiomeParams {
8080
virtual void writeParams(Settings *settings) const = 0;
8181
virtual ~BiomeParams() {}
8282

83-
int seed;
83+
s32 seed;
8484
};
8585

8686
class BiomeGen {

src/noise.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials)
156156

157157
///////////////////////////////////////////////////////////////////////////////
158158

159-
float noise2d(int x, int y, int seed)
159+
float noise2d(int x, int y, s32 seed)
160160
{
161161
unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
162162
+ NOISE_MAGIC_SEED * seed) & 0x7fffffff;
@@ -166,7 +166,7 @@ float noise2d(int x, int y, int seed)
166166
}
167167

168168

169-
float noise3d(int x, int y, int z, int seed)
169+
float noise3d(int x, int y, int z, s32 seed)
170170
{
171171
unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
172172
+ NOISE_MAGIC_SEED * seed) & 0x7fffffff;
@@ -235,7 +235,7 @@ float triLinearInterpolationNoEase(
235235
return linearInterpolation(u, v, z);
236236
}
237237

238-
float noise2d_gradient(float x, float y, int seed, bool eased)
238+
float noise2d_gradient(float x, float y, s32 seed, bool eased)
239239
{
240240
// Calculate the integer coordinates
241241
int x0 = myfloor(x);
@@ -256,7 +256,7 @@ float noise2d_gradient(float x, float y, int seed, bool eased)
256256
}
257257

258258

259-
float noise3d_gradient(float x, float y, float z, int seed, bool eased)
259+
float noise3d_gradient(float x, float y, float z, s32 seed, bool eased)
260260
{
261261
// Calculate the integer coordinates
262262
int x0 = myfloor(x);
@@ -290,7 +290,7 @@ float noise3d_gradient(float x, float y, float z, int seed, bool eased)
290290
}
291291

292292

293-
float noise2d_perlin(float x, float y, int seed,
293+
float noise2d_perlin(float x, float y, s32 seed,
294294
int octaves, float persistence, bool eased)
295295
{
296296
float a = 0;
@@ -306,7 +306,7 @@ float noise2d_perlin(float x, float y, int seed,
306306
}
307307

308308

309-
float noise2d_perlin_abs(float x, float y, int seed,
309+
float noise2d_perlin_abs(float x, float y, s32 seed,
310310
int octaves, float persistence, bool eased)
311311
{
312312
float a = 0;
@@ -321,7 +321,7 @@ float noise2d_perlin_abs(float x, float y, int seed,
321321
}
322322

323323

324-
float noise3d_perlin(float x, float y, float z, int seed,
324+
float noise3d_perlin(float x, float y, float z, s32 seed,
325325
int octaves, float persistence, bool eased)
326326
{
327327
float a = 0;
@@ -336,7 +336,7 @@ float noise3d_perlin(float x, float y, float z, int seed,
336336
}
337337

338338

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

365365

366-
float NoisePerlin2D(NoiseParams *np, float x, float y, int seed)
366+
float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
367367
{
368368
float a = 0;
369369
float f = 1.0;
@@ -389,7 +389,7 @@ float NoisePerlin2D(NoiseParams *np, float x, float y, int seed)
389389
}
390390

391391

392-
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed)
392+
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
393393
{
394394
float a = 0;
395395
float f = 1.0;
@@ -416,7 +416,7 @@ float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed)
416416
}
417417

418418

419-
Noise::Noise(NoiseParams *np_, int seed, u32 sx, u32 sy, u32 sz)
419+
Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
420420
{
421421
memcpy(&np, np_, sizeof(np));
422422
this->seed = seed;
@@ -543,7 +543,7 @@ void Noise::resizeNoiseBuf(bool is3d)
543543
void Noise::gradientMap2D(
544544
float x, float y,
545545
float step_x, float step_y,
546-
int seed)
546+
s32 seed)
547547
{
548548
float v00, v01, v10, v11, u, v, orig_u;
549549
u32 index, i, j, noisex, noisey;
@@ -607,7 +607,7 @@ void Noise::gradientMap2D(
607607
void Noise::gradientMap3D(
608608
float x, float y, float z,
609609
float step_x, float step_y, float step_z,
610-
int seed)
610+
s32 seed)
611611
{
612612
float v000, v010, v100, v110;
613613
float v001, v011, v101, v111;

src/noise.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct NoiseParams {
148148
class Noise {
149149
public:
150150
NoiseParams np;
151-
int seed;
151+
s32 seed;
152152
u32 sx;
153153
u32 sy;
154154
u32 sz;
@@ -157,7 +157,7 @@ class Noise {
157157
float *persist_buf;
158158
float *result;
159159

160-
Noise(NoiseParams *np, int seed, u32 sx, u32 sy, u32 sz=1);
160+
Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
161161
~Noise();
162162

163163
void setSize(u32 sx, u32 sy, u32 sz=1);
@@ -167,11 +167,11 @@ class Noise {
167167
void gradientMap2D(
168168
float x, float y,
169169
float step_x, float step_y,
170-
int seed);
170+
s32 seed);
171171
void gradientMap3D(
172172
float x, float y, float z,
173173
float step_x, float step_y, float step_z,
174-
int seed);
174+
s32 seed);
175175

176176
float *perlinMap2D(float x, float y, float *persistence_map=NULL);
177177
float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
@@ -202,11 +202,11 @@ class Noise {
202202

203203
};
204204

205-
float NoisePerlin2D(NoiseParams *np, float x, float y, int seed);
206-
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed);
205+
float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
206+
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);
207207

208208
inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
209-
float y, float yoff, int seed)
209+
float y, float yoff, s32 seed)
210210
{
211211
return NoisePerlin2D(np,
212212
x + xoff * np->spread.X,
@@ -215,7 +215,7 @@ inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
215215
}
216216

217217
inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
218-
float y, float yoff, float z, float zoff, int seed)
218+
float y, float yoff, float z, float zoff, s32 seed)
219219
{
220220
return NoisePerlin3D(np,
221221
x + xoff * np->spread.X,
@@ -225,22 +225,22 @@ inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
225225
}
226226

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

231-
float noise2d_gradient(float x, float y, int seed, bool eased=true);
232-
float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
231+
float noise2d_gradient(float x, float y, s32 seed, bool eased=true);
232+
float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);
233233

234-
float noise2d_perlin(float x, float y, int seed,
234+
float noise2d_perlin(float x, float y, s32 seed,
235235
int octaves, float persistence, bool eased=true);
236236

237-
float noise2d_perlin_abs(float x, float y, int seed,
237+
float noise2d_perlin_abs(float x, float y, s32 seed,
238238
int octaves, float persistence, bool eased=true);
239239

240-
float noise3d_perlin(float x, float y, float z, int seed,
240+
float noise3d_perlin(float x, float y, float z, s32 seed,
241241
int octaves, float persistence, bool eased=false);
242242

243-
float noise3d_perlin_abs(float x, float y, float z, int seed,
243+
float noise3d_perlin_abs(float x, float y, float z, s32 seed,
244244
int octaves, float persistence, bool eased=false);
245245

246246
inline float easeCurve(float t)

src/script/lua_api/l_env.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L)
758758
return 0;
759759
v3s16 size = read_v3s16(L, 2);
760760

761-
int seed = (int)(env->getServerMap().getSeed());
761+
s32 seed = (s32)(env->getServerMap().getSeed());
762762
LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size);
763763
*(void **)(lua_newuserdata(L, sizeof(void *))) = n;
764764
luaL_getmetatable(L, "PerlinNoiseMap");

src/script/lua_api/l_noise.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const luaL_reg LuaPerlinNoise::methods[] = {
146146
LuaPerlinNoiseMap
147147
*/
148148

149-
LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, int seed, v3s16 size)
149+
LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, s32 seed, v3s16 size)
150150
{
151151
m_is3d = size.Z > 1;
152152
np = *params;

src/script/lua_api/l_noise.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class LuaPerlinNoiseMap : public ModApiBase {
7979
static int l_getMapSlice(lua_State *L);
8080

8181
public:
82-
LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size);
82+
LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size);
8383

8484
~LuaPerlinNoiseMap();
8585

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

113113
public:
114-
LuaPseudoRandom(int seed) :
114+
LuaPseudoRandom(s32 seed) :
115115
m_pseudo(seed) {}
116116

117117
// LuaPseudoRandom(seed)

0 commit comments

Comments
 (0)