OSDN Git Service

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