OSDN Git Service

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