OSDN Git Service

Merge pull request #1861 from dis-/feature/refactor-enum-class-format
[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/object-type-definition.h"
40 #include "system/player-type-definition.h"
41 #include "term/screen-processor.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(player_type *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     int ask = true;
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を使いますか?", "(%^ss %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     choice = always_show_list ? ESCAPE : 1;
112
113     while (!flag) {
114         if (choice == ESCAPE)
115             choice = ' ';
116         else if (!get_com(out_val, &choice, false))
117             break;
118
119         if (use_menu && choice != ' ') {
120             switch (choice) {
121             case '0': {
122                 screen_load();
123                 return false;
124             }
125
126             case '8':
127             case 'k':
128             case 'K': {
129                 do {
130                     menu_line += 31;
131                     if (menu_line > 32)
132                         menu_line -= 32;
133                 } while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1))));
134                 break;
135             }
136
137             case '2':
138             case 'j':
139             case 'J': {
140                 do {
141                     menu_line++;
142                     if (menu_line > 32)
143                         menu_line -= 32;
144                 } while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1))));
145                 break;
146             }
147
148             case '4':
149             case 'h':
150             case 'H':
151             case '6':
152             case 'l':
153             case 'L': {
154                 bool reverse = false;
155                 if ((choice == '4') || (choice == 'h') || (choice == 'H'))
156                     reverse = true;
157                 if (menu_line > 16) {
158                     menu_line -= 16;
159                     reverse = true;
160                 } else
161                     menu_line += 16;
162                 while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1)))) {
163                     if (reverse) {
164                         menu_line--;
165                         if (menu_line < 2)
166                             reverse = false;
167                     } else {
168                         menu_line++;
169                         if (menu_line > 31)
170                             reverse = true;
171                     }
172                 }
173                 break;
174             }
175
176             case 'x':
177             case 'X':
178             case '\r':
179             case '\n': {
180                 i = menu_line - 1;
181                 ask = false;
182                 break;
183             }
184             }
185         }
186         /* Request redraw */
187         if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && ask)) {
188             /* Show the list */
189             if (!redraw || use_menu) {
190                 char psi_desc[80];
191                 int line;
192                 redraw = true;
193                 if (!use_menu)
194                     screen_save();
195
196                 /* Display a list of spells */
197                 prt("", y, x);
198                 put_str(_("名前              Lv  MP      名前              Lv  MP ", "name              Lv  SP      name              Lv  SP "), y, x + 5);
199                 prt("", y + 1, x);
200                 /* Dump the spells */
201                 for (i = 0, line = 0; i < 32; i++) {
202                     spell = technic_info[TECHNIC_HISSATSU][i];
203
204                     if (spell.slevel > PY_MAX_LEVEL)
205                         continue;
206                     line++;
207                     if (!(player_ptr->spell_learned1 >> i))
208                         break;
209
210                     /* Access the spell */
211                     if (spell.slevel > plev)
212                         continue;
213                     if (!(player_ptr->spell_learned1 & (1UL << i)))
214                         continue;
215                     if (use_menu) {
216                         if (i == (menu_line - 1))
217                             strcpy(psi_desc, _("  》", "  > "));
218                         else
219                             strcpy(psi_desc, "    ");
220
221                     } else {
222                         char letter;
223                         if (line <= 26)
224                             letter = I2A(line - 1);
225                         else
226                             letter = '0' + line - 27;
227                         sprintf(psi_desc, "  %c)", letter);
228                     }
229
230                     /* Dump the spell --(-- */
231                     strcat(psi_desc, format(" %-18s%2d %3d", exe_spell(player_ptr, REALM_HISSATSU, i, SpellProcessType::NAME), spell.slevel, spell.smana));
232                     prt(psi_desc, y + (line % 17) + (line >= 17), x + (line / 17) * 30);
233                     prt("", y + (line % 17) + (line >= 17) + 1, x + (line / 17) * 30);
234                 }
235             }
236
237             /* Hide the list */
238             else {
239                 /* Hide list */
240                 redraw = false;
241                 screen_load();
242             }
243
244             /* Redo asking */
245             continue;
246         }
247
248         if (!use_menu) {
249             if (isalpha(choice)) {
250                 /* Note verify */
251                 ask = (isupper(choice));
252
253                 /* Lowercase */
254                 if (ask)
255                     choice = (char)tolower(choice);
256
257                 /* Extract request */
258                 i = (islower(choice) ? A2I(choice) : -1);
259             } else {
260                 ask = false; /* Can't uppercase digits */
261
262                 i = choice - '0' + 26;
263             }
264         }
265
266         /* Totally Illegal */
267         if ((i < 0) || (i >= 32) || !(player_ptr->spell_learned1 & (1U << sentaku[i]))) {
268             bell();
269             continue;
270         }
271
272         j = sentaku[i];
273
274         /* Verify it */
275         if (ask) {
276             char tmp_val[160];
277
278             /* Prompt */
279             (void)strnfmt(tmp_val, 78, _("%sを使いますか?", "Use %s? "), exe_spell(player_ptr, REALM_HISSATSU, j, SpellProcessType::NAME));
280
281             /* Belay that order */
282             if (!get_check(tmp_val))
283                 continue;
284         }
285
286         /* Stop the loop */
287         flag = true;
288     }
289     if (redraw)
290         screen_load();
291
292     player_ptr->window_flags |= (PW_SPELL);
293     handle_stuff(player_ptr);
294
295     /* Abort if needed */
296     if (!flag)
297         return false;
298
299     /* Save the choice */
300     (*sn) = j;
301
302     repeat_push((COMMAND_CODE)j);
303
304     /* Success */
305     return true;
306 }
307
308 /*!
309  * @brief 剣術コマンドのメインルーチン
310  */
311 void do_cmd_hissatsu(player_type *player_ptr)
312 {
313     SPELL_IDX n = 0;
314     magic_type spell;
315
316     if (cmd_limit_confused(player_ptr))
317         return;
318     if (!has_melee_weapon(player_ptr, INVEN_MAIN_HAND) && !has_melee_weapon(player_ptr, INVEN_SUB_HAND)) {
319         if (flush_failure)
320             flush();
321         msg_print(_("武器を持たないと必殺技は使えない!", "You need to wield a weapon!"));
322         return;
323     }
324     if (!player_ptr->spell_learned1) {
325         msg_print(_("何も技を知らない。", "You don't know any special attacks."));
326         return;
327     }
328
329     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::IAI, SamuraiStanceType::FUUJIN, SamuraiStanceType::KOUKIJIN });
330
331     if (!get_hissatsu_power(player_ptr, &n))
332         return;
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         /* Warning */
341         msg_print(_("MPが足りません。", "You do not have enough mana to use this power."));
342         msg_print(nullptr);
343         return;
344     }
345
346     sound(SOUND_ZAP);
347
348     if (!exe_spell(player_ptr, REALM_HISSATSU, n, SpellProcessType::CAST))
349         return;
350
351     PlayerEnergy(player_ptr).set_player_turn_energy(100);
352
353     /* Use some mana */
354     player_ptr->csp -= spell.smana;
355
356     /* Limit */
357     if (player_ptr->csp < 0)
358         player_ptr->csp = 0;
359     player_ptr->redraw |= (PR_MANA);
360     player_ptr->window_flags |= (PW_PLAYER | PW_SPELL);
361 }
362
363 /*!
364  * @brief 剣術コマンドの学習
365  */
366 void do_cmd_gain_hissatsu(player_type *player_ptr)
367 {
368     OBJECT_IDX item;
369     int i, j;
370
371     object_type *o_ptr;
372     concptr q, s;
373
374     bool gain = false;
375
376     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::KOUKIJIN });
377
378     if (cmd_limit_blind(player_ptr))
379         return;
380     if (cmd_limit_confused(player_ptr))
381         return;
382
383     if (!(player_ptr->new_spells)) {
384         msg_print(_("新しい必殺技を覚えることはできない!", "You cannot learn any new special attacks!"));
385         return;
386     }
387
388 #ifdef JP
389     msg_format("あと %d 種の必殺技を学べる。", player_ptr->new_spells);
390 #else
391     msg_format("You can learn %d new special attack%s.", player_ptr->new_spells, (player_ptr->new_spells == 1 ? "" : "s"));
392 #endif
393
394     q = _("どの書から学びますか? ", "Study which book? ");
395     s = _("読める書がない。", "You have no books that you can read.");
396
397     o_ptr = choose_object(player_ptr, &item, q, s, (USE_INVEN | USE_FLOOR), TvalItemTester(ItemKindType::HISSATSU_BOOK));
398     if (!o_ptr)
399         return;
400
401     for (i = o_ptr->sval * 8; i < o_ptr->sval * 8 + 8; i++) {
402         if (player_ptr->spell_learned1 & (1UL << i))
403             continue;
404         if (technic_info[TECHNIC_HISSATSU][i].slevel > player_ptr->lev)
405             continue;
406
407         player_ptr->spell_learned1 |= (1UL << i);
408         player_ptr->spell_worked1 |= (1UL << i);
409         msg_format(_("%sの技を覚えた。", "You have learned the special attack of %s."), exe_spell(player_ptr, REALM_HISSATSU, i, SpellProcessType::NAME));
410         for (j = 0; j < 64; j++) {
411             /* Stop at the first empty space */
412             if (player_ptr->spell_order[j] == 99)
413                 break;
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     player_ptr->update |= (PU_SPELLS);
426 }