OSDN Git Service

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