ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
Revision: 1.74
Committed: Sun Apr 20 05:24:55 2008 UTC (16 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.73: +54 -14 lines
Log Message:
- implement archetype gc
- implement "proper" refcounting for arches
- serialise resource file loads
- implement memory poisoning
- minor cleanups

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 loader etc. to suppress/request special processing
34 arch_ptr 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 (10000);
47 archvec archetypes;
48 static unordered_vector<archetype *> allarch;
49 static int dirtycnt;
50
51 // 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 /**
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 archetype *
68 find_archetype_by_object_name (const char *name)
69 {
70 shstr_cmp name_cmp (name);
71
72 for_all_archetypes (at)
73 if (at->name == name_cmp)
74 return at;
75
76 return 0;
77 }
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 archetype *
85 find_archetype_by_object_type_name (int type, const char *name)
86 {
87 shstr_cmp name_cmp (name);
88
89 for_all_archetypes (at)
90 if (at->name == name_cmp && at->type == type)
91 return at;
92
93 return 0;
94 }
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 object *
102 get_archetype_by_skill_name (const char *skill, int type)
103 {
104 shstr_cmp skill_cmp (skill);
105
106 for_all_archetypes (at)
107 if (at->skill == skill_cmp && (type == -1 || type == at->type))
108 return arch_to_object (at);
109
110 return 0;
111 }
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 archetype *
121 get_archetype_by_type_subtype (int type, int subtype)
122 {
123 for_all_archetypes (at)
124 if ((type == -1 || type == at->type) && (subtype == -1 || subtype == at->subtype))
125 return at;
126
127 return 0;
128 }
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 * doesn't malloc it each time - not that this function is used much,
140 * but it otherwise had a big memory leak.
141 */
142 object *
143 get_archetype_by_object_name (const char *name)
144 {
145 char tmpname[MAX_BUF];
146 int i;
147
148 assign (tmpname, name);
149
150 for (i = strlen (tmpname); i > 0; i--)
151 {
152 tmpname[i] = 0;
153
154 if (archetype *at = find_archetype_by_object_name (tmpname))
155 return arch_to_object (at);
156 }
157
158 return create_singularity (name);
159 }
160
161 /* This is a subset of the parse_id command. Basically, name can be
162 * a string seperated lists of things to match, with certain keywords.
163 * pl is the player (only needed to set count properly)
164 * op is the item we are trying to match. Calling function takes care
165 * of what action might need to be done and if it is valid
166 * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
167 * value means a better match. 0 means no match.
168 *
169 * Brief outline of the procedure:
170 * We take apart the name variable into the individual components.
171 * cases for 'all' and unpaid are pretty obvious.
172 * Next, we check for a count (either specified in name, or in the
173 * player object.)
174 * If count is 1, make a quick check on the name.
175 * IF count is >1, we need to make plural name. Return if match.
176 * Last, make a check on the full name.
177 */
178 int
179 item_matched_string (object *pl, object *op, const char *name)
180 {
181 char *cp, local_name[MAX_BUF];
182 int count, retval = 0;
183
184 assign (local_name, name); /* strtok is destructive to name */
185
186 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
187 {
188 while (cp[0] == ' ')
189 ++cp; /* get rid of spaces */
190
191 /* LOG(llevDebug,"Trying to match %s\n", cp); */
192 /* All is a very generic match - low match value */
193 if (!strcmp (cp, "all"))
194 return 1;
195
196 /* unpaid is a little more specific */
197 if (!strcmp (cp, "unpaid") && QUERY_FLAG (op, FLAG_UNPAID))
198 return 2;
199 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 if (!strcasecmp (cp, query_name (op)))
231 retval = 20;
232 else if (!strcasecmp (cp, query_short_name (op)))
233 retval = 18;
234 else if (!strcasecmp (cp, query_base_name (op, 0)))
235 retval = 16;
236 else if (!strcasecmp (cp, query_base_name (op, 1)))
237 retval = 16;
238 else if (op->custom_name && !strcasecmp (cp, op->custom_name))
239 retval = 15;
240 else if (!strncasecmp (cp, query_base_name (op, 0), strlen (cp)))
241 retval = 14;
242 else if (!strncasecmp (cp, query_base_name (op, 1), strlen (cp)))
243 retval = 14;
244 /* Do substring checks, so things like 'Str+1' will match.
245 * retval of these should perhaps be lower - they are lower
246 * then the specific strcasecmp aboves, but still higher than
247 * some other match criteria.
248 */
249 else if (strstr (query_base_name (op, 1), cp))
250 retval = 12;
251 else if (strstr (query_base_name (op, 0), cp))
252 retval = 12;
253 else if (strstr (query_short_name (op), cp))
254 retval = 12;
255 /* Check against plural/non plural based on count. */
256 else if (count > 1 && !strcasecmp (cp, op->name_pl))
257 retval = 6;
258 else if (count == 1 && !strcasecmp (op->name, cp))
259 retval = 6;
260 /* base name matched - not bad */
261 else if (strcasecmp (cp, op->name) == 0 && !count)
262 retval = 4;
263 /* Check for partial custom name, but give a real low priority */
264 else if (op->custom_name && strstr (op->custom_name, cp))
265 retval = 3;
266
267 if (retval)
268 {
269 if (pl->type == PLAYER)
270 pl->contr->count = count;
271
272 return retval;
273 }
274 }
275
276 return 0;
277 }
278
279 archetype::archetype (const char *name)
280 {
281 arch = this;
282 this->archname = this->name = this->name_pl = name;
283 }
284
285 archetype::~archetype ()
286 {
287 unlink ();
288 }
289
290 void
291 archetype::link ()
292 {
293 if (!archetypes.contains (this))
294 {
295 archetypes.insert (this);
296 ht.insert (std::make_pair (archname, this));
297 }
298 }
299
300 void
301 archetype::unlink ()
302 {
303 if (archetypes.contains (this))
304 {
305 archetypes.erase (this);
306 ht.erase (archname);
307 }
308 }
309
310 /*
311 * Finds, using the hashtable, which archetype matches the given name.
312 * returns a pointer to the found archetype, otherwise NULL.
313 */
314 archetype *
315 archetype::find (const char *name)
316 {
317 if (!name)
318 return 0;
319
320 auto (i, ht.find (name));
321
322 if (i == ht.end ())
323 return 0;
324 else
325 return i->second;
326 }
327
328 archetype *
329 archetype::read (object_thawer &f)
330 {
331 assert (f.kw == KW_object);
332
333 std::vector<archetype *> parts;
334
335 coroapi::cede_to_tick ();
336
337 for (;;)
338 {
339 archetype *at = new archetype (f.get_str ());
340
341 f.next ();
342
343 #if 0
344 // implementing it here in the server does neither allow multiple inheritence
345 // nor does it cleanly "just override". it would allow use in map files, though,
346 // and other resource files dynamically laoded (as opposed to being preprocessed).
347 // not that any of this is relevant as of yet...
348 if (f.kw == KW_inherit)
349 {
350 if (archetype *at = find (f.get_str ()))
351 *op = at->clone;
352 else
353 LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n",
354 &at->archname, f.get_str ());
355
356 f.next ();
357 }
358 #endif
359
360 loading_arch = at; // hack to tell parse_kv et al. to behave
361 bool parse_ok = at->parse_kv (f);
362 loading_arch = 0;
363
364 if (!parse_ok)
365 goto fail;
366
367 loading_arch = at; // hack to tell parse_kv et al. to behave
368 at->post_load_check ();
369 loading_arch = 0;
370
371 parts.push_back (at);
372
373 if (f.kw != KW_more)
374 break;
375
376 f.next ();
377
378 if (f.kw != KW_object)
379 {
380 f.parse_error ("more object");
381 goto fail;
382 }
383 }
384
385 {
386 auto (at, parts.begin ());
387
388 archetype *new_head = parts.front ();
389 archetype *old_head = find (new_head->archname);
390
391 if (old_head && !old_head->is_head ())
392 {
393 LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n",
394 &new_head->archname, &old_head->archname);
395 goto fail;
396 }
397
398 // check that all archetypes belong to the same old object or are new
399 for (auto (at, parts.begin ()); at != parts.end (); ++at)
400 {
401 archetype *new_part = *at;
402 archetype *old_part = find (new_part->archname);
403
404 if (old_part && old_part->head_ () != old_head)
405 {
406 LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n",
407 &new_part->archname, &((archetype *)old_part->head_ ())->archname);
408 goto fail;
409 }
410 }
411
412 // assemble new chain
413 new_head->min_x = new_head->max_x = new_head->x;
414 new_head->min_y = new_head->max_y = new_head->y;
415
416 archetype *less = new_head;
417 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
418 {
419 archetype *at = *p;
420
421 // some flags get inherited from the head (probably a lot more)
422 // doing it here doesn't feel too cozy, but it allows code
423 // to ignore head checks for these flags, which saves time
424 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
425 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
426 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
427 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
428
429 new_head->min_x = min (new_head->min_x, at->x);
430 new_head->min_y = min (new_head->min_y, at->y);
431 new_head->max_x = max (new_head->max_x, at->x);
432 new_head->max_y = max (new_head->max_y, at->y);
433
434 at->head = new_head;
435 less->more = at;
436 less = at;
437 }
438
439 postponed_arch.insert (postponed_arch.end (), parts.begin (), parts.end ());
440
441 return new_head;
442 }
443
444 fail:
445 for (auto (p, parts.begin ()); p != parts.end (); ++p)
446 (*p)->destroy (true);
447
448 return 0;
449 }
450
451 void
452 archetype::postpone_arch_ref (arch_ptr &ref, const_utf8_string other_arch)
453 {
454 ref = 0;
455 postponed_arch_ref.push_back (std::pair<arch_ptr *, shstr>(&ref, shstr (other_arch)));
456 }
457
458 void
459 archetype::commit_load ()
460 {
461 // unlink old archetypes and link in new ones */
462 for (auto (p, postponed_arch.begin ()); p != postponed_arch.end (); ++p)
463 {
464 archetype *at = *p;
465
466 if (archetype *old = find (at->archname))
467 old->unlink ();
468
469 allarch.push_back (at);
470
471 at->link ();
472 ++dirtycnt;
473 }
474
475 postponed_arch.clear ();
476
477 // now resolve arch references
478 for (auto (p, postponed_arch_ref.begin ()); p != postponed_arch_ref.end (); ++p)
479 {
480 arch_ptr *ap = p->first;
481 archetype *at = find (p->second);
482
483 if (!at)
484 LOG (llevError, "unable to resolve postponed arch reference to '%s'", &p->second);
485
486 *ap = at;
487 }
488
489 postponed_arch_ref.clear ();
490
491 empty = find (shstr_empty_archetype);
492 }
493
494 void
495 archetype::gc ()
496 {
497 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
498 dirtycnt = max (0, dirtycnt - cnt);
499
500 do
501 {
502 static int idx;
503
504 if (idx >= allarch.size ())
505 if (idx)
506 idx = 0;
507 else
508 return;
509
510 archetype *at = allarch [idx];
511
512 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
513 ++idx;
514 else
515 {
516 LOG (llevDebug, "garbage collect arch %s", at->debug_desc ());
517 assert (at->arch == at); // verify that refcnt == 1 is truely valid
518 allarch.erase (idx);
519 at->arch = 0;
520 at->more = 0;
521 at->destroy ();
522 }
523 }
524 while (--cnt);
525 }
526
527 /*
528 * Creates and returns a new object which is a copy of the given archetype.
529 * This function returns NULL on failure.
530 */
531 object *
532 arch_to_object (archetype *at)
533 {
534 if (!at)
535 {
536 LOG (llevError, "Couldn't find archetype.\n");
537 return 0;
538 }
539
540 object *op = at->clone ();
541 op->arch = at;
542 op->instantiate ();
543
544 return op;
545 }
546
547 object *
548 archetype::instance ()
549 {
550 return arch_to_object (this);
551 }
552
553 /*
554 * Creates an object. This function is called by get_archetype()
555 * if it fails to find the appropriate archetype.
556 * Thus get_archetype() will be guaranteed to always return
557 * an object, and never NULL.
558 */
559 object *
560 create_singularity (const char *name)
561 {
562 LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name);
563
564 if (!strcmp (name, "bug"))
565 abort ();
566
567 char buf[MAX_BUF];
568 sprintf (buf, "bug, please report (%s)", name);
569
570 object *op = get_archetype ("bug");
571 op->name = op->name_pl = buf;
572
573 return op;
574 }
575
576 /*
577 * Finds which archetype matches the given name, and returns a new
578 * object containing a copy of the archetype.
579 */
580 object *
581 get_archetype (const char *name)
582 {
583 archetype *at = archetype::find (name);
584
585 if (!at)
586 return create_singularity (name);
587
588 return arch_to_object (at);
589 }
590
591 /*
592 * Returns the first archetype using the given type.
593 * Used in treasure-generation.
594 */
595 archetype *
596 type_to_archetype (int type)
597 {
598 for_all_archetypes (at)
599 if (at->type == type && at->head_ () != at)
600 return at;
601
602 return 0;
603 }
604
605 /*
606 * Returns a new object copied from the first archetype matching
607 * the given type.
608 * Used in treasure-generation.
609 */
610 object *
611 clone_arch (int type)
612 {
613 archetype *at;
614
615 if ((at = type_to_archetype (type)) == NULL)
616 {
617 LOG (llevError, "Can't clone archetype %d\n", type);
618 return 0;
619 }
620
621 object *op = at->clone ();
622 op->instantiate ();
623 return op;
624 }
625
626 /*
627 * member: make instance from class
628 */
629 object *
630 object_create_arch (archetype *at)
631 {
632 object *op, *prev = 0, *head = 0;
633
634 while (at)
635 {
636 op = arch_to_object (at);
637
638 op->x = at->x;
639 op->y = at->y;
640
641 if (head)
642 op->head = head, prev->more = op;
643
644 if (!head)
645 head = op;
646
647 prev = op;
648 at = (archetype *)at->more;
649 }
650
651 return head;
652 }
653