OSDN Git Service

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