ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.91
Committed: Tue Oct 27 01:38:39 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.90: +10 -8 lines
Log Message:
*** empty log message ***

File Contents

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