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.25 by root, Tue Nov 7 16:30:54 2006 UTC vs.
Revision 1.34 by pippijn, Wed Jan 3 00:10:04 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").
358 archetype *at; 358 archetype *at;
359 359
360 LOG (llevDebug, " Setting up archetable...\n"); 360 LOG (llevDebug, " Setting up archetable...\n");
361 361
362 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)
363 add_arch (at); 363 at->hash_add ();
364 364
365 LOG (llevDebug, "done\n"); 365 LOG (llevDebug, "done\n");
366} 366}
367 367
368void 368void
381 delete 381 delete
382 at; 382 at;
383 383
384 i++; 384 i++;
385 } 385 }
386
386 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 387 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
387} 388}
388 389
389archetype::archetype () 390archetype::archetype ()
390{ 391{
391 clear_object (&clone); /* to initial state other also */
392 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 */
393 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 393 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
394} 394}
395 395
396archetype::~archetype () 396archetype::~archetype ()
397{ 397{
402 * of archetype-structures. 402 * of archetype-structures.
403 */ 403 */
404void 404void
405first_arch_pass (object_thawer & fp) 405first_arch_pass (object_thawer & fp)
406{ 406{
407 archetype *at, *head = NULL, *last_more = NULL; 407 archetype *head = 0, *last_more = 0;
408 408
409 archetype *at = new archetype;
409 at->clone.arch = first_archetype = at = new archetype; 410 at->clone.arch = first_archetype = at;
410 411
411 while (int i = load_object (fp, &at->clone, 0)) 412 while (int i = load_object (fp, &at->clone, 0))
412 { 413 {
413 at->clone.speed_left = (float) (-0.1); 414 at->clone.speed_left = (float) (-0.1);
414 /* 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
552void 553void
553load_archetypes (void) 554load_archetypes (void)
554{ 555{
555 char filename[MAX_BUF]; 556 char filename[MAX_BUF];
556 557
557#if TIME_ARCH_LOAD
558 struct timeval
559 tv1,
560 tv2;
561#endif
562
563 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 558 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
564 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 559 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
565 560
566 { 561 {
567 object_thawer 562 object_thawer
580 object_thawer 575 object_thawer
581 thawer (filename); 576 thawer (filename);
582 577
583 LOG (llevDebug, " loading treasure...\n"); 578 LOG (llevDebug, " loading treasure...\n");
584 load_treasures (); 579 load_treasures ();
580 LOG (llevDebug, " done\n");
585 LOG (llevDebug, " done\n arch-pass 2...\n"); 581 LOG (llevDebug, " arch-pass 2...\n");
586 second_arch_pass (thawer); 582 second_arch_pass (thawer);
587 LOG (llevDebug, " done\n"); 583 LOG (llevDebug, " done\n");
588#ifdef DEBUG 584#ifdef DEBUG
589 check_generators (); 585 check_generators ();
590#endif 586#endif
594 590
595/* 591/*
596 * Creates and returns a new object which is a copy of the given archetype. 592 * Creates and returns a new object which is a copy of the given archetype.
597 * This function returns NULL on failure. 593 * This function returns NULL on failure.
598 */ 594 */
599
600object * 595object *
601arch_to_object (archetype *at) 596arch_to_object (archetype *at)
602{ 597{
603 object *op; 598 object *op;
604 599
608 LOG (llevError, "Couldn't find archetype.\n"); 603 LOG (llevError, "Couldn't find archetype.\n");
609 604
610 return NULL; 605 return NULL;
611 } 606 }
612 607
613 op = get_object (); 608 op = at->clone.clone ();
614 copy_object (&at->clone, op);
615 op->arch = at; 609 op->arch = at;
616 op->instantiate (); 610 op->instantiate ();
617 return op; 611 return op;
618} 612}
619 613
621 * Creates an object. This function is called by get_archetype() 615 * Creates an object. This function is called by get_archetype()
622 * if it fails to find the appropriate archetype. 616 * if it fails to find the appropriate archetype.
623 * Thus get_archetype() will be guaranteed to always return 617 * Thus get_archetype() will be guaranteed to always return
624 * an object, and never NULL. 618 * an object, and never NULL.
625 */ 619 */
626
627object * 620object *
628create_singularity (const char *name) 621create_singularity (const char *name)
629{ 622{
630 object *op; 623 object *op;
631 char buf[MAX_BUF]; 624 char buf[MAX_BUF];
632 625
633 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 626 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
634 op = get_object (); 627 op = object::create ();
635 op->name = op->name_pl = buf; 628 op->name = op->name_pl = buf;
636 SET_FLAG (op, FLAG_NO_PICK); 629 SET_FLAG (op, FLAG_NO_PICK);
637 return op; 630 return op;
638} 631}
639 632
640/* 633/*
641 * Finds which archetype matches the given name, and returns a new 634 * Finds which archetype matches the given name, and returns a new
642 * object containing a copy of the archetype. 635 * object containing a copy of the archetype.
643 */ 636 */
644
645object * 637object *
646get_archetype (const char *name) 638get_archetype (const char *name)
647{ 639{
648 archetype *at = archetype::find (name); 640 archetype *at = archetype::find (name);
649 641
731} 723}
732 724
733/* 725/*
734 * Adds an archetype to the hashtable. 726 * Adds an archetype to the hashtable.
735 */ 727 */
736 728void
737static void 729archetype::hash_add ()
738add_arch (archetype *at)
739{ 730{
740#if USE_UNORDERED_MAP 731#if USE_UNORDERED_MAP
741 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 732 ht.insert (std::make_pair ((size_t) (const char *) name, this));
742#endif 733#else
743 734
744 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 735 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
745 736
746 for (;;) 737 for (;;)
747 { 738 {
748 if (arch_table[index] == NULL) 739 if (!arch_table[index])
749 { 740 {
750 arch_table[index] = at; 741 arch_table[index] = this;
751 return; 742 break;
752 } 743 }
753 744
754 if (++index == ARCHTABLE) 745 if (++index == ARCHTABLE)
755 index = 0; 746 index = 0;
756 747
757 if (index == org_index) 748 if (index == org_index)
758 fatal (ARCHTABLE_TOO_SMALL); 749 fatal (ARCHTABLE_TOO_SMALL);
759 } 750 }
751#endif
752}
753
754void
755archetype::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
760} 778}
761 779
762/* 780/*
763 * Returns the first archetype using the given type. 781 * Returns the first archetype using the given type.
764 * Used in treasure-generation. 782 * Used in treasure-generation.
784 802
785object * 803object *
786clone_arch (int type) 804clone_arch (int type)
787{ 805{
788 archetype *at; 806 archetype *at;
789 object *op = get_object ();
790 807
791 if ((at = type_to_archetype (type)) == NULL) 808 if ((at = type_to_archetype (type)) == NULL)
792 { 809 {
793 LOG (llevError, "Can't clone archetype %d\n", type); 810 LOG (llevError, "Can't clone archetype %d\n", type);
794 free_object (op);
795 return NULL; 811 return 0;
796 } 812 }
797 813
798 copy_object (&at->clone, op); 814 object *op = at->clone.clone ();
799 op->instantiate (); 815 op->instantiate ();
800 return op; 816 return op;
801} 817}
802 818
803/* 819/*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines