OSDN Git Service

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