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.73 by root, Sun Apr 20 00:44:12 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
33archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing 32archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
34archetype *archetype::empty; 33arch_ptr archetype::empty;
35 34
36// the hashtable 35// the hashtable
37typedef std::tr1::unordered_map 36typedef std::tr1::unordered_map
38 < 37 <
39 const char *, 38 const char *,
41 str_hash, 40 str_hash,
42 str_equal, 41 str_equal,
43 slice_allocator< std::pair<const char *const, arch_ptr> > 42 slice_allocator< std::pair<const char *const, arch_ptr> >
44 > HT; 43 > HT;
45 44
46static HT ht (5000); 45static HT ht (10000);
47archvec archetypes; 46archvec archetypes;
47static unordered_vector<archetype *> allarch;
48static int dirtycnt;
48 49
49// the vector of other_arch references to be resolved 50// the vector of other_arch references to be resolved
50static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; 51static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref;
51// the vector of loaded but not yet committed archetypes 52// the vector of loaded but not yet committed archetypes
52static std::vector<archetype *> postponed_arch; 53static std::vector<archetype *> postponed_arch;
101{ 102{
102 shstr_cmp skill_cmp (skill); 103 shstr_cmp skill_cmp (skill);
103 104
104 for_all_archetypes (at) 105 for_all_archetypes (at)
105 if (at->skill == skill_cmp && (type == -1 || type == at->type)) 106 if (at->skill == skill_cmp && (type == -1 || type == at->type))
106 return arch_to_object (at); 107 return at->instance ();
107 108
108 return 0; 109 return 0;
109} 110}
110 111
111/* similiar to above - this returns the first archetype 112/* similiar to above - this returns the first archetype
132 * - name: The name we're searching for (ex: "writing pen"); 133 * - name: The name we're searching for (ex: "writing pen");
133 * Return value: 134 * Return value:
134 * - a corresponding object if found; a singularity object if not found. 135 * - a corresponding object if found; a singularity object if not found.
135 * 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
136 * 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
137 * 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,
138 * but it otherwise had a big memory leak. 139 * but it otherwise had a big memory leak.
139 */ 140 */
140object * 141object *
141get_archetype_by_object_name (const char *name) 142get_archetype_by_object_name (const char *name)
142{ 143{
148 for (i = strlen (tmpname); i > 0; i--) 149 for (i = strlen (tmpname); i > 0; i--)
149 { 150 {
150 tmpname[i] = 0; 151 tmpname[i] = 0;
151 152
152 if (archetype *at = find_archetype_by_object_name (tmpname)) 153 if (archetype *at = find_archetype_by_object_name (tmpname))
153 return arch_to_object (at); 154 return at->instance ();
154 } 155 }
155 156
156 return create_singularity (name); 157 return create_singularity (name);
157} 158}
158 159
286} 287}
287 288
288void 289void
289archetype::link () 290archetype::link ()
290{ 291{
291 ht.insert (std::make_pair (archname, this));
292
293 if (!archetypes.contains (this)) 292 if (!archetypes.contains (this))
293 {
294 archetypes.insert (this); 294 archetypes.insert (this);
295 ht.insert (std::make_pair (archname, this));
296 }
295} 297}
296 298
297void 299void
298archetype::unlink () 300archetype::unlink ()
299{ 301{
300 ht.erase (archname);
301
302 if (archetypes.contains (this)) 302 if (archetypes.contains (this))
303 {
303 archetypes.erase (this); 304 archetypes.erase (this);
305 ht.erase (archname);
306 }
304} 307}
305 308
306/* 309/*
307 * Finds, using the hashtable, which archetype matches the given name. 310 * Finds, using the hashtable, which archetype matches the given name.
308 * returns a pointer to the found archetype, otherwise NULL. 311 * returns a pointer to the found archetype, otherwise NULL.
412 archetype *less = new_head; 415 archetype *less = new_head;
413 for (auto (p, parts.begin () + 1); p != parts.end (); ++p) 416 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
414 { 417 {
415 archetype *at = *p; 418 archetype *at = *p;
416 419
417 // some flags get inherited formt he head (probably a lot more) 420 // some flags get inherited from the head (probably a lot more)
418 // doing it here doesn't feel too cozy, but it allows code 421 // doing it here doesn't feel too cozy, but it allows code
419 // to ignore head checks for these flags, which saves time 422 // to ignore head checks for these flags, which saves time
420 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; 423 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
421 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; 424 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
422 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; 425 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
423 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; 426 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
424 427
425 if (at->x < new_head->min_x) new_head->min_x = at->x; 428 new_head->min_x = min (new_head->min_x, at->x);
426 if (at->y < new_head->min_y) new_head->min_y = at->y; 429 new_head->min_y = min (new_head->min_y, at->y);
427 if (at->x > new_head->max_x) new_head->max_x = at->x; 430 new_head->max_x = max (new_head->max_x, at->x);
428 if (at->y > new_head->max_y) new_head->max_y = at->y; 431 new_head->max_y = max (new_head->max_y, at->y);
429 432
430 at->head = new_head; 433 at->head = new_head;
431 less->more = at; 434 less->more = at;
432 less = at; 435 less = at;
433 } 436 }
460 archetype *at = *p; 463 archetype *at = *p;
461 464
462 if (archetype *old = find (at->archname)) 465 if (archetype *old = find (at->archname))
463 old->unlink (); 466 old->unlink ();
464 467
468 allarch.push_back (at);
469
465 at->link (); 470 at->link ();
471 ++dirtycnt;
466 } 472 }
467 473
468 postponed_arch.clear (); 474 postponed_arch.clear ();
469 475
470 // now resolve arch references 476 // now resolve arch references
482 postponed_arch_ref.clear (); 488 postponed_arch_ref.clear ();
483 489
484 empty = find (shstr_empty_archetype); 490 empty = find (shstr_empty_archetype);
485} 491}
486 492
493void
494archetype::gc ()
495{
496 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
497 dirtycnt = max (0, dirtycnt - cnt);
498
499 do
500 {
501 static int idx;
502
503 if (idx >= allarch.size ())
504 if (idx)
505 idx = 0;
506 else
507 return;
508
509 archetype *at = allarch [idx];
510
511 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
512 ++idx;
513 else
514 {
515 LOG (llevDebug, "garbage collect arch %s", at->debug_desc ());
516 assert (at->arch == at); // verify that refcnt == 1 is truly valid
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 ();
529 at->arch = 0;
530 }
531 }
532 while (--cnt);
533}
534
487/* 535/*
488 * Creates and returns a new object which is a copy of the given archetype. 536 * Creates and returns a new object which is a copy of the given archetype.
489 * This function returns NULL on failure. 537 * This function returns NULL on failure.
490 */ 538 */
491object * 539object *
495 { 543 {
496 LOG (llevError, "Couldn't find archetype.\n"); 544 LOG (llevError, "Couldn't find archetype.\n");
497 return 0; 545 return 0;
498 } 546 }
499 547
548 return at->instance ();
549}
550
551object *
552archetype::instance ()
553{
500 object *op = at->clone (); 554 object *op = clone ();
501 op->arch = at;
502 op->instantiate (); 555 op->instantiate ();
503
504 return op; 556 return op;
505}
506
507object *
508archetype::instance ()
509{
510 return arch_to_object (this);
511} 557}
512 558
513/* 559/*
514 * Creates an object. This function is called by get_archetype() 560 * Creates an object. This function is called by get_archetype()
515 * if it fails to find the appropriate archetype. 561 * if it fails to find the appropriate archetype.
522 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); 568 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
523 569
524 if (!strcmp (name, "bug")) 570 if (!strcmp (name, "bug"))
525 abort (); 571 abort ();
526 572
527 char buf[MAX_BUF];
528 sprintf (buf, "bug, please report (%s)", name);
529
530 object *op = get_archetype ("bug"); 573 object *op = archetype::get ("bug");
531 op->name = op->name_pl = buf; 574 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
532 575
533 return op; 576 return op;
534} 577}
535 578
536/* 579/*
538 * object containing a copy of the archetype. 581 * object containing a copy of the archetype.
539 */ 582 */
540object * 583object *
541get_archetype (const char *name) 584get_archetype (const char *name)
542{ 585{
586 return archetype::get (name);
587}
588
589object *
590archetype::get (const char *name)
591{
543 archetype *at = archetype::find (name); 592 archetype *at = find (name);
544 593
545 if (!at) 594 if (!at)
546 return create_singularity (name); 595 return create_singularity (name);
547 596
548 return arch_to_object (at); 597 return at->instance ();
549} 598}
550 599
551/* 600/*
552 * Returns the first archetype using the given type. 601 * Returns the first archetype using the given type.
553 * Used in treasure-generation. 602 * Used in treasure-generation.
568 * Used in treasure-generation. 617 * Used in treasure-generation.
569 */ 618 */
570object * 619object *
571clone_arch (int type) 620clone_arch (int type)
572{ 621{
573 archetype *at; 622 archetype *at = type_to_archetype (type);
574 623
575 if ((at = type_to_archetype (type)) == NULL) 624 if (!at)
576 { 625 {
577 LOG (llevError, "Can't clone archetype %d\n", type); 626 LOG (llevError, "Can't clone archetype %d\n", type);
578 return 0; 627 return 0;
579 } 628 }
580 629
581 object *op = at->clone (); 630 return at->instance ();
582 op->instantiate ();
583 return op;
584} 631}
585 632
586/* 633/*
587 * member: make instance from class 634 * member: make instance from class
588 */ 635 */
591{ 638{
592 object *op, *prev = 0, *head = 0; 639 object *op, *prev = 0, *head = 0;
593 640
594 while (at) 641 while (at)
595 { 642 {
596 op = arch_to_object (at); 643 op = at->instance ();
597 644
598 op->x = at->x; 645 op->x = at->x;
599 op->y = at->y; 646 op->y = at->y;
600 647
601 if (head) 648 if (head)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines