OSDN Git Service

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