OSDN Git Service

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