OSDN Git Service

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