MFC: Make apps using '#define _POSIX_C_SOURCE' compile.
[dragonfly.git] / games / hack / hack.mon.c
blob79a491838e5464f985e86501c9fbcf00be93af03
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 $ */
6 #include "hack.h"
7 #include "hack.mfndpos.h"
9 int warnlevel; /* used by movemon and dochugw */
10 long lastwarntime;
11 int lastwarnlev;
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 *);
22 void
23 movemon(void)
25 struct monst *mtmp;
26 int fr;
28 warnlevel = 0;
30 while(1) {
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 */
38 break;
40 next_mon:
41 mtmp->mlstmv = moves;
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));
51 mondead(mtmp);
52 continue;
54 /* but eels have a difficult time outside */
55 if(iseel && !inpool) {
56 if(mtmp->mhp > 1) mtmp->mhp--;
57 mtmp->mflee = 1;
58 mtmp->mfleetim += 2;
61 if(mtmp->mblinded && !--mtmp->mblinded)
62 mtmp->mcansee = 1;
63 if(mtmp->mfleetim && !--mtmp->mfleetim)
64 mtmp->mflee = 0;
65 if(mtmp->mimic) continue;
66 if(mtmp->mspeed != MSLOW || !(moves%2)){
67 /* continue if the monster died fighting */
68 fr = -1;
69 if(Conflict && cansee(mtmp->mx,mtmp->my)
70 && (fr = fightm(mtmp)) == 2)
71 continue;
72 if(fr<0 && dochugw(mtmp))
73 continue;
75 if(mtmp->mspeed == MFAST && dochugw(mtmp))
76 continue;
79 warnlevel -= u.ulevel;
80 if(warnlevel >= SIZE(warnings))
81 warnlevel = SIZE(warnings)-1;
82 if(warnlevel >= 0)
83 if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
84 const char *rr;
85 switch(Warning & (LEFT_RING | RIGHT_RING)){
86 case LEFT_RING:
87 rr = "Your left ring glows";
88 break;
89 case RIGHT_RING:
90 rr = "Your right ring glows";
91 break;
92 case LEFT_RING | RIGHT_RING:
93 rr = "Both your rings glow";
94 break;
95 default:
96 rr = "Your fingertips glow";
97 break;
99 pline("%s %s!", rr, warnings[warnlevel]);
100 lastwarntime = moves;
101 lastwarnlev = warnlevel;
104 dmonsfree(); /* remove all dead monsters */
107 void
108 justswld(struct monst *mtmp, const char *name)
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();
123 void
124 youswld(struct monst *mtmp, int dam, int die, const char *name)
126 if(mtmp != u.ustuck) return;
127 kludge("%s digests you!",name);
128 u.uhp -= dam;
129 if((int)u.uswldtim++ >= die){ /* a3 */
130 pline("It totally digests you!");
131 u.uhp = -1;
133 if(u.uhp < 1) done_in_by(mtmp);
134 /* flags.botlx = 1; */ /* should we show status line ? */
137 static int
138 dochugw(struct monst *mtmp)
140 int x = mtmp->mx;
141 int y = mtmp->my;
142 int d1 = dochug(mtmp);
143 int dd;
144 if(!d1) /* 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(d1);
155 /* returns 1 if monster died moving, 0 otherwise */
156 bool
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)]);
164 mdat = mtmp->data;
165 if(mdat->mlevel < 0)
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)
171 mtmp->mhp++;
173 if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
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);
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)){
196 rloc(mtmp);
197 return(0);
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))
204 mtmp->mflee = 0;
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));
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
223 tmp = m_move(mtmp,0); /* 2: monster died moving */
224 if(tmp == 2 || (tmp && mdat->mmove <= 12))
225 return(tmp == 2);
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') */
233 /* extra movement for fast monsters */
234 if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
235 return(tmp == 2);
239 m_move(struct monst *mtmp, int after)
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 = 0, likegems = 0, 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];
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 */
258 if(mtmp->mhide && o_at(mtmp->mx,mtmp->my) && rn2(10))
259 return(0); /* do not leave hiding place */
261 #ifndef NOWORM
262 if(mtmp->wormno)
263 goto not_special;
264 #endif /* NOWORM */
266 /* my dog gets a special treatment */
267 if(mtmp->mtame) {
268 return( dog_move(mtmp, after) );
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 */
279 /* and for the guard */
280 if(mtmp->isgd) {
281 mmoved = gd_move();
282 goto postmov;
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;
297 /* spit fire ('D') or use a wand ('1') when appropriate */
298 if(index("D1", msym))
299 inrange(mtmp);
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 */
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;
324 /* random criterion for 'smell' or track finding ability
325 should use mtmp->msmell or sth
327 if(msym == '@' ||
328 ('a' <= msym && msym <= 'z')) {
329 coord *cp;
330 schar mroom;
331 mroom = inroom(omx,omy);
332 if(mroom < 0 || mroom != inroom(u.ux,u.uy)){
333 cp = gettrack(omx,omy);
334 if(cp){
335 gx = cp->x;
336 gy = cp->y;
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 */
347 int dd;
348 if(likegold){
349 struct gold *gold;
350 for(gold = fgold; gold; gold = gold->ngold)
351 if((dd = DIST(omx,omy,gold->gx,gold->gy)) < mind){
352 mind = dd;
353 gx = gold->gx;
354 gy = gold->gy;
357 if(likegems || likeobjs){
358 struct obj *otmp;
359 for(otmp = fobj; otmp; otmp = otmp->nobj)
360 if(likeobjs || otmp->olet == GEM_SYM)
361 if(msym != 'u' ||
362 objects[otmp->otyp].g_val != 0)
363 if((dd = DIST(omx,omy,otmp->ox,otmp->oy)) < mind){
364 mind = dd;
365 gx = otmp->ox;
366 gy = otmp->oy;
369 if(mind < SRCHRADIUS && appr == -1) {
370 if(dist(omx,omy) < 10) {
371 gx = u.ux;
372 gy = u.uy;
373 } else
374 appr = 1;
377 nix = omx;
378 niy = omy;
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 ? */
384 chcnt = 0;
385 chi = -1;
386 for(i=0; i<cnt; i++) {
387 nx = poss[i].x;
388 ny = poss[i].y;
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;
392 #ifdef STUPID
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);
396 nearer = (d1 < d2);
398 #else
399 nearer = (DIST(nx,ny,gx,gy) < DIST(nix,niy,gx,gy));
400 #endif /* STUPID */
401 if((appr == 1 && nearer) || (appr == -1 && !nearer) ||
402 !mmoved ||
403 (!appr && !rn2(++chcnt))){
404 nix = nx;
405 niy = ny;
406 chi = i;
407 mmoved = 1;
409 nxti: ;
411 if(mmoved){
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);
416 return(0);
418 if(info[chi] & ALLOW_U){
419 hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd)+1);
420 return(0);
422 mtmp->mx = nix;
423 mtmp->my = niy;
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;
427 #ifndef NOWORM
428 if(mtmp->wormno) worm_move(mtmp);
429 #endif /* NOWORM */
430 } else {
431 if(msym == 'u' && rn2(2)){
432 rloc(mtmp);
433 return(0);
435 #ifndef NOWORM
436 if(mtmp->wormno) worm_nomove(mtmp);
437 #endif /* NOWORM */
439 postmov:
440 if(mmoved == 1) {
441 if(mintrap(mtmp) == 2) /* he died */
442 return(2);
443 if(likegold) mpickgold(mtmp);
444 if(likegems) mpickgems(mtmp);
445 if(mtmp->mhide) mtmp->mundetected = 1;
447 pmon(mtmp);
448 return(mmoved);
451 static void
452 mpickgold(struct monst *mtmp)
454 struct gold *gold;
455 while((gold = g_at(mtmp->mx, mtmp->my))){
456 mtmp->mgold += gold->amount;
457 freegold(gold);
458 if(levl[mtmp->mx][mtmp->my].scrsym == '$')
459 newsym(mtmp->mx, mtmp->my);
463 static void
464 mpickgems(struct monst *mtmp)
466 struct obj *otmp;
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){
471 freeobj(otmp);
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;
484 struct monst *mtmp;
485 int nowtyp;
486 boolean pool;
488 x = mon->mx;
489 y = mon->my;
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 */
495 if(mon->mconf) {
496 flag |= ALLOW_ALL;
497 flag &= ~NOTONL;
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) {
504 info[cnt] = 0;
505 if(nx == u.ux && ny == u.uy){
506 if(!(flag & ALLOW_U)) continue;
507 info[cnt] = ALLOW_U;
508 } else if((mtmp = m_at(nx,ny))){
509 if(!(flag & ALLOW_M)) continue;
510 info[cnt] = ALLOW_M;
511 if(mtmp->mtame){
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;
531 info[cnt] |= NOTONL;
533 /* we cannot avoid traps of an unknown kind */
534 { struct trap *ttmp = t_at(nx, ny);
535 int tt;
536 if(ttmp) {
537 tt = 1 << ttmp->ttyp;
538 if(mon->mtrapseen & tt){
539 if(!(flag & tt)) continue;
540 info[cnt] |= tt;
544 poss[cnt].x = nx;
545 poss[cnt].y = ny;
546 cnt++;
548 if(!cnt && pool && nowtyp != POOL) {
549 pool = FALSE;
550 goto nexttry;
552 return(cnt);
556 dist(int x, int y)
558 return((x-u.ux)*(x-u.ux) + (y-u.uy)*(y-u.uy));
561 void
562 poisoned(const char *string, const char *pname)
564 int i;
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.");
570 return;
572 i = rn2(10);
573 if(i == 0) {
574 u.uhp = -1;
575 pline("I am afraid the poison was deadly ...");
576 } else if(i <= 5) {
577 losestr(rn1(3,3));
578 } else {
579 losehp(rn1(10,6), pname);
581 if(u.uhp < 1) {
582 killer = pname;
583 done("died");
587 void
588 mondead(struct monst *mtmp)
590 relobj(mtmp,1);
591 unpmon(mtmp);
592 relmon(mtmp);
593 unstuck(mtmp);
594 if(mtmp->isshk) shkdead(mtmp);
595 if(mtmp->isgd) gddead();
596 #ifndef NOWORM
597 if(mtmp->wormno) wormdead(mtmp);
598 #endif /* NOWORM */
599 monfree(mtmp);
602 /* called when monster is moved to larger structure */
603 void
604 replmon(struct monst *mtmp, struct monst *mtmp2)
606 relmon(mtmp);
607 monfree(mtmp);
608 mtmp2->nmon = fmon;
609 fmon = mtmp2;
610 if(u.ustuck == mtmp) u.ustuck = mtmp2;
611 if(mtmp2->isshk) replshk(mtmp,mtmp2);
612 if(mtmp2->isgd) replgd(mtmp,mtmp2);
615 void
616 relmon(struct monst *mon)
618 struct monst *mtmp;
620 if(mon == fmon) fmon = fmon->nmon;
621 else {
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 */
631 void
632 monfree(struct monst *mtmp)
634 mtmp->nmon = fdmon;
635 fdmon = mtmp;
638 static void
639 dmonsfree(void)
641 struct monst *mtmp;
642 while((mtmp = fdmon)){
643 fdmon = mtmp->nmon;
644 free((char *) mtmp);
648 void
649 unstuck(struct monst *mtmp)
651 if(u.ustuck == mtmp) {
652 if(u.uswallow){
653 u.ux = mtmp->mx;
654 u.uy = mtmp->my;
655 u.uswallow = 0;
656 setsee();
657 docrt();
659 u.ustuck = 0;
663 void
664 killed(struct monst *mtmp)
666 #ifdef lint
667 #define NEW_SCORING
668 int tmp2;
669 #endif /* lint */
670 int tmp,nk,x,y;
671 struct permonst *mdat;
673 if(mtmp->cham) mtmp->data = PM_CHAMELEON;
674 mdat = mtmp->data;
675 if(Blind) pline("You destroy it!");
676 else {
677 pline("You destroy %s!",
678 mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
680 if(u.umconf) {
681 if(!Blind) pline("Your hands stop glowing blue.");
682 u.umconf = 0;
685 /* count killed monsters */
686 #define MAXMONNO 100
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) {
690 u.nr_killed[tmp]++;
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;
711 #ifdef NEW_SCORING
712 /* ------- recent addition: make nr of points decrease
713 when this is not the first of this kind */
714 { int ul = u.ulevel;
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;
723 tmp2 = ml - ul -1;
724 tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
725 if(!tmp) tmp = 1;
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);
732 flags.botl = 1;
733 while(u.ulevel < 14 && u.uexp >= newuexp()){
734 pline("Welcome to experience level %u.", ++u.ulevel);
735 tmp = rnd(10);
736 if(tmp < 3) tmp = rnd(10);
737 u.uhpmax += tmp;
738 u.uhp += tmp;
739 flags.botl = 1;
742 /* dispose of monster and make cadaver */
743 x = mtmp->mx; y = mtmp->my;
744 mondead(mtmp);
745 tmp = mdat->mlet;
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); */
750 stackobj(fobj);
751 } else
752 #ifndef NOWORM
753 if(tmp == 'w') {
754 mksobj_at(WORM_TOOTH, x, y);
755 stackobj(fobj);
756 } else
757 #endif /* NOWORM */
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);
764 if(cansee(x,y))
765 atl(x,y,obj2->olet);
766 stackobj(obj2);
770 void
771 kludge(const char *str, const char *arg)
773 if(Blind) {
774 if(*str == '%') pline(str,"It");
775 else pline(str,"it");
776 } else pline(str,arg);
779 void
780 rescham(void) /* force all chameleons to become normal */
782 struct monst *mtmp;
784 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
785 if(mtmp->cham) {
786 mtmp->cham = 0;
787 newcham(mtmp, PM_CHAMELEON);
791 /* make a chameleon look like a new monster */
792 /* returns 1 if the monster actually changed */
793 bool
794 newcham(struct monst *mtmp, struct permonst *mdat)
796 int mhp, hpn, hpd;
798 if(mdat == mtmp->data) return(0); /* still the same monster */
799 #ifndef NOWORM
800 if(mtmp->wormno) wormdead(mtmp); /* throw tail away */
801 #endif /* NOWORM */
802 if (u.ustuck == mtmp) {
803 if (u.uswallow) {
804 u.uswallow = 0;
805 u.uswldtim = 0;
806 mnexto (mtmp);
807 docrt ();
808 prme ();
810 u.ustuck = 0;
812 hpn = mtmp->mhp;
813 hpd = (mtmp->data->mlevel)*8;
814 if(!hpd) hpd = 4;
815 mtmp->data = mdat;
816 mhp = (mdat->mlevel)*8;
817 /* new hp: same fraction of max as before */
818 mtmp->mhp = 2 + (hpn*mhp)/hpd;
819 hpn = mtmp->mhpmax;
820 mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
821 mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
822 #ifndef NOWORM
823 if(mdat->mlet == 'w' && getwn(mtmp)) initworm(mtmp);
824 /* perhaps we should clear mtmp->mtame here? */
825 #endif /* NOWORM */
826 unpmon(mtmp); /* necessary for 'I' and to force pmon */
827 pmon(mtmp);
828 return(1);
831 /* Make monster mtmp next to you (if possible) */
832 void
833 mnexto(struct monst *mtmp)
835 coord mm;
836 mm = enexto(u.ux, u.uy);
837 mtmp->mx = mm.x;
838 mtmp->my = mm.y;
839 pmon(mtmp);
842 static bool
843 ishuman(struct monst *mtmp)
845 return(mtmp->data->mlet == '@');
848 void
849 setmangry(struct monst *mtmp)
851 if(!mtmp->mpeaceful) return;
852 if(mtmp->mtame) return;
853 mtmp->mpeaceful = 0;
854 if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
857 /* not one hundred procent correct: now a snake may hide under an
858 invisible object */
859 bool
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));