OSDN Git Service

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