… | |
… | |
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 | */ |
142 | object * |
141 | object * |
143 | get_archetype_by_object_name (const char *name) |
142 | get_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 | |
… | |
… | |
441 | return new_head; |
440 | return new_head; |
442 | } |
441 | } |
443 | |
442 | |
444 | fail: |
443 | fail: |
445 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
444 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
446 | (*p)->destroy (true); |
445 | (*p)->destroy (); |
447 | |
446 | |
448 | return 0; |
447 | return 0; |
449 | } |
448 | } |
450 | |
449 | |
451 | void |
450 | void |
… | |
… | |
511 | |
510 | |
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->archname); |
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 | |
527 | /* |
|
|
528 | * Creates and returns a new object which is a copy of the given archetype. |
|
|
529 | * This function returns NULL on failure. |
|
|
530 | */ |
|
|
531 | object * |
535 | object * |
532 | arch_to_object (archetype *at) |
536 | archetype::instance () |
533 | { |
537 | { |
534 | if (!at) |
|
|
535 | { |
|
|
536 | LOG (llevError, "Couldn't find archetype.\n"); |
|
|
537 | return 0; |
|
|
538 | } |
|
|
539 | |
|
|
540 | object *op = at->clone (); |
538 | object *op = clone (); |
541 | op->arch = at; |
|
|
542 | op->instantiate (); |
539 | op->instantiate (); |
543 | |
|
|
544 | return op; |
540 | return op; |
545 | } |
|
|
546 | |
|
|
547 | object * |
|
|
548 | archetype::instance () |
|
|
549 | { |
|
|
550 | return arch_to_object (this); |
|
|
551 | } |
541 | } |
552 | |
542 | |
553 | /* |
543 | /* |
554 | * Creates an object. This function is called by get_archetype() |
544 | * Creates an object. This function is called by get_archetype() |
555 | * if it fails to find the appropriate archetype. |
545 | * if it fails to find the appropriate archetype. |
… | |
… | |
562 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
552 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
563 | |
553 | |
564 | if (!strcmp (name, "bug")) |
554 | if (!strcmp (name, "bug")) |
565 | abort (); |
555 | abort (); |
566 | |
556 | |
567 | char buf[MAX_BUF]; |
|
|
568 | sprintf (buf, "bug, please report (%s)", name); |
|
|
569 | |
|
|
570 | object *op = get_archetype ("bug"); |
557 | object *op = archetype::get (shstr_bug); |
571 | op->name = op->name_pl = buf; |
558 | op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name); |
572 | |
559 | |
573 | return op; |
560 | return op; |
574 | } |
561 | } |
575 | |
562 | |
576 | /* |
563 | /* |
… | |
… | |
578 | * object containing a copy of the archetype. |
565 | * object containing a copy of the archetype. |
579 | */ |
566 | */ |
580 | object * |
567 | object * |
581 | get_archetype (const char *name) |
568 | get_archetype (const char *name) |
582 | { |
569 | { |
|
|
570 | return archetype::get (name); |
|
|
571 | } |
|
|
572 | |
|
|
573 | object * |
|
|
574 | archetype::get (const char *name) |
|
|
575 | { |
583 | archetype *at = archetype::find (name); |
576 | archetype *at = find (name); |
584 | |
577 | |
585 | if (!at) |
578 | if (!at) |
586 | return create_singularity (name); |
579 | return create_singularity (name); |
587 | |
580 | |
588 | return arch_to_object (at); |
581 | return at->instance (); |
589 | } |
582 | } |
590 | |
583 | |
591 | /* |
584 | /* |
592 | * Returns the first archetype using the given type. |
585 | * Returns the first archetype using the given type. |
593 | * Used in treasure-generation. |
586 | * Used in treasure-generation. |
… | |
… | |
608 | * Used in treasure-generation. |
601 | * Used in treasure-generation. |
609 | */ |
602 | */ |
610 | object * |
603 | object * |
611 | clone_arch (int type) |
604 | clone_arch (int type) |
612 | { |
605 | { |
613 | archetype *at; |
606 | archetype *at = type_to_archetype (type); |
614 | |
607 | |
615 | if ((at = type_to_archetype (type)) == NULL) |
608 | if (!at) |
616 | { |
609 | { |
617 | LOG (llevError, "Can't clone archetype %d\n", type); |
610 | LOG (llevError, "Can't clone archetype %d\n", type); |
618 | return 0; |
611 | return 0; |
619 | } |
612 | } |
620 | |
613 | |
621 | object *op = at->clone (); |
614 | return at->instance (); |
622 | op->instantiate (); |
|
|
623 | return op; |
|
|
624 | } |
615 | } |
625 | |
616 | |
626 | /* |
617 | /* |
627 | * member: make instance from class |
618 | * member: make instance from class |
628 | */ |
619 | */ |
… | |
… | |
631 | { |
622 | { |
632 | object *op, *prev = 0, *head = 0; |
623 | object *op, *prev = 0, *head = 0; |
633 | |
624 | |
634 | while (at) |
625 | while (at) |
635 | { |
626 | { |
636 | op = arch_to_object (at); |
627 | op = at->instance (); |
637 | |
628 | |
638 | op->x = at->x; |
629 | op->x = at->x; |
639 | op->y = at->y; |
630 | op->y = at->y; |
640 | |
631 | |
641 | if (head) |
632 | if (head) |