diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/core.c | 13 | ||||
-rw-r--r-- | src/common/random.c | 30 | ||||
-rw-r--r-- | src/common/random.h | 30 |
3 files changed, 53 insertions, 20 deletions
diff --git a/src/common/core.c b/src/common/core.c index 314bdf947..f5860e992 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -25,15 +25,15 @@ #include "common/cbasetypes.h" #include "common/console.h" -#include "common/des.h" #include "common/db.h" +#include "common/des.h" #include "common/memmgr.h" #include "common/mmo.h" -#include "common/random.h" +#include "common/nullpo.h" #include "common/showmsg.h" #include "common/strlib.h" #include "common/sysinfo.h" -#include "common/nullpo.h" +#include "common/timer.h" #include "common/utils.h" #ifndef MINICORE @@ -42,10 +42,10 @@ # include "common/ers.h" # include "common/md5calc.h" # include "common/mutex.h" +# include "common/random.h" # include "common/socket.h" # include "common/sql.h" # include "common/thread.h" -# include "common/timer.h" #endif #ifndef _WIN32 @@ -264,6 +264,7 @@ void core_defaults(void) { timer_defaults(); db_defaults(); socket_defaults(); + rnd_defaults(); md5_defaults(); #endif } @@ -515,8 +516,7 @@ int main (int argc, char **argv) { timer->init(); /* timer first */ - rnd_init(); - srand((unsigned int)timer->gettick()); + rnd->init(); console->init(); @@ -543,6 +543,7 @@ int main (int argc, char **argv) { DB->final(); rathread_final(); ers_final(); + rnd->final(); #endif cmdline->final(); //sysinfo->final(); Called by iMalloc->final() diff --git a/src/common/random.c b/src/common/random.c index ba70a5c1d..dbb2ca830 100644 --- a/src/common/random.c +++ b/src/common/random.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -27,6 +27,7 @@ #include "common/timer.h" // gettick #include <mt19937ar/mt19937ar.h> // init_genrand, genrand_int32, genrand_res53 +#include <stdlib.h> #if defined(WIN32) # include "common/winapi.h" @@ -35,6 +36,8 @@ # include <unistd.h> #endif +struct rnd_interface rnd_s; +struct rnd_interface *rnd; /// Initializes the random number generator with an appropriate seed. void rnd_init(void) @@ -53,8 +56,14 @@ void rnd_init(void) #endif // HAVE_GETTID #endif init_genrand(seed); + + // Also initialize the stdlib rng, just in case it's used somewhere. + srand((unsigned int)timer->gettick()); } +void rnd_final(void) +{ +} /// Initializes the random number generator. void rnd_seed(uint32 seed) @@ -64,7 +73,7 @@ void rnd_seed(uint32 seed) /// Generates a random number in the interval [0, SINT32_MAX] -int32 rnd(void) +int32 rnd_random(void) { return (int32)genrand_int31(); } @@ -74,7 +83,7 @@ int32 rnd(void) /// NOTE: interval is open ended, so dice_faces is excluded (unless it's 0) uint32 rnd_roll(uint32 dice_faces) { - return (uint32)(rnd_uniform()*dice_faces); + return (uint32)(rnd->uniform()*dice_faces); } @@ -84,7 +93,7 @@ int32 rnd_value(int32 min, int32 max) { if( min >= max ) return min; - return min + (int32)(rnd_uniform()*(max-min+1)); + return min + (int32)(rnd->uniform()*(max-min+1)); } @@ -103,3 +112,16 @@ double rnd_uniform53(void) { return genrand_res53(); } + +void rnd_defaults(void) +{ + rnd = &rnd_s; + rnd->init = rnd_init; + rnd->final = rnd_final; + rnd->seed = rnd_seed; + rnd->random = rnd_random; + rnd->roll = rnd_roll; + rnd->value = rnd_value; + rnd->uniform = rnd_uniform; + rnd->uniform53 = rnd_uniform53; +} diff --git a/src/common/random.h b/src/common/random.h index 0b5abbcdc..41bdb4083 100644 --- a/src/common/random.h +++ b/src/common/random.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -21,17 +21,27 @@ #ifndef COMMON_RANDOM_H #define COMMON_RANDOM_H -#include "common/cbasetypes.h" +#include "common/hercules.h" + +struct rnd_interface { + void (*init) (void); + void (*final) (void); + + void (*seed) (uint32); + + int32 (*random) (void);// [0, SINT32_MAX] + uint32 (*roll) (uint32 dice_faces);// [0, dice_faces) + int32 (*value) (int32 min, int32 max);// [min, max] + double (*uniform) (void);// [0.0, 1.0) + double (*uniform53) (void);// [0.0, 1.0) +}; + +#define rnd() rnd->random() #ifdef HERCULES_CORE -void rnd_init(void); -void rnd_seed(uint32); - -int32 rnd(void);// [0, SINT32_MAX] -uint32 rnd_roll(uint32 dice_faces);// [0, dice_faces) -int32 rnd_value(int32 min, int32 max);// [min, max] -double rnd_uniform(void);// [0.0, 1.0) -double rnd_uniform53(void);// [0.0, 1.0) +void rnd_defaults(void); #endif // HERCULES_CORE +HPShared struct rnd_interface *rnd; + #endif /* COMMON_RANDOM_H */ |