ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.71
Committed: Mon Oct 12 14:00:59 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_81
Changes since 1.70: +7 -6 lines
Log Message:
clarify license

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
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 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 <loader.h>
28 #include <sproto.h>
29
30 //TODO: make this a constructor
31 static materialtype_t *
32 get_empty_mat (void)
33 {
34 materialtype_t *mt;
35 int i;
36
37 mt = new materialtype_t;
38
39 mt->name = shstr_unknown;
40 mt->description = 0;
41
42 for (i = 0; i < NROFATTACKS; i++)
43 {
44 mt->save[i] = 0;
45 mt->mod[i] = 0;
46 }
47
48 mt->chance = 0;
49 mt->difficulty = 0;
50 mt->magic = 0;
51 mt->damage = 0;
52 mt->wc = 0;
53 mt->ac = 0;
54 mt->sp = 0;
55 mt->weight = 100;
56 mt->value = 100;
57 mt->density = 1;
58 mt->next = 0;
59
60 return mt;
61 }
62
63 void
64 load_materials (void)
65 {
66 char filename[MAX_BUF];
67
68 sprintf (filename, "%s/materials", settings.datadir);
69 LOG (llevDebug, "Reading material type data from %s...\n", filename);
70
71 //TODO: somehow free old materials, or update them in-place
72 materialt = 0;
73
74 object_thawer thawer (filename);
75
76 if (!thawer)
77 {
78 LOG (llevError, "Cannot open %s for reading\n", filename);
79 goto done;
80 }
81
82 while (thawer.kw != KW_name)
83 {
84 thawer.next ();
85
86 if (thawer.kw == KW_EOF)
87 goto done;
88 }
89
90 materialtype_t *mt;
91
92 for (;;)
93 {
94 switch (thawer.kw)
95 {
96 case KW_name:
97 mt = get_empty_mat ();
98 mt->next = materialt;
99 materialt = mt;
100
101 thawer.get (mt->name);
102 mt->description = mt->name;
103 break;
104
105 case KW_description:
106 thawer.get (mt->description);
107 break;
108
109 case KW_material:
110 thawer.get (mt->material);
111 break;
112
113 case KW_saves:
114 {
115 const char *cp = thawer.get_str () - 1;
116
117 for (int i = 0; i < NROFATTACKS; i++)
118 {
119 if (!cp)
120 {
121 mt->save[i] = 0;
122 continue;
123 }
124
125 int value;
126 ++cp;
127 sscanf (cp, "%d", &value);
128 mt->save[i] = (sint8) value;
129 cp = strchr (cp, ',');
130 }
131 }
132 break;
133
134 case KW_mods:
135 {
136 const char *cp = thawer.get_str () - 1;
137
138 for (int i = 0; i < NROFATTACKS; i++)
139 {
140 if (!cp)
141 {
142 mt->save[i] = 0;
143 continue;
144 }
145
146 ++cp;
147 int value;
148 sscanf (cp, "%d", &value);
149 mt->mod[i] = (sint8) value;
150 cp = strchr (cp, ',');
151 }
152 }
153 break;
154
155 case KW_chance: thawer.get (mt->chance); break;
156 case KW_difficulty: // cf+ alias, not original cf
157 case KW_diff: thawer.get (mt->difficulty); break;
158 case KW_magic: thawer.get (mt->magic); break;
159 case KW_dam: // cf+ alias, not original cf
160 case KW_damage: thawer.get (mt->damage); break;
161 case KW_wc: thawer.get (mt->wc); break;
162 case KW_ac: thawer.get (mt->ac); break;
163 case KW_sp: thawer.get (mt->sp); break;
164 case KW_weight: thawer.get (mt->weight); break;
165 case KW_value: thawer.get (mt->value); break;
166 case KW_density: thawer.get (mt->density); break;
167
168 case KW_EOF:
169 goto done;
170
171 default:
172 if (!thawer.parse_error ("materials file", "materials"))
173 goto done;
174 break;
175 }
176
177 thawer.next ();
178 }
179
180 done:
181 if (!materialt)
182 materialt = get_empty_mat ();
183
184 LOG (llevDebug, "Done.\n");
185 }
186
187 /* This loads the settings file. There could be debate whether this should
188 * be here or in the common directory - but since only the server needs this
189 * information, having it here probably makes more sense.
190 */
191 void
192 load_settings (void)
193 {
194 char buf[MAX_BUF], *cp;
195 int has_val, comp;
196 FILE *fp;
197
198 sprintf (buf, "%s/settings", settings.confdir);
199
200 /* We don't require a settings file at current time, but down the road,
201 * there will probably be so many values that not having a settings file
202 * will not be a good thing.
203 */
204 if (!(fp = open_and_uncompress (buf, 0, &comp)))
205 {
206 LOG (llevError, "Error: No settings file found\n");
207 exit (1);
208 }
209
210 while (fgets (buf, MAX_BUF - 1, fp) != NULL)
211 {
212 if (buf[0] == '#')
213 continue;
214 /* eliminate newline */
215 if ((cp = strrchr (buf, '\n')) != NULL)
216 *cp = '\0';
217
218 /* Skip over empty lines */
219 if (buf[0] == 0)
220 continue;
221
222 /* Skip all the spaces and set them to nulls. If not space,
223 * set cp to "" to make strcpy's and the like easier down below.
224 */
225 if ((cp = strchr (buf, ' ')) != NULL)
226 {
227 while (*cp == ' ')
228 *cp++ = 0;
229 has_val = 1;
230 }
231 else
232 {
233 cp = (char *)"";
234 has_val = 0;
235 }
236
237 if (!strcasecmp (buf, "motd"))
238 {
239 if (has_val)
240 strcpy (settings.motd, cp);
241 else
242 LOG (llevError, "load_settings: motd must have a value.\n");
243 }
244 else if (!strcasecmp (buf, "dm_mail"))
245 {
246 if (has_val)
247 strcpy (settings.dm_mail, cp);
248 else
249 LOG (llevError, "load_settings: dm_mail must have a value.\n");
250 }
251 else if (!strcasecmp (buf, "worldmapstartx"))
252 {
253 int size = atoi (cp);
254
255 if (size < 0)
256 LOG (llevError, "load_settings: worldmapstartx must be at least " "0, %d is invalid\n", size);
257 else
258 settings.worldmapstartx = size;
259 }
260 else if (!strcasecmp (buf, "worldmapstarty"))
261 {
262 int size = atoi (cp);
263
264 if (size < 0)
265 LOG (llevError, "load_settings: worldmapstarty must be at least " "0, %d is invalid\n", size);
266 else
267 settings.worldmapstarty = size;
268 }
269 else if (!strcasecmp (buf, "worldmaptilesx"))
270 {
271 int size = atoi (cp);
272
273 if (size < 1)
274 LOG (llevError, "load_settings: worldmaptilesx must be greater " "than 1, %d is invalid\n", size);
275 else
276 settings.worldmaptilesx = size;
277 }
278 else if (!strcasecmp (buf, "worldmaptilesy"))
279 {
280 int size = atoi (cp);
281
282 if (size < 1)
283 LOG (llevError, "load_settings: worldmaptilesy must be greater " "than 1, %d is invalid\n", size);
284 else
285 settings.worldmaptilesy = size;
286 }
287 else if (!strcasecmp (buf, "worldmaptilesizex"))
288 {
289 int size = atoi (cp);
290
291 if (size < 1)
292 LOG (llevError, "load_settings: worldmaptilesizex must be " "greater than 1, %d is invalid\n", size);
293 else
294 settings.worldmaptilesizex = size;
295 }
296 else if (!strcasecmp (buf, "worldmaptilesizey"))
297 {
298 int size = atoi (cp);
299
300 if (size < 1)
301 LOG (llevError, "load_settings: worldmaptilesizey must be " "greater than 1, %d is invalid\n", size);
302 else
303 settings.worldmaptilesizey = size;
304 }
305 else if (!strcasecmp (buf, "dynamiclevel"))
306 {
307 int lev = atoi (cp);
308
309 if (lev < 0)
310 LOG (llevError, "load_settings: dynamiclevel must be " "at least 0, %d is invalid\n", lev);
311 else
312 settings.dynamiclevel = lev;
313 }
314 else if (!strcasecmp (buf, "fastclock"))
315 {
316 int lev = atoi (cp);
317
318 if (lev < 0)
319 LOG (llevError, "load_settings: fastclock must be at least 0" ", %d is invalid\n", lev);
320 else
321 settings.fastclock = lev;
322 }
323 else if (!strcasecmp (buf, "not_permadeth"))
324 {
325 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
326 {
327 settings.not_permadeth = TRUE;
328 }
329 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
330 {
331 settings.not_permadeth = FALSE;
332 }
333 else
334 {
335 LOG (llevError, "load_settings: Unknown value for not_permadeth" ": %s\n", cp);
336 }
337 }
338 else if (!strcasecmp (buf, "resurrection"))
339 {
340 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
341 {
342 settings.resurrection = TRUE;
343 }
344 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
345 {
346 settings.resurrection = FALSE;
347 }
348 else
349 {
350 LOG (llevError, "load_settings: Unknown value for resurrection" ": %s\n", cp);
351 }
352 }
353 else if (!strcasecmp (buf, "set_title"))
354 {
355 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
356 {
357 settings.set_title = TRUE;
358 }
359 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
360 {
361 settings.set_title = FALSE;
362 }
363 else
364 {
365 LOG (llevError, "load_settings: Unknown value for set_title" ": %s\n", cp);
366 }
367 }
368 else if (!strcasecmp (buf, "search_items"))
369 {
370 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
371 {
372 settings.search_items = TRUE;
373 }
374 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
375 {
376 settings.search_items = FALSE;
377 }
378 else
379 {
380 LOG (llevError, "load_settings: Unknown value for search_items" ": %s\n", cp);
381 }
382 }
383 else if (!strcasecmp (buf, "spell_encumbrance"))
384 {
385 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
386 {
387 settings.spell_encumbrance = TRUE;
388 }
389 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
390 {
391 settings.spell_encumbrance = FALSE;
392 }
393 else
394 {
395 LOG (llevError, "load_settings: Unknown value for " "spell_encumbrance: %s\n", cp);
396 }
397 }
398 else if (!strcasecmp (buf, "spell_failure_effects"))
399 {
400 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
401 {
402 settings.spell_failure_effects = TRUE;
403 }
404 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
405 {
406 settings.spell_failure_effects = FALSE;
407 }
408 else
409 {
410 LOG (llevError, "load_settings: Unknown value for " "spell_failure_effects: %s\n", cp);
411 }
412 }
413 else if (!strcasecmp (buf, "spellpoint_level_depend"))
414 {
415 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
416 {
417 settings.spellpoint_level_depend = TRUE;
418 }
419 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
420 {
421 settings.spellpoint_level_depend = FALSE;
422 }
423 else
424 {
425 LOG (llevError, "load_settings: Unknown value for " "spellpoint_level_depend: %s\n", cp);
426 }
427 }
428 else if (!strcasecmp (buf, "stat_loss_on_death"))
429 {
430 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
431 {
432 settings.stat_loss_on_death = TRUE;
433 }
434 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
435 {
436 settings.stat_loss_on_death = FALSE;
437 }
438 else
439 {
440 LOG (llevError, "load_settings: Unknown value for " "stat_loss_on_death: %s\n", cp);
441 }
442 }
443 else if (!strcasecmp (buf, "use_permanent_experience"))
444 {
445 LOG (llevError, "use_permanent_experience is deprecated, use" "permenent_experience_percentage instead\n");
446 }
447 else if (!strcasecmp (buf, "permanent_experience_percentage"))
448 {
449 int val = atoi (cp);
450
451 if (val < 0 || val > 100)
452 LOG (llevError, "load_settings: permenent_experience_percentage" "must be between 0 and 100, %d is invalid\n", val);
453 else
454 settings.permanent_exp_ratio = val;
455 }
456 else if (!strcasecmp (buf, "death_penalty_percentage"))
457 {
458 int val = atoi (cp);
459
460 if (val < 0 || val > 100)
461 LOG (llevError, "load_settings: death_penalty_percentage" "must be between 0 and 100, %d is invalid\n", val);
462 else
463 settings.death_penalty_ratio = val;
464 }
465 else if (!strcasecmp (buf, "death_penalty_levels"))
466 {
467 int val = atoi (cp);
468
469 if (val < 0 || val > 255)
470 LOG (llevError, "load_settings: death_penalty_levels" "can not be negative, %d is invalid\n", val);
471 else
472 settings.death_penalty_level = val;
473 }
474 else if (!strcasecmp (buf, "balanced_stat_loss"))
475 {
476 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
477 {
478 settings.balanced_stat_loss = TRUE;
479 }
480 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
481 {
482 settings.balanced_stat_loss = FALSE;
483 }
484 else
485 {
486 LOG (llevError, "load_settings: Unknown value for " "balanced_stat_loss: %s\n", cp);
487 }
488 }
489 else if (!strcasecmp (buf, "simple_exp"))
490 {
491 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
492 {
493 settings.simple_exp = TRUE;
494 }
495 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
496 {
497 settings.simple_exp = FALSE;
498 }
499 else
500 {
501 LOG (llevError, "load_settings: Unknown value for simple_exp: %s\n", cp);
502 }
503 }
504 else if (!strcasecmp (buf, "item_power_factor"))
505 {
506 float tmp = atof (cp);
507
508 if (tmp < 0)
509 LOG (llevError, "load_settings: item_power_factor must be a positive number (%f < 0)\n", tmp);
510 else
511 settings.item_power_factor = tmp;
512 }
513 else if (!strcasecmp (buf, "pk_luck_penalty"))
514 {
515 sint16 val = atoi (cp);
516
517 if (val < -100 || val > 100)
518 LOG (llevError, "load_settings: pk_luck_penalty must be between -100 and 100" ", %d is invalid\n", val);
519 else
520 settings.pk_luck_penalty = val;
521 }
522 else if (!strcasecmp (buf, "set_friendly_fire"))
523 {
524 int val = atoi (cp);
525
526 if (val < 0 || val > 100)
527 LOG (llevError, "load_settings: set_friendly_fire must be between 0 an 100" ", %d is invalid\n", val);
528 else
529 settings.set_friendly_fire = val;
530 }
531 else if (!strcasecmp (buf, "armor_max_enchant"))
532 {
533 int max_e = atoi (cp);
534
535 if (max_e <= 0)
536 LOG (llevError, "load_settings: armor_max_enchant is %d\n", max_e);
537 else
538 settings.armor_max_enchant = max_e;
539 }
540 else if (!strcasecmp (buf, "armor_weight_reduction"))
541 {
542 int wr = atoi (cp);
543
544 if (wr < 0)
545 LOG (llevError, "load_settings: armor_weight_reduction is %d\n", wr);
546 else
547 settings.armor_weight_reduction = wr;
548 }
549 else if (!strcasecmp (buf, "armor_weight_linear"))
550 {
551 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
552 {
553 settings.armor_weight_linear = TRUE;
554 }
555 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
556 {
557 settings.armor_weight_linear = FALSE;
558 }
559 else
560 {
561 LOG (llevError, "load_settings: unknown value for armor_weight_linear: %s\n", cp);
562 }
563
564 }
565 else if (!strcasecmp (buf, "armor_speed_improvement"))
566 {
567 int wr = atoi (cp);
568
569 if (wr < 0)
570 LOG (llevError, "load_settings: armor_speed_improvement is %d\n", wr);
571 else
572 settings.armor_speed_improvement = wr;
573 }
574 else if (!strcasecmp (buf, "armor_speed_linear"))
575 {
576 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
577 {
578 settings.armor_speed_linear = TRUE;
579 }
580 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
581 {
582 settings.armor_speed_linear = FALSE;
583 }
584 else
585 {
586 LOG (llevError, "load_settings: unknown value for armor_speed_linear: %s\n", cp);
587 }
588
589 }
590 else
591 LOG (llevError, "Unknown value in settings file: %s\n", buf);
592 }
593
594 close_and_delete (fp, comp);
595 }
596
597 /*
598 * init() is called only once, when starting the program.
599 */
600 void
601 init (int argc, char **argv)
602 {
603 init_done = 0; /* Must be done before init_signal() */
604
605 init_environ ();
606 cfperl_init ();
607 init_done = 1;
608 }
609
610 void
611 usage (void)
612 {
613 fprintf (stderr, "Usage: deliantra-server [-h] [-<flags>]...\n");
614 }
615
616 void
617 help (void)
618 {
619
620 /* The information in usage is redundant with what is given below, so why call it? */
621
622 /* usage();*/
623 printf ("Flags:\n");
624 printf (" -csport <port> Specifies the port to use for the new client/server code.\n");
625 printf (" -d Turns on some debugging.\n");
626 printf (" +d Turns off debugging (useful if server compiled with debugging\n");
627 printf (" as default).\n");
628 printf (" -detach The server will go in the background, closing all\n");
629 printf (" connections to the tty.\n");
630 printf (" -h Display this information.\n");
631 printf (" -log <file> Specifies which file to send output to.\n");
632 printf (" Only has meaning if -detach is specified.\n");
633 printf (" -mon Turns on monster debugging.\n");
634 printf (" -o Prints out info on what was defined at compile time.\n");
635 printf (" -s Display the high-score list.\n");
636 printf (" -score <name or class> Displays all high scores with matching name/class.\n");
637 printf (" -v Print version and contributors.\n");
638 printf (" -data Sets the lib dir (archetypes, treasures, etc.)\n");
639 printf (" -local Read/write local data (hiscore, unique items, etc.)\n");
640 printf (" -maps Sets the directory for maps.\n");
641 printf (" -arch Sets the archetype file to use.\n");
642 printf (" -regions Sets the regions file to use.\n");
643 printf (" -playerdir Sets the directory for the player files.\n");
644 printf (" -templatedir Sets the directory for template generate maps.\n");
645 printf (" -treasures Sets the treasures file to use.\n");
646 printf (" -uniquedir Sets the unique items/maps directory.\n");
647 printf (" -tmpdir Sets the directory for temporary files (mostly maps.)\n");
648 printf (" -m Lists out suggested experience for all monsters.\n");
649 printf (" -m2 Dumps out abilities.\n");
650 printf (" -m3 Dumps out artifact information.\n");
651 printf (" -m4 Dumps out spell information.\n");
652 printf (" -m5 Dumps out skill information.\n");
653 printf (" -m6 Dumps out race information.\n");
654 printf (" -m7 Dumps out alchemy information.\n");
655 printf (" -m8 Dumps out gods information.\n");
656 printf (" -m9 Dumps out more alchemy information (formula checking).\n");
657 printf (" -mt <name> Dumps out list of treasures for a monster.\n");
658 exit (0);
659 }
660
661 void
662 init_beforeplay (void)
663 {
664 init_artifacts (); /* If not called before, reads all artifacts from file */
665 init_races (); /* overwrite race designations using entries in lib/races file */
666 init_gods (); /* init linked list of gods from archs */
667 init_readable (); /* inits useful arrays for readable texts */
668 init_formulae (); /* If not called before, reads formulae from file */
669 }
670
671 /* Signal handlers: */
672
673 static void
674 rec_sigabrt (int i)
675 {
676 signal (SIGABRT, SIG_DFL);
677
678 LOG (llevError, "SIGABRT received.\n");
679 cleanup ("SIGABRT received", 1);
680 }
681
682 static void
683 rec_sigsegv (int i)
684 {
685 signal (SIGSEGV, SIG_DFL);
686
687 LOG (llevError, "SIGSEGV received.\n");
688 cleanup ("SIGSEGV received", 1);
689 }
690
691 static void
692 rec_sigquit (int i)
693 {
694 signal (SIGQUIT, SIG_IGN);
695
696 LOG (llevInfo, "SIGQUIT received\n");
697 cleanup ("SIGQUIT received", 1);
698 }
699
700 static void
701 rec_sigbus (int i)
702 {
703 signal (SIGBUS, SIG_DFL);
704
705 LOG (llevError, "SIGBUS received\n");
706 cleanup ("SIGBUS received", 1);
707 }
708
709 void
710 reset_signals ()
711 {
712 signal (SIGABRT, SIG_DFL);
713 signal (SIGQUIT, SIG_DFL);
714 signal (SIGSEGV, SIG_DFL);
715 signal (SIGBUS , SIG_DFL);
716 signal (SIGINT , SIG_DFL);
717 signal (SIGTERM, SIG_DFL);
718 }
719
720 void
721 init_signals (void)
722 {
723 // large stack, but it's important data we want to save, and it is not usually
724 // being physically allocated anyways
725 const size_t stacksize = 8 * 1024 * 1024 + SIGSTKSZ;
726
727 stack_t ss;
728 ss.ss_sp = malloc (stacksize);
729 ss.ss_flags = 0;
730 ss.ss_size = stacksize;
731 sigaltstack (&ss, 0);
732
733 struct sigaction sa;
734
735 sigfillset (&sa.sa_mask);
736 sa.sa_flags = SA_RESTART;
737
738 sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0);
739 sa.sa_handler = rec_sigabrt; sigaction (SIGABRT, &sa, 0);
740 sa.sa_handler = rec_sigquit; sigaction (SIGQUIT, &sa, 0);
741 sa.sa_handler = rec_sigbus; sigaction (SIGBUS, &sa, 0);
742
743 sa.sa_flags |= SA_ONSTACK;
744 sa.sa_handler = rec_sigsegv; sigaction (SIGSEGV, &sa, 0);
745 }
746
747 /* init_races() - reads the races file in the lib/ directory, then
748 * overwrites old 'race' entries. This routine allow us to quickly
749 * re-configure the 'alignment' of monsters, objects. Useful for
750 * putting together lists of creatures, etc that belong to gods.
751 */
752 void
753 init_races (void)
754 {
755 FILE *file;
756 char race[MAX_BUF], fname[MAX_BUF], buf[MAX_BUF], *cp, variable[MAX_BUF];
757 archetype *mon = NULL;
758 static int init_done = 0;
759
760 if (init_done)
761 return;
762 init_done = 1;
763 first_race = 0;
764
765 sprintf (fname, "%s/races", settings.datadir);
766 LOG (llevDebug, "Reading races from %s...\n", fname);
767 if (!(file = fopen (fname, "r")))
768 {
769 LOG (llevError, "Cannot open races file %s: %s\n", fname, strerror (errno));
770 return;
771 }
772
773 while (fgets (buf, MAX_BUF, file) != NULL)
774 {
775 int set_race = 1, set_list = 1;
776
777 if (*buf == '#')
778 continue;
779
780 if ((cp = strchr (buf, '\n')) != NULL)
781 *cp = '\0';
782
783 cp = buf;
784 while (*cp == ' ' || *cp == '!' || *cp == '@')
785 {
786 if (*cp == '!') set_race = 0;
787 if (*cp == '@') set_list = 0;
788
789 cp++;
790 }
791
792 if (sscanf (cp, "RACE %s", variable))
793 /* set new race value */
794 strcpy (race, variable);
795 else
796 {
797 char *cp1;
798
799 /* Take out beginning spaces */
800 for (cp1 = cp; *cp1 == ' '; cp1++)
801 ;
802 /* Remove newline and trailing spaces */
803 for (cp1 = cp + strlen (cp) - 1; *cp1 == '\n' || *cp1 == ' '; cp1--)
804 {
805 *cp1 = '\0';
806 if (cp == cp1)
807 break;
808 }
809
810 if (cp[strlen (cp) - 1] == '\n')
811 cp[strlen (cp) - 1] = '\0';
812
813 /* set creature race to race value */
814 if ((mon = archetype::find (cp)) == NULL)
815 LOG (llevError, "Creature %s in race file lacks archetype\n", cp);
816 else
817 {
818 if (set_race && (!mon->race || strcmp (&mon->race, race)))
819 {
820 if (mon->race)
821 LOG (llevDebug, "Resetting race to %s from %s for archetype %s\n", race, &mon->race, &mon->archname);
822
823 mon->race = race;
824 }
825
826 /* if the arch is a monster, add it to the race list */
827 if (set_list && QUERY_FLAG (mon, FLAG_MONSTER))
828 add_to_racelist (race, mon);
829 }
830 }
831 }
832
833 fclose (file);
834 LOG (llevDebug, "done.\n");
835 }
836
837 void
838 dump_races (void)
839 {
840 racelink *list;
841 objectlink *tmp;
842
843 for (list = first_race; list; list = list->next)
844 {
845 fprintf (stderr, "\nRACE %s:\t", &list->name);
846 for (tmp = list->member; tmp; tmp = tmp->next)
847 fprintf (stderr, "%s(%d), ", &tmp->ob->arch->archname, tmp->ob->level);
848 }
849
850 fprintf (stderr, "\n");
851 }
852
853 void
854 add_to_racelist (const char *race_name, object *op)
855 {
856 racelink *race;
857
858 if (!op || !race_name)
859 return;
860
861 race = find_racelink (race_name);
862
863 if (!race)
864 { /* add in a new race list */
865 race = get_racelist ();
866 race->next = first_race;
867 first_race = race;
868 race->name = race_name;
869 }
870
871 if (race->member->ob)
872 {
873 objectlink *tmp = get_objectlink ();
874
875 tmp->next = race->member;
876 race->member = tmp;
877 }
878
879 race->nrof++;
880 race->member->ob = op;
881 }
882
883 racelink *
884 get_racelist ()
885 {
886 racelink *list = new racelink;
887
888 list->name = 0;
889 list->nrof = 0;
890 list->next = 0;
891 list->member = get_objectlink ();
892
893 return list;
894 }
895
896 racelink *
897 find_racelink (const char *name)
898 {
899 if (name)
900 for (racelink *link = first_race; link; link = link->next)
901 if (!link->name || !strcmp (name, link->name))
902 return link;
903
904 return 0;
905 }