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.17 by root, Tue Sep 12 19:20:06 2006 UTC vs.
Revision 1.21 by root, Thu Sep 14 22:33:58 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>
48 * MSW 2003-04-29 48 * MSW 2003-04-29
49 */ 49 */
50 50
51#if USE_UNORDERED_MAP 51#if USE_UNORDERED_MAP
52// the hashtable 52// the hashtable
53typedef
54 std::tr1::unordered_map < 53typedef std::tr1::unordered_map
55size_t, archetype *> 54 <
55 std::size_t,
56 archetype *,
57 std::hash<size_t>,
58 std::equal_to<size_t>,
59 slice_allocator< std::pair<const std::size_t, archetype *> >
60 true,
56 HT; 61 > HT;
57 62
58static 63static HT ht;
59 HT
60 ht;
61#endif 64#endif
62 65
63/** 66/**
64 * GROS - This function retrieves an archetype given the name that appears 67 * GROS - This function retrieves an archetype given the name that appears
65 * during the game (for example, "writing pen" instead of "stylus"). 68 * during the game (for example, "writing pen" instead of "stylus").
72 * - the archetype found or null if nothing was found. 75 * - the archetype found or null if nothing was found.
73 */ 76 */
74archetype * 77archetype *
75find_archetype_by_object_name (const char *name) 78find_archetype_by_object_name (const char *name)
76{ 79{
77 archetype * 80 archetype *at;
78 at;
79 81
80 if (name == NULL) 82 if (name == NULL)
81 return (archetype *) NULL; 83 return (archetype *) NULL;
82 84
83 for (at = first_archetype; at != NULL; at = at->next) 85 for (at = first_archetype; at != NULL; at = at->next)
94 * except that it considers only items of the given type. 96 * except that it considers only items of the given type.
95 */ 97 */
96archetype * 98archetype *
97find_archetype_by_object_type_name (int type, const char *name) 99find_archetype_by_object_type_name (int type, const char *name)
98{ 100{
99 archetype * 101 archetype *at;
100 at;
101 102
102 if (name == NULL) 103 if (name == NULL)
103 return NULL; 104 return NULL;
104 105
105 for (at = first_archetype; at != NULL; at = at->next) 106 for (at = first_archetype; at != NULL; at = at->next)
179 { 180 {
180 tmpname[i] = 0; 181 tmpname[i] = 0;
181 at = find_archetype_by_object_name (tmpname); 182 at = find_archetype_by_object_name (tmpname);
182 183
183 if (at != NULL) 184 if (at != NULL)
184 {
185 return arch_to_object (at); 185 return arch_to_object (at);
186 }
187 } 186 }
188 187
189 return create_singularity (name); 188 return create_singularity (name);
190} 189}
191 190
324void 323void
325init_archetypes (void) 324init_archetypes (void)
326{ /* called from add_player() and edit() */ 325{ /* called from add_player() and edit() */
327 if (first_archetype != NULL) /* Only do this once */ 326 if (first_archetype != NULL) /* Only do this once */
328 return; 327 return;
328
329 arch_init = 1; 329 arch_init = 1;
330 load_archetypes (); 330 load_archetypes ();
331 arch_init = 0; 331 arch_init = 0;
332 empty_archetype = find_archetype ("empty_archetype"); 332 empty_archetype = archetype::find ("empty_archetype");
333 333
334/* init_blocksview();*/ 334/* init_blocksview();*/
335} 335}
336 336
337/* 337/*
546 cp--; 546 cp--;
547 } 547 }
548 } 548 }
549 if (!strcmp ("Object", variable)) 549 if (!strcmp ("Object", variable))
550 { 550 {
551 if ((at = find_archetype (argument)) == NULL) 551 if ((at = archetype::find (argument)) == NULL)
552 LOG (llevError, "Warning: failed to find arch %s\n", argument); 552 LOG (llevError, "Warning: failed to find arch %s\n", argument);
553 } 553 }
554 else if (!strcmp ("other_arch", variable)) 554 else if (!strcmp ("other_arch", variable))
555 { 555 {
556 if (at != NULL && at->clone.other_arch == NULL) 556 if (at != NULL && at->clone.other_arch == NULL)
557 { 557 {
558 if ((other = find_archetype (argument)) == NULL) 558 if ((other = archetype::find (argument)) == NULL)
559 LOG (llevError, "Warning: failed to find other_arch %s\n", argument); 559 LOG (llevError, "Warning: failed to find other_arch %s\n", argument);
560 else if (at != NULL) 560 else if (at != NULL)
561 at->clone.other_arch = other; 561 at->clone.other_arch = other;
562 } 562 }
563 } 563 }
675 */ 675 */
676 676
677object * 677object *
678create_singularity (const char *name) 678create_singularity (const char *name)
679{ 679{
680 object * 680 object * op;
681 op;
682 char
683 buf[MAX_BUF]; 681 char buf[MAX_BUF];
684 682
685 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 683 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
686 op = get_object (); 684 op = get_object ();
687 op->name = op->name_pl = buf; 685 op->name = op->name_pl = buf;
688 SET_FLAG (op, FLAG_NO_PICK); 686 SET_FLAG (op, FLAG_NO_PICK);
695 */ 693 */
696 694
697object * 695object *
698get_archetype (const char *name) 696get_archetype (const char *name)
699{ 697{
700 archetype * 698 archetype *at = archetype::find (name);
701 at;
702 699
703 at = find_archetype (name); 700 if (!at)
704 if (at == NULL)
705 return create_singularity (name); 701 return create_singularity (name);
706 702
707 return arch_to_object (at); 703 return arch_to_object (at);
708} 704}
709 705
712 */ 708 */
713 709
714unsigned long 710unsigned long
715hasharch (const char *str, int tablesize) 711hasharch (const char *str, int tablesize)
716{ 712{
717 unsigned long 713 unsigned long hash = 0;
718 hash = 0;
719 unsigned int 714 unsigned int i = 0;
720 i = 0;
721 const char * 715 const char *p;
722 p;
723 716
724 /* use the one-at-a-time hash function, which supposedly is 717 /* use the one-at-a-time hash function, which supposedly is
725 * better than the djb2-like one used by perl5.005, but 718 * better than the djb2-like one used by perl5.005, but
726 * certainly is better then the bug used here before. 719 * certainly is better then the bug used here before.
727 * see http://burtleburtle.net/bob/hash/doobs.html 720 * see http://burtleburtle.net/bob/hash/doobs.html
744 * Finds, using the hashtable, which archetype matches the given name. 737 * Finds, using the hashtable, which archetype matches the given name.
745 * returns a pointer to the found archetype, otherwise NULL. 738 * returns a pointer to the found archetype, otherwise NULL.
746 */ 739 */
747 740
748archetype * 741archetype *
749find_archetype (const char *name) 742archetype::find (const char *name)
750{ 743{
751#if USE_UNORDERED_MAP
752 name = shstr::find (name);
753
754 if (!name) 744 if (!name)
755 return 0; 745 return 0;
756 746
757 HT::const_iterator i = ht.find ((size_t) name); 747#if USE_UNORDERED_MAP
748 AUTODECL (i, ht.find ((size_t) name));
758 749
759 if (i == ht.end ()) 750 if (i == ht.end ())
760 return 0; 751 return 0;
761 else 752 else
762 return i->second; 753 return i->second;
763#endif 754#endif
764 755
765 archetype * 756 archetype *at;
766 at;
767 unsigned long 757 unsigned long index;
768 index;
769
770 if (name == NULL)
771 return (archetype *) NULL;
772 758
773 index = hasharch (name, ARCHTABLE); 759 index = hasharch (name, ARCHTABLE);
774 arch_search++; 760 arch_search++;
775 for (;;) 761 for (;;)
776 { 762 {
777 at = arch_table[index]; 763 at = arch_table[index];
764
778 if (at == NULL) 765 if (at == NULL)
779 { 766 {
780 if (warn_archetypes) 767 if (warn_archetypes)
781 LOG (llevError, "Couldn't find archetype %s\n", name); 768 LOG (llevError, "Couldn't find archetype %s\n", name);
769
782 return NULL; 770 return NULL;
783 } 771 }
772
784 arch_cmp++; 773 arch_cmp++;
774
785 if (!strcmp ((const char *) at->name, name)) 775 if (!strcmp ((const char *) at->name, name))
786 return at; 776 return at;
777
787 if (++index >= ARCHTABLE) 778 if (++index >= ARCHTABLE)
788 index = 0; 779 index = 0;
789 } 780 }
790} 781}
791 782
798{ 789{
799#if USE_UNORDERED_MAP 790#if USE_UNORDERED_MAP
800 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 791 ht.insert (std::make_pair ((size_t) (const char *) at->name, at));
801#endif 792#endif
802 793
803 int
804 index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 794 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index;
805 795
806 for (;;) 796 for (;;)
807 { 797 {
808 if (arch_table[index] == NULL) 798 if (arch_table[index] == NULL)
809 { 799 {
825 */ 815 */
826 816
827archetype * 817archetype *
828type_to_archetype (int type) 818type_to_archetype (int type)
829{ 819{
830 archetype * 820 archetype *at;
831 at;
832 821
833 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 822 for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more)
834 if (at->clone.type == type) 823 if (at->clone.type == type)
835 return at; 824 return at;
825
836 return NULL; 826 return 0;
837} 827}
838 828
839/* 829/*
840 * Returns a new object copied from the first archetype matching 830 * Returns a new object copied from the first archetype matching
841 * the given type. 831 * the given type.
843 */ 833 */
844 834
845object * 835object *
846clone_arch (int type) 836clone_arch (int type)
847{ 837{
848 archetype * 838 archetype *at;
849 at;
850 object *
851 op = get_object (); 839 object *op = get_object ();
852 840
853 if ((at = type_to_archetype (type)) == NULL) 841 if ((at = type_to_archetype (type)) == NULL)
854 { 842 {
855 LOG (llevError, "Can't clone archetype %d\n", type); 843 LOG (llevError, "Can't clone archetype %d\n", type);
856 free_object (op); 844 free_object (op);
857 return NULL; 845 return NULL;
858 } 846 }
847
859 copy_object (&at->clone, op); 848 copy_object (&at->clone, op);
860 op->instantiate (); 849 op->instantiate ();
861 return op; 850 return op;
862} 851}
863 852
866 */ 855 */
867 856
868object * 857object *
869object_create_arch (archetype *at) 858object_create_arch (archetype *at)
870{ 859{
871 object * 860 object *op, *prev = 0, *head = 0;
872 op, *
873 prev = NULL, *head = NULL;
874 861
875 while (at) 862 while (at)
876 { 863 {
877 op = arch_to_object (at); 864 op = arch_to_object (at);
878 op->x = at->clone.x; 865 op->x = at->clone.x;
879 op->y = at->clone.y; 866 op->y = at->clone.y;
867
880 if (head) 868 if (head)
881 op->head = head, prev->more = op; 869 op->head = head, prev->more = op;
870
882 if (!head) 871 if (!head)
883 head = op; 872 head = op;
873
884 prev = op; 874 prev = op;
885 at = at->more; 875 at = at->more;
886 } 876 }
877
887 return (head); 878 return (head);
888} 879}
889 880
890/*** end of arch.c ***/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines