Debian Codegraph @

server/init.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
16 * Server initialization, settings, and command-line handling.
17 */
18
19 #include "global.h"
20
21 #include <errno.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* Needed for strcasecmp(). */
27 #ifndef WIN32
28 #include <strings.h>
29 #endif
30
31 #include "loader.h"
32 #include "version.h"
33 #include "server.h"
34 #include "sproto.h"
35
36 static void help(void);
37 static void init_beforeplay(void);
38 static void init_startup(void);
39 static void init_signals(void);
40 static void init_races(void);
41 static void dump_races(void);
42 static void add_to_racelist(const char *race_name, object *op);
43 static racelink *get_racelist(void);
44
45 /**
46 * Command line option: set logfile name.
47 * @param val new name.
48 */
49 static void set_logfile(char *val) {
50 settings.logfilename = val;
51 }
52
53 /** Command line option: show version. */
54 static void call_version(void) {
55 puts(FULL_VERSION);
56 exit(EXIT_SUCCESS);
57 }
58
59 /** Command line option: debug flag. */
60 static void set_debug(void) {
61 settings.debug = llevDebug;
62 }
63
64 /** Command line option: unset debug flag. */
65 static void unset_debug(void) {
66 settings.debug = llevInfo;
67 }
68
69 /** Command line option: monster debug flag. */
70 static void set_mondebug(void) {
71 settings.debug = llevMonster;
72 }
73
74 /** Command line option: dump monsters. */
75 static void set_dumpmon1(void) {
76 settings.dumpvalues = 1;
77 }
78
79 /** Command line option: dump abilities. */
80 static void set_dumpmon2(void) {
81 settings.dumpvalues = 2;
82 }
83
84 /** Command line option: dump artifacts. */
85 static void set_dumpmon3(void) {
86 settings.dumpvalues = 3;
87 }
88
89 /** Command line option: dump spells. */
90 static void set_dumpmon4(void) {
91 settings.dumpvalues = 4;
92 }
93
94 /** Command line option: ? */
95 static void set_dumpmon5(void) {
96 settings.dumpvalues = 5;
97 }
98
99 /** Command line option: dump races. */
100 static void set_dumpmon6(void) {
101 settings.dumpvalues = 6;
102 }
103
104 /** Command line option: dump alchemy. */
105 static void set_dumpmon7(void) {
106 settings.dumpvalues = 7;
107 }
108
109 /** Command line option: dump gods. */
110 static void set_dumpmon8(void) {
111 settings.dumpvalues = 8;
112 }
113
114 /** Command line option: dump alchemy costs. */
115 static void set_dumpmon9(void) {
116 settings.dumpvalues = 9;
117 }
118
119 /**
120 * Command line option: dump monster treasures.
121 * @param name monster's name to dump treasure for.
122 */
123 static void set_dumpmont(const char *name) {
124 settings.dumpvalues = 10;
125 settings.dumparg = name;
126 }
127
128 /**
129 * Command line option: set data path.
130 * @param path new path.
131 */
132 static void set_datadir(const char *path) {
133 settings.datadir = path;
134 }
135
136 /**
137 * Command line option: set configuration path.
138 * @param path new path.
139 */
140 static void set_confdir(const char *path) {
141 settings.confdir = path;
142 }
143
144 /**
145 * Command line option: set local path.
146 * @param path new path.
147 */
148 static void set_localdir(const char *path) {
149 settings.localdir = path;
150 }
151
152 /**
153 * Command line option: set map path.
154 * @param path new path.
155 */
156 static void set_mapdir(const char *path) {
157 settings.mapdir = path;
158 }
159
160 /**
161 * Command line option: set archetypes file name.
162 * @param path new name.
163 */
164 static void set_archetypes(const char *path) {
165 settings.archetypes = path;
166 }
167
168 /**
169 * Command line option: set regions file name.
170 * @param path new name.
171 */
172 static void set_regions(const char *path) {
173 settings.regions = path;
174 }
175
176 /**
177 * Command line option: set treasures file name.
178 * @param path new name.
179 */
180 static void set_treasures(const char *path) {
181 settings.treasures = path;
182 }
183
184 /**
185 * Command line option: set unique path.
186 * @param path new path.
187 */
188 static void set_uniquedir(const char *path) {
189 settings.uniquedir = path;
190 }
191
192 /**
193 * Command line option: set template path.
194 * @param path new path.
195 */
196 static void set_templatedir(const char *path) {
197 settings.templatedir = path;
198 }
199
200 /**
201 * Command line option: set player path.
202 * @param path new path.
203 */
204 static void set_playerdir(const char *path) {
205 settings.playerdir = path;
206 }
207
208 /**
209 * Command line option: set temporary file path.
210 * @param path new path.
211 */
212 static void set_tmpdir(const char *path) {
213 settings.tmpdir = path;
214 }
215
216 static void free_races(void);
217
218 static void free_materials(void);
219
220 /**
221 * Change the server's port. Will exit() if invalid value.
222 *
223 * @param val
224 * port to use. Must be a valid one, between 1 and 65535 inclusive.
225 */
226 static void set_csport(const char *val) {
227 int port = atoi(val);
228 if (port <= 0 || port > 65535) {
229 LOG(llevError, "%d is an invalid csport number, must be between 1 and 65535.\n", port);
230 exit(1);
231 }
232 settings.csport = port;
233 }
234
235 /**
236 * Disable a plugin.
237 * @param name plugin's name, without extension.
238 */
239 static void set_disable_plugin(const char *name) {
240 linked_char *disable = calloc(1, sizeof(linked_char));
241 disable->next = settings.disabled_plugins;
242 disable->name = strdup(name);
243 settings.disabled_plugins = disable;
244 }
245
246 /**
247 * Dump all animations, then exit.
248 */
249 static void server_dump_animations(void) {
250 dump_animations();
251 cleanup();
252 }
253
254 /** Typedefs used when calling option handlers. */
255 /*@{*/
256 typedef void (*cmdlinefunc_args0)(void);
257 typedef void (*cmdlinefunc_args1)(const char* arg1);
258 typedef void (*cmdlinefunc_args2)(const char* arg1, const char* arg2);
259 /*@}*/
260
261 /**
262 * One command line option definition.
263 * Most of this is shamelessly stolen from XSysStats. But since that is
264 * also my program, no problem.
265 */
266 struct Command_Line_Options {
267 const char *cmd_option; /**< How it is called on the command line. */
268 uint8_t num_args; /**< Number or args it takes. */
269 uint8_t pass; /**< What pass this should be processed on. @todo describe passes :) */
270 void (*func)(); /**< function to call when we match this.
271 * if num_args is true, than that gets passed
272 * to the function, otherwise nothing is passed
273 */
274 };
275
276 /**
277 * Actual valid command line options.
278 * The way this system works is pretty simple - parse_args takes
279 * the options passed to the program and a pass number. If an option
280 * matches both in name and in pass (and we have enough options),
281 * we call the associated function. This makes writing a multi
282 * pass system very easy, and it is very easy to add in new options.
283 */
284 static struct Command_Line_Options options[] = {
285 /** Pass 1 functions - Stuff that can/should be called before we actually
286 * initialize any data.
287 */
288 { "-arch", 1, 1, set_archetypes },
289 { "-conf", 1, 1, set_confdir },
290 { "-d", 0, 1, set_debug },
291 { "-data", 1, 1, set_datadir },
292 { "-disable-plugin", 1, 1, set_disable_plugin },
293 { "-h", 0, 1, help },
294 { "-local", 1, 1, set_localdir },
295 { "-log", 1, 1, set_logfile },
296 { "-maps", 1, 1, set_mapdir },
297 { "-mon", 0, 1, set_mondebug },
298 { "-n", 0, 1, unset_debug },
299 { "-playerdir", 1, 1, set_playerdir },
300 { "-regions", 1, 1, set_regions },
301 { "-templatedir", 1, 1, set_templatedir },
302 { "-tmpdir", 1, 1, set_tmpdir },
303 { "-treasures", 1, 1, set_treasures },
304 { "-uniquedir", 1, 1, set_uniquedir },
305 { "-v", 0, 1, call_version },
306
307 #ifdef WIN32
308 /* Windows service stuff */
309 { "-regsrv", 0, 1, service_register },
310 { "-unregsrv", 0, 1, service_unregister },
311 { "-srv", 0, 1, service_handle },
312 #endif
313
314 /** Pass 2 functions. Most of these could probably be in pass 1,
315 * as they don't require much of anything to bet set up.
316 */
317 { "-p", 1, 2, set_csport },
318
319 /** Start of pass 3 information. In theory, by pass 3, all data paths
320 * and defaults should have been set up.
321 */
322 { "-m", 0, 3, set_dumpmon1 },
323 { "-m2", 0, 3, set_dumpmon2 },
324 { "-m3", 0, 3, set_dumpmon3 },
325 { "-m4", 0, 3, set_dumpmon4 },
326 { "-m5", 0, 3, set_dumpmon5 },
327 { "-m6", 0, 3, set_dumpmon6 },
328 { "-m7", 0, 3, set_dumpmon7 },
329 { "-m8", 0, 3, set_dumpmon8 },
330 { "-m9", 0, 3, set_dumpmon9 },
331 { "-mt", 1, 3, set_dumpmont },
332 { "-mexp", 0, 3, dump_experience },
333 { "-mq", 0, 3, dump_quests },
334 { "-dump-anims", 0, 3, server_dump_animations },
335 };
336
337 /**
338 * Parse command line arguments.
339 *
340 * Note since this may be called before the library has been set up,
341 * we don't use any of crossfires built in logging functions.
342 *
343 * @param argc
344 * length of argv.
345 * @param argv
346 * arguments.
347 * @param pass
348 * initialization pass arguments to use.
349 * @todo describe pass.
350 */
351 static void parse_args(int argc, char *argv[], int pass) {
352 size_t i;
353 int on_arg = 1;
354
355 while (on_arg < argc) {
356 for (i = 0; i < sizeof(options)/sizeof(struct Command_Line_Options); i++) {
357 if (!strcmp(options[i].cmd_option, argv[on_arg])) {
358 /* Found a matching option, but should not be processed on
359 * this pass. Just skip over it
360 */
361 if (options[i].pass != pass) {
362 on_arg += options[i].num_args+1;
363 break;
364 }
365 if (options[i].num_args) {
366 if ((on_arg+options[i].num_args) >= argc) {
367 fprintf(stderr, "%s requires an argument.\n", options[i].cmd_option);
368 exit(1);
369 }
370
371 if (options[i].num_args == 1)
372 ((cmdlinefunc_args1)options[i].func)(argv[on_arg+1]);
373 if (options[i].num_args == 2)
374 ((cmdlinefunc_args2)options[i].func)(argv[on_arg+1], argv[on_arg+2]);
375 on_arg += options[i].num_args+1;
376 } else { /* takes no args */
377 ((cmdlinefunc_args0)options[i].func)();
378 on_arg++;
379 }
380 break;
381 }
382 }
383 if (i == sizeof(options)/sizeof(struct Command_Line_Options)) {
384 fprintf(stderr, "Unknown option: %s\n", argv[on_arg]);
385 fprintf(stderr, "Type '%s -h' for usage.\n", argv[0]);
386 exit(1);
387 }
388 }
389 }
390
391 /**
392 * Creates an empty materialtype_t structure.
393 *
394 * @return
395 * new blanked structure.
396 * @note
397 * will fatal() instead of returning NULL.
398 */
399 static materialtype_t *get_empty_mat(void) {
400 materialtype_t *mt;
401 int i;
402
403 mt = (materialtype_t *)malloc(sizeof(materialtype_t));
404 if (mt == NULL)
405 fatal(OUT_OF_MEMORY);
406 mt->name = NULL;
407 mt->description = NULL;
408 for (i = 0; i < NROFATTACKS; i++) {
409 mt->save[i] = 0;
410 mt->mod[i] = 0;
411 }
412 mt->next = NULL;
413 return mt;
414 }
415
416 /**
417 * Loads the materials.
418 * @todo describe materials and such.
419 */
420 static void load_materials(void) {
421 char buf[MAX_BUF], filename[MAX_BUF], *cp, *next;
422 FILE *fp;
423 materialtype_t *mt;
424 int i, value;
425
426 snprintf(filename, sizeof(filename), "%s/materials", settings.datadir);
427 if ((fp = fopen(filename, "r")) == NULL) {
428 LOG(llevError, "Cannot open %s for reading\n", filename);
429 mt = get_empty_mat();
430 mt->next = NULL;
431 materialt = mt;
432 return;
433 }
434 mt = get_empty_mat();
435 materialt = mt;
436 while (fgets(buf, MAX_BUF, fp) != NULL) {
437 if (*buf == '#')
438 continue;
439 if ((cp = strchr(buf, '\n')) != NULL)
440 *cp = '\0';
441 cp = buf;
442 while (*cp == ' ') /* Skip blanks */
443 cp++;
444 if (!strncmp(cp, "name", 4)) {
445 /* clean up the previous entry */
446 if (mt->next != NULL) {
447 if (mt->description == NULL)
448 mt->description = add_string(mt->name);
449 mt = mt->next;
450 }
451 mt->next = get_empty_mat();
452 mt->name = add_string(strchr(cp, ' ')+1);
453 } else if (!strncmp(cp, "description", 11)) {
454 mt->description = add_string(strchr(cp, ' ')+1);
455 } else if (sscanf(cp, "material %d", &value)) {
456 mt->material = value;
457 } else if (!strncmp(cp, "saves", 5)) {
458 cp = strchr(cp, ' ')+1;
459 for (i = 0; i < NROFATTACKS; i++) {
460 if (cp == NULL) {
461 mt->save[i] = 0;
462 continue;
463 }
464 if ((next = strchr(cp, ',')) != NULL)
465 *(next++) = '\0';
466 sscanf(cp, "%d", &value);
467 mt->save[i] = (int8_t)value;
468 cp = next;
469 }
470 } else if (!strncmp(cp, "mods", 4)) {
471 cp = strchr(cp, ' ')+1;
472 for (i = 0; i < NROFATTACKS; i++) {
473 if (cp == NULL) {
474 mt->save[i] = 0;
475 continue;
476 }
477 if ((next = strchr(cp, ',')) != NULL)
478 *(next++) = '\0';
479 sscanf(cp, "%d", &value);
480 mt->mod[i] = (int8_t)value;
481 cp = next;
482 }
483 }
484 }
485 free(mt->next);
486 mt->next = NULL;
487 LOG(llevDebug, "loaded material type data\n");
488 fclose(fp);
489 }
490
491 /**
492 * Frees all memory allocated to materials.
493 */
494 static void free_materials(void) {
495 materialtype_t *next;
496
497 while (materialt) {
498 next = materialt->next;
499 free(materialt);
500 materialt = next;
501 }
502 materialt = NULL;
503 }
504
505 /**
506 * This loads the settings file. There could be debate whether this should
507 * be here or in the common directory - but since only the server needs this
508 * information, having it here probably makes more sense.
509 */
510 static void load_settings(void) {
511 char buf[MAX_BUF], *cp, dummy[1];
512 int has_val;
513 FILE *fp;
514
515 dummy[0] = '\0';
516 snprintf(buf, sizeof(buf), "%s/settings", settings.confdir);
517
518 /* We don't require a settings file at current time, but down the road,
519 * there will probably be so many values that not having a settings file
520 * will not be a good thing.
521 */
522 if ((fp = fopen(buf, "r")) == NULL) {
523 LOG(llevError, "Warning: No settings file found\n");
524 return;
525 }
526 while (fgets(buf, MAX_BUF-1, fp) != NULL) {
527 if (buf[0] == '#')
528 continue;
529 /* eliminate newline */
530 if ((cp = strrchr(buf, '\n')) != NULL)
531 *cp = '\0';
532
533 /* Skip over empty lines */
534 if (buf[0] == 0)
535 continue;
536
537 /* Skip all the spaces and set them to nulls. If not space,
538 * set cp to "" to make strcpy's and the like easier down below.
539 */
540 if ((cp = strchr(buf, ' ')) != NULL) {
541 while (*cp == ' ')
542 *cp++ = 0;
543 has_val = 1;
544 } else {
545 cp = dummy;
546 has_val = 0;
547 }
548
549 if (!strcasecmp(buf, "metaserver_notification")) {
550 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
551 settings.meta_on = TRUE;
552 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
553 settings.meta_on = FALSE;
554 } else {
555 LOG(llevError, "load_settings: Unknown value for metaserver_notification: %s\n", cp);
556 }
557 } else if (!strcasecmp(buf, "metaserver_server")) {
558 if (has_val)
559 safe_strncpy(settings.meta_server, cp, sizeof(settings.meta_server));
560 else
561 LOG(llevError, "load_settings: metaserver_server must have a value.\n");
562 } else if (!strcasecmp(buf, "motd")) {
563 if (has_val)
564 safe_strncpy(settings.motd, cp, sizeof(settings.motd));
565 else
566 LOG(llevError, "load_settings: motd must have a value.\n");
567 } else if (!strcasecmp(buf, "dm_mail")) {
568 if (has_val)
569 safe_strncpy(settings.dm_mail, cp, sizeof(settings.dm_mail));
570 else
571 LOG(llevError, "load_settings: dm_mail must have a value.\n");
572 } else if (!strcasecmp(buf, "metaserver_host")) {
573 if (has_val)
574 safe_strncpy(settings.meta_host, cp, sizeof(settings.meta_host));
575 else
576 LOG(llevError, "load_settings: metaserver_host must have a value.\n");
577 } else if (!strcasecmp(buf, "port")) {
578 set_csport(cp);
579 } else if (!strcasecmp(buf, "metaserver_port")) {
580 int port = atoi(cp);
581
582 if (port < 1 || port > 65535)
583 LOG(llevError, "load_settings: metaserver_port must be between 1 and 65535, %d is invalid\n", port);
584 else
585 settings.meta_port = port;
586 } else if (!strcasecmp(buf, "metaserver_comment")) {
587 safe_strncpy(settings.meta_comment, cp, sizeof(settings.meta_comment));
588 } else if (!strcasecmp(buf, "worldmapstartx")) {
589 int size = atoi(cp);
590
591 if (size < 0)
592 LOG(llevError, "load_settings: worldmapstartx must be at least 0, %d is invalid\n", size);
593 else
594 settings.worldmapstartx = size;
595 } else if (!strcasecmp(buf, "worldmapstarty")) {
596 int size = atoi(cp);
597
598 if (size < 0)
599 LOG(llevError, "load_settings: worldmapstarty must be at least 0, %d is invalid\n", size);
600 else
601 settings.worldmapstarty = size;
602 } else if (!strcasecmp(buf, "worldmaptilesx")) {
603 int size = atoi(cp);
604
605 if (size < 1)
606 LOG(llevError, "load_settings: worldmaptilesx must be greater than 1, %d is invalid\n", size);
607 else
608 settings.worldmaptilesx = size;
609 } else if (!strcasecmp(buf, "worldmaptilesy")) {
610 int size = atoi(cp);
611
612 if (size < 1)
613 LOG(llevError, "load_settings: worldmaptilesy must be greater than 1, %d is invalid\n", size);
614 else
615 settings.worldmaptilesy = size;
616 } else if (!strcasecmp(buf, "worldmaptilesizex")) {
617 int size = atoi(cp);
618
619 if (size < 1)
620 LOG(llevError, "load_settings: worldmaptilesizex must be greater than 1, %d is invalid\n", size);
621 else
622 settings.worldmaptilesizex = size;
623 } else if (!strcasecmp(buf, "worldmaptilesizey")) {
624 int size = atoi(cp);
625
626 if (size < 1)
627 LOG(llevError, "load_settings: worldmaptilesizey must be greater than 1, %d is invalid\n", size);
628 else
629 settings.worldmaptilesizey = size;
630 } else if (!strcasecmp(buf, "fastclock")) {
631 int lev = atoi(cp);
632
633 if (lev < 0)
634 LOG(llevError, "load_settings: fastclock must be at least 0, %d is invalid\n", lev);
635 else
636 settings.fastclock = lev;
637 } else if (!strcasecmp(buf, "not_permadeth")) {
638 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
639 settings.not_permadeth = TRUE;
640 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
641 settings.not_permadeth = FALSE;
642 } else {
643 LOG(llevError, "load_settings: Unknown value for not_permadeth: %s\n", cp);
644 }
645 } else if (!strcasecmp(buf, "resurrection")) {
646 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
647 settings.resurrection = TRUE;
648 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
649 settings.resurrection = FALSE;
650 } else {
651 LOG(llevError, "load_settings: Unknown value for resurrection: %s\n", cp);
652 }
653 } else if (!strcasecmp(buf, "set_title")) {
654 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
655 settings.set_title = TRUE;
656 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
657 settings.set_title = FALSE;
658 } else {
659 LOG(llevError, "load_settings: Unknown value for set_title: %s\n", cp);
660 }
661 } else if (!strcasecmp(buf, "search_items")) {
662 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
663 settings.search_items = TRUE;
664 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
665 settings.search_items = FALSE;
666 } else {
667 LOG(llevError, "load_settings: Unknown value for search_items: %s\n", cp);
668 }
669 } else if (!strcasecmp(buf, "spell_encumbrance")) {
670 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
671 settings.spell_encumbrance = TRUE;
672 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
673 settings.spell_encumbrance = FALSE;
674 } else {
675 LOG(llevError, "load_settings: Unknown value for spell_encumbrance: %s\n", cp);
676 }
677 } else if (!strcasecmp(buf, "spell_failure_effects")) {
678 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
679 settings.spell_failure_effects = TRUE;
680 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
681 settings.spell_failure_effects = FALSE;
682 } else {
683 LOG(llevError, "load_settings: Unknown value for spell_failure_effects: %s\n", cp);
684 }
685 } else if (!strcasecmp(buf, "casting_time")) {
686 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
687 settings.casting_time = TRUE;
688 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
689 settings.casting_time = FALSE;
690 } else {
691 LOG(llevError, "load_settings: Unknown value for casting_time: %s\n", cp);
692 }
693 } else if (!strcasecmp(buf, "real_wiz")) {
694 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
695 settings.real_wiz = TRUE;
696 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
697 settings.real_wiz = FALSE;
698 } else {
699 LOG(llevError, "load_settings: Unknown value for real_wiz: %s\n", cp);
700 }
701 } else if (!strcasecmp(buf, "recycle_tmp_maps")) {
702 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
703 settings.recycle_tmp_maps = TRUE;
704 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
705 settings.recycle_tmp_maps = FALSE;
706 } else {
707 LOG(llevError, "load_settings: Unknown value for recycle_tmp_maps: %s\n", cp);
708 }
709 } else if (!strcasecmp(buf, "always_show_hp")) {
710 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
711 settings.always_show_hp = 2;
712 } else if (!strcasecmp(cp, "damaged")) {
713 settings.always_show_hp = 1;
714 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
715 settings.always_show_hp = 0;
716 } else {
717 LOG(llevError, "load_settings: Unknown value for always_show_hp: %s\n", cp);
718 }
719 } else if (!strcasecmp(buf, "who_format")) {
720 if (has_val)
721 safe_strncpy(settings.who_format, cp,
722 sizeof(settings.who_format));
723 } else if (!strcasecmp(buf, "who_wiz_format")) {
724 if (has_val) {
725 safe_strncpy(settings.who_wiz_format, cp,
726 sizeof(settings.who_wiz_format));
727 }
728 } else if (!strcasecmp(buf, "spellpoint_level_depend")) {
729 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
730 settings.spellpoint_level_depend = TRUE;
731 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
732 settings.spellpoint_level_depend = FALSE;
733 } else {
734 LOG(llevError, "load_settings: Unknown value for spellpoint_level_depend: %s\n", cp);
735 }
736 } else if (!strcasecmp(buf, "stat_loss_on_death")) {
737 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
738 settings.stat_loss_on_death = TRUE;
739 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
740 settings.stat_loss_on_death = FALSE;
741 } else {
742 LOG(llevError, "load_settings: Unknown value for stat_loss_on_death: %s\n", cp);
743 }
744 } else if (!strcasecmp(buf, "use_permanent_experience")) {
745 LOG(llevError, "use_permanent_experience is deprecated, usepermenent_experience_percentage instead\n");
746 } else if (!strcasecmp(buf, "permanent_experience_percentage")) {
747 int val = atoi(cp);
748 if (val < 0 || val > 100)
749 LOG(llevError, "load_settings: permenent_experience_percentage must be between 0 and 100, %d is invalid\n", val);
750 else
751 settings.permanent_exp_ratio = val;
752 } else if (!strcasecmp(buf, "death_penalty_percentage")) {
753 int val = atoi(cp);
754 if (val < 0 || val > 100)
755 LOG(llevError, "load_settings: death_penalty_percentage must be between 0 and 100, %d is invalid\n", val);
756 else
757 settings.death_penalty_ratio = val;
758 } else if (!strcasecmp(buf, "death_penalty_levels")) {
759 int val = atoi(cp);
760 if (val < 0 || val > 255)
761 LOG(llevError, "load_settings: death_penalty_levels can not be negative, %d is invalid\n", val);
762 else
763 settings.death_penalty_level = val;
764 } else if (!strcasecmp(buf, "balanced_stat_loss")) {
765 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
766 settings.balanced_stat_loss = TRUE;
767 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
768 settings.balanced_stat_loss = FALSE;
769 } else {
770 LOG(llevError, "load_settings: Unknown value for balanced_stat_loss: %s\n", cp);
771 }
772 } else if (!strcasecmp(buf, "simple_exp")) {
773 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
774 settings.simple_exp = TRUE;
775 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
776 settings.simple_exp = FALSE;
777 } else {
778 LOG(llevError, "load_settings: Unknown value for simple_exp: %s\n", cp);
779 }
780 } else if (!strcasecmp(buf, "item_power_factor")) {
781 float tmp = atof(cp);
782 if (tmp < 0)
783 LOG(llevError, "load_settings: item_power_factor must be a positive number (%f < 0)\n", tmp);
784 else
785 settings.item_power_factor = tmp;
786 } else if (!strcasecmp(buf, "pk_luck_penalty")) {
787 int16_t val = atoi(cp);
788
789 if (val < -100 || val > 100)
790 LOG(llevError, "load_settings: pk_luck_penalty must be between -100 and 100, %d is invalid\n", val);
791 else
792 settings.pk_luck_penalty = val;
793 } else if (!strcasecmp(buf, "set_friendly_fire")) {
794 int val = atoi(cp);
795
796 if (val < 1 || val > 100)
797 LOG(llevError, "load_settings: set_friendly_fire must be between 1 an 100, %d is invalid\n", val);
798 else
799 settings.set_friendly_fire = val;
800 } else if (!strcasecmp(buf, "armor_max_enchant")) {
801 int max_e = atoi(cp);
802 if (max_e <= 0)
803 LOG(llevError, "load_settings: armor_max_enchant is %d\n", max_e);
804 else
805 settings.armor_max_enchant = max_e;
806 } else if (!strcasecmp(buf, "armor_weight_reduction")) {
807 int wr = atoi(cp);
808 if (wr < 0)
809 LOG(llevError, "load_settings: armor_weight_reduction is %d\n", wr);
810 else
811 settings.armor_weight_reduction = wr;
812 } else if (!strcasecmp(buf, "armor_weight_linear")) {
813 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
814 settings.armor_weight_linear = TRUE;
815 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
816 settings.armor_weight_linear = FALSE;
817 } else {
818 LOG(llevError, "load_settings: unknown value for armor_weight_linear: %s\n", cp);
819 }
820 } else if (!strcasecmp(buf, "armor_speed_improvement")) {
821 int wr = atoi(cp);
822 if (wr < 0)
823 LOG(llevError, "load_settings: armor_speed_improvement is %d\n", wr);
824 else
825 settings.armor_speed_improvement = wr;
826 } else if (!strcasecmp(buf, "armor_speed_linear")) {
827 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
828 settings.armor_speed_linear = TRUE;
829 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
830 settings.armor_speed_linear = FALSE;
831 } else {
832 LOG(llevError, "load_settings: unknown value for armor_speed_linear: %s\n", cp);
833 }
834 } else if (!strcasecmp(buf, "no_player_stealing")) {
835 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
836 settings.no_player_stealing = TRUE;
837 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
838 settings.no_player_stealing = FALSE;
839 } else {
840 LOG(llevError, "load_settings: unknown value for no_player_stealing: %s\n", cp);
841 }
842 } else if (!strcasecmp(buf, "create_home_portals")) {
843 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
844 settings.create_home_portals = TRUE;
845 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
846 settings.create_home_portals = FALSE;
847 } else {
848 LOG(llevError, "load_settings: unknown value for create_home_portals: %s\n", cp);
849 }
850 } else if (!strcasecmp(buf, "personalized_blessings")) {
851 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
852 settings.personalized_blessings = TRUE;
853 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
854 settings.personalized_blessings = FALSE;
855 } else {
856 LOG(llevError, "load_settings: unknown value for personalized_blessings: %s\n", cp);
857 }
858 } else if (!strcasecmp(buf, "pk_max_experience")) {
859 int64_t pkme = atoll(cp);
860 if (pkme < 0)
861 pkme = -1;
862 settings.pk_max_experience = pkme;
863 } else if (!strcasecmp(buf, "pk_max_experience_percent")) {
864 int pkmep = atoi(cp);
865 if (pkmep < 0) {
866 LOG(llevError, "load_settings: pk_max_experience_percent should be positive or zero (was \"%s\")\n", cp);
867 } else
868 settings.pk_max_experience_percent = pkmep;
869 } else if (!strcasecmp(buf, "allow_denied_spells_writing")) {
870 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
871 settings.allow_denied_spells_writing = TRUE;
872 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
873 settings.allow_denied_spells_writing = FALSE;
874 } else {
875 LOG(llevError, "load_settings: unknown value for allow_denied_spells_writing: %s\n", cp);
876 }
877 } else if (!strcasecmp(buf, "allow_broken_converters")) {
878 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
879 settings.allow_broken_converters = TRUE;
880 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
881 settings.allow_broken_converters = FALSE;
882 } else {
883 LOG(llevError, "load_settings: unknown value for allow_broken_converters: %s\n", cp);
884 }
885 } else if (!strcasecmp(buf, "log_timestamp")) {
886 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
887 settings.log_timestamp = TRUE;
888 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
889 settings.log_timestamp = FALSE;
890 } else {
891 LOG(llevError, "load_settings: unknown value for log_timestamp: %s\n", cp);
892 }
893 } else if (!strcasecmp(buf, "log_timestamp_format")) {
894 free(settings.log_timestamp_format);
895 settings.log_timestamp_format = strdup_local(cp);
896 } else if (!strcasecmp(buf, "starting_stat_min")) {
897 int val = atoi(cp);
898
899 if (val < 1 || val > settings.max_stat || val > settings.starting_stat_max)
900 LOG(llevError, "load_settings: starting_stat_min (%d) need to be within %d-%d (%d)\n",
901 val, 1, settings.max_stat, settings.starting_stat_max);
902 else
903 settings.starting_stat_min = val;
904 } else if (!strcasecmp(buf, "starting_stat_max")) {
905 int val = atoi(cp);
906
907 if (val < 1 || val > settings.max_stat || val<settings.starting_stat_min)
908 LOG(llevError, "load_settings: starting_stat_max (%d) need to be within %d-%d (%d)\n",
909 val, 1, settings.max_stat, settings.starting_stat_min);
910 else
911 settings.starting_stat_max = val;
912 } else if (!strcasecmp(buf, "starting_stat_points")) {
913 int val = atoi(cp);
914
915 if (val < NUM_STATS * settings.starting_stat_min ||
916 val > NUM_STATS * settings.starting_stat_max)
917 LOG(llevError, "load_settings: starting_stat_points (%d) need to be within %d-%d\n",
918 val, NUM_STATS * settings.starting_stat_min, NUM_STATS * settings.starting_stat_max);
919 else
920 settings.starting_stat_points = val;
921 } else if (!strcasecmp(buf, "roll_stat_points")) {
922 int val = atoi(cp);
923
924 /* The 3 and 18 values are hard coded in because we know that
925 * roll_stat() generates a value between 3 and 18 - if that ever
926 * changed, this code should change also, but that code will eventually
927 * go away.
928 */
929 if (val < NUM_STATS * 3 || val > NUM_STATS * 18)
930 LOG(llevError, "load_settings: roll_stat_points need to be within %d-%d\n",
931 NUM_STATS * 3, NUM_STATS * 18);
932 else
933 settings.roll_stat_points = val;
934 } else if (!strcasecmp(buf, "special_break_map")) {
935 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
936 settings.special_break_map = TRUE;
937 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
938 settings.special_break_map = FALSE;
939 } else {
940 LOG(llevError, "load_settings: unknown value for special_break_map: %s\n", cp);
941 }
942 } else if (!strcasecmp(buf, "ignore_plugin_compatibility")) {
943 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
944 settings.ignore_plugin_compatibility = TRUE;
945 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
946 settings.ignore_plugin_compatibility = FALSE;
947 } else {
948 LOG(llevError, "load_settings: unknown value for ignore_plugin_compatibility: %s\n", cp);
949 }
950 } else if (!strcasecmp(buf, "account_block_create")) {
951 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
952 settings.account_block_create = TRUE;
953 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
954 settings.account_block_create = FALSE;
955 } else {
956 LOG(llevError, "load_settings: unknown value for account_block_create: %s\n", cp);
957 }
958 } else if (!strcasecmp(buf, "account_trusted_host")) {
959 free(settings.account_trusted_host);
960 settings.account_trusted_host = strdup_local(cp);
961 } else if (!strcasecmp(buf, "crypt_mode")) {
962 int val = atoi(cp);
963 if (val != 0 && val != 1) {
964 LOG(llevError, "load_settings: crypt_mode must be 0 or 1\n");
965 } else {
966 settings.crypt_mode = val;
967 }
968 } else if (!strcasecmp(buf, "min_name")) {
969 int val = atoi(cp);
970
971 if (val < 1 || val > MAX_NAME )
972 LOG(llevError, "load_settings: min_name (%d) need to be within %d-%d\n",
973 val, 1, MAX_NAME);
974 else
975 settings.min_name = val;
976 } else {
977 LOG(llevError, "Unknown value in settings file: %s\n", buf);
978 }
979 }
980 fclose(fp);
981 if (settings.log_timestamp_format == NULL)
982 settings.log_timestamp_format = strdup_local("%y/%m/%d %H:%M:%S");
983
984 /*
985 * The who formats are defined in config to be blank. They should have been
986 * overridden by the settings file, if there are no entries however, it will
987 * have stayed blank. Since this probably isn't what is wanted, we will check if
988 * new formats have been specified, and if not we will use the old defaults.
989 */
990 if (!strcmp(settings.who_format, ""))
991 strcpy(settings.who_format, "%N_%T%t%h%d%b%n<%m>");
992 if (!strcmp(settings.who_wiz_format, ""))
993 strcpy(settings.who_wiz_format, "%N_%T%t%h%d%b%nLevel %l <%m>(@%i)(%c)");
994 }
995
996 /**
997 * This is the main server initialization function.
998 *
999 * Called only once, when starting the program.
1000 * @param argc argument count.
1001 * @param argv arguments on the command line.
1002 */
1003 void init(int argc, char **argv) {
1004 init_done = 0; /* Must be done before init_signal() */
1005 logfile = stderr;
1006
1007 /* First argument pass - right now it does nothing, but in the future specifying
1008 * the LibDir in this pass would be reasonable. */
1009 parse_args(argc, argv, 1);
1010
1011 init_library(); /* Must be called early */
1012 load_settings(); /* Load the settings file */
1013 load_materials();
1014 parse_args(argc, argv, 2);
1015
1016 LOG(llevInfo, "Crossfire %s\n", FULL_VERSION);
1017 SRANDOM(time(NULL));
1018
1019 init_startup(); /* Check shutdown/forbid files */
1020 init_signals(); /* Sets up signal interceptions */
1021 init_commands(); /* Sort command tables */
1022 read_map_log(); /* Load up the old temp map files */
1023 init_skills();
1024 init_ob_methods();
1025 cftimer_init();
1026 hiscore_init();
1027
1028 parse_args(argc, argv, 3);
1029
1030 init_beforeplay();
1031 init_server();
1032 metaserver2_init();
1033 accounts_load();
1034 reset_sleep();
1035 init_done = 1;
1036 }
1037
1038 /**
1039 * Frees all memory allocated around here:
1040 * - materials
1041 * - races
1042 */
1043 void free_server(void) {
1044 free_materials();
1045 free_races();
1046 free_quest();
1047 free_quest_definitions();
1048 while (settings.disabled_plugins) {
1049 linked_char *next = settings.disabled_plugins->next;
1050 free((void *)settings.disabled_plugins->name);
1051 free(settings.disabled_plugins);
1052 settings.disabled_plugins = next;
1053 }
1054 }
1055
1056 /**
1057 * Display the command line options and exits.
1058 */
1059 static void help(void) {
1060 printf("Usage: crossfire-server [options]\n\n");
1061
1062 printf("Options:\n");
1063 printf(" -arch Set the file with archetype definitions.\n");
1064 printf(" -conf Set the directory to find configuration files.\n");
1065 printf(" -d Turn on extra debugging messages.\n");
1066 printf(" -data Set the data (share/) directory (archetypes, treasures, etc).\n");
1067 printf(" -disable-plugin\n"
1068 " Disables specified plugin. Use the name without the extension.\n"
1069 " Can be specified multiple times. 'All' disables all plugins.\n");
1070 printf(" -dump-anims Dump animations.\n");
1071 printf(" -h Print this help message.\n");
1072 printf(" -local Set the local data (var/) directory.\n");
1073 printf(" -log <file> Write logging information to the given file.\n");
1074 printf(" -m List suggested experience for all monsters.\n");
1075 printf(" -m2 Dump monster abilities.\n");
1076 printf(" -m3 Dump artifact information.\n");
1077 printf(" -m4 Dump spell information.\n");
1078 printf(" -m5 Dump skill information.\n");
1079 printf(" -m6 Dump race information.\n");
1080 printf(" -m7 Dump alchemy information.\n");
1081 printf(" -m8 Dump gods information.\n");
1082 printf(" -m9 Dump more alchemy information (formula checking).\n");
1083 printf(" -maps Set the map directory.\n");
1084 printf(" -mexp Dump the experience table.\n");
1085 printf(" -mon Turn on monster debugging.\n");
1086 printf(" -mq Dump the quest list.\n");
1087 printf(" -mt <name> Dump a list of treasures for a monster.\n");
1088 printf(" -n Turn off debugging messages if on by default.\n");
1089 printf(" -p <port> Specifies the port to listen on for incoming connections.\n");
1090 printf(" -playerdir Set the player files directory.\n");
1091 printf(" -regions Set the region file.\n");
1092 printf(" -templatedir Set the template map directory.\n");
1093 printf(" -tmpdir Set the directory for temporary files (mostly maps.)\n");
1094 printf(" -treasures Set the treasures file.\n");
1095 printf(" -uniquedir Set the unique items/maps directory.\n");
1096 printf(" -v Print version information.\n");
1097 exit(EXIT_SUCCESS);
1098 }
1099
1100 /**
1101 * Called before the server starts listening to connections, processes various
1102 * dump-related options.
1103 */
1104 static void init_beforeplay(void) {
1105 init_archetypes(); /* If not called before, reads all archetypes from file */
1106 init_artifacts(); /* If not called before, reads all artifacts from file */
1107 check_spells(); /* If not called before, links archtypes used by spells */
1108 init_regions(); /* If not called before, reads all regions from file */
1109 init_archetype_pointers(); /* Setup global pointers to archetypes */
1110 init_races(); /* overwrite race designations using entries in lib/races file */
1111 init_gods(); /* init linked list of gods from archs*/
1112 init_readable(); /* inits useful arrays for readable texts */
1113 init_formulae(); /* If not called before, reads formulae from file */
1114
1115 switch (settings.dumpvalues) {
1116 case 1:
1117 print_monsters();
1118 cleanup();
1119
1120 case 2:
1121 dump_abilities();
1122 cleanup();
1123
1124 case 3:
1125 dump_artifacts();
1126 cleanup();
1127
1128 case 4:
1129 dump_spells();
1130 cleanup();
1131
1132 case 5:
1133 cleanup();
1134
1135 case 6:
1136 dump_races();
1137 cleanup();
1138
1139 case 7:
1140 dump_alchemy();
1141 cleanup();
1142
1143 case 8:
1144 dump_gods();
1145 cleanup();
1146
1147 case 9:
1148 dump_alchemy_costs();
1149 cleanup();
1150
1151 case 10:
1152 dump_monster_treasure(settings.dumparg);
1153 cleanup();
1154 }
1155 }
1156
1157 /**
1158 * Checks if starting the server is allowed.
1159 *
1160 * @todo describe forbid_play() and such restrictions.
1161 */
1162 static void init_startup(void) {
1163 char buf[MAX_BUF];
1164 FILE *fp;
1165
1166 #ifdef SHUTDOWN_FILE
1167 snprintf(buf, sizeof(buf), "%s/%s", settings.confdir, SHUTDOWN_FILE);
1168 if ((fp = fopen(buf, "r")) != NULL) {
1169 while (fgets(buf, MAX_BUF-1, fp) != NULL)
1170 printf("%s", buf);
1171 fclose(fp);
1172 exit(1);
1173 }
1174 #endif
1175
1176 if (forbid_play()) { /* Maybe showing highscore should be allowed? */
1177 LOG(llevError, "CrossFire: Playing not allowed.\n");
1178 exit(-1);
1179 }
1180 }
1181
1182 /**
1183 * Signal handler that begins a normal server shutdown.
1184 */
1185 static void signal_shutdown(int signum_unused) {
1186 (void) signum_unused; /* avoid unused warning if enambled */
1187 shutdown_flag += 1;
1188 }
1189
1190 /**
1191 * SIGHUP handler.
1192 * SIGHUP handlers on daemons typically make them reread the config
1193 * files and reinitialize itself. This behaviour is better left for
1194 * an explicit shutdown and restart with Crossfire, as there is just
1195 * too much persistent runtime state. However, another function of
1196 * SIGHUP handlers is to reopen the log file for logrotate's benefit.
1197 * We can do that here.
1198 *
1199 * @param i
1200 * unused.
1201 */
1202 static void rec_sighup(int i) {
1203 /* Don't call LOG(). It calls non-reentrant functions. The other
1204 * signal handlers shouldn't really call LOG() either. */
1205 if (logfile != stderr) {
1206 reopen_logfile = 1;
1207 }
1208 }
1209
1210 /**
1211 * Setup our signal handlers.
1212 */
1213 static void init_signals(void) {
1214 #ifndef WIN32 /* init_signals() remove signals */
1215 struct sigaction sa;
1216
1217 sa.sa_sigaction = NULL;
1218 sigemptyset(&sa.sa_mask);
1219 sa.sa_flags = 0;
1220 sa.sa_handler = rec_sighup;
1221 sigaction(SIGHUP, &sa, NULL);
1222 signal(SIGINT, signal_shutdown);
1223 signal(SIGPIPE, SIG_IGN);
1224 #endif /* win32 */
1225 }
1226
1227 /**
1228 * Reads the races file in the lib/ directory, then
1229 * overwrites old 'race' entries. This routine allow us to quickly
1230 * re-configure the 'alignment' of monsters, objects. Useful for
1231 * putting together lists of creatures, etc that belong to gods.
1232 */
1233 static void init_races(void) {
1234 FILE *file;
1235 char race[MAX_BUF], fname[MAX_BUF], buf[MAX_BUF], *cp, variable[MAX_BUF];
1236 archetype *mon = NULL;
1237 static int init_done = 0;
1238
1239 if (init_done)
1240 return;
1241 init_done = 1;
1242 first_race = NULL;
1243
1244 snprintf(fname, sizeof(fname), "%s/races", settings.datadir);
1245 if (!(file = fopen(fname, "r"))) {
1246 LOG(llevError, "Cannot open races file %s: %s\n", fname, strerror(errno));
1247 return;
1248 }
1249
1250 while (fgets(buf, MAX_BUF, file) != NULL) {
1251 int set_race = 1, set_list = 1;
1252 if (*buf == '#')
1253 continue;
1254 if ((cp = strchr(buf, '\n')) != NULL)
1255 *cp = '\0';
1256 cp = buf;
1257 while (*cp == ' ' || *cp == '!' || *cp == '@') {
1258 if (*cp == '!')
1259 set_race = 0;
1260 if (*cp == '@')
1261 set_list = 0;
1262 cp++;
1263 }
1264 if (sscanf(cp, "RACE %s", variable)) { /* set new race value */
1265 strcpy(race, variable);
1266 } else {
1267 char *cp1;
1268
1269 /* Take out beginning spaces */
1270 for (cp1 = cp; *cp1 == ' '; cp1++)
1271 ;
1272 /* Remove newline and trailing spaces */
1273 for (cp1 = cp+strlen(cp)-1; *cp1 == '\n' || *cp1 == ' '; cp1--) {
1274 *cp1 = '\0';
1275 if (cp == cp1)
1276 break;
1277 }
1278
1279 if (cp[strlen(cp)-1] == '\n')
1280 cp[strlen(cp)-1] = '\0';
1281 /* set creature race to race value */
1282 if ((mon = find_archetype(cp)) == NULL)
1283 LOG(llevError, "Creature %s in race file lacks archetype\n", cp);
1284 else {
1285 // If the race is specified somewhere in the race field, we are good.
1286 // This allows us to specify multiple races on arches that are in the races file.
1287 if (set_race && (!mon->clone.race || !strstr(mon->clone.race, race))) {
1288 if (mon->clone.race) {
1289 LOG(llevDebug, "races: Appending to %s from %s for archetype %s\n", race, mon->clone.race, mon->name);
1290 // Copy the existing race into the race total list.
1291 strcpy(race+strlen(race), ","); // Arbitrary separator
1292 strncpy(race+strlen(race), mon->clone.race, MAX_BUF - 1 - strlen(race));
1293 // Clear the existing race string so we can add the new one
1294 free_string(mon->clone.race);
1295 }
1296 mon->clone.race = add_string(race);
1297 }
1298 /* if the arch is a monster, add it to the race list */
1299 if (set_list && QUERY_FLAG(&mon->clone, FLAG_MONSTER))
1300 add_to_racelist(race, &mon->clone);
1301 }
1302 }
1303 }
1304 fclose(file);
1305 LOG(llevDebug, "loaded races\n");
1306 }
1307
1308 /**
1309 * Dumps all race information to stderr.
1310 */
1311 static void dump_races(void) {
1312 racelink *list;
1313 objectlink *tmp;
1314
1315 for (list = first_race; list; list = list->next) {
1316 fprintf(stderr, "\nRACE %s:\t", list->name);
1317 for (tmp = list->member; tmp; tmp = tmp->next)
1318 fprintf(stderr, "%s(%d), ", tmp->ob->arch->name, tmp->ob->level);
1319 }
1320 fprintf(stderr, "\n");
1321 }
1322
1323 /**
1324 * Frees all race-related information.
1325 */
1326 static void free_races(void) {
1327 racelink *race;
1328 objectlink *link;
1329
1330 LOG(llevDebug, "Freeing race information.\n");
1331 while (first_race) {
1332 race = first_race->next;
1333 while (first_race->member) {
1334 link = first_race->member->next;
1335 free(first_race->member);
1336 first_race->member = link;
1337 }
1338 free_string(first_race->name);
1339 free(first_race);
1340 first_race = race;
1341 }
1342 }
1343
1344 /**
1345 * Add an object to the racelist.
1346 *
1347 * @param race_name
1348 * race name.
1349 * @param op
1350 * what object to add to the race.
1351 */
1352 static void add_to_racelist(const char *race_name, object *op) {
1353 racelink *race;
1354
1355 if (!op || !race_name)
1356 return;
1357 race = find_racelink(race_name);
1358
1359 if (!race) { /* add in a new race list */
1360 race = get_racelist();
1361 race->next = first_race;
1362 first_race = race;
1363 race->name = add_string(race_name);
1364 }
1365
1366 if (race->member->ob) {
1367 objectlink *tmp = get_objectlink();
1368
1369 tmp->next = race->member;
1370 race->member = tmp;
1371 }
1372 race->nrof++;
1373 race->member->ob = op;
1374 }
1375
1376 /**
1377 * Create a new ::racelink structure.
1378 *
1379 * @note
1380 * will call fatal() in case of memory allocation failure.
1381 * @return
1382 * empty structure.
1383 */
1384 static racelink *get_racelist(void) {
1385 racelink *list;
1386
1387 list = (racelink *)malloc(sizeof(racelink));
1388 if (!list)
1389 fatal(OUT_OF_MEMORY);
1390 list->name = NULL;
1391 list->nrof = 0;
1392 list->member = get_objectlink();
1393 list->next = NULL;
1394
1395 return list;
1396 }
1397
1398 /**
1399 * Find the race information for the specified name.
1400 *
1401 * @param name
1402 * race to search for.
1403 * @return
1404 * race structure, NULL if not found.
1405 */
1406 racelink *find_racelink(const char *name) {
1407 racelink *test = NULL;
1408
1409 if (name && first_race)
1410 for (test = first_race; test && test != test->next; test = test->next)
1411 if (!test->name || !strcmp(name, test->name))
1412 break;
1413
1414 return test;
1415 }