… | |
… | |
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"). |
… | |
… | |
118 | * If type is -1, ew don't match on type. |
118 | * If type is -1, ew don't match on type. |
119 | */ |
119 | */ |
120 | object * |
120 | object * |
121 | get_archetype_by_skill_name (const char *skill, int type) |
121 | get_archetype_by_skill_name (const char *skill, int type) |
122 | { |
122 | { |
123 | archetype * |
123 | archetype *at; |
124 | at; |
|
|
125 | |
124 | |
126 | if (skill == NULL) |
125 | if (skill) |
127 | return NULL; |
|
|
128 | |
|
|
129 | for (at = first_archetype; at != NULL; at = at->next) |
126 | for (at = first_archetype; at; at = at->next) |
130 | { |
|
|
131 | 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))) |
132 | return arch_to_object (at); |
128 | return arch_to_object (at); |
133 | } |
129 | |
134 | return NULL; |
130 | return 0; |
135 | } |
131 | } |
136 | |
132 | |
137 | /* similiar to above - this returns the first archetype |
133 | /* similiar to above - this returns the first archetype |
138 | * that matches both the type and subtype. type and subtype |
134 | * that matches both the type and subtype. type and subtype |
139 | * 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 |
… | |
… | |
179 | for (i = strlen (tmpname); i > 0; i--) |
175 | for (i = strlen (tmpname); i > 0; i--) |
180 | { |
176 | { |
181 | tmpname[i] = 0; |
177 | tmpname[i] = 0; |
182 | at = find_archetype_by_object_name (tmpname); |
178 | at = find_archetype_by_object_name (tmpname); |
183 | |
179 | |
184 | if (at != NULL) |
180 | if (at) |
185 | return arch_to_object (at); |
181 | return arch_to_object (at); |
186 | } |
182 | } |
187 | |
183 | |
188 | return create_singularity (name); |
184 | return create_singularity (name); |
189 | } |
185 | } |
… | |
… | |
358 | archetype *at; |
354 | archetype *at; |
359 | |
355 | |
360 | LOG (llevDebug, " Setting up archetable...\n"); |
356 | LOG (llevDebug, " Setting up archetable...\n"); |
361 | |
357 | |
362 | 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) |
363 | add_arch (at); |
359 | at->hash_add (); |
364 | |
360 | |
365 | LOG (llevDebug, "done\n"); |
361 | LOG (llevDebug, "done\n"); |
366 | } |
362 | } |
367 | |
363 | |
368 | void |
364 | void |
… | |
… | |
381 | delete |
377 | delete |
382 | at; |
378 | at; |
383 | |
379 | |
384 | i++; |
380 | i++; |
385 | } |
381 | } |
|
|
382 | |
386 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
383 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
387 | } |
384 | } |
388 | |
385 | |
389 | archetype::archetype () |
386 | archetype::archetype () |
390 | { |
387 | { |
391 | clear_object (&clone); /* to initial state other also */ |
|
|
392 | 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 */ |
393 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
389 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
394 | } |
390 | } |
395 | |
391 | |
396 | archetype::~archetype () |
392 | archetype::~archetype () |
397 | { |
393 | { |
… | |
… | |
402 | * of archetype-structures. |
398 | * of archetype-structures. |
403 | */ |
399 | */ |
404 | void |
400 | void |
405 | first_arch_pass (object_thawer & fp) |
401 | first_arch_pass (object_thawer & fp) |
406 | { |
402 | { |
407 | archetype *at, *head = NULL, *last_more = NULL; |
403 | archetype *head = 0, *last_more = 0; |
408 | |
404 | |
|
|
405 | archetype *at = new archetype; |
409 | at->clone.arch = first_archetype = at = new archetype; |
406 | at->clone.arch = first_archetype = at; |
410 | |
407 | |
411 | while (int i = load_object (fp, &at->clone, 0)) |
408 | while (int i = load_object (fp, &at->clone, 0)) |
412 | { |
409 | { |
413 | at->clone.speed_left = (float) (-0.1); |
410 | at->clone.speed_left = (float) (-0.1); |
414 | /* 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 |
… | |
… | |
552 | void |
549 | void |
553 | load_archetypes (void) |
550 | load_archetypes (void) |
554 | { |
551 | { |
555 | char filename[MAX_BUF]; |
552 | char filename[MAX_BUF]; |
556 | |
553 | |
557 | #if TIME_ARCH_LOAD |
|
|
558 | struct timeval |
|
|
559 | tv1, |
|
|
560 | tv2; |
|
|
561 | #endif |
|
|
562 | |
|
|
563 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
554 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
564 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
555 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
565 | |
556 | |
566 | { |
557 | { |
567 | object_thawer |
558 | object_thawer |
… | |
… | |
580 | object_thawer |
571 | object_thawer |
581 | thawer (filename); |
572 | thawer (filename); |
582 | |
573 | |
583 | LOG (llevDebug, " loading treasure...\n"); |
574 | LOG (llevDebug, " loading treasure...\n"); |
584 | load_treasures (); |
575 | load_treasures (); |
|
|
576 | LOG (llevDebug, " done\n"); |
585 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
577 | LOG (llevDebug, " arch-pass 2...\n"); |
586 | second_arch_pass (thawer); |
578 | second_arch_pass (thawer); |
587 | LOG (llevDebug, " done\n"); |
579 | LOG (llevDebug, " done\n"); |
588 | #ifdef DEBUG |
580 | #ifdef DEBUG |
589 | check_generators (); |
581 | check_generators (); |
590 | #endif |
582 | #endif |
… | |
… | |
594 | |
586 | |
595 | /* |
587 | /* |
596 | * 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. |
597 | * This function returns NULL on failure. |
589 | * This function returns NULL on failure. |
598 | */ |
590 | */ |
599 | |
|
|
600 | object * |
591 | object * |
601 | arch_to_object (archetype *at) |
592 | arch_to_object (archetype *at) |
602 | { |
593 | { |
603 | object *op; |
594 | object *op; |
604 | |
595 | |
… | |
… | |
608 | LOG (llevError, "Couldn't find archetype.\n"); |
599 | LOG (llevError, "Couldn't find archetype.\n"); |
609 | |
600 | |
610 | return NULL; |
601 | return NULL; |
611 | } |
602 | } |
612 | |
603 | |
613 | op = get_object (); |
604 | op = at->clone.clone (); |
614 | copy_object (&at->clone, op); |
|
|
615 | op->arch = at; |
605 | op->arch = at; |
616 | op->instantiate (); |
606 | op->instantiate (); |
617 | return op; |
607 | return op; |
618 | } |
608 | } |
619 | |
609 | |
… | |
… | |
621 | * Creates an object. This function is called by get_archetype() |
611 | * Creates an object. This function is called by get_archetype() |
622 | * if it fails to find the appropriate archetype. |
612 | * if it fails to find the appropriate archetype. |
623 | * Thus get_archetype() will be guaranteed to always return |
613 | * Thus get_archetype() will be guaranteed to always return |
624 | * an object, and never NULL. |
614 | * an object, and never NULL. |
625 | */ |
615 | */ |
626 | |
|
|
627 | object * |
616 | object * |
628 | create_singularity (const char *name) |
617 | create_singularity (const char *name) |
629 | { |
618 | { |
630 | object *op; |
619 | object *op; |
631 | char buf[MAX_BUF]; |
620 | char buf[MAX_BUF]; |
632 | |
621 | |
633 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
622 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
634 | op = get_object (); |
623 | op = object::create (); |
635 | op->name = op->name_pl = buf; |
624 | op->name = op->name_pl = buf; |
636 | SET_FLAG (op, FLAG_NO_PICK); |
625 | SET_FLAG (op, FLAG_NO_PICK); |
637 | return op; |
626 | return op; |
638 | } |
627 | } |
639 | |
628 | |
640 | /* |
629 | /* |
641 | * Finds which archetype matches the given name, and returns a new |
630 | * Finds which archetype matches the given name, and returns a new |
642 | * object containing a copy of the archetype. |
631 | * object containing a copy of the archetype. |
643 | */ |
632 | */ |
644 | |
|
|
645 | object * |
633 | object * |
646 | get_archetype (const char *name) |
634 | get_archetype (const char *name) |
647 | { |
635 | { |
648 | archetype *at = archetype::find (name); |
636 | archetype *at = archetype::find (name); |
649 | |
637 | |
… | |
… | |
731 | } |
719 | } |
732 | |
720 | |
733 | /* |
721 | /* |
734 | * Adds an archetype to the hashtable. |
722 | * Adds an archetype to the hashtable. |
735 | */ |
723 | */ |
736 | |
724 | void |
737 | static void |
725 | archetype::hash_add () |
738 | add_arch (archetype *at) |
|
|
739 | { |
726 | { |
740 | #if USE_UNORDERED_MAP |
727 | #if USE_UNORDERED_MAP |
741 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
728 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
742 | #endif |
729 | #else |
743 | |
730 | |
744 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
731 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
745 | |
732 | |
746 | for (;;) |
733 | for (;;) |
747 | { |
734 | { |
748 | if (arch_table[index] == NULL) |
735 | if (!arch_table[index]) |
749 | { |
736 | { |
750 | arch_table[index] = at; |
737 | arch_table[index] = this; |
751 | return; |
738 | break; |
752 | } |
739 | } |
753 | |
740 | |
754 | if (++index == ARCHTABLE) |
741 | if (++index == ARCHTABLE) |
755 | index = 0; |
742 | index = 0; |
756 | |
743 | |
757 | if (index == org_index) |
744 | if (index == org_index) |
758 | fatal (ARCHTABLE_TOO_SMALL); |
745 | fatal (ARCHTABLE_TOO_SMALL); |
759 | } |
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 |
760 | } |
774 | } |
761 | |
775 | |
762 | /* |
776 | /* |
763 | * Returns the first archetype using the given type. |
777 | * Returns the first archetype using the given type. |
764 | * Used in treasure-generation. |
778 | * Used in treasure-generation. |
… | |
… | |
784 | |
798 | |
785 | object * |
799 | object * |
786 | clone_arch (int type) |
800 | clone_arch (int type) |
787 | { |
801 | { |
788 | archetype *at; |
802 | archetype *at; |
789 | object *op = get_object (); |
|
|
790 | |
803 | |
791 | if ((at = type_to_archetype (type)) == NULL) |
804 | if ((at = type_to_archetype (type)) == NULL) |
792 | { |
805 | { |
793 | LOG (llevError, "Can't clone archetype %d\n", type); |
806 | LOG (llevError, "Can't clone archetype %d\n", type); |
794 | free_object (op); |
|
|
795 | return NULL; |
807 | return 0; |
796 | } |
808 | } |
797 | |
809 | |
798 | copy_object (&at->clone, op); |
810 | object *op = at->clone.clone (); |
799 | op->instantiate (); |
811 | op->instantiate (); |
800 | return op; |
812 | return op; |
801 | } |
813 | } |
802 | |
814 | |
803 | /* |
815 | /* |