ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.c
Revision: 1.1.1.2 (vendor branch)
Committed: Wed Feb 22 18:03:21 2006 UTC (18 years, 3 months ago) by elmex
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_02_22
Changes since 1.1.1.1: +2 -2 lines
Log Message:
cvs -z7 -d:ext:elmex@cvs.schmorp.de:/schmorpforge import cf.schmorp.de UPSTREAM UPSTREAM_2006_02_22

File Contents

# Content
1 /*
2 * static char *rcsid_init_c =
3 * "$Id: init.c,v 1.73 2006/02/10 23:59:27 akirschbaum Exp $";
4 */
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 const char *cmd_option; /* how it is called on the command line */
89 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 mt->next = NULL;
321 LOG(llevDebug, "Done.\n");
322 fclose(fp);
323 }
324
325 /* This loads the settings file. There could be debate whether this should
326 * be here or in the common directory - but since only the server needs this
327 * information, having it here probably makes more sense.
328 */
329 static void load_settings(void)
330 {
331 char buf[MAX_BUF],*cp;
332 int has_val,comp;
333 FILE *fp;
334
335 sprintf(buf,"%s/settings",settings.confdir);
336
337 /* We don't require a settings file at current time, but down the road,
338 * there will probably be so many values that not having a settings file
339 * will not be a good thing.
340 */
341 if ((fp = open_and_uncompress(buf, 0, &comp)) == NULL) {
342 LOG(llevError,"Warning: No settings file found\n");
343 return;
344 }
345 while (fgets(buf, MAX_BUF-1, fp) != NULL) {
346 if (buf[0] == '#') continue;
347 /* eliminate newline */
348 if ((cp=strrchr(buf,'\n'))!=NULL) *cp='\0';
349
350 /* Skip over empty lines */
351 if (buf[0] == 0) continue;
352
353 /* Skip all the spaces and set them to nulls. If not space,
354 * set cp to "" to make strcpy's and the like easier down below.
355 */
356 if ((cp = strchr(buf,' '))!=NULL) {
357 while (*cp==' ') *cp++=0;
358 has_val=1;
359 } else {
360 cp="";
361 has_val=0;
362 }
363
364 if (!strcasecmp(buf,"metaserver_notification")) {
365 if (!strcasecmp(cp,"on") || !strcasecmp(cp,"true")) {
366 settings.meta_on=TRUE;
367 } else if (!strcasecmp(cp,"off") || !strcasecmp(cp,"false")) {
368 settings.meta_on=FALSE;
369 } else {
370 LOG(llevError,"load_settings: Unknown value for metaserver_notification: %s\n",
371 cp);
372 }
373 } else if (!strcasecmp(buf,"metaserver_server")) {
374 if (has_val) strcpy(settings.meta_server, cp);
375 else
376 LOG(llevError,"load_settings: metaserver_server must have a value.\n");
377 } else if (!strcasecmp(buf,"motd")) {
378 if (has_val)
379 strcpy(settings.motd, cp);
380 else
381 LOG(llevError,"load_settings: motd must have a value.\n");
382 } else if (!strcasecmp(buf,"dm_mail")) {
383 if (has_val)
384 strcpy(settings.dm_mail, cp);
385 else
386 LOG(llevError,"load_settings: dm_mail must have a value.\n");
387 } else if (!strcasecmp(buf,"metaserver_host")) {
388 if (has_val) strcpy(settings.meta_host, cp);
389 else
390 LOG(llevError,"load_settings: metaserver_host must have a value.\n");
391 } else if (!strcasecmp(buf,"metaserver_port")) {
392 int port = atoi(cp);
393
394 if (port<1 || port>65535)
395 LOG(llevError,"load_settings: metaserver_port must be between 1 and 65535, %d is invalid\n",
396 port);
397 else settings.meta_port = port;
398 } else if (!strcasecmp(buf,"metaserver_comment")) {
399 strcpy(settings.meta_comment, cp);
400 } else if (!strcasecmp(buf, "worldmapstartx")) {
401 int size = atoi(cp);
402
403 if (size < 0)
404 LOG(llevError, "load_settings: worldmapstartx must be at least "
405 "0, %d is invalid\n", size);
406 else
407 settings.worldmapstartx = size;
408 } else if (!strcasecmp(buf, "worldmapstarty")) {
409 int size = atoi(cp);
410
411 if (size < 0)
412 LOG(llevError, "load_settings: worldmapstarty must be at least "
413 "0, %d is invalid\n", size);
414 else
415 settings.worldmapstarty = size;
416 } else if (!strcasecmp(buf, "worldmaptilesx")) {
417 int size = atoi(cp);
418
419 if (size < 1)
420 LOG(llevError, "load_settings: worldmaptilesx must be greater "
421 "than 1, %d is invalid\n", size);
422 else
423 settings.worldmaptilesx = size;
424 } else if (!strcasecmp(buf, "worldmaptilesy")) {
425 int size = atoi(cp);
426
427 if (size < 1)
428 LOG(llevError, "load_settings: worldmaptilesy must be greater "
429 "than 1, %d is invalid\n", size);
430 else
431 settings.worldmaptilesy = size;
432 } else if (!strcasecmp(buf, "worldmaptilesizex")) {
433 int size = atoi(cp);
434
435 if (size < 1)
436 LOG(llevError, "load_settings: worldmaptilesizex must be "
437 "greater than 1, %d is invalid\n", size);
438 else
439 settings.worldmaptilesizex = size;
440 } else if (!strcasecmp(buf, "worldmaptilesizey")) {
441 int size = atoi(cp);
442
443 if (size < 1)
444 LOG(llevError, "load_settings: worldmaptilesizey must be "
445 "greater than 1, %d is invalid\n", size);
446 else
447 settings.worldmaptilesizey = size;
448 } else if (!strcasecmp(buf, "dynamiclevel")) {
449 int lev = atoi(cp);
450
451 if (lev < 0)
452 LOG(llevError, "load_settings: dynamiclevel must be "
453 "at least 0, %d is invalid\n", lev);
454 else
455 settings.dynamiclevel = lev;
456 } else if (!strcasecmp(buf, "fastclock")) {
457 int lev = atoi(cp);
458
459 if (lev < 0)
460 LOG(llevError, "load_settings: fastclock must be at least 0"
461 ", %d is invalid\n", lev);
462 else
463 settings.fastclock = lev;
464 } else if (!strcasecmp(buf, "not_permadeth")) {
465 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
466 settings.not_permadeth=TRUE;
467 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
468 settings.not_permadeth=FALSE;
469 } else {
470 LOG(llevError, "load_settings: Unknown value for not_permadeth"
471 ": %s\n", cp);
472 }
473 } else if (!strcasecmp(buf, "resurrection")) {
474 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
475 settings.resurrection=TRUE;
476 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
477 settings.resurrection=FALSE;
478 } else {
479 LOG(llevError, "load_settings: Unknown value for resurrection"
480 ": %s\n", cp);
481 }
482 } else if (!strcasecmp(buf, "set_title")) {
483 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
484 settings.set_title=TRUE;
485 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
486 settings.set_title=FALSE;
487 } else {
488 LOG(llevError, "load_settings: Unknown value for set_title"
489 ": %s\n", cp);
490 }
491 } else if (!strcasecmp(buf, "search_items")) {
492 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
493 settings.search_items=TRUE;
494 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
495 settings.search_items=FALSE;
496 } else {
497 LOG(llevError, "load_settings: Unknown value for search_items"
498 ": %s\n", cp);
499 }
500 } else if (!strcasecmp(buf, "spell_encumbrance")) {
501 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
502 settings.spell_encumbrance=TRUE;
503 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
504 settings.spell_encumbrance=FALSE;
505 } else {
506 LOG(llevError, "load_settings: Unknown value for "
507 "spell_encumbrance: %s\n", cp);
508 }
509 } else if (!strcasecmp(buf, "spell_failure_effects")) {
510 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
511 settings.spell_failure_effects=TRUE;
512 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
513 settings.spell_failure_effects=FALSE;
514 } else {
515 LOG(llevError, "load_settings: Unknown value for "
516 "spell_failure_effects: %s\n", cp);
517 }
518 } else if (!strcasecmp(buf, "casting_time")) {
519 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
520 settings.casting_time=TRUE;
521 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
522 settings.casting_time=FALSE;
523 } else {
524 LOG(llevError, "load_settings: Unknown value for "
525 "casting_time: %s\n", cp);
526 }
527 } else if (!strcasecmp(buf, "real_wiz")) {
528 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
529 settings.real_wiz=TRUE;
530 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
531 settings.real_wiz=FALSE;
532 } else {
533 LOG(llevError, "load_settings: Unknown value for "
534 "real_wiz: %s\n", cp);
535 }
536 } else if (!strcasecmp(buf, "recycle_tmp_maps")) {
537 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
538 settings.recycle_tmp_maps=TRUE;
539 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
540 settings.recycle_tmp_maps=FALSE;
541 } else {
542 LOG(llevError, "load_settings: Unknown value for "
543 "recycle_tmp_maps: %s\n", cp);
544 }
545 } else if (!strcasecmp(buf, "explore_mode")) {
546 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
547 settings.explore_mode=TRUE;
548 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
549 settings.explore_mode=FALSE;
550 } else {
551 LOG(llevError, "load_settings: Unknown value for "
552 "explore_mode: %s\n", cp);
553 }
554 } else if (!strcasecmp(buf,"who_format")) {
555 if (has_val)
556 strcpy(settings.who_format, cp);
557 } else if (!strcasecmp(buf,"who_wiz_format")) {
558 if (has_val)
559 strcpy(settings.who_wiz_format, cp);
560 } else if (!strcasecmp(buf, "spellpoint_level_depend")) {
561 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
562 settings.spellpoint_level_depend=TRUE;
563 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
564 settings.spellpoint_level_depend=FALSE;
565 } else {
566 LOG(llevError, "load_settings: Unknown value for "
567 "spellpoint_level_depend: %s\n", cp);
568 }
569 } else if (!strcasecmp(buf, "stat_loss_on_death")) {
570 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
571 settings.stat_loss_on_death=TRUE;
572 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
573 settings.stat_loss_on_death=FALSE;
574 } else {
575 LOG(llevError, "load_settings: Unknown value for "
576 "stat_loss_on_death: %s\n", cp);
577 }
578 } else if (!strcasecmp(buf, "use_permanent_experience")) {
579 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
580 settings.use_permanent_experience=TRUE;
581 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
582 settings.use_permanent_experience=FALSE;
583 } else {
584 LOG(llevError, "load_settings: Unknown value for "
585 "use_permanent_experience: %s\n", cp);
586 }
587 } else if (!strcasecmp(buf, "balanced_stat_loss")) {
588 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
589 settings.balanced_stat_loss=TRUE;
590 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
591 settings.balanced_stat_loss=FALSE;
592 } else {
593 LOG(llevError, "load_settings: Unknown value for "
594 "balanced_stat_loss: %s\n", cp);
595 }
596 } else if (!strcasecmp(buf,"simple_exp")) {
597 if (!strcasecmp(cp,"on") || !strcasecmp(cp,"true")) {
598 settings.simple_exp=TRUE;
599 } else if (!strcasecmp(cp,"off") || !strcasecmp(cp,"false")) {
600 settings.simple_exp=FALSE;
601 } else {
602 LOG(llevError,"load_settings: Unknown value for simple_exp: %s\n",
603 cp);
604 }
605 } else if (!strcasecmp(buf, "item_power_factor")) {
606 float tmp = atof(cp);
607 if (tmp < 0)
608 LOG(llevError, "load_settings: item_power_factor must be a positive number (%f < 0)\n",
609 tmp);
610 else
611 settings.item_power_factor = tmp;
612 } else if (!strcasecmp(buf, "pk_luck_penalty")) {
613 sint16 val = atoi(cp);
614
615 if (val < -100 || val >100)
616 LOG(llevError, "load_settings: pk_luck_penalty must be between -100 and 100"
617 ", %d is invalid\n", val);
618 else
619 settings.pk_luck_penalty = val;
620 } else if (!strcasecmp(buf, "set_friendly_fire")) {
621 int val = atoi(cp);
622
623 if (val < 1 || val >100)
624 LOG(llevError, "load_settings: set_friendly_fire must be between 1 an 100"
625 ", %d is invalid\n", val);
626 else
627 settings.set_friendly_fire = val;
628 } else if ( !strcasecmp( buf, "armor_max_enchant" ) ) {
629 int max_e = atoi( cp );
630 if ( max_e <= 0 )
631 LOG( llevError, "load_settings: armor_max_enchant is %d\n", max_e );
632 else
633 settings.armor_max_enchant = max_e;
634 } else if ( !strcasecmp( buf, "armor_weight_reduction" ) ) {
635 int wr = atoi( cp );
636 if ( wr < 0 )
637 LOG( llevError, "load_settings: armor_weight_reduction is %d\n", wr );
638 else
639 settings.armor_weight_reduction = wr;
640 } else if ( !strcasecmp( buf, "armor_weight_linear" ) ) {
641 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
642 settings.armor_weight_linear=TRUE;
643 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
644 settings.armor_weight_linear=FALSE;
645 } else {
646 LOG(llevError, "load_settings: unknown value for armor_weight_linear: %s\n", cp);
647 }
648
649 } else if ( !strcasecmp( buf, "armor_speed_improvement" ) ) {
650 int wr = atoi( cp );
651 if ( wr < 0 )
652 LOG( llevError, "load_settings: armor_speed_improvement is %d\n", wr );
653 else
654 settings.armor_speed_improvement = wr;
655 } else if ( !strcasecmp( buf, "armor_speed_linear" ) ) {
656 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
657 settings.armor_speed_linear = TRUE;
658 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
659 settings.armor_speed_linear = FALSE;
660 } else {
661 LOG(llevError, "load_settings: unknown value for armor_speed_linear: %s\n", cp);
662 }
663
664 } else if ( !strcasecmp( buf, "no_player_stealing" ) ) {
665 if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) {
666 settings.no_player_stealing = TRUE;
667 } else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) {
668 settings.no_player_stealing = FALSE;
669 } else {
670 LOG(llevError, "load_settings: unknown value for no_player_stealing: %s\n", cp);
671 }
672
673 } else {
674 LOG(llevError,"Unknown value in settings file: %s\n", buf);
675 }
676 }
677 close_and_delete(fp, comp);
678 }
679
680
681 /*
682 * init() is called only once, when starting the program.
683 */
684
685 void init(int argc, char **argv) {
686
687 init_done=0; /* Must be done before init_signal() */
688 logfile=stderr;
689 parse_args(argc, argv, 1); /* First arg pass - right now it does
690 * nothing, but in future specifying the
691 * LibDir in this pass would be reasonable*/
692
693 init_library(); /* Must be called early */
694 load_settings(); /* Load the settings file */
695 init_weather();
696 load_materials();
697 parse_args(argc, argv, 2);
698 fprintf(logfile,"Welcome to CrossFire, v%s\n",VERSION);
699 fprintf(logfile,"Copyright (C) 1994 Mark Wedel.\n");
700 fprintf(logfile,"Copyright (C) 1992 Frank Tore Johansen.\n");
701
702 if (strcmp(settings.dm_mail, "") != 0) {
703 fprintf(logfile,"Maintained locally by: %s\n", settings.dm_mail);
704 fprintf(logfile,"Questions and bugs should be mailed to above address.\n");
705 }
706 SRANDOM(time(NULL));
707
708 init_startup(); /* Write (C), check shutdown/forbid files */
709 init_signals(); /* Sets up signal interceptions */
710 init_commands(); /* Sort command tables */
711 read_map_log(); /* Load up the old temp map files */
712 init_skills();
713
714 parse_args(argc, argv, 3);
715
716 #ifndef WIN32 /* ***win32: no BecomeDaemon in windows */
717 if (settings.daemonmode)
718 logfile = BecomeDaemon(settings.logfilename[0]=='\0'?"logfile":settings.logfilename);
719 #endif
720
721 init_beforeplay();
722 init_ericserver();
723 metaserver_init();
724 reset_sleep();
725 init_done=1;
726 }
727
728 void usage(void) {
729 (void) fprintf(logfile,
730 "Usage: crossfire [-h] [-<flags>]...\n");
731 }
732
733 void help(void) {
734 /* The information in usage is redundant with what is given below, so why call it? */
735 /* usage();*/
736 printf("Flags:\n");
737 printf(" -csport <port> Specifies the port to use for the new client/server code.\n");
738 printf(" -d Turns on some debugging.\n");
739 printf(" +d Turns off debugging (useful if server compiled with debugging\n");
740 printf(" as default).\n");
741 printf(" -detach The server will go in the background, closing all\n");
742 printf(" connections to the tty.\n");
743 printf(" -h Display this information.\n");
744 printf(" -log <file> Specifies which file to send output to.\n");
745 printf(" Only has meaning if -detach is specified.\n");
746 printf(" -mon Turns on monster debugging.\n");
747 printf(" -o Prints out info on what was defined at compile time.\n");
748 printf(" -s Display the high-score list.\n");
749 printf(" -score <name or class> Displays all high scores with matching name/class.\n");
750 printf(" -v Print version and contributors.\n");
751 printf(" -data Sets the lib dir (archetypes, treasures, etc.)\n");
752 printf(" -local Read/write local data (hiscore, unique items, etc.)\n");
753 printf(" -maps Sets the directory for maps.\n");
754 printf(" -arch Sets the archetype file to use.\n");
755 printf(" -regions Sets the regions file to use.\n");
756 printf(" -playerdir Sets the directory for the player files.\n");
757 printf(" -templatedir Sets the directory for template generate maps.\n");
758 printf(" -treasures Sets the treasures file to use.\n");
759 printf(" -uniquedir Sets the unique items/maps directory.\n");
760 printf(" -tmpdir Sets the directory for temporary files (mostly maps.)\n");
761 printf(" -m Lists out suggested experience for all monsters.\n");
762 printf(" -m2 Dumps out abilities.\n");
763 printf(" -m3 Dumps out artifact information.\n");
764 printf(" -m4 Dumps out spell information.\n");
765 printf(" -m5 Dumps out skill information.\n");
766 printf(" -m6 Dumps out race information.\n");
767 printf(" -m7 Dumps out alchemy information.\n");
768 printf(" -m8 Dumps out gods information.\n");
769 printf(" -m9 Dumps out more alchemy information (formula checking).\n");
770 printf(" -mt <name> Dumps out list of treasures for a monster.\n");
771 exit(0);
772 }
773
774 void init_beforeplay(void) {
775 init_archetypes(); /* If not called before, reads all archetypes from file */
776 init_artifacts(); /* If not called before, reads all artifacts from file */
777 init_spells(); /* If not called before, links archtypes used by spells */
778 init_regions(); /* If not called before, reads all regions from file */
779 init_archetype_pointers(); /* Setup global pointers to archetypes */
780 init_races(); /* overwrite race designations using entries in lib/races file */
781 init_gods(); /* init linked list of gods from archs*/
782 init_readable(); /* inits useful arrays for readable texts */
783 init_formulae(); /* If not called before, reads formulae from file */
784
785 switch(settings.dumpvalues) {
786 case 1:
787 print_monsters();
788 exit(0);
789 case 2:
790 dump_abilities();
791 exit(0);
792 case 3:
793 dump_artifacts();
794 exit(0);
795 case 4:
796 dump_spells();
797 exit(0);
798 case 5:
799 exit(0);
800 case 6:
801 dump_races();
802 exit(0);
803 case 7:
804 dump_alchemy();
805 exit(0);
806 case 8:
807 dump_gods();
808 exit(0);
809 case 9:
810 dump_alchemy_costs();
811 exit(0);
812 case 10:
813 dump_monster_treasure(settings.dumparg);
814 exit(0);
815 }
816 }
817
818 void init_startup(void) {
819 char buf[MAX_BUF];
820 FILE *fp;
821 int comp;
822
823 #ifdef SHUTDOWN_FILE
824 sprintf(buf,"%s/%s",settings.confdir,SHUTDOWN_FILE);
825 if ((fp = open_and_uncompress(buf, 0, &comp)) != NULL) {
826 while (fgets(buf, MAX_BUF-1, fp) != NULL)
827 printf("%s", buf);
828 close_and_delete(fp, comp);
829 exit(1);
830 }
831 #endif
832
833 if (forbid_play()) { /* Maybe showing highscore should be allowed? */
834 LOG(llevError, "CrossFire: Playing not allowed.\n");
835 exit(-1);
836 }
837 }
838
839 /*
840 * compile_info(): activated with the -o flag.
841 * It writes out information on how Imakefile and config.h was configured
842 * at compile time.
843 */
844
845 void compile_info(void) {
846 int i=0;
847 printf("Non-standard include files:\n");
848 #if !defined (__STRICT_ANSI__) || defined (__sun__)
849 #if !defined (Mips)
850 printf("<stdlib.h>\n");
851 i=1;
852 #endif
853 #if !defined (MACH) && !defined (sony)
854 printf("<malloc.h>\n");
855 i=1;
856 #endif
857 #endif
858 #ifndef __STRICT_ANSI__
859 #ifndef MACH
860 printf("<memory.h\n");
861 i=1;
862 #endif
863 #endif
864 #ifndef sgi
865 printf("<sys/timeb.h>\n");
866 i=1;
867 #endif
868 if(!i)
869 printf("(none)\n");
870 printf("Datadir:\t\t%s\n",settings.datadir);
871 printf("Localdir:\t\t%s\n",settings.localdir);
872 #ifdef PERM_FILE
873 printf("Perm file:\t<ETC>/%s\n",PERM_FILE);
874 #endif
875 #ifdef SHUTDOWN_FILE
876 printf("Shutdown file:\t<ETC>/%s\n",SHUTDOWN_FILE);
877 #endif
878 printf("Save player:\t<true>\n");
879 printf("Save mode:\t%4.4o\n",SAVE_MODE);
880 printf("Playerdir:\t<VAR>/%s\n",settings.playerdir);
881 printf("Itemsdir:\t<VAR>/%s\n", settings.uniquedir);
882 printf("Tmpdir:\t\t%s\n",settings.tmpdir);
883 printf("Map max timeout:\t%d\n",MAP_MAXTIMEOUT);
884 printf("Max objects:\t%d\n",MAX_OBJECTS);
885 #ifdef USE_CALLOC
886 printf("Use_calloc:\t<true>\n");
887 #else
888 printf("Use_calloc:\t<false>\n");
889 #endif
890
891 #ifdef X_EDITOR
892 printf("Editor:\t\t%s\n",X_EDITOR);
893 #endif
894
895 printf("Max_time:\t%d\n",MAX_TIME);
896
897 #ifdef WIN32 /* ***win32 compile_info(): remove execl... */
898 printf("Logfilename:\t%s\n",settings.logfilename);
899 exit(0);
900 #else
901 execl("/bin/uname", "uname", "-a", NULL);
902 LOG(llevError, "Oops, shouldn't have gotten here: execl(/bin/uname) failed: %s\n", strerror_local(errno));
903 exit(-1);
904 #endif
905 }
906
907 /* Signal handlers: */
908
909 void rec_sigsegv(int i) {
910 LOG(llevError,"\nSIGSEGV received.\n");
911 fatal_signal(1, 1);
912 }
913
914 void rec_sigint(int i) {
915 LOG(llevInfo,"\nSIGINT received.\n");
916 fatal_signal(0, 1);
917 }
918
919 void rec_sighup(int i) {
920 LOG(llevInfo,"\nSIGHUP received\n");
921 if(init_done) {
922 emergency_save(0);
923 cleanup();
924 }
925 exit(0);
926 }
927
928 void rec_sigquit(int i) {
929 LOG(llevInfo,"\nSIGQUIT received\n");
930 fatal_signal(1, 1);
931 }
932
933 void rec_sigpipe(int i) {
934
935 /* Keep running if we receive a sigpipe. Crossfire should really be able
936 * to handle this signal (at least at some point in the future if not
937 * right now). By causing a dump right when it is received, it is not
938 * doing much good. However, if it core dumps later on, at least it can
939 * be looked at later on, and maybe fix the problem that caused it to
940 * dump core. There is no reason that SIGPIPES should be fatal
941 */
942 LOG(llevError,"\nSIGPIPE--------------\n------------\n--------\n---\n");
943 #if 1 && !defined(WIN32) /* ***win32: we don't want send SIGPIPE */
944 LOG(llevInfo,"\nReceived SIGPIPE, ignoring...\n");
945 signal(SIGPIPE,rec_sigpipe);/* hocky-pux clears signal handlers */
946 #else
947 LOG(llevError,"\nSIGPIPE received, not ignoring...\n");
948 fatal_signal(1, 1); /*Might consider to uncomment this line */
949 #endif
950 }
951
952 void rec_sigbus(int i) {
953 #ifdef SIGBUS
954 LOG(llevError,"\nSIGBUS received\n");
955 fatal_signal(1, 1);
956 #endif
957 }
958
959 void rec_sigterm(int i) {
960 LOG(llevInfo,"\nSIGTERM received\n");
961 fatal_signal(0, 1);
962 }
963
964 void fatal_signal(int make_core, int close_sockets) {
965 if(init_done) {
966 emergency_save(0);
967 clean_tmp_files();
968 }
969 if(make_core)
970 abort();
971 exit(0);
972 }
973
974 void init_signals(void) {
975 #ifndef WIN32 /* init_signals() remove signals */
976 signal(SIGHUP,rec_sighup);
977 signal(SIGINT,rec_sigint);
978 #ifndef DEBUG
979 signal(SIGQUIT,rec_sigquit);
980 signal(SIGSEGV,rec_sigsegv);
981 LOG(llevInfo,"\n---------registering SIGPIPE\n");
982 signal(SIGPIPE,rec_sigpipe);
983 #ifdef SIGBUS
984 signal(SIGBUS,rec_sigbus);
985 #endif
986 signal(SIGTERM,rec_sigterm);
987 #endif
988 #endif /* win32 */
989 }
990
991 /* init_races() - reads the races file in the lib/ directory, then
992 * overwrites old 'race' entries. This routine allow us to quickly
993 * re-configure the 'alignment' of monsters, objects. Useful for
994 * putting together lists of creatures, etc that belong to gods.
995 */
996
997 void init_races (void) {
998 FILE *file;
999 char race[MAX_BUF], fname[MAX_BUF], buf[MAX_BUF], *cp, variable[MAX_BUF];
1000 archetype *mon=NULL;
1001 static int init_done=0;
1002
1003 if (init_done) return;
1004 init_done=1;
1005 first_race=NULL;
1006
1007 sprintf(fname,"%s/races",settings.datadir);
1008 LOG(llevDebug, "Reading races from %s...",fname);
1009 if(! (file=fopen(fname,"r"))) {
1010 LOG(llevError, "Cannot open races file %s: %s\n", fname, strerror_local(errno));
1011 return;
1012 }
1013
1014 while(fgets(buf,MAX_BUF,file)!=NULL) {
1015 int set_race=1,set_list=1;
1016 if(*buf=='#') continue;
1017 if((cp=strchr(buf,'\n'))!=NULL)
1018 *cp='\0';
1019 cp=buf;
1020 while(*cp==' '||*cp=='!'||*cp=='@') {
1021 if(*cp=='!') set_race=0;
1022 if(*cp=='@') set_list=0;
1023 cp++;
1024 }
1025 if(sscanf(cp,"RACE %s",variable)) { /* set new race value */
1026 strcpy(race,variable);
1027 } else {
1028 char *cp1;
1029 /* Take out beginning spaces */
1030 for (cp1 = cp; *cp1==' '; cp1++);
1031 /* Remove newline and trailing spaces */
1032 for (cp1 = cp + strlen(cp) -1; *cp1 == '\n' || *cp1 == ' '; cp1 --) {
1033 *cp1='\0';
1034 if (cp==cp1) break;
1035 }
1036
1037 if (cp[strlen(cp)-1]=='\n') cp[strlen(cp)-1]='\0';
1038 /* set creature race to race value */
1039 if((mon=find_archetype(cp))==NULL)
1040 LOG(llevError,"\nCreature %s in race file lacks archetype",cp);
1041 else {
1042 if(set_race&&(!mon->clone.race||strcmp(mon->clone.race,race))) {
1043 if(mon->clone.race) {
1044 LOG(llevDebug,"\n Resetting race to %s from %s for archetype %s",
1045 race,mon->clone.race,mon->name);
1046 free_string(mon->clone.race);
1047 }
1048 mon->clone.race=add_string(race);
1049 }
1050 /* if the arch is a monster, add it to the race list */
1051 if(set_list&&QUERY_FLAG(&mon->clone,FLAG_MONSTER))
1052 add_to_racelist(race, &mon->clone);
1053 }
1054 }
1055 }
1056 fclose(file);
1057 LOG(llevDebug,"done.\n");
1058 }
1059
1060 void dump_races(void)
1061 {
1062 racelink *list;
1063 objectlink *tmp;
1064 for(list=first_race;list;list=list->next) {
1065 fprintf(stderr,"\nRACE %s:\t",list->name);
1066 for(tmp=list->member;tmp;tmp=tmp->next)
1067 fprintf(stderr,"%s(%d), ",tmp->ob->arch->name,tmp->ob->level);
1068 }
1069 fprintf(stderr,"\n");
1070 }
1071
1072 void add_to_racelist (const char *race_name, object *op) {
1073 racelink *race;
1074
1075 if(!op||!race_name) return;
1076 race=find_racelink(race_name);
1077
1078 if(!race) { /* add in a new race list */
1079 race = get_racelist();
1080 race->next = first_race;
1081 first_race = race;
1082 race->name=add_string(race_name);
1083 }
1084
1085 if(race->member->ob) {
1086 objectlink *tmp = get_objectlink();
1087 tmp->next=race->member;
1088 race->member = tmp;
1089 }
1090 race->nrof++;
1091 race->member->ob = op;
1092 }
1093
1094 racelink * get_racelist ( ) {
1095 racelink *list;
1096
1097 list = (racelink *) malloc(sizeof(racelink ));
1098 list->name=NULL;
1099 list->nrof=0;
1100 list->member=get_objectlink();
1101 list->next=NULL;
1102
1103 return list;
1104 }
1105
1106 racelink * find_racelink( const char *name ) {
1107 racelink *test=NULL;
1108
1109 if(name&&first_race)
1110 for(test=first_race;test&&test!=test->next;test=test->next)
1111 if(!test->name||!strcmp(name,test->name)) break;
1112
1113 return test;
1114 }