OSDN Git Service

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