OSDN Git Service

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