OSDN Git Service

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