OSDN Git Service

snipe_concentrate および reset_concentration 関数で PU_BONUS フラグを立てていなかったため、集中が途切れた後も一時テレパシ...
[hengband/hengband.git] / src / monster2.c
1 /* File: monster2.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: misc code for monsters */
12
13 #include "angband.h"
14
15 #define HORDE_NOGOOD 0x01
16 #define HORDE_NOEVIL 0x02
17
18 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                         /* Snipers get telepathy when they concentrate deeper */
2395                         else if (p_ptr->telepathy)
2396                         {
2397                                 /* Empty mind, no telepathy */
2398                                 if (r_ptr->flags2 & (RF2_EMPTY_MIND))
2399                                 {
2400                                         /* Memorize flags */
2401                                         if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags2 |= (RF2_EMPTY_MIND);
2402                                 }
2403
2404                                 /* Weird mind, occasional telepathy */
2405                                 else if (r_ptr->flags2 & (RF2_WEIRD_MIND))
2406                                 {
2407                                         /* One in ten individuals are detectable */
2408                                         if ((m_idx % 10) == 5)
2409                                         {
2410                                                 /* Detectable */
2411                                                 flag = TRUE;
2412
2413                                                 if (is_original_ap(m_ptr) && !p_ptr->image)
2414                                                 {
2415                                                         /* Memorize flags */
2416                                                         r_ptr->r_flags2 |= (RF2_WEIRD_MIND);
2417
2418                                                         /* Hack -- Memorize mental flags */
2419                                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2420                                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2421                                                 }
2422                                         }
2423                                 }
2424
2425                                 /* Normal mind, allow telepathy */
2426                                 else
2427                                 {
2428                                         /* Detectable */
2429                                         flag = TRUE;
2430
2431                                         if (is_original_ap(m_ptr) && !p_ptr->image)
2432                                         {
2433                                                 /* Hack -- Memorize mental flags */
2434                                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2435                                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2436                                         }
2437                                 }
2438                         }
2439
2440                         /* Magical sensing */
2441                         if ((p_ptr->esp_animal) && (r_ptr->flags3 & (RF3_ANIMAL)))
2442                         {
2443                                 flag = TRUE;
2444                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_ANIMAL);
2445                         }
2446
2447                         /* Magical sensing */
2448                         if ((p_ptr->esp_undead) && (r_ptr->flags3 & (RF3_UNDEAD)))
2449                         {
2450                                 flag = TRUE;
2451                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_UNDEAD);
2452                         }
2453
2454                         /* Magical sensing */
2455                         if ((p_ptr->esp_demon) && (r_ptr->flags3 & (RF3_DEMON)))
2456                         {
2457                                 flag = TRUE;
2458                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_DEMON);
2459                         }
2460
2461                         /* Magical sensing */
2462                         if ((p_ptr->esp_orc) && (r_ptr->flags3 & (RF3_ORC)))
2463                         {
2464                                 flag = TRUE;
2465                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_ORC);
2466                         }
2467
2468                         /* Magical sensing */
2469                         if ((p_ptr->esp_troll) && (r_ptr->flags3 & (RF3_TROLL)))
2470                         {
2471                                 flag = TRUE;
2472                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_TROLL);
2473                         }
2474
2475                         /* Magical sensing */
2476                         if ((p_ptr->esp_giant) && (r_ptr->flags3 & (RF3_GIANT)))
2477                         {
2478                                 flag = TRUE;
2479                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_GIANT);
2480                         }
2481
2482                         /* Magical sensing */
2483                         if ((p_ptr->esp_dragon) && (r_ptr->flags3 & (RF3_DRAGON)))
2484                         {
2485                                 flag = TRUE;
2486                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_DRAGON);
2487                         }
2488
2489                         /* Magical sensing */
2490                         if ((p_ptr->esp_human) && (r_ptr->flags2 & (RF2_HUMAN)))
2491                         {
2492                                 flag = TRUE;
2493                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags2 |= (RF2_HUMAN);
2494                         }
2495
2496                         /* Magical sensing */
2497                         if ((p_ptr->esp_evil) && (r_ptr->flags3 & (RF3_EVIL)))
2498                         {
2499                                 flag = TRUE;
2500                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_EVIL);
2501                         }
2502
2503                         /* Magical sensing */
2504                         if ((p_ptr->esp_good) && (r_ptr->flags3 & (RF3_GOOD)))
2505                         {
2506                                 flag = TRUE;
2507                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_GOOD);
2508                         }
2509
2510                         /* Magical sensing */
2511                         if ((p_ptr->esp_nonliving) &&
2512                             ((r_ptr->flags3 & (RF3_DEMON | RF3_UNDEAD | RF3_NONLIVING)) == RF3_NONLIVING))
2513                         {
2514                                 flag = TRUE;
2515                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_NONLIVING);
2516                         }
2517
2518                         /* Magical sensing */
2519                         if ((p_ptr->esp_unique) && (r_ptr->flags1 & (RF1_UNIQUE)))
2520                         {
2521                                 flag = TRUE;
2522                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags1 |= (RF1_UNIQUE);
2523                         }
2524                 }
2525
2526                 /* Normal line of sight, and not blind */
2527                 if (player_has_los_bold(fy, fx) && !p_ptr->blind)
2528                 {
2529                         bool do_invisible = FALSE;
2530                         bool do_cold_blood = FALSE;
2531
2532                         /* Snipers can see targets in darkness when they concentrate deeper */
2533                         if (p_ptr->concent >= CONCENT_RADAR_THRESHOLD)
2534                         {
2535                                 /* Easy to see */
2536                                 easy = flag = TRUE;
2537                         }
2538
2539                         /* Use "infravision" */
2540                         if (d <= p_ptr->see_infra)
2541                         {
2542                                 /* Handle "cold blooded" monsters */
2543                                 if ((r_ptr->flags2 & (RF2_COLD_BLOOD | RF2_AURA_FIRE)) == RF2_COLD_BLOOD)
2544                                 {
2545                                         /* Take note */
2546                                         do_cold_blood = TRUE;
2547                                 }
2548
2549                                 /* Handle "warm blooded" monsters */
2550                                 else
2551                                 {
2552                                         /* Easy to see */
2553                                         easy = flag = TRUE;
2554                                 }
2555                         }
2556
2557                         /* Use "illumination" */
2558                         if (player_can_see_bold(fy, fx))
2559                         {
2560                                 /* Handle "invisible" monsters */
2561                                 if (r_ptr->flags2 & (RF2_INVISIBLE))
2562                                 {
2563                                         /* Take note */
2564                                         do_invisible = TRUE;
2565
2566                                         /* See invisible */
2567                                         if (p_ptr->see_inv)
2568                                         {
2569                                                 /* Easy to see */
2570                                                 easy = flag = TRUE;
2571                                         }
2572                                 }
2573
2574                                 /* Handle "normal" monsters */
2575                                 else
2576                                 {
2577                                         /* Easy to see */
2578                                         easy = flag = TRUE;
2579                                 }
2580                         }
2581
2582                         /* Visible */
2583                         if (flag)
2584                         {
2585                                 if (is_original_ap(m_ptr) && !p_ptr->image)
2586                                 {
2587                                         /* Memorize flags */
2588                                         if (do_invisible) r_ptr->r_flags2 |= (RF2_INVISIBLE);
2589                                         if (do_cold_blood) r_ptr->r_flags2 |= (RF2_COLD_BLOOD);
2590                                 }
2591                         }
2592                 }
2593         }
2594
2595
2596         /* The monster is now visible */
2597         if (flag)
2598         {
2599                 /* It was previously unseen */
2600                 if (!m_ptr->ml)
2601                 {
2602                         /* Mark as visible */
2603                         m_ptr->ml = TRUE;
2604
2605                         /* Draw the monster */
2606                         lite_spot(fy, fx);
2607
2608                         /* Update health bar as needed */
2609                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2610                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2611
2612                         /* Hack -- Count "fresh" sightings */
2613                         if (!p_ptr->image)
2614                         {
2615                                 if ((m_ptr->ap_r_idx == MON_KAGE) && (r_info[MON_KAGE].r_sights < MAX_SHORT))
2616                                         r_info[MON_KAGE].r_sights++;
2617                                 else if (is_original_ap(m_ptr) && (r_ptr->r_sights < MAX_SHORT))
2618                                         r_ptr->r_sights++;
2619                         }
2620
2621                         /* Eldritch Horror */
2622                         if (r_info[m_ptr->ap_r_idx].flags2 & RF2_ELDRITCH_HORROR)
2623                         {
2624                                 sanity_blast(m_ptr, FALSE);
2625                         }
2626
2627                         /* Disturb on appearance */
2628                         if (disturb_near && (projectable(m_ptr->fy, m_ptr->fx, py, px) && projectable(py, px, m_ptr->fy, m_ptr->fx)))
2629                         {
2630                                 if (disturb_pets || is_hostile(m_ptr))
2631                                         disturb(1, 0);
2632                         }
2633                 }
2634         }
2635
2636         /* The monster is not visible */
2637         else
2638         {
2639                 /* It was previously seen */
2640                 if (m_ptr->ml)
2641                 {
2642                         /* Mark as not visible */
2643                         m_ptr->ml = FALSE;
2644
2645                         /* Erase the monster */
2646                         lite_spot(fy, fx);
2647
2648                         /* Update health bar as needed */
2649                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2650                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2651
2652                         /* Disturb on disappearance */
2653                         if (do_disturb)
2654                         {
2655                                 if (disturb_pets || is_hostile(m_ptr))
2656                                         disturb(1, 0);
2657                         }
2658                 }
2659         }
2660
2661
2662         /* The monster is now easily visible */
2663         if (easy)
2664         {
2665                 /* Change */
2666                 if (!(m_ptr->mflag & (MFLAG_VIEW)))
2667                 {
2668                         /* Mark as easily visible */
2669                         m_ptr->mflag |= (MFLAG_VIEW);
2670
2671                         /* Disturb on appearance */
2672                         if (do_disturb)
2673                         {
2674                                 if (disturb_pets || is_hostile(m_ptr))
2675                                         disturb(1, 0);
2676                         }
2677                 }
2678         }
2679
2680         /* The monster is not easily visible */
2681         else
2682         {
2683                 /* Change */
2684                 if (m_ptr->mflag & (MFLAG_VIEW))
2685                 {
2686                         /* Mark as not easily visible */
2687                         m_ptr->mflag &= ~(MFLAG_VIEW);
2688
2689                         /* Disturb on disappearance */
2690                         if (do_disturb)
2691                         {
2692                                 if (disturb_pets || is_hostile(m_ptr))
2693                                         disturb(1, 0);
2694                         }
2695                 }
2696         }
2697 }
2698
2699
2700 /*
2701  * This function simply updates all the (non-dead) monsters (see above).
2702  */
2703 void update_monsters(bool full)
2704 {
2705         int i;
2706
2707         /* Update each (live) monster */
2708         for (i = 1; i < m_max; i++)
2709         {
2710                 monster_type *m_ptr = &m_list[i];
2711
2712                 /* Skip dead monsters */
2713                 if (!m_ptr->r_idx) continue;
2714
2715                 /* Update the monster */
2716                 update_mon(i, full);
2717         }
2718 }
2719
2720
2721 /*
2722  * Hack -- the index of the summoning monster
2723  */
2724 static bool monster_hook_chameleon_lord(int r_idx)
2725 {
2726         monster_race *r_ptr = &r_info[r_idx];
2727         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2728         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2729
2730         if (!(r_ptr->flags1 & (RF1_UNIQUE))) return FALSE;
2731         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2732
2733         if (ABS(r_ptr->level - r_info[MON_CHAMELEON_K].level) > 5) return FALSE;
2734
2735         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))
2736                 return FALSE;
2737
2738         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2739
2740         /* Not born */
2741         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2742         {
2743                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
2744         }
2745
2746         /* Born now */
2747         else if (summon_specific_who > 0)
2748         {
2749                 if (monster_has_hostile_align(&m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2750         }
2751
2752         return TRUE;
2753 }
2754
2755 static bool monster_hook_chameleon(int r_idx)
2756 {
2757         monster_race *r_ptr = &r_info[r_idx];
2758         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2759         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2760
2761         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2762         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2763         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2764         
2765         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))
2766                 return FALSE;
2767
2768         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2769
2770         /* Not born */
2771         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2772         {
2773                 if ((old_r_ptr->flags3 & RF3_GOOD) && !(r_ptr->flags3 & RF3_GOOD)) return FALSE;
2774                 if ((old_r_ptr->flags3 & RF3_EVIL) && !(r_ptr->flags3 & RF3_EVIL)) return FALSE;
2775                 if (!(old_r_ptr->flags3 & (RF3_GOOD | RF3_EVIL)) && (r_ptr->flags3 & (RF3_GOOD | RF3_EVIL))) return FALSE;
2776         }
2777
2778         /* Born now */
2779         else if (summon_specific_who > 0)
2780         {
2781                 if (monster_has_hostile_align(&m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2782         }
2783
2784         return (*(get_monster_hook()))(r_idx);
2785 }
2786
2787
2788 void choose_new_monster(int m_idx, bool born, int r_idx)
2789 {
2790         int oldmaxhp;
2791         monster_type *m_ptr = &m_list[m_idx];
2792         monster_race *r_ptr;
2793         char old_m_name[80];
2794         bool old_unique = FALSE;
2795         int old_r_idx = m_ptr->r_idx;
2796
2797         if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2798                 old_unique = TRUE;
2799         if (old_unique && (r_idx == MON_CHAMELEON)) r_idx = MON_CHAMELEON_K;
2800         r_ptr = &r_info[r_idx];
2801
2802         monster_desc(old_m_name, m_ptr, 0);
2803
2804         if (!r_idx)
2805         {
2806                 int level;
2807
2808                 chameleon_change_m_idx = m_idx;
2809                 if (old_unique)
2810                         get_mon_num_prep(monster_hook_chameleon_lord, NULL);
2811                 else
2812                         get_mon_num_prep(monster_hook_chameleon, NULL);
2813
2814                 if (old_unique)
2815                         level = r_info[MON_CHAMELEON_K].level;
2816                 else if (!dun_level)
2817                         level = wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].level;
2818                 else
2819                         level = dun_level;
2820
2821                 if (d_info[dungeon_type].flags1 & DF1_CHAMELEON) level+= 2+randint1(3);
2822
2823                 r_idx = get_mon_num(level);
2824                 r_ptr = &r_info[r_idx];
2825
2826                 chameleon_change_m_idx = 0;
2827
2828                 /* Paranoia */
2829                 if (!r_idx) return;
2830         }
2831
2832         if (is_pet(m_ptr)) check_pets_num_and_align(m_ptr, FALSE);
2833
2834         m_ptr->r_idx = r_idx;
2835         m_ptr->ap_r_idx = r_idx;
2836         update_mon(m_idx, FALSE);
2837         lite_spot(m_ptr->fy, m_ptr->fx);
2838
2839         if ((r_info[old_r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)) ||
2840             (r_ptr->flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)))
2841                 p_ptr->update |= (PU_MON_LITE);
2842
2843         if (is_pet(m_ptr)) check_pets_num_and_align(m_ptr, TRUE);
2844
2845         if (born)
2846         {
2847                 /* Sub-alignment of a chameleon */
2848                 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
2849                 {
2850                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2851                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2852                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2853                 }
2854                 return;
2855         }
2856
2857         if (m_idx == p_ptr->riding)
2858         {
2859                 char m_name[80];
2860                 monster_desc(m_name, m_ptr, 0);
2861 #ifdef JP
2862                 msg_format("ÆÍÁ³%s¤¬ÊѿȤ·¤¿¡£", old_m_name);
2863 #else
2864                 msg_format("Suddenly, %s transforms!", old_m_name);
2865 #endif
2866                 if (!(r_ptr->flags7 & RF7_RIDING))
2867 #ifdef JP
2868                         if (rakuba(0, TRUE)) msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2869 #else
2870                         if (rakuba(0, TRUE)) msg_format("You have fallen from %s.", m_name);
2871 #endif
2872         }
2873
2874         /* Extract the monster base speed */
2875         m_ptr->mspeed = get_mspeed(r_ptr);
2876
2877         oldmaxhp = m_ptr->max_maxhp;
2878         /* Assign maximal hitpoints */
2879         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2880         {
2881                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2882         }
2883         else
2884         {
2885                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2886         }
2887
2888         /* Monsters have double hitpoints in Nightmare mode */
2889         if (ironman_nightmare)
2890         {
2891                 u32b hp = m_ptr->max_maxhp * 2L;
2892                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
2893         }
2894
2895         m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
2896         if (m_ptr->maxhp < 1) m_ptr->maxhp = 1;
2897         m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
2898 }
2899
2900
2901 /*
2902  *  Hook for Tanuki
2903  */
2904 static bool monster_hook_tanuki(int r_idx)
2905 {
2906         monster_race *r_ptr = &r_info[r_idx];
2907
2908         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2909         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2910         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2911         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2912         
2913         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))
2914                 return FALSE;
2915
2916         return (*(get_monster_hook()))(r_idx);
2917 }
2918
2919
2920 /*
2921  *  Set initial racial appearance of a monster
2922  */
2923 static int initial_r_appearance(int r_idx)
2924 {
2925         int attempts = 1000;
2926
2927         int ap_r_idx;
2928         int min = MIN(base_level-5, 50);
2929
2930         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2931                 return r_idx;
2932
2933         get_mon_num_prep(monster_hook_tanuki, NULL);
2934
2935         while (--attempts)
2936         {
2937                 ap_r_idx = get_mon_num(base_level + 10);
2938                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2939         }
2940
2941         return r_idx;
2942 }
2943
2944
2945 /*
2946  * Get initial monster speed
2947  */
2948 byte get_mspeed(monster_race *r_ptr)
2949 {
2950         /* Extract the monster base speed */
2951         int mspeed = r_ptr->speed;
2952
2953         /* Hack -- small racial variety */
2954         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2955         {
2956                 /* Allow some small variation per monster */
2957                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2958                 if (i) mspeed += rand_spread(0, i);
2959         }
2960
2961         if (mspeed > 199) mspeed = 199;
2962
2963         return (byte)mspeed;
2964 }
2965
2966
2967 /*
2968  * Attempt to place a monster of the given race at the given location.
2969  *
2970  * To give the player a sporting chance, any monster that appears in
2971  * line-of-sight and is extremely dangerous can be marked as
2972  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2973  * which often (but not always) lets the player move before they do.
2974  *
2975  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2976  *
2977  * XXX XXX XXX Use special "here" and "dead" flags for unique monsters,
2978  * remove old "cur_num" and "max_num" fields.
2979  *
2980  * XXX XXX XXX Actually, do something similar for artifacts, to simplify
2981  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2982  *
2983  * This is the only function which may place a monster in the dungeon,
2984  * except for the savefile loading code.
2985  */
2986 static bool place_monster_one(int who, int y, int x, int r_idx, u32b mode)
2987 {
2988         /* Access the location */
2989         cave_type               *c_ptr = &cave[y][x];
2990
2991         monster_type    *m_ptr;
2992
2993         monster_race    *r_ptr = &r_info[r_idx];
2994
2995         cptr            name = (r_name + r_ptr->name);
2996
2997         int cmi;
2998
2999         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
3000         if (p_ptr->wild_mode) return FALSE;
3001
3002         /* Verify location */
3003         if (!in_bounds(y, x)) return (FALSE);
3004
3005         /* Paranoia */
3006         if (!r_idx) return (FALSE);
3007
3008         /* Paranoia */
3009         if (!r_ptr->name) return (FALSE);
3010
3011         if (!(mode & PM_IGNORE_TERRAIN))
3012         {
3013                 /* Not on the Pattern */
3014                 if (pattern_tile(y, x)) return FALSE;
3015
3016                 /* Require empty space (if not ghostly) */
3017                 if (!monster_can_enter(y, x, r_ptr, 0)) return FALSE;
3018         }
3019
3020         if (!p_ptr->inside_battle)
3021         {
3022                 /* Hack -- "unique" monsters must be "unique" */
3023                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
3024                      (r_ptr->flags7 & (RF7_NAZGUL))) &&
3025                     (r_ptr->cur_num >= r_ptr->max_num))
3026                 {
3027                         /* Cannot create */
3028                         return (FALSE);
3029                 }
3030
3031                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
3032                     (r_ptr->cur_num >= 1))
3033                 {
3034                         return (FALSE);
3035                 }
3036
3037                 if (r_idx == MON_BANORLUPART)
3038                 {
3039                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
3040                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
3041                 }
3042
3043                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
3044                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (dun_level < r_ptr->level) &&
3045                     (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
3046                 {
3047                         /* Cannot create */
3048                         return (FALSE);
3049                 }
3050         }
3051
3052         if (quest_number(dun_level))
3053         {
3054                 int hoge = quest_number(dun_level);
3055                 if ((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
3056                 {
3057                         if(r_idx == quest[hoge].r_idx)
3058                         {
3059                                 int number_mon, i2, j2;
3060                                 number_mon = 0;
3061
3062                                 /* Count all quest monsters */
3063                                 for (i2 = 0; i2 < cur_wid; ++i2)
3064                                         for (j2 = 0; j2 < cur_hgt; j2++)
3065                                                 if (cave[j2][i2].m_idx > 0)
3066                                                         if (m_list[cave[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
3067                                                                 number_mon++;
3068                                 if(number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
3069                                         return FALSE;
3070                         }
3071                 }
3072         }
3073
3074         if (is_glyph_grid(c_ptr))
3075         {
3076                 if (randint1(BREAK_GLYPH) < (r_ptr->level+20))
3077                 {
3078                         /* Describe observable breakage */
3079                         if (c_ptr->info & CAVE_MARK)
3080                         {
3081 #ifdef JP
3082 msg_print("¼é¤ê¤Î¥ë¡¼¥ó¤¬²õ¤ì¤¿¡ª");
3083 #else
3084                                 msg_print("The rune of protection is broken!");
3085 #endif
3086
3087                         }
3088
3089                         /* Forget the rune */
3090                         c_ptr->info &= ~(CAVE_MARK);
3091
3092                         /* Break the rune */
3093                         c_ptr->info &= ~(CAVE_OBJECT);
3094                         c_ptr->mimic = 0;
3095
3096                         /* Notice */
3097                         note_spot(y, x);
3098                 }
3099                 else return FALSE;
3100         }
3101
3102         /* Powerful monster */
3103         if (r_ptr->level > dun_level)
3104         {
3105                 /* Unique monsters */
3106                 if (r_ptr->flags1 & (RF1_UNIQUE))
3107                 {
3108                         /* Message for cheaters */
3109 #ifdef JP
3110                         if (cheat_hear) msg_format("¿¼ÁؤΥæ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3111 #else
3112                         if (cheat_hear) msg_format("Deep Unique (%s).", name);
3113 #endif
3114                 }
3115
3116                 /* Normal monsters */
3117                 else
3118                 {
3119                         /* Message for cheaters */
3120 #ifdef JP
3121                         if (cheat_hear) msg_format("¿¼ÁؤΥâ¥ó¥¹¥¿¡¼ (%s)¡£", name);
3122 #else
3123                         if (cheat_hear) msg_format("Deep Monster (%s).", name);
3124 #endif
3125                 }
3126         }
3127
3128         /* Note the monster */
3129         else if (r_ptr->flags1 & (RF1_UNIQUE))
3130         {
3131                 /* Unique monsters induce message */
3132 #ifdef JP
3133                 if (cheat_hear) msg_format("¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3134 #else
3135                 if (cheat_hear) msg_format("Unique (%s).", name);
3136 #endif
3137
3138         }
3139
3140         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL) || (r_ptr->level < 10)) mode &= ~PM_KAGE;
3141
3142         /* Make a new monster */
3143         c_ptr->m_idx = m_pop();
3144         hack_m_idx_ii = c_ptr->m_idx;
3145
3146         /* Mega-Hack -- catch "failure" */
3147         if (!c_ptr->m_idx) return (FALSE);
3148
3149
3150         /* Get a new monster record */
3151         m_ptr = &m_list[c_ptr->m_idx];
3152
3153         /* Save the race */
3154         m_ptr->r_idx = r_idx;
3155         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
3156
3157         /* No flags */
3158         m_ptr->mflag = 0;
3159         m_ptr->mflag2 = 0;
3160
3161         /* Hack -- Appearance transfer */
3162         if ((mode & PM_MULTIPLY) && (who > 0) && !is_original_ap(&m_list[who]))
3163         {
3164                 m_ptr->ap_r_idx = m_list[who].ap_r_idx;
3165
3166                 /* Hack -- Shadower spawns Shadower */
3167                 if (m_list[who].mflag2 & MFLAG2_KAGE) m_ptr->mflag2 |= MFLAG2_KAGE;
3168         }
3169
3170         /* Sub-alignment of a monster */
3171         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
3172                 m_ptr->sub_align = m_list[who].sub_align;
3173         else
3174         {
3175                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3176                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
3177                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
3178         }
3179
3180         /* Place the monster at the location */
3181         m_ptr->fy = y;
3182         m_ptr->fx = x;
3183
3184
3185         /* No "timed status" yet */
3186         for (cmi = 0; cmi < MAX_MTIMED; cmi++) m_ptr->mtimed[cmi] = 0;
3187
3188         /* Unknown distance */
3189         m_ptr->cdis = 0;
3190
3191         reset_target(m_ptr);
3192
3193         m_ptr->nickname = 0;
3194
3195         m_ptr->exp = 0;
3196
3197
3198         /* Your pet summons its pet. */
3199         if (who > 0 && is_pet(&m_list[who]))
3200         {
3201                 mode |= PM_FORCE_PET;
3202                 m_ptr->parent_m_idx = who;
3203         }
3204         else
3205         {
3206                 m_ptr->parent_m_idx = 0;
3207         }
3208
3209         if (r_ptr->flags7 & RF7_CHAMELEON)
3210         {
3211                 choose_new_monster(c_ptr->m_idx, TRUE, 0);
3212                 r_ptr = &r_info[m_ptr->r_idx];
3213                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
3214
3215                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
3216                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
3217                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3218         }
3219         else if ((mode & PM_KAGE) && !(mode & PM_FORCE_PET))
3220         {
3221                 m_ptr->ap_r_idx = MON_KAGE;
3222                 m_ptr->mflag2 |= MFLAG2_KAGE;
3223         }
3224
3225         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
3226
3227         /* Not visible */
3228         m_ptr->ml = FALSE;
3229
3230         /* Pet? */
3231         if (mode & PM_FORCE_PET)
3232         {
3233                 set_pet(m_ptr);
3234         }
3235         /* Friendly? */
3236         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
3237                  (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(who))
3238         {
3239                 if (!monster_has_hostile_align(NULL, 0, -1, r_ptr)) set_friendly(m_ptr);
3240         }
3241
3242         /* Assume no sleeping */
3243         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
3244
3245         /* Enforce sleeping if needed */
3246         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
3247         {
3248                 int val = r_ptr->sleep;
3249                 (void)set_monster_csleep(c_ptr->m_idx, (val * 2) + randint1(val * 10));
3250         }
3251
3252         /* Assign maximal hitpoints */
3253         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
3254         {
3255                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
3256         }
3257         else
3258         {
3259                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
3260         }
3261
3262         /* Monsters have double hitpoints in Nightmare mode */
3263         if (ironman_nightmare)
3264         {
3265                 u32b hp = m_ptr->max_maxhp * 2L;
3266
3267                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
3268         }
3269
3270         m_ptr->maxhp = m_ptr->max_maxhp;
3271
3272         /* And start out fully healthy */
3273         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
3274                 m_ptr->hp = m_ptr->maxhp / 2;
3275         else m_ptr->hp = m_ptr->maxhp;
3276
3277
3278         /* Extract the monster base speed */
3279         m_ptr->mspeed = get_mspeed(r_ptr);
3280
3281         if (mode & PM_HASTE) (void)set_monster_fast(c_ptr->m_idx, 100);
3282
3283         /* Give a random starting energy */
3284         if (!ironman_nightmare)
3285         {
3286                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
3287         }
3288         else
3289         {
3290                 /* Nightmare monsters are more prepared */
3291                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
3292         }
3293
3294         /* Force monster to wait for player, unless in Nightmare mode */
3295         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
3296         {
3297                 /* Monster is still being nice */
3298                 m_ptr->mflag |= (MFLAG_NICE);
3299
3300                 /* Must repair monsters */
3301                 repair_monsters = TRUE;
3302         }
3303
3304         /* Hack -- see "process_monsters()" */
3305         if (c_ptr->m_idx < hack_m_idx)
3306         {
3307                 /* Monster is still being born */
3308                 m_ptr->mflag |= (MFLAG_BORN);
3309         }
3310
3311
3312         if (r_ptr->flags7 & RF7_SELF_LD_MASK)
3313                 p_ptr->update |= (PU_MON_LITE);
3314         else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !MON_CSLEEP(m_ptr))
3315                 p_ptr->update |= (PU_MON_LITE);
3316
3317         /* Update the monster */
3318         update_mon(c_ptr->m_idx, TRUE);
3319
3320
3321         /* Count the monsters on the level */
3322         real_r_ptr(m_ptr)->cur_num++;
3323
3324         /*
3325          * Memorize location of the unique monster in saved floors.
3326          * A unique monster move from old saved floor.
3327          */
3328         if (character_dungeon &&
3329             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
3330                 real_r_ptr(m_ptr)->floor_id = p_ptr->floor_id;
3331
3332         /* Hack -- Count the number of "reproducers" */
3333         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
3334
3335         /* Hack -- Notice new multi-hued monsters */
3336         {
3337                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
3338                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
3339                         shimmer_monsters = TRUE;
3340         }
3341
3342         if (p_ptr->warning && character_dungeon)
3343         {
3344                 if (r_ptr->flags1 & RF1_UNIQUE)
3345                 {
3346                         cptr color;
3347                         object_type *o_ptr;
3348                         char o_name[MAX_NLEN];
3349
3350                         if (r_ptr->level > p_ptr->lev + 30)
3351 #ifdef JP
3352                                 color = "¹õ¤¯";
3353 #else
3354                                 color = "black";
3355 #endif
3356                         else if (r_ptr->level > p_ptr->lev + 15)
3357 #ifdef JP
3358                                 color = "»ç¿§¤Ë";
3359 #else
3360                                 color = "purple";
3361 #endif
3362                         else if (r_ptr->level > p_ptr->lev + 5)
3363 #ifdef JP
3364                                 color = "¥ë¥Ó¡¼¿§¤Ë";
3365 #else
3366                                 color = "deep red";
3367 #endif
3368                         else if (r_ptr->level > p_ptr->lev - 5)
3369 #ifdef JP
3370                                 color = "ÀÖ¤¯";
3371 #else
3372                                 color = "red";
3373 #endif
3374                         else if (r_ptr->level > p_ptr->lev - 15)
3375 #ifdef JP
3376                                 color = "¥Ô¥ó¥¯¿§¤Ë";
3377 #else
3378                                 color = "pink";
3379 #endif
3380                         else
3381 #ifdef JP
3382                                 color = "Çò¤¯";
3383 #else
3384                                 color = "white";
3385 #endif
3386
3387                         o_ptr = choose_warning_item();
3388                         if (o_ptr)
3389                         {
3390                                 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
3391 #ifdef JP
3392                                 msg_format("%s¤Ï%s¸÷¤Ã¤¿¡£", o_name, color);
3393 #else
3394                                 msg_format("%s glows %s.", o_name, color);
3395 #endif
3396                         }
3397                         else
3398                         {
3399 #ifdef JP
3400                                 msg_format("s%¸÷¤ëʪ¤¬Æ¬¤ËÉ⤫¤ó¤À¡£", color);
3401 #else
3402                                 msg_format("An %s image forms in your mind.");
3403 #endif
3404                         }
3405                 }
3406         }
3407
3408         if (is_explosive_rune_grid(c_ptr))
3409         {
3410                 /* Break the ward */
3411                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3412                 {
3413                         /* Describe observable breakage */
3414                         if (c_ptr->info & CAVE_MARK)
3415                         {
3416 #ifdef JP
3417 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3418 #else
3419                                 msg_print("The rune explodes!");
3420 #endif
3421
3422                                 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);
3423                         }
3424                 }
3425                 else
3426                 {
3427 #ifdef JP
3428 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3429 #else
3430                         msg_print("An explosive rune was disarmed.");
3431 #endif
3432                 }
3433
3434                 /* Forget the rune */
3435                 c_ptr->info &= ~(CAVE_MARK);
3436
3437                 /* Break the rune */
3438                 c_ptr->info &= ~(CAVE_OBJECT);
3439                 c_ptr->mimic = 0;
3440
3441                 note_spot(y, x);
3442                 lite_spot(y, x);
3443         }
3444
3445         /* Success */
3446         return (TRUE);
3447 }
3448
3449
3450 /*
3451  *  improved version of scatter() for place monster
3452  */
3453
3454 #define MON_SCAT_MAXD 10
3455
3456 static bool mon_scatter(int r_idx, int *yp, int *xp, int y, int x, int max_dist)
3457 {
3458         int place_x[MON_SCAT_MAXD];
3459         int place_y[MON_SCAT_MAXD];
3460         int num[MON_SCAT_MAXD];
3461         int i;
3462         int nx, ny;
3463
3464         if (max_dist >= MON_SCAT_MAXD)
3465                 return FALSE;
3466
3467         for (i = 0; i < MON_SCAT_MAXD; i++)
3468                 num[i] = 0;
3469
3470         for (nx = x - max_dist; nx <= x + max_dist; nx++)
3471         {
3472                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
3473                 {
3474                         /* Ignore annoying locations */
3475                         if (!in_bounds(ny, nx)) continue;
3476
3477                         /* Require "line of projection" */
3478                         if (!projectable(y, x, ny, nx)) continue;
3479
3480                         if (r_idx > 0)
3481                         {
3482                                 monster_race *r_ptr = &r_info[r_idx];
3483
3484                                 /* Require empty space (if not ghostly) */
3485                                 if (!monster_can_enter(ny, nx, r_ptr, 0))
3486                                         continue;
3487                         }
3488                         else
3489                         {
3490                                 /* Walls and Monsters block flow */
3491                                 if (!cave_empty_bold2(ny, nx)) continue;
3492
3493                                 /* ... nor on the Pattern */
3494                                 if (pattern_tile(ny, nx)) continue;
3495                         }
3496
3497                         i = distance(y, x, ny, nx);
3498
3499                         if (i > max_dist)
3500                                 continue;
3501
3502                         num[i]++;
3503
3504                         /* random swap */
3505                         if (one_in_(num[i]))
3506                         {
3507                                 place_x[i] = nx;
3508                                 place_y[i] = ny;
3509                         }
3510                 }
3511         }
3512
3513         i = 0;
3514         while (i < MON_SCAT_MAXD && 0 == num[i])
3515                 i++;
3516         if (i >= MON_SCAT_MAXD)
3517                 return FALSE;
3518
3519         *xp = place_x[i];
3520         *yp = place_y[i];
3521
3522         return TRUE;
3523 }
3524
3525
3526 /*
3527  * Maximum size of a group of monsters
3528  */
3529 #define GROUP_MAX       32
3530
3531
3532 /*
3533  * Attempt to place a "group" of monsters around the given location
3534  */
3535 static bool place_monster_group(int who, int y, int x, int r_idx, u32b mode)
3536 {
3537         monster_race *r_ptr = &r_info[r_idx];
3538
3539         int n, i;
3540         int total = 0, extra = 0;
3541
3542         int hack_n = 0;
3543
3544         byte hack_y[GROUP_MAX];
3545         byte hack_x[GROUP_MAX];
3546
3547
3548         /* Pick a group size */
3549         total = randint1(10);
3550
3551         /* Hard monsters, small groups */
3552         if (r_ptr->level > dun_level)
3553         {
3554                 extra = r_ptr->level - dun_level;
3555                 extra = 0 - randint1(extra);
3556         }
3557
3558         /* Easy monsters, large groups */
3559         else if (r_ptr->level < dun_level)
3560         {
3561                 extra = dun_level - r_ptr->level;
3562                 extra = randint1(extra);
3563         }
3564
3565         /* Hack -- limit group reduction */
3566         if (extra > 9) extra = 9;
3567
3568         /* Modify the group size */
3569         total += extra;
3570
3571         /* Minimum size */
3572         if (total < 1) total = 1;
3573
3574         /* Maximum size */
3575         if (total > GROUP_MAX) total = GROUP_MAX;
3576
3577
3578         /* Start on the monster */
3579         hack_n = 1;
3580         hack_x[0] = x;
3581         hack_y[0] = y;
3582
3583         /* Puddle monsters, breadth first, up to total */
3584         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3585         {
3586                 /* Grab the location */
3587                 int hx = hack_x[n];
3588                 int hy = hack_y[n];
3589
3590                 /* Check each direction, up to total */
3591                 for (i = 0; (i < 8) && (hack_n < total); i++)
3592                 {
3593                         int mx, my;
3594
3595                         scatter(&my, &mx, hy, hx, 4, 0);
3596
3597                         /* Walls and Monsters block flow */
3598                         if (!cave_empty_bold2(my, mx)) continue;
3599
3600                         /* Attempt to place another monster */
3601                         if (place_monster_one(who, my, mx, r_idx, mode))
3602                         {
3603                                 /* Add it to the "hack" set */
3604                                 hack_y[hack_n] = my;
3605                                 hack_x[hack_n] = mx;
3606                                 hack_n++;
3607                         }
3608                 }
3609         }
3610
3611
3612         /* Success */
3613         return (TRUE);
3614 }
3615
3616
3617 /*
3618  * Hack -- help pick an escort type
3619  */
3620 static int place_monster_idx = 0;
3621 static int place_monster_m_idx = 0;
3622
3623 /*
3624  * Hack -- help pick an escort type
3625  */
3626 static bool place_monster_okay(int r_idx)
3627 {
3628         monster_race *r_ptr = &r_info[place_monster_idx];
3629         monster_type *m_ptr = &m_list[place_monster_m_idx];
3630
3631         monster_race *z_ptr = &r_info[r_idx];
3632
3633         /* Hack - Escorts have to have the same dungeon flag */
3634         if (mon_hook_dungeon(place_monster_idx) != mon_hook_dungeon(r_idx)) return (FALSE);
3635
3636         /* Require similar "race" */
3637         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3638
3639         /* Skip more advanced monsters */
3640         if (z_ptr->level > r_ptr->level) return (FALSE);
3641
3642         /* Skip unique monsters */
3643         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3644
3645         /* Paranoia -- Skip identical monsters */
3646         if (place_monster_idx == r_idx) return (FALSE);
3647
3648         /* Skip different alignment */
3649         if (monster_has_hostile_align(m_ptr, 0, 0, z_ptr)) return FALSE;
3650
3651         if (r_ptr->flags7 & RF7_FRIENDLY)
3652         {
3653                 if (monster_has_hostile_align(NULL, 1, -1, z_ptr)) return FALSE;
3654         }
3655
3656         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3657                 return FALSE;
3658
3659         /* Okay */
3660         return (TRUE);
3661 }
3662
3663
3664 /*
3665  * Attempt to place a monster of the given race at the given location
3666  *
3667  * Note that certain monsters are now marked as requiring "friends".
3668  * These monsters, if successfully placed, and if the "grp" parameter
3669  * is TRUE, will be surrounded by a "group" of identical monsters.
3670  *
3671  * Note that certain monsters are now marked as requiring an "escort",
3672  * which is a collection of monsters with similar "race" but lower level.
3673  *
3674  * Some monsters induce a fake "group" flag on their escorts.
3675  *
3676  * Note the "bizarre" use of non-recursion to prevent annoying output
3677  * when running a code profiler.
3678  *
3679  * Note the use of the new "monster allocation table" code to restrict
3680  * the "get_mon_num()" function to "legal" escort types.
3681  */
3682 bool place_monster_aux(int who, int y, int x, int r_idx, u32b mode)
3683 {
3684         int             i;
3685         monster_race    *r_ptr = &r_info[r_idx];
3686
3687         if (!(mode & PM_NO_KAGE) && one_in_(333))
3688                 mode |= PM_KAGE;
3689
3690         /* Place one monster, or fail */
3691         if (!place_monster_one(who, y, x, r_idx, mode)) return (FALSE);
3692
3693
3694         /* Require the "group" flag */
3695         if (!(mode & PM_ALLOW_GROUP)) return (TRUE);
3696
3697         place_monster_m_idx = hack_m_idx_ii;
3698
3699         /* Friends for certain monsters */
3700         if (r_ptr->flags1 & (RF1_FRIENDS))
3701         {
3702                 /* Attempt to place a group */
3703                 (void)place_monster_group(who, y, x, r_idx, mode);
3704         }
3705
3706
3707         /* Escorts for certain monsters */
3708         if (r_ptr->flags1 & (RF1_ESCORT))
3709         {
3710                 /* Set the escort index */
3711                 place_monster_idx = r_idx;
3712
3713                 /* Try to place several "escorts" */
3714                 for (i = 0; i < 32; i++)
3715                 {
3716                         int nx, ny, z, d = 3;
3717
3718                         /* Pick a location */
3719                         scatter(&ny, &nx, y, x, d, 0);
3720
3721                         /* Require empty grids */
3722                         if (!cave_empty_bold2(ny, nx)) continue;
3723
3724                         /* Prepare allocation table */
3725                         get_mon_num_prep(place_monster_okay, get_monster_hook2(ny, nx));
3726
3727                         /* Pick a random race */
3728                         z = get_mon_num(r_ptr->level);
3729
3730                         /* Handle failure */
3731                         if (!z) break;
3732
3733                         /* Place a single escort */
3734                         (void)place_monster_one(place_monster_m_idx, ny, nx, z, mode);
3735
3736                         /* Place a "group" of escorts if needed */
3737                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3738                             (r_ptr->flags1 & RF1_ESCORTS))
3739                         {
3740                                 /* Place a group of monsters */
3741                                 (void)place_monster_group(place_monster_m_idx, ny, nx, z, mode);
3742                         }
3743                 }
3744         }
3745
3746         /* Success */
3747         return (TRUE);
3748 }
3749
3750
3751 /*
3752  * Hack -- attempt to place a monster at the given location
3753  *
3754  * Attempt to find a monster appropriate to the "monster_level"
3755  */
3756 bool place_monster(int y, int x, u32b mode)
3757 {
3758         int r_idx;
3759
3760         /* Prepare allocation table */
3761         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3762
3763         /* Pick a monster */
3764         r_idx = get_mon_num(monster_level);
3765
3766         /* Handle failure */
3767         if (!r_idx) return (FALSE);
3768
3769         /* Attempt to place the monster */
3770         if (place_monster_aux(0, y, x, r_idx, mode)) return (TRUE);
3771
3772         /* Oops */
3773         return (FALSE);
3774 }
3775
3776
3777 #ifdef MONSTER_HORDES
3778
3779 bool alloc_horde(int y, int x)
3780 {
3781         monster_race *r_ptr = NULL;
3782         int r_idx = 0;
3783         int m_idx;
3784         int attempts = 1000;
3785         int cy = y;
3786         int cx = x;
3787
3788         /* Prepare allocation table */
3789         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3790
3791         while (--attempts)
3792         {
3793                 /* Pick a monster */
3794                 r_idx = get_mon_num(monster_level);
3795
3796                 /* Handle failure */
3797                 if (!r_idx) return (FALSE);
3798
3799                 r_ptr = &r_info[r_idx];
3800
3801                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3802
3803                 if (r_idx == MON_HAGURE) continue;
3804                 break;
3805         }
3806         if (attempts < 1) return FALSE;
3807
3808         attempts = 1000;
3809
3810         while (--attempts)
3811         {
3812                 /* Attempt to place the monster */
3813                 if (place_monster_aux(0, y, x, r_idx, 0L)) break;
3814         }
3815
3816         if (attempts < 1) return FALSE;
3817
3818         m_idx = cave[y][x].m_idx;
3819
3820         if (m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[m_list[m_idx].r_idx];
3821         summon_kin_type = r_ptr->d_char;
3822
3823         for (attempts = randint1(10) + 5; attempts; attempts--)
3824         {
3825                 scatter(&cy, &cx, y, x, 5, 0);
3826
3827                 (void)summon_specific(m_idx, cy, cx, dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
3828
3829                 y = cy;
3830                 x = cx;
3831         }
3832
3833         return TRUE;
3834 }
3835
3836 #endif /* MONSTER_HORDES */
3837
3838
3839 /*
3840  * Put the Guardian
3841  */
3842 bool alloc_guardian(bool def_val)
3843 {
3844         int guardian = d_info[dungeon_type].final_guardian;
3845
3846         if (guardian && (d_info[dungeon_type].maxdepth == dun_level) && (r_info[guardian].cur_num < r_info[guardian].max_num))
3847         {
3848                 int oy;
3849                 int ox;
3850                 int try = 4000;
3851
3852                 /* Find a good position */
3853                 while (try)
3854                 {
3855                         /* Get a random spot */
3856                         oy = randint1(cur_hgt - 4) + 2;
3857                         ox = randint1(cur_wid - 4) + 2;
3858
3859                         /* Is it a good spot ? */
3860                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[guardian], 0))
3861                         {
3862                                 /* Place the guardian */
3863                                 if (place_monster_aux(0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) return TRUE;
3864                         }
3865
3866                         /* One less try */
3867                         try--;
3868                 }
3869
3870                 return FALSE;
3871         }
3872
3873         return def_val;
3874 }
3875
3876
3877 /*
3878  * Attempt to allocate a random monster in the dungeon.
3879  *
3880  * Place the monster at least "dis" distance from the player.
3881  *
3882  * Use "slp" to choose the initial "sleep" status
3883  *
3884  * Use "monster_level" for the monster level
3885  */
3886 bool alloc_monster(int dis, u32b mode)
3887 {
3888         int                     y = 0, x = 0;
3889         int         attempts_left = 10000;
3890
3891         /* Put the Guardian */
3892         if (alloc_guardian(FALSE)) return TRUE;
3893
3894         /* Find a legal, distant, unoccupied, space */
3895         while (attempts_left--)
3896         {
3897                 /* Pick a location */
3898                 y = randint0(cur_hgt);
3899                 x = randint0(cur_wid);
3900
3901                 /* Require empty floor grid (was "naked") */
3902                 if (dun_level)
3903                 {
3904                         if (!cave_empty_bold2(y, x)) continue;
3905                 }
3906                 else
3907                 {
3908                         if (!cave_empty_bold(y, x)) continue;
3909                 }
3910
3911                 /* Accept far away grids */
3912                 if (distance(y, x, py, px) > dis) break;
3913         }
3914
3915         if (!attempts_left)
3916         {
3917                 if (cheat_xtra || cheat_hear)
3918                 {
3919 #ifdef JP
3920 msg_print("·Ù¹ð¡ª¿·¤¿¤Ê¥â¥ó¥¹¥¿¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡£¾®¤µ¤¤³¬¤Ç¤¹¤«¡©");
3921 #else
3922                         msg_print("Warning! Could not allocate a new monster. Small level?");
3923 #endif
3924
3925                 }
3926
3927                 return (FALSE);
3928         }
3929
3930
3931 #ifdef MONSTER_HORDES
3932         if (randint1(5000) <= dun_level)
3933         {
3934                 if (alloc_horde(y, x))
3935                 {
3936 #ifdef JP
3937                         if (cheat_hear) msg_format("¥â¥ó¥¹¥¿¡¼¤ÎÂç·²(%c)", summon_kin_type);
3938 #else
3939                         if (cheat_hear) msg_format("Monster horde (%c).", summon_kin_type);
3940 #endif
3941
3942                         return (TRUE);
3943                 }
3944         }
3945         else
3946         {
3947 #endif /* MONSTER_HORDES */
3948
3949                 /* Attempt to place the monster, allow groups */
3950                 if (place_monster(y, x, (mode | PM_ALLOW_GROUP))) return (TRUE);
3951
3952 #ifdef MONSTER_HORDES
3953         }
3954 #endif /* MONSTER_HORDES */
3955
3956         /* Nope */
3957         return (FALSE);
3958 }
3959
3960
3961
3962
3963 /*
3964  * Hack -- help decide if a monster race is "okay" to summon
3965  */
3966 static bool summon_specific_okay(int r_idx)
3967 {
3968         monster_race *r_ptr = &r_info[r_idx];
3969
3970         /* Hack - Only summon dungeon monsters */
3971         if (!mon_hook_dungeon(r_idx)) return (FALSE);
3972
3973         /* Hack -- identify the summoning monster */
3974         if (summon_specific_who > 0)
3975         {
3976                 monster_type *m_ptr = &m_list[summon_specific_who];
3977
3978                 /* Do not summon enemies */
3979
3980                 /* Friendly vs. opposite aligned normal or pet */
3981                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
3982         }
3983         /* Use the player's alignment */
3984         else if (summon_specific_who < 0)
3985         {
3986                 /* Do not summon enemies of the pets */
3987                 if (monster_has_hostile_align(NULL, 10, -10, r_ptr))
3988                 {
3989                         if (!one_in_(ABS(p_ptr->align) / 2 + 1)) return FALSE;
3990                 }
3991         }
3992
3993         /* Hack -- no specific type specified */
3994         if (!summon_specific_type) return (TRUE);
3995
3996         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))) return FALSE;
3997
3998         if ((summon_specific_who < 0) &&
3999             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
4000             monster_has_hostile_align(NULL, 10, -10, r_ptr))
4001                 return FALSE;
4002
4003         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[dungeon_type].flags1 & DF1_CHAMELEON)) return TRUE;
4004
4005         return (summon_specific_aux(r_idx));
4006 }
4007
4008
4009 /*
4010  * Place a monster (of the specified "type") near the given
4011  * location.  Return TRUE if a monster was actually summoned.
4012  *
4013  * We will attempt to place the monster up to 10 times before giving up.
4014  *
4015  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
4016  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
4017  * Note: None of the other summon codes will ever summon Unique's.
4018  *
4019  * This function has been changed.  We now take the "monster level"
4020  * of the summoning monster as a parameter, and use that, along with
4021  * the current dungeon level, to help determine the level of the
4022  * desired monster.  Note that this is an upper bound, and also
4023  * tends to "prefer" monsters of that level.  Currently, we use
4024  * the average of the dungeon and monster levels, and then add
4025  * five to allow slight increases in monster power.
4026  *
4027  * Note that we use the new "monster allocation table" creation code
4028  * to restrict the "get_mon_num()" function to the set of "legal"
4029  * monsters, making this function much faster and more reliable.
4030  *
4031  * Note that this function may not succeed, though this is very rare.
4032  */
4033 bool summon_specific(int who, int y1, int x1, int lev, int type, u32b mode)
4034 {
4035         int x, y, r_idx;
4036
4037         if (p_ptr->inside_arena) return (FALSE);
4038
4039         if (!mon_scatter(0, &y, &x, y1, x1, 2)) return FALSE;
4040
4041         /* Save the summoner */
4042         summon_specific_who = who;
4043
4044         /* Save the "summon" type */
4045         summon_specific_type = type;
4046
4047         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
4048
4049         /* Prepare allocation table */
4050         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
4051
4052         /* Pick a monster, using the level calculation */
4053         r_idx = get_mon_num((dun_level + lev) / 2 + 5);
4054
4055         /* Handle failure */
4056         if (!r_idx)
4057         {
4058                 summon_specific_type = 0;
4059                 return (FALSE);
4060         }
4061
4062         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
4063
4064         /* Attempt to place the monster (awake, allow groups) */
4065         if (!place_monster_aux(who, y, x, r_idx, mode))
4066         {
4067                 summon_specific_type = 0;
4068                 return (FALSE);
4069         }
4070
4071         summon_specific_type = 0;
4072         /* Success */
4073         return (TRUE);
4074 }
4075
4076 /* A "dangerous" function, creates a pet of the specified type */
4077 bool summon_named_creature (int who, int oy, int ox, int r_idx, u32b mode)
4078 {
4079         int x, y;
4080
4081         /* Paranoia */
4082         /* if (!r_idx) return; */
4083
4084         /* Prevent illegal monsters */
4085         if (r_idx >= max_r_idx) return FALSE;
4086
4087         if (p_ptr->inside_arena) return FALSE;
4088
4089         if (!mon_scatter(r_idx, &y, &x, oy, ox, 2)) return FALSE;
4090
4091         /* Place it (allow groups) */
4092         return place_monster_aux(who, y, x, r_idx, (mode | PM_NO_KAGE));
4093 }
4094
4095
4096 /*
4097  * Let the given monster attempt to reproduce.
4098  *
4099  * Note that "reproduction" REQUIRES empty space.
4100  */
4101 bool multiply_monster(int m_idx, bool clone, u32b mode)
4102 {
4103         monster_type    *m_ptr = &m_list[m_idx];
4104
4105         int y, x;
4106
4107         if (!mon_scatter(m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
4108                 return FALSE;
4109
4110         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
4111
4112         /* Create a new monster (awake, no groups) */
4113         if (!place_monster_aux(m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
4114                 return FALSE;
4115
4116         /* Hack -- Transfer "clone" flag */
4117         if (clone || (m_ptr->smart & SM_CLONED))
4118         {
4119                 m_list[hack_m_idx_ii].smart |= SM_CLONED;
4120                 m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
4121         }
4122
4123         return TRUE;
4124 }
4125
4126
4127
4128
4129
4130 /*
4131  * Dump a message describing a monster's reaction to damage
4132  *
4133  * Technically should attempt to treat "Beholder"'s as jelly's
4134  */
4135 void message_pain(int m_idx, int dam)
4136 {
4137         long oldhp, newhp, tmp;
4138         int percentage;
4139
4140         monster_type *m_ptr = &m_list[m_idx];
4141         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4142
4143         char m_name[80];
4144
4145
4146         /* Get the monster name */
4147         monster_desc(m_name, m_ptr, 0);
4148
4149         /* Notice non-damage */
4150         if (dam == 0)
4151         {
4152 #ifdef JP
4153                 msg_format("%^s¤Ï¥À¥á¡¼¥¸¤ò¼õ¤±¤Æ¤¤¤Ê¤¤¡£", m_name);
4154 #else
4155                 msg_format("%^s is unharmed.", m_name);
4156 #endif
4157
4158                 return;
4159         }
4160
4161         /* Note -- subtle fix -CFT */
4162         newhp = (long)(m_ptr->hp);
4163         oldhp = newhp + (long)(dam);
4164         tmp = (newhp * 100L) / oldhp;
4165         percentage = (int)(tmp);
4166
4167
4168         /* Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs */
4169         if (my_strchr(",ejmvwQ", r_ptr->d_char))
4170         {
4171 #ifdef JP
4172                 if (percentage > 95)
4173                         msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4174                 else if (percentage > 75)
4175                         msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4176                 else if (percentage > 50)
4177                         msg_format("%^s¤Ï½Ì¤³¤Þ¤Ã¤¿¡£", m_name);
4178                 else if (percentage > 35)
4179                         msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4180                 else if (percentage > 20)
4181                         msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4182                 else if (percentage > 10)
4183                         msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4184                 else
4185                         msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4186 #else
4187                 if (percentage > 95)
4188                         msg_format("%^s barely notices.", m_name);
4189                 else if (percentage > 75)
4190                         msg_format("%^s flinches.", m_name);
4191                 else if (percentage > 50)
4192                         msg_format("%^s squelches.", m_name);
4193                 else if (percentage > 35)
4194                         msg_format("%^s quivers in pain.", m_name);
4195                 else if (percentage > 20)
4196                         msg_format("%^s writhes about.", m_name);
4197                 else if (percentage > 10)
4198                         msg_format("%^s writhes in agony.", m_name);
4199                 else
4200                         msg_format("%^s jerks limply.", m_name);
4201 #endif
4202
4203         }
4204
4205
4206         /* Fish */
4207         else if (my_strchr("l", r_ptr->d_char))
4208         {
4209                 if (percentage > 95)
4210 #ifdef JP
4211 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4212 #else
4213                         msg_format("%^s barely notices.", m_name);
4214 #endif
4215                 else if (percentage > 75)
4216 #ifdef JP
4217 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4218 #else
4219                         msg_format("%^s flinches.", m_name);
4220 #endif
4221                 else if (percentage > 50)
4222 #ifdef JP
4223 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4224 #else
4225                         msg_format("%^s hesitates.", m_name);
4226 #endif
4227                 else if (percentage > 35)
4228 #ifdef JP
4229 msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4230 #else
4231                         msg_format("%^s quivers in pain.", m_name);
4232 #endif
4233                 else if (percentage > 20)
4234 #ifdef JP
4235 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4236 #else
4237                         msg_format("%^s writhes about.", m_name);
4238 #endif
4239                 else if (percentage > 10)
4240 #ifdef JP
4241 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4242 #else
4243                         msg_format("%^s writhes in agony.", m_name);
4244 #endif
4245                 else
4246 #ifdef JP
4247 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4248 #else
4249                         msg_format("%^s jerks limply.", m_name);
4250 #endif
4251         }
4252
4253
4254         /* Golems, Walls, Doors, Stairs */
4255         else if (my_strchr("g#+<>", r_ptr->d_char))
4256         {
4257                 if (percentage > 95)
4258 #ifdef JP
4259 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4260 #else
4261                         msg_format("%^s ignores the attack.", m_name);
4262 #endif
4263                 else if (percentage > 75)
4264 #ifdef JP
4265 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4266 #else
4267                         msg_format("%^s shrugs off the attack.", m_name);
4268 #endif
4269                 else if (percentage > 50)
4270 #ifdef JP
4271 msg_format("%^s¤ÏÍëÌĤΤ褦¤ËËʤ¨¤¿¡£", m_name);
4272 #else
4273                         msg_format("%^s roars thunderously.", m_name);
4274 #endif
4275                 else if (percentage > 35)
4276 #ifdef JP
4277 msg_format("%^s¤Ï¶ì¤·¤²¤ËËʤ¨¤¿¡£", m_name);
4278 #else
4279                         msg_format("%^s rumbles.", m_name);
4280 #endif
4281                 else if (percentage > 20)
4282 #ifdef JP
4283 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4284 #else
4285                         msg_format("%^s grunts.", m_name);
4286 #endif
4287                 else if (percentage > 10)
4288 #ifdef JP
4289 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4290 #else
4291                         msg_format("%^s hesitates.", m_name);
4292 #endif
4293                 else
4294 #ifdef JP
4295 msg_format("%^s¤Ï¤¯¤·¤ã¤¯¤·¤ã¤Ë¤Ê¤Ã¤¿¡£", m_name);
4296 #else
4297                         msg_format("%^s crumples.", m_name);
4298 #endif
4299         }
4300
4301
4302         /* Snakes, Hydrae, Reptiles, Mimics */
4303         else if (my_strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char))
4304         {
4305                 if (percentage > 95)
4306 #ifdef JP
4307 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4308 #else
4309                         msg_format("%^s barely notices.", m_name);
4310 #endif
4311                 else if (percentage > 75)
4312 #ifdef JP
4313 msg_format("%^s¤Ï¥·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4314 #else
4315                         msg_format("%^s hisses.", m_name);
4316 #endif
4317                 else if (percentage > 50)
4318 #ifdef JP
4319 msg_format("%^s¤ÏÅܤäÆƬ¤ò¾å¤²¤¿¡£", m_name);
4320 #else
4321                         msg_format("%^s rears up in anger.", m_name);
4322 #endif
4323                 else if (percentage > 35)
4324 #ifdef JP
4325 msg_format("%^s¤ÏÌÔÁ³¤È°Ò³Å¤·¤¿¡£", m_name);
4326 #else
4327                         msg_format("%^s hisses furiously.", m_name);
4328 #endif
4329                 else if (percentage > 20)
4330 #ifdef JP
4331 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4332 #else
4333                         msg_format("%^s writhes about.", m_name);
4334 #endif
4335                 else if (percentage > 10)
4336 #ifdef JP
4337 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4338 #else
4339                         msg_format("%^s writhes in agony.", m_name);
4340 #endif
4341                 else
4342 #ifdef JP
4343 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4344 #else
4345                         msg_format("%^s jerks limply.", m_name);
4346 #endif
4347         }
4348
4349
4350         /* Felines */
4351         else if (my_strchr("f", r_ptr->d_char))
4352         {
4353                 if (percentage > 95)
4354 #ifdef JP
4355 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4356 #else
4357                         msg_format("%^s shrugs off the attack.", m_name);
4358 #endif
4359                 else if (percentage > 75)
4360 #ifdef JP
4361 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4362 #else
4363                         msg_format("%^s roars.", m_name);
4364 #endif
4365                 else if (percentage > 50)
4366 #ifdef JP
4367 msg_format("%^s¤ÏÅܤäÆËʤ¨¤¿¡£", m_name);
4368 #else
4369                         msg_format("%^s growls angrily.", m_name);
4370 #endif
4371                 else if (percentage > 35)
4372 #ifdef JP
4373 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4374 #else
4375                         msg_format("%^s hisses with pain.", m_name);
4376 #endif
4377                 else if (percentage > 20)
4378 #ifdef JP
4379 msg_format("%^s¤ÏÄˤߤǼ塹¤·¤¯ÌĤ¤¤¿¡£", m_name);
4380 #else
4381                         msg_format("%^s mewls in pain.", m_name);
4382 #endif
4383                 else if (percentage > 10)
4384 #ifdef JP
4385 msg_format("%^s¤Ï¶ìÄˤˤ¦¤á¤¤¤¿¡£", m_name);
4386 #else
4387                         msg_format("%^s hisses in agony.", m_name);
4388 #endif
4389                 else
4390 #ifdef JP
4391 msg_format("%s¤Ï°¥¤ì¤ÊÌĤ­À¼¤ò½Ð¤·¤¿¡£", m_name);
4392 #else
4393                         msg_format("%^s mewls pitifully.", m_name);
4394 #endif
4395         }
4396
4397
4398         /* Ants, Centipedes, Flies, Insects, Beetles, Spiders */
4399         else if (my_strchr("acFIKS", r_ptr->d_char))
4400         {
4401                 if (percentage > 95)
4402 #ifdef JP
4403 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4404 #else
4405                         msg_format("%^s ignores the attack.", m_name);
4406 #endif
4407                 else if (percentage > 75)
4408 #ifdef JP
4409 msg_format("%^s¤Ï¥­¡¼¥­¡¼ÌĤ¤¤¿¡£", m_name);
4410 #else
4411                         msg_format("%^s chitters.", m_name);
4412 #endif
4413
4414                 else if (percentage > 50)
4415 #ifdef JP
4416 msg_format("%^s¤Ï¥è¥í¥è¥íƨ¤²²ó¤Ã¤¿¡£", m_name);
4417 #else
4418                         msg_format("%^s scuttles about.", m_name);
4419 #endif
4420
4421                 else if (percentage > 35)
4422 #ifdef JP
4423 msg_format("%^s¤Ï¤¦¤ë¤µ¤¯ÌĤ¤¤¿¡£", m_name);
4424 #else
4425                         msg_format("%^s twitters.", m_name);
4426 #endif
4427
4428                 else if (percentage > 20)
4429 #ifdef JP
4430 msg_format("%^s¤ÏÄˤߤËáÛÚ»¤·¤¿¡£", m_name);
4431 #else
4432                         msg_format("%^s jerks in pain.", m_name);
4433 #endif
4434
4435                 else if (percentage > 10)
4436 #ifdef JP
4437 msg_format("%^s¤Ï¶ìÄˤÇáÛÚ»¤·¤¿¡£", m_name);
4438 #else
4439                         msg_format("%^s jerks in agony.", m_name);
4440 #endif
4441
4442                 else
4443 #ifdef JP
4444 msg_format("%^s¤Ï¥Ô¥¯¥Ô¥¯¤Ò¤­¤Ä¤Ã¤¿¡£", m_name);
4445 #else
4446                         msg_format("%^s twitches.", m_name);
4447 #endif
4448
4449         }
4450
4451
4452         /* Birds */
4453         else if (my_strchr("B", r_ptr->d_char))
4454         {
4455                 if (percentage > 95)
4456 #ifdef JP
4457 msg_format("%^s¤Ï¤µ¤¨¤º¤Ã¤¿¡£", m_name);
4458 #else
4459                         msg_format("%^s chirps.", m_name);
4460 #endif
4461
4462                 else if (percentage > 75)
4463 #ifdef JP
4464 msg_format("%^s¤Ï¥Ô¡¼¥Ô¡¼ÌĤ¤¤¿¡£", m_name);
4465 #else
4466                         msg_format("%^s twitters.", m_name);
4467 #endif
4468
4469                 else if (percentage > 50)
4470 #ifdef JP
4471 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ¤¤¿¡£", m_name);
4472 #else
4473                         msg_format("%^s squawks.", m_name);
4474 #endif
4475
4476                 else if (percentage > 35)
4477 #ifdef JP
4478 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4479 #else
4480                         msg_format("%^s chatters.", m_name);
4481 #endif
4482
4483                 else if (percentage > 20)
4484 #ifdef JP
4485 msg_format("%^s¤Ï¶ì¤·¤ó¤À¡£", m_name);
4486 #else
4487                         msg_format("%^s jeers.", m_name);
4488 #endif
4489
4490                 else if (percentage > 10)
4491 #ifdef JP
4492 msg_format("%^s¤Ï¤Î¤¿¤¦¤Á²ó¤Ã¤¿¡£", m_name);
4493 #else
4494                         msg_format("%^s flutters about.", m_name);
4495 #endif
4496
4497                 else
4498 #ifdef JP
4499 msg_format("%^s¤Ï¥­¡¼¥­¡¼¤ÈÌĤ­¶«¤ó¤À¡£", m_name);
4500 #else
4501                         msg_format("%^s squeaks.", m_name);
4502 #endif
4503
4504         }
4505
4506
4507         /* Dragons, Demons, High Undead */
4508         else if (my_strchr("duDLUW", r_ptr->d_char))
4509         {
4510                 if (percentage > 95)
4511 #ifdef JP
4512 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4513 #else
4514                         msg_format("%^s ignores the attack.", m_name);
4515 #endif
4516
4517                 else if (percentage > 75)
4518 #ifdef JP
4519 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4520 #else
4521                         msg_format("%^s flinches.", m_name);
4522 #endif
4523
4524                 else if (percentage > 50)
4525 #ifdef JP
4526 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4527 #else
4528                         msg_format("%^s hisses in pain.", m_name);
4529 #endif
4530
4531                 else if (percentage > 35)
4532 #ifdef JP
4533 msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4534 #else
4535                         msg_format("%^s snarls with pain.", m_name);
4536 #endif
4537
4538                 else if (percentage > 20)
4539 #ifdef JP
4540 msg_format("%^s¤ÏÄˤߤËËʤ¨¤¿¡£", m_name);
4541 #else
4542                         msg_format("%^s roars with pain.", m_name);
4543 #endif
4544
4545                 else if (percentage > 10)
4546 #ifdef JP
4547 msg_format("%^s¤Ï¶ì¤·¤²¤Ë¶«¤ó¤À¡£", m_name);
4548 #else
4549                         msg_format("%^s gasps.", m_name);
4550 #endif
4551
4552                 else
4553 #ifdef JP
4554 msg_format("%^s¤Ï¼å¡¹¤·¤¯¤¦¤Ê¤Ã¤¿¡£", m_name);
4555 #else
4556                         msg_format("%^s snarls feebly.", m_name);
4557 #endif
4558
4559         }
4560
4561
4562         /* Skeletons */
4563         else if (my_strchr("s", r_ptr->d_char))
4564         {
4565                 if (percentage > 95)
4566 #ifdef JP
4567 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4568 #else
4569                         msg_format("%^s ignores the attack.", m_name);
4570 #endif
4571
4572                 else if (percentage > 75)
4573 #ifdef JP
4574 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4575 #else
4576                         msg_format("%^s shrugs off the attack.", m_name);
4577 #endif
4578
4579                 else if (percentage > 50)
4580 #ifdef JP
4581 msg_format("%^s¤Ï¥«¥¿¥«¥¿¤È¾Ð¤Ã¤¿¡£", m_name);
4582 #else
4583                         msg_format("%^s rattles.", m_name);
4584 #endif
4585
4586                 else if (percentage > 35)
4587 #ifdef JP
4588 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4589 #else
4590                         msg_format("%^s stumbles.", m_name);
4591 #endif
4592
4593                 else if (percentage > 20)
4594 #ifdef JP
4595 msg_format("%^s¤Ï¥«¥¿¥«¥¿¸À¤Ã¤¿¡£", m_name);
4596 #else
4597                         msg_format("%^s rattles.", m_name);
4598 #endif
4599
4600                 else if (percentage > 10)
4601 #ifdef JP
4602 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4603 #else
4604                         msg_format("%^s staggers.", m_name);
4605 #endif
4606
4607                 else
4608 #ifdef JP
4609 msg_format("%^s¤Ï¥¬¥¿¥¬¥¿¸À¤Ã¤¿¡£", m_name);
4610 #else
4611                         msg_format("%^s clatters.", m_name);
4612 #endif
4613
4614         }
4615
4616
4617         /* Zombies */
4618         else if (my_strchr("z", r_ptr->d_char))
4619         {
4620                 if (percentage > 95)
4621 #ifdef JP
4622 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4623 #else
4624                         msg_format("%^s ignores the attack.", m_name);
4625 #endif
4626
4627                 else if (percentage > 75)
4628 #ifdef JP
4629 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4630 #else
4631                         msg_format("%^s shrugs off the attack.", m_name);
4632 #endif
4633
4634                 else if (percentage > 50)
4635 #ifdef JP
4636 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4637 #else
4638                         msg_format("%^s groans.", m_name);
4639 #endif
4640
4641                 else if (percentage > 35)
4642 #ifdef JP
4643 msg_format("%s¤Ï¶ì¤·¤²¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4644 #else
4645                         msg_format("%^s moans.", m_name);
4646 #endif
4647
4648                 else if (percentage > 20)
4649 #ifdef JP
4650 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4651 #else
4652                         msg_format("%^s hesitates.", m_name);
4653 #endif
4654
4655                 else if (percentage > 10)
4656 #ifdef JP
4657 msg_format("%^s¤Ï¤¦¤Ê¤Ã¤¿¡£", m_name);
4658 #else
4659                         msg_format("%^s grunts.", m_name);
4660 #endif
4661
4662                 else
4663 #ifdef JP
4664 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4665 #else
4666                         msg_format("%^s staggers.", m_name);
4667 #endif
4668
4669         }
4670
4671
4672         /* Ghosts */
4673         else if (my_strchr("G", r_ptr->d_char))
4674
4675         {
4676                 if (percentage > 95)
4677 #ifdef JP
4678 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4679 #else
4680                         msg_format("%^s ignores the attack.", m_name);
4681 #endif
4682
4683                 else if (percentage > 75)
4684 #ifdef JP
4685 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4686 #else
4687                         msg_format("%^s shrugs off the attack.", m_name);
4688 #endif
4689
4690                 else if (percentage > 50)
4691 #ifdef JP
4692 msg_format("%s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4693 #else
4694                         msg_format("%^s moans.", m_name);
4695 #endif
4696
4697                 else if (percentage > 35)
4698 #ifdef JP
4699 msg_format("%^s¤Ïµã¤­¤ï¤á¤¤¤¿¡£", m_name);
4700 #else
4701                         msg_format("%^s wails.", m_name);
4702 #endif
4703
4704                 else if (percentage > 20)
4705 #ifdef JP
4706 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4707 #else
4708                         msg_format("%^s howls.", m_name);
4709 #endif
4710
4711                 else if (percentage > 10)
4712 #ifdef JP
4713 msg_format("%s¤Ï¼å¡¹¤·¤¯¤¦¤á¤¤¤¿¡£", m_name);
4714 #else
4715                         msg_format("%^s moans softly.", m_name);
4716 #endif
4717
4718                 else
4719 #ifdef JP
4720 msg_format("%^s¤Ï¤«¤¹¤«¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4721 #else
4722                         msg_format("%^s sighs.", m_name);
4723 #endif
4724
4725         }
4726
4727
4728         /* Dogs and Hounds */
4729         else if (my_strchr("CZ", r_ptr->d_char))
4730         {
4731 #ifdef JP
4732                 if (percentage > 95)
4733                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4734                 else if (percentage > 75)
4735                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4736                 else if (percentage > 50)
4737                         msg_format("%^s¤ÏÄˤߤǥ­¥ã¥ó¥­¥ã¥óËʤ¨¤¿¡£", m_name);
4738                 else if (percentage > 35)
4739                         msg_format("%^s¤ÏÄˤߤÇÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4740                 else if (percentage > 20)
4741                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4742                 else if (percentage > 10)
4743                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4744                 else
4745                         msg_format("%^s¤Ï¼å¡¹¤·¤¯Ëʤ¨¤¿¡£", m_name);
4746 #else
4747                 if (percentage > 95)
4748                         msg_format("%^s shrugs off the attack.", m_name);
4749                 else if (percentage > 75)
4750                         msg_format("%^s snarls with pain.", m_name);
4751                 else if (percentage > 50)
4752                         msg_format("%^s yelps in pain.", m_name);
4753                 else if (percentage > 35)
4754                         msg_format("%^s howls in pain.", m_name);
4755                 else if (percentage > 20)
4756                         msg_format("%^s howls in agony.", m_name);
4757                 else if (percentage > 10)
4758                         msg_format("%^s writhes in agony.", m_name);
4759                 else
4760                         msg_format("%^s yelps feebly.", m_name);
4761 #endif
4762
4763         }
4764
4765         /* One type of monsters (ignore,squeal,shriek) */
4766         else if (my_strchr("Xbilqrt", r_ptr->d_char))
4767         {
4768 #ifdef JP
4769                 if (percentage > 95)
4770                         msg_format("%^s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4771                 else if (percentage > 75)
4772                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4773                 else if (percentage > 50)
4774                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4775                 else if (percentage > 35)
4776                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4777                 else if (percentage > 20)
4778                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4779                 else if (percentage > 10)
4780                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4781                 else
4782                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4783 #else
4784                 if (percentage > 95)
4785                         msg_format("%^s ignores the attack.", m_name);
4786                 else if (percentage > 75)
4787                         msg_format("%^s grunts with pain.", m_name);
4788                 else if (percentage > 50)
4789                         msg_format("%^s squeals in pain.", m_name);
4790                 else if (percentage > 35)
4791                         msg_format("%^s shrieks in pain.", m_name);
4792                 else if (percentage > 20)
4793                         msg_format("%^s shrieks in agony.", m_name);
4794                 else if (percentage > 10)
4795                         msg_format("%^s writhes in agony.", m_name);
4796                 else
4797                         msg_format("%^s cries out feebly.", m_name);
4798 #endif
4799
4800         }
4801
4802         /* Another type of monsters (shrug,cry,scream) */
4803         else
4804         {
4805 #ifdef JP
4806                 if (percentage > 95)
4807                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4808                 else if (percentage > 75)
4809                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4810                 else if (percentage > 50)
4811                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4812                 else if (percentage > 35)
4813                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4814                 else if (percentage > 20)
4815                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4816                 else if (percentage > 10)
4817                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4818                 else
4819                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4820 #else
4821                 if (percentage > 95)
4822                         msg_format("%^s shrugs off the attack.", m_name);
4823                 else if (percentage > 75)
4824                         msg_format("%^s grunts with pain.", m_name);
4825                 else if (percentage > 50)
4826                         msg_format("%^s cries out in pain.", m_name);
4827                 else if (percentage > 35)
4828                         msg_format("%^s screams in pain.", m_name);
4829                 else if (percentage > 20)
4830                         msg_format("%^s screams in agony.", m_name);
4831                 else if (percentage > 10)
4832                         msg_format("%^s writhes in agony.", m_name);
4833                 else
4834                         msg_format("%^s cries out feebly.", m_name);
4835 #endif
4836
4837         }
4838 }
4839
4840
4841 /*
4842  * Learn about an "observed" resistance.
4843  */
4844 void update_smart_learn(int m_idx, int what)
4845 {
4846         monster_type *m_ptr = &m_list[m_idx];
4847
4848         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4849
4850
4851         /* Not allowed to learn */
4852         if (!smart_learn) return;
4853
4854         /* Too stupid to learn anything */
4855         if (r_ptr->flags2 & (RF2_STUPID)) return;
4856
4857         /* Not intelligent, only learn sometimes */
4858         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
4859
4860
4861         /* XXX XXX XXX */
4862
4863         /* Analyze the knowledge */
4864         switch (what)
4865         {
4866         case DRS_ACID:
4867                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
4868                 if (IS_OPPOSE_ACID()) m_ptr->smart |= (SM_OPP_ACID);
4869                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
4870                 break;
4871
4872         case DRS_ELEC:
4873                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
4874                 if (IS_OPPOSE_ELEC()) m_ptr->smart |= (SM_OPP_ELEC);
4875                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
4876                 break;
4877
4878         case DRS_FIRE:
4879                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
4880                 if (IS_OPPOSE_FIRE()) m_ptr->smart |= (SM_OPP_FIRE);
4881                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
4882                 break;
4883
4884         case DRS_COLD:
4885                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
4886                 if (IS_OPPOSE_COLD()) m_ptr->smart |= (SM_OPP_COLD);
4887                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
4888                 break;
4889
4890         case DRS_POIS:
4891                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
4892                 if (IS_OPPOSE_POIS()) m_ptr->smart |= (SM_OPP_POIS);
4893                 break;
4894
4895
4896         case DRS_NETH:
4897                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
4898                 break;
4899
4900         case DRS_LITE:
4901                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
4902                 break;
4903
4904         case DRS_DARK:
4905                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
4906                 break;
4907
4908         case DRS_FEAR:
4909                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
4910                 break;
4911
4912         case DRS_CONF:
4913                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
4914                 break;
4915
4916         case DRS_CHAOS:
4917                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4918                 break;
4919
4920         case DRS_DISEN:
4921                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4922                 break;
4923
4924         case DRS_BLIND:
4925                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4926                 break;
4927
4928         case DRS_NEXUS:
4929                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4930                 break;
4931
4932         case DRS_SOUND:
4933                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4934                 break;
4935
4936         case DRS_SHARD:
4937                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4938                 break;
4939
4940         case DRS_FREE:
4941                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4942                 break;
4943
4944         case DRS_MANA:
4945                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4946                 break;
4947
4948         case DRS_REFLECT:
4949                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
4950                 break;
4951         }
4952 }
4953
4954
4955 /*
4956  * Place the player in the dungeon XXX XXX
4957  */
4958 bool player_place(int y, int x)
4959 {
4960         /* Paranoia XXX XXX */
4961         if (cave[y][x].m_idx != 0) return FALSE;
4962
4963         /* Save player location */
4964         py = y;
4965         px = x;
4966
4967         /* Success */
4968         return TRUE;
4969 }
4970
4971
4972 /*
4973  * Drop all items carried by a monster
4974  */
4975 void monster_drop_carried_objects(monster_type *m_ptr)
4976 {
4977         s16b this_o_idx, next_o_idx = 0;
4978         object_type forge;
4979         object_type *o_ptr;
4980         object_type *q_ptr;
4981
4982
4983         /* Drop objects being carried */
4984         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4985         {
4986                 /* Acquire object */
4987                 o_ptr = &o_list[this_o_idx];
4988
4989                 /* Acquire next object */
4990                 next_o_idx = o_ptr->next_o_idx;
4991
4992                 /* Get local object */
4993                 q_ptr = &forge;
4994
4995                 /* Copy the object */
4996                 object_copy(q_ptr, o_ptr);
4997
4998                 /* Forget monster */
4999                 q_ptr->held_m_idx = 0;
5000
5001                 /* Delete the object */
5002                 delete_object_idx(this_o_idx);
5003
5004                 /* Drop it */
5005                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
5006         }
5007
5008         /* Forget objects */
5009         m_ptr->hold_o_idx = 0;
5010 }