OSDN Git Service

mprocシステムの変更に関する話し合いの結果より, 以下のように変更.
[hengband/hengband.git] / src / init2.c
1 /* File: init2.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: Initialization (part 2) -BEN- */
12
13 #include "angband.h"
14
15 #include "init.h"
16
17 #ifndef MACINTOSH
18 #ifdef CHECK_MODIFICATION_TIME
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #endif /* CHECK_MODIFICATION_TIME */
22 #endif
23
24 /*
25  * This file is used to initialize various variables and arrays for the
26  * Angband game.  Note the use of "fd_read()" and "fd_write()" to bypass
27  * the common limitation of "read()" and "write()" to only 32767 bytes
28  * at a time.
29  *
30  * Several of the arrays for Angband are built from "template" files in
31  * the "lib/file" directory, from which quick-load binary "image" files
32  * are constructed whenever they are not present in the "lib/data"
33  * directory, or if those files become obsolete, if we are allowed.
34  *
35  * Warning -- the "ascii" file parsers use a minor hack to collect the
36  * name and text information in a single pass.  Thus, the game will not
37  * be able to load any template file with more than 20K of names or 60K
38  * of text, even though technically, up to 64K should be legal.
39  *
40  * The "init1.c" file is used only to parse the ascii template files,
41  * to create the binary image files.  If you include the binary image
42  * files instead of the ascii template files, then you can undefine
43  * "ALLOW_TEMPLATES", saving about 20K by removing "init1.c".  Note
44  * that the binary image files are extremely system dependant.
45  */
46
47
48
49 /*
50  * Find the default paths to all of our important sub-directories.
51  *
52  * The purpose of each sub-directory is described in "variable.c".
53  *
54  * All of the sub-directories should, by default, be located inside
55  * the main "lib" directory, whose location is very system dependant.
56  *
57  * This function takes a writable buffer, initially containing the
58  * "path" to the "lib" directory, for example, "/pkg/lib/angband/",
59  * or a system dependant string, for example, ":lib:".  The buffer
60  * must be large enough to contain at least 32 more characters.
61  *
62  * Various command line options may allow some of the important
63  * directories to be changed to user-specified directories, most
64  * importantly, the "info" and "user" and "save" directories,
65  * but this is done after this function, see "main.c".
66  *
67  * In general, the initial path should end in the appropriate "PATH_SEP"
68  * string.  All of the "sub-directory" paths (created below or supplied
69  * by the user) will NOT end in the "PATH_SEP" string, see the special
70  * "path_build()" function in "util.c" for more information.
71  *
72  * Mega-Hack -- support fat raw files under NEXTSTEP, using special
73  * "suffixed" directories for the "ANGBAND_DIR_DATA" directory, but
74  * requiring the directories to be created by hand by the user.
75  *
76  * Hack -- first we free all the strings, since this is known
77  * to succeed even if the strings have not been allocated yet,
78  * as long as the variables start out as "NULL".  This allows
79  * this function to be called multiple times, for example, to
80  * try several base "path" values until a good one is found.
81  */
82 void init_file_paths(char *path)
83 {
84         char *tail;
85
86 #ifdef PRIVATE_USER_PATH
87         char buf[1024];
88 #endif /* PRIVATE_USER_PATH */
89
90         /*** Free everything ***/
91
92         /* Free the main path */
93         string_free(ANGBAND_DIR);
94
95         /* Free the sub-paths */
96         string_free(ANGBAND_DIR_APEX);
97         string_free(ANGBAND_DIR_BONE);
98         string_free(ANGBAND_DIR_DATA);
99         string_free(ANGBAND_DIR_EDIT);
100         string_free(ANGBAND_DIR_SCRIPT);
101         string_free(ANGBAND_DIR_FILE);
102         string_free(ANGBAND_DIR_HELP);
103         string_free(ANGBAND_DIR_INFO);
104         string_free(ANGBAND_DIR_SAVE);
105         string_free(ANGBAND_DIR_USER);
106         string_free(ANGBAND_DIR_XTRA);
107
108
109         /*** Prepare the "path" ***/
110
111         /* Hack -- save the main directory */
112         ANGBAND_DIR = string_make(path);
113
114         /* Prepare to append to the Base Path */
115         tail = path + strlen(path);
116
117
118 #ifdef VM
119
120         /*** Use "flat" paths with VM/ESA ***/
121
122         /* Use "blank" path names */
123         ANGBAND_DIR_APEX = string_make("");
124         ANGBAND_DIR_BONE = string_make("");
125         ANGBAND_DIR_DATA = string_make("");
126         ANGBAND_DIR_EDIT = string_make("");
127         ANGBAND_DIR_SCRIPT = string_make("");
128         ANGBAND_DIR_FILE = string_make("");
129         ANGBAND_DIR_HELP = string_make("");
130         ANGBAND_DIR_INFO = string_make("");
131         ANGBAND_DIR_SAVE = string_make("");
132         ANGBAND_DIR_USER = string_make("");
133         ANGBAND_DIR_XTRA = string_make("");
134
135
136 #else /* VM */
137
138
139         /*** Build the sub-directory names ***/
140
141         /* Build a path name */
142         strcpy(tail, "apex");
143         ANGBAND_DIR_APEX = string_make(path);
144
145         /* Build a path name */
146         strcpy(tail, "bone");
147         ANGBAND_DIR_BONE = string_make(path);
148
149         /* Build a path name */
150         strcpy(tail, "data");
151         ANGBAND_DIR_DATA = string_make(path);
152
153         /* Build a path name */
154         strcpy(tail, "edit");
155         ANGBAND_DIR_EDIT = string_make(path);
156
157         /* Build a path name */
158         strcpy(tail, "script");
159         ANGBAND_DIR_SCRIPT = string_make(path);
160
161         /* Build a path name */
162         strcpy(tail, "file");
163         ANGBAND_DIR_FILE = string_make(path);
164
165         /* Build a path name */
166         strcpy(tail, "help");
167         ANGBAND_DIR_HELP = string_make(path);
168
169         /* Build a path name */
170         strcpy(tail, "info");
171         ANGBAND_DIR_INFO = string_make(path);
172
173         /* Build a path name */
174         strcpy(tail, "pref");
175         ANGBAND_DIR_PREF = string_make(path);
176
177         /* Build a path name */
178         strcpy(tail, "save");
179         ANGBAND_DIR_SAVE = string_make(path);
180
181 #ifdef PRIVATE_USER_PATH
182
183         /* Build the path to the user specific directory */
184         path_build(buf, sizeof(buf), PRIVATE_USER_PATH, VERSION_NAME);
185
186         /* Build a relative path name */
187         ANGBAND_DIR_USER = string_make(buf);
188
189 #else /* PRIVATE_USER_PATH */
190
191         /* Build a path name */
192         strcpy(tail, "user");
193         ANGBAND_DIR_USER = string_make(path);
194
195 #endif /* PRIVATE_USER_PATH */
196
197         /* Build a path name */
198         strcpy(tail, "xtra");
199         ANGBAND_DIR_XTRA = string_make(path);
200
201 #endif /* VM */
202
203
204 #ifdef NeXT
205
206         /* Allow "fat binary" usage with NeXT */
207         if (TRUE)
208         {
209                 cptr next = NULL;
210
211 # if defined(m68k)
212                 next = "m68k";
213 # endif
214
215 # if defined(i386)
216                 next = "i386";
217 # endif
218
219 # if defined(sparc)
220                 next = "sparc";
221 # endif
222
223 # if defined(hppa)
224                 next = "hppa";
225 # endif
226
227                 /* Use special directory */
228                 if (next)
229                 {
230                         /* Forget the old path name */
231                         string_free(ANGBAND_DIR_DATA);
232
233                         /* Build a new path name */
234                         sprintf(tail, "data-%s", next);
235                         ANGBAND_DIR_DATA = string_make(path);
236                 }
237         }
238
239 #endif /* NeXT */
240
241 }
242
243
244
245 #ifdef ALLOW_TEMPLATES
246
247
248 /*
249  * Hack -- help give useful error messages
250  */
251 int error_idx;
252 int error_line;
253
254
255 /*
256  * Standard error message text
257  */
258 cptr err_str[PARSE_ERROR_MAX] =
259 {
260         NULL,
261 #ifdef JP
262         "ʸˡ¥¨¥é¡¼",
263         "¸Å¤¤¥Õ¥¡¥¤¥ë",
264         "µ­Ï¿¥Ø¥Ã¥À¤¬¤Ê¤¤",
265         "ÉÔϢ³¥ì¥³¡¼¥É",
266         "¤ª¤«¤·¤Ê¥Õ¥é¥°Â¸ºß",
267         "̤ÄêµÁÌ¿Îá",
268         "¥á¥â¥êÉÔ­",
269         "ºÂɸÈϰϳ°",
270         "°ú¿ôÉÔ­",
271         "̤ÄêµÁÃÏ·Á¥¿¥°",
272 #else
273         "parse error",
274         "obsolete file",
275         "missing record header",
276         "non-sequential records",
277         "invalid flag specification",
278         "undefined directive",
279         "out of memory",
280         "coordinates out of bounds",
281         "too few arguments",
282         "undefined terrain tag",
283 #endif
284
285 };
286
287
288 #endif /* ALLOW_TEMPLATES */
289
290
291 /*
292  * File headers
293  */
294 header v_head;
295 header f_head;
296 header k_head;
297 header a_head;
298 header e_head;
299 header r_head;
300 header d_head;
301 header s_head;
302 header m_head;
303
304 #ifdef CHECK_MODIFICATION_TIME
305
306 static errr check_modification_date(int fd, cptr template_file)
307 {
308         char buf[1024];
309
310         struct stat txt_stat, raw_stat;
311
312         /* Build the filename */
313         path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, template_file);
314
315         /* Access stats on text file */
316         if (stat(buf, &txt_stat))
317         {
318                 /* No text file - continue */
319         }
320
321         /* Access stats on raw file */
322         else if (fstat(fd, &raw_stat))
323         {
324                 /* Error */
325                 return (-1);
326         }
327
328         /* Ensure text file is not newer than raw file */
329         else if (txt_stat.st_mtime > raw_stat.st_mtime)
330         {
331                 /* Reprocess text file */
332                 return (-1);
333         }
334
335         return (0);
336 }
337
338 #endif /* CHECK_MODIFICATION_TIME */
339
340
341
342 /*** Initialize from binary image files ***/
343
344
345 /*
346  * Initialize the "*_info" array, by parsing a binary "image" file
347  */
348 static errr init_info_raw(int fd, header *head)
349 {
350         header test;
351
352         /* Read and Verify the header */
353         if (fd_read(fd, (char*)(&test), sizeof(header)) ||
354             (test.v_major != head->v_major) ||
355             (test.v_minor != head->v_minor) ||
356             (test.v_patch != head->v_patch) ||
357             (test.info_num != head->info_num) ||
358             (test.info_len != head->info_len) ||
359             (test.head_size != head->head_size) ||
360             (test.info_size != head->info_size))
361         {
362                 /* Error */
363                 return (-1);
364         }
365
366
367         /* Accept the header */
368         (*head) = test;
369
370
371         /* Allocate the "*_info" array */
372         C_MAKE(head->info_ptr, head->info_size, char);
373
374         /* Read the "*_info" array */
375         fd_read(fd, head->info_ptr, head->info_size);
376
377
378         if (head->name_size)
379         {
380                 /* Allocate the "*_name" array */
381                 C_MAKE(head->name_ptr, head->name_size, char);
382
383                 /* Read the "*_name" array */
384                 fd_read(fd, head->name_ptr, head->name_size);
385         }
386
387
388         if (head->text_size)
389         {
390                 /* Allocate the "*_text" array */
391                 C_MAKE(head->text_ptr, head->text_size, char);
392
393                 /* Read the "*_text" array */
394                 fd_read(fd, head->text_ptr, head->text_size);
395         }
396
397
398         if (head->tag_size)
399         {
400                 /* Allocate the "*_tag" array */
401                 C_MAKE(head->tag_ptr, head->tag_size, char);
402
403                 /* Read the "*_tag" array */
404                 fd_read(fd, head->tag_ptr, head->tag_size);
405         }
406
407
408         /* Success */
409         return (0);
410 }
411
412
413
414 /*
415  * Initialize the header of an *_info.raw file.
416  */
417 static void init_header(header *head, int num, int len)
418 {
419         /* Save the "version" */
420         head->v_major = FAKE_VER_MAJOR;
421         head->v_minor = FAKE_VER_MINOR;
422         head->v_patch = FAKE_VER_PATCH;
423         head->v_extra = 0;
424
425         /* Save the "record" information */
426         head->info_num = num;
427         head->info_len = len;
428
429         /* Save the size of "*_head" and "*_info" */
430         head->head_size = sizeof(header);
431         head->info_size = head->info_num * head->info_len;
432 }
433
434
435 /*
436  * Initialize the "*_info" array
437  *
438  * Note that we let each entry have a unique "name" and "text" string,
439  * even if the string happens to be empty (everyone has a unique '\0').
440  */
441 static errr init_info(cptr filename, header *head,
442                       void **info, char **name, char **text, char **tag)
443 {
444         int fd;
445
446         int mode = 0644;
447
448         errr err = 1;
449
450         FILE *fp;
451
452         /* General buffer */
453         char buf[1024];
454
455
456 #ifdef ALLOW_TEMPLATES
457
458         /*** Load the binary image file ***/
459
460         /* Build the filename */
461 #ifdef JP
462         path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s_j.raw", filename));
463 #else
464         path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename));
465 #endif
466
467
468         /* Attempt to open the "raw" file */
469         fd = fd_open(buf, O_RDONLY);
470
471         /* Process existing "raw" file */
472         if (fd >= 0)
473         {
474 #ifdef CHECK_MODIFICATION_TIME
475
476                 err = check_modification_date(fd, format("%s.txt", filename));
477
478 #endif /* CHECK_MODIFICATION_TIME */
479
480                 /* Attempt to parse the "raw" file */
481                 if (!err)
482                         err = init_info_raw(fd, head);
483
484                 /* Close it */
485                 (void)fd_close(fd);
486         }
487
488
489         /* Do we have to parse the *.txt file? */
490         if (err)
491         {
492                 /*** Make the fake arrays ***/
493
494                 /* Allocate the "*_info" array */
495                 C_MAKE(head->info_ptr, head->info_size, char);
496
497                 /* Hack -- make "fake" arrays */
498                 if (name) C_MAKE(head->name_ptr, FAKE_NAME_SIZE, char);
499                 if (text) C_MAKE(head->text_ptr, FAKE_TEXT_SIZE, char);
500                 if (tag)  C_MAKE(head->tag_ptr, FAKE_TAG_SIZE, char);
501
502                 if (info) (*info) = head->info_ptr;
503                 if (name) (*name) = head->name_ptr;
504                 if (text) (*text) = head->text_ptr;
505                 if (tag)  (*tag)  = head->tag_ptr;
506
507                 /*** Load the ascii template file ***/
508
509                 /* Build the filename */
510
511                 path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, format("%s.txt", filename));
512
513                 /* Open the file */
514                 fp = my_fopen(buf, "r");
515
516                 /* Parse it */
517 #ifdef JP
518                 if (!fp) quit(format("'%s.txt'¥Õ¥¡¥¤¥ë¤ò¥ª¡¼¥×¥ó¤Ç¤­¤Þ¤»¤ó¡£", filename));
519 #else
520                 if (!fp) quit(format("Cannot open '%s.txt' file.", filename));
521 #endif
522
523
524                 /* Parse the file */
525                 err = init_info_txt(fp, buf, head, head->parse_info_txt);
526
527                 /* Close it */
528                 my_fclose(fp);
529
530                 /* Errors */
531                 if (err)
532                 {
533                         cptr oops;
534
535 #ifdef JP
536                         /* Error string */
537                         oops = (((err > 0) && (err < PARSE_ERROR_MAX)) ? err_str[err] : "̤ÃΤÎ");
538
539                         /* Oops */
540                         msg_format("'%s.txt'¥Õ¥¡¥¤¥ë¤Î %d ¹ÔÌܤ˥¨¥é¡¼¡£", filename, error_line);
541                         msg_format("¥ì¥³¡¼¥É %d ¤Ï '%s' ¥¨¥é¡¼¤¬¤¢¤ê¤Þ¤¹¡£", error_idx, oops);
542                         msg_format("¹½Ê¸ '%s'¡£", buf);
543                         msg_print(NULL);
544
545                         /* Quit */
546                         quit(format("'%s.txt'¥Õ¥¡¥¤¥ë¤Ë¥¨¥é¡¼", filename));
547 #else
548                         /* Error string */
549                         oops = (((err > 0) && (err < PARSE_ERROR_MAX)) ? err_str[err] : "unknown");
550
551                         /* Oops */
552                         msg_format("Error %d at line %d of '%s.txt'.", err, error_line, filename);
553                         msg_format("Record %d contains a '%s' error.", error_idx, oops);
554                         msg_format("Parsing '%s'.", buf);
555                         msg_print(NULL);
556
557                         /* Quit */
558                         quit(format("Error in '%s.txt' file.", filename));
559 #endif
560
561                 }
562
563
564                 /*** Make final retouch on fake tags ***/
565
566                 if (head->retouch)
567                 {
568                         (*head->retouch)(head);
569                 }
570
571
572                 /*** Dump the binary image file ***/
573
574                 /* File type is "DATA" */
575                 FILE_TYPE(FILE_TYPE_DATA);
576
577                 /* Build the filename */
578 #ifdef JP
579                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s_j.raw", filename));
580 #else
581                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename));
582 #endif
583
584
585                 /* Grab permissions */
586                 safe_setuid_grab();
587
588                 /* Kill the old file */
589                 (void)fd_kill(buf);
590
591                 /* Attempt to create the raw file */
592                 fd = fd_make(buf, mode);
593
594                 /* Drop permissions */
595                 safe_setuid_drop();
596
597                 /* Dump to the file */
598                 if (fd >= 0)
599                 {
600                         /* Dump it */
601                         fd_write(fd, (cptr)(head), head->head_size);
602
603                         /* Dump the "*_info" array */
604                         fd_write(fd, head->info_ptr, head->info_size);
605
606                         /* Dump the "*_name" array */
607                         fd_write(fd, head->name_ptr, head->name_size);
608
609                         /* Dump the "*_text" array */
610                         fd_write(fd, head->text_ptr, head->text_size);
611
612                         /* Dump the "*_tag" array */
613                         fd_write(fd, head->tag_ptr, head->tag_size);
614
615                         /* Close */
616                         (void)fd_close(fd);
617                 }
618
619
620                 /*** Kill the fake arrays ***/
621
622                 /* Free the "*_info" array */
623                 C_KILL(head->info_ptr, head->info_size, char);
624
625                 /* Hack -- Free the "fake" arrays */
626                 if (name) C_KILL(head->name_ptr, FAKE_NAME_SIZE, char);
627                 if (text) C_KILL(head->text_ptr, FAKE_TEXT_SIZE, char);
628                 if (tag)  C_KILL(head->tag_ptr, FAKE_TAG_SIZE, char);
629
630 #endif  /* ALLOW_TEMPLATES */
631
632
633                 /*** Load the binary image file ***/
634
635                 /* Build the filename */
636 #ifdef JP
637                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s_j.raw", filename));
638 #else
639                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename));
640 #endif
641
642
643                 /* Attempt to open the "raw" file */
644                 fd = fd_open(buf, O_RDONLY);
645
646                 /* Process existing "raw" file */
647 #ifdef JP
648                 if (fd < 0) quit(format("'%s_j.raw'¥Õ¥¡¥¤¥ë¤ò¥í¡¼¥É¤Ç¤­¤Þ¤»¤ó¡£", filename));
649 #else
650                 if (fd < 0) quit(format("Cannot load '%s.raw' file.", filename));
651 #endif
652
653
654                 /* Attempt to parse the "raw" file */
655                 err = init_info_raw(fd, head);
656
657                 /* Close it */
658                 (void)fd_close(fd);
659
660                 /* Error */
661 #ifdef JP
662                 if (err) quit(format("'%s_j.raw'¥Õ¥¡¥¤¥ë¤ò²òÀϤǤ­¤Þ¤»¤ó¡£", filename));
663 #else
664                 if (err) quit(format("Cannot parse '%s.raw' file.", filename));
665 #endif
666
667 #ifdef ALLOW_TEMPLATES
668         }
669 #endif
670
671         if (info) (*info) = head->info_ptr;
672         if (name) (*name) = head->name_ptr;
673         if (text) (*text) = head->text_ptr;
674         if (tag)  (*tag)  = head->tag_ptr;
675
676         /* Success */
677         return (0);
678 }
679
680
681 /*
682  * Initialize the "f_info" array
683  */
684 static errr init_f_info(void)
685 {
686         /* Init the header */
687         init_header(&f_head, max_f_idx, sizeof(feature_type));
688
689 #ifdef ALLOW_TEMPLATES
690
691         /* Save a pointer to the parsing function */
692         f_head.parse_info_txt = parse_f_info;
693
694         /* Save a pointer to the retouch fake tags */
695         f_head.retouch = retouch_f_info;
696
697 #endif /* ALLOW_TEMPLATES */
698
699         return init_info("f_info", &f_head,
700                          (void*)&f_info, &f_name, NULL, &f_tag);
701 }
702
703
704 /*
705  * Initialize the "k_info" array
706  */
707 static errr init_k_info(void)
708 {
709         /* Init the header */
710         init_header(&k_head, max_k_idx, sizeof(object_kind));
711
712 #ifdef ALLOW_TEMPLATES
713
714         /* Save a pointer to the parsing function */
715         k_head.parse_info_txt = parse_k_info;
716
717 #endif /* ALLOW_TEMPLATES */
718
719         return init_info("k_info", &k_head,
720                          (void*)&k_info, &k_name, &k_text, NULL);
721 }
722
723
724
725 /*
726  * Initialize the "a_info" array
727  */
728 static errr init_a_info(void)
729 {
730         /* Init the header */
731         init_header(&a_head, max_a_idx, sizeof(artifact_type));
732
733 #ifdef ALLOW_TEMPLATES
734
735         /* Save a pointer to the parsing function */
736         a_head.parse_info_txt = parse_a_info;
737
738 #endif /* ALLOW_TEMPLATES */
739
740         return init_info("a_info", &a_head,
741                          (void*)&a_info, &a_name, &a_text, NULL);
742 }
743
744
745
746 /*
747  * Initialize the "e_info" array
748  */
749 static errr init_e_info(void)
750 {
751         /* Init the header */
752         init_header(&e_head, max_e_idx, sizeof(ego_item_type));
753
754 #ifdef ALLOW_TEMPLATES
755
756         /* Save a pointer to the parsing function */
757         e_head.parse_info_txt = parse_e_info;
758
759 #endif /* ALLOW_TEMPLATES */
760
761         return init_info("e_info", &e_head,
762                          (void*)&e_info, &e_name, &e_text, NULL);
763 }
764
765
766
767 /*
768  * Initialize the "r_info" array
769  */
770 static errr init_r_info(void)
771 {
772         /* Init the header */
773         init_header(&r_head, max_r_idx, sizeof(monster_race));
774
775 #ifdef ALLOW_TEMPLATES
776
777         /* Save a pointer to the parsing function */
778         r_head.parse_info_txt = parse_r_info;
779
780 #endif /* ALLOW_TEMPLATES */
781
782         return init_info("r_info", &r_head,
783                          (void*)&r_info, &r_name, &r_text, NULL);
784 }
785
786
787
788 /*
789  * Initialize the "d_info" array
790  */
791 static errr init_d_info(void)
792 {
793         /* Init the header */
794         init_header(&d_head, max_d_idx, sizeof(dungeon_info_type));
795
796 #ifdef ALLOW_TEMPLATES
797
798         /* Save a pointer to the parsing function */
799         d_head.parse_info_txt = parse_d_info;
800
801 #endif /* ALLOW_TEMPLATES */
802
803         return init_info("d_info", &d_head,
804                          (void*)&d_info, &d_name, &d_text, NULL);
805 }
806
807
808 /*
809  * Initialize the "v_info" array
810  *
811  * Note that we let each entry have a unique "name" and "text" string,
812  * even if the string happens to be empty (everyone has a unique '\0').
813  */
814 errr init_v_info(void)
815 {
816         /* Init the header */
817         init_header(&v_head, max_v_idx, sizeof(vault_type));
818
819 #ifdef ALLOW_TEMPLATES
820
821         /* Save a pointer to the parsing function */
822         v_head.parse_info_txt = parse_v_info;
823
824 #endif /* ALLOW_TEMPLATES */
825
826         return init_info("v_info", &v_head,
827                          (void*)&v_info, &v_name, &v_text, NULL);
828 }
829
830
831 /*
832  * Initialize the "s_info" array
833  */
834 static errr init_s_info(void)
835 {
836         /* Init the header */
837         init_header(&s_head, MAX_CLASS, sizeof(skill_table));
838
839 #ifdef ALLOW_TEMPLATES
840
841         /* Save a pointer to the parsing function */
842         s_head.parse_info_txt = parse_s_info;
843
844 #endif /* ALLOW_TEMPLATES */
845
846         return init_info("s_info", &s_head,
847                          (void*)&s_info, NULL, NULL, NULL);
848 }
849
850
851 /*
852  * Initialize the "m_info" array
853  */
854 static errr init_m_info(void)
855 {
856         /* Init the header */
857         init_header(&m_head, MAX_CLASS, sizeof(player_magic));
858
859 #ifdef ALLOW_TEMPLATES
860
861         /* Save a pointer to the parsing function */
862         m_head.parse_info_txt = parse_m_info;
863
864 #endif /* ALLOW_TEMPLATES */
865
866         return init_info("m_info", &m_head,
867                          (void*)&m_info, NULL, NULL, NULL);
868 }
869
870
871
872 /*** Initialize others ***/
873
874 /*
875  * Hack -- Objects sold in the stores -- by tval/sval pair.
876  */
877 static byte store_table[MAX_STORES][STORE_CHOICES][2] =
878 {
879         {
880                 /* General Store */
881
882                 { TV_FOOD, SV_FOOD_RATION },
883                 { TV_FOOD, SV_FOOD_RATION },
884                 { TV_FOOD, SV_FOOD_RATION },
885                 { TV_FOOD, SV_FOOD_RATION },
886
887                 { TV_FOOD, SV_FOOD_RATION },
888                 { TV_FOOD, SV_FOOD_BISCUIT },
889                 { TV_FOOD, SV_FOOD_JERKY },
890                 { TV_FOOD, SV_FOOD_JERKY },
891
892                 { TV_FOOD, SV_FOOD_PINT_OF_WINE },
893                 { TV_FOOD, SV_FOOD_PINT_OF_ALE },
894                 { TV_LITE, SV_LITE_TORCH },
895                 { TV_LITE, SV_LITE_TORCH },
896
897                 { TV_LITE, SV_LITE_TORCH },
898                 { TV_LITE, SV_LITE_TORCH },
899                 { TV_LITE, SV_LITE_LANTERN },
900                 { TV_LITE, SV_LITE_LANTERN },
901
902                 { TV_FLASK, 0 },
903                 { TV_FLASK, 0 },
904                 { TV_FLASK, 0 },
905                 { TV_FLASK, 0 },
906
907                 { TV_FLASK, 0 },
908                 { TV_FLASK, 0 },
909                 { TV_SPIKE, 0 },
910                 { TV_SPIKE, 0 },
911
912                 { TV_SHOT, SV_AMMO_NORMAL },
913                 { TV_ARROW, SV_AMMO_NORMAL },
914                 { TV_BOLT, SV_AMMO_NORMAL },
915                 { TV_DIGGING, SV_SHOVEL },
916
917                 { TV_DIGGING, SV_PICK },
918                 { TV_CLOAK, SV_CLOAK },
919                 { TV_CLOAK, SV_CLOAK },
920                 { TV_CLOAK, SV_FUR_CLOAK },
921
922                 { TV_FOOD, SV_FOOD_RATION },
923                 { TV_FOOD, SV_FOOD_RATION },
924                 { TV_FOOD, SV_FOOD_RATION },
925                 { TV_FOOD, SV_FOOD_RATION },
926
927                 { TV_POTION, SV_POTION_WATER },
928                 { TV_POTION, SV_POTION_WATER },
929                 { TV_LITE, SV_LITE_LANTERN },
930                 { TV_LITE, SV_LITE_LANTERN },
931
932                 { TV_FOOD, SV_FOOD_WAYBREAD },
933                 { TV_FOOD, SV_FOOD_WAYBREAD },
934                 { TV_CAPTURE, 0 },
935                 { TV_FIGURINE, 0 },
936
937                 { TV_SHOT, SV_AMMO_NORMAL },
938                 { TV_ARROW, SV_AMMO_NORMAL },
939                 { TV_BOLT, SV_AMMO_NORMAL },
940                 { TV_DIGGING, SV_SHOVEL }
941         },
942
943         {
944                 /* Armoury */
945
946                 { TV_BOOTS, SV_PAIR_OF_SOFT_LEATHER_BOOTS },
947                 { TV_BOOTS, SV_PAIR_OF_SOFT_LEATHER_BOOTS },
948                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
949                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
950
951                 { TV_HELM, SV_HARD_LEATHER_CAP },
952                 { TV_HELM, SV_HARD_LEATHER_CAP },
953                 { TV_HELM, SV_METAL_CAP },
954                 { TV_HELM, SV_IRON_HELM },
955
956                 { TV_SOFT_ARMOR, SV_ROBE },
957                 { TV_SOFT_ARMOR, SV_ROBE },
958                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
959                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
960
961                 { TV_SOFT_ARMOR, SV_HARD_LEATHER_ARMOR },
962                 { TV_SOFT_ARMOR, SV_HARD_LEATHER_ARMOR },
963                 { TV_SOFT_ARMOR, SV_HARD_STUDDED_LEATHER },
964                 { TV_SOFT_ARMOR, SV_HARD_STUDDED_LEATHER },
965
966                 { TV_SOFT_ARMOR, SV_RHINO_HIDE_ARMOR },
967                 { TV_SOFT_ARMOR, SV_LEATHER_SCALE_MAIL },
968                 { TV_HARD_ARMOR, SV_METAL_SCALE_MAIL },
969                 { TV_HARD_ARMOR, SV_CHAIN_MAIL },
970
971                 { TV_HARD_ARMOR, SV_DOUBLE_RING_MAIL },
972                 { TV_HARD_ARMOR, SV_AUGMENTED_CHAIN_MAIL },
973                 { TV_HARD_ARMOR, SV_BAR_CHAIN_MAIL },
974                 { TV_HARD_ARMOR, SV_DOUBLE_CHAIN_MAIL },
975
976                 { TV_HARD_ARMOR, SV_METAL_BRIGANDINE_ARMOUR },
977                 { TV_HARD_ARMOR, SV_SPLINT_MAIL },
978                 { TV_GLOVES, SV_SET_OF_LEATHER_GLOVES },
979                 { TV_GLOVES, SV_SET_OF_LEATHER_GLOVES },
980
981                 { TV_GLOVES, SV_SET_OF_GAUNTLETS },
982                 { TV_SHIELD, SV_SMALL_LEATHER_SHIELD },
983                 { TV_SHIELD, SV_LARGE_LEATHER_SHIELD },
984                 { TV_SHIELD, SV_SMALL_METAL_SHIELD },
985
986                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
987                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
988                 { TV_HELM, SV_HARD_LEATHER_CAP },
989                 { TV_HELM, SV_HARD_LEATHER_CAP },
990
991                 { TV_SOFT_ARMOR, SV_ROBE },
992                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
993                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
994                 { TV_SOFT_ARMOR, SV_HARD_LEATHER_ARMOR },
995
996                 { TV_SOFT_ARMOR, SV_LEATHER_JACK },
997                 { TV_HARD_ARMOR, SV_METAL_SCALE_MAIL },
998                 { TV_HARD_ARMOR, SV_CHAIN_MAIL },
999                 { TV_HARD_ARMOR, SV_CHAIN_MAIL },
1000
1001                 { TV_GLOVES, SV_SET_OF_LEATHER_GLOVES },
1002                 { TV_GLOVES, SV_SET_OF_GAUNTLETS },
1003                 { TV_SHIELD, SV_SMALL_LEATHER_SHIELD },
1004                 { TV_SHIELD, SV_SMALL_LEATHER_SHIELD }
1005         },
1006
1007         {
1008                 /* Weaponsmith */
1009
1010                 { TV_SWORD, SV_DAGGER },
1011                 { TV_SWORD, SV_MAIN_GAUCHE },
1012                 { TV_SWORD, SV_RAPIER },
1013                 { TV_SWORD, SV_SMALL_SWORD },
1014
1015                 { TV_SWORD, SV_SHORT_SWORD },
1016                 { TV_SWORD, SV_SABRE },
1017                 { TV_SWORD, SV_CUTLASS },
1018                 { TV_SWORD, SV_TULWAR },
1019
1020                 { TV_SWORD, SV_BROAD_SWORD },
1021                 { TV_SWORD, SV_LONG_SWORD },
1022                 { TV_SWORD, SV_SCIMITAR },
1023                 { TV_SWORD, SV_KATANA },
1024
1025                 { TV_SWORD, SV_BASTARD_SWORD },
1026                 { TV_POLEARM, SV_SPEAR },
1027                 { TV_POLEARM, SV_AWL_PIKE },
1028                 { TV_POLEARM, SV_TRIDENT },
1029
1030                 { TV_POLEARM, SV_PIKE },
1031                 { TV_POLEARM, SV_BEAKED_AXE },
1032                 { TV_POLEARM, SV_BROAD_AXE },
1033                 { TV_POLEARM, SV_LANCE },
1034
1035                 { TV_POLEARM, SV_BATTLE_AXE },
1036                 { TV_POLEARM, SV_HATCHET },
1037                 { TV_BOW, SV_SLING },
1038                 { TV_BOW, SV_SHORT_BOW },
1039
1040                 { TV_BOW, SV_LIGHT_XBOW },
1041                 { TV_SHOT, SV_AMMO_NORMAL },
1042                 { TV_SHOT, SV_AMMO_NORMAL },
1043                 { TV_ARROW, SV_AMMO_NORMAL },
1044
1045                 { TV_ARROW, SV_AMMO_NORMAL },
1046                 { TV_BOLT, SV_AMMO_NORMAL },
1047                 { TV_BOLT, SV_AMMO_NORMAL },
1048                 { TV_BOW, SV_LIGHT_XBOW },
1049
1050                 { TV_ARROW, SV_AMMO_NORMAL },
1051                 { TV_BOLT, SV_AMMO_NORMAL },
1052                 { TV_BOW, SV_SHORT_BOW },
1053                 { TV_BOW, SV_LIGHT_XBOW },
1054
1055                 { TV_SWORD, SV_DAGGER },
1056                 { TV_SWORD, SV_TANTO },
1057                 { TV_SWORD, SV_RAPIER },
1058                 { TV_SWORD, SV_SMALL_SWORD },
1059
1060                 { TV_SWORD, SV_SHORT_SWORD },
1061                 { TV_SWORD, SV_LONG_SWORD },
1062                 { TV_SWORD, SV_SCIMITAR },
1063                 { TV_SWORD, SV_BROAD_SWORD },
1064
1065                 { TV_HISSATSU_BOOK, 0 },
1066                 { TV_HISSATSU_BOOK, 0 },
1067                 { TV_HISSATSU_BOOK, 1 },
1068                 { TV_HISSATSU_BOOK, 1 },
1069         },
1070
1071         {
1072                 /* Temple */
1073
1074                 { TV_HAFTED, SV_NUNCHAKU },
1075                 { TV_HAFTED, SV_QUARTERSTAFF },
1076                 { TV_HAFTED, SV_MACE },
1077                 { TV_HAFTED, SV_BO_STAFF },
1078
1079                 { TV_HAFTED, SV_WAR_HAMMER },
1080                 { TV_HAFTED, SV_WAR_HAMMER },
1081                 { TV_HAFTED, SV_MORNING_STAR },
1082                 { TV_HAFTED, SV_FLAIL },
1083
1084                 { TV_HAFTED, SV_LEAD_FILLED_MACE },
1085                 { TV_SCROLL, SV_SCROLL_REMOVE_CURSE },
1086                 { TV_SCROLL, SV_SCROLL_BLESSING },
1087                 { TV_SCROLL, SV_SCROLL_HOLY_CHANT },
1088
1089                 { TV_POTION, SV_POTION_HEROISM },
1090                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1091                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1092                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1093
1094                 { TV_POTION, SV_POTION_CURE_LIGHT },
1095                 { TV_POTION, SV_POTION_CURE_SERIOUS },
1096                 { TV_POTION, SV_POTION_CURE_SERIOUS },
1097                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1098
1099                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1100                 { TV_POTION, SV_POTION_RESTORE_EXP },
1101                 { TV_POTION, SV_POTION_RESTORE_EXP },
1102                 { TV_POTION, SV_POTION_RESTORE_EXP },
1103
1104                 { TV_LIFE_BOOK, 0 },
1105                 { TV_LIFE_BOOK, 0 },
1106                 { TV_LIFE_BOOK, 1 },
1107                 { TV_LIFE_BOOK, 1 },
1108
1109                 { TV_CRUSADE_BOOK, 0 },
1110                 { TV_CRUSADE_BOOK, 0 },
1111                 { TV_CRUSADE_BOOK, 1 },
1112                 { TV_CRUSADE_BOOK, 1 },
1113
1114                 { TV_HAFTED, SV_WHIP },
1115                 { TV_HAFTED, SV_MACE },
1116                 { TV_HAFTED, SV_BALL_AND_CHAIN },
1117                 { TV_HAFTED, SV_WAR_HAMMER },
1118
1119                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1120                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1121                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1122                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1123
1124                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1125                 { TV_POTION, SV_POTION_RESTORE_EXP },
1126
1127                 { TV_FIGURINE, 0 },
1128                 { TV_STATUE, SV_ANY },
1129
1130                 { TV_SCROLL, SV_SCROLL_REMOVE_CURSE },
1131                 { TV_SCROLL, SV_SCROLL_REMOVE_CURSE },
1132                 { TV_SCROLL, SV_SCROLL_STAR_REMOVE_CURSE },
1133                 { TV_SCROLL, SV_SCROLL_STAR_REMOVE_CURSE }
1134         },
1135
1136         {
1137                 /* Alchemy shop */
1138
1139                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_HIT },
1140                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_DAM },
1141                 { TV_SCROLL, SV_SCROLL_ENCHANT_ARMOR },
1142                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1143
1144                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1145                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1146                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1147                 { TV_SCROLL, SV_SCROLL_LIGHT },
1148
1149                 { TV_SCROLL, SV_SCROLL_PHASE_DOOR },
1150                 { TV_SCROLL, SV_SCROLL_PHASE_DOOR },
1151                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1152                 { TV_SCROLL, SV_SCROLL_MONSTER_CONFUSION },
1153
1154                 { TV_SCROLL, SV_SCROLL_MAPPING },
1155                 { TV_SCROLL, SV_SCROLL_DETECT_GOLD },
1156                 { TV_SCROLL, SV_SCROLL_DETECT_ITEM },
1157                 { TV_SCROLL, SV_SCROLL_DETECT_TRAP },
1158
1159                 { TV_SCROLL, SV_SCROLL_DETECT_INVIS },
1160                 { TV_SCROLL, SV_SCROLL_RECHARGING },
1161                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1162                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1163
1164                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1165                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1166                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1167                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1168
1169                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1170                 { TV_POTION, SV_POTION_RES_STR },
1171                 { TV_POTION, SV_POTION_RES_INT },
1172                 { TV_POTION, SV_POTION_RES_WIS },
1173
1174                 { TV_POTION, SV_POTION_RES_DEX },
1175                 { TV_POTION, SV_POTION_RES_CON },
1176                 { TV_POTION, SV_POTION_RES_CHR },
1177                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1178
1179                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1180                 { TV_SCROLL, SV_SCROLL_STAR_IDENTIFY },  /* Yep, occasionally! */
1181                 { TV_SCROLL, SV_SCROLL_STAR_IDENTIFY },
1182                 { TV_SCROLL, SV_SCROLL_LIGHT },
1183
1184                 { TV_POTION, SV_POTION_RES_STR },
1185                 { TV_POTION, SV_POTION_RES_INT },
1186                 { TV_POTION, SV_POTION_RES_WIS },
1187                 { TV_POTION, SV_POTION_RES_DEX },
1188
1189                 { TV_POTION, SV_POTION_RES_CON },
1190                 { TV_POTION, SV_POTION_RES_CHR },
1191                 { TV_SCROLL, SV_SCROLL_ENCHANT_ARMOR },
1192                 { TV_SCROLL, SV_SCROLL_ENCHANT_ARMOR },
1193
1194                 { TV_SCROLL, SV_SCROLL_RECHARGING },
1195                 { TV_SCROLL, SV_SCROLL_PHASE_DOOR },
1196                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_HIT },
1197                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_DAM },
1198
1199         },
1200
1201         {
1202                 /* Magic-User store */
1203
1204                 { TV_RING, SV_RING_PROTECTION },
1205                 { TV_RING, SV_RING_LEVITATION_FALL },
1206                 { TV_RING, SV_RING_PROTECTION },
1207                 { TV_RING, SV_RING_RESIST_FIRE },
1208
1209                 { TV_RING, SV_RING_RESIST_COLD },
1210                 { TV_AMULET, SV_AMULET_CHARISMA },
1211                 { TV_RING, SV_RING_WARNING },
1212                 { TV_AMULET, SV_AMULET_RESIST_ACID },
1213
1214                 { TV_AMULET, SV_AMULET_SEARCHING },
1215                 { TV_WAND, SV_WAND_SLOW_MONSTER },
1216                 { TV_WAND, SV_WAND_CONFUSE_MONSTER },
1217                 { TV_WAND, SV_WAND_SLEEP_MONSTER },
1218
1219                 { TV_WAND, SV_WAND_MAGIC_MISSILE },
1220                 { TV_WAND, SV_WAND_STINKING_CLOUD },
1221                 { TV_WAND, SV_WAND_WONDER },
1222                 { TV_WAND, SV_WAND_DISARMING },
1223
1224                 { TV_STAFF, SV_STAFF_LITE },
1225                 { TV_STAFF, SV_STAFF_MAPPING },
1226                 { TV_STAFF, SV_STAFF_DETECT_TRAP },
1227                 { TV_STAFF, SV_STAFF_DETECT_DOOR },
1228
1229                 { TV_STAFF, SV_STAFF_DETECT_GOLD },
1230                 { TV_STAFF, SV_STAFF_DETECT_ITEM },
1231                 { TV_STAFF, SV_STAFF_DETECT_INVIS },
1232                 { TV_STAFF, SV_STAFF_DETECT_EVIL },
1233
1234                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1235                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1236                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1237                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1238
1239                 { TV_STAFF, SV_STAFF_IDENTIFY },
1240                 { TV_STAFF, SV_STAFF_IDENTIFY },
1241                 { TV_STAFF, SV_STAFF_IDENTIFY },
1242
1243                 { TV_STAFF, SV_STAFF_IDENTIFY },
1244                 { TV_STAFF, SV_STAFF_REMOVE_CURSE },
1245                 { TV_STAFF, SV_STAFF_CURE_LIGHT },
1246                 { TV_STAFF, SV_STAFF_PROBING },
1247
1248                 { TV_FIGURINE, 0 },
1249
1250                 { TV_SORCERY_BOOK, 0 },
1251                 { TV_SORCERY_BOOK, 0 },
1252                 { TV_SORCERY_BOOK, 1 },
1253                 { TV_SORCERY_BOOK, 1 },
1254
1255                 { TV_ARCANE_BOOK, 0 },
1256                 { TV_ARCANE_BOOK, 0 },
1257                 { TV_ARCANE_BOOK, 1 },
1258                 { TV_ARCANE_BOOK, 1 },
1259
1260                 { TV_ARCANE_BOOK, 2 },
1261                 { TV_ARCANE_BOOK, 2 },
1262                 { TV_ARCANE_BOOK, 3 },
1263                 { TV_ARCANE_BOOK, 3 },
1264
1265         },
1266
1267         {
1268                 /* Black Market (unused) */
1269                 { 0, 0 },
1270                 { 0, 0 },
1271                 { 0, 0 },
1272                 { 0, 0 },
1273                 { 0, 0 },
1274                 { 0, 0 },
1275                 { 0, 0 },
1276                 { 0, 0 },
1277                 { 0, 0 },
1278                 { 0, 0 },
1279                 { 0, 0 },
1280                 { 0, 0 },
1281                 { 0, 0 },
1282                 { 0, 0 },
1283                 { 0, 0 },
1284                 { 0, 0 },
1285                 { 0, 0 },
1286                 { 0, 0 },
1287                 { 0, 0 },
1288                 { 0, 0 },
1289                 { 0, 0 },
1290                 { 0, 0 },
1291                 { 0, 0 },
1292                 { 0, 0 },
1293                 { 0, 0 },
1294                 { 0, 0 },
1295                 { 0, 0 },
1296                 { 0, 0 },
1297                 { 0, 0 },
1298                 { 0, 0 },
1299                 { 0, 0 },
1300                 { 0, 0 }
1301         },
1302
1303         {
1304                 /* Home (unused) */
1305                 { 0, 0 },
1306                 { 0, 0 },
1307                 { 0, 0 },
1308                 { 0, 0 },
1309                 { 0, 0 },
1310                 { 0, 0 },
1311                 { 0, 0 },
1312                 { 0, 0 },
1313                 { 0, 0 },
1314                 { 0, 0 },
1315                 { 0, 0 },
1316                 { 0, 0 },
1317                 { 0, 0 },
1318                 { 0, 0 },
1319                 { 0, 0 },
1320                 { 0, 0 },
1321                 { 0, 0 },
1322                 { 0, 0 },
1323                 { 0, 0 },
1324                 { 0, 0 },
1325                 { 0, 0 },
1326                 { 0, 0 },
1327                 { 0, 0 },
1328                 { 0, 0 },
1329                 { 0, 0 },
1330                 { 0, 0 },
1331                 { 0, 0 },
1332                 { 0, 0 },
1333                 { 0, 0 },
1334                 { 0, 0 },
1335                 { 0, 0 },
1336                 { 0, 0 }
1337         },
1338
1339         {
1340                 /* Bookstore */
1341                 { TV_SORCERY_BOOK, 0 },
1342                 { TV_SORCERY_BOOK, 0 },
1343                 { TV_SORCERY_BOOK, 1 },
1344                 { TV_SORCERY_BOOK, 1 },
1345
1346                 { TV_NATURE_BOOK, 0 },
1347                 { TV_NATURE_BOOK, 0 },
1348                 { TV_NATURE_BOOK, 1 },
1349                 { TV_NATURE_BOOK, 1 },
1350
1351                 { TV_CHAOS_BOOK, 0 },
1352                 { TV_CHAOS_BOOK, 0 },
1353                 { TV_CHAOS_BOOK, 1 },
1354                 { TV_CHAOS_BOOK, 1 },
1355
1356                 { TV_DEATH_BOOK, 0 },
1357                 { TV_DEATH_BOOK, 0 },
1358                 { TV_DEATH_BOOK, 1 },
1359                 { TV_DEATH_BOOK, 1 },
1360
1361                 { TV_TRUMP_BOOK, 0 },           /* +16 */
1362                 { TV_TRUMP_BOOK, 0 },
1363                 { TV_TRUMP_BOOK, 1 },
1364                 { TV_TRUMP_BOOK, 1 },
1365
1366                 { TV_ARCANE_BOOK, 0 },
1367                 { TV_ARCANE_BOOK, 1 },
1368                 { TV_ARCANE_BOOK, 2 },
1369                 { TV_ARCANE_BOOK, 3 },
1370
1371                 { TV_CRAFT_BOOK, 0 },
1372                 { TV_CRAFT_BOOK, 0 },
1373                 { TV_CRAFT_BOOK, 1 },
1374                 { TV_CRAFT_BOOK, 1 },
1375
1376                 { TV_DAEMON_BOOK, 0 },
1377                 { TV_DAEMON_BOOK, 0 },
1378                 { TV_DAEMON_BOOK, 1 },
1379                 { TV_DAEMON_BOOK, 1 },
1380
1381                 { TV_MUSIC_BOOK, 0 },
1382                 { TV_MUSIC_BOOK, 0 },
1383                 { TV_MUSIC_BOOK, 1 },
1384                 { TV_MUSIC_BOOK, 1 },
1385         },
1386
1387         {
1388                 /* Museum (unused) */
1389                 { 0, 0 },
1390                 { 0, 0 },
1391                 { 0, 0 },
1392                 { 0, 0 },
1393                 { 0, 0 },
1394                 { 0, 0 },
1395                 { 0, 0 },
1396                 { 0, 0 },
1397                 { 0, 0 },
1398                 { 0, 0 },
1399                 { 0, 0 },
1400                 { 0, 0 },
1401                 { 0, 0 },
1402                 { 0, 0 },
1403                 { 0, 0 },
1404                 { 0, 0 },
1405                 { 0, 0 },
1406                 { 0, 0 },
1407                 { 0, 0 },
1408                 { 0, 0 },
1409                 { 0, 0 },
1410                 { 0, 0 },
1411                 { 0, 0 },
1412                 { 0, 0 },
1413                 { 0, 0 },
1414                 { 0, 0 },
1415                 { 0, 0 },
1416                 { 0, 0 },
1417                 { 0, 0 },
1418                 { 0, 0 },
1419                 { 0, 0 },
1420                 { 0, 0 }
1421         }
1422 };
1423
1424
1425 /*
1426  * Initialize misc. values
1427  */
1428 static errr init_misc(void)
1429 {
1430         /* Initialize the values */
1431         process_dungeon_file("misc.txt", 0, 0, 0, 0);
1432
1433         return 0;
1434 }
1435
1436
1437 /*
1438  * Initialize town array
1439  */
1440 static errr init_towns(void)
1441 {
1442         int i, j, k;
1443
1444         /*** Prepare the Towns ***/
1445
1446         /* Allocate the towns */
1447         C_MAKE(town, max_towns, town_type);
1448
1449         for (i = 1; i < max_towns; i++)
1450         {
1451                 /*** Prepare the Stores ***/
1452
1453                 /* Allocate the stores */
1454                 C_MAKE(town[i].store, MAX_STORES, store_type);
1455
1456                 /* Fill in each store */
1457                 for (j = 0; j < MAX_STORES; j++)
1458                 {
1459                         /* Access the store */
1460                         store_type *st_ptr = &town[i].store[j];
1461
1462                         if ((i > 1) && (j == STORE_MUSEUM || j == STORE_HOME)) continue;
1463
1464                         /* Assume full stock */
1465
1466                 /*
1467                  * ²æ¤¬²È¤¬ 20 ¥Ú¡¼¥¸¤Þ¤Ç»È¤¨¤ë±£¤·µ¡Ç½¤Î¤¿¤á¤Î½àÈ÷¡£
1468                  * ¥ª¥×¥·¥ç¥ó¤¬Í­¸ú¤Ç¤â¤½¤¦¤Ç¤Ê¤¯¤Æ¤â°ì±þ¥¹¥Ú¡¼¥¹
1469                  * ¤òºî¤Ã¤Æ¤ª¤¯¡£
1470                  */
1471                 if (j == STORE_HOME)
1472                 {
1473                         st_ptr->stock_size = (STORE_INVEN_MAX * 10);
1474                 }
1475                 else if (j == STORE_MUSEUM)
1476                 {
1477                         st_ptr->stock_size = (STORE_INVEN_MAX * 50);
1478                 }
1479                 else
1480                 {
1481                         st_ptr->stock_size = STORE_INVEN_MAX;
1482                 }
1483
1484
1485                         /* Allocate the stock */
1486                         C_MAKE(st_ptr->stock, st_ptr->stock_size, object_type);
1487
1488                         /* No table for the black market or home */
1489                         if ((j == STORE_BLACK) || (j == STORE_HOME) || (j == STORE_MUSEUM)) continue;
1490
1491                         /* Assume full table */
1492                         st_ptr->table_size = STORE_CHOICES;
1493
1494                         /* Allocate the stock */
1495                         C_MAKE(st_ptr->table, st_ptr->table_size, s16b);
1496
1497                         /* Scan the choices */
1498                         for (k = 0; k < STORE_CHOICES; k++)
1499                         {
1500                                 int k_idx;
1501
1502                                 /* Extract the tval/sval codes */
1503                                 int tv = store_table[j][k][0];
1504                                 int sv = store_table[j][k][1];
1505
1506                                 /* Look for it */
1507                                 for (k_idx = 1; k_idx < max_k_idx; k_idx++)
1508                                 {
1509                                         object_kind *k_ptr = &k_info[k_idx];
1510
1511                                         /* Found a match */
1512                                         if ((k_ptr->tval == tv) && (k_ptr->sval == sv)) break;
1513                                 }
1514
1515                                 /* Catch errors */
1516                                 if (k_idx == max_k_idx) continue;
1517
1518                                 /* Add that item index to the table */
1519                                 st_ptr->table[st_ptr->table_num++] = k_idx;
1520                         }
1521                 }
1522         }
1523
1524         return 0;
1525 }
1526
1527 /*
1528  * Initialize buildings
1529  */
1530 errr init_buildings(void)
1531 {
1532         int i, j;
1533
1534         for (i = 0; i < MAX_BLDG; i++)
1535         {
1536                 building[i].name[0] = '\0';
1537                 building[i].owner_name[0] = '\0';
1538                 building[i].owner_race[0] = '\0';
1539
1540                 for (j = 0; j < 8; j++)
1541                 {
1542                         building[i].act_names[j][0] = '\0';
1543                         building[i].member_costs[j] = 0;
1544                         building[i].other_costs[j] = 0;
1545                         building[i].letters[j] = 0;
1546                         building[i].actions[j] = 0;
1547                         building[i].action_restr[j] = 0;
1548                 }
1549
1550                 for (j = 0; j < MAX_CLASS; j++)
1551                 {
1552                         building[i].member_class[j] = 0;
1553                 }
1554
1555                 for (j = 0; j < MAX_RACES; j++)
1556                 {
1557                         building[i].member_race[j] = 0;
1558                 }
1559
1560                 for (j = 0; j < MAX_MAGIC+1; j++)
1561                 {
1562                         building[i].member_realm[j] = 0;
1563                 }
1564         }
1565
1566         return (0);
1567 }
1568
1569
1570 /*
1571  * Initialize quest array
1572  */
1573 static errr init_quests(void)
1574 {
1575         int i;
1576
1577         /*** Prepare the quests ***/
1578
1579         /* Allocate the quests */
1580         C_MAKE(quest, max_quests, quest_type);
1581
1582         /* Set all quest to "untaken" */
1583         for (i = 0; i < max_quests; i++)
1584         {
1585                 quest[i].status = QUEST_STATUS_UNTAKEN;
1586         }
1587
1588         return 0;
1589 }
1590
1591
1592 /*
1593  * Initialize some other arrays
1594  */
1595 static errr init_other(void)
1596 {
1597         int i, n;
1598
1599
1600         /*** Prepare the "dungeon" information ***/
1601
1602         /* Allocate and Wipe the object list */
1603         C_MAKE(o_list, max_o_idx, object_type);
1604
1605         /* Allocate and Wipe the monster list */
1606         C_MAKE(m_list, max_m_idx, monster_type);
1607
1608         /* Allocate and Wipe the monster process list */
1609         for (i = 0; i < MAX_MTIMED; i++)
1610         {
1611                 C_MAKE(mproc_list[i], max_m_idx, s16b);
1612         }
1613
1614         /* Allocate and Wipe the max dungeon level */
1615         C_MAKE(max_dlv, max_d_idx, s16b);
1616
1617         /* Allocate and wipe each line of the cave */
1618         for (i = 0; i < MAX_HGT; i++)
1619         {
1620                 /* Allocate one row of the cave */
1621                 C_MAKE(cave[i], MAX_WID, cave_type);
1622         }
1623
1624
1625         /*** Prepare the various "bizarre" arrays ***/
1626
1627         /* Macro variables */
1628         C_MAKE(macro__pat, MACRO_MAX, cptr);
1629         C_MAKE(macro__act, MACRO_MAX, cptr);
1630         C_MAKE(macro__cmd, MACRO_MAX, bool);
1631
1632         /* Macro action buffer */
1633         C_MAKE(macro__buf, 1024, char);
1634
1635         /* Quark variables */
1636         quark_init();
1637
1638         /* Message variables */
1639         C_MAKE(message__ptr, MESSAGE_MAX, u16b);
1640         C_MAKE(message__buf, MESSAGE_BUF, char);
1641
1642         /* Hack -- No messages yet */
1643         message__tail = MESSAGE_BUF;
1644
1645
1646         /*** Prepare the Player inventory ***/
1647
1648         /* Allocate it */
1649         C_MAKE(inventory, INVEN_TOTAL, object_type);
1650
1651
1652         /*** Prepare the options ***/
1653
1654         /* Scan the options */
1655         for (i = 0; option_info[i].o_desc; i++)
1656         {
1657                 int os = option_info[i].o_set;
1658                 int ob = option_info[i].o_bit;
1659
1660                 /* Set the "default" options */
1661                 if (option_info[i].o_var)
1662                 {
1663                         /* Accept */
1664                         option_mask[os] |= (1L << ob);
1665
1666                         /* Set */
1667                         if (option_info[i].o_norm)
1668                         {
1669                                 /* Set */
1670                                 option_flag[os] |= (1L << ob);
1671                         }
1672
1673                         /* Clear */
1674                         else
1675                         {
1676                                 /* Clear */
1677                                 option_flag[os] &= ~(1L << ob);
1678                         }
1679                 }
1680         }
1681
1682         /* Analyze the windows */
1683         for (n = 0; n < 8; n++)
1684         {
1685                 /* Analyze the options */
1686                 for (i = 0; i < 32; i++)
1687                 {
1688                         /* Accept */
1689                         if (window_flag_desc[i])
1690                         {
1691                                 /* Accept */
1692                                 window_mask[n] |= (1L << i);
1693                         }
1694                 }
1695         }
1696
1697         /*
1698          *  Set the "default" window flags
1699          *  Window 1 : Display messages
1700          *  Window 2 : Display inven/equip
1701          */
1702         window_flag[1] = 1L << 6;
1703         window_flag[2] = 1L << 0;
1704
1705
1706         /*** Pre-allocate space for the "format()" buffer ***/
1707
1708         /* Hack -- Just call the "format()" function */
1709         (void)format("%s (%s).", "Mr.Hoge", MAINTAINER);
1710
1711
1712         /* Success */
1713         return (0);
1714 }
1715
1716
1717 /*
1718  * Initialize some other arrays
1719  */
1720 static errr init_object_alloc(void)
1721 {
1722         int i, j;
1723         object_kind *k_ptr;
1724         alloc_entry *table;
1725         s16b num[MAX_DEPTH];
1726         s16b aux[MAX_DEPTH];
1727
1728
1729         /*** Analyze object allocation info ***/
1730
1731         /* Clear the "aux" array */
1732         (void)C_WIPE(&aux, MAX_DEPTH, s16b);
1733
1734         /* Clear the "num" array */
1735         (void)C_WIPE(&num, MAX_DEPTH, s16b);
1736
1737         /* Free the old "alloc_kind_table" (if it exists) */
1738         if (alloc_kind_table)
1739         {
1740                 C_KILL(alloc_kind_table, alloc_kind_size, alloc_entry);
1741         }
1742
1743         /* Size of "alloc_kind_table" */
1744         alloc_kind_size = 0;
1745
1746         /* Scan the objects */
1747         for (i = 1; i < max_k_idx; i++)
1748         {
1749                 k_ptr = &k_info[i];
1750
1751                 /* Scan allocation pairs */
1752                 for (j = 0; j < 4; j++)
1753                 {
1754                         /* Count the "legal" entries */
1755                         if (k_ptr->chance[j])
1756                         {
1757                                 /* Count the entries */
1758                                 alloc_kind_size++;
1759
1760                                 /* Group by level */
1761                                 num[k_ptr->locale[j]]++;
1762                         }
1763                 }
1764         }
1765
1766         /* Collect the level indexes */
1767         for (i = 1; i < MAX_DEPTH; i++)
1768         {
1769                 /* Group by level */
1770                 num[i] += num[i-1];
1771         }
1772
1773         /* Paranoia */
1774 #ifdef JP
1775 if (!num[0]) quit("Ä®¤Î¥¢¥¤¥Æ¥à¤¬¤Ê¤¤¡ª");
1776 #else
1777         if (!num[0]) quit("No town objects!");
1778 #endif
1779
1780
1781
1782         /*** Initialize object allocation info ***/
1783
1784         /* Allocate the alloc_kind_table */
1785         C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry);
1786
1787         /* Access the table entry */
1788         table = alloc_kind_table;
1789
1790         /* Scan the objects */
1791         for (i = 1; i < max_k_idx; i++)
1792         {
1793                 k_ptr = &k_info[i];
1794
1795                 /* Scan allocation pairs */
1796                 for (j = 0; j < 4; j++)
1797                 {
1798                         /* Count the "legal" entries */
1799                         if (k_ptr->chance[j])
1800                         {
1801                                 int p, x, y, z;
1802
1803                                 /* Extract the base level */
1804                                 x = k_ptr->locale[j];
1805
1806                                 /* Extract the base probability */
1807                                 p = (100 / k_ptr->chance[j]);
1808
1809                                 /* Skip entries preceding our locale */
1810                                 y = (x > 0) ? num[x-1] : 0;
1811
1812                                 /* Skip previous entries at this locale */
1813                                 z = y + aux[x];
1814
1815                                 /* Load the entry */
1816                                 table[z].index = i;
1817                                 table[z].level = x;
1818                                 table[z].prob1 = p;
1819                                 table[z].prob2 = p;
1820                                 table[z].prob3 = p;
1821
1822                                 /* Another entry complete for this locale */
1823                                 aux[x]++;
1824                         }
1825                 }
1826         }
1827
1828         /* Success */
1829         return (0);
1830 }
1831
1832
1833 /*
1834  * Initialize some other arrays
1835  */
1836 static errr init_alloc(void)
1837 {
1838         int i;
1839         monster_race *r_ptr;
1840
1841 #ifdef SORT_R_INFO
1842
1843         tag_type *elements;
1844
1845         /* Allocate the "r_info" array */
1846         C_MAKE(elements, max_r_idx, tag_type);
1847
1848         /* Scan the monsters */
1849         for (i = 1; i < max_r_idx; i++)
1850         {
1851                 elements[i].tag = r_info[i].level;
1852                 elements[i].pointer = (void*)i;
1853         }
1854
1855         tag_sort(elements, max_r_idx);
1856
1857         /*** Initialize monster allocation info ***/
1858
1859         /* Size of "alloc_race_table" */
1860         alloc_race_size = max_r_idx;
1861
1862         /* Allocate the alloc_race_table */
1863         C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
1864
1865         /* Scan the monsters */
1866         for (i = 1; i < max_r_idx; i++)
1867         {
1868                 /* Get the i'th race */
1869                 r_ptr = &r_info[(int)elements[i].pointer];
1870
1871                 /* Count valid pairs */
1872                 if (r_ptr->rarity)
1873                 {
1874                         int p, x;
1875
1876                         /* Extract the base level */
1877                         x = r_ptr->level;
1878
1879                         /* Extract the base probability */
1880                         p = (100 / r_ptr->rarity);
1881
1882                         /* Load the entry */
1883                         alloc_race_table[i].index = (int)elements[i].pointer;
1884                         alloc_race_table[i].level = x;
1885                         alloc_race_table[i].prob1 = p;
1886                         alloc_race_table[i].prob2 = p;
1887                         alloc_race_table[i].prob3 = p;
1888                 }
1889         }
1890
1891         /* Free the "r_info" array */
1892         C_KILL(elements, max_r_idx, tag_type);
1893
1894 #else /* SORT_R_INFO */
1895
1896         int j;
1897         alloc_entry *table;
1898         s16b num[MAX_DEPTH];
1899         s16b aux[MAX_DEPTH];
1900
1901         /*** Analyze monster allocation info ***/
1902
1903         /* Clear the "aux" array */
1904         C_WIPE(&aux, MAX_DEPTH, s16b);
1905
1906         /* Clear the "num" array */
1907         C_WIPE(&num, MAX_DEPTH, s16b);
1908
1909         /* Size of "alloc_race_table" */
1910         alloc_race_size = 0;
1911
1912         /* Scan the monsters */
1913         for (i = 1; i < max_r_idx; i++)
1914         {
1915                 /* Get the i'th race */
1916                 r_ptr = &r_info[i];
1917
1918                 /* Legal monsters */
1919                 if (r_ptr->rarity)
1920                 {
1921                         /* Count the entries */
1922                         alloc_race_size++;
1923
1924                         /* Group by level */
1925                         num[r_ptr->level]++;
1926                 }
1927         }
1928
1929         /* Collect the level indexes */
1930         for (i = 1; i < MAX_DEPTH; i++)
1931         {
1932                 /* Group by level */
1933                 num[i] += num[i-1];
1934         }
1935
1936         /* Paranoia */
1937 #ifdef JP
1938         if (!num[0]) quit("Ä®¤Î¥â¥ó¥¹¥¿¡¼¤¬¤Ê¤¤¡ª");
1939 #else
1940         if (!num[0]) quit("No town monsters!");
1941 #endif
1942
1943
1944
1945         /*** Initialize monster allocation info ***/
1946
1947         /* Allocate the alloc_race_table */
1948         C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
1949
1950         /* Access the table entry */
1951         table = alloc_race_table;
1952
1953         /* Scan the monsters */
1954         for (i = 1; i < max_r_idx; i++)
1955         {
1956                 /* Get the i'th race */
1957                 r_ptr = &r_info[i];
1958
1959                 /* Count valid pairs */
1960                 if (r_ptr->rarity)
1961                 {
1962                         int p, x, y, z;
1963
1964                         /* Extract the base level */
1965                         x = r_ptr->level;
1966
1967                         /* Extract the base probability */
1968                         p = (100 / r_ptr->rarity);
1969
1970                         /* Skip entries preceding our locale */
1971                         y = (x > 0) ? num[x-1] : 0;
1972
1973                         /* Skip previous entries at this locale */
1974                         z = y + aux[x];
1975
1976                         /* Load the entry */
1977                         table[z].index = i;
1978                         table[z].level = x;
1979                         table[z].prob1 = p;
1980                         table[z].prob2 = p;
1981                         table[z].prob3 = p;
1982
1983                         /* Another entry complete for this locale */
1984                         aux[x]++;
1985                 }
1986         }
1987
1988 #endif /* SORT_R_INFO */
1989
1990         /* Init the "alloc_kind_table" */
1991         (void)init_object_alloc();
1992
1993         /* Success */
1994         return (0);
1995 }
1996
1997
1998
1999 /*
2000  * Hack -- take notes on line 23
2001  */
2002 static void note(cptr str)
2003 {
2004         Term_erase(0, 23, 255);
2005         Term_putstr(20, 23, -1, TERM_WHITE, str);
2006         Term_fresh();
2007 }
2008
2009
2010
2011 /*
2012  * Hack -- Explain a broken "lib" folder and quit (see below).
2013  *
2014  * XXX XXX XXX This function is "messy" because various things
2015  * may or may not be initialized, but the "plog()" and "quit()"
2016  * functions are "supposed" to work under any conditions.
2017  */
2018 static void init_angband_aux(cptr why)
2019 {
2020         /* Why */
2021         plog(why);
2022
2023 #ifdef JP
2024         /* Explain */
2025         plog("'lib'¥Ç¥£¥ì¥¯¥È¥ê¤¬Â¸ºß¤·¤Ê¤¤¤«²õ¤ì¤Æ¤¤¤ë¤è¤¦¤Ç¤¹¡£");
2026
2027         /* More details */
2028         plog("¤Ò¤ç¤Ã¤È¤¹¤ë¤È¥¢¡¼¥«¥¤¥Ö¤¬Àµ¤·¤¯²òÅव¤ì¤Æ¤¤¤Ê¤¤¤Î¤«¤â¤·¤ì¤Þ¤»¤ó¡£");
2029
2030         /* Explain */
2031         plog("³ºÅö¤¹¤ë'README'¥Õ¥¡¥¤¥ë¤òÆɤó¤Ç³Îǧ¤·¤Æ¤ß¤Æ²¼¤µ¤¤¡£");
2032
2033         /* Quit with error */
2034         quit("Ã×̿Ū¤Ê¥¨¥é¡¼¡£");
2035 #else
2036         /* Explain */
2037         plog("The 'lib' directory is probably missing or broken.");
2038
2039         /* More details */
2040         plog("Perhaps the archive was not extracted correctly.");
2041
2042         /* Explain */
2043         plog("See the 'README' file for more information.");
2044
2045         /* Quit with error */
2046         quit("Fatal Error.");
2047 #endif
2048
2049 }
2050
2051
2052 /*
2053  * Hack -- main Angband initialization entry point
2054  *
2055  * Verify some files, display the "news.txt" file, create
2056  * the high score file, initialize all internal arrays, and
2057  * load the basic "user pref files".
2058  *
2059  * Be very careful to keep track of the order in which things
2060  * are initialized, in particular, the only thing *known* to
2061  * be available when this function is called is the "z-term.c"
2062  * package, and that may not be fully initialized until the
2063  * end of this function, when the default "user pref files"
2064  * are loaded and "Term_xtra(TERM_XTRA_REACT,0)" is called.
2065  *
2066  * Note that this function attempts to verify the "news" file,
2067  * and the game aborts (cleanly) on failure, since without the
2068  * "news" file, it is likely that the "lib" folder has not been
2069  * correctly located.  Otherwise, the news file is displayed for
2070  * the user.
2071  *
2072  * Note that this function attempts to verify (or create) the
2073  * "high score" file, and the game aborts (cleanly) on failure,
2074  * since one of the most common "extraction" failures involves
2075  * failing to extract all sub-directories (even empty ones), such
2076  * as by failing to use the "-d" option of "pkunzip", or failing
2077  * to use the "save empty directories" option with "Compact Pro".
2078  * This error will often be caught by the "high score" creation
2079  * code below, since the "lib/apex" directory, being empty in the
2080  * standard distributions, is most likely to be "lost", making it
2081  * impossible to create the high score file.
2082  *
2083  * Note that various things are initialized by this function,
2084  * including everything that was once done by "init_some_arrays".
2085  *
2086  * This initialization involves the parsing of special files
2087  * in the "lib/data" and sometimes the "lib/edit" directories.
2088  *
2089  * Note that the "template" files are initialized first, since they
2090  * often contain errors.  This means that macros and message recall
2091  * and things like that are not available until after they are done.
2092  *
2093  * We load the default "user pref files" here in case any "color"
2094  * changes are needed before character creation.
2095  *
2096  * Note that the "graf-xxx.prf" file must be loaded separately,
2097  * if needed, in the first (?) pass through "TERM_XTRA_REACT".
2098  */
2099 void init_angband(void)
2100 {
2101         int fd = -1;
2102
2103         int mode = 0664;
2104
2105         FILE *fp;
2106
2107         char buf[1024];
2108
2109
2110         /*** Verify the "news" file ***/
2111
2112         /* Build the filename */
2113 #ifdef JP
2114         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news_j.txt");
2115 #else
2116         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news.txt");
2117 #endif
2118
2119
2120         /* Attempt to open the file */
2121         fd = fd_open(buf, O_RDONLY);
2122
2123         /* Failure */
2124         if (fd < 0)
2125         {
2126                 char why[1024];
2127
2128                 /* Message */
2129 #ifdef JP
2130         sprintf(why, "'%s'¥Õ¥¡¥¤¥ë¤Ë¥¢¥¯¥»¥¹¤Ç¤­¤Þ¤»¤ó!", buf);
2131 #else
2132                 sprintf(why, "Cannot access the '%s' file!", buf);
2133 #endif
2134
2135
2136                 /* Crash and burn */
2137                 init_angband_aux(why);
2138         }
2139
2140         /* Close it */
2141         (void)fd_close(fd);
2142
2143
2144         /*** Display the "news" file ***/
2145
2146         /* Clear screen */
2147         Term_clear();
2148
2149         /* Build the filename */
2150 #ifdef JP
2151         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news_j.txt");
2152 #else
2153         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news.txt");
2154 #endif
2155
2156
2157         /* Open the News file */
2158         fp = my_fopen(buf, "r");
2159
2160         /* Dump */
2161         if (fp)
2162         {
2163                 int i = 0;
2164
2165                 /* Dump the file to the screen */
2166                 while (0 == my_fgets(fp, buf, sizeof(buf)))
2167                 {
2168                         /* Display and advance */
2169                         Term_putstr(0, i++, -1, TERM_WHITE, buf);
2170                 }
2171
2172                 /* Close */
2173                 my_fclose(fp);
2174         }
2175
2176         /* Flush it */
2177         Term_fresh();
2178
2179
2180         /*** Verify (or create) the "high score" file ***/
2181
2182         /* Build the filename */
2183         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
2184
2185         /* Attempt to open the high score file */
2186         fd = fd_open(buf, O_RDONLY);
2187
2188         /* Failure */
2189         if (fd < 0)
2190         {
2191                 /* File type is "DATA" */
2192                 FILE_TYPE(FILE_TYPE_DATA);
2193
2194                 /* Grab permissions */
2195                 safe_setuid_grab();
2196
2197                 /* Create a new high score file */
2198                 fd = fd_make(buf, mode);
2199
2200                 /* Drop permissions */
2201                 safe_setuid_drop();
2202
2203                 /* Failure */
2204                 if (fd < 0)
2205                 {
2206                         char why[1024];
2207
2208                         /* Message */
2209 #ifdef JP
2210                         sprintf(why, "'%s'¥Õ¥¡¥¤¥ë¤òºîÀ®¤Ç¤­¤Þ¤»¤ó!", buf);
2211 #else
2212                         sprintf(why, "Cannot create the '%s' file!", buf);
2213 #endif
2214
2215
2216                         /* Crash and burn */
2217                         init_angband_aux(why);
2218                 }
2219         }
2220
2221         /* Close it */
2222         (void)fd_close(fd);
2223
2224
2225         /*** Initialize some arrays ***/
2226
2227         /* Initialize misc. values */
2228 #ifdef JP
2229 note("[ÊÑ¿ô¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹...(¤½¤Î¾)");
2230 #else
2231         note("[Initializing values... (misc)]");
2232 #endif
2233
2234 #ifdef JP
2235 if (init_misc()) quit("¤½¤Î¾¤ÎÊÑ¿ô¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó");
2236 #else
2237         if (init_misc()) quit("Cannot initialize misc. values");
2238 #endif
2239
2240
2241         /* Initialize feature info */
2242 #ifdef JP
2243         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (ÃÏ·Á)]");
2244         if (init_f_info()) quit("ÃÏ·Á½é´ü²½ÉÔǽ");
2245 #else
2246         note("[Initializing arrays... (features)]");
2247         if (init_f_info()) quit("Cannot initialize features");
2248 #endif
2249
2250
2251         /* Initialize object info */
2252 #ifdef JP
2253         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (¥¢¥¤¥Æ¥à)]");
2254         if (init_k_info()) quit("¥¢¥¤¥Æ¥à½é´ü²½ÉÔǽ");
2255 #else
2256         note("[Initializing arrays... (objects)]");
2257         if (init_k_info()) quit("Cannot initialize objects");
2258 #endif
2259
2260
2261         /* Initialize artifact info */
2262 #ifdef JP
2263         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (ÅÁÀâ¤Î¥¢¥¤¥Æ¥à)]");
2264         if (init_a_info()) quit("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à½é´ü²½ÉÔǽ");
2265 #else
2266         note("[Initializing arrays... (artifacts)]");
2267         if (init_a_info()) quit("Cannot initialize artifacts");
2268 #endif
2269
2270
2271         /* Initialize ego-item info */
2272 #ifdef JP
2273         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (̾¤Î¤¢¤ë¥¢¥¤¥Æ¥à)]");
2274         if (init_e_info()) quit("̾¤Î¤¢¤ë¥¢¥¤¥Æ¥à½é´ü²½ÉÔǽ");
2275 #else
2276         note("[Initializing arrays... (ego-items)]");
2277         if (init_e_info()) quit("Cannot initialize ego-items");
2278 #endif
2279
2280
2281         /* Initialize monster info */
2282 #ifdef JP
2283         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (¥â¥ó¥¹¥¿¡¼)]");
2284         if (init_r_info()) quit("¥â¥ó¥¹¥¿¡¼½é´ü²½ÉÔǽ");
2285 #else
2286         note("[Initializing arrays... (monsters)]");
2287         if (init_r_info()) quit("Cannot initialize monsters");
2288 #endif
2289
2290
2291         /* Initialize dungeon info */
2292 #ifdef JP
2293         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (¥À¥ó¥¸¥ç¥ó)]");
2294         if (init_d_info()) quit("¥À¥ó¥¸¥ç¥ó½é´ü²½ÉÔǽ");
2295 #else
2296         note("[Initializing arrays... (dungeon)]");
2297         if (init_d_info()) quit("Cannot initialize dungeon");
2298 #endif
2299         {
2300                 int i;
2301                 for (i = 1; i < max_d_idx; i++)
2302                         if (d_info[i].final_guardian)
2303                                 r_info[d_info[i].final_guardian].flags7 |= RF7_GUARDIAN;
2304         }
2305
2306         /* Initialize magic info */
2307 #ifdef JP
2308         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (ËâË¡)]");
2309         if (init_m_info()) quit("ËâË¡½é´ü²½ÉÔǽ");
2310 #else
2311         note("[Initializing arrays... (magic)]");
2312         if (init_m_info()) quit("Cannot initialize magic");
2313 #endif
2314
2315         /* Initialize weapon_exp info */
2316 #ifdef JP
2317         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (½ÏÎýÅÙ)]");
2318         if (init_s_info()) quit("½ÏÎýÅÙ½é´ü²½ÉÔǽ");
2319 #else
2320         note("[Initializing arrays... (skill)]");
2321         if (init_s_info()) quit("Cannot initialize skill");
2322 #endif
2323
2324         /* Initialize wilderness array */
2325 #ifdef JP
2326 note("[ÇÛÎó¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹... (¹ÓÌî)]");
2327 #else
2328         note("[Initializing arrays... (wilderness)]");
2329 #endif
2330
2331 #ifdef JP
2332 if (init_wilderness()) quit("¹ÓÌî¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó");
2333 #else
2334         if (init_wilderness()) quit("Cannot initialize wilderness");
2335 #endif
2336
2337
2338         /* Initialize town array */
2339 #ifdef JP
2340 note("[ÇÛÎó¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹... (³¹)]");
2341 #else
2342         note("[Initializing arrays... (towns)]");
2343 #endif
2344
2345 #ifdef JP
2346 if (init_towns()) quit("³¹¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó");
2347 #else
2348         if (init_towns()) quit("Cannot initialize towns");
2349 #endif
2350
2351
2352         /* Initialize building array */
2353 #ifdef JP
2354 note("[ÇÛÎó¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹... (·úʪ)]");
2355 #else
2356         note("[Initializing arrays... (buildings)]");
2357 #endif
2358
2359 #ifdef JP
2360 if (init_buildings()) quit("·úʪ¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó");
2361 #else
2362         if (init_buildings()) quit("Cannot initialize buildings");
2363 #endif
2364
2365
2366         /* Initialize quest array */
2367 #ifdef JP
2368 note("[ÇÛÎó¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹... (¥¯¥¨¥¹¥È)]");
2369 #else
2370         note("[Initializing arrays... (quests)]");
2371 #endif
2372
2373 #ifdef JP
2374 if (init_quests()) quit("¥¯¥¨¥¹¥È¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó");
2375 #else
2376         if (init_quests()) quit("Cannot initialize quests");
2377 #endif
2378
2379
2380         /* Initialize vault info */
2381 #ifdef JP
2382         if (init_v_info()) quit("vault ½é´ü²½ÉÔǽ");
2383 #else
2384         if (init_v_info()) quit("Cannot initialize vaults");
2385 #endif
2386
2387
2388         /* Initialize some other arrays */
2389 #ifdef JP
2390         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (¤½¤Î¾)]");
2391         if (init_other()) quit("¤½¤Î¾¤Î¥Ç¡¼¥¿½é´ü²½ÉÔǽ");
2392 #else
2393         note("[Initializing arrays... (other)]");
2394         if (init_other()) quit("Cannot initialize other stuff");
2395 #endif
2396
2397
2398         /* Initialize some other arrays */
2399 #ifdef JP
2400         /* translation */
2401         note("[¥Ç¡¼¥¿¤Î½é´ü²½Ãæ... (¥¢¥í¥±¡¼¥·¥ç¥ó)]");
2402         if (init_alloc()) quit("¥¢¥í¥±¡¼¥·¥ç¥ó¡¦¥¹¥¿¥Ã¥Õ½é´ü²½ÉÔǽ");
2403 #else
2404         note("[Initializing arrays... (alloc)]");
2405         if (init_alloc()) quit("Cannot initialize alloc stuff");
2406 #endif
2407
2408
2409
2410         /*** Load default user pref files ***/
2411
2412         /* Initialize feature info */
2413 #ifdef JP
2414 note("[¥æ¡¼¥¶¡¼ÀßÄê¥Õ¥¡¥¤¥ë¤ò½é´ü²½¤·¤Æ¤¤¤Þ¤¹...]");
2415 #else
2416         note("[Initializing user pref files...]");
2417 #endif
2418
2419
2420         /* Access the "basic" pref file */
2421         strcpy(buf, "pref.prf");
2422
2423         /* Process that file */
2424         process_pref_file(buf);
2425
2426         /* Access the "basic" system pref file */
2427         sprintf(buf, "pref-%s.prf", ANGBAND_SYS);
2428
2429         /* Process that file */
2430         process_pref_file(buf);
2431
2432         /* Done */
2433 #ifdef JP
2434         note("[½é´ü²½½ªÎ»]");
2435 #else
2436         note("[Initialization complete]");
2437 #endif
2438
2439 }
2440
2441 /*
2442  *  Get check sum in string form
2443  */
2444 cptr get_check_sum(void)
2445 {
2446         return format("%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
2447                       f_head.v_extra, 
2448                       k_head.v_extra, 
2449                       a_head.v_extra, 
2450                       e_head.v_extra, 
2451                       r_head.v_extra, 
2452                       d_head.v_extra, 
2453                       m_head.v_extra, 
2454                       s_head.v_extra, 
2455                       v_head.v_extra);
2456 }
2457