--- deliantra/server/common/utils.C 2006/12/12 20:53:02 1.21 +++ deliantra/server/common/utils.C 2007/01/15 02:42:15 1.38 @@ -1,6 +1,7 @@ /* 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 @@ -25,6 +26,13 @@ * General convenience functions for crossfire. */ +#include +#include +#include +#include +#include +#include + #include #include #include @@ -195,72 +203,72 @@ return (RANDOM () % diff + min); } -/* decay and destroy persihable items in a map */ - +/* decay and destroy perishable items in a map */ void -decay_objects (maptile *m) +maptile::decay_objects () { - int x, y, destroy; - object *op, *otmp; - - if (m->unique) + if (!spaces) 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) - { - 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)) - { - op->remove (); - op->destroy (0); - } - } + for (mapspace *ms = spaces + size (); ms-- > spaces; ) + for (object *above, *op = ms->bot; op; op = above) + { + above = op->above; + + bool destroy = 0; + + // do not decay anything above unique floor tiles (yet :) + 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) + || 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 */ @@ -298,7 +306,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); @@ -354,7 +362,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; } } @@ -372,7 +380,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) @@ -380,9 +388,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; @@ -398,7 +406,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; @@ -543,31 +551,38 @@ ///////////////////////////////////////////////////////////////////////////// -#if 0 -refcounted *refcounted::rc_first; - -refcounted::refcounted () +void +fork_abort (const char *msg) { - refcnt = 0; - rc_next = rc_first; - rc_first = this; + if (!fork ()) + { + signal (SIGABRT, SIG_DFL); + abort (); + } + + LOG (llevError, "fork abort: %s\n", msg); } -refcounted::~refcounted () +void *salloc_ (int n) throw (std::bad_alloc) { - assert (!rc_next); - assert (!refcnt); + void *ptr = g_slice_alloc (n); + + if (!ptr) + throw std::bad_alloc (); + + return ptr; } -#endif -void *alloc (int s) throw (std::bad_alloc) +void *salloc_ (int n, void *src) throw (std::bad_alloc) { - void *p = g_slice_alloc (s); + void *ptr = salloc_ (n); - if (!p) - throw std::bad_alloc (); + if (src) + memcpy (ptr, src, n); + else + memset (ptr, 0, n); - return p; + return ptr; } void assign (char *dst, const char *src, int maxlen) @@ -594,4 +609,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; +}