--- deliantra/server/common/utils.C 2006/09/12 20:55:40 1.12 +++ deliantra/server/common/utils.C 2007/01/19 15:15:49 1.42 @@ -1,42 +1,82 @@ - /* - * static char *rcsid_utils_c = - * "$Id: utils.C,v 1.12 2006/09/12 20:55:40 root Exp $"; + * 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) 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-devel@real-time.com -*/ - -/* * General convenience functions for crossfire. */ +#include +#include +#include +#include +#include +#include + #include #include #include #include +rand_gen rndm; + +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); + + 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 @@ -52,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; - - 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); } /* @@ -105,11 +133,7 @@ if (max < 1 || diff < 1) { -#ifndef WIN32 - LOG (llevError, "Calling random_roll with min=%lld max=%lld\n", min, max); -#else - LOG (llevError, "Calling random_roll with min=%I64d max=%I64d\n", min, max); -#endif + LOG (llevError, "Calling random_roll with min=%" PRId64 " max=%" PRId64 "\n", min, max); return (min); /* avoids a float exception */ } @@ -186,91 +210,72 @@ 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) +/* decay and destroy perishable items in a map */ +void +maptile::decay_objects () { - int diff; - - diff = max - min + 1; - if (max < 1 || diff < 1) - return (min); - - return (RANDOM () % diff + min); -} + if (!spaces) + return; -/* decay and destroy persihable items in a map */ + for (mapspace *ms = spaces + size (); ms-- > spaces; ) + for (object *above, *op = ms->bot; op; op = above) + { + above = op->above; -void -decay_objects (mapstruct *m) -{ - int x, y, destroy; - object *op, *otmp; + bool destroy = 0; - if (m->unique) - return; + // do not decay anything above unique floor tiles (yet :) + if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) + break; - for (x = 0; x < MAP_WIDTH (m); x++) - for (y = 0; y < MAP_HEIGHT (m); y++) - for (op = get_map_ob (m, x, y); op; op = otmp) - { - destroy = 0; - otmp = op->above; - if (QUERY_FLAG (op, FLAG_IS_FLOOR) && QUERY_FLAG (op, FLAG_UNIQUE)) - break; - if (QUERY_FLAG (op, FLAG_IS_FLOOR) || - QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) || - QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) || - QUERY_FLAG (op, FLAG_UNIQUE) || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) || QUERY_FLAG (op, FLAG_UNPAID) || IS_LIVE (op)) - continue; - /* otherwise, we decay and destroy */ - if (IS_WEAPON (op)) - { - op->stats.dam--; - if (op->stats.dam < 0) - destroy = 1; - } - else if (IS_ARMOR (op)) - { - op->stats.ac--; - if (op->stats.ac < 0) - destroy = 1; - } - else if (op->type == FOOD) - { - op->stats.food -= rndm (5, 20); - if (op->stats.food < 0) - destroy = 1; - } - else - { - if (op->material & M_PAPER || op->material & M_LEATHER || - op->material & M_WOOD || op->material & M_ORGANIC || op->material & M_CLOTH || op->material & M_LIQUID) - destroy = 1; - if (op->material & M_IRON && rndm (1, 5) == 1) - destroy = 1; - if (op->material & M_GLASS && rndm (1, 2) == 1) - destroy = 1; - if ((op->material & M_STONE || op->material & M_ADAMANT) && rndm (1, 10) == 1) - destroy = 1; - if ((op->material & M_SOFT_METAL || op->material & M_BONE) && rndm (1, 3) == 1) - destroy = 1; - if (op->material & M_ICE && MAP_TEMP (m) > 32) - destroy = 1; - } - /* adjust overall chance below */ - if (destroy && rndm (0, 1)) - { - remove_ob (op); - free_object (op); - } - } + if (QUERY_FLAG (op, FLAG_IS_FLOOR) + || QUERY_FLAG (op, FLAG_OBJ_ORIGINAL) + || QUERY_FLAG (op, FLAG_OBJ_SAVE_ON_OVL) + || QUERY_FLAG (op, FLAG_UNIQUE) + || QUERY_FLAG (op, FLAG_OVERLAY_FLOOR) + || QUERY_FLAG (op, FLAG_UNPAID) + || op->is_alive ()) + ; // do not decay + else if (op->is_weapon ()) + { + op->stats.dam--; + if (op->stats.dam < 0) + destroy = 1; + } + else if (op->is_armor ()) + { + op->stats.ac--; + if (op->stats.ac < 0) + destroy = 1; + } + else if (op->type == FOOD) + { + op->stats.food -= rndm (5, 20); + if (op->stats.food < 0) + destroy = 1; + } + else + { + int mat = op->material; + + if (mat & M_PAPER + || mat & M_LEATHER + || mat & M_WOOD + || mat & M_ORGANIC + || mat & M_CLOTH + || mat & M_LIQUID + || (mat & M_IRON && rndm (1, 5) == 1) + || (mat & M_GLASS && rndm (1, 2) == 1) + || ((mat & M_STONE || mat & M_ADAMANT) && rndm (1, 10) == 1) + || ((mat & M_SOFT_METAL || mat & M_BONE) && rndm (1, 3) == 1) + || (mat & M_ICE && temp > 32)) + destroy = 1; + } + + /* adjust overall chance below */ + if (destroy && rndm (0, 1)) + op->destroy (); + } } /* convert materialname to materialtype_t */ @@ -308,7 +313,7 @@ if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) return; - if (!IS_ARMOR (op)) + if (!op->is_armor ()) return; mt = name_to_material (op->materialname); @@ -364,7 +369,7 @@ difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) { lmt = mt; - if (!(IS_WEAPON (op) || IS_ARMOR (op))) + if (!(op->is_weapon () || op->is_armor ())) break; } } @@ -382,7 +387,7 @@ return; #else - if (op->stats.dam && IS_WEAPON (op)) + if (op->stats.dam && op->is_weapon ()) { op->stats.dam += lmt->damage; if (op->stats.dam < 1) @@ -390,9 +395,9 @@ } if (op->stats.sp && op->type == BOW) op->stats.sp += lmt->sp; - if (op->stats.wc && IS_WEAPON (op)) + if (op->stats.wc && op->is_weapon ()) op->stats.wc += lmt->wc; - if (IS_ARMOR (op)) + if (op->is_armor ()) { if (op->stats.ac) op->stats.ac += lmt->ac; @@ -408,7 +413,7 @@ } op->materialname = add_string (lmt->name); /* dont make it unstackable if it doesn't need to be */ - if (IS_WEAPON (op) || IS_ARMOR (op)) + if (op->is_weapon () || op->is_armor ()) { op->weight = (op->weight * lmt->weight) / 100; op->value = (op->value * lmt->value) / 100; @@ -551,18 +556,40 @@ return; } -void *alloc (int s) throw (std::bad_alloc) +///////////////////////////////////////////////////////////////////////////// + +void +fork_abort (const char *msg) { - if (s) - return g_slice_alloc (s); - else - return 0; + if (!fork ()) + { + signal (SIGABRT, SIG_DFL); + abort (); + } + + LOG (llevError, "fork abort: %s\n", msg); } -void dealloc (void *p, int s) throw () +void *salloc_ (int n) throw (std::bad_alloc) { - if (s) - g_slice_free1 (s, p); + void *ptr = g_slice_alloc (n); + + if (!ptr) + throw std::bad_alloc (); + + return ptr; +} + +void *salloc_ (int n, void *src) throw (std::bad_alloc) +{ + void *ptr = salloc_ (n); + + if (src) + memcpy (ptr, src, n); + else + memset (ptr, 0, n); + + return ptr; } void assign (char *dst, const char *src, int maxlen) @@ -589,3 +616,21 @@ memcpy (dst, src, len + 1); } +tstamp now () +{ + struct timeval tv; + + gettimeofday (&tv, 0); + return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); +} + +int +similar_direction (int a, int b) +{ + if (!a || !b) + return 0; + + int diff = (b - a) & 7; + return diff <= 1 || diff >= 7; +} +