OSDN Git Service

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