OSDN Git Service

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