OSDN Git Service

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