… | |
… | |
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 | |
38 | int arch_cmp = 0; /* How many strcmp's */ |
|
|
39 | int arch_search = 0; /* How many searches */ |
|
|
40 | int arch_init; /* True if doing arch initialization */ |
34 | int 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 |
52 | typedef std::tr1::unordered_map |
45 | typedef 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 | |
62 | static HT ht; |
54 | static HT ht (5000); |
63 | #else |
|
|
64 | static 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 | */ |
78 | archetype * |
67 | archetype * |
79 | find_archetype_by_object_name (const char *name) |
68 | find_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 | */ |
99 | archetype * |
84 | archetype * |
100 | find_archetype_by_object_type_name (int type, const char *name) |
85 | find_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 | */ |
121 | object * |
101 | object * |
122 | get_archetype_by_skill_name (const char *skill, int type) |
102 | get_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 | */ |
141 | archetype * |
120 | archetype * |
142 | get_archetype_by_type_subtype (int type, int subtype) |
121 | get_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 | */ |
167 | object * |
142 | object * |
168 | get_archetype_by_object_name (const char *name) |
143 | get_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 | } |
… | |
… | |
206 | item_matched_string (object *pl, object *op, const char *name) |
179 | item_matched_string (object *pl, object *op, const char *name) |
207 | { |
180 | { |
208 | char *cp, local_name[MAX_BUF]; |
181 | char *cp, local_name[MAX_BUF]; |
209 | int count, retval = 0; |
182 | int count, retval = 0; |
210 | |
183 | |
211 | strcpy (local_name, name); /* strtok is destructive to name */ |
184 | assign (local_name, name); /* strtok is destructive to name */ |
212 | |
185 | |
213 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
186 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
214 | { |
187 | { |
215 | while (cp[0] == ' ') |
188 | while (cp[0] == ' ') |
216 | ++cp; /* get rid of spaces */ |
189 | ++cp; /* get rid of spaces */ |
… | |
… | |
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 | |
|
|
331 | void |
|
|
332 | arch_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 | |
|
|
342 | void |
|
|
343 | clear_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 | |
|
|
352 | void |
302 | void |
353 | init_archetable (void) |
303 | init_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 | |
… | |
… | |
366 | free_all_archs (void) |
316 | free_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; |
… | |
… | |
496 | { |
446 | { |
497 | *cp = '\0'; |
447 | *cp = '\0'; |
498 | cp--; |
448 | cp--; |
499 | } |
449 | } |
500 | } |
450 | } |
501 | if (!strcmp ("Object", variable)) |
451 | if (!strcmp ("object", variable)) |
502 | { |
452 | { |
503 | if ((at = archetype::find (argument)) == NULL) |
453 | if ((at = archetype::find (argument)) == NULL) |
504 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
454 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
505 | } |
455 | } |
506 | else if (!strcmp ("other_arch", variable)) |
456 | else if (!strcmp ("other_arch", variable)) |
… | |
… | |
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 | |
|
|
680 | archetype * |
628 | archetype * |
681 | archetype::find (const char *name) |
629 | archetype::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 | */ |
725 | void |
645 | void |
726 | archetype::hash_add () |
646 | archetype::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 | |
751 | void |
651 | void |
752 | archetype::hash_del () |
652 | archetype::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 | |
|
|
782 | archetype * |
661 | archetype * |
783 | type_to_archetype (int type) |
662 | type_to_archetype (int type) |
784 | { |
663 | { |
785 | archetype *at; |
664 | archetype *at; |
786 | |
665 | |