OSDN Git Service

Merge remote-tracking branch 'remotes/hengbandosx/english-market-edits' into feature...
[hengband/hengband.git] / src / floor / floor-leaver.c
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"
39
40 static void check_riding_preservation(player_type *master_ptr)
41 {
42     if (!master_ptr->riding)
43         return;
44
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;
50     } else {
51         (void)COPY(&party_mon[0], m_ptr, monster_type);
52         delete_monster_idx(master_ptr, master_ptr->riding);
53     }
54 }
55
56 static bool check_pet_preservation_conditions(player_type *master_ptr, monster_type *m_ptr)
57 {
58     if (reinit_wilderness)
59         return FALSE;
60
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))
63         return TRUE;
64
65     if (m_ptr->nickname
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)))) {
69         if (dis > 3)
70             return TRUE;
71     } else if (dis > 1)
72         return TRUE;
73
74     return FALSE;
75 }
76
77 static void sweep_preserving_pet(player_type *master_ptr)
78 {
79     if (master_ptr->wild_mode || master_ptr->current_floor_ptr->inside_arena || master_ptr->phase_out)
80         return;
81
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))
85             continue;
86
87         (void)COPY(&party_mon[party_monster_num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
88         party_monster_num++;
89         delete_monster_idx(master_ptr, i);
90     }
91 }
92
93 static void record_pet_diary(player_type *master_ptr)
94 {
95     if (!record_named_pet)
96         return;
97
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))
102             continue;
103
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);
106     }
107 }
108
109 /*!
110  * @brief フロア移動時のペット保存処理 / Preserve_pets
111  * @param master_ptr プレーヤーへの参照ポインタ
112  * @return なし
113  */
114 static void preserve_pet(player_type *master_ptr)
115 {
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;
118
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))
125             continue;
126
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);
131         }
132
133         delete_monster_idx(master_ptr, i);
134     }
135 }
136
137 /*!
138  * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
139  * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
140  * @return なし
141  */
142 static void locate_connected_stairs(player_type *creature_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 (has_flag(f_ptr->flags, FF_LESS) && has_flag(f_ptr->flags, FF_STAIRS) && !has_flag(f_ptr->flags, FF_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 (has_flag(f_ptr->flags, FF_MORE) && has_flag(f_ptr->flags, FF_STAIRS) && !has_flag(f_ptr->flags, FF_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 (has_flag(f_ptr->flags, FF_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         creature_ptr->y = sy;
186         creature_ptr->x = sx;
187         return;
188     }
189
190     if (num == 0) {
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;
194
195         return;
196     }
197
198     int i = randint0(num);
199     creature_ptr->y = y_table[i];
200     creature_ptr->x = x_table[i];
201 }
202
203 /*!
204  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
205  * @return なし
206  */
207 static void get_out_monster(player_type *protected_ptr)
208 {
209     int tries = 0;
210     POSITION dis = 1;
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;
215     if (m_idx == 0)
216         return;
217
218     while (TRUE) {
219         monster_type *m_ptr;
220         POSITION ny = rand_spread(oy, dis);
221         POSITION nx = rand_spread(ox, dis);
222         tries++;
223         if (tries > 10000)
224             return;
225
226         if (tries > 20 * dis * dis)
227             dis++;
228
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))
231             continue;
232
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;
236         m_ptr->fy = ny;
237         m_ptr->fx = nx;
238         return;
239     }
240 }
241
242 /*!
243  * @brief クエスト・フロア内のモンスター・インベントリ情報を保存する
244  * @param creature_ptr プレーヤーへの参照ポインタ
245  * @return なし
246  */
247 static void preserve_info(player_type *creature_ptr)
248 {
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;
255         }
256     }
257
258     for (DUNGEON_IDX i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
259         monster_race *r_ptr;
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))
262             continue;
263
264         r_ptr = real_r_ptr(m_ptr);
265         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
266             continue;
267
268         delete_monster_idx(creature_ptr, i);
269     }
270
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))
274             continue;
275
276         if (object_is_fixed_artifact(o_ptr))
277             a_info[o_ptr->name1].floor_id = 0;
278     }
279 }
280
281 static void set_grid_by_leaving_floor(player_type *creature_ptr, grid_type **g_ptr)
282 {
283     if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) == 0)
284         return;
285
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;
290
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);
293 }
294
295 static void jump_floors(player_type *creature_ptr)
296 {
297     if ((creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) == 0)
298         return;
299
300     int move_num = 0;
301     if (creature_ptr->change_floor_mode & CFM_DOWN)
302         move_num = 1;
303     else if (creature_ptr->change_floor_mode & CFM_UP)
304         move_num = -1;
305
306     if (creature_ptr->change_floor_mode & CFM_SHAFT)
307         move_num += SGN(move_num);
308
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;
315     }
316
317     creature_ptr->current_floor_ptr->dun_level += move_num;
318 }
319
320 static void exit_to_wilderness(player_type *creature_ptr)
321 {
322     if ((creature_ptr->current_floor_ptr->dun_level != 0) || (creature_ptr->dungeon_idx == 0))
323         return;
324
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;
329     }
330
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
334 }
335
336 static void kill_saved_floors(player_type *creature_ptr, saved_floor_type *sf_ptr)
337 {
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]);
341
342         latest_visit_mark = 1;
343         return;
344     }
345     
346     if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
347         kill_saved_floor(creature_ptr, sf_ptr);
348 }
349
350 static void refresh_new_floor_id(player_type *creature_ptr, grid_type *g_ptr)
351 {
352     if (new_floor_id != 0)
353         return;
354
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;
358 }
359
360 static void update_upper_lower_or_floor_id(player_type *creature_ptr, saved_floor_type *sf_ptr)
361 {
362     if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) == 0)
363         return;
364
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;
369 }
370
371 static void exe_leave_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
372 {
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)
379         return;
380
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))
384         return;
385
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))
392         return;
393
394     prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
395     kill_saved_floor(creature_ptr, get_sf_ptr(creature_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 creature_ptr プレーヤーへの参照ポインタ
402  * @return なし
403  */
404 void leave_floor(player_type *creature_ptr)
405 {
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);
410
411     new_floor_id = 0;
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);
415
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);
420
421     exe_leave_floor(creature_ptr, sf_ptr);
422 }