OSDN Git Service

[Refactor] #37353 process_angar() をprocess_monster() から分離 / Separated process_angar...
[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 "util.h"
18
19 #include "cmd-dump.h"
20 #include "cmd-pet.h"
21 #include "creature.h"
22 #include "melee.h"
23 #include "spells.h"
24 #include "spells-floor.h"
25 #include "spells-summon.h"
26 #include "quest.h"
27 #include "avatar.h"
28 #include "realm-hex.h"
29 #include "object-flavor.h"
30 #include "object-hook.h"
31 #include "feature.h"
32 #include "grid.h"
33 #include "player-move.h"
34 #include "monster-status.h"
35 #include "monster-spell.h"
36 #include "monster-process.h"
37 #include "monsterrace-hook.h"
38 #include "dungeon.h"
39 #include "floor.h"
40 #include "files.h"
41 #include "view-mainwindow.h"
42
43 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
44 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
45 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
46 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
47 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
48 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
49 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
50 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
51 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m);
52 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
53 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
54 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
55
56  /*!
57   * @brief モンスターが敵に接近するための方向を決める /
58   * Calculate the direction to the next enemy
59   * @param target_ptr プレーヤーへの参照ポインタ
60   * @param m_idx モンスターの参照ID
61   * @param mm 移動するべき方角IDを返す参照ポインタ
62   * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
63   */
64 static bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
65 {
66         floor_type *floor_ptr = target_ptr->current_floor_ptr;
67         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
68         monster_race *r_ptr = &r_info[m_ptr->r_idx];
69         monster_type *t_ptr;
70
71         POSITION x = 0, y = 0;
72         if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
73         {
74                 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
75                 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
76         }
77         else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
78         {
79                 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
80                 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
81         }
82         else
83         {
84                 int start;
85                 int plus = 1;
86                 if (target_ptr->phase_out)
87                 {
88                         start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
89                         if (randint0(2)) plus = -1;
90                 }
91                 else start = floor_ptr->m_max + 1;
92
93                 /* Scan thru all monsters */
94                 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
95                 {
96                         MONSTER_IDX dummy = (i % floor_ptr->m_max);
97
98                         if (!dummy) continue;
99
100                         MONSTER_IDX t_idx = dummy;
101                         t_ptr = &floor_ptr->m_list[t_idx];
102
103                         /* The monster itself isn't a target */
104                         if (t_ptr == m_ptr) continue;
105
106                         if (!monster_is_valid(t_ptr)) continue;
107
108                         if (is_pet(m_ptr))
109                         {
110                                 /* Hack -- only fight away from player */
111                                 if (target_ptr->pet_follow_distance < 0)
112                                 {
113                                         /* No fighting near player */
114                                         if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
115                                         {
116                                                 continue;
117                                         }
118                                 }
119                                 /* Hack -- no fighting away from player */
120                                 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
121                                 {
122                                         continue;
123                                 }
124
125                                 if (r_ptr->aaf < t_ptr->cdis) continue;
126                         }
127
128                         /* Monster must be 'an enemy' */
129                         if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
130
131                         /* Monster must be projectable if we can't pass through walls */
132                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
133                                 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
134                         {
135                                 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
136                         }
137                         else
138                         {
139                                 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
140                         }
141
142                         /* OK -- we've got a target */
143                         y = t_ptr->fy;
144                         x = t_ptr->fx;
145
146                         break;
147                 }
148
149                 if (!x && !y) return FALSE;
150         }
151
152         /* Extract the direction */
153         x -= m_ptr->fx;
154         y -= m_ptr->fy;
155
156         /* North */
157         if ((y < 0) && (x == 0))
158         {
159                 mm[0] = 8;
160                 mm[1] = 7;
161                 mm[2] = 9;
162         }
163         /* South */
164         else if ((y > 0) && (x == 0))
165         {
166                 mm[0] = 2;
167                 mm[1] = 1;
168                 mm[2] = 3;
169         }
170         /* East */
171         else if ((x > 0) && (y == 0))
172         {
173                 mm[0] = 6;
174                 mm[1] = 9;
175                 mm[2] = 3;
176         }
177         /* West */
178         else if ((x < 0) && (y == 0))
179         {
180                 mm[0] = 4;
181                 mm[1] = 7;
182                 mm[2] = 1;
183         }
184         /* North-West */
185         else if ((y < 0) && (x < 0))
186         {
187                 mm[0] = 7;
188                 mm[1] = 4;
189                 mm[2] = 8;
190         }
191         /* North-East */
192         else if ((y < 0) && (x > 0))
193         {
194                 mm[0] = 9;
195                 mm[1] = 6;
196                 mm[2] = 8;
197         }
198         /* South-West */
199         else if ((y > 0) && (x < 0))
200         {
201                 mm[0] = 1;
202                 mm[1] = 4;
203                 mm[2] = 2;
204         }
205         /* South-East */
206         else if ((y > 0) && (x > 0))
207         {
208                 mm[0] = 3;
209                 mm[1] = 6;
210                 mm[2] = 2;
211         }
212
213         return TRUE;
214 }
215
216 /*!
217  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
218  * Returns whether a given monster will try to run from the player.
219  * @param m_idx 逃走するモンスターの参照ID
220  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
221  * @details
222  * Monsters will attempt to avoid very powerful players.  See below.\n
223  *\n
224  * Because this function is called so often, little details are important\n
225  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
226  * attempting to check the conditions in an optimal order.  Note that\n
227  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
228  *\n
229  * Note that this function is responsible for about one to five percent\n
230  * of the processor use in normal conditions...\n
231  */
232 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
233 {
234         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
235         monster_race *r_ptr = &r_info[m_ptr->r_idx];
236
237         PLAYER_LEVEL p_lev;
238         DEPTH m_lev;
239         HIT_POINT p_chp, p_mhp;
240         HIT_POINT m_chp, m_mhp;
241         u32b p_val, m_val;
242
243         /* Friends can be commanded to avoid the player */
244         if (is_pet(m_ptr))
245         {
246                 /* Are we trying to avoid the player? */
247                 return ((target_ptr->pet_follow_distance < 0) &&
248                         (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
249         }
250
251         /* Keep monsters from running too far away */
252         if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
253
254         /* All "afraid" monsters will run away */
255         if (MON_MONFEAR(m_ptr)) return TRUE;
256
257         /* Nearby monsters will not become terrified */
258         if (m_ptr->cdis <= 5) return FALSE;
259
260         /* Examine player power (level) */
261         p_lev = target_ptr->lev;
262
263         /* Examine monster power (level plus morale) */
264         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
265
266         /* Optimize extreme cases below */
267         if (m_lev > p_lev + 4) return FALSE;
268         if (m_lev + 4 <= p_lev) return TRUE;
269
270         /* Examine player health */
271         p_chp = target_ptr->chp;
272         p_mhp = target_ptr->mhp;
273
274         /* Examine monster health */
275         m_chp = m_ptr->hp;
276         m_mhp = m_ptr->maxhp;
277
278         /* Prepare to optimize the calculation */
279         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
280         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
281
282         /* Strong players scare strong monsters */
283         if (p_val * m_mhp > m_val * p_mhp) return TRUE;
284
285         return FALSE;
286 }
287
288
289 /*!
290  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
291  * Search spell castable grid
292  * @param target_ptr プレーヤーへの参照ポインタ
293  * @param m_idx モンスターの参照ID
294  * @param yp 適したマスのY座標を返す参照ポインタ
295  * @param xp 適したマスのX座標を返す参照ポインタ
296  * @return 有効なマスがあった場合TRUEを返す
297  */
298 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
299 {
300         floor_type *floor_ptr = target_ptr->current_floor_ptr;
301         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
302         monster_race *r_ptr = &r_info[m_ptr->r_idx];
303
304         /* Monster location */
305         POSITION y1 = m_ptr->fy;
306         POSITION x1 = m_ptr->fx;
307
308         /* Monster can already cast spell to player */
309         if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
310
311         /* Set current grid cost */
312         int now_cost = floor_ptr->grid_array[y1][x1].cost;
313         if (now_cost == 0) now_cost = 999;
314
315         /* Can monster bash or open doors? */
316         bool can_open_door = FALSE;
317         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
318         {
319                 can_open_door = TRUE;
320         }
321
322         /* Check nearby grids, diagonals first */
323         int best = 999;
324         for (int i = 7; i >= 0; i--)
325         {
326                 int cost;
327
328                 POSITION y = y1 + ddy_ddd[i];
329                 POSITION x = x1 + ddx_ddd[i];
330
331                 /* Ignore locations off of edge */
332                 if (!in_bounds2(floor_ptr, y, x)) continue;
333
334                 /* Simply move to player */
335                 if (player_bold(target_ptr, y, x)) return FALSE;
336
337                 grid_type *g_ptr;
338                 g_ptr = &floor_ptr->grid_array[y][x];
339
340                 cost = g_ptr->cost;
341
342                 /* Monster cannot kill or pass walls */
343                 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding))))
344                 {
345                         if (cost == 0) continue;
346                         if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
347                 }
348
349                 /* Hack -- for kill or pass wall monster.. */
350                 if (cost == 0) cost = 998;
351
352                 if (now_cost < cost) continue;
353
354                 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
355
356                 /* Accept louder sounds */
357                 if (best < cost) continue;
358                 best = cost;
359
360                 (*yp) = y1 + ddy_ddd[i];
361                 (*xp) = x1 + ddx_ddd[i];
362         }
363
364         if (best == 999) return FALSE;
365
366         return TRUE;
367 }
368
369
370 /*!
371  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
372  * Choose the "best" direction for "flowing"
373  * @param m_idx モンスターの参照ID
374  * @param yp 移動先のマスのY座標を返す参照ポインタ
375  * @param xp 移動先のマスのX座標を返す参照ポインタ
376  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
377  * @return 有効なマスがあった場合TRUEを返す
378  * @details
379  * Note that ghosts and rock-eaters are never allowed to "flow",\n
380  * since they should move directly towards the player.\n
381  *\n
382  * Prefer "non-diagonal" directions, but twiddle them a little\n
383  * to angle slightly towards the player's actual location.\n
384  *\n
385  * Allow very perceptive monsters to track old "spoor" left by\n
386  * previous locations occupied by the player.  This will tend\n
387  * to have monsters end up either near the player or on a grid\n
388  * recently occupied by the player (and left via "teleport").\n
389  *\n
390  * Note that if "smell" is turned on, all monsters get vicious.\n
391  *\n
392  * Also note that teleporting away from a location will cause\n
393  * the monsters who were chasing you to converge on that location\n
394  * as long as you are still near enough to "annoy" them without\n
395  * being close enough to chase directly.  I have no idea what will\n
396  * happen if you combine "smell" with low "aaf" values.\n
397  */
398 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
399 {
400         grid_type *g_ptr;
401         floor_type *floor_ptr = target_ptr->current_floor_ptr;
402         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
403         monster_race *r_ptr = &r_info[m_ptr->r_idx];
404
405         /* Can monster cast attack spell? */
406         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
407                 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
408                 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
409         {
410                 /* Can move spell castable grid? */
411                 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
412         }
413
414         /* Monster can't flow */
415         if (no_flow) return FALSE;
416
417         /* Monster can go through rocks */
418         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
419         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
420
421         /* Monster location */
422         POSITION y1 = m_ptr->fy;
423         POSITION x1 = m_ptr->fx;
424
425         /* Hack -- Player can see us, run towards him */
426         if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
427
428         /* Monster grid */
429         g_ptr = &floor_ptr->grid_array[y1][x1];
430
431         /* If we can hear noises, advance towards them */
432         int best;
433         bool use_scent = FALSE;
434         if (g_ptr->cost)
435         {
436                 best = 999;
437         }
438
439         /* Otherwise, try to follow a scent trail */
440         else if (g_ptr->when)
441         {
442                 /* Too old smell */
443                 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
444
445                 use_scent = TRUE;
446                 best = 0;
447         }
448
449         /* Otherwise, advance blindly */
450         else
451         {
452                 return FALSE;
453         }
454
455         /* Check nearby grids, diagonals first */
456         for (int i = 7; i >= 0; i--)
457         {
458                 POSITION y = y1 + ddy_ddd[i];
459                 POSITION x = x1 + ddx_ddd[i];
460
461                 /* Ignore locations off of edge */
462                 if (!in_bounds2(floor_ptr, y, x)) continue;
463
464                 g_ptr = &floor_ptr->grid_array[y][x];
465
466                 /* We're following a scent trail */
467                 if (use_scent)
468                 {
469                         int when = g_ptr->when;
470
471                         /* Accept younger scent */
472                         if (best > when) continue;
473                         best = when;
474                 }
475
476                 /* We're using sound */
477                 else
478                 {
479                         int cost;
480
481                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
482                                 cost = g_ptr->dist;
483                         else cost = g_ptr->cost;
484
485                         /* Accept louder sounds */
486                         if ((cost == 0) || (best < cost)) continue;
487                         best = cost;
488                 }
489
490                 /* Hack -- Save the "twiddled" location */
491                 (*yp) = target_ptr->y + 16 * ddy_ddd[i];
492                 (*xp) = target_ptr->x + 16 * ddx_ddd[i];
493         }
494
495         if (best == 999 || best == 0) return FALSE;
496
497         return TRUE;
498 }
499
500
501 /*!
502  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
503  * Provide a location to flee to, but give the player a wide berth.
504  * @param m_idx モンスターの参照ID
505  * @param yp 移動先のマスのY座標を返す参照ポインタ
506  * @param xp 移動先のマスのX座標を返す参照ポインタ
507  * @return 有効なマスがあった場合TRUEを返す
508  * @details
509  * A monster may wish to flee to a location that is behind the player,\n
510  * but instead of heading directly for it, the monster should "swerve"\n
511  * around the player so that he has a smaller chance of getting hit.\n
512  */
513 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
514 {
515         POSITION gy = 0, gx = 0;
516
517         /* Monster location */
518         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
519         POSITION fy = m_ptr->fy;
520         POSITION fx = m_ptr->fx;
521
522         /* Desired destination */
523         POSITION y1 = fy - (*yp);
524         POSITION x1 = fx - (*xp);
525
526         /* Check nearby grids, diagonals first */
527         int score = -1;
528         for (int i = 7; i >= 0; i--)
529         {
530                 POSITION dis, s;
531                 POSITION y = fy + ddy_ddd[i];
532                 POSITION x = fx + ddx_ddd[i];
533
534                 /* Ignore locations off of edge */
535                 if (!in_bounds2(floor_ptr, y, x)) continue;
536
537                 /* Calculate distance of this grid from our destination */
538                 dis = distance(y, x, y1, x1);
539
540                 /* Score this grid */
541                 s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
542
543                 /* No negative scores */
544                 if (s < 0) s = 0;
545
546                 /* Ignore lower scores */
547                 if (s < score) continue;
548
549                 /* Save the score and time */
550                 score = s;
551
552                 /* Save the location */
553                 gy = y;
554                 gx = x;
555         }
556
557         /* No legal move (?) */
558         if (score == -1) return FALSE;
559
560         /* Find deltas */
561         (*yp) = fy - gy;
562         (*xp) = fx - gx;
563
564         /* Success */
565         return TRUE;
566 }
567
568 /*
569  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
570  * find_hiding().
571  *
572  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
573  * offsets of all the locations with a distance of n from a central point,
574  * with an offset of (0,0) indicating no more offsets at this distance.
575  *
576  * This is, of course, fairly unreadable, but it eliminates multiple loops
577  * from the previous version.
578  *
579  * It is probably better to replace these arrays with code to compute
580  * the relevant arrays, even if the storage is pre-allocated in hard
581  * coded sizes.  At the very least, code should be included which is
582  * able to generate and dump these arrays (ala "los()").
583  *
584  * Also, the storage needs could be halved by using bytes.
585  *
586  * These arrays could be combined into two big arrays, using sub-arrays
587  * to hold the offsets and lengths of each portion of the sub-arrays, and
588  * this could perhaps also be used somehow in the "look" code.
589  */
590
591
592 static POSITION d_off_y_0[] = { 0 };
593 static POSITION d_off_x_0[] = { 0 };
594
595 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
596 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
597
598 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
599 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
600
601 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
602 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
603
604 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 };
605 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 };
606
607
608 static POSITION d_off_y_5[] =
609 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
610   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
611   5, 0 };
612
613 static POSITION d_off_x_5[] =
614 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
615   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
616   0, 1, 0 };
617
618
619 static POSITION d_off_y_6[] =
620 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
621   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
622   5, 5, 6, 6, 6, 0 };
623
624 static POSITION d_off_x_6[] =
625 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
626   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
627   -3, 2, 3, -1, 0, 1, 0 };
628
629
630 static POSITION d_off_y_7[] =
631 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
632   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
633   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
634
635 static POSITION d_off_x_7[] =
636 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
637   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
638   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
639   1, 0 };
640
641
642 static POSITION d_off_y_8[] =
643 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
644   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
645   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
646   8, 8, 8, 0 };
647
648 static POSITION d_off_x_8[] =
649 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
650   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
651   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
652   2, 3, -1, 0, 1, 0 };
653
654
655 static POSITION d_off_y_9[] =
656 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
657   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
658   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
659   7, 8, 8, 8, 8, 9, 9, 9, 0 };
660
661 static POSITION d_off_x_9[] =
662 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
663   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
664   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
665   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
666
667
668 static POSITION *dist_offsets_y[10] =
669 {
670         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
671         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
672 };
673
674 static POSITION *dist_offsets_x[10] =
675 {
676         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
677         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
678 };
679
680 /*!
681  * @brief モンスターが逃げ込める安全な地点を返す /
682  * Choose a "safe" location near a monster for it to run toward.
683  * @param target_ptr プレーヤーへの参照ポインタ
684  * @param m_idx モンスターの参照ID
685  * @param yp 移動先のマスのY座標を返す参照ポインタ
686  * @param xp 移動先のマスのX座標を返す参照ポインタ
687  * @return 有効なマスがあった場合TRUEを返す
688  * @details
689  * A location is "safe" if it can be reached quickly and the player\n
690  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
691  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
692  * try to run towards corridor openings if they are in a room.\n
693  *\n
694  * This function may take lots of CPU time if lots of monsters are\n
695  * fleeing.\n
696  *\n
697  * Return TRUE if a safe location is available.\n
698  */
699 static bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
700 {
701         floor_type *floor_ptr = target_ptr->current_floor_ptr;
702         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
703
704         POSITION fy = m_ptr->fy;
705         POSITION fx = m_ptr->fx;
706
707         POSITION gy = 0, gx = 0, gdis = 0;
708
709         /* Start with adjacent locations, spread further */
710         for (POSITION d = 1; d < 10; d++)
711         {
712                 /* Get the lists of points with a distance d from (fx, fy) */
713                 POSITION *y_offsets;
714                 y_offsets = dist_offsets_y[d];
715
716                 POSITION *x_offsets;
717                 x_offsets = dist_offsets_x[d];
718
719                 /* Check the locations */
720                 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
721                         dx != 0 || dy != 0;
722                         i++, dx = x_offsets[i], dy = y_offsets[i])
723                 {
724                         POSITION y = fy + dy;
725                         POSITION x = fx + dx;
726
727                         /* Skip illegal locations */
728                         if (!in_bounds(floor_ptr, y, x)) continue;
729
730                         grid_type *g_ptr;
731                         g_ptr = &floor_ptr->grid_array[y][x];
732
733                         /* Skip locations in a wall */
734                         if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == target_ptr->riding) ? CEM_RIDING : 0)) continue;
735
736                         /* Check for "availability" (if monsters can flow) */
737                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
738                         {
739                                 /* Ignore grids very far from the player */
740                                 if (g_ptr->dist == 0) continue;
741
742                                 /* Ignore too-distant grids */
743                                 if (g_ptr->dist > floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
744                         }
745
746                         /* Check for absence of shot (more or less) */
747                         if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
748
749                         /* Calculate distance from player */
750                         POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
751
752                         /* Remember if further than previous */
753                         if (dis <= gdis) continue;
754
755                         gy = y;
756                         gx = x;
757                         gdis = dis;
758                 }
759
760                 /* Check for success */
761                 if (gdis <= 0) continue;
762
763                 /* Good location */
764                 (*yp) = fy - gy;
765                 (*xp) = fx - gx;
766
767                 return TRUE;
768         }
769
770         return FALSE;
771 }
772
773
774 /*!
775  * @brief モンスターが隠れ潜める地点を返す /
776  * Choose a good hiding place near a monster for it to run toward.
777  * @param target_ptr プレーヤーへの参照ポインタ
778  * @param m_idx モンスターの参照ID
779  * @param yp 移動先のマスのY座標を返す参照ポインタ
780  * @param xp 移動先のマスのX座標を返す参照ポインタ
781  * @return 有効なマスがあった場合TRUEを返す
782  * @details
783  * Pack monsters will use this to "ambush" the player and lure him out\n
784  * of corridors into open space so they can swarm him.\n
785  *\n
786  * Return TRUE if a good location is available.\n
787  */
788 static bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
789 {
790         floor_type *floor_ptr = target_ptr->current_floor_ptr;
791         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
792         monster_race *r_ptr = &r_info[m_ptr->r_idx];
793
794         POSITION fy = m_ptr->fy;
795         POSITION fx = m_ptr->fx;
796
797         POSITION gy = 0, gx = 0, gdis = 999;
798
799         /* Start with adjacent locations, spread further */
800         for (POSITION d = 1; d < 10; d++)
801         {
802                 /* Get the lists of points with a distance d from (fx, fy) */
803                 POSITION *y_offsets;
804                 y_offsets = dist_offsets_y[d];
805
806                 POSITION *x_offsets;
807                 x_offsets = dist_offsets_x[d];
808
809                 /* Check the locations */
810                 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
811                         dx != 0 || dy != 0;
812                         i++, dx = x_offsets[i], dy = y_offsets[i])
813                 {
814                         POSITION y = fy + dy;
815                         POSITION x = fx + dx;
816
817                         /* Skip illegal locations */
818                         if (!in_bounds(floor_ptr, y, x)) continue;
819
820                         /* Skip occupied locations */
821                         if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
822
823                         /* Check for hidden, available grid */
824                         if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, fy, fx, y, x, FALSE))
825                                 continue;
826
827                         /* Calculate distance from player */
828                         POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
829
830                         /* Remember if closer than previous */
831                         if (dis < gdis && dis >= 2)
832                         {
833                                 gy = y;
834                                 gx = x;
835                                 gdis = dis;
836                         }
837                 }
838
839                 if (gdis >= 999) continue;
840
841                 *yp = fy - gy;
842                 *xp = fx - gx;
843
844                 return TRUE;
845         }
846
847         return FALSE;
848 }
849
850
851 /*!
852  * @brief モンスターの移動方向を返す /
853  * Choose "logical" directions for monster movement
854  * @param target_ptr プレーヤーへの参照ポインタ
855  * @param m_idx モンスターの参照ID
856  * @param mm 移動方向を返す方向IDの参照ポインタ
857  * @return 有効方向があった場合TRUEを返す
858  */
859 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
860 {
861         floor_type *floor_ptr = target_ptr->current_floor_ptr;
862         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
863         monster_race *r_ptr = &r_info[m_ptr->r_idx];
864         POSITION y = 0, ay, x = 0, ax;
865         int move_val = 0;
866         POSITION y2 = target_ptr->y;
867         POSITION x2 = target_ptr->x;
868         bool done = FALSE;
869         bool will_run = mon_will_run(target_ptr, m_idx);
870         grid_type *g_ptr;
871         bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
872         bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall));
873
874         /* Counter attack to an enemy monster */
875         if (!will_run && m_ptr->target_y)
876         {
877                 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
878
879                 /* The monster must be an enemy, and in LOS */
880                 if (t_m_idx &&
881                         are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
882                         los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
883                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
884                 {
885                         /* Extract the "pseudo-direction" */
886                         y = m_ptr->fy - m_ptr->target_y;
887                         x = m_ptr->fx - m_ptr->target_x;
888                         done = TRUE;
889                 }
890         }
891
892         if (!done && !will_run && is_hostile(m_ptr) &&
893                 (r_ptr->flags1 & RF1_FRIENDS) &&
894                 ((los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x)) ||
895                 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
896         {
897                 /*
898                  * Animal packs try to get the player out of corridors
899                  * (...unless they can move through walls -- TY)
900                  */
901                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
902                         !(r_ptr->flags2 & RF2_KILL_WALL))
903                 {
904                         int i, room = 0;
905
906                         /* Count room grids next to player */
907                         for (i = 0; i < 8; i++)
908                         {
909                                 int xx = target_ptr->x + ddx_ddd[i];
910                                 int yy = target_ptr->y + ddy_ddd[i];
911
912                                 if (!in_bounds2(floor_ptr, yy, xx)) continue;
913
914                                 g_ptr = &floor_ptr->grid_array[yy][xx];
915
916                                 /* Check grid */
917                                 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
918                                 {
919                                         /* One more room grid */
920                                         room++;
921                                 }
922                         }
923                         if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
924                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
925
926                         /* Not in a room and strong player */
927                         if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
928                                 (target_ptr->mhp + target_ptr->msp))
929                         {
930                                 /* Find hiding place */
931                                 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
932                         }
933                 }
934
935                 /* Monster groups try to surround the player */
936                 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
937                 {
938                         int i;
939
940                         /* Find an empty square near the player to fill */
941                         for (i = 0; i < 8; i++)
942                         {
943                                 /* Pick squares near player (semi-randomly) */
944                                 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
945                                 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
946
947                                 /* Already there? */
948                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
949                                 {
950                                         /* Attack the player */
951                                         y2 = target_ptr->y;
952                                         x2 = target_ptr->x;
953
954                                         break;
955                                 }
956
957                                 if (!in_bounds2(floor_ptr, y2, x2)) continue;
958
959                                 /* Ignore filled grids */
960                                 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
961
962                                 /* Try to fill this hole */
963                                 break;
964                         }
965
966                         /* Extract the new "pseudo-direction" */
967                         y = m_ptr->fy - y2;
968                         x = m_ptr->fx - x2;
969
970                         done = TRUE;
971                 }
972         }
973
974         if (!done)
975         {
976                 /* Flow towards the player */
977                 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
978
979                 /* Extract the "pseudo-direction" */
980                 y = m_ptr->fy - y2;
981                 x = m_ptr->fx - x2;
982
983                 /* Not done */
984         }
985
986         /* Apply fear if possible and necessary */
987         if (is_pet(m_ptr) && will_run)
988         {
989                 /* XXX XXX Not very "smart" */
990                 y = (-y), x = (-x);
991         }
992         else
993         {
994                 if (!done && will_run)
995                 {
996                         int tmp_x = (-x);
997                         int tmp_y = (-y);
998
999                         /* Try to find safe place */
1000                         if (find_safety(target_ptr, m_idx, &y, &x))
1001                         {
1002                                 /* Attempt to avoid the player */
1003                                 if (!no_flow)
1004                                 {
1005                                         /* Adjust movement */
1006                                         if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x)) done = TRUE;
1007                                 }
1008                         }
1009
1010                         if (!done)
1011                         {
1012                                 /* This is not a very "smart" method XXX XXX */
1013                                 y = tmp_y;
1014                                 x = tmp_x;
1015                         }
1016                 }
1017         }
1018
1019
1020         /* Check for no move */
1021         if (!x && !y) return FALSE;
1022
1023
1024         /* Extract the "absolute distances" */
1025         ax = ABS(x);
1026         ay = ABS(y);
1027
1028         /* Do something weird */
1029         if (y < 0) move_val += 8;
1030         if (x > 0) move_val += 4;
1031
1032         /* Prevent the diamond maneuvre */
1033         if (ay > (ax << 1)) move_val += 2;
1034         else if (ax > (ay << 1)) move_val++;
1035
1036         /* Extract some directions */
1037         switch (move_val)
1038         {
1039         case 0:
1040                 mm[0] = 9;
1041                 if (ay > ax)
1042                 {
1043                         mm[1] = 8;
1044                         mm[2] = 6;
1045                         mm[3] = 7;
1046                         mm[4] = 3;
1047                 }
1048                 else
1049                 {
1050                         mm[1] = 6;
1051                         mm[2] = 8;
1052                         mm[3] = 3;
1053                         mm[4] = 7;
1054                 }
1055                 break;
1056         case 1:
1057         case 9:
1058                 mm[0] = 6;
1059                 if (y < 0)
1060                 {
1061                         mm[1] = 3;
1062                         mm[2] = 9;
1063                         mm[3] = 2;
1064                         mm[4] = 8;
1065                 }
1066                 else
1067                 {
1068                         mm[1] = 9;
1069                         mm[2] = 3;
1070                         mm[3] = 8;
1071                         mm[4] = 2;
1072                 }
1073                 break;
1074         case 2:
1075         case 6:
1076                 mm[0] = 8;
1077                 if (x < 0)
1078                 {
1079                         mm[1] = 9;
1080                         mm[2] = 7;
1081                         mm[3] = 6;
1082                         mm[4] = 4;
1083                 }
1084                 else
1085                 {
1086                         mm[1] = 7;
1087                         mm[2] = 9;
1088                         mm[3] = 4;
1089                         mm[4] = 6;
1090                 }
1091                 break;
1092         case 4:
1093                 mm[0] = 7;
1094                 if (ay > ax)
1095                 {
1096                         mm[1] = 8;
1097                         mm[2] = 4;
1098                         mm[3] = 9;
1099                         mm[4] = 1;
1100                 }
1101                 else
1102                 {
1103                         mm[1] = 4;
1104                         mm[2] = 8;
1105                         mm[3] = 1;
1106                         mm[4] = 9;
1107                 }
1108                 break;
1109         case 5:
1110         case 13:
1111                 mm[0] = 4;
1112                 if (y < 0)
1113                 {
1114                         mm[1] = 1;
1115                         mm[2] = 7;
1116                         mm[3] = 2;
1117                         mm[4] = 8;
1118                 }
1119                 else
1120                 {
1121                         mm[1] = 7;
1122                         mm[2] = 1;
1123                         mm[3] = 8;
1124                         mm[4] = 2;
1125                 }
1126                 break;
1127         case 8:
1128                 mm[0] = 3;
1129                 if (ay > ax)
1130                 {
1131                         mm[1] = 2;
1132                         mm[2] = 6;
1133                         mm[3] = 1;
1134                         mm[4] = 9;
1135                 }
1136                 else
1137                 {
1138                         mm[1] = 6;
1139                         mm[2] = 2;
1140                         mm[3] = 9;
1141                         mm[4] = 1;
1142                 }
1143                 break;
1144         case 10:
1145         case 14:
1146                 mm[0] = 2;
1147                 if (x < 0)
1148                 {
1149                         mm[1] = 3;
1150                         mm[2] = 1;
1151                         mm[3] = 6;
1152                         mm[4] = 4;
1153                 }
1154                 else
1155                 {
1156                         mm[1] = 1;
1157                         mm[2] = 3;
1158                         mm[3] = 4;
1159                         mm[4] = 6;
1160                 }
1161                 break;
1162         case 12:
1163                 mm[0] = 1;
1164                 if (ay > ax)
1165                 {
1166                         mm[1] = 2;
1167                         mm[2] = 4;
1168                         mm[3] = 3;
1169                         mm[4] = 7;
1170                 }
1171                 else
1172                 {
1173                         mm[1] = 4;
1174                         mm[2] = 2;
1175                         mm[3] = 7;
1176                         mm[4] = 3;
1177                 }
1178                 break;
1179         }
1180
1181         /* Wants to move... */
1182         return TRUE;
1183 }
1184
1185
1186 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1187 {
1188         return !have_flag(f_ptr->flags, FF_GLASS) ||
1189                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1190                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1191 }
1192
1193
1194 /*!
1195  * @brief モンスター単体の1ターン行動処理メインルーチン /
1196  * Process a monster
1197  * @param target_ptr プレーヤーへの参照ポインタ
1198  * @param m_idx 行動モンスターの参照ID
1199  * @return なし
1200  * @details
1201  * The monster is known to be within 100 grids of the player\n
1202  *\n
1203  * In several cases, we directly update the monster lore\n
1204  *\n
1205  * Note that a monster is only allowed to "reproduce" if there\n
1206  * are a limited number of "reproducing" monsters on the current\n
1207  * level.  This should prevent the level from being "swamped" by\n
1208  * reproducing monsters.  It also allows a large mass of mice to\n
1209  * prevent a louse from multiplying, but this is a small price to\n
1210  * pay for a simple multiplication method.\n
1211  *\n
1212  * XXX Monster fear is slightly odd, in particular, monsters will\n
1213  * fixate on opening a door even if they cannot open it.  Actually,\n
1214  * the same thing happens to normal monsters when they hit a door\n
1215  *\n
1216  * In addition, monsters which *cannot* open or bash\n
1217  * down a door will still stand there trying to open it...\n
1218  *\n
1219  * XXX Technically, need to check for monster in the way\n
1220  * combined with that monster being in a wall (or door?)\n
1221  *\n
1222  * A "direction" of "5" means "pick a random direction".\n
1223  */
1224 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1225 {
1226         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1227         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1228         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1229
1230         int i, d;
1231         DIRECTION mm[8];
1232
1233         grid_type *g_ptr;
1234         feature_type *f_ptr;
1235         monster_type *y_ptr;
1236
1237         bool do_turn;
1238         bool do_move;
1239         bool do_view;
1240         bool must_alter_to_move;
1241
1242         bool did_open_door;
1243         bool did_bash_door;
1244         bool did_take_item;
1245         bool did_kill_item;
1246         bool did_move_body;
1247         bool did_pass_wall;
1248         bool did_kill_wall;
1249         bool can_cross;
1250         bool aware = TRUE;
1251
1252         bool fear, dead;
1253         bool is_riding_mon = (m_idx == target_ptr->riding);
1254         bool see_m = is_seen(m_ptr);
1255
1256         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1257         {
1258                 if (rakuba(target_ptr, 0, TRUE))
1259                 {
1260 #ifdef JP
1261                         msg_print("地面に落とされた。");
1262 #else
1263                         GAME_TEXT m_name[MAX_NLEN];
1264                         monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1265                         msg_format("You have fallen from %s.", m_name);
1266 #endif
1267                 }
1268         }
1269
1270         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1271         {
1272                 choose_new_monster(target_ptr, m_idx, FALSE, 0);
1273                 r_ptr = &r_info[m_ptr->r_idx];
1274         }
1275
1276         /* Players hidden in shadow are almost imperceptable. -LM- */
1277         if (target_ptr->special_defense & NINJA_S_STEALTH)
1278         {
1279                 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1280                 if (target_ptr->monlite) tmp /= 3;
1281                 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1282                 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1283                 /* Low-level monsters will find it difficult to locate the player. */
1284                 if (randint0(tmp) > (r_ptr->level + 20)) aware = FALSE;
1285         }
1286
1287         if (vanish_summoned_children(target_ptr, m_idx, see_m)) return;
1288         if (process_quantum_effect(target_ptr,m_idx, see_m)) return;
1289
1290         if (m_ptr->r_idx == MON_SHURYUUDAN)
1291         {
1292                 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1293                 if (dead) return;
1294         }
1295
1296         if (runaway_monster(target_ptr, m_idx, is_riding_mon, see_m)) return;
1297
1298         awake_monster(target_ptr, m_idx);
1299
1300         /* Handle "stun" */
1301         if (MON_STUNNED(m_ptr))
1302         {
1303                 /* Sometimes skip move */
1304                 if (one_in_(2)) return;
1305         }
1306
1307         if (is_riding_mon)
1308         {
1309                 target_ptr->update |= (PU_BONUS);
1310         }
1311
1312         process_angar(target_ptr, m_idx, see_m);
1313
1314         /* Get the origin */
1315         POSITION oy = m_ptr->fy;
1316         POSITION ox = m_ptr->fx;
1317
1318         if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1319
1320         process_special(target_ptr, m_idx);
1321         process_speak_sound(target_ptr, m_idx, oy, ox, aware);
1322         cast_spell(target_ptr, m_idx, aware);
1323
1324         /* Hack -- Assume no movement */
1325         mm[0] = mm[1] = mm[2] = mm[3] = 0;
1326         mm[4] = mm[5] = mm[6] = mm[7] = 0;
1327
1328         if (!decide_monster_movement_direction(target_ptr, mm, m_idx, aware)) return;
1329
1330         /* Assume nothing */
1331         do_turn = FALSE;
1332         do_move = FALSE;
1333         do_view = FALSE;
1334         must_alter_to_move = FALSE;
1335
1336         /* Assume nothing */
1337         did_open_door = FALSE;
1338         did_bash_door = FALSE;
1339         did_take_item = FALSE;
1340         did_kill_item = FALSE;
1341         did_move_body = FALSE;
1342         did_pass_wall = FALSE;
1343         did_kill_wall = FALSE;
1344
1345         /* Take a zero-terminated array of "directions" */
1346         for (i = 0; mm[i]; i++)
1347         {
1348                 /* Get the direction */
1349                 d = mm[i];
1350
1351                 /* Hack -- allow "randomized" motion */
1352                 if (d == 5) d = ddd[randint0(8)];
1353
1354                 /* Get the destination */
1355                 POSITION ny = oy + ddy[d];
1356                 POSITION nx = ox + ddx[d];
1357
1358                 /* Ignore locations off of edge */
1359                 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1360
1361                 /* Access that grid */
1362                 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1363                 f_ptr = &f_info[g_ptr->feat];
1364                 can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1365
1366                 /* Access that grid's contents */
1367                 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1368
1369                 /* Hack -- player 'in' wall */
1370                 if (player_bold(target_ptr, ny, nx))
1371                 {
1372                         do_move = TRUE;
1373                 }
1374
1375                 /* Possibly a monster to attack */
1376                 else if (g_ptr->m_idx)
1377                 {
1378                         do_move = TRUE;
1379                 }
1380
1381                 /* Monster destroys walls (and doors) */
1382                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1383                         (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1384                         have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1385                         check_hp_for_feat_destruction(f_ptr, m_ptr))
1386                 {
1387                         /* Eat through walls/doors/rubble */
1388                         do_move = TRUE;
1389                         if (!can_cross) must_alter_to_move = TRUE;
1390
1391                         /* Monster destroyed a wall (later) */
1392                         did_kill_wall = TRUE;
1393                 }
1394
1395                 /* Floor is open? */
1396                 else if (can_cross)
1397                 {
1398                         /* Go ahead and move */
1399                         do_move = TRUE;
1400
1401                         /* Monster moves through walls (and doors) */
1402                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || target_ptr->pass_wall) &&
1403                                 have_flag(f_ptr->flags, FF_CAN_PASS))
1404                         {
1405                                 /* Monster went through a wall */
1406                                 did_pass_wall = TRUE;
1407                         }
1408                 }
1409
1410                 /* Handle doors and secret doors */
1411                 else if (is_closed_door(target_ptr, g_ptr->feat))
1412                 {
1413                         bool may_bash = TRUE;
1414
1415                         /* Assume no move allowed */
1416                         do_move = FALSE;
1417
1418                         /* Creature can open doors. */
1419                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1420                                 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1421                         {
1422                                 /* Closed doors */
1423                                 if (!f_ptr->power)
1424                                 {
1425                                         /* The door is open */
1426                                         did_open_door = TRUE;
1427
1428                                         /* Do not bash the door */
1429                                         may_bash = FALSE;
1430
1431                                         do_turn = TRUE;
1432                                 }
1433
1434                                 /* Locked doors (not jammed) */
1435                                 else
1436                                 {
1437                                         /* Try to unlock it */
1438                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
1439                                         {
1440                                                 /* Unlock the door */
1441                                                 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1442
1443                                                 /* Do not bash the door */
1444                                                 may_bash = FALSE;
1445
1446                                                 do_turn = TRUE;
1447                                         }
1448                                 }
1449                         }
1450
1451                         /* Stuck doors -- attempt to bash them down if allowed */
1452                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1453                                 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1454                         {
1455                                 /* Attempt to Bash */
1456                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1457                                 {
1458                                         if (have_flag(f_ptr->flags, FF_GLASS))
1459                                                 msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
1460                                         else
1461                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1462
1463                                         /* Disturb (sometimes) */
1464                                         if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1465
1466                                         /* The door was bashed open */
1467                                         did_bash_door = TRUE;
1468
1469                                         /* Hack -- fall into doorway */
1470                                         do_move = TRUE;
1471                                         must_alter_to_move = TRUE;
1472                                 }
1473                         }
1474
1475
1476                         /* Deal with doors in the way */
1477                         if (did_open_door || did_bash_door)
1478                         {
1479                                 /* Break down the door */
1480                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(target_ptr, g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1481                                 {
1482                                         cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1483
1484                                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1485                                         {
1486                                                 target_ptr->update |= (PU_FLOW);
1487                                                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1488                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1489
1490                                                 return;
1491                                         }
1492                                 }
1493
1494                                 /* Open the door */
1495                                 else
1496                                 {
1497                                         cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1498                                 }
1499
1500                                 f_ptr = &f_info[g_ptr->feat];
1501
1502                                 /* Handle viewable doors */
1503                                 do_view = TRUE;
1504                         }
1505                 }
1506
1507                 /* Hack -- check for Glyph of Warding */
1508                 if (do_move && is_glyph_grid(g_ptr) &&
1509                         !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1510                 {
1511                         /* Assume no move allowed */
1512                         do_move = FALSE;
1513
1514                         /* Break the ward */
1515                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1516                         {
1517                                 /* Describe observable breakage */
1518                                 if (g_ptr->info & CAVE_MARK)
1519                                 {
1520                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1521                                 }
1522
1523                                 /* Forget the rune */
1524                                 g_ptr->info &= ~(CAVE_MARK);
1525
1526                                 /* Break the rune */
1527                                 g_ptr->info &= ~(CAVE_OBJECT);
1528                                 g_ptr->mimic = 0;
1529
1530                                 /* Allow movement */
1531                                 do_move = TRUE;
1532
1533                                 note_spot(target_ptr, ny, nx);
1534                         }
1535                 }
1536                 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1537                         !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1538                 {
1539                         /* Assume no move allowed */
1540                         do_move = FALSE;
1541
1542                         /* Break the ward */
1543                         if (!is_pet(m_ptr))
1544                         {
1545                                 /* Break the ward */
1546                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1547                                 {
1548                                         /* Describe observable breakage */
1549                                         if (g_ptr->info & CAVE_MARK)
1550                                         {
1551                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1552                                                 project(target_ptr, 0, 2, ny, nx, 2 * (target_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1553                                         }
1554                                 }
1555                                 else
1556                                 {
1557                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1558                                 }
1559
1560                                 /* Forget the rune */
1561                                 g_ptr->info &= ~(CAVE_MARK);
1562
1563                                 /* Break the rune */
1564                                 g_ptr->info &= ~(CAVE_OBJECT);
1565                                 g_ptr->mimic = 0;
1566
1567                                 note_spot(target_ptr, ny, nx);
1568                                 lite_spot(target_ptr, ny, nx);
1569
1570                                 if (!monster_is_valid(m_ptr)) return;
1571                                 /* Allow movement */
1572                                 do_move = TRUE;
1573                         }
1574                 }
1575
1576                 /* The player is in the way */
1577                 if (do_move && player_bold(target_ptr, ny, nx))
1578                 {
1579                         /* Some monsters never attack */
1580                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
1581                         {
1582                                 /* Hack -- memorize lack of attacks */
1583                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
1584
1585                                 /* Do not move */
1586                                 do_move = FALSE;
1587                         }
1588
1589                         /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1590                         if (do_move && (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
1591                         {
1592                                 if (!MON_CONFUSED(m_ptr))
1593                                 {
1594                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
1595                                         else
1596                                         {
1597                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1598                                         }
1599                                 }
1600                         }
1601
1602                         /* The player is in the way.  Attack him. */
1603                         if (do_move)
1604                         {
1605                                 if (!target_ptr->riding || one_in_(2))
1606                                 {
1607                                         /* Do the attack */
1608                                         (void)make_attack_normal(target_ptr, m_idx);
1609
1610                                         /* Do not move */
1611                                         do_move = FALSE;
1612
1613                                         /* Took a turn */
1614                                         do_turn = TRUE;
1615                                 }
1616                         }
1617                 }
1618
1619                 /* A monster is in the way */
1620                 if (do_move && g_ptr->m_idx)
1621                 {
1622                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
1623
1624                         /* Assume no movement */
1625                         do_move = FALSE;
1626
1627                         /* Attack 'enemies' */
1628                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
1629                                 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
1630                                 can_cross && (g_ptr->m_idx != target_ptr->riding)) ||
1631                                 are_enemies(target_ptr, m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
1632                         {
1633                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
1634                                 {
1635                                         if (r_ptr->flags2 & RF2_KILL_BODY)
1636                                         {
1637                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
1638                                         }
1639
1640                                         /* attack */
1641                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
1642                                         {
1643                                                 if (monst_attack_monst(target_ptr, m_idx, g_ptr->m_idx)) return;
1644
1645                                                 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1646                                                 else if (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
1647                                                 {
1648                                                         if (MON_CONFUSED(m_ptr)) return;
1649                                                         else if (r_ptr->flags2 & RF2_STUPID)
1650                                                         {
1651                                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1652                                                                 return;
1653                                                         }
1654                                                 }
1655                                         }
1656                                 }
1657                         }
1658
1659                         /* Push past weaker monsters (unless leaving a wall) */
1660                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
1661                                 (r_ptr->mexp > z_ptr->mexp) &&
1662                                 can_cross && (g_ptr->m_idx != target_ptr->riding) &&
1663                                 monster_can_cross_terrain(target_ptr, target_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
1664                         {
1665                                 /* Allow movement */
1666                                 do_move = TRUE;
1667
1668                                 /* Monster pushed past another monster */
1669                                 did_move_body = TRUE;
1670
1671                                 /* Wake up the moved monster */
1672                                 (void)set_monster_csleep(target_ptr, g_ptr->m_idx, 0);
1673
1674                                 /* Message */
1675                         }
1676                 }
1677
1678                 if (is_riding_mon)
1679                 {
1680                         if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) do_move = FALSE;
1681                 }
1682
1683                 if (did_kill_wall && do_move)
1684                 {
1685                         if (one_in_(GRINDNOISE))
1686                         {
1687                                 if (have_flag(f_ptr->flags, FF_GLASS))
1688                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
1689                                 else
1690                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
1691                         }
1692
1693                         cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
1694
1695                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1696                         {
1697                                 target_ptr->update |= (PU_FLOW);
1698                                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1699                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1700
1701                                 return;
1702                         }
1703
1704                         f_ptr = &f_info[g_ptr->feat];
1705
1706                         /* Note changes to viewable region */
1707                         do_view = TRUE;
1708                         do_turn = TRUE;
1709                 }
1710
1711                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1712                 {
1713                         if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
1714                         {
1715                                 /* Assume no move allowed */
1716                                 do_move = FALSE;
1717                         }
1718                 }
1719
1720                 /*
1721                  * Check if monster can cross terrain
1722                  * This is checked after the normal attacks
1723                  * to allow monsters to attack an enemy,
1724                  * even if it can't enter the terrain.
1725                  */
1726                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
1727                 {
1728                         /* Assume no move allowed */
1729                         do_move = FALSE;
1730                 }
1731
1732                 /* Some monsters never move */
1733                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1734                 {
1735                         /* Hack -- memorize lack of moves */
1736                         if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1737
1738                         /* Do not move */
1739                         do_move = FALSE;
1740                 }
1741
1742                 /* Creature has been allowed move */
1743                 if (!do_move)
1744                 {
1745                         if (do_turn) break;
1746                         continue;
1747                 }
1748
1749                 do_turn = TRUE;
1750
1751                 if (have_flag(f_ptr->flags, FF_TREE))
1752                 {
1753                         if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1754                         {
1755                                 m_ptr->energy_need += ENERGY_NEED();
1756                         }
1757                 }
1758
1759                 if (!is_riding_mon)
1760                 {
1761                         /* Hack -- Update the old location */
1762                         target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
1763
1764                         /* Mega-Hack -- move the old monster, if any */
1765                         if (g_ptr->m_idx)
1766                         {
1767                                 /* Move the old monster */
1768                                 y_ptr->fy = oy;
1769                                 y_ptr->fx = ox;
1770
1771                                 /* Update the old monster */
1772                                 update_monster(target_ptr, g_ptr->m_idx, TRUE);
1773                         }
1774
1775                         /* Hack -- Update the new location */
1776                         g_ptr->m_idx = m_idx;
1777
1778                         /* Move the monster */
1779                         m_ptr->fy = ny;
1780                         m_ptr->fx = nx;
1781                         update_monster(target_ptr, m_idx, TRUE);
1782
1783                         lite_spot(target_ptr, oy, ox);
1784                         lite_spot(target_ptr, ny, nx);
1785                 }
1786                 else
1787                 {
1788                         /* sound(SOUND_WALK); */
1789                         if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
1790                 }
1791
1792                 /* Possible disturb */
1793                 if (m_ptr->ml &&
1794                         (disturb_move ||
1795                         (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1796                                 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1797                 {
1798                         if (is_hostile(m_ptr))
1799                                 disturb(target_ptr, FALSE, TRUE);
1800                 }
1801
1802                 /* Take or Kill objects on the floor */
1803                 bool is_takable_or_killable = g_ptr->o_idx > 0;
1804                 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1805                 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1806                 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1807                 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1808                 if (!is_takable_or_killable)
1809                 {
1810                         if (do_turn) break;
1811                         continue;
1812                 }
1813
1814                 OBJECT_IDX this_o_idx, next_o_idx;
1815                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
1816
1817                 /* Scan all objects in the grid */
1818                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1819                 {
1820                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
1821                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
1822                         object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
1823                         next_o_idx = o_ptr->next_o_idx;
1824
1825                         if (do_take)
1826                         {
1827                                 /* Skip gold */
1828                                 if (o_ptr->tval == TV_GOLD) continue;
1829
1830                                 /*
1831                                  * Skip "real" corpses and statues, to avoid extreme
1832                                  * silliness like a novice rogue pockets full of statues
1833                                  * and corpses.
1834                                  */
1835                                 if ((o_ptr->tval == TV_CORPSE) ||
1836                                         (o_ptr->tval == TV_STATUE)) continue;
1837                         }
1838
1839                         /* Extract some flags */
1840                         object_flags(o_ptr, flgs);
1841
1842                         /* Acquire the object name */
1843                         object_desc(target_ptr, o_name, o_ptr, 0);
1844                         monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_HIDDEN);
1845
1846                         /* React to objects that hurt the monster */
1847                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
1848                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
1849                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
1850                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
1851                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
1852                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
1853                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
1854                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
1855                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
1856                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
1857                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
1858                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
1859                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
1860                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
1861                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
1862                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
1863                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
1864                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
1865                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
1866                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
1867                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
1868                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
1869                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
1870
1871                         /* The object cannot be picked up by the monster */
1872                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
1873                                 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
1874                         {
1875                                 /* Only give a message for "take_item" */
1876                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
1877                                 {
1878                                         did_take_item = TRUE;
1879
1880                                         /* Describe observable situations */
1881                                         if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
1882                                         {
1883                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
1884                                         }
1885                                 }
1886                         }
1887
1888                         /* Pick up the item */
1889                         else if (do_take)
1890                         {
1891                                 did_take_item = TRUE;
1892
1893                                 /* Describe observable situations */
1894                                 if (player_can_see_bold(target_ptr, ny, nx))
1895                                 {
1896                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
1897                                 }
1898
1899                                 /* Excise the object */
1900                                 excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
1901
1902                                 /* Forget mark */
1903                                 o_ptr->marked &= OM_TOUCHED;
1904
1905                                 /* Forget location */
1906                                 o_ptr->iy = o_ptr->ix = 0;
1907
1908                                 /* Memorize monster */
1909                                 o_ptr->held_m_idx = m_idx;
1910
1911                                 /* Build a stack */
1912                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
1913
1914                                 /* Carry object */
1915                                 m_ptr->hold_o_idx = this_o_idx;
1916                         }
1917
1918                         /* Destroy the item if not a pet */
1919                         else if (!is_pet(m_ptr))
1920                         {
1921                                 did_kill_item = TRUE;
1922
1923                                 /* Describe observable situations */
1924                                 if (player_has_los_bold(target_ptr, ny, nx))
1925                                 {
1926                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
1927                                 }
1928
1929                                 delete_object_idx(target_ptr, this_o_idx);
1930                         }
1931                 }
1932
1933                 if (do_turn) break;
1934         }
1935
1936         /*
1937          *  Forward movements failed, but now received LOS attack!
1938          *  Try to flow by smell.
1939          */
1940         if (target_ptr->no_flowed && i > 2 && m_ptr->target_y)
1941                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1942
1943         /* If we haven't done anything, try casting a spell again */
1944         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
1945         {
1946                 /* Try to cast spell again */
1947                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1948                 {
1949                         if (make_attack_spell(m_idx, target_ptr)) return;
1950                 }
1951         }
1952
1953         /* Notice changes in view */
1954         if (do_view)
1955         {
1956                 target_ptr->update |= (PU_FLOW);
1957                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1958         }
1959
1960         /* Notice changes in view */
1961         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
1962                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
1963         {
1964                 target_ptr->update |= (PU_MON_LITE);
1965         }
1966
1967         /* Learn things from observable monster */
1968         if (is_original_ap_and_seen(target_ptr, m_ptr))
1969         {
1970                 /* Monster opened a door */
1971                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
1972
1973                 /* Monster bashed a door */
1974                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1975
1976                 /* Monster tried to pick something up */
1977                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
1978
1979                 /* Monster tried to crush something */
1980                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
1981
1982                 /* Monster pushed past another monster */
1983                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
1984
1985                 /* Monster passed through a wall */
1986                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
1987
1988                 /* Monster destroyed a wall */
1989                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1990         }
1991
1992         bool is_battle_determined = !do_turn && !do_move && MON_MONFEAR(m_ptr) && aware;
1993         if (!is_battle_determined) return;
1994
1995         /* No longer afraid */
1996         (void)set_monster_monfear(target_ptr, m_idx, 0);
1997
1998         /* Message if seen */
1999         if (see_m)
2000         {
2001                 GAME_TEXT m_name[MAX_NLEN];
2002                 monster_desc(target_ptr, m_name, m_ptr, 0);
2003                 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2004         }
2005
2006         if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
2007 }
2008
2009
2010 /*!
2011  * @brief 召喚の親元が消滅した時、子供も消滅させる
2012  * @param target_ptr プレーヤーへの参照ポインタ
2013  * @param m_idx モンスターID
2014  * @param see_m モンスターが視界内にいたらTRUE
2015  * @return 召喚モンスターが消滅したらTRUE
2016  */
2017 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2018 {
2019         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2020         if ((m_ptr->parent_m_idx == 0) || target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
2021                 return FALSE;
2022
2023         if (see_m)
2024         {
2025                 GAME_TEXT m_name[MAX_NLEN];
2026                 monster_desc(target_ptr, m_name, m_ptr, 0);
2027                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2028         }
2029
2030         if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2031         {
2032                 GAME_TEXT m_name[MAX_NLEN];
2033                 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
2034                 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2035         }
2036
2037         delete_monster_idx(target_ptr, m_idx);
2038         return TRUE;
2039 }
2040
2041
2042 /*!
2043  * @brief 寝ているモンスターの起床を判定する
2044  * @param target_ptr プレーヤーへの参照ポインタ
2045  * @param m_idx モンスターID
2046  * @return なし
2047  */
2048 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
2049 {
2050         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2051         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2052         if (!MON_CSLEEP(m_ptr)) return;
2053         if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
2054
2055         (void)set_monster_csleep(target_ptr, m_idx, 0);
2056         if (m_ptr->ml)
2057         {
2058                 GAME_TEXT m_name[MAX_NLEN];
2059                 monster_desc(target_ptr, m_name, m_ptr, 0);
2060                 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2061         }
2062
2063         if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2064         {
2065                 r_ptr->r_wake++;
2066         }
2067 }
2068
2069
2070 /*!
2071  * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
2072  * @param target_ptr プレーヤーへの参照ポインタ
2073  * @param m_idx モンスターID
2074  * @param see_m モンスターが視界内にいたらTRUE
2075  * @return なし
2076  */
2077 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2078 {
2079         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2080         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2081         bool gets_angry = FALSE;
2082         if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
2083                 gets_angry = TRUE;
2084
2085         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2086                 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
2087         {
2088                 gets_angry = TRUE;
2089         }
2090
2091         if (target_ptr->phase_out || !gets_angry) return;
2092
2093         if (is_pet(m_ptr) || see_m)
2094         {
2095                 GAME_TEXT m_name[MAX_NLEN];
2096                 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2097                 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2098         }
2099
2100         set_hostile(target_ptr, m_ptr);
2101 }
2102
2103
2104 /*!
2105  * @brief 量子生物の量子的効果を実行する
2106  * @param target_ptr プレーヤーへの参照ポインタ
2107  * @param m_idx モンスターID
2108  * @param see_m モンスターが視界内にいたらTRUE
2109  * @return モンスターが量子的効果により消滅したらTRUE
2110  */
2111 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2112 {
2113         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2114         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2115         if ((r_ptr->flags2 & RF2_QUANTUM) == 0) return FALSE;
2116         if (!randint0(2)) return FALSE;
2117         if (randint0((m_idx % 100) + 10)) return FALSE;
2118
2119         bool can_disappear = (r_ptr->flags1 & RF1_UNIQUE) == 0;
2120         can_disappear &= (r_ptr->flags1 & RF1_QUESTOR) == 0;
2121         if (can_disappear)
2122         {
2123                 vanish_nonunique(target_ptr, m_idx, see_m);
2124                 return TRUE;
2125         }
2126         
2127         produce_quantum_effect(target_ptr, m_idx, see_m);
2128         return FALSE;
2129 }
2130
2131
2132 /*!
2133  * @brief ユニークでない量子生物を消滅させる
2134  * @param target_ptr プレーヤーへの参照ポインタ
2135  * @param m_idx モンスターID
2136  * @param see_m モンスターが視界内にいたらTRUE
2137  * @return なし
2138  */
2139 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2140 {
2141         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2142         if (see_m)
2143         {
2144                 GAME_TEXT m_name[MAX_NLEN];
2145                 monster_desc(target_ptr, m_name, m_ptr, 0);
2146                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2147         }
2148
2149         monster_death(target_ptr, m_idx, FALSE);
2150         delete_monster_idx(target_ptr, m_idx);
2151         if (is_pet(m_ptr) && !(m_ptr->ml))
2152         {
2153                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2154         }
2155 }
2156
2157
2158 /*!
2159  * @brief 量子生物ユニークの量子的効果 (ショート・テレポートまたは距離10のテレポート・アウェイ)を実行する
2160  * @param target_ptr プレーヤーへの参照ポインタ
2161  * @param m_idx モンスターID
2162  * @param see_m モンスターが視界内にいたらTRUE
2163  * @return なし
2164  * @details
2165  * プレーヤーが量子生物を観測しているか、量子生物がプレーヤーを観測している場合、互いの相対的な位置を確定させる
2166  * 波動関数の収縮はテレポートではないので反テレポート無効
2167  * todo パターンは収縮どころか拡散しているが、この際気にしてはいけない
2168  */
2169 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2170 {
2171         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2172         bool coherent = los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x);
2173         if (!see_m && !coherent) return;
2174
2175         if (see_m)
2176         {
2177                 GAME_TEXT m_name[MAX_NLEN];
2178                 monster_desc(target_ptr, m_name, m_ptr, 0);
2179                 msg_format(_("%sは量子的効果を起こした!", "%^s produced a decoherence!"), m_name);
2180         }
2181         else
2182         {
2183                 msg_print(_("量子的効果が起こった!", "A decoherence was produced!"));
2184         }
2185
2186         bool target = one_in_(2);
2187         const int blink = 32 * 5 + 4;
2188         if (target)
2189         {
2190                 (void)monspell_to_monster(target_ptr, blink, m_ptr->fy, m_ptr->fx, m_idx, m_idx);
2191
2192         }
2193         else
2194         {
2195                 teleport_player_away(m_idx, target_ptr, 10, TRUE);
2196         }
2197 }
2198
2199
2200 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
2201 {
2202         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2203         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2204         if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
2205         if (m_ptr->r_idx != MON_OHMU) return;
2206         if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
2207         if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
2208
2209         int count = 0;
2210         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2211         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2212
2213         for (int k = 0; k < A_MAX; k++)
2214         {
2215                 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
2216                 {
2217                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2218                 }
2219         }
2220
2221         if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2222 }
2223
2224
2225 /*!
2226  * @brief モンスターを喋らせたり足音を立てたりする
2227  * @param target_ptr プレーヤーへの参照ポインタ
2228  * @param 
2229  * @param
2230  * @param
2231  * @param aware 起きていればTRUE
2232  * @return なし
2233  */
2234 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
2235 {
2236         if (target_ptr->phase_out) return;
2237
2238         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2239         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2240         if (m_ptr->ap_r_idx == MON_CYBER &&
2241                 one_in_(CYBERNOISE) &&
2242                 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2243         {
2244                 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
2245                 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2246         }
2247
2248         if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
2249                 !one_in_(SPEAK_CHANCE) ||
2250                 !player_has_los_bold(target_ptr, oy, ox) ||
2251                 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
2252                 return;
2253
2254         GAME_TEXT m_name[MAX_NLEN];
2255         char monmessage[1024];
2256         concptr filename;
2257
2258         if (m_ptr->ml)
2259                 monster_desc(target_ptr, m_name, m_ptr, 0);
2260         else
2261                 strcpy(m_name, _("それ", "It"));
2262
2263         if (MON_MONFEAR(m_ptr))
2264                 filename = _("monfear_j.txt", "monfear.txt");
2265         else if (is_pet(m_ptr))
2266                 filename = _("monpet_j.txt", "monpet.txt");
2267         else if (is_friendly(m_ptr))
2268                 filename = _("monfrien_j.txt", "monfrien.txt");
2269         else
2270                 filename = _("monspeak_j.txt", "monspeak.txt");
2271
2272         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2273         {
2274                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2275         }
2276 }
2277
2278
2279 /*!
2280  * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
2281  * @param target_ptr プレーヤーへの参照ポインタ
2282  * @param m_idx モンスターID
2283  * @param oy 分裂元モンスターのY座標
2284  * @param ox 分裂元モンスターのX座標
2285  * @return 実際に分裂したらTRUEを返す
2286  */
2287 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
2288 {
2289         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2290         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2291         if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) && (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
2292                 return FALSE;
2293
2294         int k = 0;
2295         for (POSITION y = oy - 1; y <= oy + 1; y++)
2296         {
2297                 for (POSITION x = ox - 1; x <= ox + 1; x++)
2298                 {
2299                         if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
2300                         if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
2301                 }
2302         }
2303
2304         if (multiply_barrier(target_ptr, m_idx)) k = 8;
2305
2306         if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2307         {
2308                 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2309                 {
2310                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
2311                         {
2312                                 r_ptr->r_flags2 |= (RF2_MULTIPLY);
2313                         }
2314
2315                         return TRUE;
2316                 }
2317         }
2318
2319         return FALSE;
2320 }
2321
2322
2323 /*!
2324  * @brief モンスターの移動パターンを決定する
2325  * @param target_ptr プレーヤーへの参照ポインタ
2326  * @param mm 移動方向
2327  * @param m_idx モンスターID
2328  * @param aware 起きていればTRUE
2329  * @return 移動先が存在すればTRUE
2330  */
2331 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
2332 {
2333         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2334         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2335
2336         if (MON_CONFUSED(m_ptr) || !aware)
2337         {
2338                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2339                 return TRUE;
2340         }
2341
2342         if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
2343         {
2344                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2345
2346                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2347                 return TRUE;
2348         }
2349
2350         if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
2351         {
2352                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2353
2354                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2355                 return TRUE;
2356         }
2357
2358          if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
2359          {
2360                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2361
2362                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2363                 return TRUE;
2364          }
2365
2366         if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2367         {
2368                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2369                 return TRUE;
2370         }
2371
2372         if (is_pet(m_ptr))
2373         {
2374                 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
2375                 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
2376                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2377                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2378                 if (!get_enemy_dir(target_ptr, m_idx, mm))
2379                 {
2380                         if (avoid || lonely || distant)
2381                         {
2382                                 POSITION dis = target_ptr->pet_follow_distance;
2383                                 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
2384                                 {
2385                                         target_ptr->pet_follow_distance = PET_SEEK_DIST;
2386                                 }
2387
2388                                 (void)get_moves(target_ptr, m_idx, mm);
2389                                 target_ptr->pet_follow_distance = (s16b)dis;
2390                         }
2391                 }
2392
2393                 return TRUE;
2394         }
2395
2396         if (!is_hostile(m_ptr))
2397         {
2398                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2399                 get_enemy_dir(target_ptr, m_idx, mm);
2400                 return TRUE;
2401         }
2402
2403         if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
2404
2405         return TRUE;
2406 }
2407
2408
2409 /*!
2410  * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
2411  * @param target_ptr プレーヤーへの参照ポインタ
2412  * @param m_idx モンスターID
2413  * @param is_riding_mon 騎乗状態ならばTRUE
2414  * @param see_m モンスターが視界内にいたらTRUE
2415  * @return モンスターがフロアから消えたらTRUE
2416  */
2417 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m)
2418 {
2419         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2420         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2421         bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
2422         can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
2423         can_runaway &= !target_ptr->phase_out;
2424         if (!can_runaway) return FALSE;
2425
2426         static int riding_pinch = 0;
2427
2428         if (m_ptr->hp >= m_ptr->maxhp / 3)
2429         {
2430                 /* Reset the counter */
2431                 if (is_riding_mon) riding_pinch = 0;
2432                 
2433                 return FALSE;
2434         }
2435
2436         GAME_TEXT m_name[MAX_NLEN];
2437         monster_desc(target_ptr, m_name, m_ptr, 0);
2438         if (is_riding_mon && riding_pinch < 2)
2439         {
2440                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2441                         "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
2442                 riding_pinch++;
2443                 disturb(target_ptr, TRUE, TRUE);
2444                 return FALSE;
2445         }
2446
2447         if (is_riding_mon)
2448         {
2449                 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2450                 if (rakuba(target_ptr, -1, FALSE))
2451                 {
2452                         msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2453                 }
2454         }
2455
2456         if (see_m)
2457         {
2458                 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) &&
2459                         player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x))
2460                 {
2461                         msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2462                 }
2463
2464                 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
2465                 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2466         }
2467
2468         if (is_riding_mon && rakuba(target_ptr, -1, FALSE))
2469         {
2470                 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2471         }
2472
2473         check_quest_completion(target_ptr, m_ptr);
2474         delete_monster_idx(target_ptr, m_idx);
2475         return TRUE;
2476 }
2477
2478
2479 /*!
2480  * @brief モンスターに魔法を試行させる
2481  * @param target_ptr プレーヤーへの参照ポインタ
2482  * @param m_idx モンスターID
2483  * @param aware 起きていればTRUE
2484  * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
2485  */
2486 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
2487 {
2488         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2489         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2490         if ((r_ptr->freq_spell == 0) || !randint1(100) <= r_ptr->freq_spell)
2491                 return FALSE;
2492
2493         bool counterattack = FALSE;
2494         if (m_ptr->target_y)
2495         {
2496                 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2497                 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
2498                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2499                 {
2500                         counterattack = TRUE;
2501                 }
2502         }
2503
2504         if (counterattack)
2505         {
2506                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2507                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2508         }
2509         else
2510         {
2511                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2512                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2513         }
2514
2515         return FALSE;
2516 }
2517
2518
2519 /*!
2520  * @brief 全モンスターのターン管理メインルーチン /
2521  * Process all the "live" monsters, once per game turn.
2522  * @return なし
2523  * @details
2524  * During each game current game turn, we scan through the list of all the "live" monsters,\n
2525  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2526  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2527  *\n
2528  * Note that monsters can never move in the monster array (except when the\n
2529  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2530  *\n
2531  * This function is responsible for at least half of the processor time\n
2532  * on a normal system with a "normal" amount of monsters and a player doing\n
2533  * normal things.\n
2534  *\n
2535  * When the player is resting, virtually 90% of the processor time is spent\n
2536  * in this function, and its children, "process_monster()" and "make_move()".\n
2537  *\n
2538  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2539  * especially when the player is running.\n
2540  *\n
2541  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2542  * monsters while they are still being "born".  A monster is "fresh" only\n
2543  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2544  * determine if the monster is yet to be processed during the game turn.\n
2545  *\n
2546  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2547  * move before any "nasty" monsters get to use their spell attacks.\n
2548  *\n
2549  * Note that when the "knowledge" about the currently tracked monster\n
2550  * changes (flags, attacks, spells), we induce a redraw of the monster\n
2551  * recall window.\n
2552  */
2553 void process_monsters(player_type *target_ptr)
2554 {
2555         BIT_FLAGS old_r_flags1 = 0L;
2556         BIT_FLAGS old_r_flags2 = 0L;
2557         BIT_FLAGS old_r_flags3 = 0L;
2558         BIT_FLAGS old_r_flags4 = 0L;
2559         BIT_FLAGS old_r_flags5 = 0L;
2560         BIT_FLAGS old_r_flags6 = 0L;
2561         BIT_FLAGS old_r_flagsr = 0L;
2562
2563         byte old_r_blows0 = 0;
2564         byte old_r_blows1 = 0;
2565         byte old_r_blows2 = 0;
2566         byte old_r_blows3 = 0;
2567
2568         /* Clear monster fighting indicator */
2569         floor_type *floor_ptr = target_ptr->current_floor_ptr;
2570         floor_ptr->monster_noise = FALSE;
2571
2572         /* Memorize old race */
2573         MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
2574
2575         /* Acquire knowledge */
2576         byte old_r_cast_spell = 0;
2577         if (target_ptr->monster_race_idx)
2578         {
2579                 /* Acquire current monster */
2580                 monster_race *r_ptr;
2581                 r_ptr = &r_info[target_ptr->monster_race_idx];
2582
2583                 /* Memorize flags */
2584                 old_r_flags1 = r_ptr->r_flags1;
2585                 old_r_flags2 = r_ptr->r_flags2;
2586                 old_r_flags3 = r_ptr->r_flags3;
2587                 old_r_flags4 = r_ptr->r_flags4;
2588                 old_r_flags5 = r_ptr->r_flags5;
2589                 old_r_flags6 = r_ptr->r_flags6;
2590                 old_r_flagsr = r_ptr->r_flagsr;
2591
2592                 /* Memorize blows */
2593                 old_r_blows0 = r_ptr->r_blows[0];
2594                 old_r_blows1 = r_ptr->r_blows[1];
2595                 old_r_blows2 = r_ptr->r_blows[2];
2596                 old_r_blows3 = r_ptr->r_blows[3];
2597
2598                 /* Memorize castings */
2599                 old_r_cast_spell = r_ptr->r_cast_spell;
2600         }
2601
2602         /* Process the monsters (backwards) */
2603         bool test;
2604         for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
2605         {
2606                 monster_type *m_ptr;
2607                 monster_race *r_ptr;
2608                 m_ptr = &floor_ptr->m_list[i];
2609                 r_ptr = &r_info[m_ptr->r_idx];
2610
2611                 /* Handle "leaving" */
2612                 if (target_ptr->leaving) break;
2613
2614                 /* Ignore "dead" monsters */
2615                 if (!monster_is_valid(m_ptr)) continue;
2616
2617                 if (target_ptr->wild_mode) continue;
2618
2619
2620                 /* Handle "fresh" monsters */
2621                 if (m_ptr->mflag & MFLAG_BORN)
2622                 {
2623                         /* No longer "fresh" */
2624                         m_ptr->mflag &= ~(MFLAG_BORN);
2625
2626                         /* Skip */
2627                         continue;
2628                 }
2629
2630                 /* Hack -- Require proximity */
2631                 if (m_ptr->cdis >= AAF_LIMIT) continue;
2632
2633                 POSITION fx = m_ptr->fx;
2634                 POSITION fy = m_ptr->fy;
2635
2636                 /* Flow by smell is allowed */
2637                 if (!target_ptr->no_flowed)
2638                 {
2639                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2640                 }
2641
2642                 /* Assume no move */
2643                 test = FALSE;
2644
2645                 /* Handle "sensing radius" */
2646                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2647                 {
2648                         /* We can "sense" the player */
2649                         test = TRUE;
2650                 }
2651
2652                 /* Handle "sight" and "aggravation" */
2653                 else if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2654                         (player_has_los_bold(target_ptr, fy, fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2655                 {
2656                         /* We can "see" or "feel" the player */
2657                         test = TRUE;
2658                 }
2659
2660                 else if (m_ptr->target_y) test = TRUE;
2661
2662                 /* Do nothing */
2663                 if (!test) continue;
2664
2665                 SPEED speed;
2666                 if (target_ptr->riding == i)
2667                         speed = target_ptr->pspeed;
2668                 else
2669                 {
2670                         speed = m_ptr->mspeed;
2671
2672                         /* Monsters move quickly in Nightmare mode */
2673                         if (ironman_nightmare) speed += 5;
2674
2675                         if (MON_FAST(m_ptr)) speed += 10;
2676                         if (MON_SLOW(m_ptr)) speed -= 10;
2677                 }
2678
2679                 /* Give this monster some energy */
2680                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2681
2682                 /* Not enough energy to move */
2683                 if (m_ptr->energy_need > 0) continue;
2684
2685                 /* Use up "some" energy */
2686                 m_ptr->energy_need += ENERGY_NEED();
2687
2688                 /* Save global index */
2689                 hack_m_idx = i;
2690
2691                 /* Process the monster */
2692                 process_monster(target_ptr, i);
2693
2694                 reset_target(m_ptr);
2695
2696                 /* Give up flow_by_smell when it might useless */
2697                 if (target_ptr->no_flowed && one_in_(3))
2698                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
2699
2700                 /* Hack -- notice death or departure */
2701                 if (!target_ptr->playing || target_ptr->is_dead) break;
2702
2703                 /* Notice leaving */
2704                 if (target_ptr->leaving) break;
2705         }
2706
2707         /* Reset global index */
2708         hack_m_idx = 0;
2709
2710
2711         /* Tracking a monster race (the same one we were before) */
2712         if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2713                 return;
2714
2715         /* Acquire monster race */
2716         monster_race *r_ptr;
2717         r_ptr = &r_info[target_ptr->monster_race_idx];
2718
2719         /* Check for knowledge change */
2720         if ((old_r_flags1 != r_ptr->r_flags1) ||
2721                 (old_r_flags2 != r_ptr->r_flags2) ||
2722                 (old_r_flags3 != r_ptr->r_flags3) ||
2723                 (old_r_flags4 != r_ptr->r_flags4) ||
2724                 (old_r_flags5 != r_ptr->r_flags5) ||
2725                 (old_r_flags6 != r_ptr->r_flags6) ||
2726                 (old_r_flagsr != r_ptr->r_flagsr) ||
2727                 (old_r_blows0 != r_ptr->r_blows[0]) ||
2728                 (old_r_blows1 != r_ptr->r_blows[1]) ||
2729                 (old_r_blows2 != r_ptr->r_blows[2]) ||
2730                 (old_r_blows3 != r_ptr->r_blows[3]) ||
2731                 (old_r_cast_spell != r_ptr->r_cast_spell))
2732         {
2733                 target_ptr->window |= (PW_MONSTER);
2734         }
2735 }