… | |
… | |
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 | { |
391 | clear_object (&clone); /* to initial state other also */ |
|
|
392 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ |
392 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ |
393 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
393 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
394 | } |
394 | } |
395 | |
395 | |
396 | archetype::~archetype () |
396 | archetype::~archetype () |
397 | { |
397 | { |
… | |
… | |
402 | * of archetype-structures. |
402 | * of archetype-structures. |
403 | */ |
403 | */ |
404 | void |
404 | void |
405 | first_arch_pass (object_thawer & fp) |
405 | first_arch_pass (object_thawer & fp) |
406 | { |
406 | { |
407 | archetype *at, *head = NULL, *last_more = NULL; |
407 | archetype *head = 0, *last_more = 0; |
408 | |
408 | |
|
|
409 | archetype *at = new archetype; |
409 | at->clone.arch = first_archetype = at = new archetype; |
410 | at->clone.arch = first_archetype = at; |
410 | |
411 | |
411 | while (int i = load_object (fp, &at->clone, 0)) |
412 | while (int i = load_object (fp, &at->clone, 0)) |
412 | { |
413 | { |
413 | at->clone.speed_left = (float) (-0.1); |
414 | at->clone.speed_left = (float) (-0.1); |
414 | /* copy the body_info to the body_used - this is only really |
415 | /* copy the body_info to the body_used - this is only really |
… | |
… | |
552 | void |
553 | void |
553 | load_archetypes (void) |
554 | load_archetypes (void) |
554 | { |
555 | { |
555 | char filename[MAX_BUF]; |
556 | char filename[MAX_BUF]; |
556 | |
557 | |
557 | #if TIME_ARCH_LOAD |
|
|
558 | struct timeval |
|
|
559 | tv1, |
|
|
560 | tv2; |
|
|
561 | #endif |
|
|
562 | |
|
|
563 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
558 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
564 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
559 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
565 | |
560 | |
566 | { |
561 | { |
567 | object_thawer |
562 | object_thawer |
… | |
… | |
594 | |
589 | |
595 | /* |
590 | /* |
596 | * Creates and returns a new object which is a copy of the given archetype. |
591 | * Creates and returns a new object which is a copy of the given archetype. |
597 | * This function returns NULL on failure. |
592 | * This function returns NULL on failure. |
598 | */ |
593 | */ |
599 | |
|
|
600 | object * |
594 | object * |
601 | arch_to_object (archetype *at) |
595 | arch_to_object (archetype *at) |
602 | { |
596 | { |
603 | object *op; |
597 | object *op; |
604 | |
598 | |
… | |
… | |
608 | LOG (llevError, "Couldn't find archetype.\n"); |
602 | LOG (llevError, "Couldn't find archetype.\n"); |
609 | |
603 | |
610 | return NULL; |
604 | return NULL; |
611 | } |
605 | } |
612 | |
606 | |
613 | op = get_object (); |
607 | op = at->clone.clone (); |
614 | copy_object (&at->clone, op); |
|
|
615 | op->arch = at; |
608 | op->arch = at; |
616 | op->instantiate (); |
609 | op->instantiate (); |
617 | return op; |
610 | return op; |
618 | } |
611 | } |
619 | |
612 | |
… | |
… | |
621 | * Creates an object. This function is called by get_archetype() |
614 | * Creates an object. This function is called by get_archetype() |
622 | * if it fails to find the appropriate archetype. |
615 | * if it fails to find the appropriate archetype. |
623 | * Thus get_archetype() will be guaranteed to always return |
616 | * Thus get_archetype() will be guaranteed to always return |
624 | * an object, and never NULL. |
617 | * an object, and never NULL. |
625 | */ |
618 | */ |
626 | |
|
|
627 | object * |
619 | object * |
628 | create_singularity (const char *name) |
620 | create_singularity (const char *name) |
629 | { |
621 | { |
630 | object *op; |
622 | object *op; |
631 | char buf[MAX_BUF]; |
623 | char buf[MAX_BUF]; |
632 | |
624 | |
633 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
625 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
634 | op = get_object (); |
626 | op = object::create (); |
635 | op->name = op->name_pl = buf; |
627 | op->name = op->name_pl = buf; |
636 | SET_FLAG (op, FLAG_NO_PICK); |
628 | SET_FLAG (op, FLAG_NO_PICK); |
637 | return op; |
629 | return op; |
638 | } |
630 | } |
639 | |
631 | |
640 | /* |
632 | /* |
641 | * Finds which archetype matches the given name, and returns a new |
633 | * Finds which archetype matches the given name, and returns a new |
642 | * object containing a copy of the archetype. |
634 | * object containing a copy of the archetype. |
643 | */ |
635 | */ |
644 | |
|
|
645 | object * |
636 | object * |
646 | get_archetype (const char *name) |
637 | get_archetype (const char *name) |
647 | { |
638 | { |
648 | archetype *at = archetype::find (name); |
639 | archetype *at = archetype::find (name); |
649 | |
640 | |
… | |
… | |
731 | } |
722 | } |
732 | |
723 | |
733 | /* |
724 | /* |
734 | * Adds an archetype to the hashtable. |
725 | * Adds an archetype to the hashtable. |
735 | */ |
726 | */ |
736 | |
727 | void |
737 | static void |
728 | archetype::hash_add () |
738 | add_arch (archetype *at) |
|
|
739 | { |
729 | { |
740 | #if USE_UNORDERED_MAP |
730 | #if USE_UNORDERED_MAP |
741 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
731 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
742 | #endif |
732 | #else |
743 | |
733 | |
744 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
734 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
745 | |
735 | |
746 | for (;;) |
736 | for (;;) |
747 | { |
737 | { |
748 | if (arch_table[index] == NULL) |
738 | if (!arch_table[index]) |
749 | { |
739 | { |
750 | arch_table[index] = at; |
740 | arch_table[index] = this; |
751 | return; |
741 | break; |
752 | } |
742 | } |
753 | |
743 | |
754 | if (++index == ARCHTABLE) |
744 | if (++index == ARCHTABLE) |
755 | index = 0; |
745 | index = 0; |
756 | |
746 | |
757 | if (index == org_index) |
747 | if (index == org_index) |
758 | fatal (ARCHTABLE_TOO_SMALL); |
748 | fatal (ARCHTABLE_TOO_SMALL); |
759 | } |
749 | } |
|
|
750 | #endif |
|
|
751 | } |
|
|
752 | |
|
|
753 | void |
|
|
754 | archetype::hash_del () |
|
|
755 | { |
|
|
756 | #if USE_UNORDERED_MAP |
|
|
757 | # error remove this from HT |
|
|
758 | #else |
|
|
759 | |
|
|
760 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
|
|
761 | |
|
|
762 | for (;;) |
|
|
763 | { |
|
|
764 | if (arch_table[index] == this) |
|
|
765 | { |
|
|
766 | arch_table[index] = 0; |
|
|
767 | break; |
|
|
768 | } |
|
|
769 | |
|
|
770 | if (++index == ARCHTABLE) |
|
|
771 | index = 0; |
|
|
772 | |
|
|
773 | if (index == org_index) |
|
|
774 | break; |
|
|
775 | } |
|
|
776 | #endif |
760 | } |
777 | } |
761 | |
778 | |
762 | /* |
779 | /* |
763 | * Returns the first archetype using the given type. |
780 | * Returns the first archetype using the given type. |
764 | * Used in treasure-generation. |
781 | * Used in treasure-generation. |
… | |
… | |
784 | |
801 | |
785 | object * |
802 | object * |
786 | clone_arch (int type) |
803 | clone_arch (int type) |
787 | { |
804 | { |
788 | archetype *at; |
805 | archetype *at; |
789 | object *op = get_object (); |
|
|
790 | |
806 | |
791 | if ((at = type_to_archetype (type)) == NULL) |
807 | if ((at = type_to_archetype (type)) == NULL) |
792 | { |
808 | { |
793 | LOG (llevError, "Can't clone archetype %d\n", type); |
809 | LOG (llevError, "Can't clone archetype %d\n", type); |
794 | free_object (op); |
|
|
795 | return NULL; |
810 | return 0; |
796 | } |
811 | } |
797 | |
812 | |
798 | copy_object (&at->clone, op); |
813 | object *op = at->clone.clone (); |
799 | op->instantiate (); |
814 | op->instantiate (); |
800 | return op; |
815 | return op; |
801 | } |
816 | } |
802 | |
817 | |
803 | /* |
818 | /* |