OSDN Git Service

Merge remote-tracking branch 'remotes/origin/feature/Refactoring-Hourier' into For2...
[hengband/hengband.git] / src / init.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 #include "util.h"
37 #include "files.h"
38 #include "core.h"
39 #include "term.h"
40
41 #include "artifact.h"
42 #include "bldg.h"
43 #include "init.h"
44 #include "quest.h"
45 #include "trap.h"
46 #include "rooms.h"
47 #include "store.h"
48 #include "wild.h"
49 #include "dungeon-file.h"
50 #include "files.h"
51 #include "feature.h"
52 #include "floor.h"
53 #include "floor-town.h"
54 #include "dungeon.h"
55 #include "rooms-vault.h"
56 #include "player-skill.h"
57 #include "player-class.h"
58 #include "objectkind.h"
59 #include "object-ego.h"
60 #include "rooms-vault.h"
61 #include "world.h"
62
63 #ifndef MACINTOSH
64 #ifdef CHECK_MODIFICATION_TIME
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #endif /* CHECK_MODIFICATION_TIME */
68 #endif
69
70 static void put_title(void);
71
72 /*!
73  * @brief 各データファイルを読み取るためのパスを取得する
74  * Find the default paths to all of our important sub-directories.
75  * @param path パス保管先の文字列
76  * @return なし
77  * @details
78  * <pre>
79  * The purpose of each sub-directory is described in "variable.c".
80  * All of the sub-directories should, by default, be located inside
81  * the main "lib" directory, whose location is very system dependant.
82  * This function takes a writable buffer, initially containing the
83  * "path" to the "lib" directory, for example, "/pkg/lib/angband/",
84  * or a system dependant string, for example, ":lib:".  The buffer
85  * must be large enough to contain at least 32 more characters.
86  * Various command line options may allow some of the important
87  * directories to be changed to user-specified directories, most
88  * importantly, the "info" and "user" and "save" directories,
89  * but this is done after this function, see "main.c".
90  * In general, the initial path should end in the appropriate "PATH_SEP"
91  * string.  All of the "sub-directory" paths (created below or supplied
92  * by the user) will NOT end in the "PATH_SEP" string, see the special
93  * "path_build()" function in "util.c" for more information.
94  * Mega-Hack -- support fat raw files under NEXTSTEP, using special
95  * "suffixed" directories for the "ANGBAND_DIR_DATA" directory, but
96  * requiring the directories to be created by hand by the user.
97  * Hack -- first we free all the strings, since this is known
98  * to succeed even if the strings have not been allocated yet,
99  * as long as the variables start out as "NULL".  This allows
100  * this function to be called multiple times, for example, to
101  * try several base "path" values until a good one is found.
102  * </pre>
103  */
104 void init_file_paths(char *path)
105 {
106         char *tail;
107
108 #ifdef PRIVATE_USER_PATH
109         char buf[1024];
110 #endif /* PRIVATE_USER_PATH */
111
112         /*** Free everything ***/
113
114         /* Free the main path */
115         string_free(ANGBAND_DIR);
116
117         /* Free the sub-paths */
118         string_free(ANGBAND_DIR_APEX);
119         string_free(ANGBAND_DIR_BONE);
120         string_free(ANGBAND_DIR_DATA);
121         string_free(ANGBAND_DIR_EDIT);
122         string_free(ANGBAND_DIR_SCRIPT);
123         string_free(ANGBAND_DIR_FILE);
124         string_free(ANGBAND_DIR_HELP);
125         string_free(ANGBAND_DIR_INFO);
126         string_free(ANGBAND_DIR_SAVE);
127         string_free(ANGBAND_DIR_USER);
128         string_free(ANGBAND_DIR_XTRA);
129
130         /*** Prepare the "path" ***/
131
132         /* Hack -- save the main directory */
133         ANGBAND_DIR = string_make(path);
134
135         /* Prepare to append to the Base Path */
136         tail = path + strlen(path);
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
201 #ifdef NeXT
202
203         /* Allow "fat binary" usage with NeXT */
204         if (TRUE)
205         {
206                 concptr next = NULL;
207
208 # if defined(m68k)
209                 next = "m68k";
210 # endif
211
212 # if defined(i386)
213                 next = "i386";
214 # endif
215
216 # if defined(sparc)
217                 next = "sparc";
218 # endif
219
220 # if defined(hppa)
221                 next = "hppa";
222 # endif
223
224                 /* Use special directory */
225                 if (next)
226                 {
227                         /* Forget the old path name */
228                         string_free(ANGBAND_DIR_DATA);
229
230                         /* Build a new path name */
231                         sprintf(tail, "data-%s", next);
232                         ANGBAND_DIR_DATA = string_make(path);
233                 }
234         }
235
236 #endif /* NeXT */
237
238 }
239
240
241 #ifdef ALLOW_TEMPLATES
242
243 /*
244  * Hack -- help give useful error messages
245  */
246 int error_idx; /*!< データ読み込み/初期化時に汎用的にエラーコードを保存するグローバル変数 */
247 int error_line; /*!< データ読み込み/初期化時に汎用的にエラー行数を保存するグローバル変数 */
248
249 /*!
250  * エラーメッセージの名称定義 / Standard error message text
251  */
252 concptr err_str[PARSE_ERROR_MAX] =
253 {
254         NULL,
255 #ifdef JP
256         "文法エラー",
257         "古いファイル",
258         "記録ヘッダがない",
259         "不連続レコード",
260         "おかしなフラグ存在",
261         "未定義命令",
262         "メモリ不足",
263         "座標範囲外",
264         "引数不足",
265         "未定義地形タグ",
266 #else
267         "parse error",
268         "obsolete file",
269         "missing record header",
270         "non-sequential records",
271         "invalid flag specification",
272         "undefined directive",
273         "out of memory",
274         "coordinates out of bounds",
275         "too few arguments",
276         "undefined terrain tag",
277 #endif
278
279 };
280
281 #endif /* ALLOW_TEMPLATES */
282
283
284 /*
285  * File headers
286  */
287 header v_head; /*!< Vault情報のヘッダ構造体 */
288 header f_head; /*!< 地形情報のヘッダ構造体 */
289 header k_head; /*!< ペースアイテム情報のヘッダ構造体 */
290 header a_head; /*!< 固定アーティファクト情報のヘッダ構造体 */
291 header e_head; /*!< アイテムエゴ情報のヘッダ構造体 */
292 header r_head; /*!< モンスター種族情報のヘッダ構造体 */
293 header d_head; /*!< ダンジョン情報のヘッダ構造体 */
294 header s_head; /*!< プレイヤー職業技能情報のヘッダ構造体 */
295 header m_head; /*!< プレイヤー職業魔法情報のヘッダ構造体 */
296
297 #ifdef CHECK_MODIFICATION_TIME
298
299 /*!
300  * @brief テキストファイルとrawファイルの更新時刻を比較する
301  * Find the default paths to all of our important sub-directories.
302  * @param fd ファイルディスクリプタ
303  * @param template_file ファイル名
304  * @return テキストの方が新しいか、rawファイルがなく更新の必要がある場合-1、更新の必要がない場合0。
305  */
306 static errr check_modification_date(int fd, concptr template_file)
307 {
308         struct stat txt_stat, raw_stat;
309         char buf[1024];
310         path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, template_file);
311
312         /* Access stats on text file */
313         if (stat(buf, &txt_stat))
314         {
315                 return 0;
316         }
317
318         /* Access stats on raw file */
319         if (fstat(fd, &raw_stat))
320         {
321                 return -1;
322         }
323
324         /* Ensure text file is not newer than raw file */
325         if (txt_stat.st_mtime > raw_stat.st_mtime)
326         {
327                 return -1;
328         }
329
330         return 0;
331 }
332
333 #endif /* CHECK_MODIFICATION_TIME */
334
335 /*** Initialize from binary image files ***/
336
337 /*!
338  * @brief rawファイルからのデータの読み取り処理
339  * Initialize the "*_info" array, by parsing a binary "image" file
340  * @param fd ファイルディスクリプタ
341  * @param head rawファイルのヘッダ
342  * @return エラーコード
343  */
344 static errr init_info_raw(int fd, header *head)
345 {
346         header test;
347
348         /* Read and Verify the header */
349         if (fd_read(fd, (char*)(&test), sizeof(header)) ||
350                 (test.v_major != head->v_major) ||
351                 (test.v_minor != head->v_minor) ||
352                 (test.v_patch != head->v_patch) ||
353                 (test.info_num != head->info_num) ||
354                 (test.info_len != head->info_len) ||
355                 (test.head_size != head->head_size) ||
356                 (test.info_size != head->info_size))
357         {
358                 /* Error */
359                 return -1;
360         }
361
362         /* Accept the header */
363         (*head) = test;
364
365         /* Allocate the "*_info" array */
366         C_MAKE(head->info_ptr, head->info_size, char);
367
368         /* Read the "*_info" array */
369         fd_read(fd, head->info_ptr, head->info_size);
370
371         if (head->name_size)
372         {
373                 /* Allocate the "*_name" array */
374                 C_MAKE(head->name_ptr, head->name_size, char);
375
376                 /* Read the "*_name" array */
377                 fd_read(fd, head->name_ptr, head->name_size);
378         }
379
380         if (head->text_size)
381         {
382                 /* Allocate the "*_text" array */
383                 C_MAKE(head->text_ptr, head->text_size, char);
384
385                 /* Read the "*_text" array */
386                 fd_read(fd, head->text_ptr, head->text_size);
387         }
388
389         if (head->tag_size)
390         {
391                 /* Allocate the "*_tag" array */
392                 C_MAKE(head->tag_ptr, head->tag_size, char);
393
394                 /* Read the "*_tag" array */
395                 fd_read(fd, head->tag_ptr, head->tag_size);
396         }
397
398         return 0;
399 }
400
401
402
403 /*!
404  * @brief ヘッダ構造体の更新
405  * Initialize the header of an *_info.raw file.
406  * @param head rawファイルのヘッダ
407  * @param num データ数
408  * @param len データの長さ
409  * @return エラーコード
410  */
411 static void init_header(header *head, IDX num, int len)
412 {
413         /* Save the "version" */
414         head->v_major = FAKE_VER_MAJOR;
415         head->v_minor = FAKE_VER_MINOR;
416         head->v_patch = FAKE_VER_PATCH;
417         head->v_extra = 0;
418
419         /* Save the "record" information */
420         head->info_num = (IDX)num;
421         head->info_len = len;
422
423         /* Save the size of "*_head" and "*_info" */
424         head->head_size = sizeof(header);
425         head->info_size = head->info_num * head->info_len;
426 }
427
428
429 /*!
430  * todo プリプロがゴミすぎて整理不可能
431  * @brief ヘッダ構造体の更新
432  * Initialize the "*_info" array
433  * @param filename ファイル名(拡張子txt/raw)
434  * @param head 処理に用いるヘッダ構造体
435  * @param info データ保管先の構造体ポインタ
436  * @param name 名称用可変文字列の保管先
437  * @param text テキスト用可変文字列の保管先
438  * @param tag タグ用可変文字列の保管先
439  * @return エラーコード
440  * @note
441  * Note that we let each entry have a unique "name" and "text" string,
442  * even if the string happens to be empty (everyone has a unique '\0').
443  */
444 static errr init_info(concptr filename, header *head, void **info, char **name, char **text, char **tag)
445 {
446         FILE *fp;
447
448         /* General buffer */
449         char buf[1024];
450
451 #ifdef ALLOW_TEMPLATES
452
453         /*** Load the binary image file ***/
454         path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format(_("%s_j.raw", "%s.raw"), filename));
455
456         /* Attempt to open the "raw" file */
457         int fd = fd_open(buf, O_RDONLY);
458
459         /* Process existing "raw" file */
460         errr err = 1;
461         if (fd >= 0)
462         {
463 #ifdef CHECK_MODIFICATION_TIME
464
465                 err = check_modification_date(fd, format("%s.txt", filename));
466
467 #endif /* CHECK_MODIFICATION_TIME */
468
469                 /* Attempt to parse the "raw" file */
470                 if (!err)
471                         err = init_info_raw(fd, head);
472                 (void)fd_close(fd);
473         }
474
475         /* Do we have to parse the *.txt file? */
476         BIT_FLAGS file_permission = 0644;
477         if (err)
478         {
479                 /*** Make the fake arrays ***/
480
481                 /* Allocate the "*_info" array */
482                 C_MAKE(head->info_ptr, head->info_size, char);
483
484                 /* Hack -- make "fake" arrays */
485                 if (name) C_MAKE(head->name_ptr, FAKE_NAME_SIZE, char);
486                 if (text) C_MAKE(head->text_ptr, FAKE_TEXT_SIZE, char);
487                 if (tag)  C_MAKE(head->tag_ptr, FAKE_TAG_SIZE, char);
488
489                 if (info) (*info) = head->info_ptr;
490                 if (name) (*name) = head->name_ptr;
491                 if (text) (*text) = head->text_ptr;
492                 if (tag)  (*tag) = head->tag_ptr;
493
494                 /*** Load the ascii template file ***/
495
496                 path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, format("%s.txt", filename));
497                 fp = my_fopen(buf, "r");
498
499                 /* Parse it */
500                 if (!fp) quit(format(_("'%s.txt'ファイルをオープンできません。", "Cannot open '%s.txt' file."), filename));
501
502                 /* Parse the file */
503                 err = init_info_txt(fp, buf, head, head->parse_info_txt);
504                 my_fclose(fp);
505
506                 /* Errors */
507                 if (err)
508                 {
509                         concptr oops;
510
511 #ifdef JP
512                         /* Error string */
513                         oops = (((err > 0) && (err < PARSE_ERROR_MAX)) ? err_str[err] : "未知の");
514
515                         msg_format("'%s.txt'ファイルの %d 行目にエラー。", filename, error_line);
516                         msg_format("レコード %d は '%s' エラーがあります。", error_idx, oops);
517                         msg_format("構文 '%s'。", buf);
518                         msg_print(NULL);
519
520                         /* Quit */
521                         quit(format("'%s.txt'ファイルにエラー", filename));
522 #else
523                         /* Error string */
524                         oops = (((err > 0) && (err < PARSE_ERROR_MAX)) ? err_str[err] : "unknown");
525
526                         msg_format("Error %d at line %d of '%s.txt'.", err, error_line, filename);
527                         msg_format("Record %d contains a '%s' error.", error_idx, oops);
528                         msg_format("Parsing '%s'.", buf);
529                         msg_print(NULL);
530
531                         /* Quit */
532                         quit(format("Error in '%s.txt' file.", filename));
533 #endif
534
535                 }
536
537
538                 /*** Make final retouch on fake tags ***/
539
540                 if (head->retouch)
541                 {
542                         (*head->retouch)(head);
543                 }
544
545
546                 /*** Dump the binary image file ***/
547
548                 /* File type is "DATA" */
549                 FILE_TYPE(FILE_TYPE_DATA);
550                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format(_("%s_j.raw", "%s.raw"), filename));
551
552
553                 /* Grab permissions */
554                 safe_setuid_grab();
555
556                 /* Kill the old file */
557                 (void)fd_kill(buf);
558
559                 /* Attempt to create the raw file */
560                 fd = fd_make(buf, file_permission);
561
562                 /* Drop permissions */
563                 safe_setuid_drop();
564
565                 /* Dump to the file */
566                 if (fd >= 0)
567                 {
568                         /* Dump it */
569                         fd_write(fd, (concptr)(head), head->head_size);
570
571                         /* Dump the "*_info" array */
572                         fd_write(fd, head->info_ptr, head->info_size);
573
574                         /* Dump the "*_name" array */
575                         fd_write(fd, head->name_ptr, head->name_size);
576
577                         /* Dump the "*_text" array */
578                         fd_write(fd, head->text_ptr, head->text_size);
579
580                         /* Dump the "*_tag" array */
581                         fd_write(fd, head->tag_ptr, head->tag_size);
582
583                         /* Close */
584                         (void)fd_close(fd);
585                 }
586
587                 /*** Kill the fake arrays ***/
588
589                 /* Free the "*_info" array */
590                 C_KILL(head->info_ptr, head->info_size, char);
591
592                 /* Hack -- Free the "fake" arrays */
593                 if (name) C_KILL(head->name_ptr, FAKE_NAME_SIZE, char);
594                 if (text) C_KILL(head->text_ptr, FAKE_TEXT_SIZE, char);
595                 if (tag)  C_KILL(head->tag_ptr, FAKE_TAG_SIZE, char);
596
597 #endif  /* ALLOW_TEMPLATES */
598
599
600                 /*** Load the binary image file ***/
601                 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format(_("%s_j.raw", "%s.raw"), filename));
602
603                 /* Attempt to open the "raw" file */
604                 fd = fd_open(buf, O_RDONLY);
605
606                 /* Process existing "raw" file */
607                 if (fd < 0) quit(format(_("'%s_j.raw'ファイルをロードできません。", "Cannot load '%s.raw' file."), filename));
608
609                 /* Attempt to parse the "raw" file */
610                 err = init_info_raw(fd, head);
611                 (void)fd_close(fd);
612
613                 /* Error */
614                 if (err) quit(format(_("'%s_j.raw'ファイルを解析できません。", "Cannot parse '%s.raw' file."), filename));
615
616 #ifdef ALLOW_TEMPLATES
617         }
618 #endif
619
620         if (info) *info = head->info_ptr;
621         if (name) *name = head->name_ptr;
622         if (text) *text = head->text_ptr;
623         if (tag)  *tag = head->tag_ptr;
624         return 0;
625 }
626
627
628 /*!
629  * @brief 地形情報読み込みのメインルーチン /
630  * Initialize the "f_info" array
631  * @return エラーコード
632  */
633 static errr init_f_info(void)
634 {
635         /* Init the header */
636         init_header(&f_head, max_f_idx, sizeof(feature_type));
637
638 #ifdef ALLOW_TEMPLATES
639
640         /* Save a pointer to the parsing function */
641         f_head.parse_info_txt = parse_f_info;
642
643         /* Save a pointer to the retouch fake tags */
644         f_head.retouch = retouch_f_info;
645
646 #endif /* ALLOW_TEMPLATES */
647
648         return init_info("f_info", &f_head,
649                 (void*)&f_info, &f_name, NULL, &f_tag);
650 }
651
652
653 /*!
654  * @brief ベースアイテム情報読み込みのメインルーチン /
655  * Initialize the "k_info" array
656  * @return エラーコード
657  */
658 static errr init_k_info(void)
659 {
660         /* Init the header */
661         init_header(&k_head, max_k_idx, sizeof(object_kind));
662
663 #ifdef ALLOW_TEMPLATES
664
665         /* Save a pointer to the parsing function */
666         k_head.parse_info_txt = parse_k_info;
667
668 #endif /* ALLOW_TEMPLATES */
669
670         return init_info("k_info", &k_head,
671                 (void*)&k_info, &k_name, &k_text, NULL);
672 }
673
674
675 /*!
676  * @brief 固定アーティファクト情報読み込みのメインルーチン /
677  * Initialize the "a_info" array
678  * @return エラーコード
679  */
680 static errr init_a_info(void)
681 {
682         /* Init the header */
683         init_header(&a_head, max_a_idx, sizeof(artifact_type));
684
685 #ifdef ALLOW_TEMPLATES
686
687         /* Save a pointer to the parsing function */
688         a_head.parse_info_txt = parse_a_info;
689
690 #endif /* ALLOW_TEMPLATES */
691
692         return init_info("a_info", &a_head,
693                 (void*)&a_info, &a_name, &a_text, NULL);
694 }
695
696
697 /*!
698  * @brief 固定アーティファクト情報読み込みのメインルーチン /
699  * Initialize the "e_info" array
700  * @return エラーコード
701  */
702 static errr init_e_info(void)
703 {
704         /* Init the header */
705         init_header(&e_head, max_e_idx, sizeof(ego_item_type));
706
707 #ifdef ALLOW_TEMPLATES
708
709         /* Save a pointer to the parsing function */
710         e_head.parse_info_txt = parse_e_info;
711
712 #endif /* ALLOW_TEMPLATES */
713
714         return init_info("e_info", &e_head,
715                 (void*)&e_info, &e_name, &e_text, NULL);
716 }
717
718
719 /*!
720  * @brief モンスター種族情報読み込みのメインルーチン /
721  * Initialize the "r_info" array
722  * @return エラーコード
723  */
724 static errr init_r_info(void)
725 {
726         /* Init the header */
727         init_header(&r_head, max_r_idx, sizeof(monster_race));
728
729 #ifdef ALLOW_TEMPLATES
730
731         /* Save a pointer to the parsing function */
732         r_head.parse_info_txt = parse_r_info;
733
734 #endif /* ALLOW_TEMPLATES */
735
736         return init_info("r_info", &r_head,
737                 (void*)&r_info, &r_name, &r_text, NULL);
738 }
739
740
741 /*!
742  * @brief ダンジョン情報読み込みのメインルーチン /
743  * Initialize the "d_info" array
744  * @return エラーコード
745  */
746 static errr init_d_info(void)
747 {
748         /* Init the header */
749         init_header(&d_head, current_world_ptr->max_d_idx, sizeof(dungeon_type));
750
751 #ifdef ALLOW_TEMPLATES
752
753         /* Save a pointer to the parsing function */
754         d_head.parse_info_txt = parse_d_info;
755
756 #endif /* ALLOW_TEMPLATES */
757
758         return init_info("d_info", &d_head,
759                 (void*)&d_info, &d_name, &d_text, NULL);
760 }
761
762
763 /*!
764  * @brief Vault情報読み込みのメインルーチン /
765  * Initialize the "v_info" array
766  * @return エラーコード
767  * @note
768  * Note that we let each entry have a unique "name" and "text" string,
769  * even if the string happens to be empty (everyone has a unique '\0').
770  */
771 errr init_v_info(void)
772 {
773         /* Init the header */
774         init_header(&v_head, max_v_idx, sizeof(vault_type));
775
776 #ifdef ALLOW_TEMPLATES
777
778         /* Save a pointer to the parsing function */
779         v_head.parse_info_txt = parse_v_info;
780
781 #endif /* ALLOW_TEMPLATES */
782
783         return init_info("v_info", &v_head,
784                 (void*)&v_info, &v_name, &v_text, NULL);
785 }
786
787
788 /*!
789  * @brief 職業技能情報読み込みのメインルーチン /
790  * Initialize the "s_info" array
791  * @return エラーコード
792  */
793 static errr init_s_info(void)
794 {
795         /* Init the header */
796         init_header(&s_head, MAX_CLASS, sizeof(skill_table));
797
798 #ifdef ALLOW_TEMPLATES
799
800         /* Save a pointer to the parsing function */
801         s_head.parse_info_txt = parse_s_info;
802
803 #endif /* ALLOW_TEMPLATES */
804
805         return init_info("s_info", &s_head,
806                 (void*)&s_info, NULL, NULL, NULL);
807 }
808
809
810 /*!
811  * @brief 職業魔法情報読み込みのメインルーチン /
812  * Initialize the "m_info" array
813  * @return エラーコード
814  */
815 static errr init_m_info(void)
816 {
817         /* Init the header */
818         init_header(&m_head, MAX_CLASS, sizeof(player_magic));
819
820 #ifdef ALLOW_TEMPLATES
821
822         /* Save a pointer to the parsing function */
823         m_head.parse_info_txt = parse_m_info;
824
825 #endif /* ALLOW_TEMPLATES */
826
827         return init_info("m_info", &m_head,
828                 (void*)&m_info, NULL, NULL, NULL);
829 }
830
831
832 /*!
833  * @brief 基本情報読み込みのメインルーチン /
834  * Initialize misc. values
835  * @param player_ptr プレーヤーへの参照ポインタ
836  * @return エラーコード
837  */
838 static errr init_misc(player_type *player_ptr)
839 {
840         return process_dungeon_file(player_ptr, "misc.txt", 0, 0, 0, 0);
841 }
842
843
844 /*!
845  * @brief 町情報読み込みのメインルーチン /
846  * Initialize town array
847  * @return エラーコード
848  */
849 static errr init_towns(void)
850 {
851         /* Allocate the towns */
852         C_MAKE(town_info, max_towns, town_type);
853
854         for (int i = 1; i < max_towns; i++)
855         {
856                 /*** Prepare the Stores ***/
857
858                 /* Allocate the stores */
859                 C_MAKE(town_info[i].store, MAX_STORES, store_type);
860
861                 /* Fill in each store */
862                 for (int j = 0; j < MAX_STORES; j++)
863                 {
864                         /* Access the store */
865                         store_type *st_ptr = &town_info[i].store[j];
866
867                         if ((i > 1) && (j == STORE_MUSEUM || j == STORE_HOME)) continue;
868
869                         /* Assume full stock */
870
871                         /*
872                          * 我が家が 20 ページまで使える隠し機能のための準備。
873                          * オプションが有効でもそうでなくても一応スペースを作っておく。
874                          */
875                         if (j == STORE_HOME)
876                         {
877                                 st_ptr->stock_size = (STORE_INVEN_MAX * 10);
878                         }
879                         else if (j == STORE_MUSEUM)
880                         {
881                                 st_ptr->stock_size = (STORE_INVEN_MAX * 50);
882                         }
883                         else
884                         {
885                                 st_ptr->stock_size = STORE_INVEN_MAX;
886                         }
887
888                         /* Allocate the stock */
889                         C_MAKE(st_ptr->stock, st_ptr->stock_size, object_type);
890
891                         /* No table for the black market or home */
892                         if ((j == STORE_BLACK) || (j == STORE_HOME) || (j == STORE_MUSEUM)) continue;
893
894                         /* Assume full table */
895                         st_ptr->table_size = STORE_CHOICES;
896
897                         /* Allocate the stock */
898                         C_MAKE(st_ptr->table, st_ptr->table_size, s16b);
899
900                         /* Scan the choices */
901                         for (int k = 0; k < STORE_CHOICES; k++)
902                         {
903                                 KIND_OBJECT_IDX k_idx;
904
905                                 /* Extract the tval/sval codes */
906                                 int tv = store_table[j][k][0];
907                                 int sv = store_table[j][k][1];
908
909                                 /* Look for it */
910                                 for (k_idx = 1; k_idx < max_k_idx; k_idx++)
911                                 {
912                                         object_kind *k_ptr = &k_info[k_idx];
913
914                                         /* Found a match */
915                                         if ((k_ptr->tval == tv) && (k_ptr->sval == sv)) break;
916                                 }
917
918                                 /* Catch errors */
919                                 if (k_idx == max_k_idx) continue;
920
921                                 /* Add that item index to the table */
922                                 st_ptr->table[st_ptr->table_num++] = k_idx;
923                         }
924                 }
925         }
926
927         return 0;
928 }
929
930 /*!
931  * @brief 店情報初期化のメインルーチン /
932  * Initialize buildings
933  * @return エラーコード
934  */
935 errr init_buildings(void)
936 {
937         for (int i = 0; i < MAX_BLDG; i++)
938         {
939                 building[i].name[0] = '\0';
940                 building[i].owner_name[0] = '\0';
941                 building[i].owner_race[0] = '\0';
942
943                 for (int j = 0; j < 8; j++)
944                 {
945                         building[i].act_names[j][0] = '\0';
946                         building[i].member_costs[j] = 0;
947                         building[i].other_costs[j] = 0;
948                         building[i].letters[j] = 0;
949                         building[i].actions[j] = 0;
950                         building[i].action_restr[j] = 0;
951                 }
952
953                 for (int j = 0; j < MAX_CLASS; j++)
954                 {
955                         building[i].member_class[j] = 0;
956                 }
957
958                 for (int j = 0; j < MAX_RACES; j++)
959                 {
960                         building[i].member_race[j] = 0;
961                 }
962
963                 for (int j = 0; j < MAX_MAGIC + 1; j++)
964                 {
965                         building[i].member_realm[j] = 0;
966                 }
967         }
968
969         return 0;
970 }
971
972
973 /*!
974  * @brief クエスト情報初期化のメインルーチン /
975  * Initialize quest array
976  * @return エラーコード
977  */
978 static errr init_quests(void)
979 {
980         /* Allocate the quests */
981         C_MAKE(quest, max_q_idx, quest_type);
982
983         /* Set all quest to "untaken" */
984         for (int i = 0; i < max_q_idx; i++)
985         {
986                 quest[i].status = QUEST_STATUS_UNTAKEN;
987         }
988
989         return 0;
990 }
991
992 /*! 地形タグ情報から地形IDを得られなかった場合にTRUEを返すグローバル変数 */
993 static bool feat_tag_is_not_found = FALSE;
994
995 /*!
996  * @brief 地形タグからIDを得る /
997  * Initialize quest array
998  * @return 地形ID
999  */
1000 s16b f_tag_to_index_in_init(concptr str)
1001 {
1002         FEAT_IDX feat = f_tag_to_index(str);
1003
1004         if (feat < 0) feat_tag_is_not_found = TRUE;
1005
1006         return feat;
1007 }
1008
1009
1010 /*!
1011  * @brief 地形の汎用定義をタグを通じて取得する /
1012  * Initialize feature variables
1013  * @return エラーコード
1014  */
1015 static errr init_feat_variables(void)
1016 {
1017         feat_none = f_tag_to_index_in_init("NONE");
1018
1019         feat_floor = f_tag_to_index_in_init("FLOOR");
1020         feat_glyph = f_tag_to_index_in_init("GLYPH");
1021         feat_explosive_rune = f_tag_to_index_in_init("EXPLOSIVE_RUNE");
1022         feat_mirror = f_tag_to_index_in_init("MIRROR");
1023         
1024         feat_door[DOOR_DOOR].open = f_tag_to_index_in_init("OPEN_DOOR");
1025         feat_door[DOOR_DOOR].broken = f_tag_to_index_in_init("BROKEN_DOOR");
1026         feat_door[DOOR_DOOR].closed = f_tag_to_index_in_init("CLOSED_DOOR");
1027
1028         /* Locked doors */
1029         FEAT_IDX i;
1030         for (i = 1; i < MAX_LJ_DOORS; i++)
1031         {
1032                 s16b door = f_tag_to_index(format("LOCKED_DOOR_%d", i));
1033                 if (door < 0) break;
1034                 feat_door[DOOR_DOOR].locked[i - 1] = door;
1035         }
1036
1037         if (i == 1) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1038         feat_door[DOOR_DOOR].num_locked = i - 1;
1039
1040         /* Jammed doors */
1041         for (i = 0; i < MAX_LJ_DOORS; i++)
1042         {
1043                 s16b door = f_tag_to_index(format("JAMMED_DOOR_%d", i));
1044                 if (door < 0) break;
1045                 feat_door[DOOR_DOOR].jammed[i] = door;
1046         }
1047
1048         if (!i) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1049         feat_door[DOOR_DOOR].num_jammed = i;
1050
1051         /* Glass doors */
1052         feat_door[DOOR_GLASS_DOOR].open = f_tag_to_index_in_init("OPEN_GLASS_DOOR");
1053         feat_door[DOOR_GLASS_DOOR].broken = f_tag_to_index_in_init("BROKEN_GLASS_DOOR");
1054         feat_door[DOOR_GLASS_DOOR].closed = f_tag_to_index_in_init("CLOSED_GLASS_DOOR");
1055
1056         /* Locked glass doors */
1057         for (i = 1; i < MAX_LJ_DOORS; i++)
1058         {
1059                 s16b door = f_tag_to_index(format("LOCKED_GLASS_DOOR_%d", i));
1060                 if (door < 0) break;
1061                 feat_door[DOOR_GLASS_DOOR].locked[i - 1] = door;
1062         }
1063
1064         if (i == 1) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1065         feat_door[DOOR_GLASS_DOOR].num_locked = i - 1;
1066
1067         /* Jammed glass doors */
1068         for (i = 0; i < MAX_LJ_DOORS; i++)
1069         {
1070                 s16b door = f_tag_to_index(format("JAMMED_GLASS_DOOR_%d", i));
1071                 if (door < 0) break;
1072                 feat_door[DOOR_GLASS_DOOR].jammed[i] = door;
1073         }
1074
1075         if (!i) return PARSE_ERROR_UNDEFINED_TERRAIN_TAG;
1076         feat_door[DOOR_GLASS_DOOR].num_jammed = i;
1077
1078         /* Curtains */
1079         feat_door[DOOR_CURTAIN].open = f_tag_to_index_in_init("OPEN_CURTAIN");
1080         feat_door[DOOR_CURTAIN].broken = feat_door[DOOR_CURTAIN].open;
1081         feat_door[DOOR_CURTAIN].closed = f_tag_to_index_in_init("CLOSED_CURTAIN");
1082         feat_door[DOOR_CURTAIN].locked[0] = feat_door[DOOR_CURTAIN].closed;
1083         feat_door[DOOR_CURTAIN].num_locked = 1;
1084         feat_door[DOOR_CURTAIN].jammed[0] = feat_door[DOOR_CURTAIN].closed;
1085         feat_door[DOOR_CURTAIN].num_jammed = 1;
1086
1087         /* Stairs */
1088         feat_up_stair = f_tag_to_index_in_init("UP_STAIR");
1089         feat_down_stair = f_tag_to_index_in_init("DOWN_STAIR");
1090         feat_entrance = f_tag_to_index_in_init("ENTRANCE");
1091
1092         /* Normal traps */
1093         init_normal_traps();
1094
1095         /* Special traps */
1096         feat_trap_open = f_tag_to_index_in_init("TRAP_OPEN");
1097         feat_trap_armageddon = f_tag_to_index_in_init("TRAP_ARMAGEDDON");
1098         feat_trap_piranha = f_tag_to_index_in_init("TRAP_PIRANHA");
1099
1100         /* Rubble */
1101         feat_rubble = f_tag_to_index_in_init("RUBBLE");
1102
1103         /* Seams */
1104         feat_magma_vein = f_tag_to_index_in_init("MAGMA_VEIN");
1105         feat_quartz_vein = f_tag_to_index_in_init("QUARTZ_VEIN");
1106
1107         /* Walls */
1108         feat_granite = f_tag_to_index_in_init("GRANITE");
1109         feat_permanent = f_tag_to_index_in_init("PERMANENT");
1110
1111         /* Glass floor */
1112         feat_glass_floor = f_tag_to_index_in_init("GLASS_FLOOR");
1113
1114         /* Glass walls */
1115         feat_glass_wall = f_tag_to_index_in_init("GLASS_WALL");
1116         feat_permanent_glass_wall = f_tag_to_index_in_init("PERMANENT_GLASS_WALL");
1117
1118         /* Pattern */
1119         feat_pattern_start = f_tag_to_index_in_init("PATTERN_START");
1120         feat_pattern_1 = f_tag_to_index_in_init("PATTERN_1");
1121         feat_pattern_2 = f_tag_to_index_in_init("PATTERN_2");
1122         feat_pattern_3 = f_tag_to_index_in_init("PATTERN_3");
1123         feat_pattern_4 = f_tag_to_index_in_init("PATTERN_4");
1124         feat_pattern_end = f_tag_to_index_in_init("PATTERN_END");
1125         feat_pattern_old = f_tag_to_index_in_init("PATTERN_OLD");
1126         feat_pattern_exit = f_tag_to_index_in_init("PATTERN_EXIT");
1127         feat_pattern_corrupted = f_tag_to_index_in_init("PATTERN_CORRUPTED");
1128
1129         /* Various */
1130         feat_black_market = f_tag_to_index_in_init("BLACK_MARKET");
1131         feat_town = f_tag_to_index_in_init("TOWN");
1132
1133         /* Terrains */
1134         feat_deep_water = f_tag_to_index_in_init("DEEP_WATER");
1135         feat_shallow_water = f_tag_to_index_in_init("SHALLOW_WATER");
1136         feat_deep_lava = f_tag_to_index_in_init("DEEP_LAVA");
1137         feat_shallow_lava = f_tag_to_index_in_init("SHALLOW_LAVA");
1138         feat_heavy_cold_zone = f_tag_to_index_in_init("HEAVY_COLD_ZONE");
1139         feat_cold_zone = f_tag_to_index_in_init("COLD_ZONE");
1140         feat_heavy_electrical_zone = f_tag_to_index_in_init("HEAVY_ELECTRICAL_ZONE");
1141         feat_electrical_zone = f_tag_to_index_in_init("ELECTRICAL_ZONE");
1142         feat_deep_acid_puddle = f_tag_to_index_in_init("DEEP_ACID_PUDDLE");
1143         feat_shallow_acid_puddle = f_tag_to_index_in_init("SHALLOW_ACID_PUDDLE");
1144         feat_deep_poisonous_puddle = f_tag_to_index_in_init("DEEP_POISONOUS_PUDDLE");
1145         feat_shallow_poisonous_puddle = f_tag_to_index_in_init("SHALLOW_POISONOUS_PUDDLE");
1146         feat_dirt = f_tag_to_index_in_init("DIRT");
1147         feat_grass = f_tag_to_index_in_init("GRASS");
1148         feat_flower = f_tag_to_index_in_init("FLOWER");
1149         feat_brake = f_tag_to_index_in_init("BRAKE");
1150         feat_tree = f_tag_to_index_in_init("TREE");
1151         feat_mountain = f_tag_to_index_in_init("MOUNTAIN");
1152         feat_swamp = f_tag_to_index_in_init("SWAMP");
1153
1154         feat_undetected = f_tag_to_index_in_init("UNDETECTED");
1155
1156         init_wilderness_terrains();
1157         return feat_tag_is_not_found ? PARSE_ERROR_UNDEFINED_TERRAIN_TAG : 0;
1158 }
1159
1160
1161 /*!
1162  * @brief その他の初期情報更新 /
1163  * Initialize some other arrays
1164  * @return エラーコード
1165  */
1166 static errr init_other(player_type *player_ptr)
1167 {
1168         player_ptr->current_floor_ptr = &floor_info; // TODO:本当はこんなところで初期化したくない
1169         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1170
1171         /*** Prepare the "dungeon" information ***/
1172
1173         /* Allocate and Wipe the object list */
1174         C_MAKE(floor_ptr->o_list, current_world_ptr->max_o_idx, object_type);
1175
1176         /* Allocate and Wipe the monster list */
1177         C_MAKE(floor_ptr->m_list, current_world_ptr->max_m_idx, monster_type);
1178
1179         /* Allocate and Wipe the monster process list */
1180         for (int i = 0; i < MAX_MTIMED; i++)
1181         {
1182                 C_MAKE(floor_ptr->mproc_list[i], current_world_ptr->max_m_idx, s16b);
1183         }
1184
1185         /* Allocate and Wipe the max dungeon level */
1186         C_MAKE(max_dlv, current_world_ptr->max_d_idx, DEPTH);
1187
1188         for (int i = 0; i < MAX_HGT; i++)
1189         {
1190                 C_MAKE(floor_ptr->grid_array[i], MAX_WID, grid_type);
1191         }
1192
1193         /*** Prepare the various "bizarre" arrays ***/
1194
1195         /* Macro variables */
1196         C_MAKE(macro__pat, MACRO_MAX, concptr);
1197         C_MAKE(macro__act, MACRO_MAX, concptr);
1198         C_MAKE(macro__cmd, MACRO_MAX, bool);
1199
1200         /* Macro action buffer */
1201         C_MAKE(macro__buf, 1024, char);
1202
1203         /* Quark variables */
1204         quark_init();
1205
1206         /* Message variables */
1207         C_MAKE(message__ptr, MESSAGE_MAX, u32b);
1208         C_MAKE(message__buf, MESSAGE_BUF, char);
1209
1210         /* Hack -- No messages yet */
1211         message__tail = MESSAGE_BUF;
1212
1213         /*** Prepare the options ***/
1214
1215         /* Scan the options */
1216         for (int i = 0; option_info[i].o_desc; i++)
1217         {
1218                 int os = option_info[i].o_set;
1219                 int ob = option_info[i].o_bit;
1220
1221                 /* Set the "default" options */
1222                 if (option_info[i].o_var)
1223                 {
1224                         /* Accept */
1225                         option_mask[os] |= (1L << ob);
1226
1227                         /* Set */
1228                         if (option_info[i].o_norm)
1229                         {
1230                                 /* Set */
1231                                 option_flag[os] |= (1L << ob);
1232                         }
1233                         else
1234                         {
1235                                 option_flag[os] &= ~(1L << ob);
1236                         }
1237                 }
1238         }
1239
1240         /* Analyze the windows */
1241         for (int n = 0; n < 8; n++)
1242         {
1243                 /* Analyze the options */
1244                 for (int i = 0; i < 32; i++)
1245                 {
1246                         /* Accept */
1247                         if (window_flag_desc[i])
1248                         {
1249                                 /* Accept */
1250                                 window_mask[n] |= (1L << i);
1251                         }
1252                 }
1253         }
1254
1255         /*
1256          *  Set the "default" window flags
1257          *  Window 1 : Display messages
1258          *  Window 2 : Display inven/equip
1259          */
1260         window_flag[1] = 1L << A_MAX;
1261         window_flag[2] = 1L << 0;
1262
1263         /*** Pre-allocate space for the "format()" buffer ***/
1264
1265         /* Hack -- Just call the "format()" function */
1266         (void)format("%s (%s).", "Mr.Hoge", MAINTAINER);
1267         return 0;
1268 }
1269
1270
1271 /*!
1272  * @brief オブジェクト配列を初期化する /
1273  * Initialize some other arrays
1274  * @return エラーコード
1275  */
1276 static errr init_object_alloc(void)
1277 {
1278         s16b aux[MAX_DEPTH];
1279         (void)C_WIPE(&aux, MAX_DEPTH, s16b);
1280
1281         s16b num[MAX_DEPTH];
1282         (void)C_WIPE(&num, MAX_DEPTH, s16b);
1283
1284         /* Free the old "alloc_kind_table" (if it exists) */
1285         if (alloc_kind_table)
1286         {
1287                 C_KILL(alloc_kind_table, alloc_kind_size, alloc_entry);
1288         }
1289
1290         /* Size of "alloc_kind_table" */
1291         alloc_kind_size = 0;
1292
1293         /* Scan the objects */
1294         for (int i = 1; i < max_k_idx; i++)
1295         {
1296                 object_kind *k_ptr;
1297                 k_ptr = &k_info[i];
1298
1299                 /* Scan allocation pairs */
1300                 for (int j = 0; j < 4; j++)
1301                 {
1302                         /* Count the "legal" entries */
1303                         if (k_ptr->chance[j])
1304                         {
1305                                 /* Count the entries */
1306                                 alloc_kind_size++;
1307
1308                                 /* Group by level */
1309                                 num[k_ptr->locale[j]]++;
1310                         }
1311                 }
1312         }
1313
1314         /* Collect the level indexes */
1315         for (int i = 1; i < MAX_DEPTH; i++)
1316         {
1317                 /* Group by level */
1318                 num[i] += num[i - 1];
1319         }
1320
1321         if (!num[0]) quit(_("町のアイテムがない!", "No town objects!"));
1322
1323         /*** Initialize object allocation info ***/
1324
1325         /* Allocate the alloc_kind_table */
1326         C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry);
1327
1328         /* Access the table entry */
1329         alloc_entry *table;
1330         table = alloc_kind_table;
1331
1332         /* Scan the objects */
1333         for (int i = 1; i < max_k_idx; i++)
1334         {
1335                 object_kind *k_ptr;
1336                 k_ptr = &k_info[i];
1337
1338                 /* Scan allocation pairs */
1339                 for (int j = 0; j < 4; j++)
1340                 {
1341                         /* Count the "legal" entries */
1342                         if (k_ptr->chance[j] == 0) continue;
1343
1344                         /* Extract the base level */
1345                         int x = k_ptr->locale[j];
1346
1347                         /* Extract the base probability */
1348                         int p = (100 / k_ptr->chance[j]);
1349
1350                         /* Skip entries preceding our locale */
1351                         int y = (x > 0) ? num[x - 1] : 0;
1352
1353                         /* Skip previous entries at this locale */
1354                         int z = y + aux[x];
1355
1356                         /* Load the entry */
1357                         table[z].index = (KIND_OBJECT_IDX)i;
1358                         table[z].level = (DEPTH)x;
1359                         table[z].prob1 = (PROB)p;
1360                         table[z].prob2 = (PROB)p;
1361                         table[z].prob3 = (PROB)p;
1362
1363                         /* Another entry complete for this locale */
1364                         aux[x]++;
1365                 }
1366         }
1367
1368         return 0;
1369 }
1370
1371
1372 /*!
1373  * @brief モンスター配列と生成テーブルを初期化する /
1374  * Initialize some other arrays
1375  * @return エラーコード
1376  */
1377 static errr init_alloc(void)
1378 {
1379         monster_race *r_ptr;
1380
1381 #ifdef SORT_R_INFO
1382
1383         tag_type *elements;
1384
1385         /* Allocate the "r_info" array */
1386         C_MAKE(elements, max_r_idx, tag_type);
1387
1388         /* Scan the monsters */
1389         for (int i = 1; i < max_r_idx; i++)
1390         {
1391                 elements[i].tag = r_info[i].level;
1392                 elements[i].index = i;
1393         }
1394
1395         tag_sort(elements, max_r_idx);
1396
1397         /*** Initialize monster allocation info ***/
1398
1399         /* Size of "alloc_race_table" */
1400         alloc_race_size = max_r_idx;
1401
1402         /* Allocate the alloc_race_table */
1403         C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
1404
1405         /* Scan the monsters */
1406         for (int i = 1; i < max_r_idx; i++)
1407         {
1408                 /* Get the i'th race */
1409                 r_ptr = &r_info[elements[i].index];
1410
1411                 /* Count valid pairs */
1412                 if (r_ptr->rarity == 0) continue;
1413
1414                 /* Extract the base level */
1415                 int x = r_ptr->level;
1416
1417                 /* Extract the base probability */
1418                 int p = (100 / r_ptr->rarity);
1419
1420                 /* Load the entry */
1421                 alloc_race_table[i].index = (KIND_OBJECT_IDX)elements[i].index;
1422                 alloc_race_table[i].level = (DEPTH)x;
1423                 alloc_race_table[i].prob1 = (PROB)p;
1424                 alloc_race_table[i].prob2 = (PROB)p;
1425                 alloc_race_table[i].prob3 = (PROB)p;
1426         }
1427
1428         /* Free the "r_info" array */
1429         C_KILL(elements, max_r_idx, tag_type);
1430
1431 #else /* SORT_R_INFO */
1432
1433         alloc_entry *table;
1434         s16b num[MAX_DEPTH];
1435         s16b aux[MAX_DEPTH];
1436
1437         /*** Analyze monster allocation info ***/
1438
1439         /* Clear the "aux" array */
1440         C_WIPE(&aux, MAX_DEPTH, s16b);
1441
1442         /* Clear the "num" array */
1443         C_WIPE(&num, MAX_DEPTH, s16b);
1444
1445         /* Size of "alloc_race_table" */
1446         alloc_race_size = 0;
1447
1448         /* Scan the monsters */
1449         for (int i = 1; i < max_r_idx; i++)
1450         {
1451                 /* Get the i'th race */
1452                 r_ptr = &r_info[i];
1453
1454                 /* Legal monsters */
1455                 if (r_ptr->rarity)
1456                 {
1457                         /* Count the entries */
1458                         alloc_race_size++;
1459
1460                         /* Group by level */
1461                         num[r_ptr->level]++;
1462                 }
1463         }
1464
1465         /* Collect the level indexes */
1466         for (int i = 1; i < MAX_DEPTH; i++)
1467         {
1468                 /* Group by level */
1469                 num[i] += num[i - 1];
1470         }
1471         if (!num[0]) quit(_("町のモンスターがない!", "No town monsters!"));
1472
1473         /*** Initialize monster allocation info ***/
1474
1475         /* Allocate the alloc_race_table */
1476         C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
1477
1478         /* Access the table entry */
1479         table = alloc_race_table;
1480
1481         /* Scan the monsters */
1482         for (int i = 1; i < max_r_idx; i++)
1483         {
1484                 /* Get the i'th race */
1485                 r_ptr = &r_info[i];
1486
1487                 /* Count valid pairs */
1488                 if (r_ptr->rarity)
1489                 {
1490                         int p, x, y, z;
1491
1492                         /* Extract the base level */
1493                         x = r_ptr->level;
1494
1495                         /* Extract the base probability */
1496                         p = (100 / r_ptr->rarity);
1497
1498                         /* Skip entries preceding our locale */
1499                         y = (x > 0) ? num[x - 1] : 0;
1500
1501                         /* Skip previous entries at this locale */
1502                         z = y + aux[x];
1503
1504                         /* Load the entry */
1505                         table[z].index = i;
1506                         table[z].level = x;
1507                         table[z].prob1 = p;
1508                         table[z].prob2 = p;
1509                         table[z].prob3 = p;
1510
1511                         /* Another entry complete for this locale */
1512                         aux[x]++;
1513                 }
1514         }
1515
1516 #endif /* SORT_R_INFO */
1517
1518         (void)init_object_alloc();
1519         return 0;
1520 }
1521
1522
1523 /*!
1524  * @brief 画面左下にシステムメッセージを表示する /
1525  * Hack -- take notes on line 23
1526  * @return なし
1527  */
1528 static void note(concptr str)
1529 {
1530         Term_erase(0, 23, 255);
1531         Term_putstr(20, 23, -1, TERM_WHITE, str);
1532         Term_fresh();
1533 }
1534
1535
1536 /*!
1537  * @brief 全ゲームデータ読み込みのサブルーチン /
1538  * Hack -- Explain a broken "lib" folder and quit (see below).
1539  * @return なし
1540  * @note
1541  * <pre>
1542  * This function is "messy" because various things
1543  * may or may not be initialized, but the "plog()" and "quit()"
1544  * functions are "supposed" to work under any conditions.
1545  * </pre>
1546  */
1547 static void init_angband_aux(concptr why)
1548 {
1549         plog(why);
1550
1551 #ifdef JP
1552         /* Explain */
1553         plog("'lib'ディレクトリが存在しないか壊れているようです。");
1554
1555         /* More details */
1556         plog("ひょっとするとアーカイブが正しく解凍されていないのかもしれません。");
1557
1558         /* Explain */
1559         plog("該当する'README'ファイルを読んで確認してみて下さい。");
1560
1561         /* Quit with error */
1562         quit("致命的なエラー。");
1563 #else
1564         /* Explain */
1565         plog("The 'lib' directory is probably missing or broken.");
1566
1567         /* More details */
1568         plog("Perhaps the archive was not extracted correctly.");
1569
1570         /* Explain */
1571         plog("See the 'README' file for more information.");
1572
1573         /* Quit with error */
1574         quit("Fatal Error.");
1575 #endif
1576
1577 }
1578
1579
1580 /*!
1581  * @brief 全ゲームデータ読み込みのメインルーチン /
1582  * Hack -- main Angband initialization entry point
1583  * @return なし
1584  * @note
1585  * <pre>
1586  * This function is "messy" because various things
1587  * may or may not be initialized, but the "plog()" and "quit()"
1588  * functions are "supposed" to work under any conditions.
1589  * Verify some files, display the "news.txt" file, create
1590  * the high score file, initialize all internal arrays, and
1591  * load the basic "user pref files".
1592  * Be very careful to keep track of the order in which things
1593  * are initialized, in particular, the only thing *known* to
1594  * be available when this function is called is the "z-term.c"
1595  * package, and that may not be fully initialized until the
1596  * end of this function, when the default "user pref files"
1597  * are loaded and "Term_xtra(TERM_XTRA_REACT,0)" is called.
1598  * Note that this function attempts to verify the "news" file,
1599  * and the game aborts (cleanly) on failure, since without the
1600  * "news" file, it is likely that the "lib" folder has not been
1601  * correctly located.  Otherwise, the news file is displayed for
1602  * the user.
1603  * Note that this function attempts to verify (or create) the
1604  * "high score" file, and the game aborts (cleanly) on failure,
1605  * since one of the most common "extraction" failures involves
1606  * failing to extract all sub-directories (even empty ones), such
1607  * as by failing to use the "-d" option of "pkunzip", or failing
1608  * to use the "save empty directories" option with "Compact Pro".
1609  * This error will often be caught by the "high score" creation
1610  * code below, since the "lib/apex" directory, being empty in the
1611  * standard distributions, is most likely to be "lost", making it
1612  * impossible to create the high score file.
1613  * Note that various things are initialized by this function,
1614  * including everything that was once done by "init_some_arrays".
1615  * This initialization involves the parsing of special files
1616  * in the "lib/data" and sometimes the "lib/edit" directories.
1617  * Note that the "template" files are initialized first, since they
1618  * often contain errors.  This means that macros and message recall
1619  * and things like that are not available until after they are done.
1620  * We load the default "user pref files" here in case any "color"
1621  * changes are needed before character creation.
1622  * Note that the "graf-xxx.prf" file must be loaded separately,
1623  * if needed, in the first (?) pass through "TERM_XTRA_REACT".
1624  * </pre>
1625  */
1626 void init_angband(player_type *player_ptr)
1627 {
1628         /*** Verify the "news" file ***/
1629         char buf[1024];
1630         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
1631
1632         /* Attempt to open the file */
1633         int fd = fd_open(buf, O_RDONLY);
1634
1635         /* Failure */
1636         if (fd < 0)
1637         {
1638                 char why[1024];
1639
1640                 sprintf(why, _("'%s'ファイルにアクセスできません!", "Cannot access the '%s' file!"), buf);
1641
1642                 /* Crash and burn */
1643                 init_angband_aux(why);
1644         }
1645
1646         (void)fd_close(fd);
1647
1648         /*** Display the "news" file ***/
1649         Term_clear();
1650         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
1651
1652         /* Open the News file */
1653         FILE *fp;
1654         fp = my_fopen(buf, "r");
1655
1656         /* Dump */
1657         if (fp)
1658         {
1659                 /* Dump the file to the screen */
1660                 int i = 0;
1661                 while (0 == my_fgets(fp, buf, sizeof(buf)))
1662                 {
1663                         /* Display and advance */
1664                         Term_putstr(0, i++, -1, TERM_WHITE, buf);
1665                 }
1666
1667                 my_fclose(fp);
1668         }
1669
1670         Term_flush();
1671
1672         /*** Verify (or create) the "high score" file ***/
1673         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
1674
1675         /* Attempt to open the high score file */
1676         fd = fd_open(buf, O_RDONLY);
1677
1678         BIT_FLAGS file_permission = 0664;
1679         if (fd < 0)
1680         {
1681                 /* File type is "DATA" */
1682                 FILE_TYPE(FILE_TYPE_DATA);
1683
1684                 /* Grab permissions */
1685                 safe_setuid_grab();
1686
1687                 /* Create a new high score file */
1688                 fd = fd_make(buf, file_permission);
1689
1690                 /* Drop permissions */
1691                 safe_setuid_drop();
1692
1693                 /* Failure */
1694                 if (fd < 0)
1695                 {
1696                         char why[1024];
1697
1698                         sprintf(why, _("'%s'ファイルを作成できません!", "Cannot create the '%s' file!"), buf);
1699
1700                         /* Crash and burn */
1701                         init_angband_aux(why);
1702                 }
1703         }
1704
1705         (void)fd_close(fd);
1706
1707         put_title();
1708
1709         /*** Initialize some arrays ***/
1710
1711         /* Initialize misc. values */
1712         note(_("[変数を初期化しています...(その他)", "[Initializing values... (misc)]"));
1713         if (init_misc(player_ptr)) quit(_("その他の変数を初期化できません", "Cannot initialize misc. values"));
1714
1715         /* Initialize feature info */
1716 #ifdef JP
1717         note("[データの初期化中... (地形)]");
1718         if (init_f_info()) quit("地形初期化不能");
1719         if (init_feat_variables()) quit("地形初期化不能");
1720 #else
1721         note("[Initializing arrays... (features)]");
1722         if (init_f_info()) quit("Cannot initialize features");
1723         if (init_feat_variables()) quit("Cannot initialize features");
1724 #endif
1725
1726         /* Initialize object info */
1727         note(_("[データの初期化中... (アイテム)]", "[Initializing arrays... (objects)]"));
1728         if (init_k_info()) quit(_("アイテム初期化不能", "Cannot initialize objects"));
1729
1730         /* Initialize artifact info */
1731         note(_("[データの初期化中... (伝説のアイテム)]", "[Initializing arrays... (artifacts)]"));
1732         if (init_a_info()) quit(_("伝説のアイテム初期化不能", "Cannot initialize artifacts"));
1733
1734
1735         /* Initialize ego-item info */
1736         note(_("[データの初期化中... (名のあるアイテム)]", "[Initializing arrays... (ego-items)]"));
1737         if (init_e_info()) quit(_("名のあるアイテム初期化不能", "Cannot initialize ego-items"));
1738
1739
1740         /* Initialize monster info */
1741         note(_("[データの初期化中... (モンスター)]", "[Initializing arrays... (monsters)]"));
1742         if (init_r_info()) quit(_("モンスター初期化不能", "Cannot initialize monsters"));
1743
1744
1745         /* Initialize dungeon info */
1746         note(_("[データの初期化中... (ダンジョン)]", "[Initializing arrays... (dungeon)]"));
1747         if (init_d_info()) quit(_("ダンジョン初期化不能", "Cannot initialize dungeon"));
1748         {
1749                 for (int i = 1; i < current_world_ptr->max_d_idx; i++)
1750                         if (d_info[i].final_guardian)
1751                                 r_info[d_info[i].final_guardian].flags7 |= RF7_GUARDIAN;
1752         }
1753
1754         /* Initialize magic info */
1755         note(_("[データの初期化中... (魔法)]", "[Initializing arrays... (magic)]"));
1756         if (init_m_info()) quit(_("魔法初期化不能", "Cannot initialize magic"));
1757
1758         /* Initialize weapon_exp info */
1759         note(_("[データの初期化中... (熟練度)]", "[Initializing arrays... (skill)]"));
1760         if (init_s_info()) quit(_("熟練度初期化不能", "Cannot initialize skill"));
1761
1762         /* Initialize wilderness array */
1763         note(_("[配列を初期化しています... (荒野)]", "[Initializing arrays... (wilderness)]"));
1764
1765         if (init_wilderness()) quit(_("荒野を初期化できません", "Cannot initialize wilderness"));
1766
1767         /* Initialize town array */
1768         note(_("[配列を初期化しています... (街)]", "[Initializing arrays... (towns)]"));
1769         if (init_towns()) quit(_("街を初期化できません", "Cannot initialize towns"));
1770
1771         /* Initialize building array */
1772         note(_("[配列を初期化しています... (建物)]", "[Initializing arrays... (buildings)]"));
1773         if (init_buildings()) quit(_("建物を初期化できません", "Cannot initialize buildings"));
1774
1775         /* Initialize quest array */
1776         note(_("[配列を初期化しています... (クエスト)]", "[Initializing arrays... (quests)]"));
1777         if (init_quests()) quit(_("クエストを初期化できません", "Cannot initialize quests"));
1778
1779         /* Initialize vault info */
1780         if (init_v_info()) quit(_("vault 初期化不能", "Cannot initialize vaults"));
1781
1782         /* Initialize some other arrays */
1783         note(_("[データの初期化中... (その他)]", "[Initializing arrays... (other)]"));
1784         if (init_other(player_ptr)) quit(_("その他のデータ初期化不能", "Cannot initialize other stuff"));
1785
1786         /* Initialize some other arrays */
1787         note(_("[データの初期化中... (アロケーション)]", "[Initializing arrays... (alloc)]"));
1788         if (init_alloc()) quit(_("アロケーション・スタッフ初期化不能", "Cannot initialize alloc stuff"));
1789
1790         /*** Load default user pref files ***/
1791
1792         /* Initialize feature info */
1793         note(_("[ユーザー設定ファイルを初期化しています...]", "[Initializing user pref files...]"));
1794
1795         /* Access the "basic" pref file */
1796         strcpy(buf, "pref.prf");
1797
1798         /* Process that file */
1799         process_pref_file(player_ptr, buf);
1800
1801         /* Access the "basic" system pref file */
1802         sprintf(buf, "pref-%s.prf", ANGBAND_SYS);
1803
1804         /* Process that file */
1805         process_pref_file(player_ptr, buf);
1806
1807         note(_("[初期化終了]", "[Initialization complete]"));
1808 }
1809
1810
1811 /*!
1812  * @brief タイトル記述
1813  * @return なし
1814  */
1815 static void put_title(void)
1816 {
1817         char title[120];
1818 #if H_VER_EXTRA > 0
1819         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,
1820 #else
1821         sprintf(title, _("変愚蛮怒 %d.%d.%d(%s)", "Hengband %d.%d.%d(%s)"), H_VER_MAJOR, H_VER_MINOR, H_VER_PATCH,
1822 #endif
1823                 IS_STABLE_VERSION ? _("安定版", "Stable") : _("開発版", "Developing"));
1824         int col = (80 - strlen(title)) / 2;
1825         col = col < 0 ? 0 : col;
1826         prt(title, VER_INFO_ROW, col);
1827 }
1828
1829
1830 /*!
1831  * @brief サムチェック情報を出力 / Get check sum in string form
1832  * @return サムチェック情報の文字列
1833  */
1834 concptr get_check_sum(void)
1835 {
1836         return format("%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1837                 f_head.v_extra,
1838                 k_head.v_extra,
1839                 a_head.v_extra,
1840                 e_head.v_extra,
1841                 r_head.v_extra,
1842                 d_head.v_extra,
1843                 m_head.v_extra,
1844                 s_head.v_extra,
1845                 v_head.v_extra);
1846 }