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.4 2005/05/22 03:37:05 y0netan1 Exp $ */
7 #include "hack.mfndpos.h"
10 #define NULL (char *) 0
13 extern struct monst *makemon();
14 extern struct obj *mkobj_at();
16 int warnlevel; /* used by movemon and dochugw */
19 static const char *warnings[] = {
20 "white", "pink", "red", "ruby", "purple", "black"
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 */
116 kludge("%s swallows you!",name);
124 youswld(mtmp,dam,die,name)
129 if(mtmp != u.ustuck) return;
130 kludge("%s digests you!",name);
132 if(u.uswldtim++ >= die){ /* a3 */
133 pline("It totally digests you!");
136 if(u.uhp < 1) done_in_by(mtmp);
137 /* flags.botlx = 1; */ /* should we show status line ? */
140 dochugw(mtmp) struct monst *mtmp; {
143 int d = dochug(mtmp);
145 if(!d) /* monster still alive */
148 if(mtmp->data->mlevel > warnlevel)
149 if((dd = dist(mtmp->mx,mtmp->my)) < dist(x,y))
152 warnlevel = mtmp->data->mlevel;
156 /* returns 1 if monster died moving, 0 otherwise */
160 struct permonst *mdat;
161 int tmp, nearby, scared;
163 if(mtmp->cham && !rn2(6))
164 (void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
167 panic("bad monster %c (%d)",mdat->mlet,mdat->mlevel);
169 /* regenerate monsters */
170 if((!(moves%20) || index(MREGEN, mdat->mlet)) &&
171 mtmp->mhp < mtmp->mhpmax)
174 if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
177 /* wake up, or get out of here. */
178 /* ettins are hard to surprise */
179 /* Nymphs and Leprechauns do not easily wake up */
180 if(cansee(mtmp->mx,mtmp->my) &&
181 (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
182 (!index("NL",mdat->mlet) || !rn2(50)) &&
183 (Aggravate_monster || index("d1", mdat->mlet)
184 || (!rn2(7) && !mtmp->mimic)))
189 /* not frozen or sleeping: wipe out texts written in the dust */
190 wipe_engr_at(mtmp->mx, mtmp->my, 1);
192 /* confused monsters get unconfused with small probability */
193 if(mtmp->mconf && !rn2(50)) mtmp->mconf = 0;
195 /* some monsters teleport */
196 if(mtmp->mflee && index("tNL", mdat->mlet) && !rn2(40)){
200 if(mdat->mmove < rnd(6)) return(0);
202 /* fleeing monsters might regain courage */
203 if(mtmp->mflee && !mtmp->mfleetim
204 && mtmp->mhp == mtmp->mhpmax && !rn2(25))
207 nearby = (dist(mtmp->mx, mtmp->my) < 3);
208 scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
209 sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
210 if(scared && !mtmp->mflee) {
212 mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
218 (mtmp->minvis && !rn2(3)) ||
219 (index("BIuy", mdat->mlet) && !rn2(4)) ||
220 (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
221 (!mtmp->mcansee && !rn2(4)) ||
224 tmp = m_move(mtmp,0); /* 2: monster died moving */
225 if(tmp == 2 || (tmp && mdat->mmove <= 12))
229 if(!index("Ea", mdat->mlet) && nearby &&
230 !mtmp->mpeaceful && u.uhp > 0 && !scared) {
232 return(1); /* monster died (e.g. 'y' or 'F') */
234 /* extra movement for fast monsters */
235 if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
243 int nx,ny,omx,omy,appr,nearer,cnt,i,j;
244 xchar gx,gy,nix,niy,chcnt;
246 boolean likegold, likegems, likeobjs;
247 char msym = mtmp->data->mlet;
248 schar mmoved = 0; /* not strictly nec.: chi >= 0 will do */
252 if(mtmp->mfroz || mtmp->msleep)
256 if(i == 2) return(2); /* he died */
257 if(i == 1) return(0); /* still in trap, so didnt move */
259 if(mtmp->mhide && o_at(mtmp->mx,mtmp->my) && rn2(10))
260 return(0); /* do not leave hiding place */
267 /* my dog gets a special treatment */
269 return( dog_move(mtmp, after) );
272 /* likewise for shopkeeper */
274 mmoved = shk_move(mtmp);
277 mmoved = 0; /* follow player outside shop */
280 /* and for the guard */
286 /* teleport if that lies in our nature ('t') or when badly wounded ('1') */
287 if((msym == 't' && !rn2(5))
288 || (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
289 || levl[u.ux][u.uy].typ == STAIRS))) {
290 if(mtmp->mhp < 7 || (msym == 't' && rn2(2)))
298 /* spit fire ('D') or use a wand ('1') when appropriate */
299 if(index("D1", msym))
302 if(msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
303 mtmp->mcansee && rn2(5)) {
305 pline("%s's gaze has confused you!", Monnam(mtmp));
307 pline("You are getting more and more confused.");
308 if(rn2(3)) mtmp->mcan = 1;
309 Confusion += d(3,4); /* timeout */
312 if(!mtmp->mflee && u.uswallow && u.ustuck != mtmp) return(1);
314 if(mtmp->mflee) appr = -1;
315 if(mtmp->mconf || Invis || !mtmp->mcansee ||
316 (index("BIy", msym) && !rn2(3)))
322 if(msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
325 /* random criterion for 'smell' or track finding ability
326 should use mtmp->msmell or sth
329 ('a' <= msym && msym <= 'z')) {
330 extern coord *gettrack();
333 mroom = inroom(omx,omy);
334 if(mroom < 0 || mroom != inroom(u.ux,u.uy)){
335 cp = gettrack(omx,omy);
343 /* look for gold or jewels nearby */
344 likegold = (index("LOD", msym) != NULL);
345 likegems = (index("ODu", msym) != NULL);
346 likeobjs = mtmp->mhide;
347 #define SRCHRADIUS 25
348 { xchar mind = SRCHRADIUS; /* not too far away */
352 for(gold = fgold; gold; gold = gold->ngold)
353 if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
359 if(likegems || likeobjs){
361 for(otmp = fobj; otmp; otmp = otmp->nobj)
362 if(likeobjs || otmp->olet == GEM_SYM)
364 objects[otmp->otyp].g_val != 0)
365 if((dd = DIST(omx,omy,otmp->ox,otmp->oy)) < mind){
371 if(mind < SRCHRADIUS && appr == -1) {
372 if(dist(omx,omy) < 10) {
381 cnt = mfndpos(mtmp,poss,info,
382 msym == 'u' ? NOTONL :
383 (msym == '@' || msym == '1') ? (ALLOW_SSM | ALLOW_TRAPS) :
384 index(UNDEAD, msym) ? NOGARLIC : ALLOW_TRAPS);
385 /* ALLOW_ROCK for some monsters ? */
388 for(i=0; i<cnt; i++) {
391 for(j=0; j<MTSZ && j<cnt-1; j++)
392 if(nx == mtmp->mtrack[j].x && ny == mtmp->mtrack[j].y)
393 if(rn2(4*(cnt-j))) goto nxti;
395 /* some stupid compilers think that this is too complicated */
396 { int d1 = DIST(nx,ny,gx,gy);
397 int d2 = DIST(nix,niy,gx,gy);
401 nearer = (DIST(nx,ny,gx,gy) < DIST(nix,niy,gx,gy));
403 if((appr == 1 && nearer) || (appr == -1 && !nearer) ||
405 (!appr && !rn2(++chcnt))){
414 if(info[chi] & ALLOW_M){
415 mtmp2 = m_at(nix,niy);
416 if(hitmm(mtmp,mtmp2) == 1 && rn2(4) &&
417 hitmm(mtmp2,mtmp) == 2) return(2);
420 if(info[chi] & ALLOW_U){
421 (void) hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd)+1);
426 for(j=MTSZ-1; j>0; j--) mtmp->mtrack[j] = mtmp->mtrack[j-1];
427 mtmp->mtrack[0].x = omx;
428 mtmp->mtrack[0].y = omy;
430 if(mtmp->wormno) worm_move(mtmp);
433 if(msym == 'u' && rn2(2)){
438 if(mtmp->wormno) worm_nomove(mtmp);
443 if(mintrap(mtmp) == 2) /* he died */
445 if(likegold) mpickgold(mtmp);
446 if(likegems) mpickgems(mtmp);
447 if(mtmp->mhide) mtmp->mundetected = 1;
453 mpickgold(mtmp) 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);
463 mpickgems(mtmp) struct monst *mtmp; {
465 for(otmp = fobj; otmp; otmp = otmp->nobj)
466 if(otmp->olet == GEM_SYM)
467 if(otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
468 if(mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0){
470 mpickobj(mtmp, otmp);
471 if(levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
472 newsym(mtmp->mx, mtmp->my); /* %% */
473 return; /* pick only one object */
477 /* return number of acceptable neighbour positions */
478 mfndpos(mon,poss,info,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) {
556 return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
559 poisoned(string, pname)
560 char *string, *pname;
564 if(Blind) pline("It was poisoned.");
565 else pline("The %s was poisoned!",string);
566 if(Poison_resistance) {
567 pline("The poison doesn't seem to affect you.");
573 pline("I am afraid the poison was deadly ...");
577 losehp(rn1(10,6), pname);
592 if(mtmp->isshk) shkdead(mtmp);
593 if(mtmp->isgd) gddead();
595 if(mtmp->wormno) wormdead(mtmp);
600 /* called when monster is moved to larger structure */
602 struct monst *mtmp, *mtmp2;
608 if(u.ustuck == mtmp) u.ustuck = mtmp2;
609 if(mtmp2->isshk) replshk(mtmp,mtmp2);
610 if(mtmp2->isgd) replgd(mtmp,mtmp2);
618 if(mon == fmon) fmon = fmon->nmon;
620 for(mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon) ;
621 mtmp->nmon = mon->nmon;
625 /* we do not free monsters immediately, in order to have their name
626 available shortly after their demise */
627 struct monst *fdmon; /* chain of dead monsters, need not to be saved */
629 monfree(mtmp) struct monst *mtmp; {
645 if(u.ustuck == mtmp) {
664 struct permonst *mdat;
665 extern long newuexp();
667 if(mtmp->cham) mtmp->data = PM_CHAMELEON;
669 if(Blind) pline("You destroy it!");
671 pline("You destroy %s!",
672 mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
675 if(!Blind) pline("Your hands stop glowing blue.");
679 /* count killed monsters */
681 nk = 1; /* in case we cannot find it in mons */
682 tmp = mdat - mons; /* index in mons array (if not 'd', '@', ...) */
683 if(tmp >= 0 && tmp < CMNUM+2) {
684 extern char fut_geno[];
686 if((nk = u.nr_killed[tmp]) > MAXMONNO &&
687 !index(fut_geno, mdat->mlet))
688 charcat(fut_geno, mdat->mlet);
691 /* punish bad behaviour */
692 if(mdat->mlet == '@') Telepat = 0, u.uluck -= 2;
693 if(mtmp->mpeaceful || mtmp->mtame) u.uluck--;
694 if(mdat->mlet == 'u') u.uluck -= 5;
695 if((int)u.uluck < LUCKMIN) u.uluck = LUCKMIN;
697 /* give experience points */
698 tmp = 1 + mdat->mlevel * mdat->mlevel;
699 if(mdat->ac < 3) tmp += 2*(7 - mdat->ac);
700 if(index("AcsSDXaeRTVWU&In:P", mdat->mlet))
701 tmp += 2*mdat->mlevel;
702 if(index("DeV&P",mdat->mlet)) tmp += (7*mdat->mlevel);
703 if(mdat->mlevel > 6) tmp += 50;
704 if(mdat->mlet == ';') tmp += 1000;
707 /* ------- recent addition: make nr of points decrease
708 when this is not the first of this kind */
710 int ml = mdat->mlevel;
712 if(ul < 14) /* points are given based on present and future level */
713 for(tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
714 if(u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4<<(tmp2-1)))/nk
715 >= 10*pow((unsigned)(ul-1)))
716 if(++ul == 14) break;
719 tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
722 /* note: ul is not necessarily the future value of u.ulevel */
723 /* ------- end of recent valuation change ------- */
724 #endif /* NEW_SCORING */
726 more_experienced(tmp,0);
728 while(u.ulevel < 14 && u.uexp >= newuexp()){
729 pline("Welcome to experience level %u.", ++u.ulevel);
731 if(tmp < 3) tmp = rnd(10);
737 /* dispose of monster and make cadaver */
738 x = mtmp->mx; y = mtmp->my;
741 if(tmp == 'm') { /* he killed a minotaur, give him a wand of digging */
742 /* note: the dead minotaur will be on top of it! */
743 mksobj_at(WAN_DIGGING, x, y);
744 /* if(cansee(x,y)) atl(x,y,fobj->olet); */
749 mksobj_at(WORM_TOOTH, x, y);
753 if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
755 if(ACCESSIBLE(levl[x][y].typ)) /* might be mimic in wall or dead eel*/
756 if(x != u.ux || y != u.uy) /* might be here after swallowed */
757 if(index("NTVm&",mdat->mlet) || rn2(5)) {
758 struct obj *obj2 = mkobj_at(tmp,x,y);
769 if(*str == '%') pline(str,"It");
770 else pline(str,"it");
771 } else pline(str,arg);
774 rescham() /* force all chameleons to become normal */
778 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
781 (void) newcham(mtmp, PM_CHAMELEON);
785 newcham(mtmp,mdat) /* make a chameleon look like a new monster */
786 /* returns 1 if the monster actually changed */
788 struct permonst *mdat;
792 if(mdat == mtmp->data) return(0); /* still the same monster */
794 if(mtmp->wormno) wormdead(mtmp); /* throw tail away */
796 if (u.ustuck == mtmp) {
807 hpd = (mtmp->data->mlevel)*8;
810 mhp = (mdat->mlevel)*8;
811 /* new hp: same fraction of max as before */
812 mtmp->mhp = 2 + (hpn*mhp)/hpd;
814 mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
815 mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
817 if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
818 /* perhaps we should clear mtmp->mtame here? */
820 unpmon(mtmp); /* necessary for 'I' and to force pmon */
825 mnexto(mtmp) /* Make monster mtmp next to you (if possible) */
828 extern coord enexto();
830 mm = enexto(u.ux, u.uy);
836 ishuman(mtmp) struct monst *mtmp; {
837 return(mtmp->data->mlet == '@');
840 setmangry(mtmp) struct monst *mtmp; {
841 if(!mtmp->mpeaceful) return;
842 if(mtmp->mtame) return;
844 if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
847 /* not one hundred procent correct: now a snake may hide under an
852 return((!mtmp->minvis || See_invisible)
853 && (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
854 && cansee(mtmp->mx, mtmp->my));