Source:NetHack 3.6.2/src/weapon.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to weapon.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/weapon.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 3.6	weapon.c	$NHDT-Date: 1548209744 2019/01/23 02:15:44 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.69 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /*
7.  *      This module contains code for calculation of "to hit" and damage
8.  *      bonuses for any given weapon used, as well as weapons selection
9.  *      code for monsters.
10.  */
11. #include "hack.h"
12. 
13. STATIC_DCL void FDECL(give_may_advance_msg, (int));
14. STATIC_DCL boolean FDECL(could_advance, (int));
15. STATIC_DCL boolean FDECL(peaked_skill, (int));
16. STATIC_DCL int FDECL(slots_required, (int));
17. STATIC_DCL void FDECL(skill_advance, (int));
18. 
19. /* Categories whose names don't come from OBJ_NAME(objects[type])
20.  */
21. #define PN_BARE_HANDED (-1) /* includes martial arts */
22. #define PN_TWO_WEAPONS (-2)
23. #define PN_RIDING (-3)
24. #define PN_POLEARMS (-4)
25. #define PN_SABER (-5)
26. #define PN_HAMMER (-6)
27. #define PN_WHIP (-7)
28. #define PN_ATTACK_SPELL (-8)
29. #define PN_HEALING_SPELL (-9)
30. #define PN_DIVINATION_SPELL (-10)
31. #define PN_ENCHANTMENT_SPELL (-11)
32. #define PN_CLERIC_SPELL (-12)
33. #define PN_ESCAPE_SPELL (-13)
34. #define PN_MATTER_SPELL (-14)
35. 
36. STATIC_VAR NEARDATA const short skill_names_indices[P_NUM_SKILLS] = {
37.     0, DAGGER, KNIFE, AXE, PICK_AXE, SHORT_SWORD, BROADSWORD, LONG_SWORD,
38.     TWO_HANDED_SWORD, SCIMITAR, PN_SABER, CLUB, MACE, MORNING_STAR, FLAIL,
39.     PN_HAMMER, QUARTERSTAFF, PN_POLEARMS, SPEAR, TRIDENT, LANCE, BOW, SLING,
40.     CROSSBOW, DART, SHURIKEN, BOOMERANG, PN_WHIP, UNICORN_HORN,
41.     PN_ATTACK_SPELL, PN_HEALING_SPELL, PN_DIVINATION_SPELL,
42.     PN_ENCHANTMENT_SPELL, PN_CLERIC_SPELL, PN_ESCAPE_SPELL, PN_MATTER_SPELL,
43.     PN_BARE_HANDED, PN_TWO_WEAPONS, PN_RIDING
44. };
45. 
46. /* note: entry [0] isn't used */
47. STATIC_VAR NEARDATA const char *const odd_skill_names[] = {
48.     "no skill", "bare hands", /* use barehands_or_martial[] instead */
49.     "two weapon combat", "riding", "polearms", "saber", "hammer", "whip",
50.     "attack spells", "healing spells", "divination spells",
51.     "enchantment spells", "clerical spells", "escape spells", "matter spells",
52. };
53. /* indexed vis `is_martial() */
54. STATIC_VAR NEARDATA const char *const barehands_or_martial[] = {
55.     "bare handed combat", "martial arts"
56. };
57. 
58. #define P_NAME(type)                                    \
59.     ((skill_names_indices[type] > 0)                    \
60.          ? OBJ_NAME(objects[skill_names_indices[type]]) \
61.          : (type == P_BARE_HANDED_COMBAT)               \
62.                ? barehands_or_martial[martial_bonus()]  \
63.                : odd_skill_names[-skill_names_indices[type]])
64. 
65. static NEARDATA const char kebabable[] = { S_XORN, S_DRAGON, S_JABBERWOCK,
66.                                            S_NAGA, S_GIANT,  '\0' };
67. 
68. STATIC_OVL void
69. give_may_advance_msg(skill)
70. int skill;
71. {
72.     You_feel("more confident in your %sskills.",
73.              (skill == P_NONE) ? ""
74.                  : (skill <= P_LAST_WEAPON) ? "weapon "
75.                      : (skill <= P_LAST_SPELL) ? "spell casting "
76.                          : "fighting ");
77. }
78. 
79. /* weapon's skill category name for use as generalized description of weapon;
80.    mostly used to shorten "you drop your <weapon>" messages when slippery
81.    fingers or polymorph causes hero to involuntarily drop wielded weapon(s) */
82. const char *
83. weapon_descr(obj)
84. struct obj *obj;
85. {
86.     int skill = weapon_type(obj);
87.     const char *descr = P_NAME(skill);
88. 
89.     /* assorted special cases */
90.     switch (skill) {
91.     case P_NONE:
92.         /* not a weapon or weptool: use item class name;
93.            override class name "food" for corpses, tins, and eggs,
94.            "large rock" for statues and boulders, and "tool" for towels */
95.         descr = (obj->otyp == CORPSE || obj->otyp == TIN || obj->otyp == EGG
96.                  || obj->otyp == STATUE || obj->otyp == BOULDER
97.                  || obj->otyp == TOWEL)
98.                     ? OBJ_NAME(objects[obj->otyp])
99.                     : def_oc_syms[(int) obj->oclass].name;
100.         break;
101.     case P_SLING:
102.         if (is_ammo(obj))
103.             descr = (obj->otyp == ROCK || is_graystone(obj))
104.                         ? "stone"
105.                         /* avoid "rock"; what about known glass? */
106.                         : (obj->oclass == GEM_CLASS)
107.                             ? "gem"
108.                             /* in case somebody adds odd sling ammo */
109.                             : def_oc_syms[(int) obj->oclass].name;
110.         break;
111.     case P_BOW:
112.         if (is_ammo(obj))
113.             descr = "arrow";
114.         break;
115.     case P_CROSSBOW:
116.         if (is_ammo(obj))
117.             descr = "bolt";
118.         break;
119.     case P_FLAIL:
120.         if (obj->otyp == GRAPPLING_HOOK)
121.             descr = "hook";
122.         break;
123.     case P_PICK_AXE:
124.         /* even if "dwarvish mattock" hasn't been discovered yet */
125.         if (obj->otyp == DWARVISH_MATTOCK)
126.             descr = "mattock";
127.         break;
128.     default:
129.         break;
130.     }
131.     return makesingular(descr);
132. }
133. 
134. /*
135.  *      hitval returns an integer representing the "to hit" bonuses
136.  *      of "otmp" against the monster.
137.  */
138. int
139. hitval(otmp, mon)
140. struct obj *otmp;
141. struct monst *mon;
142. {
143.     int tmp = 0;
144.     struct permonst *ptr = mon->data;
145.     boolean Is_weapon = (otmp->oclass == WEAPON_CLASS || is_weptool(otmp));
146. 
147.     if (Is_weapon)
148.         tmp += otmp->spe;
149. 
150.     /* Put weapon specific "to hit" bonuses in below: */
151.     tmp += objects[otmp->otyp].oc_hitbon;
152. 
153.     /* Put weapon vs. monster type "to hit" bonuses in below: */
154. 
155.     /* Blessed weapons used against undead or demons */
156.     if (Is_weapon && otmp->blessed
157.         && (is_demon(ptr) || is_undead(ptr) || is_vampshifter(mon)))
158.         tmp += 2;
159. 
160.     if (is_spear(otmp) && index(kebabable, ptr->mlet))
161.         tmp += 2;
162. 
163.     /* trident is highly effective against swimmers */
164.     if (otmp->otyp == TRIDENT && is_swimmer(ptr)) {
165.         if (is_pool(mon->mx, mon->my))
166.             tmp += 4;
167.         else if (ptr->mlet == S_EEL || ptr->mlet == S_SNAKE)
168.             tmp += 2;
169.     }
170. 
171.     /* Picks used against xorns and earth elementals */
172.     if (is_pick(otmp) && (passes_walls(ptr) && thick_skinned(ptr)))
173.         tmp += 2;
174. 
175.     /* Check specially named weapon "to hit" bonuses */
176.     if (otmp->oartifact)
177.         tmp += spec_abon(otmp, mon);
178. 
179.     return tmp;
180. }
181. 
182. /* Historical note: The original versions of Hack used a range of damage
183.  * which was similar to, but not identical to the damage used in Advanced
184.  * Dungeons and Dragons.  I figured that since it was so close, I may as well
185.  * make it exactly the same as AD&D, adding some more weapons in the process.
186.  * This has the advantage that it is at least possible that the player would
187.  * already know the damage of at least some of the weapons.  This was circa
188.  * 1987 and I used the table from Unearthed Arcana until I got tired of typing
189.  * them in (leading to something of an imbalance towards weapons early in
190.  * alphabetical order).  The data structure still doesn't include fields that
191.  * fully allow the appropriate damage to be described (there's no way to say
192.  * 3d6 or 1d6+1) so we add on the extra damage in dmgval() if the weapon
193.  * doesn't do an exact die of damage.
194.  *
195.  * Of course new weapons were added later in the development of Nethack.  No
196.  * AD&D consistency was kept, but most of these don't exist in AD&D anyway.
197.  *
198.  * Second edition AD&D came out a few years later; luckily it used the same
199.  * table.  As of this writing (1999), third edition is in progress but not
200.  * released.  Let's see if the weapon table stays the same.  --KAA
201.  * October 2000: It didn't.  Oh, well.
202.  */
203. 
204. /*
205.  *      dmgval returns an integer representing the damage bonuses
206.  *      of "otmp" against the monster.
207.  */
208. int
209. dmgval(otmp, mon)
210. struct obj *otmp;
211. struct monst *mon;
212. {
213.     int tmp = 0, otyp = otmp->otyp;
214.     struct permonst *ptr = mon->data;
215.     boolean Is_weapon = (otmp->oclass == WEAPON_CLASS || is_weptool(otmp));
216. 
217.     if (otyp == CREAM_PIE)
218.         return 0;
219. 
220.     if (bigmonst(ptr)) {
221.         if (objects[otyp].oc_wldam)
222.             tmp = rnd(objects[otyp].oc_wldam);
223.         switch (otyp) {
224.         case IRON_CHAIN:
225.         case CROSSBOW_BOLT:
226.         case MORNING_STAR:
227.         case PARTISAN:
228.         case RUNESWORD:
229.         case ELVEN_BROADSWORD:
230.         case BROADSWORD:
231.             tmp++;
232.             break;
233. 
234.         case FLAIL:
235.         case RANSEUR:
236.         case VOULGE:
237.             tmp += rnd(4);
238.             break;
239. 
240.         case ACID_VENOM:
241.         case HALBERD:
242.         case SPETUM:
243.             tmp += rnd(6);
244.             break;
245. 
246.         case BATTLE_AXE:
247.         case BARDICHE:
248.         case TRIDENT:
249.             tmp += d(2, 4);
250.             break;
251. 
252.         case TSURUGI:
253.         case DWARVISH_MATTOCK:
254.         case TWO_HANDED_SWORD:
255.             tmp += d(2, 6);
256.             break;
257.         }
258.     } else {
259.         if (objects[otyp].oc_wsdam)
260.             tmp = rnd(objects[otyp].oc_wsdam);
261.         switch (otyp) {
262.         case IRON_CHAIN:
263.         case CROSSBOW_BOLT:
264.         case MACE:
265.         case WAR_HAMMER:
266.         case FLAIL:
267.         case SPETUM:
268.         case TRIDENT:
269.             tmp++;
270.             break;
271. 
272.         case BATTLE_AXE:
273.         case BARDICHE:
274.         case BILL_GUISARME:
275.         case GUISARME:
276.         case LUCERN_HAMMER:
277.         case MORNING_STAR:
278.         case RANSEUR:
279.         case BROADSWORD:
280.         case ELVEN_BROADSWORD:
281.         case RUNESWORD:
282.         case VOULGE:
283.             tmp += rnd(4);
284.             break;
285. 
286.         case ACID_VENOM:
287.             tmp += rnd(6);
288.             break;
289.         }
290.     }
291.     if (Is_weapon) {
292.         tmp += otmp->spe;
293.         /* negative enchantment mustn't produce negative damage */
294.         if (tmp < 0)
295.             tmp = 0;
296.     }
297. 
298.     if (objects[otyp].oc_material <= LEATHER && thick_skinned(ptr))
299.         /* thick skinned/scaled creatures don't feel it */
300.         tmp = 0;
301.     if (ptr == &mons[PM_SHADE] && !shade_glare(otmp))
302.         tmp = 0;
303. 
304.     /* "very heavy iron ball"; weight increase is in increments */
305.     if (otyp == HEAVY_IRON_BALL && tmp > 0) {
306.         int wt = (int) objects[HEAVY_IRON_BALL].oc_weight;
307. 
308.         if ((int) otmp->owt > wt) {
309.             wt = ((int) otmp->owt - wt) / IRON_BALL_W_INCR;
310.             tmp += rnd(4 * wt);
311.             if (tmp > 25)
312.                 tmp = 25; /* objects[].oc_wldam */
313.         }
314.     }
315. 
316.     /* Put weapon vs. monster type damage bonuses in below: */
317.     if (Is_weapon || otmp->oclass == GEM_CLASS || otmp->oclass == BALL_CLASS
318.         || otmp->oclass == CHAIN_CLASS) {
319.         int bonus = 0;
320. 
321.         if (otmp->blessed
322.             && (is_undead(ptr) || is_demon(ptr) || is_vampshifter(mon)))
323.             bonus += rnd(4);
324.         if (is_axe(otmp) && is_wooden(ptr))
325.             bonus += rnd(4);
326.         if (objects[otyp].oc_material == SILVER && mon_hates_silver(mon))
327.             bonus += rnd(20);
328.         if (artifact_light(otmp) && otmp->lamplit && hates_light(ptr))
329.             bonus += rnd(8);
330. 
331.         /* if the weapon is going to get a double damage bonus, adjust
332.            this bonus so that effectively it's added after the doubling */
333.         if (bonus > 1 && otmp->oartifact && spec_dbon(otmp, mon, 25) >= 25)
334.             bonus = (bonus + 1) / 2;
335. 
336.         tmp += bonus;
337.     }
338. 
339.     if (tmp > 0) {
340.         /* It's debatable whether a rusted blunt instrument
341.            should do less damage than a pristine one, since
342.            it will hit with essentially the same impact, but
343.            there ought to some penalty for using damaged gear
344.            so always subtract erosion even for blunt weapons. */
345.         tmp -= greatest_erosion(otmp);
346.         if (tmp < 1)
347.             tmp = 1;
348.     }
349. 
350.     return  tmp;
351. }
352. 
353. /* check whether blessed and/or silver damage applies for *non-weapon* hit;
354.    return value is the amount of the extra damage */
355. int
356. special_dmgval(magr, mdef, armask, silverhit_p)
357. struct monst *magr, *mdef;
358. long armask; /* armor mask, multiple bits accepted for W_ARMC|W_ARM|W_ARMU
359.               * or W_ARMG|W_RINGL|W_RINGR only */
360. long *silverhit_p; /* output flag mask for silver bonus */
361. {
362.     struct obj *obj;
363.     struct permonst *ptr = mdef->data;
364.     boolean left_ring = (armask & W_RINGL) ? TRUE : FALSE,
365.             right_ring = (armask & W_RINGR) ? TRUE : FALSE;
366.     long silverhit = 0L;
367.     int bonus = 0;
368. 
369.     obj = 0;
370.     if (armask & (W_ARMC | W_ARM | W_ARMU)) {
371.         if ((armask & W_ARMC) != 0L
372.             && (obj = which_armor(magr, W_ARMC)) != 0)
373.             armask = W_ARMC;
374.         else if ((armask & W_ARM) != 0L
375.                  && (obj = which_armor(magr, W_ARM)) != 0)
376.             armask = W_ARM;
377.         else if ((armask & W_ARMU) != 0L
378.                  && (obj = which_armor(magr, W_ARMU)) != 0)
379.             armask = W_ARMU;
380.         else
381.             armask = 0L;
382.     } else if (armask & (W_ARMG | W_RINGL | W_RINGR)) {
383.         armask = ((obj = which_armor(magr, W_ARMG)) != 0) ?  W_ARMG : 0L;
384.     } else {
385.         obj = which_armor(magr, armask);
386.     }
387. 
388.     if (obj) {
389.         if (obj->blessed
390.             && (is_undead(ptr) || is_demon(ptr) || is_vampshifter(mdef)))
391.             bonus += rnd(4);
392.         /* the only silver armor is shield of reflection (silver dragon
393.            scales refer to color, not material) and the only way to hit
394.            with one--aside from throwing--is to wield it and perform a
395.            weapon hit, but we include a general check here */
396.         if (objects[obj->otyp].oc_material == SILVER
397.             && mon_hates_silver(mdef)) {
398.             bonus += rnd(20);
399.             silverhit |= armask;
400.         }
401. 
402.     /* when no gloves we check for silver rings (blessed rings ignored) */
403.     } else if ((left_ring || right_ring) && magr == &youmonst) {
404.         if (left_ring && uleft) {
405.             if (objects[uleft->otyp].oc_material == SILVER
406.                 && mon_hates_silver(mdef)) {
407.                 bonus += rnd(20);
408.                 silverhit |= W_RINGL;
409.             }
410.         }
411.         if (right_ring && uright) {
412.             if (objects[uright->otyp].oc_material == SILVER
413.                 && mon_hates_silver(mdef)) {
414.                 /* two silver rings don't give double silver damage
415.                    but 'silverhit' messages might be adjusted for them */
416.                 if (!(silverhit & W_RINGL))
417.                     bonus += rnd(20);
418.                 silverhit |= W_RINGR;
419.             }
420.         }
421.     }
422. 
423.     if (silverhit_p)
424.         *silverhit_p = silverhit;
425.     return bonus;
426. }
427. 
428. /* give a "silver <item> sears <target>" message;
429.    not used for weapon hit, so we only handle rings */
430. void
431. silver_sears(magr, mdef, silverhit)
432. struct monst *magr UNUSED;
433. struct monst *mdef;
434. long silverhit;
435. {
436.     char rings[20]; /* plenty of room for "rings" */
437.     int ltyp = ((uleft && (silverhit & W_RINGL) != 0L)
438.                 ? uleft->otyp : STRANGE_OBJECT),
439.         rtyp = ((uright && (silverhit & W_RINGR) != 0L)
440.                 ? uright->otyp : STRANGE_OBJECT);
441.     boolean both,
442.         l_ag = (objects[ltyp].oc_material == SILVER && uleft->dknown),
443.         r_ag = (objects[rtyp].oc_material == SILVER && uright->dknown);
444. 
445.     if ((silverhit & (W_RINGL | W_RINGR)) != 0L) {
446.         /* plural if both the same type (so not multi_claw and both rings
447.            are non-Null) and either both known or neither known, or both
448.            silver (in case there is ever more than one type of silver ring)
449.            and both known; singular if multi_claw (where one of ltyp or
450.            rtyp will always be STRANGE_OBJECT) even if both rings are known
451.            silver [see hmonas(uhitm.c) for explanation of 'multi_claw'] */
452.         both = ((ltyp == rtyp && uleft->dknown == uright->dknown)
453.                 || (l_ag && r_ag));
454.         Sprintf(rings, "ring%s", both ? "s" : "");
455.         Your("%s%s %s %s!",
456.              (l_ag || r_ag) ? "silver "
457.              : both ? ""
458.                : ((silverhit & W_RINGL) != 0L) ? "left "
459.                  : "right ",
460.              rings, vtense(rings, "sear"), mon_nam(mdef));
461.     }
462. }
463. 
464. STATIC_DCL struct obj *FDECL(oselect, (struct monst *, int));
465. #define Oselect(x)                      \
466.     if ((otmp = oselect(mtmp, x)) != 0) \
467.         return otmp;
468. 
469. STATIC_OVL struct obj *
470. oselect(mtmp, x)
471. struct monst *mtmp;
472. int x;
473. {
474.     struct obj *otmp;
475. 
476.     for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) {
477.         if (otmp->otyp == x
478.             /* never select non-cockatrice corpses */
479.             && !((x == CORPSE || x == EGG)
480.                  && !touch_petrifies(&mons[otmp->corpsenm]))
481.             && (!otmp->oartifact || touch_artifact(otmp, mtmp)))
482.             return otmp;
483.     }
484.     return (struct obj *) 0;
485. }
486. 
487. /* TODO: have monsters use aklys' throw-and-return */
488. static NEARDATA const int rwep[] = {
489.     DWARVISH_SPEAR, SILVER_SPEAR, ELVEN_SPEAR, SPEAR, ORCISH_SPEAR, JAVELIN,
490.     SHURIKEN, YA, SILVER_ARROW, ELVEN_ARROW, ARROW, ORCISH_ARROW,
491.     CROSSBOW_BOLT, SILVER_DAGGER, ELVEN_DAGGER, DAGGER, ORCISH_DAGGER, KNIFE,
492.     FLINT, ROCK, LOADSTONE, LUCKSTONE, DART,
493.     /* BOOMERANG, */ CREAM_PIE
494. };
495. 
496. static NEARDATA const int pwep[] = { HALBERD,       BARDICHE, SPETUM,
497.                                      BILL_GUISARME, VOULGE,   RANSEUR,
498.                                      GUISARME,      GLAIVE,   LUCERN_HAMMER,
499.                                      BEC_DE_CORBIN, FAUCHARD, PARTISAN,
500.                                      LANCE };
501. 
502. static struct obj *propellor;
503. 
504. /* select a ranged weapon for the monster */
505. struct obj *
506. select_rwep(mtmp)
507. register struct monst *mtmp;
508. {
509.     register struct obj *otmp;
510.     struct obj *mwep;
511.     boolean mweponly;
512.     int i;
513. 
514.     char mlet = mtmp->data->mlet;
515. 
516.     propellor = (struct obj *) &zeroobj;
517.     Oselect(EGG);      /* cockatrice egg */
518.     if (mlet == S_KOP) /* pies are first choice for Kops */
519.         Oselect(CREAM_PIE);
520.     if (throws_rocks(mtmp->data)) /* ...boulders for giants */
521.         Oselect(BOULDER);
522. 
523.     /* Select polearms first; they do more damage and aren't expendable.
524.        But don't pick one if monster's weapon is welded, because then
525.        we'd never have a chance to throw non-wielding missiles. */
526.     /* The limit of 13 here is based on the monster polearm range limit
527.      * (defined as 5 in mthrowu.c).  5 corresponds to a distance of 2 in
528.      * one direction and 1 in another; one space beyond that would be 3 in
529.      * one direction and 2 in another; 3^2+2^2=13.
530.      */
531.     mwep = MON_WEP(mtmp);
532.     /* NO_WEAPON_WANTED means we already tried to wield and failed */
533.     mweponly = (mwelded(mwep) && mtmp->weapon_check == NO_WEAPON_WANTED);
534.     if (dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 13
535.         && couldsee(mtmp->mx, mtmp->my)) {
536.         for (i = 0; i < SIZE(pwep); i++) {
537.             /* Only strong monsters can wield big (esp. long) weapons.
538.              * Big weapon is basically the same as bimanual.
539.              * All monsters can wield the remaining weapons.
540.              */
541.             if (((strongmonst(mtmp->data)
542.                   && (mtmp->misc_worn_check & W_ARMS) == 0)
543.                  || !objects[pwep[i]].oc_bimanual)
544.                 && (objects[pwep[i]].oc_material != SILVER
545.                     || !mon_hates_silver(mtmp))) {
546.                 if ((otmp = oselect(mtmp, pwep[i])) != 0
547.                     && (otmp == mwep || !mweponly)) {
548.                     propellor = otmp; /* force the monster to wield it */
549.                     return otmp;
550.                 }
551.             }
552.         }
553.     }
554. 
555.     /*
556.      * other than these two specific cases, always select the
557.      * most potent ranged weapon to hand.
558.      */
559.     for (i = 0; i < SIZE(rwep); i++) {
560.         int prop;
561. 
562.         /* shooting gems from slings; this goes just before the darts */
563.         /* (shooting rocks is already handled via the rwep[] ordering) */
564.         if (rwep[i] == DART && !likes_gems(mtmp->data)
565.             && m_carrying(mtmp, SLING)) { /* propellor */
566.             for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
567.                 if (otmp->oclass == GEM_CLASS
568.                     && (otmp->otyp != LOADSTONE || !otmp->cursed)) {
569.                     propellor = m_carrying(mtmp, SLING);
570.                     return otmp;
571.                 }
572.         }
573. 
574.         /* KMH -- This belongs here so darts will work */
575.         propellor = (struct obj *) &zeroobj;
576. 
577.         prop = objects[rwep[i]].oc_skill;
578.         if (prop < 0) {
579.             switch (-prop) {
580.             case P_BOW:
581.                 propellor = oselect(mtmp, YUMI);
582.                 if (!propellor)
583.                     propellor = oselect(mtmp, ELVEN_BOW);
584.                 if (!propellor)
585.                     propellor = oselect(mtmp, BOW);
586.                 if (!propellor)
587.                     propellor = oselect(mtmp, ORCISH_BOW);
588.                 break;
589.             case P_SLING:
590.                 propellor = oselect(mtmp, SLING);
591.                 break;
592.             case P_CROSSBOW:
593.                 propellor = oselect(mtmp, CROSSBOW);
594.             }
595.             if ((otmp = MON_WEP(mtmp)) && mwelded(otmp) && otmp != propellor
596.                 && mtmp->weapon_check == NO_WEAPON_WANTED)
597.                 propellor = 0;
598.         }
599.         /* propellor = obj, propellor to use
600.          * propellor = &zeroobj, doesn't need a propellor
601.          * propellor = 0, needed one and didn't have one
602.          */
603.         if (propellor != 0) {
604.             /* Note: cannot use m_carrying for loadstones, since it will
605.              * always select the first object of a type, and maybe the
606.              * monster is carrying two but only the first is unthrowable.
607.              */
608.             if (rwep[i] != LOADSTONE) {
609.                 /* Don't throw a cursed weapon-in-hand or an artifact */
610.                 if ((otmp = oselect(mtmp, rwep[i])) && !otmp->oartifact
611.                     && !(otmp == MON_WEP(mtmp) && mwelded(otmp)))
612.                     return otmp;
613.             } else
614.                 for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) {
615.                     if (otmp->otyp == LOADSTONE && !otmp->cursed)
616.                         return otmp;
617.                 }
618.         }
619.     }
620. 
621.     /* failure */
622.     return (struct obj *) 0;
623. }
624. 
625. /* is 'obj' a type of weapon that any monster knows how to throw? */
626. boolean
627. monmightthrowwep(obj)
628. struct obj *obj;
629. {
630.     short idx;
631. 
632.     for (idx = 0; idx < SIZE(rwep); ++idx)
633.         if (obj->otyp == rwep[idx])
634.             return TRUE;
635.     return FALSE;
636. }
637. 
638. /* Weapons in order of preference */
639. static const NEARDATA short hwep[] = {
640.     CORPSE, /* cockatrice corpse */
641.     TSURUGI, RUNESWORD, DWARVISH_MATTOCK, TWO_HANDED_SWORD, BATTLE_AXE,
642.     KATANA, UNICORN_HORN, CRYSKNIFE, TRIDENT, LONG_SWORD, ELVEN_BROADSWORD,
643.     BROADSWORD, SCIMITAR, SILVER_SABER, MORNING_STAR, ELVEN_SHORT_SWORD,
644.     DWARVISH_SHORT_SWORD, SHORT_SWORD, ORCISH_SHORT_SWORD, MACE, AXE,
645.     DWARVISH_SPEAR, SILVER_SPEAR, ELVEN_SPEAR, SPEAR, ORCISH_SPEAR, FLAIL,
646.     BULLWHIP, QUARTERSTAFF, JAVELIN, AKLYS, CLUB, PICK_AXE, RUBBER_HOSE,
647.     WAR_HAMMER, SILVER_DAGGER, ELVEN_DAGGER, DAGGER, ORCISH_DAGGER, ATHAME,
648.     SCALPEL, KNIFE, WORM_TOOTH
649. };
650. 
651. /* select a hand to hand weapon for the monster */
652. struct obj *
653. select_hwep(mtmp)
654. register struct monst *mtmp;
655. {
656.     register struct obj *otmp;
657.     register int i;
658.     boolean strong = strongmonst(mtmp->data);
659.     boolean wearing_shield = (mtmp->misc_worn_check & W_ARMS) != 0;
660. 
661.     /* prefer artifacts to everything else */
662.     for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) {
663.         if (otmp->oclass == WEAPON_CLASS && otmp->oartifact
664.             && touch_artifact(otmp, mtmp)
665.             && ((strong && !wearing_shield)
666.                 || !objects[otmp->otyp].oc_bimanual))
667.             return otmp;
668.     }
669. 
670.     if (is_giant(mtmp->data)) /* giants just love to use clubs */
671.         Oselect(CLUB);
672. 
673.     /* only strong monsters can wield big (esp. long) weapons */
674.     /* big weapon is basically the same as bimanual */
675.     /* all monsters can wield the remaining weapons */
676.     for (i = 0; i < SIZE(hwep); i++) {
677.         if (hwep[i] == CORPSE && !(mtmp->misc_worn_check & W_ARMG)
678.             && !resists_ston(mtmp))
679.             continue;
680.         if (((strong && !wearing_shield) || !objects[hwep[i]].oc_bimanual)
681.             && (objects[hwep[i]].oc_material != SILVER
682.                 || !mon_hates_silver(mtmp)))
683.             Oselect(hwep[i]);
684.     }
685. 
686.     /* failure */
687.     return (struct obj *) 0;
688. }
689. 
690. /* Called after polymorphing a monster, robbing it, etc....  Monsters
691.  * otherwise never unwield stuff on their own.  Might print message.
692.  */
693. void
694. possibly_unwield(mon, polyspot)
695. struct monst *mon;
696. boolean polyspot;
697. {
698.     struct obj *obj, *mw_tmp;
699. 
700.     if (!(mw_tmp = MON_WEP(mon)))
701.         return;
702.     for (obj = mon->minvent; obj; obj = obj->nobj)
703.         if (obj == mw_tmp)
704.             break;
705.     if (!obj) { /* The weapon was stolen or destroyed */
706.         MON_NOWEP(mon);
707.         mon->weapon_check = NEED_WEAPON;
708.         return;
709.     }
710.     if (!attacktype(mon->data, AT_WEAP)) {
711.         setmnotwielded(mon, mw_tmp);
712.         mon->weapon_check = NO_WEAPON_WANTED;
713.         obj_extract_self(obj);
714.         if (cansee(mon->mx, mon->my)) {
715.             pline("%s drops %s.", Monnam(mon), distant_name(obj, doname));
716.             newsym(mon->mx, mon->my);
717.         }
718.         /* might be dropping object into water or lava */
719.         if (!flooreffects(obj, mon->mx, mon->my, "drop")) {
720.             if (polyspot)
721.                 bypass_obj(obj);
722.             place_object(obj, mon->mx, mon->my);
723.             stackobj(obj);
724.         }
725.         return;
726.     }
727.     /* The remaining case where there is a change is where a monster
728.      * is polymorphed into a stronger/weaker monster with a different
729.      * choice of weapons.  This has no parallel for players.  It can
730.      * be handled by waiting until mon_wield_item is actually called.
731.      * Though the monster still wields the wrong weapon until then,
732.      * this is OK since the player can't see it.  (FIXME: Not okay since
733.      * probing can reveal it.)
734.      * Note that if there is no change, setting the check to NEED_WEAPON
735.      * is harmless.
736.      * Possible problem: big monster with big cursed weapon gets
737.      * polymorphed into little monster.  But it's not quite clear how to
738.      * handle this anyway....
739.      */
740.     if (!(mwelded(mw_tmp) && mon->weapon_check == NO_WEAPON_WANTED))
741.         mon->weapon_check = NEED_WEAPON;
742.     return;
743. }
744. 
745. /* Let a monster try to wield a weapon, based on mon->weapon_check.
746.  * Returns 1 if the monster took time to do it, 0 if it did not.
747.  */
748. int
749. mon_wield_item(mon)
750. register struct monst *mon;
751. {
752.     struct obj *obj;
753. 
754.     /* This case actually should never happen */
755.     if (mon->weapon_check == NO_WEAPON_WANTED)
756.         return 0;
757.     switch (mon->weapon_check) {
758.     case NEED_HTH_WEAPON:
759.         obj = select_hwep(mon);
760.         break;
761.     case NEED_RANGED_WEAPON:
762.         (void) select_rwep(mon);
763.         obj = propellor;
764.         break;
765.     case NEED_PICK_AXE:
766.         obj = m_carrying(mon, PICK_AXE);
767.         /* KMH -- allow other picks */
768.         if (!obj && !which_armor(mon, W_ARMS))
769.             obj = m_carrying(mon, DWARVISH_MATTOCK);
770.         break;
771.     case NEED_AXE:
772.         /* currently, only 2 types of axe */
773.         obj = m_carrying(mon, BATTLE_AXE);
774.         if (!obj || which_armor(mon, W_ARMS))
775.             obj = m_carrying(mon, AXE);
776.         break;
777.     case NEED_PICK_OR_AXE:
778.         /* prefer pick for fewer switches on most levels */
779.         obj = m_carrying(mon, DWARVISH_MATTOCK);
780.         if (!obj)
781.             obj = m_carrying(mon, BATTLE_AXE);
782.         if (!obj || which_armor(mon, W_ARMS)) {
783.             obj = m_carrying(mon, PICK_AXE);
784.             if (!obj)
785.                 obj = m_carrying(mon, AXE);
786.         }
787.         break;
788.     default:
789.         impossible("weapon_check %d for %s?", mon->weapon_check,
790.                    mon_nam(mon));
791.         return 0;
792.     }
793.     if (obj && obj != &zeroobj) {
794.         struct obj *mw_tmp = MON_WEP(mon);
795. 
796.         if (mw_tmp && mw_tmp->otyp == obj->otyp) {
797.             /* already wielding it */
798.             mon->weapon_check = NEED_WEAPON;
799.             return 0;
800.         }
801.         /* Actually, this isn't necessary--as soon as the monster
802.          * wields the weapon, the weapon welds itself, so the monster
803.          * can know it's cursed and needn't even bother trying.
804.          * Still....
805.          */
806.         if (mw_tmp && mwelded(mw_tmp)) {
807.             if (canseemon(mon)) {
808.                 char welded_buf[BUFSZ];
809.                 const char *mon_hand = mbodypart(mon, HAND);
810. 
811.                 if (bimanual(mw_tmp))
812.                     mon_hand = makeplural(mon_hand);
813.                 Sprintf(welded_buf, "%s welded to %s %s",
814.                         otense(mw_tmp, "are"), mhis(mon), mon_hand);
815. 
816.                 if (obj->otyp == PICK_AXE) {
817.                     pline("Since %s weapon%s %s,", s_suffix(mon_nam(mon)),
818.                           plur(mw_tmp->quan), welded_buf);
819.                     pline("%s cannot wield that %s.", mon_nam(mon),
820.                           xname(obj));
821.                 } else {
822.                     pline("%s tries to wield %s.", Monnam(mon), doname(obj));
823.                     pline("%s %s!", Yname2(mw_tmp), welded_buf);
824.                 }
825.                 mw_tmp->bknown = 1;
826.             }
827.             mon->weapon_check = NO_WEAPON_WANTED;
828.             return 1;
829.         }
830.         mon->mw = obj; /* wield obj */
831.         setmnotwielded(mon, mw_tmp);
832.         mon->weapon_check = NEED_WEAPON;
833.         if (canseemon(mon)) {
834.             pline("%s wields %s!", Monnam(mon), doname(obj));
835.             if (mwelded(mw_tmp)) {
836.                 pline("%s %s to %s %s!", Tobjnam(obj, "weld"),
837.                       is_plural(obj) ? "themselves" : "itself",
838.                       s_suffix(mon_nam(mon)), mbodypart(mon, HAND));
839.                 obj->bknown = 1;
840.             }
841.         }
842.         if (artifact_light(obj) && !obj->lamplit) {
843.             begin_burn(obj, FALSE);
844.             if (canseemon(mon))
845.                 pline("%s %s in %s %s!", Tobjnam(obj, "shine"),
846.                       arti_light_description(obj), s_suffix(mon_nam(mon)),
847.                       mbodypart(mon, HAND));
848.         }
849.         obj->owornmask = W_WEP;
850.         return 1;
851.     }
852.     mon->weapon_check = NEED_WEAPON;
853.     return 0;
854. }
855. 
856. /* force monster to stop wielding current weapon, if any */
857. void
858. mwepgone(mon)
859. struct monst *mon;
860. {
861.     struct obj *mwep = MON_WEP(mon);
862. 
863.     if (mwep) {
864.         setmnotwielded(mon, mwep);
865.         mon->weapon_check = NEED_WEAPON;
866.     }
867. }
868. 
869. /* attack bonus for strength & dexterity */
870. int
871. abon()
872. {
873.     int sbon;
874.     int str = ACURR(A_STR), dex = ACURR(A_DEX);
875. 
876.     if (Upolyd)
877.         return (adj_lev(&mons[u.umonnum]) - 3);
878.     if (str < 6)
879.         sbon = -2;
880.     else if (str < 8)
881.         sbon = -1;
882.     else if (str < 17)
883.         sbon = 0;
884.     else if (str <= STR18(50))
885.         sbon = 1; /* up to 18/50 */
886.     else if (str < STR18(100))
887.         sbon = 2;
888.     else
889.         sbon = 3;
890. 
891.     /* Game tuning kludge: make it a bit easier for a low level character to
892.      * hit */
893.     sbon += (u.ulevel < 3) ? 1 : 0;
894. 
895.     if (dex < 4)
896.         return (sbon - 3);
897.     else if (dex < 6)
898.         return (sbon - 2);
899.     else if (dex < 8)
900.         return (sbon - 1);
901.     else if (dex < 14)
902.         return sbon;
903.     else
904.         return (sbon + dex - 14);
905. }
906. 
907. /* damage bonus for strength */
908. int
909. dbon()
910. {
911.     int str = ACURR(A_STR);
912. 
913.     if (Upolyd)
914.         return 0;
915. 
916.     if (str < 6)
917.         return -1;
918.     else if (str < 16)
919.         return 0;
920.     else if (str < 18)
921.         return 1;
922.     else if (str == 18)
923.         return 2; /* up to 18 */
924.     else if (str <= STR18(75))
925.         return 3; /* up to 18/75 */
926.     else if (str <= STR18(90))
927.         return 4; /* up to 18/90 */
928.     else if (str < STR18(100))
929.         return 5; /* up to 18/99 */
930.     else
931.         return 6;
932. }
933. 
934. /* increase a towel's wetness */
935. void
936. wet_a_towel(obj, amt, verbose)
937. struct obj *obj;
938. int amt; /* positive: new value; negative: increment by -amt; zero: no-op */
939. boolean verbose;
940. {
941.     int newspe = (amt <= 0) ? obj->spe - amt : amt;
942. 
943.     /* new state is only reported if it's an increase */
944.     if (newspe > obj->spe) {
945.         if (verbose) {
946.             const char *wetness = (newspe < 3)
947.                                      ? (!obj->spe ? "damp" : "damper")
948.                                      : (!obj->spe ? "wet" : "wetter");
949. 
950.             if (carried(obj))
951.                 pline("%s gets %s.", Yobjnam2(obj, (const char *) 0),
952.                       wetness);
953.             else if (mcarried(obj) && canseemon(obj->ocarry))
954.                 pline("%s %s gets %s.", s_suffix(Monnam(obj->ocarry)),
955.                       xname(obj), wetness);
956.         }
957.     }
958.     obj->spe = min(newspe, 7);
959. 
960.     /* if hero is wielding this towel, don't give "you begin bashing
961.        with your wet towel" message on next attack with it */
962.     if (obj == uwep)
963.         unweapon = !is_wet_towel(obj);
964. }
965. 
966. /* decrease a towel's wetness */
967. void
968. dry_a_towel(obj, amt, verbose)
969. struct obj *obj;
970. int amt; /* positive: new value; negative: decrement by -amt; zero: no-op */
971. boolean verbose;
972. {
973.     int newspe = (amt <= 0) ? obj->spe + amt : amt;
974. 
975.     /* new state is only reported if it's a decrease */
976.     if (newspe < obj->spe) {
977.         if (verbose) {
978.             if (carried(obj))
979.                 pline("%s dries%s.", Yobjnam2(obj, (const char *) 0),
980.                       !newspe ? " out" : "");
981.             else if (mcarried(obj) && canseemon(obj->ocarry))
982.                 pline("%s %s drie%s.", s_suffix(Monnam(obj->ocarry)),
983.                       xname(obj), !newspe ? " out" : "");
984.         }
985.     }
986.     newspe = min(newspe, 7);
987.     obj->spe = max(newspe, 0);
988. 
989.     /* if hero is wielding this towel and it is now dry, give "you begin
990.        bashing with your towel" message on next attack with it */
991.     if (obj == uwep)
992.         unweapon = !is_wet_towel(obj);
993. }
994. 
995. /* copy the skill level name into the given buffer */
996. char *
997. skill_level_name(skill, buf)
998. int skill;
999. char *buf;
1000. {
1001.     const char *ptr;
1002. 
1003.     switch (P_SKILL(skill)) {
1004.     case P_UNSKILLED:
1005.         ptr = "Unskilled";
1006.         break;
1007.     case P_BASIC:
1008.         ptr = "Basic";
1009.         break;
1010.     case P_SKILLED:
1011.         ptr = "Skilled";
1012.         break;
1013.     case P_EXPERT:
1014.         ptr = "Expert";
1015.         break;
1016.     /* these are for unarmed combat/martial arts only */
1017.     case P_MASTER:
1018.         ptr = "Master";
1019.         break;
1020.     case P_GRAND_MASTER:
1021.         ptr = "Grand Master";
1022.         break;
1023.     default:
1024.         ptr = "Unknown";
1025.         break;
1026.     }
1027.     Strcpy(buf, ptr);
1028.     return buf;
1029. }
1030. 
1031. const char *
1032. skill_name(skill)
1033. int skill;
1034. {
1035.     return P_NAME(skill);
1036. }
1037. 
1038. /* return the # of slots required to advance the skill */
1039. STATIC_OVL int
1040. slots_required(skill)
1041. int skill;
1042. {
1043.     int tmp = P_SKILL(skill);
1044. 
1045.     /* The more difficult the training, the more slots it takes.
1046.      *  unskilled -> basic      1
1047.      *  basic -> skilled        2
1048.      *  skilled -> expert       3
1049.      */
1050.     if (skill <= P_LAST_WEAPON || skill == P_TWO_WEAPON_COMBAT)
1051.         return tmp;
1052. 
1053.     /* Fewer slots used up for unarmed or martial.
1054.      *  unskilled -> basic      1
1055.      *  basic -> skilled        1
1056.      *  skilled -> expert       2
1057.      *  expert -> master        2
1058.      *  master -> grand master  3
1059.      */
1060.     return (tmp + 1) / 2;
1061. }
1062. 
1063. /* return true if this skill can be advanced */
1064. boolean
1065. can_advance(skill, speedy)
1066. int skill;
1067. boolean speedy;
1068. {
1069.     if (P_RESTRICTED(skill)
1070.         || P_SKILL(skill) >= P_MAX_SKILL(skill)
1071.         || u.skills_advanced >= P_SKILL_LIMIT)
1072.         return FALSE;
1073. 
1074.     if (wizard && speedy)
1075.         return TRUE;
1076. 
1077.     return (boolean) ((int) P_ADVANCE(skill)
1078.                       >= practice_needed_to_advance(P_SKILL(skill))
1079.                       && u.weapon_slots >= slots_required(skill));
1080. }
1081. 
1082. /* return true if this skill could be advanced if more slots were available */
1083. STATIC_OVL boolean
1084. could_advance(skill)
1085. int skill;
1086. {
1087.     if (P_RESTRICTED(skill)
1088.         || P_SKILL(skill) >= P_MAX_SKILL(skill)
1089.         || u.skills_advanced >= P_SKILL_LIMIT)
1090.         return FALSE;
1091. 
1092.     return (boolean) ((int) P_ADVANCE(skill)
1093.                       >= practice_needed_to_advance(P_SKILL(skill)));
1094. }
1095. 
1096. /* return true if this skill has reached its maximum and there's been enough
1097.    practice to become eligible for the next step if that had been possible */
1098. STATIC_OVL boolean
1099. peaked_skill(skill)
1100. int skill;
1101. {
1102.     if (P_RESTRICTED(skill))
1103.         return FALSE;
1104. 
1105.     return (boolean) (P_SKILL(skill) >= P_MAX_SKILL(skill)
1106.                       && ((int) P_ADVANCE(skill)
1107.                           >= practice_needed_to_advance(P_SKILL(skill))));
1108. }
1109. 
1110. STATIC_OVL void
1111. skill_advance(skill)
1112. int skill;
1113. {
1114.     u.weapon_slots -= slots_required(skill);
1115.     P_SKILL(skill)++;
1116.     u.skill_record[u.skills_advanced++] = skill;
1117.     /* subtly change the advance message to indicate no more advancement */
1118.     You("are now %s skilled in %s.",
1119.         P_SKILL(skill) >= P_MAX_SKILL(skill) ? "most" : "more",
1120.         P_NAME(skill));
1121. }
1122. 
1123. static const struct skill_range {
1124.     short first, last;
1125.     const char *name;
1126. } skill_ranges[] = {
1127.     { P_FIRST_H_TO_H, P_LAST_H_TO_H, "Fighting Skills" },
1128.     { P_FIRST_WEAPON, P_LAST_WEAPON, "Weapon Skills" },
1129.     { P_FIRST_SPELL, P_LAST_SPELL, "Spellcasting Skills" },
1130. };
1131. 
1132. /*
1133.  * The `#enhance' extended command.  What we _really_ would like is
1134.  * to keep being able to pick things to advance until we couldn't any
1135.  * more.  This is currently not possible -- the menu code has no way
1136.  * to call us back for instant action.  Even if it did, we would also need
1137.  * to be able to update the menu since selecting one item could make
1138.  * others unselectable.
1139.  */
1140. int
1141. enhance_weapon_skill()
1142. {
1143.     int pass, i, n, len, longest, to_advance, eventually_advance, maxxed_cnt;
1144.     char buf[BUFSZ], sklnambuf[BUFSZ];
1145.     const char *prefix;
1146.     menu_item *selected;
1147.     anything any;
1148.     winid win;
1149.     boolean speedy = FALSE;
1150. 
1151.     if (wizard && yn("Advance skills without practice?") == 'y')
1152.         speedy = TRUE;
1153. 
1154.     do {
1155.         /* find longest available skill name, count those that can advance */
1156.         to_advance = eventually_advance = maxxed_cnt = 0;
1157.         for (longest = 0, i = 0; i < P_NUM_SKILLS; i++) {
1158.             if (P_RESTRICTED(i))
1159.                 continue;
1160.             if ((len = strlen(P_NAME(i))) > longest)
1161.                 longest = len;
1162.             if (can_advance(i, speedy))
1163.                 to_advance++;
1164.             else if (could_advance(i))
1165.                 eventually_advance++;
1166.             else if (peaked_skill(i))
1167.                 maxxed_cnt++;
1168.         }
1169. 
1170.         win = create_nhwindow(NHW_MENU);
1171.         start_menu(win);
1172. 
1173.         /* start with a legend if any entries will be annotated
1174.            with "*" or "#" below */
1175.         if (eventually_advance > 0 || maxxed_cnt > 0) {
1176.             any = zeroany;
1177.             if (eventually_advance > 0) {
1178.                 Sprintf(buf, "(Skill%s flagged by \"*\" may be enhanced %s.)",
1179.                         plur(eventually_advance),
1180.                         (u.ulevel < MAXULEV)
1181.                             ? "when you're more experienced"
1182.                             : "if skill slots become available");
1183.                 add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, buf,
1184.                          MENU_UNSELECTED);
1185.             }
1186.             if (maxxed_cnt > 0) {
1187.                 Sprintf(buf,
1188.                  "(Skill%s flagged by \"#\" cannot be enhanced any further.)",
1189.                         plur(maxxed_cnt));
1190.                 add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, buf,
1191.                          MENU_UNSELECTED);
1192.             }
1193.             add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "",
1194.                      MENU_UNSELECTED);
1195.         }
1196. 
1197.         /* List the skills, making ones that could be advanced
1198.            selectable.  List the miscellaneous skills first.
1199.            Possible future enhancement:  list spell skills before
1200.            weapon skills for spellcaster roles. */
1201.         for (pass = 0; pass < SIZE(skill_ranges); pass++)
1202.             for (i = skill_ranges[pass].first; i <= skill_ranges[pass].last;
1203.                  i++) {
1204.                 /* Print headings for skill types */
1205.                 any = zeroany;
1206.                 if (i == skill_ranges[pass].first)
1207.                     add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings,
1208.                              skill_ranges[pass].name, MENU_UNSELECTED);
1209. 
1210.                 if (P_RESTRICTED(i))
1211.                     continue;
1212.                 /*
1213.                  * Sigh, this assumes a monospaced font unless
1214.                  * iflags.menu_tab_sep is set in which case it puts
1215.                  * tabs between columns.
1216.                  * The 12 is the longest skill level name.
1217.                  * The "    " is room for a selection letter and dash, "a - ".
1218.                  */
1219.                 if (can_advance(i, speedy))
1220.                     prefix = ""; /* will be preceded by menu choice */
1221.                 else if (could_advance(i))
1222.                     prefix = "  * ";
1223.                 else if (peaked_skill(i))
1224.                     prefix = "  # ";
1225.                 else
1226.                     prefix =
1227.                         (to_advance + eventually_advance + maxxed_cnt > 0)
1228.                             ? "    "
1229.                             : "";
1230.                 (void) skill_level_name(i, sklnambuf);
1231.                 if (wizard) {
1232.                     if (!iflags.menu_tab_sep)
1233.                         Sprintf(buf, " %s%-*s %-12s %5d(%4d)", prefix,
1234.                                 longest, P_NAME(i), sklnambuf, P_ADVANCE(i),
1235.                                 practice_needed_to_advance(P_SKILL(i)));
1236.                     else
1237.                         Sprintf(buf, " %s%s\t%s\t%5d(%4d)", prefix, P_NAME(i),
1238.                                 sklnambuf, P_ADVANCE(i),
1239.                                 practice_needed_to_advance(P_SKILL(i)));
1240.                 } else {
1241.                     if (!iflags.menu_tab_sep)
1242.                         Sprintf(buf, " %s %-*s [%s]", prefix, longest,
1243.                                 P_NAME(i), sklnambuf);
1244.                     else
1245.                         Sprintf(buf, " %s%s\t[%s]", prefix, P_NAME(i),
1246.                                 sklnambuf);
1247.                 }
1248.                 any.a_int = can_advance(i, speedy) ? i + 1 : 0;
1249.                 add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, buf,
1250.                          MENU_UNSELECTED);
1251.             }
1252. 
1253.         Strcpy(buf, (to_advance > 0) ? "Pick a skill to advance:"
1254.                                      : "Current skills:");
1255.         if (wizard && !speedy)
1256.             Sprintf(eos(buf), "  (%d slot%s available)", u.weapon_slots,
1257.                     plur(u.weapon_slots));
1258.         end_menu(win, buf);
1259.         n = select_menu(win, to_advance ? PICK_ONE : PICK_NONE, &selected);
1260.         destroy_nhwindow(win);
1261.         if (n > 0) {
1262.             n = selected[0].item.a_int - 1; /* get item selected */
1263.             free((genericptr_t) selected);
1264.             skill_advance(n);
1265.             /* check for more skills able to advance, if so then .. */
1266.             for (n = i = 0; i < P_NUM_SKILLS; i++) {
1267.                 if (can_advance(i, speedy)) {
1268.                     if (!speedy)
1269.                         You_feel("you could be more dangerous!");
1270.                     n++;
1271.                     break;
1272.                 }
1273.             }
1274.         }
1275.     } while (speedy && n > 0);
1276.     return 0;
1277. }
1278. 
1279. /*
1280.  * Change from restricted to unrestricted, allowing P_BASIC as max.  This
1281.  * function may be called with with P_NONE.  Used in pray.c as well as below.
1282.  */
1283. void
1284. unrestrict_weapon_skill(skill)
1285. int skill;
1286. {
1287.     if (skill < P_NUM_SKILLS && P_RESTRICTED(skill)) {
1288.         P_SKILL(skill) = P_UNSKILLED;
1289.         P_MAX_SKILL(skill) = P_BASIC;
1290.         P_ADVANCE(skill) = 0;
1291.     }
1292. }
1293. 
1294. void
1295. use_skill(skill, degree)
1296. int skill;
1297. int degree;
1298. {
1299.     boolean advance_before;
1300. 
1301.     if (skill != P_NONE && !P_RESTRICTED(skill)) {
1302.         advance_before = can_advance(skill, FALSE);
1303.         P_ADVANCE(skill) += degree;
1304.         if (!advance_before && can_advance(skill, FALSE))
1305.             give_may_advance_msg(skill);
1306.     }
1307. }
1308. 
1309. void
1310. add_weapon_skill(n)
1311. int n; /* number of slots to gain; normally one */
1312. {
1313.     int i, before, after;
1314. 
1315.     for (i = 0, before = 0; i < P_NUM_SKILLS; i++)
1316.         if (can_advance(i, FALSE))
1317.             before++;
1318.     u.weapon_slots += n;
1319.     for (i = 0, after = 0; i < P_NUM_SKILLS; i++)
1320.         if (can_advance(i, FALSE))
1321.             after++;
1322.     if (before < after)
1323.         give_may_advance_msg(P_NONE);
1324. }
1325. 
1326. void
1327. lose_weapon_skill(n)
1328. int n; /* number of slots to lose; normally one */
1329. {
1330.     int skill;
1331. 
1332.     while (--n >= 0) {
1333.         /* deduct first from unused slots then from last placed one, if any */
1334.         if (u.weapon_slots) {
1335.             u.weapon_slots--;
1336.         } else if (u.skills_advanced) {
1337.             skill = u.skill_record[--u.skills_advanced];
1338.             if (P_SKILL(skill) <= P_UNSKILLED)
1339.                 panic("lose_weapon_skill (%d)", skill);
1340.             P_SKILL(skill)--; /* drop skill one level */
1341.             /* Lost skill might have taken more than one slot; refund rest. */
1342.             u.weapon_slots = slots_required(skill) - 1;
1343.             /* It might now be possible to advance some other pending
1344.                skill by using the refunded slots, but giving a message
1345.                to that effect would seem pretty confusing.... */
1346.         }
1347.     }
1348. }
1349. 
1350. int
1351. weapon_type(obj)
1352. struct obj *obj;
1353. {
1354.     /* KMH -- now uses the object table */
1355.     int type;
1356. 
1357.     if (!obj)
1358.         return P_BARE_HANDED_COMBAT; /* Not using a weapon */
1359.     if (obj->oclass != WEAPON_CLASS && obj->oclass != TOOL_CLASS
1360.         && obj->oclass != GEM_CLASS)
1361.         return P_NONE; /* Not a weapon, weapon-tool, or ammo */
1362.     type = objects[obj->otyp].oc_skill;
1363.     return (type < 0) ? -type : type;
1364. }
1365. 
1366. int
1367. uwep_skill_type()
1368. {
1369.     if (u.twoweap)
1370.         return P_TWO_WEAPON_COMBAT;
1371.     return weapon_type(uwep);
1372. }
1373. 
1374. /*
1375.  * Return hit bonus/penalty based on skill of weapon.
1376.  * Treat restricted weapons as unskilled.
1377.  */
1378. int
1379. weapon_hit_bonus(weapon)
1380. struct obj *weapon;
1381. {
1382.     int type, wep_type, skill, bonus = 0;
1383.     static const char bad_skill[] = "weapon_hit_bonus: bad skill %d";
1384. 
1385.     wep_type = weapon_type(weapon);
1386.     /* use two weapon skill only if attacking with one of the wielded weapons
1387.      */
1388.     type = (u.twoweap && (weapon == uwep || weapon == uswapwep))
1389.                ? P_TWO_WEAPON_COMBAT
1390.                : wep_type;
1391.     if (type == P_NONE) {
1392.         bonus = 0;
1393.     } else if (type <= P_LAST_WEAPON) {
1394.         switch (P_SKILL(type)) {
1395.         default:
1396.             impossible(bad_skill, P_SKILL(type)); /* fall through */
1397.         case P_ISRESTRICTED:
1398.         case P_UNSKILLED:
1399.             bonus = -4;
1400.             break;
1401.         case P_BASIC:
1402.             bonus = 0;
1403.             break;
1404.         case P_SKILLED:
1405.             bonus = 2;
1406.             break;
1407.         case P_EXPERT:
1408.             bonus = 3;
1409.             break;
1410.         }
1411.     } else if (type == P_TWO_WEAPON_COMBAT) {
1412.         skill = P_SKILL(P_TWO_WEAPON_COMBAT);
1413.         if (P_SKILL(wep_type) < skill)
1414.             skill = P_SKILL(wep_type);
1415.         switch (skill) {
1416.         default:
1417.             impossible(bad_skill, skill); /* fall through */
1418.         case P_ISRESTRICTED:
1419.         case P_UNSKILLED:
1420.             bonus = -9;
1421.             break;
1422.         case P_BASIC:
1423.             bonus = -7;
1424.             break;
1425.         case P_SKILLED:
1426.             bonus = -5;
1427.             break;
1428.         case P_EXPERT:
1429.             bonus = -3;
1430.             break;
1431.         }
1432.     } else if (type == P_BARE_HANDED_COMBAT) {
1433.         /*
1434.          *        b.h. m.a.
1435.          * unskl:  +1  n/a
1436.          * basic:  +1   +3
1437.          * skild:  +2   +4
1438.          * exprt:  +2   +5
1439.          * mastr:  +3   +6
1440.          * grand:  +3   +7
1441.          */
1442.         bonus = P_SKILL(type);
1443.         bonus = max(bonus, P_UNSKILLED) - 1; /* unskilled => 0 */
1444.         bonus = ((bonus + 2) * (martial_bonus() ? 2 : 1)) / 2;
1445.     }
1446. 
1447.     /* KMH -- It's harder to hit while you are riding */
1448.     if (u.usteed) {
1449.         switch (P_SKILL(P_RIDING)) {
1450.         case P_ISRESTRICTED:
1451.         case P_UNSKILLED:
1452.             bonus -= 2;
1453.             break;
1454.         case P_BASIC:
1455.             bonus -= 1;
1456.             break;
1457.         case P_SKILLED:
1458.             break;
1459.         case P_EXPERT:
1460.             break;
1461.         }
1462.         if (u.twoweap)
1463.             bonus -= 2;
1464.     }
1465. 
1466.     return bonus;
1467. }
1468. 
1469. /*
1470.  * Return damage bonus/penalty based on skill of weapon.
1471.  * Treat restricted weapons as unskilled.
1472.  */
1473. int
1474. weapon_dam_bonus(weapon)
1475. struct obj *weapon;
1476. {
1477.     int type, wep_type, skill, bonus = 0;
1478. 
1479.     wep_type = weapon_type(weapon);
1480.     /* use two weapon skill only if attacking with one of the wielded weapons
1481.      */
1482.     type = (u.twoweap && (weapon == uwep || weapon == uswapwep))
1483.                ? P_TWO_WEAPON_COMBAT
1484.                : wep_type;
1485.     if (type == P_NONE) {
1486.         bonus = 0;
1487.     } else if (type <= P_LAST_WEAPON) {
1488.         switch (P_SKILL(type)) {
1489.         default:
1490.             impossible("weapon_dam_bonus: bad skill %d", P_SKILL(type));
1491.         /* fall through */
1492.         case P_ISRESTRICTED:
1493.         case P_UNSKILLED:
1494.             bonus = -2;
1495.             break;
1496.         case P_BASIC:
1497.             bonus = 0;
1498.             break;
1499.         case P_SKILLED:
1500.             bonus = 1;
1501.             break;
1502.         case P_EXPERT:
1503.             bonus = 2;
1504.             break;
1505.         }
1506.     } else if (type == P_TWO_WEAPON_COMBAT) {
1507.         skill = P_SKILL(P_TWO_WEAPON_COMBAT);
1508.         if (P_SKILL(wep_type) < skill)
1509.             skill = P_SKILL(wep_type);
1510.         switch (skill) {
1511.         default:
1512.         case P_ISRESTRICTED:
1513.         case P_UNSKILLED:
1514.             bonus = -3;
1515.             break;
1516.         case P_BASIC:
1517.             bonus = -1;
1518.             break;
1519.         case P_SKILLED:
1520.             bonus = 0;
1521.             break;
1522.         case P_EXPERT:
1523.             bonus = 1;
1524.             break;
1525.         }
1526.     } else if (type == P_BARE_HANDED_COMBAT) {
1527.         /*
1528.          *        b.h. m.a.
1529.          * unskl:   0  n/a
1530.          * basic:  +1   +3
1531.          * skild:  +1   +4
1532.          * exprt:  +2   +6
1533.          * mastr:  +2   +7
1534.          * grand:  +3   +9
1535.          */
1536.         bonus = P_SKILL(type);
1537.         bonus = max(bonus, P_UNSKILLED) - 1; /* unskilled => 0 */
1538.         bonus = ((bonus + 1) * (martial_bonus() ? 3 : 1)) / 2;
1539.     }
1540. 
1541.     /* KMH -- Riding gives some thrusting damage */
1542.     if (u.usteed && type != P_TWO_WEAPON_COMBAT) {
1543.         switch (P_SKILL(P_RIDING)) {
1544.         case P_ISRESTRICTED:
1545.         case P_UNSKILLED:
1546.             break;
1547.         case P_BASIC:
1548.             break;
1549.         case P_SKILLED:
1550.             bonus += 1;
1551.             break;
1552.         case P_EXPERT:
1553.             bonus += 2;
1554.             break;
1555.         }
1556.     }
1557. 
1558.     return bonus;
1559. }
1560. 
1561. /*
1562.  * Initialize weapon skill array for the game.  Start by setting all
1563.  * skills to restricted, then set the skill for every weapon the
1564.  * hero is holding, finally reading the given array that sets
1565.  * maximums.
1566.  */
1567. void
1568. skill_init(class_skill)
1569. const struct def_skill *class_skill;
1570. {
1571.     struct obj *obj;
1572.     int skmax, skill;
1573. 
1574.     /* initialize skill array; by default, everything is restricted */
1575.     for (skill = 0; skill < P_NUM_SKILLS; skill++) {
1576.         P_SKILL(skill) = P_ISRESTRICTED;
1577.         P_MAX_SKILL(skill) = P_ISRESTRICTED;
1578.         P_ADVANCE(skill) = 0;
1579.     }
1580. 
1581.     /* Set skill for all weapons in inventory to be basic */
1582.     for (obj = invent; obj; obj = obj->nobj) {
1583.         /* don't give skill just because of carried ammo, wait until
1584.            we see the relevant launcher (prevents an archeologist's
1585.            touchstone from inadvertently providing skill in sling) */
1586.         if (is_ammo(obj))
1587.             continue;
1588. 
1589.         skill = weapon_type(obj);
1590.         if (skill != P_NONE)
1591.             P_SKILL(skill) = P_BASIC;
1592.     }
1593. 
1594.     /* set skills for magic */
1595.     if (Role_if(PM_HEALER) || Role_if(PM_MONK)) {
1596.         P_SKILL(P_HEALING_SPELL) = P_BASIC;
1597.     } else if (Role_if(PM_PRIEST)) {
1598.         P_SKILL(P_CLERIC_SPELL) = P_BASIC;
1599.     } else if (Role_if(PM_WIZARD)) {
1600.         P_SKILL(P_ATTACK_SPELL) = P_BASIC;
1601.         P_SKILL(P_ENCHANTMENT_SPELL) = P_BASIC;
1602.     }
1603. 
1604.     /* walk through array to set skill maximums */
1605.     for (; class_skill->skill != P_NONE; class_skill++) {
1606.         skmax = class_skill->skmax;
1607.         skill = class_skill->skill;
1608. 
1609.         P_MAX_SKILL(skill) = skmax;
1610.         if (P_SKILL(skill) == P_ISRESTRICTED) /* skill pre-set */
1611.             P_SKILL(skill) = P_UNSKILLED;
1612.     }
1613. 
1614.     /* High potential fighters already know how to use their hands. */
1615.     if (P_MAX_SKILL(P_BARE_HANDED_COMBAT) > P_EXPERT)
1616.         P_SKILL(P_BARE_HANDED_COMBAT) = P_BASIC;
1617. 
1618.     /* Roles that start with a horse know how to ride it */
1619.     if (urole.petnum == PM_PONY)
1620.         P_SKILL(P_RIDING) = P_BASIC;
1621. 
1622.     /*
1623.      * Make sure we haven't missed setting the max on a skill
1624.      * & set advance
1625.      */
1626.     for (skill = 0; skill < P_NUM_SKILLS; skill++) {
1627.         if (!P_RESTRICTED(skill)) {
1628.             if (P_MAX_SKILL(skill) < P_SKILL(skill)) {
1629.                 impossible("skill_init: curr > max: %s", P_NAME(skill));
1630.                 P_MAX_SKILL(skill) = P_SKILL(skill);
1631.             }
1632.             P_ADVANCE(skill) = practice_needed_to_advance(P_SKILL(skill) - 1);
1633.         }
1634.     }
1635. 
1636.     /* each role has a special spell; allow at least basic for its type
1637.        (despite the function name, this works for spell skills too) */
1638.     unrestrict_weapon_skill(spell_skilltype(urole.spelspec));
1639. }
1640. 
1641. void
1642. setmnotwielded(mon, obj)
1643. register struct monst *mon;
1644. register struct obj *obj;
1645. {
1646.     if (!obj)
1647.         return;
1648.     if (artifact_light(obj) && obj->lamplit) {
1649.         end_burn(obj, FALSE);
1650.         if (canseemon(mon))
1651.             pline("%s in %s %s %s shining.", The(xname(obj)),
1652.                   s_suffix(mon_nam(mon)), mbodypart(mon, HAND),
1653.                   otense(obj, "stop"));
1654.     }
1655.     if (MON_WEP(mon) == obj)
1656.         MON_NOWEP(mon);
1657.     obj->owornmask &= ~W_WEP;
1658. }
1659. 
1660. /*weapon.c*/