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.36 by pippijn, Sat Jan 6 14:42:28 2007 UTC vs.
Revision 1.39 by root, Fri Feb 2 22:07:51 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * CrossFire, A Multiplayer game for X-windows
3 3 *
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
6 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (C) 1992 Frank Tore Johansen
7 7 *
8 This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version. 11 * (at your option) any later version.
12 12 *
13 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 16 * GNU General Public License for more details.
17 17 *
18 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 21 *
22 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail at <crossfire@schmorp.de>
23*/ 23 */
24 24
25#include <cassert> 25#include <cassert>
26 26
27#include <global.h> 27#include <global.h>
28#include <funcpoint.h> 28#include <funcpoint.h>
29#include <loader.h> 29#include <loader.h>
30 30
31#define USE_UNORDERED_MAP 0
32
33#if USE_UNORDERED_MAP
34# include <tr1/functional> 31#include <tr1/functional>
35# include <tr1/unordered_map> 32#include <tr1/unordered_map>
36#endif
37 33
38int arch_cmp = 0; /* How many strcmp's */
39int arch_search = 0; /* How many searches */
40int arch_init; /* True if doing arch initialization */ 34int arch_init; /* True if doing arch initialization */
41 35
42/* The naming of these functions is really poor - they are all 36/* The naming of these functions is really poor - they are all
43 * pretty much named '.._arch_...', but they may more may not 37 * pretty much named '.._arch_...', but they may more may not
44 * return archetypes. Some make the arch_to_object call, and thus 38 * return archetypes. Some make the arch_to_object call, and thus
45 * return an object. Perhaps those should be called 'archob' functions 39 * return an object. Perhaps those should be called 'archob' functions
46 * to denote they return an object derived from the archetype. 40 * to denote they return an object derived from the archetype.
47 * MSW 2003-04-29 41 * MSW 2003-04-29
48 */ 42 */
49 43
50#if USE_UNORDERED_MAP
51// the hashtable 44// the hashtable
52typedef std::tr1::unordered_map 45typedef std::tr1::unordered_map
53 < 46 <
54 std::size_t, 47 const char *,
55 arch_ptr, 48 arch_ptr,
56 std::tr1::hash<size_t>, 49 str_hash,
57 std::equal_to<size_t>, 50 str_equal,
58 slice_allocator< std::pair<const std::size_t, archetype *> > 51 slice_allocator< std::pair<const char *const, arch_ptr> >
59 true,
60 > HT; 52 > HT;
61 53
62static HT ht; 54static HT ht (5000);
63#else
64static arch_ptr arch_table[ARCHTABLE];
65#endif
66 55
67/** 56/**
68 * GROS - This function retrieves an archetype given the name that appears 57 * GROS - This function retrieves an archetype given the name that appears
69 * during the game (for example, "writing pen" instead of "stylus"). 58 * during the game (for example, "writing pen" instead of "stylus").
70 * It does not use the hashtable system, but browse the whole archlist each time. 59 * It does not use the hashtable system, but browse the whole archlist each time.
76 * - the archetype found or null if nothing was found. 65 * - the archetype found or null if nothing was found.
77 */ 66 */
78archetype * 67archetype *
79find_archetype_by_object_name (const char *name) 68find_archetype_by_object_name (const char *name)
80{ 69{
81 archetype *at; 70 shstr_cmp name_cmp (name);
82 71
83 if (name == NULL)
84 return (archetype *) NULL;
85
86 for (at = first_archetype; at != NULL; at = at->next) 72 for (archetype *at = first_archetype; at; at = at->next)
87 { 73 if (at->clone.name == name_cmp)
88 if (!strcmp (at->clone.name, name))
89 return at; 74 return at;
90 } 75
91 return NULL; 76 return 0;
92} 77}
93 78
94/** 79/**
95 * This function retrieves an archetype by type and name that appears during 80 * This function retrieves an archetype by type and name that appears during
96 * the game. It is basically the same as find_archetype_by_object_name() 81 * the game. It is basically the same as find_archetype_by_object_name()
97 * except that it considers only items of the given type. 82 * except that it considers only items of the given type.
98 */ 83 */
99archetype * 84archetype *
100find_archetype_by_object_type_name (int type, const char *name) 85find_archetype_by_object_type_name (int type, const char *name)
101{ 86{
102 archetype *at; 87 shstr_cmp name_cmp (name);
103 88
104 if (name == NULL)
105 return NULL;
106
107 for (at = first_archetype; at != NULL; at = at->next) 89 for (archetype *at = first_archetype; at; at = at->next)
108 { 90 if (at->clone.name == name_cmp && at->clone.type == type)
109 if (at->clone.type == type && strcmp (at->clone.name, name) == 0)
110 return at; 91 return at;
111 }
112 92
113 return NULL; 93 return 0;
114} 94}
115 95
116/* This is a lot like the above function. Instead, we are trying to match 96/* This is a lot like the above function. Instead, we are trying to match
117 * the arch->skill values. type is the type of object to match 97 * the arch->skill values. type is the type of object to match
118 * against (eg, to only match against skills or only skill objects for example). 98 * against (eg, to only match against skills or only skill objects for example).
119 * If type is -1, ew don't match on type. 99 * If type is -1, ew don't match on type.
120 */ 100 */
121object * 101object *
122get_archetype_by_skill_name (const char *skill, int type) 102get_archetype_by_skill_name (const char *skill, int type)
123{ 103{
124 archetype *at; 104 shstr_cmp skill_cmp (skill);
125 105
126 if (skill)
127 for (at = first_archetype; at; at = at->next) 106 for (archetype *at = first_archetype; at; at = at->next)
128 if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) 107 if (at->clone.skill == skill_cmp && (type == -1 || type == at->clone.type))
129 return arch_to_object (at); 108 return arch_to_object (at);
130 109
131 return 0; 110 return 0;
132} 111}
133 112
134/* similiar to above - this returns the first archetype 113/* similiar to above - this returns the first archetype
139 * (eg, skills) 118 * (eg, skills)
140 */ 119 */
141archetype * 120archetype *
142get_archetype_by_type_subtype (int type, int subtype) 121get_archetype_by_type_subtype (int type, int subtype)
143{ 122{
144 archetype *
145 at;
146
147 for (at = first_archetype; at != NULL; at = at->next) 123 for (archetype *at = first_archetype; at; at = at->next)
148 {
149 if (((type == -1) || (type == at->clone.type)) && (subtype == -1 || subtype == at->clone.subtype)) 124 if ((type == -1 || type == at->clone.type) && (subtype == -1 || subtype == at->clone.subtype))
150 return at; 125 return at;
151 } 126
152 return NULL; 127 return 0;
153} 128}
154 129
155/** 130/**
156 * GROS - this returns a new object given the name that appears during the game 131 * GROS - this returns a new object given the name that appears during the game
157 * (for example, "writing pen" instead of "stylus"). 132 * (for example, "writing pen" instead of "stylus").
165 * but it otherwise had a big memory leak. 140 * but it otherwise had a big memory leak.
166 */ 141 */
167object * 142object *
168get_archetype_by_object_name (const char *name) 143get_archetype_by_object_name (const char *name)
169{ 144{
170 archetype *at;
171 char tmpname[MAX_BUF]; 145 char tmpname[MAX_BUF];
172 int i; 146 int i;
173 147
174 assign (tmpname, name); 148 assign (tmpname, name);
175 149
176 for (i = strlen (tmpname); i > 0; i--) 150 for (i = strlen (tmpname); i > 0; i--)
177 { 151 {
178 tmpname[i] = 0; 152 tmpname[i] = 0;
153
179 at = find_archetype_by_object_name (tmpname); 154 if (archetype *at = find_archetype_by_object_name (tmpname))
180
181 if (at)
182 return arch_to_object (at); 155 return arch_to_object (at);
183 } 156 }
184 157
185 return create_singularity (name); 158 return create_singularity (name);
186} 159}
322 295
323/* init_blocksview();*/ 296/* init_blocksview();*/
324} 297}
325 298
326/* 299/*
327 * Stores debug-information about how efficient the hashtable
328 * used for archetypes has been in the static errmsg array.
329 */
330
331void
332arch_info (object *op)
333{
334 sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp);
335 new_draw_info (NDI_BLACK, 0, op, errmsg);
336}
337
338/*
339 * Initialise the hashtable used by the archetypes.
340 */
341
342void
343clear_archetable (void)
344{
345 memset ((void *) arch_table, 0, ARCHTABLE * sizeof (archetype *));
346}
347
348/*
349 * An alternative way to init the hashtable which is slower, but _works_... 300 * An alternative way to init the hashtable which is slower, but _works_...
350 */ 301 */
351
352void 302void
353init_archetable (void) 303init_archetable (void)
354{ 304{
355 archetype *at; 305 archetype *at;
356 306
357 LOG (llevDebug, " Setting up archetable...\n"); 307 LOG (llevDebug, " Setting up archetable...\n");
358 308
359 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) 309 for (at = first_archetype; at; at = at->more ? at->more : at->next)
360 at->hash_add (); 310 at->hash_add ();
361 311
362 LOG (llevDebug, "done\n"); 312 LOG (llevDebug, "done\n");
363} 313}
364 314
366free_all_archs (void) 316free_all_archs (void)
367{ 317{
368 archetype *at, *next; 318 archetype *at, *next;
369 int i = 0, f = 0; 319 int i = 0, f = 0;
370 320
371 for (at = first_archetype; at != NULL; at = next) 321 for (at = first_archetype; at; at = next)
372 { 322 {
373 if (at->more) 323 if (at->more)
374 next = at->more; 324 next = at->more;
375 else 325 else
376 next = at->next; 326 next = at->next;
557 507
558 { 508 {
559 object_thawer 509 object_thawer
560 thawer (filename); 510 thawer (filename);
561 511
562 clear_archetable ();
563 LOG (llevDebug, " arch-pass 1...\n"); 512 LOG (llevDebug, " arch-pass 1...\n");
564 first_arch_pass (thawer); 513 first_arch_pass (thawer);
565 LOG (llevDebug, " done\n"); 514 LOG (llevDebug, " done\n");
566 } 515 }
567 516
674 623
675/* 624/*
676 * Finds, using the hashtable, which archetype matches the given name. 625 * Finds, using the hashtable, which archetype matches the given name.
677 * returns a pointer to the found archetype, otherwise NULL. 626 * returns a pointer to the found archetype, otherwise NULL.
678 */ 627 */
679
680archetype * 628archetype *
681archetype::find (const char *name) 629archetype::find (const char *name)
682{ 630{
683 if (!name) 631 if (!name)
684 return 0; 632 return 0;
685 633
686#if USE_UNORDERED_MAP
687 AUTODECL (i, ht.find ((size_t) name)); 634 AUTODECL (i, ht.find (name));
688 635
689 if (i == ht.end ()) 636 if (i == ht.end ())
690 return 0; 637 return 0;
691 else 638 else
692 return i->second; 639 return i->second;
693#endif
694
695 archetype *at;
696 unsigned long index;
697
698 index = hasharch (name, ARCHTABLE);
699 arch_search++;
700 for (;;)
701 {
702 at = arch_table[index];
703
704 if (at == NULL)
705 {
706 if (warn_archetypes)
707 LOG (llevError, "Couldn't find archetype %s\n", name);
708
709 return NULL;
710 }
711
712 arch_cmp++;
713
714 if (!strcmp ((const char *) at->name, name))
715 return at;
716
717 if (++index >= ARCHTABLE)
718 index = 0;
719 }
720} 640}
721 641
722/* 642/*
723 * Adds an archetype to the hashtable. 643 * Adds an archetype to the hashtable.
724 */ 644 */
725void 645void
726archetype::hash_add () 646archetype::hash_add ()
727{ 647{
728#if USE_UNORDERED_MAP
729 ht.insert (std::make_pair ((size_t) (const char *) name, this)); 648 ht.insert (std::make_pair (name, this));
730#else
731
732 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
733
734 for (;;)
735 {
736 if (!arch_table[index])
737 {
738 arch_table[index] = this;
739 break;
740 }
741
742 if (++index == ARCHTABLE)
743 index = 0;
744
745 if (index == org_index)
746 fatal (ARCHTABLE_TOO_SMALL);
747 }
748#endif
749} 649}
750 650
751void 651void
752archetype::hash_del () 652archetype::hash_del ()
753{ 653{
754#if USE_UNORDERED_MAP 654 ht.erase (name);
755# error remove this from HT
756#else
757
758 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
759
760 for (;;)
761 {
762 if (arch_table[index] == this)
763 {
764 arch_table[index] = 0;
765 break;
766 }
767
768 if (++index == ARCHTABLE)
769 index = 0;
770
771 if (index == org_index)
772 break;
773 }
774#endif
775} 655}
776 656
777/* 657/*
778 * Returns the first archetype using the given type. 658 * Returns the first archetype using the given type.
779 * Used in treasure-generation. 659 * Used in treasure-generation.
780 */ 660 */
781
782archetype * 661archetype *
783type_to_archetype (int type) 662type_to_archetype (int type)
784{ 663{
785 archetype *at; 664 archetype *at;
786 665

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines