OSDN Git Service

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