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.53 by root, Tue Apr 17 18:40:31 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 op->destroy ();
317} 319}
318 320
319archetype * 321archetype *
320archetype::read (object_thawer &f) 322archetype::read (object_thawer &f)
321{ 323{
322 assert (f.kw == KW_object); 324 assert (f.kw == KW_object);
323 325
326 loading_arch = true; // hack to tell parse_kv et al. to behave
327
328 typedef std::pair<archetype *, object *> part;
329 std::vector<part> parts;
330
331 coroapi::cede_to_tick_every (100);
332
333 for (;;)
334 {
324 archetype *head = get (f.get_str ()); 335 archetype *at = get (f.get_str ());
325 unlink (head);
326 object *op = object::create (); 336 object *op = object::create ();
327 337
328 if (!op->parse_kv (f)) 338 if (!op->parse_kv (f))
329 { 339 goto fail;
330 op->destroy (true);
331 // leak head
332 return 0;
333 }
334 340
335 overwrite (head, op); 341 parts.push_back (std::make_pair (at, op));
336 op->destroy ();
337 342
338 for (archetype *prev = head; f.kw == KW_more; ) 343 if (f.kw != KW_more)
339 { 344 break;
345
340 f.next (); 346 f.next ();
341 assert (f.kw == KW_object); 347 assert (f.kw == KW_object);
348 }
342 349
343 archetype *more = get (f.get_str ()); 350 {
344 unlink (more); 351 archetype *head = parts.front ().first;
345 object *op = object::create ();
346 352
347 if (!op->parse_kv (f)) 353 // check that all archetypes belong to the same object or are heads
354 for (auto (p, parts.begin ()); p != parts.end (); ++p)
355 {
356 archetype *at = p->first;
357
358 if (at->head != head && at->head)
348 { 359 {
349 op->destroy (true); 360 LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name);
350 // leak head more* 361 goto fail;
351 return 0;
352 } 362 }
353 363
354 overwrite (more, op); 364 if (at->next && at != head)
355 op->destroy (); 365 {
356 366 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; 367 goto fail;
358 if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; 368 }
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 } 369 }
367 370
368 if (!head->next) 371 // sever chain of existing head object
372 for (archetype *more, *at = head; at; at = more)
369 { 373 {
374 more = at->more;
375
376 at->head = 0;
377 at->more = 0;
378 }
379
380 // replace/update head
381 overwrite (head, parts.front ().second);
382 head->tail_x = 0;
383 head->tail_y = 0;
384
385 // link into list of heads, if not already there
386 if (!head->linked)
387 {
388 head->linked = true;
370 head->next = first_archetype; 389 head->next = first_archetype;
371 first_archetype = head; 390 first_archetype = head;
372 } 391 }
373 392
393 // reassemble new chain
394 archetype *prev = head;
395 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
396 {
397 archetype *at = p->first;
398 overwrite (at, p->second);
399
400 if (at->clone.x > head->tail_x) head->tail_x = at->clone.x;
401 if (at->clone.y > head->tail_y) head->tail_y = at->clone.y;
402
403 at->head = head;
404 at->clone.head = &head->clone;
405 prev->more = at;
406 prev->clone.more = &at->clone;
407
408 prev = at;
409 }
410
411 loading_arch = false;
374 return head; 412 return head;
375} 413 }
376 414
377/* 415fail:
378 * Reads/parses the archetype-file, and copies into a linked list 416 for (auto (p, parts.begin ()); p != parts.end (); ++p)
379 * of archetype-structures. 417 p->second->destroy (true);
380 */
381static bool
382load_archetypes (object_thawer &f)
383{
384 for (;;)
385 {
386 switch (f.kw)
387 {
388 case KW_object:
389 loading_arch = true;
390 if (!archetype::read (f))
391 {
392 loading_arch = false;
393 return false;
394 }
395 418
396 loading_arch = false; 419 loading_arch = false;
397 continue; 420 return 0;
398
399 case KW_EOF:
400 return true;
401
402 default:
403 if (!f.parse_error ("archetypes file"))
404 return false;
405 }
406
407 f.next ();
408 }
409} 421}
410 422
411/* 423/*
412 * First initialises the archtype hash-table (init_archetable()). 424 * Initialize global archtype pointers:
413 * Reads and parses the archetype file (with the first and second-pass
414 * functions).
415 */ 425 */
416bool 426void
417load_archetype_file (const char *filename) 427init_archetype_pointers ()
418{ 428{
419 object_thawer f (filename); 429 ring_arch = archetype::find ("ring");
420 430 amulet_arch = archetype::find ("amulet");
421 f.next (); 431 staff_arch = archetype::find ("staff");
422 432 crown_arch = archetype::find ("crown");
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))
427 return false;
428
429 warn_archetypes = 1;
430
431 empty_archetype = archetype::find ("empty_archetype"); 433 empty_archetype = archetype::find ("empty_archetype");
432 if (!empty_archetype)
433 return false;
434
435 coroapi::cede ();
436
437 return true;
438} 434}
439 435
440/* 436/*
441 * Creates and returns a new object which is a copy of the given archetype. 437 * Creates and returns a new object which is a copy of the given archetype.
442 * This function returns NULL on failure. 438 * This function returns NULL on failure.
444object * 440object *
445arch_to_object (archetype *at) 441arch_to_object (archetype *at)
446{ 442{
447 if (!at) 443 if (!at)
448 { 444 {
449 if (warn_archetypes)
450 LOG (llevError, "Couldn't find archetype.\n"); 445 LOG (llevError, "Couldn't find archetype.\n");
451
452 return NULL; 446 return 0;
453 } 447 }
454 448
455 object *op = at->clone.clone (); 449 object *op = at->clone.clone ();
456 op->arch = at; 450 op->arch = at;
457 op->instantiate (); 451 op->instantiate ();
452
458 return op; 453 return op;
459} 454}
460 455
461/* 456/*
462 * Creates an object. This function is called by get_archetype() 457 * Creates an object. This function is called by get_archetype()
491 486
492 return arch_to_object (at); 487 return arch_to_object (at);
493} 488}
494 489
495/* 490/*
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. 491 * Finds, using the hashtable, which archetype matches the given name.
527 * returns a pointer to the found archetype, otherwise NULL. 492 * returns a pointer to the found archetype, otherwise NULL.
528 */ 493 */
529archetype * 494archetype *
530archetype::find (const char *name) 495archetype::find (const char *name)
531{ 496{
532 if (!name) 497 if (!name)
533 return 0; 498 return 0;
534 499
535 AUTODECL (i, ht.find (name)); 500 auto (i, ht.find (name));
536 501
537 if (i == ht.end ()) 502 if (i == ht.end ())
538 return 0; 503 return 0;
539 else 504 else
540 return i->second; 505 return i->second;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines