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.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>
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
327void 323void
328init_archetypes (void) 324init_archetypes (void)
329{ /* called from add_player() and edit() */ 325{ /* called from add_player() and edit() */
330 if (first_archetype != NULL) /* Only do this once */ 326 if (first_archetype != NULL) /* Only do this once */
331 return; 327 return;
328
332 arch_init = 1; 329 arch_init = 1;
333 load_archetypes (); 330 load_archetypes ();
334 arch_init = 0; 331 arch_init = 0;
335 empty_archetype = find_archetype ("empty_archetype"); 332 empty_archetype = archetype::find ("empty_archetype");
336 333
337/* init_blocksview();*/ 334/* init_blocksview();*/
338} 335}
339 336
340/* 337/*
549 cp--; 546 cp--;
550 } 547 }
551 } 548 }
552 if (!strcmp ("Object", variable)) 549 if (!strcmp ("Object", variable))
553 { 550 {
554 if ((at = find_archetype (argument)) == NULL) 551 if ((at = archetype::find (argument)) == NULL)
555 LOG (llevError, "Warning: failed to find arch %s\n", argument); 552 LOG (llevError, "Warning: failed to find arch %s\n", argument);
556 } 553 }
557 else if (!strcmp ("other_arch", variable)) 554 else if (!strcmp ("other_arch", variable))
558 { 555 {
559 if (at != NULL && at->clone.other_arch == NULL) 556 if (at != NULL && at->clone.other_arch == NULL)
560 { 557 {
561 if ((other = find_archetype (argument)) == NULL) 558 if ((other = archetype::find (argument)) == NULL)
562 LOG (llevError, "Warning: failed to find other_arch %s\n", argument); 559 LOG (llevError, "Warning: failed to find other_arch %s\n", argument);
563 else if (at != NULL) 560 else if (at != NULL)
564 at->clone.other_arch = other; 561 at->clone.other_arch = other;
565 } 562 }
566 } 563 }
678 */ 675 */
679 676
680object * 677object *
681create_singularity (const char *name) 678create_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
700object * 695object *
701get_archetype (const char *name) 696get_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
717unsigned long 710unsigned long
718hasharch (const char *str, int tablesize) 711hasharch (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
751archetype * 741archetype *
752find_archetype (const char *name) 742archetype::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
830archetype * 817archetype *
831type_to_archetype (int type) 818type_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
848object * 835object *
849clone_arch (int type) 836clone_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
871object * 857object *
872object_create_arch (archetype *at) 858object_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 ***/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines