OSDN Git Service

Implement reinforcement system.
[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         (void)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         int                     r_idx;
1257         long            value, total;
1258         monster_race    *r_ptr;
1259         alloc_entry             *table = alloc_race_table;
1260
1261         int pls_kakuritu, pls_level;
1262         int hoge = mysqrt(level*10000L);
1263
1264         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
1265
1266         pls_kakuritu = MAX(NASTY_MON_MAX, NASTY_MON_BASE - ((dungeon_turn / (TURNS_PER_TICK * 2500L) - hoge / 10)));
1267         pls_level    = MIN(NASTY_MON_PLUS_MAX, 3 + dungeon_turn / (TURNS_PER_TICK * 20000L) - hoge / 40 + MIN(5, level / 10)) ;
1268
1269         if (d_info[dungeon_type].flags1 & DF1_MAZE)
1270         {
1271                 pls_kakuritu = MIN(pls_kakuritu / 2, pls_kakuritu - 10);
1272                 if (pls_kakuritu < 2) pls_kakuritu = 2;
1273                 pls_level += 2;
1274                 level += 3;
1275         }
1276
1277         /* Boost the level */
1278         if (!p_ptr->inside_battle && !(d_info[dungeon_type].flags1 & DF1_BEGINNER))
1279         {
1280                 /* Nightmare mode allows more out-of depth monsters */
1281                 if (ironman_nightmare && !randint0(pls_kakuritu))
1282                 {
1283                         /* What a bizarre calculation */
1284                         level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
1285                 }
1286                 else
1287                 {
1288                         /* Occasional "nasty" monster */
1289                         if (!randint0(pls_kakuritu))
1290                         {
1291                                 /* Pick a level bonus */
1292                                 level += pls_level;
1293                         }
1294                 }
1295         }
1296
1297         /* Reset total */
1298         total = 0L;
1299
1300         /* Process probabilities */
1301         for (i = 0; i < alloc_race_size; i++)
1302         {
1303                 /* Monsters are sorted by depth */
1304                 if (table[i].level > level) break;
1305
1306                 /* Default */
1307                 table[i].prob3 = 0;
1308
1309                 /* Access the "r_idx" of the chosen monster */
1310                 r_idx = table[i].index;
1311
1312                 /* Access the actual race */
1313                 r_ptr = &r_info[r_idx];
1314
1315                 if (!p_ptr->inside_battle && !chameleon_change_m_idx)
1316                 {
1317                         /* Hack -- "unique" monsters must be "unique" */
1318                         if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
1319                              (r_ptr->flags7 & (RF7_NAZGUL))) &&
1320                             (r_ptr->cur_num >= r_ptr->max_num))
1321                         {
1322                                 continue;
1323                         }
1324
1325                         if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
1326                             (r_ptr->cur_num >= 1))
1327                         {
1328                                 continue;
1329                         }
1330
1331                         if (r_idx == MON_BANORLUPART)
1332                         {
1333                                 if (r_info[MON_BANOR].cur_num > 0) continue;
1334                                 if (r_info[MON_LUPART].cur_num > 0) continue;
1335                         }
1336                 }
1337
1338                 /* Accept */
1339                 table[i].prob3 = table[i].prob2;
1340
1341                 /* Total */
1342                 total += table[i].prob3;
1343         }
1344
1345         /* No legal monsters */
1346         if (total <= 0) return (0);
1347
1348         /* Pick a monster */
1349         value = randint0(total);
1350
1351         /* Find the monster */
1352         for (i = 0; i < alloc_race_size; i++)
1353         {
1354                 /* Found the entry */
1355                 if (value < table[i].prob3) break;
1356
1357                 /* Decrement */
1358                 value = value - table[i].prob3;
1359         }
1360
1361         /* Power boost */
1362         p = randint0(100);
1363
1364         /* Try for a "harder" monster once (50%) or twice (10%) */
1365         if (p < 60)
1366         {
1367                 /* Save old */
1368                 j = i;
1369
1370                 /* Pick a monster */
1371                 value = randint0(total);
1372
1373                 /* Find the monster */
1374                 for (i = 0; i < alloc_race_size; i++)
1375                 {
1376                         /* Found the entry */
1377                         if (value < table[i].prob3) break;
1378
1379                         /* Decrement */
1380                         value = value - table[i].prob3;
1381                 }
1382
1383                 /* Keep the "best" one */
1384                 if (table[i].level < table[j].level) i = j;
1385         }
1386
1387         /* Try for a "harder" monster twice (10%) */
1388         if (p < 10)
1389         {
1390                 /* Save old */
1391                 j = i;
1392
1393                 /* Pick a monster */
1394                 value = randint0(total);
1395
1396                 /* Find the monster */
1397                 for (i = 0; i < alloc_race_size; i++)
1398                 {
1399                         /* Found the entry */
1400                         if (value < table[i].prob3) break;
1401
1402                         /* Decrement */
1403                         value = value - table[i].prob3;
1404                 }
1405
1406                 /* Keep the "best" one */
1407                 if (table[i].level < table[j].level) i = j;
1408         }
1409
1410         /* Result */
1411         return (table[i].index);
1412 }
1413
1414
1415
1416
1417
1418 /*
1419  * Build a string describing a monster in some way.
1420  *
1421  * We can correctly describe monsters based on their visibility.
1422  * We can force all monsters to be treated as visible or invisible.
1423  * We can build nominatives, objectives, possessives, or reflexives.
1424  * We can selectively pronominalize hidden, visible, or all monsters.
1425  * We can use definite or indefinite descriptions for hidden monsters.
1426  * We can use definite or indefinite descriptions for visible monsters.
1427  *
1428  * Pronominalization involves the gender whenever possible and allowed,
1429  * so that by cleverly requesting pronominalization / visibility, you
1430  * can get messages like "You hit someone.  She screams in agony!".
1431  *
1432  * Reflexives are acquired by requesting Objective plus Possessive.
1433  *
1434  * If no m_ptr arg is given (?), the monster is assumed to be hidden,
1435  * unless the "Assume Visible" mode is requested.
1436  *
1437  * If no r_ptr arg is given, it is extracted from m_ptr and r_info
1438  * If neither m_ptr nor r_ptr is given, the monster is assumed to
1439  * be neuter, singular, and hidden (unless "Assume Visible" is set),
1440  * in which case you may be in trouble... :-)
1441  *
1442  * I am assuming that no monster name is more than 70 characters long,
1443  * so that "char desc[80];" is sufficiently large for any result.
1444  *
1445  * Mode Flags:
1446  *  MD_OBJECTIVE      --> Objective (or Reflexive)
1447  *  MD_POSSESSIVE     --> Possessive (or Reflexive)
1448  *  MD_INDEF_HIDDEN   --> Use indefinites for hidden monsters ("something")
1449  *  MD_INDEF_VISIBLE  --> Use indefinites for visible monsters ("a kobold")
1450  *  MD_PRON_HIDDEN    --> Pronominalize hidden monsters
1451  *  MD_PRON_VISIBLE   --> Pronominalize visible monsters
1452  *  MD_ASSUME_HIDDEN  --> Assume the monster is hidden
1453  *  MD_ASSUME_VISIBLE --> Assume the monster is visible
1454  *  MD_TRUE_NAME      --> Chameleon's true name
1455  *  MD_IGNORE_HALLU   --> Ignore hallucination, and penetrate shape change
1456  *
1457  * Useful Modes:
1458  *  0x00 --> Full nominative name ("the kobold") or "it"
1459  *  MD_INDEF_HIDDEN --> Full nominative name ("the kobold") or "something"
1460  *  MD_ASSUME_VISIBLE --> Genocide resistance name ("the kobold")
1461  *  MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE --> Killing name ("a kobold")
1462  *  MD_PRON_VISIBLE | MD_POSSESSIVE
1463  *    --> Possessive, genderized if visable ("his") or "its"
1464  *  MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE
1465  *    --> Reflexive, genderized if visable ("himself") or "itself"
1466  */
1467 void monster_desc(char *desc, monster_type *m_ptr, int mode)
1468 {
1469         cptr            res;
1470         monster_race    *r_ptr;
1471
1472         cptr            name;
1473         char            buf[128];
1474         char            silly_name[1024];
1475         bool            seen, pron;
1476         bool            named = FALSE;
1477
1478         r_ptr = &r_info[m_ptr->ap_r_idx];
1479
1480         /* Mode of MD_TRUE_NAME will reveal Chameleon's true name */
1481         if (mode & MD_TRUE_NAME) name = (r_name + real_r_ptr(m_ptr)->name);
1482         else name = (r_name + r_ptr->name);
1483
1484         /* Are we hallucinating? (Idea from Nethack...) */
1485         if (p_ptr->image && !(mode & MD_IGNORE_HALLU))
1486         {
1487                 if (one_in_(2))
1488                 {
1489 #ifdef JP
1490                         if (!get_rnd_line("silly_j.txt", m_ptr->r_idx, silly_name))
1491 #else
1492                         if (!get_rnd_line("silly.txt", m_ptr->r_idx, silly_name))
1493 #endif
1494
1495                                 named = TRUE;
1496                 }
1497
1498                 if (!named)
1499                 {
1500                         monster_race *hallu_race;
1501
1502                         do
1503                         {
1504                                 hallu_race = &r_info[randint1(max_r_idx - 1)];
1505                         }
1506                         while (!hallu_race->name || (hallu_race->flags1 & RF1_UNIQUE));
1507
1508                         strcpy(silly_name, (r_name + hallu_race->name));
1509                 }
1510
1511                 /* Better not strcpy it, or we could corrupt r_info... */
1512                 name = silly_name;
1513         }
1514
1515         /* Can we "see" it (exists + forced, or visible + not unforced) */
1516         seen = (m_ptr && ((mode & MD_ASSUME_VISIBLE) || (!(mode & MD_ASSUME_HIDDEN) && m_ptr->ml)));
1517
1518         /* Sexed Pronouns (seen and allowed, or unseen and allowed) */
1519         pron = (m_ptr && ((seen && (mode & MD_PRON_VISIBLE)) || (!seen && (mode & MD_PRON_HIDDEN))));
1520
1521
1522         /* First, try using pronouns, or describing hidden monsters */
1523         if (!seen || pron)
1524         {
1525                 /* an encoding of the monster "sex" */
1526                 int kind = 0x00;
1527
1528                 /* Extract the gender (if applicable) */
1529                 if (r_ptr->flags1 & (RF1_FEMALE)) kind = 0x20;
1530                 else if (r_ptr->flags1 & (RF1_MALE)) kind = 0x10;
1531
1532                 /* Ignore the gender (if desired) */
1533                 if (!m_ptr || !pron) kind = 0x00;
1534
1535
1536                 /* Assume simple result */
1537 #ifdef JP
1538                 res = "²¿¤«";
1539 #else
1540                 res = "it";
1541 #endif
1542
1543
1544                 /* Brute force: split on the possibilities */
1545                 switch (kind + (mode & (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE)))
1546                 {
1547                         /* Neuter, or unknown */
1548 #ifdef JP
1549                         case 0x00:                                                    res = "²¿¤«"; break;
1550                         case 0x00 + (MD_OBJECTIVE):                                   res = "²¿¤«"; break;
1551                         case 0x00 + (MD_POSSESSIVE):                                  res = "²¿¤«¤Î"; break;
1552                         case 0x00 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "²¿¤«¼«¿È"; break;
1553                         case 0x00 + (MD_INDEF_HIDDEN):                                res = "²¿¤«"; break;
1554                         case 0x00 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "²¿¤«"; break;
1555                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "²¿¤«"; break;
1556                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "¤½¤ì¼«¿È"; break;
1557 #else
1558                         case 0x00:                                                    res = "it"; break;
1559                         case 0x00 + (MD_OBJECTIVE):                                   res = "it"; break;
1560                         case 0x00 + (MD_POSSESSIVE):                                  res = "its"; break;
1561                         case 0x00 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "itself"; break;
1562                         case 0x00 + (MD_INDEF_HIDDEN):                                res = "something"; break;
1563                         case 0x00 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "something"; break;
1564                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "something's"; break;
1565                         case 0x00 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "itself"; break;
1566 #endif
1567
1568
1569                         /* Male (assume human if vague) */
1570 #ifdef JP
1571                         case 0x10:                                                    res = "Èà"; break;
1572                         case 0x10 + (MD_OBJECTIVE):                                   res = "Èà"; break;
1573                         case 0x10 + (MD_POSSESSIVE):                                  res = "Èà¤Î"; break;
1574                         case 0x10 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "È༫¿È"; break;
1575                         case 0x10 + (MD_INDEF_HIDDEN):                                res = "狼"; break;
1576                         case 0x10 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "狼"; break;
1577                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "狼¤Î"; break;
1578                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "È༫¿È"; break;
1579 #else
1580                         case 0x10:                                                    res = "he"; break;
1581                         case 0x10 + (MD_OBJECTIVE):                                   res = "him"; break;
1582                         case 0x10 + (MD_POSSESSIVE):                                  res = "his"; break;
1583                         case 0x10 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "himself"; break;
1584                         case 0x10 + (MD_INDEF_HIDDEN):                                res = "someone"; break;
1585                         case 0x10 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "someone"; break;
1586                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "someone's"; break;
1587                         case 0x10 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "himself"; break;
1588 #endif
1589
1590
1591                         /* Female (assume human if vague) */
1592 #ifdef JP
1593                         case 0x20:                                                    res = "Èà½÷"; break;
1594                         case 0x20 + (MD_OBJECTIVE):                                   res = "Èà½÷"; break;
1595                         case 0x20 + (MD_POSSESSIVE):                                  res = "Èà½÷¤Î"; break;
1596                         case 0x20 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "Èà½÷¼«¿È"; break;
1597                         case 0x20 + (MD_INDEF_HIDDEN):                                res = "狼"; break;
1598                         case 0x20 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "狼"; break;
1599                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "狼¤Î"; break;
1600                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "Èà½÷¼«¿È"; break;
1601 #else
1602                         case 0x20:                                                    res = "she"; break;
1603                         case 0x20 + (MD_OBJECTIVE):                                   res = "her"; break;
1604                         case 0x20 + (MD_POSSESSIVE):                                  res = "her"; break;
1605                         case 0x20 + (MD_POSSESSIVE | MD_OBJECTIVE):                   res = "herself"; break;
1606                         case 0x20 + (MD_INDEF_HIDDEN):                                res = "someone"; break;
1607                         case 0x20 + (MD_INDEF_HIDDEN | MD_OBJECTIVE):                 res = "someone"; break;
1608                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE):                res = "someone's"; break;
1609                         case 0x20 + (MD_INDEF_HIDDEN | MD_POSSESSIVE | MD_OBJECTIVE): res = "herself"; break;
1610 #endif
1611                 }
1612
1613                 /* Copy the result */
1614                 (void)strcpy(desc, res);
1615         }
1616
1617
1618         /* Handle visible monsters, "reflexive" request */
1619         else if ((mode & (MD_POSSESSIVE | MD_OBJECTIVE)) == (MD_POSSESSIVE | MD_OBJECTIVE))
1620         {
1621                 /* The monster is visible, so use its gender */
1622 #ifdef JP
1623                 if (r_ptr->flags1 & (RF1_FEMALE)) strcpy(desc, "Èà½÷¼«¿È");
1624                 else if (r_ptr->flags1 & (RF1_MALE)) strcpy(desc, "È༫¿È");
1625                 else strcpy(desc, "¤½¤ì¼«¿È");
1626 #else
1627                 if (r_ptr->flags1 & RF1_FEMALE) strcpy(desc, "herself");
1628                 else if (r_ptr->flags1 & RF1_MALE) strcpy(desc, "himself");
1629                 else strcpy(desc, "itself");
1630 #endif
1631         }
1632
1633
1634         /* Handle all other visible monster requests */
1635         else
1636         {
1637                 /* Tanuki? */
1638                 if (is_pet(m_ptr) && !is_original_ap(m_ptr))
1639                 {
1640 #ifdef JP
1641                         char *t;
1642                         strcpy(buf, name);
1643                         t = buf;
1644                         while(strncmp(t, "¡Ù", 2) && *t) t++;
1645                         if (*t)
1646                         {
1647                                 *t = '\0';
1648                                 (void)sprintf(desc, "%s¡©¡Ù", buf);
1649                         }
1650                         else
1651                                 (void)sprintf(desc, "%s¡©", name);
1652 #else
1653                         (void)sprintf(desc, "%s?", name);
1654 #endif
1655                 }
1656                 else
1657
1658                 /* It could be a Unique */
1659                 if ((r_ptr->flags1 & RF1_UNIQUE) && !(p_ptr->image && !(mode & MD_IGNORE_HALLU)))
1660                 {
1661                         /* Start with the name (thus nominative and objective) */
1662                         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && !(mode & MD_TRUE_NAME))
1663                         {
1664 #ifdef JP
1665                                 char *t;
1666                                 strcpy(buf, name);
1667                                 t = buf;
1668                                 while (strncmp(t, "¡Ù", 2) && *t) t++;
1669                                 if (*t)
1670                                 {
1671                                         *t = '\0';
1672                                         (void)sprintf(desc, "%s¡©¡Ù", buf);
1673                                 }
1674                                 else
1675                                         (void)sprintf(desc, "%s¡©", name);
1676 #else
1677                                 (void)sprintf(desc, "%s?", name);
1678 #endif
1679                         }
1680
1681                         /* Inside monster arena, and it is not your mount */
1682                         else if (p_ptr->inside_battle &&
1683                                  !(p_ptr->riding && (&m_list[p_ptr->riding] == m_ptr)))
1684                         {
1685                                 /* It is a fake unique monster */
1686 #ifdef JP
1687                                 (void)sprintf(desc, "%s¤â¤É¤­", name);
1688 #else
1689                                 (void)sprintf(desc, "fake %s", name);
1690 #endif
1691                         }
1692
1693                         else
1694                         {
1695                                 (void)strcpy(desc, name);
1696                         }
1697                 }
1698
1699                 /* It could be an indefinite monster */
1700                 else if (mode & MD_INDEF_VISIBLE)
1701                 {
1702                         /* XXX Check plurality for "some" */
1703
1704                         /* Indefinite monsters need an indefinite article */
1705 #ifdef JP
1706                         (void)strcpy(desc, "");
1707 #else
1708                         (void)strcpy(desc, is_a_vowel(name[0]) ? "an " : "a ");
1709 #endif
1710
1711                         (void)strcat(desc, name);
1712                 }
1713
1714                 /* It could be a normal, definite, monster */
1715                 else
1716                 {
1717                         /* Definite monsters need a definite article */
1718                         if (is_pet(m_ptr))
1719 #ifdef JP
1720                                 (void)strcpy(desc, "¤¢¤Ê¤¿¤Î");
1721 #else
1722                                 (void)strcpy(desc, "your ");
1723 #endif
1724
1725                         else
1726 #ifdef JP
1727                                 (void)strcpy(desc, "");
1728 #else
1729                                 (void)strcpy(desc, "the ");
1730 #endif
1731
1732                         (void)strcat(desc, name);
1733                 }
1734
1735                 if (m_ptr->nickname)
1736                 {
1737 #ifdef JP
1738                         sprintf(buf,"¡Ö%s¡×",quark_str(m_ptr->nickname));
1739 #else
1740                         sprintf(buf," called %s",quark_str(m_ptr->nickname));
1741 #endif
1742                         strcat(desc,buf);
1743                 }
1744
1745                 if (p_ptr->riding && (&m_list[p_ptr->riding] == m_ptr))
1746                 {
1747 #ifdef JP
1748                         strcat(desc,"(¾èÇÏÃæ)");
1749 #else
1750                         strcat(desc,"(riding)");
1751 #endif
1752                 }
1753
1754                 if ((mode & MD_IGNORE_HALLU) && (m_ptr->mflag2 & MFLAG2_CHAMELEON))
1755                 {
1756                         if (r_ptr->flags1 & RF1_UNIQUE)
1757                         {
1758 #ifdef JP
1759                                 strcat(desc,"(¥«¥á¥ì¥ª¥ó¤Î²¦)");
1760 #else
1761                                 strcat(desc,"(Chameleon Lord)");
1762 #endif
1763                         }
1764                         else
1765                         {
1766 #ifdef JP
1767                                 strcat(desc,"(¥«¥á¥ì¥ª¥ó)");
1768 #else
1769                                 strcat(desc,"(Chameleon)");
1770 #endif
1771                         }
1772                 }
1773
1774                 if ((mode & MD_IGNORE_HALLU) && !is_original_ap(m_ptr))
1775                 {
1776                         strcat(desc, format("(%s)", r_name + r_info[m_ptr->r_idx].name));
1777                 }
1778
1779                 /* Handle the Possessive as a special afterthought */
1780                 if (mode & MD_POSSESSIVE)
1781                 {
1782                         /* XXX Check for trailing "s" */
1783                         
1784                         /* Simply append "apostrophe" and "s" */
1785 #ifdef JP
1786                         (void)strcat(desc, "¤Î");
1787 #else
1788                         (void)strcat(desc, "'s");
1789 #endif
1790                 }
1791         }
1792 }
1793
1794
1795
1796
1797 /*
1798  * Learn about a monster (by "probing" it)
1799  *
1800  * Return the number of new flags learnt.  -Mogami-
1801  */
1802 int lore_do_probe(int r_idx)
1803 {
1804         monster_race *r_ptr = &r_info[r_idx];
1805         int i, n = 0;
1806         byte tmp_byte;
1807
1808         /* Maximal info about awareness */
1809         if (r_ptr->r_wake != MAX_UCHAR) n++;
1810         if (r_ptr->r_ignore != MAX_UCHAR) n++;
1811         r_ptr->r_wake = r_ptr->r_ignore = MAX_UCHAR;
1812
1813         /* Observe "maximal" attacks */
1814         for (i = 0; i < 4; i++)
1815         {
1816                 /* Examine "actual" blows */
1817                 if (r_ptr->blow[i].effect || r_ptr->blow[i].method)
1818                 {
1819                         /* Maximal observations */
1820                         if (r_ptr->r_blows[i] != MAX_UCHAR) n++;
1821                         r_ptr->r_blows[i] = MAX_UCHAR;
1822                 }
1823         }
1824
1825         /* Maximal drops */
1826         tmp_byte =
1827                 (((r_ptr->flags1 & RF1_DROP_4D2) ? 8 : 0) +
1828                  ((r_ptr->flags1 & RF1_DROP_3D2) ? 6 : 0) +
1829                  ((r_ptr->flags1 & RF1_DROP_2D2) ? 4 : 0) +
1830                  ((r_ptr->flags1 & RF1_DROP_1D2) ? 2 : 0) +
1831                  ((r_ptr->flags1 & RF1_DROP_90)  ? 1 : 0) +
1832                  ((r_ptr->flags1 & RF1_DROP_60)  ? 1 : 0));
1833
1834         /* Only "valid" drops */
1835         if (!(r_ptr->flags1 & RF1_ONLY_GOLD))
1836         {
1837                 if (r_ptr->r_drop_item != tmp_byte) n++;
1838                 r_ptr->r_drop_item = tmp_byte;
1839         }
1840         if (!(r_ptr->flags1 & RF1_ONLY_ITEM))
1841         {
1842                 if (r_ptr->r_drop_gold != tmp_byte) n++;
1843                 r_ptr->r_drop_gold = tmp_byte;
1844         }
1845
1846         /* Observe many spells */
1847         if (r_ptr->r_cast_spell != MAX_UCHAR) n++;
1848         r_ptr->r_cast_spell = MAX_UCHAR;
1849
1850         /* Count unknown flags */
1851         for (i = 0; i < 32; i++)
1852         {
1853                 if (!(r_ptr->r_flags1 & (1L << i)) &&
1854                     (r_ptr->flags1 & (1L << i))) n++;
1855                 if (!(r_ptr->r_flags2 & (1L << i)) &&
1856                     (r_ptr->flags2 & (1L << i))) n++;
1857                 if (!(r_ptr->r_flags3 & (1L << i)) &&
1858                     (r_ptr->flags3 & (1L << i))) n++;
1859                 if (!(r_ptr->r_flags4 & (1L << i)) &&
1860                     (r_ptr->flags4 & (1L << i))) n++;
1861                 if (!(r_ptr->r_flags5 & (1L << i)) &&
1862                     (r_ptr->flags5 & (1L << i))) n++;
1863                 if (!(r_ptr->r_flags6 & (1L << i)) &&
1864                     (r_ptr->flags6 & (1L << i))) n++;
1865                 if (!(r_ptr->r_flagsr & (1L << i)) &&
1866                     (r_ptr->flagsr & (1L << i))) n++;
1867
1868                 /* r_flags7 is actually unused */
1869 #if 0
1870                 if (!(r_ptr->r_flags7 & (1L << i)) &&
1871                     (r_ptr->flags7 & (1L << i))) n++;
1872 #endif
1873         }
1874
1875         /* Know all the flags */
1876         r_ptr->r_flags1 = r_ptr->flags1;
1877         r_ptr->r_flags2 = r_ptr->flags2;
1878         r_ptr->r_flags3 = r_ptr->flags3;
1879         r_ptr->r_flags4 = r_ptr->flags4;
1880         r_ptr->r_flags5 = r_ptr->flags5;
1881         r_ptr->r_flags6 = r_ptr->flags6;
1882         r_ptr->r_flagsr = r_ptr->flagsr;
1883
1884         /* r_flags7 is actually unused */
1885         /* r_ptr->r_flags7 = r_ptr->flags7; */
1886
1887         /* Know about evolution */
1888         if (!(r_ptr->r_xtra1 & MR1_SINKA)) n++;
1889         r_ptr->r_xtra1 |= MR1_SINKA;
1890
1891         /* Update monster recall window */
1892         if (p_ptr->monster_race_idx == r_idx)
1893         {
1894                 /* Window stuff */
1895                 p_ptr->window |= (PW_MONSTER);
1896         }
1897
1898         /* Return the number of new flags learnt */
1899         return n;
1900 }
1901
1902
1903 /*
1904  * Take note that the given monster just dropped some treasure
1905  *
1906  * Note that learning the "GOOD"/"GREAT" flags gives information
1907  * about the treasure (even when the monster is killed for the first
1908  * time, such as uniques, and the treasure has not been examined yet).
1909  *
1910  * This "indirect" method is used to prevent the player from learning
1911  * exactly how much treasure a monster can drop from observing only
1912  * a single example of a drop.  This method actually observes how much
1913  * gold and items are dropped, and remembers that information to be
1914  * described later by the monster recall code.
1915  */
1916 void lore_treasure(int m_idx, int num_item, int num_gold)
1917 {
1918         monster_type *m_ptr = &m_list[m_idx];
1919
1920         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1921
1922         /* If the monster doesn't have original appearance, don't note */
1923         if (!is_original_ap(m_ptr)) return;
1924
1925         /* Note the number of things dropped */
1926         if (num_item > r_ptr->r_drop_item) r_ptr->r_drop_item = num_item;
1927         if (num_gold > r_ptr->r_drop_gold) r_ptr->r_drop_gold = num_gold;
1928
1929         /* Hack -- memorize the good/great flags */
1930         if (r_ptr->flags1 & (RF1_DROP_GOOD)) r_ptr->r_flags1 |= (RF1_DROP_GOOD);
1931         if (r_ptr->flags1 & (RF1_DROP_GREAT)) r_ptr->r_flags1 |= (RF1_DROP_GREAT);
1932
1933         /* Update monster recall window */
1934         if (p_ptr->monster_race_idx == m_ptr->r_idx)
1935         {
1936                 /* Window stuff */
1937                 p_ptr->window |= (PW_MONSTER);
1938         }
1939 }
1940
1941
1942
1943 void sanity_blast(monster_type *m_ptr, bool necro)
1944 {
1945         bool happened = FALSE;
1946         int power = 100;
1947
1948         if (p_ptr->inside_battle || !character_dungeon) return;
1949
1950         if (!necro)
1951         {
1952                 char            m_name[80];
1953                 monster_race    *r_ptr = &r_info[m_ptr->ap_r_idx];
1954
1955                 power = r_ptr->level / 2;
1956
1957                 monster_desc(m_name, m_ptr, 0);
1958
1959                 if (!(r_ptr->flags1 & RF1_UNIQUE))
1960                 {
1961                         if (r_ptr->flags1 & RF1_FRIENDS)
1962                         power /= 2;
1963                 }
1964                 else power *= 2;
1965
1966                 if (!hack_mind)
1967                         return; /* No effect yet, just loaded... */
1968
1969                 if (!m_ptr->ml)
1970                         return; /* Cannot see it for some reason */
1971
1972                 if (!(r_ptr->flags2 & RF2_ELDRITCH_HORROR))
1973                         return; /* oops */
1974
1975
1976
1977                 if (is_pet(m_ptr))
1978                         return; /* Pet eldritch horrors are safe most of the time */
1979
1980                 if (randint1(100) > power) return;
1981
1982                 if (saving_throw(p_ptr->skill_sav - power))
1983                 {
1984                         return; /* Save, no adverse effects */
1985                 }
1986
1987                 if (p_ptr->image)
1988                 {
1989                         /* Something silly happens... */
1990 #ifdef JP
1991                         msg_format("%s%s¤Î´é¤ò¸«¤Æ¤·¤Þ¤Ã¤¿¡ª",
1992                                 funny_desc[randint0(MAX_SAN_FUNNY)], m_name);
1993 #else
1994                         msg_format("You behold the %s visage of %s!",
1995                                 funny_desc[randint0(MAX_SAN_FUNNY)], m_name);
1996 #endif
1997
1998
1999                         if (one_in_(3))
2000                         {
2001                                 msg_print(funny_comments[randint0(MAX_SAN_COMMENT)]);
2002                                 p_ptr->image = p_ptr->image + randint1(r_ptr->level);
2003                         }
2004
2005                         return; /* Never mind; we can't see it clearly enough */
2006                 }
2007
2008                 /* Something frightening happens... */
2009 #ifdef JP
2010                 msg_format("%s%s¤Î´é¤ò¸«¤Æ¤·¤Þ¤Ã¤¿¡ª",
2011                         horror_desc[randint0(MAX_SAN_HORROR)], m_name);
2012 #else
2013                 msg_format("You behold the %s visage of %s!",
2014                         horror_desc[randint0(MAX_SAN_HORROR)], m_name);
2015 #endif
2016
2017                 r_ptr->r_flags2 |= RF2_ELDRITCH_HORROR;
2018
2019                 /* Demon characters are unaffected */
2020                 if (prace_is_(RACE_IMP) || prace_is_(RACE_DEMON) || (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_DEMON)) return;
2021                 if (p_ptr->wizard) return;
2022
2023                 /* Undead characters are 50% likely to be unaffected */
2024                 if (prace_is_(RACE_SKELETON) || prace_is_(RACE_ZOMBIE)
2025                         || prace_is_(RACE_VAMPIRE) || prace_is_(RACE_SPECTRE) ||
2026                     (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_UNDEAD))
2027                 {
2028                         if (saving_throw(25 + p_ptr->lev)) return;
2029                 }
2030         }
2031         else
2032         {
2033 #ifdef JP
2034 msg_print("¥Í¥¯¥í¥Î¥ß¥³¥ó¤òÆɤó¤ÇÀµµ¤¤ò¼º¤Ã¤¿¡ª");
2035 #else
2036                 msg_print("Your sanity is shaken by reading the Necronomicon!");
2037 #endif
2038
2039         }
2040
2041         if (!saving_throw(p_ptr->skill_sav - power)) /* Mind blast */
2042         {
2043                 if (!p_ptr->resist_conf)
2044                 {
2045                         (void)set_confused(p_ptr->confused + randint0(4) + 4);
2046                 }
2047                 if (!p_ptr->resist_chaos && one_in_(3))
2048                 {
2049                         (void)set_image(p_ptr->image + randint0(250) + 150);
2050                 }
2051                 return;
2052         }
2053
2054         if (!saving_throw(p_ptr->skill_sav - power)) /* Lose int & wis */
2055         {
2056                 do_dec_stat(A_INT);
2057                 do_dec_stat(A_WIS);
2058                 return;
2059         }
2060
2061         if (!saving_throw(p_ptr->skill_sav - power)) /* Brain smash */
2062         {
2063                 if (!p_ptr->resist_conf)
2064                 {
2065                         (void)set_confused(p_ptr->confused + randint0(4) + 4);
2066                 }
2067                 if (!p_ptr->free_act)
2068                 {
2069                         (void)set_paralyzed(p_ptr->paralyzed + randint0(4) + 4);
2070                 }
2071                 while (randint0(100) > p_ptr->skill_sav)
2072                         (void)do_dec_stat(A_INT);
2073                 while (randint0(100) > p_ptr->skill_sav)
2074                         (void)do_dec_stat(A_WIS);
2075                 if (!p_ptr->resist_chaos)
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)) /* Amnesia */
2083         {
2084
2085                 if (lose_all_info())
2086 #ifdef JP
2087 msg_print("¤¢¤Þ¤ê¤Î¶²ÉݤËÁ´¤Æ¤Î¤³¤È¤ò˺¤ì¤Æ¤·¤Þ¤Ã¤¿¡ª");
2088 #else
2089                         msg_print("You forget everything in your utmost terror!");
2090 #endif
2091
2092                 return;
2093         }
2094
2095         if (saving_throw(p_ptr->skill_sav - power))
2096         {
2097                 return;
2098         }
2099
2100         /* Else gain permanent insanity */
2101         if ((p_ptr->muta3 & MUT3_MORONIC) && /*(p_ptr->muta2 & MUT2_BERS_RAGE) &&*/
2102                 ((p_ptr->muta2 & MUT2_COWARDICE) || (p_ptr->resist_fear)) &&
2103                 ((p_ptr->muta2 & MUT2_HALLU) || (p_ptr->resist_chaos)))
2104         {
2105                 /* The poor bastard already has all possible insanities! */
2106                 return;
2107         }
2108
2109         while (!happened)
2110         {
2111                 switch (randint1(21))
2112                 {
2113                         case 1:
2114                                 if (!(p_ptr->muta3 & MUT3_MORONIC) && one_in_(5))
2115                                 {
2116                                         if ((p_ptr->stat_use[A_INT] < 4) && (p_ptr->stat_use[A_WIS] < 4))
2117                                         {
2118 #ifdef JP
2119 msg_print("¤¢¤Ê¤¿¤Ï´°àú¤ÊÇϼ¯¤Ë¤Ê¤Ã¤¿¤è¤¦¤Êµ¤¤¬¤·¤¿¡£¤·¤«¤·¤½¤ì¤Ï¸µ¡¹¤À¤Ã¤¿¡£");
2120 #else
2121                                                 msg_print("You turn into an utter moron!");
2122 #endif
2123                                         }
2124                                         else
2125                                         {
2126 #ifdef JP
2127 msg_print("¤¢¤Ê¤¿¤Ï´°àú¤ÊÇϼ¯¤Ë¤Ê¤Ã¤¿¡ª");
2128 #else
2129                                                 msg_print("You turn into an utter moron!");
2130 #endif
2131                                         }
2132
2133                                         if (p_ptr->muta3 & MUT3_HYPER_INT)
2134                                         {
2135 #ifdef JP
2136 msg_print("¤¢¤Ê¤¿¤ÎǾ¤ÏÀ¸ÂÎ¥³¥ó¥Ô¥å¡¼¥¿¤Ç¤Ï¤Ê¤¯¤Ê¤Ã¤¿¡£");
2137 #else
2138                                                 msg_print("Your brain is no longer a living computer.");
2139 #endif
2140
2141                                                 p_ptr->muta3 &= ~(MUT3_HYPER_INT);
2142                                         }
2143                                         p_ptr->muta3 |= MUT3_MORONIC;
2144                                         happened = TRUE;
2145                                 }
2146                                 break;
2147                         case 2:
2148                         case 3:
2149                         case 4:
2150                         case 5:
2151                         case 6:
2152                         case 7:
2153                         case 8:
2154                         case 9:
2155                         case 10:
2156                         case 11:
2157                                 if (!(p_ptr->muta2 & MUT2_COWARDICE) && !p_ptr->resist_fear)
2158                                 {
2159 #ifdef JP
2160 msg_print("¤¢¤Ê¤¿¤Ï¥Ñ¥é¥Î¥¤¥¢¤Ë¤Ê¤Ã¤¿¡ª");
2161 #else
2162                                         msg_print("You become paranoid!");
2163 #endif
2164
2165
2166                                         /* Duh, the following should never happen, but anyway... */
2167                                         if (p_ptr->muta3 & MUT3_FEARLESS)
2168                                         {
2169 #ifdef JP
2170 msg_print("¤¢¤Ê¤¿¤Ï¤â¤¦¶²¤ìÃΤ餺¤Ç¤Ï¤Ê¤¯¤Ê¤Ã¤¿¡£");
2171 #else
2172                                                 msg_print("You are no longer fearless.");
2173 #endif
2174
2175                                                 p_ptr->muta3 &= ~(MUT3_FEARLESS);
2176                                         }
2177
2178                                         p_ptr->muta2 |= MUT2_COWARDICE;
2179                                         happened = TRUE;
2180                                 }
2181                                 break;
2182                         case 12:
2183                         case 13:
2184                         case 14:
2185                         case 15:
2186                         case 16:
2187                         case 17:
2188                         case 18:
2189                         case 19:
2190                         case 20:
2191                         case 21:
2192                                 if (!(p_ptr->muta2 & MUT2_HALLU) && !p_ptr->resist_chaos)
2193                                 {
2194 #ifdef JP
2195 msg_print("¸¸³Ð¤ò¤Ò¤­µ¯¤³¤¹Àº¿ÀºøÍð¤Ë´Ù¤Ã¤¿¡ª");
2196 #else
2197                                         msg_print("You are afflicted by a hallucinatory insanity!");
2198 #endif
2199
2200                                         p_ptr->muta2 |= MUT2_HALLU;
2201                                         happened = TRUE;
2202                                 }
2203                                 break;
2204                         default:
2205                                 if (!(p_ptr->muta2 & MUT2_BERS_RAGE))
2206                                 {
2207 #ifdef JP
2208 msg_print("·ãÎõ¤Ê´¶¾ð¤Îȯºî¤Ë¤ª¤½¤ï¤ì¤ë¤è¤¦¤Ë¤Ê¤Ã¤¿¡ª");
2209 #else
2210                                         msg_print("You become subject to fits of berserk rage!");
2211 #endif
2212
2213                                         p_ptr->muta2 |= MUT2_BERS_RAGE;
2214                                         happened = TRUE;
2215                                 }
2216                                 break;
2217                 }
2218         }
2219
2220         p_ptr->update |= PU_BONUS;
2221         handle_stuff();
2222 }
2223
2224
2225 /*
2226  * This function updates the monster record of the given monster
2227  *
2228  * This involves extracting the distance to the player (if requested),
2229  * and then checking for visibility (natural, infravision, see-invis,
2230  * telepathy), updating the monster visibility flag, redrawing (or
2231  * erasing) the monster when its visibility changes, and taking note
2232  * of any interesting monster flags (cold-blooded, invisible, etc).
2233  *
2234  * Note the new "mflag" field which encodes several monster state flags,
2235  * including "view" for when the monster is currently in line of sight,
2236  * and "mark" for when the monster is currently visible via detection.
2237  *
2238  * The only monster fields that are changed here are "cdis" (the
2239  * distance from the player), "ml" (visible to the player), and
2240  * "mflag" (to maintain the "MFLAG_VIEW" flag).
2241  *
2242  * Note the special "update_monsters()" function which can be used to
2243  * call this function once for every monster.
2244  *
2245  * Note the "full" flag which requests that the "cdis" field be updated,
2246  * this is only needed when the monster (or the player) has moved.
2247  *
2248  * Every time a monster moves, we must call this function for that
2249  * monster, and update the distance, and the visibility.  Every time
2250  * the player moves, we must call this function for every monster, and
2251  * update the distance, and the visibility.  Whenever the player "state"
2252  * changes in certain ways ("blindness", "infravision", "telepathy",
2253  * and "see invisible"), we must call this function for every monster,
2254  * and update the visibility.
2255  *
2256  * Routines that change the "illumination" of a grid must also call this
2257  * function for any monster in that grid, since the "visibility" of some
2258  * monsters may be based on the illumination of their grid.
2259  *
2260  * Note that this function is called once per monster every time the
2261  * player moves.  When the player is running, this function is one
2262  * of the primary bottlenecks, along with "update_view()" and the
2263  * "process_monsters()" code, so efficiency is important.
2264  *
2265  * Note the optimized "inline" version of the "distance()" function.
2266  *
2267  * A monster is "visible" to the player if (1) it has been detected
2268  * by the player, (2) it is close to the player and the player has
2269  * telepathy, or (3) it is close to the player, and in line of sight
2270  * of the player, and it is "illuminated" by some combination of
2271  * infravision, torch light, or permanent light (invisible monsters
2272  * are only affected by "light" if the player can see invisible).
2273  *
2274  * Monsters which are not on the current panel may be "visible" to
2275  * the player, and their descriptions will include an "offscreen"
2276  * reference.  Currently, offscreen monsters cannot be targetted
2277  * or viewed directly, but old targets will remain set.  XXX XXX
2278  *
2279  * The player can choose to be disturbed by several things, including
2280  * "disturb_move" (monster which is viewable moves in some way), and
2281  * "disturb_near" (monster which is "easily" viewable moves in some
2282  * way).  Note that "moves" includes "appears" and "disappears".
2283  */
2284 void update_mon(int m_idx, bool full)
2285 {
2286         monster_type *m_ptr = &m_list[m_idx];
2287
2288         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2289
2290         bool do_disturb = disturb_move;
2291
2292         int d;
2293
2294         /* Current location */
2295         int fy = m_ptr->fy;
2296         int fx = m_ptr->fx;
2297
2298         /* Seen at all */
2299         bool flag = FALSE;
2300
2301         /* Seen by vision */
2302         bool easy = FALSE;
2303
2304         /* Non-Ninja player in the darkness */
2305         bool in_darkness = (d_info[dungeon_type].flags1 & DF1_DARKNESS) && !p_ptr->see_nocto;
2306
2307         /* Do disturb? */
2308         if (disturb_high)
2309         {
2310                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2311
2312                 if (ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)
2313                         do_disturb = TRUE;
2314         }
2315
2316         /* Compute distance */
2317         if (full)
2318         {
2319                 /* Distance components */
2320                 int dy = (py > fy) ? (py - fy) : (fy - py);
2321                 int dx = (px > fx) ? (px - fx) : (fx - px);
2322
2323                 /* Approximate distance */
2324                 d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
2325
2326                 /* Restrict distance */
2327                 if (d > 255) d = 255;
2328
2329                 if (!d) d = 1;
2330
2331                 /* Save the distance */
2332                 m_ptr->cdis = d;
2333         }
2334
2335         /* Extract distance */
2336         else
2337         {
2338                 /* Extract the distance */
2339                 d = m_ptr->cdis;
2340         }
2341
2342
2343         /* Detected */
2344         if (m_ptr->mflag2 & (MFLAG2_MARK)) flag = TRUE;
2345
2346
2347         /* Nearby */
2348         if (d <= (in_darkness ? MAX_SIGHT / 2 : MAX_SIGHT))
2349         {
2350                 if (!in_darkness || (d <= MAX_SIGHT / 4))
2351                 {
2352                         if (p_ptr->special_defense & KATA_MUSOU)
2353                         {
2354                                 /* Detectable */
2355                                 flag = TRUE;
2356
2357                                 if (is_original_ap(m_ptr) && !p_ptr->image)
2358                                 {
2359                                         /* Hack -- Memorize mental flags */
2360                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2361                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2362                                 }
2363                         }
2364
2365                         /* Basic telepathy */
2366                         /* Snipers get telepathy when they concentrate deeper */
2367                         else if (p_ptr->telepathy)
2368                         {
2369                                 /* Empty mind, no telepathy */
2370                                 if (r_ptr->flags2 & (RF2_EMPTY_MIND))
2371                                 {
2372                                         /* Memorize flags */
2373                                         if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags2 |= (RF2_EMPTY_MIND);
2374                                 }
2375
2376                                 /* Weird mind, occasional telepathy */
2377                                 else if (r_ptr->flags2 & (RF2_WEIRD_MIND))
2378                                 {
2379                                         /* One in ten individuals are detectable */
2380                                         if ((m_idx % 10) == 5)
2381                                         {
2382                                                 /* Detectable */
2383                                                 flag = TRUE;
2384
2385                                                 if (is_original_ap(m_ptr) && !p_ptr->image)
2386                                                 {
2387                                                         /* Memorize flags */
2388                                                         r_ptr->r_flags2 |= (RF2_WEIRD_MIND);
2389
2390                                                         /* Hack -- Memorize mental flags */
2391                                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2392                                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2393                                                 }
2394                                         }
2395                                 }
2396
2397                                 /* Normal mind, allow telepathy */
2398                                 else
2399                                 {
2400                                         /* Detectable */
2401                                         flag = TRUE;
2402
2403                                         if (is_original_ap(m_ptr) && !p_ptr->image)
2404                                         {
2405                                                 /* Hack -- Memorize mental flags */
2406                                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2407                                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2408                                         }
2409                                 }
2410                         }
2411
2412                         /* Magical sensing */
2413                         if ((p_ptr->esp_animal) && (r_ptr->flags3 & (RF3_ANIMAL)))
2414                         {
2415                                 flag = TRUE;
2416                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_ANIMAL);
2417                         }
2418
2419                         /* Magical sensing */
2420                         if ((p_ptr->esp_undead) && (r_ptr->flags3 & (RF3_UNDEAD)))
2421                         {
2422                                 flag = TRUE;
2423                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_UNDEAD);
2424                         }
2425
2426                         /* Magical sensing */
2427                         if ((p_ptr->esp_demon) && (r_ptr->flags3 & (RF3_DEMON)))
2428                         {
2429                                 flag = TRUE;
2430                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_DEMON);
2431                         }
2432
2433                         /* Magical sensing */
2434                         if ((p_ptr->esp_orc) && (r_ptr->flags3 & (RF3_ORC)))
2435                         {
2436                                 flag = TRUE;
2437                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_ORC);
2438                         }
2439
2440                         /* Magical sensing */
2441                         if ((p_ptr->esp_troll) && (r_ptr->flags3 & (RF3_TROLL)))
2442                         {
2443                                 flag = TRUE;
2444                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_TROLL);
2445                         }
2446
2447                         /* Magical sensing */
2448                         if ((p_ptr->esp_giant) && (r_ptr->flags3 & (RF3_GIANT)))
2449                         {
2450                                 flag = TRUE;
2451                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_GIANT);
2452                         }
2453
2454                         /* Magical sensing */
2455                         if ((p_ptr->esp_dragon) && (r_ptr->flags3 & (RF3_DRAGON)))
2456                         {
2457                                 flag = TRUE;
2458                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_DRAGON);
2459                         }
2460
2461                         /* Magical sensing */
2462                         if ((p_ptr->esp_human) && (r_ptr->flags2 & (RF2_HUMAN)))
2463                         {
2464                                 flag = TRUE;
2465                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags2 |= (RF2_HUMAN);
2466                         }
2467
2468                         /* Magical sensing */
2469                         if ((p_ptr->esp_evil) && (r_ptr->flags3 & (RF3_EVIL)))
2470                         {
2471                                 flag = TRUE;
2472                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_EVIL);
2473                         }
2474
2475                         /* Magical sensing */
2476                         if ((p_ptr->esp_good) && (r_ptr->flags3 & (RF3_GOOD)))
2477                         {
2478                                 flag = TRUE;
2479                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_GOOD);
2480                         }
2481
2482                         /* Magical sensing */
2483                         if ((p_ptr->esp_nonliving) &&
2484                             ((r_ptr->flags3 & (RF3_DEMON | RF3_UNDEAD | RF3_NONLIVING)) == RF3_NONLIVING))
2485                         {
2486                                 flag = TRUE;
2487                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags3 |= (RF3_NONLIVING);
2488                         }
2489
2490                         /* Magical sensing */
2491                         if ((p_ptr->esp_unique) && (r_ptr->flags1 & (RF1_UNIQUE)))
2492                         {
2493                                 flag = TRUE;
2494                                 if (is_original_ap(m_ptr) && !p_ptr->image) r_ptr->r_flags1 |= (RF1_UNIQUE);
2495                         }
2496                 }
2497
2498                 /* Normal line of sight, and not blind */
2499                 if (player_has_los_bold(fy, fx) && !p_ptr->blind)
2500                 {
2501                         bool do_invisible = FALSE;
2502                         bool do_cold_blood = FALSE;
2503
2504                         /* Snipers can see targets in darkness when they concentrate deeper */
2505                         if (p_ptr->concent >= CONCENT_RADAR_THRESHOLD)
2506                         {
2507                                 /* Easy to see */
2508                                 easy = flag = TRUE;
2509                         }
2510
2511                         /* Use "infravision" */
2512                         if (d <= p_ptr->see_infra)
2513                         {
2514                                 /* Handle "cold blooded" monsters */
2515                                 if ((r_ptr->flags2 & (RF2_COLD_BLOOD | RF2_AURA_FIRE)) == RF2_COLD_BLOOD)
2516                                 {
2517                                         /* Take note */
2518                                         do_cold_blood = TRUE;
2519                                 }
2520
2521                                 /* Handle "warm blooded" monsters */
2522                                 else
2523                                 {
2524                                         /* Easy to see */
2525                                         easy = flag = TRUE;
2526                                 }
2527                         }
2528
2529                         /* Use "illumination" */
2530                         if (player_can_see_bold(fy, fx))
2531                         {
2532                                 /* Handle "invisible" monsters */
2533                                 if (r_ptr->flags2 & (RF2_INVISIBLE))
2534                                 {
2535                                         /* Take note */
2536                                         do_invisible = TRUE;
2537
2538                                         /* See invisible */
2539                                         if (p_ptr->see_inv)
2540                                         {
2541                                                 /* Easy to see */
2542                                                 easy = flag = TRUE;
2543                                         }
2544                                 }
2545
2546                                 /* Handle "normal" monsters */
2547                                 else
2548                                 {
2549                                         /* Easy to see */
2550                                         easy = flag = TRUE;
2551                                 }
2552                         }
2553
2554                         /* Visible */
2555                         if (flag)
2556                         {
2557                                 if (is_original_ap(m_ptr) && !p_ptr->image)
2558                                 {
2559                                         /* Memorize flags */
2560                                         if (do_invisible) r_ptr->r_flags2 |= (RF2_INVISIBLE);
2561                                         if (do_cold_blood) r_ptr->r_flags2 |= (RF2_COLD_BLOOD);
2562                                 }
2563                         }
2564                 }
2565         }
2566
2567
2568         /* The monster is now visible */
2569         if (flag)
2570         {
2571                 /* It was previously unseen */
2572                 if (!m_ptr->ml)
2573                 {
2574                         /* Mark as visible */
2575                         m_ptr->ml = TRUE;
2576
2577                         /* Draw the monster */
2578                         lite_spot(fy, fx);
2579
2580                         /* Update health bar as needed */
2581                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2582                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2583
2584                         /* Hack -- Count "fresh" sightings */
2585                         if (!p_ptr->image)
2586                         {
2587                                 if ((m_ptr->ap_r_idx == MON_KAGE) && (r_info[MON_KAGE].r_sights < MAX_SHORT))
2588                                         r_info[MON_KAGE].r_sights++;
2589                                 else if (is_original_ap(m_ptr) && (r_ptr->r_sights < MAX_SHORT))
2590                                         r_ptr->r_sights++;
2591                         }
2592
2593                         /* Eldritch Horror */
2594                         if (r_info[m_ptr->ap_r_idx].flags2 & RF2_ELDRITCH_HORROR)
2595                         {
2596                                 sanity_blast(m_ptr, FALSE);
2597                         }
2598
2599                         /* Disturb on appearance */
2600                         if (disturb_near && (projectable(m_ptr->fy, m_ptr->fx, py, px) && projectable(py, px, m_ptr->fy, m_ptr->fx)))
2601                         {
2602                                 if (disturb_pets || is_hostile(m_ptr))
2603                                         disturb(1, 1);
2604                         }
2605                 }
2606         }
2607
2608         /* The monster is not visible */
2609         else
2610         {
2611                 /* It was previously seen */
2612                 if (m_ptr->ml)
2613                 {
2614                         /* Mark as not visible */
2615                         m_ptr->ml = FALSE;
2616
2617                         /* Erase the monster */
2618                         lite_spot(fy, fx);
2619
2620                         /* Update health bar as needed */
2621                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2622                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2623
2624                         /* Disturb on disappearance */
2625                         if (do_disturb)
2626                         {
2627                                 if (disturb_pets || is_hostile(m_ptr))
2628                                         disturb(1, 1);
2629                         }
2630                 }
2631         }
2632
2633
2634         /* The monster is now easily visible */
2635         if (easy)
2636         {
2637                 /* Change */
2638                 if (!(m_ptr->mflag & (MFLAG_VIEW)))
2639                 {
2640                         /* Mark as easily visible */
2641                         m_ptr->mflag |= (MFLAG_VIEW);
2642
2643                         /* Disturb on appearance */
2644                         if (do_disturb)
2645                         {
2646                                 if (disturb_pets || is_hostile(m_ptr))
2647                                         disturb(1, 1);
2648                         }
2649                 }
2650         }
2651
2652         /* The monster is not easily visible */
2653         else
2654         {
2655                 /* Change */
2656                 if (m_ptr->mflag & (MFLAG_VIEW))
2657                 {
2658                         /* Mark as not easily visible */
2659                         m_ptr->mflag &= ~(MFLAG_VIEW);
2660
2661                         /* Disturb on disappearance */
2662                         if (do_disturb)
2663                         {
2664                                 if (disturb_pets || is_hostile(m_ptr))
2665                                         disturb(1, 1);
2666                         }
2667                 }
2668         }
2669 }
2670
2671
2672 /*
2673  * This function simply updates all the (non-dead) monsters (see above).
2674  */
2675 void update_monsters(bool full)
2676 {
2677         int i;
2678
2679         /* Update each (live) monster */
2680         for (i = 1; i < m_max; i++)
2681         {
2682                 monster_type *m_ptr = &m_list[i];
2683
2684                 /* Skip dead monsters */
2685                 if (!m_ptr->r_idx) continue;
2686
2687                 /* Update the monster */
2688                 update_mon(i, full);
2689         }
2690 }
2691
2692
2693 /*
2694  * Hack -- the index of the summoning monster
2695  */
2696 static bool monster_hook_chameleon_lord(int r_idx)
2697 {
2698         monster_race *r_ptr = &r_info[r_idx];
2699         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2700         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2701
2702         if (!(r_ptr->flags1 & (RF1_UNIQUE))) return FALSE;
2703         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2704
2705         if (ABS(r_ptr->level - r_info[MON_CHAMELEON_K].level) > 5) return FALSE;
2706
2707         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))
2708                 return FALSE;
2709
2710         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2711
2712         /* Not born */
2713         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2714         {
2715                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
2716         }
2717
2718         /* Born now */
2719         else if (summon_specific_who > 0)
2720         {
2721                 if (monster_has_hostile_align(&m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2722         }
2723
2724         return TRUE;
2725 }
2726
2727 static bool monster_hook_chameleon(int r_idx)
2728 {
2729         monster_race *r_ptr = &r_info[r_idx];
2730         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2731         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2732
2733         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2734         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2735         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2736         
2737         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))
2738                 return FALSE;
2739
2740         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr, 0)) return FALSE;
2741
2742         /* Not born */
2743         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2744         {
2745                 if ((old_r_ptr->flags3 & RF3_GOOD) && !(r_ptr->flags3 & RF3_GOOD)) return FALSE;
2746                 if ((old_r_ptr->flags3 & RF3_EVIL) && !(r_ptr->flags3 & RF3_EVIL)) return FALSE;
2747                 if (!(old_r_ptr->flags3 & (RF3_GOOD | RF3_EVIL)) && (r_ptr->flags3 & (RF3_GOOD | RF3_EVIL))) return FALSE;
2748         }
2749
2750         /* Born now */
2751         else if (summon_specific_who > 0)
2752         {
2753                 if (monster_has_hostile_align(&m_list[summon_specific_who], 0, 0, r_ptr)) return FALSE;
2754         }
2755
2756         return (*(get_monster_hook()))(r_idx);
2757 }
2758
2759
2760 void choose_new_monster(int m_idx, bool born, int r_idx)
2761 {
2762         int oldmaxhp;
2763         monster_type *m_ptr = &m_list[m_idx];
2764         monster_race *r_ptr;
2765         char old_m_name[80];
2766         bool old_unique = FALSE;
2767         int old_r_idx = m_ptr->r_idx;
2768
2769         if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2770                 old_unique = TRUE;
2771         if (old_unique && (r_idx == MON_CHAMELEON)) r_idx = MON_CHAMELEON_K;
2772         r_ptr = &r_info[r_idx];
2773
2774         monster_desc(old_m_name, m_ptr, 0);
2775
2776         if (!r_idx)
2777         {
2778                 int level;
2779
2780                 chameleon_change_m_idx = m_idx;
2781                 if (old_unique)
2782                         get_mon_num_prep(monster_hook_chameleon_lord, NULL);
2783                 else
2784                         get_mon_num_prep(monster_hook_chameleon, NULL);
2785
2786                 if (old_unique)
2787                         level = r_info[MON_CHAMELEON_K].level;
2788                 else if (!dun_level)
2789                         level = wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].level;
2790                 else
2791                         level = dun_level;
2792
2793                 if (d_info[dungeon_type].flags1 & DF1_CHAMELEON) level+= 2+randint1(3);
2794
2795                 r_idx = get_mon_num(level);
2796                 r_ptr = &r_info[r_idx];
2797
2798                 chameleon_change_m_idx = 0;
2799
2800                 /* Paranoia */
2801                 if (!r_idx) return;
2802         }
2803
2804         if (is_pet(m_ptr)) check_pets_num_and_align(m_ptr, FALSE);
2805
2806         m_ptr->r_idx = r_idx;
2807         m_ptr->ap_r_idx = r_idx;
2808         update_mon(m_idx, FALSE);
2809         lite_spot(m_ptr->fy, m_ptr->fx);
2810
2811         if ((r_info[old_r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)) ||
2812             (r_ptr->flags7 & (RF7_LITE_MASK | RF7_DARK_MASK)))
2813                 p_ptr->update |= (PU_MON_LITE);
2814
2815         if (is_pet(m_ptr)) check_pets_num_and_align(m_ptr, TRUE);
2816
2817         if (born)
2818         {
2819                 /* Sub-alignment of a chameleon */
2820                 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
2821                 {
2822                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2823                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2824                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2825                 }
2826                 return;
2827         }
2828
2829         if (m_idx == p_ptr->riding)
2830         {
2831                 char m_name[80];
2832                 monster_desc(m_name, m_ptr, 0);
2833 #ifdef JP
2834                 msg_format("ÆÍÁ³%s¤¬ÊѿȤ·¤¿¡£", old_m_name);
2835 #else
2836                 msg_format("Suddenly, %s transforms!", old_m_name);
2837 #endif
2838                 if (!(r_ptr->flags7 & RF7_RIDING))
2839 #ifdef JP
2840                         if (rakuba(0, TRUE)) msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2841 #else
2842                         if (rakuba(0, TRUE)) msg_format("You have fallen from %s.", m_name);
2843 #endif
2844         }
2845
2846         /* Extract the monster base speed */
2847         m_ptr->mspeed = get_mspeed(r_ptr);
2848
2849         oldmaxhp = m_ptr->max_maxhp;
2850         /* Assign maximal hitpoints */
2851         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2852         {
2853                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2854         }
2855         else
2856         {
2857                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2858         }
2859
2860         /* Monsters have double hitpoints in Nightmare mode */
2861         if (ironman_nightmare)
2862         {
2863                 u32b hp = m_ptr->max_maxhp * 2L;
2864                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
2865         }
2866
2867         m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
2868         if (m_ptr->maxhp < 1) m_ptr->maxhp = 1;
2869         m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
2870         
2871         /* reset dealt_damage */
2872         m_ptr->dealt_damage = 0;
2873 }
2874
2875
2876 /*
2877  *  Hook for Tanuki
2878  */
2879 static bool monster_hook_tanuki(int r_idx)
2880 {
2881         monster_race *r_ptr = &r_info[r_idx];
2882
2883         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2884         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2885         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2886         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2887         
2888         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))
2889                 return FALSE;
2890
2891         return (*(get_monster_hook()))(r_idx);
2892 }
2893
2894
2895 /*
2896  *  Set initial racial appearance of a monster
2897  */
2898 static int initial_r_appearance(int r_idx)
2899 {
2900         int attempts = 1000;
2901
2902         int ap_r_idx;
2903         int min = MIN(base_level-5, 50);
2904
2905         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2906                 return r_idx;
2907
2908         get_mon_num_prep(monster_hook_tanuki, NULL);
2909
2910         while (--attempts)
2911         {
2912                 ap_r_idx = get_mon_num(base_level + 10);
2913                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2914         }
2915
2916         return r_idx;
2917 }
2918
2919
2920 /*
2921  * Get initial monster speed
2922  */
2923 byte get_mspeed(monster_race *r_ptr)
2924 {
2925         /* Extract the monster base speed */
2926         int mspeed = r_ptr->speed;
2927
2928         /* Hack -- small racial variety */
2929         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2930         {
2931                 /* Allow some small variation per monster */
2932                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2933                 if (i) mspeed += rand_spread(0, i);
2934         }
2935
2936         if (mspeed > 199) mspeed = 199;
2937
2938         return (byte)mspeed;
2939 }
2940
2941
2942 /*
2943  * Attempt to place a monster of the given race at the given location.
2944  *
2945  * To give the player a sporting chance, any monster that appears in
2946  * line-of-sight and is extremely dangerous can be marked as
2947  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2948  * which often (but not always) lets the player move before they do.
2949  *
2950  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2951  *
2952  * XXX XXX XXX Use special "here" and "dead" flags for unique monsters,
2953  * remove old "cur_num" and "max_num" fields.
2954  *
2955  * XXX XXX XXX Actually, do something similar for artifacts, to simplify
2956  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2957  *
2958  * This is the only function which may place a monster in the dungeon,
2959  * except for the savefile loading code.
2960  */
2961 static bool place_monster_one(int who, int y, int x, int r_idx, u32b mode)
2962 {
2963         /* Access the location */
2964         cave_type               *c_ptr = &cave[y][x];
2965
2966         monster_type    *m_ptr;
2967
2968         monster_race    *r_ptr = &r_info[r_idx];
2969
2970         cptr            name = (r_name + r_ptr->name);
2971
2972         int cmi;
2973
2974         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
2975         if (p_ptr->wild_mode) return FALSE;
2976
2977         /* Verify location */
2978         if (!in_bounds(y, x)) return (FALSE);
2979
2980         /* Paranoia */
2981         if (!r_idx) return (FALSE);
2982
2983         /* Paranoia */
2984         if (!r_ptr->name) return (FALSE);
2985
2986         if (!(mode & PM_IGNORE_TERRAIN))
2987         {
2988                 /* Not on the Pattern */
2989                 if (pattern_tile(y, x)) return FALSE;
2990
2991                 /* Require empty space (if not ghostly) */
2992                 if (!monster_can_enter(y, x, r_ptr, 0)) return FALSE;
2993         }
2994
2995         if (!p_ptr->inside_battle)
2996         {
2997                 /* Hack -- "unique" monsters must be "unique" */
2998                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
2999                      (r_ptr->flags7 & (RF7_NAZGUL))) &&
3000                     (r_ptr->cur_num >= r_ptr->max_num))
3001                 {
3002                         /* Cannot create */
3003                         return (FALSE);
3004                 }
3005
3006                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
3007                     (r_ptr->cur_num >= 1))
3008                 {
3009                         return (FALSE);
3010                 }
3011
3012                 if (r_idx == MON_BANORLUPART)
3013                 {
3014                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
3015                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
3016                 }
3017
3018                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
3019                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (dun_level < r_ptr->level) &&
3020                     (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
3021                 {
3022                         /* Cannot create */
3023                         return (FALSE);
3024                 }
3025         }
3026
3027         if (quest_number(dun_level))
3028         {
3029                 int hoge = quest_number(dun_level);
3030                 if ((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
3031                 {
3032                         if(r_idx == quest[hoge].r_idx)
3033                         {
3034                                 int number_mon, i2, j2;
3035                                 number_mon = 0;
3036
3037                                 /* Count all quest monsters */
3038                                 for (i2 = 0; i2 < cur_wid; ++i2)
3039                                         for (j2 = 0; j2 < cur_hgt; j2++)
3040                                                 if (cave[j2][i2].m_idx > 0)
3041                                                         if (m_list[cave[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
3042                                                                 number_mon++;
3043                                 if(number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
3044                                         return FALSE;
3045                         }
3046                 }
3047         }
3048
3049         if (is_glyph_grid(c_ptr))
3050         {
3051                 if (randint1(BREAK_GLYPH) < (r_ptr->level+20))
3052                 {
3053                         /* Describe observable breakage */
3054                         if (c_ptr->info & CAVE_MARK)
3055                         {
3056 #ifdef JP
3057 msg_print("¼é¤ê¤Î¥ë¡¼¥ó¤¬²õ¤ì¤¿¡ª");
3058 #else
3059                                 msg_print("The rune of protection is broken!");
3060 #endif
3061
3062                         }
3063
3064                         /* Forget the rune */
3065                         c_ptr->info &= ~(CAVE_MARK);
3066
3067                         /* Break the rune */
3068                         c_ptr->info &= ~(CAVE_OBJECT);
3069                         c_ptr->mimic = 0;
3070
3071                         /* Notice */
3072                         note_spot(y, x);
3073                 }
3074                 else return FALSE;
3075         }
3076
3077         /* Powerful monster */
3078         if (r_ptr->level > dun_level)
3079         {
3080                 /* Unique monsters */
3081                 if (r_ptr->flags1 & (RF1_UNIQUE))
3082                 {
3083                         /* Message for cheaters */
3084 #ifdef JP
3085                         if (cheat_hear) msg_format("¿¼ÁؤΥæ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3086 #else
3087                         if (cheat_hear) msg_format("Deep Unique (%s).", name);
3088 #endif
3089                 }
3090
3091                 /* Normal monsters */
3092                 else
3093                 {
3094                         /* Message for cheaters */
3095 #ifdef JP
3096                         if (cheat_hear) msg_format("¿¼ÁؤΥâ¥ó¥¹¥¿¡¼ (%s)¡£", name);
3097 #else
3098                         if (cheat_hear) msg_format("Deep Monster (%s).", name);
3099 #endif
3100                 }
3101         }
3102
3103         /* Note the monster */
3104         else if (r_ptr->flags1 & (RF1_UNIQUE))
3105         {
3106                 /* Unique monsters induce message */
3107 #ifdef JP
3108                 if (cheat_hear) msg_format("¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3109 #else
3110                 if (cheat_hear) msg_format("Unique (%s).", name);
3111 #endif
3112
3113         }
3114
3115         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL) || (r_ptr->level < 10)) mode &= ~PM_KAGE;
3116
3117         /* Make a new monster */
3118         c_ptr->m_idx = m_pop();
3119         hack_m_idx_ii = c_ptr->m_idx;
3120
3121         /* Mega-Hack -- catch "failure" */
3122         if (!c_ptr->m_idx) return (FALSE);
3123
3124
3125         /* Get a new monster record */
3126         m_ptr = &m_list[c_ptr->m_idx];
3127
3128         /* Save the race */
3129         m_ptr->r_idx = r_idx;
3130         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
3131
3132         /* No flags */
3133         m_ptr->mflag = 0;
3134         m_ptr->mflag2 = 0;
3135
3136         /* Hack -- Appearance transfer */
3137         if ((mode & PM_MULTIPLY) && (who > 0) && !is_original_ap(&m_list[who]))
3138         {
3139                 m_ptr->ap_r_idx = m_list[who].ap_r_idx;
3140
3141                 /* Hack -- Shadower spawns Shadower */
3142                 if (m_list[who].mflag2 & MFLAG2_KAGE) m_ptr->mflag2 |= MFLAG2_KAGE;
3143         }
3144
3145         /* Sub-alignment of a monster */
3146         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
3147                 m_ptr->sub_align = m_list[who].sub_align;
3148         else
3149         {
3150                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3151                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
3152                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
3153         }
3154
3155         /* Place the monster at the location */
3156         m_ptr->fy = y;
3157         m_ptr->fx = x;
3158
3159
3160         /* No "timed status" yet */
3161         for (cmi = 0; cmi < MAX_MTIMED; cmi++) m_ptr->mtimed[cmi] = 0;
3162
3163         /* Unknown distance */
3164         m_ptr->cdis = 0;
3165
3166         reset_target(m_ptr);
3167
3168         m_ptr->nickname = 0;
3169
3170         m_ptr->exp = 0;
3171
3172
3173         /* Your pet summons its pet. */
3174         if (who > 0 && is_pet(&m_list[who]))
3175         {
3176                 mode |= PM_FORCE_PET;
3177                 m_ptr->parent_m_idx = who;
3178         }
3179         else
3180         {
3181                 m_ptr->parent_m_idx = 0;
3182         }
3183
3184         if (r_ptr->flags7 & RF7_CHAMELEON)
3185         {
3186                 choose_new_monster(c_ptr->m_idx, TRUE, 0);
3187                 r_ptr = &r_info[m_ptr->r_idx];
3188                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
3189
3190                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
3191                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
3192                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3193         }
3194         else if ((mode & PM_KAGE) && !(mode & PM_FORCE_PET))
3195         {
3196                 m_ptr->ap_r_idx = MON_KAGE;
3197                 m_ptr->mflag2 |= MFLAG2_KAGE;
3198         }
3199
3200         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
3201
3202         /* Not visible */
3203         m_ptr->ml = FALSE;
3204
3205         /* Pet? */
3206         if (mode & PM_FORCE_PET)
3207         {
3208                 set_pet(m_ptr);
3209         }
3210         /* Friendly? */
3211         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
3212                  (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(who))
3213         {
3214                 if (!monster_has_hostile_align(NULL, 0, -1, r_ptr)) set_friendly(m_ptr);
3215         }
3216
3217         /* Assume no sleeping */
3218         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
3219
3220         /* Enforce sleeping if needed */
3221         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
3222         {
3223                 int val = r_ptr->sleep;
3224                 (void)set_monster_csleep(c_ptr->m_idx, (val * 2) + randint1(val * 10));
3225         }
3226
3227         /* Assign maximal hitpoints */
3228         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
3229         {
3230                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
3231         }
3232         else
3233         {
3234                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
3235         }
3236
3237         /* Monsters have double hitpoints in Nightmare mode */
3238         if (ironman_nightmare)
3239         {
3240                 u32b hp = m_ptr->max_maxhp * 2L;
3241
3242                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
3243         }
3244
3245         m_ptr->maxhp = m_ptr->max_maxhp;
3246
3247         /* And start out fully healthy */
3248         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
3249                 m_ptr->hp = m_ptr->maxhp / 2;
3250         else m_ptr->hp = m_ptr->maxhp;
3251         
3252         
3253         /* dealt damage is 0 at initial*/
3254         m_ptr->dealt_damage = 0;
3255
3256
3257         /* Extract the monster base speed */
3258         m_ptr->mspeed = get_mspeed(r_ptr);
3259
3260         if (mode & PM_HASTE) (void)set_monster_fast(c_ptr->m_idx, 100);
3261
3262         /* Give a random starting energy */
3263         if (!ironman_nightmare)
3264         {
3265                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
3266         }
3267         else
3268         {
3269                 /* Nightmare monsters are more prepared */
3270                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
3271         }
3272
3273         /* Force monster to wait for player, unless in Nightmare mode */
3274         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
3275         {
3276                 /* Monster is still being nice */
3277                 m_ptr->mflag |= (MFLAG_NICE);
3278
3279                 /* Must repair monsters */
3280                 repair_monsters = TRUE;
3281         }
3282
3283         /* Hack -- see "process_monsters()" */
3284         if (c_ptr->m_idx < hack_m_idx)
3285         {
3286                 /* Monster is still being born */
3287                 m_ptr->mflag |= (MFLAG_BORN);
3288         }
3289
3290
3291         if (r_ptr->flags7 & RF7_SELF_LD_MASK)
3292                 p_ptr->update |= (PU_MON_LITE);
3293         else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !MON_CSLEEP(m_ptr))
3294                 p_ptr->update |= (PU_MON_LITE);
3295
3296         /* Update the monster */
3297         update_mon(c_ptr->m_idx, TRUE);
3298
3299
3300         /* Count the monsters on the level */
3301         real_r_ptr(m_ptr)->cur_num++;
3302
3303         /*
3304          * Memorize location of the unique monster in saved floors.
3305          * A unique monster move from old saved floor.
3306          */
3307         if (character_dungeon &&
3308             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
3309                 real_r_ptr(m_ptr)->floor_id = p_ptr->floor_id;
3310
3311         /* Hack -- Count the number of "reproducers" */
3312         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
3313
3314         /* Hack -- Notice new multi-hued monsters */
3315         {
3316                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
3317                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
3318                         shimmer_monsters = TRUE;
3319         }
3320
3321         if (p_ptr->warning && character_dungeon)
3322         {
3323                 if (r_ptr->flags1 & RF1_UNIQUE)
3324                 {
3325                         cptr color;
3326                         object_type *o_ptr;
3327                         char o_name[MAX_NLEN];
3328
3329                         if (r_ptr->level > p_ptr->lev + 30)
3330 #ifdef JP
3331                                 color = "¹õ¤¯";
3332 #else
3333                                 color = "black";
3334 #endif
3335                         else if (r_ptr->level > p_ptr->lev + 15)
3336 #ifdef JP
3337                                 color = "»ç¿§¤Ë";
3338 #else
3339                                 color = "purple";
3340 #endif
3341                         else if (r_ptr->level > p_ptr->lev + 5)
3342 #ifdef JP
3343                                 color = "¥ë¥Ó¡¼¿§¤Ë";
3344 #else
3345                                 color = "deep red";
3346 #endif
3347                         else if (r_ptr->level > p_ptr->lev - 5)
3348 #ifdef JP
3349                                 color = "ÀÖ¤¯";
3350 #else
3351                                 color = "red";
3352 #endif
3353                         else if (r_ptr->level > p_ptr->lev - 15)
3354 #ifdef JP
3355                                 color = "¥Ô¥ó¥¯¿§¤Ë";
3356 #else
3357                                 color = "pink";
3358 #endif
3359                         else
3360 #ifdef JP
3361                                 color = "Çò¤¯";
3362 #else
3363                                 color = "white";
3364 #endif
3365
3366                         o_ptr = choose_warning_item();
3367                         if (o_ptr)
3368                         {
3369                                 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
3370 #ifdef JP
3371                                 msg_format("%s¤Ï%s¸÷¤Ã¤¿¡£", o_name, color);
3372 #else
3373                                 msg_format("%s glows %s.", o_name, color);
3374 #endif
3375                         }
3376                         else
3377                         {
3378 #ifdef JP
3379                                 msg_format("%s¸÷¤ëʪ¤¬Æ¬¤ËÉ⤫¤ó¤À¡£", color);
3380 #else
3381                                 msg_format("An %s image forms in your mind.");
3382 #endif
3383                         }
3384                 }
3385         }
3386
3387         if (is_explosive_rune_grid(c_ptr))
3388         {
3389                 /* Break the ward */
3390                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3391                 {
3392                         /* Describe observable breakage */
3393                         if (c_ptr->info & CAVE_MARK)
3394                         {
3395 #ifdef JP
3396 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3397 #else
3398                                 msg_print("The rune explodes!");
3399 #endif
3400
3401                                 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);
3402                         }
3403                 }
3404                 else
3405                 {
3406 #ifdef JP
3407 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3408 #else
3409                         msg_print("An explosive rune was disarmed.");
3410 #endif
3411                 }
3412
3413                 /* Forget the rune */
3414                 c_ptr->info &= ~(CAVE_MARK);
3415
3416                 /* Break the rune */
3417                 c_ptr->info &= ~(CAVE_OBJECT);
3418                 c_ptr->mimic = 0;
3419
3420                 note_spot(y, x);
3421                 lite_spot(y, x);
3422         }
3423
3424         /* Success */
3425         return (TRUE);
3426 }
3427
3428
3429 /*
3430  *  improved version of scatter() for place monster
3431  */
3432
3433 #define MON_SCAT_MAXD 10
3434
3435 static bool mon_scatter(int r_idx, int *yp, int *xp, int y, int x, int max_dist)
3436 {
3437         int place_x[MON_SCAT_MAXD];
3438         int place_y[MON_SCAT_MAXD];
3439         int num[MON_SCAT_MAXD];
3440         int i;
3441         int nx, ny;
3442
3443         if (max_dist >= MON_SCAT_MAXD)
3444                 return FALSE;
3445
3446         for (i = 0; i < MON_SCAT_MAXD; i++)
3447                 num[i] = 0;
3448
3449         for (nx = x - max_dist; nx <= x + max_dist; nx++)
3450         {
3451                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
3452                 {
3453                         /* Ignore annoying locations */
3454                         if (!in_bounds(ny, nx)) continue;
3455
3456                         /* Require "line of projection" */
3457                         if (!projectable(y, x, ny, nx)) continue;
3458
3459                         if (r_idx > 0)
3460                         {
3461                                 monster_race *r_ptr = &r_info[r_idx];
3462
3463                                 /* Require empty space (if not ghostly) */
3464                                 if (!monster_can_enter(ny, nx, r_ptr, 0))
3465                                         continue;
3466                         }
3467                         else
3468                         {
3469                                 /* Walls and Monsters block flow */
3470                                 if (!cave_empty_bold2(ny, nx)) continue;
3471
3472                                 /* ... nor on the Pattern */
3473                                 if (pattern_tile(ny, nx)) continue;
3474                         }
3475
3476                         i = distance(y, x, ny, nx);
3477
3478                         if (i > max_dist)
3479                                 continue;
3480
3481                         num[i]++;
3482
3483                         /* random swap */
3484                         if (one_in_(num[i]))
3485                         {
3486                                 place_x[i] = nx;
3487                                 place_y[i] = ny;
3488                         }
3489                 }
3490         }
3491
3492         i = 0;
3493         while (i < MON_SCAT_MAXD && 0 == num[i])
3494                 i++;
3495         if (i >= MON_SCAT_MAXD)
3496                 return FALSE;
3497
3498         *xp = place_x[i];
3499         *yp = place_y[i];
3500
3501         return TRUE;
3502 }
3503
3504
3505 /*
3506  * Maximum size of a group of monsters
3507  */
3508 #define GROUP_MAX       32
3509
3510
3511 /*
3512  * Attempt to place a "group" of monsters around the given location
3513  */
3514 static bool place_monster_group(int who, int y, int x, int r_idx, u32b mode)
3515 {
3516         monster_race *r_ptr = &r_info[r_idx];
3517
3518         int n, i;
3519         int total = 0, extra = 0;
3520
3521         int hack_n = 0;
3522
3523         byte hack_y[GROUP_MAX];
3524         byte hack_x[GROUP_MAX];
3525
3526
3527         /* Pick a group size */
3528         total = randint1(10);
3529
3530         /* Hard monsters, small groups */
3531         if (r_ptr->level > dun_level)
3532         {
3533                 extra = r_ptr->level - dun_level;
3534                 extra = 0 - randint1(extra);
3535         }
3536
3537         /* Easy monsters, large groups */
3538         else if (r_ptr->level < dun_level)
3539         {
3540                 extra = dun_level - r_ptr->level;
3541                 extra = randint1(extra);
3542         }
3543
3544         /* Hack -- limit group reduction */
3545         if (extra > 9) extra = 9;
3546
3547         /* Modify the group size */
3548         total += extra;
3549
3550         /* Minimum size */
3551         if (total < 1) total = 1;
3552
3553         /* Maximum size */
3554         if (total > GROUP_MAX) total = GROUP_MAX;
3555
3556
3557         /* Start on the monster */
3558         hack_n = 1;
3559         hack_x[0] = x;
3560         hack_y[0] = y;
3561
3562         /* Puddle monsters, breadth first, up to total */
3563         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3564         {
3565                 /* Grab the location */
3566                 int hx = hack_x[n];
3567                 int hy = hack_y[n];
3568
3569                 /* Check each direction, up to total */
3570                 for (i = 0; (i < 8) && (hack_n < total); i++)
3571                 {
3572                         int mx, my;
3573
3574                         scatter(&my, &mx, hy, hx, 4, 0);
3575
3576                         /* Walls and Monsters block flow */
3577                         if (!cave_empty_bold2(my, mx)) continue;
3578
3579                         /* Attempt to place another monster */
3580                         if (place_monster_one(who, my, mx, r_idx, mode))
3581                         {
3582                                 /* Add it to the "hack" set */
3583                                 hack_y[hack_n] = my;
3584                                 hack_x[hack_n] = mx;
3585                                 hack_n++;
3586                         }
3587                 }
3588         }
3589
3590
3591         /* Success */
3592         return (TRUE);
3593 }
3594
3595
3596 /*
3597  * Hack -- help pick an escort type
3598  */
3599 static int place_monster_idx = 0;
3600 static int place_monster_m_idx = 0;
3601
3602 /*
3603  * Hack -- help pick an escort type
3604  */
3605 static bool place_monster_okay(int r_idx)
3606 {
3607         monster_race *r_ptr = &r_info[place_monster_idx];
3608         monster_type *m_ptr = &m_list[place_monster_m_idx];
3609
3610         monster_race *z_ptr = &r_info[r_idx];
3611
3612         /* Hack - Escorts have to have the same dungeon flag */
3613         if (mon_hook_dungeon(place_monster_idx) != mon_hook_dungeon(r_idx)) return (FALSE);
3614
3615         /* Require similar "race" */
3616         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3617
3618         /* Skip more advanced monsters */
3619         if (z_ptr->level > r_ptr->level) return (FALSE);
3620
3621         /* Skip unique monsters */
3622         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3623
3624         /* Paranoia -- Skip identical monsters */
3625         if (place_monster_idx == r_idx) return (FALSE);
3626
3627         /* Skip different alignment */
3628         if (monster_has_hostile_align(m_ptr, 0, 0, z_ptr)) return FALSE;
3629
3630         if (r_ptr->flags7 & RF7_FRIENDLY)
3631         {
3632                 if (monster_has_hostile_align(NULL, 1, -1, z_ptr)) return FALSE;
3633         }
3634
3635         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3636                 return FALSE;
3637
3638         /* Okay */
3639         return (TRUE);
3640 }
3641
3642
3643 /*
3644  * Attempt to place a monster of the given race at the given location
3645  *
3646  * Note that certain monsters are now marked as requiring "friends".
3647  * These monsters, if successfully placed, and if the "grp" parameter
3648  * is TRUE, will be surrounded by a "group" of identical monsters.
3649  *
3650  * Note that certain monsters are now marked as requiring an "escort",
3651  * which is a collection of monsters with similar "race" but lower level.
3652  *
3653  * Some monsters induce a fake "group" flag on their escorts.
3654  *
3655  * Note the "bizarre" use of non-recursion to prevent annoying output
3656  * when running a code profiler.
3657  *
3658  * Note the use of the new "monster allocation table" code to restrict
3659  * the "get_mon_num()" function to "legal" escort types.
3660  */
3661 bool place_monster_aux(int who, int y, int x, int r_idx, u32b mode)
3662 {
3663         int             i, j, n;
3664         monster_race    *r_ptr = &r_info[r_idx];
3665
3666         if (!(mode & PM_NO_KAGE) && one_in_(333))
3667                 mode |= PM_KAGE;
3668
3669         /* Place one monster, or fail */
3670         if (!place_monster_one(who, y, x, r_idx, mode)) return (FALSE);
3671
3672         /* Require the "group" flag */
3673         if (!(mode & PM_ALLOW_GROUP)) return (TRUE);
3674
3675         place_monster_m_idx = hack_m_idx_ii;
3676
3677         /* Reinforcement */
3678         for(i = 0; i < 6; i++)
3679         {
3680                 if(!r_ptr->reinforce_id[i]) break;
3681                 n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
3682                 for(j = 0; j < n; j++)
3683                 {
3684                         int nx, ny, z, d = 7;
3685                         scatter(&ny, &nx, y, x, d, 0);
3686                         (void)place_monster_one(place_monster_m_idx, ny, nx, r_ptr->reinforce_id[i], mode);
3687                 }
3688         }
3689
3690         /* Friends for certain monsters */
3691         if (r_ptr->flags1 & (RF1_FRIENDS))
3692         {
3693                 /* Attempt to place a group */
3694                 (void)place_monster_group(who, y, x, r_idx, mode);
3695         }
3696
3697         /* Escorts for certain monsters */
3698         if (r_ptr->flags1 & (RF1_ESCORT))
3699         {
3700                 /* Set the escort index */
3701                 place_monster_idx = r_idx;
3702
3703                 /* Try to place several "escorts" */
3704                 for (i = 0; i < 32; i++)
3705                 {
3706                         int nx, ny, z, d = 3;
3707
3708                         /* Pick a location */
3709                         scatter(&ny, &nx, y, x, d, 0);
3710
3711                         /* Require empty grids */
3712                         if (!cave_empty_bold2(ny, nx)) continue;
3713
3714                         /* Prepare allocation table */
3715                         get_mon_num_prep(place_monster_okay, get_monster_hook2(ny, nx));
3716
3717                         /* Pick a random race */
3718                         z = get_mon_num(r_ptr->level);
3719
3720                         /* Handle failure */
3721                         if (!z) break;
3722
3723                         /* Place a single escort */
3724                         (void)place_monster_one(place_monster_m_idx, ny, nx, z, mode);
3725
3726                         /* Place a "group" of escorts if needed */
3727                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3728                             (r_ptr->flags1 & RF1_ESCORTS))
3729                         {
3730                                 /* Place a group of monsters */
3731                                 (void)place_monster_group(place_monster_m_idx, ny, nx, z, mode);
3732                         }
3733                 }
3734         }
3735
3736         /* Success */
3737         return (TRUE);
3738 }
3739
3740
3741 /*
3742  * Hack -- attempt to place a monster at the given location
3743  *
3744  * Attempt to find a monster appropriate to the "monster_level"
3745  */
3746 bool place_monster(int y, int x, u32b mode)
3747 {
3748         int r_idx;
3749
3750         /* Prepare allocation table */
3751         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3752
3753         /* Pick a monster */
3754         r_idx = get_mon_num(monster_level);
3755
3756         /* Handle failure */
3757         if (!r_idx) return (FALSE);
3758
3759         /* Attempt to place the monster */
3760         if (place_monster_aux(0, y, x, r_idx, mode)) return (TRUE);
3761
3762         /* Oops */
3763         return (FALSE);
3764 }
3765
3766
3767 #ifdef MONSTER_HORDES
3768
3769 bool alloc_horde(int y, int x)
3770 {
3771         monster_race *r_ptr = NULL;
3772         int r_idx = 0;
3773         int m_idx;
3774         int attempts = 1000;
3775         int cy = y;
3776         int cx = x;
3777
3778         /* Prepare allocation table */
3779         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3780
3781         while (--attempts)
3782         {
3783                 /* Pick a monster */
3784                 r_idx = get_mon_num(monster_level);
3785
3786                 /* Handle failure */
3787                 if (!r_idx) return (FALSE);
3788
3789                 r_ptr = &r_info[r_idx];
3790
3791                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3792
3793                 if (r_idx == MON_HAGURE) continue;
3794                 break;
3795         }
3796         if (attempts < 1) return FALSE;
3797
3798         attempts = 1000;
3799
3800         while (--attempts)
3801         {
3802                 /* Attempt to place the monster */
3803                 if (place_monster_aux(0, y, x, r_idx, 0L)) break;
3804         }
3805
3806         if (attempts < 1) return FALSE;
3807
3808         m_idx = cave[y][x].m_idx;
3809
3810         if (m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[m_list[m_idx].r_idx];
3811         summon_kin_type = r_ptr->d_char;
3812
3813         for (attempts = randint1(10) + 5; attempts; attempts--)
3814         {
3815                 scatter(&cy, &cx, y, x, 5, 0);
3816
3817                 (void)summon_specific(m_idx, cy, cx, dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
3818
3819                 y = cy;
3820                 x = cx;
3821         }
3822
3823         return TRUE;
3824 }
3825
3826 #endif /* MONSTER_HORDES */
3827
3828
3829 /*
3830  * Put the Guardian
3831  */
3832 bool alloc_guardian(bool def_val)
3833 {
3834         int guardian = d_info[dungeon_type].final_guardian;
3835
3836         if (guardian && (d_info[dungeon_type].maxdepth == dun_level) && (r_info[guardian].cur_num < r_info[guardian].max_num))
3837         {
3838                 int oy;
3839                 int ox;
3840                 int try_count = 4000;
3841
3842                 /* Find a good position */
3843                 while (try_count)
3844                 {
3845                         /* Get a random spot */
3846                         oy = randint1(cur_hgt - 4) + 2;
3847                         ox = randint1(cur_wid - 4) + 2;
3848
3849                         /* Is it a good spot ? */
3850                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[guardian], 0))
3851                         {
3852                                 /* Place the guardian */
3853                                 if (place_monster_aux(0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) return TRUE;
3854                         }
3855
3856                         /* One less try count */
3857                         try_count--;
3858                 }
3859
3860                 return FALSE;
3861         }
3862
3863         return def_val;
3864 }
3865
3866
3867 /*
3868  * Attempt to allocate a random monster in the dungeon.
3869  *
3870  * Place the monster at least "dis" distance from the player.
3871  *
3872  * Use "slp" to choose the initial "sleep" status
3873  *
3874  * Use "monster_level" for the monster level
3875  */
3876 bool alloc_monster(int dis, u32b mode)
3877 {
3878         int                     y = 0, x = 0;
3879         int         attempts_left = 10000;
3880
3881         /* Put the Guardian */
3882         if (alloc_guardian(FALSE)) return TRUE;
3883
3884         /* Find a legal, distant, unoccupied, space */
3885         while (attempts_left--)
3886         {
3887                 /* Pick a location */
3888                 y = randint0(cur_hgt);
3889                 x = randint0(cur_wid);
3890
3891                 /* Require empty floor grid (was "naked") */
3892                 if (dun_level)
3893                 {
3894                         if (!cave_empty_bold2(y, x)) continue;
3895                 }
3896                 else
3897                 {
3898                         if (!cave_empty_bold(y, x)) continue;
3899                 }
3900
3901                 /* Accept far away grids */
3902                 if (distance(y, x, py, px) > dis) break;
3903         }
3904
3905         if (!attempts_left)
3906         {
3907                 if (cheat_xtra || cheat_hear)
3908                 {
3909 #ifdef JP
3910 msg_print("·Ù¹ð¡ª¿·¤¿¤Ê¥â¥ó¥¹¥¿¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡£¾®¤µ¤¤³¬¤Ç¤¹¤«¡©");
3911 #else
3912                         msg_print("Warning! Could not allocate a new monster. Small level?");
3913 #endif
3914
3915                 }
3916
3917                 return (FALSE);
3918         }
3919
3920
3921 #ifdef MONSTER_HORDES
3922         if (randint1(5000) <= dun_level)
3923         {
3924                 if (alloc_horde(y, x))
3925                 {
3926 #ifdef JP
3927                         if (cheat_hear) msg_format("¥â¥ó¥¹¥¿¡¼¤ÎÂç·²(%c)", summon_kin_type);
3928 #else
3929                         if (cheat_hear) msg_format("Monster horde (%c).", summon_kin_type);
3930 #endif
3931
3932                         return (TRUE);
3933                 }
3934         }
3935         else
3936         {
3937 #endif /* MONSTER_HORDES */
3938
3939                 /* Attempt to place the monster, allow groups */
3940                 if (place_monster(y, x, (mode | PM_ALLOW_GROUP))) return (TRUE);
3941
3942 #ifdef MONSTER_HORDES
3943         }
3944 #endif /* MONSTER_HORDES */
3945
3946         /* Nope */
3947         return (FALSE);
3948 }
3949
3950
3951
3952
3953 /*
3954  * Hack -- help decide if a monster race is "okay" to summon
3955  */
3956 static bool summon_specific_okay(int r_idx)
3957 {
3958         monster_race *r_ptr = &r_info[r_idx];
3959
3960         /* Hack - Only summon dungeon monsters */
3961         if (!mon_hook_dungeon(r_idx)) return (FALSE);
3962
3963         /* Hack -- identify the summoning monster */
3964         if (summon_specific_who > 0)
3965         {
3966                 monster_type *m_ptr = &m_list[summon_specific_who];
3967
3968                 /* Do not summon enemies */
3969
3970                 /* Friendly vs. opposite aligned normal or pet */
3971                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
3972         }
3973         /* Use the player's alignment */
3974         else if (summon_specific_who < 0)
3975         {
3976                 /* Do not summon enemies of the pets */
3977                 if (monster_has_hostile_align(NULL, 10, -10, r_ptr))
3978                 {
3979                         if (!one_in_(ABS(p_ptr->align) / 2 + 1)) return FALSE;
3980                 }
3981         }
3982
3983         /* Hack -- no specific type specified */
3984         if (!summon_specific_type) return (TRUE);
3985
3986         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))) return FALSE;
3987
3988         if ((summon_specific_who < 0) &&
3989             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
3990             monster_has_hostile_align(NULL, 10, -10, r_ptr))
3991                 return FALSE;
3992
3993         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[dungeon_type].flags1 & DF1_CHAMELEON)) return TRUE;
3994
3995         return (summon_specific_aux(r_idx));
3996 }
3997
3998
3999 /*
4000  * Place a monster (of the specified "type") near the given
4001  * location.  Return TRUE if a monster was actually summoned.
4002  *
4003  * We will attempt to place the monster up to 10 times before giving up.
4004  *
4005  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
4006  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
4007  * Note: None of the other summon codes will ever summon Unique's.
4008  *
4009  * This function has been changed.  We now take the "monster level"
4010  * of the summoning monster as a parameter, and use that, along with
4011  * the current dungeon level, to help determine the level of the
4012  * desired monster.  Note that this is an upper bound, and also
4013  * tends to "prefer" monsters of that level.  Currently, we use
4014  * the average of the dungeon and monster levels, and then add
4015  * five to allow slight increases in monster power.
4016  *
4017  * Note that we use the new "monster allocation table" creation code
4018  * to restrict the "get_mon_num()" function to the set of "legal"
4019  * monsters, making this function much faster and more reliable.
4020  *
4021  * Note that this function may not succeed, though this is very rare.
4022  */
4023 bool summon_specific(int who, int y1, int x1, int lev, int type, u32b mode)
4024 {
4025         int x, y, r_idx;
4026
4027         if (p_ptr->inside_arena) return (FALSE);
4028
4029         if (!mon_scatter(0, &y, &x, y1, x1, 2)) return FALSE;
4030
4031         /* Save the summoner */
4032         summon_specific_who = who;
4033
4034         /* Save the "summon" type */
4035         summon_specific_type = type;
4036
4037         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
4038
4039         /* Prepare allocation table */
4040         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
4041
4042         /* Pick a monster, using the level calculation */
4043         r_idx = get_mon_num((dun_level + lev) / 2 + 5);
4044
4045         /* Handle failure */
4046         if (!r_idx)
4047         {
4048                 summon_specific_type = 0;
4049                 return (FALSE);
4050         }
4051
4052         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
4053
4054         /* Attempt to place the monster (awake, allow groups) */
4055         if (!place_monster_aux(who, y, x, r_idx, mode))
4056         {
4057                 summon_specific_type = 0;
4058                 return (FALSE);
4059         }
4060
4061         summon_specific_type = 0;
4062         /* Success */
4063         return (TRUE);
4064 }
4065
4066 /* A "dangerous" function, creates a pet of the specified type */
4067 bool summon_named_creature (int who, int oy, int ox, int r_idx, u32b mode)
4068 {
4069         int x, y;
4070
4071         /* Paranoia */
4072         /* if (!r_idx) return; */
4073
4074         /* Prevent illegal monsters */
4075         if (r_idx >= max_r_idx) return FALSE;
4076
4077         if (p_ptr->inside_arena) return FALSE;
4078
4079         if (!mon_scatter(r_idx, &y, &x, oy, ox, 2)) return FALSE;
4080
4081         /* Place it (allow groups) */
4082         return place_monster_aux(who, y, x, r_idx, (mode | PM_NO_KAGE));
4083 }
4084
4085
4086 /*
4087  * Let the given monster attempt to reproduce.
4088  *
4089  * Note that "reproduction" REQUIRES empty space.
4090  */
4091 bool multiply_monster(int m_idx, bool clone, u32b mode)
4092 {
4093         monster_type    *m_ptr = &m_list[m_idx];
4094
4095         int y, x;
4096
4097         if (!mon_scatter(m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
4098                 return FALSE;
4099
4100         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
4101
4102         /* Create a new monster (awake, no groups) */
4103         if (!place_monster_aux(m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
4104                 return FALSE;
4105
4106         /* Hack -- Transfer "clone" flag */
4107         if (clone || (m_ptr->smart & SM_CLONED))
4108         {
4109                 m_list[hack_m_idx_ii].smart |= SM_CLONED;
4110                 m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
4111         }
4112
4113         return TRUE;
4114 }
4115
4116
4117
4118
4119
4120 /*
4121  * Dump a message describing a monster's reaction to damage
4122  *
4123  * Technically should attempt to treat "Beholder"'s as jelly's
4124  */
4125 void message_pain(int m_idx, int dam)
4126 {
4127         long oldhp, newhp, tmp;
4128         int percentage;
4129
4130         monster_type *m_ptr = &m_list[m_idx];
4131         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4132
4133         char m_name[80];
4134
4135         /* Get the monster name */
4136         monster_desc(m_name, m_ptr, 0);
4137
4138         if(dam == 0) // Notice non-damage
4139         {
4140 #ifdef JP
4141                 msg_format("%^s¤Ï¥À¥á¡¼¥¸¤ò¼õ¤±¤Æ¤¤¤Ê¤¤¡£", m_name);
4142 #else
4143                 msg_format("%^s is unharmed.", m_name);
4144 #endif
4145                 return;
4146         }
4147
4148         /* Note -- subtle fix -CFT */
4149         newhp = (long)(m_ptr->hp);
4150         oldhp = newhp + (long)(dam);
4151         tmp = (newhp * 100L) / oldhp;
4152         percentage = (int)(tmp);
4153
4154         if(my_strchr(",ejmvwQ", r_ptr->d_char)) // Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs
4155         {
4156 #ifdef JP
4157                 if(percentage > 95) msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4158                 else if(percentage > 75) msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4159                 else if(percentage > 50) msg_format("%^s¤Ï½Ì¤³¤Þ¤Ã¤¿¡£", m_name);
4160                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4161                 else if(percentage > 20) msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4162                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4163                 else msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4164 #else
4165                 if(percentage > 95) msg_format("%^s barely notices.", m_name);
4166                 else if(percentage > 75) msg_format("%^s flinches.", m_name);
4167                 else if(percentage > 50) msg_format("%^s squelches.", m_name);
4168                 else if(percentage > 35) msg_format("%^s quivers in pain.", m_name);
4169                 else if(percentage > 20) msg_format("%^s writhes about.", m_name);
4170                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
4171                 else msg_format("%^s jerks limply.", m_name);
4172 #endif
4173         }
4174
4175         else if(my_strchr("l", r_ptr->d_char)) // Fish
4176         {
4177 #ifdef JP
4178                 if(percentage > 95) msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4179                 else if(percentage > 75) msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4180                 else if(percentage > 50) msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4181                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4182                 else if(percentage > 20) msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4183                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4184                 else msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4185 #else
4186                 if(percentage > 95) msg_format("%^s barely notices.", m_name);
4187                 else if(percentage > 75) msg_format("%^s flinches.", m_name);
4188                 else if(percentage > 50) msg_format("%^s hesitates.", m_name);
4189                 else if(percentage > 35) msg_format("%^s quivers in pain.", m_name);
4190                 else if(percentage > 20) msg_format("%^s writhes about.", m_name);
4191                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
4192                 else msg_format("%^s jerks limply.", m_name);
4193 #endif          
4194         }
4195
4196         else if(my_strchr("g#+<>", r_ptr->d_char)) // Golems, Walls, Doors, Stairs
4197         {       
4198 #ifdef JP
4199                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4200                 else if(percentage > 75) msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4201                 else if(percentage > 50) msg_format("%^s¤ÏÍëÌĤΤ褦¤ËËʤ¨¤¿¡£", m_name);
4202                 else if(percentage > 35) msg_format("%^s¤Ï¶ì¤·¤²¤ËËʤ¨¤¿¡£", m_name);
4203                 else if(percentage > 20) msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4204                 else if(percentage > 10) msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4205                 else msg_format("%^s¤Ï¤¯¤·¤ã¤¯¤·¤ã¤Ë¤Ê¤Ã¤¿¡£", m_name);
4206 #else
4207                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
4208                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
4209                 else if(percentage > 50) msg_format("%^s roars thunderously.", m_name);
4210                 else if(percentage > 35) msg_format("%^s rumbles.", m_name);
4211                 else if(percentage > 20) msg_format("%^s grunts.", m_name);
4212                 else if(percentage > 10) msg_format("%^s hesitates.", m_name);
4213                 else msg_format("%^s crumples.", m_name);
4214 #endif
4215         }
4216
4217         else if(my_strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char)) // Snakes, Hydrae, Reptiles, Mimics
4218         {
4219 #ifdef JP
4220                 if(percentage > 95) msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4221                 else if(percentage > 75) msg_format("%^s¤Ï¥·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4222                 else if(percentage > 50) msg_format("%^s¤ÏÅܤäÆƬ¤ò¾å¤²¤¿¡£", m_name);
4223                 else if(percentage > 35) msg_format("%^s¤ÏÌÔÁ³¤È°Ò³Å¤·¤¿¡£", m_name);
4224                 else if(percentage > 20) msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4225                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4226                 else msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4227 #else
4228                 if(percentage > 95) msg_format("%^s barely notices.", m_name);
4229                 else if(percentage > 75) msg_format("%^s hisses.", m_name);
4230                 else if(percentage > 50) msg_format("%^s rears up in anger.", m_name);
4231                 else if(percentage > 35) msg_format("%^s hisses furiously.", m_name);
4232                 else if(percentage > 20) msg_format("%^s writhes about.", m_name);
4233                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
4234                 else msg_format("%^s jerks limply.", m_name);
4235 #endif
4236         }
4237
4238         else if(my_strchr("f", r_ptr->d_char))
4239         {
4240 #ifdef JP
4241                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4242                 else if(percentage > 75) msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4243                 else if(percentage > 50) msg_format("%^s¤ÏÅܤäÆËʤ¨¤¿¡£", m_name);
4244                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4245                 else if(percentage > 20) msg_format("%^s¤ÏÄˤߤǼ塹¤·¤¯ÌĤ¤¤¿¡£", m_name);
4246                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤˤ¦¤á¤¤¤¿¡£", m_name);
4247                 else msg_format("%s¤Ï°¥¤ì¤ÊÌĤ­À¼¤ò½Ð¤·¤¿¡£", m_name);
4248 #else
4249                 if(percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
4250                 else if(percentage > 75) msg_format("%^s roars.", m_name);
4251                 else if(percentage > 50) msg_format("%^s growls angrily.", m_name);
4252                 else if(percentage > 35) msg_format("%^s hisses with pain.", m_name);
4253                 else if(percentage > 20) msg_format("%^s mewls in pain.", m_name);
4254                 else if(percentage > 10) msg_format("%^s hisses in agony.", m_name);
4255                 else msg_format("%^s mewls pitifully.", m_name);
4256 #endif
4257         }
4258
4259         else if(my_strchr("acFIKS", r_ptr->d_char)) // Ants, Centipedes, Flies, Insects, Beetles, Spiders
4260         {
4261 #ifdef JP
4262                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4263                 else if(percentage > 75) msg_format("%^s¤Ï¥­¡¼¥­¡¼ÌĤ¤¤¿¡£", m_name);
4264                 else if(percentage > 50) msg_format("%^s¤Ï¥è¥í¥è¥íƨ¤²²ó¤Ã¤¿¡£", m_name);
4265                 else if(percentage > 35) msg_format("%^s¤Ï¤¦¤ë¤µ¤¯ÌĤ¤¤¿¡£", m_name);
4266                 else if(percentage > 20) msg_format("%^s¤ÏÄˤߤËáÛÚ»¤·¤¿¡£", m_name);
4267                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤÇáÛÚ»¤·¤¿¡£", m_name);
4268                 else msg_format("%^s¤Ï¥Ô¥¯¥Ô¥¯¤Ò¤­¤Ä¤Ã¤¿¡£", m_name);
4269 #else
4270                 if(percentage > 95)     msg_format("%^s ignores the attack.", m_name);
4271                 else if(percentage > 75) msg_format("%^s chitters.", m_name);
4272                 else if(percentage > 50) msg_format("%^s scuttles about.", m_name);
4273                 else if(percentage > 35) msg_format("%^s twitters.", m_name);
4274                 else if(percentage > 20) msg_format("%^s jerks in pain.", m_name);
4275                 else if(percentage > 10) msg_format("%^s jerks in agony.", m_name);
4276                 else msg_format("%^s twitches.", m_name);
4277 #endif
4278         }
4279
4280         else if(my_strchr("B", r_ptr->d_char)) // Birds
4281         {               
4282 #ifdef JP
4283                 if(percentage > 95) msg_format("%^s¤Ï¤µ¤¨¤º¤Ã¤¿¡£", m_name);
4284                 else if(percentage > 75) msg_format("%^s¤Ï¥Ô¡¼¥Ô¡¼ÌĤ¤¤¿¡£", m_name);
4285                 else if(percentage > 50) msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ¤¤¿¡£", m_name);
4286                 else if(percentage > 35) msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4287                 else if(percentage > 20) msg_format("%^s¤Ï¶ì¤·¤ó¤À¡£", m_name);
4288                 else if(percentage > 10) msg_format("%^s¤Ï¤Î¤¿¤¦¤Á²ó¤Ã¤¿¡£", m_name);
4289                 else msg_format("%^s¤Ï¥­¡¼¥­¡¼¤ÈÌĤ­¶«¤ó¤À¡£", m_name);
4290 #else
4291                 if(percentage > 95)     msg_format("%^s chirps.", m_name);
4292                 else if(percentage > 75) msg_format("%^s twitters.", m_name);
4293                 else if(percentage > 50) msg_format("%^s squawks.", m_name);
4294                 else if(percentage > 35) msg_format("%^s chatters.", m_name);
4295                 else if(percentage > 20) msg_format("%^s jeers.", m_name);
4296                 else if(percentage > 10) msg_format("%^s flutters about.", m_name);
4297                 else msg_format("%^s squeaks.", m_name);
4298 #endif
4299         }
4300
4301         else if(my_strchr("duDLUW", r_ptr->d_char)) // Dragons, Demons, High Undead
4302         {       
4303 #ifdef JP
4304                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4305                 else if(percentage > 75) msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4306                 else if(percentage > 50) msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4307                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4308                 else if(percentage > 20) msg_format("%^s¤ÏÄˤߤËËʤ¨¤¿¡£", m_name);
4309                 else if(percentage > 10) msg_format("%^s¤Ï¶ì¤·¤²¤Ë¶«¤ó¤À¡£", m_name);
4310                 else msg_format("%^s¤Ï¼å¡¹¤·¤¯¤¦¤Ê¤Ã¤¿¡£", m_name);
4311 #else
4312                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
4313                 else if(percentage > 75) msg_format("%^s flinches.", m_name);
4314                 else if(percentage > 50) msg_format("%^s hisses in pain.", m_name);
4315                 else if(percentage > 35) msg_format("%^s snarls with pain.", m_name);
4316                 else if(percentage > 20) msg_format("%^s roars with pain.", m_name);
4317                 else if(percentage > 10) msg_format("%^s gasps.", m_name);
4318                 else msg_format("%^s snarls feebly.", m_name);
4319 #endif
4320         }
4321
4322         else if(my_strchr("s", r_ptr->d_char)) // Skeletons
4323         {
4324 #ifdef JP
4325                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4326                 else if(percentage > 75) msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4327                 else if(percentage > 50) msg_format("%^s¤Ï¥«¥¿¥«¥¿¤È¾Ð¤Ã¤¿¡£", m_name);
4328                 else if(percentage > 35) msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4329                 else if(percentage > 20) msg_format("%^s¤Ï¥«¥¿¥«¥¿¸À¤Ã¤¿¡£", m_name);
4330                 else if(percentage > 10) msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4331                 else msg_format("%^s¤Ï¥¬¥¿¥¬¥¿¸À¤Ã¤¿¡£", m_name);
4332 #else
4333                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
4334                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
4335                 else if(percentage > 50) msg_format("%^s rattles.", m_name);
4336                 else if(percentage > 35) msg_format("%^s stumbles.", m_name);
4337                 else if(percentage > 20) msg_format("%^s rattles.", m_name);
4338                 else if(percentage > 10) msg_format("%^s staggers.", m_name);
4339                 else msg_format("%^s clatters.", m_name);
4340 #endif
4341         }
4342
4343         else if(my_strchr("z", r_ptr->d_char)) // Zombies
4344         {               
4345 #ifdef JP
4346                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4347                 else if(percentage > 75) msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4348                 else if(percentage > 50) msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4349                 else if(percentage > 35) msg_format("%s¤Ï¶ì¤·¤²¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4350                 else if(percentage > 20) msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4351                 else if(percentage > 10) msg_format("%^s¤Ï¤¦¤Ê¤Ã¤¿¡£", m_name);
4352                 else msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4353 #else
4354                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
4355                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
4356                 else if(percentage > 50) msg_format("%^s groans.", m_name);
4357                 else if(percentage > 35) msg_format("%^s moans.", m_name);
4358                 else if(percentage > 20) msg_format("%^s hesitates.", m_name);
4359                 else if(percentage > 10) msg_format("%^s grunts.", m_name);
4360                 else msg_format("%^s staggers.", m_name);
4361 #endif
4362         }
4363
4364         else if(my_strchr("G", r_ptr->d_char)) // Ghosts
4365         {
4366 #ifdef JP
4367                 if(percentage > 95) msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4368                 else if(percentage > 75) msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4369                 else if(percentage > 50) msg_format("%s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4370                 else if(percentage > 35) msg_format("%^s¤Ïµã¤­¤ï¤á¤¤¤¿¡£", m_name);
4371                 else if(percentage > 20) msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4372                 else if(percentage > 10) msg_format("%s¤Ï¼å¡¹¤·¤¯¤¦¤á¤¤¤¿¡£", m_name);
4373                 else msg_format("%^s¤Ï¤«¤¹¤«¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4374 #else
4375                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
4376                 else if(percentage > 75) msg_format("%^s shrugs off the attack.", m_name);
4377                 else if(percentage > 50)  msg_format("%^s moans.", m_name);
4378                 else if(percentage > 35) msg_format("%^s wails.", m_name);
4379                 else if(percentage > 20) msg_format("%^s howls.", m_name);
4380                 else if(percentage > 10) msg_format("%^s moans softly.", m_name);
4381                 else msg_format("%^s sighs.", m_name);
4382 #endif
4383         }
4384
4385         else if(my_strchr("CZ", r_ptr->d_char)) // Dogs and Hounds
4386         {
4387 #ifdef JP
4388                 if(percentage > 95) msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4389                 else if(percentage > 75) msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4390                 else if(percentage > 50) msg_format("%^s¤ÏÄˤߤǥ­¥ã¥ó¥­¥ã¥óËʤ¨¤¿¡£", m_name);
4391                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤÇÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4392                 else if(percentage > 20) msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4393                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4394                 else msg_format("%^s¤Ï¼å¡¹¤·¤¯Ëʤ¨¤¿¡£", m_name);
4395 #else
4396                 if(percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
4397                 else if(percentage > 75) msg_format("%^s snarls with pain.", m_name);
4398                 else if(percentage > 50) msg_format("%^s yelps in pain.", m_name);
4399                 else if(percentage > 35) msg_format("%^s howls in pain.", m_name);
4400                 else if(percentage > 20) msg_format("%^s howls in agony.", m_name);
4401                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
4402                 else msg_format("%^s yelps feebly.", m_name);
4403 #endif
4404         }
4405
4406         else if(my_strchr("Xbilqrt", r_ptr->d_char)) // One type of creatures (ignore,squeal,shriek)
4407         {
4408 #ifdef JP
4409                 if(percentage > 95) msg_format("%^s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4410                 else if(percentage > 75) msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4411                 else if(percentage > 50) msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4412                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4413                 else if(percentage > 20) msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4414                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4415                 else msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4416 #else
4417                 if(percentage > 95) msg_format("%^s ignores the attack.", m_name);
4418                 else if(percentage > 75) msg_format("%^s grunts with pain.", m_name);
4419                 else if(percentage > 50) msg_format("%^s squeals in pain.", m_name);
4420                 else if(percentage > 35) msg_format("%^s shrieks in pain.", m_name);
4421                 else if(percentage > 20) msg_format("%^s shrieks in agony.", m_name);
4422                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
4423                 else msg_format("%^s cries out feebly.", m_name);
4424 #endif
4425         }
4426
4427         else // Another type of creatures (shrug,cry,scream)
4428         {
4429 #ifdef JP
4430                 if(percentage > 95) msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4431                 else if(percentage > 75) msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4432                 else if(percentage > 50) msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4433                 else if(percentage > 35) msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4434                 else if(percentage > 20) msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4435                 else if(percentage > 10) msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4436                 else msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4437 #else
4438                 if(percentage > 95) msg_format("%^s shrugs off the attack.", m_name);
4439                 else if(percentage > 75) msg_format("%^s grunts with pain.", m_name);
4440                 else if(percentage > 50) msg_format("%^s cries out in pain.", m_name);
4441                 else if(percentage > 35) msg_format("%^s screams in pain.", m_name);
4442                 else if(percentage > 20) msg_format("%^s screams in agony.", m_name);
4443                 else if(percentage > 10) msg_format("%^s writhes in agony.", m_name);
4444                 else msg_format("%^s cries out feebly.", m_name);
4445 #endif
4446         }
4447 }
4448
4449
4450
4451
4452 /*
4453  * Learn about an "observed" resistance.
4454  */
4455 void update_smart_learn(int m_idx, int what)
4456 {
4457         monster_type *m_ptr = &m_list[m_idx];
4458
4459         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4460
4461
4462         /* Not allowed to learn */
4463         if (!smart_learn) return;
4464
4465         /* Too stupid to learn anything */
4466         if (r_ptr->flags2 & (RF2_STUPID)) return;
4467
4468         /* Not intelligent, only learn sometimes */
4469         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
4470
4471
4472         /* XXX XXX XXX */
4473
4474         /* Analyze the knowledge */
4475         switch (what)
4476         {
4477         case DRS_ACID:
4478                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
4479                 if (IS_OPPOSE_ACID()) m_ptr->smart |= (SM_OPP_ACID);
4480                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
4481                 break;
4482
4483         case DRS_ELEC:
4484                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
4485                 if (IS_OPPOSE_ELEC()) m_ptr->smart |= (SM_OPP_ELEC);
4486                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
4487                 break;
4488
4489         case DRS_FIRE:
4490                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
4491                 if (IS_OPPOSE_FIRE()) m_ptr->smart |= (SM_OPP_FIRE);
4492                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
4493                 break;
4494
4495         case DRS_COLD:
4496                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
4497                 if (IS_OPPOSE_COLD()) m_ptr->smart |= (SM_OPP_COLD);
4498                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
4499                 break;
4500
4501         case DRS_POIS:
4502                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
4503                 if (IS_OPPOSE_POIS()) m_ptr->smart |= (SM_OPP_POIS);
4504                 break;
4505
4506
4507         case DRS_NETH:
4508                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
4509                 break;
4510
4511         case DRS_LITE:
4512                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
4513                 break;
4514
4515         case DRS_DARK:
4516                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
4517                 break;
4518
4519         case DRS_FEAR:
4520                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
4521                 break;
4522
4523         case DRS_CONF:
4524                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
4525                 break;
4526
4527         case DRS_CHAOS:
4528                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4529                 break;
4530
4531         case DRS_DISEN:
4532                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4533                 break;
4534
4535         case DRS_BLIND:
4536                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4537                 break;
4538
4539         case DRS_NEXUS:
4540                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4541                 break;
4542
4543         case DRS_SOUND:
4544                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4545                 break;
4546
4547         case DRS_SHARD:
4548                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4549                 break;
4550
4551         case DRS_FREE:
4552                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4553                 break;
4554
4555         case DRS_MANA:
4556                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4557                 break;
4558
4559         case DRS_REFLECT:
4560                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
4561                 break;
4562         }
4563 }
4564
4565
4566 /*
4567  * Place the player in the dungeon XXX XXX
4568  */
4569 bool player_place(int y, int x)
4570 {
4571         /* Paranoia XXX XXX */
4572         if (cave[y][x].m_idx != 0) return FALSE;
4573
4574         /* Save player location */
4575         py = y;
4576         px = x;
4577
4578         /* Success */
4579         return TRUE;
4580 }
4581
4582
4583 /*
4584  * Drop all items carried by a monster
4585  */
4586 void monster_drop_carried_objects(monster_type *m_ptr)
4587 {
4588         s16b this_o_idx, next_o_idx = 0;
4589         object_type forge;
4590         object_type *o_ptr;
4591         object_type *q_ptr;
4592
4593
4594         /* Drop objects being carried */
4595         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4596         {
4597                 /* Acquire object */
4598                 o_ptr = &o_list[this_o_idx];
4599
4600                 /* Acquire next object */
4601                 next_o_idx = o_ptr->next_o_idx;
4602
4603                 /* Get local object */
4604                 q_ptr = &forge;
4605
4606                 /* Copy the object */
4607                 object_copy(q_ptr, o_ptr);
4608
4609                 /* Forget monster */
4610                 q_ptr->held_m_idx = 0;
4611
4612                 /* Delete the object */
4613                 delete_object_idx(this_o_idx);
4614
4615                 /* Drop it */
4616                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
4617         }
4618
4619         /* Forget objects */
4620         m_ptr->hold_o_idx = 0;
4621 }