OSDN Git Service

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