Source:NetHack 5.0.0/src/worn.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to worn.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/worn.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	worn.c	$NHDT-Date: 1770949988 2026/02/12 18:33:08 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.119 $ */
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. #include "hack.h"
7. 
8. staticfn void m_lose_armor(struct monst *, struct obj *, boolean) NONNULLPTRS;
9. staticfn void clear_bypass(struct obj *) NO_NNARGS;
10. staticfn void m_dowear_type(struct monst *, long, boolean, boolean)
11.                                                                   NONNULLARG1;
12. staticfn int extra_pref(struct monst *, struct obj *) NONNULLARG1;
13. 
14. static const struct worn {
15.     long w_mask;
16.     struct obj **w_obj;
17.     const char *w_what; /* for failing sanity check's feedback */
18. } worn[] = { { W_ARM, &uarm, "suit" },
19.              { W_ARMC, &uarmc, "cloak" },
20.              { W_ARMH, &uarmh, "helmet" },
21.              { W_ARMS, &uarms, "shield" },
22.              { W_ARMG, &uarmg, "gloves" },
23.              { W_ARMF, &uarmf, "boots" },
24.              { W_ARMU, &uarmu, "shirt" },
25.              { W_RINGL, &uleft, "left ring" },
26.              { W_RINGR, &uright, "right ring" },
27.              { W_WEP, &uwep, "weapon" },
28.              { W_SWAPWEP, &uswapwep, "alternate weapon" },
29.              { W_QUIVER, &uquiver, "quiver" },
30.              { W_AMUL, &uamul, "amulet" },
31.              { W_TOOL, &ublindf, "facewear" }, /* blindfold|towel|lenses */
32.              { W_BALL, &uball, "chained ball" },
33.              { W_CHAIN, &uchain, "attached chain" },
34.              { 0, 0, (char *) 0 }
35. };
36. 
37. /* This only allows for one blocking item per property */
38. #define w_blocks(o, m) \
39.     ((o->otyp == MUMMY_WRAPPING && ((m) & W_ARMC) != 0L) ? INVIS        \
40.      : (o->otyp == CORNUTHAUM && ((m) & W_ARMH) != 0L                   \
41.         && !Role_if(PM_WIZARD)) ? CLAIRVOYANT                           \
42.        : (is_art(o, ART_EYES_OF_THE_OVERWORLD)                          \
43.           && ((m) & W_TOOL) != 0L) ? BLINDED                            \
44.          : 0)
45. /* note: monsters don't have clairvoyance, so dependency on hero's role here
46.    has no significant effect on their use of w_blocks() */
47. 
48. /* calc the range of hero's unblind telepathy */
49. void
50. recalc_telepat_range(void)
51. {
52.     const struct worn *wp;
53.     int nobjs = 0;
54. 
55.     for (wp = worn; wp->w_mask; wp++) {
56.         struct obj *oobj = *(wp->w_obj);
57. 
58.         if (oobj && objects[oobj->otyp].oc_oprop == TELEPAT)
59.             nobjs++;
60.     }
61.     /* count all artifacts with SPFX_ESP as one */
62.     if (ETelepat & W_ART)
63.         nobjs++;
64. 
65.     if (nobjs)
66.         u.unblind_telepat_range = (BOLT_LIM * BOLT_LIM) * nobjs;
67.     else
68.         u.unblind_telepat_range = -1;
69. }
70. 
71. /* Updated to use the extrinsic and blocked fields. */
72. void
73. setworn(struct obj *obj, long mask)
74. {
75.     const struct worn *wp;
76.     struct obj *oobj;
77.     int p;
78. 
79.     if ((mask & (W_ARM | I_SPECIAL)) == (W_ARM | I_SPECIAL)) {
80.         /* restoring saved game; no properties are conferred via skin */
81.         uskin = obj;
82.         /* assert( !uarm ); */
83.     } else {
84.         for (wp = worn; wp->w_mask; wp++) {
85.             if (wp->w_mask & mask) {
86.                 oobj = *(wp->w_obj);
87.                 if (oobj && !(oobj->owornmask & wp->w_mask))
88.                     impossible("Setworn: mask=0x%08lx.", wp->w_mask);
89.                 if (oobj) {
90.                     if (u.twoweap && (oobj->owornmask & (W_WEP | W_SWAPWEP)))
91.                         set_twoweap(FALSE); /* u.twoweap = FALSE */
92.                     oobj->owornmask &= ~wp->w_mask;
93.                     if (wp->w_mask & ~(W_SWAPWEP | W_QUIVER)) {
94.                         /* leave as "x = x <op> y", here and below, for broken
95.                          * compilers */
96.                         p = objects[oobj->otyp].oc_oprop;
97.                         u.uprops[p].extrinsic =
98.                             u.uprops[p].extrinsic & ~wp->w_mask;
99.                         /* if the hero removed an extrinsic-granting item,
100.                            nearby monsters will notice and attempt attacks of
101.                            that type again */
102.                         monstunseesu_prop(p);
103.                         if ((p = w_blocks(oobj, mask)) != 0)
104.                             u.uprops[p].blocked &= ~wp->w_mask;
105.                         if (oobj->oartifact)
106.                             set_artifact_intrinsic(oobj, 0, mask);
107.                     }
108.                     /* in case wearing or removal is in progress or removal
109.                        is pending (via 'A' command for multiple items) */
110.                     cancel_doff(oobj, wp->w_mask);
111.                 }
112.                 *(wp->w_obj) = obj;
113.                 if (obj) {
114.                     obj->owornmask |= wp->w_mask;
115.                     /* Prevent getting/blocking intrinsics from wielding
116.                      * potions, through the quiver, etc.
117.                      * Allow weapon-tools, too.
118.                      * wp_mask should be same as mask at this point.
119.                      */
120.                     if (wp->w_mask & ~(W_SWAPWEP | W_QUIVER)) {
121.                         if (obj->oclass == WEAPON_CLASS || is_weptool(obj)
122.                             || mask != W_WEP) {
123.                             p = objects[obj->otyp].oc_oprop;
124.                             u.uprops[p].extrinsic =
125.                                 u.uprops[p].extrinsic | wp->w_mask;
126.                             if ((p = w_blocks(obj, mask)) != 0)
127.                                 u.uprops[p].blocked |= wp->w_mask;
128.                         }
129.                         if (obj->oartifact)
130.                             set_artifact_intrinsic(obj, 1, mask);
131.                     }
132.                 }
133.             }
134.         }
135.         if (obj && (obj->owornmask & W_ARMOR) != 0L)
136.             u.uroleplay.nudist = FALSE;
137.         /* tux -> tuxedo -> "monkey suit" -> monk's suit */
138.         iflags.tux_penalty = (uarm && Role_if(PM_MONK) && gu.urole.spelarmr);
139.     }
140.     if ((flags.weaponstatus && (mask & W_WEP) != 0L)
141.         || (flags.armorstatus && (mask & W_ARMOR) != 0L))
142.         disp.botl = TRUE;
143.     update_inventory();
144.     recalc_telepat_range();
145. }
146. 
147. /* called e.g. when obj is destroyed */
148. /* Updated to use the extrinsic and blocked fields. */
149. void
150. setnotworn(struct obj *obj)
151. {
152.     const struct worn *wp;
153.     int p;
154.     long unworn = 0L;
155. 
156.     if (!obj)
157.         return;
158.     if (u.twoweap && (obj == uwep || obj == uswapwep))
159.         set_twoweap(FALSE); /* u.twoweap = FALSE */
160.     for (wp = worn; wp->w_mask; wp++)
161.         if (obj == *(wp->w_obj)) {
162.             /* in case wearing or removal is in progress or removal
163.                is pending (via 'A' command for multiple items) */
164.             cancel_doff(obj, wp->w_mask);
165. 
166.             *(wp->w_obj) = (struct obj *) 0;
167.             unworn |= wp->w_mask;
168.             p = objects[obj->otyp].oc_oprop;
169.             u.uprops[p].extrinsic = u.uprops[p].extrinsic & ~wp->w_mask;
170.             monstunseesu_prop(p); /* remove this extrinsic from seenres */
171.             obj->owornmask &= ~wp->w_mask;
172.             if (obj->oartifact)
173.                 set_artifact_intrinsic(obj, 0, wp->w_mask);
174.             if ((p = w_blocks(obj, wp->w_mask)) != 0)
175.                 u.uprops[p].blocked &= ~wp->w_mask;
176.         }
177.     if (!uarm)
178.         iflags.tux_penalty = FALSE;
179.     if ((flags.weaponstatus && (unworn & W_WEP) != 0L)
180.         || (flags.armorstatus && (unworn & W_ARMOR) != 0L))
181.         disp.botl = TRUE;
182.     update_inventory();
183.     recalc_telepat_range();
184. }
185. 
186. /* called when saving with FREEING flag set has just discarded inventory */
187. void
188. allunworn(void)
189. {
190.     const struct worn *wp;
191. 
192.     u.twoweap = 0; /* uwep and uswapwep are going away */
193.     /* remove stale pointers; called after the objects have been freed
194.        (without first being unworn) while saving invent during game save;
195.        note: uball and uchain might not be freed yet but we clear them
196.        here anyway (savegamestate() and its callers deal with them) */
197.     for (wp = worn; wp->w_mask; wp++) {
198.         /* object is already gone so we don't/can't update is owornmask */
199.         *(wp->w_obj) = (struct obj *) 0;
200.     }
201. }
202. 
203. 
204. /* return item worn in slot indicated by wornmask; needed by poly_obj() */
205. struct obj *
206. wearmask_to_obj(long wornmask)
207. {
208.     const struct worn *wp;
209. 
210.     for (wp = worn; wp->w_mask; wp++)
211.         if (wp->w_mask & wornmask)
212.             return *wp->w_obj;
213.     return (struct obj *) 0;
214. }
215. 
216. /* convert an armor wornmask to corresponding category */
217. int
218. wornmask_to_armcat(long mask)
219. {
220.     int cat = 0;
221. 
222.     switch (mask & W_ARMOR) {
223.     case W_ARM:
224.         cat = ARM_SUIT;
225.         break;
226.     case W_ARMC:
227.         cat = ARM_CLOAK;
228.         break;
229.     case W_ARMH:
230.         cat = ARM_HELM;
231.         break;
232.     case W_ARMS:
233.         cat = ARM_SHIELD;
234.         break;
235.     case W_ARMG:
236.         cat = ARM_GLOVES;
237.         break;
238.     case W_ARMF:
239.         cat = ARM_BOOTS;
240.         break;
241.     case W_ARMU:
242.         cat = ARM_SHIRT;
243.         break;
244.     }
245.     return cat;
246. }
247. 
248. /* convert an armor category to corresponding wornmask */
249. long
250. armcat_to_wornmask(int cat)
251. {
252.     long mask = 0L;
253. 
254.     switch (cat) {
255.     case ARM_SUIT:
256.         mask = W_ARM;
257.         break;
258.     case ARM_CLOAK:
259.         mask = W_ARMC;
260.         break;
261.     case ARM_HELM:
262.         mask = W_ARMH;
263.         break;
264.     case ARM_SHIELD:
265.         mask = W_ARMS;
266.         break;
267.     case ARM_GLOVES:
268.         mask = W_ARMG;
269.         break;
270.     case ARM_BOOTS:
271.         mask = W_ARMF;
272.         break;
273.     case ARM_SHIRT:
274.         mask = W_ARMU;
275.         break;
276.     }
277.     return mask;
278. }
279. 
280. /* return a bitmask of the equipment slot(s) a given item might be worn in */
281. long
282. wearslot(struct obj *obj)
283. {
284.     int otyp = obj->otyp;
285.     /* practically any item can be wielded or quivered; it's up to
286.        our caller to handle such things--we assume "normal" usage */
287.     long res = 0L; /* default: can't be worn anywhere */
288. 
289.     switch (obj->oclass) {
290.     case AMULET_CLASS:
291.         res = W_AMUL; /* WORN_AMUL */
292.         break;
293.     case RING_CLASS:
294.         res = W_RINGL | W_RINGR; /* W_RING, BOTH_SIDES */
295.         break;
296.     case ARMOR_CLASS:
297.         switch (objects[otyp].oc_armcat) {
298.         case ARM_SUIT:
299.             res = W_ARM;
300.             break; /* WORN_ARMOR */
301.         case ARM_SHIELD:
302.             res = W_ARMS;
303.             break; /* WORN_SHIELD */
304.         case ARM_HELM:
305.             res = W_ARMH;
306.             break; /* WORN_HELMET */
307.         case ARM_GLOVES:
308.             res = W_ARMG;
309.             break; /* WORN_GLOVES */
310.         case ARM_BOOTS:
311.             res = W_ARMF;
312.             break; /* WORN_BOOTS */
313.         case ARM_CLOAK:
314.             res = W_ARMC;
315.             break; /* WORN_CLOAK */
316.         case ARM_SHIRT:
317.             res = W_ARMU;
318.             break; /* WORN_SHIRT */
319.         }
320.         break;
321.     case WEAPON_CLASS:
322.         res = W_WEP | W_SWAPWEP;
323.         if (objects[otyp].oc_merge)
324.             res |= W_QUIVER;
325.         break;
326.     case TOOL_CLASS:
327.         if (otyp == BLINDFOLD || otyp == TOWEL || otyp == LENSES)
328.             res = W_TOOL; /* WORN_BLINDF */
329.         else if (is_weptool(obj) || otyp == TIN_OPENER)
330.             res = W_WEP | W_SWAPWEP;
331.         else if (otyp == SADDLE)
332.             res = W_SADDLE;
333.         break;
334.     case FOOD_CLASS:
335.         if (obj->otyp == MEAT_RING)
336.             res = W_RINGL | W_RINGR;
337.         break;
338.     case GEM_CLASS:
339.         res = W_QUIVER;
340.         break;
341.     case BALL_CLASS:
342.         res = W_BALL;
343.         break;
344.     case CHAIN_CLASS:
345.         res = W_CHAIN;
346.         break;
347.     default:
348.         break;
349.     }
350.     return res;
351. }
352. 
353. /* for 'sanity_check' option, called by you_sanity_check() */
354. void
355. check_wornmask_slots(void)
356. {
357.     /* we'll skip ball and chain here--they warrant separate sanity check */
358. #define IGNORE_SLOTS (W_ART | W_ARTI | W_SADDLE | W_BALL| W_CHAIN)
359.     char whybuf[BUFSZ];
360.     const struct worn *wp;
361.     struct obj *o, *otmp;
362.     long m;
363. 
364.     for (wp = worn; wp->w_mask; wp++) {
365.         m = wp->w_mask;
366.         if ((m & IGNORE_SLOTS) != 0L && (m & ~IGNORE_SLOTS) == 0L)
367.             continue;
368.         if ((o = *wp->w_obj) != 0) {
369.             whybuf[0] = '\0';
370.             /* slot pointer (uarm, uwep, &c) is populated; check that object
371.                is in inventory and has the relevant owornmask bit set */
372.             for (otmp = gi.invent; otmp; otmp = otmp->nobj)
373.                 if (otmp == o)
374.                     break;
375.             if (!otmp)
376.                 Sprintf(whybuf, "%s (%s) not found in invent",
377.                         wp->w_what, fmt_ptr(o));
378.             else if ((o->owornmask & m) == 0L)
379.                 Sprintf(whybuf, "%s bit not set in owornmask [0x%08lx]",
380.                         wp->w_what, o->owornmask);
381.             else if ((o->owornmask & ~(m | IGNORE_SLOTS)) != 0L)
382.                 Sprintf(whybuf, "%s wrong bit set in owornmask [0x%08lx]",
383.                         wp->w_what, o->owornmask);
384.             if (whybuf[0])
385.                 impossible("Worn-slot insanity: %s.", whybuf);
386.         } /* o != NULL */
387. 
388.         /* check whether any item other than the one in the slot pointer
389.            claims to be worn/wielded in this slot; make this test whether
390.            'o' is Null or not; [sanity_check_worn(mkobj.c) for object by
391.            object checking will most likely have already caught this] */
392.         for (otmp = gi.invent; otmp; otmp = otmp->nobj) {
393.             if (otmp != o && (otmp->owornmask & m) != 0L
394.                 /* embedded scales owornmask is W_ARM|I_SPECIAL so would
395.                    give a false complaint about item other than uarm having
396.                    W_ARM bit set if we didn't screen it out here */
397.                 && (m != W_ARM || otmp != uskin
398.                     || (otmp->owornmask & I_SPECIAL) == 0L)) {
399.                 Sprintf(whybuf, "%s [0x%08lx] has %s mask 0x%08lx bit set",
400.                         simpleonames(otmp), otmp->owornmask, wp->w_what, m);
401.                 impossible("Worn-slot insanity: %s.", whybuf);
402.             }
403.         }
404.     } /* for wp in worn[] */
405. 
406. #ifdef EXTRA_SANITY_CHECKS
407.     if (uskin) {
408.         const char *what = "embedded scales";
409. 
410.         o = uskin;
411.         m = W_ARM | I_SPECIAL;
412.         whybuf[0] = '\0';
413.         for (otmp = gi.invent; otmp; otmp = otmp->nobj)
414.             if (otmp == o)
415.                 break;
416.         if (!otmp)
417.             Sprintf(whybuf, "%s (%s) not found in invent",
418.                     what, fmt_ptr(o));
419.         else if ((o->owornmask & m) != m)
420.             Sprintf(whybuf, "%s bits not set in owornmask [0x%08lx]",
421.                     what, o->owornmask);
422.         else if ((o->owornmask & ~(m | IGNORE_SLOTS)) != 0L)
423.             Sprintf(whybuf, "%s wrong bit set in owornmask [0x%08lx]",
424.                     what, o->owornmask);
425.         else if (!Is_dragon_scales(o))
426.             Sprintf(whybuf, "%s (%s) %s not dragon scales",
427.                     what, simpleonames(o), otense(o, "are"));
428.         else if (Dragon_scales_to_pm(o) != &mons[u.umonnum])
429.             Sprintf(whybuf, "%s, hero is not %s",
430.                     what, an(mons[u.umonnum].pmnames[NEUTRAL]));
431.         if (whybuf[0])
432.             impossible("Worn-slot insanity: %s.", whybuf);
433.     } /* uskin */
434. #endif /* EXTRA_SANITY_CHECKS */
435. 
436. #ifdef EXTRA_SANITY_CHECKS
437.     /* dual wielding: not a slot but lots of things to verify */
438.     if (u.twoweap) {
439.         const char *why = NULL;
440. 
441.         if (!uwep || !uswapwep) {
442.             Sprintf(whybuf, "without %s%s%s",
443.                     !uwep ? "uwep" : "",
444.                     (!uwep && !uswapwep) ? " and without " : "",
445.                     !uswapwep ? "uswapwep" : "");
446.             why = whybuf;
447.         } else if (uarms)
448.             why = "while wearing shield";
449.         else if (uwep->oclass != WEAPON_CLASS && !is_weptool(uwep))
450.             why = "uwep is not a weapon";
451.         else if (is_launcher(uwep) || is_ammo(uwep) || is_missile(uwep))
452.             why = "uwep is not a melee weapon";
453.         else if (bimanual(uwep))
454.             why = "uwep is two-handed";
455.         else if (uswapwep->oclass != WEAPON_CLASS && !is_weptool(uswapwep))
456.             why = "uswapwep is not a weapon";
457.         else if (is_launcher(uswapwep) || is_ammo(uswapwep)
458.                  || is_missile(uswapwep))
459.             why = "uswapwep is not a melee weapon";
460.         else if (bimanual(uswapwep))
461.             why = "uswapwep is two-handed";
462.         else if (!could_twoweap(gy.youmonst.data))
463.             why = "without two weapon attacks";
464. 
465.         if (why)
466.             impossible("Two-weapon insanity: %s.", why);
467.     }
468. #endif /* EXTRA_SANITY_CHECKS */
469.     return;
470. #undef IGNORE_SLOTS
471. } /* check_wornmask_slots() */
472. 
473. void
474. mon_set_minvis(
475.     struct monst *mon,
476.     boolean cursed_potion)
477. {
478.     mon->perminvis = !cursed_potion ? 1 : 0;
479.     if (!mon->invis_blkd) {
480.         mon->minvis = mon->perminvis;
481.         newsym(mon->mx, mon->my); /* make it disappear */
482.         if (mon->wormno)
483.             see_wsegs(mon); /* and any tail too */
484.     }
485. }
486. 
487. void
488. mon_adjust_speed(
489.     struct monst *mon,
490.     int adjust,        /* positive => increase speed, negative => decrease */
491.     struct obj *obj)   /* item to make known if effect can be seen */
492. {
493.     struct obj *otmp;
494.     boolean give_msg = !gi.in_mklev, petrify = FALSE;
495.     unsigned int oldspeed = mon->mspeed;
496. 
497.     switch (adjust) {
498.     case 2:
499.         mon->permspeed = MFAST;
500.         give_msg = FALSE; /* special-case monster creation */
501.         break;
502.     case 1:
503.         if (mon->permspeed == MSLOW)
504.             mon->permspeed = 0;
505.         else
506.             mon->permspeed = MFAST;
507.         break;
508.     case 0: /* just check for worn speed boots */
509.         break;
510.     case -1:
511.         if (mon->permspeed == MFAST)
512.             mon->permspeed = 0;
513.         else
514.             mon->permspeed = MSLOW;
515.         break;
516.     case -2:
517.         mon->permspeed = MSLOW;
518.         give_msg = FALSE; /* (not currently used) */
519.         break;
520.     case -3: /* petrification */
521.         /* take away intrinsic speed but don't reduce normal speed */
522.         if (mon->permspeed == MFAST)
523.             mon->permspeed = 0;
524.         petrify = TRUE;
525.         break;
526.     case -4: /* green slime */
527.         if (mon->permspeed == MFAST)
528.             mon->permspeed = 0;
529.         give_msg = FALSE;
530.         break;
531.     }
532. 
533.     for (otmp = mon->minvent; otmp; otmp = otmp->nobj)
534.         if (otmp->owornmask && objects[otmp->otyp].oc_oprop == FAST)
535.             break;
536.     if (otmp) /* speed boots */
537.         mon->mspeed = MFAST;
538.     else
539.         mon->mspeed = mon->permspeed;
540. 
541.     /* no message if monster is immobile (temp or perm) or unseen */
542.     if (give_msg && (mon->mspeed != oldspeed || petrify) && mon->data->mmove
543.         && !(mon->mfrozen || mon->msleeping) && canseemon(mon)) {
544.         /* fast to slow (skipping intermediate state) or vice versa */
545.         const char *howmuch =
546.             (mon->mspeed + oldspeed == MFAST + MSLOW) ? "much " : "";
547. 
548.         if (petrify) {
549.             /* mimic the player's petrification countdown; "slowing down"
550.                even if fast movement rate retained via worn speed boots */
551.             if (flags.verbose)
552.                 pline_mon(mon, "%s is slowing down.", Monnam(mon));
553.         } else if (adjust > 0 || mon->mspeed == MFAST)
554.             pline_mon(mon, "%s is suddenly moving %sfaster.",
555.                       Monnam(mon), howmuch);
556.         else
557.             pline_mon(mon, "%s seems to be moving %sslower.",
558.                       Monnam(mon), howmuch);
559. 
560.         /* might discover an object if we see the speed change happen */
561.         if (obj != 0)
562.             learnwand(obj);
563.     }
564. }
565. 
566. /* alchemy smock confers two properties, poison and acid resistance
567.    but objects[ALCHEMY_SMOCK].oc_oprop can only describe one of them;
568.    if it is poison resistance, alternate property is acid resistance;
569.    if someone changes it to acid resistance, alt becomes poison resist;
570.    if someone changes it to hallucination resistance, all bets are off
571.    [TODO: handle alternate properties conferred by dragon scales/mail] */
572. #define altprop(o) \
573.     (((o)->otyp == ALCHEMY_SMOCK)                               \
574.      ? (POISON_RES + ACID_RES - objects[(o)->otyp].oc_oprop)    \
575.      : 0)
576. 
577. /* armor put on or taken off; might be magical variety */
578. void
579. update_mon_extrinsics(
580.     struct monst *mon,
581.     struct obj *obj,   /* armor being worn or taken off */
582.     boolean on,
583.     boolean silently)
584. {
585.     int unseen;
586.     uchar mask;
587.     struct obj *otmp;
588.     int which = (int) objects[obj->otyp].oc_oprop,
589.         altwhich = altprop(obj);
590. 
591.     unseen = !canseemon(mon);
592.     if (!which && !altwhich)
593.         goto maybe_blocks;
594. 
595.  again:
596.     if (on) {
597.         switch (which) {
598.         case INVIS:
599.             mon->minvis = !mon->invis_blkd;
600.             break;
601.         case FAST: {
602.             boolean save_in_mklev = gi.in_mklev;
603.             if (silently)
604.                 gi.in_mklev = TRUE;
605.             mon_adjust_speed(mon, 0, obj);
606.             gi.in_mklev = save_in_mklev;
607.             break;
608.         }
609.         /* properties handled elsewhere */
610.         case ANTIMAGIC:
611.         case REFLECTING:
612.         case PROTECTION:
613.             break;
614.         /* properties which have no effect for monsters */
615.         case CLAIRVOYANT:
616.         case STEALTH:
617.         case TELEPAT:
618.             break;
619.         /* properties which should have an effect but aren't implemented */
620.         case LEVITATION:
621.         case FLYING:
622.         case WWALKING:
623.             break;
624.         /* properties which maybe should have an effect but don't */
625.         case DISPLACED:
626.         case FUMBLING:
627.         case JUMPING:
628.             break;
629.         default:
630.             mon->mextrinsics |= (unsigned short) res_to_mr(which);
631.             break;
632.         }
633.     } else { /* off */
634.         switch (which) {
635.         case INVIS:
636.             mon->minvis = mon->perminvis;
637.             break;
638.         case FAST: {
639.             boolean save_in_mklev = gi.in_mklev;
640.             if (silently)
641.                 gi.in_mklev = TRUE;
642.             mon_adjust_speed(mon, 0, obj);
643.             gi.in_mklev = save_in_mklev;
644.             break;
645.         }
646.         case FIRE_RES:
647.         case COLD_RES:
648.         case SLEEP_RES:
649.         case DISINT_RES:
650.         case SHOCK_RES:
651.         case POISON_RES:
652.         case ACID_RES:
653.         case STONE_RES:
654.             /*
655.              * Update monster's extrinsics (for worn objects only;
656.              * 'obj' itself might still be worn or already unworn).
657.              *
658.              * If an alchemy smock is being taken off, this code will
659.              * be run twice (via 'goto again') and other worn gear
660.              * gets tested for conferring poison resistance on the
661.              * first pass and acid resistance on the second.
662.              *
663.              * If some other item is being taken off, there will be
664.              * only one pass but a worn alchemy smock will be an
665.              * alternate source for either of those two resistances.
666.              */
667.             mask = res_to_mr(which);
668.             for (otmp = mon->minvent; otmp; otmp = otmp->nobj) {
669.                 if (otmp == obj || !otmp->owornmask)
670.                     continue;
671.                 if ((int) objects[otmp->otyp].oc_oprop == which)
672.                     break;
673.                 /* check whether 'otmp' confers target property as an extra
674.                    one rather than as the one specified for it in objects[] */
675.                 if (altprop(otmp) == which)
676.                     break;
677.             }
678.             if (!otmp)
679.                 mon->mextrinsics &= ~((unsigned short) mask);
680.             break;
681.         default:
682.             break;
683.         }
684.     }
685. 
686.     /* worn alchemy smock/apron confers both poison resistance and acid
687.        resistance to the hero so do likewise for monster who wears one */
688.     if (altwhich && which != altwhich) {
689.         which = altwhich;
690.         goto again;
691.     }
692. 
693.  maybe_blocks:
694.     /* obj->owornmask has been cleared by this point, so we can't use it.
695.        However, since monsters don't wield armor, we don't have to guard
696.        against that and can get away with a blanket worn-mask value. */
697.     switch (w_blocks(obj, ~0L)) {
698.     case INVIS:
699.         mon->invis_blkd = on ? 1 : 0;
700.         mon->minvis = on ? 0 : mon->perminvis;
701.         break;
702.     default:
703.         break;
704.     }
705. 
706.     if (!on && mon == u.usteed && obj->otyp == SADDLE)
707.         dismount_steed(DISMOUNT_FELL);
708. 
709.     /* if couldn't see it but now can, or vice versa, update display */
710.     if (!silently && (unseen ^ !canseemon(mon)))
711.         newsym(mon->mx, mon->my);
712. }
713. 
714. #undef altprop
715. 
716. int
717. find_mac(struct monst *mon)
718. {
719.     struct obj *obj;
720.     int base = mon->data->ac;
721.     long mwflags = mon->misc_worn_check;
722. 
723.     for (obj = mon->minvent; obj; obj = obj->nobj) {
724.         if (obj->owornmask & mwflags) {
725.             if (obj->otyp == AMULET_OF_GUARDING)
726.                 base -= 2; /* fixed amount, not impacted by erosion */
727.             else
728.                 base -= ARM_BONUS(obj);
729.             /* since ARM_BONUS is positive, subtracting it increases AC */
730.         }
731.     }
732.     /* same cap as for hero [find_ac(do_wear.c)] */
733.     if (abs(base) > AC_MAX)
734.         base = sgn(base) * AC_MAX;
735.     return base;
736. }
737. 
738. /*
739.  * weapons are handled separately;
740.  * rings and eyewear aren't used by monsters
741.  */
742. 
743. /* Wear the best object of each type that the monster has.  During creation,
744.  * the monster can put everything on at once; otherwise, wearing takes time.
745.  * This doesn't affect monster searching for objects--a monster may very well
746.  * search for objects it would not want to wear, because we don't want to
747.  * check which_armor() each round.
748.  *
749.  * We'll let monsters put on shirts and/or suits under worn cloaks, but
750.  * not shirts under worn suits.  This is somewhat arbitrary, but it's
751.  * too tedious to have them remove and later replace outer garments,
752.  * and preventing suits under cloaks makes it a little bit too easy for
753.  * players to influence what gets worn.  Putting on a shirt underneath
754.  * already worn body armor is too obviously buggy...
755.  */
756. void
757. m_dowear(struct monst *mon, boolean creation)
758. {
759.     boolean can_wear_armor;
760. 
761. #define RACE_EXCEPTION TRUE
762.     /* Note the restrictions here are the same as in dowear in do_wear.c
763.      * except for the additional restriction on intelligence.  (Players
764.      * are always intelligent, even if polymorphed).
765.      */
766.     if (verysmall(mon->data) || nohands(mon->data) || is_animal(mon->data))
767.         return;
768.     /* give mummies a chance to wear their wrappings
769.      * and let skeletons wear their initial armor */
770.     if (mindless(mon->data)
771.         && (!creation || (mon->data->mlet != S_MUMMY
772.                           && mon->data != &mons[PM_SKELETON])))
773.         return;
774. 
775.     m_dowear_type(mon, W_AMUL, creation, FALSE);
776.     can_wear_armor = !cantweararm(mon->data); /* for suit, cloak, shirt */
777.     /* can't put on shirt if already wearing suit */
778.     if (can_wear_armor && !(mon->misc_worn_check & W_ARM))
779.         m_dowear_type(mon, W_ARMU, creation, FALSE);
780.     /* WrappingAllowed() makes any size between small and huge eligible;
781.        treating small as a special case allows hobbits, gnomes, and
782.        kobolds to wear all cloaks; large and huge allows giants and such
783.        to wear mummy wrappings but not other cloaks */
784.     if (can_wear_armor || WrappingAllowed(mon->data))
785.         m_dowear_type(mon, W_ARMC, creation, FALSE);
786.     m_dowear_type(mon, W_ARMH, creation, FALSE);
787.     if (!MON_WEP(mon) || !bimanual(MON_WEP(mon)))
788.         m_dowear_type(mon, W_ARMS, creation, FALSE);
789.     m_dowear_type(mon, W_ARMG, creation, FALSE);
790.     if (!slithy(mon->data) && mon->data->mlet != S_CENTAUR)
791.         m_dowear_type(mon, W_ARMF, creation, FALSE);
792.     if (can_wear_armor)
793.         m_dowear_type(mon, W_ARM, creation, FALSE);
794.     else
795.         m_dowear_type(mon, W_ARM, creation, RACE_EXCEPTION);
796. }
797. 
798. staticfn void
799. m_dowear_type(
800.     struct monst *mon,
801.     long flag,               /* wornmask value */
802.     boolean creation,        /* no wear messages when mon is being created */
803.     boolean racialexception) /* small monsters that are allowed for player
804.                               * races (gnomes) can wear suits */
805. {
806.     struct obj *old, *best, *obj;
807.     long oldmask = 0L;
808.     int m_delay = 0;
809.     int sawmon = canseemon(mon), sawloc = cansee(mon->mx, mon->my);
810.     boolean autocurse;
811.     char nambuf[BUFSZ];
812. 
813.     if (mon->mfrozen)
814.         return; /* probably putting previous item on */
815. 
816.     /* Get a copy of monster's name before altering its visibility */
817.     Strcpy(nambuf, See_invisible ? Monnam(mon) : mon_nam(mon));
818. 
819.     old = which_armor(mon, flag);
820.     if (old && old->cursed)
821.         return;
822.     if (old && flag == W_AMUL && old->otyp != AMULET_OF_GUARDING)
823.         return; /* no amulet better than life-saving or reflection */
824.     best = old;
825. 
826.     for (obj = mon->minvent; obj; obj = obj->nobj) {
827.         switch (flag) {
828.         case W_AMUL:
829.             if (obj->oclass != AMULET_CLASS
830.                 || (obj->otyp != AMULET_OF_LIFE_SAVING
831.                     && obj->otyp != AMULET_OF_REFLECTION
832.                     && obj->otyp != AMULET_OF_GUARDING))
833.                 continue;
834.             /* for 'best' to be non-Null, it must be an amulet of guarding;
835.                life-saving and reflection don't get here due to early return
836.                and other amulets of guarding can't be any better */
837.             if (!best || obj->otyp != AMULET_OF_GUARDING) {
838.                 best = obj;
839.                 if (best->otyp != AMULET_OF_GUARDING)
840.                     goto outer_break; /* life-saving or reflection; use it */
841.             }
842.             continue; /* skip post-switch armor handling */
843.         case W_ARMU:
844.             if (!is_shirt(obj))
845.                 continue;
846.             break;
847.         case W_ARMC:
848.             if (!is_cloak(obj))
849.                 continue;
850.             /* mummy wrapping is only cloak allowed when bigger than human */
851.             if (mon->data->msize > MZ_HUMAN && obj->otyp != MUMMY_WRAPPING)
852.                 continue;
853.             /* avoid mummy wrapping if it will allow hero to see mon (unless
854.                this is a new mummy; an invisible one is feasible via ^G) */
855.             if (mon->minvis && w_blocks(obj, W_ARMC) == INVIS
856.                 && !See_invisible && !creation)
857.                 continue;
858.             break;
859.         case W_ARMH:
860.             if (!is_helmet(obj))
861.                 continue;
862.             /* changing alignment is not implemented for monsters;
863.                priests and minions could change alignment but wouldn't
864.                want to, so they reject helms of opposite alignment */
865.             if (obj->otyp == HELM_OF_OPPOSITE_ALIGNMENT
866.                 && (mon->ispriest || mon->isminion))
867.                 continue;
868.             /* (flimsy exception matches polyself handling) */
869.             if (has_horns(mon->data) && !is_flimsy(obj))
870.                 continue;
871.             break;
872.         case W_ARMS:
873.             if (!is_shield(obj))
874.                 continue;
875.             break;
876.         case W_ARMG:
877.             if (!is_gloves(obj))
878.                 continue;
879.             break;
880.         case W_ARMF:
881.             if (!is_boots(obj))
882.                 continue;
883.             break;
884.         case W_ARM:
885.             if (!is_suit(obj))
886.                 continue;
887.             if (racialexception && (racial_exception(mon, obj) < 1))
888.                 continue;
889.             break;
890.         }
891.         if (obj->owornmask)
892.             continue;
893.         /* I'd like to define a VISIBLE_ARM_BONUS which doesn't assume the
894.          * monster knows obj->spe, but if I did that, a monster would keep
895.          * switching forever between two -2 caps since when it took off one
896.          * it would forget spe and once again think the object is better
897.          * than what it already has.
898.          */
899.         if (best && (ARM_BONUS(best) + extra_pref(mon, best)
900.                      >= ARM_BONUS(obj) + extra_pref(mon, obj)))
901.             continue;
902.         best = obj;
903.     }
904.  outer_break:
905.     if (!best || best == old)
906.         return;
907. 
908.     /* same auto-cursing behavior as for hero */
909.     autocurse = ((best->otyp == HELM_OF_OPPOSITE_ALIGNMENT
910.                   || best->otyp == DUNCE_CAP) && !best->cursed);
911.     /* if wearing a cloak, account for the time spent removing
912.        and re-wearing it when putting on a suit or shirt */
913.     if ((flag == W_ARM || flag == W_ARMU) && (mon->misc_worn_check & W_ARMC))
914.         m_delay += 2;
915.     /* when upgrading a piece of armor, account for time spent
916.        taking off current one */
917.     if (old) {
918.         m_delay += objects[old->otyp].oc_delay;
919. 
920.         oldmask = old->owornmask; /* needed later by artifact_light() */
921.         old->owornmask = 0L; /* avoid doname() showing "(being worn)" */
922.     }
923. 
924.     if (!creation) {
925.         if (sawmon) {
926.             char buf[BUFSZ], oldarm[BUFSZ], newarm[BUFSZ + sizeof "another "];
927. 
928.             /* "<Mon> [removes <oldarm> and ]puts on <newarm>."
929.                uses accessory verbs for armor but we can live with that */
930.             if (old) {
931.                 Strcpy(oldarm, distant_name(old, doname));
932.                 Snprintf(buf, sizeof buf, " removes %s and", oldarm);
933.             } else {
934.                 buf[0] = oldarm[0] = '\0';
935.             }
936.             Strcpy(newarm, distant_name(best, doname));
937.             /* a monster will swap an item of the same type as the one it
938.                is replacing when the enchantment is better;
939.                if newarm and oldarm have identical descriptions, substitute
940.                "another <newarm>" for "a|an <newarm>" */
941.             if (!strcmpi(newarm, oldarm)) {
942.                 /* size of newarm[] has been overallocated to guarantee
943.                    enough room to insert "another " */
944.                 if (!strncmpi(newarm, "a ", 2))
945.                     (void) strsubst(newarm, "a ", "another ");
946.                 else if (!strncmpi(newarm, "an ", 3))
947.                     (void) strsubst(newarm, "an ", "another ");
948.                 newarm[BUFSZ - 1] = '\0';
949.             }
950.             pline_mon(mon, "%s%s puts on %s.", Monnam(mon), buf, newarm);
951.             if (autocurse)
952.                 pline("%s %s %s %s for a moment.", s_suffix(Monnam(mon)),
953.                       simpleonames(best), otense(best, "glow"),
954.                       hcolor(NH_BLACK));
955.         } /* can see it */
956.         m_delay += objects[best->otyp].oc_delay;
957.         mon->mfrozen = m_delay;
958.         if (mon->mfrozen)
959.             mon->mcanmove = 0;
960.     }
961.     if (old) {
962.         update_mon_extrinsics(mon, old, FALSE, creation);
963. 
964.         /* owornmask was cleared above but artifact_light() expects it */
965.         old->owornmask = oldmask;
966.         if (old->lamplit && artifact_light(old))
967.             end_burn(old, FALSE);
968.         old->owornmask = 0L;
969.     }
970.     mon->misc_worn_check |= flag;
971.     best->owornmask |= flag;
972.     if (autocurse)
973.         curse(best);
974.     if (artifact_light(best) && !best->lamplit) {
975.         begin_burn(best, FALSE);
976.         vision_recalc(1);
977.         if (!creation && best->lamplit && cansee(mon->mx, mon->my)) {
978.             const char *adesc = arti_light_description(best);
979. 
980.             if (sawmon) /* could already see monster */
981.                 pline("%s %s to shine %s.", Yname2(best),
982.                       otense(best, "begin"), adesc);
983.             else if (canseemon(mon)) /* didn't see it until new light */
984.                 pline("%s %s shining %s.", Yname2(best),
985.                       otense(best, "are"), adesc);
986.             else if (sawloc) /* saw location but not invisible monster */
987.                 pline("%s begins to shine %s.", Something, adesc);
988.             else /* didn't see location until new light */
989.                 pline("%s is shining %s.", Something, adesc);
990.         }
991.     }
992.     update_mon_extrinsics(mon, best, TRUE, creation);
993.     /* if couldn't see it but now can, or vice versa */
994.     if (!creation && (sawmon ^ canseemon(mon))) {
995.         if (mon->minvis && !See_invisible) {
996.             pline("Suddenly you cannot see %s.", nambuf);
997.             makeknown(best->otyp);
998.         /* } else if (!mon->minvis) {
999.          *     pline("%s suddenly appears!", Amonnam(mon)); */
1000.         }
1001.     }
1002. }
1003. #undef RACE_EXCEPTION
1004. 
1005. struct obj *
1006. which_armor(struct monst *mon, long flag)
1007. {
1008.     if (mon == &gy.youmonst) {
1009.         switch (flag) {
1010.         case W_ARM:
1011.             return uarm;
1012.         case W_ARMC:
1013.             return uarmc;
1014.         case W_ARMH:
1015.             return uarmh;
1016.         case W_ARMS:
1017.             return uarms;
1018.         case W_ARMG:
1019.             return uarmg;
1020.         case W_ARMF:
1021.             return uarmf;
1022.         case W_ARMU:
1023.             return uarmu;
1024.         default:
1025.             impossible("bad flag in which_armor");
1026.             return 0;
1027.         }
1028.     } else {
1029.         struct obj *obj;
1030. 
1031.         for (obj = mon->minvent; obj; obj = obj->nobj)
1032.             if (obj->owornmask & flag)
1033.                 return obj;
1034.         return (struct obj *) 0;
1035.     }
1036. }
1037. 
1038. /* remove an item of armor and then drop it */
1039. staticfn void
1040. m_lose_armor(
1041.     struct monst *mon,
1042.     struct obj *obj,
1043.     boolean polyspot)
1044. {
1045.     extract_from_minvent(mon, obj, TRUE, FALSE);
1046.     place_object(obj, mon->mx, mon->my);
1047.     if (polyspot)
1048.         bypass_obj(obj);
1049.     /* call stackobj() if we ever drop anything that can merge */
1050.     newsym(mon->mx, mon->my);
1051. }
1052. 
1053. /* clear bypass bits for an object chain, plus contents if applicable */
1054. staticfn void
1055. clear_bypass(struct obj *objchn)
1056. {
1057.     struct obj *o;
1058. 
1059.     for (o = objchn; o; o = o->nobj) {
1060.         o->bypass = 0;
1061.         if (Has_contents(o))
1062.             clear_bypass(o->cobj);
1063.     }
1064. }
1065. 
1066. /* all objects with their bypass bit set should now be reset to normal;
1067.    this can be a relatively expensive operation so is only called if
1068.    svc.context.bypasses is set */
1069. void
1070. clear_bypasses(void)
1071. {
1072.     struct monst *mtmp;
1073. 
1074.     /*
1075.      * 'Object' bypass is also used for one monster function:
1076.      * polymorph control of long worms.  Activated via setting
1077.      * svc.context.bypasses even if no specific object has been
1078.      * bypassed.
1079.      */
1080. 
1081.     clear_bypass(fobj);
1082.     clear_bypass(gi.invent);
1083.     clear_bypass(gm.migrating_objs);
1084.     clear_bypass(svl.level.buriedobjlist);
1085.     clear_bypass(gb.billobjs);
1086.     clear_bypass(go.objs_deleted);
1087.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
1088.         if (DEADMONSTER(mtmp))
1089.             continue;
1090.         clear_bypass(mtmp->minvent);
1091.         /* long worm created by polymorph has mon->mextra->mcorpsenm set
1092.            to PM_LONG_WORM to flag it as not being subject to further
1093.            polymorph (so polymorph zap won't hit monster to transform it
1094.            into a long worm, then hit that worm's tail and transform it
1095.            again on same zap); clearing mcorpsenm reverts worm to normal */
1096.         if (mtmp->data == &mons[PM_LONG_WORM] && has_mcorpsenm(mtmp))
1097.             MCORPSENM(mtmp) = NON_PM;
1098.     }
1099.     for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon) {
1100.         clear_bypass(mtmp->minvent);
1101.         /* no MCORPSENM(mtmp)==PM_LONG_WORM check here; long worms can't
1102.            be just created by polymorph and migrating at the same time */
1103.     }
1104.     /* this is a no-op since mydogs is only non-Null during level change or
1105.        final ascension and we aren't called at those times, but be thorough */
1106.     for (mtmp = gm.mydogs; mtmp; mtmp = mtmp->nmon)
1107.         clear_bypass(mtmp->minvent);
1108.     /* ball and chain can be "floating", not on any object chain (when
1109.        hero is swallowed by an engulfing monster, for instance) */
1110.     if (uball)
1111.         uball->bypass = 0;
1112.     if (uchain)
1113.         uchain->bypass = 0;
1114. 
1115.     svc.context.bypasses = FALSE;
1116. }
1117. 
1118. void
1119. bypass_obj(struct obj *obj)
1120. {
1121.     obj->bypass = 1;
1122.     svc.context.bypasses = TRUE;
1123. }
1124. 
1125. /* set or clear the bypass bit in a list of objects */
1126. void
1127. bypass_objlist(
1128.     struct obj *objchain,
1129.     boolean on) /* TRUE => set, FALSE => clear */
1130. {
1131.     if (on && objchain)
1132.         svc.context.bypasses = TRUE;
1133.     while (objchain) {
1134.         objchain->bypass = on ? 1 : 0;
1135.         objchain = objchain->nobj;
1136.     }
1137. }
1138. 
1139. /* return the first object without its bypass bit set; set that bit
1140.    before returning so that successive calls will find further objects */
1141. struct obj *
1142. nxt_unbypassed_obj(struct obj *objchain)
1143. {
1144.     while (objchain) {
1145.         if (!objchain->bypass) {
1146.             bypass_obj(objchain);
1147.             break;
1148.         }
1149.         objchain = objchain->nobj;
1150.     }
1151.     return objchain;
1152. }
1153. 
1154. /* like nxt_unbypassed_obj() but operates on sortloot_item array rather
1155.    than an object linked list; the array contains obj==Null terminator;
1156.    there's an added complication that the array may have stale pointers
1157.    for deleted objects (see Multiple-Drop case in askchain(invent.c)) */
1158. struct obj *
1159. nxt_unbypassed_loot(Loot *lootarray, struct obj *listhead)
1160. {
1161.     struct obj *o, *obj;
1162. 
1163.     while ((obj = lootarray->obj) != 0) {
1164.         for (o = listhead; o; o = o->nobj)
1165.             if (o == obj)
1166.                 break;
1167.         if (o && !obj->bypass) {
1168.             bypass_obj(obj);
1169.             break;
1170.         }
1171.         ++lootarray;
1172.     }
1173.     return obj;
1174. }
1175. 
1176. void
1177. mon_break_armor(struct monst *mon, boolean polyspot)
1178. {
1179.     struct obj *otmp;
1180.     struct permonst *mdat = mon->data;
1181.     boolean vis = cansee(mon->mx, mon->my),
1182.             handless_or_tiny = (nohands(mdat) || verysmall(mdat)),
1183.             noride = FALSE;
1184.     const char *pronoun = mhim(mon), *ppronoun = mhis(mon);
1185. 
1186.     if (breakarm(mdat)) {
1187.         if ((otmp = which_armor(mon, W_ARM)) != 0) {
1188.             if ((Is_dragon_scales(otmp) && mdat == Dragon_scales_to_pm(otmp))
1189.                 || (Is_dragon_mail(otmp) && mdat == Dragon_mail_to_pm(otmp))) {
1190.                 ; /* no message here;
1191.                      "the dragon merges with his scaly armor" is odd
1192.                      and the monster's previous form is already gone */
1193.             } else {
1194.                 Soundeffect(se_cracking_sound, 100);
1195.                 if (vis)
1196.                     pline_mon(mon, "%s breaks out of %s armor!",
1197.                               Monnam(mon), ppronoun);
1198.                 else
1199.                     You_hear("a cracking sound.");
1200.             }
1201.             m_useup(mon, otmp);
1202.         }
1203.         if ((otmp = which_armor(mon, W_ARMC)) != 0
1204.             /* mummy wrapping adapts to small and very big sizes */
1205.             && (otmp->otyp != MUMMY_WRAPPING || !WrappingAllowed(mdat))) {
1206.             if (otmp->oartifact) {
1207.                 if (vis)
1208.                     pline_mon(mon, "%s %s falls off!", s_suffix(Monnam(mon)),
1209.                           cloak_simple_name(otmp));
1210.                 m_lose_armor(mon, otmp, polyspot);
1211.             } else {
1212.                 Soundeffect(se_ripping_sound, 100);
1213.                 if (vis)
1214.                     pline_mon(mon, "%s %s tears apart!", s_suffix(Monnam(mon)),
1215.                           cloak_simple_name(otmp));
1216.                 else
1217.                     You_hear("a ripping sound.");
1218.                 m_useup(mon, otmp);
1219.             }
1220.         }
1221.         if ((otmp = which_armor(mon, W_ARMU)) != 0) {
1222.             if (vis)
1223.                 pline_mon(mon, "%s shirt rips to shreds!",
1224.                           s_suffix(Monnam(mon)));
1225.             else
1226.                 You_hear("a ripping sound.");
1227.             m_useup(mon, otmp);
1228.         }
1229.     } else if (sliparm(mdat)) {
1230.         /* sliparm checks whirly, noncorporeal, and small or under */
1231.         boolean passes_thru_clothes = !(mdat->msize <= MZ_SMALL);
1232. 
1233.         if ((otmp = which_armor(mon, W_ARM)) != 0) {
1234.             Soundeffect(se_thud, 50);
1235.             if (vis)
1236.                 pline_mon(mon, "%s armor falls around %s!",
1237.                           s_suffix(Monnam(mon)), pronoun);
1238.             else
1239.                 You_hear("a thud.");
1240.             m_lose_armor(mon, otmp, polyspot);
1241.         }
1242.         if ((otmp = which_armor(mon, W_ARMC)) != 0
1243.             /* mummy wrapping adapts to small and very big sizes */
1244.             && (otmp->otyp != MUMMY_WRAPPING || !WrappingAllowed(mdat))) {
1245.             if (vis) {
1246.                 if (is_whirly(mon->data))
1247.                     pline_mon(mon, "%s %s falls, unsupported!",
1248.                               s_suffix(Monnam(mon)), cloak_simple_name(otmp));
1249.                 else
1250.                     pline_mon(mon, "%s shrinks out of %s %s!",
1251.                               Monnam(mon), ppronoun,
1252.                               cloak_simple_name(otmp));
1253.             }
1254.             m_lose_armor(mon, otmp, polyspot);
1255.         }
1256.         if ((otmp = which_armor(mon, W_ARMU)) != 0) {
1257.             if (vis) {
1258.                 if (passes_thru_clothes)
1259.                     pline_mon(mon, "%s seeps right through %s shirt!",
1260.                               Monnam(mon), ppronoun);
1261.                 else
1262.                     pline_mon(mon, "%s becomes much too small for %s shirt!",
1263.                           Monnam(mon), ppronoun);
1264.             }
1265.             m_lose_armor(mon, otmp, polyspot);
1266.         }
1267.     }
1268.     if (handless_or_tiny) {
1269.         /* [caller needs to handle weapon checks] */
1270.         if ((otmp = which_armor(mon, W_ARMG)) != 0) {
1271.             if (vis)
1272.                 pline_mon(mon, "%s drops %s gloves%s!",
1273.                           Monnam(mon), ppronoun,
1274.                           MON_WEP(mon) ? " and weapon" : "");
1275.             m_lose_armor(mon, otmp, polyspot);
1276.         }
1277.         if ((otmp = which_armor(mon, W_ARMS)) != 0) {
1278.             Soundeffect(se_clank, 50);
1279.             if (vis)
1280.                 pline_mon(mon, "%s can no longer hold %s shield!",
1281.                           Monnam(mon), ppronoun);
1282.             else
1283.                 You_hear("a clank.");
1284.             m_lose_armor(mon, otmp, polyspot);
1285.         }
1286.     }
1287.     if (handless_or_tiny || has_horns(mdat)) {
1288.         if ((otmp = which_armor(mon, W_ARMH)) != 0
1289.             /* flimsy test for horns matches polyself handling */
1290.             && (handless_or_tiny || !is_flimsy(otmp))) {
1291.             if (vis)
1292.                 pline_mon(mon, "%s helmet falls to the %s!",
1293.                           s_suffix(Monnam(mon)), surface(mon->mx, mon->my));
1294.             else
1295.                 You_hear("a clank.");
1296.             m_lose_armor(mon, otmp, polyspot);
1297.         }
1298.     }
1299.     if (handless_or_tiny || slithy(mdat) || mdat->mlet == S_CENTAUR) {
1300.         if ((otmp = which_armor(mon, W_ARMF)) != 0) {
1301.             if (vis) {
1302.                 if (is_whirly(mon->data))
1303.                     pline_mon(mon, "%s boots fall away!",
1304.                               s_suffix(Monnam(mon)));
1305.                 else
1306.                     pline_mon(mon, "%s boots %s off %s feet!",
1307.                               s_suffix(Monnam(mon)),
1308.                           verysmall(mdat) ? "slide" : "are pushed", ppronoun);
1309.             }
1310.             m_lose_armor(mon, otmp, polyspot);
1311.         }
1312.     }
1313.     if (!can_saddle(mon)) {
1314.         if ((otmp = which_armor(mon, W_SADDLE)) != 0) {
1315.             m_lose_armor(mon, otmp, polyspot);
1316.             if (vis)
1317.                 pline_mon(mon, "%s saddle falls off.", s_suffix(Monnam(mon)));
1318.         }
1319.         if (mon == u.usteed)
1320.             noride = TRUE;
1321.     }
1322.     if (noride || (mon == u.usteed && !can_ride(mon))) {
1323.         You("can no longer ride %s.", mon_nam(mon));
1324.         if (touch_petrifies(u.usteed->data) && !Stone_resistance && rnl(3)) {
1325.             char buf[BUFSZ];
1326. 
1327.             You("touch %s.", mon_nam(u.usteed));
1328.             Sprintf(buf, "falling off %s",
1329.                     an(pmname(u.usteed->data, Mgender(u.usteed))));
1330.             instapetrify(buf);
1331.         }
1332.         dismount_steed(DISMOUNT_FELL);
1333.     }
1334.     return;
1335. }
1336. 
1337. /* bias a monster's preferences towards armor that has special benefits. */
1338. staticfn int
1339. extra_pref(struct monst *mon, struct obj *obj)
1340. {
1341.     /* currently only does speed boots, but might be expanded if monsters
1342.      * get to use more armor abilities
1343.      */
1344.     if (obj) {
1345.         if (obj->otyp == SPEED_BOOTS && mon->permspeed != MFAST)
1346.             return 20;
1347.     }
1348.     return 0;
1349. }
1350. 
1351. /*
1352.  * Exceptions to things based on race.
1353.  * Correctly checks polymorphed player race.
1354.  * Returns:
1355.  *       0 No exception, normal rules apply.
1356.  *       1 If the race/object combination is acceptable.
1357.  *      -1 If the race/object combination is unacceptable.
1358.  */
1359. int
1360. racial_exception(struct monst *mon, struct obj *obj)
1361. {
1362.     const struct permonst *ptr = raceptr(mon);
1363. 
1364.     /* Acceptable Exceptions: */
1365.     /* Allow hobbits to wear elven armor - LoTR */
1366.     if (ptr == &mons[PM_HOBBIT] && is_elven_armor(obj))
1367.         return 1;
1368.     /* Unacceptable Exceptions: */
1369.     /* Checks for object that certain races should never use go here */
1370.     /*  return -1; */
1371. 
1372.     return 0;
1373. }
1374. 
1375. /* Remove an object from a monster's inventory. */
1376. void
1377. extract_from_minvent(
1378.     struct monst *mon,
1379.     struct obj *obj,
1380.     boolean do_extrinsics,  /* whether to call update_mon_extrinsics */
1381.     boolean silently)       /* doesn't affect all possible messages,
1382.                              * just update_mon_extrinsics's */
1383. {
1384.     long unwornmask = obj->owornmask;
1385. 
1386.     /*
1387.      * At its core this is just obj_extract_self(), but it also handles
1388.      * any updates that need to happen if the gear is equipped or in
1389.      * some other sort of state that needs handling.
1390.      * Note that like obj_extract_self(), this leaves obj free.
1391.      */
1392. 
1393.     if (obj->where != OBJ_MINVENT) {
1394.         impossible("extract_from_minvent called on object not in minvent");
1395.         return;
1396.     }
1397.     /* handle gold dragon scales/scale-mail (lit when worn) before clearing
1398.        obj->owornmask because artifact_light() expects that to be W_ARM */
1399.     if ((unwornmask & W_ARM) != 0 && obj->lamplit && artifact_light(obj))
1400.         end_burn(obj, FALSE);
1401. 
1402.     obj_extract_self(obj);
1403.     obj->owornmask = 0L;
1404.     if (unwornmask) {
1405.         if (!DEADMONSTER(mon) && do_extrinsics) {
1406.             update_mon_extrinsics(mon, obj, FALSE, silently);
1407.         }
1408.         mon->misc_worn_check &= ~unwornmask;
1409.         /* give monster a chance to wear other equipment on its next
1410.            move instead of waiting until it picks something up */
1411.         check_gear_next_turn(mon);
1412.     }
1413.     obj_no_longer_held(obj);
1414.     if (unwornmask & W_WEP) {
1415.         mwepgone(mon); /* unwields and sets weapon_check to NEED_WEAPON */
1416.     }
1417. }
1418. 
1419. #undef w_blocks
1420. 
1421. /*worn.c*/