… | |
… | |
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"). |
… | |
… | |
358 | archetype *at; |
358 | archetype *at; |
359 | |
359 | |
360 | LOG (llevDebug, " Setting up archetable...\n"); |
360 | LOG (llevDebug, " Setting up archetable...\n"); |
361 | |
361 | |
362 | 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) |
363 | add_arch (at); |
363 | at->hash_add (); |
364 | |
364 | |
365 | LOG (llevDebug, "done\n"); |
365 | LOG (llevDebug, "done\n"); |
366 | } |
366 | } |
367 | |
367 | |
368 | void |
368 | void |
… | |
… | |
381 | delete |
381 | delete |
382 | at; |
382 | at; |
383 | |
383 | |
384 | i++; |
384 | i++; |
385 | } |
385 | } |
|
|
386 | |
386 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
387 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
387 | } |
388 | } |
388 | |
389 | |
389 | archetype::archetype () |
390 | archetype::archetype () |
390 | { |
391 | { |
… | |
… | |
402 | * of archetype-structures. |
403 | * of archetype-structures. |
403 | */ |
404 | */ |
404 | void |
405 | void |
405 | first_arch_pass (object_thawer & fp) |
406 | first_arch_pass (object_thawer & fp) |
406 | { |
407 | { |
407 | archetype *at, *head = NULL, *last_more = NULL; |
408 | archetype *head = 0, *last_more = 0; |
408 | |
409 | |
|
|
410 | archetype *at = new archetype; |
409 | at->clone.arch = first_archetype = at = new archetype; |
411 | at->clone.arch = first_archetype = at; |
410 | |
412 | |
411 | while (int i = load_object (fp, &at->clone, 0)) |
413 | while (int i = load_object (fp, &at->clone, 0)) |
412 | { |
414 | { |
413 | at->clone.speed_left = (float) (-0.1); |
415 | at->clone.speed_left = (float) (-0.1); |
414 | /* 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 |
… | |
… | |
552 | void |
554 | void |
553 | load_archetypes (void) |
555 | load_archetypes (void) |
554 | { |
556 | { |
555 | char filename[MAX_BUF]; |
557 | char filename[MAX_BUF]; |
556 | |
558 | |
557 | #if TIME_ARCH_LOAD |
|
|
558 | struct timeval |
|
|
559 | tv1, |
|
|
560 | tv2; |
|
|
561 | #endif |
|
|
562 | |
|
|
563 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
559 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
564 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
560 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
565 | |
561 | |
566 | { |
562 | { |
567 | object_thawer |
563 | object_thawer |
… | |
… | |
731 | } |
727 | } |
732 | |
728 | |
733 | /* |
729 | /* |
734 | * Adds an archetype to the hashtable. |
730 | * Adds an archetype to the hashtable. |
735 | */ |
731 | */ |
736 | |
732 | void |
737 | static void |
733 | archetype::hash_add () |
738 | add_arch (archetype *at) |
|
|
739 | { |
734 | { |
740 | #if USE_UNORDERED_MAP |
735 | #if USE_UNORDERED_MAP |
741 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
736 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
742 | #endif |
737 | #else |
743 | |
738 | |
744 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
739 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
745 | |
740 | |
746 | for (;;) |
741 | for (;;) |
747 | { |
742 | { |
748 | if (arch_table[index] == NULL) |
743 | if (!arch_table[index]) |
749 | { |
744 | { |
750 | arch_table[index] = at; |
745 | arch_table[index] = this; |
751 | return; |
746 | break; |
752 | } |
747 | } |
753 | |
748 | |
754 | if (++index == ARCHTABLE) |
749 | if (++index == ARCHTABLE) |
755 | index = 0; |
750 | index = 0; |
756 | |
751 | |
757 | if (index == org_index) |
752 | if (index == org_index) |
758 | fatal (ARCHTABLE_TOO_SMALL); |
753 | fatal (ARCHTABLE_TOO_SMALL); |
759 | } |
754 | } |
|
|
755 | #endif |
|
|
756 | } |
|
|
757 | |
|
|
758 | void |
|
|
759 | archetype::hash_del () |
|
|
760 | { |
|
|
761 | #if USE_UNORDERED_MAP |
|
|
762 | # error remove this from HT |
|
|
763 | #else |
|
|
764 | |
|
|
765 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
|
|
766 | |
|
|
767 | for (;;) |
|
|
768 | { |
|
|
769 | if (arch_table[index] == this) |
|
|
770 | { |
|
|
771 | arch_table[index] = 0; |
|
|
772 | break; |
|
|
773 | } |
|
|
774 | |
|
|
775 | if (++index == ARCHTABLE) |
|
|
776 | index = 0; |
|
|
777 | |
|
|
778 | if (index == org_index) |
|
|
779 | break; |
|
|
780 | } |
|
|
781 | #endif |
760 | } |
782 | } |
761 | |
783 | |
762 | /* |
784 | /* |
763 | * Returns the first archetype using the given type. |
785 | * Returns the first archetype using the given type. |
764 | * Used in treasure-generation. |
786 | * Used in treasure-generation. |