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.18 by root, Tue Sep 12 20:55:40 2006 UTC vs.
Revision 1.22 by root, Thu Sep 14 22:35:53 2006 UTC

16 16
17 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 20
21 The authors can be reached via e-mail at crossfire-devel@real-time.com 21 The authors can be reached via e-mail at <crossfire@schmorp.de>
22*/ 22*/
23 23
24#include <cassert> 24#include <cassert>
25 25
26#include <tr1/unordered_map> 26#include <tr1/unordered_map>
75 * - the archetype found or null if nothing was found. 75 * - the archetype found or null if nothing was found.
76 */ 76 */
77archetype * 77archetype *
78find_archetype_by_object_name (const char *name) 78find_archetype_by_object_name (const char *name)
79{ 79{
80 archetype * 80 archetype *at;
81 at;
82 81
83 if (name == NULL) 82 if (name == NULL)
84 return (archetype *) NULL; 83 return (archetype *) NULL;
85 84
86 for (at = first_archetype; at != NULL; at = at->next) 85 for (at = first_archetype; at != NULL; at = at->next)
97 * except that it considers only items of the given type. 96 * except that it considers only items of the given type.
98 */ 97 */
99archetype * 98archetype *
100find_archetype_by_object_type_name (int type, const char *name) 99find_archetype_by_object_type_name (int type, const char *name)
101{ 100{
102 archetype * 101 archetype *at;
103 at;
104 102
105 if (name == NULL) 103 if (name == NULL)
106 return NULL; 104 return NULL;
107 105
108 for (at = first_archetype; at != NULL; at = at->next) 106 for (at = first_archetype; at != NULL; at = at->next)
182 { 180 {
183 tmpname[i] = 0; 181 tmpname[i] = 0;
184 at = find_archetype_by_object_name (tmpname); 182 at = find_archetype_by_object_name (tmpname);
185 183
186 if (at != NULL) 184 if (at != NULL)
187 {
188 return arch_to_object (at); 185 return arch_to_object (at);
189 }
190 } 186 }
191 187
192 return create_singularity (name); 188 return create_singularity (name);
193} 189}
194 190
210 * Last, make a check on the full name. 206 * Last, make a check on the full name.
211 */ 207 */
212int 208int
213item_matched_string (object *pl, object *op, const char *name) 209item_matched_string (object *pl, object *op, const char *name)
214{ 210{
215 char *
216 cp,
217 local_name[MAX_BUF]; 211 char *cp, local_name[MAX_BUF];
218 int 212 int count, retval = 0;
219 count,
220 retval = 0;
221 213
222 strcpy (local_name, name); /* strtok is destructive to name */ 214 strcpy (local_name, name); /* strtok is destructive to name */
223 215
224 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 216 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
225 { 217 {
327void 319void
328init_archetypes (void) 320init_archetypes (void)
329{ /* called from add_player() and edit() */ 321{ /* called from add_player() and edit() */
330 if (first_archetype != NULL) /* Only do this once */ 322 if (first_archetype != NULL) /* Only do this once */
331 return; 323 return;
324
332 arch_init = 1; 325 arch_init = 1;
333 load_archetypes (); 326 load_archetypes ();
334 arch_init = 0; 327 arch_init = 0;
335 empty_archetype = find_archetype ("empty_archetype"); 328 empty_archetype = archetype::find ("empty_archetype");
336 329
337/* init_blocksview();*/ 330/* init_blocksview();*/
338} 331}
339 332
340/* 333/*
364 */ 357 */
365 358
366void 359void
367init_archetable (void) 360init_archetable (void)
368{ 361{
369 archetype * 362 archetype *at;
370 at;
371 363
372 LOG (llevDebug, " Setting up archetable...\n"); 364 LOG (llevDebug, " Setting up archetable...\n");
373 365
374 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 366 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
375 add_arch (at); 367 add_arch (at);
394 */ 386 */
395 387
396void 388void
397dump_all_archetypes (void) 389dump_all_archetypes (void)
398{ 390{
399 archetype * 391 archetype *at;
400 at;
401 392
402 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 393 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more)
403 { 394 {
404 dump_arch (at); 395 dump_arch (at);
405 fprintf (logfile, "%s\n", errmsg); 396 fprintf (logfile, "%s\n", errmsg);
407} 398}
408 399
409void 400void
410free_all_archs (void) 401free_all_archs (void)
411{ 402{
412 archetype * 403 archetype *at, *next;
413 at, *
414 next;
415 int
416 i = 0, f = 0; 404 int i = 0, f = 0;
417 405
418 for (at = first_archetype; at != NULL; at = next) 406 for (at = first_archetype; at != NULL; at = next)
419 { 407 {
420 if (at->more) 408 if (at->more)
421 next = at->more; 409 next = at->more;
446 * of archetype-structures. 434 * of archetype-structures.
447 */ 435 */
448void 436void
449first_arch_pass (object_thawer & fp) 437first_arch_pass (object_thawer & fp)
450{ 438{
451 archetype *
452 at, *
453 head = NULL, *last_more = NULL; 439 archetype *at, *head = NULL, *last_more = NULL;
454 440
455 at->clone.arch = first_archetype = at = new archetype; 441 at->clone.arch = first_archetype = at = new archetype;
456 442
457 while (int i = load_object (fp, &at->clone, 0)) 443 while (int i = load_object (fp, &at->clone, 0))
458 { 444 {
527 */ 513 */
528 514
529void 515void
530second_arch_pass (object_thawer & thawer) 516second_arch_pass (object_thawer & thawer)
531{ 517{
532 char
533 buf[MAX_BUF], *
534 variable = buf, *argument, *cp; 518 char buf[MAX_BUF], *variable = buf, *argument, *cp;
535 archetype *
536 at = NULL, *other; 519 archetype *at = NULL, *other;
537 520
538 while (fgets (buf, MAX_BUF, thawer) != NULL) 521 while (fgets (buf, MAX_BUF, thawer) != NULL)
539 { 522 {
540 if (*buf == '#') 523 if (*buf == '#')
541 continue; 524 continue;
549 cp--; 532 cp--;
550 } 533 }
551 } 534 }
552 if (!strcmp ("Object", variable)) 535 if (!strcmp ("Object", variable))
553 { 536 {
554 if ((at = find_archetype (argument)) == NULL) 537 if ((at = archetype::find (argument)) == NULL)
555 LOG (llevError, "Warning: failed to find arch %s\n", argument); 538 LOG (llevError, "Warning: failed to find arch %s\n", argument);
556 } 539 }
557 else if (!strcmp ("other_arch", variable)) 540 else if (!strcmp ("other_arch", variable))
558 { 541 {
559 if (at != NULL && at->clone.other_arch == NULL) 542 if (at != NULL && at->clone.other_arch == NULL)
560 { 543 {
561 if ((other = find_archetype (argument)) == NULL) 544 if ((other = archetype::find (argument)) == NULL)
562 LOG (llevError, "Warning: failed to find other_arch %s\n", argument); 545 LOG (llevError, "Warning: failed to find other_arch %s\n", argument);
563 else if (at != NULL) 546 else if (at != NULL)
564 at->clone.other_arch = other; 547 at->clone.other_arch = other;
565 } 548 }
566 } 549 }
582 565
583#ifdef DEBUG 566#ifdef DEBUG
584void 567void
585check_generators (void) 568check_generators (void)
586{ 569{
587 archetype * 570 archetype *at;
588 at;
589 571
590 for (at = first_archetype; at != NULL; at = at->next) 572 for (at = first_archetype; at != NULL; at = at->next)
591 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) 573 if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL)
592 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); 574 LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name);
593} 575}
601 */ 583 */
602 584
603void 585void
604load_archetypes (void) 586load_archetypes (void)
605{ 587{
606 char
607 filename[MAX_BUF]; 588 char filename[MAX_BUF];
608 589
609#if TIME_ARCH_LOAD 590#if TIME_ARCH_LOAD
610 struct timeval 591 struct timeval
611 tv1, 592 tv1,
612 tv2; 593 tv2;
650 */ 631 */
651 632
652object * 633object *
653arch_to_object (archetype *at) 634arch_to_object (archetype *at)
654{ 635{
655 object * 636 object *op;
656 op;
657 637
658 if (at == NULL) 638 if (at == NULL)
659 { 639 {
660 if (warn_archetypes) 640 if (warn_archetypes)
661 LOG (llevError, "Couldn't find archetype.\n"); 641 LOG (llevError, "Couldn't find archetype.\n");
678 */ 658 */
679 659
680object * 660object *
681create_singularity (const char *name) 661create_singularity (const char *name)
682{ 662{
683 object * 663 object *op;
684 op;
685 char
686 buf[MAX_BUF]; 664 char buf[MAX_BUF];
687 665
688 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 666 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
689 op = get_object (); 667 op = get_object ();
690 op->name = op->name_pl = buf; 668 op->name = op->name_pl = buf;
691 SET_FLAG (op, FLAG_NO_PICK); 669 SET_FLAG (op, FLAG_NO_PICK);
698 */ 676 */
699 677
700object * 678object *
701get_archetype (const char *name) 679get_archetype (const char *name)
702{ 680{
703 archetype * 681 archetype *at = archetype::find (name);
704 at;
705 682
706 at = find_archetype (name); 683 if (!at)
707 if (at == NULL)
708 return create_singularity (name); 684 return create_singularity (name);
709 685
710 return arch_to_object (at); 686 return arch_to_object (at);
711} 687}
712 688
715 */ 691 */
716 692
717unsigned long 693unsigned long
718hasharch (const char *str, int tablesize) 694hasharch (const char *str, int tablesize)
719{ 695{
720 unsigned long 696 unsigned long hash = 0;
721 hash = 0;
722 unsigned int 697 unsigned int i = 0;
723 i = 0;
724 const char * 698 const char *p;
725 p;
726 699
727 /* use the one-at-a-time hash function, which supposedly is 700 /* use the one-at-a-time hash function, which supposedly is
728 * better than the djb2-like one used by perl5.005, but 701 * better than the djb2-like one used by perl5.005, but
729 * certainly is better then the bug used here before. 702 * certainly is better then the bug used here before.
730 * see http://burtleburtle.net/bob/hash/doobs.html 703 * see http://burtleburtle.net/bob/hash/doobs.html
747 * Finds, using the hashtable, which archetype matches the given name. 720 * Finds, using the hashtable, which archetype matches the given name.
748 * returns a pointer to the found archetype, otherwise NULL. 721 * returns a pointer to the found archetype, otherwise NULL.
749 */ 722 */
750 723
751archetype * 724archetype *
752find_archetype (const char *name) 725archetype::find (const char *name)
753{ 726{
754#if USE_UNORDERED_MAP
755 name = shstr::find (name);
756
757 if (!name) 727 if (!name)
758 return 0; 728 return 0;
759 729
760 HT::const_iterator i = ht.find ((size_t) name); 730#if USE_UNORDERED_MAP
731 AUTODECL (i, ht.find ((size_t) name));
761 732
762 if (i == ht.end ()) 733 if (i == ht.end ())
763 return 0; 734 return 0;
764 else 735 else
765 return i->second; 736 return i->second;
766#endif 737#endif
767 738
768 archetype * 739 archetype *at;
769 at;
770 unsigned long 740 unsigned long index;
771 index;
772
773 if (name == NULL)
774 return (archetype *) NULL;
775 741
776 index = hasharch (name, ARCHTABLE); 742 index = hasharch (name, ARCHTABLE);
777 arch_search++; 743 arch_search++;
778 for (;;) 744 for (;;)
779 { 745 {
780 at = arch_table[index]; 746 at = arch_table[index];
747
781 if (at == NULL) 748 if (at == NULL)
782 { 749 {
783 if (warn_archetypes) 750 if (warn_archetypes)
784 LOG (llevError, "Couldn't find archetype %s\n", name); 751 LOG (llevError, "Couldn't find archetype %s\n", name);
752
785 return NULL; 753 return NULL;
786 } 754 }
755
787 arch_cmp++; 756 arch_cmp++;
757
788 if (!strcmp ((const char *) at->name, name)) 758 if (!strcmp ((const char *) at->name, name))
789 return at; 759 return at;
760
790 if (++index >= ARCHTABLE) 761 if (++index >= ARCHTABLE)
791 index = 0; 762 index = 0;
792 } 763 }
793} 764}
794 765
801{ 772{
802#if USE_UNORDERED_MAP 773#if USE_UNORDERED_MAP
803 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 774 ht.insert (std::make_pair ((size_t) (const char *) at->name, at));
804#endif 775#endif
805 776
806 int
807 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 777 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
808 778
809 for (;;) 779 for (;;)
810 { 780 {
811 if (arch_table[index] == NULL) 781 if (arch_table[index] == NULL)
812 { 782 {
828 */ 798 */
829 799
830archetype * 800archetype *
831type_to_archetype (int type) 801type_to_archetype (int type)
832{ 802{
833 archetype * 803 archetype *at;
834 at;
835 804
836 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 805 for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more)
837 if (at->clone.type == type) 806 if (at->clone.type == type)
838 return at; 807 return at;
808
839 return NULL; 809 return 0;
840} 810}
841 811
842/* 812/*
843 * Returns a new object copied from the first archetype matching 813 * Returns a new object copied from the first archetype matching
844 * the given type. 814 * the given type.
846 */ 816 */
847 817
848object * 818object *
849clone_arch (int type) 819clone_arch (int type)
850{ 820{
851 archetype * 821 archetype *at;
852 at;
853 object *
854 op = get_object (); 822 object *op = get_object ();
855 823
856 if ((at = type_to_archetype (type)) == NULL) 824 if ((at = type_to_archetype (type)) == NULL)
857 { 825 {
858 LOG (llevError, "Can't clone archetype %d\n", type); 826 LOG (llevError, "Can't clone archetype %d\n", type);
859 free_object (op); 827 free_object (op);
860 return NULL; 828 return NULL;
861 } 829 }
830
862 copy_object (&at->clone, op); 831 copy_object (&at->clone, op);
863 op->instantiate (); 832 op->instantiate ();
864 return op; 833 return op;
865} 834}
866 835
869 */ 838 */
870 839
871object * 840object *
872object_create_arch (archetype *at) 841object_create_arch (archetype *at)
873{ 842{
874 object * 843 object *op, *prev = 0, *head = 0;
875 op, *
876 prev = NULL, *head = NULL;
877 844
878 while (at) 845 while (at)
879 { 846 {
880 op = arch_to_object (at); 847 op = arch_to_object (at);
881 op->x = at->clone.x; 848 op->x = at->clone.x;
882 op->y = at->clone.y; 849 op->y = at->clone.y;
850
883 if (head) 851 if (head)
884 op->head = head, prev->more = op; 852 op->head = head, prev->more = op;
853
885 if (!head) 854 if (!head)
886 head = op; 855 head = op;
856
887 prev = op; 857 prev = op;
888 at = at->more; 858 at = at->more;
889 } 859 }
860
890 return (head); 861 return (head);
891} 862}
892 863
893/*** end of arch.c ***/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines