ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.105
Committed: Thu Apr 15 00:36:51 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.104: +5 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * 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 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * 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 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 #include <cassert>
26
27 #include <global.h>
28
29 #include <tr1/functional>
30 #include <tr1/unordered_map>
31
32 archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
33 arch_ptr archetype::empty;
34
35 // the hashtable
36 typedef std::tr1::unordered_map
37 <
38 const char *,
39 arch_ptr,
40 str_hash,
41 str_equal,
42 slice_allocator< std::pair<const char *const, arch_ptr> >
43 > HT;
44
45 static HT ht (10000);
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;
54
55 //+GPL
56
57 /*
58 * Creates an object. This function is called by get_archetype ()
59 * if it fails to find the appropriate archetype.
60 * Thus get_archetype() will be guaranteed to always return
61 * an object, and never NULL.
62 */
63 static object *
64 create_singularity (const char *name)
65 {
66 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
67
68 if (!strcmp (name, "bug"))
69 abort ();
70
71 object *op = archetype::get (shstr_bug);
72 op->name = op->name_pl = format ("bug, please report (missing archetype %s)", name);
73
74 return op;
75 }
76
77 /**
78 * GROS - This function retrieves an archetype given the name that appears
79 * during the game (for example, "writing pen" instead of "stylus").
80 * It does not use the hashtable system, but browse the whole archlist each time.
81 * I suggest not to use it unless you really need it because of performance issue.
82 * It is currently used by scripting extensions (create-object).
83 * Params:
84 * - name: the name we're searching for (ex: "writing pen");
85 * Return value:
86 * - the archetype found or null if nothing was found.
87 */
88 archetype *
89 find_archetype_by_object_name (const char *name)
90 {
91 shstr_cmp name_cmp (name);
92
93 for_all_archetypes (at)
94 if (at->name == name_cmp)
95 return at;
96
97 return 0;
98 }
99
100 /**
101 * This function retrieves an archetype by type and name that appears during
102 * the game. It is basically the same as find_archetype_by_object_name()
103 * except that it considers only items of the given type.
104 */
105 archetype *
106 find_archetype_by_object_type_name (int type, const char *name)
107 {
108 shstr_cmp name_cmp (name);
109
110 for_all_archetypes (at)
111 if (at->name == name_cmp && at->type == type)
112 return at;
113
114 return 0;
115 }
116
117 /* This is a lot like the above function. Instead, we are trying to match
118 * the arch->skill values. type is the type of object to match
119 * against (eg, to only match against skills or only skill objects for example).
120 * If type is -1, ew don't match on type.
121 */
122 object *
123 get_archetype_by_skill_name (const char *skill, int type)
124 {
125 shstr_cmp skill_cmp (skill);
126
127 for_all_archetypes (at)
128 if (at->skill == skill_cmp && (type == -1 || type == at->type))
129 return at->instance ();
130
131 return 0;
132 }
133
134 /* similiar to above - this returns the first archetype
135 * that matches both the type and subtype. type and subtype
136 * can be -1 to say ignore, but in this case, the match it does
137 * may not be very useful. This function is most useful when
138 * subtypes are known to be unique for a particular type
139 * (eg, skills)
140 */
141 archetype *
142 get_archetype_by_type_subtype (int type, int subtype)
143 {
144 for_all_archetypes (at)
145 if ((type == -1 || type == at->type) && (subtype == -1 || subtype == at->subtype))
146 return at;
147
148 return 0;
149 }
150
151 /**
152 * GROS - this returns a new object given the name that appears during the game
153 * (for example, "writing pen" instead of "stylus").
154 * Params:
155 * - name: The name we're searching for (ex: "writing pen");
156 * Return value:
157 * - a corresponding object if found; a singularity object if not found.
158 * Note by MSW - it appears that it takes the full name and keeps
159 * shortening it until it finds a match. I re-wrote this so that it
160 * doesn't allocate it each time - not that this function is used much,
161 * but it otherwise had a big memory leak.
162 */
163 object *
164 get_archetype_by_object_name (const char *name)
165 {
166 char tmpname[MAX_BUF];
167
168 assign (tmpname, name);
169
170 for (int i = strlen (tmpname); i > 0; i--)
171 {
172 tmpname[i] = 0;
173
174 if (archetype *at = find_archetype_by_object_name (tmpname))
175 return at->instance ();
176 }
177
178 return create_singularity (name);
179 }
180
181 /* This is a subset of the parse_id command. Basically, name can be
182 * a string seperated lists of things to match, with certain keywords.
183 * pl is the player (only needed to set count properly)
184 * op is the item we are trying to match. Calling function takes care
185 * of what action might need to be done and if it is valid
186 * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
187 * value means a better match. 0 means no match.
188 *
189 * Brief outline of the procedure:
190 * We take apart the name variable into the individual components.
191 * cases for 'all' and unpaid are pretty obvious.
192 * Next, we check for a count (either specified in name, or in the
193 * player object.)
194 * If count is 1, make a quick check on the name.
195 * IF count is >1, we need to make plural name. Return if match.
196 * Last, make a check on the full name.
197 */
198 int
199 item_matched_string (object *pl, object *op, const char *name)
200 {
201 char *cp, local_name[MAX_BUF];
202 int count, retval = 0;
203
204 assign (local_name, name); /* strtok is destructive to name */
205
206 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
207 {
208 while (cp[0] == ' ')
209 ++cp; /* get rid of spaces */
210
211 /* LOG(llevDebug,"Trying to match %s\n", cp); */
212 /* All is a very generic match - low match value */
213 if (!strcmp (cp, "all"))
214 return 1;
215
216 /* unpaid is a little more specific */
217 if (!strcmp (cp, "unpaid") && op->flag [FLAG_UNPAID])
218 return 2;
219
220 if (!strcmp (cp, "cursed") && op->flag [FLAG_KNOWN_CURSED] && (op->flag [FLAG_CURSED] || op->flag [FLAG_DAMNED]))
221 return 2;
222
223 if (!strcmp (cp, "unlocked") && !op->flag [FLAG_INV_LOCKED])
224 return 2;
225
226 /* Allow for things like '100 arrows' */
227 if ((count = atoi (cp)) != 0)
228 {
229 cp = strchr (cp, ' ');
230 while (cp && cp[0] == ' ')
231 ++cp; /* get rid of spaces */
232 }
233 else
234 {
235 if (pl->type == PLAYER)
236 count = pl->contr->count;
237 else
238 count = 0;
239 }
240
241 if (!cp || cp[0] == '\0' || count < 0)
242 return 0;
243
244
245 /* The code here should go from highest retval to lowest. That
246 * is because of the 'else' handling - we don't want to match on
247 * something and set a low retval, even though it may match a higher retcal
248 * later. So keep it in descending order here, so we try for the best
249 * match first, and work downward.
250 */
251 const char *qbn0, *qbn1, *qsn; // query base name/short name caches
252
253 if (!strcasecmp (cp, query_name (op)))
254 retval = 20;
255 else if (!strcasecmp (cp, qsn = query_short_name (op)))
256 retval = 18;
257 else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0)))
258 retval = 16;
259 else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1)))
260 retval = 16;
261 else if (op->custom_name && !strcasecmp (cp, op->custom_name))
262 retval = 15;
263 else if (!strncasecmp (cp, qbn0, strlen (cp)))
264 retval = 14;
265 else if (!strncasecmp (cp, qbn1, strlen (cp)))
266 retval = 14;
267 /* Do substring checks, so things like 'Str+1' will match.
268 * retval of these should perhaps be lower - they are lower
269 * then the specific strcasecmp aboves, but still higher than
270 * some other match criteria.
271 */
272 else if (strstr (qbn1, cp))
273 retval = 12;
274 else if (strstr (qbn0, cp))
275 retval = 12;
276 else if (strstr (qsn, cp))
277 retval = 12;
278 /* Check against plural/non plural based on count. */
279 else if (count > 1 && !strcasecmp (cp, op->name_pl))
280 retval = 6;
281 else if (count == 1 && !strcasecmp (op->name, cp))
282 retval = 6;
283 /* base name matched - not bad */
284 else if (strcasecmp (cp, op->name) == 0 && !count)
285 retval = 4;
286 /* Check for partial custom name, but give a real low priority */
287 else if (op->custom_name.contains (cp))
288 retval = 3;
289
290 if (retval)
291 {
292 if (pl->type == PLAYER)
293 pl->contr->count = count;
294
295 return retval;
296 }
297 }
298
299 return 0;
300 }
301
302 //-GPL
303
304 void
305 archetype::do_delete ()
306 {
307 delete this;
308 }
309
310 archetype::archetype (const char *name)
311 {
312 arch = this;
313 this->archname = this->name = this->name_pl = name;
314 }
315
316 archetype::~archetype ()
317 {
318 unlink ();
319 }
320
321 void
322 archetype::link ()
323 {
324 if (!archetypes.contains (this))
325 {
326 archetypes.insert (this);
327 ht.insert (std::make_pair (archname, this));
328 }
329 }
330
331 void
332 archetype::unlink ()
333 {
334 if (archetypes.contains (this))
335 {
336 archetypes.erase (this);
337 ht.erase (archname);
338 }
339 }
340
341 /*
342 * Finds, using the hashtable, which archetype matches the given name.
343 * returns a pointer to the found archetype, otherwise NULL.
344 */
345 archetype *
346 archetype::find (const char *name)
347 {
348 if (!name)
349 return 0;
350
351 auto (i, ht.find (name));
352
353 if (i == ht.end ())
354 return 0;
355 else
356 return i->second;
357 }
358
359 void
360 archetype::post_load_check ()
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
367 object::post_load_check ();
368 }
369
370 archetype *
371 archetype::read (object_thawer &f)
372 {
373 assert (f.kw == KW_object);
374
375 std::vector<archetype *> parts;
376
377 coroapi::cede_to_tick ();
378
379 for (;;)
380 {
381 archetype *at = new archetype (f.get_str ());
382
383 f.next ();
384
385 #if 0
386 // implementing it here in the server does neither allow multiple inheritence
387 // nor does it cleanly "just override". it would allow use in map files, though,
388 // and other resource files dynamically loaded (as opposed to being preprocessed).
389 // not that any of this is relevant as of yet...
390 if (f.kw == KW_inherit)
391 {
392 if (archetype *at = find (f.get_str ()))
393 *op = at->clone;
394 else
395 LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n",
396 &at->archname, f.get_str ());
397
398 f.next ();
399 }
400 #endif
401
402 loading_arch = at; // hack to tell parse_kv et al. to behave
403 bool parse_ok = at->parse_kv (f);
404 loading_arch = 0;
405
406 if (!parse_ok)
407 goto fail;
408
409 loading_arch = at; // hack to tell parse_kv et al. to behave
410 at->post_load_check ();
411 loading_arch = 0;
412
413 parts.push_back (at);
414
415 if (f.kw != KW_more)
416 break;
417
418 f.next ();
419
420 if (f.kw != KW_object)
421 {
422 f.parse_error ("more object");
423 goto fail;
424 }
425 }
426
427 {
428 auto (at, parts.begin ());
429
430 archetype *new_head = parts.front ();
431 archetype *old_head = find (new_head->archname);
432
433 if (old_head && !old_head->is_head ())
434 {
435 LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n",
436 &new_head->archname, &old_head->archname);
437 goto fail;
438 }
439
440 // check that all archetypes belong to the same old object or are new
441 for (auto (at, parts.begin ()); at != parts.end (); ++at)
442 {
443 archetype *new_part = *at;
444 archetype *old_part = find (new_part->archname);
445
446 if (old_part && old_part->head_ () != old_head)
447 {
448 LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n",
449 &new_part->archname, &((archetype *)old_part->head_ ())->archname);
450 goto fail;
451 }
452 }
453
454 // assemble new chain
455 new_head->max_x = new_head->x;
456
457 archetype *less = new_head;
458 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
459 {
460 archetype *at = *p;
461
462 // some flags get inherited from the head (probably a lot more)
463 // doing it here doesn't feel too cozy, but it allows code
464 // to ignore head checks for these flags, which saves time
465 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
466 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
467 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
468 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
469
470 new_head->max_x = max (new_head->max_x, at->x);
471
472 at->head = new_head;
473 less->more = at;
474 less = at;
475 }
476
477 postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ());
478
479 return new_head;
480 }
481
482 fail:
483 for (auto (p, parts.begin ()); p != parts.end (); ++p)
484 (*p)->destroy ();
485
486 return 0;
487 }
488
489 void
490 archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch)
491 {
492 ref = 0;
493 postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch)));
494 }
495
496 void
497 archetype::commit_load ()
498 {
499 // unlink old archetypes and link in new ones */
500 for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p)
501 {
502 archetype *at = *p;
503
504 if (archetype *old = find (at->archname))
505 old->unlink ();
506
507 allarch.push_back (at);
508
509 at->link ();
510 ++dirtycnt;
511 }
512
513 postponed_arch.clear ();
514
515 // now resolve arch references
516 for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p)
517 {
518 arch_ptr *ap = p->first;
519 archetype *at = find (p->second);
520
521 if (!at)
522 LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second);
523
524 *ap = at;
525 }
526
527 postponed_arch_ref.clear ();
528
529 empty = find (shstr_empty_archetype);
530 }
531
532 void
533 archetype::gc ()
534 {
535 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
536 dirtycnt = max (0, dirtycnt - cnt);
537
538 do
539 {
540 static int idx;
541
542 if (idx >= allarch.size ())
543 if (idx)
544 idx = 0;
545 else
546 return;
547
548 archetype *at = allarch [idx];
549
550 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
551 ++idx;
552 else
553 {
554 LOG (llevDebug, "garbage collect arch %s", &at->archname);
555 assert (at->arch == at); // verify that refcnt == 1 is truly valid
556 allarch.erase (idx);
557
558 // break chain
559 for (object *op = at->head_ (); op; )
560 {
561 object *next = op->more;
562 op->head = 0;
563 op->more = 0;
564 op = next;
565 }
566
567 at->destroy ();
568 at->arch = 0;
569 }
570 }
571 while (--cnt);
572 }
573
574 object *
575 archetype::instance ()
576 {
577 object *op = clone ();
578 op->instantiate ();
579 return op;
580 }
581
582 //+GPL
583
584 /*
585 * Finds which archetype matches the given name, and returns a new
586 * object containing a copy of the archetype.
587 */
588 object *
589 get_archetype (const char *name)
590 {
591 return archetype::get (name);
592 }
593
594 object *
595 archetype::get (const char *name)
596 {
597 archetype *at = find (name);
598
599 if (!at)
600 return create_singularity (name);
601
602 return at->instance ();
603 }
604
605 /*
606 * Returns the first archetype using the given type.
607 * Used in treasure-generation.
608 */
609 static archetype *
610 type_to_archetype (int type)
611 {
612 for_all_archetypes (at)
613 if (at->type == type && at->head_ () != at)
614 return at;
615
616 return 0;
617 }
618
619 /*
620 * Returns a new object copied from the first archetype matching
621 * the given type.
622 * Used in treasure-generation.
623 */
624 object *
625 clone_arch (int type)
626 {
627 archetype *at = type_to_archetype (type);
628
629 if (!at)
630 {
631 LOG (llevError, "Can't clone archetype %d\n", type);
632 return 0;
633 }
634
635 return at->instance ();
636 }
637
638 /*
639 * member: make instance from class
640 */
641 object *
642 object_create_arch (archetype *at)
643 {
644 object *op, *prev = 0, *head = 0;
645
646 while (at)
647 {
648 op = at->instance ();
649
650 op->x = at->x;
651 op->y = at->y;
652
653 if (head)
654 op->head = head, prev->more = op;
655
656 if (!head)
657 head = op;
658
659 prev = op;
660 at = (archetype *)at->more;
661 }
662
663 return head;
664 }
665
666 //-GPL
667