--- deliantra/server/common/utils.C 2006/09/10 16:00:23 1.9 +++ deliantra/server/common/utils.C 2006/12/25 14:54:44 1.31 @@ -1,9 +1,3 @@ - -/* - * static char *rcsid_utils_c = - * "$Id: utils.C,v 1.9 2006/09/10 16:00:23 root Exp $"; - */ - /* CrossFire, A Multiplayer game for X-windows @@ -24,7 +18,7 @@ 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 + The authors can be reached via e-mail at */ /* @@ -35,6 +29,8 @@ #include #include +#include +#include #include /* @@ -105,11 +101,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 */ } @@ -208,7 +200,7 @@ /* decay and destroy persihable items in a map */ void -decay_objects (mapstruct *m) +decay_objects (maptile *m) { int x, y, destroy; object *op, *otmp; @@ -216,27 +208,31 @@ if (m->unique) return; - 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) + for (x = 0; x < m->width; x++) + for (y = 0; y < m->height; 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)) + 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 ()) continue; + /* otherwise, we decay and destroy */ - if (IS_WEAPON (op)) + if (op->is_weapon ()) { op->stats.dam--; if (op->stats.dam < 0) destroy = 1; } - else if (IS_ARMOR (op)) + else if (op->is_armor ()) { op->stats.ac--; if (op->stats.ac < 0) @@ -261,15 +257,12 @@ 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) + if (op->material & M_ICE && m->temp > 32) destroy = 1; } /* adjust overall chance below */ if (destroy && rndm (0, 1)) - { - remove_ob (op); - free_object (op); - } + op->destroy (); } } @@ -308,7 +301,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 +357,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 +375,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 +383,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 +401,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; @@ -533,6 +526,7 @@ /*trim all trailing commas, spaces etc. */ for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) tmp[i] = '\0'; + strcat (tmp, "."); p = strrchr (tmp, ','); @@ -546,37 +540,63 @@ } else strcpy (input, tmp); + return; } -void * - zero_initialised::operator -new (size_t s) +///////////////////////////////////////////////////////////////////////////// + +void *salloc_ (int n) throw (std::bad_alloc) { - //return calloc (1, s); - return g_slice_alloc0 (s); + void *ptr = g_slice_alloc (n); + + if (!ptr) + throw std::bad_alloc (); + + return ptr; } -void * - zero_initialised::operator -new[] (size_t s) +void *salloc_ (int n, void *src) throw (std::bad_alloc) { - //return calloc (1, s); - return g_slice_alloc0 (s); + void *ptr = salloc_ (n); + + if (src) + memcpy (ptr, src, n); + else + memset (ptr, 0, n); + + return ptr; } -void - zero_initialised::operator -delete (void *p, size_t s) +void assign (char *dst, const char *src, int maxlen) { - //free (p); return; - g_slice_free1 (s, p); + if (!src) + src = ""; + + int len = strlen (src); + + if (len >= maxlen - 1) + { + if (maxlen <= 4) + { + memset (dst, '.', maxlen - 1); + dst [maxlen - 1] = 0; + } + else + { + memcpy (dst, src, maxlen - 4); + memcpy (dst + maxlen - 4, "...", 4); + } + } + else + memcpy (dst, src, len + 1); } -void - zero_initialised::operator -delete[] (void *p, size_t s) +tstamp now () { - //free (p); return; - g_slice_free1 (s, p); + struct timeval tv; + + gettimeofday (&tv, 0); + return tstamp (tv.tv_sec) + tstamp (tv.tv_usec) * tstamp (1e-6); } +