1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
|
|
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
8 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the terms of the Affero GNU General Public License as published by the |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
12 | * option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * GNU General Public License for more details. |
17 | * |
18 | * |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * You should have received a copy of the Affero GNU General Public License |
19 | * and the GNU General Public License along with this program. If not, see |
20 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
21 | * <http://www.gnu.org/licenses/>. |
21 | * |
22 | * |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
24 | */ |
24 | |
25 | |
25 | #include <cassert> |
26 | #include <cassert> |
|
|
27 | #include <functional> |
|
|
28 | |
|
|
29 | #include <flat_hash_map.hpp> |
26 | |
30 | |
27 | #include <global.h> |
31 | #include <global.h> |
28 | |
|
|
29 | #include <tr1/functional> |
|
|
30 | #include <tr1/unordered_map> |
|
|
31 | |
32 | |
32 | archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
33 | archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
33 | arch_ptr archetype::empty; |
34 | arch_ptr archetype::empty; |
34 | |
35 | |
35 | // the hashtable |
36 | // the hashtable |
36 | typedef std::tr1::unordered_map |
37 | typedef ska::flat_hash_map< |
37 | < |
|
|
38 | const char *, |
38 | const char *, |
39 | arch_ptr, |
39 | arch_ptr, |
40 | str_hash, |
40 | str_hash, |
41 | str_equal, |
41 | str_equal, |
42 | slice_allocator< std::pair<const char *const, arch_ptr> > |
42 | slice_allocator< std::pair<const char *const, arch_ptr> > |
43 | > HT; |
43 | > HT; |
44 | |
44 | |
45 | static HT ht (10000); |
45 | static HT ht (10000); |
46 | archvec archetypes; |
46 | archvec archetypes; |
47 | static unordered_vector<archetype *> allarch; |
47 | static unordered_vector<archetype *> allarch; |
48 | static int dirtycnt; |
48 | static int dirtycnt; |
… | |
… | |
53 | static std::vector<archetype *> postponed_arch; |
53 | static std::vector<archetype *> postponed_arch; |
54 | |
54 | |
55 | //+GPL |
55 | //+GPL |
56 | |
56 | |
57 | /* |
57 | /* |
58 | * Creates an object. This function is called by get_archetype () |
58 | * Creates an object. This function is called by archetype::get () |
59 | * if it fails to find the appropriate archetype. |
59 | * if it fails to find the appropriate archetype. |
60 | * Thus get_archetype() will be guaranteed to always return |
60 | * Thus archetype::get() will be guaranteed to always return |
61 | * an object, and never NULL. |
61 | * an object, and never NULL. |
62 | */ |
62 | */ |
63 | static object * |
63 | static object * |
64 | create_singularity (const char *name) |
64 | create_singularity (const char *name) |
65 | { |
65 | { |
… | |
… | |
357 | } |
357 | } |
358 | |
358 | |
359 | void |
359 | void |
360 | archetype::post_load_check () |
360 | archetype::post_load_check () |
361 | { |
361 | { |
|
|
362 | assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL])); |
|
|
363 | |
|
|
364 | if (type == SKILL) |
|
|
365 | add_skill_archetype (this); |
|
|
366 | |
362 | object::post_load_check (); |
367 | object::post_load_check (); |
363 | |
|
|
364 | assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL])); |
|
|
365 | } |
368 | } |
366 | |
369 | |
367 | archetype * |
370 | archetype * |
368 | archetype::read (object_thawer &f) |
371 | archetype::read (object_thawer &f) |
369 | { |
372 | { |
… | |
… | |
420 | goto fail; |
423 | goto fail; |
421 | } |
424 | } |
422 | } |
425 | } |
423 | |
426 | |
424 | { |
427 | { |
425 | auto (at, parts.begin ()); |
|
|
426 | |
|
|
427 | archetype *new_head = parts.front (); |
428 | archetype *new_head = parts.front (); |
428 | archetype *old_head = find (new_head->archname); |
429 | archetype *old_head = find (new_head->archname); |
429 | |
430 | |
430 | if (old_head && !old_head->is_head ()) |
431 | if (old_head && !old_head->is_head ()) |
431 | { |
432 | { |
… | |
… | |
433 | &new_head->archname, &old_head->archname); |
434 | &new_head->archname, &old_head->archname); |
434 | goto fail; |
435 | goto fail; |
435 | } |
436 | } |
436 | |
437 | |
437 | // check that all archetypes belong to the same old object or are new |
438 | // check that all archetypes belong to the same old object or are new |
438 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
439 | for (auto &&at : parts) |
439 | { |
440 | { |
440 | archetype *new_part = *at; |
441 | archetype *new_part = at; |
441 | archetype *old_part = find (new_part->archname); |
442 | archetype *old_part = find (new_part->archname); |
442 | |
443 | |
443 | if (old_part && old_part->head_ () != old_head) |
444 | if (old_part && old_part->head_ () != old_head) |
444 | { |
445 | { |
445 | LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
446 | LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
… | |
… | |
450 | |
451 | |
451 | // assemble new chain |
452 | // assemble new chain |
452 | new_head->max_x = new_head->x; |
453 | new_head->max_x = new_head->x; |
453 | |
454 | |
454 | archetype *less = new_head; |
455 | archetype *less = new_head; |
455 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
456 | for (auto &&p = parts.begin () + 1; p != parts.end (); ++p) |
456 | { |
457 | { |
457 | archetype *at = *p; |
458 | archetype *at = *p; |
458 | |
459 | |
459 | // some flags get inherited from the head (probably a lot more) |
460 | // some flags get inherited from the head (probably a lot more) |
460 | // doing it here doesn't feel too cozy, but it allows code |
461 | // doing it here doesn't feel too cozy, but it allows code |
… | |
… | |
475 | |
476 | |
476 | return new_head; |
477 | return new_head; |
477 | } |
478 | } |
478 | |
479 | |
479 | fail: |
480 | fail: |
480 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
481 | for (auto &&at : parts) |
481 | (*p)->destroy (); |
482 | at->destroy (); |
482 | |
483 | |
483 | return 0; |
484 | return 0; |
484 | } |
485 | } |
485 | |
486 | |
486 | void |
487 | void |
… | |
… | |
492 | |
493 | |
493 | void |
494 | void |
494 | archetype::commit_load () |
495 | archetype::commit_load () |
495 | { |
496 | { |
496 | // unlink old archetypes and link in new ones */ |
497 | // unlink old archetypes and link in new ones */ |
497 | for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
498 | for (auto &&at : postponed_arch) |
498 | { |
499 | { |
499 | archetype *at = *p; |
|
|
500 | |
|
|
501 | if (archetype *old = find (at->archname)) |
500 | if (archetype *old = find (at->archname)) |
502 | old->unlink (); |
501 | old->unlink (); |
503 | |
502 | |
504 | allarch.push_back (at); |
503 | allarch.push_back (at); |
505 | |
504 | |
… | |
… | |
508 | } |
507 | } |
509 | |
508 | |
510 | postponed_arch.clear (); |
509 | postponed_arch.clear (); |
511 | |
510 | |
512 | // now resolve arch references |
511 | // now resolve arch references |
513 | for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
512 | for (auto &&p : postponed_arch_ref) // not yet C++17 |
514 | { |
513 | { |
515 | arch_ptr *ap = p->first; |
514 | arch_ptr *ap = p.first; |
516 | archetype *at = find (p->second); |
515 | archetype *at = find (p.second); |
517 | |
516 | |
518 | if (!at) |
517 | if (!at) |
519 | LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second); |
518 | LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p.second); |
520 | |
519 | |
521 | *ap = at; |
520 | *ap = at; |
522 | } |
521 | } |
523 | |
522 | |
524 | postponed_arch_ref.clear (); |
523 | postponed_arch_ref.clear (); |
… | |
… | |
527 | } |
526 | } |
528 | |
527 | |
529 | void |
528 | void |
530 | archetype::gc () |
529 | archetype::gc () |
531 | { |
530 | { |
532 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
531 | int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt)); |
533 | dirtycnt = max (0, dirtycnt - cnt); |
532 | dirtycnt = max (0, dirtycnt - cnt); |
534 | |
533 | |
535 | do |
534 | do |
536 | { |
535 | { |
537 | static int idx; |
536 | static int idx; |
… | |
… | |
546 | |
545 | |
547 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
546 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
548 | ++idx; |
547 | ++idx; |
549 | else |
548 | else |
550 | { |
549 | { |
551 | LOG (llevDebug, "garbage collect arch %s", &at->archname); |
550 | //LOG (llevDebug, "garbage collect arch %s", &at->archname); |
552 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
551 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
553 | allarch.erase (idx); |
552 | allarch.erase (idx); |
554 | |
553 | |
555 | // break chain |
554 | // break chain |
556 | for (object *op = at->head_ (); op; ) |
555 | for (object *op = at->head_ (); op; ) |
… | |
… | |
581 | /* |
580 | /* |
582 | * Finds which archetype matches the given name, and returns a new |
581 | * Finds which archetype matches the given name, and returns a new |
583 | * object containing a copy of the archetype. |
582 | * object containing a copy of the archetype. |
584 | */ |
583 | */ |
585 | object * |
584 | object * |
586 | get_archetype (const char *name) |
|
|
587 | { |
|
|
588 | return archetype::get (name); |
|
|
589 | } |
|
|
590 | |
|
|
591 | object * |
|
|
592 | archetype::get (const char *name) |
585 | archetype::get (const char *name) |
593 | { |
586 | { |
594 | archetype *at = find (name); |
587 | archetype *at = find (name); |
595 | |
588 | |
596 | if (!at) |
589 | if (!at) |