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