OSDN Git Service

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