--- deliantra/server/common/object.C 2006/09/12 19:20:06 1.36 +++ deliantra/server/common/object.C 2006/09/14 01:19:47 1.42 @@ -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; } @@ -838,7 +920,6 @@ } static unordered_vector mortals; -static std::vector freed; void object::free_mortals () { @@ -847,13 +928,17 @@ ++i; // further delay freeing else { - freed.push_back (*i);//D - //delete *i; + delete *i; mortals.erase (i); } - if (mortals.size() && 0)//D - LOG (llevDebug, "%d objects in mortal queue\n", mortals.size());//D + static int lastmortals = 0;//D + + if (mortals.size() != lastmortals)//D + { + lastmortals = mortals.size ();//D + LOG (llevDebug, "%d objects in mortal queue\n", lastmortals);//D + } } object::object () @@ -873,6 +958,7 @@ void object::link () { count = ++ob_count; + uuid = gen_uuid (); prev = 0; next = objects; @@ -885,40 +971,23 @@ 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 () { - object *op; - - if (freed.empty ()) - op = new object; - else - { - // highly annoying, but the only way to get it stable right now - op = freed.back (); - freed.pop_back (); - op->~object (); - new ((void *) op) object; - } - + object *op = new object; op->link (); return op; } @@ -1224,7 +1293,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 +2723,8 @@ { LOG (llevError, "Error - Unable to access load object temp file\n"); return NULL; - }; + } + fprintf (tempfile, obstr); fclose (tempfile);