3 * @brief ファイル入出力管理 / Purpose: code dealing with files (and death)
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
17 #include "signal-handlers.h"
18 #include "uid-checker.h"
20 #include "core.h" // リファクタリングして後で消す
23 #include "character-dump.h"
26 #include "player-move.h"
27 #include "player-personality.h"
28 #include "player-effects.h"
29 #include "monster-status.h"
30 #include "view-mainwindow.h"
31 #include "objectkind.h"
34 #include "io/gf-descriptions.h"
35 #include "io/tokenizer.h"
37 #define PREF_TYPE_NORMAL 0
38 #define PREF_TYPE_AUTOPICK 1
39 #define PREF_TYPE_HISTPREF 2
41 concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
42 concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
43 concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
44 concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
45 concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
46 concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
47 concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
48 concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
49 concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
50 concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
51 concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
52 concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
53 concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
56 * Buffer to hold the current savefile name
57 * 'savefile' holds full path name. 'savefile_base' holds only base name.
60 char savefile_base[40];
63 * @brief 設定ファイルの各行から各種テキスト情報を取得する /
64 * Parse a sub-file of the "extra info" (format shown below)
65 * @param creature_ptr プレーヤーへの参照ポインタ
66 * @param buf データテキストの参照ポインタ
70 * Each "action" line has an "action symbol" in the first column,
71 * followed by a colon, followed by some command specific info,
72 * usually in the form of "tokens" separated by colons or slashes.
73 * Blank lines, lines starting with white space, and lines starting
74 * with pound signs ("#") are ignored (as comments).
75 * Note the use of "tokenize()" to allow the use of both colons and
76 * slashes as delimeters, while still allowing final tokens which
77 * may contain any characters including "delimiters".
78 * Note the use of "strtol()" to allow all "integers" to be encoded
79 * in decimal, hexidecimal, or octal form.
80 * Note that "monster zero" is used for the "player" attr/char, "object
81 * zero" will be used for the "stack" attr/char, and "feature zero" is
82 * used for the "nothing" attr/char.
83 * Parse another file recursively, see below for details
85 * Specify the attr/char values for "monsters" by race index
86 * R:\<num\>:\<a\>:\<c\>
87 * Specify the attr/char values for "objects" by kind index
88 * K:\<num\>:\<a\>:\<c\>
89 * Specify the attr/char values for "features" by feature index
90 * F:\<num\>:\<a\>:\<c\>
91 * Specify the attr/char values for unaware "objects" by kind tval
92 * U:\<tv\>:\<a\>:\<c\>
93 * Specify the attr/char values for inventory "objects" by kind tval
94 * E:\<tv\>:\<a\>:\<c\>
95 * Define a macro action, given an encoded macro action
97 * Create a normal macro, given an encoded macro trigger
99 * Create a command macro, given an encoded macro trigger
101 * Create a keyset mapping
102 * S:\<key\>:\<key\>:\<dir\>
103 * Turn an option off, given its name
105 * Turn an option on, given its name
107 * Specify visual information, given an index, and some data
108 * V:\<num\>:\<kv\>:\<rv\>:\<gv\>:\<bv\>
109 * Specify the set of colors to use when drawing a zapped spell
111 * Specify a macro trigger template and macro trigger names.
112 * T:\<template\>:\<modifier chr\>:\<modifier name1\>:\<modifier name2\>:...
113 * T:\<trigger\>:\<keycode\>:\<shift-keycode\>
116 errr process_pref_file_command(player_type *creature_ptr, char *buf)
118 if (buf[1] != ':') return 1;
125 /* Process "H:<history>" */
126 add_history_from_pref_line(buf + 2);
131 /* Process "R:<num>:<a>/<c>" -- attr/char for monster races */
132 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
135 int i = (huge)strtol(zz[0], NULL, 0);
136 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
137 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
138 if (i >= max_r_idx) return 1;
140 if (n1 || (!(n2 & 0x80) && n2)) r_ptr->x_attr = n1; /* Allow TERM_DARK text */
141 if (n2) r_ptr->x_char = n2;
146 /* Process "K:<num>:<a>/<c>" -- attr/char for object kinds */
147 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
150 int i = (huge)strtol(zz[0], NULL, 0);
151 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
152 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
153 if (i >= max_k_idx) return 1;
155 if (n1 || (!(n2 & 0x80) && n2)) k_ptr->x_attr = n1; /* Allow TERM_DARK text */
156 if (n2) k_ptr->x_char = n2;
161 /* Process "F:<num>:<a>/<c>" -- attr/char for terrain features */
162 /* "F:<num>:<a>/<c>" */
163 /* "F:<num>:<a>/<c>:LIT" */
164 /* "F:<num>:<a>/<c>:<la>/<lc>:<da>/<dc>" */
166 int num = tokenize(buf + 2, F_LIT_MAX * 2 + 1, zz, TOKENIZE_CHECKQUOTE);
168 if ((num != 3) && (num != 4) && (num != F_LIT_MAX * 2 + 1)) return 1;
169 else if ((num == 4) && !streq(zz[3], "LIT")) return 1;
171 int i = (huge)strtol(zz[0], NULL, 0);
172 if (i >= max_f_idx) return 1;
175 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
176 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
177 if (n1 || (!(n2 & 0x80) && n2)) f_ptr->x_attr[F_LIT_STANDARD] = n1; /* Allow TERM_DARK text */
178 if (n2) f_ptr->x_char[F_LIT_STANDARD] = n2;
184 /* No lighting support */
185 n1 = f_ptr->x_attr[F_LIT_STANDARD];
186 n2 = f_ptr->x_char[F_LIT_STANDARD];
187 for (int j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++)
189 f_ptr->x_attr[j] = n1;
190 f_ptr->x_char[j] = n2;
197 /* Use default lighting */
198 apply_default_feat_lighting(f_ptr->x_attr, f_ptr->x_char);
201 case F_LIT_MAX * 2 + 1:
203 /* Use desired lighting */
204 for (int j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++)
206 n1 = (TERM_COLOR)strtol(zz[j * 2 + 1], NULL, 0);
207 n2 = (SYMBOL_CODE)strtol(zz[j * 2 + 2], NULL, 0);
208 if (n1 || (!(n2 & 0x80) && n2)) f_ptr->x_attr[j] = n1; /* Allow TERM_DARK text */
209 if (n2) f_ptr->x_char[j] = n2;
220 /* Process "S:<num>:<a>/<c>" -- attr/char for special things */
221 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
223 int j = (byte)strtol(zz[0], NULL, 0);
224 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
225 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
226 misc_to_attr[j] = n1;
227 misc_to_char[j] = n2;
232 /* Process "U:<tv>:<a>/<c>" -- attr/char for unaware items */
233 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
235 int j = (huge)strtol(zz[0], NULL, 0);
236 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
237 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
238 for (int i = 1; i < max_k_idx; i++)
240 object_kind *k_ptr = &k_info[i];
241 if (k_ptr->tval == j)
243 if (n1) k_ptr->d_attr = n1;
244 if (n2) k_ptr->d_char = n2;
252 /* Process "E:<tv>:<a>" -- attribute for inventory objects */
253 if (tokenize(buf + 2, 2, zz, TOKENIZE_CHECKQUOTE) != 2) return 1;
255 int j = (byte)strtol(zz[0], NULL, 0) % 128;
256 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
257 if (n1) tval_to_attr[j] = n1;
262 /* Process "A:<str>" -- save an "action" for later */
263 text_to_ascii(macro__buf, buf + 2);
268 /* Process "P:<str>" -- normal macro */
270 text_to_ascii(tmp, buf + 2);
271 macro_add(tmp, macro__buf);
276 /* Process "C:<str>" -- create keymap */
277 if (tokenize(buf + 2, 2, zz, TOKENIZE_CHECKQUOTE) != 2) return 1;
279 int mode = strtol(zz[0], NULL, 0);
280 if ((mode < 0) || (mode >= KEYMAP_MODES)) return 1;
283 text_to_ascii(tmp, zz[1]);
284 if (!tmp[0] || tmp[1]) return 1;
286 int i = (byte)(tmp[0]);
287 string_free(keymap_act[mode][i]);
288 keymap_act[mode][i] = string_make(macro__buf);
293 /* Process "V:<num>:<kv>:<rv>:<gv>:<bv>" -- visual info */
294 if (tokenize(buf + 2, 5, zz, TOKENIZE_CHECKQUOTE) != 5) return 1;
296 int i = (byte)strtol(zz[0], NULL, 0);
297 angband_color_table[i][0] = (byte)strtol(zz[1], NULL, 0);
298 angband_color_table[i][1] = (byte)strtol(zz[2], NULL, 0);
299 angband_color_table[i][2] = (byte)strtol(zz[3], NULL, 0);
300 angband_color_table[i][3] = (byte)strtol(zz[4], NULL, 0);
306 /* Process "X:<str>" -- turn option off */
307 /* Process "Y:<str>" -- turn option on */
308 for (int i = 0; option_info[i].o_desc; i++)
310 bool is_option = option_info[i].o_var != NULL;
311 is_option &= option_info[i].o_text != NULL;
312 is_option &= streq(option_info[i].o_text, buf + 2);
313 if (!is_option) continue;
315 int os = option_info[i].o_set;
316 int ob = option_info[i].o_bit;
318 if ((creature_ptr->playing || current_world_ptr->character_xtra) &&
319 (OPT_PAGE_BIRTH == option_info[i].o_page) && !current_world_ptr->wizard)
321 msg_format(_("初期オプションは変更できません! '%s'", "Birth options can not changed! '%s'"), buf);
328 option_flag[os] &= ~(1L << ob);
329 (*option_info[i].o_var) = FALSE;
333 option_flag[os] |= (1L << ob);
334 (*option_info[i].o_var) = TRUE;
338 msg_format(_("オプションの名前が正しくありません: %s", "Ignored invalid option: %s"), buf);
344 /* Process "Z:<type>:<str>" -- set spell color */
345 char *t = my_strchr(buf + 2, ':');
349 for (int i = 0; i < MAX_NAMED_NUM; i++)
351 if (!streq(gf_desc[i].name, buf + 2)) continue;
353 gf_color[gf_desc[i].num] = (TERM_COLOR)quark_add(t);
361 /* Initialize macro trigger names and a template */
362 /* Process "T:<trigger>:<keycode>:<shift-keycode>" */
363 /* Process "T:<template>:<modifier chr>:<modifier name>:..." */
364 int tok = tokenize(buf + 2, 2 + MAX_MACRO_MOD, zz, 0);
366 /* Process "T:<template>:<modifier chr>:<modifier name>:..." */
369 if (macro_template != NULL)
371 int macro_modifier_length = strlen(macro_modifier_chr);
372 string_free(macro_template);
373 macro_template = NULL;
374 string_free(macro_modifier_chr);
375 for (int i = 0; i < macro_modifier_length; i++)
377 string_free(macro_modifier_name[i]);
380 for (int i = 0; i < max_macrotrigger; i++)
382 string_free(macro_trigger_name[i]);
383 string_free(macro_trigger_keycode[0][i]);
384 string_free(macro_trigger_keycode[1][i]);
387 max_macrotrigger = 0;
390 if (*zz[0] == '\0') return 0;
392 int zz_length = strlen(zz[1]);
393 zz_length = MIN(MAX_MACRO_MOD, zz_length);
394 if (2 + zz_length != tok) return 1;
396 macro_template = string_make(zz[0]);
397 macro_modifier_chr = string_make(zz[1]);
398 for (int i = 0; i < zz_length; i++)
400 macro_modifier_name[i] = string_make(zz[2 + i]);
406 /* Process "T:<trigger>:<keycode>:<shift-keycode>" */
407 if (tok < 2) return 0;
411 if (max_macrotrigger >= MAX_MACRO_TRIG)
413 msg_print(_("マクロトリガーの設定が多すぎます!", "Too many macro triggers!"));
417 int m = max_macrotrigger;
428 macro_trigger_name[m] = string_make(buf_aux);
429 macro_trigger_keycode[0][m] = string_make(zz[1]);
432 macro_trigger_keycode[1][m] = string_make(zz[2]);
436 macro_trigger_keycode[1][m] = string_make(zz[1]);
446 * @brief process_pref_fileのサブルーチンとして条件分岐処理の解釈と結果を返す /
447 * Helper function for "process_pref_file()"
448 * @param creature_ptr プレーヤーへの参照ポインタ
449 * @param sp テキスト文字列の参照ポインタ
450 * @param fp 再帰中のポインタ参照
455 * v: output buffer array
461 concptr process_pref_file_expr(player_type *creature_ptr, char **sp, char *fp)
465 while (iswspace(*s)) s++;
485 t = process_pref_file_expr(creature_ptr, &s, &f);
490 else if (streq(t, "IOR"))
493 while (*s && (f != b2))
495 t = process_pref_file_expr(creature_ptr, &s, &f);
496 if (*t && !streq(t, "0")) v = "1";
499 else if (streq(t, "AND"))
502 while (*s && (f != b2))
504 t = process_pref_file_expr(creature_ptr, &s, &f);
505 if (*t && streq(t, "0")) v = "0";
508 else if (streq(t, "NOT"))
511 while (*s && (f != b2))
513 t = process_pref_file_expr(creature_ptr, &s, &f);
514 if (*t && streq(t, "1")) v = "0";
517 else if (streq(t, "EQU"))
522 t = process_pref_file_expr(creature_ptr, &s, &f);
524 while (*s && (f != b2))
526 p = process_pref_file_expr(creature_ptr, &s, &f);
527 if (streq(t, p)) v = "1";
530 else if (streq(t, "LEQ"))
535 t = process_pref_file_expr(creature_ptr, &s, &f);
537 while (*s && (f != b2))
540 t = process_pref_file_expr(creature_ptr, &s, &f);
541 if (*t && atoi(p) > atoi(t)) v = "0";
544 else if (streq(t, "GEQ"))
549 t = process_pref_file_expr(creature_ptr, &s, &f);
551 while (*s && (f != b2))
554 t = process_pref_file_expr(creature_ptr, &s, &f);
555 if (*t && atoi(p) < atoi(t)) v = "0";
560 while (*s && (f != b2))
562 t = process_pref_file_expr(creature_ptr, &s, &f);
566 if (f != b2) v = "?x?x?";
568 if ((f = *s) != '\0') *s++ = '\0';
575 /* Accept all printables except spaces and brackets */
577 while (iskanji(*s) || (isprint(*s) && !my_strchr(" []", *s)))
579 if (iskanji(*s)) s++;
583 while (isprint(*s) && !my_strchr(" []", *s)) ++s;
586 if ((f = *s) != '\0') *s++ = '\0';
596 if (streq(b + 1, "SYS"))
600 else if (streq(b + 1, "KEYBOARD"))
602 v = ANGBAND_KEYBOARD;
604 else if (streq(b + 1, "GRAF"))
608 else if (streq(b + 1, "MONOCHROME"))
615 else if (streq(b + 1, "RACE"))
623 else if (streq(b + 1, "CLASS"))
631 else if (streq(b + 1, "PLAYER"))
633 static char tmp_player_name[32];
635 for (pn = creature_ptr->name, tpn = tmp_player_name; *pn; pn++, tpn++)
645 *tpn = my_strchr(" []", *pn) ? '_' : *pn;
651 else if (streq(b + 1, "REALM1"))
654 v = E_realm_names[creature_ptr->realm1];
656 v = realm_names[creature_ptr->realm1];
659 else if (streq(b + 1, "REALM2"))
662 v = E_realm_names[creature_ptr->realm2];
664 v = realm_names[creature_ptr->realm2];
667 else if (streq(b + 1, "LEVEL"))
669 sprintf(tmp, "%02d", creature_ptr->lev);
672 else if (streq(b + 1, "AUTOREGISTER"))
674 if (creature_ptr->autopick_autoregister)
679 else if (streq(b + 1, "MONEY"))
681 sprintf(tmp, "%09ld", (long int)creature_ptr->au);
692 * @brief process_pref_fileのサブルーチン /
693 * Open the "user pref file" and parse it.
694 * @param creature_ptr プレーヤーへの参照ポインタ
695 * @param name 読み込むファイル名
696 * @param preftype prefファイルのタイプ
701 * v: output buffer array
707 static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype)
710 fp = my_fopen(name, "r");
718 while (my_fgets(fp, buf, sizeof(buf)) == 0)
721 if (!buf[0]) continue;
724 if (!iskanji(buf[0]))
726 if (iswspace(buf[0])) continue;
728 if (buf[0] == '#') continue;
731 /* Process "?:<expr>" */
732 if ((buf[0] == '?') && (buf[1] == ':'))
737 concptr v = process_pref_file_expr(creature_ptr, &s, &f);
738 bypass = streq(v, "0");
742 if (bypass) continue;
744 /* Process "%:<file>" */
747 static int depth_count = 0;
748 if (depth_count > 20) continue;
753 case PREF_TYPE_AUTOPICK:
754 (void)process_autopick_file(creature_ptr, buf + 2);
756 case PREF_TYPE_HISTPREF:
757 (void)process_histpref_file(creature_ptr, buf + 2);
760 (void)process_pref_file(creature_ptr, buf + 2);
768 err = process_pref_file_command(creature_ptr, buf);
771 if (preftype != PREF_TYPE_AUTOPICK)
773 err = process_autopick_file_command(buf);
779 /* Print error message */
780 /* ToDo: Add better error messages */
781 msg_format(_("ファイル'%s'の%d行でエラー番号%dのエラー。", "Error %d in line %d of file '%s'."),
782 _(name, err), line, _(err, name));
783 msg_format(_("('%s'を解析中)", "Parsing '%s'"), old);
793 * @brief pref設定ファイルを読み込み設定を反映させる /
794 * Process the "user pref file" with the given name
795 * @param creature_ptr プレーヤーへの参照ポインタ
796 * @param name 読み込むファイル名
800 * See the functions above for a list of legal "commands".
801 * We also accept the special "?" and "%" directives, which
802 * allow conditional evaluation and filename inclusion.
805 errr process_pref_file(player_type *creature_ptr, concptr name)
808 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name);
810 errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
811 if (err1 > 0) return err1;
813 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
814 errr err2 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
815 if (err2 < 0 && !err1)
823 * @brief プレイヤーステータスをファイルダンプ出力する
824 * Hack -- Dump a character description file
825 * @param creature_ptr プレーヤーへの参照ポインタ
826 * @param name 出力ファイル名
829 * Allow the "full" flag to dump additional info,
830 * and trigger its usage from various places in the code.
832 errr file_character(player_type *creature_ptr, concptr name, display_player_pf display_player, map_name_pf map_name)
835 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
837 FILE_TYPE(FILE_TYPE_TEXT);
839 int fd = fd_open(buf, O_RDONLY);
844 (void)sprintf(out_val, _("現存するファイル %s に上書きしますか? ", "Replace existing file %s? "), buf);
845 if (get_check_strict(out_val, CHECK_NO_HISTORY)) fd = -1;
849 if (fd < 0) fff = my_fopen(buf, "w");
853 prt(_("キャラクタ情報のファイルへの書き出しに失敗しました!", "Character dump failed!"), 0, 0);
858 make_character_dump(creature_ptr, fff, update_playtime, display_player, map_name);
860 msg_print(_("キャラクタ情報のファイルへの書き出しに成功しました。", "Character dump successful."));
867 * @brief ファイル内容の一行をコンソールに出力する
868 * Display single line of on-line help file
875 * You can insert some special color tag to change text color.
877 * WHITETEXT [[[[y|SOME TEXT WHICH IS DISPLAYED IN YELLOW| WHITETEXT
878 * A colored segment is between "[[[[y|" and the last "|".
879 * You can use any single character in place of the "|".
882 static void show_file_aux_line(concptr str, int cy, concptr shower)
894 static const char tag_str[] = "[[[[";
895 byte color = TERM_WHITE;
897 for (int i = 0; str[i];)
899 int len = strlen(&str[i]);
900 int showercol = len + 1;
901 int bracketcol = len + 1;
906 ptr = my_strstr(&lcstr[i], shower);
907 if (ptr) showercol = ptr - &lcstr[i];
910 ptr = in_tag ? my_strchr(&str[i], in_tag) : my_strstr(&str[i], tag_str);
911 if (ptr) bracketcol = ptr - &str[i];
912 if (bracketcol < endcol) endcol = bracketcol;
913 if (showercol < endcol) endcol = showercol;
915 Term_addstr(endcol, color, &str[i]);
919 if (endcol == showercol)
921 int showerlen = strlen(shower);
922 Term_addstr(showerlen, TERM_YELLOW, &str[i]);
928 if (endcol != bracketcol) continue;
938 i += sizeof(tag_str) - 1;
939 color = color_char_to_attr(str[i]);
940 if (color == 255 || str[i + 1] == '\0')
943 Term_addstr(-1, TERM_WHITE, tag_str);
944 cx += sizeof(tag_str) - 1;
953 Term_erase(cx, cy, 255);
958 * @brief ファイル内容をコンソールに出力する
959 * Recursive file perusal.
960 * @param creature_ptr プレーヤーへの参照ポインタ
961 * @param show_version TRUEならばコンソール上にゲームのバージョンを表示する
962 * @param name ファイル名の文字列
963 * @param what 内容キャプションの文字列
969 * Process various special text in the input file, including
970 * the "menu" structures used by the "help file" system.
971 * Return FALSE on 'q' to exit from a deep, otherwise TRUE.
974 bool show_file(player_type *creature_ptr, bool show_version, concptr name, concptr what, int line, BIT_FLAGS mode)
977 Term_get_size(&wid, &hgt);
980 strcpy(finder_str, "");
983 strcpy(shower_str, "");
989 for (int i = 0; i < 68; i++)
995 strcpy(filename, name);
996 int n = strlen(filename);
999 for (int i = 0; i < n; i++)
1001 if (filename[i] == '#')
1004 tag = filename + i + 1;
1014 strcpy(caption, what);
1016 fff = my_fopen(path, "r");
1021 sprintf(caption, _("ヘルプ・ファイル'%s'", "Help file '%s'"), name);
1022 path_build(path, sizeof(path), ANGBAND_DIR_HELP, name);
1023 fff = my_fopen(path, "r");
1028 sprintf(caption, _("スポイラー・ファイル'%s'", "Info file '%s'"), name);
1029 path_build(path, sizeof(path), ANGBAND_DIR_INFO, name);
1030 fff = my_fopen(path, "r");
1035 path_build(path, sizeof(path), ANGBAND_DIR, name);
1037 for (int i = 0; path[i]; i++)
1038 if ('\\' == path[i])
1039 path[i] = PATH_SEP[0];
1041 sprintf(caption, _("スポイラー・ファイル'%s'", "Info file '%s'"), name);
1042 fff = my_fopen(path, "r");
1047 msg_format(_("'%s'をオープンできません。", "Cannot open '%s'."), name);
1059 bool reverse = (line < 0);
1063 if (my_fgets(fff, buf, sizeof(buf))) break;
1064 if (!prefix(str, "***** "))
1070 if ((str[6] == '[') && isalpha(str[7]))
1072 int k = str[7] - 'A';
1074 if ((str[8] == ']') && (str[9] == ' '))
1076 strncpy(hook[k], str + 10, 31);
1083 if (str[6] != '<') continue;
1085 size_t len = strlen(str);
1086 if (str[len - 1] == '>')
1088 str[len - 1] = '\0';
1089 if (tag && streq(str + 7, tag)) line = next;
1096 line = ((size - 1) / rows)*rows;
1100 concptr find = NULL;
1103 if (line >= size - rows)
1105 if (line < 0) line = 0;
1110 fff = my_fopen(path, "r");
1111 if (!fff) return FALSE;
1118 if (my_fgets(fff, buf, sizeof(buf))) break;
1119 if (prefix(buf, "***** ")) continue;
1124 concptr shower = NULL;
1125 for (int i = 0; i < rows; i++)
1128 if (!i) line = next;
1129 if (my_fgets(fff, buf, sizeof(buf))) break;
1130 if (prefix(buf, "***** ")) continue;
1135 strcpy(lc_buf, str);
1136 str_tolower(lc_buf);
1137 if (!my_strstr(lc_buf, find)) continue;
1141 show_file_aux_line(str, i + 2, shower);
1145 while (row_count < rows)
1147 Term_erase(0, row_count + 2, 255);
1161 prt(format(_("[変愚蛮怒 %d.%d.%d, %s, %d/%d]", "[Hengband %d.%d.%d, %s, Line %d/%d]"),
1162 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH,
1163 caption, line, size), 0, 0);
1167 prt(format(_("[%s, %d/%d]", "[%s, Line %d/%d]"),
1168 caption, line, size), 0, 0);
1173 prt(_("[キー:(?)ヘルプ (ESC)終了]", "[Press ESC to exit.]"), hgt - 1, 0);
1179 prt("[キー:(RET/スペース)↑ (-)↓ (?)ヘルプ (ESC)終了]", hgt - 1, 0);
1181 prt("[キー:(RET/スペース)↓ (-)↑ (?)ヘルプ (ESC)終了]", hgt - 1, 0);
1183 prt("[Press Return, Space, -, =, /, |, or ESC to exit.]", hgt - 1, 0);
1187 skey = inkey_special(TRUE);
1191 if (strcmp(name, _("jhelpinfo.txt", "helpinfo.txt")) != 0)
1192 show_file(creature_ptr, TRUE, _("jhelpinfo.txt", "helpinfo.txt"), NULL, 0, mode);
1195 prt(_("強調: ", "Show: "), hgt - 1, 0);
1198 strcpy(back_str, shower_str);
1199 if (askfor(shower_str, 80))
1203 str_tolower(shower_str);
1204 shower = shower_str;
1208 else strcpy(shower_str, back_str);
1213 prt(_("検索: ", "Find: "), hgt - 1, 0);
1214 strcpy(back_str, finder_str);
1215 if (askfor(finder_str, 80))
1222 str_tolower(finder_str);
1223 shower = finder_str;
1227 else strcpy(finder_str, back_str);
1233 prt(_("行: ", "Goto Line: "), hgt - 1, 0);
1236 if (askfor(tmp, 80)) line = atoi(tmp);
1245 line = ((size - 1) / rows) * rows;
1251 prt(_("ファイル・ネーム: ", "Goto File: "), hgt - 1, 0);
1252 strcpy(tmp, _("jhelp.hlp", "help.hlp"));
1254 if (askfor(tmp, 80))
1256 if (!show_file(creature_ptr, TRUE, tmp, NULL, 0, mode)) skey = 'q';
1263 line = line + (reverse ? rows : -rows);
1264 if (line < 0) line = 0;
1269 if (line < 0) line = 0;
1274 line = line + (reverse ? -1 : 1);
1275 if (line < 0) line = 0;
1281 if (line < 0) line = 0;
1290 line = line + (reverse ? -rows : rows);
1291 if (line < 0) line = 0;
1302 if (!(skey & SKEY_MASK) && isalpha(skey))
1305 if ((key > -1) && hook[key][0])
1307 /* Recurse on that file */
1308 if (!show_file(creature_ptr, TRUE, hook[key], NULL, 0, mode))
1321 if (!get_string(_("ファイル名: ", "File name: "), xtmp, 80)) continue;
1323 path_build(buff, sizeof(buff), ANGBAND_DIR_USER, xtmp);
1325 /* Hack -- Re-Open the file */
1326 fff = my_fopen(path, "r");
1328 ffp = my_fopen(buff, "w");
1332 msg_print(_("ファイルを開けません。", "Failed to open file."));
1337 sprintf(xtmp, "%s: %s", creature_ptr->name, what ? what : caption);
1338 my_fputs(ffp, xtmp, 80);
1339 my_fputs(ffp, "\n", 80);
1341 while (!my_fgets(fff, buff, sizeof(buff)))
1342 my_fputs(ffp, buff, 80);
1345 fff = my_fopen(path, "r");
1348 if ((skey == ESCAPE) || (skey == '<')) break;
1350 if (skey == KTRL('q')) skey = 'q';
1352 if (skey == 'q') break;
1356 return (skey != 'q');
1361 * @brief ヘルプを表示するコマンドのメインルーチン
1362 * Peruse the On-Line-Help
1363 * @param creature_ptr プレーヤーへの参照ポインタ
1367 void do_cmd_help(player_type *creature_ptr)
1370 (void)show_file(creature_ptr, TRUE, _("jhelp.hlp", "help.hlp"), NULL, 0, 0);
1376 * @brief プレイヤーの名前をチェックして修正する
1377 * Process the player name.
1378 * @param player_ptr プレーヤーへの参照ポインタ
1379 * @param sf セーブファイル名に合わせた修正を行うならばTRUE
1382 * Extract a clean "base name".
1383 * Build the savefile name if needed.
1385 void process_player_name(player_type *creature_ptr, bool sf)
1387 char old_player_base[32] = "";
1388 if (current_world_ptr->character_generated)
1389 strcpy(old_player_base, creature_ptr->base_name);
1391 for (int i = 0; creature_ptr->name[i]; i++)
1394 if (iskanji(creature_ptr->name[i]))
1400 if (iscntrl((unsigned char)creature_ptr->name[i]))
1402 if (iscntrl(creature_ptr->name[i]))
1405 quit_fmt(_("'%s' という名前は不正なコントロールコードを含んでいます。", "The name '%s' contains control chars!"), creature_ptr->name);
1410 for (int i = 0; creature_ptr->name[i]; i++)
1413 unsigned char c = creature_ptr->name[i];
1415 char c = creature_ptr->name[i];
1420 if (k + 2 >= sizeof(creature_ptr->base_name) || !creature_ptr->name[i + 1])
1423 creature_ptr->base_name[k++] = c;
1425 creature_ptr->base_name[k++] = creature_ptr->name[i];
1428 else if (iskana(c)) creature_ptr->base_name[k++] = c;
1432 if (!strncmp(PATH_SEP, creature_ptr->name + i, strlen(PATH_SEP)))
1434 creature_ptr->base_name[k++] = '_';
1435 i += strlen(PATH_SEP);
1437 #if defined(WINDOWS)
1438 else if (my_strchr("\"*,/:;<>?\\|", c))
1439 creature_ptr->base_name[k++] = '_';
1441 else if (isprint(c))
1442 creature_ptr->base_name[k++] = c;
1445 creature_ptr->base_name[k] = '\0';
1446 if (!creature_ptr->base_name[0])
1447 strcpy(creature_ptr->base_name, "PLAYER");
1449 #ifdef SAVEFILE_MUTABLE
1452 if (!savefile_base[0] && savefile[0])
1454 concptr s = savefile;
1458 t = my_strstr(s, PATH_SEP);
1464 strcpy(savefile_base, s);
1467 if (!savefile_base[0] || !savefile[0])
1473 strcpy(savefile_base, creature_ptr->base_name);
1475 #ifdef SAVEFILE_USE_UID
1476 /* Rename the savefile, using the creature_ptr->player_uid and creature_ptr->base_name */
1477 (void)sprintf(temp, "%d.%s", creature_ptr->player_uid, creature_ptr->base_name);
1479 /* Rename the savefile, using the creature_ptr->base_name */
1480 (void)sprintf(temp, "%s", creature_ptr->base_name);
1482 path_build(savefile, sizeof(savefile), ANGBAND_DIR_SAVE, temp);
1485 if (current_world_ptr->character_generated && !streq(old_player_base, creature_ptr->base_name))
1487 autopick_load_pref(creature_ptr, FALSE);
1493 * @brief プレイヤーの名前を変更するコマンドのメインルーチン
1494 * Gets a name for the character, reacting to name changes.
1495 * @param creature_ptr プレーヤーへの参照ポインタ
1499 * Assumes that "display_player()" has just been called
1500 * Perhaps we should NOT ask for a name (at "birth()") on
1501 * Unix machines? XXX XXX
1502 * What a horrible name for a global function.
1505 void get_name(player_type *creature_ptr)
1508 strcpy(tmp, creature_ptr->name);
1510 if (get_string(_("キャラクターの名前を入力して下さい: ", "Enter a name for your character: "), tmp, 15))
1512 strcpy(creature_ptr->name, tmp);
1515 if (strlen(creature_ptr->name) == 0)
1517 strcpy(creature_ptr->name, "PLAYER");
1520 strcpy(tmp, ap_ptr->title);
1522 if (ap_ptr->no == 1)
1527 strcat(tmp, creature_ptr->name);
1529 Term_erase(34, 1, 255);
1530 c_put_str(TERM_L_BLUE, tmp, 1, 34);
1536 * @brief セーブするコマンドのメインルーチン
1538 * @param creature_ptr プレーヤーへの参照ポインタ
1539 * @param is_autosave オートセーブ中の処理ならばTRUE
1543 void do_cmd_save_game(player_type *creature_ptr, int is_autosave)
1547 msg_print(_("自動セーブ中", "Autosaving the game..."));
1551 disturb(creature_ptr, TRUE, TRUE);
1555 handle_stuff(creature_ptr);
1556 prt(_("ゲームをセーブしています...", "Saving game..."), 0, 0);
1558 (void)strcpy(creature_ptr->died_from, _("(セーブ)", "(saved)"));
1559 signals_ignore_tstp();
1560 if (save_player(creature_ptr))
1562 prt(_("ゲームをセーブしています... 終了", "Saving game... done."), 0, 0);
1566 prt(_("ゲームをセーブしています... 失敗!", "Saving game... failed!"), 0, 0);
1569 signals_handle_tstp();
1571 (void)strcpy(creature_ptr->died_from, _("(元気に生きている)", "(alive and well)"));
1572 current_world_ptr->is_loading_now = FALSE;
1573 update_creature(creature_ptr);
1574 mproc_init(creature_ptr->current_floor_ptr);
1575 current_world_ptr->is_loading_now = TRUE;
1580 * @brief セーブ後にゲーム中断フラグを立てる/
1581 * Save the game and exit
1585 void do_cmd_save_and_exit(player_type *creature_ptr)
1587 creature_ptr->playing = FALSE;
1588 creature_ptr->leaving = TRUE;
1589 exe_write_diary(creature_ptr, DIARY_GAMESTART, 0, _("----ゲーム中断----", "---- Save and Exit Game ----"));
1594 * @brief 異常発生時のゲーム緊急終了処理 /
1595 * Handle abrupt death of the visual system
1596 * @param creature_ptr プレーヤーへの参照ポインタ
1599 * This routine is called only in very rare situations, and only
1600 * by certain visual systems, when they experience fatal errors.
1602 void exit_game_panic(player_type *creature_ptr)
1604 if (!current_world_ptr->character_generated || current_world_ptr->character_saved)
1605 quit(_("緊急事態", "panic"));
1609 disturb(creature_ptr, TRUE, TRUE);
1610 if (creature_ptr->chp < 0) creature_ptr->is_dead = FALSE;
1612 creature_ptr->panic_save = 1;
1613 signals_ignore_tstp();
1614 (void)strcpy(creature_ptr->died_from, _("(緊急セーブ)", "(panic save)"));
1615 if (!save_player(creature_ptr)) quit(_("緊急セーブ失敗!", "panic save failed!"));
1616 quit(_("緊急セーブ成功!", "panic save succeeded!"));
1621 * @brief ファイルからランダムに行を一つ取得する /
1622 * Get a random line from a file
1623 * @param file_name ファイル名
1624 * @param entry 特定条件時のN:タグヘッダID
1625 * @param output 出力先の文字列参照ポインタ
1629 * Based on the monster speech patch by Matt Graham,
1632 errr get_rnd_line(concptr file_name, int entry, char *output)
1635 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, file_name);
1637 fp = my_fopen(buf, "r");
1644 if (my_fgets(fp, buf, sizeof(buf)) != 0)
1651 if ((buf[0] != 'N') || (buf[1] != ':')) continue;
1657 else if (buf[2] == 'M')
1659 if (r_info[entry].flags1 & RF1_MALE) break;
1661 else if (buf[2] == 'F')
1663 if (r_info[entry].flags1 & RF1_FEMALE) break;
1665 else if (sscanf(&(buf[2]), "%d", &test) != EOF)
1667 if (test == entry) break;
1670 msg_format("Error in line %d of %s!", line_num, file_name);
1676 for (counter = 0; ; counter++)
1680 test = my_fgets(fp, buf, sizeof(buf));
1683 /* Ignore lines starting with 'N:' */
1684 if ((buf[0] == 'N') && (buf[1] == ':')) continue;
1686 if (buf[0] != '#') break;
1693 if (one_in_(counter + 1)) strcpy(output, buf);
1697 return counter ? 0 : -1;
1703 * @brief ファイルからランダムに行を一つ取得する(日本語文字列のみ) /
1704 * @param file_name ファイル名
1705 * @param entry 特定条件時のN:タグヘッダID
1706 * @param output 出力先の文字列参照ポインタ
1711 errr get_rnd_line_jonly(concptr file_name, int entry, char *output, int count)
1714 for (int i = 0; i < count; i++)
1716 result = get_rnd_line(file_name, entry, output);
1719 for (int j = 0; output[j]; j++) kanji |= iskanji(output[j]);
1729 * @brief 自動拾いファイルを読み込む /
1730 * @param creature_ptr プレーヤーへの参照ポインタ
1734 errr process_autopick_file(player_type *creature_ptr, concptr name)
1737 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
1738 errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK);
1744 * @brief プレイヤーの生い立ちファイルを読み込む /
1745 * Process file for player's history editor.
1746 * @param creature_ptr プレーヤーへの参照ポインタ
1751 errr process_histpref_file(player_type *creature_ptr, concptr name)
1753 bool old_character_xtra = current_world_ptr->character_xtra;
1755 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
1757 /* Hack -- prevent modification birth options in this file */
1758 current_world_ptr->character_xtra = TRUE;
1759 errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_HISTPREF);
1760 current_world_ptr->character_xtra = old_character_xtra;
1766 * @brief ファイル位置をシーク /
1767 * @param creature_ptr プレーヤーへの参照ポインタ
1768 * @param fd ファイルディスクリプタ
1769 * @param where ファイルバイト位置
1770 * @param flag FALSEならば現ファイルを超えた位置へシーク時エラー、TRUEなら足りない間を0で埋め尽くす
1774 static errr counts_seek(player_type *creature_ptr, int fd, u32b where, bool flag)
1776 char temp1[128], temp2[128];
1777 #ifdef SAVEFILE_USE_UID
1778 (void)sprintf(temp1, "%d.%s.%d%d%d", creature_ptr->player_uid, savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
1780 (void)sprintf(temp1, "%s.%d%d%d", savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
1782 for (int i = 0; temp1[i]; i++)
1783 temp1[i] ^= (i + 1) * 63;
1786 u32b zero_header[3] = { 0L, 0L, 0L };
1789 if (fd_seek(fd, seekpoint + 3 * sizeof(u32b)))
1791 if (fd_read(fd, (char*)(temp2), sizeof(temp2)))
1796 fd_seek(fd, seekpoint);
1797 fd_write(fd, (char*)zero_header, 3 * sizeof(u32b));
1798 fd_write(fd, (char*)(temp1), sizeof(temp1));
1802 if (strcmp(temp1, temp2) == 0)
1805 seekpoint += 128 + 3 * sizeof(u32b);
1808 return fd_seek(fd, seekpoint + where * sizeof(u32b));
1813 * @brief ファイル位置を読み込む
1814 * @param creature_ptr プレーヤーへの参照ポインタ
1815 * @param where ファイルバイト位置
1819 u32b counts_read(player_type *creature_ptr, int where)
1822 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
1823 int fd = fd_open(buf, O_RDONLY);
1826 if (counts_seek(creature_ptr, fd, where, FALSE) ||
1827 fd_read(fd, (char*)(&count), sizeof(u32b)))
1837 * @brief ファイル位置に書き込む /
1838 * @param creature_ptr プレーヤーへの参照ポインタ
1839 * @param where ファイルバイト位置
1840 * @param count 書き込む値
1844 errr counts_write(player_type *creature_ptr, int where, u32b count)
1847 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
1850 int fd = fd_open(buf, O_RDWR);
1854 FILE_TYPE(FILE_TYPE_DATA);
1856 fd = fd_make(buf, 0644);
1861 errr err = fd_lock(fd, F_WRLCK);
1865 counts_seek(creature_ptr, fd, where, TRUE);
1866 fd_write(fd, (char*)(&count), sizeof(u32b));
1868 err = fd_lock(fd, F_UNLCK);
1879 * @brief 墓のアスキーアートテンプレを読み込む
1880 * @param buf テンプレへのバッファ
1881 * @param buf_size バッファの長さ
1884 void read_dead_file(char *buf, size_t buf_size)
1886 path_build(buf, buf_size, ANGBAND_DIR_FILE, _("dead_j.txt", "dead.txt"));
1889 fp = my_fopen(buf, "r");
1893 while (my_fgets(fp, buf, buf_size) == 0)
1895 put_str(buf, i++, 0);