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> |
|
|
33 | |
|
|
34 | #include <global.h> |
26 | #include <global.h> |
35 | #include <arch.h> |
|
|
36 | #include <funcpoint.h> |
27 | #include <funcpoint.h> |
37 | #include <loader.h> |
28 | #include <loader.h> |
38 | |
29 | |
39 | /* IF set, does a little timing on the archetype load. */ |
30 | #define USE_UNORDERED_MAP 0 |
40 | #define TIME_ARCH_LOAD 0 |
|
|
41 | |
31 | |
42 | static void add_arch (archetype *at); |
32 | #if USE_UNORDERED_MAP |
|
|
33 | # include <tr1/functional> |
|
|
34 | # include <tr1/unordered_map> |
|
|
35 | #endif |
43 | |
36 | |
44 | static archetype *arch_table[ARCHTABLE]; |
|
|
45 | int arch_cmp = 0; /* How many strcmp's */ |
37 | int arch_cmp = 0; /* How many strcmp's */ |
46 | int arch_search = 0; /* How many searches */ |
38 | int arch_search = 0; /* How many searches */ |
47 | int arch_init; /* True if doing arch initialization */ |
39 | int arch_init; /* True if doing arch initialization */ |
48 | |
40 | |
49 | /* The naming of these functions is really poor - they are all |
41 | /* The naming of these functions is really poor - they are all |
… | |
… | |
54 | * MSW 2003-04-29 |
46 | * MSW 2003-04-29 |
55 | */ |
47 | */ |
56 | |
48 | |
57 | #if USE_UNORDERED_MAP |
49 | #if USE_UNORDERED_MAP |
58 | // the hashtable |
50 | // the hashtable |
59 | typedef |
|
|
60 | std::tr1::unordered_map < |
51 | typedef std::tr1::unordered_map |
61 | size_t, archetype *> |
52 | < |
|
|
53 | std::size_t, |
|
|
54 | arch_ptr, |
|
|
55 | std::tr1::hash<size_t>, |
|
|
56 | std::equal_to<size_t>, |
|
|
57 | slice_allocator< std::pair<const std::size_t, archetype *> > |
|
|
58 | true, |
62 | HT; |
59 | > HT; |
63 | |
60 | |
64 | static |
61 | static HT ht; |
65 | HT |
62 | #else |
66 | ht; |
63 | static arch_ptr arch_table[ARCHTABLE]; |
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) |
… | |
… | |
123 | * If type is -1, ew don't match on type. |
118 | * If type is -1, ew don't match on type. |
124 | */ |
119 | */ |
125 | object * |
120 | object * |
126 | get_archetype_by_skill_name (const char *skill, int type) |
121 | get_archetype_by_skill_name (const char *skill, int type) |
127 | { |
122 | { |
128 | archetype * |
123 | archetype *at; |
129 | at; |
|
|
130 | |
124 | |
131 | if (skill == NULL) |
125 | if (skill) |
132 | return NULL; |
|
|
133 | |
|
|
134 | for (at = first_archetype; at != NULL; at = at->next) |
126 | for (at = first_archetype; at; at = at->next) |
135 | { |
|
|
136 | if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) |
127 | if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) |
137 | return arch_to_object (at); |
128 | return arch_to_object (at); |
138 | } |
129 | |
139 | return NULL; |
130 | return 0; |
140 | } |
131 | } |
141 | |
132 | |
142 | /* similiar to above - this returns the first archetype |
133 | /* similiar to above - this returns the first archetype |
143 | * that matches both the type and subtype. type and subtype |
134 | * that matches both the type and subtype. type and subtype |
144 | * can be -1 to say ignore, but in this case, the match it does |
135 | * can be -1 to say ignore, but in this case, the match it does |
… | |
… | |
173 | * but it otherwise had a big memory leak. |
164 | * but it otherwise had a big memory leak. |
174 | */ |
165 | */ |
175 | object * |
166 | object * |
176 | get_archetype_by_object_name (const char *name) |
167 | get_archetype_by_object_name (const char *name) |
177 | { |
168 | { |
178 | archetype * |
169 | archetype *at; |
179 | at; |
|
|
180 | char |
|
|
181 | tmpname[MAX_BUF]; |
170 | char tmpname[MAX_BUF]; |
182 | int |
171 | int i; |
183 | i; |
|
|
184 | |
172 | |
185 | strncpy (tmpname, name, MAX_BUF - 1); |
173 | assign (tmpname, name); |
186 | tmpname[MAX_BUF - 1] = 0; |
174 | |
187 | for (i = strlen (tmpname); i > 0; i--) |
175 | for (i = strlen (tmpname); i > 0; i--) |
188 | { |
176 | { |
189 | tmpname[i] = 0; |
177 | tmpname[i] = 0; |
190 | at = find_archetype_by_object_name (tmpname); |
178 | at = find_archetype_by_object_name (tmpname); |
|
|
179 | |
191 | if (at != NULL) |
180 | if (at) |
192 | { |
|
|
193 | return arch_to_object (at); |
181 | return arch_to_object (at); |
194 | } |
|
|
195 | } |
182 | } |
|
|
183 | |
196 | return create_singularity (name); |
184 | return create_singularity (name); |
197 | } |
185 | } |
198 | |
186 | |
199 | /* This is a subset of the parse_id command. Basically, name can be |
187 | /* 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. |
188 | * a string seperated lists of things to match, with certain keywords. |
… | |
… | |
214 | * Last, make a check on the full name. |
202 | * Last, make a check on the full name. |
215 | */ |
203 | */ |
216 | int |
204 | int |
217 | item_matched_string (object *pl, object *op, const char *name) |
205 | item_matched_string (object *pl, object *op, const char *name) |
218 | { |
206 | { |
219 | char * |
|
|
220 | cp, |
|
|
221 | local_name[MAX_BUF]; |
207 | char *cp, local_name[MAX_BUF]; |
222 | int |
208 | int count, retval = 0; |
223 | count, |
|
|
224 | retval = 0; |
|
|
225 | |
209 | |
226 | strcpy (local_name, name); /* strtok is destructive to name */ |
210 | strcpy (local_name, name); /* strtok is destructive to name */ |
227 | |
211 | |
228 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
212 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
229 | { |
213 | { |
… | |
… | |
281 | retval = 15; |
265 | retval = 15; |
282 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
266 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
283 | retval = 14; |
267 | retval = 14; |
284 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
268 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
285 | retval = 14; |
269 | retval = 14; |
286 | |
|
|
287 | /* Do substring checks, so things like 'Str+1' will match. |
270 | /* Do substring checks, so things like 'Str+1' will match. |
288 | * retval of these should perhaps be lower - they are lower |
271 | * retval of these should perhaps be lower - they are lower |
289 | * then the specific strcasecmp aboves, but still higher than |
272 | * then the specific strcasecmp aboves, but still higher than |
290 | * some other match criteria. |
273 | * some other match criteria. |
291 | */ |
274 | */ |
… | |
… | |
293 | retval = 12; |
276 | retval = 12; |
294 | else if (strstr (query_base_name (op, 0), cp)) |
277 | else if (strstr (query_base_name (op, 0), cp)) |
295 | retval = 12; |
278 | retval = 12; |
296 | else if (strstr (query_short_name (op), cp)) |
279 | else if (strstr (query_short_name (op), cp)) |
297 | retval = 12; |
280 | retval = 12; |
298 | |
|
|
299 | /* Check against plural/non plural based on count. */ |
281 | /* Check against plural/non plural based on count. */ |
300 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
282 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
301 | { |
|
|
302 | retval = 6; |
283 | retval = 6; |
303 | } |
|
|
304 | else if (count == 1 && !strcasecmp (op->name, cp)) |
284 | else if (count == 1 && !strcasecmp (op->name, cp)) |
305 | { |
|
|
306 | retval = 6; |
285 | retval = 6; |
307 | } |
|
|
308 | /* base name matched - not bad */ |
286 | /* base name matched - not bad */ |
309 | else if (strcasecmp (cp, op->name) == 0 && !count) |
287 | else if (strcasecmp (cp, op->name) == 0 && !count) |
310 | retval = 4; |
288 | retval = 4; |
311 | /* Check for partial custom name, but give a real low priority */ |
289 | /* Check for partial custom name, but give a real low priority */ |
312 | else if (op->custom_name && strstr (op->custom_name, cp)) |
290 | else if (op->custom_name && strstr (op->custom_name, cp)) |
… | |
… | |
314 | |
292 | |
315 | if (retval) |
293 | if (retval) |
316 | { |
294 | { |
317 | if (pl->type == PLAYER) |
295 | if (pl->type == PLAYER) |
318 | pl->contr->count = count; |
296 | pl->contr->count = count; |
|
|
297 | |
319 | return retval; |
298 | return retval; |
320 | } |
299 | } |
321 | } |
300 | } |
|
|
301 | |
322 | return 0; |
302 | return 0; |
323 | } |
303 | } |
324 | |
304 | |
325 | /* |
305 | /* |
326 | * Initialises the internal linked list of archetypes (read from file). |
306 | * Initialises the internal linked list of archetypes (read from file). |
… | |
… | |
331 | void |
311 | void |
332 | init_archetypes (void) |
312 | init_archetypes (void) |
333 | { /* called from add_player() and edit() */ |
313 | { /* called from add_player() and edit() */ |
334 | if (first_archetype != NULL) /* Only do this once */ |
314 | if (first_archetype != NULL) /* Only do this once */ |
335 | return; |
315 | return; |
|
|
316 | |
336 | arch_init = 1; |
317 | arch_init = 1; |
337 | load_archetypes (); |
318 | load_archetypes (); |
338 | arch_init = 0; |
319 | arch_init = 0; |
339 | empty_archetype = find_archetype ("empty_archetype"); |
320 | empty_archetype = archetype::find ("empty_archetype"); |
340 | |
321 | |
341 | /* init_blocksview();*/ |
322 | /* init_blocksview();*/ |
342 | } |
323 | } |
343 | |
324 | |
344 | /* |
325 | /* |
… | |
… | |
368 | */ |
349 | */ |
369 | |
350 | |
370 | void |
351 | void |
371 | init_archetable (void) |
352 | init_archetable (void) |
372 | { |
353 | { |
373 | archetype * |
354 | archetype *at; |
374 | at; |
|
|
375 | |
355 | |
376 | LOG (llevDebug, " Setting up archetable...\n"); |
356 | LOG (llevDebug, " Setting up archetable...\n"); |
377 | |
357 | |
378 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
358 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
379 | add_arch (at); |
359 | at->hash_add (); |
380 | |
360 | |
381 | LOG (llevDebug, "done\n"); |
361 | LOG (llevDebug, "done\n"); |
382 | } |
362 | } |
383 | |
363 | |
384 | /* |
|
|
385 | * Dumps an archetype to debug-level output. |
|
|
386 | */ |
|
|
387 | |
|
|
388 | void |
|
|
389 | dump_arch (archetype *at) |
|
|
390 | { |
|
|
391 | dump_object (&at->clone); |
|
|
392 | } |
|
|
393 | |
|
|
394 | /* |
|
|
395 | * Dumps _all_ archetypes to debug-level output. |
|
|
396 | * If you run crossfire with debug, and enter DM-mode, you can trigger |
|
|
397 | * this with the O key. |
|
|
398 | */ |
|
|
399 | |
|
|
400 | void |
|
|
401 | dump_all_archetypes (void) |
|
|
402 | { |
|
|
403 | archetype * |
|
|
404 | at; |
|
|
405 | |
|
|
406 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
|
|
407 | { |
|
|
408 | dump_arch (at); |
|
|
409 | fprintf (logfile, "%s\n", errmsg); |
|
|
410 | } |
|
|
411 | } |
|
|
412 | |
|
|
413 | void |
364 | void |
414 | free_all_archs (void) |
365 | free_all_archs (void) |
415 | { |
366 | { |
416 | archetype * |
367 | archetype *at, *next; |
417 | at, * |
|
|
418 | next; |
|
|
419 | int |
|
|
420 | i = 0, f = 0; |
368 | int i = 0, f = 0; |
421 | |
369 | |
422 | for (at = first_archetype; at != NULL; at = next) |
370 | for (at = first_archetype; at != NULL; at = next) |
423 | { |
371 | { |
424 | if (at->more) |
372 | if (at->more) |
425 | next = at->more; |
373 | next = at->more; |
… | |
… | |
429 | delete |
377 | delete |
430 | at; |
378 | at; |
431 | |
379 | |
432 | i++; |
380 | i++; |
433 | } |
381 | } |
|
|
382 | |
434 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
383 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
435 | } |
384 | } |
436 | |
385 | |
437 | archetype::archetype () |
386 | archetype::archetype () |
438 | { |
387 | { |
439 | clear_object (&clone); /* to initial state other also */ |
|
|
440 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ |
388 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ |
441 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
389 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
442 | } |
390 | } |
443 | |
391 | |
444 | archetype::~archetype () |
392 | archetype::~archetype () |
445 | { |
393 | { |
… | |
… | |
450 | * of archetype-structures. |
398 | * of archetype-structures. |
451 | */ |
399 | */ |
452 | void |
400 | void |
453 | first_arch_pass (object_thawer & fp) |
401 | first_arch_pass (object_thawer & fp) |
454 | { |
402 | { |
455 | archetype * |
403 | archetype *head = 0, *last_more = 0; |
456 | at, * |
|
|
457 | head = NULL, *last_more = NULL; |
|
|
458 | |
404 | |
|
|
405 | archetype *at = new archetype; |
459 | at->clone.arch = first_archetype = at = new archetype; |
406 | at->clone.arch = first_archetype = at; |
460 | |
407 | |
461 | while (int i = load_object (fp, &at->clone, 0)) |
408 | while (int i = load_object (fp, &at->clone, 0)) |
462 | { |
409 | { |
463 | at->clone.speed_left = (float) (-0.1); |
410 | at->clone.speed_left = (float) (-0.1); |
464 | /* copy the body_info to the body_used - this is only really |
411 | /* copy the body_info to the body_used - this is only really |
… | |
… | |
531 | */ |
478 | */ |
532 | |
479 | |
533 | void |
480 | void |
534 | second_arch_pass (object_thawer & thawer) |
481 | second_arch_pass (object_thawer & thawer) |
535 | { |
482 | { |
536 | char |
|
|
537 | buf[MAX_BUF], * |
|
|
538 | variable = buf, *argument, *cp; |
483 | char buf[MAX_BUF], *variable = buf, *argument, *cp; |
539 | archetype * |
|
|
540 | at = NULL, *other; |
484 | archetype *at = NULL, *other; |
541 | |
485 | |
542 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
486 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
543 | { |
487 | { |
544 | if (*buf == '#') |
488 | if (*buf == '#') |
545 | continue; |
489 | continue; |
… | |
… | |
553 | cp--; |
497 | cp--; |
554 | } |
498 | } |
555 | } |
499 | } |
556 | if (!strcmp ("Object", variable)) |
500 | if (!strcmp ("Object", variable)) |
557 | { |
501 | { |
558 | if ((at = find_archetype (argument)) == NULL) |
502 | if ((at = archetype::find (argument)) == NULL) |
559 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
503 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
560 | } |
504 | } |
561 | else if (!strcmp ("other_arch", variable)) |
505 | else if (!strcmp ("other_arch", variable)) |
562 | { |
506 | { |
563 | if (at != NULL && at->clone.other_arch == NULL) |
507 | if (at != NULL && at->clone.other_arch == NULL) |
564 | { |
508 | { |
565 | if ((other = find_archetype (argument)) == NULL) |
509 | if ((other = archetype::find (argument)) == NULL) |
566 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
510 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
567 | else if (at != NULL) |
511 | else if (at != NULL) |
568 | at->clone.other_arch = other; |
512 | at->clone.other_arch = other; |
569 | } |
513 | } |
570 | } |
514 | } |
571 | else if (!strcmp ("randomitems", variable)) |
515 | else if (!strcmp ("randomitems", variable)) |
572 | { |
516 | { |
573 | if (at != NULL) |
517 | if (at != NULL) |
574 | { |
518 | { |
575 | treasurelist * |
|
|
576 | tl = find_treasurelist (argument); |
519 | treasurelist *tl = find_treasurelist (argument); |
577 | |
520 | |
578 | if (tl == NULL) |
521 | if (tl == NULL) |
579 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
522 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
580 | else |
523 | else |
581 | at->clone.randomitems = tl; |
524 | at->clone.randomitems = tl; |
… | |
… | |
586 | |
529 | |
587 | #ifdef DEBUG |
530 | #ifdef DEBUG |
588 | void |
531 | void |
589 | check_generators (void) |
532 | check_generators (void) |
590 | { |
533 | { |
591 | archetype * |
534 | archetype *at; |
592 | at; |
|
|
593 | |
535 | |
594 | for (at = first_archetype; at != NULL; at = at->next) |
536 | for (at = first_archetype; at != NULL; at = at->next) |
595 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
537 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
596 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
538 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
597 | } |
539 | } |
… | |
… | |
605 | */ |
547 | */ |
606 | |
548 | |
607 | void |
549 | void |
608 | load_archetypes (void) |
550 | load_archetypes (void) |
609 | { |
551 | { |
610 | char |
|
|
611 | filename[MAX_BUF]; |
552 | char filename[MAX_BUF]; |
612 | |
|
|
613 | #if TIME_ARCH_LOAD |
|
|
614 | struct timeval |
|
|
615 | tv1, |
|
|
616 | tv2; |
|
|
617 | #endif |
|
|
618 | |
553 | |
619 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
554 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
620 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
555 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
621 | |
556 | |
622 | { |
557 | { |
… | |
… | |
636 | object_thawer |
571 | object_thawer |
637 | thawer (filename); |
572 | thawer (filename); |
638 | |
573 | |
639 | LOG (llevDebug, " loading treasure...\n"); |
574 | LOG (llevDebug, " loading treasure...\n"); |
640 | load_treasures (); |
575 | load_treasures (); |
|
|
576 | LOG (llevDebug, " done\n"); |
641 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
577 | LOG (llevDebug, " arch-pass 2...\n"); |
642 | second_arch_pass (thawer); |
578 | second_arch_pass (thawer); |
643 | LOG (llevDebug, " done\n"); |
579 | LOG (llevDebug, " done\n"); |
644 | #ifdef DEBUG |
580 | #ifdef DEBUG |
645 | check_generators (); |
581 | check_generators (); |
646 | #endif |
582 | #endif |
… | |
… | |
650 | |
586 | |
651 | /* |
587 | /* |
652 | * Creates and returns a new object which is a copy of the given archetype. |
588 | * Creates and returns a new object which is a copy of the given archetype. |
653 | * This function returns NULL on failure. |
589 | * This function returns NULL on failure. |
654 | */ |
590 | */ |
655 | |
|
|
656 | object * |
591 | object * |
657 | arch_to_object (archetype *at) |
592 | arch_to_object (archetype *at) |
658 | { |
593 | { |
659 | object * |
594 | object *op; |
660 | op; |
|
|
661 | |
595 | |
662 | if (at == NULL) |
596 | if (at == NULL) |
663 | { |
597 | { |
664 | if (warn_archetypes) |
598 | if (warn_archetypes) |
665 | LOG (llevError, "Couldn't find archetype.\n"); |
599 | LOG (llevError, "Couldn't find archetype.\n"); |
666 | |
600 | |
667 | return NULL; |
601 | return NULL; |
668 | } |
602 | } |
669 | |
603 | |
670 | op = get_object (); |
604 | op = at->clone.clone (); |
671 | copy_object (&at->clone, op); |
|
|
672 | op->arch = at; |
605 | op->arch = at; |
673 | op->instantiate (); |
606 | op->instantiate (); |
674 | return op; |
607 | return op; |
675 | } |
608 | } |
676 | |
609 | |
… | |
… | |
678 | * Creates an object. This function is called by get_archetype() |
611 | * Creates an object. This function is called by get_archetype() |
679 | * if it fails to find the appropriate archetype. |
612 | * if it fails to find the appropriate archetype. |
680 | * Thus get_archetype() will be guaranteed to always return |
613 | * Thus get_archetype() will be guaranteed to always return |
681 | * an object, and never NULL. |
614 | * an object, and never NULL. |
682 | */ |
615 | */ |
683 | |
|
|
684 | object * |
616 | object * |
685 | create_singularity (const char *name) |
617 | create_singularity (const char *name) |
686 | { |
618 | { |
687 | object * |
619 | object *op; |
688 | op; |
|
|
689 | char |
|
|
690 | buf[MAX_BUF]; |
620 | char buf[MAX_BUF]; |
691 | |
621 | |
692 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
622 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
693 | op = get_object (); |
623 | op = object::create (); |
694 | op->name = op->name_pl = buf; |
624 | op->name = op->name_pl = buf; |
695 | SET_FLAG (op, FLAG_NO_PICK); |
625 | SET_FLAG (op, FLAG_NO_PICK); |
696 | return op; |
626 | return op; |
697 | } |
627 | } |
698 | |
628 | |
699 | /* |
629 | /* |
700 | * Finds which archetype matches the given name, and returns a new |
630 | * Finds which archetype matches the given name, and returns a new |
701 | * object containing a copy of the archetype. |
631 | * object containing a copy of the archetype. |
702 | */ |
632 | */ |
703 | |
|
|
704 | object * |
633 | object * |
705 | get_archetype (const char *name) |
634 | get_archetype (const char *name) |
706 | { |
635 | { |
707 | archetype * |
636 | archetype *at = archetype::find (name); |
708 | at; |
|
|
709 | |
637 | |
710 | at = find_archetype (name); |
638 | if (!at) |
711 | if (at == NULL) |
|
|
712 | return create_singularity (name); |
639 | return create_singularity (name); |
713 | |
640 | |
714 | return arch_to_object (at); |
641 | return arch_to_object (at); |
715 | } |
642 | } |
716 | |
643 | |
… | |
… | |
719 | */ |
646 | */ |
720 | |
647 | |
721 | unsigned long |
648 | unsigned long |
722 | hasharch (const char *str, int tablesize) |
649 | hasharch (const char *str, int tablesize) |
723 | { |
650 | { |
724 | unsigned long |
651 | unsigned long hash = 0; |
725 | hash = 0; |
|
|
726 | unsigned int |
652 | unsigned int i = 0; |
727 | i = 0; |
|
|
728 | const char * |
653 | const char *p; |
729 | p; |
|
|
730 | |
654 | |
731 | /* use the one-at-a-time hash function, which supposedly is |
655 | /* use the one-at-a-time hash function, which supposedly is |
732 | * better than the djb2-like one used by perl5.005, but |
656 | * better than the djb2-like one used by perl5.005, but |
733 | * certainly is better then the bug used here before. |
657 | * certainly is better then the bug used here before. |
734 | * see http://burtleburtle.net/bob/hash/doobs.html |
658 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
751 | * Finds, using the hashtable, which archetype matches the given name. |
675 | * Finds, using the hashtable, which archetype matches the given name. |
752 | * returns a pointer to the found archetype, otherwise NULL. |
676 | * returns a pointer to the found archetype, otherwise NULL. |
753 | */ |
677 | */ |
754 | |
678 | |
755 | archetype * |
679 | archetype * |
756 | find_archetype (const char *name) |
680 | archetype::find (const char *name) |
757 | { |
681 | { |
758 | #if USE_UNORDERED_MAP |
|
|
759 | name = shstr::find (name); |
|
|
760 | |
|
|
761 | if (!name) |
682 | if (!name) |
762 | return 0; |
683 | return 0; |
763 | |
684 | |
764 | HT::const_iterator i = ht.find ((size_t) name); |
685 | #if USE_UNORDERED_MAP |
|
|
686 | AUTODECL (i, ht.find ((size_t) name)); |
765 | |
687 | |
766 | if (i == ht.end ()) |
688 | if (i == ht.end ()) |
767 | return 0; |
689 | return 0; |
768 | else |
690 | else |
769 | return i->second; |
691 | return i->second; |
770 | #endif |
692 | #endif |
771 | |
693 | |
772 | archetype * |
694 | archetype *at; |
773 | at; |
|
|
774 | unsigned long |
695 | unsigned long index; |
775 | index; |
|
|
776 | |
|
|
777 | if (name == NULL) |
|
|
778 | return (archetype *) NULL; |
|
|
779 | |
696 | |
780 | index = hasharch (name, ARCHTABLE); |
697 | index = hasharch (name, ARCHTABLE); |
781 | arch_search++; |
698 | arch_search++; |
782 | for (;;) |
699 | for (;;) |
783 | { |
700 | { |
784 | at = arch_table[index]; |
701 | at = arch_table[index]; |
|
|
702 | |
785 | if (at == NULL) |
703 | if (at == NULL) |
786 | { |
704 | { |
787 | if (warn_archetypes) |
705 | if (warn_archetypes) |
788 | LOG (llevError, "Couldn't find archetype %s\n", name); |
706 | LOG (llevError, "Couldn't find archetype %s\n", name); |
|
|
707 | |
789 | return NULL; |
708 | return NULL; |
790 | } |
709 | } |
|
|
710 | |
791 | arch_cmp++; |
711 | arch_cmp++; |
|
|
712 | |
792 | if (!strcmp ((const char *) at->name, name)) |
713 | if (!strcmp ((const char *) at->name, name)) |
793 | return at; |
714 | return at; |
|
|
715 | |
794 | if (++index >= ARCHTABLE) |
716 | if (++index >= ARCHTABLE) |
795 | index = 0; |
717 | index = 0; |
796 | } |
718 | } |
797 | } |
719 | } |
798 | |
720 | |
799 | /* |
721 | /* |
800 | * Adds an archetype to the hashtable. |
722 | * Adds an archetype to the hashtable. |
801 | */ |
723 | */ |
802 | |
724 | void |
803 | static void |
725 | archetype::hash_add () |
804 | add_arch (archetype *at) |
|
|
805 | { |
726 | { |
806 | #if USE_UNORDERED_MAP |
727 | #if USE_UNORDERED_MAP |
807 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
728 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
808 | #endif |
729 | #else |
809 | |
730 | |
810 | int |
|
|
811 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
731 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
812 | |
732 | |
813 | for (;;) |
733 | for (;;) |
814 | { |
734 | { |
815 | if (arch_table[index] == NULL) |
735 | if (!arch_table[index]) |
816 | { |
736 | { |
817 | arch_table[index] = at; |
737 | arch_table[index] = this; |
818 | return; |
738 | break; |
819 | } |
739 | } |
820 | |
740 | |
821 | if (++index == ARCHTABLE) |
741 | if (++index == ARCHTABLE) |
822 | index = 0; |
742 | index = 0; |
823 | |
743 | |
824 | if (index == org_index) |
744 | if (index == org_index) |
825 | fatal (ARCHTABLE_TOO_SMALL); |
745 | fatal (ARCHTABLE_TOO_SMALL); |
826 | } |
746 | } |
|
|
747 | #endif |
|
|
748 | } |
|
|
749 | |
|
|
750 | void |
|
|
751 | archetype::hash_del () |
|
|
752 | { |
|
|
753 | #if USE_UNORDERED_MAP |
|
|
754 | # error remove this from HT |
|
|
755 | #else |
|
|
756 | |
|
|
757 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
|
|
758 | |
|
|
759 | for (;;) |
|
|
760 | { |
|
|
761 | if (arch_table[index] == this) |
|
|
762 | { |
|
|
763 | arch_table[index] = 0; |
|
|
764 | break; |
|
|
765 | } |
|
|
766 | |
|
|
767 | if (++index == ARCHTABLE) |
|
|
768 | index = 0; |
|
|
769 | |
|
|
770 | if (index == org_index) |
|
|
771 | break; |
|
|
772 | } |
|
|
773 | #endif |
827 | } |
774 | } |
828 | |
775 | |
829 | /* |
776 | /* |
830 | * Returns the first archetype using the given type. |
777 | * Returns the first archetype using the given type. |
831 | * Used in treasure-generation. |
778 | * Used in treasure-generation. |
832 | */ |
779 | */ |
833 | |
780 | |
834 | archetype * |
781 | archetype * |
835 | type_to_archetype (int type) |
782 | type_to_archetype (int type) |
836 | { |
783 | { |
837 | archetype * |
784 | archetype *at; |
838 | at; |
|
|
839 | |
785 | |
840 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
786 | for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more) |
841 | if (at->clone.type == type) |
787 | if (at->clone.type == type) |
842 | return at; |
788 | return at; |
|
|
789 | |
843 | return NULL; |
790 | return 0; |
844 | } |
791 | } |
845 | |
792 | |
846 | /* |
793 | /* |
847 | * Returns a new object copied from the first archetype matching |
794 | * Returns a new object copied from the first archetype matching |
848 | * the given type. |
795 | * the given type. |
… | |
… | |
850 | */ |
797 | */ |
851 | |
798 | |
852 | object * |
799 | object * |
853 | clone_arch (int type) |
800 | clone_arch (int type) |
854 | { |
801 | { |
855 | archetype * |
802 | archetype *at; |
856 | at; |
|
|
857 | object * |
|
|
858 | op = get_object (); |
|
|
859 | |
803 | |
860 | if ((at = type_to_archetype (type)) == NULL) |
804 | if ((at = type_to_archetype (type)) == NULL) |
861 | { |
805 | { |
862 | LOG (llevError, "Can't clone archetype %d\n", type); |
806 | LOG (llevError, "Can't clone archetype %d\n", type); |
863 | free_object (op); |
|
|
864 | return NULL; |
807 | return 0; |
865 | } |
808 | } |
866 | copy_object (&at->clone, op); |
809 | |
|
|
810 | object *op = at->clone.clone (); |
867 | op->instantiate (); |
811 | op->instantiate (); |
868 | return op; |
812 | return op; |
869 | } |
813 | } |
870 | |
814 | |
871 | /* |
815 | /* |
… | |
… | |
873 | */ |
817 | */ |
874 | |
818 | |
875 | object * |
819 | object * |
876 | object_create_arch (archetype *at) |
820 | object_create_arch (archetype *at) |
877 | { |
821 | { |
878 | object * |
822 | object *op, *prev = 0, *head = 0; |
879 | op, * |
|
|
880 | prev = NULL, *head = NULL; |
|
|
881 | |
823 | |
882 | while (at) |
824 | while (at) |
883 | { |
825 | { |
884 | op = arch_to_object (at); |
826 | op = arch_to_object (at); |
885 | op->x = at->clone.x; |
827 | op->x = at->clone.x; |
886 | op->y = at->clone.y; |
828 | op->y = at->clone.y; |
|
|
829 | |
887 | if (head) |
830 | if (head) |
888 | op->head = head, prev->more = op; |
831 | op->head = head, prev->more = op; |
|
|
832 | |
889 | if (!head) |
833 | if (!head) |
890 | head = op; |
834 | head = op; |
|
|
835 | |
891 | prev = op; |
836 | prev = op; |
892 | at = at->more; |
837 | at = at->more; |
893 | } |
838 | } |
|
|
839 | |
894 | return (head); |
840 | return (head); |
895 | } |
841 | } |
896 | |
842 | |
897 | /*** end of arch.c ***/ |
|
|