OSDN Git Service

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