… | |
… | |
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 | } |
… | |
… | |
269 | retval = 15; |
265 | retval = 15; |
270 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
266 | else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) |
271 | retval = 14; |
267 | retval = 14; |
272 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
268 | else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) |
273 | retval = 14; |
269 | retval = 14; |
274 | |
|
|
275 | /* Do substring checks, so things like 'Str+1' will match. |
270 | /* Do substring checks, so things like 'Str+1' will match. |
276 | * retval of these should perhaps be lower - they are lower |
271 | * retval of these should perhaps be lower - they are lower |
277 | * then the specific strcasecmp aboves, but still higher than |
272 | * then the specific strcasecmp aboves, but still higher than |
278 | * some other match criteria. |
273 | * some other match criteria. |
279 | */ |
274 | */ |
… | |
… | |
281 | retval = 12; |
276 | retval = 12; |
282 | else if (strstr (query_base_name (op, 0), cp)) |
277 | else if (strstr (query_base_name (op, 0), cp)) |
283 | retval = 12; |
278 | retval = 12; |
284 | else if (strstr (query_short_name (op), cp)) |
279 | else if (strstr (query_short_name (op), cp)) |
285 | retval = 12; |
280 | retval = 12; |
286 | |
|
|
287 | /* Check against plural/non plural based on count. */ |
281 | /* Check against plural/non plural based on count. */ |
288 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
282 | else if (count > 1 && !strcasecmp (cp, op->name_pl)) |
289 | { |
|
|
290 | retval = 6; |
283 | retval = 6; |
291 | } |
|
|
292 | else if (count == 1 && !strcasecmp (op->name, cp)) |
284 | else if (count == 1 && !strcasecmp (op->name, cp)) |
293 | { |
|
|
294 | retval = 6; |
285 | retval = 6; |
295 | } |
|
|
296 | /* base name matched - not bad */ |
286 | /* base name matched - not bad */ |
297 | else if (strcasecmp (cp, op->name) == 0 && !count) |
287 | else if (strcasecmp (cp, op->name) == 0 && !count) |
298 | retval = 4; |
288 | retval = 4; |
299 | /* Check for partial custom name, but give a real low priority */ |
289 | /* Check for partial custom name, but give a real low priority */ |
300 | else if (op->custom_name && strstr (op->custom_name, cp)) |
290 | else if (op->custom_name && strstr (op->custom_name, cp)) |
… | |
… | |
302 | |
292 | |
303 | if (retval) |
293 | if (retval) |
304 | { |
294 | { |
305 | if (pl->type == PLAYER) |
295 | if (pl->type == PLAYER) |
306 | pl->contr->count = count; |
296 | pl->contr->count = count; |
|
|
297 | |
307 | return retval; |
298 | return retval; |
308 | } |
299 | } |
309 | } |
300 | } |
|
|
301 | |
310 | return 0; |
302 | return 0; |
311 | } |
303 | } |
312 | |
304 | |
313 | /* |
305 | /* |
314 | * Initialises the internal linked list of archetypes (read from file). |
306 | * Initialises the internal linked list of archetypes (read from file). |
… | |
… | |
362 | archetype *at; |
354 | archetype *at; |
363 | |
355 | |
364 | LOG (llevDebug, " Setting up archetable...\n"); |
356 | LOG (llevDebug, " Setting up archetable...\n"); |
365 | |
357 | |
366 | 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) |
367 | add_arch (at); |
359 | at->hash_add (); |
368 | |
360 | |
369 | LOG (llevDebug, "done\n"); |
361 | LOG (llevDebug, "done\n"); |
370 | } |
|
|
371 | |
|
|
372 | /* |
|
|
373 | * Dumps an archetype to debug-level output. |
|
|
374 | */ |
|
|
375 | |
|
|
376 | void |
|
|
377 | dump_arch (archetype *at) |
|
|
378 | { |
|
|
379 | dump_object (&at->clone); |
|
|
380 | } |
|
|
381 | |
|
|
382 | /* |
|
|
383 | * Dumps _all_ archetypes to debug-level output. |
|
|
384 | * If you run crossfire with debug, and enter DM-mode, you can trigger |
|
|
385 | * this with the O key. |
|
|
386 | */ |
|
|
387 | |
|
|
388 | void |
|
|
389 | dump_all_archetypes (void) |
|
|
390 | { |
|
|
391 | archetype *at; |
|
|
392 | |
|
|
393 | for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) |
|
|
394 | { |
|
|
395 | dump_arch (at); |
|
|
396 | fprintf (logfile, "%s\n", errmsg); |
|
|
397 | } |
|
|
398 | } |
362 | } |
399 | |
363 | |
400 | void |
364 | void |
401 | free_all_archs (void) |
365 | free_all_archs (void) |
402 | { |
366 | { |
… | |
… | |
413 | delete |
377 | delete |
414 | at; |
378 | at; |
415 | |
379 | |
416 | i++; |
380 | i++; |
417 | } |
381 | } |
|
|
382 | |
418 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
383 | LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); |
419 | } |
384 | } |
420 | |
385 | |
421 | archetype::archetype () |
386 | archetype::archetype () |
422 | { |
387 | { |
423 | clear_object (&clone); /* to initial state other also */ |
|
|
424 | 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 */ |
425 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
389 | SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ |
426 | } |
390 | } |
427 | |
391 | |
428 | archetype::~archetype () |
392 | archetype::~archetype () |
429 | { |
393 | { |
… | |
… | |
434 | * of archetype-structures. |
398 | * of archetype-structures. |
435 | */ |
399 | */ |
436 | void |
400 | void |
437 | first_arch_pass (object_thawer & fp) |
401 | first_arch_pass (object_thawer & fp) |
438 | { |
402 | { |
439 | archetype *at, *head = NULL, *last_more = NULL; |
403 | archetype *head = 0, *last_more = 0; |
440 | |
404 | |
|
|
405 | archetype *at = new archetype; |
441 | at->clone.arch = first_archetype = at = new archetype; |
406 | at->clone.arch = first_archetype = at; |
442 | |
407 | |
443 | while (int i = load_object (fp, &at->clone, 0)) |
408 | while (int i = load_object (fp, &at->clone, 0)) |
444 | { |
409 | { |
445 | at->clone.speed_left = (float) (-0.1); |
410 | at->clone.speed_left = (float) (-0.1); |
446 | /* 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 |
… | |
… | |
549 | } |
514 | } |
550 | else if (!strcmp ("randomitems", variable)) |
515 | else if (!strcmp ("randomitems", variable)) |
551 | { |
516 | { |
552 | if (at != NULL) |
517 | if (at != NULL) |
553 | { |
518 | { |
554 | treasurelist * |
|
|
555 | tl = find_treasurelist (argument); |
519 | treasurelist *tl = find_treasurelist (argument); |
556 | |
520 | |
557 | if (tl == NULL) |
521 | if (tl == NULL) |
558 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
522 | LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); |
559 | else |
523 | else |
560 | at->clone.randomitems = tl; |
524 | at->clone.randomitems = tl; |
… | |
… | |
585 | void |
549 | void |
586 | load_archetypes (void) |
550 | load_archetypes (void) |
587 | { |
551 | { |
588 | char filename[MAX_BUF]; |
552 | char filename[MAX_BUF]; |
589 | |
553 | |
590 | #if TIME_ARCH_LOAD |
|
|
591 | struct timeval |
|
|
592 | tv1, |
|
|
593 | tv2; |
|
|
594 | #endif |
|
|
595 | |
|
|
596 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
554 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
597 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
555 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
598 | |
556 | |
599 | { |
557 | { |
600 | object_thawer |
558 | object_thawer |
… | |
… | |
613 | object_thawer |
571 | object_thawer |
614 | thawer (filename); |
572 | thawer (filename); |
615 | |
573 | |
616 | LOG (llevDebug, " loading treasure...\n"); |
574 | LOG (llevDebug, " loading treasure...\n"); |
617 | load_treasures (); |
575 | load_treasures (); |
|
|
576 | LOG (llevDebug, " done\n"); |
618 | LOG (llevDebug, " done\n arch-pass 2...\n"); |
577 | LOG (llevDebug, " arch-pass 2...\n"); |
619 | second_arch_pass (thawer); |
578 | second_arch_pass (thawer); |
620 | LOG (llevDebug, " done\n"); |
579 | LOG (llevDebug, " done\n"); |
621 | #ifdef DEBUG |
580 | #ifdef DEBUG |
622 | check_generators (); |
581 | check_generators (); |
623 | #endif |
582 | #endif |
… | |
… | |
627 | |
586 | |
628 | /* |
587 | /* |
629 | * 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. |
630 | * This function returns NULL on failure. |
589 | * This function returns NULL on failure. |
631 | */ |
590 | */ |
632 | |
|
|
633 | object * |
591 | object * |
634 | arch_to_object (archetype *at) |
592 | arch_to_object (archetype *at) |
635 | { |
593 | { |
636 | object *op; |
594 | object *op; |
637 | |
595 | |
… | |
… | |
641 | LOG (llevError, "Couldn't find archetype.\n"); |
599 | LOG (llevError, "Couldn't find archetype.\n"); |
642 | |
600 | |
643 | return NULL; |
601 | return NULL; |
644 | } |
602 | } |
645 | |
603 | |
646 | op = get_object (); |
604 | op = at->clone.clone (); |
647 | copy_object (&at->clone, op); |
|
|
648 | op->arch = at; |
605 | op->arch = at; |
649 | op->instantiate (); |
606 | op->instantiate (); |
650 | return op; |
607 | return op; |
651 | } |
608 | } |
652 | |
609 | |
… | |
… | |
654 | * Creates an object. This function is called by get_archetype() |
611 | * Creates an object. This function is called by get_archetype() |
655 | * if it fails to find the appropriate archetype. |
612 | * if it fails to find the appropriate archetype. |
656 | * Thus get_archetype() will be guaranteed to always return |
613 | * Thus get_archetype() will be guaranteed to always return |
657 | * an object, and never NULL. |
614 | * an object, and never NULL. |
658 | */ |
615 | */ |
659 | |
|
|
660 | object * |
616 | object * |
661 | create_singularity (const char *name) |
617 | create_singularity (const char *name) |
662 | { |
618 | { |
663 | object *op; |
619 | object *op; |
664 | char buf[MAX_BUF]; |
620 | char buf[MAX_BUF]; |
665 | |
621 | |
666 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
622 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
667 | op = get_object (); |
623 | op = object::create (); |
668 | op->name = op->name_pl = buf; |
624 | op->name = op->name_pl = buf; |
669 | SET_FLAG (op, FLAG_NO_PICK); |
625 | SET_FLAG (op, FLAG_NO_PICK); |
670 | return op; |
626 | return op; |
671 | } |
627 | } |
672 | |
628 | |
673 | /* |
629 | /* |
674 | * Finds which archetype matches the given name, and returns a new |
630 | * Finds which archetype matches the given name, and returns a new |
675 | * object containing a copy of the archetype. |
631 | * object containing a copy of the archetype. |
676 | */ |
632 | */ |
677 | |
|
|
678 | object * |
633 | object * |
679 | get_archetype (const char *name) |
634 | get_archetype (const char *name) |
680 | { |
635 | { |
681 | archetype *at = archetype::find (name); |
636 | archetype *at = archetype::find (name); |
682 | |
637 | |
… | |
… | |
764 | } |
719 | } |
765 | |
720 | |
766 | /* |
721 | /* |
767 | * Adds an archetype to the hashtable. |
722 | * Adds an archetype to the hashtable. |
768 | */ |
723 | */ |
769 | |
724 | void |
770 | static void |
725 | archetype::hash_add () |
771 | add_arch (archetype *at) |
|
|
772 | { |
726 | { |
773 | #if USE_UNORDERED_MAP |
727 | #if USE_UNORDERED_MAP |
774 | ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); |
728 | ht.insert (std::make_pair ((size_t) (const char *) name, this)); |
775 | #endif |
729 | #else |
776 | |
730 | |
777 | int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; |
731 | int index = hasharch ((const char *) name, ARCHTABLE), org_index = index; |
778 | |
732 | |
779 | for (;;) |
733 | for (;;) |
780 | { |
734 | { |
781 | if (arch_table[index] == NULL) |
735 | if (!arch_table[index]) |
782 | { |
736 | { |
783 | arch_table[index] = at; |
737 | arch_table[index] = this; |
784 | return; |
738 | break; |
785 | } |
739 | } |
786 | |
740 | |
787 | if (++index == ARCHTABLE) |
741 | if (++index == ARCHTABLE) |
788 | index = 0; |
742 | index = 0; |
789 | |
743 | |
790 | if (index == org_index) |
744 | if (index == org_index) |
791 | fatal (ARCHTABLE_TOO_SMALL); |
745 | fatal (ARCHTABLE_TOO_SMALL); |
792 | } |
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 |
793 | } |
774 | } |
794 | |
775 | |
795 | /* |
776 | /* |
796 | * Returns the first archetype using the given type. |
777 | * Returns the first archetype using the given type. |
797 | * Used in treasure-generation. |
778 | * Used in treasure-generation. |
… | |
… | |
817 | |
798 | |
818 | object * |
799 | object * |
819 | clone_arch (int type) |
800 | clone_arch (int type) |
820 | { |
801 | { |
821 | archetype *at; |
802 | archetype *at; |
822 | object *op = get_object (); |
|
|
823 | |
803 | |
824 | if ((at = type_to_archetype (type)) == NULL) |
804 | if ((at = type_to_archetype (type)) == NULL) |
825 | { |
805 | { |
826 | LOG (llevError, "Can't clone archetype %d\n", type); |
806 | LOG (llevError, "Can't clone archetype %d\n", type); |
827 | free_object (op); |
|
|
828 | return NULL; |
807 | return 0; |
829 | } |
808 | } |
830 | |
809 | |
831 | copy_object (&at->clone, op); |
810 | object *op = at->clone.clone (); |
832 | op->instantiate (); |
811 | op->instantiate (); |
833 | return op; |
812 | return op; |
834 | } |
813 | } |
835 | |
814 | |
836 | /* |
815 | /* |