ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.c
Revision: 1.6
Committed: Fri Apr 28 13:56:26 2006 UTC (18 years ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: LAST_C_VERSION, difficulty_fix_merge_060810_2300
Branch point for: difficulty_fix
Changes since 1.5: +9 -0 lines
Log Message:
Adding setting to allow for portals in apartments and other personal maps.

File Contents

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