ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
(Generate patch)

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.23 by root, Thu Sep 14 23:13:48 2006 UTC vs.
Revision 1.35 by root, Wed Jan 3 20:32:13 2007 UTC

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
36static void add_arch (archetype *at); 32#if USE_UNORDERED_MAP
33# include <tr1/functional>
34# include <tr1/unordered_map>
35#endif
37 36
38static archetype *arch_table[ARCHTABLE];
39int arch_cmp = 0; /* How many strcmp's */ 37int arch_cmp = 0; /* How many strcmp's */
40int arch_search = 0; /* How many searches */ 38int arch_search = 0; /* How many searches */
41int arch_init; /* True if doing arch initialization */ 39int 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
53typedef std::tr1::unordered_map 51typedef 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
63static HT ht; 61static HT ht;
62#else
63static 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 */
120object * 120object *
121get_archetype_by_skill_name (const char *skill, int type) 121get_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}
358 archetype *at; 354 archetype *at;
359 355
360 LOG (llevDebug, " Setting up archetable...\n"); 356 LOG (llevDebug, " Setting up archetable...\n");
361 357
362 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)
363 add_arch (at); 359 at->hash_add ();
364 360
365 LOG (llevDebug, "done\n"); 361 LOG (llevDebug, "done\n");
366}
367
368/*
369 * Dumps an archetype to debug-level output.
370 */
371
372void
373dump_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
384void
385dump_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} 362}
395 363
396void 364void
397free_all_archs (void) 365free_all_archs (void)
398{ 366{
409 delete 377 delete
410 at; 378 at;
411 379
412 i++; 380 i++;
413 } 381 }
382
414 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 383 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
415} 384}
416 385
417archetype::archetype () 386archetype::archetype ()
418{ 387{
419 clear_object (&clone); /* to initial state other also */
420 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 */
421 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 389 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
422} 390}
423 391
424archetype::~archetype () 392archetype::~archetype ()
425{ 393{
430 * of archetype-structures. 398 * of archetype-structures.
431 */ 399 */
432void 400void
433first_arch_pass (object_thawer & fp) 401first_arch_pass (object_thawer & fp)
434{ 402{
435 archetype *at, *head = NULL, *last_more = NULL; 403 archetype *head = 0, *last_more = 0;
436 404
405 archetype *at = new archetype;
437 at->clone.arch = first_archetype = at = new archetype; 406 at->clone.arch = first_archetype = at;
438 407
439 while (int i = load_object (fp, &at->clone, 0)) 408 while (int i = load_object (fp, &at->clone, 0))
440 { 409 {
441 at->clone.speed_left = (float) (-0.1); 410 at->clone.speed_left = (float) (-0.1);
442 /* 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
545 } 514 }
546 else if (!strcmp ("randomitems", variable)) 515 else if (!strcmp ("randomitems", variable))
547 { 516 {
548 if (at != NULL) 517 if (at != NULL)
549 { 518 {
550 treasurelist *
551 tl = find_treasurelist (argument); 519 treasurelist *tl = find_treasurelist (argument);
552 520
553 if (tl == NULL) 521 if (tl == NULL)
554 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);
555 else 523 else
556 at->clone.randomitems = tl; 524 at->clone.randomitems = tl;
581void 549void
582load_archetypes (void) 550load_archetypes (void)
583{ 551{
584 char filename[MAX_BUF]; 552 char filename[MAX_BUF];
585 553
586#if TIME_ARCH_LOAD
587 struct timeval
588 tv1,
589 tv2;
590#endif
591
592 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 554 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
593 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 555 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
594 556
595 { 557 {
596 object_thawer 558 object_thawer
609 object_thawer 571 object_thawer
610 thawer (filename); 572 thawer (filename);
611 573
612 LOG (llevDebug, " loading treasure...\n"); 574 LOG (llevDebug, " loading treasure...\n");
613 load_treasures (); 575 load_treasures ();
576 LOG (llevDebug, " done\n");
614 LOG (llevDebug, " done\n arch-pass 2...\n"); 577 LOG (llevDebug, " arch-pass 2...\n");
615 second_arch_pass (thawer); 578 second_arch_pass (thawer);
616 LOG (llevDebug, " done\n"); 579 LOG (llevDebug, " done\n");
617#ifdef DEBUG 580#ifdef DEBUG
618 check_generators (); 581 check_generators ();
619#endif 582#endif
623 586
624/* 587/*
625 * 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.
626 * This function returns NULL on failure. 589 * This function returns NULL on failure.
627 */ 590 */
628
629object * 591object *
630arch_to_object (archetype *at) 592arch_to_object (archetype *at)
631{ 593{
632 object *op; 594 object *op;
633 595
637 LOG (llevError, "Couldn't find archetype.\n"); 599 LOG (llevError, "Couldn't find archetype.\n");
638 600
639 return NULL; 601 return NULL;
640 } 602 }
641 603
642 op = get_object (); 604 op = at->clone.clone ();
643 copy_object (&at->clone, op);
644 op->arch = at; 605 op->arch = at;
645 op->instantiate (); 606 op->instantiate ();
646 return op; 607 return op;
647} 608}
648 609
650 * Creates an object. This function is called by get_archetype() 611 * Creates an object. This function is called by get_archetype()
651 * if it fails to find the appropriate archetype. 612 * if it fails to find the appropriate archetype.
652 * Thus get_archetype() will be guaranteed to always return 613 * Thus get_archetype() will be guaranteed to always return
653 * an object, and never NULL. 614 * an object, and never NULL.
654 */ 615 */
655
656object * 616object *
657create_singularity (const char *name) 617create_singularity (const char *name)
658{ 618{
659 object *op; 619 object *op;
660 char buf[MAX_BUF]; 620 char buf[MAX_BUF];
661 621
662 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 622 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
663 op = get_object (); 623 op = object::create ();
664 op->name = op->name_pl = buf; 624 op->name = op->name_pl = buf;
665 SET_FLAG (op, FLAG_NO_PICK); 625 SET_FLAG (op, FLAG_NO_PICK);
666 return op; 626 return op;
667} 627}
668 628
669/* 629/*
670 * Finds which archetype matches the given name, and returns a new 630 * Finds which archetype matches the given name, and returns a new
671 * object containing a copy of the archetype. 631 * object containing a copy of the archetype.
672 */ 632 */
673
674object * 633object *
675get_archetype (const char *name) 634get_archetype (const char *name)
676{ 635{
677 archetype *at = archetype::find (name); 636 archetype *at = archetype::find (name);
678 637
760} 719}
761 720
762/* 721/*
763 * Adds an archetype to the hashtable. 722 * Adds an archetype to the hashtable.
764 */ 723 */
765 724void
766static void 725archetype::hash_add ()
767add_arch (archetype *at)
768{ 726{
769#if USE_UNORDERED_MAP 727#if USE_UNORDERED_MAP
770 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 728 ht.insert (std::make_pair ((size_t) (const char *) name, this));
771#endif 729#else
772 730
773 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 731 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
774 732
775 for (;;) 733 for (;;)
776 { 734 {
777 if (arch_table[index] == NULL) 735 if (!arch_table[index])
778 { 736 {
779 arch_table[index] = at; 737 arch_table[index] = this;
780 return; 738 break;
781 } 739 }
782 740
783 if (++index == ARCHTABLE) 741 if (++index == ARCHTABLE)
784 index = 0; 742 index = 0;
785 743
786 if (index == org_index) 744 if (index == org_index)
787 fatal (ARCHTABLE_TOO_SMALL); 745 fatal (ARCHTABLE_TOO_SMALL);
788 } 746 }
747#endif
748}
749
750void
751archetype::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
789} 774}
790 775
791/* 776/*
792 * Returns the first archetype using the given type. 777 * Returns the first archetype using the given type.
793 * Used in treasure-generation. 778 * Used in treasure-generation.
813 798
814object * 799object *
815clone_arch (int type) 800clone_arch (int type)
816{ 801{
817 archetype *at; 802 archetype *at;
818 object *op = get_object ();
819 803
820 if ((at = type_to_archetype (type)) == NULL) 804 if ((at = type_to_archetype (type)) == NULL)
821 { 805 {
822 LOG (llevError, "Can't clone archetype %d\n", type); 806 LOG (llevError, "Can't clone archetype %d\n", type);
823 free_object (op);
824 return NULL; 807 return 0;
825 } 808 }
826 809
827 copy_object (&at->clone, op); 810 object *op = at->clone.clone ();
828 op->instantiate (); 811 op->instantiate ();
829 return op; 812 return op;
830} 813}
831 814
832/* 815/*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines