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,2011 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; |
… | |
… | |
423 | goto fail; |
423 | goto fail; |
424 | } |
424 | } |
425 | } |
425 | } |
426 | |
426 | |
427 | { |
427 | { |
428 | auto (at, parts.begin ()); |
|
|
429 | |
|
|
430 | archetype *new_head = parts.front (); |
428 | archetype *new_head = parts.front (); |
431 | archetype *old_head = find (new_head->archname); |
429 | archetype *old_head = find (new_head->archname); |
432 | |
430 | |
433 | if (old_head && !old_head->is_head ()) |
431 | if (old_head && !old_head->is_head ()) |
434 | { |
432 | { |
… | |
… | |
436 | &new_head->archname, &old_head->archname); |
434 | &new_head->archname, &old_head->archname); |
437 | goto fail; |
435 | goto fail; |
438 | } |
436 | } |
439 | |
437 | |
440 | // 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 |
441 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
439 | for (auto &&at : parts) |
442 | { |
440 | { |
443 | archetype *new_part = *at; |
441 | archetype *new_part = at; |
444 | archetype *old_part = find (new_part->archname); |
442 | archetype *old_part = find (new_part->archname); |
445 | |
443 | |
446 | if (old_part && old_part->head_ () != old_head) |
444 | if (old_part && old_part->head_ () != old_head) |
447 | { |
445 | { |
448 | 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", |
… | |
… | |
453 | |
451 | |
454 | // assemble new chain |
452 | // assemble new chain |
455 | new_head->max_x = new_head->x; |
453 | new_head->max_x = new_head->x; |
456 | |
454 | |
457 | archetype *less = new_head; |
455 | archetype *less = new_head; |
458 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
456 | for (auto &&p = parts.begin () + 1; p != parts.end (); ++p) |
459 | { |
457 | { |
460 | archetype *at = *p; |
458 | archetype *at = *p; |
461 | |
459 | |
462 | // some flags get inherited from the head (probably a lot more) |
460 | // some flags get inherited from the head (probably a lot more) |
463 | // 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 |
… | |
… | |
478 | |
476 | |
479 | return new_head; |
477 | return new_head; |
480 | } |
478 | } |
481 | |
479 | |
482 | fail: |
480 | fail: |
483 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
481 | for (auto &&at : parts) |
484 | (*p)->destroy (); |
482 | at->destroy (); |
485 | |
483 | |
486 | return 0; |
484 | return 0; |
487 | } |
485 | } |
488 | |
486 | |
489 | void |
487 | void |
… | |
… | |
495 | |
493 | |
496 | void |
494 | void |
497 | archetype::commit_load () |
495 | archetype::commit_load () |
498 | { |
496 | { |
499 | // unlink old archetypes and link in new ones */ |
497 | // unlink old archetypes and link in new ones */ |
500 | for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
498 | for (auto &&at : postponed_arch) |
501 | { |
499 | { |
502 | archetype *at = *p; |
|
|
503 | |
|
|
504 | if (archetype *old = find (at->archname)) |
500 | if (archetype *old = find (at->archname)) |
505 | old->unlink (); |
501 | old->unlink (); |
506 | |
502 | |
507 | allarch.push_back (at); |
503 | allarch.push_back (at); |
508 | |
504 | |
… | |
… | |
511 | } |
507 | } |
512 | |
508 | |
513 | postponed_arch.clear (); |
509 | postponed_arch.clear (); |
514 | |
510 | |
515 | // now resolve arch references |
511 | // now resolve arch references |
516 | for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
512 | for (auto &&p : postponed_arch_ref) // not yet C++17 |
517 | { |
513 | { |
518 | arch_ptr *ap = p->first; |
514 | arch_ptr *ap = p.first; |
519 | archetype *at = find (p->second); |
515 | archetype *at = find (p.second); |
520 | |
516 | |
521 | if (!at) |
517 | if (!at) |
522 | 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); |
523 | |
519 | |
524 | *ap = at; |
520 | *ap = at; |
525 | } |
521 | } |
526 | |
522 | |
527 | postponed_arch_ref.clear (); |
523 | postponed_arch_ref.clear (); |
… | |
… | |
530 | } |
526 | } |
531 | |
527 | |
532 | void |
528 | void |
533 | archetype::gc () |
529 | archetype::gc () |
534 | { |
530 | { |
535 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
531 | int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt)); |
536 | dirtycnt = max (0, dirtycnt - cnt); |
532 | dirtycnt = max (0, dirtycnt - cnt); |
537 | |
533 | |
538 | do |
534 | do |
539 | { |
535 | { |
540 | static int idx; |
536 | static int idx; |