… | |
… | |
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 | } |
|
|
367 | |
|
|
368 | /* |
|
|
369 | * Dumps an archetype to debug-level output. |
|
|
370 | */ |
|
|
371 | |
|
|
372 | void |
|
|
373 | dump_arch (archetype *at) |
|
|
374 | { |
|
|
375 | dump_object (&at->clone); |
|
|
376 | } |
|
|
377 | |
|
|
378 | /* |
|
|
379 | * Dumps _all_ archetypes to debug-level output. |
|
|
380 | * If you run crossfire with debug, and enter DM-mode, you can trigger |
|
|
381 | * this with the O key. |
|
|
382 | */ |
|
|
383 | |
|
|
384 | void |
|
|
385 | dump_all_archetypes (void) |
|
|
386 | { |
|
|
387 | archetype *at; |
|
|
388 | |
|
|
389 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
|
|
390 | { |
|
|
391 | dump_arch (at); |
|
|
392 | fprintf (logfile, "%s\n", errmsg); |
|
|
393 | } |
|
|
394 | } |
366 | } |
395 | |
367 | |
396 | void |
368 | void |
397 | free_all_archs (void) |
369 | free_all_archs (void) |
398 | { |
370 | { |
… | |
… | |
409 | delete |
381 | delete |
410 | at; |
382 | at; |
411 | |
383 | |
412 | i++; |
384 | i++; |
413 | } |
385 | } |
|
|
386 | |
414 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
387 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
415 | } |
388 | } |
416 | |
389 | |
417 | archetype::archetype () |
390 | archetype::archetype () |
418 | { |
391 | { |
419 | clear_object (&clone); /* to initial state other also */ |
|
|
420 | 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 */ |
421 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
393 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
422 | } |
394 | } |
423 | |
395 | |
424 | archetype::~archetype () |
396 | archetype::~archetype () |
425 | { |
397 | { |
… | |
… | |
430 | * of archetype-structures. |
402 | * of archetype-structures. |
431 | */ |
403 | */ |
432 | void |
404 | void |
433 | first_arch_pass (object_thawer & fp) |
405 | first_arch_pass (object_thawer & fp) |
434 | { |
406 | { |
435 | archetype *at, *head = NULL, *last_more = NULL; |
407 | archetype *head = 0, *last_more = 0; |
436 | |
408 | |
|
|
409 | archetype *at = new archetype; |
437 | at->clone.arch = first_archetype = at = new archetype; |
410 | at->clone.arch = first_archetype = at; |
438 | |
411 | |
439 | while (int i = load_object (fp, &at->clone, 0)) |
412 | while (int i = load_object (fp, &at->clone, 0)) |
440 | { |
413 | { |
441 | at->clone.speed_left = (float) (-0.1); |
414 | at->clone.speed_left = (float) (-0.1); |
442 | /* 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 |
… | |
… | |
545 | } |
518 | } |
546 | else if (!strcmp ("randomitems", variable)) |
519 | else if (!strcmp ("randomitems", variable)) |
547 | { |
520 | { |
548 | if (at != NULL) |
521 | if (at != NULL) |
549 | { |
522 | { |
550 | treasurelist * |
|
|
551 | tl = find_treasurelist (argument); |
523 | treasurelist *tl = find_treasurelist (argument); |
552 | |
524 | |
553 | if (tl == NULL) |
525 | if (tl == NULL) |
554 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
526 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
555 | else |
527 | else |
556 | at->clone.randomitems = tl; |
528 | at->clone.randomitems = tl; |
… | |
… | |
581 | void |
553 | void |
582 | load_archetypes (void) |
554 | load_archetypes (void) |
583 | { |
555 | { |
584 | char filename[MAX_BUF]; |
556 | char filename[MAX_BUF]; |
585 | |
557 | |
586 | #if TIME_ARCH_LOAD |
|
|
587 | struct timeval |
|
|
588 | tv1, |
|
|
589 | tv2; |
|
|
590 | #endif |
|
|
591 | |
|
|
592 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
558 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
593 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
559 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
594 | |
560 | |
595 | { |
561 | { |
596 | object_thawer |
562 | object_thawer |
… | |
… | |
609 | object_thawer |
575 | object_thawer |
610 | thawer (filename); |
576 | thawer (filename); |
611 | |
577 | |
612 | LOG (llevDebug, " loading treasure...\n"); |
578 | LOG (llevDebug, " loading treasure...\n"); |
613 | load_treasures (); |
579 | load_treasures (); |
|
|
580 | LOG (llevDebug, " done\n"); |
614 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
581 | LOG (llevDebug, " arch-pass 2...\n"); |
615 | second_arch_pass (thawer); |
582 | second_arch_pass (thawer); |
616 | LOG (llevDebug, " done\n"); |
583 | LOG (llevDebug, " done\n"); |
617 | #ifdef DEBUG |
584 | #ifdef DEBUG |
618 | check_generators (); |
585 | check_generators (); |
619 | #endif |
586 | #endif |
… | |
… | |
623 | |
590 | |
624 | /* |
591 | /* |
625 | * Creates and returns a new object which is a copy of the given archetype. |
592 | * Creates and returns a new object which is a copy of the given archetype. |
626 | * This function returns NULL on failure. |
593 | * This function returns NULL on failure. |
627 | */ |
594 | */ |
628 | |
|
|
629 | object * |
595 | object * |
630 | arch_to_object (archetype *at) |
596 | arch_to_object (archetype *at) |
631 | { |
597 | { |
632 | object *op; |
598 | object *op; |
633 | |
599 | |
… | |
… | |
637 | LOG (llevError, "Couldn't find archetype.\n"); |
603 | LOG (llevError, "Couldn't find archetype.\n"); |
638 | |
604 | |
639 | return NULL; |
605 | return NULL; |
640 | } |
606 | } |
641 | |
607 | |
642 | op = get_object (); |
608 | op = at->clone.clone (); |
643 | copy_object (&at->clone, op); |
|
|
644 | op->arch = at; |
609 | op->arch = at; |
645 | op->instantiate (); |
610 | op->instantiate (); |
646 | return op; |
611 | return op; |
647 | } |
612 | } |
648 | |
613 | |
… | |
… | |
650 | * Creates an object. This function is called by get_archetype() |
615 | * Creates an object. This function is called by get_archetype() |
651 | * if it fails to find the appropriate archetype. |
616 | * if it fails to find the appropriate archetype. |
652 | * Thus get_archetype() will be guaranteed to always return |
617 | * Thus get_archetype() will be guaranteed to always return |
653 | * an object, and never NULL. |
618 | * an object, and never NULL. |
654 | */ |
619 | */ |
655 | |
|
|
656 | object * |
620 | object * |
657 | create_singularity (const char *name) |
621 | create_singularity (const char *name) |
658 | { |
622 | { |
659 | object *op; |
623 | object *op; |
660 | char buf[MAX_BUF]; |
624 | char buf[MAX_BUF]; |
661 | |
625 | |
662 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
626 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
663 | op = get_object (); |
627 | op = object::create (); |
664 | op->name = op->name_pl = buf; |
628 | op->name = op->name_pl = buf; |
665 | SET_FLAG (op, FLAG_NO_PICK); |
629 | SET_FLAG (op, FLAG_NO_PICK); |
666 | return op; |
630 | return op; |
667 | } |
631 | } |
668 | |
632 | |
669 | /* |
633 | /* |
670 | * Finds which archetype matches the given name, and returns a new |
634 | * Finds which archetype matches the given name, and returns a new |
671 | * object containing a copy of the archetype. |
635 | * object containing a copy of the archetype. |
672 | */ |
636 | */ |
673 | |
|
|
674 | object * |
637 | object * |
675 | get_archetype (const char *name) |
638 | get_archetype (const char *name) |
676 | { |
639 | { |
677 | archetype *at = archetype::find (name); |
640 | archetype *at = archetype::find (name); |
678 | |
641 | |
… | |
… | |
760 | } |
723 | } |
761 | |
724 | |
762 | /* |
725 | /* |
763 | * Adds an archetype to the hashtable. |
726 | * Adds an archetype to the hashtable. |
764 | */ |
727 | */ |
765 | |
728 | void |
766 | static void |
729 | archetype::hash_add () |
767 | add_arch (archetype *at) |
|
|
768 | { |
730 | { |
769 | #if USE_UNORDERED_MAP |
731 | #if USE_UNORDERED_MAP |
770 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
732 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
771 | #endif |
733 | #else |
772 | |
734 | |
773 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
735 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
774 | |
736 | |
775 | for (;;) |
737 | for (;;) |
776 | { |
738 | { |
777 | if (arch_table[index] == NULL) |
739 | if (!arch_table[index]) |
778 | { |
740 | { |
779 | arch_table[index] = at; |
741 | arch_table[index] = this; |
780 | return; |
742 | break; |
781 | } |
743 | } |
782 | |
744 | |
783 | if (++index == ARCHTABLE) |
745 | if (++index == ARCHTABLE) |
784 | index = 0; |
746 | index = 0; |
785 | |
747 | |
786 | if (index == org_index) |
748 | if (index == org_index) |
787 | fatal (ARCHTABLE_TOO_SMALL); |
749 | fatal (ARCHTABLE_TOO_SMALL); |
788 | } |
750 | } |
|
|
751 | #endif |
|
|
752 | } |
|
|
753 | |
|
|
754 | void |
|
|
755 | archetype::hash_del () |
|
|
756 | { |
|
|
757 | #if USE_UNORDERED_MAP |
|
|
758 | # error remove this from HT |
|
|
759 | #else |
|
|
760 | |
|
|
761 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
|
|
762 | |
|
|
763 | for (;;) |
|
|
764 | { |
|
|
765 | if (arch_table[index] == this) |
|
|
766 | { |
|
|
767 | arch_table[index] = 0; |
|
|
768 | break; |
|
|
769 | } |
|
|
770 | |
|
|
771 | if (++index == ARCHTABLE) |
|
|
772 | index = 0; |
|
|
773 | |
|
|
774 | if (index == org_index) |
|
|
775 | break; |
|
|
776 | } |
|
|
777 | #endif |
789 | } |
778 | } |
790 | |
779 | |
791 | /* |
780 | /* |
792 | * Returns the first archetype using the given type. |
781 | * Returns the first archetype using the given type. |
793 | * Used in treasure-generation. |
782 | * Used in treasure-generation. |
… | |
… | |
813 | |
802 | |
814 | object * |
803 | object * |
815 | clone_arch (int type) |
804 | clone_arch (int type) |
816 | { |
805 | { |
817 | archetype *at; |
806 | archetype *at; |
818 | object *op = get_object (); |
|
|
819 | |
807 | |
820 | if ((at = type_to_archetype (type)) == NULL) |
808 | if ((at = type_to_archetype (type)) == NULL) |
821 | { |
809 | { |
822 | LOG (llevError, "Can't clone archetype %d\n", type); |
810 | LOG (llevError, "Can't clone archetype %d\n", type); |
823 | free_object (op); |
|
|
824 | return NULL; |
811 | return 0; |
825 | } |
812 | } |
826 | |
813 | |
827 | copy_object (&at->clone, op); |
814 | object *op = at->clone.clone (); |
828 | op->instantiate (); |
815 | op->instantiate (); |
829 | return op; |
816 | return op; |
830 | } |
817 | } |
831 | |
818 | |
832 | /* |
819 | /* |