OSDN Git Service

[Refactor] #2631 create_named_art() の中でArtifactType::is_generated をtrueにするよう設計を変えた
[hengbandforosx/hengbandosx.git] / src / wizard / wizard-item-modifier.cpp
1 #include "wizard/wizard-item-modifier.h"
2 #include "artifact/fixed-art-generator.h"
3 #include "artifact/fixed-art-types.h"
4 #include "artifact/random-art-effects.h"
5 #include "artifact/random-art-generator.h"
6 #include "core/asking-player.h"
7 #include "core/player-update-types.h"
8 #include "core/show-file.h"
9 #include "core/stuff-handler.h"
10 #include "core/window-redrawer.h"
11 #include "flavor/flavor-describer.h"
12 #include "flavor/object-flavor-types.h"
13 #include "floor/floor-object.h"
14 #include "game-option/cheat-options.h"
15 #include "inventory/inventory-slot-types.h"
16 #include "io/input-key-acceptor.h"
17 #include "io/input-key-requester.h"
18 #include "object-enchant/item-apply-magic.h"
19 #include "object-enchant/item-magic-applier.h"
20 #include "object-enchant/object-ego.h"
21 #include "object-enchant/special-object-flags.h"
22 #include "object-enchant/tr-types.h"
23 #include "object/item-use-flags.h"
24 #include "object/object-flags.h"
25 #include "object/object-info.h"
26 #include "object/object-kind-hook.h"
27 #include "object/object-kind.h"
28 #include "object/object-mark-types.h"
29 #include "object/object-value.h"
30 #include "spell-kind/spells-perception.h"
31 #include "spell/spells-object.h"
32 #include "system/alloc-entries.h"
33 #include "system/artifact-type-definition.h"
34 #include "system/floor-type-definition.h"
35 #include "system/object-type-definition.h"
36 #include "system/player-type-definition.h"
37 #include "system/system-variables.h"
38 #include "term/screen-processor.h"
39 #include "term/term-color-types.h"
40 #include "util/bit-flags-calculator.h"
41 #include "util/int-char-converter.h"
42 #include "util/string-processor.h"
43 #include "view/display-messages.h"
44 #include "wizard/wizard-special-process.h"
45 #include "world/world.h"
46 #include <algorithm>
47 #include <limits>
48 #include <sstream>
49 #include <tuple>
50 #include <vector>
51
52 #define K_MAX_DEPTH 110 /*!< アイテムの階層毎生成率を表示する最大階 */
53
54 namespace {
55 /*!
56  * @brief アイテム設定コマンド一覧表
57  */
58 constexpr std::array wizard_sub_menu_table = {
59     std::make_tuple('a', _("アーティファクト出現フラグリセット", "Restore aware flag of fixed artifact")),
60     std::make_tuple('A', _("アーティファクトを出現済みにする", "Make a fixed artifact awared")),
61     std::make_tuple('e', _("高級品獲得ドロップ", "Drop excellent object")),
62     std::make_tuple('f', _("*鑑定*", "*Idenfity*")),
63     std::make_tuple('i', _("鑑定", "Idenfity")),
64     std::make_tuple('I', _("インベントリ全*鑑定*", "Idenfity all objects fully in inventory")),
65     std::make_tuple('l', _("指定アイテム番号まで一括鑑定", "Make objects awared to target object id")),
66     std::make_tuple('g', _("上質なアイテムドロップ", "Drop good object")),
67     std::make_tuple('s', _("特別品獲得ドロップ", "Drop special object")),
68     std::make_tuple('w', _("願い", "Wishing")),
69     std::make_tuple('U', _("発動を変更する", "Modify item activation")),
70 };
71
72 /*!
73  * @brief ゲーム設定コマンドの一覧を表示する
74  */
75 void display_wizard_sub_menu()
76 {
77     for (auto y = 1U; y <= wizard_sub_menu_table.size(); y++) {
78         term_erase(14, y, 64);
79     }
80
81     int r = 1;
82     int c = 15;
83     for (const auto &[symbol, desc] : wizard_sub_menu_table) {
84         std::stringstream ss;
85         ss << symbol << ") " << desc;
86         put_str(ss.str().c_str(), r++, c);
87     }
88 }
89 }
90
91 /*!
92  * @brief キャスト先の型の最小値、最大値でclampする。
93  */
94 template <typename T>
95 T clamp_cast(int val)
96 {
97     return static_cast<T>(std::clamp(val,
98         static_cast<int>(std::numeric_limits<T>::min()),
99         static_cast<int>(std::numeric_limits<T>::max())));
100 }
101
102 void wiz_restore_aware_flag_of_fixed_arfifact(FixedArtifactId reset_artifact_idx, bool aware = false);
103 void wiz_modify_item_activation(PlayerType *player_ptr);
104 void wiz_identify_full_inventory(PlayerType *player_ptr);
105
106 /*!
107  * @brief ゲーム設定コマンドの入力を受け付ける
108  * @param player_ptr プレイヤーの情報へのポインタ
109  */
110 void wizard_item_modifier(PlayerType *player_ptr)
111 {
112     screen_save();
113     display_wizard_sub_menu();
114
115     char cmd;
116     get_com("Player Command: ", &cmd, false);
117     screen_load();
118
119     switch (cmd) {
120     case ESCAPE:
121     case ' ':
122     case '\n':
123     case '\r':
124         break;
125     case 'a':
126         wiz_restore_aware_flag_of_fixed_arfifact(i2enum<FixedArtifactId>(command_arg));
127         break;
128     case 'A':
129         wiz_restore_aware_flag_of_fixed_arfifact(i2enum<FixedArtifactId>(command_arg), true);
130         break;
131     case 'e':
132         if (command_arg <= 0) {
133             command_arg = 1;
134         }
135
136         acquirement(player_ptr, player_ptr->y, player_ptr->x, command_arg, true, false, true);
137         break;
138     case 'f':
139         identify_fully(player_ptr, false);
140         break;
141     case 'g':
142         if (command_arg <= 0) {
143             command_arg = 1;
144         }
145
146         acquirement(player_ptr, player_ptr->y, player_ptr->x, command_arg, false, false, true);
147         break;
148     case 'i':
149         (void)ident_spell(player_ptr, false);
150         break;
151     case 'I':
152         wiz_identify_full_inventory(player_ptr);
153         break;
154     case 'l':
155         wiz_learn_items_all(player_ptr);
156         break;
157     case 's':
158         if (command_arg <= 0) {
159             command_arg = 1;
160         }
161
162         acquirement(player_ptr, player_ptr->y, player_ptr->x, command_arg, true, true, true);
163         break;
164     case 'U':
165         wiz_modify_item_activation(player_ptr);
166         break;
167     case 'w':
168         do_cmd_wishing(player_ptr, -1, true, true, true);
169         break;
170     }
171 }
172
173 /*!
174  * @brief 固定アーティファクトの出現フラグをリセットする
175  * @param a_idx 指定したアーティファクトID
176  * @details 外からはenum class を受け取るが、この関数内では数値の直指定処理なので数値型にキャストする.
177  */
178 void wiz_restore_aware_flag_of_fixed_arfifact(FixedArtifactId reset_artifact_idx, bool aware)
179 {
180     auto max_a_idx = enum2i(a_info.rbegin()->first);
181     auto int_a_idx = enum2i(reset_artifact_idx);
182     if (int_a_idx <= 0) {
183         char tmp[80] = "";
184         sprintf(tmp, "Artifact ID (1-%d): ", max_a_idx);
185         char tmp_val[10] = "";
186         if (!get_string(tmp, tmp_val, 3)) {
187             return;
188         }
189
190         int_a_idx = static_cast<short>(atoi(tmp_val));
191     }
192
193     if ((int_a_idx <= 0) || (int_a_idx > static_cast<short>(max_a_idx))) {
194         msg_format(_("番号は1から%dの間で指定して下さい。", "ID must be between 1 to %d."), max_a_idx);
195         return;
196     }
197
198     a_info.at(i2enum<FixedArtifactId>(int_a_idx)).is_generated = aware;
199     msg_print(aware ? "Modified." : "Restored.");
200 }
201
202 /*!
203  * @brief オブジェクトに発動を追加する/変更する
204  * @param catser_ptr プレイヤー情報への参照ポインタ
205  */
206 void wiz_modify_item_activation(PlayerType *player_ptr)
207 {
208     auto q = _("どのアイテムの発動を変更しますか? ", "Which object? ");
209     auto s = _("発動を変更するアイテムがない。", "Nothing to do with.");
210     OBJECT_IDX item;
211     auto *o_ptr = choose_object(player_ptr, &item, q, s, USE_EQUIP | USE_INVEN | USE_FLOOR | IGNORE_BOTHHAND_SLOT);
212     if (!o_ptr) {
213         return;
214     }
215
216     int val;
217     if (!get_value("Activation ID", enum2i(RandomArtActType::NONE), enum2i(RandomArtActType::MAX) - 1, &val)) {
218         return;
219     }
220
221     auto act_idx = i2enum<RandomArtActType>(val);
222     o_ptr->art_flags.set(TR_ACTIVATE);
223     o_ptr->activation_id = act_idx;
224 }
225
226 /*!
227  * @brief インベントリ内のアイテムを全て*鑑定*済みにする
228  * @param catser_ptr プレイヤー情報への参照ポインタ
229  */
230 void wiz_identify_full_inventory(PlayerType *player_ptr)
231 {
232     for (int i = 0; i < INVEN_TOTAL; i++) {
233         auto *o_ptr = &player_ptr->inventory_list[i];
234         if (!o_ptr->k_idx) {
235             continue;
236         }
237
238         auto k_ptr = &k_info[o_ptr->k_idx];
239         k_ptr->aware = true; //!< @note 記録には残さないためTRUEを立てるのみ
240         set_bits(o_ptr->ident, IDENT_KNOWN | IDENT_FULL_KNOWN);
241         set_bits(o_ptr->marked, OM_TOUCHED);
242     }
243
244     /* Refrect item informaiton onto subwindows without updating inventory */
245     reset_bits(player_ptr->update, PU_COMBINE | PU_REORDER);
246     handle_stuff(player_ptr);
247     set_bits(player_ptr->update, PU_COMBINE | PU_REORDER);
248     set_bits(player_ptr->window_flags, PW_INVEN | PW_EQUIP);
249 }
250
251 /*!
252  * @brief アイテムの階層毎生成率を表示する / Output a rarity graph for a type of object.
253  * @param tval ベースアイテムの大項目ID
254  * @param sval ベースアイテムの小項目ID
255  * @param row 表示列
256  * @param col 表示行
257  */
258 static void prt_alloc(ItemKindType tval, OBJECT_SUBTYPE_VALUE sval, TERM_LEN row, TERM_LEN col)
259 {
260     uint32_t rarity[K_MAX_DEPTH] = {};
261     uint32_t total[K_MAX_DEPTH] = {};
262     int32_t display[22] = {};
263
264     int home = 0;
265     for (int i = 0; i < K_MAX_DEPTH; i++) {
266         int total_frac = 0;
267         object_kind *k_ptr;
268         for (const auto &entry : alloc_kind_table) {
269             PERCENTAGE prob = 0;
270
271             if (entry.level <= i) {
272                 prob = entry.prob1 * GREAT_OBJ * K_MAX_DEPTH;
273             } else if (entry.level - 1 > 0) {
274                 prob = entry.prob1 * i * K_MAX_DEPTH / (entry.level - 1);
275             }
276
277             k_ptr = &k_info[entry.index];
278
279             total[i] += prob / (GREAT_OBJ * K_MAX_DEPTH);
280             total_frac += prob % (GREAT_OBJ * K_MAX_DEPTH);
281
282             if ((k_ptr->tval == tval) && (k_ptr->sval == sval)) {
283                 home = k_ptr->level;
284                 rarity[i] += prob / (GREAT_OBJ * K_MAX_DEPTH);
285             }
286         }
287
288         total[i] += total_frac / (GREAT_OBJ * K_MAX_DEPTH);
289     }
290
291     for (int i = 0; i < 22; i++) {
292         int possibility = 0;
293         for (int j = i * K_MAX_DEPTH / 22; j < (i + 1) * K_MAX_DEPTH / 22; j++) {
294             possibility += rarity[j] * 100000 / total[j];
295         }
296
297         display[i] = possibility / 5;
298     }
299
300     for (int i = 0; i < 22; i++) {
301         term_putch(col, row + i + 1, TERM_WHITE, '|');
302         prt(format("%2dF", (i * 5)), row + i + 1, col);
303         if ((i * K_MAX_DEPTH / 22 <= home) && (home < (i + 1) * K_MAX_DEPTH / 22)) {
304             c_prt(TERM_RED, format("%3d.%04d%%", display[i] / 1000, display[i] % 1000), row + i + 1, col + 3);
305         } else {
306             c_prt(TERM_WHITE, format("%3d.%04d%%", display[i] / 1000, display[i] % 1000), row + i + 1, col + 3);
307         }
308     }
309
310     concptr r = "+---Rate---+";
311     prt(r, row, col);
312 }
313
314 /*!
315  * @brief 32ビット変数のビット配列を並べて描画する / Output a long int in binary format.
316  */
317 static void prt_binary(BIT_FLAGS flags, const int row, int col)
318 {
319     uint32_t bitmask;
320     for (int i = bitmask = 1; i <= 32; i++, bitmask *= 2) {
321         if (flags & bitmask) {
322             term_putch(col++, row, TERM_BLUE, '*');
323         } else {
324             term_putch(col++, row, TERM_WHITE, '-');
325         }
326     }
327 }
328
329 /*!
330  * @brief アイテムの詳細ステータスを表示する /
331  * Change various "permanent" player variables.
332  * @param player_ptr プレイヤーへの参照ポインタ
333  * @param o_ptr 詳細を表示するアイテム情報の参照ポインタ
334  */
335 static void wiz_display_item(PlayerType *player_ptr, ObjectType *o_ptr)
336 {
337     auto flgs = object_flags(o_ptr);
338     auto get_seq_32bits = [](const TrFlags &flgs, uint start) {
339         BIT_FLAGS result = 0U;
340         for (auto i = 0U; i < 32 && start + i < flgs.size(); i++) {
341             if (flgs.has(i2enum<tr_type>(start + i))) {
342                 result |= 1U << i;
343             }
344         }
345         return result;
346     };
347     int j = 13;
348     for (int i = 1; i <= 23; i++) {
349         prt("", i, j - 2);
350     }
351
352     prt_alloc(o_ptr->tval, o_ptr->sval, 1, 0);
353     char buf[256];
354     describe_flavor(player_ptr, buf, o_ptr, OD_STORE);
355     prt(buf, 2, j);
356
357     auto line = 4;
358     prt(format("kind = %-5d  level = %-4d  tval = %-5d  sval = %-5d", o_ptr->k_idx, k_info[o_ptr->k_idx].level, o_ptr->tval, o_ptr->sval), line, j);
359     prt(format("number = %-3d  wgt = %-6d  ac = %-5d    damage = %dd%d", o_ptr->number, o_ptr->weight, o_ptr->ac, o_ptr->dd, o_ptr->ds), ++line, j);
360     prt(format("pval = %-5d  toac = %-5d  tohit = %-4d  todam = %-4d", o_ptr->pval, o_ptr->to_a, o_ptr->to_h, o_ptr->to_d), ++line, j);
361     prt(format("fixed_artifact_idx = %-4d  ego_idx = %-4d  cost = %ld", o_ptr->fixed_artifact_idx, o_ptr->ego_idx, object_value_real(o_ptr)), ++line, j);
362     prt(format("ident = %04x  activation_id = %-4d  timeout = %-d", o_ptr->ident, o_ptr->activation_id, o_ptr->timeout), ++line, j);
363     prt(format("chest_level = %-4d  fuel = %-d", o_ptr->chest_level, o_ptr->fuel), ++line, j);
364     prt(format("smith_hit = %-4d  smith_damage = %-4d", o_ptr->smith_hit, o_ptr->smith_damage), ++line, j);
365     prt(format("cursed  = %-d  captured_monster_speed = %-4d", o_ptr->curse_flags, o_ptr->captured_monster_speed), ++line, j);
366     prt(format("captured_monster_max_hp = %-4d  captured_monster_max_hp = %-4d", o_ptr->captured_monster_current_hp, o_ptr->captured_monster_max_hp), ++line, j);
367
368     prt("+------------FLAGS1------------+", ++line, j);
369     prt("AFFECT........SLAY........BRAND.", ++line, j);
370     prt("      mf      cvae      xsqpaefc", ++line, j);
371     prt("siwdccsossidsahanvudotgddhuoclio", ++line, j);
372     prt("tnieohtctrnipttmiinmrrnrrraiierl", ++line, j);
373     prt("rtsxnarelcfgdkcpmldncltggpksdced", ++line, j);
374     prt_binary(get_seq_32bits(flgs, 32 * 0), ++line, j);
375
376     prt("+------------FLAGS2------------+", ++line, j);
377     prt("SUST....IMMUN.RESIST............", ++line, j);
378     prt("      reaefctrpsaefcpfldbc sn   ", ++line, j);
379     prt("siwdcciaclioheatcliooeialoshtncd", ++line, j);
380     prt("tnieohdsierlrfraierliatrnnnrhehi", ++line, j);
381     prt("rtsxnaeydcedwlatdcedsrekdfddrxss", ++line, j);
382     prt_binary(get_seq_32bits(flgs, 32 * 1), ++line, j);
383
384     line = 13;
385     prt("+------------FLAGS3------------+", line, j + 32);
386     prt("fe cnn t      stdrmsiiii d ab   ", ++line, j + 32);
387     prt("aa aoomywhs lleeieihgggg rtgl   ", ++line, j + 32);
388     prt("uu utmacaih eielgggonnnnaaere   ", ++line, j + 32);
389     prt("rr reanurdo vtieeehtrrrrcilas   ", ++line, j + 32);
390     prt("aa algarnew ienpsntsaefctnevs   ", ++line, j + 32);
391     prt_binary(get_seq_32bits(flgs, 32 * 2), ++line, j + 32);
392
393     prt("+------------FLAGS4------------+", ++line, j + 32);
394     prt("KILL....ESP.........            ", ++line, j + 32);
395     prt("aeud tghaud tgdhegnu            ", ++line, j + 32);
396     prt("nvneoriunneoriruvoon            ", ++line, j + 32);
397     prt("iidmroamidmroagmionq            ", ++line, j + 32);
398     prt("mlenclnmmenclnnnldlu            ", ++line, j + 32);
399     prt_binary(get_seq_32bits(flgs, 32 * 3), ++line, j + 32);
400 }
401
402 /*!
403  * @brief 検査対象のアイテムを基準とした生成テストを行う /
404  * Try to create an item again. Output some statistics.    -Bernd-
405  * @param player_ptr プレイヤーへの参照ポインタ
406  * @param o_ptr 生成テストの基準となるアイテム情報の参照ポインタ
407  * The statistics are correct now.  We acquire a clean grid, and then
408  * repeatedly place an object in this grid, copying it into an item
409  * holder, and then deleting the object.  We fiddle with the artifact
410  * counter flags to prevent weirdness.  We use the items to collect
411  * statistics on item creation relative to the initial item.
412  */
413 static void wiz_statistics(PlayerType *player_ptr, ObjectType *o_ptr)
414 {
415     concptr q = "Rolls: %ld  Correct: %ld  Matches: %ld  Better: %ld  Worse: %ld  Other: %ld";
416     concptr p = "Enter number of items to roll: ";
417     char tmp_val[80];
418
419     if (o_ptr->is_fixed_artifact()) {
420         a_info.at(o_ptr->fixed_artifact_idx).is_generated = false;
421     }
422
423     uint32_t i, matches, better, worse, other, correct;
424     uint32_t test_roll = 1000000;
425     char ch;
426     concptr quality;
427     BIT_FLAGS mode;
428     while (true) {
429         concptr pmt = "Roll for [n]ormal, [g]ood, or [e]xcellent treasure? ";
430         wiz_display_item(player_ptr, o_ptr);
431         if (!get_com(pmt, &ch, false)) {
432             break;
433         }
434
435         if (ch == 'n' || ch == 'N') {
436             mode = 0L;
437             quality = "normal";
438         } else if (ch == 'g' || ch == 'G') {
439             mode = AM_GOOD;
440             quality = "good";
441         } else if (ch == 'e' || ch == 'E') {
442             mode = AM_GOOD | AM_GREAT;
443             quality = "excellent";
444         } else {
445             break;
446         }
447
448         sprintf(tmp_val, "%ld", (long int)test_roll);
449         if (get_string(p, tmp_val, 10)) {
450             test_roll = atol(tmp_val);
451         }
452         test_roll = std::max<uint>(1, test_roll);
453         msg_format("Creating a lot of %s items. Base level = %d.", quality, player_ptr->current_floor_ptr->dun_level);
454         msg_print(nullptr);
455
456         correct = matches = better = worse = other = 0;
457         for (i = 0; i <= test_roll; i++) {
458             if ((i < 100) || (i % 100 == 0)) {
459                 inkey_scan = true;
460                 if (inkey()) {
461                     flush();
462                     break; // stop rolling
463                 }
464
465                 prt(format(q, i, correct, matches, better, worse, other), 0, 0);
466                 term_fresh();
467             }
468
469             ObjectType forge;
470             auto *q_ptr = &forge;
471             q_ptr->wipe();
472             make_object(player_ptr, q_ptr, mode);
473             if (q_ptr->is_fixed_artifact()) {
474                 a_info.at(q_ptr->fixed_artifact_idx).is_generated = false;
475             }
476
477             if ((o_ptr->tval != q_ptr->tval) || (o_ptr->sval != q_ptr->sval)) {
478                 continue;
479             }
480
481             correct++;
482             const auto is_same_fixed_artifact_idx = q_ptr->fixed_artifact_idx == o_ptr->fixed_artifact_idx;
483             if ((q_ptr->pval == o_ptr->pval) && (q_ptr->to_a == o_ptr->to_a) && (q_ptr->to_h == o_ptr->to_h) && (q_ptr->to_d == o_ptr->to_d) && is_same_fixed_artifact_idx) {
484                 matches++;
485             } else if ((q_ptr->pval >= o_ptr->pval) && (q_ptr->to_a >= o_ptr->to_a) && (q_ptr->to_h >= o_ptr->to_h) && (q_ptr->to_d >= o_ptr->to_d)) {
486                 better++;
487             } else if ((q_ptr->pval <= o_ptr->pval) && (q_ptr->to_a <= o_ptr->to_a) && (q_ptr->to_h <= o_ptr->to_h) && (q_ptr->to_d <= o_ptr->to_d)) {
488                 worse++;
489             } else {
490                 other++;
491             }
492         }
493
494         msg_format(q, i, correct, matches, better, worse, other);
495         msg_print(nullptr);
496     }
497
498     if (o_ptr->is_fixed_artifact()) {
499         a_info.at(o_ptr->fixed_artifact_idx).is_generated = true;
500     }
501 }
502
503 /*!
504  * @brief アイテムの質を選択して再生成する /
505  * Apply magic to an item or turn it into an artifact. -Bernd-
506  * @param o_ptr 再生成の対象となるアイテム情報の参照ポインタ
507  */
508 static void wiz_reroll_item(PlayerType *player_ptr, ObjectType *o_ptr)
509 {
510     if (o_ptr->is_artifact()) {
511         return;
512     }
513
514     ObjectType forge;
515     ObjectType *q_ptr;
516     q_ptr = &forge;
517     q_ptr->copy_from(o_ptr);
518
519     char ch;
520     bool changed = false;
521     while (true) {
522         wiz_display_item(player_ptr, q_ptr);
523         if (!get_com("[a]ccept, [w]orthless, [c]ursed, [n]ormal, [g]ood, [e]xcellent, [s]pecial? ", &ch, false)) {
524             if (q_ptr->is_fixed_artifact()) {
525                 a_info.at(q_ptr->fixed_artifact_idx).is_generated = false;
526                 q_ptr->fixed_artifact_idx = FixedArtifactId::NONE;
527             }
528
529             changed = false;
530             break;
531         }
532
533         if (ch == 'A' || ch == 'a') {
534             changed = true;
535             break;
536         }
537
538         if (q_ptr->is_fixed_artifact()) {
539             a_info.at(q_ptr->fixed_artifact_idx).is_generated = false;
540             q_ptr->fixed_artifact_idx = FixedArtifactId::NONE;
541         }
542
543         switch (tolower(ch)) {
544         /* Apply bad magic, but first clear object */
545         case 'w':
546             q_ptr->prep(o_ptr->k_idx);
547             ItemMagicApplier(player_ptr, q_ptr, player_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD | AM_GREAT | AM_CURSED).execute();
548             break;
549         /* Apply bad magic, but first clear object */
550         case 'c':
551             q_ptr->prep(o_ptr->k_idx);
552             ItemMagicApplier(player_ptr, q_ptr, player_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD | AM_CURSED).execute();
553             break;
554         /* Apply normal magic, but first clear object */
555         case 'n':
556             q_ptr->prep(o_ptr->k_idx);
557             ItemMagicApplier(player_ptr, q_ptr, player_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART).execute();
558             break;
559         /* Apply good magic, but first clear object */
560         case 'g':
561             q_ptr->prep(o_ptr->k_idx);
562             ItemMagicApplier(player_ptr, q_ptr, player_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD).execute();
563             break;
564         /* Apply great magic, but first clear object */
565         case 'e':
566             q_ptr->prep(o_ptr->k_idx);
567             ItemMagicApplier(player_ptr, q_ptr, player_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD | AM_GREAT).execute();
568             break;
569         /* Apply special magic, but first clear object */
570         case 's':
571             q_ptr->prep(o_ptr->k_idx);
572             ItemMagicApplier(player_ptr, q_ptr, player_ptr->current_floor_ptr->dun_level, AM_GOOD | AM_GREAT | AM_SPECIAL).execute();
573             if (!q_ptr->is_artifact()) {
574                 become_random_artifact(player_ptr, q_ptr, false);
575             }
576
577             break;
578         default:
579             break;
580         }
581
582         q_ptr->iy = o_ptr->iy;
583         q_ptr->ix = o_ptr->ix;
584         q_ptr->marked = o_ptr->marked;
585     }
586
587     if (!changed) {
588         return;
589     }
590
591     o_ptr->copy_from(q_ptr);
592     set_bits(player_ptr->update, PU_BONUS | PU_COMBINE | PU_REORDER);
593     set_bits(player_ptr->window_flags, PW_INVEN | PW_EQUIP | PW_SPELL | PW_PLAYER | PW_FLOOR_ITEM_LIST);
594 }
595
596 /*!
597  * @briefアイテムの基礎能力値を調整する / Tweak an item
598  * @param player_ptr プレイヤーへの参照ポインタ
599  * @param o_ptr 調整するアイテムの参照ポインタ
600  */
601 static void wiz_tweak_item(PlayerType *player_ptr, ObjectType *o_ptr)
602 {
603     if (o_ptr->is_artifact()) {
604         return;
605     }
606
607     concptr p = "Enter new 'pval' setting: ";
608     char tmp_val[80];
609     sprintf(tmp_val, "%d", o_ptr->pval);
610     if (!get_string(p, tmp_val, 5)) {
611         return;
612     }
613
614     o_ptr->pval = clamp_cast<int16_t>(atoi(tmp_val));
615     wiz_display_item(player_ptr, o_ptr);
616     p = "Enter new 'to_a' setting: ";
617     sprintf(tmp_val, "%d", o_ptr->to_a);
618     if (!get_string(p, tmp_val, 5)) {
619         return;
620     }
621
622     o_ptr->to_a = clamp_cast<int16_t>(atoi(tmp_val));
623     wiz_display_item(player_ptr, o_ptr);
624     p = "Enter new 'to_h' setting: ";
625     sprintf(tmp_val, "%d", o_ptr->to_h);
626     if (!get_string(p, tmp_val, 5)) {
627         return;
628     }
629
630     o_ptr->to_h = clamp_cast<int16_t>(atoi(tmp_val));
631     wiz_display_item(player_ptr, o_ptr);
632     p = "Enter new 'to_d' setting: ";
633     sprintf(tmp_val, "%d", (int)o_ptr->to_d);
634     if (!get_string(p, tmp_val, 5)) {
635         return;
636     }
637
638     o_ptr->to_d = clamp_cast<int16_t>(atoi(tmp_val));
639     wiz_display_item(player_ptr, o_ptr);
640 }
641
642 /*!
643  * @brief 検査対象のアイテムの数を変更する /
644  * Change the quantity of a the item
645  * @param player_ptr プレイヤーへの参照ポインタ
646  * @param o_ptr 変更するアイテム情報構造体の参照ポインタ
647  */
648 static void wiz_quantity_item(ObjectType *o_ptr)
649 {
650     if (o_ptr->is_artifact()) {
651         return;
652     }
653
654     int tmp_qnt = o_ptr->number;
655     char tmp_val[100];
656     sprintf(tmp_val, "%d", (int)o_ptr->number);
657     if (get_string("Quantity: ", tmp_val, 2)) {
658         int tmp_int = atoi(tmp_val);
659         if (tmp_int < 1) {
660             tmp_int = 1;
661         }
662
663         if (tmp_int > 99) {
664             tmp_int = 99;
665         }
666
667         o_ptr->number = (byte)tmp_int;
668     }
669
670     if (o_ptr->tval == ItemKindType::ROD) {
671         o_ptr->pval = o_ptr->pval * o_ptr->number / tmp_qnt;
672     }
673 }
674
675 /*!
676  * @brief アイテムを弄るデバッグコマンド
677  * Play with an item. Options include:
678  * @details
679  *   - Output statistics (via wiz_roll_item)<br>
680  *   - Reroll item (via wiz_reroll_item)<br>
681  *   - Change properties (via wiz_tweak_item)<br>
682  *   - Change the number of items (via wiz_quantity_item)<br>
683  */
684 void wiz_modify_item(PlayerType *player_ptr)
685 {
686     concptr q = "Play with which object? ";
687     concptr s = "You have nothing to play with.";
688     OBJECT_IDX item;
689     ObjectType *o_ptr;
690     o_ptr = choose_object(player_ptr, &item, q, s, USE_EQUIP | USE_INVEN | USE_FLOOR | IGNORE_BOTHHAND_SLOT);
691     if (!o_ptr) {
692         return;
693     }
694
695     screen_save();
696
697     ObjectType forge;
698     ObjectType *q_ptr;
699     q_ptr = &forge;
700     q_ptr->copy_from(o_ptr);
701     char ch;
702     bool changed = false;
703     while (true) {
704         wiz_display_item(player_ptr, q_ptr);
705         if (!get_com("[a]ccept [s]tatistics [r]eroll [t]weak [q]uantity? ", &ch, false)) {
706             changed = false;
707             break;
708         }
709
710         if (ch == 'A' || ch == 'a') {
711             changed = true;
712             break;
713         }
714
715         if (ch == 's' || ch == 'S') {
716             wiz_statistics(player_ptr, q_ptr);
717         }
718
719         if (ch == 'r' || ch == 'R') {
720             wiz_reroll_item(player_ptr, q_ptr);
721         }
722
723         if (ch == 't' || ch == 'T') {
724             wiz_tweak_item(player_ptr, q_ptr);
725         }
726
727         if (ch == 'q' || ch == 'Q') {
728             wiz_quantity_item(q_ptr);
729         }
730     }
731
732     screen_load();
733     if (changed) {
734         msg_print("Changes accepted.");
735
736         o_ptr->copy_from(q_ptr);
737         set_bits(player_ptr->update, PU_BONUS | PU_COMBINE | PU_REORDER);
738         set_bits(player_ptr->window_flags, PW_INVEN | PW_EQUIP | PW_SPELL | PW_PLAYER | PW_FLOOR_ITEM_LIST);
739     } else {
740         msg_print("Changes ignored.");
741     }
742 }
743
744 /*!
745  * @brief オブジェクトの装備スロットがエゴが有効なスロットかどうか判定
746  */
747 static int is_slot_able_to_be_ego(PlayerType *player_ptr, ObjectType *o_ptr)
748 {
749     int slot = wield_slot(player_ptr, o_ptr);
750
751     if (slot > -1) {
752         return slot;
753     }
754
755     if ((o_ptr->tval == ItemKindType::SHOT) || (o_ptr->tval == ItemKindType::ARROW) || (o_ptr->tval == ItemKindType::BOLT)) {
756         return INVEN_AMMO;
757     }
758
759     return -1;
760 }
761
762 /*!
763  * @brief 願ったが消えてしまった場合のメッセージ
764  */
765 static void wishing_puff_of_smoke(void)
766 {
767     msg_print(_("何かが足下に転がってきたが、煙のように消えてしまった。",
768         "You feel something roll beneath your feet, but it disappears in a puff of smoke!"));
769 }
770
771 /*!
772  * @brief 願ったが消えてしまった場合のメッセージ
773  * @param player_ptr 願ったプレイヤー情報への参照ポインタ
774  * @param prob ★などを願った場合の生成確率
775  * @param art_ok アーティファクトの生成を許すならTRUE
776  * @param ego_ok エゴの生成を許すならTRUE
777  * @param confirm 願わない場合に確認するかどうか
778  * @return 願った結果
779  */
780 WishResultType do_cmd_wishing(PlayerType *player_ptr, int prob, bool allow_art, bool allow_ego, bool confirm)
781 {
782     concptr fixed_str[] = {
783 #ifdef JP
784         "燃えない",
785         "錆びない",
786         "腐食しない",
787         "安定した",
788 #else
789         "rotproof",
790         "fireproof",
791         "rustproof",
792         "erodeproof",
793         "corrodeproof",
794         "fixed",
795 #endif
796         nullptr,
797     };
798
799     char buf[MAX_NLEN] = "\0";
800     char *str = buf;
801     ObjectType forge;
802     auto *o_ptr = &forge;
803     char o_name[MAX_NLEN];
804
805     bool wish_art = false;
806     bool wish_randart = false;
807     bool wish_ego = false;
808     bool exam_base = true;
809     bool ok_art = randint0(100) < prob;
810     bool ok_ego = randint0(100) < 50 + prob;
811     bool must = prob < 0;
812     bool blessed = false;
813     bool fixed = true;
814
815     while (1) {
816         if (get_string(_("何をお望み? ", "For what do you wish?"), buf, (MAX_NLEN - 1))) {
817             break;
818         }
819         if (confirm) {
820             if (!get_check(_("何も願いません。本当によろしいですか?", "Do you wish nothing, really? "))) {
821                 continue;
822             }
823         }
824         return WishResultType::NOTHING;
825     }
826
827 #ifndef JP
828     str_tolower(str);
829
830     /* remove 'a' */
831     if (!strncmp(buf, "a ", 2)) {
832         str = ltrim(str + 1);
833     } else if (!strncmp(buf, "an ", 3)) {
834         str = ltrim(str + 2);
835     }
836 #endif // !JP
837
838     str = rtrim(str);
839
840     if (!strncmp(str, _("祝福された", "blessed"), _(10, 7))) {
841         str = ltrim(str + _(10, 7));
842         blessed = true;
843     }
844
845     for (int i = 0; fixed_str[i] != nullptr; i++) {
846         int len = strlen(fixed_str[i]);
847         if (!strncmp(str, fixed_str[i], len)) {
848             str = ltrim(str + len);
849             fixed = true;
850             break;
851         }
852     }
853
854 #ifdef JP
855     if (!strncmp(str, "★", 2)) {
856         str = ltrim(str + 2);
857         wish_art = true;
858         exam_base = false;
859     } else
860 #endif
861
862         if (!strncmp(str, _("☆", "The "), _(2, 4))) {
863         str = ltrim(str + _(2, 4));
864         wish_art = true;
865         wish_randart = true;
866     }
867
868     /* wishing random ego ? */
869     else if (!strncmp(str, _("高級な", "excellent "), _(6, 9))) {
870         str = ltrim(str + _(6, 9));
871         wish_ego = true;
872     }
873
874     if (strlen(str) < 1) {
875         msg_print(_("名前がない!", "What?"));
876         return WishResultType::NOTHING;
877     }
878
879     if (!allow_art && wish_art) {
880         msg_print(_("アーティファクトは願えない!", "You can not wish artifacts!"));
881         return WishResultType::NOTHING;
882     }
883
884     if (cheat_xtra) {
885         msg_format("Wishing %s....", buf);
886     }
887
888     std::vector<KIND_OBJECT_IDX> k_ids;
889     std::vector<EgoType> e_ids;
890     if (exam_base) {
891         int len;
892         int max_len = 0;
893         for (const auto &k_ref : k_info) {
894             if (k_ref.idx == 0 || k_ref.name.empty()) {
895                 continue;
896             }
897
898             o_ptr->prep(k_ref.idx);
899             describe_flavor(player_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY | OD_STORE));
900 #ifndef JP
901             str_tolower(o_name);
902 #endif
903             if (cheat_xtra) {
904                 msg_format("Matching object No.%d %s", k_ref.idx, o_name);
905             }
906
907             len = strlen(o_name);
908
909             if (_(!strrncmp(str, o_name, len), !strncmp(str, o_name, len))) {
910                 if (len > max_len) {
911                     k_ids.push_back(k_ref.idx);
912                     max_len = len;
913                 }
914             }
915         }
916
917         if (allow_ego && k_ids.size() == 1) {
918             KIND_OBJECT_IDX k_idx = k_ids.back();
919             o_ptr->prep(k_idx);
920
921             for (const auto &[e_idx, e_ref] : e_info) {
922                 if (e_ref.idx == EgoType::NONE || e_ref.name.empty()) {
923                     continue;
924                 }
925
926                 strcpy(o_name, e_ref.name.c_str());
927 #ifndef JP
928                 str_tolower(o_name);
929 #endif
930                 if (cheat_xtra) {
931                     msg_format("matching ego no.%d %s...", e_ref.idx, o_name);
932                 }
933
934                 if (_(!strncmp(str, o_name, strlen(o_name)), !strrncmp(str, o_name, strlen(o_name)))) {
935                     if (is_slot_able_to_be_ego(player_ptr, o_ptr) != e_ref.slot) {
936                         continue;
937                     }
938
939                     e_ids.push_back(e_ref.idx);
940                 }
941             }
942         }
943     }
944
945     std::vector<FixedArtifactId> a_ids;
946
947     if (allow_art) {
948         char a_desc[MAX_NLEN] = "\0";
949         char *a_str = a_desc;
950
951         int len;
952         int mlen = 0;
953         for (const auto &[a_idx, a_ref] : a_info) {
954             if (a_idx == FixedArtifactId::NONE || a_ref.name.empty()) {
955                 continue;
956             }
957
958             KIND_OBJECT_IDX k_idx = lookup_kind(a_ref.tval, a_ref.sval);
959             if (!k_idx) {
960                 continue;
961             }
962
963             o_ptr->prep(k_idx);
964             o_ptr->fixed_artifact_idx = a_idx;
965
966             describe_flavor(player_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY | OD_STORE));
967 #ifndef JP
968             str_tolower(o_name);
969 #endif
970             a_str = a_desc;
971             strcpy(a_desc, a_ref.name.c_str());
972
973             if (*a_str == '$') {
974                 a_str++;
975             }
976 #ifdef JP
977             /* remove quotes */
978             if (!strncmp(a_str, "『", 2)) {
979                 a_str += 2;
980                 char *s = strstr(a_str, "』");
981                 *s = '\0';
982             }
983             /* remove 'of' */
984             else {
985                 int l = strlen(a_str);
986                 if (!strrncmp(a_str, "の", 2)) {
987                     a_str[l - 2] = '\0';
988                 }
989             }
990 #else
991             /* remove quotes */
992             if (a_str[0] == '\'') {
993                 a_str += 1;
994                 char *s = strchr(a_desc, '\'');
995                 *s = '\0';
996             }
997             /* remove 'of ' */
998             else if (!strncmp(a_str, (const char *)"of ", 3)) {
999                 a_str += 3;
1000             }
1001
1002             str_tolower(a_str);
1003 #endif
1004
1005             if (cheat_xtra) {
1006                 msg_format("Matching artifact No.%d %s(%s)", a_idx, a_desc, _(&o_name[2], o_name));
1007             }
1008
1009             std::vector<const char *> l = { a_str, a_ref.name.c_str(), _(&o_name[2], o_name) };
1010             for (size_t c = 0; c < l.size(); c++) {
1011                 if (!strcmp(str, l.at(c))) {
1012                     len = strlen(l.at(c));
1013                     if (len > mlen) {
1014                         a_ids.push_back(a_idx);
1015                         mlen = len;
1016                     }
1017                 }
1018             }
1019         }
1020     }
1021
1022     if (w_ptr->wizard && (a_ids.size() > 1 || e_ids.size() > 1)) {
1023         msg_print(_("候補が多すぎる!", "Too many matches!"));
1024         return WishResultType::FAIL;
1025     }
1026
1027     if (a_ids.size() == 1) {
1028         const auto a_idx = a_ids.back();
1029         auto &a_ref = a_info.at(a_idx);
1030         if (must || (ok_art && !a_ref.is_generated)) {
1031             (void)create_named_art(player_ptr, a_idx, player_ptr->y, player_ptr->x);
1032         } else {
1033             wishing_puff_of_smoke();
1034         }
1035
1036         return WishResultType::ARTIFACT;
1037     }
1038
1039     if (!allow_ego && (wish_ego || e_ids.size() > 0)) {
1040         msg_print(_("エゴアイテムは願えない!", "Can not wish ego item."));
1041         return WishResultType::NOTHING;
1042     }
1043
1044     if (k_ids.size() == 1) {
1045         KIND_OBJECT_IDX k_idx = k_ids.back();
1046         auto *k_ptr = &k_info[k_idx];
1047
1048         FixedArtifactId a_idx = FixedArtifactId::NONE;
1049         if (k_ptr->gen_flags.has(ItemGenerationTraitType::INSTA_ART)) {
1050             for (const auto &[a_idx_loop, a_ref_loop] : a_info) {
1051                 if (a_idx_loop == FixedArtifactId::NONE || a_ref_loop.tval != k_ptr->tval || a_ref_loop.sval != k_ptr->sval) {
1052                     continue;
1053                 }
1054                 a_idx = a_idx_loop;
1055                 break;
1056             }
1057         }
1058
1059         if (a_idx != FixedArtifactId::NONE) {
1060             auto &a_ref = a_info.at(a_idx);
1061             if (must || (ok_art && !a_ref.is_generated)) {
1062                 (void)create_named_art(player_ptr, a_idx, player_ptr->y, player_ptr->x);
1063             } else {
1064                 wishing_puff_of_smoke();
1065             }
1066
1067             return WishResultType::ARTIFACT;
1068         }
1069
1070         if (wish_randart) {
1071             if (must || ok_art) {
1072                 bool is_fixed_artifact;
1073                 do {
1074                     o_ptr->prep(k_idx);
1075                     ItemMagicApplier(player_ptr, o_ptr, k_ptr->level, AM_SPECIAL | AM_NO_FIXED_ART).execute();
1076                     is_fixed_artifact = o_ptr->fixed_artifact_idx != FixedArtifactId::NONE;
1077                 } while (!o_ptr->art_name || is_fixed_artifact || o_ptr->is_ego() || o_ptr->is_cursed());
1078
1079                 if (o_ptr->art_name) {
1080                     drop_near(player_ptr, o_ptr, -1, player_ptr->y, player_ptr->x);
1081                 }
1082             } else {
1083                 wishing_puff_of_smoke();
1084             }
1085             return WishResultType::ARTIFACT;
1086         }
1087
1088         WishResultType res = WishResultType::NOTHING;
1089         if (allow_ego && (wish_ego || e_ids.size() > 0)) {
1090             if (must || ok_ego) {
1091                 if (e_ids.size() > 0) {
1092                     o_ptr->prep(k_idx);
1093                     o_ptr->ego_idx = e_ids[0];
1094                     apply_ego(o_ptr, player_ptr->current_floor_ptr->base_level);
1095                 } else {
1096                     int max_roll = 1000;
1097                     int i = 0;
1098                     for (i = 0; i < max_roll; i++) {
1099                         o_ptr->prep(k_idx);
1100                         ItemMagicApplier(player_ptr, o_ptr, k_ptr->level, AM_GREAT | AM_NO_FIXED_ART).execute();
1101                         const auto is_fixed_artifact = o_ptr->fixed_artifact_idx != FixedArtifactId::NONE;
1102                         if (is_fixed_artifact || o_ptr->art_name) {
1103                             continue;
1104                         }
1105
1106                         if (wish_ego) {
1107                             break;
1108                         }
1109
1110                         EgoType e_idx = EgoType::NONE;
1111                         for (auto e : e_ids) {
1112                             if (o_ptr->ego_idx == e) {
1113                                 e_idx = e;
1114                                 break;
1115                             }
1116                         }
1117
1118                         if (e_idx != EgoType::NONE) {
1119                             break;
1120                         }
1121                     }
1122
1123                     if (i == max_roll) {
1124                         msg_print(_("失敗!もう一度願ってみてください。", "Failed! Try again."));
1125                         return WishResultType::FAIL;
1126                     }
1127                 }
1128             } else {
1129                 wishing_puff_of_smoke();
1130             }
1131
1132             res = WishResultType::EGO;
1133         } else {
1134             for (int i = 0; i < 100; i++) {
1135                 o_ptr->prep(k_idx);
1136                 ItemMagicApplier(player_ptr, o_ptr, 0, AM_NO_FIXED_ART).execute();
1137                 if (!o_ptr->is_cursed()) {
1138                     break;
1139                 }
1140             }
1141             res = WishResultType::NORMAL;
1142         }
1143
1144         if (blessed && wield_slot(player_ptr, o_ptr) != -1) {
1145             o_ptr->art_flags.set(TR_BLESSED);
1146         }
1147
1148         if (fixed && wield_slot(player_ptr, o_ptr) != -1) {
1149             o_ptr->art_flags.set(TR_IGNORE_ACID);
1150             o_ptr->art_flags.set(TR_IGNORE_FIRE);
1151         }
1152
1153         (void)drop_near(player_ptr, o_ptr, -1, player_ptr->y, player_ptr->x);
1154
1155         return res;
1156     }
1157
1158     msg_print(_("うーん、そんなものは存在しないようだ。", "Ummmm, that is not existing..."));
1159     return WishResultType::FAIL;
1160 }