mirror of
https://github.com/1dot13/source.git
synced 2026-09-09 14:46:05 +02:00
Move random functions to .h and inline them. This will speed up the code overall given their high usage.
git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@2788 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
@@ -1,12 +1,8 @@
|
|||||||
#ifdef JA2_PRECOMPILED_HEADERS
|
#include "Random.h"
|
||||||
#include "JA2 SGP ALL.H"
|
#include <time.h>
|
||||||
#else
|
|
||||||
#include "Random.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
UINT32 guiPreRandomIndex = 0;
|
UINT32 guiPreRandomIndex = 0;
|
||||||
UINT32 guiPreRandomNums[ MAX_PREGENERATED_NUMS ];
|
std::vector<UINT32> guiPreRandomNums(MAX_PREGENERATED_NUMS, 0);
|
||||||
|
|
||||||
|
|
||||||
void InitializeRandom()
|
void InitializeRandom()
|
||||||
{
|
{
|
||||||
@@ -21,64 +17,3 @@ void InitializeRandom()
|
|||||||
}
|
}
|
||||||
guiPreRandomIndex = 0;
|
guiPreRandomIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a pseudo-random integer between 0 and uiRange
|
|
||||||
UINT32 Random(UINT32 uiRange)
|
|
||||||
{
|
|
||||||
// Always return 0, if no range given (it's not an error)
|
|
||||||
if (uiRange == 0)
|
|
||||||
return(0);
|
|
||||||
return rand() * uiRange / RAND_MAX % uiRange;
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOLEAN Chance( UINT32 uiChance )
|
|
||||||
{
|
|
||||||
//AssertLE(uiChance, 100);
|
|
||||||
// lalien: since uiChance calculation is based on different values it can happen that uiChance is > than 100%
|
|
||||||
// It's not critical because uiChance >= 100 will always return TRUE. No need to crash the entire game.
|
|
||||||
// Make sure that uiChance value is not < than 0, or it can switch to positive
|
|
||||||
|
|
||||||
return (BOOLEAN)(Random( 100 ) < uiChance);
|
|
||||||
}
|
|
||||||
|
|
||||||
UINT32 PreRandom( UINT32 uiRange )
|
|
||||||
{
|
|
||||||
UINT32 uiNum;
|
|
||||||
if( !uiRange )
|
|
||||||
return 0;
|
|
||||||
//Extract the current pregenerated number
|
|
||||||
uiNum = guiPreRandomNums[ guiPreRandomIndex ] * uiRange / RAND_MAX % uiRange;
|
|
||||||
//Replace the current pregenerated number with a new one.
|
|
||||||
|
|
||||||
//This was removed in the name of optimization. Uncomment if you hate recycling.
|
|
||||||
//guiPreRandomNums[ guiPreRandomIndex ] = rand();
|
|
||||||
|
|
||||||
//Go to the next index.
|
|
||||||
guiPreRandomIndex++;
|
|
||||||
//if( guiPreRandomIndex >= (UINT32)MAX_PREGENERATED_NUMS )
|
|
||||||
// guiPreRandomIndex = 0;
|
|
||||||
// WDS 07/06/2008 fix prerandom
|
|
||||||
if (guiPreRandomIndex == MAX_PREGENERATED_NUMS / 2) {
|
|
||||||
// [0..(MAX_PREGENERATED_NUMS/2) -1]
|
|
||||||
for( unsigned idx = 0; idx < MAX_PREGENERATED_NUMS / 2; ++idx ) {
|
|
||||||
guiPreRandomNums[ idx ] = rand();
|
|
||||||
}
|
|
||||||
} else if (guiPreRandomIndex >= (UINT32)MAX_PREGENERATED_NUMS ) {
|
|
||||||
// MAX_PREGENERATED_NUMS/2 .. MAX_PREGENERATED_NUMS-1]
|
|
||||||
for( unsigned idx = MAX_PREGENERATED_NUMS / 2; idx < MAX_PREGENERATED_NUMS; ++idx ) {
|
|
||||||
guiPreRandomNums[ idx ] = rand();
|
|
||||||
}
|
|
||||||
guiPreRandomIndex = 0;
|
|
||||||
}
|
|
||||||
return uiNum;
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOLEAN PreChance( UINT32 uiChance )
|
|
||||||
{
|
|
||||||
//AssertLE(uiChance, 100);
|
|
||||||
// lalien: since uiChance calculation is based on different values it can happen that uiChance is > than 100%
|
|
||||||
// It's not critical because uiChance >= 100 will always return TRUE. No need to crash the entire game.
|
|
||||||
// Make sure that uiChance value is not < than 0, or it can switch to positive
|
|
||||||
|
|
||||||
return (BOOLEAN)(PreRandom( 100 ) < uiChance);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,24 +4,92 @@
|
|||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
#include "Debug.h"
|
#include "Debug.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
|
||||||
|
//IMPORTANT: Changing this define will invalidate the JA2 save. If this
|
||||||
|
// is necessary, please ifdef your own value.
|
||||||
|
#define MAX_PREGENERATED_NUMS 256
|
||||||
|
|
||||||
|
extern UINT32 guiPreRandomIndex;
|
||||||
|
extern std::vector<UINT32> guiPreRandomNums;
|
||||||
|
|
||||||
extern void InitializeRandom(void);
|
extern void InitializeRandom(void);
|
||||||
extern UINT32 Random( UINT32 uiRange );
|
|
||||||
|
|
||||||
|
// WDS 04/20/2009 -- Random functions were moved to inline functions here in the header file
|
||||||
|
// to speed up the whole program a bit.
|
||||||
|
|
||||||
|
|
||||||
|
// Returns a pseudo-random unsigned integer between 0 and uiRange-1 inclusive.
|
||||||
|
// NOTE THE -1! So if you call Random(100) the numbers returned will
|
||||||
|
// be between 0 and 99 inclusive.
|
||||||
|
inline UINT32 Random(UINT32 uiRange)
|
||||||
|
{
|
||||||
|
// Always return 0, if no range given (it's not an error)
|
||||||
|
if (uiRange == 0)
|
||||||
|
return(0);
|
||||||
|
return rand() * uiRange / RAND_MAX % uiRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Returns a pseudo-random integer between 0 and uiRange-1
|
||||||
|
inline INT32 iRandom(UINT32 uiRange)
|
||||||
|
{
|
||||||
|
// Always return 0, if no range given (it's not an error)
|
||||||
|
if (uiRange == 0)
|
||||||
|
return(0);
|
||||||
|
return rand() * uiRange / RAND_MAX % uiRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Chance( 74 ) returns TRUE 74% of the time. If uiChance >= 100, then it will always return TRUE.
|
//Chance( 74 ) returns TRUE 74% of the time. If uiChance >= 100, then it will always return TRUE.
|
||||||
extern BOOLEAN Chance( UINT32 uiChance );
|
inline BOOLEAN Chance( UINT32 uiChance )
|
||||||
|
{
|
||||||
|
//AssertLE(uiChance, 100);
|
||||||
|
// lalien: since uiChance calculation is based on different values it can happen that uiChance is > than 100%
|
||||||
|
// It's not critical because uiChance >= 100 will always return TRUE. No need to crash the entire game.
|
||||||
|
// Make sure that uiChance value is not < than 0, or it can switch to positive
|
||||||
|
return (BOOLEAN)(Random( 100 ) < uiChance);
|
||||||
|
}
|
||||||
|
|
||||||
//Returns a pregenerated random number.
|
|
||||||
//Used to deter Ian's tactic of shoot, miss, restore saved game :)
|
|
||||||
extern UINT32 PreRandom( UINT32 uiRange );
|
|
||||||
extern BOOLEAN PreChance( UINT32 uiChance );
|
|
||||||
|
|
||||||
//IMPORTANT: Changing this define will invalidate the JA2 save. If this
|
//Returns a pregenerated random number.
|
||||||
// is necessary, please ifdef your own value.
|
//Used to deter Ian's tactic of shoot, miss, restore saved game :)
|
||||||
#define MAX_PREGENERATED_NUMS 256
|
inline UINT32 PreRandom( UINT32 uiRange )
|
||||||
extern UINT32 guiPreRandomIndex;
|
{
|
||||||
extern UINT32 guiPreRandomNums[ MAX_PREGENERATED_NUMS ];
|
UINT32 uiNum;
|
||||||
|
if( uiRange == 0 )
|
||||||
|
return 0;
|
||||||
|
//Extract the current pregenerated number
|
||||||
|
uiNum = guiPreRandomNums[ guiPreRandomIndex ] * uiRange / RAND_MAX % uiRange;
|
||||||
|
|
||||||
|
//Go to the next index.
|
||||||
|
guiPreRandomIndex++;
|
||||||
|
|
||||||
|
// Reload the random numbers even so often
|
||||||
|
if (guiPreRandomIndex == MAX_PREGENERATED_NUMS / 2) {
|
||||||
|
// [0..(MAX_PREGENERATED_NUMS/2) -1]
|
||||||
|
for( unsigned idx = 0; idx < MAX_PREGENERATED_NUMS / 2; ++idx ) {
|
||||||
|
guiPreRandomNums[ idx ] = rand();
|
||||||
|
}
|
||||||
|
} else if (guiPreRandomIndex >= (UINT32)MAX_PREGENERATED_NUMS ) {
|
||||||
|
// [MAX_PREGENERATED_NUMS/2 .. MAX_PREGENERATED_NUMS-1]
|
||||||
|
for( unsigned idx = MAX_PREGENERATED_NUMS / 2; idx < MAX_PREGENERATED_NUMS; ++idx ) {
|
||||||
|
guiPreRandomNums[ idx ] = rand();
|
||||||
|
}
|
||||||
|
guiPreRandomIndex = 0;
|
||||||
|
}
|
||||||
|
return uiNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline BOOLEAN PreChance( UINT32 uiChance )
|
||||||
|
{
|
||||||
|
//AssertLE(uiChance, 100);
|
||||||
|
// lalien: since uiChance calculation is based on different values it can happen that uiChance is > than 100%
|
||||||
|
// It's not critical because uiChance >= 100 will always return TRUE. No need to crash the entire game.
|
||||||
|
// Make sure that uiChance value is not < than 0, or it can switch to positive
|
||||||
|
|
||||||
|
return (BOOLEAN)(PreRandom( 100 ) < uiChance);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user