Source:NetHack 5.0.0/src/dothrow.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to dothrow.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/dothrow.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	dothrow.c	$NHDT-Date: 1737343372 2025/01/19 19:22:52 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.300 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2013. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /* Contains code for 't' (throw) */
7. 
8. #include "hack.h"
9. 
10. staticfn int throw_obj(struct obj *, int);
11. staticfn boolean ok_to_throw(int *);
12. staticfn int throw_ok(struct obj *);
13. staticfn void autoquiver(void);
14. staticfn struct obj *find_launcher(struct obj *);
15. staticfn int gem_accept(struct monst *, struct obj *);
16. staticfn boolean toss_up(struct obj *, boolean) NONNULLARG1;
17. staticfn void sho_obj_return_to_u(struct obj * obj);
18. staticfn void throwit_return(boolean);
19. staticfn void swallowit(struct obj *);
20. staticfn struct obj *return_throw_to_inv(struct obj *, long, boolean,
21.                                        struct obj *);
22. staticfn void tmiss(struct obj *, struct monst *, boolean);
23. staticfn int throw_gold(struct obj *);
24. staticfn void check_shop_obj(struct obj *, coordxy, coordxy, boolean);
25. staticfn void breakmsg(struct obj *, boolean);
26. staticfn boolean mhurtle_step(genericptr_t, coordxy, coordxy);
27. 
28. /* uwep might already be removed from inventory so test for W_WEP instead;
29.    for Valk+Mjollnir, caller needs to validate the strength requirement */
30. #define AutoReturn(o,wmsk) \
31.     ((((wmsk) & W_WEP) != 0                                             \
32.       && ((o)->otyp == AKLYS                                            \
33.           || ((o)->oartifact == ART_MJOLLNIR && Role_if(PM_VALKYRIE)))) \
34.      || (o)->otyp == BOOMERANG)
35. 
36. /* gt.thrownobj (decl.c) tracks an object until it lands */
37. 
38. int
39. multishot_class_bonus(
40.     int pm,
41.     struct obj *ammo,
42.     struct obj *launcher) /* can be NULL */
43. {
44.     int multishot = 0;
45.     schar skill = objects[ammo->otyp].oc_skill;
46. 
47.     switch (pm) {
48.     case PM_CAVE_DWELLER:
49.         /* give bonus for low-tech gear */
50.         if (skill == -P_SLING || skill == P_SPEAR)
51.             multishot++;
52.         break;
53.     case PM_MONK:
54.         /* allow higher volley count despite skill limitation */
55.         if (skill == -P_SHURIKEN)
56.             multishot++;
57.         break;
58.     case PM_RANGER:
59.         /* arbitrary; encourage use of other missiles beside daggers */
60.         if (skill != P_DAGGER)
61.             multishot++;
62.         break;
63.     case PM_ROGUE:
64.         /* possibly should add knives... */
65.         if (skill == P_DAGGER)
66.             multishot++;
67.         break;
68.     case PM_NINJA:
69.         if (skill == -P_SHURIKEN || skill == -P_DART)
70.             multishot++;
71.         FALLTHROUGH;
72.         /*FALLTHRU*/
73.     case PM_SAMURAI:
74.         /* role-specific launcher and its ammo */
75.         if (ammo->otyp == YA && launcher && launcher->otyp == YUMI)
76.             multishot++;
77.         break;
78.     default:
79.         break; /* No bonus */
80.     }
81. 
82.     return multishot;
83. }
84. 
85. /* Throw the selected object, asking for direction */
86. staticfn int
87. throw_obj(struct obj *obj, int shotlimit)
88. {
89.     struct obj *otmp, *oldslot;
90.     int multishot;
91.     schar skill;
92.     long wep_mask;
93.     boolean twoweap, weakmultishot;
94.     int res = ECMD_TIME;
95.     struct obj_split save_osplit = svc.context.objsplit;
96. 
97.     /* ask "in what direction?" */
98.     if (!getdir((char *) 0)) {
99.         /* No direction specified, so cancel the throw */
100.         res = ECMD_CANCEL; /* no time passes */
101.         goto unsplit_stack;
102.     }
103. 
104.     /*
105.      * Throwing gold is usually for getting rid of it when
106.      * a leprechaun approaches, or for bribing an oncoming
107.      * angry monster.  So throw the whole object.
108.      *
109.      * If the gold is in quiver, throw one coin at a time,
110.      * possibly using a sling.
111.      */
112.     if (obj->oclass == COIN_CLASS && obj != uquiver) {
113.         /* throw_gold will unsplit the stack itself if necessary and may have
114.            freed the object, so don't route through unsplit_stack here */
115.         return throw_gold(obj); /* check */
116.     }
117. 
118.     if (!canletgo(obj, "throw")) {
119.         res = ECMD_OK;
120.         goto unsplit_stack;
121.     }
122.     if (is_art(obj, ART_MJOLLNIR) && obj != uwep) {
123.         pline("%s must be wielded before it can be thrown.", The(xname(obj)));
124.         res = ECMD_OK;
125.         goto unsplit_stack;
126.     }
127.     if ((is_art(obj, ART_MJOLLNIR) && ACURR(A_STR) < STR19(25))
128.         || (obj->otyp == BOULDER && !throws_rocks(gy.youmonst.data))) {
129.         pline("It's too heavy.");
130.         res = ECMD_TIME;
131.         goto unsplit_stack;
132.     }
133.     if (!u.dx && !u.dy && !u.dz) {
134.         You("cannot throw an object at yourself.");
135.         res = ECMD_OK;
136.         goto unsplit_stack;
137.     }
138.     u_wipe_engr(2);
139.     if (!uarmg && obj->otyp == CORPSE && touch_petrifies(&mons[obj->corpsenm])
140.         && !Stone_resistance) {
141.         You("throw %s with your bare %s.",
142.             corpse_xname(obj, (const char *) 0, CXN_PFX_THE),
143.             /* throwing with one hand, but pluralize since the
144.                expression "with your bare hands" sounds better */
145.             makeplural(body_part(HAND)));
146.         Sprintf(svk.killer.name, "throwing %s bare-handed",
147.                 killer_xname(obj));
148.         instapetrify(svk.killer.name);
149.     }
150.     if (welded(obj)) {
151.         weldmsg(obj);
152.         res = ECMD_TIME;
153.         goto unsplit_stack;
154.     }
155.     if (is_wet_towel(obj))
156.         dry_a_towel(obj, -1, FALSE);
157. 
158.     /* Multishot calculations
159.      * (potential volley of up to N missiles; default for N is 1)
160.      */
161.     multishot = 1;
162.     skill = objects[obj->otyp].oc_skill;
163.     if (obj->quan > 1L /* no point checking if there's only 1 */
164.         /* ammo requires corresponding launcher be wielded */
165.         && (is_ammo(obj) ? matching_launcher(obj, uwep)
166.                          /* otherwise any stackable (non-ammo) weapon */
167.                          : obj->oclass == WEAPON_CLASS)
168.         && !(Confusion || Stunned)) {
169.         /* some roles don't get a volley bonus until becoming expert */
170.         weakmultishot = (Role_if(PM_WIZARD) || Role_if(PM_CLERIC)
171.                          || (Role_if(PM_HEALER) && skill != P_KNIFE)
172.                          || (Role_if(PM_TOURIST) && skill != -P_DART)
173.                          /* poor dexterity also inhibits multishot */
174.                          || Fumbling || ACURR(A_DEX) <= 6);
175. 
176.         /* Bonus if the player is proficient in this weapon... */
177.         switch (P_SKILL(weapon_type(obj))) {
178.         case P_EXPERT:
179.             multishot++;
180.             FALLTHROUGH;
181.         /*FALLTHRU*/
182.         case P_SKILLED:
183.             if (!weakmultishot)
184.                 multishot++;
185.             break;
186.         default: /* basic or unskilled: no bonus */
187.             break;
188.         }
189.         /* ...or is using a special weapon for their role... */
190.         multishot += multishot_class_bonus(Role_switch, obj, uwep);
191. 
192.         /* ...or using their race's special bow; no bonus for spears */
193.         if (!weakmultishot) {
194.             switch (Race_switch) {
195.             case PM_ELF:
196.                 if (obj->otyp == ELVEN_ARROW && uwep
197.                     && uwep->otyp == ELVEN_BOW)
198.                     multishot++;
199.                 break;
200.             case PM_ORC:
201.                 if (obj->otyp == ORCISH_ARROW && uwep
202.                     && uwep->otyp == ORCISH_BOW)
203.                     multishot++;
204.                 break;
205.             case PM_GNOME:
206.                 /* arbitrary; there isn't any gnome-specific gear */
207.                 if (skill == -P_CROSSBOW)
208.                     multishot++;
209.                 break;
210.             case PM_HUMAN:
211.             case PM_DWARF:
212.             default:
213.                 break; /* No bonus */
214.             }
215. 
216.             /* when launcher is own quest artifact, give extra +1 with any
217.                type of ammo appropriate for that launcher (compensates for
218.                elven and orcish rangers loss of bonus for use of racial bow
219.                plus racial arrows if they switch to the Longbow of Diana) */
220.             if (uwep && is_quest_artifact(uwep)
221.                 && ammo_and_launcher(obj, uwep))
222.                 ++multishot;
223.         }
224. 
225.         /* crossbows are slow to load and probably shouldn't allow multiple
226.            shots at all, but that would result in players never using them;
227.            instead, high strength is necessary to load and shoot quickly */
228.         if (multishot > 1 && skill == -P_CROSSBOW
229.             && ammo_and_launcher(obj, uwep)
230.             && (int) ACURRSTR < (Race_if(PM_GNOME) ? 16 : 18))
231.             multishot = rnd(multishot);
232. 
233.         multishot = rnd(multishot);
234.         if ((long) multishot > obj->quan)
235.             multishot = (int) obj->quan;
236.         if (shotlimit > 0 && multishot > shotlimit)
237.             multishot = shotlimit;
238.     }
239. 
240.     gm.m_shot.s = ammo_and_launcher(obj, uwep) ? TRUE : FALSE;
241.     /* give a message if shooting more than one, or if player
242.        attempted to specify a count */
243.     if (multishot > 1 || shotlimit > 0) {
244.         /* "You shoot N arrows." or "You throw N daggers." */
245.         You("%s %d %s.", gm.m_shot.s ? "shoot" : "throw",
246.             multishot, /* (might be 1 if player gave shotlimit) */
247.             (multishot == 1) ? singular(obj, xname) : xname(obj));
248.     }
249. 
250.     wep_mask = obj->owornmask;
251.     oldslot = 0;
252.     gm.m_shot.o = obj->otyp;
253.     gm.m_shot.n = multishot;
254.     for (gm.m_shot.i = 1; gm.m_shot.i <= gm.m_shot.n; gm.m_shot.i++) {
255.         twoweap = u.twoweap;
256.         assert(obj != NULL); /* m_shot.i <= m_shot.n guarantees this */
257.         /* split this object off from its slot if necessary */
258.         if (obj->quan > 1L) {
259.             otmp = splitobj(obj, 1L);
260.         } else {
261.             otmp = obj;
262.             if (otmp->owornmask)
263.                 remove_worn_item(otmp, FALSE);
264.             oldslot = obj->nobj;
265.             /* obj will leave inventory and may be freed by throwit, don't
266.                try to unsplit it from potential parent stack below */
267.             obj = (struct obj *) 0;
268.         }
269.         freeinv(otmp);
270.         throwit(otmp, wep_mask, twoweap, oldslot);
271.         encumber_msg();
272.     }
273.     gm.m_shot.n = gm.m_shot.i = 0;
274.     gm.m_shot.o = STRANGE_OBJECT;
275.     gm.m_shot.s = FALSE;
276. 
277.  unsplit_stack:
278.     /* might need to undo an object split.
279.      * We used to use freeinv(obj),addinv(obj) here, but that can
280.      * merge obj into another stack--usually quiver--even if it hadn't
281.      * been split from there (possibly triggering a panic in addinv),
282.      * and freeinv+addinv potentially has other side-effects.
283.      */
284.     if (obj && obj != uquiver
285.         && (obj->o_id == save_osplit.parent_oid
286.             || obj->o_id == save_osplit.child_oid)) {
287.         /* futureproofing: objsplit will have been affected if partial stack
288.            was thrown; objects will have been split off stack to throw. */
289.         svc.context.objsplit = save_osplit;
290.         (void) unsplitobj(obj);
291.     }
292.     return res;
293. }
294. 
295. /* common to dothrow() and dofire() */
296. staticfn boolean
297. ok_to_throw(int *shotlimit_p) /* (see dothrow()) */
298. {
299.     *shotlimit_p = LIMIT_TO_RANGE_INT(0, LARGEST_INT, gc.command_count);
300.     gm.multi = 0; /* reset; it's been used up */
301. 
302.     if (notake(gy.youmonst.data)) {
303.         You("are physically incapable of throwing or shooting anything.");
304.         return FALSE;
305.     } else if (nohands(gy.youmonst.data)) {
306.         You_cant("throw or shoot without hands."); /* not body_part(HAND) */
307.         return FALSE;
308.         /*[what about !freehand(), aside from cursed missile launcher?]*/
309.     }
310.     if (check_capacity((char *) 0))
311.         return FALSE;
312.     return TRUE;
313. }
314. 
315. /* getobj callback for object to be thrown */
316. staticfn int
317. throw_ok(struct obj *obj)
318. {
319.     if (!obj)
320.         return GETOBJ_EXCLUDE;
321. 
322.     if (obj->bknown && welded(obj)) /* not a candidate if known to be stuck */
323.         return GETOBJ_DOWNPLAY;
324. 
325.     if (AutoReturn(obj, obj->owornmask)
326.         /* to get here, obj is boomerang or is uwep and (alkys or Mjollnir) */
327.         && (obj->oartifact != ART_MJOLLNIR || ACURR(A_STR) >= STR19(25)))
328.         return GETOBJ_SUGGEST;
329. 
330.     if (obj->quan == 1 && (obj == uwep || (obj == uswapwep && u.twoweap)))
331.         return GETOBJ_DOWNPLAY;
332. 
333.     if (obj->oclass == COIN_CLASS)
334.         return GETOBJ_SUGGEST;
335. 
336.     if (!uslinging() && obj->oclass == WEAPON_CLASS)
337.         return GETOBJ_SUGGEST;
338.     /* Possible extension: exclude weapons that make no sense to throw,
339.        such as whips, bows, slings, rubber hoses. */
340. 
341.     if (uslinging() && obj->oclass == GEM_CLASS)
342.         return GETOBJ_SUGGEST;
343. 
344.     if (throws_rocks(gy.youmonst.data) && obj->otyp == BOULDER)
345.         return GETOBJ_SUGGEST;
346. 
347.     return GETOBJ_DOWNPLAY;
348. }
349. 
350. /* the #throw command */
351. int
352. dothrow(void)
353. {
354.     struct obj *obj;
355.     int shotlimit;
356. 
357.     /*
358.      * Since some characters shoot multiple missiles at one time,
359.      * allow user to specify a count prefix for 'f' or 't' to limit
360.      * number of items thrown (to avoid possibly hitting something
361.      * behind target after killing it, or perhaps to conserve ammo).
362.      *
363.      * Prior to 3.3.0, command ``3t meant ``t(shoot) t(shoot) t(shoot)
364.      * and took 3 turns.  Now it means ``t(shoot at most 3 missiles).
365.      *
366.      * [3.6.0:  shot count setup has been moved into ok_to_throw().]
367.      */
368.     if (!ok_to_throw(&shotlimit))
369.         return ECMD_OK;
370. 
371.     obj = getobj("throw", throw_ok, GETOBJ_PROMPT | GETOBJ_ALLOWCNT);
372.     /* it is also possible to throw food */
373.     /* (or jewels, or iron balls... ) */
374. 
375.     return obj ? throw_obj(obj, shotlimit) : ECMD_CANCEL;
376. }
377. 
378. /* KMH -- Automatically fill quiver */
379. /* Suggested by Jeffrey Bay <jbay@convex.hp.com> */
380. staticfn void
381. autoquiver(void)
382. {
383.     struct obj *otmp, *oammo = 0, *omissile = 0, *omisc = 0, *altammo = 0;
384. 
385.     if (uquiver)
386.         return;
387. 
388.     /* Scan through the inventory */
389.     for (otmp = gi.invent; otmp; otmp = otmp->nobj) {
390.         if (otmp->owornmask || otmp->oartifact || !otmp->dknown) {
391.             ; /* Skip it */
392.         } else if (otmp->otyp == ROCK
393.                    /* seen rocks or known flint or known glass */
394.                    || (otmp->otyp == FLINT
395.                        && objects[otmp->otyp].oc_name_known)
396.                    || (otmp->oclass == GEM_CLASS
397.                        && objects[otmp->otyp].oc_material == GLASS
398.                        && objects[otmp->otyp].oc_name_known)) {
399.             if (uslinging())
400.                 oammo = otmp;
401.             else if (ammo_and_launcher(otmp, uswapwep))
402.                 altammo = otmp;
403.             else if (!omisc)
404.                 omisc = otmp;
405.         } else if (otmp->oclass == GEM_CLASS) {
406.             ; /* skip non-rock gems--they're ammo but
407.                  player has to select them explicitly */
408.         } else if (is_ammo(otmp)) {
409.             if (ammo_and_launcher(otmp, uwep))
410.                 /* Ammo matched with launcher (bow+arrow, crossbow+bolt) */
411.                 oammo = otmp;
412.             else if (ammo_and_launcher(otmp, uswapwep))
413.                 altammo = otmp;
414.             else
415.                 /* Mismatched ammo (no better than an ordinary weapon) */
416.                 omisc = otmp;
417.         } else if (is_missile(otmp)) {
418.             /* Missile (dart, shuriken, etc.) */
419.             omissile = otmp;
420.         } else if (otmp->oclass == WEAPON_CLASS && throwing_weapon(otmp)) {
421.             /* Ordinary weapon */
422.             if (objects[otmp->otyp].oc_skill == P_DAGGER && !omissile)
423.                 omissile = otmp;
424.             else if (otmp->otyp == AKLYS)
425.                 continue;
426.             else
427.                 omisc = otmp;
428.         }
429.     }
430. 
431.     /* Pick the best choice */
432.     if (oammo)
433.         setuqwep(oammo);
434.     else if (omissile)
435.         setuqwep(omissile);
436.     else if (altammo)
437.         setuqwep(altammo);
438.     else if (omisc)
439.         setuqwep(omisc);
440. 
441.     return;
442. }
443. 
444. /* look through hero inventory for launcher matching ammo,
445.    avoiding known cursed items. Returns NULL if no match. */
446. staticfn struct obj *
447. find_launcher(struct obj *ammo)
448. {
449.     struct obj *otmp, *oX;
450. 
451.     if (!ammo)
452.         return (struct obj *) 0;
453. 
454.     for (oX = 0, otmp = gi.invent; otmp; otmp = otmp->nobj) {
455.         if (otmp->cursed && otmp->bknown)
456.             continue; /* known to be cursed, so skip */
457.         if (ammo_and_launcher(ammo, otmp)) {
458.             if (otmp->bknown)
459.                 return otmp; /* known-B or known-U (known-C won't get here) */
460.             if (!oX)
461.                 oX = otmp; /* unknown-BUC; used if no known-BU item found */
462.         }
463.     }
464.     return oX;
465. }
466. 
467. /* the #fire command -- throw from the quiver or use wielded polearm */
468. int
469. dofire(void)
470. {
471.     int shotlimit;
472.     struct obj *obj;
473.     /* AutoReturn() verifies Valkyrie if weapon is Mjollnir, but it relies
474.        on its caller to make sure hero is strong enough to throw that */
475.     boolean uwep_Throw_and_Return = (uwep && AutoReturn(uwep, uwep->owornmask)
476.                                      && (uwep->oartifact != ART_MJOLLNIR
477.                                          || ACURR(A_STR) >= STR19(25))),
478.             skip_fireassist = FALSE;
479.     int altres, res = ECMD_OK;
480. 
481.     /*
482.      * Same as dothrow(), except we use quivered missile instead
483.      * of asking what to throw/shoot.  [Note: with the advent of
484.      * fireassist that is no longer accurate...]
485.      *
486.      * If hero is wielding a thrown-and-return weapon and quiver
487.      * is empty or contains ammo, use the wielded weapon (won't
488.      * have any ammo's launcher wielded due to the weapon).
489.      * If quiver is empty, use autoquiver to fill it when the
490.      * corresponding option is on.
491.      * If option is off or autoquiver doesn't select anything,
492.      * we ask what to throw.
493.      * Then we put the chosen item into the quiver slot unless
494.      * it is already in another slot.  [Matters most if it is a
495.      * stack but also matters for single item if this throw gets
496.      * aborted (ESC at the direction prompt).]
497.      */
498.     if (!ok_to_throw(&shotlimit))
499.         return ECMD_OK;
500. 
501.     obj = uquiver;
502.     /* if wielding a throw-and-return weapon, throw it if quiver is empty
503.        or has ammo rather than missiles [since the throw/return weapon is
504.        wielded, the ammo's launcher isn't; the ammo-only policy avoids
505.        throwing Mjollnir if quiver contains daggers] */
506.     if (uwep_Throw_and_Return && (!obj || is_ammo(obj))) {
507.         obj = uwep;
508.         skip_fireassist = TRUE;
509. 
510.     } else if (!obj) {
511.         if (!flags.autoquiver) {
512.             /* if we're wielding a polearm, apply it */
513.             if (uwep && is_pole(uwep)) {
514.                 return use_pole(uwep, TRUE);
515.             /* if we're wielding a bullwhip, apply it */
516.             } else if (uwep && uwep->otyp == BULLWHIP) {
517.                 return use_whip(uwep);
518.             } else if (iflags.fireassist
519.                        && uswapwep && is_pole(uswapwep)
520.                        && !(uswapwep->cursed && uswapwep->bknown)) {
521.                 /* we have a known not-cursed polearm as swap weapon.
522.                    swap to it and retry */
523.                 cmdq_add_ec(CQ_CANNED, doswapweapon);
524.                 cmdq_add_ec(CQ_CANNED, dofire);
525.                 return ECMD_OK; /* haven't taken any time yet */
526.             } else {
527.                 You("have no ammunition readied.");
528.             }
529.         } else {
530.             autoquiver();
531.             obj = uquiver;
532.             if (obj) {
533.                 /* give feedback if quiver has now been filled */
534.                 uquiver->owornmask &= ~W_QUIVER; /* less verbose */
535.                 prinv("You ready:", obj, 0L);
536.                 uquiver->owornmask |= W_QUIVER;
537.             } else {
538.                 You("have nothing appropriate for your quiver.");
539.             }
540.         }
541.     }
542. 
543.     /* if autoquiver is disabled or has failed, prompt for missile */
544.     if (!obj) {
545.         /* in case we're using ^A to repeat prior 'f' command, don't
546.            use direction of previous throw as getobj()'s choice here */
547.         gi.in_doagain = 0;
548. 
549.         /* this gives its own feedback about populating the quiver slot */
550.         res = doquiver_core("fire");
551.         if (res != ECMD_OK && res != ECMD_TIME)
552.             return res;
553. 
554.         obj = uquiver;
555.     }
556. 
557.     if (uquiver && is_ammo(uquiver) && iflags.fireassist
558.         && !skip_fireassist) {
559.         struct obj *olauncher;
560. 
561.         if (uwep && is_pole(uwep) && could_pole_mon())
562.             return use_pole(uwep, TRUE);
563.         /* Try to find a launcher */
564.         if (ammo_and_launcher(uquiver, uwep)) {
565.             obj = uquiver;
566.         } else if (ammo_and_launcher(uquiver, uswapwep)) {
567.             /* swap weapons and retry fire */
568.             cmdq_add_ec(CQ_CANNED, doswapweapon);
569.             cmdq_add_ec(CQ_CANNED, dofire);
570.             return res;
571.         } else if ((olauncher = find_launcher(uquiver)) != 0) {
572.             /* wield launcher, retry fire */
573.             if (uwep && !flags.pushweapon)
574.                 cmdq_add_ec(CQ_CANNED, doswapweapon);
575.             cmdq_add_ec(CQ_CANNED, dowield);
576.             cmdq_add_key(CQ_CANNED, olauncher->invlet);
577.             cmdq_add_ec(CQ_CANNED, dofire);
578.             return res;
579.         }
580.     }
581. 
582.     altres = obj ? throw_obj(obj, shotlimit) : ECMD_CANCEL;
583.     /* fire can take time by filling quiver (if that causes something which
584.        was wielded to be unwielded) even if the throw itself gets cancelled */
585.     return (res == ECMD_TIME) ? res : altres;
586. }
587. 
588. /* if in midst of multishot shooting/throwing, stop early */
589. void
590. endmultishot(boolean verbose)
591. {
592.     if (gm.m_shot.i < gm.m_shot.n) {
593.         if (verbose && !svc.context.mon_moving) {
594.             You("stop %s after the %d%s %s.",
595.                 gm.m_shot.s ? "firing" : "throwing",
596.                 gm.m_shot.i, ordin(gm.m_shot.i),
597.                 gm.m_shot.s ? "shot" : "toss");
598.         }
599.         gm.m_shot.n = gm.m_shot.i; /* make current shot be the last */
600.     }
601. }
602. 
603. /* Object hits floor at hero's feet.
604.    Called from drop(), throwit(), hold_another_object(), litter(). */
605. void
606. hitfloor(
607.     struct obj *obj,
608.     boolean verbosely) /* usually True; False if caller has given drop mesg */
609. {
610.     if (IS_SOFT(levl[u.ux][u.uy].typ) || u.uinwater || u.uswallow) {
611.         dropy(obj);
612.         return;
613.     }
614.     if (IS_ALTAR(levl[u.ux][u.uy].typ)) {
615.         doaltarobj(obj);
616.     } else if (verbosely) {
617.         const char *verb = (obj->otyp == WAN_STRIKING) ? "strike" : "hit";
618.         const char *surf = surface(u.ux, u.uy);
619.         struct trap *t = t_at(u.ux, u.uy);
620. 
621.         /* describe something that might keep the object where it is
622.            or precede next message stating that it falls */
623.         if (t && t->tseen) {
624.             switch (t->ttyp) {
625.             case TRAPDOOR:
626.                 surf = "trap door";
627.                 break;
628.             case HOLE:
629.                 surf = "edge of the hole";
630.                 break;
631.             case PIT:
632.             case SPIKED_PIT:
633.                 surf = "edge of the pit";
634.                 break;
635.             default:
636.                 break;
637.             }
638.         }
639.         pline("%s %s the %s.", Doname2(obj), otense(obj, verb), surf);
640.     }
641. 
642.     if (hero_breaks(obj, u.ux, u.uy, BRK_FROM_INV))
643.         return;
644.     if (ship_object(obj, u.ux, u.uy, FALSE))
645.         return;
646.     dropz(obj, TRUE);
647. }
648. 
649. /*
650.  * Walk a path from src_cc to dest_cc, calling a proc for each location
651.  * except the starting one.  If the proc returns FALSE, stop walking
652.  * and return FALSE.  If stopped early, dest_cc will be the location
653.  * before the failed callback.
654.  */
655. boolean
656. walk_path(
657.     coord *src_cc, coord *dest_cc,
658.     boolean (*check_proc)(genericptr_t, coordxy, coordxy),
659.     genericptr_t arg)
660. {
661.     int err;
662.     coordxy x, y, dx, dy, x_change, y_change, i, prev_x, prev_y;
663.     boolean keep_going = TRUE;
664. 
665.     /* Use Bresenham's Line Algorithm to walk from src to dest.
666.      *
667.      * This should be replaced with a more versatile algorithm
668.      * since it handles slanted moves in a suboptimal way.
669.      * Going from 'x' to 'y' needs to pass through 'z', and will
670.      * fail if there's an obstacle there, but it could choose to
671.      * pass through 'Z' instead if that way imposes no obstacle.
672.      *     ..y          .Zy
673.      *     xz.    vs    x..
674.      * Perhaps we should check both paths and accept whichever
675.      * one isn't blocked.  But then multiple zigs and zags could
676.      * potentially produce a meandering path rather than the best
677.      * attempt at a straight line.  And (*check_proc)() would
678.      * need to work more like 'travel', distinguishing between
679.      * testing a possible move and actually attempting that move.
680.      */
681.     dx = dest_cc->x - src_cc->x;
682.     dy = dest_cc->y - src_cc->y;
683.     prev_x = x = src_cc->x;
684.     prev_y = y = src_cc->y;
685. 
686.     if (dx < 0) {
687.         x_change = -1;
688.         dx = -dx;
689.     } else {
690.         x_change = 1;
691.     }
692.     if (dy < 0) {
693.         y_change = -1;
694.         dy = -dy;
695.     } else {
696.         y_change = 1;
697.     }
698.     i = err = 0;
699.     if (dx < dy) {
700.         while (i++ < dy) {
701.             prev_x = x;
702.             prev_y = y;
703.             y += y_change;
704.             err += dx << 1;
705.             if (err > dy) {
706.                 x += x_change;
707.                 err -= dy << 1;
708.             }
709.             /* check for early exit condition */
710.             if (!(keep_going = (*check_proc)(arg, x, y)))
711.                 break;
712.         }
713.     } else {
714.         while (i++ < dx) {
715.             prev_x = x;
716.             prev_y = y;
717.             x += x_change;
718.             err += dy << 1;
719.             if (err > dx) {
720.                 y += y_change;
721.                 err -= dx << 1;
722.             }
723.             /* check for early exit condition */
724.             if (!(keep_going = (*check_proc)(arg, x, y)))
725.                 break;
726.         }
727.     }
728. 
729.     if (keep_going)
730.         return TRUE; /* successful */
731. 
732.     dest_cc->x = prev_x;
733.     dest_cc->y = prev_y;
734.     return FALSE;
735. }
736. 
737. /* hack for hurtle_step() -- it ought to be changed to take an argument
738.    indicating lev/fly-to-dest vs lev/fly-to-dest-minus-one-land-on-dest
739.    vs drag-to-dest; original callers use first mode, jumping wants second,
740.    grappling hook backfire and thrown chained ball need third */
741. boolean
742. hurtle_jump(genericptr_t arg, coordxy x, coordxy y)
743. {
744.     boolean res;
745.     long save_EWwalking = EWwalking;
746. 
747.     /* prevent jumping over water from being placed in that water */
748.     EWwalking |= I_SPECIAL;
749.     res = hurtle_step(arg, x, y);
750.     EWwalking = save_EWwalking;
751.     return res;
752. }
753. 
754. /*
755.  * Single step for the hero flying through the air from jumping, flying,
756.  * etc.  Called from hurtle() and jump() via walk_path().  We expect the
757.  * argument to be a pointer to an integer -- the range -- which is
758.  * used in the calculation of points off if we hit something.
759.  *
760.  * Bumping into monsters won't cause damage but will wake them and make
761.  * them angry.  Auto-pickup isn't done, since you don't have control over
762.  * your movements at the time.
763.  *
764.  * Possible additions/changes:
765.  *      o really attack monster if we hit one
766.  *      o set stunned if we hit a wall or door
767.  *      o reset nomul when we stop
768.  *      o creepy feeling if pass through monster (if ever implemented...)
769.  *      o bounce off walls
770.  *      o let jumps go over boulders
771.  */
772. boolean
773. hurtle_step(genericptr_t arg, coordxy x, coordxy y)
774. {
775.     coordxy ox, oy;
776.     int *range = (int *) arg;
777.     struct obj *obj;
778.     struct monst *mon;
779.     boolean may_pass = TRUE, via_jumping, stopping_short;
780.     struct trap *ttmp;
781.     struct rm *lev;
782.     int ltyp, dmg = 0;
783. 
784.     if (!isok(x, y)) {
785.         You_feel("the spirits holding you back.");
786.         return FALSE;
787.     } else if (!in_out_region(x, y)) {
788.         return FALSE;
789.     } else if (*range == 0) {
790.         return FALSE; /* previous step wants to stop now */
791.     }
792.     via_jumping = (EWwalking & I_SPECIAL) != 0L;
793.     stopping_short = (via_jumping && *range < 2);
794.     lev = &levl[x][y];
795.     ltyp = lev->typ;
796. 
797.     if (!Passes_walls || !(may_pass = may_passwall(x, y))) {
798.         const char *why = NULL;
799.         boolean diagonal = (u.ux - x) != 0 && (u.uy - y) != 0,
800.                 open_door = IS_DOOR(ltyp) && (lev->doormask & D_ISOPEN) != 0,
801.                 odoor_diag = open_door && diagonal;
802. 
803.         if (IS_OBSTRUCTED(levl[x][y].typ)
804.             || closed_door(x, y) || odoor_diag) {
805.             why = IS_TREE(ltyp) ? "bumping into a tree"
806.                   : IS_OBSTRUCTED(ltyp) ? "bumping into a wall"
807.                     : odoor_diag ? "bumping into a door frame"
808.                       : "bumping into a closed door";
809.             if (odoor_diag)
810.                 You("hit the door frame!");
811.             pline("Ouch!");
812.         } else if (ltyp == IRONBARS) {
813.             why = "crashing into iron bars";
814.             You("crash into some iron bars.  Ouch!");
815.         } else if ((obj = sobj_at(BOULDER, x, y)) != 0) {
816.             why = "bumping into a boulder";
817.             You("bump into a %s.  Ouch!", xname(obj));
818.         }  else if (!may_pass) {
819.             /* did we hit a no-dig non-wall position? */
820.             why = "touching the edge of the universe";
821.             You("smack into something!");
822.         } else if (diagonal
823.                    && bad_rock(gy.youmonst.data, u.ux, y)
824.                    && bad_rock(gy.youmonst.data, x, u.uy)) {
825.             boolean too_much = (gi.invent
826.                        && (inv_weight() + weight_cap() > WT_TOOMUCH_DIAGONAL));
827. 
828.             if (bigmonst(gy.youmonst.data) || too_much) {
829.                 why = "wedging into a narrow crevice";
830.                 You("%sget forcefully wedged into a crevice.",
831.                     too_much ? "and all your belongings " : "");
832.             }
833.         }
834.         if (why) {
835.             dmg = rnd(2 + *range);
836.             losehp(Maybe_Half_Phys(dmg), why, KILLED_BY);
837.             wake_nearto(x, y, 10);
838.             return FALSE;
839.         }
840.     }
841. 
842.     if ((mon = m_at(x, y)) != 0
843. #if 0   /* we can't include these two exceptions unless we know we're
844.          * going to end up past the current spot rather than on it;
845.          * for that, we need to know that the range is not exhausted
846.          * and also that the next spot doesn't contain an obstacle */
847.         && !(mon->mundetected && hides_under(mon) && (Flying || Levitation))
848.         && !(mon->mundetected && mon->data->mlet == S_EEL
849.              && (Flying || Levitation || Wwalking))
850. #endif
851.         ) {
852.         const char *mnam;
853.         int glyph = glyph_at(x, y);
854. 
855.         mon->mundetected = 0; /* wakeup() will handle mimic */
856.         /* after unhiding; combination of a_monnam() and some_mon_nam();
857.            yields "someone" or "something" instead of "it" for unseen mon */
858.         mnam = x_monnam(mon, ARTICLE_A, (char *) 0,
859.                         ((has_mgivenname(mon) ? SUPPRESS_SADDLE : 0)
860.                          | AUGMENT_IT),
861.                         FALSE);
862.         if (!glyph_is_monster(glyph) && !glyph_is_invisible(glyph))
863.             You("find %s by bumping into %s.", mnam, noit_mhim(mon));
864.         else
865.             You("bump into %s.", mnam);
866.         wakeup(mon, FALSE);
867.         if (!canspotmon(mon))
868.             map_invisible(mon->mx, mon->my);
869.         setmangry(mon, FALSE);
870.         if (touch_petrifies(mon->data)
871.             /* this is a bodily collision, so check for body armor */
872.             && !uarmu && !uarm && !uarmc) {
873.             Sprintf(svk.killer.name, "bumping into %s",
874.                     an(pmname(mon->data, NEUTRAL)));
875.             instapetrify(svk.killer.name);
876.         }
877.         if (touch_petrifies(gy.youmonst.data)
878.             && !which_armor(mon, W_ARMU | W_ARM | W_ARMC)) {
879.             minstapetrify(mon, TRUE);
880.         }
881.         wake_nearto(x, y, 10);
882.         return FALSE;
883.     }
884. 
885.     if ((u.ux - x) && (u.uy - y)
886.         && bad_rock(gy.youmonst.data, u.ux, y)
887.         && bad_rock(gy.youmonst.data, x, u.uy)) {
888.         /* Move at a diagonal. */
889.         if (Sokoban) {
890.             You("come to an abrupt halt!");
891.             return FALSE;
892.         }
893.     }
894. 
895.     /* caller has already determined that dragging the ball is allowed;
896.        if ball is carried we might still need to drag the chain */
897.     if (Punished) {
898.         int bc_control;
899.         coordxy ballx, bally, chainx, chainy;
900.         boolean cause_delay;
901. 
902.         if (drag_ball(x, y, &bc_control, &ballx, &bally, &chainx,
903.                       &chainy, &cause_delay, TRUE))
904.             move_bc(0, bc_control, ballx, bally, chainx, chainy);
905.     }
906. 
907.     ox = u.ux;
908.     oy = u.uy;
909.     u_on_newpos(x, y); /* set u.<ux,uy>, u.usteed-><mx,my>; cliparound(); */
910.     newsym(ox, oy);    /* update old position */
911.     vision_recalc(1);  /* update for new position */
912.     flush_screen(1);
913.     /* if terrain type changes, levitation or flying might become blocked
914.        or unblocked; might issue message, so do this after map+vision has
915.        been updated for new location instead of right after u_on_newpos() */
916.     if (ltyp != levl[ox][oy].typ)
917.         switch_terrain();
918. 
919.     /* might be entering a special room (treasure zoo, thrown room, &c) that
920.        has a first-time entry message, or leaving shop with unpaid goods */
921.     check_special_room(FALSE);
922. 
923.     if (is_pool(x, y) && !u.uinwater) {
924.         if (is_waterwall(x, y) || !(Levitation || Flying || Wwalking)) {
925.             /* couldn't move while hurtling; allow movement now so that
926.                drown() will give a chance to crawl out of pool and survive */
927.             gm.multi = 0;
928.             (void) drown();
929.             return FALSE;
930.         } else if (!Is_waterlevel(&u.uz) && !stopping_short) {
931.             Norep("You move over %s.", an(is_moat(x, y) ? "moat" : "pool"));
932.         }
933.     } else if (is_lava(x, y) && !stopping_short) {
934.         Norep("You move over some lava.");
935.     }
936. 
937.     /* FIXME:
938.      * Each trap should really trigger on the recoil if it would
939.      * trigger during normal movement. However, not all the possible
940.      * side-effects of this are tested [as of 3.4.0] so we trigger
941.      * those that we have tested, and offer a message for the ones
942.      * that we have not yet tested.
943.      */
944.     if ((ttmp = t_at(x, y)) != 0) {
945.         if (stopping_short) {
946.             ; /* see the comment above hurtle_jump() */
947.         } else if (ttmp->ttyp == MAGIC_PORTAL) {
948.             dotrap(ttmp, NO_TRAP_FLAGS);
949.             return FALSE;
950.         } else if (ttmp->ttyp == VIBRATING_SQUARE) {
951.             pline("The ground vibrates as you pass it.");
952.             dotrap(ttmp, NO_TRAP_FLAGS); /* doesn't print messages */
953.         } else if (ttmp->ttyp == FIRE_TRAP) {
954.             dotrap(ttmp, NO_TRAP_FLAGS);
955.         } else if ((is_pit(ttmp->ttyp) || is_hole(ttmp->ttyp)) && Sokoban) {
956.             /* air currents overcome the recoil in Sokoban;
957.                when jumping, caller performs last step and enters trap */
958.             if (!via_jumping)
959.                 dotrap(ttmp, NO_TRAP_FLAGS);
960.             *range = 0;
961.             return TRUE;
962.         } else {
963.             if (ttmp->tseen)
964.                 You("pass right over %s.", an(trapname(ttmp->ttyp, FALSE)));
965.         }
966.     }
967.     if (--*range < 0) /* make sure our range never goes negative */
968.         *range = 0;
969.     if (*range != 0)
970.         nh_delay_output();
971.     return TRUE;
972. }
973. 
974. /* used by mhurtle_step() for actual hurtling and also to vary message
975.    if target will/won't change location when knocked back */
976. boolean
977. will_hurtle(struct monst *mon, coordxy x, coordxy y)
978. {
979.     if (!isok(x, y))
980.         return FALSE;
981.     /* redundant when called by mhurtle() but needed for mhitm_knockback() */
982.     if (mon->data->msize >= MZ_HUGE || mon == u.ustuck || mon->mtrapped)
983.         return FALSE;
984.     /*
985.      * TODO: Treat walls, doors, iron bars, etc. specially
986.      * rather than just stopping before.
987.      */
988.     return goodpos(x, y, mon, MM_IGNOREWATER | MM_IGNORELAVA);
989. }
990. 
991. staticfn boolean
992. mhurtle_step(genericptr_t arg, coordxy x, coordxy y)
993. {
994.     struct monst *mon = (struct monst *) arg;
995.     struct monst *mtmp;
996. 
997.     if (!isok(x, y))
998.         return FALSE;
999. 
1000.     if (will_hurtle(mon, x, y) && m_in_out_region(mon, x, y)) {
1001.         int res;
1002. 
1003.         if (mon != u.usteed) {
1004.             remove_monster(mon->mx, mon->my);
1005.             newsym(mon->mx, mon->my);
1006.             place_monster(mon, x, y);
1007.             newsym(mon->mx, mon->my);
1008.         } else {
1009.             /* steed is hurtling, move hero which will also move steed */
1010.             u.ux0 = u.ux, u.uy0 = u.uy;
1011.             u_on_newpos(x, y);
1012.             newsym(u.ux0, u.uy0); /* update old position */
1013.             vision_recalc(0); /* new location => different lines of sight */
1014.         }
1015.         flush_screen(1);
1016.         nh_delay_output();
1017.         set_apparxy(mon);
1018.         if (is_waterwall(x, y))
1019.             return FALSE;
1020.         res = mintrap(mon, HURTLING);
1021.         if (res == Trap_Killed_Mon
1022.             || res == Trap_Caught_Mon
1023.             || res == Trap_Moved_Mon)
1024.             return FALSE;
1025.         return TRUE;
1026.     }
1027.     if ((mtmp = m_at(x, y)) != 0 && mtmp != mon) {
1028.         if (canseemon(mon) || canseemon(mtmp))
1029.             pline("%s bumps into %s.", Monnam(mon), a_monnam(mtmp));
1030.         wakeup(mtmp, !svc.context.mon_moving);
1031.         /* check whether 'mon' is turned to stone by touching 'mtmp' */
1032.         if (touch_petrifies(mtmp->data)
1033.             && !which_armor(mon, W_ARMU | W_ARM | W_ARMC)) {
1034.             minstapetrify(mon, !svc.context.mon_moving);
1035.             newsym(mon->mx, mon->my);
1036.         }
1037.         /* and whether 'mtmp' is turned to stone by being touched by 'mon' */
1038.         if (touch_petrifies(mon->data)
1039.             && !which_armor(mtmp, W_ARMU | W_ARM | W_ARMC)) {
1040.             minstapetrify(mtmp, !svc.context.mon_moving);
1041.             newsym(mtmp->mx, mtmp->my);
1042.         }
1043.     } else if (u_at(x, y)) {
1044.         /* a monster has caused 'mon' to hurtle against hero */
1045.         pline("%s bumps into you.", Some_Monnam(mon));
1046.         stop_occupation();
1047.         /* check whether 'mon' is turned to stone by touching poly'd hero */
1048.         if (Upolyd && touch_petrifies(gy.youmonst.data)
1049.             && !which_armor(mon, W_ARMU | W_ARM | W_ARMC)) {
1050.             /* give poly'd hero credit/blame despite a monster causing it */
1051.             minstapetrify(mon, TRUE);
1052.             newsym(mon->mx, mon->my);
1053.         }
1054.         /* and whether hero is turned to stone by being touched by 'mon' */
1055.         if (touch_petrifies(mon->data) && !(uarmu || uarm || uarmc)) {
1056.             Snprintf(svk.killer.name, sizeof svk.killer.name,
1057.                      "being hit by %s",
1058.                      /* combine m_monnam() and noname_monnam():
1059.                         "{your,a} hurtling cockatrice" w/o assigned name */
1060.                      x_monnam(mon, mon->mtame ? ARTICLE_YOUR : ARTICLE_A,
1061.                               "hurtling", EXACT_NAME | SUPPRESS_NAME, FALSE));
1062.             instapetrify(svk.killer.name);
1063.             newsym(u.ux, u.uy);
1064.         }
1065.     }
1066. 
1067.     return FALSE;
1068. }
1069. 
1070. /*
1071.  * The player moves through the air for a few squares as a result of
1072.  * throwing or kicking something.
1073.  *
1074.  * dx and dy should be the direction of the hurtle, not of the original
1075.  * kick or throw.
1076.  */
1077. void
1078. hurtle(int dx, int dy, int range, boolean verbose)
1079. {
1080.     coord uc, cc;
1081. 
1082.     /* The chain is stretched vertically, so you shouldn't be able to move
1083.      * very far diagonally.  The premise that you should be able to move one
1084.      * spot leads to calculations that allow you to only move one spot away
1085.      * from the ball, if you are levitating over the ball, or one spot
1086.      * towards the ball, if you are at the end of the chain.  Rather than
1087.      * bother with all of that, assume that there is no slack in the chain
1088.      * for diagonal movement, give the player a message and return.
1089.      */
1090.     if (Punished && !carried(uball)) {
1091.         You_feel("a tug from the iron ball.");
1092.         nomul(0);
1093.         return;
1094.     } else if (u.utrap) {
1095.         You("are anchored by the %s.",
1096.             (u.utraptype == TT_WEB) ? "web"
1097.             : (u.utraptype == TT_LAVA) ? hliquid("lava")
1098.               : (u.utraptype == TT_INFLOOR) ? surface(u.ux, u.uy)
1099.                 : (u.utraptype == TT_BURIEDBALL) ? "buried ball"
1100.                   : "trap");
1101.         nomul(0);
1102.         return;
1103.     }
1104. 
1105.     /* make sure dx and dy are [-1,0,1] */
1106.     dx = sgn(dx);
1107.     dy = sgn(dy);
1108. 
1109.     if (!range || (!dx && !dy) || u.ustuck)
1110.         return; /* paranoia */
1111. 
1112.     nomul(-range);
1113.     gm.multi_reason = "moving through the air";
1114.     gn.nomovemsg = ""; /* it just happens */
1115.     if (verbose)
1116.         You("%s in the opposite direction.",
1117.             (range > 1) ? "hurtle" : "float");
1118.     /* if we're in the midst of shooting multiple projectiles, stop */
1119.     endmultishot(TRUE);
1120.     uc.x = u.ux;
1121.     uc.y = u.uy;
1122.     /* this setting of cc is only correct if dx and dy are [-1,0,1] only */
1123.     cc.x = u.ux + (dx * range);
1124.     cc.y = u.uy + (dy * range);
1125.     (void) walk_path(&uc, &cc, hurtle_step, (genericptr_t) &range);
1126. }
1127. 
1128. /* Move a monster through the air for a few squares. */
1129. void
1130. mhurtle(struct monst *mon, int dx, int dy, int range)
1131. {
1132.     coord mc, cc;
1133. 
1134.     wakeup(mon, !svc.context.mon_moving);
1135.     /* At the very least, debilitate the monster */
1136.     mon->movement = 0;
1137.     mon->mstun = 1;
1138. 
1139.     /* Is the monster stuck or too heavy to push?
1140.      * (very large monsters have too much inertia, even floaters and flyers)
1141.      */
1142.     if (mon->data->msize >= MZ_HUGE || mon == u.ustuck || mon->mtrapped) {
1143.         if (canseemon(mon))
1144.             pline("%s doesn't budge!", Monnam(mon));
1145.         return;
1146.     }
1147. 
1148.     /* Make sure dx and dy are [-1,0,1] */
1149.     dx = sgn(dx);
1150.     dy = sgn(dy);
1151.     if (!range || (!dx && !dy))
1152.         return; /* paranoia */
1153.     /* don't let grid bugs be hurtled diagonally */
1154.     if (dx && dy && NODIAG(monsndx(mon->data)))
1155.         return;
1156. 
1157.     /* undetected monster can be moved by your strike */
1158.     if (mon->mundetected) {
1159.         mon->mundetected = 0;
1160.         newsym(mon->mx, mon->my);
1161.     }
1162.     if (M_AP_TYPE(mon))
1163.         seemimic(mon);
1164. 
1165.     /* Send the monster along the path */
1166.     mc.x = mon->mx;
1167.     mc.y = mon->my;
1168.     cc.x = mon->mx + (dx * range);
1169.     cc.y = mon->my + (dy * range);
1170.     (void) walk_path(&mc, &cc, mhurtle_step, (genericptr_t) mon);
1171.     if (!DEADMONSTER(mon)) {
1172.         if (t_at(mon->mx, mon->my))
1173.             (void) mintrap(mon, FORCEBUNGLE);
1174.         else
1175.             (void) minliquid(mon);
1176.     }
1177.     return;
1178. }
1179. 
1180. staticfn void
1181. check_shop_obj(struct obj *obj, coordxy x, coordxy y, boolean broken)
1182. {
1183.     boolean costly_xy;
1184.     struct monst *shkp = shop_keeper(*u.ushops);
1185. 
1186.     if (!shkp)
1187.         return;
1188. 
1189.     costly_xy = costly_spot(x, y);
1190.     if (broken || !costly_xy || *in_rooms(x, y, SHOPBASE) != *u.ushops) {
1191.         /* thrown out of a shop or into a different shop */
1192.         if (is_unpaid(obj))
1193.             (void) stolen_value(obj, u.ux, u.uy, (boolean) shkp->mpeaceful,
1194.                                 FALSE);
1195.         if (broken)
1196.             obj->no_charge = 1;
1197.     } else if (costly_xy) {
1198.         char *oshops = in_rooms(x, y, SHOPBASE);
1199. 
1200.         /* ushops0: in case we threw while levitating and recoiled
1201.            out of shop (most likely to the shk's spot in front of door) */
1202.         if (*oshops == *u.ushops || *oshops == *u.ushops0) {
1203.             if (is_unpaid(obj)) {
1204.                 long gtg = Has_contents(obj) ? contained_gold(obj, TRUE) : 0L;
1205. 
1206.                 subfrombill(obj, shkp);
1207.                 if (gtg > 0L)
1208.                     donate_gold(gtg, shkp, TRUE);
1209.             } else if (x != shkp->mx || y != shkp->my) {
1210.                 sellobj(obj, x, y);
1211.             }
1212.         }
1213.     }
1214. }
1215. 
1216. /* Will 'obj' cause damage if it falls on hero's head when thrown upward?
1217.    Not used to handle things which break when they hit.
1218.    Stone missile hitting hero w/ Passes_walls attribute handled separately. */
1219. boolean
1220. harmless_missile(struct obj *obj)
1221. {
1222.     int otyp = obj->otyp;
1223. 
1224.     /* this list is fairly arbitrary */
1225.     switch (otyp) {
1226.     case SLING:
1227.     case EUCALYPTUS_LEAF:
1228.     case KELP_FROND:
1229.     case SPRIG_OF_WOLFSBANE:
1230.     case FORTUNE_COOKIE:
1231.     case PANCAKE:
1232.         return TRUE;
1233.     case RUBBER_HOSE:
1234.     case BAG_OF_TRICKS:
1235.         return (obj->spe < 1);
1236.     case SACK:
1237.     case OILSKIN_SACK:
1238.     case BAG_OF_HOLDING:
1239.         return !Has_contents(obj);
1240.     default:
1241.         if (obj->oclass == SCROLL_CLASS) /* scrolls but not all paper objs */
1242.             return TRUE;
1243.         if (objects[otyp].oc_material == CLOTH)
1244.             return TRUE;
1245.         break;
1246.     }
1247.     return FALSE;
1248. }
1249. 
1250. /*
1251.  * Hero tosses an object upwards with appropriate consequences.
1252.  *
1253.  * Returns FALSE if the object is gone.
1254.  */
1255. staticfn boolean
1256. toss_up(struct obj *obj, boolean hitsroof)
1257. {
1258.     const char *action;
1259.     int otyp = obj->otyp;
1260.     boolean petrifier = ((otyp == EGG || otyp == CORPSE)
1261.                          && ismnum(obj->corpsenm)
1262.                          && touch_petrifies(&mons[obj->corpsenm]));
1263.     /* note: obj->quan == 1 */
1264. 
1265.     if (!has_ceiling(&u.uz)) {
1266.         action = "flies up into"; /* into "the sky" or "the water above" */
1267.     } else if (hitsroof) {
1268.         if (breaktest(obj)) {
1269.             pline("%s hits the %s.", Doname2(obj), ceiling(u.ux, u.uy));
1270.             breakmsg(obj, !Blind);
1271.             /* crackable armor will return True for breaktest() but will
1272.                usually return False for breakobj() */
1273.             if (!breakobj(obj, u.ux, u.uy, TRUE, TRUE)) {
1274.                 hitfloor(obj, FALSE);
1275.                 gt.thrownobj = 0;
1276.                 return TRUE;
1277.             }
1278.             return FALSE;
1279.         }
1280.         action = "hits";
1281.     } else {
1282.         action = "almost hits";
1283.     }
1284.     pline("%s %s the %s, then falls back on top of your %s.", Doname2(obj),
1285.           action, ceiling(u.ux, u.uy), body_part(HEAD));
1286. 
1287.     /* object now hits you */
1288. 
1289.     if (obj->oclass == POTION_CLASS) {
1290.         potionhit(&gy.youmonst, obj, POTHIT_HERO_THROW);
1291.     } else if (breaktest(obj)) {
1292.         int blindinc;
1293. 
1294.         /* need to check for blindness result prior to destroying obj */
1295.         blindinc = ((otyp == CREAM_PIE || otyp == BLINDING_VENOM)
1296.                     /* AT_WEAP is ok here even if attack type was AT_SPIT */
1297.                     && can_blnd(&gy.youmonst, &gy.youmonst, AT_WEAP, obj))
1298.                        ? rnd(25)
1299.                        : 0;
1300.         breakmsg(obj, !Blind);
1301.         if (breakobj(obj, u.ux, u.uy, TRUE, TRUE))
1302.             obj = 0; /* it's now gone */
1303. 
1304.         switch (otyp) {
1305.         case EGG:
1306.             if (petrifier && !Stone_resistance
1307.                 && !(poly_when_stoned(gy.youmonst.data)
1308.                      && polymon(PM_STONE_GOLEM))) {
1309.                 /* egg ends up "all over your face"; perhaps
1310.                    visored helmet should still save you here */
1311.                 if (uarmh)
1312.                     Your("%s fails to protect you.", helm_simple_name(uarmh));
1313.                 goto petrify;
1314.             }
1315.             FALLTHROUGH;
1316.             /*FALLTHRU*/
1317.         case CREAM_PIE:
1318.         case BLINDING_VENOM:
1319.             pline("You've got it all over your %s!", body_part(FACE));
1320.             if (blindinc) {
1321.                 if (otyp == BLINDING_VENOM && !Blind)
1322.                     pline("It blinds you!");
1323.                 u.ucreamed += blindinc;
1324.                 make_blinded(BlindedTimeout + (long) blindinc, FALSE);
1325.                 if (!Blind)
1326.                     Your1(vision_clears);
1327.             }
1328.             break;
1329.         default:
1330.             break;
1331.         }
1332.         if (!obj)
1333.             return FALSE;
1334.         /* 'obj' still exists, so drop it and return True */
1335.         hitfloor(obj, FALSE);
1336.         gt.thrownobj = 0;
1337.     } else if (harmless_missile(obj)) {
1338.         pline("It doesn't hurt.");
1339.         hitfloor(obj, FALSE);
1340.         gt.thrownobj = 0;
1341.     } else { /* neither potion nor other breaking object */
1342.         int material = objects[otyp].oc_material;
1343.         boolean is_silver = (material == SILVER),
1344.                 less_damage = (hard_helmet(uarmh)
1345.                                && (!is_silver || !Hate_silver)),
1346.                 harmless = (stone_missile(obj)
1347.                             && passes_rocks(gy.youmonst.data)),
1348.                 artimsg = FALSE;
1349.         int dmg = dmgval(obj, &gy.youmonst);
1350. 
1351.         if (obj->oartifact && !harmless)
1352.             /* need a fake die roll here; rn1(18,2) avoids 1 and 20 */
1353.             artimsg = artifact_hit((struct monst *) 0, &gy.youmonst, obj,
1354.                                    &dmg, rn1(18, 2));
1355. 
1356.         if (!dmg) { /* probably wasn't a weapon; base damage on weight */
1357.             dmg = ((int) obj->owt + (WT_TO_DMG - 1)) / WT_TO_DMG;
1358.             dmg = (dmg <= 1) ? 1 : rnd(dmg);
1359.             if (dmg > 6)
1360.                 dmg = 6;
1361.             /* since obj is a non-weapon, bonuses for silver and blessed
1362.                haven't been applied (otherwise '!dmg' test will fail when
1363.                they're applicable here); we don't have to worry about
1364.                dmgval()'s artifact light against gremlin or axe against
1365.                woody creature since both involve weapons; hero-as-shade is
1366.                hypothetical because hero can't polymorph into that form */
1367.             if (gy.youmonst.data == &mons[PM_SHADE] && !is_silver)
1368.                 dmg = 0;
1369.             if (obj->blessed && mon_hates_blessings(&gy.youmonst))
1370.                 dmg += rnd(4);
1371.             if (is_silver && Hate_silver)
1372.                 dmg += rnd(20);
1373.         }
1374.         if (dmg > 1 && less_damage)
1375.             dmg = 1;
1376.         if (dmg > 0)
1377.             dmg += u.udaminc;
1378.         if (dmg < 0)
1379.             dmg = 0; /* beware negative rings of increase damage */
1380.         dmg = Maybe_Half_Phys(dmg);
1381. 
1382.         if (uarmh) {
1383.             /* note: 'harmless' and 'petrifier' are mutually exclusive */
1384.             if ((less_damage && dmg < (Upolyd ? u.mh : u.uhp)) || harmless) {
1385.                 if (!artimsg) {
1386.                     if (!harmless) /* !harmless => less_damage here */
1387.                         pline("Fortunately, you are wearing a hard helmet.");
1388.                     else
1389.                         pline("Unfortunately, you are wearing %s.",
1390.                               an(helm_simple_name(uarmh))); /* helm or hat */
1391.                 }
1392. 
1393.             /* helmet definitely protects you when it blocks petrification */
1394.             } else if (!petrifier) {
1395.                 if (flags.verbose)
1396.                     Your("%s does not protect you.", helm_simple_name(uarmh));
1397.             }
1398.             /* stone missile against hero in xorn form would have been
1399.                harmless, but hitting a worn helmet negates that */
1400.             harmless = FALSE;
1401.         } else if (petrifier && !Stone_resistance
1402.                    && !(poly_when_stoned(gy.youmonst.data)
1403.                         && polymon(PM_STONE_GOLEM))) {
1404.  petrify:
1405.             svk.killer.format = KILLED_BY;
1406.             /* what goes up... */
1407.             Strcpy(svk.killer.name, "elementary physics");
1408.             You("turn to stone.");
1409.             if (obj)
1410.                 dropy(obj); /* bypass most of hitfloor() */
1411.             gt.thrownobj = 0;  /* now either gone or on floor */
1412.             done(STONING);
1413.             return obj ? TRUE : FALSE;
1414.         }
1415.         if (is_silver && Hate_silver)
1416.             pline_The("silver sears you!");
1417.         if (harmless)
1418.             hit(thesimpleoname(obj), &gy.youmonst, " but doesn't hurt.");
1419. 
1420.         hitfloor(obj, TRUE);
1421.         gt.thrownobj = 0;
1422.         if (!harmless)
1423.             losehp(dmg, "falling object", KILLED_BY_AN);
1424.     }
1425.     return TRUE;
1426. }
1427. 
1428. /* return true for weapon meant to be thrown; excludes ammo */
1429. boolean
1430. throwing_weapon(struct obj *obj)
1431. {
1432.     return (boolean) (is_missile(obj) || is_spear(obj)
1433.                       /* daggers and knife (excludes scalpel) */
1434.                       || (is_blade(obj) && !is_sword(obj)
1435.                           && (objects[obj->otyp].oc_dir & PIERCE))
1436.                       /* special cases [might want to add AXE] */
1437.                       || obj->otyp == WAR_HAMMER || obj->otyp == AKLYS);
1438. }
1439. 
1440. /* the currently thrown object is returning to you (not for boomerangs) */
1441. staticfn void
1442. sho_obj_return_to_u(struct obj *obj)
1443. {
1444.     /* might already be our location (bounced off a wall) */
1445.     if ((u.dx || u.dy) && (gb.bhitpos.x != u.ux || gb.bhitpos.y != u.uy)) {
1446.         int x = gb.bhitpos.x - u.dx, y = gb.bhitpos.y - u.dy;
1447. 
1448.         tmp_at(DISP_FLASH, obj_to_glyph(obj, rn2_on_display_rng));
1449.         while (isok(x,y) && (x != u.ux || y != u.uy)) {
1450.             tmp_at(x, y);
1451.             nh_delay_output();
1452.             x -= u.dx;
1453.             y -= u.dy;
1454.         }
1455.         tmp_at(DISP_END, 0);
1456.     }
1457. }
1458. 
1459. staticfn void
1460. throwit_return(boolean clear_thrownobj)
1461. {
1462.     iflags.returning_missile = (genericptr_t) 0;
1463.     if (clear_thrownobj)
1464.         gt.thrownobj = (struct obj *) 0;
1465. }
1466. 
1467. staticfn void
1468. swallowit(struct obj *obj)
1469. {
1470.     if (obj != uball) {
1471.         (void) mpickobj(u.ustuck, obj); /* clears 'gt.thrownobj' */
1472.         throwit_return(FALSE);
1473.     } else
1474.         throwit_return(TRUE);
1475. }
1476. 
1477. /* thrown object hits a monster.
1478.    mon may be NULL.
1479.    returns TRUE if shopkeeper caught the object.
1480.    may delete object, clearing gt.thrownobj */
1481. boolean
1482. throwit_mon_hit(struct obj *obj, struct monst *mon)
1483. {
1484.     if (mon) {
1485.         boolean obj_gone;
1486. 
1487.         if (mon->isshk && obj->where == OBJ_MINVENT && obj->ocarry == mon) {
1488.             return TRUE;
1489.         }
1490.         (void) snuff_candle(obj);
1491.         gn.notonhead = (gb.bhitpos.x != mon->mx || gb.bhitpos.y != mon->my);
1492.         obj_gone = thitmonst(mon, obj);
1493.         /* Monster may have been tamed; this frees old mon [obsolete] */
1494.         mon = m_at(gb.bhitpos.x, gb.bhitpos.y);
1495. 
1496.         /* [perhaps this should be moved into thitmonst or hmon] */
1497.         if (mon && mon->isshk
1498.             && (!inside_shop(u.ux, u.uy)
1499.                 || !strchr(in_rooms(mon->mx, mon->my, SHOPBASE), *u.ushops)))
1500.             hot_pursuit(mon);
1501. 
1502.         if (obj_gone)
1503.             gt.thrownobj = (struct obj *) 0;
1504.     }
1505.     return FALSE;
1506. }
1507. 
1508. /* throw an object, NB: obj may be consumed in the process */
1509. void
1510. throwit(
1511.     struct obj *obj,
1512.     long wep_mask,       /* used to re-equip returning boomerang */
1513.     boolean twoweap,     /* used to restore twoweapon mode if
1514.                           * wielded weapon returns */
1515.     struct obj *oldslot) /* for thrown-and-return used with !fixinv */
1516. {
1517.     struct monst *mon;
1518.     int range, urange;
1519.     const struct throw_and_return_weapon *arw = autoreturn_weapon(obj);
1520.     boolean crossbowing,
1521.             impaired = (Confusion || Stunned || Blind
1522.                         || Hallucination || Fumbling),
1523.             tethered_weapon = (arw && arw->tethered && (wep_mask & W_WEP) != 0);
1524. 
1525.     gn.notonhead = FALSE; /* reset potentially stale value */
1526.     if ((obj->cursed || obj->greased) && (u.dx || u.dy) && !rn2(7)) {
1527.         boolean slipok = TRUE;
1528. 
1529.         if (ammo_and_launcher(obj, uwep)) {
1530.             pline("%s!", Tobjnam(obj, "misfire"));
1531.         } else {
1532.             /* only slip if it's greased or meant to be thrown */
1533.             if (obj->greased || throwing_weapon(obj))
1534.                 /* BUG: this message is grammatically incorrect if obj has
1535.                    a plural name; greased gloves or boots for instance. */
1536.                 pline("%s as you throw it!", Tobjnam(obj, "slip"));
1537.             else
1538.                 slipok = FALSE;
1539.         }
1540.         if (slipok) {
1541.             u.dx = rn2(3) - 1;
1542.             u.dy = rn2(3) - 1;
1543.             if (!u.dx && !u.dy)
1544.                 u.dz = 1;
1545.             impaired = TRUE;
1546.         }
1547.     }
1548. 
1549.     if ((u.dx || u.dy || (u.dz < 1))
1550.         && calc_capacity((int) obj->owt) > SLT_ENCUMBER
1551.         && (Upolyd ? (u.mh < 5 && u.mh != u.mhmax)
1552.                    : (u.uhp < 10 && u.uhp != u.uhpmax))
1553.         && obj->owt > (unsigned) ((Upolyd ? u.mh : u.uhp) * 2)
1554.         && !Is_airlevel(&u.uz)) {
1555.         You("have so little stamina, %s drops from your grasp.",
1556.             the(xname(obj)));
1557.         exercise(A_CON, FALSE);
1558.         u.dx = u.dy = 0;
1559.         u.dz = 1;
1560.     }
1561. 
1562.     gt.thrownobj = obj;
1563.     gt.thrownobj->how_lost = LOST_THROWN;
1564.     iflags.returning_missile = AutoReturn(obj, wep_mask) ? (genericptr_t) obj
1565.                                                          : (genericptr_t) 0;
1566.     /* NOTE:  No early returns after this point or returning_missile
1567.        will be left with a stale pointer. */
1568. 
1569.     if (u.uswallow) {
1570.         if (obj == uball) {
1571.             uball->ox = uchain->ox = u.ux;
1572.             uball->oy = uchain->oy = u.uy;
1573.         }
1574.         mon = u.ustuck;
1575.         gb.bhitpos.x = mon->mx;
1576.         gb.bhitpos.y = mon->my;
1577.         if (tethered_weapon)
1578.             tmp_at(DISP_TETHER, obj_to_glyph(obj, rn2_on_display_rng));
1579.     } else if (u.dz) {
1580.         if (u.dz < 0
1581.             /* Mjollnir must we wielded to be thrown--caller verifies this;
1582.                aklys must we wielded as primary to return when thrown */
1583.             && iflags.returning_missile
1584.             && !impaired) {
1585.             pline("%s the %s and returns to your hand!", Tobjnam(obj, "hit"),
1586.                   ceiling(u.ux, u.uy));
1587.             obj = return_throw_to_inv(obj, wep_mask, twoweap, oldslot);
1588.         } else if (u.dz < 0) {
1589.             (void) toss_up(obj, rn2(5) && !Underwater);
1590.         } else if (u.dz > 0 && u.usteed && obj->oclass == POTION_CLASS
1591.                    && rn2(6)) {
1592.             /* alternative to prayer or wand of opening/spell of knock
1593.                for dealing with cursed saddle:  throw holy water > */
1594.             potionhit(u.usteed, obj, POTHIT_HERO_THROW);
1595.         } else {
1596.             hitfloor(obj, TRUE);
1597.         }
1598.         throwit_return(TRUE);
1599.         return;
1600. 
1601.     } else if (obj->otyp == BOOMERANG && !Underwater) {
1602.         if (Is_airlevel(&u.uz) || Levitation)
1603.             hurtle(-u.dx, -u.dy, 1, TRUE);
1604.         mon = boomhit(obj, u.dx, u.dy);
1605.         iflags.returning_missile = 0; /* has returned or isn't going to */
1606.         if (mon == &gy.youmonst) { /* the thing was caught */
1607.             exercise(A_DEX, TRUE);
1608.             obj = return_throw_to_inv(obj, wep_mask, twoweap, oldslot);
1609.             throwit_return(TRUE);
1610.             return;
1611.         }
1612.     } else {
1613.         /* crossbow range is independent of strength */
1614.         crossbowing = (ammo_and_launcher(obj, uwep)
1615.                        && weapon_type(uwep) == P_CROSSBOW);
1616.         urange = (crossbowing ? 18 : (int) ACURRSTR) / 2;
1617.         /* balls are easy to throw or at least roll;
1618.          * also, this insures the maximum range of a ball is greater
1619.          * than 1, so the effects from throwing attached balls are
1620.          * actually possible
1621.          */
1622.         if (obj->otyp == HEAVY_IRON_BALL)
1623.             range = urange - (int) (obj->owt / 100);
1624.         else
1625.             range = urange - (int) (obj->owt / 40);
1626.         if (obj == uball) {
1627.             if (u.ustuck)
1628.                 range = 1;
1629.             else if (range >= 5)
1630.                 range = 5;
1631.         }
1632.         if (range < 1)
1633.             range = 1;
1634. 
1635.         if (is_ammo(obj)) {
1636.             if (ammo_and_launcher(obj, uwep)) {
1637.                 if (crossbowing)
1638.                     range = BOLT_LIM;
1639.                 else
1640.                     range++;
1641.             } else if (obj->oclass != GEM_CLASS) {
1642.                 range /= 2;
1643.                 pline("You aren't wielding %s, so you throw your %s by %s.",
1644.                       an(skill_name(weapon_type(obj))),
1645.                       weapon_descr(obj),
1646.                       body_part(HAND));
1647.             }
1648.         }
1649. 
1650.         if (Is_airlevel(&u.uz) || Levitation) {
1651.             /* action, reaction... */
1652.             urange -= range;
1653.             if (urange < 1)
1654.                 urange = 1;
1655.             range -= urange;
1656.             if (range < 1)
1657.                 range = 1;
1658.         }
1659. 
1660.         if (obj->otyp == BOULDER)
1661.             range = 20; /* you must be giant */
1662.         else if (is_art(obj, ART_MJOLLNIR))
1663.             range = (range + 1) / 2; /* it's heavy */
1664.         else if (tethered_weapon) /* primary weapon is aklys */
1665.             /* range of a tethered_weapon is limited by the
1666.                length of the attached cord [implicit aspect of item] */
1667.             range = min(range, isqrt(arw->range));
1668.         else if (obj == uball && u.utrap && u.utraptype == TT_INFLOOR)
1669.             range = 1;
1670. 
1671.         if (Underwater)
1672.             range = 1;
1673. 
1674.         mon = bhit(u.dx, u.dy, range,
1675.                    tethered_weapon ? THROWN_TETHERED_WEAPON : THROWN_WEAPON,
1676.                    (int (*)(MONST_P, OBJ_P)) 0,
1677.                    (int (*)(OBJ_P, OBJ_P)) 0, &obj);
1678.         gt.thrownobj = obj; /* obj may be null now */
1679. 
1680.         /* have to do this after bhit() so u.ux & u.uy are correct */
1681.         if (Is_airlevel(&u.uz) || Levitation)
1682.             hurtle(-u.dx, -u.dy, urange, TRUE);
1683. 
1684.         if (!obj) {
1685.             /* bhit display cleanup was left with this caller
1686.                for tethered_weapon, but clean it up now since
1687.                we're about to return */
1688.             if (tethered_weapon)
1689.                 tmp_at(DISP_END, 0);
1690.             throwit_return(FALSE);
1691.             return;
1692.         }
1693.     }
1694. 
1695.     if (throwit_mon_hit(obj, mon)) {
1696.         throwit_return(TRUE); /* alert shk caught it */
1697.         return;
1698.     }
1699. 
1700.     if (!gt.thrownobj) {
1701.         /* missile has already been handled */
1702.         if (tethered_weapon)
1703.             tmp_at(DISP_END, 0);
1704.     } else if (u.uswallow && !iflags.returning_missile) {
1705.         swallowit(obj);
1706.         return;
1707.     } else {
1708.         /* Mjollnir must be wielded to be thrown--caller verifies this;
1709.            aklys must be wielded as primary to return when thrown */
1710.         if (iflags.returning_missile) { /* Mjollnir or aklys */
1711.             if (rn2(100)) {
1712.                 if (tethered_weapon)
1713.                     tmp_at(DISP_END, BACKTRACK);
1714.                 else
1715.                     sho_obj_return_to_u(obj); /* display its flight */
1716. 
1717.                 if (!impaired && rn2(100)) {
1718.                     pline("%s to your hand!", Tobjnam(obj, "return"));
1719.                     obj = addinv_before(obj, oldslot);
1720.                     encumber_msg();
1721.                     /* addinv autoquivers an aklys if quiver is empty;
1722.                        if obj is quivered, remove it before wielding */
1723.                     if (obj->owornmask & W_QUIVER)
1724.                         setuqwep((struct obj *) 0);
1725.                     setuwep(obj);
1726.                     set_twoweap(twoweap); /* u.twoweap = twoweap */
1727.                     if (cansee(gb.bhitpos.x, gb.bhitpos.y))
1728.                         newsym(gb.bhitpos.x, gb.bhitpos.y);
1729.                 } else {
1730.                     int dmg = rn2(2);
1731. 
1732.                     if (!dmg) {
1733.                         pline(Blind ? "%s lands %s your %s."
1734.                                     : "%s back to you, landing %s your %s.",
1735.                               Blind ? Something : Tobjnam(obj, "return"),
1736.                               Levitation ? "beneath" : "at",
1737.                               makeplural(body_part(FOOT)));
1738.                     } else {
1739.                         dmg += rnd(3);
1740.                         pline(Blind ? "%s your %s!"
1741.                                     : "%s back toward you, hitting your %s!",
1742.                               Tobjnam(obj, Blind ? "hit" : "fly"),
1743.                               body_part(ARM));
1744.                         if (obj->oartifact)
1745.                             (void) artifact_hit((struct monst *) 0,
1746.                                                 &gy.youmonst, obj, &dmg, 0);
1747.                         losehp(Maybe_Half_Phys(dmg), killer_xname(obj),
1748.                                KILLED_BY);
1749.                     }
1750. 
1751.                     if (u.uswallow) {
1752.                         swallowit(obj);
1753.                         return;
1754.                     }
1755.                     if (!ship_object(obj, u.ux, u.uy, FALSE))
1756.                         dropy(obj);
1757.                 }
1758.                 throwit_return(TRUE);
1759.                 return;
1760.             } else {
1761.                 if (tethered_weapon)
1762.                     tmp_at(DISP_END, 0);
1763.                 /* when this location is stepped on, the weapon will be
1764.                    auto-picked up due to 'obj->how_lost' of LOST_THROWN;
1765.                    addinv() prevents thrown Mjollnir from being placed
1766.                    into the quiver slot, but an aklys will end up there if
1767.                    that slot is empty at the time; since hero will need to
1768.                    explicitly rewield the weapon to get throw-and-return
1769.                    capability back anyway, quivered or not shouldn't matter */
1770.                 pline("%s to return!", Tobjnam(obj, "fail"));
1771. 
1772.                 if (u.uswallow) {
1773.                     swallowit(obj);
1774.                     return;
1775.                 }
1776.                 /* continue below with placing 'obj' at target location */
1777.             }
1778.         }
1779. 
1780.         if ((!IS_SOFT(levl[gb.bhitpos.x][gb.bhitpos.y].typ) && breaktest(obj))
1781.             /* venom [via #monster to spit while poly'd] fails breaktest()
1782.                but we want to force breakage even when location IS_SOFT() */
1783.             || obj->oclass == VENOM_CLASS) {
1784.             tmp_at(DISP_FLASH, obj_to_glyph(obj, rn2_on_display_rng));
1785.             tmp_at(gb.bhitpos.x, gb.bhitpos.y);
1786.             nh_delay_output();
1787.             tmp_at(DISP_END, 0);
1788.             breakmsg(obj, cansee(gb.bhitpos.x, gb.bhitpos.y));
1789.             if (breakobj(obj, gb.bhitpos.x, gb.bhitpos.y, TRUE, TRUE)) {
1790.                 throwit_return(TRUE);
1791.                 return;
1792.             }
1793.         }
1794.         if (!Deaf && !Underwater) {
1795.             /* Some sound effects when item lands in water or lava */
1796.             if (is_pool(gb.bhitpos.x, gb.bhitpos.y)
1797.                 || (is_lava(gb.bhitpos.x, gb.bhitpos.y)
1798.                     && !is_flammable(obj))) {
1799.                 Soundeffect(se_splash, 50);
1800.                 pline((weight(obj) > WT_SPLASH_THRESHOLD)
1801.                       ? "Splash!" : "Plop!");
1802.             }
1803.         }
1804.         if (flooreffects(obj, gb.bhitpos.x, gb.bhitpos.y, "fall")) {
1805.             throwit_return(TRUE);
1806.             return;
1807.         }
1808.         obj_no_longer_held(obj);
1809.         if (mon && mon->isshk && is_pick(obj)) {
1810.             if (cansee(gb.bhitpos.x, gb.bhitpos.y))
1811.                 pline("%s snatches up %s.", Monnam(mon), the(xname(obj)));
1812.             if (*u.ushops || obj->unpaid)
1813.                 check_shop_obj(obj, gb.bhitpos.x, gb.bhitpos.y, FALSE);
1814.             (void) mpickobj(mon, obj); /* may merge and free obj */
1815.             throwit_return(TRUE);
1816.             return;
1817.         }
1818.         (void) snuff_candle(obj);
1819.         if (!mon && ship_object(obj, gb.bhitpos.x, gb.bhitpos.y, FALSE)) {
1820.             throwit_return(TRUE);
1821.             return;
1822.         }
1823.         gt.thrownobj = (struct obj *) 0;
1824.         place_object(obj, gb.bhitpos.x, gb.bhitpos.y);
1825.         /* container contents might break;
1826.            do so before turning ownership of gt.thrownobj over to shk
1827.            (container_impact_dmg handles item already owned by shop) */
1828.         if (!IS_SOFT(levl[gb.bhitpos.x][gb.bhitpos.y].typ)) {
1829.             /* <x,y> is spot where you initiated throw, not gb.bhitpos */
1830.             container_impact_dmg(obj, u.ux, u.uy);
1831.             impact_disturbs_zombies(obj, TRUE);
1832.         }
1833.         /* charge for items thrown out of shop;
1834.            shk takes possession for items thrown into one */
1835.         if ((*u.ushops || obj->unpaid) && obj != uball)
1836.             check_shop_obj(obj, gb.bhitpos.x, gb.bhitpos.y, FALSE);
1837. 
1838.         stackobj(obj);
1839.         if (obj == uball)
1840.             drop_ball(gb.bhitpos.x, gb.bhitpos.y);
1841.         if (cansee(gb.bhitpos.x, gb.bhitpos.y))
1842.             newsym(gb.bhitpos.x, gb.bhitpos.y);
1843.         if (obj_sheds_light(obj))
1844.             gv.vision_full_recalc = 1;
1845.     }
1846. 
1847.     throwit_return(FALSE);
1848.     return;
1849. }
1850. 
1851. /* handle a throw-and-return missile coming back into inventory; makes sure
1852.    that if it was wielded, it will be re-wielded; if it was split off of a
1853.    stack (boomerang), don't let it merge with a different compatible stack */
1854. staticfn struct obj *
1855. return_throw_to_inv(
1856.     struct obj *obj,     /* object to add to invent */
1857.     long wep_mask,       /* its owornmask before it was removed from invent */
1858.     boolean twoweap,     /* True if hero was dual-wielding before removal */
1859.     struct obj *oldslot) /* following item in invent in case of '!fixinv' */
1860. {
1861.     struct obj *otmp = NULL;
1862. 
1863.     /* if 'obj' is from a stack split, we can put it back by undoing split
1864.        so there's no chance of merging with some other compatible stack */
1865.     if (obj->o_id == svc.context.objsplit.parent_oid
1866.         || obj->o_id == svc.context.objsplit.child_oid) {
1867.         obj->nobj = gi.invent;
1868.         gi.invent = obj;
1869.         obj->where = OBJ_INVENT;
1870.         otmp = unsplitobj(obj);
1871.         if (!otmp) {
1872.             gi.invent = obj->nobj;
1873.             obj->nobj = 0;
1874.             obj->where = OBJ_FREE;
1875.         } else {
1876.             obj = otmp;
1877.         }
1878.     }
1879. 
1880.     /* if 'obj' wasn't from a stack split or if it wouldn't merge back
1881.        (maybe new erosion damage?) then it needs to be added to invent;
1882.        don't merge with any other stack even if there is a compatible one
1883.        (others with similar erosion?); can't use addinv_nomerge() here */
1884.     if (!otmp) {
1885.         obj->nomerge = 1; /* redundant unless 'oldslot' somehow went away */
1886.         obj = addinv_before(obj, oldslot);
1887.         obj->nomerge = 0;
1888. 
1889.         /* in case addinv() autoquivered */
1890.         if ((obj->owornmask & W_QUIVER) != 0
1891.             && ((obj->owornmask | wep_mask) & (W_WEP | W_SWAPWEP)) != 0)
1892.             setuqwep((struct obj *) 0);
1893. 
1894.         if ((wep_mask & W_WEP) && !uwep)
1895.             setuwep(obj);
1896.         else if ((wep_mask & W_SWAPWEP) && !uswapwep)
1897.             setuswapwep(obj);
1898.         else if ((wep_mask & W_QUIVER) && !uquiver)
1899.             setuqwep(obj);
1900. 
1901.         /* in case the throw ended dual-wielding, reinstate it after
1902.            successful catch (not needed for boomerang split/unsplit) */
1903.         if (twoweap && !u.twoweap)
1904.             set_twoweap(TRUE); /* u.twoweap = TRUE */
1905.     }
1906. 
1907.     encumber_msg();
1908.     return obj;
1909. }
1910. 
1911. /* an object may hit a monster; various factors adjust chance of hitting */
1912. int
1913. omon_adj(struct monst *mon, struct obj *obj, boolean mon_notices)
1914. {
1915.     int tmp = 0;
1916. 
1917.     /* size of target affects the chance of hitting */
1918.     tmp += (mon->data->msize - MZ_MEDIUM); /* -2..+5 */
1919.     /* sleeping target is more likely to be hit */
1920.     if (mon->msleeping) {
1921.         tmp += 2;
1922.     }
1923.     /* ditto for immobilized target */
1924.     if (!mon->mcanmove || !mon->data->mmove) {
1925.         tmp += 4;
1926.         if (mon_notices && mon->data->mmove && !rn2(10)) {
1927.             mon->mcanmove = 1;
1928.             mon->mfrozen = 0;
1929.         }
1930.     }
1931.     /* some objects are more likely to hit than others */
1932.     switch (obj->otyp) {
1933.     case HEAVY_IRON_BALL:
1934.         if (obj != uball)
1935.             tmp += 2;
1936.         break;
1937.     case BOULDER:
1938.         tmp += 6;
1939.         break;
1940.     default:
1941.         if (obj->oclass == WEAPON_CLASS || is_weptool(obj)
1942.             || obj->oclass == GEM_CLASS)
1943.             tmp += hitval(obj, mon);
1944.         break;
1945.     }
1946.     return tmp;
1947. }
1948. 
1949. /* thrown object misses target monster */
1950. staticfn void
1951. tmiss(struct obj *obj, struct monst *mon, boolean maybe_wakeup)
1952. {
1953.     const char *missile = mshot_xname(obj);
1954. 
1955.     /* If the target can't be seen or doesn't look like a valid target,
1956.        avoid "the arrow misses it," or worse, "the arrows misses the mimic."
1957.        An attentive player will still notice that this is different from
1958.        an arrow just landing short of any target (no message in that case),
1959.        so will realize that there is a valid target here anyway. */
1960.     if (!canseemon(mon) || (M_AP_TYPE(mon) && M_AP_TYPE(mon) != M_AP_MONSTER))
1961.         pline("%s %s.", The(missile), otense(obj, "miss"));
1962.     else
1963.         miss(missile, mon);
1964.     if (maybe_wakeup && !rn2(3))
1965.         wakeup(mon, TRUE);
1966.     return;
1967. }
1968. 
1969. #define special_obj_hits_leader(obj, mon) \
1970.     ((is_quest_artifact(obj) || objects[obj->otyp].oc_unique    \
1971.       || (obj->otyp == FAKE_AMULET_OF_YENDOR && !obj->known))   \
1972.      && mon->m_id == svq.quest_status.leader_m_id)
1973. 
1974. /* whether or not object should be destroyed when it hits its target */
1975. boolean
1976. should_mulch_missile(struct obj *obj)
1977. {
1978.     boolean broken;
1979.     int chance;
1980. 
1981.     /* only ammo (excluding magic stones) or missiles will break */
1982.     if (!obj || !(is_ammo(obj) || is_missile(obj))
1983.         || obj->otyp == BOOMERANG
1984.         || objects[obj->otyp].oc_magic)
1985.         return FALSE;
1986. 
1987.     /* we had been breaking 2/3 of everything unconditionally.  we still don't
1988.        want anything to survive unconditionally, but we need ammo to stay
1989.        around longer on average. */
1990.     chance = 3 + greatest_erosion(obj) - obj->spe;
1991.     broken = chance > 1 ? rn2(chance) : !rn2(4);
1992.     if (obj->blessed && (svc.context.mon_moving ? !rn2(3) : !rnl(4)))
1993.         broken = FALSE;
1994. 
1995.     /* Flint and hard gems don't break easily */
1996.     if (((obj->oclass == GEM_CLASS && objects[obj->otyp].oc_tough)
1997.          || obj->otyp == FLINT)
1998.         && !rn2(2))
1999.         broken = FALSE;
2000. 
2001.     return broken;
2002. }
2003. 
2004. /*
2005.  * Object thrown by player arrives at monster's location.
2006.  * Return 1 if obj has disappeared or otherwise been taken care of,
2007.  * 0 if caller must take care of it.
2008.  * Also used for kicked objects and for polearms/grapnel applied at range.
2009.  */
2010. int
2011. thitmonst(
2012.     struct monst *mon,
2013.     struct obj *obj) /* gt.thrownobj or gk.kickedobj or uwep */
2014. {
2015.     int tmp;     /* Base chance to hit */
2016.     int disttmp; /* distance modifier */
2017.     int otyp = obj->otyp, hmode;
2018.     boolean guaranteed_hit = engulfing_u(mon);
2019.     int dieroll;
2020. 
2021.     hmode = (obj == uwep) ? HMON_APPLIED
2022.               : (obj == gk.kickedobj) ? HMON_KICKED
2023.                 : HMON_THROWN;
2024. 
2025.     /* Differences from melee weapons:
2026.      *
2027.      * Dex still gives a bonus, but strength does not.
2028.      * Polymorphed players lacking attacks may still throw.
2029.      * There's a base -1 to hit.
2030.      * No bonuses for fleeing or stunned targets (they don't dodge
2031.      *    melee blows as readily, but dodging arrows is hard anyway).
2032.      * Not affected by traps, etc.
2033.      * Certain items which don't in themselves do damage ignore 'tmp'.
2034.      * Distance and monster size affect chance to hit.
2035.      */
2036.     tmp = -1 + Luck + find_mac(mon) + u.uhitinc
2037.           + maybe_polyd(gy.youmonst.data->mlevel, u.ulevel);
2038.     if (ACURR(A_DEX) < 4)
2039.         tmp -= 3;
2040.     else if (ACURR(A_DEX) < 6)
2041.         tmp -= 2;
2042.     else if (ACURR(A_DEX) < 8)
2043.         tmp -= 1;
2044.     else if (ACURR(A_DEX) >= 14)
2045.         tmp += (ACURR(A_DEX) - 14);
2046. 
2047.     /* Modify to-hit depending on distance; but keep it sane.
2048.      * Polearms get a distance penalty even when wielded; it's
2049.      * hard to hit at a distance.
2050.      */
2051.     disttmp = 3 - distmin(u.ux, u.uy, mon->mx, mon->my);
2052.     if (disttmp < -4)
2053.         disttmp = -4;
2054.     tmp += disttmp;
2055. 
2056.     /* gloves are a hindrance to proper use of bows */
2057.     if (uarmg && uwep && objects[uwep->otyp].oc_skill == P_BOW) {
2058.         switch (uarmg->otyp) {
2059.         case GAUNTLETS_OF_POWER: /* metal */
2060.             tmp -= 2;
2061.             break;
2062.         case GAUNTLETS_OF_FUMBLING:
2063.             tmp -= 3;
2064.             break;
2065.         case LEATHER_GLOVES:
2066.         case GAUNTLETS_OF_DEXTERITY:
2067.             break;
2068.         default:
2069.             impossible("Unknown type of gloves (%d)", uarmg->otyp);
2070.             break;
2071.         }
2072.     }
2073. 
2074.     tmp += omon_adj(mon, obj, TRUE);
2075.     if (is_orc(mon->data)
2076.         && maybe_polyd(is_elf(gy.youmonst.data), Race_if(PM_ELF)))
2077.         tmp++;
2078.     if (guaranteed_hit) {
2079.         tmp += 1000; /* Guaranteed hit */
2080.     }
2081. 
2082.     /* throwing real gems to co-aligned unicorns boosts Luck,
2083.        to cross-aligned unicorns changes Luck by random amount;
2084.        throwing worthless glass doesn't affect Luck but doesn't anger them;
2085.        5.0: treat rocks and gray stones as attacks rather than like glass
2086.        and also treat gems or glass shot via sling as attacks */
2087.     if (obj->oclass == GEM_CLASS && is_unicorn(mon->data)
2088.         && objects[obj->otyp].oc_material != MINERAL && !uslinging()) {
2089.         if (helpless(mon)) {
2090.             tmiss(obj, mon, FALSE);
2091.             return 0;
2092.         } else if (mon->mtame) {
2093.             pline("%s catches and drops %s.", Monnam(mon), the(xname(obj)));
2094.             return 0;
2095.         } else {
2096.             pline("%s catches %s.", Monnam(mon), the(xname(obj)));
2097.             return gem_accept(mon, obj);
2098.         }
2099.     }
2100. 
2101.     /* don't make game unwinnable if naive player throws artifact
2102.        at leader... (kicked artifact is ok too; HMON_APPLIED could
2103.        occur if quest artifact polearm or grapnel ever gets added) */
2104.     if (hmode != HMON_APPLIED && special_obj_hits_leader(obj, mon)) {
2105.         /* AIS: changes to wakeup() means that it's now less inappropriate
2106.            here than it used to be, but manual version works just as well */
2107.         mon->msleeping = 0;
2108.         mon->mstrategy &= ~STRAT_WAITMASK;
2109. 
2110.         if (mon->mcanmove) {
2111.             pline("%s catches %s.", Some_Monnam(mon), the(xname(obj)));
2112.             /* leader will keep tossed invocation item after you've done the
2113.                invocation and it's become unnecessary for completion.. */
2114.             if ((u.uevent.invoked && objects[obj->otyp].oc_unique
2115.                  && obj->otyp != AMULET_OF_YENDOR)
2116.                 /* ...or any special item, if you've made him angry */
2117.                 || !mon->mpeaceful) {
2118.                 /* give an explanation for keeping the item only if leader is
2119.                    not doing it out of anger */
2120.                 if (mon->mpeaceful && !Deaf) {
2121.                     /* just in case, identify the object so its name will
2122.                        appear in the message */
2123.                     fully_identify_obj(obj);
2124.                     verbalize("%s part in this is finished.",
2125.                               s_suffix(The(xname(obj))));
2126.                     verbalize(
2127.                "We will guard it in case it is ever needed again, %s forbid.",
2128.                               align_gname(u.ualignbase[A_ORIGINAL]));
2129.                 }
2130.                 if (*u.ushops || obj->unpaid) /* not very likely... */
2131.                     check_shop_obj(obj, mon->mx, mon->my, FALSE);
2132.                 (void) mpickobj(mon, obj);
2133.             } else {
2134.                 /* under normal circumstances, leader will say something and
2135.                    then return the item to the hero */
2136.                 boolean next2u = monnear(mon, u.ux, u.uy);
2137. 
2138.                 finish_quest(obj); /* acknowledge quest completion */
2139.                 pline("%s %s %s back to you.", Some_Monnam(mon),
2140.                       (next2u ? "hands" : "tosses"), the(xname(obj)));
2141.                 if (!next2u)
2142.                     sho_obj_return_to_u(obj);
2143.                 obj = addinv(obj); /* back into your inventory */
2144.                 nhUse(obj);
2145.                 encumber_msg();
2146.             }
2147.             return 1; /* caller doesn't need to place it */
2148.         }
2149.         return 0;
2150.     }
2151. 
2152.     dieroll = rnd(20);
2153. 
2154.     if (obj->oclass == WEAPON_CLASS || is_weptool(obj)
2155.         || obj->oclass == GEM_CLASS) {
2156.         if (hmode == HMON_KICKED) {
2157.             /* throwing adjustments and weapon skill bonus don't apply */
2158.             tmp -= (is_ammo(obj) ? 5 : 3);
2159.         } else if (is_ammo(obj)) {
2160.             if (!ammo_and_launcher(obj, uwep)) {
2161.                 tmp -= 4;
2162.             } else {
2163.                 tmp += uwep->spe - greatest_erosion(uwep);
2164.                 tmp += weapon_hit_bonus(uwep);
2165.                 if (uwep->oartifact)
2166.                     tmp += spec_abon(uwep, mon);
2167.                 /*
2168.                  * Elves and Samurais are highly trained w/bows,
2169.                  * especially their own special types of bow.
2170.                  * Polymorphing won't make you a bow expert.
2171.                  */
2172.                 if ((Race_if(PM_ELF) || Role_if(PM_SAMURAI))
2173.                     && (!Upolyd || your_race(gy.youmonst.data))
2174.                     && objects[uwep->otyp].oc_skill == P_BOW) {
2175.                     ++tmp;
2176.                     if ((Race_if(PM_ELF) && uwep->otyp == ELVEN_BOW)
2177.                         || (Role_if(PM_SAMURAI) && uwep->otyp == YUMI))
2178.                         ++tmp;
2179.                 }
2180.             }
2181.         } else { /* thrown non-ammo or applied polearm/grapnel */
2182.             if (otyp == BOOMERANG) /* arbitrary */
2183.                 tmp += 4;
2184.             else if (throwing_weapon(obj)) /* meant to be thrown */
2185.                 tmp += 2;
2186.             else if (obj == gt.thrownobj) /* not meant to be thrown */
2187.                 tmp -= 2;
2188.             /* we know we're dealing with a weapon or weptool handled
2189.                by WEAPON_SKILLS once ammo objects have been excluded */
2190.             tmp += weapon_hit_bonus(obj);
2191.         }
2192. 
2193.         if (tmp >= dieroll) {
2194.             boolean wasthrown = (gt.thrownobj != 0),
2195.                     /* remember weapon attribute; hmon() might destroy obj */
2196.                     chopper = is_axe(obj);
2197. 
2198.             /* attack hits mon */
2199.             if (hmode == HMON_APPLIED) { /* ranged hit with wielded polearm */
2200.                 /* hmon()'s caller is expected to do this; however, hmon()
2201.                    delivers the "hit with wielded weapon for first time"
2202.                    gamelog message when applicable */
2203.                 u.uconduct.weaphit++;
2204.             }
2205.             if (hmon(mon, obj, hmode, dieroll)) { /* mon still alive */
2206.                 if (mon->wormno)
2207.                     cutworm(mon, gb.bhitpos.x, gb.bhitpos.y, chopper);
2208.             }
2209.             exercise(A_DEX, TRUE);
2210.             /* if hero was swallowed and projectile killed the engulfer,
2211.                'obj' got added to engulfer's inventory and then dropped,
2212.                so we can't safely use that pointer anymore; it escapes
2213.                the chance to be used up here... */
2214.             if (wasthrown && !gt.thrownobj)
2215.                 return 1;
2216. 
2217.             /* projectiles other than magic stones sometimes disappear
2218.                when thrown; projectiles aren't among the types of weapon
2219.                that hmon() might have destroyed so obj is intact */
2220.             if (should_mulch_missile(obj)) {
2221.                 if (*u.ushops || obj->unpaid)
2222.                     check_shop_obj(obj, gb.bhitpos.x, gb.bhitpos.y, TRUE);
2223.                 obfree(obj, (struct obj *) 0);
2224.                 return 1;
2225.             }
2226.             passive_obj(mon, obj, (struct attack *) 0);
2227.         } else {
2228.             tmiss(obj, mon, TRUE);
2229.             if (hmode == HMON_APPLIED)
2230.                 wakeup(mon, TRUE);
2231.         }
2232. 
2233.     } else if (otyp == HEAVY_IRON_BALL) {
2234.         exercise(A_STR, TRUE);
2235.         if (tmp >= dieroll) {
2236.             int was_swallowed = guaranteed_hit;
2237. 
2238.             exercise(A_DEX, TRUE);
2239.             if (!hmon(mon, obj, hmode, dieroll)) { /* mon killed */
2240.                 if (was_swallowed && !u.uswallow && obj == uball)
2241.                     return 1; /* already did placebc() */
2242.             }
2243.         } else {
2244.             tmiss(obj, mon, TRUE);
2245.         }
2246. 
2247.     } else if (otyp == BOULDER) {
2248.         exercise(A_STR, TRUE);
2249.         if (tmp >= dieroll) {
2250.             exercise(A_DEX, TRUE);
2251.             (void) hmon(mon, obj, hmode, dieroll);
2252.         } else {
2253.             tmiss(obj, mon, TRUE);
2254.         }
2255. 
2256.     } else if ((otyp == EGG || otyp == CREAM_PIE || otyp == BLINDING_VENOM
2257.                 || otyp == ACID_VENOM)
2258.                && (guaranteed_hit || ACURR(A_DEX) > rnd(25))) {
2259.         (void) hmon(mon, obj, hmode, dieroll);
2260.         return 1; /* hmon used it up */
2261. 
2262.     } else if (obj->oclass == POTION_CLASS
2263.                && (guaranteed_hit || ACURR(A_DEX) > rnd(25))) {
2264.         potionhit(mon, obj, POTHIT_HERO_THROW);
2265.         return 1;
2266. 
2267.     } else if (befriend_with_obj(mon->data, obj)
2268.                || (mon->mtame && dogfood(mon, obj) <= ACCFOOD)) {
2269.         if (tamedog(mon, obj, TRUE)) {
2270.             return 1; /* obj is gone */
2271.         } else {
2272.             tmiss(obj, mon, FALSE);
2273.             mon->msleeping = 0;
2274.             mon->mstrategy &= ~STRAT_WAITMASK;
2275.         }
2276.     } else if (guaranteed_hit) {
2277.         char trail[BUFSZ];
2278.         char *monname;
2279.         struct permonst *md = u.ustuck->data;
2280. 
2281.         /* this assumes that guaranteed_hit is due to swallowing */
2282.         wakeup(mon, TRUE);
2283.         if (obj->otyp == CORPSE && touch_petrifies(&mons[obj->corpsenm])) {
2284.             if (is_animal(md)) {
2285.                 minstapetrify(u.ustuck, TRUE);
2286.                 /* Don't leave a cockatrice corpse available in a statue */
2287.                 if (!u.uswallow) {
2288.                     delobj(obj);
2289.                     return 1;
2290.                 }
2291.             }
2292.         }
2293.         Strcpy(trail,
2294.                digests(md) ? " entrails" : is_whirly(md) ? " currents" : "");
2295.         monname = mon_nam(mon);
2296.         if (*trail)
2297.             monname = s_suffix(monname);
2298.         pline("%s into %s%s.", Tobjnam(obj, "vanish"), monname, trail);
2299.     } else {
2300.         tmiss(obj, mon, TRUE);
2301.     }
2302. 
2303.     return 0;
2304. }
2305. 
2306. #undef special_obj_hits_leader
2307. 
2308. staticfn int
2309. gem_accept(struct monst *mon, struct obj *obj)
2310. {
2311.     static NEARDATA const char
2312.         nogood[]     = " is not interested in your junk.",
2313.         acceptgift[] = " accepts your gift.",
2314.         maybeluck[]  = " hesitatingly",
2315.         noluck[]     = " graciously",
2316.         addluck[]    = " gratefully";
2317.     char buf[BUFSZ];
2318.     boolean is_buddy = sgn(mon->data->maligntyp) == sgn(u.ualign.type);
2319.     boolean is_gem = objects[obj->otyp].oc_material == GEMSTONE;
2320.     int ret = 0;
2321. 
2322.     Strcpy(buf, Monnam(mon));
2323.     mon->mpeaceful = 1;
2324.     mon->mavenge = 0;
2325. 
2326.     /* object properly identified */
2327.     if (obj->dknown && objects[obj->otyp].oc_name_known) {
2328.         if (is_gem) {
2329.             if (is_buddy) {
2330.                 Strcat(buf, addluck);
2331.                 change_luck(5);
2332.             } else {
2333.                 Strcat(buf, maybeluck);
2334.                 change_luck(rn2(7) - 3);
2335.             }
2336.         } else {
2337.             Strcat(buf, nogood);
2338.             goto nopick;
2339.         }
2340. 
2341.     /* making guesses */
2342.     } else if (has_oname(obj) || objects[obj->otyp].oc_uname) {
2343.         if (is_gem) {
2344.             if (is_buddy) {
2345.                 Strcat(buf, addluck);
2346.                 change_luck(2);
2347.             } else {
2348.                 Strcat(buf, maybeluck);
2349.                 change_luck(rn2(3) - 1);
2350.             }
2351.         } else {
2352.             Strcat(buf, nogood);
2353.             goto nopick;
2354.         }
2355. 
2356.     /* value completely unknown to @ */
2357.     } else {
2358.         if (is_gem) {
2359.             if (is_buddy) {
2360.                 Strcat(buf, addluck);
2361.                 change_luck(1);
2362.             } else {
2363.                 Strcat(buf, maybeluck);
2364.                 change_luck(rn2(3) - 1);
2365.             }
2366.         } else {
2367.             Strcat(buf, noluck);
2368.         }
2369.     }
2370.     Strcat(buf, acceptgift);
2371.     if (*u.ushops || obj->unpaid)
2372.         check_shop_obj(obj, mon->mx, mon->my, TRUE);
2373.     (void) mpickobj(mon, obj); /* may merge and free obj */
2374.     ret = 1;
2375. 
2376.  nopick:
2377.     if (!Blind)
2378.         pline1(buf);
2379.     if (!tele_restrict(mon))
2380.         (void) rloc(mon, RLOC_MSG);
2381.     return ret;
2382. }
2383. 
2384. /*
2385.  * Comments about the restructuring of the old breaks() routine.
2386.  *
2387.  * There are now three distinct phases to object breaking:
2388.  *     breaktest() - which makes the check/decision about whether the
2389.  *                   object is going to break.
2390.  *     breakmsg()  - which outputs a message about the breakage,
2391.  *                   appropriate for that particular object. Should
2392.  *                   only be called after a positive breaktest().
2393.  *                   on the object and, if it going to be called,
2394.  *                   it must be called before calling breakobj().
2395.  *                   Calling breakmsg() is optional.
2396.  *     breakobj()  - which actually does the breakage and the side-effects
2397.  *                   of breaking that particular object. This should
2398.  *                   only be called after a positive breaktest() on the
2399.  *                   object.
2400.  *
2401.  * Each of the above routines is currently static to this source module.
2402.  * There are two routines callable from outside this source module which
2403.  * perform the routines above in the correct sequence.
2404.  *
2405.  *   hero_breaks() - called when an object is to be broken as a result
2406.  *                   of something that the hero has done. (throwing it,
2407.  *                   kicking it, etc.)
2408.  *   breaks()      - called when an object is to be broken for some
2409.  *                   reason other than the hero doing something to it.
2410.  */
2411. 
2412. /*
2413.  * The hero causes breakage of an object (throwing, dropping it, etc.)
2414.  * Return 0 if the object didn't break, 1 if the object broke.
2415.  */
2416. int
2417. hero_breaks(
2418.     struct obj *obj,
2419.     coordxy x, coordxy y, /* object location (ox, oy may not be right) */
2420.     unsigned breakflags)
2421. {
2422.     /* from_invent: thrown or dropped by player; maybe on shop bill;
2423.        by-hero is implicit so callers don't need to specify BRK_BY_HERO */
2424.     boolean from_invent = (breakflags & BRK_FROM_INV) != 0,
2425.             in_view = Blind ? FALSE : (from_invent || cansee(x, y));
2426.     unsigned brk = (breakflags & BRK_KNOWN_OUTCOME);
2427. 
2428.     /* only call breaktest if caller hasn't already specified the outcome */
2429.     if (!brk)
2430.         brk = breaktest(obj) ? BRK_KNOWN2BREAK : BRK_KNOWN2NOTBREAK;
2431.     if (brk == BRK_KNOWN2NOTBREAK)
2432.         return 0;
2433. 
2434.     breakmsg(obj, in_view);
2435.     return breakobj(obj, x, y, TRUE, from_invent);
2436. }
2437. 
2438. /*
2439.  * The object is going to break for a reason other than the hero doing
2440.  * something to it.
2441.  * Return 0 if the object doesn't break, 1 if the object broke.
2442.  */
2443. int
2444. breaks(
2445.     struct obj *obj,
2446.     coordxy x, coordxy y) /* object location (ox, oy may not be right) */
2447. {
2448.     boolean in_view = Blind ? FALSE : cansee(x, y);
2449. 
2450.     if (!breaktest(obj))
2451.         return 0;
2452.     breakmsg(obj, in_view);
2453.     return breakobj(obj, x, y, FALSE, FALSE);
2454. }
2455. 
2456. void
2457. release_camera_demon(struct obj *obj, coordxy x, coordxy y)
2458. {
2459.     struct monst *mtmp;
2460.     if (!rn2(3)
2461.         && (mtmp = makemon(&mons[rn2(3) ? PM_HOMUNCULUS : PM_IMP], x, y,
2462.                            MM_NOMSG)) != 0) {
2463.         if (canspotmon(mtmp))
2464.             pline("%s is released!", Hallucination
2465.                                          ? An(rndmonnam(NULL))
2466.                                          : "The picture-painting demon");
2467.         mtmp->mpeaceful = !obj->cursed;
2468.         set_malign(mtmp);
2469.     }
2470. }
2471. 
2472. /*
2473.  * Break an object.  Breakable armor goes through erosion steps; other
2474.  * items break unconditionally.  Assumes all resistance checks
2475.  * and break messages have been delivered prior to getting here.
2476.  * (No longer true; breakmsg() is silent for crackable armor and we
2477.  * call erode_obj() for it and that delivers a damaged-the-item message.)
2478.  */
2479. int
2480. breakobj(
2481.     struct obj *obj,
2482.     coordxy x, coordxy y,   /* object location (ox, oy may not be right) */
2483.     boolean hero_caused,    /* is this the hero's fault? */
2484.     boolean from_invent)
2485. {
2486.     boolean fracture = FALSE;
2487.     boolean explosion = FALSE;
2488. 
2489.     if (is_crackable(obj)) /* if erodeproof, erode_obj() will say so */
2490.         return (erode_obj(obj, armor_simple_name(obj), ERODE_CRACK,
2491.                           EF_DESTROY | EF_VERBOSE) == ER_DESTROYED);
2492. 
2493.     switch (obj->oclass == POTION_CLASS ? POT_WATER : obj->otyp) {
2494.     case MIRROR:
2495.         if (hero_caused)
2496.             change_luck(-2);
2497.         break;
2498.     case POT_WATER:      /* really, all potions */
2499.         obj->in_use = 1; /* in case it's fatal */
2500.         if (obj->otyp == POT_OIL && obj->lamplit) {
2501.             explode_oil(obj, x, y);
2502.         } else if (next2u(x, y)) {
2503.             if (!breathless(gy.youmonst.data) || haseyes(gy.youmonst.data)) {
2504.                 /* wet towel protects both eyes and breathing */
2505.                 if (obj->otyp != POT_WATER && !Half_gas_damage) {
2506.                     if (!breathless(gy.youmonst.data)) {
2507.                         /* [what about "familiar odor" when known?] */
2508.                         You("smell a peculiar odor...");
2509.                     } else {
2510.                         const char *eyes = body_part(EYE);
2511. 
2512.                         if (eyecount(gy.youmonst.data) != 1)
2513.                             eyes = makeplural(eyes);
2514.                         Your("%s %s.", eyes, vtense(eyes, "water"));
2515.                     }
2516.                 }
2517.                 potionbreathe(obj);
2518.             }
2519.         }
2520.         /* monster breathing isn't handled... [yet?] */
2521.         break;
2522.     case EXPENSIVE_CAMERA:
2523.         release_camera_demon(obj, x, y);
2524.         break;
2525.     case EGG:
2526.         /* breaking your own eggs is bad luck */
2527.         if (hero_caused && obj->spe && ismnum(obj->corpsenm))
2528.             change_luck((schar) -min(obj->quan, 5L));
2529.         if (obj->corpsenm == PM_PYROLISK)
2530.             explosion = TRUE;
2531.         break;
2532.     case BOULDER:
2533.     case STATUE:
2534.         /* caller will handle object disposition;
2535.            we're just doing the shop theft handling */
2536.         fracture = TRUE;
2537.         break;
2538.     default:
2539.         break;
2540.     }
2541. 
2542.     if (hero_caused) {
2543.         if (from_invent || obj->unpaid) {
2544.             if (*u.ushops || obj->unpaid)
2545.                 check_shop_obj(obj, x, y, TRUE);
2546.         } else if (!obj->no_charge && costly_spot(x, y)) {
2547.             /* it is assumed that the obj is a floor-object */
2548.             char *o_shop = in_rooms(x, y, SHOPBASE);
2549.             struct monst *shkp = shop_keeper(*o_shop);
2550. 
2551.             if (shkp) { /* (implies *o_shop != '\0') */
2552.                 struct eshk *eshkp = ESHK(shkp);
2553. 
2554.                 /* base shk actions on her peacefulness at start of
2555.                    this turn, so that "simultaneous" multiple breakage
2556.                    isn't drastically worse than single breakage */
2557.                 if (gh.hero_seq != eshkp->break_seq)
2558.                     eshkp->seq_peaceful = shkp->mpeaceful;
2559.                 if ((stolen_value(obj, x, y, eshkp->seq_peaceful, FALSE) > 0L)
2560.                     && (*o_shop != u.ushops[0] || !inside_shop(u.ux, u.uy))
2561.                     && gh.hero_seq != eshkp->break_seq)
2562.                     make_angry_shk(shkp, x, y);
2563.                 /* make_angry_shk() is only called on the first instance
2564.                    of breakage during any particular hero move */
2565.                 eshkp->break_seq = gh.hero_seq;
2566.             }
2567.         }
2568.     }
2569.     if (!fracture)
2570.         delobj(obj);
2571.     if (explosion)
2572.         explode(x, y, -11, d(3, 6), 0, EXPL_FIERY);
2573.     return 1;
2574. }
2575. 
2576. /*
2577.  * Check to see if obj (which has just hit hard something at speed, e.g.
2578.  * thrown or dropped from height) is going to break, but don't actually
2579.  * break it. Return 0 if the object isn't going to break, 1 if it is.
2580.  */
2581. boolean
2582. breaktest(struct obj *obj)
2583. {
2584.     int nonbreakchance = 1; /* chance for non-artifacts to resist */
2585. 
2586.     /* this may need to be changed if actual glass armor gets added someday;
2587.        for now, it affects crystal plate mail and helm of brilliance;
2588.        either of them will have to be cracked 4 times before breaking */
2589.     if (obj->oclass == ARMOR_CLASS && objects[obj->otyp].oc_material == GLASS)
2590.         nonbreakchance = 90;
2591. 
2592.     if (obj_resists(obj, nonbreakchance, 99))
2593.         return FALSE;
2594.     if (objects[obj->otyp].oc_material == GLASS && !obj->oartifact
2595.         && obj->oclass != GEM_CLASS)
2596.         return TRUE;
2597.     switch (obj->oclass == POTION_CLASS ? POT_WATER : obj->otyp) {
2598.     case EXPENSIVE_CAMERA:
2599.     case POT_WATER: /* really, all potions */
2600.     case EGG:
2601.     case CREAM_PIE:
2602.     case MELON:
2603.     case ACID_VENOM:
2604.     case BLINDING_VENOM:
2605.         return TRUE;
2606.     default:
2607.         return FALSE;
2608.     }
2609. }
2610. 
2611. staticfn void
2612. breakmsg(struct obj *obj, boolean in_view)
2613. {
2614.     const char *to_pieces;
2615. 
2616.     if (is_crackable(obj)) /* breakobj() will call erode_obj() for message */
2617.         return;
2618. 
2619.     to_pieces = "";
2620.     switch (obj->oclass == POTION_CLASS ? POT_WATER : obj->otyp) {
2621.     default: /* glass or crystal wand */
2622.         if (obj->oclass != WAND_CLASS)
2623.             impossible("breaking odd object (%d)?", obj->otyp);
2624.         FALLTHROUGH;
2625.         /*FALLTHRU*/
2626.     case LENSES:
2627.     case MIRROR:
2628.     case CRYSTAL_BALL:
2629.     case EXPENSIVE_CAMERA:
2630.         to_pieces = " into a thousand pieces";
2631.         FALLTHROUGH;
2632.     /*FALLTHRU*/
2633.     case POT_WATER: /* really, all potions */
2634.         if (!in_view)
2635.             You_hear("%s shatter!", something);
2636.         else
2637.             pline("%s shatter%s%s!", Doname2(obj),
2638.                   (obj->quan == 1L) ? "s" : "", to_pieces);
2639.         break;
2640.     case EGG:
2641.     case MELON:
2642.         pline("Splat!");
2643.         break;
2644.     case CREAM_PIE:
2645.         if (in_view)
2646.             pline("What a mess!");
2647.         break;
2648.     case ACID_VENOM:
2649.     case BLINDING_VENOM:
2650.         pline("Splash!");
2651.         break;
2652.     }
2653. }
2654. 
2655. staticfn int
2656. throw_gold(struct obj *obj)
2657. {
2658.     int range, odx, ody;
2659.     struct monst *mon;
2660. 
2661.     if (!u.dx && !u.dy && !u.dz) {
2662.         You("cannot throw gold at yourself.");
2663.         /* If we tried to throw part of a stack, force it to merge back
2664.            together (same as in throw_obj).  Essential for gold. */
2665.         if (obj->o_id == svc.context.objsplit.parent_oid
2666.             || obj->o_id == svc.context.objsplit.child_oid)
2667.             (void) unsplitobj(obj);
2668.         return ECMD_CANCEL;
2669.     }
2670.     freeinv(obj);
2671.     if (u.uswallow) {
2672.         const char *swallower = mon_nam(u.ustuck);
2673. 
2674.         if (digests(u.ustuck->data))
2675.             /* note: s_suffix() returns a modifiable buffer */
2676.             swallower = strcat(s_suffix(swallower), " entrails");
2677.         pline_The("gold disappears into %s.", swallower);
2678.         add_to_minv(u.ustuck, obj);
2679.         return ECMD_TIME;
2680.     }
2681. 
2682.     if (u.dz) {
2683.         if (u.dz < 0 && !Is_airlevel(&u.uz) && !Underwater
2684.             && !Is_waterlevel(&u.uz)) {
2685.             pline_The("gold hits the %s, then falls back on top of your %s.",
2686.                       ceiling(u.ux, u.uy), body_part(HEAD));
2687.             /* some self damage? */
2688.             if (uarmh)
2689.                 pline("Fortunately, you are wearing %s!",
2690.                       an(helm_simple_name(uarmh)));
2691.         }
2692.         gb.bhitpos.x = u.ux;
2693.         gb.bhitpos.y = u.uy;
2694.     } else {
2695.         /* consistent with range for normal objects */
2696.         range = (int) ((ACURRSTR) / 2 - obj->owt / 40);
2697. 
2698.         /* see if the gold has a place to move into */
2699.         odx = u.ux + u.dx;
2700.         ody = u.uy + u.dy;
2701.         if (!isok(odx, ody)
2702.             || !ZAP_POS(levl[odx][ody].typ) || closed_door(odx, ody)) {
2703.             gb.bhitpos.x = u.ux;
2704.             gb.bhitpos.y = u.uy;
2705.         } else {
2706.             mon = bhit(u.dx, u.dy, range, THROWN_WEAPON,
2707.                        (int (*)(MONST_P, OBJ_P)) 0,
2708.                        (int (*)(OBJ_P, OBJ_P)) 0, &obj);
2709.             if (!obj)
2710.                 return ECMD_TIME; /* object is gone */
2711.             if (mon) {
2712.                 if (ghitm(mon, obj)) /* was it caught? */
2713.                     return ECMD_TIME;
2714.             } else {
2715.                 if (ship_object(obj, gb.bhitpos.x, gb.bhitpos.y, FALSE))
2716.                     return ECMD_TIME;
2717.             }
2718.         }
2719.     }
2720. 
2721.     if (flooreffects(obj, gb.bhitpos.x, gb.bhitpos.y, "fall"))
2722.         return ECMD_TIME;
2723.     if (u.dz > 0)
2724.         pline_The("gold hits the %s.", surface(gb.bhitpos.x, gb.bhitpos.y));
2725.     place_object(obj, gb.bhitpos.x, gb.bhitpos.y);
2726.     if (*u.ushops)
2727.         sellobj(obj, gb.bhitpos.x, gb.bhitpos.y);
2728.     stackobj(obj);
2729.     newsym(gb.bhitpos.x, gb.bhitpos.y);
2730.     return ECMD_TIME;
2731. }
2732. 
2733. #undef AutoReturn
2734. 
2735. /*dothrow.c*/