ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.81
Committed: Wed Apr 28 19:49:51 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.80: +1 -3 lines
Log Message:
less MAX_BUF, more object_thawer dir/file convenience

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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 Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 #include <global.h>
26 #include <material.h>
27 #include <sproto.h>
28
29 /* This loads the settings file. There could be debate whether this should
30 * be here or in the common directory - but since only the server needs this
31 * information, having it here probably makes more sense.
32 */
33 void
34 load_settings ()
35 {
36 object_thawer thawer (settings.confdir, "settings");
37
38 if (!thawer)
39 {
40 LOG (llevError, "Error: No settings file found\n");
41 exit (1);
42 }
43
44 while (thawer.kw)
45 {
46 const char *buf = thawer.kw_str;
47 const char *cp = thawer.value_nn;
48
49 if (!strcmp (buf, "not_permadeth"))
50 {
51 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
52 {
53 settings.not_permadeth = TRUE;
54 }
55 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
56 {
57 settings.not_permadeth = FALSE;
58 }
59 else
60 {
61 LOG (llevError, "load_settings: Unknown value for not_permadeth" ": %s\n", cp);
62 }
63 }
64 else if (!strcmp (buf, "resurrection"))
65 {
66 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
67 {
68 settings.resurrection = TRUE;
69 }
70 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
71 {
72 settings.resurrection = FALSE;
73 }
74 else
75 {
76 LOG (llevError, "load_settings: Unknown value for resurrection" ": %s\n", cp);
77 }
78 }
79 else if (!strcmp (buf, "set_title"))
80 {
81 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
82 {
83 settings.set_title = TRUE;
84 }
85 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
86 {
87 settings.set_title = FALSE;
88 }
89 else
90 {
91 LOG (llevError, "load_settings: Unknown value for set_title" ": %s\n", cp);
92 }
93 }
94 else if (!strcmp (buf, "search_items"))
95 {
96 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
97 {
98 settings.search_items = TRUE;
99 }
100 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
101 {
102 settings.search_items = FALSE;
103 }
104 else
105 {
106 LOG (llevError, "load_settings: Unknown value for search_items" ": %s\n", cp);
107 }
108 }
109 else if (!strcmp (buf, "spell_encumbrance"))
110 {
111 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
112 {
113 settings.spell_encumbrance = TRUE;
114 }
115 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
116 {
117 settings.spell_encumbrance = FALSE;
118 }
119 else
120 {
121 LOG (llevError, "load_settings: Unknown value for " "spell_encumbrance: %s\n", cp);
122 }
123 }
124 else if (!strcmp (buf, "spell_failure_effects"))
125 {
126 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
127 {
128 settings.spell_failure_effects = TRUE;
129 }
130 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
131 {
132 settings.spell_failure_effects = FALSE;
133 }
134 else
135 {
136 LOG (llevError, "load_settings: Unknown value for " "spell_failure_effects: %s\n", cp);
137 }
138 }
139 else if (!strcmp (buf, "spellpoint_level_depend"))
140 {
141 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
142 {
143 settings.spellpoint_level_depend = TRUE;
144 }
145 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
146 {
147 settings.spellpoint_level_depend = FALSE;
148 }
149 else
150 {
151 LOG (llevError, "load_settings: Unknown value for " "spellpoint_level_depend: %s\n", cp);
152 }
153 }
154 else if (!strcmp (buf, "stat_loss_on_death"))
155 {
156 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
157 {
158 settings.stat_loss_on_death = TRUE;
159 }
160 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
161 {
162 settings.stat_loss_on_death = FALSE;
163 }
164 else
165 {
166 LOG (llevError, "load_settings: Unknown value for " "stat_loss_on_death: %s\n", cp);
167 }
168 }
169 else if (!strcmp (buf, "use_permanent_experience"))
170 {
171 LOG (llevError, "use_permanent_experience is deprecated, use" "permenent_experience_percentage instead\n");
172 }
173 else if (!strcmp (buf, "permanent_experience_percentage"))
174 {
175 int val = atoi (cp);
176
177 if (val < 0 || val > 100)
178 LOG (llevError, "load_settings: permenent_experience_percentage" "must be between 0 and 100, %d is invalid\n", val);
179 else
180 settings.permanent_exp_ratio = val;
181 }
182 else if (!strcmp (buf, "death_penalty_percentage"))
183 {
184 int val = atoi (cp);
185
186 if (val < 0 || val > 100)
187 LOG (llevError, "load_settings: death_penalty_percentage" "must be between 0 and 100, %d is invalid\n", val);
188 else
189 settings.death_penalty_ratio = val;
190 }
191 else if (!strcmp (buf, "death_penalty_levels"))
192 {
193 int val = atoi (cp);
194
195 if (val < 0 || val > 255)
196 LOG (llevError, "load_settings: death_penalty_levels" "can not be negative, %d is invalid\n", val);
197 else
198 settings.death_penalty_level = val;
199 }
200 else if (!strcmp (buf, "balanced_stat_loss"))
201 {
202 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
203 {
204 settings.balanced_stat_loss = TRUE;
205 }
206 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
207 {
208 settings.balanced_stat_loss = FALSE;
209 }
210 else
211 {
212 LOG (llevError, "load_settings: Unknown value for " "balanced_stat_loss: %s\n", cp);
213 }
214 }
215 else if (!strcmp (buf, "simple_exp"))
216 {
217 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
218 {
219 settings.simple_exp = TRUE;
220 }
221 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
222 {
223 settings.simple_exp = FALSE;
224 }
225 else
226 {
227 LOG (llevError, "load_settings: Unknown value for simple_exp: %s\n", cp);
228 }
229 }
230 else if (!strcmp (buf, "item_power_factor"))
231 {
232 float tmp = atof (cp);
233
234 if (tmp < 0)
235 LOG (llevError, "load_settings: item_power_factor must be a positive number (%f < 0)\n", tmp);
236 else
237 settings.item_power_factor = tmp;
238 }
239 else if (!strcmp (buf, "pk_luck_penalty"))
240 {
241 sint16 val = atoi (cp);
242
243 if (val < -100 || val > 100)
244 LOG (llevError, "load_settings: pk_luck_penalty must be between -100 and 100" ", %d is invalid\n", val);
245 else
246 settings.pk_luck_penalty = val;
247 }
248 else if (!strcmp (buf, "set_friendly_fire"))
249 {
250 int val = atoi (cp);
251
252 if (val < 0 || val > 100)
253 LOG (llevError, "load_settings: set_friendly_fire must be between 0 an 100" ", %d is invalid\n", val);
254 else
255 settings.set_friendly_fire = val;
256 }
257 else if (!strcmp (buf, "armor_max_enchant"))
258 {
259 int max_e = atoi (cp);
260
261 if (max_e <= 0)
262 LOG (llevError, "load_settings: armor_max_enchant is %d\n", max_e);
263 else
264 settings.armor_max_enchant = max_e;
265 }
266 else if (!strcmp (buf, "armor_weight_reduction"))
267 {
268 int wr = atoi (cp);
269
270 if (wr < 0)
271 LOG (llevError, "load_settings: armor_weight_reduction is %d\n", wr);
272 else
273 settings.armor_weight_reduction = wr;
274 }
275 else if (!strcmp (buf, "armor_weight_linear"))
276 {
277 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
278 {
279 settings.armor_weight_linear = TRUE;
280 }
281 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
282 {
283 settings.armor_weight_linear = FALSE;
284 }
285 else
286 {
287 LOG (llevError, "load_settings: unknown value for armor_weight_linear: %s\n", cp);
288 }
289
290 }
291 else if (!strcmp (buf, "armor_speed_improvement"))
292 {
293 int wr = atoi (cp);
294
295 if (wr < 0)
296 LOG (llevError, "load_settings: armor_speed_improvement is %d\n", wr);
297 else
298 settings.armor_speed_improvement = wr;
299 }
300 else if (!strcmp (buf, "armor_speed_linear"))
301 {
302 if (!strcmp (cp, "on") || !strcmp (cp, "true"))
303 {
304 settings.armor_speed_linear = TRUE;
305 }
306 else if (!strcmp (cp, "off") || !strcmp (cp, "false"))
307 {
308 settings.armor_speed_linear = FALSE;
309 }
310 else
311 {
312 LOG (llevError, "load_settings: unknown value for armor_speed_linear: %s\n", cp);
313 }
314
315 }
316 else
317 thawer.parse_error ("settings file");
318
319 thawer.next ();
320 }
321 }
322
323 /*
324 * init() is called only once, when starting the program.
325 */
326 void
327 init (int argc, char **argv)
328 {
329 init_done = 0; /* Must be done before init_signal() */
330
331 init_environ ();
332 cfperl_init ();
333 init_done = 1;
334 }
335
336 void
337 usage ()
338 {
339 fprintf (stderr, "Usage: deliantra-server [-h] [-<flags>]...\n");
340 }
341
342 void
343 help ()
344 {
345
346 /* The information in usage is redundant with what is given below, so why call it? */
347
348 /* usage();*/
349 printf ("Flags:\n");
350 printf (" -csport <port> Specifies the port to use for the new client/server code.\n");
351 printf (" -d Turns on some debugging.\n");
352 printf (" +d Turns off debugging (useful if server compiled with debugging\n");
353 printf (" as default).\n");
354 printf (" -detach The server will go in the background, closing all\n");
355 printf (" connections to the tty.\n");
356 printf (" -h Display this information.\n");
357 printf (" -log <file> Specifies which file to send output to.\n");
358 printf (" Only has meaning if -detach is specified.\n");
359 printf (" -mon Turns on monster debugging.\n");
360 printf (" -o Prints out info on what was defined at compile time.\n");
361 printf (" -s Display the high-score list.\n");
362 printf (" -score <name or class> Displays all high scores with matching name/class.\n");
363 printf (" -v Print version and contributors.\n");
364 printf (" -data Sets the lib dir (archetypes, treasures, etc.)\n");
365 printf (" -local Read/write local data (hiscore, unique items, etc.)\n");
366 printf (" -maps Sets the directory for maps.\n");
367 printf (" -arch Sets the archetype file to use.\n");
368 printf (" -regions Sets the regions file to use.\n");
369 printf (" -playerdir Sets the directory for the player files.\n");
370 printf (" -templatedir Sets the directory for template generate maps.\n");
371 printf (" -treasures Sets the treasures file to use.\n");
372 printf (" -uniquedir Sets the unique items/maps directory.\n");
373 printf (" -tmpdir Sets the directory for temporary files (mostly maps.)\n");
374 printf (" -m Lists out suggested experience for all monsters.\n");
375 printf (" -m2 Dumps out abilities.\n");
376 printf (" -m3 Dumps out artifact information.\n");
377 printf (" -m4 Dumps out spell information.\n");
378 printf (" -m5 Dumps out skill information.\n");
379 printf (" -m6 Dumps out race information.\n");
380 printf (" -m7 Dumps out alchemy information.\n");
381 printf (" -m8 Dumps out gods information.\n");
382 printf (" -m9 Dumps out more alchemy information (formula checking).\n");
383 printf (" -mt <name> Dumps out list of treasures for a monster.\n");
384 exit (0);
385 }
386
387 /* Signal handlers: */
388
389 static void
390 rec_sigabrt (int i)
391 {
392 signal (SIGABRT, SIG_DFL);
393
394 LOG (llevError, "SIGABRT received.\n");
395 cleanup ("SIGABRT received", 1);
396 }
397
398 static void
399 rec_sigsegv (int i)
400 {
401 signal (SIGSEGV, SIG_DFL);
402
403 LOG (llevError, "SIGSEGV received.\n");
404 cleanup ("SIGSEGV received", 1);
405 }
406
407 static void
408 rec_sigquit (int i)
409 {
410 signal (SIGQUIT, SIG_IGN);
411
412 LOG (llevInfo, "SIGQUIT received\n");
413 cleanup ("SIGQUIT received", 1);
414 }
415
416 static void
417 rec_sigbus (int i)
418 {
419 signal (SIGBUS, SIG_DFL);
420
421 LOG (llevError, "SIGBUS received\n");
422 cleanup ("SIGBUS received", 1);
423 }
424
425 void
426 reset_signals ()
427 {
428 signal (SIGABRT, SIG_DFL);
429 signal (SIGQUIT, SIG_DFL);
430 signal (SIGSEGV, SIG_DFL);
431 signal (SIGBUS , SIG_DFL);
432 signal (SIGINT , SIG_DFL);
433 signal (SIGTERM, SIG_DFL);
434 }
435
436 void
437 init_signals ()
438 {
439 // large stack, but it's important data we want to save, and it is not usually
440 // being physically allocated anyways
441 const size_t stacksize = 8 * 1024 * 1024 + SIGSTKSZ;
442
443 stack_t ss;
444 ss.ss_sp = malloc (stacksize);
445 ss.ss_flags = 0;
446 ss.ss_size = stacksize;
447 sigaltstack (&ss, 0);
448
449 struct sigaction sa;
450
451 sigfillset (&sa.sa_mask);
452 sa.sa_flags = SA_RESTART;
453
454 sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0);
455 sa.sa_handler = rec_sigabrt; sigaction (SIGABRT, &sa, 0);
456 sa.sa_handler = rec_sigquit; sigaction (SIGQUIT, &sa, 0);
457 sa.sa_handler = rec_sigbus; sigaction (SIGBUS, &sa, 0);
458
459 sa.sa_flags |= SA_ONSTACK;
460 sa.sa_handler = rec_sigsegv; sigaction (SIGSEGV, &sa, 0);
461 }
462
463 static racelink *
464 get_racelist ()
465 {
466 racelink *list = new racelink;
467
468 list->name = 0;
469 list->nrof = 0;
470 list->next = 0;
471 list->member = get_objectlink ();
472
473 return list;
474 }
475
476 racelink *
477 find_racelink (const char *name)
478 {
479 if (name)
480 for (racelink *link = first_race; link; link = link->next)
481 if (!link->name || !strcmp (name, link->name))
482 return link;
483
484 return 0;
485 }
486
487 static void
488 add_to_racelist (const char *race_name, object *op)
489 {
490 racelink *race;
491
492 if (!op || !race_name)
493 return;
494
495 race = find_racelink (race_name);
496
497 if (!race)
498 { /* add in a new race list */
499 race = get_racelist ();
500 race->next = first_race;
501 first_race = race;
502 race->name = race_name;
503 }
504
505 if (race->member->ob)
506 {
507 objectlink *tmp = get_objectlink ();
508
509 tmp->next = race->member;
510 race->member = tmp;
511 }
512
513 race->nrof++;
514 race->member->ob = op;
515 }
516
517 /* init_races() - reads the races file in the lib/ directory, then
518 * overwrites old 'race' entries. This routine allow us to quickly
519 * re-configure the 'alignment' of monsters, objects. Useful for
520 * putting together lists of creatures, etc that belong to gods.
521 */
522 static void
523 init_races ()
524 {
525 FILE *file;
526 char race[MAX_BUF], fname[MAX_BUF], buf[MAX_BUF], *cp, variable[MAX_BUF];
527 archetype *mon = NULL;
528 static int init_done = 0;
529
530 if (init_done)
531 return;
532 init_done = 1;
533 first_race = 0;
534
535 sprintf (fname, "%s/races", settings.datadir);
536 LOG (llevDebug, "Reading races from %s...\n", fname);
537 if (!(file = fopen (fname, "r")))
538 {
539 LOG (llevError, "Cannot open races file %s: %s\n", fname, strerror (errno));
540 return;
541 }
542
543 while (fgets (buf, MAX_BUF, file) != NULL)
544 {
545 int set_race = 1, set_list = 1;
546
547 if (*buf == '#')
548 continue;
549
550 if ((cp = strchr (buf, '\n')) != NULL)
551 *cp = '\0';
552
553 cp = buf;
554 while (*cp == ' ' || *cp == '!' || *cp == '@')
555 {
556 if (*cp == '!') set_race = 0;
557 if (*cp == '@') set_list = 0;
558
559 cp++;
560 }
561
562 if (sscanf (cp, "RACE %s", variable))
563 /* set new race value */
564 strcpy (race, variable);
565 else
566 {
567 char *cp1;
568
569 /* Take out beginning spaces */
570 for (cp1 = cp; *cp1 == ' '; cp1++)
571 ;
572 /* Remove newline and trailing spaces */
573 for (cp1 = cp + strlen (cp) - 1; *cp1 == '\n' || *cp1 == ' '; cp1--)
574 {
575 *cp1 = '\0';
576 if (cp == cp1)
577 break;
578 }
579
580 if (cp[strlen (cp) - 1] == '\n')
581 cp[strlen (cp) - 1] = '\0';
582
583 /* set creature race to race value */
584 if ((mon = archetype::find (cp)) == NULL)
585 LOG (llevError, "Creature %s in race file lacks archetype\n", cp);
586 else
587 {
588 if (set_race && (!mon->race || strcmp (&mon->race, race)))
589 {
590 if (mon->race)
591 LOG (llevDebug, "Resetting race to %s from %s for archetype %s\n", race, &mon->race, &mon->archname);
592
593 mon->race = race;
594 }
595
596 /* if the arch is a monster, add it to the race list */
597 if (set_list && mon->flag [FLAG_MONSTER])
598 add_to_racelist (race, mon);
599 }
600 }
601 }
602
603 fclose (file);
604 LOG (llevDebug, "done.\n");
605 }
606
607 void
608 init_beforeplay ()
609 {
610 init_artifacts (); /* If not called before, reads all artifacts from file */
611 init_races (); /* overwrite race designations using entries in lib/races file */
612 init_gods (); /* init linked list of gods from archs */
613 init_readable (); /* inits useful arrays for readable texts */
614 init_formulae (); /* If not called before, reads formulae from file */
615 }
616