ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.101
Committed: Fri Mar 26 01:04:43 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.100: +1 -1 lines
Log Message:
update copyright for up to 2010

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.101 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.100 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992 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    
29 root 1.38 #include <tr1/functional>
30     #include <tr1/unordered_map>
31 elmex 1.1
32 root 1.74 archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
33     arch_ptr archetype::empty;
34 root 1.46
35 root 1.12 // the hashtable
36 root 1.18 typedef std::tr1::unordered_map
37     <
38 root 1.38 const char *,
39 root 1.26 arch_ptr,
40 root 1.38 str_hash,
41     str_equal,
42     slice_allocator< std::pair<const char *const, arch_ptr> >
43 root 1.18 > HT;
44    
45 root 1.74 static HT ht (10000);
46 root 1.62 archvec archetypes;
47 root 1.74 static unordered_vector<archetype *> allarch;
48     static int dirtycnt;
49 root 1.12
50 root 1.73 // 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 root 1.92 //+GPL
56    
57 root 1.93 /*
58 root 1.96 * Creates an object. This function is called by get_archetype ()
59 root 1.93 * 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 root 1.94 static object *
64 root 1.93 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 elmex 1.1 /**
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 root 1.12 archetype *
89     find_archetype_by_object_name (const char *name)
90     {
91 root 1.39 shstr_cmp name_cmp (name);
92 elmex 1.1
93 root 1.63 for_all_archetypes (at)
94 root 1.66 if (at->name == name_cmp)
95 root 1.39 return at;
96 elmex 1.1
97 root 1.39 return 0;
98 elmex 1.1 }
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 root 1.12 archetype *
106     find_archetype_by_object_type_name (int type, const char *name)
107     {
108 root 1.39 shstr_cmp name_cmp (name);
109 elmex 1.1
110 root 1.63 for_all_archetypes (at)
111 root 1.66 if (at->name == name_cmp && at->type == type)
112 root 1.39 return at;
113 elmex 1.1
114 root 1.39 return 0;
115 elmex 1.1 }
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 root 1.12 object *
123     get_archetype_by_skill_name (const char *skill, int type)
124     {
125 root 1.39 shstr_cmp skill_cmp (skill);
126 elmex 1.1
127 root 1.63 for_all_archetypes (at)
128     if (at->skill == skill_cmp && (type == -1 || type == at->type))
129 root 1.80 return at->instance ();
130 root 1.35
131     return 0;
132 elmex 1.1 }
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 root 1.12 archetype *
142     get_archetype_by_type_subtype (int type, int subtype)
143     {
144 root 1.63 for_all_archetypes (at)
145     if ((type == -1 || type == at->type) && (subtype == -1 || subtype == at->subtype))
146 root 1.39 return at;
147 elmex 1.1
148 root 1.39 return 0;
149 elmex 1.1 }
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 root 1.78 * doesn't allocate it each time - not that this function is used much,
161 elmex 1.1 * but it otherwise had a big memory leak.
162     */
163 root 1.12 object *
164     get_archetype_by_object_name (const char *name)
165     {
166 root 1.17 char tmpname[MAX_BUF];
167    
168     assign (tmpname, name);
169 root 1.12
170 root 1.90 for (int i = strlen (tmpname); i > 0; i--)
171 root 1.12 {
172     tmpname[i] = 0;
173 root 1.17
174 root 1.39 if (archetype *at = find_archetype_by_object_name (tmpname))
175 root 1.80 return at->instance ();
176 elmex 1.1 }
177 root 1.17
178 root 1.12 return create_singularity (name);
179 elmex 1.1 }
180    
181 root 1.65 /* 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 root 1.12 int
199 root 1.15 item_matched_string (object *pl, object *op, const char *name)
200 elmex 1.1 {
201 root 1.22 char *cp, local_name[MAX_BUF];
202     int count, retval = 0;
203 root 1.15
204 root 1.40 assign (local_name, name); /* strtok is destructive to name */
205 root 1.12
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") && QUERY_FLAG (op, FLAG_UNPAID))
218     return 2;
219 root 1.90
220 root 1.12 if (!strcmp (cp, "cursed") && QUERY_FLAG (op, FLAG_KNOWN_CURSED) && (QUERY_FLAG (op, FLAG_CURSED) || QUERY_FLAG (op, FLAG_DAMNED)))
221     return 2;
222    
223     if (!strcmp (cp, "unlocked") && !QUERY_FLAG (op, 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 root 1.91 const char *qbn0, *qbn1, *qsn; // query base name/short name caches
252    
253 root 1.12 if (!strcasecmp (cp, query_name (op)))
254     retval = 20;
255 root 1.91 else if (!strcasecmp (cp, qsn = query_short_name (op)))
256 root 1.12 retval = 18;
257 root 1.91 else if (!strcasecmp (cp, qbn0 = query_base_name (op, 0)))
258 root 1.12 retval = 16;
259 root 1.91 else if (!strcasecmp (cp, qbn1 = query_base_name (op, 1)))
260 root 1.12 retval = 16;
261     else if (op->custom_name && !strcasecmp (cp, op->custom_name))
262     retval = 15;
263 root 1.91 else if (!strncasecmp (cp, qbn0, strlen (cp)))
264 root 1.12 retval = 14;
265 root 1.91 else if (!strncasecmp (cp, qbn1, strlen (cp)))
266 root 1.12 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 root 1.91 else if (strstr (qbn1, cp))
273 root 1.12 retval = 12;
274 root 1.91 else if (strstr (qbn0, cp))
275 root 1.12 retval = 12;
276 root 1.91 else if (strstr (qsn, cp))
277 root 1.12 retval = 12;
278     /* Check against plural/non plural based on count. */
279     else if (count > 1 && !strcasecmp (cp, op->name_pl))
280 root 1.23 retval = 6;
281 root 1.12 else if (count == 1 && !strcasecmp (op->name, cp))
282 root 1.23 retval = 6;
283 root 1.12 /* 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 root 1.88 else if (op->custom_name.contains (cp))
288 root 1.12 retval = 3;
289    
290     if (retval)
291     {
292     if (pl->type == PLAYER)
293     pl->contr->count = count;
294 root 1.23
295 root 1.12 return retval;
296 root 1.7 }
297 elmex 1.1 }
298 root 1.23
299 root 1.12 return 0;
300 elmex 1.1 }
301    
302 root 1.92 //-GPL
303    
304 root 1.63 archetype::archetype (const char *name)
305 root 1.12 {
306 root 1.63 arch = this;
307     this->archname = this->name = this->name_pl = name;
308 root 1.46 }
309 root 1.12
310 root 1.46 archetype::~archetype ()
311     {
312 root 1.63 unlink ();
313 elmex 1.1 }
314    
315 root 1.63 void
316     archetype::link ()
317 elmex 1.1 {
318 root 1.63 if (!archetypes.contains (this))
319 root 1.74 {
320     archetypes.insert (this);
321     ht.insert (std::make_pair (archname, this));
322     }
323 elmex 1.1 }
324    
325 root 1.46 void
326 root 1.63 archetype::unlink ()
327     {
328     if (archetypes.contains (this))
329 root 1.74 {
330     archetypes.erase (this);
331     ht.erase (archname);
332     }
333 root 1.63 }
334    
335     /*
336     * Finds, using the hashtable, which archetype matches the given name.
337     * returns a pointer to the found archetype, otherwise NULL.
338     */
339     archetype *
340     archetype::find (const char *name)
341     {
342     if (!name)
343     return 0;
344    
345     auto (i, ht.find (name));
346    
347     if (i == ht.end ())
348     return 0;
349     else
350     return i->second;
351     }
352    
353 root 1.97 void
354     archetype::post_load_check ()
355     {
356     object::post_load_check ();
357    
358     assert (("obj_original MUST NOT be set for archetypes", !flag [FLAG_OBJ_ORIGINAL]));
359     }
360    
361 root 1.63 archetype *
362 root 1.53 archetype::read (object_thawer &f)
363 root 1.12 {
364 root 1.43 assert (f.kw == KW_object);
365    
366 root 1.63 std::vector<archetype *> parts;
367 root 1.12
368 root 1.72 coroapi::cede_to_tick ();
369 root 1.52
370 root 1.49 for (;;)
371 root 1.43 {
372 root 1.73 archetype *at = new archetype (f.get_str ());
373 root 1.63
374 root 1.54 f.next ();
375 root 1.49
376 root 1.56 #if 0
377 root 1.63 // implementing it here in the server does neither allow multiple inheritence
378     // nor does it cleanly "just override". it would allow use in map files, though,
379     // and other resource files dynamically laoded (as opposed to being preprocessed).
380     // not that any of this is relevant as of yet...
381 root 1.55 if (f.kw == KW_inherit)
382     {
383     if (archetype *at = find (f.get_str ()))
384     *op = at->clone;
385     else
386     LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n",
387 root 1.63 &at->archname, f.get_str ());
388 root 1.55
389     f.next ();
390     }
391 root 1.56 #endif
392 root 1.55
393 root 1.69 loading_arch = at; // hack to tell parse_kv et al. to behave
394     bool parse_ok = at->parse_kv (f);
395     loading_arch = 0;
396    
397     if (!parse_ok)
398 root 1.49 goto fail;
399    
400 root 1.69 loading_arch = at; // hack to tell parse_kv et al. to behave
401 root 1.63 at->post_load_check ();
402 root 1.69 loading_arch = 0;
403 root 1.59
404 root 1.63 parts.push_back (at);
405 root 1.12
406 root 1.49 if (f.kw != KW_more)
407     break;
408 root 1.46
409 root 1.43 f.next ();
410 root 1.54
411     if (f.kw != KW_object)
412     {
413     f.parse_error ("more object");
414     goto fail;
415     }
416 root 1.49 }
417 root 1.43
418 root 1.49 {
419 root 1.63 auto (at, parts.begin ());
420 root 1.12
421 root 1.63 archetype *new_head = parts.front ();
422     archetype *old_head = find (new_head->archname);
423    
424 root 1.64 if (old_head && !old_head->is_head ())
425     {
426     LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n",
427     &new_head->archname, &old_head->archname);
428     goto fail;
429     }
430 root 1.63
431     // check that all archetypes belong to the same old object or are new
432     for (auto (at, parts.begin ()); at != parts.end (); ++at)
433 root 1.49 {
434 root 1.63 archetype *new_part = *at;
435     archetype *old_part = find (new_part->archname);
436 root 1.49
437 root 1.63 if (old_part && old_part->head_ () != old_head)
438 root 1.49 {
439 root 1.63 LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n",
440     &new_part->archname, &((archetype *)old_part->head_ ())->archname);
441 root 1.49 goto fail;
442     }
443     }
444    
445 root 1.64 // assemble new chain
446 root 1.99 new_head->max_x = new_head->x;
447 root 1.49
448 root 1.63 archetype *less = new_head;
449 root 1.51 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
450 root 1.49 {
451 root 1.63 archetype *at = *p;
452 root 1.49
453 root 1.74 // some flags get inherited from the head (probably a lot more)
454 root 1.67 // doing it here doesn't feel too cozy, but it allows code
455     // to ignore head checks for these flags, which saves time
456     at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
457     at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
458     at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
459     at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
460    
461 root 1.74 new_head->max_x = max (new_head->max_x, at->x);
462 root 1.49
463 root 1.64 at->head = new_head;
464     less->more = at;
465     less = at;
466 root 1.63 }
467 root 1.49
468 root 1.73 postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ());
469 root 1.53
470 root 1.63 return new_head;
471 root 1.49 }
472 root 1.43
473 root 1.49 fail:
474 root 1.51 for (auto (p, parts.begin ()); p != parts.end (); ++p)
475 root 1.85 (*p)->destroy ();
476 root 1.43
477 root 1.53 return 0;
478 root 1.43 }
479    
480 root 1.53 void
481 root 1.73 archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch)
482     {
483     ref = 0;
484     postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch)));
485     }
486    
487     void
488     archetype::commit_load ()
489 root 1.43 {
490 root 1.73 // unlink old archetypes and link in new ones */
491     for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p)
492     {
493     archetype *at = *p;
494    
495     if (archetype *old = find (at->archname))
496     old->unlink ();
497    
498 root 1.74 allarch.push_back (at);
499    
500 root 1.73 at->link ();
501 root 1.74 ++dirtycnt;
502 root 1.73 }
503    
504     postponed_arch.clear ();
505    
506     // now resolve arch references
507     for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p)
508     {
509     arch_ptr *ap = p->first;
510     archetype *at = find (p->second);
511    
512     if (!at)
513     LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second);
514    
515     *ap = at;
516     }
517    
518     postponed_arch_ref.clear ();
519    
520     empty = find (shstr_empty_archetype);
521 elmex 1.1 }
522    
523 root 1.74 void
524     archetype::gc ()
525     {
526     int cnt = max (1, min (allarch.size () / 128, dirtycnt));
527     dirtycnt = max (0, dirtycnt - cnt);
528    
529     do
530     {
531     static int idx;
532    
533     if (idx >= allarch.size ())
534     if (idx)
535     idx = 0;
536     else
537     return;
538    
539     archetype *at = allarch [idx];
540    
541     if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
542     ++idx;
543     else
544     {
545 root 1.86 LOG (llevDebug, "garbage collect arch %s", &at->archname);
546 root 1.79 assert (at->arch == at); // verify that refcnt == 1 is truly valid
547 root 1.74 allarch.erase (idx);
548 root 1.77
549     // break chain
550     for (object *op = at->head_ (); op; )
551     {
552     object *next = op->more;
553     op->head = 0;
554     op->more = 0;
555     op = next;
556     }
557    
558 root 1.85 at->destroy ();
559 root 1.74 at->arch = 0;
560     }
561     }
562     while (--cnt);
563     }
564    
565 root 1.70 object *
566     archetype::instance ()
567     {
568 root 1.80 object *op = clone ();
569     op->instantiate ();
570     return op;
571 root 1.70 }
572    
573 root 1.92 //+GPL
574    
575 elmex 1.1 /*
576     * Finds which archetype matches the given name, and returns a new
577     * object containing a copy of the archetype.
578     */
579 root 1.12 object *
580     get_archetype (const char *name)
581     {
582 root 1.80 return archetype::get (name);
583 elmex 1.1 }
584    
585 root 1.76 object *
586     archetype::get (const char *name)
587     {
588 root 1.80 archetype *at = find (name);
589    
590     if (!at)
591     return create_singularity (name);
592    
593     return at->instance ();
594 root 1.76 }
595    
596 elmex 1.1 /*
597     * Returns the first archetype using the given type.
598     * Used in treasure-generation.
599     */
600 root 1.95 static archetype *
601 root 1.12 type_to_archetype (int type)
602     {
603 root 1.63 for_all_archetypes (at)
604     if (at->type == type && at->head_ () != at)
605 elmex 1.1 return at;
606 root 1.20
607     return 0;
608 elmex 1.1 }
609    
610     /*
611     * Returns a new object copied from the first archetype matching
612     * the given type.
613     * Used in treasure-generation.
614     */
615 root 1.12 object *
616     clone_arch (int type)
617     {
618 root 1.80 archetype *at = type_to_archetype (type);
619 elmex 1.1
620 root 1.80 if (!at)
621 root 1.12 {
622     LOG (llevError, "Can't clone archetype %d\n", type);
623 root 1.30 return 0;
624 root 1.12 }
625 root 1.20
626 root 1.80 return at->instance ();
627 elmex 1.1 }
628    
629     /*
630     * member: make instance from class
631     */
632 root 1.12 object *
633 root 1.15 object_create_arch (archetype *at)
634 elmex 1.1 {
635 root 1.20 object *op, *prev = 0, *head = 0;
636 elmex 1.1
637 root 1.12 while (at)
638     {
639 root 1.80 op = at->instance ();
640 root 1.63
641     op->x = at->x;
642     op->y = at->y;
643 root 1.20
644 root 1.12 if (head)
645     op->head = head, prev->more = op;
646 root 1.20
647 root 1.12 if (!head)
648     head = op;
649 root 1.20
650 root 1.12 prev = op;
651 root 1.63 at = (archetype *)at->more;
652 elmex 1.1 }
653 root 1.20
654 root 1.63 return head;
655 elmex 1.1 }
656    
657 root 1.92 //-GPL
658