1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
8 | * Crossfire TRT is free software: you can redistribute it and/or modify |
9 | * under the terms of the GNU General Public License as published by the Free |
9 | * it under the terms of the GNU General Public License as published by |
10 | * Software Foundation; either version 2 of the License, or (at your option) |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * any later version. |
11 | * (at your option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, but |
13 | * This program is distributed in the hope that it will be useful, |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License along |
18 | * You should have received a copy of the GNU General Public License |
19 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
21 | * |
20 | * |
22 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #include <cassert> |
24 | #include <cassert> |
… | |
… | |
37 | * return an object. Perhaps those should be called 'archob' functions |
36 | * return an object. Perhaps those should be called 'archob' functions |
38 | * to denote they return an object derived from the archetype. |
37 | * to denote they return an object derived from the archetype. |
39 | * MSW 2003-04-29 |
38 | * MSW 2003-04-29 |
40 | */ |
39 | */ |
41 | |
40 | |
42 | bool loading_arch; // ugly flag to object laoder etc. to suppress/request special processing |
41 | archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing |
43 | |
42 | |
44 | // the hashtable |
43 | // the hashtable |
45 | typedef std::tr1::unordered_map |
44 | typedef std::tr1::unordered_map |
46 | < |
45 | < |
47 | const char *, |
46 | const char *, |
… | |
… | |
69 | find_archetype_by_object_name (const char *name) |
68 | find_archetype_by_object_name (const char *name) |
70 | { |
69 | { |
71 | shstr_cmp name_cmp (name); |
70 | shstr_cmp name_cmp (name); |
72 | |
71 | |
73 | for_all_archetypes (at) |
72 | for_all_archetypes (at) |
74 | if (at->archname == name_cmp) |
73 | if (at->name == name_cmp) |
75 | return at; |
74 | return at; |
76 | |
75 | |
77 | return 0; |
76 | return 0; |
78 | } |
77 | } |
79 | |
78 | |
… | |
… | |
86 | find_archetype_by_object_type_name (int type, const char *name) |
85 | find_archetype_by_object_type_name (int type, const char *name) |
87 | { |
86 | { |
88 | shstr_cmp name_cmp (name); |
87 | shstr_cmp name_cmp (name); |
89 | |
88 | |
90 | for_all_archetypes (at) |
89 | for_all_archetypes (at) |
91 | if (at->archname == name_cmp && at->type == type) |
90 | if (at->name == name_cmp && at->type == type) |
92 | return at; |
91 | return at; |
93 | |
92 | |
94 | return 0; |
93 | return 0; |
95 | } |
94 | } |
96 | |
95 | |
… | |
… | |
157 | } |
156 | } |
158 | |
157 | |
159 | return create_singularity (name); |
158 | return create_singularity (name); |
160 | } |
159 | } |
161 | |
160 | |
162 | /* This is a subset of the parse_id command. Basically, name can be |
161 | /* This is a subset of the parse_id command. Basically, name can be |
163 | * a string seperated lists of things to match, with certain keywords. |
162 | * a string seperated lists of things to match, with certain keywords. |
164 | * pl is the player (only needed to set count properly) |
163 | * pl is the player (only needed to set count properly) |
165 | * op is the item we are trying to match. Calling function takes care |
164 | * op is the item we are trying to match. Calling function takes care |
166 | * of what action might need to be done and if it is valid |
165 | * of what action might need to be done and if it is valid |
167 | * (pickup, drop, etc.) Return NONZERO if we have a match. A higher |
166 | * (pickup, drop, etc.) Return NONZERO if we have a match. A higher |
168 | * value means a better match. 0 means no match. |
167 | * value means a better match. 0 means no match. |
169 | * |
168 | * |
170 | * Brief outline of the procedure: |
169 | * Brief outline of the procedure: |
171 | * We take apart the name variable into the individual components. |
170 | * We take apart the name variable into the individual components. |
172 | * cases for 'all' and unpaid are pretty obvious. |
171 | * cases for 'all' and unpaid are pretty obvious. |
173 | * Next, we check for a count (either specified in name, or in the |
172 | * Next, we check for a count (either specified in name, or in the |
174 | * player object.) |
173 | * player object.) |
175 | * If count is 1, make a quick check on the name. |
174 | * If count is 1, make a quick check on the name. |
176 | * IF count is >1, we need to make plural name. Return if match. |
175 | * IF count is >1, we need to make plural name. Return if match. |
177 | * Last, make a check on the full name. |
176 | * Last, make a check on the full name. |
178 | */ |
177 | */ |
179 | int |
178 | int |
180 | item_matched_string (object *pl, object *op, const char *name) |
179 | item_matched_string (object *pl, object *op, const char *name) |
181 | { |
180 | { |
182 | char *cp, local_name[MAX_BUF]; |
181 | char *cp, local_name[MAX_BUF]; |
183 | int count, retval = 0; |
182 | int count, retval = 0; |
… | |
… | |
332 | { |
331 | { |
333 | LOG (llevError, "null archetype requested\n"); |
332 | LOG (llevError, "null archetype requested\n"); |
334 | name = "(null)"; |
333 | name = "(null)"; |
335 | } |
334 | } |
336 | |
335 | |
|
|
336 | if (loading_arch && !strcmp (&loading_arch->archname, name)) |
|
|
337 | return loading_arch; |
|
|
338 | |
337 | archetype *at = find (name); |
339 | archetype *at = find (name); |
338 | |
340 | |
339 | if (loading_arch && at && !at->stub) |
341 | if (!at) |
340 | { |
|
|
341 | at->unlink (); |
|
|
342 | at = 0; |
|
|
343 | } |
|
|
344 | |
|
|
345 | if (!at || (!at->stub && loading_arch)) |
|
|
346 | { |
342 | { |
347 | at = new archetype (name); |
343 | at = new archetype (name); |
348 | at->link (); |
344 | at->link (); |
349 | } |
345 | } |
350 | |
346 | |
… | |
… | |
353 | |
349 | |
354 | archetype * |
350 | archetype * |
355 | archetype::read (object_thawer &f) |
351 | archetype::read (object_thawer &f) |
356 | { |
352 | { |
357 | assert (f.kw == KW_object); |
353 | assert (f.kw == KW_object); |
358 | |
|
|
359 | loading_arch = true; // hack to tell parse_kv et al. to behave |
|
|
360 | |
354 | |
361 | std::vector<archetype *> parts; |
355 | std::vector<archetype *> parts; |
362 | |
356 | |
363 | coroapi::cede_to_tick_every (10); |
357 | coroapi::cede_to_tick_every (10); |
364 | |
358 | |
… | |
… | |
388 | |
382 | |
389 | f.next (); |
383 | f.next (); |
390 | } |
384 | } |
391 | #endif |
385 | #endif |
392 | |
386 | |
|
|
387 | loading_arch = at; // hack to tell parse_kv et al. to behave |
|
|
388 | bool parse_ok = at->parse_kv (f); |
|
|
389 | loading_arch = 0; |
|
|
390 | |
393 | if (!at->parse_kv (f)) |
391 | if (!parse_ok) |
394 | goto fail; |
392 | goto fail; |
395 | |
393 | |
|
|
394 | loading_arch = at; // hack to tell parse_kv et al. to behave |
396 | at->post_load_check (); |
395 | at->post_load_check (); |
|
|
396 | loading_arch = 0; |
397 | |
397 | |
398 | parts.push_back (at); |
398 | parts.push_back (at); |
399 | |
399 | |
400 | if (f.kw != KW_more) |
400 | if (f.kw != KW_more) |
401 | break; |
401 | break; |
… | |
… | |
407 | f.parse_error ("more object"); |
407 | f.parse_error ("more object"); |
408 | goto fail; |
408 | goto fail; |
409 | } |
409 | } |
410 | } |
410 | } |
411 | |
411 | |
412 | loading_arch = false; |
|
|
413 | |
|
|
414 | { |
412 | { |
415 | auto (at, parts.begin ()); |
413 | auto (at, parts.begin ()); |
416 | |
414 | |
417 | archetype *new_head = parts.front (); |
415 | archetype *new_head = parts.front (); |
418 | archetype *old_head = find (new_head->archname); |
416 | archetype *old_head = find (new_head->archname); |
419 | |
417 | |
420 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
418 | if (old_head && !old_head->is_head ()) |
421 | fprintf (stderr, "%d: %s\n", at - parts.begin(), &(*at)->archname);//D |
419 | { |
|
|
420 | LOG (llevError, "%s: unable to overwrite non-head archetype '%s' with head archetype, skipping.\n", |
|
|
421 | &new_head->archname, &old_head->archname); |
|
|
422 | goto fail; |
|
|
423 | } |
422 | |
424 | |
423 | // check that all archetypes belong to the same old object or are new |
425 | // check that all archetypes belong to the same old object or are new |
424 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
426 | for (auto (at, parts.begin ()); at != parts.end (); ++at) |
425 | { |
427 | { |
426 | archetype *new_part = *at; |
428 | archetype *new_part = *at; |
427 | archetype *old_part = find (new_part->archname); |
429 | archetype *old_part = find (new_part->archname); |
428 | |
430 | |
429 | if (old_part && old_part->head_ () != old_head) |
431 | if (old_part && old_part->head_ () != old_head) |
430 | { |
432 | { |
431 | fprintf (stderr, "nhn %s\n", &new_head->archname);//D |
|
|
432 | fprintf (stderr, "old_head %p,%p new_head %p,%p ph %p\n", old_head, old_part, new_head, new_part, old_part->head_()); |
|
|
433 | LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
433 | LOG (llevError, "%s: unable to overwrite archetype '%s' with archetype of different object, skipping.\n", |
434 | &new_part->archname, &((archetype *)old_part->head_ ())->archname); |
434 | &new_part->archname, &((archetype *)old_part->head_ ())->archname); |
435 | goto fail; |
435 | goto fail; |
436 | } |
436 | } |
437 | } |
437 | } |
438 | |
438 | |
439 | fprintf (stderr, "oh %p %s\n", old_head, &new_head->archname);//D |
|
|
440 | |
|
|
441 | // deactivate existing archetype |
439 | // deactivate existing archetype |
442 | for (archetype *at = old_head; at; at = (archetype *)at->more) |
440 | for (archetype *at = old_head; at; at = (archetype *)at->more) |
443 | at->unlink (); |
441 | at->unlink (); |
444 | |
442 | |
445 | // reassemble new chain |
443 | // assemble new chain |
446 | new_head->min_x = new_head->max_x = new_head->x; |
444 | new_head->min_x = new_head->max_x = new_head->x; |
447 | new_head->min_y = new_head->max_y = new_head->y; |
445 | new_head->min_y = new_head->max_y = new_head->y; |
448 | |
446 | |
449 | archetype *less = new_head; |
447 | archetype *less = new_head; |
450 | archetype *head = 0; |
|
|
451 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
448 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
452 | { |
449 | { |
453 | archetype *at = *p; |
450 | archetype *at = *p; |
|
|
451 | |
|
|
452 | // some flags get inherited formt he head (probably a lot more) |
|
|
453 | // doing it here doesn't feel too cozy, but it allows code |
|
|
454 | // to ignore head checks for these flags, which saves time |
|
|
455 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
|
|
456 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
|
|
457 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
|
|
458 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
454 | |
459 | |
455 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
460 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
456 | if (at->y < new_head->min_y) new_head->min_y = at->y; |
461 | if (at->y < new_head->min_y) new_head->min_y = at->y; |
457 | if (at->x > new_head->max_x) new_head->max_x = at->x; |
462 | if (at->x > new_head->max_x) new_head->max_x = at->x; |
458 | if (at->y > new_head->max_y) new_head->max_y = at->y; |
463 | if (at->y > new_head->max_y) new_head->max_y = at->y; |
459 | |
464 | |
460 | at->head = head; head = new_head; |
465 | at->head = new_head; |
461 | less->more = at; less = at; |
466 | less->more = at; |
|
|
467 | less = at; |
462 | } |
468 | } |
463 | |
469 | |
464 | // now activate it |
470 | // now activate it |
465 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
471 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
466 | { |
472 | { |
… | |
… | |
474 | |
480 | |
475 | fail: |
481 | fail: |
476 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
482 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
477 | (*p)->destroy (true); |
483 | (*p)->destroy (true); |
478 | |
484 | |
479 | loading_arch = false; |
|
|
480 | return 0; |
485 | return 0; |
481 | } |
486 | } |
482 | |
487 | |
483 | /* |
488 | /* |
484 | * Initialize global archtype pointers: |
489 | * Initialize global archtype pointers: |
… | |
… | |
520 | * an object, and never NULL. |
525 | * an object, and never NULL. |
521 | */ |
526 | */ |
522 | object * |
527 | object * |
523 | create_singularity (const char *name) |
528 | create_singularity (const char *name) |
524 | { |
529 | { |
525 | object *op; |
530 | LOG (llevError | logBacktrace, "FATAL: creating singularity for '%s'.\n", name); |
|
|
531 | |
|
|
532 | if (!strcmp (name, "bug")) |
|
|
533 | abort (); |
|
|
534 | |
526 | char buf[MAX_BUF]; |
535 | char buf[MAX_BUF]; |
|
|
536 | sprintf (buf, "bug, please report (%s)", name); |
527 | |
537 | |
528 | sprintf (buf, "%s (%s)", ARCH_SINGULARITY, name); |
538 | object *op = get_archetype ("bug"); |
529 | op = object::create (); |
|
|
530 | op->name = op->name_pl = buf; |
539 | op->name = op->name_pl = buf; |
531 | SET_FLAG (op, FLAG_NO_PICK); |
540 | |
532 | return op; |
541 | return op; |
533 | } |
542 | } |
534 | |
543 | |
535 | /* |
544 | /* |
536 | * Finds which archetype matches the given name, and returns a new |
545 | * Finds which archetype matches the given name, and returns a new |