Skip to content

Commit b18feac

Browse files
authored
refactor(random): Simplify code in RandomValue (TheSuperHackers#2370)
1 parent 41a848c commit b18feac

1 file changed

Lines changed: 81 additions & 101 deletions

File tree

Core/GameEngine/Source/Common/RandomValue.cpp

Lines changed: 81 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,42 @@
2828

2929
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
3030

31-
3231
#include "Lib/BaseType.h"
3332
#include "Common/RandomValue.h"
3433
#include "Common/crc.h"
3534
#include "Common/Debug.h"
3635
#include "GameLogic/GameLogic.h"
3736

38-
//#define DETERMINISTIC // to allow repetition for debugging
39-
40-
37+
#undef DEBUG_RANDOM_AUDIO
4138
#undef DEBUG_RANDOM_CLIENT
4239
#undef DEBUG_RANDOM_LOGIC
43-
#undef DEBUG_RANDOM_AUDIO
40+
#undef DETERMINISTIC
4441

42+
//#define DEBUG_RANDOM_AUDIO
4543
//#define DEBUG_RANDOM_CLIENT
4644
//#define DEBUG_RANDOM_LOGIC
47-
//#define DEBUG_RANDOM_AUDIO
45+
//#define DETERMINISTIC // to allow repetition for debugging
4846

49-
static const Real theMultFactor = 1.0f / (powf(2, 8 * sizeof(UnsignedInt)) - 1.0f);
47+
static const Real theMultFactor = 1.0f / static_cast<float>(UINT_MAX);
5048

5149
// Initial seed values.
52-
static UnsignedInt theGameClientSeed[6] =
50+
static UnsignedInt theGameAudioSeed[6] =
5351
{
54-
0xf22d0e56L, 0x883126e9L, 0xc624dd2fL, 0x702c49cL, 0x9e353f7dL, 0x6fdf3b64L
52+
0xf22d0e56L, 0x883126e9L, 0xc624dd2fL, 0x702c49cL, 0x9e353f7dL, 0x6fdf3b64L
5553
};
5654

57-
static UnsignedInt theGameAudioSeed[6] =
55+
static UnsignedInt theGameClientSeed[6] =
5856
{
59-
0xf22d0e56L, 0x883126e9L, 0xc624dd2fL, 0x702c49cL, 0x9e353f7dL, 0x6fdf3b64L
57+
0xf22d0e56L, 0x883126e9L, 0xc624dd2fL, 0x702c49cL, 0x9e353f7dL, 0x6fdf3b64L
6058
};
6159

62-
static UnsignedInt theGameLogicBaseSeed = 0;
6360
static UnsignedInt theGameLogicSeed[6] =
6461
{
65-
0xf22d0e56L, 0x883126e9L, 0xc624dd2fL, 0x702c49cL, 0x9e353f7dL, 0x6fdf3b64L
62+
0xf22d0e56L, 0x883126e9L, 0xc624dd2fL, 0x702c49cL, 0x9e353f7dL, 0x6fdf3b64L
6663
};
6764

65+
static UnsignedInt theGameLogicBaseSeed = 0;
66+
6867
UnsignedInt GetGameLogicRandomSeed()
6968
{
7069
return theGameLogicBaseSeed;
@@ -73,11 +72,11 @@ UnsignedInt GetGameLogicRandomSeed()
7372
UnsignedInt GetGameLogicRandomSeedCRC()
7473
{
7574
CRC c;
76-
c.computeCRC(theGameLogicSeed, 6*sizeof(UnsignedInt));
75+
c.computeCRC(theGameLogicSeed, sizeof(theGameLogicSeed));
7776
return c.get();
7877
}
7978

80-
static void seedRandom(UnsignedInt SEED, UnsignedInt *seed)
79+
static void seedRandom(UnsignedInt SEED, UnsignedInt (&seed)[6])
8180
{
8281
UnsignedInt ax;
8382

@@ -100,12 +99,12 @@ void InitRandom()
10099
{
101100
#ifdef DETERMINISTIC
102101
// needs to be the same every time
103-
seedRandom(0, theGameClientSeed);
104102
seedRandom(0, theGameAudioSeed);
103+
seedRandom(0, theGameClientSeed);
105104
seedRandom(0, theGameLogicSeed);
106105
theGameLogicBaseSeed = 0;
107106
#else
108-
time_t seconds = time( nullptr );
107+
const time_t seconds = time( nullptr );
109108

110109
seedRandom(seconds, theGameAudioSeed);
111110
seedRandom(seconds, theGameClientSeed);
@@ -116,24 +115,29 @@ void InitRandom()
116115

117116
void InitRandom( UnsignedInt seed )
118117
{
118+
#ifdef DETERMINISTIC
119+
seed = 0;
120+
#endif
121+
119122
seedRandom(seed, theGameAudioSeed);
120123
seedRandom(seed, theGameClientSeed);
121124
seedRandom(seed, theGameLogicSeed);
122125
theGameLogicBaseSeed = seed;
126+
123127
#ifdef DEBUG_RANDOM_LOGIC
124-
DEBUG_LOG(( "InitRandom %08lx",seed));
128+
DEBUG_LOG(("InitRandom %08lx", seed));
125129
#endif
126130
}
127131

128-
// Add with carry. SUM is replaced with A + B + C, C is replaced with 1 if there was a carry, 0 if there wasn't. A carry occurred if the sum is less than one of the inputs. This is addition, so carry can never be more than one.
129-
#define ADC(SUM, A, B, C) SUM = (A) + (B) + (C); C = ((SUM < (A)) || (SUM < (B)))
132+
// Add with carry. SUM is replaced with A + B + C, C is replaced with 1 if there was a carry, 0 if there wasn't.
133+
// A carry occurred if the sum is less than one of the inputs. This is addition, so carry can never be more than one.
134+
#define ADC(SUM, A, B, C) SUM = (A) + (B) + (C); C = ((SUM < (A)) || (SUM < (B)))
130135

131-
static UnsignedInt randomValue(UnsignedInt *seed)
136+
static UnsignedInt randomValue(UnsignedInt (&seed)[6])
132137
{
133138
UnsignedInt ax;
134139
UnsignedInt c = 0;
135140

136-
137141
ADC(ax, seed[5], seed[4], c); /* mov ax,seed+20 */
138142
/* add ax,seed+16 */
139143
seed[4] = ax; /* mov seed+8,ax */
@@ -168,7 +172,8 @@ static UnsignedInt randomValue(UnsignedInt *seed)
168172
}
169173
}
170174
}
171-
return(ax);
175+
176+
return ax;
172177
}
173178

174179
//
@@ -182,21 +187,18 @@ static UnsignedInt randomValue(UnsignedInt *seed)
182187
//
183188
Int GetGameAudioRandomValue( int lo, int hi, const char *file, int line )
184189
{
185-
UnsignedInt delta = hi - lo + 1;
186-
Int rval;
187-
188-
if (delta == 0)
190+
if (lo >= hi)
189191
return hi;
190192

191-
rval = ((Int)(randomValue(theGameAudioSeed) % delta)) + lo;
193+
const UnsignedInt delta = hi - lo + 1;
194+
const Int rval = ((Int)(randomValue(theGameAudioSeed) % delta)) + lo;
192195

193-
/**/
194196
#ifdef DEBUG_RANDOM_AUDIO
195-
DEBUG_LOG(( "%d: GetGameAudioRandomValue = %d (%d - %d), %s line %d",
196-
TheGameLogic->getFrame(), rval, lo, hi, file, line ));
197+
DEBUG_LOG(( "%d: GetGameAudioRandomValue = %d (%d - %d), %s line %d",
198+
TheGameLogic->getFrame(), rval, lo, hi, file, line ));
197199
#endif
198-
/**/
199200

201+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
200202
return rval;
201203
}
202204

@@ -205,22 +207,18 @@ DEBUG_LOG(( "%d: GetGameAudioRandomValue = %d (%d - %d), %s line %d",
205207
//
206208
Real GetGameAudioRandomValueReal( Real lo, Real hi, const char *file, int line )
207209
{
208-
Real delta = hi - lo;
209-
Real rval;
210-
211-
if (delta <= 0.0f)
210+
if (lo >= hi)
212211
return hi;
213212

214-
rval = ((Real)(randomValue(theGameAudioSeed)) * theMultFactor ) * delta + lo;
213+
const Real delta = hi - lo;
214+
const Real rval = ((Real)(randomValue(theGameAudioSeed)) * theMultFactor) * delta + lo;
215215

216-
DEBUG_ASSERTCRASH( rval >= lo && rval <= hi, ("Bad random val"));
217-
/**/
218216
#ifdef DEBUG_RANDOM_AUDIO
219-
DEBUG_LOG(( "%d: GetGameAudioRandomValueReal = %f, %s line %d",
220-
TheGameLogic->getFrame(), rval, file, line ));
217+
DEBUG_LOG(( "%d: GetGameAudioRandomValueReal = %f, %s line %d",
218+
TheGameLogic->getFrame(), rval, file, line ));
221219
#endif
222-
/**/
223220

221+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
224222
return rval;
225223
}
226224

@@ -229,21 +227,18 @@ DEBUG_LOG(( "%d: GetGameAudioRandomValueReal = %f, %s line %d",
229227
//
230228
Int GetGameClientRandomValue( int lo, int hi, const char *file, int line )
231229
{
232-
UnsignedInt delta = hi - lo + 1;
233-
Int rval;
234-
235-
if (delta == 0)
230+
if (lo >= hi)
236231
return hi;
237232

238-
rval = ((Int)(randomValue(theGameClientSeed) % delta)) + lo;
233+
const UnsignedInt delta = hi - lo + 1;
234+
const Int rval = ((Int)(randomValue(theGameClientSeed) % delta)) + lo;
239235

240-
/**/
241236
#ifdef DEBUG_RANDOM_CLIENT
242-
DEBUG_LOG(( "%d: GetGameClientRandomValue = %d (%d - %d), %s line %d",
243-
TheGameLogic ? TheGameLogic->getFrame() : -1, rval, lo, hi, file, line ));
237+
DEBUG_LOG(( "%d: GetGameClientRandomValue = %d (%d - %d), %s line %d",
238+
TheGameLogic ? TheGameLogic->getFrame() : -1, rval, lo, hi, file, line ));
244239
#endif
245-
/**/
246240

241+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
247242
return rval;
248243
}
249244

@@ -252,22 +247,18 @@ DEBUG_LOG(( "%d: GetGameClientRandomValue = %d (%d - %d), %s line %d",
252247
//
253248
Real GetGameClientRandomValueReal( Real lo, Real hi, const char *file, int line )
254249
{
255-
Real delta = hi - lo;
256-
Real rval;
257-
258-
if (delta <= 0.0f)
250+
if (lo >= hi)
259251
return hi;
260252

261-
rval = ((Real)(randomValue(theGameClientSeed)) * theMultFactor ) * delta + lo;
253+
const Real delta = hi - lo;
254+
const Real rval = ((Real)(randomValue(theGameClientSeed)) * theMultFactor) * delta + lo;
262255

263-
DEBUG_ASSERTCRASH( rval >= lo && rval <= hi, ("Bad random val"));
264-
/**/
265256
#ifdef DEBUG_RANDOM_CLIENT
266-
DEBUG_LOG(( "%d: GetGameClientRandomValueReal = %f, %s line %d",
267-
TheGameLogic->getFrame(), rval, file, line ));
257+
DEBUG_LOG(( "%d: GetGameClientRandomValueReal = %f, %s line %d",
258+
TheGameLogic->getFrame(), rval, file, line ));
268259
#endif
269-
/**/
270260

261+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
271262
return rval;
272263
}
273264

@@ -276,34 +267,25 @@ DEBUG_LOG(( "%d: GetGameClientRandomValueReal = %f, %s line %d",
276267
//
277268
Int GetGameLogicRandomValue( int lo, int hi, const char *file, int line )
278269
{
279-
//Int delta = hi - lo + 1;
280-
//Int rval;
281-
282-
//if (delta == 0)
283-
//return hi;
284-
285-
//rval = ((Int)(randomValue(theGameLogicSeed) % delta)) + lo;
286-
287-
UnsignedInt delta = hi - lo + 1;
288-
//UnsignedInt temp;
289-
Int rval;
290-
270+
#if RETAIL_COMPATIBLE_CRC
271+
const UnsignedInt delta = hi - lo + 1;
291272
if (delta == 0)
292273
return hi;
274+
#else
275+
if (lo >= hi)
276+
return hi;
293277

294-
rval = ((Int)(randomValue(theGameLogicSeed) % delta)) + lo;
295-
//temp = randomValue(theGameLogicSeed);
296-
//temp = temp % delta;
278+
const UnsignedInt delta = hi - lo + 1;
279+
#endif
297280

298-
//rval = temp + lo;
281+
const Int rval = ((Int)(randomValue(theGameLogicSeed) % delta)) + lo;
299282

300-
/**/
301283
#ifdef DEBUG_RANDOM_LOGIC
302-
DEBUG_LOG(( "%d: GetGameLogicRandomValue = %d (%d - %d), %s line %d",
303-
TheGameLogic->getFrame(), rval, lo, hi, file, line ));
284+
DEBUG_LOG(( "%d: GetGameLogicRandomValue = %d (%d - %d), %s line %d",
285+
TheGameLogic->getFrame(), rval, lo, hi, file, line ));
304286
#endif
305-
/**/
306287

288+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
307289
return rval;
308290
}
309291

@@ -312,22 +294,25 @@ DEBUG_LOG(( "%d: GetGameLogicRandomValue = %d (%d - %d), %s line %d",
312294
//
313295
Real GetGameLogicRandomValueReal( Real lo, Real hi, const char *file, int line )
314296
{
315-
Real delta = hi - lo;
316-
Real rval;
317-
297+
#if RETAIL_COMPATIBLE_CRC
298+
const Real delta = hi - lo;
318299
if (delta <= 0.0f)
319300
return hi;
301+
#else
302+
if (lo >= hi)
303+
return hi;
320304

321-
rval = ((Real)(randomValue(theGameLogicSeed)) * theMultFactor ) * delta + lo;
305+
const Real delta = hi - lo;
306+
#endif
307+
308+
const Real rval = ((Real)(randomValue(theGameLogicSeed)) * theMultFactor) * delta + lo;
322309

323-
DEBUG_ASSERTCRASH( rval >= lo && rval <= hi, ("Bad random val"));
324-
/**/
325310
#ifdef DEBUG_RANDOM_LOGIC
326-
DEBUG_LOG(( "%d: GetGameLogicRandomValueReal = %f, %s line %d",
327-
TheGameLogic->getFrame(), rval, file, line ));
311+
DEBUG_LOG(( "%d: GetGameLogicRandomValueReal = %f, %s line %d",
312+
TheGameLogic->getFrame(), rval, file, line ));
328313
#endif
329-
/**/
330314

315+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
331316
return rval;
332317
}
333318

@@ -343,22 +328,21 @@ Int GetGameLogicRandomValueUnchanged( int lo, int hi, const char *file, int line
343328
return GetGameLogicRandomValue(lo, hi, file, line);
344329
#endif
345330

346-
const UnsignedInt delta = hi - lo + 1;
347-
if (delta == 0)
331+
if (lo >= hi)
348332
return hi;
349333

350334
UnsignedInt seed[ARRAY_SIZE(theGameLogicSeed)];
351335
memcpy(&seed[0], &theGameLogicSeed[0], sizeof(seed));
352336

337+
const UnsignedInt delta = hi - lo + 1;
353338
const Int rval = ((Int)(randomValue(seed) % delta)) + lo;
354339

355-
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
356-
357340
#ifdef DEBUG_RANDOM_LOGIC
358341
DEBUG_LOG(( "%d: GetGameLogicRandomValueUnchanged = %d (%d - %d), %s line %d",
359342
TheGameLogic->getFrame(), rval, lo, hi, file, line ));
360343
#endif
361344

345+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
362346
return rval;
363347
}
364348

@@ -374,22 +358,21 @@ Real GetGameLogicRandomValueRealUnchanged( Real lo, Real hi, const char *file, i
374358
return GetGameLogicRandomValueReal(lo, hi, file, line);
375359
#endif
376360

377-
const Real delta = hi - lo;
378-
if (delta <= 0.0f)
361+
if (lo >= hi)
379362
return hi;
380363

381364
UnsignedInt seed[ARRAY_SIZE(theGameLogicSeed)];
382365
memcpy(&seed[0], &theGameLogicSeed[0], sizeof(seed));
383366

367+
const Real delta = hi - lo;
384368
const Real rval = ((Real)(randomValue(seed)) * theMultFactor) * delta + lo;
385369

386-
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
387-
388370
#ifdef DEBUG_RANDOM_LOGIC
389371
DEBUG_LOG(( "%d: GetGameLogicRandomValueRealUnchanged = %f, %s line %d",
390372
TheGameLogic->getFrame(), rval, file, line ));
391373
#endif
392374

375+
DEBUG_ASSERTCRASH(rval >= lo && rval <= hi, ("Bad random val"));
393376
return rval;
394377
}
395378

@@ -438,7 +421,6 @@ Real GameClientRandomVariable::getValue() const
438421
}
439422
}
440423

441-
442424
//--------------------------------------------------------------------------------------------------------------
443425
// GameLogicRandomVariable
444426
//
@@ -483,5 +465,3 @@ Real GameLogicRandomVariable::getValue() const
483465
return 0.0f;
484466
}
485467
}
486-
487-

0 commit comments

Comments
 (0)