ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.57
Committed: Mon Aug 20 22:09:04 2007 UTC (16 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.56: +5 -1 lines
Log Message:
- include experience table as our first generic resource file
- swizzle init code around
- do not calculate checksum in cfutil but when loading the facedata.
  this increases loading time by 20%, but since thats just 1.2s instead
  of 1.0s its not worth the extra effort of doing it beforehand.

File Contents

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