OSDN Git Service

[Refactor] path_g を projection_path クラスに変更
[hengbandforosx/hengbandosx.git] / src / mind / mind-ninja.cpp
1 #include "mind/mind-ninja.h"
2 #include "cmd-action/cmd-attack.h"
3 #include "cmd-item/cmd-throw.h"
4 #include "combat/combat-options-type.h"
5 #include "core/disturbance.h"
6 #include "core/player-redraw-types.h"
7 #include "effect/attribute-types.h"
8 #include "effect/effect-characteristics.h"
9 #include "effect/effect-processor.h"
10 #include "effect/spells-effect-util.h"
11 #include "floor/cave.h"
12 #include "floor/floor-object.h"
13 #include "floor/floor-util.h"
14 #include "floor/geometry.h"
15 #include "game-option/disturbance-options.h"
16 #include "grid/feature.h"
17 #include "grid/grid.h"
18 #include "inventory/inventory-slot-types.h"
19 #include "mind/mind-mirror-master.h"
20 #include "mind/mind-numbers.h"
21 #include "mind/mind-warrior.h"
22 #include "monster-race/monster-race.h"
23 #include "monster-race/race-flags-resistance.h"
24 #include "monster-race/race-indice-types.h"
25 #include "monster/monster-describer.h"
26 #include "monster/monster-status.h"
27 #include "monster/monster-update.h"
28 #include "object-enchant/trc-types.h"
29 #include "object/object-kind-hook.h"
30 #include "player-attack/player-attack-util.h"
31 #include "player-base/player-class.h"
32 #include "player-info/equipment-info.h"
33 #include "player-info/ninja-data-type.h"
34 #include "player-status/player-energy.h"
35 #include "player/attack-defense-types.h"
36 #include "player/player-status-flags.h"
37 #include "player/special-defense-types.h"
38 #include "spell-kind/spells-detection.h"
39 #include "spell-kind/spells-fetcher.h"
40 #include "spell-kind/spells-floor.h"
41 #include "spell-kind/spells-grid.h"
42 #include "spell-kind/spells-launcher.h"
43 #include "spell-kind/spells-lite.h"
44 #include "spell-kind/spells-perception.h"
45 #include "spell-kind/spells-teleport.h"
46 #include "spell/spells-status.h"
47 #include "status/action-setter.h"
48 #include "status/body-improvement.h"
49 #include "status/element-resistance.h"
50 #include "status/temporary-resistance.h"
51 #include "system/floor-type-definition.h"
52 #include "system/grid-type-definition.h"
53 #include "system/monster-race-definition.h"
54 #include "system/monster-type-definition.h"
55 #include "system/object-type-definition.h"
56 #include "system/player-type-definition.h"
57 #include "target/projection-path-calculator.h"
58 #include "target/target-checker.h"
59 #include "target/target-getter.h"
60 #include "timed-effect/player-confusion.h"
61 #include "timed-effect/player-hallucination.h"
62 #include "timed-effect/player-paralysis.h"
63 #include "timed-effect/player-stun.h"
64 #include "timed-effect/timed-effects.h"
65 #include "util/bit-flags-calculator.h"
66 #include "view/display-messages.h"
67
68 /*!
69  * @brief 変わり身処理
70  * @param player_ptr プレイヤーへの参照ポインタ
71  * @param success 判定成功上の処理ならばTRUE
72  * @return 作用が実際にあった場合TRUEを返す
73  */
74 bool kawarimi(PlayerType *player_ptr, bool success)
75 {
76     auto ninja_data = PlayerClass(player_ptr).get_specific_data<ninja_data_type>();
77     if (!ninja_data || !ninja_data->kawarimi) {
78         return false;
79     }
80
81     ObjectType forge;
82     auto *q_ptr = &forge;
83     if (player_ptr->is_dead) {
84         return false;
85     }
86
87     auto effects = player_ptr->effects();
88     auto is_confused = effects->confusion()->is_confused();
89     auto is_hallucinated = effects->hallucination()->is_hallucinated();
90     auto is_paralyzed = effects->paralysis()->is_paralyzed();
91     if (is_confused || player_ptr->blind || is_paralyzed || is_hallucinated) {
92         return false;
93     }
94
95     if (effects->stun()->current() > randint0(200)) {
96         return false;
97     }
98
99     if (!success && one_in_(3)) {
100         msg_print(_("変わり身失敗!逃げられなかった。", "Kawarimi failed! You couldn't run away."));
101         ninja_data->kawarimi = false;
102         player_ptr->redraw |= (PR_STATUS);
103         return false;
104     }
105
106     POSITION y = player_ptr->y;
107     POSITION x = player_ptr->x;
108
109     teleport_player(player_ptr, 10 + randint1(90), TELEPORT_SPONTANEOUS);
110     q_ptr->wipe();
111     const int SV_WOODEN_STATUE = 0;
112     q_ptr->prep(lookup_kind(ItemKindType::STATUE, SV_WOODEN_STATUE));
113
114     q_ptr->pval = enum2i(MonsterRaceId::NINJA);
115     (void)drop_near(player_ptr, q_ptr, -1, y, x);
116
117     if (success) {
118         msg_print(_("攻撃を受ける前に素早く身をひるがえした。", "You have turned around just before the attack hit you."));
119     } else {
120         msg_print(_("変わり身失敗!攻撃を受けてしまった。", "Kawarimi failed! You are hit by the attack."));
121     }
122
123     ninja_data->kawarimi = false;
124     player_ptr->redraw |= (PR_STATUS);
125     return true;
126 }
127
128 /*!
129  * @brief 入身処理 / "Rush Attack" routine for Samurai or Ninja
130  * @param player_ptr プレイヤーへの参照ポインタ
131  * @param mdeath 目標モンスターが死亡したかを返す
132  * @return 作用が実際にあった場合TRUEを返す /  Return value is for checking "done"
133  */
134 bool rush_attack(PlayerType *player_ptr, bool *mdeath)
135 {
136     if (mdeath) {
137         *mdeath = false;
138     }
139
140     project_length = 5;
141     DIRECTION dir;
142     if (!get_aim_dir(player_ptr, &dir)) {
143         return false;
144     }
145
146     int tx = player_ptr->x + project_length * ddx[dir];
147     int ty = player_ptr->y + project_length * ddy[dir];
148
149     if ((dir == 5) && target_okay(player_ptr)) {
150         tx = target_col;
151         ty = target_row;
152     }
153
154     int tm_idx = 0;
155     auto *floor_ptr = player_ptr->current_floor_ptr;
156     if (in_bounds(floor_ptr, ty, tx)) {
157         tm_idx = floor_ptr->grid_array[ty][tx].m_idx;
158     }
159
160     projection_path path_g(player_ptr, project_length, player_ptr->y, player_ptr->x, ty, tx, PROJECT_STOP | PROJECT_KILL);
161     project_length = 0;
162     if (path_g.path_num() == 0) {
163         return true;
164     }
165
166     ty = player_ptr->y;
167     tx = player_ptr->x;
168     bool tmp_mdeath = false;
169     bool moved = false;
170     for (const auto &[ny, nx] : path_g) {
171         monster_type *m_ptr;
172
173         if (is_cave_empty_bold(player_ptr, ny, nx) && player_can_enter(player_ptr, floor_ptr->grid_array[ny][nx].feat, 0)) {
174             ty = ny;
175             tx = nx;
176             continue;
177         }
178
179         if (!floor_ptr->grid_array[ny][nx].m_idx) {
180             if (tm_idx) {
181                 msg_print(_("失敗!", "Failed!"));
182             } else {
183                 msg_print(_("ここには入身では入れない。", "You can't move to that place."));
184             }
185
186             break;
187         }
188
189         if (!player_bold(player_ptr, ty, tx)) {
190             teleport_player_to(player_ptr, ty, tx, TELEPORT_NONMAGICAL);
191         }
192         update_monster(player_ptr, floor_ptr->grid_array[ny][nx].m_idx, true);
193
194         m_ptr = &floor_ptr->m_list[floor_ptr->grid_array[ny][nx].m_idx];
195         if (tm_idx != floor_ptr->grid_array[ny][nx].m_idx) {
196 #ifdef JP
197             msg_format("%s%sが立ちふさがっている!", tm_idx ? "別の" : "", m_ptr->ml ? "モンスター" : "何か");
198 #else
199             msg_format("There is %s in the way!", m_ptr->ml ? (tm_idx ? "another monster" : "a monster") : "someone");
200 #endif
201         } else if (!player_bold(player_ptr, ty, tx)) {
202             GAME_TEXT m_name[MAX_NLEN];
203             monster_desc(player_ptr, m_name, m_ptr, 0);
204             msg_format(_("素早く%sの懐に入り込んだ!", "You quickly jump in and attack %s!"), m_name);
205         }
206
207         if (!player_bold(player_ptr, ty, tx)) {
208             teleport_player_to(player_ptr, ty, tx, TELEPORT_NONMAGICAL);
209         }
210         moved = true;
211         tmp_mdeath = do_cmd_attack(player_ptr, ny, nx, HISSATSU_NYUSIN);
212
213         break;
214     }
215
216     if (!moved && !player_bold(player_ptr, ty, tx)) {
217         teleport_player_to(player_ptr, ty, tx, TELEPORT_NONMAGICAL);
218     }
219
220     if (mdeath) {
221         *mdeath = tmp_mdeath;
222     }
223     return true;
224 }
225
226 /*!
227  * @brief 盗賊と忍者における不意打ち
228  * @param player_ptr プレイヤーへの参照ポインタ
229  * @param pa_ptr 直接攻撃構造体への参照ポインタ
230  */
231 void process_surprise_attack(PlayerType *player_ptr, player_attack_type *pa_ptr)
232 {
233     auto *r_ptr = &r_info[pa_ptr->m_ptr->r_idx];
234     if (!has_melee_weapon(player_ptr, INVEN_MAIN_HAND + pa_ptr->hand) || player_ptr->is_icky_wield[pa_ptr->hand]) {
235         return;
236     }
237
238     int tmp = player_ptr->lev * 6 + (player_ptr->skill_stl + 10) * 4;
239     if (player_ptr->monlite && (pa_ptr->mode != HISSATSU_NYUSIN)) {
240         tmp /= 3;
241     }
242     if (has_aggravate(player_ptr)) {
243         tmp /= 2;
244     }
245     if (r_ptr->level > (player_ptr->lev * player_ptr->lev / 20 + 10)) {
246         tmp /= 3;
247     }
248
249     auto ninja_data = PlayerClass(player_ptr).get_specific_data<ninja_data_type>();
250     if (monster_csleep_remaining(pa_ptr->m_ptr) && pa_ptr->m_ptr->ml) {
251         /* Can't backstab creatures that we can't see, right? */
252         pa_ptr->backstab = true;
253     } else if ((ninja_data && ninja_data->s_stealth) && (randint0(tmp) > (r_ptr->level + 20)) &&
254                pa_ptr->m_ptr->ml && !r_ptr->resistance_flags.has(MonsterResistanceType::RESIST_ALL)) {
255         pa_ptr->surprise_attack = true;
256     } else if (monster_fear_remaining(pa_ptr->m_ptr) && pa_ptr->m_ptr->ml) {
257         pa_ptr->stab_fleeing = true;
258     }
259 }
260
261 void print_surprise_attack(player_attack_type *pa_ptr)
262 {
263     if (pa_ptr->backstab) {
264         msg_format(_("あなたは冷酷にも眠っている無力な%sを突き刺した!", "You cruelly stab the helpless, sleeping %s!"), pa_ptr->m_name);
265     } else if (pa_ptr->surprise_attack) {
266         msg_format(_("不意を突いて%sに強烈な一撃を喰らわせた!", "You make surprise attack, and hit %s with a powerful blow!"), pa_ptr->m_name);
267     } else if (pa_ptr->stab_fleeing) {
268         msg_format(_("逃げる%sを背中から突き刺した!", "You backstab the fleeing %s!"), pa_ptr->m_name);
269     } else if (!pa_ptr->monk_attack) {
270         msg_format(_("%sを攻撃した。", "You hit %s."), pa_ptr->m_name);
271     }
272 }
273
274 /*!
275  * @brief 盗賊と忍者における不意打ちのダメージ計算
276  * @param player_ptr プレイヤーへの参照ポインタ
277  * @param pa_ptr 直接攻撃構造体への参照ポインタ
278  */
279 void calc_surprise_attack_damage(PlayerType *player_ptr, player_attack_type *pa_ptr)
280 {
281     if (pa_ptr->backstab) {
282         pa_ptr->attack_damage *= (3 + (player_ptr->lev / 20));
283         return;
284     }
285
286     if (pa_ptr->surprise_attack) {
287         pa_ptr->attack_damage = pa_ptr->attack_damage * (5 + (player_ptr->lev * 2 / 25)) / 2;
288         return;
289     }
290
291     if (pa_ptr->stab_fleeing) {
292         pa_ptr->attack_damage = (3 * pa_ptr->attack_damage) / 2;
293     }
294 }
295
296 /*!
297  * @brief 速駆け処理
298  * @param player_ptr プレイヤーへの参照ポインタ
299  * @return 常にTRUE
300  */
301 bool hayagake(PlayerType *player_ptr)
302 {
303     PlayerEnergy energy(player_ptr);
304     if (player_ptr->action == ACTION_HAYAGAKE) {
305         set_action(player_ptr, ACTION_NONE);
306         energy.reset_player_turn();
307         return true;
308     }
309
310     auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x];
311     auto *f_ptr = &f_info[g_ptr->feat];
312
313     if (f_ptr->flags.has_not(FloorFeatureType::PROJECT) || (!player_ptr->levitation && f_ptr->flags.has(FloorFeatureType::DEEP))) {
314         msg_print(_("ここでは素早く動けない。", "You cannot run in here."));
315     } else {
316         set_action(player_ptr, ACTION_HAYAGAKE);
317     }
318
319     energy.reset_player_turn();
320     return true;
321 }
322
323 /*!
324  * @brief 超隠密状態をセットする
325  * @param set TRUEならば超隠密状態になる。
326  * @return ステータスに影響を及ぼす変化があった場合TRUEを返す。
327  */
328 bool set_superstealth(PlayerType *player_ptr, bool set)
329 {
330     bool notice = false;
331
332     auto ninja_data = PlayerClass(player_ptr).get_specific_data<ninja_data_type>();
333     if (!ninja_data || player_ptr->is_dead) {
334         return false;
335     }
336
337     if (set) {
338         if (!ninja_data->s_stealth) {
339             if (player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].info & CAVE_MNLT) {
340                 msg_print(_("敵の目から薄い影の中に覆い隠された。", "You are mantled in weak shadow from ordinary eyes."));
341                 player_ptr->monlite = player_ptr->old_monlite = true;
342             } else {
343                 msg_print(_("敵の目から影の中に覆い隠された!", "You are mantled in shadow from ordinary eyes!"));
344                 player_ptr->monlite = player_ptr->old_monlite = false;
345             }
346
347             notice = true;
348             ninja_data->s_stealth = true;
349         }
350     } else {
351         if (ninja_data->s_stealth) {
352             msg_print(_("再び敵の目にさらされるようになった。", "You are exposed to common sight once more."));
353             notice = true;
354             ninja_data->s_stealth = false;
355         }
356     }
357
358     if (!notice) {
359         return false;
360     }
361     player_ptr->redraw |= (PR_STATUS);
362
363     if (disturb_state) {
364         disturb(player_ptr, false, false);
365     }
366     return true;
367 }
368
369 /*!
370  * @brief 忍術の発動 /
371  * do_cmd_cast calls this function if the player's class is 'ninja'.
372  * @param player_ptr プレイヤーへの参照ポインタ
373  * @param spell 発動する特殊技能のID
374  * @return 処理を実行したらTRUE、キャンセルした場合FALSEを返す。
375  */
376 bool cast_ninja_spell(PlayerType *player_ptr, mind_ninja_type spell)
377 {
378     POSITION x = 0, y = 0;
379     DIRECTION dir;
380     PLAYER_LEVEL plev = player_ptr->lev;
381     auto ninja_data = PlayerClass(player_ptr).get_specific_data<ninja_data_type>();
382     switch (spell) {
383     case DARKNESS_CREATION:
384         (void)unlite_area(player_ptr, 0, 3);
385         break;
386     case DETECT_NEAR:
387         if (plev > 44) {
388             wiz_lite(player_ptr, true);
389         }
390
391         detect_monsters_normal(player_ptr, DETECT_RAD_DEFAULT);
392         if (plev > 4) {
393             detect_traps(player_ptr, DETECT_RAD_DEFAULT, true);
394             detect_doors(player_ptr, DETECT_RAD_DEFAULT);
395             detect_stairs(player_ptr, DETECT_RAD_DEFAULT);
396         }
397
398         if (plev > 14) {
399             detect_objects_normal(player_ptr, DETECT_RAD_DEFAULT);
400         }
401
402         break;
403     case HIDE_LEAVES:
404         teleport_player(player_ptr, 10, TELEPORT_SPONTANEOUS);
405         break;
406     case KAWARIMI:
407         if (ninja_data && !ninja_data->kawarimi) {
408             msg_print(_("敵の攻撃に対して敏感になった。", "You are now prepared to evade any attacks."));
409             ninja_data->kawarimi = true;
410             player_ptr->redraw |= (PR_STATUS);
411         }
412
413         break;
414     case ABSCONDING:
415         teleport_player(player_ptr, player_ptr->lev * 5, TELEPORT_SPONTANEOUS);
416         break;
417     case HIT_AND_AWAY:
418         if (!hit_and_away(player_ptr)) {
419             return false;
420         }
421
422         break;
423     case BIND_MONSTER:
424         if (!get_aim_dir(player_ptr, &dir)) {
425             return false;
426         }
427
428         (void)stasis_monster(player_ptr, dir);
429         break;
430     case ANCIENT_KNOWLEDGE:
431         return ident_spell(player_ptr, false);
432     case FLOATING:
433         set_tim_levitation(player_ptr, randint1(20) + 20, false);
434         break;
435     case HIDE_FLAMES:
436         fire_ball(player_ptr, AttributeType::FIRE, 0, 50 + plev, plev / 10 + 2);
437         teleport_player(player_ptr, 30, TELEPORT_SPONTANEOUS);
438         set_oppose_fire(player_ptr, (TIME_EFFECT)plev, false);
439         break;
440     case NYUSIN:
441         return rush_attack(player_ptr, nullptr);
442     case SYURIKEN_SPREADING: {
443         for (int i = 0; i < 8; i++) {
444             OBJECT_IDX slot;
445
446             for (slot = 0; slot < INVEN_PACK; slot++) {
447                 if (player_ptr->inventory_list[slot].tval == ItemKindType::SPIKE) {
448                     break;
449                 }
450             }
451
452             if (slot == INVEN_PACK) {
453                 if (!i) {
454                     msg_print(_("くさびを持っていない。", "You have no Iron Spikes."));
455                 } else {
456                     msg_print(_("くさびがなくなった。", "You have no more Iron Spikes."));
457                 }
458
459                 return false;
460             }
461
462             (void)ThrowCommand(player_ptr).do_cmd_throw(1, false, slot);
463             PlayerEnergy(player_ptr).set_player_turn_energy(100);
464         }
465
466         break;
467     }
468     case CHAIN_HOOK:
469         (void)fetch_monster(player_ptr);
470         break;
471     case SMOKE_BALL:
472         if (!get_aim_dir(player_ptr, &dir)) {
473             return false;
474         }
475
476         fire_ball(player_ptr, AttributeType::OLD_CONF, dir, plev * 3, 3);
477         break;
478     case SWAP_POSITION:
479         project_length = -1;
480         if (!get_aim_dir(player_ptr, &dir)) {
481             project_length = 0;
482             return false;
483         }
484
485         project_length = 0;
486         (void)teleport_swap(player_ptr, dir);
487         break;
488     case EXPLOSIVE_RUNE:
489         create_rune_explosion(player_ptr, player_ptr->y, player_ptr->x);
490         break;
491     case HIDE_MUD:
492         (void)set_pass_wall(player_ptr, randint1(plev / 2) + plev / 2, false);
493         set_oppose_acid(player_ptr, (TIME_EFFECT)plev, false);
494         break;
495     case HIDE_MIST:
496         fire_ball(player_ptr, AttributeType::POIS, 0, 75 + plev * 2 / 3, plev / 5 + 2);
497         fire_ball(player_ptr, AttributeType::HYPODYNAMIA, 0, 75 + plev * 2 / 3, plev / 5 + 2);
498         fire_ball(player_ptr, AttributeType::CONFUSION, 0, 75 + plev * 2 / 3, plev / 5 + 2);
499         teleport_player(player_ptr, 30, TELEPORT_SPONTANEOUS);
500         break;
501     case PURGATORY_FLAME: {
502         int num = damroll(3, 9);
503         for (int k = 0; k < num; k++) {
504             AttributeType typ = one_in_(2) ? AttributeType::FIRE : one_in_(3) ? AttributeType::NETHER
505                                                                               : AttributeType::PLASMA;
506             int attempts = 1000;
507             while (attempts--) {
508                 scatter(player_ptr, &y, &x, player_ptr->y, player_ptr->x, 4, PROJECT_NONE);
509                 if (!player_bold(player_ptr, y, x)) {
510                     break;
511                 }
512             }
513
514             project(player_ptr, 0, 0, y, x, damroll(6 + plev / 8, 10), typ, (PROJECT_BEAM | PROJECT_THRU | PROJECT_GRID | PROJECT_KILL));
515         }
516
517         break;
518     }
519     case ALTER_EGO:
520         set_multishadow(player_ptr, 6 + randint1(6), false);
521         break;
522     default:
523         msg_print(_("なに?", "Zap?"));
524         break;
525     }
526     return true;
527 }