OSDN Git Service

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