OSDN Git Service

efb89453c4293c4538bc1d9c0aab8b6c085e3331
[hengband/hengband.git] / src / monster2.c
1 /* File: monster2.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: misc code for monsters */
12
13 #include "angband.h"
14
15 #define HORDE_NOGOOD 0x01
16 #define HORDE_NOEVIL 0x02
17
18 bool is_kage = FALSE;
19
20 cptr horror_desc[MAX_SAN_HORROR] =
21 {
22 #ifdef JP
23         "´÷¤Þ¤ï¤·¤¤",
24         "ÄìÃΤì¤Ì",
25         "¤¾¤Ã¤È¤¹¤ë",
26         "ÇËÌÇŪ¤Ê",
27         "ËÁÆÂŪ¤Ê",
28
29         "¤¤¤ä¤Ê",
30         "¶²¤í¤·¤¤",
31         "ÉÔ·é¤Ê",
32         "ÍƼϤΤʤ¤",
33         "¤ª¤¾¤Þ¤·¤¤",
34
35         "ÃϹö¤Î",
36         "¿È¤ÎÌÓ¤â¤è¤À¤Ä",
37         "ÃϹö¤Î",
38         "´÷¤Þ¤ï¤·¤¤",
39         "°­Ì´¤Î¤è¤¦¤Ê",
40
41         "·ù°­¤ò´¶¤¸¤ë",
42         "ȳÅö¤¿¤ê¤Ê",
43         "¶²¤¤",
44         "ÉÔ¾ô¤Ê",
45         "¸À¤¦¤â¤ª¤¾¤Þ¤·¤¤",
46 #else
47         "abominable",
48         "abysmal",
49         "appalling",
50         "baleful",
51         "blasphemous",
52
53         "disgusting",
54         "dreadful",
55         "filthy",
56         "grisly",
57         "hideous",
58
59         "hellish",
60         "horrible",
61         "infernal",
62         "loathsome",
63         "nightmarish",
64
65         "repulsive",
66         "sacrilegious",
67         "terrible",
68         "unclean",
69         "unspeakable",
70 #endif
71
72 };
73
74 cptr funny_desc[MAX_SAN_FUNNY] =
75 {
76 #ifdef JP
77         "´ÖÈ´¤±¤Ê",
78         "³ê·Î¤Ê",
79         "¤Ð¤«¤é¤·¤¤",
80         "̵̣´¥Áç¤Ê",
81         "Çϼ¯¤²¤¿",
82
83         "¾Ð¤¨¤ë",
84         "¤Ð¤«¤Ð¤«¤·¤¤",
85         "¤Ö¤Ã¤È¤ó¤À",
86         "¤¤¤«¤·¤¿",
87         "¥Ý¥¹¥È¥â¥À¥ó¤Ê",
88
89         "¥Õ¥¡¥ó¥¿¥¹¥Æ¥£¥Ã¥¯¤Ê",
90         "¥À¥À¥¤¥º¥àŪ¤Ê",
91         "¥­¥å¥Ó¥º¥àŪ¤Ê",
92         "±§ÃèŪ¤Ê",
93         "Âî±Û¤·¤¿",
94
95         "Íý²òÉÔǽ¤Ê",
96         "¤â¤Î¤¹¤´¤¤",
97         "¶Ã¤¯¤Ù¤­",
98         "¿®¤¸¤é¤ì¤Ê¤¤",
99         "¥«¥ª¥Æ¥£¥Ã¥¯¤Ê",
100
101         "ÌîÀ­Åª¤Ê",
102         "Èó¾ï¼±¤Ê",
103 #else
104         "silly",
105         "hilarious",
106         "absurd",
107         "insipid",
108         "ridiculous",
109
110         "laughable",
111         "ludicrous",
112         "far-out",
113         "groovy",
114         "postmodern",
115
116         "fantastic",
117         "dadaistic",
118         "cubistic",
119         "cosmic",
120         "awesome",
121
122         "incomprehensible",
123         "fabulous",
124         "amazing",
125         "incredible",
126         "chaotic",
127
128         "wild",
129         "preposterous",
130 #endif
131
132 };
133
134 cptr funny_comments[MAX_SAN_COMMENT] =
135 {
136 #ifdef JP
137   /* nuke me */
138         "ºÇ¹â¤À¤¼¡ª",
139         "¤¦¤Ò¤ç¡¼¡ª",
140         "¤¤¤«¤¹¤¼¡ª",
141         "¤¹¤ó¤Ð¤é¤·¤¤¡ª",
142         "¤Ö¤Ã¤È¤Ó¡¼¡ª"
143 #else
144         "Wow, cosmic, man!",
145         "Rad!",
146         "Groovy!",
147         "Cool!",
148         "Far out!"
149 #endif
150
151 };
152
153
154 /*
155  * Set the target of counter attack
156  */
157 void set_target(monster_type *m_ptr, int y, int x)
158 {
159         m_ptr->target_y = y;
160         m_ptr->target_x = x;
161 }
162
163
164 /*
165  * Reset the target of counter attack
166  */
167 void reset_target(monster_type *m_ptr)
168 {
169         set_target(m_ptr, 0, 0);
170 }
171
172
173 /*
174  *  Extract monster race pointer of a monster's true form
175  */
176 monster_race *real_r_ptr(monster_type *m_ptr)
177 {
178         monster_race *r_ptr = &r_info[m_ptr->r_idx];
179
180         /* Extract real race */
181         if (m_ptr->mflag2 & MFLAG2_CHAMELEON)
182         {
183                 if (r_ptr->flags1 & RF1_UNIQUE)
184                         return &r_info[MON_CHAMELEON_K];
185                 else
186                         return &r_info[MON_CHAMELEON];
187         }
188         else
189         {
190                 return r_ptr;
191         }
192 }
193
194
195 /*
196  * Delete a monster by index.
197  *
198  * When a monster is deleted, all of its objects are deleted.
199  */
200 void delete_monster_idx(int i)
201 {
202         int x, y;
203
204         monster_type *m_ptr = &m_list[i];
205
206         monster_race *r_ptr = &r_info[m_ptr->r_idx];
207
208         s16b this_o_idx, next_o_idx = 0;
209
210
211         /* Get location */
212         y = m_ptr->fy;
213         x = m_ptr->fx;
214
215
216         /* Hack -- Reduce the racial counter */
217         real_r_ptr(m_ptr)->cur_num--;
218
219         /* Hack -- count the number of "reproducers" */
220         if (r_ptr->flags2 & (RF2_MULTIPLY)) num_repro--;
221
222
223         /* Hack -- remove target monster */
224         if (i == target_who) target_who = 0;
225
226         /* Hack -- remove tracked monster */
227         if (i == p_ptr->health_who) health_track(0);
228
229         if (pet_t_m_idx == i ) pet_t_m_idx = 0;
230         if (riding_t_m_idx == i) riding_t_m_idx = 0;
231         if (p_ptr->riding == i) p_ptr->riding = 0;
232
233         /* Monster is gone */
234         cave[y][x].m_idx = 0;
235
236
237         /* Delete objects */
238         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
239         {
240                 object_type *o_ptr;
241
242                 /* Acquire object */
243                 o_ptr = &o_list[this_o_idx];
244
245                 /* Acquire next object */
246                 next_o_idx = o_ptr->next_o_idx;
247
248                 /* Hack -- efficiency */
249                 o_ptr->held_m_idx = 0;
250
251                 /* Delete the object */
252                 delete_object_idx(this_o_idx);
253         }
254
255
256         /* Wipe the Monster */
257         (void)WIPE(m_ptr, monster_type);
258
259         /* Count monsters */
260         m_cnt--;
261
262         /* Visual update */
263         lite_spot(y, x);
264
265         /* Update some things */
266         p_ptr->update |= (PU_MON_LITE);
267 }
268
269
270 /*
271  * Delete the monster, if any, at a given location
272  */
273 void delete_monster(int y, int x)
274 {
275         cave_type *c_ptr;
276
277         /* Paranoia */
278         if (!in_bounds(y, x)) return;
279
280         /* Check the grid */
281         c_ptr = &cave[y][x];
282
283         /* Delete the monster (if any) */
284         if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
285 }
286
287
288 /*
289  * Move an object from index i1 to index i2 in the object list
290  */
291 static void compact_monsters_aux(int i1, int i2)
292 {
293         int y, x;
294
295         cave_type *c_ptr;
296
297         monster_type *m_ptr;
298
299         s16b this_o_idx, next_o_idx = 0;
300
301
302         /* Do nothing */
303         if (i1 == i2) return;
304
305
306         /* Old monster */
307         m_ptr = &m_list[i1];
308
309         /* Location */
310         y = m_ptr->fy;
311         x = m_ptr->fx;
312
313         /* Cave grid */
314         c_ptr = &cave[y][x];
315
316         /* Update the cave */
317         c_ptr->m_idx = i2;
318
319         /* Repair objects being carried by monster */
320         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
321         {
322                 object_type *o_ptr;
323
324                 /* Acquire object */
325                 o_ptr = &o_list[this_o_idx];
326
327                 /* Acquire next object */
328                 next_o_idx = o_ptr->next_o_idx;
329
330                 /* Reset monster pointer */
331                 o_ptr->held_m_idx = i2;
332         }
333
334         /* Hack -- Update the target */
335         if (target_who == i1) target_who = i2;
336
337         /* Hack -- Update the target */
338         if (pet_t_m_idx == i1) pet_t_m_idx = i2;
339         if (riding_t_m_idx == i1) riding_t_m_idx = i2;
340
341         /* Hack -- Update the riding */
342         if (p_ptr->riding == i1) p_ptr->riding = i2;
343
344         /* Hack -- Update the health bar */
345         if (p_ptr->health_who == i1) health_track(i2);
346
347         /* Structure copy */
348         COPY(&m_list[i2], &m_list[i1], monster_type);
349
350         /* Wipe the hole */
351         (void)WIPE(&m_list[i1], monster_type);
352
353 }
354
355
356 /*
357  * Compact and Reorder the monster list
358  *
359  * This function can be very dangerous, use with caution!
360  *
361  * When actually "compacting" monsters, we base the saving throw
362  * on a combination of monster level, distance from player, and
363  * current "desperation".
364  *
365  * After "compacting" (if needed), we "reorder" the monsters into a more
366  * compact order, and we reset the allocation info, and the "live" array.
367  */
368 void compact_monsters(int size)
369 {
370         int             i, num, cnt;
371         int             cur_lev, cur_dis, chance;
372
373         /* Message (only if compacting) */
374 #ifdef JP
375         if (size) msg_print("¥â¥ó¥¹¥¿¡¼¾ðÊó¤ò°µ½Ì¤·¤Æ¤¤¤Þ¤¹...");
376 #else
377         if (size) msg_print("Compacting monsters...");
378 #endif
379
380
381         /* Compact at least 'size' objects */
382         for (num = 0, cnt = 1; num < size; cnt++)
383         {
384                 /* Get more vicious each iteration */
385                 cur_lev = 5 * cnt;
386
387                 /* Get closer each iteration */
388                 cur_dis = 5 * (20 - cnt);
389
390                 /* Check all the monsters */
391                 for (i = 1; i < m_max; i++)
392                 {
393                         monster_type *m_ptr = &m_list[i];
394
395                         monster_race *r_ptr = &r_info[m_ptr->r_idx];
396
397                         /* Paranoia -- skip "dead" monsters */
398                         if (!m_ptr->r_idx) continue;
399
400                         /* Hack -- High level monsters start out "immune" */
401                         if (r_ptr->level > cur_lev) continue;
402
403                         if (i == p_ptr->riding) continue;
404
405                         /* Ignore nearby monsters */
406                         if ((cur_dis > 0) && (m_ptr->cdis < cur_dis)) continue;
407
408                         /* Saving throw chance */
409                         chance = 90;
410
411                         /* Only compact "Quest" Monsters in emergencies */
412                         if ((r_ptr->flags1 & (RF1_QUESTOR)) && (cnt < 1000)) chance = 100;
413
414                         /* Try not to compact Unique Monsters */
415                         if (r_ptr->flags1 & (RF1_UNIQUE)) chance = 100;
416
417                         /* All monsters get a saving throw */
418                         if (randint0(100) < chance) continue;
419
420                         /* Delete the monster */
421                         delete_monster_idx(i);
422
423                         /* Count the monster */
424                         num++;
425                 }
426         }
427
428
429         /* Excise dead monsters (backwards!) */
430         for (i = m_max - 1; i >= 1; i--)
431         {
432                 /* Get the i'th monster */
433                 monster_type *m_ptr = &m_list[i];
434
435                 /* Skip real monsters */
436                 if (m_ptr->r_idx) continue;
437
438                 /* Move last monster into open hole */
439                 compact_monsters_aux(m_max - 1, i);
440
441                 /* Compress "m_max" */
442                 m_max--;
443         }
444 }
445
446
447 /*
448  * Pre-calculate the racial counters of preserved pets
449  * To prevent multiple generation of unique monster who is the minion of player
450  */
451 void precalc_cur_num_of_pet(void)
452 {
453         monster_type *m_ptr;
454         int i;
455         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
456
457         for (i = 0; i < max_num; i++)
458         {
459                 m_ptr = &party_mon[i];
460
461                 /* Skip empty monsters */
462                 if (!m_ptr->r_idx) continue;
463
464                 /* Hack -- Increase the racial counter */
465                 real_r_ptr(m_ptr)->cur_num++;
466         }
467 }
468
469
470 /*
471  * Delete/Remove all the monsters when the player leaves the level
472  *
473  * This is an efficient method of simulating multiple calls to the
474  * "delete_monster()" function, with no visual effects.
475  */
476 void wipe_m_list(void)
477 {
478         int i;
479
480         /* Hack -- if Banor or Lupart dies, stay another dead */
481         if (!r_info[MON_BANORLUPART].max_num)
482         {
483                 if (r_info[MON_BANOR].max_num)
484                 {
485                         r_info[MON_BANOR].max_num = 0;
486                         r_info[MON_BANOR].r_pkills++;
487                         if (r_info[MON_BANOR].r_tkills < MAX_SHORT) r_info[MON_BANOR].r_tkills++;
488                 }
489                 if (r_info[MON_LUPART].max_num)
490                 {
491                         r_info[MON_LUPART].max_num = 0;
492                         r_info[MON_LUPART].r_pkills++;
493                         if (r_info[MON_LUPART].r_tkills < MAX_SHORT) r_info[MON_LUPART].r_tkills++;
494                 }
495         }
496
497         /* Delete all the monsters */
498         for (i = m_max - 1; i >= 1; i--)
499         {
500                 monster_type *m_ptr = &m_list[i];
501
502                 /* Skip dead monsters */
503                 if (!m_ptr->r_idx) continue;
504
505                 /* Monster is gone */
506                 cave[m_ptr->fy][m_ptr->fx].m_idx = 0;
507
508                 /* Wipe the Monster */
509                 (void)WIPE(m_ptr, monster_type);
510
511         }
512
513         /*
514          * Wiping racial counters of all monsters and incrementing of racial
515          * counters of monsters in party_mon[] is required to prevent multiple
516          * generation of unique monster who is the minion of player.
517          */
518
519         /* Hack -- Wipe the racial counter of all monster races */
520         for (i = 1; i < max_r_idx; i++) r_info[i].cur_num = 0;
521
522         /* Hack -- Increase the racial counters of pets */
523         precalc_cur_num_of_pet();
524
525         /* Reset "m_max" */
526         m_max = 1;
527
528         /* Reset "m_cnt" */
529         m_cnt = 0;
530
531         /* Hack -- reset "reproducer" count */
532         num_repro = 0;
533
534         /* Hack -- no more target */
535         target_who = 0;
536         pet_t_m_idx = 0;
537         riding_t_m_idx = 0;
538
539         /* Hack -- no more tracking */
540         health_track(0);
541 }
542
543
544 /*
545  * Acquires and returns the index of a "free" monster.
546  *
547  * This routine should almost never fail, but it *can* happen.
548  */
549 s16b m_pop(void)
550 {
551         int i;
552
553
554         /* Normal allocation */
555         if (m_max < max_m_idx)
556         {
557                 /* Access the next hole */
558                 i = m_max;
559
560                 /* Expand the array */
561                 m_max++;
562
563                 /* Count monsters */
564                 m_cnt++;
565
566                 /* Return the index */
567                 return (i);
568         }
569
570
571         /* Recycle dead monsters */
572         for (i = 1; i < m_max; i++)
573         {
574                 monster_type *m_ptr;
575
576                 /* Acquire monster */
577                 m_ptr = &m_list[i];
578
579                 /* Skip live monsters */
580                 if (m_ptr->r_idx) continue;
581
582                 /* Count monsters */
583                 m_cnt++;
584
585                 /* Use this monster */
586                 return (i);
587         }
588
589
590         /* Warn the player (except during dungeon creation) */
591 #ifdef JP
592         if (character_dungeon) msg_print("¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë¡ª");
593 #else
594         if (character_dungeon) msg_print("Too many monsters!");
595 #endif
596
597
598         /* Try not to crash */
599         return (0);
600 }
601
602
603
604
605 /*
606  * Hack -- the "type" of the current "summon specific"
607  */
608 static int summon_specific_type = 0;
609
610
611 /*
612  * Hack -- the index of the summoning monster
613  */
614 static int summon_specific_who = -1;
615
616
617 /*
618  * Hack -- the hostility of the summoned monster
619  */
620 static bool summon_specific_hostile = TRUE;
621
622 static bool summon_unique_okay = FALSE;
623
624
625 static bool summon_specific_aux(int r_idx)
626 {
627         monster_race *r_ptr = &r_info[r_idx];
628         int okay = FALSE;
629
630         /* Check our requirements */
631         switch (summon_specific_type)
632         {
633                 case SUMMON_ANT:
634                 {
635                         okay = (r_ptr->d_char == 'a');
636                         break;
637                 }
638
639                 case SUMMON_SPIDER:
640                 {
641                         okay = (r_ptr->d_char == 'S');
642                         break;
643                 }
644
645                 case SUMMON_HOUND:
646                 {
647                         okay = ((r_ptr->d_char == 'C') || (r_ptr->d_char == 'Z'));
648                         break;
649                 }
650
651                 case SUMMON_HYDRA:
652                 {
653                         okay = (r_ptr->d_char == 'M');
654                         break;
655                 }
656
657                 case SUMMON_ANGEL:
658                 {
659                         okay = (r_ptr->d_char == 'A' && ((r_ptr->flags3 & RF3_EVIL) || (r_ptr->flags3 & RF3_GOOD)));
660                         break;
661                 }
662
663                 case SUMMON_DEMON:
664                 {
665                         okay = (r_ptr->flags3 & RF3_DEMON);
666                         break;
667                 }
668
669                 case SUMMON_UNDEAD:
670                 {
671                         okay = (r_ptr->flags3 & RF3_UNDEAD);
672                         break;
673                 }
674
675                 case SUMMON_DRAGON:
676                 {
677                         okay = (r_ptr->flags3 & RF3_DRAGON);
678                         break;
679                 }
680
681                 case SUMMON_HI_UNDEAD:
682                 {
683                         okay = ((r_ptr->d_char == 'L') ||
684                                 (r_ptr->d_char == 'V') ||
685                                 (r_ptr->d_char == 'W'));
686                         break;
687                 }
688
689                 case SUMMON_HI_DRAGON:
690                 {
691                         okay = (r_ptr->d_char == 'D');
692                         break;
693                 }
694
695                 case SUMMON_HI_DEMON:
696                 {
697                         okay = (((r_ptr->d_char == 'U') ||
698                                  (r_ptr->d_char == 'H') ||
699                                  (r_ptr->d_char == 'B')) &&
700                                 (r_ptr->flags3 & RF3_DEMON)) ? TRUE : FALSE;
701                         break;
702                 }
703
704                 case SUMMON_AMBERITES:
705                 {
706                         okay = (r_ptr->flags3 & (RF3_AMBERITE)) ? TRUE : FALSE;
707                         break;
708                 }
709
710                 case SUMMON_UNIQUE:
711                 {
712                         okay = (r_ptr->flags1 & (RF1_UNIQUE)) ? TRUE : FALSE;
713                         break;
714                 }
715
716                 case SUMMON_BIZARRE1:
717                 {
718                         okay = (r_ptr->d_char == 'm');
719                         break;
720                 }
721                 case SUMMON_BIZARRE2:
722                 {
723                         okay = (r_ptr->d_char == 'b');
724                         break;
725                 }
726                 case SUMMON_BIZARRE3:
727                 {
728                         okay = (r_ptr->d_char == 'Q');
729                         break;
730                 }
731
732                 case SUMMON_BIZARRE4:
733                 {
734                         okay = (r_ptr->d_char == 'v');
735                         break;
736                 }
737
738                 case SUMMON_BIZARRE5:
739                 {
740                         okay = (r_ptr->d_char == '$');
741                         break;
742                 }
743
744                 case SUMMON_BIZARRE6:
745                 {
746                         okay = ((r_ptr->d_char == '!') ||
747                                  (r_ptr->d_char == '?') ||
748                                  (r_ptr->d_char == '=') ||
749                                  (r_ptr->d_char == '$') ||
750                                  (r_ptr->d_char == '|'));
751                         break;
752                 }
753
754                 case SUMMON_GOLEM:
755                 {
756                         okay = (r_ptr->d_char == 'g');
757                         break;
758                 }
759
760                 case SUMMON_CYBER:
761                 {
762                         okay = ((r_ptr->d_char == 'U') &&
763                                 (r_ptr->flags4 & RF4_ROCKET));
764                         break;
765                 }
766
767
768                 case SUMMON_KIN:
769                 {
770                         okay = ((r_ptr->d_char == summon_kin_type) && (r_idx != MON_HAGURE));
771                         break;
772                 }
773
774                 case SUMMON_DAWN:
775                 {
776                         okay = (r_idx == MON_DAWN);
777                         break;
778                 }
779
780                 case SUMMON_ANIMAL:
781                 {
782                         okay = (r_ptr->flags3 & (RF3_ANIMAL));
783                         break;
784                 }
785
786                 case SUMMON_ANIMAL_RANGER:
787                 {
788                         okay = ((r_ptr->flags3 & (RF3_ANIMAL)) &&
789                                (strchr("abcflqrwBCHIJKMRS", r_ptr->d_char)) &&
790                                !(r_ptr->flags3 & (RF3_DRAGON)) &&
791                                !(r_ptr->flags3 & (RF3_EVIL)) &&
792                                !(r_ptr->flags3 & (RF3_UNDEAD)) &&
793                                !(r_ptr->flags3 & (RF3_DEMON)) &&
794                                !(r_ptr->flags2 & (RF2_MULTIPLY)) &&
795                                !(r_ptr->flags4 || r_ptr->flags5 || r_ptr->flags6));
796                         break;
797                 }
798
799                 case SUMMON_HI_DRAGON_LIVING:
800                 {
801                         okay = ((r_ptr->d_char == 'D') && monster_living(r_ptr));
802                         break;
803                 }
804
805                 case SUMMON_LIVING:
806                 {
807                         okay = monster_living(r_ptr);
808                         break;
809                 }
810
811                 case SUMMON_PHANTOM:
812                 {
813                         okay = (r_idx == MON_PHANTOM_B || r_idx == MON_PHANTOM_W);
814                         break;
815                 }
816
817                 case SUMMON_BLUE_HORROR:
818                 {
819                         okay = (r_idx == MON_BLUE_HORROR);
820                         break;
821                 }
822
823                 case SUMMON_ELEMENTAL:
824                 {
825                         okay = (r_ptr->d_char == 'E');
826                         break;
827                 }
828
829                 case SUMMON_VORTEX:
830                 {
831                         okay = (r_ptr->d_char == 'v');
832                         break;
833                 }
834
835                 case SUMMON_HYBRID:
836                 {
837                         okay = (r_ptr->d_char == 'H');
838                         break;
839                 }
840
841                 case SUMMON_BIRD:
842                 {
843                         okay = (r_ptr->d_char == 'B');
844                         break;
845                 }
846
847                 case SUMMON_KAMIKAZE:
848                 {
849                         int i;
850                         for (i = 0; i < 4; i++)
851                                 if (r_ptr->blow[i].method == RBM_EXPLODE) okay = TRUE;
852                         break;
853                 }
854
855                 case SUMMON_KAMIKAZE_LIVING:
856                 {
857                         int i;
858
859                         for (i = 0; i < 4; i++)
860                                 if (r_ptr->blow[i].method == RBM_EXPLODE) okay = TRUE;
861                         okay = (okay && monster_living(r_ptr));
862                         break;
863                 }
864
865                 case SUMMON_MANES:
866                 {
867                         okay = (r_idx == MON_MANES);
868                         break;
869                 }
870
871                 case SUMMON_LOUSE:
872                 {
873                         okay = (r_idx == MON_LOUSE);
874                         break;
875                 }
876
877                 case SUMMON_GUARDIANS:
878                 {
879                         okay = (r_ptr->flags7 & RF7_GUARDIAN);
880                         break;
881                 }
882
883                 case SUMMON_KNIGHTS:
884                 {
885                         okay = ((r_idx == MON_NOV_PALADIN) ||
886                                 (r_idx == MON_NOV_PALADIN_G) ||
887                                 (r_idx == MON_PALADIN) ||
888                                 (r_idx == MON_W_KNIGHT) ||
889                                 (r_idx == MON_ULTRA_PALADIN) ||
890                                 (r_idx == MON_KNI_TEMPLAR));
891                         break;
892                 }
893
894                 case SUMMON_EAGLES:
895                 {
896                         okay = (r_ptr->d_char == 'B' &&
897                                 (r_ptr->flags8 & RF8_WILD_MOUNTAIN) &&
898                                 (r_ptr->flags8 & RF8_WILD_ONLY));
899                         break;
900                 }
901
902                 case SUMMON_PIRANHAS:
903                 {
904                         okay = (r_idx == MON_PIRANHA);
905                         break;
906                 }
907
908                 case SUMMON_ARMAGE_GOOD:
909                 {
910                         okay = (r_ptr->d_char == 'A' && (r_ptr->flags3 & RF3_GOOD));
911                         break;
912                 }
913
914                 case SUMMON_ARMAGE_EVIL:
915                 {
916                         okay = ((r_ptr->flags3 & RF3_DEMON) ||
917                                 (r_ptr->d_char == 'A' && (r_ptr->flags3 & RF3_EVIL)));
918                         break;
919                 }
920         }
921
922         /* Result */
923         /* Since okay is int, "return (okay);" is not correct. */
924         return (bool)(okay ? TRUE : FALSE);
925 }
926
927
928 static int chameleon_change_m_idx = 0;
929
930
931 /*
932  * Some dungeon types restrict the possible monsters.
933  * Return TRUE is the monster is OK and FALSE otherwise
934  */
935 static bool restrict_monster_to_dungeon(int r_idx)
936 {
937         dungeon_info_type *d_ptr = &d_info[dungeon_type];
938         monster_race *r_ptr = &r_info[r_idx];
939         byte a;
940
941         if (d_ptr->flags1 & DF1_CHAMELEON)
942         {
943                 if (chameleon_change_m_idx) return TRUE;
944         }
945         if (d_ptr->flags1 & DF1_NO_MAGIC)
946         {
947                 if (r_idx != MON_CHAMELEON &&
948                     r_ptr->freq_spell && 
949                     !(r_ptr->flags4 & RF4_NOMAGIC_MASK) &&
950                     !(r_ptr->flags5 & RF5_NOMAGIC_MASK) &&
951                     !(r_ptr->flags6 & RF6_NOMAGIC_MASK))
952                         return FALSE;
953         }
954         if (d_ptr->flags1 & DF1_NO_MELEE)
955         {
956                 if (r_idx == MON_CHAMELEON) return TRUE;
957                 if (!(r_ptr->flags4 & (RF4_BOLT_MASK | RF4_BEAM_MASK | RF4_BALL_MASK)) &&
958                     !(r_ptr->flags5 & (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)) &&
959                     !(r_ptr->flags6 & (RF6_BOLT_MASK | RF6_BEAM_MASK | RF6_BALL_MASK)))
960                         return FALSE;
961         }
962         if (d_ptr->flags1 & DF1_BEGINNER)
963         {
964                 if (r_ptr->level > dun_level)
965                         return FALSE;
966         }
967
968         if (d_ptr->special_div >= 64) return TRUE;
969         if (summon_specific_type && !(d_ptr->flags1 & DF1_CHAMELEON)) return TRUE;
970
971         switch (d_ptr->mode)
972         {
973         case DUNGEON_MODE_AND:
974                 if (d_ptr->mflags1)
975                 {
976                         if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
977                                 return FALSE;
978                 }
979                 if (d_ptr->mflags2)
980                 {
981                         if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
982                                 return FALSE;
983                 }
984                 if (d_ptr->mflags3)
985                 {
986                         if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
987                                 return FALSE;
988                 }
989                 if (d_ptr->mflags4)
990                 {
991                         if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
992                                 return FALSE;
993                 }
994                 if (d_ptr->mflags5)
995                 {
996                         if ((d_ptr->mflags5 & r_ptr->flags5) != d_ptr->mflags5)
997                                 return FALSE;
998                 }
999                 if (d_ptr->mflags6)
1000                 {
1001                         if ((d_ptr->mflags6 & r_ptr->flags6) != d_ptr->mflags6)
1002                                 return FALSE;
1003                 }
1004                 if (d_ptr->mflags7)
1005                 {
1006                         if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
1007                                 return FALSE;
1008                 }
1009                 if (d_ptr->mflags8)
1010                 {
1011                         if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
1012                                 return FALSE;
1013                 }
1014                 if (d_ptr->mflags9)
1015                 {
1016                         if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
1017                                 return FALSE;
1018                 }
1019                 if (d_ptr->mflagsr)
1020                 {
1021                         if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
1022                                 return FALSE;
1023                 }
1024                 for (a = 0; a < 5; a++)
1025                         if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char)) return FALSE;
1026
1027                 return TRUE;
1028
1029         case DUNGEON_MODE_NAND:
1030                 if (d_ptr->mflags1)
1031                 {
1032                         if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
1033                                 return TRUE;
1034                 }
1035                 if (d_ptr->mflags2)
1036                 {
1037                         if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
1038                                 return TRUE;
1039                 }
1040                 if (d_ptr->mflags3)
1041                 {
1042                         if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
1043                                 return TRUE;
1044                 }
1045                 if (d_ptr->mflags4)
1046                 {
1047                         if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
1048                                 return TRUE;
1049                 }
1050                 if (d_ptr->mflags5)
1051                 {
1052                         if ((d_ptr->mflags5 & r_ptr->flags5) != d_ptr->mflags5)
1053                                 return TRUE;
1054                 }
1055                 if (d_ptr->mflags6)
1056                 {
1057                         if ((d_ptr->mflags6 & r_ptr->flags6) != d_ptr->mflags6)
1058                                 return TRUE;
1059                 }
1060                 if (d_ptr->mflags7)
1061                 {
1062                         if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
1063                                 return TRUE;
1064                 }
1065                 if (d_ptr->mflags8)
1066                 {
1067                         if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
1068                                 return TRUE;
1069                 }
1070                 if (d_ptr->mflags9)
1071                 {
1072                         if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
1073                                 return TRUE;
1074                 }
1075                 if (d_ptr->mflagsr)
1076                 {
1077                         if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
1078                                 return TRUE;
1079                 }
1080                 for (a = 0; a < 5; a++)
1081                         if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char)) return TRUE;
1082
1083                 return FALSE;
1084
1085         case DUNGEON_MODE_OR:
1086                 if (r_ptr->flags1 & d_ptr->mflags1) return TRUE;
1087                 if (r_ptr->flags2 & d_ptr->mflags2) return TRUE;
1088                 if (r_ptr->flags3 & d_ptr->mflags3) return TRUE;
1089                 if (r_ptr->flags4 & d_ptr->mflags4) return TRUE;
1090                 if (r_ptr->flags5 & d_ptr->mflags5) return TRUE;
1091                 if (r_ptr->flags6 & d_ptr->mflags6) return TRUE;
1092                 if (r_ptr->flags7 & d_ptr->mflags7) return TRUE;
1093                 if (r_ptr->flags8 & d_ptr->mflags8) return TRUE;
1094                 if (r_ptr->flags9 & d_ptr->mflags9) return TRUE;
1095                 if (r_ptr->flagsr & d_ptr->mflagsr) return TRUE;
1096                 for (a = 0; a < 5; a++)
1097                         if (d_ptr->r_char[a] == r_ptr->d_char) return TRUE;
1098
1099                 return FALSE;
1100
1101         case DUNGEON_MODE_NOR:
1102                 if (r_ptr->flags1 & d_ptr->mflags1) return FALSE;
1103                 if (r_ptr->flags2 & d_ptr->mflags2) return FALSE;
1104                 if (r_ptr->flags3 & d_ptr->mflags3) return FALSE;
1105                 if (r_ptr->flags4 & d_ptr->mflags4) return FALSE;
1106                 if (r_ptr->flags5 & d_ptr->mflags5) return FALSE;
1107                 if (r_ptr->flags6 & d_ptr->mflags6) return FALSE;
1108                 if (r_ptr->flags7 & d_ptr->mflags7) return FALSE;
1109                 if (r_ptr->flags8 & d_ptr->mflags8) return FALSE;
1110                 if (r_ptr->flags9 & d_ptr->mflags9) return FALSE;
1111                 if (r_ptr->flagsr & d_ptr->mflagsr) return FALSE;
1112                 for (a = 0; a < 5; a++)
1113                         if (d_ptr->r_char[a] == r_ptr->d_char) return FALSE;
1114
1115                 return TRUE;
1116         }
1117
1118         return TRUE;
1119 }
1120
1121 /*
1122  * Apply a "monster restriction function" to the "monster allocation table"
1123  */
1124 errr get_mon_num_prep(monster_hook_type monster_hook,
1125                                           monster_hook_type monster_hook2)
1126 {
1127         int i;
1128
1129         /* Todo: Check the hooks for non-changes */
1130
1131         /* Set the new hooks */
1132         get_mon_num_hook = monster_hook;
1133         get_mon_num2_hook = monster_hook2;
1134
1135         /* Scan the allocation table */
1136         for (i = 0; i < alloc_race_size; i++)
1137         {
1138                 monster_race    *r_ptr;
1139                 
1140                 /* Get the entry */
1141                 alloc_entry *entry = &alloc_race_table[i];
1142
1143                 entry->prob2 = 0;
1144                 r_ptr = &r_info[entry->index];
1145
1146                 /* Skip monsters which don't pass the restriction */
1147                 if ((get_mon_num_hook && !((*get_mon_num_hook)(entry->index))) ||
1148                     (get_mon_num2_hook && !((*get_mon_num2_hook)(entry->index))))
1149                         continue;
1150
1151                 if (!p_ptr->inside_battle && !chameleon_change_m_idx &&
1152                     summon_specific_type != SUMMON_GUARDIANS)
1153                 {
1154                         /* Hack -- don't create questors */
1155                         if (r_ptr->flags1 & RF1_QUESTOR)
1156                                 continue;
1157
1158                         if (r_ptr->flags7 & RF7_GUARDIAN)
1159                                 continue;
1160
1161                         /* Depth Monsters never appear out of depth */
1162                         if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) &&
1163                             (r_ptr->level > dun_level))
1164                                 continue;
1165                 }
1166
1167                 /* Accept this monster */
1168                 entry->prob2 = entry->prob1;
1169
1170                 if (dun_level && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest)) && !restrict_monster_to_dungeon(entry->index) && !p_ptr->inside_battle)
1171                 {
1172                         int hoge = entry->prob2 * d_info[dungeon_type].special_div;
1173                         entry->prob2 = hoge / 64;
1174                         if (randint0(64) < (hoge & 0x3f)) entry->prob2++;
1175                 }
1176         }
1177
1178         /* Success */
1179         return (0);
1180 }
1181
1182
1183 static int mysqrt(int n)
1184 {
1185         int tmp = n>>1;
1186         int tasu = 10;
1187         int kaeriti = 1;
1188
1189         if (!tmp)
1190         {
1191                 if (n) return 1;
1192                 else return 0;
1193         }
1194
1195         while(tmp)
1196         {
1197                 if ((n/tmp) < tmp)
1198                 {
1199                         tmp >>= 1;
1200                 }
1201                 else break;
1202         }
1203         kaeriti = tmp;
1204         while(tasu)
1205         {
1206                 if ((n/tmp) < tmp)
1207                 {
1208                         tasu--;
1209                         tmp = kaeriti;
1210                 }
1211                 else
1212                 {
1213                         kaeriti = tmp;
1214                         tmp += tasu;
1215                 }
1216         }
1217         return kaeriti;
1218 }
1219
1220 /*
1221  * Choose a monster race that seems "appropriate" to the given level
1222  *
1223  * This function uses the "prob2" field of the "monster allocation table",
1224  * and various local information, to calculate the "prob3" field of the
1225  * same table, which is then used to choose an "appropriate" monster, in
1226  * a relatively efficient manner.
1227  *
1228  * Note that "town" monsters will *only* be created in the town, and
1229  * "normal" monsters will *never* be created in the town, unless the
1230  * "level" is "modified", for example, by polymorph or summoning.
1231  *
1232  * There is a small chance (1/50) of "boosting" the given depth by
1233  * a small amount (up to four levels), except in the town.
1234  *
1235  * It is (slightly) more likely to acquire a monster of the given level
1236  * than one of a lower level.  This is done by choosing several monsters
1237  * appropriate to the given level and keeping the "hardest" one.
1238  *
1239  * Note that if no monsters are "appropriate", then this function will
1240  * fail, and return zero, but this should *almost* never happen.
1241  */
1242 s16b get_mon_num(int level)
1243 {
1244         int                     i, j, p;
1245
1246         int                     r_idx;
1247
1248         long            value, total;
1249
1250         monster_race    *r_ptr;
1251
1252         alloc_entry             *table = alloc_race_table;
1253
1254         int pls_kakuritu, pls_level;
1255         int hoge=mysqrt(level*10000L);
1256
1257         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
1258
1259         if ((dungeon_turn > hoge*(TURNS_PER_TICK*500L)) && !level)
1260         {
1261                 pls_kakuritu = MAX(2, NASTY_MON-((dungeon_turn/(TURNS_PER_TICK*2500L)-hoge/10)));
1262                 pls_level = MIN(8,3 + dungeon_turn/(TURNS_PER_TICK*20000L)-hoge/40);
1263         }
1264         else
1265         {
1266                 pls_kakuritu = NASTY_MON;
1267                 pls_level = 2;
1268         }
1269
1270         if (d_info[dungeon_type].flags1 & DF1_MAZE)
1271         {
1272                 pls_kakuritu = MIN(pls_kakuritu/2, pls_kakuritu-10);
1273                 if (pls_kakuritu < 2) pls_kakuritu = 2;
1274                 pls_level += 2;
1275                 level += 3;
1276         }
1277
1278         /* Boost the level */
1279         if ((level > 0) && !p_ptr->inside_battle && !(d_info[dungeon_type].flags1 & DF1_BEGINNER))
1280         {
1281                 /* Nightmare mode allows more out-of depth monsters */
1282                 if (ironman_nightmare && !randint0(pls_kakuritu))
1283                 {
1284                         /* What a bizarre calculation */
1285                         level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
1286                 }
1287                 else
1288                 {
1289                         /* Occasional "nasty" monster */
1290                         if (!randint0(pls_kakuritu))
1291                         {
1292                                 /* Pick a level bonus */
1293                                 int d = MIN(5, level/10) + pls_level;
1294
1295                                 /* Boost the level */
1296                                 level += d;
1297                         }
1298
1299                         /* Occasional "nasty" monster */
1300                         if (!randint0(pls_kakuritu))
1301                         {
1302                                 /* Pick a level bonus */
1303                                 int d = MIN(5, level/10) + pls_level;
1304
1305                                 /* Boost the level */
1306                                 level += d;
1307                         }
1308                 }
1309         }
1310
1311
1312         /* Reset total */
1313         total = 0L;
1314
1315         /* Process probabilities */
1316         for (i = 0; i < alloc_race_size; i++)
1317         {
1318                 /* Monsters are sorted by depth */
1319                 if (table[i].level > level) break;
1320
1321                 /* Default */
1322                 table[i].prob3 = 0;
1323
1324                 /* Access the "r_idx" of the chosen monster */
1325                 r_idx = table[i].index;
1326
1327                 /* Access the actual race */
1328                 r_ptr = &r_info[r_idx];
1329
1330                 if (!p_ptr->inside_battle && !chameleon_change_m_idx)
1331                 {
1332                         /* Hack -- "unique" monsters must be "unique" */
1333                         if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
1334                              (r_ptr->flags7 & (RF7_UNIQUE_7))) &&
1335                             (r_ptr->cur_num >= r_ptr->max_num))
1336                         {
1337                                 continue;
1338                         }
1339
1340                         if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
1341                             (r_ptr->cur_num >= 1))
1342                         {
1343                                 continue;
1344                         }
1345
1346                         if (r_idx == MON_BANORLUPART)
1347                         {
1348                                 if (r_info[MON_BANOR].cur_num > 0) continue;
1349                                 if (r_info[MON_LUPART].cur_num > 0) continue;
1350                         }
1351                 }
1352
1353                 /* Accept */
1354                 table[i].prob3 = table[i].prob2;
1355
1356                 /* Total */
1357                 total += table[i].prob3;
1358         }
1359
1360         /* No legal monsters */
1361         if (total <= 0) return (0);
1362
1363
1364         /* Pick a monster */
1365         value = randint0(total);
1366
1367         /* Find the monster */
1368         for (i = 0; i < alloc_race_size; i++)
1369         {
1370                 /* Found the entry */
1371                 if (value < table[i].prob3) break;
1372
1373                 /* Decrement */
1374                 value = value - table[i].prob3;
1375         }
1376
1377
1378         /* Power boost */
1379         p = randint0(100);
1380
1381         /* Try for a "harder" monster once (50%) or twice (10%) */
1382         if (p < 60)
1383         {
1384                 /* Save old */
1385                 j = i;
1386
1387                 /* Pick a monster */
1388                 value = randint0(total);
1389
1390                 /* Find the monster */
1391                 for (i = 0; i < alloc_race_size; i++)
1392                 {
1393                         /* Found the entry */
1394                         if (value < table[i].prob3) break;
1395
1396                         /* Decrement */
1397                         value = value - table[i].prob3;
1398                 }
1399
1400                 /* Keep the "best" one */
1401                 if (table[i].level < table[j].level) i = j;
1402         }
1403
1404         /* Try for a "harder" monster twice (10%) */
1405         if (p < 10)
1406         {
1407                 /* Save old */
1408                 j = i;
1409
1410                 /* Pick a monster */
1411                 value = randint0(total);
1412
1413                 /* Find the monster */
1414                 for (i = 0; i < alloc_race_size; i++)
1415                 {
1416                         /* Found the entry */
1417                         if (value < table[i].prob3) break;
1418
1419                         /* Decrement */
1420                         value = value - table[i].prob3;
1421                 }
1422
1423                 /* Keep the "best" one */
1424                 if (table[i].level < table[j].level) i = j;
1425         }
1426
1427         /* Result */
1428         return (table[i].index);
1429 }
1430
1431
1432
1433
1434
1435 /*
1436  * Build a string describing a monster in some way.
1437  *
1438  * We can correctly describe monsters based on their visibility.
1439  * We can force all monsters to be treated as visible or invisible.
1440  * We can build nominatives, objectives, possessives, or reflexives.
1441  * We can selectively pronominalize hidden, visible, or all monsters.
1442  * We can use definite or indefinite descriptions for hidden monsters.
1443  * We can use definite or indefinite descriptions for visible monsters.
1444  *
1445  * Pronominalization involves the gender whenever possible and allowed,
1446  * so that by cleverly requesting pronominalization / visibility, you
1447  * can get messages like "You hit someone.  She screams in agony!".
1448  *
1449  * Reflexives are acquired by requesting Objective plus Possessive.
1450  *
1451  * If no m_ptr arg is given (?), the monster is assumed to be hidden,
1452  * unless the "Assume Visible" mode is requested.
1453  *
1454  * If no r_ptr arg is given, it is extracted from m_ptr and r_info
1455  * If neither m_ptr nor r_ptr is given, the monster is assumed to
1456  * be neuter, singular, and hidden (unless "Assume Visible" is set),
1457  * in which case you may be in trouble... :-)
1458  *
1459  * I am assuming that no monster name is more than 70 characters long,
1460  * so that "char desc[80];" is sufficiently large for any result.
1461  *
1462  * Mode Flags:
1463  *  MD_OBJECTIVE      --> Objective (or Reflexive)
1464  *  MD_POSSESSIVE     --> Possessive (or Reflexive)
1465  *  MD_INDEF_HIDDEN   --> Use indefinites for hidden monsters ("something")
1466  *  MD_INDEF_VISIBLE  --> Use indefinites for visible monsters ("a kobold")
1467  *  MD_PRON_HIDDEN    --> Pronominalize hidden monsters
1468  *  MD_PRON_VISIBLE   --> Pronominalize visible monsters
1469  *  MD_ASSUME_HIDDEN  --> Assume the monster is hidden
1470  *  MD_ASSUME_VISIBLE --> Assume the monster is visible
1471  *  MD_TRUE_NAME      --> Chameleon's true name
1472  *  MD_IGNORE_HALLU   --> Ignore hallucination, and penetrate shape change
1473  *  MD_ASSUME_OUTSIDE --> Assume this monster pet waiting outside the floor
1474  *
1475  * Useful Modes:
1476  *  0x00 --> Full nominative name ("the kobold") or "it"
1477  *  MD_INDEF_HIDDEN --> Full nominative name ("the kobold") or "something"
1478  *  MD_ASSUME_VISIBLE --> Genocide resistance name ("the kobold")
1479  *  MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE --> Killing name ("a kobold")
1480  *  MD_PRON_VISIBLE | MD_POSSESSIVE
1481  *    --> Possessive, genderized if visable ("his") or "its"
1482  *  MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE
1483  *    --> Reflexive, genderized if visable ("himself") or "itself"
1484  */
1485 void monster_desc(char *desc, monster_type *m_ptr, int mode)
1486 {
1487         cptr            res;
1488         monster_race    *r_ptr;
1489
1490         cptr            name;
1491         char            buf[128];
1492         char            silly_name[1024];
1493         bool            seen, pron;
1494         bool            named = FALSE;
1495
1496         r_ptr = &r_info[m_ptr->ap_r_idx];
1497
1498         /* Mode of MD_TRUE_NAME will reveal Chameleon's true name */
1499         if (mode & MD_TRUE_NAME) name = (r_name + real_r_ptr(m_ptr)->name);
1500         else name = (r_name + r_ptr->name);
1501
1502         /* Are we hallucinating? (Idea from Nethack...) */
1503         if (p_ptr->image && !(mode & MD_IGNORE_HALLU))
1504         {
1505                 if (one_in_(2))
1506                 {
1507 #ifdef JP
1508                         if (!get_rnd_line("silly_j.txt", m_ptr->r_idx, silly_name))
1509 #else
1510                         if (!get_rnd_line("silly.txt", m_ptr->r_idx, silly_name))
1511 #endif
1512
1513                                 named = TRUE;
1514                 }
1515
1516                 if (!named)
1517                 {
1518                         monster_race *hallu_race;
1519
1520                         do
1521                         {
1522                                 hallu_race = &r_info[randint1(max_r_idx - 1)];
1523                         }
1524                         while (hallu_race->flags1 & RF1_UNIQUE);
1525
1526                         strcpy(silly_name, (r_name + hallu_race->name));
1527                 }
1528
1529                 /* Better not strcpy it, or we could corrupt r_info... */
1530                 name = silly_name;
1531         }
1532
1533         /* Can we "see" it (exists + forced, or visible + not unforced) */
1534         seen = (m_ptr && ((mode & MD_ASSUME_VISIBLE) || (!(mode & MD_ASSUME_HIDDEN) && m_ptr->ml)));
1535
1536         /* Sexed Pronouns (seen and allowed, or unseen and allowed) */
1537         pron = (m_ptr && ((seen && (mode & MD_PRON_VISIBLE)) || (!seen && (mode & MD_PRON_HIDDEN))));
1538
1539
1540         /* First, try using pronouns, or describing hidden monsters */
1541         if (!seen || pron)
1542         {
1543                 /* an encoding of the monster "sex" */
1544                 int kind = 0x00;
1545
1546                 /* Extract the gender (if applicable) */
1547                 if (r_ptr->flags1 & (RF1_FEMALE)) kind = 0x20;
1548                 else if (r_ptr->flags1 & (RF1_MALE)) kind = 0x10;
1549
1550                 /* Ignore the gender (if desired) */
1551                 if (!m_ptr || !pron) kind = 0x00;
1552
1553
1554                 /* Assume simple result */
1555 #ifdef JP
1556                 res = "²¿¤«";
1557 #else
1558                 res = "it";
1559 #endif
1560
1561
1562                 /* Brute force: split on the possibilities */
1563                 switch (kind + (mode & (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE)))
1564                 {
1565                         /* Neuter, or unknown */
1566 #ifdef JP
1567                         case 0x00:                                                    res = "²¿¤«"; break;
1568                         case 0x00 + (MD_OBJECTIVE):                                   res = "²¿¤«"; break;
1569                         case 0x00 + (MD_POSSESSIVE):                                  res = "²¿¤«¤Î"; break;
1570                         case 0x00 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "²¿¤«¼«¿È"; break;
1571                         case 0x00 + (MD_INDEF_HIDDEN):                                res = "²¿¤«"; break;
1572                         case 0x00 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "²¿¤«"; break;
1573                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "²¿¤«"; break;
1574                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "¤½¤ì¼«¿È"; break;
1575 #else
1576                         case 0x00:                                                    res = "it"; break;
1577                         case 0x00 + (MD_OBJECTIVE):                                   res = "it"; break;
1578                         case 0x00 + (MD_POSSESSIVE):                                  res = "its"; break;
1579                         case 0x00 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "itself"; break;
1580                         case 0x00 + (MD_INDEF_HIDDEN):                                res = "something"; break;
1581                         case 0x00 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "something"; break;
1582                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "something's"; break;
1583                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "itself"; break;
1584 #endif
1585
1586
1587                         /* Male (assume human if vague) */
1588 #ifdef JP
1589                         case 0x10:                                                    res = "Èà"; break;
1590                         case 0x10 + (MD_OBJECTIVE):                                   res = "Èà"; break;
1591                         case 0x10 + (MD_POSSESSIVE):                                  res = "Èà¤Î"; break;
1592                         case 0x10 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "È༫¿È"; break;
1593                         case 0x10 + (MD_INDEF_HIDDEN):                                res = "狼"; break;
1594                         case 0x10 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "狼"; break;
1595                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "狼¤Î"; break;
1596                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "È༫¿È"; break;
1597 #else
1598                         case 0x10:                                                    res = "he"; break;
1599                         case 0x10 + (MD_OBJECTIVE):                                   res = "him"; break;
1600                         case 0x10 + (MD_POSSESSIVE):                                  res = "his"; break;
1601                         case 0x10 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "himself"; break;
1602                         case 0x10 + (MD_INDEF_HIDDEN):                                res = "someone"; break;
1603                         case 0x10 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "someone"; break;
1604                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "someone's"; break;
1605                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "himself"; break;
1606 #endif
1607
1608
1609                         /* Female (assume human if vague) */
1610 #ifdef JP
1611                         case 0x20:                                                    res = "Èà½÷"; break;
1612                         case 0x20 + (MD_OBJECTIVE):                                   res = "Èà½÷"; break;
1613                         case 0x20 + (MD_POSSESSIVE):                                  res = "Èà½÷¤Î"; break;
1614                         case 0x20 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "Èà½÷¼«¿È"; break;
1615                         case 0x20 + (MD_INDEF_HIDDEN):                                res = "狼"; break;
1616                         case 0x20 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "狼"; break;
1617                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "狼¤Î"; break;
1618                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "Èà½÷¼«¿È"; break;
1619 #else
1620                         case 0x20:                                                    res = "she"; break;
1621                         case 0x20 + (MD_OBJECTIVE):                                   res = "her"; break;
1622                         case 0x20 + (MD_POSSESSIVE):                                  res = "her"; break;
1623                         case 0x20 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "herself"; break;
1624                         case 0x20 + (MD_INDEF_HIDDEN):                                res = "someone"; break;
1625                         case 0x20 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "someone"; break;
1626                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "someone's"; break;
1627                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "herself"; break;
1628 #endif
1629                 }
1630
1631                 /* Copy the result */
1632                 (void)strcpy(desc, res);
1633         }
1634
1635
1636         /* Handle visible monsters, "reflexive" request */
1637         else if ((mode & (MD_POSSESSIVE | MD_OBJECTIVE)) == (MD_POSSESSIVE | MD_OBJECTIVE))
1638         {
1639                 /* The monster is visible, so use its gender */
1640 #ifdef JP
1641                 if (r_ptr->flags1 & (RF1_FEMALE)) strcpy(desc, "Èà½÷¼«¿È");
1642                 else if (r_ptr->flags1 & (RF1_MALE)) strcpy(desc, "È༫¿È");
1643                 else strcpy(desc, "¤½¤ì¼«¿È");
1644 #else
1645                 if (r_ptr->flags1 & RF1_FEMALE) strcpy(desc, "herself");
1646                 else if (r_ptr->flags1 & RF1_MALE) strcpy(desc, "himself");
1647                 else strcpy(desc, "itself");
1648 #endif
1649         }
1650
1651
1652         /* Handle all other visible monster requests */
1653         else
1654         {
1655                 /* Tanuki? */
1656                 if (is_pet(m_ptr) && m_ptr->ap_r_idx != m_ptr->r_idx)
1657                 {
1658 #ifdef JP
1659                         char *t;
1660                         strcpy(buf, name);
1661                         t = buf;
1662                         while(strncmp(t, "¡Ù", 2) && *t) t++;
1663                         if (*t)
1664                         {
1665                                 *t = '\0';
1666                                 (void)sprintf(desc, "%s¡©¡Ù", buf);
1667                         }
1668                         else
1669                                 (void)sprintf(desc, "%s¡©", name);
1670 #else
1671                         (void)sprintf(desc, "%s?", name);
1672 #endif
1673                 }
1674                 else
1675
1676                 /* It could be a Unique */
1677                 if ((r_ptr->flags1 & RF1_UNIQUE) && !(p_ptr->image && !(mode & MD_IGNORE_HALLU)))
1678                 {
1679                         /* Start with the name (thus nominative and objective) */
1680                         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && !(mode & MD_TRUE_NAME))
1681                         {
1682 #ifdef JP
1683                                 char *t;
1684                                 strcpy(buf, name);
1685                                 t = buf;
1686                                 while (strncmp(t, "¡Ù", 2) && *t) t++;
1687                                 if (*t)
1688                                 {
1689                                         *t = '\0';
1690                                         (void)sprintf(desc, "%s¡©¡Ù", buf);
1691                                 }
1692                                 else
1693                                         (void)sprintf(desc, "%s¡©", name);
1694 #else
1695                                 (void)sprintf(desc, "%s?", name);
1696 #endif
1697                         }
1698                         else if ((cave[m_ptr->fy][m_ptr->fx].m_idx == p_ptr->riding) || !p_ptr->inside_battle)
1699                                 (void)strcpy(desc, name);
1700                         else
1701 #ifdef JP
1702                                 (void)sprintf(desc, "%s¤â¤É¤­", name);
1703 #else
1704                                 (void)sprintf(desc, "fake %s", name);
1705 #endif
1706                 }
1707
1708                 /* It could be an indefinite monster */
1709                 else if (mode & MD_INDEF_VISIBLE)
1710                 {
1711                         /* XXX Check plurality for "some" */
1712
1713                         /* Indefinite monsters need an indefinite article */
1714 #ifdef JP
1715                         (void)strcpy(desc, "");
1716 #else
1717                         (void)strcpy(desc, is_a_vowel(name[0]) ? "an " : "a ");
1718 #endif
1719
1720                         (void)strcat(desc, name);
1721                 }
1722
1723                 /* It could be a normal, definite, monster */
1724                 else
1725                 {
1726                         /* Definite monsters need a definite article */
1727                         if (is_pet(m_ptr))
1728 #ifdef JP
1729                                 (void)strcpy(desc, "¤¢¤Ê¤¿¤Î");
1730 #else
1731                                 (void)strcpy(desc, "your ");
1732 #endif
1733
1734                         else
1735 #ifdef JP
1736                                 (void)strcpy(desc, "");
1737 #else
1738                                 (void)strcpy(desc, "the ");
1739 #endif
1740
1741                         (void)strcat(desc, name);
1742                 }
1743
1744                 if (m_ptr->nickname)
1745                 {
1746 #ifdef JP
1747                         sprintf(buf,"¡Ö%s¡×",quark_str(m_ptr->nickname));
1748 #else
1749                         sprintf(buf," called %s",quark_str(m_ptr->nickname));
1750 #endif
1751                         strcat(desc,buf);
1752                 }
1753
1754                 if (player_bold(m_ptr->fy, m_ptr->fx))
1755                 {
1756 #ifdef JP
1757                         strcat(desc,"(¾èÇÏÃæ)");
1758 #else
1759                         strcat(desc,"(riding)");
1760 #endif
1761                 }
1762
1763                 if ((mode & MD_IGNORE_HALLU) && (m_ptr->mflag2 & MFLAG2_CHAMELEON))
1764                 {
1765                         if (r_ptr->flags1 & RF1_UNIQUE)
1766                         {
1767 #ifdef JP
1768                                 strcat(desc,"(¥«¥á¥ì¥ª¥ó¤Î²¦)");
1769 #else
1770                                 strcat(desc,"(Chameleon Lord)");
1771 #endif
1772                         }
1773                         else
1774                         {
1775 #ifdef JP
1776                                 strcat(desc,"(¥«¥á¥ì¥ª¥ó)");
1777 #else
1778                                 strcat(desc,"(Chameleon)");
1779 #endif
1780                         }
1781                 }
1782
1783                 if ((mode & MD_IGNORE_HALLU) && m_ptr->ap_r_idx != m_ptr->r_idx)
1784                 {
1785                         strcat(desc, format("(%s)", r_name + r_info[m_ptr->r_idx].name));
1786                 }
1787
1788                 if (mode & MD_ASSUME_OUTSIDE)
1789                 {
1790 #ifdef JP
1791                         strcat(desc,"(ÂÔµ¡Ãæ)");
1792 #else
1793                         strcat(desc,"(waiting)");
1794 #endif
1795                 }
1796
1797                 /* Handle the Possessive as a special afterthought */
1798                 if (mode & MD_POSSESSIVE)
1799                 {
1800                         /* XXX Check for trailing "s" */
1801                         
1802                         /* Simply append "apostrophe" and "s" */
1803 #ifdef JP
1804                         (void)strcat(desc, "¤Î");
1805 #else
1806                         (void)strcat(desc, "'s");
1807 #endif
1808                 }
1809         }
1810 }
1811
1812
1813
1814
1815 /*
1816  * Learn about a monster (by "probing" it)
1817  *
1818  * Return the number of new flags learnt.  -Mogami-
1819  */
1820 int lore_do_probe(int r_idx)
1821 {
1822         monster_race *r_ptr = &r_info[r_idx];
1823         int i, n = 0;
1824         byte tmp_byte;
1825
1826         /* Maximal info about awareness */
1827         if (r_ptr->r_wake != MAX_UCHAR) n++;
1828         if (r_ptr->r_ignore != MAX_UCHAR) n++;
1829         r_ptr->r_wake = r_ptr->r_ignore = MAX_UCHAR;
1830
1831         /* Observe "maximal" attacks */
1832         for (i = 0; i < 4; i++)
1833         {
1834                 /* Examine "actual" blows */
1835                 if (r_ptr->blow[i].effect || r_ptr->blow[i].method)
1836                 {
1837                         /* Maximal observations */
1838                         if (r_ptr->r_blows[i] != MAX_UCHAR) n++;
1839                         r_ptr->r_blows[i] = MAX_UCHAR;
1840                 }
1841         }
1842
1843         /* Maximal drops */
1844         tmp_byte =
1845                 (((r_ptr->flags1 & RF1_DROP_4D2) ? 8 : 0) +
1846                  ((r_ptr->flags1 & RF1_DROP_3D2) ? 6 : 0) +
1847                  ((r_ptr->flags1 & RF1_DROP_2D2) ? 4 : 0) +
1848                  ((r_ptr->flags1 & RF1_DROP_1D2) ? 2 : 0) +
1849                  ((r_ptr->flags1 & RF1_DROP_90)  ? 1 : 0) +
1850                  ((r_ptr->flags1 & RF1_DROP_60)  ? 1 : 0));
1851
1852         /* Only "valid" drops */
1853         if (!(r_ptr->flags1 & RF1_ONLY_GOLD))
1854         {
1855                 if (r_ptr->r_drop_item != tmp_byte) n++;
1856                 r_ptr->r_drop_item = tmp_byte;
1857         }
1858         if (!(r_ptr->flags1 & RF1_ONLY_ITEM))
1859         {
1860                 if (r_ptr->r_drop_gold != tmp_byte) n++;
1861                 r_ptr->r_drop_gold = tmp_byte;
1862         }
1863
1864         /* Observe many spells */
1865         if (r_ptr->r_cast_spell != MAX_UCHAR) n++;
1866         r_ptr->r_cast_spell = MAX_UCHAR;
1867
1868         /* Count unknown flags */
1869         for (i = 0; i < 32; i++)
1870         {
1871                 if (!(r_ptr->r_flags1 & (1L << i)) &&
1872                     (r_ptr->flags1 & (1L << i))) n++;
1873                 if (!(r_ptr->r_flags2 & (1L << i)) &&
1874                     (r_ptr->flags2 & (1L << i))) n++;
1875                 if (!(r_ptr->r_flags3 & (1L << i)) &&
1876                     (r_ptr->flags3 & (1L << i))) n++;
1877                 if (!(r_ptr->r_flags4 & (1L << i)) &&
1878                     (r_ptr->flags4 & (1L << i))) n++;
1879                 if (!(r_ptr->r_flags5 & (1L << i)) &&
1880                     (r_ptr->flags5 & (1L << i))) n++;
1881                 if (!(r_ptr->r_flags6 & (1L << i)) &&
1882                     (r_ptr->flags6 & (1L << i))) n++;
1883                 if (!(r_ptr->r_flagsr & (1L << i)) &&
1884                     (r_ptr->flagsr & (1L << i))) n++;
1885
1886                 /* r_flags7 is actually unused */
1887 #if 0
1888                 if (!(r_ptr->r_flags7 & (1L << i)) &&
1889                     (r_ptr->flags7 & (1L << i))) n++;
1890 #endif
1891         }
1892
1893         /* Know all the flags */
1894         r_ptr->r_flags1 = r_ptr->flags1;
1895         r_ptr->r_flags2 = r_ptr->flags2;
1896         r_ptr->r_flags3 = r_ptr->flags3;
1897         r_ptr->r_flags4 = r_ptr->flags4;
1898         r_ptr->r_flags5 = r_ptr->flags5;
1899         r_ptr->r_flags6 = r_ptr->flags6;
1900         r_ptr->r_flagsr = r_ptr->flagsr;
1901
1902         /* r_flags7 is actually unused */
1903         /* r_ptr->r_flags7 = r_ptr->flags7; */
1904
1905         /* Know about evolution */
1906         if (!(r_ptr->r_xtra1 & MR1_SINKA)) n++;
1907         r_ptr->r_xtra1 |= MR1_SINKA;
1908
1909         /* Update monster recall window */
1910         if (p_ptr->monster_race_idx == r_idx)
1911         {
1912                 /* Window stuff */
1913                 p_ptr->window |= (PW_MONSTER);
1914         }
1915
1916         /* Return the number of new flags learnt */
1917         return n;
1918 }
1919
1920
1921 /*
1922  * Take note that the given monster just dropped some treasure
1923  *
1924  * Note that learning the "GOOD"/"GREAT" flags gives information
1925  * about the treasure (even when the monster is killed for the first
1926  * time, such as uniques, and the treasure has not been examined yet).
1927  *
1928  * This "indirect" method is used to prevent the player from learning
1929  * exactly how much treasure a monster can drop from observing only
1930  * a single example of a drop.  This method actually observes how much
1931  * gold and items are dropped, and remembers that information to be
1932  * described later by the monster recall code.
1933  */
1934 void lore_treasure(int m_idx, int num_item, int num_gold)
1935 {
1936         monster_type *m_ptr = &m_list[m_idx];
1937
1938         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1939
1940         /* If the monster doesn't have original appearance, don't note */
1941         if (!is_original_ap(m_ptr)) return;
1942
1943         /* Note the number of things dropped */
1944         if (num_item > r_ptr->r_drop_item) r_ptr->r_drop_item = num_item;
1945         if (num_gold > r_ptr->r_drop_gold) r_ptr->r_drop_gold = num_gold;
1946
1947         /* Hack -- memorize the good/great flags */
1948         if (r_ptr->flags1 & (RF1_DROP_GOOD)) r_ptr->r_flags1 |= (RF1_DROP_GOOD);
1949         if (r_ptr->flags1 & (RF1_DROP_GREAT)) r_ptr->r_flags1 |= (RF1_DROP_GREAT);
1950
1951         /* Update monster recall window */
1952         if (p_ptr->monster_race_idx == m_ptr->r_idx)
1953         {
1954                 /* Window stuff */
1955                 p_ptr->window |= (PW_MONSTER);
1956         }
1957 }
1958
1959
1960
1961 void sanity_blast(monster_type *m_ptr, bool necro)
1962 {
1963         bool happened = FALSE;
1964         int power = 100;
1965
1966         if (p_ptr->inside_battle || !character_dungeon) return;
1967
1968         if (!necro)
1969         {
1970                 char            m_name[80];
1971                 monster_race    *r_ptr = &r_info[m_ptr->ap_r_idx];
1972
1973                 power = r_ptr->level / 2;
1974
1975                 monster_desc(m_name, m_ptr, 0);
1976
1977                 if (!(r_ptr->flags1 & RF1_UNIQUE))
1978                 {
1979                         if (r_ptr->flags1 & RF1_FRIENDS)
1980                         power /= 2;
1981                 }
1982                 else power *= 2;
1983
1984                 if (!hack_mind)
1985                         return; /* No effect yet, just loaded... */
1986
1987                 if (!m_ptr->ml)
1988                         return; /* Cannot see it for some reason */
1989
1990                 if (!(r_ptr->flags2 & RF2_ELDRITCH_HORROR))
1991                         return; /* oops */
1992
1993
1994
1995                 if (is_pet(m_ptr))
1996                         return; /* Pet eldritch horrors are safe most of the time */
1997
1998                 if (randint1(100) > power) return;
1999
2000                 if (saving_throw(p_ptr->skill_sav - power))
2001                 {
2002                         return; /* Save, no adverse effects */
2003                 }
2004
2005                 if (p_ptr->image)
2006                 {
2007                         /* Something silly happens... */
2008 #ifdef JP
2009                         msg_format("%s%s¤Î´é¤ò¸«¤Æ¤·¤Þ¤Ã¤¿¡ª",
2010                                 funny_desc[randint0(MAX_SAN_FUNNY)], m_name);
2011 #else
2012                         msg_format("You behold the %s visage of %s!",
2013                                 funny_desc[randint0(MAX_SAN_FUNNY)], m_name);
2014 #endif
2015
2016
2017                         if (one_in_(3))
2018                         {
2019                                 msg_print(funny_comments[randint0(MAX_SAN_COMMENT)]);
2020                                 p_ptr->image = p_ptr->image + randint1(r_ptr->level);
2021                         }
2022
2023                         return; /* Never mind; we can't see it clearly enough */
2024                 }
2025
2026                 /* Something frightening happens... */
2027 #ifdef JP
2028                 msg_format("%s%s¤Î´é¤ò¸«¤Æ¤·¤Þ¤Ã¤¿¡ª",
2029                         horror_desc[randint0(MAX_SAN_HORROR)], m_name);
2030 #else
2031                 msg_format("You behold the %s visage of %s!",
2032                         horror_desc[randint0(MAX_SAN_HORROR)], m_name);
2033 #endif
2034
2035                 r_ptr->r_flags2 |= RF2_ELDRITCH_HORROR;
2036
2037                 /* Demon characters are unaffected */
2038                 if (prace_is_(RACE_IMP) || prace_is_(RACE_DEMON) || (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_DEMON)) return;
2039                 if (p_ptr->wizard) return;
2040
2041                 /* Undead characters are 50% likely to be unaffected */
2042                 if (prace_is_(RACE_SKELETON) || prace_is_(RACE_ZOMBIE)
2043                         || prace_is_(RACE_VAMPIRE) || prace_is_(RACE_SPECTRE) ||
2044                     (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_UNDEAD))
2045                 {
2046                         if (saving_throw(25 + p_ptr->lev)) return;
2047                 }
2048         }
2049         else
2050         {
2051 #ifdef JP
2052 msg_print("¥Í¥¯¥í¥Î¥ß¥³¥ó¤òÆɤó¤ÇÀµµ¤¤ò¼º¤Ã¤¿¡ª");
2053 #else
2054                 msg_print("Your sanity is shaken by reading the Necronomicon!");
2055 #endif
2056
2057         }
2058
2059         if (!saving_throw(p_ptr->skill_sav - power)) /* Mind blast */
2060         {
2061                 if (!p_ptr->resist_conf)
2062                 {
2063                         (void)set_confused(p_ptr->confused + randint0(4) + 4);
2064                 }
2065                 if (!p_ptr->resist_chaos && one_in_(3))
2066                 {
2067                         (void)set_image(p_ptr->image + randint0(250) + 150);
2068                 }
2069                 return;
2070         }
2071
2072         if (!saving_throw(p_ptr->skill_sav - power)) /* Lose int & wis */
2073         {
2074                 do_dec_stat(A_INT);
2075                 do_dec_stat(A_WIS);
2076                 return;
2077         }
2078
2079         if (!saving_throw(p_ptr->skill_sav - power)) /* Brain smash */
2080         {
2081                 if (!p_ptr->resist_conf)
2082                 {
2083                         (void)set_confused(p_ptr->confused + randint0(4) + 4);
2084                 }
2085                 if (!p_ptr->free_act)
2086                 {
2087                         (void)set_paralyzed(p_ptr->paralyzed + randint0(4) + 4);
2088                 }
2089                 while (randint0(100) > p_ptr->skill_sav)
2090                         (void)do_dec_stat(A_INT);
2091                 while (randint0(100) > p_ptr->skill_sav)
2092                         (void)do_dec_stat(A_WIS);
2093                 if (!p_ptr->resist_chaos)
2094                 {
2095                         (void)set_image(p_ptr->image + randint0(250) + 150);
2096                 }
2097                 return;
2098         }
2099
2100         if (!saving_throw(p_ptr->skill_sav - power)) /* Amnesia */
2101         {
2102
2103                 if (lose_all_info())
2104 #ifdef JP
2105 msg_print("¤¢¤Þ¤ê¤Î¶²ÉݤËÁ´¤Æ¤Î¤³¤È¤ò˺¤ì¤Æ¤·¤Þ¤Ã¤¿¡ª");
2106 #else
2107                         msg_print("You forget everything in your utmost terror!");
2108 #endif
2109
2110                 return;
2111         }
2112
2113         if (saving_throw(p_ptr->skill_sav - power))
2114         {
2115                 return;
2116         }
2117
2118         /* Else gain permanent insanity */
2119         if ((p_ptr->muta3 & MUT3_MORONIC) && /*(p_ptr->muta2 & MUT2_BERS_RAGE) &&*/
2120                 ((p_ptr->muta2 & MUT2_COWARDICE) || (p_ptr->resist_fear)) &&
2121                 ((p_ptr->muta2 & MUT2_HALLU) || (p_ptr->resist_chaos)))
2122         {
2123                 /* The poor bastard already has all possible insanities! */
2124                 return;
2125         }
2126
2127         while (!happened)
2128         {
2129                 switch (randint1(21))
2130                 {
2131                         case 1:
2132                                 if (!(p_ptr->muta3 & MUT3_MORONIC) && one_in_(5))
2133                                 {
2134                                         if ((p_ptr->stat_use[A_INT] < 4) && (p_ptr->stat_use[A_WIS] < 4))
2135                                         {
2136 #ifdef JP
2137 msg_print("¤¢¤Ê¤¿¤Ï´°àú¤ÊÇϼ¯¤Ë¤Ê¤Ã¤¿¤è¤¦¤Êµ¤¤¬¤·¤¿¡£¤·¤«¤·¤½¤ì¤Ï¸µ¡¹¤À¤Ã¤¿¡£");
2138 #else
2139                                                 msg_print("You turn into an utter moron!");
2140 #endif
2141                                         }
2142                                         else
2143                                         {
2144 #ifdef JP
2145 msg_print("¤¢¤Ê¤¿¤Ï´°àú¤ÊÇϼ¯¤Ë¤Ê¤Ã¤¿¡ª");
2146 #else
2147                                                 msg_print("You turn into an utter moron!");
2148 #endif
2149                                         }
2150
2151                                         if (p_ptr->muta3 & MUT3_HYPER_INT)
2152                                         {
2153 #ifdef JP
2154 msg_print("¤¢¤Ê¤¿¤ÎǾ¤ÏÀ¸ÂÎ¥³¥ó¥Ô¥å¡¼¥¿¤Ç¤Ï¤Ê¤¯¤Ê¤Ã¤¿¡£");
2155 #else
2156                                                 msg_print("Your brain is no longer a living computer.");
2157 #endif
2158
2159                                                 p_ptr->muta3 &= ~(MUT3_HYPER_INT);
2160                                         }
2161                                         p_ptr->muta3 |= MUT3_MORONIC;
2162                                         happened = TRUE;
2163                                 }
2164                                 break;
2165                         case 2:
2166                         case 3:
2167                         case 4:
2168                         case 5:
2169                         case 6:
2170                         case 7:
2171                         case 8:
2172                         case 9:
2173                         case 10:
2174                         case 11:
2175                                 if (!(p_ptr->muta2 & MUT2_COWARDICE) && !p_ptr->resist_fear)
2176                                 {
2177 #ifdef JP
2178 msg_print("¤¢¤Ê¤¿¤Ï¥Ñ¥é¥Î¥¤¥¢¤Ë¤Ê¤Ã¤¿¡ª");
2179 #else
2180                                         msg_print("You become paranoid!");
2181 #endif
2182
2183
2184                                         /* Duh, the following should never happen, but anyway... */
2185                                         if (p_ptr->muta3 & MUT3_FEARLESS)
2186                                         {
2187 #ifdef JP
2188 msg_print("¤¢¤Ê¤¿¤Ï¤â¤¦¶²¤ìÃΤ餺¤Ç¤Ï¤Ê¤¯¤Ê¤Ã¤¿¡£");
2189 #else
2190                                                 msg_print("You are no longer fearless.");
2191 #endif
2192
2193                                                 p_ptr->muta3 &= ~(MUT3_FEARLESS);
2194                                         }
2195
2196                                         p_ptr->muta2 |= MUT2_COWARDICE;
2197                                         happened = TRUE;
2198                                 }
2199                                 break;
2200                         case 12:
2201                         case 13:
2202                         case 14:
2203                         case 15:
2204                         case 16:
2205                         case 17:
2206                         case 18:
2207                         case 19:
2208                         case 20:
2209                         case 21:
2210                                 if (!(p_ptr->muta2 & MUT2_HALLU) && !p_ptr->resist_chaos)
2211                                 {
2212 #ifdef JP
2213 msg_print("¸¸³Ð¤ò¤Ò¤­µ¯¤³¤¹Àº¿ÀºøÍð¤Ë´Ù¤Ã¤¿¡ª");
2214 #else
2215                                         msg_print("You are afflicted by a hallucinatory insanity!");
2216 #endif
2217
2218                                         p_ptr->muta2 |= MUT2_HALLU;
2219                                         happened = TRUE;
2220                                 }
2221                                 break;
2222                         default:
2223                                 if (!(p_ptr->muta2 & MUT2_BERS_RAGE))
2224                                 {
2225 #ifdef JP
2226 msg_print("·ãÎõ¤Ê´¶¾ð¤Îȯºî¤Ë¤ª¤½¤ï¤ì¤ë¤è¤¦¤Ë¤Ê¤Ã¤¿¡ª");
2227 #else
2228                                         msg_print("You become subject to fits of berserk rage!");
2229 #endif
2230
2231                                         p_ptr->muta2 |= MUT2_BERS_RAGE;
2232                                         happened = TRUE;
2233                                 }
2234                                 break;
2235                 }
2236         }
2237
2238         p_ptr->update |= PU_BONUS;
2239         handle_stuff();
2240 }
2241
2242
2243 /*
2244  * This function updates the monster record of the given monster
2245  *
2246  * This involves extracting the distance to the player (if requested),
2247  * and then checking for visibility (natural, infravision, see-invis,
2248  * telepathy), updating the monster visibility flag, redrawing (or
2249  * erasing) the monster when its visibility changes, and taking note
2250  * of any interesting monster flags (cold-blooded, invisible, etc).
2251  *
2252  * Note the new "mflag" field which encodes several monster state flags,
2253  * including "view" for when the monster is currently in line of sight,
2254  * and "mark" for when the monster is currently visible via detection.
2255  *
2256  * The only monster fields that are changed here are "cdis" (the
2257  * distance from the player), "ml" (visible to the player), and
2258  * "mflag" (to maintain the "MFLAG_VIEW" flag).
2259  *
2260  * Note the special "update_monsters()" function which can be used to
2261  * call this function once for every monster.
2262  *
2263  * Note the "full" flag which requests that the "cdis" field be updated,
2264  * this is only needed when the monster (or the player) has moved.
2265  *
2266  * Every time a monster moves, we must call this function for that
2267  * monster, and update the distance, and the visibility.  Every time
2268  * the player moves, we must call this function for every monster, and
2269  * update the distance, and the visibility.  Whenever the player "state"
2270  * changes in certain ways ("blindness", "infravision", "telepathy",
2271  * and "see invisible"), we must call this function for every monster,
2272  * and update the visibility.
2273  *
2274  * Routines that change the "illumination" of a grid must also call this
2275  * function for any monster in that grid, since the "visibility" of some
2276  * monsters may be based on the illumination of their grid.
2277  *
2278  * Note that this function is called once per monster every time the
2279  * player moves.  When the player is running, this function is one
2280  * of the primary bottlenecks, along with "update_view()" and the
2281  * "process_monsters()" code, so efficiency is important.
2282  *
2283  * Note the optimized "inline" version of the "distance()" function.
2284  *
2285  * A monster is "visible" to the player if (1) it has been detected
2286  * by the player, (2) it is close to the player and the player has
2287  * telepathy, or (3) it is close to the player, and in line of sight
2288  * of the player, and it is "illuminated" by some combination of
2289  * infravision, torch light, or permanent light (invisible monsters
2290  * are only affected by "light" if the player can see invisible).
2291  *
2292  * Monsters which are not on the current panel may be "visible" to
2293  * the player, and their descriptions will include an "offscreen"
2294  * reference.  Currently, offscreen monsters cannot be targetted
2295  * or viewed directly, but old targets will remain set.  XXX XXX
2296  *
2297  * The player can choose to be disturbed by several things, including
2298  * "disturb_move" (monster which is viewable moves in some way), and
2299  * "disturb_near" (monster which is "easily" viewable moves in some
2300  * way).  Note that "moves" includes "appears" and "disappears".
2301  */
2302 void update_mon(int m_idx, bool full)
2303 {
2304         monster_type *m_ptr = &m_list[m_idx];
2305
2306         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2307
2308         bool do_disturb = disturb_move;
2309
2310         int d;
2311
2312         /* Current location */
2313         int fy = m_ptr->fy;
2314         int fx = m_ptr->fx;
2315
2316         /* Seen at all */
2317         bool flag = FALSE;
2318
2319         /* Seen by vision */
2320         bool easy = FALSE;
2321
2322         /* Non-Ninja player in the darkness */
2323         bool in_darkness = (d_info[dungeon_type].flags1 & DF1_DARKNESS) && !p_ptr->see_nocto;
2324
2325         /* Do disturb? */
2326         if (disturb_high)
2327         {
2328                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2329
2330                 if (ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)
2331                         do_disturb = TRUE;
2332         }
2333
2334         /* Compute distance */
2335         if (full)
2336         {
2337                 /* Distance components */
2338                 int dy = (py > fy) ? (py - fy) : (fy - py);
2339                 int dx = (px > fx) ? (px - fx) : (fx - px);
2340
2341                 /* Approximate distance */
2342                 d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
2343
2344                 /* Restrict distance */
2345                 if (d > 255) d = 255;
2346
2347                 if (!d) d = 1;
2348
2349                 /* Save the distance */
2350                 m_ptr->cdis = d;
2351         }
2352
2353         /* Extract distance */
2354         else
2355         {
2356                 /* Extract the distance */
2357                 d = m_ptr->cdis;
2358         }
2359
2360
2361         /* Detected */
2362         if (m_ptr->mflag2 & (MFLAG2_MARK)) flag = TRUE;
2363
2364
2365         /* Nearby */
2366         if (d <= (in_darkness ? MAX_SIGHT / 2 : MAX_SIGHT))
2367         {
2368                 if (!in_darkness || (d <= MAX_SIGHT / 4))
2369                 {
2370                         if (p_ptr->special_defense & KATA_MUSOU)
2371                         {
2372                                 /* Detectable */
2373                                 flag = TRUE;
2374
2375                                 if (is_original_ap(m_ptr))
2376                                 {
2377                                         /* Hack -- Memorize mental flags */
2378                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2379                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2380                                 }
2381                         }
2382
2383                         /* Basic telepathy */
2384                         else if (p_ptr->telepathy)
2385                         {
2386                                 /* Empty mind, no telepathy */
2387                                 if (r_ptr->flags2 & (RF2_EMPTY_MIND))
2388                                 {
2389                                         /* Memorize flags */
2390                                         if (is_original_ap(m_ptr)) r_ptr->r_flags2 |= (RF2_EMPTY_MIND);
2391                                 }
2392
2393                                 /* Weird mind, occasional telepathy */
2394                                 else if (r_ptr->flags2 & (RF2_WEIRD_MIND))
2395                                 {
2396                                         /* One in ten individuals are detectable */
2397                                         if ((m_idx % 10) == 5)
2398                                         {
2399                                                 /* Detectable */
2400                                                 flag = TRUE;
2401
2402                                                 if (is_original_ap(m_ptr))
2403                                                 {
2404                                                         /* Memorize flags */
2405                                                         r_ptr->r_flags2 |= (RF2_WEIRD_MIND);
2406
2407                                                         /* Hack -- Memorize mental flags */
2408                                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2409                                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2410                                                 }
2411                                         }
2412                                 }
2413
2414                                 /* Normal mind, allow telepathy */
2415                                 else
2416                                 {
2417                                         /* Detectable */
2418                                         flag = TRUE;
2419
2420                                         if (is_original_ap(m_ptr))
2421                                         {
2422                                                 /* Hack -- Memorize mental flags */
2423                                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2424                                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2425                                         }
2426                                 }
2427                         }
2428
2429                         /* Magical sensing */
2430                         if ((p_ptr->esp_animal) && (r_ptr->flags3 & (RF3_ANIMAL)))
2431                         {
2432                                 flag = TRUE;
2433                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_ANIMAL);
2434                         }
2435
2436                         /* Magical sensing */
2437                         if ((p_ptr->esp_undead) && (r_ptr->flags3 & (RF3_UNDEAD)))
2438                         {
2439                                 flag = TRUE;
2440                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_UNDEAD);
2441                         }
2442
2443                         /* Magical sensing */
2444                         if ((p_ptr->esp_demon) && (r_ptr->flags3 & (RF3_DEMON)))
2445                         {
2446                                 flag = TRUE;
2447                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_DEMON);
2448                         }
2449
2450                         /* Magical sensing */
2451                         if ((p_ptr->esp_orc) && (r_ptr->flags3 & (RF3_ORC)))
2452                         {
2453                                 flag = TRUE;
2454                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_ORC);
2455                         }
2456
2457                         /* Magical sensing */
2458                         if ((p_ptr->esp_troll) && (r_ptr->flags3 & (RF3_TROLL)))
2459                         {
2460                                 flag = TRUE;
2461                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_TROLL);
2462                         }
2463
2464                         /* Magical sensing */
2465                         if ((p_ptr->esp_giant) && (r_ptr->flags3 & (RF3_GIANT)))
2466                         {
2467                                 flag = TRUE;
2468                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_GIANT);
2469                         }
2470
2471                         /* Magical sensing */
2472                         if ((p_ptr->esp_dragon) && (r_ptr->flags3 & (RF3_DRAGON)))
2473                         {
2474                                 flag = TRUE;
2475                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_DRAGON);
2476                         }
2477
2478                         /* Magical sensing */
2479                         if ((p_ptr->esp_human) && (r_ptr->flags2 & (RF2_HUMAN)))
2480                         {
2481                                 flag = TRUE;
2482                                 if (is_original_ap(m_ptr)) r_ptr->r_flags2 |= (RF2_HUMAN);
2483                         }
2484
2485                         /* Magical sensing */
2486                         if ((p_ptr->esp_evil) && (r_ptr->flags3 & (RF3_EVIL)))
2487                         {
2488                                 flag = TRUE;
2489                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_EVIL);
2490                         }
2491
2492                         /* Magical sensing */
2493                         if ((p_ptr->esp_good) && (r_ptr->flags3 & (RF3_GOOD)))
2494                         {
2495                                 flag = TRUE;
2496                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_GOOD);
2497                         }
2498
2499                         /* Magical sensing */
2500                         if ((p_ptr->esp_nonliving) &&
2501                             ((r_ptr->flags3 & (RF3_DEMON | RF3_UNDEAD | RF3_NONLIVING)) == RF3_NONLIVING))
2502                         {
2503                                 flag = TRUE;
2504                                 if (is_original_ap(m_ptr)) r_ptr->r_flags3 |= (RF3_NONLIVING);
2505                         }
2506
2507                         /* Magical sensing */
2508                         if ((p_ptr->esp_unique) && (r_ptr->flags1 & (RF1_UNIQUE)))
2509                         {
2510                                 flag = TRUE;
2511                                 if (is_original_ap(m_ptr)) r_ptr->r_flags1 |= (RF1_UNIQUE);
2512                         }
2513                 }
2514
2515                 /* Normal line of sight, and not blind */
2516                 if (player_has_los_bold(fy, fx) && !p_ptr->blind)
2517                 {
2518                         bool do_invisible = FALSE;
2519                         bool do_cold_blood = FALSE;
2520
2521                         /* Use "infravision" */
2522                         if (d <= p_ptr->see_infra)
2523                         {
2524                                 /* Handle "cold blooded" monsters */
2525                                 if ((r_ptr->flags2 & (RF2_COLD_BLOOD | RF2_AURA_FIRE)) == RF2_COLD_BLOOD)
2526                                 {
2527                                         /* Take note */
2528                                         do_cold_blood = TRUE;
2529                                 }
2530
2531                                 /* Handle "warm blooded" monsters */
2532                                 else
2533                                 {
2534                                         /* Easy to see */
2535                                         easy = flag = TRUE;
2536                                 }
2537                         }
2538
2539                         /* Use "illumination" */
2540                         if (player_can_see_bold(fy, fx))
2541                         {
2542                                 /* Handle "invisible" monsters */
2543                                 if (r_ptr->flags2 & (RF2_INVISIBLE))
2544                                 {
2545                                         /* Take note */
2546                                         do_invisible = TRUE;
2547
2548                                         /* See invisible */
2549                                         if (p_ptr->see_inv)
2550                                         {
2551                                                 /* Easy to see */
2552                                                 easy = flag = TRUE;
2553                                         }
2554                                 }
2555
2556                                 /* Handle "normal" monsters */
2557                                 else
2558                                 {
2559                                         /* Easy to see */
2560                                         easy = flag = TRUE;
2561                                 }
2562                         }
2563
2564                         /* Visible */
2565                         if (flag)
2566                         {
2567                                 if (is_original_ap(m_ptr))
2568                                 {
2569                                         /* Memorize flags */
2570                                         if (do_invisible) r_ptr->r_flags2 |= (RF2_INVISIBLE);
2571                                         if (do_cold_blood) r_ptr->r_flags2 |= (RF2_COLD_BLOOD);
2572                                 }
2573                         }
2574                 }
2575         }
2576
2577
2578         /* The monster is now visible */
2579         if (flag)
2580         {
2581                 /* It was previously unseen */
2582                 if (!m_ptr->ml)
2583                 {
2584                         /* Mark as visible */
2585                         m_ptr->ml = TRUE;
2586
2587                         /* Draw the monster */
2588                         lite_spot(fy, fx);
2589
2590                         /* Update health bar as needed */
2591                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2592                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2593
2594                         /* Hack -- Count "fresh" sightings */
2595                         if ((m_ptr->ap_r_idx == MON_KAGE) && (r_info[MON_KAGE].r_sights < MAX_SHORT))
2596                                 r_info[MON_KAGE].r_sights++;
2597                         else if (m_ptr->ap_r_idx == m_ptr->r_idx && 
2598                                  r_ptr->r_sights < MAX_SHORT) r_ptr->r_sights++;
2599
2600                         /* Eldritch Horror */
2601                         if (r_ptr->flags2 & RF2_ELDRITCH_HORROR)
2602                         {
2603                                 sanity_blast(m_ptr, FALSE);
2604                         }
2605
2606                         /* Disturb on appearance */
2607                         if (disturb_near && (projectable(m_ptr->fy, m_ptr->fx, py, px) && projectable(py, px, m_ptr->fy, m_ptr->fx)))
2608                         {
2609                                 if (disturb_pets || is_hostile(m_ptr))
2610                                         disturb(1, 0);
2611                         }
2612                 }
2613         }
2614
2615         /* The monster is not visible */
2616         else
2617         {
2618                 /* It was previously seen */
2619                 if (m_ptr->ml)
2620                 {
2621                         /* Mark as not visible */
2622                         m_ptr->ml = FALSE;
2623
2624                         /* Erase the monster */
2625                         lite_spot(fy, fx);
2626
2627                         /* Update health bar as needed */
2628                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2629                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2630
2631                         /* Disturb on disappearance */
2632                         if (do_disturb)
2633                         {
2634                                 if (disturb_pets || is_hostile(m_ptr))
2635                                         disturb(1, 0);
2636                         }
2637                 }
2638         }
2639
2640
2641         /* The monster is now easily visible */
2642         if (easy)
2643         {
2644                 /* Change */
2645                 if (!(m_ptr->mflag & (MFLAG_VIEW)))
2646                 {
2647                         /* Mark as easily visible */
2648                         m_ptr->mflag |= (MFLAG_VIEW);
2649
2650                         /* Disturb on appearance */
2651                         if (do_disturb)
2652                         {
2653                                 if (disturb_pets || is_hostile(m_ptr))
2654                                         disturb(1, 0);
2655                         }
2656                 }
2657         }
2658
2659         /* The monster is not easily visible */
2660         else
2661         {
2662                 /* Change */
2663                 if (m_ptr->mflag & (MFLAG_VIEW))
2664                 {
2665                         /* Mark as not easily visible */
2666                         m_ptr->mflag &= ~(MFLAG_VIEW);
2667
2668                         /* Disturb on disappearance */
2669                         if (do_disturb)
2670                         {
2671                                 if (disturb_pets || is_hostile(m_ptr))
2672                                         disturb(1, 0);
2673                         }
2674                 }
2675         }
2676 }
2677
2678
2679 /*
2680  * This function simply updates all the (non-dead) monsters (see above).
2681  */
2682 void update_monsters(bool full)
2683 {
2684         int i;
2685
2686         /* Update each (live) monster */
2687         for (i = 1; i < m_max; i++)
2688         {
2689                 monster_type *m_ptr = &m_list[i];
2690
2691                 /* Skip dead monsters */
2692                 if (!m_ptr->r_idx) continue;
2693
2694                 /* Update the monster */
2695                 update_mon(i, full);
2696         }
2697 }
2698
2699
2700 /*
2701  * Hack -- the index of the summoning monster
2702  */
2703 static bool monster_hook_chameleon_lord(int r_idx)
2704 {
2705         monster_race *r_ptr = &r_info[r_idx];
2706         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2707         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2708
2709         if (!(r_ptr->flags1 & (RF1_UNIQUE))) return FALSE;
2710         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2711
2712         if (ABS(r_ptr->level - r_info[MON_CHAMELEON_K].level) > 5) return FALSE;
2713
2714         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))
2715                 return FALSE;
2716
2717         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr)) return FALSE;
2718
2719         /* Not born */
2720         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2721         {
2722                 if ((m_ptr->sub_align & SUB_ALIGN_EVIL) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
2723                 if ((m_ptr->sub_align & SUB_ALIGN_GOOD) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
2724         }
2725
2726         /* Born now */
2727         else if (summon_specific_who > 0)
2728         {
2729                 monster_type *sm_ptr = &m_list[summon_specific_who];
2730
2731                 if ((sm_ptr->sub_align & SUB_ALIGN_EVIL) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
2732                 if ((sm_ptr->sub_align & SUB_ALIGN_GOOD) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
2733         }
2734
2735         return TRUE;
2736 }
2737
2738 static bool monster_hook_chameleon(int r_idx)
2739 {
2740         monster_race *r_ptr = &r_info[r_idx];
2741         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2742         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2743
2744         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2745         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2746         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2747         
2748         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))
2749                 return FALSE;
2750
2751         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr)) return FALSE;
2752
2753         /* Not born */
2754         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2755         {
2756                 if ((old_r_ptr->flags3 & RF3_GOOD) && !(r_ptr->flags3 & RF3_GOOD)) return FALSE;
2757                 if ((old_r_ptr->flags3 & RF3_EVIL) && !(r_ptr->flags3 & RF3_EVIL)) return FALSE;
2758                 if (!(old_r_ptr->flags3 & (RF3_GOOD | RF3_EVIL)) && (r_ptr->flags3 & (RF3_GOOD | RF3_EVIL))) return FALSE;
2759         }
2760
2761         /* Born now */
2762         else if (summon_specific_who > 0)
2763         {
2764                 monster_type *sm_ptr = &m_list[summon_specific_who];
2765
2766                 if ((sm_ptr->sub_align & SUB_ALIGN_EVIL) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
2767                 if ((sm_ptr->sub_align & SUB_ALIGN_GOOD) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
2768         }
2769
2770         return (*(get_monster_hook()))(r_idx);
2771 }
2772
2773
2774 void choose_new_monster(int m_idx, bool born, int r_idx)
2775 {
2776         int oldmaxhp;
2777         monster_type *m_ptr = &m_list[m_idx];
2778         monster_race *r_ptr;
2779         char old_m_name[80];
2780         bool old_unique = FALSE;
2781
2782         if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2783                 old_unique = TRUE;
2784         if (old_unique && (r_idx == MON_CHAMELEON)) r_idx = MON_CHAMELEON_K;
2785         r_ptr = &r_info[r_idx];
2786
2787         monster_desc(old_m_name, m_ptr, 0);
2788
2789         if (!r_idx)
2790         {
2791                 int level;
2792
2793                 chameleon_change_m_idx = m_idx;
2794                 if (old_unique)
2795                         get_mon_num_prep(monster_hook_chameleon_lord, NULL);
2796                 else
2797                         get_mon_num_prep(monster_hook_chameleon, NULL);
2798
2799                 if (old_unique)
2800                         level = r_info[MON_CHAMELEON_K].level;
2801                 else if (!dun_level)
2802                         level = wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].level;
2803                 else
2804                         level = dun_level;
2805
2806                 if (d_info[dungeon_type].flags1 & DF1_CHAMELEON) level+= 2+randint1(3);
2807
2808                 r_idx = get_mon_num(level);
2809                 r_ptr = &r_info[r_idx];
2810
2811                 chameleon_change_m_idx = 0;
2812
2813                 /* Paranoia */
2814                 if (!r_idx) return;
2815         }
2816
2817         m_ptr->r_idx = r_idx;
2818         m_ptr->ap_r_idx = r_idx;
2819         update_mon(m_idx, FALSE);
2820         lite_spot(m_ptr->fy, m_ptr->fx);
2821         if (born)
2822         {
2823                 /* Sub-alignment of a chameleon */
2824                 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
2825                 {
2826                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2827                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2828                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2829                 }
2830                 return;
2831         }
2832
2833         if (m_idx == p_ptr->riding)
2834         {
2835                 char m_name[80];
2836                 monster_desc(m_name, m_ptr, 0);
2837 #ifdef JP
2838                 msg_format("ÆÍÁ³%s¤¬ÊѿȤ·¤¿¡£", old_m_name);
2839 #else
2840                 msg_format("Suddenly, %s transforms!", old_m_name);
2841 #endif
2842                 if (!(r_ptr->flags7 & RF7_RIDING))
2843 #ifdef JP
2844                         if (rakuba(0, TRUE)) msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2845 #else
2846                         if (rakuba(0, TRUE)) msg_format("You have fallen from %s.", m_name);
2847 #endif
2848         }
2849
2850         /* Extract the monster base speed */
2851         m_ptr->mspeed = get_mspeed(r_ptr);
2852
2853         oldmaxhp = m_ptr->max_maxhp;
2854         /* Assign maximal hitpoints */
2855         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2856         {
2857                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2858         }
2859         else
2860         {
2861                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2862         }
2863         m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
2864         m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
2865 }
2866
2867
2868 /*
2869  *  Hook for Tanuki
2870  */
2871 static bool monster_hook_tanuki(int r_idx)
2872 {
2873         monster_race *r_ptr = &r_info[r_idx];
2874
2875         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2876         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2877         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2878         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2879         
2880         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))
2881                 return FALSE;
2882
2883         return (*(get_monster_hook()))(r_idx);
2884 }
2885
2886
2887 /*
2888  *  Set initial racial appearance of a monster
2889  */
2890 static int initial_r_appearance(int r_idx)
2891 {
2892         int attempts = 1000;
2893
2894         int ap_r_idx;
2895         int min = MIN(base_level-5, 50);
2896
2897         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2898                 return r_idx;
2899
2900         get_mon_num_prep(monster_hook_tanuki, NULL);
2901
2902         while (--attempts)
2903         {
2904                 ap_r_idx = get_mon_num(base_level + 10);
2905                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2906         }
2907
2908         return r_idx;
2909 }
2910
2911
2912 /*
2913  * Get initial monster speed
2914  */
2915 byte get_mspeed(monster_race *r_ptr)
2916 {
2917         /* Extract the monster base speed */
2918         int mspeed = r_ptr->speed;
2919
2920         /* Hack -- small racial variety */
2921         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2922         {
2923                 /* Allow some small variation per monster */
2924                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2925                 if (i) mspeed += rand_spread(0, i);
2926         }
2927
2928         if (mspeed > 199) mspeed = 199;
2929
2930         return (byte)mspeed;
2931 }
2932
2933
2934 /*
2935  * Attempt to place a monster of the given race at the given location.
2936  *
2937  * To give the player a sporting chance, any monster that appears in
2938  * line-of-sight and is extremely dangerous can be marked as
2939  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2940  * which often (but not always) lets the player move before they do.
2941  *
2942  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2943  *
2944  * XXX XXX XXX Use special "here" and "dead" flags for unique monsters,
2945  * remove old "cur_num" and "max_num" fields.
2946  *
2947  * XXX XXX XXX Actually, do something similar for artifacts, to simplify
2948  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2949  *
2950  * This is the only function which may place a monster in the dungeon,
2951  * except for the savefile loading code.
2952  */
2953 bool place_monster_one(int who, int y, int x, int r_idx, u32b mode)
2954 {
2955         cave_type               *c_ptr;
2956
2957         monster_type    *m_ptr;
2958
2959         monster_race    *r_ptr = &r_info[r_idx];
2960
2961         cptr            name = (r_name + r_ptr->name);
2962
2963         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
2964         if(p_ptr->wild_mode) return FALSE;
2965
2966         /* Verify location */
2967         if (!in_bounds(y, x)) return (FALSE);
2968
2969         /* Require empty space (if not ghostly) */
2970         if (!(!dun_level && (cave[y][x].feat == FEAT_MOUNTAIN) && ((r_ptr->flags8 & RF8_WILD_MOUNTAIN) || (r_ptr->flags7 & RF7_CAN_FLY))) &&
2971             !(cave_empty_bold2(y, x) || (mode & PM_IGNORE_TERRAIN)) &&
2972             !((r_ptr->flags2 & RF2_PASS_WALL) &&
2973               !(cave_perma_bold(y, x) || cave[y][x].m_idx ||
2974             player_bold(y, x)))) return (FALSE);
2975
2976         /* Paranoia */
2977         if (!r_idx) return (FALSE);
2978
2979         /* Paranoia */
2980         if (!r_ptr->name) return (FALSE);
2981
2982         /* Nor on the Pattern */
2983         if ((cave[y][x].feat >= FEAT_PATTERN_START)
2984          && (cave[y][x].feat <= FEAT_PATTERN_XTRA2))
2985                 return (FALSE);
2986
2987         if (!(mode & PM_IGNORE_TERRAIN) &&
2988             !monster_can_cross_terrain(cave[y][x].feat, r_ptr))
2989         {
2990                 return FALSE;
2991         }
2992
2993         if (!p_ptr->inside_battle)
2994         {
2995                 /* Hack -- "unique" monsters must be "unique" */
2996                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
2997                      (r_ptr->flags7 & (RF7_UNIQUE_7))) &&
2998                     (r_ptr->cur_num >= r_ptr->max_num))
2999                 {
3000                         /* Cannot create */
3001                         return (FALSE);
3002                 }
3003
3004                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
3005                     (r_ptr->cur_num >= 1))
3006                 {
3007                         return (FALSE);
3008                 }
3009
3010                 if (r_idx == MON_BANORLUPART)
3011                 {
3012                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
3013                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
3014                 }
3015
3016                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
3017                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (dun_level < r_ptr->level) &&
3018                     (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
3019                 {
3020                         /* Cannot create */
3021                         return (FALSE);
3022                 }
3023         }
3024
3025         if (quest_number(dun_level))
3026         {
3027                 int hoge = quest_number(dun_level);
3028                 if ((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
3029                 {
3030                         if(r_idx == quest[hoge].r_idx)
3031                         {
3032                                 int number_mon, i2, j2;
3033                                 number_mon = 0;
3034
3035                                 /* Count all quest monsters */
3036                                 for (i2 = 0; i2 < cur_wid; ++i2)
3037                                         for (j2 = 0; j2 < cur_hgt; j2++)
3038                                                 if (cave[j2][i2].m_idx > 0)
3039                                                         if (m_list[cave[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
3040                                                                 number_mon++;
3041                                 if(number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
3042                                         return FALSE;
3043                         }
3044                 }
3045         }
3046
3047         /* Access the location */
3048         c_ptr = &cave[y][x];
3049
3050         if (is_glyph_grid(c_ptr))
3051         {
3052                 if (randint1(BREAK_GLYPH) < (r_ptr->level+20))
3053                 {
3054                         /* Describe observable breakage */
3055                         if (c_ptr->info & CAVE_MARK)
3056                         {
3057 #ifdef JP
3058 msg_print("¼é¤ê¤Î¥ë¡¼¥ó¤¬²õ¤ì¤¿¡ª");
3059 #else
3060                                 msg_print("The rune of protection is broken!");
3061 #endif
3062
3063                         }
3064
3065                         /* Forget the rune */
3066                         c_ptr->info &= ~(CAVE_MARK);
3067
3068                         /* Break the rune */
3069                         c_ptr->info &= ~(CAVE_OBJECT);
3070                         c_ptr->mimic = 0;
3071
3072                         /* Notice */
3073                         note_spot(y, x);
3074                 }
3075                 else return FALSE;
3076         }
3077
3078         /* Powerful monster */
3079         if (r_ptr->level > dun_level)
3080         {
3081                 /* Unique monsters */
3082                 if (r_ptr->flags1 & (RF1_UNIQUE))
3083                 {
3084                         /* Message for cheaters */
3085 #ifdef JP
3086                         if (cheat_hear) msg_format("¿¼ÁؤΥæ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3087 #else
3088                         if (cheat_hear) msg_format("Deep Unique (%s).", name);
3089 #endif
3090
3091
3092                         /* Boost rating by twice delta-depth */
3093                         rating += (r_ptr->level - dun_level) * 2;
3094                 }
3095
3096                 /* Normal monsters */
3097                 else
3098                 {
3099                         /* Message for cheaters */
3100 #ifdef JP
3101                         if (cheat_hear) msg_format("¿¼ÁؤΥâ¥ó¥¹¥¿¡¼ (%s)¡£", name);
3102 #else
3103                         if (cheat_hear) msg_format("Deep Monster (%s).", name);
3104 #endif
3105
3106
3107                         /* Boost rating by delta-depth */
3108                         rating += (r_ptr->level - dun_level);
3109                 }
3110         }
3111
3112         /* Note the monster */
3113         else if (r_ptr->flags1 & (RF1_UNIQUE))
3114         {
3115                 /* Unique monsters induce message */
3116 #ifdef JP
3117                 if (cheat_hear) msg_format("¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3118 #else
3119                 if (cheat_hear) msg_format("Unique (%s).", name);
3120 #endif
3121
3122         }
3123
3124         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7) || (r_ptr->level < 10)) is_kage = FALSE;
3125
3126         /* Make a new monster */
3127         c_ptr->m_idx = m_pop();
3128         hack_m_idx_ii = c_ptr->m_idx;
3129
3130         /* Mega-Hack -- catch "failure" */
3131         if (!c_ptr->m_idx) return (FALSE);
3132
3133
3134         /* Get a new monster record */
3135         m_ptr = &m_list[c_ptr->m_idx];
3136
3137         /* Save the race */
3138         m_ptr->r_idx = r_idx;
3139         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
3140
3141         /* Sub-alignment of a monster */
3142         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
3143                 m_ptr->sub_align = m_list[who].sub_align;
3144         else
3145         {
3146                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3147                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
3148                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
3149         }
3150
3151         /* Place the monster at the location */
3152         m_ptr->fy = y;
3153         m_ptr->fx = x;
3154
3155
3156         /* No "damage" yet */
3157         m_ptr->stunned = 0;
3158         m_ptr->confused = 0;
3159         m_ptr->monfear = 0;
3160
3161         /* Unknown distance */
3162         m_ptr->cdis = 0;
3163
3164         reset_target(m_ptr);
3165
3166         m_ptr->nickname = 0;
3167
3168         m_ptr->exp = 0;
3169
3170         /* No flags */
3171         m_ptr->mflag = 0;
3172         m_ptr->mflag2 = 0;
3173
3174         if (r_ptr->flags7 & RF7_CHAMELEON)
3175         {
3176                 choose_new_monster(c_ptr->m_idx, TRUE, 0);
3177                 r_ptr = &r_info[m_ptr->r_idx];
3178                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
3179                 rating++;
3180
3181                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
3182                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
3183                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3184         }
3185         else if (is_kage)
3186         {
3187                 m_ptr->ap_r_idx = MON_KAGE;
3188                 m_ptr->mflag2 |= MFLAG2_KAGE;
3189         }
3190
3191         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
3192
3193         /* Not visible */
3194         m_ptr->ml = FALSE;
3195
3196         /* Pet? */
3197         if (mode & PM_FORCE_PET)
3198         {
3199                 set_pet(m_ptr);
3200         }
3201         /* Friendly? */
3202         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
3203                  (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(who))
3204         {
3205                 if (!(p_ptr->align >= 0 && (r_ptr->flags3 & RF3_EVIL)) &&
3206                     !(p_ptr->align < 0 && (r_ptr->flags3 & RF3_GOOD)))
3207                 {
3208                         set_friendly(m_ptr);
3209                 }
3210         }
3211
3212         /* Assume no sleeping */
3213         m_ptr->csleep = 0;
3214
3215         /* Enforce sleeping if needed */
3216         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
3217         {
3218                 int val = r_ptr->sleep;
3219                 m_ptr->csleep = ((val * 2) + randint1(val * 10));
3220         }
3221
3222         /* Assign maximal hitpoints */
3223         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
3224         {
3225                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
3226         }
3227         else
3228         {
3229                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
3230         }
3231
3232         /* Monsters have double hitpoints in Nightmare mode */
3233         if (ironman_nightmare)
3234         {
3235                 u32b hp = m_ptr->max_maxhp * 2L;
3236
3237                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
3238         }
3239
3240         m_ptr->maxhp = m_ptr->max_maxhp;
3241
3242         /* And start out fully healthy */
3243         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
3244                 m_ptr->hp = m_ptr->maxhp / 2;
3245         else m_ptr->hp = m_ptr->maxhp;
3246
3247
3248         /* Extract the monster base speed */
3249         m_ptr->mspeed = get_mspeed(r_ptr);
3250
3251         if (mode & PM_HASTE) m_ptr->fast = 100;
3252
3253         /* Give a random starting energy */
3254         if (!ironman_nightmare)
3255         {
3256                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
3257         }
3258         else
3259         {
3260                 /* Nightmare monsters are more prepared */
3261                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
3262         }
3263
3264         /* Force monster to wait for player, unless in Nightmare mode */
3265         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
3266         {
3267                 /* Monster is still being nice */
3268                 m_ptr->mflag |= (MFLAG_NICE);
3269
3270                 /* Must repair monsters */
3271                 repair_monsters = TRUE;
3272         }
3273
3274         /* Hack -- see "process_monsters()" */
3275         if (c_ptr->m_idx < hack_m_idx)
3276         {
3277                 /* Monster is still being born */
3278                 m_ptr->mflag |= (MFLAG_BORN);
3279         }
3280
3281
3282         if (r_ptr->flags7 & RF7_SELF_LD_MASK)
3283                 p_ptr->update |= (PU_MON_LITE);
3284         else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !m_ptr->csleep)
3285                 p_ptr->update |= (PU_MON_LITE);
3286
3287         /* Update the monster */
3288         update_mon(c_ptr->m_idx, TRUE);
3289
3290
3291         /* Count the monsters on the level */
3292         real_r_ptr(m_ptr)->cur_num++;
3293
3294         /*
3295          * Memorize location of the unique monster in saved floors.
3296          * A unique monster move from old saved floor.
3297          */
3298         if (character_dungeon &&
3299             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)))
3300                 real_r_ptr(m_ptr)->floor_id = p_ptr->floor_id;
3301
3302         /* Hack -- Count the number of "reproducers" */
3303         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
3304
3305
3306         /* Hack -- Notice new multi-hued monsters */
3307         {
3308                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
3309                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
3310                         shimmer_monsters = TRUE;
3311         }
3312
3313         if (p_ptr->warning && character_dungeon)
3314         {
3315                 if (r_ptr->flags1 & RF1_UNIQUE)
3316                 {
3317                         cptr color;
3318                         object_type *o_ptr;
3319                         char o_name[MAX_NLEN];
3320
3321                         if (r_ptr->level > p_ptr->lev + 30)
3322 #ifdef JP
3323                                 color = "¹õ¤¯";
3324 #else
3325                                 color = "black";
3326 #endif
3327                         else if (r_ptr->level > p_ptr->lev + 15)
3328 #ifdef JP
3329                                 color = "»ç¿§¤Ë";
3330 #else
3331                                 color = "purple";
3332 #endif
3333                         else if (r_ptr->level > p_ptr->lev + 5)
3334 #ifdef JP
3335                                 color = "¥ë¥Ó¡¼¿§¤Ë";
3336 #else
3337                                 color = "deep red";
3338 #endif
3339                         else if (r_ptr->level > p_ptr->lev - 5)
3340 #ifdef JP
3341                                 color = "ÀÖ¤¯";
3342 #else
3343                                 color = "red";
3344 #endif
3345                         else if (r_ptr->level > p_ptr->lev - 15)
3346 #ifdef JP
3347                                 color = "¥Ô¥ó¥¯¿§¤Ë";
3348 #else
3349                                 color = "pink";
3350 #endif
3351                         else
3352 #ifdef JP
3353                                 color = "Çò¤¯";
3354 #else
3355                                 color = "white";
3356 #endif
3357
3358                         o_ptr = choose_warning_item();
3359                         object_desc(o_name, o_ptr, FALSE, 0);
3360 #ifdef JP
3361                         msg_format("%s¤Ï%s¸÷¤Ã¤¿¡£",o_name, color);
3362 #else
3363                         msg_format("%s glows %s.",o_name, color);
3364 #endif
3365                 }
3366         }
3367
3368         if (is_explosive_rune_grid(c_ptr))
3369         {
3370                 /* Break the ward */
3371                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3372                 {
3373                         /* Describe observable breakage */
3374                         if (c_ptr->info & CAVE_MARK)
3375                         {
3376 #ifdef JP
3377 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3378 #else
3379                                 msg_print("The rune explodes!");
3380 #endif
3381
3382                                 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);
3383                         }
3384                 }
3385                 else
3386                 {
3387 #ifdef JP
3388 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3389 #else
3390                         msg_print("An explosive rune was disarmed.");
3391 #endif
3392                 }
3393
3394                 /* Forget the rune */
3395                 c_ptr->info &= ~(CAVE_MARK);
3396
3397                 /* Break the rune */
3398                 c_ptr->info &= ~(CAVE_OBJECT);
3399                 c_ptr->mimic = 0;
3400
3401                 note_spot(y, x);
3402                 lite_spot(y, x);
3403         }
3404
3405         /* Success */
3406         return (TRUE);
3407 }
3408
3409
3410 /*
3411  *  improved version of scatter() for place monster
3412  */
3413
3414 #define MON_SCAT_MAXD 10
3415
3416 static bool mon_scatter(int *yp, int *xp, int y, int x, int max_dist)
3417 {
3418         int place_x[MON_SCAT_MAXD];
3419         int place_y[MON_SCAT_MAXD];
3420         int num[MON_SCAT_MAXD];
3421         int i;
3422         int nx, ny;
3423
3424         if (max_dist >= MON_SCAT_MAXD)
3425                 return FALSE;
3426
3427         for (i = 0; i < MON_SCAT_MAXD; i++)
3428                 num[i] = 0;
3429
3430         for (nx = x - max_dist; nx <= x + max_dist; nx++)
3431                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
3432                 {
3433                         /* Ignore annoying locations */
3434                         if (!in_bounds(ny, nx)) continue;
3435
3436                         /* Require "line of sight" */
3437                         if (!los(y, x, ny, nx)) continue;
3438
3439                         /* Walls and Monsters block flow */
3440                         if (!cave_empty_bold2(ny, nx)) continue;
3441                         if (cave[ny][nx].m_idx) continue;
3442                         if (player_bold(ny, nx)) continue;
3443
3444                         /* ... nor on the Pattern */
3445                         if ((cave[ny][nx].feat >= FEAT_PATTERN_START) &&
3446                             (cave[ny][nx].feat <= FEAT_PATTERN_XTRA2))
3447                                 continue;
3448
3449                         i = distance(y, x, ny, nx);
3450
3451                         if (i > max_dist)
3452                                 continue;
3453
3454                         num[i]++;
3455
3456                         /* random swap */
3457                         if(one_in_(num[i]))
3458                         {
3459                                 place_x[i] = nx;
3460                                 place_y[i] = ny;
3461                         }
3462                 }
3463
3464         i = 0;
3465         while (i < MON_SCAT_MAXD && 0 == num[i])
3466                 i++;
3467         if (i >= MON_SCAT_MAXD)
3468                 return FALSE;
3469
3470         *xp = place_x[i];
3471         *yp = place_y[i];
3472
3473         return TRUE;
3474 }
3475
3476
3477 /*
3478  * Maximum size of a group of monsters
3479  */
3480 #define GROUP_MAX       32
3481
3482
3483 /*
3484  * Attempt to place a "group" of monsters around the given location
3485  */
3486 static bool place_monster_group(int who, int y, int x, int r_idx, u32b mode)
3487 {
3488         monster_race *r_ptr = &r_info[r_idx];
3489
3490         int old, n, i;
3491         int total = 0, extra = 0;
3492
3493         int hack_n = 0;
3494
3495         byte hack_y[GROUP_MAX];
3496         byte hack_x[GROUP_MAX];
3497
3498
3499         /* Pick a group size */
3500         total = randint1(10);
3501
3502         /* Hard monsters, small groups */
3503         if (r_ptr->level > dun_level)
3504         {
3505                 extra = r_ptr->level - dun_level;
3506                 extra = 0 - randint1(extra);
3507         }
3508
3509         /* Easy monsters, large groups */
3510         else if (r_ptr->level < dun_level)
3511         {
3512                 extra = dun_level - r_ptr->level;
3513                 extra = randint1(extra);
3514         }
3515
3516         /* Hack -- limit group reduction */
3517         if (extra > 9) extra = 9;
3518
3519         /* Modify the group size */
3520         total += extra;
3521
3522         /* Minimum size */
3523         if (total < 1) total = 1;
3524
3525         /* Maximum size */
3526         if (total > GROUP_MAX) total = GROUP_MAX;
3527
3528
3529         /* Save the rating */
3530         old = rating;
3531
3532         /* Start on the monster */
3533         hack_n = 1;
3534         hack_x[0] = x;
3535         hack_y[0] = y;
3536
3537         /* Puddle monsters, breadth first, up to total */
3538         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3539         {
3540                 /* Grab the location */
3541                 int hx = hack_x[n];
3542                 int hy = hack_y[n];
3543
3544                 /* Check each direction, up to total */
3545                 for (i = 0; (i < 8) && (hack_n < total); i++)
3546                 {
3547                         int mx, my;
3548
3549                         scatter(&my, &mx, hy, hx, 4, 0);
3550
3551                         /* Walls and Monsters block flow */
3552                         if (!cave_empty_bold2(my, mx)) continue;
3553
3554                         /* Attempt to place another monster */
3555                         if (place_monster_one(who, my, mx, r_idx, mode))
3556                         {
3557                                 /* Add it to the "hack" set */
3558                                 hack_y[hack_n] = my;
3559                                 hack_x[hack_n] = mx;
3560                                 hack_n++;
3561                         }
3562                 }
3563         }
3564
3565         /* Hack -- restore the rating */
3566         rating = old;
3567
3568
3569         /* Success */
3570         return (TRUE);
3571 }
3572
3573
3574 /*
3575  * Hack -- help pick an escort type
3576  */
3577 static int place_monster_idx = 0;
3578 static int place_monster_m_idx = 0;
3579
3580 /*
3581  * Hack -- help pick an escort type
3582  */
3583 static bool place_monster_okay(int r_idx)
3584 {
3585         monster_race *r_ptr = &r_info[place_monster_idx];
3586         monster_type *m_ptr = &m_list[place_monster_m_idx];
3587
3588         monster_race *z_ptr = &r_info[r_idx];
3589
3590         /* Hack - Escorts have to have the same dungeon flag */
3591         if (monster_dungeon(place_monster_idx) != monster_dungeon(r_idx)) return (FALSE);
3592
3593         /* Require similar "race" */
3594         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3595
3596         /* Skip more advanced monsters */
3597         if (z_ptr->level > r_ptr->level) return (FALSE);
3598
3599         /* Skip unique monsters */
3600         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3601
3602         /* Paranoia -- Skip identical monsters */
3603         if (place_monster_idx == r_idx) return (FALSE);
3604
3605         /* Skip different alignment */
3606         if (((m_ptr->sub_align & SUB_ALIGN_EVIL) && (z_ptr->flags3 & RF3_GOOD)) ||
3607             ((m_ptr->sub_align & SUB_ALIGN_GOOD) && (z_ptr->flags3 & RF3_EVIL)))
3608                 return FALSE;
3609
3610         if (r_ptr->flags7 & RF7_FRIENDLY)
3611         {
3612                 if (((p_ptr->align < 0) && (z_ptr->flags3 & RF3_GOOD)) ||
3613                     ((p_ptr->align > 0) && (z_ptr->flags3 & RF3_EVIL)))
3614                         return FALSE;
3615         }
3616
3617         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3618                 return FALSE;
3619
3620         /* Okay */
3621         return (TRUE);
3622 }
3623
3624
3625 /*
3626  * Attempt to place a monster of the given race at the given location
3627  *
3628  * Note that certain monsters are now marked as requiring "friends".
3629  * These monsters, if successfully placed, and if the "grp" parameter
3630  * is TRUE, will be surrounded by a "group" of identical monsters.
3631  *
3632  * Note that certain monsters are now marked as requiring an "escort",
3633  * which is a collection of monsters with similar "race" but lower level.
3634  *
3635  * Some monsters induce a fake "group" flag on their escorts.
3636  *
3637  * Note the "bizarre" use of non-recursion to prevent annoying output
3638  * when running a code profiler.
3639  *
3640  * Note the use of the new "monster allocation table" code to restrict
3641  * the "get_mon_num()" function to "legal" escort types.
3642  */
3643 bool place_monster_aux(int who, int y, int x, int r_idx, u32b mode)
3644 {
3645         int             i;
3646         monster_race    *r_ptr = &r_info[r_idx];
3647
3648         if (one_in_(333) && !(mode & PM_NO_KAGE) && !(mode & PM_FORCE_PET))
3649                 is_kage = TRUE;
3650         else
3651                 is_kage = FALSE;
3652
3653         /* Place one monster, or fail */
3654         if (!place_monster_one(who, y, x, r_idx, mode)) return (FALSE);
3655
3656
3657         /* Require the "group" flag */
3658         if (!(mode & PM_ALLOW_GROUP)) return (TRUE);
3659
3660         place_monster_m_idx = hack_m_idx_ii;
3661
3662         /* Friends for certain monsters */
3663         if (r_ptr->flags1 & (RF1_FRIENDS))
3664         {
3665                 /* Attempt to place a group */
3666                 (void)place_monster_group(who, y, x, r_idx, mode);
3667         }
3668
3669
3670         /* Escorts for certain monsters */
3671         if (r_ptr->flags1 & (RF1_ESCORT))
3672         {
3673                 /* Set the escort index */
3674                 place_monster_idx = r_idx;
3675
3676                 /* Try to place several "escorts" */
3677                 for (i = 0; i < 32; i++)
3678                 {
3679                         int nx, ny, z, d = 3;
3680
3681                         /* Pick a location */
3682                         scatter(&ny, &nx, y, x, d, 0);
3683
3684                         /* Require empty grids */
3685                         if (!cave_empty_bold2(ny, nx)) continue;
3686
3687                         /* Prepare allocation table */
3688                         get_mon_num_prep(place_monster_okay, get_monster_hook2(ny, nx));
3689
3690                         /* Pick a random race */
3691                         z = get_mon_num(r_ptr->level);
3692
3693                         /* Handle failure */
3694                         if (!z) break;
3695
3696                         /* Place a single escort */
3697                         (void)place_monster_one(place_monster_m_idx, ny, nx, z, mode);
3698
3699                         /* Place a "group" of escorts if needed */
3700                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3701                             (r_ptr->flags1 & RF1_ESCORTS))
3702                         {
3703                                 /* Place a group of monsters */
3704                                 (void)place_monster_group(place_monster_m_idx, ny, nx, z, mode);
3705                         }
3706                 }
3707         }
3708
3709         /* Success */
3710         return (TRUE);
3711 }
3712
3713
3714 /*
3715  * Hack -- attempt to place a monster at the given location
3716  *
3717  * Attempt to find a monster appropriate to the "monster_level"
3718  */
3719 bool place_monster(int y, int x, u32b mode)
3720 {
3721         int r_idx;
3722
3723         /* Prepare allocation table */
3724         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3725
3726         /* Pick a monster */
3727         r_idx = get_mon_num(monster_level);
3728
3729         /* Handle failure */
3730         if (!r_idx) return (FALSE);
3731
3732         /* Attempt to place the monster */
3733         if (place_monster_aux(0, y, x, r_idx, mode)) return (TRUE);
3734
3735         /* Oops */
3736         return (FALSE);
3737 }
3738
3739
3740 #ifdef MONSTER_HORDES
3741
3742 bool alloc_horde(int y, int x)
3743 {
3744         monster_race *r_ptr = NULL;
3745         int r_idx = 0;
3746         int m_idx;
3747         int attempts = 1000;
3748         int cy = y;
3749         int cx = x;
3750
3751         /* Prepare allocation table */
3752         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3753
3754         while (--attempts)
3755         {
3756                 /* Pick a monster */
3757                 r_idx = get_mon_num(monster_level);
3758
3759                 /* Handle failure */
3760                 if (!r_idx) return (FALSE);
3761
3762                 r_ptr = &r_info[r_idx];
3763
3764                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3765
3766                 if (r_idx == MON_HAGURE) continue;
3767                 break;
3768         }
3769         if (attempts < 1) return FALSE;
3770
3771         attempts = 1000;
3772
3773         while (--attempts)
3774         {
3775                 /* Attempt to place the monster */
3776                 if (place_monster_aux(0, y, x, r_idx, 0L)) break;
3777         }
3778
3779         if (attempts < 1) return FALSE;
3780
3781         m_idx = cave[y][x].m_idx;
3782
3783         if (m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[m_list[m_idx].r_idx];
3784         summon_kin_type = r_ptr->d_char;
3785
3786         for (attempts = randint1(10) + 5; attempts; attempts--)
3787         {
3788                 scatter(&cy, &cx, y, x, 5, 0);
3789
3790                 (void)summon_specific(m_idx, cy, cx, dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
3791
3792                 y = cy;
3793                 x = cx;
3794         }
3795
3796         return TRUE;
3797 }
3798
3799 #endif /* MONSTER_HORDES */
3800
3801
3802 /*
3803  * Put the Guardian
3804  */
3805 bool alloc_guardian(void)
3806 {
3807         int guardian = d_info[dungeon_type].final_guardian;
3808
3809         if (guardian && (d_info[dungeon_type].maxdepth == dun_level) && (r_info[guardian].cur_num < r_info[guardian].max_num))
3810         {
3811                 int oy;
3812                 int ox;
3813                 int try = 4000;
3814
3815                 /* Find a good position */
3816                 while (try)
3817                 {
3818                         /* Get a random spot */
3819                         oy = randint1(cur_hgt - 4) + 2;
3820                         ox = randint1(cur_wid - 4) + 2;
3821
3822                         /* Is it a good spot ? */
3823                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[guardian]))
3824                         {
3825                                 /* Place the guardian */
3826                                 if (place_monster_aux(0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) return TRUE;
3827                         }
3828
3829                         /* One less try */
3830                         try--;
3831                 }
3832         }
3833
3834         return FALSE;
3835 }
3836
3837
3838 /*
3839  * Attempt to allocate a random monster in the dungeon.
3840  *
3841  * Place the monster at least "dis" distance from the player.
3842  *
3843  * Use "slp" to choose the initial "sleep" status
3844  *
3845  * Use "monster_level" for the monster level
3846  */
3847 bool alloc_monster(int dis, u32b mode)
3848 {
3849         int                     y = 0, x = 0;
3850         int         attempts_left = 10000;
3851
3852         /* Put the Guardian */
3853         if (alloc_guardian()) return TRUE;
3854
3855         /* Find a legal, distant, unoccupied, space */
3856         while (attempts_left--)
3857         {
3858                 /* Pick a location */
3859                 y = randint0(cur_hgt);
3860                 x = randint0(cur_wid);
3861
3862                 /* Require empty floor grid (was "naked") */
3863                 if (dun_level || (wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].terrain != TERRAIN_MOUNTAIN))
3864                 {
3865                         if (!cave_empty_bold2(y, x)) continue;
3866                 }
3867                 else
3868                 {
3869                         if (!cave_empty_bold2(y, x) && (cave[y][x].feat != FEAT_MOUNTAIN)) continue;
3870                 }
3871
3872                 /* Accept far away grids */
3873                 if (distance(y, x, py, px) > dis) break;
3874         }
3875
3876         if (!attempts_left)
3877         {
3878                 if (cheat_xtra || cheat_hear)
3879                 {
3880 #ifdef JP
3881 msg_print("·Ù¹ð¡ª¿·¤¿¤Ê¥â¥ó¥¹¥¿¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡£¾®¤µ¤¤³¬¤Ç¤¹¤«¡©");
3882 #else
3883                         msg_print("Warning! Could not allocate a new monster. Small level?");
3884 #endif
3885
3886                 }
3887
3888                 return (FALSE);
3889         }
3890
3891
3892 #ifdef MONSTER_HORDES
3893         if (randint1(5000) <= dun_level)
3894         {
3895                 if (alloc_horde(y, x))
3896                 {
3897 #ifdef JP
3898                         if (cheat_hear) msg_format("¥â¥ó¥¹¥¿¡¼¤ÎÂç·²(%c)", summon_kin_type);
3899 #else
3900                         if (cheat_hear) msg_format("Monster horde (%c).", summon_kin_type);
3901 #endif
3902
3903                         return (TRUE);
3904                 }
3905         }
3906         else
3907         {
3908 #endif /* MONSTER_HORDES */
3909
3910                 /* Attempt to place the monster, allow groups */
3911                 if (place_monster(y, x, (mode | PM_ALLOW_GROUP))) return (TRUE);
3912
3913 #ifdef MONSTER_HORDES
3914         }
3915 #endif /* MONSTER_HORDES */
3916
3917         /* Nope */
3918         return (FALSE);
3919 }
3920
3921
3922
3923
3924 /*
3925  * Hack -- help decide if a monster race is "okay" to summon
3926  */
3927 static bool summon_specific_okay(int r_idx)
3928 {
3929         monster_race *r_ptr = &r_info[r_idx];
3930
3931         /* Hack - Only summon dungeon monsters */
3932         if (!monster_dungeon(r_idx)) return (FALSE);
3933
3934         /* Hack -- identify the summoning monster */
3935         if (summon_specific_who > 0)
3936         {
3937                 monster_type *m_ptr = &m_list[summon_specific_who];
3938
3939                 /* Do not summon enemies */
3940
3941                 /* Friendly vs. opposite aligned normal or pet */
3942                 if (((r_ptr->flags3 & RF3_EVIL) &&
3943                           (m_ptr->sub_align & SUB_ALIGN_GOOD)) ||
3944                          ((r_ptr->flags3 & RF3_GOOD) &&
3945                           (m_ptr->sub_align & SUB_ALIGN_EVIL)))
3946                 {
3947                         return FALSE;
3948                 }
3949
3950                 /* Hostile vs. non-hostile */
3951                 if (is_hostile(m_ptr) != summon_specific_hostile)
3952                 {
3953                         return FALSE;
3954                 }
3955         }
3956         /* Use the player's alignment */
3957         else if (summon_specific_who < 0)
3958         {
3959                 /* Do not summon enemies of the pets */
3960                 if ((p_ptr->align < -9) && (r_ptr->flags3 & RF3_GOOD))
3961                 {
3962                         if (!one_in_((0-p_ptr->align)/2+1)) return FALSE;
3963                 }
3964                 else if ((p_ptr->align > 9) && (r_ptr->flags3 & RF3_EVIL))
3965                 {
3966                         if (!one_in_(p_ptr->align/2+1)) return FALSE;
3967                 }
3968         }
3969
3970         /* Hack -- no specific type specified */
3971         if (!summon_specific_type) return (TRUE);
3972
3973         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7))) return FALSE;
3974
3975         if ((summon_specific_who < 0) &&
3976             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)) &&
3977             (((p_ptr->align > 9) && (r_ptr->flags3 & RF3_EVIL)) ||
3978              ((p_ptr->align < -9) && (r_ptr->flags3 & RF3_GOOD))))
3979                 return FALSE;
3980
3981         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[dungeon_type].flags1 & DF1_CHAMELEON)) return TRUE;
3982
3983         return (summon_specific_aux(r_idx));
3984 }
3985
3986
3987 /*
3988  * Place a monster (of the specified "type") near the given
3989  * location.  Return TRUE if a monster was actually summoned.
3990  *
3991  * We will attempt to place the monster up to 10 times before giving up.
3992  *
3993  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
3994  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
3995  * Note: None of the other summon codes will ever summon Unique's.
3996  *
3997  * This function has been changed.  We now take the "monster level"
3998  * of the summoning monster as a parameter, and use that, along with
3999  * the current dungeon level, to help determine the level of the
4000  * desired monster.  Note that this is an upper bound, and also
4001  * tends to "prefer" monsters of that level.  Currently, we use
4002  * the average of the dungeon and monster levels, and then add
4003  * five to allow slight increases in monster power.
4004  *
4005  * Note that we use the new "monster allocation table" creation code
4006  * to restrict the "get_mon_num()" function to the set of "legal"
4007  * monsters, making this function much faster and more reliable.
4008  *
4009  * Note that this function may not succeed, though this is very rare.
4010  */
4011 bool summon_specific(int who, int y1, int x1, int lev, int type, u32b mode)
4012 {
4013         int x, y, r_idx;
4014
4015         if (p_ptr->inside_arena) return (FALSE);
4016
4017         if (!mon_scatter(&y, &x, y1, x1, 2)) return FALSE;
4018
4019         /* Save the summoner */
4020         summon_specific_who = who;
4021
4022         /* Save the "summon" type */
4023         summon_specific_type = type;
4024
4025         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
4026
4027         /* Save the hostility */
4028         summon_specific_hostile = (!(mode & PM_FORCE_FRIENDLY) && !(is_friendly_idx(who)) && !(mode & PM_FORCE_PET));
4029
4030         /* Prepare allocation table */
4031         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
4032
4033         /* Pick a monster, using the level calculation */
4034         r_idx = get_mon_num((dun_level + lev) / 2 + 5);
4035
4036         /* Handle failure */
4037         if (!r_idx)
4038         {
4039                 summon_specific_type = 0;
4040                 return (FALSE);
4041         }
4042
4043         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
4044
4045         /* Attempt to place the monster (awake, allow groups) */
4046         if (!place_monster_aux(who, y, x, r_idx, mode))
4047         {
4048                 summon_specific_type = 0;
4049                 return (FALSE);
4050         }
4051
4052         summon_specific_type = 0;
4053         /* Success */
4054         return (TRUE);
4055 }
4056
4057 /* A "dangerous" function, creates a pet of the specified type */
4058 bool summon_named_creature (int who, int oy, int ox, int r_idx, u32b mode)
4059 {
4060         int x, y;
4061
4062         /* Paranoia */
4063         /* if (!r_idx) return; */
4064
4065         /* Prevent illegal monsters */
4066         if (r_idx >= max_r_idx) return FALSE;
4067
4068         if (p_ptr->inside_arena) return FALSE;
4069
4070         if (!mon_scatter(&y, &x, oy, ox, 2)) return FALSE;
4071
4072         /* Place it (allow groups) */
4073         return place_monster_aux(who, y, x, r_idx, (mode | PM_NO_KAGE));
4074 }
4075
4076
4077 /*
4078  * Let the given monster attempt to reproduce.
4079  *
4080  * Note that "reproduction" REQUIRES empty space.
4081  */
4082 bool multiply_monster(int m_idx, bool clone, u32b mode)
4083 {
4084         monster_type    *m_ptr = &m_list[m_idx];
4085
4086         int y, x;
4087
4088         if (!mon_scatter(&y, &x, m_ptr->fy, m_ptr->fx, 1))
4089                 return FALSE;
4090
4091         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
4092
4093         /* Create a new monster (awake, no groups) */
4094         if (!place_monster_aux(m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE)))
4095                 return FALSE;
4096
4097         if (clone)
4098         {
4099                 m_list[hack_m_idx_ii].smart |= SM_CLONED;
4100                 m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
4101         }
4102
4103         /* Hack -- Shadower spawns Shadower */
4104         if (m_ptr->mflag2 & MFLAG2_KAGE) m_list[hack_m_idx_ii].mflag2 |= MFLAG2_KAGE;
4105
4106         /* Hack -- Appearance transfer */
4107         if (!is_original_ap(m_ptr)) m_list[hack_m_idx_ii].ap_r_idx = m_ptr->ap_r_idx;
4108
4109         return TRUE;
4110 }
4111
4112
4113
4114
4115
4116 /*
4117  * Dump a message describing a monster's reaction to damage
4118  *
4119  * Technically should attempt to treat "Beholder"'s as jelly's
4120  */
4121 void message_pain(int m_idx, int dam)
4122 {
4123         long oldhp, newhp, tmp;
4124         int percentage;
4125
4126         monster_type *m_ptr = &m_list[m_idx];
4127         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4128
4129         char m_name[80];
4130
4131
4132         /* Get the monster name */
4133         monster_desc(m_name, m_ptr, 0);
4134
4135         /* Notice non-damage */
4136         if (dam == 0)
4137         {
4138 #ifdef JP
4139                 msg_format("%^s¤Ï¥À¥á¡¼¥¸¤ò¼õ¤±¤Æ¤¤¤Ê¤¤¡£", m_name);
4140 #else
4141                 msg_format("%^s is unharmed.", m_name);
4142 #endif
4143
4144                 return;
4145         }
4146
4147         /* Note -- subtle fix -CFT */
4148         newhp = (long)(m_ptr->hp);
4149         oldhp = newhp + (long)(dam);
4150         tmp = (newhp * 100L) / oldhp;
4151         percentage = (int)(tmp);
4152
4153
4154         /* Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs */
4155         if (strchr(",ejmvwQ", r_ptr->d_char))
4156         {
4157 #ifdef JP
4158                 if (percentage > 95)
4159                         msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4160                 else if (percentage > 75)
4161                         msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4162                 else if (percentage > 50)
4163                         msg_format("%^s¤Ï½Ì¤³¤Þ¤Ã¤¿¡£", m_name);
4164                 else if (percentage > 35)
4165                         msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4166                 else if (percentage > 20)
4167                         msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4168                 else if (percentage > 10)
4169                         msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4170                 else
4171                         msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4172 #else
4173                 if (percentage > 95)
4174                         msg_format("%^s barely notices.", m_name);
4175                 else if (percentage > 75)
4176                         msg_format("%^s flinches.", m_name);
4177                 else if (percentage > 50)
4178                         msg_format("%^s squelches.", m_name);
4179                 else if (percentage > 35)
4180                         msg_format("%^s quivers in pain.", m_name);
4181                 else if (percentage > 20)
4182                         msg_format("%^s writhes about.", m_name);
4183                 else if (percentage > 10)
4184                         msg_format("%^s writhes in agony.", m_name);
4185                 else
4186                         msg_format("%^s jerks limply.", m_name);
4187 #endif
4188
4189         }
4190
4191
4192         /* Fish */
4193         else if (strchr("l", r_ptr->d_char))
4194         {
4195                 if (percentage > 95)
4196 #ifdef JP
4197 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4198 #else
4199                         msg_format("%^s barely notices.", m_name);
4200 #endif
4201                 else if (percentage > 75)
4202 #ifdef JP
4203 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4204 #else
4205                         msg_format("%^s flinches.", m_name);
4206 #endif
4207                 else if (percentage > 50)
4208 #ifdef JP
4209 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4210 #else
4211                         msg_format("%^s hesitates.", m_name);
4212 #endif
4213                 else if (percentage > 35)
4214 #ifdef JP
4215 msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4216 #else
4217                         msg_format("%^s quivers in pain.", m_name);
4218 #endif
4219                 else if (percentage > 20)
4220 #ifdef JP
4221 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4222 #else
4223                         msg_format("%^s writhes about.", m_name);
4224 #endif
4225                 else if (percentage > 10)
4226 #ifdef JP
4227 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4228 #else
4229                         msg_format("%^s writhes in agony.", m_name);
4230 #endif
4231                 else
4232 #ifdef JP
4233 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4234 #else
4235                         msg_format("%^s jerks limply.", m_name);
4236 #endif
4237         }
4238
4239
4240         /* Golems, Walls, Doors, Stairs */
4241         else if (strchr("g#+<>", r_ptr->d_char))
4242         {
4243                 if (percentage > 95)
4244 #ifdef JP
4245 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4246 #else
4247                         msg_format("%^s ignores the attack.", m_name);
4248 #endif
4249                 else if (percentage > 75)
4250 #ifdef JP
4251 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4252 #else
4253                         msg_format("%^s shrugs off the attack.", m_name);
4254 #endif
4255                 else if (percentage > 50)
4256 #ifdef JP
4257 msg_format("%^s¤ÏÍëÌĤΤ褦¤ËËʤ¨¤¿¡£", m_name);
4258 #else
4259                         msg_format("%^s roars thunderously.", m_name);
4260 #endif
4261                 else if (percentage > 35)
4262 #ifdef JP
4263 msg_format("%^s¤Ï¶ì¤·¤²¤ËËʤ¨¤¿¡£", m_name);
4264 #else
4265                         msg_format("%^s rumbles.", m_name);
4266 #endif
4267                 else if (percentage > 20)
4268 #ifdef JP
4269 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4270 #else
4271                         msg_format("%^s grunts.", m_name);
4272 #endif
4273                 else if (percentage > 10)
4274 #ifdef JP
4275 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4276 #else
4277                         msg_format("%^s hesitates.", m_name);
4278 #endif
4279                 else
4280 #ifdef JP
4281 msg_format("%^s¤Ï¤¯¤·¤ã¤¯¤·¤ã¤Ë¤Ê¤Ã¤¿¡£", m_name);
4282 #else
4283                         msg_format("%^s crumples.", m_name);
4284 #endif
4285         }
4286
4287
4288         /* Snakes, Hydrae, Reptiles, Mimics */
4289         else if (strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char))
4290         {
4291                 if (percentage > 95)
4292 #ifdef JP
4293 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4294 #else
4295                         msg_format("%^s barely notices.", m_name);
4296 #endif
4297                 else if (percentage > 75)
4298 #ifdef JP
4299 msg_format("%^s¤Ï¥·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4300 #else
4301                         msg_format("%^s hisses.", m_name);
4302 #endif
4303                 else if (percentage > 50)
4304 #ifdef JP
4305 msg_format("%^s¤ÏÅܤäÆƬ¤ò¾å¤²¤¿¡£", m_name);
4306 #else
4307                         msg_format("%^s rears up in anger.", m_name);
4308 #endif
4309                 else if (percentage > 35)
4310 #ifdef JP
4311 msg_format("%^s¤ÏÌÔÁ³¤È°Ò³Å¤·¤¿¡£", m_name);
4312 #else
4313                         msg_format("%^s hisses furiously.", m_name);
4314 #endif
4315                 else if (percentage > 20)
4316 #ifdef JP
4317 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4318 #else
4319                         msg_format("%^s writhes about.", m_name);
4320 #endif
4321                 else if (percentage > 10)
4322 #ifdef JP
4323 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4324 #else
4325                         msg_format("%^s writhes in agony.", m_name);
4326 #endif
4327                 else
4328 #ifdef JP
4329 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4330 #else
4331                         msg_format("%^s jerks limply.", m_name);
4332 #endif
4333         }
4334
4335
4336         /* Felines */
4337         else if (strchr("f", r_ptr->d_char))
4338         {
4339                 if (percentage > 95)
4340 #ifdef JP
4341 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4342 #else
4343                         msg_format("%^s shrugs off the attack.", m_name);
4344 #endif
4345                 else if (percentage > 75)
4346 #ifdef JP
4347 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4348 #else
4349                         msg_format("%^s roars.", m_name);
4350 #endif
4351                 else if (percentage > 50)
4352 #ifdef JP
4353 msg_format("%^s¤ÏÅܤäÆËʤ¨¤¿¡£", m_name);
4354 #else
4355                         msg_format("%^s growls angrily.", m_name);
4356 #endif
4357                 else if (percentage > 35)
4358 #ifdef JP
4359 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4360 #else
4361                         msg_format("%^s hisses with pain.", m_name);
4362 #endif
4363                 else if (percentage > 20)
4364 #ifdef JP
4365 msg_format("%^s¤ÏÄˤߤǼ塹¤·¤¯ÌĤ¤¤¿¡£", m_name);
4366 #else
4367                         msg_format("%^s mewls in pain.", m_name);
4368 #endif
4369                 else if (percentage > 10)
4370 #ifdef JP
4371 msg_format("%^s¤Ï¶ìÄˤˤ¦¤á¤¤¤¿¡£", m_name);
4372 #else
4373                         msg_format("%^s hisses in agony.", m_name);
4374 #endif
4375                 else
4376 #ifdef JP
4377 msg_format("%s¤Ï°¥¤ì¤ÊÌĤ­À¼¤ò½Ð¤·¤¿¡£", m_name);
4378 #else
4379                         msg_format("%^s mewls pitifully.", m_name);
4380 #endif
4381         }
4382
4383
4384         /* Ants, Centipedes, Flies, Insects, Beetles, Spiders */
4385         else if (strchr("acFIKS", r_ptr->d_char))
4386         {
4387                 if (percentage > 95)
4388 #ifdef JP
4389 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4390 #else
4391                         msg_format("%^s ignores the attack.", m_name);
4392 #endif
4393                 else if (percentage > 75)
4394 #ifdef JP
4395 msg_format("%^s¤Ï¥­¡¼¥­¡¼ÌĤ¤¤¿¡£", m_name);
4396 #else
4397                         msg_format("%^s chitters.", m_name);
4398 #endif
4399
4400                 else if (percentage > 50)
4401 #ifdef JP
4402 msg_format("%^s¤Ï¥è¥í¥è¥íƨ¤²²ó¤Ã¤¿¡£", m_name);
4403 #else
4404                         msg_format("%^s scuttles about.", m_name);
4405 #endif
4406
4407                 else if (percentage > 35)
4408 #ifdef JP
4409 msg_format("%^s¤Ï¤¦¤ë¤µ¤¯ÌĤ¤¤¿¡£", m_name);
4410 #else
4411                         msg_format("%^s twitters.", m_name);
4412 #endif
4413
4414                 else if (percentage > 20)
4415 #ifdef JP
4416 msg_format("%^s¤ÏÄˤߤËáÛÚ»¤·¤¿¡£", m_name);
4417 #else
4418                         msg_format("%^s jerks in pain.", m_name);
4419 #endif
4420
4421                 else if (percentage > 10)
4422 #ifdef JP
4423 msg_format("%^s¤Ï¶ìÄˤÇáÛÚ»¤·¤¿¡£", m_name);
4424 #else
4425                         msg_format("%^s jerks in agony.", m_name);
4426 #endif
4427
4428                 else
4429 #ifdef JP
4430 msg_format("%^s¤Ï¥Ô¥¯¥Ô¥¯¤Ò¤­¤Ä¤Ã¤¿¡£", m_name);
4431 #else
4432                         msg_format("%^s twitches.", m_name);
4433 #endif
4434
4435         }
4436
4437
4438         /* Birds */
4439         else if (strchr("B", r_ptr->d_char))
4440         {
4441                 if (percentage > 95)
4442 #ifdef JP
4443 msg_format("%^s¤Ï¤µ¤¨¤º¤Ã¤¿¡£", m_name);
4444 #else
4445                         msg_format("%^s chirps.", m_name);
4446 #endif
4447
4448                 else if (percentage > 75)
4449 #ifdef JP
4450 msg_format("%^s¤Ï¥Ô¡¼¥Ô¡¼ÌĤ¤¤¿¡£", m_name);
4451 #else
4452                         msg_format("%^s twitters.", m_name);
4453 #endif
4454
4455                 else if (percentage > 50)
4456 #ifdef JP
4457 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ¤¤¿¡£", m_name);
4458 #else
4459                         msg_format("%^s squawks.", m_name);
4460 #endif
4461
4462                 else if (percentage > 35)
4463 #ifdef JP
4464 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4465 #else
4466                         msg_format("%^s chatters.", m_name);
4467 #endif
4468
4469                 else if (percentage > 20)
4470 #ifdef JP
4471 msg_format("%^s¤Ï¶ì¤·¤ó¤À¡£", m_name);
4472 #else
4473                         msg_format("%^s jeers.", m_name);
4474 #endif
4475
4476                 else if (percentage > 10)
4477 #ifdef JP
4478 msg_format("%^s¤Ï¤Î¤¿¤¦¤Á²ó¤Ã¤¿¡£", m_name);
4479 #else
4480                         msg_format("%^s flutters about.", m_name);
4481 #endif
4482
4483                 else
4484 #ifdef JP
4485 msg_format("%^s¤Ï¥­¡¼¥­¡¼¤ÈÌĤ­¶«¤ó¤À¡£", m_name);
4486 #else
4487                         msg_format("%^s squeaks.", m_name);
4488 #endif
4489
4490         }
4491
4492
4493         /* Dragons, Demons, High Undead */
4494         else if (strchr("duDLUW", r_ptr->d_char))
4495         {
4496                 if (percentage > 95)
4497 #ifdef JP
4498 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4499 #else
4500                         msg_format("%^s ignores the attack.", m_name);
4501 #endif
4502
4503                 else if (percentage > 75)
4504 #ifdef JP
4505 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4506 #else
4507                         msg_format("%^s flinches.", m_name);
4508 #endif
4509
4510                 else if (percentage > 50)
4511 #ifdef JP
4512 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4513 #else
4514                         msg_format("%^s hisses in pain.", m_name);
4515 #endif
4516
4517                 else if (percentage > 35)
4518 #ifdef JP
4519 msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4520 #else
4521                         msg_format("%^s snarls with pain.", m_name);
4522 #endif
4523
4524                 else if (percentage > 20)
4525 #ifdef JP
4526 msg_format("%^s¤ÏÄˤߤËËʤ¨¤¿¡£", m_name);
4527 #else
4528                         msg_format("%^s roars with pain.", m_name);
4529 #endif
4530
4531                 else if (percentage > 10)
4532 #ifdef JP
4533 msg_format("%^s¤Ï¶ì¤·¤²¤Ë¶«¤ó¤À¡£", m_name);
4534 #else
4535                         msg_format("%^s gasps.", m_name);
4536 #endif
4537
4538                 else
4539 #ifdef JP
4540 msg_format("%^s¤Ï¼å¡¹¤·¤¯¤¦¤Ê¤Ã¤¿¡£", m_name);
4541 #else
4542                         msg_format("%^s snarls feebly.", m_name);
4543 #endif
4544
4545         }
4546
4547
4548         /* Skeletons */
4549         else if (strchr("s", r_ptr->d_char))
4550         {
4551                 if (percentage > 95)
4552 #ifdef JP
4553 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4554 #else
4555                         msg_format("%^s ignores the attack.", m_name);
4556 #endif
4557
4558                 else if (percentage > 75)
4559 #ifdef JP
4560 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4561 #else
4562                         msg_format("%^s shrugs off the attack.", m_name);
4563 #endif
4564
4565                 else if (percentage > 50)
4566 #ifdef JP
4567 msg_format("%^s¤Ï¥«¥¿¥«¥¿¤È¾Ð¤Ã¤¿¡£", m_name);
4568 #else
4569                         msg_format("%^s rattles.", m_name);
4570 #endif
4571
4572                 else if (percentage > 35)
4573 #ifdef JP
4574 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4575 #else
4576                         msg_format("%^s stumbles.", m_name);
4577 #endif
4578
4579                 else if (percentage > 20)
4580 #ifdef JP
4581 msg_format("%^s¤Ï¥«¥¿¥«¥¿¸À¤Ã¤¿¡£", m_name);
4582 #else
4583                         msg_format("%^s rattles.", m_name);
4584 #endif
4585
4586                 else if (percentage > 10)
4587 #ifdef JP
4588 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4589 #else
4590                         msg_format("%^s staggers.", m_name);
4591 #endif
4592
4593                 else
4594 #ifdef JP
4595 msg_format("%^s¤Ï¥¬¥¿¥¬¥¿¸À¤Ã¤¿¡£", m_name);
4596 #else
4597                         msg_format("%^s clatters.", m_name);
4598 #endif
4599
4600         }
4601
4602
4603         /* Zombies */
4604         else if (strchr("z", r_ptr->d_char))
4605         {
4606                 if (percentage > 95)
4607 #ifdef JP
4608 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4609 #else
4610                         msg_format("%^s ignores the attack.", m_name);
4611 #endif
4612
4613                 else if (percentage > 75)
4614 #ifdef JP
4615 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4616 #else
4617                         msg_format("%^s shrugs off the attack.", m_name);
4618 #endif
4619
4620                 else if (percentage > 50)
4621 #ifdef JP
4622 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4623 #else
4624                         msg_format("%^s groans.", m_name);
4625 #endif
4626
4627                 else if (percentage > 35)
4628 #ifdef JP
4629 msg_format("%s¤Ï¶ì¤·¤²¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4630 #else
4631                         msg_format("%^s moans.", m_name);
4632 #endif
4633
4634                 else if (percentage > 20)
4635 #ifdef JP
4636 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4637 #else
4638                         msg_format("%^s hesitates.", m_name);
4639 #endif
4640
4641                 else if (percentage > 10)
4642 #ifdef JP
4643 msg_format("%^s¤Ï¤¦¤Ê¤Ã¤¿¡£", m_name);
4644 #else
4645                         msg_format("%^s grunts.", m_name);
4646 #endif
4647
4648                 else
4649 #ifdef JP
4650 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4651 #else
4652                         msg_format("%^s staggers.", m_name);
4653 #endif
4654
4655         }
4656
4657
4658         /* Ghosts */
4659         else if (strchr("G", r_ptr->d_char))
4660
4661         {
4662                 if (percentage > 95)
4663 #ifdef JP
4664 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4665 #else
4666                         msg_format("%^s ignores the attack.", m_name);
4667 #endif
4668
4669                 else if (percentage > 75)
4670 #ifdef JP
4671 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4672 #else
4673                         msg_format("%^s shrugs off the attack.", m_name);
4674 #endif
4675
4676                 else if (percentage > 50)
4677 #ifdef JP
4678 msg_format("%s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4679 #else
4680                         msg_format("%^s moans.", m_name);
4681 #endif
4682
4683                 else if (percentage > 35)
4684 #ifdef JP
4685 msg_format("%^s¤Ïµã¤­¤ï¤á¤¤¤¿¡£", m_name);
4686 #else
4687                         msg_format("%^s wails.", m_name);
4688 #endif
4689
4690                 else if (percentage > 20)
4691 #ifdef JP
4692 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4693 #else
4694                         msg_format("%^s howls.", m_name);
4695 #endif
4696
4697                 else if (percentage > 10)
4698 #ifdef JP
4699 msg_format("%s¤Ï¼å¡¹¤·¤¯¤¦¤á¤¤¤¿¡£", m_name);
4700 #else
4701                         msg_format("%^s moans softly.", m_name);
4702 #endif
4703
4704                 else
4705 #ifdef JP
4706 msg_format("%^s¤Ï¤«¤¹¤«¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4707 #else
4708                         msg_format("%^s sighs.", m_name);
4709 #endif
4710
4711         }
4712
4713
4714         /* Dogs and Hounds */
4715         else if (strchr("CZ", r_ptr->d_char))
4716         {
4717 #ifdef JP
4718                 if (percentage > 95)
4719                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4720                 else if (percentage > 75)
4721                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4722                 else if (percentage > 50)
4723                         msg_format("%^s¤ÏÄˤߤǥ­¥ã¥ó¥­¥ã¥óËʤ¨¤¿¡£", m_name);
4724                 else if (percentage > 35)
4725                         msg_format("%^s¤ÏÄˤߤÇÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4726                 else if (percentage > 20)
4727                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4728                 else if (percentage > 10)
4729                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4730                 else
4731                         msg_format("%^s¤Ï¼å¡¹¤·¤¯Ëʤ¨¤¿¡£", m_name);
4732 #else
4733                 if (percentage > 95)
4734                         msg_format("%^s shrugs off the attack.", m_name);
4735                 else if (percentage > 75)
4736                         msg_format("%^s snarls with pain.", m_name);
4737                 else if (percentage > 50)
4738                         msg_format("%^s yelps in pain.", m_name);
4739                 else if (percentage > 35)
4740                         msg_format("%^s howls in pain.", m_name);
4741                 else if (percentage > 20)
4742                         msg_format("%^s howls in agony.", m_name);
4743                 else if (percentage > 10)
4744                         msg_format("%^s writhes in agony.", m_name);
4745                 else
4746                         msg_format("%^s yelps feebly.", m_name);
4747 #endif
4748
4749         }
4750
4751         /* One type of monsters (ignore,squeal,shriek) */
4752         else if (strchr("Xbilqrt", r_ptr->d_char))
4753         {
4754 #ifdef JP
4755                 if (percentage > 95)
4756                         msg_format("%^s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4757                 else if (percentage > 75)
4758                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4759                 else if (percentage > 50)
4760                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4761                 else if (percentage > 35)
4762                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4763                 else if (percentage > 20)
4764                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4765                 else if (percentage > 10)
4766                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4767                 else
4768                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4769 #else
4770                 if (percentage > 95)
4771                         msg_format("%^s ignores the attack.", m_name);
4772                 else if (percentage > 75)
4773                         msg_format("%^s grunts with pain.", m_name);
4774                 else if (percentage > 50)
4775                         msg_format("%^s squeals in pain.", m_name);
4776                 else if (percentage > 35)
4777                         msg_format("%^s shrieks in pain.", m_name);
4778                 else if (percentage > 20)
4779                         msg_format("%^s shrieks in agony.", m_name);
4780                 else if (percentage > 10)
4781                         msg_format("%^s writhes in agony.", m_name);
4782                 else
4783                         msg_format("%^s cries out feebly.", m_name);
4784 #endif
4785
4786         }
4787
4788         /* Another type of monsters (shrug,cry,scream) */
4789         else
4790         {
4791 #ifdef JP
4792                 if (percentage > 95)
4793                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4794                 else if (percentage > 75)
4795                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4796                 else if (percentage > 50)
4797                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4798                 else if (percentage > 35)
4799                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4800                 else if (percentage > 20)
4801                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4802                 else if (percentage > 10)
4803                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4804                 else
4805                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4806 #else
4807                 if (percentage > 95)
4808                         msg_format("%^s shrugs off the attack.", m_name);
4809                 else if (percentage > 75)
4810                         msg_format("%^s grunts with pain.", m_name);
4811                 else if (percentage > 50)
4812                         msg_format("%^s cries out in pain.", m_name);
4813                 else if (percentage > 35)
4814                         msg_format("%^s screams in pain.", m_name);
4815                 else if (percentage > 20)
4816                         msg_format("%^s screams in agony.", m_name);
4817                 else if (percentage > 10)
4818                         msg_format("%^s writhes in agony.", m_name);
4819                 else
4820                         msg_format("%^s cries out feebly.", m_name);
4821 #endif
4822
4823         }
4824 }
4825
4826
4827 /*
4828  * Learn about an "observed" resistance.
4829  */
4830 void update_smart_learn(int m_idx, int what)
4831 {
4832 #ifdef DRS_SMART_OPTIONS
4833
4834         monster_type *m_ptr = &m_list[m_idx];
4835
4836         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4837
4838
4839         /* Not allowed to learn */
4840         if (!smart_learn) return;
4841
4842         /* Too stupid to learn anything */
4843         if (r_ptr->flags2 & (RF2_STUPID)) return;
4844
4845         /* Not intelligent, only learn sometimes */
4846         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
4847
4848
4849         /* XXX XXX XXX */
4850
4851         /* Analyze the knowledge */
4852         switch (what)
4853         {
4854         case DRS_ACID:
4855                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
4856                 if (IS_OPPOSE_ACID()) m_ptr->smart |= (SM_OPP_ACID);
4857                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
4858                 break;
4859
4860         case DRS_ELEC:
4861                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
4862                 if (IS_OPPOSE_ELEC()) m_ptr->smart |= (SM_OPP_ELEC);
4863                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
4864                 break;
4865
4866         case DRS_FIRE:
4867                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
4868                 if (IS_OPPOSE_FIRE()) m_ptr->smart |= (SM_OPP_FIRE);
4869                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
4870                 break;
4871
4872         case DRS_COLD:
4873                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
4874                 if (IS_OPPOSE_COLD()) m_ptr->smart |= (SM_OPP_COLD);
4875                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
4876                 break;
4877
4878         case DRS_POIS:
4879                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
4880                 if (IS_OPPOSE_POIS()) m_ptr->smart |= (SM_OPP_POIS);
4881                 break;
4882
4883
4884         case DRS_NETH:
4885                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
4886                 break;
4887
4888         case DRS_LITE:
4889                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
4890                 break;
4891
4892         case DRS_DARK:
4893                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
4894                 break;
4895
4896         case DRS_FEAR:
4897                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
4898                 break;
4899
4900         case DRS_CONF:
4901                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
4902                 break;
4903
4904         case DRS_CHAOS:
4905                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4906                 break;
4907
4908         case DRS_DISEN:
4909                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4910                 break;
4911
4912         case DRS_BLIND:
4913                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4914                 break;
4915
4916         case DRS_NEXUS:
4917                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4918                 break;
4919
4920         case DRS_SOUND:
4921                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4922                 break;
4923
4924         case DRS_SHARD:
4925                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4926                 break;
4927
4928         case DRS_FREE:
4929                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4930                 break;
4931
4932         case DRS_MANA:
4933                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4934                 break;
4935
4936         case DRS_REFLECT:
4937                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
4938                 break;
4939         }
4940
4941 #endif /* DRS_SMART_OPTIONS */
4942
4943 }
4944
4945
4946 /*
4947  * Place the player in the dungeon XXX XXX
4948  */
4949 bool player_place(int y, int x)
4950 {
4951         /* Paranoia XXX XXX */
4952         if (cave[y][x].m_idx != 0) return FALSE;
4953
4954         /* Save player location */
4955         py = y;
4956         px = x;
4957
4958         /* Success */
4959         return TRUE;
4960 }
4961
4962
4963 /*
4964  * Drop all items carried by a monster
4965  */
4966 void monster_drop_carried_objects(monster_type *m_ptr)
4967 {
4968         s16b this_o_idx, next_o_idx = 0;
4969         object_type forge;
4970         object_type *o_ptr;
4971         object_type *q_ptr;
4972
4973
4974         /* Drop objects being carried */
4975         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4976         {
4977                 /* Acquire object */
4978                 o_ptr = &o_list[this_o_idx];
4979
4980                 /* Acquire next object */
4981                 next_o_idx = o_ptr->next_o_idx;
4982
4983                 /* Paranoia */
4984                 o_ptr->held_m_idx = 0;
4985
4986                 /* Get local object */
4987                 q_ptr = &forge;
4988
4989                 /* Copy the object */
4990                 object_copy(q_ptr, o_ptr);
4991
4992                 /* Delete the object */
4993                 delete_object_idx(this_o_idx);
4994
4995                 /* Drop it */
4996                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
4997         }
4998
4999         /* Forget objects */
5000         m_ptr->hold_o_idx = 0;
5001 }