… | |
… | |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation, either version 3 of the License, or |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
11 | * option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | #include <cassert> |
25 | #include <cassert> |
25 | |
26 | |
26 | #include <global.h> |
27 | #include <global.h> |
27 | #include <funcpoint.h> |
|
|
28 | #include <loader.h> |
28 | #include <loader.h> |
29 | |
29 | |
30 | #include <tr1/functional> |
30 | #include <tr1/functional> |
31 | #include <tr1/unordered_map> |
31 | #include <tr1/unordered_map> |
32 | |
32 | |
33 | archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing |
33 | archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
34 | archetype *archetype::empty; |
34 | arch_ptr archetype::empty; |
35 | |
35 | |
36 | // the hashtable |
36 | // the hashtable |
37 | typedef std::tr1::unordered_map |
37 | typedef std::tr1::unordered_map |
38 | < |
38 | < |
39 | const char *, |
39 | const char *, |
… | |
… | |
41 | str_hash, |
41 | str_hash, |
42 | str_equal, |
42 | str_equal, |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
44 | > HT; |
44 | > HT; |
45 | |
45 | |
46 | static HT ht (5000); |
46 | static HT ht (10000); |
47 | archvec archetypes; |
47 | archvec archetypes; |
|
|
48 | static unordered_vector<archetype *> allarch; |
|
|
49 | static int dirtycnt; |
48 | |
50 | |
49 | // the vector of other_arch references to be resolved |
51 | // the vector of other_arch references to be resolved |
50 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
52 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
51 | // the vector of loaded but not yet committed archetypes |
53 | // the vector of loaded but not yet committed archetypes |
52 | static std::vector<archetype *> postponed_arch; |
54 | static std::vector<archetype *> postponed_arch; |
… | |
… | |
101 | { |
103 | { |
102 | shstr_cmp skill_cmp (skill); |
104 | shstr_cmp skill_cmp (skill); |
103 | |
105 | |
104 | for_all_archetypes (at) |
106 | for_all_archetypes (at) |
105 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
107 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
106 | return arch_to_object (at); |
108 | return at->instance (); |
107 | |
109 | |
108 | return 0; |
110 | return 0; |
109 | } |
111 | } |
110 | |
112 | |
111 | /* similiar to above - this returns the first archetype |
113 | /* similiar to above - this returns the first archetype |
… | |
… | |
132 | * - name: The name we're searching for (ex: "writing pen"); |
134 | * - name: The name we're searching for (ex: "writing pen"); |
133 | * Return value: |
135 | * Return value: |
134 | * - a corresponding object if found; a singularity object if not found. |
136 | * - 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 |
137 | * 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 |
138 | * 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, |
139 | * doesn't allocate it each time - not that this function is used much, |
138 | * but it otherwise had a big memory leak. |
140 | * but it otherwise had a big memory leak. |
139 | */ |
141 | */ |
140 | object * |
142 | object * |
141 | get_archetype_by_object_name (const char *name) |
143 | get_archetype_by_object_name (const char *name) |
142 | { |
144 | { |
… | |
… | |
148 | for (i = strlen (tmpname); i > 0; i--) |
150 | for (i = strlen (tmpname); i > 0; i--) |
149 | { |
151 | { |
150 | tmpname[i] = 0; |
152 | tmpname[i] = 0; |
151 | |
153 | |
152 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
154 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
153 | return arch_to_object (at); |
155 | return at->instance (); |
154 | } |
156 | } |
155 | |
157 | |
156 | return create_singularity (name); |
158 | return create_singularity (name); |
157 | } |
159 | } |
158 | |
160 | |
… | |
… | |
257 | retval = 6; |
259 | retval = 6; |
258 | /* base name matched - not bad */ |
260 | /* base name matched - not bad */ |
259 | else if (strcasecmp (cp, op->name) == 0 && !count) |
261 | else if (strcasecmp (cp, op->name) == 0 && !count) |
260 | retval = 4; |
262 | retval = 4; |
261 | /* Check for partial custom name, but give a real low priority */ |
263 | /* Check for partial custom name, but give a real low priority */ |
262 | else if (op->custom_name && strstr (op->custom_name, cp)) |
264 | else if (op->custom_name.contains (cp)) |
263 | retval = 3; |
265 | retval = 3; |
264 | |
266 | |
265 | if (retval) |
267 | if (retval) |
266 | { |
268 | { |
267 | if (pl->type == PLAYER) |
269 | if (pl->type == PLAYER) |
… | |
… | |
286 | } |
288 | } |
287 | |
289 | |
288 | void |
290 | void |
289 | archetype::link () |
291 | archetype::link () |
290 | { |
292 | { |
291 | ht.insert (std::make_pair (archname, this)); |
|
|
292 | |
|
|
293 | if (!archetypes.contains (this)) |
293 | if (!archetypes.contains (this)) |
|
|
294 | { |
294 | archetypes.insert (this); |
295 | archetypes.insert (this); |
|
|
296 | ht.insert (std::make_pair (archname, this)); |
|
|
297 | } |
295 | } |
298 | } |
296 | |
299 | |
297 | void |
300 | void |
298 | archetype::unlink () |
301 | archetype::unlink () |
299 | { |
302 | { |
300 | ht.erase (archname); |
|
|
301 | |
|
|
302 | if (archetypes.contains (this)) |
303 | if (archetypes.contains (this)) |
|
|
304 | { |
303 | archetypes.erase (this); |
305 | archetypes.erase (this); |
|
|
306 | ht.erase (archname); |
|
|
307 | } |
304 | } |
308 | } |
305 | |
309 | |
306 | /* |
310 | /* |
307 | * Finds, using the hashtable, which archetype matches the given name. |
311 | * Finds, using the hashtable, which archetype matches the given name. |
308 | * returns a pointer to the found archetype, otherwise NULL. |
312 | * returns a pointer to the found archetype, otherwise NULL. |
… | |
… | |
412 | archetype *less = new_head; |
416 | archetype *less = new_head; |
413 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
417 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
414 | { |
418 | { |
415 | archetype *at = *p; |
419 | archetype *at = *p; |
416 | |
420 | |
417 | // some flags get inherited formt he head (probably a lot more) |
421 | // some flags get inherited from the head (probably a lot more) |
418 | // doing it here doesn't feel too cozy, but it allows code |
422 | // doing it here doesn't feel too cozy, but it allows code |
419 | // to ignore head checks for these flags, which saves time |
423 | // to ignore head checks for these flags, which saves time |
420 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
424 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
421 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
425 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
422 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
426 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
423 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
427 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
424 | |
428 | |
425 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
429 | 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; |
430 | 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; |
431 | 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; |
432 | new_head->max_y = max (new_head->max_y, at->y); |
429 | |
433 | |
430 | at->head = new_head; |
434 | at->head = new_head; |
431 | less->more = at; |
435 | less->more = at; |
432 | less = at; |
436 | less = at; |
433 | } |
437 | } |
… | |
… | |
437 | return new_head; |
441 | return new_head; |
438 | } |
442 | } |
439 | |
443 | |
440 | fail: |
444 | fail: |
441 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
445 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
442 | (*p)->destroy (true); |
446 | (*p)->destroy (); |
443 | |
447 | |
444 | return 0; |
448 | return 0; |
445 | } |
449 | } |
446 | |
450 | |
447 | void |
451 | void |
… | |
… | |
460 | archetype *at = *p; |
464 | archetype *at = *p; |
461 | |
465 | |
462 | if (archetype *old = find (at->archname)) |
466 | if (archetype *old = find (at->archname)) |
463 | old->unlink (); |
467 | old->unlink (); |
464 | |
468 | |
|
|
469 | allarch.push_back (at); |
|
|
470 | |
465 | at->link (); |
471 | at->link (); |
|
|
472 | ++dirtycnt; |
466 | } |
473 | } |
467 | |
474 | |
468 | postponed_arch.clear (); |
475 | postponed_arch.clear (); |
469 | |
476 | |
470 | // now resolve arch references |
477 | // now resolve arch references |
… | |
… | |
482 | postponed_arch_ref.clear (); |
489 | postponed_arch_ref.clear (); |
483 | |
490 | |
484 | empty = find (shstr_empty_archetype); |
491 | empty = find (shstr_empty_archetype); |
485 | } |
492 | } |
486 | |
493 | |
487 | /* |
494 | void |
488 | * Creates and returns a new object which is a copy of the given archetype. |
495 | archetype::gc () |
489 | * This function returns NULL on failure. |
|
|
490 | */ |
|
|
491 | object * |
|
|
492 | arch_to_object (archetype *at) |
|
|
493 | { |
496 | { |
494 | if (!at) |
497 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
|
|
498 | dirtycnt = max (0, dirtycnt - cnt); |
|
|
499 | |
|
|
500 | do |
495 | { |
501 | { |
496 | LOG (llevError, "Couldn't find archetype.\n"); |
502 | static int idx; |
|
|
503 | |
|
|
504 | if (idx >= allarch.size ()) |
|
|
505 | if (idx) |
|
|
506 | idx = 0; |
|
|
507 | else |
497 | return 0; |
508 | return; |
498 | } |
|
|
499 | |
509 | |
|
|
510 | archetype *at = allarch [idx]; |
|
|
511 | |
|
|
512 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
|
|
513 | ++idx; |
|
|
514 | else |
|
|
515 | { |
|
|
516 | LOG (llevDebug, "garbage collect arch %s", &at->archname); |
|
|
517 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
|
|
518 | allarch.erase (idx); |
|
|
519 | |
|
|
520 | // break chain |
|
|
521 | for (object *op = at->head_ (); op; ) |
|
|
522 | { |
|
|
523 | object *next = op->more; |
|
|
524 | op->head = 0; |
|
|
525 | op->more = 0; |
|
|
526 | op = next; |
|
|
527 | } |
|
|
528 | |
|
|
529 | at->destroy (); |
|
|
530 | at->arch = 0; |
|
|
531 | } |
|
|
532 | } |
|
|
533 | while (--cnt); |
|
|
534 | } |
|
|
535 | |
|
|
536 | object * |
|
|
537 | archetype::instance () |
|
|
538 | { |
500 | object *op = at->clone (); |
539 | object *op = clone (); |
501 | op->arch = at; |
|
|
502 | op->instantiate (); |
540 | op->instantiate (); |
503 | |
|
|
504 | return op; |
541 | return op; |
505 | } |
|
|
506 | |
|
|
507 | object * |
|
|
508 | archetype::instance () |
|
|
509 | { |
|
|
510 | return arch_to_object (this); |
|
|
511 | } |
542 | } |
512 | |
543 | |
513 | /* |
544 | /* |
514 | * Creates an object. This function is called by get_archetype() |
545 | * Creates an object. This function is called by get_archetype() |
515 | * if it fails to find the appropriate archetype. |
546 | * if it fails to find the appropriate archetype. |
… | |
… | |
522 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
553 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
523 | |
554 | |
524 | if (!strcmp (name, "bug")) |
555 | if (!strcmp (name, "bug")) |
525 | abort (); |
556 | abort (); |
526 | |
557 | |
527 | char buf[MAX_BUF]; |
|
|
528 | sprintf (buf, "bug, please report (%s)", name); |
|
|
529 | |
|
|
530 | object *op = get_archetype ("bug"); |
558 | object *op = archetype::get (shstr_bug); |
531 | op->name = op->name_pl = buf; |
559 | op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name); |
532 | |
560 | |
533 | return op; |
561 | return op; |
534 | } |
562 | } |
535 | |
563 | |
536 | /* |
564 | /* |
… | |
… | |
538 | * object containing a copy of the archetype. |
566 | * object containing a copy of the archetype. |
539 | */ |
567 | */ |
540 | object * |
568 | object * |
541 | get_archetype (const char *name) |
569 | get_archetype (const char *name) |
542 | { |
570 | { |
|
|
571 | return archetype::get (name); |
|
|
572 | } |
|
|
573 | |
|
|
574 | object * |
|
|
575 | archetype::get (const char *name) |
|
|
576 | { |
543 | archetype *at = archetype::find (name); |
577 | archetype *at = find (name); |
544 | |
578 | |
545 | if (!at) |
579 | if (!at) |
546 | return create_singularity (name); |
580 | return create_singularity (name); |
547 | |
581 | |
548 | return arch_to_object (at); |
582 | return at->instance (); |
549 | } |
583 | } |
550 | |
584 | |
551 | /* |
585 | /* |
552 | * Returns the first archetype using the given type. |
586 | * Returns the first archetype using the given type. |
553 | * Used in treasure-generation. |
587 | * Used in treasure-generation. |
… | |
… | |
568 | * Used in treasure-generation. |
602 | * Used in treasure-generation. |
569 | */ |
603 | */ |
570 | object * |
604 | object * |
571 | clone_arch (int type) |
605 | clone_arch (int type) |
572 | { |
606 | { |
573 | archetype *at; |
607 | archetype *at = type_to_archetype (type); |
574 | |
608 | |
575 | if ((at = type_to_archetype (type)) == NULL) |
609 | if (!at) |
576 | { |
610 | { |
577 | LOG (llevError, "Can't clone archetype %d\n", type); |
611 | LOG (llevError, "Can't clone archetype %d\n", type); |
578 | return 0; |
612 | return 0; |
579 | } |
613 | } |
580 | |
614 | |
581 | object *op = at->clone (); |
615 | return at->instance (); |
582 | op->instantiate (); |
|
|
583 | return op; |
|
|
584 | } |
616 | } |
585 | |
617 | |
586 | /* |
618 | /* |
587 | * member: make instance from class |
619 | * member: make instance from class |
588 | */ |
620 | */ |
… | |
… | |
591 | { |
623 | { |
592 | object *op, *prev = 0, *head = 0; |
624 | object *op, *prev = 0, *head = 0; |
593 | |
625 | |
594 | while (at) |
626 | while (at) |
595 | { |
627 | { |
596 | op = arch_to_object (at); |
628 | op = at->instance (); |
597 | |
629 | |
598 | op->x = at->x; |
630 | op->x = at->x; |
599 | op->y = at->y; |
631 | op->y = at->y; |
600 | |
632 | |
601 | if (head) |
633 | if (head) |