Source:NetHack 5.0.0/src/mthrowu.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to mthrowu.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/mthrowu.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	mthrowu.c	$NHDT-Date: 1737392015 2025/01/20 08:53:35 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.173 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Pasi Kallinen, 2016. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. staticfn int monmulti(struct monst *, struct obj *, struct obj *);
9. staticfn void monshoot(struct monst *, struct obj *, struct obj *);
10. staticfn boolean ucatchgem(struct obj *, struct monst *);
11. staticfn boolean u_catch_thrown_obj(struct obj *);
12. staticfn const char *breathwep_name(int);
13. staticfn boolean drop_throw(struct obj *, boolean, coordxy, coordxy);
14. staticfn boolean blocking_terrain(coordxy, coordxy);
15. staticfn int m_lined_up(struct monst *, struct monst *) NONNULLARG12;
16. staticfn void return_from_mtoss(struct monst *, struct obj *, boolean);
17. 
18. #define URETREATING(x, y) \
19.     (distmin(u.ux, u.uy, x, y) > distmin(u.ux0, u.uy0, x, y))
20. 
21. /*
22.  * Keep consistent with breath weapons in zap.c, and AD_* in monattk.h.
23.  */
24. static NEARDATA const char *breathwep[] = {
25.     "fragments", "fire", "frost", "sleep gas", "a disintegration blast",
26.     "lightning", "poison gas", "acid", "strange breath #8",
27.     "strange breath #9"
28. };
29. 
30. /* hallucinatory ray types */
31. static const char *const hallublasts[] = {
32.     "asteroids", "beads", "bubbles", "butterflies", "champagne", "chaos",
33.     "coins", "cotton candy", "crumbs", "dark matter", "darkness", "data",
34.     "dust specks", "emoticons", "emotions", "entropy", "flowers", "foam",
35.     "fog", "gamma rays", "gelatin", "gemstones", "ghosts", "glass shards",
36.     "glitter", "good vibes", "gravel", "gravity", "gravy", "grawlixes",
37.     "holy light", "hornets", "hot air", "hyphens", "hypnosis", "infrared",
38.     "insects", "jargon", "laser beams", "leaves", "lightening", "logic gates",
39.     "magma", "marbles", "mathematics", "megabytes", "metal shavings",
40.     "metapatterns", "meteors", "mist", "mud", "music", "nanites", "needles",
41.     "noise", "nostalgia", "oil", "paint", "photons", "pixels", "plasma",
42.     "polarity", "powder", "powerups", "prismatic light", "pure logic",
43.     "purple", "radio waves", "rainbows", "rock music", "rocket fuel", "rope",
44.     "sadness", "salt", "sand", "scrolls", "sludge", "smileys", "snowflakes",
45.     "sparkles", "specularity", "spores", "stars", "steam", "tetrahedrons",
46.     "text", "the past", "tornadoes", "toxic waste", "ultraviolet light",
47.     "viruses", "water", "waveforms", "wind", "X-rays", "zorkmids"
48. };
49. 
50. /* Return a random hallucinatory blast. */
51. const char *
52. rnd_hallublast(void)
53. {
54.     return ROLL_FROM(hallublasts);
55. }
56. 
57. boolean
58. m_has_launcher_and_ammo(struct monst *mtmp)
59. {
60.     struct obj *mwep = MON_WEP(mtmp);
61. 
62.     if (mwep && is_launcher(mwep)) {
63.         struct obj *otmp;
64. 
65.         for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
66.             if (ammo_and_launcher(otmp, mwep))
67.                 return TRUE;
68.     }
69.     return FALSE;
70. }
71. 
72. /* hero is hit by something other than a monster (though it could be a
73.    missile thrown or shot by a monster) */
74. int
75. thitu(
76.     int tlev, /* pseudo-level used when deciding whether to hit hero's AC */
77.     int dam,
78.     struct obj **objp,
79.     const char *name) /* if null, then format `*objp' */
80. {
81.     struct obj *obj = objp ? *objp : 0;
82.     const char *onm, *knm;
83.     boolean is_acid, named = (name != 0);
84.     int kprefix = KILLED_BY_AN, dieroll;
85.     char onmbuf[BUFSZ], knmbuf[BUFSZ];
86. 
87.     if (!name) {
88.         if (!obj)
89.             panic("thitu: name & obj both null?");
90.         name = strcpy(onmbuf,
91.                       (obj->quan > 1L) ? doname(obj) : mshot_xname(obj));
92.         knm = strcpy(knmbuf, killer_xname(obj));
93.         kprefix = KILLED_BY; /* killer_name supplies "an" if warranted */
94.     } else {
95.         knm = name;
96.         /* [perhaps ought to check for plural here too] */
97.         if (!strncmpi(name, "the ", 4) || !strncmpi(name, "an ", 3)
98.             || !strncmpi(name, "a ", 2))
99.             kprefix = KILLED_BY;
100.     }
101.     onm = (obj && obj_is_pname(obj)) ? the(name)
102.           : (obj && obj->quan > 1L) ? name
103.             : an(name);
104.     is_acid = (obj && obj->otyp == ACID_VENOM);
105. 
106.     if (u.uac + tlev <= (dieroll = rnd(20))) {
107.         ++gm.mesg_given;
108.         if (Blind || !flags.verbose) {
109.             pline("It misses.");
110.         } else if (u.uac + tlev <= dieroll - 2) {
111.             if (onm != onmbuf)
112.                 Strcpy(onmbuf, onm); /* [modifiable buffer for upstart()] */
113.             pline("%s %s you.", upstart(onmbuf), vtense(onmbuf, "miss"));
114.         } else
115.             You("are almost hit by %s.", onm);
116.         return 0;
117.     } else {
118.         if (Blind || !flags.verbose)
119.             You("are hit%s", exclam(dam));
120.         else
121.             You("are hit by %s%s", onm, exclam(dam));
122. 
123.         if (is_acid && Acid_resistance) {
124.             pline("It doesn't seem to hurt you.");
125.             monstseesu(M_SEEN_ACID);
126.         } else if (obj && stone_missile(obj)
127.                    && passes_rocks(gy.youmonst.data)) {
128.             /* use 'named' as an approximation for "hitting from above";
129.                we avoid "passes through you" for horizontal flight path
130.                because missile stops and that wording would suggest that
131.                it should keep going */
132.             pline("It %s you.",
133.                   named ? "passes harmlessly through" : "doesn't harm");
134.         } else if (obj && obj->oclass == POTION_CLASS) {
135.             /* an explosion which scatters objects might hit hero with one
136.                (potions deliberately thrown at hero are handled by m_throw) */
137.             potionhit(&gy.youmonst, obj, POTHIT_OTHER_THROW);
138.             *objp = obj = 0; /* potionhit() uses up the potion */
139.         } else {
140.             if (obj && objects[obj->otyp].oc_material == SILVER
141.                 && Hate_silver) {
142.                 /* extra damage already applied by dmgval() */
143.                 pline_The("silver sears your flesh!");
144.                 exercise(A_CON, FALSE);
145.             }
146.             if (is_acid) {
147.                 pline("It burns!");
148.                 monstunseesu(M_SEEN_ACID);
149.             }
150.             losehp(dam, knm, kprefix); /* acid damage */
151.             exercise(A_STR, FALSE);
152.         }
153.         return 1;
154.     }
155. }
156. 
157. /* Be sure this corresponds with what happens to player-thrown objects in
158.  * dothrow.c (for consistency). --KAA
159.  * Returns FALSE if object still exists (not destroyed).
160.  */
161. staticfn boolean
162. drop_throw(
163.     struct obj *obj,
164.     boolean ohit,
165.     coordxy x,
166.     coordxy y)
167. {
168.     boolean broken;
169. 
170.     if (obj->otyp == CREAM_PIE || obj->oclass == VENOM_CLASS
171.         || (ohit && obj->otyp == EGG)) {
172.         broken = TRUE;
173.     } else {
174.         broken = (ohit && should_mulch_missile(obj));
175.     }
176. 
177.     if (broken) {
178.         delobj(obj);
179.     } else {
180.         if (down_gate(x, y) != -1)
181.             broken = ship_object(obj, x, y, FALSE);
182.         if (!broken) {
183.             struct monst *mtmp = m_at(x, y);
184.             if (!(broken = flooreffects(obj, x, y, "fall"))) {
185.                 place_object(obj, x, y);
186.                 if (!mtmp && u_at(x, y))
187.                     mtmp = &gy.youmonst;
188.                 if (mtmp && ohit)
189.                     passive_obj(mtmp, obj, (struct attack *) 0);
190.                 stackobj(obj);
191.             }
192.         }
193.     }
194.     gt.thrownobj = 0;
195.     return broken;
196. }
197. 
198. /* calculate multishot volley count for mtmp throwing otmp (if not ammo) or
199.    shooting otmp with mwep (if otmp is ammo and mwep appropriate launcher) */
200. staticfn int
201. monmulti(
202.     struct monst *mtmp,
203.     struct obj *otmp, struct obj *mwep)
204. {
205.     int multishot = 1;
206. 
207.     if (otmp->quan > 1L /* no point checking if there's only 1 */
208.         /* ammo requires corresponding launcher be wielded */
209.         && (is_ammo(otmp)
210.                ? matching_launcher(otmp, mwep)
211.                /* otherwise any stackable (non-ammo) weapon */
212.                : otmp->oclass == WEAPON_CLASS)
213.         && !mtmp->mconf) {
214.         /* Assumes lords are skilled, princes are expert */
215.         if (is_prince(mtmp->data))
216.             multishot += 2;
217.         else if (is_lord(mtmp->data))
218.             multishot++;
219.         /* fake players treated as skilled (regardless of role limits) */
220.         else if (is_mplayer(mtmp->data))
221.             multishot++;
222. 
223.         /* this portion is different from hero multishot; from slash'em?
224.          */
225.         /* Elven Craftsmanship makes for light, quick bows */
226.         if (otmp->otyp == ELVEN_ARROW && !otmp->cursed)
227.             multishot++;
228.         /* for arrow, we checked bow&arrow when entering block, but for
229.            bow, so far we've only validated that otmp is a weapon stack;
230.            need to verify that it's a stack of arrows rather than darts */
231.         if (mwep && mwep->otyp == ELVEN_BOW && ammo_and_launcher(otmp, mwep)
232.             && !mwep->cursed)
233.             multishot++;
234.         /* 1/3 of launcher enchantment */
235.         if (ammo_and_launcher(otmp, mwep) && mwep->spe > 1)
236.             multishot += (long) rounddiv(mwep->spe, 3);
237.         /* Some randomness */
238.         multishot = rnd((int) multishot);
239. 
240.         /* class bonus */
241.         multishot += multishot_class_bonus(monsndx(mtmp->data), otmp, mwep);
242. 
243.         /* racial bonus */
244.         if ((is_elf(mtmp->data) && otmp->otyp == ELVEN_ARROW
245.              && mwep && mwep->otyp == ELVEN_BOW)
246.             || (is_orc(mtmp->data) && otmp->otyp == ORCISH_ARROW
247.                 && mwep && mwep->otyp == ORCISH_BOW)
248.             || (is_gnome(mtmp->data) && otmp->otyp == CROSSBOW_BOLT
249.                 && mwep && mwep->otyp == CROSSBOW))
250.             multishot++;
251.     }
252. 
253.     if (otmp->quan < multishot)
254.         multishot = (int) otmp->quan;
255.     if (multishot < 1)
256.         multishot = 1;
257.     return multishot;
258. }
259. 
260. /* mtmp throws otmp, or shoots otmp with mwep, at hero or at monster mtarg */
261. staticfn void
262. monshoot(struct monst *mtmp, struct obj *otmp, struct obj *mwep)
263. {
264.     struct monst *mtarg = gm.mtarget;
265.     int dm = distmin(mtmp->mx, mtmp->my,
266.                      mtarg ? mtarg->mx : mtmp->mux,
267.                      mtarg ? mtarg->my : mtmp->muy),
268.         multishot = monmulti(mtmp, otmp, mwep);
269. 
270.     /*
271.      * Caller must have called linedup() to set up <gt.tbx, gt.tby>.
272.      */
273. 
274.     if (canseemon(mtmp)) {
275.         const char *onm;
276.         char onmbuf[BUFSZ], trgbuf[BUFSZ];
277. 
278.         if (multishot > 1) {
279.             /* "N arrows"; multishot > 1 implies otmp->quan > 1, so
280.                xname()'s result will already be pluralized */
281.             Sprintf(onmbuf, "%d %s", multishot, xname(otmp));
282.             onm = onmbuf;
283.         } else {
284.             /* "an arrow" */
285.             onm = singular(otmp, xname);
286.             onm = obj_is_pname(otmp) ? the(onm) : an(onm);
287.         }
288.         gm.m_shot.s = ammo_and_launcher(otmp, mwep) ? TRUE : FALSE;
289.         Strcpy(trgbuf, mtarg ? some_mon_nam(mtarg) : "");
290.         set_msg_xy(mtmp->mx, mtmp->my);
291.         pline("%s %s %s%s%s!", Monnam(mtmp),
292.               gm.m_shot.s ? "shoots" : "throws", onm,
293.               mtarg ? " at " : "", trgbuf);
294.         gm.m_shot.o = otmp->otyp;
295.     } else {
296.         gm.m_shot.o = STRANGE_OBJECT; /* don't give multishot feedback */
297.     }
298.     gm.m_shot.n = multishot;
299.     for (gm.m_shot.i = 1; gm.m_shot.i <= gm.m_shot.n; gm.m_shot.i++) {
300.         m_throw(mtmp, mtmp->mx, mtmp->my, sgn(gt.tbx), sgn(gt.tby), dm, otmp);
301.         /* conceptually all N missiles are in flight at once, but
302.            if mtmp gets killed (shot kills adjacent gas spore and
303.            triggers explosion, perhaps), inventory will be dropped
304.            and otmp might go away via merging into another stack */
305.         if (DEADMONSTER(mtmp) && gm.m_shot.i < gm.m_shot.n)
306.             /* cancel pending shots (perhaps ought to give a message here
307.                since we gave one above about throwing/shooting N missiles) */
308.             break; /* endmultishot(FALSE); */
309.     }
310.     /* reset 'gm.m_shot' */
311.     gm.m_shot.n = gm.m_shot.i = 0;
312.     gm.m_shot.o = STRANGE_OBJECT;
313.     gm.m_shot.s = FALSE;
314. }
315. 
316. /* an object launched by someone/thing other than player attacks a monster;
317.    return 1 if the object has stopped moving (hit or its range used up);
318.    can anger the monster, if this happened due to hero (eg. exploding
319.    bag of holding throwing the items) */
320. boolean
321. ohitmon(
322.     struct monst *mtmp, /* accidental target, located at <gb.bhitpos.x,.y> */
323.     struct obj *otmp,   /* missile; might be destroyed by drop_throw */
324.     int range,          /* how much farther will object travel if it misses;
325.                          * use -1 to signify to keep going even after hit,
326.                          * unless it's gone (for rolling_boulder_traps) */
327.     boolean verbose) /* give messages even when you can't see what happened */
328. {
329.     int damage, tmp;
330.     boolean vis, ismimic, objgone;
331.     struct obj *mon_launcher = gm.marcher ? MON_WEP(gm.marcher) : NULL;
332. 
333.     /* assert(otmp != NULL); */
334.     gn.notonhead = (gb.bhitpos.x != mtmp->mx || gb.bhitpos.y != mtmp->my);
335.     ismimic = M_AP_TYPE(mtmp) && M_AP_TYPE(mtmp) != M_AP_MONSTER;
336.     vis = cansee(gb.bhitpos.x, gb.bhitpos.y);
337.     if (vis)
338.         observe_object(otmp);
339. 
340.     tmp = 5 + find_mac(mtmp) + omon_adj(mtmp, otmp, FALSE);
341.     /* High level monsters will be more likely to hit */
342.     /* This check applies only if this monster is the target
343.      * the archer was aiming at. */
344.     if (gm.marcher && gm.mtarget == mtmp) {
345.         if (gm.marcher->m_lev > 5)
346.             tmp += gm.marcher->m_lev - 5;
347.         if (mon_launcher && mon_launcher->oartifact)
348.             tmp += spec_abon(mon_launcher, mtmp);
349.     }
350.     if (tmp < rnd(20)) {
351.         if (!ismimic) {
352.             if (vis)
353.                 miss(distant_name(otmp, mshot_xname), mtmp);
354.             else if (verbose && !gm.mtarget)
355.                 pline("It is missed.");
356.         }
357.         if (!range) { /* Last position; object drops */
358.             (void) drop_throw(otmp, 0, mtmp->mx, mtmp->my);
359.             return 1;
360.         }
361.     } else if (otmp->oclass == POTION_CLASS) {
362.         if (ismimic)
363.             seemimic(mtmp);
364.         mtmp->msleeping = 0;
365.         /* probably thrown by a monster rather than 'other', but the
366.            distinction only matters when hitting the hero */
367.         potionhit(mtmp, otmp, POTHIT_OTHER_THROW);
368.         return 1;
369.     } else {
370.         int material = objects[otmp->otyp].oc_material;
371.         boolean harmless = (stone_missile(otmp) && passes_rocks(mtmp->data));
372. 
373.         damage = dmgval(otmp, mtmp);
374.         if (otmp->otyp == ACID_VENOM && resists_acid(mtmp))
375.             damage = 0;
376. #if 0 /* can't use this because we don't have the attacker */
377.         if (is_orc(mtmp->data) && is_elf(?magr?))
378.             damage++;
379. #endif
380.         if (ismimic)
381.             seemimic(mtmp);
382.         mtmp->msleeping = 0;
383.         Soundeffect(se_splat_egg, 35);
384.         if (vis) {
385.             if (otmp->otyp == EGG) {
386.                 pline("Splat!  %s is hit with %s egg!", Monnam(mtmp),
387.                       otmp->known ? an(mons[otmp->corpsenm].pmnames[NEUTRAL])
388.                                   : "an");
389.             } else {
390.                 char how[BUFSZ];
391. 
392.                 if (!harmless)
393.                     Strcpy(how, exclam(damage)); /* "!" or "." */
394.                 else
395.                     Sprintf(how, " but passes harmlessly through %.9s.",
396.                             mhim(mtmp));
397.                 hit(distant_name(otmp, mshot_xname), mtmp, how);
398.             }
399.         } else if (verbose && !gm.mtarget)
400.             pline("%s%s is hit%s", (otmp->otyp == EGG) ? "Splat!  " : "",
401.                   Monnam(mtmp), exclam(damage));
402. 
403.         if (otmp->opoisoned && is_poisonable(otmp)) {
404.             if (resists_poison(mtmp)) {
405.                 if (vis)
406.                     pline_The("poison doesn't seem to affect %s.",
407.                               mon_nam(mtmp));
408.             } else {
409.                 if (rn2(30)) {
410.                     damage += rnd(6);
411.                 } else {
412.                     if (vis)
413.                         pline_The("poison was deadly...");
414.                     damage = mtmp->mhp;
415.                 }
416.             }
417.         }
418.         if (material == SILVER && mon_hates_silver(mtmp)) {
419.             boolean flesh = (!noncorporeal(mtmp->data)
420.                              && !amorphous(mtmp->data));
421. 
422.             /* note: extra silver damage is handled by dmgval() */
423.             if (vis) {
424.                 char *m_name = mon_nam(mtmp);
425. 
426.                 if (flesh) /* s_suffix returns a modifiable buffer */
427.                     m_name = strcat(s_suffix(m_name), " flesh");
428.                 pline_The("silver sears %s!", m_name);
429.             } else if (verbose && !gm.mtarget) {
430.                 pline("%s is seared!", flesh ? "Its flesh" : "It");
431.             }
432.         }
433.         if (otmp->otyp == ACID_VENOM && cansee(mtmp->mx, mtmp->my)) {
434.             if (resists_acid(mtmp)) {
435.                 if (vis || (verbose && !gm.mtarget))
436.                     pline("%s is unaffected.", Monnam(mtmp));
437.             } else {
438.                 if (vis)
439.                     pline_The("%s burns %s!", hliquid("acid"), mon_nam(mtmp));
440.                 else if (verbose && !gm.mtarget)
441.                     pline("It is burned!");
442.             }
443.         }
444.         if (otmp->otyp == EGG && touch_petrifies(&mons[otmp->corpsenm])) {
445.             if (!munstone(mtmp, FALSE))
446.                 minstapetrify(mtmp, FALSE);
447.             if (resists_ston(mtmp))
448.                 damage = 0;
449.         }
450. 
451.         /* might already be dead (if petrified) */
452.         if (!harmless && !DEADMONSTER(mtmp)) {
453.             mtmp->mhp -= damage;
454.             if (DEADMONSTER(mtmp)) {
455.                 if (vis || (verbose && !gm.mtarget))
456.                     pline("%s is %s!", Monnam(mtmp),
457.                           (nonliving(mtmp->data) || is_vampshifter(mtmp)
458.                            || !canspotmon(mtmp)) ? "destroyed" : "killed");
459.                 /* don't blame hero for unknown rolling boulder trap */
460.                 if (!svc.context.mon_moving
461.                    && (otmp->otyp != BOULDER || range >= 0 || otmp->otrapped))
462.                     xkilled(mtmp, XKILL_NOMSG);
463.                 else
464.                     mondied(mtmp);
465.             }
466.         }
467. 
468.         /* blinding venom and cream pie do 0 damage, but verify
469.            that the target is still alive anyway */
470.         if (!DEADMONSTER(mtmp)
471.             && can_blnd((struct monst *) 0, mtmp,
472.                         (uchar) ((otmp->otyp == BLINDING_VENOM) ? AT_SPIT
473.                                                                 : AT_WEAP),
474.                         otmp)) {
475.             if (vis && mtmp->mcansee)
476.                 /* shorten object name to reduce redundancy in the
477.                    two message [first via hit() above] sequence:
478.                    "The {splash of venom,cream pie} hits <mon>."
479.                    "<Mon> is blinded by the {venom,pie}." */
480.                 pline("%s is blinded by %s.", Monnam(mtmp),
481.                       the((otmp->oclass == VENOM_CLASS) ? "venom"
482.                           : (otmp->otyp == CREAM_PIE) ? "pie"
483.                             : xname(otmp))); /* catchall; not used */
484.             mtmp->mcansee = 0;
485.             tmp = (int) mtmp->mblinded + rnd(25) + 20;
486.             if (tmp > 127)
487.                 tmp = 127;
488.             mtmp->mblinded = tmp;
489.         }
490. 
491.         if (!DEADMONSTER(mtmp) && !svc.context.mon_moving)
492.             setmangry(mtmp, TRUE);
493. 
494.         objgone = drop_throw(otmp, 1, gb.bhitpos.x, gb.bhitpos.y);
495.         if (!objgone && range == -1) { /* special case */
496.             obj_extract_self(otmp);    /* free it for motion again */
497.             return FALSE;
498.         }
499.         return TRUE;
500.     }
501.     return FALSE;
502. }
503. 
504. /* hero catches gem thrown by mon iff poly'd into unicorn; might drop it */
505. staticfn boolean
506. ucatchgem(
507.     struct obj *gem,   /* caller has verified gem->oclass */
508.     struct monst *mon)
509. {
510.     /* won't catch rock or gray stone; catch (then drop) worthless glass */
511.     if (gem->otyp <= LAST_GLASS_GEM && is_unicorn(gy.youmonst.data)) {
512.         char *gem_xname = xname(gem),
513.              *mon_s_name = s_suffix(mon_nam(mon));
514. 
515.         if (gem->otyp >= FIRST_GLASS_GEM) {
516.             You("catch the %s.", gem_xname);
517.             You("are not interested in %s junk.", mon_s_name);
518.             makeknown(gem->otyp);
519.             dropy(gem);
520.         } else {
521.             You("accept %s gift in the spirit in which it was intended.",
522.                 mon_s_name);
523.             (void) hold_another_object(gem, "You catch, but drop, %s.",
524.                                        gem_xname, "You catch:");
525.         }
526.         return TRUE;
527.     }
528.     return FALSE;
529. }
530. 
531. /* hero may catch thrown obj. it is added to inventory, if possible */
532. staticfn boolean
533. u_catch_thrown_obj(struct obj *otmp)
534. {
535.     int catch_chance = 100 - ACURR(A_DEX)
536.                        - ((Role_if(PM_MONK) || Role_if(PM_ROGUE)) ? 20 : 0);
537. 
538.     if (!Blind && !Confusion && !Stunned && !Fumbling
539.         && otmp->oclass != VENOM_CLASS
540.         && !nohands(gy.youmonst.data) && freehand()
541.         && calc_capacity(otmp->owt) <= SLT_ENCUMBER && !rn2(catch_chance)) {
542.         char buf[BUFSZ];
543. 
544.         Snprintf(buf, BUFSZ, "You catch the %s!", simpleonames(otmp));
545.         (void) hold_another_object(otmp, "You catch, but drop, the %s.",
546.                                    simpleonames(otmp), buf);
547.         return TRUE;
548.     }
549.     return FALSE;
550. }
551. 
552. #define MT_FLIGHTCHECK(pre,forcehit) \
553.     (/* missile hits edge of screen */                                 \
554.      !isok(gb.bhitpos.x + dx, gb.bhitpos.y + dy)                       \
555.      /* missile hits the wall */                                       \
556.      || IS_OBSTRUCTED(levl[gb.bhitpos.x + dx][gb.bhitpos.y + dy].typ)  \
557.      /* missile hit closed door */                                     \
558.      || closed_door(gb.bhitpos.x + dx, gb.bhitpos.y + dy)              \
559.      /* missile might hit iron bars */                                 \
560.      /* the random chance for small objects hitting bars is */         \
561.      /* skipped when reaching them at point blank range */             \
562.      || (levl[gb.bhitpos.x + dx][gb.bhitpos.y + dy].typ == IRONBARS    \
563.          && hits_bars(&singleobj,                                      \
564.                       gb.bhitpos.x, gb.bhitpos.y,                      \
565.                       gb.bhitpos.x + dx, gb.bhitpos.y + dy,            \
566.                       ((pre) ? 0 : forcehit), 0))                      \
567.      /* Thrown objects "sink" */                                       \
568.      || (!(pre) && IS_SINK(levl[gb.bhitpos.x][gb.bhitpos.y].typ))      \
569.      )
570. 
571. void
572. m_throw(
573.     struct monst *mon,      /* launching monster */
574.     coordxy x, coordxy y,   /* launch point */
575.     coordxy dx, coordxy dy, /* direction */
576.     int range,              /* maximum distance */
577.     struct obj *obj)        /* missile (or stack providing it) */
578. {
579.     struct monst *mtmp;
580.     struct obj *singleobj;
581.     boolean forcehit;
582.     char sym = obj->oclass;
583.     int hitu = 0, oldumort, blindinc = 0;
584.     const struct throw_and_return_weapon *arw = autoreturn_weapon(obj);
585.     boolean tethered_weapon =
586.                 (obj == MON_WEP(mon) && arw && arw->tethered != 0),
587.             return_flightpath = FALSE;
588. 
589.     gb.bhitpos.x = x;
590.     gb.bhitpos.y = y;
591.     gn.notonhead = FALSE; /* reset potentially stale value */
592. 
593.     if (obj->quan == 1L) {
594.         /*
595.          * Remove object from minvent.  This cannot be done later on;
596.          * what if the player dies before then, leaving the monster
597.          * with 0 daggers?  (This caused the infamous 2^32-1 orcish
598.          * dagger bug).
599.          *
600.          * VENOM is not in minvent--it should already be OBJ_FREE.
601.          * The extract below does nothing.
602.          */
603. 
604.         /* not possibly_unwield(), which checks the object's location,
605.            not its existence */
606.         if (MON_WEP(mon) == obj)
607.             setmnotwielded(mon, obj);
608.         obj_extract_self(obj);
609.         singleobj = obj;
610.         obj = (struct obj *) 0;
611.     } else {
612.         singleobj = splitobj(obj, 1L);
613.         obj_extract_self(singleobj);
614.     }
615.     /* global pointer for missile object in OBJ_FREE state */
616.     gt.thrownobj = singleobj;
617. 
618.     singleobj->owornmask = 0L; /* threw one of multiple weapons in hand? */
619.     if (!canseemon(mon))
620.         clear_dknown(singleobj); /* singleobj->dknown = 0; */
621. 
622.     if ((singleobj->cursed || singleobj->greased) && (dx || dy) && !rn2(7)) {
623.         if (canseemon(mon) && flags.verbose) {
624.             if (is_ammo(singleobj))
625.                 pline("%s misfires!", Monnam(mon));
626.             else
627.                 pline("%s as %s throws it!", Tobjnam(singleobj, "slip"),
628.                       mon_nam(mon));
629.         }
630.         dx = rn2(3) - 1;
631.         dy = rn2(3) - 1;
632.         /* check validity of new direction */
633.         if (!dx && !dy) {
634.             (void) drop_throw(singleobj, 0, gb.bhitpos.x, gb.bhitpos.y);
635.             return;
636.         }
637.     }
638. 
639.     if (MT_FLIGHTCHECK(TRUE, 0)) {
640.         (void) drop_throw(singleobj, 0, gb.bhitpos.x, gb.bhitpos.y);
641.         return;
642.     }
643.     gm.mesg_given = 0; /* a 'missile misses' message has not yet been shown */
644. 
645.     /* Note: drop_throw may destroy singleobj.  Since obj must be destroyed
646.      * early to avoid the dagger bug, anyone who modifies this code should
647.      * be careful not to use either one after it's been freed.
648.      */
649.     if (sym) {
650.         if (!tethered_weapon) {
651.             tmp_at(DISP_FLASH, obj_to_glyph(singleobj, rn2_on_display_rng));
652.         } else {
653.             tmp_at(DISP_TETHER, obj_to_glyph(singleobj, rn2_on_display_rng));
654.             /*
655.              * Considerations for a tethered object based on throwit()/bhit() :
656.              * - wall of water/lava will stop items, and triggers return.
657.              * - iron bars will stop items, and triggers return.
658.              * - pass harmlessly through shades.
659.              * X stops forward motion at hit monster/hero, triggers return.
660.              * - closed door will stop item's forward motion, triggers return.
661.              * - sinks stop forward motion, triggers fall, then return.
662.              * - object can get tangled in a web, no return (tether snaps?).
663.              * On return:
664.              * X rn2(100) chance of returning to thrower's location.
665.              * X if impaired and rn2(100) == 0,
666.              *      -50/50 chance of landing on the ground.
667.              *      -50/50 chance of hitting the thrower and causing
668.              *       rnd(3) damage.
669.              *
670.              */
671.         }
672.     }
673.     while (range-- > 0) { /* Actually the loop is always exited by break */
674.         singleobj->ox = gb.bhitpos.x += dx;
675.         singleobj->oy = gb.bhitpos.y += dy;
676.         if (cansee(gb.bhitpos.x, gb.bhitpos.y))
677.             observe_object(singleobj);
678. 
679.         mtmp = m_at(gb.bhitpos.x, gb.bhitpos.y);
680.         if (mtmp && shade_miss(mon, mtmp, singleobj, TRUE, TRUE)) {
681.             /* if mtmp is a shade and missile passes harmlessly through it,
682.                give message and skip it in order to keep going */
683.             mtmp = (struct monst *) 0;
684.         } else if (mtmp) {
685.             if (ohitmon(mtmp, singleobj, range, TRUE))
686.                 break;
687.         } else if (u_at(gb.bhitpos.x, gb.bhitpos.y)) {
688.             if (gm.multi)
689.                 nomul(0);
690. 
691.             /* hero might be poly'd into a unicorn */
692.             if (singleobj->oclass == GEM_CLASS && ucatchgem(singleobj, mon))
693.                 break;
694. 
695.             if (!tethered_weapon && u_catch_thrown_obj(singleobj))
696.                 break;
697. 
698.             if (singleobj->oclass == POTION_CLASS) {
699.                 potionhit(&gy.youmonst, singleobj, POTHIT_MONST_THROW);
700.                 break;
701.             }
702.             oldumort = u.umortality;
703. 
704.             switch (singleobj->otyp) {
705.             case EGG:
706.                 if (!touch_petrifies(&mons[singleobj->corpsenm])) {
707.                     impossible("monster throwing egg type %d",
708.                                singleobj->corpsenm);
709.                     hitu = 0;
710.                     break;
711.                 }
712.                 FALLTHROUGH;
713.                 /*FALLTHRU*/
714.             case CREAM_PIE:
715.             case BLINDING_VENOM:
716.                 hitu = thitu(8, 0, &singleobj, (char *) 0);
717.                 break;
718.             default:
719.                 {
720.                     int dam, hitv;
721. 
722.                     dam = dmgval(singleobj, &gy.youmonst);
723.                     hitv = 3 - distmin(u.ux, u.uy, mon->mx, mon->my);
724.                     if (hitv < -4)
725.                         hitv = -4;
726.                     /* [elves get a shooting bonus, orcs don't...] */
727.                     if (is_elf(mon->data)
728.                         && objects[singleobj->otyp].oc_skill == -P_BOW) {
729.                         hitv++;
730.                         if (MON_WEP(mon) && MON_WEP(mon)->otyp == ELVEN_BOW)
731.                             hitv++;
732.                         if (singleobj->otyp == ELVEN_ARROW)
733.                             dam++;
734.                     }
735.                     if (bigmonst(gy.youmonst.data))
736.                         hitv++;
737.                     hitv += 8 + singleobj->spe;
738.                     if (dam < 1)
739.                         dam = 1;
740.                     if (singleobj->otyp != ACID_VENOM)
741.                         dam = Maybe_Half_Phys(dam);
742.                     hitu = thitu(hitv, dam, &singleobj, (char *) 0);
743.                 }
744.             }
745.             if (hitu && singleobj->opoisoned && is_poisonable(singleobj)) {
746.                 char onmbuf[BUFSZ], knmbuf[BUFSZ];
747. 
748.                 Strcpy(onmbuf, xname(singleobj));
749.                 Strcpy(knmbuf, killer_xname(singleobj));
750.                 poisoned(onmbuf, A_STR, knmbuf,
751.                          /* if damage triggered life-saving,
752.                             poison is limited to attrib loss */
753.                          (u.umortality > oldumort) ? 0 : 10, TRUE);
754.             }
755.             if (hitu && can_blnd((struct monst *) 0, &gy.youmonst,
756.                                  (uchar) ((singleobj->otyp == BLINDING_VENOM)
757.                                              ? AT_SPIT
758.                                              : AT_WEAP),
759.                                  singleobj)) {
760.                 blindinc = rnd(25);
761.                 if (singleobj->otyp == CREAM_PIE) {
762.                     if (!Blind)
763.                         pline("Yecch!  You've been creamed.");
764.                     else
765.                         pline("There's %s sticky all over your %s.",
766.                               something, body_part(FACE));
767.                 } else if (singleobj->otyp == BLINDING_VENOM) {
768.                     const char *eyes = body_part(EYE);
769. 
770.                     if (eyecount(gy.youmonst.data) != 1)
771.                         eyes = makeplural(eyes);
772.                     /* venom in the eyes */
773.                     if (!Blind)
774.                         pline_The("venom blinds you.");
775.                     else
776.                         Your("%s %s.", eyes, vtense(eyes, "sting"));
777.                 }
778.             }
779.             if (hitu && singleobj->otyp == EGG) {
780.                 if (!Stoned && !Stone_resistance
781.                     && !(poly_when_stoned(gy.youmonst.data)
782.                          && polymon(PM_STONE_GOLEM))) {
783.                     make_stoned(5L, (char *) 0, KILLED_BY, "");
784.                 }
785.             }
786.             stop_occupation();
787.             if (hitu) {
788.                 if (!tethered_weapon) {
789.                     (void) drop_throw(singleobj, hitu, u.ux, u.uy);
790.                 } else {
791.                     /* ready for return journey */
792.                     return_flightpath = TRUE;
793.                 }
794.                 break;
795.             }
796.         }
797. 
798.         forcehit = !rn2(5);
799.         if (!range || MT_FLIGHTCHECK(FALSE, forcehit)) {
800.             /* end of path or blocked */
801.             if (singleobj) { /* hits_bars might have destroyed it */
802.                 /* note: pline(The(missile)) rather than pline_The(missile)
803.                    in order to get "Grimtooth" rather than "The Grimtooth" */
804.                 if (range && cansee(gb.bhitpos.x, gb.bhitpos.y)
805.                     && IS_SINK(levl[gb.bhitpos.x][gb.bhitpos.y].typ))
806.                     pline("%s %s onto the sink.", The(mshot_xname(singleobj)),
807.                           otense(singleobj, Hallucination ? "plop" : "drop"));
808.                 else if (gm.m_shot.n > 1
809.                          && (!gm.mesg_given
810.                              || gb.bhitpos.x != u.ux || gb.bhitpos.y != u.uy)
811.                          && (cansee(gb.bhitpos.x, gb.bhitpos.y)
812.                              || (gm.marcher && canseemon(gm.marcher))))
813.                     pline("%s misses.", The(mshot_xname(singleobj)));
814.                 if (!tethered_weapon) {
815.                     (void) drop_throw(singleobj, 0,
816.                                       gb.bhitpos.x, gb.bhitpos.y);
817.                 } else {
818.                     /*ready for return journey */
819.                     return_flightpath = TRUE;
820.                 }
821.             }
822.             break;
823.         }
824.         tmp_at(gb.bhitpos.x, gb.bhitpos.y);
825.         nh_delay_output();
826.     }
827.     tmp_at(gb.bhitpos.x, gb.bhitpos.y);
828.     nh_delay_output();
829.     if (arw && return_flightpath)
830.         return_from_mtoss(mon, singleobj, tethered_weapon);
831.         /* mon could be DEADMONSTER now */
832.     else
833.         tmp_at(DISP_END, 0);
834.     gm.mesg_given = 0; /* reset */
835. 
836.     if (blindinc) {
837.         u.ucreamed += blindinc;
838.         make_blinded(BlindedTimeout + (long) blindinc, FALSE);
839.         if (!Blind)
840.             Your1(vision_clears);
841.     }
842.     /* note: all early returns follow drop_throw() which clears thrownobj */
843.     gt.thrownobj = 0;
844.     return;
845. }
846. 
847. #undef MT_FLIGHTCHECK
848. 
849. staticfn void
850. return_from_mtoss(
851.     struct monst *magr,
852.     struct obj *otmp,
853.     boolean tethered_weapon)
854. {
855.     boolean impaired = (magr->mconf || magr->mstun || magr->mblinded),
856.             notcaught = FALSE, hits_thrower = FALSE;
857.     coordxy x = gb.bhitpos.x, y = gb.bhitpos.y;
858.     int made_it_back = rn2(100), dmg = 0;
859. 
860.     if (otmp && made_it_back) {
861.         /* it made it back to thrower's location */
862.         if (tethered_weapon) {
863.             tmp_at(DISP_END, BACKTRACK);
864.         } else {
865.             int dx = sgn(x - magr->mx),
866.                 dy = sgn(y - magr->my);
867. 
868.             if (x != magr->mx || y != magr->my) {
869.                 tmp_at(DISP_FLASH, obj_to_glyph(otmp, rn2_on_display_rng));
870.                 while (isok(x, y) && (x != magr->mx || y != magr->my)) {
871.                     tmp_at(x, y);
872.                     nh_delay_output();
873.                     x -= dx;
874.                     y -= dy;
875.                 }
876.                 tmp_at(DISP_END, 0);
877.             }
878.         }
879.         x = magr->mx;
880.         y = magr->my;
881.         if (!impaired && rn2(100)) {
882.             /* FIXME: this should be moved to struct g (gd these days) */
883.             static long do_not_annoy = 0;
884. 
885.             if (!do_not_annoy || (svm.moves - do_not_annoy) > 500L) {
886.                 pline("%s to %s %s!", Tobjnam(otmp, "return"),
887.                       s_suffix(mon_nam(magr)), mbodypart(magr, HAND));
888.                 do_not_annoy = svm.moves;
889.             }
890.             if (otmp) {
891.                 add_to_minv(magr, otmp);
892.                 if (tethered_weapon) {
893.                     magr->mw = otmp;
894.                     otmp->owornmask |= W_WEP;
895.                 }
896.             }
897.             if (cansee(x, y))
898.                 newsym(x, y);
899.         } else {
900.             boolean mlevitating = FALSE;  /* msg future-proofing only */
901. 
902.             dmg = rn2(2);
903.             if (!dmg) {
904.                 if (canseemon(magr)) {
905.                     pline("%s back to %s, landing %s %s %s.",
906.                           Tobjnam(otmp, "return"), mon_nam(magr),
907.                           mlevitating ? "beneath" : "at", mhis(magr),
908.                           makeplural(mbodypart(magr, FOOT)));
909.                 } else if (!Deaf) {
910.                     You_hear("%s land near %s.", Something, mon_nam(magr));
911.                 }
912.             } else {
913.                 dmg += rnd(3);
914.                 if (canseemon(magr)) {
915.                     pline("%s back toward %s, hitting %s %s!",
916.                           Tobjnam(otmp, "fly"), mon_nam(magr),
917.                           mhis(magr), body_part(ARM));
918.                 } else if (!Deaf) {
919.                     You_hear("%s hit %s with a thud!", something,
920.                              mon_nam(magr));
921.                 }
922.                 hits_thrower = TRUE;
923.             }
924.             notcaught = TRUE;
925.         }
926.     } else {
927.         /* it didn't make it back to thrower's location */
928.         if (tethered_weapon)
929.             tmp_at(DISP_END, 0);
930.         You_hear("a loud snap!");
931.         notcaught = TRUE;
932.     }
933.     if (otmp) {
934.         if (hits_thrower) {
935.             if (otmp->oartifact)
936.                 (void) artifact_hit((struct monst *) 0, magr, otmp, &dmg, 0);
937.             magr->mhp -= dmg;
938.             if (DEADMONSTER(magr))
939.                 monkilled(magr, canspotmon(magr) ? "" : (char *) 0, AD_PHYS);
940.         }
941.         if (notcaught) {
942.             (void) snuff_candle(otmp);
943.             if (!ship_object(otmp, x, y, FALSE)) {
944.                 if (flooreffects(otmp, x, y, "drop")) {
945.                     if (cansee(x, y))
946.                         newsym(x, y);
947.                     return;
948.                 }
949.                 place_object(otmp, x, y);
950.                 stackobj(otmp);
951.             }
952.             if (!Deaf && !Underwater) {
953.                 /* Some sound effects when item lands in water or lava */
954.                 if (is_pool(x, y) || (is_lava(x, y) && !is_flammable(otmp))) {
955.                     Soundeffect(se_splash, 50);
956.                     pline((weight(otmp) > 9) ? "Splash!" : "Plop!");
957.                 }
958.             }
959.             if (obj_sheds_light(otmp))
960.                 gv.vision_full_recalc = 1;
961.         }
962.     }
963.     if (cansee(x, y))
964.         newsym(x, y);
965. }
966. 
967. /* Monster throws item at another monster */
968. int
969. thrwmm(struct monst *mtmp, struct monst *mtarg)
970. {
971.     struct obj *otmp, *mwep;
972.     coordxy x, y;
973.     boolean ispole;
974. 
975.     /* Polearms won't be applied by monsters against other monsters */
976.     if (mtmp->weapon_check == NEED_WEAPON || !MON_WEP(mtmp)) {
977.         mtmp->weapon_check = NEED_RANGED_WEAPON;
978.         /* mon_wield_item resets weapon_check as appropriate */
979.         if (mon_wield_item(mtmp) != 0)
980.             return M_ATTK_MISS;
981.     }
982. 
983.     /* Pick a weapon */
984.     otmp = select_rwep(mtmp);
985.     if (!otmp)
986.         return M_ATTK_MISS;
987.     ispole = is_pole(otmp);
988. 
989.     x = mtmp->mx;
990.     y = mtmp->my;
991. 
992.     mwep = MON_WEP(mtmp); /* wielded weapon */
993. 
994.     if (!ispole && m_lined_up(mtarg, mtmp)) {
995.         int chance = max(BOLT_LIM - distmin(x, y, mtarg->mx, mtarg->my), 1);
996. 
997.         if (!mtarg->mflee || !rn2(chance)) {
998.             if (ammo_and_launcher(otmp, mwep)
999.                 && dist2(mtmp->mx, mtmp->my, mtarg->mx, mtarg->my)
1000.                    > PET_MISSILE_RANGE2)
1001.                 return M_ATTK_MISS; /* Out of range */
1002.             /* Set target monster */
1003.             gm.mtarget = mtarg;
1004.             gm.marcher = mtmp;
1005.             monshoot(mtmp, otmp, mwep); /* multishot shooting or throwing */
1006.             gm.marcher = gm.mtarget = (struct monst *) 0;
1007.             nomul(0);
1008.             return M_ATTK_HIT;
1009.         }
1010.     }
1011.     return M_ATTK_MISS;
1012. }
1013. 
1014. /* monster spits substance at monster */
1015. int
1016. spitmm(struct monst *mtmp, struct attack *mattk, struct monst *mtarg)
1017. {
1018.     struct obj *otmp;
1019. 
1020.     if (mtmp->mcan) {
1021.         if (!Deaf && mdistu(mtmp) < BOLT_LIM * BOLT_LIM) {
1022.             if (canspotmon(mtmp)) {
1023.                 pline("A dry rattle comes from %s throat.",
1024.                       s_suffix(mon_nam(mtmp)));
1025.             } else {
1026.                 Soundeffect(se_dry_throat_rattle, 50);
1027.                 You_hear("a dry rattle nearby.");
1028.             }
1029.         }
1030.         return M_ATTK_MISS;
1031.     }
1032.     if (m_lined_up(mtarg, mtmp)) {
1033.         boolean utarg = (mtarg == &gy.youmonst);
1034.         coordxy tx = utarg ? mtmp->mux : mtarg->mx;
1035.         coordxy ty = utarg ? mtmp->muy : mtarg->my;
1036. 
1037.         switch (mattk->adtyp) {
1038.         case AD_BLND:
1039.         case AD_DRST:
1040.             otmp = mksobj(BLINDING_VENOM, TRUE, FALSE);
1041.             break;
1042.         default:
1043.             impossible("bad attack type in spitmm");
1044.             FALLTHROUGH;
1045.             /*FALLTHRU*/
1046.         case AD_ACID:
1047.             otmp = mksobj(ACID_VENOM, TRUE, FALSE);
1048.             break;
1049.         }
1050.         if (!rn2(BOLT_LIM-distmin(mtmp->mx,mtmp->my,tx,ty))) {
1051.             if (canseemon(mtmp))
1052.                 pline("%s spits venom!", Monnam(mtmp));
1053.             if (!utarg)
1054.                 gm.mtarget = mtarg;
1055.             m_throw(mtmp, mtmp->mx, mtmp->my, sgn(gt.tbx), sgn(gt.tby),
1056.                     distmin(mtmp->mx,mtmp->my,tx,ty), otmp);
1057.             gm.mtarget = (struct monst *) 0;
1058.             nomul(0);
1059. 
1060.             /* If this is a pet, it'll get hungry. Minions and
1061.              * spell beings won't hunger */
1062.             if (mtmp->mtame && !mtmp->isminion) {
1063.                 struct edog *dog = EDOG(mtmp);
1064. 
1065.                 /* Hunger effects will catch up next move */
1066.                 if (dog->hungrytime > 1)
1067.                     dog->hungrytime -= 5;
1068.             }
1069. 
1070.             return M_ATTK_HIT;
1071.         } else {
1072.             obj_extract_self(otmp);
1073.             obfree(otmp, (struct obj *) 0);
1074.         }
1075.     }
1076.     return M_ATTK_MISS;
1077. }
1078. 
1079. /* Return the name of a breath weapon. If the player is hallucinating, return
1080.  * a silly name instead.
1081.  * typ is AD_MAGM, AD_FIRE, etc */
1082. staticfn const char *
1083. breathwep_name(int typ)
1084. {
1085.     if (Hallucination)
1086.         return rnd_hallublast();
1087. 
1088.     return breathwep[BZ_OFS_AD(typ)];
1089. }
1090. 
1091. /* monster breathes at monster (ranged) */
1092. int
1093. breamm(struct monst *mtmp, struct attack *mattk, struct monst *mtarg)
1094. {
1095.     int typ = get_atkdam_type(mattk->adtyp);
1096.     boolean utarget = (mtarg == &gy.youmonst);
1097. 
1098.     if (m_lined_up(mtarg, mtmp)) {
1099.         if (mtmp->mcan) {
1100.             if (!Deaf) {
1101.                 if (canseemon(mtmp)) {
1102.                     pline("%s coughs.", Monnam(mtmp));
1103.                 } else {
1104.                     Soundeffect(se_cough, 100);
1105.                     You_hear("a cough.");
1106.                 }
1107.             }
1108.             return M_ATTK_MISS;
1109.         }
1110. 
1111.         /* if we've seen the actual resistance, don't bother, or
1112.            if we're close by and they reflect, just jump the player */
1113.         if (utarget && (m_seenres(mtmp, cvt_adtyp_to_mseenres(typ))
1114.                         || m_seenres(mtmp, M_SEEN_REFL)))
1115.             return M_ATTK_HIT;
1116. 
1117.         if (!mtmp->mspec_used && rn2(3)) {
1118.             if (BZ_VALID_ADTYP(typ)) {
1119.                 if (canseemon(mtmp))
1120.                     pline("%s breathes %s!",
1121.                           Monnam(mtmp), breathwep_name(typ));
1122.                 gb.buzzer = mtmp;
1123.                 dobuzz(BZ_M_BREATH(BZ_OFS_AD(typ)), (int) mattk->damn,
1124.                        mtmp->mx, mtmp->my, sgn(gt.tbx), sgn(gt.tby),
1125.                        utarget, utarget, FALSE);
1126.                 gb.buzzer = 0;
1127.                 nomul(0);
1128.                 /* breath runs out sometimes. Also, give monster some
1129.                  * cunning; don't breath if the target fell asleep.
1130.                  */
1131.                 if (!utarget || !rn2(3))
1132.                     mtmp->mspec_used = 8 + rn2(18);
1133.                 if (utarget && typ == AD_SLEE && !Sleep_resistance)
1134.                     mtmp->mspec_used += rnd(20);
1135. 
1136.                 /* If this is a pet, it'll get hungry. Minions and
1137.                  * spell beings won't hunger */
1138.                 if (mtmp->mtame && !mtmp->isminion) {
1139.                     struct edog *dog = EDOG(mtmp);
1140. 
1141.                     /* Hunger effects will catch up next move */
1142.                     if (dog->hungrytime >= 10)
1143.                         dog->hungrytime -= 10;
1144.                 }
1145.             } else impossible("Breath weapon %d used", typ-1);
1146.         } else
1147.             return M_ATTK_MISS;
1148.     }
1149.     return M_ATTK_HIT;
1150. }
1151. 
1152. /* remove an entire item from a monster's inventory; destroy that item */
1153. void
1154. m_useupall(struct monst *mon, struct obj *obj)
1155. {
1156.     extract_from_minvent(mon, obj, TRUE, FALSE);
1157.     obfree(obj, (struct obj *) 0);
1158. }
1159. 
1160. /* remove one instance of an item from a monster's inventory */
1161. void
1162. m_useup(struct monst *mon, struct obj *obj)
1163. {
1164.     if (obj->quan > 1L) {
1165.         obj->quan--;
1166.         obj->owt = weight(obj);
1167.     } else {
1168.         m_useupall(mon, obj);
1169.     }
1170. }
1171. 
1172. /* monster attempts ranged weapon attack against player */
1173. void
1174. thrwmu(struct monst *mtmp)
1175. {
1176.     struct obj *otmp, *mwep;
1177.     coordxy x, y;
1178.     const char *onm;
1179.     int rang;
1180.     const struct throw_and_return_weapon *arw;
1181.     boolean always_toss = FALSE;
1182. 
1183.     /* Rearranged beginning so monsters can use polearms not in a line */
1184.     if (mtmp->weapon_check == NEED_WEAPON || !MON_WEP(mtmp)) {
1185.         mtmp->weapon_check = NEED_RANGED_WEAPON;
1186.         /* mon_wield_item resets weapon_check as appropriate */
1187.         if (mon_wield_item(mtmp) != 0)
1188.             return;
1189.     }
1190. 
1191.     /* Pick a weapon */
1192.     otmp = select_rwep(mtmp);
1193.     if (!otmp)
1194.         return;
1195. 
1196.     if (is_pole(otmp)) {
1197.         int dam, hitv;
1198. 
1199.         if (otmp != MON_WEP(mtmp))
1200.             return; /* polearm, aklys must be wielded */
1201. 
1202.         /*
1203.          * MON_POLE_DIST encompasses knight's move range (5): two spots
1204.          * away provided it's not on a straight diagonal, same as skilled
1205.          * hero.  Using polearm while adjacent is allowed but the verb
1206.          * is adjusted from "thrusts" to "bashes", where the hero would
1207.          * have to switch from applying a polearm to ordinary melee attack
1208.          * to accomplish that.
1209.          *
1210.          *  .545.
1211.          *  52125
1212.          *  41014
1213.          *  52125
1214.          *  .545.
1215.          */
1216.         rang = dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy);
1217.         if (rang > MON_POLE_DIST || !couldsee(mtmp->mx, mtmp->my))
1218.             return; /* Out of range, or intervening wall */
1219. 
1220.         if (canseemon(mtmp)) {
1221.             onm = xname(otmp);
1222.             pline_mon(mtmp, "%s %s %s.", Monnam(mtmp),
1223.                   /* "thrusts" or "swings", or "bashes with" if adjacent */
1224.                   mswings_verb(otmp, (rang <= 2) ? TRUE : FALSE),
1225.                   obj_is_pname(otmp) ? the(onm) : an(onm));
1226.         }
1227. 
1228.         dam = dmgval(otmp, &gy.youmonst);
1229.         hitv = 3 - distmin(u.ux, u.uy, mtmp->mx, mtmp->my);
1230.         if (hitv < -4)
1231.             hitv = -4;
1232.         if (bigmonst(gy.youmonst.data))
1233.             hitv++;
1234.         hitv += 8 + otmp->spe;
1235.         if (dam < 1)
1236.             dam = 1;
1237. 
1238.         (void) thitu(hitv, Maybe_Half_Phys(dam), &otmp, (char *) 0);
1239.         stop_occupation();
1240.         return;
1241.     } else if ((arw = autoreturn_weapon(otmp)) != 0 && !mwelded(otmp)) {
1242.         rang = dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy);
1243.         if (rang > arw->range || !couldsee(mtmp->mx, mtmp->my))
1244.             return; /* Out of range, or intervening wall */
1245.         always_toss = TRUE;
1246.     }
1247. 
1248.     x = mtmp->mx;
1249.     y = mtmp->my;
1250.     /* If you are coming toward the monster, the monster
1251.      * should try to soften you up with missiles.  If you are
1252.      * going away, you are probably hurt or running.  Give
1253.      * chase, but if you are getting too far away, throw.
1254.      */
1255.     if (!lined_up(mtmp)
1256.         || (URETREATING(x, y)
1257.             && (!always_toss
1258.                 && rn2(BOLT_LIM - distmin(x, y, mtmp->mux, mtmp->muy)))))
1259.         return;
1260. 
1261.     mwep = MON_WEP(mtmp); /* wielded weapon */
1262.     monshoot(mtmp, otmp, mwep); /* multishot shooting or throwing */
1263.     nomul(0);
1264. }
1265. 
1266. /* monster spits substance at you */
1267. int
1268. spitmu(struct monst *mtmp, struct attack *mattk)
1269. {
1270.     return spitmm(mtmp, mattk, &gy.youmonst);
1271. }
1272. 
1273. /* monster breathes at you (ranged) */
1274. int
1275. breamu(struct monst *mtmp, struct attack *mattk)
1276. {
1277.     return breamm(mtmp, mattk, &gy.youmonst);
1278. }
1279. 
1280. /* return TRUE if terrain at x,y blocks linedup checks */
1281. staticfn boolean
1282. blocking_terrain(coordxy x, coordxy y)
1283. {
1284.     if (!isok(x, y) || IS_OBSTRUCTED(levl[x][y].typ) || closed_door(x, y)
1285.         || is_waterwall(x, y) || levl[x][y].typ == LAVAWALL)
1286.         return TRUE;
1287.     return FALSE;
1288. }
1289. 
1290. /* Move from (ax,ay) to (bx,by), but only if distance is up to BOLT_LIM
1291.    and only in straight line or diagonal, calling fnc for each step.
1292.    Stops if fnc return TRUE, or if step was blocked by wall or closed door.
1293.    Returns TRUE if fnc returned TRUE. */
1294. boolean
1295. linedup_callback(
1296.     coordxy ax,
1297.     coordxy ay,
1298.     coordxy bx,
1299.     coordxy by,
1300.     boolean (*fnc)(coordxy, coordxy))
1301. {
1302.     int dx, dy;
1303. 
1304.     /* These two values are set for use after successful return. */
1305.     gt.tbx = ax - bx;
1306.     gt.tby = ay - by;
1307. 
1308.     /* sometimes displacement makes a monster think that you're at its
1309.        own location; prevent it from throwing and zapping in that case */
1310.     if (!gt.tbx && !gt.tby)
1311.         return FALSE;
1312. 
1313.     /* straight line, orthogonal to the map or diagonal */
1314.     if ((!gt.tbx || !gt.tby || abs(gt.tbx) == abs(gt.tby))
1315.         && distmin(gt.tbx, gt.tby, 0, 0) < BOLT_LIM) {
1316.         dx = sgn(ax - bx), dy = sgn(ay - by);
1317.         do {
1318.             /* <bx,by> is guaranteed to eventually converge with <ax,ay> */
1319.             bx += dx, by += dy;
1320.             if (blocking_terrain(bx, by))
1321.                 return FALSE;
1322.             if ((*fnc)(bx, by))
1323.                 return TRUE;
1324.         } while (bx != ax || by != ay);
1325.     }
1326.     return FALSE;
1327. }
1328. 
1329. boolean
1330. linedup(
1331.     coordxy ax,
1332.     coordxy ay,
1333.     coordxy bx,
1334.     coordxy by,
1335.     int boulderhandling) /* 0=block, 1=ignore, 2=conditionally block */
1336. {
1337.     int dx, dy, boulderspots;
1338. 
1339.     /* These two values are set for use after successful return. */
1340.     gt.tbx = ax - bx;
1341.     gt.tby = ay - by;
1342. 
1343.     /* sometimes displacement makes a monster think that you're at its
1344.        own location; prevent it from throwing and zapping in that case */
1345.     if (!gt.tbx && !gt.tby)
1346.         return FALSE;
1347. 
1348.     /* straight line, orthogonal to the map or diagonal */
1349.     if ((!gt.tbx || !gt.tby || abs(gt.tbx) == abs(gt.tby))
1350.         && distmin(gt.tbx, gt.tby, 0, 0) < BOLT_LIM) {
1351.         if (u_at(ax, ay) ? (boolean) couldsee(bx, by)
1352.                          : clear_path(ax, ay, bx, by))
1353.             return TRUE;
1354.         /* don't have line of sight, but might still be lined up
1355.            if that lack of sight is due solely to boulders */
1356.         if (boulderhandling == 0)
1357.             return FALSE;
1358.         dx = sgn(ax - bx), dy = sgn(ay - by);
1359.         boulderspots = 0;
1360.         do {
1361.             /* <bx,by> is guaranteed to eventually converge with <ax,ay> */
1362.             bx += dx, by += dy;
1363.             if (blocking_terrain(bx, by))
1364.                 return FALSE;
1365.             if (sobj_at(BOULDER, bx, by))
1366.                 ++boulderspots;
1367.         } while (bx != ax || by != ay);
1368.         /* reached target position without encountering obstacle */
1369.         if (boulderhandling == 1 || rn2(2 + boulderspots) < 2)
1370.             return TRUE;
1371.     }
1372.     return FALSE;
1373. }
1374. 
1375. staticfn int
1376. m_lined_up(struct monst *mtarg, struct monst *mtmp)
1377. {
1378.     boolean utarget = (mtarg == &gy.youmonst);
1379.     coordxy tx = utarget ? mtmp->mux : mtarg->mx;
1380.     coordxy ty = utarget ? mtmp->muy : mtarg->my;
1381.     boolean ignore_boulders = utarget && (throws_rocks(mtmp->data)
1382.                                           || m_carrying(mtmp, WAN_STRIKING));
1383. 
1384.     /* hero concealment usually trumps monst awareness of being lined up */
1385.     if (utarget && Upolyd && rn2(25)
1386.         && (u.uundetected || (U_AP_TYPE != M_AP_NOTHING
1387.                               && U_AP_TYPE != M_AP_MONSTER)))
1388.         return FALSE;
1389. 
1390.     /* [no callers care about the 1 vs 2 situation any more] */
1391.     return linedup(tx, ty, mtmp->mx, mtmp->my,
1392.                    utarget ? (ignore_boulders ? 1 : 2) : 0);
1393. }
1394. 
1395. 
1396. /* is mtmp in position to use ranged attack on hero? */
1397. boolean
1398. lined_up(struct monst *mtmp)
1399. {
1400.     return m_lined_up(&gy.youmonst, mtmp) ? TRUE : FALSE;
1401. }
1402. 
1403. /* check if a monster is carrying an item of a particular type */
1404. struct obj *
1405. m_carrying(struct monst *mtmp, int type)
1406. {
1407.     struct obj *otmp;
1408. 
1409.     for (otmp = (mtmp == &gy.youmonst) ? gi.invent : mtmp->minvent; otmp;
1410.          otmp = otmp->nobj)
1411.         if (otmp->otyp == type)
1412.             break;
1413.     return otmp;
1414. }
1415. 
1416. void
1417. hit_bars(
1418.     struct obj **objp,    /* *objp will be set to NULL if object breaks */
1419.     coordxy objx, coordxy objy, /* hero's (when wielded) or missile's spot */
1420.     coordxy barsx, coordxy barsy, /* adjacent spot where bars are located */
1421.     unsigned breakflags)  /* breakage control */
1422. {
1423.     struct obj *otmp = *objp;
1424.     int obj_type = otmp->otyp;
1425.     boolean nodissolve = (levl[barsx][barsy].wall_info & W_NONDIGGABLE) != 0,
1426.             your_fault = (breakflags & BRK_BY_HERO) != 0,
1427.             melee_attk = (breakflags & BRK_MELEE) != 0;
1428.     int noise = 0;
1429. 
1430.     if (your_fault
1431.         ? hero_breaks(otmp, objx, objy, breakflags)
1432.         : breaks(otmp, objx, objy)) {
1433.         *objp = 0; /* object is now gone */
1434.         /* breakage makes its own noises */
1435.         if (obj_type == POT_ACID) {
1436.             if (cansee(barsx, barsy) && !nodissolve) {
1437.                 pline_The("iron bars are dissolved!");
1438.             } else {
1439.                 Soundeffect(se_angry_snakes, 100);
1440.                 You_hear(Hallucination ? "angry snakes!"
1441.                                        : "a hissing noise.");
1442.             }
1443.             if (!nodissolve)
1444.                 dissolve_bars(barsx, barsy);
1445.         }
1446.     } else {
1447.         if (!Deaf) {
1448.             static enum sound_effect_entries se[] = {
1449.                 se_zero_invalid,
1450.                 se_bars_whang, se_bars_whap, se_bars_flapp,
1451.                 se_bars_clink, se_bars_clonk
1452.             };
1453.             static const char *const barsounds[] = {
1454.                 "", "Whang", "Whap", "Flapp", "Clink", "Clonk"
1455.             };
1456.             int bsindx = (obj_type == BOULDER || obj_type == HEAVY_IRON_BALL)
1457.                          ? 1
1458.                          : harmless_missile(otmp) ? 2
1459.                          : is_flimsy(otmp) ? 3
1460.                          : (otmp->oclass == COIN_CLASS
1461.                             || objects[obj_type].oc_material == GOLD
1462.                             || objects[obj_type].oc_material == SILVER)
1463.                            ? 4
1464.                            : SIZE(barsounds) - 1;
1465. 
1466.             Soundeffect(se[bsindx], 100);
1467.             pline("%s!", barsounds[bsindx]);
1468.             nhUse(se[bsindx]);
1469.         }
1470.         if (!(harmless_missile(otmp) || is_flimsy(otmp)))
1471.             noise = 4 * 4;
1472. 
1473.         if (your_fault && (otmp->otyp == WAR_HAMMER
1474.                            || otmp->otyp == HEAVY_IRON_BALL)) {
1475.             /* iron ball isn't a weapon or wep-tool so doesn't use obj->spe;
1476.                weight is normally 480 but can be increased by increments
1477.                of 160 (scrolls of punishment read while already punished) */
1478.             int spe = ((otmp->otyp == HEAVY_IRON_BALL) /* 3+ for iron ball */
1479.                        ? ((int) otmp->owt / WT_IRON_BALL_INCR)
1480.                        : otmp->spe);
1481.             /* chance: used in saving throw for the bars; more likely to
1482.                break those when 'chance' is _lower_; acurrstr(): 3..25 */
1483.             int chance = (melee_attk ? 40 : 60) - acurrstr() - spe;
1484. 
1485.             if (!rn2(max(2, chance))) {
1486.                 You("break the bars apart!");
1487.                 dissolve_bars(barsx, barsy);
1488.                 noise = noise * 2;
1489.             }
1490.         }
1491. 
1492.         if (noise)
1493.             wake_nearto(barsx, barsy, noise);
1494.     }
1495. }
1496. 
1497. /* TRUE iff thrown/kicked/rolled object doesn't pass through iron bars */
1498. boolean
1499. hits_bars(
1500.     struct obj **obj_p,   /* *obj_p will be set to NULL if object breaks */
1501.     coordxy x, coordxy y,
1502.     coordxy barsx, coordxy barsy,
1503.     int always_hit,       /* caller can force a hit for items which would
1504.                            * fit through */
1505.     int whodidit)         /* 1==hero, 0=other, -1==just check whether it
1506.                            * will pass through */
1507. {
1508.     struct obj *otmp = *obj_p;
1509.     int obj_type = otmp->otyp;
1510.     boolean hits = always_hit;
1511. 
1512.     if (!hits)
1513.         switch (otmp->oclass) {
1514.         case WEAPON_CLASS: {
1515.             int oskill = objects[obj_type].oc_skill;
1516. 
1517.             hits = (oskill != -P_BOW && oskill != -P_CROSSBOW
1518.                     && oskill != -P_DART && oskill != -P_SHURIKEN
1519.                     && oskill != P_SPEAR
1520.                     && oskill != P_KNIFE); /* but not dagger */
1521.             break;
1522.         }
1523.         case ARMOR_CLASS:
1524.             hits = (objects[obj_type].oc_armcat != ARM_GLOVES);
1525.             break;
1526.         case TOOL_CLASS:
1527.             hits = (obj_type != SKELETON_KEY && obj_type != LOCK_PICK
1528.                     && obj_type != CREDIT_CARD && obj_type != TALLOW_CANDLE
1529.                     && obj_type != WAX_CANDLE && obj_type != LENSES
1530.                     && obj_type != TIN_WHISTLE && obj_type != MAGIC_WHISTLE);
1531.             break;
1532.         case ROCK_CLASS: /* includes boulder */
1533.             if (obj_type != STATUE || mons[otmp->corpsenm].msize > MZ_TINY)
1534.                 hits = TRUE;
1535.             break;
1536.         case FOOD_CLASS:
1537.             if (obj_type == CORPSE && mons[otmp->corpsenm].msize > MZ_TINY)
1538.                 hits = TRUE;
1539.             else
1540.                 hits = (obj_type == MEAT_STICK
1541.                         || obj_type == ENORMOUS_MEATBALL);
1542.             break;
1543.         case SPBOOK_CLASS:
1544.         case WAND_CLASS:
1545.         case BALL_CLASS:
1546.         case CHAIN_CLASS:
1547.             hits = TRUE;
1548.             break;
1549.         default:
1550.             break;
1551.         }
1552. 
1553.     if (hits && whodidit != -1) {
1554.         hit_bars(obj_p, x, y, barsx, barsy,
1555.                  (whodidit == 1) ? BRK_BY_HERO : 0);
1556.     }
1557. 
1558.     return hits;
1559. }
1560. 
1561. /*mthrowu.c*/