OSDN Git Service

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