OSDN Git Service

ソースコードのUTF-8化。
[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; /*!< Vault情報のヘッダ構造体 */
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  * @brief rawファイルからのデータの読み取り処理
353  * Initialize the "*_info" array, by parsing a binary "image" file
354  * @param fd ファイルディスクリプタ
355  * @param head rawファイルのヘッダ
356  * @return エラーコード
357  */
358 static errr init_info_raw(int fd, header *head)
359 {
360         header test;
361
362         /* Read and Verify the header */
363         if (fd_read(fd, (char*)(&test), sizeof(header)) ||
364             (test.v_major != head->v_major) ||
365             (test.v_minor != head->v_minor) ||
366             (test.v_patch != head->v_patch) ||
367             (test.info_num != head->info_num) ||
368             (test.info_len != head->info_len) ||
369             (test.head_size != head->head_size) ||
370             (test.info_size != head->info_size))
371         {
372                 /* Error */
373                 return (-1);
374         }
375
376
377         /* Accept the header */
378         (*head) = test;
379
380
381         /* Allocate the "*_info" array */
382         C_MAKE(head->info_ptr, head->info_size, char);
383
384         /* Read the "*_info" array */
385         fd_read(fd, head->info_ptr, head->info_size);
386
387
388         if (head->name_size)
389         {
390                 /* Allocate the "*_name" array */
391                 C_MAKE(head->name_ptr, head->name_size, char);
392
393                 /* Read the "*_name" array */
394                 fd_read(fd, head->name_ptr, head->name_size);
395         }
396
397
398         if (head->text_size)
399         {
400                 /* Allocate the "*_text" array */
401                 C_MAKE(head->text_ptr, head->text_size, char);
402
403                 /* Read the "*_text" array */
404                 fd_read(fd, head->text_ptr, head->text_size);
405         }
406
407
408         if (head->tag_size)
409         {
410                 /* Allocate the "*_tag" array */
411                 C_MAKE(head->tag_ptr, head->tag_size, char);
412
413                 /* Read the "*_tag" array */
414                 fd_read(fd, head->tag_ptr, head->tag_size);
415         }
416
417
418         /* Success */
419         return (0);
420 }
421
422
423
424 /*!
425  * @brief ヘッダ構造体の更新
426  * Initialize the header of an *_info.raw file.
427  * @param head rawファイルのヘッダ
428  * @param num データ数
429  * @param len データの長さ
430  * @return エラーコード
431  */
432 static void init_header(header *head, int num, int len)
433 {
434         /* Save the "version" */
435         head->v_major = FAKE_VER_MAJOR;
436         head->v_minor = FAKE_VER_MINOR;
437         head->v_patch = FAKE_VER_PATCH;
438         head->v_extra = 0;
439
440         /* Save the "record" information */
441         head->info_num = num;
442         head->info_len = len;
443
444         /* Save the size of "*_head" and "*_info" */
445         head->head_size = sizeof(header);
446         head->info_size = head->info_num * head->info_len;
447 }
448
449
450 /*!
451  * @brief ヘッダ構造体の更新
452  * Initialize the "*_info" array
453  * @param filename ファイル名(拡張子txt/raw)
454  * @param head 処理に用いるヘッダ構造体
455  * @param info データ保管先の構造体ポインタ
456  * @param name 名称用可変文字列の保管先
457  * @param text テキスト用可変文字列の保管先
458  * @param tag タグ用可変文字列の保管先
459  * @return エラーコード
460  * @note
461  * Note that we let each entry have a unique "name" and "text" string,
462  * even if the string happens to be empty (everyone has a unique '\0').
463  */
464 static errr init_info(cptr filename, header *head,
465                       void **info, char **name, char **text, char **tag)
466 {
467         int fd;
468
469         int mode = 0644;
470
471         errr err = 1;
472
473         FILE *fp;
474
475         /* General buffer */
476         char buf[1024];
477
478
479 #ifdef ALLOW_TEMPLATES
480
481         /*** Load the binary image file ***/
482
483         /* Build the filename */
484 #ifdef JP
485         path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s_j.raw", filename));
486 #else
487         path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename));
488 #endif
489
490
491         /* Attempt to open the "raw" file */
492         fd = fd_open(buf, O_RDONLY);
493
494         /* Process existing "raw" file */
495         if (fd >= 0)
496         {
497 #ifdef CHECK_MODIFICATION_TIME
498
499                 err = check_modification_date(fd, format("%s.txt", filename));
500
501 #endif /* CHECK_MODIFICATION_TIME */
502
503                 /* Attempt to parse the "raw" file */
504                 if (!err)
505                         err = init_info_raw(fd, head);
506
507                 /* Close it */
508                 (void)fd_close(fd);
509         }
510
511
512         /* Do we have to parse the *.txt file? */
513         if (err)
514         {
515                 /*** Make the fake arrays ***/
516
517                 /* Allocate the "*_info" array */
518                 C_MAKE(head->info_ptr, head->info_size, char);
519
520                 /* Hack -- make "fake" arrays */
521                 if (name) C_MAKE(head->name_ptr, FAKE_NAME_SIZE, char);
522                 if (text) C_MAKE(head->text_ptr, FAKE_TEXT_SIZE, char);
523                 if (tag)  C_MAKE(head->tag_ptr, FAKE_TAG_SIZE, char);
524
525                 if (info) (*info) = head->info_ptr;
526                 if (name) (*name) = head->name_ptr;
527                 if (text) (*text) = head->text_ptr;
528                 if (tag)  (*tag)  = head->tag_ptr;
529
530                 /*** Load the ascii template file ***/
531
532                 /* Build the filename */
533
534                 path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, format("%s.txt", filename));
535
536                 /* Open the file */
537                 fp = my_fopen(buf, "r");
538
539                 /* Parse it */
540 #ifdef JP
541                 if (!fp) quit(format("'%s.txt'ファイルをオープンできません。", filename));
542 #else
543                 if (!fp) quit(format("Cannot open '%s.txt' file.", filename));
544 #endif
545
546
547                 /* Parse the file */
548                 err = init_info_txt(fp, buf, head, head->parse_info_txt);
549
550                 /* Close it */
551                 my_fclose(fp);
552
553                 /* Errors */
554                 if (err)
555                 {
556                         cptr oops;
557
558 #ifdef JP
559                         /* Error string */
560                         oops = (((err > 0) && (err < PARSE_ERROR_MAX)) ? err_str[err] : "未知の");
561
562                         /* Oops */
563                         msg_format("'%s.txt'ファイルの %d 行目にエラー。", filename, error_line);
564                         msg_format("レコード %d は '%s' エラーがあります。", error_idx, oops);
565                         msg_format("構文 '%s'。", buf);
566                         msg_print(NULL);
567
568                         /* Quit */
569                         quit(format("'%s.txt'ファイルにエラー", filename));
570 #else
571                         /* Error string */
572                         oops = (((err > 0) && (err < PARSE_ERROR_MAX)) ? err_str[err] : "unknown");
573
574                         /* Oops */
575                         msg_format("Error %d at line %d of '%s.txt'.", err, error_line, filename);
576                         msg_format("Record %d contains a '%s' error.", error_idx, oops);
577                         msg_format("Parsing '%s'.", buf);
578                         msg_print(NULL);
579
580                         /* Quit */
581                         quit(format("Error in '%s.txt' file.", filename));
582 #endif
583
584                 }
585
586
587                 /*** Make final retouch on fake tags ***/
588
589                 if (head->retouch)
590                 {
591                         (*head->retouch)(head);
592                 }
593
594
595                 /*** Dump the binary image file ***/
596
597                 /* File type is "DATA" */
598                 FILE_TYPE(FILE_TYPE_DATA);
599
600                 /* Build the filename */
601 #ifdef JP
602                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s_j.raw", filename));
603 #else
604                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename));
605 #endif
606
607
608                 /* Grab permissions */
609                 safe_setuid_grab();
610
611                 /* Kill the old file */
612                 (void)fd_kill(buf);
613
614                 /* Attempt to create the raw file */
615                 fd = fd_make(buf, mode);
616
617                 /* Drop permissions */
618                 safe_setuid_drop();
619
620                 /* Dump to the file */
621                 if (fd >= 0)
622                 {
623                         /* Dump it */
624                         fd_write(fd, (cptr)(head), head->head_size);
625
626                         /* Dump the "*_info" array */
627                         fd_write(fd, head->info_ptr, head->info_size);
628
629                         /* Dump the "*_name" array */
630                         fd_write(fd, head->name_ptr, head->name_size);
631
632                         /* Dump the "*_text" array */
633                         fd_write(fd, head->text_ptr, head->text_size);
634
635                         /* Dump the "*_tag" array */
636                         fd_write(fd, head->tag_ptr, head->tag_size);
637
638                         /* Close */
639                         (void)fd_close(fd);
640                 }
641
642
643                 /*** Kill the fake arrays ***/
644
645                 /* Free the "*_info" array */
646                 C_KILL(head->info_ptr, head->info_size, char);
647
648                 /* Hack -- Free the "fake" arrays */
649                 if (name) C_KILL(head->name_ptr, FAKE_NAME_SIZE, char);
650                 if (text) C_KILL(head->text_ptr, FAKE_TEXT_SIZE, char);
651                 if (tag)  C_KILL(head->tag_ptr, FAKE_TAG_SIZE, char);
652
653 #endif  /* ALLOW_TEMPLATES */
654
655
656                 /*** Load the binary image file ***/
657
658                 /* Build the filename */
659 #ifdef JP
660                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s_j.raw", filename));
661 #else
662                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename));
663 #endif
664
665
666                 /* Attempt to open the "raw" file */
667                 fd = fd_open(buf, O_RDONLY);
668
669                 /* Process existing "raw" file */
670 #ifdef JP
671                 if (fd < 0) quit(format("'%s_j.raw'ファイルをロードできません。", filename));
672 #else
673                 if (fd < 0) quit(format("Cannot load '%s.raw' file.", filename));
674 #endif
675
676
677                 /* Attempt to parse the "raw" file */
678                 err = init_info_raw(fd, head);
679
680                 /* Close it */
681                 (void)fd_close(fd);
682
683                 /* Error */
684 #ifdef JP
685                 if (err) quit(format("'%s_j.raw'ファイルを解析できません。", filename));
686 #else
687                 if (err) quit(format("Cannot parse '%s.raw' file.", filename));
688 #endif
689
690 #ifdef ALLOW_TEMPLATES
691         }
692 #endif
693
694         if (info) (*info) = head->info_ptr;
695         if (name) (*name) = head->name_ptr;
696         if (text) (*text) = head->text_ptr;
697         if (tag)  (*tag)  = head->tag_ptr;
698
699         /* Success */
700         return (0);
701 }
702
703
704 /*!
705  * @brief 地形情報読み込みのメインルーチン /
706  * Initialize the "f_info" array
707  * @return エラーコード
708  */
709 static errr init_f_info(void)
710 {
711         /* Init the header */
712         init_header(&f_head, max_f_idx, sizeof(feature_type));
713
714 #ifdef ALLOW_TEMPLATES
715
716         /* Save a pointer to the parsing function */
717         f_head.parse_info_txt = parse_f_info;
718
719         /* Save a pointer to the retouch fake tags */
720         f_head.retouch = retouch_f_info;
721
722 #endif /* ALLOW_TEMPLATES */
723
724         return init_info("f_info", &f_head,
725                          (void*)&f_info, &f_name, NULL, &f_tag);
726 }
727
728
729 /*!
730  * @brief ベースアイテム情報読み込みのメインルーチン /
731  * Initialize the "k_info" array
732  * @return エラーコード
733  */
734 static errr init_k_info(void)
735 {
736         /* Init the header */
737         init_header(&k_head, max_k_idx, sizeof(object_kind));
738
739 #ifdef ALLOW_TEMPLATES
740
741         /* Save a pointer to the parsing function */
742         k_head.parse_info_txt = parse_k_info;
743
744 #endif /* ALLOW_TEMPLATES */
745
746         return init_info("k_info", &k_head,
747                          (void*)&k_info, &k_name, &k_text, NULL);
748 }
749
750
751
752 /*!
753  * @brief 固定アーティファクト情報読み込みのメインルーチン /
754  * Initialize the "a_info" array
755  * @return エラーコード
756  */
757 static errr init_a_info(void)
758 {
759         /* Init the header */
760         init_header(&a_head, max_a_idx, sizeof(artifact_type));
761
762 #ifdef ALLOW_TEMPLATES
763
764         /* Save a pointer to the parsing function */
765         a_head.parse_info_txt = parse_a_info;
766
767 #endif /* ALLOW_TEMPLATES */
768
769         return init_info("a_info", &a_head,
770                          (void*)&a_info, &a_name, &a_text, NULL);
771 }
772
773
774
775 /*!
776  * @brief 固定アーティファクト情報読み込みのメインルーチン /
777  * Initialize the "e_info" array
778  * @return エラーコード
779  */
780 static errr init_e_info(void)
781 {
782         /* Init the header */
783         init_header(&e_head, max_e_idx, sizeof(ego_item_type));
784
785 #ifdef ALLOW_TEMPLATES
786
787         /* Save a pointer to the parsing function */
788         e_head.parse_info_txt = parse_e_info;
789
790 #endif /* ALLOW_TEMPLATES */
791
792         return init_info("e_info", &e_head,
793                          (void*)&e_info, &e_name, &e_text, NULL);
794 }
795
796
797
798 /*!
799  * @brief モンスター種族情報読み込みのメインルーチン /
800  * Initialize the "r_info" array
801  * @return エラーコード
802  */
803 static errr init_r_info(void)
804 {
805         /* Init the header */
806         init_header(&r_head, max_r_idx, sizeof(monster_race));
807
808 #ifdef ALLOW_TEMPLATES
809
810         /* Save a pointer to the parsing function */
811         r_head.parse_info_txt = parse_r_info;
812
813 #endif /* ALLOW_TEMPLATES */
814
815         return init_info("r_info", &r_head,
816                          (void*)&r_info, &r_name, &r_text, NULL);
817 }
818
819
820
821 /*!
822  * @brief ダンジョン情報読み込みのメインルーチン /
823  * Initialize the "d_info" array
824  * @return エラーコード
825  */
826 static errr init_d_info(void)
827 {
828         /* Init the header */
829         init_header(&d_head, max_d_idx, sizeof(dungeon_info_type));
830
831 #ifdef ALLOW_TEMPLATES
832
833         /* Save a pointer to the parsing function */
834         d_head.parse_info_txt = parse_d_info;
835
836 #endif /* ALLOW_TEMPLATES */
837
838         return init_info("d_info", &d_head,
839                          (void*)&d_info, &d_name, &d_text, NULL);
840 }
841
842
843 /*!
844  * @brief Vault情報読み込みのメインルーチン /
845  * Initialize the "v_info" array
846  * @return エラーコード
847  * @note
848  * Note that we let each entry have a unique "name" and "text" string,
849  * even if the string happens to be empty (everyone has a unique '\0').
850  */
851 errr init_v_info(void)
852 {
853         /* Init the header */
854         init_header(&v_head, max_v_idx, sizeof(vault_type));
855
856 #ifdef ALLOW_TEMPLATES
857
858         /* Save a pointer to the parsing function */
859         v_head.parse_info_txt = parse_v_info;
860
861 #endif /* ALLOW_TEMPLATES */
862
863         return init_info("v_info", &v_head,
864                          (void*)&v_info, &v_name, &v_text, NULL);
865 }
866
867
868 /*!
869  * @brief 職業技能情報読み込みのメインルーチン /
870  * Initialize the "s_info" array
871  * @return エラーコード
872  */
873 static errr init_s_info(void)
874 {
875         /* Init the header */
876         init_header(&s_head, MAX_CLASS, sizeof(skill_table));
877
878 #ifdef ALLOW_TEMPLATES
879
880         /* Save a pointer to the parsing function */
881         s_head.parse_info_txt = parse_s_info;
882
883 #endif /* ALLOW_TEMPLATES */
884
885         return init_info("s_info", &s_head,
886                          (void*)&s_info, NULL, NULL, NULL);
887 }
888
889
890 /*!
891  * @brief 職業魔法情報読み込みのメインルーチン /
892  * Initialize the "m_info" array
893  * @return エラーコード
894  */
895 static errr init_m_info(void)
896 {
897         /* Init the header */
898         init_header(&m_head, MAX_CLASS, sizeof(player_magic));
899
900 #ifdef ALLOW_TEMPLATES
901
902         /* Save a pointer to the parsing function */
903         m_head.parse_info_txt = parse_m_info;
904
905 #endif /* ALLOW_TEMPLATES */
906
907         return init_info("m_info", &m_head,
908                          (void*)&m_info, NULL, NULL, NULL);
909 }
910
911
912
913 /*** Initialize others ***/
914
915 /*!
916  * 店舗で販売するオブジェクトを定義する / Hack -- Objects sold in the stores -- by tval/sval pair.
917  */
918 static byte store_table[MAX_STORES][STORE_CHOICES][2] =
919 {
920         {
921                 /* General Store */
922
923                 { TV_FOOD, SV_FOOD_RATION },
924                 { TV_FOOD, SV_FOOD_RATION },
925                 { TV_FOOD, SV_FOOD_RATION },
926                 { TV_FOOD, SV_FOOD_RATION },
927
928                 { TV_FOOD, SV_FOOD_RATION },
929                 { TV_FOOD, SV_FOOD_BISCUIT },
930                 { TV_FOOD, SV_FOOD_JERKY },
931                 { TV_FOOD, SV_FOOD_JERKY },
932
933                 { TV_FOOD, SV_FOOD_PINT_OF_WINE },
934                 { TV_FOOD, SV_FOOD_PINT_OF_ALE },
935                 { TV_LITE, SV_LITE_TORCH },
936                 { TV_LITE, SV_LITE_TORCH },
937
938                 { TV_LITE, SV_LITE_TORCH },
939                 { TV_LITE, SV_LITE_TORCH },
940                 { TV_LITE, SV_LITE_LANTERN },
941                 { TV_LITE, SV_LITE_LANTERN },
942
943                 { TV_FLASK, 0 },
944                 { TV_FLASK, 0 },
945                 { TV_FLASK, 0 },
946                 { TV_FLASK, 0 },
947
948                 { TV_FLASK, 0 },
949                 { TV_FLASK, 0 },
950                 { TV_SPIKE, 0 },
951                 { TV_SPIKE, 0 },
952
953                 { TV_SHOT, SV_AMMO_NORMAL },
954                 { TV_ARROW, SV_AMMO_NORMAL },
955                 { TV_BOLT, SV_AMMO_NORMAL },
956                 { TV_DIGGING, SV_SHOVEL },
957
958                 { TV_DIGGING, SV_PICK },
959                 { TV_CLOAK, SV_CLOAK },
960                 { TV_CLOAK, SV_CLOAK },
961                 { TV_CLOAK, SV_FUR_CLOAK },
962
963                 { TV_FOOD, SV_FOOD_RATION },
964                 { TV_FOOD, SV_FOOD_RATION },
965                 { TV_FOOD, SV_FOOD_RATION },
966                 { TV_FOOD, SV_FOOD_RATION },
967
968                 { TV_POTION, SV_POTION_WATER },
969                 { TV_POTION, SV_POTION_WATER },
970                 { TV_LITE, SV_LITE_LANTERN },
971                 { TV_LITE, SV_LITE_LANTERN },
972
973                 { TV_FOOD, SV_FOOD_WAYBREAD },
974                 { TV_FOOD, SV_FOOD_WAYBREAD },
975                 { TV_CAPTURE, 0 },
976                 { TV_FIGURINE, 0 },
977
978                 { TV_SHOT, SV_AMMO_NORMAL },
979                 { TV_ARROW, SV_AMMO_NORMAL },
980                 { TV_BOLT, SV_AMMO_NORMAL },
981                 { TV_DIGGING, SV_SHOVEL }
982         },
983
984         {
985                 /* Armoury */
986
987                 { TV_BOOTS, SV_PAIR_OF_SOFT_LEATHER_BOOTS },
988                 { TV_BOOTS, SV_PAIR_OF_SOFT_LEATHER_BOOTS },
989                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
990                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
991
992                 { TV_HELM, SV_HARD_LEATHER_CAP },
993                 { TV_HELM, SV_HARD_LEATHER_CAP },
994                 { TV_HELM, SV_METAL_CAP },
995                 { TV_HELM, SV_IRON_HELM },
996
997                 { TV_SOFT_ARMOR, SV_ROBE },
998                 { TV_SOFT_ARMOR, SV_ROBE },
999                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
1000                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
1001
1002                 { TV_SOFT_ARMOR, SV_HARD_LEATHER_ARMOR },
1003                 { TV_SOFT_ARMOR, SV_HARD_LEATHER_ARMOR },
1004                 { TV_SOFT_ARMOR, SV_HARD_STUDDED_LEATHER },
1005                 { TV_SOFT_ARMOR, SV_HARD_STUDDED_LEATHER },
1006
1007                 { TV_SOFT_ARMOR, SV_RHINO_HIDE_ARMOR },
1008                 { TV_SOFT_ARMOR, SV_LEATHER_SCALE_MAIL },
1009                 { TV_HARD_ARMOR, SV_METAL_SCALE_MAIL },
1010                 { TV_HARD_ARMOR, SV_CHAIN_MAIL },
1011
1012                 { TV_HARD_ARMOR, SV_DOUBLE_RING_MAIL },
1013                 { TV_HARD_ARMOR, SV_AUGMENTED_CHAIN_MAIL },
1014                 { TV_HARD_ARMOR, SV_BAR_CHAIN_MAIL },
1015                 { TV_HARD_ARMOR, SV_DOUBLE_CHAIN_MAIL },
1016
1017                 { TV_HARD_ARMOR, SV_METAL_BRIGANDINE_ARMOUR },
1018                 { TV_HARD_ARMOR, SV_SPLINT_MAIL },
1019                 { TV_GLOVES, SV_SET_OF_LEATHER_GLOVES },
1020                 { TV_GLOVES, SV_SET_OF_LEATHER_GLOVES },
1021
1022                 { TV_GLOVES, SV_SET_OF_GAUNTLETS },
1023                 { TV_SHIELD, SV_SMALL_LEATHER_SHIELD },
1024                 { TV_SHIELD, SV_LARGE_LEATHER_SHIELD },
1025                 { TV_SHIELD, SV_SMALL_METAL_SHIELD },
1026
1027                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
1028                 { TV_BOOTS, SV_PAIR_OF_HARD_LEATHER_BOOTS },
1029                 { TV_HELM, SV_HARD_LEATHER_CAP },
1030                 { TV_HELM, SV_HARD_LEATHER_CAP },
1031
1032                 { TV_SOFT_ARMOR, SV_ROBE },
1033                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
1034                 { TV_SOFT_ARMOR, SV_SOFT_LEATHER_ARMOR },
1035                 { TV_SOFT_ARMOR, SV_HARD_LEATHER_ARMOR },
1036
1037                 { TV_SOFT_ARMOR, SV_LEATHER_JACK },
1038                 { TV_HARD_ARMOR, SV_METAL_SCALE_MAIL },
1039                 { TV_HARD_ARMOR, SV_CHAIN_MAIL },
1040                 { TV_HARD_ARMOR, SV_CHAIN_MAIL },
1041
1042                 { TV_GLOVES, SV_SET_OF_LEATHER_GLOVES },
1043                 { TV_GLOVES, SV_SET_OF_GAUNTLETS },
1044                 { TV_SHIELD, SV_SMALL_LEATHER_SHIELD },
1045                 { TV_SHIELD, SV_SMALL_LEATHER_SHIELD }
1046         },
1047
1048         {
1049                 /* Weaponsmith */
1050
1051                 { TV_SWORD, SV_DAGGER },
1052                 { TV_SWORD, SV_MAIN_GAUCHE },
1053                 { TV_SWORD, SV_RAPIER },
1054                 { TV_SWORD, SV_SMALL_SWORD },
1055
1056                 { TV_SWORD, SV_SHORT_SWORD },
1057                 { TV_SWORD, SV_SABRE },
1058                 { TV_SWORD, SV_CUTLASS },
1059                 { TV_SWORD, SV_TULWAR },
1060
1061                 { TV_SWORD, SV_BROAD_SWORD },
1062                 { TV_SWORD, SV_LONG_SWORD },
1063                 { TV_SWORD, SV_SCIMITAR },
1064                 { TV_SWORD, SV_KATANA },
1065
1066                 { TV_SWORD, SV_BASTARD_SWORD },
1067                 { TV_POLEARM, SV_SPEAR },
1068                 { TV_POLEARM, SV_AWL_PIKE },
1069                 { TV_POLEARM, SV_TRIDENT },
1070
1071                 { TV_POLEARM, SV_PIKE },
1072                 { TV_POLEARM, SV_BEAKED_AXE },
1073                 { TV_POLEARM, SV_BROAD_AXE },
1074                 { TV_POLEARM, SV_LANCE },
1075
1076                 { TV_POLEARM, SV_BATTLE_AXE },
1077                 { TV_POLEARM, SV_HATCHET },
1078                 { TV_BOW, SV_SLING },
1079                 { TV_BOW, SV_SHORT_BOW },
1080
1081                 { TV_BOW, SV_LIGHT_XBOW },
1082                 { TV_SHOT, SV_AMMO_NORMAL },
1083                 { TV_SHOT, SV_AMMO_NORMAL },
1084                 { TV_ARROW, SV_AMMO_NORMAL },
1085
1086                 { TV_ARROW, SV_AMMO_NORMAL },
1087                 { TV_BOLT, SV_AMMO_NORMAL },
1088                 { TV_BOLT, SV_AMMO_NORMAL },
1089                 { TV_BOW, SV_LIGHT_XBOW },
1090
1091                 { TV_ARROW, SV_AMMO_NORMAL },
1092                 { TV_BOLT, SV_AMMO_NORMAL },
1093                 { TV_BOW, SV_SHORT_BOW },
1094                 { TV_BOW, SV_LIGHT_XBOW },
1095
1096                 { TV_SWORD, SV_DAGGER },
1097                 { TV_SWORD, SV_TANTO },
1098                 { TV_SWORD, SV_RAPIER },
1099                 { TV_SWORD, SV_SMALL_SWORD },
1100
1101                 { TV_SWORD, SV_SHORT_SWORD },
1102                 { TV_SWORD, SV_LONG_SWORD },
1103                 { TV_SWORD, SV_SCIMITAR },
1104                 { TV_SWORD, SV_BROAD_SWORD },
1105
1106                 { TV_HISSATSU_BOOK, 0 },
1107                 { TV_HISSATSU_BOOK, 0 },
1108                 { TV_HISSATSU_BOOK, 1 },
1109                 { TV_HISSATSU_BOOK, 1 },
1110         },
1111
1112         {
1113                 /* Temple */
1114
1115                 { TV_HAFTED, SV_NUNCHAKU },
1116                 { TV_HAFTED, SV_QUARTERSTAFF },
1117                 { TV_HAFTED, SV_MACE },
1118                 { TV_HAFTED, SV_BO_STAFF },
1119
1120                 { TV_HAFTED, SV_WAR_HAMMER },
1121                 { TV_HAFTED, SV_WAR_HAMMER },
1122                 { TV_HAFTED, SV_MORNING_STAR },
1123                 { TV_HAFTED, SV_FLAIL },
1124
1125                 { TV_HAFTED, SV_LEAD_FILLED_MACE },
1126                 { TV_SCROLL, SV_SCROLL_REMOVE_CURSE },
1127                 { TV_SCROLL, SV_SCROLL_BLESSING },
1128                 { TV_SCROLL, SV_SCROLL_HOLY_CHANT },
1129
1130                 { TV_POTION, SV_POTION_HEROISM },
1131                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1132                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1133                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1134
1135                 { TV_POTION, SV_POTION_CURE_LIGHT },
1136                 { TV_POTION, SV_POTION_CURE_SERIOUS },
1137                 { TV_POTION, SV_POTION_CURE_SERIOUS },
1138                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1139
1140                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1141                 { TV_POTION, SV_POTION_RESTORE_EXP },
1142                 { TV_POTION, SV_POTION_RESTORE_EXP },
1143                 { TV_POTION, SV_POTION_RESTORE_EXP },
1144
1145                 { TV_LIFE_BOOK, 0 },
1146                 { TV_LIFE_BOOK, 0 },
1147                 { TV_LIFE_BOOK, 1 },
1148                 { TV_LIFE_BOOK, 1 },
1149
1150                 { TV_CRUSADE_BOOK, 0 },
1151                 { TV_CRUSADE_BOOK, 0 },
1152                 { TV_CRUSADE_BOOK, 1 },
1153                 { TV_CRUSADE_BOOK, 1 },
1154
1155                 { TV_HAFTED, SV_WHIP },
1156                 { TV_HAFTED, SV_MACE },
1157                 { TV_HAFTED, SV_BALL_AND_CHAIN },
1158                 { TV_HAFTED, SV_WAR_HAMMER },
1159
1160                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1161                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1162                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1163                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1164
1165                 { TV_POTION, SV_POTION_CURE_CRITICAL },
1166                 { TV_POTION, SV_POTION_RESTORE_EXP },
1167
1168                 { TV_FIGURINE, 0 },
1169                 { TV_STATUE, SV_ANY },
1170
1171                 { TV_SCROLL, SV_SCROLL_REMOVE_CURSE },
1172                 { TV_SCROLL, SV_SCROLL_REMOVE_CURSE },
1173                 { TV_SCROLL, SV_SCROLL_STAR_REMOVE_CURSE },
1174                 { TV_SCROLL, SV_SCROLL_STAR_REMOVE_CURSE }
1175         },
1176
1177         {
1178                 /* Alchemy shop */
1179
1180                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_HIT },
1181                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_DAM },
1182                 { TV_SCROLL, SV_SCROLL_ENCHANT_ARMOR },
1183                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1184
1185                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1186                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1187                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1188                 { TV_SCROLL, SV_SCROLL_LIGHT },
1189
1190                 { TV_SCROLL, SV_SCROLL_PHASE_DOOR },
1191                 { TV_SCROLL, SV_SCROLL_PHASE_DOOR },
1192                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1193                 { TV_SCROLL, SV_SCROLL_MONSTER_CONFUSION },
1194
1195                 { TV_SCROLL, SV_SCROLL_MAPPING },
1196                 { TV_SCROLL, SV_SCROLL_DETECT_GOLD },
1197                 { TV_SCROLL, SV_SCROLL_DETECT_ITEM },
1198                 { TV_SCROLL, SV_SCROLL_DETECT_TRAP },
1199
1200                 { TV_SCROLL, SV_SCROLL_DETECT_INVIS },
1201                 { TV_SCROLL, SV_SCROLL_RECHARGING },
1202                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1203                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1204
1205                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1206                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1207                 { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL },
1208                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1209
1210                 { TV_SCROLL, SV_SCROLL_TELEPORT },
1211                 { TV_POTION, SV_POTION_RES_STR },
1212                 { TV_POTION, SV_POTION_RES_INT },
1213                 { TV_POTION, SV_POTION_RES_WIS },
1214
1215                 { TV_POTION, SV_POTION_RES_DEX },
1216                 { TV_POTION, SV_POTION_RES_CON },
1217                 { TV_POTION, SV_POTION_RES_CHR },
1218                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1219
1220                 { TV_SCROLL, SV_SCROLL_IDENTIFY },
1221                 { TV_SCROLL, SV_SCROLL_STAR_IDENTIFY },  /* Yep, occasionally! */
1222                 { TV_SCROLL, SV_SCROLL_STAR_IDENTIFY },
1223                 { TV_SCROLL, SV_SCROLL_LIGHT },
1224
1225                 { TV_POTION, SV_POTION_RES_STR },
1226                 { TV_POTION, SV_POTION_RES_INT },
1227                 { TV_POTION, SV_POTION_RES_WIS },
1228                 { TV_POTION, SV_POTION_RES_DEX },
1229
1230                 { TV_POTION, SV_POTION_RES_CON },
1231                 { TV_POTION, SV_POTION_RES_CHR },
1232                 { TV_SCROLL, SV_SCROLL_ENCHANT_ARMOR },
1233                 { TV_SCROLL, SV_SCROLL_ENCHANT_ARMOR },
1234
1235                 { TV_SCROLL, SV_SCROLL_RECHARGING },
1236                 { TV_SCROLL, SV_SCROLL_PHASE_DOOR },
1237                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_HIT },
1238                 { TV_SCROLL, SV_SCROLL_ENCHANT_WEAPON_TO_DAM },
1239
1240         },
1241
1242         {
1243                 /* Magic-User store */
1244
1245                 { TV_RING, SV_RING_PROTECTION },
1246                 { TV_RING, SV_RING_LEVITATION_FALL },
1247                 { TV_RING, SV_RING_PROTECTION },
1248                 { TV_RING, SV_RING_RESIST_FIRE },
1249
1250                 { TV_RING, SV_RING_RESIST_COLD },
1251                 { TV_AMULET, SV_AMULET_CHARISMA },
1252                 { TV_RING, SV_RING_WARNING },
1253                 { TV_AMULET, SV_AMULET_RESIST_ACID },
1254
1255                 { TV_AMULET, SV_AMULET_SEARCHING },
1256                 { TV_WAND, SV_WAND_SLOW_MONSTER },
1257                 { TV_WAND, SV_WAND_CONFUSE_MONSTER },
1258                 { TV_WAND, SV_WAND_SLEEP_MONSTER },
1259
1260                 { TV_WAND, SV_WAND_MAGIC_MISSILE },
1261                 { TV_WAND, SV_WAND_STINKING_CLOUD },
1262                 { TV_WAND, SV_WAND_WONDER },
1263                 { TV_WAND, SV_WAND_DISARMING },
1264
1265                 { TV_STAFF, SV_STAFF_LITE },
1266                 { TV_STAFF, SV_STAFF_MAPPING },
1267                 { TV_STAFF, SV_STAFF_DETECT_TRAP },
1268                 { TV_STAFF, SV_STAFF_DETECT_DOOR },
1269
1270                 { TV_STAFF, SV_STAFF_DETECT_GOLD },
1271                 { TV_STAFF, SV_STAFF_DETECT_ITEM },
1272                 { TV_STAFF, SV_STAFF_DETECT_INVIS },
1273                 { TV_STAFF, SV_STAFF_DETECT_EVIL },
1274
1275                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1276                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1277                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1278                 { TV_STAFF, SV_STAFF_TELEPORTATION },
1279
1280                 { TV_STAFF, SV_STAFF_IDENTIFY },
1281                 { TV_STAFF, SV_STAFF_IDENTIFY },
1282                 { TV_STAFF, SV_STAFF_IDENTIFY },
1283
1284                 { TV_STAFF, SV_STAFF_IDENTIFY },
1285                 { TV_STAFF, SV_STAFF_REMOVE_CURSE },
1286                 { TV_STAFF, SV_STAFF_CURE_LIGHT },
1287                 { TV_STAFF, SV_STAFF_PROBING },
1288
1289                 { TV_FIGURINE, 0 },
1290
1291                 { TV_SORCERY_BOOK, 0 },
1292                 { TV_SORCERY_BOOK, 0 },
1293                 { TV_SORCERY_BOOK, 1 },
1294                 { TV_SORCERY_BOOK, 1 },
1295
1296                 { TV_ARCANE_BOOK, 0 },
1297                 { TV_ARCANE_BOOK, 0 },
1298                 { TV_ARCANE_BOOK, 1 },
1299                 { TV_ARCANE_BOOK, 1 },
1300
1301                 { TV_ARCANE_BOOK, 2 },
1302                 { TV_ARCANE_BOOK, 2 },
1303                 { TV_ARCANE_BOOK, 3 },
1304                 { TV_ARCANE_BOOK, 3 },
1305
1306         },
1307
1308         {
1309                 /* Black Market (unused) */
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                 { 0, 0 },
1338                 { 0, 0 },
1339                 { 0, 0 },
1340                 { 0, 0 },
1341                 { 0, 0 }
1342         },
1343
1344         {
1345                 /* Home (unused) */
1346                 { 0, 0 },
1347                 { 0, 0 },
1348                 { 0, 0 },
1349                 { 0, 0 },
1350                 { 0, 0 },
1351                 { 0, 0 },
1352                 { 0, 0 },
1353                 { 0, 0 },
1354                 { 0, 0 },
1355                 { 0, 0 },
1356                 { 0, 0 },
1357                 { 0, 0 },
1358                 { 0, 0 },
1359                 { 0, 0 },
1360                 { 0, 0 },
1361                 { 0, 0 },
1362                 { 0, 0 },
1363                 { 0, 0 },
1364                 { 0, 0 },
1365                 { 0, 0 },
1366                 { 0, 0 },
1367                 { 0, 0 },
1368                 { 0, 0 },
1369                 { 0, 0 },
1370                 { 0, 0 },
1371                 { 0, 0 },
1372                 { 0, 0 },
1373                 { 0, 0 },
1374                 { 0, 0 },
1375                 { 0, 0 },
1376                 { 0, 0 },
1377                 { 0, 0 }
1378         },
1379
1380         {
1381                 /* Bookstore */
1382                 { TV_SORCERY_BOOK, 0 },
1383                 { TV_SORCERY_BOOK, 0 },
1384                 { TV_SORCERY_BOOK, 1 },
1385                 { TV_SORCERY_BOOK, 1 },
1386
1387                 { TV_NATURE_BOOK, 0 },
1388                 { TV_NATURE_BOOK, 0 },
1389                 { TV_NATURE_BOOK, 1 },
1390                 { TV_NATURE_BOOK, 1 },
1391
1392                 { TV_CHAOS_BOOK, 0 },
1393                 { TV_CHAOS_BOOK, 0 },
1394                 { TV_CHAOS_BOOK, 1 },
1395                 { TV_CHAOS_BOOK, 1 },
1396
1397                 { TV_DEATH_BOOK, 0 },
1398                 { TV_DEATH_BOOK, 0 },
1399                 { TV_DEATH_BOOK, 1 },
1400                 { TV_DEATH_BOOK, 1 },
1401
1402                 { TV_TRUMP_BOOK, 0 },           /* +16 */
1403                 { TV_TRUMP_BOOK, 0 },
1404                 { TV_TRUMP_BOOK, 1 },
1405                 { TV_TRUMP_BOOK, 1 },
1406
1407                 { TV_ARCANE_BOOK, 0 },
1408                 { TV_ARCANE_BOOK, 1 },
1409                 { TV_ARCANE_BOOK, 2 },
1410                 { TV_ARCANE_BOOK, 3 },
1411
1412                 { TV_CRAFT_BOOK, 0 },
1413                 { TV_CRAFT_BOOK, 0 },
1414                 { TV_CRAFT_BOOK, 1 },
1415                 { TV_CRAFT_BOOK, 1 },
1416
1417                 { TV_DAEMON_BOOK, 0 },
1418                 { TV_DAEMON_BOOK, 0 },
1419                 { TV_DAEMON_BOOK, 1 },
1420                 { TV_DAEMON_BOOK, 1 },
1421
1422                 { TV_MUSIC_BOOK, 0 },
1423                 { TV_MUSIC_BOOK, 0 },
1424                 { TV_MUSIC_BOOK, 1 },
1425                 { TV_MUSIC_BOOK, 1 },
1426
1427                 { TV_HEX_BOOK, 0 },
1428                 { TV_HEX_BOOK, 0 },
1429                 { TV_HEX_BOOK, 1 },
1430                 { TV_HEX_BOOK, 1 },
1431         },
1432
1433         {
1434                 /* Museum (unused) */
1435                 { 0, 0 },
1436                 { 0, 0 },
1437                 { 0, 0 },
1438                 { 0, 0 },
1439                 { 0, 0 },
1440                 { 0, 0 },
1441                 { 0, 0 },
1442                 { 0, 0 },
1443                 { 0, 0 },
1444                 { 0, 0 },
1445                 { 0, 0 },
1446                 { 0, 0 },
1447                 { 0, 0 },
1448                 { 0, 0 },
1449                 { 0, 0 },
1450                 { 0, 0 },
1451                 { 0, 0 },
1452                 { 0, 0 },
1453                 { 0, 0 },
1454                 { 0, 0 },
1455                 { 0, 0 },
1456                 { 0, 0 },
1457                 { 0, 0 },
1458                 { 0, 0 },
1459                 { 0, 0 },
1460                 { 0, 0 },
1461                 { 0, 0 },
1462                 { 0, 0 },
1463                 { 0, 0 },
1464                 { 0, 0 },
1465                 { 0, 0 },
1466                 { 0, 0 }
1467         }
1468 };
1469
1470
1471 /*!
1472  * @brief 基本情報読み込みのメインルーチン /
1473  * Initialize misc. values
1474  * @return エラーコード
1475  */
1476 static errr init_misc(void)
1477 {
1478         /* Initialize the values */
1479         process_dungeon_file("misc.txt", 0, 0, 0, 0);
1480
1481         return 0;
1482 }
1483
1484
1485 /*!
1486  * @brief 町情報読み込みのメインルーチン /
1487  * Initialize town array
1488  * @return エラーコード
1489  */
1490 static errr init_towns(void)
1491 {
1492         int i, j, k;
1493
1494         /*** Prepare the Towns ***/
1495
1496         /* Allocate the towns */
1497         C_MAKE(town, max_towns, town_type);
1498
1499         for (i = 1; i < max_towns; i++)
1500         {
1501                 /*** Prepare the Stores ***/
1502
1503                 /* Allocate the stores */
1504                 C_MAKE(town[i].store, MAX_STORES, store_type);
1505
1506                 /* Fill in each store */
1507                 for (j = 0; j < MAX_STORES; j++)
1508                 {
1509                         /* Access the store */
1510                         store_type *st_ptr = &town[i].store[j];
1511
1512                         if ((i > 1) && (j == STORE_MUSEUM || j == STORE_HOME)) continue;
1513
1514                         /* Assume full stock */
1515
1516                 /*
1517                  * 我が家が 20 ページまで使える隠し機能のための準備。
1518                  * オプションが有効でもそうでなくても一応スペース
1519                  * を作っておく。
1520                  */
1521                 if (j == STORE_HOME)
1522                 {
1523                         st_ptr->stock_size = (STORE_INVEN_MAX * 10);
1524                 }
1525                 else if (j == STORE_MUSEUM)
1526                 {
1527                         st_ptr->stock_size = (STORE_INVEN_MAX * 50);
1528                 }
1529                 else
1530                 {
1531                         st_ptr->stock_size = STORE_INVEN_MAX;
1532                 }
1533
1534
1535                         /* Allocate the stock */
1536                         C_MAKE(st_ptr->stock, st_ptr->stock_size, object_type);
1537
1538                         /* No table for the black market or home */
1539                         if ((j == STORE_BLACK) || (j == STORE_HOME) || (j == STORE_MUSEUM)) continue;
1540
1541                         /* Assume full table */
1542                         st_ptr->table_size = STORE_CHOICES;
1543
1544                         /* Allocate the stock */
1545                         C_MAKE(st_ptr->table, st_ptr->table_size, s16b);
1546
1547                         /* Scan the choices */
1548                         for (k = 0; k < STORE_CHOICES; k++)
1549                         {
1550                                 int k_idx;
1551
1552                                 /* Extract the tval/sval codes */
1553                                 int tv = store_table[j][k][0];
1554                                 int sv = store_table[j][k][1];
1555
1556                                 /* Look for it */
1557                                 for (k_idx = 1; k_idx < max_k_idx; k_idx++)
1558                                 {
1559                                         object_kind *k_ptr = &k_info[k_idx];
1560
1561                                         /* Found a match */
1562                                         if ((k_ptr->tval == tv) && (k_ptr->sval == sv)) break;
1563                                 }
1564
1565                                 /* Catch errors */
1566                                 if (k_idx == max_k_idx) continue;
1567
1568                                 /* Add that item index to the table */
1569                                 st_ptr->table[st_ptr->table_num++] = k_idx;
1570                         }
1571                 }
1572         }
1573
1574         return 0;
1575 }
1576
1577 /*!
1578  * @brief 店情報初期化のメインルーチン /
1579  * Initialize buildings
1580  * @return エラーコード
1581  */
1582 errr init_buildings(void)
1583 {
1584         int i, j;
1585
1586         for (i = 0; i < MAX_BLDG; i++)
1587         {
1588                 building[i].name[0] = '\0';
1589                 building[i].owner_name[0] = '\0';
1590                 building[i].owner_race[0] = '\0';
1591
1592                 for (j = 0; j < 8; j++)
1593                 {
1594                         building[i].act_names[j][0] = '\0';
1595                         building[i].member_costs[j] = 0;
1596                         building[i].other_costs[j] = 0;
1597                         building[i].letters[j] = 0;
1598                         building[i].actions[j] = 0;
1599                         building[i].action_restr[j] = 0;
1600                 }
1601
1602                 for (j = 0; j < MAX_CLASS; j++)
1603                 {
1604                         building[i].member_class[j] = 0;
1605                 }
1606
1607                 for (j = 0; j < MAX_RACES; j++)
1608                 {
1609                         building[i].member_race[j] = 0;
1610                 }
1611
1612                 for (j = 0; j < MAX_MAGIC+1; j++)
1613                 {
1614                         building[i].member_realm[j] = 0;
1615                 }
1616         }
1617
1618         return (0);
1619 }
1620
1621
1622 /*!
1623  * @brief クエスト情報初期化のメインルーチン /
1624  * Initialize quest array
1625  * @return エラーコード
1626  */
1627 static errr init_quests(void)
1628 {
1629         int i;
1630
1631         /*** Prepare the quests ***/
1632
1633         /* Allocate the quests */
1634         C_MAKE(quest, max_quests, quest_type);
1635
1636         /* Set all quest to "untaken" */
1637         for (i = 0; i < max_quests; i++)
1638         {
1639                 quest[i].status = QUEST_STATUS_UNTAKEN;
1640         }
1641
1642         return 0;
1643 }
1644
1645 /*! 地形タグ情報から地形IDを得られなかった場合にTRUEを返すグローバル変数 */
1646 static bool feat_tag_is_not_found = FALSE;
1647
1648 /*!
1649  * @brief 地形タグからIDを得る /
1650  * Initialize quest array
1651  * @return 地形ID
1652  */
1653 s16b f_tag_to_index_in_init(cptr str)
1654 {
1655         s16b feat = f_tag_to_index(str);
1656
1657         if (feat < 0) feat_tag_is_not_found = TRUE;
1658
1659         return feat;
1660 }
1661
1662
1663 /*!
1664  * @brief 地形の汎用定義をタグを通じて取得する /
1665  * Initialize feature variables
1666  * @return エラーコード
1667  */
1668 static errr init_feat_variables(void)
1669 {
1670         int i;
1671
1672         /* Nothing */
1673         feat_none = f_tag_to_index_in_init("NONE");
1674
1675         /* Floor */
1676         feat_floor = f_tag_to_index_in_init("FLOOR");
1677
1678         /* Objects */
1679         feat_glyph = f_tag_to_index_in_init("GLYPH");
1680         feat_explosive_rune = f_tag_to_index_in_init("EXPLOSIVE_RUNE");
1681         feat_mirror = f_tag_to_index_in_init("MIRROR");
1682
1683         /* Doors */
1684         feat_door[DOOR_DOOR].open = f_tag_to_index_in_init("OPEN_DOOR");
1685         feat_door[DOOR_DOOR].broken = f_tag_to_index_in_init("BROKEN_DOOR");
1686         feat_door[DOOR_DOOR].closed = f_tag_to_index_in_init("CLOSED_DOOR");
1687
1688         /* Locked doors */
1689         for (i = 1; i < MAX_LJ_DOORS; i++)
1690         {
1691                 s16b door = f_tag_to_index(format("LOCKED_DOOR_%d", i));
1692                 if (door < 0) break;
1693                 feat_door[DOOR_DOOR].locked[i - 1] = door;
1694         }
1695         if (i == 1) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1696         feat_door[DOOR_DOOR].num_locked = i - 1;
1697
1698         /* Jammed doors */
1699         for (i = 0; i < MAX_LJ_DOORS; i++)
1700         {
1701                 s16b door = f_tag_to_index(format("JAMMED_DOOR_%d", i));
1702                 if (door < 0) break;
1703                 feat_door[DOOR_DOOR].jammed[i] = door;
1704         }
1705         if (!i) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1706         feat_door[DOOR_DOOR].num_jammed = i;
1707
1708         /* Glass doors */
1709         feat_door[DOOR_GLASS_DOOR].open = f_tag_to_index_in_init("OPEN_GLASS_DOOR");
1710         feat_door[DOOR_GLASS_DOOR].broken = f_tag_to_index_in_init("BROKEN_GLASS_DOOR");
1711         feat_door[DOOR_GLASS_DOOR].closed = f_tag_to_index_in_init("CLOSED_GLASS_DOOR");
1712
1713         /* Locked glass doors */
1714         for (i = 1; i < MAX_LJ_DOORS; i++)
1715         {
1716                 s16b door = f_tag_to_index(format("LOCKED_GLASS_DOOR_%d", i));
1717                 if (door < 0) break;
1718                 feat_door[DOOR_GLASS_DOOR].locked[i - 1] = door;
1719         }
1720         if (i == 1) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1721         feat_door[DOOR_GLASS_DOOR].num_locked = i - 1;
1722
1723         /* Jammed glass doors */
1724         for (i = 0; i < MAX_LJ_DOORS; i++)
1725         {
1726                 s16b door = f_tag_to_index(format("JAMMED_GLASS_DOOR_%d", i));
1727                 if (door < 0) break;
1728                 feat_door[DOOR_GLASS_DOOR].jammed[i] = door;
1729         }
1730         if (!i) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1731         feat_door[DOOR_GLASS_DOOR].num_jammed = i;
1732
1733         /* Curtains */
1734         feat_door[DOOR_CURTAIN].open = f_tag_to_index_in_init("OPEN_CURTAIN");
1735         feat_door[DOOR_CURTAIN].broken = feat_door[DOOR_CURTAIN].open;
1736         feat_door[DOOR_CURTAIN].closed = f_tag_to_index_in_init("CLOSED_CURTAIN");
1737         feat_door[DOOR_CURTAIN].locked[0] = feat_door[DOOR_CURTAIN].closed;
1738         feat_door[DOOR_CURTAIN].num_locked = 1;
1739         feat_door[DOOR_CURTAIN].jammed[0] = feat_door[DOOR_CURTAIN].closed;
1740         feat_door[DOOR_CURTAIN].num_jammed = 1;
1741
1742         /* Stairs */
1743         feat_up_stair = f_tag_to_index_in_init("UP_STAIR");
1744         feat_down_stair = f_tag_to_index_in_init("DOWN_STAIR");
1745         feat_entrance = f_tag_to_index_in_init("ENTRANCE");
1746
1747         /* Normal traps */
1748         init_normal_traps();
1749
1750         /* Special traps */
1751         feat_trap_open = f_tag_to_index_in_init("TRAP_OPEN");
1752         feat_trap_armageddon = f_tag_to_index_in_init("TRAP_ARMAGEDDON");
1753         feat_trap_piranha = f_tag_to_index_in_init("TRAP_PIRANHA");
1754
1755         /* Rubble */
1756         feat_rubble = f_tag_to_index_in_init("RUBBLE");
1757
1758         /* Seams */
1759         feat_magma_vein = f_tag_to_index_in_init("MAGMA_VEIN");
1760         feat_quartz_vein = f_tag_to_index_in_init("QUARTZ_VEIN");
1761
1762         /* Walls */
1763         feat_granite = f_tag_to_index_in_init("GRANITE");
1764         feat_permanent = f_tag_to_index_in_init("PERMANENT");
1765
1766         /* Glass floor */
1767         feat_glass_floor = f_tag_to_index_in_init("GLASS_FLOOR");
1768
1769         /* Glass walls */
1770         feat_glass_wall = f_tag_to_index_in_init("GLASS_WALL");
1771         feat_permanent_glass_wall = f_tag_to_index_in_init("PERMANENT_GLASS_WALL");
1772
1773         /* Pattern */
1774         feat_pattern_start = f_tag_to_index_in_init("PATTERN_START");
1775         feat_pattern_1 = f_tag_to_index_in_init("PATTERN_1");
1776         feat_pattern_2 = f_tag_to_index_in_init("PATTERN_2");
1777         feat_pattern_3 = f_tag_to_index_in_init("PATTERN_3");
1778         feat_pattern_4 = f_tag_to_index_in_init("PATTERN_4");
1779         feat_pattern_end = f_tag_to_index_in_init("PATTERN_END");
1780         feat_pattern_old = f_tag_to_index_in_init("PATTERN_OLD");
1781         feat_pattern_exit = f_tag_to_index_in_init("PATTERN_EXIT");
1782         feat_pattern_corrupted = f_tag_to_index_in_init("PATTERN_CORRUPTED");
1783
1784         /* Various */
1785         feat_black_market = f_tag_to_index_in_init("BLACK_MARKET");
1786         feat_town = f_tag_to_index_in_init("TOWN");
1787
1788         /* Terrains */
1789         feat_deep_water = f_tag_to_index_in_init("DEEP_WATER");
1790         feat_shallow_water = f_tag_to_index_in_init("SHALLOW_WATER");
1791         feat_deep_lava = f_tag_to_index_in_init("DEEP_LAVA");
1792         feat_shallow_lava = f_tag_to_index_in_init("SHALLOW_LAVA");
1793         feat_dirt = f_tag_to_index_in_init("DIRT");
1794         feat_grass = f_tag_to_index_in_init("GRASS");
1795         feat_flower = f_tag_to_index_in_init("FLOWER");
1796         feat_brake = f_tag_to_index_in_init("BRAKE");
1797         feat_tree = f_tag_to_index_in_init("TREE");
1798         feat_mountain = f_tag_to_index_in_init("MOUNTAIN");
1799         feat_swamp = f_tag_to_index_in_init("SWAMP");
1800
1801         /* Unknown grid (not detected) */
1802         feat_undetected = f_tag_to_index_in_init("UNDETECTED");
1803
1804         /* Wilderness terrains */
1805         init_wilderness_terrains();
1806
1807         return feat_tag_is_not_found ? PARSE_ERROR_UNDEFINED_TERRAIN_TAG : 0;
1808 }
1809
1810
1811 /*!
1812  * @brief その他の初期情報更新 /
1813  * Initialize some other arrays
1814  * @return エラーコード
1815  */
1816 static errr init_other(void)
1817 {
1818         int i, n;
1819
1820
1821         /*** Prepare the "dungeon" information ***/
1822
1823         /* Allocate and Wipe the object list */
1824         C_MAKE(o_list, max_o_idx, object_type);
1825
1826         /* Allocate and Wipe the monster list */
1827         C_MAKE(m_list, max_m_idx, monster_type);
1828
1829         /* Allocate and Wipe the monster process list */
1830         for (i = 0; i < MAX_MTIMED; i++)
1831         {
1832                 C_MAKE(mproc_list[i], max_m_idx, s16b);
1833         }
1834
1835         /* Allocate and Wipe the max dungeon level */
1836         C_MAKE(max_dlv, max_d_idx, s16b);
1837
1838         /* Allocate and wipe each line of the cave */
1839         for (i = 0; i < MAX_HGT; i++)
1840         {
1841                 /* Allocate one row of the cave */
1842                 C_MAKE(cave[i], MAX_WID, cave_type);
1843         }
1844
1845
1846         /*** Prepare the various "bizarre" arrays ***/
1847
1848         /* Macro variables */
1849         C_MAKE(macro__pat, MACRO_MAX, cptr);
1850         C_MAKE(macro__act, MACRO_MAX, cptr);
1851         C_MAKE(macro__cmd, MACRO_MAX, bool);
1852
1853         /* Macro action buffer */
1854         C_MAKE(macro__buf, 1024, char);
1855
1856         /* Quark variables */
1857         quark_init();
1858
1859         /* Message variables */
1860         C_MAKE(message__ptr, MESSAGE_MAX, u16b);
1861         C_MAKE(message__buf, MESSAGE_BUF, char);
1862
1863         /* Hack -- No messages yet */
1864         message__tail = MESSAGE_BUF;
1865
1866
1867         /*** Prepare the Player inventory ***/
1868
1869         /* Allocate it */
1870         C_MAKE(inventory, INVEN_TOTAL, object_type);
1871
1872
1873         /*** Prepare the options ***/
1874
1875         /* Scan the options */
1876         for (i = 0; option_info[i].o_desc; i++)
1877         {
1878                 int os = option_info[i].o_set;
1879                 int ob = option_info[i].o_bit;
1880
1881                 /* Set the "default" options */
1882                 if (option_info[i].o_var)
1883                 {
1884                         /* Accept */
1885                         option_mask[os] |= (1L << ob);
1886
1887                         /* Set */
1888                         if (option_info[i].o_norm)
1889                         {
1890                                 /* Set */
1891                                 option_flag[os] |= (1L << ob);
1892                         }
1893
1894                         /* Clear */
1895                         else
1896                         {
1897                                 /* Clear */
1898                                 option_flag[os] &= ~(1L << ob);
1899                         }
1900                 }
1901         }
1902
1903         /* Analyze the windows */
1904         for (n = 0; n < 8; n++)
1905         {
1906                 /* Analyze the options */
1907                 for (i = 0; i < 32; i++)
1908                 {
1909                         /* Accept */
1910                         if (window_flag_desc[i])
1911                         {
1912                                 /* Accept */
1913                                 window_mask[n] |= (1L << i);
1914                         }
1915                 }
1916         }
1917
1918         /*
1919          *  Set the "default" window flags
1920          *  Window 1 : Display messages
1921          *  Window 2 : Display inven/equip
1922          */
1923         window_flag[1] = 1L << 6;
1924         window_flag[2] = 1L << 0;
1925
1926
1927         /*** Pre-allocate space for the "format()" buffer ***/
1928
1929         /* Hack -- Just call the "format()" function */
1930         (void)format("%s (%s).", "Mr.Hoge", MAINTAINER);
1931
1932
1933         /* Success */
1934         return (0);
1935 }
1936
1937
1938 /*!
1939  * @brief オブジェクト配列を初期化する /
1940  * Initialize some other arrays
1941  * @return エラーコード
1942  */
1943 static errr init_object_alloc(void)
1944 {
1945         int i, j;
1946         object_kind *k_ptr;
1947         alloc_entry *table;
1948         s16b num[MAX_DEPTH];
1949         s16b aux[MAX_DEPTH];
1950
1951
1952         /*** Analyze object allocation info ***/
1953
1954         /* Clear the "aux" array */
1955         (void)C_WIPE(&aux, MAX_DEPTH, s16b);
1956
1957         /* Clear the "num" array */
1958         (void)C_WIPE(&num, MAX_DEPTH, s16b);
1959
1960         /* Free the old "alloc_kind_table" (if it exists) */
1961         if (alloc_kind_table)
1962         {
1963                 C_KILL(alloc_kind_table, alloc_kind_size, alloc_entry);
1964         }
1965
1966         /* Size of "alloc_kind_table" */
1967         alloc_kind_size = 0;
1968
1969         /* Scan the objects */
1970         for (i = 1; i < max_k_idx; i++)
1971         {
1972                 k_ptr = &k_info[i];
1973
1974                 /* Scan allocation pairs */
1975                 for (j = 0; j < 4; j++)
1976                 {
1977                         /* Count the "legal" entries */
1978                         if (k_ptr->chance[j])
1979                         {
1980                                 /* Count the entries */
1981                                 alloc_kind_size++;
1982
1983                                 /* Group by level */
1984                                 num[k_ptr->locale[j]]++;
1985                         }
1986                 }
1987         }
1988
1989         /* Collect the level indexes */
1990         for (i = 1; i < MAX_DEPTH; i++)
1991         {
1992                 /* Group by level */
1993                 num[i] += num[i-1];
1994         }
1995
1996         /* Paranoia */
1997 #ifdef JP
1998 if (!num[0]) quit("町のアイテムがない!");
1999 #else
2000         if (!num[0]) quit("No town objects!");
2001 #endif
2002
2003
2004
2005         /*** Initialize object allocation info ***/
2006
2007         /* Allocate the alloc_kind_table */
2008         C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry);
2009
2010         /* Access the table entry */
2011         table = alloc_kind_table;
2012
2013         /* Scan the objects */
2014         for (i = 1; i < max_k_idx; i++)
2015         {
2016                 k_ptr = &k_info[i];
2017
2018                 /* Scan allocation pairs */
2019                 for (j = 0; j < 4; j++)
2020                 {
2021                         /* Count the "legal" entries */
2022                         if (k_ptr->chance[j])
2023                         {
2024                                 int p, x, y, z;
2025
2026                                 /* Extract the base level */
2027                                 x = k_ptr->locale[j];
2028
2029                                 /* Extract the base probability */
2030                                 p = (100 / k_ptr->chance[j]);
2031
2032                                 /* Skip entries preceding our locale */
2033                                 y = (x > 0) ? num[x-1] : 0;
2034
2035                                 /* Skip previous entries at this locale */
2036                                 z = y + aux[x];
2037
2038                                 /* Load the entry */
2039                                 table[z].index = i;
2040                                 table[z].level = x;
2041                                 table[z].prob1 = p;
2042                                 table[z].prob2 = p;
2043                                 table[z].prob3 = p;
2044
2045                                 /* Another entry complete for this locale */
2046                                 aux[x]++;
2047                         }
2048                 }
2049         }
2050
2051         /* Success */
2052         return (0);
2053 }
2054
2055
2056 /*!
2057  * @brief モンスター配列と生成テーブルを初期化する /
2058  * Initialize some other arrays
2059  * @return エラーコード
2060  */
2061 static errr init_alloc(void)
2062 {
2063         int i;
2064         monster_race *r_ptr;
2065
2066 #ifdef SORT_R_INFO
2067
2068         tag_type *elements;
2069
2070         /* Allocate the "r_info" array */
2071         C_MAKE(elements, max_r_idx, tag_type);
2072
2073         /* Scan the monsters */
2074         for (i = 1; i < max_r_idx; i++)
2075         {
2076                 elements[i].tag = r_info[i].level;
2077                 elements[i].index = i;
2078         }
2079
2080         tag_sort(elements, max_r_idx);
2081
2082         /*** Initialize monster allocation info ***/
2083
2084         /* Size of "alloc_race_table" */
2085         alloc_race_size = max_r_idx;
2086
2087         /* Allocate the alloc_race_table */
2088         C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
2089
2090         /* Scan the monsters */
2091         for (i = 1; i < max_r_idx; i++)
2092         {
2093                 /* Get the i'th race */
2094                 r_ptr = &r_info[elements[i].index];
2095
2096                 /* Count valid pairs */
2097                 if (r_ptr->rarity)
2098                 {
2099                         int p, x;
2100
2101                         /* Extract the base level */
2102                         x = r_ptr->level;
2103
2104                         /* Extract the base probability */
2105                         p = (100 / r_ptr->rarity);
2106
2107                         /* Load the entry */
2108                         alloc_race_table[i].index = elements[i].index;
2109                         alloc_race_table[i].level = x;
2110                         alloc_race_table[i].prob1 = p;
2111                         alloc_race_table[i].prob2 = p;
2112                         alloc_race_table[i].prob3 = p;
2113                 }
2114         }
2115
2116         /* Free the "r_info" array */
2117         C_KILL(elements, max_r_idx, tag_type);
2118
2119 #else /* SORT_R_INFO */
2120
2121         int j;
2122         alloc_entry *table;
2123         s16b num[MAX_DEPTH];
2124         s16b aux[MAX_DEPTH];
2125
2126         /*** Analyze monster allocation info ***/
2127
2128         /* Clear the "aux" array */
2129         C_WIPE(&aux, MAX_DEPTH, s16b);
2130
2131         /* Clear the "num" array */
2132         C_WIPE(&num, MAX_DEPTH, s16b);
2133
2134         /* Size of "alloc_race_table" */
2135         alloc_race_size = 0;
2136
2137         /* Scan the monsters */
2138         for (i = 1; i < max_r_idx; i++)
2139         {
2140                 /* Get the i'th race */
2141                 r_ptr = &r_info[i];
2142
2143                 /* Legal monsters */
2144                 if (r_ptr->rarity)
2145                 {
2146                         /* Count the entries */
2147                         alloc_race_size++;
2148
2149                         /* Group by level */
2150                         num[r_ptr->level]++;
2151                 }
2152         }
2153
2154         /* Collect the level indexes */
2155         for (i = 1; i < MAX_DEPTH; i++)
2156         {
2157                 /* Group by level */
2158                 num[i] += num[i-1];
2159         }
2160
2161         /* Paranoia */
2162 #ifdef JP
2163         if (!num[0]) quit("町のモンスターがない!");
2164 #else
2165         if (!num[0]) quit("No town monsters!");
2166 #endif
2167
2168
2169
2170         /*** Initialize monster allocation info ***/
2171
2172         /* Allocate the alloc_race_table */
2173         C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
2174
2175         /* Access the table entry */
2176         table = alloc_race_table;
2177
2178         /* Scan the monsters */
2179         for (i = 1; i < max_r_idx; i++)
2180         {
2181                 /* Get the i'th race */
2182                 r_ptr = &r_info[i];
2183
2184                 /* Count valid pairs */
2185                 if (r_ptr->rarity)
2186                 {
2187                         int p, x, y, z;
2188
2189                         /* Extract the base level */
2190                         x = r_ptr->level;
2191
2192                         /* Extract the base probability */
2193                         p = (100 / r_ptr->rarity);
2194
2195                         /* Skip entries preceding our locale */
2196                         y = (x > 0) ? num[x-1] : 0;
2197
2198                         /* Skip previous entries at this locale */
2199                         z = y + aux[x];
2200
2201                         /* Load the entry */
2202                         table[z].index = i;
2203                         table[z].level = x;
2204                         table[z].prob1 = p;
2205                         table[z].prob2 = p;
2206                         table[z].prob3 = p;
2207
2208                         /* Another entry complete for this locale */
2209                         aux[x]++;
2210                 }
2211         }
2212
2213 #endif /* SORT_R_INFO */
2214
2215         /* Init the "alloc_kind_table" */
2216         (void)init_object_alloc();
2217
2218         /* Success */
2219         return (0);
2220 }
2221
2222
2223
2224 /*!
2225  * @brief 画面左下にシステムメッセージを表示する /
2226  * Hack -- take notes on line 23
2227  * @return なし
2228  */
2229 static void note(cptr str)
2230 {
2231         Term_erase(0, 23, 255);
2232         Term_putstr(20, 23, -1, TERM_WHITE, str);
2233         Term_fresh();
2234 }
2235
2236
2237
2238 /*!
2239  * @brief 全ゲームデータ読み込みのサブルーチン /
2240  * Hack -- Explain a broken "lib" folder and quit (see below).
2241  * @return なし
2242  * @note
2243  * <pre>
2244  * XXX XXX XXX This function is "messy" because various things
2245  * may or may not be initialized, but the "plog()" and "quit()"
2246  * functions are "supposed" to work under any conditions.
2247  * </pre>
2248  */
2249 static void init_angband_aux(cptr why)
2250 {
2251         /* Why */
2252         plog(why);
2253
2254 #ifdef JP
2255         /* Explain */
2256         plog("'lib'ディレクトリが存在しないか壊れているようです。");
2257
2258         /* More details */
2259         plog("ひょっとするとアーカイブが正しく解凍されていないのかもしれません。");
2260
2261         /* Explain */
2262         plog("該当する'README'ファイルを読んで確認してみて下さい。");
2263
2264         /* Quit with error */
2265         quit("致命的なエラー。");
2266 #else
2267         /* Explain */
2268         plog("The 'lib' directory is probably missing or broken.");
2269
2270         /* More details */
2271         plog("Perhaps the archive was not extracted correctly.");
2272
2273         /* Explain */
2274         plog("See the 'README' file for more information.");
2275
2276         /* Quit with error */
2277         quit("Fatal Error.");
2278 #endif
2279
2280 }
2281
2282
2283 /*!
2284  * @brief 全ゲームデータ読み込みのメインルーチン /
2285  * Hack -- main Angband initialization entry point
2286  * @return なし
2287  * @note
2288  * <pre>
2289  * XXX XXX XXX This function is "messy" because various things
2290  * may or may not be initialized, but the "plog()" and "quit()"
2291  * functions are "supposed" to work under any conditions.
2292  * Verify some files, display the "news.txt" file, create
2293  * the high score file, initialize all internal arrays, and
2294  * load the basic "user pref files".
2295  * Be very careful to keep track of the order in which things
2296  * are initialized, in particular, the only thing *known* to
2297  * be available when this function is called is the "z-term.c"
2298  * package, and that may not be fully initialized until the
2299  * end of this function, when the default "user pref files"
2300  * are loaded and "Term_xtra(TERM_XTRA_REACT,0)" is called.
2301  * Note that this function attempts to verify the "news" file,
2302  * and the game aborts (cleanly) on failure, since without the
2303  * "news" file, it is likely that the "lib" folder has not been
2304  * correctly located.  Otherwise, the news file is displayed for
2305  * the user.
2306  * Note that this function attempts to verify (or create) the
2307  * "high score" file, and the game aborts (cleanly) on failure,
2308  * since one of the most common "extraction" failures involves
2309  * failing to extract all sub-directories (even empty ones), such
2310  * as by failing to use the "-d" option of "pkunzip", or failing
2311  * to use the "save empty directories" option with "Compact Pro".
2312  * This error will often be caught by the "high score" creation
2313  * code below, since the "lib/apex" directory, being empty in the
2314  * standard distributions, is most likely to be "lost", making it
2315  * impossible to create the high score file.
2316  * Note that various things are initialized by this function,
2317  * including everything that was once done by "init_some_arrays".
2318  * This initialization involves the parsing of special files
2319  * in the "lib/data" and sometimes the "lib/edit" directories.
2320  * Note that the "template" files are initialized first, since they
2321  * often contain errors.  This means that macros and message recall
2322  * and things like that are not available until after they are done.
2323  * We load the default "user pref files" here in case any "color"
2324  * changes are needed before character creation.
2325  * Note that the "graf-xxx.prf" file must be loaded separately,
2326  * if needed, in the first (?) pass through "TERM_XTRA_REACT".
2327  * </pre>
2328  */
2329 void init_angband(void)
2330 {
2331         int fd = -1;
2332
2333         int mode = 0664;
2334
2335         FILE *fp;
2336
2337         char buf[1024];
2338
2339
2340         /*** Verify the "news" file ***/
2341
2342         /* Build the filename */
2343 #ifdef JP
2344         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news_j.txt");
2345 #else
2346         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news.txt");
2347 #endif
2348
2349
2350         /* Attempt to open the file */
2351         fd = fd_open(buf, O_RDONLY);
2352
2353         /* Failure */
2354         if (fd < 0)
2355         {
2356                 char why[1024];
2357
2358                 /* Message */
2359 #ifdef JP
2360         sprintf(why, "'%s'ファイルにアクセスできません!", buf);
2361 #else
2362                 sprintf(why, "Cannot access the '%s' file!", buf);
2363 #endif
2364
2365
2366                 /* Crash and burn */
2367                 init_angband_aux(why);
2368         }
2369
2370         /* Close it */
2371         (void)fd_close(fd);
2372
2373
2374         /*** Display the "news" file ***/
2375
2376         /* Clear screen */
2377         Term_clear();
2378
2379         /* Build the filename */
2380 #ifdef JP
2381         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news_j.txt");
2382 #else
2383         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, "news.txt");
2384 #endif
2385
2386
2387         /* Open the News file */
2388         fp = my_fopen(buf, "r");
2389
2390         /* Dump */
2391         if (fp)
2392         {
2393                 int i = 0;
2394
2395                 /* Dump the file to the screen */
2396                 while (0 == my_fgets(fp, buf, sizeof(buf)))
2397                 {
2398                         /* Display and advance */
2399                         Term_putstr(0, i++, -1, TERM_WHITE, buf);
2400                 }
2401
2402                 /* Close */
2403                 my_fclose(fp);
2404         }
2405
2406         /* Flush it */
2407         Term_flush();
2408
2409
2410         /*** Verify (or create) the "high score" file ***/
2411
2412         /* Build the filename */
2413         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
2414
2415         /* Attempt to open the high score file */
2416         fd = fd_open(buf, O_RDONLY);
2417
2418         /* Failure */
2419         if (fd < 0)
2420         {
2421                 /* File type is "DATA" */
2422                 FILE_TYPE(FILE_TYPE_DATA);
2423
2424                 /* Grab permissions */
2425                 safe_setuid_grab();
2426
2427                 /* Create a new high score file */
2428                 fd = fd_make(buf, mode);
2429
2430                 /* Drop permissions */
2431                 safe_setuid_drop();
2432
2433                 /* Failure */
2434                 if (fd < 0)
2435                 {
2436                         char why[1024];
2437
2438                         /* Message */
2439 #ifdef JP
2440                         sprintf(why, "'%s'ファイルを作成できません!", buf);
2441 #else
2442                         sprintf(why, "Cannot create the '%s' file!", buf);
2443 #endif
2444
2445
2446                         /* Crash and burn */
2447                         init_angband_aux(why);
2448                 }
2449         }
2450
2451         /* Close it */
2452         (void)fd_close(fd);
2453
2454
2455         /*** Initialize some arrays ***/
2456
2457         /* Initialize misc. values */
2458 #ifdef JP
2459 note("[変数を初期化しています...(その他)");
2460 #else
2461         note("[Initializing values... (misc)]");
2462 #endif
2463
2464 #ifdef JP
2465 if (init_misc()) quit("その他の変数を初期化できません");
2466 #else
2467         if (init_misc()) quit("Cannot initialize misc. values");
2468 #endif
2469
2470
2471         /* Initialize feature info */
2472 #ifdef JP
2473         note("[データの初期化中... (地形)]");
2474         if (init_f_info()) quit("地形初期化不能");
2475         if (init_feat_variables()) quit("地形初期化不能");
2476 #else
2477         note("[Initializing arrays... (features)]");
2478         if (init_f_info()) quit("Cannot initialize features");
2479         if (init_feat_variables()) quit("Cannot initialize features");
2480 #endif
2481
2482
2483         /* Initialize object info */
2484 #ifdef JP
2485         note("[データの初期化中... (アイテム)]");
2486         if (init_k_info()) quit("アイテム初期化不能");
2487 #else
2488         note("[Initializing arrays... (objects)]");
2489         if (init_k_info()) quit("Cannot initialize objects");
2490 #endif
2491
2492
2493         /* Initialize artifact info */
2494 #ifdef JP
2495         note("[データの初期化中... (伝説のアイテム)]");
2496         if (init_a_info()) quit("伝説のアイテム初期化不能");
2497 #else
2498         note("[Initializing arrays... (artifacts)]");
2499         if (init_a_info()) quit("Cannot initialize artifacts");
2500 #endif
2501
2502
2503         /* Initialize ego-item info */
2504 #ifdef JP
2505         note("[データの初期化中... (名のあるアイテム)]");
2506         if (init_e_info()) quit("名のあるアイテム初期化不能");
2507 #else
2508         note("[Initializing arrays... (ego-items)]");
2509         if (init_e_info()) quit("Cannot initialize ego-items");
2510 #endif
2511
2512
2513         /* Initialize monster info */
2514 #ifdef JP
2515         note("[データの初期化中... (モンスター)]");
2516         if (init_r_info()) quit("モンスター初期化不能");
2517 #else
2518         note("[Initializing arrays... (monsters)]");
2519         if (init_r_info()) quit("Cannot initialize monsters");
2520 #endif
2521
2522
2523         /* Initialize dungeon info */
2524 #ifdef JP
2525         note("[データの初期化中... (ダンジョン)]");
2526         if (init_d_info()) quit("ダンジョン初期化不能");
2527 #else
2528         note("[Initializing arrays... (dungeon)]");
2529         if (init_d_info()) quit("Cannot initialize dungeon");
2530 #endif
2531         {
2532                 int i;
2533                 for (i = 1; i < max_d_idx; i++)
2534                         if (d_info[i].final_guardian)
2535                                 r_info[d_info[i].final_guardian].flags7 |= RF7_GUARDIAN;
2536         }
2537
2538         /* Initialize magic info */
2539 #ifdef JP
2540         note("[データの初期化中... (魔法)]");
2541         if (init_m_info()) quit("魔法初期化不能");
2542 #else
2543         note("[Initializing arrays... (magic)]");
2544         if (init_m_info()) quit("Cannot initialize magic");
2545 #endif
2546
2547         /* Initialize weapon_exp info */
2548 #ifdef JP
2549         note("[データの初期化中... (熟練度)]");
2550         if (init_s_info()) quit("熟練度初期化不能");
2551 #else
2552         note("[Initializing arrays... (skill)]");
2553         if (init_s_info()) quit("Cannot initialize skill");
2554 #endif
2555
2556         /* Initialize wilderness array */
2557 #ifdef JP
2558 note("[配列を初期化しています... (荒野)]");
2559 #else
2560         note("[Initializing arrays... (wilderness)]");
2561 #endif
2562
2563 #ifdef JP
2564 if (init_wilderness()) quit("荒野を初期化できません");
2565 #else
2566         if (init_wilderness()) quit("Cannot initialize wilderness");
2567 #endif
2568
2569
2570         /* Initialize town array */
2571 #ifdef JP
2572 note("[配列を初期化しています... (街)]");
2573 #else
2574         note("[Initializing arrays... (towns)]");
2575 #endif
2576
2577 #ifdef JP
2578 if (init_towns()) quit("街を初期化できません");
2579 #else
2580         if (init_towns()) quit("Cannot initialize towns");
2581 #endif
2582
2583
2584         /* Initialize building array */
2585 #ifdef JP
2586 note("[配列を初期化しています... (建物)]");
2587 #else
2588         note("[Initializing arrays... (buildings)]");
2589 #endif
2590
2591 #ifdef JP
2592 if (init_buildings()) quit("建物を初期化できません");
2593 #else
2594         if (init_buildings()) quit("Cannot initialize buildings");
2595 #endif
2596
2597
2598         /* Initialize quest array */
2599 #ifdef JP
2600 note("[配列を初期化しています... (クエスト)]");
2601 #else
2602         note("[Initializing arrays... (quests)]");
2603 #endif
2604
2605 #ifdef JP
2606 if (init_quests()) quit("クエストを初期化できません");
2607 #else
2608         if (init_quests()) quit("Cannot initialize quests");
2609 #endif
2610
2611
2612         /* Initialize vault info */
2613 #ifdef JP
2614         if (init_v_info()) quit("vault 初期化不能");
2615 #else
2616         if (init_v_info()) quit("Cannot initialize vaults");
2617 #endif
2618
2619
2620         /* Initialize some other arrays */
2621 #ifdef JP
2622         note("[データの初期化中... (その他)]");
2623         if (init_other()) quit("その他のデータ初期化不能");
2624 #else
2625         note("[Initializing arrays... (other)]");
2626         if (init_other()) quit("Cannot initialize other stuff");
2627 #endif
2628
2629
2630         /* Initialize some other arrays */
2631 #ifdef JP
2632         /* translation */
2633         note("[データの初期化中... (アロケーション)]");
2634         if (init_alloc()) quit("アロケーション・スタッフ初期化不能");
2635 #else
2636         note("[Initializing arrays... (alloc)]");
2637         if (init_alloc()) quit("Cannot initialize alloc stuff");
2638 #endif
2639
2640
2641
2642         /*** Load default user pref files ***/
2643
2644         /* Initialize feature info */
2645 #ifdef JP
2646 note("[ユーザー設定ファイルを初期化しています...]");
2647 #else
2648         note("[Initializing user pref files...]");
2649 #endif
2650
2651
2652         /* Access the "basic" pref file */
2653         strcpy(buf, "pref.prf");
2654
2655         /* Process that file */
2656         process_pref_file(buf);
2657
2658         /* Access the "basic" system pref file */
2659         sprintf(buf, "pref-%s.prf", ANGBAND_SYS);
2660
2661         /* Process that file */
2662         process_pref_file(buf);
2663
2664         /* Done */
2665 #ifdef JP
2666         note("[初期化終了]");
2667 #else
2668         note("[Initialization complete]");
2669 #endif
2670
2671 }
2672
2673 /*!
2674  * @brief サムチェック情報を出力 / Get check sum in string form
2675  * @return サムチェック情報の文字列
2676  */
2677 cptr get_check_sum(void)
2678 {
2679         return format("%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
2680                       f_head.v_extra, 
2681                       k_head.v_extra, 
2682                       a_head.v_extra, 
2683                       e_head.v_extra, 
2684                       r_head.v_extra, 
2685                       d_head.v_extra, 
2686                       m_head.v_extra, 
2687                       s_head.v_extra, 
2688                       v_head.v_extra);
2689 }
2690