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.93 by root, Fri Nov 6 12:27:05 2009 UTC

3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra 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 * 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>
25 26
26#include <global.h> 27#include <global.h>
27#include <funcpoint.h>
28#include <loader.h> 28#include <loader.h>
29 29
30#include <tr1/functional> 30#include <tr1/functional>
31#include <tr1/unordered_map> 31#include <tr1/unordered_map>
32 32
33archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing 33archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
34archetype *archetype::empty; 34arch_ptr archetype::empty;
35 35
36// the hashtable 36// the hashtable
37typedef std::tr1::unordered_map 37typedef std::tr1::unordered_map
38 < 38 <
39 const char *, 39 const char *,
41 str_hash, 41 str_hash,
42 str_equal, 42 str_equal,
43 slice_allocator< std::pair<const char *const, arch_ptr> > 43 slice_allocator< std::pair<const char *const, arch_ptr> >
44 > HT; 44 > HT;
45 45
46static HT ht (5000); 46static HT ht (10000);
47archvec archetypes; 47archvec archetypes;
48static unordered_vector<archetype *> allarch;
49static int dirtycnt;
48 50
49// the vector of other_arch references to be resolved 51// the vector of other_arch references to be resolved
50static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; 52static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref;
51// the vector of loaded but not yet committed archetypes 53// the vector of loaded but not yet committed archetypes
52static std::vector<archetype *> postponed_arch; 54static std::vector<archetype *> postponed_arch;
55
56//+GPL
57
58/*
59 * Creates an object. This function is called by get_archetype()
60 * if it fails to find the appropriate archetype.
61 * Thus get_archetype() will be guaranteed to always return
62 * an object, and never NULL.
63 */
64object *
65create_singularity (const char *name)
66{
67 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
68
69 if (!strcmp (name, "bug"))
70 abort ();
71
72 object *op = archetype::get (shstr_bug);
73 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
74
75 return op;
76}
53 77
54/** 78/**
55 * GROS - This function retrieves an archetype given the name that appears 79 * GROS - This function retrieves an archetype given the name that appears
56 * during the game (for example, "writing pen" instead of "stylus"). 80 * 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. 81 * It does not use the hashtable system, but browse the whole archlist each time.
101{ 125{
102 shstr_cmp skill_cmp (skill); 126 shstr_cmp skill_cmp (skill);
103 127
104 for_all_archetypes (at) 128 for_all_archetypes (at)
105 if (at->skill == skill_cmp && (type == -1 || type == at->type)) 129 if (at->skill == skill_cmp && (type == -1 || type == at->type))
106 return arch_to_object (at); 130 return at->instance ();
107 131
108 return 0; 132 return 0;
109} 133}
110 134
111/* similiar to above - this returns the first archetype 135/* similiar to above - this returns the first archetype
132 * - name: The name we're searching for (ex: "writing pen"); 156 * - name: The name we're searching for (ex: "writing pen");
133 * Return value: 157 * Return value:
134 * - a corresponding object if found; a singularity object if not found. 158 * - 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 159 * 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 160 * 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, 161 * doesn't allocate it each time - not that this function is used much,
138 * but it otherwise had a big memory leak. 162 * but it otherwise had a big memory leak.
139 */ 163 */
140object * 164object *
141get_archetype_by_object_name (const char *name) 165get_archetype_by_object_name (const char *name)
142{ 166{
143 char tmpname[MAX_BUF]; 167 char tmpname[MAX_BUF];
144 int i;
145 168
146 assign (tmpname, name); 169 assign (tmpname, name);
147 170
148 for (i = strlen (tmpname); i > 0; i--) 171 for (int i = strlen (tmpname); i > 0; i--)
149 { 172 {
150 tmpname[i] = 0; 173 tmpname[i] = 0;
151 174
152 if (archetype *at = find_archetype_by_object_name (tmpname)) 175 if (archetype *at = find_archetype_by_object_name (tmpname))
153 return arch_to_object (at); 176 return at->instance ();
154 } 177 }
155 178
156 return create_singularity (name); 179 return create_singularity (name);
157} 180}
158 181
192 return 1; 215 return 1;
193 216
194 /* unpaid is a little more specific */ 217 /* unpaid is a little more specific */
195 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) 218 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID))
196 return 2; 219 return 2;
220
197 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") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED)))
198 return 2; 222 return 2;
199 223
200 if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) 224 if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED))
201 return 2; 225 return 2;
223 * is because of the 'else' handling - we don't want to match on 247 * 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 248 * 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 249 * later. So keep it in descending order here, so we try for the best
226 * match first, and work downward. 250 * match first, and work downward.
227 */ 251 */
252 const char *qbn0, *qbn1, *qsn; // query base name/short name caches
253
228 if (!strcasecmp (cp, query_name (op))) 254 if (!strcasecmp (cp, query_name (op)))
229 retval = 20; 255 retval = 20;
230 else if (!strcasecmp (cp, query_short_name (op))) 256 else if (!strcasecmp (cp, qsn = query_short_name (op)))
231 retval = 18; 257 retval = 18;
232 else if (!strcasecmp (cp, query_base_name (op, 0))) 258 else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0)))
233 retval = 16; 259 retval = 16;
234 else if (!strcasecmp (cp, query_base_name (op, 1))) 260 else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1)))
235 retval = 16; 261 retval = 16;
236 else if (op->custom_name && !strcasecmp (cp, op->custom_name)) 262 else if (op->custom_name && !strcasecmp (cp, op->custom_name))
237 retval = 15; 263 retval = 15;
238 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) 264 else if (!strncasecmp (cp, qbn0, strlen (cp)))
239 retval = 14; 265 retval = 14;
240 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) 266 else if (!strncasecmp (cp, qbn1, strlen (cp)))
241 retval = 14; 267 retval = 14;
242 /* Do substring checks, so things like 'Str+1' will match. 268 /* Do substring checks, so things like 'Str+1' will match.
243 * retval of these should perhaps be lower - they are lower 269 * retval of these should perhaps be lower - they are lower
244 * then the specific strcasecmp aboves, but still higher than 270 * then the specific strcasecmp aboves, but still higher than
245 * some other match criteria. 271 * some other match criteria.
246 */ 272 */
247 else if (strstr (query_base_name (op, 1), cp)) 273 else if (strstr (qbn1, cp))
248 retval = 12; 274 retval = 12;
249 else if (strstr (query_base_name (op, 0), cp)) 275 else if (strstr (qbn0, cp))
250 retval = 12; 276 retval = 12;
251 else if (strstr (query_short_name (op), cp)) 277 else if (strstr (qsn, cp))
252 retval = 12; 278 retval = 12;
253 /* Check against plural/non plural based on count. */ 279 /* Check against plural/non plural based on count. */
254 else if (count > 1 && !strcasecmp (cp, op->name_pl)) 280 else if (count > 1 && !strcasecmp (cp, op->name_pl))
255 retval = 6; 281 retval = 6;
256 else if (count == 1 && !strcasecmp (op->name, cp)) 282 else if (count == 1 && !strcasecmp (op->name, cp))
257 retval = 6; 283 retval = 6;
258 /* base name matched - not bad */ 284 /* base name matched - not bad */
259 else if (strcasecmp (cp, op->name) == 0 && !count) 285 else if (strcasecmp (cp, op->name) == 0 && !count)
260 retval = 4; 286 retval = 4;
261 /* Check for partial custom name, but give a real low priority */ 287 /* Check for partial custom name, but give a real low priority */
262 else if (op->custom_name && strstr (op->custom_name, cp)) 288 else if (op->custom_name.contains (cp))
263 retval = 3; 289 retval = 3;
264 290
265 if (retval) 291 if (retval)
266 { 292 {
267 if (pl->type == PLAYER) 293 if (pl->type == PLAYER)
272 } 298 }
273 299
274 return 0; 300 return 0;
275} 301}
276 302
303//-GPL
304
277archetype::archetype (const char *name) 305archetype::archetype (const char *name)
278{ 306{
279 arch = this; 307 arch = this;
280 this->archname = this->name = this->name_pl = name; 308 this->archname = this->name = this->name_pl = name;
281} 309}
286} 314}
287 315
288void 316void
289archetype::link () 317archetype::link ()
290{ 318{
291 ht.insert (std::make_pair (archname, this));
292
293 if (!archetypes.contains (this)) 319 if (!archetypes.contains (this))
320 {
294 archetypes.insert (this); 321 archetypes.insert (this);
322 ht.insert (std::make_pair (archname, this));
323 }
295} 324}
296 325
297void 326void
298archetype::unlink () 327archetype::unlink ()
299{ 328{
300 ht.erase (archname);
301
302 if (archetypes.contains (this)) 329 if (archetypes.contains (this))
330 {
303 archetypes.erase (this); 331 archetypes.erase (this);
332 ht.erase (archname);
333 }
304} 334}
305 335
306/* 336/*
307 * Finds, using the hashtable, which archetype matches the given name. 337 * Finds, using the hashtable, which archetype matches the given name.
308 * returns a pointer to the found archetype, otherwise NULL. 338 * returns a pointer to the found archetype, otherwise NULL.
412 archetype *less = new_head; 442 archetype *less = new_head;
413 for (auto (p, parts.begin () + 1); p != parts.end (); ++p) 443 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
414 { 444 {
415 archetype *at = *p; 445 archetype *at = *p;
416 446
417 // some flags get inherited formt he head (probably a lot more) 447 // some flags get inherited from the head (probably a lot more)
418 // doing it here doesn't feel too cozy, but it allows code 448 // doing it here doesn't feel too cozy, but it allows code
419 // to ignore head checks for these flags, which saves time 449 // to ignore head checks for these flags, which saves time
420 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; 450 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
421 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; 451 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
422 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; 452 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
423 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; 453 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
424 454
425 if (at->x < new_head->min_x) new_head->min_x = at->x; 455 new_head->min_x = min (new_head->min_x, at->x);
426 if (at->y < new_head->min_y) new_head->min_y = at->y; 456 new_head->min_y = min (new_head->min_y, at->y);
427 if (at->x > new_head->max_x) new_head->max_x = at->x; 457 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; 458 new_head->max_y = max (new_head->max_y, at->y);
429 459
430 at->head = new_head; 460 at->head = new_head;
431 less->more = at; 461 less->more = at;
432 less = at; 462 less = at;
433 } 463 }
437 return new_head; 467 return new_head;
438 } 468 }
439 469
440fail: 470fail:
441 for (auto (p, parts.begin ()); p != parts.end (); ++p) 471 for (auto (p, parts.begin ()); p != parts.end (); ++p)
442 (*p)->destroy (true); 472 (*p)->destroy ();
443 473
444 return 0; 474 return 0;
445} 475}
446 476
447void 477void
460 archetype *at = *p; 490 archetype *at = *p;
461 491
462 if (archetype *old = find (at->archname)) 492 if (archetype *old = find (at->archname))
463 old->unlink (); 493 old->unlink ();
464 494
495 allarch.push_back (at);
496
465 at->link (); 497 at->link ();
498 ++dirtycnt;
466 } 499 }
467 500
468 postponed_arch.clear (); 501 postponed_arch.clear ();
469 502
470 // now resolve arch references 503 // now resolve arch references
482 postponed_arch_ref.clear (); 515 postponed_arch_ref.clear ();
483 516
484 empty = find (shstr_empty_archetype); 517 empty = find (shstr_empty_archetype);
485} 518}
486 519
487/* 520void
488 * Creates and returns a new object which is a copy of the given archetype. 521archetype::gc ()
489 * This function returns NULL on failure.
490 */
491object *
492arch_to_object (archetype *at)
493{ 522{
494 if (!at) 523 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
524 dirtycnt = max (0, dirtycnt - cnt);
525
526 do
495 { 527 {
496 LOG (llevError, "Couldn't find archetype.\n"); 528 static int idx;
529
530 if (idx >= allarch.size ())
531 if (idx)
532 idx = 0;
533 else
497 return 0; 534 return;
498 }
499 535
536 archetype *at = allarch [idx];
537
538 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
539 ++idx;
540 else
541 {
542 LOG (llevDebug, "garbage collect arch %s", &at->archname);
543 assert (at->arch == at); // verify that refcnt == 1 is truly valid
544 allarch.erase (idx);
545
546 // break chain
547 for (object *op = at->head_ (); op; )
548 {
549 object *next = op->more;
550 op->head = 0;
551 op->more = 0;
552 op = next;
553 }
554
555 at->destroy ();
556 at->arch = 0;
557 }
558 }
559 while (--cnt);
560}
561
562object *
563archetype::instance ()
564{
500 object *op = at->clone (); 565 object *op = clone ();
501 op->arch = at;
502 op->instantiate (); 566 op->instantiate ();
503
504 return op; 567 return op;
505} 568}
506 569
507object * 570//+GPL
508archetype::instance ()
509{
510 return arch_to_object (this);
511}
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;
534}
535 571
536/* 572/*
537 * Finds which archetype matches the given name, and returns a new 573 * Finds which archetype matches the given name, and returns a new
538 * object containing a copy of the archetype. 574 * object containing a copy of the archetype.
539 */ 575 */
540object * 576object *
541get_archetype (const char *name) 577get_archetype (const char *name)
542{ 578{
579 return archetype::get (name);
580}
581
582object *
583archetype::get (const char *name)
584{
543 archetype *at = archetype::find (name); 585 archetype *at = find (name);
544 586
545 if (!at) 587 if (!at)
546 return create_singularity (name); 588 return create_singularity (name);
547 589
548 return arch_to_object (at); 590 return at->instance ();
549} 591}
550 592
551/* 593/*
552 * Returns the first archetype using the given type. 594 * Returns the first archetype using the given type.
553 * Used in treasure-generation. 595 * Used in treasure-generation.
568 * Used in treasure-generation. 610 * Used in treasure-generation.
569 */ 611 */
570object * 612object *
571clone_arch (int type) 613clone_arch (int type)
572{ 614{
573 archetype *at; 615 archetype *at = type_to_archetype (type);
574 616
575 if ((at = type_to_archetype (type)) == NULL) 617 if (!at)
576 { 618 {
577 LOG (llevError, "Can't clone archetype %d\n", type); 619 LOG (llevError, "Can't clone archetype %d\n", type);
578 return 0; 620 return 0;
579 } 621 }
580 622
581 object *op = at->clone (); 623 return at->instance ();
582 op->instantiate ();
583 return op;
584} 624}
585 625
586/* 626/*
587 * member: make instance from class 627 * member: make instance from class
588 */ 628 */
591{ 631{
592 object *op, *prev = 0, *head = 0; 632 object *op, *prev = 0, *head = 0;
593 633
594 while (at) 634 while (at)
595 { 635 {
596 op = arch_to_object (at); 636 op = at->instance ();
597 637
598 op->x = at->x; 638 op->x = at->x;
599 op->y = at->y; 639 op->y = at->y;
600 640
601 if (head) 641 if (head)
609 } 649 }
610 650
611 return head; 651 return head;
612} 652}
613 653
654//-GPL
655

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines