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) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | * |
7 | 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 |
8 | 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 |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
11 | * (at your option) any later version. |
11 | |
12 | * |
12 | 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, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
16 | |
17 | * |
17 | 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 |
18 | along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | |
21 | * |
21 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | #include <cassert> |
25 | #include <cassert> |
25 | |
26 | |
26 | #include <tr1/unordered_map> |
|
|
27 | |
|
|
28 | #include <global.h> |
27 | #include <global.h> |
29 | #include <arch.h> |
|
|
30 | #include <funcpoint.h> |
28 | #include <funcpoint.h> |
31 | #include <loader.h> |
29 | #include <loader.h> |
32 | |
30 | |
33 | /* IF set, does a little timing on the archetype load. */ |
31 | #include <tr1/functional> |
34 | #define TIME_ARCH_LOAD 0 |
32 | #include <tr1/unordered_map> |
35 | |
33 | |
36 | static void add_arch (archetype *at); |
|
|
37 | |
|
|
38 | static archetype *arch_table[ARCHTABLE]; |
|
|
39 | int arch_cmp = 0; /* How many strcmp's */ |
|
|
40 | int arch_search = 0; /* How many searches */ |
|
|
41 | int arch_init; /* True if doing arch initialization */ |
34 | int arch_init; /* True if doing arch initialization */ |
42 | |
35 | |
43 | /* The naming of these functions is really poor - they are all |
36 | /* The naming of these functions is really poor - they are all |
44 | * pretty much named '.._arch_...', but they may more may not |
37 | * pretty much named '.._arch_...', but they may more may not |
45 | * return archetypes. Some make the arch_to_object call, and thus |
38 | * return archetypes. Some make the arch_to_object call, and thus |
46 | * return an object. Perhaps those should be called 'archob' functions |
39 | * return an object. Perhaps those should be called 'archob' functions |
47 | * to denote they return an object derived from the archetype. |
40 | * to denote they return an object derived from the archetype. |
48 | * MSW 2003-04-29 |
41 | * MSW 2003-04-29 |
49 | */ |
42 | */ |
50 | |
43 | |
51 | #if USE_UNORDERED_MAP |
|
|
52 | // the hashtable |
44 | // the hashtable |
53 | typedef |
|
|
54 | std::tr1::unordered_map < |
45 | typedef std::tr1::unordered_map |
55 | size_t, archetype *> |
46 | < |
|
|
47 | const char *, |
|
|
48 | arch_ptr, |
|
|
49 | str_hash, |
|
|
50 | str_equal, |
|
|
51 | slice_allocator< std::pair<const char *const, arch_ptr> > |
56 | HT; |
52 | > HT; |
57 | |
53 | |
58 | static |
54 | static HT ht (5000); |
59 | HT |
|
|
60 | ht; |
|
|
61 | #endif |
|
|
62 | |
55 | |
63 | /** |
56 | /** |
64 | * GROS - This function retrieves an archetype given the name that appears |
57 | * GROS - This function retrieves an archetype given the name that appears |
65 | * during the game (for example, "writing pen" instead of "stylus"). |
58 | * during the game (for example, "writing pen" instead of "stylus"). |
66 | * 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. |
… | |
… | |
72 | * - the archetype found or null if nothing was found. |
65 | * - the archetype found or null if nothing was found. |
73 | */ |
66 | */ |
74 | archetype * |
67 | archetype * |
75 | find_archetype_by_object_name (const char *name) |
68 | find_archetype_by_object_name (const char *name) |
76 | { |
69 | { |
77 | archetype * |
70 | archetype *at; |
78 | at; |
|
|
79 | |
71 | |
80 | if (name == NULL) |
72 | if (name == NULL) |
81 | return (archetype *) NULL; |
73 | return (archetype *) NULL; |
82 | |
74 | |
83 | for (at = first_archetype; at != NULL; at = at->next) |
75 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
94 | * except that it considers only items of the given type. |
86 | * except that it considers only items of the given type. |
95 | */ |
87 | */ |
96 | archetype * |
88 | archetype * |
97 | find_archetype_by_object_type_name (int type, const char *name) |
89 | find_archetype_by_object_type_name (int type, const char *name) |
98 | { |
90 | { |
99 | archetype * |
91 | archetype *at; |
100 | at; |
|
|
101 | |
92 | |
102 | if (name == NULL) |
93 | if (name == NULL) |
103 | return NULL; |
94 | return NULL; |
104 | |
95 | |
105 | for (at = first_archetype; at != NULL; at = at->next) |
96 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
117 | * If type is -1, ew don't match on type. |
108 | * If type is -1, ew don't match on type. |
118 | */ |
109 | */ |
119 | object * |
110 | object * |
120 | get_archetype_by_skill_name (const char *skill, int type) |
111 | get_archetype_by_skill_name (const char *skill, int type) |
121 | { |
112 | { |
122 | archetype * |
113 | archetype *at; |
123 | at; |
|
|
124 | |
114 | |
125 | if (skill == NULL) |
115 | if (skill) |
126 | return NULL; |
|
|
127 | |
|
|
128 | for (at = first_archetype; at != NULL; at = at->next) |
116 | for (at = first_archetype; at; at = at->next) |
129 | { |
|
|
130 | if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) |
117 | if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) |
131 | return arch_to_object (at); |
118 | return arch_to_object (at); |
132 | } |
119 | |
133 | return NULL; |
120 | return 0; |
134 | } |
121 | } |
135 | |
122 | |
136 | /* similiar to above - this returns the first archetype |
123 | /* similiar to above - this returns the first archetype |
137 | * that matches both the type and subtype. type and subtype |
124 | * that matches both the type and subtype. type and subtype |
138 | * can be -1 to say ignore, but in this case, the match it does |
125 | * can be -1 to say ignore, but in this case, the match it does |
… | |
… | |
178 | for (i = strlen (tmpname); i > 0; i--) |
165 | for (i = strlen (tmpname); i > 0; i--) |
179 | { |
166 | { |
180 | tmpname[i] = 0; |
167 | tmpname[i] = 0; |
181 | at = find_archetype_by_object_name (tmpname); |
168 | at = find_archetype_by_object_name (tmpname); |
182 | |
169 | |
183 | if (at != NULL) |
170 | if (at) |
184 | { |
|
|
185 | return arch_to_object (at); |
171 | return arch_to_object (at); |
186 | } |
|
|
187 | } |
172 | } |
188 | |
173 | |
189 | return create_singularity (name); |
174 | return create_singularity (name); |
190 | } |
175 | } |
191 | |
176 | |
… | |
… | |
207 | * Last, make a check on the full name. |
192 | * Last, make a check on the full name. |
208 | */ |
193 | */ |
209 | int |
194 | int |
210 | item_matched_string (object *pl, object *op, const char *name) |
195 | item_matched_string (object *pl, object *op, const char *name) |
211 | { |
196 | { |
212 | char * |
|
|
213 | cp, |
|
|
214 | local_name[MAX_BUF]; |
197 | char *cp, local_name[MAX_BUF]; |
215 | int |
198 | int count, retval = 0; |
216 | count, |
|
|
217 | retval = 0; |
|
|
218 | |
199 | |
219 | strcpy (local_name, name); /* strtok is destructive to name */ |
200 | strcpy (local_name, name); /* strtok is destructive to name */ |
220 | |
201 | |
221 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
202 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
222 | { |
203 | { |
… | |
… | |
274 | retval = 15; |
255 | retval = 15; |
275 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
256 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
276 | retval = 14; |
257 | retval = 14; |
277 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
258 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
278 | retval = 14; |
259 | retval = 14; |
279 | |
|
|
280 | /* Do substring checks, so things like 'Str+1' will match. |
260 | /* Do substring checks, so things like 'Str+1' will match. |
281 | * retval of these should perhaps be lower - they are lower |
261 | * retval of these should perhaps be lower - they are lower |
282 | * then the specific strcasecmp aboves, but still higher than |
262 | * then the specific strcasecmp aboves, but still higher than |
283 | * some other match criteria. |
263 | * some other match criteria. |
284 | */ |
264 | */ |
… | |
… | |
286 | retval = 12; |
266 | retval = 12; |
287 | else if (strstr (query_base_name (op, 0), cp)) |
267 | else if (strstr (query_base_name (op, 0), cp)) |
288 | retval = 12; |
268 | retval = 12; |
289 | else if (strstr (query_short_name (op), cp)) |
269 | else if (strstr (query_short_name (op), cp)) |
290 | retval = 12; |
270 | retval = 12; |
291 | |
|
|
292 | /* Check against plural/non plural based on count. */ |
271 | /* Check against plural/non plural based on count. */ |
293 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
272 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
294 | { |
|
|
295 | retval = 6; |
273 | retval = 6; |
296 | } |
|
|
297 | else if (count == 1 && !strcasecmp (op->name, cp)) |
274 | else if (count == 1 && !strcasecmp (op->name, cp)) |
298 | { |
|
|
299 | retval = 6; |
275 | retval = 6; |
300 | } |
|
|
301 | /* base name matched - not bad */ |
276 | /* base name matched - not bad */ |
302 | else if (strcasecmp (cp, op->name) == 0 && !count) |
277 | else if (strcasecmp (cp, op->name) == 0 && !count) |
303 | retval = 4; |
278 | retval = 4; |
304 | /* Check for partial custom name, but give a real low priority */ |
279 | /* Check for partial custom name, but give a real low priority */ |
305 | else if (op->custom_name && strstr (op->custom_name, cp)) |
280 | else if (op->custom_name && strstr (op->custom_name, cp)) |
… | |
… | |
307 | |
282 | |
308 | if (retval) |
283 | if (retval) |
309 | { |
284 | { |
310 | if (pl->type == PLAYER) |
285 | if (pl->type == PLAYER) |
311 | pl->contr->count = count; |
286 | pl->contr->count = count; |
|
|
287 | |
312 | return retval; |
288 | return retval; |
313 | } |
289 | } |
314 | } |
290 | } |
|
|
291 | |
315 | return 0; |
292 | return 0; |
316 | } |
293 | } |
317 | |
294 | |
318 | /* |
295 | /* |
319 | * Initialises the internal linked list of archetypes (read from file). |
296 | * Initialises the internal linked list of archetypes (read from file). |
… | |
… | |
324 | void |
301 | void |
325 | init_archetypes (void) |
302 | init_archetypes (void) |
326 | { /* called from add_player() and edit() */ |
303 | { /* called from add_player() and edit() */ |
327 | if (first_archetype != NULL) /* Only do this once */ |
304 | if (first_archetype != NULL) /* Only do this once */ |
328 | return; |
305 | return; |
|
|
306 | |
329 | arch_init = 1; |
307 | arch_init = 1; |
330 | load_archetypes (); |
308 | load_archetypes (); |
331 | arch_init = 0; |
309 | arch_init = 0; |
332 | empty_archetype = find_archetype ("empty_archetype"); |
310 | empty_archetype = archetype::find ("empty_archetype"); |
333 | |
311 | |
334 | /* init_blocksview();*/ |
312 | /* init_blocksview();*/ |
335 | } |
313 | } |
336 | |
314 | |
337 | /* |
315 | /* |
338 | * Stores debug-information about how efficient the hashtable |
|
|
339 | * used for archetypes has been in the static errmsg array. |
|
|
340 | */ |
|
|
341 | |
|
|
342 | void |
|
|
343 | arch_info (object *op) |
|
|
344 | { |
|
|
345 | sprintf (errmsg, "%d searches and %d strcmp()'s", arch_search, arch_cmp); |
|
|
346 | new_draw_info (NDI_BLACK, 0, op, errmsg); |
|
|
347 | } |
|
|
348 | |
|
|
349 | /* |
|
|
350 | * Initialise the hashtable used by the archetypes. |
|
|
351 | */ |
|
|
352 | |
|
|
353 | void |
|
|
354 | clear_archetable (void) |
|
|
355 | { |
|
|
356 | memset ((void *) arch_table, 0, ARCHTABLE * sizeof (archetype *)); |
|
|
357 | } |
|
|
358 | |
|
|
359 | /* |
|
|
360 | * An alternative way to init the hashtable which is slower, but _works_... |
316 | * An alternative way to init the hashtable which is slower, but _works_... |
361 | */ |
317 | */ |
362 | |
|
|
363 | void |
318 | void |
364 | init_archetable (void) |
319 | init_archetable (void) |
365 | { |
320 | { |
366 | archetype * |
321 | archetype *at; |
367 | at; |
|
|
368 | |
322 | |
369 | LOG (llevDebug, " Setting up archetable...\n"); |
323 | LOG (llevDebug, " Setting up archetable...\n"); |
370 | |
324 | |
371 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
325 | for (at = first_archetype; at; at = at->more ? at->more : at->next) |
372 | add_arch (at); |
326 | at->hash_add (); |
373 | |
327 | |
374 | LOG (llevDebug, "done\n"); |
328 | LOG (llevDebug, "done\n"); |
375 | } |
329 | } |
376 | |
330 | |
377 | /* |
|
|
378 | * Dumps an archetype to debug-level output. |
|
|
379 | */ |
|
|
380 | |
|
|
381 | void |
|
|
382 | dump_arch (archetype *at) |
|
|
383 | { |
|
|
384 | dump_object (&at->clone); |
|
|
385 | } |
|
|
386 | |
|
|
387 | /* |
|
|
388 | * Dumps _all_ archetypes to debug-level output. |
|
|
389 | * If you run crossfire with debug, and enter DM-mode, you can trigger |
|
|
390 | * this with the O key. |
|
|
391 | */ |
|
|
392 | |
|
|
393 | void |
|
|
394 | dump_all_archetypes (void) |
|
|
395 | { |
|
|
396 | archetype * |
|
|
397 | at; |
|
|
398 | |
|
|
399 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
|
|
400 | { |
|
|
401 | dump_arch (at); |
|
|
402 | fprintf (logfile, "%s\n", errmsg); |
|
|
403 | } |
|
|
404 | } |
|
|
405 | |
|
|
406 | void |
331 | void |
407 | free_all_archs (void) |
332 | free_all_archs (void) |
408 | { |
333 | { |
409 | archetype * |
334 | archetype *at, *next; |
410 | at, * |
|
|
411 | next; |
|
|
412 | int |
|
|
413 | i = 0, f = 0; |
335 | int i = 0, f = 0; |
414 | |
336 | |
415 | for (at = first_archetype; at != NULL; at = next) |
337 | for (at = first_archetype; at; at = next) |
416 | { |
338 | { |
417 | if (at->more) |
339 | if (at->more) |
418 | next = at->more; |
340 | next = at->more; |
419 | else |
341 | else |
420 | next = at->next; |
342 | next = at->next; |
… | |
… | |
422 | delete |
344 | delete |
423 | at; |
345 | at; |
424 | |
346 | |
425 | i++; |
347 | i++; |
426 | } |
348 | } |
|
|
349 | |
427 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
350 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
428 | } |
351 | } |
429 | |
352 | |
430 | archetype::archetype () |
353 | archetype::archetype () |
431 | { |
354 | { |
432 | clear_object (&clone); /* to initial state other also */ |
|
|
433 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ |
355 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ |
434 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
356 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
435 | } |
357 | } |
436 | |
358 | |
437 | archetype::~archetype () |
359 | archetype::~archetype () |
438 | { |
360 | { |
… | |
… | |
443 | * of archetype-structures. |
365 | * of archetype-structures. |
444 | */ |
366 | */ |
445 | void |
367 | void |
446 | first_arch_pass (object_thawer & fp) |
368 | first_arch_pass (object_thawer & fp) |
447 | { |
369 | { |
448 | archetype * |
370 | archetype *head = 0, *last_more = 0; |
449 | at, * |
|
|
450 | head = NULL, *last_more = NULL; |
|
|
451 | |
371 | |
|
|
372 | archetype *at = new archetype; |
452 | at->clone.arch = first_archetype = at = new archetype; |
373 | at->clone.arch = first_archetype = at; |
453 | |
374 | |
454 | while (int i = load_object (fp, &at->clone, 0)) |
375 | while (int i = load_object (fp, &at->clone, 0)) |
455 | { |
376 | { |
456 | at->clone.speed_left = (float) (-0.1); |
377 | at->clone.speed_left = (float) (-0.1); |
457 | /* copy the body_info to the body_used - this is only really |
378 | /* copy the body_info to the body_used - this is only really |
… | |
… | |
524 | */ |
445 | */ |
525 | |
446 | |
526 | void |
447 | void |
527 | second_arch_pass (object_thawer & thawer) |
448 | second_arch_pass (object_thawer & thawer) |
528 | { |
449 | { |
529 | char |
|
|
530 | buf[MAX_BUF], * |
|
|
531 | variable = buf, *argument, *cp; |
450 | char buf[MAX_BUF], *variable = buf, *argument, *cp; |
532 | archetype * |
|
|
533 | at = NULL, *other; |
451 | archetype *at = NULL, *other; |
534 | |
452 | |
535 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
453 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
536 | { |
454 | { |
537 | if (*buf == '#') |
455 | if (*buf == '#') |
538 | continue; |
456 | continue; |
… | |
… | |
546 | cp--; |
464 | cp--; |
547 | } |
465 | } |
548 | } |
466 | } |
549 | if (!strcmp ("Object", variable)) |
467 | if (!strcmp ("Object", variable)) |
550 | { |
468 | { |
551 | if ((at = find_archetype (argument)) == NULL) |
469 | if ((at = archetype::find (argument)) == NULL) |
552 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
470 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
553 | } |
471 | } |
554 | else if (!strcmp ("other_arch", variable)) |
472 | else if (!strcmp ("other_arch", variable)) |
555 | { |
473 | { |
556 | if (at != NULL && at->clone.other_arch == NULL) |
474 | if (at != NULL && at->clone.other_arch == NULL) |
557 | { |
475 | { |
558 | if ((other = find_archetype (argument)) == NULL) |
476 | if ((other = archetype::find (argument)) == NULL) |
559 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
477 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
560 | else if (at != NULL) |
478 | else if (at != NULL) |
561 | at->clone.other_arch = other; |
479 | at->clone.other_arch = other; |
562 | } |
480 | } |
563 | } |
481 | } |
564 | else if (!strcmp ("randomitems", variable)) |
482 | else if (!strcmp ("randomitems", variable)) |
565 | { |
483 | { |
566 | if (at != NULL) |
484 | if (at != NULL) |
567 | { |
485 | { |
568 | treasurelist * |
|
|
569 | tl = find_treasurelist (argument); |
486 | treasurelist *tl = find_treasurelist (argument); |
570 | |
487 | |
571 | if (tl == NULL) |
488 | if (tl == NULL) |
572 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
489 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
573 | else |
490 | else |
574 | at->clone.randomitems = tl; |
491 | at->clone.randomitems = tl; |
… | |
… | |
579 | |
496 | |
580 | #ifdef DEBUG |
497 | #ifdef DEBUG |
581 | void |
498 | void |
582 | check_generators (void) |
499 | check_generators (void) |
583 | { |
500 | { |
584 | archetype * |
501 | archetype *at; |
585 | at; |
|
|
586 | |
502 | |
587 | for (at = first_archetype; at != NULL; at = at->next) |
503 | for (at = first_archetype; at != NULL; at = at->next) |
588 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
504 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
589 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
505 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
590 | } |
506 | } |
… | |
… | |
598 | */ |
514 | */ |
599 | |
515 | |
600 | void |
516 | void |
601 | load_archetypes (void) |
517 | load_archetypes (void) |
602 | { |
518 | { |
603 | char |
|
|
604 | filename[MAX_BUF]; |
519 | char filename[MAX_BUF]; |
605 | |
|
|
606 | #if TIME_ARCH_LOAD |
|
|
607 | struct timeval |
|
|
608 | tv1, |
|
|
609 | tv2; |
|
|
610 | #endif |
|
|
611 | |
520 | |
612 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
521 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
613 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
522 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
614 | |
523 | |
615 | { |
524 | { |
616 | object_thawer |
525 | object_thawer |
617 | thawer (filename); |
526 | thawer (filename); |
618 | |
527 | |
619 | clear_archetable (); |
|
|
620 | LOG (llevDebug, " arch-pass 1...\n"); |
528 | LOG (llevDebug, " arch-pass 1...\n"); |
621 | first_arch_pass (thawer); |
529 | first_arch_pass (thawer); |
622 | LOG (llevDebug, " done\n"); |
530 | LOG (llevDebug, " done\n"); |
623 | } |
531 | } |
624 | |
532 | |
… | |
… | |
629 | object_thawer |
537 | object_thawer |
630 | thawer (filename); |
538 | thawer (filename); |
631 | |
539 | |
632 | LOG (llevDebug, " loading treasure...\n"); |
540 | LOG (llevDebug, " loading treasure...\n"); |
633 | load_treasures (); |
541 | load_treasures (); |
|
|
542 | LOG (llevDebug, " done\n"); |
634 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
543 | LOG (llevDebug, " arch-pass 2...\n"); |
635 | second_arch_pass (thawer); |
544 | second_arch_pass (thawer); |
636 | LOG (llevDebug, " done\n"); |
545 | LOG (llevDebug, " done\n"); |
637 | #ifdef DEBUG |
546 | #ifdef DEBUG |
638 | check_generators (); |
547 | check_generators (); |
639 | #endif |
548 | #endif |
… | |
… | |
643 | |
552 | |
644 | /* |
553 | /* |
645 | * Creates and returns a new object which is a copy of the given archetype. |
554 | * Creates and returns a new object which is a copy of the given archetype. |
646 | * This function returns NULL on failure. |
555 | * This function returns NULL on failure. |
647 | */ |
556 | */ |
648 | |
|
|
649 | object * |
557 | object * |
650 | arch_to_object (archetype *at) |
558 | arch_to_object (archetype *at) |
651 | { |
559 | { |
652 | object * |
560 | object *op; |
653 | op; |
|
|
654 | |
561 | |
655 | if (at == NULL) |
562 | if (at == NULL) |
656 | { |
563 | { |
657 | if (warn_archetypes) |
564 | if (warn_archetypes) |
658 | LOG (llevError, "Couldn't find archetype.\n"); |
565 | LOG (llevError, "Couldn't find archetype.\n"); |
659 | |
566 | |
660 | return NULL; |
567 | return NULL; |
661 | } |
568 | } |
662 | |
569 | |
663 | op = get_object (); |
570 | op = at->clone.clone (); |
664 | copy_object (&at->clone, op); |
|
|
665 | op->arch = at; |
571 | op->arch = at; |
666 | op->instantiate (); |
572 | op->instantiate (); |
667 | return op; |
573 | return op; |
668 | } |
574 | } |
669 | |
575 | |
… | |
… | |
671 | * Creates an object. This function is called by get_archetype() |
577 | * Creates an object. This function is called by get_archetype() |
672 | * if it fails to find the appropriate archetype. |
578 | * if it fails to find the appropriate archetype. |
673 | * Thus get_archetype() will be guaranteed to always return |
579 | * Thus get_archetype() will be guaranteed to always return |
674 | * an object, and never NULL. |
580 | * an object, and never NULL. |
675 | */ |
581 | */ |
676 | |
|
|
677 | object * |
582 | object * |
678 | create_singularity (const char *name) |
583 | create_singularity (const char *name) |
679 | { |
584 | { |
680 | object * |
585 | object *op; |
681 | op; |
|
|
682 | char |
|
|
683 | buf[MAX_BUF]; |
586 | char buf[MAX_BUF]; |
684 | |
587 | |
685 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
588 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
686 | op = get_object (); |
589 | op = object::create (); |
687 | op->name = op->name_pl = buf; |
590 | op->name = op->name_pl = buf; |
688 | SET_FLAG (op, FLAG_NO_PICK); |
591 | SET_FLAG (op, FLAG_NO_PICK); |
689 | return op; |
592 | return op; |
690 | } |
593 | } |
691 | |
594 | |
692 | /* |
595 | /* |
693 | * Finds which archetype matches the given name, and returns a new |
596 | * Finds which archetype matches the given name, and returns a new |
694 | * object containing a copy of the archetype. |
597 | * object containing a copy of the archetype. |
695 | */ |
598 | */ |
696 | |
|
|
697 | object * |
599 | object * |
698 | get_archetype (const char *name) |
600 | get_archetype (const char *name) |
699 | { |
601 | { |
700 | archetype * |
602 | archetype *at = archetype::find (name); |
701 | at; |
|
|
702 | |
603 | |
703 | at = find_archetype (name); |
604 | if (!at) |
704 | if (at == NULL) |
|
|
705 | return create_singularity (name); |
605 | return create_singularity (name); |
706 | |
606 | |
707 | return arch_to_object (at); |
607 | return arch_to_object (at); |
708 | } |
608 | } |
709 | |
609 | |
… | |
… | |
712 | */ |
612 | */ |
713 | |
613 | |
714 | unsigned long |
614 | unsigned long |
715 | hasharch (const char *str, int tablesize) |
615 | hasharch (const char *str, int tablesize) |
716 | { |
616 | { |
717 | unsigned long |
617 | unsigned long hash = 0; |
718 | hash = 0; |
|
|
719 | unsigned int |
618 | unsigned int i = 0; |
720 | i = 0; |
|
|
721 | const char * |
619 | const char *p; |
722 | p; |
|
|
723 | |
620 | |
724 | /* use the one-at-a-time hash function, which supposedly is |
621 | /* use the one-at-a-time hash function, which supposedly is |
725 | * better than the djb2-like one used by perl5.005, but |
622 | * better than the djb2-like one used by perl5.005, but |
726 | * certainly is better then the bug used here before. |
623 | * certainly is better then the bug used here before. |
727 | * see http://burtleburtle.net/bob/hash/doobs.html |
624 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
742 | |
639 | |
743 | /* |
640 | /* |
744 | * Finds, using the hashtable, which archetype matches the given name. |
641 | * Finds, using the hashtable, which archetype matches the given name. |
745 | * returns a pointer to the found archetype, otherwise NULL. |
642 | * returns a pointer to the found archetype, otherwise NULL. |
746 | */ |
643 | */ |
747 | |
|
|
748 | archetype * |
644 | archetype * |
749 | find_archetype (const char *name) |
645 | archetype::find (const char *name) |
750 | { |
646 | { |
751 | #if USE_UNORDERED_MAP |
|
|
752 | name = shstr::find (name); |
|
|
753 | |
|
|
754 | if (!name) |
647 | if (!name) |
755 | return 0; |
648 | return 0; |
756 | |
649 | |
757 | HT::const_iterator i = ht.find ((size_t) name); |
650 | AUTODECL (i, ht.find (name)); |
758 | |
651 | |
759 | if (i == ht.end ()) |
652 | if (i == ht.end ()) |
760 | return 0; |
653 | return 0; |
761 | else |
654 | else |
762 | return i->second; |
655 | return i->second; |
763 | #endif |
|
|
764 | |
|
|
765 | archetype * |
|
|
766 | at; |
|
|
767 | unsigned long |
|
|
768 | index; |
|
|
769 | |
|
|
770 | if (name == NULL) |
|
|
771 | return (archetype *) NULL; |
|
|
772 | |
|
|
773 | index = hasharch (name, ARCHTABLE); |
|
|
774 | arch_search++; |
|
|
775 | for (;;) |
|
|
776 | { |
|
|
777 | at = arch_table[index]; |
|
|
778 | if (at == NULL) |
|
|
779 | { |
|
|
780 | if (warn_archetypes) |
|
|
781 | LOG (llevError, "Couldn't find archetype %s\n", name); |
|
|
782 | return NULL; |
|
|
783 | } |
|
|
784 | arch_cmp++; |
|
|
785 | if (!strcmp ((const char *) at->name, name)) |
|
|
786 | return at; |
|
|
787 | if (++index >= ARCHTABLE) |
|
|
788 | index = 0; |
|
|
789 | } |
|
|
790 | } |
656 | } |
791 | |
657 | |
792 | /* |
658 | /* |
793 | * Adds an archetype to the hashtable. |
659 | * Adds an archetype to the hashtable. |
794 | */ |
660 | */ |
795 | |
661 | void |
796 | static void |
662 | archetype::hash_add () |
797 | add_arch (archetype *at) |
|
|
798 | { |
663 | { |
799 | #if USE_UNORDERED_MAP |
664 | ht.insert (std::make_pair (name, this)); |
800 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
665 | } |
801 | #endif |
|
|
802 | |
666 | |
803 | int |
667 | void |
804 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
668 | archetype::hash_del () |
805 | |
669 | { |
806 | for (;;) |
670 | ht.erase (name); |
807 | { |
|
|
808 | if (arch_table[index] == NULL) |
|
|
809 | { |
|
|
810 | arch_table[index] = at; |
|
|
811 | return; |
|
|
812 | } |
|
|
813 | |
|
|
814 | if (++index == ARCHTABLE) |
|
|
815 | index = 0; |
|
|
816 | |
|
|
817 | if (index == org_index) |
|
|
818 | fatal (ARCHTABLE_TOO_SMALL); |
|
|
819 | } |
|
|
820 | } |
671 | } |
821 | |
672 | |
822 | /* |
673 | /* |
823 | * Returns the first archetype using the given type. |
674 | * Returns the first archetype using the given type. |
824 | * Used in treasure-generation. |
675 | * Used in treasure-generation. |
825 | */ |
676 | */ |
826 | |
|
|
827 | archetype * |
677 | archetype * |
828 | type_to_archetype (int type) |
678 | type_to_archetype (int type) |
829 | { |
679 | { |
830 | archetype * |
680 | archetype *at; |
831 | at; |
|
|
832 | |
681 | |
833 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
682 | for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more) |
834 | if (at->clone.type == type) |
683 | if (at->clone.type == type) |
835 | return at; |
684 | return at; |
|
|
685 | |
836 | return NULL; |
686 | return 0; |
837 | } |
687 | } |
838 | |
688 | |
839 | /* |
689 | /* |
840 | * Returns a new object copied from the first archetype matching |
690 | * Returns a new object copied from the first archetype matching |
841 | * the given type. |
691 | * the given type. |
… | |
… | |
843 | */ |
693 | */ |
844 | |
694 | |
845 | object * |
695 | object * |
846 | clone_arch (int type) |
696 | clone_arch (int type) |
847 | { |
697 | { |
848 | archetype * |
698 | archetype *at; |
849 | at; |
|
|
850 | object * |
|
|
851 | op = get_object (); |
|
|
852 | |
699 | |
853 | if ((at = type_to_archetype (type)) == NULL) |
700 | if ((at = type_to_archetype (type)) == NULL) |
854 | { |
701 | { |
855 | LOG (llevError, "Can't clone archetype %d\n", type); |
702 | LOG (llevError, "Can't clone archetype %d\n", type); |
856 | free_object (op); |
|
|
857 | return NULL; |
703 | return 0; |
858 | } |
704 | } |
859 | copy_object (&at->clone, op); |
705 | |
|
|
706 | object *op = at->clone.clone (); |
860 | op->instantiate (); |
707 | op->instantiate (); |
861 | return op; |
708 | return op; |
862 | } |
709 | } |
863 | |
710 | |
864 | /* |
711 | /* |
… | |
… | |
866 | */ |
713 | */ |
867 | |
714 | |
868 | object * |
715 | object * |
869 | object_create_arch (archetype *at) |
716 | object_create_arch (archetype *at) |
870 | { |
717 | { |
871 | object * |
718 | object *op, *prev = 0, *head = 0; |
872 | op, * |
|
|
873 | prev = NULL, *head = NULL; |
|
|
874 | |
719 | |
875 | while (at) |
720 | while (at) |
876 | { |
721 | { |
877 | op = arch_to_object (at); |
722 | op = arch_to_object (at); |
878 | op->x = at->clone.x; |
723 | op->x = at->clone.x; |
879 | op->y = at->clone.y; |
724 | op->y = at->clone.y; |
|
|
725 | |
880 | if (head) |
726 | if (head) |
881 | op->head = head, prev->more = op; |
727 | op->head = head, prev->more = op; |
|
|
728 | |
882 | if (!head) |
729 | if (!head) |
883 | head = op; |
730 | head = op; |
|
|
731 | |
884 | prev = op; |
732 | prev = op; |
885 | at = at->more; |
733 | at = at->more; |
886 | } |
734 | } |
|
|
735 | |
887 | return (head); |
736 | return (head); |
888 | } |
737 | } |
889 | |
738 | |
890 | /*** end of arch.c ***/ |
|
|