ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.83
Committed: Sat Apr 23 04:56:56 2011 UTC (13 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.82: +1 -1 lines
Log Message:
update copyright to 2011

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 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 /* Signal handlers: */
337
338 static void
339 rec_sigabrt (int i)
340 {
341 signal (SIGABRT, SIG_DFL);
342
343 LOG (llevError, "SIGABRT received.\n");
344 cleanup ("SIGABRT received", 1);
345 }
346
347 static void
348 rec_sigsegv (int i)
349 {
350 signal (SIGSEGV, SIG_DFL);
351
352 LOG (llevError, "SIGSEGV received.\n");
353 cleanup ("SIGSEGV received", 1);
354 }
355
356 static void
357 rec_sigquit (int i)
358 {
359 signal (SIGQUIT, SIG_IGN);
360
361 LOG (llevInfo, "SIGQUIT received\n");
362 cleanup ("SIGQUIT received", 1);
363 }
364
365 static void
366 rec_sigbus (int i)
367 {
368 signal (SIGBUS, SIG_DFL);
369
370 LOG (llevError, "SIGBUS received\n");
371 cleanup ("SIGBUS received", 1);
372 }
373
374 void
375 reset_signals ()
376 {
377 signal (SIGABRT, SIG_DFL);
378 signal (SIGQUIT, SIG_DFL);
379 signal (SIGSEGV, SIG_DFL);
380 signal (SIGBUS , SIG_DFL);
381 signal (SIGINT , SIG_DFL);
382 signal (SIGTERM, SIG_DFL);
383 }
384
385 void
386 init_signals ()
387 {
388 // large stack, but it's important data we want to save, and it is not usually
389 // being physically allocated anyways
390 const size_t stacksize = 8 * 1024 * 1024 + SIGSTKSZ;
391
392 stack_t ss;
393 ss.ss_sp = malloc (stacksize);
394 ss.ss_flags = 0;
395 ss.ss_size = stacksize;
396 sigaltstack (&ss, 0);
397
398 struct sigaction sa;
399
400 sigfillset (&sa.sa_mask);
401 sa.sa_flags = SA_RESTART;
402
403 sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0);
404 sa.sa_handler = rec_sigabrt; sigaction (SIGABRT, &sa, 0);
405 sa.sa_handler = rec_sigquit; sigaction (SIGQUIT, &sa, 0);
406 sa.sa_handler = rec_sigbus; sigaction (SIGBUS, &sa, 0);
407
408 sa.sa_flags |= SA_ONSTACK;
409 sa.sa_handler = rec_sigsegv; sigaction (SIGSEGV, &sa, 0);
410 }
411
412 static racelink *
413 get_racelist ()
414 {
415 racelink *list = new racelink;
416
417 list->name = 0;
418 list->nrof = 0;
419 list->next = 0;
420 list->member = get_objectlink ();
421
422 return list;
423 }
424
425 racelink *
426 find_racelink (const char *name)
427 {
428 if (name)
429 for (racelink *link = first_race; link; link = link->next)
430 if (!link->name || !strcmp (name, link->name))
431 return link;
432
433 return 0;
434 }
435
436 static void
437 add_to_racelist (const char *race_name, object *op)
438 {
439 racelink *race;
440
441 if (!op || !race_name)
442 return;
443
444 race = find_racelink (race_name);
445
446 if (!race)
447 { /* add in a new race list */
448 race = get_racelist ();
449 race->next = first_race;
450 first_race = race;
451 race->name = race_name;
452 }
453
454 if (race->member->ob)
455 {
456 objectlink *tmp = get_objectlink ();
457
458 tmp->next = race->member;
459 race->member = tmp;
460 }
461
462 race->nrof++;
463 race->member->ob = op;
464 }
465
466 /* init_races() - reads the races file in the lib/ directory, then
467 * overwrites old 'race' entries. This routine allow us to quickly
468 * re-configure the 'alignment' of monsters, objects. Useful for
469 * putting together lists of creatures, etc that belong to gods.
470 */
471 static void
472 init_races ()
473 {
474 FILE *file;
475 char race[MAX_BUF], fname[MAX_BUF], buf[MAX_BUF], *cp, variable[MAX_BUF];
476 archetype *mon = NULL;
477 static int init_done = 0;
478
479 if (init_done)
480 return;
481 init_done = 1;
482 first_race = 0;
483
484 sprintf (fname, "%s/races", settings.datadir);
485 LOG (llevDebug, "Reading races from %s...\n", fname);
486 if (!(file = fopen (fname, "r")))
487 {
488 LOG (llevError, "Cannot open races file %s: %s\n", fname, strerror (errno));
489 return;
490 }
491
492 while (fgets (buf, MAX_BUF, file) != NULL)
493 {
494 int set_race = 1, set_list = 1;
495
496 if (*buf == '#')
497 continue;
498
499 if ((cp = strchr (buf, '\n')) != NULL)
500 *cp = '\0';
501
502 cp = buf;
503 while (*cp == ' ' || *cp == '!' || *cp == '@')
504 {
505 if (*cp == '!') set_race = 0;
506 if (*cp == '@') set_list = 0;
507
508 cp++;
509 }
510
511 if (sscanf (cp, "RACE %s", variable))
512 /* set new race value */
513 strcpy (race, variable);
514 else
515 {
516 char *cp1;
517
518 /* Take out beginning spaces */
519 for (cp1 = cp; *cp1 == ' '; cp1++)
520 ;
521 /* Remove newline and trailing spaces */
522 for (cp1 = cp + strlen (cp) - 1; *cp1 == '\n' || *cp1 == ' '; cp1--)
523 {
524 *cp1 = '\0';
525 if (cp == cp1)
526 break;
527 }
528
529 if (cp[strlen (cp) - 1] == '\n')
530 cp[strlen (cp) - 1] = '\0';
531
532 /* set creature race to race value */
533 if ((mon = archetype::find (cp)) == NULL)
534 LOG (llevError, "Creature %s in race file lacks archetype\n", cp);
535 else
536 {
537 if (set_race && (!mon->race || strcmp (&mon->race, race)))
538 {
539 if (mon->race)
540 LOG (llevDebug, "Resetting race to %s from %s for archetype %s\n", race, &mon->race, &mon->archname);
541
542 mon->race = race;
543 }
544
545 /* if the arch is a monster, add it to the race list */
546 if (set_list && mon->flag [FLAG_MONSTER])
547 add_to_racelist (race, mon);
548 }
549 }
550 }
551
552 fclose (file);
553 LOG (llevDebug, "done.\n");
554 }
555
556 void
557 init_beforeplay ()
558 {
559 init_artifacts (); /* If not called before, reads all artifacts from file */
560 init_races (); /* overwrite race designations using entries in lib/races file */
561 init_gods (); /* init linked list of gods from archs */
562 init_readable (); /* inits useful arrays for readable texts */
563 init_formulae (); /* If not called before, reads formulae from file */
564 }
565