OSDN Git Service

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