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