OSDN Git Service

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