ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.70
Committed: Thu Jan 1 16:05:13 2009 UTC (15 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_80, rel-2_76, rel-2_77, rel-2_75, rel-2_79, rel-2_78
Changes since 1.69: +1 -1 lines
Log Message:
rmeove most shstr-strcmps

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 = shstr_unknown;
39 mt->description = 0;
40
41 for (i = 0; i < NROFATTACKS; i++)
42 {
43 mt->save[i] = 0;
44 mt->mod[i] = 0;
45 }
46
47 mt->chance = 0;
48 mt->difficulty = 0;
49 mt->magic = 0;
50 mt->damage = 0;
51 mt->wc = 0;
52 mt->ac = 0;
53 mt->sp = 0;
54 mt->weight = 100;
55 mt->value = 100;
56 mt->density = 1;
57 mt->next = 0;
58
59 return mt;
60 }
61
62 void
63 load_materials (void)
64 {
65 char filename[MAX_BUF];
66
67 sprintf (filename, "%s/materials", settings.datadir);
68 LOG (llevDebug, "Reading material type data from %s...\n", filename);
69
70 //TODO: somehow free old materials, or update them in-place
71 materialt = 0;
72
73 object_thawer thawer (filename);
74
75 if (!thawer)
76 {
77 LOG (llevError, "Cannot open %s for reading\n", filename);
78 goto done;
79 }
80
81 while (thawer.kw != KW_name)
82 {
83 thawer.next ();
84
85 if (thawer.kw == KW_EOF)
86 goto done;
87 }
88
89 materialtype_t *mt;
90
91 for (;;)
92 {
93 switch (thawer.kw)
94 {
95 case KW_name:
96 mt = get_empty_mat ();
97 mt->next = materialt;
98 materialt = mt;
99
100 thawer.get (mt->name);
101 mt->description = mt->name;
102 break;
103
104 case KW_description:
105 thawer.get (mt->description);
106 break;
107
108 case KW_material:
109 thawer.get (mt->material);
110 break;
111
112 case KW_saves:
113 {
114 const char *cp = thawer.get_str () - 1;
115
116 for (int i = 0; i < NROFATTACKS; i++)
117 {
118 if (!cp)
119 {
120 mt->save[i] = 0;
121 continue;
122 }
123
124 int value;
125 ++cp;
126 sscanf (cp, "%d", &value);
127 mt->save[i] = (sint8) value;
128 cp = strchr (cp, ',');
129 }
130 }
131 break;
132
133 case KW_mods:
134 {
135 const char *cp = thawer.get_str () - 1;
136
137 for (int i = 0; i < NROFATTACKS; i++)
138 {
139 if (!cp)
140 {
141 mt->save[i] = 0;
142 continue;
143 }
144
145 ++cp;
146 int value;
147 sscanf (cp, "%d", &value);
148 mt->mod[i] = (sint8) value;
149 cp = strchr (cp, ',');
150 }
151 }
152 break;
153
154 case KW_chance: thawer.get (mt->chance); break;
155 case KW_difficulty: // cf+ alias, not original cf
156 case KW_diff: thawer.get (mt->difficulty); break;
157 case KW_magic: thawer.get (mt->magic); break;
158 case KW_dam: // cf+ alias, not original cf
159 case KW_damage: thawer.get (mt->damage); break;
160 case KW_wc: thawer.get (mt->wc); break;
161 case KW_ac: thawer.get (mt->ac); break;
162 case KW_sp: thawer.get (mt->sp); break;
163 case KW_weight: thawer.get (mt->weight); break;
164 case KW_value: thawer.get (mt->value); break;
165 case KW_density: thawer.get (mt->density); break;
166
167 case KW_EOF:
168 goto done;
169
170 default:
171 if (!thawer.parse_error ("materials file", "materials"))
172 goto done;
173 break;
174 }
175
176 thawer.next ();
177 }
178
179 done:
180 if (!materialt)
181 materialt = get_empty_mat ();
182
183 LOG (llevDebug, "Done.\n");
184 }
185
186 /* This loads the settings file. There could be debate whether this should
187 * be here or in the common directory - but since only the server needs this
188 * information, having it here probably makes more sense.
189 */
190 void
191 load_settings (void)
192 {
193 char buf[MAX_BUF], *cp;
194 int has_val, comp;
195 FILE *fp;
196
197 sprintf (buf, "%s/settings", settings.confdir);
198
199 /* We don't require a settings file at current time, but down the road,
200 * there will probably be so many values that not having a settings file
201 * will not be a good thing.
202 */
203 if (!(fp = open_and_uncompress (buf, 0, &comp)))
204 {
205 LOG (llevError, "Error: No settings file found\n");
206 exit (1);
207 }
208
209 while (fgets (buf, MAX_BUF - 1, fp) != NULL)
210 {
211 if (buf[0] == '#')
212 continue;
213 /* eliminate newline */
214 if ((cp = strrchr (buf, '\n')) != NULL)
215 *cp = '\0';
216
217 /* Skip over empty lines */
218 if (buf[0] == 0)
219 continue;
220
221 /* Skip all the spaces and set them to nulls. If not space,
222 * set cp to "" to make strcpy's and the like easier down below.
223 */
224 if ((cp = strchr (buf, ' ')) != NULL)
225 {
226 while (*cp == ' ')
227 *cp++ = 0;
228 has_val = 1;
229 }
230 else
231 {
232 cp = (char *)"";
233 has_val = 0;
234 }
235
236 if (!strcasecmp (buf, "motd"))
237 {
238 if (has_val)
239 strcpy (settings.motd, cp);
240 else
241 LOG (llevError, "load_settings: motd must have a value.\n");
242 }
243 else if (!strcasecmp (buf, "dm_mail"))
244 {
245 if (has_val)
246 strcpy (settings.dm_mail, cp);
247 else
248 LOG (llevError, "load_settings: dm_mail must have a value.\n");
249 }
250 else if (!strcasecmp (buf, "worldmapstartx"))
251 {
252 int size = atoi (cp);
253
254 if (size < 0)
255 LOG (llevError, "load_settings: worldmapstartx must be at least " "0, %d is invalid\n", size);
256 else
257 settings.worldmapstartx = size;
258 }
259 else if (!strcasecmp (buf, "worldmapstarty"))
260 {
261 int size = atoi (cp);
262
263 if (size < 0)
264 LOG (llevError, "load_settings: worldmapstarty must be at least " "0, %d is invalid\n", size);
265 else
266 settings.worldmapstarty = size;
267 }
268 else if (!strcasecmp (buf, "worldmaptilesx"))
269 {
270 int size = atoi (cp);
271
272 if (size < 1)
273 LOG (llevError, "load_settings: worldmaptilesx must be greater " "than 1, %d is invalid\n", size);
274 else
275 settings.worldmaptilesx = size;
276 }
277 else if (!strcasecmp (buf, "worldmaptilesy"))
278 {
279 int size = atoi (cp);
280
281 if (size < 1)
282 LOG (llevError, "load_settings: worldmaptilesy must be greater " "than 1, %d is invalid\n", size);
283 else
284 settings.worldmaptilesy = size;
285 }
286 else if (!strcasecmp (buf, "worldmaptilesizex"))
287 {
288 int size = atoi (cp);
289
290 if (size < 1)
291 LOG (llevError, "load_settings: worldmaptilesizex must be " "greater than 1, %d is invalid\n", size);
292 else
293 settings.worldmaptilesizex = size;
294 }
295 else if (!strcasecmp (buf, "worldmaptilesizey"))
296 {
297 int size = atoi (cp);
298
299 if (size < 1)
300 LOG (llevError, "load_settings: worldmaptilesizey must be " "greater than 1, %d is invalid\n", size);
301 else
302 settings.worldmaptilesizey = size;
303 }
304 else if (!strcasecmp (buf, "dynamiclevel"))
305 {
306 int lev = atoi (cp);
307
308 if (lev < 0)
309 LOG (llevError, "load_settings: dynamiclevel must be " "at least 0, %d is invalid\n", lev);
310 else
311 settings.dynamiclevel = lev;
312 }
313 else if (!strcasecmp (buf, "fastclock"))
314 {
315 int lev = atoi (cp);
316
317 if (lev < 0)
318 LOG (llevError, "load_settings: fastclock must be at least 0" ", %d is invalid\n", lev);
319 else
320 settings.fastclock = lev;
321 }
322 else if (!strcasecmp (buf, "not_permadeth"))
323 {
324 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
325 {
326 settings.not_permadeth = TRUE;
327 }
328 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
329 {
330 settings.not_permadeth = FALSE;
331 }
332 else
333 {
334 LOG (llevError, "load_settings: Unknown value for not_permadeth" ": %s\n", cp);
335 }
336 }
337 else if (!strcasecmp (buf, "resurrection"))
338 {
339 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
340 {
341 settings.resurrection = TRUE;
342 }
343 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
344 {
345 settings.resurrection = FALSE;
346 }
347 else
348 {
349 LOG (llevError, "load_settings: Unknown value for resurrection" ": %s\n", cp);
350 }
351 }
352 else if (!strcasecmp (buf, "set_title"))
353 {
354 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
355 {
356 settings.set_title = TRUE;
357 }
358 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
359 {
360 settings.set_title = FALSE;
361 }
362 else
363 {
364 LOG (llevError, "load_settings: Unknown value for set_title" ": %s\n", cp);
365 }
366 }
367 else if (!strcasecmp (buf, "search_items"))
368 {
369 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
370 {
371 settings.search_items = TRUE;
372 }
373 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
374 {
375 settings.search_items = FALSE;
376 }
377 else
378 {
379 LOG (llevError, "load_settings: Unknown value for search_items" ": %s\n", cp);
380 }
381 }
382 else if (!strcasecmp (buf, "spell_encumbrance"))
383 {
384 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
385 {
386 settings.spell_encumbrance = TRUE;
387 }
388 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
389 {
390 settings.spell_encumbrance = FALSE;
391 }
392 else
393 {
394 LOG (llevError, "load_settings: Unknown value for " "spell_encumbrance: %s\n", cp);
395 }
396 }
397 else if (!strcasecmp (buf, "spell_failure_effects"))
398 {
399 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
400 {
401 settings.spell_failure_effects = TRUE;
402 }
403 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
404 {
405 settings.spell_failure_effects = FALSE;
406 }
407 else
408 {
409 LOG (llevError, "load_settings: Unknown value for " "spell_failure_effects: %s\n", cp);
410 }
411 }
412 else if (!strcasecmp (buf, "spellpoint_level_depend"))
413 {
414 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
415 {
416 settings.spellpoint_level_depend = TRUE;
417 }
418 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
419 {
420 settings.spellpoint_level_depend = FALSE;
421 }
422 else
423 {
424 LOG (llevError, "load_settings: Unknown value for " "spellpoint_level_depend: %s\n", cp);
425 }
426 }
427 else if (!strcasecmp (buf, "stat_loss_on_death"))
428 {
429 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
430 {
431 settings.stat_loss_on_death = TRUE;
432 }
433 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
434 {
435 settings.stat_loss_on_death = FALSE;
436 }
437 else
438 {
439 LOG (llevError, "load_settings: Unknown value for " "stat_loss_on_death: %s\n", cp);
440 }
441 }
442 else if (!strcasecmp (buf, "use_permanent_experience"))
443 {
444 LOG (llevError, "use_permanent_experience is deprecated, use" "permenent_experience_percentage instead\n");
445 }
446 else if (!strcasecmp (buf, "permanent_experience_percentage"))
447 {
448 int val = atoi (cp);
449
450 if (val < 0 || val > 100)
451 LOG (llevError, "load_settings: permenent_experience_percentage" "must be between 0 and 100, %d is invalid\n", val);
452 else
453 settings.permanent_exp_ratio = val;
454 }
455 else if (!strcasecmp (buf, "death_penalty_percentage"))
456 {
457 int val = atoi (cp);
458
459 if (val < 0 || val > 100)
460 LOG (llevError, "load_settings: death_penalty_percentage" "must be between 0 and 100, %d is invalid\n", val);
461 else
462 settings.death_penalty_ratio = val;
463 }
464 else if (!strcasecmp (buf, "death_penalty_levels"))
465 {
466 int val = atoi (cp);
467
468 if (val < 0 || val > 255)
469 LOG (llevError, "load_settings: death_penalty_levels" "can not be negative, %d is invalid\n", val);
470 else
471 settings.death_penalty_level = val;
472 }
473 else if (!strcasecmp (buf, "balanced_stat_loss"))
474 {
475 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
476 {
477 settings.balanced_stat_loss = TRUE;
478 }
479 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
480 {
481 settings.balanced_stat_loss = FALSE;
482 }
483 else
484 {
485 LOG (llevError, "load_settings: Unknown value for " "balanced_stat_loss: %s\n", cp);
486 }
487 }
488 else if (!strcasecmp (buf, "simple_exp"))
489 {
490 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
491 {
492 settings.simple_exp = TRUE;
493 }
494 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
495 {
496 settings.simple_exp = FALSE;
497 }
498 else
499 {
500 LOG (llevError, "load_settings: Unknown value for simple_exp: %s\n", cp);
501 }
502 }
503 else if (!strcasecmp (buf, "item_power_factor"))
504 {
505 float tmp = atof (cp);
506
507 if (tmp < 0)
508 LOG (llevError, "load_settings: item_power_factor must be a positive number (%f < 0)\n", tmp);
509 else
510 settings.item_power_factor = tmp;
511 }
512 else if (!strcasecmp (buf, "pk_luck_penalty"))
513 {
514 sint16 val = atoi (cp);
515
516 if (val < -100 || val > 100)
517 LOG (llevError, "load_settings: pk_luck_penalty must be between -100 and 100" ", %d is invalid\n", val);
518 else
519 settings.pk_luck_penalty = val;
520 }
521 else if (!strcasecmp (buf, "set_friendly_fire"))
522 {
523 int val = atoi (cp);
524
525 if (val < 0 || val > 100)
526 LOG (llevError, "load_settings: set_friendly_fire must be between 0 an 100" ", %d is invalid\n", val);
527 else
528 settings.set_friendly_fire = val;
529 }
530 else if (!strcasecmp (buf, "armor_max_enchant"))
531 {
532 int max_e = atoi (cp);
533
534 if (max_e <= 0)
535 LOG (llevError, "load_settings: armor_max_enchant is %d\n", max_e);
536 else
537 settings.armor_max_enchant = max_e;
538 }
539 else if (!strcasecmp (buf, "armor_weight_reduction"))
540 {
541 int wr = atoi (cp);
542
543 if (wr < 0)
544 LOG (llevError, "load_settings: armor_weight_reduction is %d\n", wr);
545 else
546 settings.armor_weight_reduction = wr;
547 }
548 else if (!strcasecmp (buf, "armor_weight_linear"))
549 {
550 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
551 {
552 settings.armor_weight_linear = TRUE;
553 }
554 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
555 {
556 settings.armor_weight_linear = FALSE;
557 }
558 else
559 {
560 LOG (llevError, "load_settings: unknown value for armor_weight_linear: %s\n", cp);
561 }
562
563 }
564 else if (!strcasecmp (buf, "armor_speed_improvement"))
565 {
566 int wr = atoi (cp);
567
568 if (wr < 0)
569 LOG (llevError, "load_settings: armor_speed_improvement is %d\n", wr);
570 else
571 settings.armor_speed_improvement = wr;
572 }
573 else if (!strcasecmp (buf, "armor_speed_linear"))
574 {
575 if (!strcasecmp (cp, "on") || !strcasecmp (cp, "true"))
576 {
577 settings.armor_speed_linear = TRUE;
578 }
579 else if (!strcasecmp (cp, "off") || !strcasecmp (cp, "false"))
580 {
581 settings.armor_speed_linear = FALSE;
582 }
583 else
584 {
585 LOG (llevError, "load_settings: unknown value for armor_speed_linear: %s\n", cp);
586 }
587
588 }
589 else
590 LOG (llevError, "Unknown value in settings file: %s\n", buf);
591 }
592
593 close_and_delete (fp, comp);
594 }
595
596 /*
597 * init() is called only once, when starting the program.
598 */
599 void
600 init (int argc, char **argv)
601 {
602 init_done = 0; /* Must be done before init_signal() */
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 }