ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/init.C
Revision: 1.68
Committed: Sun Dec 28 06:59:27 2008 UTC (15 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_74
Changes since 1.67: +22 -25 lines
Log Message:
smell, remove gcfclient support, material fixes, cfpod parse fixes, refactoring

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 rndm.seed (time (0));
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 }