Browse Source

RNG: do not rely on implicit conversions in arithmetic expressions

pull/1170/head
Xadhoom 5 years ago committed by Anders Jenbo
parent
commit
cb65d308cf
  1. 18
      Source/engine.cpp
  2. 6
      Source/engine.h

18
Source/engine.cpp

@ -16,20 +16,20 @@
DEVILUTION_BEGIN_NAMESPACE DEVILUTION_BEGIN_NAMESPACE
/** Seed value before the most recent call to SetRndSeed() */ /** Seed value before the most recent call to SetRndSeed() */
int orgseed; Sint32 orgseed;
/** Current game seed */ /** Current game seed */
int sglGameSeed; Sint32 sglGameSeed;
static CCritSect sgMemCrit; static CCritSect sgMemCrit;
/** /**
* Specifies the increment used in the Borland C/C++ pseudo-random. * Specifies the increment used in the Borland C/C++ pseudo-random.
*/ */
const int RndInc = 1; const Uint32 RndInc = 1;
/** /**
* Specifies the multiplier used in the Borland C/C++ pseudo-random number generator algorithm. * Specifies the multiplier used in the Borland C/C++ pseudo-random number generator algorithm.
*/ */
const int RndMult = 0x015A4E35; const Uint32 RndMult = 0x015A4E35;
void CelDrawTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth) void CelDrawTo(CelOutputBuffer out, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth)
{ {
@ -634,7 +634,7 @@ int GetDirection(int x1, int y1, int x2, int y2)
* @brief Set the RNG seed * @brief Set the RNG seed
* @param s RNG seed * @param s RNG seed
*/ */
void SetRndSeed(int s) void SetRndSeed(Sint32 s)
{ {
sglGameSeed = s; sglGameSeed = s;
orgseed = s; orgseed = s;
@ -644,9 +644,9 @@ void SetRndSeed(int s)
* @brief Advance the internal RNG seed and return the new value * @brief Advance the internal RNG seed and return the new value
* @return RNG seed * @return RNG seed
*/ */
int AdvanceRndSeed() Sint32 AdvanceRndSeed()
{ {
sglGameSeed = static_cast<unsigned int>(RndMult) * sglGameSeed + RndInc; sglGameSeed = (RndMult * static_cast<Uint32>(sglGameSeed)) + RndInc;
return abs(sglGameSeed); return abs(sglGameSeed);
} }
@ -654,7 +654,7 @@ int AdvanceRndSeed()
* @brief Get the current RNG seed * @brief Get the current RNG seed
* @return RNG seed * @return RNG seed
*/ */
int GetRndSeed() Sint32 GetRndSeed()
{ {
return abs(sglGameSeed); return abs(sglGameSeed);
} }
@ -665,7 +665,7 @@ int GetRndSeed()
* @param v The upper limit for the return value * @param v The upper limit for the return value
* @return A random number from 0 to (v-1) * @return A random number from 0 to (v-1)
*/ */
int random_(BYTE idx, int v) Sint32 random_(BYTE idx, Sint32 v)
{ {
if (v <= 0) if (v <= 0)
return 0; return 0;

6
Source/engine.h

@ -323,9 +323,9 @@ void DrawHalfTransparentRectTo(CelOutputBuffer out, int sx, int sy, int width, i
int GetDirection(int x1, int y1, int x2, int y2); int GetDirection(int x1, int y1, int x2, int y2);
void SetRndSeed(int s); void SetRndSeed(int s);
int AdvanceRndSeed(); Sint32 AdvanceRndSeed();
int GetRndSeed(); Sint32 GetRndSeed();
int random_(BYTE idx, int v); Sint32 random_(BYTE idx, int v);
BYTE *DiabloAllocPtr(DWORD dwBytes); BYTE *DiabloAllocPtr(DWORD dwBytes);
void mem_free_dbg(void *p); void mem_free_dbg(void *p);
BYTE *LoadFileInMem(const char *pszName, DWORD *pdwFileLen); BYTE *LoadFileInMem(const char *pszName, DWORD *pdwFileLen);

Loading…
Cancel
Save