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 $ */
6 #include "hack.mfndpos.h"
9 #define NULL (char *) 0
12 extern struct monst *makemon();
13 extern struct obj *mkobj_at();
15 int warnlevel; /* used by movemon and dochugw */
19 "white", "pink", "red", "ruby", "purple", "black"
30 /* find a monster that we haven't treated yet */
31 /* note that mtmp or mtmp->nmon might get killed
32 while mtmp moves, so we cannot just walk down the
33 chain (even new monsters might get created!) */
34 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
35 if(mtmp->mlstmv < moves) goto next_mon;
36 /* treated all monsters */
42 /* most monsters drown in pools */
43 { boolean inpool, iseel;
45 inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
46 iseel = (mtmp->data->mlet == ';');
47 if(inpool && !iseel) {
48 if(cansee(mtmp->mx,mtmp->my))
49 pline("%s drowns.", Monnam(mtmp));
53 /* but eels have a difficult time outside */
54 if(iseel && !inpool) {
55 if(mtmp->mhp > 1) mtmp->mhp--;
60 if(mtmp->mblinded && !--mtmp->mblinded)
62 if(mtmp->mfleetim && !--mtmp->mfleetim)
64 if(mtmp->mimic) continue;
65 if(mtmp->mspeed != MSLOW || !(moves%2)){
66 /* continue if the monster died fighting */
68 if(Conflict && cansee(mtmp->mx,mtmp->my)
69 && (fr = fightm(mtmp)) == 2)
71 if(fr<0 && dochugw(mtmp))
74 if(mtmp->mspeed == MFAST && dochugw(mtmp))
78 warnlevel -= u.ulevel;
79 if(warnlevel >= SIZE(warnings))
80 warnlevel = SIZE(warnings)-1;
82 if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
84 switch(Warning & (LEFT_RING | RIGHT_RING)){
86 rr = "Your left ring glows";
89 rr = "Your right ring glows";
91 case LEFT_RING | RIGHT_RING:
92 rr = "Both your rings glow";
95 rr = "Your fingertips glow";
98 pline("%s %s!", rr, warnings[warnlevel]);
100 lastwarnlev = warnlevel;
103 dmonsfree(); /* remove all dead monsters */
115 kludge("%s swallows you!",name);
123 youswld(mtmp,dam,die,name)
128 if(mtmp != u.ustuck) return;
129 kludge("%s digests you!",name);
131 if(u.uswldtim++ >= die){ /* a3 */
132 pline("It totally digests you!");
135 if(u.uhp < 1) done_in_by(mtmp);
136 /* flags.botlx = 1; */ /* should we show status line ? */
139 dochugw(mtmp) struct monst *mtmp; {
142 int d = dochug(mtmp);
144 if(!d) /* 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 */
159 struct permonst *mdat;
160 int tmp, nearby, scared;
162 if(mtmp->cham && !rn2(6))
163 (void) 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);
242 int nx,ny,omx,omy,appr,nearer,cnt,i,j;
243 xchar gx,gy,nix,niy,chcnt;
245 boolean likegold, likegems, 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')) {
329 extern coord *gettrack();
332 mroom = inroom(omx,omy);
333 if(mroom < 0 || mroom != inroom(u.ux,u.uy)){
334 cp = gettrack(omx,omy);
342 /* look for gold or jewels nearby */
343 likegold = (index("LOD", msym) != NULL);
344 likegems = (index("ODu", msym) != NULL);
345 likeobjs = mtmp->mhide;
346 #define SRCHRADIUS 25
347 { xchar mind = SRCHRADIUS; /* not too far away */
351 for(gold = fgold; gold; gold = gold->ngold)
352 if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
358 if(likegems || likeobjs){
360 for(otmp = fobj; otmp; otmp = otmp->nobj)
361 if(likeobjs || otmp->olet == GEM_SYM)
363 objects[otmp->otyp].g_val != 0)
364 if((dd = DIST(omx,omy,otmp->ox,otmp->oy)) < mind){
370 if(mind < SRCHRADIUS && appr == -1) {
371 if(dist(omx,omy) < 10) {
380 cnt = mfndpos(mtmp,poss,info,
381 msym == 'u' ? NOTONL :
382 (msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
383 index(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
384 /* ALLOW_ROCK for some monsters ? */
387 for(i=0; i<cnt; i++) {
390 for(j=0; j<MTSZ && j<cnt-1; j++)
391 if(nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
392 if(rn2(4*(cnt-j))) goto nxti;
394 /* some stupid compilers think that this is too complicated */
395 { int d1 = DIST(nx,ny,gx,gy);
396 int d2 = DIST(nix,niy,gx,gy);
400 nearer = (DIST(nx,ny,gx,gy) < DIST(nix,niy,gx,gy));
402 if((appr == 1 && nearer) || (appr == -1 && !nearer) ||
404 (!appr && !rn2(++chcnt))){
413 if(info[chi] & ALLOW_M){
414 mtmp2 = m_at(nix,niy);
415 if(hitmm(mtmp,mtmp2) == 1 && rn2(4) &&
416 hitmm(mtmp2,mtmp) == 2) return(2);
419 if(info[chi] & ALLOW_U){
420 (void) hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd)+1);
425 for(j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
426 mtmp->mtrack[0].x = omx;
427 mtmp->mtrack[0].y = omy;
429 if(mtmp->wormno) worm_move(mtmp);
432 if(msym == 'u' && rn2(2)){
437 if(mtmp->wormno) worm_nomove(mtmp);
442 if(mintrap(mtmp) == 2) /* he died */
444 if(likegold) mpickgold(mtmp);
445 if(likegems) mpickgems(mtmp);
446 if(mtmp->mhide) mtmp->mundetected = 1;
452 mpickgold(mtmp) struct monst *mtmp; {
454 while(gold = g_at(mtmp->mx, mtmp->my)){
455 mtmp->mgold += gold->amount;
457 if(levl[mtmp->mx][mtmp->my].scrsym == '$')
458 newsym(mtmp->mx, mtmp->my);
462 mpickgems(mtmp) struct monst *mtmp; {
464 for(otmp = fobj; otmp; otmp = otmp->nobj)
465 if(otmp->olet == GEM_SYM)
466 if(otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
467 if(mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0){
469 mpickobj(mtmp, otmp);
470 if(levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
471 newsym(mtmp->mx, mtmp->my); /* %% */
472 return; /* pick only one object */
476 /* return number of acceptable neighbour positions */
477 mfndpos(mon,poss,info,flag)
482 int x,y,nx,ny,cnt = 0,ntyp;
489 nowtyp = levl[x][y].typ;
491 pool = (mon->data->mlet == ';');
492 nexttry: /* eels prefer the water, but if there is no water nearby,
493 they will crawl over land */
498 for(nx = x-1; nx <= x+1; nx++) for(ny = y-1; ny <= y+1; ny++)
499 if(nx != x || ny != y) if(isok(nx,ny))
500 if(!IS_ROCK(ntyp = levl[nx][ny].typ))
501 if(!(nx != x && ny != y && (nowtyp == DOOR || ntyp == DOOR)))
502 if((ntyp == POOL) == pool) {
504 if(nx == u.ux && ny == u.uy){
505 if(!(flag & ALLOW_U)) continue;
507 } else if(mtmp = m_at(nx,ny)){
508 if(!(flag & ALLOW_M)) continue;
511 if(!(flag & ALLOW_TM)) continue;
512 info[cnt] |= ALLOW_TM;
515 if(sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
516 if(flag & NOGARLIC) continue;
517 info[cnt] |= NOGARLIC;
519 if(sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
520 (!mon->mpeaceful && sengr_at("Elbereth", nx, ny))) {
521 if(!(flag & ALLOW_SSM)) continue;
522 info[cnt] |= ALLOW_SSM;
524 if(sobj_at(ENORMOUS_ROCK, nx, ny)) {
525 if(!(flag & ALLOW_ROCK)) continue;
526 info[cnt] |= ALLOW_ROCK;
528 if(!Invis && online(nx,ny)){
529 if(flag & NOTONL) continue;
532 /* we cannot avoid traps of an unknown kind */
533 { struct trap *ttmp = t_at(nx, ny);
536 tt = 1 << ttmp->ttyp;
537 if(mon->mtrapseen & tt){
538 if(!(flag & tt)) continue;
547 if(!cnt && pool && nowtyp != POOL) {
555 return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
558 poisoned(string, pname)
559 char *string, *pname;
563 if(Blind) pline("It was poisoned.");
564 else pline("The %s was poisoned!",string);
565 if(Poison_resistance) {
566 pline("The poison doesn't seem to affect you.");
572 pline("I am afraid the poison was deadly ...");
576 losehp(rn1(10,6), pname);
591 if(mtmp->isshk) shkdead(mtmp);
592 if(mtmp->isgd) gddead();
594 if(mtmp->wormno) wormdead(mtmp);
599 /* called when monster is moved to larger structure */
601 struct monst *mtmp, *mtmp2;
607 if(u.ustuck == mtmp) u.ustuck = mtmp2;
608 if(mtmp2->isshk) replshk(mtmp,mtmp2);
609 if(mtmp2->isgd) replgd(mtmp,mtmp2);
617 if(mon == fmon) fmon = fmon->nmon;
619 for(mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon) ;
620 mtmp->nmon = mon->nmon;
624 /* we do not free monsters immediately, in order to have their name
625 available shortly after their demise */
626 struct monst *fdmon; /* chain of dead monsters, need not to be saved */
628 monfree(mtmp) struct monst *mtmp; {
644 if(u.ustuck == mtmp) {
663 struct permonst *mdat;
664 extern long newuexp();
666 if(mtmp->cham) mtmp->data = PM_CHAMELEON;
668 if(Blind) pline("You destroy it!");
670 pline("You destroy %s!",
671 mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
674 if(!Blind) pline("Your hands stop glowing blue.");
678 /* count killed monsters */
680 nk = 1; /* in case we cannot find it in mons */
681 tmp = mdat - mons; /* index in mons array (if not 'd', '@', ...) */
682 if(tmp >= 0 && tmp < CMNUM+2) {
683 extern char fut_geno[];
685 if((nk = u.nr_killed[tmp]) > MAXMONNO &&
686 !index(fut_geno, mdat->mlet))
687 charcat(fut_geno, mdat->mlet);
690 /* punish bad behaviour */
691 if(mdat->mlet == '@') Telepat = 0, u.uluck -= 2;
692 if(mtmp->mpeaceful || mtmp->mtame) u.uluck--;
693 if(mdat->mlet == 'u') u.uluck -= 5;
694 if((int)u.uluck < LUCKMIN) u.uluck = LUCKMIN;
696 /* give experience points */
697 tmp = 1 + mdat->mlevel * mdat->mlevel;
698 if(mdat->ac < 3) tmp += 2*(7 - mdat->ac);
699 if(index("AcsSDXaeRTVWU&In:P", mdat->mlet))
700 tmp += 2*mdat->mlevel;
701 if(index("DeV&P",mdat->mlet)) tmp += (7*mdat->mlevel);
702 if(mdat->mlevel > 6) tmp += 50;
703 if(mdat->mlet == ';') tmp += 1000;
706 /* ------- recent addition: make nr of points decrease
707 when this is not the first of this kind */
709 int ml = mdat->mlevel;
711 if(ul < 14) /* points are given based on present and future level */
712 for(tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
713 if(u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4<<(tmp2-1)))/nk
714 >= 10*pow((unsigned)(ul-1)))
715 if(++ul == 14) break;
718 tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
721 /* note: ul is not necessarily the future value of u.ulevel */
722 /* ------- end of recent valuation change ------- */
725 more_experienced(tmp,0);
727 while(u.ulevel < 14 && u.uexp >= newuexp()){
728 pline("Welcome to experience level %u.", ++u.ulevel);
730 if(tmp < 3) tmp = rnd(10);
736 /* dispose of monster and make cadaver */
737 x = mtmp->mx; y = mtmp->my;
740 if(tmp == 'm') { /* he killed a minotaur, give him a wand of digging */
741 /* note: the dead minotaur will be on top of it! */
742 mksobj_at(WAN_DIGGING, x, y);
743 /* if(cansee(x,y)) atl(x,y,fobj->olet); */
748 mksobj_at(WORM_TOOTH, x, y);
752 if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
754 if(ACCESSIBLE(levl[x][y].typ)) /* might be mimic in wall or dead eel*/
755 if(x != u.ux || y != u.uy) /* might be here after swallowed */
756 if(index("NTVm&",mdat->mlet) || rn2(5)) {
757 struct obj *obj2 = mkobj_at(tmp,x,y);
768 if(*str == '%') pline(str,"It");
769 else pline(str,"it");
770 } else pline(str,arg);
773 rescham() /* force all chameleons to become normal */
777 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
780 (void) newcham(mtmp, PM_CHAMELEON);
784 newcham(mtmp,mdat) /* make a chameleon look like a new monster */
785 /* returns 1 if the monster actually changed */
787 struct permonst *mdat;
791 if(mdat == mtmp->data) return(0); /* still the same monster */
793 if(mtmp->wormno) wormdead(mtmp); /* throw tail away */
795 if (u.ustuck == mtmp) {
806 hpd = (mtmp->data->mlevel)*8;
809 mhp = (mdat->mlevel)*8;
810 /* new hp: same fraction of max as before */
811 mtmp->mhp = 2 + (hpn*mhp)/hpd;
813 mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
814 mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
816 if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
817 /* perhaps we should clear mtmp->mtame here? */
819 unpmon(mtmp); /* necessary for 'I' and to force pmon */
824 mnexto(mtmp) /* Make monster mtmp next to you (if possible) */
827 extern coord enexto();
829 mm = enexto(u.ux, u.uy);
835 ishuman(mtmp) struct monst *mtmp; {
836 return(mtmp->data->mlet == '@');
839 setmangry(mtmp) struct monst *mtmp; {
840 if(!mtmp->mpeaceful) return;
841 if(mtmp->mtame) return;
843 if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
846 /* not one hundred procent correct: now a snake may hide under an
851 return((!mtmp->minvis || See_invisible)
852 && (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
853 && cansee(mtmp->mx, mtmp->my));