OSDN Git Service

プレイヤーの位置を表すpy, pxとの比較をマクロplayer_bold()にまとめた.
[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)))
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         /* Do disturb? */
2371         if (disturb_high)
2372         {
2373                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2374
2375                 if (ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)
2376                         do_disturb = TRUE;
2377         }
2378
2379         /* Compute distance */
2380         if (full)
2381         {
2382                 /* Distance components */
2383                 int dy = (py > fy) ? (py - fy) : (fy - py);
2384                 int dx = (px > fx) ? (px - fx) : (fx - px);
2385
2386                 /* Approximate distance */
2387                 d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
2388
2389                 /* Restrict distance */
2390                 if (d > 255) d = 255;
2391
2392                 if (!d) d = 1;
2393
2394                 /* Save the distance */
2395                 m_ptr->cdis = d;
2396         }
2397
2398         /* Extract distance */
2399         else
2400         {
2401                 /* Extract the distance */
2402                 d = m_ptr->cdis;
2403         }
2404
2405
2406         /* Detected */
2407         if (m_ptr->mflag2 & (MFLAG2_MARK)) flag = TRUE;
2408
2409
2410         /* Nearby */
2411         if (d <= ((d_info[dungeon_type].flags1 & DF1_DARKNESS) ? MAX_SIGHT / 2 : MAX_SIGHT))
2412         {
2413                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS) || (d <= MAX_SIGHT / 4))
2414                 {
2415                         if (p_ptr->special_defense & KATA_MUSOU)
2416                         {
2417                                 /* Detectable */
2418                                 flag = TRUE;
2419
2420                                 /* Hack -- Memorize mental flags */
2421                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2422                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2423                         }
2424
2425                         /* Basic telepathy */
2426                         else if (p_ptr->telepathy)
2427                         {
2428                                 /* Empty mind, no telepathy */
2429                                 if (r_ptr->flags2 & (RF2_EMPTY_MIND))
2430                                 {
2431                                         /* Memorize flags */
2432                                         r_ptr->r_flags2 |= (RF2_EMPTY_MIND);
2433                                 }
2434
2435                                 /* Weird mind, occasional telepathy */
2436                                 else if (r_ptr->flags2 & (RF2_WEIRD_MIND))
2437                                 {
2438                                         /* One in ten individuals are detectable */
2439                                         if ((m_idx % 10) == 5)
2440                                         {
2441                                                 /* Detectable */
2442                                                 flag = TRUE;
2443
2444                                                 /* Memorize flags */
2445                                                 r_ptr->r_flags2 |= (RF2_WEIRD_MIND);
2446
2447                                                 /* Hack -- Memorize mental flags */
2448                                                 if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2449                                                 if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2450                                         }
2451                                 }
2452
2453                                 /* Normal mind, allow telepathy */
2454                                 else
2455                                 {
2456                                         /* Detectable */
2457                                         flag = TRUE;
2458
2459                                         /* Hack -- Memorize mental flags */
2460                                         if (r_ptr->flags2 & (RF2_SMART)) r_ptr->r_flags2 |= (RF2_SMART);
2461                                         if (r_ptr->flags2 & (RF2_STUPID)) r_ptr->r_flags2 |= (RF2_STUPID);
2462                                 }
2463                         }
2464
2465                         /* Magical sensing */
2466                         if ((p_ptr->esp_animal) && (r_ptr->flags3 & (RF3_ANIMAL)))
2467                         {
2468                                 flag = TRUE;
2469                                 r_ptr->r_flags3 |= (RF3_ANIMAL);
2470
2471                         }
2472
2473                         /* Magical sensing */
2474                         if ((p_ptr->esp_undead) && (r_ptr->flags3 & (RF3_UNDEAD)))
2475                         {
2476                                 flag = TRUE;
2477                                 r_ptr->r_flags3 |= (RF3_UNDEAD);
2478
2479                         }
2480
2481                         /* Magical sensing */
2482                         if ((p_ptr->esp_demon) && (r_ptr->flags3 & (RF3_DEMON)))
2483                         {
2484                                 flag = TRUE;
2485                                 r_ptr->r_flags3 |= (RF3_DEMON);
2486
2487                         }
2488
2489                         /* Magical sensing */
2490                         if ((p_ptr->esp_orc) && (r_ptr->flags3 & (RF3_ORC)))
2491                         {
2492                                 flag = TRUE;
2493                                 r_ptr->r_flags3 |= (RF3_ORC);
2494
2495                         }
2496
2497                         /* Magical sensing */
2498                         if ((p_ptr->esp_troll) && (r_ptr->flags3 & (RF3_TROLL)))
2499                         {
2500                                 flag = TRUE;
2501                                 r_ptr->r_flags3 |= (RF3_TROLL);
2502
2503                         }
2504
2505                         /* Magical sensing */
2506                         if ((p_ptr->esp_giant) && (r_ptr->flags3 & (RF3_GIANT)))
2507                         {
2508                                 flag = TRUE;
2509                                 r_ptr->r_flags3 |= (RF3_GIANT);
2510
2511                         }
2512
2513                         /* Magical sensing */
2514                         if ((p_ptr->esp_dragon) && (r_ptr->flags3 & (RF3_DRAGON)))
2515                         {
2516                                 flag = TRUE;
2517                                 r_ptr->r_flags3 |= (RF3_DRAGON);
2518
2519                         }
2520
2521                         /* Magical sensing */
2522                         if ((p_ptr->esp_human) && (r_ptr->flags2 & (RF2_HUMAN)))
2523                         {
2524                                 flag = TRUE;
2525                                 r_ptr->r_flags2 |= (RF2_HUMAN);
2526
2527                         }
2528
2529                         /* Magical sensing */
2530                         if ((p_ptr->esp_evil) && (r_ptr->flags3 & (RF3_EVIL)))
2531                         {
2532                                 flag = TRUE;
2533                                 r_ptr->r_flags3 |= (RF3_EVIL);
2534
2535                         }
2536
2537                         /* Magical sensing */
2538                         if ((p_ptr->esp_good) && (r_ptr->flags3 & (RF3_GOOD)))
2539                         {
2540                                 flag = TRUE;
2541                                 r_ptr->r_flags3 |= (RF3_GOOD);
2542
2543                         }
2544
2545                         /* Magical sensing */
2546                         if ((p_ptr->esp_nonliving) && 
2547                             (r_ptr->flags3 & (RF3_NONLIVING)) &&
2548                             !(r_ptr->flags3 & (RF3_DEMON)) &&
2549                             !(r_ptr->flags3 & (RF3_UNDEAD)))
2550                         {
2551                                 flag = TRUE;
2552                                 r_ptr->r_flags3 |= (RF3_NONLIVING);
2553
2554                         }
2555
2556                         /* Magical sensing */
2557                         if ((p_ptr->esp_unique) && (r_ptr->flags1 & (RF1_UNIQUE)))
2558                         {
2559                                 flag = TRUE;
2560                                 r_ptr->r_flags1 |= (RF1_UNIQUE);
2561
2562                         }
2563                 }
2564
2565                 /* Normal line of sight, and not blind */
2566                 if (player_has_los_bold(fy, fx) && !p_ptr->blind)
2567                 {
2568                         bool do_invisible = FALSE;
2569                         bool do_cold_blood = FALSE;
2570
2571                         /* Use "infravision" */
2572                         if (d <= p_ptr->see_infra)
2573                         {
2574                                 /* Handle "cold blooded" monsters */
2575                                 if (r_ptr->flags2 & (RF2_COLD_BLOOD))
2576                                 {
2577                                         /* Take note */
2578                                         do_cold_blood = TRUE;
2579                                 }
2580
2581                                 /* Handle "warm blooded" monsters */
2582                                 else
2583                                 {
2584                                         /* Easy to see */
2585                                         easy = flag = TRUE;
2586                                 }
2587                         }
2588
2589                         /* Use "illumination" */
2590                         if (player_can_see_bold(fy, fx))
2591                         {
2592                                 /* Handle "invisible" monsters */
2593                                 if (r_ptr->flags2 & (RF2_INVISIBLE))
2594                                 {
2595                                         /* Take note */
2596                                         do_invisible = TRUE;
2597
2598                                         /* See invisible */
2599                                         if (p_ptr->see_inv)
2600                                         {
2601                                                 /* Easy to see */
2602                                                 easy = flag = TRUE;
2603                                         }
2604                                 }
2605
2606                                 /* Handle "normal" monsters */
2607                                 else
2608                                 {
2609                                         /* Easy to see */
2610                                         easy = flag = TRUE;
2611                                 }
2612                         }
2613
2614                         /* Visible */
2615                         if (flag)
2616                         {
2617                                 /* Memorize flags */
2618                                 if (do_invisible) r_ptr->r_flags2 |= (RF2_INVISIBLE);
2619                                 if (do_cold_blood) r_ptr->r_flags2 |= (RF2_COLD_BLOOD);
2620                         }
2621                 }
2622         }
2623
2624
2625         /* The monster is now visible */
2626         if (flag)
2627         {
2628                 /* It was previously unseen */
2629                 if (!m_ptr->ml)
2630                 {
2631                         /* Mark as visible */
2632                         m_ptr->ml = TRUE;
2633
2634                         /* Draw the monster */
2635                         lite_spot(fy, fx);
2636
2637                         /* Update health bar as needed */
2638                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2639                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2640
2641                         /* Hack -- Count "fresh" sightings */
2642                         if ((m_ptr->ap_r_idx == MON_KAGE) && (r_info[MON_KAGE].r_sights < MAX_SHORT))
2643                                 r_info[MON_KAGE].r_sights++;
2644                         else if (m_ptr->ap_r_idx == m_ptr->r_idx && 
2645                                  r_ptr->r_sights < MAX_SHORT) r_ptr->r_sights++;
2646
2647                         /* Eldritch Horror */
2648                         if (r_ptr->flags2 & RF2_ELDRITCH_HORROR)
2649                         {
2650                                 sanity_blast(m_ptr, FALSE);
2651                         }
2652
2653                         /* Disturb on appearance */
2654                         if (disturb_near && (projectable(m_ptr->fy, m_ptr->fx, py, px) && projectable(py, px, m_ptr->fy, m_ptr->fx)))
2655                         {
2656                                 if (disturb_pets || is_hostile(m_ptr))
2657                                         disturb(1, 0);
2658                         }
2659                 }
2660         }
2661
2662         /* The monster is not visible */
2663         else
2664         {
2665                 /* It was previously seen */
2666                 if (m_ptr->ml)
2667                 {
2668                         /* Mark as not visible */
2669                         m_ptr->ml = FALSE;
2670
2671                         /* Erase the monster */
2672                         lite_spot(fy, fx);
2673
2674                         /* Update health bar as needed */
2675                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2676                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2677
2678                         /* Disturb on disappearance */
2679                         if (do_disturb)
2680                         {
2681                                 if (disturb_pets || is_hostile(m_ptr))
2682                                         disturb(1, 0);
2683                         }
2684                 }
2685         }
2686
2687
2688         /* The monster is now easily visible */
2689         if (easy)
2690         {
2691                 /* Change */
2692                 if (!(m_ptr->mflag & (MFLAG_VIEW)))
2693                 {
2694                         /* Mark as easily visible */
2695                         m_ptr->mflag |= (MFLAG_VIEW);
2696
2697                         /* Disturb on appearance */
2698                         if (do_disturb)
2699                         {
2700                                 if (disturb_pets || is_hostile(m_ptr))
2701                                         disturb(1, 0);
2702                         }
2703                 }
2704         }
2705
2706         /* The monster is not easily visible */
2707         else
2708         {
2709                 /* Change */
2710                 if (m_ptr->mflag & (MFLAG_VIEW))
2711                 {
2712                         /* Mark as not easily visible */
2713                         m_ptr->mflag &= ~(MFLAG_VIEW);
2714
2715                         /* Disturb on disappearance */
2716                         if (do_disturb)
2717                         {
2718                                 if (disturb_pets || is_hostile(m_ptr))
2719                                         disturb(1, 0);
2720                         }
2721                 }
2722         }
2723 }
2724
2725
2726 /*
2727  * This function simply updates all the (non-dead) monsters (see above).
2728  */
2729 void update_monsters(bool full)
2730 {
2731         int i;
2732
2733         /* Update each (live) monster */
2734         for (i = 1; i < m_max; i++)
2735         {
2736                 monster_type *m_ptr = &m_list[i];
2737
2738                 /* Skip dead monsters */
2739                 if (!m_ptr->r_idx) continue;
2740
2741                 /* Update the monster */
2742                 update_mon(i, full);
2743         }
2744 }
2745
2746
2747 /*
2748  * Hack -- the index of the summoning monster
2749  */
2750 static bool monster_hook_chameleon_lord(int r_idx)
2751 {
2752         monster_race *r_ptr = &r_info[r_idx];
2753         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2754         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2755
2756         if (!(r_ptr->flags1 & (RF1_UNIQUE))) return FALSE;
2757         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2758
2759         if (ABS(r_ptr->level - r_info[MON_CHAMELEON_K].level) > 5) return FALSE;
2760
2761         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))
2762                 return FALSE;
2763
2764         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr)) return FALSE;
2765
2766         /* Not born */
2767         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2768         {
2769                 if ((m_ptr->sub_align & SUB_ALIGN_EVIL) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
2770                 if ((m_ptr->sub_align & SUB_ALIGN_GOOD) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
2771         }
2772
2773         /* Born now */
2774         else if (summon_specific_who > 0)
2775         {
2776                 monster_type *sm_ptr = &m_list[summon_specific_who];
2777
2778                 if ((sm_ptr->sub_align & SUB_ALIGN_EVIL) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
2779                 if ((sm_ptr->sub_align & SUB_ALIGN_GOOD) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
2780         }
2781
2782         return TRUE;
2783 }
2784
2785 static bool monster_hook_chameleon(int r_idx)
2786 {
2787         monster_race *r_ptr = &r_info[r_idx];
2788         monster_type *m_ptr = &m_list[chameleon_change_m_idx];
2789         monster_race *old_r_ptr = &r_info[m_ptr->r_idx];
2790
2791         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2792         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2793         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2794         
2795         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))
2796                 return FALSE;
2797
2798         if (!monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, r_ptr)) return FALSE;
2799
2800         /* Not born */
2801         if (!(old_r_ptr->flags7 & RF7_CHAMELEON))
2802         {
2803                 if ((old_r_ptr->flags3 & RF3_GOOD) && !(r_ptr->flags3 & RF3_GOOD)) return FALSE;
2804                 if ((old_r_ptr->flags3 & RF3_EVIL) && !(r_ptr->flags3 & RF3_EVIL)) return FALSE;
2805                 if (!(old_r_ptr->flags3 & (RF3_GOOD | RF3_EVIL)) && (r_ptr->flags3 & (RF3_GOOD | RF3_EVIL))) return FALSE;
2806         }
2807
2808         /* Born now */
2809         else if (summon_specific_who > 0)
2810         {
2811                 monster_type *sm_ptr = &m_list[summon_specific_who];
2812
2813                 if ((sm_ptr->sub_align & SUB_ALIGN_EVIL) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
2814                 if ((sm_ptr->sub_align & SUB_ALIGN_GOOD) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
2815         }
2816
2817         return (*(get_monster_hook()))(r_idx);
2818 }
2819
2820
2821 void choose_new_monster(int m_idx, bool born, int r_idx)
2822 {
2823         int oldmaxhp;
2824         monster_type *m_ptr = &m_list[m_idx];
2825         monster_race *r_ptr;
2826         char old_m_name[80];
2827         bool old_unique = FALSE;
2828
2829         if (r_info[m_ptr->r_idx].flags1 & RF1_UNIQUE)
2830                 old_unique = TRUE;
2831         if (old_unique && (r_idx == MON_CHAMELEON)) r_idx = MON_CHAMELEON_K;
2832         r_ptr = &r_info[r_idx];
2833
2834         monster_desc(old_m_name, m_ptr, 0);
2835
2836         if (!r_idx)
2837         {
2838                 int level;
2839
2840                 chameleon_change_m_idx = m_idx;
2841                 if (old_unique)
2842                         get_mon_num_prep(monster_hook_chameleon_lord, NULL);
2843                 else
2844                         get_mon_num_prep(monster_hook_chameleon, NULL);
2845
2846                 if (old_unique)
2847                         level = r_info[MON_CHAMELEON_K].level;
2848                 else if (!dun_level)
2849                         level = wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].level;
2850                 else
2851                         level = dun_level;
2852
2853                 if (d_info[dungeon_type].flags1 & DF1_CHAMELEON) level+= 2+randint1(3);
2854
2855                 r_idx = get_mon_num(level);
2856                 r_ptr = &r_info[r_idx];
2857
2858                 chameleon_change_m_idx = 0;
2859
2860                 /* Paranoia */
2861                 if (!r_idx) return;
2862         }
2863
2864         m_ptr->r_idx = r_idx;
2865         m_ptr->ap_r_idx = r_idx;
2866         update_mon(m_idx, FALSE);
2867         lite_spot(m_ptr->fy, m_ptr->fx);
2868         if (born)
2869         {
2870                 /* Sub-alignment of a chameleon */
2871                 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
2872                 {
2873                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
2874                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
2875                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
2876                 }
2877                 return;
2878         }
2879
2880         if (m_idx == p_ptr->riding)
2881         {
2882                 char m_name[80];
2883                 monster_desc(m_name, m_ptr, 0);
2884 #ifdef JP
2885                 msg_format("ÆÍÁ³%s¤¬ÊѿȤ·¤¿¡£", old_m_name);
2886 #else
2887                 msg_format("Suddenly, %s transforms!", old_m_name);
2888 #endif
2889                 if (!(r_ptr->flags7 & RF7_RIDING))
2890 #ifdef JP
2891                         if (rakuba(0, TRUE)) msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2892 #else
2893                         if (rakuba(0, TRUE)) msg_format("You have fallen from %s.", m_name);
2894 #endif
2895         }
2896
2897         /* Extract the monster base speed */
2898         m_ptr->mspeed = get_mspeed(r_ptr);
2899
2900         oldmaxhp = m_ptr->max_maxhp;
2901         /* Assign maximal hitpoints */
2902         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2903         {
2904                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2905         }
2906         else
2907         {
2908                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2909         }
2910         m_ptr->maxhp = (long)(m_ptr->maxhp * m_ptr->max_maxhp) / oldmaxhp;
2911         m_ptr->hp = (long)(m_ptr->hp * m_ptr->max_maxhp) / oldmaxhp;
2912 }
2913
2914
2915 /*
2916  *  Hook for Tanuki
2917  */
2918 static bool monster_hook_tanuki(int r_idx)
2919 {
2920         monster_race *r_ptr = &r_info[r_idx];
2921
2922         if (r_ptr->flags1 & (RF1_UNIQUE)) return FALSE;
2923         if (r_ptr->flags2 & RF2_MULTIPLY) return FALSE;
2924         if (r_ptr->flags7 & (RF7_FRIENDLY | RF7_CHAMELEON)) return FALSE;
2925         if (r_ptr->flags7 & RF7_AQUATIC) return FALSE;
2926         
2927         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))
2928                 return FALSE;
2929
2930         return (*(get_monster_hook()))(r_idx);
2931 }
2932
2933
2934 /*
2935  *  Set initial racial appearance of a monster
2936  */
2937 static int initial_r_appearance(int r_idx)
2938 {
2939         int attempts = 1000;
2940
2941         int ap_r_idx;
2942         int min = MIN(base_level-5, 50);
2943
2944         if (!(r_info[r_idx].flags7 & RF7_TANUKI))
2945                 return r_idx;
2946
2947         get_mon_num_prep(monster_hook_tanuki, NULL);
2948
2949         while (--attempts)
2950         {
2951                 ap_r_idx = get_mon_num(base_level + 10);
2952                 if (r_info[ap_r_idx].level >= min) return ap_r_idx;
2953         }
2954
2955         return r_idx;
2956 }
2957
2958
2959 /*
2960  * Get initial monster speed
2961  */
2962 byte get_mspeed(monster_race *r_ptr)
2963 {
2964         /* Extract the monster base speed */
2965         int mspeed = r_ptr->speed;
2966
2967         /* Hack -- small racial variety */
2968         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2969         {
2970                 /* Allow some small variation per monster */
2971                 int i = SPEED_TO_ENERGY(r_ptr->speed) / (one_in_(4) ? 3 : 10);
2972                 if (i) mspeed += rand_spread(0, i);
2973         }
2974
2975         if (mspeed > 199) mspeed = 199;
2976
2977         return (byte)mspeed;
2978 }
2979
2980
2981 /*
2982  * Attempt to place a monster of the given race at the given location.
2983  *
2984  * To give the player a sporting chance, any monster that appears in
2985  * line-of-sight and is extremely dangerous can be marked as
2986  * "FORCE_SLEEP", which will cause them to be placed with low energy,
2987  * which often (but not always) lets the player move before they do.
2988  *
2989  * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
2990  *
2991  * XXX XXX XXX Use special "here" and "dead" flags for unique monsters,
2992  * remove old "cur_num" and "max_num" fields.
2993  *
2994  * XXX XXX XXX Actually, do something similar for artifacts, to simplify
2995  * the "preserve" mode, and to make the "what artifacts" flag more useful.
2996  *
2997  * This is the only function which may place a monster in the dungeon,
2998  * except for the savefile loading code.
2999  */
3000 bool place_monster_one(int who, int y, int x, int r_idx, u32b mode)
3001 {
3002         cave_type               *c_ptr;
3003
3004         monster_type    *m_ptr;
3005
3006         monster_race    *r_ptr = &r_info[r_idx];
3007
3008         cptr            name = (r_name + r_ptr->name);
3009
3010         /* DO NOT PLACE A MONSTER IN THE SMALL SCALE WILDERNESS !!! */
3011         if(p_ptr->wild_mode) return FALSE;
3012
3013         /* Verify location */
3014         if (!in_bounds(y, x)) return (FALSE);
3015
3016         /* Require empty space (if not ghostly) */
3017         if (!(!dun_level && (cave[y][x].feat == FEAT_MOUNTAIN) && ((r_ptr->flags8 & RF8_WILD_MOUNTAIN) || (r_ptr->flags7 & RF7_CAN_FLY))) &&
3018             !(cave_empty_bold2(y, x) || (mode & PM_IGNORE_TERRAIN)) &&
3019             !((r_ptr->flags2 & RF2_PASS_WALL) &&
3020               !(cave_perma_bold(y, x) || cave[y][x].m_idx ||
3021             player_bold(y, x)))) return (FALSE);
3022
3023         /* Paranoia */
3024         if (!r_idx) return (FALSE);
3025
3026         /* Paranoia */
3027         if (!r_ptr->name) return (FALSE);
3028
3029         /* Nor on the Pattern */
3030         if ((cave[y][x].feat >= FEAT_PATTERN_START)
3031          && (cave[y][x].feat <= FEAT_PATTERN_XTRA2))
3032                 return (FALSE);
3033
3034         if (!(mode & PM_IGNORE_TERRAIN) &&
3035             !monster_can_cross_terrain(cave[y][x].feat, r_ptr))
3036         {
3037                 return FALSE;
3038         }
3039
3040         if (!p_ptr->inside_battle)
3041         {
3042                 /* Hack -- "unique" monsters must be "unique" */
3043                 if (((r_ptr->flags1 & (RF1_UNIQUE)) ||
3044                      (r_ptr->flags7 & (RF7_UNIQUE_7))) &&
3045                     (r_ptr->cur_num >= r_ptr->max_num))
3046                 {
3047                         /* Cannot create */
3048                         return (FALSE);
3049                 }
3050                 
3051                 if ((r_ptr->flags7 & (RF7_UNIQUE2)) &&
3052                     (r_ptr->cur_num >= 1))
3053                 {
3054                         return (FALSE);
3055                 }
3056
3057                 if (r_idx == MON_BANORLUPART)
3058                 {
3059                         if (r_info[MON_BANOR].cur_num > 0) return FALSE;
3060                         if (r_info[MON_LUPART].cur_num > 0) return FALSE;
3061                 }
3062                 
3063                 /* Depth monsters may NOT be created out of depth, unless in Nightmare mode */
3064                 if ((r_ptr->flags1 & (RF1_FORCE_DEPTH)) && (dun_level < r_ptr->level) &&
3065                     (!ironman_nightmare || (r_ptr->flags1 & (RF1_QUESTOR))))
3066                 {
3067                         /* Cannot create */
3068                         return (FALSE);
3069                 }
3070         }
3071
3072         if(quest_number(dun_level))
3073         {
3074                 int hoge = quest_number(dun_level);
3075                 if((quest[hoge].type == QUEST_TYPE_KILL_LEVEL) || (quest[hoge].type == QUEST_TYPE_RANDOM))
3076                 {
3077                         if(r_idx == quest[hoge].r_idx)
3078                         {
3079                                 int number_mon, i2, j2;
3080                                 number_mon = 0;
3081
3082                                 /* Count all quest monsters */
3083                                 for (i2 = 0; i2 < cur_wid; ++i2)
3084                                         for (j2 = 0; j2 < cur_hgt; j2++)
3085                                                 if (cave[j2][i2].m_idx > 0)
3086                                                         if (m_list[cave[j2][i2].m_idx].r_idx == quest[hoge].r_idx)
3087                                                                 number_mon++;
3088                                 if(number_mon + quest[hoge].cur_num >= quest[hoge].max_num)
3089                                         return FALSE;
3090                         }
3091                 }
3092         }
3093
3094         /* Access the location */
3095         c_ptr = &cave[y][x];
3096
3097         if (is_glyph_grid(c_ptr))
3098         {
3099                 if (randint1(BREAK_GLYPH) < (r_ptr->level+20))
3100                 {
3101                         /* Describe observable breakage */
3102                         if (c_ptr->info & CAVE_MARK)
3103                         {
3104 #ifdef JP
3105 msg_print("¼é¤ê¤Î¥ë¡¼¥ó¤¬²õ¤ì¤¿¡ª");
3106 #else
3107                                 msg_print("The rune of protection is broken!");
3108 #endif
3109
3110                         }
3111
3112                         /* Forget the rune */
3113                         c_ptr->info &= ~(CAVE_MARK);
3114
3115                         /* Break the rune */
3116                         c_ptr->info &= ~(CAVE_OBJECT);
3117                         c_ptr->mimic = 0;
3118
3119                         /* Notice */
3120                         note_spot(y, x);
3121                 }
3122                 else return FALSE;
3123         }
3124
3125         /* Powerful monster */
3126         if (r_ptr->level > dun_level)
3127         {
3128                 /* Unique monsters */
3129                 if (r_ptr->flags1 & (RF1_UNIQUE))
3130                 {
3131                         /* Message for cheaters */
3132 #ifdef JP
3133                         if (cheat_hear) msg_format("¿¼ÁؤΥæ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3134 #else
3135                         if (cheat_hear) msg_format("Deep Unique (%s).", name);
3136 #endif
3137
3138
3139                         /* Boost rating by twice delta-depth */
3140                         rating += (r_ptr->level - dun_level) * 2;
3141                 }
3142
3143                 /* Normal monsters */
3144                 else
3145                 {
3146                         /* Message for cheaters */
3147 #ifdef JP
3148                         if (cheat_hear) msg_format("¿¼ÁؤΥâ¥ó¥¹¥¿¡¼ (%s)¡£", name);
3149 #else
3150                         if (cheat_hear) msg_format("Deep Monster (%s).", name);
3151 #endif
3152
3153
3154                         /* Boost rating by delta-depth */
3155                         rating += (r_ptr->level - dun_level);
3156                 }
3157         }
3158
3159         /* Note the monster */
3160         else if (r_ptr->flags1 & (RF1_UNIQUE))
3161         {
3162                 /* Unique monsters induce message */
3163 #ifdef JP
3164                 if (cheat_hear) msg_format("¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
3165 #else
3166                 if (cheat_hear) msg_format("Unique (%s).", name);
3167 #endif
3168
3169         }
3170
3171         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7) || (r_ptr->level < 10)) is_kage = FALSE;
3172
3173         /* Make a new monster */
3174         c_ptr->m_idx = m_pop();
3175         hack_m_idx_ii = c_ptr->m_idx;
3176
3177         /* Mega-Hack -- catch "failure" */
3178         if (!c_ptr->m_idx) return (FALSE);
3179
3180
3181         /* Get a new monster record */
3182         m_ptr = &m_list[c_ptr->m_idx];
3183
3184         /* Save the race */
3185         m_ptr->r_idx = r_idx;
3186         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
3187
3188         /* Sub-alignment of a monster */
3189         if ((who > 0) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
3190                 m_ptr->sub_align = m_list[who].sub_align;
3191         else
3192         {
3193                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3194                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
3195                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
3196         }
3197
3198         /* Place the monster at the location */
3199         m_ptr->fy = y;
3200         m_ptr->fx = x;
3201
3202
3203         /* No "damage" yet */
3204         m_ptr->stunned = 0;
3205         m_ptr->confused = 0;
3206         m_ptr->monfear = 0;
3207
3208         /* Unknown distance */
3209         m_ptr->cdis = 0;
3210
3211         reset_target(m_ptr);
3212
3213         m_ptr->nickname = 0;
3214
3215         m_ptr->exp = 0;
3216
3217         /* No flags */
3218         m_ptr->mflag = 0;
3219         m_ptr->mflag2 = 0;
3220
3221         if (r_ptr->flags7 & RF7_CHAMELEON)
3222         {
3223                 choose_new_monster(c_ptr->m_idx, TRUE, 0);
3224                 r_ptr = &r_info[m_ptr->r_idx];
3225                 m_ptr->mflag2 |= MFLAG2_CHAMELEON;
3226                 rating++;
3227
3228                 /* Hack - Set sub_align to neutral when the Chameleon Lord is generated as "GUARDIAN" */
3229                 if ((r_ptr->flags1 & RF1_UNIQUE) && (who <= 0))
3230                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3231         }
3232         else if (is_kage)
3233         {
3234                 m_ptr->ap_r_idx = MON_KAGE;
3235                 m_ptr->mflag2 |= MFLAG2_KAGE;
3236         }
3237
3238         if (mode & PM_NO_PET) m_ptr->mflag2 |= MFLAG2_NOPET;
3239
3240         /* Not visible */
3241         m_ptr->ml = FALSE;
3242
3243         /* Pet? */
3244         if (mode & PM_FORCE_PET)
3245         {
3246                 set_pet(m_ptr);
3247         }
3248         /* Friendly? */
3249         else if ((r_ptr->flags7 & RF7_FRIENDLY) ||
3250                  (mode & PM_FORCE_FRIENDLY) || is_friendly_idx(who))
3251         {
3252                 if (!(p_ptr->align >= 0 && (r_ptr->flags3 & RF3_EVIL)) &&
3253                     !(p_ptr->align < 0 && (r_ptr->flags3 & RF3_GOOD)))
3254                 {
3255                         set_friendly(m_ptr);
3256                 }
3257         }
3258
3259         /* Assume no sleeping */
3260         m_ptr->csleep = 0;
3261
3262         /* Enforce sleeping if needed */
3263         if ((mode & PM_ALLOW_SLEEP) && r_ptr->sleep && !ironman_nightmare)
3264         {
3265                 int val = r_ptr->sleep;
3266                 m_ptr->csleep = ((val * 2) + randint1(val * 10));
3267         }
3268
3269         /* Assign maximal hitpoints */
3270         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
3271         {
3272                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
3273         }
3274         else
3275         {
3276                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
3277         }
3278
3279         /* Monsters have double hitpoints in Nightmare mode */
3280         if (ironman_nightmare)
3281         {
3282                 u32b hp = m_ptr->max_maxhp * 2L;
3283
3284                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
3285         }
3286
3287         m_ptr->maxhp = m_ptr->max_maxhp;
3288
3289         /* And start out fully healthy */
3290         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
3291                 m_ptr->hp = m_ptr->maxhp / 2;
3292         else m_ptr->hp = m_ptr->maxhp;
3293
3294
3295         /* Extract the monster base speed */
3296         m_ptr->mspeed = get_mspeed(r_ptr);
3297
3298         if (mode & PM_HASTE) m_ptr->fast = 100;
3299
3300         /* Give a random starting energy */
3301         if (!ironman_nightmare)
3302         {
3303                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
3304         }
3305         else
3306         {
3307                 /* Nightmare monsters are more prepared */
3308                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
3309         }
3310
3311         /* Force monster to wait for player, unless in Nightmare mode */
3312         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
3313         {
3314                 /* Monster is still being nice */
3315                 m_ptr->mflag |= (MFLAG_NICE);
3316
3317                 /* Must repair monsters */
3318                 repair_monsters = TRUE;
3319         }
3320
3321         /* Hack -- see "process_monsters()" */
3322         if (c_ptr->m_idx < hack_m_idx)
3323         {
3324                 /* Monster is still being born */
3325                 m_ptr->mflag |= (MFLAG_BORN);
3326         }
3327
3328
3329         if (r_ptr->flags7 & (RF7_SELF_LITE_1 | RF7_SELF_LITE_2))
3330                 p_ptr->update |= (PU_MON_LITE);
3331         else if ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !m_ptr->csleep)
3332                 p_ptr->update |= (PU_MON_LITE);
3333
3334         /* Update the monster */
3335         update_mon(c_ptr->m_idx, TRUE);
3336
3337
3338         /* Count the monsters on the level */
3339         real_r_ptr(m_ptr)->cur_num++;
3340
3341         /*
3342          * Memorize location of the unique monster in saved floors.
3343          * A unique monster move from old saved floor.
3344          */
3345         if (character_dungeon &&
3346             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)))
3347                 real_r_ptr(m_ptr)->floor_id = p_ptr->floor_id;
3348
3349         /* Hack -- Count the number of "reproducers" */
3350         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
3351
3352
3353         /* Hack -- Notice new multi-hued monsters */
3354         {
3355                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
3356                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
3357                         shimmer_monsters = TRUE;
3358         }
3359
3360         if (p_ptr->warning && character_dungeon)
3361         {
3362                 if (r_ptr->flags1 & RF1_UNIQUE)
3363                 {
3364                         cptr color;
3365                         object_type *o_ptr;
3366                         char o_name[MAX_NLEN];
3367
3368                         if (r_ptr->level > p_ptr->lev + 30)
3369 #ifdef JP
3370                                 color = "¹õ¤¯";
3371 #else
3372                                 color = "black";
3373 #endif
3374                         else if (r_ptr->level > p_ptr->lev + 15)
3375 #ifdef JP
3376                                 color = "»ç¿§¤Ë";
3377 #else
3378                                 color = "purple";
3379 #endif
3380                         else if (r_ptr->level > p_ptr->lev + 5)
3381 #ifdef JP
3382                                 color = "¥ë¥Ó¡¼¿§¤Ë";
3383 #else
3384                                 color = "deep red";
3385 #endif
3386                         else if (r_ptr->level > p_ptr->lev - 5)
3387 #ifdef JP
3388                                 color = "ÀÖ¤¯";
3389 #else
3390                                 color = "red";
3391 #endif
3392                         else if (r_ptr->level > p_ptr->lev - 15)
3393 #ifdef JP
3394                                 color = "¥Ô¥ó¥¯¿§¤Ë";
3395 #else
3396                                 color = "pink";
3397 #endif
3398                         else
3399 #ifdef JP
3400                                 color = "Çò¤¯";
3401 #else
3402                                 color = "white";
3403 #endif
3404
3405                         o_ptr = choose_warning_item();
3406                         object_desc(o_name, o_ptr, FALSE, 0);
3407 #ifdef JP
3408                         msg_format("%s¤Ï%s¸÷¤Ã¤¿¡£",o_name, color);
3409 #else
3410                         msg_format("%s glows %s.",o_name, color);
3411 #endif
3412                 }
3413         }
3414
3415         if (is_explosive_rune_grid(c_ptr))
3416         {
3417                 /* Break the ward */
3418                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3419                 {
3420                         /* Describe observable breakage */
3421                         if (c_ptr->info & CAVE_MARK)
3422                         {
3423 #ifdef JP
3424 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3425 #else
3426                                 msg_print("The rune explodes!");
3427 #endif
3428
3429                                 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);
3430                         }
3431                 }
3432                 else
3433                 {
3434 #ifdef JP
3435 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3436 #else
3437                         msg_print("An explosive rune was disarmed.");
3438 #endif
3439                 }
3440
3441                 /* Forget the rune */
3442                 c_ptr->info &= ~(CAVE_MARK);
3443
3444                 /* Break the rune */
3445                 c_ptr->info &= ~(CAVE_OBJECT);
3446                 c_ptr->mimic = 0;
3447
3448                 note_spot(y, x);
3449                 lite_spot(y, x);
3450         }
3451
3452         /* Success */
3453         return (TRUE);
3454 }
3455
3456
3457 /*
3458  *  improved version of scatter() for place monster
3459  */
3460
3461 #define MON_SCAT_MAXD 10
3462
3463 static bool mon_scatter(int *yp, int *xp, int y, int x, int max_dist)
3464 {
3465         int place_x[MON_SCAT_MAXD];
3466         int place_y[MON_SCAT_MAXD];
3467         int num[MON_SCAT_MAXD];
3468         int i;
3469         int nx, ny;
3470
3471         if (max_dist >= MON_SCAT_MAXD)
3472                 return FALSE;
3473
3474         for (i = 0; i < MON_SCAT_MAXD; i++)
3475                 num[i] = 0;
3476
3477         for (nx = x - max_dist; nx <= x + max_dist; nx++)
3478                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
3479                 {
3480                         /* Ignore annoying locations */
3481                         if (!in_bounds(ny, nx)) continue;
3482
3483                         /* Require "line of sight" */
3484                         if (!los(y, x, ny, nx)) continue;
3485
3486                         /* Walls and Monsters block flow */
3487                         if (!cave_empty_bold2(ny, nx)) continue;
3488                         if (cave[ny][nx].m_idx) continue;
3489                         if (player_bold(ny, nx)) continue;
3490
3491                         /* ... nor on the Pattern */
3492                         if ((cave[ny][nx].feat >= FEAT_PATTERN_START) &&
3493                             (cave[ny][nx].feat <= FEAT_PATTERN_XTRA2))
3494                                 continue;
3495
3496                         i = distance(y, x, ny, nx);
3497
3498                         if (i > max_dist)
3499                                 continue;
3500
3501                         num[i]++;
3502
3503                         /* random swap */
3504                         if(one_in_(num[i]))
3505                         {
3506                                 place_x[i] = nx;
3507                                 place_y[i] = ny;
3508                         }
3509                 }
3510
3511         i = 0;
3512         while (i < MON_SCAT_MAXD && 0 == num[i])
3513                 i++;
3514         if (i >= MON_SCAT_MAXD)
3515                 return FALSE;
3516
3517         *xp = place_x[i];
3518         *yp = place_y[i];
3519
3520         return TRUE;
3521 }
3522
3523
3524 /*
3525  * Maximum size of a group of monsters
3526  */
3527 #define GROUP_MAX       32
3528
3529
3530 /*
3531  * Attempt to place a "group" of monsters around the given location
3532  */
3533 static bool place_monster_group(int who, int y, int x, int r_idx, u32b mode)
3534 {
3535         monster_race *r_ptr = &r_info[r_idx];
3536
3537         int old, n, i;
3538         int total = 0, extra = 0;
3539
3540         int hack_n = 0;
3541
3542         byte hack_y[GROUP_MAX];
3543         byte hack_x[GROUP_MAX];
3544
3545
3546         /* Pick a group size */
3547         total = randint1(10);
3548
3549         /* Hard monsters, small groups */
3550         if (r_ptr->level > dun_level)
3551         {
3552                 extra = r_ptr->level - dun_level;
3553                 extra = 0 - randint1(extra);
3554         }
3555
3556         /* Easy monsters, large groups */
3557         else if (r_ptr->level < dun_level)
3558         {
3559                 extra = dun_level - r_ptr->level;
3560                 extra = randint1(extra);
3561         }
3562
3563         /* Hack -- limit group reduction */
3564         if (extra > 9) extra = 9;
3565
3566         /* Modify the group size */
3567         total += extra;
3568
3569         /* Minimum size */
3570         if (total < 1) total = 1;
3571
3572         /* Maximum size */
3573         if (total > GROUP_MAX) total = GROUP_MAX;
3574
3575
3576         /* Save the rating */
3577         old = rating;
3578
3579         /* Start on the monster */
3580         hack_n = 1;
3581         hack_x[0] = x;
3582         hack_y[0] = y;
3583
3584         /* Puddle monsters, breadth first, up to total */
3585         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3586         {
3587                 /* Grab the location */
3588                 int hx = hack_x[n];
3589                 int hy = hack_y[n];
3590
3591                 /* Check each direction, up to total */
3592                 for (i = 0; (i < 8) && (hack_n < total); i++)
3593                 {
3594                         int mx, my;
3595
3596                         scatter(&my, &mx, hy, hx, 4, 0);
3597
3598                         /* Walls and Monsters block flow */
3599                         if (!cave_empty_bold2(my, mx)) continue;
3600
3601                         /* Attempt to place another monster */
3602                         if (place_monster_one(who, my, mx, r_idx, mode))
3603                         {
3604                                 /* Add it to the "hack" set */
3605                                 hack_y[hack_n] = my;
3606                                 hack_x[hack_n] = mx;
3607                                 hack_n++;
3608                         }
3609                 }
3610         }
3611
3612         /* Hack -- restore the rating */
3613         rating = old;
3614
3615
3616         /* Success */
3617         return (TRUE);
3618 }
3619
3620
3621 /*
3622  * Hack -- help pick an escort type
3623  */
3624 static int place_monster_idx = 0;
3625 static int place_monster_m_idx = 0;
3626
3627 /*
3628  * Hack -- help pick an escort type
3629  */
3630 static bool place_monster_okay(int r_idx)
3631 {
3632         monster_race *r_ptr = &r_info[place_monster_idx];
3633         monster_type *m_ptr = &m_list[place_monster_m_idx];
3634
3635         monster_race *z_ptr = &r_info[r_idx];
3636
3637         /* Hack - Escorts have to have the same dungeon flag */
3638         if (monster_dungeon(place_monster_idx) != monster_dungeon(r_idx)) return (FALSE);
3639
3640         /* Require similar "race" */
3641         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3642
3643         /* Skip more advanced monsters */
3644         if (z_ptr->level > r_ptr->level) return (FALSE);
3645
3646         /* Skip unique monsters */
3647         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3648
3649         /* Paranoia -- Skip identical monsters */
3650         if (place_monster_idx == r_idx) return (FALSE);
3651
3652         /* Skip different alignment */
3653         if (((m_ptr->sub_align & SUB_ALIGN_EVIL) && (z_ptr->flags3 & RF3_GOOD)) ||
3654             ((m_ptr->sub_align & SUB_ALIGN_GOOD) && (z_ptr->flags3 & RF3_EVIL)))
3655                 return FALSE;
3656
3657         if (r_ptr->flags7 & RF7_FRIENDLY)
3658         {
3659                 if (((p_ptr->align < 0) && (z_ptr->flags3 & RF3_GOOD)) ||
3660                     ((p_ptr->align > 0) && (z_ptr->flags3 & RF3_EVIL)))
3661                         return FALSE;
3662         }
3663
3664         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3665                 return FALSE;
3666
3667         /* Okay */
3668         return (TRUE);
3669 }
3670
3671
3672 /*
3673  * Attempt to place a monster of the given race at the given location
3674  *
3675  * Note that certain monsters are now marked as requiring "friends".
3676  * These monsters, if successfully placed, and if the "grp" parameter
3677  * is TRUE, will be surrounded by a "group" of identical monsters.
3678  *
3679  * Note that certain monsters are now marked as requiring an "escort",
3680  * which is a collection of monsters with similar "race" but lower level.
3681  *
3682  * Some monsters induce a fake "group" flag on their escorts.
3683  *
3684  * Note the "bizarre" use of non-recursion to prevent annoying output
3685  * when running a code profiler.
3686  *
3687  * Note the use of the new "monster allocation table" code to restrict
3688  * the "get_mon_num()" function to "legal" escort types.
3689  */
3690 bool place_monster_aux(int who, int y, int x, int r_idx, u32b mode)
3691 {
3692         int             i;
3693         monster_race    *r_ptr = &r_info[r_idx];
3694
3695         if (one_in_(333) && !(mode & PM_NO_KAGE) && !(mode & PM_FORCE_PET))
3696                 is_kage = TRUE;
3697         else
3698                 is_kage = FALSE;
3699
3700         /* Place one monster, or fail */
3701         if (!place_monster_one(who, y, x, r_idx, mode)) return (FALSE);
3702
3703
3704         /* Require the "group" flag */
3705         if (!(mode & PM_ALLOW_GROUP)) return (TRUE);
3706
3707         place_monster_m_idx = hack_m_idx_ii;
3708
3709         /* Friends for certain monsters */
3710         if (r_ptr->flags1 & (RF1_FRIENDS))
3711         {
3712                 /* Attempt to place a group */
3713                 (void)place_monster_group(who, y, x, r_idx, mode);
3714         }
3715
3716
3717         /* Escorts for certain monsters */
3718         if (r_ptr->flags1 & (RF1_ESCORT))
3719         {
3720                 /* Set the escort index */
3721                 place_monster_idx = r_idx;
3722
3723                 /* Try to place several "escorts" */
3724                 for (i = 0; i < 32; i++)
3725                 {
3726                         int nx, ny, z, d = 3;
3727
3728                         /* Pick a location */
3729                         scatter(&ny, &nx, y, x, d, 0);
3730
3731                         /* Require empty grids */
3732                         if (!cave_empty_bold2(ny, nx)) continue;
3733
3734                         /* Prepare allocation table */
3735                         get_mon_num_prep(place_monster_okay, get_monster_hook2(ny, nx));
3736
3737                         /* Pick a random race */
3738                         z = get_mon_num(r_ptr->level);
3739
3740                         /* Handle failure */
3741                         if (!z) break;
3742
3743                         /* Place a single escort */
3744                         (void)place_monster_one(place_monster_m_idx, ny, nx, z, mode);
3745
3746                         /* Place a "group" of escorts if needed */
3747                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3748                             (r_ptr->flags1 & RF1_ESCORTS))
3749                         {
3750                                 /* Place a group of monsters */
3751                                 (void)place_monster_group(place_monster_m_idx, ny, nx, z, mode);
3752                         }
3753                 }
3754         }
3755
3756         /* Success */
3757         return (TRUE);
3758 }
3759
3760
3761 /*
3762  * Hack -- attempt to place a monster at the given location
3763  *
3764  * Attempt to find a monster appropriate to the "monster_level"
3765  */
3766 bool place_monster(int y, int x, u32b mode)
3767 {
3768         int r_idx;
3769
3770         /* Prepare allocation table */
3771         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3772
3773         /* Pick a monster */
3774         r_idx = get_mon_num(monster_level);
3775
3776         /* Handle failure */
3777         if (!r_idx) return (FALSE);
3778
3779         /* Attempt to place the monster */
3780         if (place_monster_aux(0, y, x, r_idx, mode)) return (TRUE);
3781
3782         /* Oops */
3783         return (FALSE);
3784 }
3785
3786
3787 #ifdef MONSTER_HORDES
3788
3789 bool alloc_horde(int y, int x)
3790 {
3791         monster_race *r_ptr = NULL;
3792         int r_idx = 0;
3793         int m_idx;
3794         int attempts = 1000;
3795         int cy = y;
3796         int cx = x;
3797
3798         /* Prepare allocation table */
3799         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3800
3801         while (--attempts)
3802         {
3803                 /* Pick a monster */
3804                 r_idx = get_mon_num(monster_level);
3805
3806                 /* Handle failure */
3807                 if (!r_idx) return (FALSE);
3808
3809                 r_ptr = &r_info[r_idx];
3810
3811                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3812
3813                 if (r_idx == MON_HAGURE) continue;
3814                 break;
3815         }
3816         if (attempts < 1) return FALSE;
3817
3818         attempts = 1000;
3819
3820         while (--attempts)
3821         {
3822                 /* Attempt to place the monster */
3823                 if (place_monster_aux(0, y, x, r_idx, 0L)) break;
3824         }
3825
3826         if (attempts < 1) return FALSE;
3827
3828         m_idx = cave[y][x].m_idx;
3829
3830         if (m_list[m_idx].mflag2 & MFLAG2_CHAMELEON) r_ptr = &r_info[m_list[m_idx].r_idx];
3831         summon_kin_type = r_ptr->d_char;
3832
3833         for (attempts = randint1(10) + 5; attempts; attempts--)
3834         {
3835                 scatter(&cy, &cx, y, x, 5, 0);
3836
3837                 (void)summon_specific(m_idx, cy, cx, dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
3838
3839                 y = cy;
3840                 x = cx;
3841         }
3842
3843         return TRUE;
3844 }
3845
3846 #endif /* MONSTER_HORDES */
3847
3848
3849
3850 /*
3851  * Attempt to allocate a random monster in the dungeon.
3852  *
3853  * Place the monster at least "dis" distance from the player.
3854  *
3855  * Use "slp" to choose the initial "sleep" status
3856  *
3857  * Use "monster_level" for the monster level
3858  */
3859 bool alloc_monster(int dis, u32b mode)
3860 {
3861         int                     y = 0, x = 0;
3862         int         attempts_left = 10000;
3863         int guardian = d_info[dungeon_type].final_guardian;
3864
3865         /* Put an Guardian */
3866         if(guardian && d_info[dungeon_type].maxdepth == dun_level && r_info[guardian].cur_num < r_info[guardian].max_num )
3867         {
3868                 int oy;
3869                 int ox;
3870                 int try = 4000;
3871
3872                 /* Find a good position */
3873                 while(try)
3874                 {
3875                         /* Get a random spot */
3876                         oy = randint1(cur_hgt - 4) + 2;
3877                         ox = randint1(cur_wid - 4) + 2;
3878
3879                         /* Is it a good spot ? */
3880                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[guardian]))
3881                         {
3882                                 /* Place the guardian */
3883                                 if (place_monster_aux(0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) break;
3884                         }
3885                         /* One less try */
3886                         try--;
3887                 }
3888         }
3889
3890
3891         /* Find a legal, distant, unoccupied, space */
3892         while (attempts_left--)
3893         {
3894                 /* Pick a location */
3895                 y = randint0(cur_hgt);
3896                 x = randint0(cur_wid);
3897
3898                 /* Require empty floor grid (was "naked") */
3899                 if (dun_level || (wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].terrain != TERRAIN_MOUNTAIN))
3900                 {
3901                         if (!cave_empty_bold2(y, x)) continue;
3902                 }
3903                 else
3904                 {
3905                         if (!cave_empty_bold2(y, x) && (cave[y][x].feat != FEAT_MOUNTAIN)) continue;
3906                 }
3907
3908                 /* Accept far away grids */
3909                 if (distance(y, x, py, px) > dis) break;
3910         }
3911
3912         if (!attempts_left)
3913         {
3914                 if (cheat_xtra || cheat_hear)
3915                 {
3916 #ifdef JP
3917 msg_print("·Ù¹ð¡ª¿·¤¿¤Ê¥â¥ó¥¹¥¿¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡£¾®¤µ¤¤³¬¤Ç¤¹¤«¡©");
3918 #else
3919                         msg_print("Warning! Could not allocate a new monster. Small level?");
3920 #endif
3921
3922                 }
3923
3924                 return (FALSE);
3925         }
3926
3927
3928 #ifdef MONSTER_HORDES
3929         if (randint1(5000) <= dun_level)
3930         {
3931                 if (alloc_horde(y, x))
3932                 {
3933 #ifdef JP
3934                         if (cheat_hear) msg_format("¥â¥ó¥¹¥¿¡¼¤ÎÂç·²(%c)", summon_kin_type);
3935 #else
3936                         if (cheat_hear) msg_format("Monster horde (%c).", summon_kin_type);
3937 #endif
3938
3939                         return (TRUE);
3940                 }
3941         }
3942         else
3943         {
3944 #endif /* MONSTER_HORDES */
3945
3946                 /* Attempt to place the monster, allow groups */
3947                 if (place_monster(y, x, (mode | PM_ALLOW_GROUP))) return (TRUE);
3948
3949 #ifdef MONSTER_HORDES
3950         }
3951 #endif /* MONSTER_HORDES */
3952
3953         /* Nope */
3954         return (FALSE);
3955 }
3956
3957
3958
3959
3960 /*
3961  * Hack -- help decide if a monster race is "okay" to summon
3962  */
3963 static bool summon_specific_okay(int r_idx)
3964 {
3965         monster_race *r_ptr = &r_info[r_idx];
3966
3967         /* Hack - Only summon dungeon monsters */
3968         if (!monster_dungeon(r_idx)) return (FALSE);
3969
3970         /* Hack -- identify the summoning monster */
3971         if (summon_specific_who > 0)
3972         {
3973                 monster_type *m_ptr = &m_list[summon_specific_who];
3974
3975                 /* Do not summon enemies */
3976
3977                 /* Friendly vs. opposite aligned normal or pet */
3978                 if (((r_ptr->flags3 & RF3_EVIL) &&
3979                           (m_ptr->sub_align & SUB_ALIGN_GOOD)) ||
3980                          ((r_ptr->flags3 & RF3_GOOD) &&
3981                           (m_ptr->sub_align & SUB_ALIGN_EVIL)))
3982                 {
3983                         return FALSE;
3984                 }
3985
3986                 /* Hostile vs. non-hostile */
3987                 if (is_hostile(m_ptr) != summon_specific_hostile)
3988                 {
3989                         return FALSE;
3990                 }
3991         }
3992         /* Use the player's alignment */
3993         else if (summon_specific_who < 0)
3994         {
3995                 /* Do not summon enemies of the pets */
3996                 if ((p_ptr->align < -9) && (r_ptr->flags3 & RF3_GOOD))
3997                 {
3998                         if (!one_in_((0-p_ptr->align)/2+1)) return FALSE;
3999                 }
4000                 else if ((p_ptr->align > 9) && (r_ptr->flags3 & RF3_EVIL))
4001                 {
4002                         if (!one_in_(p_ptr->align/2+1)) return FALSE;
4003                 }
4004         }
4005
4006         /* Hack -- no specific type specified */
4007         if (!summon_specific_type) return (TRUE);
4008
4009         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7))) return FALSE;
4010
4011         if ((summon_specific_who < 0) &&
4012             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)) &&
4013             (((p_ptr->align > 9) && (r_ptr->flags3 & RF3_EVIL)) ||
4014              ((p_ptr->align < -9) && (r_ptr->flags3 & RF3_GOOD))))
4015                 return FALSE;
4016
4017         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[dungeon_type].flags1 & DF1_CHAMELEON)) return TRUE;
4018
4019         return (summon_specific_aux(r_idx));
4020 }
4021
4022
4023 /*
4024  * Place a monster (of the specified "type") near the given
4025  * location.  Return TRUE if a monster was actually summoned.
4026  *
4027  * We will attempt to place the monster up to 10 times before giving up.
4028  *
4029  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
4030  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
4031  * Note: None of the other summon codes will ever summon Unique's.
4032  *
4033  * This function has been changed.  We now take the "monster level"
4034  * of the summoning monster as a parameter, and use that, along with
4035  * the current dungeon level, to help determine the level of the
4036  * desired monster.  Note that this is an upper bound, and also
4037  * tends to "prefer" monsters of that level.  Currently, we use
4038  * the average of the dungeon and monster levels, and then add
4039  * five to allow slight increases in monster power.
4040  *
4041  * Note that we use the new "monster allocation table" creation code
4042  * to restrict the "get_mon_num()" function to the set of "legal"
4043  * monsters, making this function much faster and more reliable.
4044  *
4045  * Note that this function may not succeed, though this is very rare.
4046  */
4047 bool summon_specific(int who, int y1, int x1, int lev, int type, u32b mode)
4048 {
4049         int x, y, r_idx;
4050
4051         if (p_ptr->inside_arena) return (FALSE);
4052
4053         if (!mon_scatter(&y, &x, y1, x1, 2)) return FALSE;
4054
4055         /* Save the summoner */
4056         summon_specific_who = who;
4057
4058         /* Save the "summon" type */
4059         summon_specific_type = type;
4060
4061         summon_unique_okay = (mode & PM_ALLOW_UNIQUE) ? TRUE : FALSE;
4062
4063         /* Save the hostility */
4064         summon_specific_hostile = (!(mode & PM_FORCE_FRIENDLY) && !(is_friendly_idx(who)) && !(mode & PM_FORCE_PET));
4065
4066         /* Prepare allocation table */
4067         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
4068
4069         /* Pick a monster, using the level calculation */
4070         r_idx = get_mon_num((dun_level + lev) / 2 + 5);
4071
4072         /* Handle failure */
4073         if (!r_idx)
4074         {
4075                 summon_specific_type = 0;
4076                 return (FALSE);
4077         }
4078
4079         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) mode |= PM_NO_KAGE;
4080
4081         /* Attempt to place the monster (awake, allow groups) */
4082         if (!place_monster_aux(who, y, x, r_idx, mode))
4083         {
4084                 summon_specific_type = 0;
4085                 return (FALSE);
4086         }
4087
4088         summon_specific_type = 0;
4089         /* Success */
4090         return (TRUE);
4091 }
4092
4093 /* A "dangerous" function, creates a pet of the specified type */
4094 bool summon_named_creature (int who, int oy, int ox, int r_idx, u32b mode)
4095 {
4096         int x, y;
4097
4098         /* Paranoia */
4099         /* if (!r_idx) return; */
4100
4101         /* Prevent illegal monsters */
4102         if (r_idx >= max_r_idx) return FALSE;
4103
4104         if (p_ptr->inside_arena) return FALSE;
4105
4106         if (!mon_scatter(&y, &x, oy, ox, 2)) return FALSE;
4107
4108         /* Place it (allow groups) */
4109         return place_monster_aux(who, y, x, r_idx, (mode | PM_NO_KAGE));
4110 }
4111
4112
4113 /*
4114  * Let the given monster attempt to reproduce.
4115  *
4116  * Note that "reproduction" REQUIRES empty space.
4117  */
4118 bool multiply_monster(int m_idx, bool clone, u32b mode)
4119 {
4120         monster_type    *m_ptr = &m_list[m_idx];
4121
4122         int y, x;
4123
4124         if (!mon_scatter(&y, &x, m_ptr->fy, m_ptr->fx, 1))
4125                 return FALSE;
4126
4127         if (m_ptr->mflag2 & MFLAG2_NOPET) mode |= PM_NO_PET;
4128
4129         /* Create a new monster (awake, no groups) */
4130         if (!place_monster_aux(m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE)))
4131                 return FALSE;
4132
4133         if (clone)
4134         {
4135                 m_list[hack_m_idx_ii].smart |= SM_CLONED;
4136                 m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
4137         }
4138
4139         return TRUE;
4140 }
4141
4142
4143
4144
4145
4146 /*
4147  * Dump a message describing a monster's reaction to damage
4148  *
4149  * Technically should attempt to treat "Beholder"'s as jelly's
4150  */
4151 void message_pain(int m_idx, int dam)
4152 {
4153         long oldhp, newhp, tmp;
4154         int percentage;
4155
4156         monster_type *m_ptr = &m_list[m_idx];
4157         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4158
4159         char m_name[80];
4160
4161
4162         /* Get the monster name */
4163         monster_desc(m_name, m_ptr, 0);
4164
4165         /* Notice non-damage */
4166         if (dam == 0)
4167         {
4168 #ifdef JP
4169                 msg_format("%^s¤Ï¥À¥á¡¼¥¸¤ò¼õ¤±¤Æ¤¤¤Ê¤¤¡£", m_name);
4170 #else
4171                 msg_format("%^s is unharmed.", m_name);
4172 #endif
4173
4174                 return;
4175         }
4176
4177         /* Note -- subtle fix -CFT */
4178         newhp = (long)(m_ptr->hp);
4179         oldhp = newhp + (long)(dam);
4180         tmp = (newhp * 100L) / oldhp;
4181         percentage = (int)(tmp);
4182
4183
4184         /* Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs */
4185         if (strchr(",ejmvwQ", r_ptr->d_char))
4186         {
4187 #ifdef JP
4188                 if (percentage > 95)
4189                         msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4190                 else if (percentage > 75)
4191                         msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4192                 else if (percentage > 50)
4193                         msg_format("%^s¤Ï½Ì¤³¤Þ¤Ã¤¿¡£", m_name);
4194                 else if (percentage > 35)
4195                         msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4196                 else if (percentage > 20)
4197                         msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4198                 else if (percentage > 10)
4199                         msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4200                 else
4201                         msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4202 #else
4203                 if (percentage > 95)
4204                         msg_format("%^s barely notices.", m_name);
4205                 else if (percentage > 75)
4206                         msg_format("%^s flinches.", m_name);
4207                 else if (percentage > 50)
4208                         msg_format("%^s squelches.", m_name);
4209                 else if (percentage > 35)
4210                         msg_format("%^s quivers in pain.", m_name);
4211                 else if (percentage > 20)
4212                         msg_format("%^s writhes about.", m_name);
4213                 else if (percentage > 10)
4214                         msg_format("%^s writhes in agony.", m_name);
4215                 else
4216                         msg_format("%^s jerks limply.", m_name);
4217 #endif
4218
4219         }
4220
4221
4222         /* Fish */
4223         else if (strchr("l", r_ptr->d_char))
4224         {
4225                 if (percentage > 95)
4226 #ifdef JP
4227 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4228 #else
4229                         msg_format("%^s barely notices.", m_name);
4230 #endif
4231                 else if (percentage > 75)
4232 #ifdef JP
4233 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4234 #else
4235                         msg_format("%^s flinches.", m_name);
4236 #endif
4237                 else if (percentage > 50)
4238 #ifdef JP
4239 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4240 #else
4241                         msg_format("%^s hesitates.", m_name);
4242 #endif
4243                 else if (percentage > 35)
4244 #ifdef JP
4245 msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
4246 #else
4247                         msg_format("%^s quivers in pain.", m_name);
4248 #endif
4249                 else if (percentage > 20)
4250 #ifdef JP
4251 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4252 #else
4253                         msg_format("%^s writhes about.", m_name);
4254 #endif
4255                 else if (percentage > 10)
4256 #ifdef JP
4257 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4258 #else
4259                         msg_format("%^s writhes in agony.", m_name);
4260 #endif
4261                 else
4262 #ifdef JP
4263 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4264 #else
4265                         msg_format("%^s jerks limply.", m_name);
4266 #endif
4267         }
4268
4269
4270         /* Golems, Walls, Doors, Stairs */
4271         else if (strchr("g#+<>", r_ptr->d_char))
4272         {
4273                 if (percentage > 95)
4274 #ifdef JP
4275 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4276 #else
4277                         msg_format("%^s ignores the attack.", m_name);
4278 #endif
4279                 else if (percentage > 75)
4280 #ifdef JP
4281 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4282 #else
4283                         msg_format("%^s shrugs off the attack.", m_name);
4284 #endif
4285                 else if (percentage > 50)
4286 #ifdef JP
4287 msg_format("%^s¤ÏÍëÌĤΤ褦¤ËËʤ¨¤¿¡£", m_name);
4288 #else
4289                         msg_format("%^s roars thunderously.", m_name);
4290 #endif
4291                 else if (percentage > 35)
4292 #ifdef JP
4293 msg_format("%^s¤Ï¶ì¤·¤²¤ËËʤ¨¤¿¡£", m_name);
4294 #else
4295                         msg_format("%^s rumbles.", m_name);
4296 #endif
4297                 else if (percentage > 20)
4298 #ifdef JP
4299 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4300 #else
4301                         msg_format("%^s grunts.", m_name);
4302 #endif
4303                 else if (percentage > 10)
4304 #ifdef JP
4305 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4306 #else
4307                         msg_format("%^s hesitates.", m_name);
4308 #endif
4309                 else
4310 #ifdef JP
4311 msg_format("%^s¤Ï¤¯¤·¤ã¤¯¤·¤ã¤Ë¤Ê¤Ã¤¿¡£", m_name);
4312 #else
4313                         msg_format("%^s crumples.", m_name);
4314 #endif
4315         }
4316
4317
4318         /* Snakes, Hydrae, Reptiles, Mimics */
4319         else if (strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char))
4320         {
4321                 if (percentage > 95)
4322 #ifdef JP
4323 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4324 #else
4325                         msg_format("%^s barely notices.", m_name);
4326 #endif
4327                 else if (percentage > 75)
4328 #ifdef JP
4329 msg_format("%^s¤Ï¥·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4330 #else
4331                         msg_format("%^s hisses.", m_name);
4332 #endif
4333                 else if (percentage > 50)
4334 #ifdef JP
4335 msg_format("%^s¤ÏÅܤäÆƬ¤ò¾å¤²¤¿¡£", m_name);
4336 #else
4337                         msg_format("%^s rears up in anger.", m_name);
4338 #endif
4339                 else if (percentage > 35)
4340 #ifdef JP
4341 msg_format("%^s¤ÏÌÔÁ³¤È°Ò³Å¤·¤¿¡£", m_name);
4342 #else
4343                         msg_format("%^s hisses furiously.", m_name);
4344 #endif
4345                 else if (percentage > 20)
4346 #ifdef JP
4347 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4348 #else
4349                         msg_format("%^s writhes about.", m_name);
4350 #endif
4351                 else if (percentage > 10)
4352 #ifdef JP
4353 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4354 #else
4355                         msg_format("%^s writhes in agony.", m_name);
4356 #endif
4357                 else
4358 #ifdef JP
4359 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4360 #else
4361                         msg_format("%^s jerks limply.", m_name);
4362 #endif
4363         }
4364
4365
4366         /* Felines */
4367         else if (strchr("f", r_ptr->d_char))
4368         {
4369                 if (percentage > 95)
4370 #ifdef JP
4371 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4372 #else
4373                         msg_format("%^s shrugs off the attack.", m_name);
4374 #endif
4375                 else if (percentage > 75)
4376 #ifdef JP
4377 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4378 #else
4379                         msg_format("%^s roars.", m_name);
4380 #endif
4381                 else if (percentage > 50)
4382 #ifdef JP
4383 msg_format("%^s¤ÏÅܤäÆËʤ¨¤¿¡£", m_name);
4384 #else
4385                         msg_format("%^s growls angrily.", m_name);
4386 #endif
4387                 else if (percentage > 35)
4388 #ifdef JP
4389 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4390 #else
4391                         msg_format("%^s hisses with pain.", m_name);
4392 #endif
4393                 else if (percentage > 20)
4394 #ifdef JP
4395 msg_format("%^s¤ÏÄˤߤǼ塹¤·¤¯ÌĤ¤¤¿¡£", m_name);
4396 #else
4397                         msg_format("%^s mewls in pain.", m_name);
4398 #endif
4399                 else if (percentage > 10)
4400 #ifdef JP
4401 msg_format("%^s¤Ï¶ìÄˤˤ¦¤á¤¤¤¿¡£", m_name);
4402 #else
4403                         msg_format("%^s hisses in agony.", m_name);
4404 #endif
4405                 else
4406 #ifdef JP
4407 msg_format("%s¤Ï°¥¤ì¤ÊÌĤ­À¼¤ò½Ð¤·¤¿¡£", m_name);
4408 #else
4409                         msg_format("%^s mewls pitifully.", m_name);
4410 #endif
4411         }
4412
4413
4414         /* Ants, Centipedes, Flies, Insects, Beetles, Spiders */
4415         else if (strchr("acFIKS", r_ptr->d_char))
4416         {
4417                 if (percentage > 95)
4418 #ifdef JP
4419 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4420 #else
4421                         msg_format("%^s ignores the attack.", m_name);
4422 #endif
4423                 else if (percentage > 75)
4424 #ifdef JP
4425 msg_format("%^s¤Ï¥­¡¼¥­¡¼ÌĤ¤¤¿¡£", m_name);
4426 #else
4427                         msg_format("%^s chitters.", m_name);
4428 #endif
4429
4430                 else if (percentage > 50)
4431 #ifdef JP
4432 msg_format("%^s¤Ï¥è¥í¥è¥íƨ¤²²ó¤Ã¤¿¡£", m_name);
4433 #else
4434                         msg_format("%^s scuttles about.", m_name);
4435 #endif
4436
4437                 else if (percentage > 35)
4438 #ifdef JP
4439 msg_format("%^s¤Ï¤¦¤ë¤µ¤¯ÌĤ¤¤¿¡£", m_name);
4440 #else
4441                         msg_format("%^s twitters.", m_name);
4442 #endif
4443
4444                 else if (percentage > 20)
4445 #ifdef JP
4446 msg_format("%^s¤ÏÄˤߤËáÛÚ»¤·¤¿¡£", m_name);
4447 #else
4448                         msg_format("%^s jerks in pain.", m_name);
4449 #endif
4450
4451                 else if (percentage > 10)
4452 #ifdef JP
4453 msg_format("%^s¤Ï¶ìÄˤÇáÛÚ»¤·¤¿¡£", m_name);
4454 #else
4455                         msg_format("%^s jerks in agony.", m_name);
4456 #endif
4457
4458                 else
4459 #ifdef JP
4460 msg_format("%^s¤Ï¥Ô¥¯¥Ô¥¯¤Ò¤­¤Ä¤Ã¤¿¡£", m_name);
4461 #else
4462                         msg_format("%^s twitches.", m_name);
4463 #endif
4464
4465         }
4466
4467
4468         /* Birds */
4469         else if (strchr("B", r_ptr->d_char))
4470         {
4471                 if (percentage > 95)
4472 #ifdef JP
4473 msg_format("%^s¤Ï¤µ¤¨¤º¤Ã¤¿¡£", m_name);
4474 #else
4475                         msg_format("%^s chirps.", m_name);
4476 #endif
4477
4478                 else if (percentage > 75)
4479 #ifdef JP
4480 msg_format("%^s¤Ï¥Ô¡¼¥Ô¡¼ÌĤ¤¤¿¡£", m_name);
4481 #else
4482                         msg_format("%^s twitters.", m_name);
4483 #endif
4484
4485                 else if (percentage > 50)
4486 #ifdef JP
4487 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ¤¤¿¡£", m_name);
4488 #else
4489                         msg_format("%^s squawks.", m_name);
4490 #endif
4491
4492                 else if (percentage > 35)
4493 #ifdef JP
4494 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4495 #else
4496                         msg_format("%^s chatters.", m_name);
4497 #endif
4498
4499                 else if (percentage > 20)
4500 #ifdef JP
4501 msg_format("%^s¤Ï¶ì¤·¤ó¤À¡£", m_name);
4502 #else
4503                         msg_format("%^s jeers.", m_name);
4504 #endif
4505
4506                 else if (percentage > 10)
4507 #ifdef JP
4508 msg_format("%^s¤Ï¤Î¤¿¤¦¤Á²ó¤Ã¤¿¡£", m_name);
4509 #else
4510                         msg_format("%^s flutters about.", m_name);
4511 #endif
4512
4513                 else
4514 #ifdef JP
4515 msg_format("%^s¤Ï¥­¡¼¥­¡¼¤ÈÌĤ­¶«¤ó¤À¡£", m_name);
4516 #else
4517                         msg_format("%^s squeaks.", m_name);
4518 #endif
4519
4520         }
4521
4522
4523         /* Dragons, Demons, High Undead */
4524         else if (strchr("duDLUW", r_ptr->d_char))
4525         {
4526                 if (percentage > 95)
4527 #ifdef JP
4528 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4529 #else
4530                         msg_format("%^s ignores the attack.", m_name);
4531 #endif
4532
4533                 else if (percentage > 75)
4534 #ifdef JP
4535 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4536 #else
4537                         msg_format("%^s flinches.", m_name);
4538 #endif
4539
4540                 else if (percentage > 50)
4541 #ifdef JP
4542 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4543 #else
4544                         msg_format("%^s hisses in pain.", m_name);
4545 #endif
4546
4547                 else if (percentage > 35)
4548 #ifdef JP
4549 msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4550 #else
4551                         msg_format("%^s snarls with pain.", m_name);
4552 #endif
4553
4554                 else if (percentage > 20)
4555 #ifdef JP
4556 msg_format("%^s¤ÏÄˤߤËËʤ¨¤¿¡£", m_name);
4557 #else
4558                         msg_format("%^s roars with pain.", m_name);
4559 #endif
4560
4561                 else if (percentage > 10)
4562 #ifdef JP
4563 msg_format("%^s¤Ï¶ì¤·¤²¤Ë¶«¤ó¤À¡£", m_name);
4564 #else
4565                         msg_format("%^s gasps.", m_name);
4566 #endif
4567
4568                 else
4569 #ifdef JP
4570 msg_format("%^s¤Ï¼å¡¹¤·¤¯¤¦¤Ê¤Ã¤¿¡£", m_name);
4571 #else
4572                         msg_format("%^s snarls feebly.", m_name);
4573 #endif
4574
4575         }
4576
4577
4578         /* Skeletons */
4579         else if (strchr("s", r_ptr->d_char))
4580         {
4581                 if (percentage > 95)
4582 #ifdef JP
4583 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4584 #else
4585                         msg_format("%^s ignores the attack.", m_name);
4586 #endif
4587
4588                 else if (percentage > 75)
4589 #ifdef JP
4590 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4591 #else
4592                         msg_format("%^s shrugs off the attack.", m_name);
4593 #endif
4594
4595                 else if (percentage > 50)
4596 #ifdef JP
4597 msg_format("%^s¤Ï¥«¥¿¥«¥¿¤È¾Ð¤Ã¤¿¡£", m_name);
4598 #else
4599                         msg_format("%^s rattles.", m_name);
4600 #endif
4601
4602                 else if (percentage > 35)
4603 #ifdef JP
4604 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4605 #else
4606                         msg_format("%^s stumbles.", m_name);
4607 #endif
4608
4609                 else if (percentage > 20)
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 > 10)
4617 #ifdef JP
4618 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4619 #else
4620                         msg_format("%^s staggers.", m_name);
4621 #endif
4622
4623                 else
4624 #ifdef JP
4625 msg_format("%^s¤Ï¥¬¥¿¥¬¥¿¸À¤Ã¤¿¡£", m_name);
4626 #else
4627                         msg_format("%^s clatters.", m_name);
4628 #endif
4629
4630         }
4631
4632
4633         /* Zombies */
4634         else if (strchr("z", r_ptr->d_char))
4635         {
4636                 if (percentage > 95)
4637 #ifdef JP
4638 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4639 #else
4640                         msg_format("%^s ignores the attack.", m_name);
4641 #endif
4642
4643                 else if (percentage > 75)
4644 #ifdef JP
4645 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4646 #else
4647                         msg_format("%^s shrugs off the attack.", m_name);
4648 #endif
4649
4650                 else if (percentage > 50)
4651 #ifdef JP
4652 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4653 #else
4654                         msg_format("%^s groans.", m_name);
4655 #endif
4656
4657                 else if (percentage > 35)
4658 #ifdef JP
4659 msg_format("%s¤Ï¶ì¤·¤²¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4660 #else
4661                         msg_format("%^s moans.", m_name);
4662 #endif
4663
4664                 else if (percentage > 20)
4665 #ifdef JP
4666 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4667 #else
4668                         msg_format("%^s hesitates.", m_name);
4669 #endif
4670
4671                 else if (percentage > 10)
4672 #ifdef JP
4673 msg_format("%^s¤Ï¤¦¤Ê¤Ã¤¿¡£", m_name);
4674 #else
4675                         msg_format("%^s grunts.", m_name);
4676 #endif
4677
4678                 else
4679 #ifdef JP
4680 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4681 #else
4682                         msg_format("%^s staggers.", m_name);
4683 #endif
4684
4685         }
4686
4687
4688         /* Ghosts */
4689         else if (strchr("G", r_ptr->d_char))
4690
4691         {
4692                 if (percentage > 95)
4693 #ifdef JP
4694 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4695 #else
4696                         msg_format("%^s ignores the attack.", m_name);
4697 #endif
4698
4699                 else if (percentage > 75)
4700 #ifdef JP
4701 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4702 #else
4703                         msg_format("%^s shrugs off the attack.", m_name);
4704 #endif
4705
4706                 else if (percentage > 50)
4707 #ifdef JP
4708 msg_format("%s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4709 #else
4710                         msg_format("%^s moans.", m_name);
4711 #endif
4712
4713                 else if (percentage > 35)
4714 #ifdef JP
4715 msg_format("%^s¤Ïµã¤­¤ï¤á¤¤¤¿¡£", m_name);
4716 #else
4717                         msg_format("%^s wails.", m_name);
4718 #endif
4719
4720                 else if (percentage > 20)
4721 #ifdef JP
4722 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4723 #else
4724                         msg_format("%^s howls.", m_name);
4725 #endif
4726
4727                 else if (percentage > 10)
4728 #ifdef JP
4729 msg_format("%s¤Ï¼å¡¹¤·¤¯¤¦¤á¤¤¤¿¡£", m_name);
4730 #else
4731                         msg_format("%^s moans softly.", m_name);
4732 #endif
4733
4734                 else
4735 #ifdef JP
4736 msg_format("%^s¤Ï¤«¤¹¤«¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4737 #else
4738                         msg_format("%^s sighs.", m_name);
4739 #endif
4740
4741         }
4742
4743
4744         /* Dogs and Hounds */
4745         else if (strchr("CZ", r_ptr->d_char))
4746         {
4747 #ifdef JP
4748                 if (percentage > 95)
4749                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4750                 else if (percentage > 75)
4751                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4752                 else if (percentage > 50)
4753                         msg_format("%^s¤ÏÄˤߤǥ­¥ã¥ó¥­¥ã¥óËʤ¨¤¿¡£", m_name);
4754                 else if (percentage > 35)
4755                         msg_format("%^s¤ÏÄˤߤÇÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4756                 else if (percentage > 20)
4757                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4758                 else if (percentage > 10)
4759                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4760                 else
4761                         msg_format("%^s¤Ï¼å¡¹¤·¤¯Ëʤ¨¤¿¡£", m_name);
4762 #else
4763                 if (percentage > 95)
4764                         msg_format("%^s shrugs off the attack.", m_name);
4765                 else if (percentage > 75)
4766                         msg_format("%^s snarls with pain.", m_name);
4767                 else if (percentage > 50)
4768                         msg_format("%^s yelps in pain.", m_name);
4769                 else if (percentage > 35)
4770                         msg_format("%^s howls in pain.", m_name);
4771                 else if (percentage > 20)
4772                         msg_format("%^s howls in agony.", m_name);
4773                 else if (percentage > 10)
4774                         msg_format("%^s writhes in agony.", m_name);
4775                 else
4776                         msg_format("%^s yelps feebly.", m_name);
4777 #endif
4778
4779         }
4780
4781         /* One type of monsters (ignore,squeal,shriek) */
4782         else if (strchr("Xbilqrt", r_ptr->d_char))
4783         {
4784 #ifdef JP
4785                 if (percentage > 95)
4786                         msg_format("%^s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4787                 else if (percentage > 75)
4788                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4789                 else if (percentage > 50)
4790                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4791                 else if (percentage > 35)
4792                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4793                 else if (percentage > 20)
4794                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4795                 else if (percentage > 10)
4796                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4797                 else
4798                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4799 #else
4800                 if (percentage > 95)
4801                         msg_format("%^s ignores the attack.", m_name);
4802                 else if (percentage > 75)
4803                         msg_format("%^s grunts with pain.", m_name);
4804                 else if (percentage > 50)
4805                         msg_format("%^s squeals in pain.", m_name);
4806                 else if (percentage > 35)
4807                         msg_format("%^s shrieks in pain.", m_name);
4808                 else if (percentage > 20)
4809                         msg_format("%^s shrieks in agony.", m_name);
4810                 else if (percentage > 10)
4811                         msg_format("%^s writhes in agony.", m_name);
4812                 else
4813                         msg_format("%^s cries out feebly.", m_name);
4814 #endif
4815
4816         }
4817
4818         /* Another type of monsters (shrug,cry,scream) */
4819         else
4820         {
4821 #ifdef JP
4822                 if (percentage > 95)
4823                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4824                 else if (percentage > 75)
4825                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4826                 else if (percentage > 50)
4827                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4828                 else if (percentage > 35)
4829                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4830                 else if (percentage > 20)
4831                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4832                 else if (percentage > 10)
4833                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4834                 else
4835                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4836 #else
4837                 if (percentage > 95)
4838                         msg_format("%^s shrugs off the attack.", m_name);
4839                 else if (percentage > 75)
4840                         msg_format("%^s grunts with pain.", m_name);
4841                 else if (percentage > 50)
4842                         msg_format("%^s cries out in pain.", m_name);
4843                 else if (percentage > 35)
4844                         msg_format("%^s screams in pain.", m_name);
4845                 else if (percentage > 20)
4846                         msg_format("%^s screams in agony.", m_name);
4847                 else if (percentage > 10)
4848                         msg_format("%^s writhes in agony.", m_name);
4849                 else
4850                         msg_format("%^s cries out feebly.", m_name);
4851 #endif
4852
4853         }
4854 }
4855
4856
4857 /*
4858  * Learn about an "observed" resistance.
4859  */
4860 void update_smart_learn(int m_idx, int what)
4861 {
4862 #ifdef DRS_SMART_OPTIONS
4863
4864         monster_type *m_ptr = &m_list[m_idx];
4865
4866         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4867
4868
4869         /* Not allowed to learn */
4870         if (!smart_learn) return;
4871
4872         /* Too stupid to learn anything */
4873         if (r_ptr->flags2 & (RF2_STUPID)) return;
4874
4875         /* Not intelligent, only learn sometimes */
4876         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
4877
4878
4879         /* XXX XXX XXX */
4880
4881         /* Analyze the knowledge */
4882         switch (what)
4883         {
4884         case DRS_ACID:
4885                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
4886                 if (IS_OPPOSE_ACID()) m_ptr->smart |= (SM_OPP_ACID);
4887                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
4888                 break;
4889
4890         case DRS_ELEC:
4891                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
4892                 if (IS_OPPOSE_ELEC()) m_ptr->smart |= (SM_OPP_ELEC);
4893                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
4894                 break;
4895
4896         case DRS_FIRE:
4897                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
4898                 if (IS_OPPOSE_FIRE()) m_ptr->smart |= (SM_OPP_FIRE);
4899                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
4900                 break;
4901
4902         case DRS_COLD:
4903                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
4904                 if (IS_OPPOSE_COLD()) m_ptr->smart |= (SM_OPP_COLD);
4905                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
4906                 break;
4907
4908         case DRS_POIS:
4909                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
4910                 if (IS_OPPOSE_POIS()) m_ptr->smart |= (SM_OPP_POIS);
4911                 break;
4912
4913
4914         case DRS_NETH:
4915                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
4916                 break;
4917
4918         case DRS_LITE:
4919                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
4920                 break;
4921
4922         case DRS_DARK:
4923                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
4924                 break;
4925
4926         case DRS_FEAR:
4927                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
4928                 break;
4929
4930         case DRS_CONF:
4931                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
4932                 break;
4933
4934         case DRS_CHAOS:
4935                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4936                 break;
4937
4938         case DRS_DISEN:
4939                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4940                 break;
4941
4942         case DRS_BLIND:
4943                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4944                 break;
4945
4946         case DRS_NEXUS:
4947                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4948                 break;
4949
4950         case DRS_SOUND:
4951                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4952                 break;
4953
4954         case DRS_SHARD:
4955                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4956                 break;
4957
4958         case DRS_FREE:
4959                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4960                 break;
4961
4962         case DRS_MANA:
4963                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4964                 break;
4965
4966         case DRS_REFLECT:
4967                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
4968                 break;
4969         }
4970
4971 #endif /* DRS_SMART_OPTIONS */
4972
4973 }
4974
4975
4976 /*
4977  * Place the player in the dungeon XXX XXX
4978  */
4979 bool player_place(int y, int x)
4980 {
4981         /* Paranoia XXX XXX */
4982         if (cave[y][x].m_idx != 0) return FALSE;
4983
4984         /* Save player location */
4985         py = y;
4986         px = x;
4987
4988         /* Success */
4989         return TRUE;
4990 }
4991
4992
4993 /*
4994  * Drop all items carried by a monster
4995  */
4996 void monster_drop_carried_objects(monster_type *m_ptr)
4997 {
4998         s16b this_o_idx, next_o_idx = 0;
4999         object_type forge;
5000         object_type *o_ptr;
5001         object_type *q_ptr;
5002
5003
5004         /* Drop objects being carried */
5005         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
5006         {
5007                 /* Acquire object */
5008                 o_ptr = &o_list[this_o_idx];
5009
5010                 /* Acquire next object */
5011                 next_o_idx = o_ptr->next_o_idx;
5012
5013                 /* Paranoia */
5014                 o_ptr->held_m_idx = 0;
5015
5016                 /* Get local object */
5017                 q_ptr = &forge;
5018
5019                 /* Copy the object */
5020                 object_copy(q_ptr, o_ptr);
5021
5022                 /* Delete the object */
5023                 delete_object_idx(this_o_idx);
5024
5025                 /* Drop it */
5026                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
5027         }
5028
5029         /* Forget objects */
5030         m_ptr->hold_o_idx = 0;
5031 }