2 * @brief グリッドの実装 / low level dungeon routines -BEN-
5 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\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
11 * Support for Adam Bolt's tileset, lighting and transparency effects\n
12 * by Robert Ruehlmann (rr9@angband.org)\n
14 * 2013 Deskull Doxygen向けのコメント整理\n
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.h"
31 #include "grid/object-placer.h"
32 #include "grid/trap.h"
33 #include "io/screen-util.h"
34 #include "monster-floor/monster-remover.h"
35 #include "monster-race/monster-race.h"
36 #include "monster-race/race-flags2.h"
37 #include "monster-race/race-flags7.h"
38 #include "monster/monster-info.h"
39 #include "monster/monster-status.h"
40 #include "monster/monster-update.h"
41 #include "object/item-tester-hooker.h"
42 #include "object/object-mark-types.h"
43 #include "player/player-class.h"
44 #include "player/player-status-flags.h"
45 #include "player/player-status.h"
46 #include "room/rooms-builder.h"
47 #include "spell/spell-types.h"
48 #include "system/floor-type-definition.h"
49 #include "system/grid-type-definition.h"
50 #include "system/monster-race-definition.h"
51 #include "system/monster-type-definition.h"
52 #include "system/object-type-definition.h"
53 #include "system/player-type-definition.h"
54 #include "term/term-color-types.h"
55 #include "util/bit-flags-calculator.h"
56 #include "util/point-2d.h"
57 #include "view/display-map.h"
58 #include "view/display-messages.h"
59 #include "window/main-window-util.h"
60 #include "world/world.h"
63 #define MONSTER_FLOW_DEPTH \
64 32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
67 * Feature action flags
69 #define FAF_DESTROY 0x01
70 #define FAF_NO_DROP 0x02
71 #define FAF_CRASH_GLASS 0x04
74 * @brief 地形状態フラグテーブル /
75 * The table of features' actions
77 static const byte feature_action_flags[FF_FLAG_MAX] = {
88 FAF_CRASH_GLASS, /* BASH */
90 FAF_DESTROY, /* DISARM */
92 FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
93 0, /* MAY_HAVE_GOLD */
99 0, /* RUNE_OF_PROTECTION */
123 FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
178 FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
185 0, /* RUNE_OF_EXPLOSION */
191 0, /* TELEPORTABLE */
197 * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
198 * @param creature_ptr 配置したいクリーチャーの参照ポインタ
199 * @return 配置に成功したらTRUEを返す
201 bool new_player_spot(player_type *creature_ptr)
203 POSITION y = 0, x = 0;
204 int max_attempts = 10000;
209 while (max_attempts--) {
210 /* Pick a legal spot */
211 y = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->height - 2);
212 x = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->width - 2);
214 g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
216 /* Must be a "naked" floor grid */
219 if (is_in_dungeon(creature_ptr)) {
220 f_ptr = &f_info[g_ptr->feat];
222 if (max_attempts > 5000) /* Rule 1 */
224 if (!has_flag(f_ptr->flags, FF_FLOOR))
228 if (!has_flag(f_ptr->flags, FF_MOVE))
230 if (has_flag(f_ptr->flags, FF_HIT_TRAP))
234 /* Refuse to start on anti-teleport grids in dungeon */
235 if (!has_flag(f_ptr->flags, FF_TELEPORTABLE))
238 if (!player_can_enter(creature_ptr, g_ptr->feat, 0))
240 if (!in_bounds(creature_ptr->current_floor_ptr, y, x))
243 /* Refuse to start on anti-teleport grids */
244 if (g_ptr->is_icky())
250 if (max_attempts < 1) /* Should be -1, actually if we failed... */
253 /* Save the new player grid */
261 * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
262 * @param player_ptr プレーヤーへの参照ポインタ
263 * @param g_ptr マス構造体の参照ポインタ
264 * @return 隠されたドアがあるならTRUEを返す。
266 bool is_hidden_door(player_type *player_ptr, grid_type *g_ptr)
268 if ((g_ptr->mimic || cave_has_flag_grid(g_ptr, FF_SECRET)) && is_closed_door(player_ptr, g_ptr->feat))
275 * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
278 * @return 指定された座標に照明がかかっているならTRUEを返す。。
280 bool check_local_illumination(player_type *creature_ptr, POSITION y, POSITION x)
282 /* Hack -- move towards player */
283 POSITION yy = (y < creature_ptr->y) ? (y + 1) : (y > creature_ptr->y) ? (y - 1) : y;
284 POSITION xx = (x < creature_ptr->x) ? (x + 1) : (x > creature_ptr->x) ? (x - 1) : x;
286 /* Check for "local" illumination */
288 /* Check for "complex" illumination */
289 auto *floor_ptr = creature_ptr->current_floor_ptr;
290 if ((feat_supports_los(floor_ptr->grid_array[yy][xx].get_feat_mimic())
291 && (floor_ptr->grid_array[yy][xx].info & CAVE_GLOW))
292 || (feat_supports_los(floor_ptr->grid_array[y][xx].get_feat_mimic())
293 && (floor_ptr->grid_array[y][xx].info & CAVE_GLOW))
294 || (feat_supports_los(floor_ptr->grid_array[yy][x].get_feat_mimic())
295 && (floor_ptr->grid_array[yy][x].info & CAVE_GLOW))) {
301 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
302 #define update_local_illumination_aux(C, Y, X) \
304 if (player_has_los_bold((C), (Y), (X))) { \
305 /* Update the monster */ \
306 if ((C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx) \
307 update_monster((C), (C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx, false); \
309 /* Notice and redraw */ \
310 note_spot((C), (Y), (X)); \
311 lite_spot((C), (Y), (X)); \
316 * @brief 指定された座標の照明状態を更新する / Update "local" illumination
317 * @param creature_ptr 視界元のクリーチャー
321 void update_local_illumination(player_type *creature_ptr, POSITION y, POSITION x)
326 if (!in_bounds(creature_ptr->current_floor_ptr, y, x))
329 if ((y != creature_ptr->y) && (x != creature_ptr->x)) {
330 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
331 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
332 update_local_illumination_aux(creature_ptr, yy, xx);
333 update_local_illumination_aux(creature_ptr, y, xx);
334 update_local_illumination_aux(creature_ptr, yy, x);
335 } else if (x != creature_ptr->x) /* y == creature_ptr->y */
337 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
338 for (i = -1; i <= 1; i++) {
340 update_local_illumination_aux(creature_ptr, yy, xx);
343 update_local_illumination_aux(creature_ptr, yy, x);
345 update_local_illumination_aux(creature_ptr, yy, x);
346 } else if (y != creature_ptr->y) /* x == creature_ptr->x */
348 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
349 for (i = -1; i <= 1; i++) {
351 update_local_illumination_aux(creature_ptr, yy, xx);
354 update_local_illumination_aux(creature_ptr, y, xx);
356 update_local_illumination_aux(creature_ptr, y, xx);
357 } else /* Player's grid */
359 for (i = 0; i < 8; i++) {
362 update_local_illumination_aux(creature_ptr, yy, xx);
368 * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
369 * @return 視覚に収められていないならTRUEを返す
370 * @details player_can_see_bold()関数の返り値の否定を返している。
372 bool no_lite(player_type *creature_ptr)
374 return (!player_can_see_bold(creature_ptr, creature_ptr->y, creature_ptr->x));
378 * Place an attr/char pair at the given map coordinate, if legal.
380 void print_rel(player_type *subject_ptr, SYMBOL_CODE c, TERM_COLOR a, POSITION y, POSITION x)
382 /* Only do "legal" locations */
383 if (panel_contains(y, x)) {
384 /* Hack -- fake monochrome */
386 if (current_world_ptr->timewalk_m_idx)
388 else if (is_invuln(subject_ptr) || subject_ptr->timewalk)
390 else if (subject_ptr->wraith_form)
394 /* Draw the char using the attr */
395 term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
400 * @todo ここにplayer_type を追加した時のコンパイルエラーに対処できなかったので保留
401 * Memorize interesting viewable object/features in the given grid
403 * This function should only be called on "legal" grids.
405 * This function will memorize the object and/or feature in the given
406 * grid, if they are (1) viewable and (2) interesting. Note that all
407 * objects are interesting, all terrain features except floors (and
408 * invisible traps) are interesting, and floors (and invisible traps)
409 * are interesting sometimes (depending on various options involving
410 * the illumination of floor grids).
412 * The automatic memorization of all objects and non-floor terrain
413 * features as soon as they are displayed allows incredible amounts
414 * of optimization in various places, especially "map_info()".
416 * Note that the memorization of objects is completely separate from
417 * the memorization of terrain features, preventing annoying floor
418 * memorization when a detected object is picked up from a dark floor,
419 * and object memorization when an object is dropped into a floor grid
420 * which is memorized but out-of-sight.
422 * This function should be called every time the "memorization" of
423 * a grid (or the object in a grid) is called into question, such
424 * as when an object is created in a grid, when a terrain feature
425 * "changes" from "floor" to "non-floor", when any grid becomes
426 * "illuminated" or "viewable", and when a "floor" grid becomes
429 * Note the relatively efficient use of this function by the various
430 * "update_view()" and "update_lite()" calls, to allow objects and
431 * terrain features to be memorized (and drawn) whenever they become
432 * viewable or illuminated in any way, but not when they "maintain"
433 * or "lose" their previous viewability or illumination.
435 * Note the butchered "internal" version of "player_can_see_bold()",
436 * optimized primarily for the most common cases, that is, for the
437 * non-marked floor grids.
439 void note_spot(player_type *player_ptr, POSITION y, POSITION x)
441 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
443 /* Blind players see nothing */
444 if (player_ptr->blind)
447 /* Analyze non-torch-lit grids */
448 if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT))) {
449 /* Require line of sight to the grid */
450 if (!(g_ptr->info & (CAVE_VIEW)))
453 /* Require "perma-lite" of the grid */
454 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) {
456 if (!player_ptr->see_nocto)
461 /* Hack -- memorize objects */
462 for (const auto this_o_idx : g_ptr->o_idx_list) {
463 object_type *o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
465 /* Memorize objects */
466 o_ptr->marked |= OM_FOUND;
469 /* Hack -- memorize grids */
470 if (!g_ptr->is_mark()) {
471 /* Feature code (applying "mimic" field) */
472 feature_type *f_ptr = &f_info[g_ptr->get_feat_mimic()];
474 /* Memorize some "boring" grids */
475 if (!has_flag(f_ptr->flags, FF_REMEMBER)) {
476 /* Option -- memorize all torch-lit floors */
477 if (view_torch_grids && ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || player_ptr->see_nocto)) {
478 g_ptr->info |= (CAVE_MARK);
481 /* Option -- memorize all perma-lit floors */
482 else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW)) {
483 g_ptr->info |= (CAVE_MARK);
487 /* Memorize normal grids */
488 else if (has_flag(f_ptr->flags, FF_LOS)) {
489 g_ptr->info |= (CAVE_MARK);
492 /* Memorize torch-lit walls */
493 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) {
494 g_ptr->info |= (CAVE_MARK);
497 /* Memorize walls seen by noctovision of Ninja */
498 else if (player_ptr->see_nocto) {
499 g_ptr->info |= (CAVE_MARK);
502 /* Memorize certain non-torch-lit wall grids */
503 else if (check_local_illumination(player_ptr, y, x)) {
504 g_ptr->info |= (CAVE_MARK);
508 /* Memorize terrain of the grid */
509 g_ptr->info |= (CAVE_KNOWN);
513 * Redraw (on the screen) a given MAP location
515 * This function should only be called on "legal" grids
517 void lite_spot(player_type *player_ptr, POSITION y, POSITION x)
519 /* Redraw if on screen */
520 if (panel_contains(y, x) && in_bounds2(player_ptr->current_floor_ptr, y, x)) {
526 map_info(player_ptr, y, x, &a, &c, &ta, &tc);
528 /* Hack -- fake monochrome */
530 if (current_world_ptr->timewalk_m_idx)
532 else if (is_invuln(player_ptr) || player_ptr->timewalk)
534 else if (player_ptr->wraith_form)
538 /* Hack -- Queue it */
539 term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
541 /* Update sub-windows */
542 player_ptr->window_flags |= (PW_OVERHEAD | PW_DUNGEON);
547 * Some comments on the grid flags. -BEN-
550 * One of the major bottlenecks in previous versions of Angband was in
551 * the calculation of "line of sight" from the player to various grids,
552 * such as monsters. This was such a nasty bottleneck that a lot of
553 * silly things were done to reduce the dependancy on "line of sight",
554 * for example, you could not "see" any grids in a lit room until you
555 * actually entered the room, and there were all kinds of bizarre grid
556 * flags to enable this behavior. This is also why the "call light"
557 * spells always lit an entire room.
559 * The code below provides functions to calculate the "field of view"
560 * for the player, which, once calculated, provides extremely fast
561 * calculation of "line of sight from the player", and to calculate
562 * the "field of torch lite", which, again, once calculated, provides
563 * extremely fast calculation of "which grids are lit by the player's
564 * lite source". In addition to marking grids as "GRID_VIEW" and/or
565 * "GRID_LITE", as appropriate, these functions maintain an array for
566 * each of these two flags, each array containing the locations of all
567 * of the grids marked with the appropriate flag, which can be used to
568 * very quickly scan through all of the grids in a given set.
570 * To allow more "semantically valid" field of view semantics, whenever
571 * the field of view (or the set of torch lit grids) changes, all of the
572 * grids in the field of view (or the set of torch lit grids) are "drawn"
573 * so that changes in the world will become apparent as soon as possible.
574 * This has been optimized so that only grids which actually "change" are
575 * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
576 * of the grids which are entering or leaving the relevent set of grids.
578 * These new methods are so efficient that the old nasty code was removed.
580 * Note that there is no reason to "update" the "viewable space" unless
581 * the player "moves", or walls/doors are created/destroyed, and there
582 * is no reason to "update" the "torch lit grids" unless the field of
583 * view changes, or the "light radius" changes. This means that when
584 * the player is resting, or digging, or doing anything that does not
585 * involve movement or changing the state of the dungeon, there is no
586 * need to update the "view" or the "lite" regions, which is nice.
588 * Note that the calls to the nasty "los()" function have been reduced
589 * to a bare minimum by the use of the new "field of view" calculations.
591 * I wouldn't be surprised if slight modifications to the "update_view()"
592 * function would allow us to determine "reverse line-of-sight" as well
593 * as "normal line-of-sight", which would allow monsters to use a more
594 * "correct" calculation to determine if they can "see" the player. For
595 * now, monsters simply "cheat" somewhat and assume that if the player
596 * has "line of sight" to the monster, then the monster can "pretend"
597 * that it has "line of sight" to the player.
600 * The "update_lite()" function maintains the "CAVE_LITE" flag for each
601 * grid and maintains an array of all "CAVE_LITE" grids.
603 * This set of grids is the complete set of all grids which are lit by
604 * the players light source, which allows the "player_can_see_bold()"
605 * function to work very quickly.
607 * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
608 * fact, the player (unless blind) can always "see" all grids which are
609 * marked as "CAVE_LITE", unless they are "off screen".
612 * The "update_view()" function maintains the "CAVE_VIEW" flag for each
613 * grid and maintains an array of all "CAVE_VIEW" grids.
615 * This set of grids is the complete set of all grids within line of sight
616 * of the player, allowing the "player_has_los_bold()" macro to work very
620 * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
621 * temporary internal flag to mark those grids which are not only in view,
622 * but which are also "easily" in line of sight of the player. This flag
623 * is always cleared when we are done.
626 * The current "update_lite()" and "update_view()" algorithms use the
627 * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
628 * to keep track of which grids were previously marked as "CAVE_LITE" or
629 * "CAVE_VIEW", which allows us to optimize the "screen updates".
631 * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
632 * for various other purposes, such as spreading lite or darkness during
633 * "lite_room()" / "unlite_room()", and for calculating monster flow.
636 * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
637 * in some way permanently lit. However, for the player to "see" anything
638 * in the grid, as determined by "player_can_see()", the player must not be
639 * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
640 * grids, even if marked as "perma lit", are only illuminated if they touch
641 * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
644 * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
645 * that even if the player cannot "see" the grid, he "knows" the terrain in
646 * that grid. This is used to "remember" walls/doors/stairs/floors when they
647 * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
648 * or when one of the "memorize floor grids" options induces memorization.
650 * Objects are "memorized" in a different way, using a special "marked" flag
651 * on the object itself, which is set when an object is observed or detected.
654 * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
655 * and should be illuminated by "lite room" and "darkness" spells.
658 * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
659 * and should be unavailable for "teleportation" destinations.
662 * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
663 * which is observed, and the "view_torch_grids" allows the player to memorize
664 * every torch-lit grid. The player will always memorize important walls,
665 * doors, stairs, and other terrain features, as well as any "detected" grids.
667 * Note that the new "update_view()" method allows, among other things, a room
668 * to be "partially" seen as the player approaches it, with a growing cone of
669 * floor appearing as the player gets closer to the door. Also, by not turning
670 * on the "memorize perma-lit grids" option, the player will only "see" those
671 * floor grids which are actually in line of sight.
673 * And my favorite "plus" is that you can now use a special option to draw the
674 * "floors" in the "viewable region" brightly (actually, to draw the *other*
675 * grids dimly), providing a "pretty" effect as the player runs around, and
676 * to efficiently display the "torch lite" in a special color.
679 * Some comments on the "update_view()" algorithm...
681 * The algorithm is very fast, since it spreads "obvious" grids very quickly,
682 * and only has to call "los()" on the borderline cases. The major axes/diags
683 * even terminate early when they hit walls. I need to find a quick way
684 * to "terminate" the other scans.
686 * Note that in the worst case (a big empty area with say 5% scattered walls),
687 * each of the 1500 or so nearby grids is checked once, most of them getting
688 * an "instant" rating, and only a small portion requiring a call to "los()".
690 * The only time that the algorithm appears to be "noticeably" too slow is
691 * when running, and this is usually only important in town, since the town
692 * provides about the worst scenario possible, with large open regions and
693 * a few scattered obstructions. There is a special "efficiency" option to
694 * allow the player to reduce his field of view in town, if needed.
696 * In the "best" case (say, a normal stretch of corridor), the algorithm
697 * makes one check for each viewable grid, and makes no calls to "los()".
698 * So running in corridors is very fast, and if a lot of monsters are
699 * nearby, it is much faster than the old methods.
701 * Note that resting, most normal commands, and several forms of running,
702 * plus all commands executed near large groups of monsters, are strictly
703 * more efficient with "update_view()" that with the old "compute los() on
704 * demand" method, primarily because once the "field of view" has been
705 * calculated, it does not have to be recalculated until the player moves
706 * (or a wall or door is created or destroyed).
708 * Note that we no longer have to do as many "los()" checks, since once the
709 * "view" region has been built, very few things cause it to be "changed"
710 * (player movement, and the opening/closing of doors, changes in wall status).
711 * Note that door/wall changes are only relevant when the door/wall itself is
712 * in the "view" region.
714 * The algorithm seems to only call "los()" from zero to ten times, usually
715 * only when coming down a corridor into a room, or standing in a room, just
716 * misaligned with a corridor. So if, say, there are five "nearby" monsters,
717 * we will be reducing the calls to "los()".
719 * I am thinking in terms of an algorithm that "walks" from the central point
720 * out to the maximal "distance", at each point, determining the "view" code
721 * (above). For each grid not on a major axis or diagonal, the "view" code
722 * depends on the "cave_los_bold()" and "view" of exactly two other grids
723 * (the one along the nearest diagonal, and the one next to that one, see
724 * "update_view_aux()"...).
726 * We "memorize" the viewable space array, so that at the cost of under 3000
727 * bytes, we reduce the time taken by "forget_view()" to one assignment for
728 * each grid actually in the "viewable space". And for another 3000 bytes,
729 * we prevent "erase + redraw" ineffiencies via the "seen" set. These bytes
730 * are also used by other routines, thus reducing the cost to almost nothing.
732 * A similar thing is done for "forget_lite()" in which case the savings are
733 * much less, but save us from doing bizarre maintenance checking.
735 * In the worst "normal" case (in the middle of the town), the reachable space
736 * actually reaches to more than half of the largest possible "circle" of view,
737 * or about 800 grids, and in the worse case (in the middle of a dungeon level
738 * where all the walls have been removed), the reachable space actually reaches
739 * the theoretical maximum size of just under 1500 grids.
741 * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
742 * If G1 is lite, G is lite. Else if G2 is lite, G is half. Else if G1 and G2
743 * are both half, G is half. Else G is dark. It only takes 2 (or 4) bits to
744 * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
745 * entire possible space (including initialization) in one step per grid. If
746 * we do the "clearing" as a separate step (and use an array of "view" grids),
747 * then the clearing will take as many steps as grids that were viewed, and the
748 * algorithm will be able to "stop" scanning at various points.
749 * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
753 * Hack - speed up the update_flow algorithm by only doing
754 * it everytime the player moves out of LOS of the last
757 static POSITION flow_x = 0;
758 static POSITION flow_y = 0;
761 * Hack -- fill in the "cost" field of every grid that the player
762 * can "reach" with the number of steps needed to reach that grid.
763 * This also yields the "distance" of the player from every grid.
765 * In addition, mark the "when" of the grids that can reach
766 * the player with the incremented value of "flow_n".
768 * Hack -- use the "seen" array as a "circular queue".
770 * We do not need a priority queue because the cost from grid
771 * to grid is always "one" and we process them in order.
773 void update_flow(player_type *subject_ptr)
777 floor_type *f_ptr = subject_ptr->current_floor_ptr;
779 /* The last way-point is on the map */
780 if (subject_ptr->running && in_bounds(f_ptr, flow_y, flow_x)) {
781 /* The way point is in sight - do not update. (Speedup) */
782 if (f_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW)
786 /* Erase all of the current flow information */
787 for (y = 0; y < f_ptr->height; y++) {
788 for (x = 0; x < f_ptr->width; x++) {
789 memset(&f_ptr->grid_array[y][x].costs, 0, sizeof(f_ptr->grid_array[y][x].costs));
790 memset(&f_ptr->grid_array[y][x].dists, 0, sizeof(f_ptr->grid_array[y][x].dists));
794 /* Save player position */
795 flow_y = subject_ptr->y;
796 flow_x = subject_ptr->x;
798 for (int i = 0; i < FLOW_MAX; i++) {
800 std::queue<Pos2D> que;
801 que.emplace(subject_ptr->y, subject_ptr->x);
803 /* Now process the queue */
804 while (!que.empty()) {
805 /* Extract the next entry */
806 const auto [ty, tx] = que.front();
809 /* Add the "children" */
810 for (d = 0; d < 8; d++) {
811 byte m = subject_ptr->current_floor_ptr->grid_array[ty][tx].costs[i] + 1;
812 byte n = subject_ptr->current_floor_ptr->grid_array[ty][tx].dists[i] + 1;
818 /* Ignore player's grid */
819 if (player_bold(subject_ptr, y, x))
822 grid_type *g_ptr = &subject_ptr->current_floor_ptr->grid_array[y][x];
824 if (is_closed_door(subject_ptr, g_ptr->feat))
827 /* Ignore "pre-stamped" entries */
828 if (g_ptr->dists[i] != 0 && g_ptr->dists[i] <= n && g_ptr->costs[i] <= m)
831 /* Ignore "walls", "holes" and "rubble" */
832 bool can_move = false;
835 can_move = cave_has_flag_grid(g_ptr, FF_MOVE) || cave_has_flag_grid(g_ptr, FF_CAN_FLY);
838 can_move = cave_has_flag_grid(g_ptr, FF_MOVE);
842 if (!can_move && !is_closed_door(subject_ptr, g_ptr->feat))
845 /* Save the flow cost */
846 if (g_ptr->costs[i] == 0 || g_ptr->costs[i] > m)
848 if (g_ptr->dists[i] == 0 || g_ptr->dists[i] > n)
851 /* Hack -- limit flow depth */
852 if (n == MONSTER_FLOW_DEPTH)
855 /* Enqueue that entry */
863 * Take a feature, determine what that feature becomes
864 * through applying the given action.
866 FEAT_IDX feat_state(floor_type *floor_ptr, FEAT_IDX feat, int action)
868 feature_type *f_ptr = &f_info[feat];
871 /* Get the new feature */
872 for (i = 0; i < MAX_FEAT_STATES; i++) {
873 if (f_ptr->state[i].action == action)
874 return conv_dungeon_feat(floor_ptr, f_ptr->state[i].result);
877 if (has_flag(f_ptr->flags, FF_PERMANENT))
880 return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(floor_ptr, f_ptr->destroyed) : feat;
884 * Takes a location and action and changes the feature at that
885 * location through applying the given action.
887 void cave_alter_feat(player_type *player_ptr, POSITION y, POSITION x, int action)
889 /* Set old feature */
890 floor_type *floor_ptr = player_ptr->current_floor_ptr;
891 FEAT_IDX oldfeat = floor_ptr->grid_array[y][x].feat;
893 /* Get the new feat */
894 FEAT_IDX newfeat = feat_state(player_ptr->current_floor_ptr, oldfeat, action);
897 if (newfeat == oldfeat)
900 /* Set the new feature */
901 cave_set_feat(player_ptr, y, x, newfeat);
903 if (!(feature_action_flags[action] & FAF_NO_DROP)) {
904 feature_type *old_f_ptr = &f_info[oldfeat];
905 feature_type *f_ptr = &f_info[newfeat];
909 if (has_flag(old_f_ptr->flags, FF_HAS_GOLD) && !has_flag(f_ptr->flags, FF_HAS_GOLD)) {
910 /* Place some gold */
911 place_gold(player_ptr, y, x);
916 if (has_flag(old_f_ptr->flags, FF_HAS_ITEM) && !has_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - floor_ptr->dun_level / 2))) {
918 place_object(player_ptr, y, x, 0L);
922 if (found && current_world_ptr->character_dungeon && player_can_see_bold(player_ptr, y, x)) {
923 msg_print(_("何かを発見した!", "You have found something!"));
927 if (feature_action_flags[action] & FAF_CRASH_GLASS) {
928 feature_type *old_f_ptr = &f_info[oldfeat];
930 if (has_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon) {
931 project(player_ptr, PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(floor_ptr->dun_level, 100) / 4, GF_SHARDS,
932 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI));
937 /* Remove a mirror */
938 void remove_mirror(player_type *caster_ptr, POSITION y, POSITION x)
940 grid_type *g_ptr = &caster_ptr->current_floor_ptr->grid_array[y][x];
942 /* Remove the mirror */
943 g_ptr->info &= ~(CAVE_OBJECT);
946 if (d_info[caster_ptr->dungeon_idx].flags.has(DF::DARKNESS)) {
947 g_ptr->info &= ~(CAVE_GLOW);
948 if (!view_torch_grids)
949 g_ptr->info &= ~(CAVE_MARK);
951 update_monster(caster_ptr, g_ptr->m_idx, false);
953 update_local_illumination(caster_ptr, y, x);
956 note_spot(caster_ptr, y, x);
958 lite_spot(caster_ptr, y, x);
962 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
963 * @param player_ptr プレーヤーへの参照ポインタ
964 * @param m_idx モンスターID
968 * @return テレポート先として妥当ならばtrue
970 bool cave_monster_teleportable_bold(player_type *player_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, teleport_flags mode)
972 monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
973 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
974 feature_type *f_ptr = &f_info[g_ptr->feat];
976 /* Require "teleportable" space */
977 if (!has_flag(f_ptr->flags, FF_TELEPORTABLE))
980 if (g_ptr->m_idx && (g_ptr->m_idx != m_idx))
982 if (player_bold(player_ptr, y, x))
985 /* Hack -- no teleport onto rune of protection */
986 if (g_ptr->is_rune_protection())
988 if (g_ptr->is_rune_explosion())
991 if (!(mode & TELEPORT_PASSIVE)) {
992 if (!monster_can_cross_terrain(player_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], 0))
1000 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
1001 * @param player_ptr プレーヤーへの参照ポインタ
1005 * @return テレポート先として妥当ならばtrue
1007 bool cave_player_teleportable_bold(player_type *player_ptr, POSITION y, POSITION x, teleport_flags mode)
1009 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1010 feature_type *f_ptr = &f_info[g_ptr->feat];
1012 /* Require "teleportable" space */
1013 if (!has_flag(f_ptr->flags, FF_TELEPORTABLE))
1016 /* No magical teleporting into vaults and such */
1017 if (!(mode & TELEPORT_NONMAGICAL) && g_ptr->is_icky())
1020 if (g_ptr->m_idx && (g_ptr->m_idx != player_ptr->riding))
1023 /* don't teleport on a trap. */
1024 if (has_flag(f_ptr->flags, FF_HIT_TRAP))
1027 if (!(mode & TELEPORT_PASSIVE)) {
1028 if (!player_can_enter(player_ptr, g_ptr->feat, 0))
1031 if (has_flag(f_ptr->flags, FF_WATER) && has_flag(f_ptr->flags, FF_DEEP)) {
1032 if (!player_ptr->levitation && !player_ptr->can_swim)
1036 if (has_flag(f_ptr->flags, FF_LAVA) && !has_immune_fire(player_ptr) && !is_invuln(player_ptr)) {
1037 /* Always forbid deep lava */
1038 if (has_flag(f_ptr->flags, FF_DEEP))
1041 /* Forbid shallow lava when the player don't have levitation */
1042 if (!player_ptr->levitation)
1051 * @brief 地形は開くものであって、かつ開かれているかを返す /
1052 * Attempt to open the given chest at the given location
1054 * @return 開いた地形である場合TRUEを返す / Return TRUE if the given feature is an open door
1056 bool is_open(player_type *player_ptr, FEAT_IDX feat)
1058 return has_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(player_ptr->current_floor_ptr, feat, FF_CLOSE));
1062 * @brief プレイヤーが地形踏破可能かを返す
1063 * @param feature 判定したい地形ID
1064 * @param mode 移動に関するオプションフラグ
1065 * @return 移動可能ならばTRUEを返す
1067 bool player_can_enter(player_type *creature_ptr, FEAT_IDX feature, BIT_FLAGS16 mode)
1069 feature_type *f_ptr = &f_info[feature];
1071 if (creature_ptr->riding)
1072 return monster_can_cross_terrain(
1073 creature_ptr, feature, &r_info[creature_ptr->current_floor_ptr->m_list[creature_ptr->riding].r_idx], mode | CEM_RIDING);
1075 if (has_flag(f_ptr->flags, FF_PATTERN)) {
1076 if (!(mode & CEM_P_CAN_ENTER_PATTERN))
1080 if (has_flag(f_ptr->flags, FF_CAN_FLY) && creature_ptr->levitation)
1082 if (has_flag(f_ptr->flags, FF_CAN_SWIM) && creature_ptr->can_swim)
1084 if (has_flag(f_ptr->flags, FF_CAN_PASS) && has_pass_wall(creature_ptr))
1087 if (!has_flag(f_ptr->flags, FF_MOVE))
1093 void place_grid(player_type *player_ptr, grid_type *g_ptr, grid_bold_type gb_type)
1097 g_ptr->feat = feat_ground_type[randint0(100)];
1098 g_ptr->info &= ~(CAVE_MASK);
1099 g_ptr->info |= CAVE_FLOOR;
1103 g_ptr->feat = feat_wall_type[randint0(100)];
1104 g_ptr->info &= ~(CAVE_MASK);
1105 g_ptr->info |= CAVE_EXTRA;
1108 case GB_EXTRA_PERM: {
1109 g_ptr->feat = feat_permanent;
1110 g_ptr->info &= ~(CAVE_MASK);
1111 g_ptr->info |= CAVE_EXTRA;
1115 g_ptr->feat = feat_wall_inner;
1116 g_ptr->info &= ~(CAVE_MASK);
1117 g_ptr->info |= CAVE_INNER;
1120 case GB_INNER_PERM: {
1121 g_ptr->feat = feat_permanent;
1122 g_ptr->info &= ~(CAVE_MASK);
1123 g_ptr->info |= CAVE_INNER;
1127 g_ptr->feat = feat_wall_outer;
1128 g_ptr->info &= ~(CAVE_MASK);
1129 g_ptr->info |= CAVE_OUTER;
1132 case GB_OUTER_NOPERM: {
1133 feature_type *f_ptr = &f_info[feat_wall_outer];
1134 if (permanent_wall(f_ptr)) {
1135 g_ptr->feat = (s16b)feat_state(player_ptr->current_floor_ptr, feat_wall_outer, FF_UNPERM);
1137 g_ptr->feat = feat_wall_outer;
1140 g_ptr->info &= ~(CAVE_MASK);
1141 g_ptr->info |= (CAVE_OUTER | CAVE_VAULT);
1145 g_ptr->feat = feat_wall_solid;
1146 g_ptr->info &= ~(CAVE_MASK);
1147 g_ptr->info |= CAVE_SOLID;
1150 case GB_SOLID_PERM: {
1151 g_ptr->feat = feat_permanent;
1152 g_ptr->info &= ~(CAVE_MASK);
1153 g_ptr->info |= CAVE_SOLID;
1156 case GB_SOLID_NOPERM: {
1157 feature_type *f_ptr = &f_info[feat_wall_solid];
1158 if ((g_ptr->info & CAVE_VAULT) && permanent_wall(f_ptr))
1159 g_ptr->feat = (s16b)feat_state(player_ptr->current_floor_ptr, feat_wall_solid, FF_UNPERM);
1161 g_ptr->feat = feat_wall_solid;
1162 g_ptr->info &= ~(CAVE_MASK);
1163 g_ptr->info |= CAVE_SOLID;
1171 if (g_ptr->m_idx > 0)
1172 delete_monster_idx(player_ptr, g_ptr->m_idx);
1176 * モンスターにより照明が消されている地形か否かを判定する。 / Is this grid "darkened" by monster?
1177 * @param player_ptr プレーヤーへの参照ポインタ
1178 * @param g_ptr グリッドへの参照ポインタ
1179 * @return 照明が消されている地形ならばTRUE
1181 bool darkened_grid(player_type *player_ptr, grid_type *g_ptr)
1183 return ((g_ptr->info & (CAVE_VIEW | CAVE_LITE | CAVE_MNLT | CAVE_MNDK)) == (CAVE_VIEW | CAVE_MNDK)) && !player_ptr->see_nocto;
1186 void place_bold(player_type *player_ptr, POSITION y, POSITION x, grid_bold_type gb_type)
1188 grid_type *const g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1189 place_grid(player_ptr, g_ptr, gb_type);
1192 void set_cave_feat(floor_type *floor_ptr, POSITION y, POSITION x, FEAT_IDX feature_idx)
1194 floor_ptr->grid_array[y][x].feat = feature_idx;
1198 * @todo intをenumに変更する
1200 void add_cave_info(floor_type *floor_ptr, POSITION y, POSITION x, int cave_mask)
1202 floor_ptr->grid_array[y][x].info |= cave_mask;
1206 * @brief プレイヤーの周辺9マスに該当する地形がいくつあるかを返す /
1207 * Attempt to open the given chest at the given location
1208 * @param y 該当する地形の中から1つのY座標を返す参照ポインタ
1209 * @param x 該当する地形の中から1つのX座標を返す参照ポインタ
1210 * @param test 地形条件を判定するための関数ポインタ
1211 * @param under TRUEならばプレイヤーの直下の座標も走査対象にする
1213 * @details Return the number of features around (or under) the character.
1214 * Usually look for doors and floor traps.
1216 int count_dt(player_type *creature_ptr, POSITION *y, POSITION *x, bool (*test)(player_type *, FEAT_IDX), bool under)
1219 for (DIRECTION d = 0; d < 9; d++) {
1222 if ((d == 8) && !under)
1225 POSITION yy = creature_ptr->y + ddy_ddd[d];
1226 POSITION xx = creature_ptr->x + ddx_ddd[d];
1227 g_ptr = &creature_ptr->current_floor_ptr->grid_array[yy][xx];
1228 if (!g_ptr->is_mark())
1231 feat = g_ptr->get_feat_mimic();
1232 if (!((*test)(creature_ptr, feat)))
1244 * @brief マス構造体のspecial要素を利用する地形かどうかを判定する.
1246 bool feat_uses_special(FEAT_IDX f_idx)
1248 return has_flag(f_info[(f_idx)].flags, FF_SPECIAL);
1252 * This function allows us to efficiently add a grid to the "lite" array,
1253 * note that we are never called for illegal grids, or for grids which
1254 * have already been placed into the "lite" array, and we are never
1255 * called when the "lite" array is full.
1257 void cave_lite_hack(floor_type *floor_ptr, POSITION y, POSITION x)
1259 auto *g_ptr = &floor_ptr->grid_array[y][x];
1260 if (g_ptr->is_lite()) {
1264 g_ptr->info |= CAVE_LITE;
1265 floor_ptr->lite_y[floor_ptr->lite_n] = y;
1266 floor_ptr->lite_x[floor_ptr->lite_n++] = x;
1270 * For delayed visual update
1272 void cave_redraw_later(floor_type *floor_ptr, POSITION y, POSITION x)
1274 auto *g_ptr = &floor_ptr->grid_array[y][x];
1275 if (g_ptr->is_redraw()) {
1279 g_ptr->info |= CAVE_REDRAW;
1280 floor_ptr->redraw_y[floor_ptr->redraw_n] = y;
1281 floor_ptr->redraw_x[floor_ptr->redraw_n++] = x;
1285 * For delayed visual update
1287 void cave_note_and_redraw_later(floor_type *floor_ptr, POSITION y, POSITION x)
1289 floor_ptr->grid_array[y][x].info |= CAVE_NOTE;
1290 cave_redraw_later(floor_ptr, y, x);
1293 void cave_view_hack(floor_type *floor_ptr, POSITION y, POSITION x)
1295 auto *g_ptr = &floor_ptr->grid_array[y][x];
1296 if (g_ptr->is_view()) {
1300 g_ptr->info |= CAVE_VIEW;
1301 floor_ptr->view_y[floor_ptr->view_n] = y;
1302 floor_ptr->view_x[floor_ptr->view_n] = x;
1303 floor_ptr->view_n++;