OSDN Git Service

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