OSDN Git Service

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