OSDN Git Service

Merge pull request #1512 from Hourier/feature/Rename-Doxygen-Player-Pointer
[hengbandforosx/hengbandosx.git] / src / grid / grid.cpp
1 /*!
2  * @brief グリッドの実装 / low level dungeon routines -BEN-
3  * @date 2013/12/30
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  *\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * \n
11  * Support for Adam Bolt's tileset, lighting and transparency effects\n
12  * by Robert Ruehlmann (rr9@angband.org)\n
13  * \n
14  * 2013 Deskull Doxygen向けのコメント整理\n
15  */
16
17 #include "grid/grid.h"
18 #include "core/window-redrawer.h"
19 #include "dungeon/dungeon-flag-types.h"
20 #include "dungeon/dungeon.h"
21 #include "dungeon/quest.h"
22 #include "effect/effect-characteristics.h"
23 #include "effect/effect-processor.h"
24 #include "floor/cave.h"
25 #include "floor/floor-generator.h"
26 #include "floor/geometry.h"
27 #include "game-option/game-play-options.h"
28 #include "game-option/map-screen-options.h"
29 #include "game-option/special-options.h"
30 #include "grid/feature-action-flags.h"
31 #include "grid/feature.h"
32 #include "grid/object-placer.h"
33 #include "grid/trap.h"
34 #include "io/screen-util.h"
35 #include "monster-floor/monster-remover.h"
36 #include "monster-race/monster-race.h"
37 #include "monster-race/race-flags2.h"
38 #include "monster-race/race-flags7.h"
39 #include "monster/monster-info.h"
40 #include "monster/monster-status.h"
41 #include "monster/monster-update.h"
42 #include "object/item-tester-hooker.h"
43 #include "object/object-mark-types.h"
44 #include "player-info/class-info.h"
45 #include "player/player-status-flags.h"
46 #include "player/player-status.h"
47 #include "room/rooms-builder.h"
48 #include "spell/spell-types.h"
49 #include "system/floor-type-definition.h"
50 #include "system/grid-type-definition.h"
51 #include "system/monster-race-definition.h"
52 #include "system/monster-type-definition.h"
53 #include "system/object-type-definition.h"
54 #include "system/player-type-definition.h"
55 #include "term/term-color-types.h"
56 #include "util/bit-flags-calculator.h"
57 #include "util/enum-converter.h"
58 #include "util/point-2d.h"
59 #include "view/display-map.h"
60 #include "view/display-messages.h"
61 #include "window/main-window-util.h"
62 #include "world/world.h"
63 #include <queue>
64
65 #define MONSTER_FLOW_DEPTH                                                                                                                                     \
66     32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
67
68 /*!
69  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
70  * @param player_ptr プレイヤーへの参照ポインタ
71  * @return 配置に成功したらTRUEを返す
72  */
73 bool new_player_spot(player_type *player_ptr)
74 {
75     POSITION y = 0, x = 0;
76     int max_attempts = 10000;
77
78     grid_type *g_ptr;
79     feature_type *f_ptr;
80
81     while (max_attempts--) {
82         /* Pick a legal spot */
83         y = (POSITION)rand_range(1, player_ptr->current_floor_ptr->height - 2);
84         x = (POSITION)rand_range(1, player_ptr->current_floor_ptr->width - 2);
85
86         g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
87
88         /* Must be a "naked" floor grid */
89         if (g_ptr->m_idx)
90             continue;
91         if (is_in_dungeon(player_ptr)) {
92             f_ptr = &f_info[g_ptr->feat];
93
94             if (max_attempts > 5000) /* Rule 1 */
95             {
96                 if (f_ptr->flags.has_not(FF::FLOOR))
97                     continue;
98             } else /* Rule 2 */
99             {
100                 if (f_ptr->flags.has_not(FF::MOVE))
101                     continue;
102                 if (f_ptr->flags.has(FF::HIT_TRAP))
103                     continue;
104             }
105
106             /* Refuse to start on anti-teleport grids in dungeon */
107             if (f_ptr->flags.has_not(FF::TELEPORTABLE))
108                 continue;
109         }
110         if (!player_can_enter(player_ptr, g_ptr->feat, 0))
111             continue;
112         if (!in_bounds(player_ptr->current_floor_ptr, y, x))
113             continue;
114
115         /* Refuse to start on anti-teleport grids */
116         if (g_ptr->is_icky())
117             continue;
118
119         break;
120     }
121
122     if (max_attempts < 1) /* Should be -1, actually if we failed... */
123         return false;
124
125     /* Save the new player grid */
126     player_ptr->y = y;
127     player_ptr->x = x;
128
129     return true;
130 }
131
132 /*!
133  * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
134  * @param player_ptr プレイヤーへの参照ポインタ
135  * @param g_ptr マス構造体の参照ポインタ
136  * @return 隠されたドアがあるならTRUEを返す。
137  */
138 bool is_hidden_door(player_type *player_ptr, grid_type *g_ptr)
139 {
140     if ((g_ptr->mimic || g_ptr->cave_has_flag(FF::SECRET)) && is_closed_door(player_ptr, g_ptr->feat))
141         return true;
142     else
143         return false;
144 }
145
146 /*!
147  * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
148  * @param y y座標
149  * @param x x座標
150  * @return 指定された座標に照明がかかっているならTRUEを返す。。
151  */
152 bool check_local_illumination(player_type *player_ptr, POSITION y, POSITION x)
153 {
154     /* Hack -- move towards player */
155     POSITION yy = (y < player_ptr->y) ? (y + 1) : (y > player_ptr->y) ? (y - 1) : y;
156     POSITION xx = (x < player_ptr->x) ? (x + 1) : (x > player_ptr->x) ? (x - 1) : x;
157
158     /* Check for "local" illumination */
159
160     /* Check for "complex" illumination */
161     auto *floor_ptr = player_ptr->current_floor_ptr;
162     if ((feat_supports_los(floor_ptr->grid_array[yy][xx].get_feat_mimic()) && (floor_ptr->grid_array[yy][xx].info & CAVE_GLOW))
163         || (feat_supports_los(floor_ptr->grid_array[y][xx].get_feat_mimic()) && (floor_ptr->grid_array[y][xx].info & CAVE_GLOW))
164         || (feat_supports_los(floor_ptr->grid_array[yy][x].get_feat_mimic()) && (floor_ptr->grid_array[yy][x].info & CAVE_GLOW))) {
165         return true;
166     } else
167         return false;
168 }
169
170 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
171 #define update_local_illumination_aux(C, Y, X)                                                                                                                 \
172     {                                                                                                                                                          \
173         if (player_has_los_bold((C), (Y), (X))) {                                                                                                              \
174             /* Update the monster */                                                                                                                           \
175             if ((C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx)                                                                                            \
176                 update_monster((C), (C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx, false);                                                                \
177                                                                                                                                                                \
178             /* Notice and redraw */                                                                                                                            \
179             note_spot((C), (Y), (X));                                                                                                                          \
180             lite_spot((C), (Y), (X));                                                                                                                          \
181         }                                                                                                                                                      \
182     }
183
184 /*!
185  * @brief 指定された座標の照明状態を更新する / Update "local" illumination
186  * @param player_ptr プレイヤーへの参照ポインタ
187  * @param y 視界先y座標
188  * @param x 視界先x座標
189  */
190 void update_local_illumination(player_type *player_ptr, POSITION y, POSITION x)
191 {
192     int i;
193     POSITION yy, xx;
194
195     if (!in_bounds(player_ptr->current_floor_ptr, y, x))
196         return;
197
198     if ((y != player_ptr->y) && (x != player_ptr->x)) {
199         yy = (y < player_ptr->y) ? (y - 1) : (y + 1);
200         xx = (x < player_ptr->x) ? (x - 1) : (x + 1);
201         update_local_illumination_aux(player_ptr, yy, xx);
202         update_local_illumination_aux(player_ptr, y, xx);
203         update_local_illumination_aux(player_ptr, yy, x);
204     } else if (x != player_ptr->x) /* y == player_ptr->y */
205     {
206         xx = (x < player_ptr->x) ? (x - 1) : (x + 1);
207         for (i = -1; i <= 1; i++) {
208             yy = y + i;
209             update_local_illumination_aux(player_ptr, yy, xx);
210         }
211         yy = y - 1;
212         update_local_illumination_aux(player_ptr, yy, x);
213         yy = y + 1;
214         update_local_illumination_aux(player_ptr, yy, x);
215     } else if (y != player_ptr->y) /* x == player_ptr->x */
216     {
217         yy = (y < player_ptr->y) ? (y - 1) : (y + 1);
218         for (i = -1; i <= 1; i++) {
219             xx = x + i;
220             update_local_illumination_aux(player_ptr, yy, xx);
221         }
222         xx = x - 1;
223         update_local_illumination_aux(player_ptr, y, xx);
224         xx = x + 1;
225         update_local_illumination_aux(player_ptr, y, xx);
226     } else /* Player's grid */
227     {
228         for (i = 0; i < 8; i++) {
229             yy = y + ddy_cdd[i];
230             xx = x + ddx_cdd[i];
231             update_local_illumination_aux(player_ptr, yy, xx);
232         }
233     }
234 }
235
236 /*!
237  * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
238  * @return 視覚に収められていないならTRUEを返す
239  * @details player_can_see_bold()関数の返り値の否定を返している。
240  */
241 bool no_lite(player_type *player_ptr)
242 {
243     return (!player_can_see_bold(player_ptr, player_ptr->y, player_ptr->x));
244 }
245
246 /*
247  * Place an attr/char pair at the given map coordinate, if legal.
248  */
249 void print_rel(player_type *player_ptr, SYMBOL_CODE c, TERM_COLOR a, POSITION y, POSITION x)
250 {
251     /* Only do "legal" locations */
252     if (panel_contains(y, x)) {
253         /* Hack -- fake monochrome */
254         if (!use_graphics) {
255             if (current_world_ptr->timewalk_m_idx)
256                 a = TERM_DARK;
257             else if (is_invuln(player_ptr) || player_ptr->timewalk)
258                 a = TERM_WHITE;
259             else if (player_ptr->wraith_form)
260                 a = TERM_L_DARK;
261         }
262
263         /* Draw the char using the attr */
264         term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
265     }
266 }
267
268 /*!
269  * @todo ここにplayer_type を追加した時のコンパイルエラーに対処できなかったので保留
270  * Memorize interesting viewable object/features in the given grid
271  *
272  * This function should only be called on "legal" grids.
273  *
274  * This function will memorize the object and/or feature in the given
275  * grid, if they are (1) viewable and (2) interesting.  Note that all
276  * objects are interesting, all terrain features except floors (and
277  * invisible traps) are interesting, and floors (and invisible traps)
278  * are interesting sometimes (depending on various options involving
279  * the illumination of floor grids).
280  *
281  * The automatic memorization of all objects and non-floor terrain
282  * features as soon as they are displayed allows incredible amounts
283  * of optimization in various places, especially "map_info()".
284  *
285  * Note that the memorization of objects is completely separate from
286  * the memorization of terrain features, preventing annoying floor
287  * memorization when a detected object is picked up from a dark floor,
288  * and object memorization when an object is dropped into a floor grid
289  * which is memorized but out-of-sight.
290  *
291  * This function should be called every time the "memorization" of
292  * a grid (or the object in a grid) is called into question, such
293  * as when an object is created in a grid, when a terrain feature
294  * "changes" from "floor" to "non-floor", when any grid becomes
295  * "illuminated" or "viewable", and when a "floor" grid becomes
296  * "torch-lit".
297  *
298  * Note the relatively efficient use of this function by the various
299  * "update_view()" and "update_lite()" calls, to allow objects and
300  * terrain features to be memorized (and drawn) whenever they become
301  * viewable or illuminated in any way, but not when they "maintain"
302  * or "lose" their previous viewability or illumination.
303  *
304  * Note the butchered "internal" version of "player_can_see_bold()",
305  * optimized primarily for the most common cases, that is, for the
306  * non-marked floor grids.
307  */
308 void note_spot(player_type *player_ptr, POSITION y, POSITION x)
309 {
310     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
311
312     /* Blind players see nothing */
313     if (player_ptr->blind)
314         return;
315
316     /* Analyze non-torch-lit grids */
317     if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT))) {
318         /* Require line of sight to the grid */
319         if (!(g_ptr->info & (CAVE_VIEW)))
320             return;
321
322         /* Require "perma-lite" of the grid */
323         if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) {
324             /* Not Ninja */
325             if (!player_ptr->see_nocto)
326                 return;
327         }
328     }
329
330     /* Hack -- memorize objects */
331     for (const auto this_o_idx : g_ptr->o_idx_list) {
332         object_type *o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
333
334         /* Memorize objects */
335         o_ptr->marked |= OM_FOUND;
336     }
337
338     /* Hack -- memorize grids */
339     if (!g_ptr->is_mark()) {
340         /* Feature code (applying "mimic" field) */
341         feature_type *f_ptr = &f_info[g_ptr->get_feat_mimic()];
342
343         /* Memorize some "boring" grids */
344         if (f_ptr->flags.has_not(FF::REMEMBER)) {
345             /* Option -- memorize all torch-lit floors */
346             if (view_torch_grids && ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || player_ptr->see_nocto)) {
347                 g_ptr->info |= (CAVE_MARK);
348             }
349
350             /* Option -- memorize all perma-lit floors */
351             else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW)) {
352                 g_ptr->info |= (CAVE_MARK);
353             }
354         }
355
356         /* Memorize normal grids */
357         else if (f_ptr->flags.has(FF::LOS)) {
358             g_ptr->info |= (CAVE_MARK);
359         }
360
361         /* Memorize torch-lit walls */
362         else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) {
363             g_ptr->info |= (CAVE_MARK);
364         }
365
366         /* Memorize walls seen by noctovision of Ninja */
367         else if (player_ptr->see_nocto) {
368             g_ptr->info |= (CAVE_MARK);
369         }
370
371         /* Memorize certain non-torch-lit wall grids */
372         else if (check_local_illumination(player_ptr, y, x)) {
373             g_ptr->info |= (CAVE_MARK);
374         }
375     }
376
377     /* Memorize terrain of the grid */
378     g_ptr->info |= (CAVE_KNOWN);
379 }
380
381 /*
382  * Redraw (on the screen) a given MAP location
383  *
384  * This function should only be called on "legal" grids
385  */
386 void lite_spot(player_type *player_ptr, POSITION y, POSITION x)
387 {
388     /* Redraw if on screen */
389     if (panel_contains(y, x) && in_bounds2(player_ptr->current_floor_ptr, y, x)) {
390         TERM_COLOR a;
391         SYMBOL_CODE c;
392         TERM_COLOR ta;
393         SYMBOL_CODE tc;
394
395         map_info(player_ptr, y, x, &a, &c, &ta, &tc);
396
397         /* Hack -- fake monochrome */
398         if (!use_graphics) {
399             if (current_world_ptr->timewalk_m_idx)
400                 a = TERM_DARK;
401             else if (is_invuln(player_ptr) || player_ptr->timewalk)
402                 a = TERM_WHITE;
403             else if (player_ptr->wraith_form)
404                 a = TERM_L_DARK;
405         }
406
407         /* Hack -- Queue it */
408         term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
409
410         /* Update sub-windows */
411         player_ptr->window_flags |= (PW_OVERHEAD | PW_DUNGEON);
412     }
413 }
414
415 /*
416  * Some comments on the grid flags.  -BEN-
417  *
418  *
419  * One of the major bottlenecks in previous versions of Angband was in
420  * the calculation of "line of sight" from the player to various grids,
421  * such as monsters.  This was such a nasty bottleneck that a lot of
422  * silly things were done to reduce the dependancy on "line of sight",
423  * for example, you could not "see" any grids in a lit room until you
424  * actually entered the room, and there were all kinds of bizarre grid
425  * flags to enable this behavior.  This is also why the "call light"
426  * spells always lit an entire room.
427  *
428  * The code below provides functions to calculate the "field of view"
429  * for the player, which, once calculated, provides extremely fast
430  * calculation of "line of sight from the player", and to calculate
431  * the "field of torch lite", which, again, once calculated, provides
432  * extremely fast calculation of "which grids are lit by the player's
433  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
434  * "GRID_LITE", as appropriate, these functions maintain an array for
435  * each of these two flags, each array containing the locations of all
436  * of the grids marked with the appropriate flag, which can be used to
437  * very quickly scan through all of the grids in a given set.
438  *
439  * To allow more "semantically valid" field of view semantics, whenever
440  * the field of view (or the set of torch lit grids) changes, all of the
441  * grids in the field of view (or the set of torch lit grids) are "drawn"
442  * so that changes in the world will become apparent as soon as possible.
443  * This has been optimized so that only grids which actually "change" are
444  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
445  * of the grids which are entering or leaving the relevent set of grids.
446  *
447  * These new methods are so efficient that the old nasty code was removed.
448  *
449  * Note that there is no reason to "update" the "viewable space" unless
450  * the player "moves", or walls/doors are created/destroyed, and there
451  * is no reason to "update" the "torch lit grids" unless the field of
452  * view changes, or the "light radius" changes.  This means that when
453  * the player is resting, or digging, or doing anything that does not
454  * involve movement or changing the state of the dungeon, there is no
455  * need to update the "view" or the "lite" regions, which is nice.
456  *
457  * Note that the calls to the nasty "los()" function have been reduced
458  * to a bare minimum by the use of the new "field of view" calculations.
459  *
460  * I wouldn't be surprised if slight modifications to the "update_view()"
461  * function would allow us to determine "reverse line-of-sight" as well
462  * as "normal line-of-sight", which would allow monsters to use a more
463  * "correct" calculation to determine if they can "see" the player.  For
464  * now, monsters simply "cheat" somewhat and assume that if the player
465  * has "line of sight" to the monster, then the monster can "pretend"
466  * that it has "line of sight" to the player.
467  *
468  *
469  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
470  * grid and maintains an array of all "CAVE_LITE" grids.
471  *
472  * This set of grids is the complete set of all grids which are lit by
473  * the players light source, which allows the "player_can_see_bold()"
474  * function to work very quickly.
475  *
476  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
477  * fact, the player (unless blind) can always "see" all grids which are
478  * marked as "CAVE_LITE", unless they are "off screen".
479  *
480  *
481  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
482  * grid and maintains an array of all "CAVE_VIEW" grids.
483  *
484  * This set of grids is the complete set of all grids within line of sight
485  * of the player, allowing the "player_has_los_bold()" macro to work very
486  * quickly.
487  *
488  *
489  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
490  * temporary internal flag to mark those grids which are not only in view,
491  * but which are also "easily" in line of sight of the player.  This flag
492  * is always cleared when we are done.
493  *
494  *
495  * The current "update_lite()" and "update_view()" algorithms use the
496  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
497  * to keep track of which grids were previously marked as "CAVE_LITE" or
498  * "CAVE_VIEW", which allows us to optimize the "screen updates".
499  *
500  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
501  * for various other purposes, such as spreading lite or darkness during
502  * "lite_room()" / "unlite_room()", and for calculating monster flow.
503  *
504  *
505  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
506  * in some way permanently lit.  However, for the player to "see" anything
507  * in the grid, as determined by "player_can_see()", the player must not be
508  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
509  * grids, even if marked as "perma lit", are only illuminated if they touch
510  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
511  *
512  *
513  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
514  * that even if the player cannot "see" the grid, he "knows" the terrain in
515  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
516  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
517  * or when one of the "memorize floor grids" options induces memorization.
518  *
519  * Objects are "memorized" in a different way, using a special "marked" flag
520  * on the object itself, which is set when an object is observed or detected.
521  *
522  *
523  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
524  * and should be illuminated by "lite room" and "darkness" spells.
525  *
526  *
527  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
528  * and should be unavailable for "teleportation" destinations.
529  *
530  *
531  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
532  * which is observed, and the "view_torch_grids" allows the player to memorize
533  * every torch-lit grid.  The player will always memorize important walls,
534  * doors, stairs, and other terrain features, as well as any "detected" grids.
535  *
536  * Note that the new "update_view()" method allows, among other things, a room
537  * to be "partially" seen as the player approaches it, with a growing cone of
538  * floor appearing as the player gets closer to the door.  Also, by not turning
539  * on the "memorize perma-lit grids" option, the player will only "see" those
540  * floor grids which are actually in line of sight.
541  *
542  * And my favorite "plus" is that you can now use a special option to draw the
543  * "floors" in the "viewable region" brightly (actually, to draw the *other*
544  * grids dimly), providing a "pretty" effect as the player runs around, and
545  * to efficiently display the "torch lite" in a special color.
546  *
547  *
548  * Some comments on the "update_view()" algorithm...
549  *
550  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
551  * and only has to call "los()" on the borderline cases.  The major axes/diags
552  * even terminate early when they hit walls.  I need to find a quick way
553  * to "terminate" the other scans.
554  *
555  * Note that in the worst case (a big empty area with say 5% scattered walls),
556  * each of the 1500 or so nearby grids is checked once, most of them getting
557  * an "instant" rating, and only a small portion requiring a call to "los()".
558  *
559  * The only time that the algorithm appears to be "noticeably" too slow is
560  * when running, and this is usually only important in town, since the town
561  * provides about the worst scenario possible, with large open regions and
562  * a few scattered obstructions.  There is a special "efficiency" option to
563  * allow the player to reduce his field of view in town, if needed.
564  *
565  * In the "best" case (say, a normal stretch of corridor), the algorithm
566  * makes one check for each viewable grid, and makes no calls to "los()".
567  * So running in corridors is very fast, and if a lot of monsters are
568  * nearby, it is much faster than the old methods.
569  *
570  * Note that resting, most normal commands, and several forms of running,
571  * plus all commands executed near large groups of monsters, are strictly
572  * more efficient with "update_view()" that with the old "compute los() on
573  * demand" method, primarily because once the "field of view" has been
574  * calculated, it does not have to be recalculated until the player moves
575  * (or a wall or door is created or destroyed).
576  *
577  * Note that we no longer have to do as many "los()" checks, since once the
578  * "view" region has been built, very few things cause it to be "changed"
579  * (player movement, and the opening/closing of doors, changes in wall status).
580  * Note that door/wall changes are only relevant when the door/wall itself is
581  * in the "view" region.
582  *
583  * The algorithm seems to only call "los()" from zero to ten times, usually
584  * only when coming down a corridor into a room, or standing in a room, just
585  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
586  * we will be reducing the calls to "los()".
587  *
588  * I am thinking in terms of an algorithm that "walks" from the central point
589  * out to the maximal "distance", at each point, determining the "view" code
590  * (above).  For each grid not on a major axis or diagonal, the "view" code
591  * depends on the "cave_los_bold()" and "view" of exactly two other grids
592  * (the one along the nearest diagonal, and the one next to that one, see
593  * "update_view_aux()"...).
594  *
595  * We "memorize" the viewable space array, so that at the cost of under 3000
596  * bytes, we reduce the time taken by "forget_view()" to one assignment for
597  * each grid actually in the "viewable space".  And for another 3000 bytes,
598  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
599  * are also used by other routines, thus reducing the cost to almost nothing.
600  *
601  * A similar thing is done for "forget_lite()" in which case the savings are
602  * much less, but save us from doing bizarre maintenance checking.
603  *
604  * In the worst "normal" case (in the middle of the town), the reachable space
605  * actually reaches to more than half of the largest possible "circle" of view,
606  * or about 800 grids, and in the worse case (in the middle of a dungeon level
607  * where all the walls have been removed), the reachable space actually reaches
608  * the theoretical maximum size of just under 1500 grids.
609  *
610  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
611  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
612  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
613  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
614  * entire possible space (including initialization) in one step per grid.  If
615  * we do the "clearing" as a separate step (and use an array of "view" grids),
616  * then the clearing will take as many steps as grids that were viewed, and the
617  * algorithm will be able to "stop" scanning at various points.
618  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
619  */
620
621 /*
622  * Hack - speed up the update_flow algorithm by only doing
623  * it everytime the player moves out of LOS of the last
624  * "way-point".
625  */
626 static POSITION flow_x = 0;
627 static POSITION flow_y = 0;
628
629 /*
630  * Hack -- fill in the "cost" field of every grid that the player
631  * can "reach" with the number of steps needed to reach that grid.
632  * This also yields the "distance" of the player from every grid.
633  *
634  * In addition, mark the "when" of the grids that can reach
635  * the player with the incremented value of "flow_n".
636  *
637  * Hack -- use the "seen" array as a "circular queue".
638  *
639  * We do not need a priority queue because the cost from grid
640  * to grid is always "one" and we process them in order.
641  */
642 void update_flow(player_type *player_ptr)
643 {
644     POSITION x, y;
645     DIRECTION d;
646     floor_type *f_ptr = player_ptr->current_floor_ptr;
647
648     /* The last way-point is on the map */
649     if (player_ptr->running && in_bounds(f_ptr, flow_y, flow_x)) {
650         /* The way point is in sight - do not update.  (Speedup) */
651         if (f_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW)
652             return;
653     }
654
655     /* Erase all of the current flow information */
656     for (y = 0; y < f_ptr->height; y++) {
657         for (x = 0; x < f_ptr->width; x++) {
658             memset(&f_ptr->grid_array[y][x].costs, 0, sizeof(f_ptr->grid_array[y][x].costs));
659             memset(&f_ptr->grid_array[y][x].dists, 0, sizeof(f_ptr->grid_array[y][x].dists));
660         }
661     }
662
663     /* Save player position */
664     flow_y = player_ptr->y;
665     flow_x = player_ptr->x;
666
667     for (int i = 0; i < FLOW_MAX; i++) {
668         // 幅優先探索用のキュー。
669         std::queue<Pos2D> que;
670         que.emplace(player_ptr->y, player_ptr->x);
671
672         /* Now process the queue */
673         while (!que.empty()) {
674             /* Extract the next entry */
675             const auto [ty, tx] = que.front();
676             que.pop();
677
678             /* Add the "children" */
679             for (d = 0; d < 8; d++) {
680                 byte m = player_ptr->current_floor_ptr->grid_array[ty][tx].costs[i] + 1;
681                 byte n = player_ptr->current_floor_ptr->grid_array[ty][tx].dists[i] + 1;
682
683                 /* Child location */
684                 y = ty + ddy_ddd[d];
685                 x = tx + ddx_ddd[d];
686
687                 /* Ignore player's grid */
688                 if (player_bold(player_ptr, y, x))
689                     continue;
690
691                 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
692
693                 if (is_closed_door(player_ptr, g_ptr->feat))
694                     m += 3;
695
696                 /* Ignore "pre-stamped" entries */
697                 if (g_ptr->dists[i] != 0 && g_ptr->dists[i] <= n && g_ptr->costs[i] <= m)
698                     continue;
699
700                 /* Ignore "walls", "holes" and "rubble" */
701                 bool can_move = false;
702                 switch (i) {
703                 case FLOW_CAN_FLY:
704                     can_move = g_ptr->cave_has_flag(FF::MOVE) || g_ptr->cave_has_flag(FF::CAN_FLY);
705                     break;
706                 default:
707                     can_move = g_ptr->cave_has_flag(FF::MOVE);
708                     break;
709                 }
710
711                 if (!can_move && !is_closed_door(player_ptr, g_ptr->feat))
712                     continue;
713
714                 /* Save the flow cost */
715                 if (g_ptr->costs[i] == 0 || g_ptr->costs[i] > m)
716                     g_ptr->costs[i] = m;
717                 if (g_ptr->dists[i] == 0 || g_ptr->dists[i] > n)
718                     g_ptr->dists[i] = n;
719
720                 /* Hack -- limit flow depth */
721                 if (n == MONSTER_FLOW_DEPTH)
722                     continue;
723
724                 /* Enqueue that entry */
725                 que.emplace(y, x);
726             }
727         }
728     }
729 }
730
731 /*
732  * Take a feature, determine what that feature becomes
733  * through applying the given action.
734  */
735 FEAT_IDX feat_state(floor_type *floor_ptr, FEAT_IDX feat, FF action)
736 {
737     feature_type *f_ptr = &f_info[feat];
738     int i;
739
740     /* Get the new feature */
741     for (i = 0; i < MAX_FEAT_STATES; i++) {
742         if (f_ptr->state[i].action == action)
743             return conv_dungeon_feat(floor_ptr, f_ptr->state[i].result);
744     }
745
746     if (f_ptr->flags.has(FF::PERMANENT))
747         return feat;
748
749     return (feature_action_flags[enum2i(action)] & FAF_DESTROY) ? conv_dungeon_feat(floor_ptr, f_ptr->destroyed) : feat;
750 }
751
752 /*
753  * Takes a location and action and changes the feature at that
754  * location through applying the given action.
755  */
756 void cave_alter_feat(player_type *player_ptr, POSITION y, POSITION x, FF action)
757 {
758     /* Set old feature */
759     floor_type *floor_ptr = player_ptr->current_floor_ptr;
760     FEAT_IDX oldfeat = floor_ptr->grid_array[y][x].feat;
761
762     /* Get the new feat */
763     FEAT_IDX newfeat = feat_state(player_ptr->current_floor_ptr, oldfeat, action);
764
765     /* No change */
766     if (newfeat == oldfeat)
767         return;
768
769     /* Set the new feature */
770     cave_set_feat(player_ptr, y, x, newfeat);
771
772     if (!(feature_action_flags[enum2i(action)] & FAF_NO_DROP)) {
773         feature_type *old_f_ptr = &f_info[oldfeat];
774         feature_type *f_ptr = &f_info[newfeat];
775         bool found = false;
776
777         /* Handle gold */
778         if (old_f_ptr->flags.has(FF::HAS_GOLD) && f_ptr->flags.has_not(FF::HAS_GOLD)) {
779             /* Place some gold */
780             place_gold(player_ptr, y, x);
781             found = true;
782         }
783
784         /* Handle item */
785         if (old_f_ptr->flags.has(FF::HAS_ITEM) && f_ptr->flags.has_not(FF::HAS_ITEM) && (randint0(100) < (15 - floor_ptr->dun_level / 2))) {
786             /* Place object */
787             place_object(player_ptr, y, x, 0L);
788             found = true;
789         }
790
791         if (found && current_world_ptr->character_dungeon && player_can_see_bold(player_ptr, y, x)) {
792             msg_print(_("何かを発見した!", "You have found something!"));
793         }
794     }
795
796     if (feature_action_flags[enum2i(action)] & FAF_CRASH_GLASS) {
797         feature_type *old_f_ptr = &f_info[oldfeat];
798
799         if (old_f_ptr->flags.has(FF::GLASS) && current_world_ptr->character_dungeon) {
800             project(player_ptr, PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(floor_ptr->dun_level, 100) / 4, GF_SHARDS,
801                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI));
802         }
803     }
804 }
805
806 /* Remove a mirror */
807 void remove_mirror(player_type *player_ptr, POSITION y, POSITION x)
808 {
809     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
810
811     /* Remove the mirror */
812     g_ptr->info &= ~(CAVE_OBJECT);
813     g_ptr->mimic = 0;
814
815     if (d_info[player_ptr->dungeon_idx].flags.has(DF::DARKNESS)) {
816         g_ptr->info &= ~(CAVE_GLOW);
817         if (!view_torch_grids)
818             g_ptr->info &= ~(CAVE_MARK);
819         if (g_ptr->m_idx)
820             update_monster(player_ptr, g_ptr->m_idx, false);
821
822         update_local_illumination(player_ptr, y, x);
823     }
824
825     note_spot(player_ptr, y, x);
826
827     lite_spot(player_ptr, y, x);
828 }
829
830 /*!
831  * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
832  * @param player_ptr プレイヤーへの参照ポインタ
833  * @param m_idx モンスターID
834  * @param y 移動先Y座標
835  * @param x 移動先X座標
836  * @param mode オプション
837  * @return テレポート先として妥当ならばtrue
838  */
839 bool cave_monster_teleportable_bold(player_type *player_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, teleport_flags mode)
840 {
841     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
842     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
843     feature_type *f_ptr = &f_info[g_ptr->feat];
844
845     /* Require "teleportable" space */
846     if (f_ptr->flags.has_not(FF::TELEPORTABLE))
847         return false;
848
849     if (g_ptr->m_idx && (g_ptr->m_idx != m_idx))
850         return false;
851     if (player_bold(player_ptr, y, x))
852         return false;
853
854     /* Hack -- no teleport onto rune of protection */
855     if (g_ptr->is_rune_protection())
856         return false;
857     if (g_ptr->is_rune_explosion())
858         return false;
859
860     if (!(mode & TELEPORT_PASSIVE)) {
861         if (!monster_can_cross_terrain(player_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], 0))
862             return false;
863     }
864
865     return true;
866 }
867
868 /*!
869  * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
870  * @param player_ptr プレイヤーへの参照ポインタ
871  * @param y 移動先Y座標
872  * @param x 移動先X座標
873  * @param mode オプション
874  * @return テレポート先として妥当ならばtrue
875  */
876 bool cave_player_teleportable_bold(player_type *player_ptr, POSITION y, POSITION x, teleport_flags mode)
877 {
878     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
879     feature_type *f_ptr = &f_info[g_ptr->feat];
880
881     /* Require "teleportable" space */
882     if (f_ptr->flags.has_not(FF::TELEPORTABLE))
883         return false;
884
885     /* No magical teleporting into vaults and such */
886     if (!(mode & TELEPORT_NONMAGICAL) && g_ptr->is_icky())
887         return false;
888
889     if (g_ptr->m_idx && (g_ptr->m_idx != player_ptr->riding))
890         return false;
891
892     /* don't teleport on a trap. */
893     if (f_ptr->flags.has(FF::HIT_TRAP))
894         return false;
895
896     if (!(mode & TELEPORT_PASSIVE)) {
897         if (!player_can_enter(player_ptr, g_ptr->feat, 0))
898             return false;
899
900         if (f_ptr->flags.has_all_of({ FF::WATER, FF::DEEP })) {
901             if (!player_ptr->levitation && !player_ptr->can_swim)
902                 return false;
903         }
904
905         if (f_ptr->flags.has(FF::LAVA) && !has_immune_fire(player_ptr) && !is_invuln(player_ptr)) {
906             /* Always forbid deep lava */
907             if (f_ptr->flags.has(FF::DEEP))
908                 return false;
909
910             /* Forbid shallow lava when the player don't have levitation */
911             if (!player_ptr->levitation)
912                 return false;
913         }
914     }
915
916     return true;
917 }
918
919 /*!
920  * @brief 地形は開くものであって、かつ開かれているかを返す /
921  * Attempt to open the given chest at the given location
922  * @param feat 地形ID
923  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
924  */
925 bool is_open(player_type *player_ptr, FEAT_IDX feat)
926 {
927     return f_info[feat].flags.has(FF::CLOSE) && (feat != feat_state(player_ptr->current_floor_ptr, feat, FF::CLOSE));
928 }
929
930 /*!
931  * @brief プレイヤーが地形踏破可能かを返す
932  * @param feature 判定したい地形ID
933  * @param mode 移動に関するオプションフラグ
934  * @return 移動可能ならばTRUEを返す
935  */
936 bool player_can_enter(player_type *player_ptr, FEAT_IDX feature, BIT_FLAGS16 mode)
937 {
938     feature_type *f_ptr = &f_info[feature];
939
940     if (player_ptr->riding)
941         return monster_can_cross_terrain(
942             player_ptr, feature, &r_info[player_ptr->current_floor_ptr->m_list[player_ptr->riding].r_idx], mode | CEM_RIDING);
943
944     if (f_ptr->flags.has(FF::PATTERN)) {
945         if (!(mode & CEM_P_CAN_ENTER_PATTERN))
946             return false;
947     }
948
949     if (f_ptr->flags.has(FF::CAN_FLY) && player_ptr->levitation)
950         return true;
951     if (f_ptr->flags.has(FF::CAN_SWIM) && player_ptr->can_swim)
952         return true;
953     if (f_ptr->flags.has(FF::CAN_PASS) && has_pass_wall(player_ptr))
954         return true;
955
956     if (f_ptr->flags.has_not(FF::MOVE))
957         return false;
958
959     return true;
960 }
961
962 void place_grid(player_type *player_ptr, grid_type *g_ptr, grid_bold_type gb_type)
963 {
964     switch (gb_type) {
965     case GB_FLOOR: {
966         g_ptr->feat = feat_ground_type[randint0(100)];
967         g_ptr->info &= ~(CAVE_MASK);
968         g_ptr->info |= CAVE_FLOOR;
969         break;
970     }
971     case GB_EXTRA: {
972         g_ptr->feat = feat_wall_type[randint0(100)];
973         g_ptr->info &= ~(CAVE_MASK);
974         g_ptr->info |= CAVE_EXTRA;
975         break;
976     }
977     case GB_EXTRA_PERM: {
978         g_ptr->feat = feat_permanent;
979         g_ptr->info &= ~(CAVE_MASK);
980         g_ptr->info |= CAVE_EXTRA;
981         break;
982     }
983     case GB_INNER: {
984         g_ptr->feat = feat_wall_inner;
985         g_ptr->info &= ~(CAVE_MASK);
986         g_ptr->info |= CAVE_INNER;
987         break;
988     }
989     case GB_INNER_PERM: {
990         g_ptr->feat = feat_permanent;
991         g_ptr->info &= ~(CAVE_MASK);
992         g_ptr->info |= CAVE_INNER;
993         break;
994     }
995     case GB_OUTER: {
996         g_ptr->feat = feat_wall_outer;
997         g_ptr->info &= ~(CAVE_MASK);
998         g_ptr->info |= CAVE_OUTER;
999         break;
1000     }
1001     case GB_OUTER_NOPERM: {
1002         feature_type *f_ptr = &f_info[feat_wall_outer];
1003         if (permanent_wall(f_ptr)) {
1004             g_ptr->feat = (int16_t)feat_state(player_ptr->current_floor_ptr, feat_wall_outer, FF::UNPERM);
1005         } else {
1006             g_ptr->feat = feat_wall_outer;
1007         }
1008
1009         g_ptr->info &= ~(CAVE_MASK);
1010         g_ptr->info |= (CAVE_OUTER | CAVE_VAULT);
1011         break;
1012     }
1013     case GB_SOLID: {
1014         g_ptr->feat = feat_wall_solid;
1015         g_ptr->info &= ~(CAVE_MASK);
1016         g_ptr->info |= CAVE_SOLID;
1017         break;
1018     }
1019     case GB_SOLID_PERM: {
1020         g_ptr->feat = feat_permanent;
1021         g_ptr->info &= ~(CAVE_MASK);
1022         g_ptr->info |= CAVE_SOLID;
1023         break;
1024     }
1025     case GB_SOLID_NOPERM: {
1026         feature_type *f_ptr = &f_info[feat_wall_solid];
1027         if ((g_ptr->info & CAVE_VAULT) && permanent_wall(f_ptr))
1028             g_ptr->feat = (int16_t)feat_state(player_ptr->current_floor_ptr, feat_wall_solid, FF::UNPERM);
1029         else
1030             g_ptr->feat = feat_wall_solid;
1031         g_ptr->info &= ~(CAVE_MASK);
1032         g_ptr->info |= CAVE_SOLID;
1033         break;
1034     }
1035     default:
1036         // 未知の値が渡されたら何もしない。
1037         return;
1038     }
1039
1040     if (g_ptr->m_idx > 0)
1041         delete_monster_idx(player_ptr, g_ptr->m_idx);
1042 }
1043
1044 /*!
1045  * モンスターにより照明が消されている地形か否かを判定する。 / Is this grid "darkened" by monster?
1046  * @param player_ptr プレイヤーへの参照ポインタ
1047  * @param g_ptr グリッドへの参照ポインタ
1048  * @return 照明が消されている地形ならばTRUE
1049  */
1050 bool darkened_grid(player_type *player_ptr, grid_type *g_ptr)
1051 {
1052     return ((g_ptr->info & (CAVE_VIEW | CAVE_LITE | CAVE_MNLT | CAVE_MNDK)) == (CAVE_VIEW | CAVE_MNDK)) && !player_ptr->see_nocto;
1053 }
1054
1055 void place_bold(player_type *player_ptr, POSITION y, POSITION x, grid_bold_type gb_type)
1056 {
1057     grid_type *const g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1058     place_grid(player_ptr, g_ptr, gb_type);
1059 }
1060
1061 void set_cave_feat(floor_type *floor_ptr, POSITION y, POSITION x, FEAT_IDX feature_idx)
1062 {
1063     floor_ptr->grid_array[y][x].feat = feature_idx;
1064 }
1065
1066 /*!
1067  * @brief プレイヤーの周辺9マスに該当する地形がいくつあるかを返す /
1068  * Attempt to open the given chest at the given location
1069  * @param y 該当する地形の中から1つのY座標を返す参照ポインタ
1070  * @param x 該当する地形の中から1つのX座標を返す参照ポインタ
1071  * @param test 地形条件を判定するための関数ポインタ
1072  * @param under TRUEならばプレイヤーの直下の座標も走査対象にする
1073  * @return 該当する地形の数
1074  * @details Return the number of features around (or under) the character.
1075  * Usually look for doors and floor traps.
1076  */
1077 int count_dt(player_type *player_ptr, POSITION *y, POSITION *x, bool (*test)(player_type *, FEAT_IDX), bool under)
1078 {
1079     int count = 0;
1080     for (DIRECTION d = 0; d < 9; d++) {
1081         grid_type *g_ptr;
1082         FEAT_IDX feat;
1083         if ((d == 8) && !under)
1084             continue;
1085
1086         POSITION yy = player_ptr->y + ddy_ddd[d];
1087         POSITION xx = player_ptr->x + ddx_ddd[d];
1088         g_ptr = &player_ptr->current_floor_ptr->grid_array[yy][xx];
1089         if (!g_ptr->is_mark())
1090             continue;
1091
1092         feat = g_ptr->get_feat_mimic();
1093         if (!((*test)(player_ptr, feat)))
1094             continue;
1095
1096         ++count;
1097         *y = yy;
1098         *x = xx;
1099     }
1100
1101     return count;
1102 }
1103
1104 /*!
1105  * @brief マス構造体のspecial要素を利用する地形かどうかを判定する.
1106  */
1107 bool feat_uses_special(FEAT_IDX f_idx)
1108 {
1109     return f_info[(f_idx)].flags.has(FF::SPECIAL);
1110 }
1111
1112 /*
1113  * This function allows us to efficiently add a grid to the "lite" array,
1114  * note that we are never called for illegal grids, or for grids which
1115  * have already been placed into the "lite" array, and we are never
1116  * called when the "lite" array is full.
1117  */
1118 void cave_lite_hack(floor_type *floor_ptr, POSITION y, POSITION x)
1119 {
1120     auto *g_ptr = &floor_ptr->grid_array[y][x];
1121     if (g_ptr->is_lite()) {
1122         return;
1123     }
1124
1125     g_ptr->info |= CAVE_LITE;
1126     floor_ptr->lite_y[floor_ptr->lite_n] = y;
1127     floor_ptr->lite_x[floor_ptr->lite_n++] = x;
1128 }
1129
1130 /*
1131  * For delayed visual update
1132  */
1133 void cave_redraw_later(floor_type *floor_ptr, POSITION y, POSITION x)
1134 {
1135     auto *g_ptr = &floor_ptr->grid_array[y][x];
1136     if (g_ptr->is_redraw()) {
1137         return;
1138     }
1139
1140     g_ptr->info |= CAVE_REDRAW;
1141     floor_ptr->redraw_y[floor_ptr->redraw_n] = y;
1142     floor_ptr->redraw_x[floor_ptr->redraw_n++] = x;
1143 }
1144
1145 /*
1146  * For delayed visual update
1147  */
1148 void cave_note_and_redraw_later(floor_type *floor_ptr, POSITION y, POSITION x)
1149 {
1150     floor_ptr->grid_array[y][x].info |= CAVE_NOTE;
1151     cave_redraw_later(floor_ptr, y, x);
1152 }
1153
1154 void cave_view_hack(floor_type *floor_ptr, POSITION y, POSITION x)
1155 {
1156     auto *g_ptr = &floor_ptr->grid_array[y][x];
1157     if (g_ptr->is_view()) {
1158         return;
1159     }
1160
1161     g_ptr->info |= CAVE_VIEW;
1162     floor_ptr->view_y[floor_ptr->view_n] = y;
1163     floor_ptr->view_x[floor_ptr->view_n] = x;
1164     floor_ptr->view_n++;
1165 }