OSDN Git Service

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