OSDN Git Service

d7fc49b46da2df10f072ac35e4f5846568443b27
[hengband/hengband.git] / src / monster2.c
1 /* File: monster2.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: misc code for monsters */
12
13 #include "angband.h"
14
15 #define HORDE_NOGOOD 0x01
16 #define HORDE_NOEVIL 0x02
17
18 cptr horror_desc[MAX_SAN_HORROR] =
19 {
20 #ifdef JP
21         "´÷¤Þ¤ï¤·¤¤",
22         "ÄìÃΤì¤Ì",
23         "¤¾¤Ã¤È¤¹¤ë",
24         "ÇËÌÇŪ¤Ê",
25         "ËÁÆÂŪ¤Ê",
26
27         "¤¤¤ä¤Ê",
28         "¶²¤í¤·¤¤",
29         "ÉÔ·é¤Ê",
30         "ÍƼϤΤʤ¤",
31         "¤ª¤¾¤Þ¤·¤¤",
32
33         "ÃϹö¤Î",
34         "¿È¤ÎÌÓ¤â¤è¤À¤Ä",
35         "ÃϹö¤Î",
36         "´÷¤Þ¤ï¤·¤¤",
37         "°­Ì´¤Î¤è¤¦¤Ê",
38
39         "·ù°­¤ò´¶¤¸¤ë",
40         "ȳÅö¤¿¤ê¤Ê",
41         "¶²¤¤",
42         "ÉÔ¾ô¤Ê",
43         "¸À¤¦¤â¤ª¤¾¤Þ¤·¤¤",
44 #else
45         "abominable",
46         "abysmal",
47         "appalling",
48         "baleful",
49         "blasphemous",
50
51         "disgusting",
52         "dreadful",
53         "filthy",
54         "grisly",
55         "hideous",
56
57         "hellish",
58         "horrible",
59         "infernal",
60         "loathsome",
61         "nightmarish",
62
63         "repulsive",
64         "sacrilegious",
65         "terrible",
66         "unclean",
67         "unspeakable",
68 #endif
69
70 };
71
72 cptr funny_desc[MAX_SAN_FUNNY] =
73 {
74 #ifdef JP
75         "´ÖÈ´¤±¤Ê",
76         "³ê·Î¤Ê",
77         "¤Ð¤«¤é¤·¤¤",
78         "̵̣´¥Áç¤Ê",
79         "Çϼ¯¤²¤¿",
80
81         "¾Ð¤¨¤ë",
82         "¤Ð¤«¤Ð¤«¤·¤¤",
83         "¤Ö¤Ã¤È¤ó¤À",
84         "¤¤¤«¤·¤¿",
85         "¥Ý¥¹¥È¥â¥À¥ó¤Ê",
86
87         "¥Õ¥¡¥ó¥¿¥¹¥Æ¥£¥Ã¥¯¤Ê",
88         "¥À¥À¥¤¥º¥àŪ¤Ê",
89         "¥­¥å¥Ó¥º¥àŪ¤Ê",
90         "±§ÃèŪ¤Ê",
91         "Âî±Û¤·¤¿",
92
93         "Íý²òÉÔǽ¤Ê",
94         "¤â¤Î¤¹¤´¤¤",
95         "¶Ã¤¯¤Ù¤­",
96         "¿®¤¸¤é¤ì¤Ê¤¤",
97         "¥«¥ª¥Æ¥£¥Ã¥¯¤Ê",
98
99         "ÌîÀ­Åª¤Ê",
100         "Èó¾ï¼±¤Ê",
101 #else
102         "silly",
103         "hilarious",
104         "absurd",
105         "insipid",
106         "ridiculous",
107
108         "laughable",
109         "ludicrous",
110         "far-out",
111         "groovy",
112         "postmodern",
113
114         "fantastic",
115         "dadaistic",
116         "cubistic",
117         "cosmic",
118         "awesome",
119
120         "incomprehensible",
121         "fabulous",
122         "amazing",
123         "incredible",
124         "chaotic",
125
126         "wild",
127         "preposterous",
128 #endif
129
130 };
131
132 cptr funny_comments[MAX_SAN_COMMENT] =
133 {
134 #ifdef JP
135   /* nuke me */
136         "ºÇ¹â¤À¤¼¡ª",
137         "¤¦¤Ò¤ç¡¼¡ª",
138         "¤¤¤«¤¹¤¼¡ª",
139         "¤¹¤ó¤Ð¤é¤·¤¤¡ª",
140         "¤Ö¤Ã¤È¤Ó¡¼¡ª"
141 #else
142         "Wow, cosmic, man!",
143         "Rad!",
144         "Groovy!",
145         "Cool!",
146         "Far out!"
147 #endif
148
149 };
150
151
152 /*
153  * Set the target of counter attack
154  */
155 void set_target(monster_type *m_ptr, int y, int x)
156 {
157         m_ptr->target_y = y;
158         m_ptr->target_x = x;
159 }
160
161
162 /*
163  * Reset the target of counter attack
164  */
165 void reset_target(monster_type *m_ptr)
166 {
167         set_target(m_ptr, 0, 0);
168 }
169
170
171 /*
172  *  Extract monster race pointer of a monster's true form
173  */
174 monster_race *real_r_ptr(monster_type *m_ptr)
175 {
176         monster_race *r_ptr = &r_info[m_ptr->r_idx];
177
178         /* Extract real race */
179         if (m_ptr->mflag2 & MFLAG2_CHAMELEON)
180         {
181                 if (r_ptr->flags1 & RF1_UNIQUE)
182                         return &r_info[MON_CHAMELEON_K];
183                 else
184                         return &r_info[MON_CHAMELEON];
185         }
186         else
187         {
188                 return r_ptr;
189         }
190 }
191
192
193 /*
194  * Delete a monster by index.
195  *
196  * When a monster is deleted, all of its objects are deleted.
197  */
198 void delete_monster_idx(int i)
199 {
200         int x, y;
201
202         monster_type *m_ptr = &m_list[i];
203
204         monster_race *r_ptr = &r_info[m_ptr->r_idx];
205
206         s16b this_o_idx, next_o_idx = 0;
207
208
209         /* Get location */
210         y = m_ptr->fy;
211         x = m_ptr->fx;
212
213
214         /* Hack -- Reduce the racial counter */
215         real_r_ptr(m_ptr)->cur_num--;
216
217         /* Hack -- count the number of "reproducers" */
218         if (r_ptr->flags2 & (RF2_MULTIPLY)) num_repro--;
219
220         if (MON_CSLEEP(m_ptr)) (void)set_monster_csleep(i, 0);
221         if (MON_FAST(m_ptr)) (void)set_monster_fast(i, 0);
222         if (MON_SLOW(m_ptr)) (void)set_monster_slow(i, 0);
223         if (MON_STUNNED(m_ptr)) (void)set_monster_stunned(i, 0);
224         if (MON_CONFUSED(m_ptr)) (void)set_monster_confused(i, 0);
225         if (MON_MONFEAR(m_ptr)) (void)set_monster_monfear(i, 0);
226         if (MON_INVULNER(m_ptr)) (void)set_monster_invulner(i, 0, FALSE);
227
228
229         /* Hack -- remove target monster */
230         if (i == target_who) target_who = 0;
231
232         /* Hack -- remove tracked monster */
233         if (i == p_ptr->health_who) health_track(0);
234
235         if (pet_t_m_idx == i ) pet_t_m_idx = 0;
236         if (riding_t_m_idx == i) riding_t_m_idx = 0;
237         if (p_ptr->riding == i) p_ptr->riding = 0;
238
239         /* Monster is gone */
240         cave[y][x].m_idx = 0;
241
242
243         /* Delete objects */
244         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
245         {
246                 object_type *o_ptr;
247
248                 /* Acquire object */
249                 o_ptr = &o_list[this_o_idx];
250
251                 /* Acquire next object */
252                 next_o_idx = o_ptr->next_o_idx;
253
254                 /*
255                  * o_ptr->held_m_idx is needed in delete_object_idx()
256                  * to prevent calling lite_spot()
257                  */
258
259                 /* Delete the object */
260                 delete_object_idx(this_o_idx);
261         }
262
263
264         if (is_pet(m_ptr)) check_pets_num_and_align(m_ptr, FALSE);
265
266
267         /* Wipe the Monster */
268         (void)WIPE(m_ptr, monster_type);
269
270         /* Count monsters */
271         m_cnt--;
272
273         /* Visual update */
274         lite_spot(y, x);
275
276         /* Update some things */
277         if (r_ptr->flags7 & (RF7_LITE_MASK | RF7_DARK_MASK))
278                 p_ptr->update |= (PU_MON_LITE);
279 }
280
281
282 /*
283  * Delete the monster, if any, at a given location
284  */
285 void delete_monster(int y, int x)
286 {
287         cave_type *c_ptr;
288
289         /* Paranoia */
290         if (!in_bounds(y, x)) return;
291
292         /* Check the grid */
293         c_ptr = &cave[y][x];
294
295         /* Delete the monster (if any) */
296         if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
297 }
298
299
300 /*
301  * Move an object from index i1 to index i2 in the object list
302  */
303 static void compact_monsters_aux(int i1, int i2)
304 {
305         int y, x, i;
306
307         cave_type *c_ptr;
308
309         monster_type *m_ptr;
310
311         s16b this_o_idx, next_o_idx = 0;
312
313
314         /* Do nothing */
315         if (i1 == i2) return;
316
317
318         /* Old monster */
319         m_ptr = &m_list[i1];
320
321         /* Location */
322         y = m_ptr->fy;
323         x = m_ptr->fx;
324
325         /* Cave grid */
326         c_ptr = &cave[y][x];
327
328         /* Update the cave */
329         c_ptr->m_idx = i2;
330
331         /* Repair objects being carried by monster */
332         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
333         {
334                 object_type *o_ptr;
335
336                 /* Acquire object */
337                 o_ptr = &o_list[this_o_idx];
338
339                 /* Acquire next object */
340                 next_o_idx = o_ptr->next_o_idx;
341
342                 /* Reset monster pointer */
343                 o_ptr->held_m_idx = i2;
344         }
345
346         /* Hack -- Update the target */
347         if (target_who == i1) target_who = i2;
348
349         /* Hack -- Update the target */
350         if (pet_t_m_idx == i1) pet_t_m_idx = i2;
351         if (riding_t_m_idx == i1) riding_t_m_idx = i2;
352
353         /* Hack -- Update the riding */
354         if (p_ptr->riding == i1) p_ptr->riding = i2;
355
356         /* Hack -- Update the health bar */
357         if (p_ptr->health_who == i1) health_track(i2);
358
359         /* Hack -- Update parent index */
360         if (is_pet(m_ptr))
361         {
362                 for (i = 1; i < m_max; i++)
363                 {
364                         monster_type *m2_ptr = &m_list[i];
365
366                         if (m2_ptr->parent_m_idx == i1)
367                                 m2_ptr->parent_m_idx = i2;
368                 }
369         }
370
371         /* Structure copy */
372         COPY(&m_list[i2], &m_list[i1], monster_type);
373
374         /* Wipe the hole */
375         (void)WIPE(&m_list[i1], monster_type);
376
377         for (i = 0; i < MAX_MTIMED; i++)
378         {
379                 int mproc_idx = get_mproc_idx(i1, i);
380                 if (mproc_idx >= 0) mproc_list[i][mproc_idx] = i2;
381         }
382 }
383
384
385 /*
386  * Compact and Reorder the monster list
387  *
388  * This function can be very dangerous, use with caution!
389  *
390  * When actually "compacting" monsters, we base the saving throw
391  * on a combination of monster level, distance from player, and
392  * current "desperation".
393  *
394  * After "compacting" (if needed), we "reorder" the monsters into a more
395  * compact order, and we reset the allocation info, and the "live" array.
396  */
397 void compact_monsters(int size)
398 {
399         int             i, num, cnt;
400         int             cur_lev, cur_dis, chance;
401
402         /* Message (only if compacting) */
403 #ifdef JP
404         if (size) msg_print("¥â¥ó¥¹¥¿¡¼¾ðÊó¤ò°µ½Ì¤·¤Æ¤¤¤Þ¤¹...");
405 #else
406         if (size) msg_print("Compacting monsters...");
407 #endif
408
409
410         /* Compact at least 'size' objects */
411         for (num = 0, cnt = 1; num < size; cnt++)
412         {
413                 /* Get more vicious each iteration */
414                 cur_lev = 5 * cnt;
415
416                 /* Get closer each iteration */
417                 cur_dis = 5 * (20 - cnt);
418
419                 /* Check all the monsters */
420                 for (i = 1; i < m_max; i++)
421                 {
422                         monster_type *m_ptr = &m_list[i];
423
424                         monster_race *r_ptr = &r_info[m_ptr->r_idx];
425
426                         /* Paranoia -- skip "dead" monsters */
427                         if (!m_ptr->r_idx) continue;
428
429                         /* Hack -- High level monsters start out "immune" */
430                         if (r_ptr->level > cur_lev) continue;
431
432                         if (i == p_ptr->riding) continue;
433
434                         /* Ignore nearby monsters */
435                         if ((cur_dis > 0) && (m_ptr->cdis < cur_dis)) continue;
436
437                         /* Saving throw chance */
438                         chance = 90;
439
440                         /* Only compact "Quest" Monsters in emergencies */
441                         if ((r_ptr->flags1 & (RF1_QUESTOR)) && (cnt < 1000)) chance = 100;
442
443                         /* Try not to compact Unique Monsters */
444                         if (r_ptr->flags1 & (RF1_UNIQUE)) chance = 100;
445
446                         /* All monsters get a saving throw */
447                         if (randint0(100) < chance) continue;
448
449                         if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
450                         {
451                                 char m_name[80];
452
453                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
454                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_COMPACT, m_name);
455                         }
456
457                         /* Delete the monster */
458                         delete_monster_idx(i);
459
460                         /* Count the monster */
461                         num++;
462                 }
463         }
464
465
466         /* Excise dead monsters (backwards!) */
467         for (i = m_max - 1; i >= 1; i--)
468         {
469                 /* Get the i'th monster */
470                 monster_type *m_ptr = &m_list[i];
471
472                 /* Skip real monsters */
473                 if (m_ptr->r_idx) continue;
474
475                 /* Move last monster into open hole */
476                 compact_monsters_aux(m_max - 1, i);
477
478                 /* Compress "m_max" */
479                 m_max--;
480         }
481 }
482
483
484 /*
485  * Delete/Remove all the monsters when the player leaves the level
486  *
487  * This is an efficient method of simulating multiple calls to the
488  * "delete_monster()" function, with no visual effects.
489  */
490 void wipe_m_list(void)
491 {
492         int i;
493
494         /* Hack -- if Banor or Lupart dies, stay another dead */
495         if (!r_info[MON_BANORLUPART].max_num)
496         {
497                 if (r_info[MON_BANOR].max_num)
498                 {
499                         r_info[MON_BANOR].max_num = 0;
500                         r_info[MON_BANOR].r_pkills++;
501                         r_info[MON_BANOR].r_akills++;
502                         if (r_info[MON_BANOR].r_tkills < MAX_SHORT) r_info[MON_BANOR].r_tkills++;
503                 }
504                 if (r_info[MON_LUPART].max_num)
505                 {
506                         r_info[MON_LUPART].max_num = 0;
507                         r_info[MON_LUPART].r_pkills++;
508                         r_info[MON_LUPART].r_akills++;
509                         if (r_info[MON_LUPART].r_tkills < MAX_SHORT) r_info[MON_LUPART].r_tkills++;
510                 }
511         }
512
513         /* Delete all the monsters */
514         for (i = m_max - 1; i >= 1; i--)
515         {
516                 monster_type *m_ptr = &m_list[i];
517
518                 /* Skip dead monsters */
519                 if (!m_ptr->r_idx) continue;
520
521                 /* Monster is gone */
522                 cave[m_ptr->fy][m_ptr->fx].m_idx = 0;
523
524                 /* Wipe the Monster */
525                 (void)WIPE(m_ptr, monster_type);
526
527         }
528
529         /*
530          * Wiping racial counters of all monsters and incrementing of racial
531          * counters of monsters in party_mon[] are required to prevent multiple
532          * generation of unique monster who is the minion of player.
533          */
534
535         /* Hack -- Wipe the racial counter of all monster races */
536         for (i = 1; i < max_r_idx; i++) r_info[i].cur_num = 0;
537
538         /* Reset "m_max" */
539         m_max = 1;
540
541         /* Reset "m_cnt" */
542         m_cnt = 0;
543
544         /* Reset "mproc_max[]" */
545         for (i = 0; i < MAX_MTIMED; i++) mproc_max[i] = 0;
546
547         /* Hack -- reset "reproducer" count */
548         num_repro = 0;
549
550         /* Hack -- no more target */
551         target_who = 0;
552         pet_t_m_idx = 0;
553         riding_t_m_idx = 0;
554
555         /* Hack -- no more tracking */
556         health_track(0);
557 }
558
559
560 /*
561  * Acquires and returns the index of a "free" monster.
562  *
563  * This routine should almost never fail, but it *can* happen.
564  */
565 s16b m_pop(void)
566 {
567         int i;
568
569
570         /* Normal allocation */
571         if (m_max < max_m_idx)
572         {
573                 /* Access the next hole */
574                 i = m_max;
575
576                 /* Expand the array */
577                 m_max++;
578
579                 /* Count monsters */
580                 m_cnt++;
581
582                 /* Return the index */
583                 return (i);
584         }
585
586
587         /* Recycle dead monsters */
588         for (i = 1; i < m_max; i++)
589         {
590                 monster_type *m_ptr;
591
592                 /* Acquire monster */
593                 m_ptr = &m_list[i];
594
595                 /* Skip live monsters */
596                 if (m_ptr->r_idx) continue;
597
598                 /* Count monsters */
599                 m_cnt++;
600
601                 /* Use this monster */
602                 return (i);
603         }
604
605
606         /* Warn the player (except during dungeon creation) */
607 #ifdef JP
608         if (character_dungeon) msg_print("¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë¡ª");
609 #else
610         if (character_dungeon) msg_print("Too many monsters!");
611 #endif
612
613
614         /* Try not to crash */
615         return (0);
616 }
617
618
619
620
621 /*
622  * Hack -- the "type" of the current "summon specific"
623  */
624 static int summon_specific_type = 0;
625
626
627 /*
628  * Hack -- the index of the summoning monster
629  */
630 static int summon_specific_who = -1;
631
632
633 static bool summon_unique_okay = FALSE;
634
635
636 static bool summon_specific_aux(int r_idx)
637 {
638         monster_race *r_ptr = &r_info[r_idx];
639         int okay = FALSE;
640
641         /* Check our requirements */
642         switch (summon_specific_type)
643         {
644                 case SUMMON_ANT:
645                 {
646                         okay = (r_ptr->d_char == 'a');
647                         break;
648                 }
649
650                 case SUMMON_SPIDER:
651                 {
652                         okay = (r_ptr->d_char == 'S');
653                         break;
654                 }
655
656                 case SUMMON_HOUND:
657                 {
658                         okay = ((r_ptr->d_char == 'C') || (r_ptr->d_char == 'Z'));
659                         break;
660                 }
661
662                 case SUMMON_HYDRA:
663                 {
664                         okay = (r_ptr->d_char == 'M');
665                         break;
666                 }
667
668                 case SUMMON_ANGEL:
669                 {
670                         okay = (r_ptr->d_char == 'A' && ((r_ptr->flags3 & RF3_EVIL) || (r_ptr->flags3 & RF3_GOOD)));
671                         break;
672                 }
673
674                 case SUMMON_DEMON:
675                 {
676                         okay = (r_ptr->flags3 & RF3_DEMON);
677                         break;
678                 }
679
680                 case SUMMON_UNDEAD:
681                 {
682                         okay = (r_ptr->flags3 & RF3_UNDEAD);
683                         break;
684                 }
685
686                 case SUMMON_DRAGON:
687                 {
688                         okay = (r_ptr->flags3 & RF3_DRAGON);
689                         break;
690                 }
691
692                 case SUMMON_HI_UNDEAD:
693                 {
694                         okay = ((r_ptr->d_char == 'L') ||
695                                 (r_ptr->d_char == 'V') ||
696                                 (r_ptr->d_char == 'W'));
697                         break;
698                 }
699
700                 case SUMMON_HI_DRAGON:
701                 {
702                         okay = (r_ptr->d_char == 'D');
703                         break;
704                 }
705
706                 case SUMMON_HI_DEMON:
707                 {
708                         okay = (((r_ptr->d_char == 'U') ||
709                                  (r_ptr->d_char == 'H') ||
710                                  (r_ptr->d_char == 'B')) &&
711                                 (r_ptr->flags3 & RF3_DEMON)) ? TRUE : FALSE;
712                         break;
713                 }
714
715                 case SUMMON_AMBERITES:
716                 {
717                         okay = (r_ptr->flags3 & (RF3_AMBERITE)) ? TRUE : FALSE;
718                         break;
719                 }
720
721                 case SUMMON_UNIQUE:
722                 {
723                         okay = (r_ptr->flags1 & (RF1_UNIQUE)) ? TRUE : FALSE;
724                         break;
725                 }
726
727                 case SUMMON_BIZARRE1:
728                 {
729                         okay = (r_ptr->d_char == 'm');
730                         break;
731                 }
732                 case SUMMON_BIZARRE2:
733                 {
734                         okay = (r_ptr->d_char == 'b');
735                         break;
736                 }
737                 case SUMMON_BIZARRE3:
738                 {
739                         okay = (r_ptr->d_char == 'Q');
740                         break;
741                 }
742
743                 case SUMMON_BIZARRE4:
744                 {
745                         okay = (r_ptr->d_char == 'v');
746                         break;
747                 }
748
749                 case SUMMON_BIZARRE5:
750                 {
751                         okay = (r_ptr->d_char == '$');
752                         break;
753                 }
754
755                 case SUMMON_BIZARRE6:
756                 {
757                         okay = ((r_ptr->d_char == '!') ||
758                                  (r_ptr->d_char == '?') ||
759                                  (r_ptr->d_char == '=') ||
760                                  (r_ptr->d_char == '$') ||
761                                  (r_ptr->d_char == '|'));
762                         break;
763                 }
764
765                 case SUMMON_GOLEM:
766                 {
767                         okay = (r_ptr->d_char == 'g');
768                         break;
769                 }
770
771                 case SUMMON_CYBER:
772                 {
773                         okay = ((r_ptr->d_char == 'U') &&
774                                 (r_ptr->flags4 & RF4_ROCKET));
775                         break;
776                 }
777
778
779                 case SUMMON_KIN:
780                 {
781                         okay = ((r_ptr->d_char == summon_kin_type) && (r_idx != MON_HAGURE));
782                         break;
783                 }
784
785                 case SUMMON_DAWN:
786                 {
787                         okay = (r_idx == MON_DAWN);
788                         break;
789                 }
790
791                 case SUMMON_ANIMAL:
792                 {
793                         okay = (r_ptr->flags3 & (RF3_ANIMAL));
794                         break;
795                 }
796
797                 case SUMMON_ANIMAL_RANGER:
798                 {
799                         okay = ((r_ptr->flags3 & (RF3_ANIMAL)) &&
800                                (my_strchr("abcflqrwBCHIJKMRS", r_ptr->d_char)) &&
801                                !(r_ptr->flags3 & (RF3_DRAGON)) &&
802                                !(r_ptr->flags3 & (RF3_EVIL)) &&
803                                !(r_ptr->flags3 & (RF3_UNDEAD)) &&
804                                !(r_ptr->flags3 & (RF3_DEMON)) &&
805                                !(r_ptr->flags2 & (RF2_MULTIPLY)) &&
806                                !(r_ptr->flags4 || r_ptr->flags5 || r_ptr->flags6));
807                         break;
808                 }
809
810                 case SUMMON_HI_DRAGON_LIVING:
811                 {
812                         okay = ((r_ptr->d_char == 'D') && monster_living(r_ptr));
813                         break;
814                 }
815
816                 case SUMMON_LIVING:
817                 {
818                         okay = monster_living(r_ptr);
819                         break;
820                 }
821
822                 case SUMMON_PHANTOM:
823                 {
824                         okay = (r_idx == MON_PHANTOM_B || r_idx == MON_PHANTOM_W);
825                         break;
826                 }
827
828                 case SUMMON_BLUE_HORROR:
829                 {
830                         okay = (r_idx == MON_BLUE_HORROR);
831                         break;
832                 }
833
834                 case SUMMON_ELEMENTAL:
835                 {
836                         okay = (r_ptr->d_char == 'E');
837                         break;
838                 }
839
840                 case SUMMON_VORTEX:
841                 {
842                         okay = (r_ptr->d_char == 'v');
843                         break;
844                 }
845
846                 case SUMMON_HYBRID:
847                 {
848                         okay = (r_ptr->d_char == 'H');
849                         break;
850                 }
851
852                 case SUMMON_BIRD:
853                 {
854                         okay = (r_ptr->d_char == 'B');
855                         break;
856                 }
857
858                 case SUMMON_KAMIKAZE:
859                 {
860                         int i;
861                         for (i = 0; i < 4; i++)
862                                 if (r_ptr->blow[i].method == RBM_EXPLODE) okay = TRUE;
863                         break;
864                 }
865
866                 case SUMMON_KAMIKAZE_LIVING:
867                 {
868                         int i;
869
870                         for (i = 0; i < 4; i++)
871                                 if (r_ptr->blow[i].method == RBM_EXPLODE) okay = TRUE;
872                         okay = (okay && monster_living(r_ptr));
873                         break;
874                 }
875
876                 case SUMMON_MANES:
877                 {
878                         okay = (r_idx == MON_MANES);
879                         break;
880                 }
881
882                 case SUMMON_LOUSE:
883                 {
884                         okay = (r_idx == MON_LOUSE);
885                         break;
886                 }
887
888                 case SUMMON_GUARDIANS:
889                 {
890                         okay = (r_ptr->flags7 & RF7_GUARDIAN);
891                         break;
892                 }
893
894                 case SUMMON_KNIGHTS:
895                 {
896                         okay = ((r_idx == MON_NOV_PALADIN) ||
897                                 (r_idx == MON_NOV_PALADIN_G) ||
898                                 (r_idx == MON_PALADIN) ||
899                                 (r_idx == MON_W_KNIGHT) ||
900                                 (r_idx == MON_ULTRA_PALADIN) ||
901                                 (r_idx == MON_KNI_TEMPLAR));
902                         break;
903                 }
904
905                 case SUMMON_EAGLES:
906                 {
907                         okay = (r_ptr->d_char == 'B' &&
908                                 (r_ptr->flags8 & RF8_WILD_MOUNTAIN) &&
909                                 (r_ptr->flags8 & RF8_WILD_ONLY));
910                         break;
911                 }
912
913                 case SUMMON_PIRANHAS:
914                 {
915                         okay = (r_idx == MON_PIRANHA);
916                         break;
917                 }
918
919                 case SUMMON_ARMAGE_GOOD:
920                 {
921                         okay = (r_ptr->d_char == 'A' && (r_ptr->flags3 & RF3_GOOD));
922                         break;
923                 }
924
925                 case SUMMON_ARMAGE_EVIL:
926                 {
927                         okay = ((r_ptr->flags3 & RF3_DEMON) ||
928                                 (r_ptr->d_char == 'A' && (r_ptr->flags3 & RF3_EVIL)));
929                         break;
930                 }
931         }
932
933         /* Result */
934         /* Since okay is int, "return (okay);" is not correct. */
935         return (bool)(okay ? TRUE : FALSE);
936 }
937
938
939 static int chameleon_change_m_idx = 0;
940
941
942 /*
943  * Some dungeon types restrict the possible monsters.
944  * Return TRUE is the monster is OK and FALSE otherwise
945  */
946 static bool restrict_monster_to_dungeon(int r_idx)
947 {
948         dungeon_info_type *d_ptr = &d_info[dungeon_type];
949         monster_race *r_ptr = &r_info[r_idx];
950         byte a;
951
952         if (d_ptr->flags1 & DF1_CHAMELEON)
953         {
954                 if (chameleon_change_m_idx) return TRUE;
955         }
956         if (d_ptr->flags1 & DF1_NO_MAGIC)
957         {
958                 if (r_idx != MON_CHAMELEON &&
959                     r_ptr->freq_spell && 
960                     !(r_ptr->flags4 & RF4_NOMAGIC_MASK) &&
961                     !(r_ptr->flags5 & RF5_NOMAGIC_MASK) &&
962                     !(r_ptr->flags6 & RF6_NOMAGIC_MASK))
963                         return FALSE;
964         }
965         if (d_ptr->flags1 & DF1_NO_MELEE)
966         {
967                 if (r_idx == MON_CHAMELEON) return TRUE;
968                 if (!(r_ptr->flags4 & (RF4_BOLT_MASK | RF4_BEAM_MASK | RF4_BALL_MASK)) &&
969                     !(r_ptr->flags5 & (RF5_BOLT_MASK | RF5_BEAM_MASK | RF5_BALL_MASK | RF5_CAUSE_1 | RF5_CAUSE_2 | RF5_CAUSE_3 | RF5_CAUSE_4 | RF5_MIND_BLAST | RF5_BRAIN_SMASH)) &&
970                     !(r_ptr->flags6 & (RF6_BOLT_MASK | RF6_BEAM_MASK | RF6_BALL_MASK)))
971                         return FALSE;
972         }
973         if (d_ptr->flags1 & DF1_BEGINNER)
974         {
975                 if (r_ptr->level > dun_level)
976                         return FALSE;
977         }
978
979         if (d_ptr->special_div >= 64) return TRUE;
980         if (summon_specific_type && !(d_ptr->flags1 & DF1_CHAMELEON)) return TRUE;
981
982         switch (d_ptr->mode)
983         {
984         case DUNGEON_MODE_AND:
985                 if (d_ptr->mflags1)
986                 {
987                         if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
988                                 return FALSE;
989                 }
990                 if (d_ptr->mflags2)
991                 {
992                         if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
993                                 return FALSE;
994                 }
995                 if (d_ptr->mflags3)
996                 {
997                         if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
998                                 return FALSE;
999                 }
1000                 if (d_ptr->mflags4)
1001                 {
1002                         if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
1003                                 return FALSE;
1004                 }
1005                 if (d_ptr->mflags5)
1006                 {
1007                         if ((d_ptr->mflags5 & r_ptr->flags5) != d_ptr->mflags5)
1008                                 return FALSE;
1009                 }
1010                 if (d_ptr->mflags6)
1011                 {
1012                         if ((d_ptr->mflags6 & r_ptr->flags6) != d_ptr->mflags6)
1013                                 return FALSE;
1014                 }
1015                 if (d_ptr->mflags7)
1016                 {
1017                         if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
1018                                 return FALSE;
1019                 }
1020                 if (d_ptr->mflags8)
1021                 {
1022                         if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
1023                                 return FALSE;
1024                 }
1025                 if (d_ptr->mflags9)
1026                 {
1027                         if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
1028                                 return FALSE;
1029                 }
1030                 if (d_ptr->mflagsr)
1031                 {
1032                         if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
1033                                 return FALSE;
1034                 }
1035                 for (a = 0; a < 5; a++)
1036                         if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char)) return FALSE;
1037
1038                 return TRUE;
1039
1040         case DUNGEON_MODE_NAND:
1041                 if (d_ptr->mflags1)
1042                 {
1043                         if ((d_ptr->mflags1 & r_ptr->flags1) != d_ptr->mflags1)
1044                                 return TRUE;
1045                 }
1046                 if (d_ptr->mflags2)
1047                 {
1048                         if ((d_ptr->mflags2 & r_ptr->flags2) != d_ptr->mflags2)
1049                                 return TRUE;
1050                 }
1051                 if (d_ptr->mflags3)
1052                 {
1053                         if ((d_ptr->mflags3 & r_ptr->flags3) != d_ptr->mflags3)
1054                                 return TRUE;
1055                 }
1056                 if (d_ptr->mflags4)
1057                 {
1058                         if ((d_ptr->mflags4 & r_ptr->flags4) != d_ptr->mflags4)
1059                                 return TRUE;
1060                 }
1061                 if (d_ptr->mflags5)
1062                 {
1063                         if ((d_ptr->mflags5 & r_ptr->flags5) != d_ptr->mflags5)
1064                                 return TRUE;
1065                 }
1066                 if (d_ptr->mflags6)
1067                 {
1068                         if ((d_ptr->mflags6 & r_ptr->flags6) != d_ptr->mflags6)
1069                                 return TRUE;
1070                 }
1071                 if (d_ptr->mflags7)
1072                 {
1073                         if ((d_ptr->mflags7 & r_ptr->flags7) != d_ptr->mflags7)
1074                                 return TRUE;
1075                 }
1076                 if (d_ptr->mflags8)
1077                 {
1078                         if ((d_ptr->mflags8 & r_ptr->flags8) != d_ptr->mflags8)
1079                                 return TRUE;
1080                 }
1081                 if (d_ptr->mflags9)
1082                 {
1083                         if ((d_ptr->mflags9 & r_ptr->flags9) != d_ptr->mflags9)
1084                                 return TRUE;
1085                 }
1086                 if (d_ptr->mflagsr)
1087                 {
1088                         if ((d_ptr->mflagsr & r_ptr->flagsr) != d_ptr->mflagsr)
1089                                 return TRUE;
1090                 }
1091                 for (a = 0; a < 5; a++)
1092                         if (d_ptr->r_char[a] && (d_ptr->r_char[a] != r_ptr->d_char)) return TRUE;
1093
1094                 return FALSE;
1095
1096         case DUNGEON_MODE_OR:
1097                 if (r_ptr->flags1 & d_ptr->mflags1) return TRUE;
1098                 if (r_ptr->flags2 & d_ptr->mflags2) return TRUE;
1099                 if (r_ptr->flags3 & d_ptr->mflags3) return TRUE;
1100                 if (r_ptr->flags4 & d_ptr->mflags4) return TRUE;
1101                 if (r_ptr->flags5 & d_ptr->mflags5) return TRUE;
1102                 if (r_ptr->flags6 & d_ptr->mflags6) return TRUE;
1103                 if (r_ptr->flags7 & d_ptr->mflags7) return TRUE;
1104                 if (r_ptr->flags8 & d_ptr->mflags8) return TRUE;
1105                 if (r_ptr->flags9 & d_ptr->mflags9) return TRUE;
1106                 if (r_ptr->flagsr & d_ptr->mflagsr) return TRUE;
1107                 for (a = 0; a < 5; a++)
1108                         if (d_ptr->r_char[a] == r_ptr->d_char) return TRUE;
1109
1110                 return FALSE;
1111
1112         case DUNGEON_MODE_NOR:
1113                 if (r_ptr->flags1 & d_ptr->mflags1) return FALSE;
1114                 if (r_ptr->flags2 & d_ptr->mflags2) return FALSE;
1115                 if (r_ptr->flags3 & d_ptr->mflags3) return FALSE;
1116                 if (r_ptr->flags4 & d_ptr->mflags4) return FALSE;
1117                 if (r_ptr->flags5 & d_ptr->mflags5) return FALSE;
1118                 if (r_ptr->flags6 & d_ptr->mflags6) return FALSE;
1119                 if (r_ptr->flags7 & d_ptr->mflags7) return FALSE;
1120                 if (r_ptr->flags8 & d_ptr->mflags8) return FALSE;
1121                 if (r_ptr->flags9 & d_ptr->mflags9) return FALSE;
1122                 if (r_ptr->flagsr & d_ptr->mflagsr) return FALSE;
1123                 for (a = 0; a < 5; a++)
1124                         if (d_ptr->r_char[a] == r_ptr->d_char) return FALSE;
1125
1126                 return TRUE;
1127         }
1128
1129         return TRUE;
1130 }
1131
1132 /*
1133  * Apply a "monster restriction function" to the "monster allocation table"
1134  */
1135 errr get_mon_num_prep(monster_hook_type monster_hook,
1136                                           monster_hook_type monster_hook2)
1137 {
1138         int i;
1139
1140         /* Todo: Check the hooks for non-changes */
1141
1142         /* Set the new hooks */
1143         get_mon_num_hook = monster_hook;
1144         get_mon_num2_hook = monster_hook2;
1145
1146         /* Scan the allocation table */
1147         for (i = 0; i < alloc_race_size; i++)
1148         {
1149                 monster_race    *r_ptr;
1150                 
1151                 /* Get the entry */
1152                 alloc_entry *entry = &alloc_race_table[i];
1153
1154                 entry->prob2 = 0;
1155                 r_ptr = &r_info[entry->index];
1156
1157                 /* Skip monsters which don't pass the restriction */
1158                 if ((get_mon_num_hook && !((*get_mon_num_hook)(entry->index))) ||
1159                     (get_mon_num2_hook && !((*get_mon_num2_hook)(entry->index))))
1160                         continue;
1161
1162                 if (!p_ptr->inside_battle && !chameleon_change_m_idx &&
1163                     summon_specific_type != SUMMON_GUARDIANS)
1164                 {
1165                         /* Hack -- don't create questors */
1166                         if (r_ptr->flags1 & RF1_QUESTOR)
1167                                 continue;
1168
1169                         if (r_ptr->flags7 & RF7_GUARDIAN)
1170                                 continue;
1171
1172                         /* Depth Monsters never appear out of depth */
1173                         if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) &&
1174                             (r_ptr->level > dun_level))
1175                                 continue;
1176                 }
1177
1178                 /* Accept this monster */
1179                 entry->prob2 = entry->prob1;
1180
1181                 if (dun_level && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest)) && !restrict_monster_to_dungeon(entry->index) && !p_ptr->inside_battle)
1182                 {
1183                         int hoge = entry->prob2 * d_info[dungeon_type].special_div;
1184                         entry->prob2 = hoge / 64;
1185                         if (randint0(64) < (hoge & 0x3f)) entry->prob2++;
1186                 }
1187         }
1188
1189         /* Success */
1190         return (0);
1191 }
1192
1193
1194 static int mysqrt(int n)
1195 {
1196         int tmp = n>>1;
1197         int tasu = 10;
1198         int kaeriti = 1;
1199
1200         if (!tmp)
1201         {
1202                 if (n) return 1;
1203                 else return 0;
1204         }
1205
1206         while(tmp)
1207         {
1208                 if ((n/tmp) < tmp)
1209                 {
1210                         tmp >>= 1;
1211                 }
1212                 else break;
1213         }
1214         kaeriti = tmp;
1215         while(tasu)
1216         {
1217                 if ((n/tmp) < tmp)
1218                 {
1219                         tasu--;
1220                         tmp = kaeriti;
1221                 }
1222                 else
1223                 {
1224                         kaeriti = tmp;
1225                         tmp += tasu;
1226                 }
1227         }
1228         return kaeriti;
1229 }
1230
1231 /*
1232  * Choose a monster race that seems "appropriate" to the given level
1233  *
1234  * This function uses the "prob2" field of the "monster allocation table",
1235  * and various local information, to calculate the "prob3" field of the
1236  * same table, which is then used to choose an "appropriate" monster, in
1237  * a relatively efficient manner.
1238  *
1239  * Note that "town" monsters will *only* be created in the town, and
1240  * "normal" monsters will *never* be created in the town, unless the
1241  * "level" is "modified", for example, by polymorph or summoning.
1242  *
1243  * There is a small chance (1/50) of "boosting" the given depth by
1244  * a small amount (up to four levels), except in the town.
1245  *
1246  * It is (slightly) more likely to acquire a monster of the given level
1247  * than one of a lower level.  This is done by choosing several monsters
1248  * appropriate to the given level and keeping the "hardest" one.
1249  *
1250  * Note that if no monsters are "appropriate", then this function will
1251  * fail, and return zero, but this should *almost* never happen.
1252  */
1253 s16b get_mon_num(int level)
1254 {
1255         int                     i, j, p;
1256         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, 0);
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, 0);
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, 0);
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, 0);
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
2872
2873 /*
2874  *  Hook for Tanuki
2875  */
2876 static bool monster_hook_tanuki(int r_idx)
2877 {
2878         monster_race *r_ptr = &r_info[r_idx];
2879
2880         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2881         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2882         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2883         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2884         
2885         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))
2886                 return FALSE;
2887
2888         return (*(get_monster_hook()))(r_idx);
2889 }
2890
2891
2892 /*
2893  *  Set initial racial appearance of a monster
2894  */
2895 static int initial_r_appearance(int r_idx)
2896 {
2897         int attempts = 1000;
2898
2899         int ap_r_idx;
2900         int min = MIN(base_level-5, 50);
2901
2902         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2903                 return r_idx;
2904
2905         get_mon_num_prep(monster_hook_tanuki, NULL);
2906
2907         while (--attempts)
2908         {
2909                 ap_r_idx = get_mon_num(base_level + 10);
2910                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2911         }
2912
2913         return r_idx;
2914 }
2915
2916
2917 /*
2918  * Get initial monster speed
2919  */
2920 byte get_mspeed(monster_race *r_ptr)
2921 {
2922         /* Extract the monster base speed */
2923         int mspeed = r_ptr->speed;
2924
2925         /* Hack -- small racial variety */
2926         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2927         {
2928                 /* Allow some small variation per monster */
2929                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2930                 if (i) mspeed += rand_spread(0, i);
2931         }
2932
2933         if (mspeed > 199) mspeed = 199;
2934
2935         return (byte)mspeed;
2936 }
2937
2938
2939 /*
2940  * Attempt to place a monster of the given race at the given location.
2941  *
2942  * To give the player a sporting chance, any monster that appears in
2943  * line-of-sight and is extremely dangerous can be marked as
2944  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2945  * which often (but not always) lets the player move before they do.
2946  *
2947  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2948  *
2949  * XXX XXX XXX Use special "here" and "dead" flags for unique monsters,
2950  * remove old "cur_num" and "max_num" fields.
2951  *
2952  * XXX XXX XXX Actually, do something similar for artifacts, to simplify
2953  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2954  *
2955  * This is the only function which may place a monster in the dungeon,
2956  * except for the savefile loading code.
2957  */
2958 static bool place_monster_one(int who, int y, int x, int r_idx, u32b mode)
2959 {
2960         /* Access the location */
2961         cave_type               *c_ptr = &cave[y][x];
2962
2963         monster_type    *m_ptr;
2964
2965         monster_race    *r_ptr = &r_info[r_idx];
2966
2967         cptr            name = (r_name + r_ptr->name);
2968
2969         int cmi;
2970
2971         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
2972         if (p_ptr->wild_mode) return FALSE;
2973
2974         /* Verify location */
2975         if (!in_bounds(y, x)) return (FALSE);
2976
2977         /* Paranoia */
2978         if (!r_idx) return (FALSE);
2979
2980         /* Paranoia */
2981         if (!r_ptr->name) return (FALSE);
2982
2983         if (!(mode & PM_IGNORE_TERRAIN))
2984         {
2985                 /* Not on the Pattern */
2986                 if (pattern_tile(y, x)) return FALSE;
2987
2988                 /* Require empty space (if not ghostly) */
2989                 if (!monster_can_enter(y, x, r_ptr, 0)) return FALSE;
2990         }
2991
2992         if (!p_ptr->inside_battle)
2993         {
2994                 /* Hack -- "unique" monsters must be "unique" */
2995                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
2996                      (r_ptr->flags7 & (RF7_NAZGUL))) &&
2997                     (r_ptr->cur_num >= r_ptr->max_num))
2998                 {
2999                         /* Cannot create */
3000                         return (FALSE);
3001                 }
3002
3003                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
3004                     (r_ptr->cur_num >= 1))
3005                 {
3006                         return (FALSE);
3007                 }
3008
3009                 if (r_idx == MON_BANORLUPART)
3010                 {
3011                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
3012                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
3013                 }
3014
3015                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
3016                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (dun_level < r_ptr->level) &&
3017                     (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
3018                 {
3019                         /* Cannot create */
3020                         return (FALSE);
3021                 }
3022         }
3023
3024         if (quest_number(dun_level))
3025         {
3026                 int hoge = quest_number(dun_level);
3027                 if ((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
3028                 {
3029                         if(r_idx == quest[hoge].r_idx)
3030                         {
3031                                 int number_mon, i2, j2;
3032                                 number_mon = 0;
3033
3034                                 /* Count all quest monsters */
3035                                 for (i2 = 0; i2 < cur_wid; ++i2)
3036                                         for (j2 = 0; j2 < cur_hgt; j2++)
3037                                                 if (cave[j2][i2].m_idx > 0)
3038                                                         if (m_list[cave[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
3039                                                                 number_mon++;
3040                                 if(number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
3041                                         return FALSE;
3042                         }
3043                 }
3044         }
3045
3046         if (is_glyph_grid(c_ptr))
3047         {
3048                 if (randint1(BREAK_GLYPH) < (r_ptr->level+20))
3049                 {
3050                         /* Describe observable breakage */
3051                         if (c_ptr->info & CAVE_MARK)
3052                         {
3053 #ifdef JP
3054 msg_print("¼é¤ê¤Î¥ë¡¼¥ó¤¬²õ¤ì¤¿¡ª");
3055 #else
3056                                 msg_print("The rune of protection is broken!");
3057 #endif
3058
3059                         }
3060
3061                         /* Forget the rune */
3062                         c_ptr->info &= ~(CAVE_MARK);
3063
3064                         /* Break the rune */
3065                         c_ptr->info &= ~(CAVE_OBJECT);
3066                         c_ptr->mimic = 0;
3067
3068                         /* Notice */
3069                         note_spot(y, x);
3070                 }
3071                 else return FALSE;
3072         }
3073
3074         /* Powerful monster */
3075         if (r_ptr->level > dun_level)
3076         {
3077                 /* Unique monsters */
3078                 if (r_ptr->flags1 & (RF1_UNIQUE))
3079                 {
3080                         /* Message for cheaters */
3081 #ifdef JP
3082                         if (cheat_hear) msg_format("¿¼ÁؤΥæ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3083 #else
3084                         if (cheat_hear) msg_format("Deep Unique (%s).", name);
3085 #endif
3086                 }
3087
3088                 /* Normal monsters */
3089                 else
3090                 {
3091                         /* Message for cheaters */
3092 #ifdef JP
3093                         if (cheat_hear) msg_format("¿¼ÁؤΥâ¥ó¥¹¥¿¡¼ (%s)¡£", name);
3094 #else
3095                         if (cheat_hear) msg_format("Deep Monster (%s).", name);
3096 #endif
3097                 }
3098         }
3099
3100         /* Note the monster */
3101         else if (r_ptr->flags1 & (RF1_UNIQUE))
3102         {
3103                 /* Unique monsters induce message */
3104 #ifdef JP
3105                 if (cheat_hear) msg_format("¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3106 #else
3107                 if (cheat_hear) msg_format("Unique (%s).", name);
3108 #endif
3109
3110         }
3111
3112         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL) || (r_ptr->level < 10)) mode &= ~PM_KAGE;
3113
3114         /* Make a new monster */
3115         c_ptr->m_idx = m_pop();
3116         hack_m_idx_ii = c_ptr->m_idx;
3117
3118         /* Mega-Hack -- catch "failure" */
3119         if (!c_ptr->m_idx) return (FALSE);
3120
3121
3122         /* Get a new monster record */
3123         m_ptr = &m_list[c_ptr->m_idx];
3124
3125         /* Save the race */
3126         m_ptr->r_idx = r_idx;
3127         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
3128
3129         /* No flags */
3130         m_ptr->mflag = 0;
3131         m_ptr->mflag2 = 0;
3132
3133         /* Hack -- Appearance transfer */
3134         if ((mode & PM_MULTIPLY) && (who > 0) && !is_original_ap(&m_list[who]))
3135         {
3136                 m_ptr->ap_r_idx = m_list[who].ap_r_idx;
3137
3138                 /* Hack -- Shadower spawns Shadower */
3139                 if (m_list[who].mflag2 & MFLAG2_KAGE) m_ptr->mflag2 |= MFLAG2_KAGE;
3140         }
3141
3142         /* Sub-alignment of a monster */
3143         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
3144                 m_ptr->sub_align = m_list[who].sub_align;
3145         else
3146         {
3147                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3148                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
3149                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
3150         }
3151
3152         /* Place the monster at the location */
3153         m_ptr->fy = y;
3154         m_ptr->fx = x;
3155
3156
3157         /* No "timed status" yet */
3158         for (cmi = 0; cmi < MAX_MTIMED; cmi++) m_ptr->mtimed[cmi] = 0;
3159
3160         /* Unknown distance */
3161         m_ptr->cdis = 0;
3162
3163         reset_target(m_ptr);
3164
3165         m_ptr->nickname = 0;
3166
3167         m_ptr->exp = 0;
3168
3169
3170         /* Your pet summons its pet. */
3171         if (who > 0 && is_pet(&m_list[who]))
3172         {
3173                 mode |= PM_FORCE_PET;
3174                 m_ptr->parent_m_idx = who;
3175         }
3176         else
3177         {
3178                 m_ptr->parent_m_idx = 0;
3179         }
3180
3181         if (r_ptr->flags7 & RF7_CHAMELEON)
3182         {
3183                 choose_new_monster(c_ptr->m_idx, TRUE, 0);
3184                 r_ptr = &r_info[m_ptr->r_idx];
3185                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
3186
3187                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
3188                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
3189                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3190         }
3191         else if ((mode & PM_KAGE) && !(mode & PM_FORCE_PET))
3192         {
3193                 m_ptr->ap_r_idx = MON_KAGE;
3194                 m_ptr->mflag2 |= MFLAG2_KAGE;
3195         }
3196
3197         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
3198
3199         /* Not visible */
3200         m_ptr->ml = FALSE;
3201
3202         /* Pet? */
3203         if (mode & PM_FORCE_PET)
3204         {
3205                 set_pet(m_ptr);
3206         }
3207         /* Friendly? */
3208         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
3209                  (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(who))
3210         {
3211                 if (!monster_has_hostile_align(NULL, 0, -1, r_ptr)) set_friendly(m_ptr);
3212         }
3213
3214         /* Assume no sleeping */
3215         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
3216
3217         /* Enforce sleeping if needed */
3218         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
3219         {
3220                 int val = r_ptr->sleep;
3221                 (void)set_monster_csleep(c_ptr->m_idx, (val * 2) + randint1(val * 10));
3222         }
3223
3224         /* Assign maximal hitpoints */
3225         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
3226         {
3227                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
3228         }
3229         else
3230         {
3231                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
3232         }
3233
3234         /* Monsters have double hitpoints in Nightmare mode */
3235         if (ironman_nightmare)
3236         {
3237                 u32b hp = m_ptr->max_maxhp * 2L;
3238
3239                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
3240         }
3241
3242         m_ptr->maxhp = m_ptr->max_maxhp;
3243
3244         /* And start out fully healthy */
3245         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
3246                 m_ptr->hp = m_ptr->maxhp / 2;
3247         else m_ptr->hp = m_ptr->maxhp;
3248
3249
3250         /* Extract the monster base speed */
3251         m_ptr->mspeed = get_mspeed(r_ptr);
3252
3253         if (mode & PM_HASTE) (void)set_monster_fast(c_ptr->m_idx, 100);
3254
3255         /* Give a random starting energy */
3256         if (!ironman_nightmare)
3257         {
3258                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
3259         }
3260         else
3261         {
3262                 /* Nightmare monsters are more prepared */
3263                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
3264         }
3265
3266         /* Force monster to wait for player, unless in Nightmare mode */
3267         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
3268         {
3269                 /* Monster is still being nice */
3270                 m_ptr->mflag |= (MFLAG_NICE);
3271
3272                 /* Must repair monsters */
3273                 repair_monsters = TRUE;
3274         }
3275
3276         /* Hack -- see "process_monsters()" */
3277         if (c_ptr->m_idx < hack_m_idx)
3278         {
3279                 /* Monster is still being born */
3280                 m_ptr->mflag |= (MFLAG_BORN);
3281         }
3282
3283
3284         if (r_ptr->flags7 & RF7_SELF_LD_MASK)
3285                 p_ptr->update |= (PU_MON_LITE);
3286         else if ((r_ptr->flags7 & RF7_HAS_LD_MASK) && !MON_CSLEEP(m_ptr))
3287                 p_ptr->update |= (PU_MON_LITE);
3288
3289         /* Update the monster */
3290         update_mon(c_ptr->m_idx, TRUE);
3291
3292
3293         /* Count the monsters on the level */
3294         real_r_ptr(m_ptr)->cur_num++;
3295
3296         /*
3297          * Memorize location of the unique monster in saved floors.
3298          * A unique monster move from old saved floor.
3299          */
3300         if (character_dungeon &&
3301             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)))
3302                 real_r_ptr(m_ptr)->floor_id = p_ptr->floor_id;
3303
3304         /* Hack -- Count the number of "reproducers" */
3305         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
3306
3307         /* Hack -- Notice new multi-hued monsters */
3308         {
3309                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
3310                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
3311                         shimmer_monsters = TRUE;
3312         }
3313
3314         if (p_ptr->warning && character_dungeon)
3315         {
3316                 if (r_ptr->flags1 & RF1_UNIQUE)
3317                 {
3318                         cptr color;
3319                         object_type *o_ptr;
3320                         char o_name[MAX_NLEN];
3321
3322                         if (r_ptr->level > p_ptr->lev + 30)
3323 #ifdef JP
3324                                 color = "¹õ¤¯";
3325 #else
3326                                 color = "black";
3327 #endif
3328                         else if (r_ptr->level > p_ptr->lev + 15)
3329 #ifdef JP
3330                                 color = "»ç¿§¤Ë";
3331 #else
3332                                 color = "purple";
3333 #endif
3334                         else if (r_ptr->level > p_ptr->lev + 5)
3335 #ifdef JP
3336                                 color = "¥ë¥Ó¡¼¿§¤Ë";
3337 #else
3338                                 color = "deep red";
3339 #endif
3340                         else if (r_ptr->level > p_ptr->lev - 5)
3341 #ifdef JP
3342                                 color = "ÀÖ¤¯";
3343 #else
3344                                 color = "red";
3345 #endif
3346                         else if (r_ptr->level > p_ptr->lev - 15)
3347 #ifdef JP
3348                                 color = "¥Ô¥ó¥¯¿§¤Ë";
3349 #else
3350                                 color = "pink";
3351 #endif
3352                         else
3353 #ifdef JP
3354                                 color = "Çò¤¯";
3355 #else
3356                                 color = "white";
3357 #endif
3358
3359                         o_ptr = choose_warning_item();
3360                         if (o_ptr)
3361                         {
3362                                 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
3363 #ifdef JP
3364                                 msg_format("%s¤Ï%s¸÷¤Ã¤¿¡£", o_name, color);
3365 #else
3366                                 msg_format("%s glows %s.", o_name, color);
3367 #endif
3368                         }
3369                         else
3370                         {
3371 #ifdef JP
3372                                 msg_format("%s¸÷¤ëʪ¤¬Æ¬¤ËÉ⤫¤ó¤À¡£", color);
3373 #else
3374                                 msg_format("An %s image forms in your mind.");
3375 #endif
3376                         }
3377                 }
3378         }
3379
3380         if (is_explosive_rune_grid(c_ptr))
3381         {
3382                 /* Break the ward */
3383                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3384                 {
3385                         /* Describe observable breakage */
3386                         if (c_ptr->info & CAVE_MARK)
3387                         {
3388 #ifdef JP
3389 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3390 #else
3391                                 msg_print("The rune explodes!");
3392 #endif
3393
3394                                 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);
3395                         }
3396                 }
3397                 else
3398                 {
3399 #ifdef JP
3400 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3401 #else
3402                         msg_print("An explosive rune was disarmed.");
3403 #endif
3404                 }
3405
3406                 /* Forget the rune */
3407                 c_ptr->info &= ~(CAVE_MARK);
3408
3409                 /* Break the rune */
3410                 c_ptr->info &= ~(CAVE_OBJECT);
3411                 c_ptr->mimic = 0;
3412
3413                 note_spot(y, x);
3414                 lite_spot(y, x);
3415         }
3416
3417         /* Success */
3418         return (TRUE);
3419 }
3420
3421
3422 /*
3423  *  improved version of scatter() for place monster
3424  */
3425
3426 #define MON_SCAT_MAXD 10
3427
3428 static bool mon_scatter(int r_idx, int *yp, int *xp, int y, int x, int max_dist)
3429 {
3430         int place_x[MON_SCAT_MAXD];
3431         int place_y[MON_SCAT_MAXD];
3432         int num[MON_SCAT_MAXD];
3433         int i;
3434         int nx, ny;
3435
3436         if (max_dist >= MON_SCAT_MAXD)
3437                 return FALSE;
3438
3439         for (i = 0; i < MON_SCAT_MAXD; i++)
3440                 num[i] = 0;
3441
3442         for (nx = x - max_dist; nx <= x + max_dist; nx++)
3443         {
3444                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
3445                 {
3446                         /* Ignore annoying locations */
3447                         if (!in_bounds(ny, nx)) continue;
3448
3449                         /* Require "line of projection" */
3450                         if (!projectable(y, x, ny, nx)) continue;
3451
3452                         if (r_idx > 0)
3453                         {
3454                                 monster_race *r_ptr = &r_info[r_idx];
3455
3456                                 /* Require empty space (if not ghostly) */
3457                                 if (!monster_can_enter(ny, nx, r_ptr, 0))
3458                                         continue;
3459                         }
3460                         else
3461                         {
3462                                 /* Walls and Monsters block flow */
3463                                 if (!cave_empty_bold2(ny, nx)) continue;
3464
3465                                 /* ... nor on the Pattern */
3466                                 if (pattern_tile(ny, nx)) continue;
3467                         }
3468
3469                         i = distance(y, x, ny, nx);
3470
3471                         if (i > max_dist)
3472                                 continue;
3473
3474                         num[i]++;
3475
3476                         /* random swap */
3477                         if (one_in_(num[i]))
3478                         {
3479                                 place_x[i] = nx;
3480                                 place_y[i] = ny;
3481                         }
3482                 }
3483         }
3484
3485         i = 0;
3486         while (i < MON_SCAT_MAXD && 0 == num[i])
3487                 i++;
3488         if (i >= MON_SCAT_MAXD)
3489                 return FALSE;
3490
3491         *xp = place_x[i];
3492         *yp = place_y[i];
3493
3494         return TRUE;
3495 }
3496
3497
3498 /*
3499  * Maximum size of a group of monsters
3500  */
3501 #define GROUP_MAX       32
3502
3503
3504 /*
3505  * Attempt to place a "group" of monsters around the given location
3506  */
3507 static bool place_monster_group(int who, int y, int x, int r_idx, u32b mode)
3508 {
3509         monster_race *r_ptr = &r_info[r_idx];
3510
3511         int n, i;
3512         int total = 0, extra = 0;
3513
3514         int hack_n = 0;
3515
3516         byte hack_y[GROUP_MAX];
3517         byte hack_x[GROUP_MAX];
3518
3519
3520         /* Pick a group size */
3521         total = randint1(10);
3522
3523         /* Hard monsters, small groups */
3524         if (r_ptr->level > dun_level)
3525         {
3526                 extra = r_ptr->level - dun_level;
3527                 extra = 0 - randint1(extra);
3528         }
3529
3530         /* Easy monsters, large groups */
3531         else if (r_ptr->level < dun_level)
3532         {
3533                 extra = dun_level - r_ptr->level;
3534                 extra = randint1(extra);
3535         }
3536
3537         /* Hack -- limit group reduction */
3538         if (extra > 9) extra = 9;
3539
3540         /* Modify the group size */
3541         total += extra;
3542
3543         /* Minimum size */
3544         if (total < 1) total = 1;
3545
3546         /* Maximum size */
3547         if (total > GROUP_MAX) total = GROUP_MAX;
3548
3549
3550         /* Start on the monster */
3551         hack_n = 1;
3552         hack_x[0] = x;
3553         hack_y[0] = y;
3554
3555         /* Puddle monsters, breadth first, up to total */
3556         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3557         {
3558                 /* Grab the location */
3559                 int hx = hack_x[n];
3560                 int hy = hack_y[n];
3561
3562                 /* Check each direction, up to total */
3563                 for (i = 0; (i < 8) && (hack_n < total); i++)
3564                 {
3565                         int mx, my;
3566
3567                         scatter(&my, &mx, hy, hx, 4, 0);
3568
3569                         /* Walls and Monsters block flow */
3570                         if (!cave_empty_bold2(my, mx)) continue;
3571
3572                         /* Attempt to place another monster */
3573                         if (place_monster_one(who, my, mx, r_idx, mode))
3574                         {
3575                                 /* Add it to the "hack" set */
3576                                 hack_y[hack_n] = my;
3577                                 hack_x[hack_n] = mx;
3578                                 hack_n++;
3579                         }
3580                 }
3581         }
3582
3583
3584         /* Success */
3585         return (TRUE);
3586 }
3587
3588
3589 /*
3590  * Hack -- help pick an escort type
3591  */
3592 static int place_monster_idx = 0;
3593 static int place_monster_m_idx = 0;
3594
3595 /*
3596  * Hack -- help pick an escort type
3597  */
3598 static bool place_monster_okay(int r_idx)
3599 {
3600         monster_race *r_ptr = &r_info[place_monster_idx];
3601         monster_type *m_ptr = &m_list[place_monster_m_idx];
3602
3603         monster_race *z_ptr = &r_info[r_idx];
3604
3605         /* Hack - Escorts have to have the same dungeon flag */
3606         if (mon_hook_dungeon(place_monster_idx) != mon_hook_dungeon(r_idx)) return (FALSE);
3607
3608         /* Require similar "race" */
3609         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3610
3611         /* Skip more advanced monsters */
3612         if (z_ptr->level > r_ptr->level) return (FALSE);
3613
3614         /* Skip unique monsters */
3615         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3616
3617         /* Paranoia -- Skip identical monsters */
3618         if (place_monster_idx == r_idx) return (FALSE);
3619
3620         /* Skip different alignment */
3621         if (monster_has_hostile_align(m_ptr, 0, 0, z_ptr)) return FALSE;
3622
3623         if (r_ptr->flags7 & RF7_FRIENDLY)
3624         {
3625                 if (monster_has_hostile_align(NULL, 1, -1, z_ptr)) return FALSE;
3626         }
3627
3628         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3629                 return FALSE;
3630
3631         /* Okay */
3632         return (TRUE);
3633 }
3634
3635
3636 /*
3637  * Attempt to place a monster of the given race at the given location
3638  *
3639  * Note that certain monsters are now marked as requiring "friends".
3640  * These monsters, if successfully placed, and if the "grp" parameter
3641  * is TRUE, will be surrounded by a "group" of identical monsters.
3642  *
3643  * Note that certain monsters are now marked as requiring an "escort",
3644  * which is a collection of monsters with similar "race" but lower level.
3645  *
3646  * Some monsters induce a fake "group" flag on their escorts.
3647  *
3648  * Note the "bizarre" use of non-recursion to prevent annoying output
3649  * when running a code profiler.
3650  *
3651  * Note the use of the new "monster allocation table" code to restrict
3652  * the "get_mon_num()" function to "legal" escort types.
3653  */
3654 bool place_monster_aux(int who, int y, int x, int r_idx, u32b mode)
3655 {
3656         int             i;
3657         monster_race    *r_ptr = &r_info[r_idx];
3658
3659         if (!(mode & PM_NO_KAGE) && one_in_(333))
3660                 mode |= PM_KAGE;
3661
3662         /* Place one monster, or fail */
3663         if (!place_monster_one(who, y, x, r_idx, mode)) return (FALSE);
3664
3665
3666         /* Require the "group" flag */
3667         if (!(mode & PM_ALLOW_GROUP)) return (TRUE);
3668
3669         place_monster_m_idx = hack_m_idx_ii;
3670
3671         /* Friends for certain monsters */
3672         if (r_ptr->flags1 & (RF1_FRIENDS))
3673         {
3674                 /* Attempt to place a group */
3675                 (void)place_monster_group(who, y, x, r_idx, mode);
3676         }
3677
3678
3679         /* Escorts for certain monsters */
3680         if (r_ptr->flags1 & (RF1_ESCORT))
3681         {
3682                 /* Set the escort index */
3683                 place_monster_idx = r_idx;
3684
3685                 /* Try to place several "escorts" */
3686                 for (i = 0; i < 32; i++)
3687                 {
3688                         int nx, ny, z, d = 3;
3689
3690                         /* Pick a location */
3691                         scatter(&ny, &nx, y, x, d, 0);
3692
3693                         /* Require empty grids */
3694                         if (!cave_empty_bold2(ny, nx)) continue;
3695
3696                         /* Prepare allocation table */
3697                         get_mon_num_prep(place_monster_okay, get_monster_hook2(ny, nx));
3698
3699                         /* Pick a random race */
3700                         z = get_mon_num(r_ptr->level);
3701
3702                         /* Handle failure */
3703                         if (!z) break;
3704
3705                         /* Place a single escort */
3706                         (void)place_monster_one(place_monster_m_idx, ny, nx, z, mode);
3707
3708                         /* Place a "group" of escorts if needed */
3709                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3710                             (r_ptr->flags1 & RF1_ESCORTS))
3711                         {
3712                                 /* Place a group of monsters */
3713                                 (void)place_monster_group(place_monster_m_idx, ny, nx, z, mode);
3714                         }
3715                 }
3716         }
3717
3718         /* Success */
3719         return (TRUE);
3720 }
3721
3722
3723 /*
3724  * Hack -- attempt to place a monster at the given location
3725  *
3726  * Attempt to find a monster appropriate to the "monster_level"
3727  */
3728 bool place_monster(int y, int x, u32b mode)
3729 {
3730         int r_idx;
3731
3732         /* Prepare allocation table */
3733         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3734
3735         /* Pick a monster */
3736         r_idx = get_mon_num(monster_level);
3737
3738         /* Handle failure */
3739         if (!r_idx) return (FALSE);
3740
3741         /* Attempt to place the monster */
3742         if (place_monster_aux(0, y, x, r_idx, mode)) return (TRUE);
3743
3744         /* Oops */
3745         return (FALSE);
3746 }
3747
3748
3749 #ifdef MONSTER_HORDES
3750
3751 bool alloc_horde(int y, int x)
3752 {
3753         monster_race *r_ptr = NULL;
3754         int r_idx = 0;
3755         int m_idx;
3756         int attempts = 1000;
3757         int cy = y;
3758         int cx = x;
3759
3760         /* Prepare allocation table */
3761         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3762
3763         while (--attempts)
3764         {
3765                 /* Pick a monster */
3766                 r_idx = get_mon_num(monster_level);
3767
3768                 /* Handle failure */
3769                 if (!r_idx) return (FALSE);
3770
3771                 r_ptr = &r_info[r_idx];
3772
3773                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3774
3775                 if (r_idx == MON_HAGURE) continue;
3776                 break;
3777         }
3778         if (attempts < 1) return FALSE;
3779
3780         attempts = 1000;
3781
3782         while (--attempts)
3783         {
3784                 /* Attempt to place the monster */
3785                 if (place_monster_aux(0, y, x, r_idx, 0L)) break;
3786         }
3787
3788         if (attempts < 1) return FALSE;
3789
3790         m_idx = cave[y][x].m_idx;
3791
3792         if (m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[m_list[m_idx].r_idx];
3793         summon_kin_type = r_ptr->d_char;
3794
3795         for (attempts = randint1(10) + 5; attempts; attempts--)
3796         {
3797                 scatter(&cy, &cx, y, x, 5, 0);
3798
3799                 (void)summon_specific(m_idx, cy, cx, dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
3800
3801                 y = cy;
3802                 x = cx;
3803         }
3804
3805         return TRUE;
3806 }
3807
3808 #endif /* MONSTER_HORDES */
3809
3810
3811 /*
3812  * Put the Guardian
3813  */
3814 bool alloc_guardian(bool def_val)
3815 {
3816         int guardian = d_info[dungeon_type].final_guardian;
3817
3818         if (guardian && (d_info[dungeon_type].maxdepth == dun_level) && (r_info[guardian].cur_num < r_info[guardian].max_num))
3819         {
3820                 int oy;
3821                 int ox;
3822                 int try = 4000;
3823
3824                 /* Find a good position */
3825                 while (try)
3826                 {
3827                         /* Get a random spot */
3828                         oy = randint1(cur_hgt - 4) + 2;
3829                         ox = randint1(cur_wid - 4) + 2;
3830
3831                         /* Is it a good spot ? */
3832                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[guardian], 0))
3833                         {
3834                                 /* Place the guardian */
3835                                 if (place_monster_aux(0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) return TRUE;
3836                         }
3837
3838                         /* One less try */
3839                         try--;
3840                 }
3841
3842                 return FALSE;
3843         }
3844
3845         return def_val;
3846 }
3847
3848
3849 /*
3850  * Attempt to allocate a random monster in the dungeon.
3851  *
3852  * Place the monster at least "dis" distance from the player.
3853  *
3854  * Use "slp" to choose the initial "sleep" status
3855  *
3856  * Use "monster_level" for the monster level
3857  */
3858 bool alloc_monster(int dis, u32b mode)
3859 {
3860         int                     y = 0, x = 0;
3861         int         attempts_left = 10000;
3862
3863         /* Put the Guardian */
3864         if (alloc_guardian(FALSE)) return TRUE;
3865
3866         /* Find a legal, distant, unoccupied, space */
3867         while (attempts_left--)
3868         {
3869                 /* Pick a location */
3870                 y = randint0(cur_hgt);
3871                 x = randint0(cur_wid);
3872
3873                 /* Require empty floor grid (was "naked") */
3874                 if (dun_level)
3875                 {
3876                         if (!cave_empty_bold2(y, x)) continue;
3877                 }
3878                 else
3879                 {
3880                         if (!cave_empty_bold(y, x)) continue;
3881                 }
3882
3883                 /* Accept far away grids */
3884                 if (distance(y, x, py, px) > dis) break;
3885         }
3886
3887         if (!attempts_left)
3888         {
3889                 if (cheat_xtra || cheat_hear)
3890                 {
3891 #ifdef JP
3892 msg_print("·Ù¹ð¡ª¿·¤¿¤Ê¥â¥ó¥¹¥¿¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡£¾®¤µ¤¤³¬¤Ç¤¹¤«¡©");
3893 #else
3894                         msg_print("Warning! Could not allocate a new monster. Small level?");
3895 #endif
3896
3897                 }
3898
3899                 return (FALSE);
3900         }
3901
3902
3903 #ifdef MONSTER_HORDES
3904         if (randint1(5000) <= dun_level)
3905         {
3906                 if (alloc_horde(y, x))
3907                 {
3908 #ifdef JP
3909                         if (cheat_hear) msg_format("¥â¥ó¥¹¥¿¡¼¤ÎÂç·²(%c)", summon_kin_type);
3910 #else
3911                         if (cheat_hear) msg_format("Monster horde (%c).", summon_kin_type);
3912 #endif
3913
3914                         return (TRUE);
3915                 }
3916         }
3917         else
3918         {
3919 #endif /* MONSTER_HORDES */
3920
3921                 /* Attempt to place the monster, allow groups */
3922                 if (place_monster(y, x, (mode | PM_ALLOW_GROUP))) return (TRUE);
3923
3924 #ifdef MONSTER_HORDES
3925         }
3926 #endif /* MONSTER_HORDES */
3927
3928         /* Nope */
3929         return (FALSE);
3930 }
3931
3932
3933
3934
3935 /*
3936  * Hack -- help decide if a monster race is "okay" to summon
3937  */
3938 static bool summon_specific_okay(int r_idx)
3939 {
3940         monster_race *r_ptr = &r_info[r_idx];
3941
3942         /* Hack - Only summon dungeon monsters */
3943         if (!mon_hook_dungeon(r_idx)) return (FALSE);
3944
3945         /* Hack -- identify the summoning monster */
3946         if (summon_specific_who > 0)
3947         {
3948                 monster_type *m_ptr = &m_list[summon_specific_who];
3949
3950                 /* Do not summon enemies */
3951
3952                 /* Friendly vs. opposite aligned normal or pet */
3953                 if (monster_has_hostile_align(m_ptr, 0, 0, r_ptr)) return FALSE;
3954         }
3955         /* Use the player's alignment */
3956         else if (summon_specific_who < 0)
3957         {
3958                 /* Do not summon enemies of the pets */
3959                 if (monster_has_hostile_align(NULL, 10, -10, r_ptr))
3960                 {
3961                         if (!one_in_(ABS(p_ptr->align) / 2 + 1)) return FALSE;
3962                 }
3963         }
3964
3965         /* Hack -- no specific type specified */
3966         if (!summon_specific_type) return (TRUE);
3967
3968         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))) return FALSE;
3969
3970         if ((summon_specific_who < 0) &&
3971             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
3972             monster_has_hostile_align(NULL, 10, -10, r_ptr))
3973                 return FALSE;
3974
3975         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[dungeon_type].flags1 & DF1_CHAMELEON)) return TRUE;
3976
3977         return (summon_specific_aux(r_idx));
3978 }
3979
3980
3981 /*
3982  * Place a monster (of the specified "type") near the given
3983  * location.  Return TRUE if a monster was actually summoned.
3984  *
3985  * We will attempt to place the monster up to 10 times before giving up.
3986  *
3987  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
3988  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
3989  * Note: None of the other summon codes will ever summon Unique's.
3990  *
3991  * This function has been changed.  We now take the "monster level"
3992  * of the summoning monster as a parameter, and use that, along with
3993  * the current dungeon level, to help determine the level of the
3994  * desired monster.  Note that this is an upper bound, and also
3995  * tends to "prefer" monsters of that level.  Currently, we use
3996  * the average of the dungeon and monster levels, and then add
3997  * five to allow slight increases in monster power.
3998  *
3999  * Note that we use the new "monster allocation table" creation code
4000  * to restrict the "get_mon_num()" function to the set of "legal"
4001  * monsters, making this function much faster and more reliable.
4002  *
4003  * Note that this function may not succeed, though this is very rare.
4004  */
4005 bool summon_specific(int who, int y1, int x1, int lev, int type, u32b mode)
4006 {
4007         int x, y, r_idx;
4008
4009         if (p_ptr->inside_arena) return (FALSE);
4010
4011         if (!mon_scatter(0, &y, &x, y1, x1, 2)) return FALSE;
4012
4013         /* Save the summoner */
4014         summon_specific_who = who;
4015
4016         /* Save the "summon" type */
4017         summon_specific_type = type;
4018
4019         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
4020
4021         /* Prepare allocation table */
4022         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
4023
4024         /* Pick a monster, using the level calculation */
4025         r_idx = get_mon_num((dun_level + lev) / 2 + 5);
4026
4027         /* Handle failure */
4028         if (!r_idx)
4029         {
4030                 summon_specific_type = 0;
4031                 return (FALSE);
4032         }
4033
4034         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
4035
4036         /* Attempt to place the monster (awake, allow groups) */
4037         if (!place_monster_aux(who, y, x, r_idx, mode))
4038         {
4039                 summon_specific_type = 0;
4040                 return (FALSE);
4041         }
4042
4043         summon_specific_type = 0;
4044         /* Success */
4045         return (TRUE);
4046 }
4047
4048 /* A "dangerous" function, creates a pet of the specified type */
4049 bool summon_named_creature (int who, int oy, int ox, int r_idx, u32b mode)
4050 {
4051         int x, y;
4052
4053         /* Paranoia */
4054         /* if (!r_idx) return; */
4055
4056         /* Prevent illegal monsters */
4057         if (r_idx >= max_r_idx) return FALSE;
4058
4059         if (p_ptr->inside_arena) return FALSE;
4060
4061         if (!mon_scatter(r_idx, &y, &x, oy, ox, 2)) return FALSE;
4062
4063         /* Place it (allow groups) */
4064         return place_monster_aux(who, y, x, r_idx, (mode | PM_NO_KAGE));
4065 }
4066
4067
4068 /*
4069  * Let the given monster attempt to reproduce.
4070  *
4071  * Note that "reproduction" REQUIRES empty space.
4072  */
4073 bool multiply_monster(int m_idx, bool clone, u32b mode)
4074 {
4075         monster_type    *m_ptr = &m_list[m_idx];
4076
4077         int y, x;
4078
4079         if (!mon_scatter(m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
4080                 return FALSE;
4081
4082         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
4083
4084         /* Create a new monster (awake, no groups) */
4085         if (!place_monster_aux(m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
4086                 return FALSE;
4087
4088         /* Hack -- Transfer "clone" flag */
4089         if (clone || (m_ptr->smart & SM_CLONED))
4090         {
4091                 m_list[hack_m_idx_ii].smart |= SM_CLONED;
4092                 m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
4093         }
4094
4095         return TRUE;
4096 }
4097
4098
4099
4100
4101
4102 /*
4103  * Dump a message describing a monster's reaction to damage
4104  *
4105  * Technically should attempt to treat "Beholder"'s as jelly's
4106  */
4107 void message_pain(int m_idx, int dam)
4108 {
4109         long oldhp, newhp, tmp;
4110         int percentage;
4111
4112         monster_type *m_ptr = &m_list[m_idx];
4113         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4114
4115         char m_name[80];
4116
4117
4118         /* Get the monster name */
4119         monster_desc(m_name, m_ptr, 0);
4120
4121         /* Notice non-damage */
4122         if (dam == 0)
4123         {
4124 #ifdef JP
4125                 msg_format("%^s¤Ï¥À¥á¡¼¥¸¤ò¼õ¤±¤Æ¤¤¤Ê¤¤¡£", m_name);
4126 #else
4127                 msg_format("%^s is unharmed.", m_name);
4128 #endif
4129
4130                 return;
4131         }
4132
4133         /* Note -- subtle fix -CFT */
4134         newhp = (long)(m_ptr->hp);
4135         oldhp = newhp + (long)(dam);
4136         tmp = (newhp * 100L) / oldhp;
4137         percentage = (int)(tmp);
4138
4139
4140         /* Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs */
4141         if (my_strchr(",ejmvwQ", r_ptr->d_char))
4142         {
4143 #ifdef JP
4144                 if (percentage > 95)
4145                         msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4146                 else if (percentage > 75)
4147                         msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4148                 else if (percentage > 50)
4149                         msg_format("%^s¤Ï½Ì¤³¤Þ¤Ã¤¿¡£", m_name);
4150                 else if (percentage > 35)
4151                         msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4152                 else if (percentage > 20)
4153                         msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4154                 else if (percentage > 10)
4155                         msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4156                 else
4157                         msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4158 #else
4159                 if (percentage > 95)
4160                         msg_format("%^s barely notices.", m_name);
4161                 else if (percentage > 75)
4162                         msg_format("%^s flinches.", m_name);
4163                 else if (percentage > 50)
4164                         msg_format("%^s squelches.", m_name);
4165                 else if (percentage > 35)
4166                         msg_format("%^s quivers in pain.", m_name);
4167                 else if (percentage > 20)
4168                         msg_format("%^s writhes about.", m_name);
4169                 else if (percentage > 10)
4170                         msg_format("%^s writhes in agony.", m_name);
4171                 else
4172                         msg_format("%^s jerks limply.", m_name);
4173 #endif
4174
4175         }
4176
4177
4178         /* Fish */
4179         else if (my_strchr("l", r_ptr->d_char))
4180         {
4181                 if (percentage > 95)
4182 #ifdef JP
4183 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4184 #else
4185                         msg_format("%^s barely notices.", m_name);
4186 #endif
4187                 else if (percentage > 75)
4188 #ifdef JP
4189 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4190 #else
4191                         msg_format("%^s flinches.", m_name);
4192 #endif
4193                 else if (percentage > 50)
4194 #ifdef JP
4195 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4196 #else
4197                         msg_format("%^s hesitates.", m_name);
4198 #endif
4199                 else if (percentage > 35)
4200 #ifdef JP
4201 msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4202 #else
4203                         msg_format("%^s quivers in pain.", m_name);
4204 #endif
4205                 else if (percentage > 20)
4206 #ifdef JP
4207 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4208 #else
4209                         msg_format("%^s writhes about.", m_name);
4210 #endif
4211                 else if (percentage > 10)
4212 #ifdef JP
4213 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4214 #else
4215                         msg_format("%^s writhes in agony.", m_name);
4216 #endif
4217                 else
4218 #ifdef JP
4219 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4220 #else
4221                         msg_format("%^s jerks limply.", m_name);
4222 #endif
4223         }
4224
4225
4226         /* Golems, Walls, Doors, Stairs */
4227         else if (my_strchr("g#+<>", r_ptr->d_char))
4228         {
4229                 if (percentage > 95)
4230 #ifdef JP
4231 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4232 #else
4233                         msg_format("%^s ignores the attack.", m_name);
4234 #endif
4235                 else if (percentage > 75)
4236 #ifdef JP
4237 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4238 #else
4239                         msg_format("%^s shrugs off the attack.", m_name);
4240 #endif
4241                 else if (percentage > 50)
4242 #ifdef JP
4243 msg_format("%^s¤ÏÍëÌĤΤ褦¤ËËʤ¨¤¿¡£", m_name);
4244 #else
4245                         msg_format("%^s roars thunderously.", m_name);
4246 #endif
4247                 else if (percentage > 35)
4248 #ifdef JP
4249 msg_format("%^s¤Ï¶ì¤·¤²¤ËËʤ¨¤¿¡£", m_name);
4250 #else
4251                         msg_format("%^s rumbles.", m_name);
4252 #endif
4253                 else if (percentage > 20)
4254 #ifdef JP
4255 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4256 #else
4257                         msg_format("%^s grunts.", m_name);
4258 #endif
4259                 else if (percentage > 10)
4260 #ifdef JP
4261 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4262 #else
4263                         msg_format("%^s hesitates.", m_name);
4264 #endif
4265                 else
4266 #ifdef JP
4267 msg_format("%^s¤Ï¤¯¤·¤ã¤¯¤·¤ã¤Ë¤Ê¤Ã¤¿¡£", m_name);
4268 #else
4269                         msg_format("%^s crumples.", m_name);
4270 #endif
4271         }
4272
4273
4274         /* Snakes, Hydrae, Reptiles, Mimics */
4275         else if (my_strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char))
4276         {
4277                 if (percentage > 95)
4278 #ifdef JP
4279 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4280 #else
4281                         msg_format("%^s barely notices.", m_name);
4282 #endif
4283                 else if (percentage > 75)
4284 #ifdef JP
4285 msg_format("%^s¤Ï¥·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4286 #else
4287                         msg_format("%^s hisses.", m_name);
4288 #endif
4289                 else if (percentage > 50)
4290 #ifdef JP
4291 msg_format("%^s¤ÏÅܤäÆƬ¤ò¾å¤²¤¿¡£", m_name);
4292 #else
4293                         msg_format("%^s rears up in anger.", m_name);
4294 #endif
4295                 else if (percentage > 35)
4296 #ifdef JP
4297 msg_format("%^s¤ÏÌÔÁ³¤È°Ò³Å¤·¤¿¡£", m_name);
4298 #else
4299                         msg_format("%^s hisses furiously.", m_name);
4300 #endif
4301                 else if (percentage > 20)
4302 #ifdef JP
4303 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4304 #else
4305                         msg_format("%^s writhes about.", m_name);
4306 #endif
4307                 else if (percentage > 10)
4308 #ifdef JP
4309 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4310 #else
4311                         msg_format("%^s writhes in agony.", m_name);
4312 #endif
4313                 else
4314 #ifdef JP
4315 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4316 #else
4317                         msg_format("%^s jerks limply.", m_name);
4318 #endif
4319         }
4320
4321
4322         /* Felines */
4323         else if (my_strchr("f", r_ptr->d_char))
4324         {
4325                 if (percentage > 95)
4326 #ifdef JP
4327 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4328 #else
4329                         msg_format("%^s shrugs off the attack.", m_name);
4330 #endif
4331                 else if (percentage > 75)
4332 #ifdef JP
4333 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4334 #else
4335                         msg_format("%^s roars.", m_name);
4336 #endif
4337                 else if (percentage > 50)
4338 #ifdef JP
4339 msg_format("%^s¤ÏÅܤäÆËʤ¨¤¿¡£", m_name);
4340 #else
4341                         msg_format("%^s growls angrily.", m_name);
4342 #endif
4343                 else if (percentage > 35)
4344 #ifdef JP
4345 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4346 #else
4347                         msg_format("%^s hisses with pain.", m_name);
4348 #endif
4349                 else if (percentage > 20)
4350 #ifdef JP
4351 msg_format("%^s¤ÏÄˤߤǼ塹¤·¤¯ÌĤ¤¤¿¡£", m_name);
4352 #else
4353                         msg_format("%^s mewls in pain.", m_name);
4354 #endif
4355                 else if (percentage > 10)
4356 #ifdef JP
4357 msg_format("%^s¤Ï¶ìÄˤˤ¦¤á¤¤¤¿¡£", m_name);
4358 #else
4359                         msg_format("%^s hisses in agony.", m_name);
4360 #endif
4361                 else
4362 #ifdef JP
4363 msg_format("%s¤Ï°¥¤ì¤ÊÌĤ­À¼¤ò½Ð¤·¤¿¡£", m_name);
4364 #else
4365                         msg_format("%^s mewls pitifully.", m_name);
4366 #endif
4367         }
4368
4369
4370         /* Ants, Centipedes, Flies, Insects, Beetles, Spiders */
4371         else if (my_strchr("acFIKS", r_ptr->d_char))
4372         {
4373                 if (percentage > 95)
4374 #ifdef JP
4375 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4376 #else
4377                         msg_format("%^s ignores the attack.", m_name);
4378 #endif
4379                 else if (percentage > 75)
4380 #ifdef JP
4381 msg_format("%^s¤Ï¥­¡¼¥­¡¼ÌĤ¤¤¿¡£", m_name);
4382 #else
4383                         msg_format("%^s chitters.", m_name);
4384 #endif
4385
4386                 else if (percentage > 50)
4387 #ifdef JP
4388 msg_format("%^s¤Ï¥è¥í¥è¥íƨ¤²²ó¤Ã¤¿¡£", m_name);
4389 #else
4390                         msg_format("%^s scuttles about.", m_name);
4391 #endif
4392
4393                 else if (percentage > 35)
4394 #ifdef JP
4395 msg_format("%^s¤Ï¤¦¤ë¤µ¤¯ÌĤ¤¤¿¡£", m_name);
4396 #else
4397                         msg_format("%^s twitters.", m_name);
4398 #endif
4399
4400                 else if (percentage > 20)
4401 #ifdef JP
4402 msg_format("%^s¤ÏÄˤߤËáÛÚ»¤·¤¿¡£", m_name);
4403 #else
4404                         msg_format("%^s jerks in pain.", m_name);
4405 #endif
4406
4407                 else if (percentage > 10)
4408 #ifdef JP
4409 msg_format("%^s¤Ï¶ìÄˤÇáÛÚ»¤·¤¿¡£", m_name);
4410 #else
4411                         msg_format("%^s jerks in agony.", m_name);
4412 #endif
4413
4414                 else
4415 #ifdef JP
4416 msg_format("%^s¤Ï¥Ô¥¯¥Ô¥¯¤Ò¤­¤Ä¤Ã¤¿¡£", m_name);
4417 #else
4418                         msg_format("%^s twitches.", m_name);
4419 #endif
4420
4421         }
4422
4423
4424         /* Birds */
4425         else if (my_strchr("B", r_ptr->d_char))
4426         {
4427                 if (percentage > 95)
4428 #ifdef JP
4429 msg_format("%^s¤Ï¤µ¤¨¤º¤Ã¤¿¡£", m_name);
4430 #else
4431                         msg_format("%^s chirps.", m_name);
4432 #endif
4433
4434                 else if (percentage > 75)
4435 #ifdef JP
4436 msg_format("%^s¤Ï¥Ô¡¼¥Ô¡¼ÌĤ¤¤¿¡£", m_name);
4437 #else
4438                         msg_format("%^s twitters.", m_name);
4439 #endif
4440
4441                 else if (percentage > 50)
4442 #ifdef JP
4443 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ¤¤¿¡£", m_name);
4444 #else
4445                         msg_format("%^s squawks.", m_name);
4446 #endif
4447
4448                 else if (percentage > 35)
4449 #ifdef JP
4450 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4451 #else
4452                         msg_format("%^s chatters.", m_name);
4453 #endif
4454
4455                 else if (percentage > 20)
4456 #ifdef JP
4457 msg_format("%^s¤Ï¶ì¤·¤ó¤À¡£", m_name);
4458 #else
4459                         msg_format("%^s jeers.", m_name);
4460 #endif
4461
4462                 else if (percentage > 10)
4463 #ifdef JP
4464 msg_format("%^s¤Ï¤Î¤¿¤¦¤Á²ó¤Ã¤¿¡£", m_name);
4465 #else
4466                         msg_format("%^s flutters about.", m_name);
4467 #endif
4468
4469                 else
4470 #ifdef JP
4471 msg_format("%^s¤Ï¥­¡¼¥­¡¼¤ÈÌĤ­¶«¤ó¤À¡£", m_name);
4472 #else
4473                         msg_format("%^s squeaks.", m_name);
4474 #endif
4475
4476         }
4477
4478
4479         /* Dragons, Demons, High Undead */
4480         else if (my_strchr("duDLUW", r_ptr->d_char))
4481         {
4482                 if (percentage > 95)
4483 #ifdef JP
4484 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4485 #else
4486                         msg_format("%^s ignores the attack.", m_name);
4487 #endif
4488
4489                 else if (percentage > 75)
4490 #ifdef JP
4491 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4492 #else
4493                         msg_format("%^s flinches.", m_name);
4494 #endif
4495
4496                 else if (percentage > 50)
4497 #ifdef JP
4498 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4499 #else
4500                         msg_format("%^s hisses in pain.", m_name);
4501 #endif
4502
4503                 else if (percentage > 35)
4504 #ifdef JP
4505 msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4506 #else
4507                         msg_format("%^s snarls with pain.", m_name);
4508 #endif
4509
4510                 else if (percentage > 20)
4511 #ifdef JP
4512 msg_format("%^s¤ÏÄˤߤËËʤ¨¤¿¡£", m_name);
4513 #else
4514                         msg_format("%^s roars with pain.", m_name);
4515 #endif
4516
4517                 else if (percentage > 10)
4518 #ifdef JP
4519 msg_format("%^s¤Ï¶ì¤·¤²¤Ë¶«¤ó¤À¡£", m_name);
4520 #else
4521                         msg_format("%^s gasps.", m_name);
4522 #endif
4523
4524                 else
4525 #ifdef JP
4526 msg_format("%^s¤Ï¼å¡¹¤·¤¯¤¦¤Ê¤Ã¤¿¡£", m_name);
4527 #else
4528                         msg_format("%^s snarls feebly.", m_name);
4529 #endif
4530
4531         }
4532
4533
4534         /* Skeletons */
4535         else if (my_strchr("s", r_ptr->d_char))
4536         {
4537                 if (percentage > 95)
4538 #ifdef JP
4539 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4540 #else
4541                         msg_format("%^s ignores the attack.", m_name);
4542 #endif
4543
4544                 else if (percentage > 75)
4545 #ifdef JP
4546 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4547 #else
4548                         msg_format("%^s shrugs off the attack.", m_name);
4549 #endif
4550
4551                 else if (percentage > 50)
4552 #ifdef JP
4553 msg_format("%^s¤Ï¥«¥¿¥«¥¿¤È¾Ð¤Ã¤¿¡£", m_name);
4554 #else
4555                         msg_format("%^s rattles.", m_name);
4556 #endif
4557
4558                 else if (percentage > 35)
4559 #ifdef JP
4560 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4561 #else
4562                         msg_format("%^s stumbles.", m_name);
4563 #endif
4564
4565                 else if (percentage > 20)
4566 #ifdef JP
4567 msg_format("%^s¤Ï¥«¥¿¥«¥¿¸À¤Ã¤¿¡£", m_name);
4568 #else
4569                         msg_format("%^s rattles.", m_name);
4570 #endif
4571
4572                 else if (percentage > 10)
4573 #ifdef JP
4574 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4575 #else
4576                         msg_format("%^s staggers.", m_name);
4577 #endif
4578
4579                 else
4580 #ifdef JP
4581 msg_format("%^s¤Ï¥¬¥¿¥¬¥¿¸À¤Ã¤¿¡£", m_name);
4582 #else
4583                         msg_format("%^s clatters.", m_name);
4584 #endif
4585
4586         }
4587
4588
4589         /* Zombies */
4590         else if (my_strchr("z", r_ptr->d_char))
4591         {
4592                 if (percentage > 95)
4593 #ifdef JP
4594 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4595 #else
4596                         msg_format("%^s ignores the attack.", m_name);
4597 #endif
4598
4599                 else if (percentage > 75)
4600 #ifdef JP
4601 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4602 #else
4603                         msg_format("%^s shrugs off the attack.", m_name);
4604 #endif
4605
4606                 else if (percentage > 50)
4607 #ifdef JP
4608 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4609 #else
4610                         msg_format("%^s groans.", m_name);
4611 #endif
4612
4613                 else if (percentage > 35)
4614 #ifdef JP
4615 msg_format("%s¤Ï¶ì¤·¤²¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4616 #else
4617                         msg_format("%^s moans.", m_name);
4618 #endif
4619
4620                 else if (percentage > 20)
4621 #ifdef JP
4622 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4623 #else
4624                         msg_format("%^s hesitates.", m_name);
4625 #endif
4626
4627                 else if (percentage > 10)
4628 #ifdef JP
4629 msg_format("%^s¤Ï¤¦¤Ê¤Ã¤¿¡£", m_name);
4630 #else
4631                         msg_format("%^s grunts.", m_name);
4632 #endif
4633
4634                 else
4635 #ifdef JP
4636 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4637 #else
4638                         msg_format("%^s staggers.", m_name);
4639 #endif
4640
4641         }
4642
4643
4644         /* Ghosts */
4645         else if (my_strchr("G", r_ptr->d_char))
4646
4647         {
4648                 if (percentage > 95)
4649 #ifdef JP
4650 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4651 #else
4652                         msg_format("%^s ignores the attack.", m_name);
4653 #endif
4654
4655                 else if (percentage > 75)
4656 #ifdef JP
4657 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4658 #else
4659                         msg_format("%^s shrugs off the attack.", m_name);
4660 #endif
4661
4662                 else if (percentage > 50)
4663 #ifdef JP
4664 msg_format("%s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4665 #else
4666                         msg_format("%^s moans.", m_name);
4667 #endif
4668
4669                 else if (percentage > 35)
4670 #ifdef JP
4671 msg_format("%^s¤Ïµã¤­¤ï¤á¤¤¤¿¡£", m_name);
4672 #else
4673                         msg_format("%^s wails.", m_name);
4674 #endif
4675
4676                 else if (percentage > 20)
4677 #ifdef JP
4678 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4679 #else
4680                         msg_format("%^s howls.", m_name);
4681 #endif
4682
4683                 else if (percentage > 10)
4684 #ifdef JP
4685 msg_format("%s¤Ï¼å¡¹¤·¤¯¤¦¤á¤¤¤¿¡£", m_name);
4686 #else
4687                         msg_format("%^s moans softly.", m_name);
4688 #endif
4689
4690                 else
4691 #ifdef JP
4692 msg_format("%^s¤Ï¤«¤¹¤«¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4693 #else
4694                         msg_format("%^s sighs.", m_name);
4695 #endif
4696
4697         }
4698
4699
4700         /* Dogs and Hounds */
4701         else if (my_strchr("CZ", r_ptr->d_char))
4702         {
4703 #ifdef JP
4704                 if (percentage > 95)
4705                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4706                 else if (percentage > 75)
4707                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4708                 else if (percentage > 50)
4709                         msg_format("%^s¤ÏÄˤߤǥ­¥ã¥ó¥­¥ã¥óËʤ¨¤¿¡£", m_name);
4710                 else if (percentage > 35)
4711                         msg_format("%^s¤ÏÄˤߤÇÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4712                 else if (percentage > 20)
4713                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4714                 else if (percentage > 10)
4715                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4716                 else
4717                         msg_format("%^s¤Ï¼å¡¹¤·¤¯Ëʤ¨¤¿¡£", m_name);
4718 #else
4719                 if (percentage > 95)
4720                         msg_format("%^s shrugs off the attack.", m_name);
4721                 else if (percentage > 75)
4722                         msg_format("%^s snarls with pain.", m_name);
4723                 else if (percentage > 50)
4724                         msg_format("%^s yelps in pain.", m_name);
4725                 else if (percentage > 35)
4726                         msg_format("%^s howls in pain.", m_name);
4727                 else if (percentage > 20)
4728                         msg_format("%^s howls in agony.", m_name);
4729                 else if (percentage > 10)
4730                         msg_format("%^s writhes in agony.", m_name);
4731                 else
4732                         msg_format("%^s yelps feebly.", m_name);
4733 #endif
4734
4735         }
4736
4737         /* One type of monsters (ignore,squeal,shriek) */
4738         else if (my_strchr("Xbilqrt", r_ptr->d_char))
4739         {
4740 #ifdef JP
4741                 if (percentage > 95)
4742                         msg_format("%^s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4743                 else if (percentage > 75)
4744                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4745                 else if (percentage > 50)
4746                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4747                 else if (percentage > 35)
4748                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4749                 else if (percentage > 20)
4750                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4751                 else if (percentage > 10)
4752                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4753                 else
4754                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4755 #else
4756                 if (percentage > 95)
4757                         msg_format("%^s ignores the attack.", m_name);
4758                 else if (percentage > 75)
4759                         msg_format("%^s grunts with pain.", m_name);
4760                 else if (percentage > 50)
4761                         msg_format("%^s squeals in pain.", m_name);
4762                 else if (percentage > 35)
4763                         msg_format("%^s shrieks in pain.", m_name);
4764                 else if (percentage > 20)
4765                         msg_format("%^s shrieks in agony.", m_name);
4766                 else if (percentage > 10)
4767                         msg_format("%^s writhes in agony.", m_name);
4768                 else
4769                         msg_format("%^s cries out feebly.", m_name);
4770 #endif
4771
4772         }
4773
4774         /* Another type of monsters (shrug,cry,scream) */
4775         else
4776         {
4777 #ifdef JP
4778                 if (percentage > 95)
4779                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4780                 else if (percentage > 75)
4781                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4782                 else if (percentage > 50)
4783                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4784                 else if (percentage > 35)
4785                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4786                 else if (percentage > 20)
4787                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4788                 else if (percentage > 10)
4789                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4790                 else
4791                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4792 #else
4793                 if (percentage > 95)
4794                         msg_format("%^s shrugs off the attack.", m_name);
4795                 else if (percentage > 75)
4796                         msg_format("%^s grunts with pain.", m_name);
4797                 else if (percentage > 50)
4798                         msg_format("%^s cries out in pain.", m_name);
4799                 else if (percentage > 35)
4800                         msg_format("%^s screams in pain.", m_name);
4801                 else if (percentage > 20)
4802                         msg_format("%^s screams in agony.", m_name);
4803                 else if (percentage > 10)
4804                         msg_format("%^s writhes in agony.", m_name);
4805                 else
4806                         msg_format("%^s cries out feebly.", m_name);
4807 #endif
4808
4809         }
4810 }
4811
4812
4813 /*
4814  * Learn about an "observed" resistance.
4815  */
4816 void update_smart_learn(int m_idx, int what)
4817 {
4818         monster_type *m_ptr = &m_list[m_idx];
4819
4820         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4821
4822
4823         /* Not allowed to learn */
4824         if (!smart_learn) return;
4825
4826         /* Too stupid to learn anything */
4827         if (r_ptr->flags2 & (RF2_STUPID)) return;
4828
4829         /* Not intelligent, only learn sometimes */
4830         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
4831
4832
4833         /* XXX XXX XXX */
4834
4835         /* Analyze the knowledge */
4836         switch (what)
4837         {
4838         case DRS_ACID:
4839                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
4840                 if (IS_OPPOSE_ACID()) m_ptr->smart |= (SM_OPP_ACID);
4841                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
4842                 break;
4843
4844         case DRS_ELEC:
4845                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
4846                 if (IS_OPPOSE_ELEC()) m_ptr->smart |= (SM_OPP_ELEC);
4847                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
4848                 break;
4849
4850         case DRS_FIRE:
4851                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
4852                 if (IS_OPPOSE_FIRE()) m_ptr->smart |= (SM_OPP_FIRE);
4853                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
4854                 break;
4855
4856         case DRS_COLD:
4857                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
4858                 if (IS_OPPOSE_COLD()) m_ptr->smart |= (SM_OPP_COLD);
4859                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
4860                 break;
4861
4862         case DRS_POIS:
4863                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
4864                 if (IS_OPPOSE_POIS()) m_ptr->smart |= (SM_OPP_POIS);
4865                 break;
4866
4867
4868         case DRS_NETH:
4869                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
4870                 break;
4871
4872         case DRS_LITE:
4873                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
4874                 break;
4875
4876         case DRS_DARK:
4877                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
4878                 break;
4879
4880         case DRS_FEAR:
4881                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
4882                 break;
4883
4884         case DRS_CONF:
4885                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
4886                 break;
4887
4888         case DRS_CHAOS:
4889                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4890                 break;
4891
4892         case DRS_DISEN:
4893                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4894                 break;
4895
4896         case DRS_BLIND:
4897                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4898                 break;
4899
4900         case DRS_NEXUS:
4901                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4902                 break;
4903
4904         case DRS_SOUND:
4905                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4906                 break;
4907
4908         case DRS_SHARD:
4909                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4910                 break;
4911
4912         case DRS_FREE:
4913                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4914                 break;
4915
4916         case DRS_MANA:
4917                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4918                 break;
4919
4920         case DRS_REFLECT:
4921                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
4922                 break;
4923         }
4924 }
4925
4926
4927 /*
4928  * Place the player in the dungeon XXX XXX
4929  */
4930 bool player_place(int y, int x)
4931 {
4932         /* Paranoia XXX XXX */
4933         if (cave[y][x].m_idx != 0) return FALSE;
4934
4935         /* Save player location */
4936         py = y;
4937         px = x;
4938
4939         /* Success */
4940         return TRUE;
4941 }
4942
4943
4944 /*
4945  * Drop all items carried by a monster
4946  */
4947 void monster_drop_carried_objects(monster_type *m_ptr)
4948 {
4949         s16b this_o_idx, next_o_idx = 0;
4950         object_type forge;
4951         object_type *o_ptr;
4952         object_type *q_ptr;
4953
4954
4955         /* Drop objects being carried */
4956         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4957         {
4958                 /* Acquire object */
4959                 o_ptr = &o_list[this_o_idx];
4960
4961                 /* Acquire next object */
4962                 next_o_idx = o_ptr->next_o_idx;
4963
4964                 /* Get local object */
4965                 q_ptr = &forge;
4966
4967                 /* Copy the object */
4968                 object_copy(q_ptr, o_ptr);
4969
4970                 /* Forget monster */
4971                 q_ptr->held_m_idx = 0;
4972
4973                 /* Delete the object */
4974                 delete_object_idx(this_o_idx);
4975
4976                 /* Drop it */
4977                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
4978         }
4979
4980         /* Forget objects */
4981         m_ptr->hold_o_idx = 0;
4982 }