OSDN Git Service

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