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.14 by root, Fri Sep 8 17:14:07 2006 UTC vs.
Revision 1.15 by root, Sun Sep 10 13:20:12 2006 UTC

1
1/* 2/*
2 * static char *rcsid_arch_c = 3 * static char *rcsid_arch_c =
3 * "$Id: arch.C,v 1.14 2006/09/08 17:14:07 root Exp $"; 4 * "$Id: arch.C,v 1.15 2006/09/10 13:20:12 root Exp $";
4 */ 5 */
5 6
6/* 7/*
7 CrossFire, A Multiplayer game for X-windows 8 CrossFire, A Multiplayer game for X-windows
8 9
36#include <loader.h> 37#include <loader.h>
37 38
38/* IF set, does a little timing on the archetype load. */ 39/* IF set, does a little timing on the archetype load. */
39#define TIME_ARCH_LOAD 0 40#define TIME_ARCH_LOAD 0
40 41
41static void add_arch (archetype * at); 42static void add_arch (archetype *at);
42 43
43static archetype *arch_table[ARCHTABLE]; 44static archetype *arch_table[ARCHTABLE];
44int arch_cmp = 0; /* How many strcmp's */ 45int arch_cmp = 0; /* How many strcmp's */
45int arch_search = 0; /* How many searches */ 46int arch_search = 0; /* How many searches */
46int arch_init; /* True if doing arch initialization */ 47int arch_init; /* True if doing arch initialization */
53 * MSW 2003-04-29 54 * MSW 2003-04-29
54 */ 55 */
55 56
56#if USE_UNORDERED_MAP 57#if USE_UNORDERED_MAP
57// the hashtable 58// the hashtable
58typedef std::tr1::unordered_map<size_t, archetype *> HT; 59typedef
60 std::tr1::unordered_map <
61size_t, archetype *>
62 HT;
59 63
60static HT ht; 64static HT
65 ht;
61#endif 66#endif
62 67
63/** 68/**
64 * GROS - This function retrieves an archetype given the name that appears 69 * GROS - This function retrieves an archetype given the name that appears
65 * during the game (for example, "writing pen" instead of "stylus"). 70 * during the game (for example, "writing pen" instead of "stylus").
72 * - the archetype found or null if nothing was found. 77 * - the archetype found or null if nothing was found.
73 */ 78 */
74archetype * 79archetype *
75find_archetype_by_object_name (const char *name) 80find_archetype_by_object_name (const char *name)
76{ 81{
77 archetype *at; 82 archetype *
83 at;
78 84
79 if (name == NULL) 85 if (name == NULL)
80 return (archetype *) NULL; 86 return (archetype *) NULL;
81 87
82 for (at = first_archetype; at != NULL; at = at->next) 88 for (at = first_archetype; at != NULL; at = at->next)
93 * except that it considers only items of the given type. 99 * except that it considers only items of the given type.
94 */ 100 */
95archetype * 101archetype *
96find_archetype_by_object_type_name (int type, const char *name) 102find_archetype_by_object_type_name (int type, const char *name)
97{ 103{
98 archetype *at; 104 archetype *
105 at;
99 106
100 if (name == NULL) 107 if (name == NULL)
101 return NULL; 108 return NULL;
102 109
103 for (at = first_archetype; at != NULL; at = at->next) 110 for (at = first_archetype; at != NULL; at = at->next)
204 * If count is 1, make a quick check on the name. 211 * If count is 1, make a quick check on the name.
205 * IF count is >1, we need to make plural name. Return if match. 212 * IF count is >1, we need to make plural name. Return if match.
206 * Last, make a check on the full name. 213 * Last, make a check on the full name.
207 */ 214 */
208int 215int
209item_matched_string (object * pl, object * op, const char *name) 216item_matched_string (object *pl, object *op, const char *name)
210{ 217{
211 char * 218 char *
212 cp, 219 cp,
213 local_name[MAX_BUF]; 220 local_name[MAX_BUF];
214 int 221 int
215 count, 222 count,
216 retval = 0; 223 retval = 0;
224
217 strcpy (local_name, name); /* strtok is destructive to name */ 225 strcpy (local_name, name); /* strtok is destructive to name */
218 226
219 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 227 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
220 { 228 {
221 while (cp[0] == ' ') 229 while (cp[0] == ' ')
326 return; 334 return;
327 arch_init = 1; 335 arch_init = 1;
328 load_archetypes (); 336 load_archetypes ();
329 arch_init = 0; 337 arch_init = 0;
330 empty_archetype = find_archetype ("empty_archetype"); 338 empty_archetype = find_archetype ("empty_archetype");
339
331/* init_blocksview();*/ 340/* init_blocksview();*/
332} 341}
333 342
334/* 343/*
335 * Stores debug-information about how efficient the hashtable 344 * Stores debug-information about how efficient the hashtable
336 * used for archetypes has been in the static errmsg array. 345 * used for archetypes has been in the static errmsg array.
337 */ 346 */
338 347
339void 348void
340arch_info (object * op) 349arch_info (object *op)
341{ 350{
342 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); 351 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
343 new_draw_info (NDI_BLACK, 0, op, errmsg); 352 new_draw_info (NDI_BLACK, 0, op, errmsg);
344} 353}
345 354
358 */ 367 */
359 368
360void 369void
361init_archetable (void) 370init_archetable (void)
362{ 371{
363 archetype *at; 372 archetype *
373 at;
364 374
365 LOG (llevDebug, " Setting up archetable...\n"); 375 LOG (llevDebug, " Setting up archetable...\n");
366 376
367 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 377 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
368 add_arch (at); 378 add_arch (at);
373/* 383/*
374 * Dumps an archetype to debug-level output. 384 * Dumps an archetype to debug-level output.
375 */ 385 */
376 386
377void 387void
378dump_arch (archetype * at) 388dump_arch (archetype *at)
379{ 389{
380 dump_object (&at->clone); 390 dump_object (&at->clone);
381} 391}
382 392
383/* 393/*
389void 399void
390dump_all_archetypes (void) 400dump_all_archetypes (void)
391{ 401{
392 archetype * 402 archetype *
393 at; 403 at;
404
394 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 405 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
395 { 406 {
396 dump_arch (at); 407 dump_arch (at);
397 fprintf (logfile, "%s\n", errmsg); 408 fprintf (logfile, "%s\n", errmsg);
398 } 409 }
412 if (at->more) 423 if (at->more)
413 next = at->more; 424 next = at->more;
414 else 425 else
415 next = at->next; 426 next = at->next;
416 427
417 delete 428 delete at;
418 at;
419 i++; 429 i++;
420 } 430 }
421 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 431 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
422} 432}
423 433
424archetype::archetype () 434archetype::archetype ()
425{ 435{
426 clear_object (&clone); /* to initial state other also */ 436 clear_object (&clone); /* to initial state other also */
427 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 437 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */
428 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 438 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
429} 439}
430 440
431archetype::~archetype () 441archetype::~archetype ()
432{ 442{
433} 443}
437 * of archetype-structures. 447 * of archetype-structures.
438 */ 448 */
439void 449void
440first_arch_pass (object_thawer & fp) 450first_arch_pass (object_thawer & fp)
441{ 451{
452 archetype *
453 at, *
442 archetype *at, *head = NULL, *last_more = NULL; 454 head = NULL, *last_more = NULL;
443 455
444 at->clone.arch = first_archetype = at = new archetype; 456 at->clone.arch = first_archetype = at = new archetype;
445 457
446 while (int i = load_object (fp, &at->clone, 0)) 458 while (int i = load_object (fp, &at->clone, 0))
447 { 459 {
454 */ 466 */
455 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info)); 467 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
456 468
457 switch (i) 469 switch (i)
458 { 470 {
459 case LL_NORMAL: /* A new archetype, just link it with the previous */ 471 case LL_NORMAL: /* A new archetype, just link it with the previous */
460 if (last_more != NULL) 472 if (last_more != NULL)
461 last_more->next = at; 473 last_more->next = at;
462 if (head != NULL) 474 if (head != NULL)
463 head->next = at; 475 head->next = at;
464 head = last_more = at; 476 head = last_more = at;
465#if 0 477#if 0
466 if (!op->type) 478 if (!op->type)
467 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name); 479 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name);
468#endif 480#endif
469 at->tail_x = 0; 481 at->tail_x = 0;
470 at->tail_y = 0; 482 at->tail_y = 0;
471 break; 483 break;
472 484
473 case LL_MORE: /* Another part of the previous archetype, link it correctly */ 485 case LL_MORE: /* Another part of the previous archetype, link it correctly */
474 486
475 at->head = head; 487 at->head = head;
476 at->clone.head = &head->clone; 488 at->clone.head = &head->clone;
477 if (last_more != NULL) 489 if (last_more != NULL)
478 { 490 {
479 last_more->more = at; 491 last_more->more = at;
480 last_more->clone.more = &at->clone; 492 last_more->clone.more = &at->clone;
481 } 493 }
482 last_more = at; 494 last_more = at;
483 495
484 /* If this multipart image is still composed of individual small 496 /* If this multipart image is still composed of individual small
485 * images, don't set the tail_.. values. We can't use them anyways, 497 * images, don't set the tail_.. values. We can't use them anyways,
486 * and setting these to zero makes the map sending to the client much 498 * and setting these to zero makes the map sending to the client much
487 * easier as just looking at the head, we know what to do. 499 * easier as just looking at the head, we know what to do.
488 */ 500 */
489 if (at->clone.face != head->clone.face) 501 if (at->clone.face != head->clone.face)
490 { 502 {
491 head->tail_x = 0; 503 head->tail_x = 0;
492 head->tail_y = 0; 504 head->tail_y = 0;
493 } 505 }
494 else 506 else
495 { 507 {
496 if (at->clone.x > head->tail_x) 508 if (at->clone.x > head->tail_x)
497 head->tail_x = at->clone.x; 509 head->tail_x = at->clone.x;
498 if (at->clone.y > head->tail_y) 510 if (at->clone.y > head->tail_y)
499 head->tail_y = at->clone.y; 511 head->tail_y = at->clone.y;
500 } 512 }
501 break; 513 break;
502 514
503 } 515 }
504 516
505 at = new archetype; 517 at = new archetype;
518
506 at->clone.arch = at; 519 at->clone.arch = at;
507 } 520 }
508 521
509 delete at; 522 delete
523 at;
510} 524}
511 525
512/* 526/*
513 * Reads the archetype file once more, and links all pointers between 527 * Reads the archetype file once more, and links all pointers between
514 * archetypes. 528 * archetypes.
515 */ 529 */
516 530
517void 531void
518second_arch_pass (object_thawer & thawer) 532second_arch_pass (object_thawer & thawer)
519{ 533{
534 char
535 buf[MAX_BUF], *
520 char buf[MAX_BUF], *variable = buf, *argument, *cp; 536 variable = buf, *argument, *cp;
537 archetype *
521 archetype *at = NULL, *other; 538 at = NULL, *other;
522 539
523 while (fgets (buf, MAX_BUF, thawer) != NULL) 540 while (fgets (buf, MAX_BUF, thawer) != NULL)
524 { 541 {
525 if (*buf == '#') 542 if (*buf == '#')
526 continue; 543 continue;
553 { 570 {
554 if (at != NULL) 571 if (at != NULL)
555 { 572 {
556 treasurelist * 573 treasurelist *
557 tl = find_treasurelist (argument); 574 tl = find_treasurelist (argument);
575
558 if (tl == NULL) 576 if (tl == NULL)
559 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 577 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
560 else 578 else
561 at->clone.randomitems = tl; 579 at->clone.randomitems = tl;
562 } 580 }
568void 586void
569check_generators (void) 587check_generators (void)
570{ 588{
571 archetype * 589 archetype *
572 at; 590 at;
591
573 for (at = first_archetype; at != NULL; at = at->next) 592 for (at = first_archetype; at != NULL; at = at->next)
574 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 593 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL)
575 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); 594 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
576} 595}
577#endif 596#endif
586void 605void
587load_archetypes (void) 606load_archetypes (void)
588{ 607{
589 char 608 char
590 filename[MAX_BUF]; 609 filename[MAX_BUF];
610
591#if TIME_ARCH_LOAD 611#if TIME_ARCH_LOAD
592 struct timeval 612 struct timeval
593 tv1, 613 tv1,
594 tv2; 614 tv2;
595#endif 615#endif
596 616
597 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 617 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
598 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 618 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
599 619
600 { 620 {
601 object_thawer
602 thawer (filename); 621 object_thawer thawer (filename);
603 622
604 clear_archetable (); 623 clear_archetable ();
605 LOG (llevDebug, " arch-pass 1...\n"); 624 LOG (llevDebug, " arch-pass 1...\n");
606 first_arch_pass (thawer); 625 first_arch_pass (thawer);
607 LOG (llevDebug, " done\n"); 626 LOG (llevDebug, " done\n");
609 628
610 init_archetable (); 629 init_archetable ();
611 warn_archetypes = 1; 630 warn_archetypes = 1;
612 631
613 { 632 {
614 object_thawer
615 thawer (filename); 633 object_thawer thawer (filename);
616 634
617 LOG (llevDebug, " loading treasure...\n"); 635 LOG (llevDebug, " loading treasure...\n");
618 load_treasures (); 636 load_treasures ();
619 LOG (llevDebug, " done\n arch-pass 2...\n"); 637 LOG (llevDebug, " done\n arch-pass 2...\n");
620 second_arch_pass (thawer); 638 second_arch_pass (thawer);
630 * Creates and returns a new object which is a copy of the given archetype. 648 * Creates and returns a new object which is a copy of the given archetype.
631 * This function returns NULL on failure. 649 * This function returns NULL on failure.
632 */ 650 */
633 651
634object * 652object *
635arch_to_object (archetype * at) 653arch_to_object (archetype *at)
636{ 654{
637 object * 655 object *
638 op; 656 op;
657
639 if (at == NULL) 658 if (at == NULL)
640 { 659 {
641 if (warn_archetypes) 660 if (warn_archetypes)
642 LOG (llevError, "Couldn't find archetype.\n"); 661 LOG (llevError, "Couldn't find archetype.\n");
662
643 return NULL; 663 return NULL;
644 } 664 }
665
645 op = get_object (); 666 op = get_object ();
646 copy_object (&at->clone, op); 667 copy_object (&at->clone, op);
668 op->arch = at;
647 op->instantiate (); 669 op->instantiate ();
648 op->arch = at;
649 return op; 670 return op;
650} 671}
651 672
652/* 673/*
653 * Creates an object. This function is called by get_archetype() 674 * Creates an object. This function is called by get_archetype()
657 */ 678 */
658 679
659object * 680object *
660create_singularity (const char *name) 681create_singularity (const char *name)
661{ 682{
662 object *op; 683 object *
684 op;
685 char
663 char buf[MAX_BUF]; 686 buf[MAX_BUF];
687
664 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 688 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
665 op = get_object (); 689 op = get_object ();
666 op->name = op->name_pl = buf; 690 op->name = op->name_pl = buf;
667 SET_FLAG (op, FLAG_NO_PICK); 691 SET_FLAG (op, FLAG_NO_PICK);
668 return op; 692 return op;
674 */ 698 */
675 699
676object * 700object *
677get_archetype (const char *name) 701get_archetype (const char *name)
678{ 702{
679 archetype *at; 703 archetype *
704 at;
705
680 at = find_archetype (name); 706 at = find_archetype (name);
681 if (at == NULL) 707 if (at == NULL)
682 return create_singularity (name); 708 return create_singularity (name);
709
683 return arch_to_object (at); 710 return arch_to_object (at);
684} 711}
685 712
686/* 713/*
687 * Hash-function used by the arch-hashtable. 714 * Hash-function used by the arch-hashtable.
688 */ 715 */
689 716
690unsigned long 717unsigned long
691hasharch (const char *str, int tablesize) 718hasharch (const char *str, int tablesize)
692{ 719{
693 unsigned long hash = 0; 720 unsigned long
721 hash = 0;
694 unsigned int i = 0; 722 unsigned int
723 i = 0;
695 const char *p; 724 const char *
725 p;
696 726
697 /* use the one-at-a-time hash function, which supposedly is 727 /* use the one-at-a-time hash function, which supposedly is
698 * better than the djb2-like one used by perl5.005, but 728 * better than the djb2-like one used by perl5.005, but
699 * certainly is better then the bug used here before. 729 * certainly is better then the bug used here before.
700 * see http://burtleburtle.net/bob/hash/doobs.html 730 * see http://burtleburtle.net/bob/hash/doobs.html
725 name = shstr::find (name); 755 name = shstr::find (name);
726 756
727 if (!name) 757 if (!name)
728 return 0; 758 return 0;
729 759
730 HT::const_iterator i = ht.find ((size_t)name); 760 HT::const_iterator i = ht.find ((size_t) name);
731 761
732 if (i == ht.end ()) 762 if (i == ht.end ())
733 return 0; 763 return 0;
734 else 764 else
735 return i->second; 765 return i->second;
736#endif 766#endif
737 767
738 archetype *at; 768 archetype *
769 at;
739 unsigned long index; 770 unsigned long
771 index;
740 772
741 if (name == NULL) 773 if (name == NULL)
742 return (archetype *) NULL; 774 return (archetype *) NULL;
743 775
744 index = hasharch (name, ARCHTABLE); 776 index = hasharch (name, ARCHTABLE);
766 798
767static void 799static void
768add_arch (archetype *at) 800add_arch (archetype *at)
769{ 801{
770#if USE_UNORDERED_MAP 802#if USE_UNORDERED_MAP
771 ht.insert (std::make_pair ((size_t)(const char *)at->name, at)); 803 ht.insert (std::make_pair ((size_t) (const char *) at->name, at));
772#endif 804#endif
773 805
806 int
774 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 807 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
775 808
776 for (;;) 809 for (;;)
777 { 810 {
778 if (arch_table[index] == NULL) 811 if (arch_table[index] == NULL)
779 { 812 {
834/* 867/*
835 * member: make instance from class 868 * member: make instance from class
836 */ 869 */
837 870
838object * 871object *
839object_create_arch (archetype * at) 872object_create_arch (archetype *at)
840{ 873{
841 object * 874 object *
842 op, * 875 op, *
843 prev = NULL, *head = NULL; 876 prev = NULL, *head = NULL;
844 877

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines