OSDN Git Service

In Vault Quest, target weapons must not be auto-picked by diceboost.
[hengband/hengband.git] / src / cmd5.c
1 /* File: cmd5.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: Spell/Prayer commands */
12
13 #include "angband.h"
14
15 cptr spell_category_name(int tval)
16 {
17         switch (tval)
18         {
19 #ifdef JP
20         case TV_HISSATSU_BOOK:
21                 return "ɬ»¦µ»";
22         case TV_LIFE_BOOK:
23                 return "µ§¤ê";
24         case TV_MUSIC_BOOK:
25                 return "²Î";
26         default:
27                 return "¼öʸ";
28 #else
29         case TV_HISSATSU_BOOK:
30                 return "art";
31         case TV_LIFE_BOOK:
32                 return "prayer";
33         case TV_MUSIC_BOOK:
34                 return "song";
35         default:
36                 return "spell";
37 #endif
38         }
39 }
40
41 /*
42  * Allow user to choose a spell/prayer from the given book.
43  *
44  * If a valid spell is chosen, saves it in '*sn' and returns TRUE
45  * If the user hits escape, returns FALSE, and set '*sn' to -1
46  * If there are no legal choices, returns FALSE, and sets '*sn' to -2
47  *
48  * The "prompt" should be "cast", "recite", or "study"
49  * The "known" should be TRUE for cast/pray, FALSE for study
50  */
51
52 bool select_the_force = FALSE;
53
54 static int get_spell(int *sn, cptr prompt, int sval, bool learned, int use_realm)
55 {
56         int         i;
57         int         spell = -1;
58         int         num = 0;
59         int         ask = TRUE;
60         int         need_mana;
61         byte        spells[64];
62         bool        flag, redraw, okay;
63         char        choice;
64         const magic_type  *s_ptr;
65         char        out_val[160];
66         cptr        p;
67 #ifdef JP
68         char jverb_buf[128];
69 #endif
70         int menu_line = (use_menu ? 1 : 0);
71
72 #ifdef ALLOW_REPEAT /* TNB */
73
74         /* Get the spell, if available */
75         if (repeat_pull(sn))
76         {
77                 /* Verify the spell */
78                 if (spell_okay(*sn, learned, FALSE, use_realm))
79                 {
80                         /* Success */
81                         return (TRUE);
82                 }
83         }
84
85 #endif /* ALLOW_REPEAT -- TNB */
86
87         p = spell_category_name(mp_ptr->spell_book);
88
89         /* Extract spells */
90         for (spell = 0; spell < 32; spell++)
91         {
92                 /* Check for this spell */
93                 if ((fake_spell_flags[sval] & (1L << spell)))
94                 {
95                         /* Collect this spell */
96                         spells[num++] = spell;
97                 }
98         }
99
100         /* Assume no usable spells */
101         okay = FALSE;
102
103         /* Assume no spells available */
104         (*sn) = -2;
105
106         /* Check for "okay" spells */
107         for (i = 0; i < num; i++)
108         {
109                 /* Look for "okay" spells */
110                 if (spell_okay(spells[i], learned, FALSE, use_realm)) okay = TRUE;
111         }
112
113         /* No "okay" spells */
114         if (!okay) return (FALSE);
115         if (((use_realm) != p_ptr->realm1) && ((use_realm) != p_ptr->realm2) && (p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE)) return FALSE;
116         if (((p_ptr->pclass == CLASS_SORCERER) || (p_ptr->pclass == CLASS_RED_MAGE)) && !is_magic(use_realm)) return FALSE;
117         if ((p_ptr->pclass == CLASS_RED_MAGE) && ((use_realm) != REALM_ARCANE) && (sval > 1)) return FALSE;
118
119         /* Assume cancelled */
120         *sn = (-1);
121
122         /* Nothing chosen yet */
123         flag = FALSE;
124
125         /* No redraw yet */
126         redraw = FALSE;
127
128         /* Show choices */
129         p_ptr->window |= (PW_SPELL);
130
131         /* Window stuff */
132         window_stuff();
133
134         /* Build a prompt (accept all spells) */
135 #ifdef JP
136         jverb1( prompt, jverb_buf );
137         (void) strnfmt(out_val, 78, "(%^s:%c-%c, '*'¤Ç°ìÍ÷, ESC¤ÇÃæÃÇ) ¤É¤Î%s¤ò%^s¤Þ¤¹¤«? ",
138                 p, I2A(0), I2A(num - 1), p, jverb_buf );
139 #else
140         (void)strnfmt(out_val, 78, "(%^ss %c-%c, *=List, ESC=exit) %^s which %s? ",
141                 p, I2A(0), I2A(num - 1), prompt, p);
142 #endif
143
144         /* Get a spell from the user */
145
146         choice = (always_show_list || use_menu) ? ESCAPE : 1;
147         while (!flag)
148         {
149                 if (choice == ESCAPE) choice = ' '; 
150                 else if (!get_com(out_val, &choice, TRUE))break;
151
152                 if (use_menu && choice != ' ')
153                 {
154                         switch (choice)
155                         {
156                                 case '0':
157                                 {
158                                         screen_load();
159                                         return FALSE;
160                                 }
161
162                                 case '8':
163                                 case 'k':
164                                 case 'K':
165                                 {
166                                         menu_line += (num - 1);
167                                         break;
168                                 }
169
170                                 case '2':
171                                 case 'j':
172                                 case 'J':
173                                 {
174                                         menu_line++;
175                                         break;
176                                 }
177
178                                 case 'x':
179                                 case 'X':
180                                 case '\r':
181                                 case '\n':
182                                 {
183                                         i = menu_line - 1;
184                                         ask = FALSE;
185                                         break;
186                                 }
187                         }
188                         if (menu_line > num) menu_line -= num;
189                         /* Display a list of spells */
190                         print_spells(menu_line, spells, num, 1, 15, use_realm);
191                         if (ask) continue;
192                 }
193                 else
194                 {
195                         /* Request redraw */
196                         if ((choice == ' ') || (choice == '*') || (choice == '?'))
197                         {
198                                 /* Show the list */
199                                 if (!redraw)
200                                 {
201                                         /* Show list */
202                                         redraw = TRUE;
203
204                                         /* Save the screen */
205                                         screen_save();
206
207                                         /* Display a list of spells */
208                                         print_spells(menu_line, spells, num, 1, 15, use_realm);
209                                 }
210
211                                 /* Hide the list */
212                                 else
213                                 {
214                                         if (use_menu) continue;
215
216                                         /* Hide list */
217                                         redraw = FALSE;
218
219                                         /* Restore the screen */
220                                         screen_load();
221                                 }
222
223                                 /* Redo asking */
224                                 continue;
225                         }
226
227
228                         /* Note verify */
229                         ask = (isupper(choice));
230
231                         /* Lowercase */
232                         if (ask) choice = tolower(choice);
233
234                         /* Extract request */
235                         i = (islower(choice) ? A2I(choice) : -1);
236                 }
237
238                 /* Totally Illegal */
239                 if ((i < 0) || (i >= num))
240                 {
241                         bell();
242                         continue;
243                 }
244
245                 /* Save the spell index */
246                 spell = spells[i];
247
248                 /* Require "okay" spells */
249                 if (!spell_okay(spell, learned, FALSE, use_realm))
250                 {
251                         bell();
252 #ifdef JP
253                         msg_format("¤½¤Î%s¤ò%s¤³¤È¤Ï¤Ç¤­¤Þ¤»¤ó¡£", p, prompt);
254 #else
255                         msg_format("You may not %s that %s.", prompt, p);
256 #endif
257
258                         continue;
259                 }
260
261                 /* Verify it */
262                 if (ask)
263                 {
264                         char tmp_val[160];
265
266                         /* Access the spell */
267                         if (!is_magic(use_realm))
268                         {
269                                 s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
270                         }
271                         else
272                         {
273                                 s_ptr = &mp_ptr->info[use_realm - 1][spell];
274                         }
275
276                         /* Extract mana consumption rate */
277                         if (use_realm == REALM_HISSATSU)
278                         {
279                                 need_mana = s_ptr->smana;
280                         }
281                         else
282                         {
283                                 need_mana = mod_need_mana(s_ptr->smana, spell, use_realm);
284                         }
285
286                         /* Prompt */
287 #ifdef JP
288                         jverb1( prompt, jverb_buf );
289                         /* ±ÑÆüÀÚ¤êÂؤ¨µ¡Ç½¤ËÂбþ */
290                         (void) strnfmt(tmp_val, 78, "%s(MP%d, ¼ºÇÔΨ%d%%)¤ò%s¤Þ¤¹¤«? ",
291                                 do_spell(use_realm, spell, SPELL_NAME), need_mana,
292                                        spell_chance(spell, use_realm),jverb_buf);
293 #else
294                         (void)strnfmt(tmp_val, 78, "%^s %s (%d mana, %d%% fail)? ",
295                                 prompt, do_spell(use_realm, spell, SPELL_NAME), need_mana,
296                                 spell_chance(spell, use_realm));
297 #endif
298
299
300                         /* Belay that order */
301                         if (!get_check(tmp_val)) continue;
302                 }
303
304                 /* Stop the loop */
305                 flag = TRUE;
306         }
307
308
309         /* Restore the screen */
310         if (redraw) screen_load();
311
312
313         /* Show choices */
314         p_ptr->window |= (PW_SPELL);
315
316         /* Window stuff */
317         window_stuff();
318
319
320         /* Abort if needed */
321         if (!flag) return FALSE;
322
323         /* Save the choice */
324         (*sn) = spell;
325
326 #ifdef ALLOW_REPEAT /* TNB */
327
328         repeat_push(*sn);
329
330 #endif /* ALLOW_REPEAT -- TNB */
331
332         /* Success */
333         return TRUE;
334 }
335
336
337 static bool item_tester_learn_spell(object_type *o_ptr)
338 {
339         s32b choices = realm_choices2[p_ptr->pclass];
340
341         if (p_ptr->pclass == CLASS_PRIEST)
342         {
343                 if (is_good_realm(p_ptr->realm1))
344                 {
345                         choices &= ~(CH_DEATH | CH_DAEMON);
346                 }
347                 else
348                 {
349                         choices &= ~(CH_LIFE | CH_CRUSADE);
350                 }
351         }
352
353         if ((o_ptr->tval < TV_LIFE_BOOK) || (o_ptr->tval > (TV_LIFE_BOOK + MAX_REALM - 1))) return (FALSE);
354         if ((o_ptr->tval == TV_MUSIC_BOOK) && (p_ptr->pclass == CLASS_BARD)) return (TRUE);
355         else if (!is_magic(tval2realm(o_ptr->tval))) return FALSE;
356         if ((REALM1_BOOK == o_ptr->tval) || (REALM2_BOOK == o_ptr->tval)) return (TRUE);
357         if (choices & (0x0001 << (tval2realm(o_ptr->tval) - 1))) return (TRUE);
358         return (FALSE);
359 }
360
361
362 static bool player_has_no_spellbooks(void)
363 {
364         int         i;
365         object_type *o_ptr;
366
367         for (i = 0; i < INVEN_PACK; i++)
368         {
369                 o_ptr = &inventory[i];
370                 if (o_ptr->k_idx && check_book_realm(o_ptr->tval, o_ptr->sval)) return FALSE;
371         }
372
373         for (i = cave[py][px].o_idx; i; i = o_ptr->next_o_idx)
374         {
375                 o_ptr = &o_list[i];
376                 if (o_ptr->k_idx && (o_ptr->marked & OM_FOUND) && check_book_realm(o_ptr->tval, o_ptr->sval)) return FALSE;
377         }
378
379         return TRUE;
380 }
381
382
383 static void confirm_use_force(bool browse_only)
384 {
385         int  item;
386         char which;
387
388 #ifdef ALLOW_REPEAT
389
390         /* Get the item index */
391         if (repeat_pull(&item) && (item == INVEN_FORCE))
392         {
393                 browse_only ? do_cmd_mind_browse() : do_cmd_mind();
394                 return;
395         }
396
397 #endif /* ALLOW_REPEAT */
398
399         /* Show the prompt */
400 #ifdef JP
401         prt("('w'Îýµ¤½Ñ, ESC) 'w'¤«ESC¤ò²¡¤·¤Æ¤¯¤À¤µ¤¤¡£ ", 0, 0);
402 #else
403         prt("(w for the Force, ESC) Hit 'w' or ESC. ", 0, 0);
404 #endif
405
406         while (1)
407         {
408                 /* Get a key */
409                 which = inkey();
410
411                 if (which == ESCAPE) break;
412                 else if (which == 'w')
413                 {
414
415 #ifdef ALLOW_REPEAT
416
417                         repeat_push(INVEN_FORCE);
418
419 #endif /* ALLOW_REPEAT */
420
421                         break;
422                 }
423         }
424
425         /* Clear the prompt line */
426         prt("", 0, 0);
427
428         if (which == 'w')
429         {
430                 browse_only ? do_cmd_mind_browse() : do_cmd_mind();
431         }
432 }
433
434
435 /*
436  * Peruse the spells/prayers in a book
437  *
438  * Note that *all* spells in the book are listed
439  *
440  * Note that browsing is allowed while confused or blind,
441  * and in the dark, primarily to allow browsing in stores.
442  */
443 void do_cmd_browse(void)
444 {
445         int             item, sval, use_realm = 0, j, line;
446         int             spell = -1;
447         int             num = 0;
448
449         byte            spells[64];
450         char            temp[62*4];
451
452         object_type     *o_ptr;
453
454         cptr q, s;
455
456         /* Warriors are illiterate */
457         if (!(p_ptr->realm1 || p_ptr->realm2) && (p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE))
458         {
459 #ifdef JP
460                 msg_print("ËܤòÆɤळ¤È¤¬¤Ç¤­¤Ê¤¤¡ª");
461 #else
462                 msg_print("You cannot read books!");
463 #endif
464
465                 return;
466         }
467
468         if (p_ptr->special_defense & KATA_MUSOU)
469         {
470                 set_action(ACTION_NONE);
471         }
472
473         if (p_ptr->pclass == CLASS_FORCETRAINER)
474         {
475                 if (player_has_no_spellbooks())
476                 {
477                         confirm_use_force(TRUE);
478                         return;
479                 }
480                 select_the_force = TRUE;
481         }
482
483         /* Restrict choices to "useful" books */
484         if (p_ptr->realm2 == REALM_NONE) item_tester_tval = mp_ptr->spell_book;
485         else item_tester_hook = item_tester_learn_spell;
486
487         /* Get an item */
488 #ifdef JP
489         q = "¤É¤ÎËܤòÆɤߤޤ¹¤«? ";
490 #else
491         q = "Browse which book? ";
492 #endif
493
494 #ifdef JP
495         s = "Æɤá¤ëËܤ¬¤Ê¤¤¡£";
496 #else
497         s = "You have no books that you can read.";
498 #endif
499
500         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR)))
501         {
502                 select_the_force = FALSE;
503                 return;
504         }
505         select_the_force = FALSE;
506
507         if (item == INVEN_FORCE) /* the_force */
508         {
509                 do_cmd_mind_browse();
510                 return;
511         }
512
513         /* Get the item (in the pack) */
514         else if (item >= 0)
515         {
516                 o_ptr = &inventory[item];
517         }
518
519         /* Get the item (on the floor) */
520         else
521         {
522                 o_ptr = &o_list[0 - item];
523         }
524
525         /* Access the item's sval */
526         sval = o_ptr->sval;
527
528         use_realm = tval2realm(o_ptr->tval);
529
530         /* Track the object kind */
531         object_kind_track(o_ptr->k_idx);
532
533         /* Hack -- Handle stuff */
534         handle_stuff();
535
536
537         /* Extract spells */
538         for (spell = 0; spell < 32; spell++)
539         {
540                 /* Check for this spell */
541                 if ((fake_spell_flags[sval] & (1L << spell)))
542                 {
543                         /* Collect this spell */
544                         spells[num++] = spell;
545                 }
546         }
547
548
549         /* Save the screen */
550         screen_save();
551
552         /* Clear the top line */
553         prt("", 0, 0);
554
555         /* Keep browsing spells.  Exit browsing on cancel. */
556         while(TRUE)
557         {
558                 /* Ask for a spell, allow cancel */
559 #ifdef JP
560                 if (!get_spell(&spell, "Æɤà", o_ptr->sval, TRUE, use_realm))
561 #else
562                 if (!get_spell(&spell, "browse", o_ptr->sval, TRUE, use_realm))
563 #endif
564                 {
565                         /* If cancelled, leave immediately. */
566                         if (spell == -1) break;
567
568                         /* Display a list of spells */
569                         print_spells(0, spells, num, 1, 15, use_realm);
570
571                         /* Notify that there's nothing to see, and wait. */
572                         if (use_realm == REALM_HISSATSU)
573 #ifdef JP
574                                 prt("Æɤá¤ëµ»¤¬¤Ê¤¤¡£", 0, 0);
575 #else
576                                 prt("No techniques to browse.", 0, 0);
577 #endif
578                         else
579 #ifdef JP
580                                 prt("Æɤá¤ë¼öʸ¤¬¤Ê¤¤¡£", 0, 0);
581 #else
582                                 prt("No spells to browse.", 0, 0);
583 #endif
584                         (void)inkey();
585
586
587                         /* Restore the screen */
588                         screen_load();
589
590                         return;
591                 }
592
593                 /* Clear lines, position cursor  (really should use strlen here) */
594                 Term_erase(14, 14, 255);
595                 Term_erase(14, 13, 255);
596                 Term_erase(14, 12, 255);
597                 Term_erase(14, 11, 255);
598
599                 roff_to_buf(do_spell(use_realm, spell, SPELL_DESC), 62, temp, sizeof(temp));
600
601                 for (j = 0, line = 11; temp[j]; j += 1 + strlen(&temp[j]))
602                 {
603                         prt(&temp[j], line, 15);
604                         line++;
605                 }
606         }
607
608         /* Restore the screen */
609         screen_load();
610 }
611
612
613 static void change_realm2(int next_realm)
614 {
615         int i, j = 0;
616         char tmp[80];
617
618         for (i = 0; i < 64; i++)
619         {
620                 p_ptr->spell_order[j] = p_ptr->spell_order[i];
621                 if (p_ptr->spell_order[i] < 32) j++;
622         }
623         for (; j < 64; j++)
624                 p_ptr->spell_order[j] = 99;
625
626         for (i = 32; i < 64; i++)
627         {
628                 p_ptr->spell_exp[i] = SPELL_EXP_UNSKILLED;
629         }
630         p_ptr->spell_learned2 = 0L;
631         p_ptr->spell_worked2 = 0L;
632         p_ptr->spell_forgotten2 = 0L;
633
634 #ifdef JP
635         sprintf(tmp,"ËâË¡¤ÎÎΰè¤ò%s¤«¤é%s¤ËÊѹ¹¤·¤¿¡£", realm_names[p_ptr->realm2], realm_names[next_realm]);
636 #else
637         sprintf(tmp,"change magic realm from %s to %s.", realm_names[p_ptr->realm2], realm_names[next_realm]);
638 #endif
639         do_cmd_write_nikki(NIKKI_BUNSHOU, 0, tmp);
640         p_ptr->old_realm |= 1 << (p_ptr->realm2-1);
641         p_ptr->realm2 = next_realm;
642
643         p_ptr->notice |= (PN_REORDER);
644         p_ptr->update |= (PU_SPELLS);
645         handle_stuff();
646
647         /* Load an autopick preference file */
648         autopick_load_pref(FALSE);
649 }
650
651
652 /*
653  * Study a book to gain a new spell/prayer
654  */
655 void do_cmd_study(void)
656 {
657         int     i, item, sval;
658         int     increment = 0;
659         bool    learned = FALSE;
660
661         /* Spells of realm2 will have an increment of +32 */
662         int     spell = -1;
663
664         cptr p = spell_category_name(mp_ptr->spell_book);
665
666         object_type *o_ptr;
667
668         cptr q, s;
669
670         if (!p_ptr->realm1)
671         {
672 #ifdef JP
673 msg_print("ËܤòÆɤळ¤È¤¬¤Ç¤­¤Ê¤¤¡ª");
674 #else
675                 msg_print("You cannot read books!");
676 #endif
677
678                 return;
679         }
680
681         if (p_ptr->blind || no_lite())
682         {
683 #ifdef JP
684 msg_print("Ìܤ¬¸«¤¨¤Ê¤¤¡ª");
685 #else
686                 msg_print("You cannot see!");
687 #endif
688
689                 return;
690         }
691
692         if (p_ptr->confused)
693         {
694 #ifdef JP
695 msg_print("º®Í𤷤Ƥ¤¤ÆÆɤá¤Ê¤¤¡ª");
696 #else
697                 msg_print("You are too confused!");
698 #endif
699
700                 return;
701         }
702
703         if (!(p_ptr->new_spells))
704         {
705 #ifdef JP
706 msg_format("¿·¤·¤¤%s¤ò³Ð¤¨¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤¡ª", p);
707 #else
708                 msg_format("You cannot learn any new %ss!", p);
709 #endif
710
711                 return;
712         }
713
714         if (p_ptr->special_defense & KATA_MUSOU)
715         {
716                 set_action(ACTION_NONE);
717         }
718
719 #ifdef JP
720         if( p_ptr->new_spells < 10 ){
721                 msg_format("¤¢¤È %d ¤Ä¤Î%s¤ò³Ø¤Ù¤ë¡£", p_ptr->new_spells, p);
722         }else{
723                 msg_format("¤¢¤È %d ¸Ä¤Î%s¤ò³Ø¤Ù¤ë¡£", p_ptr->new_spells, p);
724         }
725 #else
726         msg_format("You can learn %d new %s%s.", p_ptr->new_spells, p,
727                 (p_ptr->new_spells == 1?"":"s"));
728 #endif
729
730         msg_print(NULL);
731
732
733         /* Restrict choices to "useful" books */
734         if (p_ptr->realm2 == REALM_NONE) item_tester_tval = mp_ptr->spell_book;
735         else item_tester_hook = item_tester_learn_spell;
736
737         /* Get an item */
738 #ifdef JP
739 q = "¤É¤ÎËܤ«¤é³Ø¤Ó¤Þ¤¹¤«? ";
740 #else
741         q = "Study which book? ";
742 #endif
743
744 #ifdef JP
745 s = "Æɤá¤ëËܤ¬¤Ê¤¤¡£";
746 #else
747         s = "You have no books that you can read.";
748 #endif
749
750         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
751
752         /* Get the item (in the pack) */
753         if (item >= 0)
754         {
755                 o_ptr = &inventory[item];
756         }
757
758         /* Get the item (on the floor) */
759         else
760         {
761                 o_ptr = &o_list[0 - item];
762         }
763
764         /* Access the item's sval */
765         sval = o_ptr->sval;
766
767         if (o_ptr->tval == REALM2_BOOK) increment = 32;
768         else if (o_ptr->tval != REALM1_BOOK)
769         {
770 #ifdef JP
771                 if (!get_check("ËÜÅö¤ËËâË¡¤ÎÎΰè¤òÊѹ¹¤·¤Þ¤¹¤«¡©")) return;
772 #else
773                 if (!get_check("Really, change magic realm? ")) return;
774 #endif
775                 change_realm2(tval2realm(o_ptr->tval));
776                 increment = 32;
777         }
778
779         /* Track the object kind */
780         object_kind_track(o_ptr->k_idx);
781
782         /* Hack -- Handle stuff */
783         handle_stuff();
784
785         /* Mage -- Learn a selected spell */
786         if (mp_ptr->spell_book != TV_LIFE_BOOK)
787         {
788                 /* Ask for a spell, allow cancel */
789 #ifdef JP
790                 if (!get_spell(&spell, "³Ø¤Ö", sval, FALSE, o_ptr->tval - TV_LIFE_BOOK + 1)
791                         && (spell == -1)) return;
792 #else
793                 if (!get_spell(&spell, "study", sval, FALSE, o_ptr->tval - TV_LIFE_BOOK + 1)
794                         && (spell == -1)) return;
795 #endif
796
797         }
798
799         /* Priest -- Learn a random prayer */
800         else
801         {
802                 int k = 0;
803
804                 int gift = -1;
805
806                 /* Extract spells */
807                 for (spell = 0; spell < 32; spell++)
808                 {
809                         /* Check spells in the book */
810                         if ((fake_spell_flags[sval] & (1L << spell)))
811                         {
812                                 /* Skip non "okay" prayers */
813                                 if (!spell_okay(spell, FALSE, TRUE,
814                                         (increment ? p_ptr->realm2 : p_ptr->realm1))) continue;
815
816                                 /* Hack -- Prepare the randomizer */
817                                 k++;
818
819                                 /* Hack -- Apply the randomizer */
820                                 if (one_in_(k)) gift = spell;
821                         }
822                 }
823
824                 /* Accept gift */
825                 spell = gift;
826         }
827
828         /* Nothing to study */
829         if (spell < 0)
830         {
831                 /* Message */
832 #ifdef JP
833 msg_format("¤½¤ÎËܤˤϳؤ֤٤­%s¤¬¤Ê¤¤¡£", p);
834 #else
835                 msg_format("You cannot learn any %ss in that book.", p);
836 #endif
837
838
839                 /* Abort */
840                 return;
841         }
842
843
844         if (increment) spell += increment;
845
846         /* Learn the spell */
847         if (spell < 32)
848         {
849                 if (p_ptr->spell_learned1 & (1L << spell)) learned = TRUE;
850                 else p_ptr->spell_learned1 |= (1L << spell);
851         }
852         else
853         {
854                 if (p_ptr->spell_learned2 & (1L << (spell - 32))) learned = TRUE;
855                 else p_ptr->spell_learned2 |= (1L << (spell - 32));
856         }
857
858         if (learned)
859         {
860                 int max_exp = (spell < 32) ? SPELL_EXP_MASTER : SPELL_EXP_EXPERT;
861                 int old_exp = p_ptr->spell_exp[spell];
862                 int new_rank = EXP_LEVEL_UNSKILLED;
863                 cptr name = do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell%32, SPELL_NAME);
864
865                 if (old_exp >= max_exp)
866                 {
867 #ifdef JP
868                         msg_format("¤½¤Î%s¤Ï´°Á´¤Ë»È¤¤¤³¤Ê¤»¤ë¤Î¤Ç³Ø¤ÖɬÍפϤʤ¤¡£", p);
869 #else
870                         msg_format("You don't need to study this %s anymore.", p);
871 #endif
872                         return;
873                 }
874 #ifdef JP
875                 if (!get_check(format("%s¤Î%s¤ò¤µ¤é¤Ë³Ø¤Ó¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡©", name, p)))
876 #else
877                 if (!get_check(format("You will study a %s of %s again. Are you sure? ", p, name)))
878 #endif
879                 {
880                         return;
881                 }
882                 else if (old_exp >= SPELL_EXP_EXPERT)
883                 {
884                         p_ptr->spell_exp[spell] = SPELL_EXP_MASTER;
885                         new_rank = EXP_LEVEL_MASTER;
886                 }
887                 else if (old_exp >= SPELL_EXP_SKILLED)
888                 {
889                         if (spell >= 32) p_ptr->spell_exp[spell] = SPELL_EXP_EXPERT;
890                         else p_ptr->spell_exp[spell] += SPELL_EXP_EXPERT - SPELL_EXP_SKILLED;
891                         new_rank = EXP_LEVEL_EXPERT;
892                 }
893                 else if (old_exp >= SPELL_EXP_BEGINNER)
894                 {
895                         p_ptr->spell_exp[spell] = SPELL_EXP_SKILLED + (old_exp - SPELL_EXP_BEGINNER) * 2 / 3;
896                         new_rank = EXP_LEVEL_SKILLED;
897                 }
898                 else
899                 {
900                         p_ptr->spell_exp[spell] = SPELL_EXP_BEGINNER + old_exp / 3;
901                         new_rank = EXP_LEVEL_BEGINNER;
902                 }
903 #ifdef JP
904                 msg_format("%s¤Î½ÏÎýÅÙ¤¬%s¤Ë¾å¤¬¤Ã¤¿¡£", name, exp_level_str[new_rank]);
905 #else
906                 msg_format("Your proficiency of %s is now %s rank.", name, exp_level_str[new_rank]);
907 #endif
908         }
909         else
910         {
911                 /* Find the next open entry in "p_ptr->spell_order[]" */
912                 for (i = 0; i < 64; i++)
913                 {
914                         /* Stop at the first empty space */
915                         if (p_ptr->spell_order[i] == 99) break;
916                 }
917
918                 /* Add the spell to the known list */
919                 p_ptr->spell_order[i++] = spell;
920
921                 /* Mention the result */
922 #ifdef JP
923                 /* ±ÑÆüÀÚ¤êÂؤ¨µ¡Ç½¤ËÂбþ */
924                 if (mp_ptr->spell_book == TV_MUSIC_BOOK)
925                 {
926                         msg_format("%s¤ò³Ø¤ó¤À¡£",
927                                     do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell % 32, SPELL_NAME));
928                 }
929                 else
930                 {
931                         msg_format("%s¤Î%s¤ò³Ø¤ó¤À¡£",
932                                     do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell % 32, SPELL_NAME) ,p);
933                 }
934 #else
935                 msg_format("You have learned the %s of %s.",
936                         p, do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell % 32, SPELL_NAME));
937 #endif
938         }
939
940         /* Take a turn */
941         energy_use = 100;
942
943         switch (mp_ptr->spell_book)
944         {
945         case TV_LIFE_BOOK:
946                 chg_virtue(V_FAITH, 1);
947                 break;
948         case TV_DEATH_BOOK:
949                 chg_virtue(V_UNLIFE, 1);
950                 break;
951         case TV_NATURE_BOOK:
952                 chg_virtue(V_NATURE, 1);
953                 break;
954         default:
955                 chg_virtue(V_KNOWLEDGE, 1);
956                 break;
957         }
958
959         /* Sound */
960         sound(SOUND_STUDY);
961
962         /* One less spell available */
963         p_ptr->learned_spells++;
964 #if 0
965         /* Message if needed */
966         if (p_ptr->new_spells)
967         {
968                 /* Message */
969 #ifdef JP
970                 if (p_ptr->new_spells < 10) msg_format("¤¢¤È %d ¤Ä¤Î%s¤ò³Ø¤Ù¤ë¡£", p_ptr->new_spells, p);
971                 else msg_format("¤¢¤È %d ¸Ä¤Î%s¤ò³Ø¤Ù¤ë¡£", p_ptr->new_spells, p);
972 #else
973                 msg_format("You can learn %d more %s%s.", p_ptr->new_spells, p,
974                            (p_ptr->new_spells != 1) ? "s" : "");
975 #endif
976         }
977 #endif
978
979         /* Update Study */
980         p_ptr->update |= (PU_SPELLS);
981         update_stuff();
982
983         /* Redraw object recall */
984         p_ptr->window |= (PW_OBJECT);
985 }
986
987
988 static void wild_magic(int spell)
989 {
990         int counter = 0;
991         int type = SUMMON_BIZARRE1 + randint0(6);
992
993         if (type < SUMMON_BIZARRE1) type = SUMMON_BIZARRE1;
994         else if (type > SUMMON_BIZARRE6) type = SUMMON_BIZARRE6;
995
996         switch (randint1(spell) + randint1(8) + 1)
997         {
998         case 1:
999         case 2:
1000         case 3:
1001                 teleport_player(10, TELEPORT_PASSIVE);
1002                 break;
1003         case 4:
1004         case 5:
1005         case 6:
1006                 teleport_player(100, TELEPORT_PASSIVE);
1007                 break;
1008         case 7:
1009         case 8:
1010                 teleport_player(200, TELEPORT_PASSIVE);
1011                 break;
1012         case 9:
1013         case 10:
1014         case 11:
1015                 unlite_area(10, 3);
1016                 break;
1017         case 12:
1018         case 13:
1019         case 14:
1020                 lite_area(damroll(2, 3), 2);
1021                 break;
1022         case 15:
1023                 destroy_doors_touch();
1024                 break;
1025         case 16: case 17:
1026                 wall_breaker();
1027         case 18:
1028                 sleep_monsters_touch();
1029                 break;
1030         case 19:
1031         case 20:
1032                 trap_creation(py, px);
1033                 break;
1034         case 21:
1035         case 22:
1036                 door_creation();
1037                 break;
1038         case 23:
1039         case 24:
1040         case 25:
1041                 aggravate_monsters(0);
1042                 break;
1043         case 26:
1044                 earthquake(py, px, 5);
1045                 break;
1046         case 27:
1047         case 28:
1048                 (void)gain_random_mutation(0);
1049                 break;
1050         case 29:
1051         case 30:
1052                 apply_disenchant(1);
1053                 break;
1054         case 31:
1055                 lose_all_info();
1056                 break;
1057         case 32:
1058                 fire_ball(GF_CHAOS, 0, spell + 5, 1 + (spell / 10));
1059                 break;
1060         case 33:
1061                 wall_stone();
1062                 break;
1063         case 34:
1064         case 35:
1065                 while (counter++ < 8)
1066                 {
1067                         (void)summon_specific(0, py, px, (dun_level * 3) / 2, type, (PM_ALLOW_GROUP | PM_NO_PET));
1068                 }
1069                 break;
1070         case 36:
1071         case 37:
1072                 activate_hi_summon(py, px, FALSE);
1073                 break;
1074         case 38:
1075                 (void)summon_cyber(-1, py, px);
1076                 break;
1077         default:
1078                 {
1079                         int count = 0;
1080                         (void)activate_ty_curse(FALSE, &count);
1081                         break;
1082                 }
1083         }
1084
1085         return;
1086 }
1087
1088
1089 /*
1090  * Cast a spell
1091  */
1092 void do_cmd_cast(void)
1093 {
1094         int     item, sval, spell, realm;
1095         int     chance;
1096         int     increment = 0;
1097         int     use_realm;
1098         int     need_mana;
1099
1100         cptr prayer;
1101
1102         object_type     *o_ptr;
1103
1104         const magic_type *s_ptr;
1105
1106         cptr q, s;
1107
1108         bool over_exerted = FALSE;
1109
1110         /* Require spell ability */
1111         if (!p_ptr->realm1 && (p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE))
1112         {
1113 #ifdef JP
1114                 msg_print("¼öʸ¤ò¾§¤¨¤é¤ì¤Ê¤¤¡ª");
1115 #else
1116                 msg_print("You cannot cast spells!");
1117 #endif
1118
1119                 return;
1120         }
1121
1122         /* Require lite */
1123         if (p_ptr->blind || no_lite())
1124         {
1125                 if (p_ptr->pclass == CLASS_FORCETRAINER) confirm_use_force(FALSE);
1126                 else
1127                 {
1128 #ifdef JP
1129                         msg_print("Ìܤ¬¸«¤¨¤Ê¤¤¡ª");
1130 #else
1131                         msg_print("You cannot see!");
1132 #endif
1133                         flush();
1134                 }
1135                 return;
1136         }
1137
1138         /* Not when confused */
1139         if (p_ptr->confused)
1140         {
1141 #ifdef JP
1142                 msg_print("º®Í𤷤Ƥ¤¤Æ¾§¤¨¤é¤ì¤Ê¤¤¡ª");
1143 #else
1144                 msg_print("You are too confused!");
1145 #endif
1146                 flush();
1147                 return;
1148         }
1149
1150         /* Hex */
1151         if (p_ptr->realm1 == REALM_HEX)
1152         {
1153                 if (hex_spell_fully())
1154                 {
1155                         bool flag = FALSE;
1156 #ifdef JP
1157                         msg_print("¤³¤ì°Ê¾å¿·¤·¤¤¼öʸ¤ò±Ó¾§¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤¡£");
1158 #else
1159                         msg_print("Can not spell new spells more.");
1160 #endif
1161                         flush();
1162                         if (p_ptr->lev >= 35) flag = stop_hex_spell();
1163                         if (!flag) return;
1164                 }
1165         }
1166
1167         if (p_ptr->pclass == CLASS_FORCETRAINER)
1168         {
1169                 if (player_has_no_spellbooks())
1170                 {
1171                         confirm_use_force(FALSE);
1172                         return;
1173                 }
1174                 select_the_force = TRUE;
1175         }
1176
1177         prayer = spell_category_name(mp_ptr->spell_book);
1178
1179         /* Restrict choices to spell books */
1180         item_tester_tval = mp_ptr->spell_book;
1181
1182         /* Get an item */
1183 #ifdef JP
1184         q = "¤É¤Î¼öʸ½ñ¤ò»È¤¤¤Þ¤¹¤«? ";
1185 #else
1186         q = "Use which book? ";
1187 #endif
1188
1189 #ifdef JP
1190         s = "¼öʸ½ñ¤¬¤Ê¤¤¡ª";
1191 #else
1192         s = "You have no spell books!";
1193 #endif
1194
1195         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR)))
1196         {
1197                 select_the_force = FALSE;
1198                 return;
1199         }
1200         select_the_force = FALSE;
1201
1202         if (item == INVEN_FORCE) /* the_force */
1203         {
1204                 do_cmd_mind();
1205                 return;
1206         }
1207
1208         /* Get the item (in the pack) */
1209         else if (item >= 0)
1210         {
1211                 o_ptr = &inventory[item];
1212         }
1213
1214         /* Get the item (on the floor) */
1215         else
1216         {
1217                 o_ptr = &o_list[0 - item];
1218         }
1219
1220         /* Access the item's sval */
1221         sval = o_ptr->sval;
1222
1223         if ((p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE) && (o_ptr->tval == REALM2_BOOK)) increment = 32;
1224
1225
1226         /* Track the object kind */
1227         object_kind_track(o_ptr->k_idx);
1228
1229         /* Hack -- Handle stuff */
1230         handle_stuff();
1231
1232         if ((p_ptr->pclass == CLASS_SORCERER) || (p_ptr->pclass == CLASS_RED_MAGE))
1233                 realm = o_ptr->tval - TV_LIFE_BOOK + 1;
1234         else if (increment) realm = p_ptr->realm2;
1235         else realm = p_ptr->realm1;
1236
1237         /* Ask for a spell */
1238 #ifdef JP
1239         if (!get_spell(&spell,  
1240                                 ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "±Ó¾§¤¹¤ë" : (mp_ptr->spell_book == TV_MUSIC_BOOK) ? "²Î¤¦" : "¾§¤¨¤ë"), 
1241                        sval, TRUE, realm))
1242         {
1243                 if (spell == -2) msg_format("¤½¤ÎËܤˤÏÃΤäƤ¤¤ë%s¤¬¤Ê¤¤¡£", prayer);
1244                 return;
1245         }
1246 #else
1247         if (!get_spell(&spell, ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "recite" : "cast"),
1248                 sval, TRUE, realm))
1249         {
1250                 if (spell == -2)
1251                         msg_format("You don't know any %ss in that book.", prayer);
1252                 return;
1253         }
1254 #endif
1255
1256
1257         use_realm = tval2realm(o_ptr->tval);
1258
1259         /* Hex */
1260         if (use_realm == REALM_HEX)
1261         {
1262                 if (hex_spelling(spell))
1263                 {
1264 #ifdef JP
1265                         msg_print("¤½¤Î¼öʸ¤Ï¤¹¤Ç¤Ë±Ó¾§Ãæ¤À¡£");
1266 #else
1267                         msg_print("You are already casting it.");
1268 #endif
1269                         return;
1270                 }
1271         }
1272
1273         if (!is_magic(use_realm))
1274         {
1275                 s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
1276         }
1277         else
1278         {
1279                 s_ptr = &mp_ptr->info[realm - 1][spell];
1280         }
1281
1282         /* Extract mana consumption rate */
1283         need_mana = mod_need_mana(s_ptr->smana, spell, realm);
1284
1285         /* Verify "dangerous" spells */
1286         if (need_mana > p_ptr->csp)
1287         {
1288                 if (flush_failure) flush();
1289
1290                 /* Warning */
1291 #ifdef JP
1292 msg_format("¤½¤Î%s¤ò%s¤Î¤Ë½½Ê¬¤Ê¥Þ¥¸¥Ã¥¯¥Ý¥¤¥ó¥È¤¬¤Ê¤¤¡£",prayer,
1293  ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "±Ó¾§¤¹¤ë" : (mp_ptr->spell_book == TV_LIFE_BOOK) ? "²Î¤¦" : "¾§¤¨¤ë"));
1294 #else
1295                 msg_format("You do not have enough mana to %s this %s.",
1296                         ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "recite" : "cast"),
1297                         prayer);
1298 #endif
1299
1300
1301                 if (!over_exert) return;
1302
1303                 /* Verify */
1304 #ifdef JP
1305                 if (!get_check_strict("¤½¤ì¤Ç¤âÄ©À路¤Þ¤¹¤«? ", CHECK_OKAY_CANCEL)) return;
1306 #else
1307                 if (!get_check_strict("Attempt it anyway? ", CHECK_OKAY_CANCEL)) return;
1308 #endif
1309
1310         }
1311
1312         /* Spell failure chance */
1313         chance = spell_chance(spell, use_realm);
1314
1315         /* Sufficient mana */
1316         if (need_mana <= p_ptr->csp)
1317         {
1318                 /* Use some mana */
1319                 p_ptr->csp -= need_mana;
1320         }
1321         else over_exerted = TRUE;
1322
1323         /* Failed spell */
1324         if (randint0(100) < chance)
1325         {
1326                 if (flush_failure) flush();
1327
1328 #ifdef JP
1329 msg_format("%s¤ò¤¦¤Þ¤¯¾§¤¨¤é¤ì¤Ê¤«¤Ã¤¿¡ª", prayer);
1330 #else
1331                 msg_format("You failed to get the %s off!", prayer);
1332 #endif
1333
1334                 sound(SOUND_FAIL);
1335
1336                 switch (realm)
1337                 {
1338                 case REALM_LIFE:
1339                         if (randint1(100) < chance) chg_virtue(V_VITALITY, -1);
1340                         break;
1341                 case REALM_DEATH:
1342                         if (randint1(100) < chance) chg_virtue(V_UNLIFE, -1);
1343                         break;
1344                 case REALM_NATURE:
1345                         if (randint1(100) < chance) chg_virtue(V_NATURE, -1);
1346                         break;
1347                 case REALM_DAEMON:
1348                         if (randint1(100) < chance) chg_virtue(V_JUSTICE, 1);
1349                         break;
1350                 case REALM_CRUSADE:
1351                         if (randint1(100) < chance) chg_virtue(V_JUSTICE, -1);
1352                         break;
1353                 case REALM_HEX:
1354                         if (randint1(100) < chance) chg_virtue(V_COMPASSION, -1);
1355                         break;
1356                 default:
1357                         if (randint1(100) < chance) chg_virtue(V_KNOWLEDGE, -1);
1358                         break;
1359                 }
1360
1361                 /* Failure casting may activate some side effect */
1362                 do_spell(realm, spell, SPELL_FAIL);
1363
1364
1365                 if ((o_ptr->tval == TV_CHAOS_BOOK) && (randint1(100) < spell))
1366                 {
1367 #ifdef JP
1368 msg_print("¥«¥ª¥¹Åª¤Ê¸ú²Ì¤òȯÀ¸¤·¤¿¡ª");
1369 #else
1370                         msg_print("You produce a chaotic effect!");
1371 #endif
1372
1373                         wild_magic(spell);
1374                 }
1375                 else if ((o_ptr->tval == TV_DEATH_BOOK) && (randint1(100) < spell))
1376                 {
1377                         if ((sval == 3) && one_in_(2))
1378                         {
1379                                 sanity_blast(0, TRUE);
1380                         }
1381                         else
1382                         {
1383 #ifdef JP
1384                                 msg_print("Äˤ¤¡ª");
1385 #else
1386                                 msg_print("It hurts!");
1387 #endif
1388
1389 #ifdef JP
1390                                 take_hit(DAMAGE_LOSELIFE, damroll(o_ptr->sval + 1, 6), "°Å¹õËâË¡¤ÎµÕή", -1);
1391 #else
1392                                 take_hit(DAMAGE_LOSELIFE, damroll(o_ptr->sval + 1, 6), "a miscast Death spell", -1);
1393 #endif
1394
1395                                 if ((spell > 15) && one_in_(6) && !p_ptr->hold_life)
1396                                         lose_exp(spell * 250);
1397                         }
1398                 }
1399                 else if ((o_ptr->tval == TV_MUSIC_BOOK) && (randint1(200) < spell))
1400                 {
1401 #ifdef JP
1402 msg_print("¤¤¤ä¤Ê²»¤¬¶Á¤¤¤¿");
1403 #else
1404 msg_print("An infernal sound echoed.");
1405 #endif
1406
1407                         aggravate_monsters(0);
1408                 }
1409                 if (randint1(100) >= chance)
1410                         chg_virtue(V_CHANCE,-1);
1411         }
1412
1413         /* Process spell */
1414         else
1415         {
1416                 /* Canceled spells cost neither a turn nor mana */
1417                 if (!do_spell(realm, spell, SPELL_CAST)) return;
1418
1419                 if (randint1(100) < chance)
1420                         chg_virtue(V_CHANCE,1);
1421
1422                 /* A spell was cast */
1423                 if (!(increment ?
1424                     (p_ptr->spell_worked2 & (1L << spell)) :
1425                     (p_ptr->spell_worked1 & (1L << spell)))
1426                     && (p_ptr->pclass != CLASS_SORCERER)
1427                     && (p_ptr->pclass != CLASS_RED_MAGE))
1428                 {
1429                         int e = s_ptr->sexp;
1430
1431                         /* The spell worked */
1432                         if (realm == p_ptr->realm1)
1433                         {
1434                                 p_ptr->spell_worked1 |= (1L << spell);
1435                         }
1436                         else
1437                         {
1438                                 p_ptr->spell_worked2 |= (1L << spell);
1439                         }
1440
1441                         /* Gain experience */
1442                         gain_exp(e * s_ptr->slevel);
1443
1444                         /* Redraw object recall */
1445                         p_ptr->window |= (PW_OBJECT);
1446
1447                         switch (realm)
1448                         {
1449                         case REALM_LIFE:
1450                                 chg_virtue(V_TEMPERANCE, 1);
1451                                 chg_virtue(V_COMPASSION, 1);
1452                                 chg_virtue(V_VITALITY, 1);
1453                                 chg_virtue(V_DILIGENCE, 1);
1454                                 break;
1455                         case REALM_DEATH:
1456                                 chg_virtue(V_UNLIFE, 1);
1457                                 chg_virtue(V_JUSTICE, -1);
1458                                 chg_virtue(V_FAITH, -1);
1459                                 chg_virtue(V_VITALITY, -1);
1460                                 break;
1461                         case REALM_DAEMON:
1462                                 chg_virtue(V_JUSTICE, -1);
1463                                 chg_virtue(V_FAITH, -1);
1464                                 chg_virtue(V_HONOUR, -1);
1465                                 chg_virtue(V_TEMPERANCE, -1);
1466                                 break;
1467                         case REALM_CRUSADE:
1468                                 chg_virtue(V_FAITH, 1);
1469                                 chg_virtue(V_JUSTICE, 1);
1470                                 chg_virtue(V_SACRIFICE, 1);
1471                                 chg_virtue(V_HONOUR, 1);
1472                                 break;
1473                         case REALM_NATURE:
1474                                 chg_virtue(V_NATURE, 1);
1475                                 chg_virtue(V_HARMONY, 1);
1476                                 break;
1477                         case REALM_HEX:
1478                                 chg_virtue(V_JUSTICE, -1);
1479                                 chg_virtue(V_FAITH, -1);
1480                                 chg_virtue(V_HONOUR, -1);
1481                                 chg_virtue(V_COMPASSION, -1);
1482                                 break;
1483                         default:
1484                                 chg_virtue(V_KNOWLEDGE, 1);
1485                                 break;
1486                         }
1487                 }
1488                 switch (realm)
1489                 {
1490                 case REALM_LIFE:
1491                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_TEMPERANCE, 1);
1492                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_COMPASSION, 1);
1493                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_VITALITY, 1);
1494                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_DILIGENCE, 1);
1495                         break;
1496                 case REALM_DEATH:
1497                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_UNLIFE, 1);
1498                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, -1);
1499                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, -1);
1500                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_VITALITY, -1);
1501                         break;
1502                 case REALM_DAEMON:
1503                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, -1);
1504                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, -1);
1505                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HONOUR, -1);
1506                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_TEMPERANCE, -1);
1507                         break;
1508                 case REALM_CRUSADE:
1509                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, 1);
1510                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, 1);
1511                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_SACRIFICE, 1);
1512                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HONOUR, 1);
1513                         break;
1514                 case REALM_NATURE:
1515                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_NATURE, 1);
1516                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HARMONY, 1);
1517                         break;
1518                 case REALM_HEX:
1519                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, -1);
1520                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, -1);
1521                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HONOUR, -1);
1522                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_COMPASSION, -1);
1523                         break;
1524                 }
1525                 if (mp_ptr->spell_xtra & MAGIC_GAIN_EXP)
1526                 {
1527                         s16b cur_exp = p_ptr->spell_exp[(increment ? 32 : 0)+spell];
1528                         s16b exp_gain = 0;
1529
1530                         if (cur_exp < SPELL_EXP_BEGINNER)
1531                                 exp_gain += 60;
1532                         else if (cur_exp < SPELL_EXP_SKILLED)
1533                         {
1534                                 if ((dun_level > 4) && ((dun_level + 10) > p_ptr->lev))
1535                                         exp_gain = 8;
1536                         }
1537                         else if (cur_exp < SPELL_EXP_EXPERT)
1538                         {
1539                                 if (((dun_level + 5) > p_ptr->lev) && ((dun_level + 5) > s_ptr->slevel))
1540                                         exp_gain = 2;
1541                         }
1542                         else if ((cur_exp < SPELL_EXP_MASTER) && !increment)
1543                         {
1544                                 if (((dun_level + 5) > p_ptr->lev) && (dun_level > s_ptr->slevel))
1545                                         exp_gain = 1;
1546                         }
1547                         p_ptr->spell_exp[(increment ? 32 : 0) + spell] += exp_gain;
1548                 }
1549         }
1550
1551         /* Take a turn */
1552         energy_use = 100;
1553
1554
1555         /* Over-exert the player */
1556         if(over_exerted)
1557         {
1558                 int oops = need_mana;
1559
1560                 /* No mana left */
1561                 p_ptr->csp = 0;
1562                 p_ptr->csp_frac = 0;
1563
1564                 /* Message */
1565 #ifdef JP
1566 msg_print("Àº¿À¤ò½¸Ã椷¤¹¤®¤Æµ¤¤ò¼º¤Ã¤Æ¤·¤Þ¤Ã¤¿¡ª");
1567 #else
1568                 msg_print("You faint from the effort!");
1569 #endif
1570
1571
1572                 /* Hack -- Bypass free action */
1573                 (void)set_paralyzed(p_ptr->paralyzed + randint1(5 * oops + 1));
1574
1575                 switch (realm)
1576                 {
1577                 case REALM_LIFE:
1578                         chg_virtue(V_VITALITY, -10);
1579                         break;
1580                 case REALM_DEATH:
1581                         chg_virtue(V_UNLIFE, -10);
1582                         break;
1583                 case REALM_DAEMON:
1584                         chg_virtue(V_JUSTICE, 10);
1585                         break;
1586                 case REALM_NATURE:
1587                         chg_virtue(V_NATURE, -10);
1588                         break;
1589                 case REALM_CRUSADE:
1590                         chg_virtue(V_JUSTICE, -10);
1591                         break;
1592                 case REALM_HEX:
1593                         chg_virtue(V_COMPASSION, 10);
1594                         break;
1595                 default:
1596                         chg_virtue(V_KNOWLEDGE, -10);
1597                         break;
1598                 }
1599
1600                 /* Damage CON (possibly permanently) */
1601                 if (randint0(100) < 50)
1602                 {
1603                         bool perm = (randint0(100) < 25);
1604
1605                         /* Message */
1606 #ifdef JP
1607 msg_print("ÂΤò°­¤¯¤·¤Æ¤·¤Þ¤Ã¤¿¡ª");
1608 #else
1609                         msg_print("You have damaged your health!");
1610 #endif
1611
1612
1613                         /* Reduce constitution */
1614                         (void)dec_stat(A_CON, 15 + randint1(10), perm);
1615                 }
1616         }
1617
1618         /* Redraw mana */
1619         p_ptr->redraw |= (PR_MANA);
1620
1621         /* Window stuff */
1622         p_ptr->window |= (PW_PLAYER);
1623         p_ptr->window |= (PW_SPELL);
1624 }
1625
1626
1627 static bool ang_sort_comp_pet_dismiss(vptr u, vptr v, int a, int b)
1628 {
1629         u16b *who = (u16b*)(u);
1630
1631         int w1 = who[a];
1632         int w2 = who[b];
1633
1634         monster_type *m_ptr1 = &m_list[w1];
1635         monster_type *m_ptr2 = &m_list[w2];
1636         monster_race *r_ptr1 = &r_info[m_ptr1->r_idx];
1637         monster_race *r_ptr2 = &r_info[m_ptr2->r_idx];
1638
1639         /* Unused */
1640         (void)v;
1641
1642         if (w1 == p_ptr->riding) return TRUE;
1643         if (w2 == p_ptr->riding) return FALSE;
1644
1645         if (m_ptr1->nickname && !m_ptr2->nickname) return TRUE;
1646         if (m_ptr2->nickname && !m_ptr1->nickname) return FALSE;
1647
1648         if (!m_ptr1->parent_m_idx && m_ptr2->parent_m_idx) return TRUE;
1649         if (!m_ptr2->parent_m_idx && m_ptr1->parent_m_idx) return FALSE;
1650
1651         if ((r_ptr1->flags1 & RF1_UNIQUE) && !(r_ptr2->flags1 & RF1_UNIQUE)) return TRUE;
1652         if ((r_ptr2->flags1 & RF1_UNIQUE) && !(r_ptr1->flags1 & RF1_UNIQUE)) return FALSE;
1653
1654         if (r_ptr1->level > r_ptr2->level) return TRUE;
1655         if (r_ptr2->level > r_ptr1->level) return FALSE;
1656
1657         if (m_ptr1->hp > m_ptr2->hp) return TRUE;
1658         if (m_ptr2->hp > m_ptr1->hp) return FALSE;
1659         
1660         return w1 <= w2;
1661 }
1662
1663 void check_pets_num_and_align(monster_type *m_ptr, bool inc)
1664 {
1665         s32b old_friend_align = friend_align;
1666         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1667
1668         if (inc)
1669         {
1670                 total_friends++;
1671                 if (r_ptr->flags3 & RF3_GOOD) friend_align += r_ptr->level;
1672                 if (r_ptr->flags3 & RF3_EVIL) friend_align -= r_ptr->level;
1673         }
1674         else
1675         {
1676                 total_friends--;
1677                 if (r_ptr->flags3 & RF3_GOOD) friend_align -= r_ptr->level;
1678                 if (r_ptr->flags3 & RF3_EVIL) friend_align += r_ptr->level;
1679         }
1680
1681         if (old_friend_align != friend_align) p_ptr->update |= (PU_BONUS);
1682 }
1683
1684 int calculate_upkeep(void)
1685 {
1686         s32b old_friend_align = friend_align;
1687         int m_idx;
1688         bool have_a_unique = FALSE;
1689         s32b total_friend_levels = 0;
1690
1691         total_friends = 0;
1692         friend_align = 0;
1693
1694         for (m_idx = m_max - 1; m_idx >=1; m_idx--)
1695         {
1696                 monster_type *m_ptr;
1697                 monster_race *r_ptr;
1698                 
1699                 m_ptr = &m_list[m_idx];
1700                 if (!m_ptr->r_idx) continue;
1701                 r_ptr = &r_info[m_ptr->r_idx];
1702
1703                 if (is_pet(m_ptr))
1704                 {
1705                         total_friends++;
1706                         if (r_ptr->flags1 & RF1_UNIQUE)
1707                         {
1708                                 if (p_ptr->pclass == CLASS_CAVALRY)
1709                                 {
1710                                         if (p_ptr->riding == m_idx)
1711                                                 total_friend_levels += (r_ptr->level+5)*2;
1712                                         else if (!have_a_unique && (r_info[m_ptr->r_idx].flags7 & RF7_RIDING))
1713                                                 total_friend_levels += (r_ptr->level+5)*7/2;
1714                                         else
1715                                                 total_friend_levels += (r_ptr->level+5)*10;
1716                                         have_a_unique = TRUE;
1717                                 }
1718                                 else
1719                                         total_friend_levels += (r_ptr->level+5)*10;
1720                         }
1721                         else
1722                                 total_friend_levels += r_ptr->level;
1723
1724                         /* Determine pet alignment */
1725                         if (r_ptr->flags3 & RF3_GOOD) friend_align += r_ptr->level;
1726                         if (r_ptr->flags3 & RF3_EVIL) friend_align -= r_ptr->level;
1727                 }
1728         }
1729         if (old_friend_align != friend_align) p_ptr->update |= (PU_BONUS);
1730         if (total_friends)
1731         {
1732                 int upkeep_factor;
1733                 upkeep_factor = (total_friend_levels - (p_ptr->lev * 80 / (cp_ptr->pet_upkeep_div)));
1734                 if (upkeep_factor < 0) upkeep_factor = 0;
1735                 if (upkeep_factor > 1000) upkeep_factor = 1000;
1736                 return upkeep_factor;
1737         }
1738         else
1739                 return 0;
1740 }
1741
1742 void do_cmd_pet_dismiss(void)
1743 {
1744         monster_type    *m_ptr;
1745         bool            all_pets = FALSE;
1746         int pet_ctr, i;
1747         int Dismissed = 0;
1748
1749         u16b *who;
1750         u16b dummy_why;
1751         int max_pet = 0;
1752         int cu, cv;
1753
1754         cu = Term->scr->cu;
1755         cv = Term->scr->cv;
1756         Term->scr->cu = 0;
1757         Term->scr->cv = 1;
1758
1759         /* Allocate the "who" array */
1760         C_MAKE(who, max_m_idx, u16b);
1761
1762         /* Process the monsters (backwards) */
1763         for (pet_ctr = m_max - 1; pet_ctr >= 1; pet_ctr--)
1764         {
1765                 if (is_pet(&m_list[pet_ctr]))
1766                         who[max_pet++] = pet_ctr;
1767         }
1768
1769         /* Select the sort method */
1770         ang_sort_comp = ang_sort_comp_pet_dismiss;
1771         ang_sort_swap = ang_sort_swap_hook;
1772
1773         ang_sort(who, &dummy_why, max_pet);
1774
1775         /* Process the monsters (backwards) */
1776         for (i = 0; i < max_pet; i++)
1777         {
1778                 bool delete_this;
1779                 char friend_name[80];
1780                 char buf[80];
1781                 bool kakunin;
1782
1783                 /* Access the monster */
1784                 pet_ctr = who[i];
1785                 m_ptr = &m_list[pet_ctr];
1786
1787                 delete_this = FALSE;
1788                 kakunin = ((pet_ctr == p_ptr->riding) || (m_ptr->nickname));
1789                 monster_desc(friend_name, m_ptr, MD_ASSUME_VISIBLE);
1790
1791                 if (!all_pets)
1792                 {
1793                         /* Hack -- health bar for this monster */
1794                         health_track(pet_ctr);
1795
1796                         /* Hack -- handle stuff */
1797                         handle_stuff();
1798
1799 #ifdef JP
1800                         sprintf(buf, "%s¤òÊü¤·¤Þ¤¹¤«¡© [Yes/No/Unnamed (%dÂÎ)]", friend_name, max_pet - i);
1801 #else
1802                         sprintf(buf, "Dismiss %s? [Yes/No/Unnamed (%d remain)]", friend_name, max_pet - i);
1803 #endif
1804                         prt(buf, 0, 0);
1805
1806                         if (m_ptr->ml)
1807                                 move_cursor_relative(m_ptr->fy, m_ptr->fx);
1808
1809                         while (TRUE)
1810                         {
1811                                 char ch = inkey();
1812
1813                                 if (ch == 'Y' || ch == 'y')
1814                                 {
1815                                         delete_this = TRUE;
1816
1817                                         if (kakunin)
1818                                         {
1819 #ifdef JP
1820                                                 sprintf(buf, "ËÜÅö¤Ë¤è¤í¤·¤¤¤Ç¤¹¤«¡© (%s) ", friend_name);
1821 #else
1822                                                 sprintf(buf, "Are you sure? (%s) ", friend_name);
1823 #endif
1824                                                 if (!get_check(buf))
1825                                                         delete_this = FALSE;
1826                                         }
1827                                         break;
1828                                 }
1829
1830                                 if (ch == 'U' || ch == 'u')
1831                                 {
1832                                         all_pets = TRUE;
1833                                         break;
1834                                 }
1835
1836                                 if (ch == ESCAPE || ch == 'N' || ch == 'n')
1837                                         break;
1838
1839                                 bell();
1840                         }
1841                 }
1842
1843                 if ((all_pets && !kakunin) || (!all_pets && delete_this))
1844                 {
1845                         if (record_named_pet && m_ptr->nickname)
1846                         {
1847                                 char m_name[80];
1848
1849                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1850                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_DISMISS, m_name);
1851                         }
1852
1853                         if (pet_ctr == p_ptr->riding)
1854                         {
1855 #ifdef JP
1856                                 msg_format("%s¤«¤é¹ß¤ê¤¿¡£", friend_name);
1857 #else
1858                                 msg_format("You have got off %s. ", friend_name);
1859 #endif
1860
1861                                 p_ptr->riding = 0;
1862
1863                                 /* Update the monsters */
1864                                 p_ptr->update |= (PU_BONUS | PU_MONSTERS);
1865                                 p_ptr->redraw |= (PR_EXTRA | PR_UHEALTH);
1866                         }
1867
1868                         /* HACK : Add the line to message buffer */
1869 #ifdef JP
1870                         sprintf(buf, "%s ¤òÊü¤·¤¿¡£", friend_name);
1871 #else
1872                         sprintf(buf, "Dismissed %s.", friend_name);
1873 #endif
1874                         message_add(buf);
1875                         p_ptr->window |= (PW_MESSAGE);
1876                         window_stuff();
1877
1878                         delete_monster_idx(pet_ctr);
1879                         Dismissed++;
1880                 }
1881         }
1882
1883         Term->scr->cu = cu;
1884         Term->scr->cv = cv;
1885         Term_fresh();
1886
1887         C_KILL(who, max_m_idx, u16b);
1888
1889 #ifdef JP
1890         msg_format("%d ÂΤΥڥåȤòÊü¤·¤Þ¤·¤¿¡£", Dismissed);
1891 #else
1892         msg_format("You have dismissed %d pet%s.", Dismissed,
1893                    (Dismissed == 1 ? "" : "s"));
1894 #endif
1895         if (Dismissed == 0 && all_pets)
1896 #ifdef JP
1897                 msg_print("'U'nnamed ¤Ï¡¢¾èÇϰʳ°¤Î̾Á°¤Î¤Ê¤¤¥Ú¥Ã¥È¤À¤±¤òÁ´¤Æ²òÊü¤·¤Þ¤¹¡£");
1898 #else
1899                 msg_print("'U'nnamed means all your pets except named pets and your mount.");
1900 #endif
1901 }
1902
1903 static bool player_can_ride_aux(cave_type *c_ptr, bool now_riding)
1904 {
1905         bool p_can_enter;
1906         bool old_character_xtra = character_xtra;
1907         int  old_riding = p_ptr->riding;
1908         bool old_riding_ryoute = p_ptr->riding_ryoute;
1909         bool old_old_riding_ryoute = p_ptr->old_riding_ryoute;
1910         bool old_pf_ryoute = (p_ptr->pet_extra_flags & PF_RYOUTE) ? TRUE : FALSE;
1911
1912         /* Hack -- prevent "icky" message */
1913         character_xtra = TRUE;
1914
1915         if (now_riding) p_ptr->riding = c_ptr->m_idx;
1916         else
1917         {
1918                 p_ptr->riding = 0;
1919                 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
1920                 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
1921         }
1922
1923         calc_bonuses();
1924
1925         p_can_enter = player_can_enter(c_ptr->feat, CEM_P_CAN_ENTER_PATTERN);
1926
1927         p_ptr->riding = old_riding;
1928         if (old_pf_ryoute) p_ptr->pet_extra_flags |= (PF_RYOUTE);
1929         else p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
1930         p_ptr->riding_ryoute = old_riding_ryoute;
1931         p_ptr->old_riding_ryoute = old_old_riding_ryoute;
1932
1933         calc_bonuses();
1934
1935         character_xtra = old_character_xtra;
1936
1937         return p_can_enter;
1938 }
1939
1940 bool rakuba(int dam, bool force)
1941 {
1942         int i, y, x, oy, ox;
1943         int sn = 0, sy = 0, sx = 0;
1944         char m_name[80];
1945         monster_type *m_ptr = &m_list[p_ptr->riding];
1946         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1947         bool fall_dam = FALSE;
1948
1949         if (!p_ptr->riding) return FALSE;
1950         if (p_ptr->wild_mode) return FALSE;
1951
1952         if (dam >= 0 || force)
1953         {
1954                 if (!force)
1955                 {
1956                         int cur = p_ptr->skill_exp[GINOU_RIDING];
1957                         int max = s_info[p_ptr->pclass].s_max[GINOU_RIDING];
1958                         int ridinglevel = r_ptr->level;
1959
1960                         /* ÍîÇϤΤ·¤ä¤¹¤µ */
1961                         int rakubalevel = r_ptr->level;
1962                         if (p_ptr->riding_ryoute) rakubalevel += 20;
1963
1964                         if ((cur < max) && (max > 1000) &&
1965                             (dam / 2 + ridinglevel) > (cur / 30 + 10))
1966                         {
1967                                 int inc = 0;
1968
1969                                 if (ridinglevel > (cur / 100 + 15))
1970                                         inc += 1 + (ridinglevel - cur / 100 - 15);
1971                                 else
1972                                         inc += 1;
1973
1974                                 p_ptr->skill_exp[GINOU_RIDING] = MIN(max, cur + inc);
1975                         }
1976
1977                         /* ¥ì¥Ù¥ë¤ÎÄ㤤¾èÇϤ«¤é¤ÏÍîÇϤ·¤Ë¤¯¤¤ */
1978                         if (randint0(dam / 2 + rakubalevel * 2) < cur / 30 + 10)
1979                         {
1980                                 if ((((p_ptr->pclass == CLASS_BEASTMASTER) || (p_ptr->pclass == CLASS_CAVALRY)) && !p_ptr->riding_ryoute) || !one_in_(p_ptr->lev*(p_ptr->riding_ryoute ? 2 : 3) + 30))
1981                                 {
1982                                         return FALSE;
1983                                 }
1984                         }
1985                 }
1986
1987                 /* Check around the player */
1988                 for (i = 0; i < 8; i++)
1989                 {
1990                         cave_type *c_ptr;
1991
1992                         /* Access the location */
1993                         y = py + ddy_ddd[i];
1994                         x = px + ddx_ddd[i];
1995
1996                         c_ptr = &cave[y][x];
1997
1998                         if (c_ptr->m_idx) continue;
1999
2000                         /* Skip non-empty grids */
2001                         if (!cave_have_flag_grid(c_ptr, FF_MOVE) && !cave_have_flag_grid(c_ptr, FF_CAN_FLY))
2002                         {
2003                                 if (!player_can_ride_aux(c_ptr, FALSE)) continue;
2004                         }
2005
2006                         if (cave_have_flag_grid(c_ptr, FF_PATTERN)) continue;
2007
2008                         /* Count "safe" grids */
2009                         sn++;
2010
2011                         /* Randomize choice */
2012                         if (randint0(sn) > 0) continue;
2013
2014                         /* Save the safe location */
2015                         sy = y; sx = x;
2016                 }
2017                 if (!sn)
2018                 {
2019                         monster_desc(m_name, m_ptr, 0);
2020 #ifdef JP
2021 msg_format("%s¤«¤é¿¶¤êÍî¤È¤µ¤ì¤½¤¦¤Ë¤Ê¤Ã¤Æ¡¢Êɤˤ֤Ĥ«¤Ã¤¿¡£",m_name);
2022                         take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, "Êɤؤξ×ÆÍ", -1);
2023 #else
2024                         msg_format("You have nearly fallen from %s, but bumped into wall.",m_name);
2025                         take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, "bumping into wall", -1);
2026 #endif
2027                         return FALSE;
2028                 }
2029
2030                 oy = py;
2031                 ox = px;
2032
2033                 py = sy;
2034                 px = sx;
2035
2036                 /* Redraw the old spot */
2037                 lite_spot(oy, ox);
2038
2039                 /* Redraw the new spot */
2040                 lite_spot(py, px);
2041
2042                 /* Check for new panel */
2043                 verify_panel();
2044         }
2045
2046         p_ptr->riding = 0;
2047         p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
2048         p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
2049
2050         calc_bonuses();
2051
2052         p_ptr->update |= (PU_BONUS);
2053
2054         /* Update stuff */
2055         p_ptr->update |= (PU_VIEW | PU_LITE | PU_FLOW | PU_MON_LITE | PU_MONSTERS);
2056
2057         /* Window stuff */
2058         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2059
2060         p_ptr->redraw |= (PR_EXTRA);
2061
2062         /* Update health track of mount */
2063         p_ptr->redraw |= (PR_UHEALTH);
2064
2065         if (p_ptr->levitation && !force)
2066         {
2067                 monster_desc(m_name, m_ptr, 0);
2068 #ifdef JP
2069                 msg_format("%s¤«¤éÍî¤Á¤¿¤¬¡¢¶õÃæ¤Ç¤¦¤Þ¤¯ÂÎÀª¤òΩ¤Æľ¤·¤ÆÃåÃϤ·¤¿¡£",m_name);
2070 #else
2071                 msg_format("You are thrown from %s, but make a good landing.",m_name);
2072 #endif
2073         }
2074         else
2075         {
2076 #ifdef JP
2077                 take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, "ÍîÇÏ", -1);
2078 #else
2079                 take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, "Falling from riding", -1);
2080 #endif
2081                 fall_dam = TRUE;
2082         }
2083
2084         /* Move the player */
2085         if (sy && !p_ptr->is_dead)
2086                 (void)move_player_effect(py, px, MPE_DONT_PICKUP | MPE_DONT_SWAP_MON);
2087
2088         return fall_dam;
2089 }
2090
2091 bool do_riding(bool force)
2092 {
2093         int x, y, dir = 0;
2094         cave_type *c_ptr;
2095         monster_type *m_ptr;
2096
2097         if (!get_rep_dir2(&dir)) return FALSE;
2098         y = py + ddy[dir];
2099         x = px + ddx[dir];
2100         c_ptr = &cave[y][x];
2101
2102         if (p_ptr->special_defense & KATA_MUSOU) set_action(ACTION_NONE);
2103
2104         if (p_ptr->riding)
2105         {
2106                 /* Skip non-empty grids */
2107                 if (!player_can_ride_aux(c_ptr, FALSE))
2108                 {
2109 #ifdef JP
2110                         msg_print("¤½¤Á¤é¤Ë¤Ï¹ß¤ê¤é¤ì¤Þ¤»¤ó¡£");
2111 #else
2112                         msg_print("You cannot go to that direction.");
2113 #endif
2114                         return FALSE;
2115                 }
2116
2117                 if (!pattern_seq(py, px, y, x)) return FALSE;
2118
2119                 if (c_ptr->m_idx)
2120                 {
2121                         /* Take a turn */
2122                         energy_use = 100;
2123
2124                         /* Message */
2125 #ifdef JP
2126                         msg_print("¥â¥ó¥¹¥¿¡¼¤¬Î©¤Á¤Õ¤µ¤¬¤Ã¤Æ¤¤¤ë¡ª");
2127 #else
2128                         msg_print("There is a monster in the way!");
2129 #endif
2130
2131                         py_attack(y, x, 0);
2132                         return FALSE;
2133                 }
2134
2135                 p_ptr->riding = 0;
2136                 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
2137                 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
2138         }
2139         else
2140         {
2141                 if (p_ptr->confused)
2142                 {
2143 #ifdef JP
2144                         msg_print("º®Í𤷤Ƥ¤¤Æ¾è¤ì¤Ê¤¤¡ª");
2145 #else
2146                         msg_print("You are too confused!");
2147 #endif
2148                         return FALSE;
2149                 }
2150
2151                 m_ptr = &m_list[c_ptr->m_idx];
2152
2153                 if (!c_ptr->m_idx || !m_ptr->ml)
2154                 {
2155 #ifdef JP
2156                         msg_print("¤½¤Î¾ì½ê¤Ë¤Ï¥â¥ó¥¹¥¿¡¼¤Ï¤¤¤Þ¤»¤ó¡£");
2157 #else
2158                         msg_print("Here is no monster.");
2159 #endif
2160
2161                         return FALSE;
2162                 }
2163                 if (!is_pet(m_ptr) && !force)
2164                 {
2165 #ifdef JP
2166                         msg_print("¤½¤Î¥â¥ó¥¹¥¿¡¼¤Ï¥Ú¥Ã¥È¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£");
2167 #else
2168                         msg_print("That monster is not a pet.");
2169 #endif
2170
2171                         return FALSE;
2172                 }
2173                 if (!(r_info[m_ptr->r_idx].flags7 & RF7_RIDING))
2174                 {
2175 #ifdef JP
2176                         msg_print("¤½¤Î¥â¥ó¥¹¥¿¡¼¤Ë¤Ï¾è¤ì¤Ê¤µ¤½¤¦¤À¡£");
2177 #else
2178                         msg_print("This monster doesn't seem suitable for riding.");
2179 #endif
2180
2181                         return FALSE;
2182                 }
2183
2184                 if (!pattern_seq(py, px, y, x)) return FALSE;
2185
2186                 if (!player_can_ride_aux(c_ptr, TRUE))
2187                 {
2188                         /* Feature code (applying "mimic" field) */
2189                         feature_type *f_ptr = &f_info[get_feat_mimic(c_ptr)];
2190 #ifdef JP
2191                         msg_format("¤½¤Î¥â¥ó¥¹¥¿¡¼¤Ï%s¤Î%s¤Ë¤¤¤ë¡£", f_name + f_ptr->name,
2192                                    ((!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) ||
2193                                     (!have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_TREE))) ?
2194                                    "Ãæ" : "¾å");
2195 #else
2196                         msg_format("This monster is %s the %s.",
2197                                    ((!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) ||
2198                                     (!have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_TREE))) ?
2199                                    "in" : "on", f_name + f_ptr->name);
2200 #endif
2201
2202                         return FALSE;
2203                 }
2204                 if (r_info[m_ptr->r_idx].level > randint1((p_ptr->skill_exp[GINOU_RIDING] / 50 + p_ptr->lev / 2 + 20)))
2205                 {
2206 #ifdef JP
2207                         msg_print("¤¦¤Þ¤¯¾è¤ì¤Ê¤«¤Ã¤¿¡£");
2208 #else
2209                         msg_print("You failed to ride.");
2210 #endif
2211
2212                         energy_use = 100;
2213
2214                         return FALSE;
2215                 }
2216
2217                 if (MON_CSLEEP(m_ptr))
2218                 {
2219                         char m_name[80];
2220                         monster_desc(m_name, m_ptr, 0);
2221                         (void)set_monster_csleep(c_ptr->m_idx, 0);
2222 #ifdef JP
2223                         msg_format("%s¤òµ¯¤³¤·¤¿¡£", m_name);
2224 #else
2225                         msg_format("You have waked %s up.", m_name);
2226 #endif
2227                 }
2228
2229                 if (p_ptr->action == ACTION_KAMAE) set_action(ACTION_NONE);
2230
2231                 p_ptr->riding = c_ptr->m_idx;
2232
2233                 /* Hack -- remove tracked monster */
2234                 if (p_ptr->riding == p_ptr->health_who) health_track(0);
2235         }
2236
2237         energy_use = 100;
2238
2239         /* Mega-Hack -- Forget the view and lite */
2240         p_ptr->update |= (PU_UN_VIEW | PU_UN_LITE);
2241
2242         /* Update the monsters */
2243         p_ptr->update |= (PU_BONUS);
2244
2245         /* Redraw map */
2246         p_ptr->redraw |= (PR_MAP | PR_EXTRA);
2247
2248         p_ptr->redraw |= (PR_UHEALTH);
2249
2250         /* Move the player */
2251         (void)move_player_effect(y, x, MPE_HANDLE_STUFF | MPE_ENERGY_USE | MPE_DONT_PICKUP | MPE_DONT_SWAP_MON);
2252
2253         return TRUE;
2254 }
2255
2256 static void do_name_pet(void)
2257 {
2258         monster_type *m_ptr;
2259         char out_val[20];
2260         char m_name[80];
2261         bool old_name = FALSE;
2262         bool old_target_pet = target_pet;
2263
2264         target_pet = TRUE;
2265         if (!target_set(TARGET_KILL))
2266         {
2267                 target_pet = old_target_pet;
2268                 return;
2269         }
2270         target_pet = old_target_pet;
2271
2272         if (cave[target_row][target_col].m_idx)
2273         {
2274                 m_ptr = &m_list[cave[target_row][target_col].m_idx];
2275
2276                 if (!is_pet(m_ptr))
2277                 {
2278                         /* Message */
2279 #ifdef JP
2280                         msg_print("¤½¤Î¥â¥ó¥¹¥¿¡¼¤Ï¥Ú¥Ã¥È¤Ç¤Ï¤Ê¤¤¡£");
2281 #else
2282                         msg_format("This monster is not a pet.");
2283 #endif
2284                         return;
2285                 }
2286                 if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2287                 {
2288 #ifdef JP
2289                         msg_print("¤½¤Î¥â¥ó¥¹¥¿¡¼¤Î̾Á°¤ÏÊѤ¨¤é¤ì¤Ê¤¤¡ª");
2290 #else
2291                         msg_format("You cannot change name of this monster!");
2292 #endif
2293                         return;
2294                 }
2295                 monster_desc(m_name, m_ptr, 0);
2296
2297                 /* Message */
2298 #ifdef JP
2299                 msg_format("%s¤Ë̾Á°¤ò¤Ä¤±¤ë¡£", m_name);
2300 #else
2301                 msg_format("Name %s.", m_name);
2302 #endif
2303
2304                 msg_print(NULL);
2305
2306                 /* Start with nothing */
2307                 strcpy(out_val, "");
2308
2309                 /* Use old inscription */
2310                 if (m_ptr->nickname)
2311                 {
2312                         /* Start with the old inscription */
2313                         strcpy(out_val, quark_str(m_ptr->nickname));
2314                         old_name = TRUE;
2315                 }
2316
2317                 /* Get a new inscription (possibly empty) */
2318 #ifdef JP
2319                 if (get_string("̾Á°: ", out_val, 15))
2320 #else
2321                 if (get_string("Name: ", out_val, 15))
2322 #endif
2323
2324                 {
2325                         if (out_val[0])
2326                         {
2327                                 /* Save the inscription */
2328                                 m_ptr->nickname = quark_add(out_val);
2329                                 if (record_named_pet)
2330                                 {
2331                                         char m_name[80];
2332
2333                                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2334                                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_NAME, m_name);
2335                                 }
2336                         }
2337                         else
2338                         {
2339                                 if (record_named_pet && old_name)
2340                                 {
2341                                         char m_name[80];
2342
2343                                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2344                                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_UNNAME, m_name);
2345                                 }
2346                                 m_ptr->nickname = 0;
2347                         }
2348                 }
2349         }
2350 }
2351
2352 /*
2353  * Issue a pet command
2354  */
2355 void do_cmd_pet(void)
2356 {
2357         int                     i = 0;
2358         int                     num;
2359         int                     powers[36];
2360         cptr                    power_desc[36];
2361         bool                    flag, redraw;
2362         int                     ask;
2363         char                    choice;
2364         char                    out_val[160];
2365         int                     pet_ctr;
2366         monster_type    *m_ptr;
2367
2368         int mode = 0;
2369
2370         char buf[160];
2371         char target_buf[160];
2372
2373         int menu_line = use_menu ? 1 : 0;
2374
2375         num = 0;
2376
2377 #ifdef JP
2378         power_desc[num] = "¥Ú¥Ã¥È¤òÊü¤¹";
2379 #else
2380         power_desc[num] = "dismiss pets";
2381 #endif
2382
2383         powers[num++] = PET_DISMISS;
2384
2385 #ifdef JP
2386         sprintf(target_buf, "¥Ú¥Ã¥È¤Î¥¿¡¼¥²¥Ã¥È¤ò»ØÄê (¸½ºß¡§%s)",
2387                 (pet_t_m_idx ? (p_ptr->image ? "²¿¤«´ñ̯¤Êʪ" : (r_name + r_info[m_list[pet_t_m_idx].ap_r_idx].name)) : "»ØÄê¤Ê¤·"));
2388 #else
2389         sprintf(target_buf, "specify a target of pet (now:%s)",
2390                 (pet_t_m_idx ? (p_ptr->image ? "something strange" : (r_name + r_info[m_list[pet_t_m_idx].ap_r_idx].name)) : "nothing"));
2391 #endif
2392         power_desc[num] = target_buf;
2393
2394         powers[num++] = PET_TARGET;
2395
2396 #ifdef JP
2397         power_desc[num] = "¶á¤¯¤Ë¤¤¤í";
2398 #else
2399         power_desc[num] = "stay close";
2400 #endif
2401
2402         if (p_ptr->pet_follow_distance == PET_CLOSE_DIST) mode = num;
2403         powers[num++] = PET_STAY_CLOSE;
2404
2405 #ifdef JP
2406         power_desc[num] = "¤Ä¤¤¤ÆÍ褤";
2407 #else
2408         power_desc[num] = "follow me";
2409 #endif
2410
2411         if (p_ptr->pet_follow_distance == PET_FOLLOW_DIST) mode = num;
2412         powers[num++] = PET_FOLLOW_ME;
2413
2414 #ifdef JP
2415         power_desc[num] = "Ũ¤ò¸«¤Ä¤±¤ÆÅݤ»";
2416 #else
2417         power_desc[num] = "seek and destroy";
2418 #endif
2419
2420         if (p_ptr->pet_follow_distance == PET_DESTROY_DIST) mode = num;
2421         powers[num++] = PET_SEEK_AND_DESTROY;
2422
2423 #ifdef JP
2424         power_desc[num] = "¾¯¤·Î¥¤ì¤Æ¤¤¤í";
2425 #else
2426         power_desc[num] = "give me space";
2427 #endif
2428
2429         if (p_ptr->pet_follow_distance == PET_SPACE_DIST) mode = num;
2430         powers[num++] = PET_ALLOW_SPACE;
2431
2432 #ifdef JP
2433         power_desc[num] = "Î¥¤ì¤Æ¤¤¤í";
2434 #else
2435         power_desc[num] = "stay away";
2436 #endif
2437
2438         if (p_ptr->pet_follow_distance == PET_AWAY_DIST) mode = num;
2439         powers[num++] = PET_STAY_AWAY;
2440
2441         if (p_ptr->pet_extra_flags & PF_OPEN_DOORS)
2442         {
2443 #ifdef JP
2444                 power_desc[num] = "¥É¥¢¤ò³«¤±¤ë (¸½ºß:ON)";
2445 #else
2446                 power_desc[num] = "pets open doors (now On)";
2447 #endif
2448         }
2449         else
2450         {
2451 #ifdef JP
2452                 power_desc[num] = "¥É¥¢¤ò³«¤±¤ë (¸½ºß:OFF)";
2453 #else
2454                 power_desc[num] = "pets open doors (now Off)";
2455 #endif
2456         }
2457         powers[num++] = PET_OPEN_DOORS;
2458
2459         if (p_ptr->pet_extra_flags & PF_PICKUP_ITEMS)
2460         {
2461 #ifdef JP
2462                 power_desc[num] = "¥¢¥¤¥Æ¥à¤ò½¦¤¦ (¸½ºß:ON)";
2463 #else
2464                 power_desc[num] = "pets pick up items (now On)";
2465 #endif
2466         }
2467         else
2468         {
2469 #ifdef JP
2470                 power_desc[num] = "¥¢¥¤¥Æ¥à¤ò½¦¤¦ (¸½ºß:OFF)";
2471 #else
2472                 power_desc[num] = "pets pick up items (now Off)";
2473 #endif
2474         }
2475         powers[num++] = PET_TAKE_ITEMS;
2476
2477         if (p_ptr->pet_extra_flags & PF_TELEPORT)
2478         {
2479 #ifdef JP
2480                 power_desc[num] = "¥Æ¥ì¥Ý¡¼¥È·ÏËâË¡¤ò»È¤¦ (¸½ºß:ON)";
2481 #else
2482                 power_desc[num] = "allow teleport (now On)";
2483 #endif
2484         }
2485         else
2486         {
2487 #ifdef JP
2488                 power_desc[num] = "¥Æ¥ì¥Ý¡¼¥È·ÏËâË¡¤ò»È¤¦ (¸½ºß:OFF)";
2489 #else
2490                 power_desc[num] = "allow teleport (now Off)";
2491 #endif
2492         }
2493         powers[num++] = PET_TELEPORT;
2494
2495         if (p_ptr->pet_extra_flags & PF_ATTACK_SPELL)
2496         {
2497 #ifdef JP
2498                 power_desc[num] = "¹¶·âËâË¡¤ò»È¤¦ (¸½ºß:ON)";
2499 #else
2500                 power_desc[num] = "allow cast attack spell (now On)";
2501 #endif
2502         }
2503         else
2504         {
2505 #ifdef JP
2506                 power_desc[num] = "¹¶·âËâË¡¤ò»È¤¦ (¸½ºß:OFF)";
2507 #else
2508                 power_desc[num] = "allow cast attack spell (now Off)";
2509 #endif
2510         }
2511         powers[num++] = PET_ATTACK_SPELL;
2512
2513         if (p_ptr->pet_extra_flags & PF_SUMMON_SPELL)
2514         {
2515 #ifdef JP
2516                 power_desc[num] = "¾¤´­ËâË¡¤ò»È¤¦ (¸½ºß:ON)";
2517 #else
2518                 power_desc[num] = "allow cast summon spell (now On)";
2519 #endif
2520         }
2521         else
2522         {
2523 #ifdef JP
2524                 power_desc[num] = "¾¤´­ËâË¡¤ò»È¤¦ (¸½ºß:OFF)";
2525 #else
2526                 power_desc[num] = "allow cast summon spell (now Off)";
2527 #endif
2528         }
2529         powers[num++] = PET_SUMMON_SPELL;
2530
2531         if (p_ptr->pet_extra_flags & PF_BALL_SPELL)
2532         {
2533 #ifdef JP
2534                 power_desc[num] = "¥×¥ì¥¤¥ä¡¼¤ò´¬¤­¹þ¤àÈÏ°ÏËâË¡¤ò»È¤¦ (¸½ºß:ON)";
2535 #else
2536                 power_desc[num] = "allow involve player in area spell (now On)";
2537 #endif
2538         }
2539         else
2540         {
2541 #ifdef JP
2542                 power_desc[num] = "¥×¥ì¥¤¥ä¡¼¤ò´¬¤­¹þ¤àÈÏ°ÏËâË¡¤ò»È¤¦ (¸½ºß:OFF)";
2543 #else
2544                 power_desc[num] = "allow involve player in area spell (now Off)";
2545 #endif
2546         }
2547         powers[num++] = PET_BALL_SPELL;
2548
2549         if (p_ptr->riding)
2550         {
2551 #ifdef JP
2552                 power_desc[num] = "¥Ú¥Ã¥È¤«¤é¹ß¤ê¤ë";
2553 #else
2554                 power_desc[num] = "get off a pet";
2555 #endif
2556         }
2557         else
2558         {
2559 #ifdef JP
2560                 power_desc[num] = "¥Ú¥Ã¥È¤Ë¾è¤ë";
2561 #else
2562                 power_desc[num] = "ride a pet";
2563 #endif
2564         }
2565         powers[num++] = PET_RIDING;
2566
2567 #ifdef JP
2568         power_desc[num] = "¥Ú¥Ã¥È¤Ë̾Á°¤ò¤Ä¤±¤ë";
2569 #else
2570         power_desc[num] = "name pets";
2571 #endif
2572
2573         powers[num++] = PET_NAME;
2574
2575         if (p_ptr->riding)
2576         {
2577                 if ((p_ptr->migite && (empty_hands(FALSE) == EMPTY_HAND_LARM) &&
2578                      object_allow_two_hands_wielding(&inventory[INVEN_RARM])) ||
2579                     (p_ptr->hidarite && (empty_hands(FALSE) == EMPTY_HAND_RARM) &&
2580                          object_allow_two_hands_wielding(&inventory[INVEN_LARM])))
2581                 {
2582                         if (p_ptr->pet_extra_flags & PF_RYOUTE)
2583                         {
2584 #ifdef JP
2585                                 power_desc[num] = "Éð´ï¤òÊÒ¼ê¤Ç»ý¤Ä";
2586 #else
2587                                 power_desc[num] = "use one hand to control a riding pet";
2588 #endif
2589                         }
2590                         else
2591                         {
2592 #ifdef JP
2593                                 power_desc[num] = "Éð´ï¤òξ¼ê¤Ç»ý¤Ä";
2594 #else
2595                                 power_desc[num] = "use both hands for a weapon";
2596 #endif
2597                         }
2598
2599                         powers[num++] = PET_RYOUTE;
2600                 }
2601                 else
2602                 {
2603                         switch (p_ptr->pclass)
2604                         {
2605                         case CLASS_MONK:
2606                         case CLASS_FORCETRAINER:
2607                         case CLASS_BERSERKER:
2608                                 if (empty_hands(FALSE) == (EMPTY_HAND_RARM | EMPTY_HAND_LARM))
2609                                 {
2610                                         if (p_ptr->pet_extra_flags & PF_RYOUTE)
2611                                         {
2612 #ifdef JP
2613                                                 power_desc[num] = "ÊÒ¼ê¤Ç³ÊÆ®¤¹¤ë";
2614 #else
2615                                                 power_desc[num] = "use one hand to control a riding pet";
2616 #endif
2617                                         }
2618                                         else
2619                                         {
2620 #ifdef JP
2621                                                 power_desc[num] = "ξ¼ê¤Ç³ÊÆ®¤¹¤ë";
2622 #else
2623                                                 power_desc[num] = "use both hands for melee";
2624 #endif
2625                                         }
2626
2627                                         powers[num++] = PET_RYOUTE;
2628                                 }
2629                                 else if ((empty_hands(FALSE) != EMPTY_HAND_NONE) && !buki_motteruka(INVEN_RARM) && !buki_motteruka(INVEN_LARM))
2630                                 {
2631                                         if (p_ptr->pet_extra_flags & PF_RYOUTE)
2632                                         {
2633 #ifdef JP
2634                                                 power_desc[num] = "³ÊÆ®¤ò¹Ô¤ï¤Ê¤¤";
2635 #else
2636                                                 power_desc[num] = "use one hand to control a riding pet";
2637 #endif
2638                                         }
2639                                         else
2640                                         {
2641 #ifdef JP
2642                                                 power_desc[num] = "³ÊÆ®¤ò¹Ô¤¦";
2643 #else
2644                                                 power_desc[num] = "use one hand for melee";
2645 #endif
2646                                         }
2647
2648                                         powers[num++] = PET_RYOUTE;
2649                                 }
2650                                 break;
2651                         }
2652                 }
2653         }
2654
2655 #ifdef ALLOW_REPEAT
2656         if (!(repeat_pull(&i) && (i >= 0) && (i < num)))
2657         {
2658 #endif /* ALLOW_REPEAT */
2659
2660         /* Nothing chosen yet */
2661         flag = FALSE;
2662
2663         /* No redraw yet */
2664         redraw = FALSE;
2665
2666         if (use_menu)
2667         {
2668                 /* Save the screen */
2669                 screen_save();
2670
2671                 /* Build a prompt */
2672 #ifdef JP
2673                 strnfmt(out_val, 78, "(¥³¥Þ¥ó¥É¡¢ESC=½ªÎ») ¥³¥Þ¥ó¥É¤òÁª¤ó¤Ç¤¯¤À¤µ¤¤:");
2674 #else
2675                 strnfmt(out_val, 78, "(Command, ESC=exit) Choose command from menu.");
2676 #endif
2677         }
2678         else
2679         {
2680                 /* Build a prompt */
2681                 strnfmt(out_val, 78,
2682 #ifdef JP
2683                         "(¥³¥Þ¥ó¥É %c-%c¡¢'*'=°ìÍ÷¡¢ESC=½ªÎ») ¥³¥Þ¥ó¥É¤òÁª¤ó¤Ç¤¯¤À¤µ¤¤:",
2684 #else
2685                         "(Command %c-%c, *=List, ESC=exit) Select a command: ",
2686 #endif
2687                         I2A(0), I2A(num - 1));
2688         }
2689
2690         choice = (always_show_list || use_menu) ? ESCAPE : 1;
2691
2692         /* Get a command from the user */
2693         while (!flag)
2694         {
2695                 if (choice == ESCAPE) choice = ' ';
2696                 else if (!get_com(out_val, &choice, TRUE)) break;
2697
2698                 if (use_menu && (choice != ' '))
2699                 {
2700                         switch (choice)
2701                         {
2702                         case '0':
2703                                 screen_load();
2704                                 return;
2705
2706                         case '8':
2707                         case 'k':
2708                         case 'K':
2709                                 menu_line += (num - 1);
2710                                 break;
2711
2712                         case '2':
2713                         case 'j':
2714                         case 'J':
2715                                 menu_line++;
2716                                 break;
2717
2718                         case '4':
2719                         case 'h':
2720                         case 'H':
2721                                 menu_line = 1;
2722                                 break;
2723
2724                         case '6':
2725                         case 'l':
2726                         case 'L':
2727                                 menu_line = num;
2728                                 break;
2729
2730                         case 'x':
2731                         case 'X':
2732                         case '\r':
2733                         case '\n':
2734                                 i = menu_line - 1;
2735                                 ask = FALSE;
2736                                 break;
2737                         }
2738                         if (menu_line > num) menu_line -= num;
2739                 }
2740
2741                 /* Request redraw */
2742                 if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && ask))
2743                 {
2744                         /* Show the list */
2745                         if (!redraw || use_menu)
2746                         {
2747                                 byte y = 1, x = 0;
2748                                 int ctr = 0;
2749
2750                                 /* Show list */
2751                                 redraw = TRUE;
2752
2753                                 /* Save the screen */
2754                                 if (!use_menu) screen_save();
2755
2756                                 prt("", y++, x);
2757
2758                                 /* Print list */
2759                                 for (ctr = 0; ctr < num; ctr++)
2760                                 {
2761                                         /* Letter/number for power selection */
2762 #ifdef JP
2763                                         if (use_menu) sprintf(buf, "%c%s ", (ctr == mode) ? '*' : ' ', (ctr == (menu_line - 1)) ? "¡Õ" : "  ");
2764 #else
2765                                         if (use_menu) sprintf(buf, "%c%s ", (ctr == mode) ? '*' : ' ', (ctr == (menu_line - 1)) ? "> " : "  ");
2766 #endif
2767                                         else sprintf(buf, "%c%c) ", (ctr == mode) ? '*' : ' ', I2A(ctr));
2768
2769                                         strcat(buf, power_desc[ctr]);
2770
2771                                         prt(buf, y + ctr, x);
2772                                 }
2773
2774                                 prt("", y + MIN(ctr, 17), x);
2775                         }
2776
2777                         /* Hide the list */
2778                         else
2779                         {
2780                                 /* Hide list */
2781                                 redraw = FALSE;
2782
2783                                 /* Restore the screen */
2784                                 screen_load();
2785                         }
2786
2787                         /* Redo asking */
2788                         continue;
2789                 }
2790
2791                 if (!use_menu)
2792                 {
2793                         /* Note verify */
2794                         ask = (isupper(choice));
2795
2796                         /* Lowercase */
2797                         if (ask) choice = tolower(choice);
2798
2799                         /* Extract request */
2800                         i = (islower(choice) ? A2I(choice) : -1);
2801                 }
2802
2803                 /* Totally Illegal */
2804                 if ((i < 0) || (i >= num))
2805                 {
2806                         bell();
2807                         continue;
2808                 }
2809
2810                 /* Verify it */
2811                 if (ask)
2812                 {
2813                         /* Prompt */
2814 #ifdef JP
2815                         strnfmt(buf, 78, "%s¤ò»È¤¤¤Þ¤¹¤«¡© ", power_desc[i]);
2816 #else
2817                         strnfmt(buf, 78, "Use %s? ", power_desc[i]);
2818 #endif
2819
2820                         /* Belay that order */
2821                         if (!get_check(buf)) continue;
2822                 }
2823
2824                 /* Stop the loop */
2825                 flag = TRUE;
2826         }
2827
2828         /* Restore the screen */
2829         if (redraw) screen_load();
2830
2831         /* Abort if needed */
2832         if (!flag)
2833         {
2834                 energy_use = 0;
2835                 return;
2836         }
2837
2838 #ifdef ALLOW_REPEAT
2839         repeat_push(i);
2840         }
2841 #endif /* ALLOW_REPEAT */
2842
2843         switch (powers[i])
2844         {
2845                 case PET_DISMISS: /* Dismiss pets */
2846                 {
2847                         /* Check pets (backwards) */
2848                         for (pet_ctr = m_max - 1; pet_ctr >= 1; pet_ctr--)
2849                         {
2850                                 /* Player has pet */
2851                                 if (is_pet(&m_list[pet_ctr])) break;
2852                         }
2853
2854                         if (!pet_ctr)
2855                         {
2856 #ifdef JP
2857                                 msg_print("¥Ú¥Ã¥È¤¬¤¤¤Ê¤¤¡ª");
2858 #else
2859                                 msg_print("You have no pets!");
2860 #endif
2861                                 break;
2862                         }
2863                         do_cmd_pet_dismiss();
2864                         (void)calculate_upkeep();
2865                         break;
2866                 }
2867                 case PET_TARGET:
2868                 {
2869                         project_length = -1;
2870                         if (!target_set(TARGET_KILL)) pet_t_m_idx = 0;
2871                         else
2872                         {
2873                                 cave_type *c_ptr = &cave[target_row][target_col];
2874                                 if (c_ptr->m_idx && (m_list[c_ptr->m_idx].ml))
2875                                 {
2876                                         pet_t_m_idx = cave[target_row][target_col].m_idx;
2877                                         p_ptr->pet_follow_distance = PET_DESTROY_DIST;
2878                                 }
2879                                 else pet_t_m_idx = 0;
2880                         }
2881                         project_length = 0;
2882
2883                         break;
2884                 }
2885                 /* Call pets */
2886                 case PET_STAY_CLOSE:
2887                 {
2888                         p_ptr->pet_follow_distance = PET_CLOSE_DIST;
2889                         pet_t_m_idx = 0;
2890                         break;
2891                 }
2892                 /* "Follow Me" */
2893                 case PET_FOLLOW_ME:
2894                 {
2895                         p_ptr->pet_follow_distance = PET_FOLLOW_DIST;
2896                         pet_t_m_idx = 0;
2897                         break;
2898                 }
2899                 /* "Seek and destoy" */
2900                 case PET_SEEK_AND_DESTROY:
2901                 {
2902                         p_ptr->pet_follow_distance = PET_DESTROY_DIST;
2903                         break;
2904                 }
2905                 /* "Give me space" */
2906                 case PET_ALLOW_SPACE:
2907                 {
2908                         p_ptr->pet_follow_distance = PET_SPACE_DIST;
2909                         break;
2910                 }
2911                 /* "Stay away" */
2912                 case PET_STAY_AWAY:
2913                 {
2914                         p_ptr->pet_follow_distance = PET_AWAY_DIST;
2915                         break;
2916                 }
2917                 /* flag - allow pets to open doors */
2918                 case PET_OPEN_DOORS:
2919                 {
2920                         if (p_ptr->pet_extra_flags & PF_OPEN_DOORS) p_ptr->pet_extra_flags &= ~(PF_OPEN_DOORS);
2921                         else p_ptr->pet_extra_flags |= (PF_OPEN_DOORS);
2922                         break;
2923                 }
2924                 /* flag - allow pets to pickup items */
2925                 case PET_TAKE_ITEMS:
2926                 {
2927                         if (p_ptr->pet_extra_flags & PF_PICKUP_ITEMS)
2928                         {
2929                                 p_ptr->pet_extra_flags &= ~(PF_PICKUP_ITEMS);
2930                                 for (pet_ctr = m_max - 1; pet_ctr >= 1; pet_ctr--)
2931                                 {
2932                                         /* Access the monster */
2933                                         m_ptr = &m_list[pet_ctr];
2934
2935                                         if (is_pet(m_ptr))
2936                                         {
2937                                                 monster_drop_carried_objects(m_ptr);
2938                                         }
2939                                 }
2940                         }
2941                         else p_ptr->pet_extra_flags |= (PF_PICKUP_ITEMS);
2942
2943                         break;
2944                 }
2945                 /* flag - allow pets to teleport */
2946                 case PET_TELEPORT:
2947                 {
2948                         if (p_ptr->pet_extra_flags & PF_TELEPORT) p_ptr->pet_extra_flags &= ~(PF_TELEPORT);
2949                         else p_ptr->pet_extra_flags |= (PF_TELEPORT);
2950                         break;
2951                 }
2952                 /* flag - allow pets to cast attack spell */
2953                 case PET_ATTACK_SPELL:
2954                 {
2955                         if (p_ptr->pet_extra_flags & PF_ATTACK_SPELL) p_ptr->pet_extra_flags &= ~(PF_ATTACK_SPELL);
2956                         else p_ptr->pet_extra_flags |= (PF_ATTACK_SPELL);
2957                         break;
2958                 }
2959                 /* flag - allow pets to cast attack spell */
2960                 case PET_SUMMON_SPELL:
2961                 {
2962                         if (p_ptr->pet_extra_flags & PF_SUMMON_SPELL) p_ptr->pet_extra_flags &= ~(PF_SUMMON_SPELL);
2963                         else p_ptr->pet_extra_flags |= (PF_SUMMON_SPELL);
2964                         break;
2965                 }
2966                 /* flag - allow pets to cast attack spell */
2967                 case PET_BALL_SPELL:
2968                 {
2969                         if (p_ptr->pet_extra_flags & PF_BALL_SPELL) p_ptr->pet_extra_flags &= ~(PF_BALL_SPELL);
2970                         else p_ptr->pet_extra_flags |= (PF_BALL_SPELL);
2971                         break;
2972                 }
2973
2974                 case PET_RIDING:
2975                 {
2976                         (void)do_riding(FALSE);
2977                         break;
2978                 }
2979
2980                 case PET_NAME:
2981                 {
2982                         do_name_pet();
2983                         break;
2984                 }
2985
2986                 case PET_RYOUTE:
2987                 {
2988                         if (p_ptr->pet_extra_flags & PF_RYOUTE) p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
2989                         else p_ptr->pet_extra_flags |= (PF_RYOUTE);
2990                         p_ptr->update |= (PU_BONUS);
2991                         handle_stuff();
2992                         break;
2993                 }
2994         }
2995 }