OSDN Git Service

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