ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.73
Committed: Sun Apr 20 00:44:12 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.72: +45 -59 lines
Log Message:
reloadable archetypes, maybe

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