OSDN Git Service

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