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