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/line-of-sight.h"
11 #include "game-option/birth-options.h"
12 #include "game-option/play-record-options.h"
13 #include "grid/feature.h"
14 #include "inventory/inventory-slot-types.h"
15 #include "io/write-diary.h"
16 #include "mind/mind-mirror-master.h"
17 #include "mind/mind-ninja.h"
18 #include "monster-floor/monster-lite.h"
19 #include "monster-floor/monster-remover.h"
20 #include "monster-race/monster-race.h"
21 #include "monster-race/race-flags1.h"
22 #include "monster-race/race-flags7.h"
23 #include "monster/monster-describer.h"
24 #include "monster/monster-description-types.h"
25 #include "monster/monster-info.h"
26 #include "monster/monster-status.h"
27 #include "object-hook/hook-checker.h"
28 #include "object-hook/hook-enchant.h"
29 #include "pet/pet-util.h"
30 #include "player/special-defense-types.h"
31 #include "save/floor-writer.h"
32 #include "system/artifact-type-definition.h"
33 #include "system/floor-type-definition.h"
34 #include "system/monster-type-definition.h"
35 #include "target/projection-path-calculator.h"
36 #include "util/bit-flags-calculator.h"
37 #include "view/display-messages.h"
38 #include "world/world.h"
40 static void check_riding_preservation(player_type *master_ptr)
42 if (!master_ptr->riding)
45 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
46 if (m_ptr->parent_m_idx) {
47 master_ptr->riding = 0;
48 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
49 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
51 (void)COPY(&party_mon[0], m_ptr, monster_type);
52 delete_monster_idx(master_ptr, master_ptr->riding);
56 static bool check_pet_preservation_conditions(player_type *master_ptr, monster_type *m_ptr)
58 if (reinit_wilderness)
61 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
62 if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr) || (m_ptr->parent_m_idx != 0))
66 && ((player_has_los_bold(master_ptr, m_ptr->fy, m_ptr->fx) && projectable(master_ptr, master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx))
67 || (los(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)
68 && projectable(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)))) {
77 static void sweep_preserving_pet(player_type *master_ptr)
79 if (master_ptr->wild_mode || master_ptr->current_floor_ptr->inside_arena || master_ptr->phase_out)
82 for (MONSTER_IDX i = master_ptr->current_floor_ptr->m_max - 1, party_monster_num = 1; (i >= 1) && (party_monster_num < MAX_PARTY_MON); i--) {
83 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
84 if (!monster_is_valid(m_ptr) || !is_pet(m_ptr) || (i == master_ptr->riding) || check_pet_preservation_conditions(master_ptr, m_ptr))
87 (void)COPY(&party_mon[party_monster_num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
89 delete_monster_idx(master_ptr, i);
93 static void record_pet_diary(player_type *master_ptr)
95 if (!record_named_pet)
98 for (MONSTER_IDX i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
99 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
100 GAME_TEXT m_name[MAX_NLEN];
101 if (!monster_is_valid(m_ptr) || !is_pet(m_ptr) || !m_ptr->nickname || (master_ptr->riding == i))
104 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
105 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
110 * @brief フロア移動時のペット保存処理 / Preserve_pets
111 * @param master_ptr プレーヤーへの参照ポインタ
114 static void preserve_pet(player_type *master_ptr)
116 for (MONSTER_IDX party_monster_num = 0; party_monster_num < MAX_PARTY_MON; party_monster_num++)
117 party_mon[party_monster_num].r_idx = 0;
119 check_riding_preservation(master_ptr);
120 sweep_preserving_pet(master_ptr);
121 record_pet_diary(master_ptr);
122 for (MONSTER_IDX i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
123 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
124 if ((m_ptr->parent_m_idx == 0) || (master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx != 0))
127 if (is_seen(master_ptr, m_ptr)) {
128 GAME_TEXT m_name[MAX_NLEN];
129 monster_desc(master_ptr, m_name, m_ptr, 0);
130 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
133 delete_monster_idx(master_ptr, i);
138 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
139 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
142 static void locate_connected_stairs(player_type *creature_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 (has_flag(f_ptr->flags, FF_LESS) && has_flag(f_ptr->flags, FF_STAIRS) && !has_flag(f_ptr->flags, FF_SPECIAL)) {
157 if (g_ptr->special && g_ptr->special == sf_ptr->upper_floor_id) {
162 } else if (floor_mode & CFM_DOWN) {
163 if (has_flag(f_ptr->flags, FF_MORE) && has_flag(f_ptr->flags, FF_STAIRS) && !has_flag(f_ptr->flags, FF_SPECIAL)) {
165 if (g_ptr->special && g_ptr->special == sf_ptr->lower_floor_id) {
171 if (has_flag(f_ptr->flags, FF_BLDG)) {
176 if (ok && (num < 20)) {
185 creature_ptr->y = sy;
186 creature_ptr->x = sx;
191 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
192 if (!feat_uses_special(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].feat))
193 floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].special = 0;
198 int i = randint0(num);
199 creature_ptr->y = y_table[i];
200 creature_ptr->x = x_table[i];
204 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
207 static void get_out_monster(player_type *protected_ptr)
211 POSITION oy = protected_ptr->y;
212 POSITION ox = protected_ptr->x;
213 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
214 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
220 POSITION ny = rand_spread(oy, dis);
221 POSITION nx = rand_spread(ox, dis);
226 if (tries > 20 * dis * dis)
229 if (!in_bounds(floor_ptr, ny, nx) || !is_cave_empty_bold(protected_ptr, ny, nx) || is_glyph_grid(&floor_ptr->grid_array[ny][nx])
230 || is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx]) || pattern_tile(floor_ptr, ny, nx))
233 m_ptr = &floor_ptr->m_list[m_idx];
234 floor_ptr->grid_array[oy][ox].m_idx = 0;
235 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
243 * @brief クエスト・フロア内のモンスター・インベントリ情報を保存する
244 * @param creature_ptr プレーヤーへの参照ポインタ
247 static void preserve_info(player_type *creature_ptr)
249 MONRACE_IDX quest_r_idx = 0;
250 for (DUNGEON_IDX i = 0; i < max_q_idx; i++) {
251 if ((quest[i].status == QUEST_STATUS_TAKEN) && ((quest[i].type == QUEST_TYPE_KILL_LEVEL) || (quest[i].type == QUEST_TYPE_RANDOM))
252 && (quest[i].level == creature_ptr->current_floor_ptr->dun_level) && (creature_ptr->dungeon_idx == quest[i].dungeon)
253 && !(quest[i].flags & QUEST_FLAG_PRESET)) {
254 quest_r_idx = quest[i].r_idx;
258 for (DUNGEON_IDX i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
260 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
261 if (!monster_is_valid(m_ptr) || (quest_r_idx != m_ptr->r_idx))
264 r_ptr = real_r_ptr(m_ptr);
265 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
268 delete_monster_idx(creature_ptr, i);
271 for (DUNGEON_IDX i = 0; i < INVEN_PACK; i++) {
272 object_type *o_ptr = &creature_ptr->inventory_list[i];
273 if (!object_is_valid(o_ptr))
276 if (object_is_fixed_artifact(o_ptr))
277 a_info[o_ptr->name1].floor_id = 0;
281 static void set_grid_by_leaving_floor(player_type *creature_ptr, grid_type **g_ptr)
283 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) == 0)
286 *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
287 feature_type *f_ptr = &f_info[(*g_ptr)->feat];
288 if ((*g_ptr)->special && !has_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr((*g_ptr)->special))
289 new_floor_id = (*g_ptr)->special;
291 if (has_flag(f_ptr->flags, FF_STAIRS) && has_flag(f_ptr->flags, FF_SHAFT))
292 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
295 static void jump_floors(player_type *creature_ptr)
297 if ((creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) == 0)
301 if (creature_ptr->change_floor_mode & CFM_DOWN)
303 else if (creature_ptr->change_floor_mode & CFM_UP)
306 if (creature_ptr->change_floor_mode & CFM_SHAFT)
307 move_num += SGN(move_num);
309 if (creature_ptr->change_floor_mode & CFM_DOWN) {
310 if (!creature_ptr->current_floor_ptr->dun_level)
311 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
312 } else if (creature_ptr->change_floor_mode & CFM_UP) {
313 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
314 move_num = -creature_ptr->current_floor_ptr->dun_level;
317 creature_ptr->current_floor_ptr->dun_level += move_num;
320 static void exit_to_wilderness(player_type *creature_ptr)
322 if ((creature_ptr->current_floor_ptr->dun_level != 0) || (creature_ptr->dungeon_idx == 0))
325 creature_ptr->leaving_dungeon = TRUE;
326 if (!vanilla_town && !lite_town) {
327 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
328 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
331 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
332 creature_ptr->dungeon_idx = 0;
333 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
336 static void kill_saved_floors(player_type *creature_ptr, saved_floor_type *sf_ptr)
338 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)) {
339 for (DUNGEON_IDX i = 0; i < MAX_SAVED_FLOORS; i++)
340 kill_saved_floor(creature_ptr, &saved_floors[i]);
342 latest_visit_mark = 1;
346 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
347 kill_saved_floor(creature_ptr, sf_ptr);
350 static void refresh_new_floor_id(player_type *creature_ptr, grid_type *g_ptr)
352 if (new_floor_id != 0)
355 new_floor_id = get_new_floor_id(creature_ptr);
356 if ((g_ptr != NULL) && !feat_uses_special(g_ptr->feat))
357 g_ptr->special = new_floor_id;
360 static void update_upper_lower_or_floor_id(player_type *creature_ptr, saved_floor_type *sf_ptr)
362 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) == 0)
365 if (creature_ptr->change_floor_mode & CFM_UP)
366 sf_ptr->upper_floor_id = new_floor_id;
367 else if (creature_ptr->change_floor_mode & CFM_DOWN)
368 sf_ptr->lower_floor_id = new_floor_id;
371 static void exe_leave_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
373 grid_type *g_ptr = NULL;
374 set_grid_by_leaving_floor(creature_ptr, &g_ptr);
375 jump_floors(creature_ptr);
376 exit_to_wilderness(creature_ptr);
377 kill_saved_floors(creature_ptr, sf_ptr);
378 if (creature_ptr->floor_id == 0)
381 refresh_new_floor_id(creature_ptr, g_ptr);
382 update_upper_lower_or_floor_id(creature_ptr, sf_ptr);
383 if (((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) == 0) || ((creature_ptr->change_floor_mode & CFM_NO_RETURN) != 0))
386 get_out_monster(creature_ptr);
387 sf_ptr->last_visit = current_world_ptr->game_turn;
388 forget_lite(creature_ptr->current_floor_ptr);
389 forget_view(creature_ptr->current_floor_ptr);
390 clear_mon_lite(creature_ptr->current_floor_ptr);
391 if (save_floor(creature_ptr, sf_ptr, 0))
394 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
395 kill_saved_floor(creature_ptr, get_sf_ptr(creature_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 creature_ptr プレーヤーへの参照ポインタ
404 void leave_floor(player_type *creature_ptr)
406 preserve_pet(creature_ptr);
407 remove_all_mirrors(creature_ptr, FALSE);
408 if (creature_ptr->special_defense & NINJA_S_STEALTH)
409 set_superstealth(creature_ptr, FALSE);
412 FLOOR_IDX tmp_floor_idx = 0;
413 if (!creature_ptr->floor_id && (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
414 tmp_floor_idx = get_new_floor_id(creature_ptr);
416 preserve_info(creature_ptr);
417 saved_floor_type *sf_ptr = get_sf_ptr(creature_ptr->floor_id);
418 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
419 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
421 exe_leave_floor(creature_ptr, sf_ptr);