… | |
… | |
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 | |
… | |
… | |
258 | retval = 6; |
258 | retval = 6; |
259 | /* base name matched - not bad */ |
259 | /* base name matched - not bad */ |
260 | else if (strcasecmp (cp, op->name) == 0 && !count) |
260 | else if (strcasecmp (cp, op->name) == 0 && !count) |
261 | retval = 4; |
261 | retval = 4; |
262 | /* Check for partial custom name, but give a real low priority */ |
262 | /* Check for partial custom name, but give a real low priority */ |
263 | else if (op->custom_name && strstr (op->custom_name, cp)) |
263 | else if (op->custom_name.contains (cp)) |
264 | retval = 3; |
264 | retval = 3; |
265 | |
265 | |
266 | if (retval) |
266 | if (retval) |
267 | { |
267 | { |
268 | if (pl->type == PLAYER) |
268 | if (pl->type == PLAYER) |
… | |
… | |
440 | return new_head; |
440 | return new_head; |
441 | } |
441 | } |
442 | |
442 | |
443 | fail: |
443 | fail: |
444 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
444 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
445 | (*p)->destroy (true); |
445 | (*p)->destroy (); |
446 | |
446 | |
447 | return 0; |
447 | return 0; |
448 | } |
448 | } |
449 | |
449 | |
450 | void |
450 | void |
… | |
… | |
510 | |
510 | |
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->archname); |
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 | |
518 | |
519 | // break chain |
519 | // break chain |
520 | for (object *op = at->head_ (); op; ) |
520 | for (object *op = at->head_ (); op; ) |
521 | { |
521 | { |
… | |
… | |
530 | } |
530 | } |
531 | } |
531 | } |
532 | while (--cnt); |
532 | while (--cnt); |
533 | } |
533 | } |
534 | |
534 | |
535 | /* |
|
|
536 | * Creates and returns a new object which is a copy of the given archetype. |
|
|
537 | * This function returns NULL on failure. |
|
|
538 | */ |
|
|
539 | object * |
535 | object * |
540 | arch_to_object (archetype *at) |
536 | archetype::instance () |
541 | { |
537 | { |
542 | if (!at) |
|
|
543 | { |
|
|
544 | LOG (llevError, "Couldn't find archetype.\n"); |
|
|
545 | return 0; |
|
|
546 | } |
|
|
547 | |
|
|
548 | object *op = at->clone (); |
538 | object *op = clone (); |
549 | op->arch = at; |
|
|
550 | op->instantiate (); |
539 | op->instantiate (); |
551 | |
|
|
552 | return op; |
540 | return op; |
553 | } |
|
|
554 | |
|
|
555 | object * |
|
|
556 | archetype::instance () |
|
|
557 | { |
|
|
558 | return arch_to_object (this); |
|
|
559 | } |
541 | } |
560 | |
542 | |
561 | /* |
543 | /* |
562 | * Creates an object. This function is called by get_archetype() |
544 | * Creates an object. This function is called by get_archetype() |
563 | * if it fails to find the appropriate archetype. |
545 | * if it fails to find the appropriate archetype. |
… | |
… | |
570 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
552 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
571 | |
553 | |
572 | if (!strcmp (name, "bug")) |
554 | if (!strcmp (name, "bug")) |
573 | abort (); |
555 | abort (); |
574 | |
556 | |
575 | char buf[MAX_BUF]; |
|
|
576 | sprintf (buf, "bug, please report (%s)", name); |
|
|
577 | |
|
|
578 | object *op = get_archetype ("bug"); |
557 | object *op = archetype::get (shstr_bug); |
579 | op->name = op->name_pl = buf; |
558 | op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name); |
580 | |
559 | |
581 | return op; |
560 | return op; |
582 | } |
561 | } |
583 | |
562 | |
584 | /* |
563 | /* |
… | |
… | |
586 | * object containing a copy of the archetype. |
565 | * object containing a copy of the archetype. |
587 | */ |
566 | */ |
588 | object * |
567 | object * |
589 | get_archetype (const char *name) |
568 | get_archetype (const char *name) |
590 | { |
569 | { |
|
|
570 | return archetype::get (name); |
|
|
571 | } |
|
|
572 | |
|
|
573 | object * |
|
|
574 | archetype::get (const char *name) |
|
|
575 | { |
591 | archetype *at = archetype::find (name); |
576 | archetype *at = find (name); |
592 | |
577 | |
593 | if (!at) |
578 | if (!at) |
594 | return create_singularity (name); |
579 | return create_singularity (name); |
595 | |
580 | |
596 | return arch_to_object (at); |
581 | return at->instance (); |
597 | } |
|
|
598 | |
|
|
599 | object * |
|
|
600 | archetype::get (const char *name) |
|
|
601 | { |
|
|
602 | return get_archetype (name); |
|
|
603 | } |
582 | } |
604 | |
583 | |
605 | /* |
584 | /* |
606 | * Returns the first archetype using the given type. |
585 | * Returns the first archetype using the given type. |
607 | * Used in treasure-generation. |
586 | * Used in treasure-generation. |
… | |
… | |
622 | * Used in treasure-generation. |
601 | * Used in treasure-generation. |
623 | */ |
602 | */ |
624 | object * |
603 | object * |
625 | clone_arch (int type) |
604 | clone_arch (int type) |
626 | { |
605 | { |
627 | archetype *at; |
606 | archetype *at = type_to_archetype (type); |
628 | |
607 | |
629 | if ((at = type_to_archetype (type)) == NULL) |
608 | if (!at) |
630 | { |
609 | { |
631 | LOG (llevError, "Can't clone archetype %d\n", type); |
610 | LOG (llevError, "Can't clone archetype %d\n", type); |
632 | return 0; |
611 | return 0; |
633 | } |
612 | } |
634 | |
613 | |
635 | object *op = at->clone (); |
614 | return at->instance (); |
636 | op->instantiate (); |
|
|
637 | return op; |
|
|
638 | } |
615 | } |
639 | |
616 | |
640 | /* |
617 | /* |
641 | * member: make instance from class |
618 | * member: make instance from class |
642 | */ |
619 | */ |
… | |
… | |
645 | { |
622 | { |
646 | object *op, *prev = 0, *head = 0; |
623 | object *op, *prev = 0, *head = 0; |
647 | |
624 | |
648 | while (at) |
625 | while (at) |
649 | { |
626 | { |
650 | op = arch_to_object (at); |
627 | op = at->instance (); |
651 | |
628 | |
652 | op->x = at->x; |
629 | op->x = at->x; |
653 | op->y = at->y; |
630 | op->y = at->y; |
654 | |
631 | |
655 | if (head) |
632 | if (head) |