ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
(Generate patch)

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.48 by root, Mon Apr 16 11:09:30 2007 UTC vs.
Revision 1.52 by root, Tue Apr 17 10:06:32 2007 UTC

311overwrite (archetype *at, object *op) 311overwrite (archetype *at, object *op)
312{ 312{
313 at->clone = *op; 313 at->clone = *op;
314 314
315 at->clone.arch = at; 315 at->clone.arch = at;
316 //TODO: inv et al. 316 at->clone.inv = op->inv; op->inv = 0;
317}
318 317
319archetype * 318 op->destroy ();
319}
320
321bool
320archetype::read (object_thawer &f) 322archetype::load (object_thawer &f)
321{ 323{
322 assert (f.kw == KW_object); 324 assert (f.kw == KW_object);
323 325
326 typedef std::pair<archetype *, object *> part;
327 std::vector<part> parts;
328
329 coroapi::cede_to_tick_every (100);
330
331 for (;;)
332 {
324 archetype *head = get (f.get_str ()); 333 archetype *at = get (f.get_str ());
325 unlink (head);
326 object *op = object::create (); 334 object *op = object::create ();
327 335
328 if (!op->parse_kv (f)) 336 if (!op->parse_kv (f))
329 { 337 goto fail;
330 op->destroy (true);
331 // leak head
332 return 0;
333 }
334 338
335 overwrite (head, op); 339 parts.push_back (std::make_pair (at, op));
336 op->destroy ();
337 340
338 for (archetype *prev = head; f.kw == KW_more; ) 341 if (f.kw != KW_more)
339 { 342 break;
343
340 f.next (); 344 f.next ();
341 assert (f.kw == KW_object); 345 assert (f.kw == KW_object);
346 }
342 347
343 archetype *more = get (f.get_str ()); 348 {
344 unlink (more); 349 archetype *head = parts.front ().first;
345 object *op = object::create ();
346 350
347 if (!op->parse_kv (f)) 351 // check that all archetypes belong to the same object or are heads
352 for (auto (p, parts.begin ()); p != parts.end (); ++p)
353 {
354 archetype *at = p->first;
355
356 if (at->head != head && at->head)
348 { 357 {
349 op->destroy (true); 358 LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name);
350 // leak head more* 359 goto fail;
351 return 0;
352 } 360 }
353 361
354 overwrite (more, op); 362 if (at->next && at != head)
355 op->destroy (); 363 {
356 364 LOG (llevError, "%s: unable to overwrite foreign head archetype with non-head archetype\n", &at->name);
357 if (more->clone.x > head->tail_x) head->tail_x = more->clone.x; 365 goto fail;
358 if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; 366 }
359
360 more->head = head;
361 more->clone.head = &head->clone;
362 prev->more = more;
363 prev->clone.more = &more->clone;
364
365 prev = more;
366 } 367 }
367 368
368 if (!head->next) 369 // sever chain of existing head object
370 for (archetype *more, *at = head; at; at = more)
369 { 371 {
372 more = at->more;
373
374 at->head = 0;
375 at->more = 0;
376 }
377
378 // replace/update head
379 overwrite (head, parts.front ().second);
380 head->tail_x = 0;
381 head->tail_y = 0;
382
383 // link into list of heads, if not already there
384 if (!head->linked)
385 {
386 head->linked = true;
370 head->next = first_archetype; 387 head->next = first_archetype;
371 first_archetype = head; 388 first_archetype = head;
372 } 389 }
373 390
391 // reassemble new chain
392 archetype *prev = head;
393 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
394 {
395 archetype *at = p->first;
396 overwrite (at, p->second);
397
398 if (at->clone.x > head->tail_x) head->tail_x = at->clone.x;
399 if (at->clone.y > head->tail_y) head->tail_y = at->clone.y;
400
401 at->head = head;
402 at->clone.head = &head->clone;
403 prev->more = at;
404 prev->clone.more = &at->clone;
405
406 prev = at;
407 }
408 }
409
374 return head; 410 return true;
411
412fail:
413 for (auto (p, parts.begin ()); p != parts.end (); ++p)
414 p->second->destroy (true);
415
416 return false;
375} 417}
376 418
377/* 419/*
378 * Reads/parses the archetype-file, and copies into a linked list 420 * Reads/parses the archetype-file, and copies into a linked list
379 * of archetype-structures. 421 * of archetype-structures.
385 { 427 {
386 switch (f.kw) 428 switch (f.kw)
387 { 429 {
388 case KW_object: 430 case KW_object:
389 loading_arch = true; 431 loading_arch = true;
390 if (!archetype::read (f)) 432 if (!archetype::load (f))
391 { 433 {
392 loading_arch = false; 434 loading_arch = false;
393 return false; 435 return false;
394 } 436 }
395 437
418{ 460{
419 object_thawer f (filename); 461 object_thawer f (filename);
420 462
421 f.next (); 463 f.next ();
422 464
423 // make sure the next - long - step is only done after a tick
424 coroapi::wait_for_tick_begin ();
425
426 if (!load_archetypes (f)) 465 if (!load_archetypes (f))
427 return false; 466 return false;
428 467
429 warn_archetypes = 1; 468 warn_archetypes = 1;
430 469
431 empty_archetype = archetype::find ("empty_archetype"); 470 empty_archetype = archetype::find ("empty_archetype");
432 if (!empty_archetype) 471 if (!empty_archetype)
433 return false; 472 return false;
434
435 coroapi::cede ();
436 473
437 return true; 474 return true;
438} 475}
439 476
440/* 477/*
491 528
492 return arch_to_object (at); 529 return arch_to_object (at);
493} 530}
494 531
495/* 532/*
496 * Hash-function used by the arch-hashtable.
497 */
498
499unsigned long
500hasharch (const char *str, int tablesize)
501{
502 unsigned long hash = 0;
503 unsigned int i = 0;
504 const char *p;
505
506 /* use the one-at-a-time hash function, which supposedly is
507 * better than the djb2-like one used by perl5.005, but
508 * certainly is better then the bug used here before.
509 * see http://burtleburtle.net/bob/hash/doobs.html
510 */
511 for (p = str; i < MAXSTRING && *p; p++, i++)
512 {
513 hash += *p;
514 hash += hash << 10;
515 hash ^= hash >> 6;
516 }
517
518 hash += hash << 3;
519 hash ^= hash >> 11;
520 hash += hash << 15;
521
522 return hash % tablesize;
523}
524
525/*
526 * Finds, using the hashtable, which archetype matches the given name. 533 * Finds, using the hashtable, which archetype matches the given name.
527 * returns a pointer to the found archetype, otherwise NULL. 534 * returns a pointer to the found archetype, otherwise NULL.
528 */ 535 */
529archetype * 536archetype *
530archetype::find (const char *name) 537archetype::find (const char *name)
531{ 538{
532 if (!name) 539 if (!name)
533 return 0; 540 return 0;
534 541
535 AUTODECL (i, ht.find (name)); 542 auto (i, ht.find (name));
536 543
537 if (i == ht.end ()) 544 if (i == ht.end ())
538 return 0; 545 return 0;
539 else 546 else
540 return i->second; 547 return i->second;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines