Source:NetHack 3.4.0/dothrow.c

From NetHackWiki
(Redirected from NetHack 3.4.0/dothrow.c)
Jump to navigation Jump to search

Below is the full text to dothrow.c from the source code of NetHack 3.4.0. To link to a particular line, write [[NetHack 3.4.0/dothrow.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

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.    /*	SCCS Id: @(#)dothrow.c	3.4	2002/02/21	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    /* Contains code for 't' (throw) */
6.    
7.    #include "hack.h"
8.    
9.    STATIC_DCL int FDECL(throw_obj, (struct obj *,int));
10.   STATIC_DCL void NDECL(autoquiver);
11.   STATIC_DCL int FDECL(gem_accept, (struct monst *, struct obj *));
12.   STATIC_DCL void FDECL(tmiss, (struct obj *, struct monst *));
13.   STATIC_DCL int FDECL(throw_gold, (struct obj *));
14.   STATIC_DCL void FDECL(check_shop_obj, (struct obj *,XCHAR_P,XCHAR_P,BOOLEAN_P));
15.   STATIC_DCL void FDECL(breakobj, (struct obj *,XCHAR_P,XCHAR_P,BOOLEAN_P,BOOLEAN_P));
16.   STATIC_DCL void FDECL(breakmsg, (struct obj *,BOOLEAN_P));
17.   STATIC_DCL boolean FDECL(toss_up,(struct obj *, BOOLEAN_P));
18.   STATIC_DCL boolean FDECL(throwing_weapon, (struct obj *));
19.   STATIC_DCL void FDECL(sho_obj_return_to_u, (struct obj *obj));
20.   STATIC_DCL boolean FDECL(mhurtle_step, (genericptr_t,int,int));
21.   
22.   
23.   static NEARDATA const char toss_objs[] =
24.   	{ ALLOW_COUNT, GOLD_CLASS, ALL_CLASSES, WEAPON_CLASS, 0 };
25.   /* different default choices when wielding a sling (gold must be included) */
26.   static NEARDATA const char bullets[] =
27.   	{ ALLOW_COUNT, GOLD_CLASS, ALL_CLASSES, GEM_CLASS, 0 };
28.   
29.   extern boolean notonhead;	/* for long worms */
30.   
31.   
32.   /* Throw the selected object, asking for direction */
33.   STATIC_OVL int
34.   throw_obj(obj, shotlimit)
35.   struct obj *obj;
36.   int shotlimit;
37.   {
38.   	struct obj *otmp;
39.   	int multishot = 1;
40.   	schar skill;
41.   	long wep_mask;
42.   	boolean twoweap;
43.   
44.   	/* ask "in what direction?" */
45.   #ifndef GOLDOBJ
46.   	if (!getdir((char *)0)) {
47.   		if (obj->oclass == GOLD_CLASS) {
48.   		    u.ugold += obj->quan;
49.   		    flags.botl = 1;
50.   		    dealloc_obj(obj);
51.   		}
52.   		return(0);
53.   	}
54.   
55.   	if(obj->oclass == GOLD_CLASS) return(throw_gold(obj));
56.   #else
57.   	if (!getdir((char *)0)) {
58.   	    /* obj might need to be merged back into the singular gold object */
59.   	    freeinv(obj);
60.   	    addinv(obj);
61.   	    return(0);
62.   	}
63.   
64.           /*
65.   	  Throwing money is usually for getting rid of it when
66.             a leprechaun approaches, or for bribing an oncoming 
67.             angry monster.  So throw the whole object.
68.   
69.             If the money is in quiver, throw one coin at a time,
70.             possibly using a sling.
71.           */
72.   	if(obj->oclass == GOLD_CLASS && obj != uquiver) return(throw_gold(obj));
73.   #endif
74.   
75.   	if(!canletgo(obj,"throw"))
76.   		return(0);
77.   	if (obj->oartifact == ART_MJOLLNIR && obj != uwep) {
78.   	    pline("%s must be wielded before it can be thrown.",
79.   		The(xname(obj)));
80.   		return(0);
81.   	}
82.   	if ((obj->oartifact == ART_MJOLLNIR && ACURR(A_STR) < STR19(25))
83.   	   || (obj->otyp == BOULDER && !throws_rocks(youmonst.data))) {
84.   		pline("It's too heavy.");
85.   		return(1);
86.   	}
87.   	if(!u.dx && !u.dy && !u.dz) {
88.   		You("cannot throw an object at yourself.");
89.   		return(0);
90.   	}
91.   	u_wipe_engr(2);
92.   	if (!uarmg && !Stone_resistance && (obj->otyp == CORPSE &&
93.   		    touch_petrifies(&mons[obj->corpsenm]))) {
94.   		You("throw the %s corpse with your bare %s.",
95.   		    mons[obj->corpsenm].mname, body_part(HAND));
96.   		Sprintf(killer_buf, "%s corpse", an(mons[obj->corpsenm].mname));
97.   		instapetrify(killer_buf);
98.   	}
99.   	if (welded(obj)) {
100.  		weldmsg(obj);
101.  		return 1;
102.  	}
103.  
104.  	/* Multishot calculations
105.  	 */
106.  	skill = objects[obj->otyp].oc_skill;
107.  	if ((ammo_and_launcher(obj, uwep) || skill == P_DAGGER ||
108.  			skill == -P_DART || skill == -P_SHURIKEN) &&
109.  		!(Confusion || Stunned)) {
110.  	    /* Bonus if the player is proficient in this weapon... */
111.  	    switch (P_SKILL(weapon_type(obj))) {
112.  	    default:	break; /* No bonus */
113.  	    case P_SKILLED:	multishot++; break;
114.  	    case P_EXPERT:	multishot += 2; break;
115.  	    }
116.  	    /* ...or is using a special weapon for their role... */
117.  	    switch (Role_switch) {
118.  	    case PM_RANGER:
119.  		multishot++;
120.  		break;
121.  	    case PM_ROGUE:
122.  		if (skill == P_DAGGER) multishot++;
123.  		break;
124.  	    case PM_SAMURAI:
125.  		if (obj->otyp == YA && uwep && uwep->otyp == YUMI) multishot++;
126.  		break;
127.  	    default:
128.  		break;	/* No bonus */
129.  	    }
130.  	    /* ...or using their race's special bow */
131.  	    switch (Race_switch) {
132.  	    case PM_ELF:
133.  		if (obj->otyp == ELVEN_ARROW && uwep &&
134.  				uwep->otyp == ELVEN_BOW) multishot++;
135.  		break;
136.  	    case PM_ORC:
137.  		if (obj->otyp == ORCISH_ARROW && uwep &&
138.  				uwep->otyp == ORCISH_BOW) multishot++;
139.  		break;
140.  	    default:
141.  		break;	/* No bonus */
142.  	    }
143.  	}
144.  
145.  	if ((long)multishot > obj->quan) multishot = (int)obj->quan;
146.  	multishot = rnd(multishot);
147.  	if (shotlimit > 0 && multishot > shotlimit) multishot = shotlimit;
148.  
149.  	m_shot.s = ammo_and_launcher(obj,uwep) ? TRUE : FALSE;
150.  	/* give a message if shooting more than one, or if player
151.  	   attempted to specify a count */
152.  	if (multishot > 1 || shotlimit > 0) {
153.  	    /* "You shoot N arrows." or "You throw N daggers." */
154.  	    You("%s %d %s.",
155.  		m_shot.s ? "shoot" : "throw",
156.  		multishot,	/* (might be 1 if player gave shotlimit) */
157.  		(multishot == 1) ? singular(obj, xname) :  xname(obj));
158.  	}
159.  
160.  	wep_mask = obj->owornmask;
161.  	m_shot.o = obj->otyp;
162.  	m_shot.n = multishot;
163.  	for (m_shot.i = 1; m_shot.i <= m_shot.n; m_shot.i++) {
164.  	    twoweap = u.twoweap;
165.  	    /* split this object off from its slot if necessary */
166.  	    if (obj->quan > 1L) {
167.  		otmp = splitobj(obj, 1L);
168.  	    } else {
169.  		otmp = obj;
170.  		if (otmp->owornmask && otmp != uball)
171.  		    remove_worn_item(otmp);
172.  	    }
173.  	    freeinv(otmp);
174.  	    throwit(otmp, wep_mask, twoweap);
175.  	}
176.  	m_shot.n = m_shot.i = 0;
177.  	m_shot.o = STRANGE_OBJECT;
178.  	m_shot.s = FALSE;
179.  
180.  	return 1;
181.  }
182.  
183.  
184.  int
185.  dothrow()
186.  {
187.  	register struct obj *obj;
188.  	int shotlimit;
189.  
190.  	/*
191.  	 * Since some characters shoot multiple missiles at one time,
192.  	 * allow user to specify a count prefix for 'f' or 't' to limit
193.  	 * number of items thrown (to avoid possibly hitting something
194.  	 * behind target after killing it, or perhaps to conserve ammo).
195.  	 *
196.  	 * Prior to 3.3.0, command ``3t meant ``t(shoot) t(shoot) t(shoot)
197.  	 * and took 3 turns.  Now it means ``t(shoot at most 3 missiles).
198.  	 */
199.  	/* kludge to work around parse()'s pre-decrement of `multi' */
200.  	shotlimit = (multi || save_cm) ? multi + 1 : 0;
201.  	multi = 0;		/* reset; it's been used up */
202.  
203.  	if(check_capacity((char *)0)) return(0);
204.  	obj = getobj(uslinging() ? bullets : toss_objs, "throw");
205.  	/* it is also possible to throw food */
206.  	/* (or jewels, or iron balls... ) */
207.  
208.  	if (!obj) return(0);
209.  	return throw_obj(obj, shotlimit);
210.  }
211.  
212.  
213.  /* KMH -- Automatically fill quiver */
214.  /* Suggested by Jeffrey Bay <jbay@convex.hp.com> */
215.  static void
216.  autoquiver()
217.  {
218.  	register struct obj *otmp, *oammo = 0, *omissile = 0, *omisc = 0;
219.  
220.  	if (uquiver)
221.  	    return;
222.  
223.  	/* Scan through the inventory */
224.  	for (otmp = invent; otmp; otmp = otmp->nobj) {
225.  	    if (otmp->owornmask || otmp->oartifact || !otmp->dknown) {
226.  		;	/* Skip it */
227.  	    } else if (otmp->otyp == ROCK ||
228.  			/* seen rocks or known flint or known glass */
229.  			(objects[otmp->otyp].oc_name_known &&
230.  			 otmp->otyp == FLINT) ||
231.  			(objects[otmp->otyp].oc_name_known &&
232.  			 otmp->oclass == GEM_CLASS &&
233.  			 objects[otmp->otyp].oc_material == GLASS)) {
234.  		if (uslinging())
235.  		    oammo = otmp;
236.  		else if (!omisc)
237.  		    omisc = otmp;
238.  	    } else if (otmp->oclass == GEM_CLASS) {
239.  		;	/* skip non-rock gems--they're ammo but
240.  			   player has to select them explicitly */
241.  	    } else if (is_ammo(otmp)) {
242.  		if (ammo_and_launcher(otmp, uwep))
243.  		    /* Ammo matched with launcher (bow and arrow, crossbow and bolt) */
244.  		    oammo = otmp;
245.  		else
246.  		    /* Mismatched ammo (no better than an ordinary weapon) */
247.  		    omisc = otmp;
248.  	    } else if (is_missile(otmp)) {
249.  		/* Missile (dart, shuriken, etc.) */
250.  		omissile = otmp;
251.  	    } else if (otmp->oclass == WEAPON_CLASS && throwing_weapon(otmp)) {
252.  		/* Ordinary weapon */
253.  		if (objects[otmp->otyp].oc_skill == P_DAGGER
254.  			&& !omissile) 
255.  		    omissile = otmp;
256.  		else
257.  		    omisc = otmp;
258.  	    }
259.  	}
260.  
261.  	/* Pick the best choice */
262.  	if (oammo)
263.  	    setuqwep(oammo);
264.  	else if (omissile)
265.  	    setuqwep(omissile);
266.  	else if (omisc)
267.  	    setuqwep(omisc);
268.  
269.  	return;
270.  }
271.  
272.  
273.  /* Throw from the quiver */
274.  int
275.  dofire()
276.  {
277.  	int shotlimit;
278.  
279.  	if(check_capacity((char *)0)) return(0);
280.  	if (!uquiver) {
281.  		if (!flags.autoquiver) {
282.  			/* Don't automatically fill the quiver */
283.  			You("have no ammunition readied!");
284.  			return(dothrow());
285.  		}
286.  		autoquiver();
287.  		if (!uquiver) {
288.  			You("have nothing appropriate for your quiver!");
289.  			return(dothrow());
290.  		} else {
291.  			You("fill your quiver:");
292.  			prinv((char *)0, uquiver, 0L);
293.  		}
294.  	}
295.  
296.  	/*
297.  	 * Since some characters shoot multiple missiles at one time,
298.  	 * allow user to specify a count prefix for 'f' or 't' to limit
299.  	 * number of items thrown (to avoid possibly hitting something
300.  	 * behind target after killing it, or perhaps to conserve ammo).
301.  	 *
302.  	 * The number specified can never increase the number of missiles.
303.  	 * Using ``5f when the shooting skill (plus RNG) dictates launch
304.  	 * of 3 projectiles will result in 3 being shot, not 5.
305.  	 */
306.  	/* kludge to work around parse()'s pre-decrement of `multi' */
307.  	shotlimit = (multi || save_cm) ? multi + 1 : 0;
308.  	multi = 0;		/* reset; it's been used up */
309.  
310.  	return throw_obj(uquiver, shotlimit);
311.  }
312.  
313.  
314.  /*
315.   * Object hits floor at hero's feet.  Called from drop() and throwit().
316.   */
317.  void
318.  hitfloor(obj)
319.  register struct obj *obj;
320.  {
321.  	if (IS_SOFT(levl[u.ux][u.uy].typ) || u.uinwater) {
322.  		dropy(obj);
323.  		return;
324.  	}
325.  	if (IS_ALTAR(levl[u.ux][u.uy].typ))
326.  		doaltarobj(obj);
327.  	else
328.  		pline("%s hit%s the %s.", Doname2(obj),
329.  		      (obj->quan == 1L) ? "s" : "", surface(u.ux,u.uy));
330.  
331.  	if (hero_breaks(obj, u.ux, u.uy, TRUE)) return;
332.  	if (ship_object(obj, u.ux, u.uy, FALSE)) return;
333.  	dropy(obj);
334.  }
335.  
336.  /*
337.   * Walk a path from src_cc to dest_cc, calling a proc for each location
338.   * except the starting one.  If the proc returns FALSE, stop walking
339.   * and return FALSE.  If stopped early, dest_cc will be the location
340.   * before the failed callback.
341.   */
342.  boolean
343.  walk_path(src_cc, dest_cc, check_proc, arg)
344.      coord *src_cc;
345.      coord *dest_cc;
346.      boolean FDECL((*check_proc), (genericptr_t, int, int));
347.      genericptr_t arg;
348.  {
349.      int x, y, dx, dy, x_change, y_change, err, i, prev_x, prev_y;
350.      boolean keep_going = TRUE;
351.  
352.      /* Use Bresenham's Line Algorithm to walk from src to dest */
353.      dx = dest_cc->x - src_cc->x;
354.      dy = dest_cc->y - src_cc->y;
355.      prev_x = x = src_cc->x;
356.      prev_y = y = src_cc->y;
357.  
358.      if (dx < 0) {
359.  	x_change = -1;
360.  	dx = -dx;
361.      } else
362.  	x_change = 1;
363.      if (dy < 0) {
364.  	y_change = -1;
365.  	dy = -dy;
366.      } else
367.  	y_change = 1;
368.  
369.      i = err = 0;
370.      if (dx < dy) {
371.  	while (i++ < dy) {
372.  	    prev_x = x;
373.  	    prev_y = y;
374.  	    y += y_change;
375.  	    err += dx;
376.  	    if (err >= dy) {
377.  		x += x_change;
378.  		err -= dy;
379.  	    }
380.  	/* check for early exit condition */
381.  	if (!(keep_going = (*check_proc)(arg, x, y)))
382.  	    break;
383.  	}
384.      } else {
385.  	while (i++ < dx) {
386.  	    prev_x = x;
387.  	    prev_y = y;
388.  	    x += x_change;
389.  	    err += dy;
390.  	    if (err >= dx) {
391.  		y += y_change;
392.  		err -= dx;
393.  	    }
394.  	/* check for early exit condition */
395.  	if (!(keep_going = (*check_proc)(arg, x, y)))
396.  	    break;
397.  	}
398.      }
399.  
400.      if (keep_going)
401.  	return TRUE;	/* successful */
402.  
403.      dest_cc->x = prev_x;
404.      dest_cc->y = prev_y;
405.      return FALSE;
406.  }
407.  
408.  /*
409.   * Single step for the hero flying through the air from jumping, flying,
410.   * etc.  Called from hurtle() and jump() via walk_path().  We expect the
411.   * argument to be a pointer to an integer -- the range -- which is
412.   * used in the calculation of points off if we hit something.
413.   *
414.   * Bumping into monsters won't cause damage but will wake them and make
415.   * them angry.  Auto-pickup isn't done, since you don't have control over
416.   * your movements at the time.
417.   *
418.   * Possible additions/changes:
419.   *	o really attack monster if we hit one
420.   *	o set stunned if we hit a wall or door
421.   *	o reset nomul when we stop
422.   *	o creepy feeling if pass through monster (if ever implemented...)
423.   *	o bounce off walls
424.   *	o let jumps go over boulders
425.   */
426.  boolean
427.  hurtle_step(arg, x, y)
428.      genericptr_t arg;
429.      int x, y;
430.  {
431.      int ox, oy, *range = (int *)arg;
432.      struct obj *obj;
433.      struct monst *mon;
434.      boolean may_pass = TRUE;
435.      struct trap *ttmp;
436.      
437.      if (!isok(x,y)) {
438.  	You_feel("the spirits holding you back.");
439.  	return FALSE;
440.      }
441.  
442.      if (!Passes_walls || !(may_pass = may_passwall(x, y))) {
443.  	if (IS_ROCK(levl[x][y].typ) || closed_door(x,y)) {
444.  	    char *s;
445.  
446.  	    pline("Ouch!");
447.  	    if (IS_TREE(levl[x][y].typ))
448.  		s = "bumping into a tree";
449.  	    else if (IS_ROCK(levl[x][y].typ))
450.  		s = "bumping into a wall";
451.  	    else
452.  		s = "bumping into a door";
453.  	    losehp(rnd(2+*range), s, KILLED_BY);
454.  	    return FALSE;
455.  	}
456.  	if (levl[x][y].typ == IRONBARS) {
457.  	    You("crash into some iron bars.  Ouch!");
458.  	    losehp(rnd(2+*range), "crashing into iron bars", KILLED_BY);
459.  	    return FALSE;
460.  	}
461.  	if ((obj = sobj_at(BOULDER,x,y)) != 0) {
462.  	    You("bump into a %s.  Ouch!", xname(obj));
463.  	    losehp(rnd(2+*range), "bumping into a boulder", KILLED_BY);
464.  	    return FALSE;
465.  	}
466.  	if (!may_pass) {
467.  	    /* did we hit a no-dig non-wall position? */
468.  	    You("smack into something!");
469.  	    losehp(rnd(2+*range), "touching the edge of the universe", KILLED_BY);
470.  	    return FALSE;
471.  	}
472.      }
473.  
474.      if ((mon = m_at(x, y)) != 0) {
475.  	You("bump into %s.", a_monnam(mon));
476.  	wakeup(mon);
477.  	return FALSE;
478.      }
479.      if ((u.ux - x) && (u.uy - y) &&
480.  	bad_rock(youmonst.data,u.ux,y) && bad_rock(youmonst.data,x,u.uy)) {
481.  	/* Move at a diagonal. */
482.  	if (In_sokoban(&u.uz)) {
483.  	    You("come to an abrupt halt!");
484.  	    return FALSE;
485.  	}
486.      }
487.  
488.      ox = u.ux;
489.      oy = u.uy;
490.      u.ux = x;
491.      u.uy = y;
492.      newsym(ox, oy);		/* update old position */
493.      vision_recalc(1);		/* update for new position */
494.      flush_screen(1);
495.      /* FIXME:
496.       * Each trap should really trigger on the recoil if
497.       * it would trigger during normal movement. However,
498.       * not all the possible side-effects of this are
499.       * tested [as of 3.4.0] so we trigger those that
500.       * we have tested, and offer a message for the
501.       * ones that we have not yet tested.
502.       */
503.      if ((ttmp = t_at(x, y)) != 0) {
504.      	if (ttmp->ttyp == MAGIC_PORTAL) {
505.      		dotrap(ttmp,0);
506.      		return FALSE;
507.  	} else if (ttmp->ttyp == FIRE_TRAP) {
508.      		dotrap(ttmp,0);
509.  	} else if ((ttmp->ttyp == PIT || ttmp->ttyp == SPIKED_PIT ||
510.  		    ttmp->ttyp == HOLE || ttmp->ttyp == TRAPDOOR) &&
511.  		   In_sokoban(&u.uz)) {
512.  		/* Air currents overcome the recoil */
513.      		dotrap(ttmp,0);
514.  		return FALSE;
515.      	} else {
516.  		if (ttmp->tseen)
517.  		    You("pass right over %s %s.",
518.  		    	(ttmp->ttyp == ARROW_TRAP) ? "an" : "a",
519.  		    	defsyms[trap_to_defsym(ttmp->ttyp)].explanation);
520.      	}
521.      }
522.      if (--*range < 0)		/* make sure our range never goes negative */
523.  	*range = 0;
524.      if (*range != 0)
525.  	delay_output();
526.      return TRUE;
527.  }
528.  
529.  STATIC_OVL boolean
530.  mhurtle_step(arg, x, y)
531.      genericptr_t arg;
532.      int x, y;
533.  {
534.  	struct monst *mon = (struct monst *)arg;
535.  
536.  	/* TODO: Treat walls, doors, iron bars, pools, lava, etc. specially
537.  	 * rather than just stopping before.
538.  	 */
539.  	if (goodpos(x, y, mon) && m_in_out_region(mon, x, y)) {
540.  	    remove_monster(mon->mx, mon->my);
541.  	    newsym(mon->mx, mon->my);
542.  	    place_monster(mon, x, y);
543.  	    newsym(mon->mx, mon->my);
544.  	    set_apparxy(mon);
545.  	    (void) mintrap(mon);
546.  	    return TRUE;
547.  	}
548.  	return FALSE;
549.  }
550.  
551.  /*
552.   * The player moves through the air for a few squares as a result of
553.   * throwing or kicking something.
554.   *
555.   * dx and dy should be the direction of the hurtle, not of the original
556.   * kick or throw and be only.
557.   */
558.  void
559.  hurtle(dx, dy, range, verbose)
560.      int dx, dy, range;
561.      boolean verbose;
562.  {
563.      coord uc, cc;
564.  
565.      /* The chain is stretched vertically, so you shouldn't be able to move
566.       * very far diagonally.  The premise that you should be able to move one
567.       * spot leads to calculations that allow you to only move one spot away
568.       * from the ball, if you are levitating over the ball, or one spot
569.       * towards the ball, if you are at the end of the chain.  Rather than
570.       * bother with all of that, assume that there is no slack in the chain
571.       * for diagonal movement, give the player a message and return.
572.       */
573.      if(Punished && !carried(uball)) {
574.  	You_feel("a tug from the iron ball.");
575.  	nomul(0);
576.  	return;
577.      } else if (u.utrap) {
578.  	You("are anchored by the %s.",
579.  	    u.utraptype == TT_WEB ? "web" : u.utraptype == TT_LAVA ? "lava" :
580.  		u.utraptype == TT_INFLOOR ? surface(u.ux,u.uy) : "trap");
581.  	nomul(0);
582.  	return;
583.      }
584.  
585.      /* make sure dx and dy are [-1,0,1] */
586.      dx = sgn(dx);
587.      dy = sgn(dy);
588.  
589.      if(!range || (!dx && !dy) || u.ustuck) return; /* paranoia */
590.  
591.      nomul(-range);
592.      if (verbose)
593.  	You("%s in the opposite direction.", range > 1 ? "hurtle" : "float");
594.      /* if we're in the midst of shooting multiple projectiles, stop */
595.      if (m_shot.i < m_shot.n) {
596.  	/* last message before hurtling was "you shoot N arrows" */
597.  	You("stop %sing after the first %s.",
598.  	    m_shot.s ? "shoot" : "throw", m_shot.s ? "shot" : "toss");
599.  	m_shot.n = m_shot.i;	/* make current shot be the last */
600.      }
601.      if (In_sokoban(&u.uz))
602.  	change_luck(-1);	/* Sokoban guilt */
603.      uc.x = u.ux;
604.      uc.y = u.uy;
605.      /* this setting of cc is only correct if dx and dy are [-1,0,1] only */
606.      cc.x = u.ux + (dx * range);
607.      cc.y = u.uy + (dy * range);
608.      (void) walk_path(&uc, &cc, hurtle_step, (genericptr_t)&range);
609.  }
610.  
611.  /* Move a monster through the air for a few squares.
612.   */
613.  void
614.  mhurtle(mon, dx, dy, range)
615.  	struct monst *mon;
616.  	int dx, dy, range;
617.  {
618.      coord mc, cc;
619.  
620.  	/* At the very least, debilitate the monster */
621.  	mon->movement = 0;
622.  	mon->mstun = 1;
623.  
624.  	/* Is the monster stuck or too heavy to push?
625.  	 * (very large monsters have too much inertia, even floaters and flyers)
626.  	 */
627.  	if (mon->data->msize >= MZ_HUGE || mon == u.ustuck || mon->mtrapped)
628.  	    return;
629.  
630.      /* Make sure dx and dy are [-1,0,1] */
631.      dx = sgn(dx);
632.      dy = sgn(dy);
633.      if(!range || (!dx && !dy)) return; /* paranoia */
634.  
635.  	/* Send the monster along the path */
636.  	mc.x = mon->mx;
637.  	mc.y = mon->my;
638.  	cc.x = mon->mx + (dx * range);
639.  	cc.y = mon->my + (dy * range);
640.  	(void) walk_path(&mc, &cc, mhurtle_step, (genericptr_t)mon);
641.  	return;
642.  }
643.  
644.  STATIC_OVL void
645.  check_shop_obj(obj, x, y, broken)
646.  register struct obj *obj;
647.  register xchar x, y;
648.  register boolean broken;
649.  {
650.  	struct monst *shkp = shop_keeper(*u.ushops);
651.  
652.  	if(!shkp) return;
653.  
654.  	if(broken) {
655.  		if (obj->unpaid) {
656.  		    (void)stolen_value(obj, u.ux, u.uy,
657.  				       (boolean)shkp->mpeaceful, FALSE);
658.  		    subfrombill(obj, shkp);
659.  		}
660.  		obj->no_charge = 1;
661.  		return;
662.  	}
663.  
664.  	if (!costly_spot(x, y) || *in_rooms(x, y, SHOPBASE) != *u.ushops) {
665.  		/* thrown out of a shop or into a different shop */
666.  		if (obj->unpaid) {
667.  		    (void)stolen_value(obj, u.ux, u.uy,
668.  				       (boolean)shkp->mpeaceful, FALSE);
669.  		    subfrombill(obj, shkp);
670.  		}
671.  	} else {
672.  		if (costly_spot(u.ux, u.uy) && costly_spot(x, y)) {
673.  		    if(obj->unpaid) subfrombill(obj, shkp);
674.  		    else if(!(x == shkp->mx && y == shkp->my))
675.  			    sellobj(obj, x, y);
676.  		}
677.  	}
678.  }
679.  
680.  /*
681.   * Hero tosses an object upwards with appropriate consequences.
682.   *
683.   * Returns FALSE if the object is gone.
684.   */
685.  STATIC_OVL boolean
686.  toss_up(obj, hitsroof)
687.  struct obj *obj;
688.  boolean hitsroof;
689.  {
690.      const char *almost;
691.      /* note: obj->quan == 1 */
692.  
693.      if (hitsroof) {
694.  	if (breaktest(obj)) {
695.  		pline("%s hits the %s.", Doname2(obj), ceiling(u.ux, u.uy));
696.  		breakmsg(obj, !Blind);
697.  		breakobj(obj, u.ux, u.uy, TRUE, TRUE);
698.  		return FALSE;
699.  	}
700.  	almost = "";
701.      } else {
702.  	almost = " almost";
703.      }
704.      pline("%s%s hits the %s, then falls back on top of your %s.",
705.  	  Doname2(obj), almost, ceiling(u.ux,u.uy), body_part(HEAD));
706.  
707.      /* object now hits you */
708.  
709.      if (obj->oclass == POTION_CLASS) {
710.  	potionhit(&youmonst, obj, TRUE);
711.      } else if (breaktest(obj)) {
712.  	int otyp = obj->otyp, ocorpsenm = obj->corpsenm;
713.  	int blindinc;
714.  
715.  	/* need to check for blindness result prior to destroying obj */
716.  	blindinc = (otyp == CREAM_PIE || otyp == BLINDING_VENOM) &&
717.  		   /* AT_WEAP is ok here even if attack type was AT_SPIT */
718.  		   can_blnd(&youmonst, &youmonst, AT_WEAP, obj) ? rnd(25) : 0;
719.  
720.  	breakmsg(obj, !Blind);
721.  	breakobj(obj, u.ux, u.uy, TRUE, TRUE);
722.  	obj = 0;	/* it's now gone */
723.  	switch (otyp) {
724.  	case EGG:
725.  		if (touch_petrifies(&mons[ocorpsenm]) &&
726.  		    !uarmh && !Stone_resistance &&
727.  		    !(poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM)))
728.  		goto petrify;
729.  	case CREAM_PIE:
730.  	case BLINDING_VENOM:
731.  		pline("You've got it all over your %s!", body_part(FACE));
732.  		if (blindinc) {
733.  		    if (otyp == BLINDING_VENOM && !Blind)
734.  			pline("It blinds you!");
735.  		    u.ucreamed += blindinc;
736.  		    make_blinded(Blinded + (long)blindinc, FALSE);
737.  		    if (!Blind) Your(vision_clears);
738.  		}
739.  		break;
740.  	default:
741.  		break;
742.  	}
743.  	return FALSE;
744.      } else {		/* neither potion nor other breaking object */
745.  	boolean less_damage = uarmh && is_metallic(uarmh), artimsg = FALSE;
746.  	int dmg = dmgval(obj, &youmonst);
747.  
748.  	if (obj->oartifact)
749.  	    /* need a fake die roll here; rn1(18,2) avoids 1 and 20 */
750.  	    artimsg = artifact_hit((struct monst *)0, &youmonst,
751.  				   obj, &dmg, rn1(18,2));
752.  
753.  	if (!dmg) {	/* probably wasn't a weapon; base damage on weight */
754.  	    dmg = (int) obj->owt / 100;
755.  	    if (dmg < 1) dmg = 1;
756.  	    else if (dmg > 6) dmg = 6;
757.  	    if (youmonst.data == &mons[PM_SHADE] &&
758.  		    objects[obj->otyp].oc_material != SILVER)
759.  		dmg = 0;
760.  	}
761.  	if (dmg > 1 && less_damage) dmg = 1;
762.  	if (dmg > 0) dmg += u.udaminc;
763.  	if (dmg < 0) dmg = 0;	/* beware negative rings of increase damage */
764.  	if (Half_physical_damage) dmg = (dmg + 1) / 2;
765.  
766.  	if (uarmh) {
767.  	    if (less_damage && dmg < (Upolyd ? u.mh : u.uhp)) {
768.  		if (!artimsg)
769.  		    pline("Fortunately, you are wearing a hard helmet.");
770.  	    } else if (flags.verbose &&
771.  		    !(obj->otyp == CORPSE && touch_petrifies(&mons[obj->corpsenm])))
772.  		Your("%s does not protect you.", xname(uarmh));
773.  	} else if (obj->otyp == CORPSE && touch_petrifies(&mons[obj->corpsenm])) {
774.  	    if (!Stone_resistance &&
775.  		    !(poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM))) {
776.   petrify:
777.  		killer_format = KILLED_BY;
778.  		killer = "elementary physics";	/* "what goes up..." */
779.  		You("turn to stone.");
780.  		if (obj) dropy(obj);	/* bypass most of hitfloor() */
781.  		done(STONING);
782.  		return obj ? TRUE : FALSE;
783.  	    }
784.  	}
785.  	hitfloor(obj);
786.  	losehp(dmg, "falling object", KILLED_BY_AN);
787.      }
788.      return TRUE;
789.  }
790.  
791.  /* return true for weapon meant to be thrown; excludes ammo */
792.  STATIC_OVL boolean
793.  throwing_weapon(obj)
794.  struct obj *obj;
795.  {
796.  	return (is_missile(obj) || is_spear(obj) ||
797.  			/* daggers and knife (excludes scalpel) */
798.  			(is_blade(obj) && (objects[obj->otyp].oc_dir & PIERCE)) ||
799.  			/* special cases [might want to add AXE] */
800.  			obj->otyp == WAR_HAMMER || obj->otyp == AKLYS);
801.  }
802.  
803.  /* the currently thrown object is returning to you (not for boomerangs) */
804.  STATIC_OVL void
805.  sho_obj_return_to_u(obj)
806.  struct obj *obj;
807.  {
808.      /* might already be our location (bounced off a wall) */
809.      if (bhitpos.x != u.ux || bhitpos.y != u.uy) {
810.  	int x = bhitpos.x - u.dx, y = bhitpos.y - u.dy;
811.  
812.  	tmp_at(DISP_FLASH, obj_to_glyph(obj));
813.  	while(x != u.ux || y != u.uy) {
814.  	    tmp_at(x, y);
815.  	    delay_output();
816.  	    x -= u.dx; y -= u.dy;
817.  	}
818.  	tmp_at(DISP_END, 0);
819.      }
820.  }
821.  
822.  void
823.  throwit(obj, wep_mask, twoweap)
824.  register struct obj *obj;
825.  long wep_mask;	/* used to re-equip returning boomerang */
826.  boolean twoweap; /* used to restore twoweapon mode if wielded weapon returns */
827.  {
828.  	register struct monst *mon;
829.  	register int range, urange;
830.  	boolean impaired = (Confusion || Stunned || Blind ||
831.  			   Hallucination || Fumbling);
832.  
833.  	if ((obj->cursed || obj->greased) && (u.dx || u.dy) && !rn2(7)) {
834.  	    boolean slipok = TRUE;
835.  	    if (ammo_and_launcher(obj, uwep))
836.  		pline("%s!", Tobjnam(obj, "misfire"));
837.  	    else {
838.  		/* only slip if it's greased or meant to be thrown */
839.  		if (obj->greased || throwing_weapon(obj))
840.  		    /* BUG: this message is grammatically incorrect if obj has
841.  		       a plural name; greased gloves or boots for instance. */
842.  		    pline("%s as you throw it!", Tobjnam(obj, "slip"));
843.  		else slipok = FALSE;
844.  	    }
845.  	    if (slipok) {
846.  		u.dx = rn2(3)-1;
847.  		u.dy = rn2(3)-1;
848.  		if (!u.dx && !u.dy) u.dz = 1;
849.  		impaired = TRUE;
850.  	    }
851.  	}
852.  
853.  	if ((u.dx || u.dy || (u.dz < 1)) &&
854.  	    calc_capacity((int)obj->owt) > SLT_ENCUMBER &&
855.  	    (Upolyd ? (u.mh < 5 && u.mh != u.mhmax)
856.  	     : (u.uhp < 10 && u.uhp != u.uhpmax)) &&
857.  	    obj->owt > (unsigned)((Upolyd ? u.mh : u.uhp) * 2) &&
858.  	    !Is_airlevel(&u.uz)) {
859.  	    You("have so little stamina, %s drops from your grasp.",
860.  		the(xname(obj)));
861.  	    exercise(A_CON, FALSE);
862.  	    u.dx = u.dy = 0;
863.  	    u.dz = 1;
864.  	}
865.  
866.  	if(u.uswallow) {
867.  		mon = u.ustuck;
868.  		bhitpos.x = mon->mx;
869.  		bhitpos.y = mon->my;
870.  	} else if(u.dz) {
871.  	    if (u.dz < 0 && Role_if(PM_VALKYRIE) &&
872.  		    obj->oartifact == ART_MJOLLNIR && !impaired) {
873.  		pline("%s the %s and returns to your hand!",
874.  		      Tobjnam(obj, "hit"), ceiling(u.ux,u.uy));
875.  		obj = addinv(obj);
876.  		(void) encumber_msg();
877.  		setuwep(obj);
878.  		u.twoweap = twoweap;
879.  	    } else if (u.dz < 0 && !Is_airlevel(&u.uz) &&
880.  		    !Underwater && !Is_waterlevel(&u.uz)) {
881.  		(void) toss_up(obj, rn2(5));
882.  	    } else {
883.  		hitfloor(obj);
884.  	    }
885.  	    return;
886.  
887.  	} else if(obj->otyp == BOOMERANG && !Underwater) {
888.  		if(Is_airlevel(&u.uz) || Levitation)
889.  		    hurtle(-u.dx, -u.dy, 1, TRUE);
890.  		mon = boomhit(u.dx, u.dy);
891.  		if(mon == &youmonst) {		/* the thing was caught */
892.  			exercise(A_DEX, TRUE);
893.  			obj = addinv(obj);
894.  			(void) encumber_msg();
895.  			if (wep_mask && !(obj->owornmask & wep_mask)) {
896.  			    setworn(obj, wep_mask);
897.  			    u.twoweap = twoweap;
898.  			}
899.  			return;
900.  		}
901.  	} else {
902.  		urange = (int)(ACURRSTR)/2;
903.  		/* balls are easy to throw or at least roll */
904.  		/* also, this insures the maximum range of a ball is greater
905.  		 * than 1, so the effects from throwing attached balls are
906.  		 * actually possible
907.  		 */
908.  		if (obj->otyp == HEAVY_IRON_BALL)
909.  			range = urange - (int)(obj->owt/100);
910.  		else
911.  			range = urange - (int)(obj->owt/40);
912.  		if (obj == uball) {
913.  			if (u.ustuck) range = 1;
914.  			else if (range >= 5) range = 5;
915.  		}
916.  		if (range < 1) range = 1;
917.  
918.  		if (is_ammo(obj)) {
919.  		    if (ammo_and_launcher(obj, uwep))
920.  			range++;
921.  		    else if (obj->oclass != GEM_CLASS)
922.  			range /= 2;
923.  		}
924.  
925.  		if (Is_airlevel(&u.uz) || Levitation) {
926.  		    /* action, reaction... */
927.  		    urange -= range;
928.  		    if(urange < 1) urange = 1;
929.  		    range -= urange;
930.  		    if(range < 1) range = 1;
931.  		}
932.  
933.  		if (obj->otyp == BOULDER)
934.  		    range = 20;		/* you must be giant */
935.  		else if (obj->oartifact == ART_MJOLLNIR)
936.  		    range = (range + 1) / 2;	/* it's heavy */
937.  		else if (obj == uball && u.utrap && u.utraptype == TT_INFLOOR)
938.  		    range = 1;
939.  
940.  		if (Underwater) range = 1;
941.  
942.  		mon = bhit(u.dx, u.dy, range, THROWN_WEAPON,
943.  			   (int FDECL((*),(MONST_P,OBJ_P)))0,
944.  			   (int FDECL((*),(OBJ_P,OBJ_P)))0,
945.  			   obj);
946.  
947.  		/* have to do this after bhit() so u.ux & u.uy are correct */
948.  		if(Is_airlevel(&u.uz) || Levitation)
949.  		    hurtle(-u.dx, -u.dy, urange, TRUE);
950.  	}
951.  
952.  	if (mon) {
953.  		boolean obj_gone;
954.  
955.  		if (mon->isshk &&
956.  			obj->where == OBJ_MINVENT && obj->ocarry == mon)
957.  		    return;		/* alert shk caught it */
958.  		(void) snuff_candle(obj);
959.  		notonhead = (bhitpos.x != mon->mx || bhitpos.y != mon->my);
960.  		obj_gone = thitmonst(mon, obj);
961.  		/* Monster may have been tamed; this frees old mon */
962.  		mon = m_at(bhitpos.x, bhitpos.y);
963.  
964.  		/* [perhaps this should be moved into thitmonst or hmon] */
965.  		if (mon && mon->isshk &&
966.  			(!inside_shop(u.ux, u.uy) ||
967.  			 !index(in_rooms(mon->mx, mon->my, SHOPBASE), *u.ushops)))
968.  		    hot_pursuit(mon);
969.  
970.  		if (obj_gone) return;
971.  	}
972.  
973.  	if (u.uswallow) {
974.  		/* ball is not picked up by monster */
975.  		if (obj != uball) (void) mpickobj(u.ustuck,obj);
976.  	} else {
977.  		/* the code following might become part of dropy() */
978.  		if (obj->oartifact == ART_MJOLLNIR &&
979.  			Role_if(PM_VALKYRIE) && rn2(100)) {
980.  		    /* we must be wearing Gauntlets of Power to get here */
981.  		    sho_obj_return_to_u(obj);	    /* display its flight */
982.  
983.  		    if (!impaired && rn2(100)) {
984.  			pline("%s to your hand!", Tobjnam(obj, "return"));
985.  			obj = addinv(obj);
986.  			(void) encumber_msg();
987.  			setuwep(obj);
988.  			u.twoweap = twoweap;
989.  			if(cansee(bhitpos.x, bhitpos.y))
990.  			    newsym(bhitpos.x,bhitpos.y);
991.  		    } else {
992.  			int dmg = rn2(2);
993.  			if (!dmg) {
994.  			    pline(Blind ? "%s lands %s your %s." :
995.  					"%s back to you, landing %s your %s.",
996.  				  Blind ? Something : Tobjnam(obj, "return"),
997.  				  Levitation ? "beneath" : "at",
998.  				  makeplural(body_part(FOOT)));
999.  			} else {
1000. 			    dmg += rnd(3);
1001. 			    pline(Blind ? "%s your %s!" :
1002. 					"%s back toward you, hitting your %s!",
1003. 				  Tobjnam(obj, Blind ? "hit" : "fly"),
1004. 				  body_part(ARM));
1005. 			    (void) artifact_hit((struct monst *)0,
1006. 						&youmonst, obj, &dmg, 0);
1007. 			    losehp(dmg, xname(obj), KILLED_BY);
1008. 			}
1009. 			if (ship_object(obj, u.ux, u.uy, FALSE))
1010. 			    return;
1011. 			dropy(obj);
1012. 		    }
1013. 		    return;
1014. 		}
1015. 
1016. 		if (!IS_SOFT(levl[bhitpos.x][bhitpos.y].typ) &&
1017. 			breaktest(obj)) {
1018. 		    tmp_at(DISP_FLASH, obj_to_glyph(obj));
1019. 		    tmp_at(bhitpos.x, bhitpos.y);
1020. 		    delay_output();
1021. 		    tmp_at(DISP_END, 0);
1022. 		    breakmsg(obj, cansee(bhitpos.x, bhitpos.y));
1023. 		    breakobj(obj, bhitpos.x, bhitpos.y, TRUE, TRUE);
1024. 		    return;
1025. 		}
1026. 		if(flooreffects(obj,bhitpos.x,bhitpos.y,"fall")) return;
1027. 		obj_no_longer_held(obj);
1028. 		if (mon && mon->isshk && is_pick(obj)) {
1029. 		    if (cansee(bhitpos.x, bhitpos.y))
1030. 			pline("%s snatches up %s.",
1031. 			      Monnam(mon), the(xname(obj)));
1032. 		    if(*u.ushops)
1033. 			check_shop_obj(obj, bhitpos.x, bhitpos.y, FALSE);
1034. 		    (void) mpickobj(mon, obj);	/* may merge and free obj */
1035. 		    return;
1036. 		}
1037. 		(void) snuff_candle(obj);
1038. 		if (!mon && ship_object(obj, bhitpos.x, bhitpos.y, FALSE))
1039. 		    return;
1040. 		place_object(obj, bhitpos.x, bhitpos.y);
1041. 		if(*u.ushops && obj != uball)
1042. 		    check_shop_obj(obj, bhitpos.x, bhitpos.y, FALSE);
1043. 
1044. 		stackobj(obj);
1045. 		if (obj == uball)
1046. 		    drop_ball(bhitpos.x, bhitpos.y);
1047. 		if (cansee(bhitpos.x, bhitpos.y))
1048. 		    newsym(bhitpos.x,bhitpos.y);
1049. 		if (obj_sheds_light(obj))
1050. 		    vision_full_recalc = 1;
1051. 	}
1052. }
1053. 
1054. /* an object may hit a monster; various factors adjust the chance of hitting */
1055. int
1056. omon_adj(mon, obj, mon_notices)
1057. struct monst *mon;
1058. struct obj *obj;
1059. boolean mon_notices;
1060. {
1061. 	int tmp = 0;
1062. 
1063. 	/* size of target affects the chance of hitting */
1064. 	tmp += (mon->data->msize - MZ_MEDIUM);		/* -2..+5 */
1065. 	/* sleeping target is more likely to be hit */
1066. 	if (mon->msleeping) {
1067. 	    tmp += 2;
1068. 	    if (mon_notices) mon->msleeping = 0;
1069. 	}
1070. 	/* ditto for immobilized target */
1071. 	if (!mon->mcanmove || !mon->data->mmove) {
1072. 	    tmp += 4;
1073. 	    if (mon_notices && mon->data->mmove && !rn2(10)) {
1074. 		mon->mcanmove = 1;
1075. 		mon->mfrozen = 0;
1076. 	    }
1077. 	}
1078. 	/* some objects are more likely to hit than others */
1079. 	switch (obj->otyp) {
1080. 	case HEAVY_IRON_BALL:
1081. 	    if (obj != uball) tmp += 2;
1082. 	    break;
1083. 	case BOULDER:
1084. 	    tmp += 6;
1085. 	    break;
1086. 	default:
1087. 	    if (obj->oclass == WEAPON_CLASS || is_weptool(obj) ||
1088. 		    obj->oclass == GEM_CLASS)
1089. 		tmp += hitval(obj, mon);
1090. 	    break;
1091. 	}
1092. 	return tmp;
1093. }
1094. 
1095. /* thrown object misses target monster */
1096. STATIC_OVL void
1097. tmiss(obj, mon)
1098. struct obj *obj;
1099. struct monst *mon;
1100. {
1101.     const char *missile = mshot_xname(obj);
1102. 
1103.     /* If the target can't be seen or doesn't look like a valid target,
1104.        avoid "the arrow misses it," or worse, "the arrows misses the mimic."
1105.        An attentive player will still notice that this is different from
1106.        an arrow just landing short of any target (no message in that case),
1107.        so will realize that there is a valid target here anyway. */
1108.     if (!canseemon(mon) || (mon->m_ap_type && mon->m_ap_type != M_AP_MONSTER))
1109. 	pline("%s misses.", The(missile));
1110.     else
1111. 	miss(missile, mon);
1112.     if (!rn2(3)) wakeup(mon);
1113.     return;
1114. }
1115. 
1116. #define quest_arti_hits_leader(obj,mon)	\
1117.   (obj->oartifact && is_quest_artifact(obj) && (mon->data->msound == MS_LEADER))
1118. 
1119. /*
1120.  * Object thrown by player arrives at monster's location.
1121.  * Return 1 if obj has disappeared or otherwise been taken care of,
1122.  * 0 if caller must take care of it.
1123.  */
1124. int
1125. thitmonst(mon, obj)
1126. register struct monst *mon;
1127. register struct obj   *obj;
1128. {
1129. 	register int	tmp; /* Base chance to hit */
1130. 	register int	disttmp; /* distance modifier */
1131. 	int otyp = obj->otyp;
1132. 	boolean guaranteed_hit = (u.uswallow && mon == u.ustuck);
1133. 
1134. 	/* Differences from melee weapons:
1135. 	 *
1136. 	 * Dex still gives a bonus, but strength does not.
1137. 	 * Polymorphed players lacking attacks may still throw.
1138. 	 * There's a base -1 to hit.
1139. 	 * No bonuses for fleeing or stunned targets (they don't dodge
1140. 	 *    melee blows as readily, but dodging arrows is hard anyway).
1141. 	 * Not affected by traps, etc.
1142. 	 * Certain items which don't in themselves do damage ignore tmp.
1143. 	 * Distance and monster size affect chance to hit.
1144. 	 */
1145. 	tmp = -1 + Luck + find_mac(mon) + u.uhitinc +
1146. 			maybe_polyd(youmonst.data->mlevel, u.ulevel);
1147. 	if (ACURR(A_DEX) < 4) tmp -= 3;
1148. 	else if (ACURR(A_DEX) < 6) tmp -= 2;
1149. 	else if (ACURR(A_DEX) < 8) tmp -= 1;
1150. 	else if (ACURR(A_DEX) >= 14) tmp += (ACURR(A_DEX) - 14);
1151. 
1152. 	/* Modify to-hit depending on distance; but keep it sane.
1153. 	 * Polearms get a distance penalty even when wielded; it's
1154. 	 * hard to hit at a distance.
1155. 	 */
1156. 	disttmp = 3 - distmin(u.ux, u.uy, mon->mx, mon->my);
1157. 	if(disttmp < -4) disttmp = -4;
1158. 	tmp += disttmp;
1159. 
1160. 	/* gloves are a hinderance to proper use of bows */
1161. 	if (uarmg && uwep && objects[uwep->otyp].oc_skill == P_BOW) {
1162. 	    switch (uarmg->otyp) {
1163. 	    case GAUNTLETS_OF_POWER:    /* metal */
1164. 		tmp -= 2;
1165. 		break;
1166. 	    case GAUNTLETS_OF_FUMBLING:
1167. 		tmp -= 3;
1168. 		break;
1169. 	    case LEATHER_GLOVES:
1170. 	    case GAUNTLETS_OF_DEXTERITY:
1171. 		break;
1172. 	    default:
1173. 		impossible("Unknown type of gloves (%d)", uarmg->otyp);
1174. 		break;
1175. 	    }
1176. 	}
1177. 
1178. 	tmp += omon_adj(mon, obj, TRUE);
1179. 	if (is_orc(mon->data) && maybe_polyd(is_elf(youmonst.data),
1180. 			Race_if(PM_ELF)))
1181. 	    tmp++;
1182. 	if (guaranteed_hit) {
1183. 	    tmp += 1000; /* Guaranteed hit */
1184. 	}
1185. 
1186. 	if (obj->oclass == GEM_CLASS && is_unicorn(mon->data)) {
1187. 	    if (mon->mtame) {
1188. 		pline("%s catches and drops %s.", Monnam(mon), the(xname(obj)));
1189. 		return 0;
1190. 	    } else {
1191. 		pline("%s catches %s.", Monnam(mon), the(xname(obj)));
1192. 		return gem_accept(mon, obj);
1193. 	    }
1194. 	}
1195. 
1196. 	/* don't make game unwinnable if naive player throws artifact
1197. 	   at leader.... */
1198. 	if (quest_arti_hits_leader(obj, mon)) {
1199. 	    /* not wakeup(), which angers non-tame monsters */
1200. 	    mon->msleeping = 0;
1201. 	    mon->mstrategy &= ~STRAT_WAITMASK;
1202. 
1203. 	    if (mon->mcanmove) {
1204. 		pline("%s catches %s.", Monnam(mon), the(xname(obj)));
1205. 		if (mon->mpeaceful) {
1206. 		    boolean next2u = monnear(mon, u.ux, u.uy);
1207. 
1208. 		    finish_quest(obj);	/* acknowledge quest completion */
1209. 		    pline("%s %s %s back to you.", Monnam(mon),
1210. 			  (next2u ? "hands" : "tosses"), the(xname(obj)));
1211. 		    if (!next2u) sho_obj_return_to_u(obj);
1212. 		    obj = addinv(obj);	/* back into your inventory */
1213. 		    (void) encumber_msg();
1214. 		} else {
1215. 		    /* angry leader caught it and isn't returning it */
1216. 		    (void) mpickobj(mon, obj);
1217. 		}
1218. 		return 1;		/* caller doesn't need to place it */
1219. 	    }
1220. 	    return(0);
1221. 	}
1222. 
1223. 	if (obj->oclass == WEAPON_CLASS || is_weptool(obj) ||
1224. 		obj->oclass == GEM_CLASS) {
1225. 	    if (is_ammo(obj)) {
1226. 		if (!ammo_and_launcher(obj, uwep)) {
1227. 		    tmp -= 4;
1228. 		} else {
1229. 		    tmp += uwep->spe - greatest_erosion(uwep);
1230. 		    tmp += weapon_hit_bonus(uwep);
1231. 		    if (uwep->oartifact) tmp += spec_abon(uwep, mon);
1232. 		    /*
1233. 		     * Elves and Samurais are highly trained w/bows,
1234. 		     * especially their own special types of bow.
1235. 		     * Polymorphing won't make you a bow expert.
1236. 		     */
1237. 		    if ((Race_if(PM_ELF) || Role_if(PM_SAMURAI)) &&
1238. 				(!Upolyd || your_race(youmonst.data)) &&
1239. 				objects[uwep->otyp].oc_skill == P_BOW) {
1240. 			tmp++;
1241. 			if (Race_if(PM_ELF) && uwep->otyp == ELVEN_BOW)
1242. 			    tmp++;
1243. 			else if (Role_if(PM_SAMURAI) && uwep->otyp == YUMI)
1244. 			    tmp++;
1245. 		    }
1246. 		}
1247. 	    } else {
1248. 		if (otyp == BOOMERANG)		/* arbitrary */
1249. 		    tmp += 4;
1250. 		else if (throwing_weapon(obj))	/* meant to be thrown */
1251. 		    tmp += 2;
1252. 		else				/* not meant to be thrown */
1253. 		    tmp -= 2;
1254. 		/* we know we're dealing with a weapon or weptool handled
1255. 		   by WEAPON_SKILLS once ammo objects have been excluded */
1256. 		tmp += weapon_hit_bonus(obj);
1257. 	    }
1258. 
1259. 	    if (tmp >= rnd(20)) {
1260. 		if (hmon(mon,obj,1)) {	/* mon still alive */
1261. 		    cutworm(mon, bhitpos.x, bhitpos.y, obj);
1262. 		}
1263. 		exercise(A_DEX, TRUE);
1264. 		/* projectiles other than magic stones
1265. 		   sometimes disappear when thrown */
1266. 		if (objects[otyp].oc_skill < P_NONE &&
1267. 				objects[otyp].oc_skill > -P_BOOMERANG &&
1268. 				!objects[otyp].oc_magic && rn2(3)) {
1269. 		    if (*u.ushops)
1270. 			check_shop_obj(obj, bhitpos.x,bhitpos.y, TRUE);
1271. 		    obfree(obj, (struct obj *)0);
1272. 		    return 1;
1273. 		}
1274. 		passive_obj(mon, obj, (struct attack *)0);
1275. 	    } else {
1276. 		tmiss(obj, mon);
1277. 	    }
1278. 
1279. 	} else if (otyp == HEAVY_IRON_BALL) {
1280. 	    exercise(A_STR, TRUE);
1281. 	    if (tmp >= rnd(20)) {
1282. 		int was_swallowed = guaranteed_hit;
1283. 
1284. 		exercise(A_DEX, TRUE);
1285. 		if (!hmon(mon,obj,1)) {		/* mon killed */
1286. 		    if (was_swallowed && !u.uswallow && obj == uball)
1287. 			return 1;	/* already did placebc() */
1288. 		}
1289. 	    } else {
1290. 		tmiss(obj, mon);
1291. 	    }
1292. 
1293. 	} else if (otyp == BOULDER) {
1294. 	    exercise(A_STR, TRUE);
1295. 	    if (tmp >= rnd(20)) {
1296. 		exercise(A_DEX, TRUE);
1297. 		(void) hmon(mon,obj,1);
1298. 	    } else {
1299. 		tmiss(obj, mon);
1300. 	    }
1301. 
1302. 	} else if ((otyp == EGG || otyp == CREAM_PIE ||
1303. 		    otyp == BLINDING_VENOM || otyp == ACID_VENOM) &&
1304. 		(guaranteed_hit || ACURR(A_DEX) > rnd(25))) {
1305. 	    (void) hmon(mon, obj, 1);
1306. 	    return 1;	/* hmon used it up */
1307. 
1308. 	} else if (obj->oclass == POTION_CLASS &&
1309. 		(guaranteed_hit || ACURR(A_DEX) > rnd(25))) {
1310. 	    potionhit(mon, obj, TRUE);
1311. 	    return 1;
1312. 
1313. 	} else if (befriend_with_obj(mon->data, obj)) {
1314. 	    if (tamedog(mon, obj))
1315. 		return 1;           	/* obj is gone */
1316. 	    else {
1317. 		/* not tmiss(), which angers non-tame monsters */
1318. 		miss(xname(obj), mon);
1319. 		mon->msleeping = 0;
1320. 		mon->mstrategy &= ~STRAT_WAITMASK;
1321. 	    }
1322. 	} else if (guaranteed_hit) {
1323. 	    /* this assumes that guaranteed_hit is due to swallowing */
1324. 	    wakeup(mon);
1325. 	    if (obj->otyp == CORPSE && touch_petrifies(&mons[obj->corpsenm])) {
1326. 		if (is_animal(u.ustuck->data)) {
1327. 			minstapetrify(u.ustuck, TRUE);
1328. 			/* Don't leave a cockatrice corpse available in a statue */
1329. 			if (!u.uswallow) {
1330. 				delobj(obj);
1331. 				return 1;
1332. 			}
1333. 	    	}
1334. 	    }
1335. 	    pline("%s into %s %s.",
1336. 		Tobjnam(obj, "vanish"), s_suffix(mon_nam(mon)),
1337. 		is_animal(u.ustuck->data) ? "entrails" : "currents");
1338. 	} else {
1339. 	    tmiss(obj, mon);
1340. 	}
1341. 
1342. 	return 0;
1343. }
1344. 
1345. STATIC_OVL int
1346. gem_accept(mon, obj)
1347. register struct monst *mon;
1348. register struct obj *obj;
1349. {
1350. 	char buf[BUFSZ];
1351. 	boolean is_buddy = sgn(mon->data->maligntyp) == sgn(u.ualign.type);
1352. 	boolean is_gem = objects[obj->otyp].oc_material == GEMSTONE;
1353. 	int ret = 0;
1354. 	static NEARDATA const char nogood[] = " is not interested in your junk.";
1355. 	static NEARDATA const char acceptgift[] = " accepts your gift.";
1356. 	static NEARDATA const char maybeluck[] = " hesitatingly";
1357. 	static NEARDATA const char noluck[] = " graciously";
1358. 	static NEARDATA const char addluck[] = " gratefully";
1359. 
1360. 	Strcpy(buf,Monnam(mon));
1361. 	mon->mpeaceful = 1;
1362. 	mon->mavenge = 0;
1363. 
1364. 	/* object properly identified */
1365. 	if(obj->dknown && objects[obj->otyp].oc_name_known) {
1366. 		if(is_gem) {
1367. 			if(is_buddy) {
1368. 				Strcat(buf,addluck);
1369. 				change_luck(5);
1370. 			} else {
1371. 				Strcat(buf,maybeluck);
1372. 				change_luck(rn2(7)-3);
1373. 			}
1374. 		} else {
1375. 			Strcat(buf,nogood);
1376. 			goto nopick;
1377. 		}
1378. 	/* making guesses */
1379. 	} else if(obj->onamelth || objects[obj->otyp].oc_uname) {
1380. 		if(is_gem) {
1381. 			if(is_buddy) {
1382. 				Strcat(buf,addluck);
1383. 				change_luck(2);
1384. 			} else {
1385. 				Strcat(buf,maybeluck);
1386. 				change_luck(rn2(3)-1);
1387. 			}
1388. 		} else {
1389. 			Strcat(buf,nogood);
1390. 			goto nopick;
1391. 		}
1392. 	/* value completely unknown to @ */
1393. 	} else {
1394. 		if(is_gem) {
1395. 			if(is_buddy) {
1396. 				Strcat(buf,addluck);
1397. 				change_luck(1);
1398. 			} else {
1399. 				Strcat(buf,maybeluck);
1400. 				change_luck(rn2(3)-1);
1401. 			}
1402. 		} else {
1403. 			Strcat(buf,noluck);
1404. 		}
1405. 	}
1406. 	Strcat(buf,acceptgift);
1407. 	if(*u.ushops) check_shop_obj(obj, mon->mx, mon->my, TRUE);
1408. 	(void) mpickobj(mon, obj);	/* may merge and free obj */
1409. 	ret = 1;
1410. 
1411. nopick:
1412. 	if(!Blind) pline("%s", buf);
1413. 	if (!tele_restrict(mon)) rloc(mon);
1414. 	return(ret);
1415. }
1416. 
1417. /*
1418.  * Comments about the restructuring of the old breaks() routine.
1419.  *
1420.  * There are now three distinct phases to object breaking:
1421.  *     breaktest() - which makes the check/decision about whether the
1422.  *                   object is going to break.
1423.  *     breakmsg()  - which outputs a message about the breakage,
1424.  *                   appropriate for that particular object. Should
1425.  *                   only be called after a positve breaktest().
1426.  *                   on the object and, if it going to be called,
1427.  *                   it must be called before calling breakobj().
1428.  *                   Calling breakmsg() is optional.
1429.  *     breakobj()  - which actually does the breakage and the side-effects
1430.  *                   of breaking that particular object. This should
1431.  *                   only be called after a positive breaktest() on the
1432.  *                   object.
1433.  *
1434.  * Each of the above routines is currently static to this source module.
1435.  * There are two routines callable from outside this source module which
1436.  * perform the routines above in the correct sequence.
1437.  *
1438.  *   hero_breaks() - called when an object is to be broken as a result
1439.  *                   of something that the hero has done. (throwing it,
1440.  *                   kicking it, etc.)
1441.  *   breaks()      - called when an object is to be broken for some
1442.  *                   reason other than the hero doing something to it.
1443.  */
1444. 
1445. /*
1446.  * The hero causes breakage of an object (throwing, dropping it, etc.)
1447.  * Return 0 if the object didn't break, 1 if the object broke.
1448.  */
1449. int
1450. hero_breaks(obj, x, y, from_invent)
1451. struct obj *obj;
1452. xchar x, y;		/* object location (ox, oy may not be right) */
1453. boolean from_invent;	/* thrown or dropped by player; maybe on shop bill */
1454. {
1455. 	boolean in_view = !Blind;
1456. 	if (!breaktest(obj)) return 0;
1457. 	breakmsg(obj, in_view);
1458. 	breakobj(obj, x, y, TRUE, from_invent);
1459. 	return 1;
1460. }
1461. 
1462. /*
1463.  * The object is going to break for a reason other than the hero doing
1464.  * something to it.
1465.  * Return 0 if the object doesn't break, 1 if the object broke.
1466.  */
1467. int
1468. breaks(obj, x, y)
1469. struct obj *obj;
1470. xchar x, y;		/* object location (ox, oy may not be right) */
1471. {
1472. 	boolean in_view = Blind ? FALSE : cansee(x, y);
1473. 
1474. 	if (!breaktest(obj)) return 0;
1475. 	breakmsg(obj, in_view);
1476. 	breakobj(obj, x, y, FALSE, FALSE);
1477. 	return 1;
1478. }
1479. 
1480. /*
1481.  * Unconditionally break an object. Assumes all resistance checks
1482.  * and break messages have been delivered prior to getting here.
1483.  * This routine assumes the cause is the hero if heros_fault is TRUE.
1484.  *
1485.  */
1486. STATIC_OVL void
1487. breakobj(obj, x, y, heros_fault, from_invent)
1488. struct obj *obj;
1489. xchar x, y;		/* object location (ox, oy may not be right) */
1490. boolean heros_fault;
1491. boolean from_invent;
1492. {
1493. 	switch (obj->oclass == POTION_CLASS ? POT_WATER : obj->otyp) {
1494. 		case MIRROR:
1495. 			if (heros_fault)
1496. 			    change_luck(-2);
1497. 			break;
1498. 		case POT_WATER:		/* really, all potions */
1499. 			if (obj->otyp == POT_OIL && obj->lamplit) {
1500. 			    splatter_burning_oil(x,y);
1501. 			} else if (distu(x,y) <= 2) {
1502. 			    if (!breathless(youmonst.data) || haseyes(youmonst.data)) {
1503. 				if (obj->otyp != POT_WATER) {
1504. 					if (!breathless(youmonst.data))
1505. 			    		     /* [what about "familiar odor" when known?] */
1506. 					    You("smell a peculiar odor...");
1507. 					else {
1508. 					    int numeyes = eyecount(youmonst.data);
1509. 					    Your("%s water%s.",
1510. 						 (numeyes == 1) ? body_part(EYE) :
1511. 							makeplural(body_part(EYE)),
1512. 						 (numeyes == 1) ? "s" : "");
1513. 					}
1514. 				}
1515. 				potionbreathe(obj);
1516. 			    }
1517. 			}
1518. 			/* monster breathing isn't handled... [yet?] */
1519. 			break;
1520. 		case EGG:
1521. 			/* breaking your own eggs is bad luck */
1522. 			if (heros_fault && obj->spe && obj->corpsenm >= LOW_PM)
1523. 			    change_luck((schar) -min(obj->quan, 5L));
1524. 			break;
1525. 	}
1526. 	if (heros_fault) {
1527. 	    if (from_invent) {
1528. 		if (*u.ushops)
1529. 			check_shop_obj(obj, x, y, TRUE);
1530. 	    } else if (!obj->no_charge && costly_spot(x, y)) {
1531. 		/* it is assumed that the obj is a floor-object */
1532. 		char *o_shop = in_rooms(x, y, SHOPBASE);
1533. 		struct monst *shkp = shop_keeper(*o_shop);
1534. 
1535. 		if (shkp) {		/* (implies *o_shop != '\0') */
1536. 		    static NEARDATA long lastmovetime = 0L;
1537. 		    static NEARDATA boolean peaceful_shk = FALSE;
1538. 		    /*  We want to base shk actions on her peacefulness
1539. 			at start of this turn, so that "simultaneous"
1540. 			multiple breakage isn't drastically worse than
1541. 			single breakage.  (ought to be done via ESHK)  */
1542. 		    if (moves != lastmovetime)
1543. 			peaceful_shk = shkp->mpeaceful;
1544. 		    if (stolen_value(obj, x, y, peaceful_shk, FALSE) > 0L &&
1545. 			(*o_shop != u.ushops[0] || !inside_shop(u.ux, u.uy)) &&
1546. 			moves != lastmovetime) make_angry_shk(shkp, x, y);
1547. 		    lastmovetime = moves;
1548. 		}
1549. 	    }
1550. 	}
1551. 	delobj(obj);
1552. }
1553. 
1554. /*
1555.  * Check to see if obj is going to break, but don't actually break it.
1556.  * Return 0 if the object isn't going to break, 1 if it is.
1557.  */
1558. boolean
1559. breaktest(obj)
1560. struct obj *obj;
1561. {
1562. 	if (obj_resists(obj, 1, 99)) return 0;
1563. 	if (objects[obj->otyp].oc_material == GLASS && !obj->oartifact &&
1564. 		obj->oclass != GEM_CLASS)
1565. 	    return 1;
1566. 	switch (obj->oclass == POTION_CLASS ? POT_WATER : obj->otyp) {
1567. #ifdef TOURIST
1568. 		case EXPENSIVE_CAMERA:
1569. #endif
1570. 		case POT_WATER:		/* really, all potions */
1571. 		case EGG:
1572. 		case CREAM_PIE:
1573. 		case ACID_VENOM:
1574. 		case BLINDING_VENOM:
1575. 			return 1;
1576. 		default:
1577. 			return 0;
1578. 	}
1579. }
1580. 
1581. STATIC_OVL void
1582. breakmsg(obj, in_view)
1583. struct obj *obj;
1584. boolean in_view;
1585. {
1586. 	const char *to_pieces;
1587. 
1588. 	to_pieces = "";
1589. 	switch (obj->oclass == POTION_CLASS ? POT_WATER : obj->otyp) {
1590. 		default: /* glass or crystal wand */
1591. 		    if (obj->oclass != WAND_CLASS)
1592. 			impossible("breaking odd object?");
1593. 		case CRYSTAL_PLATE_MAIL:
1594. 		case LENSES:
1595. 		case MIRROR:
1596. 		case CRYSTAL_BALL:
1597. #ifdef TOURIST
1598. 		case EXPENSIVE_CAMERA:
1599. #endif
1600. 			to_pieces = " into a thousand pieces";
1601. 			/*FALLTHRU*/
1602. 		case POT_WATER:		/* really, all potions */
1603. 			if (!in_view)
1604. 			    You_hear("%s shatter!", something);
1605. 			else
1606. 			    pline("%s shatter%s%s!", Doname2(obj),
1607. 				(obj->quan==1) ? "s" : "", to_pieces);
1608. 			break;
1609. 		case EGG:
1610. 			pline("Splat!");
1611. 			break;
1612. 		case CREAM_PIE:
1613. 			if (in_view) pline("What a mess!");
1614. 			break;
1615. 		case ACID_VENOM:
1616. 		case BLINDING_VENOM:
1617. 			pline("Splash!");
1618. 			break;
1619. 	}
1620. }
1621. 
1622. /*
1623.  *  Note that the gold object is *not* attached to the fobj chain.
1624.  */
1625. STATIC_OVL int
1626. throw_gold(obj)
1627. struct obj *obj;
1628. {
1629. 	int range, odx, ody;
1630. #ifndef GOLDOBJ
1631. 	long zorks = obj->quan;
1632. #endif
1633. 	register struct monst *mon;
1634. 
1635. 	if(!u.dx && !u.dy && !u.dz) {
1636. 		You("cannot throw gold at yourself.");
1637. 		return(0);
1638. 	}
1639. #ifdef GOLDOBJ
1640.         freeinv(obj);
1641. #endif
1642. 	if(u.uswallow) {
1643. 		pline(is_animal(u.ustuck->data) ?
1644. 			"%s in the %s's entrails." : "%s into %s.",
1645. #ifndef GOLDOBJ
1646. 			"The gold disappears", mon_nam(u.ustuck));
1647. 		u.ustuck->mgold += zorks;
1648. 		dealloc_obj(obj);
1649. #else
1650. 			"The money disappears", mon_nam(u.ustuck));
1651. 		add_to_minv(u.ustuck, obj);
1652. #endif
1653. 		return(1);
1654. 	}
1655. 
1656. 	if(u.dz) {
1657. 		if (u.dz < 0 && !Is_airlevel(&u.uz) &&
1658. 					!Underwater && !Is_waterlevel(&u.uz)) {
1659. 	pline_The("gold hits the %s, then falls back on top of your %s.",
1660. 		    ceiling(u.ux,u.uy), body_part(HEAD));
1661. 		    /* some self damage? */
1662. 		    if(uarmh) pline("Fortunately, you are wearing a helmet!");
1663. 		}
1664. 		bhitpos.x = u.ux;
1665. 		bhitpos.y = u.uy;
1666. 	} else {
1667. 		/* consistent with range for normal objects */
1668. 		range = (int)((ACURRSTR)/2 - obj->owt/40);
1669. 
1670. 		/* see if the gold has a place to move into */
1671. 		odx = u.ux + u.dx;
1672. 		ody = u.uy + u.dy;
1673. 		if(!ZAP_POS(levl[odx][ody].typ) || closed_door(odx, ody)) {
1674. 			bhitpos.x = u.ux;
1675. 			bhitpos.y = u.uy;
1676. 		} else {
1677. 			mon = bhit(u.dx, u.dy, range, THROWN_WEAPON,
1678. 				   (int FDECL((*),(MONST_P,OBJ_P)))0,
1679. 				   (int FDECL((*),(OBJ_P,OBJ_P)))0,
1680. 				   obj);
1681. 			if(mon) {
1682. 			    if (ghitm(mon, obj))	/* was it caught? */
1683. 				return 1;
1684. 			} else {
1685. 			    if(ship_object(obj, bhitpos.x, bhitpos.y, FALSE))
1686. 				return 1;
1687. 			}
1688. 		}
1689. 	}
1690. 
1691. 	if(flooreffects(obj,bhitpos.x,bhitpos.y,"fall")) return(1);
1692. 	if(u.dz > 0)
1693. 		pline_The("gold hits the %s.", surface(bhitpos.x,bhitpos.y));
1694. 	place_object(obj,bhitpos.x,bhitpos.y);
1695. 	if(*u.ushops) sellobj(obj, bhitpos.x, bhitpos.y);
1696. 	stackobj(obj);
1697. 	newsym(bhitpos.x,bhitpos.y);
1698. 	return(1);
1699. }
1700. 
1701. /*dothrow.c*/