3 * @brief グリッドの実装 / low level dungeon routines -BEN-
6 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
8 * This software may be copied and distributed for educational, research,\n
9 * and not for profit purposes provided that this copyright and statement\n
10 * are included in all such copies. Other copyrights may also apply.\n
12 * Support for Adam Bolt's tileset, lighting and transparency effects\n
13 * by Robert Ruehlmann (rr9@angband.org)\n
15 * 2013 Deskull Doxygen向けのコメント整理\n
22 #include "world/world.h"
23 #include "object-flavor.h"
24 #include "object-hook.h"
25 #include "dungeon/dungeon.h"
26 #include "floor-generate.h"
33 #include "monster-status.h"
34 #include "player-status.h"
35 #include "player-effects.h"
36 #include "player-class.h"
37 #include "view/display-main-window.h"
38 #include "realm/realm-song.h"
39 #include "spell/process-effect.h"
40 #include "effect/effect-characteristics.h"
42 #define MONSTER_FLOW_DEPTH 32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
45 * Feature action flags
47 #define FAF_DESTROY 0x01
48 #define FAF_NO_DROP 0x02
49 #define FAF_CRASH_GLASS 0x04
52 * @brief 地形状態フラグテーブル /
53 * The table of features' actions
55 static const byte feature_action_flags[FF_FLAG_MAX] =
67 FAF_CRASH_GLASS, /* BASH */
69 FAF_DESTROY, /* DISARM */
71 FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
72 0, /* MAY_HAVE_GOLD */
102 FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
157 FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
170 0, /* TELEPORTABLE */
176 * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
177 * @param creature_ptr 配置したいクリーチャーの参照ポインタ
178 * @return 配置に成功したらTRUEを返す
180 bool new_player_spot(player_type *creature_ptr)
182 POSITION y = 0, x = 0;
183 int max_attempts = 10000;
188 while (max_attempts--)
190 /* Pick a legal spot */
191 y = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->height - 2);
192 x = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->width - 2);
194 g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
196 /* Must be a "naked" floor grid */
197 if (g_ptr->m_idx) continue;
198 if (creature_ptr->current_floor_ptr->dun_level)
200 f_ptr = &f_info[g_ptr->feat];
202 if (max_attempts > 5000) /* Rule 1 */
204 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
208 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
209 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
212 /* Refuse to start on anti-teleport grids in dungeon */
213 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
215 if (!player_can_enter(creature_ptr, g_ptr->feat, 0)) continue;
216 if (!in_bounds(creature_ptr->current_floor_ptr, y, x)) continue;
218 /* Refuse to start on anti-teleport grids */
219 if (g_ptr->info & (CAVE_ICKY)) continue;
224 if (max_attempts < 1) /* Should be -1, actually if we failed... */
227 /* Save the new player grid */
235 * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
236 * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
239 void place_bound_perm_wall(player_type *player_ptr, grid_type *g_ptr)
241 if (bound_walls_perm)
243 /* Clear boundary mimic */
248 feature_type *f_ptr = &f_info[g_ptr->feat];
250 /* Hack -- Decline boundary walls with known treasure */
251 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
252 !have_flag(f_ptr->flags, FF_SECRET))
253 g_ptr->feat = feat_state(player_ptr, g_ptr->feat, FF_ENSECRET);
255 /* Set boundary mimic */
256 g_ptr->mimic = g_ptr->feat;
259 /* Add "solid" perma-wall */
260 place_grid(player_ptr, g_ptr, GB_SOLID_PERM);
264 * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
265 * @param player_ptr プレーヤーへの参照ポインタ
266 * @param g_ptr マス構造体の参照ポインタ
267 * @return 看破済みの罠があるならTRUEを返す。
269 bool is_known_trap(player_type *player_ptr, grid_type *g_ptr)
271 if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
272 is_trap(player_ptr, g_ptr->feat)) return TRUE;
280 * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
281 * @param player_ptr プレーヤーへの参照ポインタ
282 * @param g_ptr マス構造体の参照ポインタ
283 * @return 隠されたドアがあるならTRUEを返す。
285 bool is_hidden_door(player_type *player_ptr, grid_type *g_ptr)
287 if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
288 is_closed_door(player_ptr, g_ptr->feat))
294 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
298 * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
301 * @return 指定された座標に照明がかかっているならTRUEを返す。。
303 bool check_local_illumination(player_type *creature_ptr, POSITION y, POSITION x)
305 /* Hack -- move towards player */
306 POSITION yy = (y < creature_ptr->y) ? (y + 1) : (y > creature_ptr->y) ? (y - 1) : y;
307 POSITION xx = (x < creature_ptr->x) ? (x + 1) : (x > creature_ptr->x) ? (x - 1) : x;
309 /* Check for "local" illumination */
311 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
313 /* Check for "complex" illumination */
314 if ((feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[yy][xx])) &&
315 (creature_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
316 (feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[y][xx])) &&
317 (creature_ptr->current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
318 (feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[yy][x])) &&
319 (creature_ptr->current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
325 #else /* COMPLEX_WALL_ILLUMINATION */
327 /* Check for "simple" illumination */
328 return (creature_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
330 #endif /* COMPLEX_WALL_ILLUMINATION */
334 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
335 #define update_local_illumination_aux(C, Y, X) \
337 if (player_has_los_bold((C), (Y), (X))) \
339 /* Update the monster */ \
340 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); \
342 /* Notice and redraw */ \
343 note_spot((C), (Y), (X)); \
344 lite_spot((C), (Y), (X)); \
349 * @brief 指定された座標の照明状態を更新する / Update "local" illumination
350 * @param creature_ptr 視界元のクリーチャー
355 void update_local_illumination(player_type * creature_ptr, POSITION y, POSITION x)
360 if (!in_bounds(creature_ptr->current_floor_ptr, y, x)) return;
362 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
364 if ((y != creature_ptr->y) && (x != creature_ptr->x))
366 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
367 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
368 update_local_illumination_aux(creature_ptr, yy, xx);
369 update_local_illumination_aux(creature_ptr, y, xx);
370 update_local_illumination_aux(creature_ptr, yy, x);
372 else if (x != creature_ptr->x) /* y == creature_ptr->y */
374 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
375 for (i = -1; i <= 1; i++)
378 update_local_illumination_aux(creature_ptr, yy, xx);
381 update_local_illumination_aux(creature_ptr, yy, x);
383 update_local_illumination_aux(creature_ptr, yy, x);
385 else if (y != creature_ptr->y) /* x == creature_ptr->x */
387 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
388 for (i = -1; i <= 1; i++)
391 update_local_illumination_aux(creature_ptr, yy, xx);
394 update_local_illumination_aux(creature_ptr, y, xx);
396 update_local_illumination_aux(creature_ptr, y, xx);
398 else /* Player's grid */
400 for (i = 0; i < 8; i++)
404 update_local_illumination_aux(creature_ptr, yy, xx);
408 #else /* COMPLEX_WALL_ILLUMINATION */
410 if ((y != creature_ptr->y) && (x != creature_ptr->x))
412 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
413 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
414 update_local_illumination_aux(creature_ptr, yy, xx);
416 else if (x != creature_ptr->x) /* y == creature_ptr->y */
418 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
419 for (i = -1; i <= 1; i++)
422 update_local_illumination_aux(creature_ptr, yy, xx);
425 else if (y != creature_ptr->y) /* x == creature_ptr->x */
427 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
428 for (i = -1; i <= 1; i++)
431 update_local_illumination_aux(creature_ptr, yy, xx);
434 else /* Player's grid */
436 for (i = 0; i < 8; i++)
440 update_local_illumination_aux(creature_ptr, yy, xx);
444 #endif /* COMPLEX_WALL_ILLUMINATION */
449 * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
450 * @return 視覚に収められていないならTRUEを返す
451 * @details player_can_see_bold()関数の返り値の否定を返している。
453 bool no_lite(player_type *creature_ptr)
455 return (!player_can_see_bold(creature_ptr, creature_ptr->y, creature_ptr->x));
459 * Place an attr/char pair at the given map coordinate, if legal.
461 void print_rel(player_type *subject_ptr, SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
463 /* Only do "legal" locations */
464 if (panel_contains(y, x))
466 /* Hack -- fake monochrome */
469 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
470 else if (IS_INVULN(subject_ptr) || subject_ptr->timewalk) a = TERM_WHITE;
471 else if (subject_ptr->wraith_form) a = TERM_L_DARK;
474 /* Draw the char using the attr */
475 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
481 * todo ここにplayer_type を追加した時のコンパイルエラーに対処できなかったので保留
482 * Memorize interesting viewable object/features in the given grid
484 * This function should only be called on "legal" grids.
486 * This function will memorize the object and/or feature in the given
487 * grid, if they are (1) viewable and (2) interesting. Note that all
488 * objects are interesting, all terrain features except floors (and
489 * invisible traps) are interesting, and floors (and invisible traps)
490 * are interesting sometimes (depending on various options involving
491 * the illumination of floor grids).
493 * The automatic memorization of all objects and non-floor terrain
494 * features as soon as they are displayed allows incredible amounts
495 * of optimization in various places, especially "map_info()".
497 * Note that the memorization of objects is completely separate from
498 * the memorization of terrain features, preventing annoying floor
499 * memorization when a detected object is picked up from a dark floor,
500 * and object memorization when an object is dropped into a floor grid
501 * which is memorized but out-of-sight.
503 * This function should be called every time the "memorization" of
504 * a grid (or the object in a grid) is called into question, such
505 * as when an object is created in a grid, when a terrain feature
506 * "changes" from "floor" to "non-floor", when any grid becomes
507 * "illuminated" or "viewable", and when a "floor" grid becomes
510 * Note the relatively efficient use of this function by the various
511 * "update_view()" and "update_lite()" calls, to allow objects and
512 * terrain features to be memorized (and drawn) whenever they become
513 * viewable or illuminated in any way, but not when they "maintain"
514 * or "lose" their previous viewability or illumination.
516 * Note the butchered "internal" version of "player_can_see_bold()",
517 * optimized primarily for the most common cases, that is, for the
518 * non-marked floor grids.
520 void note_spot(player_type *player_ptr, POSITION y, POSITION x)
522 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
523 OBJECT_IDX this_o_idx, next_o_idx = 0;
525 /* Blind players see nothing */
526 if (player_ptr->blind) return;
528 /* Analyze non-torch-lit grids */
529 if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
531 /* Require line of sight to the grid */
532 if (!(g_ptr->info & (CAVE_VIEW))) return;
534 /* Require "perma-lite" of the grid */
535 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
538 if (!player_ptr->see_nocto) return;
543 /* Hack -- memorize objects */
544 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
546 object_type *o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
547 next_o_idx = o_ptr->next_o_idx;
549 /* Memorize objects */
550 o_ptr->marked |= OM_FOUND;
554 /* Hack -- memorize grids */
555 if (!(g_ptr->info & (CAVE_MARK)))
557 /* Feature code (applying "mimic" field) */
558 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
560 /* Memorize some "boring" grids */
561 if (!have_flag(f_ptr->flags, FF_REMEMBER))
563 /* Option -- memorize all torch-lit floors */
564 if (view_torch_grids &&
565 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || player_ptr->see_nocto))
567 g_ptr->info |= (CAVE_MARK);
570 /* Option -- memorize all perma-lit floors */
571 else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
573 g_ptr->info |= (CAVE_MARK);
577 /* Memorize normal grids */
578 else if (have_flag(f_ptr->flags, FF_LOS))
580 g_ptr->info |= (CAVE_MARK);
583 /* Memorize torch-lit walls */
584 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
586 g_ptr->info |= (CAVE_MARK);
589 /* Memorize walls seen by noctovision of Ninja */
590 else if (player_ptr->see_nocto)
592 g_ptr->info |= (CAVE_MARK);
595 /* Memorize certain non-torch-lit wall grids */
596 else if (check_local_illumination(player_ptr, y, x))
598 g_ptr->info |= (CAVE_MARK);
602 /* Memorize terrain of the grid */
603 g_ptr->info |= (CAVE_KNOWN);
607 * Redraw (on the screen) a given MAP location
609 * This function should only be called on "legal" grids
611 void lite_spot(player_type *player_ptr, POSITION y, POSITION x)
613 /* Redraw if on screen */
614 if (panel_contains(y, x) && in_bounds2(player_ptr->current_floor_ptr, y, x))
621 map_info(player_ptr, y, x, &a, &c, &ta, &tc);
623 /* Hack -- fake monochrome */
626 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
627 else if (IS_INVULN(player_ptr) || player_ptr->timewalk) a = TERM_WHITE;
628 else if (player_ptr->wraith_form) a = TERM_L_DARK;
631 /* Hack -- Queue it */
632 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
634 /* Update sub-windows */
635 player_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
640 * Some comments on the grid flags. -BEN-
643 * One of the major bottlenecks in previous versions of Angband was in
644 * the calculation of "line of sight" from the player to various grids,
645 * such as monsters. This was such a nasty bottleneck that a lot of
646 * silly things were done to reduce the dependancy on "line of sight",
647 * for example, you could not "see" any grids in a lit room until you
648 * actually entered the room, and there were all kinds of bizarre grid
649 * flags to enable this behavior. This is also why the "call light"
650 * spells always lit an entire room.
652 * The code below provides functions to calculate the "field of view"
653 * for the player, which, once calculated, provides extremely fast
654 * calculation of "line of sight from the player", and to calculate
655 * the "field of torch lite", which, again, once calculated, provides
656 * extremely fast calculation of "which grids are lit by the player's
657 * lite source". In addition to marking grids as "GRID_VIEW" and/or
658 * "GRID_LITE", as appropriate, these functions maintain an array for
659 * each of these two flags, each array containing the locations of all
660 * of the grids marked with the appropriate flag, which can be used to
661 * very quickly scan through all of the grids in a given set.
663 * To allow more "semantically valid" field of view semantics, whenever
664 * the field of view (or the set of torch lit grids) changes, all of the
665 * grids in the field of view (or the set of torch lit grids) are "drawn"
666 * so that changes in the world will become apparent as soon as possible.
667 * This has been optimized so that only grids which actually "change" are
668 * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
669 * of the grids which are entering or leaving the relevent set of grids.
671 * These new methods are so efficient that the old nasty code was removed.
673 * Note that there is no reason to "update" the "viewable space" unless
674 * the player "moves", or walls/doors are created/destroyed, and there
675 * is no reason to "update" the "torch lit grids" unless the field of
676 * view changes, or the "light radius" changes. This means that when
677 * the player is resting, or digging, or doing anything that does not
678 * involve movement or changing the state of the dungeon, there is no
679 * need to update the "view" or the "lite" regions, which is nice.
681 * Note that the calls to the nasty "los()" function have been reduced
682 * to a bare minimum by the use of the new "field of view" calculations.
684 * I wouldn't be surprised if slight modifications to the "update_view()"
685 * function would allow us to determine "reverse line-of-sight" as well
686 * as "normal line-of-sight", which would allow monsters to use a more
687 * "correct" calculation to determine if they can "see" the player. For
688 * now, monsters simply "cheat" somewhat and assume that if the player
689 * has "line of sight" to the monster, then the monster can "pretend"
690 * that it has "line of sight" to the player.
693 * The "update_lite()" function maintains the "CAVE_LITE" flag for each
694 * grid and maintains an array of all "CAVE_LITE" grids.
696 * This set of grids is the complete set of all grids which are lit by
697 * the players light source, which allows the "player_can_see_bold()"
698 * function to work very quickly.
700 * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
701 * fact, the player (unless blind) can always "see" all grids which are
702 * marked as "CAVE_LITE", unless they are "off screen".
705 * The "update_view()" function maintains the "CAVE_VIEW" flag for each
706 * grid and maintains an array of all "CAVE_VIEW" grids.
708 * This set of grids is the complete set of all grids within line of sight
709 * of the player, allowing the "player_has_los_bold()" macro to work very
713 * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
714 * temporary internal flag to mark those grids which are not only in view,
715 * but which are also "easily" in line of sight of the player. This flag
716 * is always cleared when we are done.
719 * The current "update_lite()" and "update_view()" algorithms use the
720 * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
721 * to keep track of which grids were previously marked as "CAVE_LITE" or
722 * "CAVE_VIEW", which allows us to optimize the "screen updates".
724 * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
725 * for various other purposes, such as spreading lite or darkness during
726 * "lite_room()" / "unlite_room()", and for calculating monster flow.
729 * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
730 * in some way permanently lit. However, for the player to "see" anything
731 * in the grid, as determined by "player_can_see()", the player must not be
732 * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
733 * grids, even if marked as "perma lit", are only illuminated if they touch
734 * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
737 * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
738 * that even if the player cannot "see" the grid, he "knows" the terrain in
739 * that grid. This is used to "remember" walls/doors/stairs/floors when they
740 * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
741 * or when one of the "memorize floor grids" options induces memorization.
743 * Objects are "memorized" in a different way, using a special "marked" flag
744 * on the object itself, which is set when an object is observed or detected.
747 * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
748 * and should be illuminated by "lite room" and "darkness" spells.
751 * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
752 * and should be unavailable for "teleportation" destinations.
755 * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
756 * which is observed, and the "view_torch_grids" allows the player to memorize
757 * every torch-lit grid. The player will always memorize important walls,
758 * doors, stairs, and other terrain features, as well as any "detected" grids.
760 * Note that the new "update_view()" method allows, among other things, a room
761 * to be "partially" seen as the player approaches it, with a growing cone of
762 * floor appearing as the player gets closer to the door. Also, by not turning
763 * on the "memorize perma-lit grids" option, the player will only "see" those
764 * floor grids which are actually in line of sight.
766 * And my favorite "plus" is that you can now use a special option to draw the
767 * "floors" in the "viewable region" brightly (actually, to draw the *other*
768 * grids dimly), providing a "pretty" effect as the player runs around, and
769 * to efficiently display the "torch lite" in a special color.
772 * Some comments on the "update_view()" algorithm...
774 * The algorithm is very fast, since it spreads "obvious" grids very quickly,
775 * and only has to call "los()" on the borderline cases. The major axes/diags
776 * even terminate early when they hit walls. I need to find a quick way
777 * to "terminate" the other scans.
779 * Note that in the worst case (a big empty area with say 5% scattered walls),
780 * each of the 1500 or so nearby grids is checked once, most of them getting
781 * an "instant" rating, and only a small portion requiring a call to "los()".
783 * The only time that the algorithm appears to be "noticeably" too slow is
784 * when running, and this is usually only important in town, since the town
785 * provides about the worst scenario possible, with large open regions and
786 * a few scattered obstructions. There is a special "efficiency" option to
787 * allow the player to reduce his field of view in town, if needed.
789 * In the "best" case (say, a normal stretch of corridor), the algorithm
790 * makes one check for each viewable grid, and makes no calls to "los()".
791 * So running in corridors is very fast, and if a lot of monsters are
792 * nearby, it is much faster than the old methods.
794 * Note that resting, most normal commands, and several forms of running,
795 * plus all commands executed near large groups of monsters, are strictly
796 * more efficient with "update_view()" that with the old "compute los() on
797 * demand" method, primarily because once the "field of view" has been
798 * calculated, it does not have to be recalculated until the player moves
799 * (or a wall or door is created or destroyed).
801 * Note that we no longer have to do as many "los()" checks, since once the
802 * "view" region has been built, very few things cause it to be "changed"
803 * (player movement, and the opening/closing of doors, changes in wall status).
804 * Note that door/wall changes are only relevant when the door/wall itself is
805 * in the "view" region.
807 * The algorithm seems to only call "los()" from zero to ten times, usually
808 * only when coming down a corridor into a room, or standing in a room, just
809 * misaligned with a corridor. So if, say, there are five "nearby" monsters,
810 * we will be reducing the calls to "los()".
812 * I am thinking in terms of an algorithm that "walks" from the central point
813 * out to the maximal "distance", at each point, determining the "view" code
814 * (above). For each grid not on a major axis or diagonal, the "view" code
815 * depends on the "cave_los_bold()" and "view" of exactly two other grids
816 * (the one along the nearest diagonal, and the one next to that one, see
817 * "update_view_aux()"...).
819 * We "memorize" the viewable space array, so that at the cost of under 3000
820 * bytes, we reduce the time taken by "forget_view()" to one assignment for
821 * each grid actually in the "viewable space". And for another 3000 bytes,
822 * we prevent "erase + redraw" ineffiencies via the "seen" set. These bytes
823 * are also used by other routines, thus reducing the cost to almost nothing.
825 * A similar thing is done for "forget_lite()" in which case the savings are
826 * much less, but save us from doing bizarre maintenance checking.
828 * In the worst "normal" case (in the middle of the town), the reachable space
829 * actually reaches to more than half of the largest possible "circle" of view,
830 * or about 800 grids, and in the worse case (in the middle of a dungeon level
831 * where all the walls have been removed), the reachable space actually reaches
832 * the theoretical maximum size of just under 1500 grids.
834 * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
835 * If G1 is lite, G is lite. Else if G2 is lite, G is half. Else if G1 and G2
836 * are both half, G is half. Else G is dark. It only takes 2 (or 4) bits to
837 * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
838 * entire possible space (including initialization) in one step per grid. If
839 * we do the "clearing" as a separate step (and use an array of "view" grids),
840 * then the clearing will take as many steps as grids that were viewed, and the
841 * algorithm will be able to "stop" scanning at various points.
842 * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
846 * Hack - speed up the update_flow algorithm by only doing
847 * it everytime the player moves out of LOS of the last
850 static POSITION flow_x = 0;
851 static POSITION flow_y = 0;
854 * Hack -- fill in the "cost" field of every grid that the player
855 * can "reach" with the number of steps needed to reach that grid.
856 * This also yields the "distance" of the player from every grid.
858 * In addition, mark the "when" of the grids that can reach
859 * the player with the incremented value of "flow_n".
861 * Hack -- use the "seen" array as a "circular queue".
863 * We do not need a priority queue because the cost from grid
864 * to grid is always "one" and we process them in order.
866 void update_flow(player_type *subject_ptr)
873 /* Paranoia -- make sure the array is empty */
874 if (tmp_pos.n) return;
876 /* The last way-point is on the map */
877 if (subject_ptr->running && in_bounds(subject_ptr->current_floor_ptr, flow_y, flow_x))
879 /* The way point is in sight - do not update. (Speedup) */
880 if (subject_ptr->current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
883 /* Erase all of the current flow information */
884 for (y = 0; y < subject_ptr->current_floor_ptr->height; y++)
886 for (x = 0; x < subject_ptr->current_floor_ptr->width; x++)
888 subject_ptr->current_floor_ptr->grid_array[y][x].cost = 0;
889 subject_ptr->current_floor_ptr->grid_array[y][x].dist = 0;
893 /* Save player position */
894 flow_y = subject_ptr->y;
895 flow_x = subject_ptr->x;
897 /* Add the player's grid to the queue */
898 tmp_pos.y[0] = subject_ptr->y;
899 tmp_pos.x[0] = subject_ptr->x;
901 /* Now process the queue */
902 while (flow_head != flow_tail)
906 /* Extract the next entry */
907 ty = tmp_pos.y[flow_tail];
908 tx = tmp_pos.x[flow_tail];
910 /* Forget that entry */
911 if (++flow_tail == TEMP_MAX) flow_tail = 0;
913 /* Add the "children" */
914 for (d = 0; d < 8; d++)
916 int old_head = flow_head;
917 byte m = subject_ptr->current_floor_ptr->grid_array[ty][tx].cost + 1;
918 byte n = subject_ptr->current_floor_ptr->grid_array[ty][tx].dist + 1;
925 /* Ignore player's grid */
926 if (player_bold(subject_ptr, y, x)) continue;
928 g_ptr = &subject_ptr->current_floor_ptr->grid_array[y][x];
930 if (is_closed_door(subject_ptr, g_ptr->feat)) m += 3;
932 /* Ignore "pre-stamped" entries */
933 if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
935 /* Ignore "walls" and "rubble" */
936 if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(subject_ptr, g_ptr->feat)) continue;
938 /* Save the flow cost */
939 if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
940 if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
942 /* Hack -- limit flow depth */
943 if (n == MONSTER_FLOW_DEPTH) continue;
945 /* Enqueue that entry */
946 tmp_pos.y[flow_head] = y;
947 tmp_pos.x[flow_head] = x;
949 /* Advance the queue */
950 if (++flow_head == TEMP_MAX) flow_head = 0;
952 /* Hack -- notice overflow by forgetting new entry */
953 if (flow_head == flow_tail) flow_head = old_head;
959 * Take a feature, determine what that feature becomes
960 * through applying the given action.
962 FEAT_IDX feat_state(player_type *player_ptr, FEAT_IDX feat, int action)
964 feature_type *f_ptr = &f_info[feat];
967 /* Get the new feature */
968 floor_type *floor_ptr = player_ptr->current_floor_ptr;
969 for (i = 0; i < MAX_FEAT_STATES; i++)
971 if (f_ptr->state[i].action == action) return conv_dungeon_feat(floor_ptr, f_ptr->state[i].result);
974 if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
976 return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(floor_ptr, f_ptr->destroyed) : feat;
980 * Takes a location and action and changes the feature at that
981 * location through applying the given action.
983 void cave_alter_feat(player_type *player_ptr, POSITION y, POSITION x, int action)
985 /* Set old feature */
986 floor_type *floor_ptr = player_ptr->current_floor_ptr;
987 FEAT_IDX oldfeat = floor_ptr->grid_array[y][x].feat;
989 /* Get the new feat */
990 FEAT_IDX newfeat = feat_state(player_ptr, oldfeat, action);
993 if (newfeat == oldfeat) return;
995 /* Set the new feature */
996 cave_set_feat(player_ptr, y, x, newfeat);
998 if (!(feature_action_flags[action] & FAF_NO_DROP))
1000 feature_type *old_f_ptr = &f_info[oldfeat];
1001 feature_type *f_ptr = &f_info[newfeat];
1005 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
1007 /* Place some gold */
1008 place_gold(player_ptr, y, x);
1013 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - floor_ptr->dun_level / 2)))
1016 place_object(player_ptr, y, x, 0L);
1020 if (found && current_world_ptr->character_dungeon && player_can_see_bold(player_ptr, y, x))
1022 msg_print(_("何かを発見した!", "You have found something!"));
1026 if (feature_action_flags[action] & FAF_CRASH_GLASS)
1028 feature_type *old_f_ptr = &f_info[oldfeat];
1030 if (have_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon)
1032 project(player_ptr, PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(floor_ptr->dun_level, 100) / 4, GF_SHARDS,
1033 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1039 /* Remove a mirror */
1040 void remove_mirror(player_type *caster_ptr, POSITION y, POSITION x)
1042 grid_type *g_ptr = &caster_ptr->current_floor_ptr->grid_array[y][x];
1044 /* Remove the mirror */
1045 g_ptr->info &= ~(CAVE_OBJECT);
1048 if (d_info[caster_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
1050 g_ptr->info &= ~(CAVE_GLOW);
1051 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1052 if (g_ptr->m_idx) update_monster(caster_ptr, g_ptr->m_idx, FALSE);
1054 update_local_illumination(caster_ptr, y, x);
1057 note_spot(caster_ptr, y, x);
1059 lite_spot(caster_ptr, y, x);
1064 * Return TRUE if there is a mirror on the grid.
1066 bool is_mirror_grid(grid_type *g_ptr)
1068 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
1076 * Return TRUE if there is a mirror on the grid.
1078 bool is_glyph_grid(grid_type *g_ptr)
1080 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
1088 * Return TRUE if there is a mirror on the grid.
1090 bool is_explosive_rune_grid(grid_type *g_ptr)
1092 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
1099 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
1100 * @param player_ptr プレーヤーへの参照ポインタ
1101 * @param m_idx モンスターID
1105 * @return テレポート先として妥当ならばtrue
1107 bool cave_monster_teleportable_bold(player_type *player_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, teleport_flags mode)
1109 monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
1110 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1111 feature_type *f_ptr = &f_info[g_ptr->feat];
1113 /* Require "teleportable" space */
1114 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1116 if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
1117 if (player_bold(player_ptr, y, x)) return FALSE;
1119 /* Hack -- no teleport onto glyph of warding */
1120 if (is_glyph_grid(g_ptr)) return FALSE;
1121 if (is_explosive_rune_grid(g_ptr)) return FALSE;
1123 if (!(mode & TELEPORT_PASSIVE))
1125 if (!monster_can_cross_terrain(player_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
1132 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
1133 * @param player_ptr プレーヤーへの参照ポインタ
1137 * @return テレポート先として妥当ならばtrue
1139 bool cave_player_teleportable_bold(player_type *player_ptr, POSITION y, POSITION x, teleport_flags mode)
1141 grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1142 feature_type *f_ptr = &f_info[g_ptr->feat];
1144 /* Require "teleportable" space */
1145 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1147 /* No magical teleporting into vaults and such */
1148 if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
1150 if (g_ptr->m_idx && (g_ptr->m_idx != player_ptr->riding)) return FALSE;
1152 /* don't teleport on a trap. */
1153 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
1155 if (!(mode & TELEPORT_PASSIVE))
1157 if (!player_can_enter(player_ptr, g_ptr->feat, 0)) return FALSE;
1159 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
1161 if (!player_ptr->levitation && !player_ptr->can_swim) return FALSE;
1164 if (have_flag(f_ptr->flags, FF_LAVA) && !player_ptr->immune_fire && !IS_INVULN(player_ptr))
1166 /* Always forbid deep lava */
1167 if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
1169 /* Forbid shallow lava when the player don't have levitation */
1170 if (!player_ptr->levitation) return FALSE;
1179 * @brief 地形は開くものであって、かつ開かれているかを返す /
1180 * Attempt to open the given chest at the given location
1182 * @return 開いた地形である場合TRUEを返す / Return TRUE if the given feature is an open door
1184 bool is_open(player_type *player_ptr, FEAT_IDX feat)
1186 return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(player_ptr, feat, FF_CLOSE));
1190 * @brief プレイヤーが地形踏破可能かを返す
1191 * @param feature 判定したい地形ID
1192 * @param mode 移動に関するオプションフラグ
1193 * @return 移動可能ならばTRUEを返す
1195 bool player_can_enter(player_type *creature_ptr, FEAT_IDX feature, BIT_FLAGS16 mode)
1197 feature_type *f_ptr = &f_info[feature];
1199 if (creature_ptr->riding) return monster_can_cross_terrain(creature_ptr, feature, &r_info[creature_ptr->current_floor_ptr->m_list[creature_ptr->riding].r_idx], mode | CEM_RIDING);
1201 if (have_flag(f_ptr->flags, FF_PATTERN))
1203 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
1206 if (have_flag(f_ptr->flags, FF_CAN_FLY) && creature_ptr->levitation) return TRUE;
1207 if (have_flag(f_ptr->flags, FF_CAN_SWIM) && creature_ptr->can_swim) return TRUE;
1208 if (have_flag(f_ptr->flags, FF_CAN_PASS) && creature_ptr->pass_wall) return TRUE;
1210 if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;
1216 void place_grid(player_type *player_ptr, grid_type *g_ptr, grid_bold_type gb_type)
1222 g_ptr->feat = feat_ground_type[randint0(100)];
1223 g_ptr->info &= ~(CAVE_MASK);
1224 g_ptr->info |= CAVE_FLOOR;
1229 g_ptr->feat = feat_wall_type[randint0(100)];
1230 g_ptr->info &= ~(CAVE_MASK);
1231 g_ptr->info |= CAVE_EXTRA;
1241 g_ptr->feat = feat_wall_inner;
1242 g_ptr->info &= ~(CAVE_MASK);
1243 g_ptr->info |= CAVE_INNER;
1248 g_ptr->feat = feat_permanent;
1249 g_ptr->info &= ~(CAVE_MASK);
1250 g_ptr->info |= CAVE_INNER;
1255 g_ptr->feat = feat_wall_outer;
1256 g_ptr->info &= ~(CAVE_MASK);
1257 g_ptr->info |= CAVE_OUTER;
1260 case GB_OUTER_NOPERM:
1262 feature_type *f_ptr = &f_info[feat_wall_outer];
1263 if (permanent_wall(f_ptr))
1265 g_ptr->feat = (s16b)feat_state(player_ptr, feat_wall_outer, FF_UNPERM);
1269 g_ptr->feat = feat_wall_outer;
1272 g_ptr->info &= ~(CAVE_MASK);
1273 g_ptr->info |= (CAVE_OUTER | CAVE_VAULT);
1283 g_ptr->feat = feat_permanent;
1284 g_ptr->info &= ~(CAVE_MASK);
1285 g_ptr->info |= CAVE_SOLID;
1288 case GB_SOLID_NOPERM:
1297 if (g_ptr->m_idx > 0) delete_monster_idx(player_ptr, g_ptr->m_idx);
1302 * モンスターにより照明が消されている地形か否かを判定する。 / Is this grid "darkened" by monster?
1303 * @param player_ptr プレーヤーへの参照ポインタ
1304 * @param g_ptr グリッドへの参照ポインタ
1305 * @return 照明が消されている地形ならばTRUE
1307 bool darkened_grid(player_type *player_ptr, grid_type *g_ptr)
1309 return ((g_ptr->info & (CAVE_VIEW | CAVE_LITE | CAVE_MNLT | CAVE_MNDK)) == (CAVE_VIEW | CAVE_MNDK)) &&
1310 !player_ptr->see_nocto;
1314 void place_bold(player_type *player_ptr, POSITION y, POSITION x, grid_bold_type gb_type)
1316 floor_type *floor_ptr = player_ptr->current_floor_ptr;
1321 set_cave_feat(floor_ptr, y, x, feat_ground_type[randint0(100)]);
1322 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1323 add_cave_info(floor_ptr, y, x, CAVE_FLOOR);
1328 set_cave_feat(floor_ptr, y, x, feat_wall_type[randint0(100)]);
1329 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1330 add_cave_info(floor_ptr, y, x, CAVE_EXTRA);
1335 set_cave_feat(floor_ptr, y, x, feat_permanent);
1336 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1337 add_cave_info(floor_ptr, y, x, CAVE_EXTRA);
1342 set_cave_feat(floor_ptr, y, x, feat_wall_inner);
1343 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1344 add_cave_info(floor_ptr, y, x, CAVE_INNER);
1349 set_cave_feat(floor_ptr, y, x, feat_permanent);
1350 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1351 add_cave_info(floor_ptr, y, x, CAVE_INNER);
1356 set_cave_feat(floor_ptr, y, x, feat_wall_outer);
1357 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1358 add_cave_info(floor_ptr, y, x, CAVE_OUTER);
1361 case GB_OUTER_NOPERM:
1363 feature_type *_f_ptr = &f_info[feat_wall_outer];
1364 if (permanent_wall(_f_ptr)) set_cave_feat(floor_ptr, y, x, (s16b)feat_state(player_ptr, feat_wall_outer, FF_UNPERM));
1365 else set_cave_feat(floor_ptr, y, x, feat_wall_outer);
1366 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1367 add_cave_info(floor_ptr, y, x, (CAVE_OUTER | CAVE_VAULT));
1372 set_cave_feat(floor_ptr, y, x, feat_wall_inner);
1373 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1374 add_cave_info(floor_ptr, y, x, CAVE_SOLID);
1379 feature_type *f_ptr = &f_info[feat_wall_solid];
1380 if ((floor_ptr->grid_array[y][x].info & CAVE_VAULT) && permanent_wall(f_ptr))
1381 set_cave_feat(floor_ptr, y, x, feat_state(player_ptr, feat_wall_solid, FF_UNPERM));
1382 else set_cave_feat(floor_ptr, y, x, feat_wall_solid);
1383 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1384 add_cave_info(floor_ptr, y, x, CAVE_SOLID);
1391 delete_monster(player_ptr, y, x);