OSDN Git Service

a490e8158f78b9b493100e6781b25d60b0780eb7
[hengband/hengband.git] / src / cmd5.c
1 /*!
2  *  @file cmd5.c
3  *  @brief プレイヤーの魔法に関するコマンドの実装 / Spell/Prayer commands
4  *  @date 2014/01/02
5  *  @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7  *
8  * This software may be copied and distributed for educational, research,
9  * and not for profit purposes provided that this copyright and statement
10  * are included in all such copies.  Other copyrights may also apply.
11  */
12
13
14 #include "angband.h"
15
16 /*!
17  * @brief 領域魔法に応じて技能の名称を返す。
18  * @param tval 魔法書のtval
19  * @return 領域魔法の技能名称を保管した文字列ポインタ
20  */
21 cptr spell_category_name(OBJECT_TYPE_VALUE tval)
22 {
23         switch (tval)
24         {
25         case TV_HISSATSU_BOOK:
26                 return _("必殺技", "art");
27         case TV_LIFE_BOOK:
28                 return _("祈り", "prayer");
29         case TV_MUSIC_BOOK:
30                 return _("歌", "song");
31         default:
32                 return _("呪文", "spell");
33         }
34 }
35
36
37 bool select_the_force = FALSE;
38
39 /*!
40  * @brief 領域魔法の閲覧、学習、使用選択するインターフェイス処理
41  * Allow user to choose a spell/prayer from the given book.
42  * @param sn 選択した魔法IDを返す参照ポインタ
43  * @param prompt 魔法を利用する際の動詞表記
44  * @param sval 魔道書のsval
45  * @param learned 閲覧/使用選択ならばTRUE、学習処理ならFALSE
46  * @param use_realm 魔法領域ID
47  * @return
48  * <pre>
49  * If a valid spell is chosen, saves it in '*sn' and returns TRUE
50  * If the user hits escape, returns FALSE, and set '*sn' to -1
51  * If there are no legal choices, returns FALSE, and sets '*sn' to -2
52  * The "prompt" should be "cast", "recite", or "study"
53  * The "known" should be TRUE for cast/pray, FALSE for study
54  * </pre>
55  */
56 static int get_spell(SPELL_IDX *sn, cptr prompt, OBJECT_SUBTYPE_VALUE sval, bool learned, REALM_IDX use_realm)
57 {
58         int         i;
59         SPELL_IDX   spell = -1;
60         int         num = 0;
61         int         ask = TRUE;
62         MANA_POINT  need_mana;
63         SPELL_IDX   spells[64];
64         bool        flag, redraw, okay;
65         char        choice;
66         const magic_type  *s_ptr;
67         char        out_val[160];
68         cptr        p;
69         COMMAND_CODE code;
70 #ifdef JP
71         char jverb_buf[128];
72 #endif
73         int menu_line = (use_menu ? 1 : 0);
74
75 #ifdef ALLOW_REPEAT /* TNB */
76
77         /* Get the spell, if available */
78         if (repeat_pull(&code))
79         {
80                 *sn = (SPELL_IDX)code;
81                 /* Verify the spell */
82                 if (spell_okay(*sn, learned, FALSE, use_realm))
83                 {
84                         /* Success */
85                         return (TRUE);
86                 }
87         }
88
89 #endif /* ALLOW_REPEAT -- TNB */
90
91         p = spell_category_name(mp_ptr->spell_book);
92
93         /* Extract spells */
94         for (spell = 0; spell < 32; spell++)
95         {
96                 /* Check for this spell */
97                 if ((fake_spell_flags[sval] & (1L << spell)))
98                 {
99                         /* Collect this spell */
100                         spells[num++] = spell;
101                 }
102         }
103
104         /* Assume no usable spells */
105         okay = FALSE;
106
107         /* Assume no spells available */
108         (*sn) = -2;
109
110         /* Check for "okay" spells */
111         for (i = 0; i < num; i++)
112         {
113                 /* Look for "okay" spells */
114                 if (spell_okay(spells[i], learned, FALSE, use_realm)) okay = TRUE;
115         }
116
117         /* No "okay" spells */
118         if (!okay) return (FALSE);
119         if (((use_realm) != p_ptr->realm1) && ((use_realm) != p_ptr->realm2) && (p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE)) return FALSE;
120         if (((p_ptr->pclass == CLASS_SORCERER) || (p_ptr->pclass == CLASS_RED_MAGE)) && !is_magic(use_realm)) return FALSE;
121         if ((p_ptr->pclass == CLASS_RED_MAGE) && ((use_realm) != REALM_ARCANE) && (sval > 1)) return FALSE;
122
123         /* Assume cancelled */
124         *sn = (-1);
125
126         /* Nothing chosen yet */
127         flag = FALSE;
128
129         /* No redraw yet */
130         redraw = FALSE;
131
132         /* Show choices */
133         p_ptr->window |= (PW_SPELL);
134
135         /* Window stuff */
136         window_stuff();
137
138         /* Build a prompt (accept all spells) */
139 #ifdef JP
140         jverb( prompt, jverb_buf, JVERB_AND );
141         (void) strnfmt(out_val, 78, "(%^s:%c-%c, '*'で一覧, ESCで中断) どの%sを%^sますか? ",
142                 p, I2A(0), I2A(num - 1), p, jverb_buf );
143 #else
144         (void)strnfmt(out_val, 78, "(%^ss %c-%c, *=List, ESC=exit) %^s which %s? ",
145                 p, I2A(0), I2A(num - 1), prompt, p);
146 #endif
147
148         /* Get a spell from the user */
149
150         choice = (always_show_list || use_menu) ? ESCAPE : 1;
151         while (!flag)
152         {
153                 if (choice == ESCAPE) choice = ' '; 
154                 else if (!get_com(out_val, &choice, TRUE))break;
155
156                 if (use_menu && choice != ' ')
157                 {
158                         switch (choice)
159                         {
160                                 case '0':
161                                 {
162                                         screen_load();
163                                         return FALSE;
164                                 }
165
166                                 case '8':
167                                 case 'k':
168                                 case 'K':
169                                 {
170                                         menu_line += (num - 1);
171                                         break;
172                                 }
173
174                                 case '2':
175                                 case 'j':
176                                 case 'J':
177                                 {
178                                         menu_line++;
179                                         break;
180                                 }
181
182                                 case 'x':
183                                 case 'X':
184                                 case '\r':
185                                 case '\n':
186                                 {
187                                         i = menu_line - 1;
188                                         ask = FALSE;
189                                         break;
190                                 }
191                         }
192                         if (menu_line > num) menu_line -= num;
193                         /* Display a list of spells */
194                         print_spells(menu_line, spells, num, 1, 15, use_realm);
195                         if (ask) continue;
196                 }
197                 else
198                 {
199                         /* Request redraw */
200                         if ((choice == ' ') || (choice == '*') || (choice == '?'))
201                         {
202                                 /* Show the list */
203                                 if (!redraw)
204                                 {
205                                         /* Show list */
206                                         redraw = TRUE;
207
208                                         /* Save the screen */
209                                         screen_save();
210
211                                         /* Display a list of spells */
212                                         print_spells(menu_line, spells, num, 1, 15, use_realm);
213                                 }
214
215                                 /* Hide the list */
216                                 else
217                                 {
218                                         if (use_menu) continue;
219
220                                         /* Hide list */
221                                         redraw = FALSE;
222
223                                         /* Restore the screen */
224                                         screen_load();
225                                 }
226
227                                 /* Redo asking */
228                                 continue;
229                         }
230
231
232                         /* Note verify */
233                         ask = (isupper(choice));
234
235                         /* Lowercase */
236                         if (ask) choice = (char)tolower(choice);
237
238                         /* Extract request */
239                         i = (islower(choice) ? A2I(choice) : -1);
240                 }
241
242                 /* Totally Illegal */
243                 if ((i < 0) || (i >= num))
244                 {
245                         bell();
246                         continue;
247                 }
248
249                 /* Save the spell index */
250                 spell = spells[i];
251
252                 /* Require "okay" spells */
253                 if (!spell_okay(spell, learned, FALSE, use_realm))
254                 {
255                         bell();
256 #ifdef JP
257                         msg_format("その%sを%sことはできません。", p, prompt);
258 #else
259                         msg_format("You may not %s that %s.", prompt, p);
260 #endif
261
262                         continue;
263                 }
264
265                 /* Verify it */
266                 if (ask)
267                 {
268                         char tmp_val[160];
269
270                         /* Access the spell */
271                         if (!is_magic(use_realm))
272                         {
273                                 s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
274                         }
275                         else
276                         {
277                                 s_ptr = &mp_ptr->info[use_realm - 1][spell];
278                         }
279
280                         /* Extract mana consumption rate */
281                         if (use_realm == REALM_HISSATSU)
282                         {
283                                 need_mana = s_ptr->smana;
284                         }
285                         else
286                         {
287                                 need_mana = mod_need_mana(s_ptr->smana, spell, use_realm);
288                         }
289
290                         /* Prompt */
291 #ifdef JP
292                         jverb( prompt, jverb_buf, JVERB_AND );
293                         /* 英日切り替え機能に対応 */
294                         (void) strnfmt(tmp_val, 78, "%s(MP%d, 失敗率%d%%)を%sますか? ",
295                                 do_spell(use_realm, spell, SPELL_NAME), need_mana,
296                                        spell_chance(spell, use_realm),jverb_buf);
297 #else
298                         (void)strnfmt(tmp_val, 78, "%^s %s (%d mana, %d%% fail)? ",
299                                 prompt, do_spell(use_realm, spell, SPELL_NAME), need_mana,
300                                 spell_chance(spell, use_realm));
301 #endif
302
303
304                         /* Belay that order */
305                         if (!get_check(tmp_val)) continue;
306                 }
307
308                 /* Stop the loop */
309                 flag = TRUE;
310         }
311
312
313         /* Restore the screen */
314         if (redraw) screen_load();
315
316
317         /* Show choices */
318         p_ptr->window |= (PW_SPELL);
319
320         /* Window stuff */
321         window_stuff();
322
323
324         /* Abort if needed */
325         if (!flag) return FALSE;
326
327         /* Save the choice */
328         (*sn) = spell;
329
330 #ifdef ALLOW_REPEAT /* TNB */
331
332         repeat_push((COMMAND_CODE)spell);
333
334 #endif /* ALLOW_REPEAT -- TNB */
335
336         /* Success */
337         return TRUE;
338 }
339
340 /*!
341  * @brief オブジェクトがプレイヤーが使用可能な魔道書かどうかを判定する
342  * @param o_ptr 判定したいオブ会ジェクトの構造体参照ポインタ
343  * @return 学習できる魔道書ならばTRUEを返す
344  */
345 static bool item_tester_learn_spell(object_type *o_ptr)
346 {
347         s32b choices = realm_choices2[p_ptr->pclass];
348
349         if (p_ptr->pclass == CLASS_PRIEST)
350         {
351                 if (is_good_realm(p_ptr->realm1))
352                 {
353                         choices &= ~(CH_DEATH | CH_DAEMON);
354                 }
355                 else
356                 {
357                         choices &= ~(CH_LIFE | CH_CRUSADE);
358                 }
359         }
360
361         if ((o_ptr->tval < TV_LIFE_BOOK) || (o_ptr->tval > (TV_LIFE_BOOK + MAX_REALM - 1))) return (FALSE);
362         if ((o_ptr->tval == TV_MUSIC_BOOK) && (p_ptr->pclass == CLASS_BARD)) return (TRUE);
363         else if (!is_magic(tval2realm(o_ptr->tval))) return FALSE;
364         if ((REALM1_BOOK == o_ptr->tval) || (REALM2_BOOK == o_ptr->tval)) return (TRUE);
365         if (choices & (0x0001 << (tval2realm(o_ptr->tval) - 1))) return (TRUE);
366         return (FALSE);
367 }
368
369 /*!
370  * @brief プレイヤーが魔道書を一冊も持っていないかを判定する
371  * @return 魔道書を一冊も持っていないならTRUEを返す
372  */
373 static bool player_has_no_spellbooks(void)
374 {
375         int         i;
376         object_type *o_ptr;
377
378         for (i = 0; i < INVEN_PACK; i++)
379         {
380                 o_ptr = &inventory[i];
381                 if (o_ptr->k_idx && check_book_realm(o_ptr->tval, o_ptr->sval)) return FALSE;
382         }
383
384         for (i = cave[p_ptr->y][p_ptr->x].o_idx; i; i = o_ptr->next_o_idx)
385         {
386                 o_ptr = &o_list[i];
387                 if (o_ptr->k_idx && (o_ptr->marked & OM_FOUND) && check_book_realm(o_ptr->tval, o_ptr->sval)) return FALSE;
388         }
389
390         return TRUE;
391 }
392
393 /*!
394  * @brief プレイヤーの職業が練気術師の時、領域魔法と練気術を切り換える処理のインターフェイス
395  * @param browse_only 魔法と技能の閲覧を行うならばTRUE
396  * @return 魔道書を一冊も持っていないならTRUEを返す
397  */
398 static void confirm_use_force(bool browse_only)
399 {
400         char which;
401         COMMAND_CODE code;
402
403 #ifdef ALLOW_REPEAT
404
405         /* Get the item index */
406         if (repeat_pull(&code) && (code == INVEN_FORCE))
407         {
408                 browse_only ? do_cmd_mind_browse() : do_cmd_mind();
409                 return;
410         }
411
412 #endif /* ALLOW_REPEAT */
413
414         /* Show the prompt */
415         prt(_("('w'練気術, ESC) 'w'かESCを押してください。 ", "(w for the Force, ESC) Hit 'w' or ESC. "), 0, 0);
416
417         while (1)
418         {
419                 /* Get a key */
420                 which = inkey();
421
422                 if (which == ESCAPE) break;
423                 else if (which == 'w')
424                 {
425
426 #ifdef ALLOW_REPEAT
427
428                         repeat_push(INVEN_FORCE);
429
430 #endif /* ALLOW_REPEAT */
431
432                         break;
433                 }
434         }
435
436         /* Clear the prompt line */
437         prt("", 0, 0);
438
439         if (which == 'w')
440         {
441                 browse_only ? do_cmd_mind_browse() : do_cmd_mind();
442         }
443 }
444
445
446 /*!
447  * @brief プレイヤーの魔法と技能を閲覧するコマンドのメインルーチン /
448  * Peruse the spells/prayers in a book
449  * @return なし
450  * @details
451  * <pre>
452  * Note that *all* spells in the book are listed
453  *
454  * Note that browsing is allowed while confused or blind,
455  * and in the dark, primarily to allow browsing in stores.
456  * </pre>
457  */
458 void do_cmd_browse(void)
459 {
460         OBJECT_IDX item;
461         OBJECT_SUBTYPE_VALUE sval;
462         REALM_IDX use_realm = 0;
463         int j, line;
464         SPELL_IDX spell = -1;
465         int num = 0;
466
467         SPELL_IDX spells[64];
468         char temp[62*4];
469
470         object_type     *o_ptr;
471
472         cptr q, s;
473
474         /* Warriors are illiterate */
475         if (!(p_ptr->realm1 || p_ptr->realm2) && (p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE))
476         {
477                 msg_print(_("本を読むことができない!", "You cannot read books!"));
478                 return;
479         }
480
481         if (p_ptr->special_defense & KATA_MUSOU)
482         {
483                 set_action(ACTION_NONE);
484         }
485
486         if (p_ptr->pclass == CLASS_FORCETRAINER)
487         {
488                 if (player_has_no_spellbooks())
489                 {
490                         confirm_use_force(TRUE);
491                         return;
492                 }
493                 select_the_force = TRUE;
494         }
495
496         /* Restrict choices to "useful" books */
497         if (p_ptr->realm2 == REALM_NONE) item_tester_tval = mp_ptr->spell_book;
498         else item_tester_hook = item_tester_learn_spell;
499
500         /* Get an item */
501         q = _("どの本を読みますか? ", "Browse which book? ");
502         s = _("読める本がない。", "You have no books that you can read.");
503
504         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR)))
505         {
506                 select_the_force = FALSE;
507                 return;
508         }
509         select_the_force = FALSE;
510
511         if (item == INVEN_FORCE) /* the_force */
512         {
513                 do_cmd_mind_browse();
514                 return;
515         }
516
517         /* Get the item (in the pack) */
518         else if (item >= 0)
519         {
520                 o_ptr = &inventory[item];
521         }
522
523         /* Get the item (on the floor) */
524         else
525         {
526                 o_ptr = &o_list[0 - item];
527         }
528
529         /* Access the item's sval */
530         sval = o_ptr->sval;
531
532         use_realm = tval2realm(o_ptr->tval);
533
534         /* Track the object kind */
535         object_kind_track(o_ptr->k_idx);
536
537         /* Hack -- Handle stuff */
538         handle_stuff();
539
540
541         /* Extract spells */
542         for (spell = 0; spell < 32; spell++)
543         {
544                 /* Check for this spell */
545                 if ((fake_spell_flags[sval] & (1L << spell)))
546                 {
547                         /* Collect this spell */
548                         spells[num++] = spell;
549                 }
550         }
551
552
553         /* Save the screen */
554         screen_save();
555
556         /* Clear the top line */
557         prt("", 0, 0);
558
559         /* Keep browsing spells.  Exit browsing on cancel. */
560         while(TRUE)
561         {
562                 /* Ask for a spell, allow cancel */
563                 if (!get_spell(&spell, _("読む", "browse"), o_ptr->sval, TRUE, use_realm))
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                                 prt(_("読める技がない。", "No techniques to browse."), 0, 0);
574                         else
575                                 prt(_("読める呪文がない。", "No spells to browse."), 0, 0);
576                         (void)inkey();
577
578
579                         /* Restore the screen */
580                         screen_load();
581
582                         return;
583                 }
584
585                 /* Clear lines, position cursor  (really should use strlen here) */
586                 Term_erase(14, 14, 255);
587                 Term_erase(14, 13, 255);
588                 Term_erase(14, 12, 255);
589                 Term_erase(14, 11, 255);
590
591                 roff_to_buf(do_spell(use_realm, spell, SPELL_DESC), 62, temp, sizeof(temp));
592
593                 for (j = 0, line = 11; temp[j]; j += 1 + strlen(&temp[j]))
594                 {
595                         prt(&temp[j], line, 15);
596                         line++;
597                 }
598         }
599
600         /* Restore the screen */
601         screen_load();
602 }
603
604 /*!
605  * @brief プレイヤーの第二魔法領域を変更する /
606  * @param next_realm 変更先の魔法領域ID
607  * @return なし
608  */
609 static void change_realm2(CHARACTER_IDX next_realm)
610 {
611         int i, j = 0;
612         char tmp[80];
613
614         for (i = 0; i < 64; i++)
615         {
616                 p_ptr->spell_order[j] = p_ptr->spell_order[i];
617                 if (p_ptr->spell_order[i] < 32) j++;
618         }
619         for (; j < 64; j++)
620                 p_ptr->spell_order[j] = 99;
621
622         for (i = 32; i < 64; i++)
623         {
624                 p_ptr->spell_exp[i] = SPELL_EXP_UNSKILLED;
625         }
626         p_ptr->spell_learned2 = 0L;
627         p_ptr->spell_worked2 = 0L;
628         p_ptr->spell_forgotten2 = 0L;
629
630         sprintf(tmp,_("魔法の領域を%sから%sに変更した。", "change magic realm from %s to %s."), realm_names[p_ptr->realm2], realm_names[next_realm]);
631         do_cmd_write_nikki(NIKKI_BUNSHOU, 0, tmp);
632         p_ptr->old_realm |= 1 << (p_ptr->realm2-1);
633         p_ptr->realm2 = next_realm;
634
635         p_ptr->notice |= (PN_REORDER);
636         p_ptr->update |= (PU_SPELLS);
637         handle_stuff();
638
639         /* Load an autopick preference file */
640         autopick_load_pref(FALSE);
641 }
642
643
644 /*!
645  * @brief 魔法を学習するコマンドのメインルーチン /
646  * Study a book to gain a new spell/prayer
647  * @return なし
648  */
649 void do_cmd_study(void)
650 {
651         int     i;
652         OBJECT_IDX item;
653         OBJECT_SUBTYPE_VALUE sval;
654         int     increment = 0;
655         bool    learned = FALSE;
656
657         /* Spells of realm2 will have an increment of +32 */
658         SPELL_IDX spell = -1;
659
660         cptr p = spell_category_name(mp_ptr->spell_book);
661
662         object_type *o_ptr;
663
664         cptr q, s;
665
666         if (!p_ptr->realm1)
667         {
668                 msg_print(_("本を読むことができない!", "You cannot read books!"));
669                 return;
670         }
671
672         if (p_ptr->blind || no_lite())
673         {
674                 msg_print(_("目が見えない!", "You cannot see!"));
675                 return;
676         }
677
678         if (p_ptr->confused)
679         {
680                 msg_print(_("混乱していて読めない!", "You are too confused!"));
681                 return;
682         }
683
684         if (!(p_ptr->new_spells))
685         {
686                 msg_format(_("新しい%sを覚えることはできない!", "You cannot learn any new %ss!"), p);
687                 return;
688         }
689
690         if (p_ptr->special_defense & KATA_MUSOU)
691         {
692                 set_action(ACTION_NONE);
693         }
694
695 #ifdef JP
696         if( p_ptr->new_spells < 10 ){
697                 msg_format("あと %d つの%sを学べる。", p_ptr->new_spells, p);
698         }else{
699                 msg_format("あと %d 個の%sを学べる。", p_ptr->new_spells, p);
700         }
701 #else
702         msg_format("You can learn %d new %s%s.", p_ptr->new_spells, p,
703                 (p_ptr->new_spells == 1?"":"s"));
704 #endif
705
706         msg_print(NULL);
707
708
709         /* Restrict choices to "useful" books */
710         if (p_ptr->realm2 == REALM_NONE) item_tester_tval = mp_ptr->spell_book;
711         else item_tester_hook = item_tester_learn_spell;
712
713         /* Get an item */
714         q = _("どの本から学びますか? ", "Study which book? ");
715         s = _("読める本がない。", "You have no books that you can read.");
716
717         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
718
719         /* Get the item (in the pack) */
720         if (item >= 0)
721         {
722                 o_ptr = &inventory[item];
723         }
724
725         /* Get the item (on the floor) */
726         else
727         {
728                 o_ptr = &o_list[0 - item];
729         }
730
731         /* Access the item's sval */
732         sval = o_ptr->sval;
733
734         if (o_ptr->tval == REALM2_BOOK) increment = 32;
735         else if (o_ptr->tval != REALM1_BOOK)
736         {
737                 if (!get_check(_("本当に魔法の領域を変更しますか?", "Really, change magic realm? "))) return;
738                 change_realm2(tval2realm(o_ptr->tval));
739                 increment = 32;
740         }
741
742         /* Track the object kind */
743         object_kind_track(o_ptr->k_idx);
744
745         /* Hack -- Handle stuff */
746         handle_stuff();
747
748         /* Mage -- Learn a selected spell */
749         if (mp_ptr->spell_book != TV_LIFE_BOOK)
750         {
751                 /* Ask for a spell, allow cancel */
752 #ifdef JP
753                 if (!get_spell(&spell, "学ぶ", sval, FALSE, o_ptr->tval - TV_LIFE_BOOK + 1)
754                         && (spell == -1)) return;
755 #else
756                 if (!get_spell(&spell, "study", sval, FALSE, o_ptr->tval - TV_LIFE_BOOK + 1)
757                         && (spell == -1)) return;
758 #endif
759
760         }
761
762         /* Priest -- Learn a random prayer */
763         else
764         {
765                 int k = 0;
766
767                 int gift = -1;
768
769                 /* Extract spells */
770                 for (spell = 0; spell < 32; spell++)
771                 {
772                         /* Check spells in the book */
773                         if ((fake_spell_flags[sval] & (1L << spell)))
774                         {
775                                 /* Skip non "okay" prayers */
776                                 if (!spell_okay(spell, FALSE, TRUE,
777                                         (increment ? p_ptr->realm2 : p_ptr->realm1))) continue;
778
779                                 /* Hack -- Prepare the randomizer */
780                                 k++;
781
782                                 /* Hack -- Apply the randomizer */
783                                 if (one_in_(k)) gift = spell;
784                         }
785                 }
786
787                 /* Accept gift */
788                 spell = gift;
789         }
790
791         /* Nothing to study */
792         if (spell < 0)
793         {
794                 /* Message */
795                 msg_format(_("その本には学ぶべき%sがない。", "You cannot learn any %ss in that book."), p);
796
797                 /* Abort */
798                 return;
799         }
800
801
802         if (increment) spell += increment;
803
804         /* Learn the spell */
805         if (spell < 32)
806         {
807                 if (p_ptr->spell_learned1 & (1L << spell)) learned = TRUE;
808                 else p_ptr->spell_learned1 |= (1L << spell);
809         }
810         else
811         {
812                 if (p_ptr->spell_learned2 & (1L << (spell - 32))) learned = TRUE;
813                 else p_ptr->spell_learned2 |= (1L << (spell - 32));
814         }
815
816         if (learned)
817         {
818                 int max_exp = (spell < 32) ? SPELL_EXP_MASTER : SPELL_EXP_EXPERT;
819                 int old_exp = p_ptr->spell_exp[spell];
820                 int new_rank = EXP_LEVEL_UNSKILLED;
821                 cptr name = do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell%32, SPELL_NAME);
822
823                 if (old_exp >= max_exp)
824                 {
825                         msg_format(_("その%sは完全に使いこなせるので学ぶ必要はない。", "You don't need to study this %s anymore."), p);
826                         return;
827                 }
828 #ifdef JP
829                 if (!get_check(format("%sの%sをさらに学びます。よろしいですか?", name, p)))
830 #else
831                 if (!get_check(format("You will study a %s of %s again. Are you sure? ", p, name)))
832 #endif
833                 {
834                         return;
835                 }
836                 else if (old_exp >= SPELL_EXP_EXPERT)
837                 {
838                         p_ptr->spell_exp[spell] = SPELL_EXP_MASTER;
839                         new_rank = EXP_LEVEL_MASTER;
840                 }
841                 else if (old_exp >= SPELL_EXP_SKILLED)
842                 {
843                         if (spell >= 32) p_ptr->spell_exp[spell] = SPELL_EXP_EXPERT;
844                         else p_ptr->spell_exp[spell] += SPELL_EXP_EXPERT - SPELL_EXP_SKILLED;
845                         new_rank = EXP_LEVEL_EXPERT;
846                 }
847                 else if (old_exp >= SPELL_EXP_BEGINNER)
848                 {
849                         p_ptr->spell_exp[spell] = SPELL_EXP_SKILLED + (old_exp - SPELL_EXP_BEGINNER) * 2 / 3;
850                         new_rank = EXP_LEVEL_SKILLED;
851                 }
852                 else
853                 {
854                         p_ptr->spell_exp[spell] = SPELL_EXP_BEGINNER + old_exp / 3;
855                         new_rank = EXP_LEVEL_BEGINNER;
856                 }
857                 msg_format(_("%sの熟練度が%sに上がった。", "Your proficiency of %s is now %s rank."), name, exp_level_str[new_rank]);
858         }
859         else
860         {
861                 /* Find the next open entry in "p_ptr->spell_order[]" */
862                 for (i = 0; i < 64; i++)
863                 {
864                         /* Stop at the first empty space */
865                         if (p_ptr->spell_order[i] == 99) break;
866                 }
867
868                 /* Add the spell to the known list */
869                 p_ptr->spell_order[i++] = spell;
870
871                 /* Mention the result */
872 #ifdef JP
873                 /* 英日切り替え機能に対応 */
874                 if (mp_ptr->spell_book == TV_MUSIC_BOOK)
875                 {
876                         msg_format("%sを学んだ。",
877                                     do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell % 32, SPELL_NAME));
878                 }
879                 else
880                 {
881                         msg_format("%sの%sを学んだ。",
882                                     do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell % 32, SPELL_NAME) ,p);
883                 }
884 #else
885                 msg_format("You have learned the %s of %s.",
886                         p, do_spell(increment ? p_ptr->realm2 : p_ptr->realm1, spell % 32, SPELL_NAME));
887 #endif
888         }
889
890         /* Take a turn */
891         p_ptr->energy_use = 100;
892
893         switch (mp_ptr->spell_book)
894         {
895         case TV_LIFE_BOOK:
896                 chg_virtue(V_FAITH, 1);
897                 break;
898         case TV_DEATH_BOOK:
899                 chg_virtue(V_UNLIFE, 1);
900                 break;
901         case TV_NATURE_BOOK:
902                 chg_virtue(V_NATURE, 1);
903                 break;
904         default:
905                 chg_virtue(V_KNOWLEDGE, 1);
906                 break;
907         }
908
909         /* Sound */
910         sound(SOUND_STUDY);
911
912         /* One less spell available */
913         p_ptr->learned_spells++;
914 #if 0
915         /* Message if needed */
916         if (p_ptr->new_spells)
917         {
918                 /* Message */
919 #ifdef JP
920                 if (p_ptr->new_spells < 10) msg_format("あと %d つの%sを学べる。", p_ptr->new_spells, p);
921                 else msg_format("あと %d 個の%sを学べる。", p_ptr->new_spells, p);
922 #else
923                 msg_format("You can learn %d more %s%s.", p_ptr->new_spells, p,
924                            (p_ptr->new_spells != 1) ? "s" : "");
925 #endif
926         }
927 #endif
928
929         /* Update Study */
930         p_ptr->update |= (PU_SPELLS);
931         update_stuff();
932
933         /* Redraw object recall */
934         p_ptr->window |= (PW_OBJECT);
935 }
936
937
938 /*!
939  * @brief 魔法を詠唱するコマンドのメインルーチン /
940  * Cast a spell
941  * @return なし
942  */
943 void do_cmd_cast(void)
944 {
945         OBJECT_IDX item;
946         OBJECT_SUBTYPE_VALUE sval;
947         SPELL_IDX spell;
948         REALM_IDX realm;
949         int     chance;
950         int     increment = 0;
951         REALM_IDX use_realm;
952         MANA_POINT need_mana;
953
954         cptr prayer;
955         object_type     *o_ptr;
956         const magic_type *s_ptr;
957         cptr q, s;
958
959         bool over_exerted = FALSE;
960
961         /* Require spell ability */
962         if (!p_ptr->realm1 && (p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE))
963         {
964                 msg_print(_("呪文を唱えられない!", "You cannot cast spells!"));
965                 return;
966         }
967
968         /* Require lite */
969         if (p_ptr->blind || no_lite())
970         {
971                 if (p_ptr->pclass == CLASS_FORCETRAINER) confirm_use_force(FALSE);
972                 else
973                 {
974                         msg_print(_("目が見えない!", "You cannot see!"));
975                         flush();
976                 }
977                 return;
978         }
979
980         /* Not when confused */
981         if (p_ptr->confused)
982         {
983                 msg_print(_("混乱していて唱えられない!", "You are too confused!"));
984                 flush();
985                 return;
986         }
987
988         /* Hex */
989         if (p_ptr->realm1 == REALM_HEX)
990         {
991                 if (hex_spell_fully())
992                 {
993                         bool flag = FALSE;
994                         msg_print(_("これ以上新しい呪文を詠唱することはできない。", "Can not spell new spells more."));
995                         flush();
996                         if (p_ptr->lev >= 35) flag = stop_hex_spell();
997                         if (!flag) return;
998                 }
999         }
1000
1001         if (p_ptr->pclass == CLASS_FORCETRAINER)
1002         {
1003                 if (player_has_no_spellbooks())
1004                 {
1005                         confirm_use_force(FALSE);
1006                         return;
1007                 }
1008                 select_the_force = TRUE;
1009         }
1010
1011         prayer = spell_category_name(mp_ptr->spell_book);
1012
1013         /* Restrict choices to spell books */
1014         item_tester_tval = mp_ptr->spell_book;
1015
1016         /* Get an item */
1017         q = _("どの呪文書を使いますか? ", "Use which book? ");
1018         s = _("呪文書がない!", "You have no spell books!");
1019
1020         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR)))
1021         {
1022                 select_the_force = FALSE;
1023                 return;
1024         }
1025         select_the_force = FALSE;
1026
1027         if (item == INVEN_FORCE) /* the_force */
1028         {
1029                 do_cmd_mind();
1030                 return;
1031         }
1032
1033         /* Get the item (in the pack) */
1034         else if (item >= 0)
1035         {
1036                 o_ptr = &inventory[item];
1037         }
1038
1039         /* Get the item (on the floor) */
1040         else
1041         {
1042                 o_ptr = &o_list[0 - item];
1043         }
1044
1045         /* Access the item's sval */
1046         sval = o_ptr->sval;
1047
1048         if ((p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE) && (o_ptr->tval == REALM2_BOOK)) increment = 32;
1049
1050
1051         /* Track the object kind */
1052         object_kind_track(o_ptr->k_idx);
1053
1054         /* Hack -- Handle stuff */
1055         handle_stuff();
1056
1057         if ((p_ptr->pclass == CLASS_SORCERER) || (p_ptr->pclass == CLASS_RED_MAGE))
1058                 realm = o_ptr->tval - TV_LIFE_BOOK + 1;
1059         else if (increment) realm = p_ptr->realm2;
1060         else realm = p_ptr->realm1;
1061
1062         /* Ask for a spell */
1063 #ifdef JP
1064         if (!get_spell(&spell, ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "詠唱する" : (mp_ptr->spell_book == TV_MUSIC_BOOK) ? "歌う" : "唱える"), 
1065                         sval, TRUE, realm))
1066         {
1067                 if (spell == -2) msg_format("その本には知っている%sがない。", prayer);
1068                 return;
1069         }
1070 #else
1071         if (!get_spell(&spell, ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "recite" : "cast"),
1072                 sval, TRUE, realm))
1073         {
1074                 if (spell == -2)
1075                         msg_format("You don't know any %ss in that book.", prayer);
1076                 return;
1077         }
1078 #endif
1079
1080
1081         use_realm = tval2realm(o_ptr->tval);
1082
1083         /* Hex */
1084         if (use_realm == REALM_HEX)
1085         {
1086                 if (hex_spelling(spell))
1087                 {
1088                         msg_print(_("その呪文はすでに詠唱中だ。", "You are already casting it."));
1089                         return;
1090                 }
1091         }
1092
1093         if (!is_magic(use_realm))
1094         {
1095                 s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
1096         }
1097         else
1098         {
1099                 s_ptr = &mp_ptr->info[realm - 1][spell];
1100         }
1101
1102         /* Extract mana consumption rate */
1103         need_mana = mod_need_mana(s_ptr->smana, spell, realm);
1104
1105         /* Verify "dangerous" spells */
1106         if (need_mana > p_ptr->csp)
1107         {
1108                 if (flush_failure) flush();
1109
1110                 /* Warning */
1111 #ifdef JP
1112 msg_format("その%sを%sのに十分なマジックポイントがない。",prayer,
1113  ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "詠唱する" : (mp_ptr->spell_book == TV_LIFE_BOOK) ? "歌う" : "唱える"));
1114 #else
1115                 msg_format("You do not have enough mana to %s this %s.",
1116                         ((mp_ptr->spell_book == TV_LIFE_BOOK) ? "recite" : "cast"),
1117                         prayer);
1118 #endif
1119
1120
1121                 if (!over_exert) return;
1122
1123                 /* Verify */
1124                 if (!get_check_strict(_("それでも挑戦しますか? ", "Attempt it anyway? "), CHECK_OKAY_CANCEL)) return;
1125         }
1126
1127         /* Spell failure chance */
1128         chance = spell_chance(spell, use_realm);
1129
1130         /* Sufficient mana */
1131         if (need_mana <= p_ptr->csp)
1132         {
1133                 /* Use some mana */
1134                 p_ptr->csp -= need_mana;
1135         }
1136         else over_exerted = TRUE;
1137
1138         /* Redraw mana */
1139         p_ptr->redraw |= (PR_MANA);
1140
1141         /* Failed spell */
1142         if (randint0(100) < chance)
1143         {
1144                 if (flush_failure) flush();
1145                 
1146                 msg_format(_("%sをうまく唱えられなかった!", "You failed to get the %s off!"), prayer);
1147                 sound(SOUND_FAIL);
1148
1149                 switch (realm)
1150                 {
1151                 case REALM_LIFE:
1152                         if (randint1(100) < chance) chg_virtue(V_VITALITY, -1);
1153                         break;
1154                 case REALM_DEATH:
1155                         if (randint1(100) < chance) chg_virtue(V_UNLIFE, -1);
1156                         break;
1157                 case REALM_NATURE:
1158                         if (randint1(100) < chance) chg_virtue(V_NATURE, -1);
1159                         break;
1160                 case REALM_DAEMON:
1161                         if (randint1(100) < chance) chg_virtue(V_JUSTICE, 1);
1162                         break;
1163                 case REALM_CRUSADE:
1164                         if (randint1(100) < chance) chg_virtue(V_JUSTICE, -1);
1165                         break;
1166                 case REALM_HEX:
1167                         if (randint1(100) < chance) chg_virtue(V_COMPASSION, -1);
1168                         break;
1169                 default:
1170                         if (randint1(100) < chance) chg_virtue(V_KNOWLEDGE, -1);
1171                         break;
1172                 }
1173
1174                 /* Failure casting may activate some side effect */
1175                 do_spell(realm, spell, SPELL_FAIL);
1176
1177
1178                 if ((o_ptr->tval == TV_CHAOS_BOOK) && (randint1(100) < spell))
1179                 {
1180                         msg_print(_("カオス的な効果を発生した!", "You produce a chaotic effect!"));
1181                         wild_magic(spell);
1182                 }
1183                 else if ((o_ptr->tval == TV_DEATH_BOOK) && (randint1(100) < spell))
1184                 {
1185                         if ((sval == 3) && one_in_(2))
1186                         {
1187                                 sanity_blast(0, TRUE);
1188                         }
1189                         else
1190                         {
1191                                 msg_print(_("痛い!", "It hurts!"));
1192                                 take_hit(DAMAGE_LOSELIFE, damroll(o_ptr->sval + 1, 6), _("暗黒魔法の逆流", "a miscast Death spell"), -1);
1193
1194                                 if ((spell > 15) && one_in_(6) && !p_ptr->hold_exp)
1195                                         lose_exp(spell * 250);
1196                         }
1197                 }
1198                 else if ((o_ptr->tval == TV_MUSIC_BOOK) && (randint1(200) < spell))
1199                 {
1200                         msg_print(_("いやな音が響いた", "An infernal sound echoed."));
1201                         aggravate_monsters(0);
1202                 }
1203                 if (randint1(100) >= chance)
1204                         chg_virtue(V_CHANCE,-1);
1205         }
1206
1207         /* Process spell */
1208         else
1209         {
1210                 /* Canceled spells cost neither a turn nor mana */
1211                 if (!do_spell(realm, spell, SPELL_CAST)) return;
1212
1213                 if (randint1(100) < chance)
1214                         chg_virtue(V_CHANCE,1);
1215
1216                 /* A spell was cast */
1217                 if (!(increment ?
1218                     (p_ptr->spell_worked2 & (1L << spell)) :
1219                     (p_ptr->spell_worked1 & (1L << spell)))
1220                     && (p_ptr->pclass != CLASS_SORCERER)
1221                     && (p_ptr->pclass != CLASS_RED_MAGE))
1222                 {
1223                         int e = s_ptr->sexp;
1224
1225                         /* The spell worked */
1226                         if (realm == p_ptr->realm1)
1227                         {
1228                                 p_ptr->spell_worked1 |= (1L << spell);
1229                         }
1230                         else
1231                         {
1232                                 p_ptr->spell_worked2 |= (1L << spell);
1233                         }
1234
1235                         /* Gain experience */
1236                         gain_exp(e * s_ptr->slevel);
1237
1238                         /* Redraw object recall */
1239                         p_ptr->window |= (PW_OBJECT);
1240
1241                         switch (realm)
1242                         {
1243                         case REALM_LIFE:
1244                                 chg_virtue(V_TEMPERANCE, 1);
1245                                 chg_virtue(V_COMPASSION, 1);
1246                                 chg_virtue(V_VITALITY, 1);
1247                                 chg_virtue(V_DILIGENCE, 1);
1248                                 break;
1249                         case REALM_DEATH:
1250                                 chg_virtue(V_UNLIFE, 1);
1251                                 chg_virtue(V_JUSTICE, -1);
1252                                 chg_virtue(V_FAITH, -1);
1253                                 chg_virtue(V_VITALITY, -1);
1254                                 break;
1255                         case REALM_DAEMON:
1256                                 chg_virtue(V_JUSTICE, -1);
1257                                 chg_virtue(V_FAITH, -1);
1258                                 chg_virtue(V_HONOUR, -1);
1259                                 chg_virtue(V_TEMPERANCE, -1);
1260                                 break;
1261                         case REALM_CRUSADE:
1262                                 chg_virtue(V_FAITH, 1);
1263                                 chg_virtue(V_JUSTICE, 1);
1264                                 chg_virtue(V_SACRIFICE, 1);
1265                                 chg_virtue(V_HONOUR, 1);
1266                                 break;
1267                         case REALM_NATURE:
1268                                 chg_virtue(V_NATURE, 1);
1269                                 chg_virtue(V_HARMONY, 1);
1270                                 break;
1271                         case REALM_HEX:
1272                                 chg_virtue(V_JUSTICE, -1);
1273                                 chg_virtue(V_FAITH, -1);
1274                                 chg_virtue(V_HONOUR, -1);
1275                                 chg_virtue(V_COMPASSION, -1);
1276                                 break;
1277                         default:
1278                                 chg_virtue(V_KNOWLEDGE, 1);
1279                                 break;
1280                         }
1281                 }
1282                 switch (realm)
1283                 {
1284                 case REALM_LIFE:
1285                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_TEMPERANCE, 1);
1286                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_COMPASSION, 1);
1287                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_VITALITY, 1);
1288                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_DILIGENCE, 1);
1289                         break;
1290                 case REALM_DEATH:
1291                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_UNLIFE, 1);
1292                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, -1);
1293                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, -1);
1294                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_VITALITY, -1);
1295                         break;
1296                 case REALM_DAEMON:
1297                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, -1);
1298                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, -1);
1299                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HONOUR, -1);
1300                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_TEMPERANCE, -1);
1301                         break;
1302                 case REALM_CRUSADE:
1303                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, 1);
1304                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, 1);
1305                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_SACRIFICE, 1);
1306                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HONOUR, 1);
1307                         break;
1308                 case REALM_NATURE:
1309                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_NATURE, 1);
1310                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HARMONY, 1);
1311                         break;
1312                 case REALM_HEX:
1313                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_JUSTICE, -1);
1314                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_FAITH, -1);
1315                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_HONOUR, -1);
1316                         if (randint1(100 + p_ptr->lev) < need_mana) chg_virtue(V_COMPASSION, -1);
1317                         break;
1318                 }
1319                 if (mp_ptr->spell_xtra & MAGIC_GAIN_EXP)
1320                 {
1321                         s16b cur_exp = p_ptr->spell_exp[(increment ? 32 : 0)+spell];
1322                         s16b exp_gain = 0;
1323
1324                         if (cur_exp < SPELL_EXP_BEGINNER)
1325                                 exp_gain += 60;
1326                         else if (cur_exp < SPELL_EXP_SKILLED)
1327                         {
1328                                 if ((dun_level > 4) && ((dun_level + 10) > p_ptr->lev))
1329                                         exp_gain = 8;
1330                         }
1331                         else if (cur_exp < SPELL_EXP_EXPERT)
1332                         {
1333                                 if (((dun_level + 5) > p_ptr->lev) && ((dun_level + 5) > s_ptr->slevel))
1334                                         exp_gain = 2;
1335                         }
1336                         else if ((cur_exp < SPELL_EXP_MASTER) && !increment)
1337                         {
1338                                 if (((dun_level + 5) > p_ptr->lev) && (dun_level > s_ptr->slevel))
1339                                         exp_gain = 1;
1340                         }
1341                         p_ptr->spell_exp[(increment ? 32 : 0) + spell] += exp_gain;
1342                 }
1343         }
1344
1345         /* Take a turn */
1346         p_ptr->energy_use = 100;
1347
1348
1349         /* Over-exert the player */
1350         if(over_exerted)
1351         {
1352                 int oops = need_mana;
1353
1354                 /* No mana left */
1355                 p_ptr->csp = 0;
1356                 p_ptr->csp_frac = 0;
1357
1358                 /* Message */
1359                 msg_print(_("精神を集中しすぎて気を失ってしまった!", "You faint from the effort!"));
1360
1361                 /* Hack -- Bypass free action */
1362                 (void)set_paralyzed(p_ptr->paralyzed + randint1(5 * oops + 1));
1363
1364                 switch (realm)
1365                 {
1366                 case REALM_LIFE:
1367                         chg_virtue(V_VITALITY, -10);
1368                         break;
1369                 case REALM_DEATH:
1370                         chg_virtue(V_UNLIFE, -10);
1371                         break;
1372                 case REALM_DAEMON:
1373                         chg_virtue(V_JUSTICE, 10);
1374                         break;
1375                 case REALM_NATURE:
1376                         chg_virtue(V_NATURE, -10);
1377                         break;
1378                 case REALM_CRUSADE:
1379                         chg_virtue(V_JUSTICE, -10);
1380                         break;
1381                 case REALM_HEX:
1382                         chg_virtue(V_COMPASSION, 10);
1383                         break;
1384                 default:
1385                         chg_virtue(V_KNOWLEDGE, -10);
1386                         break;
1387                 }
1388
1389                 /* Damage CON (possibly permanently) */
1390                 if (randint0(100) < 50)
1391                 {
1392                         bool perm = (randint0(100) < 25);
1393
1394                         /* Message */
1395                         msg_print(_("体を悪くしてしまった!", "You have damaged your health!"));
1396
1397                         /* Reduce constitution */
1398                         (void)dec_stat(A_CON, 15 + randint1(10), perm);
1399                 }
1400         }
1401
1402         /* Window stuff */
1403         p_ptr->window |= (PW_PLAYER);
1404         p_ptr->window |= (PW_SPELL);
1405 }
1406
1407 /*!
1408  * @brief ペットになっているモンスターをソートするための比較処理
1409  * @param u モンスターの構造体配列
1410  * @param v 未使用
1411  * @param a 比較対象のモンスターID1
1412  * @param b 比較対象のモンスターID2
1413  * @return 2番目が大ならばTRUEを返す
1414  */
1415 static bool ang_sort_comp_pet_dismiss(vptr u, vptr v, int a, int b)
1416 {
1417         u16b *who = (u16b*)(u);
1418
1419         int w1 = who[a];
1420         int w2 = who[b];
1421
1422         monster_type *m_ptr1 = &m_list[w1];
1423         monster_type *m_ptr2 = &m_list[w2];
1424         monster_race *r_ptr1 = &r_info[m_ptr1->r_idx];
1425         monster_race *r_ptr2 = &r_info[m_ptr2->r_idx];
1426
1427         /* Unused */
1428         (void)v;
1429
1430         if (w1 == p_ptr->riding) return TRUE;
1431         if (w2 == p_ptr->riding) return FALSE;
1432
1433         if (m_ptr1->nickname && !m_ptr2->nickname) return TRUE;
1434         if (m_ptr2->nickname && !m_ptr1->nickname) return FALSE;
1435
1436         if (!m_ptr1->parent_m_idx && m_ptr2->parent_m_idx) return TRUE;
1437         if (!m_ptr2->parent_m_idx && m_ptr1->parent_m_idx) return FALSE;
1438
1439         if ((r_ptr1->flags1 & RF1_UNIQUE) && !(r_ptr2->flags1 & RF1_UNIQUE)) return TRUE;
1440         if ((r_ptr2->flags1 & RF1_UNIQUE) && !(r_ptr1->flags1 & RF1_UNIQUE)) return FALSE;
1441
1442         if (r_ptr1->level > r_ptr2->level) return TRUE;
1443         if (r_ptr2->level > r_ptr1->level) return FALSE;
1444
1445         if (m_ptr1->hp > m_ptr2->hp) return TRUE;
1446         if (m_ptr2->hp > m_ptr1->hp) return FALSE;
1447         
1448         return w1 <= w2;
1449 }
1450
1451 /*!
1452  * @brief ペットの善悪属性に応じた維持コストの途中計算処理
1453  * @param m_ptr 計算基準となるモンスターの構造体参照ポインタ
1454  * @param inc m_ptrで指定したモンスターを維持コスト計算に加えるならTRUE、外すならFALSEを指定
1455  * @return なし
1456  */
1457 void check_pets_num_and_align(monster_type *m_ptr, bool inc)
1458 {
1459         s32b old_friend_align = friend_align;
1460         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1461
1462         if (inc)
1463         {
1464                 total_friends++;
1465                 if (r_ptr->flags3 & RF3_GOOD) friend_align += r_ptr->level;
1466                 if (r_ptr->flags3 & RF3_EVIL) friend_align -= r_ptr->level;
1467         }
1468         else
1469         {
1470                 total_friends--;
1471                 if (r_ptr->flags3 & RF3_GOOD) friend_align -= r_ptr->level;
1472                 if (r_ptr->flags3 & RF3_EVIL) friend_align += r_ptr->level;
1473         }
1474
1475         if (old_friend_align != friend_align) p_ptr->update |= (PU_BONUS);
1476 }
1477
1478 /*!
1479  * @brief ペットの維持コスト計算
1480  * @return 維持コスト(%)
1481  */
1482 int calculate_upkeep(void)
1483 {
1484         s32b old_friend_align = friend_align;
1485         MONSTER_IDX m_idx;
1486         bool have_a_unique = FALSE;
1487         s32b total_friend_levels = 0;
1488
1489         total_friends = 0;
1490         friend_align = 0;
1491
1492         for (m_idx = m_max - 1; m_idx >=1; m_idx--)
1493         {
1494                 monster_type *m_ptr;
1495                 monster_race *r_ptr;
1496                 
1497                 m_ptr = &m_list[m_idx];
1498                 if (!m_ptr->r_idx) continue;
1499                 r_ptr = &r_info[m_ptr->r_idx];
1500
1501                 if (is_pet(m_ptr))
1502                 {
1503                         total_friends++;
1504                         if (r_ptr->flags1 & RF1_UNIQUE)
1505                         {
1506                                 if (p_ptr->pclass == CLASS_CAVALRY)
1507                                 {
1508                                         if (p_ptr->riding == m_idx)
1509                                                 total_friend_levels += (r_ptr->level+5)*2;
1510                                         else if (!have_a_unique && (r_info[m_ptr->r_idx].flags7 & RF7_RIDING))
1511                                                 total_friend_levels += (r_ptr->level+5)*7/2;
1512                                         else
1513                                                 total_friend_levels += (r_ptr->level+5)*10;
1514                                         have_a_unique = TRUE;
1515                                 }
1516                                 else
1517                                         total_friend_levels += (r_ptr->level+5)*10;
1518                         }
1519                         else
1520                                 total_friend_levels += r_ptr->level;
1521
1522                         /* Determine pet alignment */
1523                         if (r_ptr->flags3 & RF3_GOOD) friend_align += r_ptr->level;
1524                         if (r_ptr->flags3 & RF3_EVIL) friend_align -= r_ptr->level;
1525                 }
1526         }
1527         if (old_friend_align != friend_align) p_ptr->update |= (PU_BONUS);
1528         if (total_friends)
1529         {
1530                 int upkeep_factor;
1531                 upkeep_factor = (total_friend_levels - (p_ptr->lev * 80 / (cp_ptr->pet_upkeep_div)));
1532                 if (upkeep_factor < 0) upkeep_factor = 0;
1533                 if (upkeep_factor > 1000) upkeep_factor = 1000;
1534                 return upkeep_factor;
1535         }
1536         else
1537                 return 0;
1538 }
1539
1540 /*!
1541  * @brief ペットを開放するコマンドのメインルーチン
1542  * @return なし
1543  */
1544 void do_cmd_pet_dismiss(void)
1545 {
1546         monster_type    *m_ptr;
1547         bool            all_pets = FALSE;
1548         MONSTER_IDX pet_ctr;
1549         int i;
1550         int Dismissed = 0;
1551
1552         MONSTER_IDX *who;
1553         u16b dummy_why;
1554         int max_pet = 0;
1555         bool_hack cu, cv;
1556
1557         cu = Term->scr->cu;
1558         cv = Term->scr->cv;
1559         Term->scr->cu = 0;
1560         Term->scr->cv = 1;
1561
1562         /* Allocate the "who" array */
1563         C_MAKE(who, max_m_idx, MONSTER_IDX);
1564
1565         /* Process the monsters (backwards) */
1566         for (pet_ctr = m_max - 1; pet_ctr >= 1; pet_ctr--)
1567         {
1568                 if (is_pet(&m_list[pet_ctr]))
1569                         who[max_pet++] = pet_ctr;
1570         }
1571
1572         /* Select the sort method */
1573         ang_sort_comp = ang_sort_comp_pet_dismiss;
1574         ang_sort_swap = ang_sort_swap_hook;
1575
1576         ang_sort(who, &dummy_why, max_pet);
1577
1578         /* Process the monsters (backwards) */
1579         for (i = 0; i < max_pet; i++)
1580         {
1581                 bool delete_this;
1582                 char friend_name[80];
1583                 bool kakunin;
1584
1585                 /* Access the monster */
1586                 pet_ctr = who[i];
1587                 m_ptr = &m_list[pet_ctr];
1588
1589                 delete_this = FALSE;
1590                 kakunin = ((pet_ctr == p_ptr->riding) || (m_ptr->nickname));
1591                 monster_desc(friend_name, m_ptr, MD_ASSUME_VISIBLE);
1592
1593                 if (!all_pets)
1594                 {
1595                         /* Hack -- health bar for this monster */
1596                         health_track(pet_ctr);
1597
1598                         /* Hack -- handle stuff */
1599                         handle_stuff();
1600                         
1601                         msg_format(_("%sを放しますか? [Yes/No/Unnamed (%d体)]","Dismiss %s? [Yes/No/Unnamed (%d remain)]"), friend_name, max_pet - i);
1602                         
1603                         if (m_ptr->ml)
1604                                 move_cursor_relative(m_ptr->fy, m_ptr->fx);
1605
1606                         while (TRUE)
1607                         {
1608                                 char ch = inkey();
1609
1610                                 if (ch == 'Y' || ch == 'y')
1611                                 {
1612                                         delete_this = TRUE;
1613
1614                                         if (kakunin)
1615                                         {
1616                                                 msg_format(_("本当によろしいですか? (%s) ","Are you sure? (%s) "), friend_name);
1617                                                 ch = inkey();
1618                                                 if (ch != 'Y' && ch != 'y')
1619                                                         delete_this = FALSE;
1620                                         }
1621                                         break;
1622                                 }
1623
1624                                 if (ch == 'U' || ch == 'u')
1625                                 {
1626                                         all_pets = TRUE;
1627                                         break;
1628                                 }
1629
1630                                 if (ch == ESCAPE || ch == 'N' || ch == 'n')
1631                                         break;
1632
1633                                 bell();
1634                         }
1635                 }
1636
1637                 if ((all_pets && !kakunin) || (!all_pets && delete_this))
1638                 {
1639                         if (record_named_pet && m_ptr->nickname)
1640                         {
1641                                 char m_name[80];
1642
1643                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1644                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_DISMISS, m_name);
1645                         }
1646
1647                         if (pet_ctr == p_ptr->riding)
1648                         {
1649                                 msg_format(_("%sから降りた。","You have got off %s. "), friend_name);
1650
1651                                 p_ptr->riding = 0;
1652
1653                                 /* Update the monsters */
1654                                 p_ptr->update |= (PU_BONUS | PU_MONSTERS);
1655                                 p_ptr->redraw |= (PR_EXTRA | PR_UHEALTH);
1656                         }
1657
1658                         /* HACK : Add the line to message buffer */
1659                         msg_format(_("%s を放した。","Dismissed %s."), friend_name);
1660                         p_ptr->window |= (PW_MESSAGE);
1661                         window_stuff();
1662
1663                         delete_monster_idx(pet_ctr);
1664                         Dismissed++;
1665                 }
1666         }
1667
1668         Term->scr->cu = cu;
1669         Term->scr->cv = cv;
1670         Term_fresh();
1671
1672         C_KILL(who, max_m_idx, MONSTER_IDX);
1673
1674 #ifdef JP
1675         msg_format("%d 体のペットを放しました。", Dismissed);
1676 #else
1677         msg_format("You have dismissed %d pet%s.", Dismissed,
1678                    (Dismissed == 1 ? "" : "s"));
1679 #endif
1680         if (Dismissed == 0 && all_pets)
1681                 msg_print(_("'U'nnamed は、乗馬以外の名前のないペットだけを全て解放します。", "'U'nnamed means all your pets except named pets and your mount."));
1682 }
1683
1684 /*!
1685  * @brief プレイヤーの騎乗/下馬処理判定
1686  * @param c_ptr プレイヤーの移動先マスの構造体参照ポインタ
1687  * @param now_riding TRUEなら下馬処理、FALSEならば騎乗処理
1688  * @return 可能ならばTRUEを返す
1689  */
1690 static bool player_can_ride_aux(cave_type *c_ptr, bool now_riding)
1691 {
1692         bool p_can_enter;
1693         bool old_character_xtra = character_xtra;
1694         MONSTER_IDX old_riding = p_ptr->riding;
1695         bool old_riding_ryoute = p_ptr->riding_ryoute;
1696         bool old_old_riding_ryoute = p_ptr->old_riding_ryoute;
1697         bool old_pf_ryoute = (p_ptr->pet_extra_flags & PF_RYOUTE) ? TRUE : FALSE;
1698
1699         /* Hack -- prevent "icky" message */
1700         character_xtra = TRUE;
1701
1702         if (now_riding) p_ptr->riding = c_ptr->m_idx;
1703         else
1704         {
1705                 p_ptr->riding = 0;
1706                 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
1707                 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
1708         }
1709
1710         calc_bonuses();
1711
1712         p_can_enter = player_can_enter(c_ptr->feat, CEM_P_CAN_ENTER_PATTERN);
1713
1714         p_ptr->riding = old_riding;
1715         if (old_pf_ryoute) p_ptr->pet_extra_flags |= (PF_RYOUTE);
1716         else p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
1717         p_ptr->riding_ryoute = old_riding_ryoute;
1718         p_ptr->old_riding_ryoute = old_old_riding_ryoute;
1719
1720         calc_bonuses();
1721
1722         character_xtra = old_character_xtra;
1723
1724         return p_can_enter;
1725 }
1726
1727 /*!
1728  * @brief プレイヤーの落馬判定処理
1729  * @param dam 落馬判定を発した際に受けたダメージ量
1730  * @param force TRUEならば強制的に落馬する
1731  * @return 実際に落馬したらTRUEを返す
1732  */
1733 bool rakuba(HIT_POINT dam, bool force)
1734 {
1735         int i, y, x, oy, ox;
1736         int sn = 0, sy = 0, sx = 0;
1737         char m_name[80];
1738         monster_type *m_ptr = &m_list[p_ptr->riding];
1739         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1740         bool fall_dam = FALSE;
1741
1742         if (!p_ptr->riding) return FALSE;
1743         if (p_ptr->wild_mode) return FALSE;
1744
1745         if (dam >= 0 || force)
1746         {
1747                 if (!force)
1748                 {
1749                         int cur = p_ptr->skill_exp[GINOU_RIDING];
1750                         int max = s_info[p_ptr->pclass].s_max[GINOU_RIDING];
1751                         int ridinglevel = r_ptr->level;
1752
1753                         /* 落馬のしやすさ */
1754                         int rakubalevel = r_ptr->level;
1755                         if (p_ptr->riding_ryoute) rakubalevel += 20;
1756
1757                         if ((cur < max) && (max > 1000) &&
1758                             (dam / 2 + ridinglevel) > (cur / 30 + 10))
1759                         {
1760                                 int inc = 0;
1761
1762                                 if (ridinglevel > (cur / 100 + 15))
1763                                         inc += 1 + (ridinglevel - cur / 100 - 15);
1764                                 else
1765                                         inc += 1;
1766
1767                                 p_ptr->skill_exp[GINOU_RIDING] = MIN(max, cur + inc);
1768                         }
1769
1770                         /* レベルの低い乗馬からは落馬しにくい */
1771                         if (randint0(dam / 2 + rakubalevel * 2) < cur / 30 + 10)
1772                         {
1773                                 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))
1774                                 {
1775                                         return FALSE;
1776                                 }
1777                         }
1778                 }
1779
1780                 /* Check around the player */
1781                 for (i = 0; i < 8; i++)
1782                 {
1783                         cave_type *c_ptr;
1784
1785                         /* Access the location */
1786                         y = p_ptr->y + ddy_ddd[i];
1787                         x = p_ptr->x + ddx_ddd[i];
1788
1789                         c_ptr = &cave[y][x];
1790
1791                         if (c_ptr->m_idx) continue;
1792
1793                         /* Skip non-empty grids */
1794                         if (!cave_have_flag_grid(c_ptr, FF_MOVE) && !cave_have_flag_grid(c_ptr, FF_CAN_FLY))
1795                         {
1796                                 if (!player_can_ride_aux(c_ptr, FALSE)) continue;
1797                         }
1798
1799                         if (cave_have_flag_grid(c_ptr, FF_PATTERN)) continue;
1800
1801                         /* Count "safe" grids */
1802                         sn++;
1803
1804                         /* Randomize choice */
1805                         if (randint0(sn) > 0) continue;
1806
1807                         /* Save the safe location */
1808                         sy = y; sx = x;
1809                 }
1810                 if (!sn)
1811                 {
1812                         monster_desc(m_name, m_ptr, 0);
1813 #ifdef JP
1814 msg_format("%sから振り落とされそうになって、壁にぶつかった。",m_name);
1815                         take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, "壁への衝突", -1);
1816 #else
1817                         msg_format("You have nearly fallen from %s, but bumped into wall.",m_name);
1818                         take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, "bumping into wall", -1);
1819 #endif
1820                         return FALSE;
1821                 }
1822
1823                 oy = p_ptr->y;
1824                 ox = p_ptr->x;
1825
1826                 p_ptr->y = sy;
1827                 p_ptr->x = sx;
1828
1829                 /* Redraw the old spot */
1830                 lite_spot(oy, ox);
1831
1832                 /* Redraw the new spot */
1833                 lite_spot(p_ptr->y, p_ptr->x);
1834
1835                 /* Check for new panel */
1836                 verify_panel();
1837         }
1838
1839         p_ptr->riding = 0;
1840         p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
1841         p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
1842
1843         calc_bonuses();
1844
1845         p_ptr->update |= (PU_BONUS);
1846
1847         /* Update stuff */
1848         p_ptr->update |= (PU_VIEW | PU_LITE | PU_FLOW | PU_MON_LITE | PU_MONSTERS);
1849
1850         /* Window stuff */
1851         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1852
1853         p_ptr->redraw |= (PR_EXTRA);
1854
1855         /* Update health track of mount */
1856         p_ptr->redraw |= (PR_UHEALTH);
1857
1858         if (p_ptr->levitation && !force)
1859         {
1860                 monster_desc(m_name, m_ptr, 0);
1861                 msg_format(_("%sから落ちたが、空中でうまく体勢を立て直して着地した。", "You are thrown from %s, but make a good landing."),m_name);
1862         }
1863         else
1864         {
1865                 take_hit(DAMAGE_NOESCAPE, r_ptr->level+3, _("落馬", "Falling from riding"), -1);
1866                 fall_dam = TRUE;
1867         }
1868
1869         /* Move the player */
1870         if (sy && !p_ptr->is_dead)
1871                 (void)move_player_effect(p_ptr->y, p_ptr->x, MPE_DONT_PICKUP | MPE_DONT_SWAP_MON);
1872
1873         return fall_dam;
1874 }
1875
1876 /*!
1877  * @brief ペットから騎乗/下馬するコマンドのメインルーチン /
1878  * @param force 強制的に騎乗/下馬するならばTRUE
1879  * @return 騎乗/下馬できたらTRUE
1880  */
1881 bool do_riding(bool force)
1882 {
1883         int x, y, dir = 0;
1884         cave_type *c_ptr;
1885         monster_type *m_ptr;
1886
1887         if (!get_rep_dir2(&dir)) return FALSE;
1888         y = p_ptr->y + ddy[dir];
1889         x = p_ptr->x + ddx[dir];
1890         c_ptr = &cave[y][x];
1891
1892         if (p_ptr->special_defense & KATA_MUSOU) set_action(ACTION_NONE);
1893
1894         if (p_ptr->riding)
1895         {
1896                 /* Skip non-empty grids */
1897                 if (!player_can_ride_aux(c_ptr, FALSE))
1898                 {
1899                         msg_print(_("そちらには降りられません。", "You cannot go to that direction."));
1900                         return FALSE;
1901                 }
1902
1903                 if (!pattern_seq(p_ptr->y, p_ptr->x, y, x)) return FALSE;
1904
1905                 if (c_ptr->m_idx)
1906                 {
1907                         /* Take a turn */
1908                         p_ptr->energy_use = 100;
1909
1910                         /* Message */
1911                         msg_print(_("モンスターが立ちふさがっている!", "There is a monster in the way!"));
1912
1913                         py_attack(y, x, 0);
1914                         return FALSE;
1915                 }
1916
1917                 p_ptr->riding = 0;
1918                 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
1919                 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
1920         }
1921         else
1922         {
1923                 if (p_ptr->confused)
1924                 {
1925                         msg_print(_("混乱していて乗れない!", "You are too confused!"));
1926                         return FALSE;
1927                 }
1928
1929                 m_ptr = &m_list[c_ptr->m_idx];
1930
1931                 if (!c_ptr->m_idx || !m_ptr->ml)
1932                 {
1933                         msg_print(_("その場所にはモンスターはいません。", "Here is no monster."));
1934                         return FALSE;
1935                 }
1936                 if (!is_pet(m_ptr) && !force)
1937                 {
1938                         msg_print(_("そのモンスターはペットではありません。", "That monster is not a pet."));
1939                         return FALSE;
1940                 }
1941                 if (!(r_info[m_ptr->r_idx].flags7 & RF7_RIDING))
1942                 {
1943                         msg_print(_("そのモンスターには乗れなさそうだ。", "This monster doesn't seem suitable for riding."));
1944                         return FALSE;
1945                 }
1946
1947                 if (!pattern_seq(p_ptr->y, p_ptr->x, y, x)) return FALSE;
1948
1949                 if (!player_can_ride_aux(c_ptr, TRUE))
1950                 {
1951                         /* Feature code (applying "mimic" field) */
1952                         feature_type *f_ptr = &f_info[get_feat_mimic(c_ptr)];
1953 #ifdef JP
1954                         msg_format("そのモンスターは%sの%sにいる。", f_name + f_ptr->name,
1955                                    ((!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) ||
1956                                     (!have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_TREE))) ?
1957                                    "中" : "上");
1958 #else
1959                         msg_format("This monster is %s the %s.",
1960                                    ((!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) ||
1961                                     (!have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_TREE))) ?
1962                                    "in" : "on", f_name + f_ptr->name);
1963 #endif
1964
1965                         return FALSE;
1966                 }
1967                 if (r_info[m_ptr->r_idx].level > randint1((p_ptr->skill_exp[GINOU_RIDING] / 50 + p_ptr->lev / 2 + 20)))
1968                 {
1969                         msg_print(_("うまく乗れなかった。", "You failed to ride."));
1970                         p_ptr->energy_use = 100;
1971                         return FALSE;
1972                 }
1973
1974                 if (MON_CSLEEP(m_ptr))
1975                 {
1976                         char m_name[80];
1977                         monster_desc(m_name, m_ptr, 0);
1978                         (void)set_monster_csleep(c_ptr->m_idx, 0);
1979                         msg_format(_("%sを起こした。", "You have waked %s up."), m_name);
1980                 }
1981
1982                 if (p_ptr->action == ACTION_KAMAE) set_action(ACTION_NONE);
1983
1984                 p_ptr->riding = c_ptr->m_idx;
1985
1986                 /* Hack -- remove tracked monster */
1987                 if (p_ptr->riding == p_ptr->health_who) health_track(0);
1988         }
1989
1990         p_ptr->energy_use = 100;
1991
1992         /* Mega-Hack -- Forget the view and lite */
1993         p_ptr->update |= (PU_UN_VIEW | PU_UN_LITE);
1994
1995         /* Update the monsters */
1996         p_ptr->update |= (PU_BONUS);
1997
1998         /* Redraw map */
1999         p_ptr->redraw |= (PR_MAP | PR_EXTRA);
2000
2001         p_ptr->redraw |= (PR_UHEALTH);
2002
2003         /* Move the player */
2004         (void)move_player_effect(y, x, MPE_HANDLE_STUFF | MPE_ENERGY_USE | MPE_DONT_PICKUP | MPE_DONT_SWAP_MON);
2005
2006         return TRUE;
2007 }
2008
2009 /*!
2010  * @brief ペットに名前をつけるコマンドのメインルーチン
2011  * @return なし
2012  */
2013 static void do_name_pet(void)
2014 {
2015         monster_type *m_ptr;
2016         char out_val[20];
2017         char m_name[80];
2018         bool old_name = FALSE;
2019         bool old_target_pet = target_pet;
2020
2021         target_pet = TRUE;
2022         if (!target_set(TARGET_KILL))
2023         {
2024                 target_pet = old_target_pet;
2025                 return;
2026         }
2027         target_pet = old_target_pet;
2028
2029         if (cave[target_row][target_col].m_idx)
2030         {
2031                 m_ptr = &m_list[cave[target_row][target_col].m_idx];
2032
2033                 if (!is_pet(m_ptr))
2034                 {
2035                         /* Message */
2036                         msg_print(_("そのモンスターはペットではない。", "This monster is not a pet."));
2037                         return;
2038                 }
2039                 if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2040                 {
2041                         msg_print(_("そのモンスターの名前は変えられない!", "You cannot change name of this monster!"));
2042                         return;
2043                 }
2044                 monster_desc(m_name, m_ptr, 0);
2045
2046                 /* Message */
2047                 msg_format(_("%sに名前をつける。", "Name %s."), m_name);
2048                 msg_print(NULL);
2049
2050                 /* Start with nothing */
2051                 strcpy(out_val, "");
2052
2053                 /* Use old inscription */
2054                 if (m_ptr->nickname)
2055                 {
2056                         /* Start with the old inscription */
2057                         strcpy(out_val, quark_str(m_ptr->nickname));
2058                         old_name = TRUE;
2059                 }
2060
2061                 /* Get a new inscription (possibly empty) */
2062                 if (get_string(_("名前: ", "Name: "), out_val, 15))
2063                 {
2064                         if (out_val[0])
2065                         {
2066                                 /* Save the inscription */
2067                                 m_ptr->nickname = quark_add(out_val);
2068                                 if (record_named_pet)
2069                                 {
2070                                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2071                                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_NAME, m_name);
2072                                 }
2073                         }
2074                         else
2075                         {
2076                                 if (record_named_pet && old_name)
2077                                 {
2078                                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2079                                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_UNNAME, m_name);
2080                                 }
2081                                 m_ptr->nickname = 0;
2082                         }
2083                 }
2084         }
2085 }
2086
2087
2088 /*!
2089  * @brief ペットに関するコマンドリストのメインルーチン /
2090  * Issue a pet command
2091  * @return なし
2092  */
2093 void do_cmd_pet(void)
2094 {
2095         COMMAND_CODE i = 0;
2096         int                     num;
2097         int                     powers[36];
2098         cptr                    power_desc[36];
2099         bool                    flag, redraw;
2100         char                    choice;
2101         char                    out_val[160];
2102         int                     pet_ctr;
2103         monster_type    *m_ptr;
2104
2105         PET_COMMAND_IDX mode = 0;
2106
2107         char buf[160];
2108         char target_buf[160];
2109
2110         int menu_line = use_menu ? 1 : 0;
2111
2112         num = 0;
2113
2114         power_desc[num] = _("ペットを放す", "dismiss pets");
2115         powers[num++] = PET_DISMISS;
2116
2117 #ifdef JP
2118         sprintf(target_buf, "ペットのターゲットを指定 (現在:%s)",
2119                 (pet_t_m_idx ? (p_ptr->image ? "何か奇妙な物" : (r_name + r_info[m_list[pet_t_m_idx].ap_r_idx].name)) : "指定なし"));
2120 #else
2121         sprintf(target_buf, "specify a target of pet (now:%s)",
2122                 (pet_t_m_idx ? (p_ptr->image ? "something strange" : (r_name + r_info[m_list[pet_t_m_idx].ap_r_idx].name)) : "nothing"));
2123 #endif
2124         power_desc[num] = target_buf;
2125         powers[num++] = PET_TARGET;
2126         power_desc[num] = _("近くにいろ", "stay close");
2127
2128         if (p_ptr->pet_follow_distance == PET_CLOSE_DIST) mode = num;
2129         powers[num++] = PET_STAY_CLOSE;
2130         power_desc[num] = _("ついて来い", "follow me");
2131
2132         if (p_ptr->pet_follow_distance == PET_FOLLOW_DIST) mode = num;
2133         powers[num++] = PET_FOLLOW_ME;
2134         power_desc[num] = _("敵を見つけて倒せ", "seek and destroy");
2135
2136         if (p_ptr->pet_follow_distance == PET_DESTROY_DIST) mode = num;
2137         powers[num++] = PET_SEEK_AND_DESTROY;
2138         power_desc[num] = _("少し離れていろ", "give me space");
2139
2140         if (p_ptr->pet_follow_distance == PET_SPACE_DIST) mode = num;
2141         powers[num++] = PET_ALLOW_SPACE;
2142         power_desc[num] = _("離れていろ", "stay away");
2143
2144         if (p_ptr->pet_follow_distance == PET_AWAY_DIST) mode = num;
2145         powers[num++] = PET_STAY_AWAY;
2146
2147         if (p_ptr->pet_extra_flags & PF_OPEN_DOORS)
2148         {
2149                 power_desc[num] = _("ドアを開ける (現在:ON)", "pets open doors (now On)");
2150         }
2151         else
2152         {
2153                 power_desc[num] = _("ドアを開ける (現在:OFF)", "pets open doors (now Off)");
2154         }
2155         powers[num++] = PET_OPEN_DOORS;
2156
2157         if (p_ptr->pet_extra_flags & PF_PICKUP_ITEMS)
2158         {
2159                 power_desc[num] = _("アイテムを拾う (現在:ON)", "pets pick up items (now On)");
2160         }
2161         else
2162         {
2163                 power_desc[num] = _("アイテムを拾う (現在:OFF)", "pets pick up items (now Off)");
2164         }
2165         powers[num++] = PET_TAKE_ITEMS;
2166
2167         if (p_ptr->pet_extra_flags & PF_TELEPORT)
2168         {
2169                 power_desc[num] = _("テレポート系魔法を使う (現在:ON)", "allow teleport (now On)");
2170         }
2171         else
2172         {
2173                 power_desc[num] = _("テレポート系魔法を使う (現在:OFF)", "allow teleport (now Off)");
2174         }
2175         powers[num++] = PET_TELEPORT;
2176
2177         if (p_ptr->pet_extra_flags & PF_ATTACK_SPELL)
2178         {
2179                 power_desc[num] = _("攻撃魔法を使う (現在:ON)", "allow cast attack spell (now On)");
2180         }
2181         else
2182         {
2183                 power_desc[num] = _("攻撃魔法を使う (現在:OFF)", "allow cast attack spell (now Off)");
2184         }
2185         powers[num++] = PET_ATTACK_SPELL;
2186
2187         if (p_ptr->pet_extra_flags & PF_SUMMON_SPELL)
2188         {
2189                 power_desc[num] = _("召喚魔法を使う (現在:ON)", "allow cast summon spell (now On)");
2190         }
2191         else
2192         {
2193                 power_desc[num] = _("召喚魔法を使う (現在:OFF)", "allow cast summon spell (now Off)");
2194         }
2195         powers[num++] = PET_SUMMON_SPELL;
2196
2197         if (p_ptr->pet_extra_flags & PF_BALL_SPELL)
2198         {
2199                 power_desc[num] = _("プレイヤーを巻き込む範囲魔法を使う (現在:ON)", "allow involve player in area spell (now On)");
2200         }
2201         else
2202         {
2203                 power_desc[num] = _("プレイヤーを巻き込む範囲魔法を使う (現在:OFF)", "allow involve player in area spell (now Off)");
2204         }
2205         powers[num++] = PET_BALL_SPELL;
2206
2207         if (p_ptr->riding)
2208         {
2209                 power_desc[num] = _("ペットから降りる", "get off a pet");
2210         }
2211         else
2212         {
2213                 power_desc[num] = _("ペットに乗る", "ride a pet");
2214         }
2215         powers[num++] = PET_RIDING;
2216         power_desc[num] = _("ペットに名前をつける", "name pets");
2217         powers[num++] = PET_NAME;
2218
2219         if (p_ptr->riding)
2220         {
2221                 if ((p_ptr->migite && (empty_hands(FALSE) == EMPTY_HAND_LARM) &&
2222                      object_allow_two_hands_wielding(&inventory[INVEN_RARM])) ||
2223                     (p_ptr->hidarite && (empty_hands(FALSE) == EMPTY_HAND_RARM) &&
2224                          object_allow_two_hands_wielding(&inventory[INVEN_LARM])))
2225                 {
2226                         if (p_ptr->pet_extra_flags & PF_RYOUTE)
2227                         {
2228                                 power_desc[num] = _("武器を片手で持つ", "use one hand to control a riding pet");
2229                         }
2230                         else
2231                         {
2232                                 power_desc[num] = _("武器を両手で持つ", "use both hands for a weapon");
2233                         }
2234
2235                         powers[num++] = PET_RYOUTE;
2236                 }
2237                 else
2238                 {
2239                         switch (p_ptr->pclass)
2240                         {
2241                         case CLASS_MONK:
2242                         case CLASS_FORCETRAINER:
2243                         case CLASS_BERSERKER:
2244                                 if (empty_hands(FALSE) == (EMPTY_HAND_RARM | EMPTY_HAND_LARM))
2245                                 {
2246                                         if (p_ptr->pet_extra_flags & PF_RYOUTE)
2247                                         {
2248                                                 power_desc[num] = _("片手で格闘する", "use one hand to control a riding pet");
2249                                         }
2250                                         else
2251                                         {
2252                                                 power_desc[num] = _("両手で格闘する", "use both hands for melee");
2253                                         }
2254
2255                                         powers[num++] = PET_RYOUTE;
2256                                 }
2257                                 else if ((empty_hands(FALSE) != EMPTY_HAND_NONE) && !buki_motteruka(INVEN_RARM) && !buki_motteruka(INVEN_LARM))
2258                                 {
2259                                         if (p_ptr->pet_extra_flags & PF_RYOUTE)
2260                                         {
2261                                                 power_desc[num] = _("格闘を行わない", "use one hand to control a riding pet");
2262                                         }
2263                                         else
2264                                         {
2265                                                 power_desc[num] = _("格闘を行う", "use one hand for melee");
2266                                         }
2267
2268                                         powers[num++] = PET_RYOUTE;
2269                                 }
2270                                 break;
2271                         }
2272                 }
2273         }
2274
2275 #ifdef ALLOW_REPEAT
2276         if (!(repeat_pull(&i) && (i >= 0) && (i < num)))
2277         {
2278 #endif /* ALLOW_REPEAT */
2279
2280         /* Nothing chosen yet */
2281         flag = FALSE;
2282
2283         /* No redraw yet */
2284         redraw = FALSE;
2285
2286         if (use_menu)
2287         {
2288                 /* Save the screen */
2289                 screen_save();
2290
2291                 /* Build a prompt */
2292                 strnfmt(out_val, 78, _("(コマンド、ESC=終了) コマンドを選んでください:", "(Command, ESC=exit) Choose command from menu."));
2293         }
2294         else
2295         {
2296                 /* Build a prompt */
2297                 strnfmt(out_val, 78,
2298                         _("(コマンド %c-%c、'*'=一覧、ESC=終了) コマンドを選んでください:", "(Command %c-%c, *=List, ESC=exit) Select a command: "),
2299                         I2A(0), I2A(num - 1));
2300         }
2301
2302         choice = (always_show_list || use_menu) ? ESCAPE : 1;
2303
2304         /* Get a command from the user */
2305         while (!flag)
2306         {
2307                 int ask = TRUE;
2308
2309                 if (choice == ESCAPE) choice = ' ';
2310                 else if (!get_com(out_val, &choice, TRUE)) break;
2311
2312                 if (use_menu && (choice != ' '))
2313                 {
2314                         switch (choice)
2315                         {
2316                         case '0':
2317                                 screen_load();
2318                                 return;
2319
2320                         case '8':
2321                         case 'k':
2322                         case 'K':
2323                                 menu_line += (num - 1);
2324                                 break;
2325
2326                         case '2':
2327                         case 'j':
2328                         case 'J':
2329                                 menu_line++;
2330                                 break;
2331
2332                         case '4':
2333                         case 'h':
2334                         case 'H':
2335                                 menu_line = 1;
2336                                 break;
2337
2338                         case '6':
2339                         case 'l':
2340                         case 'L':
2341                                 menu_line = num;
2342                                 break;
2343
2344                         case 'x':
2345                         case 'X':
2346                         case '\r':
2347                         case '\n':
2348                                 i = menu_line - 1;
2349                                 ask = FALSE;
2350                                 break;
2351                         }
2352                         if (menu_line > num) menu_line -= num;
2353                 }
2354
2355                 /* Request redraw */
2356                 if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && ask))
2357                 {
2358                         /* Show the list */
2359                         if (!redraw || use_menu)
2360                         {
2361                                 byte y = 1, x = 0;
2362                                 PET_COMMAND_IDX ctr = 0;
2363
2364                                 /* Show list */
2365                                 redraw = TRUE;
2366
2367                                 /* Save the screen */
2368                                 if (!use_menu) screen_save();
2369
2370                                 prt("", y++, x);
2371
2372                                 /* Print list */
2373                                 for (ctr = 0; ctr < num; ctr++)
2374                                 {
2375                                         /* Letter/number for power selection */
2376                                         if (use_menu) 
2377                                                 sprintf(buf, "%c%s ", (ctr == mode) ? '*' : ' ', (ctr == (menu_line - 1)) ? _("》", "> ") : "  ");
2378                                         else 
2379                                                 sprintf(buf, "%c%c) ", (ctr == mode) ? '*' : ' ', I2A(ctr));
2380
2381                                         strcat(buf, power_desc[ctr]);
2382
2383                                         prt(buf, y + ctr, x);
2384                                 }
2385
2386                                 prt("", y + MIN(ctr, 17), x);
2387                         }
2388
2389                         /* Hide the list */
2390                         else
2391                         {
2392                                 /* Hide list */
2393                                 redraw = FALSE;
2394
2395                                 /* Restore the screen */
2396                                 screen_load();
2397                         }
2398
2399                         /* Redo asking */
2400                         continue;
2401                 }
2402
2403                 if (!use_menu)
2404                 {
2405                         /* Note verify */
2406                         ask = (isupper(choice));
2407
2408                         /* Lowercase */
2409                         if (ask) choice = (char)tolower(choice);
2410
2411                         /* Extract request */
2412                         i = (islower(choice) ? A2I(choice) : -1);
2413                 }
2414
2415                 /* Totally Illegal */
2416                 if ((i < 0) || (i >= num))
2417                 {
2418                         bell();
2419                         continue;
2420                 }
2421
2422                 /* Verify it */
2423                 if (ask)
2424                 {
2425                         /* Prompt */
2426                         strnfmt(buf, 78, _("%sを使いますか? ", "Use %s? "), power_desc[i]);
2427
2428                         /* Belay that order */
2429                         if (!get_check(buf)) continue;
2430                 }
2431
2432                 /* Stop the loop */
2433                 flag = TRUE;
2434         }
2435
2436         /* Restore the screen */
2437         if (redraw) screen_load();
2438
2439         /* Abort if needed */
2440         if (!flag)
2441         {
2442                 p_ptr->energy_use = 0;
2443                 return;
2444         }
2445
2446 #ifdef ALLOW_REPEAT
2447         repeat_push(i);
2448         }
2449 #endif /* ALLOW_REPEAT */
2450
2451         switch (powers[i])
2452         {
2453                 case PET_DISMISS: /* Dismiss pets */
2454                 {
2455                         /* Check pets (backwards) */
2456                         for (pet_ctr = m_max - 1; pet_ctr >= 1; pet_ctr--)
2457                         {
2458                                 /* Player has pet */
2459                                 if (is_pet(&m_list[pet_ctr])) break;
2460                         }
2461
2462                         if (!pet_ctr)
2463                         {
2464                                 msg_print(_("ペットがいない!", "You have no pets!"));
2465                                 break;
2466                         }
2467                         do_cmd_pet_dismiss();
2468                         (void)calculate_upkeep();
2469                         break;
2470                 }
2471                 case PET_TARGET:
2472                 {
2473                         project_length = -1;
2474                         if (!target_set(TARGET_KILL)) pet_t_m_idx = 0;
2475                         else
2476                         {
2477                                 cave_type *c_ptr = &cave[target_row][target_col];
2478                                 if (c_ptr->m_idx && (m_list[c_ptr->m_idx].ml))
2479                                 {
2480                                         pet_t_m_idx = cave[target_row][target_col].m_idx;
2481                                         p_ptr->pet_follow_distance = PET_DESTROY_DIST;
2482                                 }
2483                                 else pet_t_m_idx = 0;
2484                         }
2485                         project_length = 0;
2486
2487                         break;
2488                 }
2489                 /* Call pets */
2490                 case PET_STAY_CLOSE:
2491                 {
2492                         p_ptr->pet_follow_distance = PET_CLOSE_DIST;
2493                         pet_t_m_idx = 0;
2494                         break;
2495                 }
2496                 /* "Follow Me" */
2497                 case PET_FOLLOW_ME:
2498                 {
2499                         p_ptr->pet_follow_distance = PET_FOLLOW_DIST;
2500                         pet_t_m_idx = 0;
2501                         break;
2502                 }
2503                 /* "Seek and destoy" */
2504                 case PET_SEEK_AND_DESTROY:
2505                 {
2506                         p_ptr->pet_follow_distance = PET_DESTROY_DIST;
2507                         break;
2508                 }
2509                 /* "Give me space" */
2510                 case PET_ALLOW_SPACE:
2511                 {
2512                         p_ptr->pet_follow_distance = PET_SPACE_DIST;
2513                         break;
2514                 }
2515                 /* "Stay away" */
2516                 case PET_STAY_AWAY:
2517                 {
2518                         p_ptr->pet_follow_distance = PET_AWAY_DIST;
2519                         break;
2520                 }
2521                 /* flag - allow pets to open doors */
2522                 case PET_OPEN_DOORS:
2523                 {
2524                         if (p_ptr->pet_extra_flags & PF_OPEN_DOORS) p_ptr->pet_extra_flags &= ~(PF_OPEN_DOORS);
2525                         else p_ptr->pet_extra_flags |= (PF_OPEN_DOORS);
2526                         break;
2527                 }
2528                 /* flag - allow pets to pickup items */
2529                 case PET_TAKE_ITEMS:
2530                 {
2531                         if (p_ptr->pet_extra_flags & PF_PICKUP_ITEMS)
2532                         {
2533                                 p_ptr->pet_extra_flags &= ~(PF_PICKUP_ITEMS);
2534                                 for (pet_ctr = m_max - 1; pet_ctr >= 1; pet_ctr--)
2535                                 {
2536                                         /* Access the monster */
2537                                         m_ptr = &m_list[pet_ctr];
2538
2539                                         if (is_pet(m_ptr))
2540                                         {
2541                                                 monster_drop_carried_objects(m_ptr);
2542                                         }
2543                                 }
2544                         }
2545                         else p_ptr->pet_extra_flags |= (PF_PICKUP_ITEMS);
2546
2547                         break;
2548                 }
2549                 /* flag - allow pets to teleport */
2550                 case PET_TELEPORT:
2551                 {
2552                         if (p_ptr->pet_extra_flags & PF_TELEPORT) p_ptr->pet_extra_flags &= ~(PF_TELEPORT);
2553                         else p_ptr->pet_extra_flags |= (PF_TELEPORT);
2554                         break;
2555                 }
2556                 /* flag - allow pets to cast attack spell */
2557                 case PET_ATTACK_SPELL:
2558                 {
2559                         if (p_ptr->pet_extra_flags & PF_ATTACK_SPELL) p_ptr->pet_extra_flags &= ~(PF_ATTACK_SPELL);
2560                         else p_ptr->pet_extra_flags |= (PF_ATTACK_SPELL);
2561                         break;
2562                 }
2563                 /* flag - allow pets to cast attack spell */
2564                 case PET_SUMMON_SPELL:
2565                 {
2566                         if (p_ptr->pet_extra_flags & PF_SUMMON_SPELL) p_ptr->pet_extra_flags &= ~(PF_SUMMON_SPELL);
2567                         else p_ptr->pet_extra_flags |= (PF_SUMMON_SPELL);
2568                         break;
2569                 }
2570                 /* flag - allow pets to cast attack spell */
2571                 case PET_BALL_SPELL:
2572                 {
2573                         if (p_ptr->pet_extra_flags & PF_BALL_SPELL) p_ptr->pet_extra_flags &= ~(PF_BALL_SPELL);
2574                         else p_ptr->pet_extra_flags |= (PF_BALL_SPELL);
2575                         break;
2576                 }
2577
2578                 case PET_RIDING:
2579                 {
2580                         (void)do_riding(FALSE);
2581                         break;
2582                 }
2583
2584                 case PET_NAME:
2585                 {
2586                         do_name_pet();
2587                         break;
2588                 }
2589
2590                 case PET_RYOUTE:
2591                 {
2592                         if (p_ptr->pet_extra_flags & PF_RYOUTE) p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
2593                         else p_ptr->pet_extra_flags |= (PF_RYOUTE);
2594                         p_ptr->update |= (PU_BONUS);
2595                         handle_stuff();
2596                         break;
2597                 }
2598         }
2599 }