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.73 by root, Sun Apr 20 00:44:12 2008 UTC vs.
Revision 1.115 by root, Sat Nov 17 23:33:17 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 (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * 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 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24#include <cassert> 25#include <cassert>
26#include <functional>
27
28#include <flat_hash_map.hpp>
25 29
26#include <global.h> 30#include <global.h>
27#include <funcpoint.h>
28#include <loader.h>
29 31
30#include <tr1/functional>
31#include <tr1/unordered_map>
32
33archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing 32archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
34archetype *archetype::empty; 33arch_ptr archetype::empty;
35 34
36// the hashtable 35// the hashtable
37typedef std::tr1::unordered_map 36typedef ska::flat_hash_map<
38 <
39 const char *, 37 const char *,
40 arch_ptr, 38 arch_ptr,
41 str_hash, 39 str_hash,
42 str_equal, 40 str_equal,
43 slice_allocator< std::pair<const char *const, arch_ptr> > 41 slice_allocator< std::pair<const char *const, arch_ptr> >
44 > HT; 42> HT;
45 43
46static HT ht (5000); 44static HT ht (10000);
47archvec archetypes; 45archvec archetypes;
46static unordered_vector<archetype *> allarch;
47static int dirtycnt;
48 48
49// the vector of other_arch references to be resolved 49// the vector of other_arch references to be resolved
50static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; 50static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref;
51// the vector of loaded but not yet committed archetypes 51// the vector of loaded but not yet committed archetypes
52static std::vector<archetype *> postponed_arch; 52static std::vector<archetype *> postponed_arch;
53
54//+GPL
55
56/*
57 * Creates an object. This function is called by archetype::get ()
58 * if it fails to find the appropriate archetype.
59 * Thus archetype::get() will be guaranteed to always return
60 * an object, and never NULL.
61 */
62static object *
63create_singularity (const char *name)
64{
65 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
66
67 if (!strcmp (name, "bug"))
68 abort ();
69
70 object *op = archetype::get (shstr_bug);
71 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
72
73 return op;
74}
53 75
54/** 76/**
55 * GROS - This function retrieves an archetype given the name that appears 77 * GROS - This function retrieves an archetype given the name that appears
56 * during the game (for example, "writing pen" instead of "stylus"). 78 * during the game (for example, "writing pen" instead of "stylus").
57 * It does not use the hashtable system, but browse the whole archlist each time. 79 * It does not use the hashtable system, but browse the whole archlist each time.
101{ 123{
102 shstr_cmp skill_cmp (skill); 124 shstr_cmp skill_cmp (skill);
103 125
104 for_all_archetypes (at) 126 for_all_archetypes (at)
105 if (at->skill == skill_cmp && (type == -1 || type == at->type)) 127 if (at->skill == skill_cmp && (type == -1 || type == at->type))
106 return arch_to_object (at); 128 return at->instance ();
107 129
108 return 0; 130 return 0;
109} 131}
110 132
111/* similiar to above - this returns the first archetype 133/* similiar to above - this returns the first archetype
132 * - name: The name we're searching for (ex: "writing pen"); 154 * - name: The name we're searching for (ex: "writing pen");
133 * Return value: 155 * Return value:
134 * - a corresponding object if found; a singularity object if not found. 156 * - a corresponding object if found; a singularity object if not found.
135 * Note by MSW - it appears that it takes the full name and keeps 157 * Note by MSW - it appears that it takes the full name and keeps
136 * shortening it until it finds a match. I re-wrote this so that it 158 * shortening it until it finds a match. I re-wrote this so that it
137 * doesn't malloc it each time - not that this function is used much, 159 * doesn't allocate it each time - not that this function is used much,
138 * but it otherwise had a big memory leak. 160 * but it otherwise had a big memory leak.
139 */ 161 */
140object * 162object *
141get_archetype_by_object_name (const char *name) 163get_archetype_by_object_name (const char *name)
142{ 164{
143 char tmpname[MAX_BUF]; 165 char tmpname[MAX_BUF];
144 int i;
145 166
146 assign (tmpname, name); 167 assign (tmpname, name);
147 168
148 for (i = strlen (tmpname); i > 0; i--) 169 for (int i = strlen (tmpname); i > 0; i--)
149 { 170 {
150 tmpname[i] = 0; 171 tmpname[i] = 0;
151 172
152 if (archetype *at = find_archetype_by_object_name (tmpname)) 173 if (archetype *at = find_archetype_by_object_name (tmpname))
153 return arch_to_object (at); 174 return at->instance ();
154 } 175 }
155 176
156 return create_singularity (name); 177 return create_singularity (name);
157} 178}
158 179
190 /* All is a very generic match - low match value */ 211 /* All is a very generic match - low match value */
191 if (!strcmp (cp, "all")) 212 if (!strcmp (cp, "all"))
192 return 1; 213 return 1;
193 214
194 /* unpaid is a little more specific */ 215 /* unpaid is a little more specific */
195 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) 216 if (!strcmp (cp, "unpaid") && op->flag [FLAG_UNPAID])
196 return 2; 217 return 2;
197 if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) 218
219 if (!strcmp (cp, "cursed") && op->flag [FLAG_KNOWN_CURSED] && (op->flag [FLAG_CURSED] || op->flag [FLAG_DAMNED]))
198 return 2; 220 return 2;
199 221
200 if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) 222 if (!strcmp (cp, "unlocked") && !op->flag [FLAG_INV_LOCKED])
201 return 2; 223 return 2;
202 224
203 /* Allow for things like '100 arrows' */ 225 /* Allow for things like '100 arrows' */
204 if ((count = atoi (cp)) != 0) 226 if ((count = atoi (cp)) != 0)
205 { 227 {
223 * is because of the 'else' handling - we don't want to match on 245 * is because of the 'else' handling - we don't want to match on
224 * something and set a low retval, even though it may match a higher retcal 246 * something and set a low retval, even though it may match a higher retcal
225 * later. So keep it in descending order here, so we try for the best 247 * later. So keep it in descending order here, so we try for the best
226 * match first, and work downward. 248 * match first, and work downward.
227 */ 249 */
250 const char *qbn0, *qbn1, *qsn; // query base name/short name caches
251
228 if (!strcasecmp (cp, query_name (op))) 252 if (!strcasecmp (cp, query_name (op)))
229 retval = 20; 253 retval = 20;
230 else if (!strcasecmp (cp, query_short_name (op))) 254 else if (!strcasecmp (cp, qsn = query_short_name (op)))
231 retval = 18; 255 retval = 18;
232 else if (!strcasecmp (cp, query_base_name (op, 0))) 256 else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0)))
233 retval = 16; 257 retval = 16;
234 else if (!strcasecmp (cp, query_base_name (op, 1))) 258 else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1)))
235 retval = 16; 259 retval = 16;
236 else if (op->custom_name && !strcasecmp (cp, op->custom_name)) 260 else if (op->custom_name && !strcasecmp (cp, op->custom_name))
237 retval = 15; 261 retval = 15;
238 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) 262 else if (!strncasecmp (cp, qbn0, strlen (cp)))
239 retval = 14; 263 retval = 14;
240 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) 264 else if (!strncasecmp (cp, qbn1, strlen (cp)))
241 retval = 14; 265 retval = 14;
242 /* Do substring checks, so things like 'Str+1' will match. 266 /* Do substring checks, so things like 'Str+1' will match.
243 * retval of these should perhaps be lower - they are lower 267 * retval of these should perhaps be lower - they are lower
244 * then the specific strcasecmp aboves, but still higher than 268 * then the specific strcasecmp aboves, but still higher than
245 * some other match criteria. 269 * some other match criteria.
246 */ 270 */
247 else if (strstr (query_base_name (op, 1), cp)) 271 else if (strstr (qbn1, cp))
248 retval = 12; 272 retval = 12;
249 else if (strstr (query_base_name (op, 0), cp)) 273 else if (strstr (qbn0, cp))
250 retval = 12; 274 retval = 12;
251 else if (strstr (query_short_name (op), cp)) 275 else if (strstr (qsn, cp))
252 retval = 12; 276 retval = 12;
253 /* Check against plural/non plural based on count. */ 277 /* Check against plural/non plural based on count. */
254 else if (count > 1 && !strcasecmp (cp, op->name_pl)) 278 else if (count > 1 && !strcasecmp (cp, op->name_pl))
255 retval = 6; 279 retval = 6;
256 else if (count == 1 && !strcasecmp (op->name, cp)) 280 else if (count == 1 && !strcasecmp (op->name, cp))
257 retval = 6; 281 retval = 6;
258 /* base name matched - not bad */ 282 /* base name matched - not bad */
259 else if (strcasecmp (cp, op->name) == 0 && !count) 283 else if (strcasecmp (cp, op->name) == 0 && !count)
260 retval = 4; 284 retval = 4;
261 /* Check for partial custom name, but give a real low priority */ 285 /* Check for partial custom name, but give a real low priority */
262 else if (op->custom_name && strstr (op->custom_name, cp)) 286 else if (op->custom_name.contains (cp))
263 retval = 3; 287 retval = 3;
264 288
265 if (retval) 289 if (retval)
266 { 290 {
267 if (pl->type == PLAYER) 291 if (pl->type == PLAYER)
272 } 296 }
273 297
274 return 0; 298 return 0;
275} 299}
276 300
301//-GPL
302
303void
304archetype::do_delete ()
305{
306 delete this;
307}
308
277archetype::archetype (const char *name) 309archetype::archetype (const char *name)
278{ 310{
279 arch = this; 311 arch = this;
280 this->archname = this->name = this->name_pl = name; 312 this->archname = this->name = this->name_pl = name;
281} 313}
286} 318}
287 319
288void 320void
289archetype::link () 321archetype::link ()
290{ 322{
291 ht.insert (std::make_pair (archname, this));
292
293 if (!archetypes.contains (this)) 323 if (!archetypes.contains (this))
324 {
294 archetypes.insert (this); 325 archetypes.insert (this);
326 ht.insert (std::make_pair (archname, this));
327 }
295} 328}
296 329
297void 330void
298archetype::unlink () 331archetype::unlink ()
299{ 332{
300 ht.erase (archname);
301
302 if (archetypes.contains (this)) 333 if (archetypes.contains (this))
334 {
303 archetypes.erase (this); 335 archetypes.erase (this);
336 ht.erase (archname);
337 }
304} 338}
305 339
306/* 340/*
307 * Finds, using the hashtable, which archetype matches the given name. 341 * Finds, using the hashtable, which archetype matches the given name.
308 * returns a pointer to the found archetype, otherwise NULL. 342 * returns a pointer to the found archetype, otherwise NULL.
319 return 0; 353 return 0;
320 else 354 else
321 return i->second; 355 return i->second;
322} 356}
323 357
358void
359archetype::post_load_check ()
360{
361 assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL]));
362
363 if (type == SKILL)
364 add_skill_archetype (this);
365
366 object::post_load_check ();
367}
368
324archetype * 369archetype *
325archetype::read (object_thawer &f) 370archetype::read (object_thawer &f)
326{ 371{
327 assert (f.kw == KW_object); 372 assert (f.kw == KW_object);
328 373
337 f.next (); 382 f.next ();
338 383
339#if 0 384#if 0
340 // implementing it here in the server does neither allow multiple inheritence 385 // implementing it here in the server does neither allow multiple inheritence
341 // nor does it cleanly "just override". it would allow use in map files, though, 386 // nor does it cleanly "just override". it would allow use in map files, though,
342 // and other resource files dynamically laoded (as opposed to being preprocessed). 387 // and other resource files dynamically loaded (as opposed to being preprocessed).
343 // not that any of this is relevant as of yet... 388 // not that any of this is relevant as of yet...
344 if (f.kw == KW_inherit) 389 if (f.kw == KW_inherit)
345 { 390 {
346 if (archetype *at = find (f.get_str ())) 391 if (archetype *at = find (f.get_str ()))
347 *op = at->clone; 392 *op = at->clone;
404 goto fail; 449 goto fail;
405 } 450 }
406 } 451 }
407 452
408 // assemble new chain 453 // assemble new chain
409 new_head->min_x = new_head->max_x = new_head->x; 454 new_head->max_x = new_head->x;
410 new_head->min_y = new_head->max_y = new_head->y;
411 455
412 archetype *less = new_head; 456 archetype *less = new_head;
413 for (auto (p, parts.begin () + 1); p != parts.end (); ++p) 457 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
414 { 458 {
415 archetype *at = *p; 459 archetype *at = *p;
416 460
417 // some flags get inherited formt he head (probably a lot more) 461 // some flags get inherited from the head (probably a lot more)
418 // 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
419 // to ignore head checks for these flags, which saves time 463 // to ignore head checks for these flags, which saves time
420 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; 464 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
421 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; 465 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
422 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; 466 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
423 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; 467 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
424 468
425 if (at->x < new_head->min_x) new_head->min_x = at->x;
426 if (at->y < new_head->min_y) new_head->min_y = at->y;
427 if (at->x > new_head->max_x) new_head->max_x = at->x; 469 new_head->max_x = max (new_head->max_x, at->x);
428 if (at->y > new_head->max_y) new_head->max_y = at->y;
429 470
430 at->head = new_head; 471 at->head = new_head;
431 less->more = at; 472 less->more = at;
432 less = at; 473 less = at;
433 } 474 }
437 return new_head; 478 return new_head;
438 } 479 }
439 480
440fail: 481fail:
441 for (auto (p, parts.begin ()); p != parts.end (); ++p) 482 for (auto (p, parts.begin ()); p != parts.end (); ++p)
442 (*p)->destroy (true); 483 (*p)->destroy ();
443 484
444 return 0; 485 return 0;
445} 486}
446 487
447void 488void
460 archetype *at = *p; 501 archetype *at = *p;
461 502
462 if (archetype *old = find (at->archname)) 503 if (archetype *old = find (at->archname))
463 old->unlink (); 504 old->unlink ();
464 505
506 allarch.push_back (at);
507
465 at->link (); 508 at->link ();
509 ++dirtycnt;
466 } 510 }
467 511
468 postponed_arch.clear (); 512 postponed_arch.clear ();
469 513
470 // now resolve arch references 514 // now resolve arch references
482 postponed_arch_ref.clear (); 526 postponed_arch_ref.clear ();
483 527
484 empty = find (shstr_empty_archetype); 528 empty = find (shstr_empty_archetype);
485} 529}
486 530
487/* 531void
488 * Creates and returns a new object which is a copy of the given archetype. 532archetype::gc ()
489 * This function returns NULL on failure.
490 */
491object *
492arch_to_object (archetype *at)
493{ 533{
494 if (!at) 534 int cnt = max (1, min<int> (allarch.size () / 128, dirtycnt));
535 dirtycnt = max (0, dirtycnt - cnt);
536
537 do
495 { 538 {
496 LOG (llevError, "Couldn't find archetype.\n"); 539 static int idx;
540
541 if (idx >= allarch.size ())
542 if (idx)
543 idx = 0;
544 else
497 return 0; 545 return;
498 }
499 546
500 object *op = at->clone (); 547 archetype *at = allarch [idx];
501 op->arch = at;
502 op->instantiate ();
503 548
504 return op; 549 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
550 ++idx;
551 else
552 {
553 //LOG (llevDebug, "garbage collect arch %s", &at->archname);
554 assert (at->arch == at); // verify that refcnt == 1 is truly valid
555 allarch.erase (idx);
556
557 // break chain
558 for (object *op = at->head_ (); op; )
559 {
560 object *next = op->more;
561 op->head = 0;
562 op->more = 0;
563 op = next;
564 }
565
566 at->destroy ();
567 at->arch = 0;
568 }
569 }
570 while (--cnt);
505} 571}
506 572
507object * 573object *
508archetype::instance () 574archetype::instance ()
509{ 575{
510 return arch_to_object (this); 576 object *op = clone ();
511} 577 op->instantiate ();
512
513/*
514 * Creates an object. This function is called by get_archetype()
515 * if it fails to find the appropriate archetype.
516 * Thus get_archetype() will be guaranteed to always return
517 * an object, and never NULL.
518 */
519object *
520create_singularity (const char *name)
521{
522 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
523
524 if (!strcmp (name, "bug"))
525 abort ();
526
527 char buf[MAX_BUF];
528 sprintf (buf, "bug, please report (%s)", name);
529
530 object *op = get_archetype ("bug");
531 op->name = op->name_pl = buf;
532
533 return op; 578 return op;
534} 579}
580
581//+GPL
535 582
536/* 583/*
537 * Finds which archetype matches the given name, and returns a new 584 * Finds which archetype matches the given name, and returns a new
538 * object containing a copy of the archetype. 585 * object containing a copy of the archetype.
539 */ 586 */
540object * 587object *
541get_archetype (const char *name) 588archetype::get (const char *name)
542{ 589{
543 archetype *at = archetype::find (name); 590 archetype *at = find (name);
544 591
545 if (!at) 592 if (!at)
546 return create_singularity (name); 593 return create_singularity (name);
547 594
548 return arch_to_object (at); 595 return at->instance ();
549} 596}
550 597
551/* 598/*
552 * Returns the first archetype using the given type. 599 * Returns the first archetype using the given type.
553 * Used in treasure-generation. 600 * Used in treasure-generation.
554 */ 601 */
555archetype * 602static archetype *
556type_to_archetype (int type) 603type_to_archetype (int type)
557{ 604{
558 for_all_archetypes (at) 605 for_all_archetypes (at)
559 if (at->type == type && at->head_ () != at) 606 if (at->type == type && at->head_ () != at)
560 return at; 607 return at;
568 * Used in treasure-generation. 615 * Used in treasure-generation.
569 */ 616 */
570object * 617object *
571clone_arch (int type) 618clone_arch (int type)
572{ 619{
573 archetype *at; 620 archetype *at = type_to_archetype (type);
574 621
575 if ((at = type_to_archetype (type)) == NULL) 622 if (!at)
576 { 623 {
577 LOG (llevError, "Can't clone archetype %d\n", type); 624 LOG (llevError, "Can't clone archetype %d\n", type);
578 return 0; 625 return 0;
579 } 626 }
580 627
581 object *op = at->clone (); 628 return at->instance ();
582 op->instantiate ();
583 return op;
584} 629}
585 630
586/* 631/*
587 * member: make instance from class 632 * member: make instance from class
588 */ 633 */
591{ 636{
592 object *op, *prev = 0, *head = 0; 637 object *op, *prev = 0, *head = 0;
593 638
594 while (at) 639 while (at)
595 { 640 {
596 op = arch_to_object (at); 641 op = at->instance ();
597 642
598 op->x = at->x; 643 op->x = at->x;
599 op->y = at->y; 644 op->y = at->y;
600 645
601 if (head) 646 if (head)
609 } 654 }
610 655
611 return head; 656 return head;
612} 657}
613 658
659//-GPL
660

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines