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