OSDN Git Service

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