2 * @brief prefファイルの内容を解釈しメモリに展開する
7 #include "io/interpret-pref-file.h"
8 #include "birth/character-builder.h"
9 #include "cmd-io/macro-util.h"
10 #include "game-option/option-flags.h"
11 #include "game-option/option-types-table.h"
12 #include "grid/feature.h"
13 #include "io/gf-descriptions.h"
14 #include "io/input-key-requester.h"
15 #include "io/tokenizer.h"
16 #include "object/object-kind.h"
17 #include "monster-race/monster-race.h"
18 #include "system/game-option-types.h"
19 #include "system/monster-race-definition.h"
20 #include "system/player-type-definition.h"
21 #include "term/gameterm.h"
22 #include "util/quarks.h"
23 #include "util/string-processor.h"
24 #include "view/display-messages.h"
25 #include "world/world.h"
27 #define MAX_MACRO_CHARS 16128 // 1つのマクロキー押下で実行可能なコマンド最大数 (エスケープシーケンス含む).
29 char *histpref_buf = NULL;
32 * @brief Rトークンの解釈 / Process "R:<num>:<a>/<c>" -- attr/char for monster races
36 static errr interpret_r_token(char *buf)
39 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
42 int i = (int)strtol(zz[0], NULL, 0);
43 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
44 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
45 if (i >= max_r_idx) return 1;
48 if (n1 || (!(n2 & 0x80) && n2)) r_ptr->x_attr = n1; /* Allow TERM_DARK text */
49 if (n2) r_ptr->x_char = n2;
56 * @brief Kトークンの解釈 / Process "K:<num>:<a>/<c>" -- attr/char for object kinds
60 static errr interpret_k_token(char *buf)
63 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
66 int i = (int)strtol(zz[0], NULL, 0);
67 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
68 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
69 if (i >= max_k_idx) return 1;
72 if (n1 || (!(n2 & 0x80) && n2)) k_ptr->x_attr = n1; /* Allow TERM_DARK text */
73 if (n2) k_ptr->x_char = n2;
80 * @brief トークン数によって地形の文字形と色を決定する
85 static errr decide_feature_type(int i, int num, char **zz)
90 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
91 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
92 if (n1 || (!(n2 & 0x80) && n2)) f_ptr->x_attr[F_LIT_STANDARD] = n1; /* Allow TERM_DARK text */
93 if (n2) f_ptr->x_char[F_LIT_STANDARD] = n2;
99 /* No lighting support */
100 n1 = f_ptr->x_attr[F_LIT_STANDARD];
101 n2 = f_ptr->x_char[F_LIT_STANDARD];
102 for (int j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++)
104 f_ptr->x_attr[j] = n1;
105 f_ptr->x_char[j] = n2;
112 /* Use default lighting */
113 apply_default_feat_lighting(f_ptr->x_attr, f_ptr->x_char);
116 case F_LIT_MAX * 2 + 1:
118 /* Use desired lighting */
119 for (int j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++)
121 n1 = (TERM_COLOR)strtol(zz[j * 2 + 1], NULL, 0);
122 n2 = (SYMBOL_CODE)strtol(zz[j * 2 + 2], NULL, 0);
123 if (n1 || (!(n2 & 0x80) && n2)) f_ptr->x_attr[j] = n1; /* Allow TERM_DARK text */
124 if (n2) f_ptr->x_char[j] = n2;
136 * @brief Fトークンの解釈 / Process "F:<num>:<a>/<c>" -- attr/char for terrain features
141 * "F:<num>:<a>/<c>:LIT"
142 * "F:<num>:<a>/<c>:<la>/<lc>:<da>/<dc>"
144 static errr interpret_f_token(char *buf)
147 int num = tokenize(buf + 2, F_LIT_MAX * 2 + 1, zz, TOKENIZE_CHECKQUOTE);
149 if ((num != 3) && (num != 4) && (num != F_LIT_MAX * 2 + 1)) return 1;
150 else if ((num == 4) && !streq(zz[3], "LIT")) return 1;
152 int i = (int)strtol(zz[0], NULL, 0);
153 if (i >= max_f_idx) return 1;
155 return decide_feature_type(i, num, zz);
160 * @brief Fトークンの解釈 / Process "S:<num>:<a>/<c>" -- attr/char for special things
164 static errr interpret_s_token(char *buf)
167 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
169 int j = (byte)strtol(zz[0], NULL, 0);
170 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
171 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
172 misc_to_attr[j] = n1;
173 misc_to_char[j] = n2;
179 * @brief Uトークンの解釈 / Process "U:<tv>:<a>/<c>" -- attr/char for unaware items
183 static errr interpret_u_token(char *buf)
186 if (tokenize(buf + 2, 3, zz, TOKENIZE_CHECKQUOTE) != 3) return 1;
188 int j = (int)strtol(zz[0], NULL, 0);
189 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
190 SYMBOL_CODE n2 = (SYMBOL_CODE)strtol(zz[2], NULL, 0);
191 for (int i = 1; i < max_k_idx; i++)
193 object_kind *k_ptr = &k_info[i];
194 if (k_ptr->tval == j)
196 if (n1) k_ptr->d_attr = n1;
197 if (n2) k_ptr->d_char = n2;
206 * @brief Eトークンの解釈 / Process "E:<tv>:<a>" -- attribute for inventory objects
210 static errr interpret_e_token(char *buf)
213 if (tokenize(buf + 2, 2, zz, TOKENIZE_CHECKQUOTE) != 2) return 1;
215 int j = (byte)strtol(zz[0], NULL, 0) % 128;
216 TERM_COLOR n1 = (TERM_COLOR)strtol(zz[1], NULL, 0);
217 if (n1) tval_to_attr[j] = n1;
223 * @brief Pトークンの解釈 / Process "P:<str>" -- normal macro
227 static errr interpret_p_token(char *buf)
230 text_to_ascii(tmp, buf + 2);
231 return macro_add(tmp, macro__buf);
236 * @brief Cトークンの解釈 / Process "C:<str>" -- create keymap
240 static errr interpret_c_token(char *buf)
243 if (tokenize(buf + 2, 2, zz, TOKENIZE_CHECKQUOTE) != 2) return 1;
245 int mode = strtol(zz[0], NULL, 0);
246 if ((mode < 0) || (mode >= KEYMAP_MODES)) return 1;
249 text_to_ascii(tmp, zz[1]);
250 if (!tmp[0] || tmp[1]) return 1;
252 int i = (byte)(tmp[0]);
253 string_free(keymap_act[mode][i]);
254 keymap_act[mode][i] = string_make(macro__buf);
260 * @brief Vトークンの解釈 / Process "V:<num>:<kv>:<rv>:<gv>:<bv>" -- visual info
264 static errr interpret_v_token(char *buf)
267 if (tokenize(buf + 2, 5, zz, TOKENIZE_CHECKQUOTE) != 5) return 1;
269 int i = (byte)strtol(zz[0], NULL, 0);
270 angband_color_table[i][0] = (byte)strtol(zz[1], NULL, 0);
271 angband_color_table[i][1] = (byte)strtol(zz[2], NULL, 0);
272 angband_color_table[i][2] = (byte)strtol(zz[3], NULL, 0);
273 angband_color_table[i][3] = (byte)strtol(zz[4], NULL, 0);
280 * @param creature_ptr プレーヤーへの参照ポインタ
284 * Process "X:<str>" -- turn option off
285 * Process "Y:<str>" -- turn option on
287 static errr interpret_xy_token(player_type *creature_ptr, char *buf)
289 for (int i = 0; option_info[i].o_desc; i++)
291 bool is_option = option_info[i].o_var != NULL;
292 is_option &= option_info[i].o_text != NULL;
293 is_option &= streq(option_info[i].o_text, buf + 2);
294 if (!is_option) continue;
296 int os = option_info[i].o_set;
297 int ob = option_info[i].o_bit;
299 if ((creature_ptr->playing || current_world_ptr->character_xtra) &&
300 (OPT_PAGE_BIRTH == option_info[i].o_page) && !current_world_ptr->wizard)
302 msg_format(_("初期オプションは変更できません! '%s'", "Birth options can not changed! '%s'"), buf);
309 option_flag[os] &= ~(1UL << ob);
310 (*option_info[i].o_var) = FALSE;
314 option_flag[os] |= (1UL << ob);
315 (*option_info[i].o_var) = TRUE;
319 msg_format(_("オプションの名前が正しくありません: %s", "Ignored invalid option: %s"), buf);
326 * @brief Zトークンの解釈 / Process "Z:<type>:<str>" -- set spell color
328 * @param zz トークン保管文字列
331 static errr interpret_z_token(char *buf)
333 char *t = angband_strchr(buf + 2, ':');
337 for (int i = 0; i < MAX_NAMED_NUM; i++)
339 if (!streq(gf_desc[i].name, buf + 2)) continue;
341 gf_color[gf_desc[i].num] = (TERM_COLOR)quark_add(t);
350 * @brief Tトークンの解釈 / Process "T:<template>:<modifier chr>:<modifier name>:..." for 4 tokens
352 * @param zz トークン保管文字列
355 static errr decide_template_modifier(int tok, char **zz)
357 if (macro_template != NULL)
359 int macro_modifier_length = strlen(macro_modifier_chr);
360 string_free(macro_template);
361 macro_template = NULL;
362 string_free(macro_modifier_chr);
363 for (int i = 0; i < macro_modifier_length; i++)
365 string_free(macro_modifier_name[i]);
368 for (int i = 0; i < max_macrotrigger; i++)
370 string_free(macro_trigger_name[i]);
371 string_free(macro_trigger_keycode[0][i]);
372 string_free(macro_trigger_keycode[1][i]);
375 max_macrotrigger = 0;
378 if (*zz[0] == '\0') return 0;
380 int zz_length = strlen(zz[1]);
381 zz_length = MIN(MAX_MACRO_MOD, zz_length);
382 if (2 + zz_length != tok) return 1;
384 macro_template = string_make(zz[0]);
385 macro_modifier_chr = string_make(zz[1]);
386 for (int i = 0; i < zz_length; i++)
388 macro_modifier_name[i] = string_make(zz[2 + i]);
396 * @brief Tトークンの解釈 / Process "T:<trigger>:<keycode>:<shift-keycode>" for 2 or 3 tokens
398 * @param zz トークン保管文字列
401 static errr interpret_macro_keycodes(int tok, char **zz)
403 char buf_aux[MAX_MACRO_CHARS];
405 if (max_macrotrigger >= MAX_MACRO_TRIG)
407 msg_print(_("マクロトリガーの設定が多すぎます!", "Too many macro triggers!"));
411 int m = max_macrotrigger;
422 macro_trigger_name[m] = string_make(buf_aux);
423 macro_trigger_keycode[0][m] = string_make(zz[1]);
426 macro_trigger_keycode[1][m] = string_make(zz[2]);
430 macro_trigger_keycode[1][m] = string_make(zz[1]);
436 * @brief Tトークンの個数調査 (解釈はサブルーチンで) / Initialize macro trigger names and a template
439 * @todo 2.2.1r時点のコードからトークン数0~1の場合もエラーコード0だが、1であるべきでは?
441 static errr interpret_t_token(char *buf)
444 int tok = tokenize(buf + 2, 2 + MAX_MACRO_MOD, zz, 0);
445 if (tok >= 4) return decide_template_modifier(tok, zz);
446 if (tok < 2) return 0;
448 return interpret_macro_keycodes(tok, zz);
453 * @brief 設定ファイルの各行から各種テキスト情報を取得する /
454 * Parse a sub-file of the "extra info" (format shown below)
455 * @param creature_ptr プレーヤーへの参照ポインタ
456 * @param buf データテキストの参照ポインタ
460 * Each "action" line has an "action symbol" in the first column,
461 * followed by a colon, followed by some command specific info,
462 * usually in the form of "tokens" separated by colons or slashes.
463 * Blank lines, lines starting with white space, and lines starting
464 * with pound signs ("#") are ignored (as comments).
465 * Note the use of "tokenize()" to allow the use of both colons and
466 * slashes as delimeters, while still allowing final tokens which
467 * may contain any characters including "delimiters".
468 * Note the use of "strtol()" to allow all "integers" to be encoded
469 * in decimal, hexidecimal, or octal form.
470 * Note that "monster zero" is used for the "player" attr/char, "object
471 * zero" will be used for the "stack" attr/char, and "feature zero" is
472 * used for the "nothing" attr/char.
475 errr interpret_pref_file(player_type *creature_ptr, char *buf)
477 if (buf[1] != ':') return 1;
483 /* Process "H:<history>" */
484 add_history_from_pref_line(buf + 2);
488 return interpret_r_token(buf);
490 return interpret_k_token(buf);
492 return interpret_f_token(buf);
494 return interpret_s_token(buf);
496 return interpret_u_token(buf);
498 return interpret_e_token(buf);
501 /* Process "A:<str>" -- save an "action" for later */
502 text_to_ascii(macro__buf, buf + 2);
506 return interpret_p_token(buf);
508 return interpret_c_token(buf);
510 return interpret_v_token(buf);
513 return interpret_xy_token(creature_ptr, buf);
515 return interpret_z_token(buf);
517 return interpret_t_token(buf);
524 * @brief 生い立ちメッセージの内容をバッファに加える。 / Hook function for reading the histpref.prf file.
526 void add_history_from_pref_line(concptr t)
531 angband_strcat(histpref_buf, t, HISTPREF_LIMIT);