OSDN Git Service

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