Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / hack / hack.mon.c
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
5 #include "hack.h"
6 #include "hack.mfndpos.h"
7
8 #ifndef NULL
9 #define NULL    (char *) 0
10 #endif
11
12 extern struct monst *makemon();
13 extern struct obj *mkobj_at();
14
15 int warnlevel;          /* used by movemon and dochugw */
16 long lastwarntime;
17 int lastwarnlev;
18 char *warnings[] = {
19         "white", "pink", "red", "ruby", "purple", "black"
20 };
21
22 movemon()
23 {
24         struct monst *mtmp;
25         int fr;
26
27         warnlevel = 0;
28
29         while(1) {
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 */
37                 break;
38
39         next_mon:
40                 mtmp->mlstmv = moves;
41
42                 /* most monsters drown in pools */
43                 { boolean inpool, iseel;
44
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));
50                         mondead(mtmp);
51                         continue;
52                   }
53                 /* but eels have a difficult time outside */
54                   if(iseel && !inpool) {
55                         if(mtmp->mhp > 1) mtmp->mhp--;
56                         mtmp->mflee = 1;
57                         mtmp->mfleetim += 2;
58                   }
59                 }
60                 if(mtmp->mblinded && !--mtmp->mblinded)
61                         mtmp->mcansee = 1;
62                 if(mtmp->mfleetim && !--mtmp->mfleetim)
63                         mtmp->mflee = 0;
64                 if(mtmp->mimic) continue;
65                 if(mtmp->mspeed != MSLOW || !(moves%2)){
66                         /* continue if the monster died fighting */
67                         fr = -1;
68                         if(Conflict && cansee(mtmp->mx,mtmp->my)
69                                 && (fr = fightm(mtmp)) == 2)
70                                 continue;
71                         if(fr<0 && dochugw(mtmp))
72                                 continue;
73                 }
74                 if(mtmp->mspeed == MFAST && dochugw(mtmp))
75                         continue;
76         }
77
78         warnlevel -= u.ulevel;
79         if(warnlevel >= SIZE(warnings))
80                 warnlevel = SIZE(warnings)-1;
81         if(warnlevel >= 0)
82         if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
83             char *rr;
84             switch(Warning & (LEFT_RING | RIGHT_RING)){
85             case LEFT_RING:
86                 rr = "Your left ring glows";
87                 break;
88             case RIGHT_RING:
89                 rr = "Your right ring glows";
90                 break;
91             case LEFT_RING | RIGHT_RING:
92                 rr = "Both your rings glow";
93                 break;
94             default:
95                 rr = "Your fingertips glow";
96                 break;
97             }
98             pline("%s %s!", rr, warnings[warnlevel]);
99             lastwarntime = moves;
100             lastwarnlev = warnlevel;
101         }
102
103         dmonsfree();    /* remove all dead monsters */
104 }
105
106 justswld(mtmp,name)
107 struct monst *mtmp;
108 char *name;
109 {
110
111         mtmp->mx = u.ux;
112         mtmp->my = u.uy;
113         u.ustuck = mtmp;
114         pmon(mtmp);
115         kludge("%s swallows you!",name);
116         more();
117         seeoff(1);
118         u.uswallow = 1;
119         u.uswldtim = 0;
120         swallowed();
121 }
122
123 youswld(mtmp,dam,die,name)
124 struct monst *mtmp;
125 int dam,die;
126 char *name;
127 {
128         if(mtmp != u.ustuck) return;
129         kludge("%s digests you!",name);
130         u.uhp -= dam;
131         if(u.uswldtim++ >= die){        /* a3 */
132                 pline("It totally digests you!");
133                 u.uhp = -1;
134         }
135         if(u.uhp < 1) done_in_by(mtmp);
136         /* flags.botlx = 1; */          /* should we show status line ? */
137 }
138
139 dochugw(mtmp) struct monst *mtmp; {
140 int x = mtmp->mx;
141 int y = mtmp->my;
142 int d = dochug(mtmp);
143 int dd;
144         if(!d)          /* monster still alive */
145         if(Warning)
146         if(!mtmp->mpeaceful)
147         if(mtmp->data->mlevel > warnlevel)
148         if((dd = dist(mtmp->mx,mtmp->my)) < dist(x,y))
149         if(dd < 100)
150         if(!canseemon(mtmp))
151                 warnlevel = mtmp->data->mlevel;
152         return(d);
153 }
154
155 /* returns 1 if monster died moving, 0 otherwise */
156 dochug(mtmp)
157 struct monst *mtmp;
158 {
159         struct permonst *mdat;
160         int tmp, nearby, scared;
161
162         if(mtmp->cham && !rn2(6))
163                 (void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
164         mdat = mtmp->data;
165         if(mdat->mlevel < 0)
166                 panic("bad monster %c (%d)",mdat->mlet,mdat->mlevel);
167
168         /* regenerate monsters */
169         if((!(moves%20) || index(MREGEN, mdat->mlet)) &&
170             mtmp->mhp < mtmp->mhpmax)
171                 mtmp->mhp++;
172
173         if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
174
175         if(mtmp->msleep) {
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)))
184                         mtmp->msleep = 0;
185                 else return(0);
186         }
187
188         /* not frozen or sleeping: wipe out texts written in the dust */
189         wipe_engr_at(mtmp->mx, mtmp->my, 1);
190
191         /* confused monsters get unconfused with small probability */
192         if(mtmp->mconf && !rn2(50)) mtmp->mconf = 0;
193
194         /* some monsters teleport */
195         if(mtmp->mflee && index("tNL", mdat->mlet) && !rn2(40)){
196                 rloc(mtmp);
197                 return(0);
198         }
199         if(mdat->mmove < rnd(6)) return(0);
200
201         /* fleeing monsters might regain courage */
202         if(mtmp->mflee && !mtmp->mfleetim
203             && mtmp->mhp == mtmp->mhpmax && !rn2(25))
204                 mtmp->mflee = 0;
205
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) {
210                 mtmp->mflee = 1;
211                 mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
212         }
213
214         if(!nearby ||
215                 mtmp->mflee ||
216                 mtmp->mconf ||
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)) ||
221                 mtmp->mpeaceful
222            ) {
223                 tmp = m_move(mtmp,0);   /* 2: monster died moving */
224                 if(tmp == 2 || (tmp && mdat->mmove <= 12))
225                         return(tmp == 2);
226         }
227
228         if(!index("Ea", mdat->mlet) && nearby &&
229          !mtmp->mpeaceful && u.uhp > 0 && !scared) {
230                 if(mhitu(mtmp))
231                         return(1);      /* monster died (e.g. 'y' or 'F') */
232         }
233         /* extra movement for fast monsters */
234         if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
235         return(tmp == 2);
236 }
237
238 m_move(mtmp,after)
239 struct monst *mtmp;
240 {
241         struct monst *mtmp2;
242         int nx,ny,omx,omy,appr,nearer,cnt,i,j;
243         xchar gx,gy,nix,niy,chcnt;
244         schar chi;
245         boolean likegold, likegems, likeobjs;
246         char msym = mtmp->data->mlet;
247         schar mmoved = 0;       /* not strictly nec.: chi >= 0 will do */
248         coord poss[9];
249         int info[9];
250
251         if(mtmp->mfroz || mtmp->msleep)
252                 return(0);
253         if(mtmp->mtrapped) {
254                 i = mintrap(mtmp);
255                 if(i == 2) return(2);   /* he died */
256                 if(i == 1) return(0);   /* still in trap, so didnt move */
257         }
258         if(mtmp->mhide && o_at(mtmp->mx,mtmp->my) && rn2(10))
259                 return(0);              /* do not leave hiding place */
260
261 #ifndef NOWORM
262         if(mtmp->wormno)
263                 goto not_special;
264 #endif NOWORM
265
266         /* my dog gets a special treatment */
267         if(mtmp->mtame) {
268                 return( dog_move(mtmp, after) );
269         }
270
271         /* likewise for shopkeeper */
272         if(mtmp->isshk) {
273                 mmoved = shk_move(mtmp);
274                 if(mmoved >= 0)
275                         goto postmov;
276                 mmoved = 0;             /* follow player outside shop */
277         }
278
279         /* and for the guard */
280         if(mtmp->isgd) {
281                 mmoved = gd_move();
282                 goto postmov;
283         }
284
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)))
290                         rloc(mtmp);
291                 else
292                         mnexto(mtmp);
293                 mmoved = 1;
294                 goto postmov;
295         }
296
297         /* spit fire ('D') or use a wand ('1') when appropriate */
298         if(index("D1", msym))
299                 inrange(mtmp);
300
301         if(msym == 'U' && !mtmp->mcan && canseemon(mtmp) &&
302             mtmp->mcansee && rn2(5)) {
303                 if(!Confusion)
304                         pline("%s's gaze has confused you!", Monnam(mtmp));
305                 else
306                         pline("You are getting more and more confused.");
307                 if(rn2(3)) mtmp->mcan = 1;
308                 Confusion += d(3,4);            /* timeout */
309         }
310 not_special:
311         if(!mtmp->mflee && u.uswallow && u.ustuck != mtmp) return(1);
312         appr = 1;
313         if(mtmp->mflee) appr = -1;
314         if(mtmp->mconf || Invis ||  !mtmp->mcansee ||
315                 (index("BIy", msym) && !rn2(3)))
316                 appr = 0;
317         omx = mtmp->mx;
318         omy = mtmp->my;
319         gx = u.ux;
320         gy = u.uy;
321         if(msym == 'L' && appr == 1 && mtmp->mgold > u.ugold)
322                 appr = -1;
323
324         /* random criterion for 'smell' or track finding ability
325            should use mtmp->msmell or sth
326          */
327         if(msym == '@' ||
328           ('a' <= msym && msym <= 'z')) {
329         extern coord *gettrack();
330         coord *cp;
331         schar mroom;
332                 mroom = inroom(omx,omy);
333                 if(mroom < 0 || mroom != inroom(u.ux,u.uy)){
334                     cp = gettrack(omx,omy);
335                     if(cp){
336                         gx = cp->x;
337                         gy = cp->y;
338                     }
339                 }
340         }
341
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 */
348           int dd;
349           if(likegold){
350                 struct gold *gold;
351                 for(gold = fgold; gold; gold = gold->ngold)
352                   if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
353                     mind = dd;
354                     gx = gold->gx;
355                     gy = gold->gy;
356                 }
357           }
358           if(likegems || likeobjs){
359                 struct obj *otmp;
360                 for(otmp = fobj; otmp; otmp = otmp->nobj)
361                 if(likeobjs || otmp->olet == GEM_SYM)
362                 if(msym != 'u' ||
363                         objects[otmp->otyp].g_val != 0)
364                 if((dd = DIST(omx,omy,otmp->ox,otmp->oy)) < mind){
365                     mind = dd;
366                     gx = otmp->ox;
367                     gy = otmp->oy;
368                 }
369           }
370           if(mind < SRCHRADIUS && appr == -1) {
371                 if(dist(omx,omy) < 10) {
372                     gx = u.ux;
373                     gy = u.uy;
374                 } else
375                     appr = 1;
376           }
377         }
378         nix = omx;
379         niy = omy;
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 ? */
385         chcnt = 0;
386         chi = -1;
387         for(i=0; i<cnt; i++) {
388                 nx = poss[i].x;
389                 ny = poss[i].y;
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;
393 #ifdef STUPID
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);
397                   nearer = (d1 < d2);
398                 }
399 #else
400                 nearer = (DIST(nx,ny,gx,gy) < DIST(nix,niy,gx,gy));
401 #endif STUPID
402                 if((appr == 1 && nearer) || (appr == -1 && !nearer) ||
403                         !mmoved ||
404                         (!appr && !rn2(++chcnt))){
405                         nix = nx;
406                         niy = ny;
407                         chi = i;
408                         mmoved = 1;
409                 }
410         nxti:   ;
411         }
412         if(mmoved){
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);
417                         return(0);
418                 }
419                 if(info[chi] & ALLOW_U){
420                   (void) hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd)+1);
421                   return(0);
422                 }
423                 mtmp->mx = nix;
424                 mtmp->my = niy;
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;
428 #ifndef NOWORM
429                 if(mtmp->wormno) worm_move(mtmp);
430 #endif NOWORM
431         } else {
432                 if(msym == 'u' && rn2(2)){
433                         rloc(mtmp);
434                         return(0);
435                 }
436 #ifndef NOWORM
437                 if(mtmp->wormno) worm_nomove(mtmp);
438 #endif NOWORM
439         }
440 postmov:
441         if(mmoved == 1) {
442                 if(mintrap(mtmp) == 2)  /* he died */
443                         return(2);
444                 if(likegold) mpickgold(mtmp);
445                 if(likegems) mpickgems(mtmp);
446                 if(mtmp->mhide) mtmp->mundetected = 1;
447         }
448         pmon(mtmp);
449         return(mmoved);
450 }
451
452 mpickgold(mtmp) struct monst *mtmp; {
453 struct gold *gold;
454         while(gold = g_at(mtmp->mx, mtmp->my)){
455                 mtmp->mgold += gold->amount;
456                 freegold(gold);
457                 if(levl[mtmp->mx][mtmp->my].scrsym == '$')
458                         newsym(mtmp->mx, mtmp->my);
459         }
460 }
461
462 mpickgems(mtmp) struct monst *mtmp; {
463 struct obj *otmp;
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){
468                 freeobj(otmp);
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 */
473         }
474 }
475
476 /* return number of acceptable neighbour positions */
477 mfndpos(mon,poss,info,flag)
478 struct monst *mon;
479 coord poss[9];
480 int info[9], flag;
481 {
482         int x,y,nx,ny,cnt = 0,ntyp;
483         struct monst *mtmp;
484         int nowtyp;
485         boolean pool;
486
487         x = mon->mx;
488         y = mon->my;
489         nowtyp = levl[x][y].typ;
490
491         pool = (mon->data->mlet == ';');
492 nexttry:        /* eels prefer the water, but if there is no water nearby,
493                    they will crawl over land */
494         if(mon->mconf) {
495                 flag |= ALLOW_ALL;
496                 flag &= ~NOTONL;
497         }
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) {
503                 info[cnt] = 0;
504                 if(nx == u.ux && ny == u.uy){
505                         if(!(flag & ALLOW_U)) continue;
506                         info[cnt] = ALLOW_U;
507                 } else if(mtmp = m_at(nx,ny)){
508                         if(!(flag & ALLOW_M)) continue;
509                         info[cnt] = ALLOW_M;
510                         if(mtmp->mtame){
511                                 if(!(flag & ALLOW_TM)) continue;
512                                 info[cnt] |= ALLOW_TM;
513                         }
514                 }
515                 if(sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
516                         if(flag & NOGARLIC) continue;
517                         info[cnt] |= NOGARLIC;
518                 }
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;
523                 }
524                 if(sobj_at(ENORMOUS_ROCK, nx, ny)) {
525                         if(!(flag & ALLOW_ROCK)) continue;
526                         info[cnt] |= ALLOW_ROCK;
527                 }
528                 if(!Invis && online(nx,ny)){
529                         if(flag & NOTONL) continue;
530                         info[cnt] |= NOTONL;
531                 }
532                 /* we cannot avoid traps of an unknown kind */
533                 { struct trap *ttmp = t_at(nx, ny);
534                   int tt;
535                         if(ttmp) {
536                                 tt = 1 << ttmp->ttyp;
537                                 if(mon->mtrapseen & tt){
538                                         if(!(flag & tt)) continue;
539                                         info[cnt] |= tt;
540                                 }
541                         }
542                 }
543                 poss[cnt].x = nx;
544                 poss[cnt].y = ny;
545                 cnt++;
546         }
547         if(!cnt && pool && nowtyp != POOL) {
548                 pool = FALSE;
549                 goto nexttry;
550         }
551         return(cnt);
552 }
553
554 dist(x,y) int x,y; {
555         return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
556 }
557
558 poisoned(string, pname)
559 char *string, *pname;
560 {
561         int i;
562
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.");
567                 return;
568         }
569         i = rn2(10);
570         if(i == 0) {
571                 u.uhp = -1;
572                 pline("I am afraid the poison was deadly ...");
573         } else if(i <= 5) {
574                 losestr(rn1(3,3));
575         } else {
576                 losehp(rn1(10,6), pname);
577         }
578         if(u.uhp < 1) {
579                 killer = pname;
580                 done("died");
581         }
582 }
583
584 mondead(mtmp)
585 struct monst *mtmp;
586 {
587         relobj(mtmp,1);
588         unpmon(mtmp);
589         relmon(mtmp);
590         unstuck(mtmp);
591         if(mtmp->isshk) shkdead(mtmp);
592         if(mtmp->isgd) gddead();
593 #ifndef NOWORM
594         if(mtmp->wormno) wormdead(mtmp);
595 #endif NOWORM
596         monfree(mtmp);
597 }
598
599 /* called when monster is moved to larger structure */
600 replmon(mtmp,mtmp2)
601 struct monst *mtmp, *mtmp2;
602 {
603         relmon(mtmp);
604         monfree(mtmp);
605         mtmp2->nmon = fmon;
606         fmon = mtmp2;
607         if(u.ustuck == mtmp) u.ustuck = mtmp2;
608         if(mtmp2->isshk) replshk(mtmp,mtmp2);
609         if(mtmp2->isgd) replgd(mtmp,mtmp2);
610 }
611
612 relmon(mon)
613 struct monst *mon;
614 {
615         struct monst *mtmp;
616
617         if(mon == fmon) fmon = fmon->nmon;
618         else {
619                 for(mtmp = fmon; mtmp->nmon != mon; mtmp = mtmp->nmon) ;
620                 mtmp->nmon = mon->nmon;
621         }
622 }
623
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 */
627
628 monfree(mtmp) struct monst *mtmp; {
629         mtmp->nmon = fdmon;
630         fdmon = mtmp;
631 }
632
633 dmonsfree(){
634 struct monst *mtmp;
635         while(mtmp = fdmon){
636                 fdmon = mtmp->nmon;
637                 free((char *) mtmp);
638         }
639 }
640
641 unstuck(mtmp)
642 struct monst *mtmp;
643 {
644         if(u.ustuck == mtmp) {
645                 if(u.uswallow){
646                         u.ux = mtmp->mx;
647                         u.uy = mtmp->my;
648                         u.uswallow = 0;
649                         setsee();
650                         docrt();
651                 }
652                 u.ustuck = 0;
653         }
654 }
655
656 killed(mtmp)
657 struct monst *mtmp;
658 {
659 #ifdef lint
660 #define NEW_SCORING
661 #endif lint
662         int tmp,tmp2,nk,x,y;
663         struct permonst *mdat;
664         extern long newuexp();
665
666         if(mtmp->cham) mtmp->data = PM_CHAMELEON;
667         mdat = mtmp->data;
668         if(Blind) pline("You destroy it!");
669         else {
670                 pline("You destroy %s!",
671                         mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
672         }
673         if(u.umconf) {
674                 if(!Blind) pline("Your hands stop glowing blue.");
675                 u.umconf = 0;
676         }
677
678         /* count killed monsters */
679 #define MAXMONNO        100
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[];
684             u.nr_killed[tmp]++;
685             if((nk = u.nr_killed[tmp]) > MAXMONNO &&
686                 !index(fut_geno, mdat->mlet))
687                     charcat(fut_geno,  mdat->mlet);
688         }
689
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;
695
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;
704
705 #ifdef NEW_SCORING
706         /* ------- recent addition: make nr of points decrease
707                    when this is not the first of this kind */
708         { int ul = u.ulevel;
709           int ml = mdat->mlevel;
710
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;
716
717         tmp2 = ml - ul -1;
718         tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
719         if(!tmp) tmp = 1;
720         }
721         /* note: ul is not necessarily the future value of u.ulevel */
722         /* ------- end of recent valuation change ------- */
723 #endif NEW_SCORING
724
725         more_experienced(tmp,0);
726         flags.botl = 1;
727         while(u.ulevel < 14 && u.uexp >= newuexp()){
728                 pline("Welcome to experience level %u.", ++u.ulevel);
729                 tmp = rnd(10);
730                 if(tmp < 3) tmp = rnd(10);
731                 u.uhpmax += tmp;
732                 u.uhp += tmp;
733                 flags.botl = 1;
734         }
735
736         /* dispose of monster and make cadaver */
737         x = mtmp->mx;   y = mtmp->my;
738         mondead(mtmp);
739         tmp = mdat->mlet;
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); */
744                 stackobj(fobj);
745         } else
746 #ifndef NOWORM
747         if(tmp == 'w') {
748                 mksobj_at(WORM_TOOTH, x, y);
749                 stackobj(fobj);
750         } else
751 #endif  NOWORM
752         if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
753
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);
758                 if(cansee(x,y))
759                         atl(x,y,obj2->olet);
760                 stackobj(obj2);
761         }
762 }
763
764 kludge(str,arg)
765 char *str,*arg;
766 {
767         if(Blind) {
768                 if(*str == '%') pline(str,"It");
769                 else pline(str,"it");
770         } else pline(str,arg);
771 }
772
773 rescham()       /* force all chameleons to become normal */
774 {
775         struct monst *mtmp;
776
777         for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
778                 if(mtmp->cham) {
779                         mtmp->cham = 0;
780                         (void) newcham(mtmp, PM_CHAMELEON);
781                 }
782 }
783
784 newcham(mtmp,mdat)      /* make a chameleon look like a new monster */
785                         /* returns 1 if the monster actually changed */
786 struct monst *mtmp;
787 struct permonst *mdat;
788 {
789         int mhp, hpn, hpd;
790
791         if(mdat == mtmp->data) return(0);       /* still the same monster */
792 #ifndef NOWORM
793         if(mtmp->wormno) wormdead(mtmp);        /* throw tail away */
794 #endif NOWORM
795         if (u.ustuck == mtmp) {
796                 if (u.uswallow) {
797                         u.uswallow = 0;
798                         u.uswldtim = 0;
799                         mnexto (mtmp);
800                         docrt ();
801                         prme ();
802                 }
803                 u.ustuck = 0;
804         }
805         hpn = mtmp->mhp;
806         hpd = (mtmp->data->mlevel)*8;
807         if(!hpd) hpd = 4;
808         mtmp->data = mdat;
809         mhp = (mdat->mlevel)*8;
810         /* new hp: same fraction of max as before */
811         mtmp->mhp = 2 + (hpn*mhp)/hpd;
812         hpn = mtmp->mhpmax;
813         mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
814         mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
815 #ifndef NOWORM
816         if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
817                         /* perhaps we should clear mtmp->mtame here? */
818 #endif NOWORM
819         unpmon(mtmp);   /* necessary for 'I' and to force pmon */
820         pmon(mtmp);
821         return(1);
822 }
823
824 mnexto(mtmp)    /* Make monster mtmp next to you (if possible) */
825 struct monst *mtmp;
826 {
827         extern coord enexto();
828         coord mm;
829         mm = enexto(u.ux, u.uy);
830         mtmp->mx = mm.x;
831         mtmp->my = mm.y;
832         pmon(mtmp);
833 }
834
835 ishuman(mtmp) struct monst *mtmp; {
836         return(mtmp->data->mlet == '@');
837 }
838
839 setmangry(mtmp) struct monst *mtmp; {
840         if(!mtmp->mpeaceful) return;
841         if(mtmp->mtame) return;
842         mtmp->mpeaceful = 0;
843         if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
844 }
845
846 /* not one hundred procent correct: now a snake may hide under an
847    invisible object */
848 canseemon(mtmp)
849 struct monst *mtmp;
850 {
851         return((!mtmp->minvis || See_invisible)
852                 && (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
853                 && cansee(mtmp->mx, mtmp->my));
854 }