4 * @brief グリッドの実装 / low level dungeon routines -BEN-
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
9 * This software may be copied and distributed for educational, research,\n
10 * and not for profit purposes provided that this copyright and statement\n
11 * are included in all such copies. Other copyrights may also apply.\n
13 * Support for Adam Bolt's tileset, lighting and transparency effects\n
14 * by Robert Ruehlmann (rr9@angband.org)\n
16 * 2013 Deskull Doxygen向けのコメント整理\n
26 #include "object-flavor.h"
27 #include "object-hook.h"
29 #include "floor-generate.h"
36 #include "monster-status.h"
37 #include "player-status.h"
38 #include "player-effects.h"
39 #include "player-class.h"
41 #include "view-mainwindow.h"
42 #include "realm-song.h"
44 #define MONSTER_FLOW_DEPTH 32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
47 * Feature action flags
49 #define FAF_DESTROY 0x01
50 #define FAF_NO_DROP 0x02
51 #define FAF_CRASH_GLASS 0x04
54 * @brief 地形状態フラグテーブル /
55 * The table of features' actions
57 static const byte feature_action_flags[FF_FLAG_MAX] =
69 FAF_CRASH_GLASS, /* BASH */
71 FAF_DESTROY, /* DISARM */
73 FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
74 0, /* MAY_HAVE_GOLD */
104 FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
159 FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
172 0, /* TELEPORTABLE */
178 * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
179 * @param creature_ptr 配置したいクリーチャーの参照ポインタ
180 * @return 配置に成功したらTRUEを返す
182 bool new_player_spot(player_type *creature_ptr)
184 POSITION y = 0, x = 0;
185 int max_attempts = 10000;
190 while (max_attempts--)
192 /* Pick a legal spot */
193 y = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->height - 2);
194 x = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->width - 2);
196 g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
198 /* Must be a "naked" floor grid */
199 if (g_ptr->m_idx) continue;
200 if (creature_ptr->current_floor_ptr->dun_level)
202 f_ptr = &f_info[g_ptr->feat];
204 if (max_attempts > 5000) /* Rule 1 */
206 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
210 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
211 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
214 /* Refuse to start on anti-teleport grids in dungeon */
215 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
217 if (!player_can_enter(creature_ptr, g_ptr->feat, 0)) continue;
218 if (!in_bounds(creature_ptr->current_floor_ptr, y, x)) continue;
220 /* Refuse to start on anti-teleport grids */
221 if (g_ptr->info & (CAVE_ICKY)) continue;
226 if (max_attempts < 1) /* Should be -1, actually if we failed... */
229 /* Save the new player grid */
237 * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
238 * @param y トラップを配置したいマスの中心Y座標
239 * @param x トラップを配置したいマスの中心X座標
240 * @param yd Y方向の配置分散マス数
241 * @param xd X方向の配置分散マス数
242 * @param num 配置したいトラップの数
245 * Only really called by some of the "vault" routines.
247 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
251 for (i = 0; i < num; i++)
253 vault_trap_aux(p_ptr->current_floor_ptr, y, x, yd, xd);
258 * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
259 * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
262 void place_bound_perm_wall(grid_type *g_ptr)
264 if (bound_walls_perm)
266 /* Clear boundary mimic */
271 feature_type *f_ptr = &f_info[g_ptr->feat];
273 /* Hack -- Decline boundary walls with known treasure */
274 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
275 !have_flag(f_ptr->flags, FF_SECRET))
276 g_ptr->feat = feat_state(g_ptr->feat, FF_ENSECRET);
278 /* Set boundary mimic */
279 g_ptr->mimic = g_ptr->feat;
282 /* Add "solid" perma-wall */
283 place_solid_perm_grid(g_ptr);
287 * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
288 * @param g_ptr マス構造体の参照ポインタ
289 * @return 看破済みの罠があるならTRUEを返す。
291 bool is_known_trap(grid_type *g_ptr)
293 if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
294 is_trap(g_ptr->feat)) return TRUE;
302 * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
303 * @param g_ptr マス構造体の参照ポインタ
304 * @return 隠されたドアがあるならTRUEを返す。
306 bool is_hidden_door(grid_type *g_ptr)
308 if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
309 is_closed_door(g_ptr->feat))
315 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
319 * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
322 * @return 指定された座標に照明がかかっているならTRUEを返す。。
324 bool check_local_illumination(player_type *creature_ptr, POSITION y, POSITION x)
326 /* Hack -- move towards player */
327 POSITION yy = (y < creature_ptr->y) ? (y + 1) : (y > creature_ptr->y) ? (y - 1) : y;
328 POSITION xx = (x < creature_ptr->x) ? (x + 1) : (x > creature_ptr->x) ? (x - 1) : x;
330 /* Check for "local" illumination */
332 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
334 /* Check for "complex" illumination */
335 if ((feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[yy][xx])) &&
336 (creature_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
337 (feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[y][xx])) &&
338 (creature_ptr->current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
339 (feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[yy][x])) &&
340 (creature_ptr->current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
346 #else /* COMPLEX_WALL_ILLUMINATION */
348 /* Check for "simple" illumination */
349 return (creature_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
351 #endif /* COMPLEX_WALL_ILLUMINATION */
355 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
356 #define update_local_illumination_aux(C, Y, X) \
358 if (player_has_los_bold((C), (Y), (X))) \
360 /* Update the monster */ \
361 if ((C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx) update_monster((C), (C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx, FALSE); \
363 /* Notice and redraw */ \
364 note_spot((Y), (X)); \
365 lite_spot((Y), (X)); \
370 * @brief 指定された座標の照明状態を更新する / Update "local" illumination
371 * @param creature_ptr 視界元のクリーチャー
376 void update_local_illumination(player_type * creature_ptr, POSITION y, POSITION x)
381 if (!in_bounds(p_ptr->current_floor_ptr, y, x)) return;
383 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
385 if ((y != creature_ptr->y) && (x != creature_ptr->x))
387 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
388 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
389 update_local_illumination_aux(creature_ptr, yy, xx);
390 update_local_illumination_aux(creature_ptr, y, xx);
391 update_local_illumination_aux(creature_ptr, yy, x);
393 else if (x != creature_ptr->x) /* y == creature_ptr->y */
395 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
396 for (i = -1; i <= 1; i++)
399 update_local_illumination_aux(creature_ptr, yy, xx);
402 update_local_illumination_aux(creature_ptr, yy, x);
404 update_local_illumination_aux(creature_ptr, yy, x);
406 else if (y != creature_ptr->y) /* x == creature_ptr->x */
408 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
409 for (i = -1; i <= 1; i++)
412 update_local_illumination_aux(creature_ptr, yy, xx);
415 update_local_illumination_aux(creature_ptr, y, xx);
417 update_local_illumination_aux(creature_ptr, y, xx);
419 else /* Player's grid */
421 for (i = 0; i < 8; i++)
425 update_local_illumination_aux(creature_ptr, yy, xx);
429 #else /* COMPLEX_WALL_ILLUMINATION */
431 if ((y != creature_ptr->y) && (x != creature_ptr->x))
433 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
434 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
435 update_local_illumination_aux(creature_ptr, yy, xx);
437 else if (x != creature_ptr->x) /* y == creature_ptr->y */
439 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
440 for (i = -1; i <= 1; i++)
443 update_local_illumination_aux(creature_ptr, yy, xx);
446 else if (y != creature_ptr->y) /* x == creature_ptr->x */
448 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
449 for (i = -1; i <= 1; i++)
452 update_local_illumination_aux(creature_ptr, yy, xx);
455 else /* Player's grid */
457 for (i = 0; i < 8; i++)
461 update_local_illumination_aux(creature_ptr, yy, xx);
465 #endif /* COMPLEX_WALL_ILLUMINATION */
470 * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
471 * @return 視覚に収められていないならTRUEを返す
472 * @details player_can_see_bold()関数の返り値の否定を返している。
476 return (!player_can_see_bold(p_ptr, p_ptr->y, p_ptr->x));
480 * Place an attr/char pair at the given map coordinate, if legal.
482 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
484 /* Only do "legal" locations */
485 if (panel_contains(y, x))
487 /* Hack -- fake monochrome */
490 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
491 else if (IS_INVULN(p_ptr) || p_ptr->timewalk) a = TERM_WHITE;
492 else if (p_ptr->wraith_form) a = TERM_L_DARK;
495 /* Draw the char using the attr */
496 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
502 * Memorize interesting viewable object/features in the given grid
504 * This function should only be called on "legal" grids.
506 * This function will memorize the object and/or feature in the given
507 * grid, if they are (1) viewable and (2) interesting. Note that all
508 * objects are interesting, all terrain features except floors (and
509 * invisible traps) are interesting, and floors (and invisible traps)
510 * are interesting sometimes (depending on various options involving
511 * the illumination of floor grids).
513 * The automatic memorization of all objects and non-floor terrain
514 * features as soon as they are displayed allows incredible amounts
515 * of optimization in various places, especially "map_info()".
517 * Note that the memorization of objects is completely separate from
518 * the memorization of terrain features, preventing annoying floor
519 * memorization when a detected object is picked up from a dark floor,
520 * and object memorization when an object is dropped into a floor grid
521 * which is memorized but out-of-sight.
523 * This function should be called every time the "memorization" of
524 * a grid (or the object in a grid) is called into question, such
525 * as when an object is created in a grid, when a terrain feature
526 * "changes" from "floor" to "non-floor", when any grid becomes
527 * "illuminated" or "viewable", and when a "floor" grid becomes
530 * Note the relatively efficient use of this function by the various
531 * "update_view()" and "update_lite()" calls, to allow objects and
532 * terrain features to be memorized (and drawn) whenever they become
533 * viewable or illuminated in any way, but not when they "maintain"
534 * or "lose" their previous viewability or illumination.
536 * Note the butchered "internal" version of "player_can_see_bold()",
537 * optimized primarily for the most common cases, that is, for the
538 * non-marked floor grids.
540 void note_spot(POSITION y, POSITION x)
542 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
543 OBJECT_IDX this_o_idx, next_o_idx = 0;
545 /* Blind players see nothing */
546 if (p_ptr->blind) return;
548 /* Analyze non-torch-lit grids */
549 if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
551 /* Require line of sight to the grid */
552 if (!(g_ptr->info & (CAVE_VIEW))) return;
554 /* Require "perma-lite" of the grid */
555 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
558 if (!p_ptr->see_nocto) return;
563 /* Hack -- memorize objects */
564 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
566 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[this_o_idx];
567 next_o_idx = o_ptr->next_o_idx;
569 /* Memorize objects */
570 o_ptr->marked |= OM_FOUND;
574 /* Hack -- memorize grids */
575 if (!(g_ptr->info & (CAVE_MARK)))
577 /* Feature code (applying "mimic" field) */
578 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
580 /* Memorize some "boring" grids */
581 if (!have_flag(f_ptr->flags, FF_REMEMBER))
583 /* Option -- memorize all torch-lit floors */
584 if (view_torch_grids &&
585 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
587 g_ptr->info |= (CAVE_MARK);
590 /* Option -- memorize all perma-lit floors */
591 else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
593 g_ptr->info |= (CAVE_MARK);
597 /* Memorize normal grids */
598 else if (have_flag(f_ptr->flags, FF_LOS))
600 g_ptr->info |= (CAVE_MARK);
603 /* Memorize torch-lit walls */
604 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
606 g_ptr->info |= (CAVE_MARK);
609 /* Memorize walls seen by noctovision of Ninja */
610 else if (p_ptr->see_nocto)
612 g_ptr->info |= (CAVE_MARK);
615 /* Memorize certain non-torch-lit wall grids */
616 else if (check_local_illumination(p_ptr, y, x))
618 g_ptr->info |= (CAVE_MARK);
622 /* Memorize terrain of the grid */
623 g_ptr->info |= (CAVE_KNOWN);
627 * Redraw (on the screen) a given MAP location
629 * This function should only be called on "legal" grids
631 void lite_spot(POSITION y, POSITION x)
633 /* Redraw if on screen */
634 if (panel_contains(y, x) && in_bounds2(p_ptr->current_floor_ptr, y, x))
641 map_info(y, x, &a, &c, &ta, &tc);
643 /* Hack -- fake monochrome */
646 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
647 else if (IS_INVULN(p_ptr) || p_ptr->timewalk) a = TERM_WHITE;
648 else if (p_ptr->wraith_form) a = TERM_L_DARK;
651 /* Hack -- Queue it */
652 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
654 /* Update sub-windows */
655 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
660 * Some comments on the grid flags. -BEN-
663 * One of the major bottlenecks in previous versions of Angband was in
664 * the calculation of "line of sight" from the player to various grids,
665 * such as monsters. This was such a nasty bottleneck that a lot of
666 * silly things were done to reduce the dependancy on "line of sight",
667 * for example, you could not "see" any grids in a lit room until you
668 * actually entered the room, and there were all kinds of bizarre grid
669 * flags to enable this behavior. This is also why the "call light"
670 * spells always lit an entire room.
672 * The code below provides functions to calculate the "field of view"
673 * for the player, which, once calculated, provides extremely fast
674 * calculation of "line of sight from the player", and to calculate
675 * the "field of torch lite", which, again, once calculated, provides
676 * extremely fast calculation of "which grids are lit by the player's
677 * lite source". In addition to marking grids as "GRID_VIEW" and/or
678 * "GRID_LITE", as appropriate, these functions maintain an array for
679 * each of these two flags, each array containing the locations of all
680 * of the grids marked with the appropriate flag, which can be used to
681 * very quickly scan through all of the grids in a given set.
683 * To allow more "semantically valid" field of view semantics, whenever
684 * the field of view (or the set of torch lit grids) changes, all of the
685 * grids in the field of view (or the set of torch lit grids) are "drawn"
686 * so that changes in the world will become apparent as soon as possible.
687 * This has been optimized so that only grids which actually "change" are
688 * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
689 * of the grids which are entering or leaving the relevent set of grids.
691 * These new methods are so efficient that the old nasty code was removed.
693 * Note that there is no reason to "update" the "viewable space" unless
694 * the player "moves", or walls/doors are created/destroyed, and there
695 * is no reason to "update" the "torch lit grids" unless the field of
696 * view changes, or the "light radius" changes. This means that when
697 * the player is resting, or digging, or doing anything that does not
698 * involve movement or changing the state of the dungeon, there is no
699 * need to update the "view" or the "lite" regions, which is nice.
701 * Note that the calls to the nasty "los()" function have been reduced
702 * to a bare minimum by the use of the new "field of view" calculations.
704 * I wouldn't be surprised if slight modifications to the "update_view()"
705 * function would allow us to determine "reverse line-of-sight" as well
706 * as "normal line-of-sight", which would allow monsters to use a more
707 * "correct" calculation to determine if they can "see" the player. For
708 * now, monsters simply "cheat" somewhat and assume that if the player
709 * has "line of sight" to the monster, then the monster can "pretend"
710 * that it has "line of sight" to the player.
713 * The "update_lite()" function maintains the "CAVE_LITE" flag for each
714 * grid and maintains an array of all "CAVE_LITE" grids.
716 * This set of grids is the complete set of all grids which are lit by
717 * the players light source, which allows the "player_can_see_bold()"
718 * function to work very quickly.
720 * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
721 * fact, the player (unless blind) can always "see" all grids which are
722 * marked as "CAVE_LITE", unless they are "off screen".
725 * The "update_view()" function maintains the "CAVE_VIEW" flag for each
726 * grid and maintains an array of all "CAVE_VIEW" grids.
728 * This set of grids is the complete set of all grids within line of sight
729 * of the player, allowing the "player_has_los_bold(p_ptr, )" macro to work very
733 * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
734 * temporary internal flag to mark those grids which are not only in view,
735 * but which are also "easily" in line of sight of the player. This flag
736 * is always cleared when we are done.
739 * The current "update_lite()" and "update_view()" algorithms use the
740 * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
741 * to keep track of which grids were previously marked as "CAVE_LITE" or
742 * "CAVE_VIEW", which allows us to optimize the "screen updates".
744 * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
745 * for various other purposes, such as spreading lite or darkness during
746 * "lite_room()" / "unlite_room()", and for calculating monster flow.
749 * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
750 * in some way permanently lit. However, for the player to "see" anything
751 * in the grid, as determined by "player_can_see()", the player must not be
752 * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
753 * grids, even if marked as "perma lit", are only illuminated if they touch
754 * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
757 * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
758 * that even if the player cannot "see" the grid, he "knows" the terrain in
759 * that grid. This is used to "remember" walls/doors/stairs/floors when they
760 * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite(p_ptr, )",
761 * or when one of the "memorize floor grids" options induces memorization.
763 * Objects are "memorized" in a different way, using a special "marked" flag
764 * on the object itself, which is set when an object is observed or detected.
767 * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
768 * and should be illuminated by "lite room" and "darkness" spells.
771 * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
772 * and should be unavailable for "teleportation" destinations.
775 * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
776 * which is observed, and the "view_torch_grids" allows the player to memorize
777 * every torch-lit grid. The player will always memorize important walls,
778 * doors, stairs, and other terrain features, as well as any "detected" grids.
780 * Note that the new "update_view()" method allows, among other things, a room
781 * to be "partially" seen as the player approaches it, with a growing cone of
782 * floor appearing as the player gets closer to the door. Also, by not turning
783 * on the "memorize perma-lit grids" option, the player will only "see" those
784 * floor grids which are actually in line of sight.
786 * And my favorite "plus" is that you can now use a special option to draw the
787 * "floors" in the "viewable region" brightly (actually, to draw the *other*
788 * grids dimly), providing a "pretty" effect as the player runs around, and
789 * to efficiently display the "torch lite" in a special color.
792 * Some comments on the "update_view()" algorithm...
794 * The algorithm is very fast, since it spreads "obvious" grids very quickly,
795 * and only has to call "los()" on the borderline cases. The major axes/diags
796 * even terminate early when they hit walls. I need to find a quick way
797 * to "terminate" the other scans.
799 * Note that in the worst case (a big empty area with say 5% scattered walls),
800 * each of the 1500 or so nearby grids is checked once, most of them getting
801 * an "instant" rating, and only a small portion requiring a call to "los()".
803 * The only time that the algorithm appears to be "noticeably" too slow is
804 * when running, and this is usually only important in town, since the town
805 * provides about the worst scenario possible, with large open regions and
806 * a few scattered obstructions. There is a special "efficiency" option to
807 * allow the player to reduce his field of view in town, if needed.
809 * In the "best" case (say, a normal stretch of corridor), the algorithm
810 * makes one check for each viewable grid, and makes no calls to "los()".
811 * So running in corridors is very fast, and if a lot of monsters are
812 * nearby, it is much faster than the old methods.
814 * Note that resting, most normal commands, and several forms of running,
815 * plus all commands executed near large groups of monsters, are strictly
816 * more efficient with "update_view()" that with the old "compute los() on
817 * demand" method, primarily because once the "field of view" has been
818 * calculated, it does not have to be recalculated until the player moves
819 * (or a wall or door is created or destroyed).
821 * Note that we no longer have to do as many "los()" checks, since once the
822 * "view" region has been built, very few things cause it to be "changed"
823 * (player movement, and the opening/closing of doors, changes in wall status).
824 * Note that door/wall changes are only relevant when the door/wall itself is
825 * in the "view" region.
827 * The algorithm seems to only call "los()" from zero to ten times, usually
828 * only when coming down a corridor into a room, or standing in a room, just
829 * misaligned with a corridor. So if, say, there are five "nearby" monsters,
830 * we will be reducing the calls to "los()".
832 * I am thinking in terms of an algorithm that "walks" from the central point
833 * out to the maximal "distance", at each point, determining the "view" code
834 * (above). For each grid not on a major axis or diagonal, the "view" code
835 * depends on the "cave_los_bold(p_ptr->current_floor_ptr, )" and "view" of exactly two other grids
836 * (the one along the nearest diagonal, and the one next to that one, see
837 * "update_view_aux()"...).
839 * We "memorize" the viewable space array, so that at the cost of under 3000
840 * bytes, we reduce the time taken by "forget_view()" to one assignment for
841 * each grid actually in the "viewable space". And for another 3000 bytes,
842 * we prevent "erase + redraw" ineffiencies via the "seen" set. These bytes
843 * are also used by other routines, thus reducing the cost to almost nothing.
845 * A similar thing is done for "forget_lite()" in which case the savings are
846 * much less, but save us from doing bizarre maintenance checking.
848 * In the worst "normal" case (in the middle of the town), the reachable space
849 * actually reaches to more than half of the largest possible "circle" of view,
850 * or about 800 grids, and in the worse case (in the middle of a dungeon level
851 * where all the walls have been removed), the reachable space actually reaches
852 * the theoretical maximum size of just under 1500 grids.
854 * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
855 * If G1 is lite, G is lite. Else if G2 is lite, G is half. Else if G1 and G2
856 * are both half, G is half. Else G is dark. It only takes 2 (or 4) bits to
857 * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
858 * entire possible space (including initialization) in one step per grid. If
859 * we do the "clearing" as a separate step (and use an array of "view" grids),
860 * then the clearing will take as many steps as grids that were viewed, and the
861 * algorithm will be able to "stop" scanning at various points.
862 * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
866 * Mega-Hack -- Delayed visual update
867 * Only used if update_view(), update_lite() or update_mon_lite() was called
869 void delayed_visual_update(void)
875 /* Update needed grids */
876 for (i = 0; i < p_ptr->current_floor_ptr->redraw_n; i++)
878 y = p_ptr->current_floor_ptr->redraw_y[i];
879 x = p_ptr->current_floor_ptr->redraw_x[i];
880 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
882 /* Update only needed grids (prevent multiple updating) */
883 if (!(g_ptr->info & CAVE_REDRAW)) continue;
885 /* If required, note */
886 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
890 /* Hack -- Visual update of monster on this grid */
891 if (g_ptr->m_idx) update_monster(p_ptr, g_ptr->m_idx, FALSE);
893 /* No longer in the array */
894 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
898 p_ptr->current_floor_ptr->redraw_n = 0;
903 * Hack - speed up the update_flow algorithm by only doing
904 * it everytime the player moves out of LOS of the last
907 static POSITION flow_x = 0;
908 static POSITION flow_y = 0;
913 * Hack -- fill in the "cost" field of every grid that the player
914 * can "reach" with the number of steps needed to reach that grid.
915 * This also yields the "distance" of the player from every grid.
917 * In addition, mark the "when" of the grids that can reach
918 * the player with the incremented value of "flow_n".
920 * Hack -- use the "seen" array as a "circular queue".
922 * We do not need a priority queue because the cost from grid
923 * to grid is always "one" and we process them in order.
925 void update_flow(player_type *subject_ptr)
932 /* Paranoia -- make sure the array is empty */
933 if (tmp_pos.n) return;
935 /* The last way-point is on the map */
936 if (subject_ptr->running && in_bounds(subject_ptr->current_floor_ptr, flow_y, flow_x))
938 /* The way point is in sight - do not update. (Speedup) */
939 if (subject_ptr->current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
942 /* Erase all of the current flow information */
943 for (y = 0; y < subject_ptr->current_floor_ptr->height; y++)
945 for (x = 0; x < subject_ptr->current_floor_ptr->width; x++)
947 subject_ptr->current_floor_ptr->grid_array[y][x].cost = 0;
948 subject_ptr->current_floor_ptr->grid_array[y][x].dist = 0;
952 /* Save player position */
953 flow_y = subject_ptr->y;
954 flow_x = subject_ptr->x;
956 /* Add the player's grid to the queue */
957 tmp_pos.y[0] = subject_ptr->y;
958 tmp_pos.x[0] = subject_ptr->x;
960 /* Now process the queue */
961 while (flow_head != flow_tail)
965 /* Extract the next entry */
966 ty = tmp_pos.y[flow_tail];
967 tx = tmp_pos.x[flow_tail];
969 /* Forget that entry */
970 if (++flow_tail == TEMP_MAX) flow_tail = 0;
972 /* Add the "children" */
973 for (d = 0; d < 8; d++)
975 int old_head = flow_head;
976 byte_hack m = subject_ptr->current_floor_ptr->grid_array[ty][tx].cost + 1;
977 byte_hack n = subject_ptr->current_floor_ptr->grid_array[ty][tx].dist + 1;
984 /* Ignore player's grid */
985 if (player_bold(subject_ptr, y, x)) continue;
987 g_ptr = &subject_ptr->current_floor_ptr->grid_array[y][x];
989 if (is_closed_door(g_ptr->feat)) m += 3;
991 /* Ignore "pre-stamped" entries */
992 if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
994 /* Ignore "walls" and "rubble" */
995 if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
997 /* Save the flow cost */
998 if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
999 if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
1001 /* Hack -- limit flow depth */
1002 if (n == MONSTER_FLOW_DEPTH) continue;
1004 /* Enqueue that entry */
1005 tmp_pos.y[flow_head] = y;
1006 tmp_pos.x[flow_head] = x;
1008 /* Advance the queue */
1009 if (++flow_head == TEMP_MAX) flow_head = 0;
1011 /* Hack -- notice overflow by forgetting new entry */
1012 if (flow_head == flow_tail) flow_head = old_head;
1018 * Take a feature, determine what that feature becomes
1019 * through applying the given action.
1021 FEAT_IDX feat_state(FEAT_IDX feat, int action)
1023 feature_type *f_ptr = &f_info[feat];
1026 /* Get the new feature */
1027 for (i = 0; i < MAX_FEAT_STATES; i++)
1029 if (f_ptr->state[i].action == action) return conv_dungeon_feat(p_ptr->current_floor_ptr, f_ptr->state[i].result);
1032 if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
1034 return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(p_ptr->current_floor_ptr, f_ptr->destroyed) : feat;
1038 * Takes a location and action and changes the feature at that
1039 * location through applying the given action.
1041 void cave_alter_feat(POSITION y, POSITION x, int action)
1043 /* Set old feature */
1044 FEAT_IDX oldfeat = p_ptr->current_floor_ptr->grid_array[y][x].feat;
1046 /* Get the new feat */
1047 FEAT_IDX newfeat = feat_state(oldfeat, action);
1050 if (newfeat == oldfeat) return;
1052 /* Set the new feature */
1053 cave_set_feat(p_ptr->current_floor_ptr, y, x, newfeat);
1055 if (!(feature_action_flags[action] & FAF_NO_DROP))
1057 feature_type *old_f_ptr = &f_info[oldfeat];
1058 feature_type *f_ptr = &f_info[newfeat];
1062 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
1064 /* Place some gold */
1070 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - p_ptr->current_floor_ptr->dun_level / 2)))
1073 place_object(p_ptr->current_floor_ptr, y, x, 0L);
1077 if (found && current_world_ptr->character_dungeon && player_can_see_bold(p_ptr, y, x))
1079 msg_print(_("何かを発見した!", "You have found something!"));
1083 if (feature_action_flags[action] & FAF_CRASH_GLASS)
1085 feature_type *old_f_ptr = &f_info[oldfeat];
1087 if (have_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon)
1089 project(p_ptr, PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(p_ptr->current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
1090 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1096 /* Remove a mirror */
1097 void remove_mirror(POSITION y, POSITION x)
1099 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1101 /* Remove the mirror */
1102 g_ptr->info &= ~(CAVE_OBJECT);
1105 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
1107 g_ptr->info &= ~(CAVE_GLOW);
1108 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1109 if (g_ptr->m_idx) update_monster(p_ptr, g_ptr->m_idx, FALSE);
1111 update_local_illumination(p_ptr, y, x);
1121 * Return TRUE if there is a mirror on the grid.
1123 bool is_mirror_grid(grid_type *g_ptr)
1125 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
1133 * Return TRUE if there is a mirror on the grid.
1135 bool is_glyph_grid(grid_type *g_ptr)
1137 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
1145 * Return TRUE if there is a mirror on the grid.
1147 bool is_explosive_rune_grid(grid_type *g_ptr)
1149 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
1156 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
1157 * @param m_idx モンスターID
1161 * @return テレポート先として妥当ならばtrue
1163 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
1165 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1166 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1167 feature_type *f_ptr = &f_info[g_ptr->feat];
1169 /* Require "teleportable" space */
1170 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1172 if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
1173 if (player_bold(p_ptr, y, x)) return FALSE;
1175 /* Hack -- no teleport onto glyph of warding */
1176 if (is_glyph_grid(g_ptr)) return FALSE;
1177 if (is_explosive_rune_grid(g_ptr)) return FALSE;
1179 if (!(mode & TELEPORT_PASSIVE))
1181 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
1188 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
1192 * @return テレポート先として妥当ならばtrue
1194 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
1196 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1197 feature_type *f_ptr = &f_info[g_ptr->feat];
1199 /* Require "teleportable" space */
1200 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1202 /* No magical teleporting into vaults and such */
1203 if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
1205 if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
1207 /* don't teleport on a trap. */
1208 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
1210 if (!(mode & TELEPORT_PASSIVE))
1212 if (!player_can_enter(p_ptr, g_ptr->feat, 0)) return FALSE;
1214 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
1216 if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
1219 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN(p_ptr))
1221 /* Always forbid deep lava */
1222 if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
1224 /* Forbid shallow lava when the player don't have levitation */
1225 if (!p_ptr->levitation) return FALSE;
1234 * @brief 地形は開くものであって、かつ開かれているかを返す /
1235 * Attempt to open the given chest at the given location
1237 * @return 開いた地形である場合TRUEを返す / Return TRUE if the given feature is an open door
1239 bool is_open(FEAT_IDX feat)
1241 return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
1245 * @brief プレイヤーが地形踏破可能かを返す
1246 * @param feature 判定したい地形ID
1247 * @param mode 移動に関するオプションフラグ
1248 * @return 移動可能ならばTRUEを返す
1250 bool player_can_enter(player_type *creature_ptr, FEAT_IDX feature, BIT_FLAGS16 mode)
1252 feature_type *f_ptr = &f_info[feature];
1254 if (creature_ptr->riding) return monster_can_cross_terrain(feature, &r_info[creature_ptr->current_floor_ptr->m_list[creature_ptr->riding].r_idx], mode | CEM_RIDING);
1256 if (have_flag(f_ptr->flags, FF_PATTERN))
1258 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
1261 if (have_flag(f_ptr->flags, FF_CAN_FLY) && creature_ptr->levitation) return TRUE;
1262 if (have_flag(f_ptr->flags, FF_CAN_SWIM) && creature_ptr->can_swim) return TRUE;
1263 if (have_flag(f_ptr->flags, FF_CAN_PASS) && creature_ptr->pass_wall) return TRUE;
1265 if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;