hack.mon.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /* $NetBSD: hack.mon.c,v 1.6 2003/04/02 18:36:38 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.mon.c,v 1.6 2003/04/02 18:36:38 jsm Exp $");
  64. #endif /* not lint */
  65. #include <stdlib.h>
  66. #include "hack.h"
  67. #include "extern.h"
  68. #include "hack.mfndpos.h"
  69. #ifndef NULL
  70. #define NULL (char *) 0
  71. #endif
  72. int warnlevel; /* used by movemon and dochugw */
  73. long lastwarntime;
  74. int lastwarnlev;
  75. const char *const warnings[] = {
  76. "white", "pink", "red", "ruby", "purple", "black"
  77. };
  78. void
  79. movemon()
  80. {
  81. struct monst *mtmp;
  82. int fr;
  83. warnlevel = 0;
  84. while (1) {
  85. /* find a monster that we haven't treated yet */
  86. /*
  87. * note that mtmp or mtmp->nmon might get killed while mtmp
  88. * moves, so we cannot just walk down the chain (even new
  89. * monsters might get created!)
  90. */
  91. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  92. if (mtmp->mlstmv < moves)
  93. goto next_mon;
  94. /* treated all monsters */
  95. break;
  96. next_mon:
  97. mtmp->mlstmv = moves;
  98. /* most monsters drown in pools */
  99. {
  100. boolean inpool, iseel;
  101. inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
  102. iseel = (mtmp->data->mlet == ';');
  103. if (inpool && !iseel) {
  104. if (cansee(mtmp->mx, mtmp->my))
  105. pline("%s drowns.", Monnam(mtmp));
  106. mondead(mtmp);
  107. continue;
  108. }
  109. /* but eels have a difficult time outside */
  110. if (iseel && !inpool) {
  111. if (mtmp->mhp > 1)
  112. mtmp->mhp--;
  113. mtmp->mflee = 1;
  114. mtmp->mfleetim += 2;
  115. }
  116. }
  117. if (mtmp->mblinded && !--mtmp->mblinded)
  118. mtmp->mcansee = 1;
  119. if (mtmp->mfleetim && !--mtmp->mfleetim)
  120. mtmp->mflee = 0;
  121. if (mtmp->mimic)
  122. continue;
  123. if (mtmp->mspeed != MSLOW || !(moves % 2)) {
  124. /* continue if the monster died fighting */
  125. fr = -1;
  126. if (Conflict && cansee(mtmp->mx, mtmp->my)
  127. && (fr = fightm(mtmp)) == 2)
  128. continue;
  129. if (fr < 0 && dochugw(mtmp))
  130. continue;
  131. }
  132. if (mtmp->mspeed == MFAST && dochugw(mtmp))
  133. continue;
  134. }
  135. warnlevel -= u.ulevel;
  136. if (warnlevel >= SIZE(warnings))
  137. warnlevel = SIZE(warnings) - 1;
  138. if (warnlevel >= 0)
  139. if (warnlevel > lastwarnlev || moves > lastwarntime + 5) {
  140. const char *rr;
  141. switch (Warning & (LEFT_RING | RIGHT_RING)) {
  142. case LEFT_RING:
  143. rr = "Your left ring glows";
  144. break;
  145. case RIGHT_RING:
  146. rr = "Your right ring glows";
  147. break;
  148. case LEFT_RING | RIGHT_RING:
  149. rr = "Both your rings glow";
  150. break;
  151. default:
  152. rr = "Your fingertips glow";
  153. break;
  154. }
  155. pline("%s %s!", rr, warnings[warnlevel]);
  156. lastwarntime = moves;
  157. lastwarnlev = warnlevel;
  158. }
  159. dmonsfree(); /* remove all dead monsters */
  160. }
  161. void
  162. justswld(mtmp, name)
  163. struct monst *mtmp;
  164. const char *name;
  165. {
  166. mtmp->mx = u.ux;
  167. mtmp->my = u.uy;
  168. u.ustuck = mtmp;
  169. pmon(mtmp);
  170. kludge("%s swallows you!", name);
  171. more();
  172. seeoff(1);
  173. u.uswallow = 1;
  174. u.uswldtim = 0;
  175. swallowed();
  176. }
  177. void
  178. youswld(mtmp, dam, die, name)
  179. struct monst *mtmp;
  180. int dam, die;
  181. const char *name;
  182. {
  183. if (mtmp != u.ustuck)
  184. return;
  185. kludge("%s digests you!", name);
  186. u.uhp -= dam;
  187. if ((int)u.uswldtim++ >= die) { /* a3 */
  188. pline("It totally digests you!");
  189. u.uhp = -1;
  190. }
  191. if (u.uhp < 1)
  192. done_in_by(mtmp);
  193. #if 0
  194. flags.botlx = 1; /* should we show status line ? */
  195. #endif
  196. }
  197. int
  198. dochugw(mtmp)
  199. struct monst *mtmp;
  200. {
  201. int x = mtmp->mx;
  202. int y = mtmp->my;
  203. int d = dochug(mtmp);
  204. int dd;
  205. if (!d) /* monster still alive */
  206. if (Warning)
  207. if (!mtmp->mpeaceful)
  208. if (mtmp->data->mlevel > warnlevel)
  209. if ((dd = dist(mtmp->mx, mtmp->my)) < dist(x, y))
  210. if (dd < 100)
  211. if (!canseemon(mtmp))
  212. warnlevel = mtmp->data->mlevel;
  213. return (d);
  214. }
  215. /* returns 1 if monster died moving, 0 otherwise */
  216. int
  217. dochug(mtmp)
  218. struct monst *mtmp;
  219. {
  220. const struct permonst *mdat;
  221. int tmp = 0, nearby, scared;
  222. if (mtmp->cham && !rn2(6))
  223. (void) newcham(mtmp, &mons[dlevel + 14 + rn2(CMNUM - 14 - dlevel)]);
  224. mdat = mtmp->data;
  225. if (mdat->mlevel < 0)
  226. panic("bad monster %c (%d)", mdat->mlet, mdat->mlevel);
  227. /* regenerate monsters */
  228. if ((!(moves % 20) || strchr(MREGEN, mdat->mlet)) &&
  229. mtmp->mhp < mtmp->mhpmax)
  230. mtmp->mhp++;
  231. if (mtmp->mfroz)
  232. return (0); /* frozen monsters don't do anything */
  233. if (mtmp->msleep) {
  234. /* wake up, or get out of here. */
  235. /* ettins are hard to surprise */
  236. /* Nymphs and Leprechauns do not easily wake up */
  237. if (cansee(mtmp->mx, mtmp->my) &&
  238. (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
  239. (!strchr("NL", mdat->mlet) || !rn2(50)) &&
  240. (Aggravate_monster || strchr("d1", mdat->mlet)
  241. || (!rn2(7) && !mtmp->mimic)))
  242. mtmp->msleep = 0;
  243. else
  244. return (0);
  245. }
  246. /* not frozen or sleeping: wipe out texts written in the dust */
  247. wipe_engr_at(mtmp->mx, mtmp->my, 1);
  248. /* confused monsters get unconfused with small probability */
  249. if (mtmp->mconf && !rn2(50))
  250. mtmp->mconf = 0;
  251. /* some monsters teleport */
  252. if (mtmp->mflee && strchr("tNL", mdat->mlet) && !rn2(40)) {
  253. rloc(mtmp);
  254. return (0);
  255. }
  256. if (mdat->mmove < rnd(6))
  257. return (0);
  258. /* fleeing monsters might regain courage */
  259. if (mtmp->mflee && !mtmp->mfleetim
  260. && mtmp->mhp == mtmp->mhpmax && !rn2(25))
  261. mtmp->mflee = 0;
  262. nearby = (dist(mtmp->mx, mtmp->my) < 3);
  263. scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
  264. sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
  265. if (scared && !mtmp->mflee) {
  266. mtmp->mflee = 1;
  267. mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
  268. }
  269. if (!nearby ||
  270. mtmp->mflee ||
  271. mtmp->mconf ||
  272. (mtmp->minvis && !rn2(3)) ||
  273. (strchr("BIuy", mdat->mlet) && !rn2(4)) ||
  274. (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
  275. (!mtmp->mcansee && !rn2(4)) ||
  276. mtmp->mpeaceful
  277. ) {
  278. tmp = m_move(mtmp, 0); /* 2: monster died moving */
  279. if (tmp == 2 || (tmp && mdat->mmove <= 12))
  280. return (tmp == 2);
  281. }
  282. if (!strchr("Ea", mdat->mlet) && nearby &&
  283. !mtmp->mpeaceful && u.uhp > 0 && !scared) {
  284. if (mhitu(mtmp))
  285. return (1); /* monster died (e.g. 'y' or 'F') */
  286. }
  287. /* extra movement for fast monsters */
  288. if (mdat->mmove - 12 > rnd(12))
  289. tmp = m_move(mtmp, 1);
  290. return (tmp == 2);
  291. }
  292. int
  293. m_move(struct monst *mtmp, int after)
  294. {
  295. struct monst *mtmp2;
  296. int nx, ny, omx, omy, appr, nearer, cnt, i, j;
  297. xchar gx, gy, nix, niy, chcnt;
  298. schar chi;
  299. boolean likegold = 0, likegems = 0, likeobjs = 0;
  300. char msym = mtmp->data->mlet;
  301. schar mmoved = 0; /* not strictly nec.: chi >= 0 will
  302. * do */
  303. coord poss[9];
  304. int info[9];
  305. if (mtmp->mfroz || mtmp->msleep)
  306. return (0);
  307. if (mtmp->mtrapped) {
  308. i = mintrap(mtmp);
  309. if (i == 2)
  310. return (2); /* he died */
  311. if (i == 1)
  312. return (0); /* still in trap, so didnt move */
  313. }
  314. if (mtmp->mhide && o_at(mtmp->mx, mtmp->my) && rn2(10))
  315. return (0); /* do not leave hiding place */
  316. #ifndef NOWORM
  317. if (mtmp->wormno)
  318. goto not_special;
  319. #endif /* NOWORM */
  320. /* my dog gets a special treatment */
  321. if (mtmp->mtame) {
  322. return (dog_move(mtmp, after));
  323. }
  324. /* likewise for shopkeeper */
  325. if (mtmp->isshk) {
  326. mmoved = shk_move(mtmp);
  327. if (mmoved >= 0)
  328. goto postmov;
  329. mmoved = 0; /* follow player outside shop */
  330. }
  331. /* and for the guard */
  332. if (mtmp->isgd) {
  333. mmoved = gd_move();
  334. goto postmov;
  335. }
  336. /*
  337. * teleport if that lies in our nature ('t') or when badly wounded
  338. * ('1')
  339. */
  340. if ((msym == 't' && !rn2(5))
  341. || (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
  342. || levl[u.ux][u.uy].typ == STAIRS))) {
  343. if (mtmp->mhp < 7 || (msym == 't' && rn2(2)))
  344. rloc(mtmp);
  345. else
  346. mnexto(mtmp);
  347. mmoved = 1;
  348. goto postmov;
  349. }
  350. /* spit fire ('D') or use a wand ('1') when appropriate */
  351. if (strchr("D1", msym))
  352. inrange(mtmp);
  353. if (msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
  354. mtmp->mcansee && rn2(5)) {
  355. if (!Confusion)
  356. pline("%s's gaze has confused you!", Monnam(mtmp));
  357. else
  358. pline("You are getting more and more confused.");
  359. if (rn2(3))
  360. mtmp->mcan = 1;
  361. Confusion += d(3, 4); /* timeout */
  362. }
  363. not_special:
  364. if (!mtmp->mflee && u.uswallow && u.ustuck != mtmp)
  365. return (1);
  366. appr = 1;
  367. if (mtmp->mflee)
  368. appr = -1;
  369. if (mtmp->mconf || Invis || !mtmp->mcansee ||
  370. (strchr("BIy", msym) && !rn2(3)))
  371. appr = 0;
  372. omx = mtmp->mx;
  373. omy = mtmp->my;
  374. gx = u.ux;
  375. gy = u.uy;
  376. if (msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
  377. appr = -1;
  378. /*
  379. * random criterion for 'smell' or track finding ability should use
  380. * mtmp->msmell or sth
  381. */
  382. if (msym == '@' ||
  383. ('a' <= msym && msym <= 'z')) {
  384. coord *cp;
  385. schar mroom;
  386. mroom = inroom(omx, omy);
  387. if (mroom < 0 || mroom != inroom(u.ux, u.uy)) {
  388. cp = gettrack(omx, omy);
  389. if (cp) {
  390. gx = cp->x;
  391. gy = cp->y;
  392. }
  393. }
  394. }
  395. /* look for gold or jewels nearby */
  396. likegold = (strchr("LOD", msym) != NULL);
  397. likegems = (strchr("ODu", msym) != NULL);
  398. likeobjs = mtmp->mhide;
  399. #define SRCHRADIUS 25
  400. {
  401. xchar mind = SRCHRADIUS; /* not too far away */
  402. int dd;
  403. if (likegold) {
  404. struct gold *gold;
  405. for (gold = fgold; gold; gold = gold->ngold)
  406. if ((dd = DIST(omx, omy, gold->gx, gold->gy)) < mind) {
  407. mind = dd;
  408. gx = gold->gx;
  409. gy = gold->gy;
  410. }
  411. }
  412. if (likegems || likeobjs) {
  413. struct obj *otmp;
  414. for (otmp = fobj; otmp; otmp = otmp->nobj)
  415. if (likeobjs || otmp->olet == GEM_SYM)
  416. if (msym != 'u' ||
  417. objects[otmp->otyp].g_val != 0)
  418. if ((dd = DIST(omx, omy, otmp->ox, otmp->oy)) < mind) {
  419. mind = dd;
  420. gx = otmp->ox;
  421. gy = otmp->oy;
  422. }
  423. }
  424. if (mind < SRCHRADIUS && appr == -1) {
  425. if (dist(omx, omy) < 10) {
  426. gx = u.ux;
  427. gy = u.uy;
  428. } else
  429. appr = 1;
  430. }
  431. }
  432. nix = omx;
  433. niy = omy;
  434. cnt = mfndpos(mtmp, poss, info,
  435. msym == 'u' ? NOTONL :
  436. (msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
  437. strchr(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
  438. /* ALLOW_ROCK for some monsters ? */
  439. chcnt = 0;
  440. chi = -1;
  441. for (i = 0; i < cnt; i++) {
  442. nx = poss[i].x;
  443. ny = poss[i].y;
  444. for (j = 0; j < MTSZ && j < cnt - 1; j++)
  445. if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
  446. if (rn2(4 * (cnt - j)))
  447. goto nxti;
  448. #ifdef STUPID
  449. /* some stupid compilers think that this is too complicated */
  450. {
  451. int d1 = DIST(nx, ny, gx, gy);
  452. int d2 = DIST(nix, niy, gx, gy);
  453. nearer = (d1 < d2);
  454. }
  455. #else
  456. nearer = (DIST(nx, ny, gx, gy) < DIST(nix, niy, gx, gy));
  457. #endif /* STUPID */
  458. if ((appr == 1 && nearer) || (appr == -1 && !nearer) ||
  459. !mmoved ||
  460. (!appr && !rn2(++chcnt))) {
  461. nix = nx;
  462. niy = ny;
  463. chi = i;
  464. mmoved = 1;
  465. }
  466. nxti: ;
  467. }
  468. if (mmoved) {
  469. if (info[chi] & ALLOW_M) {
  470. mtmp2 = m_at(nix, niy);
  471. if (hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
  472. hitmm(mtmp2, mtmp) == 2)
  473. return (2);
  474. return (0);
  475. }
  476. if (info[chi] & ALLOW_U) {
  477. (void) hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd) + 1);
  478. return (0);
  479. }
  480. mtmp->mx = nix;
  481. mtmp->my = niy;
  482. for (j = MTSZ - 1; j > 0; j--)
  483. mtmp->mtrack[j] = mtmp->mtrack[j - 1];
  484. mtmp->mtrack[0].x = omx;
  485. mtmp->mtrack[0].y = omy;
  486. #ifndef NOWORM
  487. if (mtmp->wormno)
  488. worm_move(mtmp);
  489. #endif /* NOWORM */
  490. } else {
  491. if (msym == 'u' && rn2(2)) {
  492. rloc(mtmp);
  493. return (0);
  494. }
  495. #ifndef NOWORM
  496. if (mtmp->wormno)
  497. worm_nomove(mtmp);
  498. #endif /* NOWORM */
  499. }
  500. postmov:
  501. if (mmoved == 1) {
  502. if (mintrap(mtmp) == 2) /* he died */
  503. return (2);
  504. if (likegold)
  505. mpickgold(mtmp);
  506. if (likegems)
  507. mpickgems(mtmp);
  508. if (mtmp->mhide)
  509. mtmp->mundetected = 1;
  510. }
  511. pmon(mtmp);
  512. return (mmoved);
  513. }
  514. void
  515. mpickgold(mtmp)
  516. struct monst *mtmp;
  517. {
  518. struct gold *gold;
  519. while ((gold = g_at(mtmp->mx, mtmp->my)) != NULL) {
  520. mtmp->mgold += gold->amount;
  521. freegold(gold);
  522. if (levl[mtmp->mx][mtmp->my].scrsym == '$')
  523. newsym(mtmp->mx, mtmp->my);
  524. }
  525. }
  526. void
  527. mpickgems(mtmp)
  528. struct monst *mtmp;
  529. {
  530. struct obj *otmp;
  531. for (otmp = fobj; otmp; otmp = otmp->nobj)
  532. if (otmp->olet == GEM_SYM)
  533. if (otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
  534. if (mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0) {
  535. freeobj(otmp);
  536. mpickobj(mtmp, otmp);
  537. if (levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
  538. newsym(mtmp->mx, mtmp->my); /* %% */
  539. return; /* pick only one object */
  540. }
  541. }
  542. /* return number of acceptable neighbour positions */
  543. int
  544. mfndpos(mon, poss, info, flag)
  545. struct monst *mon;
  546. coord poss[9];
  547. int info[9], flag;
  548. {
  549. int x, y, nx, ny, cnt = 0, ntyp;
  550. struct monst *mtmp;
  551. int nowtyp;
  552. boolean pool;
  553. x = mon->mx;
  554. y = mon->my;
  555. nowtyp = levl[x][y].typ;
  556. pool = (mon->data->mlet == ';');
  557. nexttry: /* eels prefer the water, but if there is no
  558. * water nearby, they will crawl over land */
  559. if (mon->mconf) {
  560. flag |= ALLOW_ALL;
  561. flag &= ~NOTONL;
  562. }
  563. for (nx = x - 1; nx <= x + 1; nx++)
  564. for (ny = y - 1; ny <= y + 1; ny++)
  565. if (nx != x || ny != y)
  566. if (isok(nx, ny))
  567. if (!IS_ROCK(ntyp = levl[nx][ny].typ))
  568. if (!(nx != x && ny != y && (nowtyp == DOOR || ntyp == DOOR)))
  569. if ((ntyp == POOL) == pool) {
  570. info[cnt] = 0;
  571. if (nx == u.ux && ny == u.uy) {
  572. if (!(flag & ALLOW_U))
  573. continue;
  574. info[cnt] = ALLOW_U;
  575. } else if ((mtmp = m_at(nx, ny)) != NULL) {
  576. if (!(flag & ALLOW_M))
  577. continue;
  578. info[cnt] = ALLOW_M;
  579. if (mtmp->mtame) {
  580. if (!(flag & ALLOW_TM))
  581. continue;
  582. info[cnt] |= ALLOW_TM;
  583. }
  584. }
  585. if (sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
  586. if (flag & NOGARLIC)
  587. continue;
  588. info[cnt] |= NOGARLIC;
  589. }
  590. if (sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
  591. (!mon->mpeaceful && sengr_at("Elbereth", nx, ny))) {
  592. if (!(flag & ALLOW_SSM))
  593. continue;
  594. info[cnt] |= ALLOW_SSM;
  595. }
  596. if (sobj_at(ENORMOUS_ROCK, nx, ny)) {
  597. if (!(flag & ALLOW_ROCK))
  598. continue;
  599. info[cnt] |= ALLOW_ROCK;
  600. }
  601. if (!Invis && online(nx, ny)) {
  602. if (flag & NOTONL)
  603. continue;
  604. info[cnt] |= NOTONL;
  605. }
  606. /*
  607. * we cannot
  608. * avoid
  609. * traps of
  610. * an unknown
  611. * kind
  612. */
  613. {
  614. struct trap *ttmp = t_at(nx, ny);
  615. int tt;
  616. if (ttmp) {
  617. tt = 1 << ttmp->ttyp;
  618. if (mon->mtrapseen & tt) {
  619. if (!(flag & tt))
  620. continue;
  621. info[cnt] |= tt;
  622. }
  623. }
  624. }
  625. poss[cnt].x = nx;
  626. poss[cnt].y = ny;
  627. cnt++;
  628. }
  629. if (!cnt && pool && nowtyp != POOL) {
  630. pool = FALSE;
  631. goto nexttry;
  632. }
  633. return (cnt);
  634. }
  635. int
  636. dist(x, y)
  637. int x, y;
  638. {
  639. return ((x - u.ux) * (x - u.ux) + (y - u.uy) * (y - u.uy));
  640. }
  641. void
  642. poisoned(string, pname)
  643. const char *string, *pname;
  644. {
  645. int i;
  646. if (Blind)
  647. pline("It was poisoned.");
  648. else
  649. pline("The %s was poisoned!", string);
  650. if (Poison_resistance) {
  651. pline("The poison doesn't seem to affect you.");
  652. return;
  653. }
  654. i = rn2(10);
  655. if (i == 0) {
  656. u.uhp = -1;
  657. pline("I am afraid the poison was deadly ...");
  658. } else if (i <= 5) {
  659. losestr(rn1(3, 3));
  660. } else {
  661. losehp(rn1(10, 6), pname);
  662. }
  663. if (u.uhp < 1) {
  664. killer = pname;
  665. done("died");
  666. }
  667. }
  668. void
  669. mondead(mtmp)
  670. struct monst *mtmp;
  671. {
  672. relobj(mtmp, 1);
  673. unpmon(mtmp);
  674. relmon(mtmp);
  675. unstuck(mtmp);
  676. if (mtmp->isshk)
  677. shkdead(mtmp);
  678. if (mtmp->isgd)
  679. gddead();
  680. #ifndef NOWORM
  681. if (mtmp->wormno)
  682. wormdead(mtmp);
  683. #endif /* NOWORM */
  684. monfree(mtmp);
  685. }
  686. /* called when monster is moved to larger structure */
  687. void
  688. replmon(mtmp, mtmp2)
  689. struct monst *mtmp, *mtmp2;
  690. {
  691. relmon(mtmp);
  692. monfree(mtmp);
  693. mtmp2->nmon = fmon;
  694. fmon = mtmp2;
  695. if (u.ustuck == mtmp)
  696. u.ustuck = mtmp2;
  697. if (mtmp2->isshk)
  698. replshk(mtmp, mtmp2);
  699. if (mtmp2->isgd)
  700. replgd(mtmp, mtmp2);
  701. }
  702. void
  703. relmon(mon)
  704. struct monst *mon;
  705. {
  706. struct monst *mtmp;
  707. if (mon == fmon)
  708. fmon = fmon->nmon;
  709. else {
  710. for (mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon);
  711. mtmp->nmon = mon->nmon;
  712. }
  713. }
  714. /*
  715. * we do not free monsters immediately, in order to have their name available
  716. * shortly after their demise
  717. */
  718. struct monst *fdmon; /* chain of dead monsters, need not to be
  719. * saved */
  720. void
  721. monfree(mtmp)
  722. struct monst *mtmp;
  723. {
  724. mtmp->nmon = fdmon;
  725. fdmon = mtmp;
  726. }
  727. void
  728. dmonsfree()
  729. {
  730. struct monst *mtmp;
  731. while ((mtmp = fdmon) != NULL) {
  732. fdmon = mtmp->nmon;
  733. free((char *) mtmp);
  734. }
  735. }
  736. void
  737. unstuck(mtmp)
  738. struct monst *mtmp;
  739. {
  740. if (u.ustuck == mtmp) {
  741. if (u.uswallow) {
  742. u.ux = mtmp->mx;
  743. u.uy = mtmp->my;
  744. u.uswallow = 0;
  745. setsee();
  746. docrt();
  747. }
  748. u.ustuck = 0;
  749. }
  750. }
  751. void
  752. killed(mtmp)
  753. struct monst *mtmp;
  754. {
  755. #ifdef lint
  756. #define NEW_SCORING
  757. #endif /* lint */
  758. int tmp, nk, x, y;
  759. const struct permonst *mdat;
  760. if (mtmp->cham)
  761. mtmp->data = PM_CHAMELEON;
  762. mdat = mtmp->data;
  763. if (Blind)
  764. pline("You destroy it!");
  765. else {
  766. pline("You destroy %s!",
  767. mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
  768. }
  769. if (u.umconf) {
  770. if (!Blind)
  771. pline("Your hands stop glowing blue.");
  772. u.umconf = 0;
  773. }
  774. /* count killed monsters */
  775. #define MAXMONNO 100
  776. nk = 1; /* in case we cannot find it in mons */
  777. tmp = mdat - mons; /* strchr in mons array (if not 'd', '@', ...) */
  778. if (tmp >= 0 && tmp < CMNUM + 2) {
  779. u.nr_killed[tmp]++;
  780. if ((nk = u.nr_killed[tmp]) > MAXMONNO &&
  781. !strchr(fut_geno, mdat->mlet))
  782. charcat(fut_geno, mdat->mlet);
  783. }
  784. /* punish bad behaviour */
  785. if (mdat->mlet == '@')
  786. Telepat = 0, u.uluck -= 2;
  787. if (mtmp->mpeaceful || mtmp->mtame)
  788. u.uluck--;
  789. if (mdat->mlet == 'u')
  790. u.uluck -= 5;
  791. if ((int) u.uluck < LUCKMIN)
  792. u.uluck = LUCKMIN;
  793. /* give experience points */
  794. tmp = 1 + mdat->mlevel * mdat->mlevel;
  795. if (mdat->ac < 3)
  796. tmp += 2 * (7 - mdat->ac);
  797. if (strchr("AcsSDXaeRTVWU&In:P", mdat->mlet))
  798. tmp += 2 * mdat->mlevel;
  799. if (strchr("DeV&P", mdat->mlet))
  800. tmp += (7 * mdat->mlevel);
  801. if (mdat->mlevel > 6)
  802. tmp += 50;
  803. if (mdat->mlet == ';')
  804. tmp += 1000;
  805. #ifdef NEW_SCORING
  806. /*
  807. * ------- recent addition: make nr of points decrease when this is
  808. * not the first of this kind
  809. */
  810. {
  811. int ul = u.ulevel;
  812. int ml = mdat->mlevel;
  813. if (ul < 14) /* points are given based on present and
  814. * future level */
  815. for (tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
  816. if (u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4 << (tmp2 - 1))) / nk
  817. >= 10 * pow((unsigned) (ul - 1)))
  818. if (++ul == 14)
  819. break;
  820. tmp2 = ml - ul - 1;
  821. tmp = (tmp + ((tmp2 < 0) ? 0 : 4 << tmp2)) / nk;
  822. if (!tmp)
  823. tmp = 1;
  824. }
  825. /* note: ul is not necessarily the future value of u.ulevel */
  826. /* ------- end of recent valuation change ------- */
  827. #endif /* NEW_SCORING */
  828. more_experienced(tmp, 0);
  829. flags.botl = 1;
  830. while (u.ulevel < 14 && u.uexp >= newuexp()) {
  831. pline("Welcome to experience level %u.", ++u.ulevel);
  832. tmp = rnd(10);
  833. if (tmp < 3)
  834. tmp = rnd(10);
  835. u.uhpmax += tmp;
  836. u.uhp += tmp;
  837. flags.botl = 1;
  838. }
  839. /* dispose of monster and make cadaver */
  840. x = mtmp->mx;
  841. y = mtmp->my;
  842. mondead(mtmp);
  843. tmp = mdat->mlet;
  844. if (tmp == 'm') { /* he killed a minotaur, give him a wand of
  845. * digging */
  846. /* note: the dead minotaur will be on top of it! */
  847. mksobj_at(WAN_DIGGING, x, y);
  848. /* if(cansee(x,y)) atl(x,y,fobj->olet); */
  849. stackobj(fobj);
  850. } else
  851. #ifndef NOWORM
  852. if (tmp == 'w') {
  853. mksobj_at(WORM_TOOTH, x, y);
  854. stackobj(fobj);
  855. } else
  856. #endif /* NOWORM */
  857. if (!letter(tmp) || (!strchr("mw", tmp) && !rn2(3)))
  858. tmp = 0;
  859. if (ACCESSIBLE(levl[x][y].typ)) /* might be mimic in wall or dead eel */
  860. if (x != u.ux || y != u.uy) /* might be here after
  861. * swallowed */
  862. if (strchr("NTVm&", mdat->mlet) || rn2(5)) {
  863. struct obj *obj2 = mkobj_at(tmp, x, y);
  864. if (cansee(x, y))
  865. atl(x, y, obj2->olet);
  866. stackobj(obj2);
  867. }
  868. }
  869. void
  870. kludge(const char *str, const char *arg)
  871. {
  872. if (Blind) {
  873. if (*str == '%')
  874. pline(str, "It");
  875. else
  876. pline(str, "it");
  877. } else
  878. pline(str, arg);
  879. }
  880. void
  881. rescham()
  882. { /* force all chameleons to become normal */
  883. struct monst *mtmp;
  884. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  885. if (mtmp->cham) {
  886. mtmp->cham = 0;
  887. (void) newcham(mtmp, PM_CHAMELEON);
  888. }
  889. }
  890. int
  891. newcham(mtmp, mdat) /* make a chameleon look like a new monster */
  892. /* returns 1 if the monster actually changed */
  893. struct monst *mtmp;
  894. const struct permonst *mdat;
  895. {
  896. int mhp, hpn, hpd;
  897. if (mdat == mtmp->data)
  898. return (0); /* still the same monster */
  899. #ifndef NOWORM
  900. if (mtmp->wormno)
  901. wormdead(mtmp); /* throw tail away */
  902. #endif /* NOWORM */
  903. if (u.ustuck == mtmp) {
  904. if (u.uswallow) {
  905. u.uswallow = 0;
  906. u.uswldtim = 0;
  907. mnexto(mtmp);
  908. docrt();
  909. prme();
  910. }
  911. u.ustuck = 0;
  912. }
  913. hpn = mtmp->mhp;
  914. hpd = (mtmp->data->mlevel) * 8;
  915. if (!hpd)
  916. hpd = 4;
  917. mtmp->data = mdat;
  918. mhp = (mdat->mlevel) * 8;
  919. /* new hp: same fraction of max as before */
  920. mtmp->mhp = 2 + (hpn * mhp) / hpd;
  921. hpn = mtmp->mhpmax;
  922. mtmp->mhpmax = 2 + (hpn * mhp) / hpd;
  923. mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
  924. #ifndef NOWORM
  925. if (mdat->mlet == 'w' && getwn(mtmp))
  926. initworm(mtmp);
  927. /* perhaps we should clear mtmp->mtame here? */
  928. #endif /* NOWORM */
  929. unpmon(mtmp); /* necessary for 'I' and to force pmon */
  930. pmon(mtmp);
  931. return (1);
  932. }
  933. void
  934. mnexto(mtmp) /* Make monster mtmp next to you (if
  935. * possible) */
  936. struct monst *mtmp;
  937. {
  938. coord mm;
  939. mm = enexto(u.ux, u.uy);
  940. mtmp->mx = mm.x;
  941. mtmp->my = mm.y;
  942. pmon(mtmp);
  943. }
  944. int
  945. ishuman(mtmp)
  946. struct monst *mtmp;
  947. {
  948. return (mtmp->data->mlet == '@');
  949. }
  950. void
  951. setmangry(mtmp)
  952. struct monst *mtmp;
  953. {
  954. if (!mtmp->mpeaceful)
  955. return;
  956. if (mtmp->mtame)
  957. return;
  958. mtmp->mpeaceful = 0;
  959. if (ishuman(mtmp))
  960. pline("%s gets angry!", Monnam(mtmp));
  961. }
  962. /*
  963. * not one hundred procent correct: now a snake may hide under an invisible
  964. * object
  965. */
  966. int
  967. canseemon(mtmp)
  968. struct monst *mtmp;
  969. {
  970. return ((!mtmp->minvis || See_invisible)
  971. && (!mtmp->mhide || !o_at(mtmp->mx, mtmp->my))
  972. && cansee(mtmp->mx, mtmp->my));
  973. }