OSDN Git Service

[Refactor] #2141 floor_type *floor_ptrの宣言をautoに差し替えた
[hengbandforosx/hengbandosx.git] / src / floor / floor-leaver.cpp
1 #include "floor/floor-leaver.h"
2 #include "cmd-building/cmd-building.h"
3 #include "dungeon/dungeon.h"
4 #include "dungeon/quest.h"
5 #include "floor/cave.h"
6 #include "floor/floor-events.h"
7 #include "floor/floor-mode-changer.h"
8 #include "floor/floor-save-util.h"
9 #include "floor/floor-save.h"
10 #include "floor/geometry.h"
11 #include "floor/line-of-sight.h"
12 #include "game-option/birth-options.h"
13 #include "game-option/play-record-options.h"
14 #include "grid/feature.h"
15 #include "grid/grid.h"
16 #include "inventory/inventory-slot-types.h"
17 #include "io/write-diary.h"
18 #include "mind/mind-mirror-master.h"
19 #include "mind/mind-ninja.h"
20 #include "monster-floor/monster-lite.h"
21 #include "monster-floor/monster-remover.h"
22 #include "monster-race/monster-race.h"
23 #include "monster-race/race-flags1.h"
24 #include "monster-race/race-flags7.h"
25 #include "monster/monster-describer.h"
26 #include "monster/monster-description-types.h"
27 #include "monster/monster-info.h"
28 #include "monster/monster-status.h"
29 #include "pet/pet-util.h"
30 #include "player-status/player-energy.h"
31 #include "player/player-status.h"
32 #include "player/special-defense-types.h"
33 #include "save/floor-writer.h"
34 #include "system/artifact-type-definition.h"
35 #include "system/floor-type-definition.h"
36 #include "system/grid-type-definition.h"
37 #include "system/monster-race-definition.h"
38 #include "system/monster-type-definition.h"
39 #include "system/player-type-definition.h"
40 #include "target/projection-path-calculator.h"
41 #include "util/bit-flags-calculator.h"
42 #include "view/display-messages.h"
43 #include "world/world.h"
44
45 static void check_riding_preservation(PlayerType *player_ptr)
46 {
47     if (!player_ptr->riding)
48         return;
49
50     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
51     if (m_ptr->parent_m_idx) {
52         player_ptr->riding = 0;
53         player_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
54         player_ptr->riding_ryoute = player_ptr->old_riding_ryoute = false;
55     } else {
56         party_mon[0] = *m_ptr;
57         delete_monster_idx(player_ptr, player_ptr->riding);
58     }
59 }
60
61 static bool check_pet_preservation_conditions(PlayerType *player_ptr, monster_type *m_ptr)
62 {
63     if (reinit_wilderness)
64         return false;
65
66     POSITION dis = distance(player_ptr->y, player_ptr->x, m_ptr->fy, m_ptr->fx);
67     if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr) || (m_ptr->parent_m_idx != 0))
68         return true;
69
70     if (m_ptr->nickname && ((player_has_los_bold(player_ptr, m_ptr->fy, m_ptr->fx) && projectable(player_ptr, player_ptr->y, player_ptr->x, m_ptr->fy, m_ptr->fx)) || (los(player_ptr, m_ptr->fy, m_ptr->fx, player_ptr->y, player_ptr->x) && projectable(player_ptr, m_ptr->fy, m_ptr->fx, player_ptr->y, player_ptr->x)))) {
71         if (dis > 3)
72             return true;
73     } else if (dis > 1)
74         return true;
75
76     return false;
77 }
78
79 static void sweep_preserving_pet(PlayerType *player_ptr)
80 {
81     if (player_ptr->wild_mode || player_ptr->current_floor_ptr->inside_arena || player_ptr->phase_out)
82         return;
83
84     for (MONSTER_IDX i = player_ptr->current_floor_ptr->m_max - 1, party_monster_num = 1; (i >= 1) && (party_monster_num < MAX_PARTY_MON); i--) {
85         monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[i];
86         if (!monster_is_valid(m_ptr) || !is_pet(m_ptr) || (i == player_ptr->riding) || check_pet_preservation_conditions(player_ptr, m_ptr))
87             continue;
88
89         party_mon[party_monster_num] = player_ptr->current_floor_ptr->m_list[i];
90         party_monster_num++;
91         delete_monster_idx(player_ptr, i);
92     }
93 }
94
95 static void record_pet_diary(PlayerType *player_ptr)
96 {
97     if (!record_named_pet)
98         return;
99
100     for (MONSTER_IDX i = player_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
101         monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[i];
102         GAME_TEXT m_name[MAX_NLEN];
103         if (!monster_is_valid(m_ptr) || !is_pet(m_ptr) || !m_ptr->nickname || (player_ptr->riding == i))
104             continue;
105
106         monster_desc(player_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
107         exe_write_diary(player_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
108     }
109 }
110
111 /*!
112  * @brief フロア移動時のペット保存処理 / Preserve_pets
113  * @param player_ptr プレイヤーへの参照ポインタ
114  */
115 static void preserve_pet(PlayerType *player_ptr)
116 {
117     for (MONSTER_IDX party_monster_num = 0; party_monster_num < MAX_PARTY_MON; party_monster_num++)
118         party_mon[party_monster_num].r_idx = 0;
119
120     check_riding_preservation(player_ptr);
121     sweep_preserving_pet(player_ptr);
122     record_pet_diary(player_ptr);
123     for (MONSTER_IDX i = player_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
124         monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[i];
125         if ((m_ptr->parent_m_idx == 0) || (player_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx != 0))
126             continue;
127
128         if (is_seen(player_ptr, m_ptr)) {
129             GAME_TEXT m_name[MAX_NLEN];
130             monster_desc(player_ptr, m_name, m_ptr, 0);
131             msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
132         }
133
134         delete_monster_idx(player_ptr, i);
135     }
136 }
137
138 /*!
139  * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
140  * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
141  */
142 static void locate_connected_stairs(PlayerType *player_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
143 {
144     POSITION sx = 0;
145     POSITION sy = 0;
146     POSITION x_table[20];
147     POSITION y_table[20];
148     int num = 0;
149     for (POSITION y = 0; y < floor_ptr->height; y++) {
150         for (POSITION x = 0; x < floor_ptr->width; x++) {
151             grid_type *g_ptr = &floor_ptr->grid_array[y][x];
152             feature_type *f_ptr = &f_info[g_ptr->feat];
153             bool ok = false;
154             if (floor_mode & CFM_UP) {
155                 if (f_ptr->flags.has_all_of({ FloorFeatureType::LESS, FloorFeatureType::STAIRS }) && f_ptr->flags.has_not(FloorFeatureType::SPECIAL)) {
156                     ok = true;
157                     if (g_ptr->special && g_ptr->special == sf_ptr->upper_floor_id) {
158                         sx = x;
159                         sy = y;
160                     }
161                 }
162             } else if (floor_mode & CFM_DOWN) {
163                 if (f_ptr->flags.has_all_of({ FloorFeatureType::MORE, FloorFeatureType::STAIRS }) && f_ptr->flags.has_not(FloorFeatureType::SPECIAL)) {
164                     ok = true;
165                     if (g_ptr->special && g_ptr->special == sf_ptr->lower_floor_id) {
166                         sx = x;
167                         sy = y;
168                     }
169                 }
170             } else {
171                 if (f_ptr->flags.has(FloorFeatureType::BLDG)) {
172                     ok = true;
173                 }
174             }
175
176             if (ok && (num < 20)) {
177                 x_table[num] = x;
178                 y_table[num] = y;
179                 num++;
180             }
181         }
182     }
183
184     if (sx) {
185         player_ptr->y = sy;
186         player_ptr->x = sx;
187         return;
188     }
189
190     if (num == 0) {
191         prepare_change_floor_mode(player_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
192         if (!feat_uses_special(floor_ptr->grid_array[player_ptr->y][player_ptr->x].feat))
193             floor_ptr->grid_array[player_ptr->y][player_ptr->x].special = 0;
194
195         return;
196     }
197
198     int i = randint0(num);
199     player_ptr->y = y_table[i];
200     player_ptr->x = x_table[i];
201 }
202
203 /*!
204  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
205  */
206 static void get_out_monster(PlayerType *player_ptr)
207 {
208     int tries = 0;
209     POSITION dis = 1;
210     POSITION oy = player_ptr->y;
211     POSITION ox = player_ptr->x;
212     auto *floor_ptr = player_ptr->current_floor_ptr;
213     MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
214     if (m_idx == 0)
215         return;
216
217     while (true) {
218         monster_type *m_ptr;
219         POSITION ny = rand_spread(oy, dis);
220         POSITION nx = rand_spread(ox, dis);
221         tries++;
222         if (tries > 10000)
223             return;
224
225         if (tries > 20 * dis * dis)
226             dis++;
227
228         if (!in_bounds(floor_ptr, ny, nx) || !is_cave_empty_bold(player_ptr, ny, nx) || floor_ptr->grid_array[ny][nx].is_rune_protection() || floor_ptr->grid_array[ny][nx].is_rune_explosion() || pattern_tile(floor_ptr, ny, nx))
229             continue;
230
231         m_ptr = &floor_ptr->m_list[m_idx];
232         floor_ptr->grid_array[oy][ox].m_idx = 0;
233         floor_ptr->grid_array[ny][nx].m_idx = m_idx;
234         m_ptr->fy = ny;
235         m_ptr->fx = nx;
236         return;
237     }
238 }
239
240 /*!
241  * @brief クエスト・フロア内のモンスター・インベントリ情報を保存する
242  * @param player_ptr プレイヤーへの参照ポインタ
243  */
244 static void preserve_info(PlayerType *player_ptr)
245 {
246     MONRACE_IDX quest_r_idx = 0;
247     for (DUNGEON_IDX i = 0; i < max_q_idx; i++) {
248         if ((quest[i].status == QuestStatusType::TAKEN) && ((quest[i].type == QuestKindType::KILL_LEVEL) || (quest[i].type == QuestKindType::RANDOM)) && (quest[i].level == player_ptr->current_floor_ptr->dun_level) && (player_ptr->dungeon_idx == quest[i].dungeon) && !(quest[i].flags & QUEST_FLAG_PRESET)) {
249             quest_r_idx = quest[i].r_idx;
250         }
251     }
252
253     for (DUNGEON_IDX i = 1; i < player_ptr->current_floor_ptr->m_max; i++) {
254         monster_race *r_ptr;
255         monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[i];
256         if (!monster_is_valid(m_ptr) || (quest_r_idx != m_ptr->r_idx))
257             continue;
258
259         r_ptr = real_r_ptr(m_ptr);
260         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
261             continue;
262
263         delete_monster_idx(player_ptr, i);
264     }
265
266     for (DUNGEON_IDX i = 0; i < INVEN_PACK; i++) {
267         ObjectType *o_ptr = &player_ptr->inventory_list[i];
268         if (!o_ptr->is_valid())
269             continue;
270
271         if (o_ptr->is_fixed_artifact())
272             a_info[o_ptr->name1].floor_id = 0;
273     }
274 }
275
276 static void set_grid_by_leaving_floor(PlayerType *player_ptr, grid_type **g_ptr)
277 {
278     if ((player_ptr->change_floor_mode & CFM_SAVE_FLOORS) == 0)
279         return;
280
281     *g_ptr = &player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x];
282     feature_type *f_ptr = &f_info[(*g_ptr)->feat];
283     if ((*g_ptr)->special && f_ptr->flags.has_not(FloorFeatureType::SPECIAL) && get_sf_ptr((*g_ptr)->special))
284         new_floor_id = (*g_ptr)->special;
285
286     if (f_ptr->flags.has_all_of({ FloorFeatureType::STAIRS, FloorFeatureType::SHAFT }))
287         prepare_change_floor_mode(player_ptr, CFM_SHAFT);
288 }
289
290 static void jump_floors(PlayerType *player_ptr)
291 {
292     if (none_bits(player_ptr->change_floor_mode, CFM_DOWN | CFM_UP)) {
293         return;
294     }
295
296     auto move_num = 0;
297     if (any_bits(player_ptr->change_floor_mode, CFM_DOWN)) {
298         move_num = 1;
299     } else if (any_bits(player_ptr->change_floor_mode, CFM_UP)) {
300         move_num = -1;
301     }
302
303     if (any_bits(player_ptr->change_floor_mode, CFM_SHAFT)) {
304         move_num *= 2;
305     }
306
307     if (any_bits(player_ptr->change_floor_mode, CFM_DOWN)) {
308         if (!is_in_dungeon(player_ptr)) {
309             move_num = d_info[player_ptr->dungeon_idx].mindepth;
310         }
311     } else if (any_bits(player_ptr->change_floor_mode, CFM_UP)) {
312         if (player_ptr->current_floor_ptr->dun_level + move_num < d_info[player_ptr->dungeon_idx].mindepth) {
313             move_num = -player_ptr->current_floor_ptr->dun_level;
314         }
315     }
316
317     player_ptr->current_floor_ptr->dun_level += move_num;
318 }
319
320 static void exit_to_wilderness(PlayerType *player_ptr)
321 {
322     if (is_in_dungeon(player_ptr) || (player_ptr->dungeon_idx == 0))
323         return;
324
325     player_ptr->leaving_dungeon = true;
326     if (!vanilla_town && !lite_town) {
327         player_ptr->wilderness_y = d_info[player_ptr->dungeon_idx].dy;
328         player_ptr->wilderness_x = d_info[player_ptr->dungeon_idx].dx;
329     }
330
331     player_ptr->recall_dungeon = player_ptr->dungeon_idx;
332     player_ptr->word_recall = 0;
333     player_ptr->dungeon_idx = 0;
334     player_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
335 }
336
337 static void kill_saved_floors(PlayerType *player_ptr, saved_floor_type *sf_ptr)
338 {
339     if (!(player_ptr->change_floor_mode & CFM_SAVE_FLOORS)) {
340         for (DUNGEON_IDX i = 0; i < MAX_SAVED_FLOORS; i++)
341             kill_saved_floor(player_ptr, &saved_floors[i]);
342
343         latest_visit_mark = 1;
344         return;
345     }
346     if (player_ptr->change_floor_mode & CFM_NO_RETURN)
347         kill_saved_floor(player_ptr, sf_ptr);
348 }
349
350 static void refresh_new_floor_id(PlayerType *player_ptr, grid_type *g_ptr)
351 {
352     if (new_floor_id != 0)
353         return;
354
355     new_floor_id = get_new_floor_id(player_ptr);
356     if ((g_ptr != nullptr) && !feat_uses_special(g_ptr->feat))
357         g_ptr->special = new_floor_id;
358 }
359
360 static void update_upper_lower_or_floor_id(PlayerType *player_ptr, saved_floor_type *sf_ptr)
361 {
362     if ((player_ptr->change_floor_mode & CFM_RAND_CONNECT) == 0)
363         return;
364
365     if (player_ptr->change_floor_mode & CFM_UP)
366         sf_ptr->upper_floor_id = new_floor_id;
367     else if (player_ptr->change_floor_mode & CFM_DOWN)
368         sf_ptr->lower_floor_id = new_floor_id;
369 }
370
371 static void exe_leave_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr)
372 {
373     grid_type *g_ptr = nullptr;
374     set_grid_by_leaving_floor(player_ptr, &g_ptr);
375     jump_floors(player_ptr);
376     exit_to_wilderness(player_ptr);
377     kill_saved_floors(player_ptr, sf_ptr);
378     if (player_ptr->floor_id == 0)
379         return;
380
381     refresh_new_floor_id(player_ptr, g_ptr);
382     update_upper_lower_or_floor_id(player_ptr, sf_ptr);
383     if (((player_ptr->change_floor_mode & CFM_SAVE_FLOORS) == 0) || ((player_ptr->change_floor_mode & CFM_NO_RETURN) != 0))
384         return;
385
386     get_out_monster(player_ptr);
387     sf_ptr->last_visit = w_ptr->game_turn;
388     forget_lite(player_ptr->current_floor_ptr);
389     forget_view(player_ptr->current_floor_ptr);
390     clear_mon_lite(player_ptr->current_floor_ptr);
391     if (save_floor(player_ptr, sf_ptr, 0))
392         return;
393
394     prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
395     kill_saved_floor(player_ptr, get_sf_ptr(player_ptr->floor_id));
396 }
397
398 /*!
399  * @brief 現在のフロアを離れるに伴って行なわれる保存処理
400  * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
401  * @param player_ptr プレイヤーへの参照ポインタ
402  */
403 void leave_floor(PlayerType *player_ptr)
404 {
405     preserve_pet(player_ptr);
406     remove_all_mirrors(player_ptr, false);
407     set_superstealth(player_ptr, false);
408
409     new_floor_id = 0;
410
411     preserve_info(player_ptr);
412     saved_floor_type *sf_ptr = get_sf_ptr(player_ptr->floor_id);
413     if (player_ptr->change_floor_mode & CFM_RAND_CONNECT)
414         locate_connected_stairs(player_ptr, player_ptr->current_floor_ptr, sf_ptr, player_ptr->change_floor_mode);
415
416     exe_leave_floor(player_ptr, sf_ptr);
417 }
418
419 /*!
420  * @brief 任意のダンジョン及び階層に飛ぶ
421  * Go to any level
422  */
423 void jump_floor(PlayerType *player_ptr, DUNGEON_IDX dun_idx, DEPTH depth)
424 {
425     player_ptr->dungeon_idx = dun_idx;
426     player_ptr->current_floor_ptr->dun_level = depth;
427     prepare_change_floor_mode(player_ptr, CFM_RAND_PLACE);
428     if (!is_in_dungeon(player_ptr))
429         player_ptr->dungeon_idx = 0;
430
431     player_ptr->current_floor_ptr->inside_arena = false;
432     player_ptr->wild_mode = false;
433     leave_quest_check(player_ptr);
434     if (record_stair)
435         exe_write_diary(player_ptr, DIARY_WIZ_TELE, 0, nullptr);
436
437     player_ptr->current_floor_ptr->quest_number = QuestId::NONE;
438     PlayerEnergy(player_ptr).reset_player_turn();
439     player_ptr->energy_need = 0;
440     prepare_change_floor_mode(player_ptr, CFM_FIRST_FLOOR);
441     player_ptr->leaving = true;
442 }