ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.9
Committed: Sun Sep 10 15:59:57 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +1273 -852 lines
Log Message:
indent

File Contents

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