Fandom

Wikihack

Source:NetHack 3.4.0/weapon.c

2,034pages on
this wiki
Add New Page
Talk0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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

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

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
1.    /*	SCCS Id: @(#)weapon.c	3.4	2002/02/07	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    /*
6.     *	This module contains code for calculation of "to hit" and damage
7.     *	bonuses for any given weapon used, as well as weapons selection
8.     *	code for monsters.
9.     */
10.   #include "hack.h"
11.   
12.   /* Categories whose names don't come from OBJ_NAME(objects[type])
13.    */
14.   #define PN_BARE_HANDED			(-1)	/* includes martial arts */
15.   #define PN_TWO_WEAPONS			(-2)
16.   #define PN_RIDING			(-3)
17.   #define PN_POLEARMS			(-4)
18.   #define PN_SABER			(-5)
19.   #define PN_HAMMER			(-6)
20.   #define PN_WHIP				(-7)
21.   #define PN_ATTACK_SPELL			(-8)
22.   #define PN_HEALING_SPELL		(-9)
23.   #define PN_DIVINATION_SPELL		(-10)
24.   #define PN_ENCHANTMENT_SPELL		(-11)
25.   #define PN_CLERIC_SPELL			(-12)
26.   #define PN_ESCAPE_SPELL			(-13)
27.   #define PN_MATTER_SPELL			(-14)
28.   
29.   STATIC_DCL void FDECL(give_may_advance_msg, (int));
30.   
31.   #ifndef OVLB
32.   
33.   STATIC_DCL NEARDATA const short skill_names_indices[];
34.   STATIC_DCL NEARDATA const char *odd_skill_names[];
35.   STATIC_DCL NEARDATA const char *barehands_or_martial[];
36.   
37.   #else	/* OVLB */
38.   
39.   STATIC_VAR NEARDATA const short skill_names_indices[P_NUM_SKILLS] = {
40.   	0,                DAGGER,         KNIFE,        AXE,
41.   	PICK_AXE,         SHORT_SWORD,    BROADSWORD,   LONG_SWORD,
42.   	TWO_HANDED_SWORD, SCIMITAR,       PN_SABER,     CLUB,
43.   	MACE,             MORNING_STAR,   FLAIL,
44.   	PN_HAMMER,        QUARTERSTAFF,   PN_POLEARMS,  SPEAR,
45.   	JAVELIN,          TRIDENT,        LANCE,        BOW,
46.   	SLING,            CROSSBOW,       DART,
47.   	SHURIKEN,         BOOMERANG,      PN_WHIP,      UNICORN_HORN,
48.   	PN_ATTACK_SPELL,     PN_HEALING_SPELL,
49.   	PN_DIVINATION_SPELL, PN_ENCHANTMENT_SPELL,
50.   	PN_CLERIC_SPELL,     PN_ESCAPE_SPELL,
51.   	PN_MATTER_SPELL,
52.   	PN_BARE_HANDED,   PN_TWO_WEAPONS,
53.   #ifdef STEED
54.   	PN_RIDING
55.   #endif
56.   };
57.   
58.   /* note: entry [0] isn't used */
59.   STATIC_VAR NEARDATA const char *odd_skill_names[] = {
60.       "no skill",
61.       "bare hands",		/* use barehands_or_martial[] instead */
62.       "two weapon combat",
63.       "riding",
64.       "polearms",
65.       "saber",
66.       "hammer",
67.       "whip",
68.       "attack spells",
69.       "healing spells",
70.       "divination spells",
71.       "enchantment spells",
72.       "clerical spells",
73.       "escape spells",
74.       "matter spells",
75.   };
76.   /* indexed vis `is_martial() */
77.   STATIC_VAR NEARDATA const char *barehands_or_martial[] = {
78.       "bare handed combat", "martial arts"
79.   };
80.   
81.   STATIC_OVL
82.   void give_may_advance_msg(skill)
83.   int skill;
84.   {
85.   	You_feel("more confident in your %sskills.",
86.   		skill == P_NONE ?
87.   			"" :
88.   		skill <= P_LAST_WEAPON ?
89.   			"weapon " :
90.   		skill <= P_LAST_SPELL ?
91.   			"spell casting " :
92.   		"fighting ");
93.   }
94.   
95.   #endif	/* OVLB */
96.   
97.   STATIC_DCL boolean FDECL(can_advance, (int, BOOLEAN_P));
98.   STATIC_DCL boolean FDECL(could_advance, (int));
99.   STATIC_DCL boolean FDECL(peaked_skill, (int));
100.  STATIC_DCL int FDECL(slots_required, (int));
101.  
102.  #ifdef OVL1
103.  
104.  STATIC_DCL char *FDECL(skill_level_name, (int,char *));
105.  STATIC_DCL void FDECL(skill_advance, (int));
106.  
107.  #endif	/* OVL1 */
108.  
109.  #define P_NAME(type) ((skill_names_indices[type] > 0) ? \
110.  		      OBJ_NAME(objects[skill_names_indices[type]]) : \
111.  		      (type == P_BARE_HANDED_COMBAT) ? \
112.  			barehands_or_martial[martial_bonus()] : \
113.  			odd_skill_names[-skill_names_indices[type]])
114.  
115.  #ifdef OVLB
116.  static NEARDATA const char kebabable[] = {
117.  	S_XORN, S_DRAGON, S_JABBERWOCK, S_NAGA, S_GIANT, '\0'
118.  };
119.  
120.  /*
121.   *	hitval returns an integer representing the "to hit" bonuses
122.   *	of "otmp" against the monster.
123.   */
124.  int
125.  hitval(otmp, mon)
126.  struct obj *otmp;
127.  struct monst *mon;
128.  {
129.  	int	tmp = 0;
130.  	struct permonst *ptr = mon->data;
131.  	boolean Is_weapon = (otmp->oclass == WEAPON_CLASS || is_weptool(otmp));
132.  
133.  	if (Is_weapon)
134.  		tmp += otmp->spe;
135.  
136.  /*	Put weapon specific "to hit" bonuses in below:		*/
137.  	tmp += objects[otmp->otyp].oc_hitbon;
138.  
139.  /*	Put weapon vs. monster type "to hit" bonuses in below:	*/
140.  
141.  	/* Blessed weapons used against undead or demons */
142.  	if (Is_weapon && otmp->blessed &&
143.  	   (is_demon(ptr) || is_undead(ptr))) tmp += 2;
144.  
145.  	if (is_spear(otmp) &&
146.  	   index(kebabable, ptr->mlet)) tmp += 2;
147.  
148.  	/* trident is highly effective against swimmers */
149.  	if (otmp->otyp == TRIDENT && is_swimmer(ptr)) {
150.  	   if (is_pool(mon->mx, mon->my)) tmp += 4;
151.  	   else if (ptr->mlet == S_EEL || ptr->mlet == S_SNAKE) tmp += 2;
152.  	}
153.  
154.  	/* Picks used against xorns and earth elementals */
155.  	if (is_pick(otmp) &&
156.  	   (passes_walls(ptr) && thick_skinned(ptr))) tmp += 2;
157.  
158.  #ifdef INVISIBLE_OBJECTS
159.  	/* Invisible weapons against monsters who can't see invisible */
160.  	if (otmp->oinvis && !perceives(ptr)) tmp += 3;
161.  #endif
162.  
163.  	/* Check specially named weapon "to hit" bonuses */
164.  	if (otmp->oartifact) tmp += spec_abon(otmp, mon);
165.  
166.  	return tmp;
167.  }
168.  
169.  /* Historical note: The original versions of Hack used a range of damage
170.   * which was similar to, but not identical to the damage used in Advanced
171.   * Dungeons and Dragons.  I figured that since it was so close, I may as well
172.   * make it exactly the same as AD&D, adding some more weapons in the process.
173.   * This has the advantage that it is at least possible that the player would
174.   * already know the damage of at least some of the weapons.  This was circa
175.   * 1987 and I used the table from Unearthed Arcana until I got tired of typing
176.   * them in (leading to something of an imbalance towards weapons early in
177.   * alphabetical order).  The data structure still doesn't include fields that
178.   * fully allow the appropriate damage to be described (there's no way to say
179.   * 3d6 or 1d6+1) so we add on the extra damage in dmgval() if the weapon
180.   * doesn't do an exact die of damage.
181.   *
182.   * Of course new weapons were added later in the development of Nethack.  No
183.   * AD&D consistency was kept, but most of these don't exist in AD&D anyway.
184.   *
185.   * Second edition AD&D came out a few years later; luckily it used the same
186.   * table.  As of this writing (1999), third edition is in progress but not
187.   * released.  Let's see if the weapon table stays the same.  --KAA
188.   * October 2000: It didn't.  Oh, well.
189.   */
190.  
191.  /*
192.   *	dmgval returns an integer representing the damage bonuses
193.   *	of "otmp" against the monster.
194.   */
195.  int
196.  dmgval(otmp, mon)
197.  struct obj *otmp;
198.  struct monst *mon;
199.  {
200.  	int tmp = 0, otyp = otmp->otyp;
201.  	struct permonst *ptr = mon->data;
202.  	boolean Is_weapon = (otmp->oclass == WEAPON_CLASS || is_weptool(otmp));
203.  
204.  	if (otyp == CREAM_PIE) return 0;
205.  
206.  	if (bigmonst(ptr)) {
207.  	    if (objects[otyp].oc_wldam)
208.  		tmp = rnd(objects[otyp].oc_wldam);
209.  	    switch (otyp) {
210.  		case IRON_CHAIN:
211.  		case CROSSBOW_BOLT:
212.  		case MORNING_STAR:
213.  		case PARTISAN:
214.  		case RUNESWORD:
215.  		case ELVEN_BROADSWORD:
216.  		case BROADSWORD:	tmp++; break;
217.  
218.  		case FLAIL:
219.  		case RANSEUR:
220.  		case VOULGE:		tmp += rnd(4); break;
221.  
222.  		case ACID_VENOM:
223.  		case HALBERD:
224.  		case SPETUM:		tmp += rnd(6); break;
225.  
226.  		case BATTLE_AXE:
227.  		case BARDICHE:
228.  		case TRIDENT:		tmp += d(2,4); break;
229.  
230.  		case TSURUGI:
231.  		case DWARVISH_MATTOCK:
232.  		case TWO_HANDED_SWORD:	tmp += d(2,6); break;
233.  	    }
234.  	} else {
235.  	    if (objects[otyp].oc_wsdam)
236.  		tmp = rnd(objects[otyp].oc_wsdam);
237.  	    switch (otyp) {
238.  		case IRON_CHAIN:
239.  		case CROSSBOW_BOLT:
240.  		case MACE:
241.  		case WAR_HAMMER:
242.  		case FLAIL:
243.  		case SPETUM:
244.  		case TRIDENT:		tmp++; break;
245.  
246.  		case BATTLE_AXE:
247.  		case BARDICHE:
248.  		case BILL_GUISARME:
249.  		case GUISARME:
250.  		case LUCERN_HAMMER:
251.  		case MORNING_STAR:
252.  		case RANSEUR:
253.  		case BROADSWORD:
254.  		case ELVEN_BROADSWORD:
255.  		case RUNESWORD:
256.  		case VOULGE:		tmp += rnd(4); break;
257.  
258.  		case ACID_VENOM:	tmp += rnd(6); break;
259.  	    }
260.  	}
261.  	if (Is_weapon) {
262.  		tmp += otmp->spe;
263.  		/* negative enchantment mustn't produce negative damage */
264.  		if (tmp < 0) tmp = 0;
265.  	}
266.  
267.  	if (objects[otyp].oc_material <= LEATHER && thick_skinned(ptr))
268.  		/* thick skinned/scaled creatures don't feel it */
269.  		tmp = 0;
270.  	if (ptr == &mons[PM_SHADE] && objects[otyp].oc_material != SILVER)
271.  		tmp = 0;
272.  
273.  	/* "very heavy iron ball"; weight increase is in increments of 160 */
274.  	if (otyp == HEAVY_IRON_BALL && tmp > 0) {
275.  	    int wt = (int)objects[HEAVY_IRON_BALL].oc_weight;
276.  
277.  	    if ((int)otmp->owt > wt) {
278.  		wt = ((int)otmp->owt - wt) / 160;
279.  		tmp += rnd(4 * wt);
280.  		if (tmp > 25) tmp = 25;	/* objects[].oc_wldam */
281.  	    }
282.  	}
283.  
284.  /*	Put weapon vs. monster type damage bonuses in below:	*/
285.  	if (Is_weapon || otmp->oclass == GEM_CLASS ||
286.  		otmp->oclass == BALL_CLASS || otmp->oclass == CHAIN_CLASS) {
287.  	    int bonus = 0;
288.  
289.  #ifdef STEED
290.  		/* KMH -- Lances are especially made for riding */
291.  		if (otmp == uwep && u.usteed &&
292.  				objects[otmp->otyp].oc_skill == P_LANCE)
293.  			bonus += d(2,10);
294.  #endif
295.  
296.  	    if (otmp->blessed && (is_undead(ptr) || is_demon(ptr)))
297.  		bonus += rnd(4);
298.  	    if (is_axe(otmp) && is_wooden(ptr))
299.  		bonus += rnd(4);
300.  	    if (objects[otyp].oc_material == SILVER && hates_silver(ptr))
301.  		bonus += rnd(20);
302.  
303.  	    /* if the weapon is going to get a double damage bonus, adjust
304.  	       this bonus so that effectively it's added after the doubling */
305.  	    if (bonus > 1 && otmp->oartifact && spec_dbon(otmp, mon, 25) >= 25)
306.  		bonus = (bonus + 1) / 2;
307.  
308.  	    tmp += bonus;
309.  	}
310.  
311.  	if (tmp > 0) {
312.  		/* It's debateable whether a rusted blunt instrument
313.  		   should do less damage than a pristine one, since
314.  		   it will hit with essentially the same impact, but
315.  		   there ought to some penalty for using damaged gear
316.  		   so always subtract erosion even for blunt weapons. */
317.  		tmp -= greatest_erosion(otmp);
318.  		if (tmp < 1) tmp = 1;
319.  	}
320.  
321.  	return(tmp);
322.  }
323.  
324.  #endif /* OVLB */
325.  #ifdef OVL0
326.  
327.  STATIC_DCL struct obj *FDECL(oselect, (struct monst *,int));
328.  #define Oselect(x)	if ((otmp = oselect(mtmp, x)) != 0) return(otmp);
329.  
330.  STATIC_OVL struct obj *
331.  oselect(mtmp, x)
332.  struct monst *mtmp;
333.  int x;
334.  {
335.  	struct obj *otmp;
336.  
337.  	for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) {
338.  	    if (otmp->otyp == x &&
339.  		    /* never select non-cockatrice corpses */
340.  		    !((x == CORPSE || x == EGG) &&
341.  			!touch_petrifies(&mons[otmp->corpsenm])) &&
342.  		    (!otmp->oartifact || touch_artifact(otmp,mtmp)))
343.  		return otmp;
344.  	}
345.  	return (struct obj *)0;
346.  }
347.  
348.  static NEARDATA const int rwep[] =
349.  {	DWARVISH_SPEAR, SILVER_SPEAR, ELVEN_SPEAR, SPEAR, ORCISH_SPEAR,
350.  	JAVELIN, SHURIKEN, YA, SILVER_ARROW, ELVEN_ARROW, ARROW,
351.  	ORCISH_ARROW, CROSSBOW_BOLT, SILVER_DAGGER, ELVEN_DAGGER, DAGGER,
352.  	ORCISH_DAGGER, KNIFE, FLINT, ROCK, LOADSTONE, LUCKSTONE, DART,
353.  	/* BOOMERANG, */ CREAM_PIE
354.  	/* note: CREAM_PIE should NOT be #ifdef KOPS */
355.  };
356.  
357.  static NEARDATA const int pwep[] =
358.  {	HALBERD, BARDICHE, SPETUM, BILL_GUISARME, VOULGE, RANSEUR, GUISARME,
359.  	GLAIVE, LUCERN_HAMMER, BEC_DE_CORBIN, FAUCHARD, PARTISAN, LANCE
360.  };
361.  
362.  static struct obj *propellor;
363.  
364.  struct obj *
365.  select_rwep(mtmp)	/* select a ranged weapon for the monster */
366.  register struct monst *mtmp;
367.  {
368.  	register struct obj *otmp;
369.  	int i;
370.  
371.  #ifdef KOPS
372.  	char mlet = mtmp->data->mlet;
373.  #endif
374.  
375.  	propellor = &zeroobj;
376.  	Oselect(EGG); /* cockatrice egg */
377.  #ifdef KOPS
378.  	if(mlet == S_KOP)	/* pies are first choice for Kops */
379.  	    Oselect(CREAM_PIE);
380.  #endif
381.  	if(throws_rocks(mtmp->data))	/* ...boulders for giants */
382.  	    Oselect(BOULDER);
383.  
384.  	/* Select polearms first; they do more damage and aren't expendable */
385.  	/* The limit of 13 here is based on the monster polearm range limit
386.  	 * (defined as 5 in mthrowu.c).  5 corresponds to a distance of 2 in
387.  	 * one direction and 1 in another; one space beyond that would be 3 in
388.  	 * one direction and 2 in another; 3^2+2^2=13.
389.  	 */
390.  	if (dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 13 && couldsee(mtmp->mx, mtmp->my)) {
391.  	    for (i = 0; i < SIZE(pwep); i++) {
392.  		/* Only strong monsters can wield big (esp. long) weapons.
393.  		 * Big weapon is basically the same as bimanual.
394.  		 * All monsters can wield the remaining weapons.
395.  		 */
396.  		if (((strongmonst(mtmp->data) && (mtmp->misc_worn_check & W_ARMS) == 0)
397.  			|| !objects[pwep[i]].oc_bimanual) &&
398.  		    (objects[pwep[i]].oc_material != SILVER
399.  			|| !hates_silver(mtmp->data))) {
400.  		    if ((otmp = oselect(mtmp, pwep[i])) != 0) {
401.  			propellor = otmp; /* force the monster to wield it */
402.  			return otmp;
403.  		    }
404.  		}
405.  	    }
406.  	}
407.  
408.  	/*
409.  	 * other than these two specific cases, always select the
410.  	 * most potent ranged weapon to hand.
411.  	 */
412.  	for (i = 0; i < SIZE(rwep); i++) {
413.  	    int prop;
414.  
415.  	    /* shooting gems from slings; this goes just before the darts */
416.  	    /* (shooting rocks is already handled via the rwep[] ordering) */
417.  	    if (rwep[i] == DART && !likes_gems(mtmp->data) &&
418.  		    m_carrying(mtmp, SLING)) {		/* propellor */
419.  		for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
420.  		    if (otmp->oclass == GEM_CLASS &&
421.  			    (otmp->otyp != LOADSTONE || !otmp->cursed)) {
422.  			propellor = m_carrying(mtmp, SLING);
423.  			return otmp;
424.  		    }
425.  	    }
426.  
427.  		/* KMH -- This belongs here so darts will work */
428.  	    propellor = &zeroobj;
429.  
430.  	    prop = (objects[rwep[i]]).oc_skill;
431.  	    if (prop < 0) {
432.  		switch (-prop) {
433.  		case P_BOW:
434.  		  propellor = (oselect(mtmp, YUMI));
435.  		  if (!propellor) propellor = (oselect(mtmp, ELVEN_BOW));
436.  		  if (!propellor) propellor = (oselect(mtmp, BOW));
437.  		  if (!propellor) propellor = (oselect(mtmp, ORCISH_BOW));
438.  		  break;
439.  		case P_SLING:
440.  		  propellor = (oselect(mtmp, SLING));
441.  		  break;
442.  		case P_CROSSBOW:
443.  		  propellor = (oselect(mtmp, CROSSBOW));
444.  		}
445.  		if ((otmp = MON_WEP(mtmp)) && otmp->cursed && otmp != propellor
446.  				&& mtmp->weapon_check == NO_WEAPON_WANTED)
447.  			propellor = 0;
448.  	    }
449.  	    /* propellor = obj, propellor to use
450.  	     * propellor = &zeroobj, doesn't need a propellor
451.  	     * propellor = 0, needed one and didn't have one
452.  	     */
453.  	    if (propellor != 0) {
454.  		/* Note: cannot use m_carrying for loadstones, since it will
455.  		 * always select the first object of a type, and maybe the
456.  		 * monster is carrying two but only the first is unthrowable.
457.  		 */
458.  		if (rwep[i] != LOADSTONE) {
459.  			/* Don't throw a cursed weapon-in-hand or an artifact */
460.  			if ((otmp = oselect(mtmp, rwep[i])) && !otmp->oartifact
461.  			    && (!otmp->cursed || otmp != MON_WEP(mtmp)))
462.  				return(otmp);
463.  		} else for(otmp=mtmp->minvent; otmp; otmp=otmp->nobj) {
464.  		    if (otmp->otyp == LOADSTONE && !otmp->cursed)
465.  			return otmp;
466.  		}
467.  	    }
468.  	  }
469.  
470.  	/* failure */
471.  	return (struct obj *)0;
472.  }
473.  
474.  /* Weapons in order of preference */
475.  static NEARDATA short hwep[] = {
476.  	  CORPSE,  /* cockatrice corpse */
477.  	  TSURUGI, RUNESWORD, DWARVISH_MATTOCK, TWO_HANDED_SWORD, BATTLE_AXE,
478.  	  KATANA, UNICORN_HORN, CRYSKNIFE, TRIDENT, LONG_SWORD,
479.  	  ELVEN_BROADSWORD, BROADSWORD, SCIMITAR, SILVER_SABER,
480.  	  MORNING_STAR, ELVEN_SHORT_SWORD, DWARVISH_SHORT_SWORD, SHORT_SWORD,
481.  	  ORCISH_SHORT_SWORD, MACE, AXE, DWARVISH_SPEAR, SILVER_SPEAR,
482.  	  ELVEN_SPEAR, SPEAR, ORCISH_SPEAR, FLAIL, BULLWHIP, QUARTERSTAFF,
483.  	  JAVELIN, AKLYS, CLUB, PICK_AXE,
484.  #ifdef KOPS
485.  	  RUBBER_HOSE,
486.  #endif /* KOPS */
487.  	  WAR_HAMMER, SILVER_DAGGER, ELVEN_DAGGER, DAGGER, ORCISH_DAGGER,
488.  	  ATHAME, SCALPEL, KNIFE, WORM_TOOTH
489.  	};
490.  
491.  struct obj *
492.  select_hwep(mtmp)	/* select a hand to hand weapon for the monster */
493.  register struct monst *mtmp;
494.  {
495.  	register struct obj *otmp;
496.  	register int i;
497.  	boolean strong = strongmonst(mtmp->data);
498.  	boolean wearing_shield = (mtmp->misc_worn_check & W_ARMS) != 0;
499.  
500.  	/* prefer artifacts to everything else */
501.  	for(otmp=mtmp->minvent; otmp; otmp = otmp->nobj) {
502.  		if (otmp->oclass == WEAPON_CLASS
503.  			&& otmp->oartifact && touch_artifact(otmp,mtmp)
504.  			&& ((strong && !wearing_shield)
505.  			    || !objects[otmp->otyp].oc_bimanual))
506.  		    return otmp;
507.  	}
508.  
509.  	if(is_giant(mtmp->data))	/* giants just love to use clubs */
510.  	    Oselect(CLUB);
511.  
512.  	/* only strong monsters can wield big (esp. long) weapons */
513.  	/* big weapon is basically the same as bimanual */
514.  	/* all monsters can wield the remaining weapons */
515.  	for (i = 0; i < SIZE(hwep); i++) {
516.  	    if (hwep[i] == CORPSE && !(mtmp->misc_worn_check & W_ARMG))
517.  		continue;
518.  	    if (((strong && !wearing_shield)
519.  			|| !objects[hwep[i]].oc_bimanual) &&
520.  		    (objects[hwep[i]].oc_material != SILVER
521.  			|| !hates_silver(mtmp->data)))
522.  		Oselect(hwep[i]);
523.  	}
524.  
525.  	/* failure */
526.  	return (struct obj *)0;
527.  }
528.  
529.  /* Called after polymorphing a monster, robbing it, etc....  Monsters
530.   * otherwise never unwield stuff on their own.  Shouldn't print messages.
531.   */
532.  void
533.  possibly_unwield(mon)
534.  register struct monst *mon;
535.  {
536.  	register struct obj *obj;
537.  	struct obj *mw_tmp;
538.  
539.  	if (!(mw_tmp = MON_WEP(mon)))
540.  		return;
541.  	for(obj=mon->minvent; obj; obj=obj->nobj)
542.  		if (obj == mw_tmp) break;
543.  	if (!obj) { /* The weapon was stolen or destroyed */
544.  		MON_NOWEP(mon);
545.  		mon->weapon_check = NEED_WEAPON;
546.  		return;
547.  	}
548.  	if (!attacktype(mon->data, AT_WEAP)) {
549.  		setmnotwielded(mon, mw_tmp);
550.  		MON_NOWEP(mon);
551.  		mon->weapon_check = NO_WEAPON_WANTED;
552.  		obj_extract_self(obj);
553.  		/* flooreffects unnecessary, can't wield boulders */
554.  		place_object(obj, mon->mx, mon->my);
555.  		stackobj(obj);
556.  		if (cansee(mon->mx, mon->my)) {
557.  			pline("%s drops %s.", Monnam(mon),
558.  				distant_name(obj, doname));
559.  			newsym(mon->mx, mon->my);
560.  		}
561.  		return;
562.  	}
563.  	/* The remaining case where there is a change is where a monster
564.  	 * is polymorphed into a stronger/weaker monster with a different
565.  	 * choice of weapons.  This has no parallel for players.  It can
566.  	 * be handled by waiting until mon_wield_item is actually called.
567.  	 * Though the monster still wields the wrong weapon until then,
568.  	 * this is OK since the player can't see it.  (FIXME: Not okay since
569.  	 * probing can reveal it.)
570.  	 * Note that if there is no change, setting the check to NEED_WEAPON
571.  	 * is harmless.
572.  	 * Possible problem: big monster with big cursed weapon gets
573.  	 * polymorphed into little monster.  But it's not quite clear how to
574.  	 * handle this anyway....
575.  	 */
576.  	if (!(mw_tmp->cursed && mon->weapon_check == NO_WEAPON_WANTED))
577.  	    mon->weapon_check = NEED_WEAPON;
578.  }
579.  
580.  /* Let a monster try to wield a weapon, based on mon->weapon_check.
581.   * Returns 1 if the monster took time to do it, 0 if it did not.
582.   */
583.  int
584.  mon_wield_item(mon)
585.  register struct monst *mon;
586.  {
587.  	struct obj *obj;
588.  
589.  	/* This case actually should never happen */
590.  	if (mon->weapon_check == NO_WEAPON_WANTED) return 0;
591.  	switch(mon->weapon_check) {
592.  		case NEED_HTH_WEAPON:
593.  			obj = select_hwep(mon);
594.  			break;
595.  		case NEED_RANGED_WEAPON:
596.  			(void)select_rwep(mon);
597.  			obj = propellor;
598.  			break;
599.  		case NEED_PICK_AXE:
600.  			obj = m_carrying(mon, PICK_AXE);
601.  			/* KMH -- allow other picks */
602.  			if (!obj) obj = m_carrying(mon, DWARVISH_MATTOCK);
603.  			break;
604.  		default: impossible("weapon_check %d for %s?",
605.  				mon->weapon_check, mon_nam(mon));
606.  			return 0;
607.  	}
608.  	if (obj && obj != &zeroobj) {
609.  		struct obj *mw_tmp = MON_WEP(mon);
610.  		if (mw_tmp && mw_tmp->otyp == obj->otyp) {
611.  		/* already wielding it */
612.  			mon->weapon_check = NEED_WEAPON;
613.  			return 0;
614.  		}
615.  		/* Actually, this isn't necessary--as soon as the monster
616.  		 * wields the weapon, the weapon welds itself, so the monster
617.  		 * can know it's cursed and needn't even bother trying.
618.  		 * Still....
619.  		 */
620.  		if (mw_tmp && mw_tmp->cursed && mw_tmp->otyp != CORPSE) {
621.  		    if (canseemon(mon)) {
622.  			char welded_buf[BUFSZ];
623.  			const char *mon_hand = mbodypart(mon, HAND);
624.  
625.  			if (bimanual(mw_tmp)) mon_hand = makeplural(mon_hand);
626.  			Sprintf(welded_buf, "%s welded to %s %s",
627.  				otense(mw_tmp, "are"),
628.  				mhis(mon), mon_hand);
629.  
630.  			if (obj->otyp == PICK_AXE) {
631.  			    pline("Since %s weapon%s %s,",
632.  				  s_suffix(mon_nam(mon)),
633.  				  plur(mw_tmp->quan), welded_buf);
634.  			    pline("%s cannot wield that %s.",
635.  				mon_nam(mon), xname(obj));
636.  			} else {
637.  			    pline("%s tries to wield %s.", Monnam(mon),
638.  				doname(obj));
639.  			    pline("%s %s %s!",
640.  				  s_suffix(Monnam(mon)),
641.  				  xname(mw_tmp), welded_buf);
642.  			}
643.  			mw_tmp->bknown = 1;
644.  		    }
645.  		    mon->weapon_check = NO_WEAPON_WANTED;
646.  		    return 1;
647.  		}
648.  		mon->mw = obj;		/* wield obj */
649.  		setmnotwielded(mon, mw_tmp);
650.  		mon->weapon_check = NEED_WEAPON;
651.  		if (canseemon(mon)) {
652.  		    pline("%s wields %s!", Monnam(mon), doname(obj));
653.  		    if (obj->cursed && obj->otyp != CORPSE) {
654.  			pline("%s %s to %s %s!",
655.  			    Tobjnam(obj, "weld"),
656.  			    is_plural(obj) ? "themselves" : "itself",
657.  			    s_suffix(mon_nam(mon)), mbodypart(mon,HAND));
658.  			obj->bknown = 1;
659.  		    }
660.  		}
661.  		if (artifact_light(obj) && !obj->lamplit) {
662.  		    begin_burn(obj, FALSE);
663.  		    if (canseemon(mon))
664.  			pline("%s brilliantly in %s %s!",
665.  			    Tobjnam(obj, "glow"), 
666.  			    s_suffix(mon_nam(mon)), mbodypart(mon,HAND));
667.  		}
668.  		obj->owornmask = W_WEP;
669.  		return 1;
670.  	}
671.  	mon->weapon_check = NEED_WEAPON;
672.  	return 0;
673.  }
674.  
675.  int
676.  abon()		/* attack bonus for strength & dexterity */
677.  {
678.  	int sbon;
679.  	int str = ACURR(A_STR), dex = ACURR(A_DEX);
680.  
681.  	if (Upolyd) return(adj_lev(&mons[u.umonnum]) - 3);
682.  	if (str < 6) sbon = -2;
683.  	else if (str < 8) sbon = -1;
684.  	else if (str < 17) sbon = 0;
685.  	else if (str <= STR18(50)) sbon = 1;	/* up to 18/50 */
686.  	else if (str < STR18(100)) sbon = 2;
687.  	else sbon = 3;
688.  
689.  /* Game tuning kludge: make it a bit easier for a low level character to hit */
690.  	sbon += (u.ulevel < 3) ? 1 : 0;
691.  
692.  	if (dex < 4) return(sbon-3);
693.  	else if (dex < 6) return(sbon-2);
694.  	else if (dex < 8) return(sbon-1);
695.  	else if (dex < 14) return(sbon);
696.  	else return(sbon + dex-14);
697.  }
698.  
699.  #endif /* OVL0 */
700.  #ifdef OVL1
701.  
702.  int
703.  dbon()		/* damage bonus for strength */
704.  {
705.  	int str = ACURR(A_STR);
706.  
707.  	if (Upolyd) return(0);
708.  
709.  	if (str < 6) return(-1);
710.  	else if (str < 16) return(0);
711.  	else if (str < 18) return(1);
712.  	else if (str == 18) return(2);		/* up to 18 */
713.  	else if (str <= STR18(75)) return(3);		/* up to 18/75 */
714.  	else if (str <= STR18(90)) return(4);		/* up to 18/90 */
715.  	else if (str < STR18(100)) return(5);		/* up to 18/99 */
716.  	else return(6);
717.  }
718.  
719.  
720.  /* copy the skill level name into the given buffer */
721.  STATIC_OVL char *
722.  skill_level_name(skill, buf)
723.  int skill;
724.  char *buf;
725.  {
726.      const char *ptr;
727.  
728.      switch (P_SKILL(skill)) {
729.  	case P_UNSKILLED:    ptr = "Unskilled"; break;
730.  	case P_BASIC:	     ptr = "Basic";     break;
731.  	case P_SKILLED:	     ptr = "Skilled";   break;
732.  	case P_EXPERT:	     ptr = "Expert";    break;
733.  	/* these are for unarmed combat/martial arts only */
734.  	case P_MASTER:	     ptr = "Master";    break;
735.  	case P_GRAND_MASTER: ptr = "Grand Master"; break;
736.  	default:	     ptr = "Unknown";	break;
737.      }
738.      Strcpy(buf, ptr);
739.      return buf;
740.  }
741.  
742.  /* return the # of slots required to advance the skill */
743.  STATIC_OVL int
744.  slots_required(skill)
745.  int skill;
746.  {
747.      int tmp = P_SKILL(skill);
748.  
749.      /* The more difficult the training, the more slots it takes.
750.       *	unskilled -> basic	1
751.       *	basic -> skilled	2
752.       *	skilled -> expert	3
753.       */
754.      if (skill <= P_LAST_WEAPON || skill == P_TWO_WEAPON_COMBAT)
755.  	return tmp;
756.  
757.      /* Fewer slots used up for unarmed or martial.
758.       *	unskilled -> basic	1
759.       *	basic -> skilled	1
760.       *	skilled -> expert	2
761.       *	expert -> master	2
762.       *	master -> grand master	3
763.       */
764.      return (tmp + 1) / 2;
765.  }
766.  
767.  /* return true if this skill can be advanced */
768.  /*ARGSUSED*/
769.  STATIC_OVL boolean
770.  can_advance(skill, speedy)
771.  int skill;
772.  boolean speedy;
773.  {
774.      return !P_RESTRICTED(skill)
775.  	    && P_SKILL(skill) < P_MAX_SKILL(skill) && (
776.  #ifdef WIZARD
777.  	    (wizard && speedy) ||
778.  #endif
779.  	    (P_ADVANCE(skill) >=
780.  		(unsigned) practice_needed_to_advance(P_SKILL(skill))
781.  	    && u.skills_advanced < P_SKILL_LIMIT
782.  	    && u.weapon_slots >= slots_required(skill)));
783.  }
784.  
785.  /* return true if this skill could be advanced if more slots were available */
786.  STATIC_OVL boolean
787.  could_advance(skill)
788.  int skill;
789.  {
790.      return !P_RESTRICTED(skill)
791.  	    && P_SKILL(skill) < P_MAX_SKILL(skill) && (
792.  	    (P_ADVANCE(skill) >=
793.  		(unsigned) practice_needed_to_advance(P_SKILL(skill))
794.  	    && u.skills_advanced < P_SKILL_LIMIT));
795.  }
796.  
797.  /* return true if this skill has reached its maximum and there's been enough
798.     practice to become eligible for the next step if that had been possible */
799.  STATIC_OVL boolean
800.  peaked_skill(skill)
801.  int skill;
802.  {
803.      return !P_RESTRICTED(skill)
804.  	    && P_SKILL(skill) >= P_MAX_SKILL(skill) && (
805.  	    (P_ADVANCE(skill) >=
806.  		(unsigned) practice_needed_to_advance(P_SKILL(skill))));
807.  }
808.  
809.  STATIC_OVL void
810.  skill_advance(skill)
811.  int skill;
812.  {
813.      u.weapon_slots -= slots_required(skill);
814.      P_SKILL(skill)++;
815.      u.skill_record[u.skills_advanced++] = skill;
816.      /* subtly change the advance message to indicate no more advancement */
817.      You("are now %s skilled in %s.",
818.  	P_SKILL(skill) >= P_MAX_SKILL(skill) ? "most" : "more",
819.  	P_NAME(skill));
820.  }
821.  
822.  static struct skill_range {
823.  	short first, last;
824.  	const char *name;
825.  } skill_ranges[] = {
826.      { P_FIRST_H_TO_H, P_LAST_H_TO_H, "Fighting Skills" },
827.      { P_FIRST_WEAPON, P_LAST_WEAPON, "Weapon Skills" },
828.      { P_FIRST_SPELL,  P_LAST_SPELL,  "Spellcasting Skills" },
829.  };
830.  
831.  /*
832.   * The `#enhance' extended command.  What we _really_ would like is
833.   * to keep being able to pick things to advance until we couldn't any
834.   * more.  This is currently not possible -- the menu code has no way
835.   * to call us back for instant action.  Even if it did, we would also need
836.   * to be able to update the menu since selecting one item could make
837.   * others unselectable.
838.   */
839.  int
840.  enhance_weapon_skill()
841.  {
842.      int pass, i, n, len, longest,
843.  	to_advance, eventually_advance, maxxed_cnt;
844.      char buf[BUFSZ], sklnambuf[BUFSZ];
845.      const char *prefix;
846.      menu_item *selected;
847.      anything any;
848.      winid win;
849.      boolean speedy = FALSE;
850.  
851.  #ifdef WIZARD
852.  	if (wizard && yn("Advance skills without practice?") == 'y')
853.  	    speedy = TRUE;
854.  #endif
855.  
856.  	do {
857.  	    /* find longest available skill name, count those that can advance */
858.  	    to_advance = eventually_advance = maxxed_cnt = 0;
859.  	    for (longest = 0, i = 0; i < P_NUM_SKILLS; i++) {
860.  		if (P_RESTRICTED(i)) continue;
861.  		if ((len = strlen(P_NAME(i))) > longest)
862.  		    longest = len;
863.  		if (can_advance(i, speedy)) to_advance++;
864.  		else if (could_advance(i)) eventually_advance++;
865.  		else if (peaked_skill(i)) maxxed_cnt++;
866.  	    }
867.  
868.  	    win = create_nhwindow(NHW_MENU);
869.  	    start_menu(win);
870.  
871.  	    /* start with a legend if any entries will be annotated
872.  	       with "*" or "#" below */
873.  	    if (eventually_advance > 0 || maxxed_cnt > 0) {
874.  		any.a_int = 0;
875.  		if (eventually_advance > 0) {
876.  		    Sprintf(buf,
877.  			    "(Skill%s flagged by \"*\" may be enhanced %s.)",
878.  			    plur(eventually_advance),
879.  			    (u.ulevel < MAXULEV) ?
880.  				"when you're more experienced" :
881.  				"if skill slots become available");
882.  		    add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
883.  			     buf, MENU_UNSELECTED);
884.  		}
885.  		if (maxxed_cnt > 0) {
886.  		    Sprintf(buf,
887.  		  "(Skill%s flagged by \"#\" cannot be enhanced any further.)",
888.  			    plur(maxxed_cnt));
889.  		    add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
890.  			     buf, MENU_UNSELECTED);
891.  		}
892.  		add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
893.  			     "", MENU_UNSELECTED);
894.  	    }
895.  
896.  	    /* List the skills, making ones that could be advanced
897.  	       selectable.  List the miscellaneous skills first.
898.  	       Possible future enhancement:  list spell skills before
899.  	       weapon skills for spellcaster roles. */
900.  	  for (pass = 0; pass < SIZE(skill_ranges); pass++)
901.  	    for (i = skill_ranges[pass].first;
902.  		 i <= skill_ranges[pass].last; i++) {
903.  		/* Print headings for skill types */
904.  		any.a_void = 0;
905.  		if (i == skill_ranges[pass].first)
906.  		    add_menu(win, NO_GLYPH, &any, 0, 0, ATR_BOLD,
907.  			     skill_ranges[pass].name, MENU_UNSELECTED);
908.  
909.  		if (P_RESTRICTED(i)) continue;
910.  		/*
911.  		 * Sigh, this assumes a monospaced font unless
912.  		 * iflags.menu_tab_sep is set in which case it puts
913.  		 * tabs between columns.
914.  		 * The 12 is the longest skill level name.
915.  		 * The "    " is room for a selection letter and dash, "a - ".
916.  		 */
917.  		if (can_advance(i, speedy))
918.  		    prefix = "";	/* will be preceded by menu choice */
919.  		else if (could_advance(i))
920.  		    prefix = "  * ";
921.  		else if (peaked_skill(i))
922.  		    prefix = "  # ";
923.  		else
924.  		    prefix = (to_advance + eventually_advance +
925.  				maxxed_cnt > 0) ? "    " : "";
926.  		(void) skill_level_name(i, sklnambuf);
927.  #ifdef WIZARD
928.  		if (wizard) {
929.  		    if (!iflags.menu_tab_sep)
930.  			Sprintf(buf, " %s%-*s %-12s %5d(%4d)",
931.  			    prefix, longest, P_NAME(i), sklnambuf,
932.  			    P_ADVANCE(i),
933.  			    practice_needed_to_advance(P_SKILL(i)));
934.  		    else
935.  			Sprintf(buf, " %s%s\t%s\t%5d(%4d)",
936.  			    prefix, P_NAME(i), sklnambuf,
937.  			    P_ADVANCE(i),
938.  			    practice_needed_to_advance(P_SKILL(i)));
939.  		 } else
940.  #endif
941.  		{
942.  		    if (!iflags.menu_tab_sep)
943.  			Sprintf(buf, " %s %-*s [%s]",
944.  			    prefix, longest, P_NAME(i), sklnambuf);
945.  		    else
946.  			Sprintf(buf, " %s%s\t[%s]",
947.  			    prefix, P_NAME(i), sklnambuf);
948.  		}
949.  		any.a_int = can_advance(i, speedy) ? i+1 : 0;
950.  		add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
951.  			 buf, MENU_UNSELECTED);
952.  	    }
953.  
954.  	    Strcpy(buf, (to_advance > 0) ? "Pick a skill to advance:" :
955.  					   "Current skills:");
956.  #ifdef WIZARD
957.  	    if (wizard && !speedy)
958.  		Sprintf(eos(buf), "  (%d slot%s available)",
959.  			u.weapon_slots, plur(u.weapon_slots));
960.  #endif
961.  	    end_menu(win, buf);
962.  	    n = select_menu(win, to_advance ? PICK_ONE : PICK_NONE, &selected);
963.  	    destroy_nhwindow(win);
964.  	    if (n > 0) {
965.  		n = selected[0].item.a_int - 1;	/* get item selected */
966.  		free((genericptr_t)selected);
967.  		skill_advance(n);
968.  		/* check for more skills able to advance, if so then .. */
969.  		for (n = i = 0; i < P_NUM_SKILLS; i++) {
970.  		    if (can_advance(i, speedy)) {
971.  			if (!speedy) You_feel("you could be more dangerous!");
972.  			n++;
973.  			break;
974.  		    }
975.  		}
976.  	    }
977.  	} while (speedy && n > 0);
978.  	return 0;
979.  }
980.  
981.  /*
982.   * Change from restricted to unrestricted, allowing P_BASIC as max.  This
983.   * function may be called with with P_NONE.  Used in pray.c.
984.   */
985.  void
986.  unrestrict_weapon_skill(skill)
987.  int skill;
988.  {
989.      if (skill < P_NUM_SKILLS && P_RESTRICTED(skill)) {
990.  	P_SKILL(skill) = P_UNSKILLED;
991.  	P_MAX_SKILL(skill) = P_BASIC;
992.  	P_ADVANCE(skill) = 0;
993.      }
994.  }
995.  
996.  #endif /* OVL1 */
997.  #ifdef OVLB
998.  
999.  void
1000. use_skill(skill,degree)
1001. int skill;
1002. int degree;
1003. {
1004.     boolean advance_before;
1005. 
1006.     if (skill != P_NONE && !P_RESTRICTED(skill)) {
1007. 	advance_before = can_advance(skill, FALSE);
1008. 	P_ADVANCE(skill)+=degree;
1009. 	if (!advance_before && can_advance(skill, FALSE))
1010. 	    give_may_advance_msg(skill);
1011.     }
1012. }
1013. 
1014. void
1015. add_weapon_skill(n)
1016. int n;	/* number of slots to gain; normally one */
1017. {
1018.     int i, before, after;
1019. 
1020.     for (i = 0, before = 0; i < P_NUM_SKILLS; i++)
1021. 	if (can_advance(i, FALSE)) before++;
1022.     u.weapon_slots += n;
1023.     for (i = 0, after = 0; i < P_NUM_SKILLS; i++)
1024. 	if (can_advance(i, FALSE)) after++;
1025.     if (before < after)
1026. 	give_may_advance_msg(P_NONE);
1027. }
1028. 
1029. void
1030. lose_weapon_skill(n)
1031. int n;	/* number of slots to lose; normally one */
1032. {
1033.     int skill;
1034. 
1035.     while (--n >= 0) {
1036. 	/* deduct first from unused slots, then from last placed slot, if any */
1037. 	if (u.weapon_slots) {
1038. 	    u.weapon_slots--;
1039. 	} else if (u.skills_advanced) {
1040. 	    skill = u.skill_record[--u.skills_advanced];
1041. 	    if (P_SKILL(skill) <= P_UNSKILLED)
1042. 		panic("lose_weapon_skill (%d)", skill);
1043. 	    P_SKILL(skill)--;	/* drop skill one level */
1044. 	    /* Lost skill might have taken more than one slot; refund rest. */
1045. 	    u.weapon_slots = slots_required(skill) - 1;
1046. 	    /* It might now be possible to advance some other pending
1047. 	       skill by using the refunded slots, but giving a message
1048. 	       to that effect would seem pretty confusing.... */
1049. 	}
1050.     }
1051. }
1052. 
1053. int
1054. weapon_type(obj)
1055. struct obj *obj;
1056. {
1057. 	/* KMH -- now uses the object table */
1058. 	int type;
1059. 
1060. 	if (!obj)
1061. 		/* Not using a weapon */
1062. 		return (P_BARE_HANDED_COMBAT);
1063. 	if (obj->oclass != WEAPON_CLASS && obj->oclass != TOOL_CLASS &&
1064. 	    obj->oclass != GEM_CLASS)
1065. 		/* Not a weapon, weapon-tool, or ammo */
1066. 		return (P_NONE);
1067. 	type = objects[obj->otyp].oc_skill;
1068. 	return ((type < 0) ? -type : type);
1069. }
1070. 
1071. int
1072. uwep_skill_type()
1073. {
1074. 	if (u.twoweap)
1075. 		return P_TWO_WEAPON_COMBAT;
1076. 	return weapon_type(uwep);
1077. }
1078. 
1079. /*
1080.  * Return hit bonus/penalty based on skill of weapon.
1081.  * Treat restricted weapons as unskilled.
1082.  */
1083. int
1084. weapon_hit_bonus(weapon)
1085. struct obj *weapon;
1086. {
1087.     int type, wep_type, skill, bonus = 0;
1088.     static const char bad_skill[] = "weapon_hit_bonus: bad skill %d";
1089. 
1090.     wep_type = weapon_type(weapon);
1091.     /* use two weapon skill only if attacking with one of the wielded weapons */
1092.     type = (u.twoweap && (weapon == uwep || weapon == uswapwep)) ?
1093. 	    P_TWO_WEAPON_COMBAT : wep_type;
1094.     if (type == P_NONE) {
1095. 	bonus = 0;
1096.     } else if (type <= P_LAST_WEAPON) {
1097. 	switch (P_SKILL(type)) {
1098. 	    default: impossible(bad_skill, P_SKILL(type)); /* fall through */
1099. 	    case P_ISRESTRICTED:
1100. 	    case P_UNSKILLED:   bonus = -4; break;
1101. 	    case P_BASIC:       bonus =  0; break;
1102. 	    case P_SKILLED:     bonus =  2; break;
1103. 	    case P_EXPERT:      bonus =  3; break;
1104. 	}
1105.     } else if (type == P_TWO_WEAPON_COMBAT) {
1106. 	skill = P_SKILL(P_TWO_WEAPON_COMBAT);
1107. 	if (P_SKILL(wep_type) < skill) skill = P_SKILL(wep_type);
1108. 	switch (skill) {
1109. 	    default: impossible(bad_skill, skill); /* fall through */
1110. 	    case P_ISRESTRICTED:
1111. 	    case P_UNSKILLED:   bonus = -9; break;
1112. 	    case P_BASIC:	bonus = -7; break;
1113. 	    case P_SKILLED:	bonus = -5; break;
1114. 	    case P_EXPERT:	bonus = -3; break;
1115. 	}
1116.     } else if (type == P_BARE_HANDED_COMBAT) {
1117. 	/*
1118. 	 *	       b.h.  m.a.
1119. 	 *	unskl:	+1   n/a
1120. 	 *	basic:	+1    +3
1121. 	 *	skild:	+2    +4
1122. 	 *	exprt:	+2    +5
1123. 	 *	mastr:	+3    +6
1124. 	 *	grand:	+3    +7
1125. 	 */
1126. 	bonus = P_SKILL(type);
1127. 	bonus = max(bonus,P_UNSKILLED) - 1;	/* unskilled => 0 */
1128. 	bonus = ((bonus + 2) * (martial_bonus() ? 2 : 1)) / 2;
1129.     }
1130. 
1131. #ifdef STEED
1132. 	/* KMH -- It's harder to hit while you are riding */
1133. 	if (u.usteed) {
1134. 		switch (P_SKILL(P_RIDING)) {
1135. 		    case P_ISRESTRICTED:
1136. 		    case P_UNSKILLED:   bonus -= 2; break;
1137. 		    case P_BASIC:       bonus -= 1; break;
1138. 		    case P_SKILLED:     break;
1139. 		    case P_EXPERT:      break;
1140. 		}
1141. 		if (u.twoweap) bonus -= 2;
1142. 	}
1143. #endif
1144. 
1145.     return bonus;
1146. }
1147. 
1148. /*
1149.  * Return damage bonus/penalty based on skill of weapon.
1150.  * Treat restricted weapons as unskilled.
1151.  */
1152. int
1153. weapon_dam_bonus(weapon)
1154. struct obj *weapon;
1155. {
1156.     int type, wep_type, skill, bonus = 0;
1157. 
1158.     wep_type = weapon_type(weapon);
1159.     /* use two weapon skill only if attacking with one of the wielded weapons */
1160.     type = (u.twoweap && (weapon == uwep || weapon == uswapwep)) ?
1161. 	    P_TWO_WEAPON_COMBAT : wep_type;
1162.     if (type == P_NONE) {
1163. 	bonus = 0;
1164.     } else if (type <= P_LAST_WEAPON) {
1165. 	switch (P_SKILL(type)) {
1166. 	    default: impossible("weapon_dam_bonus: bad skill %d",P_SKILL(type));
1167. 		     /* fall through */
1168. 	    case P_ISRESTRICTED:
1169. 	    case P_UNSKILLED:	bonus = -2; break;
1170. 	    case P_BASIC:	bonus =  0; break;
1171. 	    case P_SKILLED:	bonus =  1; break;
1172. 	    case P_EXPERT:	bonus =  2; break;
1173. 	}
1174.     } else if (type == P_TWO_WEAPON_COMBAT) {
1175. 	skill = P_SKILL(P_TWO_WEAPON_COMBAT);
1176. 	if (P_SKILL(wep_type) < skill) skill = P_SKILL(wep_type);
1177. 	switch (skill) {
1178. 	    default:
1179. 	    case P_ISRESTRICTED:
1180. 	    case P_UNSKILLED:	bonus = -3; break;
1181. 	    case P_BASIC:	bonus = -1; break;
1182. 	    case P_SKILLED:	bonus = 0; break;
1183. 	    case P_EXPERT:	bonus = 1; break;
1184. 	}
1185.     } else if (type == P_BARE_HANDED_COMBAT) {
1186. 	/*
1187. 	 *	       b.h.  m.a.
1188. 	 *	unskl:	 0   n/a
1189. 	 *	basic:	+1    +3
1190. 	 *	skild:	+1    +4
1191. 	 *	exprt:	+2    +6
1192. 	 *	mastr:	+2    +7
1193. 	 *	grand:	+3    +9
1194. 	 */
1195. 	bonus = P_SKILL(type);
1196. 	bonus = max(bonus,P_UNSKILLED) - 1;	/* unskilled => 0 */
1197. 	bonus = ((bonus + 1) * (martial_bonus() ? 3 : 1)) / 2;
1198.     }
1199. 
1200. #ifdef STEED
1201. 	/* KMH -- Riding gives some thrusting damage */
1202. 	if (u.usteed && type != P_TWO_WEAPON_COMBAT) {
1203. 		switch (P_SKILL(P_RIDING)) {
1204. 		    case P_ISRESTRICTED:
1205. 		    case P_UNSKILLED:   break;
1206. 		    case P_BASIC:       break;
1207. 		    case P_SKILLED:     bonus += 1; break;
1208. 		    case P_EXPERT:      bonus += 2; break;
1209. 		}
1210. 	}
1211. #endif
1212. 
1213.     return bonus;
1214. }
1215. 
1216. /*
1217.  * Initialize weapon skill array for the game.  Start by setting all
1218.  * skills to restricted, then set the skill for every weapon the
1219.  * hero is holding, finally reading the given array that sets
1220.  * maximums.
1221.  */
1222. void
1223. skill_init(class_skill)
1224. struct def_skill *class_skill;
1225. {
1226. 	struct obj *obj;
1227. 	int skmax, skill;
1228. 
1229. 	/* initialize skill array; by default, everything is restricted */
1230. 	for (skill = 0; skill < P_NUM_SKILLS; skill++) {
1231. 	    P_SKILL(skill) = P_ISRESTRICTED;
1232. 	    P_MAX_SKILL(skill) = P_ISRESTRICTED;
1233. 	    P_ADVANCE(skill) = 0;
1234. 	}
1235. 
1236. 	/* Set skill for all weapons in inventory to be basic */
1237. 	for (obj = invent; obj; obj = obj->nobj) {
1238. 	    skill = weapon_type(obj);
1239. 	    if (skill != P_NONE)
1240. 		P_SKILL(skill) = P_BASIC;
1241. 	}
1242. 
1243. 	/* set skills for magic */
1244. 	if (Role_if(PM_HEALER) || Role_if(PM_MONK)) {
1245. 		P_SKILL(P_HEALING_SPELL) = P_BASIC;
1246. 	} else if (Role_if(PM_PRIEST)) {
1247. 		P_SKILL(P_CLERIC_SPELL) = P_BASIC;
1248. 	} else if (Role_if(PM_WIZARD)) {
1249. 		P_SKILL(P_ATTACK_SPELL) = P_BASIC;
1250. 		P_SKILL(P_ENCHANTMENT_SPELL) = P_BASIC;
1251. 	}
1252. 
1253. 	/* walk through array to set skill maximums */
1254. 	for (; class_skill->skill != P_NONE; class_skill++) {
1255. 	    skmax = class_skill->skmax;
1256. 	    skill = class_skill->skill;
1257. 
1258. 	    P_MAX_SKILL(skill) = skmax;
1259. 	    if (P_SKILL(skill) == P_ISRESTRICTED)	/* skill pre-set */
1260. 		P_SKILL(skill) = P_UNSKILLED;
1261. 	}
1262. 
1263. 	/* High potential fighters already know how to use their hands. */
1264. 	if (P_MAX_SKILL(P_BARE_HANDED_COMBAT) > P_EXPERT)
1265. 	    P_SKILL(P_BARE_HANDED_COMBAT) = P_BASIC;
1266. 
1267. 	/* Roles that start with a horse know how to ride it */
1268. #ifdef STEED
1269. 	if (urole.petnum == PM_PONY)
1270. 	    P_SKILL(P_RIDING) = P_BASIC;
1271. #endif
1272. 
1273. 	/*
1274. 	 * Make sure we haven't missed setting the max on a skill
1275. 	 * & set advance
1276. 	 */
1277. 	for (skill = 0; skill < P_NUM_SKILLS; skill++) {
1278. 	    if (!P_RESTRICTED(skill)) {
1279. 		if (P_MAX_SKILL(skill) < P_SKILL(skill)) {
1280. 		    impossible("skill_init: curr > max: %s", P_NAME(skill));
1281. 		    P_MAX_SKILL(skill) = P_SKILL(skill);
1282. 		}
1283. 		P_ADVANCE(skill) = practice_needed_to_advance(P_SKILL(skill)-1);
1284. 	    }
1285. 	}
1286. }
1287. 
1288. void
1289. setmnotwielded(mon,obj)
1290. register struct monst *mon;
1291. register struct obj *obj;
1292. {
1293.     if (!obj) return;
1294.     if (artifact_light(obj) && obj->lamplit) {
1295. 	end_burn(obj, FALSE);
1296. 	if (canseemon(mon))
1297. 	    pline("%s in %s %s %s glowing.", The(xname(obj)),
1298. 		  s_suffix(mon_nam(mon)), mbodypart(mon,HAND),
1299. 		  otense(obj, "stop"));
1300.     }
1301.     obj->owornmask &= ~W_WEP;
1302. }
1303. 
1304. #endif /* OVLB */
1305. 
1306. /*weapon.c*/

Also on Fandom

Random Wiki