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.24 by root, Sat Sep 16 22:24:12 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
580void 549void
581load_archetypes (void) 550load_archetypes (void)
582{ 551{
583 char filename[MAX_BUF]; 552 char filename[MAX_BUF];
584 553
585#if TIME_ARCH_LOAD
586 struct timeval
587 tv1,
588 tv2;
589#endif
590
591 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 554 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
592 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 555 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
593 556
594 { 557 {
595 object_thawer 558 object_thawer
608 object_thawer 571 object_thawer
609 thawer (filename); 572 thawer (filename);
610 573
611 LOG (llevDebug, " loading treasure...\n"); 574 LOG (llevDebug, " loading treasure...\n");
612 load_treasures (); 575 load_treasures ();
576 LOG (llevDebug, " done\n");
613 LOG (llevDebug, " done\n arch-pass 2...\n"); 577 LOG (llevDebug, " arch-pass 2...\n");
614 second_arch_pass (thawer); 578 second_arch_pass (thawer);
615 LOG (llevDebug, " done\n"); 579 LOG (llevDebug, " done\n");
616#ifdef DEBUG 580#ifdef DEBUG
617 check_generators (); 581 check_generators ();
618#endif 582#endif
622 586
623/* 587/*
624 * 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.
625 * This function returns NULL on failure. 589 * This function returns NULL on failure.
626 */ 590 */
627
628object * 591object *
629arch_to_object (archetype *at) 592arch_to_object (archetype *at)
630{ 593{
631 object *op; 594 object *op;
632 595
636 LOG (llevError, "Couldn't find archetype.\n"); 599 LOG (llevError, "Couldn't find archetype.\n");
637 600
638 return NULL; 601 return NULL;
639 } 602 }
640 603
641 op = get_object (); 604 op = at->clone.clone ();
642 copy_object (&at->clone, op);
643 op->arch = at; 605 op->arch = at;
644 op->instantiate (); 606 op->instantiate ();
645 return op; 607 return op;
646} 608}
647 609
649 * Creates an object. This function is called by get_archetype() 611 * Creates an object. This function is called by get_archetype()
650 * if it fails to find the appropriate archetype. 612 * if it fails to find the appropriate archetype.
651 * Thus get_archetype() will be guaranteed to always return 613 * Thus get_archetype() will be guaranteed to always return
652 * an object, and never NULL. 614 * an object, and never NULL.
653 */ 615 */
654
655object * 616object *
656create_singularity (const char *name) 617create_singularity (const char *name)
657{ 618{
658 object *op; 619 object *op;
659 char buf[MAX_BUF]; 620 char buf[MAX_BUF];
660 621
661 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 622 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
662 op = get_object (); 623 op = object::create ();
663 op->name = op->name_pl = buf; 624 op->name = op->name_pl = buf;
664 SET_FLAG (op, FLAG_NO_PICK); 625 SET_FLAG (op, FLAG_NO_PICK);
665 return op; 626 return op;
666} 627}
667 628
668/* 629/*
669 * Finds which archetype matches the given name, and returns a new 630 * Finds which archetype matches the given name, and returns a new
670 * object containing a copy of the archetype. 631 * object containing a copy of the archetype.
671 */ 632 */
672
673object * 633object *
674get_archetype (const char *name) 634get_archetype (const char *name)
675{ 635{
676 archetype *at = archetype::find (name); 636 archetype *at = archetype::find (name);
677 637
759} 719}
760 720
761/* 721/*
762 * Adds an archetype to the hashtable. 722 * Adds an archetype to the hashtable.
763 */ 723 */
764 724void
765static void 725archetype::hash_add ()
766add_arch (archetype *at)
767{ 726{
768#if USE_UNORDERED_MAP 727#if USE_UNORDERED_MAP
769 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 728 ht.insert (std::make_pair ((size_t) (const char *) name, this));
770#endif 729#else
771 730
772 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 731 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
773 732
774 for (;;) 733 for (;;)
775 { 734 {
776 if (arch_table[index] == NULL) 735 if (!arch_table[index])
777 { 736 {
778 arch_table[index] = at; 737 arch_table[index] = this;
779 return; 738 break;
780 } 739 }
781 740
782 if (++index == ARCHTABLE) 741 if (++index == ARCHTABLE)
783 index = 0; 742 index = 0;
784 743
785 if (index == org_index) 744 if (index == org_index)
786 fatal (ARCHTABLE_TOO_SMALL); 745 fatal (ARCHTABLE_TOO_SMALL);
787 } 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
788} 774}
789 775
790/* 776/*
791 * Returns the first archetype using the given type. 777 * Returns the first archetype using the given type.
792 * Used in treasure-generation. 778 * Used in treasure-generation.
812 798
813object * 799object *
814clone_arch (int type) 800clone_arch (int type)
815{ 801{
816 archetype *at; 802 archetype *at;
817 object *op = get_object ();
818 803
819 if ((at = type_to_archetype (type)) == NULL) 804 if ((at = type_to_archetype (type)) == NULL)
820 { 805 {
821 LOG (llevError, "Can't clone archetype %d\n", type); 806 LOG (llevError, "Can't clone archetype %d\n", type);
822 free_object (op);
823 return NULL; 807 return 0;
824 } 808 }
825 809
826 copy_object (&at->clone, op); 810 object *op = at->clone.clone ();
827 op->instantiate (); 811 op->instantiate ();
828 return op; 812 return op;
829} 813}
830 814
831/* 815/*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines