OSDN Git Service

Revert "Merge branch 'master' of git.osdn.net:/gitroot/hengband/hengband"
[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 "world.h"
22 #include "projection.h"
23 #include "object-hook.h"
24 #include "generate.h"
25 #include "grid.h"
26 #include "trap.h"
27 #include "rooms.h"
28 #include "monster.h"
29 #include "quest.h"
30 #include "feature.h"
31 #include "monster-status.h"
32 #include "player-status.h"
33
34 static byte display_autopick; /*!< 自動拾い状態の設定フラグ */
35 static int match_autopick;
36 static object_type *autopick_obj; /*!< 各種自動拾い処理時に使うオブジェクトポインタ */
37 static int feat_priority; /*!< マップ縮小表示時に表示すべき地形の優先度を保管する */
38
39
40 /*!
41  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
42  * @return 配置に成功したらTRUEを返す
43  */
44 bool new_player_spot(void)
45 {
46         POSITION y = 0, x = 0;
47         int max_attempts = 10000;
48
49         grid_type *g_ptr;
50         feature_type *f_ptr;
51
52         /* Place the player */
53         while (max_attempts--)
54         {
55                 /* Pick a legal spot */
56                 y = (POSITION)rand_range(1, current_floor_ptr->height - 2);
57                 x = (POSITION)rand_range(1, current_floor_ptr->width - 2);
58
59                 g_ptr = &current_floor_ptr->grid_array[y][x];
60
61                 /* Must be a "naked" floor grid */
62                 if (g_ptr->m_idx) continue;
63                 if (current_floor_ptr->dun_level)
64                 {
65                         f_ptr = &f_info[g_ptr->feat];
66
67                         if (max_attempts > 5000) /* Rule 1 */
68                         {
69                                 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
70                         }
71                         else /* Rule 2 */
72                         {
73                                 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
74                                 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
75                         }
76
77                         /* Refuse to start on anti-teleport grids in dungeon */
78                         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
79                 }
80                 if (!player_can_enter(g_ptr->feat, 0)) continue;
81                 if (!in_bounds(y, x)) continue;
82
83                 /* Refuse to start on anti-teleport grids */
84                 if (g_ptr->info & (CAVE_ICKY)) continue;
85
86                 break;
87         }
88
89         if (max_attempts < 1) /* Should be -1, actually if we failed... */
90                 return FALSE;
91
92         /* Save the new player grid */
93         p_ptr->y = y;
94         p_ptr->x = x;
95
96         return TRUE;
97 }
98
99
100
101 /*!
102  * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
103  * @param y 配置を試みたいマスのY座標
104  * @param x 配置を試みたいマスのX座標
105  * @return なし
106  */
107 void place_random_stairs(POSITION y, POSITION x)
108 {
109         bool up_stairs = TRUE;
110         bool down_stairs = TRUE;
111         grid_type *g_ptr;
112         g_ptr = &current_floor_ptr->grid_array[y][x];
113         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) return;
114
115         /* Town */
116         if (!current_floor_ptr->dun_level) up_stairs = FALSE;
117
118         /* Ironman */
119         if (ironman_downward) up_stairs = FALSE;
120
121         /* Bottom */
122         if (current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth) down_stairs = FALSE;
123
124         /* Quest-level */
125         if (quest_number(current_floor_ptr->dun_level) && (current_floor_ptr->dun_level > 1)) down_stairs = FALSE;
126
127         /* We can't place both */
128         if (down_stairs && up_stairs)
129         {
130                 /* Choose a staircase randomly */
131                 if (randint0(100) < 50) up_stairs = FALSE;
132                 else down_stairs = FALSE;
133         }
134
135         /* Place the stairs */
136         if (up_stairs) place_up_stairs(y, x);
137         else if (down_stairs) place_down_stairs(y, x);
138 }
139
140 /*!
141  * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
142  * @param y ドアの配置を試みたいマスのY座標
143  * @param x ドアの配置を試みたいマスのX座標
144  * @param room 部屋に接している場合向けのドア生成か否か
145  * @return なし
146  */
147 void place_random_door(POSITION y, POSITION x, bool room)
148 {
149         int tmp, type;
150         FEAT_IDX feat = feat_none;
151         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
152
153         /* Initialize mimic info */
154         g_ptr->mimic = 0;
155
156         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
157         {
158                 place_floor_bold(y, x);
159                 return;
160         }
161
162         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
163                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
164                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
165
166         /* Choose an object */
167         tmp = randint0(1000);
168
169         /* Open doors (300/1000) */
170         if (tmp < 300)
171         {
172                 /* Create open door */
173                 feat = feat_door[type].open;
174         }
175
176         /* Broken doors (100/1000) */
177         else if (tmp < 400)
178         {
179                 /* Create broken door */
180                 feat = feat_door[type].broken;
181         }
182
183         /* Secret doors (200/1000) */
184         else if (tmp < 600)
185         {
186                 /* Create secret door */
187                 place_closed_door(y, x, type);
188
189                 if (type != DOOR_CURTAIN)
190                 {
191                         /* Hide. If on the edge of room, use outer wall. */
192                         g_ptr->mimic = room ? feat_wall_outer : feat_wall_type[randint0(100)];
193
194                         /* Floor type terrain cannot hide a door */
195                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
196                         {
197                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
198                                 {
199                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
200                                 }
201                                 g_ptr->mimic = 0;
202                         }
203                 }
204         }
205
206         /* Closed, locked, or stuck doors (400/1000) */
207         else place_closed_door(y, x, type);
208
209         if (tmp < 400)
210         {
211                 if (feat != feat_none)
212                 {
213                         set_cave_feat(y, x, feat);
214                 }
215                 else
216                 {
217                         place_floor_bold(y, x);
218                 }
219         }
220
221         delete_monster(y, x);
222 }
223
224 /*!
225  * @brief 所定の位置に各種の閉じたドアを配置する / Place a random type of normal door at the given location.
226  * @param y ドアの配置を試みたいマスのY座標
227  * @param x ドアの配置を試みたいマスのX座標
228  * @param type ドアの地形ID
229  * @return なし
230  */
231 void place_closed_door(POSITION y, POSITION x, int type)
232 {
233         int tmp;
234         FEAT_IDX feat = feat_none;
235
236         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
237         {
238                 place_floor_bold(y, x);
239                 return;
240         }
241
242         /* Choose an object */
243         tmp = randint0(400);
244
245         /* Closed doors (300/400) */
246         if (tmp < 300)
247         {
248                 /* Create closed door */
249                 feat = feat_door[type].closed;
250         }
251
252         /* Locked doors (99/400) */
253         else if (tmp < 399)
254         {
255                 /* Create locked door */
256                 feat = feat_locked_door_random(type);
257         }
258
259         /* Stuck doors (1/400) */
260         else
261         {
262                 /* Create jammed door */
263                 feat = feat_jammed_door_random(type);
264         }
265
266         if (feat != feat_none)
267         {
268                 cave_set_feat(y, x, feat);
269
270                 /* Now it is not floor */
271                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
272         }
273         else
274         {
275                 place_floor_bold(y, x);
276         }
277 }
278
279 /*!
280 * @brief 鍵のかかったドアを配置する
281 * @param y 配置したいフロアのY座標
282 * @param x 配置したいフロアのX座標
283 * @return なし
284 */
285 void place_locked_door(POSITION y, POSITION x)
286 {
287         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
288         {
289                 place_floor_bold(y, x);
290         }
291         else
292         {
293                 set_cave_feat(y, x, feat_locked_door_random((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
294                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
295                 delete_monster(y, x);
296         }
297 }
298
299
300 /*!
301 * @brief 隠しドアを配置する
302 * @param y 配置したいフロアのY座標
303 * @param x 配置したいフロアのX座標
304 * @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
305 * @return なし
306 */
307 void place_secret_door(POSITION y, POSITION x, int type)
308 {
309         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
310         {
311                 place_floor_bold(y, x);
312         }
313         else
314         {
315                 grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
316
317                 if (type == DOOR_DEFAULT)
318                 {
319                         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
320                                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
321                                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
322                 }
323
324                 /* Create secret door */
325                 place_closed_door(y, x, type);
326
327                 if (type != DOOR_CURTAIN)
328                 {
329                         /* Hide by inner wall because this is used in rooms only */
330                         g_ptr->mimic = feat_wall_inner;
331
332                         /* Floor type terrain cannot hide a door */
333                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
334                         {
335                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
336                                 {
337                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
338                                 }
339                                 g_ptr->mimic = 0;
340                         }
341                 }
342
343                 g_ptr->info &= ~(CAVE_FLOOR);
344                 delete_monster(y, x);
345         }
346 }
347
348 /*
349  * Routine used by the random vault creators to add a door to a location
350  * Note that range checking has to be done in the calling routine.
351  *
352  * The doors must be INSIDE the allocated region.
353  */
354 void add_door(POSITION x, POSITION y)
355 {
356         /* Need to have a wall in the center square */
357         if (!is_outer_bold(y, x)) return;
358
359         /* look at:
360         *  x#x
361         *  .#.
362         *  x#x
363         *
364         *  where x=don't care
365         *  .=floor, #=wall
366         */
367
368         if (is_floor_bold(y - 1, x) && is_floor_bold(y + 1, x) &&
369                 (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
370         {
371                 /* secret door */
372                 place_secret_door(y, x, DOOR_DEFAULT);
373
374                 /* set boundarys so don't get wide doors */
375                 place_solid_bold(y, x - 1);
376                 place_solid_bold(y, x + 1);
377         }
378
379
380         /* look at:
381         *  x#x
382         *  .#.
383         *  x#x
384         *
385         *  where x = don't care
386         *  .=floor, #=wall
387         */
388         if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
389                 is_floor_bold(y, x - 1) && is_floor_bold(y, x + 1))
390         {
391                 /* secret door */
392                 place_secret_door(y, x, DOOR_DEFAULT);
393
394                 /* set boundarys so don't get wide doors */
395                 place_solid_bold(y - 1, x);
396                 place_solid_bold(y + 1, x);
397         }
398 }
399
400 /*!
401 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
402 * @param y1 基準となるマスのY座標
403 * @param x1 基準となるマスのX座標
404 * @return 通路の数
405 * @note Assumes "in_bounds(y1, x1)"
406 * @details
407 * XXX XXX This routine currently only counts actual "empty floor"\n
408 * grids which are not in rooms.  We might want to also count stairs,\n
409 * open doors, closed doors, etc.
410 */
411 static int next_to_corr(POSITION y1, POSITION x1)
412 {
413         int i, k = 0;
414         POSITION y, x;
415
416         grid_type *g_ptr;
417
418         /* Scan adjacent grids */
419         for (i = 0; i < 4; i++)
420         {
421                 /* Extract the location */
422                 y = y1 + ddy_ddd[i];
423                 x = x1 + ddx_ddd[i];
424                 g_ptr = &current_floor_ptr->grid_array[y][x];
425
426                 /* Skip non floors */
427                 if (cave_have_flag_grid(g_ptr, FF_WALL)) continue;
428
429                 /* Skip non "empty floor" grids */
430                 if (!is_floor_grid(g_ptr))
431                         continue;
432
433                 /* Skip grids inside rooms */
434                 if (g_ptr->info & (CAVE_ROOM)) continue;
435
436                 /* Count these grids */
437                 k++;
438         }
439
440         /* Return the number of corridors */
441         return (k);
442 }
443
444 /*!
445 * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
446 * @param y 判定を行いたいマスのY座標
447 * @param x 判定を行いたいマスのX座標
448 * @return ドアを設置可能ならばTRUEを返す
449 * @note Assumes "in_bounds(y1, x1)"
450 * @details
451 * \n
452 * Assumes "in_bounds(y, x)"\n
453 */
454 static bool possible_doorway(POSITION y, POSITION x)
455 {
456         /* Count the adjacent corridors */
457         if (next_to_corr(y, x) >= 2)
458         {
459                 /* Check Vertical */
460                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
461                         cave_have_flag_bold(y + 1, x, FF_WALL))
462                 {
463                         return (TRUE);
464                 }
465
466                 /* Check Horizontal */
467                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
468                         cave_have_flag_bold(y, x + 1, FF_WALL))
469                 {
470                         return (TRUE);
471                 }
472         }
473
474         /* No doorway */
475         return (FALSE);
476 }
477
478 /*!
479 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
480 * @param y 設置を行いたいマスのY座標
481 * @param x 設置を行いたいマスのX座標
482 * @return なし
483 */
484 void try_door(POSITION y, POSITION x)
485 {       if (!in_bounds(y, x)) return;
486
487         /* Ignore walls */
488         if (cave_have_flag_bold(y, x, FF_WALL)) return;
489
490         /* Ignore room grids */
491         if (current_floor_ptr->grid_array[y][x].info & (CAVE_ROOM)) return;
492
493         /* Occasional door (if allowed) */
494         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS))
495         {
496                 /* Place a door */
497                 place_random_door(y, x, FALSE);
498         }
499 }
500
501
502 /*!
503  * @brief 長方形の空洞を生成する / Make an empty square floor, for the middle of rooms
504  * @param x1 長方形の左端X座標(-1)
505  * @param x2 長方形の右端X座標(+1)
506  * @param y1 長方形の上端Y座標(-1)
507  * @param y2 長方形の下端Y座標(+1)
508  * @param light 照明の有無
509  * @return なし
510  */
511 void place_floor(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
512 {
513         POSITION x, y;
514
515         /* Place a full floor under the room */
516         for (y = y1 - 1; y <= y2 + 1; y++)
517         {
518                 for (x = x1 - 1; x <= x2 + 1; x++)
519                 {
520                         place_floor_bold(y, x);
521                         add_cave_info(y, x, CAVE_ROOM);
522                         if (light) add_cave_info(y, x, CAVE_GLOW);
523                 }
524         }
525 }
526
527
528 /*!
529  * @brief 長方形の部屋を生成する / Make an empty square room, only floor and wall grids
530  * @param x1 長方形の左端X座標(-1)
531  * @param x2 長方形の右端X座標(+1)
532  * @param y1 長方形の上端Y座標(-1)
533  * @param y2 長方形の下端Y座標(+1)
534  * @param light 照明の有無
535  * @return なし
536  */
537 void place_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
538 {
539         POSITION y, x;
540
541         place_floor(x1, x2, y1, y2, light);
542
543         /* Walls around the room */
544         for (y = y1 - 1; y <= y2 + 1; y++)
545         {
546                 place_outer_bold(y, x1 - 1);
547                 place_outer_bold(y, x2 + 1);
548         }
549         for (x = x1 - 1; x <= x2 + 1; x++)
550         {
551                 place_outer_bold(y1 - 1, x);
552                 place_outer_bold(y2 + 1, x);
553         }
554 }
555
556
557 /*!
558  * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
559  * @param y 配置したい中心マスのY座標
560  * @param x 配置したい中心マスのX座標
561  * @param num 配置したい数
562  * @return なし
563  * @details
564  * Only really called by some of the "vault" routines.
565  */
566 void vault_objects(POSITION y, POSITION x, int num)
567 {
568         int dummy = 0;
569         int i = 0, j = y, k = x;
570
571         grid_type *g_ptr;
572
573
574         /* Attempt to place 'num' objects */
575         for (; num > 0; --num)
576         {
577                 /* Try up to 11 spots looking for empty space */
578                 for (i = 0; i < 11; ++i)
579                 {
580                         /* Pick a random location */
581                         while (dummy < SAFE_MAX_ATTEMPTS)
582                         {
583                                 j = rand_spread(y, 2);
584                                 k = rand_spread(x, 3);
585                                 dummy++;
586                                 if (!in_bounds(j, k)) continue;
587                                 break;
588                         }
589
590                         if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
591                         {
592                                 msg_print(_("警告!地下室のアイテムを配置できません!", "Warning! Could not place vault object!"));
593                         }
594
595                         /* Require "clean" floor space */
596                         g_ptr = &current_floor_ptr->grid_array[j][k];
597                         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) continue;
598
599                         if (randint0(100) < 75)
600                         {
601                                 place_object(j, k, 0L);
602                         }
603                         else
604                         {
605                                 place_gold(j, k);
606                         }
607
608                         /* Placement accomplished */
609                         break;
610                 }
611         }
612 }
613
614 /*!
615  * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
616  * @param y トラップを配置したいマスの中心Y座標
617  * @param x トラップを配置したいマスの中心X座標
618  * @param yd Y方向の配置分散マス数
619  * @param xd X方向の配置分散マス数
620  * @return なし
621  * @details
622  * Only really called by some of the "vault" routines.
623  */
624 void vault_trap_aux(POSITION y, POSITION x, POSITION yd, POSITION xd)
625 {
626         int count = 0, y1 = y, x1 = x;
627         int dummy = 0;
628
629         grid_type *g_ptr;
630
631         /* Place traps */
632         for (count = 0; count <= 5; count++)
633         {
634                 /* Get a location */
635                 while (dummy < SAFE_MAX_ATTEMPTS)
636                 {
637                         y1 = rand_spread(y, yd);
638                         x1 = rand_spread(x, xd);
639                         dummy++;
640                         if (!in_bounds(y1, x1)) continue;
641                         break;
642                 }
643
644                 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
645                 {
646                         msg_print(_("警告!地下室のトラップを配置できません!", "Warning! Could not place vault trap!"));
647                 }
648
649                 /* Require "naked" floor grids */
650                 g_ptr = &current_floor_ptr->grid_array[y1][x1];
651                 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
652
653                 /* Place the trap */
654                 place_trap(y1, x1);
655
656                 break;
657         }
658 }
659
660 /*!
661  * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
662  * @param y トラップを配置したいマスの中心Y座標
663  * @param x トラップを配置したいマスの中心X座標
664  * @param yd Y方向の配置分散マス数
665  * @param xd X方向の配置分散マス数
666  * @param num 配置したいトラップの数
667  * @return なし
668  * @details
669  * Only really called by some of the "vault" routines.
670  */
671 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
672 {
673         int i;
674
675         for (i = 0; i < num; i++)
676         {
677                 vault_trap_aux(y, x, yd, xd);
678         }
679 }
680
681 /*!
682  * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
683  * @param y1 モンスターを配置したいマスの中心Y座標
684  * @param x1 モンスターを配置したいマスの中心X座標
685  * @param num 配置したいモンスターの数
686  * @return なし
687  * @details
688  * Only really called by some of the "vault" routines.
689  */
690 void vault_monsters(POSITION y1, POSITION x1, int num)
691 {
692         int k, i;
693         POSITION y, x;
694         grid_type *g_ptr;
695
696         /* Try to summon "num" monsters "near" the given location */
697         for (k = 0; k < num; k++)
698         {
699                 /* Try nine locations */
700                 for (i = 0; i < 9; i++)
701                 {
702                         int d = 1;
703
704                         /* Pick a nearby location */
705                         scatter(&y, &x, y1, x1, d, 0);
706
707                         /* Require "empty" floor grids */
708                         g_ptr = &current_floor_ptr->grid_array[y][x];
709                         if (!cave_empty_grid(g_ptr)) continue;
710
711                         /* Place the monster (allow groups) */
712                         current_floor_ptr->monster_level = current_floor_ptr->base_level + 2;
713                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
714                         current_floor_ptr->monster_level = current_floor_ptr->base_level;
715                 }
716         }
717 }
718
719 /*!
720  * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor.  (Includes range checking.)
721  * @param x チェックするマスのX座標
722  * @param y チェックするマスのY座標
723  * @return 床系地形ならばTRUE
724  */
725 bool get_is_floor(POSITION x, POSITION y)
726 {
727         if (!in_bounds(y, x))
728         {
729                 /* Out of bounds */
730                 return (FALSE);
731         }
732
733         /* Do the real check */
734         if (is_floor_bold(y, x)) return (TRUE);
735
736         return (FALSE);
737 }
738
739 /*!
740  * @brief 指定のマスを床地形に変える / Set a square to be floor.  (Includes range checking.)
741  * @param x 地形を変えたいマスのX座標
742  * @param y 地形を変えたいマスのY座標
743  * @return なし
744  */
745 void set_floor(POSITION x, POSITION y)
746 {
747         if (!in_bounds(y, x))
748         {
749                 /* Out of bounds */
750                 return;
751         }
752
753         if (current_floor_ptr->grid_array[y][x].info & CAVE_ROOM)
754         {
755                 /* A room border don't touch. */
756                 return;
757         }
758
759         /* Set to be floor if is a wall (don't touch lakes). */
760         if (is_extra_bold(y, x))
761                 place_floor_bold(y, x);
762 }
763
764 /*!
765  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
766  * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
767  * @return なし
768  */
769 void place_bound_perm_wall(grid_type *g_ptr)
770 {
771         if (bound_walls_perm)
772         {
773                 /* Clear boundary mimic */
774                 g_ptr->mimic = 0;
775         }
776         else
777         {
778                 feature_type *f_ptr = &f_info[g_ptr->feat];
779
780                 /* Hack -- Decline boundary walls with known treasure  */
781                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
782                         !have_flag(f_ptr->flags, FF_SECRET))
783                         g_ptr->feat = feat_state(g_ptr->feat, FF_ENSECRET);
784
785                 /* Set boundary mimic */
786                 g_ptr->mimic = g_ptr->feat;
787         }
788
789         /* Add "solid" perma-wall */
790         place_solid_perm_grid(g_ptr);
791 }
792
793
794 /*!
795  * @brief 2点間の距離をニュートン・ラプソン法で算出する / Distance between two points via Newton-Raphson technique
796  * @param y1 1点目のy座標
797  * @param x1 1点目のx座標
798  * @param y2 2点目のy座標
799  * @param x2 2点目のx座標
800  * @return 2点間の距離
801  */
802 POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
803 {
804         POSITION dy = (y1 > y2) ? (y1 - y2) : (y2 - y1);
805         POSITION dx = (x1 > x2) ? (x1 - x2) : (x2 - x1);
806
807         /* Squared distance */
808         POSITION target = (dy * dy) + (dx * dx);
809
810         /* Approximate distance: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2 */
811         POSITION d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
812
813         POSITION err;
814
815         /* Simple case */
816         if (!dy || !dx) return d;
817
818         while (1)
819         {
820                 /* Approximate error */
821                 err = (target - d * d) / (2 * d);
822
823                 /* No error - we are done */
824                 if (!err) break;
825
826                 /* Adjust distance */
827                 d += err;
828         }
829
830         return d;
831 }
832
833
834 /*!
835  * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
836  * @param g_ptr マス構造体の参照ポインタ
837  * @return 看破済みの罠があるならTRUEを返す。
838  */
839 bool is_known_trap(grid_type *g_ptr)
840 {
841         if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
842                 is_trap(g_ptr->feat)) return TRUE;
843         else
844                 return FALSE;
845 }
846
847
848
849 /*!
850  * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
851  * @param g_ptr マス構造体の参照ポインタ
852  * @return 隠されたドアがあるならTRUEを返す。
853  */
854 bool is_hidden_door(grid_type *g_ptr)
855 {
856         if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
857                 is_closed_door(g_ptr->feat))
858                 return TRUE;
859         else
860                 return FALSE;
861 }
862
863 /*!
864  * @brief LOS(Line Of Sight / 視線が通っているか)の判定を行う。
865  * @param y1 始点のy座標
866  * @param x1 始点のx座標
867  * @param y2 終点のy座標
868  * @param x2 終点のx座標
869  * @return LOSが通っているならTRUEを返す。
870  * @details
871  * A simple, fast, integer-based line-of-sight algorithm.  By Joseph Hall,\n
872  * 4116 Brewster Drive, Raleigh NC 27606.  Email to jnh@ecemwl.ncsu.edu.\n
873  *\n
874  * Returns TRUE if a line of sight can be traced from (x1,y1) to (x2,y2).\n
875  *\n
876  * The LOS begins at the center of the tile (x1,y1) and ends at the center of\n
877  * the tile (x2,y2).  If los() is to return TRUE, all of the tiles this line\n
878  * passes through must be floor tiles, except for (x1,y1) and (x2,y2).\n
879  *\n
880  * We assume that the "mathematical corner" of a non-floor tile does not\n
881  * block line of sight.\n
882  *\n
883  * Because this function uses (short) ints for all calculations, overflow may\n
884  * occur if dx and dy exceed 90.\n
885  *\n
886  * Once all the degenerate cases are eliminated, the values "qx", "qy", and\n
887  * "m" are multiplied by a scale factor "f1 = abs(dx * dy * 2)", so that\n
888  * we can use integer arithmetic.\n
889  *\n
890  * We travel from start to finish along the longer axis, starting at the border\n
891  * between the first and second tiles, where the y offset = .5 * slope, taking\n
892  * into account the scale factor.  See below.\n
893  *\n
894  * Also note that this function and the "move towards target" code do NOT\n
895  * share the same properties.  Thus, you can see someone, target them, and\n
896  * then fire a bolt at them, but the bolt may hit a wall, not them.  However\n,
897  * by clever choice of target locations, you can sometimes throw a "curve".\n
898  *\n
899  * Note that "line of sight" is not "reflexive" in all cases.\n
900  *\n
901  * Use the "projectable()" routine to test "spell/missile line of sight".\n
902  *\n
903  * Use the "update_view()" function to determine player line-of-sight.\n
904  */
905 bool los(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
906 {
907         /* Delta */
908         POSITION dx, dy;
909
910         /* Absolute */
911         POSITION ax, ay;
912
913         /* Signs */
914         POSITION sx, sy;
915
916         /* Fractions */
917         POSITION qx, qy;
918
919         /* Scanners */
920         POSITION tx, ty;
921
922         /* Scale factors */
923         POSITION f1, f2;
924
925         /* Slope, or 1/Slope, of LOS */
926         POSITION m;
927
928
929         /* Extract the offset */
930         dy = y2 - y1;
931         dx = x2 - x1;
932
933         /* Extract the absolute offset */
934         ay = ABS(dy);
935         ax = ABS(dx);
936
937
938         /* Handle adjacent (or identical) grids */
939         if ((ax < 2) && (ay < 2)) return TRUE;
940
941
942         /* Paranoia -- require "safe" origin */
943         /* if (!in_bounds(y1, x1)) return FALSE; */
944         /* if (!in_bounds(y2, x2)) return FALSE; */
945
946
947         /* Directly South/North */
948         if (!dx)
949         {
950                 /* South -- check for walls */
951                 if (dy > 0)
952                 {
953                         for (ty = y1 + 1; ty < y2; ty++)
954                         {
955                                 if (!cave_los_bold(ty, x1)) return FALSE;
956                         }
957                 }
958
959                 /* North -- check for walls */
960                 else
961                 {
962                         for (ty = y1 - 1; ty > y2; ty--)
963                         {
964                                 if (!cave_los_bold(ty, x1)) return FALSE;
965                         }
966                 }
967
968                 /* Assume los */
969                 return TRUE;
970         }
971
972         /* Directly East/West */
973         if (!dy)
974         {
975                 /* East -- check for walls */
976                 if (dx > 0)
977                 {
978                         for (tx = x1 + 1; tx < x2; tx++)
979                         {
980                                 if (!cave_los_bold(y1, tx)) return FALSE;
981                         }
982                 }
983
984                 /* West -- check for walls */
985                 else
986                 {
987                         for (tx = x1 - 1; tx > x2; tx--)
988                         {
989                                 if (!cave_los_bold(y1, tx)) return FALSE;
990                         }
991                 }
992
993                 /* Assume los */
994                 return TRUE;
995         }
996
997
998         /* Extract some signs */
999         sx = (dx < 0) ? -1 : 1;
1000         sy = (dy < 0) ? -1 : 1;
1001
1002
1003         /* Vertical "knights" */
1004         if (ax == 1)
1005         {
1006                 if (ay == 2)
1007                 {
1008                         if (cave_los_bold(y1 + sy, x1)) return TRUE;
1009                 }
1010         }
1011
1012         /* Horizontal "knights" */
1013         else if (ay == 1)
1014         {
1015                 if (ax == 2)
1016                 {
1017                         if (cave_los_bold(y1, x1 + sx)) return TRUE;
1018                 }
1019         }
1020
1021
1022         /* Calculate scale factor div 2 */
1023         f2 = (ax * ay);
1024
1025         /* Calculate scale factor */
1026         f1 = f2 << 1;
1027
1028
1029         /* Travel horizontally */
1030         if (ax >= ay)
1031         {
1032                 /* Let m = dy / dx * 2 * (dy * dx) = 2 * dy * dy */
1033                 qy = ay * ay;
1034                 m = qy << 1;
1035
1036                 tx = x1 + sx;
1037
1038                 /* Consider the special case where slope == 1. */
1039                 if (qy == f2)
1040                 {
1041                         ty = y1 + sy;
1042                         qy -= f1;
1043                 }
1044                 else
1045                 {
1046                         ty = y1;
1047                 }
1048
1049                 /* Note (below) the case (qy == f2), where */
1050                 /* the LOS exactly meets the corner of a tile. */
1051                 while (x2 - tx)
1052                 {
1053                         if (!cave_los_bold(ty, tx)) return FALSE;
1054
1055                         qy += m;
1056
1057                         if (qy < f2)
1058                         {
1059                                 tx += sx;
1060                         }
1061                         else if (qy > f2)
1062                         {
1063                                 ty += sy;
1064                                 if (!cave_los_bold(ty, tx)) return FALSE;
1065                                 qy -= f1;
1066                                 tx += sx;
1067                         }
1068                         else
1069                         {
1070                                 ty += sy;
1071                                 qy -= f1;
1072                                 tx += sx;
1073                         }
1074                 }
1075         }
1076
1077         /* Travel vertically */
1078         else
1079         {
1080                 /* Let m = dx / dy * 2 * (dx * dy) = 2 * dx * dx */
1081                 qx = ax * ax;
1082                 m = qx << 1;
1083
1084                 ty = y1 + sy;
1085
1086                 if (qx == f2)
1087                 {
1088                         tx = x1 + sx;
1089                         qx -= f1;
1090                 }
1091                 else
1092                 {
1093                         tx = x1;
1094                 }
1095
1096                 /* Note (below) the case (qx == f2), where */
1097                 /* the LOS exactly meets the corner of a tile. */
1098                 while (y2 - ty)
1099                 {
1100                         if (!cave_los_bold(ty, tx)) return FALSE;
1101
1102                         qx += m;
1103
1104                         if (qx < f2)
1105                         {
1106                                 ty += sy;
1107                         }
1108                         else if (qx > f2)
1109                         {
1110                                 tx += sx;
1111                                 if (!cave_los_bold(ty, tx)) return FALSE;
1112                                 qx -= f1;
1113                                 ty += sy;
1114                         }
1115                         else
1116                         {
1117                                 tx += sx;
1118                                 qx -= f1;
1119                                 ty += sy;
1120                         }
1121                 }
1122         }
1123
1124         /* Assume los */
1125         return TRUE;
1126 }
1127
1128 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
1129
1130
1131 /*!
1132  * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
1133  * @param y y座標
1134  * @param x x座標
1135  * @return 指定された座標に照明がかかっているならTRUEを返す。。
1136  */
1137 static bool check_local_illumination(POSITION y, POSITION x)
1138 {
1139         /* Hack -- move towards player */
1140         POSITION yy = (y < p_ptr->y) ? (y + 1) : (y > p_ptr->y) ? (y - 1) : y;
1141         POSITION xx = (x < p_ptr->x) ? (x + 1) : (x > p_ptr->x) ? (x - 1) : x;
1142
1143         /* Check for "local" illumination */
1144
1145 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
1146
1147         /* Check for "complex" illumination */
1148         if ((feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[yy][xx])) &&
1149                 (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
1150                 (feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[y][xx])) &&
1151                 (current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
1152                         (feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[yy][x])) &&
1153                 (current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
1154         {
1155                 return TRUE;
1156         }
1157         else return FALSE;
1158
1159 #else /* COMPLEX_WALL_ILLUMINATION */
1160
1161         /* Check for "simple" illumination */
1162         return (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
1163
1164 #endif /* COMPLEX_WALL_ILLUMINATION */
1165 }
1166
1167
1168 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
1169 #define update_local_illumination_aux(Y, X) \
1170 { \
1171         if (player_has_los_bold((Y), (X))) \
1172         { \
1173                 /* Update the monster */ \
1174                 if (current_floor_ptr->grid_array[(Y)][(X)].m_idx) update_monster(current_floor_ptr->grid_array[(Y)][(X)].m_idx, FALSE); \
1175 \
1176                 /* Notice and redraw */ \
1177                 note_spot((Y), (X)); \
1178                 lite_spot((Y), (X)); \
1179         } \
1180 }
1181
1182 /*!
1183  * @brief 指定された座標の照明状態を更新する / Update "local" illumination
1184  * @param y y座標
1185  * @param x x座標
1186  * @return なし
1187  */
1188 void update_local_illumination(POSITION y, POSITION x)
1189 {
1190         int i;
1191         POSITION yy, xx;
1192
1193         if (!in_bounds(y, x)) return;
1194
1195 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
1196
1197         if ((y != p_ptr->y) && (x != p_ptr->x))
1198         {
1199                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1200                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1201                 update_local_illumination_aux(yy, xx);
1202                 update_local_illumination_aux(y, xx);
1203                 update_local_illumination_aux(yy, x);
1204         }
1205         else if (x != p_ptr->x) /* y == p_ptr->y */
1206         {
1207                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1208                 for (i = -1; i <= 1; i++)
1209                 {
1210                         yy = y + i;
1211                         update_local_illumination_aux(yy, xx);
1212                 }
1213                 yy = y - 1;
1214                 update_local_illumination_aux(yy, x);
1215                 yy = y + 1;
1216                 update_local_illumination_aux(yy, x);
1217         }
1218         else if (y != p_ptr->y) /* x == p_ptr->x */
1219         {
1220                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1221                 for (i = -1; i <= 1; i++)
1222                 {
1223                         xx = x + i;
1224                         update_local_illumination_aux(yy, xx);
1225                 }
1226                 xx = x - 1;
1227                 update_local_illumination_aux(y, xx);
1228                 xx = x + 1;
1229                 update_local_illumination_aux(y, xx);
1230         }
1231         else /* Player's grid */
1232         {
1233                 for (i = 0; i < 8; i++)
1234                 {
1235                         yy = y + ddy_cdd[i];
1236                         xx = x + ddx_cdd[i];
1237                         update_local_illumination_aux(yy, xx);
1238                 }
1239         }
1240
1241 #else /* COMPLEX_WALL_ILLUMINATION */
1242
1243         if ((y != p_ptr->y) && (x != p_ptr->x))
1244         {
1245                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1246                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1247                 update_local_illumination_aux(yy, xx);
1248         }
1249         else if (x != p_ptr->x) /* y == p_ptr->y */
1250         {
1251                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
1252                 for (i = -1; i <= 1; i++)
1253                 {
1254                         yy = y + i;
1255                         update_local_illumination_aux(yy, xx);
1256                 }
1257         }
1258         else if (y != p_ptr->y) /* x == p_ptr->x */
1259         {
1260                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
1261                 for (i = -1; i <= 1; i++)
1262                 {
1263                         xx = x + i;
1264                         update_local_illumination_aux(yy, xx);
1265                 }
1266         }
1267         else /* Player's grid */
1268         {
1269                 for (i = 0; i < 8; i++)
1270                 {
1271                         yy = y + ddy_cdd[i];
1272                         xx = x + ddx_cdd[i];
1273                         update_local_illumination_aux(yy, xx);
1274                 }
1275         }
1276
1277 #endif /* COMPLEX_WALL_ILLUMINATION */
1278 }
1279
1280
1281 /*!
1282  * @brief 指定された座標をプレイヤーが視覚に収められるかを返す。 / Can the player "see" the given grid in detail?
1283  * @param y y座標
1284  * @param x x座標
1285  * @return 視覚に収められる状態ならTRUEを返す
1286  * @details
1287  * He must have vision, illumination, and line of sight.\n
1288  * \n
1289  * Note -- "CAVE_LITE" is only set if the "torch" has "los()".\n
1290  * So, given "CAVE_LITE", we know that the grid is "fully visible".\n
1291  *\n
1292  * Note that "CAVE_GLOW" makes little sense for a wall, since it would mean\n
1293  * that a wall is visible from any direction.  That would be odd.  Except\n
1294  * under wizard light, which might make sense.  Thus, for walls, we require\n
1295  * not only that they be "CAVE_GLOW", but also, that they be adjacent to a\n
1296  * grid which is not only "CAVE_GLOW", but which is a non-wall, and which is\n
1297  * in line of sight of the player.\n
1298  *\n
1299  * This extra check is expensive, but it provides a more "correct" semantics.\n
1300  *\n
1301  * Note that we should not run this check on walls which are "outer walls" of\n
1302  * the dungeon, or we will induce a memory fault, but actually verifying all\n
1303  * of the locations would be extremely expensive.\n
1304  *\n
1305  * Thus, to speed up the function, we assume that all "perma-walls" which are\n
1306  * "CAVE_GLOW" are "illuminated" from all sides.  This is correct for all cases\n
1307  * except "vaults" and the "buildings" in town.  But the town is a hack anyway,\n
1308  * and the player has more important things on his mind when he is attacking a\n
1309  * monster vault.  It is annoying, but an extremely important optimization.\n
1310  *\n
1311  * Note that "glowing walls" are only considered to be "illuminated" if the\n
1312  * grid which is next to the wall in the direction of the player is also a\n
1313  * "glowing" grid.  This prevents the player from being able to "see" the\n
1314  * walls of illuminated rooms from a corridor outside the room.\n
1315  */
1316 bool player_can_see_bold(POSITION y, POSITION x)
1317 {
1318         grid_type *g_ptr;
1319
1320         /* Blind players see nothing */
1321         if (p_ptr->blind) return FALSE;
1322
1323         g_ptr = &current_floor_ptr->grid_array[y][x];
1324
1325         /* Note that "torch-lite" yields "illumination" */
1326         if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) return TRUE;
1327
1328         /* Require line of sight to the grid */
1329         if (!player_has_los_bold(y, x)) return FALSE;
1330
1331         /* Noctovision of Ninja */
1332         if (p_ptr->see_nocto) return TRUE;
1333
1334         /* Require "perma-lite" of the grid */
1335         if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) return FALSE;
1336
1337         /* Feature code (applying "mimic" field) */
1338         /* Floors are simple */
1339         if (feat_supports_los(get_feat_mimic(g_ptr))) return TRUE;
1340
1341         /* Check for "local" illumination */
1342         return check_local_illumination(y, x);
1343 }
1344
1345 /*!
1346  * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
1347  * @return 視覚に収められていないならTRUEを返す
1348  * @details player_can_see_bold()関数の返り値の否定を返している。
1349  */
1350 bool no_lite(void)
1351 {
1352         return (!player_can_see_bold(p_ptr->y, p_ptr->x));
1353 }
1354
1355
1356 /*!
1357  * @brief 指定された座標が地震や階段生成の対象となるマスかを返す。 / Determine if a given location may be "destroyed"
1358  * @param y y座標
1359  * @param x x座標
1360  * @return 各種の変更が可能ならTRUEを返す。
1361  * @details
1362  * 条件は永久地形でなく、なおかつ該当のマスにアーティファクトが存在しないか、である。英語の旧コメントに反して*破壊*の抑止判定には現在使われていない。
1363  */
1364 bool cave_valid_bold(POSITION y, POSITION x)
1365 {
1366         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1367         OBJECT_IDX this_o_idx, next_o_idx = 0;
1368
1369         /* Forbid perma-grids */
1370         if (cave_perma_grid(g_ptr)) return (FALSE);
1371
1372         /* Check objects */
1373         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1374         {
1375                 object_type *o_ptr;
1376                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
1377                 next_o_idx = o_ptr->next_o_idx;
1378
1379                 /* Forbid artifact grids */
1380                 if (object_is_artifact(o_ptr)) return (FALSE);
1381         }
1382
1383         /* Accept */
1384         return (TRUE);
1385 }
1386
1387
1388
1389
1390 /*!
1391  * 一般的にモンスターシンボルとして扱われる記号を定義する(幻覚処理向け) / Hack -- Legal monster codes
1392  */
1393 static char image_monster_hack[] = \
1394 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1395
1396 /*!
1397  * 一般的にオブジェクトシンボルとして扱われる記号を定義する(幻覚処理向け) /  Hack -- Legal object codes
1398  */
1399 static char image_object_hack[] = "?/|\\\"!$()_-=[]{},~";
1400
1401 /*!
1402  * @brief モンスターの表示を幻覚状態に差し替える / Mega-Hack -- Hallucinatory monster
1403  * @param ap 本来の色
1404  * @param cp 本来のシンボル
1405  * @return なし
1406  */
1407 static void image_monster(TERM_COLOR *ap, SYMBOL_CODE *cp)
1408 {
1409         /* Random symbol from set above */
1410         if (use_graphics)
1411         {
1412                 monster_race *r_ptr = &r_info[randint1(max_r_idx - 1)];
1413
1414                 *cp = r_ptr->x_char;
1415                 *ap = r_ptr->x_attr;
1416         }
1417         else
1418                 /* Text mode */
1419         {
1420                 *cp = (one_in_(25) ?
1421                         image_object_hack[randint0(sizeof(image_object_hack) - 1)] :
1422                         image_monster_hack[randint0(sizeof(image_monster_hack) - 1)]);
1423
1424                 /* Random color */
1425                 *ap = randint1(15);
1426         }
1427 }
1428
1429 /*!
1430  * @brief オブジェクトの表示を幻覚状態に差し替える / Hallucinatory object
1431  * @param ap 本来の色
1432  * @param cp 本来のシンボル
1433  * @return なし
1434  */
1435 static void image_object(TERM_COLOR *ap, SYMBOL_CODE *cp)
1436 {
1437         if (use_graphics)
1438         {
1439                 object_kind *k_ptr = &k_info[randint1(max_k_idx - 1)];
1440
1441                 *cp = k_ptr->x_char;
1442                 *ap = k_ptr->x_attr;
1443         }
1444         else
1445         {
1446                 int n = sizeof(image_object_hack) - 1;
1447
1448                 *cp = image_object_hack[randint0(n)];
1449
1450                 /* Random color */
1451                 *ap = randint1(15);
1452         }
1453 }
1454
1455
1456 /*!
1457  * @brief オブジェクト&モンスターの表示を幻覚状態に差し替える / Hack -- Random hallucination
1458  * @param ap 本来の色
1459  * @param cp 本来のシンボル
1460  * @return なし
1461  */
1462 static void image_random(TERM_COLOR *ap, SYMBOL_CODE *cp)
1463 {
1464         /* Normally, assume monsters */
1465         if (randint0(100) < 75)
1466         {
1467                 image_monster(ap, cp);
1468         }
1469
1470         /* Otherwise, assume objects */
1471         else
1472         {
1473                 image_object(ap, cp);
1474         }
1475 }
1476
1477 /*!
1478  * 照明の表現を行うための色合いの関係を{暗闇時, 照明時} で定義する /
1479  * This array lists the effects of "brightness" on various "base" colours.\n
1480  *\n
1481  * This is used to do dynamic lighting effects in ascii :-)\n
1482  * At the moment, only the various "floor" tiles are affected.\n
1483  *\n
1484  * The layout of the array is [x][0] = light and [x][1] = dark.\n
1485  */
1486 static TERM_COLOR lighting_colours[16][2] =
1487 {
1488         /* TERM_DARK */
1489         {TERM_L_DARK, TERM_DARK},
1490
1491         /* TERM_WHITE */
1492         {TERM_YELLOW, TERM_SLATE},
1493
1494         /* TERM_SLATE */
1495         {TERM_WHITE, TERM_L_DARK},
1496
1497         /* TERM_ORANGE */
1498         {TERM_L_UMBER, TERM_UMBER},
1499
1500         /* TERM_RED */
1501         {TERM_RED, TERM_RED},
1502
1503         /* TERM_GREEN */
1504         {TERM_L_GREEN, TERM_GREEN},
1505
1506         /* TERM_BLUE */
1507         {TERM_BLUE, TERM_BLUE},
1508
1509         /* TERM_UMBER */
1510         {TERM_L_UMBER, TERM_RED},
1511
1512         /* TERM_L_DARK */
1513         {TERM_SLATE, TERM_L_DARK},
1514
1515         /* TERM_L_WHITE */
1516         {TERM_WHITE, TERM_SLATE},
1517
1518         /* TERM_VIOLET */
1519         {TERM_L_RED, TERM_BLUE},
1520
1521         /* TERM_YELLOW */
1522         {TERM_YELLOW, TERM_ORANGE},
1523
1524         /* TERM_L_RED */
1525         {TERM_L_RED, TERM_L_RED},
1526
1527         /* TERM_L_GREEN */
1528         {TERM_L_GREEN, TERM_GREEN},
1529
1530         /* TERM_L_BLUE */
1531         {TERM_L_BLUE, TERM_L_BLUE},
1532
1533         /* TERM_L_UMBER */
1534         {TERM_L_UMBER, TERM_UMBER}
1535 };
1536
1537 /*!
1538  * @brief 調査中
1539  * @todo コメントを付加すること
1540  */
1541 void apply_default_feat_lighting(TERM_COLOR f_attr[F_LIT_MAX], SYMBOL_CODE f_char[F_LIT_MAX])
1542 {
1543         TERM_COLOR s_attr = f_attr[F_LIT_STANDARD];
1544         SYMBOL_CODE s_char = f_char[F_LIT_STANDARD];
1545         int i;
1546
1547         if (is_ascii_graphics(s_attr)) /* For ASCII */
1548         {
1549                 f_attr[F_LIT_LITE] = lighting_colours[s_attr & 0x0f][0];
1550                 f_attr[F_LIT_DARK] = lighting_colours[s_attr & 0x0f][1];
1551                 for (i = F_LIT_NS_BEGIN; i < F_LIT_MAX; i++) f_char[i] = s_char;
1552         }
1553         else /* For tile graphics */
1554         {
1555                 for (i = F_LIT_NS_BEGIN; i < F_LIT_MAX; i++) f_attr[i] = s_attr;
1556                 f_char[F_LIT_LITE] = s_char + 2;
1557                 f_char[F_LIT_DARK] = s_char + 1;
1558         }
1559 }
1560
1561
1562 /*!
1563  * モンスターにより照明が消されている地形か否かを判定する。 / Is this grid "darkened" by monster?
1564  */
1565 #define darkened_grid(C) \
1566         ((((C)->info & (CAVE_VIEW | CAVE_LITE | CAVE_MNLT | CAVE_MNDK)) == (CAVE_VIEW | CAVE_MNDK)) && \
1567         !p_ptr->see_nocto)
1568
1569
1570  /*!
1571   * @brief Mコマンドによる縮小マップの表示を行う / Extract the attr/char to display at the given (legal) map location
1572   * @details
1573   * Basically, we "paint" the chosen attr/char in several passes, starting\n
1574   * with any known "terrain features" (defaulting to darkness), then adding\n
1575   * any known "objects", and finally, adding any known "monsters".  This\n
1576   * is not the fastest method but since most of the calls to this function\n
1577   * are made for grids with no monsters or objects, it is fast enough.\n
1578   *\n
1579   * Note that this function, if used on the grid containing the "player",\n
1580   * will return the attr/char of the grid underneath the player, and not\n
1581   * the actual player attr/char itself, allowing a lot of optimization\n
1582   * in various "display" functions.\n
1583   *\n
1584   * Note that the "zero" entry in the feature/object/monster arrays are\n
1585   * used to provide "special" attr/char codes, with "monster zero" being\n
1586   * used for the player attr/char, "object zero" being used for the "stack"\n
1587   * attr/char, and "feature zero" being used for the "nothing" attr/char,\n
1588   * though this function makes use of only "feature zero".\n
1589   *\n
1590   * Note that monsters can have some "special" flags, including "ATTR_MULTI",\n
1591   * which means their color changes, and "ATTR_CLEAR", which means they take\n
1592   * the color of whatever is under them, and "CHAR_CLEAR", which means that\n
1593   * they take the symbol of whatever is under them.  Technically, the flag\n
1594   * "CHAR_MULTI" is supposed to indicate that a monster looks strange when\n
1595   * examined, but this flag is currently ignored.\n
1596   *\n
1597   * Currently, we do nothing with multi-hued objects, because there are\n
1598   * not any.  If there were, they would have to set "shimmer_objects"\n
1599   * when they were created, and then new "shimmer" code in "dungeon.c"\n
1600   * would have to be created handle the "shimmer" effect, and the code\n
1601   * in "current_floor_ptr->grid_array.c" would have to be updated to create the shimmer effect.\n
1602   *\n
1603   * Note the effects of hallucination.  Objects always appear as random\n
1604   * "objects", monsters as random "monsters", and normal grids occasionally\n
1605   * appear as random "monsters" or "objects", but note that these random\n
1606   * "monsters" and "objects" are really just "colored ascii symbols".\n
1607   *\n
1608   * Note that "floors" and "invisible traps" (and "zero" features) are\n
1609   * drawn as "floors" using a special check for optimization purposes,\n
1610   * and these are the only features which get drawn using the special\n
1611   * lighting effects activated by "view_special_lite".\n
1612   *\n
1613   * Note the use of the "mimic" field in the "terrain feature" processing,\n
1614   * which allows any feature to "pretend" to be another feature.  This is\n
1615   * used to "hide" secret doors, and to make all "doors" appear the same,\n
1616   * and all "walls" appear the same, and "hidden" treasure stay hidden.\n
1617   * It is possible to use this field to make a feature "look" like a floor,\n
1618   * but the "special lighting effects" for floors will not be used.\n
1619   *\n
1620   * Note the use of the new "terrain feature" information.  Note that the\n
1621   * assumption that all interesting "objects" and "terrain features" are\n
1622   * memorized allows extremely optimized processing below.  Note the use\n
1623   * of separate flags on objects to mark them as memorized allows a grid\n
1624   * to have memorized "terrain" without granting knowledge of any object\n
1625   * which may appear in that grid.\n
1626   *\n
1627   * Note the efficient code used to determine if a "floor" grid is\n
1628   * "memorized" or "viewable" by the player, where the test for the\n
1629   * grid being "viewable" is based on the facts that (1) the grid\n
1630   * must be "lit" (torch-lit or perma-lit), (2) the grid must be in\n
1631   * line of sight, and (3) the player must not be blind, and uses the\n
1632   * assumption that all torch-lit grids are in line of sight.\n
1633   *\n
1634   * Note that floors (and invisible traps) are the only grids which are\n
1635   * not memorized when seen, so only these grids need to check to see if\n
1636   * the grid is "viewable" to the player (if it is not memorized).  Since\n
1637   * most non-memorized grids are in fact walls, this induces *massive*\n
1638   * efficiency, at the cost of *forcing* the memorization of non-floor\n
1639   * grids when they are first seen.  Note that "invisible traps" are\n
1640   * always treated exactly like "floors", which prevents "cheating".\n
1641   *\n
1642   * Note the "special lighting effects" which can be activated for floor\n
1643   * grids using the "view_special_lite" option (for "white" floor grids),\n
1644   * causing certain grids to be displayed using special colors.  If the\n
1645   * player is "blind", we will use "dark gray", else if the grid is lit\n
1646   * by the torch, and the "view_yellow_lite" option is set, we will use\n
1647   * "yellow", else if the grid is "dark", we will use "dark gray", else\n
1648   * if the grid is not "viewable", and the "view_bright_lite" option is\n
1649   * set, and the we will use "slate" (gray).  We will use "white" for all\n
1650   * other cases, in particular, for illuminated viewable floor grids.\n
1651   *\n
1652   * Note the "special lighting effects" which can be activated for wall\n
1653   * grids using the "view_granite_lite" option (for "white" wall grids),\n
1654   * causing certain grids to be displayed using special colors.  If the\n
1655   * player is "blind", we will use "dark gray", else if the grid is lit\n
1656   * by the torch, and the "view_yellow_lite" option is set, we will use\n
1657   * "yellow", else if the "view_bright_lite" option is set, and the grid\n
1658   * is not "viewable", or is "dark", or is glowing, but not when viewed\n
1659   * from the player's current location, we will use "slate" (gray).  We\n
1660   * will use "white" for all other cases, in particular, for correctly\n
1661   * illuminated viewable wall grids.\n
1662   *\n
1663   * Note that, when "view_granite_lite" is set, we use an inline version\n
1664   * of the "player_can_see_bold()" function to check the "viewability" of\n
1665   * grids when the "view_bright_lite" option is set, and we do NOT use\n
1666   * any special colors for "dark" wall grids, since this would allow the\n
1667   * player to notice the walls of illuminated rooms from a hallway that\n
1668   * happened to run beside the room.  The alternative, by the way, would\n
1669   * be to prevent the generation of hallways next to rooms, but this\n
1670   * would still allow problems when digging towards a room.\n
1671   *\n
1672   * Note that bizarre things must be done when the "attr" and/or "char"\n
1673   * codes have the "high-bit" set, since these values are used to encode\n
1674   * various "special" pictures in some versions, and certain situations,\n
1675   * such as "multi-hued" or "clear" monsters, cause the attr/char codes\n
1676   * to be "scrambled" in various ways.\n
1677   *\n
1678   * Note that eventually we may use the "&" symbol for embedded treasure,\n
1679   * and use the "*" symbol to indicate multiple objects, though this will\n
1680   * have to wait for Angband 2.8.0 or later.  Note that currently, this\n
1681   * is not important, since only one object or terrain feature is allowed\n
1682   * in each grid.  If needed, "k_info[0]" will hold the "stack" attr/char.\n
1683   *\n
1684   * Note the assumption that doing "x_ptr = &x_info[x]" plus a few of\n
1685   * "x_ptr->xxx", is quicker than "x_info[x].xxx", if this is incorrect\n
1686   * then a whole lot of code should be changed...  XXX XXX\n
1687   */
1688 void map_info(POSITION y, POSITION x, TERM_COLOR *ap, SYMBOL_CODE *cp, TERM_COLOR *tap, SYMBOL_CODE *tcp)
1689 {
1690         /* Get the current_floor_ptr->grid_array */
1691         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1692
1693         OBJECT_IDX this_o_idx, next_o_idx = 0;
1694
1695         /* Feature code (applying "mimic" field) */
1696         FEAT_IDX feat = get_feat_mimic(g_ptr);
1697
1698         /* Access floor */
1699         feature_type *f_ptr = &f_info[feat];
1700
1701         TERM_COLOR a;
1702         SYMBOL_CODE c;
1703
1704         /* Boring grids (floors, etc) */
1705         if (!have_flag(f_ptr->flags, FF_REMEMBER))
1706         {
1707                 /*
1708                  * Handle Memorized or visible floor
1709                  *
1710                  * No visual when blinded.
1711                  *   (to prevent strange effects on darkness breath)
1712                  * otherwise,
1713                  * - Can see grids with CAVE_MARK.
1714                  * - Can see grids with CAVE_LITE or CAVE_MNLT.
1715                  *   (Such grids also have CAVE_VIEW)
1716                  * - Can see grids with CAVE_VIEW unless darkened by monsters.
1717                  */
1718                 if (!p_ptr->blind &&
1719                         ((g_ptr->info & (CAVE_MARK | CAVE_LITE | CAVE_MNLT)) ||
1720                         ((g_ptr->info & CAVE_VIEW) && (((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW) || p_ptr->see_nocto))))
1721                 {
1722                         /* Normal attr/char */
1723                         a = f_ptr->x_attr[F_LIT_STANDARD];
1724                         c = f_ptr->x_char[F_LIT_STANDARD];
1725
1726                         if (p_ptr->wild_mode)
1727                         {
1728                                 /* Special lighting effects */
1729                                 /* Handle "night" */
1730                                 if (view_special_lite && !is_daytime())
1731                                 {
1732                                         /* Use a darkened colour/tile */
1733                                         a = f_ptr->x_attr[F_LIT_DARK];
1734                                         c = f_ptr->x_char[F_LIT_DARK];
1735                                 }
1736                         }
1737
1738                         /* Mega-Hack -- Handle "in-sight" and "darkened" grids */
1739                         else if (darkened_grid(g_ptr))
1740                         {
1741                                 /* Unsafe grid -- idea borrowed from Unangband */
1742                                 feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1743
1744                                 /* Access darkness */
1745                                 f_ptr = &f_info[feat];
1746
1747                                 /* Char and attr of darkness */
1748                                 a = f_ptr->x_attr[F_LIT_STANDARD];
1749                                 c = f_ptr->x_char[F_LIT_STANDARD];
1750                         }
1751
1752                         /* Special lighting effects */
1753                         else if (view_special_lite)
1754                         {
1755                                 /* Handle "torch-lit" grids */
1756                                 if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
1757                                 {
1758                                         /* Torch lite */
1759                                         if (view_yellow_lite)
1760                                         {
1761                                                 /* Use a brightly lit colour/tile */
1762                                                 a = f_ptr->x_attr[F_LIT_LITE];
1763                                                 c = f_ptr->x_char[F_LIT_LITE];
1764                                         }
1765                                 }
1766
1767                                 /* Handle "dark" grids */
1768                                 else if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1769                                 {
1770                                         /* Use a darkened colour/tile */
1771                                         a = f_ptr->x_attr[F_LIT_DARK];
1772                                         c = f_ptr->x_char[F_LIT_DARK];
1773                                 }
1774
1775                                 /* Handle "out-of-sight" grids */
1776                                 else if (!(g_ptr->info & CAVE_VIEW))
1777                                 {
1778                                         /* Special flag */
1779                                         if (view_bright_lite)
1780                                         {
1781                                                 /* Use a darkened colour/tile */
1782                                                 a = f_ptr->x_attr[F_LIT_DARK];
1783                                                 c = f_ptr->x_char[F_LIT_DARK];
1784                                         }
1785                                 }
1786                         }
1787                 }
1788
1789                 /* Unknown */
1790                 else
1791                 {
1792                         /* Unsafe grid -- idea borrowed from Unangband */
1793                         feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1794
1795                         /* Access darkness */
1796                         f_ptr = &f_info[feat];
1797
1798                         /* Normal attr/char */
1799                         a = f_ptr->x_attr[F_LIT_STANDARD];
1800                         c = f_ptr->x_char[F_LIT_STANDARD];
1801                 }
1802         }
1803
1804         /* Interesting grids (non-floors) */
1805         else
1806         {
1807                 /* Memorized grids */
1808                 if (g_ptr->info & CAVE_MARK)
1809                 {
1810                         /* Normal attr/char */
1811                         a = f_ptr->x_attr[F_LIT_STANDARD];
1812                         c = f_ptr->x_char[F_LIT_STANDARD];
1813
1814                         if (p_ptr->wild_mode)
1815                         {
1816                                 /* Special lighting effects */
1817                                 /* Handle "blind" or "night" */
1818                                 if (view_granite_lite && (p_ptr->blind || !is_daytime()))
1819                                 {
1820                                         /* Use a darkened colour/tile */
1821                                         a = f_ptr->x_attr[F_LIT_DARK];
1822                                         c = f_ptr->x_char[F_LIT_DARK];
1823                                 }
1824                         }
1825
1826                         /* Mega-Hack -- Handle "in-sight" and "darkened" grids */
1827                         else if (darkened_grid(g_ptr) && !p_ptr->blind)
1828                         {
1829                                 if (have_flag(f_ptr->flags, FF_LOS) && have_flag(f_ptr->flags, FF_PROJECT))
1830                                 {
1831                                         /* Unsafe grid -- idea borrowed from Unangband */
1832                                         feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1833
1834                                         /* Access darkness */
1835                                         f_ptr = &f_info[feat];
1836
1837                                         /* Char and attr of darkness */
1838                                         a = f_ptr->x_attr[F_LIT_STANDARD];
1839                                         c = f_ptr->x_char[F_LIT_STANDARD];
1840                                 }
1841                                 else if (view_granite_lite && view_bright_lite)
1842                                 {
1843                                         /* Use a darkened colour/tile */
1844                                         a = f_ptr->x_attr[F_LIT_DARK];
1845                                         c = f_ptr->x_char[F_LIT_DARK];
1846                                 }
1847                         }
1848
1849                         /* Special lighting effects */
1850                         else if (view_granite_lite)
1851                         {
1852                                 /* Handle "blind" */
1853                                 if (p_ptr->blind)
1854                                 {
1855                                         /* Use a darkened colour/tile */
1856                                         a = f_ptr->x_attr[F_LIT_DARK];
1857                                         c = f_ptr->x_char[F_LIT_DARK];
1858                                 }
1859
1860                                 /* Handle "torch-lit" grids */
1861                                 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
1862                                 {
1863                                         /* Torch lite */
1864                                         if (view_yellow_lite)
1865                                         {
1866                                                 /* Use a brightly lit colour/tile */
1867                                                 a = f_ptr->x_attr[F_LIT_LITE];
1868                                                 c = f_ptr->x_char[F_LIT_LITE];
1869                                         }
1870                                 }
1871
1872                                 /* Handle "view_bright_lite" */
1873                                 else if (view_bright_lite)
1874                                 {
1875                                         /* Not viewable */
1876                                         if (!(g_ptr->info & CAVE_VIEW))
1877                                         {
1878                                                 /* Use a darkened colour/tile */
1879                                                 a = f_ptr->x_attr[F_LIT_DARK];
1880                                                 c = f_ptr->x_char[F_LIT_DARK];
1881                                         }
1882
1883                                         /* Not glowing */
1884                                         else if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1885                                         {
1886                                                 /* Use a darkened colour/tile */
1887                                                 a = f_ptr->x_attr[F_LIT_DARK];
1888                                                 c = f_ptr->x_char[F_LIT_DARK];
1889                                         }
1890
1891                                         /* Not glowing correctly */
1892                                         else if (!have_flag(f_ptr->flags, FF_LOS) && !check_local_illumination(y, x))
1893                                         {
1894                                                 /* Use a darkened colour/tile */
1895                                                 a = f_ptr->x_attr[F_LIT_DARK];
1896                                                 c = f_ptr->x_char[F_LIT_DARK];
1897                                         }
1898                                 }
1899                         }
1900                 }
1901
1902                 /* Unknown */
1903                 else
1904                 {
1905                         /* Unsafe grid -- idea borrowed from Unangband */
1906                         feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1907
1908                         /* Access feature */
1909                         f_ptr = &f_info[feat];
1910
1911                         /* Normal attr/char */
1912                         a = f_ptr->x_attr[F_LIT_STANDARD];
1913                         c = f_ptr->x_char[F_LIT_STANDARD];
1914                 }
1915         }
1916
1917         if (feat_priority == -1) feat_priority = f_ptr->priority;
1918
1919         /* Save the terrain info for the transparency effects */
1920         (*tap) = a;
1921         (*tcp) = c;
1922
1923         /* Save the info */
1924         (*ap) = a;
1925         (*cp) = c;
1926
1927         /* Hack -- rare random hallucination, except on outer dungeon walls */
1928         if (p_ptr->image)
1929         {
1930                 if (one_in_(256))
1931                 {
1932                         image_random(ap, cp);
1933                 }
1934         }
1935
1936         /* Objects */
1937         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1938         {
1939                 object_type *o_ptr;
1940                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
1941                 next_o_idx = o_ptr->next_o_idx;
1942
1943                 /* Memorized objects */
1944                 if (o_ptr->marked & OM_FOUND)
1945                 {
1946                         if (display_autopick)
1947                         {
1948                                 byte act;
1949
1950                                 match_autopick = is_autopick(o_ptr);
1951                                 if (match_autopick == -1)
1952                                         continue;
1953
1954                                 act = autopick_list[match_autopick].action;
1955
1956                                 if ((act & DO_DISPLAY) && (act & display_autopick))
1957                                 {
1958                                         autopick_obj = o_ptr;
1959                                 }
1960                                 else
1961                                 {
1962                                         match_autopick = -1;
1963                                         continue;
1964                                 }
1965                         }
1966                         /* Normal char */
1967                         (*cp) = object_char(o_ptr);
1968
1969                         /* Normal attr */
1970                         (*ap) = object_attr(o_ptr);
1971
1972                         feat_priority = 20;
1973
1974                         /* Hack -- hallucination */
1975                         if (p_ptr->image) image_object(ap, cp);
1976
1977                         break;
1978                 }
1979         }
1980
1981
1982         /* Handle monsters */
1983         if (g_ptr->m_idx && display_autopick == 0)
1984         {
1985                 monster_type *m_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
1986
1987                 /* Visible monster */
1988                 if (m_ptr->ml)
1989                 {
1990                         monster_race *r_ptr = &r_info[m_ptr->ap_r_idx];
1991
1992                         feat_priority = 30;
1993
1994                         /* Hallucination */
1995                         if (p_ptr->image)
1996                         {
1997                                 /*
1998                                  * Monsters with both CHAR_CLEAR and ATTR_CLEAR
1999                                  * flags are always unseen.
2000                                  */
2001                                 if ((r_ptr->flags1 & (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR)) == (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR))
2002                                 {
2003                                         /* Do nothing */
2004                                 }
2005                                 else
2006                                 {
2007                                         image_monster(ap, cp);
2008                                 }
2009                         }
2010                         else
2011                         {
2012                                 /* Monster attr/char */
2013                                 a = r_ptr->x_attr;
2014                                 c = r_ptr->x_char;
2015
2016                                 /* Normal monsters */
2017                                 if (!(r_ptr->flags1 & (RF1_CHAR_CLEAR | RF1_SHAPECHANGER | RF1_ATTR_CLEAR
2018                                         | RF1_ATTR_MULTI | RF1_ATTR_SEMIRAND)))
2019                                 {
2020                                         /* Desired monster attr/char */
2021                                         *ap = a;
2022                                         *cp = c;
2023                                 }
2024
2025                                 /*
2026                                  * Monsters with both CHAR_CLEAR and ATTR_CLEAR
2027                                  * flags are always unseen.
2028                                  */
2029                                 else if ((r_ptr->flags1 & (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR)) == (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR))
2030                                 {
2031                                         /* Do nothing */
2032                                 }
2033
2034                                 else
2035                                 {
2036                                         /***  Monster's attr  ***/
2037                                         if ((r_ptr->flags1 & RF1_ATTR_CLEAR) && (*ap != TERM_DARK) && !use_graphics)
2038                                         {
2039                                                 /* Clear-attr */
2040                                                 /* Do nothing */
2041                                         }
2042                                         else if ((r_ptr->flags1 & RF1_ATTR_MULTI) && !use_graphics)
2043                                         {
2044                                                 /* Multi-hued attr */
2045                                                 if (r_ptr->flags2 & RF2_ATTR_ANY) *ap = randint1(15);
2046                                                 else switch (randint1(7))
2047                                                 {
2048                                                 case 1: *ap = TERM_RED;     break;
2049                                                 case 2: *ap = TERM_L_RED;   break;
2050                                                 case 3: *ap = TERM_WHITE;   break;
2051                                                 case 4: *ap = TERM_L_GREEN; break;
2052                                                 case 5: *ap = TERM_BLUE;    break;
2053                                                 case 6: *ap = TERM_L_DARK;  break;
2054                                                 case 7: *ap = TERM_GREEN;   break;
2055                                                 }
2056                                         }
2057                                         else if ((r_ptr->flags1 & RF1_ATTR_SEMIRAND) && !use_graphics)
2058                                         {
2059                                                 /* Use semi-random attr (usually mimics' colors vary) */
2060                                                 *ap = g_ptr->m_idx % 15 + 1;
2061                                         }
2062                                         else
2063                                         {
2064                                                 /* Normal case */
2065                                                 *ap = a;
2066                                         }
2067
2068                                         /***  Monster's char  ***/
2069                                         if ((r_ptr->flags1 & RF1_CHAR_CLEAR) && (*cp != ' ') && !use_graphics)
2070                                         {
2071                                                 /* Clear-char */
2072                                                 /* Do nothing */
2073                                         }
2074                                         else if (r_ptr->flags1 & RF1_SHAPECHANGER)
2075                                         {
2076                                                 if (use_graphics)
2077                                                 {
2078                                                         monster_race *tmp_r_ptr = &r_info[randint1(max_r_idx - 1)];
2079                                                         *cp = tmp_r_ptr->x_char;
2080                                                         *ap = tmp_r_ptr->x_attr;
2081                                                 }
2082                                                 else
2083                                                 {
2084                                                         *cp = (one_in_(25) ?
2085                                                                 image_object_hack[randint0(sizeof(image_object_hack) - 1)] :
2086                                                                 image_monster_hack[randint0(sizeof(image_monster_hack) - 1)]);
2087                                                 }
2088                                         }
2089                                         else
2090                                         {
2091                                                 /* Normal case */
2092                                                 *cp = c;
2093                                         }
2094                                 }
2095                         }
2096                 }
2097         }
2098
2099         /* Handle "player" */
2100         if (player_bold(y, x))
2101         {
2102                 monster_race *r_ptr = &r_info[0];
2103                 *ap = r_ptr->x_attr;
2104                 *cp = r_ptr->x_char;
2105                 feat_priority = 31;
2106         }
2107 }
2108
2109
2110 /*
2111  * Calculate panel colum of a location in the map
2112  */
2113 static int panel_col_of(int col)
2114 {
2115         col -= panel_col_min;
2116         if (use_bigtile) col *= 2;
2117         return col + 13;
2118 }
2119
2120
2121 /*
2122  * Moves the cursor to a given MAP (y,x) location
2123  */
2124 void move_cursor_relative(int row, int col)
2125 {
2126         /* Real co-ords convert to screen positions */
2127         row -= panel_row_prt;
2128
2129         /* Go there */
2130         Term_gotoxy(panel_col_of(col), row);
2131 }
2132
2133
2134
2135 /*
2136  * Place an attr/char pair at the given map coordinate, if legal.
2137  */
2138 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
2139 {
2140         /* Only do "legal" locations */
2141         if (panel_contains(y, x))
2142         {
2143                 /* Hack -- fake monochrome */
2144                 if (!use_graphics)
2145                 {
2146                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
2147                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
2148                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
2149                 }
2150
2151                 /* Draw the char using the attr */
2152                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
2153         }
2154 }
2155
2156
2157
2158
2159
2160 /*
2161  * Memorize interesting viewable object/features in the given grid
2162  *
2163  * This function should only be called on "legal" grids.
2164  *
2165  * This function will memorize the object and/or feature in the given
2166  * grid, if they are (1) viewable and (2) interesting.  Note that all
2167  * objects are interesting, all terrain features except floors (and
2168  * invisible traps) are interesting, and floors (and invisible traps)
2169  * are interesting sometimes (depending on various options involving
2170  * the illumination of floor grids).
2171  *
2172  * The automatic memorization of all objects and non-floor terrain
2173  * features as soon as they are displayed allows incredible amounts
2174  * of optimization in various places, especially "map_info()".
2175  *
2176  * Note that the memorization of objects is completely separate from
2177  * the memorization of terrain features, preventing annoying floor
2178  * memorization when a detected object is picked up from a dark floor,
2179  * and object memorization when an object is dropped into a floor grid
2180  * which is memorized but out-of-sight.
2181  *
2182  * This function should be called every time the "memorization" of
2183  * a grid (or the object in a grid) is called into question, such
2184  * as when an object is created in a grid, when a terrain feature
2185  * "changes" from "floor" to "non-floor", when any grid becomes
2186  * "illuminated" or "viewable", and when a "floor" grid becomes
2187  * "torch-lit".
2188  *
2189  * Note the relatively efficient use of this function by the various
2190  * "update_view()" and "update_lite()" calls, to allow objects and
2191  * terrain features to be memorized (and drawn) whenever they become
2192  * viewable or illuminated in any way, but not when they "maintain"
2193  * or "lose" their previous viewability or illumination.
2194  *
2195  * Note the butchered "internal" version of "player_can_see_bold()",
2196  * optimized primarily for the most common cases, that is, for the
2197  * non-marked floor grids.
2198  */
2199 void note_spot(POSITION y, POSITION x)
2200 {
2201         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
2202         OBJECT_IDX this_o_idx, next_o_idx = 0;
2203
2204         /* Blind players see nothing */
2205         if (p_ptr->blind) return;
2206
2207         /* Analyze non-torch-lit grids */
2208         if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
2209         {
2210                 /* Require line of sight to the grid */
2211                 if (!(g_ptr->info & (CAVE_VIEW))) return;
2212
2213                 /* Require "perma-lite" of the grid */
2214                 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
2215                 {
2216                         /* Not Ninja */
2217                         if (!p_ptr->see_nocto) return;
2218                 }
2219         }
2220
2221
2222         /* Hack -- memorize objects */
2223         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
2224         {
2225                 object_type *o_ptr = &current_floor_ptr->o_list[this_o_idx];
2226                 next_o_idx = o_ptr->next_o_idx;
2227
2228                 /* Memorize objects */
2229                 o_ptr->marked |= OM_FOUND;
2230         }
2231
2232
2233         /* Hack -- memorize grids */
2234         if (!(g_ptr->info & (CAVE_MARK)))
2235         {
2236                 /* Feature code (applying "mimic" field) */
2237                 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
2238
2239                 /* Memorize some "boring" grids */
2240                 if (!have_flag(f_ptr->flags, FF_REMEMBER))
2241                 {
2242                         /* Option -- memorize all torch-lit floors */
2243                         if (view_torch_grids &&
2244                                 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
2245                         {
2246                                 g_ptr->info |= (CAVE_MARK);
2247                         }
2248
2249                         /* Option -- memorize all perma-lit floors */
2250                         else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
2251                         {
2252                                 g_ptr->info |= (CAVE_MARK);
2253                         }
2254                 }
2255
2256                 /* Memorize normal grids */
2257                 else if (have_flag(f_ptr->flags, FF_LOS))
2258                 {
2259                         g_ptr->info |= (CAVE_MARK);
2260                 }
2261
2262                 /* Memorize torch-lit walls */
2263                 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
2264                 {
2265                         g_ptr->info |= (CAVE_MARK);
2266                 }
2267
2268                 /* Memorize walls seen by noctovision of Ninja */
2269                 else if (p_ptr->see_nocto)
2270                 {
2271                         g_ptr->info |= (CAVE_MARK);
2272                 }
2273
2274                 /* Memorize certain non-torch-lit wall grids */
2275                 else if (check_local_illumination(y, x))
2276                 {
2277                         g_ptr->info |= (CAVE_MARK);
2278                 }
2279         }
2280
2281         /* Memorize terrain of the grid */
2282         g_ptr->info |= (CAVE_KNOWN);
2283 }
2284
2285
2286 void display_dungeon(void)
2287 {
2288         TERM_LEN x, y;
2289         TERM_COLOR a;
2290         SYMBOL_CODE c;
2291
2292         TERM_COLOR ta = 0;
2293         SYMBOL_CODE tc = '\0';
2294
2295         for (x = p_ptr->x - Term->wid / 2 + 1; x <= p_ptr->x + Term->wid / 2; x++)
2296         {
2297                 for (y = p_ptr->y - Term->hgt / 2 + 1; y <= p_ptr->y + Term->hgt / 2; y++)
2298                 {
2299                         if (in_bounds2(y, x))
2300                         {
2301                                 map_info(y, x, &a, &c, &ta, &tc);
2302
2303                                 /* Hack -- fake monochrome */
2304                                 if (!use_graphics)
2305                                 {
2306                                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
2307                                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
2308                                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
2309                                 }
2310
2311                                 /* Hack -- Queue it */
2312                                 Term_queue_char(x - p_ptr->x + Term->wid / 2 - 1, y - p_ptr->y + Term->hgt / 2 - 1, a, c, ta, tc);
2313                         }
2314                         else
2315                         {
2316                                 /* Clear out-of-bound tiles */
2317
2318                                 /* Access darkness */
2319                                 feature_type *f_ptr = &f_info[feat_none];
2320
2321                                 /* Normal attr */
2322                                 a = f_ptr->x_attr[F_LIT_STANDARD];
2323
2324                                 /* Normal char */
2325                                 c = f_ptr->x_char[F_LIT_STANDARD];
2326
2327                                 /* Hack -- Queue it */
2328                                 Term_queue_char(x - p_ptr->x + Term->wid / 2 - 1, y - p_ptr->y + Term->hgt / 2 - 1, a, c, ta, tc);
2329                         }
2330                 }
2331         }
2332 }
2333
2334
2335 /*
2336  * Redraw (on the screen) a given MAP location
2337  *
2338  * This function should only be called on "legal" grids
2339  */
2340 void lite_spot(POSITION y, POSITION x)
2341 {
2342         /* Redraw if on screen */
2343         if (panel_contains(y, x) && in_bounds2(y, x))
2344         {
2345                 TERM_COLOR a;
2346                 SYMBOL_CODE c;
2347                 TERM_COLOR ta;
2348                 SYMBOL_CODE tc;
2349
2350                 map_info(y, x, &a, &c, &ta, &tc);
2351
2352                 /* Hack -- fake monochrome */
2353                 if (!use_graphics)
2354                 {
2355                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
2356                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
2357                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
2358                 }
2359
2360                 /* Hack -- Queue it */
2361                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
2362
2363                 /* Update sub-windows */
2364                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2365         }
2366 }
2367
2368
2369 /*
2370  * Prints the map of the dungeon
2371  *
2372  * Note that, for efficiency, we contain an "optimized" version
2373  * of both "lite_spot()" and "print_rel()", and that we use the
2374  * "lite_spot()" function to display the player grid, if needed.
2375  */
2376 void prt_map(void)
2377 {
2378         POSITION x, y;
2379         int v;
2380
2381         /* map bounds */
2382         POSITION xmin, xmax, ymin, ymax;
2383
2384         TERM_LEN wid, hgt;
2385
2386         Term_get_size(&wid, &hgt);
2387
2388         /* Remove map offset */
2389         wid -= COL_MAP + 2;
2390         hgt -= ROW_MAP + 2;
2391
2392         /* Access the cursor state */
2393         (void)Term_get_cursor(&v);
2394
2395         /* Hide the cursor */
2396         (void)Term_set_cursor(0);
2397
2398         /* Get bounds */
2399         xmin = (0 < panel_col_min) ? panel_col_min : 0;
2400         xmax = (current_floor_ptr->width - 1 > panel_col_max) ? panel_col_max : current_floor_ptr->width - 1;
2401         ymin = (0 < panel_row_min) ? panel_row_min : 0;
2402         ymax = (current_floor_ptr->height - 1 > panel_row_max) ? panel_row_max : current_floor_ptr->height - 1;
2403
2404         /* Bottom section of screen */
2405         for (y = 1; y <= ymin - panel_row_prt; y++)
2406         {
2407                 /* Erase the section */
2408                 Term_erase(COL_MAP, y, wid);
2409         }
2410
2411         /* Top section of screen */
2412         for (y = ymax - panel_row_prt; y <= hgt; y++)
2413         {
2414                 /* Erase the section */
2415                 Term_erase(COL_MAP, y, wid);
2416         }
2417
2418         /* Dump the map */
2419         for (y = ymin; y <= ymax; y++)
2420         {
2421                 /* Scan the columns of row "y" */
2422                 for (x = xmin; x <= xmax; x++)
2423                 {
2424                         TERM_COLOR a;
2425                         SYMBOL_CODE c;
2426
2427                         TERM_COLOR ta;
2428                         SYMBOL_CODE tc;
2429
2430                         /* Determine what is there */
2431                         map_info(y, x, &a, &c, &ta, &tc);
2432
2433                         /* Hack -- fake monochrome */
2434                         if (!use_graphics)
2435                         {
2436                                 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
2437                                 else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
2438                                 else if (p_ptr->wraith_form) a = TERM_L_DARK;
2439                         }
2440
2441                         /* Efficiency -- Redraw that grid of the map */
2442                         Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
2443                 }
2444         }
2445
2446         /* Display player */
2447         lite_spot(p_ptr->y, p_ptr->x);
2448
2449         /* Restore the cursor */
2450         (void)Term_set_cursor(v);
2451 }
2452
2453
2454
2455 /*
2456  * print project path
2457  */
2458 void prt_path(POSITION y, POSITION x)
2459 {
2460         int i;
2461         int path_n;
2462         u16b path_g[512];
2463         byte_hack default_color = TERM_SLATE;
2464
2465         if (!display_path) return;
2466         if (-1 == project_length)
2467                 return;
2468
2469         /* Get projection path */
2470         path_n = project_path(path_g, (project_length ? project_length : MAX_RANGE), p_ptr->y, p_ptr->x, y, x, PROJECT_PATH | PROJECT_THRU);
2471
2472         p_ptr->redraw |= (PR_MAP);
2473         handle_stuff();
2474
2475         /* Draw path */
2476         for (i = 0; i < path_n; i++)
2477         {
2478                 POSITION ny = GRID_Y(path_g[i]);
2479                 POSITION nx = GRID_X(path_g[i]);
2480                 grid_type *g_ptr = &current_floor_ptr->grid_array[ny][nx];
2481
2482                 if (panel_contains(ny, nx))
2483                 {
2484                         TERM_COLOR a = default_color;
2485                         char c;
2486
2487                         TERM_COLOR ta = default_color;
2488                         char tc = '*';
2489
2490                         if (g_ptr->m_idx && current_floor_ptr->m_list[g_ptr->m_idx].ml)
2491                         {
2492                                 /* Determine what is there */
2493                                 map_info(ny, nx, &a, &c, &ta, &tc);
2494
2495                                 if (!is_ascii_graphics(a))
2496                                         a = default_color;
2497                                 else if (c == '.' && (a == TERM_WHITE || a == TERM_L_WHITE))
2498                                         a = default_color;
2499                                 else if (a == default_color)
2500                                         a = TERM_WHITE;
2501                         }
2502
2503                         if (!use_graphics)
2504                         {
2505                                 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
2506                                 else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
2507                                 else if (p_ptr->wraith_form) a = TERM_L_DARK;
2508                         }
2509
2510                         c = '*';
2511
2512                         /* Hack -- Queue it */
2513                         Term_queue_bigchar(panel_col_of(nx), ny - panel_row_prt, a, c, ta, tc);
2514                 }
2515
2516                 /* Known Wall */
2517                 if ((g_ptr->info & CAVE_MARK) && !cave_have_flag_grid(g_ptr, FF_PROJECT)) break;
2518
2519                 /* Change color */
2520                 if (nx == x && ny == y) default_color = TERM_L_DARK;
2521         }
2522 }
2523
2524
2525 static concptr simplify_list[][2] =
2526 {
2527 #ifdef JP
2528         {"の魔法書", ""},
2529         {NULL, NULL}
2530 #else
2531         {"^Ring of ",   "="},
2532         {"^Amulet of ", "\""},
2533         {"^Scroll of ", "?"},
2534         {"^Scroll titled ", "?"},
2535         {"^Wand of "  , "-"},
2536         {"^Rod of "   , "-"},
2537         {"^Staff of " , "_"},
2538         {"^Potion of ", "!"},
2539         {" Spellbook ",""},
2540         {"^Book of ",   ""},
2541         {" Magic [",   "["},
2542         {" Book [",    "["},
2543         {" Arts [",    "["},
2544         {"^Set of ",    ""},
2545         {"^Pair of ",   ""},
2546         {NULL, NULL}
2547 #endif
2548 };
2549
2550 static void display_shortened_item_name(object_type *o_ptr, int y)
2551 {
2552         char buf[MAX_NLEN];
2553         char *c = buf;
2554         int len = 0;
2555         TERM_COLOR attr;
2556
2557         object_desc(buf, o_ptr, (OD_NO_FLAVOR | OD_OMIT_PREFIX | OD_NAME_ONLY));
2558         attr = tval_to_attr[o_ptr->tval % 128];
2559
2560         if (p_ptr->image)
2561         {
2562                 attr = TERM_WHITE;
2563                 strcpy(buf, _("何か奇妙な物", "something strange"));
2564         }
2565
2566         for (c = buf; *c; c++)
2567         {
2568                 int i;
2569                 for (i = 0; simplify_list[i][1]; i++)
2570                 {
2571                         concptr org_w = simplify_list[i][0];
2572
2573                         if (*org_w == '^')
2574                         {
2575                                 if (c == buf)
2576                                         org_w++;
2577                                 else
2578                                         continue;
2579                         }
2580
2581                         if (!strncmp(c, org_w, strlen(org_w)))
2582                         {
2583                                 char *s = c;
2584                                 concptr tmp = simplify_list[i][1];
2585                                 while (*tmp)
2586                                         *s++ = *tmp++;
2587                                 tmp = c + strlen(org_w);
2588                                 while (*tmp)
2589                                         *s++ = *tmp++;
2590                                 *s = '\0';
2591                         }
2592                 }
2593         }
2594
2595         c = buf;
2596         len = 0;
2597         /* 半角 12 文字分で切る */
2598         while (*c)
2599         {
2600 #ifdef JP
2601                 if (iskanji(*c))
2602                 {
2603                         if (len + 2 > 12) break;
2604                         c += 2;
2605                         len += 2;
2606                 }
2607                 else
2608 #endif
2609                 {
2610                         if (len + 1 > 12) break;
2611                         c++;
2612                         len++;
2613                 }
2614         }
2615         *c = '\0';
2616         Term_putstr(0, y, 12, attr, buf);
2617 }
2618
2619 /*
2620  * Display a "small-scale" map of the dungeon in the active Term
2621  */
2622 void display_map(int *cy, int *cx)
2623 {
2624         int i, j, x, y;
2625
2626         TERM_COLOR ta;
2627         SYMBOL_CODE tc;
2628
2629         byte tp;
2630
2631         TERM_COLOR **bigma;
2632         SYMBOL_CODE **bigmc;
2633         byte **bigmp;
2634
2635         TERM_COLOR **ma;
2636         SYMBOL_CODE **mc;
2637         byte **mp;
2638
2639         /* Save lighting effects */
2640         bool old_view_special_lite = view_special_lite;
2641         bool old_view_granite_lite = view_granite_lite;
2642
2643         TERM_LEN hgt, wid, yrat, xrat;
2644
2645         int **match_autopick_yx;
2646         object_type ***object_autopick_yx;
2647
2648         Term_get_size(&wid, &hgt);
2649         hgt -= 2;
2650         wid -= 14;
2651         if (use_bigtile) wid /= 2;
2652
2653         yrat = (current_floor_ptr->height + hgt - 1) / hgt;
2654         xrat = (current_floor_ptr->width + wid - 1) / wid;
2655
2656         /* Disable lighting effects */
2657         view_special_lite = FALSE;
2658         view_granite_lite = FALSE;
2659
2660         /* Allocate the maps */
2661         C_MAKE(ma, (hgt + 2), TERM_COLOR *);
2662         C_MAKE(mc, (hgt + 2), char_ptr);
2663         C_MAKE(mp, (hgt + 2), byte_ptr);
2664         C_MAKE(match_autopick_yx, (hgt + 2), sint_ptr);
2665         C_MAKE(object_autopick_yx, (hgt + 2), object_type **);
2666
2667         /* Allocate and wipe each line map */
2668         for (y = 0; y < (hgt + 2); y++)
2669         {
2670                 /* Allocate one row each array */
2671                 C_MAKE(ma[y], (wid + 2), TERM_COLOR);
2672                 C_MAKE(mc[y], (wid + 2), char);
2673                 C_MAKE(mp[y], (wid + 2), byte);
2674                 C_MAKE(match_autopick_yx[y], (wid + 2), int);
2675                 C_MAKE(object_autopick_yx[y], (wid + 2), object_type *);
2676
2677                 for (x = 0; x < wid + 2; ++x)
2678                 {
2679                         match_autopick_yx[y][x] = -1;
2680                         object_autopick_yx[y][x] = NULL;
2681
2682                         /* Nothing here */
2683                         ma[y][x] = TERM_WHITE;
2684                         mc[y][x] = ' ';
2685
2686                         /* No priority */
2687                         mp[y][x] = 0;
2688                 }
2689         }
2690
2691         /* Allocate the maps */
2692         C_MAKE(bigma, (current_floor_ptr->height + 2), TERM_COLOR *);
2693         C_MAKE(bigmc, (current_floor_ptr->height + 2), char_ptr);
2694         C_MAKE(bigmp, (current_floor_ptr->height + 2), byte_ptr);
2695
2696         /* Allocate and wipe each line map */
2697         for (y = 0; y < (current_floor_ptr->height + 2); y++)
2698         {
2699                 /* Allocate one row each array */
2700                 C_MAKE(bigma[y], (current_floor_ptr->width + 2), TERM_COLOR);
2701                 C_MAKE(bigmc[y], (current_floor_ptr->width + 2), char);
2702                 C_MAKE(bigmp[y], (current_floor_ptr->width + 2), byte);
2703
2704                 for (x = 0; x < current_floor_ptr->width + 2; ++x)
2705                 {
2706                         /* Nothing here */
2707                         bigma[y][x] = TERM_WHITE;
2708                         bigmc[y][x] = ' ';
2709
2710                         /* No priority */
2711                         bigmp[y][x] = 0;
2712                 }
2713         }
2714
2715         /* Fill in the map */
2716         for (i = 0; i < current_floor_ptr->width; ++i)
2717         {
2718                 for (j = 0; j < current_floor_ptr->height; ++j)
2719                 {
2720                         x = i / xrat + 1;
2721                         y = j / yrat + 1;
2722
2723                         match_autopick = -1;
2724                         autopick_obj = NULL;
2725                         feat_priority = -1;
2726
2727                         /* Extract the current attr/char at that map location */
2728                         map_info(j, i, &ta, &tc, &ta, &tc);
2729
2730                         /* Extract the priority */
2731                         tp = (byte_hack)feat_priority;
2732
2733                         if (match_autopick != -1
2734                                 && (match_autopick_yx[y][x] == -1
2735                                         || match_autopick_yx[y][x] > match_autopick))
2736                         {
2737                                 match_autopick_yx[y][x] = match_autopick;
2738                                 object_autopick_yx[y][x] = autopick_obj;
2739                                 tp = 0x7f;
2740                         }
2741
2742                         /* Save the char, attr and priority */
2743                         bigmc[j + 1][i + 1] = tc;
2744                         bigma[j + 1][i + 1] = ta;
2745                         bigmp[j + 1][i + 1] = tp;
2746                 }
2747         }
2748
2749         for (j = 0; j < current_floor_ptr->height; ++j)
2750         {
2751                 for (i = 0; i < current_floor_ptr->width; ++i)
2752                 {
2753                         x = i / xrat + 1;
2754                         y = j / yrat + 1;
2755
2756                         tc = bigmc[j + 1][i + 1];
2757                         ta = bigma[j + 1][i + 1];
2758                         tp = bigmp[j + 1][i + 1];
2759
2760                         /* rare feature has more priority */
2761                         if (mp[y][x] == tp)
2762                         {
2763                                 int t;
2764                                 int cnt = 0;
2765
2766                                 for (t = 0; t < 8; t++)
2767                                 {
2768                                         if (tc == bigmc[j + 1 + ddy_cdd[t]][i + 1 + ddx_cdd[t]] &&
2769                                                 ta == bigma[j + 1 + ddy_cdd[t]][i + 1 + ddx_cdd[t]])
2770                                                 cnt++;
2771                                 }
2772                                 if (cnt <= 4)
2773                                         tp++;
2774                         }
2775
2776                         /* Save "best" */
2777                         if (mp[y][x] < tp)
2778                         {
2779                                 /* Save the char, attr and priority */
2780                                 mc[y][x] = tc;
2781                                 ma[y][x] = ta;
2782                                 mp[y][x] = tp;
2783                         }
2784                 }
2785         }
2786
2787
2788         /* Corners */
2789         x = wid + 1;
2790         y = hgt + 1;
2791
2792         /* Draw the corners */
2793         mc[0][0] = mc[0][x] = mc[y][0] = mc[y][x] = '+';
2794
2795         /* Draw the horizontal edges */
2796         for (x = 1; x <= wid; x++) mc[0][x] = mc[y][x] = '-';
2797
2798         /* Draw the vertical edges */
2799         for (y = 1; y <= hgt; y++) mc[y][0] = mc[y][x] = '|';
2800
2801
2802         /* Display each map line in order */
2803         for (y = 0; y < hgt + 2; ++y)
2804         {
2805                 /* Start a new line */
2806                 Term_gotoxy(COL_MAP, y);
2807
2808                 /* Display the line */
2809                 for (x = 0; x < wid + 2; ++x)
2810                 {
2811                         ta = ma[y][x];
2812                         tc = mc[y][x];
2813
2814                         /* Hack -- fake monochrome */
2815                         if (!use_graphics)
2816                         {
2817                                 if (current_world_ptr->timewalk_m_idx) ta = TERM_DARK;
2818                                 else if (IS_INVULN() || p_ptr->timewalk) ta = TERM_WHITE;
2819                                 else if (p_ptr->wraith_form) ta = TERM_L_DARK;
2820                         }
2821
2822                         /* Add the character */
2823                         Term_add_bigch(ta, tc);
2824                 }
2825         }
2826
2827
2828         for (y = 1; y < hgt + 1; ++y)
2829         {
2830                 match_autopick = -1;
2831                 for (x = 1; x <= wid; x++) {
2832                         if (match_autopick_yx[y][x] != -1 &&
2833                                 (match_autopick > match_autopick_yx[y][x] ||
2834                                         match_autopick == -1)) {
2835                                 match_autopick = match_autopick_yx[y][x];
2836                                 autopick_obj = object_autopick_yx[y][x];
2837                         }
2838                 }
2839
2840                 /* Clear old display */
2841                 Term_putstr(0, y, 12, 0, "            ");
2842
2843                 if (match_autopick != -1)
2844 #if 1
2845                         display_shortened_item_name(autopick_obj, y);
2846 #else
2847                 {
2848                         char buf[13] = "\0";
2849                         strncpy(buf, autopick_list[match_autopick].name, 12);
2850                         buf[12] = '\0';
2851                         put_str(buf, y, 0);
2852                 }
2853 #endif
2854
2855         }
2856
2857         /* Player location */
2858         (*cy) = p_ptr->y / yrat + 1 + ROW_MAP;
2859         if (!use_bigtile)
2860                 (*cx) = p_ptr->x / xrat + 1 + COL_MAP;
2861         else
2862                 (*cx) = (p_ptr->x / xrat + 1) * 2 + COL_MAP;
2863
2864         /* Restore lighting effects */
2865         view_special_lite = old_view_special_lite;
2866         view_granite_lite = old_view_granite_lite;
2867
2868         /* Free each line map */
2869         for (y = 0; y < (hgt + 2); y++)
2870         {
2871                 /* Free one row each array */
2872                 C_KILL(ma[y], (wid + 2), TERM_COLOR);
2873                 C_KILL(mc[y], (wid + 2), SYMBOL_CODE);
2874                 C_KILL(mp[y], (wid + 2), byte);
2875                 C_KILL(match_autopick_yx[y], (wid + 2), int);
2876                 C_KILL(object_autopick_yx[y], (wid + 2), object_type *);
2877         }
2878
2879         /* Free each line map */
2880         C_KILL(ma, (hgt + 2), TERM_COLOR *);
2881         C_KILL(mc, (hgt + 2), char_ptr);
2882         C_KILL(mp, (hgt + 2), byte_ptr);
2883         C_KILL(match_autopick_yx, (hgt + 2), sint_ptr);
2884         C_KILL(object_autopick_yx, (hgt + 2), object_type **);
2885
2886         /* Free each line map */
2887         for (y = 0; y < (current_floor_ptr->height + 2); y++)
2888         {
2889                 /* Free one row each array */
2890                 C_KILL(bigma[y], (current_floor_ptr->width + 2), TERM_COLOR);
2891                 C_KILL(bigmc[y], (current_floor_ptr->width + 2), SYMBOL_CODE);
2892                 C_KILL(bigmp[y], (current_floor_ptr->width + 2), byte);
2893         }
2894
2895         /* Free each line map */
2896         C_KILL(bigma, (current_floor_ptr->height + 2), TERM_COLOR *);
2897         C_KILL(bigmc, (current_floor_ptr->height + 2), char_ptr);
2898         C_KILL(bigmp, (current_floor_ptr->height + 2), byte_ptr);
2899 }
2900
2901
2902 /*
2903  * Display a "small-scale" map of the dungeon for the player
2904  *
2905  * Currently, the "player" is displayed on the map.
2906  */
2907 void do_cmd_view_map(void)
2908 {
2909         int cy, cx;
2910
2911         screen_save();
2912
2913         prt(_("お待ち下さい...", "Please wait..."), 0, 0);
2914
2915         Term_fresh();
2916         Term_clear();
2917
2918         display_autopick = 0;
2919
2920         /* Display the map */
2921         display_map(&cy, &cx);
2922
2923         /* Wait for it */
2924         if (max_autopick && !p_ptr->wild_mode)
2925         {
2926                 display_autopick = ITEM_DISPLAY;
2927
2928                 while (1)
2929                 {
2930                         int i;
2931                         byte flag;
2932
2933                         int wid, hgt, row_message;
2934
2935                         Term_get_size(&wid, &hgt);
2936                         row_message = hgt - 1;
2937
2938                         put_str(_("何かキーを押してください('M':拾う 'N':放置 'D':M+N 'K':壊すアイテムを表示)",
2939                                 " Hit M, N(for ~), K(for !), or D(same as M+N) to display auto-picker items."), row_message, 1);
2940
2941                         /* Hilite the player */
2942                         move_cursor(cy, cx);
2943
2944                         i = inkey();
2945
2946                         if ('M' == i)
2947                                 flag = (DO_AUTOPICK | DO_QUERY_AUTOPICK);
2948                         else if ('N' == i)
2949                                 flag = DONT_AUTOPICK;
2950                         else if ('K' == i)
2951                                 flag = DO_AUTODESTROY;
2952                         else if ('D' == i)
2953                                 flag = (DO_AUTOPICK | DO_QUERY_AUTOPICK | DONT_AUTOPICK);
2954                         else
2955                                 break;
2956
2957                         Term_fresh();
2958
2959                         if (~display_autopick & flag)
2960                                 display_autopick |= flag;
2961                         else
2962                                 display_autopick &= ~flag;
2963                         /* Display the map */
2964                         display_map(&cy, &cx);
2965                 }
2966
2967                 display_autopick = 0;
2968
2969         }
2970         else
2971         {
2972                 put_str(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"), 23, 30);
2973                 /* Hilite the player */
2974                 move_cursor(cy, cx);
2975                 /* Get any key */
2976                 inkey();
2977         }
2978         screen_load();
2979 }
2980
2981
2982
2983
2984
2985 /*
2986  * Some comments on the grid flags.  -BEN-
2987  *
2988  *
2989  * One of the major bottlenecks in previous versions of Angband was in
2990  * the calculation of "line of sight" from the player to various grids,
2991  * such as monsters.  This was such a nasty bottleneck that a lot of
2992  * silly things were done to reduce the dependancy on "line of sight",
2993  * for example, you could not "see" any grids in a lit room until you
2994  * actually entered the room, and there were all kinds of bizarre grid
2995  * flags to enable this behavior.  This is also why the "call light"
2996  * spells always lit an entire room.
2997  *
2998  * The code below provides functions to calculate the "field of view"
2999  * for the player, which, once calculated, provides extremely fast
3000  * calculation of "line of sight from the player", and to calculate
3001  * the "field of torch lite", which, again, once calculated, provides
3002  * extremely fast calculation of "which grids are lit by the player's
3003  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
3004  * "GRID_LITE", as appropriate, these functions maintain an array for
3005  * each of these two flags, each array containing the locations of all
3006  * of the grids marked with the appropriate flag, which can be used to
3007  * very quickly scan through all of the grids in a given set.
3008  *
3009  * To allow more "semantically valid" field of view semantics, whenever
3010  * the field of view (or the set of torch lit grids) changes, all of the
3011  * grids in the field of view (or the set of torch lit grids) are "drawn"
3012  * so that changes in the world will become apparent as soon as possible.
3013  * This has been optimized so that only grids which actually "change" are
3014  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
3015  * of the grids which are entering or leaving the relevent set of grids.
3016  *
3017  * These new methods are so efficient that the old nasty code was removed.
3018  *
3019  * Note that there is no reason to "update" the "viewable space" unless
3020  * the player "moves", or walls/doors are created/destroyed, and there
3021  * is no reason to "update" the "torch lit grids" unless the field of
3022  * view changes, or the "light radius" changes.  This means that when
3023  * the player is resting, or digging, or doing anything that does not
3024  * involve movement or changing the state of the dungeon, there is no
3025  * need to update the "view" or the "lite" regions, which is nice.
3026  *
3027  * Note that the calls to the nasty "los()" function have been reduced
3028  * to a bare minimum by the use of the new "field of view" calculations.
3029  *
3030  * I wouldn't be surprised if slight modifications to the "update_view()"
3031  * function would allow us to determine "reverse line-of-sight" as well
3032  * as "normal line-of-sight", which would allow monsters to use a more
3033  * "correct" calculation to determine if they can "see" the player.  For
3034  * now, monsters simply "cheat" somewhat and assume that if the player
3035  * has "line of sight" to the monster, then the monster can "pretend"
3036  * that it has "line of sight" to the player.
3037  *
3038  *
3039  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
3040  * grid and maintains an array of all "CAVE_LITE" grids.
3041  *
3042  * This set of grids is the complete set of all grids which are lit by
3043  * the players light source, which allows the "player_can_see_bold()"
3044  * function to work very quickly.
3045  *
3046  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
3047  * fact, the player (unless blind) can always "see" all grids which are
3048  * marked as "CAVE_LITE", unless they are "off screen".
3049  *
3050  *
3051  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
3052  * grid and maintains an array of all "CAVE_VIEW" grids.
3053  *
3054  * This set of grids is the complete set of all grids within line of sight
3055  * of the player, allowing the "player_has_los_bold()" macro to work very
3056  * quickly.
3057  *
3058  *
3059  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
3060  * temporary internal flag to mark those grids which are not only in view,
3061  * but which are also "easily" in line of sight of the player.  This flag
3062  * is always cleared when we are done.
3063  *
3064  *
3065  * The current "update_lite()" and "update_view()" algorithms use the
3066  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
3067  * to keep track of which grids were previously marked as "CAVE_LITE" or
3068  * "CAVE_VIEW", which allows us to optimize the "screen updates".
3069  *
3070  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
3071  * for various other purposes, such as spreading lite or darkness during
3072  * "lite_room()" / "unlite_room()", and for calculating monster flow.
3073  *
3074  *
3075  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
3076  * in some way permanently lit.  However, for the player to "see" anything
3077  * in the grid, as determined by "player_can_see()", the player must not be
3078  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
3079  * grids, even if marked as "perma lit", are only illuminated if they touch
3080  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
3081  *
3082  *
3083  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
3084  * that even if the player cannot "see" the grid, he "knows" the terrain in
3085  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
3086  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
3087  * or when one of the "memorize floor grids" options induces memorization.
3088  *
3089  * Objects are "memorized" in a different way, using a special "marked" flag
3090  * on the object itself, which is set when an object is observed or detected.
3091  *
3092  *
3093  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
3094  * and should be illuminated by "lite room" and "darkness" spells.
3095  *
3096  *
3097  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
3098  * and should be unavailable for "teleportation" destinations.
3099  *
3100  *
3101  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
3102  * which is observed, and the "view_torch_grids" allows the player to memorize
3103  * every torch-lit grid.  The player will always memorize important walls,
3104  * doors, stairs, and other terrain features, as well as any "detected" grids.
3105  *
3106  * Note that the new "update_view()" method allows, among other things, a room
3107  * to be "partially" seen as the player approaches it, with a growing cone of
3108  * floor appearing as the player gets closer to the door.  Also, by not turning
3109  * on the "memorize perma-lit grids" option, the player will only "see" those
3110  * floor grids which are actually in line of sight.
3111  *
3112  * And my favorite "plus" is that you can now use a special option to draw the
3113  * "floors" in the "viewable region" brightly (actually, to draw the *other*
3114  * grids dimly), providing a "pretty" effect as the player runs around, and
3115  * to efficiently display the "torch lite" in a special color.
3116  *
3117  *
3118  * Some comments on the "update_view()" algorithm...
3119  *
3120  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
3121  * and only has to call "los()" on the borderline cases.  The major axes/diags
3122  * even terminate early when they hit walls.  I need to find a quick way
3123  * to "terminate" the other scans.
3124  *
3125  * Note that in the worst case (a big empty area with say 5% scattered walls),
3126  * each of the 1500 or so nearby grids is checked once, most of them getting
3127  * an "instant" rating, and only a small portion requiring a call to "los()".
3128  *
3129  * The only time that the algorithm appears to be "noticeably" too slow is
3130  * when running, and this is usually only important in town, since the town
3131  * provides about the worst scenario possible, with large open regions and
3132  * a few scattered obstructions.  There is a special "efficiency" option to
3133  * allow the player to reduce his field of view in town, if needed.
3134  *
3135  * In the "best" case (say, a normal stretch of corridor), the algorithm
3136  * makes one check for each viewable grid, and makes no calls to "los()".
3137  * So running in corridors is very fast, and if a lot of monsters are
3138  * nearby, it is much faster than the old methods.
3139  *
3140  * Note that resting, most normal commands, and several forms of running,
3141  * plus all commands executed near large groups of monsters, are strictly
3142  * more efficient with "update_view()" that with the old "compute los() on
3143  * demand" method, primarily because once the "field of view" has been
3144  * calculated, it does not have to be recalculated until the player moves
3145  * (or a wall or door is created or destroyed).
3146  *
3147  * Note that we no longer have to do as many "los()" checks, since once the
3148  * "view" region has been built, very few things cause it to be "changed"
3149  * (player movement, and the opening/closing of doors, changes in wall status).
3150  * Note that door/wall changes are only relevant when the door/wall itself is
3151  * in the "view" region.
3152  *
3153  * The algorithm seems to only call "los()" from zero to ten times, usually
3154  * only when coming down a corridor into a room, or standing in a room, just
3155  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
3156  * we will be reducing the calls to "los()".
3157  *
3158  * I am thinking in terms of an algorithm that "walks" from the central point
3159  * out to the maximal "distance", at each point, determining the "view" code
3160  * (above).  For each grid not on a major axis or diagonal, the "view" code
3161  * depends on the "cave_los_bold()" and "view" of exactly two other grids
3162  * (the one along the nearest diagonal, and the one next to that one, see
3163  * "update_view_aux()"...).
3164  *
3165  * We "memorize" the viewable space array, so that at the cost of under 3000
3166  * bytes, we reduce the time taken by "forget_view()" to one assignment for
3167  * each grid actually in the "viewable space".  And for another 3000 bytes,
3168  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
3169  * are also used by other routines, thus reducing the cost to almost nothing.
3170  *
3171  * A similar thing is done for "forget_lite()" in which case the savings are
3172  * much less, but save us from doing bizarre maintenance checking.
3173  *
3174  * In the worst "normal" case (in the middle of the town), the reachable space
3175  * actually reaches to more than half of the largest possible "circle" of view,
3176  * or about 800 grids, and in the worse case (in the middle of a dungeon level
3177  * where all the walls have been removed), the reachable space actually reaches
3178  * the theoretical maximum size of just under 1500 grids.
3179  *
3180  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
3181  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
3182  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
3183  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
3184  * entire possible space (including initialization) in one step per grid.  If
3185  * we do the "clearing" as a separate step (and use an array of "view" grids),
3186  * then the clearing will take as many steps as grids that were viewed, and the
3187  * algorithm will be able to "stop" scanning at various points.
3188  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
3189  */
3190
3191
3192
3193
3194
3195
3196
3197
3198  /*
3199   * Actually erase the entire "lite" array, redrawing every grid
3200   */
3201 void forget_lite(void)
3202 {
3203         int i;
3204         POSITION x, y;
3205
3206         /* None to forget */
3207         if (!current_floor_ptr->lite_n) return;
3208
3209         /* Clear them all */
3210         for (i = 0; i < current_floor_ptr->lite_n; i++)
3211         {
3212                 y = current_floor_ptr->lite_y[i];
3213                 x = current_floor_ptr->lite_x[i];
3214
3215                 /* Forget "LITE" flag */
3216                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_LITE);
3217
3218                 /* lite_spot(y, x); Perhaps don't need? */
3219         }
3220
3221         /* None left */
3222         current_floor_ptr->lite_n = 0;
3223 }
3224
3225
3226 /*
3227  * For delayed visual update
3228  */
3229 #define cave_note_and_redraw_later(C,Y,X) \
3230 {\
3231         (C)->info |= CAVE_NOTE; \
3232         cave_redraw_later((C), (Y), (X)); \
3233 }
3234
3235
3236  /*
3237   * For delayed visual update
3238   */
3239 #define cave_redraw_later(C,Y,X) \
3240 {\
3241         if (!((C)->info & CAVE_REDRAW)) \
3242         { \
3243                 (C)->info |= CAVE_REDRAW; \
3244                 current_floor_ptr->redraw_y[current_floor_ptr->redraw_n] = (Y); \
3245                 current_floor_ptr->redraw_x[current_floor_ptr->redraw_n++] = (X); \
3246         } \
3247 }
3248
3249
3250   /*
3251    * This macro allows us to efficiently add a grid to the "lite" array,
3252    * note that we are never called for illegal grids, or for grids which
3253    * have already been placed into the "lite" array, and we are never
3254    * called when the "lite" array is full.
3255    */
3256 #define cave_lite_hack(Y,X) \
3257 {\
3258         if (!(current_floor_ptr->grid_array[Y][X].info & (CAVE_LITE))) \
3259         { \
3260                 current_floor_ptr->grid_array[Y][X].info |= (CAVE_LITE); \
3261                 current_floor_ptr->lite_y[current_floor_ptr->lite_n] = (Y); \
3262                 current_floor_ptr->lite_x[current_floor_ptr->lite_n++] = (X); \
3263         } \
3264 }
3265
3266
3267    /*
3268         * Update the set of grids "illuminated" by the player's lite.
3269         *
3270         * This routine needs to use the results of "update_view()"
3271         *
3272         * Note that "blindness" does NOT affect "torch lite".  Be careful!
3273         *
3274         * We optimize most lites (all non-artifact lites) by using "obvious"
3275         * facts about the results of "small" lite radius, and we attempt to
3276         * list the "nearby" grids before the more "distant" ones in the
3277         * array of torch-lit grids.
3278         *
3279         * We assume that "radius zero" lite is in fact no lite at all.
3280         *
3281         *     Torch     Lantern     Artifacts
3282         *     (etc)
3283         *                              ***
3284         *                 ***         *****
3285         *      ***       *****       *******
3286         *      *@*       **@**       ***@***
3287         *      ***       *****       *******
3288         *                 ***         *****
3289         *                              ***
3290         */
3291 void update_lite(void)
3292 {
3293         int i;
3294         POSITION x, y, min_x, max_x, min_y, max_y;
3295         POSITION p = p_ptr->cur_lite;
3296         grid_type *g_ptr;
3297
3298         /*** Special case ***/
3299
3300 #if 0
3301         /* Hack -- Player has no lite */
3302         if (p <= 0)
3303         {
3304                 /* Forget the old lite */
3305                 /* forget_lite(); Perhaps don't need? */
3306
3307                 /* Add it to later visual update */
3308                 cave_redraw_later(&current_floor_ptr->grid_array[p_ptr->y][p_ptr->x], p_ptr->y, p_ptr->x);
3309         }
3310 #endif
3311
3312         /*** Save the old "lite" grids for later ***/
3313
3314         /* Clear them all */
3315         for (i = 0; i < current_floor_ptr->lite_n; i++)
3316         {
3317                 y = current_floor_ptr->lite_y[i];
3318                 x = current_floor_ptr->lite_x[i];
3319
3320                 /* Mark the grid as not "lite" */
3321                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_LITE);
3322
3323                 /* Mark the grid as "seen" */
3324                 current_floor_ptr->grid_array[y][x].info |= (CAVE_TEMP);
3325
3326                 /* Add it to the "seen" set */
3327                 tmp_pos.y[tmp_pos.n] = y;
3328                 tmp_pos.x[tmp_pos.n] = x;
3329                 tmp_pos.n++;
3330         }
3331
3332         /* None left */
3333         current_floor_ptr->lite_n = 0;
3334
3335
3336         /*** Collect the new "lite" grids ***/
3337
3338         /* Radius 1 -- torch radius */
3339         if (p >= 1)
3340         {
3341                 /* Player grid */
3342                 cave_lite_hack(p_ptr->y, p_ptr->x);
3343
3344                 /* Adjacent grid */
3345                 cave_lite_hack(p_ptr->y + 1, p_ptr->x);
3346                 cave_lite_hack(p_ptr->y - 1, p_ptr->x);
3347                 cave_lite_hack(p_ptr->y, p_ptr->x + 1);
3348                 cave_lite_hack(p_ptr->y, p_ptr->x - 1);
3349
3350                 /* Diagonal grids */
3351                 cave_lite_hack(p_ptr->y + 1, p_ptr->x + 1);
3352                 cave_lite_hack(p_ptr->y + 1, p_ptr->x - 1);
3353                 cave_lite_hack(p_ptr->y - 1, p_ptr->x + 1);
3354                 cave_lite_hack(p_ptr->y - 1, p_ptr->x - 1);
3355         }
3356
3357         /* Radius 2 -- lantern radius */
3358         if (p >= 2)
3359         {
3360                 /* South of the player */
3361                 if (cave_los_bold(p_ptr->y + 1, p_ptr->x))
3362                 {
3363                         cave_lite_hack(p_ptr->y + 2, p_ptr->x);
3364                         cave_lite_hack(p_ptr->y + 2, p_ptr->x + 1);
3365                         cave_lite_hack(p_ptr->y + 2, p_ptr->x - 1);
3366                 }
3367
3368                 /* North of the player */
3369                 if (cave_los_bold(p_ptr->y - 1, p_ptr->x))
3370                 {
3371                         cave_lite_hack(p_ptr->y - 2, p_ptr->x);
3372                         cave_lite_hack(p_ptr->y - 2, p_ptr->x + 1);
3373                         cave_lite_hack(p_ptr->y - 2, p_ptr->x - 1);
3374                 }
3375
3376                 /* East of the player */
3377                 if (cave_los_bold(p_ptr->y, p_ptr->x + 1))
3378                 {
3379                         cave_lite_hack(p_ptr->y, p_ptr->x + 2);
3380                         cave_lite_hack(p_ptr->y + 1, p_ptr->x + 2);
3381                         cave_lite_hack(p_ptr->y - 1, p_ptr->x + 2);
3382                 }
3383
3384                 /* West of the player */
3385                 if (cave_los_bold(p_ptr->y, p_ptr->x - 1))
3386                 {
3387                         cave_lite_hack(p_ptr->y, p_ptr->x - 2);
3388                         cave_lite_hack(p_ptr->y + 1, p_ptr->x - 2);
3389                         cave_lite_hack(p_ptr->y - 1, p_ptr->x - 2);
3390                 }
3391         }
3392
3393         /* Radius 3+ -- artifact radius */
3394         if (p >= 3)
3395         {
3396                 int d;
3397
3398                 /* Paranoia -- see "LITE_MAX" */
3399                 if (p > 14) p = 14;
3400
3401                 /* South-East of the player */
3402                 if (cave_los_bold(p_ptr->y + 1, p_ptr->x + 1))
3403                 {
3404                         cave_lite_hack(p_ptr->y + 2, p_ptr->x + 2);
3405                 }
3406
3407                 /* South-West of the player */
3408                 if (cave_los_bold(p_ptr->y + 1, p_ptr->x - 1))
3409                 {
3410                         cave_lite_hack(p_ptr->y + 2, p_ptr->x - 2);
3411                 }
3412
3413                 /* North-East of the player */
3414                 if (cave_los_bold(p_ptr->y - 1, p_ptr->x + 1))
3415                 {
3416                         cave_lite_hack(p_ptr->y - 2, p_ptr->x + 2);
3417                 }
3418
3419                 /* North-West of the player */
3420                 if (cave_los_bold(p_ptr->y - 1, p_ptr->x - 1))
3421                 {
3422                         cave_lite_hack(p_ptr->y - 2, p_ptr->x - 2);
3423                 }
3424
3425                 /* Maximal north */
3426                 min_y = p_ptr->y - p;
3427                 if (min_y < 0) min_y = 0;
3428
3429                 /* Maximal south */
3430                 max_y = p_ptr->y + p;
3431                 if (max_y > current_floor_ptr->height - 1) max_y = current_floor_ptr->height - 1;
3432
3433                 /* Maximal west */
3434                 min_x = p_ptr->x - p;
3435                 if (min_x < 0) min_x = 0;
3436
3437                 /* Maximal east */
3438                 max_x = p_ptr->x + p;
3439                 if (max_x > current_floor_ptr->width - 1) max_x = current_floor_ptr->width - 1;
3440
3441                 /* Scan the maximal box */
3442                 for (y = min_y; y <= max_y; y++)
3443                 {
3444                         for (x = min_x; x <= max_x; x++)
3445                         {
3446                                 int dy = (p_ptr->y > y) ? (p_ptr->y - y) : (y - p_ptr->y);
3447                                 int dx = (p_ptr->x > x) ? (p_ptr->x - x) : (x - p_ptr->x);
3448
3449                                 /* Skip the "central" grids (above) */
3450                                 if ((dy <= 2) && (dx <= 2)) continue;
3451
3452                                 /* Hack -- approximate the distance */
3453                                 d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
3454
3455                                 /* Skip distant grids */
3456                                 if (d > p) continue;
3457
3458                                 /* Viewable, nearby, grids get "torch lit" */
3459                                 if (current_floor_ptr->grid_array[y][x].info & CAVE_VIEW)
3460                                 {
3461                                         /* This grid is "torch lit" */
3462                                         cave_lite_hack(y, x);
3463                                 }
3464                         }
3465                 }
3466         }
3467
3468
3469         /*** Complete the algorithm ***/
3470
3471         /* Draw the new grids */
3472         for (i = 0; i < current_floor_ptr->lite_n; i++)
3473         {
3474                 y = current_floor_ptr->lite_y[i];
3475                 x = current_floor_ptr->lite_x[i];
3476
3477                 g_ptr = &current_floor_ptr->grid_array[y][x];
3478
3479                 /* Update fresh grids */
3480                 if (g_ptr->info & (CAVE_TEMP)) continue;
3481
3482                 /* Add it to later visual update */
3483                 cave_note_and_redraw_later(g_ptr, y, x);
3484         }
3485
3486         /* Clear them all */
3487         for (i = 0; i < tmp_pos.n; i++)
3488         {
3489                 y = tmp_pos.y[i];
3490                 x = tmp_pos.x[i];
3491
3492                 g_ptr = &current_floor_ptr->grid_array[y][x];
3493
3494                 /* No longer in the array */
3495                 g_ptr->info &= ~(CAVE_TEMP);
3496
3497                 /* Update stale grids */
3498                 if (g_ptr->info & (CAVE_LITE)) continue;
3499
3500                 /* Add it to later visual update */
3501                 cave_redraw_later(g_ptr, y, x);
3502         }
3503
3504         /* None left */
3505         tmp_pos.n = 0;
3506
3507         /* Mega-Hack -- Visual update later */
3508         p_ptr->update |= (PU_DELAY_VIS);
3509 }
3510
3511
3512 static bool mon_invis;
3513 static POSITION mon_fy, mon_fx;
3514
3515 /*
3516  * Add a square to the changes array
3517  */
3518 static void mon_lite_hack(POSITION y, POSITION x)
3519 {
3520         grid_type *g_ptr;
3521         int dpf, d;
3522         POSITION midpoint;
3523
3524         /* We trust this grid is in bounds */
3525         /* if (!in_bounds2(y, x)) return; */
3526
3527         g_ptr = &current_floor_ptr->grid_array[y][x];
3528
3529         /* Want a unlit square in view of the player */
3530         if ((g_ptr->info & (CAVE_MNLT | CAVE_VIEW)) != CAVE_VIEW) return;
3531
3532         if (!cave_los_grid(g_ptr))
3533         {
3534                 /* Hack -- Prevent monster lite leakage in walls */
3535
3536                 /* Horizontal walls between player and a monster */
3537                 if (((y < p_ptr->y) && (y > mon_fy)) || ((y > p_ptr->y) && (y < mon_fy)))
3538                 {
3539                         dpf = p_ptr->y - mon_fy;
3540                         d = y - mon_fy;
3541                         midpoint = mon_fx + ((p_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
3542
3543                         /* Only first wall viewed from mid-x is lit */
3544                         if (x < midpoint)
3545                         {
3546                                 if (!cave_los_bold(y, x + 1)) return;
3547                         }
3548                         else if (x > midpoint)
3549                         {
3550                                 if (!cave_los_bold(y, x - 1)) return;
3551                         }
3552
3553                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
3554                         else if (mon_invis) return;
3555                 }
3556
3557                 /* Vertical walls between player and a monster */
3558                 if (((x < p_ptr->x) && (x > mon_fx)) || ((x > p_ptr->x) && (x < mon_fx)))
3559                 {
3560                         dpf = p_ptr->x - mon_fx;
3561                         d = x - mon_fx;
3562                         midpoint = mon_fy + ((p_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
3563
3564                         /* Only first wall viewed from mid-y is lit */
3565                         if (y < midpoint)
3566                         {
3567                                 if (!cave_los_bold(y + 1, x)) return;
3568                         }
3569                         else if (y > midpoint)
3570                         {
3571                                 if (!cave_los_bold(y - 1, x)) return;
3572                         }
3573
3574                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
3575                         else if (mon_invis) return;
3576                 }
3577         }
3578
3579         /* We trust tmp_pos.n does not exceed TEMP_MAX */
3580
3581         /* New grid */
3582         if (!(g_ptr->info & CAVE_MNDK))
3583         {
3584                 /* Save this square */
3585                 tmp_pos.x[tmp_pos.n] = x;
3586                 tmp_pos.y[tmp_pos.n] = y;
3587                 tmp_pos.n++;
3588         }
3589
3590         /* Darkened grid */
3591         else
3592         {
3593                 /* No longer dark */
3594                 g_ptr->info &= ~(CAVE_MNDK);
3595         }
3596
3597         /* Light it */
3598         g_ptr->info |= CAVE_MNLT;
3599 }
3600
3601
3602 /*
3603  * Add a square to the changes array
3604  */
3605 static void mon_dark_hack(POSITION y, POSITION x)
3606 {
3607         grid_type *g_ptr;
3608         int       midpoint, dpf, d;
3609
3610         /* We trust this grid is in bounds */
3611         /* if (!in_bounds2(y, x)) return; */
3612
3613         g_ptr = &current_floor_ptr->grid_array[y][x];
3614
3615         /* Want a unlit and undarkened square in view of the player */
3616         if ((g_ptr->info & (CAVE_LITE | CAVE_MNLT | CAVE_MNDK | CAVE_VIEW)) != CAVE_VIEW) return;
3617
3618         if (!cave_los_grid(g_ptr) && !cave_have_flag_grid(g_ptr, FF_PROJECT))
3619         {
3620                 /* Hack -- Prevent monster dark lite leakage in walls */
3621
3622                 /* Horizontal walls between player and a monster */
3623                 if (((y < p_ptr->y) && (y > mon_fy)) || ((y > p_ptr->y) && (y < mon_fy)))
3624                 {
3625                         dpf = p_ptr->y - mon_fy;
3626                         d = y - mon_fy;
3627                         midpoint = mon_fx + ((p_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
3628
3629                         /* Only first wall viewed from mid-x is lit */
3630                         if (x < midpoint)
3631                         {
3632                                 if (!cave_los_bold(y, x + 1) && !cave_have_flag_bold(y, x + 1, FF_PROJECT)) return;
3633                         }
3634                         else if (x > midpoint)
3635                         {
3636                                 if (!cave_los_bold(y, x - 1) && !cave_have_flag_bold(y, x - 1, FF_PROJECT)) return;
3637                         }
3638
3639                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
3640                         else if (mon_invis) return;
3641                 }
3642
3643                 /* Vertical walls between player and a monster */
3644                 if (((x < p_ptr->x) && (x > mon_fx)) || ((x > p_ptr->x) && (x < mon_fx)))
3645                 {
3646                         dpf = p_ptr->x - mon_fx;
3647                         d = x - mon_fx;
3648                         midpoint = mon_fy + ((p_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
3649
3650                         /* Only first wall viewed from mid-y is lit */
3651                         if (y < midpoint)
3652                         {
3653                                 if (!cave_los_bold(y + 1, x) && !cave_have_flag_bold(y + 1, x, FF_PROJECT)) return;
3654                         }
3655                         else if (y > midpoint)
3656                         {
3657                                 if (!cave_los_bold(y - 1, x) && !cave_have_flag_bold(y - 1, x, FF_PROJECT)) return;
3658                         }
3659
3660                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
3661                         else if (mon_invis) return;
3662                 }
3663         }
3664
3665         /* We trust tmp_pos.n does not exceed TEMP_MAX */
3666
3667         /* Save this square */
3668         tmp_pos.x[tmp_pos.n] = x;
3669         tmp_pos.y[tmp_pos.n] = y;
3670         tmp_pos.n++;
3671
3672         /* Darken it */
3673         g_ptr->info |= CAVE_MNDK;
3674 }
3675
3676
3677 /*
3678  * Update squares illuminated or darkened by monsters.
3679  *
3680  * Hack - use the CAVE_ROOM flag (renamed to be CAVE_MNLT) to
3681  * denote squares illuminated by monsters.
3682  *
3683  * The CAVE_TEMP and CAVE_XTRA flag are used to store the state during the
3684  * updating.  Only squares in view of the player, whos state
3685  * changes are drawn via lite_spot().
3686  */
3687 void update_mon_lite(void)
3688 {
3689         int i, rad;
3690         grid_type *g_ptr;
3691
3692         POSITION fx, fy;
3693         void(*add_mon_lite)(POSITION, POSITION);
3694         int f_flag;
3695
3696         s16b end_temp;
3697
3698         /* Non-Ninja player in the darkness */
3699         int dis_lim = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) && !p_ptr->see_nocto) ?
3700                 (MAX_SIGHT / 2 + 1) : (MAX_SIGHT + 3);
3701
3702         /* Clear all monster lit squares */
3703         for (i = 0; i < current_floor_ptr->mon_lite_n; i++)
3704         {
3705                 /* Point to grid */
3706                 g_ptr = &current_floor_ptr->grid_array[current_floor_ptr->mon_lite_y[i]][current_floor_ptr->mon_lite_x[i]];
3707
3708                 /* Set temp or xtra flag */
3709                 g_ptr->info |= (g_ptr->info & CAVE_MNLT) ? CAVE_TEMP : CAVE_XTRA;
3710
3711                 /* Clear monster illumination flag */
3712                 g_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
3713         }
3714
3715         /* Empty temp list of new squares to lite up */
3716         tmp_pos.n = 0;
3717
3718         /* If a monster stops time, don't process */
3719         if (!current_world_ptr->timewalk_m_idx)
3720         {
3721                 monster_type *m_ptr;
3722                 monster_race *r_ptr;
3723
3724                 /* Loop through monsters, adding newly lit squares to changes list */
3725                 for (i = 1; i < m_max; i++)
3726                 {
3727                         m_ptr = &current_floor_ptr->m_list[i];
3728                         r_ptr = &r_info[m_ptr->r_idx];
3729                         if (!monster_is_valid(m_ptr)) continue;
3730
3731                         /* Is it too far away? */
3732                         if (m_ptr->cdis > dis_lim) continue;
3733
3734                         /* Get lite radius */
3735                         rad = 0;
3736
3737                         /* Note the radii are cumulative */
3738                         if (r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_SELF_LITE_1)) rad++;
3739                         if (r_ptr->flags7 & (RF7_HAS_LITE_2 | RF7_SELF_LITE_2)) rad += 2;
3740                         if (r_ptr->flags7 & (RF7_HAS_DARK_1 | RF7_SELF_DARK_1)) rad--;
3741                         if (r_ptr->flags7 & (RF7_HAS_DARK_2 | RF7_SELF_DARK_2)) rad -= 2;
3742
3743                         /* Exit if has no light */
3744                         if (!rad) continue;
3745                         else if (rad > 0)
3746                         {
3747                                 if (!(r_ptr->flags7 & (RF7_SELF_LITE_1 | RF7_SELF_LITE_2)) && (MON_CSLEEP(m_ptr) || (!current_floor_ptr->dun_level && is_daytime()) || p_ptr->inside_battle)) continue;
3748                                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) rad = 1;
3749                                 add_mon_lite = mon_lite_hack;
3750                                 f_flag = FF_LOS;
3751                         }
3752                         else
3753                         {
3754                                 if (!(r_ptr->flags7 & (RF7_SELF_DARK_1 | RF7_SELF_DARK_2)) && (MON_CSLEEP(m_ptr) || (!current_floor_ptr->dun_level && !is_daytime()))) continue;
3755                                 add_mon_lite = mon_dark_hack;
3756                                 f_flag = FF_PROJECT;
3757                                 rad = -rad; /* Use absolute value */
3758                         }
3759
3760                         mon_fx = m_ptr->fx;
3761                         mon_fy = m_ptr->fy;
3762
3763                         /* Is the monster visible? */
3764                         mon_invis = !(current_floor_ptr->grid_array[mon_fy][mon_fx].info & CAVE_VIEW);
3765
3766                         /* The square it is on */
3767                         add_mon_lite(mon_fy, mon_fx);
3768
3769                         /* Adjacent squares */
3770                         add_mon_lite(mon_fy + 1, mon_fx);
3771                         add_mon_lite(mon_fy - 1, mon_fx);
3772                         add_mon_lite(mon_fy, mon_fx + 1);
3773                         add_mon_lite(mon_fy, mon_fx - 1);
3774                         add_mon_lite(mon_fy + 1, mon_fx + 1);
3775                         add_mon_lite(mon_fy + 1, mon_fx - 1);
3776                         add_mon_lite(mon_fy - 1, mon_fx + 1);
3777                         add_mon_lite(mon_fy - 1, mon_fx - 1);
3778
3779                         /* Radius 2 */
3780                         if (rad >= 2)
3781                         {
3782                                 /* South of the monster */
3783                                 if (cave_have_flag_bold(mon_fy + 1, mon_fx, f_flag))
3784                                 {
3785                                         add_mon_lite(mon_fy + 2, mon_fx + 1);
3786                                         add_mon_lite(mon_fy + 2, mon_fx);
3787                                         add_mon_lite(mon_fy + 2, mon_fx - 1);
3788
3789                                         g_ptr = &current_floor_ptr->grid_array[mon_fy + 2][mon_fx];
3790
3791                                         /* Radius 3 */
3792                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
3793                                         {
3794                                                 add_mon_lite(mon_fy + 3, mon_fx + 1);
3795                                                 add_mon_lite(mon_fy + 3, mon_fx);
3796                                                 add_mon_lite(mon_fy + 3, mon_fx - 1);
3797                                         }
3798                                 }
3799
3800                                 /* North of the monster */
3801                                 if (cave_have_flag_bold(mon_fy - 1, mon_fx, f_flag))
3802                                 {
3803                                         add_mon_lite(mon_fy - 2, mon_fx + 1);
3804                                         add_mon_lite(mon_fy - 2, mon_fx);
3805                                         add_mon_lite(mon_fy - 2, mon_fx - 1);
3806
3807                                         g_ptr = &current_floor_ptr->grid_array[mon_fy - 2][mon_fx];
3808
3809                                         /* Radius 3 */
3810                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
3811                                         {
3812                                                 add_mon_lite(mon_fy - 3, mon_fx + 1);
3813                                                 add_mon_lite(mon_fy - 3, mon_fx);
3814                                                 add_mon_lite(mon_fy - 3, mon_fx - 1);
3815                                         }
3816                                 }
3817
3818                                 /* East of the monster */
3819                                 if (cave_have_flag_bold(mon_fy, mon_fx + 1, f_flag))
3820                                 {
3821                                         add_mon_lite(mon_fy + 1, mon_fx + 2);
3822                                         add_mon_lite(mon_fy, mon_fx + 2);
3823                                         add_mon_lite(mon_fy - 1, mon_fx + 2);
3824
3825                                         g_ptr = &current_floor_ptr->grid_array[mon_fy][mon_fx + 2];
3826
3827                                         /* Radius 3 */
3828                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
3829                                         {
3830                                                 add_mon_lite(mon_fy + 1, mon_fx + 3);
3831                                                 add_mon_lite(mon_fy, mon_fx + 3);
3832                                                 add_mon_lite(mon_fy - 1, mon_fx + 3);
3833                                         }
3834                                 }
3835
3836                                 /* West of the monster */
3837                                 if (cave_have_flag_bold(mon_fy, mon_fx - 1, f_flag))
3838                                 {
3839                                         add_mon_lite(mon_fy + 1, mon_fx - 2);
3840                                         add_mon_lite(mon_fy, mon_fx - 2);
3841                                         add_mon_lite(mon_fy - 1, mon_fx - 2);
3842
3843                                         g_ptr = &current_floor_ptr->grid_array[mon_fy][mon_fx - 2];
3844
3845                                         /* Radius 3 */
3846                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
3847                                         {
3848                                                 add_mon_lite(mon_fy + 1, mon_fx - 3);
3849                                                 add_mon_lite(mon_fy, mon_fx - 3);
3850                                                 add_mon_lite(mon_fy - 1, mon_fx - 3);
3851                                         }
3852                                 }
3853                         }
3854
3855                         /* Radius 3 */
3856                         if (rad == 3)
3857                         {
3858                                 /* South-East of the monster */
3859                                 if (cave_have_flag_bold(mon_fy + 1, mon_fx + 1, f_flag))
3860                                 {
3861                                         add_mon_lite(mon_fy + 2, mon_fx + 2);
3862                                 }
3863
3864                                 /* South-West of the monster */
3865                                 if (cave_have_flag_bold(mon_fy + 1, mon_fx - 1, f_flag))
3866                                 {
3867                                         add_mon_lite(mon_fy + 2, mon_fx - 2);
3868                                 }
3869
3870                                 /* North-East of the monster */
3871                                 if (cave_have_flag_bold(mon_fy - 1, mon_fx + 1, f_flag))
3872                                 {
3873                                         add_mon_lite(mon_fy - 2, mon_fx + 2);
3874                                 }
3875
3876                                 /* North-West of the monster */
3877                                 if (cave_have_flag_bold(mon_fy - 1, mon_fx - 1, f_flag))
3878                                 {
3879                                         add_mon_lite(mon_fy - 2, mon_fx - 2);
3880                                 }
3881                         }
3882                 }
3883         }
3884
3885         /* Save end of list of new squares */
3886         end_temp = tmp_pos.n;
3887
3888         /*
3889          * Look at old set flags to see if there are any changes.
3890          */
3891         for (i = 0; i < current_floor_ptr->mon_lite_n; i++)
3892         {
3893                 fx = current_floor_ptr->mon_lite_x[i];
3894                 fy = current_floor_ptr->mon_lite_y[i];
3895
3896                 /* We trust this grid is in bounds */
3897
3898                 /* Point to grid */
3899                 g_ptr = &current_floor_ptr->grid_array[fy][fx];
3900
3901                 if (g_ptr->info & CAVE_TEMP) /* Pervious lit */
3902                 {
3903                         /* It it no longer lit? */
3904                         if ((g_ptr->info & (CAVE_VIEW | CAVE_MNLT)) == CAVE_VIEW)
3905                         {
3906                                 /* It is now unlit */
3907                                 /* Add it to later visual update */
3908                                 cave_note_and_redraw_later(g_ptr, fy, fx);
3909                         }
3910                 }
3911                 else /* Pervious darkened */
3912                 {
3913                         /* It it no longer darken? */
3914                         if ((g_ptr->info & (CAVE_VIEW | CAVE_MNDK)) == CAVE_VIEW)
3915                         {
3916                                 /* It is now undarken */
3917                                 /* Add it to later visual update */
3918                                 cave_note_and_redraw_later(g_ptr, fy, fx);
3919                         }
3920                 }
3921
3922                 /* Add to end of temp array */
3923                 tmp_pos.x[tmp_pos.n] = fx;
3924                 tmp_pos.y[tmp_pos.n] = fy;
3925                 tmp_pos.n++;
3926         }
3927
3928         /* Clear the lite array */
3929         current_floor_ptr->mon_lite_n = 0;
3930
3931         /* Copy the temp array into the lit array lighting the new squares. */
3932         for (i = 0; i < end_temp; i++)
3933         {
3934                 fx = tmp_pos.x[i];
3935                 fy = tmp_pos.y[i];
3936
3937                 /* We trust this grid is in bounds */
3938
3939                 /* Point to grid */
3940                 g_ptr = &current_floor_ptr->grid_array[fy][fx];
3941
3942                 if (g_ptr->info & CAVE_MNLT) /* Lit */
3943                 {
3944                         /* The is the square newly lit and visible? */
3945                         if ((g_ptr->info & (CAVE_VIEW | CAVE_TEMP)) == CAVE_VIEW)
3946                         {
3947                                 /* It is now lit */
3948                                 /* Add it to later visual update */
3949                                 cave_note_and_redraw_later(g_ptr, fy, fx);
3950                         }
3951                 }
3952                 else /* Darkened */
3953                 {
3954                         /* The is the square newly darkened and visible? */
3955                         if ((g_ptr->info & (CAVE_VIEW | CAVE_XTRA)) == CAVE_VIEW)
3956                         {
3957                                 /* It is now darkened */
3958                                 /* Add it to later visual update */
3959                                 cave_note_and_redraw_later(g_ptr, fy, fx);
3960                         }
3961                 }
3962
3963                 /* Save in the monster lit or darkened array */
3964                 current_floor_ptr->mon_lite_x[current_floor_ptr->mon_lite_n] = fx;
3965                 current_floor_ptr->mon_lite_y[current_floor_ptr->mon_lite_n] = fy;
3966                 current_floor_ptr->mon_lite_n++;
3967         }
3968
3969         /* Clear the temp flag for the old lit or darken grids */
3970         for (i = end_temp; i < tmp_pos.n; i++)
3971         {
3972                 /* We trust this grid is in bounds */
3973
3974                 current_floor_ptr->grid_array[tmp_pos.y[i]][tmp_pos.x[i]].info &= ~(CAVE_TEMP | CAVE_XTRA);
3975         }
3976
3977         /* Finished with tmp_pos.n */
3978         tmp_pos.n = 0;
3979
3980         /* Mega-Hack -- Visual update later */
3981         p_ptr->update |= (PU_DELAY_VIS);
3982
3983         p_ptr->monlite = (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_MNLT) ? TRUE : FALSE;
3984
3985         if (p_ptr->special_defense & NINJA_S_STEALTH)
3986         {
3987                 if (p_ptr->old_monlite != p_ptr->monlite)
3988                 {
3989                         if (p_ptr->monlite)
3990                         {
3991                                 msg_print(_("影の覆いが薄れた気がする。", "Your mantle of shadow become thin."));
3992                         }
3993                         else
3994                         {
3995                                 msg_print(_("影の覆いが濃くなった!", "Your mantle of shadow restored its original darkness."));
3996                         }
3997                 }
3998         }
3999         p_ptr->old_monlite = p_ptr->monlite;
4000 }
4001
4002 void clear_mon_lite(void)
4003 {
4004         int i;
4005         grid_type *g_ptr;
4006
4007         /* Clear all monster lit squares */
4008         for (i = 0; i < current_floor_ptr->mon_lite_n; i++)
4009         {
4010                 /* Point to grid */
4011                 g_ptr = &current_floor_ptr->grid_array[current_floor_ptr->mon_lite_y[i]][current_floor_ptr->mon_lite_x[i]];
4012
4013                 /* Clear monster illumination flag */
4014                 g_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
4015         }
4016
4017         /* Empty the array */
4018         current_floor_ptr->mon_lite_n = 0;
4019 }
4020
4021
4022
4023 /*
4024  * Clear the viewable space
4025  */
4026 void forget_view(void)
4027 {
4028         int i;
4029
4030         grid_type *g_ptr;
4031
4032         /* None to forget */
4033         if (!current_floor_ptr->view_n) return;
4034
4035         /* Clear them all */
4036         for (i = 0; i < current_floor_ptr->view_n; i++)
4037         {
4038                 POSITION y = current_floor_ptr->view_y[i];
4039                 POSITION x = current_floor_ptr->view_x[i];
4040                 g_ptr = &current_floor_ptr->grid_array[y][x];
4041
4042                 /* Forget that the grid is viewable */
4043                 g_ptr->info &= ~(CAVE_VIEW);
4044
4045                 /* if (!panel_contains(y, x)) continue; */
4046
4047                 /* Update the screen */
4048                 /* lite_spot(y, x); Perhaps don't need? */
4049         }
4050
4051         /* None left */
4052         current_floor_ptr->view_n = 0;
4053 }
4054
4055
4056
4057 /*
4058  * This macro allows us to efficiently add a grid to the "view" array,
4059  * note that we are never called for illegal grids, or for grids which
4060  * have already been placed into the "view" array, and we are never
4061  * called when the "view" array is full.
4062  */
4063 #define cave_view_hack(C,Y,X) \
4064 {\
4065     if (!((C)->info & (CAVE_VIEW))){\
4066     (C)->info |= (CAVE_VIEW); \
4067     current_floor_ptr->view_y[current_floor_ptr->view_n] = (Y); \
4068     current_floor_ptr->view_x[current_floor_ptr->view_n] = (X); \
4069     current_floor_ptr->view_n++;}\
4070 }
4071
4072
4073
4074  /*
4075   * Helper function for "update_view()" below
4076   *
4077   * We are checking the "viewability" of grid (y,x) by the player.
4078   *
4079   * This function assumes that (y,x) is legal (i.e. on the map).
4080   *
4081   * Grid (y1,x1) is on the "diagonal" between (p_ptr->y,p_ptr->x) and (y,x)
4082   * Grid (y2,x2) is "adjacent", also between (p_ptr->y,p_ptr->x) and (y,x).
4083   *
4084   * Note that we are using the "CAVE_XTRA" field for marking grids as
4085   * "easily viewable".  This bit is cleared at the end of "update_view()".
4086   *
4087   * This function adds (y,x) to the "viewable set" if necessary.
4088   *
4089   * This function now returns "TRUE" if vision is "blocked" by grid (y,x).
4090   */
4091 static bool update_view_aux(POSITION y, POSITION x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
4092 {
4093         bool f1, f2, v1, v2, z1, z2, wall;
4094
4095         grid_type *g_ptr;
4096
4097         grid_type *g1_c_ptr;
4098         grid_type *g2_c_ptr;
4099
4100         /* Access the grids */
4101         g1_c_ptr = &current_floor_ptr->grid_array[y1][x1];
4102         g2_c_ptr = &current_floor_ptr->grid_array[y2][x2];
4103
4104
4105         /* Check for walls */
4106         f1 = (cave_los_grid(g1_c_ptr));
4107         f2 = (cave_los_grid(g2_c_ptr));
4108
4109         /* Totally blocked by physical walls */
4110         if (!f1 && !f2) return (TRUE);
4111
4112
4113         /* Check for visibility */
4114         v1 = (f1 && (g1_c_ptr->info & (CAVE_VIEW)));
4115         v2 = (f2 && (g2_c_ptr->info & (CAVE_VIEW)));
4116
4117         /* Totally blocked by "unviewable neighbors" */
4118         if (!v1 && !v2) return (TRUE);
4119
4120         g_ptr = &current_floor_ptr->grid_array[y][x];
4121
4122
4123         /* Check for walls */
4124         wall = (!cave_los_grid(g_ptr));
4125
4126
4127         /* Check the "ease" of visibility */
4128         z1 = (v1 && (g1_c_ptr->info & (CAVE_XTRA)));
4129         z2 = (v2 && (g2_c_ptr->info & (CAVE_XTRA)));
4130
4131         /* Hack -- "easy" plus "easy" yields "easy" */
4132         if (z1 && z2)
4133         {
4134                 g_ptr->info |= (CAVE_XTRA);
4135
4136                 cave_view_hack(g_ptr, y, x);
4137
4138                 return (wall);
4139         }
4140
4141         /* Hack -- primary "easy" yields "viewed" */
4142         if (z1)
4143         {
4144                 cave_view_hack(g_ptr, y, x);
4145
4146                 return (wall);
4147         }
4148
4149         /* Hack -- "view" plus "view" yields "view" */
4150         if (v1 && v2)
4151         {
4152                 /* g_ptr->info |= (CAVE_XTRA); */
4153
4154                 cave_view_hack(g_ptr, y, x);
4155
4156                 return (wall);
4157         }
4158
4159
4160         /* Mega-Hack -- the "los()" function works poorly on walls */
4161         if (wall)
4162         {
4163                 cave_view_hack(g_ptr, y, x);
4164
4165                 return (wall);
4166         }
4167
4168
4169         /* Hack -- check line of sight */
4170         if (los(p_ptr->y, p_ptr->x, y, x))
4171         {
4172                 cave_view_hack(g_ptr, y, x);
4173
4174                 return (wall);
4175         }
4176
4177
4178         /* Assume no line of sight. */
4179         return (TRUE);
4180 }
4181
4182
4183
4184 /*
4185  * Calculate the viewable space
4186  *
4187  *  1: Process the player
4188  *  1a: The player is always (easily) viewable
4189  *  2: Process the diagonals
4190  *  2a: The diagonals are (easily) viewable up to the first wall
4191  *  2b: But never go more than 2/3 of the "full" distance
4192  *  3: Process the main axes
4193  *  3a: The main axes are (easily) viewable up to the first wall
4194  *  3b: But never go more than the "full" distance
4195  *  4: Process sequential "strips" in each of the eight octants
4196  *  4a: Each strip runs along the previous strip
4197  *  4b: The main axes are "previous" to the first strip
4198  *  4c: Process both "sides" of each "direction" of each strip
4199  *  4c1: Each side aborts as soon as possible
4200  *  4c2: Each side tells the next strip how far it has to check
4201  *
4202  * Note that the octant processing involves some pretty interesting
4203  * observations involving when a grid might possibly be viewable from
4204  * a given grid, and on the order in which the strips are processed.
4205  *
4206  * Note the use of the mathematical facts shown below, which derive
4207  * from the fact that (1 < sqrt(2) < 1.5), and that the length of the
4208  * hypotenuse of a right triangle is primarily determined by the length
4209  * of the longest side, when one side is small, and is strictly less
4210  * than one-and-a-half times as long as the longest side when both of
4211  * the sides are large.
4212  *
4213  *   if (manhatten(dy,dx) < R) then (hypot(dy,dx) < R)
4214  *   if (manhatten(dy,dx) > R*3/2) then (hypot(dy,dx) > R)
4215  *
4216  *   hypot(dy,dx) is approximated by (dx+dy+MAX(dx,dy)) / 2
4217  *
4218  * These observations are important because the calculation of the actual
4219  * value of "hypot(dx,dy)" is extremely expensive, involving square roots,
4220  * while for small values (up to about 20 or so), the approximations above
4221  * are correct to within an error of at most one grid or so.
4222  *
4223  * Observe the use of "full" and "over" in the code below, and the use of
4224  * the specialized calculation involving "limit", all of which derive from
4225  * the observations given above.  Basically, we note that the "circle" of
4226  * view is completely contained in an "octagon" whose bounds are easy to
4227  * determine, and that only a few steps are needed to derive the actual
4228  * bounds of the circle given the bounds of the octagon.
4229  *
4230  * Note that by skipping all the grids in the corners of the octagon, we
4231  * place an upper limit on the number of grids in the field of view, given
4232  * that "full" is never more than 20.  Of the 1681 grids in the "square" of
4233  * view, only about 1475 of these are in the "octagon" of view, and even
4234  * fewer are in the "circle" of view, so 1500 or 1536 is more than enough
4235  * entries to completely contain the actual field of view.
4236  *
4237  * Note also the care taken to prevent "running off the map".  The use of
4238  * explicit checks on the "validity" of the "diagonal", and the fact that
4239  * the loops are never allowed to "leave" the map, lets "update_view_aux()"
4240  * use the optimized "cave_los_bold()" macro, and to avoid the overhead
4241  * of multiple checks on the validity of grids.
4242  *
4243  * Note the "optimizations" involving the "se","sw","ne","nw","es","en",
4244  * "ws","wn" variables.  They work like this: While travelling down the
4245  * south-bound strip just to the east of the main south axis, as soon as
4246  * we get to a grid which does not "transmit" viewing, if all of the strips
4247  * preceding us (in this case, just the main axis) had terminated at or before
4248  * the same point, then we can stop, and reset the "max distance" to ourself.
4249  * So, each strip (named by major axis plus offset, thus "se" in this case)
4250  * maintains a "blockage" variable, initialized during the main axis step,
4251  * and checks it whenever a blockage is observed.  After processing each
4252  * strip as far as the previous strip told us to process, the next strip is
4253  * told not to go farther than the current strip's farthest viewable grid,
4254  * unless open space is still available.  This uses the "k" variable.
4255  *
4256  * Note the use of "inline" macros for efficiency.  The "cave_los_grid()"
4257  * macro is a replacement for "cave_los_bold()" which takes a pointer to
4258  * a grid instead of its location.  The "cave_view_hack()" macro is a
4259  * chunk of code which adds the given location to the "view" array if it
4260  * is not already there, using both the actual location and a pointer to
4261  * the grid.  See above.
4262  *
4263  * By the way, the purpose of this code is to reduce the dependancy on the
4264  * "los()" function which is slow, and, in some cases, not very accurate.
4265  *
4266  * It is very possible that I am the only person who fully understands this
4267  * function, and for that I am truly sorry, but efficiency was very important
4268  * and the "simple" version of this function was just not fast enough.  I am
4269  * more than willing to replace this function with a simpler one, if it is
4270  * equally efficient, and especially willing if the new function happens to
4271  * derive "reverse-line-of-sight" at the same time, since currently monsters
4272  * just use an optimized hack of "you see me, so I see you", and then use the
4273  * actual "projectable()" function to check spell attacks.
4274  */
4275 void update_view(void)
4276 {
4277         int n, m, d, k, z;
4278         POSITION y, x;
4279
4280         int se, sw, ne, nw, es, en, ws, wn;
4281
4282         int full, over;
4283
4284         POSITION y_max = current_floor_ptr->height - 1;
4285         POSITION x_max = current_floor_ptr->width - 1;
4286
4287         grid_type *g_ptr;
4288
4289         /*** Initialize ***/
4290
4291         /* Optimize */
4292         if (view_reduce_view && !current_floor_ptr->dun_level)
4293         {
4294                 /* Full radius (10) */
4295                 full = MAX_SIGHT / 2;
4296
4297                 /* Octagon factor (15) */
4298                 over = MAX_SIGHT * 3 / 4;
4299         }
4300
4301         /* Normal */
4302         else
4303         {
4304                 /* Full radius (20) */
4305                 full = MAX_SIGHT;
4306
4307                 /* Octagon factor (30) */
4308                 over = MAX_SIGHT * 3 / 2;
4309         }
4310
4311
4312         /*** Step 0 -- Begin ***/
4313
4314         /* Save the old "view" grids for later */
4315         for (n = 0; n < current_floor_ptr->view_n; n++)
4316         {
4317                 y = current_floor_ptr->view_y[n];
4318                 x = current_floor_ptr->view_x[n];
4319                 g_ptr = &current_floor_ptr->grid_array[y][x];
4320
4321                 /* Mark the grid as not in "view" */
4322                 g_ptr->info &= ~(CAVE_VIEW);
4323
4324                 /* Mark the grid as "seen" */
4325                 g_ptr->info |= (CAVE_TEMP);
4326
4327                 /* Add it to the "seen" set */
4328                 tmp_pos.y[tmp_pos.n] = y;
4329                 tmp_pos.x[tmp_pos.n] = x;
4330                 tmp_pos.n++;
4331         }
4332
4333         /* Start over with the "view" array */
4334         current_floor_ptr->view_n = 0;
4335
4336         /*** Step 1 -- adjacent grids ***/
4337
4338         /* Now start on the player */
4339         y = p_ptr->y;
4340         x = p_ptr->x;
4341         g_ptr = &current_floor_ptr->grid_array[y][x];
4342
4343         /* Assume the player grid is easily viewable */
4344         g_ptr->info |= (CAVE_XTRA);
4345
4346         /* Assume the player grid is viewable */
4347         cave_view_hack(g_ptr, y, x);
4348
4349
4350         /*** Step 2 -- Major Diagonals ***/
4351
4352         /* Hack -- Limit */
4353         z = full * 2 / 3;
4354
4355         /* Scan south-east */
4356         for (d = 1; d <= z; d++)
4357         {
4358                 g_ptr = &current_floor_ptr->grid_array[y + d][x + d];
4359                 g_ptr->info |= (CAVE_XTRA);
4360                 cave_view_hack(g_ptr, y + d, x + d);
4361                 if (!cave_los_grid(g_ptr)) break;
4362         }
4363
4364         /* Scan south-west */
4365         for (d = 1; d <= z; d++)
4366         {
4367                 g_ptr = &current_floor_ptr->grid_array[y + d][x - d];
4368                 g_ptr->info |= (CAVE_XTRA);
4369                 cave_view_hack(g_ptr, y + d, x - d);
4370                 if (!cave_los_grid(g_ptr)) break;
4371         }
4372
4373         /* Scan north-east */
4374         for (d = 1; d <= z; d++)
4375         {
4376                 g_ptr = &current_floor_ptr->grid_array[y - d][x + d];
4377                 g_ptr->info |= (CAVE_XTRA);
4378                 cave_view_hack(g_ptr, y - d, x + d);
4379                 if (!cave_los_grid(g_ptr)) break;
4380         }
4381
4382         /* Scan north-west */
4383         for (d = 1; d <= z; d++)
4384         {
4385                 g_ptr = &current_floor_ptr->grid_array[y - d][x - d];
4386                 g_ptr->info |= (CAVE_XTRA);
4387                 cave_view_hack(g_ptr, y - d, x - d);
4388                 if (!cave_los_grid(g_ptr)) break;
4389         }
4390
4391         /*** Step 3 -- major axes ***/
4392
4393         /* Scan south */
4394         for (d = 1; d <= full; d++)
4395         {
4396                 g_ptr = &current_floor_ptr->grid_array[y + d][x];
4397                 g_ptr->info |= (CAVE_XTRA);
4398                 cave_view_hack(g_ptr, y + d, x);
4399                 if (!cave_los_grid(g_ptr)) break;
4400         }
4401
4402         /* Initialize the "south strips" */
4403         se = sw = d;
4404
4405         /* Scan north */
4406         for (d = 1; d <= full; d++)
4407         {
4408                 g_ptr = &current_floor_ptr->grid_array[y - d][x];
4409                 g_ptr->info |= (CAVE_XTRA);
4410                 cave_view_hack(g_ptr, y - d, x);
4411                 if (!cave_los_grid(g_ptr)) break;
4412         }
4413
4414         /* Initialize the "north strips" */
4415         ne = nw = d;
4416
4417         /* Scan east */
4418         for (d = 1; d <= full; d++)
4419         {
4420                 g_ptr = &current_floor_ptr->grid_array[y][x + d];
4421                 g_ptr->info |= (CAVE_XTRA);
4422                 cave_view_hack(g_ptr, y, x + d);
4423                 if (!cave_los_grid(g_ptr)) break;
4424         }
4425
4426         /* Initialize the "east strips" */
4427         es = en = d;
4428
4429         /* Scan west */
4430         for (d = 1; d <= full; d++)
4431         {
4432                 g_ptr = &current_floor_ptr->grid_array[y][x - d];
4433                 g_ptr->info |= (CAVE_XTRA);
4434                 cave_view_hack(g_ptr, y, x - d);
4435                 if (!cave_los_grid(g_ptr)) break;
4436         }
4437
4438         /* Initialize the "west strips" */
4439         ws = wn = d;
4440
4441
4442         /*** Step 4 -- Divide each "octant" into "strips" ***/
4443
4444         /* Now check each "diagonal" (in parallel) */
4445         for (n = 1; n <= over / 2; n++)
4446         {
4447                 POSITION ypn, ymn, xpn, xmn;
4448
4449                 /* Acquire the "bounds" of the maximal circle */
4450                 z = over - n - n;
4451                 if (z > full - n) z = full - n;
4452                 while ((z + n + (n >> 1)) > full) z--;
4453
4454
4455                 /* Access the four diagonal grids */
4456                 ypn = y + n;
4457                 ymn = y - n;
4458                 xpn = x + n;
4459                 xmn = x - n;
4460
4461
4462                 /* South strip */
4463                 if (ypn < y_max)
4464                 {
4465                         /* Maximum distance */
4466                         m = MIN(z, y_max - ypn);
4467
4468                         /* East side */
4469                         if ((xpn <= x_max) && (n < se))
4470                         {
4471                                 /* Scan */
4472                                 for (k = n, d = 1; d <= m; d++)
4473                                 {
4474                                         /* Check grid "d" in strip "n", notice "blockage" */
4475                                         if (update_view_aux(ypn + d, xpn, ypn + d - 1, xpn - 1, ypn + d - 1, xpn))
4476                                         {
4477                                                 if (n + d >= se) break;
4478                                         }
4479
4480                                         /* Track most distant "non-blockage" */
4481                                         else
4482                                         {
4483                                                 k = n + d;
4484                                         }
4485                                 }
4486
4487                                 /* Limit the next strip */
4488                                 se = k + 1;
4489                         }
4490
4491                         /* West side */
4492                         if ((xmn >= 0) && (n < sw))
4493                         {
4494                                 /* Scan */
4495                                 for (k = n, d = 1; d <= m; d++)
4496                                 {
4497                                         /* Check grid "d" in strip "n", notice "blockage" */
4498                                         if (update_view_aux(ypn + d, xmn, ypn + d - 1, xmn + 1, ypn + d - 1, xmn))
4499                                         {
4500                                                 if (n + d >= sw) break;
4501                                         }
4502
4503                                         /* Track most distant "non-blockage" */
4504                                         else
4505                                         {
4506                                                 k = n + d;
4507                                         }
4508                                 }
4509
4510                                 /* Limit the next strip */
4511                                 sw = k + 1;
4512                         }
4513                 }
4514
4515
4516                 /* North strip */
4517                 if (ymn > 0)
4518                 {
4519                         /* Maximum distance */
4520                         m = MIN(z, ymn);
4521
4522                         /* East side */
4523                         if ((xpn <= x_max) && (n < ne))
4524                         {
4525                                 /* Scan */
4526                                 for (k = n, d = 1; d <= m; d++)
4527                                 {
4528                                         /* Check grid "d" in strip "n", notice "blockage" */
4529                                         if (update_view_aux(ymn - d, xpn, ymn - d + 1, xpn - 1, ymn - d + 1, xpn))
4530                                         {
4531                                                 if (n + d >= ne) break;
4532                                         }
4533
4534                                         /* Track most distant "non-blockage" */
4535                                         else
4536                                         {
4537                                                 k = n + d;
4538                                         }
4539                                 }
4540
4541                                 /* Limit the next strip */
4542                                 ne = k + 1;
4543                         }
4544
4545                         /* West side */
4546                         if ((xmn >= 0) && (n < nw))
4547                         {
4548                                 /* Scan */
4549                                 for (k = n, d = 1; d <= m; d++)
4550                                 {
4551                                         /* Check grid "d" in strip "n", notice "blockage" */
4552                                         if (update_view_aux(ymn - d, xmn, ymn - d + 1, xmn + 1, ymn - d + 1, xmn))
4553                                         {
4554                                                 if (n + d >= nw) break;
4555                                         }
4556
4557                                         /* Track most distant "non-blockage" */
4558                                         else
4559                                         {
4560                                                 k = n + d;
4561                                         }
4562                                 }
4563
4564                                 /* Limit the next strip */
4565                                 nw = k + 1;
4566                         }
4567                 }
4568
4569
4570                 /* East strip */
4571                 if (xpn < x_max)
4572                 {
4573                         /* Maximum distance */
4574                         m = MIN(z, x_max - xpn);
4575
4576                         /* South side */
4577                         if ((ypn <= x_max) && (n < es))
4578                         {
4579                                 /* Scan */
4580                                 for (k = n, d = 1; d <= m; d++)
4581                                 {
4582                                         /* Check grid "d" in strip "n", notice "blockage" */
4583                                         if (update_view_aux(ypn, xpn + d, ypn - 1, xpn + d - 1, ypn, xpn + d - 1))
4584                                         {
4585                                                 if (n + d >= es) break;
4586                                         }
4587
4588                                         /* Track most distant "non-blockage" */
4589                                         else
4590                                         {
4591                                                 k = n + d;
4592                                         }
4593                                 }
4594
4595                                 /* Limit the next strip */
4596                                 es = k + 1;
4597                         }
4598
4599                         /* North side */
4600                         if ((ymn >= 0) && (n < en))
4601                         {
4602                                 /* Scan */
4603                                 for (k = n, d = 1; d <= m; d++)
4604                                 {
4605                                         /* Check grid "d" in strip "n", notice "blockage" */
4606                                         if (update_view_aux(ymn, xpn + d, ymn + 1, xpn + d - 1, ymn, xpn + d - 1))
4607                                         {
4608                                                 if (n + d >= en) break;
4609                                         }
4610
4611                                         /* Track most distant "non-blockage" */
4612                                         else
4613                                         {
4614                                                 k = n + d;
4615                                         }
4616                                 }
4617
4618                                 /* Limit the next strip */
4619                                 en = k + 1;
4620                         }
4621                 }
4622
4623
4624                 /* West strip */
4625                 if (xmn > 0)
4626                 {
4627                         /* Maximum distance */
4628                         m = MIN(z, xmn);
4629
4630                         /* South side */
4631                         if ((ypn <= y_max) && (n < ws))
4632                         {
4633                                 /* Scan */
4634                                 for (k = n, d = 1; d <= m; d++)
4635                                 {
4636                                         /* Check grid "d" in strip "n", notice "blockage" */
4637                                         if (update_view_aux(ypn, xmn - d, ypn - 1, xmn - d + 1, ypn, xmn - d + 1))
4638                                         {
4639                                                 if (n + d >= ws) break;
4640                                         }
4641
4642                                         /* Track most distant "non-blockage" */
4643                                         else
4644                                         {
4645                                                 k = n + d;
4646                                         }
4647                                 }
4648
4649                                 /* Limit the next strip */
4650                                 ws = k + 1;
4651                         }
4652
4653                         /* North side */
4654                         if ((ymn >= 0) && (n < wn))
4655                         {
4656                                 /* Scan */
4657                                 for (k = n, d = 1; d <= m; d++)
4658                                 {
4659                                         /* Check grid "d" in strip "n", notice "blockage" */
4660                                         if (update_view_aux(ymn, xmn - d, ymn + 1, xmn - d + 1, ymn, xmn - d + 1))
4661                                         {
4662                                                 if (n + d >= wn) break;
4663                                         }
4664
4665                                         /* Track most distant "non-blockage" */
4666                                         else
4667                                         {
4668                                                 k = n + d;
4669                                         }
4670                                 }
4671
4672                                 /* Limit the next strip */
4673                                 wn = k + 1;
4674                         }
4675                 }
4676         }
4677
4678
4679         /*** Step 5 -- Complete the algorithm ***/
4680
4681         /* Update all the new grids */
4682         for (n = 0; n < current_floor_ptr->view_n; n++)
4683         {
4684                 y = current_floor_ptr->view_y[n];
4685                 x = current_floor_ptr->view_x[n];
4686                 g_ptr = &current_floor_ptr->grid_array[y][x];
4687
4688                 /* Clear the "CAVE_XTRA" flag */
4689                 g_ptr->info &= ~(CAVE_XTRA);
4690
4691                 /* Update only newly viewed grids */
4692                 if (g_ptr->info & (CAVE_TEMP)) continue;
4693
4694                 /* Add it to later visual update */
4695                 cave_note_and_redraw_later(g_ptr, y, x);
4696         }
4697
4698         /* Wipe the old grids, update as needed */
4699         for (n = 0; n < tmp_pos.n; n++)
4700         {
4701                 y = tmp_pos.y[n];
4702                 x = tmp_pos.x[n];
4703                 g_ptr = &current_floor_ptr->grid_array[y][x];
4704
4705                 /* No longer in the array */
4706                 g_ptr->info &= ~(CAVE_TEMP);
4707
4708                 /* Update only non-viewable grids */
4709                 if (g_ptr->info & (CAVE_VIEW)) continue;
4710
4711                 /* Add it to later visual update */
4712                 cave_redraw_later(g_ptr, y, x);
4713         }
4714
4715         /* None left */
4716         tmp_pos.n = 0;
4717
4718         /* Mega-Hack -- Visual update later */
4719         p_ptr->update |= (PU_DELAY_VIS);
4720 }
4721
4722
4723 /*
4724  * Mega-Hack -- Delayed visual update
4725  * Only used if update_view(), update_lite() or update_mon_lite() was called
4726  */
4727 void delayed_visual_update(void)
4728 {
4729         int i;
4730         POSITION y, x;
4731         grid_type *g_ptr;
4732
4733         /* Update needed grids */
4734         for (i = 0; i < current_floor_ptr->redraw_n; i++)
4735         {
4736                 y = current_floor_ptr->redraw_y[i];
4737                 x = current_floor_ptr->redraw_x[i];
4738                 g_ptr = &current_floor_ptr->grid_array[y][x];
4739
4740                 /* Update only needed grids (prevent multiple updating) */
4741                 if (!(g_ptr->info & CAVE_REDRAW)) continue;
4742
4743                 /* If required, note */
4744                 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
4745
4746                 lite_spot(y, x);
4747
4748                 /* Hack -- Visual update of monster on this grid */
4749                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
4750
4751                 /* No longer in the array */
4752                 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
4753         }
4754
4755         /* None left */
4756         current_floor_ptr->redraw_n = 0;
4757 }
4758
4759
4760 /*
4761  * Hack -- forget the "flow" information
4762  */
4763 void forget_flow(void)
4764 {
4765         POSITION x, y;
4766
4767         /* Check the entire dungeon */
4768         for (y = 0; y < current_floor_ptr->height; y++)
4769         {
4770                 for (x = 0; x < current_floor_ptr->width; x++)
4771                 {
4772                         /* Forget the old data */
4773                         current_floor_ptr->grid_array[y][x].dist = 0;
4774                         current_floor_ptr->grid_array[y][x].cost = 0;
4775                         current_floor_ptr->grid_array[y][x].when = 0;
4776                 }
4777         }
4778 }
4779
4780
4781 /*
4782  * Hack - speed up the update_flow algorithm by only doing
4783  * it everytime the player moves out of LOS of the last
4784  * "way-point".
4785  */
4786 static POSITION flow_x = 0;
4787 static POSITION flow_y = 0;
4788
4789
4790
4791 /*
4792  * Hack -- fill in the "cost" field of every grid that the player
4793  * can "reach" with the number of steps needed to reach that grid.
4794  * This also yields the "distance" of the player from every grid.
4795  *
4796  * In addition, mark the "when" of the grids that can reach
4797  * the player with the incremented value of "flow_n".
4798  *
4799  * Hack -- use the "seen" array as a "circular queue".
4800  *
4801  * We do not need a priority queue because the cost from grid
4802  * to grid is always "one" and we process them in order.
4803  */
4804 void update_flow(void)
4805 {
4806         POSITION x, y;
4807         DIRECTION d;
4808         int flow_head = 1;
4809         int flow_tail = 0;
4810
4811         /* Paranoia -- make sure the array is empty */
4812         if (tmp_pos.n) return;
4813
4814         /* The last way-point is on the map */
4815         if (running && in_bounds(flow_y, flow_x))
4816         {
4817                 /* The way point is in sight - do not update.  (Speedup) */
4818                 if (current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
4819         }
4820
4821         /* Erase all of the current flow information */
4822         for (y = 0; y < current_floor_ptr->height; y++)
4823         {
4824                 for (x = 0; x < current_floor_ptr->width; x++)
4825                 {
4826                         current_floor_ptr->grid_array[y][x].cost = 0;
4827                         current_floor_ptr->grid_array[y][x].dist = 0;
4828                 }
4829         }
4830
4831         /* Save player position */
4832         flow_y = p_ptr->y;
4833         flow_x = p_ptr->x;
4834
4835         /* Add the player's grid to the queue */
4836         tmp_pos.y[0] = p_ptr->y;
4837         tmp_pos.x[0] = p_ptr->x;
4838
4839         /* Now process the queue */
4840         while (flow_head != flow_tail)
4841         {
4842                 int ty, tx;
4843
4844                 /* Extract the next entry */
4845                 ty = tmp_pos.y[flow_tail];
4846                 tx = tmp_pos.x[flow_tail];
4847
4848                 /* Forget that entry */
4849                 if (++flow_tail == TEMP_MAX) flow_tail = 0;
4850
4851                 /* Add the "children" */
4852                 for (d = 0; d < 8; d++)
4853                 {
4854                         int old_head = flow_head;
4855                         byte_hack m = current_floor_ptr->grid_array[ty][tx].cost + 1;
4856                         byte_hack n = current_floor_ptr->grid_array[ty][tx].dist + 1;
4857                         grid_type *g_ptr;
4858
4859                         /* Child location */
4860                         y = ty + ddy_ddd[d];
4861                         x = tx + ddx_ddd[d];
4862
4863                         /* Ignore player's grid */
4864                         if (player_bold(y, x)) continue;
4865
4866                         g_ptr = &current_floor_ptr->grid_array[y][x];
4867
4868                         if (is_closed_door(g_ptr->feat)) m += 3;
4869
4870                         /* Ignore "pre-stamped" entries */
4871                         if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
4872
4873                         /* Ignore "walls" and "rubble" */
4874                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
4875
4876                         /* Save the flow cost */
4877                         if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
4878                         if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
4879
4880                         /* Hack -- limit flow depth */
4881                         if (n == MONSTER_FLOW_DEPTH) continue;
4882
4883                         /* Enqueue that entry */
4884                         tmp_pos.y[flow_head] = y;
4885                         tmp_pos.x[flow_head] = x;
4886
4887                         /* Advance the queue */
4888                         if (++flow_head == TEMP_MAX) flow_head = 0;
4889
4890                         /* Hack -- notice overflow by forgetting new entry */
4891                         if (flow_head == flow_tail) flow_head = old_head;
4892                 }
4893         }
4894 }
4895
4896
4897 static int scent_when = 0;
4898
4899 /*
4900  * Characters leave scent trails for perceptive monsters to track.
4901  *
4902  * Smell is rather more limited than sound.  Many creatures cannot use
4903  * it at all, it doesn't extend very far outwards from the character's
4904  * current position, and monsters can use it to home in the character,
4905  * but not to run away from him.
4906  *
4907  * Smell is valued according to age.  When a character takes his current_world_ptr->game_turn,
4908  * scent is aged by one, and new scent of the current age is laid down.
4909  * Speedy characters leave more scent, true, but it also ages faster,
4910  * which makes it harder to hunt them down.
4911  *
4912  * Whenever the age count loops, most of the scent trail is erased and
4913  * the age of the remainder is recalculated.
4914  */
4915 void update_smell(void)
4916 {
4917         POSITION i, j;
4918         POSITION y, x;
4919
4920         /* Create a table that controls the spread of scent */
4921         const int scent_adjust[5][5] =
4922         {
4923                 { -1, 0, 0, 0,-1 },
4924                 {  0, 1, 1, 1, 0 },
4925                 {  0, 1, 2, 1, 0 },
4926                 {  0, 1, 1, 1, 0 },
4927                 { -1, 0, 0, 0,-1 },
4928         };
4929
4930         /* Loop the age and adjust scent values when necessary */
4931         if (++scent_when == 254)
4932         {
4933                 /* Scan the entire dungeon */
4934                 for (y = 0; y < current_floor_ptr->height; y++)
4935                 {
4936                         for (x = 0; x < current_floor_ptr->width; x++)
4937                         {
4938                                 int w = current_floor_ptr->grid_array[y][x].when;
4939                                 current_floor_ptr->grid_array[y][x].when = (w > 128) ? (w - 128) : 0;
4940                         }
4941                 }
4942
4943                 /* Restart */
4944                 scent_when = 126;
4945         }
4946
4947
4948         /* Lay down new scent */
4949         for (i = 0; i < 5; i++)
4950         {
4951                 for (j = 0; j < 5; j++)
4952                 {
4953                         grid_type *g_ptr;
4954
4955                         /* Translate table to map grids */
4956                         y = i + p_ptr->y - 2;
4957                         x = j + p_ptr->x - 2;
4958
4959                         /* Check Bounds */
4960                         if (!in_bounds(y, x)) continue;
4961
4962                         g_ptr = &current_floor_ptr->grid_array[y][x];
4963
4964                         /* Walls, water, and lava cannot hold scent. */
4965                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
4966
4967                         /* Grid must not be blocked by walls from the character */
4968                         if (!player_has_los_bold(y, x)) continue;
4969
4970                         /* Note grids that are too far away */
4971                         if (scent_adjust[i][j] == -1) continue;
4972
4973                         /* Mark the grid with new scent */
4974                         g_ptr->when = scent_when + scent_adjust[i][j];
4975                 }
4976         }
4977 }
4978
4979
4980 /*
4981  * Hack -- map the current panel (plus some) ala "magic mapping"
4982  */
4983 void map_area(POSITION range)
4984 {
4985         int i;
4986         POSITION x, y;
4987         grid_type *g_ptr;
4988         FEAT_IDX feat;
4989         feature_type *f_ptr;
4990
4991         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) range /= 3;
4992
4993         /* Scan that area */
4994         for (y = 1; y < current_floor_ptr->height - 1; y++)
4995         {
4996                 for (x = 1; x < current_floor_ptr->width - 1; x++)
4997                 {
4998                         if (distance(p_ptr->y, p_ptr->x, y, x) > range) continue;
4999
5000                         g_ptr = &current_floor_ptr->grid_array[y][x];
5001
5002                         /* Memorize terrain of the grid */
5003                         g_ptr->info |= (CAVE_KNOWN);
5004
5005                         /* Feature code (applying "mimic" field) */
5006                         feat = get_feat_mimic(g_ptr);
5007                         f_ptr = &f_info[feat];
5008
5009                         /* All non-walls are "checked" */
5010                         if (!have_flag(f_ptr->flags, FF_WALL))
5011                         {
5012                                 /* Memorize normal features */
5013                                 if (have_flag(f_ptr->flags, FF_REMEMBER))
5014                                 {
5015                                         /* Memorize the object */
5016                                         g_ptr->info |= (CAVE_MARK);
5017                                 }
5018
5019                                 /* Memorize known walls */
5020                                 for (i = 0; i < 8; i++)
5021                                 {
5022                                         g_ptr = &current_floor_ptr->grid_array[y + ddy_ddd[i]][x + ddx_ddd[i]];
5023
5024                                         /* Feature code (applying "mimic" field) */
5025                                         feat = get_feat_mimic(g_ptr);
5026                                         f_ptr = &f_info[feat];
5027
5028                                         /* Memorize walls (etc) */
5029                                         if (have_flag(f_ptr->flags, FF_REMEMBER))
5030                                         {
5031                                                 /* Memorize the walls */
5032                                                 g_ptr->info |= (CAVE_MARK);
5033                                         }
5034                                 }
5035                         }
5036                 }
5037         }
5038
5039         p_ptr->redraw |= (PR_MAP);
5040
5041         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
5042 }
5043
5044
5045 /*
5046  * Change the "feat" flag for a grid, and notice/redraw the grid
5047  */
5048 void cave_set_feat(POSITION y, POSITION x, FEAT_IDX feat)
5049 {
5050         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
5051         feature_type *f_ptr = &f_info[feat];
5052         bool old_los, old_mirror;
5053
5054         if (!character_dungeon)
5055         {
5056                 /* Clear mimic type */
5057                 g_ptr->mimic = 0;
5058
5059                 /* Change the feature */
5060                 g_ptr->feat = feat;
5061
5062                 /* Hack -- glow the GLOW terrain */
5063                 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
5064                 {
5065                         DIRECTION i;
5066                         POSITION yy, xx;
5067
5068                         for (i = 0; i < 9; i++)
5069                         {
5070                                 yy = y + ddy_ddd[i];
5071                                 xx = x + ddx_ddd[i];
5072                                 if (!in_bounds2(yy, xx)) continue;
5073                                 current_floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
5074                         }
5075                 }
5076
5077                 return;
5078         }
5079
5080         old_los = cave_have_flag_bold(y, x, FF_LOS);
5081         old_mirror = is_mirror_grid(g_ptr);
5082
5083         /* Clear mimic type */
5084         g_ptr->mimic = 0;
5085
5086         /* Change the feature */
5087         g_ptr->feat = feat;
5088
5089         /* Remove flag for mirror/glyph */
5090         g_ptr->info &= ~(CAVE_OBJECT);
5091
5092         if (old_mirror && (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
5093         {
5094                 g_ptr->info &= ~(CAVE_GLOW);
5095                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
5096
5097                 update_local_illumination(y, x);
5098         }
5099
5100         /* Check for change to boring grid */
5101         if (!have_flag(f_ptr->flags, FF_REMEMBER)) g_ptr->info &= ~(CAVE_MARK);
5102         if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
5103
5104         note_spot(y, x);
5105
5106         lite_spot(y, x);
5107
5108         /* Check if los has changed */
5109         if (old_los ^ have_flag(f_ptr->flags, FF_LOS))
5110         {
5111
5112 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
5113
5114                 update_local_illumination(y, x);
5115
5116 #endif /* COMPLEX_WALL_ILLUMINATION */
5117
5118                 /* Update the visuals */
5119                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE | PU_MONSTERS);
5120         }
5121
5122         /* Hack -- glow the GLOW terrain */
5123         if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
5124         {
5125                 DIRECTION i;
5126                 POSITION yy, xx;
5127                 grid_type *cc_ptr;
5128
5129                 for (i = 0; i < 9; i++)
5130                 {
5131                         yy = y + ddy_ddd[i];
5132                         xx = x + ddx_ddd[i];
5133                         if (!in_bounds2(yy, xx)) continue;
5134                         cc_ptr = &current_floor_ptr->grid_array[yy][xx];
5135                         cc_ptr->info |= CAVE_GLOW;
5136
5137                         if (player_has_los_grid(cc_ptr))
5138                         {
5139                                 if (cc_ptr->m_idx) update_monster(cc_ptr->m_idx, FALSE);
5140
5141                                 note_spot(yy, xx);
5142
5143                                 lite_spot(yy, xx);
5144                         }
5145
5146                         update_local_illumination(yy, xx);
5147                 }
5148
5149                 if (p_ptr->special_defense & NINJA_S_STEALTH)
5150                 {
5151                         if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(FALSE);
5152                 }
5153         }
5154 }
5155
5156
5157 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
5158 {
5159         feature_type *f_ptr = &f_info[newfeat];
5160
5161         if (have_flag(f_ptr->flags, FF_CONVERT))
5162         {
5163                 switch (f_ptr->subtype)
5164                 {
5165                 case CONVERT_TYPE_FLOOR:
5166                         return feat_ground_type[randint0(100)];
5167                 case CONVERT_TYPE_WALL:
5168                         return feat_wall_type[randint0(100)];
5169                 case CONVERT_TYPE_INNER:
5170                         return feat_wall_inner;
5171                 case CONVERT_TYPE_OUTER:
5172                         return feat_wall_outer;
5173                 case CONVERT_TYPE_SOLID:
5174                         return feat_wall_solid;
5175                 case CONVERT_TYPE_STREAM1:
5176                         return d_info[p_ptr->dungeon_idx].stream1;
5177                 case CONVERT_TYPE_STREAM2:
5178                         return d_info[p_ptr->dungeon_idx].stream2;
5179                 default:
5180                         return newfeat;
5181                 }
5182         }
5183         else return newfeat;
5184 }
5185
5186
5187 /*
5188  * Take a feature, determine what that feature becomes
5189  * through applying the given action.
5190  */
5191 FEAT_IDX feat_state(FEAT_IDX feat, int action)
5192 {
5193         feature_type *f_ptr = &f_info[feat];
5194         int i;
5195
5196         /* Get the new feature */
5197         for (i = 0; i < MAX_FEAT_STATES; i++)
5198         {
5199                 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
5200         }
5201
5202         if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
5203
5204         return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
5205 }
5206
5207 /*
5208  * Takes a location and action and changes the feature at that
5209  * location through applying the given action.
5210  */
5211 void cave_alter_feat(POSITION y, POSITION x, int action)
5212 {
5213         /* Set old feature */
5214         FEAT_IDX oldfeat = current_floor_ptr->grid_array[y][x].feat;
5215
5216         /* Get the new feat */
5217         FEAT_IDX newfeat = feat_state(oldfeat, action);
5218
5219         /* No change */
5220         if (newfeat == oldfeat) return;
5221
5222         /* Set the new feature */
5223         cave_set_feat(y, x, newfeat);
5224
5225         if (!(feature_action_flags[action] & FAF_NO_DROP))
5226         {
5227                 feature_type *old_f_ptr = &f_info[oldfeat];
5228                 feature_type *f_ptr = &f_info[newfeat];
5229                 bool found = FALSE;
5230
5231                 /* Handle gold */
5232                 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
5233                 {
5234                         /* Place some gold */
5235                         place_gold(y, x);
5236                         found = TRUE;
5237                 }
5238
5239                 /* Handle item */
5240                 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)))
5241                 {
5242                         /* Place object */
5243                         place_object(y, x, 0L);
5244                         found = TRUE;
5245                 }
5246
5247                 if (found && character_dungeon && player_can_see_bold(y, x))
5248                 {
5249                         msg_print(_("何かを発見した!", "You have found something!"));
5250                 }
5251         }
5252
5253         if (feature_action_flags[action] & FAF_CRASH_GLASS)
5254         {
5255                 feature_type *old_f_ptr = &f_info[oldfeat];
5256
5257                 if (have_flag(old_f_ptr->flags, FF_GLASS) && character_dungeon)
5258                 {
5259                         project(PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
5260                                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
5261                 }
5262         }
5263 }
5264
5265
5266 /* Remove a mirror */
5267 void remove_mirror(POSITION y, POSITION x)
5268 {
5269         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
5270
5271         /* Remove the mirror */
5272         g_ptr->info &= ~(CAVE_OBJECT);
5273         g_ptr->mimic = 0;
5274
5275         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
5276         {
5277                 g_ptr->info &= ~(CAVE_GLOW);
5278                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
5279                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
5280
5281                 update_local_illumination(y, x);
5282         }
5283
5284         note_spot(y, x);
5285
5286         lite_spot(y, x);
5287 }
5288
5289
5290 /*
5291  *  Return TRUE if there is a mirror on the grid.
5292  */
5293 bool is_mirror_grid(grid_type *g_ptr)
5294 {
5295         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
5296                 return TRUE;
5297         else
5298                 return FALSE;
5299 }
5300
5301
5302 /*
5303  *  Return TRUE if there is a mirror on the grid.
5304  */
5305 bool is_glyph_grid(grid_type *g_ptr)
5306 {
5307         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
5308                 return TRUE;
5309         else
5310                 return FALSE;
5311 }
5312
5313
5314 /*
5315  *  Return TRUE if there is a mirror on the grid.
5316  */
5317 bool is_explosive_rune_grid(grid_type *g_ptr)
5318 {
5319         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
5320                 return TRUE;
5321         else
5322                 return FALSE;
5323 }
5324
5325
5326 /*
5327  * Calculate "incremental motion". Used by project() and shoot().
5328  * Assumes that (*y,*x) lies on the path from (y1,x1) to (y2,x2).
5329  */
5330 void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
5331 {
5332         POSITION dy, dx, dist, shift;
5333
5334         /* Extract the distance travelled */
5335         dy = (*y < y1) ? y1 - *y : *y - y1;
5336         dx = (*x < x1) ? x1 - *x : *x - x1;
5337
5338         /* Number of steps */
5339         dist = (dy > dx) ? dy : dx;
5340
5341         /* We are calculating the next location */
5342         dist++;
5343
5344
5345         /* Calculate the total distance along each axis */
5346         dy = (y2 < y1) ? (y1 - y2) : (y2 - y1);
5347         dx = (x2 < x1) ? (x1 - x2) : (x2 - x1);
5348
5349         /* Paranoia -- Hack -- no motion */
5350         if (!dy && !dx) return;
5351
5352
5353         /* Move mostly vertically */
5354         if (dy > dx)
5355         {
5356                 /* Extract a shift factor */
5357                 shift = (dist * dx + (dy - 1) / 2) / dy;
5358
5359                 /* Sometimes move along the minor axis */
5360                 (*x) = (x2 < x1) ? (x1 - shift) : (x1 + shift);
5361
5362                 /* Always move along major axis */
5363                 (*y) = (y2 < y1) ? (y1 - dist) : (y1 + dist);
5364         }
5365
5366         /* Move mostly horizontally */
5367         else
5368         {
5369                 /* Extract a shift factor */
5370                 shift = (dist * dy + (dx - 1) / 2) / dx;
5371
5372                 /* Sometimes move along the minor axis */
5373                 (*y) = (y2 < y1) ? (y1 - shift) : (y1 + shift);
5374
5375                 /* Always move along major axis */
5376                 (*x) = (x2 < x1) ? (x1 - dist) : (x1 + dist);
5377         }
5378 }
5379
5380
5381
5382 /*
5383  * Determine if a bolt spell cast from (y1,x1) to (y2,x2) will arrive
5384  * at the final destination, assuming no monster gets in the way.
5385  *
5386  * This is slightly (but significantly) different from "los(y1,x1,y2,x2)".
5387  */
5388 bool projectable(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
5389 {
5390         POSITION y, x;
5391
5392         int grid_n = 0;
5393         u16b grid_g[512];
5394
5395         /* Check the projection path */
5396         grid_n = project_path(grid_g, (project_length ? project_length : MAX_RANGE), y1, x1, y2, x2, 0);
5397
5398         /* Identical grid */
5399         if (!grid_n) return TRUE;
5400
5401         /* Final grid */
5402         y = GRID_Y(grid_g[grid_n - 1]);
5403         x = GRID_X(grid_g[grid_n - 1]);
5404
5405         /* May not end in an unrequested grid */
5406         if ((y != y2) || (x != x2)) return (FALSE);
5407
5408         /* Assume okay */
5409         return (TRUE);
5410 }
5411
5412
5413 /*
5414  * Standard "find me a location" function
5415  *
5416  * Obtains a legal location within the given distance of the initial
5417  * location, and with "los()" from the source to destination location.
5418  *
5419  * This function is often called from inside a loop which searches for
5420  * locations while increasing the "d" distance.
5421  *
5422  * Currently the "m" parameter is unused.
5423  */
5424 void scatter(POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION d, BIT_FLAGS mode)
5425 {
5426         POSITION nx, ny;
5427
5428         /* Pick a location */
5429         while (TRUE)
5430         {
5431                 /* Pick a new location */
5432                 ny = rand_spread(y, d);
5433                 nx = rand_spread(x, d);
5434
5435                 /* Ignore annoying locations */
5436                 if (!in_bounds(ny, nx)) continue;
5437
5438                 /* Ignore "excessively distant" locations */
5439                 if ((d > 1) && (distance(y, x, ny, nx) > d)) continue;
5440
5441                 if (mode & PROJECT_LOS)
5442                 {
5443                         if (los(y, x, ny, nx)) break;
5444                 }
5445                 else
5446                 {
5447                         if (projectable(y, x, ny, nx)) break;
5448                 }
5449
5450         }
5451
5452         /* Save the location */
5453         (*yp) = ny;
5454         (*xp) = nx;
5455 }
5456
5457
5458
5459
5460 /*
5461  * Track a new monster
5462  */
5463 void health_track(MONSTER_IDX m_idx)
5464 {
5465         /* Mount monster is already tracked */
5466         if (m_idx && m_idx == p_ptr->riding) return;
5467
5468         /* Track a new guy */
5469         p_ptr->health_who = m_idx;
5470
5471         /* Redraw (later) */
5472         p_ptr->redraw |= (PR_HEALTH);
5473 }
5474
5475
5476
5477 /*
5478  * Hack -- track the given monster race
5479  */
5480 void monster_race_track(MONRACE_IDX r_idx)
5481 {
5482         /* Save this monster ID */
5483         p_ptr->monster_race_idx = r_idx;
5484
5485         p_ptr->window |= (PW_MONSTER);
5486 }
5487
5488
5489
5490 /*
5491  * Hack -- track the given object kind
5492  */
5493 void object_kind_track(KIND_OBJECT_IDX k_idx)
5494 {
5495         /* Save this monster ID */
5496         p_ptr->object_kind_idx = k_idx;
5497
5498         p_ptr->window |= (PW_OBJECT);
5499 }
5500
5501
5502
5503 /*
5504  * Glow deep lava and building entrances in the floor
5505  */
5506 void glow_deep_lava_and_bldg(void)
5507 {
5508         POSITION y, x, yy, xx;
5509         DIRECTION i;
5510         grid_type *g_ptr;
5511
5512         /* Not in the darkness dungeon */
5513         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) return;
5514
5515         for (y = 0; y < current_floor_ptr->height; y++)
5516         {
5517                 for (x = 0; x < current_floor_ptr->width; x++)
5518                 {
5519                         g_ptr = &current_floor_ptr->grid_array[y][x];
5520
5521                         /* Feature code (applying "mimic" field) */
5522
5523                         if (have_flag(f_info[get_feat_mimic(g_ptr)].flags, FF_GLOW))
5524                         {
5525                                 for (i = 0; i < 9; i++)
5526                                 {
5527                                         yy = y + ddy_ddd[i];
5528                                         xx = x + ddx_ddd[i];
5529                                         if (!in_bounds2(yy, xx)) continue;
5530                                         current_floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
5531                                 }
5532                         }
5533                 }
5534         }
5535
5536         /* Update the view and lite */
5537         p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
5538
5539         p_ptr->redraw |= (PR_MAP);
5540 }
5541
5542 /*!
5543 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
5544 * @param m_idx モンスターID
5545 * @param y 移動先Y座標
5546 * @param x 移動先X座標
5547 * @param mode オプション
5548 * @return テレポート先として妥当ならばtrue
5549 */
5550 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
5551 {
5552         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
5553         grid_type    *g_ptr = &current_floor_ptr->grid_array[y][x];
5554         feature_type *f_ptr = &f_info[g_ptr->feat];
5555
5556         /* Require "teleportable" space */
5557         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
5558
5559         if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
5560         if (player_bold(y, x)) return FALSE;
5561
5562         /* Hack -- no teleport onto glyph of warding */
5563         if (is_glyph_grid(g_ptr)) return FALSE;
5564         if (is_explosive_rune_grid(g_ptr)) return FALSE;
5565
5566         if (!(mode & TELEPORT_PASSIVE))
5567         {
5568                 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
5569         }
5570
5571         return TRUE;
5572 }
5573
5574 /*!
5575 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
5576 * @param y 移動先Y座標
5577 * @param x 移動先X座標
5578 * @param mode オプション
5579 * @return テレポート先として妥当ならばtrue
5580 */
5581 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
5582 {
5583         grid_type    *g_ptr = &current_floor_ptr->grid_array[y][x];
5584         feature_type *f_ptr = &f_info[g_ptr->feat];
5585
5586         /* Require "teleportable" space */
5587         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
5588
5589         /* No magical teleporting into vaults and such */
5590         if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
5591
5592         if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
5593
5594         /* don't teleport on a trap. */
5595         if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
5596
5597         if (!(mode & TELEPORT_PASSIVE))
5598         {
5599                 if (!player_can_enter(g_ptr->feat, 0)) return FALSE;
5600
5601                 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
5602                 {
5603                         if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
5604                 }
5605
5606                 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
5607                 {
5608                         /* Always forbid deep lava */
5609                         if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
5610
5611                         /* Forbid shallow lava when the player don't have levitation */
5612                         if (!p_ptr->levitation) return FALSE;
5613                 }
5614
5615         }
5616
5617         return TRUE;
5618 }
5619
5620 /*!
5621  * @brief 地形は開くものであって、かつ開かれているかを返す /
5622  * Attempt to open the given chest at the given location
5623  * @param feat 地形ID
5624  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
5625  */
5626 bool is_open(FEAT_IDX feat)
5627 {
5628         return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
5629 }
5630