OSDN Git Service

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