OSDN Git Service

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