OSDN Git Service

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