OSDN Git Service

スピードシステム改造。
[hengbandforosx/hengbandosx.git] / src / monster2.c
1 /* File: monster2.c */
2
3 /* Purpose: misc code for monsters */
4
5 /*
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14
15 #define HORDE_NOGOOD 0x01
16 #define HORDE_NOEVIL 0x02
17
18 bool is_kage = FALSE;
19 u16b horde_align = 0;
20
21 cptr horror_desc[MAX_SAN_HORROR] =
22 {
23 #ifdef JP
24         "´÷¤Þ¤ï¤·¤¤",
25         "ÄìÃΤì¤Ì",
26         "¤¾¤Ã¤È¤¹¤ë",
27         "ÇËÌÇŪ¤Ê",
28         "ËÁÆÂŪ¤Ê",
29
30         "¤¤¤ä¤Ê",
31         "¶²¤í¤·¤¤",
32         "ÉÔ·é¤Ê",
33         "ÍƼϤΤʤ¤",
34         "¤ª¤¾¤Þ¤·¤¤",
35
36         "ÃϹö¤Î",
37         "¿È¤ÎÌÓ¤â¤è¤À¤Ä",
38         "ÃϹö¤Î",
39         "´÷¤Þ¤ï¤·¤¤",
40         "°­Ì´¤Î¤è¤¦¤Ê",
41
42         "·ù°­¤ò´¶¤¸¤ë",
43         "ȳÅö¤¿¤ê¤Ê",
44         "¶²¤¤",
45         "ÉÔ¾ô¤Ê",
46         "¸À¤¦¤â¤ª¤¾¤Þ¤·¤¤",
47 #else
48         "abominable",
49         "abysmal",
50         "appalling",
51         "baleful",
52         "blasphemous",
53
54         "disgusting",
55         "dreadful",
56         "filthy",
57         "grisly",
58         "hideous",
59
60         "hellish",
61         "horrible",
62         "infernal",
63         "loathsome",
64         "nightmarish",
65
66         "repulsive",
67         "sacrilegious",
68         "terrible",
69         "unclean",
70         "unspeakable",
71 #endif
72
73 };
74
75 cptr funny_desc[MAX_SAN_FUNNY] =
76 {
77 #ifdef JP
78         "´ÖÈ´¤±¤Ê",
79         "³ê·Î¤Ê",
80         "¤Ð¤«¤é¤·¤¤",
81         "̵̣´¥Áç¤Ê",
82         "Çϼ¯¤²¤¿",
83
84         "¾Ð¤¨¤ë",
85         "¤Ð¤«¤Ð¤«¤·¤¤",
86         "¤Ö¤Ã¤È¤ó¤À",
87         "¤¤¤«¤·¤¿",
88         "¥Ý¥¹¥È¥â¥À¥ó¤Ê",
89
90         "¥Õ¥¡¥ó¥¿¥¹¥Æ¥£¥Ã¥¯¤Ê",
91         "¥À¥À¥¤¥º¥àŪ¤Ê",
92         "¥­¥å¥Ó¥º¥àŪ¤Ê",
93         "±§ÃèŪ¤Ê",
94         "Âî±Û¤·¤¿",
95
96         "Íý²òÉÔǽ¤Ê",
97         "¤â¤Î¤¹¤´¤¤",
98         "¶Ã¤¯¤Ù¤­",
99         "¿®¤¸¤é¤ì¤Ê¤¤",
100         "¥«¥ª¥Æ¥£¥Ã¥¯¤Ê",
101
102         "ÌîÀ­Åª¤Ê",
103         "Èó¾ï¼±¤Ê",
104 #else
105         "silly",
106         "hilarious",
107         "absurd",
108         "insipid",
109         "ridiculous",
110
111         "laughable",
112         "ludicrous",
113         "far-out",
114         "groovy",
115         "postmodern",
116
117         "fantastic",
118         "dadaistic",
119         "cubistic",
120         "cosmic",
121         "awesome",
122
123         "incomprehensible",
124         "fabulous",
125         "amazing",
126         "incredible",
127         "chaotic",
128
129         "wild",
130         "preposterous",
131 #endif
132
133 };
134
135 cptr funny_comments[MAX_SAN_COMMENT] =
136 {
137 #ifdef JP
138   /* nuke me */
139         "ºÇ¹â¤À¤¼¡ª",
140         "¤¦¤Ò¤ç¡¼¡ª",
141         "¤¤¤«¤¹¤¼¡ª",
142         "¤¹¤ó¤Ð¤é¤·¤¤¡ª",
143         "¤Ö¤Ã¤È¤Ó¡¼¡ª"
144 #else
145         "Wow, cosmic, man!",
146         "Rad!",
147         "Groovy!",
148         "Cool!",
149         "Far out!"
150 #endif
151
152 };
153
154
155 /*
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 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                         c_ptr->info &= ~(CAVE_MASK);
2779                         c_ptr->info |= CAVE_FLOOR;
2780
2781                         /* Notice */
2782                         note_spot(y, x);
2783                         rune_dam = 1000;
2784                 }
2785                 else return FALSE;
2786         }
2787
2788         /* Powerful monster */
2789         if (r_ptr->level > dun_level)
2790         {
2791                 /* Unique monsters */
2792                 if (r_ptr->flags1 & (RF1_UNIQUE))
2793                 {
2794                         /* Message for cheaters */
2795 #ifdef JP
2796                         if (cheat_hear) msg_format("¿¼ÁؤΥæ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
2797 #else
2798                         if (cheat_hear) msg_format("Deep Unique (%s).", name);
2799 #endif
2800
2801
2802                         /* Boost rating by twice delta-depth */
2803                         rating += (r_ptr->level - dun_level) * 2;
2804                 }
2805
2806                 /* Normal monsters */
2807                 else
2808                 {
2809                         /* Message for cheaters */
2810 #ifdef JP
2811                         if (cheat_hear) msg_format("¿¼ÁؤΥâ¥ó¥¹¥¿¡¼ (%s)¡£", name);
2812 #else
2813                         if (cheat_hear) msg_format("Deep Monster (%s).", name);
2814 #endif
2815
2816
2817                         /* Boost rating by delta-depth */
2818                         rating += (r_ptr->level - dun_level);
2819                 }
2820         }
2821
2822         /* Note the monster */
2823         else if (r_ptr->flags1 & (RF1_UNIQUE))
2824         {
2825                 /* Unique monsters induce message */
2826 #ifdef JP
2827                 if (cheat_hear) msg_format("¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼ (%s)¡£", name);
2828 #else
2829                 if (cheat_hear) msg_format("Unique (%s).", name);
2830 #endif
2831
2832         }
2833
2834         if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7) || (r_ptr->level < 10)) is_kage = FALSE;
2835
2836         /* Make a new monster */
2837         c_ptr->m_idx = m_pop();
2838         hack_m_idx_ii = c_ptr->m_idx;
2839
2840         /* Mega-Hack -- catch "failure" */
2841         if (!c_ptr->m_idx) return (FALSE);
2842
2843
2844         /* Get a new monster record */
2845         m_ptr = &m_list[c_ptr->m_idx];
2846
2847         /* Save the race */
2848         m_ptr->r_idx = r_idx;
2849         m_ptr->ap_r_idx = initial_r_appearance(r_idx);
2850
2851         /* Place the monster at the location */
2852         m_ptr->fy = y;
2853         m_ptr->fx = x;
2854
2855
2856         /* No "damage" yet */
2857         m_ptr->stunned = 0;
2858         m_ptr->confused = 0;
2859         m_ptr->monfear = 0;
2860
2861         /* Unknown distance */
2862         m_ptr->cdis = 0;
2863
2864         reset_target(m_ptr);
2865
2866         m_ptr->nickname = 0;
2867
2868         m_ptr->exp = 0;
2869
2870         /* No flags */
2871         m_ptr->mflag = 0;
2872         m_ptr->mflag2 = 0;
2873
2874         if (r_ptr->flags7 & RF7_CHAMELEON)
2875         {
2876                 choose_new_monster(c_ptr->m_idx, TRUE, 0);
2877                 r_ptr = &r_info[m_ptr->r_idx];
2878                 m_ptr->mflag2 |= MFLAG_CHAMELEON;
2879                 rating++;
2880         }
2881         else if (is_kage)
2882         {
2883                 m_ptr->ap_r_idx = MON_KAGE;
2884                 m_ptr->mflag2 |= MFLAG_KAGE;
2885         }
2886
2887         if (no_pet) m_ptr->mflag2 |= MFLAG_NOPET;
2888
2889         /* Not visible */
2890         m_ptr->ml = FALSE;
2891
2892         /* Pet? */
2893         if (pet)
2894         {
2895                 set_pet(m_ptr);
2896         }
2897         /* Friendly? */
2898         else if ((friendly || (r_ptr->flags7 & RF7_FRIENDLY)) &&
2899                  !(p_ptr->align >= 0 && (r_ptr->flags3 & RF3_EVIL)) &&
2900                  !(p_ptr->align < 0 && (r_ptr->flags3 & RF3_GOOD)))
2901         {
2902                 set_friendly(m_ptr);
2903         }
2904
2905         /* Assume no sleeping */
2906         m_ptr->csleep = 0;
2907
2908         /* Enforce sleeping if needed */
2909         if (slp && r_ptr->sleep && !ironman_nightmare)
2910         {
2911                 int val = r_ptr->sleep;
2912                 m_ptr->csleep = ((val * 2) + randint1(val * 10));
2913         }
2914
2915         /* Assign maximal hitpoints */
2916         if (r_ptr->flags1 & RF1_FORCE_MAXHP)
2917         {
2918                 m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
2919         }
2920         else
2921         {
2922                 m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
2923         }
2924
2925         /* Monsters have double hitpoints in Nightmare mode */
2926         if (ironman_nightmare)
2927         {
2928                 u32b hp = m_ptr->max_maxhp * 2L;
2929
2930                 m_ptr->max_maxhp = (s16b)MIN(30000, hp);
2931         }
2932
2933         m_ptr->maxhp = m_ptr->max_maxhp;
2934
2935         /* And start out fully healthy */
2936         if (m_ptr->r_idx == MON_WOUNDED_BEAR)
2937                 m_ptr->hp = m_ptr->maxhp / 2;
2938         else m_ptr->hp = m_ptr->maxhp;
2939
2940
2941         /* Extract the monster base speed */
2942         m_ptr->mspeed = r_ptr->speed;
2943
2944         /* Hack -- small racial variety */
2945         if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
2946         {
2947                 /* Allow some small variation per monster */
2948           if(one_in_(4)){
2949                 i = extract_energy[r_ptr->speed] / 3;
2950                 if (i) m_ptr->mspeed += rand_spread(0, i);
2951           }
2952           else{
2953                 i = extract_energy[r_ptr->speed] / 10;
2954                 if (i) m_ptr->mspeed += rand_spread(0, i);
2955           }
2956         }
2957
2958         if (m_ptr->mspeed > 199) m_ptr->mspeed = 199;
2959
2960         /* Give a random starting energy */
2961         if (!ironman_nightmare)
2962         {
2963                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100);
2964         }
2965         else
2966         {
2967                 /* Nightmare monsters are more prepared */
2968                 m_ptr->energy_need = ENERGY_NEED() - (s16b)randint0(100) * 2;
2969         }
2970
2971         /* Force monster to wait for player, unless in Nightmare mode */
2972         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
2973         {
2974                 /* Monster is still being nice */
2975                 m_ptr->mflag |= (MFLAG_NICE);
2976
2977                 /* Must repair monsters */
2978                 repair_monsters = TRUE;
2979         }
2980
2981         /* Hack -- see "process_monsters()" */
2982         if (c_ptr->m_idx < hack_m_idx)
2983         {
2984                 /* Monster is still being born */
2985                 m_ptr->mflag |= (MFLAG_BORN);
2986         }
2987
2988
2989         /* Update the monster */
2990         update_mon(c_ptr->m_idx, TRUE);
2991
2992
2993         /* Hack -- Count the monsters on the level */
2994         if (m_ptr->mflag2 & MFLAG_CHAMELEON)
2995                 r_info[r_idx].cur_num++;
2996         else
2997                 r_ptr->cur_num++;
2998
2999
3000         /* Hack -- Count the number of "reproducers" */
3001         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
3002
3003
3004         /* Hack -- Notice new multi-hued monsters */
3005         if (r_ptr->flags1 & RF1_ATTR_MULTI) shimmer_monsters = TRUE;
3006
3007         if (p_ptr->warning && character_dungeon)
3008         {
3009                 if (r_ptr->flags1 & RF1_UNIQUE)
3010                 {
3011                         cptr color;
3012                         object_type *o_ptr;
3013                         char o_name[MAX_NLEN];
3014
3015                         if (r_ptr->level > p_ptr->lev + 30)
3016 #ifdef JP
3017                                 color = "¹õ¤¯";
3018 #else
3019                                 color = "black";
3020 #endif
3021                         else if (r_ptr->level > p_ptr->lev + 15)
3022 #ifdef JP
3023                                 color = "»ç¿§¤Ë";
3024 #else
3025                                 color = "perple";
3026 #endif
3027                         else if (r_ptr->level > p_ptr->lev + 5)
3028 #ifdef JP
3029                                 color = "¥ë¥Ó¡¼¿§¤Ë";
3030 #else
3031                                 color = "deep red";
3032 #endif
3033                         else if (r_ptr->level > p_ptr->lev - 5)
3034 #ifdef JP
3035                                 color = "ÀÖ¤¯";
3036 #else
3037                                 color = "red";
3038 #endif
3039                         else if (r_ptr->level > p_ptr->lev - 15)
3040 #ifdef JP
3041                                 color = "¥Ô¥ó¥¯¿§¤Ë";
3042 #else
3043                                 color = "pink";
3044 #endif
3045                         else
3046 #ifdef JP
3047                                 color = "Çò¤¯";
3048 #else
3049                                 color = "white";
3050 #endif
3051
3052                         o_ptr = choose_warning_item();
3053                         object_desc(o_name, o_ptr, FALSE, 0);
3054 #ifdef JP
3055                         msg_format("%s¤Ï%s¸÷¤Ã¤¿¡£",o_name, color);
3056 #else
3057                         msg_format("%s glows %s.",o_name, color);
3058 #endif
3059                 }
3060         }
3061
3062         if (c_ptr->feat == FEAT_MINOR_GLYPH)
3063         {
3064                 /* Break the ward */
3065                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3066                 {
3067                         /* Describe observable breakage */
3068                         if (c_ptr->info & CAVE_MARK)
3069                         {
3070 #ifdef JP
3071 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3072 #else
3073                                 msg_print("The rune explodes!");
3074 #endif
3075
3076                                 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);
3077                         }
3078                 }
3079                 else
3080                 {
3081 #ifdef JP
3082 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3083 #else
3084                         msg_print("An explosive rune was disarmed.");
3085 #endif
3086                 }
3087
3088                 /* Forget the rune */
3089                 c_ptr->info &= ~(CAVE_MARK);
3090
3091                 /* Break the rune */
3092                 c_ptr->feat = floor_type[randint0(100)];
3093                 c_ptr->info &= ~(CAVE_MASK);
3094                 c_ptr->info |= CAVE_FLOOR;
3095                 note_spot(y, x);
3096                 lite_spot(y, x);
3097         }
3098
3099         /* Success */
3100         return (TRUE);
3101 }
3102
3103
3104 /*
3105  *  improved version of scatter() for place monster
3106  */
3107
3108 #define MON_SCAT_MAXD 10
3109
3110 static bool mon_scatter(int *yp, int *xp, int y, int x, int max_dist)
3111 {
3112         int place_x[MON_SCAT_MAXD];
3113         int place_y[MON_SCAT_MAXD];
3114         int num[MON_SCAT_MAXD];
3115         int i;
3116         int nx, ny;
3117
3118         if (max_dist >= MON_SCAT_MAXD)
3119                 return FALSE;
3120
3121         for (i = 0; i < MON_SCAT_MAXD; i++)
3122                 num[i] = 0;
3123
3124         for (nx = x - max_dist; nx <= x + max_dist; nx++)
3125                 for (ny = y - max_dist; ny <= y + max_dist; ny++)
3126                 {
3127                         /* Ignore annoying locations */
3128                         if (!in_bounds(ny, nx)) continue;
3129                         
3130                         /* Require "line of sight" */
3131                         if (!los(y, x, ny, nx)) continue;
3132                         
3133                         /* Walls and Monsters block flow */
3134                         if (!cave_empty_bold2(ny, nx)) continue;
3135                         if (cave[ny][nx].m_idx) continue;
3136                         if ((ny == py) && (nx == px)) continue;
3137                         
3138 #if 0
3139                         /* Hack -- no summon on glyph of warding */
3140                         if (cave[ny][nx].feat == FEAT_GLYPH) continue;
3141                         if (cave[ny][nx].feat == FEAT_MINOR_GLYPH) continue;
3142 #endif
3143                         
3144                         /* ... nor on the Pattern */
3145                         if ((cave[ny][nx].feat >= FEAT_PATTERN_START) &&
3146                             (cave[ny][nx].feat <= FEAT_PATTERN_XTRA2))
3147                                 continue;
3148                         
3149                         i = distance(y, x, ny, nx);
3150
3151                         if (i > max_dist)
3152                                 continue;
3153                         
3154                         num[i]++;
3155
3156                         /* random swap */
3157                         if(one_in_(num[i]))
3158                         {
3159                                 place_x[i] = nx;
3160                                 place_y[i] = ny;
3161                         }
3162                         
3163                 }
3164
3165         i = 0;
3166         while (i < MON_SCAT_MAXD && 0 == num[i])
3167                 i++;
3168         if (i >= MON_SCAT_MAXD)
3169                 return FALSE;
3170
3171         *xp = place_x[i];
3172         *yp = place_y[i];
3173
3174         return TRUE;
3175 }
3176
3177
3178 /*
3179  * Maximum size of a group of monsters
3180  */
3181 #define GROUP_MAX       32
3182
3183
3184 /*
3185  * Attempt to place a "group" of monsters around the given location
3186  */
3187 static bool place_monster_group(int y, int x, int r_idx, bool slp, bool friendly, bool pet, bool no_pet)
3188 {
3189         monster_race *r_ptr = &r_info[r_idx];
3190
3191         int old, n, i;
3192         int total = 0, extra = 0;
3193
3194         int hack_n = 0;
3195
3196         byte hack_y[GROUP_MAX];
3197         byte hack_x[GROUP_MAX];
3198
3199
3200         /* Pick a group size */
3201         total = randint1(10);
3202
3203         /* Hard monsters, small groups */
3204         if (r_ptr->level > dun_level)
3205         {
3206                 extra = r_ptr->level - dun_level;
3207                 extra = 0 - randint1(extra);
3208         }
3209
3210         /* Easy monsters, large groups */
3211         else if (r_ptr->level < dun_level)
3212         {
3213                 extra = dun_level - r_ptr->level;
3214                 extra = randint1(extra);
3215         }
3216
3217         /* Hack -- limit group reduction */
3218         if (extra > 9) extra = 9;
3219
3220         /* Modify the group size */
3221         total += extra;
3222
3223         /* Minimum size */
3224         if (total < 1) total = 1;
3225
3226         /* Maximum size */
3227         if (total > GROUP_MAX) total = GROUP_MAX;
3228
3229
3230         /* Save the rating */
3231         old = rating;
3232
3233         /* Start on the monster */
3234         hack_n = 1;
3235         hack_x[0] = x;
3236         hack_y[0] = y;
3237
3238         /* Puddle monsters, breadth first, up to total */
3239         for (n = 0; (n < hack_n) && (hack_n < total); n++)
3240         {
3241                 /* Grab the location */
3242                 int hx = hack_x[n];
3243                 int hy = hack_y[n];
3244
3245                 /* Check each direction, up to total */
3246                 for (i = 0; (i < 8) && (hack_n < total); i++)
3247                 {
3248                         int mx, my;
3249
3250                         scatter(&my, &mx, hy, hx, 4, 0);
3251
3252                         /* Walls and Monsters block flow */
3253                         if (!cave_empty_bold2(my, mx)) continue;
3254
3255                         /* Attempt to place another monster */
3256                         if (place_monster_one(my, mx, r_idx, slp, friendly, pet, no_pet))
3257                         {
3258                                 /* Add it to the "hack" set */
3259                                 hack_y[hack_n] = my;
3260                                 hack_x[hack_n] = mx;
3261                                 hack_n++;
3262                         }
3263                 }
3264         }
3265
3266         /* Hack -- restore the rating */
3267         rating = old;
3268
3269
3270         /* Success */
3271         return (TRUE);
3272 }
3273
3274
3275 /*
3276  * Hack -- help pick an escort type
3277  */
3278 static int place_monster_idx = 0;
3279
3280 /*
3281  * Hack -- help pick an escort type
3282  */
3283 static bool place_monster_okay(int r_idx)
3284 {
3285         monster_race *r_ptr = &r_info[place_monster_idx];
3286
3287         monster_race *z_ptr = &r_info[r_idx];
3288
3289         /* Hack - Escorts have to have the same dungeon flag */
3290         if (monster_dungeon(place_monster_idx) != monster_dungeon(r_idx)) return (FALSE);
3291
3292         /* Require similar "race" */
3293         if (z_ptr->d_char != r_ptr->d_char) return (FALSE);
3294
3295         /* Skip more advanced monsters */
3296         if (z_ptr->level > r_ptr->level) return (FALSE);
3297
3298         /* Skip unique monsters */
3299         if (z_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3300
3301         /* Paranoia -- Skip identical monsters */
3302         if (place_monster_idx == r_idx) return (FALSE);
3303
3304         /* Skip different alignment */
3305         if (((r_ptr->flags3 & RF3_EVIL) && (z_ptr->flags3 & RF3_GOOD)) ||
3306             ((r_ptr->flags3 & RF3_GOOD) && (z_ptr->flags3 & RF3_EVIL)))
3307                 return FALSE;
3308
3309         if (r_ptr->flags7 & RF7_FRIENDLY)
3310         {
3311                 if (((p_ptr->align < 0) && (z_ptr->flags3 & RF3_GOOD)) ||
3312                     ((p_ptr->align > 0) && (z_ptr->flags3 & RF3_EVIL)))
3313                         return FALSE;
3314         }
3315
3316         if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
3317                 return FALSE;
3318
3319         /* Okay */
3320         return (TRUE);
3321 }
3322
3323
3324 /*
3325  * Attempt to place a monster of the given race at the given location
3326  *
3327  * Note that certain monsters are now marked as requiring "friends".
3328  * These monsters, if successfully placed, and if the "grp" parameter
3329  * is TRUE, will be surrounded by a "group" of identical monsters.
3330  *
3331  * Note that certain monsters are now marked as requiring an "escort",
3332  * which is a collection of monsters with similar "race" but lower level.
3333  *
3334  * Some monsters induce a fake "group" flag on their escorts.
3335  *
3336  * Note the "bizarre" use of non-recursion to prevent annoying output
3337  * when running a code profiler.
3338  *
3339  * Note the use of the new "monster allocation table" code to restrict
3340  * the "get_mon_num()" function to "legal" escort types.
3341  */
3342 bool place_monster_aux(int y, int x, int r_idx, bool slp, bool grp, bool friendly, bool pet, bool no_kage, bool no_pet)
3343 {
3344         int             i;
3345         monster_race    *r_ptr = &r_info[r_idx];
3346
3347         if (one_in_(333) && !no_kage && !pet) is_kage = TRUE;
3348         else is_kage = FALSE;
3349
3350         /* Place one monster, or fail */
3351         if (!place_monster_one(y, x, r_idx, slp, friendly, pet, no_pet)) return (FALSE);
3352
3353
3354         /* Require the "group" flag */
3355         if (!grp) return (TRUE);
3356
3357
3358         /* Friends for certain monsters */
3359         if (r_ptr->flags1 & (RF1_FRIENDS))
3360         {
3361                 /* Attempt to place a group */
3362                 (void)place_monster_group(y, x, r_idx, slp, friendly, pet, no_pet);
3363         }
3364
3365
3366         /* Escorts for certain monsters */
3367         if (r_ptr->flags1 & (RF1_ESCORT))
3368         {
3369                 /* Set the escort index */
3370                 place_monster_idx = r_idx;
3371
3372                 /* Try to place several "escorts" */
3373                 for (i = 0; i < 32; i++)
3374                 {
3375                         int nx, ny, z, d = 3;
3376
3377                         /* Pick a location */
3378                         scatter(&ny, &nx, y, x, d, 0);
3379
3380                         /* Require empty grids */
3381                         if (!cave_empty_bold2(ny, nx)) continue;
3382
3383                         /* Prepare allocation table */
3384                         get_mon_num_prep(place_monster_okay, get_monster_hook2(ny, nx));
3385
3386                         /* Pick a random race */
3387                         z = get_mon_num(r_ptr->level);
3388
3389                         /* Handle failure */
3390                         if (!z) break;
3391
3392                         /* Place a single escort */
3393                         (void)place_monster_one(ny, nx, z, slp, friendly, pet, no_pet);
3394
3395                         /* Place a "group" of escorts if needed */
3396                         if ((r_info[z].flags1 & RF1_FRIENDS) ||
3397                             (r_ptr->flags1 & RF1_ESCORTS))
3398                         {
3399                                 /* Place a group of monsters */
3400                                 (void)place_monster_group(ny, nx, z, slp, friendly, pet, no_pet);
3401                         }
3402                 }
3403         }
3404
3405         /* Success */
3406         return (TRUE);
3407 }
3408
3409
3410 /*
3411  * Hack -- attempt to place a monster at the given location
3412  *
3413  * Attempt to find a monster appropriate to the "monster_level"
3414  */
3415 bool place_monster(int y, int x, bool slp, bool grp)
3416 {
3417         int r_idx;
3418
3419         /* Prepare allocation table */
3420         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3421
3422         /* Pick a monster */
3423         r_idx = get_mon_num(monster_level);
3424
3425         /* Handle failure */
3426         if (!r_idx) return (FALSE);
3427
3428         /* Attempt to place the monster */
3429         if (place_monster_aux(y, x, r_idx, slp, grp, FALSE, FALSE, FALSE, FALSE)) return (TRUE);
3430
3431         /* Oops */
3432         return (FALSE);
3433 }
3434
3435
3436 #ifdef MONSTER_HORDES
3437
3438 bool alloc_horde(int y, int x)
3439 {
3440         monster_race *r_ptr = NULL;
3441         int r_idx = 0;
3442         int m_idx;
3443         int attempts = 1000;
3444         int cy = y;
3445         int cx = x;
3446
3447         /* Prepare allocation table */
3448         get_mon_num_prep(get_monster_hook(), get_monster_hook2(y, x));
3449
3450         while (--attempts)
3451         {
3452                 /* Pick a monster */
3453                 r_idx = get_mon_num(monster_level);
3454
3455                 /* Handle failure */
3456                 if (!r_idx) return (FALSE);
3457
3458                 r_ptr = &r_info[r_idx];
3459
3460                 if (r_ptr->flags1 & RF1_UNIQUE) continue;
3461
3462                 if (r_idx == MON_HAGURE) continue;
3463                 break;
3464         }
3465         if (attempts < 1) return FALSE;
3466
3467         if (r_ptr->flags3 & RF3_GOOD) horde_align |= HORDE_NOEVIL;
3468         if (r_ptr->flags3 & RF3_EVIL) horde_align |= HORDE_NOGOOD;
3469
3470         attempts = 1000;
3471
3472         while (--attempts)
3473         {
3474                 /* Attempt to place the monster */
3475                 if (place_monster_aux(y, x, r_idx, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)) break;
3476         }
3477
3478         if (attempts < 1) {horde_align = 0;return FALSE;}
3479
3480         m_idx = cave[y][x].m_idx;
3481
3482         if (m_list[m_idx].mflag2 & MFLAG_CHAMELEON) r_ptr = &r_info[m_list[m_idx].r_idx];
3483         summon_kin_type = r_ptr->d_char;
3484
3485         for (attempts = randint1(10) + 5; attempts; attempts--)
3486         {
3487                 scatter(&cy, &cx, y, x, 5, 0);
3488
3489                 (void)summon_specific(m_idx, cy, cx, dun_level + 5, SUMMON_KIN, TRUE, FALSE, FALSE, FALSE, FALSE);
3490
3491                 y = cy;
3492                 x = cx;
3493         }
3494
3495         horde_align = 0;
3496         return TRUE;
3497 }
3498
3499 #endif /* MONSTER_HORDES */
3500
3501
3502
3503 /*
3504  * Attempt to allocate a random monster in the dungeon.
3505  *
3506  * Place the monster at least "dis" distance from the player.
3507  *
3508  * Use "slp" to choose the initial "sleep" status
3509  *
3510  * Use "monster_level" for the monster level
3511  */
3512 bool alloc_monster(int dis, bool slp)
3513 {
3514         int                     y = 0, x = 0;
3515         int         attempts_left = 10000;
3516         int guardian = d_info[dungeon_type].final_guardian;
3517
3518         /* Put an Guardian */
3519         if(guardian && d_info[dungeon_type].maxdepth == dun_level && r_info[guardian].cur_num < r_info[guardian].max_num )
3520         {
3521                 int oy;
3522                 int ox;
3523                 int try = 4000;
3524
3525                 /* Find a good position */
3526                 while(try)
3527                 {
3528                         /* Get a random spot */
3529                         oy = randint1(cur_hgt - 4) + 2;
3530                         ox = randint1(cur_wid - 4) + 2;
3531
3532                         /* Is it a good spot ? */
3533                         if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[guardian]))
3534                         {
3535                                 /* Place the guardian */
3536                                 if (place_monster_aux(oy, ox, guardian, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE)) break;
3537                         }
3538                         /* One less try */
3539                         try--;
3540                 }
3541         }
3542
3543
3544         /* Find a legal, distant, unoccupied, space */
3545         while (attempts_left--)
3546         {
3547                 /* Pick a location */
3548                 y = randint0(cur_hgt);
3549                 x = randint0(cur_wid);
3550
3551                 /* Require empty floor grid (was "naked") */
3552                 if (dun_level || (wilderness[p_ptr->wilderness_y][p_ptr->wilderness_x].terrain != TERRAIN_MOUNTAIN))
3553                 {
3554                         if (!cave_empty_bold2(y, x)) continue;
3555                 }
3556                 else
3557                 {
3558                         if (!cave_empty_bold2(y, x) && (cave[y][x].feat != FEAT_MOUNTAIN)) continue;
3559                 }
3560
3561                 /* Accept far away grids */
3562                 if (distance(y, x, py, px) > dis) break;
3563         }
3564
3565         if (!attempts_left)
3566         {
3567                 if (cheat_xtra || cheat_hear)
3568                 {
3569 #ifdef JP
3570 msg_print("·Ù¹ð¡ª¿·¤¿¤Ê¥â¥ó¥¹¥¿¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡£¾®¤µ¤¤³¬¤Ç¤¹¤«¡©");
3571 #else
3572                         msg_print("Warning! Could not allocate a new monster. Small level?");
3573 #endif
3574
3575                 }
3576
3577                 return (FALSE);
3578         }
3579
3580
3581 #ifdef MONSTER_HORDES
3582         if (randint1(5000) <= dun_level)
3583         {
3584                 if (alloc_horde(y, x))
3585                 {
3586 #ifdef JP
3587 if (cheat_hear) msg_print("¥â¥ó¥¹¥¿¡¼¤ÎÂç·²");
3588 #else
3589                         if (cheat_hear) msg_print("Monster horde.");
3590 #endif
3591
3592                         return (TRUE);
3593                 }
3594         }
3595         else
3596         {
3597 #endif /* MONSTER_HORDES */
3598
3599                 /* Attempt to place the monster, allow groups */
3600                 if (place_monster(y, x, slp, TRUE)) return (TRUE);
3601
3602 #ifdef MONSTER_HORDES
3603         }
3604 #endif /* MONSTER_HORDES */
3605
3606         /* Nope */
3607         return (FALSE);
3608 }
3609
3610
3611
3612
3613 /*
3614  * Hack -- help decide if a monster race is "okay" to summon
3615  */
3616 static bool summon_specific_okay(int r_idx)
3617 {
3618         monster_race *r_ptr = &r_info[r_idx];
3619
3620         /* Hack - Only summon dungeon monsters */
3621         if (!monster_dungeon(r_idx)) return (FALSE);
3622
3623         /* Hack -- identify the summoning monster */
3624         if (summon_specific_who > 0)
3625         {
3626                 monster_type *m_ptr = &m_list[summon_specific_who];
3627                 monster_race *s_ptr = &r_info[m_ptr->r_idx];
3628
3629                 /* Do not summon enemies */
3630
3631                 /* Friendly vs. opposite aligned normal or pet */
3632                 if (((r_ptr->flags3 & RF3_EVIL) &&
3633                           (s_ptr->flags3 & RF3_GOOD)) ||
3634                          ((r_ptr->flags3 & RF3_GOOD) &&
3635                           (s_ptr->flags3 & RF3_EVIL)))
3636                 {
3637                         return FALSE;
3638                 }
3639
3640                 /* Hostile vs. non-hostile */
3641                 if (is_hostile(m_ptr) != summon_specific_hostile)
3642                 {
3643                         return FALSE;
3644                 }
3645         }
3646         /* Use the player's alignment */
3647         else if (summon_specific_who < 0)
3648         {
3649                 /* Do not summon enemies of the pets */
3650                 if ((p_ptr->align < -9) && (r_ptr->flags3 & RF3_GOOD))
3651                 {
3652                         if (!one_in_((0-p_ptr->align)/2+1)) return FALSE;
3653                 }
3654                 else if ((p_ptr->align > 9) && (r_ptr->flags3 & RF3_EVIL))
3655                 {
3656                         if (!one_in_(p_ptr->align/2+1)) return FALSE;
3657                 }
3658         }
3659
3660         /* Hack -- no specific type specified */
3661         if (!summon_specific_type) return (TRUE);
3662
3663         if (!summon_unique_okay && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7))) return FALSE;
3664
3665         if ((summon_specific_who < 0) &&
3666             ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)) &&
3667             (((p_ptr->align > 9) && (r_ptr->flags3 & RF3_EVIL)) ||
3668              ((p_ptr->align < -9) && (r_ptr->flags3 & RF3_GOOD))))
3669                 return FALSE;
3670
3671         if ((horde_align & HORDE_NOGOOD) && (r_ptr->flags3 & RF3_GOOD)) return FALSE;
3672         if ((horde_align & HORDE_NOEVIL) && (r_ptr->flags3 & RF3_EVIL)) return FALSE;
3673
3674         if ((r_ptr->flags7 & RF7_CHAMELEON) && (d_info[dungeon_type].flags1 & DF1_CHAMELEON)) return TRUE;
3675
3676         return (summon_specific_aux(r_idx));
3677 }
3678
3679
3680 /*
3681  * Place a monster (of the specified "type") near the given
3682  * location.  Return TRUE if a monster was actually summoned.
3683  *
3684  * We will attempt to place the monster up to 10 times before giving up.
3685  *
3686  * Note: SUMMON_UNIQUE and SUMMON_AMBERITES will summon Unique's
3687  * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Unique's
3688  * Note: None of the other summon codes will ever summon Unique's.
3689  *
3690  * This function has been changed.  We now take the "monster level"
3691  * of the summoning monster as a parameter, and use that, along with
3692  * the current dungeon level, to help determine the level of the
3693  * desired monster.  Note that this is an upper bound, and also
3694  * tends to "prefer" monsters of that level.  Currently, we use
3695  * the average of the dungeon and monster levels, and then add
3696  * five to allow slight increases in monster power.
3697  *
3698  * Note that we use the new "monster allocation table" creation code
3699  * to restrict the "get_mon_num()" function to the set of "legal"
3700  * monsters, making this function much faster and more reliable.
3701  *
3702  * Note that this function may not succeed, though this is very rare.
3703  */
3704 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)
3705 {
3706         int x, y, r_idx;
3707
3708         bool no_kage = FALSE;
3709
3710         if (p_ptr->inside_arena) return (FALSE);
3711
3712         if (!mon_scatter(&y, &x, y1, x1, 2)) return FALSE;
3713
3714         /* Save the summoner */
3715         summon_specific_who = who;
3716
3717         /* Save the "summon" type */
3718         summon_specific_type = type;
3719
3720         summon_unique_okay = unique_okay;
3721
3722         /* Save the hostility */
3723         summon_specific_hostile = (!friendly && !pet);
3724
3725         /* Prepare allocation table */
3726         get_mon_num_prep(summon_specific_okay, get_monster_hook2(y, x));
3727
3728         /* Pick a monster, using the level calculation */
3729         r_idx = get_mon_num((dun_level + lev) / 2 + 5);
3730
3731         /* Handle failure */
3732         if (!r_idx)
3733         {
3734                 summon_specific_type = 0;
3735                 return (FALSE);
3736         }
3737
3738         if ((type == SUMMON_BLUE_HORROR) || (type == SUMMON_DAWN)) no_kage = TRUE;
3739
3740         /* Attempt to place the monster (awake, allow groups) */
3741         if (!place_monster_aux(y, x, r_idx, FALSE, group, friendly, pet, no_kage, no_pet))
3742         {
3743                 summon_specific_type = 0;
3744                 return (FALSE);
3745         }
3746
3747         summon_specific_type = 0;
3748         /* Success */
3749         return (TRUE);
3750 }
3751
3752 /* A "dangerous" function, creates a pet of the specified type */
3753 bool summon_named_creature (int oy, int ox, int r_idx, bool slp, bool group_ok, bool friendly, bool pet)
3754 {
3755         int x, y;
3756
3757         /* Paranoia */
3758         /* if (!r_idx) return; */
3759
3760         /* Prevent illegal monsters */
3761         if (r_idx >= max_r_idx) return FALSE;
3762
3763         if (p_ptr->inside_arena) return FALSE;
3764
3765         if (!mon_scatter(&y, &x, oy, ox, 2)) return FALSE;
3766
3767         /* Place it (allow groups) */
3768         return place_monster_aux(y, x, r_idx, slp, group_ok, friendly, pet, TRUE, FALSE);
3769 }
3770
3771
3772 /*
3773  * Let the given monster attempt to reproduce.
3774  *
3775  * Note that "reproduction" REQUIRES empty space.
3776  */
3777 bool multiply_monster(int m_idx, bool clone, bool friendly, bool pet)
3778 {
3779         monster_type    *m_ptr = &m_list[m_idx];
3780
3781         int y, x;
3782
3783         if (!mon_scatter(&y, &x, m_ptr->fy, m_ptr->fx, 1))
3784                 return FALSE;
3785
3786         /* Create a new monster (awake, no groups) */
3787         if (!place_monster_aux(y, x, m_ptr->r_idx, FALSE, FALSE, friendly, pet, TRUE, (bool)(m_ptr->mflag2 & MFLAG_NOPET)))
3788                 return FALSE;
3789
3790         if (clone)
3791         {
3792                 m_list[hack_m_idx_ii].smart |= SM_CLONED;
3793                 m_list[hack_m_idx_ii].mflag2 |= MFLAG_NOPET;
3794         }
3795
3796         return TRUE;
3797 }
3798
3799
3800
3801
3802
3803 /*
3804  * Dump a message describing a monster's reaction to damage
3805  *
3806  * Technically should attempt to treat "Beholder"'s as jelly's
3807  */
3808 void message_pain(int m_idx, int dam)
3809 {
3810         long oldhp, newhp, tmp;
3811         int percentage;
3812
3813         monster_type *m_ptr = &m_list[m_idx];
3814         monster_race *r_ptr = &r_info[m_ptr->r_idx];
3815
3816         char m_name[80];
3817
3818
3819         /* Get the monster name */
3820         monster_desc(m_name, m_ptr, 0);
3821
3822         /* Notice non-damage */
3823         if (dam == 0)
3824         {
3825 #ifdef JP
3826                 msg_format("%^s¤Ï¥À¥á¡¼¥¸¤ò¼õ¤±¤Æ¤¤¤Ê¤¤¡£", m_name);
3827 #else
3828                 msg_format("%^s is unharmed.", m_name);
3829 #endif
3830
3831                 return;
3832         }
3833
3834         /* Note -- subtle fix -CFT */
3835         newhp = (long)(m_ptr->hp);
3836         oldhp = newhp + (long)(dam);
3837         tmp = (newhp * 100L) / oldhp;
3838         percentage = (int)(tmp);
3839
3840
3841         /* Mushrooms, Eyes, Jellies, Molds, Vortices, Worms, Quylthulgs */
3842         if (strchr(",ejmvwQ", r_ptr->d_char))
3843         {
3844 #ifdef JP
3845                 if (percentage > 95)
3846                         msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
3847                 else if (percentage > 75)
3848                         msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
3849                 else if (percentage > 50)
3850                         msg_format("%^s¤Ï½Ì¤³¤Þ¤Ã¤¿¡£", m_name);
3851                 else if (percentage > 35)
3852                         msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
3853                 else if (percentage > 20)
3854                         msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
3855                 else if (percentage > 10)
3856                         msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
3857                 else
3858                         msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
3859 #else
3860                 if (percentage > 95)
3861                         msg_format("%^s barely notices.", m_name);
3862                 else if (percentage > 75)
3863                         msg_format("%^s flinches.", m_name);
3864                 else if (percentage > 50)
3865                         msg_format("%^s squelches.", m_name);
3866                 else if (percentage > 35)
3867                         msg_format("%^s quivers in pain.", m_name);
3868                 else if (percentage > 20)
3869                         msg_format("%^s writhes about.", m_name);
3870                 else if (percentage > 10)
3871                         msg_format("%^s writhes in agony.", m_name);
3872                 else
3873                         msg_format("%^s jerks limply.", m_name);
3874 #endif
3875
3876         }
3877
3878
3879         /* Fish */
3880         else if (strchr("l", r_ptr->d_char))
3881         {
3882                 if (percentage > 95)
3883 #ifdef JP
3884 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
3885 #else
3886                         msg_format("%^s barely notices.", m_name);
3887 #endif
3888                 else if (percentage > 75)
3889 #ifdef JP
3890 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
3891 #else
3892                         msg_format("%^s flinches.", m_name);
3893 #endif
3894                 else if (percentage > 50)
3895 #ifdef JP
3896 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
3897 #else
3898                         msg_format("%^s hesitates.", m_name);
3899 #endif
3900                 else if (percentage > 35)
3901 #ifdef JP
3902 msg_format("%^s¤ÏÄˤߤ˿̤¨¤¿¡£", m_name);
3903 #else
3904                         msg_format("%^s quivers in pain.", m_name);
3905 #endif
3906                 else if (percentage > 20)
3907 #ifdef JP
3908 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
3909 #else
3910                         msg_format("%^s writhes about.", m_name);
3911 #endif
3912                 else if (percentage > 10)
3913 #ifdef JP
3914 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
3915 #else
3916                         msg_format("%^s writhes in agony.", m_name);
3917 #endif
3918                 else
3919 #ifdef JP
3920 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
3921 #else
3922                         msg_format("%^s jerks limply.", m_name);
3923 #endif
3924         }
3925
3926
3927         /* Golems, Walls, Doors, Stairs */
3928         else if (strchr("g#+<>", r_ptr->d_char))
3929         {
3930                 if (percentage > 95)
3931 #ifdef JP
3932 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
3933 #else
3934                         msg_format("%^s ignores the attack.", m_name);
3935 #endif
3936                 else if (percentage > 75)
3937 #ifdef JP
3938 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
3939 #else
3940                         msg_format("%^s shrugs off the attack.", m_name);
3941 #endif
3942                 else if (percentage > 50)
3943 #ifdef JP
3944 msg_format("%^s¤ÏÍëÌĤΤ褦¤ËËʤ¨¤¿¡£", m_name);
3945 #else
3946                         msg_format("%^s roars thunderously.", m_name);
3947 #endif
3948                 else if (percentage > 35)
3949 #ifdef JP
3950 msg_format("%^s¤Ï¶ì¤·¤²¤ËËʤ¨¤¿¡£", m_name);
3951 #else
3952                         msg_format("%^s rumbles.", m_name);
3953 #endif
3954                 else if (percentage > 20)
3955 #ifdef JP
3956 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
3957 #else
3958                         msg_format("%^s grunts.", m_name);
3959 #endif
3960                 else if (percentage > 10)
3961 #ifdef JP
3962 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
3963 #else
3964                         msg_format("%^s hesitates.", m_name);
3965 #endif
3966                 else
3967 #ifdef JP
3968 msg_format("%^s¤Ï¤¯¤·¤ã¤¯¤·¤ã¤Ë¤Ê¤Ã¤¿¡£", m_name);
3969 #else
3970                         msg_format("%^s crumples.", m_name);
3971 #endif
3972         }
3973
3974
3975         /* Snakes, Hydrae, Reptiles, Mimics */
3976         else if (strchr("JMR", r_ptr->d_char) || !isalpha(r_ptr->d_char))
3977         {
3978                 if (percentage > 95)
3979 #ifdef JP
3980 msg_format("%^s¤Ï¤Û¤È¤ó¤Éµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
3981 #else
3982                         msg_format("%^s barely notices.", m_name);
3983 #endif
3984                 else if (percentage > 75)
3985 #ifdef JP
3986 msg_format("%^s¤Ï¥·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
3987 #else
3988                         msg_format("%^s hisses.", m_name);
3989 #endif
3990                 else if (percentage > 50)
3991 #ifdef JP
3992 msg_format("%^s¤ÏÅܤäÆƬ¤ò¾å¤²¤¿¡£", m_name);
3993 #else
3994                         msg_format("%^s rears up in anger.", m_name);
3995 #endif
3996                 else if (percentage > 35)
3997 #ifdef JP
3998 msg_format("%^s¤ÏÌÔÁ³¤È°Ò³Å¤·¤¿¡£", m_name);
3999 #else
4000                         msg_format("%^s hisses furiously.", m_name);
4001 #endif
4002                 else if (percentage > 20)
4003 #ifdef JP
4004 msg_format("%^s¤Ï¿È¤â¤À¤¨¤·¤¿¡£", m_name);
4005 #else
4006                         msg_format("%^s writhes about.", m_name);
4007 #endif
4008                 else if (percentage > 10)
4009 #ifdef JP
4010 msg_format("%^s¤Ï¶ìÄˤǿȤâ¤À¤¨¤·¤¿¡£", m_name);
4011 #else
4012                         msg_format("%^s writhes in agony.", m_name);
4013 #endif
4014                 else
4015 #ifdef JP
4016 msg_format("%^s¤Ï¤°¤Ë¤ã¤°¤Ë¤ã¤ÈáÛÚ»¤·¤¿¡£", m_name);
4017 #else
4018                         msg_format("%^s jerks limply.", m_name);
4019 #endif
4020         }
4021
4022
4023         /* Felines */
4024         else if (strchr("f", r_ptr->d_char))
4025         {
4026                 if (percentage > 95)
4027 #ifdef JP
4028 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4029 #else
4030                         msg_format("%^s shrugs off the attack.", m_name);
4031 #endif
4032                 else if (percentage > 75)
4033 #ifdef JP
4034 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4035 #else
4036                         msg_format("%^s roars.", m_name);
4037 #endif
4038                 else if (percentage > 50)
4039 #ifdef JP
4040 msg_format("%^s¤ÏÅܤäÆËʤ¨¤¿¡£", m_name);
4041 #else
4042                         msg_format("%^s growls angrily.", m_name);
4043 #endif
4044                 else if (percentage > 35)
4045 #ifdef JP
4046 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4047 #else
4048                         msg_format("%^s hisses with pain.", m_name);
4049 #endif
4050                 else if (percentage > 20)
4051 #ifdef JP
4052 msg_format("%^s¤ÏÄˤߤǼ塹¤·¤¯ÌĤ¤¤¿¡£", m_name);
4053 #else
4054                         msg_format("%^s mewls in pain.", m_name);
4055 #endif
4056                 else if (percentage > 10)
4057 #ifdef JP
4058 msg_format("%^s¤Ï¶ìÄˤˤ¦¤á¤¤¤¿¡£", m_name);
4059 #else
4060                         msg_format("%^s hisses in agony.", m_name);
4061 #endif
4062                 else
4063 #ifdef JP
4064 msg_format("%s¤Ï°¥¤ì¤ÊÌĤ­À¼¤ò½Ð¤·¤¿¡£", m_name);
4065 #else
4066                         msg_format("%^s mewls pitifully.", m_name);
4067 #endif
4068         }
4069
4070
4071         /* Ants, Centipedes, Flies, Insects, Beetles, Spiders */
4072         else if (strchr("acFIKS", r_ptr->d_char))
4073         {
4074                 if (percentage > 95)
4075 #ifdef JP
4076 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4077 #else
4078                         msg_format("%^s ignores the attack.", m_name);
4079 #endif
4080                 else if (percentage > 75)
4081 #ifdef JP
4082 msg_format("%^s¤Ï¥­¡¼¥­¡¼ÌĤ¤¤¿¡£", m_name);
4083 #else
4084                         msg_format("%^s chitters.", m_name);
4085 #endif
4086
4087                 else if (percentage > 50)
4088 #ifdef JP
4089 msg_format("%^s¤Ï¥è¥í¥è¥íƨ¤²²ó¤Ã¤¿¡£", m_name);
4090 #else
4091                         msg_format("%^s scuttles about.", m_name);
4092 #endif
4093
4094                 else if (percentage > 35)
4095 #ifdef JP
4096 msg_format("%^s¤Ï¤¦¤ë¤µ¤¯ÌĤ¤¤¿¡£", m_name);
4097 #else
4098                         msg_format("%^s twitters.", m_name);
4099 #endif
4100
4101                 else if (percentage > 20)
4102 #ifdef JP
4103 msg_format("%^s¤ÏÄˤߤËáÛÚ»¤·¤¿¡£", m_name);
4104 #else
4105                         msg_format("%^s jerks in pain.", m_name);
4106 #endif
4107
4108                 else if (percentage > 10)
4109 #ifdef JP
4110 msg_format("%^s¤Ï¶ìÄˤÇáÛÚ»¤·¤¿¡£", m_name);
4111 #else
4112                         msg_format("%^s jerks in agony.", m_name);
4113 #endif
4114
4115                 else
4116 #ifdef JP
4117 msg_format("%^s¤Ï¥Ô¥¯¥Ô¥¯¤Ò¤­¤Ä¤Ã¤¿¡£", m_name);
4118 #else
4119                         msg_format("%^s twitches.", m_name);
4120 #endif
4121
4122         }
4123
4124
4125         /* Birds */
4126         else if (strchr("B", r_ptr->d_char))
4127         {
4128                 if (percentage > 95)
4129 #ifdef JP
4130 msg_format("%^s¤Ï¤µ¤¨¤º¤Ã¤¿¡£", m_name);
4131 #else
4132                         msg_format("%^s chirps.", m_name);
4133 #endif
4134
4135                 else if (percentage > 75)
4136 #ifdef JP
4137 msg_format("%^s¤Ï¥Ô¡¼¥Ô¡¼ÌĤ¤¤¿¡£", m_name);
4138 #else
4139                         msg_format("%^s twitters.", m_name);
4140 #endif
4141
4142                 else if (percentage > 50)
4143 #ifdef JP
4144 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ¤¤¿¡£", m_name);
4145 #else
4146                         msg_format("%^s squawks.", m_name);
4147 #endif
4148
4149                 else if (percentage > 35)
4150 #ifdef JP
4151 msg_format("%^s¤Ï¥®¥ã¡¼¥®¥ã¡¼ÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4152 #else
4153                         msg_format("%^s chatters.", m_name);
4154 #endif
4155
4156                 else if (percentage > 20)
4157 #ifdef JP
4158 msg_format("%^s¤Ï¶ì¤·¤ó¤À¡£", m_name);
4159 #else
4160                         msg_format("%^s jeers.", m_name);
4161 #endif
4162
4163                 else if (percentage > 10)
4164 #ifdef JP
4165 msg_format("%^s¤Ï¤Î¤¿¤¦¤Á²ó¤Ã¤¿¡£", m_name);
4166 #else
4167                         msg_format("%^s flutters about.", m_name);
4168 #endif
4169
4170                 else
4171 #ifdef JP
4172 msg_format("%^s¤Ï¥­¡¼¥­¡¼¤ÈÌĤ­¶«¤ó¤À¡£", m_name);
4173 #else
4174                         msg_format("%^s squeaks.", m_name);
4175 #endif
4176
4177         }
4178
4179
4180         /* Dragons, Demons, High Undead */
4181         else if (strchr("duDLUW", r_ptr->d_char))
4182         {
4183                 if (percentage > 95)
4184 #ifdef JP
4185 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4186 #else
4187                         msg_format("%^s ignores the attack.", m_name);
4188 #endif
4189
4190                 else if (percentage > 75)
4191 #ifdef JP
4192 msg_format("%^s¤Ï¤·¤ê¹þ¤ß¤·¤¿¡£", m_name);
4193 #else
4194                         msg_format("%^s flinches.", m_name);
4195 #endif
4196
4197                 else if (percentage > 50)
4198 #ifdef JP
4199 msg_format("%^s¤ÏÄˤߤǥ·¡¼¥Ã¤ÈÌĤ¤¤¿¡£", m_name);
4200 #else
4201                         msg_format("%^s hisses in pain.", m_name);
4202 #endif
4203
4204                 else if (percentage > 35)
4205 #ifdef JP
4206 msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4207 #else
4208                         msg_format("%^s snarls with pain.", m_name);
4209 #endif
4210
4211                 else if (percentage > 20)
4212 #ifdef JP
4213 msg_format("%^s¤ÏÄˤߤËËʤ¨¤¿¡£", m_name);
4214 #else
4215                         msg_format("%^s roars with pain.", m_name);
4216 #endif
4217
4218                 else if (percentage > 10)
4219 #ifdef JP
4220 msg_format("%^s¤Ï¶ì¤·¤²¤Ë¶«¤ó¤À¡£", m_name);
4221 #else
4222                         msg_format("%^s gasps.", m_name);
4223 #endif
4224
4225                 else
4226 #ifdef JP
4227 msg_format("%^s¤Ï¼å¡¹¤·¤¯¤¦¤Ê¤Ã¤¿¡£", m_name);
4228 #else
4229                         msg_format("%^s snarls feebly.", m_name);
4230 #endif
4231
4232         }
4233
4234
4235         /* Skeletons */
4236         else if (strchr("s", r_ptr->d_char))
4237         {
4238                 if (percentage > 95)
4239 #ifdef JP
4240 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4241 #else
4242                         msg_format("%^s ignores the attack.", m_name);
4243 #endif
4244
4245                 else if (percentage > 75)
4246 #ifdef JP
4247 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4248 #else
4249                         msg_format("%^s shrugs off the attack.", m_name);
4250 #endif
4251
4252                 else if (percentage > 50)
4253 #ifdef JP
4254 msg_format("%^s¤Ï¥«¥¿¥«¥¿¤È¾Ð¤Ã¤¿¡£", m_name);
4255 #else
4256                         msg_format("%^s rattles.", m_name);
4257 #endif
4258
4259                 else if (percentage > 35)
4260 #ifdef JP
4261 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4262 #else
4263                         msg_format("%^s stumbles.", m_name);
4264 #endif
4265
4266                 else if (percentage > 20)
4267 #ifdef JP
4268 msg_format("%^s¤Ï¥«¥¿¥«¥¿¸À¤Ã¤¿¡£", m_name);
4269 #else
4270                         msg_format("%^s rattles.", m_name);
4271 #endif
4272
4273                 else if (percentage > 10)
4274 #ifdef JP
4275 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4276 #else
4277                         msg_format("%^s staggers.", m_name);
4278 #endif
4279
4280                 else
4281 #ifdef JP
4282 msg_format("%^s¤Ï¥¬¥¿¥¬¥¿¸À¤Ã¤¿¡£", m_name);
4283 #else
4284                         msg_format("%^s clatters.", m_name);
4285 #endif
4286
4287         }
4288
4289
4290         /* Zombies */
4291         else if (strchr("z", r_ptr->d_char))
4292         {
4293                 if (percentage > 95)
4294 #ifdef JP
4295 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4296 #else
4297                         msg_format("%^s ignores the attack.", m_name);
4298 #endif
4299
4300                 else if (percentage > 75)
4301 #ifdef JP
4302 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4303 #else
4304                         msg_format("%^s shrugs off the attack.", m_name);
4305 #endif
4306
4307                 else if (percentage > 50)
4308 #ifdef JP
4309 msg_format("%^s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4310 #else
4311                         msg_format("%^s groans.", m_name);
4312 #endif
4313
4314                 else if (percentage > 35)
4315 #ifdef JP
4316 msg_format("%s¤Ï¶ì¤·¤²¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4317 #else
4318                         msg_format("%^s moans.", m_name);
4319 #endif
4320
4321                 else if (percentage > 20)
4322 #ifdef JP
4323 msg_format("%^s¤Ïí´í°¤·¤¿¡£", m_name);
4324 #else
4325                         msg_format("%^s hesitates.", m_name);
4326 #endif
4327
4328                 else if (percentage > 10)
4329 #ifdef JP
4330 msg_format("%^s¤Ï¤¦¤Ê¤Ã¤¿¡£", m_name);
4331 #else
4332                         msg_format("%^s grunts.", m_name);
4333 #endif
4334
4335                 else
4336 #ifdef JP
4337 msg_format("%^s¤Ï¤è¤í¤á¤¤¤¿¡£", m_name);
4338 #else
4339                         msg_format("%^s staggers.", m_name);
4340 #endif
4341
4342         }
4343
4344
4345         /* Ghosts */
4346         else if (strchr("G", r_ptr->d_char))
4347
4348         {
4349                 if (percentage > 95)
4350 #ifdef JP
4351 msg_format("%s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4352 #else
4353                         msg_format("%^s ignores the attack.", m_name);
4354 #endif
4355
4356                 else if (percentage > 75)
4357 #ifdef JP
4358 msg_format("%s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4359 #else
4360                         msg_format("%^s shrugs off the attack.", m_name);
4361 #endif
4362
4363                 else if (percentage > 50)
4364 #ifdef JP
4365 msg_format("%s¤Ï¤¦¤á¤¤¤¿¡£", m_name);
4366 #else
4367                         msg_format("%^s moans.", m_name);
4368 #endif
4369
4370                 else if (percentage > 35)
4371 #ifdef JP
4372 msg_format("%^s¤Ïµã¤­¤ï¤á¤¤¤¿¡£", m_name);
4373 #else
4374                         msg_format("%^s wails.", m_name);
4375 #endif
4376
4377                 else if (percentage > 20)
4378 #ifdef JP
4379 msg_format("%^s¤ÏËʤ¨¤¿¡£", m_name);
4380 #else
4381                         msg_format("%^s howls.", m_name);
4382 #endif
4383
4384                 else if (percentage > 10)
4385 #ifdef JP
4386 msg_format("%s¤Ï¼å¡¹¤·¤¯¤¦¤á¤¤¤¿¡£", m_name);
4387 #else
4388                         msg_format("%^s moans softly.", m_name);
4389 #endif
4390
4391                 else
4392 #ifdef JP
4393 msg_format("%^s¤Ï¤«¤¹¤«¤Ë¤¦¤á¤¤¤¿¡£", m_name);
4394 #else
4395                         msg_format("%^s sighs.", m_name);
4396 #endif
4397
4398         }
4399
4400
4401         /* Dogs and Hounds */
4402         else if (strchr("CZ", r_ptr->d_char))
4403         {
4404 #ifdef JP
4405                 if (percentage > 95)
4406                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4407                 else if (percentage > 75)
4408                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4409                 else if (percentage > 50)
4410                         msg_format("%^s¤ÏÄˤߤǥ­¥ã¥ó¥­¥ã¥óËʤ¨¤¿¡£", m_name);
4411                 else if (percentage > 35)
4412                         msg_format("%^s¤ÏÄˤߤÇÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4413                 else if (percentage > 20)
4414                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÌĤ­¤ï¤á¤¤¤¿¡£", m_name);
4415                 else if (percentage > 10)
4416                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4417                 else
4418                         msg_format("%^s¤Ï¼å¡¹¤·¤¯Ëʤ¨¤¿¡£", m_name);
4419 #else
4420                 if (percentage > 95)
4421                         msg_format("%^s shrugs off the attack.", m_name);
4422                 else if (percentage > 75)
4423                         msg_format("%^s snarls with pain.", m_name);
4424                 else if (percentage > 50)
4425                         msg_format("%^s yelps in pain.", m_name);
4426                 else if (percentage > 35)
4427                         msg_format("%^s howls in pain.", m_name);
4428                 else if (percentage > 20)
4429                         msg_format("%^s howls in agony.", m_name);
4430                 else if (percentage > 10)
4431                         msg_format("%^s writhes in agony.", m_name);
4432                 else
4433                         msg_format("%^s yelps feebly.", m_name);
4434 #endif
4435
4436         }
4437
4438         /* One type of monsters (ignore,squeal,shriek) */
4439         else if (strchr("Xbilqrt", r_ptr->d_char))
4440         {
4441 #ifdef JP
4442                 if (percentage > 95)
4443                         msg_format("%^s¤Ï¹¶·â¤òµ¤¤Ë¤È¤á¤Æ¤¤¤Ê¤¤¡£", m_name);
4444                 else if (percentage > 75)
4445                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4446                 else if (percentage > 50)
4447                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4448                 else if (percentage > 35)
4449                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4450                 else if (percentage > 20)
4451                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4452                 else if (percentage > 10)
4453                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4454                 else
4455                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4456 #else
4457                 if (percentage > 95)
4458                         msg_format("%^s ignores the attack.", m_name);
4459                 else if (percentage > 75)
4460                         msg_format("%^s grunts with pain.", m_name);
4461                 else if (percentage > 50)
4462                         msg_format("%^s squeals in pain.", m_name);
4463                 else if (percentage > 35)
4464                         msg_format("%^s shrieks in pain.", m_name);
4465                 else if (percentage > 20)
4466                         msg_format("%^s shrieks in agony.", m_name);
4467                 else if (percentage > 10)
4468                         msg_format("%^s writhes in agony.", m_name);
4469                 else
4470                         msg_format("%^s cries out feebly.", m_name);
4471 #endif
4472
4473         }
4474
4475         /* Another type of monsters (shrug,cry,scream) */
4476         else
4477         {
4478 #ifdef JP
4479                 if (percentage > 95)
4480                         msg_format("%^s¤Ï¹¶·â¤Ë¸ª¤ò¤¹¤¯¤á¤¿¡£", m_name);
4481                 else if (percentage > 75)
4482                         msg_format("%^s¤ÏÄˤߤǤ¦¤Ê¤Ã¤¿¡£", m_name);
4483                 else if (percentage > 50)
4484                         msg_format("%^s¤ÏÄˤߤǶ«¤ó¤À¡£", m_name);
4485                 else if (percentage > 35)
4486                         msg_format("%^s¤ÏÄˤߤÇÀ䶫¤·¤¿¡£", m_name);
4487                 else if (percentage > 20)
4488                         msg_format("%^s¤Ï¶ìÄˤΤ¢¤Þ¤êÀ䶫¤·¤¿¡£", m_name);
4489                 else if (percentage > 10)
4490                         msg_format("%^s¤Ï¶ìÄˤǤâ¤À¤¨¶ì¤·¤ó¤À¡£", m_name);
4491                 else
4492                         msg_format("%^s¤Ï¼å¡¹¤·¤¯¶«¤ó¤À¡£", m_name);
4493 #else
4494                 if (percentage > 95)
4495                         msg_format("%^s shrugs off the attack.", m_name);
4496                 else if (percentage > 75)
4497                         msg_format("%^s grunts with pain.", m_name);
4498                 else if (percentage > 50)
4499                         msg_format("%^s cries out in pain.", m_name);
4500                 else if (percentage > 35)
4501                         msg_format("%^s screams in pain.", m_name);
4502                 else if (percentage > 20)
4503                         msg_format("%^s screams in agony.", m_name);
4504                 else if (percentage > 10)
4505                         msg_format("%^s writhes in agony.", m_name);
4506                 else
4507                         msg_format("%^s cries out feebly.", m_name);
4508 #endif
4509
4510         }
4511 }
4512
4513
4514 /*
4515  * Learn about an "observed" resistance.
4516  */
4517 void update_smart_learn(int m_idx, int what)
4518 {
4519 #ifdef DRS_SMART_OPTIONS
4520
4521         monster_type *m_ptr = &m_list[m_idx];
4522
4523         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4524
4525
4526         /* Not allowed to learn */
4527         if (!smart_learn) return;
4528
4529         /* Too stupid to learn anything */
4530         if (r_ptr->flags2 & (RF2_STUPID)) return;
4531
4532         /* Not intelligent, only learn sometimes */
4533         if (!(r_ptr->flags2 & (RF2_SMART)) && (randint0(100) < 50)) return;
4534
4535
4536         /* XXX XXX XXX */
4537
4538         /* Analyze the knowledge */
4539         switch (what)
4540         {
4541         case DRS_ACID:
4542                 if (p_ptr->resist_acid) m_ptr->smart |= (SM_RES_ACID);
4543                 if (p_ptr->oppose_acid) m_ptr->smart |= (SM_OPP_ACID);
4544                 if (p_ptr->immune_acid) m_ptr->smart |= (SM_IMM_ACID);
4545                 break;
4546
4547         case DRS_ELEC:
4548                 if (p_ptr->resist_elec) m_ptr->smart |= (SM_RES_ELEC);
4549                 if (p_ptr->oppose_elec) m_ptr->smart |= (SM_OPP_ELEC);
4550                 if (p_ptr->immune_elec) m_ptr->smart |= (SM_IMM_ELEC);
4551                 break;
4552
4553         case DRS_FIRE:
4554                 if (p_ptr->resist_fire) m_ptr->smart |= (SM_RES_FIRE);
4555                 if (p_ptr->oppose_fire) m_ptr->smart |= (SM_OPP_FIRE);
4556                 if (p_ptr->immune_fire) m_ptr->smart |= (SM_IMM_FIRE);
4557                 break;
4558
4559         case DRS_COLD:
4560                 if (p_ptr->resist_cold) m_ptr->smart |= (SM_RES_COLD);
4561                 if (p_ptr->oppose_cold) m_ptr->smart |= (SM_OPP_COLD);
4562                 if (p_ptr->immune_cold) m_ptr->smart |= (SM_IMM_COLD);
4563                 break;
4564
4565         case DRS_POIS:
4566                 if (p_ptr->resist_pois) m_ptr->smart |= (SM_RES_POIS);
4567                 if (p_ptr->oppose_pois) m_ptr->smart |= (SM_OPP_POIS);
4568                 break;
4569
4570
4571         case DRS_NETH:
4572                 if (p_ptr->resist_neth) m_ptr->smart |= (SM_RES_NETH);
4573                 break;
4574
4575         case DRS_LITE:
4576                 if (p_ptr->resist_lite) m_ptr->smart |= (SM_RES_LITE);
4577                 break;
4578
4579         case DRS_DARK:
4580                 if (p_ptr->resist_dark) m_ptr->smart |= (SM_RES_DARK);
4581                 break;
4582
4583         case DRS_FEAR:
4584                 if (p_ptr->resist_fear) m_ptr->smart |= (SM_RES_FEAR);
4585                 break;
4586
4587         case DRS_CONF:
4588                 if (p_ptr->resist_conf) m_ptr->smart |= (SM_RES_CONF);
4589                 break;
4590
4591         case DRS_CHAOS:
4592                 if (p_ptr->resist_chaos) m_ptr->smart |= (SM_RES_CHAOS);
4593                 break;
4594
4595         case DRS_DISEN:
4596                 if (p_ptr->resist_disen) m_ptr->smart |= (SM_RES_DISEN);
4597                 break;
4598
4599         case DRS_BLIND:
4600                 if (p_ptr->resist_blind) m_ptr->smart |= (SM_RES_BLIND);
4601                 break;
4602
4603         case DRS_NEXUS:
4604                 if (p_ptr->resist_nexus) m_ptr->smart |= (SM_RES_NEXUS);
4605                 break;
4606
4607         case DRS_SOUND:
4608                 if (p_ptr->resist_sound) m_ptr->smart |= (SM_RES_SOUND);
4609                 break;
4610
4611         case DRS_SHARD:
4612                 if (p_ptr->resist_shard) m_ptr->smart |= (SM_RES_SHARD);
4613                 break;
4614
4615         case DRS_FREE:
4616                 if (p_ptr->free_act) m_ptr->smart |= (SM_IMM_FREE);
4617                 break;
4618
4619         case DRS_MANA:
4620                 if (!p_ptr->msp) m_ptr->smart |= (SM_IMM_MANA);
4621                 break;
4622
4623         case DRS_REFLECT:
4624                 if (p_ptr->reflect) m_ptr-> smart |= (SM_IMM_REFLECT);
4625                 break;
4626         }
4627
4628 #endif /* DRS_SMART_OPTIONS */
4629
4630 }
4631
4632
4633 /*
4634  * Place the player in the dungeon XXX XXX
4635  */
4636 bool player_place(int y, int x)
4637 {
4638         /* Paranoia XXX XXX */
4639         if (cave[y][x].m_idx != 0) return FALSE;
4640
4641         /* Save player location */
4642         py = y;
4643         px = x;
4644
4645         /* Success */
4646         return TRUE;
4647 }
4648
4649
4650 /*
4651  * Drop all items carried by a monster
4652  */
4653 void monster_drop_carried_objects(monster_type *m_ptr)
4654 {
4655         s16b this_o_idx, next_o_idx = 0;
4656         object_type forge;
4657         object_type *o_ptr;
4658         object_type *q_ptr;
4659
4660
4661         /* Drop objects being carried */
4662         for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
4663         {
4664                 /* Acquire object */
4665                 o_ptr = &o_list[this_o_idx];
4666
4667                 /* Acquire next object */
4668                 next_o_idx = o_ptr->next_o_idx;
4669
4670                 /* Paranoia */
4671                 o_ptr->held_m_idx = 0;
4672
4673                 /* Get local object */
4674                 q_ptr = &forge;
4675
4676                 /* Copy the object */
4677                 object_copy(q_ptr, o_ptr);
4678
4679                 /* Delete the object */
4680                 delete_object_idx(this_o_idx);
4681
4682                 /* Drop it */
4683                 (void)drop_near(q_ptr, -1, m_ptr->fy, m_ptr->fx);
4684         }
4685
4686         /* Forget objects */
4687         m_ptr->hold_o_idx = 0;
4688 }