OSDN Git Service

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