--- deliantra/server/common/utils.C 2007/01/18 19:32:37 1.40 +++ deliantra/server/common/utils.C 2007/02/07 02:04:46 1.47 @@ -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 @@ -79,45 +93,33 @@ * not the recipient (ie, the poor slob getting hit). [garbled 20010916] */ int -random_roll (int min, int max, const object *op, int goodbad) +random_roll (int r_min, int r_max, const object *op, int goodbad) { - int omin, diff, luck, base, ran; - - omin = min; - diff = max - min + 1; - ((diff > 2) ? (base = 20) : (base = 50)); /* d2 and d3 are corner cases */ + int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ - if (max < 1 || diff < 1) + if (r_max < 1 || r_max < r_min) { - LOG (llevError, "Calling random_roll with min=%d max=%d\n", min, max); - return (min); /* avoids a float exception */ + LOG (llevError, "Calling random_roll with min=%d max=%d\n", r_min, r_max); + return r_min; } - ran = RANDOM (); - - if (op->type != PLAYER) - return ((ran % diff) + min); - - luck = op->stats.luck; - if (RANDOM () % base < MIN (10, abs (luck))) + if (op->type == PLAYER) { - /* we have a winner */ - ((luck > 0) ? (luck = 1) : (luck = -1)); - diff -= luck; - if (diff < 1) - return (omin); /*check again */ - ((goodbad) ? (min += luck) : (diff)); + int luck = op->stats.luck; - return (MAX (omin, MIN (max, (ran % diff) + min))); + if (rndm (base) < min (10, abs (luck))) + { + //TODO: take luck into account + } } - return ((ran % diff) + min); + + return rndm (r_min, r_max); } /* * 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) { @@ -134,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)); @@ -156,6 +157,7 @@ return (MAX (omin, MIN (max, (ran % diff) + min))); } + return ((ran % diff) + min); } @@ -170,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; @@ -187,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++; @@ -196,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 */ @@ -253,7 +253,7 @@ } else { - int mat = op->material; + int mat = op->materials; if (mat & M_PAPER || mat & M_LEATHER @@ -278,20 +278,13 @@ /* convert materialname to materialtype_t */ materialtype_t * -name_to_material (const char *name) +name_to_material (const shstr &name) { - materialtype_t *mt, *nmt; + for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) + if (name == mt->name) + return mt; - mt = NULL; - for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt = nmt->next) - { - if (strcmp (name, nmt->name) == 0) - { - mt = nmt; - break; - } - } - return mt; + return materialt; } /* when doing transmutation of objects, we have to recheck the resistances, @@ -350,9 +343,9 @@ { lmt = NULL; #ifndef NEW_MATERIAL_CODE - for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) + for (mt = materialt; mt && mt->next; mt = mt->next) { - if (op->material & mt->material) + if (op->materials & mt->material) { lmt = mt; break; @@ -360,9 +353,9 @@ } #else - for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) + for (mt = materialt; mt && mt->next; mt = mt->next) { - if (op->material & mt->material && rndm (1, 100) <= mt->chance && + if (op->materials & mt->material && rndm (1, 100) <= mt->chance && difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) { lmt = mt; @@ -390,6 +383,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 ()) @@ -408,7 +402,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 ()) { @@ -569,7 +563,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 ();