--- deliantra/server/common/utils.C 2007/01/18 22:19:59 1.41 +++ deliantra/server/common/utils.C 2007/02/05 01:47:22 1.46 @@ -39,14 +39,15 @@ #include -rand_gen rndm (time (0)); +rand_gen rndm; -tausworthe_random_generator::tausworthe_random_generator (uint32_t seed) +void +tausworthe_random_generator::seed (uint32_t seed) { - state [0] = max ( 2, seed * 69069U); - state [1] = max ( 8, state [0] * 69069U); - state [2] = max ( 16, state [1] * 69069U); - state [3] = max (128, state [2] * 69069U); + state [0] = max ( 2U, seed * 69069U); + state [1] = max ( 8U, state [0] * 69069U); + state [2] = max ( 16U, state [1] * 69069U); + state [3] = max (128U, state [2] * 69069U); for (int i = 11; --i; ) operator ()(); @@ -63,6 +64,19 @@ return state [0] ^ state [1] ^ state [2] ^ state [3]; } +uint32_t +tausworthe_random_generator::get_range (uint32_t r_max) +{ + return next () % r_max; +} + +// return a number within (min .. max) +int +tausworthe_random_generator::get_range (int r_min, int r_max) +{ + return r_min + (*this) (max (r_max - r_min + 1, 1)); +} + /* * The random functions here take luck into account when rolling random * dice or numbers. This function has less of an impact the larger the @@ -106,7 +120,6 @@ * This is a 64 bit version of random_roll above. This is needed * for exp loss calculations for players changing religions. */ - sint64 random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) { @@ -123,18 +136,17 @@ return (min); /* avoids a float exception */ } - /* Don't know of a portable call to get 64 bit random values. - * So make a call to get two 32 bit random numbers, and just to - * a little byteshifting. Do make sure the first one is only - * 32 bit, so we don't get skewed results + /* + * Make a call to get two 32 bit unsigned random numbers, and just to + * a little bitshifting. */ - ran = (RANDOM () & 0xffffffff) | ((sint64) RANDOM () << 32); + ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); if (op->type != PLAYER) return ((ran % diff) + min); luck = op->stats.luck; - if (RANDOM () % base < MIN (10, abs (luck))) + if (rndm (base) < MIN (10, abs (luck))) { /* we have a winner */ ((luck > 0) ? (luck = 1) : (luck = -1)); @@ -145,6 +157,7 @@ return (MAX (omin, MIN (max, (ran % diff) + min))); } + return ((ran % diff) + min); } @@ -159,7 +172,7 @@ int die_roll (int num, int size, const object *op, int goodbad) { - int min, diff, luck, total, i, gotlucky, base, ran; + int min, diff, luck, total, i, gotlucky, base; diff = size; min = 1; @@ -176,7 +189,7 @@ for (i = 0; i < num; i++) { - if (RANDOM () % base < MIN (10, abs (luck)) && !gotlucky) + if (rndm (base) < MIN (10, abs (luck)) && !gotlucky) { /* we have a winner */ gotlucky++; @@ -185,15 +198,13 @@ if (diff < 1) return (num); /*check again */ ((goodbad) ? (min += luck) : (diff)); - ran = RANDOM (); - total += MAX (1, MIN (size, (ran % diff) + min)); + total += MAX (1, MIN (size, rndm (diff) + min)); } else - { - total += RANDOM () % size + 1; - } + total += rndm (size) + 1; } - return (total); + + return total; } /* decay and destroy perishable items in a map */ @@ -379,6 +390,7 @@ if (op->stats.dam < 1) op->stats.dam = 1; } + if (op->stats.sp && op->type == BOW) op->stats.sp += lmt->sp; if (op->stats.wc && op->is_weapon ()) @@ -397,7 +409,7 @@ op->resist[j] = -100; } } - op->materialname = add_string (lmt->name); + op->materialname = lmt->name; /* dont make it unstackable if it doesn't need to be */ if (op->is_weapon () || op->is_armor ()) { @@ -558,7 +570,11 @@ void *salloc_ (int n) throw (std::bad_alloc) { +#ifdef PREFER_MALLOC + void *ptr = malloc (n); +#else void *ptr = g_slice_alloc (n); +#endif if (!ptr) throw std::bad_alloc ();