1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_arch_c = |
|
|
4 | * "$Id: arch.C,v 1.16 2006/09/10 16:00:23 root Exp $"; |
|
|
5 | */ |
|
|
6 | |
|
|
7 | /* |
1 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
9 | |
3 | |
10 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
4 | Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
11 | Copyright (C) 1992 Frank Tore Johansen |
5 | Copyright (C) 1992 Frank Tore Johansen |
… | |
… | |
22 | |
16 | |
23 | 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 |
24 | along with this program; if not, write to the Free Software |
18 | along with this program; if not, write to the Free Software |
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
26 | |
20 | |
27 | 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> |
28 | */ |
22 | */ |
29 | |
23 | |
30 | #include <cassert> |
24 | #include <cassert> |
31 | |
25 | |
32 | #include <tr1/unordered_map> |
26 | #include <tr1/unordered_map> |
… | |
… | |
54 | * MSW 2003-04-29 |
48 | * MSW 2003-04-29 |
55 | */ |
49 | */ |
56 | |
50 | |
57 | #if USE_UNORDERED_MAP |
51 | #if USE_UNORDERED_MAP |
58 | // the hashtable |
52 | // the hashtable |
59 | typedef |
|
|
60 | std::tr1::unordered_map < |
53 | typedef std::tr1::unordered_map |
61 | 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, |
62 | HT; |
61 | > HT; |
63 | |
62 | |
64 | static |
63 | static HT ht; |
65 | HT |
|
|
66 | ht; |
|
|
67 | #endif |
64 | #endif |
68 | |
65 | |
69 | /** |
66 | /** |
70 | * GROS - This function retrieves an archetype given the name that appears |
67 | * GROS - This function retrieves an archetype given the name that appears |
71 | * during the game (for example, "writing pen" instead of "stylus"). |
68 | * during the game (for example, "writing pen" instead of "stylus"). |
… | |
… | |
78 | * - the archetype found or null if nothing was found. |
75 | * - the archetype found or null if nothing was found. |
79 | */ |
76 | */ |
80 | archetype * |
77 | archetype * |
81 | find_archetype_by_object_name (const char *name) |
78 | find_archetype_by_object_name (const char *name) |
82 | { |
79 | { |
83 | archetype * |
80 | archetype *at; |
84 | at; |
|
|
85 | |
81 | |
86 | if (name == NULL) |
82 | if (name == NULL) |
87 | return (archetype *) NULL; |
83 | return (archetype *) NULL; |
88 | |
84 | |
89 | for (at = first_archetype; at != NULL; at = at->next) |
85 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
100 | * except that it considers only items of the given type. |
96 | * except that it considers only items of the given type. |
101 | */ |
97 | */ |
102 | archetype * |
98 | archetype * |
103 | find_archetype_by_object_type_name (int type, const char *name) |
99 | find_archetype_by_object_type_name (int type, const char *name) |
104 | { |
100 | { |
105 | archetype * |
101 | archetype *at; |
106 | at; |
|
|
107 | |
102 | |
108 | if (name == NULL) |
103 | if (name == NULL) |
109 | return NULL; |
104 | return NULL; |
110 | |
105 | |
111 | for (at = first_archetype; at != NULL; at = at->next) |
106 | for (at = first_archetype; at != NULL; at = at->next) |
… | |
… | |
173 | * but it otherwise had a big memory leak. |
168 | * but it otherwise had a big memory leak. |
174 | */ |
169 | */ |
175 | object * |
170 | object * |
176 | get_archetype_by_object_name (const char *name) |
171 | get_archetype_by_object_name (const char *name) |
177 | { |
172 | { |
178 | archetype * |
173 | archetype *at; |
179 | at; |
|
|
180 | char |
|
|
181 | tmpname[MAX_BUF]; |
174 | char tmpname[MAX_BUF]; |
182 | int |
175 | int i; |
183 | i; |
|
|
184 | |
176 | |
185 | strncpy (tmpname, name, MAX_BUF - 1); |
177 | assign (tmpname, name); |
186 | tmpname[MAX_BUF - 1] = 0; |
178 | |
187 | for (i = strlen (tmpname); i > 0; i--) |
179 | for (i = strlen (tmpname); i > 0; i--) |
188 | { |
180 | { |
189 | tmpname[i] = 0; |
181 | tmpname[i] = 0; |
190 | at = find_archetype_by_object_name (tmpname); |
182 | at = find_archetype_by_object_name (tmpname); |
|
|
183 | |
191 | if (at != NULL) |
184 | if (at != NULL) |
192 | { |
|
|
193 | return arch_to_object (at); |
185 | return arch_to_object (at); |
194 | } |
|
|
195 | } |
186 | } |
|
|
187 | |
196 | return create_singularity (name); |
188 | return create_singularity (name); |
197 | } |
189 | } |
198 | |
190 | |
199 | /* This is a subset of the parse_id command. Basically, name can be |
191 | /* This is a subset of the parse_id command. Basically, name can be |
200 | * a string seperated lists of things to match, with certain keywords. |
192 | * a string seperated lists of things to match, with certain keywords. |
… | |
… | |
214 | * Last, make a check on the full name. |
206 | * Last, make a check on the full name. |
215 | */ |
207 | */ |
216 | int |
208 | int |
217 | item_matched_string (object *pl, object *op, const char *name) |
209 | item_matched_string (object *pl, object *op, const char *name) |
218 | { |
210 | { |
219 | char * |
|
|
220 | cp, |
|
|
221 | local_name[MAX_BUF]; |
211 | char *cp, local_name[MAX_BUF]; |
222 | int |
212 | int count, retval = 0; |
223 | count, |
|
|
224 | retval = 0; |
|
|
225 | |
213 | |
226 | strcpy (local_name, name); /* strtok is destructive to name */ |
214 | strcpy (local_name, name); /* strtok is destructive to name */ |
227 | |
215 | |
228 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
216 | for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) |
229 | { |
217 | { |
… | |
… | |
331 | void |
319 | void |
332 | init_archetypes (void) |
320 | init_archetypes (void) |
333 | { /* called from add_player() and edit() */ |
321 | { /* called from add_player() and edit() */ |
334 | if (first_archetype != NULL) /* Only do this once */ |
322 | if (first_archetype != NULL) /* Only do this once */ |
335 | return; |
323 | return; |
|
|
324 | |
336 | arch_init = 1; |
325 | arch_init = 1; |
337 | load_archetypes (); |
326 | load_archetypes (); |
338 | arch_init = 0; |
327 | arch_init = 0; |
339 | empty_archetype = find_archetype ("empty_archetype"); |
328 | empty_archetype = archetype::find ("empty_archetype"); |
340 | |
329 | |
341 | /* init_blocksview();*/ |
330 | /* init_blocksview();*/ |
342 | } |
331 | } |
343 | |
332 | |
344 | /* |
333 | /* |
… | |
… | |
368 | */ |
357 | */ |
369 | |
358 | |
370 | void |
359 | void |
371 | init_archetable (void) |
360 | init_archetable (void) |
372 | { |
361 | { |
373 | archetype * |
362 | archetype *at; |
374 | at; |
|
|
375 | |
363 | |
376 | LOG (llevDebug, " Setting up archetable...\n"); |
364 | LOG (llevDebug, " Setting up archetable...\n"); |
377 | |
365 | |
378 | 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) |
379 | add_arch (at); |
367 | add_arch (at); |
… | |
… | |
398 | */ |
386 | */ |
399 | |
387 | |
400 | void |
388 | void |
401 | dump_all_archetypes (void) |
389 | dump_all_archetypes (void) |
402 | { |
390 | { |
403 | archetype * |
391 | archetype *at; |
404 | at; |
|
|
405 | |
392 | |
406 | 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) |
407 | { |
394 | { |
408 | dump_arch (at); |
395 | dump_arch (at); |
409 | fprintf (logfile, "%s\n", errmsg); |
396 | fprintf (logfile, "%s\n", errmsg); |
… | |
… | |
411 | } |
398 | } |
412 | |
399 | |
413 | void |
400 | void |
414 | free_all_archs (void) |
401 | free_all_archs (void) |
415 | { |
402 | { |
416 | archetype * |
403 | archetype *at, *next; |
417 | at, * |
|
|
418 | next; |
|
|
419 | int |
|
|
420 | i = 0, f = 0; |
404 | int i = 0, f = 0; |
421 | |
405 | |
422 | for (at = first_archetype; at != NULL; at = next) |
406 | for (at = first_archetype; at != NULL; at = next) |
423 | { |
407 | { |
424 | if (at->more) |
408 | if (at->more) |
425 | next = at->more; |
409 | next = at->more; |
… | |
… | |
450 | * of archetype-structures. |
434 | * of archetype-structures. |
451 | */ |
435 | */ |
452 | void |
436 | void |
453 | first_arch_pass (object_thawer & fp) |
437 | first_arch_pass (object_thawer & fp) |
454 | { |
438 | { |
455 | archetype * |
|
|
456 | at, * |
|
|
457 | head = NULL, *last_more = NULL; |
439 | archetype *at, *head = NULL, *last_more = NULL; |
458 | |
440 | |
459 | at->clone.arch = first_archetype = at = new archetype; |
441 | at->clone.arch = first_archetype = at = new archetype; |
460 | |
442 | |
461 | while (int i = load_object (fp, &at->clone, 0)) |
443 | while (int i = load_object (fp, &at->clone, 0)) |
462 | { |
444 | { |
… | |
… | |
531 | */ |
513 | */ |
532 | |
514 | |
533 | void |
515 | void |
534 | second_arch_pass (object_thawer & thawer) |
516 | second_arch_pass (object_thawer & thawer) |
535 | { |
517 | { |
536 | char |
|
|
537 | buf[MAX_BUF], * |
|
|
538 | variable = buf, *argument, *cp; |
518 | char buf[MAX_BUF], *variable = buf, *argument, *cp; |
539 | archetype * |
|
|
540 | at = NULL, *other; |
519 | archetype *at = NULL, *other; |
541 | |
520 | |
542 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
521 | while (fgets (buf, MAX_BUF, thawer) != NULL) |
543 | { |
522 | { |
544 | if (*buf == '#') |
523 | if (*buf == '#') |
545 | continue; |
524 | continue; |
… | |
… | |
553 | cp--; |
532 | cp--; |
554 | } |
533 | } |
555 | } |
534 | } |
556 | if (!strcmp ("Object", variable)) |
535 | if (!strcmp ("Object", variable)) |
557 | { |
536 | { |
558 | if ((at = find_archetype (argument)) == NULL) |
537 | if ((at = archetype::find (argument)) == NULL) |
559 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
538 | LOG (llevError, "Warning: failed to find arch %s\n", argument); |
560 | } |
539 | } |
561 | else if (!strcmp ("other_arch", variable)) |
540 | else if (!strcmp ("other_arch", variable)) |
562 | { |
541 | { |
563 | if (at != NULL && at->clone.other_arch == NULL) |
542 | if (at != NULL && at->clone.other_arch == NULL) |
564 | { |
543 | { |
565 | if ((other = find_archetype (argument)) == NULL) |
544 | if ((other = archetype::find (argument)) == NULL) |
566 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
545 | LOG (llevError, "Warning: failed to find other_arch %s\n", argument); |
567 | else if (at != NULL) |
546 | else if (at != NULL) |
568 | at->clone.other_arch = other; |
547 | at->clone.other_arch = other; |
569 | } |
548 | } |
570 | } |
549 | } |
… | |
… | |
586 | |
565 | |
587 | #ifdef DEBUG |
566 | #ifdef DEBUG |
588 | void |
567 | void |
589 | check_generators (void) |
568 | check_generators (void) |
590 | { |
569 | { |
591 | archetype * |
570 | archetype *at; |
592 | at; |
|
|
593 | |
571 | |
594 | for (at = first_archetype; at != NULL; at = at->next) |
572 | for (at = first_archetype; at != NULL; at = at->next) |
595 | 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) |
596 | 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); |
597 | } |
575 | } |
… | |
… | |
605 | */ |
583 | */ |
606 | |
584 | |
607 | void |
585 | void |
608 | load_archetypes (void) |
586 | load_archetypes (void) |
609 | { |
587 | { |
610 | char |
|
|
611 | filename[MAX_BUF]; |
588 | char filename[MAX_BUF]; |
612 | |
589 | |
613 | #if TIME_ARCH_LOAD |
590 | #if TIME_ARCH_LOAD |
614 | struct timeval |
591 | struct timeval |
615 | tv1, |
592 | tv1, |
616 | tv2; |
593 | tv2; |
… | |
… | |
654 | */ |
631 | */ |
655 | |
632 | |
656 | object * |
633 | object * |
657 | arch_to_object (archetype *at) |
634 | arch_to_object (archetype *at) |
658 | { |
635 | { |
659 | object * |
636 | object *op; |
660 | op; |
|
|
661 | |
637 | |
662 | if (at == NULL) |
638 | if (at == NULL) |
663 | { |
639 | { |
664 | if (warn_archetypes) |
640 | if (warn_archetypes) |
665 | LOG (llevError, "Couldn't find archetype.\n"); |
641 | LOG (llevError, "Couldn't find archetype.\n"); |
… | |
… | |
682 | */ |
658 | */ |
683 | |
659 | |
684 | object * |
660 | object * |
685 | create_singularity (const char *name) |
661 | create_singularity (const char *name) |
686 | { |
662 | { |
687 | object * |
663 | object *op; |
688 | op; |
|
|
689 | char |
|
|
690 | buf[MAX_BUF]; |
664 | char buf[MAX_BUF]; |
691 | |
665 | |
692 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
666 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
693 | op = get_object (); |
667 | op = get_object (); |
694 | op->name = op->name_pl = buf; |
668 | op->name = op->name_pl = buf; |
695 | SET_FLAG (op, FLAG_NO_PICK); |
669 | SET_FLAG (op, FLAG_NO_PICK); |
… | |
… | |
702 | */ |
676 | */ |
703 | |
677 | |
704 | object * |
678 | object * |
705 | get_archetype (const char *name) |
679 | get_archetype (const char *name) |
706 | { |
680 | { |
707 | archetype * |
681 | archetype *at = archetype::find (name); |
708 | at; |
|
|
709 | |
682 | |
710 | at = find_archetype (name); |
683 | if (!at) |
711 | if (at == NULL) |
|
|
712 | return create_singularity (name); |
684 | return create_singularity (name); |
713 | |
685 | |
714 | return arch_to_object (at); |
686 | return arch_to_object (at); |
715 | } |
687 | } |
716 | |
688 | |
… | |
… | |
719 | */ |
691 | */ |
720 | |
692 | |
721 | unsigned long |
693 | unsigned long |
722 | hasharch (const char *str, int tablesize) |
694 | hasharch (const char *str, int tablesize) |
723 | { |
695 | { |
724 | unsigned long |
696 | unsigned long hash = 0; |
725 | hash = 0; |
|
|
726 | unsigned int |
697 | unsigned int i = 0; |
727 | i = 0; |
|
|
728 | const char * |
698 | const char *p; |
729 | p; |
|
|
730 | |
699 | |
731 | /* use the one-at-a-time hash function, which supposedly is |
700 | /* use the one-at-a-time hash function, which supposedly is |
732 | * better than the djb2-like one used by perl5.005, but |
701 | * better than the djb2-like one used by perl5.005, but |
733 | * certainly is better then the bug used here before. |
702 | * certainly is better then the bug used here before. |
734 | * see http://burtleburtle.net/bob/hash/doobs.html |
703 | * see http://burtleburtle.net/bob/hash/doobs.html |
… | |
… | |
751 | * Finds, using the hashtable, which archetype matches the given name. |
720 | * Finds, using the hashtable, which archetype matches the given name. |
752 | * returns a pointer to the found archetype, otherwise NULL. |
721 | * returns a pointer to the found archetype, otherwise NULL. |
753 | */ |
722 | */ |
754 | |
723 | |
755 | archetype * |
724 | archetype * |
756 | find_archetype (const char *name) |
725 | archetype::find (const char *name) |
757 | { |
726 | { |
758 | #if USE_UNORDERED_MAP |
|
|
759 | name = shstr::find (name); |
|
|
760 | |
|
|
761 | if (!name) |
727 | if (!name) |
762 | return 0; |
728 | return 0; |
763 | |
729 | |
764 | HT::const_iterator i = ht.find ((size_t) name); |
730 | #if USE_UNORDERED_MAP |
|
|
731 | AUTODECL (i, ht.find ((size_t) name)); |
765 | |
732 | |
766 | if (i == ht.end ()) |
733 | if (i == ht.end ()) |
767 | return 0; |
734 | return 0; |
768 | else |
735 | else |
769 | return i->second; |
736 | return i->second; |
770 | #endif |
737 | #endif |
771 | |
738 | |
772 | archetype * |
739 | archetype *at; |
773 | at; |
|
|
774 | unsigned long |
740 | unsigned long index; |
775 | index; |
|
|
776 | |
|
|
777 | if (name == NULL) |
|
|
778 | return (archetype *) NULL; |
|
|
779 | |
741 | |
780 | index = hasharch (name, ARCHTABLE); |
742 | index = hasharch (name, ARCHTABLE); |
781 | arch_search++; |
743 | arch_search++; |
782 | for (;;) |
744 | for (;;) |
783 | { |
745 | { |
784 | at = arch_table[index]; |
746 | at = arch_table[index]; |
|
|
747 | |
785 | if (at == NULL) |
748 | if (at == NULL) |
786 | { |
749 | { |
787 | if (warn_archetypes) |
750 | if (warn_archetypes) |
788 | LOG (llevError, "Couldn't find archetype %s\n", name); |
751 | LOG (llevError, "Couldn't find archetype %s\n", name); |
|
|
752 | |
789 | return NULL; |
753 | return NULL; |
790 | } |
754 | } |
|
|
755 | |
791 | arch_cmp++; |
756 | arch_cmp++; |
|
|
757 | |
792 | if (!strcmp ((const char *) at->name, name)) |
758 | if (!strcmp ((const char *) at->name, name)) |
793 | return at; |
759 | return at; |
|
|
760 | |
794 | if (++index >= ARCHTABLE) |
761 | if (++index >= ARCHTABLE) |
795 | index = 0; |
762 | index = 0; |
796 | } |
763 | } |
797 | } |
764 | } |
798 | |
765 | |
… | |
… | |
805 | { |
772 | { |
806 | #if USE_UNORDERED_MAP |
773 | #if USE_UNORDERED_MAP |
807 | 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)); |
808 | #endif |
775 | #endif |
809 | |
776 | |
810 | int |
|
|
811 | index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
777 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
812 | |
778 | |
813 | for (;;) |
779 | for (;;) |
814 | { |
780 | { |
815 | if (arch_table[index] == NULL) |
781 | if (arch_table[index] == NULL) |
816 | { |
782 | { |
… | |
… | |
832 | */ |
798 | */ |
833 | |
799 | |
834 | archetype * |
800 | archetype * |
835 | type_to_archetype (int type) |
801 | type_to_archetype (int type) |
836 | { |
802 | { |
837 | archetype * |
803 | archetype *at; |
838 | at; |
|
|
839 | |
804 | |
840 | 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) |
841 | if (at->clone.type == type) |
806 | if (at->clone.type == type) |
842 | return at; |
807 | return at; |
|
|
808 | |
843 | return NULL; |
809 | return 0; |
844 | } |
810 | } |
845 | |
811 | |
846 | /* |
812 | /* |
847 | * Returns a new object copied from the first archetype matching |
813 | * Returns a new object copied from the first archetype matching |
848 | * the given type. |
814 | * the given type. |
… | |
… | |
850 | */ |
816 | */ |
851 | |
817 | |
852 | object * |
818 | object * |
853 | clone_arch (int type) |
819 | clone_arch (int type) |
854 | { |
820 | { |
855 | archetype * |
821 | archetype *at; |
856 | at; |
|
|
857 | object * |
|
|
858 | op = get_object (); |
822 | object *op = get_object (); |
859 | |
823 | |
860 | if ((at = type_to_archetype (type)) == NULL) |
824 | if ((at = type_to_archetype (type)) == NULL) |
861 | { |
825 | { |
862 | LOG (llevError, "Can't clone archetype %d\n", type); |
826 | LOG (llevError, "Can't clone archetype %d\n", type); |
863 | free_object (op); |
827 | free_object (op); |
864 | return NULL; |
828 | return NULL; |
865 | } |
829 | } |
|
|
830 | |
866 | copy_object (&at->clone, op); |
831 | copy_object (&at->clone, op); |
867 | op->instantiate (); |
832 | op->instantiate (); |
868 | return op; |
833 | return op; |
869 | } |
834 | } |
870 | |
835 | |
… | |
… | |
873 | */ |
838 | */ |
874 | |
839 | |
875 | object * |
840 | object * |
876 | object_create_arch (archetype *at) |
841 | object_create_arch (archetype *at) |
877 | { |
842 | { |
878 | object * |
843 | object *op, *prev = 0, *head = 0; |
879 | op, * |
|
|
880 | prev = NULL, *head = NULL; |
|
|
881 | |
844 | |
882 | while (at) |
845 | while (at) |
883 | { |
846 | { |
884 | op = arch_to_object (at); |
847 | op = arch_to_object (at); |
885 | op->x = at->clone.x; |
848 | op->x = at->clone.x; |
886 | op->y = at->clone.y; |
849 | op->y = at->clone.y; |
|
|
850 | |
887 | if (head) |
851 | if (head) |
888 | op->head = head, prev->more = op; |
852 | op->head = head, prev->more = op; |
|
|
853 | |
889 | if (!head) |
854 | if (!head) |
890 | head = op; |
855 | head = op; |
|
|
856 | |
891 | prev = op; |
857 | prev = op; |
892 | at = at->more; |
858 | at = at->more; |
893 | } |
859 | } |
|
|
860 | |
894 | return (head); |
861 | return (head); |
895 | } |
862 | } |
896 | |
863 | |
897 | /*** end of arch.c ***/ |
|
|