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

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify
9 * 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 *
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 GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 #include <cassert>
25
26 #include <global.h>
27 #include <funcpoint.h>
28 #include <loader.h>
29
30 #include <tr1/functional>
31 #include <tr1/unordered_map>
32
33 archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing
34 archetype *archetype::empty;
35
36 // the hashtable
37 typedef std::tr1::unordered_map
38 <
39 const char *,
40 arch_ptr,
41 str_hash,
42 str_equal,
43 slice_allocator< std::pair<const char *const, arch_ptr> >
44 > HT;
45
46 static HT ht (5000);
47 archvec archetypes;
48
49 // 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 /**
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 archetype *
66 find_archetype_by_object_name (const char *name)
67 {
68 shstr_cmp name_cmp (name);
69
70 for_all_archetypes (at)
71 if (at->name == name_cmp)
72 return at;
73
74 return 0;
75 }
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 archetype *
83 find_archetype_by_object_type_name (int type, const char *name)
84 {
85 shstr_cmp name_cmp (name);
86
87 for_all_archetypes (at)
88 if (at->name == name_cmp && at->type == type)
89 return at;
90
91 return 0;
92 }
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 object *
100 get_archetype_by_skill_name (const char *skill, int type)
101 {
102 shstr_cmp skill_cmp (skill);
103
104 for_all_archetypes (at)
105 if (at->skill == skill_cmp && (type == -1 || type == at->type))
106 return arch_to_object (at);
107
108 return 0;
109 }
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 archetype *
119 get_archetype_by_type_subtype (int type, int subtype)
120 {
121 for_all_archetypes (at)
122 if ((type == -1 || type == at->type) && (subtype == -1 || subtype == at->subtype))
123 return at;
124
125 return 0;
126 }
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 object *
141 get_archetype_by_object_name (const char *name)
142 {
143 char tmpname[MAX_BUF];
144 int i;
145
146 assign (tmpname, name);
147
148 for (i = strlen (tmpname); i > 0; i--)
149 {
150 tmpname[i] = 0;
151
152 if (archetype *at = find_archetype_by_object_name (tmpname))
153 return arch_to_object (at);
154 }
155
156 return create_singularity (name);
157 }
158
159 /* 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 int
177 item_matched_string (object *pl, object *op, const char *name)
178 {
179 char *cp, local_name[MAX_BUF];
180 int count, retval = 0;
181
182 assign (local_name, name); /* strtok is destructive to name */
183
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 retval = 6;
256 else if (count == 1 && !strcasecmp (op->name, cp))
257 retval = 6;
258 /* 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
270 return retval;
271 }
272 }
273
274 return 0;
275 }
276
277 archetype::archetype (const char *name)
278 {
279 arch = this;
280 this->archname = this->name = this->name_pl = name;
281 }
282
283 archetype::~archetype ()
284 {
285 unlink ();
286 }
287
288 void
289 archetype::link ()
290 {
291 ht.insert (std::make_pair (archname, this));
292
293 if (!archetypes.contains (this))
294 archetypes.insert (this);
295 }
296
297 void
298 archetype::unlink ()
299 {
300 ht.erase (archname);
301
302 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 archetype::read (object_thawer &f)
326 {
327 assert (f.kw == KW_object);
328
329 std::vector<archetype *> parts;
330
331 coroapi::cede_to_tick ();
332
333 for (;;)
334 {
335 archetype *at = new archetype (f.get_str ());
336
337 f.next ();
338
339 #if 0
340 // 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 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 &at->archname, f.get_str ());
351
352 f.next ();
353 }
354 #endif
355
356 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 goto fail;
362
363 loading_arch = at; // hack to tell parse_kv et al. to behave
364 at->post_load_check ();
365 loading_arch = 0;
366
367 parts.push_back (at);
368
369 if (f.kw != KW_more)
370 break;
371
372 f.next ();
373
374 if (f.kw != KW_object)
375 {
376 f.parse_error ("more object");
377 goto fail;
378 }
379 }
380
381 {
382 auto (at, parts.begin ());
383
384 archetype *new_head = parts.front ();
385 archetype *old_head = find (new_head->archname);
386
387 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
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 {
397 archetype *new_part = *at;
398 archetype *old_part = find (new_part->archname);
399
400 if (old_part && old_part->head_ () != old_head)
401 {
402 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 goto fail;
405 }
406 }
407
408 // assemble new chain
409 new_head->min_x = new_head->max_x = new_head->x;
410 new_head->min_y = new_head->max_y = new_head->y;
411
412 archetype *less = new_head;
413 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
414 {
415 archetype *at = *p;
416
417 // 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 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
430 at->head = new_head;
431 less->more = at;
432 less = at;
433 }
434
435 postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ());
436
437 return new_head;
438 }
439
440 fail:
441 for (auto (p, parts.begin ()); p != parts.end (); ++p)
442 (*p)->destroy (true);
443
444 return 0;
445 }
446
447 void
448 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 {
457 // 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 }
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 object *
492 arch_to_object (archetype *at)
493 {
494 if (!at)
495 {
496 LOG (llevError, "Couldn't find archetype.\n");
497 return 0;
498 }
499
500 object *op = at->clone ();
501 op->arch = at;
502 op->instantiate ();
503
504 return op;
505 }
506
507 object *
508 archetype::instance ()
509 {
510 return arch_to_object (this);
511 }
512
513 /*
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 object *
520 create_singularity (const char *name)
521 {
522 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
523
524 if (!strcmp (name, "bug"))
525 abort ();
526
527 char buf[MAX_BUF];
528 sprintf (buf, "bug, please report (%s)", name);
529
530 object *op = get_archetype ("bug");
531 op->name = op->name_pl = buf;
532
533 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 object *
541 get_archetype (const char *name)
542 {
543 archetype *at = archetype::find (name);
544
545 if (!at)
546 return create_singularity (name);
547
548 return arch_to_object (at);
549 }
550
551 /*
552 * Returns the first archetype using the given type.
553 * Used in treasure-generation.
554 */
555 archetype *
556 type_to_archetype (int type)
557 {
558 for_all_archetypes (at)
559 if (at->type == type && at->head_ () != at)
560 return at;
561
562 return 0;
563 }
564
565 /*
566 * Returns a new object copied from the first archetype matching
567 * the given type.
568 * Used in treasure-generation.
569 */
570 object *
571 clone_arch (int type)
572 {
573 archetype *at;
574
575 if ((at = type_to_archetype (type)) == NULL)
576 {
577 LOG (llevError, "Can't clone archetype %d\n", type);
578 return 0;
579 }
580
581 object *op = at->clone ();
582 op->instantiate ();
583 return op;
584 }
585
586 /*
587 * member: make instance from class
588 */
589 object *
590 object_create_arch (archetype *at)
591 {
592 object *op, *prev = 0, *head = 0;
593
594 while (at)
595 {
596 op = arch_to_object (at);
597
598 op->x = at->x;
599 op->y = at->y;
600
601 if (head)
602 op->head = head, prev->more = op;
603
604 if (!head)
605 head = op;
606
607 prev = op;
608 at = (archetype *)at->more;
609 }
610
611 return head;
612 }
613