OSDN Git Service

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