ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.66
Committed: Tue Sep 23 04:29:11 2008 UTC (15 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_72, rel-2_73, rel-2_71
Changes since 1.65: +2 -349 lines
Log Message:
*** empty log message ***

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