2 * @brief ゲームデータ初期化2 / Initialization (part 2) -BEN-
6 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7 * This software may be copied and distributed for educational, research,
8 * and not for profit purposes provided that this copyright and statement
9 * are included in all such copies. Other copyrights may also apply.
10 * 2014 Deskull rearranged comment for Doxygen.\n
14 * This file is used to initialize various variables and arrays for the
15 * Angband game. Note the use of "fd_read()" and "fd_write()" to bypass
16 * the common limitation of "read()" and "write()" to only 32767 bytes
18 * Several of the arrays for Angband are built from "template" files in
19 * the "lib/file" directory, from which quick-load binary "image" files
20 * are constructed whenever they are not present in the "lib/data"
21 * directory, or if those files become obsolete, if we are allowed.
22 * Warning -- the "ascii" file parsers use a minor hack to collect the
23 * name and text information in a single pass. Thus, the game will not
24 * be able to load any template file with more than 20K of names or 60K
25 * of text, even though technically, up to 64K should be legal.
26 * The "init1.c" file is used only to parse the ascii template files,
27 * to create the binary image files. Note that the binary image files
28 * are extremely system dependant.
32 #include "main/init.h"
33 #include "cmd-building/cmd-building.h"
34 #include "cmd-io/macro-util.h"
35 #include "dungeon/dungeon.h"
36 #include "dungeon/quest.h"
37 #include "floor/floor-town.h"
38 #include "floor/floor-util.h"
39 #include "floor/wild.h"
40 #include "game-option/option-flags.h"
41 #include "game-option/option-types-table.h"
42 #include "grid/feature.h"
43 #include "grid/grid.h"
44 #include "grid/trap.h"
45 #include "info-reader/artifact-reader.h"
46 #include "info-reader/dungeon-reader.h"
47 #include "info-reader/ego-reader.h"
48 #include "info-reader/feature-reader.h"
49 #include "info-reader/general-parser.h"
50 #include "info-reader/kind-reader.h"
51 #include "info-reader/magic-reader.h"
52 #include "info-reader/parse-error-types.h"
53 #include "info-reader/race-reader.h"
54 #include "info-reader/skill-reader.h"
55 #include "info-reader/vault-reader.h"
56 #include "io/files-util.h"
57 #include "io/read-pref-file.h"
58 #include "io/uid-checker.h"
59 #include "main/angband-headers.h"
60 #include "main/info-initializer.h"
61 #include "market/articles-on-sale.h"
62 #include "monster-race/monster-race.h"
63 #include "monster-race/race-flags7.h"
64 #include "object-enchant/object-ego.h"
65 #include "object/object-kind.h"
66 #include "player/player-class.h"
67 #include "player/player-skill.h"
68 #include "room/rooms-builder.h"
69 #include "room/rooms-vault.h"
70 #include "store/store-util.h"
71 #include "store/store.h"
72 #include "system/alloc-entries.h"
73 #include "system/angband-version.h"
74 #include "system/artifact-type-definition.h"
75 #include "system/building-type-definition.h"
76 #include "system/floor-type-definition.h"
77 #include "system/system-variables.h"
78 #include "term/gameterm.h"
79 #include "term/screen-processor.h"
80 #include "term/term-color-types.h"
81 #include "util/angband-files.h"
82 #include "util/quarks.h"
83 #include "util/tag-sorter.h"
84 #include "view/display-messages.h"
85 #include "world/world.h"
88 * @brief マクロ登録の最大数 / Maximum number of macros (see "io.c")
89 * @note Default: assume at most 256 macros are used
91 static const int MACRO_MAX = 256;
94 * @brief 各データファイルを読み取るためのパスを取得する
95 * Find the default paths to all of our important sub-directories.
96 * @param path パス保管先の文字列
100 * The purpose of each sub-directory is described in "variable.c".
101 * All of the sub-directories should, by default, be located inside
102 * the main "lib" directory, whose location is very system dependant.
103 * This function takes a writable buffer, initially containing the
104 * "path" to the "lib" directory, for example, "/pkg/lib/angband/",
105 * or a system dependant string, for example, ":lib:". The buffer
106 * must be large enough to contain at least 32 more characters.
107 * Various command line options may allow some of the important
108 * directories to be changed to user-specified directories, most
109 * importantly, the "info" and "user" and "save" directories,
110 * but this is done after this function, see "main.c".
111 * In general, the initial path should end in the appropriate "PATH_SEP"
112 * string. All of the "sub-directory" paths (created below or supplied
113 * by the user) will NOT end in the "PATH_SEP" string, see the special
114 * "path_build()" function in "util.c" for more information.
115 * Mega-Hack -- support fat raw files under NEXTSTEP, using special
116 * "suffixed" directories for the "ANGBAND_DIR_DATA" directory, but
117 * requiring the directories to be created by hand by the user.
118 * Hack -- first we free all the strings, since this is known
119 * to succeed even if the strings have not been allocated yet,
120 * as long as the variables start out as "NULL". This allows
121 * this function to be called multiple times, for example, to
122 * try several base "path" values until a good one is found.
125 void init_file_paths(char *path)
128 #ifdef PRIVATE_USER_PATH
131 string_free(ANGBAND_DIR);
132 string_free(ANGBAND_DIR_APEX);
133 string_free(ANGBAND_DIR_BONE);
134 string_free(ANGBAND_DIR_DATA);
135 string_free(ANGBAND_DIR_EDIT);
136 string_free(ANGBAND_DIR_SCRIPT);
137 string_free(ANGBAND_DIR_FILE);
138 string_free(ANGBAND_DIR_HELP);
139 string_free(ANGBAND_DIR_INFO);
140 string_free(ANGBAND_DIR_SAVE);
141 string_free(ANGBAND_DIR_USER);
142 string_free(ANGBAND_DIR_XTRA);
144 ANGBAND_DIR = string_make(path);
145 tail = path + strlen(path);
146 strcpy(tail, "apex");
147 ANGBAND_DIR_APEX = string_make(path);
148 strcpy(tail, "bone");
149 ANGBAND_DIR_BONE = string_make(path);
150 strcpy(tail, "data");
151 ANGBAND_DIR_DATA = string_make(path);
152 strcpy(tail, "edit");
153 ANGBAND_DIR_EDIT = string_make(path);
154 strcpy(tail, "script");
155 ANGBAND_DIR_SCRIPT = string_make(path);
156 strcpy(tail, "file");
157 ANGBAND_DIR_FILE = string_make(path);
158 strcpy(tail, "help");
159 ANGBAND_DIR_HELP = string_make(path);
160 strcpy(tail, "info");
161 ANGBAND_DIR_INFO = string_make(path);
162 strcpy(tail, "pref");
163 ANGBAND_DIR_PREF = string_make(path);
164 strcpy(tail, "save");
165 ANGBAND_DIR_SAVE = string_make(path);
166 #ifdef PRIVATE_USER_PATH
167 path_build(buf, sizeof(buf), PRIVATE_USER_PATH, VERSION_NAME);
168 ANGBAND_DIR_USER = string_make(buf);
170 strcpy(tail, "user");
171 ANGBAND_DIR_USER = string_make(path);
173 strcpy(tail, "xtra");
174 ANGBAND_DIR_XTRA = string_make(path);
178 * Hack -- help give useful error messages
180 int error_idx; /*!< データ読み込み/初期化時に汎用的にエラーコードを保存するグローバル変数 */
181 int error_line; /*!< データ読み込み/初期化時に汎用的にエラー行数を保存するグローバル変数 */
184 * エラーメッセージの名称定義 / Standard error message text
186 concptr err_str[PARSE_ERROR_MAX] = {
188 _("文法エラー", "parse error"),
189 _("古いファイル", "obsolete file"),
190 _("記録ヘッダがない", "missing record header"),
191 _("不連続レコード", "non-sequential records"),
192 _("おかしなフラグ存在", "invalid flag specification"),
193 _("未定義命令", "undefined directive"),
194 _("メモリ不足", "out of memory"),
195 _("座標範囲外", "coordinates out of bounds"),
196 _("引数不足", "too few arguments"),
197 _("未定義地形タグ", "undefined terrain tag"),
201 * @brief 町情報読み込みのメインルーチン /
202 * Initialize town array
205 static errr init_towns(void)
207 C_MAKE(town_info, max_towns, town_type);
208 for (int i = 1; i < max_towns; i++) {
209 C_MAKE(town_info[i].store, MAX_STORES, store_type);
210 for (int j = 0; j < MAX_STORES; j++) {
211 store_type *store_ptr = &town_info[i].store[j];
212 if ((i > 1) && (j == STORE_MUSEUM || j == STORE_HOME))
216 * 我が家が 20 ページまで使える隠し機能のための準備。
217 * オプションが有効でもそうでなくても一応スペースを作っておく。
219 if (j == STORE_HOME) {
220 store_ptr->stock_size = (STORE_INVEN_MAX * 10);
221 } else if (j == STORE_MUSEUM) {
222 store_ptr->stock_size = (STORE_INVEN_MAX * 50);
224 store_ptr->stock_size = STORE_INVEN_MAX;
227 C_MAKE(store_ptr->stock, store_ptr->stock_size, object_type);
228 if ((j == STORE_BLACK) || (j == STORE_HOME) || (j == STORE_MUSEUM))
231 store_ptr->table_size = STORE_CHOICES;
232 C_MAKE(store_ptr->table, store_ptr->table_size, s16b);
233 for (int k = 0; k < STORE_CHOICES; k++) {
234 KIND_OBJECT_IDX k_idx;
235 int tv = store_table[j][k][0];
236 int sv = store_table[j][k][1];
237 for (k_idx = 1; k_idx < max_k_idx; k_idx++) {
238 object_kind *k_ptr = &k_info[k_idx];
239 if ((k_ptr->tval == tv) && (k_ptr->sval == sv))
243 if (k_idx == max_k_idx)
246 store_ptr->table[store_ptr->table_num++] = k_idx;
255 * @brief 店情報初期化のメインルーチン /
256 * Initialize buildings
259 errr init_buildings(void)
261 for (int i = 0; i < MAX_BLDG; i++) {
262 building[i].name[0] = '\0';
263 building[i].owner_name[0] = '\0';
264 building[i].owner_race[0] = '\0';
266 for (int j = 0; j < 8; j++) {
267 building[i].act_names[j][0] = '\0';
268 building[i].member_costs[j] = 0;
269 building[i].other_costs[j] = 0;
270 building[i].letters[j] = 0;
271 building[i].actions[j] = 0;
272 building[i].action_restr[j] = 0;
275 for (int j = 0; j < MAX_CLASS; j++)
276 building[i].member_class[j] = 0;
278 for (int j = 0; j < MAX_RACES; j++)
279 building[i].member_race[j] = 0;
281 for (int j = 0; j < MAX_MAGIC + 1; j++)
282 building[i].member_realm[j] = 0;
289 * @brief クエスト情報初期化のメインルーチン /
290 * Initialize quest array
293 static errr init_quests(void)
295 C_MAKE(quest, max_q_idx, quest_type);
296 for (int i = 0; i < max_q_idx; i++)
297 quest[i].status = QUEST_STATUS_UNTAKEN;
303 * @brief その他の初期情報更新 /
304 * Initialize some other arrays
307 static errr init_other(player_type *player_ptr)
309 player_ptr->current_floor_ptr = &floor_info; // TODO:本当はこんなところで初期化したくない
310 floor_type *floor_ptr = player_ptr->current_floor_ptr;
311 C_MAKE(floor_ptr->o_list, current_world_ptr->max_o_idx, object_type);
312 C_MAKE(floor_ptr->m_list, current_world_ptr->max_m_idx, monster_type);
313 for (int i = 0; i < MAX_MTIMED; i++)
314 C_MAKE(floor_ptr->mproc_list[i], current_world_ptr->max_m_idx, s16b);
316 C_MAKE(max_dlv, current_world_ptr->max_d_idx, DEPTH);
317 for (int i = 0; i < MAX_HGT; i++)
318 C_MAKE(floor_ptr->grid_array[i], MAX_WID, grid_type);
320 C_MAKE(macro__pat, MACRO_MAX, concptr);
321 C_MAKE(macro__act, MACRO_MAX, concptr);
322 C_MAKE(macro__cmd, MACRO_MAX, bool);
323 C_MAKE(macro__buf, 1024, char);
326 C_MAKE(message__ptr, MESSAGE_MAX, u32b);
327 C_MAKE(message__buf, MESSAGE_BUF, char);
328 message__tail = MESSAGE_BUF;
330 for (int i = 0; option_info[i].o_desc; i++) {
331 int os = option_info[i].o_set;
332 int ob = option_info[i].o_bit;
333 if (!option_info[i].o_var)
336 option_mask[os] |= (1L << ob);
337 if (option_info[i].o_norm)
338 option_flag[os] |= (1L << ob);
340 option_flag[os] &= ~(1L << ob);
343 for (int n = 0; n < 8; n++)
344 for (int i = 0; i < 32; i++)
345 if (window_flag_desc[i])
346 window_mask[n] |= (1L << i);
349 * Set the "default" window flags
350 * Window 1 : Display messages
351 * Window 2 : Display inven/equip
353 window_flag[1] = 1L << A_MAX;
354 window_flag[2] = 1L << 0;
355 (void)format("%s (%s).", "Mr.Hoge", MAINTAINER);
360 * @brief オブジェクト配列を初期化する /
361 * Initialize some other arrays
364 static errr init_object_alloc(void)
367 (void)C_WIPE(&aux, MAX_DEPTH, s16b);
370 (void)C_WIPE(&num, MAX_DEPTH, s16b);
372 if (alloc_kind_table)
373 C_KILL(alloc_kind_table, alloc_kind_size, alloc_entry);
376 for (int i = 1; i < max_k_idx; i++) {
379 for (int j = 0; j < 4; j++) {
380 if (k_ptr->chance[j]) {
382 num[k_ptr->locale[j]]++;
387 for (int i = 1; i < MAX_DEPTH; i++)
388 num[i] += num[i - 1];
391 quit(_("町のアイテムがない!", "No town objects!"));
393 C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry);
395 table = alloc_kind_table;
396 for (int i = 1; i < max_k_idx; i++) {
399 for (int j = 0; j < 4; j++) {
400 if (k_ptr->chance[j] == 0)
403 int x = k_ptr->locale[j];
404 int p = (100 / k_ptr->chance[j]);
405 int y = (x > 0) ? num[x - 1] : 0;
407 table[z].index = (KIND_OBJECT_IDX)i;
408 table[z].level = (DEPTH)x;
409 table[z].prob1 = (PROB)p;
410 table[z].prob2 = (PROB)p;
411 table[z].prob3 = (PROB)p;
420 * @brief モンスター配列と生成テーブルを初期化する /
421 * Initialize some other arrays
424 static errr init_alloc(void)
428 C_MAKE(elements, max_r_idx, tag_type);
429 for (int i = 1; i < max_r_idx; i++) {
430 elements[i].tag = r_info[i].level;
431 elements[i].index = i;
434 tag_sort(elements, max_r_idx);
435 alloc_race_size = max_r_idx;
436 C_MAKE(alloc_race_table, alloc_race_size, alloc_entry);
437 for (int i = 1; i < max_r_idx; i++) {
438 r_ptr = &r_info[elements[i].index];
439 if (r_ptr->rarity == 0)
442 int x = r_ptr->level;
443 int p = (100 / r_ptr->rarity);
444 alloc_race_table[i].index = (KIND_OBJECT_IDX)elements[i].index;
445 alloc_race_table[i].level = (DEPTH)x;
446 alloc_race_table[i].prob1 = (PROB)p;
447 alloc_race_table[i].prob2 = (PROB)p;
448 alloc_race_table[i].prob3 = (PROB)p;
451 C_KILL(elements, max_r_idx, tag_type);
452 (void)init_object_alloc();
457 * @brief 画面左下にシステムメッセージを表示する /
458 * Hack -- take notes on line 23
461 static void init_note(concptr str)
463 term_erase(0, 23, 255);
464 term_putstr(20, 23, -1, TERM_WHITE, str);
469 * @brief 全ゲームデータ読み込みのサブルーチン /
470 * Hack -- Explain a broken "lib" folder and quit (see below).
474 * This function is "messy" because various things
475 * may or may not be initialized, but the "plog()" and "quit()"
476 * functions are "supposed" to work under any conditions.
479 static void init_angband_aux(concptr why)
482 plog(_("'lib'ディレクトリが存在しないか壊れているようです。", "The 'lib' directory is probably missing or broken."));
483 plog(_("ひょっとするとアーカイブが正しく解凍されていないのかもしれません。", "The 'lib' directory is probably missing or broken."));
484 plog(_("該当する'README'ファイルを読んで確認してみて下さい。", "See the 'README' file for more information."));
485 quit(_("致命的なエラー。", "Fatal Error."));
492 static void put_title(void)
496 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,
498 sprintf(title, _("変愚蛮怒 %d.%d.%d(%s)", "Hengband %d.%d.%d(%s)"), H_VER_MAJOR, H_VER_MINOR, H_VER_PATCH,
500 IS_STABLE_VERSION ? _("安定版", "Stable") : _("開発版", "Developing"));
501 int col = (80 - strlen(title)) / 2;
502 col = col < 0 ? 0 : col;
503 const int VER_INFO_ROW = 3; //!< タイトル表記(行)
504 prt(title, VER_INFO_ROW, col);
508 * @brief 全ゲームデータ読み込みのメインルーチン /
509 * @param player_ptr プレーヤーへの参照ポインタ
510 * @param process_autopick_file_command 自動拾いファイル読み込み関数への関数ポインタ
513 void init_angband(player_type *player_ptr, process_autopick_file_command_pf process_autopick_file_command)
516 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
517 int fd = fd_open(buf, O_RDONLY);
520 sprintf(why, _("'%s'ファイルにアクセスできません!", "Cannot access the '%s' file!"), buf);
521 init_angband_aux(why);
526 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
528 fp = angband_fopen(buf, "r");
531 while (0 == angband_fgets(fp, buf, sizeof(buf)))
532 term_putstr(0, i++, -1, TERM_WHITE, buf);
538 path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
539 fd = fd_open(buf, O_RDONLY);
540 BIT_FLAGS file_permission = 0664;
542 safe_setuid_grab(player_ptr);
543 fd = fd_make(buf, file_permission);
547 sprintf(why, _("'%s'ファイルを作成できません!", "Cannot create the '%s' file!"), buf);
548 init_angband_aux(why);
555 init_note(_("[変数を初期化しています...(その他)", "[Initializing values... (misc)]"));
556 if (init_misc(player_ptr))
557 quit(_("その他の変数を初期化できません", "Cannot initialize misc. values"));
559 init_note(_("[データの初期化中... (地形)]", "[Initializing arrays... (features)]"));
560 if (init_f_info(player_ptr))
561 quit(_("地形初期化不能", "Cannot initialize features"));
563 if (init_feat_variables())
564 quit(_("地形初期化不能", "Cannot initialize features"));
566 init_note(_("[データの初期化中... (アイテム)]", "[Initializing arrays... (objects)]"));
567 if (init_k_info(player_ptr))
568 quit(_("アイテム初期化不能", "Cannot initialize objects"));
570 init_note(_("[データの初期化中... (伝説のアイテム)]", "[Initializing arrays... (artifacts)]"));
571 if (init_a_info(player_ptr))
572 quit(_("伝説のアイテム初期化不能", "Cannot initialize artifacts"));
574 init_note(_("[データの初期化中... (名のあるアイテム)]", "[Initializing arrays... (ego-items)]"));
575 if (init_e_info(player_ptr))
576 quit(_("名のあるアイテム初期化不能", "Cannot initialize ego-items"));
578 init_note(_("[データの初期化中... (モンスター)]", "[Initializing arrays... (monsters)]"));
579 if (init_r_info(player_ptr))
580 quit(_("モンスター初期化不能", "Cannot initialize monsters"));
582 init_note(_("[データの初期化中... (ダンジョン)]", "[Initializing arrays... (dungeon)]"));
583 if (init_d_info(player_ptr))
584 quit(_("ダンジョン初期化不能", "Cannot initialize dungeon"));
586 for (int i = 1; i < current_world_ptr->max_d_idx; i++)
587 if (d_info[i].final_guardian)
588 r_info[d_info[i].final_guardian].flags7 |= RF7_GUARDIAN;
590 init_note(_("[データの初期化中... (魔法)]", "[Initializing arrays... (magic)]"));
591 if (init_m_info(player_ptr))
592 quit(_("魔法初期化不能", "Cannot initialize magic"));
594 init_note(_("[データの初期化中... (熟練度)]", "[Initializing arrays... (skill)]"));
595 if (init_s_info(player_ptr))
596 quit(_("熟練度初期化不能", "Cannot initialize skill"));
598 init_note(_("[配列を初期化しています... (荒野)]", "[Initializing arrays... (wilderness)]"));
599 if (init_wilderness())
600 quit(_("荒野を初期化できません", "Cannot initialize wilderness"));
602 init_note(_("[配列を初期化しています... (街)]", "[Initializing arrays... (towns)]"));
604 quit(_("街を初期化できません", "Cannot initialize towns"));
606 init_note(_("[配列を初期化しています... (建物)]", "[Initializing arrays... (buildings)]"));
607 if (init_buildings())
608 quit(_("建物を初期化できません", "Cannot initialize buildings"));
610 init_note(_("[配列を初期化しています... (クエスト)]", "[Initializing arrays... (quests)]"));
612 quit(_("クエストを初期化できません", "Cannot initialize quests"));
614 if (init_v_info(player_ptr))
615 quit(_("vault 初期化不能", "Cannot initialize vaults"));
617 init_note(_("[データの初期化中... (その他)]", "[Initializing arrays... (other)]"));
618 if (init_other(player_ptr))
619 quit(_("その他のデータ初期化不能", "Cannot initialize other stuff"));
621 init_note(_("[データの初期化中... (アロケーション)]", "[Initializing arrays... (alloc)]"));
623 quit(_("アロケーション・スタッフ初期化不能", "Cannot initialize alloc stuff"));
625 init_note(_("[ユーザー設定ファイルを初期化しています...]", "[Initializing user pref files...]"));
626 strcpy(buf, "pref.prf");
627 process_pref_file(player_ptr, buf, process_autopick_file_command);
628 sprintf(buf, "pref-%s.prf", ANGBAND_SYS);
629 process_pref_file(player_ptr, buf, process_autopick_file_command);
630 init_note(_("[初期化終了]", "[Initialization complete]"));