OSDN Git Service

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