… | |
… | |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | |
20 | |
21 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
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> |
26 | #include <tr1/unordered_map> |
… | |
… | |
48 | * MSW 2003-04-29 |
48 | * MSW 2003-04-29 |
49 | */ |
49 | */ |
50 | |
50 | |
51 | #if USE_UNORDERED_MAP |
51 | #if USE_UNORDERED_MAP |
52 | // the hashtable |
52 | // the hashtable |
53 | typedef |
|
|
54 | std::tr1::unordered_map < |
53 | typedef std::tr1::unordered_map |
55 | size_t, archetype *> |
54 | < |
|
|
55 | std::size_t, |
|
|
56 | archetype *, |
|
|
57 | std::hash<size_t>, |
|
|
58 | std::equal_to<size_t>, |
|
|
59 | slice_allocator< std::pair<const std::size_t, archetype *> > |
|
|
60 | true, |
56 | HT; |
61 | > HT; |
57 | |
62 | |
58 | static |
63 | static HT ht; |
59 | HT |
|
|
60 | ht; |
|
|
61 | #endif |
64 | #endif |
62 | |
65 | |
63 | /** |
66 | /** |
64 | * GROS - This function retrieves an archetype given the name that appears |
67 | * GROS - This function retrieves an archetype given the name that appears |
65 | * during the game (for example, "writing pen" instead of "stylus"). |
68 | * during the game (for example, "writing pen" instead of "stylus"). |
… | |
… | |
72 | * - the archetype found or null if nothing was found. |
75 | * - the archetype found or null if nothing was found. |
73 | */ |
76 | */ |
74 | archetype * |
77 | archetype * |
75 | find_archetype_by_object_name (const char *name) |
78 | find_archetype_by_object_name (const char *name) |
76 | { |
79 | { |
77 | archetype * |
80 | archetype *at; |
78 | at; |
|
|
79 | |
81 | |
80 | if (name == NULL) |
82 | if (name == NULL) |
81 | return (archetype *) NULL; |
83 | return (archetype *) NULL; |
82 | |
84 | |
83 | for (at = first_archetype; at != NULL; at = at->next) |
85 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
94 | * except that it considers only items of the given type. |
96 | * except that it considers only items of the given type. |
95 | */ |
97 | */ |
96 | archetype * |
98 | archetype * |
97 | find_archetype_by_object_type_name (int type, const char *name) |
99 | find_archetype_by_object_type_name (int type, const char *name) |
98 | { |
100 | { |
99 | archetype * |
101 | archetype *at; |
100 | at; |
|
|
101 | |
102 | |
102 | if (name == NULL) |
103 | if (name == NULL) |
103 | return NULL; |
104 | return NULL; |
104 | |
105 | |
105 | for (at = first_archetype; at != NULL; at = at->next) |
106 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
179 | { |
180 | { |
180 | tmpname[i] = 0; |
181 | tmpname[i] = 0; |
181 | at = find_archetype_by_object_name (tmpname); |
182 | at = find_archetype_by_object_name (tmpname); |
182 | |
183 | |
183 | if (at != NULL) |
184 | if (at != NULL) |
184 | { |
|
|
185 | return arch_to_object (at); |
185 | return arch_to_object (at); |
186 | } |
|
|
187 | } |
186 | } |
188 | |
187 | |
189 | return create_singularity (name); |
188 | return create_singularity (name); |
190 | } |
189 | } |
191 | |
190 | |
… | |
… | |
207 | * Last, make a check on the full name. |
206 | * Last, make a check on the full name. |
208 | */ |
207 | */ |
209 | int |
208 | int |
210 | item_matched_string (object *pl, object *op, const char *name) |
209 | item_matched_string (object *pl, object *op, const char *name) |
211 | { |
210 | { |
212 | char * |
|
|
213 | cp, |
|
|
214 | local_name[MAX_BUF]; |
211 | char *cp, local_name[MAX_BUF]; |
215 | int |
212 | int count, retval = 0; |
216 | count, |
|
|
217 | retval = 0; |
|
|
218 | |
213 | |
219 | strcpy (local_name, name); /* strtok is destructive to name */ |
214 | strcpy (local_name, name); /* strtok is destructive to name */ |
220 | |
215 | |
221 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
216 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
222 | { |
217 | { |
… | |
… | |
324 | void |
319 | void |
325 | init_archetypes (void) |
320 | init_archetypes (void) |
326 | { /* called from add_player() and edit() */ |
321 | { /* called from add_player() and edit() */ |
327 | if (first_archetype != NULL) /* Only do this once */ |
322 | if (first_archetype != NULL) /* Only do this once */ |
328 | return; |
323 | return; |
|
|
324 | |
329 | arch_init = 1; |
325 | arch_init = 1; |
330 | load_archetypes (); |
326 | load_archetypes (); |
331 | arch_init = 0; |
327 | arch_init = 0; |
332 | empty_archetype = find_archetype ("empty_archetype"); |
328 | empty_archetype = archetype::find ("empty_archetype"); |
333 | |
329 | |
334 | /* init_blocksview();*/ |
330 | /* init_blocksview();*/ |
335 | } |
331 | } |
336 | |
332 | |
337 | /* |
333 | /* |
… | |
… | |
361 | */ |
357 | */ |
362 | |
358 | |
363 | void |
359 | void |
364 | init_archetable (void) |
360 | init_archetable (void) |
365 | { |
361 | { |
366 | archetype * |
362 | archetype *at; |
367 | at; |
|
|
368 | |
363 | |
369 | LOG (llevDebug, " Setting up archetable...\n"); |
364 | LOG (llevDebug, " Setting up archetable...\n"); |
370 | |
365 | |
371 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
366 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
372 | add_arch (at); |
367 | add_arch (at); |
… | |
… | |
391 | */ |
386 | */ |
392 | |
387 | |
393 | void |
388 | void |
394 | dump_all_archetypes (void) |
389 | dump_all_archetypes (void) |
395 | { |
390 | { |
396 | archetype * |
391 | archetype *at; |
397 | at; |
|
|
398 | |
392 | |
399 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
393 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
400 | { |
394 | { |
401 | dump_arch (at); |
395 | dump_arch (at); |
402 | fprintf (logfile, "%s\n", errmsg); |
396 | fprintf (logfile, "%s\n", errmsg); |
… | |
… | |
404 | } |
398 | } |
405 | |
399 | |
406 | void |
400 | void |
407 | free_all_archs (void) |
401 | free_all_archs (void) |
408 | { |
402 | { |
409 | archetype * |
403 | archetype *at, *next; |
410 | at, * |
|
|
411 | next; |
|
|
412 | int |
|
|
413 | i = 0, f = 0; |
404 | int i = 0, f = 0; |
414 | |
405 | |
415 | for (at = first_archetype; at != NULL; at = next) |
406 | for (at = first_archetype; at != NULL; at = next) |
416 | { |
407 | { |
417 | if (at->more) |
408 | if (at->more) |
418 | next = at->more; |
409 | next = at->more; |
… | |
… | |
443 | * of archetype-structures. |
434 | * of archetype-structures. |
444 | */ |
435 | */ |
445 | void |
436 | void |
446 | first_arch_pass (object_thawer & fp) |
437 | first_arch_pass (object_thawer & fp) |
447 | { |
438 | { |
448 | archetype * |
|
|
449 | at, * |
|
|
450 | head = NULL, *last_more = NULL; |
439 | archetype *at, *head = NULL, *last_more = NULL; |
451 | |
440 | |
452 | at->clone.arch = first_archetype = at = new archetype; |
441 | at->clone.arch = first_archetype = at = new archetype; |
453 | |
442 | |
454 | while (int i = load_object (fp, &at->clone, 0)) |
443 | while (int i = load_object (fp, &at->clone, 0)) |
455 | { |
444 | { |
… | |
… | |
524 | */ |
513 | */ |
525 | |
514 | |
526 | void |
515 | void |
527 | second_arch_pass (object_thawer & thawer) |
516 | second_arch_pass (object_thawer & thawer) |
528 | { |
517 | { |
529 | char |
|
|
530 | buf[MAX_BUF], * |
|
|
531 | variable = buf, *argument, *cp; |
518 | char buf[MAX_BUF], *variable = buf, *argument, *cp; |
532 | archetype * |
|
|
533 | at = NULL, *other; |
519 | archetype *at = NULL, *other; |
534 | |
520 | |
535 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
521 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
536 | { |
522 | { |
537 | if (*buf == '#') |
523 | if (*buf == '#') |
538 | continue; |
524 | continue; |
… | |
… | |
546 | cp--; |
532 | cp--; |
547 | } |
533 | } |
548 | } |
534 | } |
549 | if (!strcmp ("Object", variable)) |
535 | if (!strcmp ("Object", variable)) |
550 | { |
536 | { |
551 | if ((at = find_archetype (argument)) == NULL) |
537 | if ((at = archetype::find (argument)) == NULL) |
552 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
538 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
553 | } |
539 | } |
554 | else if (!strcmp ("other_arch", variable)) |
540 | else if (!strcmp ("other_arch", variable)) |
555 | { |
541 | { |
556 | if (at != NULL && at->clone.other_arch == NULL) |
542 | if (at != NULL && at->clone.other_arch == NULL) |
557 | { |
543 | { |
558 | if ((other = find_archetype (argument)) == NULL) |
544 | if ((other = archetype::find (argument)) == NULL) |
559 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
545 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
560 | else if (at != NULL) |
546 | else if (at != NULL) |
561 | at->clone.other_arch = other; |
547 | at->clone.other_arch = other; |
562 | } |
548 | } |
563 | } |
549 | } |
… | |
… | |
579 | |
565 | |
580 | #ifdef DEBUG |
566 | #ifdef DEBUG |
581 | void |
567 | void |
582 | check_generators (void) |
568 | check_generators (void) |
583 | { |
569 | { |
584 | archetype * |
570 | archetype *at; |
585 | at; |
|
|
586 | |
571 | |
587 | for (at = first_archetype; at != NULL; at = at->next) |
572 | for (at = first_archetype; at != NULL; at = at->next) |
588 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
573 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
589 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
574 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
590 | } |
575 | } |
… | |
… | |
598 | */ |
583 | */ |
599 | |
584 | |
600 | void |
585 | void |
601 | load_archetypes (void) |
586 | load_archetypes (void) |
602 | { |
587 | { |
603 | char |
|
|
604 | filename[MAX_BUF]; |
588 | char filename[MAX_BUF]; |
605 | |
589 | |
606 | #if TIME_ARCH_LOAD |
590 | #if TIME_ARCH_LOAD |
607 | struct timeval |
591 | struct timeval |
608 | tv1, |
592 | tv1, |
609 | tv2; |
593 | tv2; |
… | |
… | |
647 | */ |
631 | */ |
648 | |
632 | |
649 | object * |
633 | object * |
650 | arch_to_object (archetype *at) |
634 | arch_to_object (archetype *at) |
651 | { |
635 | { |
652 | object * |
636 | object *op; |
653 | op; |
|
|
654 | |
637 | |
655 | if (at == NULL) |
638 | if (at == NULL) |
656 | { |
639 | { |
657 | if (warn_archetypes) |
640 | if (warn_archetypes) |
658 | LOG (llevError, "Couldn't find archetype.\n"); |
641 | LOG (llevError, "Couldn't find archetype.\n"); |
… | |
… | |
675 | */ |
658 | */ |
676 | |
659 | |
677 | object * |
660 | object * |
678 | create_singularity (const char *name) |
661 | create_singularity (const char *name) |
679 | { |
662 | { |
680 | object * |
663 | object *op; |
681 | op; |
|
|
682 | char |
|
|
683 | buf[MAX_BUF]; |
664 | char buf[MAX_BUF]; |
684 | |
665 | |
685 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
666 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
686 | op = get_object (); |
667 | op = get_object (); |
687 | op->name = op->name_pl = buf; |
668 | op->name = op->name_pl = buf; |
688 | SET_FLAG (op, FLAG_NO_PICK); |
669 | SET_FLAG (op, FLAG_NO_PICK); |
… | |
… | |
695 | */ |
676 | */ |
696 | |
677 | |
697 | object * |
678 | object * |
698 | get_archetype (const char *name) |
679 | get_archetype (const char *name) |
699 | { |
680 | { |
700 | archetype * |
681 | archetype *at = archetype::find (name); |
701 | at; |
|
|
702 | |
682 | |
703 | at = find_archetype (name); |
683 | if (!at) |
704 | if (at == NULL) |
|
|
705 | return create_singularity (name); |
684 | return create_singularity (name); |
706 | |
685 | |
707 | return arch_to_object (at); |
686 | return arch_to_object (at); |
708 | } |
687 | } |
709 | |
688 | |
… | |
… | |
712 | */ |
691 | */ |
713 | |
692 | |
714 | unsigned long |
693 | unsigned long |
715 | hasharch (const char *str, int tablesize) |
694 | hasharch (const char *str, int tablesize) |
716 | { |
695 | { |
717 | unsigned long |
696 | unsigned long hash = 0; |
718 | hash = 0; |
|
|
719 | unsigned int |
697 | unsigned int i = 0; |
720 | i = 0; |
|
|
721 | const char * |
698 | const char *p; |
722 | p; |
|
|
723 | |
699 | |
724 | /* use the one-at-a-time hash function, which supposedly is |
700 | /* use the one-at-a-time hash function, which supposedly is |
725 | * better than the djb2-like one used by perl5.005, but |
701 | * better than the djb2-like one used by perl5.005, but |
726 | * certainly is better then the bug used here before. |
702 | * certainly is better then the bug used here before. |
727 | * see http://burtleburtle.net/bob/hash/doobs.html |
703 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
744 | * Finds, using the hashtable, which archetype matches the given name. |
720 | * Finds, using the hashtable, which archetype matches the given name. |
745 | * returns a pointer to the found archetype, otherwise NULL. |
721 | * returns a pointer to the found archetype, otherwise NULL. |
746 | */ |
722 | */ |
747 | |
723 | |
748 | archetype * |
724 | archetype * |
749 | find_archetype (const char *name) |
725 | archetype::find (const char *name) |
750 | { |
726 | { |
751 | #if USE_UNORDERED_MAP |
|
|
752 | name = shstr::find (name); |
|
|
753 | |
|
|
754 | if (!name) |
727 | if (!name) |
755 | return 0; |
728 | return 0; |
756 | |
729 | |
757 | HT::const_iterator i = ht.find ((size_t) name); |
730 | #if USE_UNORDERED_MAP |
|
|
731 | AUTODECL (i, ht.find ((size_t) name)); |
758 | |
732 | |
759 | if (i == ht.end ()) |
733 | if (i == ht.end ()) |
760 | return 0; |
734 | return 0; |
761 | else |
735 | else |
762 | return i->second; |
736 | return i->second; |
763 | #endif |
737 | #endif |
764 | |
738 | |
765 | archetype * |
739 | archetype *at; |
766 | at; |
|
|
767 | unsigned long |
740 | unsigned long index; |
768 | index; |
|
|
769 | |
|
|
770 | if (name == NULL) |
|
|
771 | return (archetype *) NULL; |
|
|
772 | |
741 | |
773 | index = hasharch (name, ARCHTABLE); |
742 | index = hasharch (name, ARCHTABLE); |
774 | arch_search++; |
743 | arch_search++; |
775 | for (;;) |
744 | for (;;) |
776 | { |
745 | { |
777 | at = arch_table[index]; |
746 | at = arch_table[index]; |
|
|
747 | |
778 | if (at == NULL) |
748 | if (at == NULL) |
779 | { |
749 | { |
780 | if (warn_archetypes) |
750 | if (warn_archetypes) |
781 | LOG (llevError, "Couldn't find archetype %s\n", name); |
751 | LOG (llevError, "Couldn't find archetype %s\n", name); |
|
|
752 | |
782 | return NULL; |
753 | return NULL; |
783 | } |
754 | } |
|
|
755 | |
784 | arch_cmp++; |
756 | arch_cmp++; |
|
|
757 | |
785 | if (!strcmp ((const char *) at->name, name)) |
758 | if (!strcmp ((const char *) at->name, name)) |
786 | return at; |
759 | return at; |
|
|
760 | |
787 | if (++index >= ARCHTABLE) |
761 | if (++index >= ARCHTABLE) |
788 | index = 0; |
762 | index = 0; |
789 | } |
763 | } |
790 | } |
764 | } |
791 | |
765 | |
… | |
… | |
798 | { |
772 | { |
799 | #if USE_UNORDERED_MAP |
773 | #if USE_UNORDERED_MAP |
800 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
774 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
801 | #endif |
775 | #endif |
802 | |
776 | |
803 | int |
|
|
804 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
777 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
805 | |
778 | |
806 | for (;;) |
779 | for (;;) |
807 | { |
780 | { |
808 | if (arch_table[index] == NULL) |
781 | if (arch_table[index] == NULL) |
809 | { |
782 | { |
… | |
… | |
825 | */ |
798 | */ |
826 | |
799 | |
827 | archetype * |
800 | archetype * |
828 | type_to_archetype (int type) |
801 | type_to_archetype (int type) |
829 | { |
802 | { |
830 | archetype * |
803 | archetype *at; |
831 | at; |
|
|
832 | |
804 | |
833 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
805 | for (at = first_archetype; at; at = at->more == 0 ? at->next : at->more) |
834 | if (at->clone.type == type) |
806 | if (at->clone.type == type) |
835 | return at; |
807 | return at; |
|
|
808 | |
836 | return NULL; |
809 | return 0; |
837 | } |
810 | } |
838 | |
811 | |
839 | /* |
812 | /* |
840 | * Returns a new object copied from the first archetype matching |
813 | * Returns a new object copied from the first archetype matching |
841 | * the given type. |
814 | * the given type. |
… | |
… | |
843 | */ |
816 | */ |
844 | |
817 | |
845 | object * |
818 | object * |
846 | clone_arch (int type) |
819 | clone_arch (int type) |
847 | { |
820 | { |
848 | archetype * |
821 | archetype *at; |
849 | at; |
|
|
850 | object * |
|
|
851 | op = get_object (); |
822 | object *op = get_object (); |
852 | |
823 | |
853 | if ((at = type_to_archetype (type)) == NULL) |
824 | if ((at = type_to_archetype (type)) == NULL) |
854 | { |
825 | { |
855 | LOG (llevError, "Can't clone archetype %d\n", type); |
826 | LOG (llevError, "Can't clone archetype %d\n", type); |
856 | free_object (op); |
827 | free_object (op); |
857 | return NULL; |
828 | return NULL; |
858 | } |
829 | } |
|
|
830 | |
859 | copy_object (&at->clone, op); |
831 | copy_object (&at->clone, op); |
860 | op->instantiate (); |
832 | op->instantiate (); |
861 | return op; |
833 | return op; |
862 | } |
834 | } |
863 | |
835 | |
… | |
… | |
866 | */ |
838 | */ |
867 | |
839 | |
868 | object * |
840 | object * |
869 | object_create_arch (archetype *at) |
841 | object_create_arch (archetype *at) |
870 | { |
842 | { |
871 | object * |
843 | object *op, *prev = 0, *head = 0; |
872 | op, * |
|
|
873 | prev = NULL, *head = NULL; |
|
|
874 | |
844 | |
875 | while (at) |
845 | while (at) |
876 | { |
846 | { |
877 | op = arch_to_object (at); |
847 | op = arch_to_object (at); |
878 | op->x = at->clone.x; |
848 | op->x = at->clone.x; |
879 | op->y = at->clone.y; |
849 | op->y = at->clone.y; |
|
|
850 | |
880 | if (head) |
851 | if (head) |
881 | op->head = head, prev->more = op; |
852 | op->head = head, prev->more = op; |
|
|
853 | |
882 | if (!head) |
854 | if (!head) |
883 | head = op; |
855 | head = op; |
|
|
856 | |
884 | prev = op; |
857 | prev = op; |
885 | at = at->more; |
858 | at = at->more; |
886 | } |
859 | } |
|
|
860 | |
887 | return (head); |
861 | return (head); |
888 | } |
862 | } |
889 | |
863 | |
890 | /*** end of arch.c ***/ |
|
|