1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.mon.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.mon.c,v 1.5 1999/11/16 10:26:37 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.mon.c,v 1.6 2008/06/05 18:01:49 swildner Exp $ */
7 #include "hack.mfndpos.h"
9 int warnlevel; /* used by movemon and dochugw */
12 static const char *warnings[] = {
13 "white", "pink", "red", "ruby", "purple", "black"
16 static int dochugw(struct monst *);
17 static void mpickgold(struct monst *);
18 static void mpickgems(struct monst *);
19 static void dmonsfree(void);
20 static bool ishuman(struct monst *);
31 /* find a monster that we haven't treated yet */
33 * note that mtmp or mtmp->nmon might get killed while mtmp
34 * moves, so we cannot just walk down the chain (even new
35 * monsters might get created!)
37 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
38 if (mtmp->mlstmv < moves)
40 /* treated all monsters */
46 /* most monsters drown in pools */
48 boolean inpool, iseel;
50 inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
51 iseel = (mtmp->data->mlet == ';');
52 if (inpool && !iseel) {
53 if (cansee(mtmp->mx, mtmp->my))
54 pline("%s drowns.", Monnam(mtmp));
58 /* but eels have a difficult time outside */
59 if (iseel && !inpool) {
66 if (mtmp->mblinded && !--mtmp->mblinded)
68 if (mtmp->mfleetim && !--mtmp->mfleetim)
72 if (mtmp->mspeed != MSLOW || !(moves % 2)) {
73 /* continue if the monster died fighting */
75 if (Conflict && cansee(mtmp->mx, mtmp->my)
76 && (fr = fightm(mtmp)) == 2)
78 if (fr < 0 && dochugw(mtmp))
81 if (mtmp->mspeed == MFAST && dochugw(mtmp))
85 warnlevel -= u.ulevel;
86 if (warnlevel >= SIZE(warnings))
87 warnlevel = SIZE(warnings) - 1;
89 if (warnlevel > lastwarnlev || moves > lastwarntime + 5) {
91 switch (Warning & (LEFT_RING | RIGHT_RING)) {
93 rr = "Your left ring glows";
96 rr = "Your right ring glows";
98 case LEFT_RING | RIGHT_RING:
99 rr = "Both your rings glow";
102 rr = "Your fingertips glow";
105 pline("%s %s!", rr, warnings[warnlevel]);
106 lastwarntime = moves;
107 lastwarnlev = warnlevel;
110 dmonsfree(); /* remove all dead monsters */
114 justswld(struct monst *mtmp, const char *name)
120 kludge("%s swallows you!", name);
129 youswld(struct monst *mtmp, int dam, int die, const char *name)
131 if (mtmp != u.ustuck)
133 kludge("%s digests you!", name);
135 if ((int)u.uswldtim++ >= die) { /* a3 */
136 pline("It totally digests you!");
142 flags.botlx = 1; /* should we show status line ? */
147 dochugw(struct monst *mtmp)
151 int d1 = dochug(mtmp);
154 if (!d1) /* monster still alive */
156 if (!mtmp->mpeaceful)
157 if (mtmp->data->mlevel > warnlevel)
158 if ((dd = dist(mtmp->mx, mtmp->my)) <
161 if (!canseemon(mtmp))
169 /* returns 1 if monster died moving, 0 otherwise */
171 dochug(struct monst *mtmp)
173 struct permonst *mdat;
174 int tmp = 0, nearby, scared;
176 if (mtmp->cham && !rn2(6))
177 newcham(mtmp, &mons[dlevel + 14 + rn2(CMNUM - 14 - dlevel)]);
179 if (mdat->mlevel < 0)
180 panic("bad monster %c (%d)", mdat->mlet, mdat->mlevel);
182 /* regenerate monsters */
183 if ((!(moves % 20) || strchr(MREGEN, mdat->mlet)) &&
184 mtmp->mhp < mtmp->mhpmax)
187 if (mtmp->mfroz) /* frozen monsters don't do anything */
191 /* wake up, or get out of here. */
192 /* ettins are hard to surprise */
193 /* Nymphs and Leprechauns do not easily wake up */
194 if (cansee(mtmp->mx, mtmp->my) &&
195 (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
196 (!strchr("NL", mdat->mlet) || !rn2(50)) &&
197 (Aggravate_monster || strchr("d1", mdat->mlet)
198 || (!rn2(7) && !mtmp->mimic)))
204 /* not frozen or sleeping: wipe out texts written in the dust */
205 wipe_engr_at(mtmp->mx, mtmp->my, 1);
207 /* confused monsters get unconfused with small probability */
208 if (mtmp->mconf && !rn2(50))
211 /* some monsters teleport */
212 if (mtmp->mflee && strchr("tNL", mdat->mlet) && !rn2(40)) {
216 if (mdat->mmove < rnd(6))
219 /* fleeing monsters might regain courage */
220 if (mtmp->mflee && !mtmp->mfleetim
221 && mtmp->mhp == mtmp->mhpmax && !rn2(25))
224 nearby = (dist(mtmp->mx, mtmp->my) < 3);
225 scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
226 sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
227 if (scared && !mtmp->mflee) {
229 mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
235 (mtmp->minvis && !rn2(3)) ||
236 (strchr("BIuy", mdat->mlet) && !rn2(4)) ||
237 (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
238 (!mtmp->mcansee && !rn2(4)) ||
241 tmp = m_move(mtmp, 0); /* 2: monster died moving */
242 if (tmp == 2 || (tmp && mdat->mmove <= 12))
246 if (!strchr("Ea", mdat->mlet) && nearby &&
247 !mtmp->mpeaceful && u.uhp > 0 && !scared) {
249 return (1); /* monster died (e.g. 'y' or 'F') */
251 /* extra movement for fast monsters */
252 if (mdat->mmove - 12 > rnd(12))
253 tmp = m_move(mtmp, 1);
258 m_move(struct monst *mtmp, int after)
261 int nx, ny, omx, omy, appr, nearer, cnt, i, j;
262 xchar gx, gy, nix, niy, chcnt;
264 boolean likegold = 0, likegems = 0, likeobjs;
265 char msym = mtmp->data->mlet;
266 schar mmoved = 0; /* not strictly nec.: chi >= 0 will do */
270 if (mtmp->mfroz || mtmp->msleep)
272 if (mtmp->mtrapped) {
274 if (i == 2) /* he died */
276 if (i == 1) /* still in trap, so didnt move */
279 if (mtmp->mhide && o_at(mtmp->mx, mtmp->my) && rn2(10))
280 return (0); /* do not leave hiding place */
287 /* my dog gets a special treatment */
289 return (dog_move(mtmp, after));
291 /* likewise for shopkeeper */
293 mmoved = shk_move(mtmp);
296 mmoved = 0; /* follow player outside shop */
299 /* and for the guard */
305 /* teleport if that lies in our nature ('t') or when badly wounded ('1') */
306 if ((msym == 't' && !rn2(5))
307 || (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
308 || levl[u.ux][u.uy].typ == STAIRS))) {
309 if (mtmp->mhp < 7 || (msym == 't' && rn2(2)))
317 /* spit fire ('D') or use a wand ('1') when appropriate */
318 if (strchr("D1", msym))
321 if (msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
322 mtmp->mcansee && rn2(5)) {
324 pline("%s's gaze has confused you!", Monnam(mtmp));
326 pline("You are getting more and more confused.");
329 Confusion += d(3, 4); /* timeout */
332 if (!mtmp->mflee && u.uswallow && u.ustuck != mtmp)
337 if (mtmp->mconf || Invis || !mtmp->mcansee ||
338 (strchr("BIy", msym) && !rn2(3)))
344 if (msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
348 * random criterion for 'smell' or track finding ability should use
349 * mtmp->msmell or sth
352 ('a' <= msym && msym <= 'z')) {
355 mroom = inroom(omx, omy);
356 if (mroom < 0 || mroom != inroom(u.ux, u.uy)) {
357 cp = gettrack(omx, omy);
365 /* look for gold or jewels nearby */
366 likegold = (strchr("LOD", msym) != NULL);
367 likegems = (strchr("ODu", msym) != NULL);
368 likeobjs = mtmp->mhide;
369 #define SRCHRADIUS 25
371 xchar mind = SRCHRADIUS; /* not too far away */
375 for (gold = fgold; gold; gold = gold->ngold)
376 if ((dd = DIST(omx, omy, gold->gx,
383 if (likegems || likeobjs) {
385 for (otmp = fobj; otmp; otmp = otmp->nobj)
386 if (likeobjs || otmp->olet == GEM_SYM)
388 objects[otmp->otyp].g_val != 0)
389 if ((dd = DIST(omx, omy, otmp->ox,
396 if (mind < SRCHRADIUS && appr == -1) {
397 if (dist(omx, omy) < 10) {
406 cnt = mfndpos(mtmp, poss, info,
407 msym == 'u' ? NOTONL :
408 (msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
409 strchr(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
410 /* ALLOW_ROCK for some monsters ? */
413 for (i = 0; i < cnt; i++) {
416 for (j = 0; j < MTSZ && j < cnt - 1; j++)
417 if (nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
418 if (rn2(4 * (cnt - j)))
421 /* some stupid compilers think that this is too complicated */
423 int d1 = DIST(nx, ny, gx, gy);
424 int d2 = DIST(nix, niy, gx, gy);
428 nearer = (DIST(nx, ny, gx, gy) < DIST(nix, niy, gx, gy));
430 if ((appr == 1 && nearer) || (appr == -1 && !nearer) ||
432 (!appr && !rn2(++chcnt))) {
441 if (info[chi] & ALLOW_M) {
442 mtmp2 = m_at(nix, niy);
443 if (hitmm(mtmp, mtmp2) == 1 && rn2(4) &&
444 hitmm(mtmp2, mtmp) == 2)
448 if (info[chi] & ALLOW_U) {
449 hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd) + 1);
454 for (j = MTSZ - 1; j > 0; j--)
455 mtmp->mtrack[j] = mtmp->mtrack[j - 1];
456 mtmp->mtrack[0].x = omx;
457 mtmp->mtrack[0].y = omy;
463 if (msym == 'u' && rn2(2)) {
474 if (mintrap(mtmp) == 2) /* he died */
481 mtmp->mundetected = 1;
488 mpickgold(struct monst *mtmp)
492 while ((gold = g_at(mtmp->mx, mtmp->my)) != NULL) {
493 mtmp->mgold += gold->amount;
495 if (levl[mtmp->mx][mtmp->my].scrsym == '$')
496 newsym(mtmp->mx, mtmp->my);
501 mpickgems(struct monst *mtmp)
505 for (otmp = fobj; otmp; otmp = otmp->nobj)
506 if (otmp->olet == GEM_SYM)
507 if (otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
508 if (mtmp->data->mlet != 'u' ||
509 objects[otmp->otyp].g_val != 0) {
511 mpickobj(mtmp, otmp);
512 if (levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
513 newsym(mtmp->mx, mtmp->my); /* %% */
514 return; /* pick only one object */
518 /* return number of acceptable neighbour positions */
520 mfndpos(struct monst *mon, coord poss[9], int info[9], int flag)
522 int x, y, nx, ny, cnt = 0, ntyp;
529 nowtyp = levl[x][y].typ;
531 pool = (mon->data->mlet == ';');
534 * eels prefer the water, but if there is no water nearby, they will
541 for (nx = x - 1; nx <= x + 1; nx++)
542 for (ny = y - 1; ny <= y + 1; ny++)
543 if (nx != x || ny != y)
545 if (!IS_ROCK(ntyp = levl[nx][ny].typ))
546 if (!(nx != x && ny != y &&
547 (nowtyp == DOOR || ntyp == DOOR)))
548 if ((ntyp == POOL) == pool) {
550 if (nx == u.ux && ny == u.uy) {
551 if (!(flag & ALLOW_U))
554 } else if ((mtmp = m_at(nx, ny)) != NULL) {
555 if (!(flag & ALLOW_M))
559 if (!(flag & ALLOW_TM))
561 info[cnt] |= ALLOW_TM;
564 if (sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
567 info[cnt] |= NOGARLIC;
569 if (sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
571 sengr_at("Elbereth", nx, ny))) {
572 if (!(flag & ALLOW_SSM))
574 info[cnt] |= ALLOW_SSM;
576 if (sobj_at(ENORMOUS_ROCK, nx, ny)) {
577 if (!(flag & ALLOW_ROCK))
579 info[cnt] |= ALLOW_ROCK;
581 if (!Invis && online(nx, ny)) {
586 /* we cannot avoid traps of an unknown kind */
588 struct trap *ttmp = t_at(nx, ny);
591 tt = 1 << ttmp->ttyp;
592 if (mon->mtrapseen & tt) {
603 if (!cnt && pool && nowtyp != POOL) {
613 return ((x - u.ux) * (x - u.ux) + (y - u.uy) * (y - u.uy));
617 poisoned(const char *string, const char *pname)
622 pline("It was poisoned.");
624 pline("The %s was poisoned!", string);
625 if (Poison_resistance) {
626 pline("The poison doesn't seem to affect you.");
632 pline("I am afraid the poison was deadly ...");
636 losehp(rn1(10, 6), pname);
645 mondead(struct monst *mtmp)
662 /* called when monster is moved to larger structure */
664 replmon(struct monst *mtmp, struct monst *mtmp2)
670 if (u.ustuck == mtmp)
673 replshk(mtmp, mtmp2);
679 relmon(struct monst *mon)
686 for (mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon)
688 mtmp->nmon = mon->nmon;
693 * we do not free monsters immediately, in order to have their name available
694 * shortly after their demise
696 struct monst *fdmon; /* chain of dead monsters, need not to be saved */
699 monfree(struct monst *mtmp)
710 while ((mtmp = fdmon) != NULL) {
717 unstuck(struct monst *mtmp)
719 if (u.ustuck == mtmp) {
732 killed(struct monst *mtmp)
739 struct permonst *mdat;
742 mtmp->data = PM_CHAMELEON;
745 pline("You destroy it!");
747 pline("You destroy %s!",
748 mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
751 pline("Your hands stop glowing blue.");
755 /* count killed monsters */
757 nk = 1; /* in case we cannot find it in mons */
758 tmp = mdat - mons; /* index in mons array (if not 'd', '@', ...) */
759 if (tmp >= 0 && tmp < CMNUM + 2) {
761 if ((nk = u.nr_killed[tmp]) > MAXMONNO &&
762 !strchr(fut_geno, mdat->mlet))
763 charcat(fut_geno, mdat->mlet);
766 /* punish bad behaviour */
767 if (mdat->mlet == '@')
768 Telepat = 0, u.uluck -= 2;
769 if (mtmp->mpeaceful || mtmp->mtame)
771 if (mdat->mlet == 'u')
773 if ((int)u.uluck < LUCKMIN)
776 /* give experience points */
777 tmp = 1 + mdat->mlevel * mdat->mlevel;
779 tmp += 2 * (7 - mdat->ac);
780 if (strchr("AcsSDXaeRTVWU&In:P", mdat->mlet))
781 tmp += 2 * mdat->mlevel;
782 if (strchr("DeV&P", mdat->mlet))
783 tmp += (7 * mdat->mlevel);
784 if (mdat->mlevel > 6)
786 if (mdat->mlet == ';')
790 /* ------- recent addition: make nr of points decrease
791 * when this is not the first of this kind */
794 int ml = mdat->mlevel;
796 if (ul < 14) /* points are given based on present and future level */
797 for (tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
798 if (u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4 << (tmp2 - 1))) / nk
799 >= 10 * pow((unsigned)(ul - 1)))
804 tmp = (tmp + ((tmp2 < 0) ? 0 : 4 << tmp2)) / nk;
808 /* note: ul is not necessarily the future value of u.ulevel */
809 /* ------- end of recent valuation change ------- */
810 #endif /* NEW_SCORING */
812 more_experienced(tmp, 0);
814 while (u.ulevel < 14 && u.uexp >= newuexp()) {
815 pline("Welcome to experience level %u.", ++u.ulevel);
824 /* dispose of monster and make cadaver */
829 if (tmp == 'm') { /* he killed a minotaur, give him a wand of digging */
830 /* note: the dead minotaur will be on top of it! */
831 mksobj_at(WAN_DIGGING, x, y);
832 /* if (cansee(x, y)) atl(x, y, fobj->olet); */
837 mksobj_at(WORM_TOOTH, x, y);
841 if (!letter(tmp) || (!strchr("mw", tmp) && !rn2(3)))
844 if (ACCESSIBLE(levl[x][y].typ)) /* might be mimic in wall or dead eel*/
845 if (x != u.ux || y != u.uy) /* might be here after swallowed */
846 if (strchr("NTVm&", mdat->mlet) || rn2(5)) {
847 struct obj *obj2 = mkobj_at(tmp, x, y);
849 atl(x, y, obj2->olet);
855 kludge(const char *str, const char *arg)
867 rescham(void) /* force all chameleons to become normal */
871 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
874 newcham(mtmp, PM_CHAMELEON);
878 /* make a chameleon look like a new monster */
879 /* returns 1 if the monster actually changed */
881 newcham(struct monst *mtmp, struct permonst *mdat)
885 if (mdat == mtmp->data) /* still the same monster */
888 if (mtmp->wormno) /* throw tail away */
891 if (u.ustuck == mtmp) {
902 hpd = (mtmp->data->mlevel) * 8;
906 mhp = (mdat->mlevel) * 8;
907 /* new hp: same fraction of max as before */
908 mtmp->mhp = 2 + (hpn * mhp) / hpd;
910 mtmp->mhpmax = 2 + (hpn * mhp) / hpd;
911 mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
913 if (mdat->mlet == 'w' && getwn(mtmp))
915 /* perhaps we should clear mtmp->mtame here? */
917 unpmon(mtmp); /* necessary for 'I' and to force pmon */
922 /* Make monster mtmp next to you (if possible) */
924 mnexto(struct monst *mtmp)
927 mm = enexto(u.ux, u.uy);
934 ishuman(struct monst *mtmp)
936 return (mtmp->data->mlet == '@');
940 setmangry(struct monst *mtmp)
942 if (!mtmp->mpeaceful)
948 pline("%s gets angry!", Monnam(mtmp));
952 * not one hundred percent correct: now a snake may hide under an invisible
956 canseemon(struct monst *mtmp)
958 return ((!mtmp->minvis || See_invisible)
959 && (!mtmp->mhide || !o_at(mtmp->mx, mtmp->my))
960 && cansee(mtmp->mx, mtmp->my));