Source:NetHack 2.2a/fight.c
Jump to navigation
Jump to search
Below is the full text to fight.c from the source code of NetHack 2.2a. To link to a particular line, write [[NetHack 2.2a/fight.c#line123]], for example.
Warning! This is the source code from an old release. For the latest release, see Source code
Screenshots and source code from Hack are used under the CWI license.
1. /* SCCS Id: @(#)fight.c 2.1 87/10/17 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. 4. #include <stdio.h> 5. #include "hack.h" 6. 7. extern struct permonst li_dog, dog, la_dog; 8. extern char *exclam(), *hcolor(), *xname(); 9. extern struct obj *mkobj_at(); 10. #ifdef KAA 11. extern boolean stoned; 12. extern boolean unweapon; 13. extern char *nomovemsg, *defmonnam(); 14. extern struct monst *mkmon_at(); 15. #endif 16. #ifdef RPH 17. extern struct obj *mk_named_obj(); 18. #endif 19. 20. static boolean far_noise; 21. static long noisetime; 22. 23. /* hitmm returns 0 (miss), 1 (hit), or 2 (kill) */ 24. hitmm(magr,mdef) register struct monst *magr,*mdef; { 25. register struct permonst *pa = magr->data, *pd = mdef->data; 26. int hit; 27. schar tmp; 28. boolean vis; 29. if(!magr || !mdef) return(0); /* mike@genat */ 30. if(index("Eauy", pa->mlet)) return(0); 31. if(magr->mfroz) return(0); /* riv05!a3 */ 32. tmp = pd->ac + pa->mlevel; 33. if(mdef->mconf || mdef->mfroz || mdef->msleep){ 34. tmp += 4; 35. if(mdef->msleep) mdef->msleep = 0; 36. } 37. hit = (tmp > rnd(20)); 38. if(hit) mdef->msleep = 0; 39. vis = (cansee(magr->mx,magr->my) && cansee(mdef->mx,mdef->my)); 40. if(vis){ 41. char buf[BUFSZ]; 42. if(mdef->mimic) seemimic(mdef); 43. if(magr->mimic) seemimic(magr); 44. (void) sprintf(buf,"%s %s", Monnam(magr), 45. hit ? "hits" : "misses"); 46. pline("%s %s.", buf, monnam(mdef)); 47. } else { 48. boolean farq = (dist(magr->mx, magr->my) > 15); 49. if(farq != far_noise || moves-noisetime > 10) { 50. far_noise = farq; 51. noisetime = moves; 52. pline("You hear some noises%s.", 53. farq ? " in the distance" : ""); 54. } 55. } 56. if(hit){ 57. if(magr->data->mlet == 'c' && !magr->cham) { 58. magr->mhpmax += 3; 59. if(vis) pline("%s is turned to stone!", Monnam(mdef)); 60. else if(mdef->mtame) 61. pline("You have a peculiarly sad feeling for a moment, then it passes."); 62. monstone(mdef); 63. hit = 2; 64. } else 65. if((mdef->mhp -= d(pa->damn,pa->damd)) < 1) { 66. magr->mhpmax += 1 + rn2(pd->mlevel+1); 67. if(magr->mtame && magr->mhpmax > 8*pa->mlevel){ 68. if(pa == &li_dog) magr->data = pa = &dog; 69. else if(pa == &dog) magr->data = pa = &la_dog; 70. } 71. if(vis) pline("%s is killed!", Monnam(mdef)); 72. else if(mdef->mtame) 73. pline("You have a sad feeling for a moment, then it passes."); 74. mondied(mdef); 75. hit = 2; 76. } 77. /* fixes a bug where max monster hp could overflow. */ 78. if(magr->mhpmax <= 0) magr->mhpmax = 127; 79. } 80. #ifdef KAA 81. if(hit == 1 && magr->data->mlet == 'Q') { 82. rloc(mdef); 83. if(vis && !cansee(mdef->mx,mdef->my)) 84. pline("%s suddenly disappears!",Monnam(mdef)); 85. } 86. #endif 87. return(hit); 88. } 89. 90. /* drop (perhaps) a cadaver and remove monster */ 91. mondied(mdef) register struct monst *mdef; { 92. register struct permonst *pd = mdef->data; 93. #ifdef KOPS 94. if(pd->mlet != 'K') 95. #endif 96. { 97. #if defined(ROCKMOLE) && defined(KJSMODS) 98. /* if a giant rat is killed by a monster, do not make a 99. * corpse (like Keystone Kops above). */ 100. if(!(pd->mlet == 'r' && dlevel < 4)) 101. #endif 102. if(letter(pd->mlet) && rn2(3)) { 103. if (pd->mlet == '1') panic("mondied: making obj '1'"); 104. #ifndef RPH 105. (void) mkobj_at(pd->mlet,mdef->mx,mdef->my); 106. #else 107. (void) mk_named_obj_at(pd->mlet,mdef->mx,mdef->my, 108. NAME(mdef), mdef->mnamelth); 109. #endif 110. if(cansee(mdef->mx,mdef->my)){ 111. unpmon(mdef); 112. atl(mdef->mx,mdef->my,fobj->olet); 113. } 114. stackobj(fobj); 115. } 116. mondead(mdef); 117. } 118. } 119. 120. /* drop a rock and remove monster */ 121. monstone(mdef) 122. register struct monst *mdef; 123. { 124. extern char mlarge[]; 125. if(index(mlarge, mdef->data->mlet)) 126. mksobj_at(ENORMOUS_ROCK, mdef->mx, mdef->my); 127. else 128. mksobj_at(ROCK, mdef->mx, mdef->my); 129. if(cansee(mdef->mx, mdef->my)){ 130. unpmon(mdef); 131. atl(mdef->mx,mdef->my,fobj->olet); 132. } 133. mondead(mdef); 134. } 135. 136. 137. fightm(mtmp) 138. register struct monst *mtmp; 139. { 140. register struct monst *mon; 141. 142. for(mon = fmon; mon; mon = mon->nmon) 143. if(mon != mtmp) { 144. if(DIST(mon->mx,mon->my,mtmp->mx,mtmp->my) < 3) 145. if(rn2(4)) return(hitmm(mtmp,mon)); 146. } 147. return(-1); 148. } 149. 150. /* u is hit by sth, but not a monster */ 151. thitu(tlev,dam,name) 152. register tlev,dam; 153. register char *name; 154. { 155. char buf[BUFSZ]; 156. 157. setan(name,buf); 158. if(u.uac + tlev <= rnd(20)) { 159. if(Blind) pline("It misses."); 160. else pline("You are almost hit by %s!", buf); 161. return(0); 162. } else { 163. if(Blind) pline("You are hit!"); 164. else pline("You are hit by %s!", buf); 165. losehp(dam,name); 166. return(1); 167. } 168. } 169. 170. #ifdef KAA 171. char mlarge[] = "bCDdegIlmnoPSsTUwY',&9"; 172. #else 173. char mlarge[] = "bCDdegIlmnoPSsTUwY',&"; 174. #endif 175. 176. boolean 177. hmon(mon,obj,thrown) /* return TRUE if mon still alive */ 178. register struct monst *mon; 179. register struct obj *obj; 180. register thrown; 181. { 182. register tmp; 183. boolean hittxt = FALSE; 184. 185. #ifdef KAA 186. stoned = FALSE; /* this refers to the thing hit, not you */ 187. #endif 188. if(!obj){ 189. #ifdef KAA 190. /* Note that c, y, and F can never wield weapons anyway */ 191. if (u.usym == 'c' && mon->data->mlet != 'c') { 192. pline("You turn %s to stone!", monnam(mon)); 193. stoned = TRUE; 194. xkilled(mon,0); 195. return(FALSE); 196. } else if (u.usym == 'y' && mon->data->mlet != 'y') { 197. pline("%s is blinded by your flash of light!",Monnam(mon)); 198. if (!mon->mblinded) { 199. mon->mblinded += rn2(25); 200. mon->mcansee = 0; 201. } 202. rehumanize(); 203. return(TRUE); 204. } else if (u.usym == 'F') { 205. pline("You explode!"); 206. if (!index("gFY",mon->data->mlet)) { 207. pline("%s gets blasted!", Monnam(mon)); 208. mon->mhp -= d(6,6); 209. rehumanize(); 210. if (mon->mhp <= 0) { 211. killed(mon); 212. return(FALSE); 213. } else return(TRUE); 214. } else { 215. pline("The blast doesn't seem to affect %s.", monnam(mon)); 216. rehumanize(); 217. return(TRUE); 218. } 219. } else if (index("P,'", u.usym) && u.uhunger < 1500 220. && !u.uswallow && mon->data->mlet != 'c') { 221. static char msgbuf[BUFSZ]; 222. pline("You swallow %s whole!", monnam(mon)); 223. u.uhunger += 20*mon->mhpmax; 224. newuhs(FALSE); 225. xkilled(mon,2); 226. if (tmp = mon->mhpmax/5) { 227. nomul(-tmp); 228. (void)sprintf(msgbuf, "You finished digesting %s.", 229. monnam(mon)); 230. nomovemsg = msgbuf; 231. } 232. return(FALSE); 233. } else if (u.usym != '@') { 234. if (u.usym == '&' && !rn2(5)) { 235. struct monst *dtmp; 236. pline("Some hell-p has arrived!"); 237. if((dtmp = mkmon_at('&',u.ux,u.uy))) 238. (void)tamedog(dtmp,(struct obj *)0); 239. } 240. tmp = d(mons[u.umonnum].damn, mons[u.umonnum].damd); 241. } else 242. #endif 243. tmp = rnd(2); /* attack with bare hands */ 244. #ifdef KAA 245. if (mon->data->mlet == 'c' && !uarmg && u.usym != 'c'){ 246. #else 247. if(mon->data->mlet == 'c' && !uarmg){ 248. #endif 249. pline("You hit the cockatrice with your bare hands."); 250. pline("You turn to stone ..."); 251. done_in_by(mon); 252. } 253. } else if(obj->olet == WEAPON_SYM || obj->otyp == PICK_AXE) { 254. if(obj == uwep && (obj->otyp > SPEAR || obj->otyp < BOOMERANG)) 255. tmp = rnd(2); 256. else { 257. if(index(mlarge, mon->data->mlet)) { 258. tmp = rnd(objects[obj->otyp].wldam); 259. switch (obj->otyp) { 260. case SLING_BULLET: 261. case CROSSBOW_BOLT: 262. case MORNING_STAR: 263. case PARTISAN: 264. case BROAD_SWORD: tmp += 1; break; 265. 266. case FLAIL: 267. case RANSEUR: 268. case VOULGE: tmp += rnd(4); break; 269. 270. case HALBERD: 271. case SPETUM: tmp += rnd(6); break; 272. 273. case BARDICHE: 274. case TRIDENT: tmp += d(2,4); break; 275. 276. case TWO_HANDED_SWORD: 277. case KATANA: tmp += d(2,6); break; 278. } 279. } else { 280. tmp = rnd(objects[obj->otyp].wsdam); 281. switch (obj->otyp) { 282. case SLING_BULLET: 283. case CROSSBOW_BOLT: 284. case MACE: 285. case FLAIL: 286. case SPETUM: 287. case TRIDENT: tmp += 1; break; 288. 289. case BARDICHE: 290. case BILL_GUISARME: 291. case GUISARME: 292. case LUCERN_HAMMER: 293. case MORNING_STAR: 294. case RANSEUR: 295. case BROAD_SWORD: 296. case VOULGE: tmp += rnd(4); break; 297. } 298. } 299. tmp += obj->spe; 300. #ifdef KAA 301. if(obj->olet == WEAPON_SYM && obj->dknown && index("VWZ &", 302. mon->data->mlet)) tmp += rn2(4); 303. #endif 304. if(!thrown && obj == uwep && obj->otyp == BOOMERANG 305. && !rn2(3)){ 306. pline("As you hit %s, the boomerang breaks into splinters.", 307. monnam(mon)); 308. freeinv(obj); 309. setworn((struct obj *) 0, obj->owornmask); 310. obfree(obj, (struct obj *) 0); 311. tmp++; 312. } 313. } 314. #ifdef BVH 315. if(!strcmp(ONAME(obj), "Excalibur")) tmp += rnd(10); 316. else 317. #endif 318. if(obj->otyp == KATANA 319. && !strcmp(ONAME(obj), "Snickersnee")) tmp += rnd(5); 320. 321. else if(mon->data->mlet == 'O' && obj->otyp == TWO_HANDED_SWORD 322. && !strcmp(ONAME(obj), "Orcrist")) tmp += rnd(10); 323. 324. } else switch(obj->otyp) { 325. case HEAVY_IRON_BALL: 326. tmp = rnd(25); break; 327. case ENORMOUS_ROCK: 328. tmp = rnd(20); break; 329. #ifdef RPH 330. case MIRROR: 331. pline("You break your mirror. That's bad luck!"); 332. u.uluck -= 2; 333. if ((int)u.uluck < LUCKMIN) u.uluck = LUCKMIN; 334. freeinv(obj); 335. if(obj->owornmask) 336. setworn((struct obj *) 0, obj->owornmask); 337. obfree(obj, (struct obj *) 0); 338. return(TRUE); 339. #endif 340. case EXPENSIVE_CAMERA: 341. pline("You succeed in destroying your camera. Congratulations!"); 342. freeinv(obj); 343. if(obj->owornmask) 344. setworn((struct obj *) 0, obj->owornmask); 345. obfree(obj, (struct obj *) 0); 346. return(TRUE); 347. case DEAD_COCKATRICE: /* fixed by polder@cs.vu.nl */ 348. pline("You hit %s with the cockatrice corpse.", 349. monnam(mon)); 350. if(mon->data->mlet == 'c') { 351. tmp = 1; 352. hittxt = TRUE; 353. break; 354. } 355. pline ("%s is turned to stone!", Monnam(mon)); 356. #ifdef KAA 357. stoned = TRUE; 358. xkilled(mon,0); 359. #else 360. killed(mon); 361. #endif 362. return(FALSE); 363. case CLOVE_OF_GARLIC: /* no effect against demons */ 364. if(index(UNDEAD, mon->data->mlet)) 365. mon->mflee = 1; 366. tmp = 1; 367. break; 368. default: 369. /* non-weapons can damage because of their weight */ 370. /* (but not too much) */ 371. tmp = obj->owt/10; 372. if(tmp < 1) tmp = 1; 373. else tmp = rnd(tmp); 374. if(tmp > 6) tmp = 6; 375. } 376. 377. /****** NOTE: perhaps obj is undefined!! (if !thrown && BOOMERANG) */ 378. 379. tmp += u.udaminc + dbon(); 380. if(u.uswallow) { 381. if((tmp -= u.uswldtim) <= 0) { 382. pline("Your arms are no longer able to hit."); 383. return(TRUE); 384. } 385. } 386. if(tmp < 1) tmp = 1; 387. mon->mhp -= tmp; 388. if(mon->mhp < 1) { 389. killed(mon); 390. return(FALSE); 391. } 392. if(mon->mtame && (!mon->mflee || mon->mfleetim)) { 393. mon->mflee = 1; /* Rick Richardson */ 394. mon->mfleetim += 10*rnd(tmp); 395. } 396. 397. if(!hittxt) { 398. if(thrown) 399. /* this assumes that we cannot throw plural things */ 400. hit( xname(obj) /* or: objects[obj->otyp].oc_name */, 401. mon, exclam(tmp) ); 402. else if(Blind) 403. pline("You hit it."); 404. else 405. pline("You hit %s%s", monnam(mon), exclam(tmp)); 406. } 407. 408. if(u.umconf && !thrown) { 409. if(!Blind) { 410. pline("Your hands stop glowing %s.", 411. Hallucination ? hcolor() : "blue"); 412. } 413. if (!resist(mon, '+', 0, NOTELL)) mon->mconf = 1; 414. if(!mon->mfroz && !mon->msleep && !Blind && mon->mconf) 415. pline("%s appears confused.",Monnam(mon)); 416. u.umconf = 0; 417. } 418. if(!thrown && rn2(2) && index("VW",u.usym) && 419. !index("VW",mon->data->mlet)){ 420. int tmp=d(2,6); 421. pline("%s suddenly seems weaker!",Monnam(mon)); 422. mon->mhpmax -= tmp; 423. if ((mon->mhp -= tmp) <= 0) { 424. pline("%s dies!",Monnam(mon)); 425. xkilled(mon,0); 426. return(FALSE); 427. } 428. } 429. return(TRUE); /* mon still alive */ 430. } 431. 432. /* try to attack; return FALSE if monster evaded */ 433. /* u.dx and u.dy must be set */ 434. attack(mtmp) 435. register struct monst *mtmp; 436. { 437. schar tmp; 438. boolean malive = TRUE; 439. register struct permonst *mdat; 440. mdat = mtmp->data; 441. 442. #ifdef KAA 443. if(unweapon) { 444. unweapon=FALSE; 445. if(uwep) 446. pline("You begin bashing monsters with your %s.", 447. aobjnam(uwep,(char *)0)); 448. } 449. #endif 450. u_wipe_engr(3); /* andrew@orca: prevent unlimited pick-axe attacks */ 451. 452. if(mdat->mlet == 'L' && !mtmp->mfroz && !mtmp->msleep && 453. !mtmp->mconf && mtmp->mcansee && !rn2(7) && 454. (m_move(mtmp, 0) == 2 /* he died */ || /* he moved: */ 455. mtmp->mx != u.ux+u.dx || mtmp->my != u.uy+u.dy)) 456. return(FALSE); 457. #ifdef SAFE_ATTACK 458. /* This section of code provides protection against accidentally 459. * hitting peaceful (like '@') and tame (like 'd') monsters. 460. * There is protection only if you're not blind, confused or 461. * invisible. 462. */ 463. /* changes by wwp 5/16/85 */ 464. if (!Blind && !Confusion && !Hallucination 465. && mdat->mlet == 'd' && mtmp->mtame) { 466. char *dname; /* added by Janet Walz (walz@mimsy) */ 467. mtmp->mflee = 1; 468. mtmp->mfleetim = rnd(6); 469. if (dname = NAME(mtmp)) 470. pline("You stop to avoid hitting %s.",dname); 471. else 472. pline("You stop to avoid hitting your dog."); 473. return(TRUE); 474. } 475. if (flags.confirm && (mtmp->mpeaceful || mtmp->mtame) && ! Confusion 476. && !Hallucination && !Invisible) 477. 478. if (Blind ? Telepat : (!mtmp->minvis || See_invisible)) { 479. pline("Really attack %s?", monnam(mtmp)); 480. (void) fflush(stdout); 481. if (readchar() != 'y') { 482. flags.move = 0; 483. return(TRUE); 484. } 485. } 486. #endif /* SAFE_ATTACK /**/ 487. 488. if(mtmp->mimic){ 489. if(!u.ustuck && !mtmp->mflee) u.ustuck = mtmp; 490. if (levl[u.ux+u.dx][u.uy+u.dy].scrsym == DOOR_SYM) 491. { 492. if (okdoor(u.ux+u.dx, u.uy+u.dy)) 493. pline("The door actually was %s.", defmonnam(mtmp)); 494. else 495. pline("That spellbook was %s.", defmonnam(mtmp)); 496. } 497. else if (levl[u.ux+u.dx][u.uy+u.dy].scrsym == GOLD_SYM) 498. pline("The chest was %s!", defmonnam(mtmp)); 499. else 500. pline("Wait! That's %s!",defmonnam(mtmp)); 501. wakeup(mtmp); /* clears mtmp->mimic */ 502. return(TRUE); 503. } 504. 505. wakeup(mtmp); 506. 507. if(mtmp->mhide && mtmp->mundetected){ 508. register struct obj *obj; 509. 510. mtmp->mundetected = 0; 511. if((obj = o_at(mtmp->mx,mtmp->my)) && !Blind) 512. pline("Wait! There's %s hiding under %s!", 513. defmonnam(mtmp), doname(obj)); 514. return(TRUE); 515. } 516. #ifdef KAA 517. tmp = u.uluck + (u.mtimedone ? mons[u.umonnum].mlevel : u.ulevel) + 518. mdat->ac + abon(); 519. if (u.usym=='y' || u.usym=='F') tmp=100; 520. if (index("uEa",u.usym)) return(TRUE); 521. #else 522. tmp = u.uluck + u.ulevel + mdat->ac + abon(); 523. #endif 524. if(uwep) { 525. #ifdef KAA /* Blessed weapons used against undead or demons */ 526. if(uwep->olet == WEAPON_SYM && uwep->dknown && index("VWZ &", 527. mtmp->data->mlet)) tmp += 2; 528. #endif 529. if(uwep->olet == WEAPON_SYM || uwep->otyp == PICK_AXE) 530. tmp += uwep->spe; 531. #ifdef BVH 532. if(!strcmp(ONAME(uwep),"Excalibur")) tmp += 5; 533. #endif 534. if(uwep->otyp == TWO_HANDED_SWORD) tmp -= 1; 535. else if(uwep->otyp == KATANA) tmp += 1; 536. else if(uwep->otyp == DAGGER || 537. uwep->otyp == SHURIKEN) tmp += 2; 538. else if(uwep->otyp == CRYSKNIFE) tmp += 3; 539. else if(uwep->otyp == SPEAR && 540. index("XDne", mdat->mlet)) tmp += 2; 541. } 542. if(mtmp->msleep) { 543. mtmp->msleep = 0; 544. tmp += 2; 545. } 546. if(mtmp->mfroz) { 547. tmp += 4; 548. if(!rn2(10)) mtmp->mfroz = 0; 549. } 550. if(mtmp->mflee) tmp += 2; 551. if(u.utrap) tmp -= 3; 552. 553. /* with a lot of luggage, your agility diminishes */ 554. tmp -= (inv_weight() + 40)/20; 555. 556. if(tmp <= rnd(20) && !u.uswallow){ 557. if(Blind) pline("You miss it."); 558. else pline("You miss %s.",monnam(mtmp)); 559. } else { 560. /* we hit the monster; be careful: it might die! */ 561. 562. if((malive = hmon(mtmp,uwep,0)) == TRUE) { 563. /* monster still alive */ 564. if(!rn2(25) && mtmp->mhp < mtmp->mhpmax/2) { 565. mtmp->mflee = 1; 566. if(!rn2(3)) mtmp->mfleetim = rnd(100); 567. if(u.ustuck == mtmp && !u.uswallow) 568. u.ustuck = 0; 569. } 570. #ifndef NOWORM 571. if(mtmp->wormno) 572. cutworm(mtmp, u.ux+u.dx, u.uy+u.dy, 573. uwep ? uwep->otyp : 0); 574. #endif 575. } 576. if(mdat->mlet == 'a') { 577. if(rn2(2)) { 578. if (Blind) pline("You are splashed!"); 579. else pline("You are splashed by %s's acid!",monnam(mtmp)); 580. if (u.usym != 'a') { 581. losehp_m(rnd(6), mtmp); 582. if(!rn2(30)) corrode_armor(); 583. } 584. } 585. if(!rn2(6)) corrode_weapon(); 586. } 587. } 588. #ifdef KAA 589. if (malive) if (u.usym=='N' && mtmp->minvent) { 590. struct obj *otmp, *addinv(); 591. otmp = mtmp->minvent; 592. mtmp->minvent = otmp->nobj; 593. otmp = addinv(otmp); 594. pline("You steal:"); 595. prinv(otmp); 596. } else if (u.usym=='L' && mtmp->mgold) { 597. u.ugold += mtmp->mgold; 598. mtmp->mgold = 0; 599. pline("Your purse feels heavier."); 600. } else if (u.usym=='Q') rloc(mtmp); 601. #endif 602. if(malive && mdat->mlet == 'E' && canseemon(mtmp) 603. && !mtmp->mcan && rn2(3)) { 604. if(mtmp->mcansee) { 605. pline("You are frozen by %s's gaze!",monnam(mtmp)); 606. nomul((u.ulevel > 6 || rn2(4)) ? rn1(20,-21) : -200); 607. } else { 608. pline("%s cannot defend itself.", Amonnam(mtmp,"blinded")); 609. if(!rn2(500)) if((int)u.uluck > LUCKMIN) u.uluck--; 610. } 611. } 612. return(TRUE); 613. }