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"
45 static void check_riding_preservation(PlayerType *player_ptr)
47 if (!player_ptr->riding)
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;
56 party_mon[0] = *m_ptr;
57 delete_monster_idx(player_ptr, player_ptr->riding);
61 static bool check_pet_preservation_conditions(PlayerType *player_ptr, monster_type *m_ptr)
63 if (reinit_wilderness)
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))
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)))) {
79 static void sweep_preserving_pet(PlayerType *player_ptr)
81 if (player_ptr->wild_mode || player_ptr->current_floor_ptr->inside_arena || player_ptr->phase_out)
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))
89 party_mon[party_monster_num] = player_ptr->current_floor_ptr->m_list[i];
91 delete_monster_idx(player_ptr, i);
95 static void record_pet_diary(PlayerType *player_ptr)
97 if (!record_named_pet)
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))
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);
112 * @brief フロア移動時のペット保存処理 / Preserve_pets
113 * @param player_ptr プレイヤーへの参照ポインタ
115 static void preserve_pet(PlayerType *player_ptr)
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;
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))
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);
134 delete_monster_idx(player_ptr, i);
139 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
140 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
142 static void locate_connected_stairs(PlayerType *player_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
146 POSITION x_table[20];
147 POSITION y_table[20];
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];
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)) {
157 if (g_ptr->special && g_ptr->special == sf_ptr->upper_floor_id) {
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)) {
165 if (g_ptr->special && g_ptr->special == sf_ptr->lower_floor_id) {
171 if (f_ptr->flags.has(FloorFeatureType::BLDG)) {
176 if (ok && (num < 20)) {
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;
198 int i = randint0(num);
199 player_ptr->y = y_table[i];
200 player_ptr->x = x_table[i];
204 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
206 static void get_out_monster(PlayerType *player_ptr)
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;
219 POSITION ny = rand_spread(oy, dis);
220 POSITION nx = rand_spread(ox, dis);
225 if (tries > 20 * dis * dis)
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))
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;
241 * @brief クエスト・フロア内のモンスター・インベントリ情報を保存する
242 * @param player_ptr プレイヤーへの参照ポインタ
244 static void preserve_info(PlayerType *player_ptr)
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;
253 for (DUNGEON_IDX i = 1; i < player_ptr->current_floor_ptr->m_max; i++) {
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))
259 r_ptr = real_r_ptr(m_ptr);
260 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
263 delete_monster_idx(player_ptr, i);
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())
271 if (o_ptr->is_fixed_artifact())
272 a_info[o_ptr->name1].floor_id = 0;
276 static void set_grid_by_leaving_floor(PlayerType *player_ptr, grid_type **g_ptr)
278 if ((player_ptr->change_floor_mode & CFM_SAVE_FLOORS) == 0)
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;
286 if (f_ptr->flags.has_all_of({ FloorFeatureType::STAIRS, FloorFeatureType::SHAFT }))
287 prepare_change_floor_mode(player_ptr, CFM_SHAFT);
290 static void jump_floors(PlayerType *player_ptr)
292 if (none_bits(player_ptr->change_floor_mode, CFM_DOWN | CFM_UP)) {
297 if (any_bits(player_ptr->change_floor_mode, CFM_DOWN)) {
299 } else if (any_bits(player_ptr->change_floor_mode, CFM_UP)) {
303 if (any_bits(player_ptr->change_floor_mode, CFM_SHAFT)) {
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;
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;
317 player_ptr->current_floor_ptr->dun_level += move_num;
320 static void exit_to_wilderness(PlayerType *player_ptr)
322 if (is_in_dungeon(player_ptr) || (player_ptr->dungeon_idx == 0))
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;
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
337 static void kill_saved_floors(PlayerType *player_ptr, saved_floor_type *sf_ptr)
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]);
343 latest_visit_mark = 1;
346 if (player_ptr->change_floor_mode & CFM_NO_RETURN)
347 kill_saved_floor(player_ptr, sf_ptr);
350 static void refresh_new_floor_id(PlayerType *player_ptr, grid_type *g_ptr)
352 if (new_floor_id != 0)
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;
360 static void update_upper_lower_or_floor_id(PlayerType *player_ptr, saved_floor_type *sf_ptr)
362 if ((player_ptr->change_floor_mode & CFM_RAND_CONNECT) == 0)
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;
371 static void exe_leave_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr)
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)
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))
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))
394 prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
395 kill_saved_floor(player_ptr, get_sf_ptr(player_ptr->floor_id));
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 プレイヤーへの参照ポインタ
403 void leave_floor(PlayerType *player_ptr)
405 preserve_pet(player_ptr);
406 remove_all_mirrors(player_ptr, false);
407 set_superstealth(player_ptr, false);
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);
416 exe_leave_floor(player_ptr, sf_ptr);
420 * @brief 任意のダンジョン及び階層に飛ぶ
423 void jump_floor(PlayerType *player_ptr, DUNGEON_IDX dun_idx, DEPTH depth)
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;
431 player_ptr->current_floor_ptr->inside_arena = false;
432 player_ptr->wild_mode = false;
433 leave_quest_check(player_ptr);
435 exe_write_diary(player_ptr, DIARY_WIZ_TELE, 0, nullptr);
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;