OSDN Git Service

0248a689bc57ff91b2ad896fe457ec211b307c74
[hengband/hengband.git] / src / monster-process.c
1 /*!
2  * @file monster-process.c
3  * @brief モンスターの特殊技能と移動処理/ Monster spells and movement
4  * @date 2014/01/17
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen.\n
11  * @details
12  * This file has several additions to it by Keldon Jones (keldon@umr.edu)
13  * to improve the general quality of the AI (version 0.1.1).
14  */
15
16 #include "angband.h"
17 #include "cmd-pet.h"
18 #include "monsterrace-hook.h"
19 #include "melee.h"
20 #include "projection.h"
21 #include "spells-summon.h"
22 #include "quest.h"
23 #include "avatar.h"
24 #include "realm-hex.h"
25 #include "object-hook.h"
26 #include "feature.h"
27 #include "grid.h"
28 #include "player-move.h"
29
30
31 /*!
32  * @brief モンスターが敵に接近するための方向を決める /
33  * Calculate the direction to the next enemy
34  * @param m_idx モンスターの参照ID
35  * @param mm 移動するべき方角IDを返す参照ポインタ
36  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
37  */
38 static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
39 {
40         int i;
41         POSITION x = 0, y = 0;
42         IDX t_idx;
43         int start;
44         int plus = 1;
45
46         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
47         monster_race *r_ptr = &r_info[m_ptr->r_idx];
48         monster_type *t_ptr;
49
50         if (riding_t_m_idx && player_bold(m_ptr->fy, m_ptr->fx))
51         {
52                 y = current_floor_ptr->m_list[riding_t_m_idx].fy;
53                 x = current_floor_ptr->m_list[riding_t_m_idx].fx;
54         }
55         else if (is_pet(m_ptr) && pet_t_m_idx)
56         {
57                 y = current_floor_ptr->m_list[pet_t_m_idx].fy;
58                 x = current_floor_ptr->m_list[pet_t_m_idx].fx;
59         }
60         else
61         {
62                 if (p_ptr->inside_battle)
63                 {
64                         start = randint1(m_max-1)+m_max;
65                         if(randint0(2)) plus = -1;
66                 }
67                 else start = m_max + 1;
68
69                 /* Scan thru all monsters */
70                 for (i = start; ((i < start + m_max) && (i > start - m_max)); i+=plus)
71                 {
72                         IDX dummy = (i % m_max);
73
74                         if (!dummy) continue;
75
76                         t_idx = dummy;
77                         t_ptr = &current_floor_ptr->m_list[t_idx];
78
79                         /* The monster itself isn't a target */
80                         if (t_ptr == m_ptr) continue;
81
82                         /* Paranoia -- Skip dead monsters */
83                         if (!t_ptr->r_idx) continue;
84
85                         if (is_pet(m_ptr))
86                         {
87                                 /* Hack -- only fight away from player */
88                                 if (p_ptr->pet_follow_distance < 0)
89                                 {
90                                         /* No fighting near player */
91                                         if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
92                                         {
93                                                 continue;
94                                         }
95                                 }
96                                 /* Hack -- no fighting away from player */
97                                 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
98                                 {
99                                         continue;
100                                 }
101
102                                 if (r_ptr->aaf < t_ptr->cdis) continue;
103                         }
104
105                         /* Monster must be 'an enemy' */
106                         if (!are_enemies(m_ptr, t_ptr)) continue;
107
108                         /* Monster must be projectable if we can't pass through walls */
109                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
110                             ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
111                         {
112                                 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
113                         }
114                         else
115                         {
116                                 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
117                         }
118
119                         /* OK -- we've got a target */
120                         y = t_ptr->fy;
121                         x = t_ptr->fx;
122
123                         break;
124                 }
125                 if (!x && !y) return FALSE;
126         }
127
128         /* Extract the direction */
129         x -= m_ptr->fx;
130         y -= m_ptr->fy;
131
132         /* North */
133         if ((y < 0) && (x == 0))
134         {
135                 mm[0] = 8;
136                 mm[1] = 7;
137                 mm[2] = 9;
138         }
139         /* South */
140         else if ((y > 0) && (x == 0))
141         {
142                 mm[0] = 2;
143                 mm[1] = 1;
144                 mm[2] = 3;
145         }
146         /* East */
147         else if ((x > 0) && (y == 0))
148         {
149                 mm[0] = 6;
150                 mm[1] = 9;
151                 mm[2] = 3;
152         }
153         /* West */
154         else if ((x < 0) && (y == 0))
155         {
156                 mm[0] = 4;
157                 mm[1] = 7;
158                 mm[2] = 1;
159         }
160         /* North-West */
161         else if ((y < 0) && (x < 0))
162         {
163                 mm[0] = 7;
164                 mm[1] = 4;
165                 mm[2] = 8;
166         }
167         /* North-East */
168         else if ((y < 0) && (x > 0))
169         {
170                 mm[0] = 9;
171                 mm[1] = 6;
172                 mm[2] = 8;
173         }
174         /* South-West */
175         else if ((y > 0) && (x < 0))
176         {
177                 mm[0] = 1;
178                 mm[1] = 4;
179                 mm[2] = 2;
180         }
181         /* South-East */
182         else if ((y > 0) && (x > 0))
183         {
184                 mm[0] = 3;
185                 mm[1] = 6;
186                 mm[2] = 2;
187         }
188
189         /* Found a monster */
190         return TRUE;
191 }
192
193
194 /*!
195  * @brief モンスターが敵モンスターに行う打撃処理 /
196  * Hack, based on mon_take_hit... perhaps all monster attacks on other monsters should use this?
197  * @param m_idx 目標となるモンスターの参照ID
198  * @param dam ダメージ量
199  * @param dead 目標となったモンスターの死亡状態を返す参照ポインタ
200  * @param fear 目標となったモンスターの恐慌状態を返す参照ポインタ
201  * @param note 目標モンスターが死亡した場合の特別メッセージ(NULLならば標準表示を行う)
202  * @param who 打撃を行ったモンスターの参照ID
203  * @return なし
204  */
205 void mon_take_hit_mon(MONSTER_IDX m_idx, HIT_POINT dam, bool *dead, bool *fear, concptr note, IDX who)
206 {
207         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
208         monster_race *r_ptr = &r_info[m_ptr->r_idx];
209         GAME_TEXT m_name[160];
210         bool seen = is_seen(m_ptr);
211
212         /* Can the player be aware of this attack? */
213         bool known = (m_ptr->cdis <= MAX_SIGHT);
214
215         /* Extract monster name */
216         monster_desc(m_name, m_ptr, 0);
217
218         /* Redraw (later) if needed */
219         if (m_ptr->ml)
220         {
221                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
222                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
223         }
224
225         (void)set_monster_csleep(m_idx, 0);
226
227         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
228
229         if (MON_INVULNER(m_ptr) && randint0(PENETRATE_INVULNERABILITY))
230         {
231                 if (seen)
232                 {
233                         msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), m_name);
234                 }
235                 return;
236         }
237
238         if (r_ptr->flagsr & RFR_RES_ALL)
239         {
240                 if(dam > 0)
241                 {
242                         dam /= 100;
243                         if((dam == 0) && one_in_(3)) dam = 1;
244                 }
245                 if(dam == 0)
246                 {
247                         if (seen)
248                         {
249                                 msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), m_name);
250                         }
251                         return;
252                 }
253         }
254
255         /* Hurt it */
256         m_ptr->hp -= dam;
257
258         /* It is dead now... or is it? */
259         if (m_ptr->hp < 0)
260         {
261                 if (((r_ptr->flags1 & (RF1_UNIQUE | RF1_QUESTOR)) ||
262                     (r_ptr->flags7 & RF7_NAZGUL)) &&
263                     !p_ptr->inside_battle)
264                 {
265                         m_ptr->hp = 1;
266                 }
267                 else
268                 {
269                         /* Make a sound */
270                         if (!monster_living(m_ptr->r_idx))
271                         {
272                                 sound(SOUND_N_KILL);
273                         }
274                         else
275                         {
276                                 sound(SOUND_KILL);
277                         }
278
279                         *dead = TRUE;
280
281                         if (known)
282                         {
283                                 monster_desc(m_name, m_ptr, MD_TRUE_NAME);
284                                 /* Unseen death by normal attack */
285                                 if (!seen)
286                                 {
287                                         mon_fight = TRUE;
288                                 }
289                                 /* Death by special attack */
290                                 else if (note)
291                                 {
292                                         msg_format(_("%^s%s", "%^s%s"), m_name, note);
293                                 }
294                                 /* Death by normal attack -- nonliving monster */
295                                 else if (!monster_living(m_ptr->r_idx))
296                                 {
297                                         msg_format(_("%^sは破壊された。", "%^s is destroyed."), m_name);
298                                 }
299                                 /* Death by normal attack -- living monster */
300                                 else
301                                 {
302                                         msg_format(_("%^sは殺された。", "%^s is killed."), m_name);
303                                 }
304                         }
305
306                         monster_gain_exp(who, m_ptr->r_idx);
307                         monster_death(m_idx, FALSE);
308                         delete_monster_idx(m_idx);
309
310                         /* Not afraid */
311                         (*fear) = FALSE;
312
313                         /* Monster is dead */
314                         return;
315                 }
316         }
317
318         *dead = FALSE;
319
320 #ifdef ALLOW_FEAR
321
322         /* Mega-Hack -- Pain cancels fear */
323         if (MON_MONFEAR(m_ptr) && (dam > 0))
324         {
325                 /* Cure fear */
326                 if (set_monster_monfear(m_idx, MON_MONFEAR(m_ptr) - randint1(dam / 4)))
327                 {
328                         /* No more fear */
329                         (*fear) = FALSE;
330                 }
331         }
332
333         /* Sometimes a monster gets scared by damage */
334         if (!MON_MONFEAR(m_ptr) && !(r_ptr->flags3 & RF3_NO_FEAR))
335         {
336                 /* Percentage of fully healthy */
337                 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
338
339                 /*
340                 * Run (sometimes) if at 10% or less of max hit points,
341                 * or (usually) when hit for half its current hit points
342                  */
343                 if (((percentage <= 10) && (randint0(10) < percentage)) ||
344                         ((dam >= m_ptr->hp) && (randint0(100) < 80)))
345                 {
346                         /* Hack -- note fear */
347                         (*fear) = TRUE;
348
349                         /* Hack -- Add some timed fear */
350                         (void)set_monster_monfear(m_idx, (randint1(10) +
351                                 (((dam >= m_ptr->hp) && (percentage > 7)) ?
352                                 20 : ((11 - percentage) * 5))));
353                 }
354         }
355
356 #endif /* ALLOW_FEAR */
357
358         if ((dam > 0) && !is_pet(m_ptr) && !is_friendly(m_ptr) && (who != m_idx))
359         {
360                 if (is_pet(&current_floor_ptr->m_list[who]) && !player_bold(m_ptr->target_y, m_ptr->target_x))
361                 {
362                         set_target(m_ptr, current_floor_ptr->m_list[who].fy, current_floor_ptr->m_list[who].fx);
363                 }
364         }
365
366         if (p_ptr->riding && (p_ptr->riding == m_idx) && (dam > 0))
367         {
368                 /* Extract monster name */
369                 monster_desc(m_name, m_ptr, 0);
370
371                 if (m_ptr->hp > m_ptr->maxhp/3) dam = (dam + 1) / 2;
372                 if (rakuba((dam > 200) ? 200 : dam, FALSE))
373                 {
374                         msg_format(_("%^sに振り落とされた!", "You have thrown off from %s!"), m_name);
375                 }
376         }
377
378         /* Not dead yet */
379         return;
380 }
381
382
383 /*!
384  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
385  * Returns whether a given monster will try to run from the player.
386  * @param m_idx 逃走するモンスターの参照ID
387  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
388  * @details
389  * Monsters will attempt to avoid very powerful players.  See below.\n
390  *\n
391  * Because this function is called so often, little details are important\n
392  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
393  * attempting to check the conditions in an optimal order.  Note that\n
394  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
395  *\n
396  * Note that this function is responsible for about one to five percent\n
397  * of the processor use in normal conditions...\n
398  */
399 static bool mon_will_run(MONSTER_IDX m_idx)
400 {
401         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
402
403 #ifdef ALLOW_TERROR
404
405         monster_race *r_ptr = &r_info[m_ptr->r_idx];
406
407         PLAYER_LEVEL p_lev;
408         DEPTH m_lev;
409         HIT_POINT p_chp, p_mhp;
410         HIT_POINT m_chp, m_mhp;
411         u32b p_val, m_val;
412
413 #endif
414
415         /* Friends can be commanded to avoid the player */
416         if (is_pet(m_ptr))
417         {
418                 /* Are we trying to avoid the player? */
419                 return ((p_ptr->pet_follow_distance < 0) &&
420                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
421         }
422
423         /* Keep monsters from running too far away */
424         if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
425
426         /* All "afraid" monsters will run away */
427         if (MON_MONFEAR(m_ptr)) return (TRUE);
428
429 #ifdef ALLOW_TERROR
430
431         /* Nearby monsters will not become terrified */
432         if (m_ptr->cdis <= 5) return (FALSE);
433
434         /* Examine player power (level) */
435         p_lev = p_ptr->lev;
436
437         /* Examine monster power (level plus morale) */
438         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
439
440         /* Optimize extreme cases below */
441         if (m_lev > p_lev + 4) return (FALSE);
442         if (m_lev + 4 <= p_lev) return (TRUE);
443
444         /* Examine player health */
445         p_chp = p_ptr->chp;
446         p_mhp = p_ptr->mhp;
447
448         /* Examine monster health */
449         m_chp = m_ptr->hp;
450         m_mhp = m_ptr->maxhp;
451
452         /* Prepare to optimize the calculation */
453         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
454         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
455
456         /* Strong players scare strong monsters */
457         if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
458
459 #endif
460
461         /* Assume no terror */
462         return (FALSE);
463 }
464
465
466 /*!
467  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
468  * Search spell castable grid
469  * @param m_idx モンスターの参照ID
470  * @param yp 適したマスのY座標を返す参照ポインタ
471  * @param xp 適したマスのX座標を返す参照ポインタ
472  * @return 有効なマスがあった場合TRUEを返す
473  */
474 static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
475 {
476         int i, best = 999;
477         POSITION y, x, y1, x1;
478
479         grid_type *g_ptr;
480         bool can_open_door = FALSE;
481         int now_cost;
482
483         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
484         monster_race *r_ptr = &r_info[m_ptr->r_idx];
485
486         /* Monster location */
487         y1 = m_ptr->fy;
488         x1 = m_ptr->fx;
489
490         /* Monster can already cast spell to player */
491         if (projectable(y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
492
493         /* Set current grid cost */
494         now_cost = current_floor_ptr->grid_array[y1][x1].cost;
495         if (now_cost == 0) now_cost = 999;
496
497         /* Can monster bash or open doors? */
498         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
499         {
500                 can_open_door = TRUE;
501         }
502
503         /* Check nearby grids, diagonals first */
504         for (i = 7; i >= 0; i--)
505         {
506                 int cost;
507
508                 y = y1 + ddy_ddd[i];
509                 x = x1 + ddx_ddd[i];
510
511                 /* Ignore locations off of edge */
512                 if (!in_bounds2(y, x)) continue;
513
514                 /* Simply move to player */
515                 if (player_bold(y, x)) return (FALSE);
516
517                 g_ptr = &current_floor_ptr->grid_array[y][x];
518
519                 cost = g_ptr->cost;
520
521                 /* Monster cannot kill or pass walls */
522                 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding))))
523                 {
524                         if (cost == 0) continue;
525                         if (!can_open_door && is_closed_door(g_ptr->feat)) continue;
526                 }
527
528                 /* Hack -- for kill or pass wall monster.. */
529                 if (cost == 0) cost = 998;
530
531                 if (now_cost < cost) continue;
532
533                 if (!projectable(y, x, p_ptr->y, p_ptr->x)) continue;
534
535                 /* Accept louder sounds */
536                 if (best < cost) continue;
537                 best = cost;
538
539                 (*yp) = y1 + ddy_ddd[i];
540                 (*xp) = x1 + ddx_ddd[i];
541         }
542
543         /* No legal move (?) */
544         if (best == 999) return (FALSE);
545
546         /* Success */
547         return (TRUE);
548 }
549
550
551 /*!
552  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
553  * Choose the "best" direction for "flowing"
554  * @param m_idx モンスターの参照ID
555  * @param yp 移動先のマスのY座標を返す参照ポインタ
556  * @param xp 移動先のマスのX座標を返す参照ポインタ
557  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
558  * @return 有効なマスがあった場合TRUEを返す
559  * @details
560  * Note that ghosts and rock-eaters are never allowed to "flow",\n
561  * since they should move directly towards the player.\n
562  *\n
563  * Prefer "non-diagonal" directions, but twiddle them a little\n
564  * to angle slightly towards the player's actual location.\n
565  *\n
566  * Allow very perceptive monsters to track old "spoor" left by\n
567  * previous locations occupied by the player.  This will tend\n
568  * to have monsters end up either near the player or on a grid\n
569  * recently occupied by the player (and left via "teleport").\n
570  *\n
571  * Note that if "smell" is turned on, all monsters get vicious.\n
572  *\n
573  * Also note that teleporting away from a location will cause\n
574  * the monsters who were chasing you to converge on that location\n
575  * as long as you are still near enough to "annoy" them without\n
576  * being close enough to chase directly.  I have no idea what will\n
577  * happen if you combine "smell" with low "aaf" values.\n
578  */
579 static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
580 {
581         int i, best;
582         POSITION y, x, y1, x1;
583
584         grid_type *g_ptr;
585         bool use_scent = FALSE;
586
587         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
588         monster_race *r_ptr = &r_info[m_ptr->r_idx];
589
590         /* Can monster cast attack spell? */
591         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
592             r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
593             r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
594         {
595                 /* Can move spell castable grid? */
596                 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
597         }
598
599         /* Monster can't flow */
600         if (no_flow) return (FALSE);
601
602         /* Monster can go through rocks */
603         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
604         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
605
606         /* Monster location */
607         y1 = m_ptr->fy;
608         x1 = m_ptr->fx;
609
610         /* Hack -- Player can see us, run towards him */
611         if (player_has_los_bold(y1, x1) && projectable(p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
612
613         /* Monster grid */
614         g_ptr = &current_floor_ptr->grid_array[y1][x1];
615
616         /* If we can hear noises, advance towards them */
617         if (g_ptr->cost)
618         {
619                 best = 999;
620         }
621
622         /* Otherwise, try to follow a scent trail */
623         else if (g_ptr->when)
624         {
625                 /* Too old smell */
626                 if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when - g_ptr->when > 127) return (FALSE);
627
628                 use_scent = TRUE;
629                 best = 0;
630         }
631
632         /* Otherwise, advance blindly */
633         else
634         {
635                 return (FALSE);
636         }
637
638         /* Check nearby grids, diagonals first */
639         for (i = 7; i >= 0; i--)
640         {
641                 y = y1 + ddy_ddd[i];
642                 x = x1 + ddx_ddd[i];
643
644                 /* Ignore locations off of edge */
645                 if (!in_bounds2(y, x)) continue;
646
647                 g_ptr = &current_floor_ptr->grid_array[y][x];
648
649                 /* We're following a scent trail */
650                 if (use_scent)
651                 {
652                         int when = g_ptr->when;
653
654                         /* Accept younger scent */
655                         if (best > when) continue;
656                         best = when;
657                 }
658
659                 /* We're using sound */
660                 else
661                 {
662                         int cost;
663
664                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
665                                 cost = g_ptr->dist;
666                         else cost = g_ptr->cost;
667
668                         /* Accept louder sounds */
669                         if ((cost == 0) || (best < cost)) continue;
670                         best = cost;
671                 }
672
673                 /* Hack -- Save the "twiddled" location */
674                 (*yp) = p_ptr->y + 16 * ddy_ddd[i];
675                 (*xp) = p_ptr->x + 16 * ddx_ddd[i];
676         }
677
678         /* No legal move (?) */
679         if (best == 999 || best == 0) return (FALSE);
680
681         /* Success */
682         return (TRUE);
683 }
684
685
686 /*!
687  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
688  * Provide a location to flee to, but give the player a wide berth.
689  * @param m_idx モンスターの参照ID
690  * @param yp 移動先のマスのY座標を返す参照ポインタ
691  * @param xp 移動先のマスのX座標を返す参照ポインタ
692  * @return 有効なマスがあった場合TRUEを返す
693  * @details
694  * A monster may wish to flee to a location that is behind the player,\n
695  * but instead of heading directly for it, the monster should "swerve"\n
696  * around the player so that he has a smaller chance of getting hit.\n
697  */
698 static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
699 {
700         POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
701         int score = -1;
702         int i;
703
704         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
705
706         /* Monster location */
707         fy = m_ptr->fy;
708         fx = m_ptr->fx;
709
710         /* Desired destination */
711         y1 = fy - (*yp);
712         x1 = fx - (*xp);
713
714         /* Check nearby grids, diagonals first */
715         for (i = 7; i >= 0; i--)
716         {
717                 POSITION dis, s;
718
719                 y = fy + ddy_ddd[i];
720                 x = fx + ddx_ddd[i];
721
722                 /* Ignore locations off of edge */
723                 if (!in_bounds2(y, x)) continue;
724
725                 /* Don't move toward player */
726                 /* if (current_floor_ptr->grid_array[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
727
728                 /* Calculate distance of this grid from our destination */
729                 dis = distance(y, x, y1, x1);
730
731                 /* Score this grid */
732                 s = 5000 / (dis + 3) - 500 / (current_floor_ptr->grid_array[y][x].dist + 1);
733
734                 /* No negative scores */
735                 if (s < 0) s = 0;
736
737                 /* Ignore lower scores */
738                 if (s < score) continue;
739
740                 /* Save the score and time */
741                 score = s;
742
743                 /* Save the location */
744                 gy = y;
745                 gx = x;
746         }
747
748         /* No legal move (?) */
749         if (score == -1) return (FALSE);
750
751         /* Find deltas */
752         (*yp) = fy - gy;
753         (*xp) = fx - gx;
754
755         /* Success */
756         return (TRUE);
757 }
758
759 /*
760  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
761  * find_hiding().
762  *
763  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
764  * offsets of all the locations with a distance of n from a central point,
765  * with an offset of (0,0) indicating no more offsets at this distance.
766  *
767  * This is, of course, fairly unreadable, but it eliminates multiple loops
768  * from the previous version.
769  *
770  * It is probably better to replace these arrays with code to compute
771  * the relevant arrays, even if the storage is pre-allocated in hard
772  * coded sizes.  At the very least, code should be included which is
773  * able to generate and dump these arrays (ala "los()").  
774  *
775  * Also, the storage needs could be halved by using bytes.  
776  *
777  * These arrays could be combined into two big arrays, using sub-arrays
778  * to hold the offsets and lengths of each portion of the sub-arrays, and
779  * this could perhaps also be used somehow in the "look" code.  
780  */
781
782
783 static POSITION d_off_y_0[] = { 0 };
784 static POSITION d_off_x_0[] = { 0 };
785
786 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
787 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
788
789 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
790 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
791
792 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
793 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
794
795 static POSITION d_off_y_4[] = { -1, -1, -2, -2, -3, -3, -3, -3, -4, -4, -4, 0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 0 };
796 static POSITION d_off_x_4[] = { -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, -4, 4, -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, 0 };
797
798
799 static POSITION d_off_y_5[] =
800 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
801   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
802   5, 0 };
803
804 static POSITION d_off_x_5[] =
805 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
806   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
807   0, 1, 0 };
808
809
810 static POSITION d_off_y_6[] =
811 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
812   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
813   5, 5, 6, 6, 6, 0 };
814
815 static POSITION d_off_x_6[] =
816 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
817   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
818   -3, 2, 3, -1, 0, 1, 0 };
819
820
821 static POSITION d_off_y_7[] =
822 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
823   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
824   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
825
826 static POSITION d_off_x_7[] =
827 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
828   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
829   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
830   1, 0 };
831
832
833 static POSITION d_off_y_8[] =
834 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
835   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
836   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
837   8, 8, 8, 0 };
838
839 static POSITION d_off_x_8[] =
840 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
841   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
842   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
843   2, 3, -1, 0, 1, 0 };
844
845
846 static POSITION d_off_y_9[] =
847 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
848   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
849   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
850   7, 8, 8, 8, 8, 9, 9, 9, 0 };
851
852 static POSITION d_off_x_9[] =
853 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
854   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
855   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
856   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
857
858
859 static POSITION *dist_offsets_y[10] =
860 {
861         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
862         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
863 };
864
865 static POSITION *dist_offsets_x[10] =
866 {
867         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
868         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
869 };
870
871 /*!
872  * @brief モンスターが逃げ込める安全な地点を返す /
873  * Choose a "safe" location near a monster for it to run toward.
874  * @param m_idx モンスターの参照ID
875  * @param yp 移動先のマスのY座標を返す参照ポインタ
876  * @param xp 移動先のマスのX座標を返す参照ポインタ
877  * @return 有効なマスがあった場合TRUEを返す
878  * @details
879  * A location is "safe" if it can be reached quickly and the player\n
880  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
881  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
882  * try to run towards corridor openings if they are in a room.\n
883  *\n
884  * This function may take lots of CPU time if lots of monsters are\n
885  * fleeing.\n
886  *\n
887  * Return TRUE if a safe location is available.\n
888  */
889 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
890 {
891         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
892
893         POSITION fy = m_ptr->fy;
894         POSITION fx = m_ptr->fx;
895
896         POSITION y, x, dy, dx, d, dis, i;
897         POSITION gy = 0, gx = 0, gdis = 0;
898
899         POSITION *y_offsets;
900         POSITION *x_offsets;
901
902         grid_type *g_ptr;
903
904         /* Start with adjacent locations, spread further */
905         for (d = 1; d < 10; d++)
906         {
907                 /* Get the lists of points with a distance d from (fx, fy) */
908                 y_offsets = dist_offsets_y[d];
909                 x_offsets = dist_offsets_x[d];
910
911                 /* Check the locations */
912                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
913                      dx != 0 || dy != 0;
914                      i++, dx = x_offsets[i], dy = y_offsets[i])
915                 {
916                         y = fy + dy;
917                         x = fx + dx;
918
919                         /* Skip illegal locations */
920                         if (!in_bounds(y, x)) continue;
921
922                         g_ptr = &current_floor_ptr->grid_array[y][x];
923
924                         /* Skip locations in a wall */
925                         if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
926
927                         /* Check for "availability" (if monsters can flow) */
928                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
929                         {
930                                 /* Ignore grids very far from the player */
931                                 if (g_ptr->dist == 0) continue;
932
933                                 /* Ignore too-distant grids */
934                                 if (g_ptr->dist > current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
935                         }
936
937                         /* Check for absence of shot (more or less) */
938                         if (!projectable(p_ptr->y, p_ptr->x, y, x))
939                         {
940                                 /* Calculate distance from player */
941                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
942
943                                 /* Remember if further than previous */
944                                 if (dis > gdis)
945                                 {
946                                         gy = y;
947                                         gx = x;
948                                         gdis = dis;
949                                 }
950                         }
951                 }
952
953                 /* Check for success */
954                 if (gdis > 0)
955                 {
956                         /* Good location */
957                         (*yp) = fy - gy;
958                         (*xp) = fx - gx;
959
960                         /* Found safe place */
961                         return (TRUE);
962                 }
963         }
964
965         /* No safe place */
966         return (FALSE);
967 }
968
969
970 /*!
971  * @brief モンスターが隠れ潜める地点を返す /
972  * Choose a good hiding place near a monster for it to run toward.
973  * @param m_idx モンスターの参照ID
974  * @param yp 移動先のマスのY座標を返す参照ポインタ
975  * @param xp 移動先のマスのX座標を返す参照ポインタ
976  * @return 有効なマスがあった場合TRUEを返す
977  * @details
978  * Pack monsters will use this to "ambush" the player and lure him out\n
979  * of corridors into open space so they can swarm him.\n
980  *\n
981  * Return TRUE if a good location is available.\n
982  */
983 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
984 {
985         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
986         monster_race *r_ptr = &r_info[m_ptr->r_idx];
987
988         POSITION fy = m_ptr->fy;
989         POSITION fx = m_ptr->fx;
990
991         POSITION y, x, dy, dx, d, dis, i;
992         POSITION gy = 0, gx = 0, gdis = 999;
993
994         POSITION *y_offsets, *x_offsets;
995
996         /* Start with adjacent locations, spread further */
997         for (d = 1; d < 10; d++)
998         {
999                 /* Get the lists of points with a distance d from (fx, fy) */
1000                 y_offsets = dist_offsets_y[d];
1001                 x_offsets = dist_offsets_x[d];
1002
1003                 /* Check the locations */
1004                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
1005                      dx != 0 || dy != 0;
1006                      i++, dx = x_offsets[i], dy = y_offsets[i])
1007                 {
1008                         y = fy + dy;
1009                         x = fx + dx;
1010
1011                         /* Skip illegal locations */
1012                         if (!in_bounds(y, x)) continue;
1013
1014                         /* Skip occupied locations */
1015                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
1016
1017                         /* Check for hidden, available grid */
1018                         if (!projectable(p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
1019                         {
1020                                 /* Calculate distance from player */
1021                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
1022
1023                                 /* Remember if closer than previous */
1024                                 if (dis < gdis && dis >= 2)
1025                                 {
1026                                         gy = y;
1027                                         gx = x;
1028                                         gdis = dis;
1029                                 }
1030                         }
1031                 }
1032
1033                 /* Check for success */
1034                 if (gdis < 999)
1035                 {
1036                         /* Good location */
1037                         (*yp) = fy - gy;
1038                         (*xp) = fx - gx;
1039
1040                         /* Found good place */
1041                         return (TRUE);
1042                 }
1043         }
1044
1045         /* No good place */
1046         return (FALSE);
1047 }
1048
1049
1050 /*!
1051  * @brief モンスターの移動方向を返す /
1052  * Choose "logical" directions for monster movement
1053  * @param m_idx モンスターの参照ID
1054  * @param mm 移動方向を返す方向IDの参照ポインタ
1055  * @return 有効方向があった場合TRUEを返す
1056  */
1057 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
1058 {
1059         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
1060         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1061         POSITION     y = 0, ay, x = 0, ax;
1062         int          move_val = 0;
1063         POSITION     y2 = p_ptr->y;
1064         POSITION     x2 = p_ptr->x;
1065         bool         done = FALSE;
1066         bool         will_run = mon_will_run(m_idx);
1067         grid_type    *g_ptr;
1068         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
1069         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
1070
1071         /* Counter attack to an enemy monster */
1072         if (!will_run && m_ptr->target_y)
1073         {
1074                 int t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1075
1076                 /* The monster must be an enemy, and in LOS */
1077                 if (t_m_idx &&
1078                     are_enemies(m_ptr, &current_floor_ptr->m_list[t_m_idx]) &&
1079                     los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
1080                     projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1081                 {
1082                         /* Extract the "pseudo-direction" */
1083                         y = m_ptr->fy - m_ptr->target_y;
1084                         x = m_ptr->fx - m_ptr->target_x;
1085                         done = TRUE;
1086                 }
1087         }
1088
1089         if (!done && !will_run && is_hostile(m_ptr) &&
1090             (r_ptr->flags1 & RF1_FRIENDS) &&
1091             ((los(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x)) ||
1092             (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
1093         {
1094         /*
1095          * Animal packs try to get the player out of corridors
1096          * (...unless they can move through walls -- TY)
1097          */
1098                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
1099                          !(r_ptr->flags2 & RF2_KILL_WALL))
1100                 {
1101                         int i, room = 0;
1102
1103                         /* Count room grids next to player */
1104                         for (i = 0; i < 8; i++)
1105                         {
1106                                 int xx = p_ptr->x + ddx_ddd[i];
1107                                 int yy = p_ptr->y + ddy_ddd[i];
1108
1109                                 if (!in_bounds2(yy, xx)) continue;
1110
1111                                 g_ptr = &current_floor_ptr->grid_array[yy][xx];
1112
1113                                 /* Check grid */
1114                                 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
1115                                 {
1116                                         /* One more room grid */
1117                                         room++;
1118                                 }
1119                         }
1120                         if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
1121                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
1122
1123                         /* Not in a room and strong player */
1124                         if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
1125                             (p_ptr->mhp + p_ptr->msp))
1126                         {
1127                                 /* Find hiding place */
1128                                 if (find_hiding(m_idx, &y, &x)) done = TRUE;
1129                         }
1130                 }
1131
1132                 /* Monster groups try to surround the player */
1133                 if (!done && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
1134                 {
1135                         int i;
1136
1137                         /* Find an empty square near the player to fill */
1138                         for (i = 0; i < 8; i++)
1139                         {
1140                                 /* Pick squares near player (semi-randomly) */
1141                                 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
1142                                 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
1143
1144                                 /* Already there? */
1145                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
1146                                 {
1147                                         /* Attack the player */
1148                                         y2 = p_ptr->y;
1149                                         x2 = p_ptr->x;
1150
1151                                         break;
1152                                 }
1153
1154                                 if (!in_bounds2(y2, x2)) continue;
1155
1156                                 /* Ignore filled grids */
1157                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
1158
1159                                 /* Try to fill this hole */
1160                                 break;
1161                         }
1162
1163                         /* Extract the new "pseudo-direction" */
1164                         y = m_ptr->fy - y2;
1165                         x = m_ptr->fx - x2;
1166
1167                         done = TRUE;
1168                 }
1169         }
1170
1171         if (!done)
1172         {
1173                 /* Flow towards the player */
1174                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
1175
1176                 /* Extract the "pseudo-direction" */
1177                 y = m_ptr->fy - y2;
1178                 x = m_ptr->fx - x2;
1179
1180                 /* Not done */
1181         }
1182
1183         /* Apply fear if possible and necessary */
1184         if (is_pet(m_ptr) && will_run)
1185         {
1186                 /* XXX XXX Not very "smart" */
1187                 y = (-y), x = (-x);
1188         }
1189         else
1190         {
1191                 if (!done && will_run)
1192                 {
1193                         int tmp_x = (-x);
1194                         int tmp_y = (-y);
1195
1196                         /* Try to find safe place */
1197                         if (find_safety(m_idx, &y, &x))
1198                         {
1199                                 /* Attempt to avoid the player */
1200                                 if (!no_flow)
1201                                 {
1202                                         /* Adjust movement */
1203                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1204                                 }
1205                         }
1206
1207                         if (!done)
1208                         {
1209                                 /* This is not a very "smart" method XXX XXX */
1210                                 y = tmp_y;
1211                                 x = tmp_x;
1212                         }
1213                 }
1214         }
1215
1216
1217         /* Check for no move */
1218         if (!x && !y) return (FALSE);
1219
1220
1221         /* Extract the "absolute distances" */
1222         ax = ABS(x);
1223         ay = ABS(y);
1224
1225         /* Do something weird */
1226         if (y < 0) move_val += 8;
1227         if (x > 0) move_val += 4;
1228
1229         /* Prevent the diamond maneuvre */
1230         if (ay > (ax << 1)) move_val += 2;
1231         else if (ax > (ay << 1)) move_val++;
1232
1233         /* Extract some directions */
1234         switch (move_val)
1235         {
1236         case 0:
1237                 mm[0] = 9;
1238                 if (ay > ax)
1239                 {
1240                         mm[1] = 8;
1241                         mm[2] = 6;
1242                         mm[3] = 7;
1243                         mm[4] = 3;
1244                 }
1245                 else
1246                 {
1247                         mm[1] = 6;
1248                         mm[2] = 8;
1249                         mm[3] = 3;
1250                         mm[4] = 7;
1251                 }
1252                 break;
1253         case 1:
1254         case 9:
1255                 mm[0] = 6;
1256                 if (y < 0)
1257                 {
1258                         mm[1] = 3;
1259                         mm[2] = 9;
1260                         mm[3] = 2;
1261                         mm[4] = 8;
1262                 }
1263                 else
1264                 {
1265                         mm[1] = 9;
1266                         mm[2] = 3;
1267                         mm[3] = 8;
1268                         mm[4] = 2;
1269                 }
1270                 break;
1271         case 2:
1272         case 6:
1273                 mm[0] = 8;
1274                 if (x < 0)
1275                 {
1276                         mm[1] = 9;
1277                         mm[2] = 7;
1278                         mm[3] = 6;
1279                         mm[4] = 4;
1280                 }
1281                 else
1282                 {
1283                         mm[1] = 7;
1284                         mm[2] = 9;
1285                         mm[3] = 4;
1286                         mm[4] = 6;
1287                 }
1288                 break;
1289         case 4:
1290                 mm[0] = 7;
1291                 if (ay > ax)
1292                 {
1293                         mm[1] = 8;
1294                         mm[2] = 4;
1295                         mm[3] = 9;
1296                         mm[4] = 1;
1297                 }
1298                 else
1299                 {
1300                         mm[1] = 4;
1301                         mm[2] = 8;
1302                         mm[3] = 1;
1303                         mm[4] = 9;
1304                 }
1305                 break;
1306         case 5:
1307         case 13:
1308                 mm[0] = 4;
1309                 if (y < 0)
1310                 {
1311                         mm[1] = 1;
1312                         mm[2] = 7;
1313                         mm[3] = 2;
1314                         mm[4] = 8;
1315                 }
1316                 else
1317                 {
1318                         mm[1] = 7;
1319                         mm[2] = 1;
1320                         mm[3] = 8;
1321                         mm[4] = 2;
1322                 }
1323                 break;
1324         case 8:
1325                 mm[0] = 3;
1326                 if (ay > ax)
1327                 {
1328                         mm[1] = 2;
1329                         mm[2] = 6;
1330                         mm[3] = 1;
1331                         mm[4] = 9;
1332                 }
1333                 else
1334                 {
1335                         mm[1] = 6;
1336                         mm[2] = 2;
1337                         mm[3] = 9;
1338                         mm[4] = 1;
1339                 }
1340                 break;
1341         case 10:
1342         case 14:
1343                 mm[0] = 2;
1344                 if (x < 0)
1345                 {
1346                         mm[1] = 3;
1347                         mm[2] = 1;
1348                         mm[3] = 6;
1349                         mm[4] = 4;
1350                 }
1351                 else
1352                 {
1353                         mm[1] = 1;
1354                         mm[2] = 3;
1355                         mm[3] = 4;
1356                         mm[4] = 6;
1357                 }
1358                 break;
1359         case 12:
1360                 mm[0] = 1;
1361                 if (ay > ax)
1362                 {
1363                         mm[1] = 2;
1364                         mm[2] = 4;
1365                         mm[3] = 3;
1366                         mm[4] = 7;
1367                 }
1368                 else
1369                 {
1370                         mm[1] = 4;
1371                         mm[2] = 2;
1372                         mm[3] = 7;
1373                         mm[4] = 3;
1374                 }
1375                 break;
1376         }
1377
1378         /* Wants to move... */
1379         return (TRUE);
1380 }
1381
1382
1383 /*!
1384  * @brief モンスターから敵モンスターへの命中判定
1385  * @param power 打撃属性による基本命中値
1386  * @param level 攻撃側モンスターのレベル
1387  * @param ac 目標モンスターのAC
1388  * @param stun 攻撃側モンスターが朦朧状態ならTRUEを返す
1389  * @return 命中ならばTRUEを返す
1390  */
1391 static int check_hit2(int power, DEPTH level, ARMOUR_CLASS ac, int stun)
1392 {
1393         int i, k;
1394
1395         /* Percentile dice */
1396         k = randint0(100);
1397
1398         if (stun && one_in_(2)) return FALSE;
1399
1400         /* Hack -- Always miss or hit */
1401         if (k < 10) return (k < 5);
1402
1403         /* Calculate the "attack quality" */
1404         i = (power + (level * 3));
1405
1406         /* Power and Level compete against Armor */
1407         if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1408
1409         /* Assume miss */
1410         return (FALSE);
1411 }
1412
1413
1414 #define BLOW_EFFECT_TYPE_NONE  0
1415 #define BLOW_EFFECT_TYPE_FEAR  1
1416 #define BLOW_EFFECT_TYPE_SLEEP 2
1417 #define BLOW_EFFECT_TYPE_HEAL  3
1418
1419
1420 /*!
1421  * @brief モンスターから敵モンスターへの打撃攻撃処理
1422  * @param m_idx 攻撃側モンスターの参照ID
1423  * @param t_idx 目標側モンスターの参照ID
1424  * @return 実際に打撃処理が行われた場合TRUEを返す
1425  */
1426 static bool monst_attack_monst(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
1427 {
1428         monster_type    *m_ptr = &current_floor_ptr->m_list[m_idx];
1429         monster_type    *t_ptr = &current_floor_ptr->m_list[t_idx];
1430
1431         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1432         monster_race    *tr_ptr = &r_info[t_ptr->r_idx];
1433
1434         ARMOUR_CLASS ap_cnt;
1435         ARMOUR_CLASS ac;
1436         DEPTH rlev;
1437         int pt;
1438         GAME_TEXT m_name[MAX_NLEN], t_name[MAX_NLEN];
1439         char            temp[MAX_NLEN];
1440         bool            blinked;
1441         bool            explode = FALSE, touched = FALSE, fear = FALSE, dead = FALSE;
1442         POSITION y_saver = t_ptr->fy;
1443         POSITION x_saver = t_ptr->fx;
1444         int             effect_type;
1445
1446         bool see_m = is_seen(m_ptr);
1447         bool see_t = is_seen(t_ptr);
1448         bool see_either = see_m || see_t;
1449
1450         /* Can the player be aware of this attack? */
1451         bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1452         bool do_silly_attack = (one_in_(2) && p_ptr->image);
1453
1454         /* Cannot attack self */
1455         if (m_idx == t_idx) return FALSE;
1456
1457         /* Not allowed to attack */
1458         if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1459
1460         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE) return (FALSE);
1461
1462         /* Total armor */
1463         ac = tr_ptr->ac;
1464
1465         /* Extract the effective monster level */
1466         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1467
1468         monster_desc(m_name, m_ptr, 0);
1469         monster_desc(t_name, t_ptr, 0);
1470
1471         /* Assume no blink */
1472         blinked = FALSE;
1473
1474         if (!see_either && known)
1475         {
1476                 mon_fight = TRUE;
1477         }
1478
1479         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
1480
1481         /* Scan through all four blows */
1482         for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1483         {
1484                 bool obvious = FALSE;
1485
1486                 HIT_POINT power = 0;
1487                 HIT_POINT damage = 0;
1488
1489                 concptr act = NULL;
1490
1491                 /* Extract the attack infomation */
1492                 int effect = r_ptr->blow[ap_cnt].effect;
1493                 int method = r_ptr->blow[ap_cnt].method;
1494                 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1495                 int d_side = r_ptr->blow[ap_cnt].d_side;
1496
1497                 if (!m_ptr->r_idx) break;
1498
1499                 /* Stop attacking if the target dies! */
1500                 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1501                         break;
1502
1503                 /* Hack -- no more attacks */
1504                 if (!method) break;
1505
1506                 if (method == RBM_SHOOT) continue;
1507
1508                 /* Extract the attack "power" */
1509                 power = mbe_info[effect].power;
1510
1511                 /* Monster hits */
1512                 if (!effect || check_hit2(power, rlev, ac, MON_STUNNED(m_ptr)))
1513                 {
1514                         (void)set_monster_csleep(t_idx, 0);
1515
1516                         if (t_ptr->ml)
1517                         {
1518                                 /* Redraw the health bar */
1519                                 if (p_ptr->health_who == t_idx) p_ptr->redraw |= (PR_HEALTH);
1520                                 if (p_ptr->riding == t_idx) p_ptr->redraw |= (PR_UHEALTH);
1521                         }
1522
1523                         /* Describe the attack method */
1524                         switch (method)
1525                         {
1526                         case RBM_HIT:
1527                                 {
1528                                         act = _("%sを殴った。", "hits %s.");
1529                                         touched = TRUE;
1530                                         break;
1531                                 }
1532
1533                         case RBM_TOUCH:
1534                                 {
1535                                         act = _("%sを触った。", "touches %s.");
1536                                         touched = TRUE;
1537                                         break;
1538                                 }
1539
1540                         case RBM_PUNCH:
1541                                 {
1542                                         act = _("%sをパンチした。", "punches %s.");
1543                                         touched = TRUE;
1544                                         break;
1545                                 }
1546
1547                         case RBM_KICK:
1548                                 {
1549                                         act = _("%sを蹴った。", "kicks %s.");
1550                                         touched = TRUE;
1551                                         break;
1552                                 }
1553
1554                         case RBM_CLAW:
1555                                 {
1556                                         act = _("%sをひっかいた。", "claws %s.");
1557                                         touched = TRUE;
1558                                         break;
1559                                 }
1560
1561                         case RBM_BITE:
1562                                 {
1563                                         act = _("%sを噛んだ。", "bites %s.");
1564                                         touched = TRUE;
1565                                         break;
1566                                 }
1567
1568                         case RBM_STING:
1569                                 {
1570                                         act = _("%sを刺した。", "stings %s.");
1571                                         touched = TRUE;
1572                                         break;
1573                                 }
1574
1575                         case RBM_SLASH:
1576                                 {
1577                                         act = _("%sを斬った。", "slashes %s.");
1578                                         break;
1579                                 }
1580
1581                         case RBM_BUTT:
1582                                 {
1583                                         act = _("%sを角で突いた。", "butts %s.");
1584                                         touched = TRUE;
1585                                         break;
1586                                 }
1587
1588                         case RBM_CRUSH:
1589                                 {
1590                                         act = _("%sに体当りした。", "crushes %s.");
1591                                         touched = TRUE;
1592                                         break;
1593                                 }
1594
1595                         case RBM_ENGULF:
1596                                 {
1597                                         act = _("%sを飲み込んだ。", "engulfs %s.");
1598                                         touched = TRUE;
1599                                         break;
1600                                 }
1601
1602                         case RBM_CHARGE:
1603                                 {
1604                                         act = _("%sに請求書をよこした。", "charges %s.");
1605                                         touched = TRUE;
1606                                         break;
1607                                 }
1608
1609                         case RBM_CRAWL:
1610                                 {
1611                                         act = _("%sの体の上を這い回った。", "crawls on %s.");
1612                                         touched = TRUE;
1613                                         break;
1614                                 }
1615
1616                         case RBM_DROOL:
1617                                 {
1618                                         act = _("%sによだれをたらした。", "drools on %s.");
1619                                         touched = FALSE;
1620                                         break;
1621                                 }
1622
1623                         case RBM_SPIT:
1624                                 {
1625                                         act = _("%sに唾を吐いた。", "spits on %s.");
1626                                         touched = FALSE;
1627                                         break;
1628                                 }
1629
1630                         case RBM_EXPLODE:
1631                                 {
1632                                         if (see_either) disturb(TRUE, TRUE);
1633                                         act = _("爆発した。", "explodes.");
1634                                         explode = TRUE;
1635                                         touched = FALSE;
1636                                         break;
1637                                 }
1638
1639                         case RBM_GAZE:
1640                                 {
1641                                         act = _("%sをにらんだ。", "gazes at %s.");
1642                                         touched = FALSE;
1643                                         break;
1644                                 }
1645
1646                         case RBM_WAIL:
1647                                 {
1648                                         act = _("%sに泣きついた。", "wails at %s.");
1649                                         touched = FALSE;
1650                                         break;
1651                                 }
1652
1653                         case RBM_SPORE:
1654                                 {
1655                                         act = _("%sに胞子を飛ばした。", "releases spores at %s.");
1656                                         touched = FALSE;
1657                                         break;
1658                                 }
1659
1660                         case RBM_XXX4:
1661                                 {
1662                                         act = _("%sにXXX4を飛ばした。", "projects XXX4's at %s.");
1663                                         touched = FALSE;
1664                                         break;
1665                                 }
1666
1667                         case RBM_BEG:
1668                                 {
1669                                         act = _("%sに金をせがんだ。", "begs %s for money.");
1670                                         touched = FALSE;
1671                                         break;
1672                                 }
1673
1674                         case RBM_INSULT:
1675                                 {
1676                                         act = _("%sを侮辱した。", "insults %s.");
1677                                         touched = FALSE;
1678                                         break;
1679                                 }
1680
1681                         case RBM_MOAN:
1682                                 {
1683                                         act = _("%sにむかってうめいた。", "moans at %s.");
1684                                         touched = FALSE;
1685                                         break;
1686                                 }
1687
1688                         case RBM_SHOW:
1689                                 {
1690                                         act = _("%sにむかって歌った。", "sings to %s.");
1691                                         touched = FALSE;
1692                                         break;
1693                                 }
1694                         }
1695
1696                         if (act && see_either)
1697                         {
1698 #ifdef JP
1699                                 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1700                                 strfmt(temp, act, t_name);
1701                                 msg_format("%^sは%s", m_name, temp);
1702 #else
1703                                 if (do_silly_attack)
1704                                 {
1705                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1706                                         strfmt(temp, "%s %s.", act, t_name);
1707                                 }
1708                                 else strfmt(temp, act, t_name);
1709                                 msg_format("%^s %s", m_name, temp);
1710 #endif
1711                         }
1712
1713                         /* Hack -- assume all attacks are obvious */
1714                         obvious = TRUE;
1715
1716                         /* Roll out the damage */
1717                         damage = damroll(d_dice, d_side);
1718
1719                         /* Assume no effect */
1720                         effect_type = BLOW_EFFECT_TYPE_NONE;
1721
1722                         pt = GF_MISSILE;
1723
1724                         /* Apply appropriate damage */
1725                         switch (effect)
1726                         {
1727                         case 0:
1728                         case RBE_DR_MANA:
1729                                 damage = pt = 0;
1730                                 break;
1731
1732                         case RBE_SUPERHURT:
1733                                 if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13))
1734                                 {
1735                                         int tmp_damage = damage - (damage * ((ac < 150) ? ac : 150) / 250);
1736                                         damage = MAX(damage, tmp_damage * 2);
1737                                         break;
1738                                 }
1739
1740                                 /* Fall through */
1741
1742                         case RBE_HURT:
1743                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1744                                 break;
1745
1746                         case RBE_POISON:
1747                         case RBE_DISEASE:
1748                                 pt = GF_POIS;
1749                                 break;
1750
1751                         case RBE_UN_BONUS:
1752                         case RBE_UN_POWER:
1753                                 pt = GF_DISENCHANT;
1754                                 break;
1755
1756                         case RBE_EAT_ITEM:
1757                         case RBE_EAT_GOLD:
1758                                 if ((p_ptr->riding != m_idx) && one_in_(2)) blinked = TRUE;
1759                                 break;
1760
1761                         case RBE_EAT_FOOD:
1762                         case RBE_EAT_LITE:
1763                         case RBE_BLIND:
1764                         case RBE_LOSE_STR:
1765                         case RBE_LOSE_INT:
1766                         case RBE_LOSE_WIS:
1767                         case RBE_LOSE_DEX:
1768                         case RBE_LOSE_CON:
1769                         case RBE_LOSE_CHR:
1770                         case RBE_LOSE_ALL:
1771                                 break;
1772
1773                         case RBE_ACID:
1774                                 pt = GF_ACID;
1775                                 break;
1776
1777                         case RBE_ELEC:
1778                                 pt = GF_ELEC;
1779                                 break;
1780
1781                         case RBE_FIRE:
1782                                 pt = GF_FIRE;
1783                                 break;
1784
1785                         case RBE_COLD:
1786                                 pt = GF_COLD;
1787                                 break;
1788
1789                         case RBE_CONFUSE:
1790                                 pt = GF_CONFUSION;
1791                                 break;
1792
1793                         case RBE_TERRIFY:
1794                                 effect_type = BLOW_EFFECT_TYPE_FEAR;
1795                                 break;
1796
1797                         case RBE_PARALYZE:
1798                                 effect_type = BLOW_EFFECT_TYPE_SLEEP;
1799                                 break;
1800
1801                         case RBE_SHATTER:
1802                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1803                                 if (damage > 23) earthquake_aux(m_ptr->fy, m_ptr->fx, 8, m_idx);
1804                                 break;
1805
1806                         case RBE_EXP_10:
1807                         case RBE_EXP_20:
1808                         case RBE_EXP_40:
1809                         case RBE_EXP_80:
1810                                 pt = GF_NETHER;
1811                                 break;
1812
1813                         case RBE_TIME:
1814                                 pt = GF_TIME;
1815                                 break;
1816
1817                         case RBE_DR_LIFE:
1818                                 pt = GF_HYPODYNAMIA;
1819                                 effect_type = BLOW_EFFECT_TYPE_HEAL;
1820                                 break;
1821
1822                         case RBE_INERTIA:
1823                                 pt = GF_INERTIAL;
1824                                 break;
1825
1826                         case RBE_STUN:
1827                                 pt = GF_SOUND;
1828                                 break;
1829
1830                         default:
1831                                 pt = 0;
1832                                 break;
1833                         }
1834
1835                         if (pt)
1836                         {
1837                                 /* Do damage if not exploding */
1838                                 if (!explode)
1839                                 {
1840                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1841                                                 damage, pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1842                                 }
1843
1844                                 switch (effect_type)
1845                                 {
1846                                 case BLOW_EFFECT_TYPE_FEAR:
1847                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1848                                                 damage, GF_TURN_ALL, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1849                                         break;
1850
1851                                 case BLOW_EFFECT_TYPE_SLEEP:
1852                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1853                                                 r_ptr->level, GF_OLD_SLEEP, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1854                                         break;
1855
1856                                 case BLOW_EFFECT_TYPE_HEAL:
1857                                         if ((monster_living(m_idx)) && (damage > 2))
1858                                         {
1859                                                 bool did_heal = FALSE;
1860
1861                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
1862
1863                                                 /* Heal */
1864                                                 m_ptr->hp += damroll(4, damage / 6);
1865                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
1866
1867                                                 /* Redraw (later) if needed */
1868                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
1869                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
1870
1871                                                 /* Special message */
1872                                                 if (see_m && did_heal)
1873                                                 {
1874                                                         msg_format(_("%sは体力を回復したようだ。", "%^s appears healthier."), m_name);
1875                                                 }
1876                                         }
1877                                         break;
1878                                 }
1879
1880                                 if (touched)
1881                                 {
1882                                         /* Aura fire */
1883                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
1884                                         {
1885                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
1886                                                 {
1887                                                         if (see_either)
1888                                                         {
1889                                                                 msg_format(_("%^sは突然熱くなった!", "%^s is suddenly very hot!"), m_name);
1890                                                         }
1891                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
1892                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1893                                                                 damroll (1 + ((tr_ptr->level) / 26),
1894                                                                 1 + ((tr_ptr->level) / 17)),
1895                                                                 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1896                                                 }
1897                                                 else
1898                                                 {
1899                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
1900                                                 }
1901                                         }
1902
1903                                         /* Aura cold */
1904                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
1905                                         {
1906                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
1907                                                 {
1908                                                         if (see_either)
1909                                                         {
1910                                                                 msg_format(_("%^sは突然寒くなった!", "%^s is suddenly very cold!"), m_name);
1911                                                         }
1912                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
1913                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1914                                                                 damroll (1 + ((tr_ptr->level) / 26),
1915                                                                 1 + ((tr_ptr->level) / 17)),
1916                                                                 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1917                                                 }
1918                                                 else
1919                                                 {
1920                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
1921                                                 }
1922                                         }
1923
1924                                         /* Aura elec */
1925                                         if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
1926                                         {
1927                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
1928                                                 {
1929                                                         if (see_either)
1930                                                         {
1931                                                                 msg_format(_("%^sは電撃を食らった!", "%^s gets zapped!"), m_name);
1932                                                         }
1933                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
1934                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1935                                                                 damroll (1 + ((tr_ptr->level) / 26),
1936                                                                 1 + ((tr_ptr->level) / 17)),
1937                                                                 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1938                                                 }
1939                                                 else
1940                                                 {
1941                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
1942                                                 }
1943                                         }
1944                                 }
1945                         }
1946                 }
1947
1948                 /* Monster missed player */
1949                 else
1950                 {
1951                         /* Analyze failed attacks */
1952                         switch (method)
1953                         {
1954                         case RBM_HIT:
1955                         case RBM_TOUCH:
1956                         case RBM_PUNCH:
1957                         case RBM_KICK:
1958                         case RBM_CLAW:
1959                         case RBM_BITE:
1960                         case RBM_STING:
1961                         case RBM_SLASH:
1962                         case RBM_BUTT:
1963                         case RBM_CRUSH:
1964                         case RBM_ENGULF:
1965                         case RBM_CHARGE:
1966                                 {
1967                                         (void)set_monster_csleep(t_idx, 0);
1968
1969                                         /* Visible monsters */
1970                                         if (see_m)
1971                                         {
1972 #ifdef JP
1973                                                 msg_format("%sは%^sの攻撃をかわした。", t_name, m_name);
1974 #else
1975                                                 msg_format("%^s misses %s.", m_name, t_name);
1976 #endif
1977                                         }
1978
1979                                         break;
1980                                 }
1981                         }
1982                 }
1983
1984
1985                 /* Analyze "visible" monsters only */
1986                 if (is_original_ap_and_seen(m_ptr) && !do_silly_attack)
1987                 {
1988                         /* Count "obvious" attacks (and ones that cause damage) */
1989                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
1990                         {
1991                                 /* Count attacks of this type */
1992                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
1993                                 {
1994                                         r_ptr->r_blows[ap_cnt]++;
1995                                 }
1996                         }
1997                 }
1998         }
1999
2000         if (explode)
2001         {
2002                 sound(SOUND_EXPLODE);
2003
2004                 /* Cancel Invulnerability */
2005                 (void)set_monster_invulner(m_idx, 0, FALSE);
2006                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2007                 blinked = FALSE;
2008         }
2009
2010         /* Blink away */
2011         if (blinked && m_ptr->r_idx)
2012         {
2013                 if (teleport_barrier(m_idx))
2014                 {
2015                         if (see_m)
2016                         {
2017                                 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But magic barrier obstructs it."));
2018                         }
2019                         else if (known)
2020                         {
2021                                 mon_fight = TRUE;
2022                         }
2023                 }
2024                 else
2025                 {
2026                         if (see_m)
2027                         {
2028                                 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
2029                         }
2030                         else if (known)
2031                         {
2032                                 mon_fight = TRUE;
2033                         }
2034
2035                         teleport_away(m_idx, MAX_SIGHT * 2 + 5, 0L);
2036                 }
2037         }
2038
2039         return TRUE;
2040 }
2041
2042
2043 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
2044 {
2045         return !have_flag(f_ptr->flags, FF_GLASS) ||
2046                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
2047                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
2048 }
2049
2050
2051 /*!
2052  * @brief モンスター単体の1ターン行動処理メインルーチン /
2053  * Process a monster
2054  * @param m_idx 行動モンスターの参照ID
2055  * @return なし
2056  * @details
2057  * The monster is known to be within 100 grids of the player\n
2058  *\n
2059  * In several cases, we directly update the monster lore\n
2060  *\n
2061  * Note that a monster is only allowed to "reproduce" if there\n
2062  * are a limited number of "reproducing" monsters on the current\n
2063  * level.  This should prevent the level from being "swamped" by\n
2064  * reproducing monsters.  It also allows a large mass of mice to\n
2065  * prevent a louse from multiplying, but this is a small price to\n
2066  * pay for a simple multiplication method.\n
2067  *\n
2068  * XXX Monster fear is slightly odd, in particular, monsters will\n
2069  * fixate on opening a door even if they cannot open it.  Actually,\n
2070  * the same thing happens to normal monsters when they hit a door\n
2071  *\n
2072  * In addition, monsters which *cannot* open or bash\n
2073  * down a door will still stand there trying to open it...\n
2074  *\n
2075  * XXX Technically, need to check for monster in the way\n
2076  * combined with that monster being in a wall (or door?)\n
2077  *\n
2078  * A "direction" of "5" means "pick a random direction".\n
2079  */
2080 void process_monster(MONSTER_IDX m_idx)
2081 {
2082         monster_type    *m_ptr = &current_floor_ptr->m_list[m_idx];
2083         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
2084         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2085
2086         int i, d;
2087         POSITION oy, ox, ny, nx;
2088
2089         DIRECTION mm[8];
2090
2091         grid_type       *g_ptr;
2092         feature_type    *f_ptr;
2093
2094         monster_type    *y_ptr;
2095
2096         bool            do_turn;
2097         bool            do_move;
2098         bool            do_view;
2099         bool            must_alter_to_move;
2100
2101         bool            did_open_door;
2102         bool            did_bash_door;
2103         bool            did_take_item;
2104         bool            did_kill_item;
2105         bool            did_move_body;
2106         bool            did_pass_wall;
2107         bool            did_kill_wall;
2108         bool            gets_angry = FALSE;
2109         bool            can_cross;
2110         bool            aware = TRUE;
2111
2112         bool fear, dead;
2113         bool is_riding_mon = (m_idx == p_ptr->riding);
2114         bool see_m = is_seen(m_ptr);
2115
2116         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
2117         {
2118                 if (rakuba(0, TRUE))
2119                 {
2120 #ifdef JP
2121                         msg_print("地面に落とされた。");
2122 #else
2123                         GAME_TEXT m_name[MAX_NLEN];
2124                         monster_desc(m_name, &current_floor_ptr->m_list[p_ptr->riding], 0);
2125                         msg_format("You have fallen from %s.", m_name);
2126 #endif
2127                 }
2128         }
2129
2130         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
2131         {
2132                 choose_new_monster(m_idx, FALSE, 0);
2133                 r_ptr = &r_info[m_ptr->r_idx];
2134         }
2135
2136         /* Players hidden in shadow are almost imperceptable. -LM- */
2137         if (p_ptr->special_defense & NINJA_S_STEALTH)
2138         {
2139                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
2140                 if (p_ptr->monlite) tmp /= 3;
2141                 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
2142                 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
2143                 /* Low-level monsters will find it difficult to locate the player. */
2144                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
2145         }
2146
2147         /* Are there its parent? */
2148         if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
2149         {
2150                 /* Its parent have gone, it also goes away. */
2151
2152                 if (see_m)
2153                 {
2154                         GAME_TEXT m_name[MAX_NLEN];
2155                         monster_desc(m_name, m_ptr, 0);
2156                         msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2157                 }
2158
2159                 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2160                 {
2161                         GAME_TEXT m_name[MAX_NLEN];
2162                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2163                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2164                 }
2165
2166                 delete_monster_idx(m_idx);
2167
2168                 return;
2169         }
2170
2171         /* Quantum monsters are odd */
2172         if (r_ptr->flags2 & (RF2_QUANTUM))
2173         {
2174                 /* Sometimes skip move */
2175                 if (!randint0(2)) return;
2176
2177                 /* Sometimes die */
2178                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
2179                 {
2180                         bool sad = FALSE;
2181
2182                         if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
2183
2184                         if (see_m)
2185                         {
2186                                 GAME_TEXT m_name[MAX_NLEN];
2187                                 monster_desc(m_name, m_ptr, 0);
2188
2189                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2190                         }
2191
2192                         /* Generate treasure, etc */
2193                         monster_death(m_idx, FALSE);
2194
2195                         delete_monster_idx(m_idx);
2196                         if (sad)
2197                         {
2198                                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2199                         }
2200
2201                         return;
2202                 }
2203         }
2204
2205         if (m_ptr->r_idx == MON_SHURYUUDAN)
2206         {
2207                 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2208                 if(dead) return;
2209         }
2210
2211         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2212         {
2213                 static int riding_pinch = 0;
2214
2215                 if (m_ptr->hp < m_ptr->maxhp/3)
2216                 {
2217                         GAME_TEXT m_name[MAX_NLEN];
2218                         monster_desc(m_name, m_ptr, 0);
2219
2220                         if (is_riding_mon && riding_pinch < 2)
2221                         {
2222                                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2223                                                          "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
2224                                 riding_pinch++;
2225                                 disturb(TRUE, TRUE);
2226                         }
2227                         else
2228                         {
2229                                 if (is_riding_mon)
2230                                 {
2231                                         msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2232                                         if (rakuba(-1, FALSE))
2233                                         {
2234                                                 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2235                                         }
2236                                 }
2237
2238                                 if (see_m)
2239                                 {
2240                                         if ((r_ptr->flags2 & RF2_CAN_SPEAK) && (m_ptr->r_idx != MON_GRIP) && (m_ptr->r_idx != MON_WOLF) && (m_ptr->r_idx != MON_FANG) &&
2241                                             player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))
2242                                         {
2243                                                 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2244                                         }
2245                                         msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
2246                                         msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2247                                 }
2248
2249                                 if (is_riding_mon && rakuba(-1, FALSE))
2250                                 {
2251                                         msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2252                                 }
2253
2254                                 /* Check for quest completion */
2255                                 check_quest_completion(m_ptr);
2256
2257                                 delete_monster_idx(m_idx);
2258
2259                                 return;
2260                         }
2261                 }
2262                 else
2263                 {
2264                         /* Reset the counter */
2265                         if (is_riding_mon) riding_pinch = 0;
2266                 }
2267         }
2268
2269         /* Handle "sleep" */
2270         if (MON_CSLEEP(m_ptr))
2271         {
2272                 /* Handle non-aggravation - Still sleeping */
2273                 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2274
2275                 /* Reset sleep counter */
2276                 (void)set_monster_csleep(m_idx, 0);
2277
2278                 /* Notice the "waking up" */
2279                 if (m_ptr->ml)
2280                 {
2281                         GAME_TEXT m_name[MAX_NLEN];
2282                         monster_desc(m_name, m_ptr, 0);
2283                         msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2284                 }
2285
2286                 /* Hack -- Count the wakings */
2287                 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2288                 {
2289                         r_ptr->r_wake++;
2290                 }
2291         }
2292
2293         /* Handle "stun" */
2294         if (MON_STUNNED(m_ptr))
2295         {
2296                 /* Sometimes skip move */
2297                 if (one_in_(2)) return;
2298         }
2299
2300         if (is_riding_mon)
2301         {
2302                 p_ptr->update |= (PU_BONUS);
2303         }
2304
2305         /* No one wants to be your friend if you're aggravating */
2306         if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2307                 gets_angry = TRUE;
2308
2309         /* Paranoia... no pet uniques outside wizard mode -- TY */
2310         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2311               monster_has_hostile_align(NULL, 10, -10, r_ptr))
2312              || (r_ptr->flagsr & RFR_RES_ALL)))
2313         {
2314                 gets_angry = TRUE;
2315         }
2316
2317         if (p_ptr->inside_battle) gets_angry = FALSE;
2318
2319         if (gets_angry)
2320         {
2321                 if (is_pet(m_ptr) || see_m)
2322                 {
2323                         GAME_TEXT m_name[MAX_NLEN];
2324                         monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2325                         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2326                 }
2327
2328                 set_hostile(m_ptr);
2329         }
2330
2331         /* Get the origin */
2332         oy = m_ptr->fy;
2333         ox = m_ptr->fx;
2334
2335         /* Attempt to "multiply" if able and allowed */
2336         if ((r_ptr->flags2 & RF2_MULTIPLY) && (current_floor_ptr->num_repro < MAX_REPRO))
2337         {
2338                 int k;
2339                 POSITION y, x;
2340
2341                 /* Count the adjacent monsters */
2342                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2343                 {
2344                         for (x = ox - 1; x <= ox + 1; x++)
2345                         {
2346                                 /* Ignore locations off of edge */
2347                                 if (!in_bounds2(y, x)) continue;
2348                                 if (current_floor_ptr->grid_array[y][x].m_idx) k++;
2349                         }
2350                 }
2351
2352                 /* Hex */
2353                 if (multiply_barrier(m_idx)) k = 8;
2354
2355                 /* Hack -- multiply slower in crowded areas */
2356                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2357                 {
2358                         /* Try to multiply */
2359                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2360                         {
2361                                 /* Take note if visible */
2362                                 if (current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
2363                                 {
2364                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2365                                 }
2366
2367                                 /* Multiplying takes energy */
2368                                 return;
2369                         }
2370                 }
2371         }
2372
2373         if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
2374         {
2375                 /* Hack -- Ohmu scatters molds! */
2376                 if (m_ptr->r_idx == MON_OHMU)
2377                 {
2378                         if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2379                         {
2380                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2381                                 {
2382                                         int k, count = 0;
2383                                         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2384                                         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2385
2386                                         for (k = 0; k < A_MAX; k++)
2387                                         {
2388                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode), '\0'))
2389                                                 {
2390                                                         if (current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2391                                                 }
2392                                         }
2393
2394                                         if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2395                                 }
2396                         }
2397                 }
2398         }
2399
2400         if (!p_ptr->inside_battle)
2401         {
2402                 /* Hack! "Cyber" monster makes noise... */
2403                 if (m_ptr->ap_r_idx == MON_CYBER &&
2404                     one_in_(CYBERNOISE) &&
2405                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2406                 {
2407                         if (disturb_minor) disturb(FALSE, FALSE);
2408                         msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2409                 }
2410
2411                 /* Some monsters can speak */
2412                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2413                     one_in_(SPEAK_CHANCE) &&
2414                     player_has_los_bold(oy, ox) &&
2415                     projectable(oy, ox, p_ptr->y, p_ptr->x))
2416                 {
2417                         GAME_TEXT m_name[MAX_NLEN];
2418                         char monmessage[1024];
2419                         concptr filename;
2420
2421                         /* Acquire the monster name/poss */
2422                         if (m_ptr->ml)
2423                                 monster_desc(m_name, m_ptr, 0);
2424                         else
2425                                 strcpy(m_name, _("それ", "It"));
2426
2427                         /* Select the file for monster quotes */
2428                         if (MON_MONFEAR(m_ptr))
2429                                 filename = _("monfear_j.txt", "monfear.txt");
2430                         else if (is_pet(m_ptr))
2431                                 filename = _("monpet_j.txt", "monpet.txt");
2432                         else if (is_friendly(m_ptr))
2433                                 filename = _("monfrien_j.txt", "monfrien.txt");
2434                         else
2435                                 filename = _("monspeak_j.txt", "monspeak.txt");
2436                         /* Get the monster line */
2437                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2438                         {
2439                                 /* Say something */
2440                                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2441                         }
2442                 }
2443         }
2444
2445         /* Try to cast spell occasionally */
2446         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2447         {
2448                 bool counterattack = FALSE;
2449
2450                 /* Give priority to counter attack? */
2451                 if (m_ptr->target_y)
2452                 {
2453                         MONSTER_IDX t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2454
2455                         /* The monster must be an enemy, and projectable */
2456                         if (t_m_idx && are_enemies(m_ptr, &current_floor_ptr->m_list[t_m_idx]) &&
2457                             projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2458                         {
2459                                 counterattack = TRUE;
2460                         }
2461                 }
2462
2463                 if (!counterattack)
2464                 {
2465                         /* Attempt to cast a spell */
2466                         if (aware && make_attack_spell(m_idx)) return;
2467
2468                         /*
2469                          * Attempt to cast a spell at an enemy other than the player
2470                          * (may slow the game a smidgeon, but I haven't noticed.)
2471                          */
2472                         if (monst_spell_monst(m_idx)) return;
2473                 }
2474                 else
2475                 {
2476                         /* Attempt to do counter attack at first */
2477                         if (monst_spell_monst(m_idx)) return;
2478
2479                         if (aware && make_attack_spell(m_idx)) return;
2480                 }
2481         }
2482
2483         /* Hack -- Assume no movement */
2484         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2485         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2486
2487
2488         /* Confused -- 100% random */
2489         if (MON_CONFUSED(m_ptr) || !aware)
2490         {
2491                 /* Try four "random" directions */
2492                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2493         }
2494
2495         /* 75% random movement */
2496         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2497                  (randint0(100) < 75))
2498         {
2499                 /* Memorize flags */
2500                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2501
2502                 /* Try four "random" directions */
2503                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2504         }
2505
2506         /* 50% random movement */
2507         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2508                                 (randint0(100) < 50))
2509         {
2510                 /* Memorize flags */
2511                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2512
2513                 /* Try four "random" directions */
2514                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2515         }
2516
2517         /* 25% random movement */
2518         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2519                                 (randint0(100) < 25))
2520         {
2521                 /* Memorize flags */
2522                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2523
2524                 /* Try four "random" directions */
2525                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2526         }
2527
2528         /* Can't reach player - find something else to hit */
2529         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2530         {
2531                 /* Try four "random" directions */
2532                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2533
2534                 /* Look for an enemy */
2535 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2536                 get_enemy_dir(m_idx, mm);
2537 #endif /* 0 */
2538         }
2539
2540         /* Pets will follow the player */
2541         else if (is_pet(m_ptr))
2542         {
2543                 /* Are we trying to avoid the player? */
2544                 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2545
2546                 /* Do we want to find the player? */
2547                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2548
2549                 /* Should we find the player if we can't find a monster? */
2550                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2551
2552                 /* by default, move randomly */
2553                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2554
2555                 /* Look for an enemy */
2556                 if (!get_enemy_dir(m_idx, mm))
2557                 {
2558                         /* Find the player if necessary */
2559                         if (avoid || lonely || distant)
2560                         {
2561                                 /* Remember the leash length */
2562                                 POSITION dis = p_ptr->pet_follow_distance;
2563
2564                                 /* Hack -- adjust follow distance temporarily */
2565                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2566                                 {
2567                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
2568                                 }
2569
2570                                 /* Find the player */
2571                                 (void)get_moves(m_idx, mm);
2572
2573                                 /* Restore the leash */
2574                                 p_ptr->pet_follow_distance = (s16b)dis;
2575                         }
2576                 }
2577         }
2578
2579         /* Friendly monster movement */
2580         else if (!is_hostile(m_ptr))
2581         {
2582                 /* by default, move randomly */
2583                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2584
2585                 /* Look for an enemy */
2586                 get_enemy_dir(m_idx, mm);
2587         }
2588         /* Normal movement */
2589         else
2590         {
2591                 /* Logical moves, may do nothing */
2592                 if (!get_moves(m_idx, mm)) return;
2593         }
2594
2595         /* Assume nothing */
2596         do_turn = FALSE;
2597         do_move = FALSE;
2598         do_view = FALSE;
2599         must_alter_to_move = FALSE;
2600
2601         /* Assume nothing */
2602         did_open_door = FALSE;
2603         did_bash_door = FALSE;
2604         did_take_item = FALSE;
2605         did_kill_item = FALSE;
2606         did_move_body = FALSE;
2607         did_pass_wall = FALSE;
2608         did_kill_wall = FALSE;
2609
2610         /* Take a zero-terminated array of "directions" */
2611         for (i = 0; mm[i]; i++)
2612         {
2613                 /* Get the direction */
2614                 d = mm[i];
2615
2616                 /* Hack -- allow "randomized" motion */
2617                 if (d == 5) d = ddd[randint0(8)];
2618
2619                 /* Get the destination */
2620                 ny = oy + ddy[d];
2621                 nx = ox + ddx[d];
2622
2623                 /* Ignore locations off of edge */
2624                 if (!in_bounds2(ny, nx)) continue;
2625
2626                 /* Access that grid */
2627                 g_ptr = &current_floor_ptr->grid_array[ny][nx];
2628                 f_ptr = &f_info[g_ptr->feat];
2629                 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2630
2631                 /* Access that grid's contents */
2632                 y_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
2633
2634                 /* Hack -- player 'in' wall */
2635                 if (player_bold(ny, nx))
2636                 {
2637                         do_move = TRUE;
2638                 }
2639
2640                 /* Possibly a monster to attack */
2641                 else if (g_ptr->m_idx)
2642                 {
2643                         do_move = TRUE;
2644                 }
2645
2646                 /* Monster destroys walls (and doors) */
2647                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
2648                          (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
2649                          have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
2650                          check_hp_for_feat_destruction(f_ptr, m_ptr))
2651                 {
2652                         /* Eat through walls/doors/rubble */
2653                         do_move = TRUE;
2654                         if (!can_cross) must_alter_to_move = TRUE;
2655
2656                         /* Monster destroyed a wall (later) */
2657                         did_kill_wall = TRUE;
2658                 }
2659
2660                 /* Floor is open? */
2661                 else if (can_cross)
2662                 {
2663                         /* Go ahead and move */
2664                         do_move = TRUE;
2665
2666                         /* Monster moves through walls (and doors) */
2667                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2668                             have_flag(f_ptr->flags, FF_CAN_PASS))
2669                         {
2670                                 /* Monster went through a wall */
2671                                 did_pass_wall = TRUE;
2672                         }
2673                 }
2674
2675                 /* Handle doors and secret doors */
2676                 else if (is_closed_door(g_ptr->feat))
2677                 {
2678                         bool may_bash = TRUE;
2679
2680                         /* Assume no move allowed */
2681                         do_move = FALSE;
2682
2683                         /* Creature can open doors. */
2684                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2685                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2686                         {
2687                                 /* Closed doors */
2688                                 if (!f_ptr->power)
2689                                 {
2690                                         /* The door is open */
2691                                         did_open_door = TRUE;
2692
2693                                         /* Do not bash the door */
2694                                         may_bash = FALSE;
2695
2696                                         do_turn = TRUE;
2697                                 }
2698
2699                                 /* Locked doors (not jammed) */
2700                                 else
2701                                 {
2702                                         /* Try to unlock it */
2703                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
2704                                         {
2705                                                 /* Unlock the door */
2706                                                 cave_alter_feat(ny, nx, FF_DISARM);
2707
2708                                                 /* Do not bash the door */
2709                                                 may_bash = FALSE;
2710
2711                                                 do_turn = TRUE;
2712                                         }
2713                                 }
2714                         }
2715
2716                         /* Stuck doors -- attempt to bash them down if allowed */
2717                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
2718                                 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2719                         {
2720                                 /* Attempt to Bash */
2721                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
2722                                 {
2723                                         if (have_flag(f_ptr->flags, FF_GLASS))
2724                                                 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
2725                                         else
2726                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
2727
2728                                         /* Disturb (sometimes) */
2729                                         if (disturb_minor) disturb(FALSE, FALSE);
2730
2731                                         /* The door was bashed open */
2732                                         did_bash_door = TRUE;
2733
2734                                         /* Hack -- fall into doorway */
2735                                         do_move = TRUE;
2736                                         must_alter_to_move = TRUE;
2737                                 }
2738                         }
2739
2740
2741                         /* Deal with doors in the way */
2742                         if (did_open_door || did_bash_door)
2743                         {
2744                                 /* Break down the door */
2745                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
2746                                 {
2747                                         cave_alter_feat(ny, nx, FF_BASH);
2748
2749                                         if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
2750                                         {
2751                                                 /* Update some things */
2752                                                 p_ptr->update |= (PU_FLOW);
2753                                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2754                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2755
2756                                                 return;
2757                                         }
2758                                 }
2759
2760                                 /* Open the door */
2761                                 else
2762                                 {
2763                                         cave_alter_feat(ny, nx, FF_OPEN);
2764                                 }
2765
2766                                 f_ptr = &f_info[g_ptr->feat];
2767
2768                                 /* Handle viewable doors */
2769                                 do_view = TRUE;
2770                         }
2771                 }
2772
2773                 /* Hack -- check for Glyph of Warding */
2774                 if (do_move && is_glyph_grid(g_ptr) &&
2775                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2776                 {
2777                         /* Assume no move allowed */
2778                         do_move = FALSE;
2779
2780                         /* Break the ward */
2781                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
2782                         {
2783                                 /* Describe observable breakage */
2784                                 if (g_ptr->info & CAVE_MARK)
2785                                 {
2786                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2787                                 }
2788
2789                                 /* Forget the rune */
2790                                 g_ptr->info &= ~(CAVE_MARK);
2791
2792                                 /* Break the rune */
2793                                 g_ptr->info &= ~(CAVE_OBJECT);
2794                                 g_ptr->mimic = 0;
2795
2796                                 /* Allow movement */
2797                                 do_move = TRUE;
2798
2799                                 note_spot(ny, nx);
2800                         }
2801                 }
2802                 else if (do_move && is_explosive_rune_grid(g_ptr) &&
2803                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2804                 {
2805                         /* Assume no move allowed */
2806                         do_move = FALSE;
2807
2808                         /* Break the ward */
2809                         if (!is_pet(m_ptr))
2810                         {
2811                                 /* Break the ward */
2812                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2813                                 {
2814                                         /* Describe observable breakage */
2815                                         if (g_ptr->info & CAVE_MARK)
2816                                         {
2817                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2818                                                 project(0, 2, ny, nx, 2 * (p_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
2819                                         }
2820                                 }
2821                                 else
2822                                 {
2823                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2824                                 }
2825
2826                                 /* Forget the rune */
2827                                 g_ptr->info &= ~(CAVE_MARK);
2828
2829                                 /* Break the rune */
2830                                 g_ptr->info &= ~(CAVE_OBJECT);
2831                                 g_ptr->mimic = 0;
2832
2833                                 note_spot(ny, nx);
2834                                 lite_spot(ny, nx);
2835
2836                                 if (!m_ptr->r_idx) return;
2837                                 /* Allow movement */
2838                                 do_move = TRUE;
2839                         }
2840                 }
2841
2842                 /* The player is in the way */
2843                 if (do_move && player_bold(ny, nx))
2844                 {
2845                         /* Some monsters never attack */
2846                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
2847                         {
2848                                 /* Hack -- memorize lack of attacks */
2849                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2850
2851                                 /* Do not move */
2852                                 do_move = FALSE;
2853                         }
2854
2855                         /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2856                         if (do_move && (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2857                         {
2858                                 if (!MON_CONFUSED(m_ptr))
2859                                 {
2860                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2861                                         else
2862                                         {
2863                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2864                                         }
2865                                 }
2866                         }
2867
2868                         /* The player is in the way.  Attack him. */
2869                         if (do_move)
2870                         {
2871                                 if (!p_ptr->riding || one_in_(2))
2872                                 {
2873                                         /* Do the attack */
2874                                         (void)make_attack_normal(m_idx);
2875
2876                                         /* Do not move */
2877                                         do_move = FALSE;
2878
2879                                         /* Took a current_world_ptr->game_turn */
2880                                         do_turn = TRUE;
2881                                 }
2882                         }
2883                 }
2884
2885                 /* A monster is in the way */
2886                 if (do_move && g_ptr->m_idx)
2887                 {
2888                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
2889
2890                         /* Assume no movement */
2891                         do_move = FALSE;
2892
2893                         /* Attack 'enemies' */
2894                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2895                                  (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2896                                  can_cross && (g_ptr->m_idx != p_ptr->riding)) ||
2897                                 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2898                         {
2899                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2900                                 {
2901                                         if (r_ptr->flags2 & RF2_KILL_BODY)
2902                                         {
2903                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2904                                         }
2905
2906                                         /* attack */
2907                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
2908                                         {
2909                                                 if (monst_attack_monst(m_idx, g_ptr->m_idx)) return;
2910
2911                                                 /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2912                                                 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2913                                                 {
2914                                                         if (MON_CONFUSED(m_ptr)) return;
2915                                                         else if (r_ptr->flags2 & RF2_STUPID)
2916                                                         {
2917                                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2918                                                                 return;
2919                                                         }
2920                                                 }
2921                                         }
2922                                 }
2923                         }
2924
2925                         /* Push past weaker monsters (unless leaving a wall) */
2926                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2927                                 (r_ptr->mexp > z_ptr->mexp) &&
2928                                 can_cross && (g_ptr->m_idx != p_ptr->riding) &&
2929                                 monster_can_cross_terrain(current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2930                         {
2931                                 /* Allow movement */
2932                                 do_move = TRUE;
2933
2934                                 /* Monster pushed past another monster */
2935                                 did_move_body = TRUE;
2936
2937                                 /* Wake up the moved monster */
2938                                 (void)set_monster_csleep(g_ptr->m_idx, 0);
2939
2940                                 /* Message */
2941                         }
2942                 }
2943
2944                 if (is_riding_mon)
2945                 {
2946                         if (!p_ptr->riding_ryoute && !MON_MONFEAR(&current_floor_ptr->m_list[p_ptr->riding])) do_move = FALSE;
2947                 }
2948
2949                 if (did_kill_wall && do_move)
2950                 {
2951                         if (one_in_(GRINDNOISE))
2952                         {
2953                                 if (have_flag(f_ptr->flags, FF_GLASS))
2954                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2955                                 else
2956                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2957                         }
2958
2959                         cave_alter_feat(ny, nx, FF_HURT_DISI);
2960
2961                         if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
2962                         {
2963                                 /* Update some things */
2964                                 p_ptr->update |= (PU_FLOW);
2965                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2966                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2967
2968                                 return;
2969                         }
2970
2971                         f_ptr = &f_info[g_ptr->feat];
2972
2973                         /* Note changes to viewable region */
2974                         do_view = TRUE;
2975                         do_turn = TRUE;
2976                 }
2977
2978                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2979                 {
2980                         if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2981                         {
2982                                 /* Assume no move allowed */
2983                                 do_move = FALSE;
2984                         }
2985                 }
2986
2987                 /*
2988                  * Check if monster can cross terrain
2989                  * This is checked after the normal attacks
2990                  * to allow monsters to attack an enemy,
2991                  * even if it can't enter the terrain.
2992                  */
2993                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2994                 {
2995                         /* Assume no move allowed */
2996                         do_move = FALSE;
2997                 }
2998
2999                 /* Some monsters never move */
3000                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
3001                 {
3002                         /* Hack -- memorize lack of moves */
3003                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
3004
3005                         /* Do not move */
3006                         do_move = FALSE;
3007                 }
3008
3009                 /* Creature has been allowed move */
3010                 if (do_move)
3011                 {
3012                         do_turn = TRUE;
3013
3014                         if (have_flag(f_ptr->flags, FF_TREE))
3015                         {
3016                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
3017                                 {
3018                                         m_ptr->energy_need += ENERGY_NEED();
3019                                 }
3020                         }
3021
3022                         if (!is_riding_mon)
3023                         {
3024                                 /* Hack -- Update the old location */
3025                                 current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
3026
3027                                 /* Mega-Hack -- move the old monster, if any */
3028                                 if (g_ptr->m_idx)
3029                                 {
3030                                         /* Move the old monster */
3031                                         y_ptr->fy = oy;
3032                                         y_ptr->fx = ox;
3033
3034                                         /* Update the old monster */
3035                                         update_monster(g_ptr->m_idx, TRUE);
3036                                 }
3037
3038                                 /* Hack -- Update the new location */
3039                                 g_ptr->m_idx = m_idx;
3040
3041                                 /* Move the monster */
3042                                 m_ptr->fy = ny;
3043                                 m_ptr->fx = nx;
3044                                 update_monster(m_idx, TRUE);
3045
3046                                 lite_spot(oy, ox);
3047                                 lite_spot(ny, nx);
3048                         }
3049                         else
3050                         {
3051                                 /* sound(SOUND_WALK); */
3052                                 if (!move_player_effect(ny, nx, MPE_DONT_PICKUP)) break;
3053                         }
3054
3055                         /* Possible disturb */
3056                         if (m_ptr->ml &&
3057                             (disturb_move ||
3058                              (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
3059                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
3060                         {
3061                                 if (is_hostile(m_ptr))
3062                                         disturb(FALSE, TRUE);
3063                         }
3064
3065                         /* Take or Kill objects on the floor */
3066                         if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
3067                             (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
3068                         {
3069                                 OBJECT_IDX this_o_idx, next_o_idx;
3070                                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
3071
3072                                 /* Scan all objects in the grid */
3073                                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
3074                                 {
3075                                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
3076                                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
3077                                         object_type *o_ptr = &current_floor_ptr->o_list[this_o_idx];
3078                                         next_o_idx = o_ptr->next_o_idx;
3079
3080                                         if (do_take)
3081                                         {
3082                                                 /* Skip gold */
3083                                                 if (o_ptr->tval == TV_GOLD) continue;
3084
3085                                                 /*
3086                                                  * Skip "real" corpses and statues, to avoid extreme
3087                                                  * silliness like a novice rogue pockets full of statues
3088                                                  * and corpses.
3089                                                  */
3090                                                 if ((o_ptr->tval == TV_CORPSE) ||
3091                                                     (o_ptr->tval == TV_STATUE)) continue;
3092                                         }
3093
3094                                         /* Extract some flags */
3095                                         object_flags(o_ptr, flgs);
3096
3097                                         /* Acquire the object name */
3098                                         object_desc(o_name, o_ptr, 0);
3099                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
3100
3101                                         /* React to objects that hurt the monster */
3102                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
3103                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
3104                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
3105                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
3106                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
3107                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
3108                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
3109                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
3110                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
3111                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
3112                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
3113                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
3114                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
3115                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
3116                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
3117                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
3118                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
3119                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
3120                                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
3121                                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
3122                                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
3123                                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
3124                                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
3125
3126                                         /* The object cannot be picked up by the monster */
3127                                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
3128                                             ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
3129                                         {
3130                                                 /* Only give a message for "take_item" */
3131                                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
3132                                                 {
3133                                                         did_take_item = TRUE;
3134
3135                                                         /* Describe observable situations */
3136                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
3137                                                         {
3138                                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
3139                                                         }
3140                                                 }
3141                                         }
3142
3143                                         /* Pick up the item */
3144                                         else if (do_take)
3145                                         {
3146                                                 did_take_item = TRUE;
3147
3148                                                 /* Describe observable situations */
3149                                                 if (player_can_see_bold(ny, nx))
3150                                                 {
3151                                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
3152                                                 }
3153
3154                                                 /* Excise the object */
3155                                                 excise_object_idx(this_o_idx);
3156
3157                                                 /* Forget mark */
3158                                                 o_ptr->marked &= OM_TOUCHED;
3159
3160                                                 /* Forget location */
3161                                                 o_ptr->iy = o_ptr->ix = 0;
3162
3163                                                 /* Memorize monster */
3164                                                 o_ptr->held_m_idx = m_idx;
3165
3166                                                 /* Build a stack */
3167                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
3168
3169                                                 /* Carry object */
3170                                                 m_ptr->hold_o_idx = this_o_idx;
3171                                         }
3172
3173                                         /* Destroy the item if not a pet */
3174                                         else if (!is_pet(m_ptr))
3175                                         {
3176                                                 did_kill_item = TRUE;
3177
3178                                                 /* Describe observable situations */
3179                                                 if (player_has_los_bold(ny, nx))
3180                                                 {
3181                                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
3182                                                 }
3183
3184                                                 delete_object_idx(this_o_idx);
3185                                         }
3186                                 }
3187                         }
3188                 }
3189
3190                 /* Stop when done */
3191                 if (do_turn) break;
3192         }
3193
3194         /*
3195          *  Forward movements failed, but now received LOS attack!
3196          *  Try to flow by smell.
3197          */
3198         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3199                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3200
3201         /* If we haven't done anything, try casting a spell again */
3202         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
3203         {
3204                 /* Try to cast spell again */
3205                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3206                 {
3207                         if (make_attack_spell(m_idx)) return;
3208                 }
3209         }
3210
3211
3212         /* Notice changes in view */
3213         if (do_view)
3214         {
3215                 p_ptr->update |= (PU_FLOW);
3216                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3217         }
3218
3219         /* Notice changes in view */
3220         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3221                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3222         {
3223                 /* Update some things */
3224                 p_ptr->update |= (PU_MON_LITE);
3225         }
3226
3227         /* Learn things from observable monster */
3228         if (is_original_ap_and_seen(m_ptr))
3229         {
3230                 /* Monster opened a door */
3231                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3232
3233                 /* Monster bashed a door */
3234                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3235
3236                 /* Monster tried to pick something up */
3237                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3238
3239                 /* Monster tried to crush something */
3240                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3241
3242                 /* Monster pushed past another monster */
3243                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3244
3245                 /* Monster passed through a wall */
3246                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3247
3248                 /* Monster destroyed a wall */
3249                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3250         }
3251
3252
3253         /* Hack -- get "bold" if out of options */
3254         if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
3255         {
3256                 /* No longer afraid */
3257                 (void)set_monster_monfear(m_idx, 0);
3258
3259                 /* Message if seen */
3260                 if (see_m)
3261                 {
3262                         GAME_TEXT m_name[MAX_NLEN];
3263                         monster_desc(m_name, m_ptr, 0);
3264                         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
3265                 }
3266
3267                 if (m_ptr->ml) chg_virtue(V_COMPASSION, -1);
3268
3269                 /* Actually do something now (?) */
3270         }
3271 }
3272
3273 /*!
3274  * @brief 全モンスターのターン管理メインルーチン /
3275  * Process all the "live" monsters, once per game current_world_ptr->game_turn.
3276  * @return なし
3277  * @details
3278  * During each game current_world_ptr->game_turn, we scan through the list of all the "live" monsters,\n
3279  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
3280  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
3281  *\n
3282  * Note that monsters can never move in the monster array (except when the\n
3283  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
3284  *\n
3285  * This function is responsible for at least half of the processor time\n
3286  * on a normal system with a "normal" amount of monsters and a player doing\n
3287  * normal things.\n
3288  *\n
3289  * When the player is resting, virtually 90% of the processor time is spent\n
3290  * in this function, and its children, "process_monster()" and "make_move()".\n
3291  *\n
3292  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
3293  * especially when the player is running.\n
3294  *\n
3295  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
3296  * monsters while they are still being "born".  A monster is "fresh" only\n
3297  * during the current_world_ptr->game_turn in which it is created, and we use the "hack_m_idx" to\n
3298  * determine if the monster is yet to be processed during the current current_world_ptr->game_turn.\n
3299  *\n
3300  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
3301  * move before any "nasty" monsters get to use their spell attacks.\n
3302  *\n
3303  * Note that when the "knowledge" about the currently tracked monster\n
3304  * changes (flags, attacks, spells), we induce a redraw of the monster\n
3305  * recall window.\n
3306  */
3307 void process_monsters(void)
3308 {
3309         IDX i;
3310         POSITION fx, fy;
3311
3312         bool            test;
3313
3314         monster_type    *m_ptr;
3315         monster_race    *r_ptr;
3316
3317         MONRACE_IDX old_monster_race_idx;
3318
3319         BIT_FLAGS old_r_flags1 = 0L;
3320         BIT_FLAGS old_r_flags2 = 0L;
3321         BIT_FLAGS old_r_flags3 = 0L;
3322         BIT_FLAGS old_r_flags4 = 0L;
3323         BIT_FLAGS old_r_flags5 = 0L;
3324         BIT_FLAGS old_r_flags6 = 0L;
3325         BIT_FLAGS old_r_flagsr = 0L;
3326
3327         byte old_r_blows0 = 0;
3328         byte old_r_blows1 = 0;
3329         byte old_r_blows2 = 0;
3330         byte old_r_blows3 = 0;
3331
3332         byte old_r_cast_spell = 0;
3333
3334         SPEED speed;
3335
3336         /* Clear monster fighting indicator */
3337         mon_fight = FALSE;
3338
3339         /* Memorize old race */
3340         old_monster_race_idx = p_ptr->monster_race_idx;
3341
3342         /* Acquire knowledge */
3343         if (p_ptr->monster_race_idx)
3344         {
3345                 /* Acquire current monster */
3346                 r_ptr = &r_info[p_ptr->monster_race_idx];
3347
3348                 /* Memorize flags */
3349                 old_r_flags1 = r_ptr->r_flags1;
3350                 old_r_flags2 = r_ptr->r_flags2;
3351                 old_r_flags3 = r_ptr->r_flags3;
3352                 old_r_flags4 = r_ptr->r_flags4;
3353                 old_r_flags5 = r_ptr->r_flags5;
3354                 old_r_flags6 = r_ptr->r_flags6;
3355                 old_r_flagsr = r_ptr->r_flagsr;
3356
3357                 /* Memorize blows */
3358                 old_r_blows0 = r_ptr->r_blows[0];
3359                 old_r_blows1 = r_ptr->r_blows[1];
3360                 old_r_blows2 = r_ptr->r_blows[2];
3361                 old_r_blows3 = r_ptr->r_blows[3];
3362
3363                 /* Memorize castings */
3364                 old_r_cast_spell = r_ptr->r_cast_spell;
3365         }
3366
3367
3368         /* Process the monsters (backwards) */
3369         for (i = m_max - 1; i >= 1; i--)
3370         {
3371                 /* Access the monster */
3372                 m_ptr = &current_floor_ptr->m_list[i];
3373                 r_ptr = &r_info[m_ptr->r_idx];
3374
3375                 /* Handle "leaving" */
3376                 if (p_ptr->leaving) break;
3377
3378                 /* Ignore "dead" monsters */
3379                 if (!m_ptr->r_idx) continue;
3380
3381                 if (p_ptr->wild_mode) continue;
3382
3383
3384                 /* Handle "fresh" monsters */
3385                 if (m_ptr->mflag & MFLAG_BORN)
3386                 {
3387                         /* No longer "fresh" */
3388                         m_ptr->mflag &= ~(MFLAG_BORN);
3389
3390                         /* Skip */
3391                         continue;
3392                 }
3393
3394                 /* Hack -- Require proximity */
3395                 if (m_ptr->cdis >= AAF_LIMIT) continue;
3396
3397
3398                 /* Access the location */
3399                 fx = m_ptr->fx;
3400                 fy = m_ptr->fy;
3401
3402                 /* Flow by smell is allowed */
3403                 if (!p_ptr->no_flowed)
3404                 {
3405                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3406                 }
3407
3408                 /* Assume no move */
3409                 test = FALSE;
3410
3411                 /* Handle "sensing radius" */
3412                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3413                 {
3414                         /* We can "sense" the player */
3415                         test = TRUE;
3416                 }
3417
3418                 /* Handle "sight" and "aggravation" */
3419         else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->inside_battle) &&
3420                         (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3421                 {
3422                         /* We can "see" or "feel" the player */
3423                         test = TRUE;
3424                 }
3425
3426 #if 0 /* (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) is always FALSE... */
3427                 /* Hack -- Monsters can "smell" the player from far away */
3428                 /* Note that most monsters have "aaf" of "20" or so */
3429                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3430                         cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
3431                         (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) &&
3432                         (current_floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3433                         (current_floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
3434                 {
3435                         /* We can "smell" the player */
3436                         test = TRUE;
3437                 }
3438 #endif
3439                 else if (m_ptr->target_y) test = TRUE;
3440
3441                 /* Do nothing */
3442                 if (!test) continue;
3443
3444
3445                 if (p_ptr->riding == i)
3446                         speed = p_ptr->pspeed;
3447                 else
3448                 {
3449                         speed = m_ptr->mspeed;
3450
3451                         /* Monsters move quickly in Nightmare mode */
3452                         if (ironman_nightmare) speed += 5;
3453
3454                         if (MON_FAST(m_ptr)) speed += 10;
3455                         if (MON_SLOW(m_ptr)) speed -= 10;
3456                 }
3457
3458                 /* Give this monster some energy */
3459                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3460
3461                 /* Not enough energy to move */
3462                 if (m_ptr->energy_need > 0) continue;
3463
3464                 /* Use up "some" energy */
3465                 m_ptr->energy_need += ENERGY_NEED();
3466
3467
3468                 /* Save global index */
3469                 hack_m_idx = i;
3470
3471                 /* Process the monster */
3472                 process_monster(i);
3473
3474                 reset_target(m_ptr);
3475
3476                 /* Give up flow_by_smell when it might useless */
3477                 if (p_ptr->no_flowed && one_in_(3))
3478                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
3479
3480                 /* Hack -- notice death or departure */
3481                 if (!p_ptr->playing || p_ptr->is_dead) break;
3482
3483                 /* Notice leaving */
3484                 if (p_ptr->leaving) break;
3485         }
3486
3487         /* Reset global index */
3488         hack_m_idx = 0;
3489
3490
3491         /* Tracking a monster race (the same one we were before) */
3492         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3493         {
3494                 /* Acquire monster race */
3495                 r_ptr = &r_info[p_ptr->monster_race_idx];
3496
3497                 /* Check for knowledge change */
3498                 if ((old_r_flags1 != r_ptr->r_flags1) ||
3499                         (old_r_flags2 != r_ptr->r_flags2) ||
3500                         (old_r_flags3 != r_ptr->r_flags3) ||
3501                         (old_r_flags4 != r_ptr->r_flags4) ||
3502                         (old_r_flags5 != r_ptr->r_flags5) ||
3503                         (old_r_flags6 != r_ptr->r_flags6) ||
3504                         (old_r_flagsr != r_ptr->r_flagsr) ||
3505                         (old_r_blows0 != r_ptr->r_blows[0]) ||
3506                         (old_r_blows1 != r_ptr->r_blows[1]) ||
3507                         (old_r_blows2 != r_ptr->r_blows[2]) ||
3508                         (old_r_blows3 != r_ptr->r_blows[3]) ||
3509                         (old_r_cast_spell != r_ptr->r_cast_spell))
3510                 {
3511                         p_ptr->window |= (PW_MONSTER);
3512                 }
3513         }
3514 }
3515