OSDN Git Service

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