OSDN Git Service

[Refactor] #38997 glow_deep_lava_and_bldg() の引数をplayer_type * に変更 / Changed argument...
[hengband/hengband.git] / src / floor-events.c
1 #include "angband.h"
2 #include "util.h"
3
4 #include "dungeon.h"
5 #include "floor.h"
6 #include "floor-events.h"
7 #include "grid.h"
8 #include "monster.h"
9 #include "monster-status.h"
10 #include "quest.h"
11 #include "object-hook.h"
12 #include "player-move.h"
13 #include "world.h"
14 #include "player-effects.h"
15 #include "objectkind.h"
16 #include "object-ego.h"
17 #include "cmd-dump.h"
18 #include "view-mainwindow.h"
19
20 static bool mon_invis;
21 static POSITION mon_fy, mon_fx;
22
23 void day_break(player_type *subject_ptr)
24 {
25         POSITION y, x;
26         msg_print(_("夜が明けた。", "The sun has risen."));
27
28         floor_type *floor_ptr = subject_ptr->current_floor_ptr;
29         if (!subject_ptr->wild_mode)
30         {
31                 /* Hack -- Scan the town */
32                 for (y = 0; y < floor_ptr->height; y++)
33                 {
34                         for (x = 0; x < floor_ptr->width; x++)
35                         {
36                                 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
37
38                                 /* Assume lit */
39                                 g_ptr->info |= (CAVE_GLOW);
40
41                                 /* Hack -- Memorize lit grids if allowed */
42                                 if (view_perma_grids) g_ptr->info |= (CAVE_MARK);
43
44                                 note_spot(y, x);
45                         }
46                 }
47         }
48
49         subject_ptr->update |= (PU_MONSTERS | PU_MON_LITE);
50         subject_ptr->redraw |= (PR_MAP);
51         subject_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
52
53         if (subject_ptr->special_defense & NINJA_S_STEALTH)
54         {
55                 if (floor_ptr->grid_array[subject_ptr->y][subject_ptr->x].info & CAVE_GLOW) set_superstealth(subject_ptr, FALSE);
56         }
57
58 }
59
60 void night_falls(player_type *subject_ptr)
61 {
62         POSITION y, x;
63         msg_print(_("日が沈んだ。", "The sun has fallen."));
64
65         floor_type *floor_ptr = subject_ptr->current_floor_ptr;
66         if (!subject_ptr->wild_mode)
67         {
68                 /* Hack -- Scan the town */
69                 for (y = 0; y < floor_ptr->height; y++)
70                 {
71                         for (x = 0; x < floor_ptr->width; x++)
72                         {
73                                 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
74
75                                 /* Feature code (applying "mimic" field) */
76                                 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
77
78                                 if (!is_mirror_grid(g_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
79                                         !have_flag(f_ptr->flags, FF_ENTRANCE))
80                                 {
81                                         /* Assume dark */
82                                         g_ptr->info &= ~(CAVE_GLOW);
83
84                                         if (!have_flag(f_ptr->flags, FF_REMEMBER))
85                                         {
86                                                 /* Forget the normal floor grid */
87                                                 g_ptr->info &= ~(CAVE_MARK);
88
89                                                 note_spot(y, x);
90                                         }
91                                 }
92                         }
93
94                         glow_deep_lava_and_bldg(subject_ptr);
95                 }
96         }
97
98         subject_ptr->update |= (PU_MONSTERS | PU_MON_LITE);
99         subject_ptr->redraw |= (PR_MAP);
100         subject_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
101
102         if (subject_ptr->special_defense & NINJA_S_STEALTH)
103         {
104                 if (floor_ptr->grid_array[subject_ptr->y][subject_ptr->x].info & CAVE_GLOW) set_superstealth(subject_ptr, FALSE);
105         }
106
107 }
108
109 /*!
110  * @brief 現在フロアに残っている敵モンスターの数を返す /
111  * @return 現在の敵モンスターの数
112  */
113 MONSTER_NUMBER count_all_hostile_monsters(floor_type *floor_ptr)
114 {
115         POSITION x, y;
116         MONSTER_NUMBER number_mon = 0;
117
118         for (x = 0; x < floor_ptr->width; ++x)
119         {
120                 for (y = 0; y < floor_ptr->height; ++y)
121                 {
122                         MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
123
124                         if (m_idx > 0 && is_hostile(&floor_ptr->m_list[m_idx]))
125                         {
126                                 ++number_mon;
127                         }
128                 }
129         }
130
131         return number_mon;
132 }
133
134
135
136 /*!
137  * ダンジョンの雰囲気を計算するための非線形基準値 / Dungeon rating is no longer linear
138  */
139 #define RATING_BOOST(delta) (delta * delta + 50 * delta)
140
141  /*!
142   * @brief ダンジョンの雰囲気を算出する。
143   * / Examine all monsters and unidentified objects, and get the feeling of current dungeon floor
144   * @return 算出されたダンジョンの雰囲気ランク
145   */
146 byte get_dungeon_feeling(floor_type *floor_ptr)
147 {
148         const int base = 10;
149         int rating = 0;
150         MONSTER_IDX i;
151
152         /* Hack -- no feeling in the town */
153         if (!floor_ptr->dun_level) return 0;
154
155         /* Examine each monster */
156         for (i = 1; i < floor_ptr->m_max; i++)
157         {
158                 monster_type *m_ptr = &floor_ptr->m_list[i];
159                 monster_race *r_ptr;
160                 int delta = 0;
161                 if (!monster_is_valid(m_ptr)) continue;
162
163                 if (is_pet(m_ptr)) continue;
164
165                 r_ptr = &r_info[m_ptr->r_idx];
166
167                 if (r_ptr->flags1 & (RF1_UNIQUE))
168                 {
169                         /* Nearly out-of-depth unique monsters */
170                         if (r_ptr->level + 10 > floor_ptr->dun_level)
171                         {
172                                 /* Boost rating by twice delta-depth */
173                                 delta += (r_ptr->level + 10 - floor_ptr->dun_level) * 2 * base;
174                         }
175                 }
176                 else
177                 {
178                         /* Out-of-depth monsters */
179                         if (r_ptr->level > floor_ptr->dun_level)
180                         {
181                                 /* Boost rating by delta-depth */
182                                 delta += (r_ptr->level - floor_ptr->dun_level) * base;
183                         }
184                 }
185
186                 /* Unusually crowded monsters get a little bit of rating boost */
187                 if (r_ptr->flags1 & RF1_FRIENDS)
188                 {
189                         if (5 <= get_monster_crowd_number(i)) delta += 1;
190                 }
191                 else
192                 {
193                         if (2 <= get_monster_crowd_number(i)) delta += 1;
194                 }
195
196
197                 rating += RATING_BOOST(delta);
198         }
199
200         /* Examine each unidentified object */
201         for (i = 1; i < floor_ptr->o_max; i++)
202         {
203                 object_type *o_ptr = &floor_ptr->o_list[i];
204                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
205                 int delta = 0;
206
207                 if (!OBJECT_IS_VALID(o_ptr)) continue;
208
209                 /* Skip known objects */
210                 if (object_is_known(o_ptr))
211                 {
212                         /* Touched? */
213                         if (o_ptr->marked & OM_TOUCHED) continue;
214                 }
215
216                 /* Skip pseudo-known objects */
217                 if (o_ptr->ident & IDENT_SENSE) continue;
218
219                 /* Ego objects */
220                 if (object_is_ego(o_ptr))
221                 {
222                         ego_item_type *e_ptr = &e_info[o_ptr->name2];
223
224                         delta += e_ptr->rating * base;
225                 }
226
227                 /* Artifacts */
228                 if (object_is_artifact(o_ptr))
229                 {
230                         PRICE cost = object_value_real(o_ptr);
231
232                         delta += 10 * base;
233                         if (cost > 10000L) delta += 10 * base;
234                         if (cost > 50000L) delta += 10 * base;
235                         if (cost > 100000L) delta += 10 * base;
236
237                         /* Special feeling */
238                         if (!preserve_mode) return 1;
239                 }
240
241                 if (o_ptr->tval == TV_DRAG_ARMOR) delta += 30 * base;
242                 if (o_ptr->tval == TV_SHIELD && o_ptr->sval == SV_DRAGON_SHIELD) delta += 5 * base;
243                 if (o_ptr->tval == TV_GLOVES && o_ptr->sval == SV_SET_OF_DRAGON_GLOVES) delta += 5 * base;
244                 if (o_ptr->tval == TV_BOOTS && o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE) delta += 5 * base;
245                 if (o_ptr->tval == TV_HELM && o_ptr->sval == SV_DRAGON_HELM) delta += 5 * base;
246                 if (o_ptr->tval == TV_RING && o_ptr->sval == SV_RING_SPEED && !object_is_cursed(o_ptr)) delta += 25 * base;
247                 if (o_ptr->tval == TV_RING && o_ptr->sval == SV_RING_LORDLY && !object_is_cursed(o_ptr)) delta += 15 * base;
248                 if (o_ptr->tval == TV_AMULET && o_ptr->sval == SV_AMULET_THE_MAGI && !object_is_cursed(o_ptr)) delta += 15 * base;
249
250                 /* Out-of-depth objects */
251                 if (!object_is_cursed(o_ptr) && !object_is_broken(o_ptr) && k_ptr->level > floor_ptr->dun_level)
252                 {
253                         /* Rating increase */
254                         delta += (k_ptr->level - floor_ptr->dun_level) * base;
255                 }
256
257                 rating += RATING_BOOST(delta);
258         }
259
260
261         if (rating > RATING_BOOST(1000)) return 2;
262         if (rating > RATING_BOOST(800)) return 3;
263         if (rating > RATING_BOOST(600)) return 4;
264         if (rating > RATING_BOOST(400)) return 5;
265         if (rating > RATING_BOOST(300)) return 6;
266         if (rating > RATING_BOOST(200)) return 7;
267         if (rating > RATING_BOOST(100)) return 8;
268         if (rating > RATING_BOOST(0)) return 9;
269
270         return 10;
271 }
272
273 /*!
274  * @brief ダンジョンの雰囲気を更新し、変化があった場合メッセージを表示する
275  * / Update dungeon feeling, and announce it if changed
276  * @return なし
277  */
278 void update_dungeon_feeling(player_type *subject_ptr, floor_type *floor_ptr)
279 {
280         byte new_feeling;
281         int quest_num;
282         int delay;
283
284         /* No feeling on the surface */
285         if (!floor_ptr->dun_level) return;
286
287         /* No feeling in the arena */
288         if (subject_ptr->phase_out) return;
289
290         /* Extract delay time */
291         delay = MAX(10, 150 - subject_ptr->skill_fos) * (150 - floor_ptr->dun_level) * TURNS_PER_TICK / 100;
292
293         /* Not yet felt anything */
294         if (current_world_ptr->game_turn < subject_ptr->feeling_turn + delay && !cheat_xtra) return;
295
296         /* Extract quest number (if any) */
297         quest_num = quest_number(subject_ptr, floor_ptr->dun_level);
298
299         /* No feeling in a quest */
300         if (quest_num &&
301                 (is_fixed_quest_idx(quest_num) &&
302                         !((quest_num == QUEST_OBERON) || (quest_num == QUEST_SERPENT) ||
303                                 !(quest[quest_num].flags & QUEST_FLAG_PRESET)))) return;
304
305
306         /* Get new dungeon feeling */
307         new_feeling = get_dungeon_feeling(floor_ptr);
308
309         /* Remember last time updated */
310         subject_ptr->feeling_turn = current_world_ptr->game_turn;
311
312         /* No change */
313         if (subject_ptr->feeling == new_feeling) return;
314
315         /* Dungeon feeling is changed */
316         subject_ptr->feeling = new_feeling;
317
318         /* Announce feeling */
319         do_cmd_feeling(subject_ptr);
320
321         select_floor_music(subject_ptr);
322
323         /* Update the level indicator */
324         subject_ptr->redraw |= (PR_DEPTH);
325
326         if (disturb_minor) disturb(subject_ptr, FALSE, FALSE);
327 }
328
329
330 /*
331  * Glow deep lava and building entrances in the floor
332  */
333 void glow_deep_lava_and_bldg(player_type *subject_ptr)
334 {
335         POSITION y, x, yy, xx;
336         DIRECTION i;
337         grid_type *g_ptr;
338
339         /* Not in the darkness dungeon */
340         if (d_info[subject_ptr->dungeon_idx].flags1 & DF1_DARKNESS) return;
341
342         floor_type *floor_ptr = subject_ptr->current_floor_ptr;
343         for (y = 0; y < floor_ptr->height; y++)
344         {
345                 for (x = 0; x < floor_ptr->width; x++)
346                 {
347                         g_ptr = &floor_ptr->grid_array[y][x];
348
349                         /* Feature code (applying "mimic" field) */
350
351                         if (have_flag(f_info[get_feat_mimic(g_ptr)].flags, FF_GLOW))
352                         {
353                                 for (i = 0; i < 9; i++)
354                                 {
355                                         yy = y + ddy_ddd[i];
356                                         xx = x + ddx_ddd[i];
357                                         if (!in_bounds2(floor_ptr, yy, xx)) continue;
358                                         floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
359                                 }
360                         }
361                 }
362         }
363
364         subject_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
365         subject_ptr->redraw |= (PR_MAP);
366 }
367
368 /*
369  * Actually erase the entire "lite" array, redrawing every grid
370  */
371 void forget_lite(floor_type *floor_ptr)
372 {
373         int i;
374         POSITION x, y;
375
376         /* None to forget */
377         if (!floor_ptr->lite_n) return;
378
379         /* Clear them all */
380         for (i = 0; i < floor_ptr->lite_n; i++)
381         {
382                 y = floor_ptr->lite_y[i];
383                 x = floor_ptr->lite_x[i];
384
385                 /* Forget "LITE" flag */
386                 floor_ptr->grid_array[y][x].info &= ~(CAVE_LITE);
387
388                 /* lite_spot(y, x); Perhaps don't need? */
389         }
390
391         /* None left */
392         floor_ptr->lite_n = 0;
393 }
394
395
396
397 /*
398  * Update the set of grids "illuminated" by the player's lite.
399  *
400  * This routine needs to use the results of "update_view()"
401  *
402  * Note that "blindness" does NOT affect "torch lite".  Be careful!
403  *
404  * We optimize most lites (all non-artifact lites) by using "obvious"
405  * facts about the results of "small" lite radius, and we attempt to
406  * list the "nearby" grids before the more "distant" ones in the
407  * array of torch-lit grids.
408  *
409  * We assume that "radius zero" lite is in fact no lite at all.
410  *
411  *     Torch     Lantern     Artifacts
412  *     (etc)
413  *                              ***
414  *                 ***         *****
415  *      ***       *****       *******
416  *      *@*       **@**       ***@***
417  *      ***       *****       *******
418  *                 ***         *****
419  *                              ***
420  */
421 void update_lite(player_type *subject_ptr, floor_type *floor_ptr)
422 {
423         int i;
424         POSITION x, y, min_x, max_x, min_y, max_y;
425         POSITION p = subject_ptr->cur_lite;
426         grid_type *g_ptr;
427
428         /*** Special case ***/
429
430 #if 0
431         /* Hack -- Player has no lite */
432         if (p <= 0)
433         {
434                 /* Forget the old lite */
435                 /* forget_lite(); Perhaps don't need? */
436
437                 /* Add it to later visual update */
438                 cave_redraw_later(floor_ptr, &floor_ptr->grid_array[subject_ptr->y][subject_ptr->x], subject_ptr->y, subject_ptr->x);
439         }
440 #endif
441
442         /*** Save the old "lite" grids for later ***/
443
444         /* Clear them all */
445         for (i = 0; i < floor_ptr->lite_n; i++)
446         {
447                 y = floor_ptr->lite_y[i];
448                 x = floor_ptr->lite_x[i];
449
450                 /* Mark the grid as not "lite" */
451                 floor_ptr->grid_array[y][x].info &= ~(CAVE_LITE);
452
453                 /* Mark the grid as "seen" */
454                 floor_ptr->grid_array[y][x].info |= (CAVE_TEMP);
455
456                 /* Add it to the "seen" set */
457                 tmp_pos.y[tmp_pos.n] = y;
458                 tmp_pos.x[tmp_pos.n] = x;
459                 tmp_pos.n++;
460         }
461
462         /* None left */
463         floor_ptr->lite_n = 0;
464
465
466         /*** Collect the new "lite" grids ***/
467
468         /* Radius 1 -- torch radius */
469         if (p >= 1)
470         {
471                 /* Player grid */
472                 cave_lite_hack(floor_ptr, subject_ptr->y, subject_ptr->x);
473
474                 /* Adjacent grid */
475                 cave_lite_hack(floor_ptr, subject_ptr->y + 1, subject_ptr->x);
476                 cave_lite_hack(floor_ptr, subject_ptr->y - 1, subject_ptr->x);
477                 cave_lite_hack(floor_ptr, subject_ptr->y, subject_ptr->x + 1);
478                 cave_lite_hack(floor_ptr, subject_ptr->y, subject_ptr->x - 1);
479
480                 /* Diagonal grids */
481                 cave_lite_hack(floor_ptr, subject_ptr->y + 1, subject_ptr->x + 1);
482                 cave_lite_hack(floor_ptr, subject_ptr->y + 1, subject_ptr->x - 1);
483                 cave_lite_hack(floor_ptr, subject_ptr->y - 1, subject_ptr->x + 1);
484                 cave_lite_hack(floor_ptr, subject_ptr->y - 1, subject_ptr->x - 1);
485         }
486
487         /* Radius 2 -- lantern radius */
488         if (p >= 2)
489         {
490                 /* South of the player */
491                 if (cave_los_bold(floor_ptr, subject_ptr->y + 1, subject_ptr->x))
492                 {
493                         cave_lite_hack(floor_ptr, subject_ptr->y + 2, subject_ptr->x);
494                         cave_lite_hack(floor_ptr, subject_ptr->y + 2, subject_ptr->x + 1);
495                         cave_lite_hack(floor_ptr, subject_ptr->y + 2, subject_ptr->x - 1);
496                 }
497
498                 /* North of the player */
499                 if (cave_los_bold(floor_ptr, subject_ptr->y - 1, subject_ptr->x))
500                 {
501                         cave_lite_hack(floor_ptr, subject_ptr->y - 2, subject_ptr->x);
502                         cave_lite_hack(floor_ptr, subject_ptr->y - 2, subject_ptr->x + 1);
503                         cave_lite_hack(floor_ptr, subject_ptr->y - 2, subject_ptr->x - 1);
504                 }
505
506                 /* East of the player */
507                 if (cave_los_bold(floor_ptr, subject_ptr->y, subject_ptr->x + 1))
508                 {
509                         cave_lite_hack(floor_ptr, subject_ptr->y, subject_ptr->x + 2);
510                         cave_lite_hack(floor_ptr, subject_ptr->y + 1, subject_ptr->x + 2);
511                         cave_lite_hack(floor_ptr, subject_ptr->y - 1, subject_ptr->x + 2);
512                 }
513
514                 /* West of the player */
515                 if (cave_los_bold(floor_ptr, subject_ptr->y, subject_ptr->x - 1))
516                 {
517                         cave_lite_hack(floor_ptr, subject_ptr->y, subject_ptr->x - 2);
518                         cave_lite_hack(floor_ptr, subject_ptr->y + 1, subject_ptr->x - 2);
519                         cave_lite_hack(floor_ptr, subject_ptr->y - 1, subject_ptr->x - 2);
520                 }
521         }
522
523         /* Radius 3+ -- artifact radius */
524         if (p >= 3)
525         {
526                 int d;
527
528                 /* Paranoia -- see "LITE_MAX" */
529                 if (p > 14) p = 14;
530
531                 /* South-East of the player */
532                 if (cave_los_bold(floor_ptr, subject_ptr->y + 1, subject_ptr->x + 1))
533                 {
534                         cave_lite_hack(floor_ptr, subject_ptr->y + 2, subject_ptr->x + 2);
535                 }
536
537                 /* South-West of the player */
538                 if (cave_los_bold(floor_ptr, subject_ptr->y + 1, subject_ptr->x - 1))
539                 {
540                         cave_lite_hack(floor_ptr, subject_ptr->y + 2, subject_ptr->x - 2);
541                 }
542
543                 /* North-East of the player */
544                 if (cave_los_bold(floor_ptr, subject_ptr->y - 1, subject_ptr->x + 1))
545                 {
546                         cave_lite_hack(floor_ptr, subject_ptr->y - 2, subject_ptr->x + 2);
547                 }
548
549                 /* North-West of the player */
550                 if (cave_los_bold(floor_ptr, subject_ptr->y - 1, subject_ptr->x - 1))
551                 {
552                         cave_lite_hack(floor_ptr, subject_ptr->y - 2, subject_ptr->x - 2);
553                 }
554
555                 /* Maximal north */
556                 min_y = subject_ptr->y - p;
557                 if (min_y < 0) min_y = 0;
558
559                 /* Maximal south */
560                 max_y = subject_ptr->y + p;
561                 if (max_y > floor_ptr->height - 1) max_y = floor_ptr->height - 1;
562
563                 /* Maximal west */
564                 min_x = subject_ptr->x - p;
565                 if (min_x < 0) min_x = 0;
566
567                 /* Maximal east */
568                 max_x = subject_ptr->x + p;
569                 if (max_x > floor_ptr->width - 1) max_x = floor_ptr->width - 1;
570
571                 /* Scan the maximal box */
572                 for (y = min_y; y <= max_y; y++)
573                 {
574                         for (x = min_x; x <= max_x; x++)
575                         {
576                                 int dy = (subject_ptr->y > y) ? (subject_ptr->y - y) : (y - subject_ptr->y);
577                                 int dx = (subject_ptr->x > x) ? (subject_ptr->x - x) : (x - subject_ptr->x);
578
579                                 /* Skip the "central" grids (above) */
580                                 if ((dy <= 2) && (dx <= 2)) continue;
581
582                                 /* Hack -- approximate the distance */
583                                 d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
584
585                                 /* Skip distant grids */
586                                 if (d > p) continue;
587
588                                 /* Viewable, nearby, grids get "torch lit" */
589                                 if (floor_ptr->grid_array[y][x].info & CAVE_VIEW)
590                                 {
591                                         /* This grid is "torch lit" */
592                                         cave_lite_hack(floor_ptr, y, x);
593                                 }
594                         }
595                 }
596         }
597
598
599         /*** Complete the algorithm ***/
600
601         /* Draw the new grids */
602         for (i = 0; i < floor_ptr->lite_n; i++)
603         {
604                 y = floor_ptr->lite_y[i];
605                 x = floor_ptr->lite_x[i];
606
607                 g_ptr = &floor_ptr->grid_array[y][x];
608
609                 /* Update fresh grids */
610                 if (g_ptr->info & (CAVE_TEMP)) continue;
611
612                 /* Add it to later visual update */
613                 cave_note_and_redraw_later(floor_ptr, g_ptr, y, x);
614         }
615
616         /* Clear them all */
617         for (i = 0; i < tmp_pos.n; i++)
618         {
619                 y = tmp_pos.y[i];
620                 x = tmp_pos.x[i];
621
622                 g_ptr = &floor_ptr->grid_array[y][x];
623
624                 /* No longer in the array */
625                 g_ptr->info &= ~(CAVE_TEMP);
626
627                 /* Update stale grids */
628                 if (g_ptr->info & (CAVE_LITE)) continue;
629
630                 /* Add it to later visual update */
631                 cave_redraw_later(floor_ptr, g_ptr, y, x);
632         }
633
634         /* None left */
635         tmp_pos.n = 0;
636
637         /* Mega-Hack -- Visual update later */
638         subject_ptr->update |= (PU_DELAY_VIS);
639 }
640
641
642 /*
643  * Clear the viewable space
644  */
645 void forget_view(floor_type *floor_ptr)
646 {
647         int i;
648
649         grid_type *g_ptr;
650
651         /* None to forget */
652         if (!floor_ptr->view_n) return;
653
654         /* Clear them all */
655         for (i = 0; i < floor_ptr->view_n; i++)
656         {
657                 POSITION y = floor_ptr->view_y[i];
658                 POSITION x = floor_ptr->view_x[i];
659                 g_ptr = &floor_ptr->grid_array[y][x];
660
661                 /* Forget that the grid is viewable */
662                 g_ptr->info &= ~(CAVE_VIEW);
663
664                 /* if (!panel_contains(y, x)) continue; */
665
666                 /* Update the screen */
667                 /* lite_spot(y, x); Perhaps don't need? */
668         }
669
670         /* None left */
671         floor_ptr->view_n = 0;
672 }
673
674
675 /*
676  * Helper function for "update_view()" below
677  *
678  * We are checking the "viewability" of grid (y,x) by the player.
679  *
680  * This function assumes that (y,x) is legal (i.e. on the map).
681  *
682  * Grid (y1,x1) is on the "diagonal" between (subject_ptr->y,subject_ptr->x) and (y,x)
683  * Grid (y2,x2) is "adjacent", also between (subject_ptr->y,subject_ptr->x) and (y,x).
684  *
685  * Note that we are using the "CAVE_XTRA" field for marking grids as
686  * "easily viewable".  This bit is cleared at the end of "update_view()".
687  *
688  * This function adds (y,x) to the "viewable set" if necessary.
689  *
690  * This function now returns "TRUE" if vision is "blocked" by grid (y,x).
691  */
692 static bool update_view_aux(player_type *subject_ptr, POSITION y, POSITION x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
693 {
694         bool f1, f2, v1, v2, z1, z2, wall;
695
696         grid_type *g_ptr;
697
698         grid_type *g1_c_ptr;
699         grid_type *g2_c_ptr;
700
701         floor_type *floor_ptr = subject_ptr->current_floor_ptr;
702         g1_c_ptr = &floor_ptr->grid_array[y1][x1];
703         g2_c_ptr = &floor_ptr->grid_array[y2][x2];
704
705
706         /* Check for walls */
707         f1 = (cave_los_grid(g1_c_ptr));
708         f2 = (cave_los_grid(g2_c_ptr));
709
710         /* Totally blocked by physical walls */
711         if (!f1 && !f2) return TRUE;
712
713
714         /* Check for visibility */
715         v1 = (f1 && (g1_c_ptr->info & (CAVE_VIEW)));
716         v2 = (f2 && (g2_c_ptr->info & (CAVE_VIEW)));
717
718         /* Totally blocked by "unviewable neighbors" */
719         if (!v1 && !v2) return TRUE;
720
721         g_ptr = &floor_ptr->grid_array[y][x];
722
723
724         /* Check for walls */
725         wall = (!cave_los_grid(g_ptr));
726
727
728         /* Check the "ease" of visibility */
729         z1 = (v1 && (g1_c_ptr->info & (CAVE_XTRA)));
730         z2 = (v2 && (g2_c_ptr->info & (CAVE_XTRA)));
731
732         /* Hack -- "easy" plus "easy" yields "easy" */
733         if (z1 && z2)
734         {
735                 g_ptr->info |= (CAVE_XTRA);
736
737                 cave_view_hack(floor_ptr, g_ptr, y, x);
738
739                 return (wall);
740         }
741
742         /* Hack -- primary "easy" yields "viewed" */
743         if (z1)
744         {
745                 cave_view_hack(floor_ptr, g_ptr, y, x);
746
747                 return (wall);
748         }
749
750         /* Hack -- "view" plus "view" yields "view" */
751         if (v1 && v2)
752         {
753                 /* g_ptr->info |= (CAVE_XTRA); */
754
755                 cave_view_hack(floor_ptr, g_ptr, y, x);
756
757                 return (wall);
758         }
759
760
761         /* Mega-Hack -- the "los()" function works poorly on walls */
762         if (wall)
763         {
764                 cave_view_hack(floor_ptr, g_ptr, y, x);
765
766                 return (wall);
767         }
768
769
770         /* Hack -- check line of sight */
771         if (los(floor_ptr, subject_ptr->y, subject_ptr->x, y, x))
772         {
773                 cave_view_hack(floor_ptr, g_ptr, y, x);
774
775                 return (wall);
776         }
777
778
779         /* Assume no line of sight. */
780         return TRUE;
781 }
782
783
784 /*
785  * Calculate the viewable space
786  *
787  *  1: Process the player
788  *  1a: The player is always (easily) viewable
789  *  2: Process the diagonals
790  *  2a: The diagonals are (easily) viewable up to the first wall
791  *  2b: But never go more than 2/3 of the "full" distance
792  *  3: Process the main axes
793  *  3a: The main axes are (easily) viewable up to the first wall
794  *  3b: But never go more than the "full" distance
795  *  4: Process sequential "strips" in each of the eight octants
796  *  4a: Each strip runs along the previous strip
797  *  4b: The main axes are "previous" to the first strip
798  *  4c: Process both "sides" of each "direction" of each strip
799  *  4c1: Each side aborts as soon as possible
800  *  4c2: Each side tells the next strip how far it has to check
801  *
802  * Note that the octant processing involves some pretty interesting
803  * observations involving when a grid might possibly be viewable from
804  * a given grid, and on the order in which the strips are processed.
805  *
806  * Note the use of the mathematical facts shown below, which derive
807  * from the fact that (1 < sqrt(2) < 1.5), and that the length of the
808  * hypotenuse of a right triangle is primarily determined by the length
809  * of the longest side, when one side is small, and is strictly less
810  * than one-and-a-half times as long as the longest side when both of
811  * the sides are large.
812  *
813  *   if (manhatten(dy,dx) < R) then (hypot(dy,dx) < R)
814  *   if (manhatten(dy,dx) > R*3/2) then (hypot(dy,dx) > R)
815  *
816  *   hypot(dy,dx) is approximated by (dx+dy+MAX(dx,dy)) / 2
817  *
818  * These observations are important because the calculation of the actual
819  * value of "hypot(dx,dy)" is extremely expensive, involving square roots,
820  * while for small values (up to about 20 or so), the approximations above
821  * are correct to within an error of at most one grid or so.
822  *
823  * Observe the use of "full" and "over" in the code below, and the use of
824  * the specialized calculation involving "limit", all of which derive from
825  * the observations given above.  Basically, we note that the "circle" of
826  * view is completely contained in an "octagon" whose bounds are easy to
827  * determine, and that only a few steps are needed to derive the actual
828  * bounds of the circle given the bounds of the octagon.
829  *
830  * Note that by skipping all the grids in the corners of the octagon, we
831  * place an upper limit on the number of grids in the field of view, given
832  * that "full" is never more than 20.  Of the 1681 grids in the "square" of
833  * view, only about 1475 of these are in the "octagon" of view, and even
834  * fewer are in the "circle" of view, so 1500 or 1536 is more than enough
835  * entries to completely contain the actual field of view.
836  *
837  * Note also the care taken to prevent "running off the map".  The use of
838  * explicit checks on the "validity" of the "diagonal", and the fact that
839  * the loops are never allowed to "leave" the map, lets "update_view_aux()"
840  * use the optimized "cave_los_bold()" macro, and to avoid the overhead
841  * of multiple checks on the validity of grids.
842  *
843  * Note the "optimizations" involving the "se","sw","ne","nw","es","en",
844  * "ws","wn" variables.  They work like this: While travelling down the
845  * south-bound strip just to the east of the main south axis, as soon as
846  * we get to a grid which does not "transmit" viewing, if all of the strips
847  * preceding us (in this case, just the main axis) had terminated at or before
848  * the same point, then we can stop, and reset the "max distance" to ourself.
849  * So, each strip (named by major axis plus offset, thus "se" in this case)
850  * maintains a "blockage" variable, initialized during the main axis step,
851  * and checks it whenever a blockage is observed.  After processing each
852  * strip as far as the previous strip told us to process, the next strip is
853  * told not to go farther than the current strip's farthest viewable grid,
854  * unless open space is still available.  This uses the "k" variable.
855  *
856  * Note the use of "inline" macros for efficiency.  The "cave_los_grid()"
857  * macro is a replacement for "cave_los_bold()" which takes a pointer to
858  * a grid instead of its location.  The "cave_view_hack()" macro is a
859  * chunk of code which adds the given location to the "view" array if it
860  * is not already there, using both the actual location and a pointer to
861  * the grid.  See above.
862  *
863  * By the way, the purpose of this code is to reduce the dependancy on the
864  * "los()" function which is slow, and, in some cases, not very accurate.
865  *
866  * It is very possible that I am the only person who fully understands this
867  * function, and for that I am truly sorry, but efficiency was very important
868  * and the "simple" version of this function was just not fast enough.  I am
869  * more than willing to replace this function with a simpler one, if it is
870  * equally efficient, and especially willing if the new function happens to
871  * derive "reverse-line-of-sight" at the same time, since currently monsters
872  * just use an optimized hack of "you see me, so I see you", and then use the
873  * actual "projectable()" function to check spell attacks.
874  */
875 void update_view(player_type *subject_ptr, floor_type *floor_ptr)
876 {
877         int n, m, d, k, z;
878         POSITION y, x;
879
880         int se, sw, ne, nw, es, en, ws, wn;
881
882         int full, over;
883
884         POSITION y_max = floor_ptr->height - 1;
885         POSITION x_max = floor_ptr->width - 1;
886
887         grid_type *g_ptr;
888
889         /*** Initialize ***/
890
891         /* Optimize */
892         if (view_reduce_view && !floor_ptr->dun_level)
893         {
894                 /* Full radius (10) */
895                 full = MAX_SIGHT / 2;
896
897                 /* Octagon factor (15) */
898                 over = MAX_SIGHT * 3 / 4;
899         }
900
901         /* Normal */
902         else
903         {
904                 /* Full radius (20) */
905                 full = MAX_SIGHT;
906
907                 /* Octagon factor (30) */
908                 over = MAX_SIGHT * 3 / 2;
909         }
910
911
912         /*** Step 0 -- Begin ***/
913
914         /* Save the old "view" grids for later */
915         for (n = 0; n < floor_ptr->view_n; n++)
916         {
917                 y = floor_ptr->view_y[n];
918                 x = floor_ptr->view_x[n];
919                 g_ptr = &floor_ptr->grid_array[y][x];
920
921                 /* Mark the grid as not in "view" */
922                 g_ptr->info &= ~(CAVE_VIEW);
923
924                 /* Mark the grid as "seen" */
925                 g_ptr->info |= (CAVE_TEMP);
926
927                 /* Add it to the "seen" set */
928                 tmp_pos.y[tmp_pos.n] = y;
929                 tmp_pos.x[tmp_pos.n] = x;
930                 tmp_pos.n++;
931         }
932
933         /* Start over with the "view" array */
934         floor_ptr->view_n = 0;
935
936         /*** Step 1 -- adjacent grids ***/
937
938         /* Now start on the player */
939         y = subject_ptr->y;
940         x = subject_ptr->x;
941         g_ptr = &floor_ptr->grid_array[y][x];
942
943         /* Assume the player grid is easily viewable */
944         g_ptr->info |= (CAVE_XTRA);
945
946         /* Assume the player grid is viewable */
947         cave_view_hack(floor_ptr, g_ptr, y, x);
948
949
950         /*** Step 2 -- Major Diagonals ***/
951
952         /* Hack -- Limit */
953         z = full * 2 / 3;
954
955         /* Scan south-east */
956         for (d = 1; d <= z; d++)
957         {
958                 g_ptr = &floor_ptr->grid_array[y + d][x + d];
959                 g_ptr->info |= (CAVE_XTRA);
960                 cave_view_hack(floor_ptr, g_ptr, y + d, x + d);
961                 if (!cave_los_grid(g_ptr)) break;
962         }
963
964         /* Scan south-west */
965         for (d = 1; d <= z; d++)
966         {
967                 g_ptr = &floor_ptr->grid_array[y + d][x - d];
968                 g_ptr->info |= (CAVE_XTRA);
969                 cave_view_hack(floor_ptr, g_ptr, y + d, x - d);
970                 if (!cave_los_grid(g_ptr)) break;
971         }
972
973         /* Scan north-east */
974         for (d = 1; d <= z; d++)
975         {
976                 g_ptr = &floor_ptr->grid_array[y - d][x + d];
977                 g_ptr->info |= (CAVE_XTRA);
978                 cave_view_hack(floor_ptr, g_ptr, y - d, x + d);
979                 if (!cave_los_grid(g_ptr)) break;
980         }
981
982         /* Scan north-west */
983         for (d = 1; d <= z; d++)
984         {
985                 g_ptr = &floor_ptr->grid_array[y - d][x - d];
986                 g_ptr->info |= (CAVE_XTRA);
987                 cave_view_hack(floor_ptr, g_ptr, y - d, x - d);
988                 if (!cave_los_grid(g_ptr)) break;
989         }
990
991         /*** Step 3 -- major axes ***/
992
993         /* Scan south */
994         for (d = 1; d <= full; d++)
995         {
996                 g_ptr = &floor_ptr->grid_array[y + d][x];
997                 g_ptr->info |= (CAVE_XTRA);
998                 cave_view_hack(floor_ptr, g_ptr, y + d, x);
999                 if (!cave_los_grid(g_ptr)) break;
1000         }
1001
1002         /* Initialize the "south strips" */
1003         se = sw = d;
1004
1005         /* Scan north */
1006         for (d = 1; d <= full; d++)
1007         {
1008                 g_ptr = &floor_ptr->grid_array[y - d][x];
1009                 g_ptr->info |= (CAVE_XTRA);
1010                 cave_view_hack(floor_ptr, g_ptr, y - d, x);
1011                 if (!cave_los_grid(g_ptr)) break;
1012         }
1013
1014         /* Initialize the "north strips" */
1015         ne = nw = d;
1016
1017         /* Scan east */
1018         for (d = 1; d <= full; d++)
1019         {
1020                 g_ptr = &floor_ptr->grid_array[y][x + d];
1021                 g_ptr->info |= (CAVE_XTRA);
1022                 cave_view_hack(floor_ptr, g_ptr, y, x + d);
1023                 if (!cave_los_grid(g_ptr)) break;
1024         }
1025
1026         /* Initialize the "east strips" */
1027         es = en = d;
1028
1029         /* Scan west */
1030         for (d = 1; d <= full; d++)
1031         {
1032                 g_ptr = &floor_ptr->grid_array[y][x - d];
1033                 g_ptr->info |= (CAVE_XTRA);
1034                 cave_view_hack(floor_ptr, g_ptr, y, x - d);
1035                 if (!cave_los_grid(g_ptr)) break;
1036         }
1037
1038         /* Initialize the "west strips" */
1039         ws = wn = d;
1040
1041
1042         /*** Step 4 -- Divide each "octant" into "strips" ***/
1043
1044         /* Now check each "diagonal" (in parallel) */
1045         for (n = 1; n <= over / 2; n++)
1046         {
1047                 POSITION ypn, ymn, xpn, xmn;
1048
1049                 /* Acquire the "bounds" of the maximal circle */
1050                 z = over - n - n;
1051                 if (z > full - n) z = full - n;
1052                 while ((z + n + (n >> 1)) > full) z--;
1053
1054
1055                 /* Access the four diagonal grids */
1056                 ypn = y + n;
1057                 ymn = y - n;
1058                 xpn = x + n;
1059                 xmn = x - n;
1060
1061
1062                 /* South strip */
1063                 if (ypn < y_max)
1064                 {
1065                         /* Maximum distance */
1066                         m = MIN(z, y_max - ypn);
1067
1068                         /* East side */
1069                         if ((xpn <= x_max) && (n < se))
1070                         {
1071                                 /* Scan */
1072                                 for (k = n, d = 1; d <= m; d++)
1073                                 {
1074                                         /* Check grid "d" in strip "n", notice "blockage" */
1075                                         if (update_view_aux(subject_ptr, ypn + d, xpn, ypn + d - 1, xpn - 1, ypn + d - 1, xpn))
1076                                         {
1077                                                 if (n + d >= se) break;
1078                                         }
1079
1080                                         /* Track most distant "non-blockage" */
1081                                         else
1082                                         {
1083                                                 k = n + d;
1084                                         }
1085                                 }
1086
1087                                 /* Limit the next strip */
1088                                 se = k + 1;
1089                         }
1090
1091                         /* West side */
1092                         if ((xmn >= 0) && (n < sw))
1093                         {
1094                                 /* Scan */
1095                                 for (k = n, d = 1; d <= m; d++)
1096                                 {
1097                                         /* Check grid "d" in strip "n", notice "blockage" */
1098                                         if (update_view_aux(subject_ptr, ypn + d, xmn, ypn + d - 1, xmn + 1, ypn + d - 1, xmn))
1099                                         {
1100                                                 if (n + d >= sw) break;
1101                                         }
1102
1103                                         /* Track most distant "non-blockage" */
1104                                         else
1105                                         {
1106                                                 k = n + d;
1107                                         }
1108                                 }
1109
1110                                 /* Limit the next strip */
1111                                 sw = k + 1;
1112                         }
1113                 }
1114
1115
1116                 /* North strip */
1117                 if (ymn > 0)
1118                 {
1119                         /* Maximum distance */
1120                         m = MIN(z, ymn);
1121
1122                         /* East side */
1123                         if ((xpn <= x_max) && (n < ne))
1124                         {
1125                                 /* Scan */
1126                                 for (k = n, d = 1; d <= m; d++)
1127                                 {
1128                                         /* Check grid "d" in strip "n", notice "blockage" */
1129                                         if (update_view_aux(subject_ptr, ymn - d, xpn, ymn - d + 1, xpn - 1, ymn - d + 1, xpn))
1130                                         {
1131                                                 if (n + d >= ne) break;
1132                                         }
1133
1134                                         /* Track most distant "non-blockage" */
1135                                         else
1136                                         {
1137                                                 k = n + d;
1138                                         }
1139                                 }
1140
1141                                 /* Limit the next strip */
1142                                 ne = k + 1;
1143                         }
1144
1145                         /* West side */
1146                         if ((xmn >= 0) && (n < nw))
1147                         {
1148                                 /* Scan */
1149                                 for (k = n, d = 1; d <= m; d++)
1150                                 {
1151                                         /* Check grid "d" in strip "n", notice "blockage" */
1152                                         if (update_view_aux(subject_ptr, ymn - d, xmn, ymn - d + 1, xmn + 1, ymn - d + 1, xmn))
1153                                         {
1154                                                 if (n + d >= nw) break;
1155                                         }
1156
1157                                         /* Track most distant "non-blockage" */
1158                                         else
1159                                         {
1160                                                 k = n + d;
1161                                         }
1162                                 }
1163
1164                                 /* Limit the next strip */
1165                                 nw = k + 1;
1166                         }
1167                 }
1168
1169
1170                 /* East strip */
1171                 if (xpn < x_max)
1172                 {
1173                         /* Maximum distance */
1174                         m = MIN(z, x_max - xpn);
1175
1176                         /* South side */
1177                         if ((ypn <= x_max) && (n < es))
1178                         {
1179                                 /* Scan */
1180                                 for (k = n, d = 1; d <= m; d++)
1181                                 {
1182                                         /* Check grid "d" in strip "n", notice "blockage" */
1183                                         if (update_view_aux(subject_ptr, ypn, xpn + d, ypn - 1, xpn + d - 1, ypn, xpn + d - 1))
1184                                         {
1185                                                 if (n + d >= es) break;
1186                                         }
1187
1188                                         /* Track most distant "non-blockage" */
1189                                         else
1190                                         {
1191                                                 k = n + d;
1192                                         }
1193                                 }
1194
1195                                 /* Limit the next strip */
1196                                 es = k + 1;
1197                         }
1198
1199                         /* North side */
1200                         if ((ymn >= 0) && (n < en))
1201                         {
1202                                 /* Scan */
1203                                 for (k = n, d = 1; d <= m; d++)
1204                                 {
1205                                         /* Check grid "d" in strip "n", notice "blockage" */
1206                                         if (update_view_aux(subject_ptr, ymn, xpn + d, ymn + 1, xpn + d - 1, ymn, xpn + d - 1))
1207                                         {
1208                                                 if (n + d >= en) break;
1209                                         }
1210
1211                                         /* Track most distant "non-blockage" */
1212                                         else
1213                                         {
1214                                                 k = n + d;
1215                                         }
1216                                 }
1217
1218                                 /* Limit the next strip */
1219                                 en = k + 1;
1220                         }
1221                 }
1222
1223
1224                 /* West strip */
1225                 if (xmn > 0)
1226                 {
1227                         /* Maximum distance */
1228                         m = MIN(z, xmn);
1229
1230                         /* South side */
1231                         if ((ypn <= y_max) && (n < ws))
1232                         {
1233                                 /* Scan */
1234                                 for (k = n, d = 1; d <= m; d++)
1235                                 {
1236                                         /* Check grid "d" in strip "n", notice "blockage" */
1237                                         if (update_view_aux(subject_ptr, ypn, xmn - d, ypn - 1, xmn - d + 1, ypn, xmn - d + 1))
1238                                         {
1239                                                 if (n + d >= ws) break;
1240                                         }
1241
1242                                         /* Track most distant "non-blockage" */
1243                                         else
1244                                         {
1245                                                 k = n + d;
1246                                         }
1247                                 }
1248
1249                                 /* Limit the next strip */
1250                                 ws = k + 1;
1251                         }
1252
1253                         /* North side */
1254                         if ((ymn >= 0) && (n < wn))
1255                         {
1256                                 /* Scan */
1257                                 for (k = n, d = 1; d <= m; d++)
1258                                 {
1259                                         /* Check grid "d" in strip "n", notice "blockage" */
1260                                         if (update_view_aux(subject_ptr, ymn, xmn - d, ymn + 1, xmn - d + 1, ymn, xmn - d + 1))
1261                                         {
1262                                                 if (n + d >= wn) break;
1263                                         }
1264
1265                                         /* Track most distant "non-blockage" */
1266                                         else
1267                                         {
1268                                                 k = n + d;
1269                                         }
1270                                 }
1271
1272                                 /* Limit the next strip */
1273                                 wn = k + 1;
1274                         }
1275                 }
1276         }
1277
1278
1279         /*** Step 5 -- Complete the algorithm ***/
1280
1281         /* Update all the new grids */
1282         for (n = 0; n < floor_ptr->view_n; n++)
1283         {
1284                 y = floor_ptr->view_y[n];
1285                 x = floor_ptr->view_x[n];
1286                 g_ptr = &floor_ptr->grid_array[y][x];
1287
1288                 /* Clear the "CAVE_XTRA" flag */
1289                 g_ptr->info &= ~(CAVE_XTRA);
1290
1291                 /* Update only newly viewed grids */
1292                 if (g_ptr->info & (CAVE_TEMP)) continue;
1293
1294                 /* Add it to later visual update */
1295                 cave_note_and_redraw_later(floor_ptr, g_ptr, y, x);
1296         }
1297
1298         /* Wipe the old grids, update as needed */
1299         for (n = 0; n < tmp_pos.n; n++)
1300         {
1301                 y = tmp_pos.y[n];
1302                 x = tmp_pos.x[n];
1303                 g_ptr = &floor_ptr->grid_array[y][x];
1304
1305                 /* No longer in the array */
1306                 g_ptr->info &= ~(CAVE_TEMP);
1307
1308                 /* Update only non-viewable grids */
1309                 if (g_ptr->info & (CAVE_VIEW)) continue;
1310
1311                 /* Add it to later visual update */
1312                 cave_redraw_later(floor_ptr, g_ptr, y, x);
1313         }
1314
1315         /* None left */
1316         tmp_pos.n = 0;
1317
1318         /* Mega-Hack -- Visual update later */
1319         subject_ptr->update |= (PU_DELAY_VIS);
1320 }
1321
1322
1323 /*!
1324  * @brief モンスターによる光量状態更新 / Add a square to the changes array
1325  * @param subject_ptr 主観となるクリーチャーの参照ポインタ
1326  * @param y Y座標
1327  * @param x X座標
1328  */
1329 static void mon_lite_hack(player_type *subject_ptr, POSITION y, POSITION x)
1330 {
1331         grid_type *g_ptr;
1332         int dpf, d;
1333         POSITION midpoint;
1334
1335         /* We trust this grid is in bounds */
1336         /* if (!in_bounds2(y, x)) return; */
1337
1338         g_ptr = &subject_ptr->current_floor_ptr->grid_array[y][x];
1339
1340         /* Want a unlit square in view of the player */
1341         if ((g_ptr->info & (CAVE_MNLT | CAVE_VIEW)) != CAVE_VIEW) return;
1342
1343         if (!cave_los_grid(g_ptr))
1344         {
1345                 /* Hack -- Prevent monster lite leakage in walls */
1346
1347                 /* Horizontal walls between player and a monster */
1348                 if (((y < subject_ptr->y) && (y > mon_fy)) || ((y > subject_ptr->y) && (y < mon_fy)))
1349                 {
1350                         dpf = subject_ptr->y - mon_fy;
1351                         d = y - mon_fy;
1352                         midpoint = mon_fx + ((subject_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
1353
1354                         /* Only first wall viewed from mid-x is lit */
1355                         if (x < midpoint)
1356                         {
1357                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y, x + 1)) return;
1358                         }
1359                         else if (x > midpoint)
1360                         {
1361                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y, x - 1)) return;
1362                         }
1363
1364                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
1365                         else if (mon_invis) return;
1366                 }
1367
1368                 /* Vertical walls between player and a monster */
1369                 if (((x < subject_ptr->x) && (x > mon_fx)) || ((x > subject_ptr->x) && (x < mon_fx)))
1370                 {
1371                         dpf = subject_ptr->x - mon_fx;
1372                         d = x - mon_fx;
1373                         midpoint = mon_fy + ((subject_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
1374
1375                         /* Only first wall viewed from mid-y is lit */
1376                         if (y < midpoint)
1377                         {
1378                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y + 1, x)) return;
1379                         }
1380                         else if (y > midpoint)
1381                         {
1382                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y - 1, x)) return;
1383                         }
1384
1385                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
1386                         else if (mon_invis) return;
1387                 }
1388         }
1389
1390         /* We trust tmp_pos.n does not exceed TEMP_MAX */
1391
1392         /* New grid */
1393         if (!(g_ptr->info & CAVE_MNDK))
1394         {
1395                 /* Save this square */
1396                 tmp_pos.x[tmp_pos.n] = x;
1397                 tmp_pos.y[tmp_pos.n] = y;
1398                 tmp_pos.n++;
1399         }
1400
1401         /* Darkened grid */
1402         else
1403         {
1404                 /* No longer dark */
1405                 g_ptr->info &= ~(CAVE_MNDK);
1406         }
1407
1408         /* Light it */
1409         g_ptr->info |= CAVE_MNLT;
1410 }
1411
1412
1413 /*
1414  * Add a square to the changes array
1415  */
1416 static void mon_dark_hack(player_type *subject_ptr, POSITION y, POSITION x)
1417 {
1418         grid_type *g_ptr;
1419         int midpoint, dpf, d;
1420
1421         /* We trust this grid is in bounds */
1422         /* if (!in_bounds2(y, x)) return; */
1423
1424         g_ptr = &subject_ptr->current_floor_ptr->grid_array[y][x];
1425
1426         /* Want a unlit and undarkened square in view of the player */
1427         if ((g_ptr->info & (CAVE_LITE | CAVE_MNLT | CAVE_MNDK | CAVE_VIEW)) != CAVE_VIEW) return;
1428
1429         if (!cave_los_grid(g_ptr) && !cave_have_flag_grid(g_ptr, FF_PROJECT))
1430         {
1431                 /* Hack -- Prevent monster dark lite leakage in walls */
1432
1433                 /* Horizontal walls between player and a monster */
1434                 if (((y < subject_ptr->y) && (y > mon_fy)) || ((y > subject_ptr->y) && (y < mon_fy)))
1435                 {
1436                         dpf = subject_ptr->y - mon_fy;
1437                         d = y - mon_fy;
1438                         midpoint = mon_fx + ((subject_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
1439
1440                         /* Only first wall viewed from mid-x is lit */
1441                         if (x < midpoint)
1442                         {
1443                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y, x + 1) && !cave_have_flag_bold(subject_ptr->current_floor_ptr, y, x + 1, FF_PROJECT)) return;
1444                         }
1445                         else if (x > midpoint)
1446                         {
1447                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y, x - 1) && !cave_have_flag_bold(subject_ptr->current_floor_ptr, y, x - 1, FF_PROJECT)) return;
1448                         }
1449
1450                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
1451                         else if (mon_invis) return;
1452                 }
1453
1454                 /* Vertical walls between player and a monster */
1455                 if (((x < subject_ptr->x) && (x > mon_fx)) || ((x > subject_ptr->x) && (x < mon_fx)))
1456                 {
1457                         dpf = subject_ptr->x - mon_fx;
1458                         d = x - mon_fx;
1459                         midpoint = mon_fy + ((subject_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
1460
1461                         /* Only first wall viewed from mid-y is lit */
1462                         if (y < midpoint)
1463                         {
1464                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y + 1, x) && !cave_have_flag_bold(subject_ptr->current_floor_ptr, y + 1, x, FF_PROJECT)) return;
1465                         }
1466                         else if (y > midpoint)
1467                         {
1468                                 if (!cave_los_bold(subject_ptr->current_floor_ptr, y - 1, x) && !cave_have_flag_bold(subject_ptr->current_floor_ptr, y - 1, x, FF_PROJECT)) return;
1469                         }
1470
1471                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
1472                         else if (mon_invis) return;
1473                 }
1474         }
1475
1476         /* We trust tmp_pos.n does not exceed TEMP_MAX */
1477
1478         /* Save this square */
1479         tmp_pos.x[tmp_pos.n] = x;
1480         tmp_pos.y[tmp_pos.n] = y;
1481         tmp_pos.n++;
1482
1483         /* Darken it */
1484         g_ptr->info |= CAVE_MNDK;
1485 }
1486
1487 /*
1488  * Update squares illuminated or darkened by monsters.
1489  *
1490  * Hack - use the CAVE_ROOM flag (renamed to be CAVE_MNLT) to
1491  * denote squares illuminated by monsters.
1492  *
1493  * The CAVE_TEMP and CAVE_XTRA flag are used to store the state during the
1494  * updating.  Only squares in view of the player, whos state
1495  * changes are drawn via lite_spot().
1496  */
1497 void update_mon_lite(player_type *subject_ptr, floor_type *floor_ptr)
1498 {
1499         int i, rad;
1500         grid_type *g_ptr;
1501
1502         POSITION fx, fy;
1503         void(*add_mon_lite)(player_type *, POSITION, POSITION);
1504         int f_flag;
1505
1506         s16b end_temp;
1507
1508         /* Non-Ninja player in the darkness */
1509         int dis_lim = ((d_info[subject_ptr->dungeon_idx].flags1 & DF1_DARKNESS) && !subject_ptr->see_nocto) ?
1510                 (MAX_SIGHT / 2 + 1) : (MAX_SIGHT + 3);
1511
1512         /* Clear all monster lit squares */
1513         for (i = 0; i < floor_ptr->mon_lite_n; i++)
1514         {
1515                 /* Point to grid */
1516                 g_ptr = &floor_ptr->grid_array[floor_ptr->mon_lite_y[i]][floor_ptr->mon_lite_x[i]];
1517
1518                 /* Set temp or xtra flag */
1519                 g_ptr->info |= (g_ptr->info & CAVE_MNLT) ? CAVE_TEMP : CAVE_XTRA;
1520
1521                 /* Clear monster illumination flag */
1522                 g_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
1523         }
1524
1525         /* Empty temp list of new squares to lite up */
1526         tmp_pos.n = 0;
1527
1528         /* If a monster stops time, don't process */
1529         if (!current_world_ptr->timewalk_m_idx)
1530         {
1531                 monster_type *m_ptr;
1532                 monster_race *r_ptr;
1533
1534                 /* Loop through monsters, adding newly lit squares to changes list */
1535                 for (i = 1; i < floor_ptr->m_max; i++)
1536                 {
1537                         m_ptr = &floor_ptr->m_list[i];
1538                         r_ptr = &r_info[m_ptr->r_idx];
1539                         if (!monster_is_valid(m_ptr)) continue;
1540
1541                         /* Is it too far away? */
1542                         if (m_ptr->cdis > dis_lim) continue;
1543
1544                         /* Get lite radius */
1545                         rad = 0;
1546
1547                         /* Note the radii are cumulative */
1548                         if (r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_SELF_LITE_1)) rad++;
1549                         if (r_ptr->flags7 & (RF7_HAS_LITE_2 | RF7_SELF_LITE_2)) rad += 2;
1550                         if (r_ptr->flags7 & (RF7_HAS_DARK_1 | RF7_SELF_DARK_1)) rad--;
1551                         if (r_ptr->flags7 & (RF7_HAS_DARK_2 | RF7_SELF_DARK_2)) rad -= 2;
1552
1553                         /* Exit if has no light */
1554                         if (!rad) continue;
1555                         else if (rad > 0)
1556                         {
1557                                 if (!(r_ptr->flags7 & (RF7_SELF_LITE_1 | RF7_SELF_LITE_2)) && (MON_CSLEEP(m_ptr) || (!floor_ptr->dun_level && is_daytime()) || subject_ptr->phase_out)) continue;
1558                                 if (d_info[subject_ptr->dungeon_idx].flags1 & DF1_DARKNESS) rad = 1;
1559                                 add_mon_lite = mon_lite_hack;
1560                                 f_flag = FF_LOS;
1561                         }
1562                         else
1563                         {
1564                                 if (!(r_ptr->flags7 & (RF7_SELF_DARK_1 | RF7_SELF_DARK_2)) && (MON_CSLEEP(m_ptr) || (!floor_ptr->dun_level && !is_daytime()))) continue;
1565                                 add_mon_lite = mon_dark_hack;
1566                                 f_flag = FF_PROJECT;
1567                                 rad = -rad; /* Use absolute value */
1568                         }
1569
1570                         mon_fx = m_ptr->fx;
1571                         mon_fy = m_ptr->fy;
1572
1573                         /* Is the monster visible? */
1574                         mon_invis = !(floor_ptr->grid_array[mon_fy][mon_fx].info & CAVE_VIEW);
1575
1576                         /* The square it is on */
1577                         add_mon_lite(subject_ptr, mon_fy, mon_fx);
1578
1579                         /* Adjacent squares */
1580                         add_mon_lite(subject_ptr, mon_fy + 1, mon_fx);
1581                         add_mon_lite(subject_ptr, mon_fy - 1, mon_fx);
1582                         add_mon_lite(subject_ptr, mon_fy, mon_fx + 1);
1583                         add_mon_lite(subject_ptr, mon_fy, mon_fx - 1);
1584                         add_mon_lite(subject_ptr, mon_fy + 1, mon_fx + 1);
1585                         add_mon_lite(subject_ptr, mon_fy + 1, mon_fx - 1);
1586                         add_mon_lite(subject_ptr, mon_fy - 1, mon_fx + 1);
1587                         add_mon_lite(subject_ptr, mon_fy - 1, mon_fx - 1);
1588
1589                         /* Radius 2 */
1590                         if (rad >= 2)
1591                         {
1592                                 /* South of the monster */
1593                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy + 1, mon_fx, f_flag))
1594                                 {
1595                                         add_mon_lite(subject_ptr, mon_fy + 2, mon_fx + 1);
1596                                         add_mon_lite(subject_ptr, mon_fy + 2, mon_fx);
1597                                         add_mon_lite(subject_ptr, mon_fy + 2, mon_fx - 1);
1598
1599                                         g_ptr = &floor_ptr->grid_array[mon_fy + 2][mon_fx];
1600
1601                                         /* Radius 3 */
1602                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
1603                                         {
1604                                                 add_mon_lite(subject_ptr, mon_fy + 3, mon_fx + 1);
1605                                                 add_mon_lite(subject_ptr, mon_fy + 3, mon_fx);
1606                                                 add_mon_lite(subject_ptr, mon_fy + 3, mon_fx - 1);
1607                                         }
1608                                 }
1609
1610                                 /* North of the monster */
1611                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy - 1, mon_fx, f_flag))
1612                                 {
1613                                         add_mon_lite(subject_ptr, mon_fy - 2, mon_fx + 1);
1614                                         add_mon_lite(subject_ptr, mon_fy - 2, mon_fx);
1615                                         add_mon_lite(subject_ptr, mon_fy - 2, mon_fx - 1);
1616
1617                                         g_ptr = &floor_ptr->grid_array[mon_fy - 2][mon_fx];
1618
1619                                         /* Radius 3 */
1620                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
1621                                         {
1622                                                 add_mon_lite(subject_ptr, mon_fy - 3, mon_fx + 1);
1623                                                 add_mon_lite(subject_ptr, mon_fy - 3, mon_fx);
1624                                                 add_mon_lite(subject_ptr, mon_fy - 3, mon_fx - 1);
1625                                         }
1626                                 }
1627
1628                                 /* East of the monster */
1629                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy, mon_fx + 1, f_flag))
1630                                 {
1631                                         add_mon_lite(subject_ptr, mon_fy + 1, mon_fx + 2);
1632                                         add_mon_lite(subject_ptr, mon_fy, mon_fx + 2);
1633                                         add_mon_lite(subject_ptr, mon_fy - 1, mon_fx + 2);
1634
1635                                         g_ptr = &floor_ptr->grid_array[mon_fy][mon_fx + 2];
1636
1637                                         /* Radius 3 */
1638                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
1639                                         {
1640                                                 add_mon_lite(subject_ptr, mon_fy + 1, mon_fx + 3);
1641                                                 add_mon_lite(subject_ptr, mon_fy, mon_fx + 3);
1642                                                 add_mon_lite(subject_ptr, mon_fy - 1, mon_fx + 3);
1643                                         }
1644                                 }
1645
1646                                 /* West of the monster */
1647                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy, mon_fx - 1, f_flag))
1648                                 {
1649                                         add_mon_lite(subject_ptr, mon_fy + 1, mon_fx - 2);
1650                                         add_mon_lite(subject_ptr, mon_fy, mon_fx - 2);
1651                                         add_mon_lite(subject_ptr, mon_fy - 1, mon_fx - 2);
1652
1653                                         g_ptr = &floor_ptr->grid_array[mon_fy][mon_fx - 2];
1654
1655                                         /* Radius 3 */
1656                                         if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
1657                                         {
1658                                                 add_mon_lite(subject_ptr, mon_fy + 1, mon_fx - 3);
1659                                                 add_mon_lite(subject_ptr, mon_fy, mon_fx - 3);
1660                                                 add_mon_lite(subject_ptr, mon_fy - 1, mon_fx - 3);
1661                                         }
1662                                 }
1663                         }
1664
1665                         /* Radius 3 */
1666                         if (rad == 3)
1667                         {
1668                                 /* South-East of the monster */
1669                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy + 1, mon_fx + 1, f_flag))
1670                                 {
1671                                         add_mon_lite(subject_ptr, mon_fy + 2, mon_fx + 2);
1672                                 }
1673
1674                                 /* South-West of the monster */
1675                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy + 1, mon_fx - 1, f_flag))
1676                                 {
1677                                         add_mon_lite(subject_ptr, mon_fy + 2, mon_fx - 2);
1678                                 }
1679
1680                                 /* North-East of the monster */
1681                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy - 1, mon_fx + 1, f_flag))
1682                                 {
1683                                         add_mon_lite(subject_ptr, mon_fy - 2, mon_fx + 2);
1684                                 }
1685
1686                                 /* North-West of the monster */
1687                                 if (cave_have_flag_bold(subject_ptr->current_floor_ptr, mon_fy - 1, mon_fx - 1, f_flag))
1688                                 {
1689                                         add_mon_lite(subject_ptr, mon_fy - 2, mon_fx - 2);
1690                                 }
1691                         }
1692                 }
1693         }
1694
1695         /* Save end of list of new squares */
1696         end_temp = tmp_pos.n;
1697
1698         /*
1699          * Look at old set flags to see if there are any changes.
1700          */
1701         for (i = 0; i < floor_ptr->mon_lite_n; i++)
1702         {
1703                 fx = floor_ptr->mon_lite_x[i];
1704                 fy = floor_ptr->mon_lite_y[i];
1705
1706                 /* We trust this grid is in bounds */
1707
1708                 /* Point to grid */
1709                 g_ptr = &floor_ptr->grid_array[fy][fx];
1710
1711                 if (g_ptr->info & CAVE_TEMP) /* Pervious lit */
1712                 {
1713                         /* It it no longer lit? */
1714                         if ((g_ptr->info & (CAVE_VIEW | CAVE_MNLT)) == CAVE_VIEW)
1715                         {
1716                                 /* It is now unlit */
1717                                 /* Add it to later visual update */
1718                                 cave_note_and_redraw_later(floor_ptr, g_ptr, fy, fx);
1719                         }
1720                 }
1721                 else /* Pervious darkened */
1722                 {
1723                         /* It it no longer darken? */
1724                         if ((g_ptr->info & (CAVE_VIEW | CAVE_MNDK)) == CAVE_VIEW)
1725                         {
1726                                 /* It is now undarken */
1727                                 /* Add it to later visual update */
1728                                 cave_note_and_redraw_later(floor_ptr, g_ptr, fy, fx);
1729                         }
1730                 }
1731
1732                 /* Add to end of temp array */
1733                 tmp_pos.x[tmp_pos.n] = fx;
1734                 tmp_pos.y[tmp_pos.n] = fy;
1735                 tmp_pos.n++;
1736         }
1737
1738         /* Clear the lite array */
1739         floor_ptr->mon_lite_n = 0;
1740
1741         /* Copy the temp array into the lit array lighting the new squares. */
1742         for (i = 0; i < end_temp; i++)
1743         {
1744                 fx = tmp_pos.x[i];
1745                 fy = tmp_pos.y[i];
1746
1747                 /* We trust this grid is in bounds */
1748
1749                 /* Point to grid */
1750                 g_ptr = &floor_ptr->grid_array[fy][fx];
1751
1752                 if (g_ptr->info & CAVE_MNLT) /* Lit */
1753                 {
1754                         /* The is the square newly lit and visible? */
1755                         if ((g_ptr->info & (CAVE_VIEW | CAVE_TEMP)) == CAVE_VIEW)
1756                         {
1757                                 /* It is now lit */
1758                                 /* Add it to later visual update */
1759                                 cave_note_and_redraw_later(floor_ptr, g_ptr, fy, fx);
1760                         }
1761                 }
1762                 else /* Darkened */
1763                 {
1764                         /* The is the square newly darkened and visible? */
1765                         if ((g_ptr->info & (CAVE_VIEW | CAVE_XTRA)) == CAVE_VIEW)
1766                         {
1767                                 /* It is now darkened */
1768                                 /* Add it to later visual update */
1769                                 cave_note_and_redraw_later(floor_ptr, g_ptr, fy, fx);
1770                         }
1771                 }
1772
1773                 /* Save in the monster lit or darkened array */
1774                 floor_ptr->mon_lite_x[floor_ptr->mon_lite_n] = fx;
1775                 floor_ptr->mon_lite_y[floor_ptr->mon_lite_n] = fy;
1776                 floor_ptr->mon_lite_n++;
1777         }
1778
1779         /* Clear the temp flag for the old lit or darken grids */
1780         for (i = end_temp; i < tmp_pos.n; i++)
1781         {
1782                 /* We trust this grid is in bounds */
1783
1784                 floor_ptr->grid_array[tmp_pos.y[i]][tmp_pos.x[i]].info &= ~(CAVE_TEMP | CAVE_XTRA);
1785         }
1786
1787         /* Finished with tmp_pos.n */
1788         tmp_pos.n = 0;
1789
1790         /* Mega-Hack -- Visual update later */
1791         subject_ptr->update |= (PU_DELAY_VIS);
1792
1793         subject_ptr->monlite = (floor_ptr->grid_array[subject_ptr->y][subject_ptr->x].info & CAVE_MNLT) ? TRUE : FALSE;
1794
1795         if (subject_ptr->special_defense & NINJA_S_STEALTH)
1796         {
1797                 if (subject_ptr->old_monlite != subject_ptr->monlite)
1798                 {
1799                         if (subject_ptr->monlite)
1800                         {
1801                                 msg_print(_("影の覆いが薄れた気がする。", "Your mantle of shadow become thin."));
1802                         }
1803                         else
1804                         {
1805                                 msg_print(_("影の覆いが濃くなった!", "Your mantle of shadow restored its original darkness."));
1806                         }
1807                 }
1808         }
1809         subject_ptr->old_monlite = subject_ptr->monlite;
1810 }
1811
1812 void clear_mon_lite(floor_type *floor_ptr)
1813 {
1814         int i;
1815         grid_type *g_ptr;
1816
1817         /* Clear all monster lit squares */
1818         for (i = 0; i < floor_ptr->mon_lite_n; i++)
1819         {
1820                 /* Point to grid */
1821                 g_ptr = &floor_ptr->grid_array[floor_ptr->mon_lite_y[i]][floor_ptr->mon_lite_x[i]];
1822
1823                 /* Clear monster illumination flag */
1824                 g_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
1825         }
1826
1827         /* Empty the array */
1828         floor_ptr->mon_lite_n = 0;
1829 }
1830
1831