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 */
32 /* note that mtmp or mtmp->nmon might get killed
33 while mtmp moves, so we cannot just walk down the
34 chain (even new monsters might get created!) */
35 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
36 if(mtmp->mlstmv < moves) goto next_mon;
37 /* treated all monsters */
43 /* most monsters drown in pools */
44 { boolean inpool, iseel;
46 inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
47 iseel = (mtmp->data->mlet == ';');
48 if(inpool && !iseel) {
49 if(cansee(mtmp->mx,mtmp->my))
50 pline("%s drowns.", Monnam(mtmp));
54 /* but eels have a difficult time outside */
55 if(iseel && !inpool) {
56 if(mtmp->mhp > 1) mtmp->mhp--;
61 if(mtmp->mblinded && !--mtmp->mblinded)
63 if(mtmp->mfleetim && !--mtmp->mfleetim)
65 if(mtmp->mimic) continue;
66 if(mtmp->mspeed != MSLOW || !(moves%2)){
67 /* continue if the monster died fighting */
69 if(Conflict && cansee(mtmp->mx,mtmp->my)
70 && (fr = fightm(mtmp)) == 2)
72 if(fr<0 && dochugw(mtmp))
75 if(mtmp->mspeed == MFAST && dochugw(mtmp))
79 warnlevel -= u.ulevel;
80 if(warnlevel >= SIZE(warnings))
81 warnlevel = SIZE(warnings)-1;
83 if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
85 switch(Warning & (LEFT_RING | RIGHT_RING)){
87 rr = "Your left ring glows";
90 rr = "Your right ring glows";
92 case LEFT_RING | RIGHT_RING:
93 rr = "Both your rings glow";
96 rr = "Your fingertips glow";
99 pline("%s %s!", rr, warnings[warnlevel]);
100 lastwarntime = moves;
101 lastwarnlev = warnlevel;
104 dmonsfree(); /* remove all dead monsters */
108 justswld(struct monst *mtmp, const char *name)
115 kludge("%s swallows you!",name);
124 youswld(struct monst *mtmp, int dam, int die, const char *name)
126 if(mtmp != u.ustuck) return;
127 kludge("%s digests you!",name);
129 if((int)u.uswldtim++ >= die){ /* a3 */
130 pline("It totally digests you!");
133 if(u.uhp < 1) done_in_by(mtmp);
134 /* flags.botlx = 1; */ /* should we show status line ? */
138 dochugw(struct monst *mtmp)
142 int d1 = dochug(mtmp);
144 if(!d1) /* monster still alive */
147 if(mtmp->data->mlevel > warnlevel)
148 if((dd = dist(mtmp->mx,mtmp->my)) < dist(x,y))
151 warnlevel = mtmp->data->mlevel;
155 /* returns 1 if monster died moving, 0 otherwise */
157 dochug(struct monst *mtmp)
159 struct permonst *mdat;
160 int tmp = 0, nearby, scared;
162 if(mtmp->cham && !rn2(6))
163 newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
166 panic("bad monster %c (%d)",mdat->mlet,mdat->mlevel);
168 /* regenerate monsters */
169 if((!(moves%20) || index(MREGEN, mdat->mlet)) &&
170 mtmp->mhp < mtmp->mhpmax)
173 if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
176 /* wake up, or get out of here. */
177 /* ettins are hard to surprise */
178 /* Nymphs and Leprechauns do not easily wake up */
179 if(cansee(mtmp->mx,mtmp->my) &&
180 (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
181 (!index("NL",mdat->mlet) || !rn2(50)) &&
182 (Aggravate_monster || index("d1", mdat->mlet)
183 || (!rn2(7) && !mtmp->mimic)))
188 /* not frozen or sleeping: wipe out texts written in the dust */
189 wipe_engr_at(mtmp->mx, mtmp->my, 1);
191 /* confused monsters get unconfused with small probability */
192 if(mtmp->mconf && !rn2(50)) mtmp->mconf = 0;
194 /* some monsters teleport */
195 if(mtmp->mflee && index("tNL", mdat->mlet) && !rn2(40)){
199 if(mdat->mmove < rnd(6)) return(0);
201 /* fleeing monsters might regain courage */
202 if(mtmp->mflee && !mtmp->mfleetim
203 && mtmp->mhp == mtmp->mhpmax && !rn2(25))
206 nearby = (dist(mtmp->mx, mtmp->my) < 3);
207 scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
208 sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
209 if(scared && !mtmp->mflee) {
211 mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
217 (mtmp->minvis && !rn2(3)) ||
218 (index("BIuy", mdat->mlet) && !rn2(4)) ||
219 (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
220 (!mtmp->mcansee && !rn2(4)) ||
223 tmp = m_move(mtmp,0); /* 2: monster died moving */
224 if(tmp == 2 || (tmp && mdat->mmove <= 12))
228 if(!index("Ea", mdat->mlet) && nearby &&
229 !mtmp->mpeaceful && u.uhp > 0 && !scared) {
231 return(1); /* monster died (e.g. 'y' or 'F') */
233 /* extra movement for fast monsters */
234 if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
239 m_move(struct monst *mtmp, int after)
242 int nx,ny,omx,omy,appr,nearer,cnt,i,j;
243 xchar gx,gy,nix,niy,chcnt;
245 boolean likegold = 0, likegems = 0, likeobjs;
246 char msym = mtmp->data->mlet;
247 schar mmoved = 0; /* not strictly nec.: chi >= 0 will do */
251 if(mtmp->mfroz || mtmp->msleep)
255 if(i == 2) return(2); /* he died */
256 if(i == 1) return(0); /* still in trap, so didnt move */
258 if(mtmp->mhide && o_at(mtmp->mx,mtmp->my) && rn2(10))
259 return(0); /* do not leave hiding place */
266 /* my dog gets a special treatment */
268 return( dog_move(mtmp, after) );
271 /* likewise for shopkeeper */
273 mmoved = shk_move(mtmp);
276 mmoved = 0; /* follow player outside shop */
279 /* and for the guard */
285 /* teleport if that lies in our nature ('t') or when badly wounded ('1') */
286 if((msym == 't' && !rn2(5))
287 || (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
288 || levl[u.ux][u.uy].typ == STAIRS))) {
289 if(mtmp->mhp < 7 || (msym == 't' && rn2(2)))
297 /* spit fire ('D') or use a wand ('1') when appropriate */
298 if(index("D1", msym))
301 if(msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
302 mtmp->mcansee && rn2(5)) {
304 pline("%s's gaze has confused you!", Monnam(mtmp));
306 pline("You are getting more and more confused.");
307 if(rn2(3)) mtmp->mcan = 1;
308 Confusion += d(3,4); /* timeout */
311 if(!mtmp->mflee && u.uswallow && u.ustuck != mtmp) return(1);
313 if(mtmp->mflee) appr = -1;
314 if(mtmp->mconf || Invis || !mtmp->mcansee ||
315 (index("BIy", msym) && !rn2(3)))
321 if(msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
324 /* random criterion for 'smell' or track finding ability
325 should use mtmp->msmell or sth
328 ('a' <= msym && msym <= 'z')) {
331 mroom = inroom(omx,omy);
332 if(mroom < 0 || mroom != inroom(u.ux,u.uy)){
333 cp = gettrack(omx,omy);
341 /* look for gold or jewels nearby */
342 likegold = (index("LOD", msym) != NULL);
343 likegems = (index("ODu", msym) != NULL);
344 likeobjs = mtmp->mhide;
345 #define SRCHRADIUS 25
346 { xchar mind = SRCHRADIUS; /* not too far away */
350 for(gold = fgold; gold; gold = gold->ngold)
351 if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
357 if(likegems || likeobjs){
359 for(otmp = fobj; otmp; otmp = otmp->nobj)
360 if(likeobjs || otmp->olet == GEM_SYM)
362 objects[otmp->otyp].g_val != 0)
363 if((dd = DIST(omx,omy,otmp->ox,otmp->oy)) < mind){
369 if(mind < SRCHRADIUS && appr == -1) {
370 if(dist(omx,omy) < 10) {
379 cnt = mfndpos(mtmp,poss,info,
380 msym == 'u' ? NOTONL :
381 (msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
382 index(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
383 /* ALLOW_ROCK for some monsters ? */
386 for(i=0; i<cnt; i++) {
389 for(j=0; j<MTSZ && j<cnt-1; j++)
390 if(nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
391 if(rn2(4*(cnt-j))) goto nxti;
393 /* some stupid compilers think that this is too complicated */
394 { int d1 = DIST(nx,ny,gx,gy);
395 int d2 = DIST(nix,niy,gx,gy);
399 nearer = (DIST(nx,ny,gx,gy) < DIST(nix,niy,gx,gy));
401 if((appr == 1 && nearer) || (appr == -1 && !nearer) ||
403 (!appr && !rn2(++chcnt))){
412 if(info[chi] & ALLOW_M){
413 mtmp2 = m_at(nix,niy);
414 if(hitmm(mtmp,mtmp2) == 1 && rn2(4) &&
415 hitmm(mtmp2,mtmp) == 2) return(2);
418 if(info[chi] & ALLOW_U){
419 hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd)+1);
424 for(j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
425 mtmp->mtrack[0].x = omx;
426 mtmp->mtrack[0].y = omy;
428 if(mtmp->wormno) worm_move(mtmp);
431 if(msym == 'u' && rn2(2)){
436 if(mtmp->wormno) worm_nomove(mtmp);
441 if(mintrap(mtmp) == 2) /* he died */
443 if(likegold) mpickgold(mtmp);
444 if(likegems) mpickgems(mtmp);
445 if(mtmp->mhide) mtmp->mundetected = 1;
452 mpickgold(struct monst *mtmp)
455 while((gold = g_at(mtmp->mx, mtmp->my))){
456 mtmp->mgold += gold->amount;
458 if(levl[mtmp->mx][mtmp->my].scrsym == '$')
459 newsym(mtmp->mx, mtmp->my);
464 mpickgems(struct monst *mtmp)
467 for(otmp = fobj; otmp; otmp = otmp->nobj)
468 if(otmp->olet == GEM_SYM)
469 if(otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
470 if(mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0){
472 mpickobj(mtmp, otmp);
473 if(levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
474 newsym(mtmp->mx, mtmp->my); /* %% */
475 return; /* pick only one object */
479 /* return number of acceptable neighbour positions */
481 mfndpos(struct monst *mon, coord poss[9], int info[9], int flag)
483 int x,y,nx,ny,cnt = 0,ntyp;
490 nowtyp = levl[x][y].typ;
492 pool = (mon->data->mlet == ';');
493 nexttry: /* eels prefer the water, but if there is no water nearby,
494 they will crawl over land */
499 for(nx = x-1; nx <= x+1; nx++) for(ny = y-1; ny <= y+1; ny++)
500 if(nx != x || ny != y) if(isok(nx,ny))
501 if(!IS_ROCK(ntyp = levl[nx][ny].typ))
502 if(!(nx != x && ny != y && (nowtyp == DOOR || ntyp == DOOR)))
503 if((ntyp == POOL) == pool) {
505 if(nx == u.ux && ny == u.uy){
506 if(!(flag & ALLOW_U)) continue;
508 } else if((mtmp = m_at(nx,ny))){
509 if(!(flag & ALLOW_M)) continue;
512 if(!(flag & ALLOW_TM)) continue;
513 info[cnt] |= ALLOW_TM;
516 if(sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
517 if(flag & NOGARLIC) continue;
518 info[cnt] |= NOGARLIC;
520 if(sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
521 (!mon->mpeaceful && sengr_at("Elbereth", nx, ny))) {
522 if(!(flag & ALLOW_SSM)) continue;
523 info[cnt] |= ALLOW_SSM;
525 if(sobj_at(ENORMOUS_ROCK, nx, ny)) {
526 if(!(flag & ALLOW_ROCK)) continue;
527 info[cnt] |= ALLOW_ROCK;
529 if(!Invis && online(nx,ny)){
530 if(flag & NOTONL) continue;
533 /* we cannot avoid traps of an unknown kind */
534 { struct trap *ttmp = t_at(nx, ny);
537 tt = 1 << ttmp->ttyp;
538 if(mon->mtrapseen & tt){
539 if(!(flag & tt)) continue;
548 if(!cnt && pool && nowtyp != POOL) {
558 return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
562 poisoned(const char *string, const char *pname)
566 if(Blind) pline("It was poisoned.");
567 else pline("The %s was poisoned!",string);
568 if(Poison_resistance) {
569 pline("The poison doesn't seem to affect you.");
575 pline("I am afraid the poison was deadly ...");
579 losehp(rn1(10,6), pname);
588 mondead(struct monst *mtmp)
594 if(mtmp->isshk) shkdead(mtmp);
595 if(mtmp->isgd) gddead();
597 if(mtmp->wormno) wormdead(mtmp);
602 /* called when monster is moved to larger structure */
604 replmon(struct monst *mtmp, struct monst *mtmp2)
610 if(u.ustuck == mtmp) u.ustuck = mtmp2;
611 if(mtmp2->isshk) replshk(mtmp,mtmp2);
612 if(mtmp2->isgd) replgd(mtmp,mtmp2);
616 relmon(struct monst *mon)
620 if(mon == fmon) fmon = fmon->nmon;
622 for(mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon) ;
623 mtmp->nmon = mon->nmon;
627 /* we do not free monsters immediately, in order to have their name
628 available shortly after their demise */
629 struct monst *fdmon; /* chain of dead monsters, need not to be saved */
632 monfree(struct monst *mtmp)
642 while((mtmp = fdmon)){
649 unstuck(struct monst *mtmp)
651 if(u.ustuck == mtmp) {
664 killed(struct monst *mtmp)
671 struct permonst *mdat;
673 if(mtmp->cham) mtmp->data = PM_CHAMELEON;
675 if(Blind) pline("You destroy it!");
677 pline("You destroy %s!",
678 mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
681 if(!Blind) pline("Your hands stop glowing blue.");
685 /* count killed monsters */
687 nk = 1; /* in case we cannot find it in mons */
688 tmp = mdat - mons; /* index in mons array (if not 'd', '@', ...) */
689 if(tmp >= 0 && tmp < CMNUM+2) {
691 if((nk = u.nr_killed[tmp]) > MAXMONNO &&
692 !index(fut_geno, mdat->mlet))
693 charcat(fut_geno, mdat->mlet);
696 /* punish bad behaviour */
697 if(mdat->mlet == '@') Telepat = 0, u.uluck -= 2;
698 if(mtmp->mpeaceful || mtmp->mtame) u.uluck--;
699 if(mdat->mlet == 'u') u.uluck -= 5;
700 if((int)u.uluck < LUCKMIN) u.uluck = LUCKMIN;
702 /* give experience points */
703 tmp = 1 + mdat->mlevel * mdat->mlevel;
704 if(mdat->ac < 3) tmp += 2*(7 - mdat->ac);
705 if(index("AcsSDXaeRTVWU&In:P", mdat->mlet))
706 tmp += 2*mdat->mlevel;
707 if(index("DeV&P",mdat->mlet)) tmp += (7*mdat->mlevel);
708 if(mdat->mlevel > 6) tmp += 50;
709 if(mdat->mlet == ';') tmp += 1000;
712 /* ------- recent addition: make nr of points decrease
713 when this is not the first of this kind */
715 int ml = mdat->mlevel;
717 if(ul < 14) /* points are given based on present and future level */
718 for(tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
719 if(u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4<<(tmp2-1)))/nk
720 >= 10*pow((unsigned)(ul-1)))
721 if(++ul == 14) break;
724 tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
727 /* note: ul is not necessarily the future value of u.ulevel */
728 /* ------- end of recent valuation change ------- */
729 #endif /* NEW_SCORING */
731 more_experienced(tmp,0);
733 while(u.ulevel < 14 && u.uexp >= newuexp()){
734 pline("Welcome to experience level %u.", ++u.ulevel);
736 if(tmp < 3) tmp = rnd(10);
742 /* dispose of monster and make cadaver */
743 x = mtmp->mx; y = mtmp->my;
746 if(tmp == 'm') { /* he killed a minotaur, give him a wand of digging */
747 /* note: the dead minotaur will be on top of it! */
748 mksobj_at(WAN_DIGGING, x, y);
749 /* if(cansee(x,y)) atl(x,y,fobj->olet); */
754 mksobj_at(WORM_TOOTH, x, y);
758 if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
760 if(ACCESSIBLE(levl[x][y].typ)) /* might be mimic in wall or dead eel*/
761 if(x != u.ux || y != u.uy) /* might be here after swallowed */
762 if(index("NTVm&",mdat->mlet) || rn2(5)) {
763 struct obj *obj2 = mkobj_at(tmp,x,y);
771 kludge(const char *str, const char *arg)
774 if(*str == '%') pline(str,"It");
775 else pline(str,"it");
776 } else pline(str,arg);
780 rescham(void) /* force all chameleons to become normal */
784 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
787 newcham(mtmp, PM_CHAMELEON);
791 /* make a chameleon look like a new monster */
792 /* returns 1 if the monster actually changed */
794 newcham(struct monst *mtmp, struct permonst *mdat)
798 if(mdat == mtmp->data) return(0); /* still the same monster */
800 if(mtmp->wormno) wormdead(mtmp); /* throw tail away */
802 if (u.ustuck == mtmp) {
813 hpd = (mtmp->data->mlevel)*8;
816 mhp = (mdat->mlevel)*8;
817 /* new hp: same fraction of max as before */
818 mtmp->mhp = 2 + (hpn*mhp)/hpd;
820 mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
821 mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
823 if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
824 /* perhaps we should clear mtmp->mtame here? */
826 unpmon(mtmp); /* necessary for 'I' and to force pmon */
831 /* Make monster mtmp next to you (if possible) */
833 mnexto(struct monst *mtmp)
836 mm = enexto(u.ux, u.uy);
843 ishuman(struct monst *mtmp)
845 return(mtmp->data->mlet == '@');
849 setmangry(struct monst *mtmp)
851 if(!mtmp->mpeaceful) return;
852 if(mtmp->mtame) return;
854 if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
857 /* not one hundred procent correct: now a snake may hide under an
860 canseemon(struct monst *mtmp)
862 return((!mtmp->minvis || See_invisible)
863 && (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
864 && cansee(mtmp->mx, mtmp->my));