OSDN Git Service

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