OSDN Git Service

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