Debian Codegraph @

common/arch.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file arch.c
16 * All archetype-related functions.
17 * @note
18 * The naming of these functions is really poor - they are all
19 * pretty much named '.._arch_...', but they may more may not
20 * return archetypes. Some make the arch_to_object call, and thus
21 * return an object. Perhaps those should be called 'archob' functions
22 * to denote they return an object derived from the archetype.
23 * MSW 2003-04-29
24 * @todo
25 * make the functions use the same order for parameters (type first, then name, or the opposite).
26 */
27
28 #include "global.h"
29
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "loader.h"
35
36 static void add_arch(archetype *at);
37
38 static archetype *arch_table[ARCHTABLE];
39 static int arch_cmp = 0; /**< How many strcmp's */
40 static int arch_search = 0; /**< How many searches */
41 int arch_init; /**< True if doing arch initialization */
42
43 static void load_archetypes(void);
44
45 /**
46 * This function retrieves an archetype given the name that appears
47 * during the game (for example, "writing pen" instead of "stylus").
48 * It does not use the hashtable system, but browse the whole archlist each time.
49 * I suggest not to use it unless you really need it because of performance issue.
50 * It is currently used by scripting extensions (create-object).
51 * Params:
52 * @param name
53 * the name we're searching for (ex: "writing pen")
54 * @return
55 * the archetype found or NULL if nothing was found
56 */
57 archetype *find_archetype_by_object_name(const char *name) {
58 archetype *at;
59 sstring tmp;
60
61 if (name == NULL)
62 return (archetype *)NULL;
63 tmp = find_string(name);
64 if (!tmp)
65 return NULL;
66
67 for (at = first_archetype; at != NULL; at = at->next) {
68 if (at->clone.name == tmp) {
69 return at;
70 }
71 }
72 return NULL;
73 }
74
75 /**
76 * This function retrieves an archetype by type and name that appears during
77 * the game. It is basically the same as find_archetype_by_object_name()
78 * except that it considers only items of the given type.
79 * @param type
80 * item type we're searching
81 * @param name
82 * the name we're searching for (ex: "writing pen")
83 */
84 archetype *find_archetype_by_object_type_name(int type, const char *name) {
85 archetype *at;
86
87 if (name == NULL)
88 return NULL;
89
90 for (at = first_archetype; at != NULL; at = at->next) {
91 if (at->clone.type == type && strcmp(at->clone.name, name) == 0)
92 return at;
93 }
94
95 return NULL;
96 }
97
98 /**
99 * Retrieves an archetype by skill name and type.
100 * This is a lot like the other get_archetype_ functions, with different options.
101 * @param skill
102 * skill to search for. Must not be NULL.
103 * @param type
104 * item type to search for. -1 means that it doesn't matter.
105 * @return
106 * matching archetype, or NULL if none found.
107 */
108 archetype *get_archetype_by_skill_name(const char *skill, int type) {
109 archetype *at;
110
111 if (skill == NULL)
112 return NULL;
113
114 for (at = first_archetype; at != NULL; at = at->next) {
115 if (((type == -1) || (type == at->clone.type))
116 && (at->clone.skill) && (!strcmp(at->clone.skill, skill)))
117 return at;
118 }
119 return NULL;
120 }
121
122 /**
123 * Retrieves an archetype by type and subtype.
124 * Similiar to other get_archetype_ functions. This returns the first archetype
125 * that matches both the type and subtype. type and subtype
126 * can be -1 to say ignore, but in this case, the match it does
127 * may not be very useful. This function is most useful when
128 * subtypes are known to be unique for a particular type
129 * (eg, skills)
130 * @param type
131 * object type to search for. -1 means any
132 * @param subtype
133 * object subtype to search for. -1 means any
134 * @return
135 * matching archetype, or NULL if none found.
136 */
137 archetype *get_archetype_by_type_subtype(int type, int subtype) {
138 archetype *at;
139
140 for (at = first_archetype; at != NULL; at = at->next) {
141 if (((type == -1) || (type == at->clone.type))
142 && (subtype == -1 || subtype == at->clone.subtype))
143 return at;
144 }
145 return NULL;
146 }
147
148 /**
149 * Creates an object given the name that appears during the game
150 * (for example, "writing pen" instead of "stylus").
151 * @param name
152 * the name we're searching for (ex: "writing pen"), must not be null
153 * @return
154 * a corresponding object if found; a singularity object if not found.
155 * @note
156 * Note by MSW - it appears that it takes the full name and keeps
157 * shortening it until it finds a match. I re-wrote this so that it
158 * doesn't malloc it each time - not that this function is used much,
159 * but it otherwise had a big memory leak.
160 */
161 object *create_archetype_by_object_name(const char *name) {
162 archetype *at;
163 char tmpname[MAX_BUF];
164 size_t i;
165
166 strncpy(tmpname, name, MAX_BUF-1);
167 tmpname[MAX_BUF-1] = 0;
168 for (i = strlen(tmpname); i > 0; i--) {
169 tmpname[i] = 0;
170 at = find_archetype_by_object_name(tmpname);
171 if (at != NULL) {
172 return arch_to_object(at);
173 }
174 }
175 return create_singularity(name);
176 }
177
178 /**
179 * Initialises the internal linked list of archetypes (read from file).
180 * Then the global ::empty_archetype pointer is initialised.
181 * Can be called multiple times, will just return.
182 */
183 void init_archetypes(void) {
184 if (first_archetype != NULL) /* Only do this once */
185 return;
186 arch_init = 1;
187 load_archetypes();
188 arch_init = 0;
189 empty_archetype = find_archetype("empty_archetype");
190 if (empty_archetype == NULL)
191 fatal(SEE_LAST_ERROR);
192 /* init_blocksview();*/
193 }
194
195 /**
196 * Stores debug-information about how efficient the hashtable
197 * used for archetypes has been in the static errmsg array.
198 */
199 void arch_info(object *op) {
200 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_DEBUG,
201 "%d searches and %d strcmp()'s",
202 arch_search, arch_cmp);
203 }
204
205 /**
206 * Initialise the hashtable used by the archetypes.
207 */
208 void clear_archetable(void) {
209 memset((void *)arch_table, 0, ARCHTABLE*sizeof(archetype *));
210 }
211
212 /**
213 * An alternative way to init the hashtable which is slower, but _works_...
214 */
215 static void init_archetable(void) {
216 archetype *at;
217
218 LOG(llevDebug, "arch: setting up archetable\n");
219 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) {
220 if (at->name == NULL) {
221 LOG(llevError, "archetype without name? %s\n", at->clone.name ? at->clone.name : "(no clone name)");
222 abort();
223 }
224 add_arch(at);
225 }
226 }
227
228 /**
229 * Dumps an archetype to buffer.
230 *
231 * @param at
232 * archetype to dump. Must not be NULL.
233 * @param sb
234 * buffer that will contain dumped information.
235 */
236 void dump_arch(archetype *at, StringBuffer *sb) {
237 object_dump(&at->clone, sb);
238 }
239
240 /**
241 * Dumps _all_ archetypes to debug-level output.
242 * If you run crossfire with debug, and enter DM-mode, you can trigger
243 * this with the "dumpallarchetypes" command.
244 */
245 void dump_all_archetypes(void) {
246 archetype *at;
247
248 for (at = first_archetype; at != NULL; at = (at->more == NULL) ? at->next : at->more) {
249 StringBuffer *sb;
250 char *diff;
251
252 sb = stringbuffer_new();
253 dump_arch(at, sb);
254 diff = stringbuffer_finish(sb);
255 LOG(llevDebug, "%s\n", diff);
256 free(diff);
257 }
258 }
259
260 /**
261 * Frees archetype.
262 *
263 * @param at
264 * archetype to free. Pointer becomes invalid after the call.
265 */
266 void free_arch(archetype *at) {
267 if (at->name)
268 free_string(at->name);
269 if (at->clone.name)
270 free_string(at->clone.name);
271 if (at->clone.name_pl)
272 free_string(at->clone.name_pl);
273 if (at->clone.title)
274 free_string(at->clone.title);
275 if (at->clone.race)
276 free_string(at->clone.race);
277 if (at->clone.slaying)
278 free_string(at->clone.slaying);
279 if (at->clone.msg)
280 free_string(at->clone.msg);
281 object_free_key_values(&at->clone);
282 free(at);
283 }
284
285 /**
286 * Frees all memory allocated to archetypes.
287 * After calling this, it's possible to call again init_archetypes() to reload data.
288 */
289 void free_all_archs(void) {
290 archetype *at, *next;
291 int i = 0;
292
293 for (at = first_archetype; at != NULL; at = next) {
294 if (at->more)
295 next = at->more;
296 else
297 next = at->next;
298 free_arch(at);
299 i++;
300 }
301 first_archetype = NULL;
302 /* Reset the hashtable */
303 clear_archetable();
304 LOG(llevDebug, "Freed %d archetypes\n", i);
305 }
306
307 /**
308 * Allocates, initialises and returns the pointer to an archetype structure.
309 * @return
310 * new archetype structure, will never be NULL.
311 * @note
312 * this function will call fatal() if it can't allocate memory.
313 */
314 archetype *get_archetype_struct(void) {
315 archetype *new;
316
317 new = (archetype *)CALLOC(1, sizeof(archetype));
318 if (new == NULL)
319 fatal(OUT_OF_MEMORY);
320 new->next = NULL;
321 new->name = NULL;
322 new->clone.other_arch = NULL;
323 new->clone.name = NULL;
324 new->clone.name_pl = NULL;
325 new->clone.title = NULL;
326 new->clone.race = NULL;
327 new->clone.slaying = NULL;
328 new->clone.msg = NULL;
329 object_clear(&new->clone); /* to initial state other also */
330 CLEAR_FLAG((&new->clone), FLAG_FREED); /* This shouldn't matter, since object_copy() */
331 SET_FLAG((&new->clone), FLAG_REMOVED); /* doesn't copy these flags... */
332 new->head = NULL;
333 new->more = NULL;
334 new->clone.arch = new;
335 return new;
336 }
337
338 /**
339 * Reads/parses the archetype-file, and copies into a linked list
340 * of archetype-structures.
341 * Called through load_archetypes()
342 *
343 * Will discard object in archetypes, those are handled by second_arch_pass().
344 *
345 * @param fp
346 * opened file descriptor which will be used to read the archetypes.
347 */
348 static void first_arch_pass(FILE *fp) {
349 archetype *at, *head = NULL, *last_more = NULL;
350 int i, first = LO_NEWFILE;
351
352 at = get_archetype_struct();
353 first_archetype = at;
354
355 while ((i = load_object(fp, &at->clone, first, 0))) {
356 first = 0;
357 at->clone.speed_left = (float)(-0.1);
358
359 switch (i) {
360 case LL_NORMAL: /* A new archetype, just link it with the previous */
361 if (last_more != NULL)
362 last_more->next = at;
363 if (head != NULL)
364 head->next = at;
365 head = last_more = at;
366 at->tail_x = 0;
367 at->tail_y = 0;
368 break;
369
370 case LL_MORE: /* Another part of the previous archetype, link it correctly */
371 at->head = head;
372 at->clone.head = &head->clone;
373 if (last_more != NULL) {
374 last_more->more = at;
375 last_more->clone.more = &at->clone;
376 }
377 last_more = at;
378
379 /* Set FLAG_MONSTER throughout parts if head has it */
380 if (QUERY_FLAG(&head->clone, FLAG_MONSTER)) {
381 SET_FLAG(&at->clone, FLAG_MONSTER);
382 }
383
384 /* If this multipart image is still composed of individual small
385 * images, don't set the tail_.. values. We can't use them anyways,
386 * and setting these to zero makes the map sending to the client much
387 * easier as just looking at the head, we know what to do.
388 */
389 if (at->clone.face != head->clone.face) {
390 head->tail_x = 0;
391 head->tail_y = 0;
392 } else {
393 if (at->clone.x > head->tail_x)
394 head->tail_x = at->clone.x;
395 if (at->clone.y > head->tail_y)
396 head->tail_y = at->clone.y;
397 }
398 break;
399 }
400
401 at = get_archetype_struct();
402 }
403 at->clone.arch = NULL; /* arch is checked for temporary archetypes if not NULL. */
404 free(at);
405 }
406
407 /**
408 * Reads the archetype file once more, and links all pointers between
409 * archetypes and treasure lists. Must be called after first_arch_pass().
410 *
411 * This also handles putting items in inventory when defined in archetype.
412 *
413 * @param fp
414 * file fron which to read. Won't be rewinded.
415 */
416 static void second_arch_pass(FILE *fp) {
417 char buf[MAX_BUF], *variable = buf, *argument, *cp;
418 archetype *at = NULL, *other;
419 object *inv;
420
421 while (fgets(buf, MAX_BUF, fp) != NULL) {
422 if (*buf == '#')
423 continue;
424 if ((argument = strchr(buf, ' ')) != NULL) {
425 *argument = '\0', argument++;
426 cp = argument+strlen(argument)-1;
427 while (isspace(*cp)) {
428 *cp = '\0';
429 cp--;
430 }
431 }
432 if (!strcmp("Object", variable)) {
433 if ((at = find_archetype(argument)) == NULL)
434 {
435 LOG(llevError, "Fatal: failed to find arch %s in second_arch_pass\n", argument);
436 fatal(ARCHETYPE_ISSUE);
437 }
438 } else if (!strcmp("other_arch", variable)) {
439 if (at != NULL && at->clone.other_arch == NULL) {
440 if ((other = find_archetype(argument)) == NULL)
441 LOG(llevError, "Warning: failed to find other_arch %s\n", argument);
442 else if (at != NULL)
443 at->clone.other_arch = other;
444 }
445 } else if (!strcmp("randomitems", variable)) {
446 if (at != NULL) {
447 treasurelist *tl = find_treasurelist(argument);
448 if (tl == NULL)
449 LOG(llevError, "Failed to link treasure to arch (%s): %s\n", at->name, argument);
450 else
451 at->clone.randomitems = tl;
452 }
453 } else if (!strcmp("arch", variable)) {
454 inv = create_archetype(argument);
455 load_object(fp, inv, LO_LINEMODE, 0);
456 if (at) {
457 object_insert_in_ob(inv, &at->clone);
458 } else {
459 LOG(llevError, "Got an arch %s not inside an Object.\n", argument);
460 object_free_drop_inventory(inv);
461 }
462 }
463 }
464 }
465
466 /**
467 * Check all generators have the other_arch set or something in inventory.
468 */
469 void check_generators(void) {
470 const archetype *at;
471 int abort = 0;
472
473 for (at = first_archetype; at != NULL; at = at->next) {
474 if (!QUERY_FLAG(&at->clone, FLAG_GENERATOR))
475 continue;
476
477 if (!QUERY_FLAG(&at->clone, FLAG_CONTENT_ON_GEN) && at->clone.other_arch == NULL) {
478 LOG(llevError, "Fatal: %s is generator without content_on_gen but lacks other_arch.\n", at->name);
479 abort = 1;
480 continue;
481 }
482 if (QUERY_FLAG(&at->clone, FLAG_CONTENT_ON_GEN) && at->clone.inv == NULL) {
483 LOG(llevError, "Fatal: %s is generator with content_on_gen but lacks inventory.\n", at->name);
484 abort = 1;
485 continue;
486 }
487 }
488
489 if (abort)
490 fatal(SEE_LAST_ERROR);
491 }
492
493 /**
494 * This checks all summonable items for move_type and other things.
495 * Will call fatal() if an error is found.
496 */
497 void check_summoned(void) {
498 const archetype *at;
499
500 for (at = first_archetype; at != NULL; at = at->next) {
501 if (at->clone.type == SPELL && at->clone.subtype == SP_SUMMON_GOLEM && at->clone.other_arch) {
502 if (at->clone.other_arch->clone.move_type == 0) {
503 LOG(llevError, "Summonable archetype %s [%s] has no move_type defined!\n", at->clone.other_arch->name, at->clone.other_arch->clone.name);
504 fatal(SEE_LAST_ERROR);
505 }
506 }
507 }
508 }
509
510 /**
511 * This ensures all spells have a skill defined, calling fatal() if any error was found.
512 */
513 static void check_spells(void) {
514 int abort = 0;
515 const archetype *at;
516
517 for (at = first_archetype; at != NULL; at = at->next) {
518 if (at->clone.type == SPELL && at->clone.skill == NULL) {
519 LOG(llevError, "Spell archetype %s [%s] has no skill defined!\n", at->name, at->clone.name);
520 abort = 1;
521 }
522 }
523 if (abort)
524 fatal(SEE_LAST_ERROR);
525 }
526
527 /**
528 * Loads all archetypes and treasures.
529 * First initialises the archtype hash-table (init_archetable()).
530 * Reads and parses the archetype file (with the first and second-pass
531 * functions).
532 * Then initialises treasures by calling load_treasures().
533 */
534 static void load_archetypes(void) {
535 FILE *fp;
536 char filename[MAX_BUF];
537
538 snprintf(filename, sizeof(filename), "%s/%s", settings.datadir, settings.archetypes);
539 if ((fp = fopen(filename, "r")) == NULL) {
540 LOG(llevError, " Can't open archetype file.\n");
541 return;
542 }
543 clear_archetable();
544
545 LOG(llevDebug, "arch: starting pass 1...\n");
546 first_arch_pass(fp);
547 init_archetable();
548 rewind(fp);
549
550 LOG(llevDebug, "arch: loading treasures\n");
551 load_treasures();
552 LOG(llevDebug, "arch: starting pass 2...\n");
553 second_arch_pass(fp);
554 check_generators();
555 check_spells();
556 check_summoned();
557 fclose(fp);
558 }
559
560 /**
561 * Creates and returns a new object which is a copy of the given archetype.
562 * This function returns NULL if given a NULL pointer, else an object.
563 * @param at
564 * archetype from which to get an object.
565 * @return
566 * object of specified type.
567 * @note
568 * object_new() will either allocate memory or call fatal(), so returned value
569 * is never NULL.
570 */
571 object *arch_to_object(archetype *at) {
572 object *op;
573
574 if (at == NULL) {
575 LOG(llevError, "Couldn't find archetype.\n");
576 return NULL;
577 }
578 op = object_new();
579 object_copy_with_inv(&at->clone, op);
580 op->arch = at;
581 return op;
582 }
583
584 /**
585 * Creates a dummy object. This function is called by get_archetype()
586 * if it fails to find the appropriate archetype.
587 * Thus get_archetype() will be guaranteed to always return
588 * an object, and never NULL.
589 * @param name
590 * name to give the dummy object.
591 * @return
592 * object of specified name. It fill have the ::FLAG_NO_PICK flag set.
593 * @note
594 * object_new() will either allocate memory or call fatal(), so returned value
595 * is never NULL.
596 */
597 object *create_singularity(const char *name) {
598 object *op;
599 char buf[MAX_BUF];
600
601 snprintf(buf, sizeof(buf), "%s (%s)", ARCH_SINGULARITY, name);
602 op = object_new();
603 op->arch = empty_archetype;
604 op->name = add_string(buf);
605 op->name_pl = add_string(buf);
606 SET_FLAG(op, FLAG_NO_PICK);
607 return op;
608 }
609
610 /**
611 * Finds which archetype matches the given name, and returns a new
612 * object containing a copy of the archetype.
613 * @param name
614 * archetype name
615 * @return
616 * object of specified archetype, or a singularity. Will never be NULL.
617 * @todo
618 * replace with object_create_arch() which is multi-part aware.
619 */
620 object *create_archetype(const char *name) {
621 archetype *at;
622
623 at = find_archetype(name);
624 if (at == NULL)
625 return create_singularity(name);
626 return arch_to_object(at);
627 }
628
629 /**
630 * Hash-function used by the arch-hashtable.
631 * @param str
632 * archetype name
633 * @param tablesize
634 * size of the hash table
635 * @return
636 * hash of the archetype name
637 */
638 static unsigned long
639 hasharch(const char *str, int tablesize) {
640 unsigned long hash = 0;
641 int i = 0;
642 const char *p;
643
644 /* use the one-at-a-time hash function, which supposedly is
645 * better than the djb2-like one used by perl5.005, but
646 * certainly is better then the bug used here before.
647 * see http://burtleburtle.net/bob/hash/doobs.html
648 */
649 for (p = str; i < MAXSTRING && *p; p++, i++) {
650 hash += *p;
651 hash += hash<<10;
652 hash ^= hash>>6;
653 }
654 hash += hash<<3;
655 hash ^= hash>>11;
656 hash += hash<<15;
657 return hash%tablesize;
658 }
659
660 /**
661 * Finds, using the hashtable, which archetype matches the given name. Will not LOG() if not found.
662 * @return
663 * pointer to the found archetype, otherwise NULL.
664 * @see find_archetype()
665 */
666 archetype *try_find_archetype(const char *name) {
667 archetype *at;
668 unsigned long index;
669
670 if (name == NULL)
671 return (archetype *)NULL;
672
673 index = hasharch(name, ARCHTABLE);
674 arch_search++;
675 for (;;) {
676 at = arch_table[index];
677 if (at == NULL) {
678 return NULL;
679 }
680 arch_cmp++;
681 if (!strcmp(at->name, name))
682 return at;
683 if (++index >= ARCHTABLE)
684 index = 0;
685 }
686 }
687
688 /**
689 * Finds, using the hashtable, which archetype matches the given name. Will LOG() if not found.
690 * @return
691 * pointer to the found archetype, otherwise NULL.
692 * @see try_find_archetype
693 * @todo replace by try_find_archetype() when suitable
694 */
695 archetype *find_archetype(const char *name) {
696 archetype *at;
697
698 if (name == NULL)
699 return (archetype *)NULL;
700 at = try_find_archetype(name);
701 if (at == NULL)
702 LOG(llevError, "Couldn't find archetype %s\n", name);
703 return at;
704 }
705
706 /**
707 * Adds an archetype to the hashtable.
708 * Will call fatal() if archetype table is too small to contain archetypes.
709 */
710 static void add_arch(archetype *at) {
711 unsigned long index = hasharch(at->name, ARCHTABLE), org_index = index;
712
713 for (;;) {
714 if (arch_table[index] == NULL) {
715 arch_table[index] = at;
716 return;
717 }
718 if (++index == ARCHTABLE)
719 index = 0;
720 if (index == org_index)
721 fatal(ARCHTABLE_TOO_SMALL);
722 }
723 }
724
725 /**
726 * Create a full object using the given archetype.
727 * This instanciate not only the archetype but also
728 * all linked archetypes in case of multisquare archetype.
729 * @param at
730 * archetype to instanciate. Must not be NULL.
731 * @return
732 * pointer to head of instance.
733 * @note
734 * will never return NULL.
735 */
736 object *object_create_arch(archetype *at) {
737 object *op, *prev = NULL, *head = NULL;
738
739 while (at) {
740 op = arch_to_object(at);
741 op->x = at->clone.x;
742 op->y = at->clone.y;
743 if (head)
744 op->head = head, prev->more = op;
745 if (!head)
746 head = op;
747 prev = op;
748 at = at->more;
749 }
750 return (head);
751 }
752
753 /*** end of arch.c ***/