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.46 by root, Mon Apr 16 06:23:39 2007 UTC vs.
Revision 1.58 by root, Sat Apr 21 11:55:36 2007 UTC

275 } 275 }
276 276
277 return 0; 277 return 0;
278} 278}
279 279
280void
281init_archetypes (void)
282{
283 load_archetypes ();
284
285 empty_archetype = archetype::find ("empty_archetype");
286}
287
288archetype::archetype () 280archetype::archetype ()
289{ 281{
290 clone.arch = this; 282 clone.arch = this;
291 283
292 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ 284 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */
317// dire hack, need to rationalise 309// dire hack, need to rationalise
318void 310void
319overwrite (archetype *at, object *op) 311overwrite (archetype *at, object *op)
320{ 312{
321 at->clone = *op; 313 at->clone = *op;
322
323 at->clone.arch = at; 314 at->clone.arch = at;
324 //TODO: inv et al. 315
316 at->clone.inv = op->inv; op->inv = 0;
317
318 op->destroy ();
325} 319}
326 320
327archetype * 321archetype *
328archetype::read (object_thawer &f) 322archetype::read (object_thawer &f)
329{ 323{
330 assert (f.kw == KW_object); 324 assert (f.kw == KW_object);
331 325
332 archetype *head = get (f.get_str ()); 326 loading_arch = true; // hack to tell parse_kv et al. to behave
333 unlink (head); 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 {
334 object *op = object::create (); 335 object *op = object::create ();
335 336 archetype *at = get (f.get_str ());
336 if (!op->parse_kv (f)) 337 f.get (op->name);
337 {
338 op->destroy (true);
339 // leak head
340 return 0;
341 }
342
343 overwrite (head, op);
344 op->destroy ();
345
346 for (archetype *prev = head; f.kw == KW_more; )
347 {
348 f.next (); 338 f.next ();
349 assert (f.kw == KW_object);
350 339
340#if 0
341 if (f.kw == KW_inherit)
342 {
351 archetype *more = get (f.get_str ()); 343 if (archetype *at = find (f.get_str ()))
352 unlink (more); 344 *op = at->clone;
353 object *op = object::create (); 345 else
346 LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n",
347 &at->name, f.get_str ());
348
349 f.next ();
350 }
351#endif
354 352
355 if (!op->parse_kv (f)) 353 if (!op->parse_kv (f))
354 goto fail;
355
356 parts.push_back (std::make_pair (at, op));
357
358 if (f.kw != KW_more)
359 break;
360
361 f.next ();
362
363 if (f.kw != KW_object)
356 { 364 {
357 op->destroy (true); 365 f.parse_error ("more object");
358 // leak head more* 366 goto fail;
359 return 0;
360 } 367 }
361
362 overwrite (more, op);
363 op->destroy ();
364
365 if (more->clone.x > head->tail_x) head->tail_x = more->clone.x;
366 if (more->clone.y > head->tail_y) head->tail_y = more->clone.y;
367
368 more->head = head;
369 more->clone.head = &head->clone;
370 prev->more = more;
371 prev->clone.more = &more->clone;
372
373 prev = more;
374 } 368 }
375 369
376 if (!head->next) 370 {
371 archetype *head = parts.front ().first;
372
373 // check that all archetypes belong to the same object or are heads
374 for (auto (p, parts.begin ()); p != parts.end (); ++p)
377 { 375 {
376 archetype *at = p->first;
377
378 if (at->head != head && at->head)
379 {
380 LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->name);
381 goto fail;
382 }
383
384 if (at->next && at != head)
385 {
386 LOG (llevError, "%s: unable to overwrite foreign head archetype with non-head archetype\n", &at->name);
387 goto fail;
388 }
389 }
390
391 // sever chain of existing head object
392 for (archetype *more, *at = head; at; at = more)
393 {
394 more = at->more;
395
396 at->head = 0;
397 at->more = 0;
398 }
399
400 // replace/update head
401 overwrite (head, parts.front ().second);
402 head->tail_x = 0;
403 head->tail_y = 0;
404
405 // link into list of heads, if not already there
406 if (!head->linked)
407 {
408 head->linked = true;
378 head->next = first_archetype; 409 head->next = first_archetype;
379 first_archetype = head; 410 first_archetype = head;
380 } 411 }
381 412
413 // reassemble new chain
414 archetype *prev = head;
415 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
416 {
417 archetype *at = p->first;
418 overwrite (at, p->second);
419
420 if (at->clone.x > head->tail_x) head->tail_x = at->clone.x;
421 if (at->clone.y > head->tail_y) head->tail_y = at->clone.y;
422
423 at->head = head;
424 at->clone.head = &head->clone;
425 prev->more = at;
426 prev->clone.more = &at->clone;
427
428 prev = at;
429 }
430
431 loading_arch = false;
382 return head; 432 return head;
383} 433 }
384 434
385/* 435fail:
386 * Reads/parses the archetype-file, and copies into a linked list 436 for (auto (p, parts.begin ()); p != parts.end (); ++p)
387 * of archetype-structures. 437 p->second->destroy (true);
388 */
389static bool
390load_archetypes (object_thawer &f)
391{
392 for (;;)
393 {
394 switch (f.kw)
395 {
396 case KW_object:
397 loading_arch = true;
398 if (!archetype::read (f))
399 {
400 loading_arch = false;
401 return false;
402 }
403 438
404 loading_arch = false; 439 loading_arch = false;
405 continue; 440 return 0;
406
407 case KW_EOF:
408 return true;
409
410 default:
411 if (!f.parse_error ("archetypes file"))
412 return false;
413 }
414
415 f.next ();
416 }
417} 441}
418 442
419/* 443/*
420 * First initialises the archtype hash-table (init_archetable()). 444 * Initialize global archtype pointers:
421 * Reads and parses the archetype file (with the first and second-pass
422 * functions).
423 * Then initialises treasures by calling load_treasures().
424 */ 445 */
425void 446void
426load_archetypes (void) 447init_archetype_pointers ()
427{ 448{
428 char filename[MAX_BUF]; 449 ring_arch = archetype::find ("ring");
429 450 amulet_arch = archetype::find ("amulet");
430 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 451 staff_arch = archetype::find ("staff");
431 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 452 crown_arch = archetype::find ("crown");
432 453 empty_archetype = archetype::find ("empty_archetype");
433 object_thawer f (filename);
434
435 f.next ();
436
437 if (!load_archetypes (f))
438 cleanup ("unable to load archetypes");
439
440 warn_archetypes = 1;
441} 454}
442 455
443/* 456/*
444 * Creates and returns a new object which is a copy of the given archetype. 457 * Creates and returns a new object which is a copy of the given archetype.
445 * This function returns NULL on failure. 458 * This function returns NULL on failure.
447object * 460object *
448arch_to_object (archetype *at) 461arch_to_object (archetype *at)
449{ 462{
450 if (!at) 463 if (!at)
451 { 464 {
452 if (warn_archetypes)
453 LOG (llevError, "Couldn't find archetype.\n"); 465 LOG (llevError, "Couldn't find archetype.\n");
454
455 return NULL; 466 return 0;
456 } 467 }
457 468
458 object *op = at->clone.clone (); 469 object *op = at->clone.clone ();
459 op->arch = at; 470 op->arch = at;
460 op->instantiate (); 471 op->instantiate ();
472
461 return op; 473 return op;
462} 474}
463 475
464/* 476/*
465 * Creates an object. This function is called by get_archetype() 477 * Creates an object. This function is called by get_archetype()
494 506
495 return arch_to_object (at); 507 return arch_to_object (at);
496} 508}
497 509
498/* 510/*
499 * Hash-function used by the arch-hashtable.
500 */
501
502unsigned long
503hasharch (const char *str, int tablesize)
504{
505 unsigned long hash = 0;
506 unsigned int i = 0;
507 const char *p;
508
509 /* use the one-at-a-time hash function, which supposedly is
510 * better than the djb2-like one used by perl5.005, but
511 * certainly is better then the bug used here before.
512 * see http://burtleburtle.net/bob/hash/doobs.html
513 */
514 for (p = str; i < MAXSTRING && *p; p++, i++)
515 {
516 hash += *p;
517 hash += hash << 10;
518 hash ^= hash >> 6;
519 }
520
521 hash += hash << 3;
522 hash ^= hash >> 11;
523 hash += hash << 15;
524
525 return hash % tablesize;
526}
527
528/*
529 * Finds, using the hashtable, which archetype matches the given name. 511 * Finds, using the hashtable, which archetype matches the given name.
530 * returns a pointer to the found archetype, otherwise NULL. 512 * returns a pointer to the found archetype, otherwise NULL.
531 */ 513 */
532archetype * 514archetype *
533archetype::find (const char *name) 515archetype::find (const char *name)
534{ 516{
535 if (!name) 517 if (!name)
536 return 0; 518 return 0;
537 519
538 AUTODECL (i, ht.find (name)); 520 auto (i, ht.find (name));
539 521
540 if (i == ht.end ()) 522 if (i == ht.end ())
541 return 0; 523 return 0;
542 else 524 else
543 return i->second; 525 return i->second;
544} 526}
545 527
546archetype * 528archetype *
547archetype::get (const char *name) 529archetype::get (const char *name)
548{ 530{
531 if (!name)
532 {
533 LOG (llevError, "null archetype requested\n");
534 name = "(null)";
535 }
536
549 archetype *at = find (name); 537 archetype *at = find (name);
550 538
551 if (!at) 539 if (!at)
552 { 540 {
553 archetypes.push_back (at = new archetype); 541 archetypes.push_back (at = new archetype);
554 at->name = name; 542 at->name = at->clone.name = at->clone.name_pl = name;
555 at->hash_add (); 543 at->hash_add ();
556 } 544 }
557 545
558 return at; 546 return at;
559} 547}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines