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.30 by root, Wed Dec 13 00:42:03 2006 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}
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} 366}
395 367
396void 368void
397free_all_archs (void) 369free_all_archs (void)
398{ 370{
409 delete 381 delete
410 at; 382 at;
411 383
412 i++; 384 i++;
413 } 385 }
386
414 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 387 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
415} 388}
416 389
417archetype::archetype () 390archetype::archetype ()
418{ 391{
419 clear_object (&clone); /* to initial state other also */ 392 clear_object (&clone); /* to initial state other also */
420 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_object() */ 393 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */
421 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 394 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
422} 395}
423 396
424archetype::~archetype () 397archetype::~archetype ()
425{ 398{
430 * of archetype-structures. 403 * of archetype-structures.
431 */ 404 */
432void 405void
433first_arch_pass (object_thawer & fp) 406first_arch_pass (object_thawer & fp)
434{ 407{
435 archetype *at, *head = NULL, *last_more = NULL; 408 archetype *head = 0, *last_more = 0;
436 409
410 archetype *at = new archetype;
437 at->clone.arch = first_archetype = at = new archetype; 411 at->clone.arch = first_archetype = at;
438 412
439 while (int i = load_object (fp, &at->clone, 0)) 413 while (int i = load_object (fp, &at->clone, 0))
440 { 414 {
441 at->clone.speed_left = (float) (-0.1); 415 at->clone.speed_left = (float) (-0.1);
442 /* copy the body_info to the body_used - this is only really 416 /* copy the body_info to the body_used - this is only really
580void 554void
581load_archetypes (void) 555load_archetypes (void)
582{ 556{
583 char filename[MAX_BUF]; 557 char filename[MAX_BUF];
584 558
585#if TIME_ARCH_LOAD
586 struct timeval
587 tv1,
588 tv2;
589#endif
590
591 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 559 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
592 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 560 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
593 561
594 { 562 {
595 object_thawer 563 object_thawer
636 LOG (llevError, "Couldn't find archetype.\n"); 604 LOG (llevError, "Couldn't find archetype.\n");
637 605
638 return NULL; 606 return NULL;
639 } 607 }
640 608
641 op = get_object (); 609 op = at->clone.clone ();
642 copy_object (&at->clone, op);
643 op->arch = at; 610 op->arch = at;
644 op->instantiate (); 611 op->instantiate ();
645 return op; 612 return op;
646} 613}
647 614
657{ 624{
658 object *op; 625 object *op;
659 char buf[MAX_BUF]; 626 char buf[MAX_BUF];
660 627
661 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); 628 sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name);
662 op = get_object (); 629 op = object::create ();
663 op->name = op->name_pl = buf; 630 op->name = op->name_pl = buf;
664 SET_FLAG (op, FLAG_NO_PICK); 631 SET_FLAG (op, FLAG_NO_PICK);
665 return op; 632 return op;
666} 633}
667 634
759} 726}
760 727
761/* 728/*
762 * Adds an archetype to the hashtable. 729 * Adds an archetype to the hashtable.
763 */ 730 */
764 731void
765static void 732archetype::hash_add ()
766add_arch (archetype *at)
767{ 733{
768#if USE_UNORDERED_MAP 734#if USE_UNORDERED_MAP
769 ht.insert (std::make_pair ((size_t) (const char *) at->name, at)); 735 ht.insert (std::make_pair ((size_t) (const char *) name, this));
770#endif 736#else
771 737
772 int index = hasharch ((const char *) at->name, ARCHTABLE), org_index = index; 738 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
773 739
774 for (;;) 740 for (;;)
775 { 741 {
776 if (arch_table[index] == NULL) 742 if (!arch_table[index])
777 { 743 {
778 arch_table[index] = at; 744 arch_table[index] = this;
779 return; 745 break;
780 } 746 }
781 747
782 if (++index == ARCHTABLE) 748 if (++index == ARCHTABLE)
783 index = 0; 749 index = 0;
784 750
785 if (index == org_index) 751 if (index == org_index)
786 fatal (ARCHTABLE_TOO_SMALL); 752 fatal (ARCHTABLE_TOO_SMALL);
787 } 753 }
754#endif
755}
756
757void
758archetype::hash_del ()
759{
760#if USE_UNORDERED_MAP
761# error remove this from HT
762#else
763
764 int index = hasharch ((const char *) name, ARCHTABLE), org_index = index;
765
766 for (;;)
767 {
768 if (arch_table[index] == this)
769 {
770 arch_table[index] = 0;
771 break;
772 }
773
774 if (++index == ARCHTABLE)
775 index = 0;
776
777 if (index == org_index)
778 break;
779 }
780#endif
788} 781}
789 782
790/* 783/*
791 * Returns the first archetype using the given type. 784 * Returns the first archetype using the given type.
792 * Used in treasure-generation. 785 * Used in treasure-generation.
812 805
813object * 806object *
814clone_arch (int type) 807clone_arch (int type)
815{ 808{
816 archetype *at; 809 archetype *at;
817 object *op = get_object ();
818 810
819 if ((at = type_to_archetype (type)) == NULL) 811 if ((at = type_to_archetype (type)) == NULL)
820 { 812 {
821 LOG (llevError, "Can't clone archetype %d\n", type); 813 LOG (llevError, "Can't clone archetype %d\n", type);
822 free_object (op);
823 return NULL; 814 return 0;
824 } 815 }
825 816
826 copy_object (&at->clone, op); 817 object *op = at->clone.clone ();
827 op->instantiate (); 818 op->instantiate ();
828 return op; 819 return op;
829} 820}
830 821
831/* 822/*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines