OSDN Git Service

[Refactor] #38997 compact_monsters() にplayer_type * 引数追加 / Added player_type * argume...
[hengband/hengband.git] / src / monster2.c
1 /*!
2  * @file monster2.c
3  * @brief モンスター処理 / misc code for monsters
4  * @date 2014/07/08
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7  * This software may be copied and distributed for educational, research,
8  * and not for profit purposes provided that this copyright and statement
9  * are included in all such copies.  Other copyrights may also apply.
10  * 2014 Deskull rearranged comment for Doxygen.
11  */
12
13 #include "angband.h"
14 #include "util.h"
15 #include "core.h"
16
17 #include "cmd-dump.h"
18 #include "cmd-pet.h"
19 #include "dungeon.h"
20 #include "floor.h"
21 #include "object-flavor.h"
22 #include "monsterrace-hook.h"
23 #include "monster-status.h"
24 #include "monster.h"
25 #include "spells.h"
26 #include "spells-summon.h"
27 #include "quest.h"
28 #include "grid.h"
29 #include "player-move.h"
30 #include "player-status.h"
31 #include "player-race.h"
32 #include "player-class.h"
33 #include "player-personality.h"
34 #include "wild.h"
35 #include "warning.h"
36 #include "monster-spell.h"
37 #include "files.h"
38 #include "view-mainwindow.h"
39 #include "world.h"
40 #include "monsterrace.h"
41 #include "creature.h"
42 #include "targeting.h"
43 #include "realm-song.h"
44 #include "melee.h"
45
46 #define HORDE_NOGOOD 0x01 /*!< (未実装フラグ)HORDE生成でGOODなモンスターの生成を禁止する? */
47 #define HORDE_NOEVIL 0x02 /*!< (未実装フラグ)HORDE生成でEVILなモンスターの生成を禁止する? */
48
49 MONSTER_IDX hack_m_idx = 0;     /* Hack -- see "process_monsters()" */
50 MONSTER_IDX hack_m_idx_ii = 0;
51
52 bool is_friendly_idx(player_type *player_ptr, MONSTER_IDX m_idx);
53
54 /*!
55  * @brief モンスターの目標地点をセットする / Set the target of counter attack
56  * @param m_ptr モンスターの参照ポインタ
57  * @param y 目標y座標
58  * @param x 目標x座標
59  * @return なし
60  */
61 void set_target(monster_type *m_ptr, POSITION y, POSITION x)
62 {
63         m_ptr->target_y = y;
64         m_ptr->target_x = x;
65 }
66
67
68 /*!
69  * @brief モンスターの目標地点をリセットする / Reset the target of counter attack
70  * @param m_ptr モンスターの参照ポインタ
71  * @return なし
72  */
73 void reset_target(monster_type *m_ptr)
74 {
75         set_target(m_ptr, 0, 0);
76 }
77
78
79 /*!
80  * @brief モンスターの真の種族を返す / Extract monster race pointer of a monster's true form
81  * @param m_ptr モンスターの参照ポインタ
82  * @return 本当のモンスター種族参照ポインタ
83  */
84 monster_race *real_r_ptr(monster_type *m_ptr)
85 {
86         return &r_info[real_r_idx(m_ptr)];
87 }
88
89 MONRACE_IDX real_r_idx(monster_type *m_ptr)
90 {
91         monster_race *r_ptr = &r_info[m_ptr->r_idx];
92
93         /* Extract real race */
94         if (m_ptr->mflag2 & MFLAG2_CHAMELEON)
95         {
96                 if (r_ptr->flags1 & RF1_UNIQUE)
97                         return MON_CHAMELEON_K;
98                 else
99                         return MON_CHAMELEON;
100         }
101         else
102         {
103                 return m_ptr->r_idx;
104         }
105 }
106
107
108 /*!
109  * @brief モンスター配列からモンスターを消去する / Delete a monster by index.
110  * @param i 消去するモンスターのID
111  * @return なし
112  * @details
113  * モンスターを削除するとそのモンスターが拾っていたアイテムも同時に削除される。 /
114  * When a monster is deleted, all of its objects are deleted.
115  */
116 void delete_monster_idx(MONSTER_IDX i)
117 {
118         POSITION x, y;
119         floor_type *floor_ptr = p_ptr->current_floor_ptr;
120         monster_type *m_ptr = &floor_ptr->m_list[i];
121         monster_race *r_ptr = &r_info[m_ptr->r_idx];
122         OBJECT_IDX this_o_idx, next_o_idx = 0;
123
124         y = m_ptr->fy;
125         x = m_ptr->fx;
126
127         /* Hack -- Reduce the racial counter */
128         real_r_ptr(m_ptr)->cur_num--;
129
130         /* Hack -- count the number of "reproducers" */
131         if (r_ptr->flags2 & (RF2_MULTIPLY)) floor_ptr->num_repro--;
132
133         if (MON_CSLEEP(m_ptr)) (void)set_monster_csleep(p_ptr, i, 0);
134         if (MON_FAST(m_ptr)) (void)set_monster_fast(p_ptr, i, 0);
135         if (MON_SLOW(m_ptr)) (void)set_monster_slow(p_ptr, i, 0);
136         if (MON_STUNNED(m_ptr)) (void)set_monster_stunned(p_ptr, i, 0);
137         if (MON_CONFUSED(m_ptr)) (void)set_monster_confused(p_ptr, i, 0);
138         if (MON_MONFEAR(m_ptr)) (void)set_monster_monfear(p_ptr, i, 0);
139         if (MON_INVULNER(m_ptr)) (void)set_monster_invulner(p_ptr, i, 0, FALSE);
140
141         /* Hack -- remove target monster */
142         if (i == target_who) target_who = 0;
143
144         /* Hack -- remove tracked monster */
145         if (i == p_ptr->health_who) health_track(p_ptr, 0);
146
147         if (p_ptr->pet_t_m_idx == i) p_ptr->pet_t_m_idx = 0;
148         if (p_ptr->riding_t_m_idx == i) p_ptr->riding_t_m_idx = 0;
149         if (p_ptr->riding == i) p_ptr->riding = 0;
150
151         /* Monster is gone */
152         floor_ptr->grid_array[y][x].m_idx = 0;
153
154         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
155         {
156                 object_type *o_ptr;
157                 o_ptr = &floor_ptr->o_list[this_o_idx];
158                 next_o_idx = o_ptr->next_o_idx;
159
160                 /*
161                  * o_ptr->held_m_idx is needed in delete_object_idx()
162                  * to prevent calling lite_spot()
163                  */
164
165                 delete_object_idx(floor_ptr, this_o_idx);
166         }
167
168         (void)WIPE(m_ptr, monster_type);
169
170         /* Count monsters */
171         floor_ptr->m_cnt--;
172
173         lite_spot(y, x);
174         if (r_ptr->flags7 & (RF7_LITE_MASK | RF7_DARK_MASK))
175         {
176                 p_ptr->update |= (PU_MON_LITE);
177         }
178 }
179
180
181 /*!
182  * @brief モンスター情報を配列内移動する / Move an object from index i1 to index i2 in the object list
183  * @param player_ptr プレーヤーへの参照ポインタ
184  * @param i1 配列移動元添字
185  * @param i2 配列移動先添字
186  * @return なし
187  */
188 static void compact_monsters_aux(player_type *player_ptr, MONSTER_IDX i1, MONSTER_IDX i2)
189 {
190         POSITION y, x;
191         int i;
192         grid_type *g_ptr;
193         monster_type *m_ptr;
194         OBJECT_IDX this_o_idx, next_o_idx = 0;
195
196         /* Do nothing */
197         if (i1 == i2) return;
198
199         /* Old monster */
200         floor_type *floor_ptr = player_ptr->current_floor_ptr;
201         m_ptr = &floor_ptr->m_list[i1];
202
203         y = m_ptr->fy;
204         x = m_ptr->fx;
205
206         /* Cave grid */
207         g_ptr = &floor_ptr->grid_array[y][x];
208
209         g_ptr->m_idx = i2;
210
211         /* Repair objects being carried by monster */
212         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
213         {
214                 object_type *o_ptr;
215                 o_ptr = &floor_ptr->o_list[this_o_idx];
216                 next_o_idx = o_ptr->next_o_idx;
217
218                 /* Reset monster pointer */
219                 o_ptr->held_m_idx = i2;
220         }
221
222         /* Hack -- Update the target */
223         if (target_who == i1) target_who = i2;
224
225         /* Hack -- Update the target */
226         if (player_ptr->pet_t_m_idx == i1) player_ptr->pet_t_m_idx = i2;
227         if (player_ptr->riding_t_m_idx == i1) player_ptr->riding_t_m_idx = i2;
228
229         /* Hack -- Update the riding */
230         if (player_ptr->riding == i1) player_ptr->riding = i2;
231
232         /* Hack -- Update the health bar */
233         if (player_ptr->health_who == i1) health_track(player_ptr, i2);
234
235         /* Hack -- Update parent index */
236         if (is_pet(m_ptr))
237         {
238                 for (i = 1; i < floor_ptr->m_max; i++)
239                 {
240                         monster_type *m2_ptr = &floor_ptr->m_list[i];
241
242                         if (m2_ptr->parent_m_idx == i1)
243                                 m2_ptr->parent_m_idx = i2;
244                 }
245         }
246
247         /* Structure copy */
248         (void)COPY(&floor_ptr->m_list[i2], &floor_ptr->m_list[i1], monster_type);
249
250         /* Wipe the hole */
251         (void)WIPE(&floor_ptr->m_list[i1], monster_type);
252
253         for (i = 0; i < MAX_MTIMED; i++)
254         {
255                 int mproc_idx = get_mproc_idx(floor_ptr, i1, i);
256                 if (mproc_idx >= 0) floor_ptr->mproc_list[i][mproc_idx] = i2;
257         }
258 }
259
260
261 /*!
262  * @brief モンスター情報配列を圧縮する / Compact and Reorder the monster list
263  * @param player_ptr プレーヤーへの参照ポインタ
264  * @param size 圧縮後のモンスター件数目標
265  * @return なし
266  * @details
267  * This function can be very dangerous, use with caution!
268  *
269  * When actually "compacting" monsters, we base the saving throw
270  * on a combination of monster level, distance from player, and
271  * current "desperation".
272  *
273  * After "compacting" (if needed), we "reorder" the monsters into a more
274  * compact order, and we reset the allocation info, and the "live" array.
275  */
276 void compact_monsters(player_type *player_ptr, int size)
277 {
278         MONSTER_IDX i;
279         int num, cnt;
280         int cur_lev, cur_dis, chance;
281
282         /* Message (only if compacting) */
283         if (size) msg_print(_("モンスター情報を圧縮しています...", "Compacting monsters..."));
284
285         /* Compact at least 'size' objects */
286         floor_type *floor_ptr = player_ptr->current_floor_ptr;
287         for (num = 0, cnt = 1; num < size; cnt++)
288         {
289                 /* Get more vicious each iteration */
290                 cur_lev = 5 * cnt;
291
292                 /* Get closer each iteration */
293                 cur_dis = 5 * (20 - cnt);
294
295                 /* Check all the monsters */
296                 for (i = 1; i < floor_ptr->m_max; i++)
297                 {
298                         monster_type *m_ptr = &floor_ptr->m_list[i];
299
300                         monster_race *r_ptr = &r_info[m_ptr->r_idx];
301
302                         /* Paranoia -- skip "dead" monsters */
303                         if (!monster_is_valid(m_ptr)) continue;
304
305                         /* Hack -- High level monsters start out "immune" */
306                         if (r_ptr->level > cur_lev) continue;
307
308                         if (i == player_ptr->riding) continue;
309
310                         /* Ignore nearby monsters */
311                         if ((cur_dis > 0) && (m_ptr->cdis < cur_dis)) continue;
312
313                         /* Saving throw chance */
314                         chance = 90;
315
316                         /* Only compact "Quest" Monsters in emergencies */
317                         if ((r_ptr->flags1 & (RF1_QUESTOR)) && (cnt < 1000)) chance = 100;
318
319                         /* Try not to compact Unique Monsters */
320                         if (r_ptr->flags1 & (RF1_UNIQUE)) chance = 100;
321
322                         /* All monsters get a saving throw */
323                         if (randint0(100) < chance) continue;
324
325                         if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
326                         {
327                                 GAME_TEXT m_name[MAX_NLEN];
328                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
329                                 exe_write_diary(player_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_COMPACT, m_name);
330                         }
331
332                         delete_monster_idx(i);
333
334                         /* Count the monster */
335                         num++;
336                 }
337         }
338
339
340         /* Excise dead monsters (backwards!) */
341         for (i = floor_ptr->m_max - 1; i >= 1; i--)
342         {
343                 /* Get the i'th monster */
344                 monster_type *m_ptr = &floor_ptr->m_list[i];
345
346                 /* Skip real monsters */
347                 if (m_ptr->r_idx) continue;
348
349                 /* Move last monster into open hole */
350                 compact_monsters_aux(player_ptr, floor_ptr->m_max - 1, i);
351
352                 /* Compress "floor_ptr->m_max" */
353                 floor_ptr->m_max--;
354         }
355 }
356
357
358 /*!
359  * @brief プレイヤーのフロア離脱に伴う全モンスター配列の消去 / Delete/Remove all the monsters when the player leaves the level
360  * @return なし
361  * @details
362  * This is an efficient method of simulating multiple calls to the
363  * "delete_monster()" function, with no visual effects.
364  */
365 void wipe_m_list(void)
366 {
367         int i;
368
369         /* Hack -- if Banor or Lupart dies, stay another dead */
370         if (!r_info[MON_BANORLUPART].max_num)
371         {
372                 if (r_info[MON_BANOR].max_num)
373                 {
374                         r_info[MON_BANOR].max_num = 0;
375                         r_info[MON_BANOR].r_pkills++;
376                         r_info[MON_BANOR].r_akills++;
377                         if (r_info[MON_BANOR].r_tkills < MAX_SHORT) r_info[MON_BANOR].r_tkills++;
378                 }
379                 if (r_info[MON_LUPART].max_num)
380                 {
381                         r_info[MON_LUPART].max_num = 0;
382                         r_info[MON_LUPART].r_pkills++;
383                         r_info[MON_LUPART].r_akills++;
384                         if (r_info[MON_LUPART].r_tkills < MAX_SHORT) r_info[MON_LUPART].r_tkills++;
385                 }
386         }
387
388         /* Delete all the monsters */
389         floor_type *floor_ptr = p_ptr->current_floor_ptr;
390         for (i = floor_ptr->m_max - 1; i >= 1; i--)
391         {
392                 monster_type *m_ptr = &floor_ptr->m_list[i];
393                 if (!monster_is_valid(m_ptr)) continue;
394
395                 /* Monster is gone */
396                 floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].m_idx = 0;
397
398                 (void)WIPE(m_ptr, monster_type);
399
400         }
401
402         /*
403          * Wiping racial counters of all monsters and incrementing of racial
404          * counters of monsters in party_mon[] are required to prevent multiple
405          * generation of unique monster who is the minion of player.
406          */
407
408          /* Hack -- Wipe the racial counter of all monster races */
409         for (i = 1; i < max_r_idx; i++) r_info[i].cur_num = 0;
410
411         /* Reset "floor_ptr->m_max" */
412         floor_ptr->m_max = 1;
413
414         /* Reset "floor_ptr->m_cnt" */
415         floor_ptr->m_cnt = 0;
416
417         /* Reset "floor_ptr->mproc_max[]" */
418         for (i = 0; i < MAX_MTIMED; i++) floor_ptr->mproc_max[i] = 0;
419
420         /* Hack -- reset "reproducer" count */
421         floor_ptr->num_repro = 0;
422
423         /* Hack -- no more target */
424         target_who = 0;
425         p_ptr->pet_t_m_idx = 0;
426         p_ptr->riding_t_m_idx = 0;
427
428         health_track(p_ptr, 0);
429 }
430
431
432 /*!
433  * @brief モンスター配列の空きを探す / Acquires and returns the index of a "free" monster.
434  * @return 利用可能なモンスター配列の添字
435  * @details
436  * This routine should almost never fail, but it *can* happen.
437  */
438 MONSTER_IDX m_pop(void)
439 {
440         MONSTER_IDX i;
441
442         /* Normal allocation */
443         floor_type *floor_ptr = p_ptr->current_floor_ptr;
444         if (floor_ptr->m_max < current_world_ptr->max_m_idx)
445         {
446                 /* Access the next hole */
447                 i = floor_ptr->m_max;
448
449                 /* Expand the array */
450                 floor_ptr->m_max++;
451
452                 /* Count monsters */
453                 floor_ptr->m_cnt++;
454
455                 /* Return the index */
456                 return (i);
457         }
458
459         /* Recycle dead monsters */
460         for (i = 1; i < floor_ptr->m_max; i++)
461         {
462                 monster_type *m_ptr;
463                 m_ptr = &floor_ptr->m_list[i];
464
465                 /* Skip live monsters */
466                 if (m_ptr->r_idx) continue;
467
468                 /* Count monsters */
469                 floor_ptr->m_cnt++;
470
471                 /* Use this monster */
472                 return (i);
473         }
474
475         /* Warn the player (except during dungeon creation) */
476         if (current_world_ptr->character_dungeon) msg_print(_("モンスターが多すぎる!", "Too many monsters!"));
477
478         /* Try not to crash */
479         return 0;
480 }
481
482
483
484
485 /*!
486  * @var summon_specific_type
487  * @brief 召喚条件を指定するグローバル変数 / Hack -- the "type" of the current "summon specific"
488  * @todo summon_specific_typeグローバル変数の除去と関数引数への代替を行う
489  */
490 static int summon_specific_type = 0;
491
492
493 /*!
494  * @var summon_specific_who
495  * @brief 召喚を行ったプレイヤーあるいはモンスターのIDを示すグローバル変数 / Hack -- the index of the summoning monster
496  * @todo summon_specific_who グローバル変数の除去と関数引数への代替を行う
497  */
498 static int summon_specific_who = -1;
499
500 /*!
501  * @var summon_unique_okay
502  * @brief 召喚対象にユニークを含めるかを示すグローバル変数 / summoning unique enable
503  * @todo summon_unique_okay グローバル変数の除去と関数引数への代替を行う
504  */
505 static bool summon_unique_okay = FALSE;
506
507 /*!
508  * @brief 指定されたモンスター種族がsummon_specific_typeで指定された召喚条件に合うかどうかを返す
509  * @return 召喚条件が一致するならtrue
510  * @details
511  */
512 static bool summon_specific_aux(MONRACE_IDX summoner_idx, MONRACE_IDX r_idx)
513 {
514         monster_race *r_ptr = &r_info[r_idx];
515         int okay = FALSE;
516
517         /* Check our requirements */
518         switch (summon_specific_type)
519         {
520         case SUMMON_ANT:
521         {
522                 okay = (r_ptr->d_char == 'a');
523                 break;
524         }
525
526         case SUMMON_SPIDER:
527         {
528                 okay = (r_ptr->d_char == 'S');
529                 break;
530         }
531
532         case SUMMON_HOUND:
533         {
534                 okay = ((r_ptr->d_char == 'C') || (r_ptr->d_char == 'Z'));
535                 break;
536         }
537
538         case SUMMON_HYDRA:
539         {
540                 okay = (r_ptr->d_char == 'M');
541                 break;
542         }
543
544         case SUMMON_ANGEL:
545         {
546                 okay = (r_ptr->d_char == 'A' && ((r_ptr->flags3 & RF3_EVIL) || (r_ptr->flags3 & RF3_GOOD)));
547                 break;
548         }
549
550         case SUMMON_DEMON:
551         {
552                 okay = (r_ptr->flags3 & RF3_DEMON);
553                 break;
554         }
555
556         case SUMMON_UNDEAD:
557         {
558                 okay = (r_ptr->flags3 & RF3_UNDEAD);
559                 break;
560         }
561
562         case SUMMON_DRAGON:
563         {
564                 okay = (r_ptr->flags3 & RF3_DRAGON);
565                 break;
566         }
567
568         case SUMMON_HI_UNDEAD:
569         {
570                 okay = ((r_ptr->d_char == 'L') ||
571                         (r_ptr->d_char == 'V') ||
572                         (r_ptr->d_char == 'W'));
573                 break;
574         }
575
576         case SUMMON_HI_DRAGON:
577         {
578                 okay = (r_ptr->d_char == 'D');
579                 break;
580         }
581
582         case SUMMON_HI_DEMON:
583         {
584                 okay = (((r_ptr->d_char == 'U') ||
585                         (r_ptr->d_char == 'H') ||
586                         (r_ptr->d_char == 'B')) &&
587                         (r_ptr->flags3 & RF3_DEMON)) ? TRUE : FALSE;
588                 break;
589         }
590
591         case SUMMON_AMBERITES:
592         {
593                 okay = (r_ptr->flags3 & (RF3_AMBERITE)) ? TRUE : FALSE;
594                 break;
595         }
596
597         case SUMMON_UNIQUE:
598         {
599                 okay = (r_ptr->flags1 & (RF1_UNIQUE)) ? TRUE : FALSE;
600                 break;
601         }
602
603         case SUMMON_MOLD:
604         {
605                 okay = (r_ptr->d_char == 'm');
606                 break;
607         }
608         case SUMMON_BAT:
609         {
610                 okay = (r_ptr->d_char == 'b');
611                 break;
612         }
613         case SUMMON_QUYLTHULG:
614         {
615                 okay = (r_ptr->d_char == 'Q');
616                 break;
617         }
618
619         case SUMMON_COIN_MIMIC:
620         {
621                 okay = (r_ptr->d_char == '$');
622                 break;
623         }
624
625         case SUMMON_MIMIC:
626         {
627                 okay = ((r_ptr->d_char == '!') ||
628                         (r_ptr->d_char == '?') ||
629                         (r_ptr->d_char == '=') ||
630                         (r_ptr->d_char == '$') ||
631                         (r_ptr->d_char == '|'));
632                 break;
633         }
634
635         case SUMMON_GOLEM:
636         {
637                 okay = (r_ptr->d_char == 'g');
638                 break;
639         }
640
641         case SUMMON_CYBER:
642         {
643                 okay = ((r_ptr->d_char == 'U') &&
644                         (r_ptr->flags4 & RF4_ROCKET));
645                 break;
646         }
647
648         case SUMMON_KIN:
649         {
650                 SYMBOL_CODE summon_kin_type;
651                 if (summoner_idx)
652                 {
653                         summon_kin_type = r_info[summoner_idx].d_char;
654                 }
655                 else
656                 {
657                         summon_kin_type = get_summon_symbol_from_player(p_ptr);
658                 }
659
660                 okay = ((r_ptr->d_char == summon_kin_type) && (r_idx != MON_HAGURE));
661                 break;
662         }
663
664         case SUMMON_DAWN:
665         {
666                 okay = (r_idx == MON_DAWN);
667                 break;
668         }
669
670         case SUMMON_ANIMAL:
671         {
672                 okay = (r_ptr->flags3 & (RF3_ANIMAL));
673                 break;
674         }
675
676         case SUMMON_ANIMAL_RANGER:
677         {
678                 okay = ((r_ptr->flags3 & (RF3_ANIMAL)) &&
679                         (my_strchr("abcflqrwBCHIJKMRS", r_ptr->d_char)) &&
680                         !(r_ptr->flags3 & (RF3_DRAGON)) &&
681                         !(r_ptr->flags3 & (RF3_EVIL)) &&
682                         !(r_ptr->flags3 & (RF3_UNDEAD)) &&
683                         !(r_ptr->flags3 & (RF3_DEMON)) &&
684                         !(r_ptr->flags2 & (RF2_MULTIPLY)) &&
685                         !(r_ptr->flags4 || r_ptr->a_ability_flags1 || r_ptr->a_ability_flags2));
686                 break;
687         }
688
689         case SUMMON_HI_DRAGON_LIVING:
690         {
691                 okay = ((r_ptr->d_char == 'D') && monster_living(r_idx));
692                 break;
693         }
694
695         case SUMMON_LIVING:
696         {
697                 okay = monster_living(r_idx);
698                 break;
699         }
700
701         case SUMMON_PHANTOM:
702         {
703                 okay = (r_idx == MON_PHANTOM_B || r_idx == MON_PHANTOM_W);
704                 break;
705         }
706
707         case SUMMON_BLUE_HORROR:
708         {
709                 okay = (r_idx == MON_BLUE_HORROR);
710                 break;
711         }
712
713         case SUMMON_ELEMENTAL:
714         {
715                 okay = (r_ptr->d_char == 'E');
716                 break;
717         }
718
719         case SUMMON_VORTEX:
720         {
721                 okay = (r_ptr->d_char == 'v');
722                 break;
723         }
724
725         case SUMMON_HYBRID:
726         {
727                 okay = (r_ptr->d_char == 'H');
728                 break;
729         }
730
731         case SUMMON_BIRD:
732         {
733                 okay = (r_ptr->d_char == 'B');
734                 break;
735         }
736
737         case SUMMON_KAMIKAZE:
738         {
739                 int i;
740                 for (i = 0; i < 4; i++)
741                         if (r_ptr->blow[i].method == RBM_EXPLODE) okay = TRUE;
742                 break;
743         }
744
745         case SUMMON_KAMIKAZE_LIVING:
746         {
747                 int i;
748
749                 for (i = 0; i < 4; i++)
750                         if (r_ptr->blow[i].method == RBM_EXPLODE) okay = TRUE;
751                 okay = (okay && monster_living(r_idx));
752                 break;
753         }
754
755         case SUMMON_MANES:
756         {
757                 okay = (r_idx == MON_MANES);
758                 break;
759         }
760
761         case SUMMON_LOUSE:
762         {
763                 okay = (r_idx == MON_LOUSE);
764                 break;
765         }
766
767         case SUMMON_GUARDIANS:
768         {
769                 okay = (r_ptr->flags7 & RF7_GUARDIAN);
770                 break;
771         }
772
773         case SUMMON_KNIGHTS:
774         {
775                 okay = ((r_idx == MON_NOV_PALADIN) ||
776                         (r_idx == MON_NOV_PALADIN_G) ||
777                         (r_idx == MON_PALADIN) ||
778                         (r_idx == MON_W_KNIGHT) ||
779                         (r_idx == MON_ULTRA_PALADIN) ||
780                         (r_idx == MON_KNI_TEMPLAR));
781                 break;
782         }
783
784         case SUMMON_EAGLES:
785         {
786                 okay = (r_ptr->d_char == 'B' &&
787                         (r_ptr->flags8 & RF8_WILD_MOUNTAIN) &&
788                         (r_ptr->flags8 & RF8_WILD_ONLY));
789                 break;
790         }
791
792         case SUMMON_PIRANHAS:
793         {
794                 okay = (r_idx == MON_PIRANHA);
795                 break;
796         }
797
798         case SUMMON_ARMAGE_GOOD:
799         {
800                 okay = (r_ptr->d_char == 'A' && (r_ptr->flags3 & RF3_GOOD));
801                 break;
802         }
803
804         case SUMMON_ARMAGE_EVIL:
805         {
806                 okay = ((r_ptr->flags3 & RF3_DEMON) ||
807                         (r_ptr->d_char == 'A' && (r_ptr->flags3 & RF3_EVIL)));
808                 break;
809         }
810         }
811         /* Since okay is int, "return (okay);" is not correct. */
812         return (bool)(okay ? TRUE : FALSE);
813 }
814
815 /*!
816  * @var chameleon_change_m_idx
817  * @brief カメレオンの変身先モンスターIDを受け渡すためのグローバル変数
818  * @todo 変数渡しの問題などもあるができればchameleon_change_m_idxのグローバル変数を除去し、関数引き渡しに移行すること
819  */
820 static int chameleon_change_m_idx = 0;
821
822
823 /*!
824  * @brief 指定されたモンスター種族がダンジョンの制限にかかるかどうかをチェックする / Some dungeon types restrict the possible monsters.
825  * @param r_idx チェックするモンスター種族ID
826  * @return 召喚条件が一致するならtrue / Return TRUE is the monster is OK and FALSE otherwise
827  */
828 static bool restrict_monster_to_dungeon(DUNGEON_IDX d_idx, MONRACE_IDX r_idx)
829 {
830         dungeon_type *d_ptr = &d_info[d_idx];
831         monster_race *r_ptr = &r_info[r_idx];
832         byte a;
833
834         if (d_ptr->flags1 & DF1_CHAMELEON)
835         {
836                 if (chameleon_change_m_idx) return TRUE;
837         }
838
839         if (d_ptr->flags1 & DF1_NO_MAGIC)
840         {
841                 if (r_idx != MON_CHAMELEON &&
842                         r_ptr->freq_spell &&
843                         !(r_ptr->flags4 & RF4_NOMAGIC_MASK) &&
844                         !(r_ptr->a_ability_flags1 & RF5_NOMAGIC_MASK) &&
845                         !(r_ptr->a_ability_flags2 & RF6_NOMAGIC_MASK))
846                         return FALSE;
847         }
848
849         if (d_ptr->flags1 & DF1_NO_MELEE)
850         {
851                 if (r_idx == MON_CHAMELEON) return TRUE;
852                 if (!(r_ptr->flags4 & (RF4_BOLT_MASK | RF4_BEAM_MASK | RF4_BALL_MASK)) &&
853                         !(r_ptr->a_ability_flags1 & (RF5_BOLT_MASK | RF5_BEAM_MASK | RF5_BALL_MASK | RF5_CAUSE_1 | RF5_CAUSE_2 | RF5_CAUSE_3 | RF5_CAUSE_4 | RF5_MIND_BLAST | RF5_BRAIN_SMASH)) &&
854                         !(r_ptr->a_ability_flags2 & (RF6_BOLT_MASK | RF6_BEAM_MASK | RF6_BALL_MASK)))
855                         return FALSE;
856         }
857
858         floor_type *floor_ptr = p_ptr->current_floor_ptr;
859         if (d_ptr->flags1 & DF1_BEGINNER)
860         {
861                 if (r_ptr->level > floor_ptr->dun_level)
862                         return FALSE;
863         }
864
865         if (d_ptr->special_div >= 64) return TRUE;
866         if (summon_specific_type && !(d_ptr->flags1 & DF1_CHAMELEON)) return TRUE;
867
868         switch (d_ptr->mode)
869         {
870         case DUNGEON_MODE_AND:
871                 if (d_ptr->mflags1)
872                 {
873                         if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
874                                 return FALSE;
875                 }
876                 if (d_ptr->mflags2)
877                 {
878                         if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
879                                 return FALSE;
880                 }
881                 if (d_ptr->mflags3)
882                 {
883                         if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
884                                 return FALSE;
885                 }
886                 if (d_ptr->mflags4)
887                 {
888                         if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
889                                 return FALSE;
890                 }
891                 if (d_ptr->m_a_ability_flags1)
892                 {
893                         if ((d_ptr->m_a_ability_flags1 & r_ptr->a_ability_flags1) != d_ptr->m_a_ability_flags1)
894                                 return FALSE;
895                 }
896                 if (d_ptr->m_a_ability_flags2)
897                 {
898                         if ((d_ptr->m_a_ability_flags2 & r_ptr->a_ability_flags2) != d_ptr->m_a_ability_flags2)
899                                 return FALSE;
900                 }
901                 if (d_ptr->mflags7)
902                 {
903                         if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
904                                 return FALSE;
905                 }
906                 if (d_ptr->mflags8)
907                 {
908                         if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
909                                 return FALSE;
910                 }
911                 if (d_ptr->mflags9)
912                 {
913                         if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
914                                 return FALSE;
915                 }
916                 if (d_ptr->mflagsr)
917                 {
918                         if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
919                                 return FALSE;
920                 }
921                 for (a = 0; a < 5; a++)
922                         if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char)) return FALSE;
923
924                 return TRUE;
925
926         case DUNGEON_MODE_NAND:
927                 if (d_ptr->mflags1)
928                 {
929                         if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
930                                 return TRUE;
931                 }
932                 if (d_ptr->mflags2)
933                 {
934                         if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
935                                 return TRUE;
936                 }
937                 if (d_ptr->mflags3)
938                 {
939                         if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
940                                 return TRUE;
941                 }
942                 if (d_ptr->mflags4)
943                 {
944                         if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
945                                 return TRUE;
946                 }
947                 if (d_ptr->m_a_ability_flags1)
948                 {
949                         if ((d_ptr->m_a_ability_flags1 & r_ptr->a_ability_flags1) != d_ptr->m_a_ability_flags1)
950                                 return TRUE;
951                 }
952                 if (d_ptr->m_a_ability_flags2)
953                 {
954                         if ((d_ptr->m_a_ability_flags2 & r_ptr->a_ability_flags2) != d_ptr->m_a_ability_flags2)
955                                 return TRUE;
956                 }
957                 if (d_ptr->mflags7)
958                 {
959                         if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
960                                 return TRUE;
961                 }
962                 if (d_ptr->mflags8)
963                 {
964                         if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
965                                 return TRUE;
966                 }
967                 if (d_ptr->mflags9)
968                 {
969                         if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
970                                 return TRUE;
971                 }
972                 if (d_ptr->mflagsr)
973                 {
974                         if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
975                                 return TRUE;
976                 }
977                 for (a = 0; a < 5; a++)
978                         if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char)) return TRUE;
979
980                 return FALSE;
981
982         case DUNGEON_MODE_OR:
983                 if (r_ptr->flags1 & d_ptr->mflags1) return TRUE;
984                 if (r_ptr->flags2 & d_ptr->mflags2) return TRUE;
985                 if (r_ptr->flags3 & d_ptr->mflags3) return TRUE;
986                 if (r_ptr->flags4 & d_ptr->mflags4) return TRUE;
987                 if (r_ptr->a_ability_flags1 & d_ptr->m_a_ability_flags1) return TRUE;
988                 if (r_ptr->a_ability_flags2 & d_ptr->m_a_ability_flags2) return TRUE;
989                 if (r_ptr->flags7 & d_ptr->mflags7) return TRUE;
990                 if (r_ptr->flags8 & d_ptr->mflags8) return TRUE;
991                 if (r_ptr->flags9 & d_ptr->mflags9) return TRUE;
992                 if (r_ptr->flagsr & d_ptr->mflagsr) return TRUE;
993                 for (a = 0; a < 5; a++)
994                         if (d_ptr->r_char[a] == r_ptr->d_char) return TRUE;
995
996                 return FALSE;
997
998         case DUNGEON_MODE_NOR:
999                 if (r_ptr->flags1 & d_ptr->mflags1) return FALSE;
1000                 if (r_ptr->flags2 & d_ptr->mflags2) return FALSE;
1001                 if (r_ptr->flags3 & d_ptr->mflags3) return FALSE;
1002                 if (r_ptr->flags4 & d_ptr->mflags4) return FALSE;
1003                 if (r_ptr->a_ability_flags1 & d_ptr->m_a_ability_flags1) return FALSE;
1004                 if (r_ptr->a_ability_flags2 & d_ptr->m_a_ability_flags2) return FALSE;
1005                 if (r_ptr->flags7 & d_ptr->mflags7) return FALSE;
1006                 if (r_ptr->flags8 & d_ptr->mflags8) return FALSE;
1007                 if (r_ptr->flags9 & d_ptr->mflags9) return FALSE;
1008                 if (r_ptr->flagsr & d_ptr->mflagsr) return FALSE;
1009                 for (a = 0; a < 5; a++)
1010                         if (d_ptr->r_char[a] == r_ptr->d_char) return FALSE;
1011
1012                 return TRUE;
1013         }
1014
1015         return TRUE;
1016 }
1017
1018 /*
1019  * Hack -- function hooks to restrict "get_mon_num_prep()" function
1020  */
1021 monsterrace_hook_type get_mon_num_hook;
1022 monsterrace_hook_type get_mon_num2_hook;
1023
1024 /*!
1025  * @brief モンスター生成制限関数最大2つから / Apply a "monster restriction function" to the "monster allocation table"
1026  * @param monster_hook 制限関数1
1027  * @param monster_hook2 制限関数2
1028  * @return エラーコード
1029  */
1030 errr get_mon_num_prep(monsterrace_hook_type monster_hook,
1031         monsterrace_hook_type monster_hook2)
1032 {
1033         int i;
1034
1035         /* Todo: Check the hooks for non-changes */
1036
1037         /* Set the new hooks */
1038         get_mon_num_hook = monster_hook;
1039         get_mon_num2_hook = monster_hook2;
1040
1041         /* Scan the allocation table */
1042         floor_type *floor_ptr = p_ptr->current_floor_ptr;
1043         for (i = 0; i < alloc_race_size; i++)
1044         {
1045                 monster_race *r_ptr;
1046
1047                 /* Get the entry */
1048                 alloc_entry *entry = &alloc_race_table[i];
1049
1050                 entry->prob2 = 0;
1051                 r_ptr = &r_info[entry->index];
1052
1053                 /* Skip monsters which don't pass the restriction */
1054                 if ((get_mon_num_hook && !((*get_mon_num_hook)(entry->index))) ||
1055                         (get_mon_num2_hook && !((*get_mon_num2_hook)(entry->index))))
1056                         continue;
1057
1058                 if (!p_ptr->phase_out && !chameleon_change_m_idx &&
1059                         summon_specific_type != SUMMON_GUARDIANS)
1060                 {
1061                         /* Hack -- don't create questors */
1062                         if (r_ptr->flags1 & RF1_QUESTOR)
1063                                 continue;
1064
1065                         if (r_ptr->flags7 & RF7_GUARDIAN)
1066                                 continue;
1067
1068                         /* Depth Monsters never appear out of depth */
1069                         if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) &&
1070                                 (r_ptr->level > floor_ptr->dun_level))
1071                                 continue;
1072                 }
1073
1074                 /* Accept this monster */
1075                 entry->prob2 = entry->prob1;
1076
1077                 if (floor_ptr->dun_level && (!floor_ptr->inside_quest || is_fixed_quest_idx(floor_ptr->inside_quest)) &&
1078                         !restrict_monster_to_dungeon(p_ptr->dungeon_idx, entry->index) && !p_ptr->phase_out)
1079                 {
1080                         int hoge = entry->prob2 * d_info[p_ptr->dungeon_idx].special_div;
1081                         entry->prob2 = hoge / 64;
1082                         if (randint0(64) < (hoge & 0x3f)) entry->prob2++;
1083                 }
1084         }
1085
1086         /* Success */
1087         return 0;
1088 }
1089
1090
1091 /*!
1092  * @brief 生成モンスター種族を1種生成テーブルから選択する
1093  * @param level 生成階
1094  * @return 選択されたモンスター生成種族
1095  * @details
1096  * Choose a monster race that seems "appropriate" to the given level
1097  *
1098  * This function uses the "prob2" field of the "monster allocation table",
1099  * and various local information, to calculate the "prob3" field of the
1100  * same table, which is then used to choose an "appropriate" monster, in
1101  * a relatively efficient manner.
1102  *
1103  * Note that "town" monsters will *only* be created in the town, and
1104  * "normal" monsters will *never* be created in the town, unless the
1105  * "level" is "modified", for example, by polymorph or summoning.
1106  *
1107  * There is a small chance (1/50) of "boosting" the given depth by
1108  * a small amount (up to four levels), except in the town.
1109  *
1110  * It is (slightly) more likely to acquire a monster of the given level
1111  * than one of a lower level.  This is done by choosing several monsters
1112  * appropriate to the given level and keeping the "hardest" one.
1113  *
1114  * Note that if no monsters are "appropriate", then this function will
1115  * fail, and return zero, but this should *almost* never happen.
1116  */
1117 MONRACE_IDX get_mon_num(DEPTH level)
1118 {
1119         int i, j, p;
1120         MONRACE_IDX r_idx;
1121         long value, total;
1122         monster_race *r_ptr;
1123         alloc_entry *table = alloc_race_table;
1124
1125         int pls_kakuritu, pls_level;
1126         int delay = mysqrt(level * 10000L) + 400L;
1127
1128         pls_kakuritu = MAX(NASTY_MON_MAX, NASTY_MON_BASE - ((current_world_ptr->dungeon_turn / (TURNS_PER_TICK * 5000L) - delay / 10)));
1129         pls_level = MIN(NASTY_MON_PLUS_MAX, 3 + current_world_ptr->dungeon_turn / (TURNS_PER_TICK * 40000L) - delay / 40 + MIN(5, level / 10));
1130
1131         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_MAZE)
1132         {
1133                 pls_kakuritu = MIN(pls_kakuritu / 2, pls_kakuritu - 10);
1134                 if (pls_kakuritu < 2) pls_kakuritu = 2;
1135                 pls_level += 2;
1136                 level += 3;
1137         }
1138
1139         /* Boost the level */
1140         if (!p_ptr->phase_out && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_BEGINNER))
1141         {
1142                 /* Nightmare mode allows more out-of depth monsters */
1143                 if (ironman_nightmare && !randint0(pls_kakuritu))
1144                 {
1145                         /* What a bizarre calculation */
1146                         level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
1147                 }
1148                 else
1149                 {
1150                         /* Occasional "nasty" monster */
1151                         if (!randint0(pls_kakuritu))
1152                         {
1153                                 /* Pick a level bonus */
1154                                 level += pls_level;
1155                         }
1156                 }
1157         }
1158
1159         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
1160         if (level < 0) level = 0;
1161
1162         /* Reset total */
1163         total = 0L;
1164
1165         /* Process probabilities */
1166         for (i = 0; i < alloc_race_size; i++)
1167         {
1168                 /* Monsters are sorted by depth */
1169                 if (table[i].level > level) break;
1170
1171                 /* Default */
1172                 table[i].prob3 = 0;
1173
1174                 /* Access the "r_idx" of the chosen monster */
1175                 r_idx = table[i].index;
1176
1177                 /* Access the actual race */
1178                 r_ptr = &r_info[r_idx];
1179
1180                 if (!p_ptr->phase_out && !chameleon_change_m_idx)
1181                 {
1182                         /* Hack -- "unique" monsters must be "unique" */
1183                         if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
1184                                 (r_ptr->flags7 & (RF7_NAZGUL))) &&
1185                                 (r_ptr->cur_num >= r_ptr->max_num))
1186                         {
1187                                 continue;
1188                         }
1189
1190                         if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
1191                                 (r_ptr->cur_num >= 1))
1192                         {
1193                                 continue;
1194                         }
1195
1196                         if (r_idx == MON_BANORLUPART)
1197                         {
1198                                 if (r_info[MON_BANOR].cur_num > 0) continue;
1199                                 if (r_info[MON_LUPART].cur_num > 0) continue;
1200                         }
1201                 }
1202
1203                 /* Accept */
1204                 table[i].prob3 = table[i].prob2;
1205
1206                 /* Total */
1207                 total += table[i].prob3;
1208         }
1209
1210         /* No legal monsters */
1211         if (total <= 0) return 0;
1212
1213         /* Pick a monster */
1214         value = randint0(total);
1215
1216         /* Find the monster */
1217         for (i = 0; i < alloc_race_size; i++)
1218         {
1219                 /* Found the entry */
1220                 if (value < table[i].prob3) break;
1221
1222                 /* Decrement */
1223                 value = value - table[i].prob3;
1224         }
1225
1226         /* Power boost */
1227         p = randint0(100);
1228
1229         /* Try for a "harder" monster once (50%) or twice (10%) */
1230         if (p < 60)
1231         {
1232                 /* Save old */
1233                 j = i;
1234
1235                 /* Pick a monster */
1236                 value = randint0(total);
1237
1238                 /* Find the monster */
1239                 for (i = 0; i < alloc_race_size; i++)
1240                 {
1241                         /* Found the entry */
1242                         if (value < table[i].prob3) break;
1243
1244                         /* Decrement */
1245                         value = value - table[i].prob3;
1246                 }
1247
1248                 /* Keep the "best" one */
1249                 if (table[i].level < table[j].level) i = j;
1250         }
1251
1252         /* Try for a "harder" monster twice (10%) */
1253         if (p < 10)
1254         {
1255                 /* Save old */
1256                 j = i;
1257
1258                 /* Pick a monster */
1259                 value = randint0(total);
1260
1261                 /* Find the monster */
1262                 for (i = 0; i < alloc_race_size; i++)
1263                 {
1264                         /* Found the entry */
1265                         if (value < table[i].prob3) break;
1266
1267                         /* Decrement */
1268                         value = value - table[i].prob3;
1269                 }
1270
1271                 /* Keep the "best" one */
1272                 if (table[i].level < table[j].level) i = j;
1273         }
1274         return (table[i].index);
1275 }
1276
1277
1278 /*!
1279  * @brief モンスターの呼称を作成する / Build a string describing a monster in some way.
1280  * @param desc 記述出力先の文字列参照ポインタ
1281  * @param m_ptr モンスターの参照ポインタ
1282  * @param mode 呼称オプション
1283  * @return なし
1284  * @details
1285  * We can correctly describe monsters based on their visibility.
1286  * We can force all monsters to be treated as visible or invisible.
1287  * We can build nominatives, objectives, possessives, or reflexives.
1288  * We can selectively pronominalize hidden, visible, or all monsters.
1289  * We can use definite or indefinite descriptions for hidden monsters.
1290  * We can use definite or indefinite descriptions for visible monsters.
1291  *
1292  * Pronominalization involves the gender whenever possible and allowed,
1293  * so that by cleverly requesting pronominalization / visibility, you
1294  * can get messages like "You hit someone.  She screams in agony!".
1295  *
1296  * Reflexives are acquired by requesting Objective plus Possessive.
1297  *
1298  * If no m_ptr arg is given (?), the monster is assumed to be hidden,
1299  * unless the "Assume Visible" mode is requested.
1300  *
1301  * If no r_ptr arg is given, it is extracted from m_ptr and r_info
1302  * If neither m_ptr nor r_ptr is given, the monster is assumed to
1303  * be neuter, singular, and hidden (unless "Assume Visible" is set),
1304  * in which case you may be in trouble... :-)
1305  *
1306  * I am assuming that no monster name is more than 70 characters long,
1307  * so that "char desc[80];" is sufficiently large for any result.
1308  *
1309  * Mode Flags:
1310  *  MD_OBJECTIVE      --> Objective (or Reflexive)
1311  *  MD_POSSESSIVE     --> Possessive (or Reflexive)
1312  *  MD_INDEF_HIDDEN   --> Use indefinites for hidden monsters ("something")
1313  *  MD_INDEF_VISIBLE  --> Use indefinites for visible monsters ("a kobold")
1314  *  MD_PRON_HIDDEN    --> Pronominalize hidden monsters
1315  *  MD_PRON_VISIBLE   --> Pronominalize visible monsters
1316  *  MD_ASSUME_HIDDEN  --> Assume the monster is hidden
1317  *  MD_ASSUME_VISIBLE --> Assume the monster is visible
1318  *  MD_TRUE_NAME      --> Chameleon's true name
1319  *  MD_IGNORE_HALLU   --> Ignore hallucination, and penetrate shape change
1320  *
1321  * Useful Modes:
1322  *  0x00 --> Full nominative name ("the kobold") or "it"
1323  *  MD_INDEF_HIDDEN --> Full nominative name ("the kobold") or "something"
1324  *  MD_ASSUME_VISIBLE --> Genocide resistance name ("the kobold")
1325  *  MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE --> Killing name ("a kobold")
1326  *  MD_PRON_VISIBLE | MD_POSSESSIVE
1327  *    --> Possessive, genderized if visable ("his") or "its"
1328  *  MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE
1329  *    --> Reflexive, genderized if visable ("himself") or "itself"
1330  */
1331 void monster_desc(char *desc, monster_type *m_ptr, BIT_FLAGS mode)
1332 {
1333         concptr            res;
1334         monster_race    *r_ptr;
1335
1336         concptr            name;
1337         char            buf[128];
1338         GAME_TEXT silly_name[1024];
1339         bool            seen, pron;
1340         bool            named = FALSE;
1341
1342         r_ptr = &r_info[m_ptr->ap_r_idx];
1343
1344         /* Mode of MD_TRUE_NAME will reveal Chameleon's true name */
1345         if (mode & MD_TRUE_NAME) name = (r_name + real_r_ptr(m_ptr)->name);
1346         else name = (r_name + r_ptr->name);
1347
1348         /* Are we hallucinating? (Idea from Nethack...) */
1349         if (p_ptr->image && !(mode & MD_IGNORE_HALLU))
1350         {
1351                 if (one_in_(2))
1352                 {
1353                         if (!get_rnd_line(_("silly_j.txt", "silly.txt"), m_ptr->r_idx, silly_name))
1354                                 named = TRUE;
1355                 }
1356
1357                 if (!named)
1358                 {
1359                         monster_race *hallu_race;
1360
1361                         do
1362                         {
1363                                 hallu_race = &r_info[randint1(max_r_idx - 1)];
1364                         } while (!hallu_race->name || (hallu_race->flags1 & RF1_UNIQUE));
1365
1366                         strcpy(silly_name, (r_name + hallu_race->name));
1367                 }
1368
1369                 /* Better not strcpy it, or we could corrupt r_info... */
1370                 name = silly_name;
1371         }
1372
1373         /* Can we "see" it (exists + forced, or visible + not unforced) */
1374         seen = (m_ptr && ((mode & MD_ASSUME_VISIBLE) || (!(mode & MD_ASSUME_HIDDEN) && m_ptr->ml)));
1375
1376         /* Sexed Pronouns (seen and allowed, or unseen and allowed) */
1377         pron = (m_ptr && ((seen && (mode & MD_PRON_VISIBLE)) || (!seen && (mode & MD_PRON_HIDDEN))));
1378
1379
1380         /* First, try using pronouns, or describing hidden monsters */
1381         floor_type *floor_ptr = p_ptr->current_floor_ptr;
1382         if (!seen || pron)
1383         {
1384                 /* an encoding of the monster "sex" */
1385                 int kind = 0x00;
1386
1387                 /* Extract the gender (if applicable) */
1388                 if (r_ptr->flags1 & (RF1_FEMALE)) kind = 0x20;
1389                 else if (r_ptr->flags1 & (RF1_MALE)) kind = 0x10;
1390
1391                 /* Ignore the gender (if desired) */
1392                 if (!m_ptr || !pron) kind = 0x00;
1393
1394
1395                 /* Assume simple result */
1396                 res = _("何か", "it");
1397
1398                 /* Brute force: split on the possibilities */
1399                 switch (kind + (mode & (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE)))
1400                 {
1401                         /* Neuter, or unknown */
1402 #ifdef JP
1403                 case 0x00:                                                    res = "何か"; break;
1404                         case 0x00 + (MD_OBJECTIVE) : res = "何か"; break;
1405                                 case 0x00 + (MD_POSSESSIVE) : res = "何かの"; break;
1406                                         case 0x00 + (MD_POSSESSIVE | MD_OBJECTIVE) : res = "何か自身"; break;
1407                                                 case 0x00 + (MD_INDEF_HIDDEN) : res = "何か"; break;
1408                                                         case 0x00 + (MD_INDEF_HIDDEN | MD_OBJECTIVE) : res = "何か"; break;
1409                                                                 case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE) : res = "何か"; break;
1410                                                                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE) : res = "それ自身"; break;
1411 #else
1412                 case 0x00:                                                    res = "it"; break;
1413                         case 0x00 + (MD_OBJECTIVE) : res = "it"; break;
1414                                 case 0x00 + (MD_POSSESSIVE) : res = "its"; break;
1415                                         case 0x00 + (MD_POSSESSIVE | MD_OBJECTIVE) : res = "itself"; break;
1416                                                 case 0x00 + (MD_INDEF_HIDDEN) : res = "something"; break;
1417                                                         case 0x00 + (MD_INDEF_HIDDEN | MD_OBJECTIVE) : res = "something"; break;
1418                                                                 case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE) : res = "something's"; break;
1419                                                                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE) : res = "itself"; break;
1420 #endif
1421
1422
1423                                                                                 /* Male (assume human if vague) */
1424 #ifdef JP
1425                                                                         case 0x10:                                                    res = "彼"; break;
1426                                                                                 case 0x10 + (MD_OBJECTIVE) : res = "彼"; break;
1427                                                                                         case 0x10 + (MD_POSSESSIVE) : res = "彼の"; break;
1428                                                                                                 case 0x10 + (MD_POSSESSIVE | MD_OBJECTIVE) : res = "彼自身"; break;
1429                                                                                                         case 0x10 + (MD_INDEF_HIDDEN) : res = "誰か"; break;
1430                                                                                                                 case 0x10 + (MD_INDEF_HIDDEN | MD_OBJECTIVE) : res = "誰か"; break;
1431                                                                                                                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE) : res = "誰かの"; break;
1432                                                                                                                                 case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE) : res = "彼自身"; break;
1433 #else
1434                                                                         case 0x10:                                                    res = "he"; break;
1435                                                                                 case 0x10 + (MD_OBJECTIVE) : res = "him"; break;
1436                                                                                         case 0x10 + (MD_POSSESSIVE) : res = "his"; break;
1437                                                                                                 case 0x10 + (MD_POSSESSIVE | MD_OBJECTIVE) : res = "himself"; break;
1438                                                                                                         case 0x10 + (MD_INDEF_HIDDEN) : res = "someone"; break;
1439                                                                                                                 case 0x10 + (MD_INDEF_HIDDEN | MD_OBJECTIVE) : res = "someone"; break;
1440                                                                                                                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE) : res = "someone's"; break;
1441                                                                                                                                 case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE) : res = "himself"; break;
1442 #endif
1443
1444
1445                                                                                                                                         /* Female (assume human if vague) */
1446 #ifdef JP
1447                                                                                                                                 case 0x20:                                                    res = "彼女"; break;
1448                                                                                                                                         case 0x20 + (MD_OBJECTIVE) : res = "彼女"; break;
1449                                                                                                                                                 case 0x20 + (MD_POSSESSIVE) : res = "彼女の"; break;
1450                                                                                                                                                         case 0x20 + (MD_POSSESSIVE | MD_OBJECTIVE) : res = "彼女自身"; break;
1451                                                                                                                                                                 case 0x20 + (MD_INDEF_HIDDEN) : res = "誰か"; break;
1452                                                                                                                                                                         case 0x20 + (MD_INDEF_HIDDEN | MD_OBJECTIVE) : res = "誰か"; break;
1453                                                                                                                                                                                 case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE) : res = "誰かの"; break;
1454                                                                                                                                                                                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE) : res = "彼女自身"; break;
1455 #else
1456                                                                                                                                 case 0x20:                                                    res = "she"; break;
1457                                                                                                                                         case 0x20 + (MD_OBJECTIVE) : res = "her"; break;
1458                                                                                                                                                 case 0x20 + (MD_POSSESSIVE) : res = "her"; break;
1459                                                                                                                                                         case 0x20 + (MD_POSSESSIVE | MD_OBJECTIVE) : res = "herself"; break;
1460                                                                                                                                                                 case 0x20 + (MD_INDEF_HIDDEN) : res = "someone"; break;
1461                                                                                                                                                                         case 0x20 + (MD_INDEF_HIDDEN | MD_OBJECTIVE) : res = "someone"; break;
1462                                                                                                                                                                                 case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE) : res = "someone's"; break;
1463                                                                                                                                                                                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE) : res = "herself"; break;
1464 #endif
1465                 }
1466
1467                 /* Copy the result */
1468                 (void)strcpy(desc, res);
1469         }
1470
1471
1472         /* Handle visible monsters, "reflexive" request */
1473         else if ((mode & (MD_POSSESSIVE | MD_OBJECTIVE)) == (MD_POSSESSIVE | MD_OBJECTIVE))
1474         {
1475                 /* The monster is visible, so use its gender */
1476                 if (r_ptr->flags1 & (RF1_FEMALE)) strcpy(desc, _("彼女自身", "herself"));
1477                 else if (r_ptr->flags1 & (RF1_MALE)) strcpy(desc, _("彼自身", "himself"));
1478                 else strcpy(desc, _("それ自身", "itself"));
1479         }
1480
1481
1482         /* Handle all other visible monster requests */
1483         else
1484         {
1485                 /* Tanuki? */
1486                 if (is_pet(m_ptr) && !is_original_ap(m_ptr))
1487                 {
1488 #ifdef JP
1489                         char *t;
1490                         strcpy(buf, name);
1491                         t = buf;
1492                         while (strncmp(t, "』", 2) && *t) t++;
1493                         if (*t)
1494                         {
1495                                 *t = '\0';
1496                                 (void)sprintf(desc, "%s?』", buf);
1497                         }
1498                         else
1499                                 (void)sprintf(desc, "%s?", name);
1500 #else
1501                         (void)sprintf(desc, "%s?", name);
1502 #endif
1503                 }
1504                 else
1505
1506                         /* It could be a Unique */
1507                         if ((r_ptr->flags1 & RF1_UNIQUE) && !(p_ptr->image && !(mode & MD_IGNORE_HALLU)))
1508                         {
1509                                 /* Start with the name (thus nominative and objective) */
1510                                 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && !(mode & MD_TRUE_NAME))
1511                                 {
1512 #ifdef JP
1513                                         char *t;
1514                                         strcpy(buf, name);
1515                                         t = buf;
1516                                         while (strncmp(t, "』", 2) && *t) t++;
1517                                         if (*t)
1518                                         {
1519                                                 *t = '\0';
1520                                                 (void)sprintf(desc, "%s?』", buf);
1521                                         }
1522                                         else
1523                                                 (void)sprintf(desc, "%s?", name);
1524 #else
1525                                         (void)sprintf(desc, "%s?", name);
1526 #endif
1527                                 }
1528
1529                                 /* Inside monster arena, and it is not your mount */
1530                                 else if (p_ptr->phase_out &&
1531                                         !(p_ptr->riding && (&floor_ptr->m_list[p_ptr->riding] == m_ptr)))
1532                                 {
1533                                         /* It is a fake unique monster */
1534                                         (void)sprintf(desc, _("%sもどき", "fake %s"), name);
1535                                 }
1536
1537                                 else
1538                                 {
1539                                         (void)strcpy(desc, name);
1540                                 }
1541                         }
1542
1543                 /* It could be an indefinite monster */
1544                         else if (mode & MD_INDEF_VISIBLE)
1545                         {
1546                                 /* XXX Check plurality for "some" */
1547
1548                                 /* Indefinite monsters need an indefinite article */
1549 #ifdef JP
1550                                 (void)strcpy(desc, "");
1551 #else
1552                                 (void)strcpy(desc, is_a_vowel(name[0]) ? "an " : "a ");
1553 #endif
1554
1555                                 (void)strcat(desc, name);
1556                         }
1557
1558                 /* It could be a normal, definite, monster */
1559                         else
1560                         {
1561                                 /* Definite monsters need a definite article */
1562                                 if (is_pet(m_ptr))
1563                                         (void)strcpy(desc, _("あなたの", "your "));
1564                                 else
1565                                         (void)strcpy(desc, _("", "the "));
1566
1567                                 (void)strcat(desc, name);
1568                         }
1569
1570                 if (m_ptr->nickname)
1571                 {
1572                         sprintf(buf, _("「%s」", " called %s"), quark_str(m_ptr->nickname));
1573                         strcat(desc, buf);
1574                 }
1575
1576                 if (p_ptr->riding && (&floor_ptr->m_list[p_ptr->riding] == m_ptr))
1577                 {
1578                         strcat(desc, _("(乗馬中)", "(riding)"));
1579                 }
1580
1581                 if ((mode & MD_IGNORE_HALLU) && (m_ptr->mflag2 & MFLAG2_CHAMELEON))
1582                 {
1583                         if (r_ptr->flags1 & RF1_UNIQUE)
1584                         {
1585                                 strcat(desc, _("(カメレオンの王)", "(Chameleon Lord)"));
1586                         }
1587                         else
1588                         {
1589                                 strcat(desc, _("(カメレオン)", "(Chameleon)"));
1590                         }
1591                 }
1592
1593                 if ((mode & MD_IGNORE_HALLU) && !is_original_ap(m_ptr))
1594                 {
1595                         strcat(desc, format("(%s)", r_name + r_info[m_ptr->r_idx].name));
1596                 }
1597
1598                 /* Handle the Possessive as a special afterthought */
1599                 if (mode & MD_POSSESSIVE)
1600                 {
1601                         /* XXX Check for trailing "s" */
1602
1603                         /* Simply append "apostrophe" and "s" */
1604                         (void)strcat(desc, _("の", "'s"));
1605                 }
1606         }
1607 }
1608
1609 /*!
1610 * @brief モンスターIDを取り、モンスター名をm_nameに代入する /
1611 * @param m_idx モンスターID
1612 * @param m_name モンスター名を入力する配列
1613 */
1614 void monster_name(MONSTER_IDX m_idx, char* m_name)
1615 {
1616         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1617         monster_desc(m_name, m_ptr, 0x00);
1618 }
1619
1620
1621 /*!
1622  * @brief モンスターの調査による思い出補完処理 / Learn about a monster (by "probing" it)
1623  * @param r_idx 補完されるモンスター種族ID
1624  * @return 明らかになった情報の度数
1625  * @details
1626  * Return the number of new flags learnt.  -Mogami-
1627  */
1628 int lore_do_probe(MONRACE_IDX r_idx)
1629 {
1630         monster_race *r_ptr = &r_info[r_idx];
1631         int i, n = 0;
1632         byte tmp_byte;
1633
1634         /* Maximal info about awareness */
1635         if (r_ptr->r_wake != MAX_UCHAR) n++;
1636         if (r_ptr->r_ignore != MAX_UCHAR) n++;
1637         r_ptr->r_wake = r_ptr->r_ignore = MAX_UCHAR;
1638
1639         /* Observe "maximal" attacks */
1640         for (i = 0; i < 4; i++)
1641         {
1642                 /* Examine "actual" blows */
1643                 if (r_ptr->blow[i].effect || r_ptr->blow[i].method)
1644                 {
1645                         /* Maximal observations */
1646                         if (r_ptr->r_blows[i] != MAX_UCHAR) n++;
1647                         r_ptr->r_blows[i] = MAX_UCHAR;
1648                 }
1649         }
1650
1651         /* Maximal drops */
1652         tmp_byte =
1653                 (((r_ptr->flags1 & RF1_DROP_4D2) ? 8 : 0) +
1654                 ((r_ptr->flags1 & RF1_DROP_3D2) ? 6 : 0) +
1655                         ((r_ptr->flags1 & RF1_DROP_2D2) ? 4 : 0) +
1656                         ((r_ptr->flags1 & RF1_DROP_1D2) ? 2 : 0) +
1657                         ((r_ptr->flags1 & RF1_DROP_90) ? 1 : 0) +
1658                         ((r_ptr->flags1 & RF1_DROP_60) ? 1 : 0));
1659
1660         /* Only "valid" drops */
1661         if (!(r_ptr->flags1 & RF1_ONLY_GOLD))
1662         {
1663                 if (r_ptr->r_drop_item != tmp_byte) n++;
1664                 r_ptr->r_drop_item = tmp_byte;
1665         }
1666         if (!(r_ptr->flags1 & RF1_ONLY_ITEM))
1667         {
1668                 if (r_ptr->r_drop_gold != tmp_byte) n++;
1669                 r_ptr->r_drop_gold = tmp_byte;
1670         }
1671
1672         /* Observe many spells */
1673         if (r_ptr->r_cast_spell != MAX_UCHAR) n++;
1674         r_ptr->r_cast_spell = MAX_UCHAR;
1675
1676         /* Count unknown flags */
1677         for (i = 0; i < 32; i++)
1678         {
1679                 if (!(r_ptr->r_flags1 & (1L << i)) &&
1680                         (r_ptr->flags1 & (1L << i))) n++;
1681                 if (!(r_ptr->r_flags2 & (1L << i)) &&
1682                         (r_ptr->flags2 & (1L << i))) n++;
1683                 if (!(r_ptr->r_flags3 & (1L << i)) &&
1684                         (r_ptr->flags3 & (1L << i))) n++;
1685                 if (!(r_ptr->r_flags4 & (1L << i)) &&
1686                         (r_ptr->flags4 & (1L << i))) n++;
1687                 if (!(r_ptr->r_flags5 & (1L << i)) &&
1688                         (r_ptr->a_ability_flags1 & (1L << i))) n++;
1689                 if (!(r_ptr->r_flags6 & (1L << i)) &&
1690                         (r_ptr->a_ability_flags2 & (1L << i))) n++;
1691                 if (!(r_ptr->r_flagsr & (1L << i)) &&
1692                         (r_ptr->flagsr & (1L << i))) n++;
1693
1694                 /* r_flags7 is actually unused */
1695         }
1696
1697         /* Know all the flags */
1698         r_ptr->r_flags1 = r_ptr->flags1;
1699         r_ptr->r_flags2 = r_ptr->flags2;
1700         r_ptr->r_flags3 = r_ptr->flags3;
1701         r_ptr->r_flags4 = r_ptr->flags4;
1702         r_ptr->r_flags5 = r_ptr->a_ability_flags1;
1703         r_ptr->r_flags6 = r_ptr->a_ability_flags2;
1704         r_ptr->r_flagsr = r_ptr->flagsr;
1705
1706         /* r_flags7 is actually unused */
1707         /* r_ptr->r_flags7 = r_ptr->flags7; */
1708
1709         /* Know about evolution */
1710         if (!(r_ptr->r_xtra1 & MR1_SINKA)) n++;
1711         r_ptr->r_xtra1 |= MR1_SINKA;
1712
1713         /* Update monster recall window */
1714         if (p_ptr->monster_race_idx == r_idx)
1715         {
1716                 p_ptr->window |= (PW_MONSTER);
1717         }
1718
1719         /* Return the number of new flags learnt */
1720         return n;
1721 }
1722
1723
1724 /*!
1725  * @brief モンスターの撃破に伴うドロップ情報の保管処理 / Take note that the given monster just dropped some treasure
1726  * @param m_idx モンスター情報のID
1727  * @param num_item 手に入れたアイテム数
1728  * @param num_gold 手に入れた財宝の単位数
1729  * @return なし
1730  * @details
1731  * Note that learning the "GOOD"/"GREAT" flags gives information
1732  * about the treasure (even when the monster is killed for the first
1733  * time, such as uniques, and the treasure has not been examined yet).
1734  *
1735  * This "indirect" method is used to prevent the player from learning
1736  * exactly how much treasure a monster can drop from observing only
1737  * a single example of a drop.  This method actually observes how much
1738  * gold and items are dropped, and remembers that information to be
1739  * described later by the monster recall code.
1740  */
1741 void lore_treasure(MONSTER_IDX m_idx, ITEM_NUMBER num_item, ITEM_NUMBER num_gold)
1742 {
1743         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1744
1745         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1746
1747         /* If the monster doesn't have original appearance, don't note */
1748         if (!is_original_ap(m_ptr)) return;
1749
1750         /* Note the number of things dropped */
1751         if (num_item > r_ptr->r_drop_item) r_ptr->r_drop_item = num_item;
1752         if (num_gold > r_ptr->r_drop_gold) r_ptr->r_drop_gold = num_gold;
1753
1754         /* Hack -- memorize the good/great flags */
1755         if (r_ptr->flags1 & (RF1_DROP_GOOD)) r_ptr->r_flags1 |= (RF1_DROP_GOOD);
1756         if (r_ptr->flags1 & (RF1_DROP_GREAT)) r_ptr->r_flags1 |= (RF1_DROP_GREAT);
1757
1758         /* Update monster recall window */
1759         if (p_ptr->monster_race_idx == m_ptr->r_idx)
1760         {
1761                 p_ptr->window |= (PW_MONSTER);
1762         }
1763 }
1764
1765
1766 /*!
1767  * @brief モンスターの各情報を更新する / This function updates the monster record of the given monster
1768  * @param m_idx 更新するモンスター情報のID
1769  * @param full プレイヤーとの距離更新を行うならばtrue
1770  * @return なし
1771  * @details
1772  * This involves extracting the distance to the player (if requested),
1773  * and then checking for visibility (natural, infravision, see-invis,
1774  * telepathy), updating the monster visibility flag, redrawing (or
1775  * erasing) the monster when its visibility changes, and taking note
1776  * of any interesting monster flags (cold-blooded, invisible, etc).
1777  *
1778  * Note the new "mflag" field which encodes several monster state flags,
1779  * including "view" for when the monster is currently in line of sight,
1780  * and "mark" for when the monster is currently visible via detection.
1781  *
1782  * The only monster fields that are changed here are "cdis" (the
1783  * distance from the player), "ml" (visible to the player), and
1784  * "mflag" (to maintain the "MFLAG_VIEW" flag).
1785  *
1786  * Note the special "update_monsters()" function which can be used to
1787  * call this function once for every monster.
1788  *
1789  * Note the "full" flag which requests that the "cdis" field be updated,
1790  * this is only needed when the monster (or the player) has moved.
1791  *
1792  * Every time a monster moves, we must call this function for that
1793  * monster, and update the distance, and the visibility.  Every time
1794  * the player moves, we must call this function for every monster, and
1795  * update the distance, and the visibility.  Whenever the player "state"
1796  * changes in certain ways ("blindness", "infravision", "telepathy",
1797  * and "see invisible"), we must call this function for every monster,
1798  * and update the visibility.
1799  *
1800  * Routines that change the "illumination" of a grid must also call this
1801  * function for any monster in that grid, since the "visibility" of some
1802  * monsters may be based on the illumination of their grid.
1803  *
1804  * Note that this function is called once per monster every time the
1805  * player moves.  When the player is running, this function is one
1806  * of the primary bottlenecks, along with "update_view()" and the
1807  * "process_monsters()" code, so efficiency is important.
1808  *
1809  * Note the optimized "inline" version of the "distance()" function.
1810  *
1811  * A monster is "visible" to the player if (1) it has been detected
1812  * by the player, (2) it is close to the player and the player has
1813  * telepathy, or (3) it is close to the player, and in line of sight
1814  * of the player, and it is "illuminated" by some combination of
1815  * infravision, torch light, or permanent light (invisible monsters
1816  * are only affected by "light" if the player can see invisible).
1817  *
1818  * Monsters which are not on the current panel may be "visible" to
1819  * the player, and their descriptions will include an "offscreen"
1820  * reference.  Currently, offscreen monsters cannot be targetted
1821  * or viewed directly, but old targets will remain set.  XXX XXX
1822  *
1823  * The player can choose to be disturbed by several things, including
1824  * "disturb_move" (monster which is viewable moves in some way), and
1825  * "disturb_near" (monster which is "easily" viewable moves in some
1826  * way).  Note that "moves" includes "appears" and "disappears".
1827  */
1828 void update_monster(player_type *subject_ptr, MONSTER_IDX m_idx, bool full)
1829 {
1830         monster_type *m_ptr = &subject_ptr->current_floor_ptr->m_list[m_idx];
1831         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1832
1833         bool do_disturb = disturb_move;
1834
1835         POSITION d;
1836         POSITION fy = m_ptr->fy;
1837         POSITION fx = m_ptr->fx;
1838
1839         /* Seen at all */
1840         bool flag = FALSE;
1841
1842         /* Seen by vision */
1843         bool easy = FALSE;
1844
1845         /* Non-Ninja player in the darkness */
1846         bool in_darkness = (d_info[subject_ptr->dungeon_idx].flags1 & DF1_DARKNESS) && !subject_ptr->see_nocto;
1847
1848         /* Do disturb? */
1849         if (disturb_high)
1850         {
1851                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1852
1853                 if (ap_r_ptr->r_tkills && ap_r_ptr->level >= subject_ptr->lev)
1854                         do_disturb = TRUE;
1855         }
1856
1857         /* Compute distance */
1858         if (full)
1859         {
1860                 /* Distance components */
1861                 int dy = (subject_ptr->y > fy) ? (subject_ptr->y - fy) : (fy - subject_ptr->y);
1862                 int dx = (subject_ptr->x > fx) ? (subject_ptr->x - fx) : (fx - subject_ptr->x);
1863
1864                 /* Approximate distance */
1865                 d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
1866
1867                 /* Restrict distance */
1868                 if (d > 255) d = 255;
1869
1870                 if (!d) d = 1;
1871
1872                 /* Save the distance */
1873                 m_ptr->cdis = d;
1874         }
1875
1876         /* Extract distance */
1877         else
1878         {
1879                 /* Extract the distance */
1880                 d = m_ptr->cdis;
1881         }
1882
1883         /* Detected */
1884         if (m_ptr->mflag2 & (MFLAG2_MARK)) flag = TRUE;
1885
1886         /* Nearby */
1887         if (d <= (in_darkness ? MAX_SIGHT / 2 : MAX_SIGHT))
1888         {
1889                 if (!in_darkness || (d <= MAX_SIGHT / 4))
1890                 {
1891                         if (subject_ptr->special_defense & KATA_MUSOU)
1892                         {
1893                                 /* Detectable */
1894                                 flag = TRUE;
1895
1896                                 if (is_original_ap(m_ptr) && !subject_ptr->image)
1897                                 {
1898                                         /* Hack -- Memorize mental flags */
1899                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
1900                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
1901                                 }
1902                         }
1903
1904                         /* Basic telepathy */
1905                         /* Snipers get telepathy when they concentrate deeper */
1906                         else if (subject_ptr->telepathy)
1907                         {
1908                                 /* Empty mind, no telepathy */
1909                                 if (r_ptr->flags2 & (RF2_EMPTY_MIND))
1910                                 {
1911                                         /* Memorize flags */
1912                                         if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags2 |= (RF2_EMPTY_MIND);
1913                                 }
1914
1915                                 /* Weird mind, occasional telepathy */
1916                                 else if (r_ptr->flags2 & (RF2_WEIRD_MIND))
1917                                 {
1918                                         /* One in ten individuals are detectable */
1919                                         if ((m_idx % 10) == 5)
1920                                         {
1921                                                 /* Detectable */
1922                                                 flag = TRUE;
1923
1924                                                 if (is_original_ap(m_ptr) && !subject_ptr->image)
1925                                                 {
1926                                                         /* Memorize flags */
1927                                                         r_ptr->r_flags2 |= (RF2_WEIRD_MIND);
1928
1929                                                         /* Hack -- Memorize mental flags */
1930                                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
1931                                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
1932                                                 }
1933                                         }
1934                                 }
1935
1936                                 /* Normal mind, allow telepathy */
1937                                 else
1938                                 {
1939                                         /* Detectable */
1940                                         flag = TRUE;
1941
1942                                         if (is_original_ap(m_ptr) && !subject_ptr->image)
1943                                         {
1944                                                 /* Hack -- Memorize mental flags */
1945                                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
1946                                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
1947                                         }
1948                                 }
1949                         }
1950
1951                         if ((subject_ptr->esp_animal) && (r_ptr->flags3 & (RF3_ANIMAL)))
1952                         {
1953                                 flag = TRUE;
1954                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_ANIMAL);
1955                         }
1956
1957                         if ((subject_ptr->esp_undead) && (r_ptr->flags3 & (RF3_UNDEAD)))
1958                         {
1959                                 flag = TRUE;
1960                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_UNDEAD);
1961                         }
1962
1963                         if ((subject_ptr->esp_demon) && (r_ptr->flags3 & (RF3_DEMON)))
1964                         {
1965                                 flag = TRUE;
1966                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_DEMON);
1967                         }
1968
1969                         if ((subject_ptr->esp_orc) && (r_ptr->flags3 & (RF3_ORC)))
1970                         {
1971                                 flag = TRUE;
1972                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_ORC);
1973                         }
1974
1975                         if ((subject_ptr->esp_troll) && (r_ptr->flags3 & (RF3_TROLL)))
1976                         {
1977                                 flag = TRUE;
1978                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_TROLL);
1979                         }
1980
1981                         if ((subject_ptr->esp_giant) && (r_ptr->flags3 & (RF3_GIANT)))
1982                         {
1983                                 flag = TRUE;
1984                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_GIANT);
1985                         }
1986
1987                         if ((subject_ptr->esp_dragon) && (r_ptr->flags3 & (RF3_DRAGON)))
1988                         {
1989                                 flag = TRUE;
1990                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_DRAGON);
1991                         }
1992
1993                         if ((subject_ptr->esp_human) && (r_ptr->flags2 & (RF2_HUMAN)))
1994                         {
1995                                 flag = TRUE;
1996                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags2 |= (RF2_HUMAN);
1997                         }
1998
1999                         if ((subject_ptr->esp_evil) && (r_ptr->flags3 & (RF3_EVIL)))
2000                         {
2001                                 flag = TRUE;
2002                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_EVIL);
2003                         }
2004
2005                         if ((subject_ptr->esp_good) && (r_ptr->flags3 & (RF3_GOOD)))
2006                         {
2007                                 flag = TRUE;
2008                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_GOOD);
2009                         }
2010
2011                         if ((subject_ptr->esp_nonliving) &&
2012                                 ((r_ptr->flags3 & (RF3_DEMON | RF3_UNDEAD | RF3_NONLIVING)) == RF3_NONLIVING))
2013                         {
2014                                 flag = TRUE;
2015                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags3 |= (RF3_NONLIVING);
2016                         }
2017
2018                         if ((subject_ptr->esp_unique) && (r_ptr->flags1 & (RF1_UNIQUE)))
2019                         {
2020                                 flag = TRUE;
2021                                 if (is_original_ap(m_ptr) && !subject_ptr->image) r_ptr->r_flags1 |= (RF1_UNIQUE);
2022                         }
2023                 }
2024
2025                 /* Normal line of sight, and not blind */
2026                 if (player_has_los_bold(subject_ptr, fy, fx) && !subject_ptr->blind)
2027                 {
2028                         bool do_invisible = FALSE;
2029                         bool do_cold_blood = FALSE;
2030
2031                         /* Snipers can see targets in darkness when they concentrate deeper */
2032                         if (subject_ptr->concent >= CONCENT_RADAR_THRESHOLD)
2033                         {
2034                                 /* Easy to see */
2035                                 easy = flag = TRUE;
2036                         }
2037
2038                         /* Use "infravision" */
2039                         if (d <= subject_ptr->see_infra)
2040                         {
2041                                 /* Handle "cold blooded" monsters */
2042                                 if ((r_ptr->flags2 & (RF2_COLD_BLOOD | RF2_AURA_FIRE)) == RF2_COLD_BLOOD)
2043                                 {
2044                                         do_cold_blood = TRUE;
2045                                 }
2046
2047                                 /* Handle "warm blooded" monsters */
2048                                 else
2049                                 {
2050                                         /* Easy to see */
2051                                         easy = flag = TRUE;
2052                                 }
2053                         }
2054
2055                         /* Use "illumination" */
2056                         if (player_can_see_bold(subject_ptr, fy, fx))
2057                         {
2058                                 /* Handle "invisible" monsters */
2059                                 if (r_ptr->flags2 & (RF2_INVISIBLE))
2060                                 {
2061                                         do_invisible = TRUE;
2062
2063                                         /* See invisible */
2064                                         if (subject_ptr->see_inv)
2065                                         {
2066                                                 /* Easy to see */
2067                                                 easy = flag = TRUE;
2068                                         }
2069                                 }
2070
2071                                 /* Handle "normal" monsters */
2072                                 else
2073                                 {
2074                                         /* Easy to see */
2075                                         easy = flag = TRUE;
2076                                 }
2077                         }
2078
2079                         /* Visible */
2080                         if (flag)
2081                         {
2082                                 if (is_original_ap(m_ptr) && !subject_ptr->image)
2083                                 {
2084                                         /* Memorize flags */
2085                                         if (do_invisible) r_ptr->r_flags2 |= (RF2_INVISIBLE);
2086                                         if (do_cold_blood) r_ptr->r_flags2 |= (RF2_COLD_BLOOD);
2087                                 }
2088                         }
2089                 }
2090         }
2091
2092
2093         /* The monster is now visible */
2094         if (flag)
2095         {
2096                 /* It was previously unseen */
2097                 if (!m_ptr->ml)
2098                 {
2099                         /* Mark as visible */
2100                         m_ptr->ml = TRUE;
2101
2102                         /* Draw the monster */
2103                         lite_spot(fy, fx);
2104
2105                         /* Update health bar as needed */
2106                         if (subject_ptr->health_who == m_idx) subject_ptr->redraw |= (PR_HEALTH);
2107                         if (subject_ptr->riding == m_idx) subject_ptr->redraw |= (PR_UHEALTH);
2108
2109                         /* Hack -- Count "fresh" sightings */
2110                         if (!subject_ptr->image)
2111                         {
2112                                 if ((m_ptr->ap_r_idx == MON_KAGE) && (r_info[MON_KAGE].r_sights < MAX_SHORT))
2113                                         r_info[MON_KAGE].r_sights++;
2114                                 else if (is_original_ap(m_ptr) && (r_ptr->r_sights < MAX_SHORT))
2115                                         r_ptr->r_sights++;
2116                         }
2117
2118                         if (r_info[m_ptr->ap_r_idx].flags2 & RF2_ELDRITCH_HORROR)
2119                         {
2120                                 sanity_blast(subject_ptr, m_ptr, FALSE);
2121                         }
2122
2123                         /* Disturb on appearance */
2124                         if (disturb_near && (projectable(subject_ptr, m_ptr->fy, m_ptr->fx, subject_ptr->y, subject_ptr->x) && projectable(subject_ptr, subject_ptr->y, subject_ptr->x, m_ptr->fy, m_ptr->fx)))
2125                         {
2126                                 if (disturb_pets || is_hostile(m_ptr))
2127                                         disturb(subject_ptr, TRUE, TRUE);
2128                         }
2129                 }
2130         }
2131
2132         /* The monster is not visible */
2133         else
2134         {
2135                 /* It was previously seen */
2136                 if (m_ptr->ml)
2137                 {
2138                         /* Mark as not visible */
2139                         m_ptr->ml = FALSE;
2140
2141                         /* Erase the monster */
2142                         lite_spot(fy, fx);
2143
2144                         /* Update health bar as needed */
2145                         if (subject_ptr->health_who == m_idx) subject_ptr->redraw |= (PR_HEALTH);
2146                         if (subject_ptr->riding == m_idx) subject_ptr->redraw |= (PR_UHEALTH);
2147
2148                         /* Disturb on disappearance */
2149                         if (do_disturb)
2150                         {
2151                                 if (disturb_pets || is_hostile(m_ptr))
2152                                         disturb(subject_ptr, TRUE, TRUE);
2153                         }
2154                 }
2155         }
2156
2157
2158         /* The monster is now easily visible */
2159         if (easy)
2160         {
2161                 /* Change */
2162                 if (!(m_ptr->mflag & (MFLAG_VIEW)))
2163                 {
2164                         /* Mark as easily visible */
2165                         m_ptr->mflag |= (MFLAG_VIEW);
2166
2167                         /* Disturb on appearance */
2168                         if (do_disturb)
2169                         {
2170                                 if (disturb_pets || is_hostile(m_ptr))
2171                                         disturb(subject_ptr, TRUE, TRUE);
2172                         }
2173                 }
2174         }
2175
2176         /* The monster is not easily visible */
2177         else
2178         {
2179                 /* Change */
2180                 if (m_ptr->mflag & (MFLAG_VIEW))
2181                 {
2182                         /* Mark as not easily visible */
2183                         m_ptr->mflag &= ~(MFLAG_VIEW);
2184
2185                         /* Disturb on disappearance */
2186                         if (do_disturb)
2187                         {
2188                                 if (disturb_pets || is_hostile(m_ptr))
2189                                         disturb(subject_ptr, TRUE, TRUE);
2190                         }
2191                 }
2192         }
2193 }
2194
2195
2196 /*!
2197  * @brief 単純に生存している全モンスターの更新処理を行う / This function simply updates all the (non-dead) monsters (see above).
2198  * @param full 距離更新を行うならtrue
2199  * @return なし
2200  */
2201 void update_monsters(bool full)
2202 {
2203         MONSTER_IDX i;
2204
2205         /* Update each (live) monster */
2206         floor_type *floor_ptr = p_ptr->current_floor_ptr;
2207         for (i = 1; i < floor_ptr->m_max; i++)
2208         {
2209                 monster_type *m_ptr = &floor_ptr->m_list[i];
2210                 if (!monster_is_valid(m_ptr)) continue;
2211                 update_monster(p_ptr, i, full);
2212         }
2213 }
2214
2215
2216 /*!
2217  * @brief カメレオンの王の変身対象となるモンスターかどうか判定する / Hack -- the index of the summoning monster
2218  * @param r_idx モンスター種族ID
2219  * @return 対象にできるならtrueを返す
2220  */
2221 static bool monster_hook_chameleon_lord(MONRACE_IDX r_idx)
2222 {
2223         floor_type *floor_ptr = p_ptr->current_floor_ptr;
2224         monster_race *r_ptr = &r_info[r_idx];
2225         monster_type *m_ptr = &floor_ptr->m_list[chameleon_change_m_idx];
2226         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2227
2228         if (!(r_ptr->flags1 & (RF1_UNIQUE))) return FALSE;
2229         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2230
2231         if (ABS(r_ptr->level - r_info[MON_CHAMELEON_K].level) > 5) return FALSE;
2232
2233         if ((r_ptr->blow[0].method == RBM_EXPLODE) || (r_ptr->blow[1].method == RBM_EXPLODE) || (r_ptr->blow[2].method == RBM_EXPLODE) || (r_ptr->blow[3].method == RBM_EXPLODE))
2234                 return FALSE;
2235
2236         if (!monster_can_cross_terrain(floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2237
2238         /* Not born */
2239         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2240         {
2241                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
2242         }
2243
2244         /* Born now */
2245         else if (summon_specific_who > 0)
2246         {
2247                 if (monster_has_hostile_align(&floor_ptr->m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2248         }
2249
2250         return TRUE;
2251 }
2252
2253 /*!
2254  * @brief カメレオンの変身対象となるモンスターかどうか判定する / Hack -- the index of the summoning monster
2255  * @param r_idx モンスター種族ID
2256  * @return 対象にできるならtrueを返す
2257  * @todo グローバル変数対策の上 monster_hook.cへ移す。
2258  */
2259 static bool monster_hook_chameleon(MONRACE_IDX r_idx)
2260 {
2261         floor_type *floor_ptr = p_ptr->current_floor_ptr;
2262         monster_race *r_ptr = &r_info[r_idx];
2263         monster_type *m_ptr = &floor_ptr->m_list[chameleon_change_m_idx];
2264         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2265
2266         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2267         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2268         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2269
2270         if ((r_ptr->blow[0].method == RBM_EXPLODE) || (r_ptr->blow[1].method == RBM_EXPLODE) || (r_ptr->blow[2].method == RBM_EXPLODE) || (r_ptr->blow[3].method == RBM_EXPLODE))
2271                 return FALSE;
2272
2273         if (!monster_can_cross_terrain(floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2274
2275         /* Not born */
2276         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2277         {
2278                 if ((old_r_ptr->flags3 & RF3_GOOD) && !(r_ptr->flags3 & RF3_GOOD)) return FALSE;
2279                 if ((old_r_ptr->flags3 & RF3_EVIL) && !(r_ptr->flags3 & RF3_EVIL)) return FALSE;
2280                 if (!(old_r_ptr->flags3 & (RF3_GOOD | RF3_EVIL)) && (r_ptr->flags3 & (RF3_GOOD | RF3_EVIL))) return FALSE;
2281         }
2282
2283         /* Born now */
2284         else if (summon_specific_who > 0)
2285         {
2286                 if (monster_has_hostile_align(&floor_ptr->m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2287         }
2288
2289         return (*(get_monster_hook(p_ptr)))(r_idx);
2290 }
2291
2292 /*!
2293  * @brief モンスターの変身処理
2294  * @param m_idx 変身処理を受けるモンスター情報のID
2295  * @param born 生成時の初変身先指定ならばtrue
2296  * @param r_idx 旧モンスター種族のID
2297  * @return なし
2298  */
2299 void choose_new_monster(MONSTER_IDX m_idx, bool born, MONRACE_IDX r_idx)
2300 {
2301         int oldmaxhp;
2302         floor_type *floor_ptr = p_ptr->current_floor_ptr;
2303         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
2304         monster_race *r_ptr;
2305         char old_m_name[MAX_NLEN];
2306         bool old_unique = FALSE;
2307         int old_r_idx = m_ptr->r_idx;
2308
2309         if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2310                 old_unique = TRUE;
2311         if (old_unique && (r_idx == MON_CHAMELEON)) r_idx = MON_CHAMELEON_K;
2312         r_ptr = &r_info[r_idx];
2313
2314         monster_desc(old_m_name, m_ptr, 0);
2315
2316         if (!r_idx)
2317         {
2318                 DEPTH level;
2319
2320                 chameleon_change_m_idx = m_idx;
2321                 if (old_unique)
2322                         get_mon_num_prep(monster_hook_chameleon_lord, NULL);
2323                 else
2324                         get_mon_num_prep(monster_hook_chameleon, NULL);
2325
2326                 if (old_unique)
2327                         level = r_info[MON_CHAMELEON_K].level;
2328                 else if (!floor_ptr->dun_level)
2329                         level = wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].level;
2330                 else
2331                         level = floor_ptr->dun_level;
2332
2333                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_CHAMELEON) level += 2 + randint1(3);
2334
2335                 r_idx = get_mon_num(level);
2336                 r_ptr = &r_info[r_idx];
2337
2338                 chameleon_change_m_idx = 0;
2339                 if (!r_idx) return;
2340         }
2341
2342         m_ptr->r_idx = r_idx;
2343         m_ptr->ap_r_idx = r_idx;
2344         update_monster(p_ptr, m_idx, FALSE);
2345         lite_spot(m_ptr->fy, m_ptr->fx);
2346
2347         if ((r_info[old_r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)) ||
2348                 (r_ptr->flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)))
2349                 p_ptr->update |= (PU_MON_LITE);
2350
2351         if (born)
2352         {
2353                 /* Sub-alignment of a chameleon */
2354                 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
2355                 {
2356                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2357                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2358                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2359                 }
2360                 return;
2361         }
2362
2363         if (m_idx == p_ptr->riding)
2364         {
2365                 GAME_TEXT m_name[MAX_NLEN];
2366                 monster_desc(m_name, m_ptr, 0);
2367                 msg_format(_("突然%sが変身した。", "Suddenly, %s transforms!"), old_m_name);
2368                 if (!(r_ptr->flags7 & RF7_RIDING))
2369                         if (rakuba(p_ptr, 0, TRUE)) msg_format(_("地面に落とされた。", "You have fallen from %s."), m_name);
2370         }
2371
2372         /* Extract the monster base speed */
2373         m_ptr->mspeed = get_mspeed(r_ptr);
2374
2375         oldmaxhp = m_ptr->max_maxhp;
2376         /* Assign maximal hitpoints */
2377         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2378         {
2379                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2380         }
2381         else
2382         {
2383                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2384         }
2385
2386         /* Monsters have double hitpoints in Nightmare mode */
2387         if (ironman_nightmare)
2388         {
2389                 u32b hp = m_ptr->max_maxhp * 2L;
2390                 m_ptr->max_maxhp = (HIT_POINT)MIN(30000, hp);
2391         }
2392
2393         m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
2394         if (m_ptr->maxhp < 1) m_ptr->maxhp = 1;
2395         m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
2396
2397         /* reset dealt_damage */
2398         m_ptr->dealt_damage = 0;
2399 }
2400
2401
2402 /*!
2403  * @brief たぬきの変身対象となるモンスターかどうか判定する / Hook for Tanuki
2404  * @param r_idx モンスター種族ID
2405  * @return 対象にできるならtrueを返す
2406  * @todo グローバル変数対策の上 monster_hook.cへ移す。
2407  */
2408 static bool monster_hook_tanuki(MONRACE_IDX r_idx)
2409 {
2410         monster_race *r_ptr = &r_info[r_idx];
2411
2412         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2413         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2414         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2415         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2416
2417         if ((r_ptr->blow[0].method == RBM_EXPLODE) || (r_ptr->blow[1].method == RBM_EXPLODE) || (r_ptr->blow[2].method == RBM_EXPLODE) || (r_ptr->blow[3].method == RBM_EXPLODE))
2418                 return FALSE;
2419
2420         return (*(get_monster_hook(p_ptr)))(r_idx);
2421 }
2422
2423
2424 /*!
2425  * @brief モンスターの表層IDを設定する / Set initial racial appearance of a monster
2426  * @param r_idx モンスター種族ID
2427  * @return モンスター種族の表層ID
2428  */
2429 static MONRACE_IDX initial_r_appearance(MONRACE_IDX r_idx, BIT_FLAGS generate_mode)
2430 {
2431         floor_type *floor_ptr = p_ptr->current_floor_ptr;
2432         int attempts = 1000;
2433         MONRACE_IDX ap_r_idx;
2434         DEPTH min = MIN(floor_ptr->base_level - 5, 50);
2435
2436         if (p_ptr->pseikaku == SEIKAKU_CHARGEMAN && !(generate_mode & (PM_MULTIPLY | PM_KAGE)))
2437         {
2438                 if (floor_ptr->base_level == 0 ||
2439                         (one_in_(5) && my_strchr("hkoptuyAHOPTUVY", r_info[r_idx].d_char))) return MON_ALIEN_JURAL;
2440         }
2441
2442         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2443                 return r_idx;
2444
2445         get_mon_num_prep(monster_hook_tanuki, NULL);
2446
2447         while (--attempts)
2448         {
2449                 ap_r_idx = get_mon_num(floor_ptr->base_level + 10);
2450                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2451         }
2452
2453         return r_idx;
2454 }
2455
2456
2457 /*!
2458  * @brief モンスターの個体加速を設定する / Get initial monster speed
2459  * @param r_ptr モンスター種族の参照ポインタ
2460  * @return 加速値
2461  */
2462 SPEED get_mspeed(monster_race *r_ptr)
2463 {
2464         /* Extract the monster base speed */
2465         SPEED mspeed = r_ptr->speed;
2466
2467         /* Hack -- small racial variety */
2468         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->current_floor_ptr->inside_arena)
2469         {
2470                 /* Allow some small variation per monster */
2471                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2472                 if (i) mspeed += rand_spread(0, i);
2473         }
2474
2475         if (mspeed > 199) mspeed = 199;
2476
2477         return mspeed;
2478 }
2479
2480
2481 /*!
2482  * @brief モンスターを一体生成する / Attempt to place a monster of the given race at the given location.
2483  * @param player_ptr プレーヤーへの参照ポインタ
2484  * @param who 召喚を行ったモンスターID
2485  * @param y 生成位置y座標
2486  * @param x 生成位置x座標
2487  * @param r_idx 生成モンスター種族
2488  * @param mode 生成オプション
2489  * @return 成功したらtrue
2490  * @details
2491  * To give the player a sporting chance, any monster that appears in
2492  * line-of-sight and is extremely dangerous can be marked as
2493  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2494  * which often (but not always) lets the player move before they do.
2495  *
2496  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2497  *
2498  * Use special "here" and "dead" flags for unique monsters,
2499  * remove old "cur_num" and "max_num" fields.
2500  *
2501  * Actually, do something similar for artifacts, to simplify
2502  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2503  *
2504  * This is the only function which may place a monster in the dungeon,
2505  * except for the savefile loading code.
2506  */
2507 static bool place_monster_one(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
2508 {
2509         floor_type *floor_ptr = player_ptr->current_floor_ptr;
2510         grid_type               *g_ptr = &floor_ptr->grid_array[y][x];
2511         monster_type    *m_ptr;
2512         monster_race    *r_ptr = &r_info[r_idx];
2513         concptr         name = (r_name + r_ptr->name);
2514
2515         int cmi;
2516
2517         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
2518         if (player_ptr->wild_mode) return FALSE;
2519
2520         if (!in_bounds(floor_ptr, y, x)) return FALSE;
2521         if (!r_idx) return FALSE;
2522         if (!r_ptr->name) return FALSE;
2523
2524         if (!(mode & PM_IGNORE_TERRAIN))
2525         {
2526                 /* Not on the Pattern */
2527                 if (pattern_tile(y, x)) return FALSE;
2528
2529                 /* Require empty space (if not ghostly) */
2530                 if (!monster_can_enter(y, x, r_ptr, 0)) return FALSE;
2531         }
2532
2533         if (!player_ptr->phase_out)
2534         {
2535                 /* Hack -- "unique" monsters must be "unique" */
2536                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
2537                         (r_ptr->flags7 & (RF7_NAZGUL))) &&
2538                         (r_ptr->cur_num >= r_ptr->max_num))
2539                 {
2540                         /* Cannot create */
2541                         return FALSE;
2542                 }
2543
2544                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
2545                         (r_ptr->cur_num >= 1))
2546                 {
2547                         return FALSE;
2548                 }
2549
2550                 if (r_idx == MON_BANORLUPART)
2551                 {
2552                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
2553                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
2554                 }
2555
2556                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
2557                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (floor_ptr->dun_level < r_ptr->level) &&
2558                         (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
2559                 {
2560                         /* Cannot create */
2561                         return FALSE;
2562                 }
2563         }
2564
2565         if (quest_number(player_ptr, floor_ptr->dun_level))
2566         {
2567                 int hoge = quest_number(player_ptr, floor_ptr->dun_level);
2568                 if ((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
2569                 {
2570                         if (r_idx == quest[hoge].r_idx)
2571                         {
2572                                 int number_mon, i2, j2;
2573                                 number_mon = 0;
2574
2575                                 /* Count all quest monsters */
2576                                 for (i2 = 0; i2 < floor_ptr->width; ++i2)
2577                                         for (j2 = 0; j2 < floor_ptr->height; j2++)
2578                                                 if (floor_ptr->grid_array[j2][i2].m_idx > 0)
2579                                                         if (floor_ptr->m_list[floor_ptr->grid_array[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
2580                                                                 number_mon++;
2581                                 if (number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
2582                                         return FALSE;
2583                         }
2584                 }
2585         }
2586
2587         if (is_glyph_grid(g_ptr))
2588         {
2589                 if (randint1(BREAK_GLYPH) < (r_ptr->level + 20))
2590                 {
2591                         /* Describe observable breakage */
2592                         if (g_ptr->info & CAVE_MARK)
2593                         {
2594                                 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2595                         }
2596
2597                         /* Forget the rune */
2598                         g_ptr->info &= ~(CAVE_MARK);
2599
2600                         /* Break the rune */
2601                         g_ptr->info &= ~(CAVE_OBJECT);
2602                         g_ptr->mimic = 0;
2603
2604                         note_spot(y, x);
2605                 }
2606                 else return FALSE;
2607         }
2608
2609         msg_format_wizard(CHEAT_MONSTER, _("%s(Lv%d)を生成しました。", "%s(Lv%d) was generated."), name, r_ptr->level);
2610
2611         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL) || (r_ptr->level < 10)) mode &= ~PM_KAGE;
2612
2613         /* Make a new monster */
2614         g_ptr->m_idx = m_pop();
2615         hack_m_idx_ii = g_ptr->m_idx;
2616
2617         /* Mega-Hack -- catch "failure" */
2618         if (!g_ptr->m_idx) return FALSE;
2619
2620
2621         /* Get a new monster record */
2622         m_ptr = &floor_ptr->m_list[g_ptr->m_idx];
2623
2624         /* Save the race */
2625         m_ptr->r_idx = r_idx;
2626         m_ptr->ap_r_idx = initial_r_appearance(r_idx, mode);
2627
2628         /* No flags */
2629         m_ptr->mflag = 0;
2630         m_ptr->mflag2 = 0;
2631
2632         /* Hack -- Appearance transfer */
2633         if ((mode & PM_MULTIPLY) && (who > 0) && !is_original_ap(&floor_ptr->m_list[who]))
2634         {
2635                 m_ptr->ap_r_idx = floor_ptr->m_list[who].ap_r_idx;
2636
2637                 /* Hack -- Shadower spawns Shadower */
2638                 if (floor_ptr->m_list[who].mflag2 & MFLAG2_KAGE) m_ptr->mflag2 |= MFLAG2_KAGE;
2639         }
2640
2641         /* Sub-alignment of a monster */
2642         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
2643                 m_ptr->sub_align = floor_ptr->m_list[who].sub_align;
2644         else
2645         {
2646                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2647                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2648                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2649         }
2650
2651         /* Place the monster at the location */
2652         m_ptr->fy = y;
2653         m_ptr->fx = x;
2654         m_ptr->current_floor_ptr = floor_ptr;
2655
2656         /* No "timed status" yet */
2657         for (cmi = 0; cmi < MAX_MTIMED; cmi++) m_ptr->mtimed[cmi] = 0;
2658
2659         /* Unknown distance */
2660         m_ptr->cdis = 0;
2661
2662         reset_target(m_ptr);
2663
2664         m_ptr->nickname = 0;
2665
2666         m_ptr->exp = 0;
2667
2668
2669         /* Your pet summons its pet. */
2670         if (who > 0 && is_pet(&floor_ptr->m_list[who]))
2671         {
2672                 mode |= PM_FORCE_PET;
2673                 m_ptr->parent_m_idx = who;
2674         }
2675         else
2676         {
2677                 m_ptr->parent_m_idx = 0;
2678         }
2679
2680         if (r_ptr->flags7 & RF7_CHAMELEON)
2681         {
2682                 choose_new_monster(g_ptr->m_idx, TRUE, 0);
2683                 r_ptr = &r_info[m_ptr->r_idx];
2684                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
2685
2686                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
2687                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
2688                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2689         }
2690         else if ((mode & PM_KAGE) && !(mode & PM_FORCE_PET))
2691         {
2692                 m_ptr->ap_r_idx = MON_KAGE;
2693                 m_ptr->mflag2 |= MFLAG2_KAGE;
2694         }
2695
2696         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
2697
2698         /* Not visible */
2699         m_ptr->ml = FALSE;
2700
2701         /* Pet? */
2702         if (mode & PM_FORCE_PET)
2703         {
2704                 set_pet(player_ptr, m_ptr);
2705         }
2706         /* Friendly? */
2707         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
2708                 (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(player_ptr, who))
2709         {
2710                 if (!monster_has_hostile_align(NULL, 0, -1, r_ptr)) set_friendly(m_ptr);
2711         }
2712
2713         /* Assume no sleeping */
2714         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
2715
2716         /* Enforce sleeping if needed */
2717         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
2718         {
2719                 int val = r_ptr->sleep;
2720                 (void)set_monster_csleep(player_ptr, g_ptr->m_idx, (val * 2) + randint1(val * 10));
2721         }
2722
2723         /* Assign maximal hitpoints */
2724         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2725         {
2726                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2727         }
2728         else
2729         {
2730                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2731         }
2732
2733         /* Monsters have double hitpoints in Nightmare mode */
2734         if (ironman_nightmare)
2735         {
2736                 u32b hp = m_ptr->max_maxhp * 2L;
2737
2738                 m_ptr->max_maxhp = (HIT_POINT)MIN(30000, hp);
2739         }
2740
2741         m_ptr->maxhp = m_ptr->max_maxhp;
2742
2743         /* And start out fully healthy */
2744         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
2745                 m_ptr->hp = m_ptr->maxhp / 2;
2746         else m_ptr->hp = m_ptr->maxhp;
2747
2748
2749         /* dealt damage is 0 at initial*/
2750         m_ptr->dealt_damage = 0;
2751
2752
2753         /* Extract the monster base speed */
2754         m_ptr->mspeed = get_mspeed(r_ptr);
2755
2756         if (mode & PM_HASTE) (void)set_monster_fast(player_ptr, g_ptr->m_idx, 100);
2757
2758         /* Give a random starting energy */
2759         if (!ironman_nightmare)
2760         {
2761                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
2762         }
2763         else
2764         {
2765                 /* Nightmare monsters are more prepared */
2766                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
2767         }
2768
2769         /* Force monster to wait for player, unless in Nightmare mode */
2770         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
2771         {
2772                 /* Monster is still being nice */
2773                 m_ptr->mflag |= (MFLAG_NICE);
2774
2775                 /* Must repair monsters */
2776                 repair_monsters = TRUE;
2777         }
2778
2779         /* Hack -- see "process_monsters()" */
2780         if (g_ptr->m_idx < hack_m_idx)
2781         {
2782                 /* Monster is still being born */
2783                 m_ptr->mflag |= (MFLAG_BORN);
2784         }
2785
2786
2787         if (r_ptr->flags7 & RF7_SELF_LD_MASK)
2788                 player_ptr->update |= (PU_MON_LITE);
2789         else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !MON_CSLEEP(m_ptr))
2790                 player_ptr->update |= (PU_MON_LITE);
2791         update_monster(player_ptr, g_ptr->m_idx, TRUE);
2792
2793
2794         /* Count the monsters on the level */
2795         real_r_ptr(m_ptr)->cur_num++;
2796
2797         /*
2798          * Memorize location of the unique monster in saved floors.
2799          * A unique monster move from old saved floor.
2800          */
2801         if (current_world_ptr->character_dungeon &&
2802                 ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
2803                 real_r_ptr(m_ptr)->floor_id = player_ptr->floor_id;
2804
2805         /* Hack -- Count the number of "reproducers" */
2806         if (r_ptr->flags2 & RF2_MULTIPLY) floor_ptr->num_repro++;
2807
2808         if (player_ptr->warning && current_world_ptr->character_dungeon)
2809         {
2810                 if (r_ptr->flags1 & RF1_UNIQUE)
2811                 {
2812                         concptr color;
2813                         object_type *o_ptr;
2814                         GAME_TEXT o_name[MAX_NLEN];
2815
2816                         if (r_ptr->level > player_ptr->lev + 30)
2817                                 color = _("黒く", "black");
2818                         else if (r_ptr->level > player_ptr->lev + 15)
2819                                 color = _("紫色に", "purple");
2820                         else if (r_ptr->level > player_ptr->lev + 5)
2821                                 color = _("ルビー色に", "deep red");
2822                         else if (r_ptr->level > player_ptr->lev - 5)
2823                                 color = _("赤く", "red");
2824                         else if (r_ptr->level > player_ptr->lev - 15)
2825                                 color = _("ピンク色に", "pink");
2826                         else
2827                                 color = _("白く", "white");
2828
2829                         o_ptr = choose_warning_item(player_ptr);
2830                         if (o_ptr)
2831                         {
2832                                 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
2833                                 msg_format(_("%sは%s光った。", "%s glows %s."), o_name, color);
2834                         }
2835                         else
2836                         {
2837                                 msg_format(_("%s光る物が頭に浮かんだ。", "An %s image forms in your mind."), color);
2838                         }
2839                 }
2840         }
2841
2842         if (is_explosive_rune_grid(g_ptr))
2843         {
2844                 /* Break the ward */
2845                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2846                 {
2847                         /* Describe observable breakage */
2848                         if (g_ptr->info & CAVE_MARK)
2849                         {
2850                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2851                                 project(player_ptr, 0, 2, y, x, 2 * (player_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
2852                         }
2853                 }
2854                 else
2855                 {
2856                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2857                 }
2858
2859                 /* Forget the rune */
2860                 g_ptr->info &= ~(CAVE_MARK);
2861
2862                 /* Break the rune */
2863                 g_ptr->info &= ~(CAVE_OBJECT);
2864                 g_ptr->mimic = 0;
2865
2866                 note_spot(y, x);
2867                 lite_spot(y, x);
2868         }
2869
2870         /* Success */
2871         return TRUE;
2872 }
2873
2874
2875
2876 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
2877
2878 /*!
2879  * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
2880  * @param r_idx 生成モンスター種族
2881  * @param yp 結果生成位置y座標
2882  * @param xp 結果生成位置x座標
2883  * @param y 中心生成位置y座標
2884  * @param x 中心生成位置x座標
2885  * @param max_dist 生成位置の最大半径
2886  * @return 成功したらtrue
2887  *
2888  */
2889 static bool mon_scatter(MONRACE_IDX r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
2890 {
2891         POSITION place_x[MON_SCAT_MAXD];
2892         POSITION place_y[MON_SCAT_MAXD];
2893         int num[MON_SCAT_MAXD];
2894         int i;
2895         POSITION nx, ny;
2896
2897         if (max_dist >= MON_SCAT_MAXD)
2898                 return FALSE;
2899
2900         for (i = 0; i < MON_SCAT_MAXD; i++)
2901                 num[i] = 0;
2902
2903         floor_type *floor_ptr = p_ptr->current_floor_ptr;
2904         for (nx = x - max_dist; nx <= x + max_dist; nx++)
2905         {
2906                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
2907                 {
2908                         /* Ignore annoying locations */
2909                         if (!in_bounds(floor_ptr, ny, nx)) continue;
2910
2911                         /* Require "line of projection" */
2912                         if (!projectable(p_ptr, y, x, ny, nx)) continue;
2913
2914                         if (r_idx > 0)
2915                         {
2916                                 monster_race *r_ptr = &r_info[r_idx];
2917
2918                                 /* Require empty space (if not ghostly) */
2919                                 if (!monster_can_enter(ny, nx, r_ptr, 0))
2920                                         continue;
2921                         }
2922                         else
2923                         {
2924                                 /* Walls and Monsters block flow */
2925                                 if (!cave_empty_bold2(floor_ptr, ny, nx)) continue;
2926
2927                                 /* ... nor on the Pattern */
2928                                 if (pattern_tile(ny, nx)) continue;
2929                         }
2930
2931                         i = distance(y, x, ny, nx);
2932
2933                         if (i > max_dist)
2934                                 continue;
2935
2936                         num[i]++;
2937
2938                         /* random swap */
2939                         if (one_in_(num[i]))
2940                         {
2941                                 place_x[i] = nx;
2942                                 place_y[i] = ny;
2943                         }
2944                 }
2945         }
2946
2947         i = 0;
2948         while (i < MON_SCAT_MAXD && 0 == num[i])
2949                 i++;
2950         if (i >= MON_SCAT_MAXD)
2951                 return FALSE;
2952
2953         *xp = place_x[i];
2954         *yp = place_y[i];
2955
2956         return TRUE;
2957 }
2958
2959 /*!
2960  * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
2961  * @param who 召喚主のモンスター情報ID
2962  * @param y 中心生成位置y座標
2963  * @param x 中心生成位置x座標
2964  * @param r_idx 生成モンスター種族
2965  * @param mode 生成オプション
2966  * @return 成功したらtrue
2967  */
2968 static bool place_monster_group(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
2969 {
2970         monster_race *r_ptr = &r_info[r_idx];
2971
2972         int n, i;
2973         int total = 0, extra = 0;
2974
2975         int hack_n = 0;
2976
2977         POSITION hack_y[GROUP_MAX];
2978         POSITION hack_x[GROUP_MAX];
2979
2980
2981         /* Pick a group size */
2982         total = randint1(10);
2983
2984         /* Hard monsters, small groups */
2985         floor_type *floor_ptr = player_ptr->current_floor_ptr;
2986         if (r_ptr->level > floor_ptr->dun_level)
2987         {
2988                 extra = r_ptr->level - floor_ptr->dun_level;
2989                 extra = 0 - randint1(extra);
2990         }
2991
2992         /* Easy monsters, large groups */
2993         else if (r_ptr->level < floor_ptr->dun_level)
2994         {
2995                 extra = floor_ptr->dun_level - r_ptr->level;
2996                 extra = randint1(extra);
2997         }
2998
2999         /* Hack -- limit group reduction */
3000         if (extra > 9) extra = 9;
3001
3002         /* Modify the group size */
3003         total += extra;
3004
3005         /* Minimum size */
3006         if (total < 1) total = 1;
3007
3008         /* Maximum size */
3009         if (total > GROUP_MAX) total = GROUP_MAX;
3010
3011
3012         /* Start on the monster */
3013         hack_n = 1;
3014         hack_x[0] = x;
3015         hack_y[0] = y;
3016
3017         /* Puddle monsters, breadth first, up to total */
3018         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3019         {
3020                 /* Grab the location */
3021                 POSITION hx = hack_x[n];
3022                 POSITION hy = hack_y[n];
3023
3024                 /* Check each direction, up to total */
3025                 for (i = 0; (i < 8) && (hack_n < total); i++)
3026                 {
3027                         POSITION mx, my;
3028
3029                         scatter(player_ptr, &my, &mx, hy, hx, 4, 0);
3030
3031                         /* Walls and Monsters block flow */
3032                         if (!cave_empty_bold2(floor_ptr, my, mx)) continue;
3033
3034                         /* Attempt to place another monster */
3035                         if (place_monster_one(player_ptr, who, my, mx, r_idx, mode))
3036                         {
3037                                 /* Add it to the "hack" set */
3038                                 hack_y[hack_n] = my;
3039                                 hack_x[hack_n] = mx;
3040                                 hack_n++;
3041                         }
3042                 }
3043         }
3044
3045
3046         /* Success */
3047         return TRUE;
3048 }
3049
3050 /*!
3051  * @var place_monster_idx
3052  * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
3053  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
3054  */
3055 static MONSTER_IDX place_monster_idx = 0;
3056
3057 /*!
3058  * @var place_monster_m_idx
3059  * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
3060  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
3061  */
3062 static MONSTER_IDX place_monster_m_idx = 0;
3063
3064 /*!
3065  * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
3066  * @param r_idx チェックするモンスター種族のID
3067  * @return 護衛にできるならばtrue
3068  */
3069 static bool place_monster_can_escort(MONRACE_IDX r_idx)
3070 {
3071         monster_race *r_ptr = &r_info[place_monster_idx];
3072         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[place_monster_m_idx];
3073
3074         monster_race *z_ptr = &r_info[r_idx];
3075
3076         /* Hack - Escorts have to have the same dungeon flag */
3077         if (mon_hook_dungeon(place_monster_idx) != mon_hook_dungeon(r_idx)) return FALSE;
3078
3079         /* Require similar "race" */
3080         if (z_ptr->d_char != r_ptr->d_char) return FALSE;
3081
3082         /* Skip more advanced monsters */
3083         if (z_ptr->level > r_ptr->level) return FALSE;
3084
3085         /* Skip unique monsters */
3086         if (z_ptr->flags1 & RF1_UNIQUE) return FALSE;
3087
3088         /* Paranoia -- Skip identical monsters */
3089         if (place_monster_idx == r_idx) return FALSE;
3090
3091         /* Skip different alignment */
3092         if (monster_has_hostile_align(m_ptr, 0, 0, z_ptr)) return FALSE;
3093
3094         if (r_ptr->flags7 & RF7_FRIENDLY)
3095         {
3096                 if (monster_has_hostile_align(NULL, 1, -1, z_ptr)) return FALSE;
3097         }
3098
3099         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3100                 return FALSE;
3101
3102         return TRUE;
3103 }
3104
3105
3106 /*!
3107  * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
3108  * @param player_ptr プレーヤーへの参照ポインタ
3109  * @param who 召喚主のモンスター情報ID
3110  * @param y 生成地点y座標
3111  * @param x 生成地点x座標
3112  * @param r_idx 生成するモンスターの種族ID
3113  * @param mode 生成オプション
3114  * @return 生成に成功したらtrue
3115  * @details
3116  * Note that certain monsters are now marked as requiring "friends".
3117  * These monsters, if successfully placed, and if the "grp" parameter
3118  * is TRUE, will be surrounded by a "group" of identical monsters.
3119  *
3120  * Note that certain monsters are now marked as requiring an "escort",
3121  * which is a collection of monsters with similar "race" but lower level.
3122  *
3123  * Some monsters induce a fake "group" flag on their escorts.
3124  *
3125  * Note the "bizarre" use of non-recursion to prevent annoying output
3126  * when running a code profiler.
3127  *
3128  * Note the use of the new "monster allocation table" code to restrict
3129  * the "get_mon_num()" function to "legal" escort types.
3130  */
3131 bool place_monster_aux(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
3132 {
3133         int             i, j, n;
3134         monster_race    *r_ptr = &r_info[r_idx];
3135
3136         if (!(mode & PM_NO_KAGE) && one_in_(333))
3137                 mode |= PM_KAGE;
3138
3139         /* Place one monster, or fail */
3140         if (!place_monster_one(player_ptr, who, y, x, r_idx, mode)) return FALSE;
3141
3142         /* Require the "group" flag */
3143         if (!(mode & PM_ALLOW_GROUP)) return TRUE;
3144
3145         place_monster_m_idx = hack_m_idx_ii;
3146
3147         /* Reinforcement */
3148         for (i = 0; i < 6; i++)
3149         {
3150                 if (!r_ptr->reinforce_id[i]) break;
3151                 n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
3152                 for (j = 0; j < n; j++)
3153                 {
3154                         POSITION nx, ny, d = 7;
3155                         scatter(player_ptr, &ny, &nx, y, x, d, 0);
3156                         (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, r_ptr->reinforce_id[i], mode);
3157                 }
3158         }
3159
3160         /* Friends for certain monsters */
3161         if (r_ptr->flags1 & (RF1_FRIENDS))
3162         {
3163                 /* Attempt to place a group */
3164                 (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
3165         }
3166
3167         /* Escorts for certain monsters */
3168         if (r_ptr->flags1 & (RF1_ESCORT))
3169         {
3170                 /* Set the escort index */
3171                 place_monster_idx = r_idx;
3172
3173                 /* Try to place several "escorts" */
3174                 for (i = 0; i < 32; i++)
3175                 {
3176                         POSITION nx, ny, d = 3;
3177                         MONRACE_IDX z;
3178
3179                         /* Pick a location */
3180                         scatter(player_ptr, &ny, &nx, y, x, d, 0);
3181
3182                         /* Require empty grids */
3183                         if (!cave_empty_bold2(player_ptr->current_floor_ptr, ny, nx)) continue;
3184                         get_mon_num_prep(place_monster_can_escort, get_monster_hook2(player_ptr, ny, nx));
3185
3186                         /* Pick a random race */
3187                         z = get_mon_num(r_ptr->level);
3188
3189                         /* Handle failure */
3190                         if (!z) break;
3191
3192                         /* Place a single escort */
3193                         (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, z, mode);
3194
3195                         /* Place a "group" of escorts if needed */
3196                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3197                                 (r_ptr->flags1 & RF1_ESCORTS))
3198                         {
3199                                 /* Place a group of monsters */
3200                                 (void)place_monster_group(player_ptr, place_monster_m_idx, ny, nx, z, mode);
3201                         }
3202                 }
3203         }
3204
3205         /* Success */
3206         return TRUE;
3207 }
3208
3209 /*!
3210  * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
3211  * @param player_ptr プレーヤーへの参照ポインタ
3212  * @param y 生成地点y座標
3213  * @param x 生成地点x座標
3214  * @param mode 生成オプション
3215  * @return 生成に成功したらtrue
3216  */
3217 bool place_monster(player_type *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
3218 {
3219         MONRACE_IDX r_idx;
3220         get_mon_num_prep(get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
3221
3222         /* Pick a monster */
3223         r_idx = get_mon_num(player_ptr->current_floor_ptr->monster_level);
3224
3225         /* Handle failure */
3226         if (!r_idx) return FALSE;
3227
3228         /* Attempt to place the monster */
3229         if (place_monster_aux(player_ptr, 0, y, x, r_idx, mode)) return TRUE;
3230
3231         return FALSE;
3232 }
3233
3234 /*!
3235  * @brief 指定地点に1種類のモンスター種族による群れを生成する
3236  * @param player_ptr プレーヤーへの参照ポインタ
3237  * @param y 生成地点y座標
3238  * @param x 生成地点x座標
3239  * @return 生成に成功したらtrue
3240  */
3241 bool alloc_horde(player_type *player_ptr, POSITION y, POSITION x)
3242 {
3243         monster_race *r_ptr = NULL;
3244         MONRACE_IDX r_idx = 0;
3245         MONSTER_IDX m_idx;
3246         int attempts = 1000;
3247         POSITION cy = y;
3248         POSITION cx = x;
3249         get_mon_num_prep(get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
3250
3251         floor_type *floor_ptr = player_ptr->current_floor_ptr;
3252         while (--attempts)
3253         {
3254                 /* Pick a monster */
3255                 r_idx = get_mon_num(floor_ptr->monster_level);
3256
3257                 /* Handle failure */
3258                 if (!r_idx) return FALSE;
3259
3260                 r_ptr = &r_info[r_idx];
3261
3262                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3263
3264                 if (r_idx == MON_HAGURE) continue;
3265                 break;
3266         }
3267         if (attempts < 1) return FALSE;
3268
3269         attempts = 1000;
3270
3271         while (--attempts)
3272         {
3273                 /* Attempt to place the monster */
3274                 if (place_monster_aux(player_ptr, 0, y, x, r_idx, 0L)) break;
3275         }
3276
3277         if (attempts < 1) return FALSE;
3278
3279         m_idx = floor_ptr->grid_array[y][x].m_idx;
3280
3281         if (floor_ptr->m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[floor_ptr->m_list[m_idx].r_idx];
3282
3283         for (attempts = randint1(10) + 5; attempts; attempts--)
3284         {
3285                 scatter(player_ptr, &cy, &cx, y, x, 5, 0);
3286
3287                 (void)summon_specific(player_ptr, m_idx, cy, cx, floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
3288
3289                 y = cy;
3290                 x = cx;
3291         }
3292
3293         if (cheat_hear) msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
3294         return TRUE;
3295 }
3296
3297 /*!
3298  * @brief ダンジョンの主生成を試みる / Put the Guardian
3299  * @param player_ptr プレーヤーへの参照ポインタ
3300  * @param def_val 現在の主の生成状態
3301  * @return 生成に成功したらtrue
3302  */
3303 bool alloc_guardian(player_type *player_ptr, bool def_val)
3304 {
3305         MONRACE_IDX guardian = d_info[player_ptr->dungeon_idx].final_guardian;
3306
3307         floor_type *floor_ptr = player_ptr->current_floor_ptr;
3308         if (guardian && (d_info[player_ptr->dungeon_idx].maxdepth == floor_ptr->dun_level) && (r_info[guardian].cur_num < r_info[guardian].max_num))
3309         {
3310                 POSITION oy;
3311                 POSITION ox;
3312                 int try_count = 4000;
3313
3314                 /* Find a good position */
3315                 while (try_count)
3316                 {
3317                         /* Get a random spot */
3318                         oy = randint1(floor_ptr->height - 4) + 2;
3319                         ox = randint1(floor_ptr->width - 4) + 2;
3320
3321                         /* Is it a good spot ? */
3322                         if (cave_empty_bold2(floor_ptr, oy, ox) && monster_can_cross_terrain(floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0))
3323                         {
3324                                 /* Place the guardian */
3325                                 if (place_monster_aux(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) return TRUE;
3326                         }
3327
3328                         /* One less try count */
3329                         try_count--;
3330                 }
3331
3332                 return FALSE;
3333         }
3334
3335         return def_val;
3336 }
3337
3338
3339 /*!
3340  * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
3341  * @param dis プレイヤーから離れるべき最低距離
3342  * @param mode 生成オプション
3343  * @return 生成に成功したらtrue
3344  * @details
3345  * Place the monster at least "dis" distance from the player.
3346  * Use "slp" to choose the initial "sleep" status
3347  * Use "floor_ptr->monster_level" for the monster level
3348  */
3349 bool alloc_monster(player_type *player_ptr, POSITION dis, BIT_FLAGS mode)
3350 {
3351         POSITION y = 0, x = 0;
3352         int attempts_left = 10000;
3353
3354         /* Put the Guardian */
3355         if (alloc_guardian(player_ptr, FALSE)) return TRUE;
3356
3357         /* Find a legal, distant, unoccupied, space */
3358         floor_type *floor_ptr = player_ptr->current_floor_ptr;
3359         while (attempts_left--)
3360         {
3361                 /* Pick a location */
3362                 y = randint0(floor_ptr->height);
3363                 x = randint0(floor_ptr->width);
3364
3365                 /* Require empty floor grid (was "naked") */
3366                 if (floor_ptr->dun_level)
3367                 {
3368                         if (!cave_empty_bold2(floor_ptr, y, x)) continue;
3369                 }
3370                 else
3371                 {
3372                         if (!cave_empty_bold(floor_ptr, y, x)) continue;
3373                 }
3374
3375                 /* Accept far away grids */
3376                 if (distance(y, x, player_ptr->y, player_ptr->x) > dis) break;
3377         }
3378
3379         if (!attempts_left)
3380         {
3381                 if (cheat_xtra || cheat_hear)
3382                 {
3383                         msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
3384                 }
3385
3386                 return FALSE;
3387         }
3388
3389
3390         if (randint1(5000) <= floor_ptr->dun_level)
3391         {
3392                 if (alloc_horde(player_ptr, y, x))
3393                 {
3394                         return TRUE;
3395                 }
3396         }
3397         else
3398         {
3399                 /* Attempt to place the monster, allow groups */
3400                 if (place_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP))) return TRUE;
3401         }
3402
3403         return FALSE;
3404 }
3405
3406
3407 /*!
3408  * @brief モンスターが召喚の基本条件に合っているかをチェックする / Hack -- help decide if a monster race is "okay" to summon
3409  * @param r_idx チェックするモンスター種族ID
3410  * @return 召喚対象にできるならばTRUE
3411  */
3412 static bool summon_specific_okay(MONRACE_IDX r_idx)
3413 {
3414         monster_race *r_ptr = &r_info[r_idx];
3415         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[summon_specific_who];
3416
3417         /* Hack - Only summon dungeon monsters */
3418         if (!mon_hook_dungeon(r_idx)) return FALSE;
3419
3420         /* Hack -- identify the summoning monster */
3421         if (summon_specific_who > 0)
3422         {
3423
3424                 /* Do not summon enemies */
3425
3426                 /* Friendly vs. opposite aligned normal or pet */
3427                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
3428         }
3429         /* Use the player's alignment */
3430         else if (summon_specific_who < 0)
3431         {
3432                 /* Do not summon enemies of the pets */
3433                 if (monster_has_hostile_align(NULL, 10, -10, r_ptr))
3434                 {
3435                         if (!one_in_(ABS(p_ptr->align) / 2 + 1)) return FALSE;
3436                 }
3437         }
3438
3439         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))) return FALSE;
3440
3441         /* Hack -- no specific type specified */
3442         if (!summon_specific_type) return TRUE;
3443
3444         if ((summon_specific_who < 0) &&
3445                 ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
3446                 monster_has_hostile_align(NULL, 10, -10, r_ptr))
3447                 return FALSE;
3448
3449         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[p_ptr->dungeon_idx].flags1 & DF1_CHAMELEON)) return TRUE;
3450
3451         return (summon_specific_aux(m_ptr->r_idx, r_idx));
3452 }
3453
3454
3455 /*!
3456  * @brief モンスターを召喚により配置する / Place a monster (of the specified "type") near the given location. Return TRUE if a monster was actually summoned.
3457  * @param player_ptr プレーヤーへの参照ポインタ
3458  * @param who 召喚主のモンスター情報ID
3459  * @param y1 目標地点y座標
3460  * @param x1 目標地点x座標
3461  * @param lev 相当生成階
3462  * @param type 召喚種別
3463  * @param mode 生成オプション
3464  * @return 召喚できたらtrueを返す
3465  * @details
3466  *
3467  * We will attempt to place the monster up to 10 times before giving up.
3468  *
3469  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
3470  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
3471  * Note: None of the other summon codes will ever summon Unique's.
3472  *
3473  * This function has been changed.  We now take the "monster level"
3474  * of the summoning monster as a parameter, and use that, along with
3475  * the current dungeon level, to help determine the level of the
3476  * desired monster.  Note that this is an upper bound, and also
3477  * tends to "prefer" monsters of that level.  Currently, we use
3478  * the average of the dungeon and monster levels, and then add
3479  * five to allow slight increases in monster power.
3480  *
3481  * Note that we use the new "monster allocation table" creation code
3482  * to restrict the "get_mon_num()" function to the set of "legal"
3483  * monsters, making this function much faster and more reliable.
3484  *
3485  * Note that this function may not succeed, though this is very rare.
3486  */
3487 bool summon_specific(player_type *player_ptr, MONSTER_IDX who, POSITION y1, POSITION x1, DEPTH lev, int type, BIT_FLAGS mode)
3488 {
3489         POSITION x, y;
3490         MONRACE_IDX r_idx;
3491
3492         floor_type *floor_ptr = player_ptr->current_floor_ptr;
3493         if (floor_ptr->inside_arena) return FALSE;
3494
3495         if (!mon_scatter(0, &y, &x, y1, x1, 2)) return FALSE;
3496
3497         /* Save the summoner */
3498         summon_specific_who = who;
3499
3500         /* Save the "summon" type */
3501         summon_specific_type = type;
3502
3503         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
3504         get_mon_num_prep(summon_specific_okay, get_monster_hook2(player_ptr, y, x));
3505
3506         /* Pick a monster, using the level calculation */
3507         r_idx = get_mon_num((floor_ptr->dun_level + lev) / 2 + 5);
3508
3509         /* Handle failure */
3510         if (!r_idx)
3511         {
3512                 summon_specific_type = 0;
3513                 return FALSE;
3514         }
3515
3516         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
3517
3518         /* Attempt to place the monster (awake, allow groups) */
3519         if (!place_monster_aux(player_ptr, who, y, x, r_idx, mode))
3520         {
3521                 summon_specific_type = 0;
3522                 return FALSE;
3523         }
3524
3525         summon_specific_type = 0;
3526         /* Success */
3527         sound(SOUND_SUMMON);
3528         return TRUE;
3529 }
3530
3531
3532 /*!
3533  * @brief 特定モンスター種族を召喚により生成する / A "dangerous" function, creates a pet of the specified type
3534  * @param player_ptr プレーヤーへの参照ポインタ
3535  * @param who 召喚主のモンスター情報ID
3536  * @param oy 目標地点y座標
3537  * @param ox 目標地点x座標
3538  * @param r_idx 生成するモンスター種族ID
3539  * @param mode 生成オプション
3540  * @return 召喚できたらtrueを返す
3541  */
3542 bool summon_named_creature(player_type *player_ptr, MONSTER_IDX who, POSITION oy, POSITION ox, MONRACE_IDX r_idx, BIT_FLAGS mode)
3543 {
3544         POSITION x, y;
3545         /* if (!r_idx) return; */
3546
3547         /* Prevent illegal monsters */
3548         if (r_idx >= max_r_idx) return FALSE;
3549
3550         if (player_ptr->current_floor_ptr->inside_arena) return FALSE;
3551
3552         if (!mon_scatter(r_idx, &y, &x, oy, ox, 2)) return FALSE;
3553
3554         /* Place it (allow groups) */
3555         return place_monster_aux(player_ptr, who, y, x, r_idx, (mode | PM_NO_KAGE));
3556 }
3557
3558
3559 /*!
3560  * @brief モンスターを増殖生成する / Let the given monster attempt to reproduce.
3561  * @param player_ptr プレーヤーへの参照ポインタ
3562  * @param m_idx 増殖するモンスター情報ID
3563  * @param clone クローン・モンスター処理ならばtrue
3564  * @param mode 生成オプション
3565  * @return 生成できたらtrueを返す
3566  * @details
3567  * Note that "reproduction" REQUIRES empty space.
3568  */
3569 bool multiply_monster(player_type *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
3570 {
3571         floor_type *floor_ptr = player_ptr->current_floor_ptr;
3572         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
3573         POSITION y, x;
3574
3575         if (!mon_scatter(m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
3576                 return FALSE;
3577
3578         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
3579
3580         /* Create a new monster (awake, no groups) */
3581         if (!place_monster_aux(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
3582                 return FALSE;
3583
3584         /* Hack -- Transfer "clone" flag */
3585         if (clone || (m_ptr->smart & SM_CLONED))
3586         {
3587                 floor_ptr->m_list[hack_m_idx_ii].smart |= SM_CLONED;
3588                 floor_ptr->m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
3589         }
3590
3591         return TRUE;
3592 }
3593
3594
3595 /*!
3596  * @brief ダメージを受けたモンスターの様子を記述する / Dump a message describing a monster's reaction to damage
3597  * @param m_idx モンスター情報ID
3598  * @param dam 与えたダメージ
3599  * @return なし
3600  * @details
3601  * Technically should attempt to treat "Beholder"'s as jelly's
3602  */
3603 void message_pain(MONSTER_IDX m_idx, HIT_POINT dam)
3604 {
3605         HIT_POINT oldhp, newhp;
3606         HIT_POINT tmp;
3607         PERCENTAGE percentage;
3608
3609         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
3610         monster_race *r_ptr = &r_info[m_ptr->r_idx];
3611
3612         GAME_TEXT m_name[MAX_NLEN];
3613
3614         monster_desc(m_name, m_ptr, 0);
3615
3616         if (dam == 0) // Notice non-damage
3617         {
3618                 msg_format(_("%^sはダメージを受けていない。", "%^s is unharmed."), m_name);
3619                 return;
3620         }
3621
3622         /* Note -- subtle fix -CFT */
3623         newhp = m_ptr->hp;
3624         oldhp = newhp + dam;
3625         tmp = (newhp * 100L) / oldhp;
3626         percentage = tmp;
3627
3628         if (my_strchr(",ejmvwQ", r_ptr->d_char)) // Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs
3629         {
3630 #ifdef JP
3631                 if (percentage > 95) msg_format("%^sはほとんど気にとめていない。", m_name);
3632                 else if (percentage > 75) msg_format("%^sはしり込みした。", m_name);
3633                 else if (percentage > 50) msg_format("%^sは縮こまった。", m_name);
3634                 else if (percentage > 35) msg_format("%^sは痛みに震えた。", m_name);
3635                 else if (percentage > 20) msg_format("%^sは身もだえした。", m_name);
3636                 else if (percentage > 10) msg_format("%^sは苦痛で身もだえした。", m_name);
3637                 else msg_format("%^sはぐにゃぐにゃと痙攣した。", m_name);
3638 #else
3639                 if (percentage > 95) msg_format("%^s barely notices.", m_name);
3640                 else if (percentage > 75) msg_format("%^s flinches.", m_name);
3641                 else if (percentage > 50) msg_format("%^s squelches.", m_name);
3642                 else if (percentage > 35) msg_format("%^s quivers in pain.", m_name);
3643                 else if (percentage > 20) msg_format("%^s writhes about.", m_name);
3644                 else if (percentage > 10) msg_format("%^s writhes in agony.", m_name);
3645                 else msg_format("%^s jerks limply.", m_name);
3646 #endif
3647         }
3648
3649         else if (my_strchr("l", r_ptr->d_char)) // Fish
3650         {
3651 #ifdef JP
3652                 if (percentage > 95) msg_format("%^sはほとんど気にとめていない。", m_name);
3653                 else if (percentage > 75) msg_format("%^sはしり込みした。", m_name);
3654                 else if (percentage > 50) msg_format("%^sは躊躇した。", m_name);
3655                 else if (percentage > 35) msg_format("%^sは痛みに震えた。", m_name);
3656                 else if (percentage > 20) msg_format("%^sは身もだえした。", m_name);
3657                 else if (percentage > 10) msg_format("%^sは苦痛で身もだえした。", m_name);
3658                 else msg_format("%^sはぐにゃぐにゃと痙攣した。", m_name);
3659 #else
3660                 if (percentage > 95) msg_format("%^s barely notices.", m_name);
3661                 else if (percentage > 75) msg_format("%^s flinches.", m_name);
3662                 else if (percentage > 50) msg_format("%^s hesitates.", m_name);
3663                 else if (percentage > 35) msg_format("%^s quivers in pain.", m_name);
3664                 else if (percentage > 20) msg_format("%^s writhes about.", m_name);
3665                 else if (percentage > 10) msg_format("%^s writhes in agony.", m_name);
3666                 else msg_format("%^s jerks limply.", m_name);
3667 #endif          
3668         }
3669
3670         else if (my_strchr("g#+<>", r_ptr->d_char)) // Golems, Walls, Doors, Stairs
3671         {
3672 #ifdef JP
3673                 if (percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3674                 else if (percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3675                 else if (percentage > 50) msg_format("%^sは雷鳴のように吠えた。", m_name);
3676                 else if (percentage > 35) msg_format("%^sは苦しげに吠えた。", m_name);
3677                 else if (percentage > 20) msg_format("%^sはうめいた。", m_name);
3678                 else if (percentage > 10) msg_format("%^sは躊躇した。", m_name);
3679                 else msg_format("%^sはくしゃくしゃになった。", m_name);
3680 #else
3681                 if (percentage > 95) msg_format("%^s ignores the attack.", m_name);
3682                 else if (percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3683                 else if (percentage > 50) msg_format("%^s roars thunderously.", m_name);
3684                 else if (percentage > 35) msg_format("%^s rumbles.", m_name);
3685                 else if (percentage > 20) msg_format("%^s grunts.", m_name);
3686                 else if (percentage > 10) msg_format("%^s hesitates.", m_name);
3687                 else msg_format("%^s crumples.", m_name);
3688 #endif
3689         }
3690
3691         else if (my_strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char)) // Snakes, Hydrae, Reptiles, Mimics
3692         {
3693 #ifdef JP
3694                 if (percentage > 95) msg_format("%^sはほとんど気にとめていない。", m_name);
3695                 else if (percentage > 75) msg_format("%^sはシーッと鳴いた。", m_name);
3696                 else if (percentage > 50) msg_format("%^sは怒って頭を上げた。", m_name);
3697                 else if (percentage > 35) msg_format("%^sは猛然と威嚇した。", m_name);
3698                 else if (percentage > 20) msg_format("%^sは身もだえした。", m_name);
3699                 else if (percentage > 10) msg_format("%^sは苦痛で身もだえした。", m_name);
3700                 else msg_format("%^sはぐにゃぐにゃと痙攣した。", m_name);
3701 #else
3702                 if (percentage > 95) msg_format("%^s barely notices.", m_name);
3703                 else if (percentage > 75) msg_format("%^s hisses.", m_name);
3704                 else if (percentage > 50) msg_format("%^s rears up in anger.", m_name);
3705                 else if (percentage > 35) msg_format("%^s hisses furiously.", m_name);
3706                 else if (percentage > 20) msg_format("%^s writhes about.", m_name);
3707                 else if (percentage > 10) msg_format("%^s writhes in agony.", m_name);
3708                 else msg_format("%^s jerks limply.", m_name);
3709 #endif
3710         }
3711
3712         else if (my_strchr("f", r_ptr->d_char))
3713         {
3714 #ifdef JP
3715                 if (percentage > 95) msg_format("%sは攻撃に肩をすくめた。", m_name);
3716                 else if (percentage > 75) msg_format("%^sは吠えた。", m_name);
3717                 else if (percentage > 50) msg_format("%^sは怒って吠えた。", m_name);
3718                 else if (percentage > 35) msg_format("%^sは痛みでシーッと鳴いた。", m_name);
3719                 else if (percentage > 20) msg_format("%^sは痛みで弱々しく鳴いた。", m_name);
3720                 else if (percentage > 10) msg_format("%^sは苦痛にうめいた。", m_name);
3721                 else msg_format("%sは哀れな鳴き声を出した。", m_name);
3722 #else
3723                 if (percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
3724                 else if (percentage > 75) msg_format("%^s roars.", m_name);
3725                 else if (percentage > 50) msg_format("%^s growls angrily.", m_name);
3726                 else if (percentage > 35) msg_format("%^s hisses with pain.", m_name);
3727                 else if (percentage > 20) msg_format("%^s mewls in pain.", m_name);
3728                 else if (percentage > 10) msg_format("%^s hisses in agony.", m_name);
3729                 else msg_format("%^s mewls pitifully.", m_name);
3730 #endif
3731         }
3732
3733         else if (my_strchr("acFIKS", r_ptr->d_char)) // Ants, Centipedes, Flies, Insects, Beetles, Spiders
3734         {
3735 #ifdef JP
3736                 if (percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3737                 else if (percentage > 75) msg_format("%^sはキーキー鳴いた。", m_name);
3738                 else if (percentage > 50) msg_format("%^sはヨロヨロ逃げ回った。", m_name);
3739                 else if (percentage > 35) msg_format("%^sはうるさく鳴いた。", m_name);
3740                 else if (percentage > 20) msg_format("%^sは痛みに痙攣した。", m_name);
3741                 else if (percentage > 10) msg_format("%^sは苦痛で痙攣した。", m_name);
3742                 else msg_format("%^sはピクピクひきつった。", m_name);
3743 #else
3744                 if (percentage > 95)    msg_format("%^s ignores the attack.", m_name);
3745                 else if (percentage > 75) msg_format("%^s chitters.", m_name);
3746                 else if (percentage > 50) msg_format("%^s scuttles about.", m_name);
3747                 else if (percentage > 35) msg_format("%^s twitters.", m_name);
3748                 else if (percentage > 20) msg_format("%^s jerks in pain.", m_name);
3749                 else if (percentage > 10) msg_format("%^s jerks in agony.", m_name);
3750                 else msg_format("%^s twitches.", m_name);
3751 #endif
3752         }
3753
3754         else if (my_strchr("B", r_ptr->d_char)) // Birds
3755         {
3756 #ifdef JP
3757                 if (percentage > 95) msg_format("%^sはさえずった。", m_name);
3758                 else if (percentage > 75) msg_format("%^sはピーピー鳴いた。", m_name);
3759                 else if (percentage > 50) msg_format("%^sはギャーギャー鳴いた。", m_name);
3760                 else if (percentage > 35) msg_format("%^sはギャーギャー鳴きわめいた。", m_name);
3761                 else if (percentage > 20) msg_format("%^sは苦しんだ。", m_name);
3762                 else if (percentage > 10) msg_format("%^sはのたうち回った。", m_name);
3763                 else msg_format("%^sはキーキーと鳴き叫んだ。", m_name);
3764 #else
3765                 if (percentage > 95)    msg_format("%^s chirps.", m_name);
3766                 else if (percentage > 75) msg_format("%^s twitters.", m_name);
3767                 else if (percentage > 50) msg_format("%^s squawks.", m_name);
3768                 else if (percentage > 35) msg_format("%^s chatters.", m_name);
3769                 else if (percentage > 20) msg_format("%^s jeers.", m_name);
3770                 else if (percentage > 10) msg_format("%^s flutters about.", m_name);
3771                 else msg_format("%^s squeaks.", m_name);
3772 #endif
3773         }
3774
3775         else if (my_strchr("duDLUW", r_ptr->d_char)) // Dragons, Demons, High Undead
3776         {
3777 #ifdef JP
3778                 if (percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3779                 else if (percentage > 75) msg_format("%^sはしり込みした。", m_name);
3780                 else if (percentage > 50) msg_format("%^sは痛みでシーッと鳴いた。", m_name);
3781                 else if (percentage > 35) msg_format("%^sは痛みでうなった。", m_name);
3782                 else if (percentage > 20) msg_format("%^sは痛みに吠えた。", m_name);
3783                 else if (percentage > 10) msg_format("%^sは苦しげに叫んだ。", m_name);
3784                 else msg_format("%^sは弱々しくうなった。", m_name);
3785 #else
3786                 if (percentage > 95) msg_format("%^s ignores the attack.", m_name);
3787                 else if (percentage > 75) msg_format("%^s flinches.", m_name);
3788                 else if (percentage > 50) msg_format("%^s hisses in pain.", m_name);
3789                 else if (percentage > 35) msg_format("%^s snarls with pain.", m_name);
3790                 else if (percentage > 20) msg_format("%^s roars with pain.", m_name);
3791                 else if (percentage > 10) msg_format("%^s gasps.", m_name);
3792                 else msg_format("%^s snarls feebly.", m_name);
3793 #endif
3794         }
3795
3796         else if (my_strchr("s", r_ptr->d_char)) // Skeletons
3797         {
3798 #ifdef JP
3799                 if (percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3800                 else if (percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3801                 else if (percentage > 50) msg_format("%^sはカタカタと笑った。", m_name);
3802                 else if (percentage > 35) msg_format("%^sはよろめいた。", m_name);
3803                 else if (percentage > 20) msg_format("%^sはカタカタ言った。", m_name);
3804                 else if (percentage > 10) msg_format("%^sはよろめいた。", m_name);
3805                 else msg_format("%^sはガタガタ言った。", m_name);
3806 #else
3807                 if (percentage > 95) msg_format("%^s ignores the attack.", m_name);
3808                 else if (percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3809                 else if (percentage > 50) msg_format("%^s rattles.", m_name);
3810                 else if (percentage > 35) msg_format("%^s stumbles.", m_name);
3811                 else if (percentage > 20) msg_format("%^s rattles.", m_name);
3812                 else if (percentage > 10) msg_format("%^s staggers.", m_name);
3813                 else msg_format("%^s clatters.", m_name);
3814 #endif
3815         }
3816
3817         else if (my_strchr("z", r_ptr->d_char)) // Zombies
3818         {
3819 #ifdef JP
3820                 if (percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3821                 else if (percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3822                 else if (percentage > 50) msg_format("%^sはうめいた。", m_name);
3823                 else if (percentage > 35) msg_format("%sは苦しげにうめいた。", m_name);
3824                 else if (percentage > 20) msg_format("%^sは躊躇した。", m_name);
3825                 else if (percentage > 10) msg_format("%^sはうなった。", m_name);
3826                 else msg_format("%^sはよろめいた。", m_name);
3827 #else
3828                 if (percentage > 95) msg_format("%^s ignores the attack.", m_name);
3829                 else if (percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3830                 else if (percentage > 50) msg_format("%^s groans.", m_name);
3831                 else if (percentage > 35) msg_format("%^s moans.", m_name);
3832                 else if (percentage > 20) msg_format("%^s hesitates.", m_name);
3833                 else if (percentage > 10) msg_format("%^s grunts.", m_name);
3834                 else msg_format("%^s staggers.", m_name);
3835 #endif
3836         }
3837
3838         else if (my_strchr("G", r_ptr->d_char)) // Ghosts
3839         {
3840 #ifdef JP
3841                 if (percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3842                 else if (percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3843                 else if (percentage > 50) msg_format("%sはうめいた。", m_name);
3844                 else if (percentage > 35) msg_format("%^sは泣きわめいた。", m_name);
3845                 else if (percentage > 20) msg_format("%^sは吠えた。", m_name);
3846                 else if (percentage > 10) msg_format("%sは弱々しくうめいた。", m_name);
3847                 else msg_format("%^sはかすかにうめいた。", m_name);
3848 #else
3849                 if (percentage > 95) msg_format("%^s ignores the attack.", m_name);
3850                 else if (percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3851                 else if (percentage > 50)  msg_format("%^s moans.", m_name);
3852                 else if (percentage > 35) msg_format("%^s wails.", m_name);
3853                 else if (percentage > 20) msg_format("%^s howls.", m_name);
3854                 else if (percentage > 10) msg_format("%^s moans softly.", m_name);
3855                 else msg_format("%^s sighs.", m_name);
3856 #endif
3857         }
3858
3859         else if (my_strchr("CZ", r_ptr->d_char)) // Dogs and Hounds
3860         {
3861 #ifdef JP
3862                 if (percentage > 95) msg_format("%^sは攻撃に肩をすくめた。", m_name);
3863                 else if (percentage > 75) msg_format("%^sは痛みでうなった。", m_name);
3864                 else if (percentage > 50) msg_format("%^sは痛みでキャンキャン吠えた。", m_name);
3865                 else if (percentage > 35) msg_format("%^sは痛みで鳴きわめいた。", m_name);
3866                 else if (percentage > 20) msg_format("%^sは苦痛のあまり鳴きわめいた。", m_name);
3867                 else if (percentage > 10) msg_format("%^sは苦痛でもだえ苦しんだ。", m_name);
3868                 else msg_format("%^sは弱々しく吠えた。", m_name);
3869 #else
3870                 if (percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
3871                 else if (percentage > 75) msg_format("%^s snarls with pain.", m_name);
3872                 else if (percentage > 50) msg_format("%^s yelps in pain.", m_name);
3873                 else if (percentage > 35) msg_format("%^s howls in pain.", m_name);
3874                 else if (percentage > 20) msg_format("%^s howls in agony.", m_name);
3875                 else if (percentage > 10) msg_format("%^s writhes in agony.", m_name);
3876                 else msg_format("%^s yelps feebly.", m_name);
3877 #endif
3878         }
3879
3880         else if (my_strchr("Xbilqrt", r_ptr->d_char)) // One type of creatures (ignore,squeal,shriek)
3881         {
3882 #ifdef JP
3883                 if (percentage > 95) msg_format("%^sは攻撃を気にとめていない。", m_name);
3884                 else if (percentage > 75) msg_format("%^sは痛みでうなった。", m_name);
3885                 else if (percentage > 50) msg_format("%^sは痛みで叫んだ。", m_name);
3886                 else if (percentage > 35) msg_format("%^sは痛みで絶叫した。", m_name);
3887                 else if (percentage > 20) msg_format("%^sは苦痛のあまり絶叫した。", m_name);
3888                 else if (percentage > 10) msg_format("%^sは苦痛でもだえ苦しんだ。", m_name);
3889                 else msg_format("%^sは弱々しく叫んだ。", m_name);
3890 #else
3891                 if (percentage > 95) msg_format("%^s ignores the attack.", m_name);
3892                 else if (percentage > 75) msg_format("%^s grunts with pain.", m_name);
3893                 else if (percentage > 50) msg_format("%^s squeals in pain.", m_name);
3894                 else if (percentage > 35) msg_format("%^s shrieks in pain.", m_name);
3895                 else if (percentage > 20) msg_format("%^s shrieks in agony.", m_name);
3896                 else if (percentage > 10) msg_format("%^s writhes in agony.", m_name);
3897                 else msg_format("%^s cries out feebly.", m_name);
3898 #endif
3899         }
3900
3901         else // Another type of creatures (shrug,cry,scream)
3902         {
3903 #ifdef JP
3904                 if (percentage > 95) msg_format("%^sは攻撃に肩をすくめた。", m_name);
3905                 else if (percentage > 75) msg_format("%^sは痛みでうなった。", m_name);
3906                 else if (percentage > 50) msg_format("%^sは痛みで叫んだ。", m_name);
3907                 else if (percentage > 35) msg_format("%^sは痛みで絶叫した。", m_name);
3908                 else if (percentage > 20) msg_format("%^sは苦痛のあまり絶叫した。", m_name);
3909                 else if (percentage > 10) msg_format("%^sは苦痛でもだえ苦しんだ。", m_name);
3910                 else msg_format("%^sは弱々しく叫んだ。", m_name);
3911 #else
3912                 if (percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
3913                 else if (percentage > 75) msg_format("%^s grunts with pain.", m_name);
3914                 else if (percentage > 50) msg_format("%^s cries out in pain.", m_name);
3915                 else if (percentage > 35) msg_format("%^s screams in pain.", m_name);
3916                 else if (percentage > 20) msg_format("%^s screams in agony.", m_name);
3917                 else if (percentage > 10) msg_format("%^s writhes in agony.", m_name);
3918                 else msg_format("%^s cries out feebly.", m_name);
3919 #endif
3920         }
3921 }
3922
3923
3924
3925 /*!
3926  * @brief SMART(適格に攻撃を行う)モンスターの学習状況を更新する / Learn about an "observed" resistance.
3927  * @param m_idx 更新を行う「モンスター情報ID
3928  * @param what 学習対象ID
3929  * @return なし
3930  */
3931 void update_smart_learn(MONSTER_IDX m_idx, int what)
3932 {
3933         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
3934         monster_race *r_ptr = &r_info[m_ptr->r_idx];
3935
3936         /* Not allowed to learn */
3937         if (!smart_learn) return;
3938
3939         /* Too stupid to learn anything */
3940         if (r_ptr->flags2 & (RF2_STUPID)) return;
3941
3942         /* Not intelligent, only learn sometimes */
3943         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
3944
3945         /* Analyze the knowledge */
3946         switch (what)
3947         {
3948         case DRS_ACID:
3949                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
3950                 if (is_oppose_acid(p_ptr)) m_ptr->smart |= (SM_OPP_ACID);
3951                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
3952                 break;
3953
3954         case DRS_ELEC:
3955                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
3956                 if (is_oppose_elec(p_ptr)) m_ptr->smart |= (SM_OPP_ELEC);
3957                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
3958                 break;
3959
3960         case DRS_FIRE:
3961                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
3962                 if (is_oppose_fire(p_ptr)) m_ptr->smart |= (SM_OPP_FIRE);
3963                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
3964                 break;
3965
3966         case DRS_COLD:
3967                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
3968                 if (is_oppose_cold(p_ptr)) m_ptr->smart |= (SM_OPP_COLD);
3969                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
3970                 break;
3971
3972         case DRS_POIS:
3973                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
3974                 if (is_oppose_pois(p_ptr)) m_ptr->smart |= (SM_OPP_POIS);
3975                 break;
3976
3977
3978         case DRS_NETH:
3979                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
3980                 break;
3981
3982         case DRS_LITE:
3983                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
3984                 break;
3985
3986         case DRS_DARK:
3987                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
3988                 break;
3989
3990         case DRS_FEAR:
3991                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
3992                 break;
3993
3994         case DRS_CONF:
3995                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
3996                 break;
3997
3998         case DRS_CHAOS:
3999                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4000                 break;
4001
4002         case DRS_DISEN:
4003                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4004                 break;
4005
4006         case DRS_BLIND:
4007                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4008                 break;
4009
4010         case DRS_NEXUS:
4011                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4012                 break;
4013
4014         case DRS_SOUND:
4015                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4016                 break;
4017
4018         case DRS_SHARD:
4019                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4020                 break;
4021
4022         case DRS_FREE:
4023                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4024                 break;
4025
4026         case DRS_MANA:
4027                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4028                 break;
4029
4030         case DRS_REFLECT:
4031                 if (p_ptr->reflect) m_ptr->smart |= (SM_IMM_REFLECT);
4032                 break;
4033         }
4034 }
4035
4036
4037 /*!
4038  * @brief モンスターが盗みや拾いで確保していたアイテムを全てドロップさせる / Drop all items carried by a monster
4039  * @param m_ptr モンスター参照ポインタ
4040  * @return なし
4041  */
4042 void monster_drop_carried_objects(monster_type *m_ptr)
4043 {
4044         OBJECT_IDX this_o_idx, next_o_idx = 0;
4045         object_type forge;
4046         object_type *o_ptr;
4047         object_type *q_ptr;
4048
4049         /* Drop objects being carried */
4050         floor_type *floor_ptr = p_ptr->current_floor_ptr;
4051         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4052         {
4053                 o_ptr = &floor_ptr->o_list[this_o_idx];
4054                 next_o_idx = o_ptr->next_o_idx;
4055                 q_ptr = &forge;
4056
4057                 object_copy(q_ptr, o_ptr);
4058
4059                 /* Forget monster */
4060                 q_ptr->held_m_idx = 0;
4061
4062                 delete_object_idx(floor_ptr, this_o_idx);
4063
4064                 /* Drop it */
4065                 (void)drop_near(p_ptr, q_ptr, -1, m_ptr->fy, m_ptr->fx);
4066         }
4067
4068         /* Forget objects */
4069         m_ptr->hold_o_idx = 0;
4070 }
4071
4072 /*!
4073  * @brief 指定したモンスターに隣接しているモンスターの数を返す。
4074  * / Count number of adjacent monsters
4075  * @param m_idx 隣接数を調べたいモンスターのID
4076  * @return 隣接しているモンスターの数
4077  */
4078 int get_monster_crowd_number(MONSTER_IDX m_idx)
4079 {
4080         floor_type *floor_ptr = p_ptr->current_floor_ptr;
4081         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
4082         POSITION my = m_ptr->fy;
4083         POSITION mx = m_ptr->fx;
4084         int i;
4085         int count = 0;
4086
4087         for (i = 0; i < 7; i++)
4088         {
4089                 int ay = my + ddy_ddd[i];
4090                 int ax = mx + ddx_ddd[i];
4091
4092                 if (!in_bounds(floor_ptr, ay, ax)) continue;
4093
4094                 /* Count number of monsters */
4095                 if (floor_ptr->grid_array[ay][ax].m_idx > 0) count++;
4096         }
4097
4098         return count;
4099 }
4100
4101
4102 bool is_friendly_idx(player_type *player_ptr, MONSTER_IDX m_idx)
4103 {
4104         return m_idx > 0 && is_friendly(&player_ptr->current_floor_ptr->m_list[(m_idx)]);
4105 }