OSDN Git Service

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