OSDN Git Service

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