--- deliantra/server/common/object.C 2006/09/12 00:53:56 1.33 +++ deliantra/server/common/object.C 2006/09/14 01:12:28 1.41 @@ -34,6 +34,8 @@ #include int nrofallocobjects = 0; +static UUID uuid; +const uint64 UUID_SKIP = 1<<19; object *objects; /* Pointer to the list of used objects */ object *active_objects; /* List of active objects that need to be processed */ @@ -52,6 +54,83 @@ 1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 8 }; +static void +write_uuid (void) +{ + char filename1[MAX_BUF], filename2[MAX_BUF]; + + sprintf (filename1, "%s/uuid", settings.localdir); + sprintf (filename2, "%s/uuid~", settings.localdir); + + FILE *fp; + + if (!(fp = fopen (filename2, "w"))) + { + LOG (llevError, "ERROR: cannot open %s for writing, unable to write UUID!\n", filename2); + return; + } + + fprintf (fp, "<1,%llx>\n", (unsigned long long)uuid.seq + UUID_SKIP * 2); + fclose (fp); + rename (filename2, filename1); +} + +static void +read_uuid (void) +{ + char filename[MAX_BUF]; + + sprintf (filename, "%s/uuid", settings.localdir); + + FILE *fp; + + if (!(fp = fopen (filename, "r"))) + { + if (errno == ENOENT) + { + LOG (llevInfo, "RESET uid to 1\n"); + uuid.seq = 0; + write_uuid (); + return; + } + + LOG (llevError, "FATAL: cannot open %s for reading!\n", filename); + _exit (1); + } + + int version; + unsigned long long uid; + if (2 != fscanf (fp, "<%d,%llx>\n", &version, &uid) || version != 1) + { + LOG (llevError, "FATAL: error reading uid from %s!\n", filename); + _exit (1); + } + + uuid.seq = uid; + write_uuid (); + LOG (llevDebug, "read UID: %lld\n", uid); + fclose (fp); +} + +UUID +gen_uuid () +{ + UUID uid; + + uid.seq = ++uuid.seq; + + if (!(uuid.seq & (UUID_SKIP - 1))) + write_uuid (); + + return uid; +} + +void +init_uuid () +{ + read_uuid (); +} + /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ static int compare_ob_value_lists_one (const object *wants, const object *has) @@ -256,10 +335,13 @@ sum_weight (inv); sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1); } + if (op->type == CONTAINER && op->stats.Str) sum = (sum * (100 - op->stats.Str)) / 100; + if (op->carrying != sum) op->carrying = sum; + return sum; } @@ -418,11 +500,11 @@ object * find_object_name (const char *str) { - const char *name = shstr::find (str); + shstr_cmp str_ (str); object *op; for (op = objects; op != NULL; op = op->next) - if (&op->name == name) + if (op->name == str_) break; return op; @@ -838,7 +920,7 @@ } static unordered_vector mortals; -static std::vector freed; +static std::vector > freed; void object::free_mortals () { @@ -847,12 +929,13 @@ ++i; // further delay freeing else { - freed.push_back (*i);//D - //delete *i; + //printf ("free_mortal(%p,%ld,%ld)\n", *i, pticks, (*i)->count);//D + //freed.push_back (*i);//D + delete *i; mortals.erase (i); } - if (mortals.size())//D + if (mortals.size() && 0)//D LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D } @@ -873,6 +956,7 @@ void object::link () { count = ++ob_count; + uuid = gen_uuid (); prev = 0; next = objects; @@ -885,23 +969,18 @@ void object::unlink () { - count = 0; - - /* Remove this object from the list of used objects */ - if (prev) - { - prev->next = next; - prev = 0; - } - - if (next) - { - next->prev = prev; - next = 0; - } + //count = 0;//D + if (!prev && !next) return;//D if (this == objects) objects = next; + + /* Remove this object from the list of used objects */ + if (prev) prev->next = next; + if (next) next->prev = prev; + + prev = 0; + next = 0; } object *object::create () @@ -946,6 +1025,8 @@ SET_FLAG (this, FLAG_FREED); + //printf ("free(%p,%ld,%ld)\n", this, pticks, count);//D + if (more) { more->free (free_inventory); @@ -1224,7 +1305,6 @@ * If top is NULL, it is calculated. * Returns pointer to object if it succeded in the merge, otherwise NULL */ - object * merge_ob (object *op, object *top) { @@ -2655,7 +2735,8 @@ { LOG (llevError, "Error - Unable to access load object temp file\n"); return NULL; - }; + } + fprintf (tempfile, obstr); fclose (tempfile); @@ -2722,34 +2803,27 @@ const char * get_ob_key_value (const object *op, const char *const key) { - key_value * - link; - const char * - canonical_key; - - canonical_key = shstr::find (key); + key_value *link; + shstr_cmp canonical_key (key); - if (canonical_key == NULL) + if (!canonical_key) { /* 1. There being a field named key on any object * implies there'd be a shared string to find. * 2. Since there isn't, no object has this field. * 3. Therefore, *this* object doesn't have this field. */ - return NULL; + return 0; } /* This is copied from get_ob_key_link() above - * only 4 lines, and saves the function call overhead. */ - for (link = op->key_values; link != NULL; link = link->next) - { - if (link->key == canonical_key) - { - return link->value; - } - } - return NULL; + for (link = op->key_values; link; link = link->next) + if (link->key == canonical_key) + return link->value; + + return 0; } @@ -2845,13 +2919,53 @@ return set_ob_key_value_s (op, key_, value, add_key); } -void -object::deep_iterator::next () +object::depth_iterator::depth_iterator (object *container) +: iterator_base (container) { - if (item->inv) + while (item->inv) item = item->inv; - else if (item->below) - item = item->below; +} + +void +object::depth_iterator::next () +{ + if (item->below) + { + item = item->below; + + while (item->inv) + item = item->inv; + } else - item = item->env->below; + item = item->env; } + +// return a suitable string describing an objetc in enough detail to find it +const char * +object::debug_desc (char *info) const +{ + char info2[256 * 3]; + char *p = info; + + p += snprintf (p, 256, "%d=\"%s%s%s\"", + count, + &name, + title ? " " : "", + title ? (const char *)title : ""); + + if (env) + p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); + + if (map) + p += snprintf (p, 256, "(on %s@%d+%d)", map->path, x, y); + + return info; +} + +const char * +object::debug_desc () const +{ + static char info[256 * 3]; + return debug_desc (info); +} +