OSDN Git Service

[Refactor] #37353 TERM_* 定義を term.h へ移動.
[hengband/hengband.git] / src / grid.c
1 
2  /*!
3   * @file grid.c
4   * @brief グリッドの実装 / low level dungeon routines -BEN-
5   * @date 2013/12/30
6   * @author
7   * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
8   *\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
12   * \n
13   * Support for Adam Bolt's tileset, lighting and transparency effects\n
14   * by Robert Ruehlmann (rr9@angband.org)\n
15   * \n
16   * 2013 Deskull Doxygen向けのコメント整理\n
17   */
18
19
20 #include "angband.h"
21 #include "util.h"
22 #include "term.h"
23
24 #include "floor.h"
25 #include "world.h"
26 #include "object-flavor.h"
27 #include "object-hook.h"
28 #include "dungeon.h"
29 #include "floor-generate.h"
30 #include "grid.h"
31 #include "trap.h"
32 #include "rooms.h"
33 #include "monster.h"
34 #include "quest.h"
35 #include "feature.h"
36 #include "monster-status.h"
37 #include "player-status.h"
38 #include "player-effects.h"
39 #include "spells.h"
40 #include "view-mainwindow.h"
41
42
43 /*!
44  * @brief 地形状態フラグテーブル /
45  * The table of features' actions
46  */
47 static const byte feature_action_flags[FF_FLAG_MAX] =
48 {
49         0, /* LOS */
50         0, /* PROJECT */
51         0, /* MOVE */
52         0, /* PLACE */
53         0, /* DROP */
54         0, /* SECRET */
55         0, /* NOTICE */
56         0, /* REMEMBER */
57         0, /* OPEN */
58         0, /* CLOSE */
59         FAF_CRASH_GLASS, /* BASH */
60         0, /* SPIKE */
61         FAF_DESTROY, /* DISARM */
62         0, /* STORE */
63         FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
64         0, /* MAY_HAVE_GOLD */
65         0, /* HAS_GOLD */
66         0, /* HAS_ITEM */
67         0, /* DOOR */
68         0, /* TRAP */
69         0, /* STAIRS */
70         0, /* GLYPH */
71         0, /* LESS */
72         0, /* MORE */
73         0, /* RUN */
74         0, /* FLOOR */
75         0, /* WALL */
76         0, /* PERMANENT */
77         0, /* INNER */
78         0, /* OUTER */
79         0, /* SOLID */
80         0, /* HIT_TRAP */
81
82         0, /* BRIDGE */
83         0, /* RIVER */
84         0, /* LAKE */
85         0, /* BRIDGED */
86         0, /* COVERED */
87         0, /* GLOW */
88         0, /* ENSECRET */
89         0, /* WATER */
90         0, /* LAVA */
91         0, /* SHALLOW */
92         0, /* DEEP */
93         0, /* FILLED */
94         FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
95         0, /* HURT_FIRE */
96         0, /* HURT_COLD */
97         0, /* HURT_ACID */
98         0, /* ICE */
99         0, /* ACID */
100         0, /* OIL */
101         0, /* XXX04 */
102         0, /* CAN_CLIMB */
103         0, /* CAN_FLY */
104         0, /* CAN_SWIM */
105         0, /* CAN_PASS */
106         0, /* CAN_OOZE */
107         0, /* CAN_DIG */
108         0, /* HIDE_ITEM */
109         0, /* HIDE_SNEAK */
110         0, /* HIDE_SWIM */
111         0, /* HIDE_DIG */
112         0, /* KILL_HUGE */
113         0, /* KILL_MOVE */
114
115         0, /* PICK_TRAP */
116         0, /* PICK_DOOR */
117         0, /* ALLOC */
118         0, /* CHEST */
119         0, /* DROP_1D2 */
120         0, /* DROP_2D2 */
121         0, /* DROP_GOOD */
122         0, /* DROP_GREAT */
123         0, /* HURT_POIS */
124         0, /* HURT_ELEC */
125         0, /* HURT_WATER */
126         0, /* HURT_BWATER */
127         0, /* USE_FEAT */
128         0, /* GET_FEAT */
129         0, /* GROUND */
130         0, /* OUTSIDE */
131         0, /* EASY_HIDE */
132         0, /* EASY_CLIMB */
133         0, /* MUST_CLIMB */
134         0, /* TREE */
135         0, /* NEED_TREE */
136         0, /* BLOOD */
137         0, /* DUST */
138         0, /* SLIME */
139         0, /* PLANT */
140         0, /* XXX2 */
141         0, /* INSTANT */
142         0, /* EXPLODE */
143         0, /* TIMED */
144         0, /* ERUPT */
145         0, /* STRIKE */
146         0, /* SPREAD */
147
148         0, /* SPECIAL */
149         FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
150         0, /* QUEST_ENTER */
151         0, /* QUEST_EXIT */
152         0, /* QUEST */
153         0, /* SHAFT */
154         0, /* MOUNTAIN */
155         0, /* BLDG */
156         0, /* MINOR_GLYPH */
157         0, /* PATTERN */
158         0, /* TOWN */
159         0, /* ENTRANCE */
160         0, /* MIRROR */
161         0, /* UNPERM */
162         0, /* TELEPORTABLE */
163         0, /* CONVERT */
164         0, /* GLASS */
165 };
166
167 /*!
168  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
169  * @return 配置に成功したらTRUEを返す
170  */
171 bool new_player_spot(void)
172 {
173         POSITION y = 0, x = 0;
174         int max_attempts = 10000;
175
176         grid_type *g_ptr;
177         feature_type *f_ptr;
178
179         /* Place the player */
180         while (max_attempts--)
181         {
182                 /* Pick a legal spot */
183                 y = (POSITION)rand_range(1, current_floor_ptr->height - 2);
184                 x = (POSITION)rand_range(1, current_floor_ptr->width - 2);
185
186                 g_ptr = &current_floor_ptr->grid_array[y][x];
187
188                 /* Must be a "naked" floor grid */
189                 if (g_ptr->m_idx) continue;
190                 if (current_floor_ptr->dun_level)
191                 {
192                         f_ptr = &f_info[g_ptr->feat];
193
194                         if (max_attempts > 5000) /* Rule 1 */
195                         {
196                                 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
197                         }
198                         else /* Rule 2 */
199                         {
200                                 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
201                                 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
202                         }
203
204                         /* Refuse to start on anti-teleport grids in dungeon */
205                         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
206                 }
207                 if (!player_can_enter(g_ptr->feat, 0)) continue;
208                 if (!in_bounds(y, x)) continue;
209
210                 /* Refuse to start on anti-teleport grids */
211                 if (g_ptr->info & (CAVE_ICKY)) continue;
212
213                 break;
214         }
215
216         if (max_attempts < 1) /* Should be -1, actually if we failed... */
217                 return FALSE;
218
219         /* Save the new player grid */
220         p_ptr->y = y;
221         p_ptr->x = x;
222
223         return TRUE;
224 }
225
226
227
228 /*!
229  * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
230  * @param y 配置を試みたいマスのY座標
231  * @param x 配置を試みたいマスのX座標
232  * @return なし
233  */
234 void place_random_stairs(POSITION y, POSITION x)
235 {
236         bool up_stairs = TRUE;
237         bool down_stairs = TRUE;
238         grid_type *g_ptr;
239         g_ptr = &current_floor_ptr->grid_array[y][x];
240         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) return;
241
242         /* Town */
243         if (!current_floor_ptr->dun_level) up_stairs = FALSE;
244
245         /* Ironman */
246         if (ironman_downward) up_stairs = FALSE;
247
248         /* Bottom */
249         if (current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth) down_stairs = FALSE;
250
251         /* Quest-level */
252         if (quest_number(current_floor_ptr->dun_level) && (current_floor_ptr->dun_level > 1)) down_stairs = FALSE;
253
254         /* We can't place both */
255         if (down_stairs && up_stairs)
256         {
257                 /* Choose a staircase randomly */
258                 if (randint0(100) < 50) up_stairs = FALSE;
259                 else down_stairs = FALSE;
260         }
261
262         /* Place the stairs */
263         if (up_stairs) place_up_stairs(y, x);
264         else if (down_stairs) place_down_stairs(y, x);
265 }
266
267 /*!
268  * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
269  * @param y ドアの配置を試みたいマスのY座標
270  * @param x ドアの配置を試みたいマスのX座標
271  * @param room 部屋に接している場合向けのドア生成か否か
272  * @return なし
273  */
274 void place_random_door(POSITION y, POSITION x, bool room)
275 {
276         int tmp, type;
277         FEAT_IDX feat = feat_none;
278         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
279
280         /* Initialize mimic info */
281         g_ptr->mimic = 0;
282
283         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
284         {
285                 place_floor_bold(y, x);
286                 return;
287         }
288
289         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
290                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
291                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
292
293         /* Choose an object */
294         tmp = randint0(1000);
295
296         /* Open doors (300/1000) */
297         if (tmp < 300)
298         {
299                 /* Create open door */
300                 feat = feat_door[type].open;
301         }
302
303         /* Broken doors (100/1000) */
304         else if (tmp < 400)
305         {
306                 /* Create broken door */
307                 feat = feat_door[type].broken;
308         }
309
310         /* Secret doors (200/1000) */
311         else if (tmp < 600)
312         {
313                 /* Create secret door */
314                 place_closed_door(y, x, type);
315
316                 if (type != DOOR_CURTAIN)
317                 {
318                         /* Hide. If on the edge of room, use outer wall. */
319                         g_ptr->mimic = room ? feat_wall_outer : feat_wall_type[randint0(100)];
320
321                         /* Floor type terrain cannot hide a door */
322                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
323                         {
324                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
325                                 {
326                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
327                                 }
328                                 g_ptr->mimic = 0;
329                         }
330                 }
331         }
332
333         /* Closed, locked, or stuck doors (400/1000) */
334         else place_closed_door(y, x, type);
335
336         if (tmp < 400)
337         {
338                 if (feat != feat_none)
339                 {
340                         set_cave_feat(y, x, feat);
341                 }
342                 else
343                 {
344                         place_floor_bold(y, x);
345                 }
346         }
347
348         delete_monster(y, x);
349 }
350
351 /*!
352  * @brief 所定の位置に各種の閉じたドアを配置する / Place a random type of normal door at the given location.
353  * @param y ドアの配置を試みたいマスのY座標
354  * @param x ドアの配置を試みたいマスのX座標
355  * @param type ドアの地形ID
356  * @return なし
357  */
358 void place_closed_door(POSITION y, POSITION x, int type)
359 {
360         int tmp;
361         FEAT_IDX feat = feat_none;
362
363         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
364         {
365                 place_floor_bold(y, x);
366                 return;
367         }
368
369         /* Choose an object */
370         tmp = randint0(400);
371
372         /* Closed doors (300/400) */
373         if (tmp < 300)
374         {
375                 /* Create closed door */
376                 feat = feat_door[type].closed;
377         }
378
379         /* Locked doors (99/400) */
380         else if (tmp < 399)
381         {
382                 /* Create locked door */
383                 feat = feat_locked_door_random(type);
384         }
385
386         /* Stuck doors (1/400) */
387         else
388         {
389                 /* Create jammed door */
390                 feat = feat_jammed_door_random(type);
391         }
392
393         if (feat != feat_none)
394         {
395                 cave_set_feat(y, x, feat);
396
397                 /* Now it is not floor */
398                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
399         }
400         else
401         {
402                 place_floor_bold(y, x);
403         }
404 }
405
406 /*!
407 * @brief 鍵のかかったドアを配置する
408 * @param y 配置したいフロアのY座標
409 * @param x 配置したいフロアのX座標
410 * @return なし
411 */
412 void place_locked_door(POSITION y, POSITION x)
413 {
414         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
415         {
416                 place_floor_bold(y, x);
417         }
418         else
419         {
420                 set_cave_feat(y, x, feat_locked_door_random((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
421                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
422                 delete_monster(y, x);
423         }
424 }
425
426
427 /*!
428 * @brief 隠しドアを配置する
429 * @param y 配置したいフロアのY座標
430 * @param x 配置したいフロアのX座標
431 * @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
432 * @return なし
433 */
434 void place_secret_door(POSITION y, POSITION x, int type)
435 {
436         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
437         {
438                 place_floor_bold(y, x);
439         }
440         else
441         {
442                 grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
443
444                 if (type == DOOR_DEFAULT)
445                 {
446                         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
447                                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
448                                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
449                 }
450
451                 /* Create secret door */
452                 place_closed_door(y, x, type);
453
454                 if (type != DOOR_CURTAIN)
455                 {
456                         /* Hide by inner wall because this is used in rooms only */
457                         g_ptr->mimic = feat_wall_inner;
458
459                         /* Floor type terrain cannot hide a door */
460                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
461                         {
462                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
463                                 {
464                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
465                                 }
466                                 g_ptr->mimic = 0;
467                         }
468                 }
469
470                 g_ptr->info &= ~(CAVE_FLOOR);
471                 delete_monster(y, x);
472         }
473 }
474
475 /*
476  * Routine used by the random vault creators to add a door to a location
477  * Note that range checking has to be done in the calling routine.
478  *
479  * The doors must be INSIDE the allocated region.
480  */
481 void add_door(POSITION x, POSITION y)
482 {
483         /* Need to have a wall in the center square */
484         if (!is_outer_bold(y, x)) return;
485
486         /* look at:
487         *  x#x
488         *  .#.
489         *  x#x
490         *
491         *  where x=don't care
492         *  .=floor, #=wall
493         */
494
495         if (is_floor_bold(y - 1, x) && is_floor_bold(y + 1, x) &&
496                 (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
497         {
498                 /* secret door */
499                 place_secret_door(y, x, DOOR_DEFAULT);
500
501                 /* set boundarys so don't get wide doors */
502                 place_solid_bold(y, x - 1);
503                 place_solid_bold(y, x + 1);
504         }
505
506
507         /* look at:
508         *  x#x
509         *  .#.
510         *  x#x
511         *
512         *  where x = don't care
513         *  .=floor, #=wall
514         */
515         if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
516                 is_floor_bold(y, x - 1) && is_floor_bold(y, x + 1))
517         {
518                 /* secret door */
519                 place_secret_door(y, x, DOOR_DEFAULT);
520
521                 /* set boundarys so don't get wide doors */
522                 place_solid_bold(y - 1, x);
523                 place_solid_bold(y + 1, x);
524         }
525 }
526
527 /*!
528 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
529 * @param y1 基準となるマスのY座標
530 * @param x1 基準となるマスのX座標
531 * @return 通路の数
532 * @note Assumes "in_bounds(y1, x1)"
533 * @details
534 * XXX XXX This routine currently only counts actual "empty floor"\n
535 * grids which are not in rooms.  We might want to also count stairs,\n
536 * open doors, closed doors, etc.
537 */
538 static int next_to_corr(POSITION y1, POSITION x1)
539 {
540         int i, k = 0;
541         POSITION y, x;
542
543         grid_type *g_ptr;
544
545         /* Scan adjacent grids */
546         for (i = 0; i < 4; i++)
547         {
548                 /* Extract the location */
549                 y = y1 + ddy_ddd[i];
550                 x = x1 + ddx_ddd[i];
551                 g_ptr = &current_floor_ptr->grid_array[y][x];
552
553                 /* Skip non floors */
554                 if (cave_have_flag_grid(g_ptr, FF_WALL)) continue;
555
556                 /* Skip non "empty floor" grids */
557                 if (!is_floor_grid(g_ptr))
558                         continue;
559
560                 /* Skip grids inside rooms */
561                 if (g_ptr->info & (CAVE_ROOM)) continue;
562
563                 /* Count these grids */
564                 k++;
565         }
566
567         /* Return the number of corridors */
568         return (k);
569 }
570
571 /*!
572 * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
573 * @param y 判定を行いたいマスのY座標
574 * @param x 判定を行いたいマスのX座標
575 * @return ドアを設置可能ならばTRUEを返す
576 * @note Assumes "in_bounds(y1, x1)"
577 * @details
578 * \n
579 * Assumes "in_bounds(y, x)"\n
580 */
581 static bool possible_doorway(POSITION y, POSITION x)
582 {
583         /* Count the adjacent corridors */
584         if (next_to_corr(y, x) >= 2)
585         {
586                 /* Check Vertical */
587                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
588                         cave_have_flag_bold(y + 1, x, FF_WALL))
589                 {
590                         return (TRUE);
591                 }
592
593                 /* Check Horizontal */
594                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
595                         cave_have_flag_bold(y, x + 1, FF_WALL))
596                 {
597                         return (TRUE);
598                 }
599         }
600
601         /* No doorway */
602         return (FALSE);
603 }
604
605 /*!
606 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
607 * @param y 設置を行いたいマスのY座標
608 * @param x 設置を行いたいマスのX座標
609 * @return なし
610 */
611 void try_door(POSITION y, POSITION x)
612 {       if (!in_bounds(y, x)) return;
613
614         /* Ignore walls */
615         if (cave_have_flag_bold(y, x, FF_WALL)) return;
616
617         /* Ignore room grids */
618         if (current_floor_ptr->grid_array[y][x].info & (CAVE_ROOM)) return;
619
620         /* Occasional door (if allowed) */
621         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS))
622         {
623                 /* Place a door */
624                 place_random_door(y, x, FALSE);
625         }
626 }
627
628
629 /*!
630  * @brief 長方形の空洞を生成する / Make an empty square floor, for the middle of rooms
631  * @param x1 長方形の左端X座標(-1)
632  * @param x2 長方形の右端X座標(+1)
633  * @param y1 長方形の上端Y座標(-1)
634  * @param y2 長方形の下端Y座標(+1)
635  * @param light 照明の有無
636  * @return なし
637  */
638 void place_floor(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
639 {
640         POSITION x, y;
641
642         /* Place a full floor under the room */
643         for (y = y1 - 1; y <= y2 + 1; y++)
644         {
645                 for (x = x1 - 1; x <= x2 + 1; x++)
646                 {
647                         place_floor_bold(y, x);
648                         add_cave_info(y, x, CAVE_ROOM);
649                         if (light) add_cave_info(y, x, CAVE_GLOW);
650                 }
651         }
652 }
653
654
655 /*!
656  * @brief 長方形の部屋を生成する / Make an empty square room, only floor and wall grids
657  * @param x1 長方形の左端X座標(-1)
658  * @param x2 長方形の右端X座標(+1)
659  * @param y1 長方形の上端Y座標(-1)
660  * @param y2 長方形の下端Y座標(+1)
661  * @param light 照明の有無
662  * @return なし
663  */
664 void place_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
665 {
666         POSITION y, x;
667
668         place_floor(x1, x2, y1, y2, light);
669
670         /* Walls around the room */
671         for (y = y1 - 1; y <= y2 + 1; y++)
672         {
673                 place_outer_bold(y, x1 - 1);
674                 place_outer_bold(y, x2 + 1);
675         }
676         for (x = x1 - 1; x <= x2 + 1; x++)
677         {
678                 place_outer_bold(y1 - 1, x);
679                 place_outer_bold(y2 + 1, x);
680         }
681 }
682
683
684 /*!
685  * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
686  * @param y 配置したい中心マスのY座標
687  * @param x 配置したい中心マスのX座標
688  * @param num 配置したい数
689  * @return なし
690  * @details
691  * Only really called by some of the "vault" routines.
692  */
693 void vault_objects(POSITION y, POSITION x, int num)
694 {
695         int dummy = 0;
696         int i = 0, j = y, k = x;
697
698         grid_type *g_ptr;
699
700
701         /* Attempt to place 'num' objects */
702         for (; num > 0; --num)
703         {
704                 /* Try up to 11 spots looking for empty space */
705                 for (i = 0; i < 11; ++i)
706                 {
707                         /* Pick a random location */
708                         while (dummy < SAFE_MAX_ATTEMPTS)
709                         {
710                                 j = rand_spread(y, 2);
711                                 k = rand_spread(x, 3);
712                                 dummy++;
713                                 if (!in_bounds(j, k)) continue;
714                                 break;
715                         }
716
717                         if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
718                         {
719                                 msg_print(_("警告!地下室のアイテムを配置できません!", "Warning! Could not place vault object!"));
720                         }
721
722                         /* Require "clean" floor space */
723                         g_ptr = &current_floor_ptr->grid_array[j][k];
724                         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) continue;
725
726                         if (randint0(100) < 75)
727                         {
728                                 place_object(j, k, 0L);
729                         }
730                         else
731                         {
732                                 place_gold(j, k);
733                         }
734
735                         /* Placement accomplished */
736                         break;
737                 }
738         }
739 }
740
741 /*!
742  * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
743  * @param y トラップを配置したいマスの中心Y座標
744  * @param x トラップを配置したいマスの中心X座標
745  * @param yd Y方向の配置分散マス数
746  * @param xd X方向の配置分散マス数
747  * @return なし
748  * @details
749  * Only really called by some of the "vault" routines.
750  */
751 void vault_trap_aux(POSITION y, POSITION x, POSITION yd, POSITION xd)
752 {
753         int count = 0, y1 = y, x1 = x;
754         int dummy = 0;
755
756         grid_type *g_ptr;
757
758         /* Place traps */
759         for (count = 0; count <= 5; count++)
760         {
761                 /* Get a location */
762                 while (dummy < SAFE_MAX_ATTEMPTS)
763                 {
764                         y1 = rand_spread(y, yd);
765                         x1 = rand_spread(x, xd);
766                         dummy++;
767                         if (!in_bounds(y1, x1)) continue;
768                         break;
769                 }
770
771                 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
772                 {
773                         msg_print(_("警告!地下室のトラップを配置できません!", "Warning! Could not place vault trap!"));
774                 }
775
776                 /* Require "naked" floor grids */
777                 g_ptr = &current_floor_ptr->grid_array[y1][x1];
778                 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
779
780                 /* Place the trap */
781                 place_trap(y1, x1);
782
783                 break;
784         }
785 }
786
787 /*!
788  * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
789  * @param y トラップを配置したいマスの中心Y座標
790  * @param x トラップを配置したいマスの中心X座標
791  * @param yd Y方向の配置分散マス数
792  * @param xd X方向の配置分散マス数
793  * @param num 配置したいトラップの数
794  * @return なし
795  * @details
796  * Only really called by some of the "vault" routines.
797  */
798 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
799 {
800         int i;
801
802         for (i = 0; i < num; i++)
803         {
804                 vault_trap_aux(y, x, yd, xd);
805         }
806 }
807
808 /*!
809  * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
810  * @param y1 モンスターを配置したいマスの中心Y座標
811  * @param x1 モンスターを配置したいマスの中心X座標
812  * @param num 配置したいモンスターの数
813  * @return なし
814  * @details
815  * Only really called by some of the "vault" routines.
816  */
817 void vault_monsters(POSITION y1, POSITION x1, int num)
818 {
819         int k, i;
820         POSITION y, x;
821         grid_type *g_ptr;
822
823         /* Try to summon "num" monsters "near" the given location */
824         for (k = 0; k < num; k++)
825         {
826                 /* Try nine locations */
827                 for (i = 0; i < 9; i++)
828                 {
829                         int d = 1;
830
831                         /* Pick a nearby location */
832                         scatter(&y, &x, y1, x1, d, 0);
833
834                         /* Require "empty" floor grids */
835                         g_ptr = &current_floor_ptr->grid_array[y][x];
836                         if (!cave_empty_grid(g_ptr)) continue;
837
838                         /* Place the monster (allow groups) */
839                         current_floor_ptr->monster_level = current_floor_ptr->base_level + 2;
840                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
841                         current_floor_ptr->monster_level = current_floor_ptr->base_level;
842                 }
843         }
844 }
845
846 /*!
847  * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor.  (Includes range checking.)
848  * @param x チェックするマスのX座標
849  * @param y チェックするマスのY座標
850  * @return 床系地形ならばTRUE
851  */
852 bool get_is_floor(POSITION x, POSITION y)
853 {
854         if (!in_bounds(y, x))
855         {
856                 /* Out of bounds */
857                 return (FALSE);
858         }
859
860         /* Do the real check */
861         if (is_floor_bold(y, x)) return (TRUE);
862
863         return (FALSE);
864 }
865
866 /*!
867  * @brief 指定のマスを床地形に変える / Set a square to be floor.  (Includes range checking.)
868  * @param x 地形を変えたいマスのX座標
869  * @param y 地形を変えたいマスのY座標
870  * @return なし
871  */
872 void set_floor(POSITION x, POSITION y)
873 {
874         if (!in_bounds(y, x))
875         {
876                 /* Out of bounds */
877                 return;
878         }
879
880         if (current_floor_ptr->grid_array[y][x].info & CAVE_ROOM)
881         {
882                 /* A room border don't touch. */
883                 return;
884         }
885
886         /* Set to be floor if is a wall (don't touch lakes). */
887         if (is_extra_bold(y, x))
888                 place_floor_bold(y, x);
889 }
890
891 /*!
892  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
893  * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
894  * @return なし
895  */
896 void place_bound_perm_wall(grid_type *g_ptr)
897 {
898         if (bound_walls_perm)
899         {
900                 /* Clear boundary mimic */
901                 g_ptr->mimic = 0;
902         }
903         else
904         {
905                 feature_type *f_ptr = &f_info[g_ptr->feat];
906
907                 /* Hack -- Decline boundary walls with known treasure  */
908                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
909                         !have_flag(f_ptr->flags, FF_SECRET))
910                         g_ptr->feat = feat_state(g_ptr->feat, FF_ENSECRET);
911
912                 /* Set boundary mimic */
913                 g_ptr->mimic = g_ptr->feat;
914         }
915
916         /* Add "solid" perma-wall */
917         place_solid_perm_grid(g_ptr);
918 }
919
920 /*!
921  * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
922  * @param g_ptr マス構造体の参照ポインタ
923  * @return 看破済みの罠があるならTRUEを返す。
924  */
925 bool is_known_trap(grid_type *g_ptr)
926 {
927         if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
928                 is_trap(g_ptr->feat)) return TRUE;
929         else
930                 return FALSE;
931 }
932
933
934
935 /*!
936  * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
937  * @param g_ptr マス構造体の参照ポインタ
938  * @return 隠されたドアがあるならTRUEを返す。
939  */
940 bool is_hidden_door(grid_type *g_ptr)
941 {
942         if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
943                 is_closed_door(g_ptr->feat))
944                 return TRUE;
945         else
946                 return FALSE;
947 }
948
949 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
950
951
952 /*!
953  * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
954  * @param y y座標
955  * @param x x座標
956  * @return 指定された座標に照明がかかっているならTRUEを返す。。
957  */
958 bool check_local_illumination(POSITION y, POSITION x)
959 {
960         /* Hack -- move towards player */
961         POSITION yy = (y < p_ptr->y) ? (y + 1) : (y > p_ptr->y) ? (y - 1) : y;
962         POSITION xx = (x < p_ptr->x) ? (x + 1) : (x > p_ptr->x) ? (x - 1) : x;
963
964         /* Check for "local" illumination */
965
966 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
967
968         /* Check for "complex" illumination */
969         if ((feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[yy][xx])) &&
970                 (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
971                 (feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[y][xx])) &&
972                 (current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
973                         (feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[yy][x])) &&
974                 (current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
975         {
976                 return TRUE;
977         }
978         else return FALSE;
979
980 #else /* COMPLEX_WALL_ILLUMINATION */
981
982         /* Check for "simple" illumination */
983         return (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
984
985 #endif /* COMPLEX_WALL_ILLUMINATION */
986 }
987
988
989 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
990 #define update_local_illumination_aux(Y, X) \
991 { \
992         if (player_has_los_bold((Y), (X))) \
993         { \
994                 /* Update the monster */ \
995                 if (current_floor_ptr->grid_array[(Y)][(X)].m_idx) update_monster(current_floor_ptr->grid_array[(Y)][(X)].m_idx, FALSE); \
996 \
997                 /* Notice and redraw */ \
998                 note_spot((Y), (X)); \
999                 lite_spot((Y), (X)); \
1000         } \
1001 }
1002
1003 /*!
1004  * @brief 指定された座標の照明状態を更新する / Update "local" illumination
1005  * @param y y座標
1006  * @param x x座標
1007  * @return なし
1008  */
1009 void update_local_illumination(POSITION y, POSITION x)
1010 {
1011         int i;
1012         POSITION yy, xx;
1013
1014         if (!in_bounds(y, x)) return;
1015
1016 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
1017
1018         if ((y != p_ptr->y) && (x != p_ptr->x))
1019         {
1020                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1021                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1022                 update_local_illumination_aux(yy, xx);
1023                 update_local_illumination_aux(y, xx);
1024                 update_local_illumination_aux(yy, x);
1025         }
1026         else if (x != p_ptr->x) /* y == p_ptr->y */
1027         {
1028                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1029                 for (i = -1; i <= 1; i++)
1030                 {
1031                         yy = y + i;
1032                         update_local_illumination_aux(yy, xx);
1033                 }
1034                 yy = y - 1;
1035                 update_local_illumination_aux(yy, x);
1036                 yy = y + 1;
1037                 update_local_illumination_aux(yy, x);
1038         }
1039         else if (y != p_ptr->y) /* x == p_ptr->x */
1040         {
1041                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1042                 for (i = -1; i <= 1; i++)
1043                 {
1044                         xx = x + i;
1045                         update_local_illumination_aux(yy, xx);
1046                 }
1047                 xx = x - 1;
1048                 update_local_illumination_aux(y, xx);
1049                 xx = x + 1;
1050                 update_local_illumination_aux(y, xx);
1051         }
1052         else /* Player's grid */
1053         {
1054                 for (i = 0; i < 8; i++)
1055                 {
1056                         yy = y + ddy_cdd[i];
1057                         xx = x + ddx_cdd[i];
1058                         update_local_illumination_aux(yy, xx);
1059                 }
1060         }
1061
1062 #else /* COMPLEX_WALL_ILLUMINATION */
1063
1064         if ((y != p_ptr->y) && (x != p_ptr->x))
1065         {
1066                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1067                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1068                 update_local_illumination_aux(yy, xx);
1069         }
1070         else if (x != p_ptr->x) /* y == p_ptr->y */
1071         {
1072                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1073                 for (i = -1; i <= 1; i++)
1074                 {
1075                         yy = y + i;
1076                         update_local_illumination_aux(yy, xx);
1077                 }
1078         }
1079         else if (y != p_ptr->y) /* x == p_ptr->x */
1080         {
1081                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1082                 for (i = -1; i <= 1; i++)
1083                 {
1084                         xx = x + i;
1085                         update_local_illumination_aux(yy, xx);
1086                 }
1087         }
1088         else /* Player's grid */
1089         {
1090                 for (i = 0; i < 8; i++)
1091                 {
1092                         yy = y + ddy_cdd[i];
1093                         xx = x + ddx_cdd[i];
1094                         update_local_illumination_aux(yy, xx);
1095                 }
1096         }
1097
1098 #endif /* COMPLEX_WALL_ILLUMINATION */
1099 }
1100
1101
1102 /*!
1103  * @brief 指定された座標をプレイヤーが視覚に収められるかを返す。 / Can the player "see" the given grid in detail?
1104  * @param y y座標
1105  * @param x x座標
1106  * @return 視覚に収められる状態ならTRUEを返す
1107  * @details
1108  * He must have vision, illumination, and line of sight.\n
1109  * \n
1110  * Note -- "CAVE_LITE" is only set if the "torch" has "los()".\n
1111  * So, given "CAVE_LITE", we know that the grid is "fully visible".\n
1112  *\n
1113  * Note that "CAVE_GLOW" makes little sense for a wall, since it would mean\n
1114  * that a wall is visible from any direction.  That would be odd.  Except\n
1115  * under wizard light, which might make sense.  Thus, for walls, we require\n
1116  * not only that they be "CAVE_GLOW", but also, that they be adjacent to a\n
1117  * grid which is not only "CAVE_GLOW", but which is a non-wall, and which is\n
1118  * in line of sight of the player.\n
1119  *\n
1120  * This extra check is expensive, but it provides a more "correct" semantics.\n
1121  *\n
1122  * Note that we should not run this check on walls which are "outer walls" of\n
1123  * the dungeon, or we will induce a memory fault, but actually verifying all\n
1124  * of the locations would be extremely expensive.\n
1125  *\n
1126  * Thus, to speed up the function, we assume that all "perma-walls" which are\n
1127  * "CAVE_GLOW" are "illuminated" from all sides.  This is correct for all cases\n
1128  * except "vaults" and the "buildings" in town.  But the town is a hack anyway,\n
1129  * and the player has more important things on his mind when he is attacking a\n
1130  * monster vault.  It is annoying, but an extremely important optimization.\n
1131  *\n
1132  * Note that "glowing walls" are only considered to be "illuminated" if the\n
1133  * grid which is next to the wall in the direction of the player is also a\n
1134  * "glowing" grid.  This prevents the player from being able to "see" the\n
1135  * walls of illuminated rooms from a corridor outside the room.\n
1136  */
1137 bool player_can_see_bold(POSITION y, POSITION x)
1138 {
1139         grid_type *g_ptr;
1140
1141         /* Blind players see nothing */
1142         if (p_ptr->blind) return FALSE;
1143
1144         g_ptr = &current_floor_ptr->grid_array[y][x];
1145
1146         /* Note that "torch-lite" yields "illumination" */
1147         if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) return TRUE;
1148
1149         /* Require line of sight to the grid */
1150         if (!player_has_los_bold(y, x)) return FALSE;
1151
1152         /* Noctovision of Ninja */
1153         if (p_ptr->see_nocto) return TRUE;
1154
1155         /* Require "perma-lite" of the grid */
1156         if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) return FALSE;
1157
1158         /* Feature code (applying "mimic" field) */
1159         /* Floors are simple */
1160         if (feat_supports_los(get_feat_mimic(g_ptr))) return TRUE;
1161
1162         /* Check for "local" illumination */
1163         return check_local_illumination(y, x);
1164 }
1165
1166 /*!
1167  * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
1168  * @return 視覚に収められていないならTRUEを返す
1169  * @details player_can_see_bold()関数の返り値の否定を返している。
1170  */
1171 bool no_lite(void)
1172 {
1173         return (!player_can_see_bold(p_ptr->y, p_ptr->x));
1174 }
1175
1176
1177 /*!
1178  * @brief 指定された座標が地震や階段生成の対象となるマスかを返す。 / Determine if a given location may be "destroyed"
1179  * @param y y座標
1180  * @param x x座標
1181  * @return 各種の変更が可能ならTRUEを返す。
1182  * @details
1183  * 条件は永久地形でなく、なおかつ該当のマスにアーティファクトが存在しないか、である。英語の旧コメントに反して*破壊*の抑止判定には現在使われていない。
1184  */
1185 bool cave_valid_bold(POSITION y, POSITION x)
1186 {
1187         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1188         OBJECT_IDX this_o_idx, next_o_idx = 0;
1189
1190         /* Forbid perma-grids */
1191         if (cave_perma_grid(g_ptr)) return (FALSE);
1192
1193         /* Check objects */
1194         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1195         {
1196                 object_type *o_ptr;
1197                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
1198                 next_o_idx = o_ptr->next_o_idx;
1199
1200                 /* Forbid artifact grids */
1201                 if (object_is_artifact(o_ptr)) return (FALSE);
1202         }
1203
1204         /* Accept */
1205         return (TRUE);
1206 }
1207
1208
1209
1210 /*
1211  * Place an attr/char pair at the given map coordinate, if legal.
1212  */
1213 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
1214 {
1215         /* Only do "legal" locations */
1216         if (panel_contains(y, x))
1217         {
1218                 /* Hack -- fake monochrome */
1219                 if (!use_graphics)
1220                 {
1221                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
1222                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
1223                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1224                 }
1225
1226                 /* Draw the char using the attr */
1227                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
1228         }
1229 }
1230
1231
1232
1233
1234
1235 /*
1236  * Memorize interesting viewable object/features in the given grid
1237  *
1238  * This function should only be called on "legal" grids.
1239  *
1240  * This function will memorize the object and/or feature in the given
1241  * grid, if they are (1) viewable and (2) interesting.  Note that all
1242  * objects are interesting, all terrain features except floors (and
1243  * invisible traps) are interesting, and floors (and invisible traps)
1244  * are interesting sometimes (depending on various options involving
1245  * the illumination of floor grids).
1246  *
1247  * The automatic memorization of all objects and non-floor terrain
1248  * features as soon as they are displayed allows incredible amounts
1249  * of optimization in various places, especially "map_info()".
1250  *
1251  * Note that the memorization of objects is completely separate from
1252  * the memorization of terrain features, preventing annoying floor
1253  * memorization when a detected object is picked up from a dark floor,
1254  * and object memorization when an object is dropped into a floor grid
1255  * which is memorized but out-of-sight.
1256  *
1257  * This function should be called every time the "memorization" of
1258  * a grid (or the object in a grid) is called into question, such
1259  * as when an object is created in a grid, when a terrain feature
1260  * "changes" from "floor" to "non-floor", when any grid becomes
1261  * "illuminated" or "viewable", and when a "floor" grid becomes
1262  * "torch-lit".
1263  *
1264  * Note the relatively efficient use of this function by the various
1265  * "update_view()" and "update_lite()" calls, to allow objects and
1266  * terrain features to be memorized (and drawn) whenever they become
1267  * viewable or illuminated in any way, but not when they "maintain"
1268  * or "lose" their previous viewability or illumination.
1269  *
1270  * Note the butchered "internal" version of "player_can_see_bold()",
1271  * optimized primarily for the most common cases, that is, for the
1272  * non-marked floor grids.
1273  */
1274 void note_spot(POSITION y, POSITION x)
1275 {
1276         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1277         OBJECT_IDX this_o_idx, next_o_idx = 0;
1278
1279         /* Blind players see nothing */
1280         if (p_ptr->blind) return;
1281
1282         /* Analyze non-torch-lit grids */
1283         if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
1284         {
1285                 /* Require line of sight to the grid */
1286                 if (!(g_ptr->info & (CAVE_VIEW))) return;
1287
1288                 /* Require "perma-lite" of the grid */
1289                 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1290                 {
1291                         /* Not Ninja */
1292                         if (!p_ptr->see_nocto) return;
1293                 }
1294         }
1295
1296
1297         /* Hack -- memorize objects */
1298         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1299         {
1300                 object_type *o_ptr = &current_floor_ptr->o_list[this_o_idx];
1301                 next_o_idx = o_ptr->next_o_idx;
1302
1303                 /* Memorize objects */
1304                 o_ptr->marked |= OM_FOUND;
1305         }
1306
1307
1308         /* Hack -- memorize grids */
1309         if (!(g_ptr->info & (CAVE_MARK)))
1310         {
1311                 /* Feature code (applying "mimic" field) */
1312                 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
1313
1314                 /* Memorize some "boring" grids */
1315                 if (!have_flag(f_ptr->flags, FF_REMEMBER))
1316                 {
1317                         /* Option -- memorize all torch-lit floors */
1318                         if (view_torch_grids &&
1319                                 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
1320                         {
1321                                 g_ptr->info |= (CAVE_MARK);
1322                         }
1323
1324                         /* Option -- memorize all perma-lit floors */
1325                         else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
1326                         {
1327                                 g_ptr->info |= (CAVE_MARK);
1328                         }
1329                 }
1330
1331                 /* Memorize normal grids */
1332                 else if (have_flag(f_ptr->flags, FF_LOS))
1333                 {
1334                         g_ptr->info |= (CAVE_MARK);
1335                 }
1336
1337                 /* Memorize torch-lit walls */
1338                 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
1339                 {
1340                         g_ptr->info |= (CAVE_MARK);
1341                 }
1342
1343                 /* Memorize walls seen by noctovision of Ninja */
1344                 else if (p_ptr->see_nocto)
1345                 {
1346                         g_ptr->info |= (CAVE_MARK);
1347                 }
1348
1349                 /* Memorize certain non-torch-lit wall grids */
1350                 else if (check_local_illumination(y, x))
1351                 {
1352                         g_ptr->info |= (CAVE_MARK);
1353                 }
1354         }
1355
1356         /* Memorize terrain of the grid */
1357         g_ptr->info |= (CAVE_KNOWN);
1358 }
1359
1360 /*
1361  * Redraw (on the screen) a given MAP location
1362  *
1363  * This function should only be called on "legal" grids
1364  */
1365 void lite_spot(POSITION y, POSITION x)
1366 {
1367         /* Redraw if on screen */
1368         if (panel_contains(y, x) && in_bounds2(y, x))
1369         {
1370                 TERM_COLOR a;
1371                 SYMBOL_CODE c;
1372                 TERM_COLOR ta;
1373                 SYMBOL_CODE tc;
1374
1375                 map_info(y, x, &a, &c, &ta, &tc);
1376
1377                 /* Hack -- fake monochrome */
1378                 if (!use_graphics)
1379                 {
1380                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
1381                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
1382                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1383                 }
1384
1385                 /* Hack -- Queue it */
1386                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
1387
1388                 /* Update sub-windows */
1389                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1390         }
1391 }
1392
1393 /*
1394  * Some comments on the grid flags.  -BEN-
1395  *
1396  *
1397  * One of the major bottlenecks in previous versions of Angband was in
1398  * the calculation of "line of sight" from the player to various grids,
1399  * such as monsters.  This was such a nasty bottleneck that a lot of
1400  * silly things were done to reduce the dependancy on "line of sight",
1401  * for example, you could not "see" any grids in a lit room until you
1402  * actually entered the room, and there were all kinds of bizarre grid
1403  * flags to enable this behavior.  This is also why the "call light"
1404  * spells always lit an entire room.
1405  *
1406  * The code below provides functions to calculate the "field of view"
1407  * for the player, which, once calculated, provides extremely fast
1408  * calculation of "line of sight from the player", and to calculate
1409  * the "field of torch lite", which, again, once calculated, provides
1410  * extremely fast calculation of "which grids are lit by the player's
1411  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
1412  * "GRID_LITE", as appropriate, these functions maintain an array for
1413  * each of these two flags, each array containing the locations of all
1414  * of the grids marked with the appropriate flag, which can be used to
1415  * very quickly scan through all of the grids in a given set.
1416  *
1417  * To allow more "semantically valid" field of view semantics, whenever
1418  * the field of view (or the set of torch lit grids) changes, all of the
1419  * grids in the field of view (or the set of torch lit grids) are "drawn"
1420  * so that changes in the world will become apparent as soon as possible.
1421  * This has been optimized so that only grids which actually "change" are
1422  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
1423  * of the grids which are entering or leaving the relevent set of grids.
1424  *
1425  * These new methods are so efficient that the old nasty code was removed.
1426  *
1427  * Note that there is no reason to "update" the "viewable space" unless
1428  * the player "moves", or walls/doors are created/destroyed, and there
1429  * is no reason to "update" the "torch lit grids" unless the field of
1430  * view changes, or the "light radius" changes.  This means that when
1431  * the player is resting, or digging, or doing anything that does not
1432  * involve movement or changing the state of the dungeon, there is no
1433  * need to update the "view" or the "lite" regions, which is nice.
1434  *
1435  * Note that the calls to the nasty "los()" function have been reduced
1436  * to a bare minimum by the use of the new "field of view" calculations.
1437  *
1438  * I wouldn't be surprised if slight modifications to the "update_view()"
1439  * function would allow us to determine "reverse line-of-sight" as well
1440  * as "normal line-of-sight", which would allow monsters to use a more
1441  * "correct" calculation to determine if they can "see" the player.  For
1442  * now, monsters simply "cheat" somewhat and assume that if the player
1443  * has "line of sight" to the monster, then the monster can "pretend"
1444  * that it has "line of sight" to the player.
1445  *
1446  *
1447  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
1448  * grid and maintains an array of all "CAVE_LITE" grids.
1449  *
1450  * This set of grids is the complete set of all grids which are lit by
1451  * the players light source, which allows the "player_can_see_bold()"
1452  * function to work very quickly.
1453  *
1454  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
1455  * fact, the player (unless blind) can always "see" all grids which are
1456  * marked as "CAVE_LITE", unless they are "off screen".
1457  *
1458  *
1459  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
1460  * grid and maintains an array of all "CAVE_VIEW" grids.
1461  *
1462  * This set of grids is the complete set of all grids within line of sight
1463  * of the player, allowing the "player_has_los_bold()" macro to work very
1464  * quickly.
1465  *
1466  *
1467  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
1468  * temporary internal flag to mark those grids which are not only in view,
1469  * but which are also "easily" in line of sight of the player.  This flag
1470  * is always cleared when we are done.
1471  *
1472  *
1473  * The current "update_lite()" and "update_view()" algorithms use the
1474  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
1475  * to keep track of which grids were previously marked as "CAVE_LITE" or
1476  * "CAVE_VIEW", which allows us to optimize the "screen updates".
1477  *
1478  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
1479  * for various other purposes, such as spreading lite or darkness during
1480  * "lite_room()" / "unlite_room()", and for calculating monster flow.
1481  *
1482  *
1483  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
1484  * in some way permanently lit.  However, for the player to "see" anything
1485  * in the grid, as determined by "player_can_see()", the player must not be
1486  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
1487  * grids, even if marked as "perma lit", are only illuminated if they touch
1488  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
1489  *
1490  *
1491  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
1492  * that even if the player cannot "see" the grid, he "knows" the terrain in
1493  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
1494  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
1495  * or when one of the "memorize floor grids" options induces memorization.
1496  *
1497  * Objects are "memorized" in a different way, using a special "marked" flag
1498  * on the object itself, which is set when an object is observed or detected.
1499  *
1500  *
1501  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
1502  * and should be illuminated by "lite room" and "darkness" spells.
1503  *
1504  *
1505  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
1506  * and should be unavailable for "teleportation" destinations.
1507  *
1508  *
1509  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
1510  * which is observed, and the "view_torch_grids" allows the player to memorize
1511  * every torch-lit grid.  The player will always memorize important walls,
1512  * doors, stairs, and other terrain features, as well as any "detected" grids.
1513  *
1514  * Note that the new "update_view()" method allows, among other things, a room
1515  * to be "partially" seen as the player approaches it, with a growing cone of
1516  * floor appearing as the player gets closer to the door.  Also, by not turning
1517  * on the "memorize perma-lit grids" option, the player will only "see" those
1518  * floor grids which are actually in line of sight.
1519  *
1520  * And my favorite "plus" is that you can now use a special option to draw the
1521  * "floors" in the "viewable region" brightly (actually, to draw the *other*
1522  * grids dimly), providing a "pretty" effect as the player runs around, and
1523  * to efficiently display the "torch lite" in a special color.
1524  *
1525  *
1526  * Some comments on the "update_view()" algorithm...
1527  *
1528  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
1529  * and only has to call "los()" on the borderline cases.  The major axes/diags
1530  * even terminate early when they hit walls.  I need to find a quick way
1531  * to "terminate" the other scans.
1532  *
1533  * Note that in the worst case (a big empty area with say 5% scattered walls),
1534  * each of the 1500 or so nearby grids is checked once, most of them getting
1535  * an "instant" rating, and only a small portion requiring a call to "los()".
1536  *
1537  * The only time that the algorithm appears to be "noticeably" too slow is
1538  * when running, and this is usually only important in town, since the town
1539  * provides about the worst scenario possible, with large open regions and
1540  * a few scattered obstructions.  There is a special "efficiency" option to
1541  * allow the player to reduce his field of view in town, if needed.
1542  *
1543  * In the "best" case (say, a normal stretch of corridor), the algorithm
1544  * makes one check for each viewable grid, and makes no calls to "los()".
1545  * So running in corridors is very fast, and if a lot of monsters are
1546  * nearby, it is much faster than the old methods.
1547  *
1548  * Note that resting, most normal commands, and several forms of running,
1549  * plus all commands executed near large groups of monsters, are strictly
1550  * more efficient with "update_view()" that with the old "compute los() on
1551  * demand" method, primarily because once the "field of view" has been
1552  * calculated, it does not have to be recalculated until the player moves
1553  * (or a wall or door is created or destroyed).
1554  *
1555  * Note that we no longer have to do as many "los()" checks, since once the
1556  * "view" region has been built, very few things cause it to be "changed"
1557  * (player movement, and the opening/closing of doors, changes in wall status).
1558  * Note that door/wall changes are only relevant when the door/wall itself is
1559  * in the "view" region.
1560  *
1561  * The algorithm seems to only call "los()" from zero to ten times, usually
1562  * only when coming down a corridor into a room, or standing in a room, just
1563  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
1564  * we will be reducing the calls to "los()".
1565  *
1566  * I am thinking in terms of an algorithm that "walks" from the central point
1567  * out to the maximal "distance", at each point, determining the "view" code
1568  * (above).  For each grid not on a major axis or diagonal, the "view" code
1569  * depends on the "cave_los_bold()" and "view" of exactly two other grids
1570  * (the one along the nearest diagonal, and the one next to that one, see
1571  * "update_view_aux()"...).
1572  *
1573  * We "memorize" the viewable space array, so that at the cost of under 3000
1574  * bytes, we reduce the time taken by "forget_view()" to one assignment for
1575  * each grid actually in the "viewable space".  And for another 3000 bytes,
1576  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
1577  * are also used by other routines, thus reducing the cost to almost nothing.
1578  *
1579  * A similar thing is done for "forget_lite()" in which case the savings are
1580  * much less, but save us from doing bizarre maintenance checking.
1581  *
1582  * In the worst "normal" case (in the middle of the town), the reachable space
1583  * actually reaches to more than half of the largest possible "circle" of view,
1584  * or about 800 grids, and in the worse case (in the middle of a dungeon level
1585  * where all the walls have been removed), the reachable space actually reaches
1586  * the theoretical maximum size of just under 1500 grids.
1587  *
1588  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
1589  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
1590  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
1591  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
1592  * entire possible space (including initialization) in one step per grid.  If
1593  * we do the "clearing" as a separate step (and use an array of "view" grids),
1594  * then the clearing will take as many steps as grids that were viewed, and the
1595  * algorithm will be able to "stop" scanning at various points.
1596  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
1597  */
1598
1599 /*
1600  * Mega-Hack -- Delayed visual update
1601  * Only used if update_view(), update_lite() or update_mon_lite() was called
1602  */
1603 void delayed_visual_update(void)
1604 {
1605         int i;
1606         POSITION y, x;
1607         grid_type *g_ptr;
1608
1609         /* Update needed grids */
1610         for (i = 0; i < current_floor_ptr->redraw_n; i++)
1611         {
1612                 y = current_floor_ptr->redraw_y[i];
1613                 x = current_floor_ptr->redraw_x[i];
1614                 g_ptr = &current_floor_ptr->grid_array[y][x];
1615
1616                 /* Update only needed grids (prevent multiple updating) */
1617                 if (!(g_ptr->info & CAVE_REDRAW)) continue;
1618
1619                 /* If required, note */
1620                 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
1621
1622                 lite_spot(y, x);
1623
1624                 /* Hack -- Visual update of monster on this grid */
1625                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1626
1627                 /* No longer in the array */
1628                 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
1629         }
1630
1631         /* None left */
1632         current_floor_ptr->redraw_n = 0;
1633 }
1634
1635
1636 /*
1637  * Hack -- forget the "flow" information
1638  */
1639 void forget_flow(void)
1640 {
1641         POSITION x, y;
1642
1643         /* Check the entire dungeon */
1644         for (y = 0; y < current_floor_ptr->height; y++)
1645         {
1646                 for (x = 0; x < current_floor_ptr->width; x++)
1647                 {
1648                         /* Forget the old data */
1649                         current_floor_ptr->grid_array[y][x].dist = 0;
1650                         current_floor_ptr->grid_array[y][x].cost = 0;
1651                         current_floor_ptr->grid_array[y][x].when = 0;
1652                 }
1653         }
1654 }
1655
1656
1657 /*
1658  * Hack - speed up the update_flow algorithm by only doing
1659  * it everytime the player moves out of LOS of the last
1660  * "way-point".
1661  */
1662 static POSITION flow_x = 0;
1663 static POSITION flow_y = 0;
1664
1665
1666
1667 /*
1668  * Hack -- fill in the "cost" field of every grid that the player
1669  * can "reach" with the number of steps needed to reach that grid.
1670  * This also yields the "distance" of the player from every grid.
1671  *
1672  * In addition, mark the "when" of the grids that can reach
1673  * the player with the incremented value of "flow_n".
1674  *
1675  * Hack -- use the "seen" array as a "circular queue".
1676  *
1677  * We do not need a priority queue because the cost from grid
1678  * to grid is always "one" and we process them in order.
1679  */
1680 void update_flow(void)
1681 {
1682         POSITION x, y;
1683         DIRECTION d;
1684         int flow_head = 1;
1685         int flow_tail = 0;
1686
1687         /* Paranoia -- make sure the array is empty */
1688         if (tmp_pos.n) return;
1689
1690         /* The last way-point is on the map */
1691         if (p_ptr->running && in_bounds(flow_y, flow_x))
1692         {
1693                 /* The way point is in sight - do not update.  (Speedup) */
1694                 if (current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
1695         }
1696
1697         /* Erase all of the current flow information */
1698         for (y = 0; y < current_floor_ptr->height; y++)
1699         {
1700                 for (x = 0; x < current_floor_ptr->width; x++)
1701                 {
1702                         current_floor_ptr->grid_array[y][x].cost = 0;
1703                         current_floor_ptr->grid_array[y][x].dist = 0;
1704                 }
1705         }
1706
1707         /* Save player position */
1708         flow_y = p_ptr->y;
1709         flow_x = p_ptr->x;
1710
1711         /* Add the player's grid to the queue */
1712         tmp_pos.y[0] = p_ptr->y;
1713         tmp_pos.x[0] = p_ptr->x;
1714
1715         /* Now process the queue */
1716         while (flow_head != flow_tail)
1717         {
1718                 int ty, tx;
1719
1720                 /* Extract the next entry */
1721                 ty = tmp_pos.y[flow_tail];
1722                 tx = tmp_pos.x[flow_tail];
1723
1724                 /* Forget that entry */
1725                 if (++flow_tail == TEMP_MAX) flow_tail = 0;
1726
1727                 /* Add the "children" */
1728                 for (d = 0; d < 8; d++)
1729                 {
1730                         int old_head = flow_head;
1731                         byte_hack m = current_floor_ptr->grid_array[ty][tx].cost + 1;
1732                         byte_hack n = current_floor_ptr->grid_array[ty][tx].dist + 1;
1733                         grid_type *g_ptr;
1734
1735                         /* Child location */
1736                         y = ty + ddy_ddd[d];
1737                         x = tx + ddx_ddd[d];
1738
1739                         /* Ignore player's grid */
1740                         if (player_bold(y, x)) continue;
1741
1742                         g_ptr = &current_floor_ptr->grid_array[y][x];
1743
1744                         if (is_closed_door(g_ptr->feat)) m += 3;
1745
1746                         /* Ignore "pre-stamped" entries */
1747                         if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
1748
1749                         /* Ignore "walls" and "rubble" */
1750                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
1751
1752                         /* Save the flow cost */
1753                         if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
1754                         if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
1755
1756                         /* Hack -- limit flow depth */
1757                         if (n == MONSTER_FLOW_DEPTH) continue;
1758
1759                         /* Enqueue that entry */
1760                         tmp_pos.y[flow_head] = y;
1761                         tmp_pos.x[flow_head] = x;
1762
1763                         /* Advance the queue */
1764                         if (++flow_head == TEMP_MAX) flow_head = 0;
1765
1766                         /* Hack -- notice overflow by forgetting new entry */
1767                         if (flow_head == flow_tail) flow_head = old_head;
1768                 }
1769         }
1770 }
1771
1772
1773 static int scent_when = 0;
1774
1775 /*
1776  * Characters leave scent trails for perceptive monsters to track.
1777  *
1778  * Smell is rather more limited than sound.  Many creatures cannot use
1779  * it at all, it doesn't extend very far outwards from the character's
1780  * current position, and monsters can use it to home in the character,
1781  * but not to run away from him.
1782  *
1783  * Smell is valued according to age.  When a character takes his current_world_ptr->game_turn,
1784  * scent is aged by one, and new scent of the current age is laid down.
1785  * Speedy characters leave more scent, true, but it also ages faster,
1786  * which makes it harder to hunt them down.
1787  *
1788  * Whenever the age count loops, most of the scent trail is erased and
1789  * the age of the remainder is recalculated.
1790  */
1791 void update_smell(void)
1792 {
1793         POSITION i, j;
1794         POSITION y, x;
1795
1796         /* Create a table that controls the spread of scent */
1797         const int scent_adjust[5][5] =
1798         {
1799                 { -1, 0, 0, 0,-1 },
1800                 {  0, 1, 1, 1, 0 },
1801                 {  0, 1, 2, 1, 0 },
1802                 {  0, 1, 1, 1, 0 },
1803                 { -1, 0, 0, 0,-1 },
1804         };
1805
1806         /* Loop the age and adjust scent values when necessary */
1807         if (++scent_when == 254)
1808         {
1809                 /* Scan the entire dungeon */
1810                 for (y = 0; y < current_floor_ptr->height; y++)
1811                 {
1812                         for (x = 0; x < current_floor_ptr->width; x++)
1813                         {
1814                                 int w = current_floor_ptr->grid_array[y][x].when;
1815                                 current_floor_ptr->grid_array[y][x].when = (w > 128) ? (w - 128) : 0;
1816                         }
1817                 }
1818
1819                 /* Restart */
1820                 scent_when = 126;
1821         }
1822
1823
1824         /* Lay down new scent */
1825         for (i = 0; i < 5; i++)
1826         {
1827                 for (j = 0; j < 5; j++)
1828                 {
1829                         grid_type *g_ptr;
1830
1831                         /* Translate table to map grids */
1832                         y = i + p_ptr->y - 2;
1833                         x = j + p_ptr->x - 2;
1834
1835                         /* Check Bounds */
1836                         if (!in_bounds(y, x)) continue;
1837
1838                         g_ptr = &current_floor_ptr->grid_array[y][x];
1839
1840                         /* Walls, water, and lava cannot hold scent. */
1841                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
1842
1843                         /* Grid must not be blocked by walls from the character */
1844                         if (!player_has_los_bold(y, x)) continue;
1845
1846                         /* Note grids that are too far away */
1847                         if (scent_adjust[i][j] == -1) continue;
1848
1849                         /* Mark the grid with new scent */
1850                         g_ptr->when = scent_when + scent_adjust[i][j];
1851                 }
1852         }
1853 }
1854
1855
1856
1857 /*
1858  * Change the "feat" flag for a grid, and notice/redraw the grid
1859  */
1860 void cave_set_feat(POSITION y, POSITION x, FEAT_IDX feat)
1861 {
1862         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1863         feature_type *f_ptr = &f_info[feat];
1864         bool old_los, old_mirror;
1865
1866         if (!current_world_ptr->character_dungeon)
1867         {
1868                 /* Clear mimic type */
1869                 g_ptr->mimic = 0;
1870
1871                 /* Change the feature */
1872                 g_ptr->feat = feat;
1873
1874                 /* Hack -- glow the GLOW terrain */
1875                 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1876                 {
1877                         DIRECTION i;
1878                         POSITION yy, xx;
1879
1880                         for (i = 0; i < 9; i++)
1881                         {
1882                                 yy = y + ddy_ddd[i];
1883                                 xx = x + ddx_ddd[i];
1884                                 if (!in_bounds2(yy, xx)) continue;
1885                                 current_floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
1886                         }
1887                 }
1888
1889                 return;
1890         }
1891
1892         old_los = cave_have_flag_bold(y, x, FF_LOS);
1893         old_mirror = is_mirror_grid(g_ptr);
1894
1895         /* Clear mimic type */
1896         g_ptr->mimic = 0;
1897
1898         /* Change the feature */
1899         g_ptr->feat = feat;
1900
1901         /* Remove flag for mirror/glyph */
1902         g_ptr->info &= ~(CAVE_OBJECT);
1903
1904         if (old_mirror && (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1905         {
1906                 g_ptr->info &= ~(CAVE_GLOW);
1907                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1908
1909                 update_local_illumination(y, x);
1910         }
1911
1912         /* Check for change to boring grid */
1913         if (!have_flag(f_ptr->flags, FF_REMEMBER)) g_ptr->info &= ~(CAVE_MARK);
1914         if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1915
1916         note_spot(y, x);
1917         lite_spot(y, x);
1918
1919         /* Check if los has changed */
1920         if (old_los ^ have_flag(f_ptr->flags, FF_LOS))
1921         {
1922
1923 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
1924
1925                 update_local_illumination(y, x);
1926
1927 #endif /* COMPLEX_WALL_ILLUMINATION */
1928
1929                 /* Update the visuals */
1930                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE | PU_MONSTERS);
1931         }
1932
1933         /* Hack -- glow the GLOW terrain */
1934         if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1935         {
1936                 DIRECTION i;
1937                 POSITION yy, xx;
1938                 grid_type *cc_ptr;
1939
1940                 for (i = 0; i < 9; i++)
1941                 {
1942                         yy = y + ddy_ddd[i];
1943                         xx = x + ddx_ddd[i];
1944                         if (!in_bounds2(yy, xx)) continue;
1945                         cc_ptr = &current_floor_ptr->grid_array[yy][xx];
1946                         cc_ptr->info |= CAVE_GLOW;
1947
1948                         if (player_has_los_grid(cc_ptr))
1949                         {
1950                                 if (cc_ptr->m_idx) update_monster(cc_ptr->m_idx, FALSE);
1951
1952                                 note_spot(yy, xx);
1953
1954                                 lite_spot(yy, xx);
1955                         }
1956
1957                         update_local_illumination(yy, xx);
1958                 }
1959
1960                 if (p_ptr->special_defense & NINJA_S_STEALTH)
1961                 {
1962                         if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(FALSE);
1963                 }
1964         }
1965 }
1966
1967
1968 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
1969 {
1970         feature_type *f_ptr = &f_info[newfeat];
1971
1972         if (have_flag(f_ptr->flags, FF_CONVERT))
1973         {
1974                 switch (f_ptr->subtype)
1975                 {
1976                 case CONVERT_TYPE_FLOOR:
1977                         return feat_ground_type[randint0(100)];
1978                 case CONVERT_TYPE_WALL:
1979                         return feat_wall_type[randint0(100)];
1980                 case CONVERT_TYPE_INNER:
1981                         return feat_wall_inner;
1982                 case CONVERT_TYPE_OUTER:
1983                         return feat_wall_outer;
1984                 case CONVERT_TYPE_SOLID:
1985                         return feat_wall_solid;
1986                 case CONVERT_TYPE_STREAM1:
1987                         return d_info[p_ptr->dungeon_idx].stream1;
1988                 case CONVERT_TYPE_STREAM2:
1989                         return d_info[p_ptr->dungeon_idx].stream2;
1990                 default:
1991                         return newfeat;
1992                 }
1993         }
1994         else return newfeat;
1995 }
1996
1997
1998 /*
1999  * Take a feature, determine what that feature becomes
2000  * through applying the given action.
2001  */
2002 FEAT_IDX feat_state(FEAT_IDX feat, int action)
2003 {
2004         feature_type *f_ptr = &f_info[feat];
2005         int i;
2006
2007         /* Get the new feature */
2008         for (i = 0; i < MAX_FEAT_STATES; i++)
2009         {
2010                 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
2011         }
2012
2013         if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
2014
2015         return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
2016 }
2017
2018 /*
2019  * Takes a location and action and changes the feature at that
2020  * location through applying the given action.
2021  */
2022 void cave_alter_feat(POSITION y, POSITION x, int action)
2023 {
2024         /* Set old feature */
2025         FEAT_IDX oldfeat = current_floor_ptr->grid_array[y][x].feat;
2026
2027         /* Get the new feat */
2028         FEAT_IDX newfeat = feat_state(oldfeat, action);
2029
2030         /* No change */
2031         if (newfeat == oldfeat) return;
2032
2033         /* Set the new feature */
2034         cave_set_feat(y, x, newfeat);
2035
2036         if (!(feature_action_flags[action] & FAF_NO_DROP))
2037         {
2038                 feature_type *old_f_ptr = &f_info[oldfeat];
2039                 feature_type *f_ptr = &f_info[newfeat];
2040                 bool found = FALSE;
2041
2042                 /* Handle gold */
2043                 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
2044                 {
2045                         /* Place some gold */
2046                         place_gold(y, x);
2047                         found = TRUE;
2048                 }
2049
2050                 /* Handle item */
2051                 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - current_floor_ptr->dun_level / 2)))
2052                 {
2053                         /* Place object */
2054                         place_object(y, x, 0L);
2055                         found = TRUE;
2056                 }
2057
2058                 if (found && current_world_ptr->character_dungeon && player_can_see_bold(y, x))
2059                 {
2060                         msg_print(_("何かを発見した!", "You have found something!"));
2061                 }
2062         }
2063
2064         if (feature_action_flags[action] & FAF_CRASH_GLASS)
2065         {
2066                 feature_type *old_f_ptr = &f_info[oldfeat];
2067
2068                 if (have_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon)
2069                 {
2070                         project(PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
2071                                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
2072                 }
2073         }
2074 }
2075
2076
2077 /* Remove a mirror */
2078 void remove_mirror(POSITION y, POSITION x)
2079 {
2080         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
2081
2082         /* Remove the mirror */
2083         g_ptr->info &= ~(CAVE_OBJECT);
2084         g_ptr->mimic = 0;
2085
2086         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
2087         {
2088                 g_ptr->info &= ~(CAVE_GLOW);
2089                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
2090                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
2091
2092                 update_local_illumination(y, x);
2093         }
2094
2095         note_spot(y, x);
2096
2097         lite_spot(y, x);
2098 }
2099
2100
2101 /*
2102  *  Return TRUE if there is a mirror on the grid.
2103  */
2104 bool is_mirror_grid(grid_type *g_ptr)
2105 {
2106         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
2107                 return TRUE;
2108         else
2109                 return FALSE;
2110 }
2111
2112
2113 /*
2114  *  Return TRUE if there is a mirror on the grid.
2115  */
2116 bool is_glyph_grid(grid_type *g_ptr)
2117 {
2118         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
2119                 return TRUE;
2120         else
2121                 return FALSE;
2122 }
2123
2124
2125 /*
2126  *  Return TRUE if there is a mirror on the grid.
2127  */
2128 bool is_explosive_rune_grid(grid_type *g_ptr)
2129 {
2130         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
2131                 return TRUE;
2132         else
2133                 return FALSE;
2134 }
2135
2136 /*!
2137 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
2138 * @param m_idx モンスターID
2139 * @param y 移動先Y座標
2140 * @param x 移動先X座標
2141 * @param mode オプション
2142 * @return テレポート先として妥当ならばtrue
2143 */
2144 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
2145 {
2146         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
2147         grid_type    *g_ptr = &current_floor_ptr->grid_array[y][x];
2148         feature_type *f_ptr = &f_info[g_ptr->feat];
2149
2150         /* Require "teleportable" space */
2151         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
2152
2153         if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
2154         if (player_bold(y, x)) return FALSE;
2155
2156         /* Hack -- no teleport onto glyph of warding */
2157         if (is_glyph_grid(g_ptr)) return FALSE;
2158         if (is_explosive_rune_grid(g_ptr)) return FALSE;
2159
2160         if (!(mode & TELEPORT_PASSIVE))
2161         {
2162                 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
2163         }
2164
2165         return TRUE;
2166 }
2167
2168 /*!
2169 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
2170 * @param y 移動先Y座標
2171 * @param x 移動先X座標
2172 * @param mode オプション
2173 * @return テレポート先として妥当ならばtrue
2174 */
2175 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
2176 {
2177         grid_type    *g_ptr = &current_floor_ptr->grid_array[y][x];
2178         feature_type *f_ptr = &f_info[g_ptr->feat];
2179
2180         /* Require "teleportable" space */
2181         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
2182
2183         /* No magical teleporting into vaults and such */
2184         if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
2185
2186         if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
2187
2188         /* don't teleport on a trap. */
2189         if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
2190
2191         if (!(mode & TELEPORT_PASSIVE))
2192         {
2193                 if (!player_can_enter(g_ptr->feat, 0)) return FALSE;
2194
2195                 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
2196                 {
2197                         if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
2198                 }
2199
2200                 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
2201                 {
2202                         /* Always forbid deep lava */
2203                         if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
2204
2205                         /* Forbid shallow lava when the player don't have levitation */
2206                         if (!p_ptr->levitation) return FALSE;
2207                 }
2208
2209         }
2210
2211         return TRUE;
2212 }
2213
2214 /*!
2215  * @brief 地形は開くものであって、かつ開かれているかを返す /
2216  * Attempt to open the given chest at the given location
2217  * @param feat 地形ID
2218  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
2219  */
2220 bool is_open(FEAT_IDX feat)
2221 {
2222         return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
2223 }
2224
2225 /*!
2226  * @brief プレイヤーが地形踏破可能かを返す
2227  * @param feature 判定したい地形ID
2228  * @param mode 移動に関するオプションフラグ
2229  * @return 移動可能ならばTRUEを返す
2230  */
2231 bool player_can_enter(FEAT_IDX feature, BIT_FLAGS16 mode)
2232 {
2233         feature_type *f_ptr = &f_info[feature];
2234
2235         if (p_ptr->riding) return monster_can_cross_terrain(feature, &r_info[current_floor_ptr->m_list[p_ptr->riding].r_idx], mode | CEM_RIDING);
2236
2237         if (have_flag(f_ptr->flags, FF_PATTERN))
2238         {
2239                 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
2240         }
2241
2242         if (have_flag(f_ptr->flags, FF_CAN_FLY) && p_ptr->levitation) return TRUE;
2243         if (have_flag(f_ptr->flags, FF_CAN_SWIM) && p_ptr->can_swim) return TRUE;
2244         if (have_flag(f_ptr->flags, FF_CAN_PASS) && p_ptr->pass_wall) return TRUE;
2245
2246         if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;
2247
2248         return TRUE;
2249 }
2250