1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.mon.c - version 1.0.3 */
5 #include "hack.mfndpos.h"
8 #define NULL (char *) 0
11 extern struct monst *makemon();
12 extern struct obj *mkobj_at();
14 int warnlevel; /* used by movemon and dochugw */
18 "white", "pink", "red", "ruby", "purple", "black"
23 register struct monst *mtmp;
29 /* find a monster that we haven't treated yet */
30 /* note that mtmp or mtmp->nmon might get killed
31 while mtmp moves, so we cannot just walk down the
32 chain (even new monsters might get created!) */
33 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
34 if(mtmp->mlstmv < moves) goto next_mon;
35 /* treated all monsters */
41 /* most monsters drown in pools */
42 { boolean inpool, iseel;
44 inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
45 iseel = (mtmp->data->mlet == ';');
46 if(inpool && !iseel) {
47 if(cansee(mtmp->mx,mtmp->my))
48 pline("%s drowns.", Monnam(mtmp));
52 /* but eels have a difficult time outside */
53 if(iseel && !inpool) {
54 if(mtmp->mhp > 1) mtmp->mhp--;
59 if(mtmp->mblinded && !--mtmp->mblinded)
61 if(mtmp->mfleetim && !--mtmp->mfleetim)
63 if(mtmp->mimic) continue;
64 if(mtmp->mspeed != MSLOW || !(moves%2)){
65 /* continue if the monster died fighting */
67 if(Conflict && cansee(mtmp->mx,mtmp->my)
68 && (fr = fightm(mtmp)) == 2)
70 if(fr<0 && dochugw(mtmp))
73 if(mtmp->mspeed == MFAST && dochugw(mtmp))
77 warnlevel -= u.ulevel;
78 if(warnlevel >= SIZE(warnings))
79 warnlevel = SIZE(warnings)-1;
81 if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
83 switch(Warning & (LEFT_RING | RIGHT_RING)){
85 rr = "Your left ring glows";
88 rr = "Your right ring glows";
90 case LEFT_RING | RIGHT_RING:
91 rr = "Both your rings glow";
94 rr = "Your fingertips glow";
97 pline("%s %s!", rr, warnings[warnlevel]);
99 lastwarnlev = warnlevel;
102 dmonsfree(); /* remove all dead monsters */
106 register struct monst *mtmp;
114 kludge("%s swallows you!",name);
122 youswld(mtmp,dam,die,name)
123 register struct monst *mtmp;
127 if(mtmp != u.ustuck) return;
128 kludge("%s digests you!",name);
130 if(u.uswldtim++ >= die){ /* a3 */
131 pline("It totally digests you!");
134 if(u.uhp < 1) done_in_by(mtmp);
135 /* flags.botlx = 1; */ /* should we show status line ? */
138 dochugw(mtmp) register struct monst *mtmp; {
139 register x = mtmp->mx;
140 register y = mtmp->my;
141 register d = dochug(mtmp);
143 if(!d) /* monster still alive */
146 if(mtmp->data->mlevel > warnlevel)
147 if((dd = dist(mtmp->mx,mtmp->my)) < dist(x,y))
150 warnlevel = mtmp->data->mlevel;
154 /* returns 1 if monster died moving, 0 otherwise */
156 register struct monst *mtmp;
158 register struct permonst *mdat;
159 register tmp, nearby, scared;
161 if(mtmp->cham && !rn2(6))
162 (void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
165 panic("bad monster %c (%d)",mdat->mlet,mdat->mlevel);
167 /* regenerate monsters */
168 if((!(moves%20) || index(MREGEN, mdat->mlet)) &&
169 mtmp->mhp < mtmp->mhpmax)
172 if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
175 /* wake up, or get out of here. */
176 /* ettins are hard to surprise */
177 /* Nymphs and Leprechauns do not easily wake up */
178 if(cansee(mtmp->mx,mtmp->my) &&
179 (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
180 (!index("NL",mdat->mlet) || !rn2(50)) &&
181 (Aggravate_monster || index("d1", mdat->mlet)
182 || (!rn2(7) && !mtmp->mimic)))
187 /* not frozen or sleeping: wipe out texts written in the dust */
188 wipe_engr_at(mtmp->mx, mtmp->my, 1);
190 /* confused monsters get unconfused with small probability */
191 if(mtmp->mconf && !rn2(50)) mtmp->mconf = 0;
193 /* some monsters teleport */
194 if(mtmp->mflee && index("tNL", mdat->mlet) && !rn2(40)){
198 if(mdat->mmove < rnd(6)) return(0);
200 /* fleeing monsters might regain courage */
201 if(mtmp->mflee && !mtmp->mfleetim
202 && mtmp->mhp == mtmp->mhpmax && !rn2(25))
205 nearby = (dist(mtmp->mx, mtmp->my) < 3);
206 scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
207 sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
208 if(scared && !mtmp->mflee) {
210 mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
216 (mtmp->minvis && !rn2(3)) ||
217 (index("BIuy", mdat->mlet) && !rn2(4)) ||
218 (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
219 (!mtmp->mcansee && !rn2(4)) ||
222 tmp = m_move(mtmp,0); /* 2: monster died moving */
223 if(tmp == 2 || (tmp && mdat->mmove <= 12))
227 if(!index("Ea", mdat->mlet) && nearby &&
228 !mtmp->mpeaceful && u.uhp > 0 && !scared) {
230 return(1); /* monster died (e.g. 'y' or 'F') */
232 /* extra movement for fast monsters */
233 if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
238 register struct monst *mtmp;
240 register struct monst *mtmp2;
241 register nx,ny,omx,omy,appr,nearer,cnt,i,j;
242 xchar gx,gy,nix,niy,chcnt;
244 boolean likegold, likegems, likeobjs;
245 char msym = mtmp->data->mlet;
246 schar mmoved = 0; /* not strictly nec.: chi >= 0 will do */
250 if(mtmp->mfroz || mtmp->msleep)
254 if(i == 2) return(2); /* he died */
255 if(i == 1) return(0); /* still in trap, so didnt move */
257 if(mtmp->mhide && o_at(mtmp->mx,mtmp->my) && rn2(10))
258 return(0); /* do not leave hiding place */
265 /* my dog gets a special treatment */
267 return( dog_move(mtmp, after) );
270 /* likewise for shopkeeper */
272 mmoved = shk_move(mtmp);
275 mmoved = 0; /* follow player outside shop */
278 /* and for the guard */
284 /* teleport if that lies in our nature ('t') or when badly wounded ('1') */
285 if((msym == 't' && !rn2(5))
286 || (msym == '1' && (mtmp->mhp < 7 || (!xdnstair && !rn2(5))
287 || levl[u.ux][u.uy].typ == STAIRS))) {
288 if(mtmp->mhp < 7 || (msym == 't' && rn2(2)))
296 /* spit fire ('D') or use a wand ('1') when appropriate */
297 if(index("D1", msym))
300 if(msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
301 mtmp->mcansee && rn2(5)) {
303 pline("%s's gaze has confused you!", Monnam(mtmp));
305 pline("You are getting more and more confused.");
306 if(rn2(3)) mtmp->mcan = 1;
307 Confusion += d(3,4); /* timeout */
310 if(!mtmp->mflee && u.uswallow && u.ustuck != mtmp) return(1);
312 if(mtmp->mflee) appr = -1;
313 if(mtmp->mconf || Invis || !mtmp->mcansee ||
314 (index("BIy", msym) && !rn2(3)))
320 if(msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
323 /* random criterion for 'smell' or track finding ability
324 should use mtmp->msmell or sth
327 ('a' <= msym && msym <= 'z')) {
328 extern coord *gettrack();
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 */
349 register struct gold *gold;
350 for(gold = fgold; gold; gold = gold->ngold)
351 if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
357 if(likegems || likeobjs){
358 register struct obj *otmp;
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 (void) 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;
451 mpickgold(mtmp) register struct monst *mtmp; {
452 register struct gold *gold;
453 while(gold = g_at(mtmp->mx, mtmp->my)){
454 mtmp->mgold += gold->amount;
456 if(levl[mtmp->mx][mtmp->my].scrsym == '$')
457 newsym(mtmp->mx, mtmp->my);
461 mpickgems(mtmp) register struct monst *mtmp; {
462 register struct obj *otmp;
463 for(otmp = fobj; otmp; otmp = otmp->nobj)
464 if(otmp->olet == GEM_SYM)
465 if(otmp->ox == mtmp->mx && otmp->oy == mtmp->my)
466 if(mtmp->data->mlet != 'u' || objects[otmp->otyp].g_val != 0){
468 mpickobj(mtmp, otmp);
469 if(levl[mtmp->mx][mtmp->my].scrsym == GEM_SYM)
470 newsym(mtmp->mx, mtmp->my); /* %% */
471 return; /* pick only one object */
475 /* return number of acceptable neighbour positions */
476 mfndpos(mon,poss,info,flag)
477 register struct monst *mon;
481 register int x,y,nx,ny,cnt = 0,ntyp;
482 register struct monst *mtmp;
488 nowtyp = levl[x][y].typ;
490 pool = (mon->data->mlet == ';');
491 nexttry: /* eels prefer the water, but if there is no water nearby,
492 they will crawl over land */
497 for(nx = x-1; nx <= x+1; nx++) for(ny = y-1; ny <= y+1; ny++)
498 if(nx != x || ny != y) if(isok(nx,ny))
499 if(!IS_ROCK(ntyp = levl[nx][ny].typ))
500 if(!(nx != x && ny != y && (nowtyp == DOOR || ntyp == DOOR)))
501 if((ntyp == POOL) == pool) {
503 if(nx == u.ux && ny == u.uy){
504 if(!(flag & ALLOW_U)) continue;
506 } else if(mtmp = m_at(nx,ny)){
507 if(!(flag & ALLOW_M)) continue;
510 if(!(flag & ALLOW_TM)) continue;
511 info[cnt] |= ALLOW_TM;
514 if(sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
515 if(flag & NOGARLIC) continue;
516 info[cnt] |= NOGARLIC;
518 if(sobj_at(SCR_SCARE_MONSTER, nx, ny) ||
519 (!mon->mpeaceful && sengr_at("Elbereth", nx, ny))) {
520 if(!(flag & ALLOW_SSM)) continue;
521 info[cnt] |= ALLOW_SSM;
523 if(sobj_at(ENORMOUS_ROCK, nx, ny)) {
524 if(!(flag & ALLOW_ROCK)) continue;
525 info[cnt] |= ALLOW_ROCK;
527 if(!Invis && online(nx,ny)){
528 if(flag & NOTONL) continue;
531 /* we cannot avoid traps of an unknown kind */
532 { register struct trap *ttmp = t_at(nx, ny);
535 tt = 1 << ttmp->ttyp;
536 if(mon->mtrapseen & tt){
537 if(!(flag & tt)) continue;
546 if(!cnt && pool && nowtyp != POOL) {
554 return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
557 poisoned(string, pname)
558 register char *string, *pname;
562 if(Blind) pline("It was poisoned.");
563 else pline("The %s was poisoned!",string);
564 if(Poison_resistance) {
565 pline("The poison doesn't seem to affect you.");
571 pline("I am afraid the poison was deadly ...");
575 losehp(rn1(10,6), pname);
584 register struct monst *mtmp;
590 if(mtmp->isshk) shkdead(mtmp);
591 if(mtmp->isgd) gddead();
593 if(mtmp->wormno) wormdead(mtmp);
598 /* called when monster is moved to larger structure */
600 register struct monst *mtmp, *mtmp2;
606 if(u.ustuck == mtmp) u.ustuck = mtmp2;
607 if(mtmp2->isshk) replshk(mtmp,mtmp2);
608 if(mtmp2->isgd) replgd(mtmp,mtmp2);
612 register struct monst *mon;
614 register struct monst *mtmp;
616 if(mon == fmon) fmon = fmon->nmon;
618 for(mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon) ;
619 mtmp->nmon = mon->nmon;
623 /* we do not free monsters immediately, in order to have their name
624 available shortly after their demise */
625 struct monst *fdmon; /* chain of dead monsters, need not to be saved */
627 monfree(mtmp) register struct monst *mtmp; {
633 register struct monst *mtmp;
641 register struct monst *mtmp;
643 if(u.ustuck == mtmp) {
656 register struct monst *mtmp;
661 register int tmp,tmp2,nk,x,y;
662 register struct permonst *mdat;
663 extern long newuexp();
665 if(mtmp->cham) mtmp->data = PM_CHAMELEON;
667 if(Blind) pline("You destroy it!");
669 pline("You destroy %s!",
670 mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
673 if(!Blind) pline("Your hands stop glowing blue.");
677 /* count killed monsters */
679 nk = 1; /* in case we cannot find it in mons */
680 tmp = mdat - mons; /* index in mons array (if not 'd', '@', ...) */
681 if(tmp >= 0 && tmp < CMNUM+2) {
682 extern char fut_geno[];
684 if((nk = u.nr_killed[tmp]) > MAXMONNO &&
685 !index(fut_geno, mdat->mlet))
686 charcat(fut_geno, mdat->mlet);
689 /* punish bad behaviour */
690 if(mdat->mlet == '@') Telepat = 0, u.uluck -= 2;
691 if(mtmp->mpeaceful || mtmp->mtame) u.uluck--;
692 if(mdat->mlet == 'u') u.uluck -= 5;
693 if((int)u.uluck < LUCKMIN) u.uluck = LUCKMIN;
695 /* give experience points */
696 tmp = 1 + mdat->mlevel * mdat->mlevel;
697 if(mdat->ac < 3) tmp += 2*(7 - mdat->ac);
698 if(index("AcsSDXaeRTVWU&In:P", mdat->mlet))
699 tmp += 2*mdat->mlevel;
700 if(index("DeV&P",mdat->mlet)) tmp += (7*mdat->mlevel);
701 if(mdat->mlevel > 6) tmp += 50;
702 if(mdat->mlet == ';') tmp += 1000;
705 /* ------- recent addition: make nr of points decrease
706 when this is not the first of this kind */
708 int ml = mdat->mlevel;
710 if(ul < 14) /* points are given based on present and future level */
711 for(tmp2 = 0; !tmp2 || ul + tmp2 <= ml; tmp2++)
712 if(u.uexp + 1 + (tmp + ((tmp2 <= 0) ? 0 : 4<<(tmp2-1)))/nk
713 >= 10*pow((unsigned)(ul-1)))
714 if(++ul == 14) break;
717 tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
720 /* note: ul is not necessarily the future value of u.ulevel */
721 /* ------- end of recent valuation change ------- */
724 more_experienced(tmp,0);
726 while(u.ulevel < 14 && u.uexp >= newuexp()){
727 pline("Welcome to experience level %u.", ++u.ulevel);
729 if(tmp < 3) tmp = rnd(10);
735 /* dispose of monster and make cadaver */
736 x = mtmp->mx; y = mtmp->my;
739 if(tmp == 'm') { /* he killed a minotaur, give him a wand of digging */
740 /* note: the dead minotaur will be on top of it! */
741 mksobj_at(WAN_DIGGING, x, y);
742 /* if(cansee(x,y)) atl(x,y,fobj->olet); */
747 mksobj_at(WORM_TOOTH, x, y);
751 if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
753 if(ACCESSIBLE(levl[x][y].typ)) /* might be mimic in wall or dead eel*/
754 if(x != u.ux || y != u.uy) /* might be here after swallowed */
755 if(index("NTVm&",mdat->mlet) || rn2(5)) {
756 register struct obj *obj2 = mkobj_at(tmp,x,y);
764 register char *str,*arg;
767 if(*str == '%') pline(str,"It");
768 else pline(str,"it");
769 } else pline(str,arg);
772 rescham() /* force all chameleons to become normal */
774 register struct monst *mtmp;
776 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
779 (void) newcham(mtmp, PM_CHAMELEON);
783 newcham(mtmp,mdat) /* make a chameleon look like a new monster */
784 /* returns 1 if the monster actually changed */
785 register struct monst *mtmp;
786 register struct permonst *mdat;
788 register mhp, hpn, hpd;
790 if(mdat == mtmp->data) return(0); /* still the same monster */
792 if(mtmp->wormno) wormdead(mtmp); /* throw tail away */
794 if (u.ustuck == mtmp) {
805 hpd = (mtmp->data->mlevel)*8;
808 mhp = (mdat->mlevel)*8;
809 /* new hp: same fraction of max as before */
810 mtmp->mhp = 2 + (hpn*mhp)/hpd;
812 mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
813 mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
815 if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
816 /* perhaps we should clear mtmp->mtame here? */
818 unpmon(mtmp); /* necessary for 'I' and to force pmon */
823 mnexto(mtmp) /* Make monster mtmp next to you (if possible) */
826 extern coord enexto();
828 mm = enexto(u.ux, u.uy);
834 ishuman(mtmp) register struct monst *mtmp; {
835 return(mtmp->data->mlet == '@');
838 setmangry(mtmp) register struct monst *mtmp; {
839 if(!mtmp->mpeaceful) return;
840 if(mtmp->mtame) return;
842 if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
845 /* not one hundred procent correct: now a snake may hide under an
848 register struct monst *mtmp;
850 return((!mtmp->minvis || See_invisible)
851 && (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
852 && cansee(mtmp->mx, mtmp->my));