OSDN Git Service

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