OSDN Git Service

patch artifact
[jnethack/source.git] / src / mondata.c
1 /* NetHack 3.6  mondata.c       $NHDT-Date: 1446604115 2015/11/04 02:28:35 $  $NHDT-Branch: master $:$NHDT-Revision: 1.58 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 /* JNetHack Copyright */
6 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000  */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2016            */
8 /* JNetHack may be freely redistributed.  See license for details. */
9
10 #include "hack.h"
11
12 /*      These routines provide basic data for any type of monster. */
13
14 /* set up an individual monster's base type (initial creation, shapechange) */
15 void
16 set_mon_data(mon, ptr, flag)
17 struct monst *mon;
18 struct permonst *ptr;
19 int flag;
20 {
21     mon->data = ptr;
22     if (flag == -1)
23         return; /* "don't care" */
24
25     if (flag == 1)
26         mon->mintrinsics |= (ptr->mresists & 0x00FF);
27     else
28         mon->mintrinsics = (ptr->mresists & 0x00FF);
29     return;
30 }
31
32 /* does monster-type have any attack for a specific type of damage? */
33 struct attack *
34 attacktype_fordmg(ptr, atyp, dtyp)
35 struct permonst *ptr;
36 int atyp, dtyp;
37 {
38     struct attack *a;
39
40     for (a = &ptr->mattk[0]; a < &ptr->mattk[NATTK]; a++)
41         if (a->aatyp == atyp && (dtyp == AD_ANY || a->adtyp == dtyp))
42             return a;
43     return (struct attack *) 0;
44 }
45
46 /* does monster-type have a particular type of attack */
47 boolean
48 attacktype(ptr, atyp)
49 struct permonst *ptr;
50 int atyp;
51 {
52     return attacktype_fordmg(ptr, atyp, AD_ANY) ? TRUE : FALSE;
53 }
54
55 /* returns True if monster doesn't attack, False if it does */
56 boolean
57 noattacks(ptr)
58 struct permonst *ptr;
59 {
60     int i;
61     struct attack *mattk = ptr->mattk;
62
63     for (i = 0; i < NATTK; i++) {
64         /* AT_BOOM "passive attack" (gas spore's explosion upon death)
65            isn't an attack as far as our callers are concerned */
66         if (mattk[i].aatyp == AT_BOOM)
67             continue;
68
69         if (mattk[i].aatyp)
70             return FALSE;
71     }
72     return TRUE;
73 }
74
75 /* does monster-type transform into something else when petrified? */
76 boolean
77 poly_when_stoned(ptr)
78 struct permonst *ptr;
79 {
80     /* non-stone golems turn into stone golems unless latter is genocided */
81     return (boolean) (is_golem(ptr) && ptr != &mons[PM_STONE_GOLEM]
82                       && !(mvitals[PM_STONE_GOLEM].mvflags & G_GENOD));
83     /* allow G_EXTINCT */
84 }
85
86 /* returns True if monster is drain-life resistant */
87 boolean
88 resists_drli(mon)
89 struct monst *mon;
90 {
91     struct permonst *ptr = mon->data;
92     struct obj *wep;
93
94     if (is_undead(ptr) || is_demon(ptr) || is_were(ptr)
95         /* is_were() doesn't handle hero in human form */
96         || (mon == &youmonst && u.ulycn >= LOW_PM)
97         || ptr == &mons[PM_DEATH] || is_vampshifter(mon))
98         return TRUE;
99     wep = (mon == &youmonst) ? uwep : MON_WEP(mon);
100     return (boolean) (wep && wep->oartifact && defends(AD_DRLI, wep));
101 }
102
103 /* True if monster is magic-missile (actually, general magic) resistant */
104 boolean
105 resists_magm(mon)
106 struct monst *mon;
107 {
108     struct permonst *ptr = mon->data;
109     boolean is_you = (mon == &youmonst);
110     long slotmask;
111     struct obj *o;
112
113     /* as of 3.2.0:  gray dragons, Angels, Oracle, Yeenoghu */
114     if (dmgtype(ptr, AD_MAGM) || ptr == &mons[PM_BABY_GRAY_DRAGON]
115         || dmgtype(ptr, AD_RBRE)) /* Chromatic Dragon */
116         return TRUE;
117     /* check for magic resistance granted by wielded weapon */
118     o = is_you ? uwep : MON_WEP(mon);
119     if (o && o->oartifact && defends(AD_MAGM, o))
120         return TRUE;
121     /* check for magic resistance granted by worn or carried items */
122     o = is_you ? invent : mon->minvent;
123     slotmask = W_ARMOR | W_ACCESSORY;
124     if (!is_you /* assumes monsters don't wield non-weapons */
125         || (uwep && (uwep->oclass == WEAPON_CLASS || is_weptool(uwep))))
126         slotmask |= W_WEP;
127     if (is_you && u.twoweap)
128         slotmask |= W_SWAPWEP;
129     for (; o; o = o->nobj)
130         if (((o->owornmask & slotmask) != 0L
131              && objects[o->otyp].oc_oprop == ANTIMAGIC)
132             || (o->oartifact && defends_when_carried(AD_MAGM, o)))
133             return TRUE;
134     return FALSE;
135 }
136
137 /* True iff monster is resistant to light-induced blindness */
138 boolean
139 resists_blnd(mon)
140 struct monst *mon;
141 {
142     struct permonst *ptr = mon->data;
143     boolean is_you = (mon == &youmonst);
144     long slotmask;
145     struct obj *o;
146
147     if (is_you ? (Blind || Unaware)
148                : (mon->mblinded || !mon->mcansee || !haseyes(ptr)
149                   /* BUG: temporary sleep sets mfrozen, but since
150                           paralysis does too, we can't check it */
151                   || mon->msleeping))
152         return TRUE;
153     /* yellow light, Archon; !dust vortex, !cobra, !raven */
154     if (dmgtype_fromattack(ptr, AD_BLND, AT_EXPL)
155         || dmgtype_fromattack(ptr, AD_BLND, AT_GAZE))
156         return TRUE;
157     o = is_you ? uwep : MON_WEP(mon);
158     if (o && o->oartifact && defends(AD_BLND, o))
159         return TRUE;
160     o = is_you ? invent : mon->minvent;
161     slotmask = W_ARMOR | W_ACCESSORY;
162     if (!is_you /* assumes monsters don't wield non-weapons */
163         || (uwep && (uwep->oclass == WEAPON_CLASS || is_weptool(uwep))))
164         slotmask |= W_WEP;
165     if (is_you && u.twoweap)
166         slotmask |= W_SWAPWEP;
167     for (; o; o = o->nobj)
168         if (((o->owornmask & slotmask) != 0L
169              && objects[o->otyp].oc_oprop == BLINDED)
170             || (o->oartifact && defends_when_carried(AD_BLND, o)))
171             return TRUE;
172     return FALSE;
173 }
174
175 /* True iff monster can be blinded by the given attack;
176    note: may return True when mdef is blind (e.g. new cream-pie attack) */
177 boolean
178 can_blnd(magr, mdef, aatyp, obj)
179 struct monst *magr; /* NULL == no specific aggressor */
180 struct monst *mdef;
181 uchar aatyp;
182 struct obj *obj; /* aatyp == AT_WEAP, AT_SPIT */
183 {
184     boolean is_you = (mdef == &youmonst);
185     boolean check_visor = FALSE;
186     struct obj *o;
187     const char *s;
188
189     /* no eyes protect against all attacks for now */
190     if (!haseyes(mdef->data))
191         return FALSE;
192
193     switch (aatyp) {
194     case AT_EXPL:
195     case AT_BOOM:
196     case AT_GAZE:
197     case AT_MAGC:
198     case AT_BREA: /* assumed to be lightning */
199         /* light-based attacks may be cancelled or resisted */
200         if (magr && magr->mcan)
201             return FALSE;
202         return !resists_blnd(mdef);
203
204     case AT_WEAP:
205     case AT_SPIT:
206     case AT_NONE:
207         /* an object is used (thrown/spit/other) */
208         if (obj && (obj->otyp == CREAM_PIE)) {
209             if (is_you && Blindfolded)
210                 return FALSE;
211         } else if (obj && (obj->otyp == BLINDING_VENOM)) {
212             /* all ublindf, including LENSES, protect, cream-pies too */
213             if (is_you && (ublindf || u.ucreamed))
214                 return FALSE;
215             check_visor = TRUE;
216         } else if (obj && (obj->otyp == POT_BLINDNESS)) {
217             return TRUE; /* no defense */
218         } else
219             return FALSE; /* other objects cannot cause blindness yet */
220         if ((magr == &youmonst) && u.uswallow)
221             return FALSE; /* can't affect eyes while inside monster */
222         break;
223
224     case AT_ENGL:
225         if (is_you && (Blindfolded || Unaware || u.ucreamed))
226             return FALSE;
227         if (!is_you && mdef->msleeping)
228             return FALSE;
229         break;
230
231     case AT_CLAW:
232         /* e.g. raven: all ublindf, including LENSES, protect */
233         if (is_you && ublindf)
234             return FALSE;
235         if ((magr == &youmonst) && u.uswallow)
236             return FALSE; /* can't affect eyes while inside monster */
237         check_visor = TRUE;
238         break;
239
240     case AT_TUCH:
241     case AT_STNG:
242         /* some physical, blind-inducing attacks can be cancelled */
243         if (magr && magr->mcan)
244             return FALSE;
245         break;
246
247     default:
248         break;
249     }
250
251     /* check if wearing a visor (only checked if visor might help) */
252     if (check_visor) {
253         o = (mdef == &youmonst) ? invent : mdef->minvent;
254         for (; o; o = o->nobj)
255             if ((o->owornmask & W_ARMH)
256                 && (s = OBJ_DESCR(objects[o->otyp])) != (char *) 0
257                 && !strcmp(s, "visored helmet"))
258                 return FALSE;
259     }
260
261     return TRUE;
262 }
263
264 /* returns True if monster can attack at range */
265 boolean
266 ranged_attk(ptr)
267 struct permonst *ptr;
268 {
269     register int i, atyp;
270     long atk_mask = (1L << AT_BREA) | (1L << AT_SPIT) | (1L << AT_GAZE);
271
272     /* was: (attacktype(ptr, AT_BREA) || attacktype(ptr, AT_WEAP)
273      *       || attacktype(ptr, AT_SPIT) || attacktype(ptr, AT_GAZE)
274      *       || attacktype(ptr, AT_MAGC));
275      * but that's too slow -dlc
276      */
277     for (i = 0; i < NATTK; i++) {
278         atyp = ptr->mattk[i].aatyp;
279         if (atyp >= AT_WEAP)
280             return TRUE;
281         /* assert(atyp < 32); */
282         if ((atk_mask & (1L << atyp)) != 0L)
283             return TRUE;
284     }
285     return FALSE;
286 }
287
288 /* True if specific monster is especially affected by silver weapons */
289 boolean
290 mon_hates_silver(mon)
291 struct monst *mon;
292 {
293     return (boolean) (is_vampshifter(mon) || hates_silver(mon->data));
294 }
295
296 /* True if monster-type is especially affected by silver weapons */
297 boolean
298 hates_silver(ptr)
299 register struct permonst *ptr;
300 {
301     return (boolean) (is_were(ptr) || ptr->mlet == S_VAMPIRE || is_demon(ptr)
302                       || ptr == &mons[PM_SHADE]
303                       || (ptr->mlet == S_IMP && ptr != &mons[PM_TENGU]));
304 }
305
306 /* True iff the type of monster pass through iron bars */
307 boolean
308 passes_bars(mptr)
309 struct permonst *mptr;
310 {
311     return (boolean) (passes_walls(mptr) || amorphous(mptr) || unsolid(mptr)
312                       || is_whirly(mptr) || verysmall(mptr)
313                       || dmgtype(mptr, AD_CORR) || dmgtype(mptr, AD_RUST)
314                       || (slithy(mptr) && !bigmonst(mptr)));
315 }
316
317 /* returns True if monster can blow (whistle, etc) */
318 boolean
319 can_blow(mtmp)
320 register struct monst *mtmp;
321 {
322     if ((is_silent(mtmp->data) || mtmp->data->msound == MS_BUZZ)
323         && (breathless(mtmp->data) || verysmall(mtmp->data)
324             || !has_head(mtmp->data) || mtmp->data->mlet == S_EEL))
325         return FALSE;
326     if ((mtmp == &youmonst) && Strangled)
327         return FALSE;
328     return TRUE;
329 }
330
331 /* True if mon is vulnerable to strangulation */
332 boolean
333 can_be_strangled(mon)
334 struct monst *mon;
335 {
336     struct obj *mamul;
337     boolean nonbreathing, nobrainer;
338
339     /* For amulet of strangulation support:  here we're considering
340        strangulation to be loss of blood flow to the brain due to
341        constriction of the arteries in the neck, so all headless
342        creatures are immune (no neck) as are mindless creatures
343        who don't need to breathe (brain, if any, doesn't care).
344        Mindless creatures who do need to breath are vulnerable, as
345        are non-breathing creatures which have higher brain function. */
346     if (!has_head(mon->data))
347         return FALSE;
348     if (mon == &youmonst) {
349         /* hero can't be mindless but poly'ing into mindless form can
350            confer strangulation protection */
351         nobrainer = mindless(youmonst.data);
352         nonbreathing = Breathless;
353     } else {
354         nobrainer = mindless(mon->data);
355         /* monsters don't wear amulets of magical breathing,
356            so second part doesn't achieve anything useful... */
357         nonbreathing = (breathless(mon->data)
358                         || ((mamul = which_armor(mon, W_AMUL)) != 0
359                             && (mamul->otyp == AMULET_OF_MAGICAL_BREATHING)));
360     }
361     return (boolean) (!nobrainer || !nonbreathing);
362 }
363
364 /* returns True if monster can track well */
365 boolean
366 can_track(ptr)
367 register struct permonst *ptr;
368 {
369     if (uwep && uwep->oartifact == ART_EXCALIBUR)
370         return TRUE;
371     else
372         return (boolean) haseyes(ptr);
373 }
374
375 /* creature will slide out of armor */
376 boolean
377 sliparm(ptr)
378 register struct permonst *ptr;
379 {
380     return (boolean) (is_whirly(ptr) || ptr->msize <= MZ_SMALL
381                       || noncorporeal(ptr));
382 }
383
384 /* creature will break out of armor */
385 boolean
386 breakarm(ptr)
387 register struct permonst *ptr;
388 {
389     if (sliparm(ptr))
390         return FALSE;
391
392     return (boolean) (bigmonst(ptr)
393                       || (ptr->msize > MZ_SMALL && !humanoid(ptr))
394                       /* special cases of humanoids that cannot wear suits */
395                       || ptr == &mons[PM_MARILITH]
396                       || ptr == &mons[PM_WINGED_GARGOYLE]);
397 }
398
399 /* creature sticks other creatures it hits */
400 boolean
401 sticks(ptr)
402 register struct permonst *ptr;
403 {
404     return (boolean) (dmgtype(ptr, AD_STCK) || dmgtype(ptr, AD_WRAP)
405                       || attacktype(ptr, AT_HUGS));
406 }
407
408 /* some monster-types can't vomit */
409 boolean
410 cantvomit(ptr)
411 struct permonst *ptr;
412 {
413     /* rats and mice are incapable of vomiting;
414        which other creatures have the same limitation? */
415     if (ptr->mlet == S_RODENT && ptr != &mons[PM_ROCK_MOLE]
416         && ptr != &mons[PM_WOODCHUCK])
417         return TRUE;
418     return FALSE;
419 }
420
421 /* number of horns this type of monster has on its head */
422 int
423 num_horns(ptr)
424 struct permonst *ptr;
425 {
426     switch (monsndx(ptr)) {
427     case PM_HORNED_DEVIL: /* ? "more than one" */
428     case PM_MINOTAUR:
429     case PM_ASMODEUS:
430     case PM_BALROG:
431         return 2;
432     case PM_WHITE_UNICORN:
433     case PM_GRAY_UNICORN:
434     case PM_BLACK_UNICORN:
435     case PM_KI_RIN:
436         return 1;
437     default:
438         break;
439     }
440     return 0;
441 }
442
443 /* does monster-type deal out a particular type of damage from a particular
444    type of attack? */
445 struct attack *
446 dmgtype_fromattack(ptr, dtyp, atyp)
447 struct permonst *ptr;
448 int dtyp, atyp;
449 {
450     struct attack *a;
451
452     for (a = &ptr->mattk[0]; a < &ptr->mattk[NATTK]; a++)
453         if (a->adtyp == dtyp && (atyp == AT_ANY || a->aatyp == atyp))
454             return a;
455     return (struct attack *) 0;
456 }
457
458 /* does monster-type deal out a particular type of damage from any attack */
459 boolean
460 dmgtype(ptr, dtyp)
461 struct permonst *ptr;
462 int dtyp;
463 {
464     return dmgtype_fromattack(ptr, dtyp, AT_ANY) ? TRUE : FALSE;
465 }
466
467 /* returns the maximum damage a defender can do to the attacker via
468    a passive defense */
469 int
470 max_passive_dmg(mdef, magr)
471 register struct monst *mdef, *magr;
472 {
473     int i, dmg = 0, multi2 = 0;
474     uchar adtyp;
475
476     /* each attack by magr can result in passive damage */
477     for (i = 0; i < NATTK; i++)
478         switch (magr->data->mattk[i].aatyp) {
479         case AT_CLAW:
480         case AT_BITE:
481         case AT_KICK:
482         case AT_BUTT:
483         case AT_TUCH:
484         case AT_STNG:
485         case AT_HUGS:
486         case AT_ENGL:
487         case AT_TENT:
488         case AT_WEAP:
489             multi2++;
490             break;
491         default:
492             break;
493         }
494
495     for (i = 0; i < NATTK; i++)
496         if (mdef->data->mattk[i].aatyp == AT_NONE
497             || mdef->data->mattk[i].aatyp == AT_BOOM) {
498             adtyp = mdef->data->mattk[i].adtyp;
499             if ((adtyp == AD_ACID && !resists_acid(magr))
500                 || (adtyp == AD_COLD && !resists_cold(magr))
501                 || (adtyp == AD_FIRE && !resists_fire(magr))
502                 || (adtyp == AD_ELEC && !resists_elec(magr))
503                 || adtyp == AD_PHYS) {
504                 dmg = mdef->data->mattk[i].damn;
505                 if (!dmg)
506                     dmg = mdef->data->mlevel + 1;
507                 dmg *= mdef->data->mattk[i].damd;
508             } else
509                 dmg = 0;
510
511             return dmg * multi2;
512         }
513     return 0;
514 }
515
516 /* determine whether two monster types are from the same species */
517 boolean
518 same_race(pm1, pm2)
519 struct permonst *pm1, *pm2;
520 {
521     char let1 = pm1->mlet, let2 = pm2->mlet;
522
523     if (pm1 == pm2)
524         return TRUE; /* exact match */
525     /* player races have their own predicates */
526     if (is_human(pm1))
527         return is_human(pm2);
528     if (is_elf(pm1))
529         return is_elf(pm2);
530     if (is_dwarf(pm1))
531         return is_dwarf(pm2);
532     if (is_gnome(pm1))
533         return is_gnome(pm2);
534     if (is_orc(pm1))
535         return is_orc(pm2);
536     /* other creatures are less precise */
537     if (is_giant(pm1))
538         return is_giant(pm2); /* open to quibbling here */
539     if (is_golem(pm1))
540         return is_golem(pm2); /* even moreso... */
541     if (is_mind_flayer(pm1))
542         return is_mind_flayer(pm2);
543     if (let1 == S_KOBOLD || pm1 == &mons[PM_KOBOLD_ZOMBIE]
544         || pm1 == &mons[PM_KOBOLD_MUMMY])
545         return (let2 == S_KOBOLD || pm2 == &mons[PM_KOBOLD_ZOMBIE]
546                 || pm2 == &mons[PM_KOBOLD_MUMMY]);
547     if (let1 == S_OGRE)
548         return (let2 == S_OGRE);
549     if (let1 == S_NYMPH)
550         return (let2 == S_NYMPH);
551     if (let1 == S_CENTAUR)
552         return (let2 == S_CENTAUR);
553     if (is_unicorn(pm1))
554         return is_unicorn(pm2);
555     if (let1 == S_DRAGON)
556         return (let2 == S_DRAGON);
557     if (let1 == S_NAGA)
558         return (let2 == S_NAGA);
559     /* other critters get steadily messier */
560     if (is_rider(pm1))
561         return is_rider(pm2); /* debatable */
562     if (is_minion(pm1))
563         return is_minion(pm2); /* [needs work?] */
564     /* tengu don't match imps (first test handled case of both being tengu) */
565     if (pm1 == &mons[PM_TENGU] || pm2 == &mons[PM_TENGU])
566         return FALSE;
567     if (let1 == S_IMP)
568         return (let2 == S_IMP);
569     /* and minor demons (imps) don't match major demons */
570     else if (let2 == S_IMP)
571         return FALSE;
572     if (is_demon(pm1))
573         return is_demon(pm2);
574     if (is_undead(pm1)) {
575         if (let1 == S_ZOMBIE)
576             return (let2 == S_ZOMBIE);
577         if (let1 == S_MUMMY)
578             return (let2 == S_MUMMY);
579         if (let1 == S_VAMPIRE)
580             return (let2 == S_VAMPIRE);
581         if (let1 == S_LICH)
582             return (let2 == S_LICH);
583         if (let1 == S_WRAITH)
584             return (let2 == S_WRAITH);
585         if (let1 == S_GHOST)
586             return (let2 == S_GHOST);
587     } else if (is_undead(pm2))
588         return FALSE;
589
590     /* check for monsters which grow into more mature forms */
591     if (let1 == let2) {
592         int m1 = monsndx(pm1), m2 = monsndx(pm2), prv, nxt;
593
594         /* we know m1 != m2 (very first check above); test all smaller
595            forms of m1 against m2, then all larger ones; don't need to
596            make the corresponding tests for variants of m2 against m1 */
597         for (prv = m1, nxt = big_to_little(m1); nxt != prv;
598              prv = nxt, nxt = big_to_little(nxt))
599             if (nxt == m2)
600                 return TRUE;
601         for (prv = m1, nxt = little_to_big(m1); nxt != prv;
602              prv = nxt, nxt = little_to_big(nxt))
603             if (nxt == m2)
604                 return TRUE;
605     }
606     /* not caught by little/big handling */
607     if (pm1 == &mons[PM_GARGOYLE] || pm1 == &mons[PM_WINGED_GARGOYLE])
608         return (pm2 == &mons[PM_GARGOYLE]
609                 || pm2 == &mons[PM_WINGED_GARGOYLE]);
610     if (pm1 == &mons[PM_KILLER_BEE] || pm1 == &mons[PM_QUEEN_BEE])
611         return (pm2 == &mons[PM_KILLER_BEE] || pm2 == &mons[PM_QUEEN_BEE]);
612
613     if (is_longworm(pm1))
614         return is_longworm(pm2); /* handles tail */
615     /* [currently there's no reason to bother matching up
616         assorted bugs and blobs with their closest variants] */
617     /* didn't match */
618     return FALSE;
619 }
620
621 /* return an index into the mons array */
622 int
623 monsndx(ptr)
624 struct permonst *ptr;
625 {
626     register int i;
627
628     i = (int) (ptr - &mons[0]);
629     if (i < LOW_PM || i >= NUMMONS) {
630         panic("monsndx - could not index monster (%s)",
631               fmt_ptr((genericptr_t) ptr));
632         return NON_PM; /* will not get here */
633     }
634     return i;
635 }
636
637 /* for handling alternate spellings */
638 struct alt_spl {
639     const char *name;
640     short pm_val;
641 };
642
643 /* figure out what type of monster a user-supplied string is specifying */
644 int
645 name_to_mon(in_str)
646 const char *in_str;
647 {
648     /* Be careful.  We must check the entire string in case it was
649      * something such as "ettin zombie corpse".  The calling routine
650      * doesn't know about the "corpse" until the monster name has
651      * already been taken off the front, so we have to be able to
652      * read the name with extraneous stuff such as "corpse" stuck on
653      * the end.
654      * This causes a problem for names which prefix other names such
655      * as "ettin" on "ettin zombie".  In this case we want the _longest_
656      * name which exists.
657      * This also permits plurals created by adding suffixes such as 's'
658      * or 'es'.  Other plurals must still be handled explicitly.
659      */
660     register int i;
661     register int mntmp = NON_PM;
662     register char *s, *str, *term;
663     char buf[BUFSZ];
664     int len, slen;
665
666     str = strcpy(buf, in_str);
667
668     if (!strncmp(str, "a ", 2))
669         str += 2;
670     else if (!strncmp(str, "an ", 3))
671         str += 3;
672     else if (!strncmp(str, "the ", 4))
673         str += 4;
674
675     slen = strlen(str);
676     term = str + slen;
677
678     if ((s = strstri(str, "vortices")) != 0)
679         Strcpy(s + 4, "ex");
680     /* be careful with "ies"; "priest", "zombies" */
681     else if (slen > 3 && !strcmpi(term - 3, "ies")
682              && (slen < 7 || strcmpi(term - 7, "zombies")))
683         Strcpy(term - 3, "y");
684     /* luckily no monster names end in fe or ve with ves plurals */
685     else if (slen > 3 && !strcmpi(term - 3, "ves"))
686         Strcpy(term - 3, "f");
687
688     slen = strlen(str); /* length possibly needs recomputing */
689
690     {
691         static const struct alt_spl names[] = {
692             /* Alternate spellings */
693             { "grey dragon", PM_GRAY_DRAGON },
694             { "baby grey dragon", PM_BABY_GRAY_DRAGON },
695             { "grey unicorn", PM_GRAY_UNICORN },
696             { "grey ooze", PM_GRAY_OOZE },
697             { "gray-elf", PM_GREY_ELF },
698             { "mindflayer", PM_MIND_FLAYER },
699             { "master mindflayer", PM_MASTER_MIND_FLAYER },
700             /* More alternates; priest and priestess are separate monster
701                types but that isn't the case for {aligned,high} priests */
702             { "aligned priestess", PM_ALIGNED_PRIEST },
703             { "high priestess", PM_HIGH_PRIEST },
704             /* Inappropriate singularization by -ves check above */
705             { "master of thief", PM_MASTER_OF_THIEVES },
706             /* Potential misspellings where we want to avoid falling back
707                to the rank title prefix (input has been singularized) */
708             { "master thief", PM_MASTER_OF_THIEVES },
709             { "master of assassin", PM_MASTER_ASSASSIN },
710             /* Outdated names */
711             { "invisible stalker", PM_STALKER },
712             { "high-elf", PM_ELVENKING }, /* PM_HIGH_ELF is obsolete */
713             { "halfling", PM_HOBBIT },    /* potential guess for polyself */
714             /* Hyphenated names */
715             { "ki rin", PM_KI_RIN },
716             { "uruk hai", PM_URUK_HAI },
717             { "orc captain", PM_ORC_CAPTAIN },
718             { "woodland elf", PM_WOODLAND_ELF },
719             { "green elf", PM_GREEN_ELF },
720             { "grey elf", PM_GREY_ELF },
721             { "gray elf", PM_GREY_ELF },
722             { "elf lord", PM_ELF_LORD },
723             { "olog hai", PM_OLOG_HAI },
724             { "arch lich", PM_ARCH_LICH },
725             /* Some irregular plurals */
726             { "incubi", PM_INCUBUS },
727             { "succubi", PM_SUCCUBUS },
728             { "violet fungi", PM_VIOLET_FUNGUS },
729             { "homunculi", PM_HOMUNCULUS },
730             { "baluchitheria", PM_BALUCHITHERIUM },
731             { "lurkers above", PM_LURKER_ABOVE },
732             { "cavemen", PM_CAVEMAN },
733             { "cavewomen", PM_CAVEWOMAN },
734             { "djinn", PM_DJINNI },
735             { "mumakil", PM_MUMAK },
736             { "erinyes", PM_ERINYS },
737             /* end of list */
738             { 0, NON_PM }
739         };
740         register const struct alt_spl *namep;
741
742         for (namep = names; namep->name; namep++)
743             if (!strncmpi(str, namep->name, (int) strlen(namep->name)))
744                 return namep->pm_val;
745     }
746
747     for (len = 0, i = LOW_PM; i < NUMMONS; i++) {
748         register int m_i_len = strlen(mons[i].mname);
749
750         if (m_i_len > len && !strncmpi(mons[i].mname, str, m_i_len)) {
751             if (m_i_len == slen) {
752                 return i; /* exact match */
753             } else if (slen > m_i_len
754                        && (str[m_i_len] == ' '
755                            || !strcmpi(&str[m_i_len], "s")
756                            || !strncmpi(&str[m_i_len], "s ", 2)
757                            || !strcmpi(&str[m_i_len], "'")
758                            || !strncmpi(&str[m_i_len], "' ", 2)
759                            || !strcmpi(&str[m_i_len], "'s")
760                            || !strncmpi(&str[m_i_len], "'s ", 3)
761                            || !strcmpi(&str[m_i_len], "es")
762                            || !strncmpi(&str[m_i_len], "es ", 3))) {
763                 mntmp = i;
764                 len = m_i_len;
765             }
766         }
767     }
768     if (mntmp == NON_PM)
769         mntmp = title_to_mon(str, (int *) 0, (int *) 0);
770     return mntmp;
771 }
772
773 /* monster class from user input; used for genocide and controlled polymorph;
774    returns 0 rather than MAXMCLASSES if no match is found */
775 int
776 name_to_monclass(in_str, mndx_p)
777 const char *in_str;
778 int *mndx_p;
779 {
780     /* Single letters are matched against def_monsyms[].sym; words
781        or phrases are first matched against def_monsyms[].explain
782        to check class description; if not found there, then against
783        mons[].mname to test individual monster types.  Input can be a
784        substring of the full description or mname, but to be accepted,
785        such partial matches must start at beginning of a word.  Some
786        class descriptions include "foo or bar" and "foo or other foo"
787        so we don't want to accept "or", "other", "or other" there. */
788     static NEARDATA const char *const falsematch[] = {
789         /* multiple-letter input which matches any of these gets rejected */
790         "an", "the", "or", "other", "or other", 0
791     };
792     /* positive pm_val => specific monster; negative => class */
793     static NEARDATA const struct alt_spl truematch[] = {
794         /* "long worm" won't match "worm" class but would accidentally match
795            "long worm tail" class before the comparison with monster types */
796         { "long worm", PM_LONG_WORM },
797         /* matches wrong--or at least suboptimal--class */
798         { "demon", -S_DEMON }, /* hits "imp or minor demon" */
799         /* matches specific monster (overly restrictive) */
800         { "devil", -S_DEMON }, /* always "horned devil" */
801         /* some plausible guesses which need help */
802         { "bug", -S_XAN },  /* would match bugbear... */
803         { "fish", -S_EEL }, /* wouldn't match anything */
804         /* end of list */
805         { 0, NON_PM }
806     };
807     const char *p, *x;
808     int i;
809
810     if (mndx_p)
811         *mndx_p = NON_PM; /* haven't [yet] matched a specific type */
812
813     if (!in_str || !in_str[0]) {
814         /* empty input */
815         return 0;
816     } else if (!in_str[1]) {
817         /* single character */
818         i = def_char_to_monclass(*in_str);
819         if (i == S_MIMIC_DEF) { /* ']' -> 'm' */
820             i = S_MIMIC;
821         } else if (i == S_WORM_TAIL) { /* '~' -> 'w' */
822             i = S_WORM;
823             if (mndx_p)
824                 *mndx_p = PM_LONG_WORM;
825         } else if (i == MAXMCLASSES) /* maybe 'I' */
826             i = (*in_str == DEF_INVISIBLE) ? S_invisible : 0;
827         return i;
828     } else {
829         /* multiple characters */
830         in_str = makesingular(in_str);
831         /* check for special cases */
832         for (i = 0; falsematch[i]; i++)
833             if (!strcmpi(in_str, falsematch[i]))
834                 return 0;
835         for (i = 0; truematch[i].name; i++)
836             if (!strcmpi(in_str, truematch[i].name)) {
837                 i = truematch[i].pm_val;
838                 if (i < 0)
839                     return -i; /* class */
840                 if (mndx_p)
841                     *mndx_p = i; /* monster */
842                 return mons[i].mlet;
843             }
844         /* check monster class descriptions */
845         for (i = 1; i < MAXMCLASSES; i++) {
846             x = def_monsyms[i].explain;
847             if ((p = strstri(x, in_str)) != 0 && (p == x || *(p - 1) == ' '))
848                 return i;
849         }
850         /* check individual species names; not as thorough as mon_to_name()
851            but our caller can call that directly if desired */
852         for (i = LOW_PM; i < NUMMONS; i++) {
853             x = mons[i].mname;
854             if ((p = strstri(x, in_str)) != 0
855                 && (p == x || *(p - 1) == ' ')) {
856                 if (mndx_p)
857                     *mndx_p = i;
858                 return mons[i].mlet;
859             }
860         }
861     }
862     return 0;
863 }
864
865 /* returns 3 values (0=male, 1=female, 2=none) */
866 int
867 gender(mtmp)
868 register struct monst *mtmp;
869 {
870     if (is_neuter(mtmp->data))
871         return 2;
872     return mtmp->female;
873 }
874
875 /* Like gender(), but lower animals and such are still "it".
876    This is the one we want to use when printing messages. */
877 int
878 pronoun_gender(mtmp)
879 register struct monst *mtmp;
880 {
881     if (is_neuter(mtmp->data) || !canspotmon(mtmp))
882         return 2;
883     return (humanoid(mtmp->data) || (mtmp->data->geno & G_UNIQ)
884             || type_is_pname(mtmp->data)) ? (int) mtmp->female : 2;
885 }
886
887 /* used for nearby monsters when you go to another level */
888 boolean
889 levl_follower(mtmp)
890 struct monst *mtmp;
891 {
892     if (mtmp == u.usteed)
893         return TRUE;
894
895     /* Wizard with Amulet won't bother trying to follow across levels */
896     if (mtmp->iswiz && mon_has_amulet(mtmp))
897         return FALSE;
898     /* some monsters will follow even while intending to flee from you */
899     if (mtmp->mtame || mtmp->iswiz || is_fshk(mtmp))
900         return TRUE;
901     /* stalking types follow, but won't when fleeing unless you hold
902        the Amulet */
903     return (boolean) ((mtmp->data->mflags2 & M2_STALK)
904                       && (!mtmp->mflee || u.uhave.amulet));
905 }
906
907 static const short grownups[][2] = {
908     { PM_CHICKATRICE, PM_COCKATRICE },
909     { PM_LITTLE_DOG, PM_DOG },
910     { PM_DOG, PM_LARGE_DOG },
911     { PM_HELL_HOUND_PUP, PM_HELL_HOUND },
912     { PM_WINTER_WOLF_CUB, PM_WINTER_WOLF },
913     { PM_KITTEN, PM_HOUSECAT },
914     { PM_HOUSECAT, PM_LARGE_CAT },
915     { PM_PONY, PM_HORSE },
916     { PM_HORSE, PM_WARHORSE },
917     { PM_KOBOLD, PM_LARGE_KOBOLD },
918     { PM_LARGE_KOBOLD, PM_KOBOLD_LORD },
919     { PM_GNOME, PM_GNOME_LORD },
920     { PM_GNOME_LORD, PM_GNOME_KING },
921     { PM_DWARF, PM_DWARF_LORD },
922     { PM_DWARF_LORD, PM_DWARF_KING },
923     { PM_MIND_FLAYER, PM_MASTER_MIND_FLAYER },
924     { PM_ORC, PM_ORC_CAPTAIN },
925     { PM_HILL_ORC, PM_ORC_CAPTAIN },
926     { PM_MORDOR_ORC, PM_ORC_CAPTAIN },
927     { PM_URUK_HAI, PM_ORC_CAPTAIN },
928     { PM_SEWER_RAT, PM_GIANT_RAT },
929     { PM_CAVE_SPIDER, PM_GIANT_SPIDER },
930     { PM_OGRE, PM_OGRE_LORD },
931     { PM_OGRE_LORD, PM_OGRE_KING },
932     { PM_ELF, PM_ELF_LORD },
933     { PM_WOODLAND_ELF, PM_ELF_LORD },
934     { PM_GREEN_ELF, PM_ELF_LORD },
935     { PM_GREY_ELF, PM_ELF_LORD },
936     { PM_ELF_LORD, PM_ELVENKING },
937     { PM_LICH, PM_DEMILICH },
938     { PM_DEMILICH, PM_MASTER_LICH },
939     { PM_MASTER_LICH, PM_ARCH_LICH },
940     { PM_VAMPIRE, PM_VAMPIRE_LORD },
941     { PM_BAT, PM_GIANT_BAT },
942     { PM_BABY_GRAY_DRAGON, PM_GRAY_DRAGON },
943     { PM_BABY_SILVER_DRAGON, PM_SILVER_DRAGON },
944 #if 0 /* DEFERRED */
945     {PM_BABY_SHIMMERING_DRAGON, PM_SHIMMERING_DRAGON},
946 #endif
947     { PM_BABY_RED_DRAGON, PM_RED_DRAGON },
948     { PM_BABY_WHITE_DRAGON, PM_WHITE_DRAGON },
949     { PM_BABY_ORANGE_DRAGON, PM_ORANGE_DRAGON },
950     { PM_BABY_BLACK_DRAGON, PM_BLACK_DRAGON },
951     { PM_BABY_BLUE_DRAGON, PM_BLUE_DRAGON },
952     { PM_BABY_GREEN_DRAGON, PM_GREEN_DRAGON },
953     { PM_BABY_YELLOW_DRAGON, PM_YELLOW_DRAGON },
954     { PM_RED_NAGA_HATCHLING, PM_RED_NAGA },
955     { PM_BLACK_NAGA_HATCHLING, PM_BLACK_NAGA },
956     { PM_GOLDEN_NAGA_HATCHLING, PM_GOLDEN_NAGA },
957     { PM_GUARDIAN_NAGA_HATCHLING, PM_GUARDIAN_NAGA },
958     { PM_SMALL_MIMIC, PM_LARGE_MIMIC },
959     { PM_LARGE_MIMIC, PM_GIANT_MIMIC },
960     { PM_BABY_LONG_WORM, PM_LONG_WORM },
961     { PM_BABY_PURPLE_WORM, PM_PURPLE_WORM },
962     { PM_BABY_CROCODILE, PM_CROCODILE },
963     { PM_SOLDIER, PM_SERGEANT },
964     { PM_SERGEANT, PM_LIEUTENANT },
965     { PM_LIEUTENANT, PM_CAPTAIN },
966     { PM_WATCHMAN, PM_WATCH_CAPTAIN },
967     { PM_ALIGNED_PRIEST, PM_HIGH_PRIEST },
968     { PM_STUDENT, PM_ARCHEOLOGIST },
969     { PM_ATTENDANT, PM_HEALER },
970     { PM_PAGE, PM_KNIGHT },
971     { PM_ACOLYTE, PM_PRIEST },
972     { PM_APPRENTICE, PM_WIZARD },
973     { PM_MANES, PM_LEMURE },
974     { PM_KEYSTONE_KOP, PM_KOP_SERGEANT },
975     { PM_KOP_SERGEANT, PM_KOP_LIEUTENANT },
976     { PM_KOP_LIEUTENANT, PM_KOP_KAPTAIN },
977     { NON_PM, NON_PM }
978 };
979
980 int
981 little_to_big(montype)
982 int montype;
983 {
984     register int i;
985
986     for (i = 0; grownups[i][0] >= LOW_PM; i++)
987         if (montype == grownups[i][0]) {
988             montype = grownups[i][1];
989             break;
990         }
991     return montype;
992 }
993
994 int
995 big_to_little(montype)
996 int montype;
997 {
998     register int i;
999
1000     for (i = 0; grownups[i][0] >= LOW_PM; i++)
1001         if (montype == grownups[i][1]) {
1002             montype = grownups[i][0];
1003             break;
1004         }
1005     return montype;
1006 }
1007
1008 /*
1009  * Return the permonst ptr for the race of the monster.
1010  * Returns correct pointer for non-polymorphed and polymorphed
1011  * player.  It does not return a pointer to player role character.
1012  */
1013 const struct permonst *
1014 raceptr(mtmp)
1015 struct monst *mtmp;
1016 {
1017     if (mtmp == &youmonst && !Upolyd)
1018         return &mons[urace.malenum];
1019     else
1020         return mtmp->data;
1021 }
1022
1023 /*JP
1024 static const char *levitate[4] = { "float", "Float", "wobble", "Wobble" };
1025 */
1026 static const char *levitate[4] = { "\95\82\82­", "\95\82\82­", "\82æ\82ë\82ß\82­", "\82æ\82ë\82ß\82­" };
1027 /*JP
1028 static const char *flys[4] = { "fly", "Fly", "flutter", "Flutter" };
1029 */
1030 static const char *flys[4] = { "\94ò\82Ô", "\94ò\82Ô", "\82Í\82½\82ß\82­", "\82Í\82½\82ß\82­" };
1031 /*JP
1032 static const char *flyl[4] = { "fly", "Fly", "stagger", "Stagger" };
1033 */
1034 static const char *flyl[4] = { "\94ò\82Ô", "\94ò\82Ô", "\82æ\82ë\82ß\82­", "\82æ\82ë\82ß\82­" };
1035 /*JP
1036 static const char *slither[4] = { "slither", "Slither", "falter", "Falter" };
1037 */
1038 static const char *slither[4] = { "\8a\8a\82é", "\8a\8a\82é", "\82½\82\82ë\82®", "\82½\82\82ë\82®" };
1039 /*JP
1040 static const char *ooze[4] = { "ooze", "Ooze", "tremble", "Tremble" };
1041 */
1042 static const char *ooze[4] = { "\82É\82\82Ý\8fo\82é", "\82É\82\82Ý\8fo\82é", "\90k\82¦\82é", "\90k\82¦\82é" };
1043 /*JP
1044 static const char *immobile[4] = { "wiggle", "Wiggle", "pulsate", "Pulsate" };
1045 */
1046 static const char *immobile[4] = { "\90k\82¦\82é", "\90k\82¦\82é", "\90k\82¦\82é", "\90k\82¦\82é" };
1047 /*JP
1048 static const char *crawl[4] = { "crawl", "Crawl", "falter", "Falter" };
1049 */
1050 static const char *crawl[4] = { "\82Í\82¢\82¸\82é", "\82Í\82¢\82¸\82é", "\82½\82\82ë\82®", "\82½\82\82ë\82®" };
1051
1052 const char *
1053 locomotion(ptr, def)
1054 const struct permonst *ptr;
1055 const char *def;
1056 {
1057     int capitalize = (*def == highc(*def));
1058
1059     return (is_floater(ptr) ? levitate[capitalize]
1060             : (is_flyer(ptr) && ptr->msize <= MZ_SMALL) ? flys[capitalize]
1061               : (is_flyer(ptr) && ptr->msize > MZ_SMALL) ? flyl[capitalize]
1062                 : slithy(ptr) ? slither[capitalize]
1063                   : amorphous(ptr) ? ooze[capitalize]
1064                     : !ptr->mmove ? immobile[capitalize]
1065                       : nolimbs(ptr) ? crawl[capitalize]
1066                         : def);
1067 }
1068
1069 const char *
1070 stagger(ptr, def)
1071 const struct permonst *ptr;
1072 const char *def;
1073 {
1074     int capitalize = 2 + (*def == highc(*def));
1075
1076     return (is_floater(ptr) ? levitate[capitalize]
1077             : (is_flyer(ptr) && ptr->msize <= MZ_SMALL) ? flys[capitalize]
1078               : (is_flyer(ptr) && ptr->msize > MZ_SMALL) ? flyl[capitalize]
1079                 : slithy(ptr) ? slither[capitalize]
1080                   : amorphous(ptr) ? ooze[capitalize]
1081                     : !ptr->mmove ? immobile[capitalize]
1082                       : nolimbs(ptr) ? crawl[capitalize]
1083                         : def);
1084 }
1085 #if 1 /*JP*/
1086 static const char *levitate2 = "\95\82\82«\8fo\82½";
1087 static const char *fly2 = "\94ò\82Ñ\8fo\82½";
1088 static const char *slither2 = "\8a\8a\82è\8fo\82½";
1089 static const char *ooze2 = "\82É\82\82Ý\8fo\82½";
1090 static const char *crawl2 = "\82Í\82¢\82¸\82è\8fo\82½";
1091
1092 const char *
1093 jumpedthrough(ptr, def)
1094 const struct permonst *ptr;
1095 const char *def;
1096 {
1097         return (
1098             is_floater(ptr) ? levitate2 :
1099             is_flyer(ptr)   ? fly2 :
1100             slithy(ptr)     ? slither2 :
1101             amorphous(ptr)  ? ooze2 :
1102             nolimbs(ptr)    ? crawl2 :
1103             def
1104             );
1105 }
1106 #endif
1107
1108 /* return phrase describing the effect of fire attack on a type of monster */
1109 const char *
1110 on_fire(mptr, mattk)
1111 struct permonst *mptr;
1112 struct attack *mattk;
1113 {
1114     const char *what;
1115
1116     switch (monsndx(mptr)) {
1117     case PM_FLAMING_SPHERE:
1118     case PM_FIRE_VORTEX:
1119     case PM_FIRE_ELEMENTAL:
1120     case PM_SALAMANDER:
1121 /*JP
1122         what = "already on fire";
1123 */
1124         what = "\82·\82Å\82É\89\8a\82É\82Â\82Â\82Ü\82ê\82Ä\82¢\82é";
1125         break;
1126     case PM_WATER_ELEMENTAL:
1127     case PM_FOG_CLOUD:
1128     case PM_STEAM_VORTEX:
1129 /*JP
1130         what = "boiling";
1131 */
1132         what = "\95¦\93«\82µ\82½";
1133         break;
1134     case PM_ICE_VORTEX:
1135     case PM_GLASS_GOLEM:
1136 /*JP
1137         what = "melting";
1138 */
1139         what = "\97n\82¯\82½";
1140         break;
1141     case PM_STONE_GOLEM:
1142     case PM_CLAY_GOLEM:
1143     case PM_GOLD_GOLEM:
1144     case PM_AIR_ELEMENTAL:
1145     case PM_EARTH_ELEMENTAL:
1146     case PM_DUST_VORTEX:
1147     case PM_ENERGY_VORTEX:
1148 /*JP
1149         what = "heating up";
1150 */
1151         what = "\94M\82­\82È\82Á\82½";
1152         break;
1153     default:
1154 /*JP
1155         what = (mattk->aatyp == AT_HUGS) ? "being roasted" : "on fire";
1156 */
1157         what = (mattk->aatyp == AT_HUGS) ? "\8aÛ\8fÄ\82¯\82É\82È\82Á\82½" : "\89Î\82¾\82é\82Ü\82É\82È\82Á\82½";
1158         break;
1159     }
1160     return what;
1161 }
1162
1163 /*
1164  * Returns:
1165  *      True if monster is presumed to have a sense of smell.
1166  *      False if monster definitely does not have a sense of smell.
1167  *
1168  * Do not base this on presence of a head or nose, since many
1169  * creatures sense smells other ways (feelers, forked-tongues, etc.)
1170  * We're assuming all insects can smell at a distance too.
1171  */
1172 boolean
1173 olfaction(mdat)
1174 struct permonst *mdat;
1175 {
1176     if (is_golem(mdat)
1177         || mdat->mlet == S_EYE /* spheres  */
1178         || mdat->mlet == S_JELLY || mdat->mlet == S_PUDDING
1179         || mdat->mlet == S_BLOB || mdat->mlet == S_VORTEX
1180         || mdat->mlet == S_ELEMENTAL
1181         || mdat->mlet == S_FUNGUS /* mushrooms and fungi */
1182         || mdat->mlet == S_LIGHT)
1183         return FALSE;
1184     return TRUE;
1185 }
1186
1187 /*mondata.c*/