OSDN Git Service

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