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.87 by root, Thu Jan 1 11:41:17 2009 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
257 retval = 6; 258 retval = 6;
258 /* base name matched - not bad */ 259 /* base name matched - not bad */
259 else if (strcasecmp (cp, op->name) == 0 && !count) 260 else if (strcasecmp (cp, op->name) == 0 && !count)
260 retval = 4; 261 retval = 4;
261 /* Check for partial custom name, but give a real low priority */ 262 /* Check for partial custom name, but give a real low priority */
262 else if (op->custom_name && strstr (op->custom_name, cp)) 263 else if (op->custom_name && op->custom_name.contains (cp))
263 retval = 3; 264 retval = 3;
264 265
265 if (retval) 266 if (retval)
266 { 267 {
267 if (pl->type == PLAYER) 268 if (pl->type == PLAYER)
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 }
437 return new_head; 440 return new_head;
438 } 441 }
439 442
440fail: 443fail:
441 for (auto (p, parts.begin ()); p != parts.end (); ++p) 444 for (auto (p, parts.begin ()); p != parts.end (); ++p)
442 (*p)->destroy (true); 445 (*p)->destroy ();
443 446
444 return 0; 447 return 0;
445} 448}
446 449
447void 450void
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
487/* 493void
488 * Creates and returns a new object which is a copy of the given archetype. 494archetype::gc ()
489 * This function returns NULL on failure.
490 */
491object *
492arch_to_object (archetype *at)
493{ 495{
494 if (!at) 496 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
497 dirtycnt = max (0, dirtycnt - cnt);
498
499 do
495 { 500 {
496 LOG (llevError, "Couldn't find archetype.\n"); 501 static int idx;
502
503 if (idx >= allarch.size ())
504 if (idx)
505 idx = 0;
506 else
497 return 0; 507 return;
498 }
499 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->archname);
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
535object *
536archetype::instance ()
537{
500 object *op = at->clone (); 538 object *op = clone ();
501 op->arch = at;
502 op->instantiate (); 539 op->instantiate ();
503
504 return op; 540 return op;
505}
506
507object *
508archetype::instance ()
509{
510 return arch_to_object (this);
511} 541}
512 542
513/* 543/*
514 * Creates an object. This function is called by get_archetype() 544 * Creates an object. This function is called by get_archetype()
515 * if it fails to find the appropriate archetype. 545 * if it fails to find the appropriate archetype.
522 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); 552 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
523 553
524 if (!strcmp (name, "bug")) 554 if (!strcmp (name, "bug"))
525 abort (); 555 abort ();
526 556
527 char buf[MAX_BUF];
528 sprintf (buf, "bug, please report (%s)", name);
529
530 object *op = get_archetype ("bug"); 557 object *op = archetype::get (shstr_bug);
531 op->name = op->name_pl = buf; 558 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
532 559
533 return op; 560 return op;
534} 561}
535 562
536/* 563/*
538 * object containing a copy of the archetype. 565 * object containing a copy of the archetype.
539 */ 566 */
540object * 567object *
541get_archetype (const char *name) 568get_archetype (const char *name)
542{ 569{
570 return archetype::get (name);
571}
572
573object *
574archetype::get (const char *name)
575{
543 archetype *at = archetype::find (name); 576 archetype *at = find (name);
544 577
545 if (!at) 578 if (!at)
546 return create_singularity (name); 579 return create_singularity (name);
547 580
548 return arch_to_object (at); 581 return at->instance ();
549} 582}
550 583
551/* 584/*
552 * Returns the first archetype using the given type. 585 * Returns the first archetype using the given type.
553 * Used in treasure-generation. 586 * Used in treasure-generation.
568 * Used in treasure-generation. 601 * Used in treasure-generation.
569 */ 602 */
570object * 603object *
571clone_arch (int type) 604clone_arch (int type)
572{ 605{
573 archetype *at; 606 archetype *at = type_to_archetype (type);
574 607
575 if ((at = type_to_archetype (type)) == NULL) 608 if (!at)
576 { 609 {
577 LOG (llevError, "Can't clone archetype %d\n", type); 610 LOG (llevError, "Can't clone archetype %d\n", type);
578 return 0; 611 return 0;
579 } 612 }
580 613
581 object *op = at->clone (); 614 return at->instance ();
582 op->instantiate ();
583 return op;
584} 615}
585 616
586/* 617/*
587 * member: make instance from class 618 * member: make instance from class
588 */ 619 */
591{ 622{
592 object *op, *prev = 0, *head = 0; 623 object *op, *prev = 0, *head = 0;
593 624
594 while (at) 625 while (at)
595 { 626 {
596 op = arch_to_object (at); 627 op = at->instance ();
597 628
598 op->x = at->x; 629 op->x = at->x;
599 op->y = at->y; 630 op->y = at->y;
600 631
601 if (head) 632 if (head)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines