OSDN Git Service

8733ab879a65f202f96b51907def41306de50a2a
[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
1584 /*!
1585  * @brief モンスターの調査による思い出補完処理 / Learn about a monster (by "probing" it)
1586  * @param r_idx 補完されるモンスター種族ID
1587  * @return 明らかになった情報の度数
1588  * @details
1589  * Return the number of new flags learnt.  -Mogami-
1590  */
1591 int lore_do_probe(MONRACE_IDX r_idx)
1592 {
1593         monster_race *r_ptr = &r_info[r_idx];
1594         int i, n = 0;
1595         byte tmp_byte;
1596
1597         /* Maximal info about awareness */
1598         if (r_ptr->r_wake != MAX_UCHAR) n++;
1599         if (r_ptr->r_ignore != MAX_UCHAR) n++;
1600         r_ptr->r_wake = r_ptr->r_ignore = MAX_UCHAR;
1601
1602         /* Observe "maximal" attacks */
1603         for (i = 0; i < 4; i++)
1604         {
1605                 /* Examine "actual" blows */
1606                 if (r_ptr->blow[i].effect || r_ptr->blow[i].method)
1607                 {
1608                         /* Maximal observations */
1609                         if (r_ptr->r_blows[i] != MAX_UCHAR) n++;
1610                         r_ptr->r_blows[i] = MAX_UCHAR;
1611                 }
1612         }
1613
1614         /* Maximal drops */
1615         tmp_byte =
1616                 (((r_ptr->flags1 & RF1_DROP_4D2) ? 8 : 0) +
1617                  ((r_ptr->flags1 & RF1_DROP_3D2) ? 6 : 0) +
1618                  ((r_ptr->flags1 & RF1_DROP_2D2) ? 4 : 0) +
1619                  ((r_ptr->flags1 & RF1_DROP_1D2) ? 2 : 0) +
1620                  ((r_ptr->flags1 & RF1_DROP_90)  ? 1 : 0) +
1621                  ((r_ptr->flags1 & RF1_DROP_60)  ? 1 : 0));
1622
1623         /* Only "valid" drops */
1624         if (!(r_ptr->flags1 & RF1_ONLY_GOLD))
1625         {
1626                 if (r_ptr->r_drop_item != tmp_byte) n++;
1627                 r_ptr->r_drop_item = tmp_byte;
1628         }
1629         if (!(r_ptr->flags1 & RF1_ONLY_ITEM))
1630         {
1631                 if (r_ptr->r_drop_gold != tmp_byte) n++;
1632                 r_ptr->r_drop_gold = tmp_byte;
1633         }
1634
1635         /* Observe many spells */
1636         if (r_ptr->r_cast_spell != MAX_UCHAR) n++;
1637         r_ptr->r_cast_spell = MAX_UCHAR;
1638
1639         /* Count unknown flags */
1640         for (i = 0; i < 32; i++)
1641         {
1642                 if (!(r_ptr->r_flags1 & (1L << i)) &&
1643                     (r_ptr->flags1 & (1L << i))) n++;
1644                 if (!(r_ptr->r_flags2 & (1L << i)) &&
1645                     (r_ptr->flags2 & (1L << i))) n++;
1646                 if (!(r_ptr->r_flags3 & (1L << i)) &&
1647                     (r_ptr->flags3 & (1L << i))) n++;
1648                 if (!(r_ptr->r_flags4 & (1L << i)) &&
1649                     (r_ptr->flags4 & (1L << i))) n++;
1650                 if (!(r_ptr->r_flags5 & (1L << i)) &&
1651                     (r_ptr->a_ability_flags1 & (1L << i))) n++;
1652                 if (!(r_ptr->r_flags6 & (1L << i)) &&
1653                     (r_ptr->a_ability_flags2 & (1L << i))) n++;
1654                 if (!(r_ptr->r_flagsr & (1L << i)) &&
1655                     (r_ptr->flagsr & (1L << i))) n++;
1656
1657                 /* r_flags7 is actually unused */
1658 #if 0
1659                 if (!(r_ptr->r_flags7 & (1L << i)) &&
1660                     (r_ptr->flags7 & (1L << i))) n++;
1661 #endif
1662         }
1663
1664         /* Know all the flags */
1665         r_ptr->r_flags1 = r_ptr->flags1;
1666         r_ptr->r_flags2 = r_ptr->flags2;
1667         r_ptr->r_flags3 = r_ptr->flags3;
1668         r_ptr->r_flags4 = r_ptr->flags4;
1669         r_ptr->r_flags5 = r_ptr->a_ability_flags1;
1670         r_ptr->r_flags6 = r_ptr->a_ability_flags2;
1671         r_ptr->r_flagsr = r_ptr->flagsr;
1672
1673         /* r_flags7 is actually unused */
1674         /* r_ptr->r_flags7 = r_ptr->flags7; */
1675
1676         /* Know about evolution */
1677         if (!(r_ptr->r_xtra1 & MR1_SINKA)) n++;
1678         r_ptr->r_xtra1 |= MR1_SINKA;
1679
1680         /* Update monster recall window */
1681         if (p_ptr->monster_race_idx == r_idx)
1682         {
1683                 p_ptr->window |= (PW_MONSTER);
1684         }
1685
1686         /* Return the number of new flags learnt */
1687         return n;
1688 }
1689
1690
1691 /*!
1692  * @brief モンスターの撃破に伴うドロップ情報の保管処理 / Take note that the given monster just dropped some treasure
1693  * @param m_idx モンスター情報のID
1694  * @param num_item 手に入れたアイテム数
1695  * @param num_gold 手に入れた財宝の単位数
1696  * @return なし
1697  * @details
1698  * Note that learning the "GOOD"/"GREAT" flags gives information
1699  * about the treasure (even when the monster is killed for the first
1700  * time, such as uniques, and the treasure has not been examined yet).
1701  *
1702  * This "indirect" method is used to prevent the player from learning
1703  * exactly how much treasure a monster can drop from observing only
1704  * a single example of a drop.  This method actually observes how much
1705  * gold and items are dropped, and remembers that information to be
1706  * described later by the monster recall code.
1707  */
1708 void lore_treasure(MONSTER_IDX m_idx, ITEM_NUMBER num_item, ITEM_NUMBER num_gold)
1709 {
1710         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
1711
1712         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1713
1714         /* If the monster doesn't have original appearance, don't note */
1715         if (!is_original_ap(m_ptr)) return;
1716
1717         /* Note the number of things dropped */
1718         if (num_item > r_ptr->r_drop_item) r_ptr->r_drop_item = num_item;
1719         if (num_gold > r_ptr->r_drop_gold) r_ptr->r_drop_gold = num_gold;
1720
1721         /* Hack -- memorize the good/great flags */
1722         if (r_ptr->flags1 & (RF1_DROP_GOOD)) r_ptr->r_flags1 |= (RF1_DROP_GOOD);
1723         if (r_ptr->flags1 & (RF1_DROP_GREAT)) r_ptr->r_flags1 |= (RF1_DROP_GREAT);
1724
1725         /* Update monster recall window */
1726         if (p_ptr->monster_race_idx == m_ptr->r_idx)
1727         {
1728                 p_ptr->window |= (PW_MONSTER);
1729         }
1730 }
1731
1732
1733 /*!
1734  * @brief モンスターの各情報を更新する / This function updates the monster record of the given monster
1735  * @param m_idx 更新するモンスター情報のID
1736  * @param full プレイヤーとの距離更新を行うならばtrue
1737  * @return なし
1738  * @details
1739  * This involves extracting the distance to the player (if requested),
1740  * and then checking for visibility (natural, infravision, see-invis,
1741  * telepathy), updating the monster visibility flag, redrawing (or
1742  * erasing) the monster when its visibility changes, and taking note
1743  * of any interesting monster flags (cold-blooded, invisible, etc).
1744  *
1745  * Note the new "mflag" field which encodes several monster state flags,
1746  * including "view" for when the monster is currently in line of sight,
1747  * and "mark" for when the monster is currently visible via detection.
1748  *
1749  * The only monster fields that are changed here are "cdis" (the
1750  * distance from the player), "ml" (visible to the player), and
1751  * "mflag" (to maintain the "MFLAG_VIEW" flag).
1752  *
1753  * Note the special "update_monsters()" function which can be used to
1754  * call this function once for every monster.
1755  *
1756  * Note the "full" flag which requests that the "cdis" field be updated,
1757  * this is only needed when the monster (or the player) has moved.
1758  *
1759  * Every time a monster moves, we must call this function for that
1760  * monster, and update the distance, and the visibility.  Every time
1761  * the player moves, we must call this function for every monster, and
1762  * update the distance, and the visibility.  Whenever the player "state"
1763  * changes in certain ways ("blindness", "infravision", "telepathy",
1764  * and "see invisible"), we must call this function for every monster,
1765  * and update the visibility.
1766  *
1767  * Routines that change the "illumination" of a grid must also call this
1768  * function for any monster in that grid, since the "visibility" of some
1769  * monsters may be based on the illumination of their grid.
1770  *
1771  * Note that this function is called once per monster every time the
1772  * player moves.  When the player is running, this function is one
1773  * of the primary bottlenecks, along with "update_view()" and the
1774  * "process_monsters()" code, so efficiency is important.
1775  *
1776  * Note the optimized "inline" version of the "distance()" function.
1777  *
1778  * A monster is "visible" to the player if (1) it has been detected
1779  * by the player, (2) it is close to the player and the player has
1780  * telepathy, or (3) it is close to the player, and in line of sight
1781  * of the player, and it is "illuminated" by some combination of
1782  * infravision, torch light, or permanent light (invisible monsters
1783  * are only affected by "light" if the player can see invisible).
1784  *
1785  * Monsters which are not on the current panel may be "visible" to
1786  * the player, and their descriptions will include an "offscreen"
1787  * reference.  Currently, offscreen monsters cannot be targetted
1788  * or viewed directly, but old targets will remain set.  XXX XXX
1789  *
1790  * The player can choose to be disturbed by several things, including
1791  * "disturb_move" (monster which is viewable moves in some way), and
1792  * "disturb_near" (monster which is "easily" viewable moves in some
1793  * way).  Note that "moves" includes "appears" and "disappears".
1794  */
1795 void update_monster(MONSTER_IDX m_idx, bool full)
1796 {
1797         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
1798         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1799
1800         bool do_disturb = disturb_move;
1801
1802         POSITION d;
1803
1804         /* Current location */
1805         POSITION fy = m_ptr->fy;
1806         POSITION fx = m_ptr->fx;
1807
1808         /* Seen at all */
1809         bool flag = FALSE;
1810
1811         /* Seen by vision */
1812         bool easy = FALSE;
1813
1814         /* Non-Ninja player in the darkness */
1815         bool in_darkness = (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) && !p_ptr->see_nocto;
1816
1817         /* Do disturb? */
1818         if (disturb_high)
1819         {
1820                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1821
1822                 if (ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)
1823                         do_disturb = TRUE;
1824         }
1825
1826         /* Compute distance */
1827         if (full)
1828         {
1829                 /* Distance components */
1830                 int dy = (p_ptr->y > fy) ? (p_ptr->y - fy) : (fy - p_ptr->y);
1831                 int dx = (p_ptr->x > fx) ? (p_ptr->x - fx) : (fx - p_ptr->x);
1832
1833                 /* Approximate distance */
1834                 d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
1835
1836                 /* Restrict distance */
1837                 if (d > 255) d = 255;
1838
1839                 if (!d) d = 1;
1840
1841                 /* Save the distance */
1842                 m_ptr->cdis = d;
1843         }
1844
1845         /* Extract distance */
1846         else
1847         {
1848                 /* Extract the distance */
1849                 d = m_ptr->cdis;
1850         }
1851
1852
1853         /* Detected */
1854         if (m_ptr->mflag2 & (MFLAG2_MARK)) flag = TRUE;
1855
1856
1857         /* Nearby */
1858         if (d <= (in_darkness ? MAX_SIGHT / 2 : MAX_SIGHT))
1859         {
1860                 if (!in_darkness || (d <= MAX_SIGHT / 4))
1861                 {
1862                         if (p_ptr->special_defense & KATA_MUSOU)
1863                         {
1864                                 /* Detectable */
1865                                 flag = TRUE;
1866
1867                                 if (is_original_ap(m_ptr) && !p_ptr->image)
1868                                 {
1869                                         /* Hack -- Memorize mental flags */
1870                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
1871                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
1872                                 }
1873                         }
1874
1875                         /* Basic telepathy */
1876                         /* Snipers get telepathy when they concentrate deeper */
1877                         else if (p_ptr->telepathy)
1878                         {
1879                                 /* Empty mind, no telepathy */
1880                                 if (r_ptr->flags2 & (RF2_EMPTY_MIND))
1881                                 {
1882                                         /* Memorize flags */
1883                                         if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags2 |= (RF2_EMPTY_MIND);
1884                                 }
1885
1886                                 /* Weird mind, occasional telepathy */
1887                                 else if (r_ptr->flags2 & (RF2_WEIRD_MIND))
1888                                 {
1889                                         /* One in ten individuals are detectable */
1890                                         if ((m_idx % 10) == 5)
1891                                         {
1892                                                 /* Detectable */
1893                                                 flag = TRUE;
1894
1895                                                 if (is_original_ap(m_ptr) && !p_ptr->image)
1896                                                 {
1897                                                         /* Memorize flags */
1898                                                         r_ptr->r_flags2 |= (RF2_WEIRD_MIND);
1899
1900                                                         /* Hack -- Memorize mental flags */
1901                                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
1902                                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
1903                                                 }
1904                                         }
1905                                 }
1906
1907                                 /* Normal mind, allow telepathy */
1908                                 else
1909                                 {
1910                                         /* Detectable */
1911                                         flag = TRUE;
1912
1913                                         if (is_original_ap(m_ptr) && !p_ptr->image)
1914                                         {
1915                                                 /* Hack -- Memorize mental flags */
1916                                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
1917                                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
1918                                         }
1919                                 }
1920                         }
1921
1922                         if ((p_ptr->esp_animal) && (r_ptr->flags3 & (RF3_ANIMAL)))
1923                         {
1924                                 flag = TRUE;
1925                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_ANIMAL);
1926                         }
1927
1928                         if ((p_ptr->esp_undead) && (r_ptr->flags3 & (RF3_UNDEAD)))
1929                         {
1930                                 flag = TRUE;
1931                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_UNDEAD);
1932                         }
1933
1934                         if ((p_ptr->esp_demon) && (r_ptr->flags3 & (RF3_DEMON)))
1935                         {
1936                                 flag = TRUE;
1937                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_DEMON);
1938                         }
1939
1940                         if ((p_ptr->esp_orc) && (r_ptr->flags3 & (RF3_ORC)))
1941                         {
1942                                 flag = TRUE;
1943                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_ORC);
1944                         }
1945
1946                         if ((p_ptr->esp_troll) && (r_ptr->flags3 & (RF3_TROLL)))
1947                         {
1948                                 flag = TRUE;
1949                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_TROLL);
1950                         }
1951
1952                         if ((p_ptr->esp_giant) && (r_ptr->flags3 & (RF3_GIANT)))
1953                         {
1954                                 flag = TRUE;
1955                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_GIANT);
1956                         }
1957
1958                         if ((p_ptr->esp_dragon) && (r_ptr->flags3 & (RF3_DRAGON)))
1959                         {
1960                                 flag = TRUE;
1961                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_DRAGON);
1962                         }
1963
1964                         if ((p_ptr->esp_human) && (r_ptr->flags2 & (RF2_HUMAN)))
1965                         {
1966                                 flag = TRUE;
1967                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags2 |= (RF2_HUMAN);
1968                         }
1969
1970                         if ((p_ptr->esp_evil) && (r_ptr->flags3 & (RF3_EVIL)))
1971                         {
1972                                 flag = TRUE;
1973                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_EVIL);
1974                         }
1975
1976                         if ((p_ptr->esp_good) && (r_ptr->flags3 & (RF3_GOOD)))
1977                         {
1978                                 flag = TRUE;
1979                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_GOOD);
1980                         }
1981
1982                         if ((p_ptr->esp_nonliving) &&
1983                             ((r_ptr->flags3 & (RF3_DEMON | RF3_UNDEAD | RF3_NONLIVING)) == RF3_NONLIVING))
1984                         {
1985                                 flag = TRUE;
1986                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_NONLIVING);
1987                         }
1988
1989                         if ((p_ptr->esp_unique) && (r_ptr->flags1 & (RF1_UNIQUE)))
1990                         {
1991                                 flag = TRUE;
1992                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags1 |= (RF1_UNIQUE);
1993                         }
1994                 }
1995
1996                 /* Normal line of sight, and not blind */
1997                 if (player_has_los_bold(fy, fx) && !p_ptr->blind)
1998                 {
1999                         bool do_invisible = FALSE;
2000                         bool do_cold_blood = FALSE;
2001
2002                         /* Snipers can see targets in darkness when they concentrate deeper */
2003                         if (p_ptr->concent >= CONCENT_RADAR_THRESHOLD)
2004                         {
2005                                 /* Easy to see */
2006                                 easy = flag = TRUE;
2007                         }
2008
2009                         /* Use "infravision" */
2010                         if (d <= p_ptr->see_infra)
2011                         {
2012                                 /* Handle "cold blooded" monsters */
2013                                 if ((r_ptr->flags2 & (RF2_COLD_BLOOD | RF2_AURA_FIRE)) == RF2_COLD_BLOOD)
2014                                 {
2015                                         do_cold_blood = TRUE;
2016                                 }
2017
2018                                 /* Handle "warm blooded" monsters */
2019                                 else
2020                                 {
2021                                         /* Easy to see */
2022                                         easy = flag = TRUE;
2023                                 }
2024                         }
2025
2026                         /* Use "illumination" */
2027                         if (player_can_see_bold(fy, fx))
2028                         {
2029                                 /* Handle "invisible" monsters */
2030                                 if (r_ptr->flags2 & (RF2_INVISIBLE))
2031                                 {
2032                                         do_invisible = TRUE;
2033
2034                                         /* See invisible */
2035                                         if (p_ptr->see_inv)
2036                                         {
2037                                                 /* Easy to see */
2038                                                 easy = flag = TRUE;
2039                                         }
2040                                 }
2041
2042                                 /* Handle "normal" monsters */
2043                                 else
2044                                 {
2045                                         /* Easy to see */
2046                                         easy = flag = TRUE;
2047                                 }
2048                         }
2049
2050                         /* Visible */
2051                         if (flag)
2052                         {
2053                                 if (is_original_ap(m_ptr) && !p_ptr->image)
2054                                 {
2055                                         /* Memorize flags */
2056                                         if (do_invisible) r_ptr->r_flags2 |= (RF2_INVISIBLE);
2057                                         if (do_cold_blood) r_ptr->r_flags2 |= (RF2_COLD_BLOOD);
2058                                 }
2059                         }
2060                 }
2061         }
2062
2063
2064         /* The monster is now visible */
2065         if (flag)
2066         {
2067                 /* It was previously unseen */
2068                 if (!m_ptr->ml)
2069                 {
2070                         /* Mark as visible */
2071                         m_ptr->ml = TRUE;
2072
2073                         /* Draw the monster */
2074                         lite_spot(fy, fx);
2075
2076                         /* Update health bar as needed */
2077                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2078                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2079
2080                         /* Hack -- Count "fresh" sightings */
2081                         if (!p_ptr->image)
2082                         {
2083                                 if ((m_ptr->ap_r_idx == MON_KAGE) && (r_info[MON_KAGE].r_sights < MAX_SHORT))
2084                                         r_info[MON_KAGE].r_sights++;
2085                                 else if (is_original_ap(m_ptr) && (r_ptr->r_sights < MAX_SHORT))
2086                                         r_ptr->r_sights++;
2087                         }
2088
2089                         /* Eldritch Horror */
2090                         if (r_info[m_ptr->ap_r_idx].flags2 & RF2_ELDRITCH_HORROR)
2091                         {
2092                                 sanity_blast(m_ptr, FALSE);
2093                         }
2094
2095                         /* Disturb on appearance */
2096                         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)))
2097                         {
2098                                 if (disturb_pets || is_hostile(m_ptr))
2099                                         disturb(TRUE, TRUE);
2100                         }
2101                 }
2102         }
2103
2104         /* The monster is not visible */
2105         else
2106         {
2107                 /* It was previously seen */
2108                 if (m_ptr->ml)
2109                 {
2110                         /* Mark as not visible */
2111                         m_ptr->ml = FALSE;
2112
2113                         /* Erase the monster */
2114                         lite_spot(fy, fx);
2115
2116                         /* Update health bar as needed */
2117                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2118                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2119
2120                         /* Disturb on disappearance */
2121                         if (do_disturb)
2122                         {
2123                                 if (disturb_pets || is_hostile(m_ptr))
2124                                         disturb(TRUE, TRUE);
2125                         }
2126                 }
2127         }
2128
2129
2130         /* The monster is now easily visible */
2131         if (easy)
2132         {
2133                 /* Change */
2134                 if (!(m_ptr->mflag & (MFLAG_VIEW)))
2135                 {
2136                         /* Mark as easily visible */
2137                         m_ptr->mflag |= (MFLAG_VIEW);
2138
2139                         /* Disturb on appearance */
2140                         if (do_disturb)
2141                         {
2142                                 if (disturb_pets || is_hostile(m_ptr))
2143                                         disturb(TRUE, TRUE);
2144                         }
2145                 }
2146         }
2147
2148         /* The monster is not easily visible */
2149         else
2150         {
2151                 /* Change */
2152                 if (m_ptr->mflag & (MFLAG_VIEW))
2153                 {
2154                         /* Mark as not easily visible */
2155                         m_ptr->mflag &= ~(MFLAG_VIEW);
2156
2157                         /* Disturb on disappearance */
2158                         if (do_disturb)
2159                         {
2160                                 if (disturb_pets || is_hostile(m_ptr))
2161                                         disturb(TRUE, TRUE);
2162                         }
2163                 }
2164         }
2165 }
2166
2167
2168 /*!
2169  * @brief 単純に生存している全モンスターの更新処理を行う / This function simply updates all the (non-dead) monsters (see above).
2170  * @param full 距離更新を行うならtrue
2171  * @return なし
2172  */
2173 void update_monsters(bool full)
2174 {
2175         MONSTER_IDX i;
2176
2177         /* Update each (live) monster */
2178         for (i = 1; i < m_max; i++)
2179         {
2180                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
2181                 if (!monster_is_valid(m_ptr)) continue;
2182                 update_monster(i, full);
2183         }
2184 }
2185
2186
2187 /*!
2188  * @brief カメレオンの王の変身対象となるモンスターかどうか判定する / Hack -- the index of the summoning monster
2189  * @param r_idx モンスター種族ID
2190  * @return 対象にできるならtrueを返す
2191  */
2192 static bool monster_hook_chameleon_lord(MONRACE_IDX r_idx)
2193 {
2194         monster_race *r_ptr = &r_info[r_idx];
2195         monster_type *m_ptr = &current_floor_ptr->m_list[chameleon_change_m_idx];
2196         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2197
2198         if (!(r_ptr->flags1 & (RF1_UNIQUE))) return FALSE;
2199         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2200
2201         if (ABS(r_ptr->level - r_info[MON_CHAMELEON_K].level) > 5) return FALSE;
2202
2203         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))
2204                 return FALSE;
2205
2206         if (!monster_can_cross_terrain(current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2207
2208         /* Not born */
2209         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2210         {
2211                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
2212         }
2213
2214         /* Born now */
2215         else if (summon_specific_who > 0)
2216         {
2217                 if (monster_has_hostile_align(&current_floor_ptr->m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2218         }
2219
2220         return TRUE;
2221 }
2222
2223 /*!
2224  * @brief カメレオンの変身対象となるモンスターかどうか判定する / Hack -- the index of the summoning monster
2225  * @param r_idx モンスター種族ID
2226  * @return 対象にできるならtrueを返す
2227  * @todo グローバル変数対策の上 monster_hook.cへ移す。
2228  */
2229 static bool monster_hook_chameleon(MONRACE_IDX r_idx)
2230 {
2231         monster_race *r_ptr = &r_info[r_idx];
2232         monster_type *m_ptr = &current_floor_ptr->m_list[chameleon_change_m_idx];
2233         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2234
2235         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2236         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2237         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2238         
2239         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))
2240                 return FALSE;
2241
2242         if (!monster_can_cross_terrain(current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2243
2244         /* Not born */
2245         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2246         {
2247                 if ((old_r_ptr->flags3 & RF3_GOOD) && !(r_ptr->flags3 & RF3_GOOD)) return FALSE;
2248                 if ((old_r_ptr->flags3 & RF3_EVIL) && !(r_ptr->flags3 & RF3_EVIL)) return FALSE;
2249                 if (!(old_r_ptr->flags3 & (RF3_GOOD | RF3_EVIL)) && (r_ptr->flags3 & (RF3_GOOD | RF3_EVIL))) return FALSE;
2250         }
2251
2252         /* Born now */
2253         else if (summon_specific_who > 0)
2254         {
2255                 if (monster_has_hostile_align(&current_floor_ptr->m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2256         }
2257
2258         return (*(get_monster_hook()))(r_idx);
2259 }
2260
2261 /*!
2262  * @brief モンスターの変身処理
2263  * @param m_idx 変身処理を受けるモンスター情報のID
2264  * @param born 生成時の初変身先指定ならばtrue
2265  * @param r_idx 旧モンスター種族のID
2266  * @return なし
2267  */
2268 void choose_new_monster(MONSTER_IDX m_idx, bool born, MONRACE_IDX r_idx)
2269 {
2270         int oldmaxhp;
2271         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
2272         monster_race *r_ptr;
2273         char old_m_name[MAX_NLEN];
2274         bool old_unique = FALSE;
2275         int old_r_idx = m_ptr->r_idx;
2276
2277         if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2278                 old_unique = TRUE;
2279         if (old_unique && (r_idx == MON_CHAMELEON)) r_idx = MON_CHAMELEON_K;
2280         r_ptr = &r_info[r_idx];
2281
2282         monster_desc(old_m_name, m_ptr, 0);
2283
2284         if (!r_idx)
2285         {
2286                 DEPTH level;
2287
2288                 chameleon_change_m_idx = m_idx;
2289                 if (old_unique)
2290                         get_mon_num_prep(monster_hook_chameleon_lord, NULL);
2291                 else
2292                         get_mon_num_prep(monster_hook_chameleon, NULL);
2293
2294                 if (old_unique)
2295                         level = r_info[MON_CHAMELEON_K].level;
2296                 else if (!current_floor_ptr->dun_level)
2297                         level = wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].level;
2298                 else
2299                         level = current_floor_ptr->dun_level;
2300
2301                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_CHAMELEON) level+= 2+randint1(3);
2302
2303                 r_idx = get_mon_num(level);
2304                 r_ptr = &r_info[r_idx];
2305
2306                 chameleon_change_m_idx = 0;
2307                 if (!r_idx) return;
2308         }
2309
2310         m_ptr->r_idx = r_idx;
2311         m_ptr->ap_r_idx = r_idx;
2312         update_monster(m_idx, FALSE);
2313         lite_spot(m_ptr->fy, m_ptr->fx);
2314
2315         if ((r_info[old_r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)) ||
2316             (r_ptr->flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)))
2317                 p_ptr->update |= (PU_MON_LITE);
2318
2319         if (born)
2320         {
2321                 /* Sub-alignment of a chameleon */
2322                 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
2323                 {
2324                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2325                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2326                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2327                 }
2328                 return;
2329         }
2330
2331         if (m_idx == p_ptr->riding)
2332         {
2333                 GAME_TEXT m_name[MAX_NLEN];
2334                 monster_desc(m_name, m_ptr, 0);
2335                 msg_format(_("突然%sが変身した。", "Suddenly, %s transforms!"), old_m_name);
2336                 if (!(r_ptr->flags7 & RF7_RIDING))
2337                         if (rakuba(0, TRUE)) msg_format(_("地面に落とされた。", "You have fallen from %s."), m_name);
2338         }
2339
2340         /* Extract the monster base speed */
2341         m_ptr->mspeed = get_mspeed(r_ptr);
2342
2343         oldmaxhp = m_ptr->max_maxhp;
2344         /* Assign maximal hitpoints */
2345         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2346         {
2347                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2348         }
2349         else
2350         {
2351                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2352         }
2353
2354         /* Monsters have double hitpoints in Nightmare mode */
2355         if (ironman_nightmare)
2356         {
2357                 u32b hp = m_ptr->max_maxhp * 2L;
2358                 m_ptr->max_maxhp = (HIT_POINT)MIN(30000, hp);
2359         }
2360
2361         m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
2362         if (m_ptr->maxhp < 1) m_ptr->maxhp = 1;
2363         m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
2364         
2365         /* reset dealt_damage */
2366         m_ptr->dealt_damage = 0;
2367 }
2368
2369
2370 /*!
2371  * @brief たぬきの変身対象となるモンスターかどうか判定する / Hook for Tanuki
2372  * @param r_idx モンスター種族ID
2373  * @return 対象にできるならtrueを返す
2374  * @todo グローバル変数対策の上 monster_hook.cへ移す。
2375  */
2376 static bool monster_hook_tanuki(MONRACE_IDX r_idx)
2377 {
2378         monster_race *r_ptr = &r_info[r_idx];
2379
2380         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2381         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2382         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2383         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2384         
2385         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))
2386                 return FALSE;
2387
2388         return (*(get_monster_hook()))(r_idx);
2389 }
2390
2391
2392 /*!
2393  * @brief モンスターの表層IDを設定する / Set initial racial appearance of a monster
2394  * @param r_idx モンスター種族ID
2395  * @return モンスター種族の表層ID
2396  */
2397 static MONRACE_IDX initial_r_appearance(MONRACE_IDX r_idx)
2398 {
2399         int attempts = 1000;
2400         MONRACE_IDX ap_r_idx;
2401         DEPTH min = MIN(current_floor_ptr->base_level-5, 50);
2402
2403         if (p_ptr->pseikaku == SEIKAKU_CHARGEMAN)
2404         {
2405                 if (current_floor_ptr->base_level == 0 || one_in_(5)) return MON_ALIEN_JURAL;
2406         }
2407
2408         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2409                 return r_idx;
2410
2411         get_mon_num_prep(monster_hook_tanuki, NULL);
2412
2413         while (--attempts)
2414         {
2415                 ap_r_idx = get_mon_num(current_floor_ptr->base_level + 10);
2416                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2417         }
2418
2419         return r_idx;
2420 }
2421
2422
2423 /*!
2424  * @brief モンスターの個体加速を設定する / Get initial monster speed
2425  * @param r_ptr モンスター種族の参照ポインタ
2426  * @return 加速値
2427  */
2428 SPEED get_mspeed(monster_race *r_ptr)
2429 {
2430         /* Extract the monster base speed */
2431         SPEED mspeed = r_ptr->speed;
2432
2433         /* Hack -- small racial variety */
2434         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2435         {
2436                 /* Allow some small variation per monster */
2437                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2438                 if (i) mspeed += rand_spread(0, i);
2439         }
2440
2441         if (mspeed > 199) mspeed = 199;
2442
2443         return mspeed;
2444 }
2445
2446
2447 /*!
2448  * @brief モンスターを一体生成する / Attempt to place a monster of the given race at the given location.
2449  * @param who 召喚を行ったモンスターID
2450  * @param y 生成位置y座標
2451  * @param x 生成位置x座標
2452  * @param r_idx 生成モンスター種族
2453  * @param mode 生成オプション
2454  * @return 成功したらtrue
2455  * @details 
2456  * To give the player a sporting chance, any monster that appears in
2457  * line-of-sight and is extremely dangerous can be marked as
2458  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2459  * which often (but not always) lets the player move before they do.
2460  *
2461  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2462  *
2463  * Use special "here" and "dead" flags for unique monsters,
2464  * remove old "cur_num" and "max_num" fields.
2465  *
2466  * Actually, do something similar for artifacts, to simplify
2467  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2468  *
2469  * This is the only function which may place a monster in the dungeon,
2470  * except for the savefile loading code.
2471  */
2472 static bool place_monster_one(MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
2473 {
2474         grid_type               *g_ptr = &current_floor_ptr->grid_array[y][x];
2475         monster_type    *m_ptr;
2476         monster_race    *r_ptr = &r_info[r_idx];
2477         concptr         name = (r_name + r_ptr->name);
2478
2479         int cmi;
2480
2481         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
2482         if (p_ptr->wild_mode) return FALSE;
2483
2484         /* Verify location */
2485         if (!in_bounds(y, x)) return (FALSE);
2486         if (!r_idx) return (FALSE);
2487         if (!r_ptr->name) return (FALSE);
2488
2489         if (!(mode & PM_IGNORE_TERRAIN))
2490         {
2491                 /* Not on the Pattern */
2492                 if (pattern_tile(y, x)) return FALSE;
2493
2494                 /* Require empty space (if not ghostly) */
2495                 if (!monster_can_enter(y, x, r_ptr, 0)) return FALSE;
2496         }
2497
2498         if (!p_ptr->inside_battle)
2499         {
2500                 /* Hack -- "unique" monsters must be "unique" */
2501                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
2502                      (r_ptr->flags7 & (RF7_NAZGUL))) &&
2503                     (r_ptr->cur_num >= r_ptr->max_num))
2504                 {
2505                         /* Cannot create */
2506                         return (FALSE);
2507                 }
2508
2509                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
2510                     (r_ptr->cur_num >= 1))
2511                 {
2512                         return (FALSE);
2513                 }
2514
2515                 if (r_idx == MON_BANORLUPART)
2516                 {
2517                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
2518                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
2519                 }
2520
2521                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
2522                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (current_floor_ptr->dun_level < r_ptr->level) &&
2523                     (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
2524                 {
2525                         /* Cannot create */
2526                         return (FALSE);
2527                 }
2528         }
2529
2530         if (quest_number(current_floor_ptr->dun_level))
2531         {
2532                 int hoge = quest_number(current_floor_ptr->dun_level);
2533                 if ((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
2534                 {
2535                         if(r_idx == quest[hoge].r_idx)
2536                         {
2537                                 int number_mon, i2, j2;
2538                                 number_mon = 0;
2539
2540                                 /* Count all quest monsters */
2541                                 for (i2 = 0; i2 < current_floor_ptr->width; ++i2)
2542                                         for (j2 = 0; j2 < current_floor_ptr->height; j2++)
2543                                                 if (current_floor_ptr->grid_array[j2][i2].m_idx > 0)
2544                                                         if (current_floor_ptr->m_list[current_floor_ptr->grid_array[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
2545                                                                 number_mon++;
2546                                 if(number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
2547                                         return FALSE;
2548                         }
2549                 }
2550         }
2551
2552         if (is_glyph_grid(g_ptr))
2553         {
2554                 if (randint1(BREAK_GLYPH) < (r_ptr->level+20))
2555                 {
2556                         /* Describe observable breakage */
2557                         if (g_ptr->info & CAVE_MARK)
2558                         {
2559                                 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2560                         }
2561
2562                         /* Forget the rune */
2563                         g_ptr->info &= ~(CAVE_MARK);
2564
2565                         /* Break the rune */
2566                         g_ptr->info &= ~(CAVE_OBJECT);
2567                         g_ptr->mimic = 0;
2568
2569                         note_spot(y, x);
2570                 }
2571                 else return FALSE;
2572         }
2573
2574         msg_format_wizard(CHEAT_MONSTER, _("%s(Lv%d)を生成しました。", "%s(Lv%d) was generated."), name, r_ptr->level);
2575
2576         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL) || (r_ptr->level < 10)) mode &= ~PM_KAGE;
2577
2578         /* Make a new monster */
2579         g_ptr->m_idx = m_pop();
2580         hack_m_idx_ii = g_ptr->m_idx;
2581
2582         /* Mega-Hack -- catch "failure" */
2583         if (!g_ptr->m_idx) return (FALSE);
2584
2585
2586         /* Get a new monster record */
2587         m_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
2588
2589         /* Save the race */
2590         m_ptr->r_idx = r_idx;
2591         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
2592
2593         /* No flags */
2594         m_ptr->mflag = 0;
2595         m_ptr->mflag2 = 0;
2596
2597         /* Hack -- Appearance transfer */
2598         if ((mode & PM_MULTIPLY) && (who > 0) && !is_original_ap(&current_floor_ptr->m_list[who]))
2599         {
2600                 m_ptr->ap_r_idx = current_floor_ptr->m_list[who].ap_r_idx;
2601
2602                 /* Hack -- Shadower spawns Shadower */
2603                 if (current_floor_ptr->m_list[who].mflag2 & MFLAG2_KAGE) m_ptr->mflag2 |= MFLAG2_KAGE;
2604         }
2605
2606         /* Sub-alignment of a monster */
2607         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
2608                 m_ptr->sub_align = current_floor_ptr->m_list[who].sub_align;
2609         else
2610         {
2611                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2612                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2613                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2614         }
2615
2616         /* Place the monster at the location */
2617         m_ptr->fy = y;
2618         m_ptr->fx = x;
2619
2620
2621         /* No "timed status" yet */
2622         for (cmi = 0; cmi < MAX_MTIMED; cmi++) m_ptr->mtimed[cmi] = 0;
2623
2624         /* Unknown distance */
2625         m_ptr->cdis = 0;
2626
2627         reset_target(m_ptr);
2628
2629         m_ptr->nickname = 0;
2630
2631         m_ptr->exp = 0;
2632
2633
2634         /* Your pet summons its pet. */
2635         if (who > 0 && is_pet(&current_floor_ptr->m_list[who]))
2636         {
2637                 mode |= PM_FORCE_PET;
2638                 m_ptr->parent_m_idx = who;
2639         }
2640         else
2641         {
2642                 m_ptr->parent_m_idx = 0;
2643         }
2644
2645         if (r_ptr->flags7 & RF7_CHAMELEON)
2646         {
2647                 choose_new_monster(g_ptr->m_idx, TRUE, 0);
2648                 r_ptr = &r_info[m_ptr->r_idx];
2649                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
2650
2651                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
2652                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
2653                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2654         }
2655         else if ((mode & PM_KAGE) && !(mode & PM_FORCE_PET))
2656         {
2657                 m_ptr->ap_r_idx = MON_KAGE;
2658                 m_ptr->mflag2 |= MFLAG2_KAGE;
2659         }
2660
2661         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
2662
2663         /* Not visible */
2664         m_ptr->ml = FALSE;
2665
2666         /* Pet? */
2667         if (mode & PM_FORCE_PET)
2668         {
2669                 set_pet(m_ptr);
2670         }
2671         /* Friendly? */
2672         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
2673                  (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(who))
2674         {
2675                 if (!monster_has_hostile_align(NULL, 0, -1, r_ptr)) set_friendly(m_ptr);
2676         }
2677
2678         /* Assume no sleeping */
2679         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
2680
2681         /* Enforce sleeping if needed */
2682         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
2683         {
2684                 int val = r_ptr->sleep;
2685                 (void)set_monster_csleep(g_ptr->m_idx, (val * 2) + randint1(val * 10));
2686         }
2687
2688         /* Assign maximal hitpoints */
2689         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2690         {
2691                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2692         }
2693         else
2694         {
2695                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2696         }
2697
2698         /* Monsters have double hitpoints in Nightmare mode */
2699         if (ironman_nightmare)
2700         {
2701                 u32b hp = m_ptr->max_maxhp * 2L;
2702
2703                 m_ptr->max_maxhp = (HIT_POINT)MIN(30000, hp);
2704         }
2705
2706         m_ptr->maxhp = m_ptr->max_maxhp;
2707
2708         /* And start out fully healthy */
2709         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
2710                 m_ptr->hp = m_ptr->maxhp / 2;
2711         else m_ptr->hp = m_ptr->maxhp;
2712         
2713         
2714         /* dealt damage is 0 at initial*/
2715         m_ptr->dealt_damage = 0;
2716
2717
2718         /* Extract the monster base speed */
2719         m_ptr->mspeed = get_mspeed(r_ptr);
2720
2721         if (mode & PM_HASTE) (void)set_monster_fast(g_ptr->m_idx, 100);
2722
2723         /* Give a random starting energy */
2724         if (!ironman_nightmare)
2725         {
2726                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
2727         }
2728         else
2729         {
2730                 /* Nightmare monsters are more prepared */
2731                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
2732         }
2733
2734         /* Force monster to wait for player, unless in Nightmare mode */
2735         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
2736         {
2737                 /* Monster is still being nice */
2738                 m_ptr->mflag |= (MFLAG_NICE);
2739
2740                 /* Must repair monsters */
2741                 repair_monsters = TRUE;
2742         }
2743
2744         /* Hack -- see "process_monsters()" */
2745         if (g_ptr->m_idx < hack_m_idx)
2746         {
2747                 /* Monster is still being born */
2748                 m_ptr->mflag |= (MFLAG_BORN);
2749         }
2750
2751
2752         if (r_ptr->flags7 & RF7_SELF_LD_MASK)
2753                 p_ptr->update |= (PU_MON_LITE);
2754         else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !MON_CSLEEP(m_ptr))
2755                 p_ptr->update |= (PU_MON_LITE);
2756         update_monster(g_ptr->m_idx, TRUE);
2757
2758
2759         /* Count the monsters on the level */
2760         real_r_ptr(m_ptr)->cur_num++;
2761
2762         /*
2763          * Memorize location of the unique monster in saved floors.
2764          * A unique monster move from old saved floor.
2765          */
2766         if (character_dungeon &&
2767             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
2768                 real_r_ptr(m_ptr)->floor_id = p_ptr->floor_id;
2769
2770         /* Hack -- Count the number of "reproducers" */
2771         if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
2772
2773         /* Hack -- Notice new multi-hued monsters */
2774         {
2775                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2776                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
2777                         shimmer_monsters = TRUE;
2778         }
2779
2780         if (p_ptr->warning && character_dungeon)
2781         {
2782                 if (r_ptr->flags1 & RF1_UNIQUE)
2783                 {
2784                         concptr color;
2785                         object_type *o_ptr;
2786                         GAME_TEXT o_name[MAX_NLEN];
2787
2788                         if (r_ptr->level > p_ptr->lev + 30)
2789                                 color = _("黒く", "black");
2790                         else if (r_ptr->level > p_ptr->lev + 15)
2791                                 color = _("紫色に", "purple");
2792                         else if (r_ptr->level > p_ptr->lev + 5)
2793                                 color = _("ルビー色に", "deep red");
2794                         else if (r_ptr->level > p_ptr->lev - 5)
2795                                 color = _("赤く", "red");
2796                         else if (r_ptr->level > p_ptr->lev - 15)
2797                                 color = _("ピンク色に", "pink");
2798                         else
2799                                 color = _("白く", "white");
2800
2801                         o_ptr = choose_warning_item();
2802                         if (o_ptr)
2803                         {
2804                                 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
2805                                 msg_format(_("%sは%s光った。", "%s glows %s."), o_name, color);
2806                         }
2807                         else
2808                         {
2809                                 msg_format(_("%s光る物が頭に浮かんだ。", "An %s image forms in your mind."), color);
2810                         }
2811                 }
2812         }
2813
2814         if (is_explosive_rune_grid(g_ptr))
2815         {
2816                 /* Break the ward */
2817                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2818                 {
2819                         /* Describe observable breakage */
2820                         if (g_ptr->info & CAVE_MARK)
2821                         {
2822                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2823                                 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);
2824                         }
2825                 }
2826                 else
2827                 {
2828                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2829                 }
2830
2831                 /* Forget the rune */
2832                 g_ptr->info &= ~(CAVE_MARK);
2833
2834                 /* Break the rune */
2835                 g_ptr->info &= ~(CAVE_OBJECT);
2836                 g_ptr->mimic = 0;
2837
2838                 note_spot(y, x);
2839                 lite_spot(y, x);
2840         }
2841
2842         /* Success */
2843         return (TRUE);
2844 }
2845
2846
2847
2848 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
2849
2850 /*!
2851  * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
2852  * @param r_idx 生成モンスター種族
2853  * @param yp 結果生成位置y座標
2854  * @param xp 結果生成位置x座標
2855  * @param y 中心生成位置y座標
2856  * @param x 中心生成位置x座標
2857  * @param max_dist 生成位置の最大半径
2858  * @return 成功したらtrue
2859  *  
2860  */
2861 static bool mon_scatter(MONRACE_IDX r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
2862 {
2863         POSITION place_x[MON_SCAT_MAXD];
2864         POSITION place_y[MON_SCAT_MAXD];
2865         int num[MON_SCAT_MAXD];
2866         int i;
2867         POSITION nx, ny;
2868
2869         if (max_dist >= MON_SCAT_MAXD)
2870                 return FALSE;
2871
2872         for (i = 0; i < MON_SCAT_MAXD; i++)
2873                 num[i] = 0;
2874
2875         for (nx = x - max_dist; nx <= x + max_dist; nx++)
2876         {
2877                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
2878                 {
2879                         /* Ignore annoying locations */
2880                         if (!in_bounds(ny, nx)) continue;
2881
2882                         /* Require "line of projection" */
2883                         if (!projectable(y, x, ny, nx)) continue;
2884
2885                         if (r_idx > 0)
2886                         {
2887                                 monster_race *r_ptr = &r_info[r_idx];
2888
2889                                 /* Require empty space (if not ghostly) */
2890                                 if (!monster_can_enter(ny, nx, r_ptr, 0))
2891                                         continue;
2892                         }
2893                         else
2894                         {
2895                                 /* Walls and Monsters block flow */
2896                                 if (!cave_empty_bold2(ny, nx)) continue;
2897
2898                                 /* ... nor on the Pattern */
2899                                 if (pattern_tile(ny, nx)) continue;
2900                         }
2901
2902                         i = distance(y, x, ny, nx);
2903
2904                         if (i > max_dist)
2905                                 continue;
2906
2907                         num[i]++;
2908
2909                         /* random swap */
2910                         if (one_in_(num[i]))
2911                         {
2912                                 place_x[i] = nx;
2913                                 place_y[i] = ny;
2914                         }
2915                 }
2916         }
2917
2918         i = 0;
2919         while (i < MON_SCAT_MAXD && 0 == num[i])
2920                 i++;
2921         if (i >= MON_SCAT_MAXD)
2922                 return FALSE;
2923
2924         *xp = place_x[i];
2925         *yp = place_y[i];
2926
2927         return TRUE;
2928 }
2929
2930 /*!
2931  * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
2932  * @param who 召喚主のモンスター情報ID
2933  * @param y 中心生成位置y座標
2934  * @param x 中心生成位置x座標
2935  * @param r_idx 生成モンスター種族
2936  * @param mode 生成オプション
2937  * @return 成功したらtrue
2938  */
2939 static bool place_monster_group(MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
2940 {
2941         monster_race *r_ptr = &r_info[r_idx];
2942
2943         int n, i;
2944         int total = 0, extra = 0;
2945
2946         int hack_n = 0;
2947
2948         POSITION hack_y[GROUP_MAX];
2949         POSITION hack_x[GROUP_MAX];
2950
2951
2952         /* Pick a group size */
2953         total = randint1(10);
2954
2955         /* Hard monsters, small groups */
2956         if (r_ptr->level > current_floor_ptr->dun_level)
2957         {
2958                 extra = r_ptr->level - current_floor_ptr->dun_level;
2959                 extra = 0 - randint1(extra);
2960         }
2961
2962         /* Easy monsters, large groups */
2963         else if (r_ptr->level < current_floor_ptr->dun_level)
2964         {
2965                 extra = current_floor_ptr->dun_level - r_ptr->level;
2966                 extra = randint1(extra);
2967         }
2968
2969         /* Hack -- limit group reduction */
2970         if (extra > 9) extra = 9;
2971
2972         /* Modify the group size */
2973         total += extra;
2974
2975         /* Minimum size */
2976         if (total < 1) total = 1;
2977
2978         /* Maximum size */
2979         if (total > GROUP_MAX) total = GROUP_MAX;
2980
2981
2982         /* Start on the monster */
2983         hack_n = 1;
2984         hack_x[0] = x;
2985         hack_y[0] = y;
2986
2987         /* Puddle monsters, breadth first, up to total */
2988         for (n = 0; (n < hack_n) && (hack_n < total); n++)
2989         {
2990                 /* Grab the location */
2991                 POSITION hx = hack_x[n];
2992                 POSITION hy = hack_y[n];
2993
2994                 /* Check each direction, up to total */
2995                 for (i = 0; (i < 8) && (hack_n < total); i++)
2996                 {
2997                         POSITION mx, my;
2998
2999                         scatter(&my, &mx, hy, hx, 4, 0);
3000
3001                         /* Walls and Monsters block flow */
3002                         if (!cave_empty_bold2(my, mx)) continue;
3003
3004                         /* Attempt to place another monster */
3005                         if (place_monster_one(who, my, mx, r_idx, mode))
3006                         {
3007                                 /* Add it to the "hack" set */
3008                                 hack_y[hack_n] = my;
3009                                 hack_x[hack_n] = mx;
3010                                 hack_n++;
3011                         }
3012                 }
3013         }
3014
3015
3016         /* Success */
3017         return (TRUE);
3018 }
3019
3020 /*!
3021  * @var place_monster_idx
3022  * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
3023  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
3024  */
3025 static MONSTER_IDX place_monster_idx = 0;
3026
3027 /*!
3028  * @var place_monster_m_idx
3029  * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
3030  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
3031  */
3032 static MONSTER_IDX place_monster_m_idx = 0;
3033
3034 /*!
3035  * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
3036  * @param r_idx チェックするモンスター種族のID
3037  * @return 護衛にできるならばtrue
3038  */
3039 static bool place_monster_can_escort(MONRACE_IDX r_idx)
3040 {
3041         monster_race *r_ptr = &r_info[place_monster_idx];
3042         monster_type *m_ptr = &current_floor_ptr->m_list[place_monster_m_idx];
3043
3044         monster_race *z_ptr = &r_info[r_idx];
3045
3046         /* Hack - Escorts have to have the same dungeon flag */
3047         if (mon_hook_dungeon(place_monster_idx) != mon_hook_dungeon(r_idx)) return (FALSE);
3048
3049         /* Require similar "race" */
3050         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3051
3052         /* Skip more advanced monsters */
3053         if (z_ptr->level > r_ptr->level) return (FALSE);
3054
3055         /* Skip unique monsters */
3056         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3057
3058         /* Paranoia -- Skip identical monsters */
3059         if (place_monster_idx == r_idx) return (FALSE);
3060
3061         /* Skip different alignment */
3062         if (monster_has_hostile_align(m_ptr, 0, 0, z_ptr)) return FALSE;
3063
3064         if (r_ptr->flags7 & RF7_FRIENDLY)
3065         {
3066                 if (monster_has_hostile_align(NULL, 1, -1, z_ptr)) return FALSE;
3067         }
3068
3069         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3070                 return FALSE;
3071
3072         return (TRUE);
3073 }
3074
3075
3076 /*!
3077  * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
3078  * @param who 召喚主のモンスター情報ID
3079  * @param y 生成地点y座標
3080  * @param x 生成地点x座標
3081  * @param r_idx 生成するモンスターの種族ID
3082  * @param mode 生成オプション
3083  * @return 生成に成功したらtrue
3084  * @details
3085  * Note that certain monsters are now marked as requiring "friends".
3086  * These monsters, if successfully placed, and if the "grp" parameter
3087  * is TRUE, will be surrounded by a "group" of identical monsters.
3088  *
3089  * Note that certain monsters are now marked as requiring an "escort",
3090  * which is a collection of monsters with similar "race" but lower level.
3091  *
3092  * Some monsters induce a fake "group" flag on their escorts.
3093  *
3094  * Note the "bizarre" use of non-recursion to prevent annoying output
3095  * when running a code profiler.
3096  *
3097  * Note the use of the new "monster allocation table" code to restrict
3098  * the "get_mon_num()" function to "legal" escort types.
3099  */
3100 bool place_monster_aux(MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
3101 {
3102         int             i, j, n;
3103         monster_race    *r_ptr = &r_info[r_idx];
3104
3105         if (!(mode & PM_NO_KAGE) && one_in_(333))
3106                 mode |= PM_KAGE;
3107
3108         /* Place one monster, or fail */
3109         if (!place_monster_one(who, y, x, r_idx, mode)) return (FALSE);
3110
3111         /* Require the "group" flag */
3112         if (!(mode & PM_ALLOW_GROUP)) return (TRUE);
3113
3114         place_monster_m_idx = hack_m_idx_ii;
3115
3116         /* Reinforcement */
3117         for(i = 0; i < 6; i++)
3118         {
3119                 if(!r_ptr->reinforce_id[i]) break;
3120                 n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
3121                 for(j = 0; j < n; j++)
3122                 {
3123                         POSITION nx, ny, d = 7;
3124                         scatter(&ny, &nx, y, x, d, 0);
3125                         (void)place_monster_one(place_monster_m_idx, ny, nx, r_ptr->reinforce_id[i], mode);
3126                 }
3127         }
3128
3129         /* Friends for certain monsters */
3130         if (r_ptr->flags1 & (RF1_FRIENDS))
3131         {
3132                 /* Attempt to place a group */
3133                 (void)place_monster_group(who, y, x, r_idx, mode);
3134         }
3135
3136         /* Escorts for certain monsters */
3137         if (r_ptr->flags1 & (RF1_ESCORT))
3138         {
3139                 /* Set the escort index */
3140                 place_monster_idx = r_idx;
3141
3142                 /* Try to place several "escorts" */
3143                 for (i = 0; i < 32; i++)
3144                 {
3145                         POSITION nx, ny, d = 3;
3146                         MONRACE_IDX z; 
3147
3148                         /* Pick a location */
3149                         scatter(&ny, &nx, y, x, d, 0);
3150
3151                         /* Require empty grids */
3152                         if (!cave_empty_bold2(ny, nx)) continue;
3153                         get_mon_num_prep(place_monster_can_escort, get_monster_hook2(ny, nx));
3154
3155                         /* Pick a random race */
3156                         z = get_mon_num(r_ptr->level);
3157
3158                         /* Handle failure */
3159                         if (!z) break;
3160
3161                         /* Place a single escort */
3162                         (void)place_monster_one(place_monster_m_idx, ny, nx, z, mode);
3163
3164                         /* Place a "group" of escorts if needed */
3165                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3166                             (r_ptr->flags1 & RF1_ESCORTS))
3167                         {
3168                                 /* Place a group of monsters */
3169                                 (void)place_monster_group(place_monster_m_idx, ny, nx, z, mode);
3170                         }
3171                 }
3172         }
3173
3174         /* Success */
3175         return (TRUE);
3176 }
3177
3178 /*!
3179  * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
3180  * @param y 生成地点y座標
3181  * @param x 生成地点x座標
3182  * @param mode 生成オプション
3183  * @return 生成に成功したらtrue
3184  */
3185 bool place_monster(POSITION y, POSITION x, BIT_FLAGS mode)
3186 {
3187         MONRACE_IDX r_idx;
3188         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3189
3190         /* Pick a monster */
3191         r_idx = get_mon_num(current_floor_ptr->monster_level);
3192
3193         /* Handle failure */
3194         if (!r_idx) return (FALSE);
3195
3196         /* Attempt to place the monster */
3197         if (place_monster_aux(0, y, x, r_idx, mode)) return (TRUE);
3198
3199         return (FALSE);
3200 }
3201
3202 /*!
3203  * @brief 指定地点に1種類のモンスター種族による群れを生成する
3204  * @param y 生成地点y座標
3205  * @param x 生成地点x座標
3206  * @return 生成に成功したらtrue
3207  */
3208 bool alloc_horde(POSITION y, POSITION x)
3209 {
3210         monster_race *r_ptr = NULL;
3211         MONRACE_IDX r_idx = 0;
3212         MONSTER_IDX m_idx;
3213         int attempts = 1000;
3214         POSITION cy = y;
3215         POSITION cx = x;
3216         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3217
3218         while (--attempts)
3219         {
3220                 /* Pick a monster */
3221                 r_idx = get_mon_num(current_floor_ptr->monster_level);
3222
3223                 /* Handle failure */
3224                 if (!r_idx) return (FALSE);
3225
3226                 r_ptr = &r_info[r_idx];
3227
3228                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3229
3230                 if (r_idx == MON_HAGURE) continue;
3231                 break;
3232         }
3233         if (attempts < 1) return FALSE;
3234
3235         attempts = 1000;
3236
3237         while (--attempts)
3238         {
3239                 /* Attempt to place the monster */
3240                 if (place_monster_aux(0, y, x, r_idx, 0L)) break;
3241         }
3242
3243         if (attempts < 1) return FALSE;
3244
3245         m_idx = current_floor_ptr->grid_array[y][x].m_idx;
3246
3247         if (current_floor_ptr->m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[current_floor_ptr->m_list[m_idx].r_idx];
3248
3249         for (attempts = randint1(10) + 5; attempts; attempts--)
3250         {
3251                 scatter(&cy, &cx, y, x, 5, 0);
3252
3253                 (void)summon_specific(m_idx, cy, cx, current_floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP, r_ptr->d_char);
3254
3255                 y = cy;
3256                 x = cx;
3257         }
3258
3259         if (cheat_hear) msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
3260         return TRUE;
3261 }
3262
3263 /*!
3264  * @brief ダンジョンの主生成を試みる / Put the Guardian
3265  * @param def_val 現在の主の生成状態
3266  * @return 生成に成功したらtrue
3267  */
3268 bool alloc_guardian(bool def_val)
3269 {
3270         MONRACE_IDX guardian = d_info[p_ptr->dungeon_idx].final_guardian;
3271
3272         if (guardian && (d_info[p_ptr->dungeon_idx].maxdepth == current_floor_ptr->dun_level) && (r_info[guardian].cur_num < r_info[guardian].max_num))
3273         {
3274                 POSITION oy;
3275                 POSITION ox;
3276                 int try_count = 4000;
3277
3278                 /* Find a good position */
3279                 while (try_count)
3280                 {
3281                         /* Get a random spot */
3282                         oy = randint1(current_floor_ptr->height - 4) + 2;
3283                         ox = randint1(current_floor_ptr->width - 4) + 2;
3284
3285                         /* Is it a good spot ? */
3286                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(current_floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0))
3287                         {
3288                                 /* Place the guardian */
3289                                 if (place_monster_aux(0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) return TRUE;
3290                         }
3291
3292                         /* One less try count */
3293                         try_count--;
3294                 }
3295
3296                 return FALSE;
3297         }
3298
3299         return def_val;
3300 }
3301
3302
3303 /*!
3304  * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
3305  * @param dis プレイヤーから離れるべき最低距離
3306  * @param mode 生成オプション
3307  * @return 生成に成功したらtrue
3308  * @details
3309  * Place the monster at least "dis" distance from the player.
3310  * Use "slp" to choose the initial "sleep" status
3311  * Use "current_floor_ptr->monster_level" for the monster level
3312  */
3313 bool alloc_monster(POSITION dis, BIT_FLAGS mode)
3314 {
3315         POSITION y = 0, x = 0;
3316         int attempts_left = 10000;
3317
3318         /* Put the Guardian */
3319         if (alloc_guardian(FALSE)) return TRUE;
3320
3321         /* Find a legal, distant, unoccupied, space */
3322         while (attempts_left--)
3323         {
3324                 /* Pick a location */
3325                 y = randint0(current_floor_ptr->height);
3326                 x = randint0(current_floor_ptr->width);
3327
3328                 /* Require empty floor grid (was "naked") */
3329                 if (current_floor_ptr->dun_level)
3330                 {
3331                         if (!cave_empty_bold2(y, x)) continue;
3332                 }
3333                 else
3334                 {
3335                         if (!cave_empty_bold(y, x)) continue;
3336                 }
3337
3338                 /* Accept far away grids */
3339                 if (distance(y, x, p_ptr->y, p_ptr->x) > dis) break;
3340         }
3341
3342         if (!attempts_left)
3343         {
3344                 if (cheat_xtra || cheat_hear)
3345                 {
3346                         msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
3347                 }
3348
3349                 return (FALSE);
3350         }
3351
3352
3353         if (randint1(5000) <= current_floor_ptr->dun_level)
3354         {
3355                 if (alloc_horde(y, x))
3356                 {
3357                         return (TRUE);
3358                 }
3359         }
3360         else
3361         {
3362                 /* Attempt to place the monster, allow groups */
3363                 if (place_monster(y, x, (mode | PM_ALLOW_GROUP))) return (TRUE);
3364         }
3365
3366         return (FALSE);
3367 }
3368
3369
3370 /*!
3371  * @brief モンスターが召喚の基本条件に合っているかをチェックする / Hack -- help decide if a monster race is "okay" to summon
3372  * @param r_idx チェックするモンスター種族ID
3373  * @return 召喚対象にできるならばTRUE
3374  */
3375 static bool summon_specific_okay(MONRACE_IDX r_idx)
3376 {
3377         monster_race *r_ptr = &r_info[r_idx];
3378
3379         /* Hack - Only summon dungeon monsters */
3380         if (!mon_hook_dungeon(r_idx)) return (FALSE);
3381
3382         /* Hack -- identify the summoning monster */
3383         if (summon_specific_who > 0)
3384         {
3385                 monster_type *m_ptr = &current_floor_ptr->m_list[summon_specific_who];
3386
3387                 /* Do not summon enemies */
3388
3389                 /* Friendly vs. opposite aligned normal or pet */
3390                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
3391         }
3392         /* Use the player's alignment */
3393         else if (summon_specific_who < 0)
3394         {
3395                 /* Do not summon enemies of the pets */
3396                 if (monster_has_hostile_align(NULL, 10, -10, r_ptr))
3397                 {
3398                         if (!one_in_(ABS(p_ptr->align) / 2 + 1)) return FALSE;
3399                 }
3400         }
3401
3402         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))) return FALSE;
3403
3404         /* Hack -- no specific type specified */
3405         if (!summon_specific_type) return (TRUE);
3406
3407         if ((summon_specific_who < 0) &&
3408             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
3409             monster_has_hostile_align(NULL, 10, -10, r_ptr))
3410                 return FALSE;
3411
3412         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[p_ptr->dungeon_idx].flags1 & DF1_CHAMELEON)) return TRUE;
3413
3414         return (summon_specific_aux(r_idx));
3415 }
3416
3417
3418 /*!
3419  * @brief モンスターを召喚により配置する / Place a monster (of the specified "type") near the given location. Return TRUE if a monster was actually summoned.
3420  * @param who 召喚主のモンスター情報ID
3421  * @param y1 目標地点y座標
3422  * @param x1 目標地点x座標
3423  * @param lev 相当生成階
3424  * @param type 召喚種別
3425  * @param mode 生成オプション 
3426  * @return 召喚できたらtrueを返す
3427  * @details
3428  *
3429  * We will attempt to place the monster up to 10 times before giving up.
3430  *
3431  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
3432  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
3433  * Note: None of the other summon codes will ever summon Unique's.
3434  *
3435  * This function has been changed.  We now take the "monster level"
3436  * of the summoning monster as a parameter, and use that, along with
3437  * the current dungeon level, to help determine the level of the
3438  * desired monster.  Note that this is an upper bound, and also
3439  * tends to "prefer" monsters of that level.  Currently, we use
3440  * the average of the dungeon and monster levels, and then add
3441  * five to allow slight increases in monster power.
3442  *
3443  * Note that we use the new "monster allocation table" creation code
3444  * to restrict the "get_mon_num()" function to the set of "legal"
3445  * monsters, making this function much faster and more reliable.
3446  *
3447  * Note that this function may not succeed, though this is very rare.
3448  */
3449 bool summon_specific(MONSTER_IDX who, POSITION y1, POSITION x1, DEPTH lev, int type, BIT_FLAGS mode, SYMBOL_CODE symbol)
3450 {
3451         POSITION x, y;
3452         MONRACE_IDX r_idx;
3453
3454         if (p_ptr->inside_arena) return (FALSE);
3455
3456         if (!mon_scatter(0, &y, &x, y1, x1, 2)) return FALSE;
3457
3458         /* Save the summoner */
3459         summon_specific_who = who;
3460
3461         /* Save the "summon" type */
3462         summon_specific_type = type;
3463
3464         summon_kin_type = symbol;
3465
3466         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
3467         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
3468
3469         /* Pick a monster, using the level calculation */
3470         r_idx = get_mon_num((current_floor_ptr->dun_level + lev) / 2 + 5);
3471
3472         /* Handle failure */
3473         if (!r_idx)
3474         {
3475                 summon_specific_type = 0;
3476                 return (FALSE);
3477         }
3478
3479         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
3480
3481         /* Attempt to place the monster (awake, allow groups) */
3482         if (!place_monster_aux(who, y, x, r_idx, mode))
3483         {
3484                 summon_specific_type = 0;
3485                 return (FALSE);
3486         }
3487
3488         summon_specific_type = 0;
3489         /* Success */
3490         sound(SOUND_SUMMON);
3491         return (TRUE);
3492 }
3493
3494
3495 /*!
3496  * @brief 特定モンスター種族を召喚により生成する / A "dangerous" function, creates a pet of the specified type
3497  * @param who 召喚主のモンスター情報ID
3498  * @param oy 目標地点y座標
3499  * @param ox 目標地点x座標
3500  * @param r_idx 生成するモンスター種族ID
3501  * @param mode 生成オプション 
3502  * @return 召喚できたらtrueを返す
3503  */
3504 bool summon_named_creature(MONSTER_IDX who, POSITION oy, POSITION ox, MONRACE_IDX r_idx, BIT_FLAGS mode)
3505 {
3506         POSITION x, y;
3507         /* if (!r_idx) return; */
3508
3509         /* Prevent illegal monsters */
3510         if (r_idx >= max_r_idx) return FALSE;
3511
3512         if (p_ptr->inside_arena) return FALSE;
3513
3514         if (!mon_scatter(r_idx, &y, &x, oy, ox, 2)) return FALSE;
3515
3516         /* Place it (allow groups) */
3517         return place_monster_aux(who, y, x, r_idx, (mode | PM_NO_KAGE));
3518 }
3519
3520
3521 /*!
3522  * @brief モンスターを増殖生成する / Let the given monster attempt to reproduce.
3523  * @param m_idx 増殖するモンスター情報ID
3524  * @param clone クローン・モンスター処理ならばtrue
3525  * @param mode 生成オプション 
3526  * @return 生成できたらtrueを返す
3527  * @details
3528  * Note that "reproduction" REQUIRES empty space.
3529  */
3530 bool multiply_monster(MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
3531 {
3532         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
3533         POSITION y, x;
3534
3535         if (!mon_scatter(m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
3536                 return FALSE;
3537
3538         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
3539
3540         /* Create a new monster (awake, no groups) */
3541         if (!place_monster_aux(m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
3542                 return FALSE;
3543
3544         /* Hack -- Transfer "clone" flag */
3545         if (clone || (m_ptr->smart & SM_CLONED))
3546         {
3547                 current_floor_ptr->m_list[hack_m_idx_ii].smart |= SM_CLONED;
3548                 current_floor_ptr->m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
3549         }
3550
3551         return TRUE;
3552 }
3553
3554
3555
3556 /*!
3557  * @brief ダメージを受けたモンスターの様子を記述する / Dump a message describing a monster's reaction to damage
3558  * @param m_idx モンスター情報ID
3559  * @param dam 与えたダメージ
3560  * @return なし
3561  * @details
3562  * Technically should attempt to treat "Beholder"'s as jelly's
3563  */
3564 void message_pain(MONSTER_IDX m_idx, HIT_POINT dam)
3565 {
3566         HIT_POINT oldhp, newhp;
3567         HIT_POINT tmp;
3568         PERCENTAGE percentage;
3569
3570         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
3571         monster_race *r_ptr = &r_info[m_ptr->r_idx];
3572
3573         GAME_TEXT m_name[MAX_NLEN];
3574
3575         monster_desc(m_name, m_ptr, 0);
3576
3577         if(dam == 0) // Notice non-damage
3578         {
3579                 msg_format(_("%^sはダメージを受けていない。", "%^s is unharmed."), m_name);
3580                 return;
3581         }
3582
3583         /* Note -- subtle fix -CFT */
3584         newhp = m_ptr->hp;
3585         oldhp = newhp + dam;
3586         tmp = (newhp * 100L) / oldhp;
3587         percentage = tmp;
3588
3589         if(my_strchr(",ejmvwQ", r_ptr->d_char)) // Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs
3590         {
3591 #ifdef JP
3592                 if(percentage > 95) msg_format("%^sはほとんど気にとめていない。", m_name);
3593                 else if(percentage > 75) msg_format("%^sはしり込みした。", m_name);
3594                 else if(percentage > 50) msg_format("%^sは縮こまった。", m_name);
3595                 else if(percentage > 35) msg_format("%^sは痛みに震えた。", m_name);
3596                 else if(percentage > 20) msg_format("%^sは身もだえした。", m_name);
3597                 else if(percentage > 10) msg_format("%^sは苦痛で身もだえした。", m_name);
3598                 else msg_format("%^sはぐにゃぐにゃと痙攣した。", m_name);
3599 #else
3600                 if(percentage > 95) msg_format("%^s barely notices.", m_name);
3601                 else if(percentage > 75) msg_format("%^s flinches.", m_name);
3602                 else if(percentage > 50) msg_format("%^s squelches.", m_name);
3603                 else if(percentage > 35) msg_format("%^s quivers in pain.", m_name);
3604                 else if(percentage > 20) msg_format("%^s writhes about.", m_name);
3605                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
3606                 else msg_format("%^s jerks limply.", m_name);
3607 #endif
3608         }
3609
3610         else if(my_strchr("l", r_ptr->d_char)) // Fish
3611         {
3612 #ifdef JP
3613                 if(percentage > 95) msg_format("%^sはほとんど気にとめていない。", m_name);
3614                 else if(percentage > 75) msg_format("%^sはしり込みした。", m_name);
3615                 else if(percentage > 50) msg_format("%^sは躊躇した。", m_name);
3616                 else if(percentage > 35) msg_format("%^sは痛みに震えた。", m_name);
3617                 else if(percentage > 20) msg_format("%^sは身もだえした。", m_name);
3618                 else if(percentage > 10) msg_format("%^sは苦痛で身もだえした。", m_name);
3619                 else msg_format("%^sはぐにゃぐにゃと痙攣した。", m_name);
3620 #else
3621                 if(percentage > 95) msg_format("%^s barely notices.", m_name);
3622                 else if(percentage > 75) msg_format("%^s flinches.", m_name);
3623                 else if(percentage > 50) msg_format("%^s hesitates.", m_name);
3624                 else if(percentage > 35) msg_format("%^s quivers in pain.", m_name);
3625                 else if(percentage > 20) msg_format("%^s writhes about.", m_name);
3626                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
3627                 else msg_format("%^s jerks limply.", m_name);
3628 #endif          
3629         }
3630
3631         else if(my_strchr("g#+<>", r_ptr->d_char)) // Golems, Walls, Doors, Stairs
3632         {       
3633 #ifdef JP
3634                 if(percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3635                 else if(percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3636                 else if(percentage > 50) msg_format("%^sは雷鳴のように吠えた。", m_name);
3637                 else if(percentage > 35) msg_format("%^sは苦しげに吠えた。", m_name);
3638                 else if(percentage > 20) msg_format("%^sはうめいた。", m_name);
3639                 else if(percentage > 10) msg_format("%^sは躊躇した。", m_name);
3640                 else msg_format("%^sはくしゃくしゃになった。", m_name);
3641 #else
3642                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
3643                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3644                 else if(percentage > 50) msg_format("%^s roars thunderously.", m_name);
3645                 else if(percentage > 35) msg_format("%^s rumbles.", m_name);
3646                 else if(percentage > 20) msg_format("%^s grunts.", m_name);
3647                 else if(percentage > 10) msg_format("%^s hesitates.", m_name);
3648                 else msg_format("%^s crumples.", m_name);
3649 #endif
3650         }
3651
3652         else if(my_strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char)) // Snakes, Hydrae, Reptiles, Mimics
3653         {
3654 #ifdef JP
3655                 if(percentage > 95) msg_format("%^sはほとんど気にとめていない。", m_name);
3656                 else if(percentage > 75) msg_format("%^sはシーッと鳴いた。", m_name);
3657                 else if(percentage > 50) msg_format("%^sは怒って頭を上げた。", m_name);
3658                 else if(percentage > 35) msg_format("%^sは猛然と威嚇した。", m_name);
3659                 else if(percentage > 20) msg_format("%^sは身もだえした。", m_name);
3660                 else if(percentage > 10) msg_format("%^sは苦痛で身もだえした。", m_name);
3661                 else msg_format("%^sはぐにゃぐにゃと痙攣した。", m_name);
3662 #else
3663                 if(percentage > 95) msg_format("%^s barely notices.", m_name);
3664                 else if(percentage > 75) msg_format("%^s hisses.", m_name);
3665                 else if(percentage > 50) msg_format("%^s rears up in anger.", m_name);
3666                 else if(percentage > 35) msg_format("%^s hisses furiously.", m_name);
3667                 else if(percentage > 20) msg_format("%^s writhes about.", m_name);
3668                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
3669                 else msg_format("%^s jerks limply.", m_name);
3670 #endif
3671         }
3672
3673         else if(my_strchr("f", r_ptr->d_char))
3674         {
3675 #ifdef JP
3676                 if(percentage > 95) msg_format("%sは攻撃に肩をすくめた。", m_name);
3677                 else if(percentage > 75) msg_format("%^sは吠えた。", m_name);
3678                 else if(percentage > 50) msg_format("%^sは怒って吠えた。", m_name);
3679                 else if(percentage > 35) msg_format("%^sは痛みでシーッと鳴いた。", m_name);
3680                 else if(percentage > 20) msg_format("%^sは痛みで弱々しく鳴いた。", m_name);
3681                 else if(percentage > 10) msg_format("%^sは苦痛にうめいた。", m_name);
3682                 else msg_format("%sは哀れな鳴き声を出した。", m_name);
3683 #else
3684                 if(percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
3685                 else if(percentage > 75) msg_format("%^s roars.", m_name);
3686                 else if(percentage > 50) msg_format("%^s growls angrily.", m_name);
3687                 else if(percentage > 35) msg_format("%^s hisses with pain.", m_name);
3688                 else if(percentage > 20) msg_format("%^s mewls in pain.", m_name);
3689                 else if(percentage > 10) msg_format("%^s hisses in agony.", m_name);
3690                 else msg_format("%^s mewls pitifully.", m_name);
3691 #endif
3692         }
3693
3694         else if(my_strchr("acFIKS", r_ptr->d_char)) // Ants, Centipedes, Flies, Insects, Beetles, Spiders
3695         {
3696 #ifdef JP
3697                 if(percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3698                 else if(percentage > 75) msg_format("%^sはキーキー鳴いた。", m_name);
3699                 else if(percentage > 50) msg_format("%^sはヨロヨロ逃げ回った。", m_name);
3700                 else if(percentage > 35) msg_format("%^sはうるさく鳴いた。", m_name);
3701                 else if(percentage > 20) msg_format("%^sは痛みに痙攣した。", m_name);
3702                 else if(percentage > 10) msg_format("%^sは苦痛で痙攣した。", m_name);
3703                 else msg_format("%^sはピクピクひきつった。", m_name);
3704 #else
3705                 if(percentage > 95)     msg_format("%^s ignores the attack.", m_name);
3706                 else if(percentage > 75) msg_format("%^s chitters.", m_name);
3707                 else if(percentage > 50) msg_format("%^s scuttles about.", m_name);
3708                 else if(percentage > 35) msg_format("%^s twitters.", m_name);
3709                 else if(percentage > 20) msg_format("%^s jerks in pain.", m_name);
3710                 else if(percentage > 10) msg_format("%^s jerks in agony.", m_name);
3711                 else msg_format("%^s twitches.", m_name);
3712 #endif
3713         }
3714
3715         else if(my_strchr("B", r_ptr->d_char)) // Birds
3716         {               
3717 #ifdef JP
3718                 if(percentage > 95) msg_format("%^sはさえずった。", m_name);
3719                 else if(percentage > 75) msg_format("%^sはピーピー鳴いた。", m_name);
3720                 else if(percentage > 50) msg_format("%^sはギャーギャー鳴いた。", m_name);
3721                 else if(percentage > 35) msg_format("%^sはギャーギャー鳴きわめいた。", m_name);
3722                 else if(percentage > 20) msg_format("%^sは苦しんだ。", m_name);
3723                 else if(percentage > 10) msg_format("%^sはのたうち回った。", m_name);
3724                 else msg_format("%^sはキーキーと鳴き叫んだ。", m_name);
3725 #else
3726                 if(percentage > 95)     msg_format("%^s chirps.", m_name);
3727                 else if(percentage > 75) msg_format("%^s twitters.", m_name);
3728                 else if(percentage > 50) msg_format("%^s squawks.", m_name);
3729                 else if(percentage > 35) msg_format("%^s chatters.", m_name);
3730                 else if(percentage > 20) msg_format("%^s jeers.", m_name);
3731                 else if(percentage > 10) msg_format("%^s flutters about.", m_name);
3732                 else msg_format("%^s squeaks.", m_name);
3733 #endif
3734         }
3735
3736         else if(my_strchr("duDLUW", r_ptr->d_char)) // Dragons, Demons, High Undead
3737         {       
3738 #ifdef JP
3739                 if(percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3740                 else if(percentage > 75) msg_format("%^sはしり込みした。", m_name);
3741                 else if(percentage > 50) msg_format("%^sは痛みでシーッと鳴いた。", m_name);
3742                 else if(percentage > 35) msg_format("%^sは痛みでうなった。", m_name);
3743                 else if(percentage > 20) msg_format("%^sは痛みに吠えた。", m_name);
3744                 else if(percentage > 10) msg_format("%^sは苦しげに叫んだ。", m_name);
3745                 else msg_format("%^sは弱々しくうなった。", m_name);
3746 #else
3747                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
3748                 else if(percentage > 75) msg_format("%^s flinches.", m_name);
3749                 else if(percentage > 50) msg_format("%^s hisses in pain.", m_name);
3750                 else if(percentage > 35) msg_format("%^s snarls with pain.", m_name);
3751                 else if(percentage > 20) msg_format("%^s roars with pain.", m_name);
3752                 else if(percentage > 10) msg_format("%^s gasps.", m_name);
3753                 else msg_format("%^s snarls feebly.", m_name);
3754 #endif
3755         }
3756
3757         else if(my_strchr("s", r_ptr->d_char)) // Skeletons
3758         {
3759 #ifdef JP
3760                 if(percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3761                 else if(percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3762                 else if(percentage > 50) msg_format("%^sはカタカタと笑った。", m_name);
3763                 else if(percentage > 35) msg_format("%^sはよろめいた。", m_name);
3764                 else if(percentage > 20) msg_format("%^sはカタカタ言った。", m_name);
3765                 else if(percentage > 10) msg_format("%^sはよろめいた。", m_name);
3766                 else msg_format("%^sはガタガタ言った。", m_name);
3767 #else
3768                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
3769                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3770                 else if(percentage > 50) msg_format("%^s rattles.", m_name);
3771                 else if(percentage > 35) msg_format("%^s stumbles.", m_name);
3772                 else if(percentage > 20) msg_format("%^s rattles.", m_name);
3773                 else if(percentage > 10) msg_format("%^s staggers.", m_name);
3774                 else msg_format("%^s clatters.", m_name);
3775 #endif
3776         }
3777
3778         else if(my_strchr("z", r_ptr->d_char)) // Zombies
3779         {               
3780 #ifdef JP
3781                 if(percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3782                 else if(percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3783                 else if(percentage > 50) msg_format("%^sはうめいた。", m_name);
3784                 else if(percentage > 35) msg_format("%sは苦しげにうめいた。", m_name);
3785                 else if(percentage > 20) msg_format("%^sは躊躇した。", m_name);
3786                 else if(percentage > 10) msg_format("%^sはうなった。", m_name);
3787                 else msg_format("%^sはよろめいた。", m_name);
3788 #else
3789                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
3790                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3791                 else if(percentage > 50) msg_format("%^s groans.", m_name);
3792                 else if(percentage > 35) msg_format("%^s moans.", m_name);
3793                 else if(percentage > 20) msg_format("%^s hesitates.", m_name);
3794                 else if(percentage > 10) msg_format("%^s grunts.", m_name);
3795                 else msg_format("%^s staggers.", m_name);
3796 #endif
3797         }
3798
3799         else if(my_strchr("G", r_ptr->d_char)) // Ghosts
3800         {
3801 #ifdef JP
3802                 if(percentage > 95) msg_format("%sは攻撃を気にとめていない。", m_name);
3803                 else if(percentage > 75) msg_format("%sは攻撃に肩をすくめた。", m_name);
3804                 else if(percentage > 50) msg_format("%sはうめいた。", m_name);
3805                 else if(percentage > 35) msg_format("%^sは泣きわめいた。", m_name);
3806                 else if(percentage > 20) msg_format("%^sは吠えた。", m_name);
3807                 else if(percentage > 10) msg_format("%sは弱々しくうめいた。", m_name);
3808                 else msg_format("%^sはかすかにうめいた。", m_name);
3809 #else
3810                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
3811                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
3812                 else if(percentage > 50)  msg_format("%^s moans.", m_name);
3813                 else if(percentage > 35) msg_format("%^s wails.", m_name);
3814                 else if(percentage > 20) msg_format("%^s howls.", m_name);
3815                 else if(percentage > 10) msg_format("%^s moans softly.", m_name);
3816                 else msg_format("%^s sighs.", m_name);
3817 #endif
3818         }
3819
3820         else if(my_strchr("CZ", r_ptr->d_char)) // Dogs and Hounds
3821         {
3822 #ifdef JP
3823                 if(percentage > 95) msg_format("%^sは攻撃に肩をすくめた。", m_name);
3824                 else if(percentage > 75) msg_format("%^sは痛みでうなった。", m_name);
3825                 else if(percentage > 50) msg_format("%^sは痛みでキャンキャン吠えた。", m_name);
3826                 else if(percentage > 35) msg_format("%^sは痛みで鳴きわめいた。", m_name);
3827                 else if(percentage > 20) msg_format("%^sは苦痛のあまり鳴きわめいた。", m_name);
3828                 else if(percentage > 10) msg_format("%^sは苦痛でもだえ苦しんだ。", m_name);
3829                 else msg_format("%^sは弱々しく吠えた。", m_name);
3830 #else
3831                 if(percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
3832                 else if(percentage > 75) msg_format("%^s snarls with pain.", m_name);
3833                 else if(percentage > 50) msg_format("%^s yelps in pain.", m_name);
3834                 else if(percentage > 35) msg_format("%^s howls in pain.", m_name);
3835                 else if(percentage > 20) msg_format("%^s howls in agony.", m_name);
3836                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
3837                 else msg_format("%^s yelps feebly.", m_name);
3838 #endif
3839         }
3840
3841         else if(my_strchr("Xbilqrt", r_ptr->d_char)) // One type of creatures (ignore,squeal,shriek)
3842         {
3843 #ifdef JP
3844                 if(percentage > 95) msg_format("%^sは攻撃を気にとめていない。", m_name);
3845                 else if(percentage > 75) msg_format("%^sは痛みでうなった。", m_name);
3846                 else if(percentage > 50) msg_format("%^sは痛みで叫んだ。", m_name);
3847                 else if(percentage > 35) msg_format("%^sは痛みで絶叫した。", m_name);
3848                 else if(percentage > 20) msg_format("%^sは苦痛のあまり絶叫した。", m_name);
3849                 else if(percentage > 10) msg_format("%^sは苦痛でもだえ苦しんだ。", m_name);
3850                 else msg_format("%^sは弱々しく叫んだ。", m_name);
3851 #else
3852                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
3853                 else if(percentage > 75) msg_format("%^s grunts with pain.", m_name);
3854                 else if(percentage > 50) msg_format("%^s squeals in pain.", m_name);
3855                 else if(percentage > 35) msg_format("%^s shrieks in pain.", m_name);
3856                 else if(percentage > 20) msg_format("%^s shrieks in agony.", m_name);
3857                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
3858                 else msg_format("%^s cries out feebly.", m_name);
3859 #endif
3860         }
3861
3862         else // Another type of creatures (shrug,cry,scream)
3863         {
3864 #ifdef JP
3865                 if(percentage > 95) msg_format("%^sは攻撃に肩をすくめた。", m_name);
3866                 else if(percentage > 75) msg_format("%^sは痛みでうなった。", m_name);
3867                 else if(percentage > 50) msg_format("%^sは痛みで叫んだ。", m_name);
3868                 else if(percentage > 35) msg_format("%^sは痛みで絶叫した。", m_name);
3869                 else if(percentage > 20) msg_format("%^sは苦痛のあまり絶叫した。", m_name);
3870                 else if(percentage > 10) msg_format("%^sは苦痛でもだえ苦しんだ。", m_name);
3871                 else msg_format("%^sは弱々しく叫んだ。", m_name);
3872 #else
3873                 if(percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
3874                 else if(percentage > 75) msg_format("%^s grunts with pain.", m_name);
3875                 else if(percentage > 50) msg_format("%^s cries out in pain.", m_name);
3876                 else if(percentage > 35) msg_format("%^s screams in pain.", m_name);
3877                 else if(percentage > 20) msg_format("%^s screams in agony.", m_name);
3878                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
3879                 else msg_format("%^s cries out feebly.", m_name);
3880 #endif
3881         }
3882 }
3883
3884
3885
3886 /*!
3887  * @brief SMART(適格に攻撃を行う)モンスターの学習状況を更新する / Learn about an "observed" resistance.
3888  * @param m_idx 更新を行う「モンスター情報ID
3889  * @param what 学習対象ID
3890  * @return なし
3891  */
3892 void update_smart_learn(MONSTER_IDX m_idx, int what)
3893 {
3894         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
3895         monster_race *r_ptr = &r_info[m_ptr->r_idx];
3896
3897         /* Not allowed to learn */
3898         if (!smart_learn) return;
3899
3900         /* Too stupid to learn anything */
3901         if (r_ptr->flags2 & (RF2_STUPID)) return;
3902
3903         /* Not intelligent, only learn sometimes */
3904         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
3905
3906         /* Analyze the knowledge */
3907         switch (what)
3908         {
3909         case DRS_ACID:
3910                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
3911                 if (IS_OPPOSE_ACID()) m_ptr->smart |= (SM_OPP_ACID);
3912                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
3913                 break;
3914
3915         case DRS_ELEC:
3916                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
3917                 if (IS_OPPOSE_ELEC()) m_ptr->smart |= (SM_OPP_ELEC);
3918                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
3919                 break;
3920
3921         case DRS_FIRE:
3922                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
3923                 if (IS_OPPOSE_FIRE()) m_ptr->smart |= (SM_OPP_FIRE);
3924                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
3925                 break;
3926
3927         case DRS_COLD:
3928                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
3929                 if (IS_OPPOSE_COLD()) m_ptr->smart |= (SM_OPP_COLD);
3930                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
3931                 break;
3932
3933         case DRS_POIS:
3934                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
3935                 if (IS_OPPOSE_POIS()) m_ptr->smart |= (SM_OPP_POIS);
3936                 break;
3937
3938
3939         case DRS_NETH:
3940                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
3941                 break;
3942
3943         case DRS_LITE:
3944                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
3945                 break;
3946
3947         case DRS_DARK:
3948                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
3949                 break;
3950
3951         case DRS_FEAR:
3952                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
3953                 break;
3954
3955         case DRS_CONF:
3956                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
3957                 break;
3958
3959         case DRS_CHAOS:
3960                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
3961                 break;
3962
3963         case DRS_DISEN:
3964                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
3965                 break;
3966
3967         case DRS_BLIND:
3968                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
3969                 break;
3970
3971         case DRS_NEXUS:
3972                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
3973                 break;
3974
3975         case DRS_SOUND:
3976                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
3977                 break;
3978
3979         case DRS_SHARD:
3980                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
3981                 break;
3982
3983         case DRS_FREE:
3984                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
3985                 break;
3986
3987         case DRS_MANA:
3988                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
3989                 break;
3990
3991         case DRS_REFLECT:
3992                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
3993                 break;
3994         }
3995 }
3996
3997
3998 /*!
3999  * @brief モンスターが盗みや拾いで確保していたアイテムを全てドロップさせる / Drop all items carried by a monster
4000  * @param m_ptr モンスター参照ポインタ
4001  * @return なし
4002  */
4003 void monster_drop_carried_objects(monster_type *m_ptr)
4004 {
4005         OBJECT_IDX this_o_idx, next_o_idx = 0;
4006         object_type forge;
4007         object_type *o_ptr;
4008         object_type *q_ptr;
4009
4010
4011         /* Drop objects being carried */
4012         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4013         {
4014                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
4015                 next_o_idx = o_ptr->next_o_idx;
4016                 q_ptr = &forge;
4017
4018                 /* Copy the object */
4019                 object_copy(q_ptr, o_ptr);
4020
4021                 /* Forget monster */
4022                 q_ptr->held_m_idx = 0;
4023
4024                 delete_object_idx(this_o_idx);
4025
4026                 /* Drop it */
4027                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
4028         }
4029
4030         /* Forget objects */
4031         m_ptr->hold_o_idx = 0;
4032 }