ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
(Generate patch)

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.105 by root, Thu Apr 15 00:36:51 2010 UTC vs.
Revision 1.118 by root, Tue Nov 27 18:50:22 2018 UTC

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
32archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing 33archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
33arch_ptr archetype::empty; 34arch_ptr archetype::empty;
34 35
35// the hashtable 36// the hashtable
36typedef std::tr1::unordered_map 37typedef ska::flat_hash_map
37 < 38 <
38 const char *, 39 const char *,
39 arch_ptr, 40 arch_ptr,
40 str_hash, 41 str_hash,
41 str_equal, 42 str_equal,
53static std::vector<archetype *> postponed_arch; 54static std::vector<archetype *> postponed_arch;
54 55
55//+GPL 56//+GPL
56 57
57/* 58/*
58 * Creates an object. This function is called by get_archetype () 59 * Creates an object. This function is called by archetype::get ()
59 * if it fails to find the appropriate archetype. 60 * if it fails to find the appropriate archetype.
60 * Thus get_archetype() will be guaranteed to always return 61 * Thus archetype::get() will be guaranteed to always return
61 * an object, and never NULL. 62 * an object, and never NULL.
62 */ 63 */
63static object * 64static object *
64create_singularity (const char *name) 65create_singularity (const char *name)
65{ 66{
423 goto fail; 424 goto fail;
424 } 425 }
425 } 426 }
426 427
427 { 428 {
428 auto (at, parts.begin ());
429
430 archetype *new_head = parts.front (); 429 archetype *new_head = parts.front ();
431 archetype *old_head = find (new_head->archname); 430 archetype *old_head = find (new_head->archname);
432 431
433 if (old_head && !old_head->is_head ()) 432 if (old_head && !old_head->is_head ())
434 { 433 {
436 &new_head->archname, &old_head->archname); 435 &new_head->archname, &old_head->archname);
437 goto fail; 436 goto fail;
438 } 437 }
439 438
440 // check that all archetypes belong to the same old object or are new 439 // check that all archetypes belong to the same old object or are new
441 for (auto (at, parts.begin ()); at != parts.end (); ++at) 440 for (auto &&at : parts)
442 { 441 {
443 archetype *new_part = *at; 442 archetype *new_part = at;
444 archetype *old_part = find (new_part->archname); 443 archetype *old_part = find (new_part->archname);
445 444
446 if (old_part && old_part->head_ () != old_head) 445 if (old_part && old_part->head_ () != old_head)
447 { 446 {
448 LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", 447 LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n",
453 452
454 // assemble new chain 453 // assemble new chain
455 new_head->max_x = new_head->x; 454 new_head->max_x = new_head->x;
456 455
457 archetype *less = new_head; 456 archetype *less = new_head;
458 for (auto (p, parts.begin () + 1); p != parts.end (); ++p) 457 for (auto &&p = parts.begin () + 1; p != parts.end (); ++p)
459 { 458 {
460 archetype *at = *p; 459 archetype *at = *p;
461 460
462 // some flags get inherited from the head (probably a lot more) 461 // some flags get inherited from the head (probably a lot more)
463 // doing it here doesn't feel too cozy, but it allows code 462 // doing it here doesn't feel too cozy, but it allows code
478 477
479 return new_head; 478 return new_head;
480 } 479 }
481 480
482fail: 481fail:
483 for (auto (p, parts.begin ()); p != parts.end (); ++p) 482 for (auto &&at : parts)
484 (*p)->destroy (); 483 at->destroy ();
485 484
486 return 0; 485 return 0;
487} 486}
488 487
489void 488void
495 494
496void 495void
497archetype::commit_load () 496archetype::commit_load ()
498{ 497{
499 // unlink old archetypes and link in new ones */ 498 // unlink old archetypes and link in new ones */
500 for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) 499 for (auto &&at : postponed_arch)
501 { 500 {
502 archetype *at = *p;
503
504 if (archetype *old = find (at->archname)) 501 if (archetype *old = find (at->archname))
505 old->unlink (); 502 old->unlink ();
506 503
507 allarch.push_back (at); 504 allarch.push_back (at);
508 505
511 } 508 }
512 509
513 postponed_arch.clear (); 510 postponed_arch.clear ();
514 511
515 // now resolve arch references 512 // now resolve arch references
516 for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) 513 for (auto &&p : postponed_arch_ref) // not yet C++17
517 { 514 {
518 arch_ptr *ap = p->first; 515 arch_ptr *ap = p.first;
519 archetype *at = find (p->second); 516 archetype *at = find (p.second);
520 517
521 if (!at) 518 if (!at)
522 LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second); 519 LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p.second);
523 520
524 *ap = at; 521 *ap = at;
525 } 522 }
526 523
527 postponed_arch_ref.clear (); 524 postponed_arch_ref.clear ();
530} 527}
531 528
532void 529void
533archetype::gc () 530archetype::gc ()
534{ 531{
535 int cnt = max (1, min (allarch.size () / 128, dirtycnt)); 532 int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt));
536 dirtycnt = max (0, dirtycnt - cnt); 533 dirtycnt = max (0, dirtycnt - cnt);
537 534
538 do 535 do
539 { 536 {
540 static int idx; 537 static int idx;
549 546
550 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object 547 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
551 ++idx; 548 ++idx;
552 else 549 else
553 { 550 {
554 LOG (llevDebug, "garbage collect arch %s", &at->archname); 551 //LOG (llevDebug, "garbage collect arch %s", &at->archname);
555 assert (at->arch == at); // verify that refcnt == 1 is truly valid 552 assert (at->arch == at); // verify that refcnt == 1 is truly valid
556 allarch.erase (idx); 553 allarch.erase (idx);
557 554
558 // break chain 555 // break chain
559 for (object *op = at->head_ (); op; ) 556 for (object *op = at->head_ (); op; )
584/* 581/*
585 * Finds which archetype matches the given name, and returns a new 582 * Finds which archetype matches the given name, and returns a new
586 * object containing a copy of the archetype. 583 * object containing a copy of the archetype.
587 */ 584 */
588object * 585object *
589get_archetype (const char *name)
590{
591 return archetype::get (name);
592}
593
594object *
595archetype::get (const char *name) 586archetype::get (const char *name)
596{ 587{
597 archetype *at = find (name); 588 archetype *at = find (name);
598 589
599 if (!at) 590 if (!at)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines