OSDN Git Service

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