hack.fight.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* $NetBSD: hack.fight.c,v 1.6 2003/04/02 18:36:36 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  4. * Amsterdam
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * - Neither the name of the Stichting Centrum voor Wiskunde en
  19. * Informatica, nor the names of its contributors may be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. The name of the author may not be used to endorse or promote products
  48. * derived from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  51. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  52. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  53. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  56. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  57. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  58. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  59. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. */
  61. #include <sys/cdefs.h>
  62. #ifndef lint
  63. __RCSID("$NetBSD: hack.fight.c,v 1.6 2003/04/02 18:36:36 jsm Exp $");
  64. #endif /* not lint */
  65. #include "hack.h"
  66. #include "extern.h"
  67. static boolean far_noise;
  68. static long noisetime;
  69. /* hitmm returns 0 (miss), 1 (hit), or 2 (kill) */
  70. int
  71. hitmm(magr, mdef)
  72. struct monst *magr, *mdef;
  73. {
  74. const struct permonst *pa = magr->data, *pd = mdef->data;
  75. int hit;
  76. schar tmp;
  77. boolean vis;
  78. if (strchr("Eauy", pa->mlet))
  79. return (0);
  80. if (magr->mfroz)
  81. return (0); /* riv05!a3 */
  82. tmp = pd->ac + pa->mlevel;
  83. if (mdef->mconf || mdef->mfroz || mdef->msleep) {
  84. tmp += 4;
  85. if (mdef->msleep)
  86. mdef->msleep = 0;
  87. }
  88. hit = (tmp > rnd(20));
  89. if (hit)
  90. mdef->msleep = 0;
  91. vis = (cansee(magr->mx, magr->my) && cansee(mdef->mx, mdef->my));
  92. if (vis) {
  93. char buf[BUFSZ];
  94. if (mdef->mimic)
  95. seemimic(mdef);
  96. if (magr->mimic)
  97. seemimic(magr);
  98. (void) sprintf(buf, "%s %s", Monnam(magr),
  99. hit ? "hits" : "misses");
  100. pline("%s %s.", buf, monnam(mdef));
  101. } else {
  102. boolean far = (dist(magr->mx, magr->my) > 15);
  103. if (far != far_noise || moves - noisetime > 10) {
  104. far_noise = far;
  105. noisetime = moves;
  106. pline("You hear some noises%s.",
  107. far ? " in the distance" : "");
  108. }
  109. }
  110. if (hit) {
  111. if (magr->data->mlet == 'c' && !magr->cham) {
  112. magr->mhpmax += 3;
  113. if (vis)
  114. pline("%s is turned to stone!", Monnam(mdef));
  115. else if (mdef->mtame)
  116. pline("You have a peculiarly sad feeling for a moment, then it passes.");
  117. monstone(mdef);
  118. hit = 2;
  119. } else if ((mdef->mhp -= d(pa->damn, pa->damd)) < 1) {
  120. magr->mhpmax += 1 + rn2(pd->mlevel + 1);
  121. if (magr->mtame && magr->mhpmax > 8 * pa->mlevel) {
  122. if (pa == &li_dog)
  123. magr->data = pa = &dog;
  124. else if (pa == &dog)
  125. magr->data = pa = &la_dog;
  126. }
  127. if (vis)
  128. pline("%s is killed!", Monnam(mdef));
  129. else if (mdef->mtame)
  130. pline("You have a sad feeling for a moment, then it passes.");
  131. mondied(mdef);
  132. hit = 2;
  133. }
  134. }
  135. return (hit);
  136. }
  137. /* drop (perhaps) a cadaver and remove monster */
  138. void
  139. mondied(mdef)
  140. struct monst *mdef;
  141. {
  142. const struct permonst *pd = mdef->data;
  143. if (letter(pd->mlet) && rn2(3)) {
  144. (void) mkobj_at(pd->mlet, mdef->mx, mdef->my);
  145. if (cansee(mdef->mx, mdef->my)) {
  146. unpmon(mdef);
  147. atl(mdef->mx, mdef->my, fobj->olet);
  148. }
  149. stackobj(fobj);
  150. }
  151. mondead(mdef);
  152. }
  153. /* drop a rock and remove monster */
  154. void
  155. monstone(mdef)
  156. struct monst *mdef;
  157. {
  158. if (strchr(mlarge, mdef->data->mlet))
  159. mksobj_at(ENORMOUS_ROCK, mdef->mx, mdef->my);
  160. else
  161. mksobj_at(ROCK, mdef->mx, mdef->my);
  162. if (cansee(mdef->mx, mdef->my)) {
  163. unpmon(mdef);
  164. atl(mdef->mx, mdef->my, fobj->olet);
  165. }
  166. mondead(mdef);
  167. }
  168. int
  169. fightm(mtmp)
  170. struct monst *mtmp;
  171. {
  172. struct monst *mon;
  173. for (mon = fmon; mon; mon = mon->nmon)
  174. if (mon != mtmp) {
  175. if (DIST(mon->mx, mon->my, mtmp->mx, mtmp->my) < 3)
  176. if (rn2(4))
  177. return (hitmm(mtmp, mon));
  178. }
  179. return (-1);
  180. }
  181. /* u is hit by sth, but not a monster */
  182. int
  183. thitu(tlev, dam, name)
  184. int tlev, dam;
  185. const char *name;
  186. {
  187. char buf[BUFSZ];
  188. setan(name, buf);
  189. if (u.uac + tlev <= rnd(20)) {
  190. if (Blind)
  191. pline("It misses.");
  192. else
  193. pline("You are almost hit by %s!", buf);
  194. return (0);
  195. } else {
  196. if (Blind)
  197. pline("You are hit!");
  198. else
  199. pline("You are hit by %s!", buf);
  200. losehp(dam, name);
  201. return (1);
  202. }
  203. }
  204. char mlarge[] = "bCDdegIlmnoPSsTUwY',&";
  205. boolean
  206. hmon(mon, obj, thrown) /* return TRUE if mon still alive */
  207. struct monst *mon;
  208. struct obj *obj;
  209. int thrown;
  210. {
  211. int tmp;
  212. boolean hittxt = FALSE;
  213. if (!obj) {
  214. tmp = rnd(2); /* attack with bare hands */
  215. if (mon->data->mlet == 'c' && !uarmg) {
  216. pline("You hit the cockatrice with your bare hands.");
  217. pline("You turn to stone ...");
  218. done_in_by(mon);
  219. }
  220. } else if (obj->olet == WEAPON_SYM || obj->otyp == PICK_AXE) {
  221. if (obj == uwep && (obj->otyp > SPEAR || obj->otyp < BOOMERANG))
  222. tmp = rnd(2);
  223. else {
  224. if (strchr(mlarge, mon->data->mlet)) {
  225. tmp = rnd(objects[obj->otyp].wldam);
  226. if (obj->otyp == TWO_HANDED_SWORD)
  227. tmp += d(2, 6);
  228. else if (obj->otyp == FLAIL)
  229. tmp += rnd(4);
  230. } else {
  231. tmp = rnd(objects[obj->otyp].wsdam);
  232. }
  233. tmp += obj->spe;
  234. if (!thrown && obj == uwep && obj->otyp == BOOMERANG
  235. && !rn2(3)) {
  236. pline("As you hit %s, the boomerang breaks into splinters.",
  237. monnam(mon));
  238. freeinv(obj);
  239. setworn((struct obj *) 0, obj->owornmask);
  240. obfree(obj, (struct obj *) 0);
  241. tmp++;
  242. }
  243. }
  244. if (mon->data->mlet == 'O' && obj->otyp == TWO_HANDED_SWORD &&
  245. !strcmp(ONAME(obj), "Orcrist"))
  246. tmp += rnd(10);
  247. } else
  248. switch (obj->otyp) {
  249. case HEAVY_IRON_BALL:
  250. tmp = rnd(25);
  251. break;
  252. case EXPENSIVE_CAMERA:
  253. pline("You succeed in destroying your camera. Congratulations!");
  254. freeinv(obj);
  255. if (obj->owornmask)
  256. setworn((struct obj *) 0, obj->owornmask);
  257. obfree(obj, (struct obj *) 0);
  258. return (TRUE);
  259. case DEAD_COCKATRICE:
  260. pline("You hit %s with the cockatrice corpse.",
  261. monnam(mon));
  262. if (mon->data->mlet == 'c') {
  263. tmp = 1;
  264. hittxt = TRUE;
  265. break;
  266. }
  267. pline("%s is turned to stone!", Monnam(mon));
  268. killed(mon);
  269. return (FALSE);
  270. case CLOVE_OF_GARLIC: /* no effect against demons */
  271. if (strchr(UNDEAD, mon->data->mlet))
  272. mon->mflee = 1;
  273. tmp = 1;
  274. break;
  275. default:
  276. /* non-weapons can damage because of their weight */
  277. /* (but not too much) */
  278. tmp = obj->owt / 10;
  279. if (tmp < 1)
  280. tmp = 1;
  281. else
  282. tmp = rnd(tmp);
  283. if (tmp > 6)
  284. tmp = 6;
  285. }
  286. /****** NOTE: perhaps obj is undefined!! (if !thrown && BOOMERANG) */
  287. tmp += u.udaminc + dbon();
  288. if (u.uswallow) {
  289. if ((tmp -= u.uswldtim) <= 0) {
  290. pline("Your arms are no longer able to hit.");
  291. return (TRUE);
  292. }
  293. }
  294. if (tmp < 1)
  295. tmp = 1;
  296. mon->mhp -= tmp;
  297. if (mon->mhp < 1) {
  298. killed(mon);
  299. return (FALSE);
  300. }
  301. if (mon->mtame && (!mon->mflee || mon->mfleetim)) {
  302. mon->mflee = 1; /* Rick Richardson */
  303. mon->mfleetim += 10 * rnd(tmp);
  304. }
  305. if (!hittxt) {
  306. if (thrown)
  307. /* this assumes that we cannot throw plural things */
  308. hit(xname(obj) /* or: objects[obj->otyp].oc_name */ ,
  309. mon, exclam(tmp));
  310. else if (Blind)
  311. pline("You hit it.");
  312. else
  313. pline("You hit %s%s", monnam(mon), exclam(tmp));
  314. }
  315. if (u.umconf && !thrown) {
  316. if (!Blind) {
  317. pline("Your hands stop glowing blue.");
  318. if (!mon->mfroz && !mon->msleep)
  319. pline("%s appears confused.", Monnam(mon));
  320. }
  321. mon->mconf = 1;
  322. u.umconf = 0;
  323. }
  324. return (TRUE); /* mon still alive */
  325. }
  326. /* try to attack; return FALSE if monster evaded */
  327. /* u.dx and u.dy must be set */
  328. int
  329. attack(mtmp)
  330. struct monst *mtmp;
  331. {
  332. schar tmp;
  333. boolean malive = TRUE;
  334. const struct permonst *mdat;
  335. mdat = mtmp->data;
  336. u_wipe_engr(3); /* andrew@orca: prevent unlimited pick-axe
  337. * attacks */
  338. if (mdat->mlet == 'L' && !mtmp->mfroz && !mtmp->msleep &&
  339. !mtmp->mconf && mtmp->mcansee && !rn2(7) &&
  340. (m_move(mtmp, 0) == 2 /* he died */ || /* he moved: */
  341. mtmp->mx != u.ux + u.dx || mtmp->my != u.uy + u.dy))
  342. return (FALSE);
  343. if (mtmp->mimic) {
  344. if (!u.ustuck && !mtmp->mflee)
  345. u.ustuck = mtmp;
  346. switch (levl[u.ux + u.dx][u.uy + u.dy].scrsym) {
  347. case '+':
  348. pline("The door actually was a Mimic.");
  349. break;
  350. case '$':
  351. pline("The chest was a Mimic!");
  352. break;
  353. default:
  354. pline("Wait! That's a Mimic!");
  355. }
  356. wakeup(mtmp); /* clears mtmp->mimic */
  357. return (TRUE);
  358. }
  359. wakeup(mtmp);
  360. if (mtmp->mhide && mtmp->mundetected) {
  361. struct obj *obj;
  362. mtmp->mundetected = 0;
  363. if ((obj = o_at(mtmp->mx, mtmp->my)) && !Blind)
  364. pline("Wait! There's a %s hiding under %s!",
  365. mdat->mname, doname(obj));
  366. return (TRUE);
  367. }
  368. tmp = u.uluck + u.ulevel + mdat->ac + abon();
  369. if (uwep) {
  370. if (uwep->olet == WEAPON_SYM || uwep->otyp == PICK_AXE)
  371. tmp += uwep->spe;
  372. if (uwep->otyp == TWO_HANDED_SWORD)
  373. tmp -= 1;
  374. else if (uwep->otyp == DAGGER)
  375. tmp += 2;
  376. else if (uwep->otyp == CRYSKNIFE)
  377. tmp += 3;
  378. else if (uwep->otyp == SPEAR &&
  379. strchr("XDne", mdat->mlet))
  380. tmp += 2;
  381. }
  382. if (mtmp->msleep) {
  383. mtmp->msleep = 0;
  384. tmp += 2;
  385. }
  386. if (mtmp->mfroz) {
  387. tmp += 4;
  388. if (!rn2(10))
  389. mtmp->mfroz = 0;
  390. }
  391. if (mtmp->mflee)
  392. tmp += 2;
  393. if (u.utrap)
  394. tmp -= 3;
  395. /* with a lot of luggage, your agility diminishes */
  396. tmp -= (inv_weight() + 40) / 20;
  397. if (tmp <= rnd(20) && !u.uswallow) {
  398. if (Blind)
  399. pline("You miss it.");
  400. else
  401. pline("You miss %s.", monnam(mtmp));
  402. } else {
  403. /* we hit the monster; be careful: it might die! */
  404. if ((malive = hmon(mtmp, uwep, 0)) == TRUE) {
  405. /* monster still alive */
  406. if (!rn2(25) && mtmp->mhp < mtmp->mhpmax / 2) {
  407. mtmp->mflee = 1;
  408. if (!rn2(3))
  409. mtmp->mfleetim = rnd(100);
  410. if (u.ustuck == mtmp && !u.uswallow)
  411. u.ustuck = 0;
  412. }
  413. #ifndef NOWORM
  414. if (mtmp->wormno)
  415. cutworm(mtmp, u.ux + u.dx, u.uy + u.dy,
  416. uwep ? uwep->otyp : 0);
  417. #endif /* NOWORM */
  418. }
  419. if (mdat->mlet == 'a') {
  420. if (rn2(2)) {
  421. pline("You are splashed by the blob's acid!");
  422. losehp_m(rnd(6), mtmp);
  423. if (!rn2(30))
  424. corrode_armor();
  425. }
  426. if (!rn2(6))
  427. corrode_weapon();
  428. }
  429. }
  430. if (malive && mdat->mlet == 'E' && canseemon(mtmp)
  431. && !mtmp->mcan && rn2(3)) {
  432. if (mtmp->mcansee) {
  433. pline("You are frozen by the floating eye's gaze!");
  434. nomul((u.ulevel > 6 || rn2(4)) ? rn1(20, -21) : -200);
  435. } else {
  436. pline("The blinded floating eye cannot defend itself.");
  437. if (!rn2(500))
  438. if ((int) u.uluck > LUCKMIN)
  439. u.uluck--;
  440. }
  441. }
  442. return (TRUE);
  443. }