ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.13
Committed: Thu Sep 14 22:34:04 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +1 -1 lines
Log Message:
indent

File Contents

# User Rev Content
1 elmex 1.1 /*
2     CrossFire, A Multiplayer game for X-windows
3    
4     Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5     Copyright (C) 1992 Frank Tore Johansen
6    
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 root 1.13 The authors can be reached via e-mail at <crossfire@schmorp.de>
22 elmex 1.1 */
23    
24     #include <global.h>
25     #include <material.h>
26     #include <loader.h>
27     #ifndef __CEXTRACT__
28 root 1.9 # include <sproto.h>
29 elmex 1.1 #endif
30    
31     /* global weathermap */
32     weathermap_t **weathermap;
33    
34 root 1.9 void
35     set_logfile (char *val)
36     {
37     settings.logfilename = val;
38     }
39    
40     void
41     call_version (void)
42     {
43     version (NULL);
44     exit (0);
45     }
46    
47     void
48     showscores (void)
49     {
50     display_high_score (NULL, 9999, NULL);
51     exit (0);
52     }
53    
54     void
55     set_debug (void)
56     {
57     settings.debug = llevDebug;
58     }
59    
60     void
61     unset_debug (void)
62     {
63     settings.debug = llevInfo;
64     }
65    
66     void
67     set_mondebug (void)
68     {
69     settings.debug = llevMonster;
70     }
71    
72     void
73     set_dumpmon1 (void)
74     {
75     settings.dumpvalues = 1;
76     }
77    
78     void
79     set_dumpmon2 (void)
80     {
81     settings.dumpvalues = 2;
82     }
83    
84     void
85     set_dumpmon3 (void)
86     {
87     settings.dumpvalues = 3;
88     }
89    
90     void
91     set_dumpmon4 (void)
92     {
93     settings.dumpvalues = 4;
94     }
95    
96     void
97     set_dumpmon5 (void)
98     {
99     settings.dumpvalues = 5;
100     }
101    
102     void
103     set_dumpmon6 (void)
104     {
105     settings.dumpvalues = 6;
106     }
107    
108     void
109     set_dumpmon7 (void)
110     {
111     settings.dumpvalues = 7;
112     }
113    
114     void
115     set_dumpmon8 (void)
116     {
117     settings.dumpvalues = 8;
118     }
119    
120     void
121     set_dumpmon9 (void)
122     {
123     settings.dumpvalues = 9;
124     }
125    
126     void
127     set_dumpmont (char *name)
128     {
129     settings.dumpvalues = 10;
130     settings.dumparg = name;
131     }
132    
133     void
134     set_daemon (void)
135     {
136     settings.daemonmode = 1;
137     }
138    
139     void
140     set_datadir (char *path)
141     {
142     settings.datadir = path;
143     }
144    
145     void
146     set_confdir (char *path)
147     {
148     settings.confdir = path;
149     }
150    
151     void
152     set_localdir (char *path)
153     {
154     settings.localdir = path;
155     }
156    
157     void
158     set_mapdir (char *path)
159     {
160     settings.mapdir = path;
161     }
162    
163     void
164     set_archetypes (char *path)
165     {
166     settings.archetypes = path;
167     }
168    
169     void
170     set_regions (char *path)
171     {
172     settings.regions = path;
173     }
174    
175     void
176     set_treasures (char *path)
177     {
178     settings.treasures = path;
179     }
180    
181     void
182     set_uniquedir (char *path)
183     {
184     settings.uniquedir = path;
185     }
186    
187     void
188     set_templatedir (char *path)
189     {
190     settings.templatedir = path;
191     }
192    
193     void
194     set_playerdir (char *path)
195     {
196     settings.playerdir = path;
197     }
198    
199     void
200     set_tmpdir (char *path)
201     {
202     settings.tmpdir = path;
203     }
204    
205     void
206     showscoresparm (char *data)
207     {
208     display_high_score (NULL, 9999, data);
209     exit (0);
210     }
211    
212     void
213     set_csport (char *val)
214     {
215     settings.csport = atoi (val);
216     #ifndef WIN32 /* ***win32: set_csport: we remove csport error secure check here, do this later */
217     if (settings.csport <= 0 || settings.csport > 32765 || (settings.csport < 1024 && getuid () != 0))
218     {
219     LOG (llevError, "%d is an invalid csport number.\n", settings.csport);
220     exit (1);
221 elmex 1.1 }
222     #endif /* win32 */
223     }
224    
225     /* Most of this is shamelessly stolen from XSysStats. But since that is
226     * also my program, no problem.
227     */
228 root 1.9 struct Command_Line_Options
229     {
230     const char *cmd_option; /* how it is called on the command line */
231     uint8 num_args; /* Number or args it takes */
232     uint8 pass; /* What pass this should be processed on. */
233     void (*func) (); /* function to call when we match this.
234     * if num_args is true, than that gets passed
235     * to the function, otherwise nothing is passed
236     */
237 elmex 1.1 };
238    
239     /* The way this system works is pretty simple - parse_args takes
240     * the options passed to the program and a pass number. If an option
241     * matches both in name and in pass (and we have enough options),
242     * we call the associated function. This makes writing a multi
243     * pass system very easy, and it is very easy to add in new options.
244     */
245     struct Command_Line_Options options[] = {
246    
247     /* Pass 1 functions - STuff that can/should be called before we actually
248     * initialize any data.
249     */
250 root 1.9 {"-h", 0, 1, help},
251    
252 elmex 1.1 /* Honor -help also, since it is somewhat common */
253 root 1.9 {"-help", 0, 1, help},
254     {"-v", 0, 1, call_version},
255     {"-d", 0, 1, set_debug},
256     {"+d", 0, 1, unset_debug},
257     {"-mon", 0, 1, set_mondebug},
258     {"-data", 1, 1, (void (*)()) set_datadir},
259     {"-conf", 1, 1, (void (*)()) set_confdir},
260     {"-local", 1, 1, (void (*)()) set_localdir},
261     {"-maps", 1, 1, (void (*)()) set_mapdir},
262     {"-arch", 1, 1, (void (*)()) set_archetypes},
263     {"-regions", 1, 1, (void (*)()) set_regions},
264     {"-playerdir", 1, 1, (void (*)()) set_playerdir},
265     {"-treasures", 1, 1, (void (*)()) set_treasures},
266     {"-uniquedir", 1, 1, (void (*)()) set_uniquedir},
267     {"-templatedir", 1, 1, (void (*)()) set_templatedir},
268     {"-tmpdir", 1, 1, (void (*)()) set_tmpdir},
269     {"-log", 1, 1, (void (*)()) set_logfile},
270 elmex 1.1
271     #ifdef WIN32
272 root 1.9
273 elmex 1.1 /* Windows service stuff */
274 root 1.9 {"-regsrv", 0, 1, service_register},
275     {"-unregsrv", 0, 1, service_unregister},
276     {"-srv", 0, 1, service_handle},
277 elmex 1.1 #endif
278    
279     /* Pass 2 functions. Most of these could probably be in pass 1,
280     * as they don't require much of anything to bet set up.
281     */
282 root 1.9 {"-csport", 1, 2, (void (*)()) set_csport},
283     {"-detach", 0, 2, set_daemon},
284 elmex 1.1
285     /* Start of pass 3 information. In theory, by pass 3, all data paths
286     * and defaults should have been set up.
287     */
288 root 1.9 {"-o", 0, 3, compile_info},
289     {"-m", 0, 3, set_dumpmon1},
290     {"-m2", 0, 3, set_dumpmon2},
291     {"-m3", 0, 3, set_dumpmon3},
292     {"-m4", 0, 3, set_dumpmon4},
293     {"-m5", 0, 3, set_dumpmon5},
294     {"-m6", 0, 3, set_dumpmon6},
295     {"-m7", 0, 3, set_dumpmon7},
296     {"-m8", 0, 3, set_dumpmon8},
297     {"-m9", 0, 3, set_dumpmon9},
298     {"-mt", 1, 3, (void (*)()) set_dumpmont},
299     {"-mexp", 0, 3, dump_experience},
300     {"-s", 0, 3, showscores},
301     {"-score", 1, 3, (void (*)()) showscoresparm}
302 elmex 1.1 };
303    
304    
305     /* Note since this may be called before the library has been set up,
306     * we don't use any of crossfires built in logging functions.
307     */
308 root 1.9 static void
309     parse_args (int argc, char *argv[], int pass)
310 elmex 1.1 {
311 root 1.9 size_t i;
312     int on_arg = 1;
313 elmex 1.1
314 root 1.9 while (on_arg < argc)
315     {
316     for (i = 0; i < sizeof (options) / sizeof (struct Command_Line_Options); i++)
317     {
318     if (!strcmp (options[i].cmd_option, argv[on_arg]))
319     {
320     /* Found a matching option, but should not be processed on
321     * this pass. Just skip over it
322     */
323     if (options[i].pass != pass)
324     {
325     on_arg += options[i].num_args + 1;
326     break;
327 root 1.4 }
328 root 1.9 if (options[i].num_args)
329     {
330     if ((on_arg + options[i].num_args) >= argc)
331     {
332     fprintf (stderr, "%s requires an argument.\n", options[i].cmd_option);
333     exit (1);
334 root 1.4 }
335 root 1.9 else
336     {
337     if (options[i].num_args == 1)
338     ((void (*)(char *)) options[i].func) (argv[on_arg + 1]);
339     if (options[i].num_args == 2)
340     ((void (*)(char *, char *)) options[i].func) (argv[on_arg + 1], argv[on_arg + 2]);
341     on_arg += options[i].num_args + 1;
342 root 1.4 }
343     }
344 root 1.9 else
345     { /* takes no args */
346     options[i].func ();
347     on_arg++;
348 root 1.4 }
349 root 1.9 break;
350 root 1.4 }
351     }
352 root 1.9 if (i == sizeof (options) / sizeof (struct Command_Line_Options))
353     {
354     fprintf (stderr, "Unknown option: %s\n", argv[on_arg]);
355     usage ();
356     exit (1);
357 root 1.4 }
358 elmex 1.1 }
359     }
360    
361 root 1.7 //TODO: make this a constructor
362 root 1.9 static materialtype_t *
363     get_empty_mat (void)
364     {
365     materialtype_t *mt;
366     int i;
367    
368     mt = new materialtype_t;
369    
370     mt->name = NULL;
371     mt->description = NULL;
372     for (i = 0; i < NROFATTACKS; i++)
373     {
374     mt->save[i] = 0;
375     mt->mod[i] = 0;
376 elmex 1.1 }
377 root 1.9 mt->chance = 0;
378     mt->difficulty = 0;
379     mt->magic = 0;
380     mt->damage = 0;
381     mt->wc = 0;
382     mt->ac = 0;
383     mt->sp = 0;
384     mt->weight = 100;
385     mt->value = 100;
386     mt->next = NULL;
387     return mt;
388     }
389    
390     static void
391     load_materials (void)
392     {
393     char buf[MAX_BUF], filename[MAX_BUF], *cp, *next;
394     FILE *fp;
395     materialtype_t *mt;
396     int i, value;
397    
398     sprintf (filename, "%s/materials", settings.datadir);
399     LOG (llevDebug, "Reading material type data from %s...", filename);
400     if ((fp = fopen (filename, "r")) == NULL)
401     {
402     LOG (llevError, "Cannot open %s for reading\n", filename);
403     mt = get_empty_mat ();
404     mt->next = NULL;
405     materialt = mt;
406     return;
407 elmex 1.1 }
408 root 1.9 mt = get_empty_mat ();
409     materialt = mt;
410     while (fgets (buf, MAX_BUF, fp) != NULL)
411     {
412     if (*buf == '#')
413     continue;
414     if ((cp = strchr (buf, '\n')) != NULL)
415     *cp = '\0';
416     cp = buf;
417     while (*cp == ' ') /* Skip blanks */
418     cp++;
419     if (!strncmp (cp, "name", 4))
420     {
421     /* clean up the previous entry */
422     if (mt->next != NULL)
423     {
424     if (mt->description == NULL)
425     mt->description = mt->name;
426     mt = mt->next;
427     }
428     mt->next = get_empty_mat ();
429     mt->name = strchr (cp, ' ') + 1;
430     }
431     else if (!strncmp (cp, "description", 11))
432     {
433     mt->description = strchr (cp, ' ') + 1;
434     }
435     else if (sscanf (cp, "material %d", &value))
436     {
437     mt->material = value;
438     }
439     else if (!strncmp (cp, "saves", 5))
440     {
441     cp = strchr (cp, ' ') + 1;
442     for (i = 0; i < NROFATTACKS; i++)
443     {
444     if (cp == NULL)
445     {
446     mt->save[i] = 0;
447     continue;
448 root 1.4 }
449 root 1.9 if ((next = strchr (cp, ',')) != NULL)
450     *(next++) = '\0';
451     sscanf (cp, "%d", &value);
452     mt->save[i] = (sint8) value;
453     cp = next;
454     }
455     }
456     else if (!strncmp (cp, "mods", 4))
457     {
458     cp = strchr (cp, ' ') + 1;
459     for (i = 0; i < NROFATTACKS; i++)
460     {
461     if (cp == NULL)
462     {
463     mt->save[i] = 0;
464     continue;
465 root 1.4 }
466 root 1.9 if ((next = strchr (cp, ',')) != NULL)
467     *(next++) = '\0';
468     sscanf (cp, "%d", &value);
469     mt->mod[i] = (sint8) value;
470     cp = next;
471     }
472     }
473     else if (sscanf (cp, "chance %d\n", &value))
474     {
475     mt->chance = (sint8) value;
476     }
477     else if (sscanf (cp, "diff %d\n", &value))
478     {
479     mt->difficulty = (sint8) value;
480     }
481     else if (sscanf (cp, "magic %d\n", &value))
482     {
483     mt->magic = (sint8) value;
484     }
485     else if (sscanf (cp, "damage %d\n", &value))
486     {
487     mt->damage = (sint8) value;
488     }
489     else if (sscanf (cp, "wc %d\n", &value))
490     {
491     mt->wc = (sint8) value;
492     }
493     else if (sscanf (cp, "ac %d\n", &value))
494     {
495     mt->ac = (sint8) value;
496     }
497     else if (sscanf (cp, "sp %d\n", &value))
498     {
499     mt->sp = (sint8) value;
500     }
501     else if (sscanf (cp, "weight %d\n", &value))
502     {
503     mt->weight = value;
504     }
505     else if (sscanf (cp, "value %d\n", &value))
506     {
507     mt->value = value;
508 root 1.4 }
509 elmex 1.1 }
510 root 1.9 if (mt->next)
511 elmex 1.1 {
512 root 1.9 delete mt->next;
513    
514     mt->next = NULL;
515 elmex 1.1 }
516 root 1.9 LOG (llevDebug, "Done.\n");
517     fclose (fp);
518 elmex 1.1 }
519    
520     /* This loads the settings file. There could be debate whether this should
521     * be here or in the common directory - but since only the server needs this
522     * information, having it here probably makes more sense.
523     */
524 root 1.9 static void
525     load_settings (void)
526 elmex 1.1 {
527 root 1.9 char buf[MAX_BUF], *cp;
528     int has_val, comp;
529     FILE *fp;
530    
531     sprintf (buf, "%s/settings", settings.confdir);
532    
533     /* We don't require a settings file at current time, but down the road,
534     * there will probably be so many values that not having a settings file
535     * will not be a good thing.
536     */
537     if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
538     {
539     LOG (llevError, "Warning: No settings file found\n");
540     return;
541 elmex 1.1 }
542 root 1.9 while (fgets (buf, MAX_BUF - 1, fp) != NULL)
543     {
544     if (buf[0] == '#')
545     continue;
546     /* eliminate newline */
547     if ((cp = strrchr (buf, '\n')) != NULL)
548     *cp = '\0';
549    
550     /* Skip over empty lines */
551     if (buf[0] == 0)
552     continue;
553    
554     /* Skip all the spaces and set them to nulls. If not space,
555     * set cp to "" to make strcpy's and the like easier down below.
556     */
557     if ((cp = strchr (buf, ' ')) != NULL)
558     {
559     while (*cp == ' ')
560     *cp++ = 0;
561     has_val = 1;
562     }
563     else
564     {
565     cp = "";
566     has_val = 0;
567     }
568    
569     if (!strcasecmp (buf, "metaserver_notification"))
570     {
571     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
572     {
573     settings.meta_on = TRUE;
574     }
575     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
576     {
577     settings.meta_on = FALSE;
578     }
579     else
580     {
581     LOG (llevError, "load_settings: Unknown value for metaserver_notification: %s\n", cp);
582     }
583     }
584     else if (!strcasecmp (buf, "metaserver_server"))
585     {
586     if (has_val)
587     strcpy (settings.meta_server, cp);
588     else
589     LOG (llevError, "load_settings: metaserver_server must have a value.\n");
590     }
591     else if (!strcasecmp (buf, "motd"))
592     {
593     if (has_val)
594     strcpy (settings.motd, cp);
595     else
596     LOG (llevError, "load_settings: motd must have a value.\n");
597     }
598     else if (!strcasecmp (buf, "dm_mail"))
599     {
600     if (has_val)
601     strcpy (settings.dm_mail, cp);
602     else
603     LOG (llevError, "load_settings: dm_mail must have a value.\n");
604     }
605     else if (!strcasecmp (buf, "metaserver_host"))
606     {
607     if (has_val)
608     strcpy (settings.meta_host, cp);
609     else
610     LOG (llevError, "load_settings: metaserver_host must have a value.\n");
611     }
612     else if (!strcasecmp (buf, "metaserver_port"))
613     {
614     int port = atoi (cp);
615    
616     if (port < 1 || port > 65535)
617     LOG (llevError, "load_settings: metaserver_port must be between 1 and 65535, %d is invalid\n", port);
618     else
619     settings.meta_port = port;
620     }
621     else if (!strcasecmp (buf, "metaserver_comment"))
622     {
623     strcpy (settings.meta_comment, cp);
624     }
625     else if (!strcasecmp (buf, "worldmapstartx"))
626     {
627     int size = atoi (cp);
628    
629     if (size < 0)
630     LOG (llevError, "load_settings: worldmapstartx must be at least " "0, %d is invalid\n", size);
631     else
632     settings.worldmapstartx = size;
633     }
634     else if (!strcasecmp (buf, "worldmapstarty"))
635     {
636     int size = atoi (cp);
637    
638     if (size < 0)
639     LOG (llevError, "load_settings: worldmapstarty must be at least " "0, %d is invalid\n", size);
640     else
641     settings.worldmapstarty = size;
642     }
643     else if (!strcasecmp (buf, "worldmaptilesx"))
644     {
645     int size = atoi (cp);
646    
647     if (size < 1)
648     LOG (llevError, "load_settings: worldmaptilesx must be greater " "than 1, %d is invalid\n", size);
649     else
650     settings.worldmaptilesx = size;
651     }
652     else if (!strcasecmp (buf, "worldmaptilesy"))
653     {
654     int size = atoi (cp);
655    
656     if (size < 1)
657     LOG (llevError, "load_settings: worldmaptilesy must be greater " "than 1, %d is invalid\n", size);
658     else
659     settings.worldmaptilesy = size;
660     }
661     else if (!strcasecmp (buf, "worldmaptilesizex"))
662     {
663     int size = atoi (cp);
664    
665     if (size < 1)
666     LOG (llevError, "load_settings: worldmaptilesizex must be " "greater than 1, %d is invalid\n", size);
667     else
668     settings.worldmaptilesizex = size;
669     }
670     else if (!strcasecmp (buf, "worldmaptilesizey"))
671     {
672     int size = atoi (cp);
673    
674     if (size < 1)
675     LOG (llevError, "load_settings: worldmaptilesizey must be " "greater than 1, %d is invalid\n", size);
676     else
677     settings.worldmaptilesizey = size;
678     }
679     else if (!strcasecmp (buf, "dynamiclevel"))
680     {
681     int lev = atoi (cp);
682    
683     if (lev < 0)
684     LOG (llevError, "load_settings: dynamiclevel must be " "at least 0, %d is invalid\n", lev);
685     else
686     settings.dynamiclevel = lev;
687     }
688     else if (!strcasecmp (buf, "fastclock"))
689     {
690     int lev = atoi (cp);
691    
692     if (lev < 0)
693     LOG (llevError, "load_settings: fastclock must be at least 0" ", %d is invalid\n", lev);
694     else
695     settings.fastclock = lev;
696     }
697     else if (!strcasecmp (buf, "not_permadeth"))
698     {
699     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
700     {
701     settings.not_permadeth = TRUE;
702     }
703     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
704     {
705     settings.not_permadeth = FALSE;
706     }
707     else
708     {
709     LOG (llevError, "load_settings: Unknown value for not_permadeth" ": %s\n", cp);
710     }
711     }
712     else if (!strcasecmp (buf, "resurrection"))
713     {
714     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
715     {
716     settings.resurrection = TRUE;
717     }
718     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
719     {
720     settings.resurrection = FALSE;
721     }
722     else
723     {
724     LOG (llevError, "load_settings: Unknown value for resurrection" ": %s\n", cp);
725     }
726     }
727     else if (!strcasecmp (buf, "set_title"))
728     {
729     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
730     {
731     settings.set_title = TRUE;
732     }
733     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
734     {
735     settings.set_title = FALSE;
736     }
737     else
738     {
739     LOG (llevError, "load_settings: Unknown value for set_title" ": %s\n", cp);
740     }
741     }
742     else if (!strcasecmp (buf, "search_items"))
743     {
744     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
745     {
746     settings.search_items = TRUE;
747     }
748     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
749     {
750     settings.search_items = FALSE;
751     }
752     else
753     {
754     LOG (llevError, "load_settings: Unknown value for search_items" ": %s\n", cp);
755     }
756     }
757     else if (!strcasecmp (buf, "spell_encumbrance"))
758     {
759     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
760     {
761     settings.spell_encumbrance = TRUE;
762     }
763     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
764     {
765     settings.spell_encumbrance = FALSE;
766     }
767     else
768     {
769     LOG (llevError, "load_settings: Unknown value for " "spell_encumbrance: %s\n", cp);
770     }
771     }
772     else if (!strcasecmp (buf, "spell_failure_effects"))
773     {
774     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
775     {
776     settings.spell_failure_effects = TRUE;
777     }
778     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
779     {
780     settings.spell_failure_effects = FALSE;
781     }
782     else
783     {
784     LOG (llevError, "load_settings: Unknown value for " "spell_failure_effects: %s\n", cp);
785     }
786     }
787     else if (!strcasecmp (buf, "casting_time"))
788     {
789     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
790     {
791     settings.casting_time = TRUE;
792     }
793     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
794     {
795     settings.casting_time = FALSE;
796     }
797     else
798     {
799     LOG (llevError, "load_settings: Unknown value for " "casting_time: %s\n", cp);
800     }
801     }
802     else if (!strcasecmp (buf, "real_wiz"))
803     {
804     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
805     {
806     settings.real_wiz = TRUE;
807     }
808     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
809     {
810     settings.real_wiz = FALSE;
811     }
812     else
813     {
814     LOG (llevError, "load_settings: Unknown value for " "real_wiz: %s\n", cp);
815     }
816     }
817     else if (!strcasecmp (buf, "recycle_tmp_maps"))
818     {
819     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
820     {
821     settings.recycle_tmp_maps = TRUE;
822     }
823     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
824     {
825     settings.recycle_tmp_maps = FALSE;
826     }
827     else
828     {
829     LOG (llevError, "load_settings: Unknown value for " "recycle_tmp_maps: %s\n", cp);
830     }
831     }
832     else if (!strcasecmp (buf, "explore_mode"))
833     {
834     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
835     {
836     settings.explore_mode = TRUE;
837     }
838     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
839     {
840     settings.explore_mode = FALSE;
841     }
842     else
843     {
844     LOG (llevError, "load_settings: Unknown value for " "explore_mode: %s\n", cp);
845     }
846     }
847     else if (!strcasecmp (buf, "who_format"))
848     {
849     if (has_val)
850     strcpy (settings.who_format, cp);
851     }
852     else if (!strcasecmp (buf, "who_wiz_format"))
853     {
854     if (has_val)
855     strcpy (settings.who_wiz_format, cp);
856     }
857     else if (!strcasecmp (buf, "spellpoint_level_depend"))
858     {
859     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
860     {
861     settings.spellpoint_level_depend = TRUE;
862     }
863     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
864     {
865     settings.spellpoint_level_depend = FALSE;
866     }
867     else
868     {
869     LOG (llevError, "load_settings: Unknown value for " "spellpoint_level_depend: %s\n", cp);
870     }
871     }
872     else if (!strcasecmp (buf, "stat_loss_on_death"))
873     {
874     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
875     {
876     settings.stat_loss_on_death = TRUE;
877     }
878     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
879     {
880     settings.stat_loss_on_death = FALSE;
881     }
882     else
883     {
884     LOG (llevError, "load_settings: Unknown value for " "stat_loss_on_death: %s\n", cp);
885     }
886     }
887     else if (!strcasecmp (buf, "use_permanent_experience"))
888     {
889     LOG (llevError, "use_permanent_experience is deprecated, use" "permenent_experience_percentage instead\n");
890     }
891     else if (!strcasecmp (buf, "permanent_experience_percentage"))
892     {
893     int val = atoi (cp);
894    
895     if (val < 0 || val > 100)
896     LOG (llevError, "load_settings: permenent_experience_percentage" "must be between 0 and 100, %d is invalid\n", val);
897     else
898     settings.permanent_exp_ratio = val;
899     }
900     else if (!strcasecmp (buf, "death_penalty_percentage"))
901     {
902     int val = atoi (cp);
903    
904     if (val < 0 || val > 100)
905     LOG (llevError, "load_settings: death_penalty_percentage" "must be between 0 and 100, %d is invalid\n", val);
906     else
907     settings.death_penalty_ratio = val;
908     }
909     else if (!strcasecmp (buf, "death_penalty_levels"))
910     {
911     int val = atoi (cp);
912    
913     if (val < 0 || val > 255)
914     LOG (llevError, "load_settings: death_penalty_levels" "can not be negative, %d is invalid\n", val);
915     else
916     settings.death_penalty_level = val;
917     }
918     else if (!strcasecmp (buf, "balanced_stat_loss"))
919     {
920     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
921     {
922     settings.balanced_stat_loss = TRUE;
923     }
924     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
925     {
926     settings.balanced_stat_loss = FALSE;
927     }
928     else
929     {
930     LOG (llevError, "load_settings: Unknown value for " "balanced_stat_loss: %s\n", cp);
931     }
932     }
933     else if (!strcasecmp (buf, "simple_exp"))
934     {
935     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
936     {
937     settings.simple_exp = TRUE;
938     }
939     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
940     {
941     settings.simple_exp = FALSE;
942     }
943     else
944     {
945     LOG (llevError, "load_settings: Unknown value for simple_exp: %s\n", cp);
946     }
947     }
948     else if (!strcasecmp (buf, "item_power_factor"))
949     {
950     float tmp = atof (cp);
951    
952     if (tmp < 0)
953     LOG (llevError, "load_settings: item_power_factor must be a positive number (%f < 0)\n", tmp);
954     else
955     settings.item_power_factor = tmp;
956     }
957     else if (!strcasecmp (buf, "pk_luck_penalty"))
958     {
959     sint16 val = atoi (cp);
960    
961     if (val < -100 || val > 100)
962     LOG (llevError, "load_settings: pk_luck_penalty must be between -100 and 100" ", %d is invalid\n", val);
963     else
964     settings.pk_luck_penalty = val;
965     }
966     else if (!strcasecmp (buf, "set_friendly_fire"))
967     {
968     int val = atoi (cp);
969 elmex 1.1
970     #if COZY_SERVER
971 root 1.9 if (val < 0 || val > 100)
972     LOG (llevError, "load_settings: set_friendly_fire must be between 0 an 100" ", %d is invalid\n", val);
973 elmex 1.1 #else
974 root 1.9 if (val < 1 || val > 100)
975     LOG (llevError, "load_settings: set_friendly_fire must be between 1 an 100" ", %d is invalid\n", val);
976 elmex 1.1 #endif
977 root 1.9 else
978     settings.set_friendly_fire = val;
979     }
980     else if (!strcasecmp (buf, "armor_max_enchant"))
981     {
982     int max_e = atoi (cp);
983    
984     if (max_e <= 0)
985     LOG (llevError, "load_settings: armor_max_enchant is %d\n", max_e);
986     else
987 elmex 1.1 settings.armor_max_enchant = max_e;
988 root 1.9 }
989     else if (!strcasecmp (buf, "armor_weight_reduction"))
990     {
991     int wr = atoi (cp);
992    
993     if (wr < 0)
994     LOG (llevError, "load_settings: armor_weight_reduction is %d\n", wr);
995     else
996 elmex 1.1 settings.armor_weight_reduction = wr;
997     }
998 root 1.9 else if (!strcasecmp (buf, "armor_weight_linear"))
999     {
1000     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
1001     {
1002     settings.armor_weight_linear = TRUE;
1003     }
1004     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
1005     {
1006     settings.armor_weight_linear = FALSE;
1007     }
1008     else
1009     {
1010     LOG (llevError, "load_settings: unknown value for armor_weight_linear: %s\n", cp);
1011     }
1012    
1013     }
1014     else if (!strcasecmp (buf, "armor_speed_improvement"))
1015     {
1016     int wr = atoi (cp);
1017 elmex 1.1
1018 root 1.9 if (wr < 0)
1019     LOG (llevError, "load_settings: armor_speed_improvement is %d\n", wr);
1020     else
1021 elmex 1.1 settings.armor_speed_improvement = wr;
1022     }
1023 root 1.9 else if (!strcasecmp (buf, "armor_speed_linear"))
1024     {
1025     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
1026     {
1027     settings.armor_speed_linear = TRUE;
1028     }
1029     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
1030     {
1031     settings.armor_speed_linear = FALSE;
1032     }
1033     else
1034     {
1035     LOG (llevError, "load_settings: unknown value for armor_speed_linear: %s\n", cp);
1036     }
1037    
1038     }
1039     else if (!strcasecmp (buf, "no_player_stealing"))
1040     {
1041     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
1042     {
1043     settings.no_player_stealing = TRUE;
1044     }
1045     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
1046     {
1047     settings.no_player_stealing = FALSE;
1048     }
1049     else
1050     {
1051     LOG (llevError, "load_settings: unknown value for no_player_stealing: %s\n", cp);
1052     }
1053 elmex 1.1
1054 root 1.9 }
1055     else if (!strcasecmp (buf, "create_home_portals"))
1056     {
1057     if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
1058     {
1059     settings.create_home_portals = TRUE;
1060     }
1061     else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
1062     {
1063     settings.create_home_portals = FALSE;
1064     }
1065     else
1066     {
1067     LOG (llevError, "load_settings: Unknown value for create_home_portals: %s\n", cp);
1068     }
1069    
1070     }
1071     else
1072     {
1073     LOG (llevError, "Unknown value in settings file: %s\n", buf);
1074 root 1.4 }
1075 elmex 1.1 }
1076 root 1.9 close_and_delete (fp, comp);
1077 elmex 1.1 }
1078    
1079    
1080     /*
1081     * init() is called only once, when starting the program.
1082     */
1083    
1084 root 1.9 void
1085     init (int argc, char **argv)
1086     {
1087     init_done = 0; /* Must be done before init_signal() */
1088     logfile = stderr;
1089     parse_args (argc, argv, 1); /* First arg pass - right now it does
1090 root 1.4 * nothing, but in future specifying the
1091     * LibDir in this pass would be reasonable*/
1092 elmex 1.1
1093 root 1.9 init_library (); /* Must be called early */
1094     load_settings (); /* Load the settings file */
1095     init_weather ();
1096     load_materials ();
1097     parse_args (argc, argv, 2);
1098     fprintf (logfile, "Welcome to CrossFire, v%s\n", VERSION);
1099     fprintf (logfile, "Copyright (C) 1994 Mark Wedel.\n");
1100     fprintf (logfile, "Copyright (C) 1992 Frank Tore Johansen.\n");
1101    
1102     if (strcmp (settings.dm_mail, "") != 0)
1103     {
1104     fprintf (logfile, "Maintained locally by: %s\n", settings.dm_mail);
1105     fprintf (logfile, "Questions and bugs should be mailed to above address.\n");
1106 elmex 1.1 }
1107 root 1.11
1108 root 1.9 SRANDOM (time (NULL));
1109 elmex 1.1
1110 root 1.9 init_startup (); /* Write (C), check shutdown/forbid files */
1111 root 1.11 init_uuid ();
1112 root 1.9 init_signals (); /* Sets up signal interceptions */
1113     init_commands (); /* Sort command tables */
1114     read_map_log (); /* Load up the old temp map files */
1115     init_skills ();
1116    
1117     parse_args (argc, argv, 3);
1118    
1119     #ifndef WIN32 /* ***win32: no BecomeDaemon in windows */
1120     if (settings.daemonmode)
1121     logfile = BecomeDaemon (settings.logfilename[0] == '\0' ? "logfile" : settings.logfilename);
1122 elmex 1.1 #endif
1123    
1124 root 1.9 init_beforeplay ();
1125     init_ericserver ();
1126     metaserver_init ();
1127     init_done = 1;
1128 elmex 1.1 }
1129    
1130 root 1.9 void
1131     usage (void)
1132     {
1133     (void) fprintf (logfile, "Usage: crossfire [-h] [-<flags>]...\n");
1134 elmex 1.1 }
1135    
1136 root 1.9 void
1137     help (void)
1138     {
1139    
1140 elmex 1.1 /* The information in usage is redundant with what is given below, so why call it? */
1141 root 1.9
1142 elmex 1.1 /* usage();*/
1143 root 1.9 printf ("Flags:\n");
1144     printf (" -csport <port> Specifies the port to use for the new client/server code.\n");
1145     printf (" -d Turns on some debugging.\n");
1146     printf (" +d Turns off debugging (useful if server compiled with debugging\n");
1147     printf (" as default).\n");
1148     printf (" -detach The server will go in the background, closing all\n");
1149     printf (" connections to the tty.\n");
1150     printf (" -h Display this information.\n");
1151     printf (" -log <file> Specifies which file to send output to.\n");
1152     printf (" Only has meaning if -detach is specified.\n");
1153     printf (" -mon Turns on monster debugging.\n");
1154     printf (" -o Prints out info on what was defined at compile time.\n");
1155     printf (" -s Display the high-score list.\n");
1156     printf (" -score <name or class> Displays all high scores with matching name/class.\n");
1157     printf (" -v Print version and contributors.\n");
1158     printf (" -data Sets the lib dir (archetypes, treasures, etc.)\n");
1159     printf (" -local Read/write local data (hiscore, unique items, etc.)\n");
1160     printf (" -maps Sets the directory for maps.\n");
1161     printf (" -arch Sets the archetype file to use.\n");
1162     printf (" -regions Sets the regions file to use.\n");
1163     printf (" -playerdir Sets the directory for the player files.\n");
1164     printf (" -templatedir Sets the directory for template generate maps.\n");
1165     printf (" -treasures Sets the treasures file to use.\n");
1166     printf (" -uniquedir Sets the unique items/maps directory.\n");
1167     printf (" -tmpdir Sets the directory for temporary files (mostly maps.)\n");
1168     printf (" -m Lists out suggested experience for all monsters.\n");
1169     printf (" -m2 Dumps out abilities.\n");
1170     printf (" -m3 Dumps out artifact information.\n");
1171     printf (" -m4 Dumps out spell information.\n");
1172     printf (" -m5 Dumps out skill information.\n");
1173     printf (" -m6 Dumps out race information.\n");
1174     printf (" -m7 Dumps out alchemy information.\n");
1175     printf (" -m8 Dumps out gods information.\n");
1176     printf (" -m9 Dumps out more alchemy information (formula checking).\n");
1177     printf (" -mt <name> Dumps out list of treasures for a monster.\n");
1178     exit (0);
1179     }
1180    
1181     void
1182     init_beforeplay (void)
1183     {
1184     init_archetypes (); /* If not called before, reads all archetypes from file */
1185     init_artifacts (); /* If not called before, reads all artifacts from file */
1186     init_spells (); /* If not called before, links archtypes used by spells */
1187     init_regions (); /* If not called before, reads all regions from file */
1188     init_archetype_pointers (); /* Setup global pointers to archetypes */
1189     init_races (); /* overwrite race designations using entries in lib/races file */
1190     init_gods (); /* init linked list of gods from archs */
1191     init_readable (); /* inits useful arrays for readable texts */
1192     init_formulae (); /* If not called before, reads formulae from file */
1193    
1194     switch (settings.dumpvalues)
1195     {
1196 root 1.10 case 1:
1197     print_monsters ();
1198     exit (0);
1199     case 2:
1200     dump_abilities ();
1201     exit (0);
1202     case 3:
1203     dump_artifacts ();
1204     exit (0);
1205     case 4:
1206     dump_spells ();
1207     exit (0);
1208     case 5:
1209     exit (0);
1210     case 6:
1211     dump_races ();
1212     exit (0);
1213     case 7:
1214     dump_alchemy ();
1215     exit (0);
1216     case 9:
1217     dump_alchemy_costs ();
1218     exit (0);
1219     case 10:
1220     dump_monster_treasure (settings.dumparg);
1221     exit (0);
1222 root 1.9 }
1223 elmex 1.1 }
1224    
1225 root 1.9 void
1226     init_startup (void)
1227     {
1228 elmex 1.1 char buf[MAX_BUF];
1229     FILE *fp;
1230     int comp;
1231    
1232     #ifdef SHUTDOWN_FILE
1233 root 1.9 sprintf (buf, "%s/%s", settings.confdir, SHUTDOWN_FILE);
1234     if ((fp = open_and_uncompress (buf, 0, &comp)) != NULL)
1235     {
1236     while (fgets (buf, MAX_BUF - 1, fp) != NULL)
1237     printf ("%s", buf);
1238     close_and_delete (fp, comp);
1239     exit (1);
1240     }
1241 elmex 1.1 #endif
1242    
1243 root 1.9 if (forbid_play ())
1244     { /* Maybe showing highscore should be allowed? */
1245     LOG (llevError, "CrossFire: Playing not allowed.\n");
1246     exit (-1);
1247     }
1248 elmex 1.1 }
1249    
1250     /*
1251     * compile_info(): activated with the -o flag.
1252     * It writes out information on how Imakefile and config.h was configured
1253     * at compile time.
1254     */
1255    
1256 root 1.9 void
1257     compile_info (void)
1258     {
1259     int i = 0;
1260    
1261     printf ("Non-standard include files:\n");
1262 elmex 1.1 #if !defined (__STRICT_ANSI__) || defined (__sun__)
1263 root 1.9 # if !defined (Mips)
1264     printf ("<stdlib.h>\n");
1265     i = 1;
1266     # endif
1267     # if !defined (MACH) && !defined (sony)
1268     printf ("<malloc.h>\n");
1269     i = 1;
1270     # endif
1271 elmex 1.1 #endif
1272     #ifndef __STRICT_ANSI__
1273 root 1.9 # ifndef MACH
1274     printf ("<memory.h\n");
1275     i = 1;
1276     # endif
1277 elmex 1.1 #endif
1278     #ifndef sgi
1279 root 1.9 printf ("<sys/timeb.h>\n");
1280     i = 1;
1281 elmex 1.1 #endif
1282 root 1.9 if (!i)
1283     printf ("(none)\n");
1284     printf ("Datadir:\t\t%s\n", settings.datadir);
1285     printf ("Localdir:\t\t%s\n", settings.localdir);
1286 elmex 1.1 #ifdef PERM_FILE
1287 root 1.9 printf ("Perm file:\t<ETC>/%s\n", PERM_FILE);
1288 elmex 1.1 #endif
1289     #ifdef SHUTDOWN_FILE
1290 root 1.9 printf ("Shutdown file:\t<ETC>/%s\n", SHUTDOWN_FILE);
1291 elmex 1.1 #endif
1292 root 1.9 printf ("Save player:\t<true>\n");
1293     printf ("Save mode:\t%4.4o\n", SAVE_MODE);
1294     printf ("Playerdir:\t<VAR>/%s\n", settings.playerdir);
1295     printf ("Itemsdir:\t<VAR>/%s\n", settings.uniquedir);
1296     printf ("Tmpdir:\t\t%s\n", settings.tmpdir);
1297     printf ("Map max timeout:\t%d\n", MAP_MAXTIMEOUT);
1298     printf ("Max objects:\t%d\n", MAX_OBJECTS);
1299 elmex 1.1 #ifdef USE_CALLOC
1300 root 1.9 printf ("Use_calloc:\t<true>\n");
1301 elmex 1.1 #else
1302 root 1.9 printf ("Use_calloc:\t<false>\n");
1303 elmex 1.1 #endif
1304    
1305     #ifdef X_EDITOR
1306 root 1.9 printf ("Editor:\t\t%s\n", X_EDITOR);
1307 elmex 1.1 #endif
1308    
1309 root 1.9 printf ("Max_time:\t%d\n", MAX_TIME);
1310 elmex 1.1
1311 root 1.9 #ifdef WIN32 /* ***win32 compile_info(): remove execl... */
1312     printf ("Logfilename:\t%s\n", settings.logfilename);
1313     exit (0);
1314 elmex 1.1 #else
1315 root 1.9 execl ("/bin/uname", "uname", "-a", NULL);
1316     LOG (llevError, "Oops, shouldn't have gotten here: execl(/bin/uname) failed: %s\n", strerror (errno));
1317     exit (-1);
1318 elmex 1.1 #endif
1319     }
1320    
1321     /* Signal handlers: */
1322    
1323 root 1.9 void
1324     rec_sigsegv (int i)
1325     {
1326     LOG (llevError, "\nSIGSEGV received.\n");
1327     fatal_signal (1, 1);
1328 elmex 1.1 }
1329    
1330 root 1.9 void
1331     rec_sigint (int i)
1332     {
1333     LOG (llevInfo, "\nSIGINT received.\n");
1334     fatal_signal (0, 1);
1335 elmex 1.1 }
1336    
1337 root 1.9 void
1338     rec_sighup (int i)
1339     {
1340     LOG (llevInfo, "\nSIGHUP received\n");
1341     if (init_done)
1342     {
1343     emergency_save (0);
1344     cleanup ();
1345     }
1346     exit (0);
1347 elmex 1.1 }
1348    
1349 root 1.9 void
1350     rec_sigquit (int i)
1351     {
1352     LOG (llevInfo, "\nSIGQUIT received\n");
1353     fatal_signal (1, 1);
1354 elmex 1.1 }
1355    
1356 root 1.9 void
1357     rec_sigbus (int i)
1358     {
1359 elmex 1.1 #ifdef SIGBUS
1360 root 1.9 LOG (llevError, "\nSIGBUS received\n");
1361     fatal_signal (1, 1);
1362 elmex 1.1 #endif
1363     }
1364    
1365 root 1.9 void
1366     rec_sigterm (int i)
1367     {
1368     LOG (llevInfo, "\nSIGTERM received\n");
1369     fatal_signal (0, 1);
1370     }
1371    
1372     void
1373     fatal_signal (int make_core, int close_sockets)
1374     {
1375     if (init_done)
1376     {
1377     emergency_save (0);
1378     clean_tmp_files ();
1379     }
1380     if (make_core)
1381     abort ();
1382     exit (0);
1383     }
1384    
1385     void
1386     init_signals (void)
1387     {
1388     #ifndef WIN32 /* init_signals() remove signals */
1389     signal (SIGHUP, rec_sighup);
1390     signal (SIGINT, rec_sigint);
1391     signal (SIGQUIT, rec_sigquit);
1392     signal (SIGSEGV, rec_sigsegv);
1393     signal (SIGPIPE, SIG_IGN);
1394     # ifdef SIGBUS
1395     signal (SIGBUS, rec_sigbus);
1396     # endif
1397     signal (SIGTERM, rec_sigterm);
1398 elmex 1.1 #endif
1399     }
1400    
1401     /* init_races() - reads the races file in the lib/ directory, then
1402     * overwrites old 'race' entries. This routine allow us to quickly
1403     * re-configure the 'alignment' of monsters, objects. Useful for
1404     * putting together lists of creatures, etc that belong to gods.
1405     */
1406 root 1.9
1407     void
1408     init_races (void)
1409 elmex 1.8 {
1410 elmex 1.1 FILE *file;
1411     char race[MAX_BUF], fname[MAX_BUF], buf[MAX_BUF], *cp, variable[MAX_BUF];
1412 elmex 1.8 archetype *mon = NULL;
1413     static int init_done = 0;
1414 elmex 1.1
1415 elmex 1.8 if (init_done)
1416 elmex 1.1 return;
1417 elmex 1.8 init_done = 1;
1418 root 1.10 first_race = 0;
1419 elmex 1.1
1420 elmex 1.8 sprintf (fname, "%s/races", settings.datadir);
1421     LOG (llevDebug, "Reading races from %s...", fname);
1422     if (!(file = fopen (fname, "r")))
1423     {
1424 root 1.9 LOG (llevError, "Cannot open races file %s: %s\n", fname, strerror (errno));
1425 elmex 1.8 return;
1426 elmex 1.1 }
1427 elmex 1.8
1428     while (fgets (buf, MAX_BUF, file) != NULL)
1429     {
1430     int set_race = 1, set_list = 1;
1431 root 1.9
1432 elmex 1.8 if (*buf == '#')
1433     continue;
1434 root 1.10
1435 elmex 1.8 if ((cp = strchr (buf, '\n')) != NULL)
1436     *cp = '\0';
1437 root 1.10
1438 elmex 1.8 cp = buf;
1439     while (*cp == ' ' || *cp == '!' || *cp == '@')
1440     {
1441     if (*cp == '!')
1442     set_race = 0;
1443     if (*cp == '@')
1444     set_list = 0;
1445     cp++;
1446     }
1447 root 1.10
1448 elmex 1.8 if (sscanf (cp, "RACE %s", variable))
1449     { /* set new race value */
1450     strcpy (race, variable);
1451 root 1.4 }
1452 elmex 1.8 else
1453     {
1454     char *cp1;
1455 root 1.9
1456 elmex 1.8 /* Take out beginning spaces */
1457     for (cp1 = cp; *cp1 == ' '; cp1++)
1458     ;
1459     /* Remove newline and trailing spaces */
1460     for (cp1 = cp + strlen (cp) - 1; *cp1 == '\n' || *cp1 == ' '; cp1--)
1461     {
1462     *cp1 = '\0';
1463     if (cp == cp1)
1464     break;
1465     }
1466    
1467     if (cp[strlen (cp) - 1] == '\n')
1468     cp[strlen (cp) - 1] = '\0';
1469 root 1.10
1470 elmex 1.8 /* set creature race to race value */
1471 root 1.12 if ((mon = archetype::find (cp)) == NULL)
1472 elmex 1.8 LOG (llevError, "\nCreature %s in race file lacks archetype", cp);
1473     else
1474     {
1475 root 1.9 if (set_race && (!mon->clone.race || strcmp (mon->clone.race, race)))
1476 elmex 1.8 {
1477     if (mon->clone.race)
1478 root 1.10 LOG (llevDebug, "\n Resetting race to %s from %s for archetype %s", race, &mon->clone.race, &mon->name);
1479    
1480 elmex 1.8 mon->clone.race = race;
1481 elmex 1.1 }
1482 root 1.10
1483 elmex 1.8 /* if the arch is a monster, add it to the race list */
1484     if (set_list && QUERY_FLAG (&mon->clone, FLAG_MONSTER))
1485     add_to_racelist (race, &mon->clone);
1486     }
1487 elmex 1.1 }
1488     }
1489 root 1.10
1490 elmex 1.8 fclose (file);
1491     LOG (llevDebug, "done.\n");
1492 elmex 1.1 }
1493    
1494 root 1.9 void
1495     dump_races (void)
1496     {
1497     racelink *list;
1498     objectlink *tmp;
1499    
1500     for (list = first_race; list; list = list->next)
1501     {
1502 root 1.10 fprintf (stderr, "\nRACE %s:\t", &list->name);
1503 root 1.9 for (tmp = list->member; tmp; tmp = tmp->next)
1504     fprintf (stderr, "%s(%d), ", &tmp->ob->arch->name, tmp->ob->level);
1505 elmex 1.1 }
1506 root 1.10
1507 root 1.9 fprintf (stderr, "\n");
1508 elmex 1.1 }
1509    
1510 root 1.9 void
1511     add_to_racelist (const char *race_name, object *op)
1512     {
1513 elmex 1.1 racelink *race;
1514 root 1.9
1515     if (!op || !race_name)
1516     return;
1517 root 1.10
1518 root 1.9 race = find_racelink (race_name);
1519    
1520     if (!race)
1521     { /* add in a new race list */
1522     race = get_racelist ();
1523     race->next = first_race;
1524     first_race = race;
1525     race->name = race_name;
1526     }
1527    
1528     if (race->member->ob)
1529     {
1530     objectlink *tmp = get_objectlink ();
1531    
1532     tmp->next = race->member;
1533     race->member = tmp;
1534     }
1535 root 1.10
1536 elmex 1.1 race->nrof++;
1537     race->member->ob = op;
1538     }
1539    
1540 root 1.9 racelink *
1541     get_racelist ()
1542     {
1543 root 1.10 racelink *list = new racelink;
1544 root 1.9
1545 root 1.10 list->name = 0;
1546 root 1.9 list->nrof = 0;
1547 root 1.10 list->next = 0;
1548 root 1.9 list->member = get_objectlink ();
1549 elmex 1.1
1550     return list;
1551     }
1552 root 1.9
1553     racelink *
1554     find_racelink (const char *name)
1555     {
1556 root 1.10 if (name)
1557     for (racelink *link = first_race; link; link = link->next)
1558     if (!link->name || !strcmp (name, link->name))
1559     return link;
1560 root 1.9
1561 root 1.10 return 0;
1562 elmex 1.1 }