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.74 by root, Sun Apr 20 05:24:55 2008 UTC vs.
Revision 1.80 by root, Sun May 4 18:46:01 2008 UTC

22 */ 22 */
23 23
24#include <cassert> 24#include <cassert>
25 25
26#include <global.h> 26#include <global.h>
27#include <funcpoint.h>
28#include <loader.h> 27#include <loader.h>
29 28
30#include <tr1/functional> 29#include <tr1/functional>
31#include <tr1/unordered_map> 30#include <tr1/unordered_map>
32 31
103{ 102{
104 shstr_cmp skill_cmp (skill); 103 shstr_cmp skill_cmp (skill);
105 104
106 for_all_archetypes (at) 105 for_all_archetypes (at)
107 if (at->skill == skill_cmp && (type == -1 || type == at->type)) 106 if (at->skill == skill_cmp && (type == -1 || type == at->type))
108 return arch_to_object (at); 107 return at->instance ();
109 108
110 return 0; 109 return 0;
111} 110}
112 111
113/* similiar to above - this returns the first archetype 112/* similiar to above - this returns the first archetype
134 * - name: The name we're searching for (ex: "writing pen"); 133 * - name: The name we're searching for (ex: "writing pen");
135 * Return value: 134 * Return value:
136 * - a corresponding object if found; a singularity object if not found. 135 * - a corresponding object if found; a singularity object if not found.
137 * Note by MSW - it appears that it takes the full name and keeps 136 * Note by MSW - it appears that it takes the full name and keeps
138 * shortening it until it finds a match. I re-wrote this so that it 137 * shortening it until it finds a match. I re-wrote this so that it
139 * doesn't malloc it each time - not that this function is used much, 138 * doesn't allocate it each time - not that this function is used much,
140 * but it otherwise had a big memory leak. 139 * but it otherwise had a big memory leak.
141 */ 140 */
142object * 141object *
143get_archetype_by_object_name (const char *name) 142get_archetype_by_object_name (const char *name)
144{ 143{
150 for (i = strlen (tmpname); i > 0; i--) 149 for (i = strlen (tmpname); i > 0; i--)
151 { 150 {
152 tmpname[i] = 0; 151 tmpname[i] = 0;
153 152
154 if (archetype *at = find_archetype_by_object_name (tmpname)) 153 if (archetype *at = find_archetype_by_object_name (tmpname))
155 return arch_to_object (at); 154 return at->instance ();
156 } 155 }
157 156
158 return create_singularity (name); 157 return create_singularity (name);
159} 158}
160 159
512 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object 511 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
513 ++idx; 512 ++idx;
514 else 513 else
515 { 514 {
516 LOG (llevDebug, "garbage collect arch %s", at->debug_desc ()); 515 LOG (llevDebug, "garbage collect arch %s", at->debug_desc ());
517 assert (at->arch == at); // verify that refcnt == 1 is truely valid 516 assert (at->arch == at); // verify that refcnt == 1 is truly valid
518 allarch.erase (idx); 517 allarch.erase (idx);
518
519 // break chain
520 for (object *op = at->head_ (); op; )
521 {
522 object *next = op->more;
523 op->head = 0;
524 op->more = 0;
525 op = next;
526 }
527
528 at->destroy ();
519 at->arch = 0; 529 at->arch = 0;
520 at->more = 0;
521 at->destroy ();
522 } 530 }
523 } 531 }
524 while (--cnt); 532 while (--cnt);
525} 533}
526 534
535 { 543 {
536 LOG (llevError, "Couldn't find archetype.\n"); 544 LOG (llevError, "Couldn't find archetype.\n");
537 return 0; 545 return 0;
538 } 546 }
539 547
548 return at->instance ();
549}
550
551object *
552archetype::instance ()
553{
540 object *op = at->clone (); 554 object *op = clone ();
541 op->arch = at;
542 op->instantiate (); 555 op->instantiate ();
543
544 return op; 556 return op;
545}
546
547object *
548archetype::instance ()
549{
550 return arch_to_object (this);
551} 557}
552 558
553/* 559/*
554 * Creates an object. This function is called by get_archetype() 560 * Creates an object. This function is called by get_archetype()
555 * if it fails to find the appropriate archetype. 561 * if it fails to find the appropriate archetype.
562 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); 568 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
563 569
564 if (!strcmp (name, "bug")) 570 if (!strcmp (name, "bug"))
565 abort (); 571 abort ();
566 572
567 char buf[MAX_BUF];
568 sprintf (buf, "bug, please report (%s)", name);
569
570 object *op = get_archetype ("bug"); 573 object *op = archetype::get ("bug");
571 op->name = op->name_pl = buf; 574 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
572 575
573 return op; 576 return op;
574} 577}
575 578
576/* 579/*
578 * object containing a copy of the archetype. 581 * object containing a copy of the archetype.
579 */ 582 */
580object * 583object *
581get_archetype (const char *name) 584get_archetype (const char *name)
582{ 585{
586 return archetype::get (name);
587}
588
589object *
590archetype::get (const char *name)
591{
583 archetype *at = archetype::find (name); 592 archetype *at = find (name);
584 593
585 if (!at) 594 if (!at)
586 return create_singularity (name); 595 return create_singularity (name);
587 596
588 return arch_to_object (at); 597 return at->instance ();
589} 598}
590 599
591/* 600/*
592 * Returns the first archetype using the given type. 601 * Returns the first archetype using the given type.
593 * Used in treasure-generation. 602 * Used in treasure-generation.
608 * Used in treasure-generation. 617 * Used in treasure-generation.
609 */ 618 */
610object * 619object *
611clone_arch (int type) 620clone_arch (int type)
612{ 621{
613 archetype *at; 622 archetype *at = type_to_archetype (type);
614 623
615 if ((at = type_to_archetype (type)) == NULL) 624 if (!at)
616 { 625 {
617 LOG (llevError, "Can't clone archetype %d\n", type); 626 LOG (llevError, "Can't clone archetype %d\n", type);
618 return 0; 627 return 0;
619 } 628 }
620 629
621 object *op = at->clone (); 630 return at->instance ();
622 op->instantiate ();
623 return op;
624} 631}
625 632
626/* 633/*
627 * member: make instance from class 634 * member: make instance from class
628 */ 635 */
631{ 638{
632 object *op, *prev = 0, *head = 0; 639 object *op, *prev = 0, *head = 0;
633 640
634 while (at) 641 while (at)
635 { 642 {
636 op = arch_to_object (at); 643 op = at->instance ();
637 644
638 op->x = at->x; 645 op->x = at->x;
639 op->y = at->y; 646 op->y = at->y;
640 647
641 if (head) 648 if (head)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines