OSDN Git Service

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