OSDN Git Service

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