OSDN Git Service

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