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