OSDN Git Service

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