--- deliantra/server/common/utils.C 2007/01/19 15:15:49 1.42 +++ deliantra/server/common/utils.C 2007/02/07 02:04:46 1.47 @@ -44,10 +44,10 @@ 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 ()(); @@ -120,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) { @@ -137,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)); @@ -159,6 +157,7 @@ return (MAX (omin, MIN (max, (ran % diff) + min))); } + return ((ran % diff) + min); } @@ -173,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; @@ -190,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++; @@ -199,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 */ @@ -256,7 +253,7 @@ } else { - int mat = op->material; + int mat = op->materials; if (mat & M_PAPER || mat & M_LEATHER @@ -281,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, @@ -353,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; @@ -363,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; @@ -393,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 ()) @@ -411,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 ()) { @@ -572,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 ();