OSDN Git Service

[Refactor] #3230 PlayerType::update に関わる処理を、RedrawingFlagsUpdaterに集約した
[hengbandforosx/hengbandosx.git] / src / player / player-move.cpp
1 /*!
2  *  @brief プレイヤーの移動処理 / Movement commands
3  *  @date 2014/01/02
4  *  @author
5  *  Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  */
7
8 #include "player/player-move.h"
9 #include "core/disturbance.h"
10 #include "core/player-redraw-types.h"
11 #include "core/player-update-types.h"
12 #include "core/special-internal-keys.h"
13 #include "core/stuff-handler.h"
14 #include "core/window-redrawer.h"
15 #include "dungeon/dungeon-flag-types.h"
16 #include "dungeon/quest.h"
17 #include "effect/attribute-types.h"
18 #include "effect/effect-characteristics.h"
19 #include "effect/effect-processor.h"
20 #include "floor/cave.h"
21 #include "floor/floor-util.h"
22 #include "floor/geometry.h"
23 #include "game-option/disturbance-options.h"
24 #include "grid/feature.h"
25 #include "grid/grid.h"
26 #include "grid/trap.h"
27 #include "inventory/player-inventory.h"
28 #include "io/input-key-requester.h"
29 #include "mind/mind-ninja.h"
30 #include "monster/monster-update.h"
31 #include "perception/object-perception.h"
32 #include "player-base/player-class.h"
33 #include "player-base/player-race.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/player-status.h"
38 #include "realm/realm-song-numbers.h"
39 #include "spell-kind/spells-floor.h"
40 #include "spell-realm/spells-song.h"
41 #include "status/action-setter.h"
42 #include "system/dungeon-info.h"
43 #include "system/floor-type-definition.h"
44 #include "system/grid-type-definition.h"
45 #include "system/item-entity.h"
46 #include "system/monster-entity.h"
47 #include "system/player-type-definition.h"
48 #include "system/redrawing-flags-updater.h"
49 #include "system/terrain-type-definition.h"
50 #include "target/target-checker.h"
51 #include "timed-effect/player-blindness.h"
52 #include "timed-effect/player-confusion.h"
53 #include "timed-effect/player-hallucination.h"
54 #include "timed-effect/timed-effects.h"
55 #include "util/bit-flags-calculator.h"
56 #include "util/enum-converter.h"
57 #include "view/display-messages.h"
58
59 int flow_head = 0;
60 int flow_tail = 0;
61 POSITION temp2_x[MAX_SHORT];
62 POSITION temp2_y[MAX_SHORT];
63
64 /*!
65  * @brief 地形やその上のアイテムの隠された要素を全て明かす /
66  * Search for hidden things
67  * @param player_ptr プレイヤーへの参照ポインタ
68  * @param y 対象となるマスのY座標
69  * @param x 対象となるマスのX座標
70  */
71 static void discover_hidden_things(PlayerType *player_ptr, POSITION y, POSITION x)
72 {
73     grid_type *g_ptr;
74     auto *floor_ptr = player_ptr->current_floor_ptr;
75     g_ptr = &floor_ptr->grid_array[y][x];
76     if (g_ptr->mimic && is_trap(player_ptr, g_ptr->feat)) {
77         disclose_grid(player_ptr, y, x);
78         msg_print(_("トラップを発見した。", "You have found a trap."));
79         disturb(player_ptr, false, true);
80     }
81
82     if (is_hidden_door(player_ptr, g_ptr)) {
83         msg_print(_("隠しドアを発見した。", "You have found a secret door."));
84         disclose_grid(player_ptr, y, x);
85         disturb(player_ptr, false, false);
86     }
87
88     for (const auto this_o_idx : g_ptr->o_idx_list) {
89         ItemEntity *o_ptr;
90         o_ptr = &floor_ptr->o_list[this_o_idx];
91         if (o_ptr->bi_key.tval() != ItemKindType::CHEST) {
92             continue;
93         }
94
95         if (o_ptr->pval <= 0 || chest_traps[o_ptr->pval].none()) {
96             continue;
97         }
98
99         if (!o_ptr->is_known()) {
100             msg_print(_("箱に仕掛けられたトラップを発見した!", "You have discovered a trap on the chest!"));
101             object_known(o_ptr);
102             disturb(player_ptr, false, false);
103         }
104     }
105 }
106
107 /*!
108  * @brief プレイヤーの探索処理判定
109  * @param player_ptr プレイヤーへの参照ポインタ
110  */
111 void search(PlayerType *player_ptr)
112 {
113     PERCENTAGE chance = player_ptr->skill_srh;
114     const auto effects = player_ptr->effects();
115     if (effects->blindness()->is_blind() || no_lite(player_ptr)) {
116         chance = chance / 10;
117     }
118
119     if (effects->confusion()->is_confused() || effects->hallucination()->is_hallucinated()) {
120         chance = chance / 10;
121     }
122
123     for (DIRECTION i = 0; i < 9; ++i) {
124         if (randint0(100) < chance) {
125             discover_hidden_things(player_ptr, player_ptr->y + ddy_ddd[i], player_ptr->x + ddx_ddd[i]);
126         }
127     }
128 }
129
130 /*!
131  * @brief 移動に伴うプレイヤーのステータス変化処理
132  * @param player_ptr プレイヤーへの参照ポインタ
133  * @param ny 移動先Y座標
134  * @param nx 移動先X座標
135  * @param mpe_mode 移動オプションフラグ
136  * @return プレイヤーが死亡やフロア離脱を行わず、実際に移動が可能ならばTRUEを返す。
137  */
138 bool move_player_effect(PlayerType *player_ptr, POSITION ny, POSITION nx, BIT_FLAGS mpe_mode)
139 {
140     POSITION oy = player_ptr->y;
141     POSITION ox = player_ptr->x;
142     auto *floor_ptr = player_ptr->current_floor_ptr;
143     auto *g_ptr = &floor_ptr->grid_array[ny][nx];
144     grid_type *oc_ptr = &floor_ptr->grid_array[oy][ox];
145     auto *f_ptr = &terrains_info[g_ptr->feat];
146     TerrainType *of_ptr = &terrains_info[oc_ptr->feat];
147
148     auto &rfu = RedrawingFlagsUpdater::get_instance();
149     if (!(mpe_mode & MPE_STAYING)) {
150         MONSTER_IDX om_idx = oc_ptr->m_idx;
151         MONSTER_IDX nm_idx = g_ptr->m_idx;
152         player_ptr->y = ny;
153         player_ptr->x = nx;
154         if (!(mpe_mode & MPE_DONT_SWAP_MON)) {
155             g_ptr->m_idx = om_idx;
156             oc_ptr->m_idx = nm_idx;
157             if (om_idx > 0) {
158                 MonsterEntity *om_ptr = &floor_ptr->m_list[om_idx];
159                 om_ptr->fy = ny;
160                 om_ptr->fx = nx;
161                 update_monster(player_ptr, om_idx, true);
162             }
163
164             if (nm_idx > 0) {
165                 MonsterEntity *nm_ptr = &floor_ptr->m_list[nm_idx];
166                 nm_ptr->fy = oy;
167                 nm_ptr->fx = ox;
168                 update_monster(player_ptr, nm_idx, true);
169             }
170         }
171
172         lite_spot(player_ptr, oy, ox);
173         lite_spot(player_ptr, ny, nx);
174         verify_panel(player_ptr);
175         if (mpe_mode & MPE_FORGET_FLOW) {
176             forget_flow(floor_ptr);
177             rfu.set_flag(StatusRedrawingFlag::UN_VIEW);
178             player_ptr->redraw |= PR_MAP;
179         }
180
181         const auto flags_srf = {
182             StatusRedrawingFlag::VIEW,
183             StatusRedrawingFlag::LITE,
184             StatusRedrawingFlag::FLOW,
185             StatusRedrawingFlag::MONSTER_LITE,
186             StatusRedrawingFlag::DISTANCE,
187         };
188         rfu.set_flags(flags_srf);
189         player_ptr->window_flags |= PW_OVERHEAD | PW_DUNGEON;
190         if ((!player_ptr->effects()->blindness()->is_blind() && !no_lite(player_ptr)) || !is_trap(player_ptr, g_ptr->feat)) {
191             g_ptr->info &= ~(CAVE_UNSAFE);
192         }
193
194         if (floor_ptr->dun_level && dungeons_info[player_ptr->dungeon_idx].flags.has(DungeonFeatureType::FORGET)) {
195             wiz_dark(player_ptr);
196         }
197
198         if (mpe_mode & MPE_HANDLE_STUFF) {
199             handle_stuff(player_ptr);
200         }
201
202         if (PlayerClass(player_ptr).equals(PlayerClassType::NINJA)) {
203             if (g_ptr->info & (CAVE_GLOW)) {
204                 set_superstealth(player_ptr, false);
205             } else if (player_ptr->cur_lite <= 0) {
206                 set_superstealth(player_ptr, true);
207             }
208         }
209
210         using Tc = TerrainCharacteristics;
211         if ((player_ptr->action == ACTION_HAYAGAKE) && (f_ptr->flags.has_not(Tc::PROJECT) || (!player_ptr->levitation && f_ptr->flags.has(Tc::DEEP)))) {
212             msg_print(_("ここでは素早く動けない。", "You cannot run in here."));
213             set_action(player_ptr, ACTION_NONE);
214         }
215
216         if (PlayerRace(player_ptr).equals(PlayerRaceType::MERFOLK)) {
217             if (f_ptr->flags.has(Tc::WATER) ^ of_ptr->flags.has(Tc::WATER)) {
218                 rfu.set_flag(StatusRedrawingFlag::BONUS);
219                 update_creature(player_ptr);
220             }
221         }
222     }
223
224     if (mpe_mode & MPE_ENERGY_USE) {
225         if (music_singing(player_ptr, MUSIC_WALL)) {
226             (void)project(player_ptr, 0, 0, player_ptr->y, player_ptr->x, (60 + player_ptr->lev), AttributeType::DISINTEGRATE, PROJECT_KILL | PROJECT_ITEM);
227             if (!player_bold(player_ptr, ny, nx) || player_ptr->is_dead || player_ptr->leaving) {
228                 return false;
229             }
230         }
231
232         if ((player_ptr->skill_fos >= 50) || (0 == randint0(50 - player_ptr->skill_fos))) {
233             search(player_ptr);
234         }
235
236         if (player_ptr->action == ACTION_SEARCH) {
237             search(player_ptr);
238         }
239     }
240
241     if (!(mpe_mode & MPE_DONT_PICKUP)) {
242         carry(player_ptr, any_bits(mpe_mode, MPE_DO_PICKUP));
243     }
244
245     if (!player_ptr->running) {
246         // 自動拾い/自動破壊により床上のアイテムリストが変化した可能性があるので表示を更新
247         set_bits(player_ptr->window_flags, PW_FLOOR_ITEMS | PW_FOUND_ITEMS);
248         window_stuff(player_ptr);
249     }
250
251     PlayerEnergy energy(player_ptr);
252     if (f_ptr->flags.has(TerrainCharacteristics::STORE)) {
253         disturb(player_ptr, false, true);
254         energy.reset_player_turn();
255         command_new = SPECIAL_KEY_STORE;
256     } else if (f_ptr->flags.has(TerrainCharacteristics::BLDG)) {
257         disturb(player_ptr, false, true);
258         energy.reset_player_turn();
259         command_new = SPECIAL_KEY_BUILDING;
260     } else if (f_ptr->flags.has(TerrainCharacteristics::QUEST_ENTER)) {
261         disturb(player_ptr, false, true);
262         energy.reset_player_turn();
263         command_new = SPECIAL_KEY_QUEST;
264     } else if (f_ptr->flags.has(TerrainCharacteristics::QUEST_EXIT)) {
265         const auto &quest_list = QuestList::get_instance();
266         if (quest_list[floor_ptr->quest_number].type == QuestKindType::FIND_EXIT) {
267             complete_quest(player_ptr, floor_ptr->quest_number);
268         }
269         leave_quest_check(player_ptr);
270         floor_ptr->quest_number = i2enum<QuestId>(g_ptr->special);
271         floor_ptr->dun_level = 0;
272         if (!inside_quest(floor_ptr->quest_number)) {
273             player_ptr->word_recall = 0;
274         }
275         player_ptr->oldpx = 0;
276         player_ptr->oldpy = 0;
277         player_ptr->leaving = true;
278     } else if (f_ptr->flags.has(TerrainCharacteristics::HIT_TRAP) && !(mpe_mode & MPE_STAYING)) {
279         disturb(player_ptr, false, true);
280         if (g_ptr->mimic || f_ptr->flags.has(TerrainCharacteristics::SECRET)) {
281             msg_print(_("トラップだ!", "You found a trap!"));
282             disclose_grid(player_ptr, player_ptr->y, player_ptr->x);
283         }
284
285         hit_trap(player_ptr, any_bits(mpe_mode, MPE_BREAK_TRAP));
286         if (!player_bold(player_ptr, ny, nx) || player_ptr->is_dead || player_ptr->leaving) {
287             return false;
288         }
289     }
290
291     if (!(mpe_mode & MPE_STAYING) && (disturb_trap_detect || alert_trap_detect) && player_ptr->dtrap && !(g_ptr->info & CAVE_IN_DETECT)) {
292         player_ptr->dtrap = false;
293         if (!(g_ptr->info & CAVE_UNSAFE)) {
294             if (alert_trap_detect) {
295                 msg_print(_("* 注意:この先はトラップの感知範囲外です! *", "*Leaving trap detect region!*"));
296             }
297
298             if (disturb_trap_detect) {
299                 disturb(player_ptr, false, true);
300             }
301         }
302     }
303
304     return player_bold(player_ptr, ny, nx) && !player_ptr->is_dead && !player_ptr->leaving;
305 }
306
307 /*!
308  * @brief 該当地形のトラップがプレイヤーにとって無効かどうかを判定して返す
309  * @param player_ptr プレイヤーへの参照ポインタ
310  * @param feat 地形ID
311  * @return トラップが自動的に無効ならばTRUEを返す
312  */
313 bool trap_can_be_ignored(PlayerType *player_ptr, FEAT_IDX feat)
314 {
315     auto *f_ptr = &terrains_info[feat];
316     if (f_ptr->flags.has_not(TerrainCharacteristics::TRAP)) {
317         return true;
318     }
319
320     switch (i2enum<TrapType>(f_ptr->subtype)) {
321     case TrapType::TRAPDOOR:
322     case TrapType::PIT:
323     case TrapType::SPIKED_PIT:
324     case TrapType::POISON_PIT:
325         if (player_ptr->levitation) {
326             return true;
327         }
328         break;
329     case TrapType::TELEPORT:
330         if (player_ptr->anti_tele) {
331             return true;
332         }
333         break;
334     case TrapType::FIRE:
335         if (has_immune_fire(player_ptr)) {
336             return true;
337         }
338         break;
339     case TrapType::ACID:
340         if (has_immune_acid(player_ptr)) {
341             return true;
342         }
343         break;
344     case TrapType::BLIND:
345         if (has_resist_blind(player_ptr)) {
346             return true;
347         }
348         break;
349     case TrapType::CONFUSE:
350         if (has_resist_conf(player_ptr)) {
351             return true;
352         }
353         break;
354     case TrapType::POISON:
355         if (has_resist_pois(player_ptr)) {
356             return true;
357         }
358         break;
359     case TrapType::SLEEP:
360         if (player_ptr->free_act) {
361             return true;
362         }
363         break;
364     default:
365         break;
366     }
367
368     return false;
369 }