OSDN Git Service

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