OSDN Git Service

7350ed38e46ff53640dc3c633e751dd43d8a89b2
[hengband/hengband.git] / src / wizard / wizard-item-modifier.c
1 #include "wizard/wizard-item-modifier.h"
2 #include "artifact/random-art-generator.h"
3 #include "core/asking-player.h"
4 #include "core/player-update-types.h"
5 #include "core/window-redrawer.h"
6 #include "flavor/flavor-describer.h"
7 #include "flavor/object-flavor-types.h"
8 #include "floor/floor-object.h"
9 #include "io/input-key-acceptor.h"
10 #include "object-enchant/apply-magic.h"
11 #include "object-enchant/item-apply-magic.h"
12 #include "object-hook/hook-enchant.h"
13 #include "object/item-use-flags.h"
14 #include "object/object-flags.h"
15 #include "object/object-generator.h"
16 #include "object/object-kind.h"
17 #include "object/object-value.h"
18 #include "system/alloc-entries.h"
19 #include "system/artifact-type-definition.h"
20 #include "system/floor-type-definition.h"
21 #include "system/object-type-definition.h"
22 #include "term/screen-processor.h"
23 #include "term/term-color-types.h"
24 #include "view/display-messages.h"
25
26 #define K_MAX_DEPTH 110 /*!< アイテムの階層毎生成率を表示する最大階 */
27
28 /*!
29  * @brief アイテムの階層毎生成率を表示する / Output a rarity graph for a type of object.
30  * @param tval ベースアイテムの大項目ID
31  * @param sval ベースアイテムの小項目ID
32  * @param row 表示列
33  * @param col 表示行
34  * @return なし
35  */
36 static void prt_alloc(tval_type tval, OBJECT_SUBTYPE_VALUE sval, TERM_LEN row, TERM_LEN col)
37 {
38     u32b rarity[K_MAX_DEPTH];
39     (void)C_WIPE(rarity, K_MAX_DEPTH, u32b);
40     u32b total[K_MAX_DEPTH];
41     (void)C_WIPE(total, K_MAX_DEPTH, u32b);
42     s32b display[22];
43     (void)C_WIPE(display, 22, s32b);
44
45     int home = 0;
46     for (int i = 0; i < K_MAX_DEPTH; i++) {
47         int total_frac = 0;
48         object_kind *k_ptr;
49         alloc_entry *table = alloc_kind_table;
50         for (int j = 0; j < alloc_kind_size; j++) {
51             PERCENTAGE prob = 0;
52
53             if (table[j].level <= i) {
54                 prob = table[j].prob1 * GREAT_OBJ * K_MAX_DEPTH;
55             } else if (table[j].level - 1 > 0) {
56                 prob = table[j].prob1 * i * K_MAX_DEPTH / (table[j].level - 1);
57             }
58
59             k_ptr = &k_info[table[j].index];
60
61             total[i] += prob / (GREAT_OBJ * K_MAX_DEPTH);
62             total_frac += prob % (GREAT_OBJ * K_MAX_DEPTH);
63
64             if ((k_ptr->tval == tval) && (k_ptr->sval == sval)) {
65                 home = k_ptr->level;
66                 rarity[i] += prob / (GREAT_OBJ * K_MAX_DEPTH);
67             }
68         }
69
70         total[i] += total_frac / (GREAT_OBJ * K_MAX_DEPTH);
71     }
72
73     for (int i = 0; i < 22; i++) {
74         int possibility = 0;
75         for (int j = i * K_MAX_DEPTH / 22; j < (i + 1) * K_MAX_DEPTH / 22; j++)
76             possibility += rarity[j] * 100000 / total[j];
77
78         display[i] = possibility / 5;
79     }
80
81     for (int i = 0; i < 22; i++) {
82         term_putch(col, row + i + 1, TERM_WHITE, '|');
83         prt(format("%2dF", (i * 5)), row + i + 1, col);
84         if ((i * K_MAX_DEPTH / 22 <= home) && (home < (i + 1) * K_MAX_DEPTH / 22))
85             c_prt(TERM_RED, format("%3d.%04d%%", display[i] / 1000, display[i] % 1000), row + i + 1, col + 3);
86         else
87             c_prt(TERM_WHITE, format("%3d.%04d%%", display[i] / 1000, display[i] % 1000), row + i + 1, col + 3);
88     }
89
90     concptr r = "+---Rate---+";
91     prt(r, row, col);
92 }
93
94 /*!
95  * @brief 32ビット変数のビット配列を並べて描画する / Output a long int in binary format.
96  * @return なし
97  */
98 static void prt_binary(BIT_FLAGS flags, const int row, int col)
99 {
100     u32b bitmask;
101     for (int i = bitmask = 1; i <= 32; i++, bitmask *= 2)
102         if (flags & bitmask)
103             term_putch(col++, row, TERM_BLUE, '*');
104         else
105             term_putch(col++, row, TERM_WHITE, '-');
106 }
107
108 /*!
109  * @brief アイテムの詳細ステータスを表示する /
110  * Change various "permanent" player variables.
111  * @param player_ptr プレーヤーへの参照ポインタ
112  * @param o_ptr 詳細を表示するアイテム情報の参照ポインタ
113  * @return なし
114  */
115 static void wiz_display_item(player_type *player_ptr, object_type *o_ptr)
116 {
117     BIT_FLAGS flgs[TR_FLAG_SIZE];
118     object_flags(player_ptr, o_ptr, flgs);
119     int j = 13;
120     for (int i = 1; i <= 23; i++)
121         prt("", i, j - 2);
122
123     prt_alloc(o_ptr->tval, o_ptr->sval, 1, 0);
124     char buf[256];
125     describe_flavor(player_ptr, buf, o_ptr, OD_STORE);
126     prt(buf, 2, j);
127     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);
128     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);
129     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);
130     prt(format("name1 = %-4d  name2 = %-4d  cost = %ld", o_ptr->name1, o_ptr->name2, (long)object_value_real(player_ptr, o_ptr)), 7, j);
131     prt(format("ident = %04x  xtra1 = %-4d  xtra2 = %-4d  timeout = %-d", o_ptr->ident, o_ptr->xtra1, o_ptr->xtra2, o_ptr->timeout), 8, j);
132     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);
133
134     prt("+------------FLAGS1------------+", 10, j);
135     prt("AFFECT........SLAY........BRAND.", 11, j);
136     prt("      mf      cvae      xsqpaefc", 12, j);
137     prt("siwdccsossidsahanvudotgddhuoclio", 13, j);
138     prt("tnieohtctrnipttmiinmrrnrrraiierl", 14, j);
139     prt("rtsxnarelcfgdkcpmldncltggpksdced", 15, j);
140     prt_binary(flgs[0], 16, j);
141
142     prt("+------------FLAGS2------------+", 17, j);
143     prt("SUST....IMMUN.RESIST............", 18, j);
144     prt("      reaefctrpsaefcpfldbc sn   ", 19, j);
145     prt("siwdcciaclioheatcliooeialoshtncd", 20, j);
146     prt("tnieohdsierlrfraierliatrnnnrhehi", 21, j);
147     prt("rtsxnaeydcedwlatdcedsrekdfddrxss", 22, j);
148     prt_binary(flgs[1], 23, j);
149
150     prt("+------------FLAGS3------------+", 10, j + 32);
151     prt("fe cnn t      stdrmsiiii d ab   ", 11, j + 32);
152     prt("aa aoomywhs lleeieihgggg rtgl   ", 12, j + 32);
153     prt("uu utmacaih eielgggonnnnaaere   ", 13, j + 32);
154     prt("rr reanurdo vtieeehtrrrrcilas   ", 14, j + 32);
155     prt("aa algarnew ienpsntsaefctnevs   ", 15, j + 32);
156     prt_binary(flgs[2], 16, j + 32);
157
158     prt("+------------FLAGS4------------+", 17, j + 32);
159     prt("KILL....ESP.........            ", 18, j + 32);
160     prt("aeud tghaud tgdhegnu            ", 19, j + 32);
161     prt("nvneoriunneoriruvoon            ", 20, j + 32);
162     prt("iidmroamidmroagmionq            ", 21, j + 32);
163     prt("mlenclnmmenclnnnldlu            ", 22, j + 32);
164     prt_binary(flgs[3], 23, j + 32);
165 }
166
167 /*!
168  * @brief 検査対象のアイテムを基準とした生成テストを行う /
169  * Try to create an item again. Output some statistics.    -Bernd-
170  * @param caster_ptr プレーヤーへの参照ポインタ
171  * @param o_ptr 生成テストの基準となるアイテム情報の参照ポインタ
172  * @return なし
173  * The statistics are correct now.  We acquire a clean grid, and then
174  * repeatedly place an object in this grid, copying it into an item
175  * holder, and then deleting the object.  We fiddle with the artifact
176  * counter flags to prevent weirdness.  We use the items to collect
177  * statistics on item creation relative to the initial item.
178  */
179 static void wiz_statistics(player_type *caster_ptr, object_type *o_ptr)
180 {
181     concptr q = "Rolls: %ld  Correct: %ld  Matches: %ld  Better: %ld  Worse: %ld  Other: %ld";
182     concptr p = "Enter number of items to roll: ";
183     char tmp_val[80];
184
185     if (object_is_fixed_artifact(o_ptr))
186         a_info[o_ptr->name1].cur_num = 0;
187
188     u32b i, matches, better, worse, other, correct;
189     u32b test_roll = 1000000;
190     char ch;
191     concptr quality;
192     BIT_FLAGS mode;
193     while (TRUE) {
194         concptr pmt = "Roll for [n]ormal, [g]ood, or [e]xcellent treasure? ";
195         wiz_display_item(caster_ptr, o_ptr);
196         if (!get_com(pmt, &ch, FALSE))
197             break;
198
199         if (ch == 'n' || ch == 'N') {
200             mode = 0L;
201             quality = "normal";
202         } else if (ch == 'g' || ch == 'G') {
203             mode = AM_GOOD;
204             quality = "good";
205         } else if (ch == 'e' || ch == 'E') {
206             mode = AM_GOOD | AM_GREAT;
207             quality = "excellent";
208         } else {
209             break;
210         }
211
212         sprintf(tmp_val, "%ld", (long int)test_roll);
213         if (get_string(p, tmp_val, 10))
214             test_roll = atol(tmp_val);
215         test_roll = MAX(1, test_roll);
216         msg_format("Creating a lot of %s items. Base level = %d.", quality, caster_ptr->current_floor_ptr->dun_level);
217         msg_print(NULL);
218
219         correct = matches = better = worse = other = 0;
220         for (i = 0; i <= test_roll; i++) {
221             if ((i < 100) || (i % 100 == 0)) {
222                 inkey_scan = TRUE;
223                 if (inkey()) {
224                     flush();
225                     break; // stop rolling
226                 }
227
228                 prt(format(q, i, correct, matches, better, worse, other), 0, 0);
229                 if (need_term_fresh(caster_ptr))
230                     term_fresh();
231             }
232
233             object_type forge;
234             object_type *q_ptr = &forge;
235             object_wipe(q_ptr);
236             make_object(caster_ptr, q_ptr, mode);
237             if (object_is_fixed_artifact(q_ptr))
238                 a_info[q_ptr->name1].cur_num = 0;
239
240             if ((o_ptr->tval != q_ptr->tval) || (o_ptr->sval != q_ptr->sval))
241                 continue;
242
243             correct++;
244             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)
245                 && (q_ptr->name1 == o_ptr->name1)) {
246                 matches++;
247             } 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)) {
248                 better++;
249             } 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)) {
250                 worse++;
251             } else {
252                 other++;
253             }
254         }
255
256         msg_format(q, i, correct, matches, better, worse, other);
257         msg_print(NULL);
258     }
259
260     if (object_is_fixed_artifact(o_ptr))
261         a_info[o_ptr->name1].cur_num = 1;
262 }
263
264 /*!
265  * @brief アイテムの質を選択して再生成する /
266  * Apply magic to an item or turn it into an artifact. -Bernd-
267  * @param o_ptr 再生成の対象となるアイテム情報の参照ポインタ
268  * @return なし
269  */
270 static void wiz_reroll_item(player_type *owner_ptr, object_type *o_ptr)
271 {
272     if (object_is_artifact(o_ptr))
273         return;
274
275     object_type forge;
276     object_type *q_ptr;
277     q_ptr = &forge;
278     object_copy(q_ptr, o_ptr);
279
280     char ch;
281     bool changed = FALSE;
282     while (TRUE) {
283         wiz_display_item(owner_ptr, q_ptr);
284         if (!get_com("[a]ccept, [w]orthless, [c]ursed, [n]ormal, [g]ood, [e]xcellent, [s]pecial? ", &ch, FALSE)) {
285             if (object_is_fixed_artifact(q_ptr)) {
286                 a_info[q_ptr->name1].cur_num = 0;
287                 q_ptr->name1 = 0;
288             }
289
290             changed = FALSE;
291             break;
292         }
293
294         if (ch == 'A' || ch == 'a') {
295             changed = TRUE;
296             break;
297         }
298
299         if (object_is_fixed_artifact(q_ptr)) {
300             a_info[q_ptr->name1].cur_num = 0;
301             q_ptr->name1 = 0;
302         }
303
304         switch (tolower(ch)) {
305         /* Apply bad magic, but first clear object */
306         case 'w':
307             object_prep(owner_ptr, q_ptr, o_ptr->k_idx);
308             apply_magic(owner_ptr, q_ptr, owner_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD | AM_GREAT | AM_CURSED);
309             break;
310         /* Apply bad magic, but first clear object */
311         case 'c':
312             object_prep(owner_ptr, q_ptr, o_ptr->k_idx);
313             apply_magic(owner_ptr, q_ptr, owner_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD | AM_CURSED);
314             break;
315         /* Apply normal magic, but first clear object */
316         case 'n':
317             object_prep(owner_ptr, q_ptr, o_ptr->k_idx);
318             apply_magic(owner_ptr, q_ptr, owner_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART);
319             break;
320         /* Apply good magic, but first clear object */
321         case 'g':
322             object_prep(owner_ptr, q_ptr, o_ptr->k_idx);
323             apply_magic(owner_ptr, q_ptr, owner_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD);
324             break;
325         /* Apply great magic, but first clear object */
326         case 'e':
327             object_prep(owner_ptr, q_ptr, o_ptr->k_idx);
328             apply_magic(owner_ptr, q_ptr, owner_ptr->current_floor_ptr->dun_level, AM_NO_FIXED_ART | AM_GOOD | AM_GREAT);
329             break;
330         /* Apply special magic, but first clear object */
331         case 's':
332             object_prep(owner_ptr, q_ptr, o_ptr->k_idx);
333             apply_magic(owner_ptr, q_ptr, owner_ptr->current_floor_ptr->dun_level, AM_GOOD | AM_GREAT | AM_SPECIAL);
334             if (!object_is_artifact(q_ptr))
335                 become_random_artifact(owner_ptr, q_ptr, FALSE);
336
337             break;
338         default:
339             break;
340         }
341
342         q_ptr->iy = o_ptr->iy;
343         q_ptr->ix = o_ptr->ix;
344         q_ptr->next_o_idx = o_ptr->next_o_idx;
345         q_ptr->marked = o_ptr->marked;
346     }
347
348     if (!changed)
349         return;
350
351     object_copy(o_ptr, q_ptr);
352     owner_ptr->update |= PU_BONUS;
353     owner_ptr->update |= PU_COMBINE | PU_REORDER;
354     owner_ptr->window |= PW_INVEN | PW_EQUIP | PW_SPELL | PW_PLAYER;
355 }
356
357 /*!
358  * @briefアイテムの基礎能力値を調整する / Tweak an item
359  * @param player_ptr プレーヤーへの参照ポインタ
360  * @param o_ptr 調整するアイテムの参照ポインタ
361  * @return なし
362  */
363 static void wiz_tweak_item(player_type *player_ptr, object_type *o_ptr)
364 {
365     if (object_is_artifact(o_ptr))
366         return;
367
368     concptr p = "Enter new 'pval' setting: ";
369     char tmp_val[80];
370     sprintf(tmp_val, "%d", o_ptr->pval);
371     if (!get_string(p, tmp_val, 5))
372         return;
373
374     o_ptr->pval = (s16b)atoi(tmp_val);
375     wiz_display_item(player_ptr, o_ptr);
376     p = "Enter new 'to_a' setting: ";
377     sprintf(tmp_val, "%d", o_ptr->to_a);
378     if (!get_string(p, tmp_val, 5))
379         return;
380
381     o_ptr->to_a = (s16b)atoi(tmp_val);
382     wiz_display_item(player_ptr, o_ptr);
383     p = "Enter new 'to_h' setting: ";
384     sprintf(tmp_val, "%d", o_ptr->to_h);
385     if (!get_string(p, tmp_val, 5))
386         return;
387
388     o_ptr->to_h = (s16b)atoi(tmp_val);
389     wiz_display_item(player_ptr, o_ptr);
390     p = "Enter new 'to_d' setting: ";
391     sprintf(tmp_val, "%d", (int)o_ptr->to_d);
392     if (!get_string(p, tmp_val, 5))
393         return;
394
395     o_ptr->to_d = (s16b)atoi(tmp_val);
396     wiz_display_item(player_ptr, o_ptr);
397 }
398
399 /*!
400  * @brief 検査対象のアイテムの数を変更する /
401  * Change the quantity of a the item
402  * @param caster_ptr プレーヤーへの参照ポインタ
403  * @param o_ptr 変更するアイテム情報構造体の参照ポインタ
404  * @return なし
405  */
406 static void wiz_quantity_item(object_type *o_ptr)
407 {
408     if (object_is_artifact(o_ptr))
409         return;
410
411     int tmp_qnt = o_ptr->number;
412     char tmp_val[100];
413     sprintf(tmp_val, "%d", (int)o_ptr->number);
414     if (get_string("Quantity: ", tmp_val, 2)) {
415         int tmp_int = atoi(tmp_val);
416         if (tmp_int < 1)
417             tmp_int = 1;
418
419         if (tmp_int > 99)
420             tmp_int = 99;
421
422         o_ptr->number = (byte)tmp_int;
423     }
424
425     if (o_ptr->tval == TV_ROD)
426         o_ptr->pval = o_ptr->pval * o_ptr->number / tmp_qnt;
427 }
428
429 /*!
430  * @brief アイテムを弄るデバッグコマンド
431  * Play with an item. Options include:
432  * @return なし
433  * @details
434  *   - Output statistics (via wiz_roll_item)<br>
435  *   - Reroll item (via wiz_reroll_item)<br>
436  *   - Change properties (via wiz_tweak_item)<br>
437  *   - Change the number of items (via wiz_quantity_item)<br>
438  */
439 void wiz_modify_item(player_type *creature_ptr)
440 {
441     concptr q = "Play with which object? ";
442     concptr s = "You have nothing to play with.";
443     OBJECT_IDX item;
444     object_type *o_ptr;
445     o_ptr = choose_object(creature_ptr, &item, q, s, USE_EQUIP | USE_INVEN | USE_FLOOR | IGNORE_BOTHHAND_SLOT, 0);
446     if (!o_ptr)
447         return;
448
449     screen_save(creature_ptr);
450
451     object_type forge;
452     object_type *q_ptr;
453     q_ptr = &forge;
454     object_copy(q_ptr, o_ptr);
455     char ch;
456     bool changed = FALSE;
457     while (TRUE) {
458         wiz_display_item(creature_ptr, q_ptr);
459         if (!get_com("[a]ccept [s]tatistics [r]eroll [t]weak [q]uantity? ", &ch, FALSE)) {
460             changed = FALSE;
461             break;
462         }
463
464         if (ch == 'A' || ch == 'a') {
465             changed = TRUE;
466             break;
467         }
468
469         if (ch == 's' || ch == 'S') {
470             wiz_statistics(creature_ptr, q_ptr);
471         }
472
473         if (ch == 'r' || ch == 'R') {
474             wiz_reroll_item(creature_ptr, q_ptr);
475         }
476
477         if (ch == 't' || ch == 'T') {
478             wiz_tweak_item(creature_ptr, q_ptr);
479         }
480
481         if (ch == 'q' || ch == 'Q') {
482             wiz_quantity_item(q_ptr);
483         }
484     }
485
486     screen_load(creature_ptr);
487     if (changed) {
488         msg_print("Changes accepted.");
489
490         object_copy(o_ptr, q_ptr);
491         creature_ptr->update |= PU_BONUS;
492         creature_ptr->update |= PU_COMBINE | PU_REORDER;
493         creature_ptr->window |= PW_INVEN | PW_EQUIP | PW_SPELL | PW_PLAYER;
494     } else {
495         msg_print("Changes ignored.");
496     }
497 }