home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / HACKSRC.ZIP / MON.C < prev    next >
C/C++ Source or Header  |  1985-10-16  |  22KB  |  862 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2. /* mon.c - version 1.0.3 */
  3.  
  4. #include "hack.h"
  5. #include "mfndpos.h"
  6. #define    NULL    (char *) 0
  7. extern struct monst *makemon();
  8. extern struct obj *mkobj_at();
  9.  
  10. int warnlevel;        /* used by movemon and dochugw */
  11. long lastwarntime;
  12. int lastwarnlev;
  13. char *warnings[] = {
  14.     "white", "pink", "red", "ruby", "purple", "black"
  15. };
  16.  
  17. movemon()
  18. {
  19.     register struct monst *mtmp;
  20.     register int fr;
  21.  
  22.     warnlevel = 0;
  23.  
  24.     while(1) {
  25.         /* find a monster that we haven't treated yet */
  26.         /* note that mtmp or mtmp->nmon might get killed
  27.            while mtmp moves, so we cannot just walk down the
  28.            chain (even new monsters might get created!) */
  29.         for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  30.             if(mtmp->mlstmv < moves) goto next_mon;
  31.         /* treated all monsters */
  32.         break;
  33.  
  34.     next_mon:
  35.         mtmp->mlstmv = moves;
  36.  
  37.         /* most monsters drown in pools */
  38.         { boolean inpool, iseel;
  39.  
  40.           inpool = (levl[mtmp->mx][mtmp->my].typ == POOL);
  41.           iseel = (mtmp->data->mlet == ';');
  42.           if(inpool && !iseel) {
  43.             if(cansee(mtmp->mx,mtmp->my))
  44.                 pline("%s drowns.", Monnam(mtmp));
  45.             mondead(mtmp);
  46.             continue;
  47.           }
  48.         /* but eels have a difficult time outside */
  49.           if(iseel && !inpool) {
  50.             if(mtmp->mhp > 1) mtmp->mhp--;
  51.             mtmp->mflee = 1;
  52.             mtmp->mfleetim += 2;
  53.           }
  54.         }
  55.         if(mtmp->mblinded && !--mtmp->mblinded)
  56.             mtmp->mcansee = 1;
  57.         if(mtmp->mfleetim && !--mtmp->mfleetim)
  58.             mtmp->mflee = 0;
  59.         if(mtmp->mimic) continue;
  60.         if(mtmp->mspeed != MSLOW || !(moves%2)){
  61.             /* continue if the monster died fighting */
  62.             fr = -1;
  63.             if(Conflict && cansee(mtmp->mx,mtmp->my)
  64.                 && (fr = fightm(mtmp)) == 2)
  65.                 continue;
  66.             if(fr<0 && dochugw(mtmp))
  67.                 continue;
  68.         }
  69.         if(mtmp->mspeed == MFAST && dochugw(mtmp))
  70.             continue;
  71.     }
  72.  
  73.     warnlevel -= u.ulevel;
  74.     if(warnlevel >= SIZE(warnings))
  75.         warnlevel = SIZE(warnings)-1;
  76.     if(warnlevel >= 0)
  77.     if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
  78.         register char *rr;
  79.         switch(Warning & (LEFT_RING | RIGHT_RING)){
  80.         case LEFT_RING:
  81.         rr = "Your left ring glows";
  82.         break;
  83.         case RIGHT_RING:
  84.         rr = "Your right ring glows";
  85.         break;
  86.         case LEFT_RING | RIGHT_RING:
  87.         rr = "Both your rings glow";
  88.         break;
  89.         default:
  90.         rr = "Your fingertips glow";
  91.         break;
  92.         }
  93.         pline("%s %s!", rr, warnings[warnlevel]);
  94.         lastwarntime = moves;
  95.         lastwarnlev = warnlevel;
  96.     }
  97.  
  98.     dmonsfree();    /* remove all dead monsters */
  99. }
  100.  
  101. justswld(mtmp,name)
  102. register struct monst *mtmp;
  103. char *name;
  104. {
  105.  
  106.     mtmp->mx = u.ux;
  107.     mtmp->my = u.uy;
  108.     u.ustuck = mtmp;
  109.     pmon(mtmp);
  110.     kludge("%s swallows you!",name);
  111.     more();
  112.     seeoff(1);
  113.     u.uswallow = 1;
  114.     u.uswldtim = 0;
  115.     swallowed();
  116. }
  117.  
  118. youswld(mtmp,dam,die,name)
  119. register struct monst *mtmp;
  120. register dam,die;
  121. char *name;
  122. {
  123.     if(mtmp != u.ustuck) return;
  124.     kludge("%s digests you!",name);
  125.     u.uhp -= dam;
  126.     if(u.uswldtim++ >= die){    /* a3 */
  127.         pline("It totally digests you!");
  128.         u.uhp = -1;
  129.     }
  130.     if(u.uhp < 1) done_in_by(mtmp);
  131.     /* flags.botlx = 1;        /* should we show status line ? */
  132. }
  133.  
  134. dochugw(mtmp) register struct monst *mtmp; {
  135. register x = mtmp->mx;
  136. register y = mtmp->my;
  137. register d = dochug(mtmp);
  138. register dd;
  139.     if(!d)        /* monster still alive */
  140.     if(Warning)
  141.     if(!mtmp->mpeaceful)
  142.     if(mtmp->data->mlevel > warnlevel)
  143.     if((dd = dist(mtmp->mx,mtmp->my)) < dist(x,y))
  144.     if(dd < 100)
  145.     if(!canseemon(mtmp))
  146.         warnlevel = mtmp->data->mlevel;
  147.     return(d);
  148. }
  149.  
  150. /* returns 1 if monster died moving, 0 otherwise */
  151. dochug(mtmp)
  152. register struct monst *mtmp;
  153. {
  154.     register struct permonst *mdat;
  155.     register tmp, nearby, scared;
  156.  
  157.     if(mtmp->cham && !rn2(6))
  158.         (void) newcham(mtmp, &mons[dlevel+14+rn2(CMNUM-14-dlevel)]);
  159.     mdat = mtmp->data;
  160.     if(mdat->mlevel < 0)
  161.         panic("bad monster %c (%d)",mdat->mlet,mdat->mlevel);
  162.  
  163.     /* regenerate monsters */
  164.     if((!(moves%20) || index(MREGEN, mdat->mlet)) &&
  165.         mtmp->mhp < mtmp->mhpmax)
  166.         mtmp->mhp++;
  167.  
  168.     if(mtmp->mfroz) return(0); /* frozen monsters don't do anything */
  169.  
  170.     if(mtmp->msleep) {
  171.         /* wake up, or get out of here. */
  172.         /* ettins are hard to surprise */
  173.         /* Nymphs and Leprechauns do not easily wake up */
  174.         if(cansee(mtmp->mx,mtmp->my) &&
  175.             (!Stealth || (mdat->mlet == 'e' && rn2(10))) &&
  176.             (!index("NL",mdat->mlet) || !rn2(50)) &&
  177.             (Aggravate_monster || index("d1", mdat->mlet)
  178.                 || (!rn2(7) && !mtmp->mimic)))
  179.             mtmp->msleep = 0;
  180.         else return(0);
  181.     }
  182.  
  183.     /* not frozen or sleeping: wipe out texts written in the dust */
  184.     wipe_engr_at(mtmp->mx, mtmp->my, 1);
  185.  
  186.     /* confused monsters get unconfused with small probability */
  187.     if(mtmp->mconf && !rn2(50)) mtmp->mconf = 0;
  188.  
  189.     /* some monsters teleport */
  190.     if(mtmp->mflee && index("tNL", mdat->mlet) && !rn2(40)){
  191.         rloc(mtmp);
  192.         return(0);
  193.     }
  194.     if(mdat->mmove < rnd(6)) return(0);
  195.  
  196.     /* fleeing monsters might regain courage */
  197.     if(mtmp->mflee && !mtmp->mfleetim
  198.         && mtmp->mhp == mtmp->mhpmax && !rn2(25))
  199.         mtmp->mflee = 0;
  200.  
  201.     nearby = (dist(mtmp->mx, mtmp->my) < 3);
  202.     scared = (nearby && (sengr_at("Elbereth", u.ux, u.uy) ||
  203.             sobj_at(SCR_SCARE_MONSTER, u.ux, u.uy)));
  204.     if(scared && !mtmp->mflee) {
  205.         mtmp->mflee = 1;
  206.         mtmp->mfleetim = (rn2(7) ? rnd(10) : rnd(100));
  207.     }
  208.  
  209.     if(!nearby ||
  210.         mtmp->mflee ||
  211.         mtmp->mconf ||
  212.         (mtmp->minvis && !rn2(3)) ||
  213.         (index("BIuy", mdat->mlet) && !rn2(4)) ||
  214.         (mdat->mlet == 'L' && !u.ugold && (mtmp->mgold || rn2(2))) ||
  215.         (!mtmp->mcansee && !rn2(4)) ||
  216.         mtmp->mpeaceful
  217.        ) {
  218.         tmp = m_move(mtmp,0);    /* 2: monster died moving */
  219.         if(tmp == 2 || (tmp && mdat->mmove <= 12))
  220.             return(tmp == 2);
  221.     }
  222.  
  223.     if(!index("Ea", mdat->mlet) && nearby &&
  224.      !mtmp->mpeaceful && u.uhp > 0 && !scared) {
  225.         if(mhitu(mtmp))
  226.             return(1);    /* monster died (e.g. 'y' or 'F') */
  227.     }
  228.     /* extra movement for fast monsters */
  229.     if(mdat->mmove-12 > rnd(12)) tmp = m_move(mtmp,1);
  230.     return(tmp == 2);
  231. }
  232.  
  233. m_move(mtmp,after)
  234. register struct monst *mtmp;
  235. {
  236. #ifdef REGBUG
  237.     struct monst *mtmp2;
  238.     int nx,ny,omx,omy,appr,nearer,cnt,i,j;
  239. #else
  240.     register struct monst *mtmp2;
  241.     register nx,ny,omx,omy,appr,nearer,cnt,i,j;
  242. #endif REGBUG
  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.     register 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.       register int dd;
  349.       if(likegold){
  350.         register 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.         register 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) register struct monst *mtmp; {
  453. register 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) register struct monst *mtmp; {
  463. register 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. register struct monst *mon;
  479. coord poss[9];
  480. int info[9], flag;
  481. {
  482.     register int x,y,nx,ny,cnt = 0,ntyp;
  483.     register 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.         { register struct trap *ttmp = t_at(nx, ny);
  534.           register 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. register char *string, *pname;
  560. {
  561.     register 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. register 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. register 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. register struct monst *mon;
  614. {
  615.     register 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) register struct monst *mtmp; {
  629.     mtmp->nmon = fdmon;
  630.     fdmon = mtmp;
  631. }
  632.  
  633. dmonsfree(){
  634. register struct monst *mtmp;
  635.     while(mtmp = fdmon){
  636.         fdmon = mtmp->nmon;
  637.         free((char *) mtmp);
  638.     }
  639. }
  640.  
  641. unstuck(mtmp)
  642. register 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. register struct monst *mtmp;
  658. {
  659. #ifdef lint
  660. #define    NEW_SCORING
  661. #endif lint
  662.     register int tmp,tmp2,nk,x,y;
  663.     register struct permonst *mdat = mtmp->data;
  664.     extern long newuexp();
  665.  
  666.     if(mtmp->cham) mdat = PM_CHAMELEON;
  667.     if(Blind) pline("You destroy it!");
  668.     else {
  669.         pline("You destroy %s!",
  670.             mtmp->mtame ? amonnam(mtmp, "poor") : monnam(mtmp));
  671.     }
  672.     if(u.umconf) {
  673.         if(!Blind) pline("Your hands stop glowing blue.");
  674.         u.umconf = 0;
  675.     }
  676.  
  677.     /* count killed monsters */
  678. #define    MAXMONNO    100
  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[];
  683.         u.nr_killed[tmp]++;
  684.         if((nk = u.nr_killed[tmp]) > MAXMONNO &&
  685.         !index(fut_geno, mdat->mlet))
  686.             charcat(fut_geno,  mdat->mlet);
  687.     }
  688.  
  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;
  694.  
  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;
  703.  
  704. #ifdef NEW_SCORING
  705.     /* ------- recent addition: make nr of points decrease
  706.            when this is not the first of this kind */
  707.     { int ul = u.ulevel;
  708.       int ml = mdat->mlevel;
  709.  
  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;
  715.  
  716.     tmp2 = ml - ul -1;
  717.     tmp = (tmp + ((tmp2 < 0) ? 0 : 4<<tmp2))/nk;
  718.     if(!tmp) tmp = 1;
  719.     }
  720.     /* note: ul is not necessarily the future value of u.ulevel */
  721.     /* ------- end of recent valuation change ------- */
  722. #endif NEW_SCORING
  723.  
  724.     more_experienced(tmp,0);
  725.     flags.botl = 1;
  726.     while(u.ulevel < 14 && u.uexp >= newuexp()){
  727.         pline("Welcome to experience level %u.", ++u.ulevel);
  728.         tmp = rnd(10);
  729.         if(tmp < 3) tmp = rnd(10);
  730.         u.uhpmax += tmp;
  731.         u.uhp += tmp;
  732.         flags.botl = 1;
  733.     }
  734.  
  735.     /* dispose of monster and make cadaver */
  736.     x = mtmp->mx;    y = mtmp->my;
  737.     mondead(mtmp);
  738.     tmp = mdat->mlet;
  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); */
  743.         stackobj(fobj);
  744.     } else
  745. #ifndef NOWORM
  746.     if(tmp == 'w') {
  747.         mksobj_at(WORM_TOOTH, x, y);
  748.         stackobj(fobj);
  749.     } else
  750. #endif    NOWORM
  751.     if(!letter(tmp) || (!index("mw", tmp) && !rn2(3))) tmp = 0;
  752.  
  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);
  757.         if(cansee(x,y))
  758.             atl(x,y,obj2->olet);
  759.         stackobj(obj2);
  760.     }
  761. }
  762.  
  763. kludge(str,arg)
  764. register char *str,*arg;
  765. {
  766.     if(Blind) {
  767.         if(*str == '%') pline(str,"It");
  768.         else pline(str,"it");
  769.     } else pline(str,arg);
  770. }
  771.  
  772. rescham()    /* force all chameleons to become normal */
  773. {
  774.     register struct monst *mtmp;
  775.  
  776.     for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  777.         if(mtmp->cham) {
  778.             mtmp->cham = 0;
  779.             (void) newcham(mtmp, PM_CHAMELEON);
  780.         }
  781. }
  782.  
  783. #ifdef DGK
  784. /* Let the chameleons change again -dgk */
  785. restartcham()
  786. {
  787.     register struct monst *mtmp;
  788.  
  789.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  790.         if (mtmp->data->mlet == ':') 
  791.             mtmp->cham = 1;
  792. }
  793. #endif DGK
  794.  
  795. newcham(mtmp,mdat)    /* make a chameleon look like a new monster */
  796.             /* returns 1 if the monster actually changed */
  797. register struct monst *mtmp;
  798. register struct permonst *mdat;
  799. {
  800.     register mhp, hpn, hpd;
  801.  
  802.     if(mdat == mtmp->data) return(0);    /* still the same monster */
  803. #ifndef NOWORM
  804.     if(mtmp->wormno) wormdead(mtmp);    /* throw tail away */
  805. #endif NOWORM
  806.     hpn = mtmp->mhp;
  807.     hpd = (mtmp->data->mlevel)*8;
  808.     if(!hpd) hpd = 4;
  809.     mtmp->data = mdat;
  810.     mhp = (mdat->mlevel)*8;
  811.     /* new hp: same fraction of max as before */
  812.     mtmp->mhp = 2 + (hpn*mhp)/hpd;
  813.     hpn = mtmp->mhpmax;
  814.     mtmp->mhpmax = 2 + (hpn*mhp)/hpd;
  815.     mtmp->minvis = (mdat->mlet == 'I') ? 1 : 0;
  816. #ifdef DGK
  817.     /* only snakes and scorpions can hide under things -dgk */
  818.     mtmp->mhide = (mdat->mlet == 'S' || mdat->mlet == 's') ? 1 : 0;
  819.     if (!mtmp->mhide)
  820.         mtmp->mundetected = 0;
  821. #endif DGK
  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);
  829. }
  830.  
  831. mnexto(mtmp)    /* Make monster mtmp next to you (if possible) */
  832. struct monst *mtmp;
  833. {
  834.     extern coord enexto();
  835.     coord mm;
  836.     mm = enexto(u.ux, u.uy);
  837.     mtmp->mx = mm.x;
  838.     mtmp->my = mm.y;
  839.     pmon(mtmp);
  840. }
  841.  
  842. ishuman(mtmp) register struct monst *mtmp; {
  843.     return(mtmp->data->mlet == '@');
  844. }
  845.  
  846. setmangry(mtmp) register struct monst *mtmp; {
  847.     if(!mtmp->mpeaceful) return;
  848.     if(mtmp->mtame) return;
  849.     mtmp->mpeaceful = 0;
  850.     if(ishuman(mtmp)) pline("%s gets angry!", Monnam(mtmp));
  851. }
  852.  
  853. /* not one hundred procent correct: now a snake may hide under an
  854.    invisible object */
  855. canseemon(mtmp)
  856. register struct monst *mtmp;
  857. {
  858.     return((!mtmp->minvis || See_invisible)
  859.         && (!mtmp->mhide || !o_at(mtmp->mx,mtmp->my))
  860.         && cansee(mtmp->mx, mtmp->my));
  861. }
  862.