OSDN Git Service

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