… | |
… | |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
21 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | #include <cassert> |
24 | #include <cassert> |
25 | |
25 | |
26 | #include <tr1/unordered_map> |
|
|
27 | |
|
|
28 | #include <global.h> |
26 | #include <global.h> |
29 | #include <arch.h> |
|
|
30 | #include <funcpoint.h> |
27 | #include <funcpoint.h> |
31 | #include <loader.h> |
28 | #include <loader.h> |
32 | |
29 | |
33 | /* IF set, does a little timing on the archetype load. */ |
30 | #define USE_UNORDERED_MAP 0 |
34 | #define TIME_ARCH_LOAD 0 |
|
|
35 | |
31 | |
36 | static void add_arch (archetype *at); |
32 | #if USE_UNORDERED_MAP |
|
|
33 | # include <tr1/functional> |
|
|
34 | # include <tr1/unordered_map> |
|
|
35 | #endif |
37 | |
36 | |
38 | static archetype *arch_table[ARCHTABLE]; |
|
|
39 | int arch_cmp = 0; /* How many strcmp's */ |
37 | int arch_cmp = 0; /* How many strcmp's */ |
40 | int arch_search = 0; /* How many searches */ |
38 | int arch_search = 0; /* How many searches */ |
41 | int arch_init; /* True if doing arch initialization */ |
39 | int arch_init; /* True if doing arch initialization */ |
42 | |
40 | |
43 | /* The naming of these functions is really poor - they are all |
41 | /* The naming of these functions is really poor - they are all |
… | |
… | |
51 | #if USE_UNORDERED_MAP |
49 | #if USE_UNORDERED_MAP |
52 | // the hashtable |
50 | // the hashtable |
53 | typedef std::tr1::unordered_map |
51 | typedef std::tr1::unordered_map |
54 | < |
52 | < |
55 | std::size_t, |
53 | std::size_t, |
56 | archetype *, |
54 | arch_ptr, |
57 | std::hash<size_t>, |
55 | std::tr1::hash<size_t>, |
58 | std::equal_to<size_t>, |
56 | std::equal_to<size_t>, |
59 | slice_allocator< std::pair<const std::size_t, archetype *> > |
57 | slice_allocator< std::pair<const std::size_t, archetype *> > |
60 | true, |
58 | true, |
61 | > HT; |
59 | > HT; |
62 | |
60 | |
63 | static HT ht; |
61 | static HT ht; |
|
|
62 | #else |
|
|
63 | static arch_ptr arch_table[ARCHTABLE]; |
64 | #endif |
64 | #endif |
65 | |
65 | |
66 | /** |
66 | /** |
67 | * GROS - This function retrieves an archetype given the name that appears |
67 | * GROS - This function retrieves an archetype given the name that appears |
68 | * during the game (for example, "writing pen" instead of "stylus"). |
68 | * during the game (for example, "writing pen" instead of "stylus"). |
… | |
… | |
269 | retval = 15; |
269 | retval = 15; |
270 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
270 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
271 | retval = 14; |
271 | retval = 14; |
272 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
272 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
273 | retval = 14; |
273 | retval = 14; |
274 | |
|
|
275 | /* Do substring checks, so things like 'Str+1' will match. |
274 | /* Do substring checks, so things like 'Str+1' will match. |
276 | * retval of these should perhaps be lower - they are lower |
275 | * retval of these should perhaps be lower - they are lower |
277 | * then the specific strcasecmp aboves, but still higher than |
276 | * then the specific strcasecmp aboves, but still higher than |
278 | * some other match criteria. |
277 | * some other match criteria. |
279 | */ |
278 | */ |
… | |
… | |
281 | retval = 12; |
280 | retval = 12; |
282 | else if (strstr (query_base_name (op, 0), cp)) |
281 | else if (strstr (query_base_name (op, 0), cp)) |
283 | retval = 12; |
282 | retval = 12; |
284 | else if (strstr (query_short_name (op), cp)) |
283 | else if (strstr (query_short_name (op), cp)) |
285 | retval = 12; |
284 | retval = 12; |
286 | |
|
|
287 | /* Check against plural/non plural based on count. */ |
285 | /* Check against plural/non plural based on count. */ |
288 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
286 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
289 | { |
|
|
290 | retval = 6; |
287 | retval = 6; |
291 | } |
|
|
292 | else if (count == 1 && !strcasecmp (op->name, cp)) |
288 | else if (count == 1 && !strcasecmp (op->name, cp)) |
293 | { |
|
|
294 | retval = 6; |
289 | retval = 6; |
295 | } |
|
|
296 | /* base name matched - not bad */ |
290 | /* base name matched - not bad */ |
297 | else if (strcasecmp (cp, op->name) == 0 && !count) |
291 | else if (strcasecmp (cp, op->name) == 0 && !count) |
298 | retval = 4; |
292 | retval = 4; |
299 | /* Check for partial custom name, but give a real low priority */ |
293 | /* Check for partial custom name, but give a real low priority */ |
300 | else if (op->custom_name && strstr (op->custom_name, cp)) |
294 | else if (op->custom_name && strstr (op->custom_name, cp)) |
… | |
… | |
302 | |
296 | |
303 | if (retval) |
297 | if (retval) |
304 | { |
298 | { |
305 | if (pl->type == PLAYER) |
299 | if (pl->type == PLAYER) |
306 | pl->contr->count = count; |
300 | pl->contr->count = count; |
|
|
301 | |
307 | return retval; |
302 | return retval; |
308 | } |
303 | } |
309 | } |
304 | } |
|
|
305 | |
310 | return 0; |
306 | return 0; |
311 | } |
307 | } |
312 | |
308 | |
313 | /* |
309 | /* |
314 | * Initialises the internal linked list of archetypes (read from file). |
310 | * Initialises the internal linked list of archetypes (read from file). |
… | |
… | |
362 | archetype *at; |
358 | archetype *at; |
363 | |
359 | |
364 | LOG (llevDebug, " Setting up archetable...\n"); |
360 | LOG (llevDebug, " Setting up archetable...\n"); |
365 | |
361 | |
366 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
362 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
367 | add_arch (at); |
363 | at->hash_add (); |
368 | |
364 | |
369 | LOG (llevDebug, "done\n"); |
365 | LOG (llevDebug, "done\n"); |
370 | } |
|
|
371 | |
|
|
372 | /* |
|
|
373 | * Dumps an archetype to debug-level output. |
|
|
374 | */ |
|
|
375 | |
|
|
376 | void |
|
|
377 | dump_arch (archetype *at) |
|
|
378 | { |
|
|
379 | dump_object (&at->clone); |
|
|
380 | } |
|
|
381 | |
|
|
382 | /* |
|
|
383 | * Dumps _all_ archetypes to debug-level output. |
|
|
384 | * If you run crossfire with debug, and enter DM-mode, you can trigger |
|
|
385 | * this with the O key. |
|
|
386 | */ |
|
|
387 | |
|
|
388 | void |
|
|
389 | dump_all_archetypes (void) |
|
|
390 | { |
|
|
391 | archetype *at; |
|
|
392 | |
|
|
393 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
|
|
394 | { |
|
|
395 | dump_arch (at); |
|
|
396 | fprintf (logfile, "%s\n", errmsg); |
|
|
397 | } |
|
|
398 | } |
366 | } |
399 | |
367 | |
400 | void |
368 | void |
401 | free_all_archs (void) |
369 | free_all_archs (void) |
402 | { |
370 | { |
… | |
… | |
413 | delete |
381 | delete |
414 | at; |
382 | at; |
415 | |
383 | |
416 | i++; |
384 | i++; |
417 | } |
385 | } |
|
|
386 | |
418 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
387 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
419 | } |
388 | } |
420 | |
389 | |
421 | archetype::archetype () |
390 | archetype::archetype () |
422 | { |
391 | { |
423 | clear_object (&clone); /* to initial state other also */ |
392 | clear_object (&clone); /* to initial state other also */ |
424 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ |
393 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ |
425 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
394 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
426 | } |
395 | } |
427 | |
396 | |
428 | archetype::~archetype () |
397 | archetype::~archetype () |
429 | { |
398 | { |
… | |
… | |
434 | * of archetype-structures. |
403 | * of archetype-structures. |
435 | */ |
404 | */ |
436 | void |
405 | void |
437 | first_arch_pass (object_thawer & fp) |
406 | first_arch_pass (object_thawer & fp) |
438 | { |
407 | { |
439 | archetype *at, *head = NULL, *last_more = NULL; |
408 | archetype *head = 0, *last_more = 0; |
440 | |
409 | |
|
|
410 | archetype *at = new archetype; |
441 | at->clone.arch = first_archetype = at = new archetype; |
411 | at->clone.arch = first_archetype = at; |
442 | |
412 | |
443 | while (int i = load_object (fp, &at->clone, 0)) |
413 | while (int i = load_object (fp, &at->clone, 0)) |
444 | { |
414 | { |
445 | at->clone.speed_left = (float) (-0.1); |
415 | at->clone.speed_left = (float) (-0.1); |
446 | /* copy the body_info to the body_used - this is only really |
416 | /* copy the body_info to the body_used - this is only really |
… | |
… | |
549 | } |
519 | } |
550 | else if (!strcmp ("randomitems", variable)) |
520 | else if (!strcmp ("randomitems", variable)) |
551 | { |
521 | { |
552 | if (at != NULL) |
522 | if (at != NULL) |
553 | { |
523 | { |
554 | treasurelist * |
|
|
555 | tl = find_treasurelist (argument); |
524 | treasurelist *tl = find_treasurelist (argument); |
556 | |
525 | |
557 | if (tl == NULL) |
526 | if (tl == NULL) |
558 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
527 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
559 | else |
528 | else |
560 | at->clone.randomitems = tl; |
529 | at->clone.randomitems = tl; |
… | |
… | |
584 | |
553 | |
585 | void |
554 | void |
586 | load_archetypes (void) |
555 | load_archetypes (void) |
587 | { |
556 | { |
588 | char filename[MAX_BUF]; |
557 | char filename[MAX_BUF]; |
589 | |
|
|
590 | #if TIME_ARCH_LOAD |
|
|
591 | struct timeval |
|
|
592 | tv1, |
|
|
593 | tv2; |
|
|
594 | #endif |
|
|
595 | |
558 | |
596 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
559 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
597 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
560 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
598 | |
561 | |
599 | { |
562 | { |
… | |
… | |
641 | LOG (llevError, "Couldn't find archetype.\n"); |
604 | LOG (llevError, "Couldn't find archetype.\n"); |
642 | |
605 | |
643 | return NULL; |
606 | return NULL; |
644 | } |
607 | } |
645 | |
608 | |
646 | op = get_object (); |
609 | op = at->clone.clone (); |
647 | copy_object (&at->clone, op); |
|
|
648 | op->arch = at; |
610 | op->arch = at; |
649 | op->instantiate (); |
611 | op->instantiate (); |
650 | return op; |
612 | return op; |
651 | } |
613 | } |
652 | |
614 | |
… | |
… | |
662 | { |
624 | { |
663 | object *op; |
625 | object *op; |
664 | char buf[MAX_BUF]; |
626 | char buf[MAX_BUF]; |
665 | |
627 | |
666 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
628 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
667 | op = get_object (); |
629 | op = object::create (); |
668 | op->name = op->name_pl = buf; |
630 | op->name = op->name_pl = buf; |
669 | SET_FLAG (op, FLAG_NO_PICK); |
631 | SET_FLAG (op, FLAG_NO_PICK); |
670 | return op; |
632 | return op; |
671 | } |
633 | } |
672 | |
634 | |
… | |
… | |
764 | } |
726 | } |
765 | |
727 | |
766 | /* |
728 | /* |
767 | * Adds an archetype to the hashtable. |
729 | * Adds an archetype to the hashtable. |
768 | */ |
730 | */ |
769 | |
731 | void |
770 | static void |
732 | archetype::hash_add () |
771 | add_arch (archetype *at) |
|
|
772 | { |
733 | { |
773 | #if USE_UNORDERED_MAP |
734 | #if USE_UNORDERED_MAP |
774 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
735 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
775 | #endif |
736 | #else |
776 | |
737 | |
777 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
738 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
778 | |
739 | |
779 | for (;;) |
740 | for (;;) |
780 | { |
741 | { |
781 | if (arch_table[index] == NULL) |
742 | if (!arch_table[index]) |
782 | { |
743 | { |
783 | arch_table[index] = at; |
744 | arch_table[index] = this; |
784 | return; |
745 | break; |
785 | } |
746 | } |
786 | |
747 | |
787 | if (++index == ARCHTABLE) |
748 | if (++index == ARCHTABLE) |
788 | index = 0; |
749 | index = 0; |
789 | |
750 | |
790 | if (index == org_index) |
751 | if (index == org_index) |
791 | fatal (ARCHTABLE_TOO_SMALL); |
752 | fatal (ARCHTABLE_TOO_SMALL); |
792 | } |
753 | } |
|
|
754 | #endif |
|
|
755 | } |
|
|
756 | |
|
|
757 | void |
|
|
758 | archetype::hash_del () |
|
|
759 | { |
|
|
760 | #if USE_UNORDERED_MAP |
|
|
761 | # error remove this from HT |
|
|
762 | #else |
|
|
763 | |
|
|
764 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
|
|
765 | |
|
|
766 | for (;;) |
|
|
767 | { |
|
|
768 | if (arch_table[index] == this) |
|
|
769 | { |
|
|
770 | arch_table[index] = 0; |
|
|
771 | break; |
|
|
772 | } |
|
|
773 | |
|
|
774 | if (++index == ARCHTABLE) |
|
|
775 | index = 0; |
|
|
776 | |
|
|
777 | if (index == org_index) |
|
|
778 | break; |
|
|
779 | } |
|
|
780 | #endif |
793 | } |
781 | } |
794 | |
782 | |
795 | /* |
783 | /* |
796 | * Returns the first archetype using the given type. |
784 | * Returns the first archetype using the given type. |
797 | * Used in treasure-generation. |
785 | * Used in treasure-generation. |
… | |
… | |
817 | |
805 | |
818 | object * |
806 | object * |
819 | clone_arch (int type) |
807 | clone_arch (int type) |
820 | { |
808 | { |
821 | archetype *at; |
809 | archetype *at; |
822 | object *op = get_object (); |
|
|
823 | |
810 | |
824 | if ((at = type_to_archetype (type)) == NULL) |
811 | if ((at = type_to_archetype (type)) == NULL) |
825 | { |
812 | { |
826 | LOG (llevError, "Can't clone archetype %d\n", type); |
813 | LOG (llevError, "Can't clone archetype %d\n", type); |
827 | free_object (op); |
|
|
828 | return NULL; |
814 | return 0; |
829 | } |
815 | } |
830 | |
816 | |
831 | copy_object (&at->clone, op); |
817 | object *op = at->clone.clone (); |
832 | op->instantiate (); |
818 | op->instantiate (); |
833 | return op; |
819 | return op; |
834 | } |
820 | } |
835 | |
821 | |
836 | /* |
822 | /* |