OSDN Git Service

d926dd876d277bb5006e791d8dd87f789720a56f
[hengband/hengband.git] / src / pet / pet-fall-off.c
1 /*!
2  * @brief 落馬処理
3  * @date 2020/05/31
4  * @author Hourier
5  */
6
7 #include "pet/pet-fall-off.h"
8 #include "core/stuff-handler.h"
9 #include "floor/floor.h"
10 #include "io/targeting.h"
11 #include "monster/monster-describer.h"
12 #include "pet/pet-util.h"
13 #include "player/player-damage.h"
14 #include "player/player-move.h"
15 #include "player/player-skill.h"
16 #include "view/display-messages.h"
17
18 /*!
19  * @brief モンスターから直接攻撃を受けた時に落馬するかどうかを判定し、判定アウトならば落馬させる
20  * @param creature_ptr プレーヤーへの参照ポインタ
21  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
22  * @return なし
23  */
24 void check_fall_off_horse(player_type *creature_ptr, monap_type *monap_ptr)
25 {
26     if ((creature_ptr->riding == 0) || (monap_ptr->damage == 0))
27         return;
28
29     char m_steed_name[MAX_NLEN];
30     monster_desc(creature_ptr, m_steed_name, &creature_ptr->current_floor_ptr->m_list[creature_ptr->riding], 0);
31     if (process_fall_off_horse(creature_ptr, (monap_ptr->damage > 200) ? 200 : monap_ptr->damage, FALSE))
32         msg_format(_("%^sから落ちてしまった!", "You have fallen from %s."), m_steed_name);
33 }
34
35 /*!
36  * @brief 落馬する可能性を計算する
37  * @param creature_ptr プレーヤーへの参照ポインタ
38  * @param dam 落馬判定を発した際に受けたダメージ量
39  * @param force TRUEならば強制的に落馬する
40  * @param 乗馬中のモンスターのレベル
41  * @return FALSEなら落馬しないことで確定、TRUEなら処理続行
42  * @details レベルの低い乗馬からは落馬しにくい
43  */
44 static bool calc_fall_off_possibility(player_type *creature_ptr, const HIT_POINT dam, const bool force, monster_race *r_ptr)
45 {
46     if (force)
47         return TRUE;
48
49     int cur = creature_ptr->skill_exp[GINOU_RIDING];
50     int max = s_info[creature_ptr->pclass].s_max[GINOU_RIDING];
51     int ridinglevel = r_ptr->level;
52
53     int fall_off_level = r_ptr->level;
54     if (creature_ptr->riding_ryoute)
55         fall_off_level += 20;
56
57     if ((cur < max) && (max > 1000) && (dam / 2 + ridinglevel) > (cur / 30 + 10)) {
58         int inc = 0;
59         if (ridinglevel > (cur / 100 + 15))
60             inc += 1 + (ridinglevel - cur / 100 - 15);
61         else
62             inc += 1;
63
64         creature_ptr->skill_exp[GINOU_RIDING] = MIN(max, cur + inc);
65     }
66
67     if (randint0(dam / 2 + fall_off_level * 2) >= cur / 30 + 10)
68         return TRUE;
69
70     if ((((creature_ptr->pclass == CLASS_BEASTMASTER) || (creature_ptr->pclass == CLASS_CAVALRY)) && !creature_ptr->riding_ryoute)
71         || !one_in_(creature_ptr->lev * (creature_ptr->riding_ryoute ? 2 : 3) + 30)) {
72         return FALSE;
73     }
74
75     return TRUE;
76 }
77
78 /*!
79  * @brief プレイヤーの落馬判定処理
80  * @param dam 落馬判定を発した際に受けたダメージ量
81  * @param force TRUEならば強制的に落馬する
82  * @return 実際に落馬したらTRUEを返す
83  */
84 bool process_fall_off_horse(player_type *creature_ptr, HIT_POINT dam, bool force)
85 {
86     POSITION sy = 0, sx = 0;
87     int sn = 0;
88     GAME_TEXT m_name[MAX_NLEN];
89     monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[creature_ptr->riding];
90     monster_race *r_ptr = &r_info[m_ptr->r_idx];
91
92     if (!creature_ptr->riding)
93         return FALSE;
94     if (creature_ptr->wild_mode)
95         return FALSE;
96
97     if (dam >= 0 || force) {
98         calc_fall_off_possibility(creature_ptr, dam, force, r_ptr);
99         
100         /* Check around the player */
101         for (DIRECTION i = 0; i < 8; i++) {
102             POSITION y = creature_ptr->y + ddy_ddd[i];
103             POSITION x = creature_ptr->x + ddx_ddd[i];
104
105             grid_type *g_ptr;
106             g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
107
108             if (g_ptr->m_idx)
109                 continue;
110
111             /* Skip non-empty grids */
112             if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !cave_have_flag_grid(g_ptr, FF_CAN_FLY)) {
113                 if (!can_player_ride_pet(creature_ptr, g_ptr, FALSE))
114                     continue;
115             }
116
117             if (cave_have_flag_grid(g_ptr, FF_PATTERN))
118                 continue;
119
120             /* Count "safe" grids */
121             sn++;
122
123             /* Randomize choice */
124             if (randint0(sn) > 0)
125                 continue;
126
127             /* Save the safe location */
128             sy = y;
129             sx = x;
130         }
131
132         if (!sn) {
133             monster_desc(creature_ptr, m_name, m_ptr, 0);
134             msg_format(_("%sから振り落とされそうになって、壁にぶつかった。", "You have nearly fallen from %s, but bumped into wall."), m_name);
135             take_hit(creature_ptr, DAMAGE_NOESCAPE, r_ptr->level + 3, _("壁への衝突", "bumping into wall"), -1);
136             return FALSE;
137         }
138
139         POSITION old_y = creature_ptr->y;
140         POSITION old_x = creature_ptr->x;
141         creature_ptr->y = sy;
142         creature_ptr->x = sx;
143         lite_spot(creature_ptr, old_y, old_x);
144         lite_spot(creature_ptr, creature_ptr->y, creature_ptr->x);
145         verify_panel(creature_ptr);
146     }
147
148     creature_ptr->riding = 0;
149     creature_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
150     creature_ptr->riding_ryoute = creature_ptr->old_riding_ryoute = FALSE;
151
152     creature_ptr->update |= (PU_BONUS | PU_VIEW | PU_LITE | PU_FLOW | PU_MON_LITE | PU_MONSTERS);
153     handle_stuff(creature_ptr);
154
155     creature_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
156     creature_ptr->redraw |= (PR_EXTRA);
157
158     /* Update health track of mount */
159     creature_ptr->redraw |= (PR_UHEALTH);
160
161     bool fall_dam = FALSE;
162     if (creature_ptr->levitation && !force) {
163         monster_desc(creature_ptr, m_name, m_ptr, 0);
164         msg_format(_("%sから落ちたが、空中でうまく体勢を立て直して着地した。", "You are thrown from %s, but make a good landing."), m_name);
165     } else {
166         take_hit(creature_ptr, DAMAGE_NOESCAPE, r_ptr->level + 3, _("落馬", "Falling from riding"), -1);
167         fall_dam = TRUE;
168     }
169
170     if (sy && !creature_ptr->is_dead)
171         (void)move_player_effect(creature_ptr, creature_ptr->y, creature_ptr->x, MPE_DONT_PICKUP | MPE_DONT_SWAP_MON);
172
173     return fall_dam;
174 }