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