OSDN Git Service

Merge pull request #2946 from backwardsEric/sprintf-refactor-tombstone-generation
[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 "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     /* Build a prompt (accept all spells) */
105     (void)strnfmt(out_val, 78, _("(%^s %c-%c, '*'で一覧, ESC) どの%sを使いますか?", "(%^ss %c-%c, *=List, ESC=exit) Use which %s? "), p, I2A(0),
106         "abcdefghijklmnopqrstuvwxyz012345"[num - 1], p);
107
108     if (use_menu) {
109         screen_save();
110     }
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
120         auto should_redraw_cursor = true;
121         if (use_menu && choice != ' ') {
122             switch (choice) {
123             case '0': {
124                 screen_load();
125                 return false;
126             }
127
128             case '8':
129             case 'k':
130             case 'K': {
131                 do {
132                     menu_line += 31;
133                     if (menu_line > 32) {
134                         menu_line -= 32;
135                     }
136                 } while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1))));
137                 break;
138             }
139
140             case '2':
141             case 'j':
142             case 'J': {
143                 do {
144                     menu_line++;
145                     if (menu_line > 32) {
146                         menu_line -= 32;
147                     }
148                 } while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1))));
149                 break;
150             }
151
152             case '4':
153             case 'h':
154             case 'H':
155             case '6':
156             case 'l':
157             case 'L': {
158                 bool reverse = false;
159                 if ((choice == '4') || (choice == 'h') || (choice == 'H')) {
160                     reverse = true;
161                 }
162                 if (menu_line > 16) {
163                     menu_line -= 16;
164                     reverse = true;
165                 } else {
166                     menu_line += 16;
167                 }
168                 while (!(player_ptr->spell_learned1 & (1UL << (menu_line - 1)))) {
169                     if (reverse) {
170                         menu_line--;
171                         if (menu_line < 2) {
172                             reverse = false;
173                         }
174                     } else {
175                         menu_line++;
176                         if (menu_line > 31) {
177                             reverse = true;
178                         }
179                     }
180                 }
181                 break;
182             }
183
184             case 'x':
185             case 'X':
186             case '\r':
187             case '\n': {
188                 i = menu_line - 1;
189                 should_redraw_cursor = false;
190                 break;
191             }
192             }
193         }
194         /* Request redraw */
195         if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && should_redraw_cursor)) {
196             /* Show the list */
197             if (!redraw || use_menu) {
198                 char psi_desc[80];
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                     if (use_menu) {
229                         if (i == (menu_line - 1)) {
230                             strcpy(psi_desc, _("  》", "  > "));
231                         } else {
232                             strcpy(psi_desc, "    ");
233                         }
234
235                     } else {
236                         char letter;
237                         if (line <= 26) {
238                             letter = I2A(line - 1);
239                         } else {
240                             letter = '0' + line - 27;
241                         }
242                         sprintf(psi_desc, "  %c)", letter);
243                     }
244
245                     /* Dump the spell --(-- */
246                     const auto spell_name = exe_spell(player_ptr, REALM_HISSATSU, i, SpellProcessType::NAME);
247                     strcat(psi_desc, format(" %-18s%2d %3d", spell_name->data(), spell.slevel, spell.smana));
248                     prt(psi_desc, y + (line % 17) + (line >= 17), x + (line / 17) * 30);
249                     prt("", y + (line % 17) + (line >= 17) + 1, x + (line / 17) * 30);
250                 }
251             }
252
253             /* Hide the list */
254             else {
255                 /* Hide list */
256                 redraw = false;
257                 screen_load();
258             }
259
260             /* Redo asking */
261             continue;
262         }
263
264         if (!use_menu) {
265             if (isalpha(choice)) {
266                 i = A2I(choice);
267             } else {
268                 i = choice - '0' + 26;
269             }
270         }
271
272         /* Totally Illegal */
273         if ((i < 0) || (i >= 32) || !(player_ptr->spell_learned1 & (1U << sentaku[i]))) {
274             bell();
275             continue;
276         }
277
278         j = sentaku[i];
279
280         /* Stop the loop */
281         flag = true;
282     }
283     if (redraw) {
284         screen_load();
285     }
286
287     player_ptr->window_flags |= (PW_SPELL);
288     handle_stuff(player_ptr);
289
290     /* Abort if needed */
291     if (!flag) {
292         return false;
293     }
294
295     /* Save the choice */
296     (*sn) = j;
297
298     repeat_push((COMMAND_CODE)j);
299
300     /* Success */
301     return true;
302 }
303
304 /*!
305  * @brief 剣術コマンドのメインルーチン
306  */
307 void do_cmd_hissatsu(PlayerType *player_ptr)
308 {
309     SPELL_IDX n = 0;
310     magic_type spell;
311
312     if (cmd_limit_confused(player_ptr)) {
313         return;
314     }
315     if (!has_melee_weapon(player_ptr, INVEN_MAIN_HAND) && !has_melee_weapon(player_ptr, INVEN_SUB_HAND)) {
316         if (flush_failure) {
317             flush();
318         }
319         msg_print(_("武器を持たないと必殺技は使えない!", "You need to wield a weapon!"));
320         return;
321     }
322     if (!player_ptr->spell_learned1) {
323         msg_print(_("何も技を知らない。", "You don't know any special attacks."));
324         return;
325     }
326
327     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::IAI, SamuraiStanceType::FUUJIN, SamuraiStanceType::KOUKIJIN });
328
329     if (!get_hissatsu_power(player_ptr, &n)) {
330         return;
331     }
332
333     spell = technic_info[TECHNIC_HISSATSU][n];
334
335     /* Verify "dangerous" spells */
336     if (spell.smana > player_ptr->csp) {
337         if (flush_failure) {
338             flush();
339         }
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
352     PlayerEnergy(player_ptr).set_player_turn_energy(100);
353
354     /* Use some mana */
355     player_ptr->csp -= spell.smana;
356
357     /* Limit */
358     if (player_ptr->csp < 0) {
359         player_ptr->csp = 0;
360     }
361     player_ptr->redraw |= (PR_MANA);
362     player_ptr->window_flags |= (PW_PLAYER | PW_SPELL);
363 }
364
365 /*!
366  * @brief 剣術コマンドの学習
367  */
368 void do_cmd_gain_hissatsu(PlayerType *player_ptr)
369 {
370     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU, SamuraiStanceType::KOUKIJIN });
371     if (cmd_limit_blind(player_ptr) || cmd_limit_confused(player_ptr)) {
372         return;
373     }
374
375     if (!(player_ptr->new_spells)) {
376         msg_print(_("新しい必殺技を覚えることはできない!", "You cannot learn any new special attacks!"));
377         return;
378     }
379
380 #ifdef JP
381     msg_format("あと %d 種の必殺技を学べる。", player_ptr->new_spells);
382 #else
383     msg_format("You can learn %d new special attack%s.", player_ptr->new_spells, (player_ptr->new_spells == 1 ? "" : "s"));
384 #endif
385
386     const auto q = _("どの書から学びますか? ", "Study which book? ");
387     const auto s = _("読める書がない。", "You have no books that you can read.");
388     short item;
389     const auto *o_ptr = choose_object(player_ptr, &item, q, s, (USE_INVEN | USE_FLOOR), TvalItemTester(ItemKindType::HISSATSU_BOOK));
390     if (o_ptr == nullptr) {
391         return;
392     }
393
394     const auto sval = o_ptr->bi_key.sval().value();
395     auto gain = false;
396     for (auto i = sval * 8; i < sval * 8 + 8; i++) {
397         if (player_ptr->spell_learned1 & (1UL << i)) {
398             continue;
399         }
400
401         if (technic_info[TECHNIC_HISSATSU][i].slevel > player_ptr->lev) {
402             continue;
403         }
404
405         player_ptr->spell_learned1 |= (1UL << i);
406         player_ptr->spell_worked1 |= (1UL << i);
407         const auto spell_name = exe_spell(player_ptr, REALM_HISSATSU, i, SpellProcessType::NAME);
408         msg_format(_("%sの技を覚えた。", "You have learned the special attack of %s."), spell_name->data());
409         int j;
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         }
416
417         player_ptr->spell_order[j] = i;
418         gain = true;
419     }
420
421     if (!gain) {
422         msg_print(_("何も覚えられなかった。", "You were not able to learn any special attacks."));
423     } else {
424         PlayerEnergy(player_ptr).set_player_turn_energy(100);
425     }
426
427     player_ptr->update |= (PU_SPELLS);
428 }