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 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 | < |
38 | const char *, |
39 | const char *, |
39 | arch_ptr, |
40 | arch_ptr, |
40 | str_hash, |
41 | str_hash, |
41 | str_equal, |
42 | str_equal, |
… | |
… | |
53 | static std::vector<archetype *> postponed_arch; |
54 | static 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 | */ |
63 | static object * |
64 | static object * |
64 | create_singularity (const char *name) |
65 | create_singularity (const char *name) |
65 | { |
66 | { |
… | |
… | |
212 | /* All is a very generic match - low match value */ |
213 | /* All is a very generic match - low match value */ |
213 | if (!strcmp (cp, "all")) |
214 | if (!strcmp (cp, "all")) |
214 | return 1; |
215 | return 1; |
215 | |
216 | |
216 | /* unpaid is a little more specific */ |
217 | /* unpaid is a little more specific */ |
217 | if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) |
218 | if (!strcmp (cp, "unpaid") && op->flag [FLAG_UNPAID]) |
218 | return 2; |
219 | return 2; |
219 | |
220 | |
220 | if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) |
221 | if (!strcmp (cp, "cursed") && op->flag [FLAG_KNOWN_CURSED] && (op->flag [FLAG_CURSED] || op->flag [FLAG_DAMNED])) |
221 | return 2; |
222 | return 2; |
222 | |
223 | |
223 | if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) |
224 | if (!strcmp (cp, "unlocked") && !op->flag [FLAG_INV_LOCKED]) |
224 | return 2; |
225 | return 2; |
225 | |
226 | |
226 | /* Allow for things like '100 arrows' */ |
227 | /* Allow for things like '100 arrows' */ |
227 | if ((count = atoi (cp)) != 0) |
228 | if ((count = atoi (cp)) != 0) |
228 | { |
229 | { |
… | |
… | |
299 | return 0; |
300 | return 0; |
300 | } |
301 | } |
301 | |
302 | |
302 | //-GPL |
303 | //-GPL |
303 | |
304 | |
|
|
305 | void |
|
|
306 | archetype::do_delete () |
|
|
307 | { |
|
|
308 | delete this; |
|
|
309 | } |
|
|
310 | |
304 | archetype::archetype (const char *name) |
311 | archetype::archetype (const char *name) |
305 | { |
312 | { |
306 | arch = this; |
313 | arch = this; |
307 | this->archname = this->name = this->name_pl = name; |
314 | this->archname = this->name = this->name_pl = name; |
308 | } |
315 | } |
… | |
… | |
351 | } |
358 | } |
352 | |
359 | |
353 | void |
360 | void |
354 | archetype::post_load_check () |
361 | archetype::post_load_check () |
355 | { |
362 | { |
|
|
363 | assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL])); |
|
|
364 | |
|
|
365 | if (type == SKILL) |
|
|
366 | add_skill_archetype (this); |
|
|
367 | |
356 | object::post_load_check (); |
368 | object::post_load_check (); |
357 | |
|
|
358 | assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL])); |
|
|
359 | } |
369 | } |
360 | |
370 | |
361 | archetype * |
371 | archetype * |
362 | archetype::read (object_thawer &f) |
372 | archetype::read (object_thawer &f) |
363 | { |
373 | { |
… | |
… | |
374 | f.next (); |
384 | f.next (); |
375 | |
385 | |
376 | #if 0 |
386 | #if 0 |
377 | // implementing it here in the server does neither allow multiple inheritence |
387 | // implementing it here in the server does neither allow multiple inheritence |
378 | // nor does it cleanly "just override". it would allow use in map files, though, |
388 | // nor does it cleanly "just override". it would allow use in map files, though, |
379 | // and other resource files dynamically laoded (as opposed to being preprocessed). |
389 | // and other resource files dynamically loaded (as opposed to being preprocessed). |
380 | // not that any of this is relevant as of yet... |
390 | // not that any of this is relevant as of yet... |
381 | if (f.kw == KW_inherit) |
391 | if (f.kw == KW_inherit) |
382 | { |
392 | { |
383 | if (archetype *at = find (f.get_str ())) |
393 | if (archetype *at = find (f.get_str ())) |
384 | *op = at->clone; |
394 | *op = at->clone; |
… | |
… | |
414 | goto fail; |
424 | goto fail; |
415 | } |
425 | } |
416 | } |
426 | } |
417 | |
427 | |
418 | { |
428 | { |
419 | auto (at, parts.begin ()); |
|
|
420 | |
|
|
421 | archetype *new_head = parts.front (); |
429 | archetype *new_head = parts.front (); |
422 | archetype *old_head = find (new_head->archname); |
430 | archetype *old_head = find (new_head->archname); |
423 | |
431 | |
424 | if (old_head && !old_head->is_head ()) |
432 | if (old_head && !old_head->is_head ()) |
425 | { |
433 | { |
… | |
… | |
427 | &new_head->archname, &old_head->archname); |
435 | &new_head->archname, &old_head->archname); |
428 | goto fail; |
436 | goto fail; |
429 | } |
437 | } |
430 | |
438 | |
431 | // 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 |
432 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
440 | for (auto &&at : parts) |
433 | { |
441 | { |
434 | archetype *new_part = *at; |
442 | archetype *new_part = at; |
435 | archetype *old_part = find (new_part->archname); |
443 | archetype *old_part = find (new_part->archname); |
436 | |
444 | |
437 | if (old_part && old_part->head_ () != old_head) |
445 | if (old_part && old_part->head_ () != old_head) |
438 | { |
446 | { |
439 | 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", |
… | |
… | |
444 | |
452 | |
445 | // assemble new chain |
453 | // assemble new chain |
446 | new_head->max_x = new_head->x; |
454 | new_head->max_x = new_head->x; |
447 | |
455 | |
448 | archetype *less = new_head; |
456 | archetype *less = new_head; |
449 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
457 | for (auto &&p = parts.begin () + 1; p != parts.end (); ++p) |
450 | { |
458 | { |
451 | archetype *at = *p; |
459 | archetype *at = *p; |
452 | |
460 | |
453 | // some flags get inherited from the head (probably a lot more) |
461 | // some flags get inherited from the head (probably a lot more) |
454 | // 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 |
… | |
… | |
469 | |
477 | |
470 | return new_head; |
478 | return new_head; |
471 | } |
479 | } |
472 | |
480 | |
473 | fail: |
481 | fail: |
474 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
482 | for (auto &&at : parts) |
475 | (*p)->destroy (); |
483 | at->destroy (); |
476 | |
484 | |
477 | return 0; |
485 | return 0; |
478 | } |
486 | } |
479 | |
487 | |
480 | void |
488 | void |
… | |
… | |
486 | |
494 | |
487 | void |
495 | void |
488 | archetype::commit_load () |
496 | archetype::commit_load () |
489 | { |
497 | { |
490 | // unlink old archetypes and link in new ones */ |
498 | // unlink old archetypes and link in new ones */ |
491 | for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
499 | for (auto &&at : postponed_arch) |
492 | { |
500 | { |
493 | archetype *at = *p; |
|
|
494 | |
|
|
495 | if (archetype *old = find (at->archname)) |
501 | if (archetype *old = find (at->archname)) |
496 | old->unlink (); |
502 | old->unlink (); |
497 | |
503 | |
498 | allarch.push_back (at); |
504 | allarch.push_back (at); |
499 | |
505 | |
… | |
… | |
502 | } |
508 | } |
503 | |
509 | |
504 | postponed_arch.clear (); |
510 | postponed_arch.clear (); |
505 | |
511 | |
506 | // now resolve arch references |
512 | // now resolve arch references |
507 | for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
513 | for (auto &&p : postponed_arch_ref) // not yet C++17 |
508 | { |
514 | { |
509 | arch_ptr *ap = p->first; |
515 | arch_ptr *ap = p.first; |
510 | archetype *at = find (p->second); |
516 | archetype *at = find (p.second); |
511 | |
517 | |
512 | if (!at) |
518 | if (!at) |
513 | 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); |
514 | |
520 | |
515 | *ap = at; |
521 | *ap = at; |
516 | } |
522 | } |
517 | |
523 | |
518 | postponed_arch_ref.clear (); |
524 | postponed_arch_ref.clear (); |
… | |
… | |
521 | } |
527 | } |
522 | |
528 | |
523 | void |
529 | void |
524 | archetype::gc () |
530 | archetype::gc () |
525 | { |
531 | { |
526 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
532 | int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt)); |
527 | dirtycnt = max (0, dirtycnt - cnt); |
533 | dirtycnt = max (0, dirtycnt - cnt); |
528 | |
534 | |
529 | do |
535 | do |
530 | { |
536 | { |
531 | static int idx; |
537 | static int idx; |
… | |
… | |
540 | |
546 | |
541 | 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 |
542 | ++idx; |
548 | ++idx; |
543 | else |
549 | else |
544 | { |
550 | { |
545 | LOG (llevDebug, "garbage collect arch %s", &at->archname); |
551 | //LOG (llevDebug, "garbage collect arch %s", &at->archname); |
546 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
552 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
547 | allarch.erase (idx); |
553 | allarch.erase (idx); |
548 | |
554 | |
549 | // break chain |
555 | // break chain |
550 | for (object *op = at->head_ (); op; ) |
556 | for (object *op = at->head_ (); op; ) |
… | |
… | |
575 | /* |
581 | /* |
576 | * Finds which archetype matches the given name, and returns a new |
582 | * Finds which archetype matches the given name, and returns a new |
577 | * object containing a copy of the archetype. |
583 | * object containing a copy of the archetype. |
578 | */ |
584 | */ |
579 | object * |
585 | object * |
580 | get_archetype (const char *name) |
|
|
581 | { |
|
|
582 | return archetype::get (name); |
|
|
583 | } |
|
|
584 | |
|
|
585 | object * |
|
|
586 | archetype::get (const char *name) |
586 | archetype::get (const char *name) |
587 | { |
587 | { |
588 | archetype *at = find (name); |
588 | archetype *at = find (name); |
589 | |
589 | |
590 | if (!at) |
590 | if (!at) |