OSDN Git Service

[Refactor] #40533 Removed the dependency for cmd-building.h in some files
[hengband/hengband.git] / src / cmd-item / cmd-magiceat.c
1 /*!
2  * @brief プレイヤーのアイテムに関するコマンドの実装2 / Spell/Prayer commands
3  * @date 2014/01/27
4  * @author
5  * <pre>
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7  * This software may be copied and distributed for educational, research,
8  * and not for profit purposes provided that this copyright and statement
9  * are included in all such copies.  Other copyrights may also apply.
10  * 2014 Deskull rearranged comment for Doxygen.\n
11  * </pre>
12  * @details
13  * <pre>
14  * This file includes code for eating food, drinking potions,
15  * reading scrolls, aiming wands, using staffs, zapping rods,
16  * and activating artifacts.
17  *
18  * In all cases, if the player becomes "aware" of the item's use
19  * by testing it, mark it as "aware" and reward some experience
20  * based on the object's level, always rounding up.  If the player
21  * remains "unaware", mark that object "kind" as "tried".
22  *
23  * This code now correctly handles the unstacking of wands, staffs,
24  * and rods.  Note the overly paranoid warning about potential pack
25  * overflow, which allows the player to use and drop a stacked item.
26  *
27  * In all "unstacking" scenarios, the "used" object is "carried" as if
28  * the player had just picked it up.  In particular, this means that if
29  * the use of an item induces pack overflow, that item will be dropped.
30  *
31  * For simplicity, these routines induce a full "pack reorganization"
32  * which not only combines similar items, but also reorganizes various
33  * items to obey the current "sorting" method.  This may require about
34  * 400 item comparisons, but only occasionally.
35  *
36  * There may be a BIG problem with any "effect" that can cause "changes"
37  * to the inventory.  For example, a "scroll of recharging" can cause
38  * a wand/staff to "disappear", moving the inventory up.  Luckily, the
39  * scrolls all appear BEFORE the staffs/wands, so this is not a problem.
40  * But, for example, a "staff of recharging" could cause MAJOR problems.
41  * In such a case, it will be best to either (1) "postpone" the effect
42  * until the end of the function, or (2) "change" the effect, say, into
43  * giving a staff "negative" charges, or "turning a staff into a stick".
44  * It seems as though a "rod of recharging" might in fact cause problems.
45  * The basic problem is that the act of recharging (and destroying) an
46  * item causes the inducer of that action to "move", causing "o_ptr" to
47  * no longer point at the correct item, with horrifying results.
48  *
49  * Note that food/potions/scrolls no longer use bit-flags for effects,
50  * but instead use the "sval" (which is also used to sort the objects).
51  * </pre>
52  */
53
54 #include "cmd-item/cmd-magiceat.h"
55 #include "action/action-limited.h"
56 #include "cmd-item/cmd-usestaff.h"
57 #include "cmd-item/cmd-zaprod.h"
58 #include "cmd-item/cmd-zapwand.h"
59 #include "core/asking-player.h"
60 #include "game-option/disturbance-options.h"
61 #include "game-option/text-display-options.h"
62 #include "io/command-repeater.h"
63 #include "io/input-key-acceptor.h"
64 #include "io/input-key-requester.h"
65 #include "main/sound-definitions-table.h"
66 #include "main/sound-of-music.h"
67 #include "object/object-kind-hook.h"
68 #include "object/object-kind.h"
69 #include "sv-definition/sv-other-types.h"
70 #include "sv-definition/sv-rod-types.h"
71 #include "player-info/avatar.h"
72 #include "player/player-class.h"
73 #include "player/player-status.h"
74 #include "spell/spell-info.h"
75 #include "target/target-getter.h"
76 #include "term/screen-processor.h"
77 #include "term/term-color-types.h"
78 #include "util/buffer-shaper.h"
79 #include "util/int-char-converter.h"
80 #include "view/display-messages.h"
81
82 /*!
83  * @brief 魔道具術師の取り込んだ魔力一覧から選択/閲覧する /
84  * @param only_browse 閲覧するだけならばTRUE
85  * @return 選択した魔力のID、キャンセルならば-1を返す
86  */
87 static OBJECT_SUBTYPE_VALUE select_magic_eater(player_type *creature_ptr, bool only_browse)
88 {
89         OBJECT_SUBTYPE_VALUE ext = 0;
90         char choice;
91         bool flag, request_list;
92         tval_type tval = 0;
93         int             ask = TRUE;
94         OBJECT_SUBTYPE_VALUE i = 0;
95         char            out_val[160];
96
97         int menu_line = (use_menu ? 1 : 0);
98
99         COMMAND_CODE sn;
100         if (repeat_pull(&sn))
101         {
102                 /* Verify the spell */
103                 if (sn >= EATER_EXT*2 && !(creature_ptr->magic_num1[sn] > k_info[lookup_kind(TV_ROD, sn-EATER_EXT*2)].pval * (creature_ptr->magic_num2[sn] - 1) * EATER_ROD_CHARGE))
104                         return sn;
105                 else if (sn < EATER_EXT*2 && !(creature_ptr->magic_num1[sn] < EATER_CHARGE))
106                         return sn;
107         }
108         
109         for (i = 0; i < MAX_SPELLS; i++)
110         {
111                 if (creature_ptr->magic_num2[i]) break;
112         }
113
114         if (i == MAX_SPELLS)
115         {
116                 msg_print(_("魔法を覚えていない!", "You don't have any magic!"));
117                 return -1;
118         }
119
120         if (use_menu)
121         {
122                 screen_save();
123
124                 while(!tval)
125                 {
126 #ifdef JP
127                         prt(format(" %s 杖", (menu_line == 1) ? "》" : "  "), 2, 14);
128                         prt(format(" %s 魔法棒", (menu_line == 2) ? "》" : "  "), 3, 14);
129                         prt(format(" %s ロッド", (menu_line == 3) ? "》" : "  "), 4, 14);
130 #else
131                         prt(format(" %s staff", (menu_line == 1) ? "> " : "  "), 2, 14);
132                         prt(format(" %s wand", (menu_line == 2) ? "> " : "  "), 3, 14);
133                         prt(format(" %s rod", (menu_line == 3) ? "> " : "  "), 4, 14);
134 #endif
135
136                         if (only_browse) prt(_("どの種類の魔法を見ますか?", "Which type of magic do you browse?"), 0, 0);
137                         else prt(_("どの種類の魔法を使いますか?", "Which type of magic do you use?"), 0, 0);
138
139                         choice = inkey();
140                         switch(choice)
141                         {
142                         case ESCAPE:
143                         case 'z':
144                         case 'Z':
145                                 screen_load();
146                                 return -1;
147                         case '2':
148                         case 'j':
149                         case 'J':
150                                 menu_line++;
151                                 break;
152                         case '8':
153                         case 'k':
154                         case 'K':
155                                 menu_line+= 2;
156                                 break;
157                         case '\r':
158                         case 'x':
159                         case 'X':
160                                 ext = (menu_line-1)*EATER_EXT;
161                                 if (menu_line == 1) tval = TV_STAFF;
162                                 else if (menu_line == 2) tval = TV_WAND;
163                                 else tval = TV_ROD;
164                                 break;
165                         }
166                         if (menu_line > 3) menu_line -= 3;
167                 }
168                 screen_load();
169         }
170         else
171         {
172         while (TRUE)
173         {
174                 if (!get_com(_("[A] 杖, [B] 魔法棒, [C] ロッド:", "[A] staff, [B] wand, [C] rod:"), &choice, TRUE))
175                 {
176                         return -1;
177                 }
178                 if (choice == 'A' || choice == 'a')
179                 {
180                         ext = 0;
181                         tval = TV_STAFF;
182                         break;
183                 }
184                 if (choice == 'B' || choice == 'b')
185                 {
186                         ext = EATER_EXT;
187                         tval = TV_WAND;
188                         break;
189                 }
190                 if (choice == 'C' || choice == 'c')
191                 {
192                         ext = EATER_EXT*2;
193                         tval = TV_ROD;
194                         break;
195                 }
196         }
197         }
198         for (i = ext; i < ext + EATER_EXT; i++)
199         {
200                 if (creature_ptr->magic_num2[i])
201                 {
202                         if (use_menu) menu_line = i-ext+1;
203                         break;
204                 }
205         }
206         if (i == ext+EATER_EXT)
207         {
208                 msg_print(_("その種類の魔法は覚えていない!", "You don't have that type of magic!"));
209                 return -1;
210         }
211
212         /* Nothing chosen yet */
213         flag = FALSE;
214
215         if (only_browse) strnfmt(out_val, 78, _("('*'で一覧, ESCで中断) どの魔力を見ますか?",
216                                                                                         "(*=List, ESC=exit) Browse which power? "));
217         else strnfmt(out_val, 78, _("('*'で一覧, ESCで中断) どの魔力を使いますか?",
218                                                                 "(*=List, ESC=exit) Use which power? "));
219         screen_save();
220
221         request_list = always_show_list;
222
223         while (!flag)
224         {
225                 /* Show the list */
226                 if (request_list || use_menu)
227                 {
228                         byte y, x = 0;
229                         OBJECT_SUBTYPE_VALUE ctr;
230                         PERCENTAGE chance;
231                         KIND_OBJECT_IDX k_idx;
232                         char dummy[80];
233                         POSITION x1, y1;
234                         DEPTH level;
235                         byte col;
236
237                         strcpy(dummy, "");
238
239                         for (y = 1; y < 20; y++)
240                                 prt("", y, x);
241
242                         y = 1;
243
244                         /* Print header(s) */
245 #ifdef JP
246                         prt(format("                           %s 失率                           %s 失率", (tval == TV_ROD ? "  状態  " : "使用回数"), (tval == TV_ROD ? "  状態  " : "使用回数")), y++, x);
247 #else
248                         prt(format("                           %s Fail                           %s Fail", (tval == TV_ROD ? "  Stat  " : " Charges"), (tval == TV_ROD ? "  Stat  " : " Charges")), y++, x);
249 #endif
250
251                         /* Print list */
252                         for (ctr = 0; ctr < EATER_EXT; ctr++)
253                         {
254                                 if (!creature_ptr->magic_num2[ctr+ext]) continue;
255
256                                 k_idx = lookup_kind(tval, ctr);
257
258                                 if (use_menu)
259                                 {
260                                         if (ctr == (menu_line-1))
261                                                 strcpy(dummy, _("》", "> "));
262                                         else
263                                                 strcpy(dummy, "  ");
264                                 }
265                                 /* letter/number for power selection */
266                                 else
267                                 {
268                                         char letter;
269                                         if (ctr < 26)
270                                                 letter = I2A(ctr);
271                                         else
272                                                 letter = '0' + ctr - 26;
273                                         sprintf(dummy, "%c)",letter);
274                                 }
275                                 x1 = ((ctr < EATER_EXT/2) ? x : x + 40);
276                                 y1 = ((ctr < EATER_EXT/2) ? y + ctr : y + ctr - EATER_EXT/2);
277                                 level = (tval == TV_ROD ? k_info[k_idx].level * 5 / 6 - 5 : k_info[k_idx].level);
278                                 chance = level * 4 / 5 + 20;
279                                 chance -= 3 * (adj_mag_stat[creature_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
280                                 level /= 2;
281                                 if (creature_ptr->lev > level)
282                                 {
283                                         chance -= 3 * (creature_ptr->lev - level);
284                                 }
285                                 chance = mod_spell_chance_1(creature_ptr, chance);
286                                 chance = MAX(chance, adj_mag_fail[creature_ptr->stat_ind[mp_ptr->spell_stat]]);
287                                 /* Stunning makes spells harder */
288                                 if (creature_ptr->stun > 50) chance += 25;
289                                 else if (creature_ptr->stun) chance += 15;
290
291                                 if (chance > 95) chance = 95;
292
293                                 chance = mod_spell_chance_2(creature_ptr, chance);
294
295                                 col = TERM_WHITE;
296
297                                 if (k_idx)
298                                 {
299                                         if (tval == TV_ROD)
300                                         {
301                                                 strcat(dummy, format(
302                                                                _(" %-22.22s 充填:%2d/%2d%3d%%", " %-22.22s   (%2d/%2d) %3d%%"),
303                                                                k_name + k_info[k_idx].name, 
304                                                                creature_ptr->magic_num1[ctr+ext] ? 
305                                                                (creature_ptr->magic_num1[ctr+ext] - 1) / (EATER_ROD_CHARGE * k_info[k_idx].pval) +1 : 0, 
306                                                                creature_ptr->magic_num2[ctr+ext], chance));
307                                                 if (creature_ptr->magic_num1[ctr+ext] > k_info[k_idx].pval * (creature_ptr->magic_num2[ctr+ext]-1) * EATER_ROD_CHARGE) col = TERM_RED;
308                                         }
309                                         else
310                                         {
311                                                 strcat(dummy, format(" %-22.22s    %2d/%2d %3d%%", k_name + k_info[k_idx].name, (s16b)(creature_ptr->magic_num1[ctr+ext]/EATER_CHARGE), creature_ptr->magic_num2[ctr+ext], chance));
312                                                 if (creature_ptr->magic_num1[ctr+ext] < EATER_CHARGE) col = TERM_RED;
313                                         }
314                                 }
315                                 else
316                                         strcpy(dummy, "");
317                                 c_prt(col, dummy, y1, x1);
318                         }
319                 }
320
321                 if (!get_com(out_val, &choice, FALSE)) break;
322
323                 if (use_menu && choice != ' ')
324                 {
325                         switch (choice)
326                         {
327                                 case '0':
328                                 {
329                                         screen_load();
330                                         return 0;
331                                 }
332
333                                 case '8':
334                                 case 'k':
335                                 case 'K':
336                                 {
337                                         do
338                                         {
339                                                 menu_line += EATER_EXT - 1;
340                                                 if (menu_line > EATER_EXT) menu_line -= EATER_EXT;
341                                         } while(!creature_ptr->magic_num2[menu_line+ext-1]);
342                                         break;
343                                 }
344
345                                 case '2':
346                                 case 'j':
347                                 case 'J':
348                                 {
349                                         do
350                                         {
351                                                 menu_line++;
352                                                 if (menu_line > EATER_EXT) menu_line -= EATER_EXT;
353                                         } while(!creature_ptr->magic_num2[menu_line+ext-1]);
354                                         break;
355                                 }
356
357                                 case '4':
358                                 case 'h':
359                                 case 'H':
360                                 case '6':
361                                 case 'l':
362                                 case 'L':
363                                 {
364                                         bool reverse = FALSE;
365                                         if ((choice == '4') || (choice == 'h') || (choice == 'H')) reverse = TRUE;
366                                         if (menu_line > EATER_EXT/2)
367                                         {
368                                                 menu_line -= EATER_EXT/2;
369                                                 reverse = TRUE;
370                                         }
371                                         else menu_line+=EATER_EXT/2;
372                                         while(!creature_ptr->magic_num2[menu_line+ext-1])
373                                         {
374                                                 if (reverse)
375                                                 {
376                                                         menu_line--;
377                                                         if (menu_line < 2) reverse = FALSE;
378                                                 }
379                                                 else
380                                                 {
381                                                         menu_line++;
382                                                         if (menu_line > EATER_EXT-1) reverse = TRUE;
383                                                 }
384                                         }
385                                         break;
386                                 }
387
388                                 case 'x':
389                                 case 'X':
390                                 case '\r':
391                                 {
392                                         i = menu_line - 1;
393                                         ask = FALSE;
394                                         break;
395                                 }
396                         }
397                 }
398
399                 /* Request redraw */
400                 if (use_menu && ask) continue;
401
402                 /* Request redraw */
403                 if (!use_menu && ((choice == ' ') || (choice == '*') || (choice == '?')))
404                 {
405                         /* Hide the list */
406                         if (request_list)
407                         {
408                                 /* Hide list */
409                                 request_list = FALSE;
410                                                                 screen_load();
411                                 screen_save();
412                         }
413                         else
414                                 request_list = TRUE;
415
416                         /* Redo asking */
417                         continue;
418                 }
419
420                 if (!use_menu)
421                 {
422                         if (isalpha(choice))
423                         {
424                                 /* Note verify */
425                                 ask = (isupper(choice));
426
427                                 /* Lowercase */
428                                 if (ask) choice = (char)tolower(choice);
429
430                                 /* Extract request */
431                                 i = (islower(choice) ? A2I(choice) : -1);
432                         }
433                         else
434                         {
435                                 ask = FALSE; /* Can't uppercase digits */
436
437                                 i = choice - '0' + 26;
438                         }
439                 }
440
441                 /* Totally Illegal */
442                 if ((i < 0) || (i > EATER_EXT) || !creature_ptr->magic_num2[i+ext])
443                 {
444                         bell();
445                         continue;
446                 }
447
448                 if (!only_browse)
449                 {
450                         /* Verify it */
451                         if (ask)
452                         {
453                                 char tmp_val[160];
454
455                                 /* Prompt */
456                                 (void) strnfmt(tmp_val, 78, _("%sを使いますか? ", "Use %s?"), k_name + k_info[lookup_kind(tval ,i)].name);
457
458                                 /* Belay that order */
459                                 if (!get_check(tmp_val)) continue;
460                         }
461                         if (tval == TV_ROD)
462                         {
463                                 if (creature_ptr->magic_num1[ext+i]  > k_info[lookup_kind(tval, i)].pval * (creature_ptr->magic_num2[ext+i] - 1) * EATER_ROD_CHARGE)
464                                 {
465                                         msg_print(_("その魔法はまだ充填している最中だ。", "The magic is still charging."));
466                                         msg_print(NULL);
467                                         if (use_menu) ask = TRUE;
468                                         continue;
469                                 }
470                         }
471                         else
472                         {
473                                 if (creature_ptr->magic_num1[ext+i] < EATER_CHARGE)
474                                 {
475                                         msg_print(_("その魔法は使用回数が切れている。", "The magic has no charges left."));
476                                         msg_print(NULL);
477                                         if (use_menu) ask = TRUE;
478                                         continue;
479                                 }
480                         }
481                 }
482
483                 /* Browse */
484                 else
485                 {
486                         int line, j;
487                         char temp[70 * 20];
488
489                         /* Clear lines, position cursor  (really should use strlen here) */
490                         term_erase(7, 23, 255);
491                         term_erase(7, 22, 255);
492                         term_erase(7, 21, 255);
493                         term_erase(7, 20, 255);
494
495                         shape_buffer(k_text + k_info[lookup_kind(tval, i)].text, 62, temp, sizeof(temp));
496                         for (j = 0, line = 21; temp[j]; j += 1 + strlen(&temp[j]))
497                         {
498                                 prt(&temp[j], line, 10);
499                                 line++;
500                         }
501
502                         continue;
503                 }
504
505                 /* Stop the loop */
506                 flag = TRUE;
507         }
508         screen_load();
509
510         if (!flag) return -1;
511
512         repeat_push(ext+i);
513         return ext+i;
514 }
515
516
517 /*!
518  * @brief 取り込んだ魔力を利用するコマンドのメインルーチン /
519  * Use eaten rod, wand or staff
520  * @param only_browse 閲覧するだけならばTRUE
521  * @param powerful 強力発動中の処理ならばTRUE
522  * @return 実際にコマンドを実行したならばTRUEを返す。
523  */
524 bool do_cmd_magic_eater(player_type *creature_ptr, bool only_browse, bool powerful)
525 {
526         OBJECT_SUBTYPE_VALUE item;
527         PERCENTAGE chance;
528         DEPTH level;
529         KIND_OBJECT_IDX k_idx;
530         tval_type tval;
531         OBJECT_SUBTYPE_VALUE sval;
532         bool use_charge = TRUE;
533
534         if (cmd_limit_confused(creature_ptr)) return FALSE;
535
536         item = select_magic_eater(creature_ptr, only_browse);
537         if (item == -1)
538         {
539                 free_turn(creature_ptr);
540                 return FALSE;
541         }
542         if (item >= EATER_EXT*2) {tval = TV_ROD;sval = item - EATER_EXT*2;}
543         else if (item >= EATER_EXT) {tval = TV_WAND;sval = item - EATER_EXT;}
544         else {tval = TV_STAFF; sval = item;}
545         k_idx = lookup_kind(tval, sval);
546
547         level = (tval == TV_ROD ? k_info[k_idx].level * 5 / 6 - 5 : k_info[k_idx].level);
548         chance = level * 4 / 5 + 20;
549         chance -= 3 * (adj_mag_stat[creature_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
550         level /= 2;
551         if (creature_ptr->lev > level)
552         {
553                 chance -= 3 * (creature_ptr->lev - level);
554         }
555         chance = mod_spell_chance_1(creature_ptr, chance);
556         chance = MAX(chance, adj_mag_fail[creature_ptr->stat_ind[mp_ptr->spell_stat]]);
557         /* Stunning makes spells harder */
558         if (creature_ptr->stun > 50) chance += 25;
559         else if (creature_ptr->stun) chance += 15;
560
561         if (chance > 95) chance = 95;
562
563         chance = mod_spell_chance_2(creature_ptr, chance);
564
565         if (randint0(100) < chance)
566         {
567                 if (flush_failure) flush();
568                 
569                 msg_print(_("呪文をうまく唱えられなかった!", "You failed to get the magic off!"));
570                 sound(SOUND_FAIL);
571                 if (randint1(100) >= chance)
572                         chg_virtue(creature_ptr, V_CHANCE,-1);
573                 take_turn(creature_ptr, 100);
574
575                 return TRUE;
576         }
577         else
578         {
579                 DIRECTION dir = 0;
580
581                 if (tval == TV_ROD)
582                 {
583                         if ((sval >= SV_ROD_MIN_DIRECTION) && (sval != SV_ROD_HAVOC) && (sval != SV_ROD_AGGRAVATE) && (sval != SV_ROD_PESTICIDE))
584                                 if (!get_aim_dir(creature_ptr, &dir)) return FALSE;
585                         rod_effect(creature_ptr, sval, dir, &use_charge, powerful, TRUE);
586                         if (!use_charge) return FALSE;
587                 }
588                 else if (tval == TV_WAND)
589                 {
590                         if (!get_aim_dir(creature_ptr, &dir)) return FALSE;
591                         wand_effect(creature_ptr, sval, dir, powerful, TRUE);
592                 }
593                 else
594                 {
595                         staff_effect(creature_ptr, sval, &use_charge, powerful, TRUE, TRUE);
596                         if (!use_charge) return FALSE;
597                 }
598                 if (randint1(100) < chance)
599                         chg_virtue(creature_ptr, V_CHANCE,1);
600         }
601         take_turn(creature_ptr, 100);
602         if (tval == TV_ROD) creature_ptr->magic_num1[item] += k_info[k_idx].pval * EATER_ROD_CHARGE;
603         else creature_ptr->magic_num1[item] -= EATER_CHARGE;
604
605         return TRUE;
606 }