OSDN Git Service

[Refactor] #37353 record_turn の宣言を gameoption.h へ移動.
[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 p_ptr->inventory_list.  For example, a "scroll of recharging" can cause
39  * a wand/staff to "disappear", moving the p_ptr->inventory_list 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 "util.h"
57
58 #include "cmd-basic.h"
59 #include "cmd-usestaff.h"
60 #include "cmd-zaprod.h"
61 #include "cmd-zapwand.h"
62 #include "avatar.h"
63 #include "player-status.h"
64 #include "spells.h"
65 #include "player-class.h"
66 #include "objectkind.h"
67 #include "targeting.h"
68
69 /*!
70  * @brief 魔道具術師の取り込んだ魔力一覧から選択/閲覧する /
71  * @param only_browse 閲覧するだけならばTRUE
72  * @return 選択した魔力のID、キャンセルならば-1を返す
73  */
74 static OBJECT_SUBTYPE_VALUE select_magic_eater(bool only_browse)
75 {
76         OBJECT_SUBTYPE_VALUE ext = 0;
77         char choice;
78         bool flag, request_list;
79         OBJECT_TYPE_VALUE tval = 0;
80         int             ask = TRUE;
81         OBJECT_SUBTYPE_VALUE i = 0;
82         char            out_val[160];
83
84         int menu_line = (use_menu ? 1 : 0);
85
86         COMMAND_CODE sn;
87         if (repeat_pull(&sn))
88         {
89                 /* Verify the spell */
90                 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))
91                         return sn;
92                 else if (sn < EATER_EXT*2 && !(p_ptr->magic_num1[sn] < EATER_CHARGE))
93                         return sn;
94         }
95         
96         for (i = 0; i < 108; i++)
97         {
98                 if (p_ptr->magic_num2[i]) break;
99         }
100         if (i == 108)
101         {
102                 msg_print(_("魔法を覚えていない!", "You don't have any magic!"));
103                 return -1;
104         }
105
106         if (use_menu)
107         {
108                 screen_save();
109
110                 while(!tval)
111                 {
112 #ifdef JP
113                         prt(format(" %s 杖", (menu_line == 1) ? "》" : "  "), 2, 14);
114                         prt(format(" %s 魔法棒", (menu_line == 2) ? "》" : "  "), 3, 14);
115                         prt(format(" %s ロッド", (menu_line == 3) ? "》" : "  "), 4, 14);
116 #else
117                         prt(format(" %s staff", (menu_line == 1) ? "> " : "  "), 2, 14);
118                         prt(format(" %s wand", (menu_line == 2) ? "> " : "  "), 3, 14);
119                         prt(format(" %s rod", (menu_line == 3) ? "> " : "  "), 4, 14);
120 #endif
121
122                         if (only_browse) prt(_("どの種類の魔法を見ますか?", "Which type of magic do you browse?"), 0, 0);
123                         else prt(_("どの種類の魔法を使いますか?", "Which type of magic do you use?"), 0, 0);
124
125                         choice = inkey();
126                         switch(choice)
127                         {
128                         case ESCAPE:
129                         case 'z':
130                         case 'Z':
131                                 screen_load();
132                                 return -1;
133                         case '2':
134                         case 'j':
135                         case 'J':
136                                 menu_line++;
137                                 break;
138                         case '8':
139                         case 'k':
140                         case 'K':
141                                 menu_line+= 2;
142                                 break;
143                         case '\r':
144                         case 'x':
145                         case 'X':
146                                 ext = (menu_line-1)*EATER_EXT;
147                                 if (menu_line == 1) tval = TV_STAFF;
148                                 else if (menu_line == 2) tval = TV_WAND;
149                                 else tval = TV_ROD;
150                                 break;
151                         }
152                         if (menu_line > 3) menu_line -= 3;
153                 }
154                 screen_load();
155         }
156         else
157         {
158         while (TRUE)
159         {
160                 if (!get_com(_("[A] 杖, [B] 魔法棒, [C] ロッド:", "[A] staff, [B] wand, [C] rod:"), &choice, TRUE))
161                 {
162                         return -1;
163                 }
164                 if (choice == 'A' || choice == 'a')
165                 {
166                         ext = 0;
167                         tval = TV_STAFF;
168                         break;
169                 }
170                 if (choice == 'B' || choice == 'b')
171                 {
172                         ext = EATER_EXT;
173                         tval = TV_WAND;
174                         break;
175                 }
176                 if (choice == 'C' || choice == 'c')
177                 {
178                         ext = EATER_EXT*2;
179                         tval = TV_ROD;
180                         break;
181                 }
182         }
183         }
184         for (i = ext; i < ext + EATER_EXT; i++)
185         {
186                 if (p_ptr->magic_num2[i])
187                 {
188                         if (use_menu) menu_line = i-ext+1;
189                         break;
190                 }
191         }
192         if (i == ext+EATER_EXT)
193         {
194                 msg_print(_("その種類の魔法は覚えていない!", "You don't have that type of magic!"));
195                 return -1;
196         }
197
198         /* Nothing chosen yet */
199         flag = FALSE;
200
201         /* Build a prompt */
202         if (only_browse) strnfmt(out_val, 78, _("('*'で一覧, ESCで中断) どの魔力を見ますか?",
203                                                                                         "(*=List, ESC=exit) Browse which power? "));
204         else strnfmt(out_val, 78, _("('*'で一覧, ESCで中断) どの魔力を使いますか?",
205                                                                 "(*=List, ESC=exit) Use which power? "));
206         screen_save();
207
208         request_list = always_show_list;
209
210         while (!flag)
211         {
212                 /* Show the list */
213                 if (request_list || use_menu)
214                 {
215                         byte y, x = 0;
216                         OBJECT_SUBTYPE_VALUE ctr;
217                         PERCENTAGE chance;
218                         KIND_OBJECT_IDX k_idx;
219                         char dummy[80];
220                         POSITION x1, y1;
221                         DEPTH level;
222                         byte col;
223
224                         strcpy(dummy, "");
225
226                         for (y = 1; y < 20; y++)
227                                 prt("", y, x);
228
229                         y = 1;
230
231                         /* Print header(s) */
232 #ifdef JP
233                         prt(format("                           %s 失率                           %s 失率", (tval == TV_ROD ? "  状態  " : "使用回数"), (tval == TV_ROD ? "  状態  " : "使用回数")), y++, x);
234 #else
235                         prt(format("                           %s Fail                           %s Fail", (tval == TV_ROD ? "  Stat  " : " Charges"), (tval == TV_ROD ? "  Stat  " : " Charges")), y++, x);
236 #endif
237
238                         /* Print list */
239                         for (ctr = 0; ctr < EATER_EXT; ctr++)
240                         {
241                                 if (!p_ptr->magic_num2[ctr+ext]) continue;
242
243                                 k_idx = lookup_kind(tval, ctr);
244
245                                 if (use_menu)
246                                 {
247                                         if (ctr == (menu_line-1))
248                                                 strcpy(dummy, _("》", "> "));
249                                         else
250                                                 strcpy(dummy, "  ");
251                                 }
252                                 /* letter/number for power selection */
253                                 else
254                                 {
255                                         char letter;
256                                         if (ctr < 26)
257                                                 letter = I2A(ctr);
258                                         else
259                                                 letter = '0' + ctr - 26;
260                                         sprintf(dummy, "%c)",letter);
261                                 }
262                                 x1 = ((ctr < EATER_EXT/2) ? x : x + 40);
263                                 y1 = ((ctr < EATER_EXT/2) ? y + ctr : y + ctr - EATER_EXT/2);
264                                 level = (tval == TV_ROD ? k_info[k_idx].level * 5 / 6 - 5 : k_info[k_idx].level);
265                                 chance = level * 4 / 5 + 20;
266                                 chance -= 3 * (adj_mag_stat[p_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
267                                 level /= 2;
268                                 if (p_ptr->lev > level)
269                                 {
270                                         chance -= 3 * (p_ptr->lev - level);
271                                 }
272                                 chance = mod_spell_chance_1(chance);
273                                 chance = MAX(chance, adj_mag_fail[p_ptr->stat_ind[mp_ptr->spell_stat]]);
274                                 /* Stunning makes spells harder */
275                                 if (p_ptr->stun > 50) chance += 25;
276                                 else if (p_ptr->stun) chance += 15;
277
278                                 if (chance > 95) chance = 95;
279
280                                 chance = mod_spell_chance_2(chance);
281
282                                 col = TERM_WHITE;
283
284                                 if (k_idx)
285                                 {
286                                         if (tval == TV_ROD)
287                                         {
288                                                 strcat(dummy, format(
289                                                                _(" %-22.22s 充填:%2d/%2d%3d%%", " %-22.22s   (%2d/%2d) %3d%%"),
290                                                                k_name + k_info[k_idx].name, 
291                                                                p_ptr->magic_num1[ctr+ext] ? 
292                                                                (p_ptr->magic_num1[ctr+ext] - 1) / (EATER_ROD_CHARGE * k_info[k_idx].pval) +1 : 0, 
293                                                                p_ptr->magic_num2[ctr+ext], chance));
294                                                 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;
295                                         }
296                                         else
297                                         {
298                                                 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));
299                                                 if (p_ptr->magic_num1[ctr+ext] < EATER_CHARGE) col = TERM_RED;
300                                         }
301                                 }
302                                 else
303                                         strcpy(dummy, "");
304                                 c_prt(col, dummy, y1, x1);
305                         }
306                 }
307
308                 if (!get_com(out_val, &choice, FALSE)) break;
309
310                 if (use_menu && choice != ' ')
311                 {
312                         switch (choice)
313                         {
314                                 case '0':
315                                 {
316                                         screen_load();
317                                         return 0;
318                                 }
319
320                                 case '8':
321                                 case 'k':
322                                 case 'K':
323                                 {
324                                         do
325                                         {
326                                                 menu_line += EATER_EXT - 1;
327                                                 if (menu_line > EATER_EXT) menu_line -= EATER_EXT;
328                                         } while(!p_ptr->magic_num2[menu_line+ext-1]);
329                                         break;
330                                 }
331
332                                 case '2':
333                                 case 'j':
334                                 case 'J':
335                                 {
336                                         do
337                                         {
338                                                 menu_line++;
339                                                 if (menu_line > EATER_EXT) menu_line -= EATER_EXT;
340                                         } while(!p_ptr->magic_num2[menu_line+ext-1]);
341                                         break;
342                                 }
343
344                                 case '4':
345                                 case 'h':
346                                 case 'H':
347                                 case '6':
348                                 case 'l':
349                                 case 'L':
350                                 {
351                                         bool reverse = FALSE;
352                                         if ((choice == '4') || (choice == 'h') || (choice == 'H')) reverse = TRUE;
353                                         if (menu_line > EATER_EXT/2)
354                                         {
355                                                 menu_line -= EATER_EXT/2;
356                                                 reverse = TRUE;
357                                         }
358                                         else menu_line+=EATER_EXT/2;
359                                         while(!p_ptr->magic_num2[menu_line+ext-1])
360                                         {
361                                                 if (reverse)
362                                                 {
363                                                         menu_line--;
364                                                         if (menu_line < 2) reverse = FALSE;
365                                                 }
366                                                 else
367                                                 {
368                                                         menu_line++;
369                                                         if (menu_line > EATER_EXT-1) reverse = TRUE;
370                                                 }
371                                         }
372                                         break;
373                                 }
374
375                                 case 'x':
376                                 case 'X':
377                                 case '\r':
378                                 {
379                                         i = menu_line - 1;
380                                         ask = FALSE;
381                                         break;
382                                 }
383                         }
384                 }
385
386                 /* Request redraw */
387                 if (use_menu && ask) continue;
388
389                 /* Request redraw */
390                 if (!use_menu && ((choice == ' ') || (choice == '*') || (choice == '?')))
391                 {
392                         /* Hide the list */
393                         if (request_list)
394                         {
395                                 /* Hide list */
396                                 request_list = FALSE;
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         screen_load();
496
497         if (!flag) return -1;
498
499         repeat_push(ext+i);
500         return ext+i;
501 }
502
503
504 /*!
505  * @brief 取り込んだ魔力を利用するコマンドのメインルーチン /
506  * Use eaten rod, wand or staff
507  * @param only_browse 閲覧するだけならばTRUE
508  * @param powerful 強力発動中の処理ならばTRUE
509  * @return 実際にコマンドを実行したならばTRUEを返す。
510  */
511 bool do_cmd_magic_eater(bool only_browse, bool powerful)
512 {
513         OBJECT_SUBTYPE_VALUE item;
514         PERCENTAGE chance;
515         DEPTH level;
516         KIND_OBJECT_IDX k_idx;
517         OBJECT_TYPE_VALUE tval;
518         OBJECT_SUBTYPE_VALUE sval;
519         bool use_charge = TRUE;
520
521         if (cmd_limit_confused(p_ptr)) return FALSE;
522
523         item = select_magic_eater(only_browse);
524         if (item == -1)
525         {
526                 free_turn(p_ptr);
527                 return FALSE;
528         }
529         if (item >= EATER_EXT*2) {tval = TV_ROD;sval = item - EATER_EXT*2;}
530         else if (item >= EATER_EXT) {tval = TV_WAND;sval = item - EATER_EXT;}
531         else {tval = TV_STAFF; sval = item;}
532         k_idx = lookup_kind(tval, sval);
533
534         level = (tval == TV_ROD ? k_info[k_idx].level * 5 / 6 - 5 : k_info[k_idx].level);
535         chance = level * 4 / 5 + 20;
536         chance -= 3 * (adj_mag_stat[p_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
537         level /= 2;
538         if (p_ptr->lev > level)
539         {
540                 chance -= 3 * (p_ptr->lev - level);
541         }
542         chance = mod_spell_chance_1(chance);
543         chance = MAX(chance, adj_mag_fail[p_ptr->stat_ind[mp_ptr->spell_stat]]);
544         /* Stunning makes spells harder */
545         if (p_ptr->stun > 50) chance += 25;
546         else if (p_ptr->stun) chance += 15;
547
548         if (chance > 95) chance = 95;
549
550         chance = mod_spell_chance_2(chance);
551
552         if (randint0(100) < chance)
553         {
554                 if (flush_failure) flush();
555                 
556                 msg_print(_("呪文をうまく唱えられなかった!", "You failed to get the magic off!"));
557                 sound(SOUND_FAIL);
558                 if (randint1(100) >= chance)
559                         chg_virtue(V_CHANCE,-1);
560                 take_turn(p_ptr, 100);
561
562                 return TRUE;
563         }
564         else
565         {
566                 DIRECTION dir = 0;
567
568                 if (tval == TV_ROD)
569                 {
570                         if ((sval >= SV_ROD_MIN_DIRECTION) && (sval != SV_ROD_HAVOC) && (sval != SV_ROD_AGGRAVATE) && (sval != SV_ROD_PESTICIDE))
571                                 if (!get_aim_dir(&dir)) return FALSE;
572                         rod_effect(sval, dir, &use_charge, powerful, TRUE);
573                         if (!use_charge) return FALSE;
574                 }
575                 else if (tval == TV_WAND)
576                 {
577                         if (!get_aim_dir(&dir)) return FALSE;
578                         wand_effect(sval, dir, powerful, TRUE);
579                 }
580                 else
581                 {
582                         staff_effect(sval, &use_charge, powerful, TRUE, TRUE);
583                         if (!use_charge) return FALSE;
584                 }
585                 if (randint1(100) < chance)
586                         chg_virtue(V_CHANCE,1);
587         }
588         take_turn(p_ptr, 100);
589         if (tval == TV_ROD) p_ptr->magic_num1[item] += k_info[k_idx].pval * EATER_ROD_CHARGE;
590         else p_ptr->magic_num1[item] -= EATER_CHARGE;
591
592         return TRUE;
593 }