OSDN Git Service

Merge pull request #783 from shimitei/feature/#774_newgame_bgm
[hengbandforosx/hengbandosx.git] / src / cmd-io / cmd-macro.cpp
1 #include "cmd-io/cmd-macro.h"
2 #include "cmd-io/cmd-gameoption.h"
3 #include "cmd-io/macro-util.h"
4 #include "core/asking-player.h"
5 #include "game-option/input-options.h"
6 #include "io/files-util.h"
7 #include "io/input-key-acceptor.h"
8 #include "io/input-key-requester.h"
9 #include "io/read-pref-file.h"
10 #include "main/sound-of-music.h"
11 #include "term/screen-processor.h"
12 #include "term/term-color-types.h"
13 #include "util/angband-files.h"
14 #include "util/int-char-converter.h"
15 #include "util/string-processor.h"
16 #include "view/display-messages.h"
17
18 /*!
19  * @brief マクロ情報をprefファイルに保存する /
20  * @param fname ファイル名
21  * @return なし
22  */
23 static void macro_dump(FILE **fpp, concptr fname)
24 {
25         static concptr mark = "Macro Dump";
26         char buf[1024];
27         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
28         if (!open_auto_dump(fpp, buf, mark)) return;
29
30         auto_dump_printf(*fpp, _("\n# 自動マクロセーブ\n\n", "\n# Automatic macro dump\n\n"));
31
32         for (int i = 0; i < macro__num; i++)
33         {
34                 ascii_to_text(buf, macro__act[i]);
35                 auto_dump_printf(*fpp, "A:%s\n", buf);
36                 ascii_to_text(buf, macro__pat[i]);
37                 auto_dump_printf(*fpp, "P:%s\n", buf);
38                 auto_dump_printf(*fpp, "\n");
39         }
40
41         close_auto_dump(fpp, mark);
42 }
43
44
45 /*!
46  * @brief マクロのトリガーキーを取得する /
47  * Hack -- ask for a "trigger" (see below)
48  * @param buf キー表記を保管するバッファ
49  * @return なし
50  * @details
51  * <pre>
52  * Note the complex use of the "inkey()" function from "util.c".
53  *
54  * Note that both "flush()" calls are extremely important.
55  * </pre>
56  */
57 static void do_cmd_macro_aux(char *buf)
58 {
59         flush();
60         inkey_base = TRUE;
61         char i = inkey();
62         int n = 0;
63         while (i)
64         {
65                 buf[n++] = i;
66                 inkey_base = TRUE;
67                 inkey_scan = TRUE;
68                 i = inkey();
69         }
70
71         buf[n] = '\0';
72         flush();
73         char tmp[1024];
74         ascii_to_text(tmp, buf);
75         term_addstr(-1, TERM_WHITE, tmp);
76 }
77
78
79 /*!
80  * @brief マクロのキー表記からアスキーコードを得てターミナルに表示する /
81  * Hack -- ask for a keymap "trigger" (see below)
82  * @param buf キー表記を取得するバッファ
83  * @return なし
84  * @details
85  * <pre>
86  * Note that both "flush()" calls are extremely important.  This may
87  * no longer be true, since "util.c" is much simpler now.
88  * </pre>
89  */
90 static void do_cmd_macro_aux_keymap(char *buf)
91 {
92         char tmp[1024];
93         flush();
94         buf[0] = inkey();
95         buf[1] = '\0';
96         ascii_to_text(tmp, buf);
97         term_addstr(-1, TERM_WHITE, tmp);
98         flush();
99 }
100
101
102 /*!
103  * @brief キーマップをprefファイルにダンプする /
104  * Hack -- append all keymaps to the given file
105  * @param fname ファイルネーム
106  * @return エラーコード
107  * @details
108  */
109 static errr keymap_dump(concptr fname)
110 {
111         FILE *auto_dump_stream;
112         static concptr mark = "Keymap Dump";
113         char key[1024];
114         char buf[1024];
115         BIT_FLAGS mode;
116         if (rogue_like_commands)
117         {
118                 mode = KEYMAP_MODE_ROGUE;
119         }
120         else
121         {
122                 mode = KEYMAP_MODE_ORIG;
123         }
124
125         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
126         if (!open_auto_dump(&auto_dump_stream, buf, mark)) return -1;
127
128         auto_dump_printf(auto_dump_stream, _("\n# 自動キー配置セーブ\n\n", "\n# Automatic keymap dump\n\n"));
129         for (int i = 0; i < 256; i++)
130         {
131                 concptr act;
132                 act = keymap_act[mode][i];
133                 if (!act) continue;
134
135                 buf[0] = (char)i;
136                 buf[1] = '\0';
137                 ascii_to_text(key, buf);
138                 ascii_to_text(buf, act);
139                 auto_dump_printf(auto_dump_stream, "A:%s\n", buf);
140                 auto_dump_printf(auto_dump_stream, "C:%d:%s\n", mode, key);
141         }
142
143         close_auto_dump(&auto_dump_stream, mark);
144         return 0;
145 }
146
147
148 /*!
149  * @brief マクロを設定するコマンドのメインルーチン /
150  * Interact with "macros"
151  * @return なし
152  * @details
153  * <pre>
154  * Note that the macro "action" must be defined before the trigger.
155  *
156  * Could use some helpful instructions on this page.
157  * </pre>
158  */
159 void do_cmd_macros(player_type *creature_ptr)
160 {
161         char tmp[1024];
162         char buf[1024];
163         FILE *auto_dump_stream;
164         BIT_FLAGS mode = rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG;
165         screen_save();
166         while (TRUE)
167         {
168                 term_clear();
169                 prt(_("[ マクロの設定 ]", "Interact with Macros"), 2, 0);
170                 prt(_("マクロ行動が(もしあれば)下に表示されます:", "Current action (if any) shown below:"), 20, 0);
171                 ascii_to_text(buf, macro__buf);
172                 prt(buf, 22, 0);
173
174                 prt(_("(1) ユーザー設定ファイルのロード", "(1) Load a user pref file"), 4, 5);
175                 prt(_("(2) ファイルにマクロを追加", "(2) Append macros to a file"), 5, 5);
176                 prt(_("(3) マクロの確認", "(3) Query a macro"), 6, 5);
177                 prt(_("(4) マクロの作成", "(4) Create a macro"), 7, 5);
178                 prt(_("(5) マクロの削除", "(5) Remove a macro"), 8, 5);
179                 prt(_("(6) ファイルにキー配置を追加", "(6) Append keymaps to a file"), 9, 5);
180                 prt(_("(7) キー配置の確認", "(7) Query a keymap"), 10, 5);
181                 prt(_("(8) キー配置の作成", "(8) Create a keymap"), 11, 5);
182                 prt(_("(9) キー配置の削除", "(9) Remove a keymap"), 12, 5);
183                 prt(_("(0) マクロ行動の入力", "(0) Enter a new action"), 13, 5);
184
185                 prt(_("コマンド: ", "Command: "), 16, 0);
186                 int i = inkey();
187                 if (i == ESCAPE) break;
188
189                 else if (i == '1')
190                 {
191                         prt(_("コマンド: ユーザー設定ファイルのロード", "Command: Load a user pref file"), 16, 0);
192                         prt(_("ファイル: ", "File: "), 18, 0);
193                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
194                         if (!askfor(tmp, 80)) continue;
195
196                         errr err = process_pref_file(creature_ptr, tmp);
197                         if (-2 == err)
198                                 msg_format(_("標準の設定ファイル'%s'を読み込みました。", "Loaded default '%s'."), tmp);
199                         else if (err)
200                                 msg_format(_("'%s'の読み込みに失敗しました!", "Failed to load '%s'!"), tmp);
201                         else
202                                 msg_format(_("'%s'を読み込みました。", "Loaded '%s'."), tmp);
203                 }
204                 else if (i == '2')
205                 {
206                         prt(_("コマンド: マクロをファイルに追加する", "Command: Append macros to a file"), 16, 0);
207                         prt(_("ファイル: ", "File: "), 18, 0);
208                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
209                         if (!askfor(tmp, 80)) continue;
210
211                         macro_dump(&auto_dump_stream, tmp);
212                         msg_print(_("マクロを追加しました。", "Appended macros."));
213                 }
214                 else if (i == '3')
215                 {
216                         prt(_("コマンド: マクロの確認", "Command: Query a macro"), 16, 0);
217                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
218                         do_cmd_macro_aux(buf);
219                         int k = macro_find_exact(buf);
220                         if (k < 0)
221                         {
222                                 msg_print(_("そのキーにはマクロは定義されていません。", "Found no macro."));
223                         }
224                         else
225                         {
226                                 strcpy(macro__buf, macro__act[k]);
227                                 ascii_to_text(buf, macro__buf);
228                                 prt(buf, 22, 0);
229                                 msg_print(_("マクロを確認しました。", "Found a macro."));
230                         }
231                 }
232                 else if (i == '4')
233                 {
234                         prt(_("コマンド: マクロの作成", "Command: Create a macro"), 16, 0);
235                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
236                         do_cmd_macro_aux(buf);
237                         clear_from(20);
238                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
239                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
240                         prt(_("マクロ行動: ", "Action: "), 20, 0);
241                         ascii_to_text(tmp, macro__buf);
242                         if (askfor(tmp, 80))
243                         {
244                                 text_to_ascii(macro__buf, tmp);
245                                 macro_add(buf, macro__buf);
246                                 msg_print(_("マクロを追加しました。", "Added a macro."));
247                         }
248                 }
249                 else if (i == '5')
250                 {
251                         prt(_("コマンド: マクロの削除", "Command: Remove a macro"), 16, 0);
252                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
253                         do_cmd_macro_aux(buf);
254                         macro_add(buf, buf);
255                         msg_print(_("マクロを削除しました。", "Removed a macro."));
256                 }
257                 else if (i == '6')
258                 {
259                         prt(_("コマンド: キー配置をファイルに追加する", "Command: Append keymaps to a file"), 16, 0);
260                         prt(_("ファイル: ", "File: "), 18, 0);
261                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
262                         if (!askfor(tmp, 80)) continue;
263
264                         (void)keymap_dump(tmp);
265                         msg_print(_("キー配置を追加しました。", "Appended keymaps."));
266                 }
267                 else if (i == '7')
268                 {
269                         prt(_("コマンド: キー配置の確認", "Command: Query a keymap"), 16, 0);
270                         prt(_("押すキー: ", "Keypress: "), 18, 0);
271                         do_cmd_macro_aux_keymap(buf);
272                         concptr act = keymap_act[mode][(byte)(buf[0])];
273                         if (!act)
274                         {
275                                 msg_print(_("キー配置は定義されていません。", "Found no keymap."));
276                         }
277                         else
278                         {
279                                 strcpy(macro__buf, act);
280                                 ascii_to_text(buf, macro__buf);
281                                 prt(buf, 22, 0);
282                                 msg_print(_("キー配置を確認しました。", "Found a keymap."));
283                         }
284                 }
285                 else if (i == '8')
286                 {
287                         prt(_("コマンド: キー配置の作成", "Command: Create a keymap"), 16, 0);
288                         prt(_("押すキー: ", "Keypress: "), 18, 0);
289                         do_cmd_macro_aux_keymap(buf);
290                         clear_from(20);
291                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
292                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
293                         prt(_("行動: ", "Action: "), 20, 0);
294                         ascii_to_text(tmp, macro__buf);
295                         if (askfor(tmp, 80))
296                         {
297                                 text_to_ascii(macro__buf, tmp);
298                                 string_free(keymap_act[mode][(byte)(buf[0])]);
299                                 keymap_act[mode][(byte)(buf[0])] = string_make(macro__buf);
300                                 msg_print(_("キー配置を追加しました。", "Added a keymap."));
301                         }
302                 }
303                 else if (i == '9')
304                 {
305                         prt(_("コマンド: キー配置の削除", "Command: Remove a keymap"), 16, 0);
306                         prt(_("押すキー: ", "Keypress: "), 18, 0);
307                         do_cmd_macro_aux_keymap(buf);
308                         string_free(keymap_act[mode][(byte)(buf[0])]);
309                         keymap_act[mode][(byte)(buf[0])] = NULL;
310                         msg_print(_("キー配置を削除しました。", "Removed a keymap."));
311                 }
312                 else if (i == '0')
313                 {
314                         prt(_("コマンド: マクロ行動の入力", "Command: Enter a new action"), 16, 0);
315                         clear_from(20);
316                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
317                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
318                         prt(_("マクロ行動: ", "Action: "), 20, 0);
319                         tmp[80] = '\0';
320                         if (!askfor(buf, 80)) continue;
321
322                         text_to_ascii(macro__buf, buf);
323                 }
324                 else
325                 {
326                         bell();
327                 }
328
329                 msg_erase();
330         }
331
332         screen_load();
333 }