OSDN Git Service

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