1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_arch_c = |
|
|
4 | * "$Id: arch.C,v 1.15 2006/09/10 13:20:12 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 HT |
63 | static HT ht; |
65 | ht; |
|
|
66 | #endif |
64 | #endif |
67 | |
65 | |
68 | /** |
66 | /** |
69 | * GROS - This function retrieves an archetype given the name that appears |
67 | * GROS - This function retrieves an archetype given the name that appears |
70 | * during the game (for example, "writing pen" instead of "stylus"). |
68 | * during the game (for example, "writing pen" instead of "stylus"). |
… | |
… | |
77 | * - the archetype found or null if nothing was found. |
75 | * - the archetype found or null if nothing was found. |
78 | */ |
76 | */ |
79 | archetype * |
77 | archetype * |
80 | find_archetype_by_object_name (const char *name) |
78 | find_archetype_by_object_name (const char *name) |
81 | { |
79 | { |
82 | archetype * |
80 | archetype *at; |
83 | at; |
|
|
84 | |
81 | |
85 | if (name == NULL) |
82 | if (name == NULL) |
86 | return (archetype *) NULL; |
83 | return (archetype *) NULL; |
87 | |
84 | |
88 | for (at = first_archetype; at != NULL; at = at->next) |
85 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
99 | * except that it considers only items of the given type. |
96 | * except that it considers only items of the given type. |
100 | */ |
97 | */ |
101 | archetype * |
98 | archetype * |
102 | find_archetype_by_object_type_name (int type, const char *name) |
99 | find_archetype_by_object_type_name (int type, const char *name) |
103 | { |
100 | { |
104 | archetype * |
101 | archetype *at; |
105 | at; |
|
|
106 | |
102 | |
107 | if (name == NULL) |
103 | if (name == NULL) |
108 | return NULL; |
104 | return NULL; |
109 | |
105 | |
110 | for (at = first_archetype; at != NULL; at = at->next) |
106 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
172 | * but it otherwise had a big memory leak. |
168 | * but it otherwise had a big memory leak. |
173 | */ |
169 | */ |
174 | object * |
170 | object * |
175 | get_archetype_by_object_name (const char *name) |
171 | get_archetype_by_object_name (const char *name) |
176 | { |
172 | { |
177 | archetype * |
173 | archetype *at; |
178 | at; |
|
|
179 | char |
|
|
180 | tmpname[MAX_BUF]; |
174 | char tmpname[MAX_BUF]; |
181 | int |
175 | int i; |
182 | i; |
|
|
183 | |
176 | |
184 | strncpy (tmpname, name, MAX_BUF - 1); |
177 | assign (tmpname, name); |
185 | tmpname[MAX_BUF - 1] = 0; |
178 | |
186 | for (i = strlen (tmpname); i > 0; i--) |
179 | for (i = strlen (tmpname); i > 0; i--) |
187 | { |
180 | { |
188 | tmpname[i] = 0; |
181 | tmpname[i] = 0; |
189 | at = find_archetype_by_object_name (tmpname); |
182 | at = find_archetype_by_object_name (tmpname); |
|
|
183 | |
190 | if (at != NULL) |
184 | if (at != NULL) |
191 | { |
|
|
192 | return arch_to_object (at); |
185 | return arch_to_object (at); |
193 | } |
|
|
194 | } |
186 | } |
|
|
187 | |
195 | return create_singularity (name); |
188 | return create_singularity (name); |
196 | } |
189 | } |
197 | |
190 | |
198 | /* 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 |
199 | * a string seperated lists of things to match, with certain keywords. |
192 | * a string seperated lists of things to match, with certain keywords. |
… | |
… | |
330 | void |
323 | void |
331 | init_archetypes (void) |
324 | init_archetypes (void) |
332 | { /* called from add_player() and edit() */ |
325 | { /* called from add_player() and edit() */ |
333 | if (first_archetype != NULL) /* Only do this once */ |
326 | if (first_archetype != NULL) /* Only do this once */ |
334 | return; |
327 | return; |
|
|
328 | |
335 | arch_init = 1; |
329 | arch_init = 1; |
336 | load_archetypes (); |
330 | load_archetypes (); |
337 | arch_init = 0; |
331 | arch_init = 0; |
338 | empty_archetype = find_archetype ("empty_archetype"); |
332 | empty_archetype = archetype::find ("empty_archetype"); |
339 | |
333 | |
340 | /* init_blocksview();*/ |
334 | /* init_blocksview();*/ |
341 | } |
335 | } |
342 | |
336 | |
343 | /* |
337 | /* |
… | |
… | |
423 | if (at->more) |
417 | if (at->more) |
424 | next = at->more; |
418 | next = at->more; |
425 | else |
419 | else |
426 | next = at->next; |
420 | next = at->next; |
427 | |
421 | |
428 | delete at; |
422 | delete |
|
|
423 | at; |
|
|
424 | |
429 | i++; |
425 | i++; |
430 | } |
426 | } |
431 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
427 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
432 | } |
428 | } |
433 | |
429 | |
… | |
… | |
517 | at = new archetype; |
513 | at = new archetype; |
518 | |
514 | |
519 | at->clone.arch = at; |
515 | at->clone.arch = at; |
520 | } |
516 | } |
521 | |
517 | |
522 | delete |
518 | delete at; |
523 | at; |
|
|
524 | } |
519 | } |
525 | |
520 | |
526 | /* |
521 | /* |
527 | * Reads the archetype file once more, and links all pointers between |
522 | * Reads the archetype file once more, and links all pointers between |
528 | * archetypes. |
523 | * archetypes. |
… | |
… | |
551 | cp--; |
546 | cp--; |
552 | } |
547 | } |
553 | } |
548 | } |
554 | if (!strcmp ("Object", variable)) |
549 | if (!strcmp ("Object", variable)) |
555 | { |
550 | { |
556 | if ((at = find_archetype (argument)) == NULL) |
551 | if ((at = archetype::find (argument)) == NULL) |
557 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
552 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
558 | } |
553 | } |
559 | else if (!strcmp ("other_arch", variable)) |
554 | else if (!strcmp ("other_arch", variable)) |
560 | { |
555 | { |
561 | if (at != NULL && at->clone.other_arch == NULL) |
556 | if (at != NULL && at->clone.other_arch == NULL) |
562 | { |
557 | { |
563 | if ((other = find_archetype (argument)) == NULL) |
558 | if ((other = archetype::find (argument)) == NULL) |
564 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
559 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
565 | else if (at != NULL) |
560 | else if (at != NULL) |
566 | at->clone.other_arch = other; |
561 | at->clone.other_arch = other; |
567 | } |
562 | } |
568 | } |
563 | } |
… | |
… | |
616 | |
611 | |
617 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
612 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
618 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
613 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
619 | |
614 | |
620 | { |
615 | { |
|
|
616 | object_thawer |
621 | object_thawer thawer (filename); |
617 | thawer (filename); |
622 | |
618 | |
623 | clear_archetable (); |
619 | clear_archetable (); |
624 | LOG (llevDebug, " arch-pass 1...\n"); |
620 | LOG (llevDebug, " arch-pass 1...\n"); |
625 | first_arch_pass (thawer); |
621 | first_arch_pass (thawer); |
626 | LOG (llevDebug, " done\n"); |
622 | LOG (llevDebug, " done\n"); |
… | |
… | |
628 | |
624 | |
629 | init_archetable (); |
625 | init_archetable (); |
630 | warn_archetypes = 1; |
626 | warn_archetypes = 1; |
631 | |
627 | |
632 | { |
628 | { |
|
|
629 | object_thawer |
633 | object_thawer thawer (filename); |
630 | thawer (filename); |
634 | |
631 | |
635 | LOG (llevDebug, " loading treasure...\n"); |
632 | LOG (llevDebug, " loading treasure...\n"); |
636 | load_treasures (); |
633 | load_treasures (); |
637 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
634 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
638 | second_arch_pass (thawer); |
635 | second_arch_pass (thawer); |
… | |
… | |
678 | */ |
675 | */ |
679 | |
676 | |
680 | object * |
677 | object * |
681 | create_singularity (const char *name) |
678 | create_singularity (const char *name) |
682 | { |
679 | { |
683 | object * |
680 | object * op; |
684 | op; |
|
|
685 | char |
|
|
686 | buf[MAX_BUF]; |
681 | char buf[MAX_BUF]; |
687 | |
682 | |
688 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
683 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
689 | op = get_object (); |
684 | op = get_object (); |
690 | op->name = op->name_pl = buf; |
685 | op->name = op->name_pl = buf; |
691 | SET_FLAG (op, FLAG_NO_PICK); |
686 | SET_FLAG (op, FLAG_NO_PICK); |
… | |
… | |
698 | */ |
693 | */ |
699 | |
694 | |
700 | object * |
695 | object * |
701 | get_archetype (const char *name) |
696 | get_archetype (const char *name) |
702 | { |
697 | { |
703 | archetype * |
698 | archetype *at = archetype::find (name); |
704 | at; |
|
|
705 | |
699 | |
706 | at = find_archetype (name); |
700 | if (!at) |
707 | if (at == NULL) |
|
|
708 | return create_singularity (name); |
701 | return create_singularity (name); |
709 | |
702 | |
710 | return arch_to_object (at); |
703 | return arch_to_object (at); |
711 | } |
704 | } |
712 | |
705 | |
… | |
… | |
715 | */ |
708 | */ |
716 | |
709 | |
717 | unsigned long |
710 | unsigned long |
718 | hasharch (const char *str, int tablesize) |
711 | hasharch (const char *str, int tablesize) |
719 | { |
712 | { |
720 | unsigned long |
713 | unsigned long hash = 0; |
721 | hash = 0; |
|
|
722 | unsigned int |
714 | unsigned int i = 0; |
723 | i = 0; |
|
|
724 | const char * |
715 | const char *p; |
725 | p; |
|
|
726 | |
716 | |
727 | /* use the one-at-a-time hash function, which supposedly is |
717 | /* use the one-at-a-time hash function, which supposedly is |
728 | * better than the djb2-like one used by perl5.005, but |
718 | * better than the djb2-like one used by perl5.005, but |
729 | * certainly is better then the bug used here before. |
719 | * certainly is better then the bug used here before. |
730 | * see http://burtleburtle.net/bob/hash/doobs.html |
720 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
747 | * Finds, using the hashtable, which archetype matches the given name. |
737 | * Finds, using the hashtable, which archetype matches the given name. |
748 | * returns a pointer to the found archetype, otherwise NULL. |
738 | * returns a pointer to the found archetype, otherwise NULL. |
749 | */ |
739 | */ |
750 | |
740 | |
751 | archetype * |
741 | archetype * |
752 | find_archetype (const char *name) |
742 | archetype::find (const char *name) |
753 | { |
743 | { |
754 | #if USE_UNORDERED_MAP |
|
|
755 | name = shstr::find (name); |
|
|
756 | |
|
|
757 | if (!name) |
744 | if (!name) |
758 | return 0; |
745 | return 0; |
759 | |
746 | |
760 | HT::const_iterator i = ht.find ((size_t) name); |
747 | #if USE_UNORDERED_MAP |
|
|
748 | AUTODECL (i, ht.find ((size_t) name)); |
761 | |
749 | |
762 | if (i == ht.end ()) |
750 | if (i == ht.end ()) |
763 | return 0; |
751 | return 0; |
764 | else |
752 | else |
765 | return i->second; |
753 | return i->second; |
766 | #endif |
754 | #endif |
767 | |
755 | |
768 | archetype * |
756 | archetype *at; |
769 | at; |
|
|
770 | unsigned long |
757 | unsigned long index; |
771 | index; |
|
|
772 | |
|
|
773 | if (name == NULL) |
|
|
774 | return (archetype *) NULL; |
|
|
775 | |
758 | |
776 | index = hasharch (name, ARCHTABLE); |
759 | index = hasharch (name, ARCHTABLE); |
777 | arch_search++; |
760 | arch_search++; |
778 | for (;;) |
761 | for (;;) |
779 | { |
762 | { |
780 | at = arch_table[index]; |
763 | at = arch_table[index]; |
|
|
764 | |
781 | if (at == NULL) |
765 | if (at == NULL) |
782 | { |
766 | { |
783 | if (warn_archetypes) |
767 | if (warn_archetypes) |
784 | LOG (llevError, "Couldn't find archetype %s\n", name); |
768 | LOG (llevError, "Couldn't find archetype %s\n", name); |
|
|
769 | |
785 | return NULL; |
770 | return NULL; |
786 | } |
771 | } |
|
|
772 | |
787 | arch_cmp++; |
773 | arch_cmp++; |
|
|
774 | |
788 | if (!strcmp ((const char *) at->name, name)) |
775 | if (!strcmp ((const char *) at->name, name)) |
789 | return at; |
776 | return at; |
|
|
777 | |
790 | if (++index >= ARCHTABLE) |
778 | if (++index >= ARCHTABLE) |
791 | index = 0; |
779 | index = 0; |
792 | } |
780 | } |
793 | } |
781 | } |
794 | |
782 | |
… | |
… | |
801 | { |
789 | { |
802 | #if USE_UNORDERED_MAP |
790 | #if USE_UNORDERED_MAP |
803 | 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)); |
804 | #endif |
792 | #endif |
805 | |
793 | |
806 | int |
|
|
807 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
794 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
808 | |
795 | |
809 | for (;;) |
796 | for (;;) |
810 | { |
797 | { |
811 | if (arch_table[index] == NULL) |
798 | if (arch_table[index] == NULL) |
812 | { |
799 | { |
… | |
… | |
828 | */ |
815 | */ |
829 | |
816 | |
830 | archetype * |
817 | archetype * |
831 | type_to_archetype (int type) |
818 | type_to_archetype (int type) |
832 | { |
819 | { |
833 | archetype * |
820 | archetype *at; |
834 | at; |
|
|
835 | |
821 | |
836 | 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) |
837 | if (at->clone.type == type) |
823 | if (at->clone.type == type) |
838 | return at; |
824 | return at; |
|
|
825 | |
839 | return NULL; |
826 | return 0; |
840 | } |
827 | } |
841 | |
828 | |
842 | /* |
829 | /* |
843 | * Returns a new object copied from the first archetype matching |
830 | * Returns a new object copied from the first archetype matching |
844 | * the given type. |
831 | * the given type. |
… | |
… | |
846 | */ |
833 | */ |
847 | |
834 | |
848 | object * |
835 | object * |
849 | clone_arch (int type) |
836 | clone_arch (int type) |
850 | { |
837 | { |
851 | archetype * |
838 | archetype *at; |
852 | at; |
|
|
853 | object * |
|
|
854 | op = get_object (); |
839 | object *op = get_object (); |
855 | |
840 | |
856 | if ((at = type_to_archetype (type)) == NULL) |
841 | if ((at = type_to_archetype (type)) == NULL) |
857 | { |
842 | { |
858 | LOG (llevError, "Can't clone archetype %d\n", type); |
843 | LOG (llevError, "Can't clone archetype %d\n", type); |
859 | free_object (op); |
844 | free_object (op); |
860 | return NULL; |
845 | return NULL; |
861 | } |
846 | } |
|
|
847 | |
862 | copy_object (&at->clone, op); |
848 | copy_object (&at->clone, op); |
863 | op->instantiate (); |
849 | op->instantiate (); |
864 | return op; |
850 | return op; |
865 | } |
851 | } |
866 | |
852 | |
… | |
… | |
869 | */ |
855 | */ |
870 | |
856 | |
871 | object * |
857 | object * |
872 | object_create_arch (archetype *at) |
858 | object_create_arch (archetype *at) |
873 | { |
859 | { |
874 | object * |
860 | object *op, *prev = 0, *head = 0; |
875 | op, * |
|
|
876 | prev = NULL, *head = NULL; |
|
|
877 | |
861 | |
878 | while (at) |
862 | while (at) |
879 | { |
863 | { |
880 | op = arch_to_object (at); |
864 | op = arch_to_object (at); |
881 | op->x = at->clone.x; |
865 | op->x = at->clone.x; |
882 | op->y = at->clone.y; |
866 | op->y = at->clone.y; |
|
|
867 | |
883 | if (head) |
868 | if (head) |
884 | op->head = head, prev->more = op; |
869 | op->head = head, prev->more = op; |
|
|
870 | |
885 | if (!head) |
871 | if (!head) |
886 | head = op; |
872 | head = op; |
|
|
873 | |
887 | prev = op; |
874 | prev = op; |
888 | at = at->more; |
875 | at = at->more; |
889 | } |
876 | } |
|
|
877 | |
890 | return (head); |
878 | return (head); |
891 | } |
879 | } |
892 | |
880 | |
893 | /*** end of arch.c ***/ |
|
|