ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
(Generate patch)

Comparing deliantra/server/server/init.C (file contents):
Revision 1.9 by root, Sun Sep 10 15:59:57 2006 UTC vs.
Revision 1.66 by root, Tue Sep 23 04:29:11 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines