OSDN Git Service

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