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.92 by root, Thu Nov 5 15:43:21 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
53 57
54/** 58/**
55 * GROS - This function retrieves an archetype given the name that appears 59 * GROS - This function retrieves an archetype given the name that appears
56 * during the game (for example, "writing pen" instead of "stylus"). 60 * 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. 61 * It does not use the hashtable system, but browse the whole archlist each time.
101{ 105{
102 shstr_cmp skill_cmp (skill); 106 shstr_cmp skill_cmp (skill);
103 107
104 for_all_archetypes (at) 108 for_all_archetypes (at)
105 if (at->skill == skill_cmp && (type == -1 || type == at->type)) 109 if (at->skill == skill_cmp && (type == -1 || type == at->type))
106 return arch_to_object (at); 110 return at->instance ();
107 111
108 return 0; 112 return 0;
109} 113}
110 114
111/* similiar to above - this returns the first archetype 115/* similiar to above - this returns the first archetype
132 * - name: The name we're searching for (ex: "writing pen"); 136 * - name: The name we're searching for (ex: "writing pen");
133 * Return value: 137 * Return value:
134 * - a corresponding object if found; a singularity object if not found. 138 * - 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 139 * 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 140 * 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, 141 * doesn't allocate it each time - not that this function is used much,
138 * but it otherwise had a big memory leak. 142 * but it otherwise had a big memory leak.
139 */ 143 */
140object * 144object *
141get_archetype_by_object_name (const char *name) 145get_archetype_by_object_name (const char *name)
142{ 146{
143 char tmpname[MAX_BUF]; 147 char tmpname[MAX_BUF];
144 int i;
145 148
146 assign (tmpname, name); 149 assign (tmpname, name);
147 150
148 for (i = strlen (tmpname); i > 0; i--) 151 for (int i = strlen (tmpname); i > 0; i--)
149 { 152 {
150 tmpname[i] = 0; 153 tmpname[i] = 0;
151 154
152 if (archetype *at = find_archetype_by_object_name (tmpname)) 155 if (archetype *at = find_archetype_by_object_name (tmpname))
153 return arch_to_object (at); 156 return at->instance ();
154 } 157 }
155 158
156 return create_singularity (name); 159 return create_singularity (name);
157} 160}
158 161
192 return 1; 195 return 1;
193 196
194 /* unpaid is a little more specific */ 197 /* unpaid is a little more specific */
195 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID)) 198 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID))
196 return 2; 199 return 2;
200
197 if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED))) 201 if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED)))
198 return 2; 202 return 2;
199 203
200 if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED)) 204 if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, FLAG_INV_LOCKED))
201 return 2; 205 return 2;
223 * is because of the 'else' handling - we don't want to match on 227 * 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 228 * 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 229 * later. So keep it in descending order here, so we try for the best
226 * match first, and work downward. 230 * match first, and work downward.
227 */ 231 */
232 const char *qbn0, *qbn1, *qsn; // query base name/short name caches
233
228 if (!strcasecmp (cp, query_name (op))) 234 if (!strcasecmp (cp, query_name (op)))
229 retval = 20; 235 retval = 20;
230 else if (!strcasecmp (cp, query_short_name (op))) 236 else if (!strcasecmp (cp, qsn = query_short_name (op)))
231 retval = 18; 237 retval = 18;
232 else if (!strcasecmp (cp, query_base_name (op, 0))) 238 else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0)))
233 retval = 16; 239 retval = 16;
234 else if (!strcasecmp (cp, query_base_name (op, 1))) 240 else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1)))
235 retval = 16; 241 retval = 16;
236 else if (op->custom_name && !strcasecmp (cp, op->custom_name)) 242 else if (op->custom_name && !strcasecmp (cp, op->custom_name))
237 retval = 15; 243 retval = 15;
238 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp))) 244 else if (!strncasecmp (cp, qbn0, strlen (cp)))
239 retval = 14; 245 retval = 14;
240 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp))) 246 else if (!strncasecmp (cp, qbn1, strlen (cp)))
241 retval = 14; 247 retval = 14;
242 /* Do substring checks, so things like 'Str+1' will match. 248 /* Do substring checks, so things like 'Str+1' will match.
243 * retval of these should perhaps be lower - they are lower 249 * retval of these should perhaps be lower - they are lower
244 * then the specific strcasecmp aboves, but still higher than 250 * then the specific strcasecmp aboves, but still higher than
245 * some other match criteria. 251 * some other match criteria.
246 */ 252 */
247 else if (strstr (query_base_name (op, 1), cp)) 253 else if (strstr (qbn1, cp))
248 retval = 12; 254 retval = 12;
249 else if (strstr (query_base_name (op, 0), cp)) 255 else if (strstr (qbn0, cp))
250 retval = 12; 256 retval = 12;
251 else if (strstr (query_short_name (op), cp)) 257 else if (strstr (qsn, cp))
252 retval = 12; 258 retval = 12;
253 /* Check against plural/non plural based on count. */ 259 /* Check against plural/non plural based on count. */
254 else if (count > 1 && !strcasecmp (cp, op->name_pl)) 260 else if (count > 1 && !strcasecmp (cp, op->name_pl))
255 retval = 6; 261 retval = 6;
256 else if (count == 1 && !strcasecmp (op->name, cp)) 262 else if (count == 1 && !strcasecmp (op->name, cp))
257 retval = 6; 263 retval = 6;
258 /* base name matched - not bad */ 264 /* base name matched - not bad */
259 else if (strcasecmp (cp, op->name) == 0 && !count) 265 else if (strcasecmp (cp, op->name) == 0 && !count)
260 retval = 4; 266 retval = 4;
261 /* Check for partial custom name, but give a real low priority */ 267 /* Check for partial custom name, but give a real low priority */
262 else if (op->custom_name && strstr (op->custom_name, cp)) 268 else if (op->custom_name.contains (cp))
263 retval = 3; 269 retval = 3;
264 270
265 if (retval) 271 if (retval)
266 { 272 {
267 if (pl->type == PLAYER) 273 if (pl->type == PLAYER)
272 } 278 }
273 279
274 return 0; 280 return 0;
275} 281}
276 282
283//-GPL
284
277archetype::archetype (const char *name) 285archetype::archetype (const char *name)
278{ 286{
279 arch = this; 287 arch = this;
280 this->archname = this->name = this->name_pl = name; 288 this->archname = this->name = this->name_pl = name;
281} 289}
286} 294}
287 295
288void 296void
289archetype::link () 297archetype::link ()
290{ 298{
291 ht.insert (std::make_pair (archname, this));
292
293 if (!archetypes.contains (this)) 299 if (!archetypes.contains (this))
300 {
294 archetypes.insert (this); 301 archetypes.insert (this);
302 ht.insert (std::make_pair (archname, this));
303 }
295} 304}
296 305
297void 306void
298archetype::unlink () 307archetype::unlink ()
299{ 308{
300 ht.erase (archname);
301
302 if (archetypes.contains (this)) 309 if (archetypes.contains (this))
310 {
303 archetypes.erase (this); 311 archetypes.erase (this);
312 ht.erase (archname);
313 }
304} 314}
305 315
306/* 316/*
307 * Finds, using the hashtable, which archetype matches the given name. 317 * Finds, using the hashtable, which archetype matches the given name.
308 * returns a pointer to the found archetype, otherwise NULL. 318 * returns a pointer to the found archetype, otherwise NULL.
412 archetype *less = new_head; 422 archetype *less = new_head;
413 for (auto (p, parts.begin () + 1); p != parts.end (); ++p) 423 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
414 { 424 {
415 archetype *at = *p; 425 archetype *at = *p;
416 426
417 // some flags get inherited formt he head (probably a lot more) 427 // some flags get inherited from the head (probably a lot more)
418 // doing it here doesn't feel too cozy, but it allows code 428 // doing it here doesn't feel too cozy, but it allows code
419 // to ignore head checks for these flags, which saves time 429 // to ignore head checks for these flags, which saves time
420 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; 430 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
421 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; 431 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
422 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; 432 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
423 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; 433 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
424 434
425 if (at->x < new_head->min_x) new_head->min_x = at->x; 435 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; 436 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; 437 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; 438 new_head->max_y = max (new_head->max_y, at->y);
429 439
430 at->head = new_head; 440 at->head = new_head;
431 less->more = at; 441 less->more = at;
432 less = at; 442 less = at;
433 } 443 }
437 return new_head; 447 return new_head;
438 } 448 }
439 449
440fail: 450fail:
441 for (auto (p, parts.begin ()); p != parts.end (); ++p) 451 for (auto (p, parts.begin ()); p != parts.end (); ++p)
442 (*p)->destroy (true); 452 (*p)->destroy ();
443 453
444 return 0; 454 return 0;
445} 455}
446 456
447void 457void
460 archetype *at = *p; 470 archetype *at = *p;
461 471
462 if (archetype *old = find (at->archname)) 472 if (archetype *old = find (at->archname))
463 old->unlink (); 473 old->unlink ();
464 474
475 allarch.push_back (at);
476
465 at->link (); 477 at->link ();
478 ++dirtycnt;
466 } 479 }
467 480
468 postponed_arch.clear (); 481 postponed_arch.clear ();
469 482
470 // now resolve arch references 483 // now resolve arch references
482 postponed_arch_ref.clear (); 495 postponed_arch_ref.clear ();
483 496
484 empty = find (shstr_empty_archetype); 497 empty = find (shstr_empty_archetype);
485} 498}
486 499
487/* 500void
488 * Creates and returns a new object which is a copy of the given archetype. 501archetype::gc ()
489 * This function returns NULL on failure.
490 */
491object *
492arch_to_object (archetype *at)
493{ 502{
494 if (!at) 503 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
504 dirtycnt = max (0, dirtycnt - cnt);
505
506 do
495 { 507 {
496 LOG (llevError, "Couldn't find archetype.\n"); 508 static int idx;
509
510 if (idx >= allarch.size ())
511 if (idx)
512 idx = 0;
513 else
497 return 0; 514 return;
498 }
499 515
516 archetype *at = allarch [idx];
517
518 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
519 ++idx;
520 else
521 {
522 LOG (llevDebug, "garbage collect arch %s", &at->archname);
523 assert (at->arch == at); // verify that refcnt == 1 is truly valid
524 allarch.erase (idx);
525
526 // break chain
527 for (object *op = at->head_ (); op; )
528 {
529 object *next = op->more;
530 op->head = 0;
531 op->more = 0;
532 op = next;
533 }
534
535 at->destroy ();
536 at->arch = 0;
537 }
538 }
539 while (--cnt);
540}
541
542object *
543archetype::instance ()
544{
500 object *op = at->clone (); 545 object *op = clone ();
501 op->arch = at;
502 op->instantiate (); 546 op->instantiate ();
503
504 return op; 547 return op;
505} 548}
506 549
507object * 550//+GPL
508archetype::instance ()
509{
510 return arch_to_object (this);
511}
512 551
513/* 552/*
514 * Creates an object. This function is called by get_archetype() 553 * Creates an object. This function is called by get_archetype()
515 * if it fails to find the appropriate archetype. 554 * if it fails to find the appropriate archetype.
516 * Thus get_archetype() will be guaranteed to always return 555 * Thus get_archetype() will be guaranteed to always return
522 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); 561 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
523 562
524 if (!strcmp (name, "bug")) 563 if (!strcmp (name, "bug"))
525 abort (); 564 abort ();
526 565
527 char buf[MAX_BUF];
528 sprintf (buf, "bug, please report (%s)", name);
529
530 object *op = get_archetype ("bug"); 566 object *op = archetype::get (shstr_bug);
531 op->name = op->name_pl = buf; 567 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
532 568
533 return op; 569 return op;
534} 570}
535 571
536/* 572/*
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