OSDN Git Service

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