OSDN Git Service

Merge remote-tracking branch 'remotes/origin/SHIELD_SKILL'
[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         COMMAND_CODE sn;
79         if (repeat_pull(&sn))
80         {
81                 /* Verify the spell */
82                 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))
83                         return sn;
84                 else if (sn < EATER_EXT*2 && !(p_ptr->magic_num1[sn] < EATER_CHARGE))
85                         return sn;
86         }
87         
88         for (i = 0; i < 108; i++)
89         {
90                 if (p_ptr->magic_num2[i]) break;
91         }
92         if (i == 108)
93         {
94                 msg_print(_("魔法を覚えていない!", "You don't have any magic!"));
95                 return -1;
96         }
97
98         if (use_menu)
99         {
100                 screen_save();
101
102                 while(!tval)
103                 {
104 #ifdef JP
105                         prt(format(" %s 杖", (menu_line == 1) ? "》" : "  "), 2, 14);
106                         prt(format(" %s 魔法棒", (menu_line == 2) ? "》" : "  "), 3, 14);
107                         prt(format(" %s ロッド", (menu_line == 3) ? "》" : "  "), 4, 14);
108 #else
109                         prt(format(" %s staff", (menu_line == 1) ? "> " : "  "), 2, 14);
110                         prt(format(" %s wand", (menu_line == 2) ? "> " : "  "), 3, 14);
111                         prt(format(" %s rod", (menu_line == 3) ? "> " : "  "), 4, 14);
112 #endif
113
114                         if (only_browse) prt(_("どの種類の魔法を見ますか?", "Which type of magic do you browse?"), 0, 0);
115                         else prt(_("どの種類の魔法を使いますか?", "Which type of magic do you use?"), 0, 0);
116
117                         choice = inkey();
118                         switch(choice)
119                         {
120                         case ESCAPE:
121                         case 'z':
122                         case 'Z':
123                                 screen_load();
124                                 return -1;
125                         case '2':
126                         case 'j':
127                         case 'J':
128                                 menu_line++;
129                                 break;
130                         case '8':
131                         case 'k':
132                         case 'K':
133                                 menu_line+= 2;
134                                 break;
135                         case '\r':
136                         case 'x':
137                         case 'X':
138                                 ext = (menu_line-1)*EATER_EXT;
139                                 if (menu_line == 1) tval = TV_STAFF;
140                                 else if (menu_line == 2) tval = TV_WAND;
141                                 else tval = TV_ROD;
142                                 break;
143                         }
144                         if (menu_line > 3) menu_line -= 3;
145                 }
146                 screen_load();
147         }
148         else
149         {
150         while (TRUE)
151         {
152                 if (!get_com(_("[A] 杖, [B] 魔法棒, [C] ロッド:", "[A] staff, [B] wand, [C] rod:"), &choice, TRUE))
153                 {
154                         return -1;
155                 }
156                 if (choice == 'A' || choice == 'a')
157                 {
158                         ext = 0;
159                         tval = TV_STAFF;
160                         break;
161                 }
162                 if (choice == 'B' || choice == 'b')
163                 {
164                         ext = EATER_EXT;
165                         tval = TV_WAND;
166                         break;
167                 }
168                 if (choice == 'C' || choice == 'c')
169                 {
170                         ext = EATER_EXT*2;
171                         tval = TV_ROD;
172                         break;
173                 }
174         }
175         }
176         for (i = ext; i < ext + EATER_EXT; i++)
177         {
178                 if (p_ptr->magic_num2[i])
179                 {
180                         if (use_menu) menu_line = i-ext+1;
181                         break;
182                 }
183         }
184         if (i == ext+EATER_EXT)
185         {
186                 msg_print(_("その種類の魔法は覚えていない!", "You don't have that type of magic!"));
187                 return -1;
188         }
189
190         /* Nothing chosen yet */
191         flag = FALSE;
192
193         /* Build a prompt */
194         if (only_browse) strnfmt(out_val, 78, _("('*'で一覧, ESCで中断) どの魔力を見ますか?",
195                                                                                         "(*=List, ESC=exit) Browse which power? "));
196         else strnfmt(out_val, 78, _("('*'で一覧, ESCで中断) どの魔力を使いますか?",
197                                                                 "(*=List, ESC=exit) Use which power? "));
198                 screen_save();
199
200         request_list = always_show_list;
201
202         /* Get a spell from the user */
203         while (!flag)
204         {
205                 /* Show the list */
206                 if (request_list || use_menu)
207                 {
208                         byte y, x = 0;
209                         OBJECT_SUBTYPE_VALUE ctr;
210                         PERCENTAGE chance;
211                         KIND_OBJECT_IDX k_idx;
212                         char dummy[80];
213                         POSITION x1, y1;
214                         DEPTH level;
215                         byte col;
216
217                         strcpy(dummy, "");
218
219                         for (y = 1; y < 20; y++)
220                                 prt("", y, x);
221
222                         y = 1;
223
224                         /* Print header(s) */
225 #ifdef JP
226                         prt(format("                           %s 失率                           %s 失率", (tval == TV_ROD ? "  状態  " : "使用回数"), (tval == TV_ROD ? "  状態  " : "使用回数")), y++, x);
227 #else
228                         prt(format("                           %s Fail                           %s Fail", (tval == TV_ROD ? "  Stat  " : " Charges"), (tval == TV_ROD ? "  Stat  " : " Charges")), y++, x);
229 #endif
230
231                         /* Print list */
232                         for (ctr = 0; ctr < EATER_EXT; ctr++)
233                         {
234                                 if (!p_ptr->magic_num2[ctr+ext]) continue;
235
236                                 k_idx = lookup_kind(tval, ctr);
237
238                                 if (use_menu)
239                                 {
240                                         if (ctr == (menu_line-1))
241                                                 strcpy(dummy, _("》", "> "));
242                                         else
243                                                 strcpy(dummy, "  ");
244                                 }
245                                 /* letter/number for power selection */
246                                 else
247                                 {
248                                         char letter;
249                                         if (ctr < 26)
250                                                 letter = I2A(ctr);
251                                         else
252                                                 letter = '0' + ctr - 26;
253                                         sprintf(dummy, "%c)",letter);
254                                 }
255                                 x1 = ((ctr < EATER_EXT/2) ? x : x + 40);
256                                 y1 = ((ctr < EATER_EXT/2) ? y + ctr : y + ctr - EATER_EXT/2);
257                                 level = (tval == TV_ROD ? k_info[k_idx].level * 5 / 6 - 5 : k_info[k_idx].level);
258                                 chance = level * 4 / 5 + 20;
259                                 chance -= 3 * (adj_mag_stat[p_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
260                                 level /= 2;
261                                 if (p_ptr->lev > level)
262                                 {
263                                         chance -= 3 * (p_ptr->lev - level);
264                                 }
265                                 chance = mod_spell_chance_1(chance);
266                                 chance = MAX(chance, adj_mag_fail[p_ptr->stat_ind[mp_ptr->spell_stat]]);
267                                 /* Stunning makes spells harder */
268                                 if (p_ptr->stun > 50) chance += 25;
269                                 else if (p_ptr->stun) chance += 15;
270
271                                 if (chance > 95) chance = 95;
272
273                                 chance = mod_spell_chance_2(chance);
274
275                                 col = TERM_WHITE;
276
277                                 if (k_idx)
278                                 {
279                                         if (tval == TV_ROD)
280                                         {
281                                                 strcat(dummy, format(
282                                                                _(" %-22.22s 充填:%2d/%2d%3d%%", " %-22.22s   (%2d/%2d) %3d%%"),
283                                                                k_name + k_info[k_idx].name, 
284                                                                p_ptr->magic_num1[ctr+ext] ? 
285                                                                (p_ptr->magic_num1[ctr+ext] - 1) / (EATER_ROD_CHARGE * k_info[k_idx].pval) +1 : 0, 
286                                                                p_ptr->magic_num2[ctr+ext], chance));
287                                                 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;
288                                         }
289                                         else
290                                         {
291                                                 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));
292                                                 if (p_ptr->magic_num1[ctr+ext] < EATER_CHARGE) col = TERM_RED;
293                                         }
294                                 }
295                                 else
296                                         strcpy(dummy, "");
297                                 c_prt(col, dummy, y1, x1);
298                         }
299                 }
300
301                 if (!get_com(out_val, &choice, FALSE)) break;
302
303                 if (use_menu && choice != ' ')
304                 {
305                         switch (choice)
306                         {
307                                 case '0':
308                                 {
309                                         screen_load();
310                                         return 0;
311                                 }
312
313                                 case '8':
314                                 case 'k':
315                                 case 'K':
316                                 {
317                                         do
318                                         {
319                                                 menu_line += EATER_EXT - 1;
320                                                 if (menu_line > EATER_EXT) menu_line -= EATER_EXT;
321                                         } while(!p_ptr->magic_num2[menu_line+ext-1]);
322                                         break;
323                                 }
324
325                                 case '2':
326                                 case 'j':
327                                 case 'J':
328                                 {
329                                         do
330                                         {
331                                                 menu_line++;
332                                                 if (menu_line > EATER_EXT) menu_line -= EATER_EXT;
333                                         } while(!p_ptr->magic_num2[menu_line+ext-1]);
334                                         break;
335                                 }
336
337                                 case '4':
338                                 case 'h':
339                                 case 'H':
340                                 case '6':
341                                 case 'l':
342                                 case 'L':
343                                 {
344                                         bool reverse = FALSE;
345                                         if ((choice == '4') || (choice == 'h') || (choice == 'H')) reverse = TRUE;
346                                         if (menu_line > EATER_EXT/2)
347                                         {
348                                                 menu_line -= EATER_EXT/2;
349                                                 reverse = TRUE;
350                                         }
351                                         else menu_line+=EATER_EXT/2;
352                                         while(!p_ptr->magic_num2[menu_line+ext-1])
353                                         {
354                                                 if (reverse)
355                                                 {
356                                                         menu_line--;
357                                                         if (menu_line < 2) reverse = FALSE;
358                                                 }
359                                                 else
360                                                 {
361                                                         menu_line++;
362                                                         if (menu_line > EATER_EXT-1) reverse = TRUE;
363                                                 }
364                                         }
365                                         break;
366                                 }
367
368                                 case 'x':
369                                 case 'X':
370                                 case '\r':
371                                 {
372                                         i = menu_line - 1;
373                                         ask = FALSE;
374                                         break;
375                                 }
376                         }
377                 }
378
379                 /* Request redraw */
380                 if (use_menu && ask) continue;
381
382                 /* Request redraw */
383                 if (!use_menu && ((choice == ' ') || (choice == '*') || (choice == '?')))
384                 {
385                         /* Hide the list */
386                         if (request_list)
387                         {
388                                 /* Hide list */
389                                 request_list = FALSE;
390                                                                 screen_load();
391                                 screen_save();
392                         }
393                         else
394                                 request_list = TRUE;
395
396                         /* Redo asking */
397                         continue;
398                 }
399
400                 if (!use_menu)
401                 {
402                         if (isalpha(choice))
403                         {
404                                 /* Note verify */
405                                 ask = (isupper(choice));
406
407                                 /* Lowercase */
408                                 if (ask) choice = (char)tolower(choice);
409
410                                 /* Extract request */
411                                 i = (islower(choice) ? A2I(choice) : -1);
412                         }
413                         else
414                         {
415                                 ask = FALSE; /* Can't uppercase digits */
416
417                                 i = choice - '0' + 26;
418                         }
419                 }
420
421                 /* Totally Illegal */
422                 if ((i < 0) || (i > EATER_EXT) || !p_ptr->magic_num2[i+ext])
423                 {
424                         bell();
425                         continue;
426                 }
427
428                 if (!only_browse)
429                 {
430                         /* Verify it */
431                         if (ask)
432                         {
433                                 char tmp_val[160];
434
435                                 /* Prompt */
436                                 (void) strnfmt(tmp_val, 78, _("%sを使いますか? ", "Use %s?"), k_name + k_info[lookup_kind(tval ,i)].name);
437
438                                 /* Belay that order */
439                                 if (!get_check(tmp_val)) continue;
440                         }
441                         if (tval == TV_ROD)
442                         {
443                                 if (p_ptr->magic_num1[ext+i]  > k_info[lookup_kind(tval, i)].pval * (p_ptr->magic_num2[ext+i] - 1) * EATER_ROD_CHARGE)
444                                 {
445                                         msg_print(_("その魔法はまだ充填している最中だ。", "The magic are still charging."));
446                                         msg_print(NULL);
447                                         if (use_menu) ask = TRUE;
448                                         continue;
449                                 }
450                         }
451                         else
452                         {
453                                 if (p_ptr->magic_num1[ext+i] < EATER_CHARGE)
454                                 {
455                                         msg_print(_("その魔法は使用回数が切れている。", "The magic has no charges left."));
456                                         msg_print(NULL);
457                                         if (use_menu) ask = TRUE;
458                                         continue;
459                                 }
460                         }
461                 }
462
463                 /* Browse */
464                 else
465                 {
466                         int line, j;
467                         char temp[70 * 20];
468
469                         /* Clear lines, position cursor  (really should use strlen here) */
470                         Term_erase(7, 23, 255);
471                         Term_erase(7, 22, 255);
472                         Term_erase(7, 21, 255);
473                         Term_erase(7, 20, 255);
474
475                         roff_to_buf(k_text + k_info[lookup_kind(tval, i)].text, 62, temp, sizeof(temp));
476                         for (j = 0, line = 21; temp[j]; j += 1 + strlen(&temp[j]))
477                         {
478                                 prt(&temp[j], line, 10);
479                                 line++;
480                         }
481
482                         continue;
483                 }
484
485                 /* Stop the loop */
486                 flag = TRUE;
487         }
488         screen_load();
489
490         if (!flag) return -1;
491
492         repeat_push(ext+i);
493         return ext+i;
494 }
495
496
497 /*!
498  * @brief 取り込んだ魔力を利用するコマンドのメインルーチン /
499  * Use eaten rod, wand or staff
500  * @param only_browse 閲覧するだけならばTRUE
501  * @param powerful 強力発動中の処理ならばTRUE
502  * @return 実際にコマンドを実行したならばTRUEを返す。
503  */
504 bool do_cmd_magic_eater(bool only_browse, bool powerful)
505 {
506         OBJECT_SUBTYPE_VALUE item;
507         PERCENTAGE chance;
508         DEPTH level;
509         KIND_OBJECT_IDX k_idx;
510         OBJECT_TYPE_VALUE tval;
511         OBJECT_SUBTYPE_VALUE sval;
512         bool use_charge = TRUE;
513
514         /* Not when confused */
515         if (!only_browse && p_ptr->confused)
516         {
517                 msg_print(_("混乱していて唱えられない!", "You are too confused!"));
518                 return FALSE;
519         }
520
521         item = select_magic_eater(only_browse);
522         if (item == -1)
523         {
524                 p_ptr->energy_use = 0;
525                 return FALSE;
526         }
527         if (item >= EATER_EXT*2) {tval = TV_ROD;sval = item - EATER_EXT*2;}
528         else if (item >= EATER_EXT) {tval = TV_WAND;sval = item - EATER_EXT;}
529         else {tval = TV_STAFF; sval = item;}
530         k_idx = lookup_kind(tval, sval);
531
532         level = (tval == TV_ROD ? k_info[k_idx].level * 5 / 6 - 5 : k_info[k_idx].level);
533         chance = level * 4 / 5 + 20;
534         chance -= 3 * (adj_mag_stat[p_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
535         level /= 2;
536         if (p_ptr->lev > level)
537         {
538                 chance -= 3 * (p_ptr->lev - level);
539         }
540         chance = mod_spell_chance_1(chance);
541         chance = MAX(chance, adj_mag_fail[p_ptr->stat_ind[mp_ptr->spell_stat]]);
542         /* Stunning makes spells harder */
543         if (p_ptr->stun > 50) chance += 25;
544         else if (p_ptr->stun) chance += 15;
545
546         if (chance > 95) chance = 95;
547
548         chance = mod_spell_chance_2(chance);
549
550         if (randint0(100) < chance)
551         {
552                 if (flush_failure) flush();
553                 
554                 msg_print(_("呪文をうまく唱えられなかった!", "You failed to get the magic off!"));
555                 sound(SOUND_FAIL);
556                 if (randint1(100) >= chance)
557                         chg_virtue(V_CHANCE,-1);
558                 p_ptr->energy_use = 100;
559
560                 return TRUE;
561         }
562         else
563         {
564                 DIRECTION dir = 0;
565
566                 if (tval == TV_ROD)
567                 {
568                         if ((sval >= SV_ROD_MIN_DIRECTION) && (sval != SV_ROD_HAVOC) && (sval != SV_ROD_AGGRAVATE) && (sval != SV_ROD_PESTICIDE))
569                                 if (!get_aim_dir(&dir)) return FALSE;
570                         rod_effect(sval, dir, &use_charge, powerful, TRUE);
571                         if (!use_charge) return FALSE;
572                 }
573                 else if (tval == TV_WAND)
574                 {
575                         if (!get_aim_dir(&dir)) return FALSE;
576                         wand_effect(sval, dir, powerful, TRUE);
577                 }
578                 else
579                 {
580                         staff_effect(sval, &use_charge, powerful, TRUE, TRUE);
581                         if (!use_charge) return FALSE;
582                 }
583                 if (randint1(100) < chance)
584                         chg_virtue(V_CHANCE,1);
585         }
586         p_ptr->energy_use = 100;
587         if (tval == TV_ROD) p_ptr->magic_num1[item] += k_info[k_idx].pval * EATER_ROD_CHARGE;
588         else p_ptr->magic_num1[item] -= EATER_CHARGE;
589
590         return TRUE;
591 }