OSDN Git Service

Merge pull request #3327 from Hourier/Move-GlobalFunctions-To-Monrace-Methods
[hengbandforosx/hengbandosx.git] / src / cmd-action / cmd-hissatsu.cpp
1 /*!
2  * @brief 剣術の実装 / Blade arts
3  * @date 2014/01/17
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  * This software may be copied and distributed for educational, research,\n
7  * and not for profit purposes provided that this copyright and statement\n
8  * are included in all such copies.  Other copyrights may also apply.\n
9  * 2014 Deskull rearranged comment for Doxygen.\n
10  */
11
12 #include "action/action-limited.h"
13 #include "cmd-action/cmd-spell.h"
14 #include "core/asking-player.h"
15 #include "core/stuff-handler.h"
16 #include "core/window-redrawer.h"
17 #include "floor/floor-object.h"
18 #include "game-option/disturbance-options.h"
19 #include "game-option/text-display-options.h"
20 #include "inventory/inventory-slot-types.h"
21 #include "io/command-repeater.h"
22 #include "io/input-key-requester.h"
23 #include "main/sound-definitions-table.h"
24 #include "main/sound-of-music.h"
25 #include "monster-race/monster-race-hook.h"
26 #include "object/item-tester-hooker.h"
27 #include "object/item-use-flags.h"
28 #include "player-base/player-class.h"
29 #include "player-info/equipment-info.h"
30 #include "player-info/samurai-data-type.h"
31 #include "player-status/player-energy.h"
32 #include "player/attack-defense-types.h"
33 #include "player/special-defense-types.h"
34 #include "spell/spells-execution.h"
35 #include "spell/technic-info-table.h"
36 #include "status/action-setter.h"
37 #include "system/item-entity.h"
38 #include "system/player-type-definition.h"
39 #include "system/redrawing-flags-updater.h"
40 #include "term/screen-processor.h"
41 #include "term/z-form.h"
42 #include "util/int-char-converter.h"
43 #include "view/display-messages.h"
44
45 #define TECHNIC_HISSATSU (REALM_HISSATSU - MIN_TECHNIC)
46
47 /*!
48  * @brief 使用可能な剣術を選択する /
49  * Allow user to choose a blade arts.
50  * @param sn 選択した特殊技能ID、キャンセルの場合-1、不正な選択の場合-2を返す
51  * @return 発動可能な魔法を選択した場合TRUE、キャンセル処理か不正な選択が行われた場合FALSEを返す。
52  * @details
53  * If a valid spell is chosen, saves it in '*sn' and returns TRUE\n
54  * If the user hits escape, returns FALSE, and set '*sn' to -1\n
55  * If there are no legal choices, returns FALSE, and sets '*sn' to -2\n
56  *\n
57  * The "prompt" should be "cast", "recite", or "study"\n
58  * The "known" should be TRUE for cast/pray, FALSE for study\n
59  *\n
60  * nb: This function has a (trivial) display bug which will be obvious\n
61  * when you run it. It's probably easy to fix but I haven't tried,\n
62  * sorry.\n
63  */
64 static int get_hissatsu_power(PlayerType *player_ptr, SPELL_IDX *sn)
65 {
66     SPELL_IDX i;
67     int j = 0;
68     int num = 0;
69     POSITION y = 1;
70     POSITION x = 15;
71     PLAYER_LEVEL plev = player_ptr->lev;
72     char choice;
73     char out_val[160];
74     SPELL_IDX sentaku[32];
75     concptr p = _("必殺剣", "special attack");
76     COMMAND_CODE code;
77     magic_type spell;
78     bool flag, redraw;
79     int menu_line = (use_menu ? 1 : 0);
80
81     /* Assume cancelled */
82     *sn = (-1);
83
84     /* Get the spell, if available */
85     if (repeat_pull(&code)) {
86         *sn = (SPELL_IDX)code;
87         /* Verify the spell */
88         if (technic_info[TECHNIC_HISSATSU][*sn].slevel <= plev) {
89             /* Success */
90             return true;
91         }
92     }
93
94     flag = false;
95     redraw = false;
96
97     for (i = 0; i < 32; i++) {
98         if (technic_info[TECHNIC_HISSATSU][i].slevel <= PY_MAX_LEVEL) {
99             sentaku[num] = i;
100             num++;
101         }
102     }
103
104     constexpr auto fmt = _("(%s^ %c-%c, '*'で一覧, ESC) どの%sを使いますか?", "(%s^s %c-%c, *=List, ESC=exit) Use which %s? ");
105     (void)strnfmt(out_val, 78, fmt, p, I2A(0), "abcdefghijklmnopqrstuvwxyz012345"[num - 1], p);
106
107     if (use_menu) {
108         screen_save();
109     }
110     choice = always_show_list ? ESCAPE : 1;
111
112     while (!flag) {
113         if (choice == ESCAPE) {
114             choice = ' ';
115         } else if (!get_com(out_val, &choice, false)) {
116             break;
117         }
118
119         auto should_redraw_cursor = true;
120         if (use_menu && choice != ' ') {
121             switch (choice) {
122             case '0': {
123                 screen_load();
124                 return false;
125             }
126
127             case '8':
128             case 'k':
129             case 'K': {
130                 do {
131                     menu_line += 31;
132                     if (menu_line > 32) {
133                         menu_line -= 32;
134                     }
135                 } while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1))));
136                 break;
137             }
138
139             case '2':
140             case 'j':
141             case 'J': {
142                 do {
143                     menu_line++;
144                     if (menu_line > 32) {
145                         menu_line -= 32;
146                     }
147                 } while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1))));
148                 break;
149             }
150
151             case '4':
152             case 'h':
153             case 'H':
154             case '6':
155             case 'l':
156             case 'L': {
157                 bool reverse = false;
158                 if ((choice == '4') || (choice == 'h') || (choice == 'H')) {
159                     reverse = true;
160                 }
161                 if (menu_line > 16) {
162                     menu_line -= 16;
163                     reverse = true;
164                 } else {
165                     menu_line += 16;
166                 }
167                 while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1)))) {
168                     if (reverse) {
169                         menu_line--;
170                         if (menu_line < 2) {
171                             reverse = false;
172                         }
173                     } else {
174                         menu_line++;
175                         if (menu_line > 31) {
176                             reverse = true;
177                         }
178                     }
179                 }
180                 break;
181             }
182
183             case 'x':
184             case 'X':
185             case '\r':
186             case '\n': {
187                 i = menu_line - 1;
188                 should_redraw_cursor = false;
189                 break;
190             }
191             }
192         }
193         /* Request redraw */
194         if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && should_redraw_cursor)) {
195             /* Show the list */
196             if (!redraw || use_menu) {
197                 int line;
198                 redraw = true;
199                 if (!use_menu) {
200                     screen_save();
201                 }
202
203                 /* Display a list of spells */
204                 prt("", y, x);
205                 put_str(_("名前              Lv  MP      名前              Lv  MP ", "name              Lv  SP      name              Lv  SP "), y, x + 5);
206                 prt("", y + 1, x);
207                 /* Dump the spells */
208                 for (i = 0, line = 0; i < 32; i++) {
209                     spell = technic_info[TECHNIC_HISSATSU][i];
210
211                     if (spell.slevel > PY_MAX_LEVEL) {
212                         continue;
213                     }
214                     line++;
215                     if (!(player_ptr->spell_learned1 >> i)) {
216                         break;
217                     }
218
219                     /* Access the spell */
220                     if (spell.slevel > plev) {
221                         continue;
222                     }
223                     if (!(player_ptr->spell_learned1 & (1UL << i))) {
224                         continue;
225                     }
226                     std::string psi_desc;
227                     if (use_menu) {
228                         if (i == (menu_line - 1)) {
229                             psi_desc = _("  》", "  > ");
230                         } else {
231                             psi_desc = "    ";
232                         }
233
234                     } else {
235                         char letter;
236                         if (line <= 26) {
237                             letter = I2A(line - 1);
238                         } else {
239                             letter = '0' + line - 27;
240                         }
241                         psi_desc = format("  %c)", letter);
242                     }
243
244                     /* Dump the spell --(-- */
245                     const auto spell_name = exe_spell(player_ptr, REALM_HISSATSU, i, SpellProcessType::NAME);
246                     psi_desc.append(format(" %-18s%2d %3d", spell_name->data(), spell.slevel, spell.smana));
247                     prt(psi_desc, y + (line % 17) + (line >= 17), x + (line / 17) * 30);
248                     prt("", y + (line % 17) + (line >= 17) + 1, x + (line / 17) * 30);
249                 }
250             }
251
252             /* Hide the list */
253             else {
254                 /* Hide list */
255                 redraw = false;
256                 screen_load();
257             }
258
259             /* Redo asking */
260             continue;
261         }
262
263         if (!use_menu) {
264             if (isalpha(choice)) {
265                 i = A2I(choice);
266             } else {
267                 i = choice - '0' + 26;
268             }
269         }
270
271         /* Totally Illegal */
272         if ((i < 0) || (i >= 32) || !(player_ptr->spell_learned1 & (1U << sentaku[i]))) {
273             bell();
274             continue;
275         }
276
277         j = sentaku[i];
278
279         /* Stop the loop */
280         flag = true;
281     }
282     if (redraw) {
283         screen_load();
284     }
285
286     player_ptr->window_flags |= (PW_SPELL);
287     handle_stuff(player_ptr);
288
289     /* Abort if needed */
290     if (!flag) {
291         return false;
292     }
293
294     /* Save the choice */
295     (*sn) = j;
296
297     repeat_push((COMMAND_CODE)j);
298
299     /* Success */
300     return true;
301 }
302
303 /*!
304  * @brief 剣術コマンドのメインルーチン
305  */
306 void do_cmd_hissatsu(PlayerType *player_ptr)
307 {
308     SPELL_IDX n = 0;
309     magic_type spell;
310
311     if (cmd_limit_confused(player_ptr)) {
312         return;
313     }
314     if (!has_melee_weapon(player_ptr, INVEN_MAIN_HAND) && !has_melee_weapon(player_ptr, INVEN_SUB_HAND)) {
315         if (flush_failure) {
316             flush();
317         }
318         msg_print(_("武器を持たないと必殺技は使えない!", "You need to wield a weapon!"));
319         return;
320     }
321     if (!player_ptr->spell_learned1) {
322         msg_print(_("何も技を知らない。", "You don't know any special attacks."));
323         return;
324     }
325
326     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::IAI, SamuraiStanceType::FUUJIN, SamuraiStanceType::KOUKIJIN });
327
328     if (!get_hissatsu_power(player_ptr, &n)) {
329         return;
330     }
331
332     spell = technic_info[TECHNIC_HISSATSU][n];
333
334     /* Verify "dangerous" spells */
335     if (spell.smana > player_ptr->csp) {
336         if (flush_failure) {
337             flush();
338         }
339         /* Warning */
340         msg_print(_("MPが足りません。", "You do not have enough mana to use this power."));
341         msg_print(nullptr);
342         return;
343     }
344
345     sound(SOUND_ZAP);
346
347     if (!exe_spell(player_ptr, REALM_HISSATSU, n, SpellProcessType::CAST)) {
348         return;
349     }
350
351     PlayerEnergy(player_ptr).set_player_turn_energy(100);
352     player_ptr->csp -= spell.smana;
353     if (player_ptr->csp < 0) {
354         player_ptr->csp = 0;
355     }
356
357     auto &rfu = RedrawingFlagsUpdater::get_instance();
358     rfu.set_flag(MainWindowRedrawingFlag::MP);
359     player_ptr->window_flags |= (PW_PLAYER | PW_SPELL);
360 }
361
362 /*!
363  * @brief 剣術コマンドの学習
364  */
365 void do_cmd_gain_hissatsu(PlayerType *player_ptr)
366 {
367     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::KOUKIJIN });
368     if (cmd_limit_blind(player_ptr) || cmd_limit_confused(player_ptr)) {
369         return;
370     }
371
372     if (!(player_ptr->new_spells)) {
373         msg_print(_("新しい必殺技を覚えることはできない!", "You cannot learn any new special attacks!"));
374         return;
375     }
376
377 #ifdef JP
378     msg_format("あと %d 種の必殺技を学べる。", player_ptr->new_spells);
379 #else
380     msg_format("You can learn %d new special attack%s.", player_ptr->new_spells, (player_ptr->new_spells == 1 ? "" : "s"));
381 #endif
382
383     constexpr auto q = _("どの書から学びますか? ", "Study which book? ");
384     constexpr auto s = _("読める書がない。", "You have no books that you can read.");
385     constexpr auto options = USE_INVEN | USE_FLOOR;
386     short item;
387     const auto *o_ptr = choose_object(player_ptr, &item, q, s, options, TvalItemTester(ItemKindType::HISSATSU_BOOK));
388     if (o_ptr == nullptr) {
389         return;
390     }
391
392     const auto sval = o_ptr->bi_key.sval().value();
393     auto gain = false;
394     for (auto i = sval * 8; i < sval * 8 + 8; i++) {
395         if (player_ptr->spell_learned1 & (1UL << i)) {
396             continue;
397         }
398
399         if (technic_info[TECHNIC_HISSATSU][i].slevel > player_ptr->lev) {
400             continue;
401         }
402
403         player_ptr->spell_learned1 |= (1UL << i);
404         player_ptr->spell_worked1 |= (1UL << i);
405         const auto spell_name = exe_spell(player_ptr, REALM_HISSATSU, i, SpellProcessType::NAME);
406         msg_format(_("%sの技を覚えた。", "You have learned the special attack of %s."), spell_name->data());
407         int j;
408         for (j = 0; j < 64; j++) {
409             /* Stop at the first empty space */
410             if (player_ptr->spell_order[j] == 99) {
411                 break;
412             }
413         }
414
415         player_ptr->spell_order[j] = i;
416         gain = true;
417     }
418
419     if (!gain) {
420         msg_print(_("何も覚えられなかった。", "You were not able to learn any special attacks."));
421     } else {
422         PlayerEnergy(player_ptr).set_player_turn_energy(100);
423     }
424
425     RedrawingFlagsUpdater::get_instance().set_flag(StatusRedrawingFlag::SPELLS);
426 }