1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_arch_c = |
|
|
4 | * "$Id: arch.C,v 1.16 2006/09/10 16:00:23 root Exp $"; |
|
|
5 | */ |
|
|
6 | |
|
|
7 | /* |
1 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
9 | |
3 | |
10 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
11 | Copyright (C) 1992 Frank Tore Johansen |
5 | Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
22 | |
16 | |
23 | 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 |
24 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
26 | |
20 | |
27 | 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> |
28 | */ |
22 | */ |
29 | |
23 | |
30 | #include <cassert> |
24 | #include <cassert> |
31 | |
25 | |
32 | #include <tr1/unordered_map> |
26 | #include <tr1/unordered_map> |
… | |
… | |
54 | * MSW 2003-04-29 |
48 | * MSW 2003-04-29 |
55 | */ |
49 | */ |
56 | |
50 | |
57 | #if USE_UNORDERED_MAP |
51 | #if USE_UNORDERED_MAP |
58 | // the hashtable |
52 | // the hashtable |
59 | typedef |
|
|
60 | std::tr1::unordered_map < |
53 | typedef std::tr1::unordered_map |
61 | size_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, |
62 | HT; |
61 | > HT; |
63 | |
62 | |
64 | static |
63 | static HT ht; |
65 | HT |
|
|
66 | ht; |
|
|
67 | #endif |
64 | #endif |
68 | |
65 | |
69 | /** |
66 | /** |
70 | * GROS - This function retrieves an archetype given the name that appears |
67 | * GROS - This function retrieves an archetype given the name that appears |
71 | * during the game (for example, "writing pen" instead of "stylus"). |
68 | * during the game (for example, "writing pen" instead of "stylus"). |
… | |
… | |
78 | * - the archetype found or null if nothing was found. |
75 | * - the archetype found or null if nothing was found. |
79 | */ |
76 | */ |
80 | archetype * |
77 | archetype * |
81 | find_archetype_by_object_name (const char *name) |
78 | find_archetype_by_object_name (const char *name) |
82 | { |
79 | { |
83 | archetype * |
80 | archetype *at; |
84 | at; |
|
|
85 | |
81 | |
86 | if (name == NULL) |
82 | if (name == NULL) |
87 | return (archetype *) NULL; |
83 | return (archetype *) NULL; |
88 | |
84 | |
89 | for (at = first_archetype; at != NULL; at = at->next) |
85 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
100 | * except that it considers only items of the given type. |
96 | * except that it considers only items of the given type. |
101 | */ |
97 | */ |
102 | archetype * |
98 | archetype * |
103 | find_archetype_by_object_type_name (int type, const char *name) |
99 | find_archetype_by_object_type_name (int type, const char *name) |
104 | { |
100 | { |
105 | archetype * |
101 | archetype *at; |
106 | at; |
|
|
107 | |
102 | |
108 | if (name == NULL) |
103 | if (name == NULL) |
109 | return NULL; |
104 | return NULL; |
110 | |
105 | |
111 | for (at = first_archetype; at != NULL; at = at->next) |
106 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
173 | * but it otherwise had a big memory leak. |
168 | * but it otherwise had a big memory leak. |
174 | */ |
169 | */ |
175 | object * |
170 | object * |
176 | get_archetype_by_object_name (const char *name) |
171 | get_archetype_by_object_name (const char *name) |
177 | { |
172 | { |
178 | archetype * |
173 | archetype *at; |
179 | at; |
|
|
180 | char |
|
|
181 | tmpname[MAX_BUF]; |
174 | char tmpname[MAX_BUF]; |
182 | int |
175 | int i; |
183 | i; |
|
|
184 | |
176 | |
185 | strncpy (tmpname, name, MAX_BUF - 1); |
177 | assign (tmpname, name); |
186 | tmpname[MAX_BUF - 1] = 0; |
178 | |
187 | for (i = strlen (tmpname); i > 0; i--) |
179 | for (i = strlen (tmpname); i > 0; i--) |
188 | { |
180 | { |
189 | tmpname[i] = 0; |
181 | tmpname[i] = 0; |
190 | at = find_archetype_by_object_name (tmpname); |
182 | at = find_archetype_by_object_name (tmpname); |
|
|
183 | |
191 | if (at != NULL) |
184 | if (at != NULL) |
192 | { |
|
|
193 | return arch_to_object (at); |
185 | return arch_to_object (at); |
194 | } |
|
|
195 | } |
186 | } |
|
|
187 | |
196 | return create_singularity (name); |
188 | return create_singularity (name); |
197 | } |
189 | } |
198 | |
190 | |
199 | /* This is a subset of the parse_id command. Basically, name can be |
191 | /* This is a subset of the parse_id command. Basically, name can be |
200 | * a string seperated lists of things to match, with certain keywords. |
192 | * a string seperated lists of things to match, with certain keywords. |
… | |
… | |
331 | void |
323 | void |
332 | init_archetypes (void) |
324 | init_archetypes (void) |
333 | { /* called from add_player() and edit() */ |
325 | { /* called from add_player() and edit() */ |
334 | if (first_archetype != NULL) /* Only do this once */ |
326 | if (first_archetype != NULL) /* Only do this once */ |
335 | return; |
327 | return; |
|
|
328 | |
336 | arch_init = 1; |
329 | arch_init = 1; |
337 | load_archetypes (); |
330 | load_archetypes (); |
338 | arch_init = 0; |
331 | arch_init = 0; |
339 | empty_archetype = find_archetype ("empty_archetype"); |
332 | empty_archetype = archetype::find ("empty_archetype"); |
340 | |
333 | |
341 | /* init_blocksview();*/ |
334 | /* init_blocksview();*/ |
342 | } |
335 | } |
343 | |
336 | |
344 | /* |
337 | /* |
… | |
… | |
553 | cp--; |
546 | cp--; |
554 | } |
547 | } |
555 | } |
548 | } |
556 | if (!strcmp ("Object", variable)) |
549 | if (!strcmp ("Object", variable)) |
557 | { |
550 | { |
558 | if ((at = find_archetype (argument)) == NULL) |
551 | if ((at = archetype::find (argument)) == NULL) |
559 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
552 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
560 | } |
553 | } |
561 | else if (!strcmp ("other_arch", variable)) |
554 | else if (!strcmp ("other_arch", variable)) |
562 | { |
555 | { |
563 | if (at != NULL && at->clone.other_arch == NULL) |
556 | if (at != NULL && at->clone.other_arch == NULL) |
564 | { |
557 | { |
565 | if ((other = find_archetype (argument)) == NULL) |
558 | if ((other = archetype::find (argument)) == NULL) |
566 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
559 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
567 | else if (at != NULL) |
560 | else if (at != NULL) |
568 | at->clone.other_arch = other; |
561 | at->clone.other_arch = other; |
569 | } |
562 | } |
570 | } |
563 | } |
… | |
… | |
682 | */ |
675 | */ |
683 | |
676 | |
684 | object * |
677 | object * |
685 | create_singularity (const char *name) |
678 | create_singularity (const char *name) |
686 | { |
679 | { |
687 | object * |
680 | object * op; |
688 | op; |
|
|
689 | char |
|
|
690 | buf[MAX_BUF]; |
681 | char buf[MAX_BUF]; |
691 | |
682 | |
692 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
683 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
693 | op = get_object (); |
684 | op = get_object (); |
694 | op->name = op->name_pl = buf; |
685 | op->name = op->name_pl = buf; |
695 | SET_FLAG (op, FLAG_NO_PICK); |
686 | SET_FLAG (op, FLAG_NO_PICK); |
… | |
… | |
702 | */ |
693 | */ |
703 | |
694 | |
704 | object * |
695 | object * |
705 | get_archetype (const char *name) |
696 | get_archetype (const char *name) |
706 | { |
697 | { |
707 | archetype * |
698 | archetype *at = archetype::find (name); |
708 | at; |
|
|
709 | |
699 | |
710 | at = find_archetype (name); |
700 | if (!at) |
711 | if (at == NULL) |
|
|
712 | return create_singularity (name); |
701 | return create_singularity (name); |
713 | |
702 | |
714 | return arch_to_object (at); |
703 | return arch_to_object (at); |
715 | } |
704 | } |
716 | |
705 | |
… | |
… | |
719 | */ |
708 | */ |
720 | |
709 | |
721 | unsigned long |
710 | unsigned long |
722 | hasharch (const char *str, int tablesize) |
711 | hasharch (const char *str, int tablesize) |
723 | { |
712 | { |
724 | unsigned long |
713 | unsigned long hash = 0; |
725 | hash = 0; |
|
|
726 | unsigned int |
714 | unsigned int i = 0; |
727 | i = 0; |
|
|
728 | const char * |
715 | const char *p; |
729 | p; |
|
|
730 | |
716 | |
731 | /* use the one-at-a-time hash function, which supposedly is |
717 | /* use the one-at-a-time hash function, which supposedly is |
732 | * better than the djb2-like one used by perl5.005, but |
718 | * better than the djb2-like one used by perl5.005, but |
733 | * certainly is better then the bug used here before. |
719 | * certainly is better then the bug used here before. |
734 | * see http://burtleburtle.net/bob/hash/doobs.html |
720 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
751 | * Finds, using the hashtable, which archetype matches the given name. |
737 | * Finds, using the hashtable, which archetype matches the given name. |
752 | * returns a pointer to the found archetype, otherwise NULL. |
738 | * returns a pointer to the found archetype, otherwise NULL. |
753 | */ |
739 | */ |
754 | |
740 | |
755 | archetype * |
741 | archetype * |
756 | find_archetype (const char *name) |
742 | archetype::find (const char *name) |
757 | { |
743 | { |
758 | #if USE_UNORDERED_MAP |
|
|
759 | name = shstr::find (name); |
|
|
760 | |
|
|
761 | if (!name) |
744 | if (!name) |
762 | return 0; |
745 | return 0; |
763 | |
746 | |
764 | HT::const_iterator i = ht.find ((size_t) name); |
747 | #if USE_UNORDERED_MAP |
|
|
748 | AUTODECL (i, ht.find ((size_t) name)); |
765 | |
749 | |
766 | if (i == ht.end ()) |
750 | if (i == ht.end ()) |
767 | return 0; |
751 | return 0; |
768 | else |
752 | else |
769 | return i->second; |
753 | return i->second; |
770 | #endif |
754 | #endif |
771 | |
755 | |
772 | archetype * |
756 | archetype *at; |
773 | at; |
|
|
774 | unsigned long |
757 | unsigned long index; |
775 | index; |
|
|
776 | |
|
|
777 | if (name == NULL) |
|
|
778 | return (archetype *) NULL; |
|
|
779 | |
758 | |
780 | index = hasharch (name, ARCHTABLE); |
759 | index = hasharch (name, ARCHTABLE); |
781 | arch_search++; |
760 | arch_search++; |
782 | for (;;) |
761 | for (;;) |
783 | { |
762 | { |
784 | at = arch_table[index]; |
763 | at = arch_table[index]; |
|
|
764 | |
785 | if (at == NULL) |
765 | if (at == NULL) |
786 | { |
766 | { |
787 | if (warn_archetypes) |
767 | if (warn_archetypes) |
788 | LOG (llevError, "Couldn't find archetype %s\n", name); |
768 | LOG (llevError, "Couldn't find archetype %s\n", name); |
|
|
769 | |
789 | return NULL; |
770 | return NULL; |
790 | } |
771 | } |
|
|
772 | |
791 | arch_cmp++; |
773 | arch_cmp++; |
|
|
774 | |
792 | if (!strcmp ((const char *) at->name, name)) |
775 | if (!strcmp ((const char *) at->name, name)) |
793 | return at; |
776 | return at; |
|
|
777 | |
794 | if (++index >= ARCHTABLE) |
778 | if (++index >= ARCHTABLE) |
795 | index = 0; |
779 | index = 0; |
796 | } |
780 | } |
797 | } |
781 | } |
798 | |
782 | |
… | |
… | |
805 | { |
789 | { |
806 | #if USE_UNORDERED_MAP |
790 | #if USE_UNORDERED_MAP |
807 | 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)); |
808 | #endif |
792 | #endif |
809 | |
793 | |
810 | int |
|
|
811 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
794 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
812 | |
795 | |
813 | for (;;) |
796 | for (;;) |
814 | { |
797 | { |
815 | if (arch_table[index] == NULL) |
798 | if (arch_table[index] == NULL) |
816 | { |
799 | { |
… | |
… | |
832 | */ |
815 | */ |
833 | |
816 | |
834 | archetype * |
817 | archetype * |
835 | type_to_archetype (int type) |
818 | type_to_archetype (int type) |
836 | { |
819 | { |
837 | archetype * |
820 | archetype *at; |
838 | at; |
|
|
839 | |
821 | |
840 | 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) |
841 | if (at->clone.type == type) |
823 | if (at->clone.type == type) |
842 | return at; |
824 | return at; |
|
|
825 | |
843 | return NULL; |
826 | return 0; |
844 | } |
827 | } |
845 | |
828 | |
846 | /* |
829 | /* |
847 | * Returns a new object copied from the first archetype matching |
830 | * Returns a new object copied from the first archetype matching |
848 | * the given type. |
831 | * the given type. |
… | |
… | |
850 | */ |
833 | */ |
851 | |
834 | |
852 | object * |
835 | object * |
853 | clone_arch (int type) |
836 | clone_arch (int type) |
854 | { |
837 | { |
855 | archetype * |
838 | archetype *at; |
856 | at; |
|
|
857 | object * |
|
|
858 | op = get_object (); |
839 | object *op = get_object (); |
859 | |
840 | |
860 | if ((at = type_to_archetype (type)) == NULL) |
841 | if ((at = type_to_archetype (type)) == NULL) |
861 | { |
842 | { |
862 | LOG (llevError, "Can't clone archetype %d\n", type); |
843 | LOG (llevError, "Can't clone archetype %d\n", type); |
863 | free_object (op); |
844 | free_object (op); |
864 | return NULL; |
845 | return NULL; |
865 | } |
846 | } |
|
|
847 | |
866 | copy_object (&at->clone, op); |
848 | copy_object (&at->clone, op); |
867 | op->instantiate (); |
849 | op->instantiate (); |
868 | return op; |
850 | return op; |
869 | } |
851 | } |
870 | |
852 | |
… | |
… | |
873 | */ |
855 | */ |
874 | |
856 | |
875 | object * |
857 | object * |
876 | object_create_arch (archetype *at) |
858 | object_create_arch (archetype *at) |
877 | { |
859 | { |
878 | object * |
860 | object *op, *prev = 0, *head = 0; |
879 | op, * |
|
|
880 | prev = NULL, *head = NULL; |
|
|
881 | |
861 | |
882 | while (at) |
862 | while (at) |
883 | { |
863 | { |
884 | op = arch_to_object (at); |
864 | op = arch_to_object (at); |
885 | op->x = at->clone.x; |
865 | op->x = at->clone.x; |
886 | op->y = at->clone.y; |
866 | op->y = at->clone.y; |
|
|
867 | |
887 | if (head) |
868 | if (head) |
888 | op->head = head, prev->more = op; |
869 | op->head = head, prev->more = op; |
|
|
870 | |
889 | if (!head) |
871 | if (!head) |
890 | head = op; |
872 | head = op; |
|
|
873 | |
891 | prev = op; |
874 | prev = op; |
892 | at = at->more; |
875 | at = at->more; |
893 | } |
876 | } |
|
|
877 | |
894 | return (head); |
878 | return (head); |
895 | } |
879 | } |
896 | |
880 | |
897 | /*** end of arch.c ***/ |
|
|