OSDN Git Service

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