--- deliantra/server/common/utils.C 2007/01/06 14:42:29 1.36 +++ deliantra/server/common/utils.C 2007/01/25 03:54:44 1.44 @@ -1,39 +1,82 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team - Copyright (C) 2002 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at -*/ + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2002 Mark Wedel & Crossfire Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The authors can be reached via e-mail at + */ /* * General convenience functions for crossfire. */ +#include +#include +#include +#include +#include +#include + #include #include #include -#include -#include #include +rand_gen rndm; + +void +tausworthe_random_generator::seed (uint32_t seed) +{ + 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 ()(); +} + +uint32_t +tausworthe_random_generator::next () +{ + state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U); + state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); + state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); + state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); + + 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 @@ -49,40 +92,28 @@ * Generally, op should be the player/caster/hitter requesting the roll, * 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; + int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ - omin = min; - diff = max - min + 1; - ((diff > 2) ? (base = 20) : (base = 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); } /* @@ -179,25 +210,6 @@ return (total); } -/* - * Another convenience function. Returns a number between min and max. - * It is suggested one use these functions rather than RANDOM()%, as it - * would appear that a number of off-by-one-errors exist due to improper - * use of %. This should also prevent SIGFPE. - */ - -int -rndm (int min, int max) -{ - int diff; - - diff = max - min + 1; - if (max < 1 || diff < 1) - return (min); - - return (RANDOM () % diff + min); -} - /* decay and destroy perishable items in a map */ void maptile::decay_objects () @@ -546,9 +558,25 @@ ///////////////////////////////////////////////////////////////////////////// +void +fork_abort (const char *msg) +{ + if (!fork ()) + { + signal (SIGABRT, SIG_DFL); + abort (); + } + + LOG (llevError, "fork abort: %s\n", msg); +} + 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 ();