1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Crossfire TRT is free software: you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
… | |
… | |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * |
20 | * |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | #include <cassert> |
24 | #include <cassert> |
25 | |
25 | |
26 | #include <global.h> |
26 | #include <global.h> |
27 | #include <funcpoint.h> |
|
|
28 | #include <loader.h> |
27 | #include <loader.h> |
29 | |
28 | |
30 | #include <tr1/functional> |
29 | #include <tr1/functional> |
31 | #include <tr1/unordered_map> |
30 | #include <tr1/unordered_map> |
32 | |
31 | |
33 | /* The naming of these functions is really poor - they are all |
|
|
34 | * pretty much named '.._arch_...', but they may more may not |
|
|
35 | * return archetypes. Some make the arch_to_object call, and thus |
|
|
36 | * return an object. Perhaps those should be called 'archob' functions |
|
|
37 | * to denote they return an object derived from the archetype. |
|
|
38 | * MSW 2003-04-29 |
|
|
39 | */ |
|
|
40 | |
|
|
41 | archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing |
32 | archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
|
|
33 | arch_ptr archetype::empty; |
42 | |
34 | |
43 | // the hashtable |
35 | // the hashtable |
44 | typedef std::tr1::unordered_map |
36 | typedef std::tr1::unordered_map |
45 | < |
37 | < |
46 | const char *, |
38 | const char *, |
… | |
… | |
48 | str_hash, |
40 | str_hash, |
49 | str_equal, |
41 | str_equal, |
50 | slice_allocator< std::pair<const char *const, arch_ptr> > |
42 | slice_allocator< std::pair<const char *const, arch_ptr> > |
51 | > HT; |
43 | > HT; |
52 | |
44 | |
53 | static HT ht (5000); |
45 | static HT ht (10000); |
54 | archvec archetypes; |
46 | archvec archetypes; |
|
|
47 | static unordered_vector<archetype *> allarch; |
|
|
48 | static int dirtycnt; |
|
|
49 | |
|
|
50 | // the vector of other_arch references to be resolved |
|
|
51 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
|
|
52 | // the vector of loaded but not yet committed archetypes |
|
|
53 | static std::vector<archetype *> postponed_arch; |
55 | |
54 | |
56 | /** |
55 | /** |
57 | * GROS - This function retrieves an archetype given the name that appears |
56 | * GROS - This function retrieves an archetype given the name that appears |
58 | * during the game (for example, "writing pen" instead of "stylus"). |
57 | * during the game (for example, "writing pen" instead of "stylus"). |
59 | * It does not use the hashtable system, but browse the whole archlist each time. |
58 | * It does not use the hashtable system, but browse the whole archlist each time. |
… | |
… | |
103 | { |
102 | { |
104 | shstr_cmp skill_cmp (skill); |
103 | shstr_cmp skill_cmp (skill); |
105 | |
104 | |
106 | for_all_archetypes (at) |
105 | for_all_archetypes (at) |
107 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
106 | if (at->skill == skill_cmp && (type == -1 || type == at->type)) |
108 | return arch_to_object (at); |
107 | return at->instance (); |
109 | |
108 | |
110 | return 0; |
109 | return 0; |
111 | } |
110 | } |
112 | |
111 | |
113 | /* similiar to above - this returns the first archetype |
112 | /* similiar to above - this returns the first archetype |
… | |
… | |
134 | * - name: The name we're searching for (ex: "writing pen"); |
133 | * - name: The name we're searching for (ex: "writing pen"); |
135 | * Return value: |
134 | * Return value: |
136 | * - a corresponding object if found; a singularity object if not found. |
135 | * - a corresponding object if found; a singularity object if not found. |
137 | * Note by MSW - it appears that it takes the full name and keeps |
136 | * Note by MSW - it appears that it takes the full name and keeps |
138 | * shortening it until it finds a match. I re-wrote this so that it |
137 | * shortening it until it finds a match. I re-wrote this so that it |
139 | * doesn't malloc it each time - not that this function is used much, |
138 | * doesn't allocate it each time - not that this function is used much, |
140 | * but it otherwise had a big memory leak. |
139 | * but it otherwise had a big memory leak. |
141 | */ |
140 | */ |
142 | object * |
141 | object * |
143 | get_archetype_by_object_name (const char *name) |
142 | get_archetype_by_object_name (const char *name) |
144 | { |
143 | { |
… | |
… | |
150 | for (i = strlen (tmpname); i > 0; i--) |
149 | for (i = strlen (tmpname); i > 0; i--) |
151 | { |
150 | { |
152 | tmpname[i] = 0; |
151 | tmpname[i] = 0; |
153 | |
152 | |
154 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
153 | if (archetype *at = find_archetype_by_object_name (tmpname)) |
155 | return arch_to_object (at); |
154 | return at->instance (); |
156 | } |
155 | } |
157 | |
156 | |
158 | return create_singularity (name); |
157 | return create_singularity (name); |
159 | } |
158 | } |
160 | |
159 | |
… | |
… | |
276 | return 0; |
275 | return 0; |
277 | } |
276 | } |
278 | |
277 | |
279 | archetype::archetype (const char *name) |
278 | archetype::archetype (const char *name) |
280 | { |
279 | { |
281 | stub = true; |
|
|
282 | arch = this; |
280 | arch = this; |
283 | this->archname = this->name = this->name_pl = name; |
281 | this->archname = this->name = this->name_pl = name; |
284 | } |
282 | } |
285 | |
283 | |
286 | archetype::~archetype () |
284 | archetype::~archetype () |
… | |
… | |
289 | } |
287 | } |
290 | |
288 | |
291 | void |
289 | void |
292 | archetype::link () |
290 | archetype::link () |
293 | { |
291 | { |
294 | ht.insert (std::make_pair (archname, this)); |
|
|
295 | |
|
|
296 | if (!archetypes.contains (this)) |
292 | if (!archetypes.contains (this)) |
|
|
293 | { |
297 | archetypes.insert (this); |
294 | archetypes.insert (this); |
|
|
295 | ht.insert (std::make_pair (archname, this)); |
|
|
296 | } |
298 | } |
297 | } |
299 | |
298 | |
300 | void |
299 | void |
301 | archetype::unlink () |
300 | archetype::unlink () |
302 | { |
301 | { |
303 | ht.erase (archname); |
|
|
304 | |
|
|
305 | if (archetypes.contains (this)) |
302 | if (archetypes.contains (this)) |
|
|
303 | { |
306 | archetypes.erase (this); |
304 | archetypes.erase (this); |
|
|
305 | ht.erase (archname); |
|
|
306 | } |
307 | } |
307 | } |
308 | |
308 | |
309 | /* |
309 | /* |
310 | * Finds, using the hashtable, which archetype matches the given name. |
310 | * Finds, using the hashtable, which archetype matches the given name. |
311 | * returns a pointer to the found archetype, otherwise NULL. |
311 | * returns a pointer to the found archetype, otherwise NULL. |
… | |
… | |
323 | else |
323 | else |
324 | return i->second; |
324 | return i->second; |
325 | } |
325 | } |
326 | |
326 | |
327 | archetype * |
327 | archetype * |
328 | archetype::get (const char *name) |
|
|
329 | { |
|
|
330 | if (!name) |
|
|
331 | { |
|
|
332 | LOG (llevError, "null archetype requested\n"); |
|
|
333 | name = "(null)"; |
|
|
334 | } |
|
|
335 | |
|
|
336 | if (loading_arch && !strcmp (&loading_arch->archname, name)) |
|
|
337 | return loading_arch; |
|
|
338 | |
|
|
339 | archetype *at = find (name); |
|
|
340 | |
|
|
341 | if (!at) |
|
|
342 | { |
|
|
343 | at = new archetype (name); |
|
|
344 | at->link (); |
|
|
345 | } |
|
|
346 | |
|
|
347 | return at; |
|
|
348 | } |
|
|
349 | |
|
|
350 | archetype * |
|
|
351 | archetype::read (object_thawer &f) |
328 | archetype::read (object_thawer &f) |
352 | { |
329 | { |
353 | assert (f.kw == KW_object); |
330 | assert (f.kw == KW_object); |
354 | |
331 | |
355 | std::vector<archetype *> parts; |
332 | std::vector<archetype *> parts; |
356 | |
333 | |
357 | coroapi::cede_to_tick_every (10); |
334 | coroapi::cede_to_tick (); |
358 | |
335 | |
359 | for (;;) |
336 | for (;;) |
360 | { |
337 | { |
361 | // the archetype might have been referenced earlier |
|
|
362 | // so try to find an existing stub archetype first |
|
|
363 | archetype *at = find (f.get_str ()); |
338 | archetype *at = new archetype (f.get_str ()); |
364 | |
|
|
365 | if (!at || !at->stub) |
|
|
366 | at = new archetype (f.get_str ()); |
|
|
367 | |
339 | |
368 | f.next (); |
340 | f.next (); |
369 | |
341 | |
370 | #if 0 |
342 | #if 0 |
371 | // implementing it here in the server does neither allow multiple inheritence |
343 | // implementing it here in the server does neither allow multiple inheritence |
… | |
… | |
434 | &new_part->archname, &((archetype *)old_part->head_ ())->archname); |
406 | &new_part->archname, &((archetype *)old_part->head_ ())->archname); |
435 | goto fail; |
407 | goto fail; |
436 | } |
408 | } |
437 | } |
409 | } |
438 | |
410 | |
439 | // deactivate existing archetype |
|
|
440 | for (archetype *at = old_head; at; at = (archetype *)at->more) |
|
|
441 | at->unlink (); |
|
|
442 | |
|
|
443 | // assemble new chain |
411 | // assemble new chain |
444 | new_head->min_x = new_head->max_x = new_head->x; |
412 | new_head->min_x = new_head->max_x = new_head->x; |
445 | new_head->min_y = new_head->max_y = new_head->y; |
413 | new_head->min_y = new_head->max_y = new_head->y; |
446 | |
414 | |
447 | archetype *less = new_head; |
415 | archetype *less = new_head; |
448 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
416 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
449 | { |
417 | { |
450 | archetype *at = *p; |
418 | archetype *at = *p; |
451 | |
419 | |
452 | // some flags get inherited formt he head (probably a lot more) |
420 | // some flags get inherited from the head (probably a lot more) |
453 | // doing it here doesn't feel too cozy, but it allows code |
421 | // doing it here doesn't feel too cozy, but it allows code |
454 | // to ignore head checks for these flags, which saves time |
422 | // to ignore head checks for these flags, which saves time |
455 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
423 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
456 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
424 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
457 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
425 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
458 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
426 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
459 | |
427 | |
460 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
428 | new_head->min_x = min (new_head->min_x, at->x); |
461 | if (at->y < new_head->min_y) new_head->min_y = at->y; |
429 | new_head->min_y = min (new_head->min_y, at->y); |
462 | if (at->x > new_head->max_x) new_head->max_x = at->x; |
430 | new_head->max_x = max (new_head->max_x, at->x); |
463 | if (at->y > new_head->max_y) new_head->max_y = at->y; |
431 | new_head->max_y = max (new_head->max_y, at->y); |
464 | |
432 | |
465 | at->head = new_head; |
433 | at->head = new_head; |
466 | less->more = at; |
434 | less->more = at; |
467 | less = at; |
435 | less = at; |
468 | } |
436 | } |
469 | |
437 | |
470 | // now activate it |
438 | postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ()); |
471 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
|
|
472 | { |
|
|
473 | archetype *at = *p; |
|
|
474 | at->stub = false; |
|
|
475 | at->link (); |
|
|
476 | } |
|
|
477 | |
439 | |
478 | return new_head; |
440 | return new_head; |
479 | } |
441 | } |
480 | |
442 | |
481 | fail: |
443 | fail: |
… | |
… | |
483 | (*p)->destroy (true); |
445 | (*p)->destroy (true); |
484 | |
446 | |
485 | return 0; |
447 | return 0; |
486 | } |
448 | } |
487 | |
449 | |
488 | /* |
|
|
489 | * Initialize global archtype pointers: |
|
|
490 | */ |
|
|
491 | void |
450 | void |
492 | init_archetype_pointers () |
451 | archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch) |
493 | { |
452 | { |
494 | ring_arch = archetype::find ("ring"); |
453 | ref = 0; |
495 | amulet_arch = archetype::find ("amulet"); |
454 | postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch))); |
496 | staff_arch = archetype::find ("staff"); |
|
|
497 | crown_arch = archetype::find ("crown"); |
|
|
498 | empty_archetype = archetype::find ("empty_archetype"); |
|
|
499 | } |
455 | } |
500 | |
456 | |
501 | /* |
457 | void |
502 | * Creates and returns a new object which is a copy of the given archetype. |
458 | archetype::commit_load () |
503 | * This function returns NULL on failure. |
|
|
504 | */ |
|
|
505 | object * |
|
|
506 | arch_to_object (archetype *at) |
|
|
507 | { |
459 | { |
|
|
460 | // unlink old archetypes and link in new ones */ |
|
|
461 | for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p) |
|
|
462 | { |
|
|
463 | archetype *at = *p; |
|
|
464 | |
|
|
465 | if (archetype *old = find (at->archname)) |
|
|
466 | old->unlink (); |
|
|
467 | |
|
|
468 | allarch.push_back (at); |
|
|
469 | |
|
|
470 | at->link (); |
|
|
471 | ++dirtycnt; |
|
|
472 | } |
|
|
473 | |
|
|
474 | postponed_arch.clear (); |
|
|
475 | |
|
|
476 | // now resolve arch references |
|
|
477 | for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p) |
|
|
478 | { |
|
|
479 | arch_ptr *ap = p->first; |
|
|
480 | archetype *at = find (p->second); |
|
|
481 | |
508 | if (!at) |
482 | if (!at) |
|
|
483 | LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second); |
|
|
484 | |
|
|
485 | *ap = at; |
509 | { |
486 | } |
510 | LOG (llevError, "Couldn't find archetype.\n"); |
487 | |
|
|
488 | postponed_arch_ref.clear (); |
|
|
489 | |
|
|
490 | empty = find (shstr_empty_archetype); |
|
|
491 | } |
|
|
492 | |
|
|
493 | void |
|
|
494 | archetype::gc () |
|
|
495 | { |
|
|
496 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
|
|
497 | dirtycnt = max (0, dirtycnt - cnt); |
|
|
498 | |
|
|
499 | do |
|
|
500 | { |
|
|
501 | static int idx; |
|
|
502 | |
|
|
503 | if (idx >= allarch.size ()) |
|
|
504 | if (idx) |
|
|
505 | idx = 0; |
|
|
506 | else |
511 | return 0; |
507 | return; |
512 | } |
|
|
513 | |
508 | |
|
|
509 | archetype *at = allarch [idx]; |
|
|
510 | |
|
|
511 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
|
|
512 | ++idx; |
|
|
513 | else |
|
|
514 | { |
|
|
515 | LOG (llevDebug, "garbage collect arch %s", at->debug_desc ()); |
|
|
516 | assert (at->arch == at); // verify that refcnt == 1 is truly valid |
|
|
517 | allarch.erase (idx); |
|
|
518 | |
|
|
519 | // break chain |
|
|
520 | for (object *op = at->head_ (); op; ) |
|
|
521 | { |
|
|
522 | object *next = op->more; |
|
|
523 | op->head = 0; |
|
|
524 | op->more = 0; |
|
|
525 | op = next; |
|
|
526 | } |
|
|
527 | |
|
|
528 | at->destroy (true); |
|
|
529 | at->arch = 0; |
|
|
530 | } |
|
|
531 | } |
|
|
532 | while (--cnt); |
|
|
533 | } |
|
|
534 | |
|
|
535 | object * |
|
|
536 | archetype::instance () |
|
|
537 | { |
514 | object *op = at->clone (); |
538 | object *op = clone (); |
515 | op->arch = at; |
|
|
516 | op->instantiate (); |
539 | op->instantiate (); |
517 | |
|
|
518 | return op; |
540 | return op; |
519 | } |
|
|
520 | |
|
|
521 | object * |
|
|
522 | archetype::instance () |
|
|
523 | { |
|
|
524 | return arch_to_object (this); |
|
|
525 | } |
541 | } |
526 | |
542 | |
527 | /* |
543 | /* |
528 | * Creates an object. This function is called by get_archetype() |
544 | * Creates an object. This function is called by get_archetype() |
529 | * if it fails to find the appropriate archetype. |
545 | * if it fails to find the appropriate archetype. |
… | |
… | |
536 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
552 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
537 | |
553 | |
538 | if (!strcmp (name, "bug")) |
554 | if (!strcmp (name, "bug")) |
539 | abort (); |
555 | abort (); |
540 | |
556 | |
541 | char buf[MAX_BUF]; |
|
|
542 | sprintf (buf, "bug, please report (%s)", name); |
|
|
543 | |
|
|
544 | object *op = get_archetype ("bug"); |
557 | object *op = archetype::get (shstr_bug); |
545 | op->name = op->name_pl = buf; |
558 | op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name); |
546 | |
559 | |
547 | return op; |
560 | return op; |
548 | } |
561 | } |
549 | |
562 | |
550 | /* |
563 | /* |
… | |
… | |
552 | * object containing a copy of the archetype. |
565 | * object containing a copy of the archetype. |
553 | */ |
566 | */ |
554 | object * |
567 | object * |
555 | get_archetype (const char *name) |
568 | get_archetype (const char *name) |
556 | { |
569 | { |
|
|
570 | return archetype::get (name); |
|
|
571 | } |
|
|
572 | |
|
|
573 | object * |
|
|
574 | archetype::get (const char *name) |
|
|
575 | { |
557 | archetype *at = archetype::find (name); |
576 | archetype *at = find (name); |
558 | |
577 | |
559 | if (!at) |
578 | if (!at) |
560 | return create_singularity (name); |
579 | return create_singularity (name); |
561 | |
580 | |
562 | return arch_to_object (at); |
581 | return at->instance (); |
563 | } |
582 | } |
564 | |
583 | |
565 | /* |
584 | /* |
566 | * Returns the first archetype using the given type. |
585 | * Returns the first archetype using the given type. |
567 | * Used in treasure-generation. |
586 | * Used in treasure-generation. |
… | |
… | |
582 | * Used in treasure-generation. |
601 | * Used in treasure-generation. |
583 | */ |
602 | */ |
584 | object * |
603 | object * |
585 | clone_arch (int type) |
604 | clone_arch (int type) |
586 | { |
605 | { |
587 | archetype *at; |
606 | archetype *at = type_to_archetype (type); |
588 | |
607 | |
589 | if ((at = type_to_archetype (type)) == NULL) |
608 | if (!at) |
590 | { |
609 | { |
591 | LOG (llevError, "Can't clone archetype %d\n", type); |
610 | LOG (llevError, "Can't clone archetype %d\n", type); |
592 | return 0; |
611 | return 0; |
593 | } |
612 | } |
594 | |
613 | |
595 | object *op = at->clone (); |
614 | return at->instance (); |
596 | op->instantiate (); |
|
|
597 | return op; |
|
|
598 | } |
615 | } |
599 | |
616 | |
600 | /* |
617 | /* |
601 | * member: make instance from class |
618 | * member: make instance from class |
602 | */ |
619 | */ |
… | |
… | |
605 | { |
622 | { |
606 | object *op, *prev = 0, *head = 0; |
623 | object *op, *prev = 0, *head = 0; |
607 | |
624 | |
608 | while (at) |
625 | while (at) |
609 | { |
626 | { |
610 | op = arch_to_object (at); |
627 | op = at->instance (); |
611 | |
628 | |
612 | op->x = at->x; |
629 | op->x = at->x; |
613 | op->y = at->y; |
630 | op->y = at->y; |
614 | |
631 | |
615 | if (head) |
632 | if (head) |